xref: /openbmc/linux/drivers/hwmon/gpio-fan.c (revision a2cab953)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
4  *
5  * Copyright (C) 2010 LaCie
6  *
7  * Author: Simon Guinot <sguinot@lacie.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/hwmon.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/thermal.h>
23 
24 struct gpio_fan_speed {
25 	int rpm;
26 	int ctrl_val;
27 };
28 
29 struct gpio_fan_data {
30 	struct device		*dev;
31 	struct device		*hwmon_dev;
32 	/* Cooling device if any */
33 	struct thermal_cooling_device *cdev;
34 	struct mutex		lock; /* lock GPIOs operations. */
35 	int			num_gpios;
36 	struct gpio_desc	**gpios;
37 	int			num_speed;
38 	struct gpio_fan_speed	*speed;
39 	int			speed_index;
40 	int			resume_speed;
41 	bool			pwm_enable;
42 	struct gpio_desc	*alarm_gpio;
43 	struct work_struct	alarm_work;
44 };
45 
46 /*
47  * Alarm GPIO.
48  */
49 
50 static void fan_alarm_notify(struct work_struct *ws)
51 {
52 	struct gpio_fan_data *fan_data =
53 		container_of(ws, struct gpio_fan_data, alarm_work);
54 
55 	sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm");
56 	kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE);
57 }
58 
59 static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
60 {
61 	struct gpio_fan_data *fan_data = dev_id;
62 
63 	schedule_work(&fan_data->alarm_work);
64 
65 	return IRQ_NONE;
66 }
67 
68 static ssize_t fan1_alarm_show(struct device *dev,
69 			       struct device_attribute *attr, char *buf)
70 {
71 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
72 
73 	return sprintf(buf, "%d\n",
74 		       gpiod_get_value_cansleep(fan_data->alarm_gpio));
75 }
76 
77 static DEVICE_ATTR_RO(fan1_alarm);
78 
79 static int fan_alarm_init(struct gpio_fan_data *fan_data)
80 {
81 	int alarm_irq;
82 	struct device *dev = fan_data->dev;
83 
84 	/*
85 	 * If the alarm GPIO don't support interrupts, just leave
86 	 * without initializing the fail notification support.
87 	 */
88 	alarm_irq = gpiod_to_irq(fan_data->alarm_gpio);
89 	if (alarm_irq <= 0)
90 		return 0;
91 
92 	INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
93 	irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
94 	return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
95 				IRQF_SHARED, "GPIO fan alarm", fan_data);
96 }
97 
98 /*
99  * Control GPIOs.
100  */
101 
102 /* Must be called with fan_data->lock held, except during initialization. */
103 static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
104 {
105 	int i;
106 
107 	for (i = 0; i < fan_data->num_gpios; i++)
108 		gpiod_set_value_cansleep(fan_data->gpios[i],
109 					 (ctrl_val >> i) & 1);
110 }
111 
112 static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
113 {
114 	int i;
115 	int ctrl_val = 0;
116 
117 	for (i = 0; i < fan_data->num_gpios; i++) {
118 		int value;
119 
120 		value = gpiod_get_value_cansleep(fan_data->gpios[i]);
121 		ctrl_val |= (value << i);
122 	}
123 	return ctrl_val;
124 }
125 
126 /* Must be called with fan_data->lock held, except during initialization. */
127 static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
128 {
129 	if (fan_data->speed_index == speed_index)
130 		return;
131 
132 	__set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
133 	fan_data->speed_index = speed_index;
134 }
135 
136 static int get_fan_speed_index(struct gpio_fan_data *fan_data)
137 {
138 	int ctrl_val = __get_fan_ctrl(fan_data);
139 	int i;
140 
141 	for (i = 0; i < fan_data->num_speed; i++)
142 		if (fan_data->speed[i].ctrl_val == ctrl_val)
143 			return i;
144 
145 	dev_warn(fan_data->dev,
146 		 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
147 
148 	return -ENODEV;
149 }
150 
151 static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
152 {
153 	struct gpio_fan_speed *speed = fan_data->speed;
154 	int i;
155 
156 	for (i = 0; i < fan_data->num_speed; i++)
157 		if (speed[i].rpm >= rpm)
158 			return i;
159 
160 	return fan_data->num_speed - 1;
161 }
162 
163 static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
164 			 char *buf)
165 {
166 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
167 	u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
168 
169 	return sprintf(buf, "%d\n", pwm);
170 }
171 
172 static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
173 			  const char *buf, size_t count)
174 {
175 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
176 	unsigned long pwm;
177 	int speed_index;
178 	int ret = count;
179 
180 	if (kstrtoul(buf, 10, &pwm) || pwm > 255)
181 		return -EINVAL;
182 
183 	mutex_lock(&fan_data->lock);
184 
185 	if (!fan_data->pwm_enable) {
186 		ret = -EPERM;
187 		goto exit_unlock;
188 	}
189 
190 	speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
191 	set_fan_speed(fan_data, speed_index);
192 
193 exit_unlock:
194 	mutex_unlock(&fan_data->lock);
195 
196 	return ret;
197 }
198 
199 static ssize_t pwm1_enable_show(struct device *dev,
200 				struct device_attribute *attr, char *buf)
201 {
202 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
203 
204 	return sprintf(buf, "%d\n", fan_data->pwm_enable);
205 }
206 
207 static ssize_t pwm1_enable_store(struct device *dev,
208 				 struct device_attribute *attr,
209 				 const char *buf, size_t count)
210 {
211 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
212 	unsigned long val;
213 
214 	if (kstrtoul(buf, 10, &val) || val > 1)
215 		return -EINVAL;
216 
217 	if (fan_data->pwm_enable == val)
218 		return count;
219 
220 	mutex_lock(&fan_data->lock);
221 
222 	fan_data->pwm_enable = val;
223 
224 	/* Disable manual control mode: set fan at full speed. */
225 	if (val == 0)
226 		set_fan_speed(fan_data, fan_data->num_speed - 1);
227 
228 	mutex_unlock(&fan_data->lock);
229 
230 	return count;
231 }
232 
233 static ssize_t pwm1_mode_show(struct device *dev,
234 			      struct device_attribute *attr, char *buf)
235 {
236 	return sprintf(buf, "0\n");
237 }
238 
239 static ssize_t fan1_min_show(struct device *dev,
240 			     struct device_attribute *attr, char *buf)
241 {
242 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
243 
244 	return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
245 }
246 
247 static ssize_t fan1_max_show(struct device *dev,
248 			     struct device_attribute *attr, char *buf)
249 {
250 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
251 
252 	return sprintf(buf, "%d\n",
253 		       fan_data->speed[fan_data->num_speed - 1].rpm);
254 }
255 
256 static ssize_t fan1_input_show(struct device *dev,
257 			       struct device_attribute *attr, char *buf)
258 {
259 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
260 
261 	return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
262 }
263 
264 static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
265 		       const char *buf, size_t count)
266 {
267 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
268 	unsigned long rpm;
269 	int ret = count;
270 
271 	if (kstrtoul(buf, 10, &rpm))
272 		return -EINVAL;
273 
274 	mutex_lock(&fan_data->lock);
275 
276 	if (!fan_data->pwm_enable) {
277 		ret = -EPERM;
278 		goto exit_unlock;
279 	}
280 
281 	set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
282 
283 exit_unlock:
284 	mutex_unlock(&fan_data->lock);
285 
286 	return ret;
287 }
288 
289 static DEVICE_ATTR_RW(pwm1);
290 static DEVICE_ATTR_RW(pwm1_enable);
291 static DEVICE_ATTR_RO(pwm1_mode);
292 static DEVICE_ATTR_RO(fan1_min);
293 static DEVICE_ATTR_RO(fan1_max);
294 static DEVICE_ATTR_RO(fan1_input);
295 static DEVICE_ATTR(fan1_target, 0644, fan1_input_show, set_rpm);
296 
297 static umode_t gpio_fan_is_visible(struct kobject *kobj,
298 				   struct attribute *attr, int index)
299 {
300 	struct device *dev = kobj_to_dev(kobj);
301 	struct gpio_fan_data *data = dev_get_drvdata(dev);
302 
303 	if (index == 0 && !data->alarm_gpio)
304 		return 0;
305 	if (index > 0 && !data->gpios)
306 		return 0;
307 
308 	return attr->mode;
309 }
310 
311 static struct attribute *gpio_fan_attributes[] = {
312 	&dev_attr_fan1_alarm.attr,		/* 0 */
313 	&dev_attr_pwm1.attr,			/* 1 */
314 	&dev_attr_pwm1_enable.attr,
315 	&dev_attr_pwm1_mode.attr,
316 	&dev_attr_fan1_input.attr,
317 	&dev_attr_fan1_target.attr,
318 	&dev_attr_fan1_min.attr,
319 	&dev_attr_fan1_max.attr,
320 	NULL
321 };
322 
323 static const struct attribute_group gpio_fan_group = {
324 	.attrs = gpio_fan_attributes,
325 	.is_visible = gpio_fan_is_visible,
326 };
327 
328 static const struct attribute_group *gpio_fan_groups[] = {
329 	&gpio_fan_group,
330 	NULL
331 };
332 
333 static int fan_ctrl_init(struct gpio_fan_data *fan_data)
334 {
335 	int num_gpios = fan_data->num_gpios;
336 	struct gpio_desc **gpios = fan_data->gpios;
337 	int i, err;
338 
339 	for (i = 0; i < num_gpios; i++) {
340 		/*
341 		 * The GPIO descriptors were retrieved with GPIOD_ASIS so here
342 		 * we set the GPIO into output mode, carefully preserving the
343 		 * current value by setting it to whatever it is already set
344 		 * (no surprise changes in default fan speed).
345 		 */
346 		err = gpiod_direction_output(gpios[i],
347 					gpiod_get_value_cansleep(gpios[i]));
348 		if (err)
349 			return err;
350 	}
351 
352 	fan_data->pwm_enable = true; /* Enable manual fan speed control. */
353 	fan_data->speed_index = get_fan_speed_index(fan_data);
354 	if (fan_data->speed_index < 0)
355 		return fan_data->speed_index;
356 
357 	return 0;
358 }
359 
360 static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
361 				  unsigned long *state)
362 {
363 	struct gpio_fan_data *fan_data = cdev->devdata;
364 
365 	if (!fan_data)
366 		return -EINVAL;
367 
368 	*state = fan_data->num_speed - 1;
369 	return 0;
370 }
371 
372 static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
373 				  unsigned long *state)
374 {
375 	struct gpio_fan_data *fan_data = cdev->devdata;
376 
377 	if (!fan_data)
378 		return -EINVAL;
379 
380 	*state = fan_data->speed_index;
381 	return 0;
382 }
383 
384 static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
385 				  unsigned long state)
386 {
387 	struct gpio_fan_data *fan_data = cdev->devdata;
388 
389 	if (!fan_data)
390 		return -EINVAL;
391 
392 	if (state >= fan_data->num_speed)
393 		return -EINVAL;
394 
395 	set_fan_speed(fan_data, state);
396 	return 0;
397 }
398 
399 static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
400 	.get_max_state = gpio_fan_get_max_state,
401 	.get_cur_state = gpio_fan_get_cur_state,
402 	.set_cur_state = gpio_fan_set_cur_state,
403 };
404 
405 /*
406  * Translate OpenFirmware node properties into platform_data
407  */
408 static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
409 {
410 	struct gpio_fan_speed *speed;
411 	struct device *dev = fan_data->dev;
412 	struct device_node *np = dev->of_node;
413 	struct gpio_desc **gpios;
414 	unsigned i;
415 	u32 u;
416 	struct property *prop;
417 	const __be32 *p;
418 
419 	/* Alarm GPIO if one exists */
420 	fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN);
421 	if (IS_ERR(fan_data->alarm_gpio))
422 		return PTR_ERR(fan_data->alarm_gpio);
423 
424 	/* Fill GPIO pin array */
425 	fan_data->num_gpios = gpiod_count(dev, NULL);
426 	if (fan_data->num_gpios <= 0) {
427 		if (fan_data->alarm_gpio)
428 			return 0;
429 		dev_err(dev, "DT properties empty / missing");
430 		return -ENODEV;
431 	}
432 	gpios = devm_kcalloc(dev,
433 			     fan_data->num_gpios, sizeof(struct gpio_desc *),
434 			     GFP_KERNEL);
435 	if (!gpios)
436 		return -ENOMEM;
437 	for (i = 0; i < fan_data->num_gpios; i++) {
438 		gpios[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
439 		if (IS_ERR(gpios[i]))
440 			return PTR_ERR(gpios[i]);
441 	}
442 	fan_data->gpios = gpios;
443 
444 	/* Get number of RPM/ctrl_val pairs in speed map */
445 	prop = of_find_property(np, "gpio-fan,speed-map", &i);
446 	if (!prop) {
447 		dev_err(dev, "gpio-fan,speed-map DT property missing");
448 		return -ENODEV;
449 	}
450 	i = i / sizeof(u32);
451 	if (i == 0 || i & 1) {
452 		dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
453 		return -ENODEV;
454 	}
455 	fan_data->num_speed = i / 2;
456 
457 	/*
458 	 * Populate speed map
459 	 * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
460 	 * this needs splitting into pairs to create gpio_fan_speed structs
461 	 */
462 	speed = devm_kcalloc(dev,
463 			fan_data->num_speed, sizeof(struct gpio_fan_speed),
464 			GFP_KERNEL);
465 	if (!speed)
466 		return -ENOMEM;
467 	p = NULL;
468 	for (i = 0; i < fan_data->num_speed; i++) {
469 		p = of_prop_next_u32(prop, p, &u);
470 		if (!p)
471 			return -ENODEV;
472 		speed[i].rpm = u;
473 		p = of_prop_next_u32(prop, p, &u);
474 		if (!p)
475 			return -ENODEV;
476 		speed[i].ctrl_val = u;
477 	}
478 	fan_data->speed = speed;
479 
480 	return 0;
481 }
482 
483 static const struct of_device_id of_gpio_fan_match[] = {
484 	{ .compatible = "gpio-fan", },
485 	{},
486 };
487 MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
488 
489 static void gpio_fan_stop(void *data)
490 {
491 	set_fan_speed(data, 0);
492 }
493 
494 static int gpio_fan_probe(struct platform_device *pdev)
495 {
496 	int err;
497 	struct gpio_fan_data *fan_data;
498 	struct device *dev = &pdev->dev;
499 	struct device_node *np = dev->of_node;
500 
501 	fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
502 				GFP_KERNEL);
503 	if (!fan_data)
504 		return -ENOMEM;
505 
506 	fan_data->dev = dev;
507 	err = gpio_fan_get_of_data(fan_data);
508 	if (err)
509 		return err;
510 
511 	platform_set_drvdata(pdev, fan_data);
512 	mutex_init(&fan_data->lock);
513 
514 	/* Configure control GPIOs if available. */
515 	if (fan_data->gpios && fan_data->num_gpios > 0) {
516 		if (!fan_data->speed || fan_data->num_speed <= 1)
517 			return -EINVAL;
518 		err = fan_ctrl_init(fan_data);
519 		if (err)
520 			return err;
521 		err = devm_add_action_or_reset(dev, gpio_fan_stop, fan_data);
522 		if (err)
523 			return err;
524 	}
525 
526 	/* Make this driver part of hwmon class. */
527 	fan_data->hwmon_dev =
528 		devm_hwmon_device_register_with_groups(dev,
529 						       "gpio_fan", fan_data,
530 						       gpio_fan_groups);
531 	if (IS_ERR(fan_data->hwmon_dev))
532 		return PTR_ERR(fan_data->hwmon_dev);
533 
534 	/* Configure alarm GPIO if available. */
535 	if (fan_data->alarm_gpio) {
536 		err = fan_alarm_init(fan_data);
537 		if (err)
538 			return err;
539 	}
540 
541 	/* Optional cooling device register for Device tree platforms */
542 	fan_data->cdev = devm_thermal_of_cooling_device_register(dev, np,
543 				"gpio-fan", fan_data, &gpio_fan_cool_ops);
544 
545 	dev_info(dev, "GPIO fan initialized\n");
546 
547 	return 0;
548 }
549 
550 static void gpio_fan_shutdown(struct platform_device *pdev)
551 {
552 	struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
553 
554 	if (fan_data->gpios)
555 		set_fan_speed(fan_data, 0);
556 }
557 
558 static int gpio_fan_suspend(struct device *dev)
559 {
560 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
561 
562 	if (fan_data->gpios) {
563 		fan_data->resume_speed = fan_data->speed_index;
564 		set_fan_speed(fan_data, 0);
565 	}
566 
567 	return 0;
568 }
569 
570 static int gpio_fan_resume(struct device *dev)
571 {
572 	struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
573 
574 	if (fan_data->gpios)
575 		set_fan_speed(fan_data, fan_data->resume_speed);
576 
577 	return 0;
578 }
579 
580 static DEFINE_SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
581 
582 static struct platform_driver gpio_fan_driver = {
583 	.probe		= gpio_fan_probe,
584 	.shutdown	= gpio_fan_shutdown,
585 	.driver	= {
586 		.name	= "gpio-fan",
587 		.pm	= pm_sleep_ptr(&gpio_fan_pm),
588 		.of_match_table = of_match_ptr(of_gpio_fan_match),
589 	},
590 };
591 
592 module_platform_driver(gpio_fan_driver);
593 
594 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
595 MODULE_DESCRIPTION("GPIO FAN driver");
596 MODULE_LICENSE("GPL");
597 MODULE_ALIAS("platform:gpio-fan");
598