1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 *
7 * Author: Kamil Debski <k.debski@samsung.com>
8 */
9
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/pwm.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/sysfs.h>
20 #include <linux/thermal.h>
21 #include <linux/timer.h>
22 #include <soc/rockchip/rockchip_system_monitor.h>
23
24 #define MAX_PWM 255
25
26 struct thermal_trips {
27 int temp;
28 int state;
29 };
30
31 struct pwm_fan_ctx {
32 struct mutex lock;
33 struct pwm_device *pwm;
34 struct regulator *reg_en;
35
36 int irq;
37 atomic_t pulses;
38 unsigned int rpm;
39 u8 pulses_per_revolution;
40 ktime_t sample_start;
41 struct timer_list rpm_timer;
42
43 unsigned int pwm_value;
44 unsigned int pwm_fan_state;
45 unsigned int pwm_fan_max_state;
46 unsigned int *pwm_fan_cooling_levels;
47 struct thermal_cooling_device *cdev;
48 struct notifier_block thermal_nb;
49 struct thermal_trips *thermal_trips;
50 bool thermal_notifier_is_ok;
51 };
52
53 /* This handler assumes self resetting edge triggered interrupt. */
pulse_handler(int irq,void * dev_id)54 static irqreturn_t pulse_handler(int irq, void *dev_id)
55 {
56 struct pwm_fan_ctx *ctx = dev_id;
57
58 atomic_inc(&ctx->pulses);
59
60 return IRQ_HANDLED;
61 }
62
sample_timer(struct timer_list * t)63 static void sample_timer(struct timer_list *t)
64 {
65 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
66 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
67 int pulses;
68
69 if (delta) {
70 pulses = atomic_read(&ctx->pulses);
71 atomic_sub(pulses, &ctx->pulses);
72 ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
73 (ctx->pulses_per_revolution * delta);
74
75 ctx->sample_start = ktime_get();
76 }
77
78 mod_timer(&ctx->rpm_timer, jiffies + HZ);
79 }
80
__set_pwm(struct pwm_fan_ctx * ctx,unsigned long pwm)81 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
82 {
83 unsigned long period;
84 int ret = 0;
85 struct pwm_state state = { };
86
87 mutex_lock(&ctx->lock);
88 if (ctx->pwm_value == pwm)
89 goto exit_set_pwm_err;
90
91 pwm_init_state(ctx->pwm, &state);
92 period = ctx->pwm->args.period;
93 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
94 state.enabled = pwm ? true : false;
95
96 ret = pwm_apply_state(ctx->pwm, &state);
97 if (!ret)
98 ctx->pwm_value = pwm;
99 exit_set_pwm_err:
100 mutex_unlock(&ctx->lock);
101 return ret;
102 }
103
pwm_fan_update_state(struct pwm_fan_ctx * ctx,unsigned long pwm)104 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
105 {
106 int i;
107
108 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
109 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
110 break;
111
112 ctx->pwm_fan_state = i;
113 }
114
pwm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)115 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
116 const char *buf, size_t count)
117 {
118 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
119 unsigned long pwm;
120 int ret;
121
122 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
123 return -EINVAL;
124
125 ret = __set_pwm(ctx, pwm);
126 if (ret)
127 return ret;
128
129 pwm_fan_update_state(ctx, pwm);
130 return count;
131 }
132
pwm_show(struct device * dev,struct device_attribute * attr,char * buf)133 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
134 char *buf)
135 {
136 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
137
138 return sprintf(buf, "%u\n", ctx->pwm_value);
139 }
140
rpm_show(struct device * dev,struct device_attribute * attr,char * buf)141 static ssize_t rpm_show(struct device *dev,
142 struct device_attribute *attr, char *buf)
143 {
144 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
145
146 return sprintf(buf, "%u\n", ctx->rpm);
147 }
148
149 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
150 static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
151
152 static struct attribute *pwm_fan_attrs[] = {
153 &sensor_dev_attr_pwm1.dev_attr.attr,
154 &sensor_dev_attr_fan1_input.dev_attr.attr,
155 NULL,
156 };
157
pwm_fan_attrs_visible(struct kobject * kobj,struct attribute * a,int n)158 static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
159 int n)
160 {
161 struct device *dev = container_of(kobj, struct device, kobj);
162 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
163
164 /* Hide fan_input in case no interrupt is available */
165 if (n == 1 && ctx->irq <= 0)
166 return 0;
167
168 return a->mode;
169 }
170
171 static const struct attribute_group pwm_fan_group = {
172 .attrs = pwm_fan_attrs,
173 .is_visible = pwm_fan_attrs_visible,
174 };
175
176 static const struct attribute_group *pwm_fan_groups[] = {
177 &pwm_fan_group,
178 NULL,
179 };
180
181 /* thermal cooling device callbacks */
pwm_fan_get_max_state(struct thermal_cooling_device * cdev,unsigned long * state)182 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
183 unsigned long *state)
184 {
185 struct pwm_fan_ctx *ctx = cdev->devdata;
186
187 if (!ctx)
188 return -EINVAL;
189
190 *state = ctx->pwm_fan_max_state;
191
192 return 0;
193 }
194
pwm_fan_get_cur_state(struct thermal_cooling_device * cdev,unsigned long * state)195 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
196 unsigned long *state)
197 {
198 struct pwm_fan_ctx *ctx = cdev->devdata;
199
200 if (!ctx)
201 return -EINVAL;
202
203 *state = ctx->pwm_fan_state;
204
205 return 0;
206 }
207
208 static int
pwm_fan_set_cur_state(struct thermal_cooling_device * cdev,unsigned long state)209 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
210 {
211 struct pwm_fan_ctx *ctx = cdev->devdata;
212 int ret;
213
214 if (!ctx || (state > ctx->pwm_fan_max_state))
215 return -EINVAL;
216
217 if (state == ctx->pwm_fan_state)
218 return 0;
219
220 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
221 if (ret) {
222 dev_err(&cdev->device, "Cannot set pwm!\n");
223 return ret;
224 }
225
226 ctx->pwm_fan_state = state;
227
228 return ret;
229 }
230
231 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
232 .get_max_state = pwm_fan_get_max_state,
233 .get_cur_state = pwm_fan_get_cur_state,
234 .set_cur_state = pwm_fan_set_cur_state,
235 };
236
pwm_fan_of_get_cooling_data(struct device * dev,struct pwm_fan_ctx * ctx)237 static int pwm_fan_of_get_cooling_data(struct device *dev,
238 struct pwm_fan_ctx *ctx)
239 {
240 struct device_node *np = dev->of_node;
241 int num, i, ret;
242
243 if (!of_find_property(np, "cooling-levels", NULL))
244 return 0;
245
246 ret = of_property_count_u32_elems(np, "cooling-levels");
247 if (ret <= 0) {
248 dev_err(dev, "Wrong data!\n");
249 return ret ? : -EINVAL;
250 }
251
252 num = ret;
253 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
254 GFP_KERNEL);
255 if (!ctx->pwm_fan_cooling_levels)
256 return -ENOMEM;
257
258 ret = of_property_read_u32_array(np, "cooling-levels",
259 ctx->pwm_fan_cooling_levels, num);
260 if (ret) {
261 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
262 return ret;
263 }
264
265 for (i = 0; i < num; i++) {
266 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
267 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
268 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
269 return -EINVAL;
270 }
271 }
272
273 ctx->pwm_fan_max_state = num - 1;
274
275 return 0;
276 }
277
pwm_fan_regulator_disable(void * data)278 static void pwm_fan_regulator_disable(void *data)
279 {
280 regulator_disable(data);
281 }
282
pwm_fan_pwm_disable(void * __ctx)283 static void pwm_fan_pwm_disable(void *__ctx)
284 {
285 struct pwm_fan_ctx *ctx = __ctx;
286 pwm_disable(ctx->pwm);
287 del_timer_sync(&ctx->rpm_timer);
288 }
289
pwm_fan_get_thermal_trips(struct device * dev,char * porp_name,struct thermal_trips ** trips)290 static int pwm_fan_get_thermal_trips(struct device *dev, char *porp_name,
291 struct thermal_trips **trips)
292 {
293 struct device_node *np = dev->of_node;
294 struct thermal_trips *thermal_trips;
295 const struct property *prop;
296 int count, i;
297
298 prop = of_find_property(np, porp_name, NULL);
299 if (!prop)
300 return -EINVAL;
301 if (!prop->value)
302 return -ENODATA;
303 count = of_property_count_u32_elems(np, porp_name);
304 if (count < 0)
305 return -EINVAL;
306 if (count % 2)
307 return -EINVAL;
308 thermal_trips = devm_kzalloc(dev,
309 sizeof(*thermal_trips) * (count / 2 + 1),
310 GFP_KERNEL);
311 if (!thermal_trips)
312 return -ENOMEM;
313
314 for (i = 0; i < count / 2; i++) {
315 of_property_read_u32_index(np, porp_name, 2 * i,
316 &thermal_trips[i].temp);
317 of_property_read_u32_index(np, porp_name, 2 * i + 1,
318 &thermal_trips[i].state);
319 }
320 thermal_trips[i].temp = 0;
321 thermal_trips[i].state = INT_MAX;
322
323 *trips = thermal_trips;
324
325 return 0;
326 }
327
pwm_fan_temp_to_state(struct pwm_fan_ctx * ctx,int temp)328 static int pwm_fan_temp_to_state(struct pwm_fan_ctx *ctx, int temp)
329 {
330 struct thermal_trips *trips = ctx->thermal_trips;
331 int i, state = 0;
332
333 for (i = 0; trips[i].state != INT_MAX; i++) {
334 if (temp >= trips[i].temp)
335 state = trips[i].state;
336 }
337
338 return state;
339 }
340
pwm_fan_thermal_notifier_call(struct notifier_block * nb,unsigned long event,void * data)341 static int pwm_fan_thermal_notifier_call(struct notifier_block *nb,
342 unsigned long event, void *data)
343 {
344 struct pwm_fan_ctx *ctx = container_of(nb, struct pwm_fan_ctx, thermal_nb);
345 struct system_monitor_event_data *event_data = data;
346 int state, ret;
347
348 if (event != SYSTEM_MONITOR_CHANGE_TEMP)
349 return NOTIFY_OK;
350
351 state = pwm_fan_temp_to_state(ctx, event_data->temp);
352 if (state > ctx->pwm_fan_max_state)
353 return NOTIFY_BAD;
354 if (state == ctx->pwm_fan_state)
355 return NOTIFY_OK;
356
357 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
358 if (ret)
359 return NOTIFY_BAD;
360
361 ctx->pwm_fan_state = state;
362
363 return NOTIFY_OK;
364 }
365
pwm_fan_register_thermal_notifier(struct device * dev,struct pwm_fan_ctx * ctx)366 static int pwm_fan_register_thermal_notifier(struct device *dev,
367 struct pwm_fan_ctx *ctx)
368 {
369 if (pwm_fan_get_thermal_trips(dev, "rockchip,temp-trips",
370 &ctx->thermal_trips))
371 return -EINVAL;
372
373 ctx->thermal_nb.notifier_call = pwm_fan_thermal_notifier_call;
374
375 return rockchip_system_monitor_register_notifier(&ctx->thermal_nb);
376 }
377
pwm_fan_probe(struct platform_device * pdev)378 static int pwm_fan_probe(struct platform_device *pdev)
379 {
380 struct thermal_cooling_device *cdev;
381 struct device *dev = &pdev->dev;
382 struct pwm_fan_ctx *ctx;
383 struct device *hwmon;
384 int ret;
385 struct pwm_state state = { };
386 u32 ppr = 2;
387
388 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
389 if (!ctx)
390 return -ENOMEM;
391
392 mutex_init(&ctx->lock);
393
394 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
395 if (IS_ERR(ctx->pwm))
396 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
397
398 platform_set_drvdata(pdev, ctx);
399
400 ctx->irq = platform_get_irq_optional(pdev, 0);
401 if (ctx->irq == -EPROBE_DEFER)
402 return ctx->irq;
403
404 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
405 if (IS_ERR(ctx->reg_en)) {
406 if (PTR_ERR(ctx->reg_en) != -ENODEV)
407 return PTR_ERR(ctx->reg_en);
408
409 ctx->reg_en = NULL;
410 } else {
411 ret = regulator_enable(ctx->reg_en);
412 if (ret) {
413 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
414 return ret;
415 }
416 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
417 ctx->reg_en);
418 if (ret)
419 return ret;
420 }
421
422 ctx->pwm_value = MAX_PWM;
423
424 pwm_init_state(ctx->pwm, &state);
425 /*
426 * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
427 * long. Check this here to prevent the fan running at a too low
428 * frequency.
429 */
430 if (state.period > ULONG_MAX / MAX_PWM + 1) {
431 dev_err(dev, "Configured period too big\n");
432 return -EINVAL;
433 }
434
435 /* Set duty cycle to maximum allowed and enable PWM output */
436 state.duty_cycle = ctx->pwm->args.period - 1;
437 state.enabled = true;
438
439 ret = pwm_apply_state(ctx->pwm, &state);
440 if (ret) {
441 dev_err(dev, "Failed to configure PWM: %d\n", ret);
442 return ret;
443 }
444 timer_setup(&ctx->rpm_timer, sample_timer, 0);
445 ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
446 if (ret)
447 return ret;
448
449 of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr);
450 ctx->pulses_per_revolution = ppr;
451 if (!ctx->pulses_per_revolution) {
452 dev_err(dev, "pulses-per-revolution can't be zero.\n");
453 return -EINVAL;
454 }
455
456 if (ctx->irq > 0) {
457 ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
458 pdev->name, ctx);
459 if (ret) {
460 dev_err(dev, "Failed to request interrupt: %d\n", ret);
461 return ret;
462 }
463 ctx->sample_start = ktime_get();
464 mod_timer(&ctx->rpm_timer, jiffies + HZ);
465 }
466
467 hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
468 ctx, pwm_fan_groups);
469 if (IS_ERR(hwmon)) {
470 dev_err(dev, "Failed to register hwmon device\n");
471 return PTR_ERR(hwmon);
472 }
473
474 ret = pwm_fan_of_get_cooling_data(dev, ctx);
475 if (ret)
476 return ret;
477
478 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
479 if (IS_REACHABLE(CONFIG_ROCKCHIP_SYSTEM_MONITOR) &&
480 of_find_property(dev->of_node, "rockchip,temp-trips", NULL)) {
481 ret = pwm_fan_register_thermal_notifier(dev, ctx);
482 if (ret)
483 dev_err(dev, "Failed to register thermal notifier: %d\n", ret);
484 else
485 ctx->thermal_notifier_is_ok = true;
486 return 0;
487 }
488 if (IS_ENABLED(CONFIG_THERMAL)) {
489 cdev = devm_thermal_of_cooling_device_register(dev,
490 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
491 if (IS_ERR(cdev)) {
492 ret = PTR_ERR(cdev);
493 dev_err(dev,
494 "Failed to register pwm-fan as cooling device: %d\n",
495 ret);
496 return ret;
497 }
498 ctx->cdev = cdev;
499 thermal_cdev_update(cdev);
500 }
501
502 return 0;
503 }
504
pwm_fan_disable(struct device * dev)505 static int pwm_fan_disable(struct device *dev)
506 {
507 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
508 struct pwm_args args;
509 int ret;
510
511 pwm_get_args(ctx->pwm, &args);
512
513 if (ctx->pwm_value || ctx->thermal_notifier_is_ok) {
514 ret = pwm_config(ctx->pwm, 0, args.period);
515 if (ret < 0)
516 return ret;
517
518 pwm_disable(ctx->pwm);
519 }
520
521 if (ctx->reg_en) {
522 ret = regulator_disable(ctx->reg_en);
523 if (ret) {
524 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
525 return ret;
526 }
527 }
528
529 return 0;
530 }
531
pwm_fan_shutdown(struct platform_device * pdev)532 static void pwm_fan_shutdown(struct platform_device *pdev)
533 {
534 pwm_fan_disable(&pdev->dev);
535 }
536
537 #ifdef CONFIG_PM_SLEEP
pwm_fan_suspend(struct device * dev)538 static int pwm_fan_suspend(struct device *dev)
539 {
540 return pwm_fan_disable(dev);
541 }
542
pwm_fan_resume(struct device * dev)543 static int pwm_fan_resume(struct device *dev)
544 {
545 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
546 struct pwm_args pargs;
547 unsigned long duty;
548 int ret;
549
550 if (ctx->reg_en) {
551 ret = regulator_enable(ctx->reg_en);
552 if (ret) {
553 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
554 return ret;
555 }
556 }
557
558 if (ctx->pwm_value == 0 && !ctx->thermal_notifier_is_ok)
559 return 0;
560
561 pwm_get_args(ctx->pwm, &pargs);
562 duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
563 ret = pwm_config(ctx->pwm, duty, pargs.period);
564 if (ret)
565 return ret;
566 return pwm_enable(ctx->pwm);
567 }
568 #endif
569
570 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
571
572 static const struct of_device_id of_pwm_fan_match[] = {
573 { .compatible = "pwm-fan", },
574 {},
575 };
576 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
577
578 static struct platform_driver pwm_fan_driver = {
579 .probe = pwm_fan_probe,
580 .shutdown = pwm_fan_shutdown,
581 .driver = {
582 .name = "pwm-fan",
583 .pm = &pwm_fan_pm,
584 .of_match_table = of_pwm_fan_match,
585 },
586 };
587
588 module_platform_driver(pwm_fan_driver);
589
590 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
591 MODULE_ALIAS("platform:pwm-fan");
592 MODULE_DESCRIPTION("PWM FAN driver");
593 MODULE_LICENSE("GPL");
594