rpmpd.c 11 KB

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