dsi_pll.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "%s: " fmt, __func__
  7. #include <linux/module.h>
  8. #include <linux/of_device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/err.h>
  11. #include <linux/delay.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/of_address.h>
  14. #include "dsi_pll.h"
  15. static int dsi_pll_clock_register(struct platform_device *pdev,
  16. struct dsi_pll_resource *pll_res)
  17. {
  18. int rc;
  19. switch (pll_res->pll_revision) {
  20. case DSI_PLL_5NM:
  21. rc = dsi_pll_clock_register_5nm(pdev, pll_res);
  22. break;
  23. case DSI_PLL_4NM:
  24. rc = dsi_pll_clock_register_4nm(pdev, pll_res);
  25. break;
  26. default:
  27. rc = -EINVAL;
  28. break;
  29. }
  30. if (rc)
  31. DSI_PLL_ERR(pll_res, "clock register failed rc=%d\n", rc);
  32. return rc;
  33. }
  34. static inline int dsi_pll_get_ioresources(struct platform_device *pdev,
  35. void __iomem **regmap, char *resource_name)
  36. {
  37. int rc = 0;
  38. struct resource *rsc = platform_get_resource_byname(pdev,
  39. IORESOURCE_MEM, resource_name);
  40. if (rsc) {
  41. if (!regmap)
  42. return -ENOMEM;
  43. *regmap = devm_ioremap(&pdev->dev,
  44. rsc->start, resource_size(rsc));
  45. if (!*regmap)
  46. return -ENOMEM;
  47. }
  48. return rc;
  49. }
  50. static void dsi_pll_free_bootmem(u32 mem_addr, u32 size)
  51. {
  52. unsigned long pfn_start, pfn_end, pfn_idx;
  53. pfn_start = mem_addr >> PAGE_SHIFT;
  54. pfn_end = (mem_addr + size) >> PAGE_SHIFT;
  55. for (pfn_idx = pfn_start; pfn_idx < pfn_end; pfn_idx++)
  56. free_reserved_page(pfn_to_page(pfn_idx));
  57. }
  58. static void dsi_pll_parse_dfps(struct platform_device *pdev,
  59. struct dsi_pll_resource *pll_res)
  60. {
  61. struct device_node *pnode = NULL;
  62. const u32 *addr;
  63. void *trim_codes = NULL;
  64. u64 size;
  65. u32 offsets[2];
  66. pnode = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
  67. if (IS_ERR_OR_NULL(pnode)) {
  68. DSI_PLL_INFO(pll_res, "of_parse_phandle failed\n");
  69. goto node_err;
  70. }
  71. addr = of_get_address(pnode, 0, &size, NULL);
  72. if (!addr) {
  73. DSI_PLL_ERR(pll_res,
  74. "failed to parse the dfps memory address\n");
  75. goto node_err;
  76. }
  77. /* maintain compatibility for 32/64 bit */
  78. offsets[0] = (u32) of_read_ulong(addr, 2);
  79. offsets[1] = (u32) size;
  80. trim_codes = memremap(offsets[0], offsets[1], MEMREMAP_WB);
  81. if (!trim_codes)
  82. goto mem_err;
  83. pll_res->dfps = kzalloc(sizeof(struct dfps_info), GFP_KERNEL);
  84. if (IS_ERR_OR_NULL(pll_res->dfps)) {
  85. DSI_PLL_ERR(pll_res, "pll_res->dfps allocate failed\n");
  86. goto mem_err;
  87. }
  88. /* memcopy complete dfps structure from kernel virtual memory */
  89. memcpy_fromio(pll_res->dfps, trim_codes, sizeof(struct dfps_info));
  90. mem_err:
  91. if (trim_codes)
  92. memunmap(trim_codes);
  93. /* free the dfps memory here */
  94. dsi_pll_free_bootmem(offsets[0], offsets[1]);
  95. node_err:
  96. if (pnode)
  97. of_node_put(pnode);
  98. }
  99. static int dsi_pll_parse_dfps_from_dt(struct platform_device *pdev,
  100. struct dsi_pll_resource *pll_res)
  101. {
  102. int property_len = 0, rc = 0;
  103. u32 i = 0, code_size = 0, vco_rate_cnt = 0;
  104. struct device_node *pnode = NULL;
  105. struct pll_codes_info *pll_codes_info = NULL;
  106. struct pll_codes_entry *code_entry = NULL;
  107. struct dfps_codes_info *codes_dfps = NULL;
  108. struct pll_codes_header header = {};
  109. pnode = of_parse_phandle(pdev->dev.of_node, "pll_codes_region", 0);
  110. if (IS_ERR_OR_NULL(pnode)) {
  111. DSI_PLL_ERR(pll_res, "of_parse_phandle failed\n");
  112. pnode = NULL;
  113. rc = -EINVAL;
  114. goto err;
  115. }
  116. of_get_property(pnode, "reg", &property_len);
  117. if (property_len <= 0) {
  118. DSI_PLL_ERR(pll_res, "invalid property length\n");
  119. rc = -EINVAL;
  120. goto err;
  121. }
  122. rc = of_property_read_u32_array(pnode, "reg", (u32 *)&header,
  123. sizeof(header)/4);
  124. if (rc) {
  125. DSI_PLL_ERR(pll_res, "fail to get pll_codes data header\n");
  126. goto err;
  127. }
  128. if (header.magic_id != DSI_PLL_TRIM_CODES_MAGIC_ID) {
  129. DSI_PLL_ERR(pll_res, "pll codes magic id not match\n");
  130. rc = -EINVAL;
  131. goto err;
  132. }
  133. if (header.version < DSI_PLL_TRIM_CODES_VERSION) {
  134. DSI_PLL_ERR(pll_res, "unsupported pll trim codes version:%d\n",
  135. header.version);
  136. rc = -EINVAL;
  137. goto err;
  138. }
  139. if (header.size < sizeof(struct pll_codes_header)) {
  140. DSI_PLL_ERR(pll_res, "invalid header size:%d\n", header.size);
  141. rc = -EINVAL;
  142. goto err;
  143. }
  144. if (header.size == sizeof(struct pll_codes_header)) {
  145. DSI_PLL_WARN(pll_res, "zero entry detected\n");
  146. rc = -EINVAL;
  147. goto err;
  148. }
  149. if ((header.num_entries * sizeof(struct pll_codes_entry) +
  150. sizeof(struct pll_codes_header)) != header.size) {
  151. DSI_PLL_ERR(pll_res, "num_entries not match with size\n");
  152. rc = -EINVAL;
  153. goto err;
  154. }
  155. code_size = roundup(header.size, 4);
  156. if (code_size > property_len) {
  157. DSI_PLL_ERR(pll_res, "pll code bigger than node space\n");
  158. rc = -EINVAL;
  159. goto err;
  160. }
  161. pll_codes_info = kzalloc(code_size, GFP_KERNEL);
  162. if (IS_ERR_OR_NULL(pll_codes_info)) {
  163. DSI_PLL_ERR(pll_res, "fail to alloc memory for pll codes\n");
  164. rc = -ENOMEM;
  165. goto err;
  166. }
  167. rc = of_property_read_u32_array(pnode, "reg", (u32 *)pll_codes_info,
  168. code_size/4);
  169. if (rc) {
  170. DSI_PLL_ERR(pll_res, "fail to get pll_codes data\n");
  171. goto err;
  172. }
  173. pll_res->dfps = kzalloc(sizeof(struct dfps_info), GFP_KERNEL);
  174. if (IS_ERR_OR_NULL(pll_res->dfps)) {
  175. DSI_PLL_ERR(pll_res, "pll_res->dfps allocate failed\n");
  176. rc = -ENOMEM;
  177. goto err;
  178. }
  179. code_entry = (struct pll_codes_entry *)&pll_codes_info->pll_code_data;
  180. for (i = 0; i < header.num_entries; i++) {
  181. if (code_entry[i].device_id == pll_res->index) {
  182. codes_dfps = &pll_res->dfps->codes_dfps[vco_rate_cnt];
  183. codes_dfps->is_valid = 1;
  184. codes_dfps->clk_rate = code_entry[i].vco_rate;
  185. codes_dfps->pll_codes.pll_codes_1 =
  186. code_entry[i].pll_codes[0];
  187. codes_dfps->pll_codes.pll_codes_2 =
  188. code_entry[i].pll_codes[1];
  189. codes_dfps->pll_codes.pll_codes_3 =
  190. code_entry[i].pll_codes[2];
  191. vco_rate_cnt++;
  192. }
  193. if (vco_rate_cnt >= DFPS_MAX_NUM_OF_FRAME_RATES)
  194. break;
  195. }
  196. pll_res->dfps->vco_rate_cnt = vco_rate_cnt;
  197. err:
  198. kfree(pll_codes_info);
  199. if (pnode)
  200. of_node_put(pnode);
  201. return rc;
  202. }
  203. int dsi_pll_init(struct platform_device *pdev, struct dsi_pll_resource **pll)
  204. {
  205. int rc = 0;
  206. const char *label;
  207. struct dsi_pll_resource *pll_res = NULL;
  208. if (!pdev->dev.of_node) {
  209. pr_err("Invalid DSI PHY node\n");
  210. return -ENOTSUPP;
  211. }
  212. pll_res = devm_kzalloc(&pdev->dev, sizeof(struct dsi_pll_resource),
  213. GFP_KERNEL);
  214. if (!pll_res)
  215. return -ENOMEM;
  216. *pll = pll_res;
  217. label = of_get_property(pdev->dev.of_node, "pll-label", NULL);
  218. if (!label) {
  219. DSI_PLL_ERR(pll_res, "DSI pll label not specified\n");
  220. return 0;
  221. }
  222. DSI_PLL_INFO(pll_res, "DSI pll label = %s\n", label);
  223. if (!strcmp(label, "dsi_pll_4nm"))
  224. pll_res->pll_revision = DSI_PLL_4NM;
  225. else if (!strcmp(label, "dsi_pll_5nm"))
  226. pll_res->pll_revision = DSI_PLL_5NM;
  227. else
  228. return -ENOTSUPP;
  229. rc = of_property_read_u32(pdev->dev.of_node, "cell-index",
  230. &pll_res->index);
  231. if (rc) {
  232. DSI_PLL_ERR(pll_res, "Unable to get the cell-index rc=%d\n", rc);
  233. pll_res->index = 0;
  234. }
  235. pll_res->ssc_en = of_property_read_bool(pdev->dev.of_node,
  236. "qcom,dsi-pll-ssc-en");
  237. if (pll_res->ssc_en) {
  238. DSI_PLL_INFO(pll_res, "PLL SSC enabled\n");
  239. rc = of_property_read_u32(pdev->dev.of_node,
  240. "qcom,ssc-frequency-hz", &pll_res->ssc_freq);
  241. rc = of_property_read_u32(pdev->dev.of_node,
  242. "qcom,ssc-ppm", &pll_res->ssc_ppm);
  243. pll_res->ssc_center = false;
  244. label = of_get_property(pdev->dev.of_node,
  245. "qcom,dsi-pll-ssc-mode", NULL);
  246. if (label && !strcmp(label, "center-spread"))
  247. pll_res->ssc_center = true;
  248. }
  249. pll_res->phy_pll_bypass = of_property_read_bool(pdev->dev.of_node,
  250. "qcom,dsi-phy-pll-bypass");
  251. if (dsi_pll_get_ioresources(pdev, &pll_res->pll_base, "pll_base")) {
  252. DSI_PLL_ERR(pll_res, "Unable to remap pll base resources\n");
  253. return -ENOMEM;
  254. }
  255. pr_info("PLL base=%p\n", pll_res->pll_base);
  256. if (dsi_pll_get_ioresources(pdev, &pll_res->phy_base, "dsi_phy")) {
  257. DSI_PLL_ERR(pll_res, "Unable to remap pll phy base resources\n");
  258. return -ENOMEM;
  259. }
  260. if (dsi_pll_get_ioresources(pdev, &pll_res->dyn_pll_base,
  261. "dyn_refresh_base")) {
  262. DSI_PLL_ERR(pll_res, "Unable to remap dynamic pll base resources\n");
  263. return -ENOMEM;
  264. }
  265. if (dsi_pll_get_ioresources(pdev, &pll_res->gdsc_base, "gdsc_base"))
  266. DSI_PLL_DBG(pll_res, "Unable to remap gdsc base resources\n");
  267. pll_res->in_trusted_vm = of_property_read_bool(pdev->dev.of_node,
  268. "qcom,dsi-pll-in-trusted-vm");
  269. if (pll_res->in_trusted_vm) {
  270. DSI_PLL_INFO(pll_res,
  271. "Bypassing PLL clock register for Trusted VM\n");
  272. return rc;
  273. }
  274. if (pll_res->phy_pll_bypass)
  275. return 0;
  276. rc = dsi_pll_clock_register(pdev, pll_res);
  277. if (rc) {
  278. DSI_PLL_ERR(pll_res, "clock register failed rc=%d\n", rc);
  279. return -EINVAL;
  280. }
  281. return rc;
  282. }
  283. void dsi_pll_parse_dfps_data(struct platform_device *pdev, struct dsi_pll_resource *pll_res)
  284. {
  285. if (!(pll_res->in_trusted_vm)) {
  286. if (dsi_pll_parse_dfps_from_dt(pdev, pll_res))
  287. dsi_pll_parse_dfps(pdev, pll_res);
  288. }
  289. }