rpmpd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. */
  3. #include <linux/err.h>
  4. #include <linux/init.h>
  5. #include <linux/kernel.h>
  6. #include <linux/mutex.h>
  7. #include <linux/pm_domain.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_opp.h>
  12. #include <linux/soc/qcom/smd-rpm.h>
  13. #include <dt-bindings/power/qcom-rpmpd.h>
  14. #define domain_to_rpmpd(domain) container_of(domain, struct rpmpd, pd)
  15. /* Resource types:
  16. * RPMPD_X is X encoded as a little-endian, lower-case, ASCII string */
  17. #define RPMPD_SMPA 0x61706d73
  18. #define RPMPD_LDOA 0x616f646c
  19. #define RPMPD_RWCX 0x78637772
  20. #define RPMPD_RWMX 0x786d7772
  21. #define RPMPD_RWLC 0x636c7772
  22. #define RPMPD_RWLM 0x6d6c7772
  23. #define RPMPD_RWSC 0x63737772
  24. #define RPMPD_RWSM 0x6d737772
  25. /* Operation Keys */
  26. #define KEY_CORNER 0x6e726f63 /* corn */
  27. #define KEY_ENABLE 0x6e657773 /* swen */
  28. #define KEY_FLOOR_CORNER 0x636676 /* vfc */
  29. #define KEY_FLOOR_LEVEL 0x6c6676 /* vfl */
  30. #define KEY_LEVEL 0x6c766c76 /* vlvl */
  31. #define MAX_8996_RPMPD_STATE 6
  32. #define DEFINE_RPMPD_PAIR(_platform, _name, _active, r_type, r_key, \
  33. r_id) \
  34. static struct rpmpd _platform##_##_active; \
  35. static struct rpmpd _platform##_##_name = { \
  36. .pd = { .name = #_name, }, \
  37. .peer = &_platform##_##_active, \
  38. .res_type = RPMPD_##r_type, \
  39. .res_id = r_id, \
  40. .key = KEY_##r_key, \
  41. }; \
  42. static struct rpmpd _platform##_##_active = { \
  43. .pd = { .name = #_active, }, \
  44. .peer = &_platform##_##_name, \
  45. .active_only = true, \
  46. .res_type = RPMPD_##r_type, \
  47. .res_id = r_id, \
  48. .key = KEY_##r_key, \
  49. }
  50. #define DEFINE_RPMPD_CORNER(_platform, _name, r_type, r_id) \
  51. static struct rpmpd _platform##_##_name = { \
  52. .pd = { .name = #_name, }, \
  53. .res_type = RPMPD_##r_type, \
  54. .res_id = r_id, \
  55. .key = KEY_CORNER, \
  56. }
  57. #define DEFINE_RPMPD_LEVEL(_platform, _name, r_type, r_id) \
  58. static struct rpmpd _platform##_##_name = { \
  59. .pd = { .name = #_name, }, \
  60. .res_type = RPMPD_##r_type, \
  61. .res_id = r_id, \
  62. .key = KEY_LEVEL, \
  63. }
  64. #define DEFINE_RPMPD_VFC(_platform, _name, r_type, r_id) \
  65. static struct rpmpd _platform##_##_name = { \
  66. .pd = { .name = #_name, }, \
  67. .res_type = RPMPD_##r_type, \
  68. .res_id = r_id, \
  69. .key = KEY_FLOOR_CORNER, \
  70. }
  71. #define DEFINE_RPMPD_VFL(_platform, _name, r_type, r_id) \
  72. static struct rpmpd _platform##_##_name = { \
  73. .pd = { .name = #_name, }, \
  74. .res_type = RPMPD_##r_type, \
  75. .res_id = r_id, \
  76. .key = KEY_FLOOR_LEVEL, \
  77. }
  78. struct rpmpd_req {
  79. __le32 key;
  80. __le32 nbytes;
  81. __le32 value;
  82. };
  83. struct rpmpd {
  84. struct generic_pm_domain pd;
  85. struct rpmpd *peer;
  86. const bool active_only;
  87. unsigned int corner;
  88. bool enabled;
  89. const char *res_name;
  90. const int res_type;
  91. const int res_id;
  92. struct qcom_smd_rpm *rpm;
  93. unsigned int max_state;
  94. __le32 key;
  95. };
  96. struct rpmpd_desc {
  97. struct rpmpd **rpmpds;
  98. size_t num_pds;
  99. unsigned int max_state;
  100. };
  101. static DEFINE_MUTEX(rpmpd_lock);
  102. /* msm8996 RPM Power domains */
  103. DEFINE_RPMPD_PAIR(msm8996, vddcx, vddcx_ao, SMPA, CORNER, 1);
  104. DEFINE_RPMPD_PAIR(msm8996, vddmx, vddmx_ao, SMPA, CORNER, 2);
  105. DEFINE_RPMPD_CORNER(msm8996, vddsscx, LDOA, 26);
  106. DEFINE_RPMPD_VFC(msm8996, vddcx_vfc, SMPA, 1);
  107. DEFINE_RPMPD_VFC(msm8996, vddsscx_vfc, LDOA, 26);
  108. static struct rpmpd *msm8996_rpmpds[] = {
  109. [MSM8996_VDDCX] = &msm8996_vddcx,
  110. [MSM8996_VDDCX_AO] = &msm8996_vddcx_ao,
  111. [MSM8996_VDDCX_VFC] = &msm8996_vddcx_vfc,
  112. [MSM8996_VDDMX] = &msm8996_vddmx,
  113. [MSM8996_VDDMX_AO] = &msm8996_vddmx_ao,
  114. [MSM8996_VDDSSCX] = &msm8996_vddsscx,
  115. [MSM8996_VDDSSCX_VFC] = &msm8996_vddsscx_vfc,
  116. };
  117. static const struct rpmpd_desc msm8996_desc = {
  118. .rpmpds = msm8996_rpmpds,
  119. .num_pds = ARRAY_SIZE(msm8996_rpmpds),
  120. .max_state = MAX_8996_RPMPD_STATE,
  121. };
  122. /* msm8998 RPM Power domains */
  123. DEFINE_RPMPD_PAIR(msm8998, vddcx, vddcx_ao, RWCX, LEVEL, 0);
  124. DEFINE_RPMPD_VFL(msm8998, vddcx_vfl, RWCX, 0);
  125. DEFINE_RPMPD_PAIR(msm8998, vddmx, vddmx_ao, RWMX, LEVEL, 0);
  126. DEFINE_RPMPD_VFL(msm8998, vddmx_vfl, RWMX, 0);
  127. DEFINE_RPMPD_LEVEL(msm8998, vdd_ssccx, RWSC, 0);
  128. DEFINE_RPMPD_VFL(msm8998, vdd_ssccx_vfl, RWSC, 0);
  129. DEFINE_RPMPD_LEVEL(msm8998, vdd_sscmx, RWSM, 0);
  130. DEFINE_RPMPD_VFL(msm8998, vdd_sscmx_vfl, RWSM, 0);
  131. static struct rpmpd *msm8998_rpmpds[] = {
  132. [MSM8998_VDDCX] = &msm8998_vddcx,
  133. [MSM8998_VDDCX_AO] = &msm8998_vddcx_ao,
  134. [MSM8998_VDDCX_VFL] = &msm8998_vddcx_vfl,
  135. [MSM8998_VDDMX] = &msm8998_vddmx,
  136. [MSM8998_VDDMX_AO] = &msm8998_vddmx_ao,
  137. [MSM8998_VDDMX_VFL] = &msm8998_vddmx_vfl,
  138. [MSM8998_SSCCX] = &msm8998_vdd_ssccx,
  139. [MSM8998_SSCCX_VFL] = &msm8998_vdd_ssccx_vfl,
  140. [MSM8998_SSCMX] = &msm8998_vdd_sscmx,
  141. [MSM8998_SSCMX_VFL] = &msm8998_vdd_sscmx_vfl,
  142. };
  143. static const struct rpmpd_desc msm8998_desc = {
  144. .rpmpds = msm8998_rpmpds,
  145. .num_pds = ARRAY_SIZE(msm8998_rpmpds),
  146. .max_state = RPM_SMD_LEVEL_BINNING,
  147. };
  148. /* qcs404 RPM Power domains */
  149. DEFINE_RPMPD_PAIR(qcs404, vddmx, vddmx_ao, RWMX, LEVEL, 0);
  150. DEFINE_RPMPD_VFL(qcs404, vddmx_vfl, RWMX, 0);
  151. DEFINE_RPMPD_LEVEL(qcs404, vdd_lpicx, RWLC, 0);
  152. DEFINE_RPMPD_VFL(qcs404, vdd_lpicx_vfl, RWLC, 0);
  153. DEFINE_RPMPD_LEVEL(qcs404, vdd_lpimx, RWLM, 0);
  154. DEFINE_RPMPD_VFL(qcs404, vdd_lpimx_vfl, RWLM, 0);
  155. static struct rpmpd *qcs404_rpmpds[] = {
  156. [QCS404_VDDMX] = &qcs404_vddmx,
  157. [QCS404_VDDMX_AO] = &qcs404_vddmx_ao,
  158. [QCS404_VDDMX_VFL] = &qcs404_vddmx_vfl,
  159. [QCS404_LPICX] = &qcs404_vdd_lpicx,
  160. [QCS404_LPICX_VFL] = &qcs404_vdd_lpicx_vfl,
  161. [QCS404_LPIMX] = &qcs404_vdd_lpimx,
  162. [QCS404_LPIMX_VFL] = &qcs404_vdd_lpimx_vfl,
  163. };
  164. static const struct rpmpd_desc qcs404_desc = {
  165. .rpmpds = qcs404_rpmpds,
  166. .num_pds = ARRAY_SIZE(qcs404_rpmpds),
  167. .max_state = RPM_SMD_LEVEL_BINNING,
  168. };
  169. static const struct of_device_id rpmpd_match_table[] = {
  170. { .compatible = "qcom,msm8996-rpmpd", .data = &msm8996_desc },
  171. { .compatible = "qcom,msm8998-rpmpd", .data = &msm8998_desc },
  172. { .compatible = "qcom,qcs404-rpmpd", .data = &qcs404_desc },
  173. { }
  174. };
  175. static int rpmpd_send_enable(struct rpmpd *pd, bool enable)
  176. {
  177. struct rpmpd_req req = {
  178. .key = KEY_ENABLE,
  179. .nbytes = cpu_to_le32(sizeof(u32)),
  180. .value = cpu_to_le32(enable),
  181. };
  182. return qcom_rpm_smd_write(pd->rpm, QCOM_SMD_RPM_ACTIVE_STATE,
  183. pd->res_type, pd->res_id, &req, sizeof(req));
  184. }
  185. static int rpmpd_send_corner(struct rpmpd *pd, int state, unsigned int corner)
  186. {
  187. struct rpmpd_req req = {
  188. .key = pd->key,
  189. .nbytes = cpu_to_le32(sizeof(u32)),
  190. .value = cpu_to_le32(corner),
  191. };
  192. return qcom_rpm_smd_write(pd->rpm, state, pd->res_type, pd->res_id,
  193. &req, sizeof(req));
  194. };
  195. static void to_active_sleep(struct rpmpd *pd, unsigned int corner,
  196. unsigned int *active, unsigned int *sleep)
  197. {
  198. *active = corner;
  199. if (pd->active_only)
  200. *sleep = 0;
  201. else
  202. *sleep = *active;
  203. }
  204. static int rpmpd_aggregate_corner(struct rpmpd *pd)
  205. {
  206. int ret;
  207. struct rpmpd *peer = pd->peer;
  208. unsigned int active_corner, sleep_corner;
  209. unsigned int this_active_corner = 0, this_sleep_corner = 0;
  210. unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
  211. to_active_sleep(pd, pd->corner, &this_active_corner, &this_sleep_corner);
  212. if (peer && peer->enabled)
  213. to_active_sleep(peer, peer->corner, &peer_active_corner,
  214. &peer_sleep_corner);
  215. active_corner = max(this_active_corner, peer_active_corner);
  216. ret = rpmpd_send_corner(pd, QCOM_SMD_RPM_ACTIVE_STATE, active_corner);
  217. if (ret)
  218. return ret;
  219. sleep_corner = max(this_sleep_corner, peer_sleep_corner);
  220. return rpmpd_send_corner(pd, QCOM_SMD_RPM_SLEEP_STATE, sleep_corner);
  221. }
  222. static int rpmpd_power_on(struct generic_pm_domain *domain)
  223. {
  224. int ret;
  225. struct rpmpd *pd = domain_to_rpmpd(domain);
  226. mutex_lock(&rpmpd_lock);
  227. ret = rpmpd_send_enable(pd, true);
  228. if (ret)
  229. goto out;
  230. pd->enabled = true;
  231. if (pd->corner)
  232. ret = rpmpd_aggregate_corner(pd);
  233. out:
  234. mutex_unlock(&rpmpd_lock);
  235. return ret;
  236. }
  237. static int rpmpd_power_off(struct generic_pm_domain *domain)
  238. {
  239. int ret;
  240. struct rpmpd *pd = domain_to_rpmpd(domain);
  241. mutex_lock(&rpmpd_lock);
  242. ret = rpmpd_send_enable(pd, false);
  243. if (!ret)
  244. pd->enabled = false;
  245. mutex_unlock(&rpmpd_lock);
  246. return ret;
  247. }
  248. static int rpmpd_set_performance(struct generic_pm_domain *domain,
  249. unsigned int state)
  250. {
  251. int ret = 0;
  252. struct rpmpd *pd = domain_to_rpmpd(domain);
  253. if (state > pd->max_state)
  254. state = pd->max_state;
  255. mutex_lock(&rpmpd_lock);
  256. pd->corner = state;
  257. /* Always send updates for vfc and vfl */
  258. if (!pd->enabled && pd->key != KEY_FLOOR_CORNER &&
  259. pd->key != KEY_FLOOR_LEVEL)
  260. goto out;
  261. ret = rpmpd_aggregate_corner(pd);
  262. out:
  263. mutex_unlock(&rpmpd_lock);
  264. return ret;
  265. }
  266. static unsigned int rpmpd_get_performance(struct generic_pm_domain *genpd,
  267. struct dev_pm_opp *opp)
  268. {
  269. return dev_pm_opp_get_level(opp);
  270. }
  271. static int rpmpd_probe(struct platform_device *pdev)
  272. {
  273. int i;
  274. size_t num;
  275. struct genpd_onecell_data *data;
  276. struct qcom_smd_rpm *rpm;
  277. struct rpmpd **rpmpds;
  278. const struct rpmpd_desc *desc;
  279. rpm = dev_get_drvdata(pdev->dev.parent);
  280. if (!rpm) {
  281. dev_err(&pdev->dev, "Unable to retrieve handle to RPM\n");
  282. return -ENODEV;
  283. }
  284. desc = of_device_get_match_data(&pdev->dev);
  285. if (!desc)
  286. return -EINVAL;
  287. rpmpds = desc->rpmpds;
  288. num = desc->num_pds;
  289. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  290. if (!data)
  291. return -ENOMEM;
  292. data->domains = devm_kcalloc(&pdev->dev, num, sizeof(*data->domains),
  293. GFP_KERNEL);
  294. data->num_domains = num;
  295. for (i = 0; i < num; i++) {
  296. if (!rpmpds[i]) {
  297. dev_warn(&pdev->dev, "rpmpds[] with empty entry at index=%d\n",
  298. i);
  299. continue;
  300. }
  301. rpmpds[i]->rpm = rpm;
  302. rpmpds[i]->max_state = desc->max_state;
  303. rpmpds[i]->pd.power_off = rpmpd_power_off;
  304. rpmpds[i]->pd.power_on = rpmpd_power_on;
  305. rpmpds[i]->pd.set_performance_state = rpmpd_set_performance;
  306. rpmpds[i]->pd.opp_to_performance_state = rpmpd_get_performance;
  307. pm_genpd_init(&rpmpds[i]->pd, NULL, true);
  308. data->domains[i] = &rpmpds[i]->pd;
  309. }
  310. return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
  311. }
  312. static struct platform_driver rpmpd_driver = {
  313. .driver = {
  314. .name = "qcom-rpmpd",
  315. .of_match_table = rpmpd_match_table,
  316. .suppress_bind_attrs = true,
  317. },
  318. .probe = rpmpd_probe,
  319. };
  320. static int __init rpmpd_init(void)
  321. {
  322. return platform_driver_register(&rpmpd_driver);
  323. }
  324. core_initcall(rpmpd_init);