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