pwm-fan.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include <linux/hwmon.h>
  19. #include <linux/hwmon-sysfs.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pwm.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/thermal.h>
  27. #define MAX_PWM 255
  28. struct pwm_fan_ctx {
  29. struct mutex lock;
  30. struct pwm_device *pwm;
  31. unsigned int pwm_value;
  32. unsigned int pwm_fan_state;
  33. unsigned int pwm_fan_max_state;
  34. unsigned int *pwm_fan_cooling_levels;
  35. struct thermal_cooling_device *cdev;
  36. };
  37. static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
  38. {
  39. unsigned long period;
  40. int ret = 0;
  41. struct pwm_state state = { };
  42. mutex_lock(&ctx->lock);
  43. if (ctx->pwm_value == pwm)
  44. goto exit_set_pwm_err;
  45. pwm_init_state(ctx->pwm, &state);
  46. period = ctx->pwm->args.period;
  47. state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
  48. state.enabled = pwm ? true : false;
  49. ret = pwm_apply_state(ctx->pwm, &state);
  50. if (!ret)
  51. ctx->pwm_value = pwm;
  52. exit_set_pwm_err:
  53. mutex_unlock(&ctx->lock);
  54. return ret;
  55. }
  56. static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
  57. {
  58. int i;
  59. for (i = 0; i < ctx->pwm_fan_max_state; ++i)
  60. if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
  61. break;
  62. ctx->pwm_fan_state = i;
  63. }
  64. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  65. const char *buf, size_t count)
  66. {
  67. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  68. unsigned long pwm;
  69. int ret;
  70. if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
  71. return -EINVAL;
  72. ret = __set_pwm(ctx, pwm);
  73. if (ret)
  74. return ret;
  75. pwm_fan_update_state(ctx, pwm);
  76. return count;
  77. }
  78. static ssize_t show_pwm(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  82. return sprintf(buf, "%u\n", ctx->pwm_value);
  83. }
  84. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
  85. static struct attribute *pwm_fan_attrs[] = {
  86. &sensor_dev_attr_pwm1.dev_attr.attr,
  87. NULL,
  88. };
  89. ATTRIBUTE_GROUPS(pwm_fan);
  90. /* thermal cooling device callbacks */
  91. static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
  92. unsigned long *state)
  93. {
  94. struct pwm_fan_ctx *ctx = cdev->devdata;
  95. if (!ctx)
  96. return -EINVAL;
  97. *state = ctx->pwm_fan_max_state;
  98. return 0;
  99. }
  100. static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
  101. unsigned long *state)
  102. {
  103. struct pwm_fan_ctx *ctx = cdev->devdata;
  104. if (!ctx)
  105. return -EINVAL;
  106. *state = ctx->pwm_fan_state;
  107. return 0;
  108. }
  109. static int
  110. pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  111. {
  112. struct pwm_fan_ctx *ctx = cdev->devdata;
  113. int ret;
  114. if (!ctx || (state > ctx->pwm_fan_max_state))
  115. return -EINVAL;
  116. if (state == ctx->pwm_fan_state)
  117. return 0;
  118. ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
  119. if (ret) {
  120. dev_err(&cdev->device, "Cannot set pwm!\n");
  121. return ret;
  122. }
  123. ctx->pwm_fan_state = state;
  124. return ret;
  125. }
  126. static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
  127. .get_max_state = pwm_fan_get_max_state,
  128. .get_cur_state = pwm_fan_get_cur_state,
  129. .set_cur_state = pwm_fan_set_cur_state,
  130. };
  131. static int pwm_fan_of_get_cooling_data(struct device *dev,
  132. struct pwm_fan_ctx *ctx)
  133. {
  134. struct device_node *np = dev->of_node;
  135. int num, i, ret;
  136. if (!of_find_property(np, "cooling-levels", NULL))
  137. return 0;
  138. ret = of_property_count_u32_elems(np, "cooling-levels");
  139. if (ret <= 0) {
  140. dev_err(dev, "Wrong data!\n");
  141. return ret ? : -EINVAL;
  142. }
  143. num = ret;
  144. ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
  145. GFP_KERNEL);
  146. if (!ctx->pwm_fan_cooling_levels)
  147. return -ENOMEM;
  148. ret = of_property_read_u32_array(np, "cooling-levels",
  149. ctx->pwm_fan_cooling_levels, num);
  150. if (ret) {
  151. dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
  152. return ret;
  153. }
  154. for (i = 0; i < num; i++) {
  155. if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
  156. dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
  157. ctx->pwm_fan_cooling_levels[i], MAX_PWM);
  158. return -EINVAL;
  159. }
  160. }
  161. ctx->pwm_fan_max_state = num - 1;
  162. return 0;
  163. }
  164. static int pwm_fan_probe(struct platform_device *pdev)
  165. {
  166. struct thermal_cooling_device *cdev;
  167. struct pwm_fan_ctx *ctx;
  168. struct device *hwmon;
  169. int ret;
  170. struct pwm_state state = { };
  171. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  172. if (!ctx)
  173. return -ENOMEM;
  174. mutex_init(&ctx->lock);
  175. ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
  176. if (IS_ERR(ctx->pwm)) {
  177. dev_err(&pdev->dev, "Could not get PWM\n");
  178. return PTR_ERR(ctx->pwm);
  179. }
  180. platform_set_drvdata(pdev, ctx);
  181. ctx->pwm_value = MAX_PWM;
  182. /* Set duty cycle to maximum allowed and enable PWM output */
  183. pwm_init_state(ctx->pwm, &state);
  184. state.duty_cycle = ctx->pwm->args.period - 1;
  185. state.enabled = true;
  186. ret = pwm_apply_state(ctx->pwm, &state);
  187. if (ret) {
  188. dev_err(&pdev->dev, "Failed to configure PWM\n");
  189. return ret;
  190. }
  191. hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
  192. ctx, pwm_fan_groups);
  193. if (IS_ERR(hwmon)) {
  194. dev_err(&pdev->dev, "Failed to register hwmon device\n");
  195. ret = PTR_ERR(hwmon);
  196. goto err_pwm_disable;
  197. }
  198. ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
  199. if (ret)
  200. return ret;
  201. ctx->pwm_fan_state = ctx->pwm_fan_max_state;
  202. if (IS_ENABLED(CONFIG_THERMAL)) {
  203. cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
  204. "pwm-fan", ctx,
  205. &pwm_fan_cooling_ops);
  206. if (IS_ERR(cdev)) {
  207. dev_err(&pdev->dev,
  208. "Failed to register pwm-fan as cooling device");
  209. ret = PTR_ERR(cdev);
  210. goto err_pwm_disable;
  211. }
  212. ctx->cdev = cdev;
  213. thermal_cdev_update(cdev);
  214. }
  215. return 0;
  216. err_pwm_disable:
  217. state.enabled = false;
  218. pwm_apply_state(ctx->pwm, &state);
  219. return ret;
  220. }
  221. static int pwm_fan_remove(struct platform_device *pdev)
  222. {
  223. struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
  224. thermal_cooling_device_unregister(ctx->cdev);
  225. if (ctx->pwm_value)
  226. pwm_disable(ctx->pwm);
  227. return 0;
  228. }
  229. #ifdef CONFIG_PM_SLEEP
  230. static int pwm_fan_suspend(struct device *dev)
  231. {
  232. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  233. if (ctx->pwm_value)
  234. pwm_disable(ctx->pwm);
  235. return 0;
  236. }
  237. static int pwm_fan_resume(struct device *dev)
  238. {
  239. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  240. struct pwm_args pargs;
  241. unsigned long duty;
  242. int ret;
  243. if (ctx->pwm_value == 0)
  244. return 0;
  245. pwm_get_args(ctx->pwm, &pargs);
  246. duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
  247. ret = pwm_config(ctx->pwm, duty, pargs.period);
  248. if (ret)
  249. return ret;
  250. return pwm_enable(ctx->pwm);
  251. }
  252. #endif
  253. static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
  254. static const struct of_device_id of_pwm_fan_match[] = {
  255. { .compatible = "pwm-fan", },
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
  259. static struct platform_driver pwm_fan_driver = {
  260. .probe = pwm_fan_probe,
  261. .remove = pwm_fan_remove,
  262. .driver = {
  263. .name = "pwm-fan",
  264. .pm = &pwm_fan_pm,
  265. .of_match_table = of_pwm_fan_match,
  266. },
  267. };
  268. module_platform_driver(pwm_fan_driver);
  269. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  270. MODULE_ALIAS("platform:pwm-fan");
  271. MODULE_DESCRIPTION("PWM FAN driver");
  272. MODULE_LICENSE("GPL");