clk-si5351.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /*
  2. * clk-si5351.c: Silicon Laboratories Si5351A/B/C I2C Clock Generator
  3. *
  4. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  5. * Rabeeh Khoury <rabeeh@solid-run.com>
  6. *
  7. * References:
  8. * [1] "Si5351A/B/C Data Sheet"
  9. * http://www.silabs.com/Support%20Documents/TechnicalDocs/Si5351.pdf
  10. * [2] "Manually Generating an Si5351 Register Map"
  11. * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN619.pdf
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/clk.h>
  21. #include <linux/clk-provider.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/errno.h>
  25. #include <linux/rational.h>
  26. #include <linux/i2c.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/platform_data/si5351.h>
  29. #include <linux/regmap.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include <asm/div64.h>
  33. #include "clk-si5351.h"
  34. struct si5351_driver_data;
  35. struct si5351_parameters {
  36. unsigned long p1;
  37. unsigned long p2;
  38. unsigned long p3;
  39. int valid;
  40. };
  41. struct si5351_hw_data {
  42. struct clk_hw hw;
  43. struct si5351_driver_data *drvdata;
  44. struct si5351_parameters params;
  45. unsigned char num;
  46. };
  47. struct si5351_driver_data {
  48. enum si5351_variant variant;
  49. struct i2c_client *client;
  50. struct regmap *regmap;
  51. struct clk *pxtal;
  52. const char *pxtal_name;
  53. struct clk_hw xtal;
  54. struct clk *pclkin;
  55. const char *pclkin_name;
  56. struct clk_hw clkin;
  57. struct si5351_hw_data pll[2];
  58. struct si5351_hw_data *msynth;
  59. struct si5351_hw_data *clkout;
  60. size_t num_clkout;
  61. };
  62. static const char * const si5351_input_names[] = {
  63. "xtal", "clkin"
  64. };
  65. static const char * const si5351_pll_names[] = {
  66. "si5351_plla", "si5351_pllb", "si5351_vxco"
  67. };
  68. static const char * const si5351_msynth_names[] = {
  69. "ms0", "ms1", "ms2", "ms3", "ms4", "ms5", "ms6", "ms7"
  70. };
  71. static const char * const si5351_clkout_names[] = {
  72. "clk0", "clk1", "clk2", "clk3", "clk4", "clk5", "clk6", "clk7"
  73. };
  74. /*
  75. * Si5351 i2c regmap
  76. */
  77. static inline u8 si5351_reg_read(struct si5351_driver_data *drvdata, u8 reg)
  78. {
  79. u32 val;
  80. int ret;
  81. ret = regmap_read(drvdata->regmap, reg, &val);
  82. if (ret) {
  83. dev_err(&drvdata->client->dev,
  84. "unable to read from reg%02x\n", reg);
  85. return 0;
  86. }
  87. return (u8)val;
  88. }
  89. static inline int si5351_bulk_read(struct si5351_driver_data *drvdata,
  90. u8 reg, u8 count, u8 *buf)
  91. {
  92. return regmap_bulk_read(drvdata->regmap, reg, buf, count);
  93. }
  94. static inline int si5351_reg_write(struct si5351_driver_data *drvdata,
  95. u8 reg, u8 val)
  96. {
  97. return regmap_write(drvdata->regmap, reg, val);
  98. }
  99. static inline int si5351_bulk_write(struct si5351_driver_data *drvdata,
  100. u8 reg, u8 count, const u8 *buf)
  101. {
  102. return regmap_raw_write(drvdata->regmap, reg, buf, count);
  103. }
  104. static inline int si5351_set_bits(struct si5351_driver_data *drvdata,
  105. u8 reg, u8 mask, u8 val)
  106. {
  107. return regmap_update_bits(drvdata->regmap, reg, mask, val);
  108. }
  109. static inline u8 si5351_msynth_params_address(int num)
  110. {
  111. if (num > 5)
  112. return SI5351_CLK6_PARAMETERS + (num - 6);
  113. return SI5351_CLK0_PARAMETERS + (SI5351_PARAMETERS_LENGTH * num);
  114. }
  115. static void si5351_read_parameters(struct si5351_driver_data *drvdata,
  116. u8 reg, struct si5351_parameters *params)
  117. {
  118. u8 buf[SI5351_PARAMETERS_LENGTH];
  119. switch (reg) {
  120. case SI5351_CLK6_PARAMETERS:
  121. case SI5351_CLK7_PARAMETERS:
  122. buf[0] = si5351_reg_read(drvdata, reg);
  123. params->p1 = buf[0];
  124. params->p2 = 0;
  125. params->p3 = 1;
  126. break;
  127. default:
  128. si5351_bulk_read(drvdata, reg, SI5351_PARAMETERS_LENGTH, buf);
  129. params->p1 = ((buf[2] & 0x03) << 16) | (buf[3] << 8) | buf[4];
  130. params->p2 = ((buf[5] & 0x0f) << 16) | (buf[6] << 8) | buf[7];
  131. params->p3 = ((buf[5] & 0xf0) << 12) | (buf[0] << 8) | buf[1];
  132. }
  133. params->valid = 1;
  134. }
  135. static void si5351_write_parameters(struct si5351_driver_data *drvdata,
  136. u8 reg, struct si5351_parameters *params)
  137. {
  138. u8 buf[SI5351_PARAMETERS_LENGTH];
  139. switch (reg) {
  140. case SI5351_CLK6_PARAMETERS:
  141. case SI5351_CLK7_PARAMETERS:
  142. buf[0] = params->p1 & 0xff;
  143. si5351_reg_write(drvdata, reg, buf[0]);
  144. break;
  145. default:
  146. buf[0] = ((params->p3 & 0x0ff00) >> 8) & 0xff;
  147. buf[1] = params->p3 & 0xff;
  148. /* save rdiv and divby4 */
  149. buf[2] = si5351_reg_read(drvdata, reg + 2) & ~0x03;
  150. buf[2] |= ((params->p1 & 0x30000) >> 16) & 0x03;
  151. buf[3] = ((params->p1 & 0x0ff00) >> 8) & 0xff;
  152. buf[4] = params->p1 & 0xff;
  153. buf[5] = ((params->p3 & 0xf0000) >> 12) |
  154. ((params->p2 & 0xf0000) >> 16);
  155. buf[6] = ((params->p2 & 0x0ff00) >> 8) & 0xff;
  156. buf[7] = params->p2 & 0xff;
  157. si5351_bulk_write(drvdata, reg, SI5351_PARAMETERS_LENGTH, buf);
  158. }
  159. }
  160. static bool si5351_regmap_is_volatile(struct device *dev, unsigned int reg)
  161. {
  162. switch (reg) {
  163. case SI5351_DEVICE_STATUS:
  164. case SI5351_INTERRUPT_STATUS:
  165. case SI5351_PLL_RESET:
  166. return true;
  167. }
  168. return false;
  169. }
  170. static bool si5351_regmap_is_writeable(struct device *dev, unsigned int reg)
  171. {
  172. /* reserved registers */
  173. if (reg >= 4 && reg <= 8)
  174. return false;
  175. if (reg >= 10 && reg <= 14)
  176. return false;
  177. if (reg >= 173 && reg <= 176)
  178. return false;
  179. if (reg >= 178 && reg <= 182)
  180. return false;
  181. /* read-only */
  182. if (reg == SI5351_DEVICE_STATUS)
  183. return false;
  184. return true;
  185. }
  186. static const struct regmap_config si5351_regmap_config = {
  187. .reg_bits = 8,
  188. .val_bits = 8,
  189. .cache_type = REGCACHE_RBTREE,
  190. .max_register = 187,
  191. .writeable_reg = si5351_regmap_is_writeable,
  192. .volatile_reg = si5351_regmap_is_volatile,
  193. };
  194. /*
  195. * Si5351 xtal clock input
  196. */
  197. static int si5351_xtal_prepare(struct clk_hw *hw)
  198. {
  199. struct si5351_driver_data *drvdata =
  200. container_of(hw, struct si5351_driver_data, xtal);
  201. si5351_set_bits(drvdata, SI5351_FANOUT_ENABLE,
  202. SI5351_XTAL_ENABLE, SI5351_XTAL_ENABLE);
  203. return 0;
  204. }
  205. static void si5351_xtal_unprepare(struct clk_hw *hw)
  206. {
  207. struct si5351_driver_data *drvdata =
  208. container_of(hw, struct si5351_driver_data, xtal);
  209. si5351_set_bits(drvdata, SI5351_FANOUT_ENABLE,
  210. SI5351_XTAL_ENABLE, 0);
  211. }
  212. static const struct clk_ops si5351_xtal_ops = {
  213. .prepare = si5351_xtal_prepare,
  214. .unprepare = si5351_xtal_unprepare,
  215. };
  216. /*
  217. * Si5351 clkin clock input (Si5351C only)
  218. */
  219. static int si5351_clkin_prepare(struct clk_hw *hw)
  220. {
  221. struct si5351_driver_data *drvdata =
  222. container_of(hw, struct si5351_driver_data, clkin);
  223. si5351_set_bits(drvdata, SI5351_FANOUT_ENABLE,
  224. SI5351_CLKIN_ENABLE, SI5351_CLKIN_ENABLE);
  225. return 0;
  226. }
  227. static void si5351_clkin_unprepare(struct clk_hw *hw)
  228. {
  229. struct si5351_driver_data *drvdata =
  230. container_of(hw, struct si5351_driver_data, clkin);
  231. si5351_set_bits(drvdata, SI5351_FANOUT_ENABLE,
  232. SI5351_CLKIN_ENABLE, 0);
  233. }
  234. /*
  235. * CMOS clock source constraints:
  236. * The input frequency range of the PLL is 10Mhz to 40MHz.
  237. * If CLKIN is >40MHz, the input divider must be used.
  238. */
  239. static unsigned long si5351_clkin_recalc_rate(struct clk_hw *hw,
  240. unsigned long parent_rate)
  241. {
  242. struct si5351_driver_data *drvdata =
  243. container_of(hw, struct si5351_driver_data, clkin);
  244. unsigned long rate;
  245. unsigned char idiv;
  246. rate = parent_rate;
  247. if (parent_rate > 160000000) {
  248. idiv = SI5351_CLKIN_DIV_8;
  249. rate /= 8;
  250. } else if (parent_rate > 80000000) {
  251. idiv = SI5351_CLKIN_DIV_4;
  252. rate /= 4;
  253. } else if (parent_rate > 40000000) {
  254. idiv = SI5351_CLKIN_DIV_2;
  255. rate /= 2;
  256. } else {
  257. idiv = SI5351_CLKIN_DIV_1;
  258. }
  259. si5351_set_bits(drvdata, SI5351_PLL_INPUT_SOURCE,
  260. SI5351_CLKIN_DIV_MASK, idiv);
  261. dev_dbg(&drvdata->client->dev, "%s - clkin div = %d, rate = %lu\n",
  262. __func__, (1 << (idiv >> 6)), rate);
  263. return rate;
  264. }
  265. static const struct clk_ops si5351_clkin_ops = {
  266. .prepare = si5351_clkin_prepare,
  267. .unprepare = si5351_clkin_unprepare,
  268. .recalc_rate = si5351_clkin_recalc_rate,
  269. };
  270. /*
  271. * Si5351 vxco clock input (Si5351B only)
  272. */
  273. static int si5351_vxco_prepare(struct clk_hw *hw)
  274. {
  275. struct si5351_hw_data *hwdata =
  276. container_of(hw, struct si5351_hw_data, hw);
  277. dev_warn(&hwdata->drvdata->client->dev, "VXCO currently unsupported\n");
  278. return 0;
  279. }
  280. static void si5351_vxco_unprepare(struct clk_hw *hw)
  281. {
  282. }
  283. static unsigned long si5351_vxco_recalc_rate(struct clk_hw *hw,
  284. unsigned long parent_rate)
  285. {
  286. return 0;
  287. }
  288. static int si5351_vxco_set_rate(struct clk_hw *hw, unsigned long rate,
  289. unsigned long parent)
  290. {
  291. return 0;
  292. }
  293. static const struct clk_ops si5351_vxco_ops = {
  294. .prepare = si5351_vxco_prepare,
  295. .unprepare = si5351_vxco_unprepare,
  296. .recalc_rate = si5351_vxco_recalc_rate,
  297. .set_rate = si5351_vxco_set_rate,
  298. };
  299. /*
  300. * Si5351 pll a/b
  301. *
  302. * Feedback Multisynth Divider Equations [2]
  303. *
  304. * fVCO = fIN * (a + b/c)
  305. *
  306. * with 15 + 0/1048575 <= (a + b/c) <= 90 + 0/1048575 and
  307. * fIN = fXTAL or fIN = fCLKIN/CLKIN_DIV
  308. *
  309. * Feedback Multisynth Register Equations
  310. *
  311. * (1) MSNx_P1[17:0] = 128 * a + floor(128 * b/c) - 512
  312. * (2) MSNx_P2[19:0] = 128 * b - c * floor(128 * b/c) = (128*b) mod c
  313. * (3) MSNx_P3[19:0] = c
  314. *
  315. * Transposing (2) yields: (4) floor(128 * b/c) = (128 * b / MSNx_P2)/c
  316. *
  317. * Using (4) on (1) yields:
  318. * MSNx_P1 = 128 * a + (128 * b/MSNx_P2)/c - 512
  319. * MSNx_P1 + 512 + MSNx_P2/c = 128 * a + 128 * b/c
  320. *
  321. * a + b/c = (MSNx_P1 + MSNx_P2/MSNx_P3 + 512)/128
  322. * = (MSNx_P1*MSNx_P3 + MSNx_P2 + 512*MSNx_P3)/(128*MSNx_P3)
  323. *
  324. */
  325. static int _si5351_pll_reparent(struct si5351_driver_data *drvdata,
  326. int num, enum si5351_pll_src parent)
  327. {
  328. u8 mask = (num == 0) ? SI5351_PLLA_SOURCE : SI5351_PLLB_SOURCE;
  329. if (parent == SI5351_PLL_SRC_DEFAULT)
  330. return 0;
  331. if (num > 2)
  332. return -EINVAL;
  333. if (drvdata->variant != SI5351_VARIANT_C &&
  334. parent != SI5351_PLL_SRC_XTAL)
  335. return -EINVAL;
  336. si5351_set_bits(drvdata, SI5351_PLL_INPUT_SOURCE, mask,
  337. (parent == SI5351_PLL_SRC_XTAL) ? 0 : mask);
  338. return 0;
  339. }
  340. static unsigned char si5351_pll_get_parent(struct clk_hw *hw)
  341. {
  342. struct si5351_hw_data *hwdata =
  343. container_of(hw, struct si5351_hw_data, hw);
  344. u8 mask = (hwdata->num == 0) ? SI5351_PLLA_SOURCE : SI5351_PLLB_SOURCE;
  345. u8 val;
  346. val = si5351_reg_read(hwdata->drvdata, SI5351_PLL_INPUT_SOURCE);
  347. return (val & mask) ? 1 : 0;
  348. }
  349. static int si5351_pll_set_parent(struct clk_hw *hw, u8 index)
  350. {
  351. struct si5351_hw_data *hwdata =
  352. container_of(hw, struct si5351_hw_data, hw);
  353. if (hwdata->drvdata->variant != SI5351_VARIANT_C &&
  354. index > 0)
  355. return -EPERM;
  356. if (index > 1)
  357. return -EINVAL;
  358. return _si5351_pll_reparent(hwdata->drvdata, hwdata->num,
  359. (index == 0) ? SI5351_PLL_SRC_XTAL :
  360. SI5351_PLL_SRC_CLKIN);
  361. }
  362. static unsigned long si5351_pll_recalc_rate(struct clk_hw *hw,
  363. unsigned long parent_rate)
  364. {
  365. struct si5351_hw_data *hwdata =
  366. container_of(hw, struct si5351_hw_data, hw);
  367. u8 reg = (hwdata->num == 0) ? SI5351_PLLA_PARAMETERS :
  368. SI5351_PLLB_PARAMETERS;
  369. unsigned long long rate;
  370. if (!hwdata->params.valid)
  371. si5351_read_parameters(hwdata->drvdata, reg, &hwdata->params);
  372. if (hwdata->params.p3 == 0)
  373. return parent_rate;
  374. /* fVCO = fIN * (P1*P3 + 512*P3 + P2)/(128*P3) */
  375. rate = hwdata->params.p1 * hwdata->params.p3;
  376. rate += 512 * hwdata->params.p3;
  377. rate += hwdata->params.p2;
  378. rate *= parent_rate;
  379. do_div(rate, 128 * hwdata->params.p3);
  380. dev_dbg(&hwdata->drvdata->client->dev,
  381. "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, parent_rate = %lu, rate = %lu\n",
  382. __func__, clk_hw_get_name(hw),
  383. hwdata->params.p1, hwdata->params.p2, hwdata->params.p3,
  384. parent_rate, (unsigned long)rate);
  385. return (unsigned long)rate;
  386. }
  387. static long si5351_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  388. unsigned long *parent_rate)
  389. {
  390. struct si5351_hw_data *hwdata =
  391. container_of(hw, struct si5351_hw_data, hw);
  392. unsigned long rfrac, denom, a, b, c;
  393. unsigned long long lltmp;
  394. if (rate < SI5351_PLL_VCO_MIN)
  395. rate = SI5351_PLL_VCO_MIN;
  396. if (rate > SI5351_PLL_VCO_MAX)
  397. rate = SI5351_PLL_VCO_MAX;
  398. /* determine integer part of feedback equation */
  399. a = rate / *parent_rate;
  400. if (a < SI5351_PLL_A_MIN)
  401. rate = *parent_rate * SI5351_PLL_A_MIN;
  402. if (a > SI5351_PLL_A_MAX)
  403. rate = *parent_rate * SI5351_PLL_A_MAX;
  404. /* find best approximation for b/c = fVCO mod fIN */
  405. denom = 1000 * 1000;
  406. lltmp = rate % (*parent_rate);
  407. lltmp *= denom;
  408. do_div(lltmp, *parent_rate);
  409. rfrac = (unsigned long)lltmp;
  410. b = 0;
  411. c = 1;
  412. if (rfrac)
  413. rational_best_approximation(rfrac, denom,
  414. SI5351_PLL_B_MAX, SI5351_PLL_C_MAX, &b, &c);
  415. /* calculate parameters */
  416. hwdata->params.p3 = c;
  417. hwdata->params.p2 = (128 * b) % c;
  418. hwdata->params.p1 = 128 * a;
  419. hwdata->params.p1 += (128 * b / c);
  420. hwdata->params.p1 -= 512;
  421. /* recalculate rate by fIN * (a + b/c) */
  422. lltmp = *parent_rate;
  423. lltmp *= b;
  424. do_div(lltmp, c);
  425. rate = (unsigned long)lltmp;
  426. rate += *parent_rate * a;
  427. dev_dbg(&hwdata->drvdata->client->dev,
  428. "%s - %s: a = %lu, b = %lu, c = %lu, parent_rate = %lu, rate = %lu\n",
  429. __func__, clk_hw_get_name(hw), a, b, c,
  430. *parent_rate, rate);
  431. return rate;
  432. }
  433. static int si5351_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  434. unsigned long parent_rate)
  435. {
  436. struct si5351_hw_data *hwdata =
  437. container_of(hw, struct si5351_hw_data, hw);
  438. u8 reg = (hwdata->num == 0) ? SI5351_PLLA_PARAMETERS :
  439. SI5351_PLLB_PARAMETERS;
  440. /* write multisynth parameters */
  441. si5351_write_parameters(hwdata->drvdata, reg, &hwdata->params);
  442. /* plla/pllb ctrl is in clk6/clk7 ctrl registers */
  443. si5351_set_bits(hwdata->drvdata, SI5351_CLK6_CTRL + hwdata->num,
  444. SI5351_CLK_INTEGER_MODE,
  445. (hwdata->params.p2 == 0) ? SI5351_CLK_INTEGER_MODE : 0);
  446. dev_dbg(&hwdata->drvdata->client->dev,
  447. "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, parent_rate = %lu, rate = %lu\n",
  448. __func__, clk_hw_get_name(hw),
  449. hwdata->params.p1, hwdata->params.p2, hwdata->params.p3,
  450. parent_rate, rate);
  451. return 0;
  452. }
  453. static const struct clk_ops si5351_pll_ops = {
  454. .set_parent = si5351_pll_set_parent,
  455. .get_parent = si5351_pll_get_parent,
  456. .recalc_rate = si5351_pll_recalc_rate,
  457. .round_rate = si5351_pll_round_rate,
  458. .set_rate = si5351_pll_set_rate,
  459. };
  460. /*
  461. * Si5351 multisync divider
  462. *
  463. * for fOUT <= 150 MHz:
  464. *
  465. * fOUT = (fIN * (a + b/c)) / CLKOUTDIV
  466. *
  467. * with 6 + 0/1048575 <= (a + b/c) <= 1800 + 0/1048575 and
  468. * fIN = fVCO0, fVCO1
  469. *
  470. * Output Clock Multisynth Register Equations
  471. *
  472. * MSx_P1[17:0] = 128 * a + floor(128 * b/c) - 512
  473. * MSx_P2[19:0] = 128 * b - c * floor(128 * b/c) = (128*b) mod c
  474. * MSx_P3[19:0] = c
  475. *
  476. * MS[6,7] are integer (P1) divide only, P1 = divide value,
  477. * P2 and P3 are not applicable
  478. *
  479. * for 150MHz < fOUT <= 160MHz:
  480. *
  481. * MSx_P1 = 0, MSx_P2 = 0, MSx_P3 = 1, MSx_INT = 1, MSx_DIVBY4 = 11b
  482. */
  483. static int _si5351_msynth_reparent(struct si5351_driver_data *drvdata,
  484. int num, enum si5351_multisynth_src parent)
  485. {
  486. if (parent == SI5351_MULTISYNTH_SRC_DEFAULT)
  487. return 0;
  488. if (num > 8)
  489. return -EINVAL;
  490. si5351_set_bits(drvdata, SI5351_CLK0_CTRL + num, SI5351_CLK_PLL_SELECT,
  491. (parent == SI5351_MULTISYNTH_SRC_VCO0) ? 0 :
  492. SI5351_CLK_PLL_SELECT);
  493. return 0;
  494. }
  495. static unsigned char si5351_msynth_get_parent(struct clk_hw *hw)
  496. {
  497. struct si5351_hw_data *hwdata =
  498. container_of(hw, struct si5351_hw_data, hw);
  499. u8 val;
  500. val = si5351_reg_read(hwdata->drvdata, SI5351_CLK0_CTRL + hwdata->num);
  501. return (val & SI5351_CLK_PLL_SELECT) ? 1 : 0;
  502. }
  503. static int si5351_msynth_set_parent(struct clk_hw *hw, u8 index)
  504. {
  505. struct si5351_hw_data *hwdata =
  506. container_of(hw, struct si5351_hw_data, hw);
  507. return _si5351_msynth_reparent(hwdata->drvdata, hwdata->num,
  508. (index == 0) ? SI5351_MULTISYNTH_SRC_VCO0 :
  509. SI5351_MULTISYNTH_SRC_VCO1);
  510. }
  511. static unsigned long si5351_msynth_recalc_rate(struct clk_hw *hw,
  512. unsigned long parent_rate)
  513. {
  514. struct si5351_hw_data *hwdata =
  515. container_of(hw, struct si5351_hw_data, hw);
  516. u8 reg = si5351_msynth_params_address(hwdata->num);
  517. unsigned long long rate;
  518. unsigned long m;
  519. if (!hwdata->params.valid)
  520. si5351_read_parameters(hwdata->drvdata, reg, &hwdata->params);
  521. /*
  522. * multisync0-5: fOUT = (128 * P3 * fIN) / (P1*P3 + P2 + 512*P3)
  523. * multisync6-7: fOUT = fIN / P1
  524. */
  525. rate = parent_rate;
  526. if (hwdata->num > 5) {
  527. m = hwdata->params.p1;
  528. } else if (hwdata->params.p3 == 0) {
  529. return parent_rate;
  530. } else if ((si5351_reg_read(hwdata->drvdata, reg + 2) &
  531. SI5351_OUTPUT_CLK_DIVBY4) == SI5351_OUTPUT_CLK_DIVBY4) {
  532. m = 4;
  533. } else {
  534. rate *= 128 * hwdata->params.p3;
  535. m = hwdata->params.p1 * hwdata->params.p3;
  536. m += hwdata->params.p2;
  537. m += 512 * hwdata->params.p3;
  538. }
  539. if (m == 0)
  540. return 0;
  541. do_div(rate, m);
  542. dev_dbg(&hwdata->drvdata->client->dev,
  543. "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, m = %lu, parent_rate = %lu, rate = %lu\n",
  544. __func__, clk_hw_get_name(hw),
  545. hwdata->params.p1, hwdata->params.p2, hwdata->params.p3,
  546. m, parent_rate, (unsigned long)rate);
  547. return (unsigned long)rate;
  548. }
  549. static long si5351_msynth_round_rate(struct clk_hw *hw, unsigned long rate,
  550. unsigned long *parent_rate)
  551. {
  552. struct si5351_hw_data *hwdata =
  553. container_of(hw, struct si5351_hw_data, hw);
  554. unsigned long long lltmp;
  555. unsigned long a, b, c;
  556. int divby4;
  557. /* multisync6-7 can only handle freqencies < 150MHz */
  558. if (hwdata->num >= 6 && rate > SI5351_MULTISYNTH67_MAX_FREQ)
  559. rate = SI5351_MULTISYNTH67_MAX_FREQ;
  560. /* multisync frequency is 1MHz .. 160MHz */
  561. if (rate > SI5351_MULTISYNTH_MAX_FREQ)
  562. rate = SI5351_MULTISYNTH_MAX_FREQ;
  563. if (rate < SI5351_MULTISYNTH_MIN_FREQ)
  564. rate = SI5351_MULTISYNTH_MIN_FREQ;
  565. divby4 = 0;
  566. if (rate > SI5351_MULTISYNTH_DIVBY4_FREQ)
  567. divby4 = 1;
  568. /* multisync can set pll */
  569. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  570. /*
  571. * find largest integer divider for max
  572. * vco frequency and given target rate
  573. */
  574. if (divby4 == 0) {
  575. lltmp = SI5351_PLL_VCO_MAX;
  576. do_div(lltmp, rate);
  577. a = (unsigned long)lltmp;
  578. } else
  579. a = 4;
  580. b = 0;
  581. c = 1;
  582. *parent_rate = a * rate;
  583. } else if (hwdata->num >= 6) {
  584. /* determine the closest integer divider */
  585. a = DIV_ROUND_CLOSEST(*parent_rate, rate);
  586. if (a < SI5351_MULTISYNTH_A_MIN)
  587. a = SI5351_MULTISYNTH_A_MIN;
  588. if (a > SI5351_MULTISYNTH67_A_MAX)
  589. a = SI5351_MULTISYNTH67_A_MAX;
  590. b = 0;
  591. c = 1;
  592. } else {
  593. unsigned long rfrac, denom;
  594. /* disable divby4 */
  595. if (divby4) {
  596. rate = SI5351_MULTISYNTH_DIVBY4_FREQ;
  597. divby4 = 0;
  598. }
  599. /* determine integer part of divider equation */
  600. a = *parent_rate / rate;
  601. if (a < SI5351_MULTISYNTH_A_MIN)
  602. a = SI5351_MULTISYNTH_A_MIN;
  603. if (a > SI5351_MULTISYNTH_A_MAX)
  604. a = SI5351_MULTISYNTH_A_MAX;
  605. /* find best approximation for b/c = fVCO mod fOUT */
  606. denom = 1000 * 1000;
  607. lltmp = (*parent_rate) % rate;
  608. lltmp *= denom;
  609. do_div(lltmp, rate);
  610. rfrac = (unsigned long)lltmp;
  611. b = 0;
  612. c = 1;
  613. if (rfrac)
  614. rational_best_approximation(rfrac, denom,
  615. SI5351_MULTISYNTH_B_MAX, SI5351_MULTISYNTH_C_MAX,
  616. &b, &c);
  617. }
  618. /* recalculate rate by fOUT = fIN / (a + b/c) */
  619. lltmp = *parent_rate;
  620. lltmp *= c;
  621. do_div(lltmp, a * c + b);
  622. rate = (unsigned long)lltmp;
  623. /* calculate parameters */
  624. if (divby4) {
  625. hwdata->params.p3 = 1;
  626. hwdata->params.p2 = 0;
  627. hwdata->params.p1 = 0;
  628. } else if (hwdata->num >= 6) {
  629. hwdata->params.p3 = 0;
  630. hwdata->params.p2 = 0;
  631. hwdata->params.p1 = a;
  632. } else {
  633. hwdata->params.p3 = c;
  634. hwdata->params.p2 = (128 * b) % c;
  635. hwdata->params.p1 = 128 * a;
  636. hwdata->params.p1 += (128 * b / c);
  637. hwdata->params.p1 -= 512;
  638. }
  639. dev_dbg(&hwdata->drvdata->client->dev,
  640. "%s - %s: a = %lu, b = %lu, c = %lu, divby4 = %d, parent_rate = %lu, rate = %lu\n",
  641. __func__, clk_hw_get_name(hw), a, b, c, divby4,
  642. *parent_rate, rate);
  643. return rate;
  644. }
  645. static int si5351_msynth_set_rate(struct clk_hw *hw, unsigned long rate,
  646. unsigned long parent_rate)
  647. {
  648. struct si5351_hw_data *hwdata =
  649. container_of(hw, struct si5351_hw_data, hw);
  650. u8 reg = si5351_msynth_params_address(hwdata->num);
  651. int divby4 = 0;
  652. /* write multisynth parameters */
  653. si5351_write_parameters(hwdata->drvdata, reg, &hwdata->params);
  654. if (rate > SI5351_MULTISYNTH_DIVBY4_FREQ)
  655. divby4 = 1;
  656. /* enable/disable integer mode and divby4 on multisynth0-5 */
  657. if (hwdata->num < 6) {
  658. si5351_set_bits(hwdata->drvdata, reg + 2,
  659. SI5351_OUTPUT_CLK_DIVBY4,
  660. (divby4) ? SI5351_OUTPUT_CLK_DIVBY4 : 0);
  661. si5351_set_bits(hwdata->drvdata, SI5351_CLK0_CTRL + hwdata->num,
  662. SI5351_CLK_INTEGER_MODE,
  663. (hwdata->params.p2 == 0) ? SI5351_CLK_INTEGER_MODE : 0);
  664. }
  665. dev_dbg(&hwdata->drvdata->client->dev,
  666. "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, divby4 = %d, parent_rate = %lu, rate = %lu\n",
  667. __func__, clk_hw_get_name(hw),
  668. hwdata->params.p1, hwdata->params.p2, hwdata->params.p3,
  669. divby4, parent_rate, rate);
  670. return 0;
  671. }
  672. static const struct clk_ops si5351_msynth_ops = {
  673. .set_parent = si5351_msynth_set_parent,
  674. .get_parent = si5351_msynth_get_parent,
  675. .recalc_rate = si5351_msynth_recalc_rate,
  676. .round_rate = si5351_msynth_round_rate,
  677. .set_rate = si5351_msynth_set_rate,
  678. };
  679. /*
  680. * Si5351 clkout divider
  681. */
  682. static int _si5351_clkout_reparent(struct si5351_driver_data *drvdata,
  683. int num, enum si5351_clkout_src parent)
  684. {
  685. u8 val;
  686. if (num > 8)
  687. return -EINVAL;
  688. switch (parent) {
  689. case SI5351_CLKOUT_SRC_MSYNTH_N:
  690. val = SI5351_CLK_INPUT_MULTISYNTH_N;
  691. break;
  692. case SI5351_CLKOUT_SRC_MSYNTH_0_4:
  693. /* clk0/clk4 can only connect to its own multisync */
  694. if (num == 0 || num == 4)
  695. val = SI5351_CLK_INPUT_MULTISYNTH_N;
  696. else
  697. val = SI5351_CLK_INPUT_MULTISYNTH_0_4;
  698. break;
  699. case SI5351_CLKOUT_SRC_XTAL:
  700. val = SI5351_CLK_INPUT_XTAL;
  701. break;
  702. case SI5351_CLKOUT_SRC_CLKIN:
  703. if (drvdata->variant != SI5351_VARIANT_C)
  704. return -EINVAL;
  705. val = SI5351_CLK_INPUT_CLKIN;
  706. break;
  707. default:
  708. return 0;
  709. }
  710. si5351_set_bits(drvdata, SI5351_CLK0_CTRL + num,
  711. SI5351_CLK_INPUT_MASK, val);
  712. return 0;
  713. }
  714. static int _si5351_clkout_set_drive_strength(
  715. struct si5351_driver_data *drvdata, int num,
  716. enum si5351_drive_strength drive)
  717. {
  718. u8 mask;
  719. if (num > 8)
  720. return -EINVAL;
  721. switch (drive) {
  722. case SI5351_DRIVE_2MA:
  723. mask = SI5351_CLK_DRIVE_STRENGTH_2MA;
  724. break;
  725. case SI5351_DRIVE_4MA:
  726. mask = SI5351_CLK_DRIVE_STRENGTH_4MA;
  727. break;
  728. case SI5351_DRIVE_6MA:
  729. mask = SI5351_CLK_DRIVE_STRENGTH_6MA;
  730. break;
  731. case SI5351_DRIVE_8MA:
  732. mask = SI5351_CLK_DRIVE_STRENGTH_8MA;
  733. break;
  734. default:
  735. return 0;
  736. }
  737. si5351_set_bits(drvdata, SI5351_CLK0_CTRL + num,
  738. SI5351_CLK_DRIVE_STRENGTH_MASK, mask);
  739. return 0;
  740. }
  741. static int _si5351_clkout_set_disable_state(
  742. struct si5351_driver_data *drvdata, int num,
  743. enum si5351_disable_state state)
  744. {
  745. u8 reg = (num < 4) ? SI5351_CLK3_0_DISABLE_STATE :
  746. SI5351_CLK7_4_DISABLE_STATE;
  747. u8 shift = (num < 4) ? (2 * num) : (2 * (num-4));
  748. u8 mask = SI5351_CLK_DISABLE_STATE_MASK << shift;
  749. u8 val;
  750. if (num > 8)
  751. return -EINVAL;
  752. switch (state) {
  753. case SI5351_DISABLE_LOW:
  754. val = SI5351_CLK_DISABLE_STATE_LOW;
  755. break;
  756. case SI5351_DISABLE_HIGH:
  757. val = SI5351_CLK_DISABLE_STATE_HIGH;
  758. break;
  759. case SI5351_DISABLE_FLOATING:
  760. val = SI5351_CLK_DISABLE_STATE_FLOAT;
  761. break;
  762. case SI5351_DISABLE_NEVER:
  763. val = SI5351_CLK_DISABLE_STATE_NEVER;
  764. break;
  765. default:
  766. return 0;
  767. }
  768. si5351_set_bits(drvdata, reg, mask, val << shift);
  769. return 0;
  770. }
  771. static int si5351_clkout_prepare(struct clk_hw *hw)
  772. {
  773. struct si5351_hw_data *hwdata =
  774. container_of(hw, struct si5351_hw_data, hw);
  775. si5351_set_bits(hwdata->drvdata, SI5351_CLK0_CTRL + hwdata->num,
  776. SI5351_CLK_POWERDOWN, 0);
  777. si5351_set_bits(hwdata->drvdata, SI5351_OUTPUT_ENABLE_CTRL,
  778. (1 << hwdata->num), 0);
  779. return 0;
  780. }
  781. static void si5351_clkout_unprepare(struct clk_hw *hw)
  782. {
  783. struct si5351_hw_data *hwdata =
  784. container_of(hw, struct si5351_hw_data, hw);
  785. si5351_set_bits(hwdata->drvdata, SI5351_CLK0_CTRL + hwdata->num,
  786. SI5351_CLK_POWERDOWN, SI5351_CLK_POWERDOWN);
  787. si5351_set_bits(hwdata->drvdata, SI5351_OUTPUT_ENABLE_CTRL,
  788. (1 << hwdata->num), (1 << hwdata->num));
  789. }
  790. static u8 si5351_clkout_get_parent(struct clk_hw *hw)
  791. {
  792. struct si5351_hw_data *hwdata =
  793. container_of(hw, struct si5351_hw_data, hw);
  794. int index = 0;
  795. unsigned char val;
  796. val = si5351_reg_read(hwdata->drvdata, SI5351_CLK0_CTRL + hwdata->num);
  797. switch (val & SI5351_CLK_INPUT_MASK) {
  798. case SI5351_CLK_INPUT_MULTISYNTH_N:
  799. index = 0;
  800. break;
  801. case SI5351_CLK_INPUT_MULTISYNTH_0_4:
  802. index = 1;
  803. break;
  804. case SI5351_CLK_INPUT_XTAL:
  805. index = 2;
  806. break;
  807. case SI5351_CLK_INPUT_CLKIN:
  808. index = 3;
  809. break;
  810. }
  811. return index;
  812. }
  813. static int si5351_clkout_set_parent(struct clk_hw *hw, u8 index)
  814. {
  815. struct si5351_hw_data *hwdata =
  816. container_of(hw, struct si5351_hw_data, hw);
  817. enum si5351_clkout_src parent = SI5351_CLKOUT_SRC_DEFAULT;
  818. switch (index) {
  819. case 0:
  820. parent = SI5351_CLKOUT_SRC_MSYNTH_N;
  821. break;
  822. case 1:
  823. parent = SI5351_CLKOUT_SRC_MSYNTH_0_4;
  824. break;
  825. case 2:
  826. parent = SI5351_CLKOUT_SRC_XTAL;
  827. break;
  828. case 3:
  829. parent = SI5351_CLKOUT_SRC_CLKIN;
  830. break;
  831. }
  832. return _si5351_clkout_reparent(hwdata->drvdata, hwdata->num, parent);
  833. }
  834. static unsigned long si5351_clkout_recalc_rate(struct clk_hw *hw,
  835. unsigned long parent_rate)
  836. {
  837. struct si5351_hw_data *hwdata =
  838. container_of(hw, struct si5351_hw_data, hw);
  839. unsigned char reg;
  840. unsigned char rdiv;
  841. if (hwdata->num <= 5)
  842. reg = si5351_msynth_params_address(hwdata->num) + 2;
  843. else
  844. reg = SI5351_CLK6_7_OUTPUT_DIVIDER;
  845. rdiv = si5351_reg_read(hwdata->drvdata, reg);
  846. if (hwdata->num == 6) {
  847. rdiv &= SI5351_OUTPUT_CLK6_DIV_MASK;
  848. } else {
  849. rdiv &= SI5351_OUTPUT_CLK_DIV_MASK;
  850. rdiv >>= SI5351_OUTPUT_CLK_DIV_SHIFT;
  851. }
  852. return parent_rate >> rdiv;
  853. }
  854. static long si5351_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  855. unsigned long *parent_rate)
  856. {
  857. struct si5351_hw_data *hwdata =
  858. container_of(hw, struct si5351_hw_data, hw);
  859. unsigned char rdiv;
  860. /* clkout6/7 can only handle output freqencies < 150MHz */
  861. if (hwdata->num >= 6 && rate > SI5351_CLKOUT67_MAX_FREQ)
  862. rate = SI5351_CLKOUT67_MAX_FREQ;
  863. /* clkout freqency is 8kHz - 160MHz */
  864. if (rate > SI5351_CLKOUT_MAX_FREQ)
  865. rate = SI5351_CLKOUT_MAX_FREQ;
  866. if (rate < SI5351_CLKOUT_MIN_FREQ)
  867. rate = SI5351_CLKOUT_MIN_FREQ;
  868. /* request frequency if multisync master */
  869. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  870. /* use r divider for frequencies below 1MHz */
  871. rdiv = SI5351_OUTPUT_CLK_DIV_1;
  872. while (rate < SI5351_MULTISYNTH_MIN_FREQ &&
  873. rdiv < SI5351_OUTPUT_CLK_DIV_128) {
  874. rdiv += 1;
  875. rate *= 2;
  876. }
  877. *parent_rate = rate;
  878. } else {
  879. unsigned long new_rate, new_err, err;
  880. /* round to closed rdiv */
  881. rdiv = SI5351_OUTPUT_CLK_DIV_1;
  882. new_rate = *parent_rate;
  883. err = abs(new_rate - rate);
  884. do {
  885. new_rate >>= 1;
  886. new_err = abs(new_rate - rate);
  887. if (new_err > err || rdiv == SI5351_OUTPUT_CLK_DIV_128)
  888. break;
  889. rdiv++;
  890. err = new_err;
  891. } while (1);
  892. }
  893. rate = *parent_rate >> rdiv;
  894. dev_dbg(&hwdata->drvdata->client->dev,
  895. "%s - %s: rdiv = %u, parent_rate = %lu, rate = %lu\n",
  896. __func__, clk_hw_get_name(hw), (1 << rdiv),
  897. *parent_rate, rate);
  898. return rate;
  899. }
  900. static int si5351_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  901. unsigned long parent_rate)
  902. {
  903. struct si5351_hw_data *hwdata =
  904. container_of(hw, struct si5351_hw_data, hw);
  905. unsigned long new_rate, new_err, err;
  906. unsigned char rdiv;
  907. /* round to closed rdiv */
  908. rdiv = SI5351_OUTPUT_CLK_DIV_1;
  909. new_rate = parent_rate;
  910. err = abs(new_rate - rate);
  911. do {
  912. new_rate >>= 1;
  913. new_err = abs(new_rate - rate);
  914. if (new_err > err || rdiv == SI5351_OUTPUT_CLK_DIV_128)
  915. break;
  916. rdiv++;
  917. err = new_err;
  918. } while (1);
  919. /* write output divider */
  920. switch (hwdata->num) {
  921. case 6:
  922. si5351_set_bits(hwdata->drvdata, SI5351_CLK6_7_OUTPUT_DIVIDER,
  923. SI5351_OUTPUT_CLK6_DIV_MASK, rdiv);
  924. break;
  925. case 7:
  926. si5351_set_bits(hwdata->drvdata, SI5351_CLK6_7_OUTPUT_DIVIDER,
  927. SI5351_OUTPUT_CLK_DIV_MASK,
  928. rdiv << SI5351_OUTPUT_CLK_DIV_SHIFT);
  929. break;
  930. default:
  931. si5351_set_bits(hwdata->drvdata,
  932. si5351_msynth_params_address(hwdata->num) + 2,
  933. SI5351_OUTPUT_CLK_DIV_MASK,
  934. rdiv << SI5351_OUTPUT_CLK_DIV_SHIFT);
  935. }
  936. /* powerup clkout */
  937. si5351_set_bits(hwdata->drvdata, SI5351_CLK0_CTRL + hwdata->num,
  938. SI5351_CLK_POWERDOWN, 0);
  939. /*
  940. * Do a pll soft reset on both plls, needed in some cases to get
  941. * all outputs running.
  942. */
  943. si5351_reg_write(hwdata->drvdata, SI5351_PLL_RESET,
  944. SI5351_PLL_RESET_A | SI5351_PLL_RESET_B);
  945. dev_dbg(&hwdata->drvdata->client->dev,
  946. "%s - %s: rdiv = %u, parent_rate = %lu, rate = %lu\n",
  947. __func__, clk_hw_get_name(hw), (1 << rdiv),
  948. parent_rate, rate);
  949. return 0;
  950. }
  951. static const struct clk_ops si5351_clkout_ops = {
  952. .prepare = si5351_clkout_prepare,
  953. .unprepare = si5351_clkout_unprepare,
  954. .set_parent = si5351_clkout_set_parent,
  955. .get_parent = si5351_clkout_get_parent,
  956. .recalc_rate = si5351_clkout_recalc_rate,
  957. .round_rate = si5351_clkout_round_rate,
  958. .set_rate = si5351_clkout_set_rate,
  959. };
  960. /*
  961. * Si5351 i2c probe and DT
  962. */
  963. #ifdef CONFIG_OF
  964. static const struct of_device_id si5351_dt_ids[] = {
  965. { .compatible = "silabs,si5351a", .data = (void *)SI5351_VARIANT_A, },
  966. { .compatible = "silabs,si5351a-msop",
  967. .data = (void *)SI5351_VARIANT_A3, },
  968. { .compatible = "silabs,si5351b", .data = (void *)SI5351_VARIANT_B, },
  969. { .compatible = "silabs,si5351c", .data = (void *)SI5351_VARIANT_C, },
  970. { }
  971. };
  972. MODULE_DEVICE_TABLE(of, si5351_dt_ids);
  973. static int si5351_dt_parse(struct i2c_client *client,
  974. enum si5351_variant variant)
  975. {
  976. struct device_node *child, *np = client->dev.of_node;
  977. struct si5351_platform_data *pdata;
  978. struct property *prop;
  979. const __be32 *p;
  980. int num = 0;
  981. u32 val;
  982. if (np == NULL)
  983. return 0;
  984. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  985. if (!pdata)
  986. return -ENOMEM;
  987. /*
  988. * property silabs,pll-source : <num src>, [<..>]
  989. * allow to selectively set pll source
  990. */
  991. of_property_for_each_u32(np, "silabs,pll-source", prop, p, num) {
  992. if (num >= 2) {
  993. dev_err(&client->dev,
  994. "invalid pll %d on pll-source prop\n", num);
  995. return -EINVAL;
  996. }
  997. p = of_prop_next_u32(prop, p, &val);
  998. if (!p) {
  999. dev_err(&client->dev,
  1000. "missing pll-source for pll %d\n", num);
  1001. return -EINVAL;
  1002. }
  1003. switch (val) {
  1004. case 0:
  1005. pdata->pll_src[num] = SI5351_PLL_SRC_XTAL;
  1006. break;
  1007. case 1:
  1008. if (variant != SI5351_VARIANT_C) {
  1009. dev_err(&client->dev,
  1010. "invalid parent %d for pll %d\n",
  1011. val, num);
  1012. return -EINVAL;
  1013. }
  1014. pdata->pll_src[num] = SI5351_PLL_SRC_CLKIN;
  1015. break;
  1016. default:
  1017. dev_err(&client->dev,
  1018. "invalid parent %d for pll %d\n", val, num);
  1019. return -EINVAL;
  1020. }
  1021. }
  1022. /* per clkout properties */
  1023. for_each_child_of_node(np, child) {
  1024. if (of_property_read_u32(child, "reg", &num)) {
  1025. dev_err(&client->dev, "missing reg property of %s\n",
  1026. child->name);
  1027. goto put_child;
  1028. }
  1029. if (num >= 8 ||
  1030. (variant == SI5351_VARIANT_A3 && num >= 3)) {
  1031. dev_err(&client->dev, "invalid clkout %d\n", num);
  1032. goto put_child;
  1033. }
  1034. if (!of_property_read_u32(child, "silabs,multisynth-source",
  1035. &val)) {
  1036. switch (val) {
  1037. case 0:
  1038. pdata->clkout[num].multisynth_src =
  1039. SI5351_MULTISYNTH_SRC_VCO0;
  1040. break;
  1041. case 1:
  1042. pdata->clkout[num].multisynth_src =
  1043. SI5351_MULTISYNTH_SRC_VCO1;
  1044. break;
  1045. default:
  1046. dev_err(&client->dev,
  1047. "invalid parent %d for multisynth %d\n",
  1048. val, num);
  1049. goto put_child;
  1050. }
  1051. }
  1052. if (!of_property_read_u32(child, "silabs,clock-source", &val)) {
  1053. switch (val) {
  1054. case 0:
  1055. pdata->clkout[num].clkout_src =
  1056. SI5351_CLKOUT_SRC_MSYNTH_N;
  1057. break;
  1058. case 1:
  1059. pdata->clkout[num].clkout_src =
  1060. SI5351_CLKOUT_SRC_MSYNTH_0_4;
  1061. break;
  1062. case 2:
  1063. pdata->clkout[num].clkout_src =
  1064. SI5351_CLKOUT_SRC_XTAL;
  1065. break;
  1066. case 3:
  1067. if (variant != SI5351_VARIANT_C) {
  1068. dev_err(&client->dev,
  1069. "invalid parent %d for clkout %d\n",
  1070. val, num);
  1071. goto put_child;
  1072. }
  1073. pdata->clkout[num].clkout_src =
  1074. SI5351_CLKOUT_SRC_CLKIN;
  1075. break;
  1076. default:
  1077. dev_err(&client->dev,
  1078. "invalid parent %d for clkout %d\n",
  1079. val, num);
  1080. goto put_child;
  1081. }
  1082. }
  1083. if (!of_property_read_u32(child, "silabs,drive-strength",
  1084. &val)) {
  1085. switch (val) {
  1086. case SI5351_DRIVE_2MA:
  1087. case SI5351_DRIVE_4MA:
  1088. case SI5351_DRIVE_6MA:
  1089. case SI5351_DRIVE_8MA:
  1090. pdata->clkout[num].drive = val;
  1091. break;
  1092. default:
  1093. dev_err(&client->dev,
  1094. "invalid drive strength %d for clkout %d\n",
  1095. val, num);
  1096. goto put_child;
  1097. }
  1098. }
  1099. if (!of_property_read_u32(child, "silabs,disable-state",
  1100. &val)) {
  1101. switch (val) {
  1102. case 0:
  1103. pdata->clkout[num].disable_state =
  1104. SI5351_DISABLE_LOW;
  1105. break;
  1106. case 1:
  1107. pdata->clkout[num].disable_state =
  1108. SI5351_DISABLE_HIGH;
  1109. break;
  1110. case 2:
  1111. pdata->clkout[num].disable_state =
  1112. SI5351_DISABLE_FLOATING;
  1113. break;
  1114. case 3:
  1115. pdata->clkout[num].disable_state =
  1116. SI5351_DISABLE_NEVER;
  1117. break;
  1118. default:
  1119. dev_err(&client->dev,
  1120. "invalid disable state %d for clkout %d\n",
  1121. val, num);
  1122. goto put_child;
  1123. }
  1124. }
  1125. if (!of_property_read_u32(child, "clock-frequency", &val))
  1126. pdata->clkout[num].rate = val;
  1127. pdata->clkout[num].pll_master =
  1128. of_property_read_bool(child, "silabs,pll-master");
  1129. }
  1130. client->dev.platform_data = pdata;
  1131. return 0;
  1132. put_child:
  1133. of_node_put(child);
  1134. return -EINVAL;
  1135. }
  1136. static struct clk_hw *
  1137. si53351_of_clk_get(struct of_phandle_args *clkspec, void *data)
  1138. {
  1139. struct si5351_driver_data *drvdata = data;
  1140. unsigned int idx = clkspec->args[0];
  1141. if (idx >= drvdata->num_clkout) {
  1142. pr_err("%s: invalid index %u\n", __func__, idx);
  1143. return ERR_PTR(-EINVAL);
  1144. }
  1145. return &drvdata->clkout[idx].hw;
  1146. }
  1147. #else
  1148. static int si5351_dt_parse(struct i2c_client *client, enum si5351_variant variant)
  1149. {
  1150. return 0;
  1151. }
  1152. static struct clk_hw *
  1153. si53351_of_clk_get(struct of_phandle_args *clkspec, void *data)
  1154. {
  1155. return NULL;
  1156. }
  1157. #endif /* CONFIG_OF */
  1158. static int si5351_i2c_probe(struct i2c_client *client,
  1159. const struct i2c_device_id *id)
  1160. {
  1161. enum si5351_variant variant = (enum si5351_variant)id->driver_data;
  1162. struct si5351_platform_data *pdata;
  1163. struct si5351_driver_data *drvdata;
  1164. struct clk_init_data init;
  1165. const char *parent_names[4];
  1166. u8 num_parents, num_clocks;
  1167. int ret, n;
  1168. ret = si5351_dt_parse(client, variant);
  1169. if (ret)
  1170. return ret;
  1171. pdata = client->dev.platform_data;
  1172. if (!pdata)
  1173. return -EINVAL;
  1174. drvdata = devm_kzalloc(&client->dev, sizeof(*drvdata), GFP_KERNEL);
  1175. if (drvdata == NULL) {
  1176. dev_err(&client->dev, "unable to allocate driver data\n");
  1177. return -ENOMEM;
  1178. }
  1179. i2c_set_clientdata(client, drvdata);
  1180. drvdata->client = client;
  1181. drvdata->variant = variant;
  1182. drvdata->pxtal = devm_clk_get(&client->dev, "xtal");
  1183. drvdata->pclkin = devm_clk_get(&client->dev, "clkin");
  1184. if (PTR_ERR(drvdata->pxtal) == -EPROBE_DEFER ||
  1185. PTR_ERR(drvdata->pclkin) == -EPROBE_DEFER)
  1186. return -EPROBE_DEFER;
  1187. /*
  1188. * Check for valid parent clock: VARIANT_A and VARIANT_B need XTAL,
  1189. * VARIANT_C can have CLKIN instead.
  1190. */
  1191. if (IS_ERR(drvdata->pxtal) &&
  1192. (drvdata->variant != SI5351_VARIANT_C || IS_ERR(drvdata->pclkin))) {
  1193. dev_err(&client->dev, "missing parent clock\n");
  1194. return -EINVAL;
  1195. }
  1196. drvdata->regmap = devm_regmap_init_i2c(client, &si5351_regmap_config);
  1197. if (IS_ERR(drvdata->regmap)) {
  1198. dev_err(&client->dev, "failed to allocate register map\n");
  1199. return PTR_ERR(drvdata->regmap);
  1200. }
  1201. /* Disable interrupts */
  1202. si5351_reg_write(drvdata, SI5351_INTERRUPT_MASK, 0xf0);
  1203. /* Ensure pll select is on XTAL for Si5351A/B */
  1204. if (drvdata->variant != SI5351_VARIANT_C)
  1205. si5351_set_bits(drvdata, SI5351_PLL_INPUT_SOURCE,
  1206. SI5351_PLLA_SOURCE | SI5351_PLLB_SOURCE, 0);
  1207. /* setup clock configuration */
  1208. for (n = 0; n < 2; n++) {
  1209. ret = _si5351_pll_reparent(drvdata, n, pdata->pll_src[n]);
  1210. if (ret) {
  1211. dev_err(&client->dev,
  1212. "failed to reparent pll %d to %d\n",
  1213. n, pdata->pll_src[n]);
  1214. return ret;
  1215. }
  1216. }
  1217. for (n = 0; n < 8; n++) {
  1218. ret = _si5351_msynth_reparent(drvdata, n,
  1219. pdata->clkout[n].multisynth_src);
  1220. if (ret) {
  1221. dev_err(&client->dev,
  1222. "failed to reparent multisynth %d to %d\n",
  1223. n, pdata->clkout[n].multisynth_src);
  1224. return ret;
  1225. }
  1226. ret = _si5351_clkout_reparent(drvdata, n,
  1227. pdata->clkout[n].clkout_src);
  1228. if (ret) {
  1229. dev_err(&client->dev,
  1230. "failed to reparent clkout %d to %d\n",
  1231. n, pdata->clkout[n].clkout_src);
  1232. return ret;
  1233. }
  1234. ret = _si5351_clkout_set_drive_strength(drvdata, n,
  1235. pdata->clkout[n].drive);
  1236. if (ret) {
  1237. dev_err(&client->dev,
  1238. "failed set drive strength of clkout%d to %d\n",
  1239. n, pdata->clkout[n].drive);
  1240. return ret;
  1241. }
  1242. ret = _si5351_clkout_set_disable_state(drvdata, n,
  1243. pdata->clkout[n].disable_state);
  1244. if (ret) {
  1245. dev_err(&client->dev,
  1246. "failed set disable state of clkout%d to %d\n",
  1247. n, pdata->clkout[n].disable_state);
  1248. return ret;
  1249. }
  1250. }
  1251. if (!IS_ERR(drvdata->pxtal))
  1252. clk_prepare_enable(drvdata->pxtal);
  1253. if (!IS_ERR(drvdata->pclkin))
  1254. clk_prepare_enable(drvdata->pclkin);
  1255. /* register xtal input clock gate */
  1256. memset(&init, 0, sizeof(init));
  1257. init.name = si5351_input_names[0];
  1258. init.ops = &si5351_xtal_ops;
  1259. init.flags = 0;
  1260. if (!IS_ERR(drvdata->pxtal)) {
  1261. drvdata->pxtal_name = __clk_get_name(drvdata->pxtal);
  1262. init.parent_names = &drvdata->pxtal_name;
  1263. init.num_parents = 1;
  1264. }
  1265. drvdata->xtal.init = &init;
  1266. ret = devm_clk_hw_register(&client->dev, &drvdata->xtal);
  1267. if (ret) {
  1268. dev_err(&client->dev, "unable to register %s\n", init.name);
  1269. goto err_clk;
  1270. }
  1271. /* register clkin input clock gate */
  1272. if (drvdata->variant == SI5351_VARIANT_C) {
  1273. memset(&init, 0, sizeof(init));
  1274. init.name = si5351_input_names[1];
  1275. init.ops = &si5351_clkin_ops;
  1276. if (!IS_ERR(drvdata->pclkin)) {
  1277. drvdata->pclkin_name = __clk_get_name(drvdata->pclkin);
  1278. init.parent_names = &drvdata->pclkin_name;
  1279. init.num_parents = 1;
  1280. }
  1281. drvdata->clkin.init = &init;
  1282. ret = devm_clk_hw_register(&client->dev, &drvdata->clkin);
  1283. if (ret) {
  1284. dev_err(&client->dev, "unable to register %s\n",
  1285. init.name);
  1286. goto err_clk;
  1287. }
  1288. }
  1289. /* Si5351C allows to mux either xtal or clkin to PLL input */
  1290. num_parents = (drvdata->variant == SI5351_VARIANT_C) ? 2 : 1;
  1291. parent_names[0] = si5351_input_names[0];
  1292. parent_names[1] = si5351_input_names[1];
  1293. /* register PLLA */
  1294. drvdata->pll[0].num = 0;
  1295. drvdata->pll[0].drvdata = drvdata;
  1296. drvdata->pll[0].hw.init = &init;
  1297. memset(&init, 0, sizeof(init));
  1298. init.name = si5351_pll_names[0];
  1299. init.ops = &si5351_pll_ops;
  1300. init.flags = 0;
  1301. init.parent_names = parent_names;
  1302. init.num_parents = num_parents;
  1303. ret = devm_clk_hw_register(&client->dev, &drvdata->pll[0].hw);
  1304. if (ret) {
  1305. dev_err(&client->dev, "unable to register %s\n", init.name);
  1306. goto err_clk;
  1307. }
  1308. /* register PLLB or VXCO (Si5351B) */
  1309. drvdata->pll[1].num = 1;
  1310. drvdata->pll[1].drvdata = drvdata;
  1311. drvdata->pll[1].hw.init = &init;
  1312. memset(&init, 0, sizeof(init));
  1313. if (drvdata->variant == SI5351_VARIANT_B) {
  1314. init.name = si5351_pll_names[2];
  1315. init.ops = &si5351_vxco_ops;
  1316. init.flags = 0;
  1317. init.parent_names = NULL;
  1318. init.num_parents = 0;
  1319. } else {
  1320. init.name = si5351_pll_names[1];
  1321. init.ops = &si5351_pll_ops;
  1322. init.flags = 0;
  1323. init.parent_names = parent_names;
  1324. init.num_parents = num_parents;
  1325. }
  1326. ret = devm_clk_hw_register(&client->dev, &drvdata->pll[1].hw);
  1327. if (ret) {
  1328. dev_err(&client->dev, "unable to register %s\n", init.name);
  1329. goto err_clk;
  1330. }
  1331. /* register clk multisync and clk out divider */
  1332. num_clocks = (drvdata->variant == SI5351_VARIANT_A3) ? 3 : 8;
  1333. parent_names[0] = si5351_pll_names[0];
  1334. if (drvdata->variant == SI5351_VARIANT_B)
  1335. parent_names[1] = si5351_pll_names[2];
  1336. else
  1337. parent_names[1] = si5351_pll_names[1];
  1338. drvdata->msynth = devm_kzalloc(&client->dev, num_clocks *
  1339. sizeof(*drvdata->msynth), GFP_KERNEL);
  1340. drvdata->clkout = devm_kzalloc(&client->dev, num_clocks *
  1341. sizeof(*drvdata->clkout), GFP_KERNEL);
  1342. drvdata->num_clkout = num_clocks;
  1343. if (WARN_ON(!drvdata->msynth || !drvdata->clkout)) {
  1344. ret = -ENOMEM;
  1345. goto err_clk;
  1346. }
  1347. for (n = 0; n < num_clocks; n++) {
  1348. drvdata->msynth[n].num = n;
  1349. drvdata->msynth[n].drvdata = drvdata;
  1350. drvdata->msynth[n].hw.init = &init;
  1351. memset(&init, 0, sizeof(init));
  1352. init.name = si5351_msynth_names[n];
  1353. init.ops = &si5351_msynth_ops;
  1354. init.flags = 0;
  1355. if (pdata->clkout[n].pll_master)
  1356. init.flags |= CLK_SET_RATE_PARENT;
  1357. init.parent_names = parent_names;
  1358. init.num_parents = 2;
  1359. ret = devm_clk_hw_register(&client->dev,
  1360. &drvdata->msynth[n].hw);
  1361. if (ret) {
  1362. dev_err(&client->dev, "unable to register %s\n",
  1363. init.name);
  1364. goto err_clk;
  1365. }
  1366. }
  1367. num_parents = (drvdata->variant == SI5351_VARIANT_C) ? 4 : 3;
  1368. parent_names[2] = si5351_input_names[0];
  1369. parent_names[3] = si5351_input_names[1];
  1370. for (n = 0; n < num_clocks; n++) {
  1371. parent_names[0] = si5351_msynth_names[n];
  1372. parent_names[1] = (n < 4) ? si5351_msynth_names[0] :
  1373. si5351_msynth_names[4];
  1374. drvdata->clkout[n].num = n;
  1375. drvdata->clkout[n].drvdata = drvdata;
  1376. drvdata->clkout[n].hw.init = &init;
  1377. memset(&init, 0, sizeof(init));
  1378. init.name = si5351_clkout_names[n];
  1379. init.ops = &si5351_clkout_ops;
  1380. init.flags = 0;
  1381. if (pdata->clkout[n].clkout_src == SI5351_CLKOUT_SRC_MSYNTH_N)
  1382. init.flags |= CLK_SET_RATE_PARENT;
  1383. init.parent_names = parent_names;
  1384. init.num_parents = num_parents;
  1385. ret = devm_clk_hw_register(&client->dev,
  1386. &drvdata->clkout[n].hw);
  1387. if (ret) {
  1388. dev_err(&client->dev, "unable to register %s\n",
  1389. init.name);
  1390. goto err_clk;
  1391. }
  1392. /* set initial clkout rate */
  1393. if (pdata->clkout[n].rate != 0) {
  1394. int ret;
  1395. ret = clk_set_rate(drvdata->clkout[n].hw.clk,
  1396. pdata->clkout[n].rate);
  1397. if (ret != 0) {
  1398. dev_err(&client->dev, "Cannot set rate : %d\n",
  1399. ret);
  1400. }
  1401. }
  1402. }
  1403. ret = of_clk_add_hw_provider(client->dev.of_node, si53351_of_clk_get,
  1404. drvdata);
  1405. if (ret) {
  1406. dev_err(&client->dev, "unable to add clk provider\n");
  1407. goto err_clk;
  1408. }
  1409. return 0;
  1410. err_clk:
  1411. if (!IS_ERR(drvdata->pxtal))
  1412. clk_disable_unprepare(drvdata->pxtal);
  1413. if (!IS_ERR(drvdata->pclkin))
  1414. clk_disable_unprepare(drvdata->pclkin);
  1415. return ret;
  1416. }
  1417. static const struct i2c_device_id si5351_i2c_ids[] = {
  1418. { "si5351a", SI5351_VARIANT_A },
  1419. { "si5351a-msop", SI5351_VARIANT_A3 },
  1420. { "si5351b", SI5351_VARIANT_B },
  1421. { "si5351c", SI5351_VARIANT_C },
  1422. { }
  1423. };
  1424. MODULE_DEVICE_TABLE(i2c, si5351_i2c_ids);
  1425. static struct i2c_driver si5351_driver = {
  1426. .driver = {
  1427. .name = "si5351",
  1428. .of_match_table = of_match_ptr(si5351_dt_ids),
  1429. },
  1430. .probe = si5351_i2c_probe,
  1431. .id_table = si5351_i2c_ids,
  1432. };
  1433. module_i2c_driver(si5351_driver);
  1434. MODULE_AUTHOR("Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com");
  1435. MODULE_DESCRIPTION("Silicon Labs Si5351A/B/C clock generator driver");
  1436. MODULE_LICENSE("GPL");