cdce949.c 6.1 KB

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