rpmhpd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 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/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_opp.h>
  14. #include <soc/qcom/cmd-db.h>
  15. #include <soc/qcom/rpmh.h>
  16. #include <dt-bindings/power/qcom-rpmpd.h>
  17. #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd)
  18. #define RPMH_ARC_MAX_LEVELS 16
  19. /**
  20. * struct rpmhpd - top level RPMh power domain resource data structure
  21. * @dev: rpmh power domain controller device
  22. * @pd: generic_pm_domain corresponding to the power domain
  23. * @parent: generic_pm_domain corresponding to the parent's power domain
  24. * @peer: A peer power domain in case Active only Voting is
  25. * supported
  26. * @active_only: True if it represents an Active only peer
  27. * @corner: current corner
  28. * @active_corner: current active corner
  29. * @enable_corner: lowest non-zero corner
  30. * @level: An array of level (vlvl) to corner (hlvl) mappings
  31. * derived from cmd-db
  32. * @level_count: Number of levels supported by the power domain. max
  33. * being 16 (0 - 15)
  34. * @enabled: true if the power domain is enabled
  35. * @res_name: Resource name used for cmd-db lookup
  36. * @addr: Resource address as looped up using resource name from
  37. * cmd-db
  38. */
  39. struct rpmhpd {
  40. struct device *dev;
  41. struct generic_pm_domain pd;
  42. struct generic_pm_domain *parent;
  43. struct rpmhpd *peer;
  44. const bool active_only;
  45. unsigned int corner;
  46. unsigned int active_corner;
  47. unsigned int enable_corner;
  48. u32 level[RPMH_ARC_MAX_LEVELS];
  49. size_t level_count;
  50. bool enabled;
  51. const char *res_name;
  52. u32 addr;
  53. };
  54. struct rpmhpd_desc {
  55. struct rpmhpd **rpmhpds;
  56. size_t num_pds;
  57. };
  58. static DEFINE_MUTEX(rpmhpd_lock);
  59. /* RPMH powerdomains */
  60. static struct rpmhpd cx_ao;
  61. static struct rpmhpd mx;
  62. static struct rpmhpd mx_ao;
  63. static struct rpmhpd cx = {
  64. .pd = { .name = "cx", },
  65. .peer = &cx_ao,
  66. .res_name = "cx.lvl",
  67. };
  68. static struct rpmhpd cx_ao = {
  69. .pd = { .name = "cx_ao", },
  70. .active_only = true,
  71. .peer = &cx,
  72. .res_name = "cx.lvl",
  73. };
  74. static struct rpmhpd cx_ao_w_mx_parent;
  75. static struct rpmhpd cx_w_mx_parent = {
  76. .pd = { .name = "cx", },
  77. .peer = &cx_ao_w_mx_parent,
  78. .parent = &mx.pd,
  79. .res_name = "cx.lvl",
  80. };
  81. static struct rpmhpd cx_ao_w_mx_parent = {
  82. .pd = { .name = "cx_ao", },
  83. .active_only = true,
  84. .peer = &cx_w_mx_parent,
  85. .parent = &mx_ao.pd,
  86. .res_name = "cx.lvl",
  87. };
  88. static struct rpmhpd ebi = {
  89. .pd = { .name = "ebi", },
  90. .res_name = "ebi.lvl",
  91. };
  92. static struct rpmhpd gfx = {
  93. .pd = { .name = "gfx", },
  94. .res_name = "gfx.lvl",
  95. };
  96. static struct rpmhpd lcx = {
  97. .pd = { .name = "lcx", },
  98. .res_name = "lcx.lvl",
  99. };
  100. static struct rpmhpd lmx = {
  101. .pd = { .name = "lmx", },
  102. .res_name = "lmx.lvl",
  103. };
  104. static struct rpmhpd mmcx_ao;
  105. static struct rpmhpd mmcx = {
  106. .pd = { .name = "mmcx", },
  107. .peer = &mmcx_ao,
  108. .res_name = "mmcx.lvl",
  109. };
  110. static struct rpmhpd mmcx_ao = {
  111. .pd = { .name = "mmcx_ao", },
  112. .active_only = true,
  113. .peer = &mmcx,
  114. .res_name = "mmcx.lvl",
  115. };
  116. static struct rpmhpd mmcx_ao_w_cx_parent;
  117. static struct rpmhpd mmcx_w_cx_parent = {
  118. .pd = { .name = "mmcx", },
  119. .peer = &mmcx_ao_w_cx_parent,
  120. .parent = &cx.pd,
  121. .res_name = "mmcx.lvl",
  122. };
  123. static struct rpmhpd mmcx_ao_w_cx_parent = {
  124. .pd = { .name = "mmcx_ao", },
  125. .active_only = true,
  126. .peer = &mmcx_w_cx_parent,
  127. .parent = &cx_ao.pd,
  128. .res_name = "mmcx.lvl",
  129. };
  130. static struct rpmhpd mss = {
  131. .pd = { .name = "mss", },
  132. .res_name = "mss.lvl",
  133. };
  134. static struct rpmhpd mx_ao;
  135. static struct rpmhpd mx = {
  136. .pd = { .name = "mx", },
  137. .peer = &mx_ao,
  138. .res_name = "mx.lvl",
  139. };
  140. static struct rpmhpd mx_ao = {
  141. .pd = { .name = "mx_ao", },
  142. .active_only = true,
  143. .peer = &mx,
  144. .res_name = "mx.lvl",
  145. };
  146. static struct rpmhpd mxc_ao;
  147. static struct rpmhpd mxc = {
  148. .pd = { .name = "mxc", },
  149. .peer = &mxc_ao,
  150. .res_name = "mxc.lvl",
  151. };
  152. static struct rpmhpd mxc_ao = {
  153. .pd = { .name = "mxc_ao", },
  154. .active_only = true,
  155. .peer = &mxc,
  156. .res_name = "mxc.lvl",
  157. };
  158. static struct rpmhpd nsp = {
  159. .pd = { .name = "nsp", },
  160. .res_name = "nsp.lvl",
  161. };
  162. static struct rpmhpd qphy = {
  163. .pd = { .name = "qphy", },
  164. .res_name = "qphy.lvl",
  165. };
  166. /* SA8540P RPMH powerdomains */
  167. static struct rpmhpd *sa8540p_rpmhpds[] = {
  168. [SC8280XP_CX] = &cx,
  169. [SC8280XP_CX_AO] = &cx_ao,
  170. [SC8280XP_EBI] = &ebi,
  171. [SC8280XP_GFX] = &gfx,
  172. [SC8280XP_LCX] = &lcx,
  173. [SC8280XP_LMX] = &lmx,
  174. [SC8280XP_MMCX] = &mmcx,
  175. [SC8280XP_MMCX_AO] = &mmcx_ao,
  176. [SC8280XP_MX] = &mx,
  177. [SC8280XP_MX_AO] = &mx_ao,
  178. [SC8280XP_NSP] = &nsp,
  179. };
  180. static const struct rpmhpd_desc sa8540p_desc = {
  181. .rpmhpds = sa8540p_rpmhpds,
  182. .num_pds = ARRAY_SIZE(sa8540p_rpmhpds),
  183. };
  184. /* SDM845 RPMH powerdomains */
  185. static struct rpmhpd *sdm845_rpmhpds[] = {
  186. [SDM845_CX] = &cx_w_mx_parent,
  187. [SDM845_CX_AO] = &cx_ao_w_mx_parent,
  188. [SDM845_EBI] = &ebi,
  189. [SDM845_GFX] = &gfx,
  190. [SDM845_LCX] = &lcx,
  191. [SDM845_LMX] = &lmx,
  192. [SDM845_MSS] = &mss,
  193. [SDM845_MX] = &mx,
  194. [SDM845_MX_AO] = &mx_ao,
  195. };
  196. static const struct rpmhpd_desc sdm845_desc = {
  197. .rpmhpds = sdm845_rpmhpds,
  198. .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
  199. };
  200. /* SDX55 RPMH powerdomains */
  201. static struct rpmhpd *sdx55_rpmhpds[] = {
  202. [SDX55_CX] = &cx_w_mx_parent,
  203. [SDX55_MSS] = &mss,
  204. [SDX55_MX] = &mx,
  205. };
  206. static const struct rpmhpd_desc sdx55_desc = {
  207. .rpmhpds = sdx55_rpmhpds,
  208. .num_pds = ARRAY_SIZE(sdx55_rpmhpds),
  209. };
  210. /* SDX65 RPMH powerdomains */
  211. static struct rpmhpd *sdx65_rpmhpds[] = {
  212. [SDX65_CX] = &cx_w_mx_parent,
  213. [SDX65_CX_AO] = &cx_ao_w_mx_parent,
  214. [SDX65_MSS] = &mss,
  215. [SDX65_MX] = &mx,
  216. [SDX65_MX_AO] = &mx_ao,
  217. [SDX65_MXC] = &mxc,
  218. };
  219. static const struct rpmhpd_desc sdx65_desc = {
  220. .rpmhpds = sdx65_rpmhpds,
  221. .num_pds = ARRAY_SIZE(sdx65_rpmhpds),
  222. };
  223. /* SM6350 RPMH powerdomains */
  224. static struct rpmhpd *sm6350_rpmhpds[] = {
  225. [SM6350_CX] = &cx_w_mx_parent,
  226. [SM6350_GFX] = &gfx,
  227. [SM6350_LCX] = &lcx,
  228. [SM6350_LMX] = &lmx,
  229. [SM6350_MSS] = &mss,
  230. [SM6350_MX] = &mx,
  231. };
  232. static const struct rpmhpd_desc sm6350_desc = {
  233. .rpmhpds = sm6350_rpmhpds,
  234. .num_pds = ARRAY_SIZE(sm6350_rpmhpds),
  235. };
  236. /* SM8150 RPMH powerdomains */
  237. static struct rpmhpd *sm8150_rpmhpds[] = {
  238. [SM8150_CX] = &cx_w_mx_parent,
  239. [SM8150_CX_AO] = &cx_ao_w_mx_parent,
  240. [SM8150_EBI] = &ebi,
  241. [SM8150_GFX] = &gfx,
  242. [SM8150_LCX] = &lcx,
  243. [SM8150_LMX] = &lmx,
  244. [SM8150_MMCX] = &mmcx,
  245. [SM8150_MMCX_AO] = &mmcx_ao,
  246. [SM8150_MSS] = &mss,
  247. [SM8150_MX] = &mx,
  248. [SM8150_MX_AO] = &mx_ao,
  249. };
  250. static const struct rpmhpd_desc sm8150_desc = {
  251. .rpmhpds = sm8150_rpmhpds,
  252. .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
  253. };
  254. /* SM8250 RPMH powerdomains */
  255. static struct rpmhpd *sm8250_rpmhpds[] = {
  256. [SM8250_CX] = &cx_w_mx_parent,
  257. [SM8250_CX_AO] = &cx_ao_w_mx_parent,
  258. [SM8250_EBI] = &ebi,
  259. [SM8250_GFX] = &gfx,
  260. [SM8250_LCX] = &lcx,
  261. [SM8250_LMX] = &lmx,
  262. [SM8250_MMCX] = &mmcx,
  263. [SM8250_MMCX_AO] = &mmcx_ao,
  264. [SM8250_MX] = &mx,
  265. [SM8250_MX_AO] = &mx_ao,
  266. };
  267. static const struct rpmhpd_desc sm8250_desc = {
  268. .rpmhpds = sm8250_rpmhpds,
  269. .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
  270. };
  271. /* SM8350 Power domains */
  272. static struct rpmhpd *sm8350_rpmhpds[] = {
  273. [SM8350_CX] = &cx_w_mx_parent,
  274. [SM8350_CX_AO] = &cx_ao_w_mx_parent,
  275. [SM8350_EBI] = &ebi,
  276. [SM8350_GFX] = &gfx,
  277. [SM8350_LCX] = &lcx,
  278. [SM8350_LMX] = &lmx,
  279. [SM8350_MMCX] = &mmcx,
  280. [SM8350_MMCX_AO] = &mmcx_ao,
  281. [SM8350_MSS] = &mss,
  282. [SM8350_MX] = &mx,
  283. [SM8350_MX_AO] = &mx_ao,
  284. [SM8350_MXC] = &mxc,
  285. [SM8350_MXC_AO] = &mxc_ao,
  286. };
  287. static const struct rpmhpd_desc sm8350_desc = {
  288. .rpmhpds = sm8350_rpmhpds,
  289. .num_pds = ARRAY_SIZE(sm8350_rpmhpds),
  290. };
  291. /* SM8450 RPMH powerdomains */
  292. static struct rpmhpd *sm8450_rpmhpds[] = {
  293. [SM8450_CX] = &cx,
  294. [SM8450_CX_AO] = &cx_ao,
  295. [SM8450_EBI] = &ebi,
  296. [SM8450_GFX] = &gfx,
  297. [SM8450_LCX] = &lcx,
  298. [SM8450_LMX] = &lmx,
  299. [SM8450_MMCX] = &mmcx_w_cx_parent,
  300. [SM8450_MMCX_AO] = &mmcx_ao_w_cx_parent,
  301. [SM8450_MSS] = &mss,
  302. [SM8450_MX] = &mx,
  303. [SM8450_MX_AO] = &mx_ao,
  304. [SM8450_MXC] = &mxc,
  305. [SM8450_MXC_AO] = &mxc_ao,
  306. };
  307. static const struct rpmhpd_desc sm8450_desc = {
  308. .rpmhpds = sm8450_rpmhpds,
  309. .num_pds = ARRAY_SIZE(sm8450_rpmhpds),
  310. };
  311. /* SC7180 RPMH powerdomains */
  312. static struct rpmhpd *sc7180_rpmhpds[] = {
  313. [SC7180_CX] = &cx_w_mx_parent,
  314. [SC7180_CX_AO] = &cx_ao_w_mx_parent,
  315. [SC7180_GFX] = &gfx,
  316. [SC7180_LCX] = &lcx,
  317. [SC7180_LMX] = &lmx,
  318. [SC7180_MSS] = &mss,
  319. [SC7180_MX] = &mx,
  320. [SC7180_MX_AO] = &mx_ao,
  321. };
  322. static const struct rpmhpd_desc sc7180_desc = {
  323. .rpmhpds = sc7180_rpmhpds,
  324. .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
  325. };
  326. /* SC7280 RPMH powerdomains */
  327. static struct rpmhpd *sc7280_rpmhpds[] = {
  328. [SC7280_CX] = &cx,
  329. [SC7280_CX_AO] = &cx_ao,
  330. [SC7280_EBI] = &ebi,
  331. [SC7280_GFX] = &gfx,
  332. [SC7280_LCX] = &lcx,
  333. [SC7280_LMX] = &lmx,
  334. [SC7280_MSS] = &mss,
  335. [SC7280_MX] = &mx,
  336. [SC7280_MX_AO] = &mx_ao,
  337. };
  338. static const struct rpmhpd_desc sc7280_desc = {
  339. .rpmhpds = sc7280_rpmhpds,
  340. .num_pds = ARRAY_SIZE(sc7280_rpmhpds),
  341. };
  342. /* SC8180x RPMH powerdomains */
  343. static struct rpmhpd *sc8180x_rpmhpds[] = {
  344. [SC8180X_CX] = &cx_w_mx_parent,
  345. [SC8180X_CX_AO] = &cx_ao_w_mx_parent,
  346. [SC8180X_EBI] = &ebi,
  347. [SC8180X_GFX] = &gfx,
  348. [SC8180X_LCX] = &lcx,
  349. [SC8180X_LMX] = &lmx,
  350. [SC8180X_MMCX] = &mmcx,
  351. [SC8180X_MMCX_AO] = &mmcx_ao,
  352. [SC8180X_MSS] = &mss,
  353. [SC8180X_MX] = &mx,
  354. [SC8180X_MX_AO] = &mx_ao,
  355. };
  356. static const struct rpmhpd_desc sc8180x_desc = {
  357. .rpmhpds = sc8180x_rpmhpds,
  358. .num_pds = ARRAY_SIZE(sc8180x_rpmhpds),
  359. };
  360. /* SC8280xp RPMH powerdomains */
  361. static struct rpmhpd *sc8280xp_rpmhpds[] = {
  362. [SC8280XP_CX] = &cx,
  363. [SC8280XP_CX_AO] = &cx_ao,
  364. [SC8280XP_EBI] = &ebi,
  365. [SC8280XP_GFX] = &gfx,
  366. [SC8280XP_LCX] = &lcx,
  367. [SC8280XP_LMX] = &lmx,
  368. [SC8280XP_MMCX] = &mmcx,
  369. [SC8280XP_MMCX_AO] = &mmcx_ao,
  370. [SC8280XP_MX] = &mx,
  371. [SC8280XP_MX_AO] = &mx_ao,
  372. [SC8280XP_NSP] = &nsp,
  373. [SC8280XP_QPHY] = &qphy,
  374. };
  375. static const struct rpmhpd_desc sc8280xp_desc = {
  376. .rpmhpds = sc8280xp_rpmhpds,
  377. .num_pds = ARRAY_SIZE(sc8280xp_rpmhpds),
  378. };
  379. static const struct of_device_id rpmhpd_match_table[] = {
  380. { .compatible = "qcom,sa8540p-rpmhpd", .data = &sa8540p_desc },
  381. { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
  382. { .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc },
  383. { .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc },
  384. { .compatible = "qcom,sc8280xp-rpmhpd", .data = &sc8280xp_desc },
  385. { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
  386. { .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc},
  387. { .compatible = "qcom,sdx65-rpmhpd", .data = &sdx65_desc},
  388. { .compatible = "qcom,sm6350-rpmhpd", .data = &sm6350_desc },
  389. { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
  390. { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
  391. { .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc },
  392. { .compatible = "qcom,sm8450-rpmhpd", .data = &sm8450_desc },
  393. { }
  394. };
  395. MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
  396. static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
  397. unsigned int corner, bool sync)
  398. {
  399. struct tcs_cmd cmd = {
  400. .addr = pd->addr,
  401. .data = corner,
  402. };
  403. /*
  404. * Wait for an ack only when we are increasing the
  405. * perf state of the power domain
  406. */
  407. if (sync)
  408. return rpmh_write(pd->dev, state, &cmd, 1);
  409. else
  410. return rpmh_write_async(pd->dev, state, &cmd, 1);
  411. }
  412. static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
  413. unsigned int *active, unsigned int *sleep)
  414. {
  415. *active = corner;
  416. if (pd->active_only)
  417. *sleep = 0;
  418. else
  419. *sleep = *active;
  420. }
  421. /*
  422. * This function is used to aggregate the votes across the active only
  423. * resources and its peers. The aggregated votes are sent to RPMh as
  424. * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
  425. * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
  426. * on system sleep).
  427. * We send ACTIVE_ONLY votes for resources without any peers. For others,
  428. * which have an active only peer, all 3 votes are sent.
  429. */
  430. static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
  431. {
  432. int ret;
  433. struct rpmhpd *peer = pd->peer;
  434. unsigned int active_corner, sleep_corner;
  435. unsigned int this_active_corner = 0, this_sleep_corner = 0;
  436. unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
  437. to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
  438. if (peer && peer->enabled)
  439. to_active_sleep(peer, peer->corner, &peer_active_corner,
  440. &peer_sleep_corner);
  441. active_corner = max(this_active_corner, peer_active_corner);
  442. ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
  443. active_corner > pd->active_corner);
  444. if (ret)
  445. return ret;
  446. pd->active_corner = active_corner;
  447. if (peer) {
  448. peer->active_corner = active_corner;
  449. ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
  450. active_corner, false);
  451. if (ret)
  452. return ret;
  453. sleep_corner = max(this_sleep_corner, peer_sleep_corner);
  454. return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
  455. false);
  456. }
  457. return ret;
  458. }
  459. static int rpmhpd_power_on(struct generic_pm_domain *domain)
  460. {
  461. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  462. unsigned int corner;
  463. int ret;
  464. mutex_lock(&rpmhpd_lock);
  465. corner = max(pd->corner, pd->enable_corner);
  466. ret = rpmhpd_aggregate_corner(pd, corner);
  467. if (!ret)
  468. pd->enabled = true;
  469. mutex_unlock(&rpmhpd_lock);
  470. return ret;
  471. }
  472. static int rpmhpd_power_off(struct generic_pm_domain *domain)
  473. {
  474. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  475. int ret;
  476. mutex_lock(&rpmhpd_lock);
  477. ret = rpmhpd_aggregate_corner(pd, 0);
  478. if (!ret)
  479. pd->enabled = false;
  480. mutex_unlock(&rpmhpd_lock);
  481. return ret;
  482. }
  483. static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
  484. unsigned int level)
  485. {
  486. struct rpmhpd *pd = domain_to_rpmhpd(domain);
  487. int ret = 0, i;
  488. mutex_lock(&rpmhpd_lock);
  489. for (i = 0; i < pd->level_count; i++)
  490. if (level <= pd->level[i])
  491. break;
  492. /*
  493. * If the level requested is more than that supported by the
  494. * max corner, just set it to max anyway.
  495. */
  496. if (i == pd->level_count)
  497. i--;
  498. if (pd->enabled) {
  499. /* Ensure that the domain isn't turn off */
  500. if (i < pd->enable_corner)
  501. i = pd->enable_corner;
  502. ret = rpmhpd_aggregate_corner(pd, i);
  503. if (ret)
  504. goto out;
  505. }
  506. pd->corner = i;
  507. out:
  508. mutex_unlock(&rpmhpd_lock);
  509. return ret;
  510. }
  511. static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
  512. struct dev_pm_opp *opp)
  513. {
  514. return dev_pm_opp_get_level(opp);
  515. }
  516. static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
  517. {
  518. int i;
  519. const u16 *buf;
  520. buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
  521. if (IS_ERR(buf))
  522. return PTR_ERR(buf);
  523. /* 2 bytes used for each command DB aux data entry */
  524. rpmhpd->level_count >>= 1;
  525. if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
  526. return -EINVAL;
  527. for (i = 0; i < rpmhpd->level_count; i++) {
  528. rpmhpd->level[i] = buf[i];
  529. /* Remember the first corner with non-zero level */
  530. if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
  531. rpmhpd->enable_corner = i;
  532. /*
  533. * The AUX data may be zero padded. These 0 valued entries at
  534. * the end of the map must be ignored.
  535. */
  536. if (i > 0 && rpmhpd->level[i] == 0) {
  537. rpmhpd->level_count = i;
  538. break;
  539. }
  540. pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
  541. rpmhpd->level[i]);
  542. }
  543. return 0;
  544. }
  545. static int rpmhpd_probe(struct platform_device *pdev)
  546. {
  547. int i, ret;
  548. size_t num_pds;
  549. struct device *dev = &pdev->dev;
  550. struct genpd_onecell_data *data;
  551. struct rpmhpd **rpmhpds;
  552. const struct rpmhpd_desc *desc;
  553. desc = of_device_get_match_data(dev);
  554. if (!desc)
  555. return -EINVAL;
  556. rpmhpds = desc->rpmhpds;
  557. num_pds = desc->num_pds;
  558. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  559. if (!data)
  560. return -ENOMEM;
  561. data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
  562. GFP_KERNEL);
  563. if (!data->domains)
  564. return -ENOMEM;
  565. data->num_domains = num_pds;
  566. for (i = 0; i < num_pds; i++) {
  567. if (!rpmhpds[i])
  568. continue;
  569. rpmhpds[i]->dev = dev;
  570. rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
  571. if (!rpmhpds[i]->addr) {
  572. dev_err(dev, "Could not find RPMh address for resource %s\n",
  573. rpmhpds[i]->res_name);
  574. return -ENODEV;
  575. }
  576. ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
  577. if (ret != CMD_DB_HW_ARC) {
  578. dev_err(dev, "RPMh slave ID mismatch\n");
  579. return -EINVAL;
  580. }
  581. ret = rpmhpd_update_level_mapping(rpmhpds[i]);
  582. if (ret)
  583. return ret;
  584. rpmhpds[i]->pd.power_off = rpmhpd_power_off;
  585. rpmhpds[i]->pd.power_on = rpmhpd_power_on;
  586. rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
  587. rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
  588. pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
  589. data->domains[i] = &rpmhpds[i]->pd;
  590. }
  591. /* Add subdomains */
  592. for (i = 0; i < num_pds; i++) {
  593. if (!rpmhpds[i])
  594. continue;
  595. if (rpmhpds[i]->parent)
  596. pm_genpd_add_subdomain(rpmhpds[i]->parent,
  597. &rpmhpds[i]->pd);
  598. }
  599. return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
  600. }
  601. static struct platform_driver rpmhpd_driver = {
  602. .driver = {
  603. .name = "qcom-rpmhpd",
  604. .of_match_table = rpmhpd_match_table,
  605. .suppress_bind_attrs = true,
  606. },
  607. .probe = rpmhpd_probe,
  608. };
  609. static int __init rpmhpd_init(void)
  610. {
  611. return platform_driver_register(&rpmhpd_driver);
  612. }
  613. core_initcall(rpmhpd_init);
  614. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
  615. MODULE_LICENSE("GPL v2");