thermal-cool-pwm-fan.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "thermal.cool.pwm-fan"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #define MAX_PWM 255
  17. struct pwm_fan_cool
  18. {
  19. struct rt_thermal_cooling_device parent;
  20. rt_uint32_t pwm_fan_level;
  21. rt_uint32_t pwm_fan_max_level;
  22. rt_uint32_t *pwm_fan_cooling_levels;
  23. struct rt_device_pwm *pwm_dev;
  24. struct rt_pwm_configuration pwm_conf;
  25. struct rt_regulator *supply;
  26. struct rt_spinlock lock;
  27. };
  28. #define raw_to_pwm_fan_cool(raw) rt_container_of(raw, struct pwm_fan_cool, parent)
  29. static rt_err_t pwm_fan_power_on(struct pwm_fan_cool *pf_cool)
  30. {
  31. rt_err_t err = RT_EOK;
  32. if ((err = rt_pwm_enable(pf_cool->pwm_dev, pf_cool->pwm_conf.channel)))
  33. {
  34. return err;
  35. }
  36. if (pf_cool->supply && (err = rt_regulator_enable(pf_cool->supply)))
  37. {
  38. rt_pwm_disable(pf_cool->pwm_dev, pf_cool->pwm_conf.channel);
  39. return err;
  40. }
  41. return err;
  42. }
  43. static rt_err_t pwm_fan_power_off(struct pwm_fan_cool *pf_cool)
  44. {
  45. rt_err_t err = RT_EOK;
  46. if (pf_cool->supply && (err = rt_regulator_disable(pf_cool->supply)))
  47. {
  48. return err;
  49. }
  50. if ((err = rt_pwm_disable(pf_cool->pwm_dev, pf_cool->pwm_conf.channel)))
  51. {
  52. rt_regulator_enable(pf_cool->supply);
  53. return err;
  54. }
  55. return err;
  56. }
  57. static rt_err_t pwm_fan_cool_get_max_level(struct rt_thermal_cooling_device *cdev,
  58. rt_ubase_t *out_level)
  59. {
  60. struct pwm_fan_cool *pf_cool = raw_to_pwm_fan_cool(cdev);
  61. *out_level = pf_cool->pwm_fan_max_level;
  62. return RT_EOK;
  63. }
  64. static rt_err_t pwm_fan_cool_get_cur_level(struct rt_thermal_cooling_device *cdev,
  65. rt_ubase_t *out_level)
  66. {
  67. struct pwm_fan_cool *pf_cool = raw_to_pwm_fan_cool(cdev);
  68. *out_level = pf_cool->pwm_fan_level;
  69. return RT_EOK;
  70. }
  71. static rt_err_t pwm_fan_cool_set_cur_level(struct rt_thermal_cooling_device *cdev,
  72. rt_ubase_t level)
  73. {
  74. rt_ubase_t pwm;
  75. rt_err_t err = RT_EOK;
  76. struct pwm_fan_cool *pf_cool = raw_to_pwm_fan_cool(cdev);
  77. if (pf_cool->pwm_fan_level == level)
  78. {
  79. return RT_EOK;
  80. }
  81. rt_spin_lock(&pf_cool->lock);
  82. if ((pwm = pf_cool->pwm_fan_cooling_levels[level]))
  83. {
  84. rt_ubase_t period;
  85. struct rt_pwm_configuration *pwm_conf = &pf_cool->pwm_conf;
  86. period = pwm_conf->period;
  87. pwm_conf->pulse = RT_DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
  88. err = rt_pwm_set(pf_cool->pwm_dev,
  89. pwm_conf->channel, pwm_conf->period, pwm_conf->pulse);
  90. if (!err && pf_cool->pwm_fan_level == 0)
  91. {
  92. err = pwm_fan_power_on(pf_cool);
  93. }
  94. }
  95. else if (pf_cool->pwm_fan_level > 0)
  96. {
  97. err = pwm_fan_power_off(pf_cool);
  98. }
  99. rt_spin_unlock(&pf_cool->lock);
  100. if (!err)
  101. {
  102. pf_cool->pwm_fan_level = level;
  103. }
  104. return RT_EOK;
  105. }
  106. const static struct rt_thermal_cooling_device_ops pwm_fan_cool_ops =
  107. {
  108. .get_max_level = pwm_fan_cool_get_max_level,
  109. .get_cur_level = pwm_fan_cool_get_cur_level,
  110. .set_cur_level = pwm_fan_cool_set_cur_level,
  111. };
  112. static void pwm_fan_cool_free(struct pwm_fan_cool *pf_cool)
  113. {
  114. if (!rt_is_err_or_null(pf_cool->supply))
  115. {
  116. rt_regulator_put(pf_cool->supply);
  117. }
  118. if (pf_cool->pwm_fan_cooling_levels)
  119. {
  120. rt_free(pf_cool->pwm_fan_cooling_levels);
  121. }
  122. rt_free(pf_cool);
  123. }
  124. static rt_err_t pwm_fan_cool_probe(struct rt_platform_device *pdev)
  125. {
  126. rt_err_t err;
  127. int levels_nr;
  128. struct rt_ofw_cell_args pwm_args;
  129. struct rt_device *dev = &pdev->parent;
  130. struct rt_ofw_node *np = dev->ofw_node, *pwm_np;
  131. struct pwm_fan_cool *pf_cool = rt_calloc(1, sizeof(*pf_cool));
  132. if (!pf_cool)
  133. {
  134. return -RT_ENOMEM;
  135. }
  136. if (rt_ofw_parse_phandle_cells(np, "pwms", "#pwm-cells", 0, &pwm_args))
  137. {
  138. err = -RT_EINVAL;
  139. goto _fail;
  140. }
  141. pwm_np = pwm_args.data;
  142. if (!rt_ofw_data(pwm_np))
  143. {
  144. rt_platform_ofw_request(pwm_np);
  145. }
  146. pf_cool->pwm_dev = rt_ofw_data(pwm_np);
  147. rt_ofw_node_put(pwm_np);
  148. if (!pf_cool->pwm_dev)
  149. {
  150. err = -RT_EINVAL;
  151. goto _fail;
  152. }
  153. pf_cool->pwm_conf.channel = pwm_args.args[0];
  154. pf_cool->pwm_conf.period = pwm_args.args[1];
  155. pf_cool->supply = rt_regulator_get(dev, "fan");
  156. if (rt_is_err(pf_cool->supply))
  157. {
  158. err = rt_ptr_err(pf_cool->supply);
  159. goto _fail;
  160. }
  161. if ((levels_nr = rt_dm_dev_prop_count_of_u32(dev, "cooling-levels")) <= 0)
  162. {
  163. err = -RT_EINVAL;
  164. goto _fail;
  165. }
  166. pf_cool->pwm_fan_cooling_levels = rt_calloc(levels_nr, sizeof(rt_uint32_t));
  167. if (!pf_cool->pwm_fan_cooling_levels)
  168. {
  169. err = -RT_ENOMEM;
  170. goto _fail;
  171. }
  172. if (rt_dm_dev_prop_read_u32_array_index(dev, "cooling-levels",
  173. 0, levels_nr, pf_cool->pwm_fan_cooling_levels) <= 0)
  174. {
  175. err = -RT_EINVAL;
  176. goto _fail;
  177. }
  178. pf_cool->pwm_fan_level = MAX_PWM;
  179. pf_cool->pwm_fan_max_level = levels_nr - 1;
  180. rt_spin_lock_init(&pf_cool->lock);
  181. pwm_fan_cool_set_cur_level(&pf_cool->parent, 0);
  182. rt_dm_dev_set_name(&pf_cool->parent.parent, "%s", rt_dm_dev_get_name(&pdev->parent));
  183. pf_cool->parent.parent.ofw_node = dev->ofw_node;
  184. pf_cool->parent.ops = &pwm_fan_cool_ops;
  185. if ((err = rt_thermal_cooling_device_register(&pf_cool->parent)))
  186. {
  187. goto _fail;
  188. }
  189. dev->user_data = pf_cool;
  190. return RT_EOK;
  191. _fail:
  192. pwm_fan_cool_free(pf_cool);
  193. return err;
  194. }
  195. static rt_err_t pwm_fan_cool_remove(struct rt_platform_device *pdev)
  196. {
  197. struct pwm_fan_cool *pf_cool = pdev->parent.ofw_node;
  198. rt_thermal_cooling_device_unregister(&pf_cool->parent);
  199. pwm_fan_power_off(pf_cool);
  200. pwm_fan_cool_free(pf_cool);
  201. return RT_EOK;
  202. }
  203. static rt_err_t pwm_fan_cool_shutdown(struct rt_platform_device *pdev)
  204. {
  205. return pwm_fan_cool_remove(pdev);
  206. }
  207. static const struct rt_ofw_node_id pwm_fan_cool_ofw_ids[] =
  208. {
  209. { .compatible = "pwm-fan" },
  210. { /* sentinel */ }
  211. };
  212. static struct rt_platform_driver pwm_fan_cool_driver =
  213. {
  214. .name = "pwm-fan-cool",
  215. .ids = pwm_fan_cool_ofw_ids,
  216. .probe = pwm_fan_cool_probe,
  217. .remove = pwm_fan_cool_remove,
  218. .shutdown = pwm_fan_cool_shutdown,
  219. };
  220. RT_PLATFORM_DRIVER_EXPORT(pwm_fan_cool_driver);