Lines Matching +full:speed +full:- +full:map

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
39 struct gpio_fan_speed *speed; member
56 sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm"); in fan_alarm_notify()
57 kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE); in fan_alarm_notify()
64 schedule_work(&fan_data->alarm_work); in fan_alarm_irq_handler()
75 gpiod_get_value_cansleep(fan_data->alarm_gpio)); in fan1_alarm_show()
83 struct device *dev = fan_data->dev; in fan_alarm_init()
89 alarm_irq = gpiod_to_irq(fan_data->alarm_gpio); in fan_alarm_init()
93 INIT_WORK(&fan_data->alarm_work, fan_alarm_notify); in fan_alarm_init()
103 /* Must be called with fan_data->lock held, except during initialization. */
108 for (i = 0; i < fan_data->num_gpios; i++) in __set_fan_ctrl()
109 gpiod_set_value_cansleep(fan_data->gpios[i], in __set_fan_ctrl()
118 for (i = 0; i < fan_data->num_gpios; i++) { in __get_fan_ctrl()
121 value = gpiod_get_value_cansleep(fan_data->gpios[i]); in __get_fan_ctrl()
127 /* Must be called with fan_data->lock held, except during initialization. */
130 if (fan_data->speed_index == speed_index) in set_fan_speed()
133 __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val); in set_fan_speed()
134 fan_data->speed_index = speed_index; in set_fan_speed()
142 for (i = 0; i < fan_data->num_speed; i++) in get_fan_speed_index()
143 if (fan_data->speed[i].ctrl_val == ctrl_val) in get_fan_speed_index()
146 dev_warn(fan_data->dev, in get_fan_speed_index()
147 "missing speed array entry for GPIO value 0x%x\n", ctrl_val); in get_fan_speed_index()
149 return -ENODEV; in get_fan_speed_index()
154 struct gpio_fan_speed *speed = fan_data->speed; in rpm_to_speed_index() local
157 for (i = 0; i < fan_data->num_speed; i++) in rpm_to_speed_index()
158 if (speed[i].rpm >= rpm) in rpm_to_speed_index()
161 return fan_data->num_speed - 1; in rpm_to_speed_index()
168 u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1); in pwm1_show()
182 return -EINVAL; in pwm1_store()
184 mutex_lock(&fan_data->lock); in pwm1_store()
186 if (!fan_data->pwm_enable) { in pwm1_store()
187 ret = -EPERM; in pwm1_store()
191 speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255); in pwm1_store()
195 mutex_unlock(&fan_data->lock); in pwm1_store()
205 return sprintf(buf, "%d\n", fan_data->pwm_enable); in pwm1_enable_show()
216 return -EINVAL; in pwm1_enable_store()
218 if (fan_data->pwm_enable == val) in pwm1_enable_store()
221 mutex_lock(&fan_data->lock); in pwm1_enable_store()
223 fan_data->pwm_enable = val; in pwm1_enable_store()
225 /* Disable manual control mode: set fan at full speed. */ in pwm1_enable_store()
227 set_fan_speed(fan_data, fan_data->num_speed - 1); in pwm1_enable_store()
229 mutex_unlock(&fan_data->lock); in pwm1_enable_store()
245 return sprintf(buf, "%d\n", fan_data->speed[0].rpm); in fan1_min_show()
254 fan_data->speed[fan_data->num_speed - 1].rpm); in fan1_max_show()
262 return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm); in fan1_input_show()
273 return -EINVAL; in set_rpm()
275 mutex_lock(&fan_data->lock); in set_rpm()
277 if (!fan_data->pwm_enable) { in set_rpm()
278 ret = -EPERM; in set_rpm()
285 mutex_unlock(&fan_data->lock); in set_rpm()
304 if (index == 0 && !data->alarm_gpio) in gpio_fan_is_visible()
306 if (index > 0 && !data->gpios) in gpio_fan_is_visible()
309 return attr->mode; in gpio_fan_is_visible()
336 int num_gpios = fan_data->num_gpios; in fan_ctrl_init()
337 struct gpio_desc **gpios = fan_data->gpios; in fan_ctrl_init()
345 * (no surprise changes in default fan speed). in fan_ctrl_init()
353 fan_data->pwm_enable = true; /* Enable manual fan speed control. */ in fan_ctrl_init()
354 fan_data->speed_index = get_fan_speed_index(fan_data); in fan_ctrl_init()
355 if (fan_data->speed_index < 0) in fan_ctrl_init()
356 return fan_data->speed_index; in fan_ctrl_init()
364 struct gpio_fan_data *fan_data = cdev->devdata; in gpio_fan_get_max_state()
367 return -EINVAL; in gpio_fan_get_max_state()
369 *state = fan_data->num_speed - 1; in gpio_fan_get_max_state()
376 struct gpio_fan_data *fan_data = cdev->devdata; in gpio_fan_get_cur_state()
379 return -EINVAL; in gpio_fan_get_cur_state()
381 *state = fan_data->speed_index; in gpio_fan_get_cur_state()
388 struct gpio_fan_data *fan_data = cdev->devdata; in gpio_fan_set_cur_state()
391 return -EINVAL; in gpio_fan_set_cur_state()
393 if (state >= fan_data->num_speed) in gpio_fan_set_cur_state()
394 return -EINVAL; in gpio_fan_set_cur_state()
411 struct gpio_fan_speed *speed; in gpio_fan_get_of_data() local
412 struct device *dev = fan_data->dev; in gpio_fan_get_of_data()
413 struct device_node *np = dev->of_node; in gpio_fan_get_of_data()
421 fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN); in gpio_fan_get_of_data()
422 if (IS_ERR(fan_data->alarm_gpio)) in gpio_fan_get_of_data()
423 return PTR_ERR(fan_data->alarm_gpio); in gpio_fan_get_of_data()
426 fan_data->num_gpios = gpiod_count(dev, NULL); in gpio_fan_get_of_data()
427 if (fan_data->num_gpios <= 0) { in gpio_fan_get_of_data()
428 if (fan_data->alarm_gpio) in gpio_fan_get_of_data()
431 return -ENODEV; in gpio_fan_get_of_data()
434 fan_data->num_gpios, sizeof(struct gpio_desc *), in gpio_fan_get_of_data()
437 return -ENOMEM; in gpio_fan_get_of_data()
438 for (i = 0; i < fan_data->num_gpios; i++) { in gpio_fan_get_of_data()
443 fan_data->gpios = gpios; in gpio_fan_get_of_data()
445 /* Get number of RPM/ctrl_val pairs in speed map */ in gpio_fan_get_of_data()
446 prop = of_find_property(np, "gpio-fan,speed-map", &i); in gpio_fan_get_of_data()
448 dev_err(dev, "gpio-fan,speed-map DT property missing"); in gpio_fan_get_of_data()
449 return -ENODEV; in gpio_fan_get_of_data()
453 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries"); in gpio_fan_get_of_data()
454 return -ENODEV; in gpio_fan_get_of_data()
456 fan_data->num_speed = i / 2; in gpio_fan_get_of_data()
459 * Populate speed map in gpio_fan_get_of_data()
460 * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...> in gpio_fan_get_of_data()
463 speed = devm_kcalloc(dev, in gpio_fan_get_of_data()
464 fan_data->num_speed, sizeof(struct gpio_fan_speed), in gpio_fan_get_of_data()
466 if (!speed) in gpio_fan_get_of_data()
467 return -ENOMEM; in gpio_fan_get_of_data()
469 for (i = 0; i < fan_data->num_speed; i++) { in gpio_fan_get_of_data()
472 return -ENODEV; in gpio_fan_get_of_data()
473 speed[i].rpm = u; in gpio_fan_get_of_data()
476 return -ENODEV; in gpio_fan_get_of_data()
477 speed[i].ctrl_val = u; in gpio_fan_get_of_data()
479 fan_data->speed = speed; in gpio_fan_get_of_data()
485 { .compatible = "gpio-fan", },
499 struct device *dev = &pdev->dev; in gpio_fan_probe()
500 struct device_node *np = dev->of_node; in gpio_fan_probe()
505 return -ENOMEM; in gpio_fan_probe()
507 fan_data->dev = dev; in gpio_fan_probe()
513 mutex_init(&fan_data->lock); in gpio_fan_probe()
516 if (fan_data->gpios && fan_data->num_gpios > 0) { in gpio_fan_probe()
517 if (!fan_data->speed || fan_data->num_speed <= 1) in gpio_fan_probe()
518 return -EINVAL; in gpio_fan_probe()
528 fan_data->hwmon_dev = in gpio_fan_probe()
532 if (IS_ERR(fan_data->hwmon_dev)) in gpio_fan_probe()
533 return PTR_ERR(fan_data->hwmon_dev); in gpio_fan_probe()
536 if (fan_data->alarm_gpio) { in gpio_fan_probe()
543 fan_data->cdev = devm_thermal_of_cooling_device_register(dev, np, in gpio_fan_probe()
544 "gpio-fan", fan_data, &gpio_fan_cool_ops); in gpio_fan_probe()
555 if (fan_data->gpios) in gpio_fan_shutdown()
563 if (fan_data->gpios) { in gpio_fan_suspend()
564 fan_data->resume_speed = fan_data->speed_index; in gpio_fan_suspend()
575 if (fan_data->gpios) in gpio_fan_resume()
576 set_fan_speed(fan_data, fan_data->resume_speed); in gpio_fan_resume()
587 .name = "gpio-fan",
598 MODULE_ALIAS("platform:gpio-fan");