Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * pwm-fan.c - Hwmon driver for fans connected to PWM lines. |
| 3 | * |
| 4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
| 5 | * |
| 6 | * Author: Kamil Debski <k.debski@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/hwmon.h> |
| 20 | #include <linux/hwmon-sysfs.h> |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pwm.h> |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 28 | #include <linux/sysfs.h> |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 29 | #include <linux/thermal.h> |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 30 | #include <linux/timer.h> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 31 | |
| 32 | #define MAX_PWM 255 |
| 33 | |
| 34 | struct pwm_fan_ctx { |
| 35 | struct mutex lock; |
| 36 | struct pwm_device *pwm; |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 37 | struct regulator *reg_en; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 38 | |
| 39 | int irq; |
| 40 | atomic_t pulses; |
| 41 | unsigned int rpm; |
| 42 | u8 pulses_per_revolution; |
| 43 | ktime_t sample_start; |
| 44 | struct timer_list rpm_timer; |
| 45 | |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 46 | unsigned int pwm_value; |
| 47 | unsigned int pwm_fan_state; |
| 48 | unsigned int pwm_fan_max_state; |
| 49 | unsigned int *pwm_fan_cooling_levels; |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 50 | struct thermal_cooling_device *cdev; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 53 | /* This handler assumes self resetting edge triggered interrupt. */ |
| 54 | static irqreturn_t pulse_handler(int irq, void *dev_id) |
| 55 | { |
| 56 | struct pwm_fan_ctx *ctx = dev_id; |
| 57 | |
| 58 | atomic_inc(&ctx->pulses); |
| 59 | |
| 60 | return IRQ_HANDLED; |
| 61 | } |
| 62 | |
| 63 | static void sample_timer(struct timer_list *t) |
| 64 | { |
| 65 | struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); |
| 66 | int pulses; |
| 67 | u64 tmp; |
| 68 | |
| 69 | pulses = atomic_read(&ctx->pulses); |
| 70 | atomic_sub(pulses, &ctx->pulses); |
| 71 | tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60; |
| 72 | do_div(tmp, ctx->pulses_per_revolution * 1000); |
| 73 | ctx->rpm = tmp; |
| 74 | |
| 75 | ctx->sample_start = ktime_get(); |
| 76 | mod_timer(&ctx->rpm_timer, jiffies + HZ); |
| 77 | } |
| 78 | |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 79 | static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 80 | { |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 81 | unsigned long period; |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 82 | int ret = 0; |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 83 | struct pwm_state state = { }; |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 84 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 85 | mutex_lock(&ctx->lock); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 86 | if (ctx->pwm_value == pwm) |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 87 | goto exit_set_pwm_err; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 88 | |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 89 | pwm_init_state(ctx->pwm, &state); |
| 90 | period = ctx->pwm->args.period; |
| 91 | state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); |
| 92 | state.enabled = pwm ? true : false; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 93 | |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 94 | ret = pwm_apply_state(ctx->pwm, &state); |
| 95 | if (!ret) |
| 96 | ctx->pwm_value = pwm; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 97 | exit_set_pwm_err: |
| 98 | mutex_unlock(&ctx->lock); |
| 99 | return ret; |
| 100 | } |
| 101 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 102 | static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) |
| 103 | { |
| 104 | int i; |
| 105 | |
| 106 | for (i = 0; i < ctx->pwm_fan_max_state; ++i) |
| 107 | if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) |
| 108 | break; |
| 109 | |
| 110 | ctx->pwm_fan_state = i; |
| 111 | } |
| 112 | |
Guenter Roeck | cb1d853 | 2018-12-06 10:48:56 -0800 | [diff] [blame] | 113 | static ssize_t pwm_store(struct device *dev, struct device_attribute *attr, |
| 114 | const char *buf, size_t count) |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 115 | { |
| 116 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
| 117 | unsigned long pwm; |
| 118 | int ret; |
| 119 | |
| 120 | if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | ret = __set_pwm(ctx, pwm); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 127 | pwm_fan_update_state(ctx, pwm); |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 128 | return count; |
| 129 | } |
| 130 | |
Guenter Roeck | cb1d853 | 2018-12-06 10:48:56 -0800 | [diff] [blame] | 131 | static ssize_t pwm_show(struct device *dev, struct device_attribute *attr, |
| 132 | char *buf) |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 133 | { |
| 134 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
| 135 | |
| 136 | return sprintf(buf, "%u\n", ctx->pwm_value); |
| 137 | } |
| 138 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 139 | static ssize_t rpm_show(struct device *dev, |
| 140 | struct device_attribute *attr, char *buf) |
| 141 | { |
| 142 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
| 143 | |
| 144 | return sprintf(buf, "%u\n", ctx->rpm); |
| 145 | } |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 146 | |
Guenter Roeck | cb1d853 | 2018-12-06 10:48:56 -0800 | [diff] [blame] | 147 | static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 148 | static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 149 | |
| 150 | static struct attribute *pwm_fan_attrs[] = { |
| 151 | &sensor_dev_attr_pwm1.dev_attr.attr, |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 152 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 153 | NULL, |
| 154 | }; |
| 155 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 156 | static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a, |
| 157 | int n) |
| 158 | { |
| 159 | struct device *dev = container_of(kobj, struct device, kobj); |
| 160 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
| 161 | |
| 162 | /* Hide fan_input in case no interrupt is available */ |
| 163 | if (n == 1 && ctx->irq <= 0) |
| 164 | return 0; |
| 165 | |
| 166 | return a->mode; |
| 167 | } |
| 168 | |
| 169 | static const struct attribute_group pwm_fan_group = { |
| 170 | .attrs = pwm_fan_attrs, |
| 171 | .is_visible = pwm_fan_attrs_visible, |
| 172 | }; |
| 173 | |
| 174 | static const struct attribute_group *pwm_fan_groups[] = { |
| 175 | &pwm_fan_group, |
| 176 | NULL, |
| 177 | }; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 178 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 179 | /* thermal cooling device callbacks */ |
| 180 | static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev, |
| 181 | unsigned long *state) |
| 182 | { |
| 183 | struct pwm_fan_ctx *ctx = cdev->devdata; |
| 184 | |
| 185 | if (!ctx) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | *state = ctx->pwm_fan_max_state; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev, |
| 194 | unsigned long *state) |
| 195 | { |
| 196 | struct pwm_fan_ctx *ctx = cdev->devdata; |
| 197 | |
| 198 | if (!ctx) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | *state = ctx->pwm_fan_state; |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int |
| 207 | pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) |
| 208 | { |
| 209 | struct pwm_fan_ctx *ctx = cdev->devdata; |
| 210 | int ret; |
| 211 | |
| 212 | if (!ctx || (state > ctx->pwm_fan_max_state)) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | if (state == ctx->pwm_fan_state) |
| 216 | return 0; |
| 217 | |
| 218 | ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); |
| 219 | if (ret) { |
| 220 | dev_err(&cdev->device, "Cannot set pwm!\n"); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | ctx->pwm_fan_state = state; |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = { |
| 230 | .get_max_state = pwm_fan_get_max_state, |
| 231 | .get_cur_state = pwm_fan_get_cur_state, |
| 232 | .set_cur_state = pwm_fan_set_cur_state, |
| 233 | }; |
| 234 | |
Guenter Roeck | de52b04 | 2015-03-04 09:51:05 -0800 | [diff] [blame] | 235 | static int pwm_fan_of_get_cooling_data(struct device *dev, |
| 236 | struct pwm_fan_ctx *ctx) |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 237 | { |
| 238 | struct device_node *np = dev->of_node; |
| 239 | int num, i, ret; |
| 240 | |
| 241 | if (!of_find_property(np, "cooling-levels", NULL)) |
| 242 | return 0; |
| 243 | |
| 244 | ret = of_property_count_u32_elems(np, "cooling-levels"); |
| 245 | if (ret <= 0) { |
| 246 | dev_err(dev, "Wrong data!\n"); |
| 247 | return ret ? : -EINVAL; |
| 248 | } |
| 249 | |
| 250 | num = ret; |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 251 | ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32), |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 252 | GFP_KERNEL); |
| 253 | if (!ctx->pwm_fan_cooling_levels) |
| 254 | return -ENOMEM; |
| 255 | |
| 256 | ret = of_property_read_u32_array(np, "cooling-levels", |
| 257 | ctx->pwm_fan_cooling_levels, num); |
| 258 | if (ret) { |
| 259 | dev_err(dev, "Property 'cooling-levels' cannot be read!\n"); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | for (i = 0; i < num; i++) { |
| 264 | if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { |
| 265 | dev_err(dev, "PWM fan state[%d]:%d > %d\n", i, |
| 266 | ctx->pwm_fan_cooling_levels[i], MAX_PWM); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | ctx->pwm_fan_max_state = num - 1; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 276 | static void pwm_fan_regulator_disable(void *data) |
| 277 | { |
| 278 | regulator_disable(data); |
| 279 | } |
| 280 | |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 281 | static void pwm_fan_pwm_disable(void *__ctx) |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 282 | { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 283 | struct pwm_fan_ctx *ctx = __ctx; |
| 284 | pwm_disable(ctx->pwm); |
| 285 | del_timer_sync(&ctx->rpm_timer); |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 288 | static int pwm_fan_probe(struct platform_device *pdev) |
| 289 | { |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 290 | struct thermal_cooling_device *cdev; |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 291 | struct device *dev = &pdev->dev; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 292 | struct pwm_fan_ctx *ctx; |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 293 | struct device *hwmon; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 294 | int ret; |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 295 | struct pwm_state state = { }; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 296 | u32 ppr = 2; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 297 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 298 | ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 299 | if (!ctx) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | mutex_init(&ctx->lock); |
| 303 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 304 | ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 305 | if (IS_ERR(ctx->pwm)) { |
Thierry Reding | 9f67f75 | 2018-09-21 12:10:47 +0200 | [diff] [blame] | 306 | ret = PTR_ERR(ctx->pwm); |
| 307 | |
| 308 | if (ret != -EPROBE_DEFER) |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 309 | dev_err(dev, "Could not get PWM: %d\n", ret); |
Thierry Reding | 9f67f75 | 2018-09-21 12:10:47 +0200 | [diff] [blame] | 310 | |
| 311 | return ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 314 | platform_set_drvdata(pdev, ctx); |
| 315 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 316 | ctx->irq = platform_get_irq(pdev, 0); |
| 317 | if (ctx->irq == -EPROBE_DEFER) |
| 318 | return ctx->irq; |
| 319 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 320 | ctx->reg_en = devm_regulator_get_optional(dev, "fan"); |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 321 | if (IS_ERR(ctx->reg_en)) { |
| 322 | if (PTR_ERR(ctx->reg_en) != -ENODEV) |
| 323 | return PTR_ERR(ctx->reg_en); |
| 324 | |
| 325 | ctx->reg_en = NULL; |
| 326 | } else { |
| 327 | ret = regulator_enable(ctx->reg_en); |
| 328 | if (ret) { |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 329 | dev_err(dev, "Failed to enable fan supply: %d\n", ret); |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 330 | return ret; |
| 331 | } |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 332 | devm_add_action_or_reset(dev, pwm_fan_regulator_disable, |
| 333 | ctx->reg_en); |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 336 | ctx->pwm_value = MAX_PWM; |
| 337 | |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 338 | /* Set duty cycle to maximum allowed and enable PWM output */ |
| 339 | pwm_init_state(ctx->pwm, &state); |
| 340 | state.duty_cycle = ctx->pwm->args.period - 1; |
| 341 | state.enabled = true; |
| 342 | |
| 343 | ret = pwm_apply_state(ctx->pwm, &state); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 344 | if (ret) { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 345 | dev_err(dev, "Failed to configure PWM: %d\n", ret); |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 346 | return ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 347 | } |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 348 | timer_setup(&ctx->rpm_timer, sample_timer, 0); |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 349 | devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx); |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 350 | |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 351 | of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr); |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 352 | ctx->pulses_per_revolution = ppr; |
| 353 | if (!ctx->pulses_per_revolution) { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 354 | dev_err(dev, "pulses-per-revolution can't be zero.\n"); |
| 355 | return -EINVAL; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | if (ctx->irq > 0) { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 359 | ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0, |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 360 | pdev->name, ctx); |
| 361 | if (ret) { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 362 | dev_err(dev, "Failed to request interrupt: %d\n", ret); |
| 363 | return ret; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 364 | } |
| 365 | ctx->sample_start = ktime_get(); |
| 366 | mod_timer(&ctx->rpm_timer, jiffies + HZ); |
| 367 | } |
| 368 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 369 | hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan", |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 370 | ctx, pwm_fan_groups); |
| 371 | if (IS_ERR(hwmon)) { |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 372 | dev_err(dev, "Failed to register hwmon device\n"); |
| 373 | return PTR_ERR(hwmon); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 374 | } |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 375 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 376 | ret = pwm_fan_of_get_cooling_data(dev, ctx); |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 377 | if (ret) |
| 378 | return ret; |
| 379 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 380 | ctx->pwm_fan_state = ctx->pwm_fan_max_state; |
| 381 | if (IS_ENABLED(CONFIG_THERMAL)) { |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 382 | cdev = devm_thermal_of_cooling_device_register(dev, |
| 383 | dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 384 | if (IS_ERR(cdev)) { |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 385 | ret = PTR_ERR(cdev); |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 386 | dev_err(dev, |
Robin Murphy | 841cf67 | 2019-04-12 15:05:23 +0100 | [diff] [blame] | 387 | "Failed to register pwm-fan as cooling device: %d\n", |
| 388 | ret); |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 389 | return ret; |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 390 | } |
| 391 | ctx->cdev = cdev; |
| 392 | thermal_cdev_update(cdev); |
| 393 | } |
| 394 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 395 | return 0; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | #ifdef CONFIG_PM_SLEEP |
| 399 | static int pwm_fan_suspend(struct device *dev) |
| 400 | { |
| 401 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
Thierry Reding | 95dcd64 | 2018-09-21 12:10:48 +0200 | [diff] [blame] | 402 | struct pwm_args args; |
| 403 | int ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 404 | |
Thierry Reding | 95dcd64 | 2018-09-21 12:10:48 +0200 | [diff] [blame] | 405 | pwm_get_args(ctx->pwm, &args); |
| 406 | |
| 407 | if (ctx->pwm_value) { |
| 408 | ret = pwm_config(ctx->pwm, 0, args.period); |
| 409 | if (ret < 0) |
| 410 | return ret; |
| 411 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 412 | pwm_disable(ctx->pwm); |
Thierry Reding | 95dcd64 | 2018-09-21 12:10:48 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 415 | if (ctx->reg_en) { |
| 416 | ret = regulator_disable(ctx->reg_en); |
| 417 | if (ret) { |
| 418 | dev_err(dev, "Failed to disable fan supply: %d\n", ret); |
| 419 | return ret; |
| 420 | } |
| 421 | } |
| 422 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int pwm_fan_resume(struct device *dev) |
| 427 | { |
| 428 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 429 | struct pwm_args pargs; |
Kamil Debski | 48b9d5b | 2014-11-03 15:42:55 +0100 | [diff] [blame] | 430 | unsigned long duty; |
| 431 | int ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 432 | |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 433 | if (ctx->reg_en) { |
| 434 | ret = regulator_enable(ctx->reg_en); |
| 435 | if (ret) { |
| 436 | dev_err(dev, "Failed to enable fan supply: %d\n", ret); |
| 437 | return ret; |
| 438 | } |
| 439 | } |
| 440 | |
Kamil Debski | 48b9d5b | 2014-11-03 15:42:55 +0100 | [diff] [blame] | 441 | if (ctx->pwm_value == 0) |
| 442 | return 0; |
| 443 | |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 444 | pwm_get_args(ctx->pwm, &pargs); |
| 445 | duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM); |
| 446 | ret = pwm_config(ctx->pwm, duty, pargs.period); |
Kamil Debski | 48b9d5b | 2014-11-03 15:42:55 +0100 | [diff] [blame] | 447 | if (ret) |
| 448 | return ret; |
| 449 | return pwm_enable(ctx->pwm); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 450 | } |
| 451 | #endif |
| 452 | |
| 453 | static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume); |
| 454 | |
Fabian Frederick | d720aca | 2015-03-16 20:54:36 +0100 | [diff] [blame] | 455 | static const struct of_device_id of_pwm_fan_match[] = { |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 456 | { .compatible = "pwm-fan", }, |
| 457 | {}, |
| 458 | }; |
Luis de Bethencourt | f491e70 | 2015-09-17 18:09:55 +0200 | [diff] [blame] | 459 | MODULE_DEVICE_TABLE(of, of_pwm_fan_match); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 460 | |
| 461 | static struct platform_driver pwm_fan_driver = { |
| 462 | .probe = pwm_fan_probe, |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 463 | .driver = { |
| 464 | .name = "pwm-fan", |
| 465 | .pm = &pwm_fan_pm, |
| 466 | .of_match_table = of_pwm_fan_match, |
| 467 | }, |
| 468 | }; |
| 469 | |
| 470 | module_platform_driver(pwm_fan_driver); |
| 471 | |
| 472 | MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); |
| 473 | MODULE_ALIAS("platform:pwm-fan"); |
| 474 | MODULE_DESCRIPTION("PWM FAN driver"); |
| 475 | MODULE_LICENSE("GPL"); |