cdce949.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * TI CDCE949 clock synthesizer driver
  3. *
  4. * Note: This implementation assumes an input of 27MHz to the CDCE.
  5. * This is by no means constrained by CDCE hardware although the datasheet
  6. * does use this as an example for all illustrations and more importantly:
  7. * that is the crystal input on boards it is currently used on.
  8. *
  9. * Copyright (C) 2009 Texas Instruments Incorporated. http://www.ti.com/
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/clk.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/i2c.h>
  20. #include <mach/clock.h>
  21. #include <mach/cdce949.h>
  22. #include "clock.h"
  23. static struct i2c_client *cdce_i2c_client;
  24. static DEFINE_MUTEX(cdce_mutex);
  25. /* CDCE register descriptor */
  26. struct cdce_reg {
  27. u8 addr;
  28. u8 val;
  29. };
  30. /* Per-Output (Y1, Y2 etc.) frequency descriptor */
  31. struct cdce_freq {
  32. /* Frequency in KHz */
  33. unsigned long frequency;
  34. /*
  35. * List of registers to program to obtain a particular frequency.
  36. * 0x0 in register address and value is the end of list marker.
  37. */
  38. struct cdce_reg *reglist;
  39. };
  40. #define CDCE_FREQ_TABLE_ENTRY(line, out) \
  41. { \
  42. .reglist = cdce_y ##line## _ ##out, \
  43. .frequency = out, \
  44. }
  45. /* List of CDCE outputs */
  46. struct cdce_output {
  47. /* List of frequencies on this output */
  48. struct cdce_freq *freq_table;
  49. /* Number of possible frequencies */
  50. int size;
  51. };
  52. /*
  53. * Finding out the values to program into CDCE949 registers for a particular
  54. * frequency output is not a simple calculation. Have a look at the datasheet
  55. * for the details. There is desktop software available to help users with
  56. * the calculations. Here, we just depend on the output of that software
  57. * (or hand calculations) instead trying to runtime calculate the register
  58. * values and inflicting misery on ourselves.
  59. */
  60. static struct cdce_reg cdce_y1_148500[] = {
  61. { 0x13, 0x00 },
  62. /* program PLL1_0 multiplier */
  63. { 0x18, 0xaf },
  64. { 0x19, 0x50 },
  65. { 0x1a, 0x02 },
  66. { 0x1b, 0xc9 },
  67. /* program PLL1_11 multiplier */
  68. { 0x1c, 0x00 },
  69. { 0x1d, 0x40 },
  70. { 0x1e, 0x02 },
  71. { 0x1f, 0xc9 },
  72. /* output state selection */
  73. { 0x15, 0x00 },
  74. { 0x14, 0xef },
  75. /* switch MUX to PLL1 output */
  76. { 0x14, 0x6f },
  77. { 0x16, 0x06 },
  78. /* set P2DIV divider, P3DIV and input crystal */
  79. { 0x17, 0x06 },
  80. { 0x01, 0x00 },
  81. { 0x05, 0x48 },
  82. { 0x02, 0x80 },
  83. /* enable and disable PLL */
  84. { 0x02, 0xbc },
  85. { 0x03, 0x01 },
  86. { },
  87. };
  88. static struct cdce_reg cdce_y1_74250[] = {
  89. { 0x13, 0x00 },
  90. { 0x18, 0xaf },
  91. { 0x19, 0x50 },
  92. { 0x1a, 0x02 },
  93. { 0x1b, 0xc9 },
  94. { 0x1c, 0x00 },
  95. { 0x1d, 0x40 },
  96. { 0x1e, 0x02 },
  97. { 0x1f, 0xc9 },
  98. /* output state selection */
  99. { 0x15, 0x00 },
  100. { 0x14, 0xef },
  101. /* switch MUX to PLL1 output */
  102. { 0x14, 0x6f },
  103. { 0x16, 0x06 },
  104. /* set P2DIV divider, P3DIV and input crystal */
  105. { 0x17, 0x06 },
  106. { 0x01, 0x00 },
  107. { 0x05, 0x48 },
  108. { 0x02, 0x80 },
  109. /* enable and disable PLL */
  110. { 0x02, 0xbc },
  111. { 0x03, 0x02 },
  112. { },
  113. };
  114. static struct cdce_reg cdce_y1_27000[] = {
  115. { 0x13, 0x00 },
  116. { 0x18, 0x00 },
  117. { 0x19, 0x40 },
  118. { 0x1a, 0x02 },
  119. { 0x1b, 0x08 },
  120. { 0x1c, 0x00 },
  121. { 0x1d, 0x40 },
  122. { 0x1e, 0x02 },
  123. { 0x1f, 0x08 },
  124. { 0x15, 0x02 },
  125. { 0x14, 0xed },
  126. { 0x16, 0x01 },
  127. { 0x17, 0x01 },
  128. { 0x01, 0x00 },
  129. { 0x05, 0x50 },
  130. { 0x02, 0xb4 },
  131. { 0x03, 0x01 },
  132. { },
  133. };
  134. static struct cdce_freq cdce_y1_freqs[] = {
  135. CDCE_FREQ_TABLE_ENTRY(1, 148500),
  136. CDCE_FREQ_TABLE_ENTRY(1, 74250),
  137. CDCE_FREQ_TABLE_ENTRY(1, 27000),
  138. };
  139. static struct cdce_reg cdce_y5_13500[] = {
  140. { 0x27, 0x08 },
  141. { 0x28, 0x00 },
  142. { 0x29, 0x40 },
  143. { 0x2a, 0x02 },
  144. { 0x2b, 0x08 },
  145. { 0x24, 0x6f },
  146. { },
  147. };
  148. static struct cdce_reg cdce_y5_16875[] = {
  149. { 0x27, 0x08 },
  150. { 0x28, 0x9f },
  151. { 0x29, 0xb0 },
  152. { 0x2a, 0x02 },
  153. { 0x2b, 0x89 },
  154. { 0x24, 0x6f },
  155. { },
  156. };
  157. static struct cdce_reg cdce_y5_27000[] = {
  158. { 0x27, 0x04 },
  159. { 0x28, 0x00 },
  160. { 0x29, 0x40 },
  161. { 0x2a, 0x02 },
  162. { 0x2b, 0x08 },
  163. { 0x24, 0x6f },
  164. { },
  165. };
  166. static struct cdce_reg cdce_y5_54000[] = {
  167. { 0x27, 0x04 },
  168. { 0x28, 0xff },
  169. { 0x29, 0x80 },
  170. { 0x2a, 0x02 },
  171. { 0x2b, 0x07 },
  172. { 0x24, 0x6f },
  173. { },
  174. };
  175. static struct cdce_reg cdce_y5_81000[] = {
  176. { 0x27, 0x02 },
  177. { 0x28, 0xbf },
  178. { 0x29, 0xa0 },
  179. { 0x2a, 0x03 },
  180. { 0x2b, 0x0a },
  181. { 0x24, 0x6f },
  182. { },
  183. };
  184. static struct cdce_freq cdce_y5_freqs[] = {
  185. CDCE_FREQ_TABLE_ENTRY(5, 13500),
  186. CDCE_FREQ_TABLE_ENTRY(5, 16875),
  187. CDCE_FREQ_TABLE_ENTRY(5, 27000),
  188. CDCE_FREQ_TABLE_ENTRY(5, 54000),
  189. CDCE_FREQ_TABLE_ENTRY(5, 81000),
  190. };
  191. static struct cdce_output output_list[] = {
  192. [1] = { cdce_y1_freqs, ARRAY_SIZE(cdce_y1_freqs) },
  193. [5] = { cdce_y5_freqs, ARRAY_SIZE(cdce_y5_freqs) },
  194. };
  195. int cdce_set_rate(struct clk *clk, unsigned long rate)
  196. {
  197. int i, ret = 0;
  198. struct cdce_freq *freq_table = output_list[clk->lpsc].freq_table;
  199. struct cdce_reg *regs = NULL;
  200. if (!cdce_i2c_client)
  201. return -ENODEV;
  202. if (!freq_table)
  203. return -EINVAL;
  204. for (i = 0; i < output_list[clk->lpsc].size; i++) {
  205. if (freq_table[i].frequency == rate / 1000) {
  206. regs = freq_table[i].reglist;
  207. break;
  208. }
  209. }
  210. if (!regs)
  211. return -EINVAL;
  212. mutex_lock(&cdce_mutex);
  213. for (i = 0; regs[i].addr; i++) {
  214. ret = i2c_smbus_write_byte_data(cdce_i2c_client,
  215. regs[i].addr | 0x80, regs[i].val);
  216. if (ret)
  217. break;
  218. }
  219. mutex_unlock(&cdce_mutex);
  220. if (!ret)
  221. clk->rate = rate;
  222. return ret;
  223. }
  224. static int cdce_probe(struct i2c_client *client,
  225. const struct i2c_device_id *id)
  226. {
  227. cdce_i2c_client = client;
  228. return 0;
  229. }
  230. static int __devexit cdce_remove(struct i2c_client *client)
  231. {
  232. cdce_i2c_client = NULL;
  233. return 0;
  234. }
  235. static const struct i2c_device_id cdce_id[] = {
  236. {"cdce949", 0},
  237. {},
  238. };
  239. MODULE_DEVICE_TABLE(i2c, cdce_id);
  240. static struct i2c_driver cdce_driver = {
  241. .driver = {
  242. .owner = THIS_MODULE,
  243. .name = "cdce949",
  244. },
  245. .probe = cdce_probe,
  246. .remove = __devexit_p(cdce_remove),
  247. .id_table = cdce_id,
  248. };
  249. static int __init cdce_init(void)
  250. {
  251. return i2c_add_driver(&cdce_driver);
  252. }
  253. subsys_initcall(cdce_init);
  254. static void __exit cdce_exit(void)
  255. {
  256. i2c_del_driver(&cdce_driver);
  257. }
  258. module_exit(cdce_exit);
  259. MODULE_AUTHOR("Texas Instruments");
  260. MODULE_DESCRIPTION("CDCE949 clock synthesizer driver");
  261. MODULE_LICENSE("GPL v2");