Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * pwm-fan.c - Hwmon driver for fans connected to PWM lines. |
| 4 | * |
| 5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
| 6 | * |
| 7 | * Author: Kamil Debski <k.debski@samsung.com> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/hwmon.h> |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/pwm.h> |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 17 | #include <linux/regulator/consumer.h> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 18 | #include <linux/sysfs.h> |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 19 | #include <linux/thermal.h> |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 20 | #include <linux/timer.h> |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 21 | |
| 22 | #define MAX_PWM 255 |
| 23 | |
| 24 | struct pwm_fan_ctx { |
| 25 | struct mutex lock; |
| 26 | struct pwm_device *pwm; |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 27 | struct regulator *reg_en; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 28 | |
| 29 | int irq; |
| 30 | atomic_t pulses; |
| 31 | unsigned int rpm; |
| 32 | u8 pulses_per_revolution; |
| 33 | ktime_t sample_start; |
| 34 | struct timer_list rpm_timer; |
| 35 | |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 36 | unsigned int pwm_value; |
| 37 | unsigned int pwm_fan_state; |
| 38 | unsigned int pwm_fan_max_state; |
| 39 | unsigned int *pwm_fan_cooling_levels; |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 40 | struct thermal_cooling_device *cdev; |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 41 | |
| 42 | struct hwmon_chip_info info; |
| 43 | }; |
| 44 | |
| 45 | static const u32 pwm_fan_channel_config_pwm[] = { |
| 46 | HWMON_PWM_INPUT, |
| 47 | 0 |
| 48 | }; |
| 49 | |
| 50 | static const struct hwmon_channel_info pwm_fan_channel_pwm = { |
| 51 | .type = hwmon_pwm, |
| 52 | .config = pwm_fan_channel_config_pwm, |
| 53 | }; |
| 54 | |
| 55 | static const u32 pwm_fan_channel_config_fan[] = { |
| 56 | HWMON_F_INPUT, |
| 57 | 0 |
| 58 | }; |
| 59 | |
| 60 | static const struct hwmon_channel_info pwm_fan_channel_fan = { |
| 61 | .type = hwmon_fan, |
| 62 | .config = pwm_fan_channel_config_fan, |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 65 | /* This handler assumes self resetting edge triggered interrupt. */ |
| 66 | static irqreturn_t pulse_handler(int irq, void *dev_id) |
| 67 | { |
| 68 | struct pwm_fan_ctx *ctx = dev_id; |
| 69 | |
| 70 | atomic_inc(&ctx->pulses); |
| 71 | |
| 72 | return IRQ_HANDLED; |
| 73 | } |
| 74 | |
| 75 | static void sample_timer(struct timer_list *t) |
| 76 | { |
| 77 | struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); |
Paul Barker | fd8feec | 2020-11-11 16:46:43 +0000 | [diff] [blame] | 78 | unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 79 | int pulses; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 80 | |
Paul Barker | fd8feec | 2020-11-11 16:46:43 +0000 | [diff] [blame] | 81 | if (delta) { |
| 82 | pulses = atomic_read(&ctx->pulses); |
| 83 | atomic_sub(pulses, &ctx->pulses); |
| 84 | ctx->rpm = (unsigned int)(pulses * 1000 * 60) / |
| 85 | (ctx->pulses_per_revolution * delta); |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 86 | |
Paul Barker | fd8feec | 2020-11-11 16:46:43 +0000 | [diff] [blame] | 87 | ctx->sample_start = ktime_get(); |
| 88 | } |
| 89 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 90 | mod_timer(&ctx->rpm_timer, jiffies + HZ); |
| 91 | } |
| 92 | |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 93 | static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 94 | { |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 95 | unsigned long period; |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 96 | int ret = 0; |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 97 | struct pwm_state state = { }; |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 98 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 99 | mutex_lock(&ctx->lock); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 100 | if (ctx->pwm_value == pwm) |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 101 | goto exit_set_pwm_err; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 102 | |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 103 | pwm_init_state(ctx->pwm, &state); |
| 104 | period = ctx->pwm->args.period; |
| 105 | state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); |
| 106 | state.enabled = pwm ? true : false; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 107 | |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 108 | ret = pwm_apply_state(ctx->pwm, &state); |
| 109 | if (!ret) |
| 110 | ctx->pwm_value = pwm; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 111 | exit_set_pwm_err: |
| 112 | mutex_unlock(&ctx->lock); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 116 | static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) |
| 117 | { |
| 118 | int i; |
| 119 | |
| 120 | for (i = 0; i < ctx->pwm_fan_max_state; ++i) |
| 121 | if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) |
| 122 | break; |
| 123 | |
| 124 | ctx->pwm_fan_state = i; |
| 125 | } |
| 126 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 127 | static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type, |
| 128 | u32 attr, int channel, long val) |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 129 | { |
| 130 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 131 | int ret; |
| 132 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 133 | if (val < 0 || val > MAX_PWM) |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 136 | ret = __set_pwm(ctx, val); |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 137 | if (ret) |
| 138 | return ret; |
| 139 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 140 | pwm_fan_update_state(ctx, val); |
| 141 | return 0; |
Lukasz Majewski | cb85ca3 | 2015-02-26 14:59:35 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 144 | static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type, |
| 145 | u32 attr, int channel, long *val) |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 146 | { |
| 147 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
| 148 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 149 | switch (type) { |
| 150 | case hwmon_pwm: |
| 151 | *val = ctx->pwm_value; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 152 | return 0; |
| 153 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 154 | case hwmon_fan: |
| 155 | *val = ctx->rpm; |
| 156 | return 0; |
| 157 | |
| 158 | default: |
| 159 | return -ENOTSUPP; |
| 160 | } |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 163 | static umode_t pwm_fan_is_visible(const void *data, |
| 164 | enum hwmon_sensor_types type, |
| 165 | u32 attr, int channel) |
| 166 | { |
| 167 | switch (type) { |
| 168 | case hwmon_pwm: |
| 169 | return 0644; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 170 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 171 | case hwmon_fan: |
| 172 | return 0444; |
| 173 | |
| 174 | default: |
| 175 | return 0; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static const struct hwmon_ops pwm_fan_hwmon_ops = { |
| 180 | .is_visible = pwm_fan_is_visible, |
| 181 | .read = pwm_fan_read, |
| 182 | .write = pwm_fan_write, |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 183 | }; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 184 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 185 | /* thermal cooling device callbacks */ |
| 186 | static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev, |
| 187 | unsigned long *state) |
| 188 | { |
| 189 | struct pwm_fan_ctx *ctx = cdev->devdata; |
| 190 | |
| 191 | if (!ctx) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | *state = ctx->pwm_fan_max_state; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev, |
| 200 | unsigned long *state) |
| 201 | { |
| 202 | struct pwm_fan_ctx *ctx = cdev->devdata; |
| 203 | |
| 204 | if (!ctx) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | *state = ctx->pwm_fan_state; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int |
| 213 | pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) |
| 214 | { |
| 215 | struct pwm_fan_ctx *ctx = cdev->devdata; |
| 216 | int ret; |
| 217 | |
| 218 | if (!ctx || (state > ctx->pwm_fan_max_state)) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | if (state == ctx->pwm_fan_state) |
| 222 | return 0; |
| 223 | |
| 224 | ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); |
| 225 | if (ret) { |
| 226 | dev_err(&cdev->device, "Cannot set pwm!\n"); |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | ctx->pwm_fan_state = state; |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = { |
| 236 | .get_max_state = pwm_fan_get_max_state, |
| 237 | .get_cur_state = pwm_fan_get_cur_state, |
| 238 | .set_cur_state = pwm_fan_set_cur_state, |
| 239 | }; |
| 240 | |
Guenter Roeck | de52b04 | 2015-03-04 09:51:05 -0800 | [diff] [blame] | 241 | static int pwm_fan_of_get_cooling_data(struct device *dev, |
| 242 | struct pwm_fan_ctx *ctx) |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 243 | { |
| 244 | struct device_node *np = dev->of_node; |
| 245 | int num, i, ret; |
| 246 | |
| 247 | if (!of_find_property(np, "cooling-levels", NULL)) |
| 248 | return 0; |
| 249 | |
| 250 | ret = of_property_count_u32_elems(np, "cooling-levels"); |
| 251 | if (ret <= 0) { |
| 252 | dev_err(dev, "Wrong data!\n"); |
| 253 | return ret ? : -EINVAL; |
| 254 | } |
| 255 | |
| 256 | num = ret; |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 257 | ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32), |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 258 | GFP_KERNEL); |
| 259 | if (!ctx->pwm_fan_cooling_levels) |
| 260 | return -ENOMEM; |
| 261 | |
| 262 | ret = of_property_read_u32_array(np, "cooling-levels", |
| 263 | ctx->pwm_fan_cooling_levels, num); |
| 264 | if (ret) { |
| 265 | dev_err(dev, "Property 'cooling-levels' cannot be read!\n"); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | for (i = 0; i < num; i++) { |
| 270 | if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { |
| 271 | dev_err(dev, "PWM fan state[%d]:%d > %d\n", i, |
| 272 | ctx->pwm_fan_cooling_levels[i], MAX_PWM); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | ctx->pwm_fan_max_state = num - 1; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 282 | static void pwm_fan_regulator_disable(void *data) |
| 283 | { |
| 284 | regulator_disable(data); |
| 285 | } |
| 286 | |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 287 | static void pwm_fan_pwm_disable(void *__ctx) |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 288 | { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 289 | struct pwm_fan_ctx *ctx = __ctx; |
| 290 | pwm_disable(ctx->pwm); |
| 291 | del_timer_sync(&ctx->rpm_timer); |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 294 | static int pwm_fan_probe(struct platform_device *pdev) |
| 295 | { |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 296 | struct thermal_cooling_device *cdev; |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 297 | struct device *dev = &pdev->dev; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 298 | struct pwm_fan_ctx *ctx; |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 299 | struct device *hwmon; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 300 | int ret; |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 301 | struct pwm_state state = { }; |
Paul Barker | b5fcb8a | 2020-11-26 17:44:07 +0000 | [diff] [blame] | 302 | int tach_count; |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 303 | const struct hwmon_channel_info **channels; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 304 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 305 | ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 306 | if (!ctx) |
| 307 | return -ENOMEM; |
| 308 | |
| 309 | mutex_init(&ctx->lock); |
| 310 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 311 | ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); |
Anson Huang | 65b2aad | 2020-08-17 15:34:33 +0800 | [diff] [blame] | 312 | if (IS_ERR(ctx->pwm)) |
| 313 | return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n"); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 314 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 315 | platform_set_drvdata(pdev, ctx); |
| 316 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 317 | ctx->reg_en = devm_regulator_get_optional(dev, "fan"); |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 318 | if (IS_ERR(ctx->reg_en)) { |
| 319 | if (PTR_ERR(ctx->reg_en) != -ENODEV) |
| 320 | return PTR_ERR(ctx->reg_en); |
| 321 | |
| 322 | ctx->reg_en = NULL; |
| 323 | } else { |
| 324 | ret = regulator_enable(ctx->reg_en); |
| 325 | if (ret) { |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 326 | dev_err(dev, "Failed to enable fan supply: %d\n", ret); |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 327 | return ret; |
| 328 | } |
Guenter Roeck | 5696e4a | 2019-06-07 13:27:06 -0700 | [diff] [blame] | 329 | ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable, |
| 330 | ctx->reg_en); |
| 331 | if (ret) |
| 332 | return ret; |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 335 | ctx->pwm_value = MAX_PWM; |
| 336 | |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 337 | pwm_init_state(ctx->pwm, &state); |
Uwe Kleine-König | 1eda523 | 2020-12-15 10:20:30 +0100 | [diff] [blame] | 338 | /* |
| 339 | * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned |
| 340 | * long. Check this here to prevent the fan running at a too low |
| 341 | * frequency. |
| 342 | */ |
| 343 | if (state.period > ULONG_MAX / MAX_PWM + 1) { |
| 344 | dev_err(dev, "Configured period too big\n"); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | /* Set duty cycle to maximum allowed and enable PWM output */ |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 349 | state.duty_cycle = ctx->pwm->args.period - 1; |
| 350 | state.enabled = true; |
| 351 | |
| 352 | ret = pwm_apply_state(ctx->pwm, &state); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 353 | if (ret) { |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 354 | dev_err(dev, "Failed to configure PWM: %d\n", ret); |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 355 | return ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 356 | } |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 357 | timer_setup(&ctx->rpm_timer, sample_timer, 0); |
Guenter Roeck | 5696e4a | 2019-06-07 13:27:06 -0700 | [diff] [blame] | 358 | ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx); |
| 359 | if (ret) |
| 360 | return ret; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 361 | |
Paul Barker | b5fcb8a | 2020-11-26 17:44:07 +0000 | [diff] [blame] | 362 | tach_count = platform_irq_count(pdev); |
| 363 | if (tach_count < 0) |
| 364 | return dev_err_probe(dev, tach_count, |
| 365 | "Could not get number of fan tachometer inputs\n"); |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 366 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 367 | channels = devm_kcalloc(dev, tach_count + 2, |
| 368 | sizeof(struct hwmon_channel_info *), GFP_KERNEL); |
| 369 | if (!channels) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | channels[0] = &pwm_fan_channel_pwm; |
| 373 | |
Paul Barker | b5fcb8a | 2020-11-26 17:44:07 +0000 | [diff] [blame] | 374 | if (tach_count > 0) { |
| 375 | u32 ppr = 2; |
| 376 | |
| 377 | ctx->irq = platform_get_irq(pdev, 0); |
| 378 | if (ctx->irq == -EPROBE_DEFER) |
| 379 | return ctx->irq; |
| 380 | if (ctx->irq > 0) { |
| 381 | ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0, |
| 382 | pdev->name, ctx); |
| 383 | if (ret) { |
| 384 | dev_err(dev, |
| 385 | "Failed to request interrupt: %d\n", |
| 386 | ret); |
| 387 | return ret; |
| 388 | } |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 389 | } |
Paul Barker | b5fcb8a | 2020-11-26 17:44:07 +0000 | [diff] [blame] | 390 | |
| 391 | of_property_read_u32(dev->of_node, |
| 392 | "pulses-per-revolution", |
| 393 | &ppr); |
| 394 | ctx->pulses_per_revolution = ppr; |
| 395 | if (!ctx->pulses_per_revolution) { |
| 396 | dev_err(dev, "pulses-per-revolution can't be zero.\n"); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n", |
| 401 | ctx->irq, ctx->pulses_per_revolution); |
| 402 | |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 403 | ctx->sample_start = ktime_get(); |
| 404 | mod_timer(&ctx->rpm_timer, jiffies + HZ); |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 405 | |
| 406 | channels[1] = &pwm_fan_channel_fan; |
Stefan Wahren | 6b1ec47 | 2019-04-11 15:30:11 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Paul Barker | 1aa03655 | 2020-11-28 17:49:09 +0000 | [diff] [blame] | 409 | ctx->info.ops = &pwm_fan_hwmon_ops; |
| 410 | ctx->info.info = channels; |
| 411 | |
| 412 | hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan", |
| 413 | ctx, &ctx->info, NULL); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 414 | if (IS_ERR(hwmon)) { |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 415 | dev_err(dev, "Failed to register hwmon device\n"); |
| 416 | return PTR_ERR(hwmon); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 417 | } |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 418 | |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 419 | ret = pwm_fan_of_get_cooling_data(dev, ctx); |
Lukasz Majewski | 2e5219c | 2015-02-26 14:59:36 +0100 | [diff] [blame] | 420 | if (ret) |
| 421 | return ret; |
| 422 | |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 423 | ctx->pwm_fan_state = ctx->pwm_fan_max_state; |
| 424 | if (IS_ENABLED(CONFIG_THERMAL)) { |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 425 | cdev = devm_thermal_of_cooling_device_register(dev, |
| 426 | dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 427 | if (IS_ERR(cdev)) { |
Bartlomiej Zolnierkiewicz | 677252a | 2017-04-24 15:13:17 +0200 | [diff] [blame] | 428 | ret = PTR_ERR(cdev); |
Guenter Roeck | 37bcec5 | 2019-04-18 12:58:20 -0700 | [diff] [blame] | 429 | dev_err(dev, |
Robin Murphy | 841cf67 | 2019-04-12 15:05:23 +0100 | [diff] [blame] | 430 | "Failed to register pwm-fan as cooling device: %d\n", |
| 431 | ret); |
Linus Torvalds | a455eda | 2019-05-16 07:56:57 -0700 | [diff] [blame] | 432 | return ret; |
Lukasz Majewski | b6bddec0 | 2015-02-26 14:59:37 +0100 | [diff] [blame] | 433 | } |
| 434 | ctx->cdev = cdev; |
| 435 | thermal_cdev_update(cdev); |
| 436 | } |
| 437 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 438 | return 0; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Akinobu Mita | 7992db7 | 2020-01-21 00:32:24 +0900 | [diff] [blame] | 441 | static int pwm_fan_disable(struct device *dev) |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 442 | { |
| 443 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
Thierry Reding | 95dcd64 | 2018-09-21 12:10:48 +0200 | [diff] [blame] | 444 | struct pwm_args args; |
| 445 | int ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 446 | |
Thierry Reding | 95dcd64 | 2018-09-21 12:10:48 +0200 | [diff] [blame] | 447 | pwm_get_args(ctx->pwm, &args); |
| 448 | |
| 449 | if (ctx->pwm_value) { |
| 450 | ret = pwm_config(ctx->pwm, 0, args.period); |
| 451 | if (ret < 0) |
| 452 | return ret; |
| 453 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 454 | pwm_disable(ctx->pwm); |
Thierry Reding | 95dcd64 | 2018-09-21 12:10:48 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 457 | if (ctx->reg_en) { |
| 458 | ret = regulator_disable(ctx->reg_en); |
| 459 | if (ret) { |
| 460 | dev_err(dev, "Failed to disable fan supply: %d\n", ret); |
| 461 | return ret; |
| 462 | } |
| 463 | } |
| 464 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Akinobu Mita | 7992db7 | 2020-01-21 00:32:24 +0900 | [diff] [blame] | 468 | static void pwm_fan_shutdown(struct platform_device *pdev) |
| 469 | { |
| 470 | pwm_fan_disable(&pdev->dev); |
| 471 | } |
| 472 | |
| 473 | #ifdef CONFIG_PM_SLEEP |
| 474 | static int pwm_fan_suspend(struct device *dev) |
| 475 | { |
| 476 | return pwm_fan_disable(dev); |
| 477 | } |
| 478 | |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 479 | static int pwm_fan_resume(struct device *dev) |
| 480 | { |
| 481 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 482 | struct pwm_args pargs; |
Kamil Debski | 48b9d5b | 2014-11-03 15:42:55 +0100 | [diff] [blame] | 483 | unsigned long duty; |
| 484 | int ret; |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 485 | |
Stefan Wahren | b57e1d4 | 2019-02-22 14:45:24 +0100 | [diff] [blame] | 486 | if (ctx->reg_en) { |
| 487 | ret = regulator_enable(ctx->reg_en); |
| 488 | if (ret) { |
| 489 | dev_err(dev, "Failed to enable fan supply: %d\n", ret); |
| 490 | return ret; |
| 491 | } |
| 492 | } |
| 493 | |
Kamil Debski | 48b9d5b | 2014-11-03 15:42:55 +0100 | [diff] [blame] | 494 | if (ctx->pwm_value == 0) |
| 495 | return 0; |
| 496 | |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 497 | pwm_get_args(ctx->pwm, &pargs); |
Guru Das Srinagesh | f3e4b14 | 2020-06-02 15:31:06 -0700 | [diff] [blame] | 498 | duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM); |
Boris Brezillon | 2289711 | 2016-04-14 21:17:24 +0200 | [diff] [blame] | 499 | ret = pwm_config(ctx->pwm, duty, pargs.period); |
Kamil Debski | 48b9d5b | 2014-11-03 15:42:55 +0100 | [diff] [blame] | 500 | if (ret) |
| 501 | return ret; |
| 502 | return pwm_enable(ctx->pwm); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 503 | } |
| 504 | #endif |
| 505 | |
| 506 | static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume); |
| 507 | |
Fabian Frederick | d720aca | 2015-03-16 20:54:36 +0100 | [diff] [blame] | 508 | static const struct of_device_id of_pwm_fan_match[] = { |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 509 | { .compatible = "pwm-fan", }, |
| 510 | {}, |
| 511 | }; |
Luis de Bethencourt | f491e70 | 2015-09-17 18:09:55 +0200 | [diff] [blame] | 512 | MODULE_DEVICE_TABLE(of, of_pwm_fan_match); |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 513 | |
| 514 | static struct platform_driver pwm_fan_driver = { |
| 515 | .probe = pwm_fan_probe, |
Akinobu Mita | 7992db7 | 2020-01-21 00:32:24 +0900 | [diff] [blame] | 516 | .shutdown = pwm_fan_shutdown, |
Kamil Debski | d82d577 | 2014-07-16 17:46:42 +0200 | [diff] [blame] | 517 | .driver = { |
| 518 | .name = "pwm-fan", |
| 519 | .pm = &pwm_fan_pm, |
| 520 | .of_match_table = of_pwm_fan_match, |
| 521 | }, |
| 522 | }; |
| 523 | |
| 524 | module_platform_driver(pwm_fan_driver); |
| 525 | |
| 526 | MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); |
| 527 | MODULE_ALIAS("platform:pwm-fan"); |
| 528 | MODULE_DESCRIPTION("PWM FAN driver"); |
| 529 | MODULE_LICENSE("GPL"); |