clock-dsi-8610.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/string.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/clk.h>
  19. #include <asm/processor.h>
  20. #include <mach/msm_iomap.h>
  21. #include <mach/clk-provider.h>
  22. #include "clock-dsi-8610.h"
  23. #define DSI_PHY_PHYS 0xFDD00000
  24. #define DSI_PHY_SIZE 0x00100000
  25. #define DSI_CTRL 0x0000
  26. #define DSI_DSIPHY_PLL_CTRL_0 0x0200
  27. #define DSI_DSIPHY_PLL_CTRL_1 0x0204
  28. #define DSI_DSIPHY_PLL_CTRL_2 0x0208
  29. #define DSI_DSIPHY_PLL_CTRL_3 0x020C
  30. #define DSI_DSIPHY_PLL_RDY 0x0280
  31. #define DSI_DSIPHY_PLL_CTRL_8 0x0220
  32. #define DSI_DSIPHY_PLL_CTRL_9 0x0224
  33. #define DSI_DSIPHY_PLL_CTRL_10 0x0228
  34. #define DSI_BPP 3
  35. #define DSI_PLL_RDY_BIT 0x01
  36. #define DSI_PLL_RDY_LOOP_COUNT 80000
  37. #define DSI_MAX_DIVIDER 256
  38. static unsigned char *dsi_base;
  39. static struct clk *dsi_ahb_clk;
  40. int __init dsi_clk_ctrl_init(struct clk *ahb_clk)
  41. {
  42. dsi_base = ioremap(DSI_PHY_PHYS, DSI_PHY_SIZE);
  43. if (!dsi_base) {
  44. pr_err("unable to remap dsi base\n");
  45. return -ENODEV;
  46. }
  47. dsi_ahb_clk = ahb_clk;
  48. return 0;
  49. }
  50. static int dsi_pll_vco_enable(struct clk *c)
  51. {
  52. u32 status;
  53. int i = 0, ret = 0;
  54. ret = clk_enable(dsi_ahb_clk);
  55. if (ret) {
  56. pr_err("fail to enable dsi ahb clk\n");
  57. return ret;
  58. }
  59. writel_relaxed(0x01, dsi_base + DSI_DSIPHY_PLL_CTRL_0);
  60. do {
  61. status = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_RDY);
  62. } while (!(status & DSI_PLL_RDY_BIT) && (i++ < DSI_PLL_RDY_LOOP_COUNT));
  63. if (!(status & DSI_PLL_RDY_BIT)) {
  64. pr_err("DSI PLL not ready, polling time out!\n");
  65. ret = -ETIMEDOUT;
  66. }
  67. clk_disable(dsi_ahb_clk);
  68. return ret;
  69. }
  70. static void dsi_pll_vco_disable(struct clk *c)
  71. {
  72. int ret;
  73. ret = clk_enable(dsi_ahb_clk);
  74. if (ret) {
  75. pr_err("fail to enable dsi ahb clk\n");
  76. return;
  77. }
  78. writel_relaxed(0x00, dsi_base + DSI_DSIPHY_PLL_CTRL_0);
  79. clk_disable(dsi_ahb_clk);
  80. }
  81. static int dsi_pll_vco_set_rate(struct clk *c, unsigned long rate)
  82. {
  83. int ret;
  84. u32 temp, val;
  85. unsigned long fb_divider;
  86. struct clk *parent = c->parent;
  87. struct dsi_pll_vco_clk *vco_clk =
  88. container_of(c, struct dsi_pll_vco_clk, c);
  89. if (!rate)
  90. return 0;
  91. ret = clk_prepare_enable(dsi_ahb_clk);
  92. if (ret) {
  93. pr_err("fail to enable dsi ahb clk\n");
  94. return ret;
  95. }
  96. temp = rate / 10;
  97. val = parent->rate / 10;
  98. fb_divider = (temp * vco_clk->pref_div_ratio) / val;
  99. fb_divider = fb_divider / 2 - 1;
  100. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_1);
  101. val = (temp & 0xFFFFFF00) | (fb_divider & 0xFF);
  102. writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_1);
  103. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_2);
  104. val = (temp & 0xFFFFFFF8) | ((fb_divider >> 8) & 0x07);
  105. writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_2);
  106. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_3);
  107. val = (temp & 0xFFFFFFC0) | (vco_clk->pref_div_ratio - 1);
  108. writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_3);
  109. clk_disable_unprepare(dsi_ahb_clk);
  110. return 0;
  111. }
  112. /* rate is the bit clk rate */
  113. static long dsi_pll_vco_round_rate(struct clk *c, unsigned long rate)
  114. {
  115. long vco_rate;
  116. struct dsi_pll_vco_clk *vco_clk =
  117. container_of(c, struct dsi_pll_vco_clk, c);
  118. vco_rate = rate;
  119. if (rate < vco_clk->vco_clk_min)
  120. vco_rate = vco_clk->vco_clk_min;
  121. else if (rate > vco_clk->vco_clk_max)
  122. vco_rate = vco_clk->vco_clk_max;
  123. return vco_rate;
  124. }
  125. static unsigned long dsi_pll_vco_get_rate(struct clk *c)
  126. {
  127. u32 fb_divider, ref_divider, vco_rate;
  128. u32 temp, status;
  129. struct clk *parent = c->parent;
  130. status = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_RDY);
  131. if (status & DSI_PLL_RDY_BIT) {
  132. fb_divider = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_1);
  133. fb_divider &= 0xFF;
  134. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_2) & 0x07;
  135. fb_divider = (temp << 8) | fb_divider;
  136. fb_divider += 1;
  137. ref_divider = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_3);
  138. ref_divider &= 0x3F;
  139. ref_divider += 1;
  140. vco_rate = (parent->rate / ref_divider) * fb_divider;
  141. } else {
  142. vco_rate = 0;
  143. }
  144. return vco_rate;
  145. }
  146. static enum handoff dsi_pll_vco_handoff(struct clk *c)
  147. {
  148. u32 status;
  149. if (clk_prepare_enable(dsi_ahb_clk)) {
  150. pr_err("fail to enable dsi ahb clk\n");
  151. return HANDOFF_DISABLED_CLK;
  152. }
  153. status = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_0);
  154. if (!status & DSI_PLL_RDY_BIT) {
  155. pr_err("DSI PLL not ready\n");
  156. clk_disable_unprepare(dsi_ahb_clk);
  157. return HANDOFF_DISABLED_CLK;
  158. }
  159. c->rate = dsi_pll_vco_get_rate(c);
  160. clk_disable_unprepare(dsi_ahb_clk);
  161. return HANDOFF_ENABLED_CLK;
  162. }
  163. static int dsi_byteclk_set_rate(struct clk *c, unsigned long rate)
  164. {
  165. int div, ret;
  166. long vco_rate;
  167. unsigned long bitclk_rate;
  168. u32 temp, val;
  169. struct clk *parent = clk_get_parent(c);
  170. if (rate == 0) {
  171. ret = clk_set_rate(parent, 0);
  172. return ret;
  173. }
  174. bitclk_rate = rate * 8;
  175. for (div = 1; div < DSI_MAX_DIVIDER; div++) {
  176. vco_rate = clk_round_rate(parent, bitclk_rate * div);
  177. if (vco_rate == bitclk_rate * div)
  178. break;
  179. if (vco_rate < bitclk_rate * div)
  180. return -EINVAL;
  181. }
  182. if (vco_rate != bitclk_rate * div)
  183. return -EINVAL;
  184. ret = clk_set_rate(parent, vco_rate);
  185. if (ret) {
  186. pr_err("fail to set vco rate\n");
  187. return ret;
  188. }
  189. ret = clk_prepare_enable(dsi_ahb_clk);
  190. if (ret) {
  191. pr_err("fail to enable dsi ahb clk\n");
  192. return ret;
  193. }
  194. /* set the bit clk divider */
  195. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_8);
  196. val = (temp & 0xFFFFFFF0) | (div - 1);
  197. writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_8);
  198. /* set the byte clk divider */
  199. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_9);
  200. val = (temp & 0xFFFFFF00) | (vco_rate / rate - 1);
  201. writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_9);
  202. clk_disable_unprepare(dsi_ahb_clk);
  203. return 0;
  204. }
  205. static long dsi_byteclk_round_rate(struct clk *c, unsigned long rate)
  206. {
  207. int div;
  208. long vco_rate;
  209. unsigned long bitclk_rate;
  210. struct clk *parent = clk_get_parent(c);
  211. if (rate == 0)
  212. return -EINVAL;
  213. bitclk_rate = rate * 8;
  214. for (div = 1; div < DSI_MAX_DIVIDER; div++) {
  215. vco_rate = clk_round_rate(parent, bitclk_rate * div);
  216. if (vco_rate == bitclk_rate * div)
  217. break;
  218. if (vco_rate < bitclk_rate * div)
  219. return -EINVAL;
  220. }
  221. if (vco_rate != bitclk_rate * div)
  222. return -EINVAL;
  223. return rate;
  224. }
  225. static enum handoff dsi_byteclk_handoff(struct clk *c)
  226. {
  227. struct clk *parent = clk_get_parent(c);
  228. unsigned long vco_rate = clk_get_rate(parent);
  229. u32 out_div2;
  230. if (vco_rate == 0)
  231. return HANDOFF_DISABLED_CLK;
  232. if (clk_prepare_enable(dsi_ahb_clk)) {
  233. pr_err("fail to enable dsi ahb clk\n");
  234. return HANDOFF_DISABLED_CLK;
  235. }
  236. out_div2 = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_9);
  237. out_div2 &= 0xFF;
  238. c->rate = vco_rate / (out_div2 + 1);
  239. clk_disable_unprepare(dsi_ahb_clk);
  240. return HANDOFF_ENABLED_CLK;
  241. }
  242. static int dsi_dsiclk_set_rate(struct clk *c, unsigned long rate)
  243. {
  244. u32 temp, val;
  245. int ret;
  246. struct clk *parent = clk_get_parent(c);
  247. unsigned long vco_rate = clk_get_rate(parent);
  248. if (rate == 0)
  249. return 0;
  250. if (vco_rate % rate != 0) {
  251. pr_err("dsiclk_set_rate invalid rate\n");
  252. return -EINVAL;
  253. }
  254. ret = clk_prepare_enable(dsi_ahb_clk);
  255. if (ret) {
  256. pr_err("fail to enable dsi ahb clk\n");
  257. return ret;
  258. }
  259. temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_10);
  260. val = (temp & 0xFFFFFF00) | (vco_rate / rate - 1);
  261. writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_10);
  262. clk_disable_unprepare(dsi_ahb_clk);
  263. return 0;
  264. }
  265. static long dsi_dsiclk_round_rate(struct clk *c, unsigned long rate)
  266. {
  267. /* rate is the pixel clk rate, translate into dsi clk rate*/
  268. struct clk *parent = clk_get_parent(c);
  269. unsigned long vco_rate = clk_get_rate(parent);
  270. rate *= DSI_BPP;
  271. if (vco_rate < rate)
  272. return -EINVAL;
  273. if (vco_rate % rate != 0)
  274. return -EINVAL;
  275. return rate;
  276. }
  277. static enum handoff dsi_dsiclk_handoff(struct clk *c)
  278. {
  279. struct clk *parent = clk_get_parent(c);
  280. unsigned long vco_rate = clk_get_rate(parent);
  281. u32 out_div3;
  282. if (vco_rate == 0)
  283. return HANDOFF_DISABLED_CLK;
  284. if (clk_prepare_enable(dsi_ahb_clk)) {
  285. pr_err("fail to enable dsi ahb clk\n");
  286. return HANDOFF_DISABLED_CLK;
  287. }
  288. out_div3 = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_10);
  289. out_div3 &= 0xFF;
  290. c->rate = vco_rate / (out_div3 + 1);
  291. clk_disable_unprepare(dsi_ahb_clk);
  292. return HANDOFF_ENABLED_CLK;
  293. }
  294. struct clk_ops clk_ops_dsi_dsiclk = {
  295. .set_rate = dsi_dsiclk_set_rate,
  296. .round_rate = dsi_dsiclk_round_rate,
  297. .handoff = dsi_dsiclk_handoff,
  298. };
  299. struct clk_ops clk_ops_dsi_byteclk = {
  300. .set_rate = dsi_byteclk_set_rate,
  301. .round_rate = dsi_byteclk_round_rate,
  302. .handoff = dsi_byteclk_handoff,
  303. };
  304. struct clk_ops clk_ops_dsi_vco = {
  305. .enable = dsi_pll_vco_enable,
  306. .disable = dsi_pll_vco_disable,
  307. .set_rate = dsi_pll_vco_set_rate,
  308. .round_rate = dsi_pll_vco_round_rate,
  309. .handoff = dsi_pll_vco_handoff,
  310. };