pwm-fan.c 7.8 KB

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