pwm-fan.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. ret = PTR_ERR(ctx->pwm);
  178. if (ret != -EPROBE_DEFER)
  179. dev_err(&pdev->dev, "Could not get PWM: %d\n", ret);
  180. return ret;
  181. }
  182. platform_set_drvdata(pdev, ctx);
  183. ctx->pwm_value = MAX_PWM;
  184. /* Set duty cycle to maximum allowed and enable PWM output */
  185. pwm_init_state(ctx->pwm, &state);
  186. state.duty_cycle = ctx->pwm->args.period - 1;
  187. state.enabled = true;
  188. ret = pwm_apply_state(ctx->pwm, &state);
  189. if (ret) {
  190. dev_err(&pdev->dev, "Failed to configure PWM\n");
  191. return ret;
  192. }
  193. hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
  194. ctx, pwm_fan_groups);
  195. if (IS_ERR(hwmon)) {
  196. dev_err(&pdev->dev, "Failed to register hwmon device\n");
  197. ret = PTR_ERR(hwmon);
  198. goto err_pwm_disable;
  199. }
  200. ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
  201. if (ret)
  202. goto err_pwm_disable;
  203. ctx->pwm_fan_state = ctx->pwm_fan_max_state;
  204. if (IS_ENABLED(CONFIG_THERMAL)) {
  205. cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
  206. "pwm-fan", ctx,
  207. &pwm_fan_cooling_ops);
  208. if (IS_ERR(cdev)) {
  209. dev_err(&pdev->dev,
  210. "Failed to register pwm-fan as cooling device");
  211. ret = PTR_ERR(cdev);
  212. goto err_pwm_disable;
  213. }
  214. ctx->cdev = cdev;
  215. thermal_cdev_update(cdev);
  216. }
  217. return 0;
  218. err_pwm_disable:
  219. state.enabled = false;
  220. pwm_apply_state(ctx->pwm, &state);
  221. return ret;
  222. }
  223. static int pwm_fan_remove(struct platform_device *pdev)
  224. {
  225. struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
  226. thermal_cooling_device_unregister(ctx->cdev);
  227. if (ctx->pwm_value)
  228. pwm_disable(ctx->pwm);
  229. return 0;
  230. }
  231. #ifdef CONFIG_PM_SLEEP
  232. static int pwm_fan_suspend(struct device *dev)
  233. {
  234. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  235. struct pwm_args args;
  236. int ret;
  237. pwm_get_args(ctx->pwm, &args);
  238. if (ctx->pwm_value) {
  239. ret = pwm_config(ctx->pwm, 0, args.period);
  240. if (ret < 0)
  241. return ret;
  242. pwm_disable(ctx->pwm);
  243. }
  244. return 0;
  245. }
  246. static int pwm_fan_resume(struct device *dev)
  247. {
  248. struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
  249. struct pwm_args pargs;
  250. unsigned long duty;
  251. int ret;
  252. if (ctx->pwm_value == 0)
  253. return 0;
  254. pwm_get_args(ctx->pwm, &pargs);
  255. duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
  256. ret = pwm_config(ctx->pwm, duty, pargs.period);
  257. if (ret)
  258. return ret;
  259. return pwm_enable(ctx->pwm);
  260. }
  261. #endif
  262. static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
  263. static const struct of_device_id of_pwm_fan_match[] = {
  264. { .compatible = "pwm-fan", },
  265. {},
  266. };
  267. MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
  268. static struct platform_driver pwm_fan_driver = {
  269. .probe = pwm_fan_probe,
  270. .remove = pwm_fan_remove,
  271. .driver = {
  272. .name = "pwm-fan",
  273. .pm = &pwm_fan_pm,
  274. .of_match_table = of_pwm_fan_match,
  275. },
  276. };
  277. module_platform_driver(pwm_fan_driver);
  278. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  279. MODULE_ALIAS("platform:pwm-fan");
  280. MODULE_DESCRIPTION("PWM FAN driver");
  281. MODULE_LICENSE("GPL");