0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/hwmon.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/mutex.h>
0014 #include <linux/of.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/pwm.h>
0017 #include <linux/regulator/consumer.h>
0018 #include <linux/sysfs.h>
0019 #include <linux/thermal.h>
0020 #include <linux/timer.h>
0021
0022 #define MAX_PWM 255
0023
0024 struct pwm_fan_tach {
0025 int irq;
0026 atomic_t pulses;
0027 unsigned int rpm;
0028 u8 pulses_per_revolution;
0029 };
0030
0031 struct pwm_fan_ctx {
0032 struct mutex lock;
0033 struct pwm_device *pwm;
0034 struct pwm_state pwm_state;
0035 struct regulator *reg_en;
0036
0037 int tach_count;
0038 struct pwm_fan_tach *tachs;
0039 ktime_t sample_start;
0040 struct timer_list rpm_timer;
0041
0042 unsigned int pwm_value;
0043 unsigned int pwm_fan_state;
0044 unsigned int pwm_fan_max_state;
0045 unsigned int *pwm_fan_cooling_levels;
0046 struct thermal_cooling_device *cdev;
0047
0048 struct hwmon_chip_info info;
0049 struct hwmon_channel_info fan_channel;
0050 };
0051
0052
0053 static irqreturn_t pulse_handler(int irq, void *dev_id)
0054 {
0055 struct pwm_fan_tach *tach = dev_id;
0056
0057 atomic_inc(&tach->pulses);
0058
0059 return IRQ_HANDLED;
0060 }
0061
0062 static void sample_timer(struct timer_list *t)
0063 {
0064 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
0065 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
0066 int i;
0067
0068 if (delta) {
0069 for (i = 0; i < ctx->tach_count; i++) {
0070 struct pwm_fan_tach *tach = &ctx->tachs[i];
0071 int pulses;
0072
0073 pulses = atomic_read(&tach->pulses);
0074 atomic_sub(pulses, &tach->pulses);
0075 tach->rpm = (unsigned int)(pulses * 1000 * 60) /
0076 (tach->pulses_per_revolution * delta);
0077 }
0078
0079 ctx->sample_start = ktime_get();
0080 }
0081
0082 mod_timer(&ctx->rpm_timer, jiffies + HZ);
0083 }
0084
0085 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
0086 {
0087 unsigned long period;
0088 int ret = 0;
0089 struct pwm_state *state = &ctx->pwm_state;
0090
0091 mutex_lock(&ctx->lock);
0092 if (ctx->pwm_value == pwm)
0093 goto exit_set_pwm_err;
0094
0095 period = state->period;
0096 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
0097 state->enabled = pwm ? true : false;
0098
0099 ret = pwm_apply_state(ctx->pwm, state);
0100 if (!ret)
0101 ctx->pwm_value = pwm;
0102 exit_set_pwm_err:
0103 mutex_unlock(&ctx->lock);
0104 return ret;
0105 }
0106
0107 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
0108 {
0109 int i;
0110
0111 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
0112 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
0113 break;
0114
0115 ctx->pwm_fan_state = i;
0116 }
0117
0118 static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
0119 u32 attr, int channel, long val)
0120 {
0121 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
0122 int ret;
0123
0124 if (val < 0 || val > MAX_PWM)
0125 return -EINVAL;
0126
0127 ret = __set_pwm(ctx, val);
0128 if (ret)
0129 return ret;
0130
0131 pwm_fan_update_state(ctx, val);
0132 return 0;
0133 }
0134
0135 static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
0136 u32 attr, int channel, long *val)
0137 {
0138 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
0139
0140 switch (type) {
0141 case hwmon_pwm:
0142 *val = ctx->pwm_value;
0143 return 0;
0144
0145 case hwmon_fan:
0146 *val = ctx->tachs[channel].rpm;
0147 return 0;
0148
0149 default:
0150 return -ENOTSUPP;
0151 }
0152 }
0153
0154 static umode_t pwm_fan_is_visible(const void *data,
0155 enum hwmon_sensor_types type,
0156 u32 attr, int channel)
0157 {
0158 switch (type) {
0159 case hwmon_pwm:
0160 return 0644;
0161
0162 case hwmon_fan:
0163 return 0444;
0164
0165 default:
0166 return 0;
0167 }
0168 }
0169
0170 static const struct hwmon_ops pwm_fan_hwmon_ops = {
0171 .is_visible = pwm_fan_is_visible,
0172 .read = pwm_fan_read,
0173 .write = pwm_fan_write,
0174 };
0175
0176
0177 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
0178 unsigned long *state)
0179 {
0180 struct pwm_fan_ctx *ctx = cdev->devdata;
0181
0182 if (!ctx)
0183 return -EINVAL;
0184
0185 *state = ctx->pwm_fan_max_state;
0186
0187 return 0;
0188 }
0189
0190 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
0191 unsigned long *state)
0192 {
0193 struct pwm_fan_ctx *ctx = cdev->devdata;
0194
0195 if (!ctx)
0196 return -EINVAL;
0197
0198 *state = ctx->pwm_fan_state;
0199
0200 return 0;
0201 }
0202
0203 static int
0204 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
0205 {
0206 struct pwm_fan_ctx *ctx = cdev->devdata;
0207 int ret;
0208
0209 if (!ctx || (state > ctx->pwm_fan_max_state))
0210 return -EINVAL;
0211
0212 if (state == ctx->pwm_fan_state)
0213 return 0;
0214
0215 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
0216 if (ret) {
0217 dev_err(&cdev->device, "Cannot set pwm!\n");
0218 return ret;
0219 }
0220
0221 ctx->pwm_fan_state = state;
0222
0223 return ret;
0224 }
0225
0226 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
0227 .get_max_state = pwm_fan_get_max_state,
0228 .get_cur_state = pwm_fan_get_cur_state,
0229 .set_cur_state = pwm_fan_set_cur_state,
0230 };
0231
0232 static int pwm_fan_of_get_cooling_data(struct device *dev,
0233 struct pwm_fan_ctx *ctx)
0234 {
0235 struct device_node *np = dev->of_node;
0236 int num, i, ret;
0237
0238 if (!of_find_property(np, "cooling-levels", NULL))
0239 return 0;
0240
0241 ret = of_property_count_u32_elems(np, "cooling-levels");
0242 if (ret <= 0) {
0243 dev_err(dev, "Wrong data!\n");
0244 return ret ? : -EINVAL;
0245 }
0246
0247 num = ret;
0248 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
0249 GFP_KERNEL);
0250 if (!ctx->pwm_fan_cooling_levels)
0251 return -ENOMEM;
0252
0253 ret = of_property_read_u32_array(np, "cooling-levels",
0254 ctx->pwm_fan_cooling_levels, num);
0255 if (ret) {
0256 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
0257 return ret;
0258 }
0259
0260 for (i = 0; i < num; i++) {
0261 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
0262 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
0263 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
0264 return -EINVAL;
0265 }
0266 }
0267
0268 ctx->pwm_fan_max_state = num - 1;
0269
0270 return 0;
0271 }
0272
0273 static void pwm_fan_regulator_disable(void *data)
0274 {
0275 regulator_disable(data);
0276 }
0277
0278 static void pwm_fan_pwm_disable(void *__ctx)
0279 {
0280 struct pwm_fan_ctx *ctx = __ctx;
0281
0282 ctx->pwm_state.enabled = false;
0283 pwm_apply_state(ctx->pwm, &ctx->pwm_state);
0284 del_timer_sync(&ctx->rpm_timer);
0285 }
0286
0287 static int pwm_fan_probe(struct platform_device *pdev)
0288 {
0289 struct thermal_cooling_device *cdev;
0290 struct device *dev = &pdev->dev;
0291 struct pwm_fan_ctx *ctx;
0292 struct device *hwmon;
0293 int ret;
0294 const struct hwmon_channel_info **channels;
0295 u32 *fan_channel_config;
0296 int channel_count = 1;
0297 int i;
0298
0299 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
0300 if (!ctx)
0301 return -ENOMEM;
0302
0303 mutex_init(&ctx->lock);
0304
0305 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
0306 if (IS_ERR(ctx->pwm))
0307 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
0308
0309 platform_set_drvdata(pdev, ctx);
0310
0311 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
0312 if (IS_ERR(ctx->reg_en)) {
0313 if (PTR_ERR(ctx->reg_en) != -ENODEV)
0314 return PTR_ERR(ctx->reg_en);
0315
0316 ctx->reg_en = NULL;
0317 } else {
0318 ret = regulator_enable(ctx->reg_en);
0319 if (ret) {
0320 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
0321 return ret;
0322 }
0323 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
0324 ctx->reg_en);
0325 if (ret)
0326 return ret;
0327 }
0328
0329 pwm_init_state(ctx->pwm, &ctx->pwm_state);
0330
0331
0332
0333
0334
0335
0336 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
0337 dev_err(dev, "Configured period too big\n");
0338 return -EINVAL;
0339 }
0340
0341
0342 ret = __set_pwm(ctx, MAX_PWM);
0343 if (ret) {
0344 dev_err(dev, "Failed to configure PWM: %d\n", ret);
0345 return ret;
0346 }
0347 timer_setup(&ctx->rpm_timer, sample_timer, 0);
0348 ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
0349 if (ret)
0350 return ret;
0351
0352 ctx->tach_count = platform_irq_count(pdev);
0353 if (ctx->tach_count < 0)
0354 return dev_err_probe(dev, ctx->tach_count,
0355 "Could not get number of fan tachometer inputs\n");
0356 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count);
0357
0358 if (ctx->tach_count) {
0359 channel_count++;
0360
0361 ctx->tachs = devm_kcalloc(dev, ctx->tach_count,
0362 sizeof(struct pwm_fan_tach),
0363 GFP_KERNEL);
0364 if (!ctx->tachs)
0365 return -ENOMEM;
0366
0367 ctx->fan_channel.type = hwmon_fan;
0368 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1,
0369 sizeof(u32), GFP_KERNEL);
0370 if (!fan_channel_config)
0371 return -ENOMEM;
0372 ctx->fan_channel.config = fan_channel_config;
0373 }
0374
0375 channels = devm_kcalloc(dev, channel_count + 1,
0376 sizeof(struct hwmon_channel_info *), GFP_KERNEL);
0377 if (!channels)
0378 return -ENOMEM;
0379
0380 channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT);
0381
0382 for (i = 0; i < ctx->tach_count; i++) {
0383 struct pwm_fan_tach *tach = &ctx->tachs[i];
0384 u32 ppr = 2;
0385
0386 tach->irq = platform_get_irq(pdev, i);
0387 if (tach->irq == -EPROBE_DEFER)
0388 return tach->irq;
0389 if (tach->irq > 0) {
0390 ret = devm_request_irq(dev, tach->irq, pulse_handler, 0,
0391 pdev->name, tach);
0392 if (ret) {
0393 dev_err(dev,
0394 "Failed to request interrupt: %d\n",
0395 ret);
0396 return ret;
0397 }
0398 }
0399
0400 of_property_read_u32_index(dev->of_node,
0401 "pulses-per-revolution",
0402 i,
0403 &ppr);
0404 tach->pulses_per_revolution = ppr;
0405 if (!tach->pulses_per_revolution) {
0406 dev_err(dev, "pulses-per-revolution can't be zero.\n");
0407 return -EINVAL;
0408 }
0409
0410 fan_channel_config[i] = HWMON_F_INPUT;
0411
0412 dev_dbg(dev, "tach%d: irq=%d, pulses_per_revolution=%d\n",
0413 i, tach->irq, tach->pulses_per_revolution);
0414 }
0415
0416 if (ctx->tach_count > 0) {
0417 ctx->sample_start = ktime_get();
0418 mod_timer(&ctx->rpm_timer, jiffies + HZ);
0419
0420 channels[1] = &ctx->fan_channel;
0421 }
0422
0423 ctx->info.ops = &pwm_fan_hwmon_ops;
0424 ctx->info.info = channels;
0425
0426 hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
0427 ctx, &ctx->info, NULL);
0428 if (IS_ERR(hwmon)) {
0429 dev_err(dev, "Failed to register hwmon device\n");
0430 return PTR_ERR(hwmon);
0431 }
0432
0433 ret = pwm_fan_of_get_cooling_data(dev, ctx);
0434 if (ret)
0435 return ret;
0436
0437 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
0438 if (IS_ENABLED(CONFIG_THERMAL)) {
0439 cdev = devm_thermal_of_cooling_device_register(dev,
0440 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
0441 if (IS_ERR(cdev)) {
0442 ret = PTR_ERR(cdev);
0443 dev_err(dev,
0444 "Failed to register pwm-fan as cooling device: %d\n",
0445 ret);
0446 return ret;
0447 }
0448 ctx->cdev = cdev;
0449 }
0450
0451 return 0;
0452 }
0453
0454 static int pwm_fan_disable(struct device *dev)
0455 {
0456 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
0457 int ret;
0458
0459 if (ctx->pwm_value) {
0460
0461 struct pwm_state state = ctx->pwm_state;
0462
0463 state.duty_cycle = 0;
0464 state.enabled = false;
0465 ret = pwm_apply_state(ctx->pwm, &state);
0466 if (ret < 0)
0467 return ret;
0468 }
0469
0470 if (ctx->reg_en) {
0471 ret = regulator_disable(ctx->reg_en);
0472 if (ret) {
0473 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
0474 return ret;
0475 }
0476 }
0477
0478 return 0;
0479 }
0480
0481 static void pwm_fan_shutdown(struct platform_device *pdev)
0482 {
0483 pwm_fan_disable(&pdev->dev);
0484 }
0485
0486 #ifdef CONFIG_PM_SLEEP
0487 static int pwm_fan_suspend(struct device *dev)
0488 {
0489 return pwm_fan_disable(dev);
0490 }
0491
0492 static int pwm_fan_resume(struct device *dev)
0493 {
0494 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
0495 int ret;
0496
0497 if (ctx->reg_en) {
0498 ret = regulator_enable(ctx->reg_en);
0499 if (ret) {
0500 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
0501 return ret;
0502 }
0503 }
0504
0505 if (ctx->pwm_value == 0)
0506 return 0;
0507
0508 return pwm_apply_state(ctx->pwm, &ctx->pwm_state);
0509 }
0510 #endif
0511
0512 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
0513
0514 static const struct of_device_id of_pwm_fan_match[] = {
0515 { .compatible = "pwm-fan", },
0516 {},
0517 };
0518 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
0519
0520 static struct platform_driver pwm_fan_driver = {
0521 .probe = pwm_fan_probe,
0522 .shutdown = pwm_fan_shutdown,
0523 .driver = {
0524 .name = "pwm-fan",
0525 .pm = &pwm_fan_pm,
0526 .of_match_table = of_pwm_fan_match,
0527 },
0528 };
0529
0530 module_platform_driver(pwm_fan_driver);
0531
0532 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
0533 MODULE_ALIAS("platform:pwm-fan");
0534 MODULE_DESCRIPTION("PWM FAN driver");
0535 MODULE_LICENSE("GPL");