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