clk-versaclock5.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * Driver for IDT Versaclock 5
  3. *
  4. * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /*
  17. * Possible optimizations:
  18. * - Use spread spectrum
  19. * - Use integer divider in FOD if applicable
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/delay.h>
  24. #include <linux/i2c.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/rational.h>
  31. #include <linux/regmap.h>
  32. #include <linux/slab.h>
  33. /* VersaClock5 registers */
  34. #define VC5_OTP_CONTROL 0x00
  35. /* Factory-reserved register block */
  36. #define VC5_RSVD_DEVICE_ID 0x01
  37. #define VC5_RSVD_ADC_GAIN_7_0 0x02
  38. #define VC5_RSVD_ADC_GAIN_15_8 0x03
  39. #define VC5_RSVD_ADC_OFFSET_7_0 0x04
  40. #define VC5_RSVD_ADC_OFFSET_15_8 0x05
  41. #define VC5_RSVD_TEMPY 0x06
  42. #define VC5_RSVD_OFFSET_TBIN 0x07
  43. #define VC5_RSVD_GAIN 0x08
  44. #define VC5_RSVD_TEST_NP 0x09
  45. #define VC5_RSVD_UNUSED 0x0a
  46. #define VC5_RSVD_BANDGAP_TRIM_UP 0x0b
  47. #define VC5_RSVD_BANDGAP_TRIM_DN 0x0c
  48. #define VC5_RSVD_CLK_R_12_CLK_AMP_4 0x0d
  49. #define VC5_RSVD_CLK_R_34_CLK_AMP_4 0x0e
  50. #define VC5_RSVD_CLK_AMP_123 0x0f
  51. /* Configuration register block */
  52. #define VC5_PRIM_SRC_SHDN 0x10
  53. #define VC5_PRIM_SRC_SHDN_EN_XTAL BIT(7)
  54. #define VC5_PRIM_SRC_SHDN_EN_CLKIN BIT(6)
  55. #define VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ BIT(3)
  56. #define VC5_PRIM_SRC_SHDN_SP BIT(1)
  57. #define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
  58. #define VC5_VCO_BAND 0x11
  59. #define VC5_XTAL_X1_LOAD_CAP 0x12
  60. #define VC5_XTAL_X2_LOAD_CAP 0x13
  61. #define VC5_REF_DIVIDER 0x15
  62. #define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
  63. #define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
  64. #define VC5_VCO_CTRL_AND_PREDIV 0x16
  65. #define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
  66. #define VC5_FEEDBACK_INT_DIV 0x17
  67. #define VC5_FEEDBACK_INT_DIV_BITS 0x18
  68. #define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
  69. #define VC5_RC_CONTROL0 0x1e
  70. #define VC5_RC_CONTROL1 0x1f
  71. /* Register 0x20 is factory reserved */
  72. /* Output divider control for divider 1,2,3,4 */
  73. #define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
  74. #define VC5_OUT_DIV_CONTROL_RESET BIT(7)
  75. #define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
  76. #define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
  77. #define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
  78. #define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
  79. #define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
  80. #define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
  81. #define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
  82. #define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
  83. #define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
  84. #define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
  85. #define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
  86. /* Registers 0x30, 0x40, 0x50 are factory reserved */
  87. /* Clock control register for clock 1,2 */
  88. #define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
  89. #define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
  90. #define VC5_CLK_OE_SHDN 0x68
  91. #define VC5_CLK_OS_SHDN 0x69
  92. #define VC5_GLOBAL_REGISTER 0x76
  93. #define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
  94. /* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
  95. #define VC5_PLL_VCO_MIN 2500000000UL
  96. #define VC5_PLL_VCO_MAX 3000000000UL
  97. /* VC5 Input mux settings */
  98. #define VC5_MUX_IN_XIN BIT(0)
  99. #define VC5_MUX_IN_CLKIN BIT(1)
  100. /* Maximum number of clk_out supported by this driver */
  101. #define VC5_MAX_CLK_OUT_NUM 5
  102. /* Maximum number of FODs supported by this driver */
  103. #define VC5_MAX_FOD_NUM 4
  104. /* flags to describe chip features */
  105. /* chip has built-in oscilator */
  106. #define VC5_HAS_INTERNAL_XTAL BIT(0)
  107. /* chip has PFD requency doubler */
  108. #define VC5_HAS_PFD_FREQ_DBL BIT(1)
  109. /* Supported IDT VC5 models. */
  110. enum vc5_model {
  111. IDT_VC5_5P49V5923,
  112. IDT_VC5_5P49V5925,
  113. IDT_VC5_5P49V5933,
  114. IDT_VC5_5P49V5935,
  115. IDT_VC6_5P49V6901,
  116. };
  117. /* Structure to describe features of a particular VC5 model */
  118. struct vc5_chip_info {
  119. const enum vc5_model model;
  120. const unsigned int clk_fod_cnt;
  121. const unsigned int clk_out_cnt;
  122. const u32 flags;
  123. };
  124. struct vc5_driver_data;
  125. struct vc5_hw_data {
  126. struct clk_hw hw;
  127. struct vc5_driver_data *vc5;
  128. u32 div_int;
  129. u32 div_frc;
  130. unsigned int num;
  131. };
  132. struct vc5_driver_data {
  133. struct i2c_client *client;
  134. struct regmap *regmap;
  135. const struct vc5_chip_info *chip_info;
  136. struct clk *pin_xin;
  137. struct clk *pin_clkin;
  138. unsigned char clk_mux_ins;
  139. struct clk_hw clk_mux;
  140. struct clk_hw clk_mul;
  141. struct clk_hw clk_pfd;
  142. struct vc5_hw_data clk_pll;
  143. struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
  144. struct vc5_hw_data clk_out[VC5_MAX_CLK_OUT_NUM];
  145. };
  146. static const char * const vc5_mux_names[] = {
  147. "mux"
  148. };
  149. static const char * const vc5_dbl_names[] = {
  150. "dbl"
  151. };
  152. static const char * const vc5_pfd_names[] = {
  153. "pfd"
  154. };
  155. static const char * const vc5_pll_names[] = {
  156. "pll"
  157. };
  158. static const char * const vc5_fod_names[] = {
  159. "fod0", "fod1", "fod2", "fod3",
  160. };
  161. static const char * const vc5_clk_out_names[] = {
  162. "out0_sel_i2cb", "out1", "out2", "out3", "out4",
  163. };
  164. /*
  165. * VersaClock5 i2c regmap
  166. */
  167. static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
  168. {
  169. /* Factory reserved regs, make them read-only */
  170. if (reg <= 0xf)
  171. return false;
  172. /* Factory reserved regs, make them read-only */
  173. if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
  174. return false;
  175. return true;
  176. }
  177. static const struct regmap_config vc5_regmap_config = {
  178. .reg_bits = 8,
  179. .val_bits = 8,
  180. .cache_type = REGCACHE_RBTREE,
  181. .max_register = 0x76,
  182. .writeable_reg = vc5_regmap_is_writeable,
  183. };
  184. /*
  185. * VersaClock5 input multiplexer between XTAL and CLKIN divider
  186. */
  187. static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
  188. {
  189. struct vc5_driver_data *vc5 =
  190. container_of(hw, struct vc5_driver_data, clk_mux);
  191. const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
  192. unsigned int src;
  193. regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
  194. src &= mask;
  195. if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
  196. return 0;
  197. if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
  198. return 1;
  199. dev_warn(&vc5->client->dev,
  200. "Invalid clock input configuration (%02x)\n", src);
  201. return 0;
  202. }
  203. static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
  204. {
  205. struct vc5_driver_data *vc5 =
  206. container_of(hw, struct vc5_driver_data, clk_mux);
  207. const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
  208. u8 src;
  209. if ((index > 1) || !vc5->clk_mux_ins)
  210. return -EINVAL;
  211. if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
  212. if (index == 0)
  213. src = VC5_PRIM_SRC_SHDN_EN_XTAL;
  214. if (index == 1)
  215. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  216. } else {
  217. if (index != 0)
  218. return -EINVAL;
  219. if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
  220. src = VC5_PRIM_SRC_SHDN_EN_XTAL;
  221. else if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
  222. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  223. else /* Invalid; should have been caught by vc5_probe() */
  224. return -EINVAL;
  225. }
  226. return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
  227. }
  228. static const struct clk_ops vc5_mux_ops = {
  229. .set_parent = vc5_mux_set_parent,
  230. .get_parent = vc5_mux_get_parent,
  231. };
  232. static unsigned long vc5_dbl_recalc_rate(struct clk_hw *hw,
  233. unsigned long parent_rate)
  234. {
  235. struct vc5_driver_data *vc5 =
  236. container_of(hw, struct vc5_driver_data, clk_mul);
  237. unsigned int premul;
  238. regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &premul);
  239. if (premul & VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ)
  240. parent_rate *= 2;
  241. return parent_rate;
  242. }
  243. static long vc5_dbl_round_rate(struct clk_hw *hw, unsigned long rate,
  244. unsigned long *parent_rate)
  245. {
  246. if ((*parent_rate == rate) || ((*parent_rate * 2) == rate))
  247. return rate;
  248. else
  249. return -EINVAL;
  250. }
  251. static int vc5_dbl_set_rate(struct clk_hw *hw, unsigned long rate,
  252. unsigned long parent_rate)
  253. {
  254. struct vc5_driver_data *vc5 =
  255. container_of(hw, struct vc5_driver_data, clk_mul);
  256. u32 mask;
  257. if ((parent_rate * 2) == rate)
  258. mask = VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ;
  259. else
  260. mask = 0;
  261. regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN,
  262. VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ,
  263. mask);
  264. return 0;
  265. }
  266. static const struct clk_ops vc5_dbl_ops = {
  267. .recalc_rate = vc5_dbl_recalc_rate,
  268. .round_rate = vc5_dbl_round_rate,
  269. .set_rate = vc5_dbl_set_rate,
  270. };
  271. static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
  272. unsigned long parent_rate)
  273. {
  274. struct vc5_driver_data *vc5 =
  275. container_of(hw, struct vc5_driver_data, clk_pfd);
  276. unsigned int prediv, div;
  277. regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
  278. /* The bypass_prediv is set, PLL fed from Ref_in directly. */
  279. if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
  280. return parent_rate;
  281. regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
  282. /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
  283. if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
  284. return parent_rate / 2;
  285. else
  286. return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
  287. }
  288. static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
  289. unsigned long *parent_rate)
  290. {
  291. unsigned long idiv;
  292. /* PLL cannot operate with input clock above 50 MHz. */
  293. if (rate > 50000000)
  294. return -EINVAL;
  295. /* CLKIN within range of PLL input, feed directly to PLL. */
  296. if (*parent_rate <= 50000000)
  297. return *parent_rate;
  298. idiv = DIV_ROUND_UP(*parent_rate, rate);
  299. if (idiv > 127)
  300. return -EINVAL;
  301. return *parent_rate / idiv;
  302. }
  303. static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
  304. unsigned long parent_rate)
  305. {
  306. struct vc5_driver_data *vc5 =
  307. container_of(hw, struct vc5_driver_data, clk_pfd);
  308. unsigned long idiv;
  309. u8 div;
  310. /* CLKIN within range of PLL input, feed directly to PLL. */
  311. if (parent_rate <= 50000000) {
  312. regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  313. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
  314. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
  315. regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
  316. return 0;
  317. }
  318. idiv = DIV_ROUND_UP(parent_rate, rate);
  319. /* We have dedicated div-2 predivider. */
  320. if (idiv == 2)
  321. div = VC5_REF_DIVIDER_SEL_PREDIV2;
  322. else
  323. div = VC5_REF_DIVIDER_REF_DIV(idiv);
  324. regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
  325. regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  326. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
  327. return 0;
  328. }
  329. static const struct clk_ops vc5_pfd_ops = {
  330. .recalc_rate = vc5_pfd_recalc_rate,
  331. .round_rate = vc5_pfd_round_rate,
  332. .set_rate = vc5_pfd_set_rate,
  333. };
  334. /*
  335. * VersaClock5 PLL/VCO
  336. */
  337. static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
  338. unsigned long parent_rate)
  339. {
  340. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  341. struct vc5_driver_data *vc5 = hwdata->vc5;
  342. u32 div_int, div_frc;
  343. u8 fb[5];
  344. regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  345. div_int = (fb[0] << 4) | (fb[1] >> 4);
  346. div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
  347. /* The PLL divider has 12 integer bits and 24 fractional bits */
  348. return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
  349. }
  350. static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  351. unsigned long *parent_rate)
  352. {
  353. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  354. u32 div_int;
  355. u64 div_frc;
  356. if (rate < VC5_PLL_VCO_MIN)
  357. rate = VC5_PLL_VCO_MIN;
  358. if (rate > VC5_PLL_VCO_MAX)
  359. rate = VC5_PLL_VCO_MAX;
  360. /* Determine integer part, which is 12 bit wide */
  361. div_int = rate / *parent_rate;
  362. if (div_int > 0xfff)
  363. rate = *parent_rate * 0xfff;
  364. /* Determine best fractional part, which is 24 bit wide */
  365. div_frc = rate % *parent_rate;
  366. div_frc *= BIT(24) - 1;
  367. do_div(div_frc, *parent_rate);
  368. hwdata->div_int = div_int;
  369. hwdata->div_frc = (u32)div_frc;
  370. return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
  371. }
  372. static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  373. unsigned long parent_rate)
  374. {
  375. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  376. struct vc5_driver_data *vc5 = hwdata->vc5;
  377. u8 fb[5];
  378. fb[0] = hwdata->div_int >> 4;
  379. fb[1] = hwdata->div_int << 4;
  380. fb[2] = hwdata->div_frc >> 16;
  381. fb[3] = hwdata->div_frc >> 8;
  382. fb[4] = hwdata->div_frc;
  383. return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  384. }
  385. static const struct clk_ops vc5_pll_ops = {
  386. .recalc_rate = vc5_pll_recalc_rate,
  387. .round_rate = vc5_pll_round_rate,
  388. .set_rate = vc5_pll_set_rate,
  389. };
  390. static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
  391. unsigned long parent_rate)
  392. {
  393. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  394. struct vc5_driver_data *vc5 = hwdata->vc5;
  395. /* VCO frequency is divided by two before entering FOD */
  396. u32 f_in = parent_rate / 2;
  397. u32 div_int, div_frc;
  398. u8 od_int[2];
  399. u8 od_frc[4];
  400. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
  401. od_int, 2);
  402. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  403. od_frc, 4);
  404. div_int = (od_int[0] << 4) | (od_int[1] >> 4);
  405. div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
  406. (od_frc[2] << 6) | (od_frc[3] >> 2);
  407. /* Avoid division by zero if the output is not configured. */
  408. if (div_int == 0 && div_frc == 0)
  409. return 0;
  410. /* The PLL divider has 12 integer bits and 30 fractional bits */
  411. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  412. }
  413. static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
  414. unsigned long *parent_rate)
  415. {
  416. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  417. /* VCO frequency is divided by two before entering FOD */
  418. u32 f_in = *parent_rate / 2;
  419. u32 div_int;
  420. u64 div_frc;
  421. /* Determine integer part, which is 12 bit wide */
  422. div_int = f_in / rate;
  423. /*
  424. * WARNING: The clock chip does not output signal if the integer part
  425. * of the divider is 0xfff and fractional part is non-zero.
  426. * Clamp the divider at 0xffe to keep the code simple.
  427. */
  428. if (div_int > 0xffe) {
  429. div_int = 0xffe;
  430. rate = f_in / div_int;
  431. }
  432. /* Determine best fractional part, which is 30 bit wide */
  433. div_frc = f_in % rate;
  434. div_frc <<= 24;
  435. do_div(div_frc, rate);
  436. hwdata->div_int = div_int;
  437. hwdata->div_frc = (u32)div_frc;
  438. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  439. }
  440. static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
  441. unsigned long parent_rate)
  442. {
  443. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  444. struct vc5_driver_data *vc5 = hwdata->vc5;
  445. u8 data[14] = {
  446. hwdata->div_frc >> 22, hwdata->div_frc >> 14,
  447. hwdata->div_frc >> 6, hwdata->div_frc << 2,
  448. 0, 0, 0, 0, 0,
  449. 0, 0,
  450. hwdata->div_int >> 4, hwdata->div_int << 4,
  451. 0
  452. };
  453. regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  454. data, 14);
  455. /*
  456. * Toggle magic bit in undocumented register for unknown reason.
  457. * This is what the IDT timing commander tool does and the chip
  458. * datasheet somewhat implies this is needed, but the register
  459. * and the bit is not documented.
  460. */
  461. regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  462. VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
  463. regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  464. VC5_GLOBAL_REGISTER_GLOBAL_RESET,
  465. VC5_GLOBAL_REGISTER_GLOBAL_RESET);
  466. return 0;
  467. }
  468. static const struct clk_ops vc5_fod_ops = {
  469. .recalc_rate = vc5_fod_recalc_rate,
  470. .round_rate = vc5_fod_round_rate,
  471. .set_rate = vc5_fod_set_rate,
  472. };
  473. static int vc5_clk_out_prepare(struct clk_hw *hw)
  474. {
  475. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  476. struct vc5_driver_data *vc5 = hwdata->vc5;
  477. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  478. VC5_OUT_DIV_CONTROL_SEL_EXT |
  479. VC5_OUT_DIV_CONTROL_EN_FOD;
  480. unsigned int src;
  481. int ret;
  482. /*
  483. * If the input mux is disabled, enable it first and
  484. * select source from matching FOD.
  485. */
  486. regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  487. if ((src & mask) == 0) {
  488. src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
  489. ret = regmap_update_bits(vc5->regmap,
  490. VC5_OUT_DIV_CONTROL(hwdata->num),
  491. mask | VC5_OUT_DIV_CONTROL_RESET, src);
  492. if (ret)
  493. return ret;
  494. }
  495. /* Enable the clock buffer */
  496. regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  497. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
  498. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
  499. return 0;
  500. }
  501. static void vc5_clk_out_unprepare(struct clk_hw *hw)
  502. {
  503. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  504. struct vc5_driver_data *vc5 = hwdata->vc5;
  505. /* Disable the clock buffer */
  506. regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  507. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
  508. }
  509. static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
  510. {
  511. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  512. struct vc5_driver_data *vc5 = hwdata->vc5;
  513. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  514. VC5_OUT_DIV_CONTROL_SEL_EXT |
  515. VC5_OUT_DIV_CONTROL_EN_FOD;
  516. const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  517. VC5_OUT_DIV_CONTROL_EN_FOD;
  518. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  519. VC5_OUT_DIV_CONTROL_SEL_EXT;
  520. unsigned int src;
  521. regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  522. src &= mask;
  523. if (src == 0) /* Input mux set to DISABLED */
  524. return 0;
  525. if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
  526. return 0;
  527. if (src == extclk)
  528. return 1;
  529. dev_warn(&vc5->client->dev,
  530. "Invalid clock output configuration (%02x)\n", src);
  531. return 0;
  532. }
  533. static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
  534. {
  535. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  536. struct vc5_driver_data *vc5 = hwdata->vc5;
  537. const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
  538. VC5_OUT_DIV_CONTROL_SELB_NORM |
  539. VC5_OUT_DIV_CONTROL_SEL_EXT |
  540. VC5_OUT_DIV_CONTROL_EN_FOD;
  541. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  542. VC5_OUT_DIV_CONTROL_SEL_EXT;
  543. u8 src = VC5_OUT_DIV_CONTROL_RESET;
  544. if (index == 0)
  545. src |= VC5_OUT_DIV_CONTROL_EN_FOD;
  546. else
  547. src |= extclk;
  548. return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
  549. mask, src);
  550. }
  551. static const struct clk_ops vc5_clk_out_ops = {
  552. .prepare = vc5_clk_out_prepare,
  553. .unprepare = vc5_clk_out_unprepare,
  554. .set_parent = vc5_clk_out_set_parent,
  555. .get_parent = vc5_clk_out_get_parent,
  556. };
  557. static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
  558. void *data)
  559. {
  560. struct vc5_driver_data *vc5 = data;
  561. unsigned int idx = clkspec->args[0];
  562. if (idx >= vc5->chip_info->clk_out_cnt)
  563. return ERR_PTR(-EINVAL);
  564. return &vc5->clk_out[idx].hw;
  565. }
  566. static int vc5_map_index_to_output(const enum vc5_model model,
  567. const unsigned int n)
  568. {
  569. switch (model) {
  570. case IDT_VC5_5P49V5933:
  571. return (n == 0) ? 0 : 3;
  572. case IDT_VC5_5P49V5923:
  573. case IDT_VC5_5P49V5925:
  574. case IDT_VC5_5P49V5935:
  575. case IDT_VC6_5P49V6901:
  576. default:
  577. return n;
  578. }
  579. }
  580. static const struct of_device_id clk_vc5_of_match[];
  581. static int vc5_probe(struct i2c_client *client,
  582. const struct i2c_device_id *id)
  583. {
  584. struct vc5_driver_data *vc5;
  585. struct clk_init_data init;
  586. const char *parent_names[2];
  587. unsigned int n, idx = 0;
  588. int ret;
  589. vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
  590. if (vc5 == NULL)
  591. return -ENOMEM;
  592. i2c_set_clientdata(client, vc5);
  593. vc5->client = client;
  594. vc5->chip_info = of_device_get_match_data(&client->dev);
  595. vc5->pin_xin = devm_clk_get(&client->dev, "xin");
  596. if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
  597. return -EPROBE_DEFER;
  598. vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
  599. if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
  600. return -EPROBE_DEFER;
  601. vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
  602. if (IS_ERR(vc5->regmap)) {
  603. dev_err(&client->dev, "failed to allocate register map\n");
  604. return PTR_ERR(vc5->regmap);
  605. }
  606. /* Register clock input mux */
  607. memset(&init, 0, sizeof(init));
  608. if (!IS_ERR(vc5->pin_xin)) {
  609. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  610. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  611. } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
  612. vc5->pin_xin = clk_register_fixed_rate(&client->dev,
  613. "internal-xtal", NULL,
  614. 0, 25000000);
  615. if (IS_ERR(vc5->pin_xin))
  616. return PTR_ERR(vc5->pin_xin);
  617. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  618. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  619. }
  620. if (!IS_ERR(vc5->pin_clkin)) {
  621. vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
  622. parent_names[init.num_parents++] =
  623. __clk_get_name(vc5->pin_clkin);
  624. }
  625. if (!init.num_parents) {
  626. dev_err(&client->dev, "no input clock specified!\n");
  627. return -EINVAL;
  628. }
  629. init.name = vc5_mux_names[0];
  630. init.ops = &vc5_mux_ops;
  631. init.flags = 0;
  632. init.parent_names = parent_names;
  633. vc5->clk_mux.init = &init;
  634. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
  635. if (ret) {
  636. dev_err(&client->dev, "unable to register %s\n", init.name);
  637. goto err_clk;
  638. }
  639. if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL) {
  640. /* Register frequency doubler */
  641. memset(&init, 0, sizeof(init));
  642. init.name = vc5_dbl_names[0];
  643. init.ops = &vc5_dbl_ops;
  644. init.flags = CLK_SET_RATE_PARENT;
  645. init.parent_names = vc5_mux_names;
  646. init.num_parents = 1;
  647. vc5->clk_mul.init = &init;
  648. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mul);
  649. if (ret) {
  650. dev_err(&client->dev, "unable to register %s\n",
  651. init.name);
  652. goto err_clk;
  653. }
  654. }
  655. /* Register PFD */
  656. memset(&init, 0, sizeof(init));
  657. init.name = vc5_pfd_names[0];
  658. init.ops = &vc5_pfd_ops;
  659. init.flags = CLK_SET_RATE_PARENT;
  660. if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL)
  661. init.parent_names = vc5_dbl_names;
  662. else
  663. init.parent_names = vc5_mux_names;
  664. init.num_parents = 1;
  665. vc5->clk_pfd.init = &init;
  666. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
  667. if (ret) {
  668. dev_err(&client->dev, "unable to register %s\n", init.name);
  669. goto err_clk;
  670. }
  671. /* Register PLL */
  672. memset(&init, 0, sizeof(init));
  673. init.name = vc5_pll_names[0];
  674. init.ops = &vc5_pll_ops;
  675. init.flags = CLK_SET_RATE_PARENT;
  676. init.parent_names = vc5_pfd_names;
  677. init.num_parents = 1;
  678. vc5->clk_pll.num = 0;
  679. vc5->clk_pll.vc5 = vc5;
  680. vc5->clk_pll.hw.init = &init;
  681. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
  682. if (ret) {
  683. dev_err(&client->dev, "unable to register %s\n", init.name);
  684. goto err_clk;
  685. }
  686. /* Register FODs */
  687. for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
  688. idx = vc5_map_index_to_output(vc5->chip_info->model, n);
  689. memset(&init, 0, sizeof(init));
  690. init.name = vc5_fod_names[idx];
  691. init.ops = &vc5_fod_ops;
  692. init.flags = CLK_SET_RATE_PARENT;
  693. init.parent_names = vc5_pll_names;
  694. init.num_parents = 1;
  695. vc5->clk_fod[n].num = idx;
  696. vc5->clk_fod[n].vc5 = vc5;
  697. vc5->clk_fod[n].hw.init = &init;
  698. ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
  699. if (ret) {
  700. dev_err(&client->dev, "unable to register %s\n",
  701. init.name);
  702. goto err_clk;
  703. }
  704. }
  705. /* Register MUX-connected OUT0_I2C_SELB output */
  706. memset(&init, 0, sizeof(init));
  707. init.name = vc5_clk_out_names[0];
  708. init.ops = &vc5_clk_out_ops;
  709. init.flags = CLK_SET_RATE_PARENT;
  710. init.parent_names = vc5_mux_names;
  711. init.num_parents = 1;
  712. vc5->clk_out[0].num = idx;
  713. vc5->clk_out[0].vc5 = vc5;
  714. vc5->clk_out[0].hw.init = &init;
  715. ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
  716. if (ret) {
  717. dev_err(&client->dev, "unable to register %s\n",
  718. init.name);
  719. goto err_clk;
  720. }
  721. /* Register FOD-connected OUTx outputs */
  722. for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
  723. idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
  724. parent_names[0] = vc5_fod_names[idx];
  725. if (n == 1)
  726. parent_names[1] = vc5_mux_names[0];
  727. else
  728. parent_names[1] = vc5_clk_out_names[n - 1];
  729. memset(&init, 0, sizeof(init));
  730. init.name = vc5_clk_out_names[idx + 1];
  731. init.ops = &vc5_clk_out_ops;
  732. init.flags = CLK_SET_RATE_PARENT;
  733. init.parent_names = parent_names;
  734. init.num_parents = 2;
  735. vc5->clk_out[n].num = idx;
  736. vc5->clk_out[n].vc5 = vc5;
  737. vc5->clk_out[n].hw.init = &init;
  738. ret = devm_clk_hw_register(&client->dev,
  739. &vc5->clk_out[n].hw);
  740. if (ret) {
  741. dev_err(&client->dev, "unable to register %s\n",
  742. init.name);
  743. goto err_clk;
  744. }
  745. }
  746. ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
  747. if (ret) {
  748. dev_err(&client->dev, "unable to add clk provider\n");
  749. goto err_clk;
  750. }
  751. return 0;
  752. err_clk:
  753. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  754. clk_unregister_fixed_rate(vc5->pin_xin);
  755. return ret;
  756. }
  757. static int vc5_remove(struct i2c_client *client)
  758. {
  759. struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
  760. of_clk_del_provider(client->dev.of_node);
  761. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  762. clk_unregister_fixed_rate(vc5->pin_xin);
  763. return 0;
  764. }
  765. static const struct vc5_chip_info idt_5p49v5923_info = {
  766. .model = IDT_VC5_5P49V5923,
  767. .clk_fod_cnt = 2,
  768. .clk_out_cnt = 3,
  769. .flags = 0,
  770. };
  771. static const struct vc5_chip_info idt_5p49v5925_info = {
  772. .model = IDT_VC5_5P49V5925,
  773. .clk_fod_cnt = 4,
  774. .clk_out_cnt = 5,
  775. .flags = 0,
  776. };
  777. static const struct vc5_chip_info idt_5p49v5933_info = {
  778. .model = IDT_VC5_5P49V5933,
  779. .clk_fod_cnt = 2,
  780. .clk_out_cnt = 3,
  781. .flags = VC5_HAS_INTERNAL_XTAL,
  782. };
  783. static const struct vc5_chip_info idt_5p49v5935_info = {
  784. .model = IDT_VC5_5P49V5935,
  785. .clk_fod_cnt = 4,
  786. .clk_out_cnt = 5,
  787. .flags = VC5_HAS_INTERNAL_XTAL,
  788. };
  789. static const struct vc5_chip_info idt_5p49v6901_info = {
  790. .model = IDT_VC6_5P49V6901,
  791. .clk_fod_cnt = 4,
  792. .clk_out_cnt = 5,
  793. .flags = VC5_HAS_PFD_FREQ_DBL,
  794. };
  795. static const struct i2c_device_id vc5_id[] = {
  796. { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
  797. { "5p49v5925", .driver_data = IDT_VC5_5P49V5925 },
  798. { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
  799. { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
  800. { "5p49v6901", .driver_data = IDT_VC6_5P49V6901 },
  801. { }
  802. };
  803. MODULE_DEVICE_TABLE(i2c, vc5_id);
  804. static const struct of_device_id clk_vc5_of_match[] = {
  805. { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
  806. { .compatible = "idt,5p49v5925", .data = &idt_5p49v5925_info },
  807. { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
  808. { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
  809. { .compatible = "idt,5p49v6901", .data = &idt_5p49v6901_info },
  810. { },
  811. };
  812. MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
  813. static struct i2c_driver vc5_driver = {
  814. .driver = {
  815. .name = "vc5",
  816. .of_match_table = clk_vc5_of_match,
  817. },
  818. .probe = vc5_probe,
  819. .remove = vc5_remove,
  820. .id_table = vc5_id,
  821. };
  822. module_i2c_driver(vc5_driver);
  823. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  824. MODULE_DESCRIPTION("IDT VersaClock 5 driver");
  825. MODULE_LICENSE("GPL");