clk-versaclock5.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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_SP BIT(1)
  56. #define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
  57. #define VC5_VCO_BAND 0x11
  58. #define VC5_XTAL_X1_LOAD_CAP 0x12
  59. #define VC5_XTAL_X2_LOAD_CAP 0x13
  60. #define VC5_REF_DIVIDER 0x15
  61. #define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
  62. #define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
  63. #define VC5_VCO_CTRL_AND_PREDIV 0x16
  64. #define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
  65. #define VC5_FEEDBACK_INT_DIV 0x17
  66. #define VC5_FEEDBACK_INT_DIV_BITS 0x18
  67. #define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
  68. #define VC5_RC_CONTROL0 0x1e
  69. #define VC5_RC_CONTROL1 0x1f
  70. /* Register 0x20 is factory reserved */
  71. /* Output divider control for divider 1,2,3,4 */
  72. #define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
  73. #define VC5_OUT_DIV_CONTROL_RESET BIT(7)
  74. #define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
  75. #define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
  76. #define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
  77. #define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
  78. #define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
  79. #define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
  80. #define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
  81. #define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
  82. #define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
  83. #define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
  84. #define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
  85. /* Registers 0x30, 0x40, 0x50 are factory reserved */
  86. /* Clock control register for clock 1,2 */
  87. #define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
  88. #define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
  89. #define VC5_CLK_OE_SHDN 0x68
  90. #define VC5_CLK_OS_SHDN 0x69
  91. #define VC5_GLOBAL_REGISTER 0x76
  92. #define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
  93. /* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
  94. #define VC5_PLL_VCO_MIN 2500000000UL
  95. #define VC5_PLL_VCO_MAX 3000000000UL
  96. /* VC5 Input mux settings */
  97. #define VC5_MUX_IN_XIN BIT(0)
  98. #define VC5_MUX_IN_CLKIN BIT(1)
  99. /* Maximum number of clk_out supported by this driver */
  100. #define VC5_MAX_CLK_OUT_NUM 5
  101. /* Maximum number of FODs supported by this driver */
  102. #define VC5_MAX_FOD_NUM 4
  103. /* flags to describe chip features */
  104. /* chip has built-in oscilator */
  105. #define VC5_HAS_INTERNAL_XTAL BIT(0)
  106. /* Supported IDT VC5 models. */
  107. enum vc5_model {
  108. IDT_VC5_5P49V5923,
  109. IDT_VC5_5P49V5933,
  110. IDT_VC5_5P49V5935,
  111. };
  112. /* Structure to describe features of a particular VC5 model */
  113. struct vc5_chip_info {
  114. const enum vc5_model model;
  115. const unsigned int clk_fod_cnt;
  116. const unsigned int clk_out_cnt;
  117. const u32 flags;
  118. };
  119. struct vc5_driver_data;
  120. struct vc5_hw_data {
  121. struct clk_hw hw;
  122. struct vc5_driver_data *vc5;
  123. u32 div_int;
  124. u32 div_frc;
  125. unsigned int num;
  126. };
  127. struct vc5_driver_data {
  128. struct i2c_client *client;
  129. struct regmap *regmap;
  130. const struct vc5_chip_info *chip_info;
  131. struct clk *pin_xin;
  132. struct clk *pin_clkin;
  133. unsigned char clk_mux_ins;
  134. struct clk_hw clk_mux;
  135. struct vc5_hw_data clk_pll;
  136. struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
  137. struct vc5_hw_data clk_out[VC5_MAX_CLK_OUT_NUM];
  138. };
  139. static const char * const vc5_mux_names[] = {
  140. "mux"
  141. };
  142. static const char * const vc5_pll_names[] = {
  143. "pll"
  144. };
  145. static const char * const vc5_fod_names[] = {
  146. "fod0", "fod1", "fod2", "fod3",
  147. };
  148. static const char * const vc5_clk_out_names[] = {
  149. "out0_sel_i2cb", "out1", "out2", "out3", "out4",
  150. };
  151. /*
  152. * VersaClock5 i2c regmap
  153. */
  154. static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
  155. {
  156. /* Factory reserved regs, make them read-only */
  157. if (reg <= 0xf)
  158. return false;
  159. /* Factory reserved regs, make them read-only */
  160. if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
  161. return false;
  162. return true;
  163. }
  164. static const struct regmap_config vc5_regmap_config = {
  165. .reg_bits = 8,
  166. .val_bits = 8,
  167. .cache_type = REGCACHE_RBTREE,
  168. .max_register = 0x76,
  169. .writeable_reg = vc5_regmap_is_writeable,
  170. };
  171. /*
  172. * VersaClock5 input multiplexer between XTAL and CLKIN divider
  173. */
  174. static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
  175. {
  176. struct vc5_driver_data *vc5 =
  177. container_of(hw, struct vc5_driver_data, clk_mux);
  178. const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
  179. unsigned int src;
  180. regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
  181. src &= mask;
  182. if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
  183. return 0;
  184. if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
  185. return 1;
  186. dev_warn(&vc5->client->dev,
  187. "Invalid clock input configuration (%02x)\n", src);
  188. return 0;
  189. }
  190. static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
  191. {
  192. struct vc5_driver_data *vc5 =
  193. container_of(hw, struct vc5_driver_data, clk_mux);
  194. const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
  195. u8 src;
  196. if ((index > 1) || !vc5->clk_mux_ins)
  197. return -EINVAL;
  198. if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
  199. if (index == 0)
  200. src = VC5_PRIM_SRC_SHDN_EN_XTAL;
  201. if (index == 1)
  202. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  203. } else {
  204. if (index != 0)
  205. return -EINVAL;
  206. if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
  207. src = VC5_PRIM_SRC_SHDN_EN_XTAL;
  208. if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
  209. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  210. }
  211. return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
  212. }
  213. static unsigned long vc5_mux_recalc_rate(struct clk_hw *hw,
  214. unsigned long parent_rate)
  215. {
  216. struct vc5_driver_data *vc5 =
  217. container_of(hw, struct vc5_driver_data, clk_mux);
  218. unsigned int prediv, div;
  219. regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
  220. /* The bypass_prediv is set, PLL fed from Ref_in directly. */
  221. if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
  222. return parent_rate;
  223. regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
  224. /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
  225. if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
  226. return parent_rate / 2;
  227. else
  228. return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
  229. }
  230. static long vc5_mux_round_rate(struct clk_hw *hw, unsigned long rate,
  231. unsigned long *parent_rate)
  232. {
  233. unsigned long idiv;
  234. /* PLL cannot operate with input clock above 50 MHz. */
  235. if (rate > 50000000)
  236. return -EINVAL;
  237. /* CLKIN within range of PLL input, feed directly to PLL. */
  238. if (*parent_rate <= 50000000)
  239. return *parent_rate;
  240. idiv = DIV_ROUND_UP(*parent_rate, rate);
  241. if (idiv > 127)
  242. return -EINVAL;
  243. return *parent_rate / idiv;
  244. }
  245. static int vc5_mux_set_rate(struct clk_hw *hw, unsigned long rate,
  246. unsigned long parent_rate)
  247. {
  248. struct vc5_driver_data *vc5 =
  249. container_of(hw, struct vc5_driver_data, clk_mux);
  250. unsigned long idiv;
  251. u8 div;
  252. /* CLKIN within range of PLL input, feed directly to PLL. */
  253. if (parent_rate <= 50000000) {
  254. regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  255. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
  256. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
  257. regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
  258. return 0;
  259. }
  260. idiv = DIV_ROUND_UP(parent_rate, rate);
  261. /* We have dedicated div-2 predivider. */
  262. if (idiv == 2)
  263. div = VC5_REF_DIVIDER_SEL_PREDIV2;
  264. else
  265. div = VC5_REF_DIVIDER_REF_DIV(idiv);
  266. regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
  267. regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  268. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
  269. return 0;
  270. }
  271. static const struct clk_ops vc5_mux_ops = {
  272. .set_parent = vc5_mux_set_parent,
  273. .get_parent = vc5_mux_get_parent,
  274. .recalc_rate = vc5_mux_recalc_rate,
  275. .round_rate = vc5_mux_round_rate,
  276. .set_rate = vc5_mux_set_rate,
  277. };
  278. /*
  279. * VersaClock5 PLL/VCO
  280. */
  281. static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
  282. unsigned long parent_rate)
  283. {
  284. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  285. struct vc5_driver_data *vc5 = hwdata->vc5;
  286. u32 div_int, div_frc;
  287. u8 fb[5];
  288. regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  289. div_int = (fb[0] << 4) | (fb[1] >> 4);
  290. div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
  291. /* The PLL divider has 12 integer bits and 24 fractional bits */
  292. return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
  293. }
  294. static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  295. unsigned long *parent_rate)
  296. {
  297. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  298. u32 div_int;
  299. u64 div_frc;
  300. if (rate < VC5_PLL_VCO_MIN)
  301. rate = VC5_PLL_VCO_MIN;
  302. if (rate > VC5_PLL_VCO_MAX)
  303. rate = VC5_PLL_VCO_MAX;
  304. /* Determine integer part, which is 12 bit wide */
  305. div_int = rate / *parent_rate;
  306. if (div_int > 0xfff)
  307. rate = *parent_rate * 0xfff;
  308. /* Determine best fractional part, which is 24 bit wide */
  309. div_frc = rate % *parent_rate;
  310. div_frc *= BIT(24) - 1;
  311. do_div(div_frc, *parent_rate);
  312. hwdata->div_int = div_int;
  313. hwdata->div_frc = (u32)div_frc;
  314. return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
  315. }
  316. static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  317. unsigned long parent_rate)
  318. {
  319. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  320. struct vc5_driver_data *vc5 = hwdata->vc5;
  321. u8 fb[5];
  322. fb[0] = hwdata->div_int >> 4;
  323. fb[1] = hwdata->div_int << 4;
  324. fb[2] = hwdata->div_frc >> 16;
  325. fb[3] = hwdata->div_frc >> 8;
  326. fb[4] = hwdata->div_frc;
  327. return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  328. }
  329. static const struct clk_ops vc5_pll_ops = {
  330. .recalc_rate = vc5_pll_recalc_rate,
  331. .round_rate = vc5_pll_round_rate,
  332. .set_rate = vc5_pll_set_rate,
  333. };
  334. static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
  335. unsigned long parent_rate)
  336. {
  337. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  338. struct vc5_driver_data *vc5 = hwdata->vc5;
  339. /* VCO frequency is divided by two before entering FOD */
  340. u32 f_in = parent_rate / 2;
  341. u32 div_int, div_frc;
  342. u8 od_int[2];
  343. u8 od_frc[4];
  344. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
  345. od_int, 2);
  346. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  347. od_frc, 4);
  348. div_int = (od_int[0] << 4) | (od_int[1] >> 4);
  349. div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
  350. (od_frc[2] << 6) | (od_frc[3] >> 2);
  351. /* The PLL divider has 12 integer bits and 30 fractional bits */
  352. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  353. }
  354. static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
  355. unsigned long *parent_rate)
  356. {
  357. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  358. /* VCO frequency is divided by two before entering FOD */
  359. u32 f_in = *parent_rate / 2;
  360. u32 div_int;
  361. u64 div_frc;
  362. /* Determine integer part, which is 12 bit wide */
  363. div_int = f_in / rate;
  364. /*
  365. * WARNING: The clock chip does not output signal if the integer part
  366. * of the divider is 0xfff and fractional part is non-zero.
  367. * Clamp the divider at 0xffe to keep the code simple.
  368. */
  369. if (div_int > 0xffe) {
  370. div_int = 0xffe;
  371. rate = f_in / div_int;
  372. }
  373. /* Determine best fractional part, which is 30 bit wide */
  374. div_frc = f_in % rate;
  375. div_frc <<= 24;
  376. do_div(div_frc, rate);
  377. hwdata->div_int = div_int;
  378. hwdata->div_frc = (u32)div_frc;
  379. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  380. }
  381. static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
  382. unsigned long parent_rate)
  383. {
  384. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  385. struct vc5_driver_data *vc5 = hwdata->vc5;
  386. u8 data[14] = {
  387. hwdata->div_frc >> 22, hwdata->div_frc >> 14,
  388. hwdata->div_frc >> 6, hwdata->div_frc << 2,
  389. 0, 0, 0, 0, 0,
  390. 0, 0,
  391. hwdata->div_int >> 4, hwdata->div_int << 4,
  392. 0
  393. };
  394. regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  395. data, 14);
  396. /*
  397. * Toggle magic bit in undocumented register for unknown reason.
  398. * This is what the IDT timing commander tool does and the chip
  399. * datasheet somewhat implies this is needed, but the register
  400. * and the bit is not documented.
  401. */
  402. regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  403. VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
  404. regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  405. VC5_GLOBAL_REGISTER_GLOBAL_RESET,
  406. VC5_GLOBAL_REGISTER_GLOBAL_RESET);
  407. return 0;
  408. }
  409. static const struct clk_ops vc5_fod_ops = {
  410. .recalc_rate = vc5_fod_recalc_rate,
  411. .round_rate = vc5_fod_round_rate,
  412. .set_rate = vc5_fod_set_rate,
  413. };
  414. static int vc5_clk_out_prepare(struct clk_hw *hw)
  415. {
  416. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  417. struct vc5_driver_data *vc5 = hwdata->vc5;
  418. /* Enable the clock buffer */
  419. regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  420. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
  421. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
  422. return 0;
  423. }
  424. static void vc5_clk_out_unprepare(struct clk_hw *hw)
  425. {
  426. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  427. struct vc5_driver_data *vc5 = hwdata->vc5;
  428. /* Enable the clock buffer */
  429. regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  430. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
  431. }
  432. static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
  433. {
  434. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  435. struct vc5_driver_data *vc5 = hwdata->vc5;
  436. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  437. VC5_OUT_DIV_CONTROL_SEL_EXT |
  438. VC5_OUT_DIV_CONTROL_EN_FOD;
  439. const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  440. VC5_OUT_DIV_CONTROL_EN_FOD;
  441. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  442. VC5_OUT_DIV_CONTROL_SEL_EXT;
  443. unsigned int src;
  444. regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  445. src &= mask;
  446. if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
  447. return 0;
  448. if (src == extclk)
  449. return 1;
  450. dev_warn(&vc5->client->dev,
  451. "Invalid clock output configuration (%02x)\n", src);
  452. return 0;
  453. }
  454. static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
  455. {
  456. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  457. struct vc5_driver_data *vc5 = hwdata->vc5;
  458. const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
  459. VC5_OUT_DIV_CONTROL_SELB_NORM |
  460. VC5_OUT_DIV_CONTROL_SEL_EXT |
  461. VC5_OUT_DIV_CONTROL_EN_FOD;
  462. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  463. VC5_OUT_DIV_CONTROL_SEL_EXT;
  464. u8 src = VC5_OUT_DIV_CONTROL_RESET;
  465. if (index == 0)
  466. src |= VC5_OUT_DIV_CONTROL_EN_FOD;
  467. else
  468. src |= extclk;
  469. return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
  470. mask, src);
  471. }
  472. static const struct clk_ops vc5_clk_out_ops = {
  473. .prepare = vc5_clk_out_prepare,
  474. .unprepare = vc5_clk_out_unprepare,
  475. .set_parent = vc5_clk_out_set_parent,
  476. .get_parent = vc5_clk_out_get_parent,
  477. };
  478. static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
  479. void *data)
  480. {
  481. struct vc5_driver_data *vc5 = data;
  482. unsigned int idx = clkspec->args[0];
  483. if (idx >= vc5->chip_info->clk_out_cnt)
  484. return ERR_PTR(-EINVAL);
  485. return &vc5->clk_out[idx].hw;
  486. }
  487. static int vc5_map_index_to_output(const enum vc5_model model,
  488. const unsigned int n)
  489. {
  490. switch (model) {
  491. case IDT_VC5_5P49V5933:
  492. return (n == 0) ? 0 : 3;
  493. case IDT_VC5_5P49V5923:
  494. case IDT_VC5_5P49V5935:
  495. default:
  496. return n;
  497. }
  498. }
  499. static const struct of_device_id clk_vc5_of_match[];
  500. static int vc5_probe(struct i2c_client *client,
  501. const struct i2c_device_id *id)
  502. {
  503. struct vc5_driver_data *vc5;
  504. struct clk_init_data init;
  505. const char *parent_names[2];
  506. unsigned int n, idx = 0;
  507. int ret;
  508. vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
  509. if (vc5 == NULL)
  510. return -ENOMEM;
  511. i2c_set_clientdata(client, vc5);
  512. vc5->client = client;
  513. vc5->chip_info = of_device_get_match_data(&client->dev);
  514. vc5->pin_xin = devm_clk_get(&client->dev, "xin");
  515. if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
  516. return -EPROBE_DEFER;
  517. vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
  518. if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
  519. return -EPROBE_DEFER;
  520. vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
  521. if (IS_ERR(vc5->regmap)) {
  522. dev_err(&client->dev, "failed to allocate register map\n");
  523. return PTR_ERR(vc5->regmap);
  524. }
  525. /* Register clock input mux */
  526. memset(&init, 0, sizeof(init));
  527. if (!IS_ERR(vc5->pin_xin)) {
  528. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  529. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  530. } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
  531. vc5->pin_xin = clk_register_fixed_rate(&client->dev,
  532. "internal-xtal", NULL,
  533. 0, 25000000);
  534. if (IS_ERR(vc5->pin_xin))
  535. return PTR_ERR(vc5->pin_xin);
  536. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  537. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  538. }
  539. if (!IS_ERR(vc5->pin_clkin)) {
  540. vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
  541. parent_names[init.num_parents++] =
  542. __clk_get_name(vc5->pin_clkin);
  543. }
  544. if (!init.num_parents) {
  545. dev_err(&client->dev, "no input clock specified!\n");
  546. return -EINVAL;
  547. }
  548. init.name = vc5_mux_names[0];
  549. init.ops = &vc5_mux_ops;
  550. init.flags = 0;
  551. init.parent_names = parent_names;
  552. vc5->clk_mux.init = &init;
  553. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
  554. if (ret) {
  555. dev_err(&client->dev, "unable to register %s\n", init.name);
  556. goto err_clk;
  557. }
  558. /* Register PLL */
  559. memset(&init, 0, sizeof(init));
  560. init.name = vc5_pll_names[0];
  561. init.ops = &vc5_pll_ops;
  562. init.flags = CLK_SET_RATE_PARENT;
  563. init.parent_names = vc5_mux_names;
  564. init.num_parents = 1;
  565. vc5->clk_pll.num = 0;
  566. vc5->clk_pll.vc5 = vc5;
  567. vc5->clk_pll.hw.init = &init;
  568. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
  569. if (ret) {
  570. dev_err(&client->dev, "unable to register %s\n", init.name);
  571. goto err_clk;
  572. }
  573. /* Register FODs */
  574. for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
  575. idx = vc5_map_index_to_output(vc5->chip_info->model, n);
  576. memset(&init, 0, sizeof(init));
  577. init.name = vc5_fod_names[idx];
  578. init.ops = &vc5_fod_ops;
  579. init.flags = CLK_SET_RATE_PARENT;
  580. init.parent_names = vc5_pll_names;
  581. init.num_parents = 1;
  582. vc5->clk_fod[n].num = idx;
  583. vc5->clk_fod[n].vc5 = vc5;
  584. vc5->clk_fod[n].hw.init = &init;
  585. ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
  586. if (ret) {
  587. dev_err(&client->dev, "unable to register %s\n",
  588. init.name);
  589. goto err_clk;
  590. }
  591. }
  592. /* Register MUX-connected OUT0_I2C_SELB output */
  593. memset(&init, 0, sizeof(init));
  594. init.name = vc5_clk_out_names[0];
  595. init.ops = &vc5_clk_out_ops;
  596. init.flags = CLK_SET_RATE_PARENT;
  597. init.parent_names = vc5_mux_names;
  598. init.num_parents = 1;
  599. vc5->clk_out[0].num = idx;
  600. vc5->clk_out[0].vc5 = vc5;
  601. vc5->clk_out[0].hw.init = &init;
  602. ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
  603. if (ret) {
  604. dev_err(&client->dev, "unable to register %s\n",
  605. init.name);
  606. goto err_clk;
  607. }
  608. /* Register FOD-connected OUTx outputs */
  609. for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
  610. idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
  611. parent_names[0] = vc5_fod_names[idx];
  612. if (n == 1)
  613. parent_names[1] = vc5_mux_names[0];
  614. else
  615. parent_names[1] = vc5_clk_out_names[n - 1];
  616. memset(&init, 0, sizeof(init));
  617. init.name = vc5_clk_out_names[idx + 1];
  618. init.ops = &vc5_clk_out_ops;
  619. init.flags = CLK_SET_RATE_PARENT;
  620. init.parent_names = parent_names;
  621. init.num_parents = 2;
  622. vc5->clk_out[n].num = idx;
  623. vc5->clk_out[n].vc5 = vc5;
  624. vc5->clk_out[n].hw.init = &init;
  625. ret = devm_clk_hw_register(&client->dev,
  626. &vc5->clk_out[n].hw);
  627. if (ret) {
  628. dev_err(&client->dev, "unable to register %s\n",
  629. init.name);
  630. goto err_clk;
  631. }
  632. }
  633. ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
  634. if (ret) {
  635. dev_err(&client->dev, "unable to add clk provider\n");
  636. goto err_clk;
  637. }
  638. return 0;
  639. err_clk:
  640. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  641. clk_unregister_fixed_rate(vc5->pin_xin);
  642. return ret;
  643. }
  644. static int vc5_remove(struct i2c_client *client)
  645. {
  646. struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
  647. of_clk_del_provider(client->dev.of_node);
  648. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  649. clk_unregister_fixed_rate(vc5->pin_xin);
  650. return 0;
  651. }
  652. static const struct vc5_chip_info idt_5p49v5923_info = {
  653. .model = IDT_VC5_5P49V5923,
  654. .clk_fod_cnt = 2,
  655. .clk_out_cnt = 3,
  656. .flags = 0,
  657. };
  658. static const struct vc5_chip_info idt_5p49v5933_info = {
  659. .model = IDT_VC5_5P49V5933,
  660. .clk_fod_cnt = 2,
  661. .clk_out_cnt = 3,
  662. .flags = VC5_HAS_INTERNAL_XTAL,
  663. };
  664. static const struct vc5_chip_info idt_5p49v5935_info = {
  665. .model = IDT_VC5_5P49V5935,
  666. .clk_fod_cnt = 4,
  667. .clk_out_cnt = 5,
  668. .flags = VC5_HAS_INTERNAL_XTAL,
  669. };
  670. static const struct i2c_device_id vc5_id[] = {
  671. { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
  672. { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
  673. { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
  674. { }
  675. };
  676. MODULE_DEVICE_TABLE(i2c, vc5_id);
  677. static const struct of_device_id clk_vc5_of_match[] = {
  678. { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
  679. { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
  680. { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
  681. { },
  682. };
  683. MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
  684. static struct i2c_driver vc5_driver = {
  685. .driver = {
  686. .name = "vc5",
  687. .of_match_table = clk_vc5_of_match,
  688. },
  689. .probe = vc5_probe,
  690. .remove = vc5_remove,
  691. .id_table = vc5_id,
  692. };
  693. module_i2c_driver(vc5_driver);
  694. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  695. MODULE_DESCRIPTION("IDT VersaClock 5 driver");
  696. MODULE_LICENSE("GPL");