sde_power_handle.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d]: " fmt, __func__, __LINE__
  6. #include <linux/clk.h>
  7. #include <linux/kernel.h>
  8. #include <linux/of.h>
  9. #include <linux/string.h>
  10. #include <linux/of_address.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/msm-bus.h>
  15. #include <linux/msm-bus-board.h>
  16. #include <linux/sde_io_util.h>
  17. #include <linux/sde_rsc.h>
  18. #include "sde_power_handle.h"
  19. #include "sde_trace.h"
  20. #include "sde_dbg.h"
  21. static const char *data_bus_name[SDE_POWER_HANDLE_DBUS_ID_MAX] = {
  22. [SDE_POWER_HANDLE_DBUS_ID_MNOC] = "qcom,sde-data-bus",
  23. [SDE_POWER_HANDLE_DBUS_ID_LLCC] = "qcom,sde-llcc-bus",
  24. [SDE_POWER_HANDLE_DBUS_ID_EBI] = "qcom,sde-ebi-bus",
  25. };
  26. const char *sde_power_handle_get_dbus_name(u32 bus_id)
  27. {
  28. if (bus_id < SDE_POWER_HANDLE_DBUS_ID_MAX)
  29. return data_bus_name[bus_id];
  30. return NULL;
  31. }
  32. static void sde_power_event_trigger_locked(struct sde_power_handle *phandle,
  33. u32 event_type)
  34. {
  35. struct sde_power_event *event;
  36. list_for_each_entry(event, &phandle->event_list, list) {
  37. if (event->event_type & event_type)
  38. event->cb_fnc(event_type, event->usr);
  39. }
  40. }
  41. static inline void sde_power_rsc_client_init(struct sde_power_handle *phandle)
  42. {
  43. /* creates the rsc client */
  44. if (!phandle->rsc_client_init) {
  45. phandle->rsc_client = sde_rsc_client_create(SDE_RSC_INDEX,
  46. "sde_power_handle", SDE_RSC_CLK_CLIENT, 0);
  47. if (IS_ERR_OR_NULL(phandle->rsc_client)) {
  48. pr_debug("sde rsc client create failed :%ld\n",
  49. PTR_ERR(phandle->rsc_client));
  50. phandle->rsc_client = NULL;
  51. }
  52. phandle->rsc_client_init = true;
  53. }
  54. }
  55. static int sde_power_rsc_update(struct sde_power_handle *phandle, bool enable)
  56. {
  57. u32 rsc_state;
  58. int ret = 0;
  59. rsc_state = enable ? SDE_RSC_CLK_STATE : SDE_RSC_IDLE_STATE;
  60. if (phandle->rsc_client)
  61. ret = sde_rsc_client_state_update(phandle->rsc_client,
  62. rsc_state, NULL, SDE_RSC_INVALID_CRTC_ID, NULL);
  63. return ret;
  64. }
  65. static int sde_power_parse_dt_supply(struct platform_device *pdev,
  66. struct dss_module_power *mp)
  67. {
  68. int i = 0, rc = 0;
  69. u32 tmp = 0;
  70. struct device_node *of_node = NULL, *supply_root_node = NULL;
  71. struct device_node *supply_node = NULL;
  72. if (!pdev || !mp) {
  73. pr_err("invalid input param pdev:%pK mp:%pK\n", pdev, mp);
  74. return -EINVAL;
  75. }
  76. of_node = pdev->dev.of_node;
  77. mp->num_vreg = 0;
  78. supply_root_node = of_get_child_by_name(of_node,
  79. "qcom,platform-supply-entries");
  80. if (!supply_root_node) {
  81. pr_debug("no supply entry present\n");
  82. return rc;
  83. }
  84. for_each_child_of_node(supply_root_node, supply_node)
  85. mp->num_vreg++;
  86. if (mp->num_vreg == 0) {
  87. pr_debug("no vreg\n");
  88. return rc;
  89. }
  90. pr_debug("vreg found. count=%d\n", mp->num_vreg);
  91. mp->vreg_config = devm_kzalloc(&pdev->dev, sizeof(struct dss_vreg) *
  92. mp->num_vreg, GFP_KERNEL);
  93. if (!mp->vreg_config) {
  94. rc = -ENOMEM;
  95. return rc;
  96. }
  97. for_each_child_of_node(supply_root_node, supply_node) {
  98. const char *st = NULL;
  99. rc = of_property_read_string(supply_node,
  100. "qcom,supply-name", &st);
  101. if (rc) {
  102. pr_err("error reading name. rc=%d\n", rc);
  103. goto error;
  104. }
  105. strlcpy(mp->vreg_config[i].vreg_name, st,
  106. sizeof(mp->vreg_config[i].vreg_name));
  107. rc = of_property_read_u32(supply_node,
  108. "qcom,supply-min-voltage", &tmp);
  109. if (rc) {
  110. pr_err("error reading min volt. rc=%d\n", rc);
  111. goto error;
  112. }
  113. mp->vreg_config[i].min_voltage = tmp;
  114. rc = of_property_read_u32(supply_node,
  115. "qcom,supply-max-voltage", &tmp);
  116. if (rc) {
  117. pr_err("error reading max volt. rc=%d\n", rc);
  118. goto error;
  119. }
  120. mp->vreg_config[i].max_voltage = tmp;
  121. rc = of_property_read_u32(supply_node,
  122. "qcom,supply-enable-load", &tmp);
  123. if (rc) {
  124. pr_err("error reading enable load. rc=%d\n", rc);
  125. goto error;
  126. }
  127. mp->vreg_config[i].enable_load = tmp;
  128. rc = of_property_read_u32(supply_node,
  129. "qcom,supply-disable-load", &tmp);
  130. if (rc) {
  131. pr_err("error reading disable load. rc=%d\n", rc);
  132. goto error;
  133. }
  134. mp->vreg_config[i].disable_load = tmp;
  135. rc = of_property_read_u32(supply_node,
  136. "qcom,supply-pre-on-sleep", &tmp);
  137. if (rc)
  138. pr_debug("error reading supply pre sleep value. rc=%d\n",
  139. rc);
  140. mp->vreg_config[i].pre_on_sleep = (!rc ? tmp : 0);
  141. rc = of_property_read_u32(supply_node,
  142. "qcom,supply-pre-off-sleep", &tmp);
  143. if (rc)
  144. pr_debug("error reading supply pre sleep value. rc=%d\n",
  145. rc);
  146. mp->vreg_config[i].pre_off_sleep = (!rc ? tmp : 0);
  147. rc = of_property_read_u32(supply_node,
  148. "qcom,supply-post-on-sleep", &tmp);
  149. if (rc)
  150. pr_debug("error reading supply post sleep value. rc=%d\n",
  151. rc);
  152. mp->vreg_config[i].post_on_sleep = (!rc ? tmp : 0);
  153. rc = of_property_read_u32(supply_node,
  154. "qcom,supply-post-off-sleep", &tmp);
  155. if (rc)
  156. pr_debug("error reading supply post sleep value. rc=%d\n",
  157. rc);
  158. mp->vreg_config[i].post_off_sleep = (!rc ? tmp : 0);
  159. pr_debug("%s min=%d, max=%d, enable=%d, disable=%d, preonsleep=%d, postonsleep=%d, preoffsleep=%d, postoffsleep=%d\n",
  160. mp->vreg_config[i].vreg_name,
  161. mp->vreg_config[i].min_voltage,
  162. mp->vreg_config[i].max_voltage,
  163. mp->vreg_config[i].enable_load,
  164. mp->vreg_config[i].disable_load,
  165. mp->vreg_config[i].pre_on_sleep,
  166. mp->vreg_config[i].post_on_sleep,
  167. mp->vreg_config[i].pre_off_sleep,
  168. mp->vreg_config[i].post_off_sleep);
  169. ++i;
  170. rc = 0;
  171. }
  172. return rc;
  173. error:
  174. if (mp->vreg_config) {
  175. devm_kfree(&pdev->dev, mp->vreg_config);
  176. mp->vreg_config = NULL;
  177. mp->num_vreg = 0;
  178. }
  179. return rc;
  180. }
  181. static int sde_power_parse_dt_clock(struct platform_device *pdev,
  182. struct dss_module_power *mp)
  183. {
  184. u32 i = 0, rc = 0;
  185. const char *clock_name;
  186. u32 clock_rate = 0;
  187. u32 clock_max_rate = 0;
  188. int num_clk = 0;
  189. if (!pdev || !mp) {
  190. pr_err("invalid input param pdev:%pK mp:%pK\n", pdev, mp);
  191. return -EINVAL;
  192. }
  193. mp->num_clk = 0;
  194. num_clk = of_property_count_strings(pdev->dev.of_node,
  195. "clock-names");
  196. if (num_clk <= 0) {
  197. pr_debug("clocks are not defined\n");
  198. goto clk_err;
  199. }
  200. mp->num_clk = num_clk;
  201. mp->clk_config = devm_kzalloc(&pdev->dev,
  202. sizeof(struct dss_clk) * num_clk, GFP_KERNEL);
  203. if (!mp->clk_config) {
  204. rc = -ENOMEM;
  205. mp->num_clk = 0;
  206. goto clk_err;
  207. }
  208. for (i = 0; i < num_clk; i++) {
  209. of_property_read_string_index(pdev->dev.of_node, "clock-names",
  210. i, &clock_name);
  211. strlcpy(mp->clk_config[i].clk_name, clock_name,
  212. sizeof(mp->clk_config[i].clk_name));
  213. of_property_read_u32_index(pdev->dev.of_node, "clock-rate",
  214. i, &clock_rate);
  215. mp->clk_config[i].rate = clock_rate;
  216. if (!clock_rate)
  217. mp->clk_config[i].type = DSS_CLK_AHB;
  218. else
  219. mp->clk_config[i].type = DSS_CLK_PCLK;
  220. clock_max_rate = 0;
  221. of_property_read_u32_index(pdev->dev.of_node, "clock-max-rate",
  222. i, &clock_max_rate);
  223. mp->clk_config[i].max_rate = clock_max_rate;
  224. }
  225. clk_err:
  226. return rc;
  227. }
  228. #ifdef CONFIG_QCOM_BUS_SCALING
  229. #define MAX_AXI_PORT_COUNT 3
  230. static int _sde_power_data_bus_set_quota(
  231. struct sde_power_data_bus_handle *pdbus,
  232. u64 in_ab_quota, u64 in_ib_quota)
  233. {
  234. int new_uc_idx;
  235. u64 ab_quota[MAX_AXI_PORT_COUNT] = {0, 0};
  236. u64 ib_quota[MAX_AXI_PORT_COUNT] = {0, 0};
  237. int rc;
  238. if (pdbus->data_bus_hdl < 1) {
  239. pr_err("invalid bus handle %d\n", pdbus->data_bus_hdl);
  240. return -EINVAL;
  241. }
  242. if (!in_ab_quota && !in_ib_quota) {
  243. new_uc_idx = 0;
  244. } else {
  245. int i;
  246. struct msm_bus_vectors *vect = NULL;
  247. struct msm_bus_scale_pdata *bw_table =
  248. pdbus->data_bus_scale_table;
  249. u32 total_data_paths_cnt = pdbus->data_paths_cnt;
  250. if (!bw_table || !total_data_paths_cnt ||
  251. total_data_paths_cnt > MAX_AXI_PORT_COUNT) {
  252. pr_err("invalid input\n");
  253. return -EINVAL;
  254. }
  255. ab_quota[0] = div_u64(in_ab_quota, total_data_paths_cnt);
  256. ib_quota[0] = div_u64(in_ib_quota, total_data_paths_cnt);
  257. for (i = 1; i < total_data_paths_cnt; i++) {
  258. ab_quota[i] = ab_quota[0];
  259. ib_quota[i] = ib_quota[0];
  260. }
  261. new_uc_idx = (pdbus->curr_bw_uc_idx %
  262. (bw_table->num_usecases - 1)) + 1;
  263. for (i = 0; i < total_data_paths_cnt; i++) {
  264. vect = &bw_table->usecase[new_uc_idx].vectors[i];
  265. vect->ab = ab_quota[i];
  266. vect->ib = ib_quota[i];
  267. pr_debug(
  268. "%s uc_idx=%d idx=%d ab=%llu ib=%llu\n",
  269. bw_table->name, new_uc_idx, i, vect->ab,
  270. vect->ib);
  271. }
  272. }
  273. pdbus->curr_bw_uc_idx = new_uc_idx;
  274. SDE_ATRACE_BEGIN("msm_bus_scale_req");
  275. rc = msm_bus_scale_client_update_request(pdbus->data_bus_hdl,
  276. new_uc_idx);
  277. SDE_ATRACE_END("msm_bus_scale_req");
  278. return rc;
  279. }
  280. int sde_power_data_bus_set_quota(struct sde_power_handle *phandle,
  281. u32 bus_id, u64 ab_quota, u64 ib_quota)
  282. {
  283. int rc = 0;
  284. if (!phandle || bus_id >= SDE_POWER_HANDLE_DBUS_ID_MAX) {
  285. pr_err("invalid parameters\n");
  286. return -EINVAL;
  287. }
  288. mutex_lock(&phandle->phandle_lock);
  289. trace_sde_perf_update_bus(bus_id, ab_quota, ib_quota);
  290. if (phandle->data_bus_handle[bus_id].data_bus_hdl)
  291. rc = _sde_power_data_bus_set_quota(
  292. &phandle->data_bus_handle[bus_id], ab_quota, ib_quota);
  293. mutex_unlock(&phandle->phandle_lock);
  294. return rc;
  295. }
  296. static void sde_power_data_bus_unregister(
  297. struct sde_power_data_bus_handle *pdbus)
  298. {
  299. if (pdbus->data_bus_hdl) {
  300. msm_bus_scale_unregister_client(pdbus->data_bus_hdl);
  301. pdbus->data_bus_hdl = 0;
  302. }
  303. }
  304. static int sde_power_data_bus_parse(struct platform_device *pdev,
  305. struct sde_power_data_bus_handle *pdbus, const char *name)
  306. {
  307. struct device_node *node;
  308. int rc = 0;
  309. int paths;
  310. node = of_get_child_by_name(pdev->dev.of_node, name);
  311. if (!node)
  312. goto end;
  313. rc = of_property_read_u32(node, "qcom,msm-bus,num-paths", &paths);
  314. if (rc) {
  315. pr_err("Error. qcom,msm-bus,num-paths not found\n");
  316. return rc;
  317. }
  318. pdbus->data_paths_cnt = paths;
  319. pdbus->data_bus_scale_table = msm_bus_pdata_from_node(pdev, node);
  320. if (IS_ERR_OR_NULL(pdbus->data_bus_scale_table)) {
  321. pr_err("reg bus handle parsing failed\n");
  322. rc = PTR_ERR(pdbus->data_bus_scale_table);
  323. if (!pdbus->data_bus_scale_table)
  324. rc = -EINVAL;
  325. goto end;
  326. }
  327. pdbus->data_bus_hdl = msm_bus_scale_register_client(
  328. pdbus->data_bus_scale_table);
  329. if (!pdbus->data_bus_hdl) {
  330. pr_err("data_bus_client register failed\n");
  331. rc = -EINVAL;
  332. goto end;
  333. }
  334. pr_debug("register %s data_bus_hdl=%x\n", name, pdbus->data_bus_hdl);
  335. end:
  336. return rc;
  337. }
  338. static int sde_power_reg_bus_parse(struct platform_device *pdev,
  339. struct sde_power_handle *phandle)
  340. {
  341. struct device_node *node;
  342. struct msm_bus_scale_pdata *bus_scale_table;
  343. int rc = 0;
  344. node = of_get_child_by_name(pdev->dev.of_node, "qcom,sde-reg-bus");
  345. if (node) {
  346. bus_scale_table = msm_bus_pdata_from_node(pdev, node);
  347. if (IS_ERR_OR_NULL(bus_scale_table)) {
  348. pr_err("reg bus handle parsing failed\n");
  349. rc = PTR_ERR(bus_scale_table);
  350. if (!bus_scale_table)
  351. rc = -EINVAL;
  352. goto end;
  353. }
  354. phandle->reg_bus_hdl = msm_bus_scale_register_client(
  355. bus_scale_table);
  356. if (!phandle->reg_bus_hdl) {
  357. pr_err("reg_bus_client register failed\n");
  358. rc = -EINVAL;
  359. goto end;
  360. }
  361. pr_debug("register reg_bus_hdl=%x\n", phandle->reg_bus_hdl);
  362. }
  363. end:
  364. return rc;
  365. }
  366. static void sde_power_reg_bus_unregister(u32 reg_bus_hdl)
  367. {
  368. if (reg_bus_hdl)
  369. msm_bus_scale_unregister_client(reg_bus_hdl);
  370. }
  371. static int sde_power_reg_bus_update(u32 reg_bus_hdl, u32 usecase_ndx)
  372. {
  373. int rc = 0;
  374. if (reg_bus_hdl) {
  375. SDE_ATRACE_BEGIN("msm_bus_scale_req");
  376. rc = msm_bus_scale_client_update_request(reg_bus_hdl,
  377. usecase_ndx);
  378. SDE_ATRACE_END("msm_bus_scale_req");
  379. }
  380. if (rc)
  381. pr_err("failed to set reg bus vote rc=%d\n", rc);
  382. return rc;
  383. }
  384. #else
  385. static int _sde_power_data_bus_set_quota(
  386. struct sde_power_data_bus_handle *pdbus,
  387. u64 in_ab_quota, u64 in_ib_quota)
  388. {
  389. return 0;
  390. }
  391. static int sde_power_data_bus_parse(struct platform_device *pdev,
  392. struct sde_power_data_bus_handle *pdbus, const char *name)
  393. {
  394. return 0;
  395. }
  396. static void sde_power_data_bus_unregister(
  397. struct sde_power_data_bus_handle *pdbus)
  398. {
  399. }
  400. int sde_power_data_bus_set_quota(struct sde_power_handle *phandle,
  401. u32 bus_id, u64 ab_quota, u64 ib_quota)
  402. {
  403. return 0;
  404. }
  405. static int sde_power_reg_bus_parse(struct platform_device *pdev,
  406. struct sde_power_handle *phandle)
  407. {
  408. return 0;
  409. }
  410. static void sde_power_reg_bus_unregister(u32 reg_bus_hdl)
  411. {
  412. }
  413. static int sde_power_reg_bus_update(u32 reg_bus_hdl, u32 usecase_ndx)
  414. {
  415. return 0;
  416. }
  417. int sde_power_data_bus_state_update(struct sde_power_handle *phandle,
  418. bool enable)
  419. {
  420. return 0;
  421. }
  422. #endif
  423. int sde_power_resource_init(struct platform_device *pdev,
  424. struct sde_power_handle *phandle)
  425. {
  426. int rc = 0, i;
  427. struct dss_module_power *mp;
  428. if (!phandle || !pdev) {
  429. pr_err("invalid input param\n");
  430. rc = -EINVAL;
  431. goto end;
  432. }
  433. mp = &phandle->mp;
  434. phandle->dev = &pdev->dev;
  435. rc = sde_power_parse_dt_clock(pdev, mp);
  436. if (rc) {
  437. pr_err("device clock parsing failed\n");
  438. goto end;
  439. }
  440. rc = sde_power_parse_dt_supply(pdev, mp);
  441. if (rc) {
  442. pr_err("device vreg supply parsing failed\n");
  443. goto parse_vreg_err;
  444. }
  445. rc = msm_dss_config_vreg(&pdev->dev,
  446. mp->vreg_config, mp->num_vreg, 1);
  447. if (rc) {
  448. pr_err("vreg config failed rc=%d\n", rc);
  449. goto vreg_err;
  450. }
  451. rc = msm_dss_get_clk(&pdev->dev, mp->clk_config, mp->num_clk);
  452. if (rc) {
  453. pr_err("clock get failed rc=%d\n", rc);
  454. goto clk_err;
  455. }
  456. rc = msm_dss_clk_set_rate(mp->clk_config, mp->num_clk);
  457. if (rc) {
  458. pr_err("clock set rate failed rc=%d\n", rc);
  459. goto bus_err;
  460. }
  461. rc = sde_power_reg_bus_parse(pdev, phandle);
  462. if (rc) {
  463. pr_err("register bus parse failed rc=%d\n", rc);
  464. goto bus_err;
  465. }
  466. for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
  467. i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
  468. rc = sde_power_data_bus_parse(pdev,
  469. &phandle->data_bus_handle[i],
  470. data_bus_name[i]);
  471. if (rc) {
  472. pr_err("register data bus parse failed id=%d rc=%d\n",
  473. i, rc);
  474. goto data_bus_err;
  475. }
  476. }
  477. if (of_find_property(pdev->dev.of_node, "qcom,dss-cx-ipeak", NULL))
  478. phandle->dss_cx_ipeak = cx_ipeak_register(pdev->dev.of_node,
  479. "qcom,dss-cx-ipeak");
  480. else
  481. pr_debug("cx ipeak client parse failed\n");
  482. INIT_LIST_HEAD(&phandle->event_list);
  483. phandle->rsc_client = NULL;
  484. phandle->rsc_client_init = false;
  485. mutex_init(&phandle->phandle_lock);
  486. return rc;
  487. data_bus_err:
  488. for (i--; i >= 0; i--)
  489. sde_power_data_bus_unregister(&phandle->data_bus_handle[i]);
  490. sde_power_reg_bus_unregister(phandle->reg_bus_hdl);
  491. bus_err:
  492. msm_dss_put_clk(mp->clk_config, mp->num_clk);
  493. clk_err:
  494. msm_dss_config_vreg(&pdev->dev, mp->vreg_config, mp->num_vreg, 0);
  495. vreg_err:
  496. if (mp->vreg_config)
  497. devm_kfree(&pdev->dev, mp->vreg_config);
  498. mp->num_vreg = 0;
  499. parse_vreg_err:
  500. if (mp->clk_config)
  501. devm_kfree(&pdev->dev, mp->clk_config);
  502. mp->num_clk = 0;
  503. end:
  504. return rc;
  505. }
  506. void sde_power_resource_deinit(struct platform_device *pdev,
  507. struct sde_power_handle *phandle)
  508. {
  509. struct dss_module_power *mp;
  510. struct sde_power_event *curr_event, *next_event;
  511. int i;
  512. if (!phandle || !pdev) {
  513. pr_err("invalid input param\n");
  514. return;
  515. }
  516. mp = &phandle->mp;
  517. mutex_lock(&phandle->phandle_lock);
  518. list_for_each_entry_safe(curr_event, next_event,
  519. &phandle->event_list, list) {
  520. pr_err("event:%d, client:%s still registered\n",
  521. curr_event->event_type,
  522. curr_event->client_name);
  523. curr_event->active = false;
  524. list_del(&curr_event->list);
  525. }
  526. mutex_unlock(&phandle->phandle_lock);
  527. if (phandle->dss_cx_ipeak)
  528. cx_ipeak_unregister(phandle->dss_cx_ipeak);
  529. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++)
  530. sde_power_data_bus_unregister(&phandle->data_bus_handle[i]);
  531. sde_power_reg_bus_unregister(phandle->reg_bus_hdl);
  532. msm_dss_put_clk(mp->clk_config, mp->num_clk);
  533. msm_dss_config_vreg(&pdev->dev, mp->vreg_config, mp->num_vreg, 0);
  534. if (mp->clk_config)
  535. devm_kfree(&pdev->dev, mp->clk_config);
  536. if (mp->vreg_config)
  537. devm_kfree(&pdev->dev, mp->vreg_config);
  538. mp->num_vreg = 0;
  539. mp->num_clk = 0;
  540. if (phandle->rsc_client)
  541. sde_rsc_client_destroy(phandle->rsc_client);
  542. }
  543. int sde_power_scale_reg_bus(struct sde_power_handle *phandle,
  544. u32 usecase_ndx, bool skip_lock)
  545. {
  546. int rc = 0;
  547. if (!skip_lock)
  548. mutex_lock(&phandle->phandle_lock);
  549. pr_debug("%pS: requested:%d\n",
  550. __builtin_return_address(0), usecase_ndx);
  551. rc = sde_power_reg_bus_update(phandle->reg_bus_hdl,
  552. usecase_ndx);
  553. if (rc)
  554. pr_err("failed to set reg bus vote rc=%d\n", rc);
  555. if (!skip_lock)
  556. mutex_unlock(&phandle->phandle_lock);
  557. return rc;
  558. }
  559. static inline bool _resource_changed(u32 current_usecase_ndx,
  560. u32 max_usecase_ndx)
  561. {
  562. WARN_ON((current_usecase_ndx >= VOTE_INDEX_MAX)
  563. || (max_usecase_ndx >= VOTE_INDEX_MAX));
  564. if (((current_usecase_ndx >= VOTE_INDEX_LOW) && /* enabled */
  565. (max_usecase_ndx == VOTE_INDEX_DISABLE)) || /* max disabled */
  566. ((current_usecase_ndx == VOTE_INDEX_DISABLE) && /* disabled */
  567. (max_usecase_ndx >= VOTE_INDEX_LOW))) /* max enabled */
  568. return true;
  569. return false;
  570. }
  571. int sde_power_resource_enable(struct sde_power_handle *phandle, bool enable)
  572. {
  573. int rc = 0, i = 0;
  574. struct dss_module_power *mp;
  575. if (!phandle) {
  576. pr_err("invalid input argument\n");
  577. return -EINVAL;
  578. }
  579. mp = &phandle->mp;
  580. mutex_lock(&phandle->phandle_lock);
  581. pr_debug("enable:%d\n", enable);
  582. SDE_ATRACE_BEGIN("sde_power_resource_enable");
  583. /* RSC client init */
  584. sde_power_rsc_client_init(phandle);
  585. if (enable) {
  586. sde_power_event_trigger_locked(phandle,
  587. SDE_POWER_EVENT_PRE_ENABLE);
  588. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX &&
  589. phandle->data_bus_handle[i].data_bus_hdl; i++) {
  590. rc = _sde_power_data_bus_set_quota(
  591. &phandle->data_bus_handle[i],
  592. SDE_POWER_HANDLE_ENABLE_BUS_AB_QUOTA,
  593. SDE_POWER_HANDLE_ENABLE_BUS_IB_QUOTA);
  594. if (rc) {
  595. pr_err("failed to set data bus vote id=%d rc=%d\n",
  596. i, rc);
  597. goto vreg_err;
  598. }
  599. }
  600. rc = msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg,
  601. enable);
  602. if (rc) {
  603. pr_err("failed to enable vregs rc=%d\n", rc);
  604. goto vreg_err;
  605. }
  606. rc = sde_power_scale_reg_bus(phandle, VOTE_INDEX_LOW, true);
  607. if (rc) {
  608. pr_err("failed to set reg bus vote rc=%d\n", rc);
  609. goto reg_bus_hdl_err;
  610. }
  611. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_CASE1);
  612. rc = sde_power_rsc_update(phandle, true);
  613. if (rc) {
  614. pr_err("failed to update rsc\n");
  615. goto rsc_err;
  616. }
  617. rc = msm_dss_enable_clk(mp->clk_config, mp->num_clk, enable);
  618. if (rc) {
  619. pr_err("clock enable failed rc:%d\n", rc);
  620. goto clk_err;
  621. }
  622. sde_power_event_trigger_locked(phandle,
  623. SDE_POWER_EVENT_POST_ENABLE);
  624. } else {
  625. sde_power_event_trigger_locked(phandle,
  626. SDE_POWER_EVENT_PRE_DISABLE);
  627. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_CASE2);
  628. sde_power_rsc_update(phandle, false);
  629. msm_dss_enable_clk(mp->clk_config, mp->num_clk, enable);
  630. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, true);
  631. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, enable);
  632. for (i = SDE_POWER_HANDLE_DBUS_ID_MAX - 1; i >= 0; i--)
  633. if (phandle->data_bus_handle[i].data_bus_hdl)
  634. _sde_power_data_bus_set_quota(
  635. &phandle->data_bus_handle[i],
  636. SDE_POWER_HANDLE_DISABLE_BUS_AB_QUOTA,
  637. SDE_POWER_HANDLE_DISABLE_BUS_IB_QUOTA);
  638. sde_power_event_trigger_locked(phandle,
  639. SDE_POWER_EVENT_POST_DISABLE);
  640. }
  641. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_EXIT);
  642. mutex_unlock(&phandle->phandle_lock);
  643. SDE_ATRACE_END("sde_power_resource_enable");
  644. return rc;
  645. clk_err:
  646. sde_power_rsc_update(phandle, false);
  647. rsc_err:
  648. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, true);
  649. reg_bus_hdl_err:
  650. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, 0);
  651. vreg_err:
  652. for (i-- ; i >= 0 && phandle->data_bus_handle[i].data_bus_hdl; i--)
  653. _sde_power_data_bus_set_quota(
  654. &phandle->data_bus_handle[i],
  655. SDE_POWER_HANDLE_DISABLE_BUS_AB_QUOTA,
  656. SDE_POWER_HANDLE_DISABLE_BUS_IB_QUOTA);
  657. mutex_unlock(&phandle->phandle_lock);
  658. SDE_ATRACE_END("sde_power_resource_enable");
  659. return rc;
  660. }
  661. int sde_cx_ipeak_vote(struct sde_power_handle *phandle, struct dss_clk *clock,
  662. u64 requested_clk_rate, u64 prev_clk_rate, bool enable_vote)
  663. {
  664. int ret = 0;
  665. u64 curr_core_clk_rate, max_core_clk_rate, prev_core_clk_rate;
  666. if (!phandle->dss_cx_ipeak) {
  667. pr_debug("%pS->%s: Invalid input\n",
  668. __builtin_return_address(0), __func__);
  669. return -EOPNOTSUPP;
  670. }
  671. if (strcmp("core_clk", clock->clk_name)) {
  672. pr_debug("Not a core clk , cx_ipeak vote not needed\n");
  673. return -EOPNOTSUPP;
  674. }
  675. curr_core_clk_rate = clock->rate;
  676. max_core_clk_rate = clock->max_rate;
  677. prev_core_clk_rate = prev_clk_rate;
  678. if (enable_vote && requested_clk_rate == max_core_clk_rate &&
  679. curr_core_clk_rate != requested_clk_rate)
  680. ret = cx_ipeak_update(phandle->dss_cx_ipeak, true);
  681. else if (!enable_vote && requested_clk_rate != max_core_clk_rate &&
  682. prev_core_clk_rate == max_core_clk_rate)
  683. ret = cx_ipeak_update(phandle->dss_cx_ipeak, false);
  684. if (ret)
  685. SDE_EVT32(ret, enable_vote, requested_clk_rate,
  686. curr_core_clk_rate, prev_core_clk_rate);
  687. return ret;
  688. }
  689. int sde_power_clk_set_rate(struct sde_power_handle *phandle, char *clock_name,
  690. u64 rate)
  691. {
  692. int i, rc = -EINVAL;
  693. struct dss_module_power *mp;
  694. u64 prev_clk_rate, requested_clk_rate;
  695. if (!phandle) {
  696. pr_err("invalid input power handle\n");
  697. return -EINVAL;
  698. }
  699. mp = &phandle->mp;
  700. for (i = 0; i < mp->num_clk; i++) {
  701. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  702. if (mp->clk_config[i].max_rate &&
  703. (rate > mp->clk_config[i].max_rate))
  704. rate = mp->clk_config[i].max_rate;
  705. prev_clk_rate = mp->clk_config[i].rate;
  706. requested_clk_rate = rate;
  707. sde_cx_ipeak_vote(phandle, &mp->clk_config[i],
  708. requested_clk_rate, prev_clk_rate, true);
  709. mp->clk_config[i].rate = rate;
  710. rc = msm_dss_single_clk_set_rate(&mp->clk_config[i]);
  711. if (!rc)
  712. sde_cx_ipeak_vote(phandle, &mp->clk_config[i],
  713. requested_clk_rate, prev_clk_rate, false);
  714. break;
  715. }
  716. }
  717. return rc;
  718. }
  719. u64 sde_power_clk_get_rate(struct sde_power_handle *phandle, char *clock_name)
  720. {
  721. int i;
  722. struct dss_module_power *mp;
  723. u64 rate = -EINVAL;
  724. if (!phandle) {
  725. pr_err("invalid input power handle\n");
  726. return -EINVAL;
  727. }
  728. mp = &phandle->mp;
  729. for (i = 0; i < mp->num_clk; i++) {
  730. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  731. rate = clk_get_rate(mp->clk_config[i].clk);
  732. break;
  733. }
  734. }
  735. return rate;
  736. }
  737. u64 sde_power_clk_get_max_rate(struct sde_power_handle *phandle,
  738. char *clock_name)
  739. {
  740. int i;
  741. struct dss_module_power *mp;
  742. u64 rate = 0;
  743. if (!phandle) {
  744. pr_err("invalid input power handle\n");
  745. return 0;
  746. }
  747. mp = &phandle->mp;
  748. for (i = 0; i < mp->num_clk; i++) {
  749. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  750. rate = mp->clk_config[i].max_rate;
  751. break;
  752. }
  753. }
  754. return rate;
  755. }
  756. struct clk *sde_power_clk_get_clk(struct sde_power_handle *phandle,
  757. char *clock_name)
  758. {
  759. int i;
  760. struct dss_module_power *mp;
  761. struct clk *clk = NULL;
  762. if (!phandle) {
  763. pr_err("invalid input power handle\n");
  764. return 0;
  765. }
  766. mp = &phandle->mp;
  767. for (i = 0; i < mp->num_clk; i++) {
  768. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  769. clk = mp->clk_config[i].clk;
  770. break;
  771. }
  772. }
  773. return clk;
  774. }
  775. int sde_power_clk_set_flags(struct sde_power_handle *phandle,
  776. char *clock_name, unsigned long flags)
  777. {
  778. struct clk *clk;
  779. if (!phandle) {
  780. pr_err("invalid input power handle\n");
  781. return -EINVAL;
  782. }
  783. if (!clock_name) {
  784. pr_err("invalid input clock name\n");
  785. return -EINVAL;
  786. }
  787. clk = sde_power_clk_get_clk(phandle, clock_name);
  788. if (!clk) {
  789. pr_err("get_clk failed for clk: %s\n", clock_name);
  790. return -EINVAL;
  791. }
  792. return clk_set_flags(clk, flags);
  793. }
  794. struct sde_power_event *sde_power_handle_register_event(
  795. struct sde_power_handle *phandle,
  796. u32 event_type, void (*cb_fnc)(u32 event_type, void *usr),
  797. void *usr, char *client_name)
  798. {
  799. struct sde_power_event *event;
  800. if (!phandle) {
  801. pr_err("invalid power handle\n");
  802. return ERR_PTR(-EINVAL);
  803. } else if (!cb_fnc || !event_type) {
  804. pr_err("no callback fnc or event type\n");
  805. return ERR_PTR(-EINVAL);
  806. }
  807. event = kzalloc(sizeof(struct sde_power_event), GFP_KERNEL);
  808. if (!event)
  809. return ERR_PTR(-ENOMEM);
  810. event->event_type = event_type;
  811. event->cb_fnc = cb_fnc;
  812. event->usr = usr;
  813. strlcpy(event->client_name, client_name, MAX_CLIENT_NAME_LEN);
  814. event->active = true;
  815. mutex_lock(&phandle->phandle_lock);
  816. list_add(&event->list, &phandle->event_list);
  817. mutex_unlock(&phandle->phandle_lock);
  818. return event;
  819. }
  820. void sde_power_handle_unregister_event(
  821. struct sde_power_handle *phandle,
  822. struct sde_power_event *event)
  823. {
  824. if (!phandle || !event) {
  825. pr_err("invalid phandle or event\n");
  826. } else if (!event->active) {
  827. pr_err("power handle deinit already done\n");
  828. kfree(event);
  829. } else {
  830. mutex_lock(&phandle->phandle_lock);
  831. list_del_init(&event->list);
  832. mutex_unlock(&phandle->phandle_lock);
  833. kfree(event);
  834. }
  835. }