dsi_pll.c 8.5 KB

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