pwm-fan.c 7.6 KB

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