clk-versaclock5.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
  222. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  223. }
  224. return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
  225. }
  226. static const struct clk_ops vc5_mux_ops = {
  227. .set_parent = vc5_mux_set_parent,
  228. .get_parent = vc5_mux_get_parent,
  229. };
  230. static unsigned long vc5_dbl_recalc_rate(struct clk_hw *hw,
  231. unsigned long parent_rate)
  232. {
  233. struct vc5_driver_data *vc5 =
  234. container_of(hw, struct vc5_driver_data, clk_mul);
  235. unsigned int premul;
  236. regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &premul);
  237. if (premul & VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ)
  238. parent_rate *= 2;
  239. return parent_rate;
  240. }
  241. static long vc5_dbl_round_rate(struct clk_hw *hw, unsigned long rate,
  242. unsigned long *parent_rate)
  243. {
  244. if ((*parent_rate == rate) || ((*parent_rate * 2) == rate))
  245. return rate;
  246. else
  247. return -EINVAL;
  248. }
  249. static int vc5_dbl_set_rate(struct clk_hw *hw, unsigned long rate,
  250. unsigned long parent_rate)
  251. {
  252. struct vc5_driver_data *vc5 =
  253. container_of(hw, struct vc5_driver_data, clk_mul);
  254. u32 mask;
  255. if ((parent_rate * 2) == rate)
  256. mask = VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ;
  257. else
  258. mask = 0;
  259. regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN,
  260. VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ,
  261. mask);
  262. return 0;
  263. }
  264. static const struct clk_ops vc5_dbl_ops = {
  265. .recalc_rate = vc5_dbl_recalc_rate,
  266. .round_rate = vc5_dbl_round_rate,
  267. .set_rate = vc5_dbl_set_rate,
  268. };
  269. static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
  270. unsigned long parent_rate)
  271. {
  272. struct vc5_driver_data *vc5 =
  273. container_of(hw, struct vc5_driver_data, clk_pfd);
  274. unsigned int prediv, div;
  275. regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
  276. /* The bypass_prediv is set, PLL fed from Ref_in directly. */
  277. if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
  278. return parent_rate;
  279. regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
  280. /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
  281. if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
  282. return parent_rate / 2;
  283. else
  284. return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
  285. }
  286. static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
  287. unsigned long *parent_rate)
  288. {
  289. unsigned long idiv;
  290. /* PLL cannot operate with input clock above 50 MHz. */
  291. if (rate > 50000000)
  292. return -EINVAL;
  293. /* CLKIN within range of PLL input, feed directly to PLL. */
  294. if (*parent_rate <= 50000000)
  295. return *parent_rate;
  296. idiv = DIV_ROUND_UP(*parent_rate, rate);
  297. if (idiv > 127)
  298. return -EINVAL;
  299. return *parent_rate / idiv;
  300. }
  301. static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
  302. unsigned long parent_rate)
  303. {
  304. struct vc5_driver_data *vc5 =
  305. container_of(hw, struct vc5_driver_data, clk_pfd);
  306. unsigned long idiv;
  307. u8 div;
  308. /* CLKIN within range of PLL input, feed directly to PLL. */
  309. if (parent_rate <= 50000000) {
  310. regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  311. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
  312. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
  313. regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
  314. return 0;
  315. }
  316. idiv = DIV_ROUND_UP(parent_rate, rate);
  317. /* We have dedicated div-2 predivider. */
  318. if (idiv == 2)
  319. div = VC5_REF_DIVIDER_SEL_PREDIV2;
  320. else
  321. div = VC5_REF_DIVIDER_REF_DIV(idiv);
  322. regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
  323. regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  324. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
  325. return 0;
  326. }
  327. static const struct clk_ops vc5_pfd_ops = {
  328. .recalc_rate = vc5_pfd_recalc_rate,
  329. .round_rate = vc5_pfd_round_rate,
  330. .set_rate = vc5_pfd_set_rate,
  331. };
  332. /*
  333. * VersaClock5 PLL/VCO
  334. */
  335. static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
  336. unsigned long parent_rate)
  337. {
  338. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  339. struct vc5_driver_data *vc5 = hwdata->vc5;
  340. u32 div_int, div_frc;
  341. u8 fb[5];
  342. regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  343. div_int = (fb[0] << 4) | (fb[1] >> 4);
  344. div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
  345. /* The PLL divider has 12 integer bits and 24 fractional bits */
  346. return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
  347. }
  348. static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  349. unsigned long *parent_rate)
  350. {
  351. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  352. u32 div_int;
  353. u64 div_frc;
  354. if (rate < VC5_PLL_VCO_MIN)
  355. rate = VC5_PLL_VCO_MIN;
  356. if (rate > VC5_PLL_VCO_MAX)
  357. rate = VC5_PLL_VCO_MAX;
  358. /* Determine integer part, which is 12 bit wide */
  359. div_int = rate / *parent_rate;
  360. if (div_int > 0xfff)
  361. rate = *parent_rate * 0xfff;
  362. /* Determine best fractional part, which is 24 bit wide */
  363. div_frc = rate % *parent_rate;
  364. div_frc *= BIT(24) - 1;
  365. do_div(div_frc, *parent_rate);
  366. hwdata->div_int = div_int;
  367. hwdata->div_frc = (u32)div_frc;
  368. return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
  369. }
  370. static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  371. unsigned long parent_rate)
  372. {
  373. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  374. struct vc5_driver_data *vc5 = hwdata->vc5;
  375. u8 fb[5];
  376. fb[0] = hwdata->div_int >> 4;
  377. fb[1] = hwdata->div_int << 4;
  378. fb[2] = hwdata->div_frc >> 16;
  379. fb[3] = hwdata->div_frc >> 8;
  380. fb[4] = hwdata->div_frc;
  381. return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  382. }
  383. static const struct clk_ops vc5_pll_ops = {
  384. .recalc_rate = vc5_pll_recalc_rate,
  385. .round_rate = vc5_pll_round_rate,
  386. .set_rate = vc5_pll_set_rate,
  387. };
  388. static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
  389. unsigned long parent_rate)
  390. {
  391. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  392. struct vc5_driver_data *vc5 = hwdata->vc5;
  393. /* VCO frequency is divided by two before entering FOD */
  394. u32 f_in = parent_rate / 2;
  395. u32 div_int, div_frc;
  396. u8 od_int[2];
  397. u8 od_frc[4];
  398. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
  399. od_int, 2);
  400. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  401. od_frc, 4);
  402. div_int = (od_int[0] << 4) | (od_int[1] >> 4);
  403. div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
  404. (od_frc[2] << 6) | (od_frc[3] >> 2);
  405. /* Avoid division by zero if the output is not configured. */
  406. if (div_int == 0 && div_frc == 0)
  407. return 0;
  408. /* The PLL divider has 12 integer bits and 30 fractional bits */
  409. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  410. }
  411. static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
  412. unsigned long *parent_rate)
  413. {
  414. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  415. /* VCO frequency is divided by two before entering FOD */
  416. u32 f_in = *parent_rate / 2;
  417. u32 div_int;
  418. u64 div_frc;
  419. /* Determine integer part, which is 12 bit wide */
  420. div_int = f_in / rate;
  421. /*
  422. * WARNING: The clock chip does not output signal if the integer part
  423. * of the divider is 0xfff and fractional part is non-zero.
  424. * Clamp the divider at 0xffe to keep the code simple.
  425. */
  426. if (div_int > 0xffe) {
  427. div_int = 0xffe;
  428. rate = f_in / div_int;
  429. }
  430. /* Determine best fractional part, which is 30 bit wide */
  431. div_frc = f_in % rate;
  432. div_frc <<= 24;
  433. do_div(div_frc, rate);
  434. hwdata->div_int = div_int;
  435. hwdata->div_frc = (u32)div_frc;
  436. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  437. }
  438. static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
  439. unsigned long parent_rate)
  440. {
  441. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  442. struct vc5_driver_data *vc5 = hwdata->vc5;
  443. u8 data[14] = {
  444. hwdata->div_frc >> 22, hwdata->div_frc >> 14,
  445. hwdata->div_frc >> 6, hwdata->div_frc << 2,
  446. 0, 0, 0, 0, 0,
  447. 0, 0,
  448. hwdata->div_int >> 4, hwdata->div_int << 4,
  449. 0
  450. };
  451. regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  452. data, 14);
  453. /*
  454. * Toggle magic bit in undocumented register for unknown reason.
  455. * This is what the IDT timing commander tool does and the chip
  456. * datasheet somewhat implies this is needed, but the register
  457. * and the bit is not documented.
  458. */
  459. regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  460. VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
  461. regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  462. VC5_GLOBAL_REGISTER_GLOBAL_RESET,
  463. VC5_GLOBAL_REGISTER_GLOBAL_RESET);
  464. return 0;
  465. }
  466. static const struct clk_ops vc5_fod_ops = {
  467. .recalc_rate = vc5_fod_recalc_rate,
  468. .round_rate = vc5_fod_round_rate,
  469. .set_rate = vc5_fod_set_rate,
  470. };
  471. static int vc5_clk_out_prepare(struct clk_hw *hw)
  472. {
  473. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  474. struct vc5_driver_data *vc5 = hwdata->vc5;
  475. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  476. VC5_OUT_DIV_CONTROL_SEL_EXT |
  477. VC5_OUT_DIV_CONTROL_EN_FOD;
  478. unsigned int src;
  479. int ret;
  480. /*
  481. * If the input mux is disabled, enable it first and
  482. * select source from matching FOD.
  483. */
  484. regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  485. if ((src & mask) == 0) {
  486. src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
  487. ret = regmap_update_bits(vc5->regmap,
  488. VC5_OUT_DIV_CONTROL(hwdata->num),
  489. mask | VC5_OUT_DIV_CONTROL_RESET, src);
  490. if (ret)
  491. return ret;
  492. }
  493. /* Enable the clock buffer */
  494. regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  495. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
  496. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
  497. return 0;
  498. }
  499. static void vc5_clk_out_unprepare(struct clk_hw *hw)
  500. {
  501. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  502. struct vc5_driver_data *vc5 = hwdata->vc5;
  503. /* Disable the clock buffer */
  504. regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  505. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
  506. }
  507. static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
  508. {
  509. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  510. struct vc5_driver_data *vc5 = hwdata->vc5;
  511. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  512. VC5_OUT_DIV_CONTROL_SEL_EXT |
  513. VC5_OUT_DIV_CONTROL_EN_FOD;
  514. const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  515. VC5_OUT_DIV_CONTROL_EN_FOD;
  516. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  517. VC5_OUT_DIV_CONTROL_SEL_EXT;
  518. unsigned int src;
  519. regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  520. src &= mask;
  521. if (src == 0) /* Input mux set to DISABLED */
  522. return 0;
  523. if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
  524. return 0;
  525. if (src == extclk)
  526. return 1;
  527. dev_warn(&vc5->client->dev,
  528. "Invalid clock output configuration (%02x)\n", src);
  529. return 0;
  530. }
  531. static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
  532. {
  533. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  534. struct vc5_driver_data *vc5 = hwdata->vc5;
  535. const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
  536. VC5_OUT_DIV_CONTROL_SELB_NORM |
  537. VC5_OUT_DIV_CONTROL_SEL_EXT |
  538. VC5_OUT_DIV_CONTROL_EN_FOD;
  539. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  540. VC5_OUT_DIV_CONTROL_SEL_EXT;
  541. u8 src = VC5_OUT_DIV_CONTROL_RESET;
  542. if (index == 0)
  543. src |= VC5_OUT_DIV_CONTROL_EN_FOD;
  544. else
  545. src |= extclk;
  546. return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
  547. mask, src);
  548. }
  549. static const struct clk_ops vc5_clk_out_ops = {
  550. .prepare = vc5_clk_out_prepare,
  551. .unprepare = vc5_clk_out_unprepare,
  552. .set_parent = vc5_clk_out_set_parent,
  553. .get_parent = vc5_clk_out_get_parent,
  554. };
  555. static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
  556. void *data)
  557. {
  558. struct vc5_driver_data *vc5 = data;
  559. unsigned int idx = clkspec->args[0];
  560. if (idx >= vc5->chip_info->clk_out_cnt)
  561. return ERR_PTR(-EINVAL);
  562. return &vc5->clk_out[idx].hw;
  563. }
  564. static int vc5_map_index_to_output(const enum vc5_model model,
  565. const unsigned int n)
  566. {
  567. switch (model) {
  568. case IDT_VC5_5P49V5933:
  569. return (n == 0) ? 0 : 3;
  570. case IDT_VC5_5P49V5923:
  571. case IDT_VC5_5P49V5925:
  572. case IDT_VC5_5P49V5935:
  573. case IDT_VC6_5P49V6901:
  574. default:
  575. return n;
  576. }
  577. }
  578. static const struct of_device_id clk_vc5_of_match[];
  579. static int vc5_probe(struct i2c_client *client,
  580. const struct i2c_device_id *id)
  581. {
  582. struct vc5_driver_data *vc5;
  583. struct clk_init_data init;
  584. const char *parent_names[2];
  585. unsigned int n, idx = 0;
  586. int ret;
  587. vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
  588. if (vc5 == NULL)
  589. return -ENOMEM;
  590. i2c_set_clientdata(client, vc5);
  591. vc5->client = client;
  592. vc5->chip_info = of_device_get_match_data(&client->dev);
  593. vc5->pin_xin = devm_clk_get(&client->dev, "xin");
  594. if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
  595. return -EPROBE_DEFER;
  596. vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
  597. if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
  598. return -EPROBE_DEFER;
  599. vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
  600. if (IS_ERR(vc5->regmap)) {
  601. dev_err(&client->dev, "failed to allocate register map\n");
  602. return PTR_ERR(vc5->regmap);
  603. }
  604. /* Register clock input mux */
  605. memset(&init, 0, sizeof(init));
  606. if (!IS_ERR(vc5->pin_xin)) {
  607. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  608. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  609. } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
  610. vc5->pin_xin = clk_register_fixed_rate(&client->dev,
  611. "internal-xtal", NULL,
  612. 0, 25000000);
  613. if (IS_ERR(vc5->pin_xin))
  614. return PTR_ERR(vc5->pin_xin);
  615. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  616. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  617. }
  618. if (!IS_ERR(vc5->pin_clkin)) {
  619. vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
  620. parent_names[init.num_parents++] =
  621. __clk_get_name(vc5->pin_clkin);
  622. }
  623. if (!init.num_parents) {
  624. dev_err(&client->dev, "no input clock specified!\n");
  625. return -EINVAL;
  626. }
  627. init.name = vc5_mux_names[0];
  628. init.ops = &vc5_mux_ops;
  629. init.flags = 0;
  630. init.parent_names = parent_names;
  631. vc5->clk_mux.init = &init;
  632. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
  633. if (ret) {
  634. dev_err(&client->dev, "unable to register %s\n", init.name);
  635. goto err_clk;
  636. }
  637. if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL) {
  638. /* Register frequency doubler */
  639. memset(&init, 0, sizeof(init));
  640. init.name = vc5_dbl_names[0];
  641. init.ops = &vc5_dbl_ops;
  642. init.flags = CLK_SET_RATE_PARENT;
  643. init.parent_names = vc5_mux_names;
  644. init.num_parents = 1;
  645. vc5->clk_mul.init = &init;
  646. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mul);
  647. if (ret) {
  648. dev_err(&client->dev, "unable to register %s\n",
  649. init.name);
  650. goto err_clk;
  651. }
  652. }
  653. /* Register PFD */
  654. memset(&init, 0, sizeof(init));
  655. init.name = vc5_pfd_names[0];
  656. init.ops = &vc5_pfd_ops;
  657. init.flags = CLK_SET_RATE_PARENT;
  658. if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL)
  659. init.parent_names = vc5_dbl_names;
  660. else
  661. init.parent_names = vc5_mux_names;
  662. init.num_parents = 1;
  663. vc5->clk_pfd.init = &init;
  664. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
  665. if (ret) {
  666. dev_err(&client->dev, "unable to register %s\n", init.name);
  667. goto err_clk;
  668. }
  669. /* Register PLL */
  670. memset(&init, 0, sizeof(init));
  671. init.name = vc5_pll_names[0];
  672. init.ops = &vc5_pll_ops;
  673. init.flags = CLK_SET_RATE_PARENT;
  674. init.parent_names = vc5_pfd_names;
  675. init.num_parents = 1;
  676. vc5->clk_pll.num = 0;
  677. vc5->clk_pll.vc5 = vc5;
  678. vc5->clk_pll.hw.init = &init;
  679. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
  680. if (ret) {
  681. dev_err(&client->dev, "unable to register %s\n", init.name);
  682. goto err_clk;
  683. }
  684. /* Register FODs */
  685. for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
  686. idx = vc5_map_index_to_output(vc5->chip_info->model, n);
  687. memset(&init, 0, sizeof(init));
  688. init.name = vc5_fod_names[idx];
  689. init.ops = &vc5_fod_ops;
  690. init.flags = CLK_SET_RATE_PARENT;
  691. init.parent_names = vc5_pll_names;
  692. init.num_parents = 1;
  693. vc5->clk_fod[n].num = idx;
  694. vc5->clk_fod[n].vc5 = vc5;
  695. vc5->clk_fod[n].hw.init = &init;
  696. ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
  697. if (ret) {
  698. dev_err(&client->dev, "unable to register %s\n",
  699. init.name);
  700. goto err_clk;
  701. }
  702. }
  703. /* Register MUX-connected OUT0_I2C_SELB output */
  704. memset(&init, 0, sizeof(init));
  705. init.name = vc5_clk_out_names[0];
  706. init.ops = &vc5_clk_out_ops;
  707. init.flags = CLK_SET_RATE_PARENT;
  708. init.parent_names = vc5_mux_names;
  709. init.num_parents = 1;
  710. vc5->clk_out[0].num = idx;
  711. vc5->clk_out[0].vc5 = vc5;
  712. vc5->clk_out[0].hw.init = &init;
  713. ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
  714. if (ret) {
  715. dev_err(&client->dev, "unable to register %s\n",
  716. init.name);
  717. goto err_clk;
  718. }
  719. /* Register FOD-connected OUTx outputs */
  720. for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
  721. idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
  722. parent_names[0] = vc5_fod_names[idx];
  723. if (n == 1)
  724. parent_names[1] = vc5_mux_names[0];
  725. else
  726. parent_names[1] = vc5_clk_out_names[n - 1];
  727. memset(&init, 0, sizeof(init));
  728. init.name = vc5_clk_out_names[idx + 1];
  729. init.ops = &vc5_clk_out_ops;
  730. init.flags = CLK_SET_RATE_PARENT;
  731. init.parent_names = parent_names;
  732. init.num_parents = 2;
  733. vc5->clk_out[n].num = idx;
  734. vc5->clk_out[n].vc5 = vc5;
  735. vc5->clk_out[n].hw.init = &init;
  736. ret = devm_clk_hw_register(&client->dev,
  737. &vc5->clk_out[n].hw);
  738. if (ret) {
  739. dev_err(&client->dev, "unable to register %s\n",
  740. init.name);
  741. goto err_clk;
  742. }
  743. }
  744. ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
  745. if (ret) {
  746. dev_err(&client->dev, "unable to add clk provider\n");
  747. goto err_clk;
  748. }
  749. return 0;
  750. err_clk:
  751. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  752. clk_unregister_fixed_rate(vc5->pin_xin);
  753. return ret;
  754. }
  755. static int vc5_remove(struct i2c_client *client)
  756. {
  757. struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
  758. of_clk_del_provider(client->dev.of_node);
  759. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  760. clk_unregister_fixed_rate(vc5->pin_xin);
  761. return 0;
  762. }
  763. static const struct vc5_chip_info idt_5p49v5923_info = {
  764. .model = IDT_VC5_5P49V5923,
  765. .clk_fod_cnt = 2,
  766. .clk_out_cnt = 3,
  767. .flags = 0,
  768. };
  769. static const struct vc5_chip_info idt_5p49v5925_info = {
  770. .model = IDT_VC5_5P49V5925,
  771. .clk_fod_cnt = 4,
  772. .clk_out_cnt = 5,
  773. .flags = 0,
  774. };
  775. static const struct vc5_chip_info idt_5p49v5933_info = {
  776. .model = IDT_VC5_5P49V5933,
  777. .clk_fod_cnt = 2,
  778. .clk_out_cnt = 3,
  779. .flags = VC5_HAS_INTERNAL_XTAL,
  780. };
  781. static const struct vc5_chip_info idt_5p49v5935_info = {
  782. .model = IDT_VC5_5P49V5935,
  783. .clk_fod_cnt = 4,
  784. .clk_out_cnt = 5,
  785. .flags = VC5_HAS_INTERNAL_XTAL,
  786. };
  787. static const struct vc5_chip_info idt_5p49v6901_info = {
  788. .model = IDT_VC6_5P49V6901,
  789. .clk_fod_cnt = 4,
  790. .clk_out_cnt = 5,
  791. .flags = VC5_HAS_PFD_FREQ_DBL,
  792. };
  793. static const struct i2c_device_id vc5_id[] = {
  794. { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
  795. { "5p49v5925", .driver_data = IDT_VC5_5P49V5925 },
  796. { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
  797. { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
  798. { "5p49v6901", .driver_data = IDT_VC6_5P49V6901 },
  799. { }
  800. };
  801. MODULE_DEVICE_TABLE(i2c, vc5_id);
  802. static const struct of_device_id clk_vc5_of_match[] = {
  803. { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
  804. { .compatible = "idt,5p49v5925", .data = &idt_5p49v5925_info },
  805. { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
  806. { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
  807. { .compatible = "idt,5p49v6901", .data = &idt_5p49v6901_info },
  808. { },
  809. };
  810. MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
  811. static struct i2c_driver vc5_driver = {
  812. .driver = {
  813. .name = "vc5",
  814. .of_match_table = clk_vc5_of_match,
  815. },
  816. .probe = vc5_probe,
  817. .remove = vc5_remove,
  818. .id_table = vc5_id,
  819. };
  820. module_i2c_driver(vc5_driver);
  821. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  822. MODULE_DESCRIPTION("IDT VersaClock 5 driver");
  823. MODULE_LICENSE("GPL");