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