pwm-fan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 <[email protected]>
  8. */
  9. #include <linux/hwmon.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pwm.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/thermal.h>
  19. #include <linux/timer.h>
  20. #define MAX_PWM 255
  21. struct pwm_fan_tach {
  22. int irq;
  23. atomic_t pulses;
  24. unsigned int rpm;
  25. u8 pulses_per_revolution;
  26. };
  27. enum pwm_fan_enable_mode {
  28. pwm_off_reg_off,
  29. pwm_disable_reg_enable,
  30. pwm_enable_reg_enable,
  31. pwm_disable_reg_disable,
  32. };
  33. struct pwm_fan_ctx {
  34. struct device *dev;
  35. struct mutex lock;
  36. struct pwm_device *pwm;
  37. struct pwm_state pwm_state;
  38. struct regulator *reg_en;
  39. enum pwm_fan_enable_mode enable_mode;
  40. bool regulator_enabled;
  41. bool enabled;
  42. int tach_count;
  43. struct pwm_fan_tach *tachs;
  44. ktime_t sample_start;
  45. struct timer_list rpm_timer;
  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;
  50. struct thermal_cooling_device *cdev;
  51. struct hwmon_chip_info info;
  52. struct hwmon_channel_info fan_channel;
  53. };
  54. /* This handler assumes self resetting edge triggered interrupt. */
  55. static irqreturn_t pulse_handler(int irq, void *dev_id)
  56. {
  57. struct pwm_fan_tach *tach = dev_id;
  58. atomic_inc(&tach->pulses);
  59. return IRQ_HANDLED;
  60. }
  61. static void sample_timer(struct timer_list *t)
  62. {
  63. struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
  64. unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
  65. int i;
  66. if (delta) {
  67. for (i = 0; i < ctx->tach_count; i++) {
  68. struct pwm_fan_tach *tach = &ctx->tachs[i];
  69. int pulses;
  70. pulses = atomic_read(&tach->pulses);
  71. atomic_sub(pulses, &tach->pulses);
  72. tach->rpm = (unsigned int)(pulses * 1000 * 60) /
  73. (tach->pulses_per_revolution * delta);
  74. }
  75. ctx->sample_start = ktime_get();
  76. }
  77. mod_timer(&ctx->rpm_timer, jiffies + HZ);
  78. }
  79. static void pwm_fan_enable_mode_2_state(int enable_mode,
  80. struct pwm_state *state,
  81. bool *enable_regulator)
  82. {
  83. switch (enable_mode) {
  84. case pwm_disable_reg_enable:
  85. /* disable pwm, keep regulator enabled */
  86. state->enabled = false;
  87. *enable_regulator = true;
  88. break;
  89. case pwm_enable_reg_enable:
  90. /* keep pwm and regulator enabled */
  91. state->enabled = true;
  92. *enable_regulator = true;
  93. break;
  94. case pwm_off_reg_off:
  95. case pwm_disable_reg_disable:
  96. /* disable pwm and regulator */
  97. state->enabled = false;
  98. *enable_regulator = false;
  99. }
  100. }
  101. static int pwm_fan_switch_power(struct pwm_fan_ctx *ctx, bool on)
  102. {
  103. int ret = 0;
  104. if (!ctx->reg_en)
  105. return ret;
  106. if (!ctx->regulator_enabled && on) {
  107. ret = regulator_enable(ctx->reg_en);
  108. if (ret == 0)
  109. ctx->regulator_enabled = true;
  110. } else if (ctx->regulator_enabled && !on) {
  111. ret = regulator_disable(ctx->reg_en);
  112. if (ret == 0)
  113. ctx->regulator_enabled = false;
  114. }
  115. return ret;
  116. }
  117. static int pwm_fan_power_on(struct pwm_fan_ctx *ctx)
  118. {
  119. struct pwm_state *state = &ctx->pwm_state;
  120. int ret;
  121. if (ctx->enabled)
  122. return 0;
  123. ret = pwm_fan_switch_power(ctx, true);
  124. if (ret < 0) {
  125. dev_err(ctx->dev, "failed to enable power supply\n");
  126. return ret;
  127. }
  128. state->enabled = true;
  129. ret = pwm_apply_state(ctx->pwm, state);
  130. if (ret) {
  131. dev_err(ctx->dev, "failed to enable PWM\n");
  132. goto disable_regulator;
  133. }
  134. ctx->enabled = true;
  135. return 0;
  136. disable_regulator:
  137. pwm_fan_switch_power(ctx, false);
  138. return ret;
  139. }
  140. static int pwm_fan_power_off(struct pwm_fan_ctx *ctx)
  141. {
  142. struct pwm_state *state = &ctx->pwm_state;
  143. bool enable_regulator = false;
  144. int ret;
  145. if (!ctx->enabled)
  146. return 0;
  147. pwm_fan_enable_mode_2_state(ctx->enable_mode,
  148. state,
  149. &enable_regulator);
  150. state->enabled = false;
  151. state->duty_cycle = 0;
  152. ret = pwm_apply_state(ctx->pwm, state);
  153. if (ret) {
  154. dev_err(ctx->dev, "failed to disable PWM\n");
  155. return ret;
  156. }
  157. pwm_fan_switch_power(ctx, enable_regulator);
  158. ctx->enabled = false;
  159. return 0;
  160. }
  161. static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
  162. {
  163. struct pwm_state *state = &ctx->pwm_state;
  164. unsigned long period;
  165. int ret = 0;
  166. if (pwm > 0) {
  167. if (ctx->enable_mode == pwm_off_reg_off)
  168. /* pwm-fan hard disabled */
  169. return 0;
  170. period = state->period;
  171. state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
  172. ret = pwm_apply_state(ctx->pwm, state);
  173. if (ret)
  174. return ret;
  175. ret = pwm_fan_power_on(ctx);
  176. } else {
  177. ret = pwm_fan_power_off(ctx);
  178. }
  179. if (!ret)
  180. ctx->pwm_value = pwm;
  181. return ret;
  182. }
  183. static int set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
  184. {
  185. int ret;
  186. mutex_lock(&ctx->lock);
  187. ret = __set_pwm(ctx, pwm);
  188. mutex_unlock(&ctx->lock);
  189. return ret;
  190. }
  191. static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
  192. {
  193. int i;
  194. for (i = 0; i < ctx->pwm_fan_max_state; ++i)
  195. if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
  196. break;
  197. ctx->pwm_fan_state = i;
  198. }
  199. static int pwm_fan_update_enable(struct pwm_fan_ctx *ctx, long val)
  200. {
  201. int ret = 0;
  202. int old_val;
  203. mutex_lock(&ctx->lock);
  204. if (ctx->enable_mode == val)
  205. goto out;
  206. old_val = ctx->enable_mode;
  207. ctx->enable_mode = val;
  208. if (val == 0) {
  209. /* Disable pwm-fan unconditionally */
  210. if (ctx->enabled)
  211. ret = __set_pwm(ctx, 0);
  212. else
  213. ret = pwm_fan_switch_power(ctx, false);
  214. if (ret)
  215. ctx->enable_mode = old_val;
  216. pwm_fan_update_state(ctx, 0);
  217. } else {
  218. /*
  219. * Change PWM and/or regulator state if currently disabled
  220. * Nothing to do if currently enabled
  221. */
  222. if (!ctx->enabled) {
  223. struct pwm_state *state = &ctx->pwm_state;
  224. bool enable_regulator = false;
  225. state->duty_cycle = 0;
  226. pwm_fan_enable_mode_2_state(val,
  227. state,
  228. &enable_regulator);
  229. pwm_apply_state(ctx->pwm, state);
  230. pwm_fan_switch_power(ctx, enable_regulator);
  231. pwm_fan_update_state(ctx, 0);
  232. }
  233. }
  234. out:
  235. mutex_unlock(&ctx->lock);
  236. return ret;
  237. }
  238. static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
  239. u32 attr, int channel, long val)
  240. {
  241. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  242. int ret;
  243. switch (attr) {
  244. case hwmon_pwm_input:
  245. if (val < 0 || val > MAX_PWM)
  246. return -EINVAL;
  247. ret = set_pwm(ctx, val);
  248. if (ret)
  249. return ret;
  250. pwm_fan_update_state(ctx, val);
  251. break;
  252. case hwmon_pwm_enable:
  253. if (val < 0 || val > 3)
  254. ret = -EINVAL;
  255. else
  256. ret = pwm_fan_update_enable(ctx, val);
  257. return ret;
  258. default:
  259. return -EOPNOTSUPP;
  260. }
  261. return 0;
  262. }
  263. static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
  264. u32 attr, int channel, long *val)
  265. {
  266. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  267. switch (type) {
  268. case hwmon_pwm:
  269. switch (attr) {
  270. case hwmon_pwm_input:
  271. *val = ctx->pwm_value;
  272. return 0;
  273. case hwmon_pwm_enable:
  274. *val = ctx->enable_mode;
  275. return 0;
  276. }
  277. return -EOPNOTSUPP;
  278. case hwmon_fan:
  279. *val = ctx->tachs[channel].rpm;
  280. return 0;
  281. default:
  282. return -ENOTSUPP;
  283. }
  284. }
  285. static umode_t pwm_fan_is_visible(const void *data,
  286. enum hwmon_sensor_types type,
  287. u32 attr, int channel)
  288. {
  289. switch (type) {
  290. case hwmon_pwm:
  291. return 0644;
  292. case hwmon_fan:
  293. return 0444;
  294. default:
  295. return 0;
  296. }
  297. }
  298. static const struct hwmon_ops pwm_fan_hwmon_ops = {
  299. .is_visible = pwm_fan_is_visible,
  300. .read = pwm_fan_read,
  301. .write = pwm_fan_write,
  302. };
  303. /* thermal cooling device callbacks */
  304. static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
  305. unsigned long *state)
  306. {
  307. struct pwm_fan_ctx *ctx = cdev->devdata;
  308. if (!ctx)
  309. return -EINVAL;
  310. *state = ctx->pwm_fan_max_state;
  311. return 0;
  312. }
  313. static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
  314. unsigned long *state)
  315. {
  316. struct pwm_fan_ctx *ctx = cdev->devdata;
  317. if (!ctx)
  318. return -EINVAL;
  319. *state = ctx->pwm_fan_state;
  320. return 0;
  321. }
  322. static int
  323. pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  324. {
  325. struct pwm_fan_ctx *ctx = cdev->devdata;
  326. int ret;
  327. if (!ctx || (state > ctx->pwm_fan_max_state))
  328. return -EINVAL;
  329. if (state == ctx->pwm_fan_state)
  330. return 0;
  331. ret = set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
  332. if (ret) {
  333. dev_err(&cdev->device, "Cannot set pwm!\n");
  334. return ret;
  335. }
  336. ctx->pwm_fan_state = state;
  337. return ret;
  338. }
  339. static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
  340. .get_max_state = pwm_fan_get_max_state,
  341. .get_cur_state = pwm_fan_get_cur_state,
  342. .set_cur_state = pwm_fan_set_cur_state,
  343. };
  344. static int pwm_fan_of_get_cooling_data(struct device *dev,
  345. struct pwm_fan_ctx *ctx)
  346. {
  347. struct device_node *np = dev->of_node;
  348. int num, i, ret;
  349. if (!of_find_property(np, "cooling-levels", NULL))
  350. return 0;
  351. ret = of_property_count_u32_elems(np, "cooling-levels");
  352. if (ret <= 0) {
  353. dev_err(dev, "Wrong data!\n");
  354. return ret ? : -EINVAL;
  355. }
  356. num = ret;
  357. ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
  358. GFP_KERNEL);
  359. if (!ctx->pwm_fan_cooling_levels)
  360. return -ENOMEM;
  361. ret = of_property_read_u32_array(np, "cooling-levels",
  362. ctx->pwm_fan_cooling_levels, num);
  363. if (ret) {
  364. dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
  365. return ret;
  366. }
  367. for (i = 0; i < num; i++) {
  368. if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
  369. dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
  370. ctx->pwm_fan_cooling_levels[i], MAX_PWM);
  371. return -EINVAL;
  372. }
  373. }
  374. ctx->pwm_fan_max_state = num - 1;
  375. return 0;
  376. }
  377. static void pwm_fan_cleanup(void *__ctx)
  378. {
  379. struct pwm_fan_ctx *ctx = __ctx;
  380. del_timer_sync(&ctx->rpm_timer);
  381. /* Switch off everything */
  382. ctx->enable_mode = pwm_disable_reg_disable;
  383. pwm_fan_power_off(ctx);
  384. }
  385. static int pwm_fan_probe(struct platform_device *pdev)
  386. {
  387. struct thermal_cooling_device *cdev;
  388. struct device *dev = &pdev->dev;
  389. struct pwm_fan_ctx *ctx;
  390. struct device *hwmon;
  391. int ret;
  392. const struct hwmon_channel_info **channels;
  393. u32 *fan_channel_config;
  394. int channel_count = 1; /* We always have a PWM channel. */
  395. int i;
  396. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  397. if (!ctx)
  398. return -ENOMEM;
  399. mutex_init(&ctx->lock);
  400. ctx->dev = &pdev->dev;
  401. ctx->pwm = devm_pwm_get(dev, NULL);
  402. if (IS_ERR(ctx->pwm))
  403. return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
  404. platform_set_drvdata(pdev, ctx);
  405. ctx->reg_en = devm_regulator_get_optional(dev, "fan");
  406. if (IS_ERR(ctx->reg_en)) {
  407. if (PTR_ERR(ctx->reg_en) != -ENODEV)
  408. return PTR_ERR(ctx->reg_en);
  409. ctx->reg_en = NULL;
  410. }
  411. pwm_init_state(ctx->pwm, &ctx->pwm_state);
  412. /*
  413. * set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
  414. * long. Check this here to prevent the fan running at a too low
  415. * frequency.
  416. */
  417. if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
  418. dev_err(dev, "Configured period too big\n");
  419. return -EINVAL;
  420. }
  421. ctx->enable_mode = pwm_disable_reg_enable;
  422. /*
  423. * Set duty cycle to maximum allowed and enable PWM output as well as
  424. * the regulator. In case of error nothing is changed
  425. */
  426. ret = set_pwm(ctx, MAX_PWM);
  427. if (ret) {
  428. dev_err(dev, "Failed to configure PWM: %d\n", ret);
  429. return ret;
  430. }
  431. timer_setup(&ctx->rpm_timer, sample_timer, 0);
  432. ret = devm_add_action_or_reset(dev, pwm_fan_cleanup, ctx);
  433. if (ret)
  434. return ret;
  435. ctx->tach_count = platform_irq_count(pdev);
  436. if (ctx->tach_count < 0)
  437. return dev_err_probe(dev, ctx->tach_count,
  438. "Could not get number of fan tachometer inputs\n");
  439. dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count);
  440. if (ctx->tach_count) {
  441. channel_count++; /* We also have a FAN channel. */
  442. ctx->tachs = devm_kcalloc(dev, ctx->tach_count,
  443. sizeof(struct pwm_fan_tach),
  444. GFP_KERNEL);
  445. if (!ctx->tachs)
  446. return -ENOMEM;
  447. ctx->fan_channel.type = hwmon_fan;
  448. fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1,
  449. sizeof(u32), GFP_KERNEL);
  450. if (!fan_channel_config)
  451. return -ENOMEM;
  452. ctx->fan_channel.config = fan_channel_config;
  453. }
  454. channels = devm_kcalloc(dev, channel_count + 1,
  455. sizeof(struct hwmon_channel_info *), GFP_KERNEL);
  456. if (!channels)
  457. return -ENOMEM;
  458. channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE);
  459. for (i = 0; i < ctx->tach_count; i++) {
  460. struct pwm_fan_tach *tach = &ctx->tachs[i];
  461. u32 ppr = 2;
  462. tach->irq = platform_get_irq(pdev, i);
  463. if (tach->irq == -EPROBE_DEFER)
  464. return tach->irq;
  465. if (tach->irq > 0) {
  466. ret = devm_request_irq(dev, tach->irq, pulse_handler, 0,
  467. pdev->name, tach);
  468. if (ret) {
  469. dev_err(dev,
  470. "Failed to request interrupt: %d\n",
  471. ret);
  472. return ret;
  473. }
  474. }
  475. of_property_read_u32_index(dev->of_node,
  476. "pulses-per-revolution",
  477. i,
  478. &ppr);
  479. tach->pulses_per_revolution = ppr;
  480. if (!tach->pulses_per_revolution) {
  481. dev_err(dev, "pulses-per-revolution can't be zero.\n");
  482. return -EINVAL;
  483. }
  484. fan_channel_config[i] = HWMON_F_INPUT;
  485. dev_dbg(dev, "tach%d: irq=%d, pulses_per_revolution=%d\n",
  486. i, tach->irq, tach->pulses_per_revolution);
  487. }
  488. if (ctx->tach_count > 0) {
  489. ctx->sample_start = ktime_get();
  490. mod_timer(&ctx->rpm_timer, jiffies + HZ);
  491. channels[1] = &ctx->fan_channel;
  492. }
  493. ctx->info.ops = &pwm_fan_hwmon_ops;
  494. ctx->info.info = channels;
  495. hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
  496. ctx, &ctx->info, NULL);
  497. if (IS_ERR(hwmon)) {
  498. dev_err(dev, "Failed to register hwmon device\n");
  499. return PTR_ERR(hwmon);
  500. }
  501. ret = pwm_fan_of_get_cooling_data(dev, ctx);
  502. if (ret)
  503. return ret;
  504. ctx->pwm_fan_state = ctx->pwm_fan_max_state;
  505. if (IS_ENABLED(CONFIG_THERMAL)) {
  506. cdev = devm_thermal_of_cooling_device_register(dev,
  507. dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
  508. if (IS_ERR(cdev)) {
  509. ret = PTR_ERR(cdev);
  510. dev_err(dev,
  511. "Failed to register pwm-fan as cooling device: %d\n",
  512. ret);
  513. return ret;
  514. }
  515. ctx->cdev = cdev;
  516. }
  517. return 0;
  518. }
  519. static void pwm_fan_shutdown(struct platform_device *pdev)
  520. {
  521. struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
  522. pwm_fan_cleanup(ctx);
  523. }
  524. static int pwm_fan_suspend(struct device *dev)
  525. {
  526. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  527. return pwm_fan_power_off(ctx);
  528. }
  529. static int pwm_fan_resume(struct device *dev)
  530. {
  531. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  532. return set_pwm(ctx, ctx->pwm_value);
  533. }
  534. static DEFINE_SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
  535. static const struct of_device_id of_pwm_fan_match[] = {
  536. { .compatible = "pwm-fan", },
  537. {},
  538. };
  539. MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
  540. static struct platform_driver pwm_fan_driver = {
  541. .probe = pwm_fan_probe,
  542. .shutdown = pwm_fan_shutdown,
  543. .driver = {
  544. .name = "pwm-fan",
  545. .pm = pm_sleep_ptr(&pwm_fan_pm),
  546. .of_match_table = of_pwm_fan_match,
  547. },
  548. };
  549. module_platform_driver(pwm_fan_driver);
  550. MODULE_AUTHOR("Kamil Debski <[email protected]>");
  551. MODULE_ALIAS("platform:pwm-fan");
  552. MODULE_DESCRIPTION("PWM FAN driver");
  553. MODULE_LICENSE("GPL");