ddr_cdev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/thermal.h>
  8. #include <linux/err.h>
  9. #include <linux/slab.h>
  10. #include <linux/of_device.h>
  11. #include <linux/interconnect.h>
  12. #define DDR_CDEV_NAME "ddr-cdev"
  13. struct ddr_cdev {
  14. uint32_t cur_state;
  15. uint32_t max_state;
  16. struct thermal_cooling_device *cdev;
  17. struct icc_path *icc_path;
  18. struct device *dev;
  19. uint32_t *freq_table;
  20. uint32_t freq_table_size;
  21. };
  22. /**
  23. * ddr_set_cur_state - callback function to set the ddr state.
  24. * @cdev: thermal cooling device pointer.
  25. * @state: set this variable to the current cooling state.
  26. *
  27. * Callback for the thermal cooling device to change the
  28. * DDR state.
  29. *
  30. * Return: 0 on success, an error code otherwise.
  31. */
  32. static int ddr_set_cur_state(struct thermal_cooling_device *cdev,
  33. unsigned long state)
  34. {
  35. struct ddr_cdev *ddr_cdev = cdev->devdata;
  36. int ret = 0;
  37. /* Request state should be less than max_level */
  38. if (state > ddr_cdev->max_state)
  39. return -EINVAL;
  40. /* Check if the old cooling action is same as new cooling action */
  41. if (ddr_cdev->cur_state == state)
  42. return 0;
  43. ret = icc_set_bw(ddr_cdev->icc_path, 0, ddr_cdev->freq_table[state]);
  44. if (ret < 0) {
  45. dev_err(ddr_cdev->dev, "Error placing DDR freq%u. err:%d\n",
  46. ddr_cdev->freq_table[state], ret);
  47. return ret;
  48. }
  49. ddr_cdev->cur_state = state;
  50. return ret;
  51. }
  52. /**
  53. * ddr_get_cur_state - callback function to get the current cooling
  54. * state.
  55. * @cdev: thermal cooling device pointer.
  56. * @state: fill this variable with the current cooling state.
  57. *
  58. * Callback for the thermal cooling device to return the
  59. * current DDR state request.
  60. *
  61. * Return: 0 on success, an error code otherwise.
  62. */
  63. static int ddr_get_cur_state(struct thermal_cooling_device *cdev,
  64. unsigned long *state)
  65. {
  66. struct ddr_cdev *ddr_cdev = cdev->devdata;
  67. *state = ddr_cdev->cur_state;
  68. return 0;
  69. }
  70. /**
  71. * ddr_get_max_state - callback function to get the max cooling state.
  72. * @cdev: thermal cooling device pointer.
  73. * @state: fill this variable with the max cooling state.
  74. *
  75. * Callback for the thermal cooling device to return the DDR
  76. * max cooling state.
  77. *
  78. * Return: 0 on success, an error code otherwise.
  79. */
  80. static int ddr_get_max_state(struct thermal_cooling_device *cdev,
  81. unsigned long *state)
  82. {
  83. struct ddr_cdev *ddr_cdev = cdev->devdata;
  84. *state = ddr_cdev->max_state;
  85. return 0;
  86. }
  87. static struct thermal_cooling_device_ops ddr_cdev_ops = {
  88. .get_max_state = ddr_get_max_state,
  89. .get_cur_state = ddr_get_cur_state,
  90. .set_cur_state = ddr_set_cur_state,
  91. };
  92. static int ddr_cdev_probe(struct platform_device *pdev)
  93. {
  94. int ret = 0, opp_ct = 0, bus_width = 1, idx = 0;
  95. struct ddr_cdev *ddr_cdev = NULL;
  96. struct device_node *np = pdev->dev.of_node;
  97. struct device *dev = &pdev->dev;
  98. uint32_t *freq_table = NULL;
  99. char cdev_name[THERMAL_NAME_LENGTH] = DDR_CDEV_NAME;
  100. ddr_cdev = devm_kzalloc(dev, sizeof(*ddr_cdev), GFP_KERNEL);
  101. if (!ddr_cdev)
  102. return -ENOMEM;
  103. ddr_cdev->icc_path = of_icc_get(dev, NULL);
  104. if (IS_ERR(ddr_cdev->icc_path)) {
  105. ret = PTR_ERR(ddr_cdev->icc_path);
  106. if (ret != -EPROBE_DEFER)
  107. dev_err(dev, "Unable to register icc path: %d\n",
  108. ret);
  109. return ret;
  110. }
  111. if (!of_find_property(np, "qcom,freq-table", &opp_ct)) {
  112. dev_err(dev, "No DDR frequency entries\n");
  113. ret = -ENODEV;
  114. goto err_exit;
  115. }
  116. if (opp_ct <= 0) {
  117. dev_err(dev, "No DDR frequency\n");
  118. ret = -ENODEV;
  119. goto err_exit;
  120. }
  121. opp_ct = opp_ct / sizeof(*freq_table);
  122. /* Add one more entry for 0 or no DDR BW vote */
  123. opp_ct++;
  124. freq_table = devm_kcalloc(dev, opp_ct, sizeof(*freq_table), GFP_KERNEL);
  125. if (!freq_table) {
  126. ret = -ENOMEM;
  127. goto err_exit;
  128. }
  129. freq_table[0] = 0;
  130. ret = of_property_read_u32_array(np, "qcom,freq-table",
  131. &freq_table[1], opp_ct - 1);
  132. if (ret < 0) {
  133. dev_err(dev, "DDR frequency read error:%d\n", ret);
  134. goto err_exit;
  135. }
  136. ddr_cdev->freq_table = freq_table;
  137. ddr_cdev->freq_table_size = opp_ct;
  138. ddr_cdev->cur_state = 0;
  139. ddr_cdev->max_state = opp_ct - 1;
  140. ddr_cdev->dev = dev;
  141. ret = of_property_read_u32(np, "qcom,bus-width", &bus_width);
  142. if (ret < 0) {
  143. dev_err(dev, "DDR bus width read error:%d\n", ret);
  144. goto err_exit;
  145. }
  146. for (idx = 0; idx < opp_ct; idx++)
  147. ddr_cdev->freq_table[idx] *= bus_width;
  148. ret = icc_set_bw(ddr_cdev->icc_path, 0, freq_table[0]);
  149. if (ret < 0) {
  150. dev_err(dev, "Error placing DDR freq request. err:%d\n",
  151. ret);
  152. goto err_exit;
  153. }
  154. ddr_cdev->cdev = thermal_of_cooling_device_register(np, cdev_name,
  155. ddr_cdev, &ddr_cdev_ops);
  156. if (IS_ERR(ddr_cdev->cdev)) {
  157. ret = PTR_ERR(ddr_cdev->cdev);
  158. dev_err(dev, "Cdev register failed for %s, ret:%d\n",
  159. cdev_name, ret);
  160. goto err_exit;
  161. }
  162. dev_dbg(dev, "Cooling device [%s] registered.\n", cdev_name);
  163. dev_set_drvdata(dev, ddr_cdev);
  164. return 0;
  165. err_exit:
  166. icc_put(ddr_cdev->icc_path);
  167. return ret;
  168. }
  169. static int ddr_cdev_remove(struct platform_device *pdev)
  170. {
  171. struct ddr_cdev *ddr_cdev =
  172. (struct ddr_cdev *)dev_get_drvdata(&pdev->dev);
  173. if (ddr_cdev->cdev) {
  174. thermal_cooling_device_unregister(ddr_cdev->cdev);
  175. ddr_cdev->cdev = NULL;
  176. }
  177. if (ddr_cdev->icc_path) {
  178. icc_set_bw(ddr_cdev->icc_path, 0, ddr_cdev->freq_table[0]);
  179. icc_put(ddr_cdev->icc_path);
  180. ddr_cdev->icc_path = NULL;
  181. }
  182. return 0;
  183. }
  184. static const struct of_device_id ddr_cdev_match[] = {
  185. { .compatible = "qcom,ddr-cooling-device", },
  186. {},
  187. };
  188. static struct platform_driver ddr_cdev_driver = {
  189. .probe = ddr_cdev_probe,
  190. .remove = ddr_cdev_remove,
  191. .driver = {
  192. .name = KBUILD_MODNAME,
  193. .of_match_table = ddr_cdev_match,
  194. },
  195. };
  196. module_platform_driver(ddr_cdev_driver);
  197. MODULE_LICENSE("GPL");