Lines Matching +full:data +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
34 struct mutex lock; /* lock GPIOs operations. */
36 struct gpio_desc **gpios; member
55 sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm"); in fan_alarm_notify()
56 kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE); in fan_alarm_notify()
63 schedule_work(&fan_data->alarm_work); in fan_alarm_irq_handler()
74 gpiod_get_value_cansleep(fan_data->alarm_gpio)); in fan1_alarm_show()
82 struct device *dev = fan_data->dev; in fan_alarm_init()
88 alarm_irq = gpiod_to_irq(fan_data->alarm_gpio); in fan_alarm_init()
92 INIT_WORK(&fan_data->alarm_work, fan_alarm_notify); in fan_alarm_init()
99 * Control GPIOs.
102 /* Must be called with fan_data->lock held, except during initialization. */
107 for (i = 0; i < fan_data->num_gpios; i++) in __set_fan_ctrl()
108 gpiod_set_value_cansleep(fan_data->gpios[i], in __set_fan_ctrl()
117 for (i = 0; i < fan_data->num_gpios; i++) { in __get_fan_ctrl()
120 value = gpiod_get_value_cansleep(fan_data->gpios[i]); in __get_fan_ctrl()
126 /* Must be called with fan_data->lock held, except during initialization. */
129 if (fan_data->speed_index == speed_index) in set_fan_speed()
132 __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val); in set_fan_speed()
133 fan_data->speed_index = speed_index; in set_fan_speed()
141 for (i = 0; i < fan_data->num_speed; i++) in get_fan_speed_index()
142 if (fan_data->speed[i].ctrl_val == ctrl_val) in get_fan_speed_index()
145 dev_warn(fan_data->dev, in get_fan_speed_index()
148 return -ENODEV; in get_fan_speed_index()
153 struct gpio_fan_speed *speed = fan_data->speed; in rpm_to_speed_index()
156 for (i = 0; i < fan_data->num_speed; i++) in rpm_to_speed_index()
160 return fan_data->num_speed - 1; in rpm_to_speed_index()
167 u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1); in pwm1_show()
181 return -EINVAL; in pwm1_store()
183 mutex_lock(&fan_data->lock); in pwm1_store()
185 if (!fan_data->pwm_enable) { in pwm1_store()
186 ret = -EPERM; in pwm1_store()
190 speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255); in pwm1_store()
194 mutex_unlock(&fan_data->lock); in pwm1_store()
204 return sprintf(buf, "%d\n", fan_data->pwm_enable); in pwm1_enable_show()
215 return -EINVAL; in pwm1_enable_store()
217 if (fan_data->pwm_enable == val) in pwm1_enable_store()
220 mutex_lock(&fan_data->lock); in pwm1_enable_store()
222 fan_data->pwm_enable = val; in pwm1_enable_store()
226 set_fan_speed(fan_data, fan_data->num_speed - 1); in pwm1_enable_store()
228 mutex_unlock(&fan_data->lock); in pwm1_enable_store()
244 return sprintf(buf, "%d\n", fan_data->speed[0].rpm); in fan1_min_show()
253 fan_data->speed[fan_data->num_speed - 1].rpm); in fan1_max_show()
261 return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm); in fan1_input_show()
272 return -EINVAL; in set_rpm()
274 mutex_lock(&fan_data->lock); in set_rpm()
276 if (!fan_data->pwm_enable) { in set_rpm()
277 ret = -EPERM; in set_rpm()
284 mutex_unlock(&fan_data->lock); in set_rpm()
301 struct gpio_fan_data *data = dev_get_drvdata(dev); in gpio_fan_is_visible() local
303 if (index == 0 && !data->alarm_gpio) in gpio_fan_is_visible()
305 if (index > 0 && !data->gpios) in gpio_fan_is_visible()
308 return attr->mode; in gpio_fan_is_visible()
335 int num_gpios = fan_data->num_gpios; in fan_ctrl_init()
336 struct gpio_desc **gpios = fan_data->gpios; in fan_ctrl_init() local
346 err = gpiod_direction_output(gpios[i], in fan_ctrl_init()
347 gpiod_get_value_cansleep(gpios[i])); in fan_ctrl_init()
352 fan_data->pwm_enable = true; /* Enable manual fan speed control. */ in fan_ctrl_init()
353 fan_data->speed_index = get_fan_speed_index(fan_data); in fan_ctrl_init()
354 if (fan_data->speed_index < 0) in fan_ctrl_init()
355 return fan_data->speed_index; in fan_ctrl_init()
363 struct gpio_fan_data *fan_data = cdev->devdata; in gpio_fan_get_max_state()
366 return -EINVAL; in gpio_fan_get_max_state()
368 *state = fan_data->num_speed - 1; in gpio_fan_get_max_state()
375 struct gpio_fan_data *fan_data = cdev->devdata; in gpio_fan_get_cur_state()
378 return -EINVAL; in gpio_fan_get_cur_state()
380 *state = fan_data->speed_index; in gpio_fan_get_cur_state()
387 struct gpio_fan_data *fan_data = cdev->devdata; in gpio_fan_set_cur_state()
390 return -EINVAL; in gpio_fan_set_cur_state()
392 if (state >= fan_data->num_speed) in gpio_fan_set_cur_state()
393 return -EINVAL; in gpio_fan_set_cur_state()
411 struct device *dev = fan_data->dev; in gpio_fan_get_of_data()
412 struct device_node *np = dev->of_node; in gpio_fan_get_of_data()
413 struct gpio_desc **gpios; in gpio_fan_get_of_data() local
420 fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN); in gpio_fan_get_of_data()
421 if (IS_ERR(fan_data->alarm_gpio)) in gpio_fan_get_of_data()
422 return PTR_ERR(fan_data->alarm_gpio); in gpio_fan_get_of_data()
425 fan_data->num_gpios = gpiod_count(dev, NULL); in gpio_fan_get_of_data()
426 if (fan_data->num_gpios <= 0) { in gpio_fan_get_of_data()
427 if (fan_data->alarm_gpio) in gpio_fan_get_of_data()
430 return -ENODEV; in gpio_fan_get_of_data()
432 gpios = devm_kcalloc(dev, in gpio_fan_get_of_data()
433 fan_data->num_gpios, sizeof(struct gpio_desc *), in gpio_fan_get_of_data()
435 if (!gpios) in gpio_fan_get_of_data()
436 return -ENOMEM; in gpio_fan_get_of_data()
437 for (i = 0; i < fan_data->num_gpios; i++) { in gpio_fan_get_of_data()
438 gpios[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS); in gpio_fan_get_of_data()
439 if (IS_ERR(gpios[i])) in gpio_fan_get_of_data()
440 return PTR_ERR(gpios[i]); in gpio_fan_get_of_data()
442 fan_data->gpios = gpios; in gpio_fan_get_of_data()
445 prop = of_find_property(np, "gpio-fan,speed-map", &i); in gpio_fan_get_of_data()
447 dev_err(dev, "gpio-fan,speed-map DT property missing"); in gpio_fan_get_of_data()
448 return -ENODEV; in gpio_fan_get_of_data()
452 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries"); in gpio_fan_get_of_data()
453 return -ENODEV; in gpio_fan_get_of_data()
455 fan_data->num_speed = i / 2; in gpio_fan_get_of_data()
463 fan_data->num_speed, sizeof(struct gpio_fan_speed), in gpio_fan_get_of_data()
466 return -ENOMEM; in gpio_fan_get_of_data()
468 for (i = 0; i < fan_data->num_speed; i++) { in gpio_fan_get_of_data()
471 return -ENODEV; in gpio_fan_get_of_data()
475 return -ENODEV; in gpio_fan_get_of_data()
478 fan_data->speed = speed; in gpio_fan_get_of_data()
484 { .compatible = "gpio-fan", },
489 static void gpio_fan_stop(void *data) in gpio_fan_stop() argument
491 set_fan_speed(data, 0); in gpio_fan_stop()
498 struct device *dev = &pdev->dev; in gpio_fan_probe()
499 struct device_node *np = dev->of_node; in gpio_fan_probe()
504 return -ENOMEM; in gpio_fan_probe()
506 fan_data->dev = dev; in gpio_fan_probe()
512 mutex_init(&fan_data->lock); in gpio_fan_probe()
514 /* Configure control GPIOs if available. */ in gpio_fan_probe()
515 if (fan_data->gpios && fan_data->num_gpios > 0) { in gpio_fan_probe()
516 if (!fan_data->speed || fan_data->num_speed <= 1) in gpio_fan_probe()
517 return -EINVAL; in gpio_fan_probe()
527 fan_data->hwmon_dev = in gpio_fan_probe()
531 if (IS_ERR(fan_data->hwmon_dev)) in gpio_fan_probe()
532 return PTR_ERR(fan_data->hwmon_dev); in gpio_fan_probe()
535 if (fan_data->alarm_gpio) { in gpio_fan_probe()
542 fan_data->cdev = devm_thermal_of_cooling_device_register(dev, np, in gpio_fan_probe()
543 "gpio-fan", fan_data, &gpio_fan_cool_ops); in gpio_fan_probe()
554 if (fan_data->gpios) in gpio_fan_shutdown()
562 if (fan_data->gpios) { in gpio_fan_suspend()
563 fan_data->resume_speed = fan_data->speed_index; in gpio_fan_suspend()
574 if (fan_data->gpios) in gpio_fan_resume()
575 set_fan_speed(fan_data, fan_data->resume_speed); in gpio_fan_resume()
586 .name = "gpio-fan",
597 MODULE_ALIAS("platform:gpio-fan");