23 #include <linux/module.h>
25 #include <linux/slab.h>
46 #ifdef CONFIG_PM_SLEEP
86 return sprintf(buf,
"%d\n", value);
122 err = devm_request_irq(&pdev->
dev, alarm_irq, fan_alarm_irq_handler,
146 static void __set_fan_ctrl(
struct gpio_fan_data *fan_data,
int ctrl_val)
150 for (i = 0; i < fan_data->
num_ctrl; i++)
159 for (i = 0; i < fan_data->
num_ctrl; i++) {
163 ctrl_val |= (value <<
i);
169 static void set_fan_speed(
struct gpio_fan_data *fan_data,
int speed_index)
174 __set_fan_ctrl(fan_data, fan_data->
speed[speed_index].ctrl_val);
178 static int get_fan_speed_index(
struct gpio_fan_data *fan_data)
180 int ctrl_val = __get_fan_ctrl(fan_data);
183 for (i = 0; i < fan_data->
num_speed; i++)
184 if (fan_data->
speed[i].ctrl_val == ctrl_val)
188 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
193 static int rpm_to_speed_index(
struct gpio_fan_data *fan_data,
int rpm)
198 for (i = 0; i < fan_data->
num_speed; i++)
199 if (speed[i].rpm >= rpm)
211 return sprintf(buf,
"%d\n", pwm);
215 const char *buf,
size_t count)
222 if (kstrtoul(buf, 10, &pwm) || pwm > 255)
233 set_fan_speed(fan_data, speed_index);
250 const char *buf,
size_t count)
255 if (kstrtoul(buf, 10, &val) || val > 1)
267 set_fan_speed(fan_data, fan_data->
num_speed - 1);
306 const char *buf,
size_t count)
312 if (kstrtoul(buf, 10, &rpm))
322 set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
332 show_pwm_enable, set_pwm_enable);
339 static struct attribute *gpio_fan_ctrl_attributes[] = {
341 &dev_attr_pwm1_enable.attr,
342 &dev_attr_pwm1_mode.attr,
343 &dev_attr_fan1_input.attr,
344 &dev_attr_fan1_target.attr,
345 &dev_attr_fan1_min.attr,
346 &dev_attr_fan1_max.attr,
351 .attrs = gpio_fan_ctrl_attributes,
362 for (i = 0; i < num_ctrl; i++) {
378 fan_data->
speed_index = get_fan_speed_index(fan_data);
400 return sprintf(buf,
"gpio-fan\n");
406 #ifdef CONFIG_OF_GPIO
410 static int gpio_fan_get_of_pdata(
struct device *dev,
424 pdata->
num_ctrl = of_gpio_count(node);
426 dev_err(dev,
"gpios DT property empty / missing");
433 for (i = 0; i < pdata->
num_ctrl; i++) {
436 val = of_get_gpio(node, i);
446 dev_err(dev,
"gpio-fan,speed-map DT property missing");
450 if (i == 0 || i & 1) {
451 dev_err(dev,
"gpio-fan,speed-map contains zero/odd number of entries");
477 pdata->
speed = speed;
503 { .compatible =
"gpio-fan", },
514 #ifdef CONFIG_OF_GPIO
522 err = gpio_fan_get_of_pdata(&pdev->
dev, pdata);
536 fan_data->
pdev = pdev;
537 platform_set_drvdata(pdev, fan_data);
542 err = fan_alarm_init(fan_data, pdata->
alarm);
553 err = fan_ctrl_init(fan_data, pdata);
566 goto err_remove_name;
577 fan_ctrl_free(fan_data);
580 fan_alarm_free(fan_data);
591 fan_alarm_free(fan_data);
593 fan_ctrl_free(fan_data);
598 #ifdef CONFIG_PM_SLEEP
599 static int gpio_fan_suspend(
struct device *dev)
603 if (fan_data->
ctrl) {
605 set_fan_speed(fan_data, 0);
611 static int gpio_fan_resume(
struct device *dev)
616 set_fan_speed(fan_data, fan_data->resume_speed);
622 #define GPIO_FAN_PM &gpio_fan_pm
624 #define GPIO_FAN_PM NULL
628 .probe = gpio_fan_probe,
633 #ifdef CONFIG_OF_GPIO