0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/slab.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/irq.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/err.h>
0017 #include <linux/mutex.h>
0018 #include <linux/hwmon.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/of.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/thermal.h>
0023
0024 struct gpio_fan_speed {
0025 int rpm;
0026 int ctrl_val;
0027 };
0028
0029 struct gpio_fan_data {
0030 struct device *dev;
0031 struct device *hwmon_dev;
0032
0033 struct thermal_cooling_device *cdev;
0034 struct mutex lock;
0035 int num_gpios;
0036 struct gpio_desc **gpios;
0037 int num_speed;
0038 struct gpio_fan_speed *speed;
0039 int speed_index;
0040 #ifdef CONFIG_PM_SLEEP
0041 int resume_speed;
0042 #endif
0043 bool pwm_enable;
0044 struct gpio_desc *alarm_gpio;
0045 struct work_struct alarm_work;
0046 };
0047
0048
0049
0050
0051
0052 static void fan_alarm_notify(struct work_struct *ws)
0053 {
0054 struct gpio_fan_data *fan_data =
0055 container_of(ws, struct gpio_fan_data, alarm_work);
0056
0057 sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm");
0058 kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE);
0059 }
0060
0061 static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
0062 {
0063 struct gpio_fan_data *fan_data = dev_id;
0064
0065 schedule_work(&fan_data->alarm_work);
0066
0067 return IRQ_NONE;
0068 }
0069
0070 static ssize_t fan1_alarm_show(struct device *dev,
0071 struct device_attribute *attr, char *buf)
0072 {
0073 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0074
0075 return sprintf(buf, "%d\n",
0076 gpiod_get_value_cansleep(fan_data->alarm_gpio));
0077 }
0078
0079 static DEVICE_ATTR_RO(fan1_alarm);
0080
0081 static int fan_alarm_init(struct gpio_fan_data *fan_data)
0082 {
0083 int alarm_irq;
0084 struct device *dev = fan_data->dev;
0085
0086
0087
0088
0089
0090 alarm_irq = gpiod_to_irq(fan_data->alarm_gpio);
0091 if (alarm_irq <= 0)
0092 return 0;
0093
0094 INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
0095 irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
0096 return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
0097 IRQF_SHARED, "GPIO fan alarm", fan_data);
0098 }
0099
0100
0101
0102
0103
0104
0105 static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
0106 {
0107 int i;
0108
0109 for (i = 0; i < fan_data->num_gpios; i++)
0110 gpiod_set_value_cansleep(fan_data->gpios[i],
0111 (ctrl_val >> i) & 1);
0112 }
0113
0114 static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
0115 {
0116 int i;
0117 int ctrl_val = 0;
0118
0119 for (i = 0; i < fan_data->num_gpios; i++) {
0120 int value;
0121
0122 value = gpiod_get_value_cansleep(fan_data->gpios[i]);
0123 ctrl_val |= (value << i);
0124 }
0125 return ctrl_val;
0126 }
0127
0128
0129 static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
0130 {
0131 if (fan_data->speed_index == speed_index)
0132 return;
0133
0134 __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
0135 fan_data->speed_index = speed_index;
0136 }
0137
0138 static int get_fan_speed_index(struct gpio_fan_data *fan_data)
0139 {
0140 int ctrl_val = __get_fan_ctrl(fan_data);
0141 int i;
0142
0143 for (i = 0; i < fan_data->num_speed; i++)
0144 if (fan_data->speed[i].ctrl_val == ctrl_val)
0145 return i;
0146
0147 dev_warn(fan_data->dev,
0148 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
0149
0150 return -ENODEV;
0151 }
0152
0153 static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
0154 {
0155 struct gpio_fan_speed *speed = fan_data->speed;
0156 int i;
0157
0158 for (i = 0; i < fan_data->num_speed; i++)
0159 if (speed[i].rpm >= rpm)
0160 return i;
0161
0162 return fan_data->num_speed - 1;
0163 }
0164
0165 static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
0166 char *buf)
0167 {
0168 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0169 u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
0170
0171 return sprintf(buf, "%d\n", pwm);
0172 }
0173
0174 static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
0175 const char *buf, size_t count)
0176 {
0177 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0178 unsigned long pwm;
0179 int speed_index;
0180 int ret = count;
0181
0182 if (kstrtoul(buf, 10, &pwm) || pwm > 255)
0183 return -EINVAL;
0184
0185 mutex_lock(&fan_data->lock);
0186
0187 if (!fan_data->pwm_enable) {
0188 ret = -EPERM;
0189 goto exit_unlock;
0190 }
0191
0192 speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
0193 set_fan_speed(fan_data, speed_index);
0194
0195 exit_unlock:
0196 mutex_unlock(&fan_data->lock);
0197
0198 return ret;
0199 }
0200
0201 static ssize_t pwm1_enable_show(struct device *dev,
0202 struct device_attribute *attr, char *buf)
0203 {
0204 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0205
0206 return sprintf(buf, "%d\n", fan_data->pwm_enable);
0207 }
0208
0209 static ssize_t pwm1_enable_store(struct device *dev,
0210 struct device_attribute *attr,
0211 const char *buf, size_t count)
0212 {
0213 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0214 unsigned long val;
0215
0216 if (kstrtoul(buf, 10, &val) || val > 1)
0217 return -EINVAL;
0218
0219 if (fan_data->pwm_enable == val)
0220 return count;
0221
0222 mutex_lock(&fan_data->lock);
0223
0224 fan_data->pwm_enable = val;
0225
0226
0227 if (val == 0)
0228 set_fan_speed(fan_data, fan_data->num_speed - 1);
0229
0230 mutex_unlock(&fan_data->lock);
0231
0232 return count;
0233 }
0234
0235 static ssize_t pwm1_mode_show(struct device *dev,
0236 struct device_attribute *attr, char *buf)
0237 {
0238 return sprintf(buf, "0\n");
0239 }
0240
0241 static ssize_t fan1_min_show(struct device *dev,
0242 struct device_attribute *attr, char *buf)
0243 {
0244 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0245
0246 return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
0247 }
0248
0249 static ssize_t fan1_max_show(struct device *dev,
0250 struct device_attribute *attr, char *buf)
0251 {
0252 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0253
0254 return sprintf(buf, "%d\n",
0255 fan_data->speed[fan_data->num_speed - 1].rpm);
0256 }
0257
0258 static ssize_t fan1_input_show(struct device *dev,
0259 struct device_attribute *attr, char *buf)
0260 {
0261 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0262
0263 return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
0264 }
0265
0266 static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
0267 const char *buf, size_t count)
0268 {
0269 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0270 unsigned long rpm;
0271 int ret = count;
0272
0273 if (kstrtoul(buf, 10, &rpm))
0274 return -EINVAL;
0275
0276 mutex_lock(&fan_data->lock);
0277
0278 if (!fan_data->pwm_enable) {
0279 ret = -EPERM;
0280 goto exit_unlock;
0281 }
0282
0283 set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
0284
0285 exit_unlock:
0286 mutex_unlock(&fan_data->lock);
0287
0288 return ret;
0289 }
0290
0291 static DEVICE_ATTR_RW(pwm1);
0292 static DEVICE_ATTR_RW(pwm1_enable);
0293 static DEVICE_ATTR_RO(pwm1_mode);
0294 static DEVICE_ATTR_RO(fan1_min);
0295 static DEVICE_ATTR_RO(fan1_max);
0296 static DEVICE_ATTR_RO(fan1_input);
0297 static DEVICE_ATTR(fan1_target, 0644, fan1_input_show, set_rpm);
0298
0299 static umode_t gpio_fan_is_visible(struct kobject *kobj,
0300 struct attribute *attr, int index)
0301 {
0302 struct device *dev = kobj_to_dev(kobj);
0303 struct gpio_fan_data *data = dev_get_drvdata(dev);
0304
0305 if (index == 0 && !data->alarm_gpio)
0306 return 0;
0307 if (index > 0 && !data->gpios)
0308 return 0;
0309
0310 return attr->mode;
0311 }
0312
0313 static struct attribute *gpio_fan_attributes[] = {
0314 &dev_attr_fan1_alarm.attr,
0315 &dev_attr_pwm1.attr,
0316 &dev_attr_pwm1_enable.attr,
0317 &dev_attr_pwm1_mode.attr,
0318 &dev_attr_fan1_input.attr,
0319 &dev_attr_fan1_target.attr,
0320 &dev_attr_fan1_min.attr,
0321 &dev_attr_fan1_max.attr,
0322 NULL
0323 };
0324
0325 static const struct attribute_group gpio_fan_group = {
0326 .attrs = gpio_fan_attributes,
0327 .is_visible = gpio_fan_is_visible,
0328 };
0329
0330 static const struct attribute_group *gpio_fan_groups[] = {
0331 &gpio_fan_group,
0332 NULL
0333 };
0334
0335 static int fan_ctrl_init(struct gpio_fan_data *fan_data)
0336 {
0337 int num_gpios = fan_data->num_gpios;
0338 struct gpio_desc **gpios = fan_data->gpios;
0339 int i, err;
0340
0341 for (i = 0; i < num_gpios; i++) {
0342
0343
0344
0345
0346
0347
0348 err = gpiod_direction_output(gpios[i],
0349 gpiod_get_value_cansleep(gpios[i]));
0350 if (err)
0351 return err;
0352 }
0353
0354 fan_data->pwm_enable = true;
0355 fan_data->speed_index = get_fan_speed_index(fan_data);
0356 if (fan_data->speed_index < 0)
0357 return fan_data->speed_index;
0358
0359 return 0;
0360 }
0361
0362 static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
0363 unsigned long *state)
0364 {
0365 struct gpio_fan_data *fan_data = cdev->devdata;
0366
0367 if (!fan_data)
0368 return -EINVAL;
0369
0370 *state = fan_data->num_speed - 1;
0371 return 0;
0372 }
0373
0374 static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
0375 unsigned long *state)
0376 {
0377 struct gpio_fan_data *fan_data = cdev->devdata;
0378
0379 if (!fan_data)
0380 return -EINVAL;
0381
0382 *state = fan_data->speed_index;
0383 return 0;
0384 }
0385
0386 static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
0387 unsigned long state)
0388 {
0389 struct gpio_fan_data *fan_data = cdev->devdata;
0390
0391 if (!fan_data)
0392 return -EINVAL;
0393
0394 if (state >= fan_data->num_speed)
0395 return -EINVAL;
0396
0397 set_fan_speed(fan_data, state);
0398 return 0;
0399 }
0400
0401 static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
0402 .get_max_state = gpio_fan_get_max_state,
0403 .get_cur_state = gpio_fan_get_cur_state,
0404 .set_cur_state = gpio_fan_set_cur_state,
0405 };
0406
0407
0408
0409
0410 static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
0411 {
0412 struct gpio_fan_speed *speed;
0413 struct device *dev = fan_data->dev;
0414 struct device_node *np = dev->of_node;
0415 struct gpio_desc **gpios;
0416 unsigned i;
0417 u32 u;
0418 struct property *prop;
0419 const __be32 *p;
0420
0421
0422 fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN);
0423 if (IS_ERR(fan_data->alarm_gpio))
0424 return PTR_ERR(fan_data->alarm_gpio);
0425
0426
0427 fan_data->num_gpios = gpiod_count(dev, NULL);
0428 if (fan_data->num_gpios <= 0) {
0429 if (fan_data->alarm_gpio)
0430 return 0;
0431 dev_err(dev, "DT properties empty / missing");
0432 return -ENODEV;
0433 }
0434 gpios = devm_kcalloc(dev,
0435 fan_data->num_gpios, sizeof(struct gpio_desc *),
0436 GFP_KERNEL);
0437 if (!gpios)
0438 return -ENOMEM;
0439 for (i = 0; i < fan_data->num_gpios; i++) {
0440 gpios[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
0441 if (IS_ERR(gpios[i]))
0442 return PTR_ERR(gpios[i]);
0443 }
0444 fan_data->gpios = gpios;
0445
0446
0447 prop = of_find_property(np, "gpio-fan,speed-map", &i);
0448 if (!prop) {
0449 dev_err(dev, "gpio-fan,speed-map DT property missing");
0450 return -ENODEV;
0451 }
0452 i = i / sizeof(u32);
0453 if (i == 0 || i & 1) {
0454 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
0455 return -ENODEV;
0456 }
0457 fan_data->num_speed = i / 2;
0458
0459
0460
0461
0462
0463
0464 speed = devm_kcalloc(dev,
0465 fan_data->num_speed, sizeof(struct gpio_fan_speed),
0466 GFP_KERNEL);
0467 if (!speed)
0468 return -ENOMEM;
0469 p = NULL;
0470 for (i = 0; i < fan_data->num_speed; i++) {
0471 p = of_prop_next_u32(prop, p, &u);
0472 if (!p)
0473 return -ENODEV;
0474 speed[i].rpm = u;
0475 p = of_prop_next_u32(prop, p, &u);
0476 if (!p)
0477 return -ENODEV;
0478 speed[i].ctrl_val = u;
0479 }
0480 fan_data->speed = speed;
0481
0482 return 0;
0483 }
0484
0485 static const struct of_device_id of_gpio_fan_match[] = {
0486 { .compatible = "gpio-fan", },
0487 {},
0488 };
0489 MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
0490
0491 static void gpio_fan_stop(void *data)
0492 {
0493 set_fan_speed(data, 0);
0494 }
0495
0496 static int gpio_fan_probe(struct platform_device *pdev)
0497 {
0498 int err;
0499 struct gpio_fan_data *fan_data;
0500 struct device *dev = &pdev->dev;
0501 struct device_node *np = dev->of_node;
0502
0503 fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
0504 GFP_KERNEL);
0505 if (!fan_data)
0506 return -ENOMEM;
0507
0508 fan_data->dev = dev;
0509 err = gpio_fan_get_of_data(fan_data);
0510 if (err)
0511 return err;
0512
0513 platform_set_drvdata(pdev, fan_data);
0514 mutex_init(&fan_data->lock);
0515
0516
0517 if (fan_data->gpios && fan_data->num_gpios > 0) {
0518 if (!fan_data->speed || fan_data->num_speed <= 1)
0519 return -EINVAL;
0520 err = fan_ctrl_init(fan_data);
0521 if (err)
0522 return err;
0523 err = devm_add_action_or_reset(dev, gpio_fan_stop, fan_data);
0524 if (err)
0525 return err;
0526 }
0527
0528
0529 fan_data->hwmon_dev =
0530 devm_hwmon_device_register_with_groups(dev,
0531 "gpio_fan", fan_data,
0532 gpio_fan_groups);
0533 if (IS_ERR(fan_data->hwmon_dev))
0534 return PTR_ERR(fan_data->hwmon_dev);
0535
0536
0537 if (fan_data->alarm_gpio) {
0538 err = fan_alarm_init(fan_data);
0539 if (err)
0540 return err;
0541 }
0542
0543
0544 fan_data->cdev = devm_thermal_of_cooling_device_register(dev, np,
0545 "gpio-fan", fan_data, &gpio_fan_cool_ops);
0546
0547 dev_info(dev, "GPIO fan initialized\n");
0548
0549 return 0;
0550 }
0551
0552 static void gpio_fan_shutdown(struct platform_device *pdev)
0553 {
0554 struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
0555
0556 if (fan_data->gpios)
0557 set_fan_speed(fan_data, 0);
0558 }
0559
0560 #ifdef CONFIG_PM_SLEEP
0561 static int gpio_fan_suspend(struct device *dev)
0562 {
0563 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0564
0565 if (fan_data->gpios) {
0566 fan_data->resume_speed = fan_data->speed_index;
0567 set_fan_speed(fan_data, 0);
0568 }
0569
0570 return 0;
0571 }
0572
0573 static int gpio_fan_resume(struct device *dev)
0574 {
0575 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
0576
0577 if (fan_data->gpios)
0578 set_fan_speed(fan_data, fan_data->resume_speed);
0579
0580 return 0;
0581 }
0582
0583 static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
0584 #define GPIO_FAN_PM (&gpio_fan_pm)
0585 #else
0586 #define GPIO_FAN_PM NULL
0587 #endif
0588
0589 static struct platform_driver gpio_fan_driver = {
0590 .probe = gpio_fan_probe,
0591 .shutdown = gpio_fan_shutdown,
0592 .driver = {
0593 .name = "gpio-fan",
0594 .pm = GPIO_FAN_PM,
0595 .of_match_table = of_match_ptr(of_gpio_fan_match),
0596 },
0597 };
0598
0599 module_platform_driver(gpio_fan_driver);
0600
0601 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
0602 MODULE_DESCRIPTION("GPIO FAN driver");
0603 MODULE_LICENSE("GPL");
0604 MODULE_ALIAS("platform:gpio-fan");