pm_domains.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Rockchip Generic power domain support.
  4. *
  5. * Copyright (c) 2015 ROCKCHIP, Co. Ltd.
  6. */
  7. #include <linux/io.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/err.h>
  10. #include <linux/pm_clock.h>
  11. #include <linux/pm_domain.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_clk.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/clk.h>
  16. #include <linux/regmap.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <dt-bindings/power/px30-power.h>
  19. #include <dt-bindings/power/rk3036-power.h>
  20. #include <dt-bindings/power/rk3066-power.h>
  21. #include <dt-bindings/power/rk3128-power.h>
  22. #include <dt-bindings/power/rk3188-power.h>
  23. #include <dt-bindings/power/rk3228-power.h>
  24. #include <dt-bindings/power/rk3288-power.h>
  25. #include <dt-bindings/power/rk3328-power.h>
  26. #include <dt-bindings/power/rk3366-power.h>
  27. #include <dt-bindings/power/rk3368-power.h>
  28. #include <dt-bindings/power/rk3399-power.h>
  29. struct rockchip_domain_info {
  30. int pwr_mask;
  31. int status_mask;
  32. int req_mask;
  33. int idle_mask;
  34. int ack_mask;
  35. bool active_wakeup;
  36. int pwr_w_mask;
  37. int req_w_mask;
  38. };
  39. struct rockchip_pmu_info {
  40. u32 pwr_offset;
  41. u32 status_offset;
  42. u32 req_offset;
  43. u32 idle_offset;
  44. u32 ack_offset;
  45. u32 core_pwrcnt_offset;
  46. u32 gpu_pwrcnt_offset;
  47. unsigned int core_power_transition_time;
  48. unsigned int gpu_power_transition_time;
  49. int num_domains;
  50. const struct rockchip_domain_info *domain_info;
  51. };
  52. #define MAX_QOS_REGS_NUM 5
  53. #define QOS_PRIORITY 0x08
  54. #define QOS_MODE 0x0c
  55. #define QOS_BANDWIDTH 0x10
  56. #define QOS_SATURATION 0x14
  57. #define QOS_EXTCONTROL 0x18
  58. struct rockchip_pm_domain {
  59. struct generic_pm_domain genpd;
  60. const struct rockchip_domain_info *info;
  61. struct rockchip_pmu *pmu;
  62. int num_qos;
  63. struct regmap **qos_regmap;
  64. u32 *qos_save_regs[MAX_QOS_REGS_NUM];
  65. int num_clks;
  66. struct clk_bulk_data *clks;
  67. };
  68. struct rockchip_pmu {
  69. struct device *dev;
  70. struct regmap *regmap;
  71. const struct rockchip_pmu_info *info;
  72. struct mutex mutex; /* mutex lock for pmu */
  73. struct genpd_onecell_data genpd_data;
  74. struct generic_pm_domain *domains[];
  75. };
  76. #define to_rockchip_pd(gpd) container_of(gpd, struct rockchip_pm_domain, genpd)
  77. #define DOMAIN(pwr, status, req, idle, ack, wakeup) \
  78. { \
  79. .pwr_mask = (pwr), \
  80. .status_mask = (status), \
  81. .req_mask = (req), \
  82. .idle_mask = (idle), \
  83. .ack_mask = (ack), \
  84. .active_wakeup = (wakeup), \
  85. }
  86. #define DOMAIN_M(pwr, status, req, idle, ack, wakeup) \
  87. { \
  88. .pwr_w_mask = (pwr) << 16, \
  89. .pwr_mask = (pwr), \
  90. .status_mask = (status), \
  91. .req_w_mask = (req) << 16, \
  92. .req_mask = (req), \
  93. .idle_mask = (idle), \
  94. .ack_mask = (ack), \
  95. .active_wakeup = wakeup, \
  96. }
  97. #define DOMAIN_RK3036(req, ack, idle, wakeup) \
  98. { \
  99. .req_mask = (req), \
  100. .req_w_mask = (req) << 16, \
  101. .ack_mask = (ack), \
  102. .idle_mask = (idle), \
  103. .active_wakeup = wakeup, \
  104. }
  105. #define DOMAIN_PX30(pwr, status, req, wakeup) \
  106. DOMAIN_M(pwr, status, req, (req) << 16, req, wakeup)
  107. #define DOMAIN_RK3288(pwr, status, req, wakeup) \
  108. DOMAIN(pwr, status, req, req, (req) << 16, wakeup)
  109. #define DOMAIN_RK3328(pwr, status, req, wakeup) \
  110. DOMAIN_M(pwr, pwr, req, (req) << 10, req, wakeup)
  111. #define DOMAIN_RK3368(pwr, status, req, wakeup) \
  112. DOMAIN(pwr, status, req, (req) << 16, req, wakeup)
  113. #define DOMAIN_RK3399(pwr, status, req, wakeup) \
  114. DOMAIN(pwr, status, req, req, req, wakeup)
  115. static bool rockchip_pmu_domain_is_idle(struct rockchip_pm_domain *pd)
  116. {
  117. struct rockchip_pmu *pmu = pd->pmu;
  118. const struct rockchip_domain_info *pd_info = pd->info;
  119. unsigned int val;
  120. regmap_read(pmu->regmap, pmu->info->idle_offset, &val);
  121. return (val & pd_info->idle_mask) == pd_info->idle_mask;
  122. }
  123. static unsigned int rockchip_pmu_read_ack(struct rockchip_pmu *pmu)
  124. {
  125. unsigned int val;
  126. regmap_read(pmu->regmap, pmu->info->ack_offset, &val);
  127. return val;
  128. }
  129. static int rockchip_pmu_set_idle_request(struct rockchip_pm_domain *pd,
  130. bool idle)
  131. {
  132. const struct rockchip_domain_info *pd_info = pd->info;
  133. struct generic_pm_domain *genpd = &pd->genpd;
  134. struct rockchip_pmu *pmu = pd->pmu;
  135. unsigned int target_ack;
  136. unsigned int val;
  137. bool is_idle;
  138. int ret;
  139. if (pd_info->req_mask == 0)
  140. return 0;
  141. else if (pd_info->req_w_mask)
  142. regmap_write(pmu->regmap, pmu->info->req_offset,
  143. idle ? (pd_info->req_mask | pd_info->req_w_mask) :
  144. pd_info->req_w_mask);
  145. else
  146. regmap_update_bits(pmu->regmap, pmu->info->req_offset,
  147. pd_info->req_mask, idle ? -1U : 0);
  148. dsb(sy);
  149. /* Wait util idle_ack = 1 */
  150. target_ack = idle ? pd_info->ack_mask : 0;
  151. ret = readx_poll_timeout_atomic(rockchip_pmu_read_ack, pmu, val,
  152. (val & pd_info->ack_mask) == target_ack,
  153. 0, 10000);
  154. if (ret) {
  155. dev_err(pmu->dev,
  156. "failed to get ack on domain '%s', val=0x%x\n",
  157. genpd->name, val);
  158. return ret;
  159. }
  160. ret = readx_poll_timeout_atomic(rockchip_pmu_domain_is_idle, pd,
  161. is_idle, is_idle == idle, 0, 10000);
  162. if (ret) {
  163. dev_err(pmu->dev,
  164. "failed to set idle on domain '%s', val=%d\n",
  165. genpd->name, is_idle);
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static int rockchip_pmu_save_qos(struct rockchip_pm_domain *pd)
  171. {
  172. int i;
  173. for (i = 0; i < pd->num_qos; i++) {
  174. regmap_read(pd->qos_regmap[i],
  175. QOS_PRIORITY,
  176. &pd->qos_save_regs[0][i]);
  177. regmap_read(pd->qos_regmap[i],
  178. QOS_MODE,
  179. &pd->qos_save_regs[1][i]);
  180. regmap_read(pd->qos_regmap[i],
  181. QOS_BANDWIDTH,
  182. &pd->qos_save_regs[2][i]);
  183. regmap_read(pd->qos_regmap[i],
  184. QOS_SATURATION,
  185. &pd->qos_save_regs[3][i]);
  186. regmap_read(pd->qos_regmap[i],
  187. QOS_EXTCONTROL,
  188. &pd->qos_save_regs[4][i]);
  189. }
  190. return 0;
  191. }
  192. static int rockchip_pmu_restore_qos(struct rockchip_pm_domain *pd)
  193. {
  194. int i;
  195. for (i = 0; i < pd->num_qos; i++) {
  196. regmap_write(pd->qos_regmap[i],
  197. QOS_PRIORITY,
  198. pd->qos_save_regs[0][i]);
  199. regmap_write(pd->qos_regmap[i],
  200. QOS_MODE,
  201. pd->qos_save_regs[1][i]);
  202. regmap_write(pd->qos_regmap[i],
  203. QOS_BANDWIDTH,
  204. pd->qos_save_regs[2][i]);
  205. regmap_write(pd->qos_regmap[i],
  206. QOS_SATURATION,
  207. pd->qos_save_regs[3][i]);
  208. regmap_write(pd->qos_regmap[i],
  209. QOS_EXTCONTROL,
  210. pd->qos_save_regs[4][i]);
  211. }
  212. return 0;
  213. }
  214. static bool rockchip_pmu_domain_is_on(struct rockchip_pm_domain *pd)
  215. {
  216. struct rockchip_pmu *pmu = pd->pmu;
  217. unsigned int val;
  218. /* check idle status for idle-only domains */
  219. if (pd->info->status_mask == 0)
  220. return !rockchip_pmu_domain_is_idle(pd);
  221. regmap_read(pmu->regmap, pmu->info->status_offset, &val);
  222. /* 1'b0: power on, 1'b1: power off */
  223. return !(val & pd->info->status_mask);
  224. }
  225. static void rockchip_do_pmu_set_power_domain(struct rockchip_pm_domain *pd,
  226. bool on)
  227. {
  228. struct rockchip_pmu *pmu = pd->pmu;
  229. struct generic_pm_domain *genpd = &pd->genpd;
  230. bool is_on;
  231. if (pd->info->pwr_mask == 0)
  232. return;
  233. else if (pd->info->pwr_w_mask)
  234. regmap_write(pmu->regmap, pmu->info->pwr_offset,
  235. on ? pd->info->pwr_w_mask :
  236. (pd->info->pwr_mask | pd->info->pwr_w_mask));
  237. else
  238. regmap_update_bits(pmu->regmap, pmu->info->pwr_offset,
  239. pd->info->pwr_mask, on ? 0 : -1U);
  240. dsb(sy);
  241. if (readx_poll_timeout_atomic(rockchip_pmu_domain_is_on, pd, is_on,
  242. is_on == on, 0, 10000)) {
  243. dev_err(pmu->dev,
  244. "failed to set domain '%s', val=%d\n",
  245. genpd->name, is_on);
  246. return;
  247. }
  248. }
  249. static int rockchip_pd_power(struct rockchip_pm_domain *pd, bool power_on)
  250. {
  251. struct rockchip_pmu *pmu = pd->pmu;
  252. int ret;
  253. mutex_lock(&pmu->mutex);
  254. if (rockchip_pmu_domain_is_on(pd) != power_on) {
  255. ret = clk_bulk_enable(pd->num_clks, pd->clks);
  256. if (ret < 0) {
  257. dev_err(pmu->dev, "failed to enable clocks\n");
  258. mutex_unlock(&pmu->mutex);
  259. return ret;
  260. }
  261. if (!power_on) {
  262. rockchip_pmu_save_qos(pd);
  263. /* if powering down, idle request to NIU first */
  264. rockchip_pmu_set_idle_request(pd, true);
  265. }
  266. rockchip_do_pmu_set_power_domain(pd, power_on);
  267. if (power_on) {
  268. /* if powering up, leave idle mode */
  269. rockchip_pmu_set_idle_request(pd, false);
  270. rockchip_pmu_restore_qos(pd);
  271. }
  272. clk_bulk_disable(pd->num_clks, pd->clks);
  273. }
  274. mutex_unlock(&pmu->mutex);
  275. return 0;
  276. }
  277. static int rockchip_pd_power_on(struct generic_pm_domain *domain)
  278. {
  279. struct rockchip_pm_domain *pd = to_rockchip_pd(domain);
  280. return rockchip_pd_power(pd, true);
  281. }
  282. static int rockchip_pd_power_off(struct generic_pm_domain *domain)
  283. {
  284. struct rockchip_pm_domain *pd = to_rockchip_pd(domain);
  285. return rockchip_pd_power(pd, false);
  286. }
  287. static int rockchip_pd_attach_dev(struct generic_pm_domain *genpd,
  288. struct device *dev)
  289. {
  290. struct clk *clk;
  291. int i;
  292. int error;
  293. dev_dbg(dev, "attaching to power domain '%s'\n", genpd->name);
  294. error = pm_clk_create(dev);
  295. if (error) {
  296. dev_err(dev, "pm_clk_create failed %d\n", error);
  297. return error;
  298. }
  299. i = 0;
  300. while ((clk = of_clk_get(dev->of_node, i++)) && !IS_ERR(clk)) {
  301. dev_dbg(dev, "adding clock '%pC' to list of PM clocks\n", clk);
  302. error = pm_clk_add_clk(dev, clk);
  303. if (error) {
  304. dev_err(dev, "pm_clk_add_clk failed %d\n", error);
  305. clk_put(clk);
  306. pm_clk_destroy(dev);
  307. return error;
  308. }
  309. }
  310. return 0;
  311. }
  312. static void rockchip_pd_detach_dev(struct generic_pm_domain *genpd,
  313. struct device *dev)
  314. {
  315. dev_dbg(dev, "detaching from power domain '%s'\n", genpd->name);
  316. pm_clk_destroy(dev);
  317. }
  318. static int rockchip_pm_add_one_domain(struct rockchip_pmu *pmu,
  319. struct device_node *node)
  320. {
  321. const struct rockchip_domain_info *pd_info;
  322. struct rockchip_pm_domain *pd;
  323. struct device_node *qos_node;
  324. int i, j;
  325. u32 id;
  326. int error;
  327. error = of_property_read_u32(node, "reg", &id);
  328. if (error) {
  329. dev_err(pmu->dev,
  330. "%pOFn: failed to retrieve domain id (reg): %d\n",
  331. node, error);
  332. return -EINVAL;
  333. }
  334. if (id >= pmu->info->num_domains) {
  335. dev_err(pmu->dev, "%pOFn: invalid domain id %d\n",
  336. node, id);
  337. return -EINVAL;
  338. }
  339. pd_info = &pmu->info->domain_info[id];
  340. if (!pd_info) {
  341. dev_err(pmu->dev, "%pOFn: undefined domain id %d\n",
  342. node, id);
  343. return -EINVAL;
  344. }
  345. pd = devm_kzalloc(pmu->dev, sizeof(*pd), GFP_KERNEL);
  346. if (!pd)
  347. return -ENOMEM;
  348. pd->info = pd_info;
  349. pd->pmu = pmu;
  350. pd->num_clks = of_clk_get_parent_count(node);
  351. if (pd->num_clks > 0) {
  352. pd->clks = devm_kcalloc(pmu->dev, pd->num_clks,
  353. sizeof(*pd->clks), GFP_KERNEL);
  354. if (!pd->clks)
  355. return -ENOMEM;
  356. } else {
  357. dev_dbg(pmu->dev, "%pOFn: doesn't have clocks: %d\n",
  358. node, pd->num_clks);
  359. pd->num_clks = 0;
  360. }
  361. for (i = 0; i < pd->num_clks; i++) {
  362. pd->clks[i].clk = of_clk_get(node, i);
  363. if (IS_ERR(pd->clks[i].clk)) {
  364. error = PTR_ERR(pd->clks[i].clk);
  365. dev_err(pmu->dev,
  366. "%pOFn: failed to get clk at index %d: %d\n",
  367. node, i, error);
  368. return error;
  369. }
  370. }
  371. error = clk_bulk_prepare(pd->num_clks, pd->clks);
  372. if (error)
  373. goto err_put_clocks;
  374. pd->num_qos = of_count_phandle_with_args(node, "pm_qos",
  375. NULL);
  376. if (pd->num_qos > 0) {
  377. pd->qos_regmap = devm_kcalloc(pmu->dev, pd->num_qos,
  378. sizeof(*pd->qos_regmap),
  379. GFP_KERNEL);
  380. if (!pd->qos_regmap) {
  381. error = -ENOMEM;
  382. goto err_unprepare_clocks;
  383. }
  384. for (j = 0; j < MAX_QOS_REGS_NUM; j++) {
  385. pd->qos_save_regs[j] = devm_kcalloc(pmu->dev,
  386. pd->num_qos,
  387. sizeof(u32),
  388. GFP_KERNEL);
  389. if (!pd->qos_save_regs[j]) {
  390. error = -ENOMEM;
  391. goto err_unprepare_clocks;
  392. }
  393. }
  394. for (j = 0; j < pd->num_qos; j++) {
  395. qos_node = of_parse_phandle(node, "pm_qos", j);
  396. if (!qos_node) {
  397. error = -ENODEV;
  398. goto err_unprepare_clocks;
  399. }
  400. pd->qos_regmap[j] = syscon_node_to_regmap(qos_node);
  401. if (IS_ERR(pd->qos_regmap[j])) {
  402. error = -ENODEV;
  403. of_node_put(qos_node);
  404. goto err_unprepare_clocks;
  405. }
  406. of_node_put(qos_node);
  407. }
  408. }
  409. error = rockchip_pd_power(pd, true);
  410. if (error) {
  411. dev_err(pmu->dev,
  412. "failed to power on domain '%pOFn': %d\n",
  413. node, error);
  414. goto err_unprepare_clocks;
  415. }
  416. pd->genpd.name = node->name;
  417. pd->genpd.power_off = rockchip_pd_power_off;
  418. pd->genpd.power_on = rockchip_pd_power_on;
  419. pd->genpd.attach_dev = rockchip_pd_attach_dev;
  420. pd->genpd.detach_dev = rockchip_pd_detach_dev;
  421. pd->genpd.flags = GENPD_FLAG_PM_CLK;
  422. if (pd_info->active_wakeup)
  423. pd->genpd.flags |= GENPD_FLAG_ACTIVE_WAKEUP;
  424. pm_genpd_init(&pd->genpd, NULL, false);
  425. pmu->genpd_data.domains[id] = &pd->genpd;
  426. return 0;
  427. err_unprepare_clocks:
  428. clk_bulk_unprepare(pd->num_clks, pd->clks);
  429. err_put_clocks:
  430. clk_bulk_put(pd->num_clks, pd->clks);
  431. return error;
  432. }
  433. static void rockchip_pm_remove_one_domain(struct rockchip_pm_domain *pd)
  434. {
  435. int ret;
  436. /*
  437. * We're in the error cleanup already, so we only complain,
  438. * but won't emit another error on top of the original one.
  439. */
  440. ret = pm_genpd_remove(&pd->genpd);
  441. if (ret < 0)
  442. dev_err(pd->pmu->dev, "failed to remove domain '%s' : %d - state may be inconsistent\n",
  443. pd->genpd.name, ret);
  444. clk_bulk_unprepare(pd->num_clks, pd->clks);
  445. clk_bulk_put(pd->num_clks, pd->clks);
  446. /* protect the zeroing of pm->num_clks */
  447. mutex_lock(&pd->pmu->mutex);
  448. pd->num_clks = 0;
  449. mutex_unlock(&pd->pmu->mutex);
  450. /* devm will free our memory */
  451. }
  452. static void rockchip_pm_domain_cleanup(struct rockchip_pmu *pmu)
  453. {
  454. struct generic_pm_domain *genpd;
  455. struct rockchip_pm_domain *pd;
  456. int i;
  457. for (i = 0; i < pmu->genpd_data.num_domains; i++) {
  458. genpd = pmu->genpd_data.domains[i];
  459. if (genpd) {
  460. pd = to_rockchip_pd(genpd);
  461. rockchip_pm_remove_one_domain(pd);
  462. }
  463. }
  464. /* devm will free our memory */
  465. }
  466. static void rockchip_configure_pd_cnt(struct rockchip_pmu *pmu,
  467. u32 domain_reg_offset,
  468. unsigned int count)
  469. {
  470. /* First configure domain power down transition count ... */
  471. regmap_write(pmu->regmap, domain_reg_offset, count);
  472. /* ... and then power up count. */
  473. regmap_write(pmu->regmap, domain_reg_offset + 4, count);
  474. }
  475. static int rockchip_pm_add_subdomain(struct rockchip_pmu *pmu,
  476. struct device_node *parent)
  477. {
  478. struct device_node *np;
  479. struct generic_pm_domain *child_domain, *parent_domain;
  480. int error;
  481. for_each_child_of_node(parent, np) {
  482. u32 idx;
  483. error = of_property_read_u32(parent, "reg", &idx);
  484. if (error) {
  485. dev_err(pmu->dev,
  486. "%pOFn: failed to retrieve domain id (reg): %d\n",
  487. parent, error);
  488. goto err_out;
  489. }
  490. parent_domain = pmu->genpd_data.domains[idx];
  491. error = rockchip_pm_add_one_domain(pmu, np);
  492. if (error) {
  493. dev_err(pmu->dev, "failed to handle node %pOFn: %d\n",
  494. np, error);
  495. goto err_out;
  496. }
  497. error = of_property_read_u32(np, "reg", &idx);
  498. if (error) {
  499. dev_err(pmu->dev,
  500. "%pOFn: failed to retrieve domain id (reg): %d\n",
  501. np, error);
  502. goto err_out;
  503. }
  504. child_domain = pmu->genpd_data.domains[idx];
  505. error = pm_genpd_add_subdomain(parent_domain, child_domain);
  506. if (error) {
  507. dev_err(pmu->dev, "%s failed to add subdomain %s: %d\n",
  508. parent_domain->name, child_domain->name, error);
  509. goto err_out;
  510. } else {
  511. dev_dbg(pmu->dev, "%s add subdomain: %s\n",
  512. parent_domain->name, child_domain->name);
  513. }
  514. rockchip_pm_add_subdomain(pmu, np);
  515. }
  516. return 0;
  517. err_out:
  518. of_node_put(np);
  519. return error;
  520. }
  521. static int rockchip_pm_domain_probe(struct platform_device *pdev)
  522. {
  523. struct device *dev = &pdev->dev;
  524. struct device_node *np = dev->of_node;
  525. struct device_node *node;
  526. struct device *parent;
  527. struct rockchip_pmu *pmu;
  528. const struct of_device_id *match;
  529. const struct rockchip_pmu_info *pmu_info;
  530. int error;
  531. if (!np) {
  532. dev_err(dev, "device tree node not found\n");
  533. return -ENODEV;
  534. }
  535. match = of_match_device(dev->driver->of_match_table, dev);
  536. if (!match || !match->data) {
  537. dev_err(dev, "missing pmu data\n");
  538. return -EINVAL;
  539. }
  540. pmu_info = match->data;
  541. pmu = devm_kzalloc(dev,
  542. struct_size(pmu, domains, pmu_info->num_domains),
  543. GFP_KERNEL);
  544. if (!pmu)
  545. return -ENOMEM;
  546. pmu->dev = &pdev->dev;
  547. mutex_init(&pmu->mutex);
  548. pmu->info = pmu_info;
  549. pmu->genpd_data.domains = pmu->domains;
  550. pmu->genpd_data.num_domains = pmu_info->num_domains;
  551. parent = dev->parent;
  552. if (!parent) {
  553. dev_err(dev, "no parent for syscon devices\n");
  554. return -ENODEV;
  555. }
  556. pmu->regmap = syscon_node_to_regmap(parent->of_node);
  557. if (IS_ERR(pmu->regmap)) {
  558. dev_err(dev, "no regmap available\n");
  559. return PTR_ERR(pmu->regmap);
  560. }
  561. /*
  562. * Configure power up and down transition delays for CORE
  563. * and GPU domains.
  564. */
  565. if (pmu_info->core_power_transition_time)
  566. rockchip_configure_pd_cnt(pmu, pmu_info->core_pwrcnt_offset,
  567. pmu_info->core_power_transition_time);
  568. if (pmu_info->gpu_pwrcnt_offset)
  569. rockchip_configure_pd_cnt(pmu, pmu_info->gpu_pwrcnt_offset,
  570. pmu_info->gpu_power_transition_time);
  571. error = -ENODEV;
  572. for_each_available_child_of_node(np, node) {
  573. error = rockchip_pm_add_one_domain(pmu, node);
  574. if (error) {
  575. dev_err(dev, "failed to handle node %pOFn: %d\n",
  576. node, error);
  577. of_node_put(node);
  578. goto err_out;
  579. }
  580. error = rockchip_pm_add_subdomain(pmu, node);
  581. if (error < 0) {
  582. dev_err(dev, "failed to handle subdomain node %pOFn: %d\n",
  583. node, error);
  584. of_node_put(node);
  585. goto err_out;
  586. }
  587. }
  588. if (error) {
  589. dev_dbg(dev, "no power domains defined\n");
  590. goto err_out;
  591. }
  592. error = of_genpd_add_provider_onecell(np, &pmu->genpd_data);
  593. if (error) {
  594. dev_err(dev, "failed to add provider: %d\n", error);
  595. goto err_out;
  596. }
  597. return 0;
  598. err_out:
  599. rockchip_pm_domain_cleanup(pmu);
  600. return error;
  601. }
  602. static const struct rockchip_domain_info px30_pm_domains[] = {
  603. [PX30_PD_USB] = DOMAIN_PX30(BIT(5), BIT(5), BIT(10), false),
  604. [PX30_PD_SDCARD] = DOMAIN_PX30(BIT(8), BIT(8), BIT(9), false),
  605. [PX30_PD_GMAC] = DOMAIN_PX30(BIT(10), BIT(10), BIT(6), false),
  606. [PX30_PD_MMC_NAND] = DOMAIN_PX30(BIT(11), BIT(11), BIT(5), false),
  607. [PX30_PD_VPU] = DOMAIN_PX30(BIT(12), BIT(12), BIT(14), false),
  608. [PX30_PD_VO] = DOMAIN_PX30(BIT(13), BIT(13), BIT(7), false),
  609. [PX30_PD_VI] = DOMAIN_PX30(BIT(14), BIT(14), BIT(8), false),
  610. [PX30_PD_GPU] = DOMAIN_PX30(BIT(15), BIT(15), BIT(2), false),
  611. };
  612. static const struct rockchip_domain_info rk3036_pm_domains[] = {
  613. [RK3036_PD_MSCH] = DOMAIN_RK3036(BIT(14), BIT(23), BIT(30), true),
  614. [RK3036_PD_CORE] = DOMAIN_RK3036(BIT(13), BIT(17), BIT(24), false),
  615. [RK3036_PD_PERI] = DOMAIN_RK3036(BIT(12), BIT(18), BIT(25), false),
  616. [RK3036_PD_VIO] = DOMAIN_RK3036(BIT(11), BIT(19), BIT(26), false),
  617. [RK3036_PD_VPU] = DOMAIN_RK3036(BIT(10), BIT(20), BIT(27), false),
  618. [RK3036_PD_GPU] = DOMAIN_RK3036(BIT(9), BIT(21), BIT(28), false),
  619. [RK3036_PD_SYS] = DOMAIN_RK3036(BIT(8), BIT(22), BIT(29), false),
  620. };
  621. static const struct rockchip_domain_info rk3066_pm_domains[] = {
  622. [RK3066_PD_GPU] = DOMAIN(BIT(9), BIT(9), BIT(3), BIT(24), BIT(29), false),
  623. [RK3066_PD_VIDEO] = DOMAIN(BIT(8), BIT(8), BIT(4), BIT(23), BIT(28), false),
  624. [RK3066_PD_VIO] = DOMAIN(BIT(7), BIT(7), BIT(5), BIT(22), BIT(27), false),
  625. [RK3066_PD_PERI] = DOMAIN(BIT(6), BIT(6), BIT(2), BIT(25), BIT(30), false),
  626. [RK3066_PD_CPU] = DOMAIN(0, BIT(5), BIT(1), BIT(26), BIT(31), false),
  627. };
  628. static const struct rockchip_domain_info rk3128_pm_domains[] = {
  629. [RK3128_PD_CORE] = DOMAIN_RK3288(BIT(0), BIT(0), BIT(4), false),
  630. [RK3128_PD_MSCH] = DOMAIN_RK3288(0, 0, BIT(6), true),
  631. [RK3128_PD_VIO] = DOMAIN_RK3288(BIT(3), BIT(3), BIT(2), false),
  632. [RK3128_PD_VIDEO] = DOMAIN_RK3288(BIT(2), BIT(2), BIT(1), false),
  633. [RK3128_PD_GPU] = DOMAIN_RK3288(BIT(1), BIT(1), BIT(3), false),
  634. };
  635. static const struct rockchip_domain_info rk3188_pm_domains[] = {
  636. [RK3188_PD_GPU] = DOMAIN(BIT(9), BIT(9), BIT(3), BIT(24), BIT(29), false),
  637. [RK3188_PD_VIDEO] = DOMAIN(BIT(8), BIT(8), BIT(4), BIT(23), BIT(28), false),
  638. [RK3188_PD_VIO] = DOMAIN(BIT(7), BIT(7), BIT(5), BIT(22), BIT(27), false),
  639. [RK3188_PD_PERI] = DOMAIN(BIT(6), BIT(6), BIT(2), BIT(25), BIT(30), false),
  640. [RK3188_PD_CPU] = DOMAIN(BIT(5), BIT(5), BIT(1), BIT(26), BIT(31), false),
  641. };
  642. static const struct rockchip_domain_info rk3228_pm_domains[] = {
  643. [RK3228_PD_CORE] = DOMAIN_RK3036(BIT(0), BIT(0), BIT(16), true),
  644. [RK3228_PD_MSCH] = DOMAIN_RK3036(BIT(1), BIT(1), BIT(17), true),
  645. [RK3228_PD_BUS] = DOMAIN_RK3036(BIT(2), BIT(2), BIT(18), true),
  646. [RK3228_PD_SYS] = DOMAIN_RK3036(BIT(3), BIT(3), BIT(19), true),
  647. [RK3228_PD_VIO] = DOMAIN_RK3036(BIT(4), BIT(4), BIT(20), false),
  648. [RK3228_PD_VOP] = DOMAIN_RK3036(BIT(5), BIT(5), BIT(21), false),
  649. [RK3228_PD_VPU] = DOMAIN_RK3036(BIT(6), BIT(6), BIT(22), false),
  650. [RK3228_PD_RKVDEC] = DOMAIN_RK3036(BIT(7), BIT(7), BIT(23), false),
  651. [RK3228_PD_GPU] = DOMAIN_RK3036(BIT(8), BIT(8), BIT(24), false),
  652. [RK3228_PD_PERI] = DOMAIN_RK3036(BIT(9), BIT(9), BIT(25), true),
  653. [RK3228_PD_GMAC] = DOMAIN_RK3036(BIT(10), BIT(10), BIT(26), false),
  654. };
  655. static const struct rockchip_domain_info rk3288_pm_domains[] = {
  656. [RK3288_PD_VIO] = DOMAIN_RK3288(BIT(7), BIT(7), BIT(4), false),
  657. [RK3288_PD_HEVC] = DOMAIN_RK3288(BIT(14), BIT(10), BIT(9), false),
  658. [RK3288_PD_VIDEO] = DOMAIN_RK3288(BIT(8), BIT(8), BIT(3), false),
  659. [RK3288_PD_GPU] = DOMAIN_RK3288(BIT(9), BIT(9), BIT(2), false),
  660. };
  661. static const struct rockchip_domain_info rk3328_pm_domains[] = {
  662. [RK3328_PD_CORE] = DOMAIN_RK3328(0, BIT(0), BIT(0), false),
  663. [RK3328_PD_GPU] = DOMAIN_RK3328(0, BIT(1), BIT(1), false),
  664. [RK3328_PD_BUS] = DOMAIN_RK3328(0, BIT(2), BIT(2), true),
  665. [RK3328_PD_MSCH] = DOMAIN_RK3328(0, BIT(3), BIT(3), true),
  666. [RK3328_PD_PERI] = DOMAIN_RK3328(0, BIT(4), BIT(4), true),
  667. [RK3328_PD_VIDEO] = DOMAIN_RK3328(0, BIT(5), BIT(5), false),
  668. [RK3328_PD_HEVC] = DOMAIN_RK3328(0, BIT(6), BIT(6), false),
  669. [RK3328_PD_VIO] = DOMAIN_RK3328(0, BIT(8), BIT(8), false),
  670. [RK3328_PD_VPU] = DOMAIN_RK3328(0, BIT(9), BIT(9), false),
  671. };
  672. static const struct rockchip_domain_info rk3366_pm_domains[] = {
  673. [RK3366_PD_PERI] = DOMAIN_RK3368(BIT(10), BIT(10), BIT(6), true),
  674. [RK3366_PD_VIO] = DOMAIN_RK3368(BIT(14), BIT(14), BIT(8), false),
  675. [RK3366_PD_VIDEO] = DOMAIN_RK3368(BIT(13), BIT(13), BIT(7), false),
  676. [RK3366_PD_RKVDEC] = DOMAIN_RK3368(BIT(11), BIT(11), BIT(7), false),
  677. [RK3366_PD_WIFIBT] = DOMAIN_RK3368(BIT(8), BIT(8), BIT(9), false),
  678. [RK3366_PD_VPU] = DOMAIN_RK3368(BIT(12), BIT(12), BIT(7), false),
  679. [RK3366_PD_GPU] = DOMAIN_RK3368(BIT(15), BIT(15), BIT(2), false),
  680. };
  681. static const struct rockchip_domain_info rk3368_pm_domains[] = {
  682. [RK3368_PD_PERI] = DOMAIN_RK3368(BIT(13), BIT(12), BIT(6), true),
  683. [RK3368_PD_VIO] = DOMAIN_RK3368(BIT(15), BIT(14), BIT(8), false),
  684. [RK3368_PD_VIDEO] = DOMAIN_RK3368(BIT(14), BIT(13), BIT(7), false),
  685. [RK3368_PD_GPU_0] = DOMAIN_RK3368(BIT(16), BIT(15), BIT(2), false),
  686. [RK3368_PD_GPU_1] = DOMAIN_RK3368(BIT(17), BIT(16), BIT(2), false),
  687. };
  688. static const struct rockchip_domain_info rk3399_pm_domains[] = {
  689. [RK3399_PD_TCPD0] = DOMAIN_RK3399(BIT(8), BIT(8), 0, false),
  690. [RK3399_PD_TCPD1] = DOMAIN_RK3399(BIT(9), BIT(9), 0, false),
  691. [RK3399_PD_CCI] = DOMAIN_RK3399(BIT(10), BIT(10), 0, true),
  692. [RK3399_PD_CCI0] = DOMAIN_RK3399(0, 0, BIT(15), true),
  693. [RK3399_PD_CCI1] = DOMAIN_RK3399(0, 0, BIT(16), true),
  694. [RK3399_PD_PERILP] = DOMAIN_RK3399(BIT(11), BIT(11), BIT(1), true),
  695. [RK3399_PD_PERIHP] = DOMAIN_RK3399(BIT(12), BIT(12), BIT(2), true),
  696. [RK3399_PD_CENTER] = DOMAIN_RK3399(BIT(13), BIT(13), BIT(14), true),
  697. [RK3399_PD_VIO] = DOMAIN_RK3399(BIT(14), BIT(14), BIT(17), false),
  698. [RK3399_PD_GPU] = DOMAIN_RK3399(BIT(15), BIT(15), BIT(0), false),
  699. [RK3399_PD_VCODEC] = DOMAIN_RK3399(BIT(16), BIT(16), BIT(3), false),
  700. [RK3399_PD_VDU] = DOMAIN_RK3399(BIT(17), BIT(17), BIT(4), false),
  701. [RK3399_PD_RGA] = DOMAIN_RK3399(BIT(18), BIT(18), BIT(5), false),
  702. [RK3399_PD_IEP] = DOMAIN_RK3399(BIT(19), BIT(19), BIT(6), false),
  703. [RK3399_PD_VO] = DOMAIN_RK3399(BIT(20), BIT(20), 0, false),
  704. [RK3399_PD_VOPB] = DOMAIN_RK3399(0, 0, BIT(7), false),
  705. [RK3399_PD_VOPL] = DOMAIN_RK3399(0, 0, BIT(8), false),
  706. [RK3399_PD_ISP0] = DOMAIN_RK3399(BIT(22), BIT(22), BIT(9), false),
  707. [RK3399_PD_ISP1] = DOMAIN_RK3399(BIT(23), BIT(23), BIT(10), false),
  708. [RK3399_PD_HDCP] = DOMAIN_RK3399(BIT(24), BIT(24), BIT(11), false),
  709. [RK3399_PD_GMAC] = DOMAIN_RK3399(BIT(25), BIT(25), BIT(23), true),
  710. [RK3399_PD_EMMC] = DOMAIN_RK3399(BIT(26), BIT(26), BIT(24), true),
  711. [RK3399_PD_USB3] = DOMAIN_RK3399(BIT(27), BIT(27), BIT(12), true),
  712. [RK3399_PD_EDP] = DOMAIN_RK3399(BIT(28), BIT(28), BIT(22), false),
  713. [RK3399_PD_GIC] = DOMAIN_RK3399(BIT(29), BIT(29), BIT(27), true),
  714. [RK3399_PD_SD] = DOMAIN_RK3399(BIT(30), BIT(30), BIT(28), true),
  715. [RK3399_PD_SDIOAUDIO] = DOMAIN_RK3399(BIT(31), BIT(31), BIT(29), true),
  716. };
  717. static const struct rockchip_pmu_info px30_pmu = {
  718. .pwr_offset = 0x18,
  719. .status_offset = 0x20,
  720. .req_offset = 0x64,
  721. .idle_offset = 0x6c,
  722. .ack_offset = 0x6c,
  723. .num_domains = ARRAY_SIZE(px30_pm_domains),
  724. .domain_info = px30_pm_domains,
  725. };
  726. static const struct rockchip_pmu_info rk3036_pmu = {
  727. .req_offset = 0x148,
  728. .idle_offset = 0x14c,
  729. .ack_offset = 0x14c,
  730. .num_domains = ARRAY_SIZE(rk3036_pm_domains),
  731. .domain_info = rk3036_pm_domains,
  732. };
  733. static const struct rockchip_pmu_info rk3066_pmu = {
  734. .pwr_offset = 0x08,
  735. .status_offset = 0x0c,
  736. .req_offset = 0x38, /* PMU_MISC_CON1 */
  737. .idle_offset = 0x0c,
  738. .ack_offset = 0x0c,
  739. .num_domains = ARRAY_SIZE(rk3066_pm_domains),
  740. .domain_info = rk3066_pm_domains,
  741. };
  742. static const struct rockchip_pmu_info rk3128_pmu = {
  743. .pwr_offset = 0x04,
  744. .status_offset = 0x08,
  745. .req_offset = 0x0c,
  746. .idle_offset = 0x10,
  747. .ack_offset = 0x10,
  748. .num_domains = ARRAY_SIZE(rk3128_pm_domains),
  749. .domain_info = rk3128_pm_domains,
  750. };
  751. static const struct rockchip_pmu_info rk3188_pmu = {
  752. .pwr_offset = 0x08,
  753. .status_offset = 0x0c,
  754. .req_offset = 0x38, /* PMU_MISC_CON1 */
  755. .idle_offset = 0x0c,
  756. .ack_offset = 0x0c,
  757. .num_domains = ARRAY_SIZE(rk3188_pm_domains),
  758. .domain_info = rk3188_pm_domains,
  759. };
  760. static const struct rockchip_pmu_info rk3228_pmu = {
  761. .req_offset = 0x40c,
  762. .idle_offset = 0x488,
  763. .ack_offset = 0x488,
  764. .num_domains = ARRAY_SIZE(rk3228_pm_domains),
  765. .domain_info = rk3228_pm_domains,
  766. };
  767. static const struct rockchip_pmu_info rk3288_pmu = {
  768. .pwr_offset = 0x08,
  769. .status_offset = 0x0c,
  770. .req_offset = 0x10,
  771. .idle_offset = 0x14,
  772. .ack_offset = 0x14,
  773. .core_pwrcnt_offset = 0x34,
  774. .gpu_pwrcnt_offset = 0x3c,
  775. .core_power_transition_time = 24, /* 1us */
  776. .gpu_power_transition_time = 24, /* 1us */
  777. .num_domains = ARRAY_SIZE(rk3288_pm_domains),
  778. .domain_info = rk3288_pm_domains,
  779. };
  780. static const struct rockchip_pmu_info rk3328_pmu = {
  781. .req_offset = 0x414,
  782. .idle_offset = 0x484,
  783. .ack_offset = 0x484,
  784. .num_domains = ARRAY_SIZE(rk3328_pm_domains),
  785. .domain_info = rk3328_pm_domains,
  786. };
  787. static const struct rockchip_pmu_info rk3366_pmu = {
  788. .pwr_offset = 0x0c,
  789. .status_offset = 0x10,
  790. .req_offset = 0x3c,
  791. .idle_offset = 0x40,
  792. .ack_offset = 0x40,
  793. .core_pwrcnt_offset = 0x48,
  794. .gpu_pwrcnt_offset = 0x50,
  795. .core_power_transition_time = 24,
  796. .gpu_power_transition_time = 24,
  797. .num_domains = ARRAY_SIZE(rk3366_pm_domains),
  798. .domain_info = rk3366_pm_domains,
  799. };
  800. static const struct rockchip_pmu_info rk3368_pmu = {
  801. .pwr_offset = 0x0c,
  802. .status_offset = 0x10,
  803. .req_offset = 0x3c,
  804. .idle_offset = 0x40,
  805. .ack_offset = 0x40,
  806. .core_pwrcnt_offset = 0x48,
  807. .gpu_pwrcnt_offset = 0x50,
  808. .core_power_transition_time = 24,
  809. .gpu_power_transition_time = 24,
  810. .num_domains = ARRAY_SIZE(rk3368_pm_domains),
  811. .domain_info = rk3368_pm_domains,
  812. };
  813. static const struct rockchip_pmu_info rk3399_pmu = {
  814. .pwr_offset = 0x14,
  815. .status_offset = 0x18,
  816. .req_offset = 0x60,
  817. .idle_offset = 0x64,
  818. .ack_offset = 0x68,
  819. /* ARM Trusted Firmware manages power transition times */
  820. .num_domains = ARRAY_SIZE(rk3399_pm_domains),
  821. .domain_info = rk3399_pm_domains,
  822. };
  823. static const struct of_device_id rockchip_pm_domain_dt_match[] = {
  824. {
  825. .compatible = "rockchip,px30-power-controller",
  826. .data = (void *)&px30_pmu,
  827. },
  828. {
  829. .compatible = "rockchip,rk3036-power-controller",
  830. .data = (void *)&rk3036_pmu,
  831. },
  832. {
  833. .compatible = "rockchip,rk3066-power-controller",
  834. .data = (void *)&rk3066_pmu,
  835. },
  836. {
  837. .compatible = "rockchip,rk3128-power-controller",
  838. .data = (void *)&rk3128_pmu,
  839. },
  840. {
  841. .compatible = "rockchip,rk3188-power-controller",
  842. .data = (void *)&rk3188_pmu,
  843. },
  844. {
  845. .compatible = "rockchip,rk3228-power-controller",
  846. .data = (void *)&rk3228_pmu,
  847. },
  848. {
  849. .compatible = "rockchip,rk3288-power-controller",
  850. .data = (void *)&rk3288_pmu,
  851. },
  852. {
  853. .compatible = "rockchip,rk3328-power-controller",
  854. .data = (void *)&rk3328_pmu,
  855. },
  856. {
  857. .compatible = "rockchip,rk3366-power-controller",
  858. .data = (void *)&rk3366_pmu,
  859. },
  860. {
  861. .compatible = "rockchip,rk3368-power-controller",
  862. .data = (void *)&rk3368_pmu,
  863. },
  864. {
  865. .compatible = "rockchip,rk3399-power-controller",
  866. .data = (void *)&rk3399_pmu,
  867. },
  868. { /* sentinel */ },
  869. };
  870. static struct platform_driver rockchip_pm_domain_driver = {
  871. .probe = rockchip_pm_domain_probe,
  872. .driver = {
  873. .name = "rockchip-pm-domain",
  874. .of_match_table = rockchip_pm_domain_dt_match,
  875. /*
  876. * We can't forcibly eject devices form power domain,
  877. * so we can't really remove power domains once they
  878. * were added.
  879. */
  880. .suppress_bind_attrs = true,
  881. },
  882. };
  883. static int __init rockchip_pm_domain_drv_register(void)
  884. {
  885. return platform_driver_register(&rockchip_pm_domain_driver);
  886. }
  887. postcore_initcall(rockchip_pm_domain_drv_register);