clk-versaclock5.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for IDT Versaclock 5
  4. *
  5. * Copyright (C) 2017 Marek Vasut <[email protected]>
  6. */
  7. /*
  8. * Possible optimizations:
  9. * - Use spread spectrum
  10. * - Use integer divider in FOD if applicable
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/rational.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include <dt-bindings/clock/versaclock.h>
  25. /* VersaClock5 registers */
  26. #define VC5_OTP_CONTROL 0x00
  27. /* Factory-reserved register block */
  28. #define VC5_RSVD_DEVICE_ID 0x01
  29. #define VC5_RSVD_ADC_GAIN_7_0 0x02
  30. #define VC5_RSVD_ADC_GAIN_15_8 0x03
  31. #define VC5_RSVD_ADC_OFFSET_7_0 0x04
  32. #define VC5_RSVD_ADC_OFFSET_15_8 0x05
  33. #define VC5_RSVD_TEMPY 0x06
  34. #define VC5_RSVD_OFFSET_TBIN 0x07
  35. #define VC5_RSVD_GAIN 0x08
  36. #define VC5_RSVD_TEST_NP 0x09
  37. #define VC5_RSVD_UNUSED 0x0a
  38. #define VC5_RSVD_BANDGAP_TRIM_UP 0x0b
  39. #define VC5_RSVD_BANDGAP_TRIM_DN 0x0c
  40. #define VC5_RSVD_CLK_R_12_CLK_AMP_4 0x0d
  41. #define VC5_RSVD_CLK_R_34_CLK_AMP_4 0x0e
  42. #define VC5_RSVD_CLK_AMP_123 0x0f
  43. /* Configuration register block */
  44. #define VC5_PRIM_SRC_SHDN 0x10
  45. #define VC5_PRIM_SRC_SHDN_EN_XTAL BIT(7)
  46. #define VC5_PRIM_SRC_SHDN_EN_CLKIN BIT(6)
  47. #define VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ BIT(3)
  48. #define VC5_PRIM_SRC_SHDN_SP BIT(1)
  49. #define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
  50. #define VC5_VCO_BAND 0x11
  51. #define VC5_XTAL_X1_LOAD_CAP 0x12
  52. #define VC5_XTAL_X2_LOAD_CAP 0x13
  53. #define VC5_REF_DIVIDER 0x15
  54. #define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
  55. #define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
  56. #define VC5_VCO_CTRL_AND_PREDIV 0x16
  57. #define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
  58. #define VC5_FEEDBACK_INT_DIV 0x17
  59. #define VC5_FEEDBACK_INT_DIV_BITS 0x18
  60. #define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
  61. #define VC5_RC_CONTROL0 0x1e
  62. #define VC5_RC_CONTROL1 0x1f
  63. /* These registers are named "Unused Factory Reserved Registers" */
  64. #define VC5_RESERVED_X0(idx) (0x20 + ((idx) * 0x10))
  65. #define VC5_RESERVED_X0_BYPASS_SYNC BIT(7) /* bypass_sync<idx> bit */
  66. /* Output divider control for divider 1,2,3,4 */
  67. #define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
  68. #define VC5_OUT_DIV_CONTROL_RESET BIT(7)
  69. #define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
  70. #define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
  71. #define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
  72. #define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
  73. #define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
  74. #define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
  75. #define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
  76. #define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
  77. #define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
  78. #define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
  79. #define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
  80. /* Clock control register for clock 1,2 */
  81. #define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
  82. #define VC5_CLK_OUTPUT_CFG0_CFG_SHIFT 5
  83. #define VC5_CLK_OUTPUT_CFG0_CFG_MASK GENMASK(7, VC5_CLK_OUTPUT_CFG0_CFG_SHIFT)
  84. #define VC5_CLK_OUTPUT_CFG0_CFG_LVPECL (VC5_LVPECL)
  85. #define VC5_CLK_OUTPUT_CFG0_CFG_CMOS (VC5_CMOS)
  86. #define VC5_CLK_OUTPUT_CFG0_CFG_HCSL33 (VC5_HCSL33)
  87. #define VC5_CLK_OUTPUT_CFG0_CFG_LVDS (VC5_LVDS)
  88. #define VC5_CLK_OUTPUT_CFG0_CFG_CMOS2 (VC5_CMOS2)
  89. #define VC5_CLK_OUTPUT_CFG0_CFG_CMOSD (VC5_CMOSD)
  90. #define VC5_CLK_OUTPUT_CFG0_CFG_HCSL25 (VC5_HCSL25)
  91. #define VC5_CLK_OUTPUT_CFG0_PWR_SHIFT 3
  92. #define VC5_CLK_OUTPUT_CFG0_PWR_MASK GENMASK(4, VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
  93. #define VC5_CLK_OUTPUT_CFG0_PWR_18 (0<<VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
  94. #define VC5_CLK_OUTPUT_CFG0_PWR_25 (2<<VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
  95. #define VC5_CLK_OUTPUT_CFG0_PWR_33 (3<<VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
  96. #define VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT 0
  97. #define VC5_CLK_OUTPUT_CFG0_SLEW_MASK GENMASK(1, VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
  98. #define VC5_CLK_OUTPUT_CFG0_SLEW_80 (0<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
  99. #define VC5_CLK_OUTPUT_CFG0_SLEW_85 (1<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
  100. #define VC5_CLK_OUTPUT_CFG0_SLEW_90 (2<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
  101. #define VC5_CLK_OUTPUT_CFG0_SLEW_100 (3<<VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
  102. #define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
  103. #define VC5_CLK_OE_SHDN 0x68
  104. #define VC5_CLK_OS_SHDN 0x69
  105. #define VC5_GLOBAL_REGISTER 0x76
  106. #define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
  107. /* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
  108. #define VC5_PLL_VCO_MIN 2500000000UL
  109. #define VC5_PLL_VCO_MAX 3000000000UL
  110. /* VC5 Input mux settings */
  111. #define VC5_MUX_IN_XIN BIT(0)
  112. #define VC5_MUX_IN_CLKIN BIT(1)
  113. /* Maximum number of clk_out supported by this driver */
  114. #define VC5_MAX_CLK_OUT_NUM 5
  115. /* Maximum number of FODs supported by this driver */
  116. #define VC5_MAX_FOD_NUM 4
  117. /* flags to describe chip features */
  118. /* chip has built-in oscilator */
  119. #define VC5_HAS_INTERNAL_XTAL BIT(0)
  120. /* chip has PFD requency doubler */
  121. #define VC5_HAS_PFD_FREQ_DBL BIT(1)
  122. /* chip has bits to disable FOD sync */
  123. #define VC5_HAS_BYPASS_SYNC_BIT BIT(2)
  124. /* Supported IDT VC5 models. */
  125. enum vc5_model {
  126. IDT_VC5_5P49V5923,
  127. IDT_VC5_5P49V5925,
  128. IDT_VC5_5P49V5933,
  129. IDT_VC5_5P49V5935,
  130. IDT_VC6_5P49V6901,
  131. IDT_VC6_5P49V6965,
  132. IDT_VC6_5P49V6975,
  133. };
  134. /* Structure to describe features of a particular VC5 model */
  135. struct vc5_chip_info {
  136. const enum vc5_model model;
  137. const unsigned int clk_fod_cnt;
  138. const unsigned int clk_out_cnt;
  139. const u32 flags;
  140. };
  141. struct vc5_driver_data;
  142. struct vc5_hw_data {
  143. struct clk_hw hw;
  144. struct vc5_driver_data *vc5;
  145. u32 div_int;
  146. u32 div_frc;
  147. unsigned int num;
  148. };
  149. struct vc5_out_data {
  150. struct clk_hw hw;
  151. struct vc5_driver_data *vc5;
  152. unsigned int num;
  153. unsigned int clk_output_cfg0;
  154. unsigned int clk_output_cfg0_mask;
  155. };
  156. struct vc5_driver_data {
  157. struct i2c_client *client;
  158. struct regmap *regmap;
  159. const struct vc5_chip_info *chip_info;
  160. struct clk *pin_xin;
  161. struct clk *pin_clkin;
  162. unsigned char clk_mux_ins;
  163. struct clk_hw clk_mux;
  164. struct clk_hw clk_mul;
  165. struct clk_hw clk_pfd;
  166. struct vc5_hw_data clk_pll;
  167. struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
  168. struct vc5_out_data clk_out[VC5_MAX_CLK_OUT_NUM];
  169. };
  170. /*
  171. * VersaClock5 i2c regmap
  172. */
  173. static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
  174. {
  175. /* Factory reserved regs, make them read-only */
  176. if (reg <= 0xf)
  177. return false;
  178. /* Factory reserved regs, make them read-only */
  179. if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
  180. return false;
  181. return true;
  182. }
  183. static const struct regmap_config vc5_regmap_config = {
  184. .reg_bits = 8,
  185. .val_bits = 8,
  186. .cache_type = REGCACHE_RBTREE,
  187. .max_register = 0x76,
  188. .writeable_reg = vc5_regmap_is_writeable,
  189. };
  190. /*
  191. * VersaClock5 input multiplexer between XTAL and CLKIN divider
  192. */
  193. static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
  194. {
  195. struct vc5_driver_data *vc5 =
  196. container_of(hw, struct vc5_driver_data, clk_mux);
  197. const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
  198. unsigned int src;
  199. int ret;
  200. ret = regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
  201. if (ret)
  202. return 0;
  203. src &= mask;
  204. if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
  205. return 0;
  206. if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
  207. return 1;
  208. dev_warn(&vc5->client->dev,
  209. "Invalid clock input configuration (%02x)\n", src);
  210. return 0;
  211. }
  212. static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
  213. {
  214. struct vc5_driver_data *vc5 =
  215. container_of(hw, struct vc5_driver_data, clk_mux);
  216. const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
  217. u8 src;
  218. if ((index > 1) || !vc5->clk_mux_ins)
  219. return -EINVAL;
  220. if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
  221. if (index == 0)
  222. src = VC5_PRIM_SRC_SHDN_EN_XTAL;
  223. if (index == 1)
  224. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  225. } else {
  226. if (index != 0)
  227. return -EINVAL;
  228. if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
  229. src = VC5_PRIM_SRC_SHDN_EN_XTAL;
  230. else if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
  231. src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
  232. else /* Invalid; should have been caught by vc5_probe() */
  233. return -EINVAL;
  234. }
  235. return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
  236. }
  237. static const struct clk_ops vc5_mux_ops = {
  238. .set_parent = vc5_mux_set_parent,
  239. .get_parent = vc5_mux_get_parent,
  240. };
  241. static unsigned long vc5_dbl_recalc_rate(struct clk_hw *hw,
  242. unsigned long parent_rate)
  243. {
  244. struct vc5_driver_data *vc5 =
  245. container_of(hw, struct vc5_driver_data, clk_mul);
  246. unsigned int premul;
  247. int ret;
  248. ret = regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &premul);
  249. if (ret)
  250. return 0;
  251. if (premul & VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ)
  252. parent_rate *= 2;
  253. return parent_rate;
  254. }
  255. static long vc5_dbl_round_rate(struct clk_hw *hw, unsigned long rate,
  256. unsigned long *parent_rate)
  257. {
  258. if ((*parent_rate == rate) || ((*parent_rate * 2) == rate))
  259. return rate;
  260. else
  261. return -EINVAL;
  262. }
  263. static int vc5_dbl_set_rate(struct clk_hw *hw, unsigned long rate,
  264. unsigned long parent_rate)
  265. {
  266. struct vc5_driver_data *vc5 =
  267. container_of(hw, struct vc5_driver_data, clk_mul);
  268. u32 mask;
  269. if ((parent_rate * 2) == rate)
  270. mask = VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ;
  271. else
  272. mask = 0;
  273. return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN,
  274. VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ,
  275. mask);
  276. }
  277. static const struct clk_ops vc5_dbl_ops = {
  278. .recalc_rate = vc5_dbl_recalc_rate,
  279. .round_rate = vc5_dbl_round_rate,
  280. .set_rate = vc5_dbl_set_rate,
  281. };
  282. static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
  283. unsigned long parent_rate)
  284. {
  285. struct vc5_driver_data *vc5 =
  286. container_of(hw, struct vc5_driver_data, clk_pfd);
  287. unsigned int prediv, div;
  288. int ret;
  289. ret = regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
  290. if (ret)
  291. return 0;
  292. /* The bypass_prediv is set, PLL fed from Ref_in directly. */
  293. if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
  294. return parent_rate;
  295. ret = regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
  296. if (ret)
  297. return 0;
  298. /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
  299. if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
  300. return parent_rate / 2;
  301. else
  302. return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
  303. }
  304. static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
  305. unsigned long *parent_rate)
  306. {
  307. unsigned long idiv;
  308. /* PLL cannot operate with input clock above 50 MHz. */
  309. if (rate > 50000000)
  310. return -EINVAL;
  311. /* CLKIN within range of PLL input, feed directly to PLL. */
  312. if (*parent_rate <= 50000000)
  313. return *parent_rate;
  314. idiv = DIV_ROUND_UP(*parent_rate, rate);
  315. if (idiv > 127)
  316. return -EINVAL;
  317. return *parent_rate / idiv;
  318. }
  319. static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
  320. unsigned long parent_rate)
  321. {
  322. struct vc5_driver_data *vc5 =
  323. container_of(hw, struct vc5_driver_data, clk_pfd);
  324. unsigned long idiv;
  325. int ret;
  326. u8 div;
  327. /* CLKIN within range of PLL input, feed directly to PLL. */
  328. if (parent_rate <= 50000000) {
  329. ret = regmap_set_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  330. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
  331. if (ret)
  332. return ret;
  333. return regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
  334. }
  335. idiv = DIV_ROUND_UP(parent_rate, rate);
  336. /* We have dedicated div-2 predivider. */
  337. if (idiv == 2)
  338. div = VC5_REF_DIVIDER_SEL_PREDIV2;
  339. else
  340. div = VC5_REF_DIVIDER_REF_DIV(idiv);
  341. ret = regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
  342. if (ret)
  343. return ret;
  344. return regmap_clear_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
  345. VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
  346. }
  347. static const struct clk_ops vc5_pfd_ops = {
  348. .recalc_rate = vc5_pfd_recalc_rate,
  349. .round_rate = vc5_pfd_round_rate,
  350. .set_rate = vc5_pfd_set_rate,
  351. };
  352. /*
  353. * VersaClock5 PLL/VCO
  354. */
  355. static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
  356. unsigned long parent_rate)
  357. {
  358. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  359. struct vc5_driver_data *vc5 = hwdata->vc5;
  360. u32 div_int, div_frc;
  361. u8 fb[5];
  362. regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  363. div_int = (fb[0] << 4) | (fb[1] >> 4);
  364. div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
  365. /* The PLL divider has 12 integer bits and 24 fractional bits */
  366. return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
  367. }
  368. static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  369. unsigned long *parent_rate)
  370. {
  371. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  372. u32 div_int;
  373. u64 div_frc;
  374. rate = clamp(rate, VC5_PLL_VCO_MIN, VC5_PLL_VCO_MAX);
  375. /* Determine integer part, which is 12 bit wide */
  376. div_int = rate / *parent_rate;
  377. if (div_int > 0xfff)
  378. rate = *parent_rate * 0xfff;
  379. /* Determine best fractional part, which is 24 bit wide */
  380. div_frc = rate % *parent_rate;
  381. div_frc *= BIT(24) - 1;
  382. do_div(div_frc, *parent_rate);
  383. hwdata->div_int = div_int;
  384. hwdata->div_frc = (u32)div_frc;
  385. return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
  386. }
  387. static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  388. unsigned long parent_rate)
  389. {
  390. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  391. struct vc5_driver_data *vc5 = hwdata->vc5;
  392. u8 fb[5];
  393. fb[0] = hwdata->div_int >> 4;
  394. fb[1] = hwdata->div_int << 4;
  395. fb[2] = hwdata->div_frc >> 16;
  396. fb[3] = hwdata->div_frc >> 8;
  397. fb[4] = hwdata->div_frc;
  398. return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
  399. }
  400. static const struct clk_ops vc5_pll_ops = {
  401. .recalc_rate = vc5_pll_recalc_rate,
  402. .round_rate = vc5_pll_round_rate,
  403. .set_rate = vc5_pll_set_rate,
  404. };
  405. static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
  406. unsigned long parent_rate)
  407. {
  408. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  409. struct vc5_driver_data *vc5 = hwdata->vc5;
  410. /* VCO frequency is divided by two before entering FOD */
  411. u32 f_in = parent_rate / 2;
  412. u32 div_int, div_frc;
  413. u8 od_int[2];
  414. u8 od_frc[4];
  415. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
  416. od_int, 2);
  417. regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  418. od_frc, 4);
  419. div_int = (od_int[0] << 4) | (od_int[1] >> 4);
  420. div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
  421. (od_frc[2] << 6) | (od_frc[3] >> 2);
  422. /* Avoid division by zero if the output is not configured. */
  423. if (div_int == 0 && div_frc == 0)
  424. return 0;
  425. /* The PLL divider has 12 integer bits and 30 fractional bits */
  426. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  427. }
  428. static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
  429. unsigned long *parent_rate)
  430. {
  431. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  432. /* VCO frequency is divided by two before entering FOD */
  433. u32 f_in = *parent_rate / 2;
  434. u32 div_int;
  435. u64 div_frc;
  436. /* Determine integer part, which is 12 bit wide */
  437. div_int = f_in / rate;
  438. /*
  439. * WARNING: The clock chip does not output signal if the integer part
  440. * of the divider is 0xfff and fractional part is non-zero.
  441. * Clamp the divider at 0xffe to keep the code simple.
  442. */
  443. if (div_int > 0xffe) {
  444. div_int = 0xffe;
  445. rate = f_in / div_int;
  446. }
  447. /* Determine best fractional part, which is 30 bit wide */
  448. div_frc = f_in % rate;
  449. div_frc <<= 24;
  450. do_div(div_frc, rate);
  451. hwdata->div_int = div_int;
  452. hwdata->div_frc = (u32)div_frc;
  453. return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
  454. }
  455. static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
  456. unsigned long parent_rate)
  457. {
  458. struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
  459. struct vc5_driver_data *vc5 = hwdata->vc5;
  460. u8 data[14] = {
  461. hwdata->div_frc >> 22, hwdata->div_frc >> 14,
  462. hwdata->div_frc >> 6, hwdata->div_frc << 2,
  463. 0, 0, 0, 0, 0,
  464. 0, 0,
  465. hwdata->div_int >> 4, hwdata->div_int << 4,
  466. 0
  467. };
  468. int ret;
  469. ret = regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
  470. data, 14);
  471. if (ret)
  472. return ret;
  473. /*
  474. * Toggle magic bit in undocumented register for unknown reason.
  475. * This is what the IDT timing commander tool does and the chip
  476. * datasheet somewhat implies this is needed, but the register
  477. * and the bit is not documented.
  478. */
  479. ret = regmap_clear_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  480. VC5_GLOBAL_REGISTER_GLOBAL_RESET);
  481. if (ret)
  482. return ret;
  483. return regmap_set_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
  484. VC5_GLOBAL_REGISTER_GLOBAL_RESET);
  485. }
  486. static const struct clk_ops vc5_fod_ops = {
  487. .recalc_rate = vc5_fod_recalc_rate,
  488. .round_rate = vc5_fod_round_rate,
  489. .set_rate = vc5_fod_set_rate,
  490. };
  491. static int vc5_clk_out_prepare(struct clk_hw *hw)
  492. {
  493. struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
  494. struct vc5_driver_data *vc5 = hwdata->vc5;
  495. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  496. VC5_OUT_DIV_CONTROL_SEL_EXT |
  497. VC5_OUT_DIV_CONTROL_EN_FOD;
  498. unsigned int src;
  499. int ret;
  500. /*
  501. * When enabling a FOD, all currently enabled FODs are briefly
  502. * stopped in order to synchronize all of them. This causes a clock
  503. * disruption to any unrelated chips that might be already using
  504. * other clock outputs. Bypass the sync feature to avoid the issue,
  505. * which is possible on the VersaClock 6E family via reserved
  506. * registers.
  507. */
  508. if (vc5->chip_info->flags & VC5_HAS_BYPASS_SYNC_BIT) {
  509. ret = regmap_set_bits(vc5->regmap,
  510. VC5_RESERVED_X0(hwdata->num),
  511. VC5_RESERVED_X0_BYPASS_SYNC);
  512. if (ret)
  513. return ret;
  514. }
  515. /*
  516. * If the input mux is disabled, enable it first and
  517. * select source from matching FOD.
  518. */
  519. ret = regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  520. if (ret)
  521. return ret;
  522. if ((src & mask) == 0) {
  523. src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
  524. ret = regmap_update_bits(vc5->regmap,
  525. VC5_OUT_DIV_CONTROL(hwdata->num),
  526. mask | VC5_OUT_DIV_CONTROL_RESET, src);
  527. if (ret)
  528. return ret;
  529. }
  530. /* Enable the clock buffer */
  531. ret = regmap_set_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  532. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
  533. if (ret)
  534. return ret;
  535. if (hwdata->clk_output_cfg0_mask) {
  536. dev_dbg(&vc5->client->dev, "Update output %d mask 0x%0X val 0x%0X\n",
  537. hwdata->num, hwdata->clk_output_cfg0_mask,
  538. hwdata->clk_output_cfg0);
  539. ret = regmap_update_bits(vc5->regmap,
  540. VC5_CLK_OUTPUT_CFG(hwdata->num, 0),
  541. hwdata->clk_output_cfg0_mask,
  542. hwdata->clk_output_cfg0);
  543. if (ret)
  544. return ret;
  545. }
  546. return 0;
  547. }
  548. static void vc5_clk_out_unprepare(struct clk_hw *hw)
  549. {
  550. struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
  551. struct vc5_driver_data *vc5 = hwdata->vc5;
  552. /* Disable the clock buffer */
  553. regmap_clear_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
  554. VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
  555. }
  556. static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
  557. {
  558. struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
  559. struct vc5_driver_data *vc5 = hwdata->vc5;
  560. const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  561. VC5_OUT_DIV_CONTROL_SEL_EXT |
  562. VC5_OUT_DIV_CONTROL_EN_FOD;
  563. const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
  564. VC5_OUT_DIV_CONTROL_EN_FOD;
  565. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  566. VC5_OUT_DIV_CONTROL_SEL_EXT;
  567. unsigned int src;
  568. int ret;
  569. ret = regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
  570. if (ret)
  571. return 0;
  572. src &= mask;
  573. if (src == 0) /* Input mux set to DISABLED */
  574. return 0;
  575. if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
  576. return 0;
  577. if (src == extclk)
  578. return 1;
  579. dev_warn(&vc5->client->dev,
  580. "Invalid clock output configuration (%02x)\n", src);
  581. return 0;
  582. }
  583. static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
  584. {
  585. struct vc5_out_data *hwdata = container_of(hw, struct vc5_out_data, hw);
  586. struct vc5_driver_data *vc5 = hwdata->vc5;
  587. const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
  588. VC5_OUT_DIV_CONTROL_SELB_NORM |
  589. VC5_OUT_DIV_CONTROL_SEL_EXT |
  590. VC5_OUT_DIV_CONTROL_EN_FOD;
  591. const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
  592. VC5_OUT_DIV_CONTROL_SEL_EXT;
  593. u8 src = VC5_OUT_DIV_CONTROL_RESET;
  594. if (index == 0)
  595. src |= VC5_OUT_DIV_CONTROL_EN_FOD;
  596. else
  597. src |= extclk;
  598. return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
  599. mask, src);
  600. }
  601. static const struct clk_ops vc5_clk_out_ops = {
  602. .prepare = vc5_clk_out_prepare,
  603. .unprepare = vc5_clk_out_unprepare,
  604. .set_parent = vc5_clk_out_set_parent,
  605. .get_parent = vc5_clk_out_get_parent,
  606. };
  607. static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
  608. void *data)
  609. {
  610. struct vc5_driver_data *vc5 = data;
  611. unsigned int idx = clkspec->args[0];
  612. if (idx >= vc5->chip_info->clk_out_cnt)
  613. return ERR_PTR(-EINVAL);
  614. return &vc5->clk_out[idx].hw;
  615. }
  616. static int vc5_map_index_to_output(const enum vc5_model model,
  617. const unsigned int n)
  618. {
  619. switch (model) {
  620. case IDT_VC5_5P49V5933:
  621. return (n == 0) ? 0 : 3;
  622. case IDT_VC5_5P49V5923:
  623. case IDT_VC5_5P49V5925:
  624. case IDT_VC5_5P49V5935:
  625. case IDT_VC6_5P49V6901:
  626. case IDT_VC6_5P49V6965:
  627. case IDT_VC6_5P49V6975:
  628. default:
  629. return n;
  630. }
  631. }
  632. static int vc5_update_mode(struct device_node *np_output,
  633. struct vc5_out_data *clk_out)
  634. {
  635. u32 value;
  636. if (!of_property_read_u32(np_output, "idt,mode", &value)) {
  637. clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_CFG_MASK;
  638. switch (value) {
  639. case VC5_CLK_OUTPUT_CFG0_CFG_LVPECL:
  640. case VC5_CLK_OUTPUT_CFG0_CFG_CMOS:
  641. case VC5_CLK_OUTPUT_CFG0_CFG_HCSL33:
  642. case VC5_CLK_OUTPUT_CFG0_CFG_LVDS:
  643. case VC5_CLK_OUTPUT_CFG0_CFG_CMOS2:
  644. case VC5_CLK_OUTPUT_CFG0_CFG_CMOSD:
  645. case VC5_CLK_OUTPUT_CFG0_CFG_HCSL25:
  646. clk_out->clk_output_cfg0 |=
  647. value << VC5_CLK_OUTPUT_CFG0_CFG_SHIFT;
  648. break;
  649. default:
  650. return -EINVAL;
  651. }
  652. }
  653. return 0;
  654. }
  655. static int vc5_update_power(struct device_node *np_output,
  656. struct vc5_out_data *clk_out)
  657. {
  658. u32 value;
  659. if (!of_property_read_u32(np_output, "idt,voltage-microvolt",
  660. &value)) {
  661. clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_PWR_MASK;
  662. switch (value) {
  663. case 1800000:
  664. clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_18;
  665. break;
  666. case 2500000:
  667. clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_25;
  668. break;
  669. case 3300000:
  670. clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_33;
  671. break;
  672. default:
  673. return -EINVAL;
  674. }
  675. }
  676. return 0;
  677. }
  678. static int vc5_map_cap_value(u32 femtofarads)
  679. {
  680. int mapped_value;
  681. /*
  682. * The datasheet explicitly states 9000 - 25000 with 0.5pF
  683. * steps, but the Programmer's guide shows the steps are 0.430pF.
  684. * After getting feedback from Renesas, the .5pF steps were the
  685. * goal, but 430nF was the actual values.
  686. * Because of this, the actual range goes to 22760 instead of 25000
  687. */
  688. if (femtofarads < 9000 || femtofarads > 22760)
  689. return -EINVAL;
  690. /*
  691. * The Programmer's guide shows XTAL[5:0] but in reality,
  692. * XTAL[0] and XTAL[1] are both LSB which makes the math
  693. * strange. With clarfication from Renesas, setting the
  694. * values should be simpler by ignoring XTAL[0]
  695. */
  696. mapped_value = DIV_ROUND_CLOSEST(femtofarads - 9000, 430);
  697. /*
  698. * Since the calculation ignores XTAL[0], there is one
  699. * special case where mapped_value = 32. In reality, this means
  700. * the real mapped value should be 111111b. In other cases,
  701. * the mapped_value needs to be shifted 1 to the left.
  702. */
  703. if (mapped_value > 31)
  704. mapped_value = 0x3f;
  705. else
  706. mapped_value <<= 1;
  707. return mapped_value;
  708. }
  709. static int vc5_update_cap_load(struct device_node *node, struct vc5_driver_data *vc5)
  710. {
  711. u32 value;
  712. int mapped_value;
  713. int ret;
  714. if (of_property_read_u32(node, "idt,xtal-load-femtofarads", &value))
  715. return 0;
  716. mapped_value = vc5_map_cap_value(value);
  717. if (mapped_value < 0)
  718. return mapped_value;
  719. /*
  720. * The mapped_value is really the high 6 bits of
  721. * VC5_XTAL_X1_LOAD_CAP and VC5_XTAL_X2_LOAD_CAP, so
  722. * shift the value 2 places.
  723. */
  724. ret = regmap_update_bits(vc5->regmap, VC5_XTAL_X1_LOAD_CAP, ~0x03,
  725. mapped_value << 2);
  726. if (ret)
  727. return ret;
  728. return regmap_update_bits(vc5->regmap, VC5_XTAL_X2_LOAD_CAP, ~0x03,
  729. mapped_value << 2);
  730. }
  731. static int vc5_update_slew(struct device_node *np_output,
  732. struct vc5_out_data *clk_out)
  733. {
  734. u32 value;
  735. if (!of_property_read_u32(np_output, "idt,slew-percent", &value)) {
  736. clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_SLEW_MASK;
  737. switch (value) {
  738. case 80:
  739. clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_80;
  740. break;
  741. case 85:
  742. clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_85;
  743. break;
  744. case 90:
  745. clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_90;
  746. break;
  747. case 100:
  748. clk_out->clk_output_cfg0 |=
  749. VC5_CLK_OUTPUT_CFG0_SLEW_100;
  750. break;
  751. default:
  752. return -EINVAL;
  753. }
  754. }
  755. return 0;
  756. }
  757. static int vc5_get_output_config(struct i2c_client *client,
  758. struct vc5_out_data *clk_out)
  759. {
  760. struct device_node *np_output;
  761. char *child_name;
  762. int ret = 0;
  763. child_name = kasprintf(GFP_KERNEL, "OUT%d", clk_out->num + 1);
  764. if (!child_name)
  765. return -ENOMEM;
  766. np_output = of_get_child_by_name(client->dev.of_node, child_name);
  767. kfree(child_name);
  768. if (!np_output)
  769. return 0;
  770. ret = vc5_update_mode(np_output, clk_out);
  771. if (ret)
  772. goto output_error;
  773. ret = vc5_update_power(np_output, clk_out);
  774. if (ret)
  775. goto output_error;
  776. ret = vc5_update_slew(np_output, clk_out);
  777. output_error:
  778. if (ret) {
  779. dev_err(&client->dev,
  780. "Invalid clock output configuration OUT%d\n",
  781. clk_out->num + 1);
  782. }
  783. of_node_put(np_output);
  784. return ret;
  785. }
  786. static const struct of_device_id clk_vc5_of_match[];
  787. static int vc5_probe(struct i2c_client *client)
  788. {
  789. unsigned int oe, sd, src_mask = 0, src_val = 0;
  790. struct vc5_driver_data *vc5;
  791. struct clk_init_data init;
  792. const char *parent_names[2];
  793. unsigned int n, idx = 0;
  794. int ret;
  795. vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
  796. if (!vc5)
  797. return -ENOMEM;
  798. i2c_set_clientdata(client, vc5);
  799. vc5->client = client;
  800. vc5->chip_info = of_device_get_match_data(&client->dev);
  801. vc5->pin_xin = devm_clk_get(&client->dev, "xin");
  802. if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
  803. return -EPROBE_DEFER;
  804. vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
  805. if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
  806. return -EPROBE_DEFER;
  807. vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
  808. if (IS_ERR(vc5->regmap))
  809. return dev_err_probe(&client->dev, PTR_ERR(vc5->regmap),
  810. "failed to allocate register map\n");
  811. ret = of_property_read_u32(client->dev.of_node, "idt,shutdown", &sd);
  812. if (!ret) {
  813. src_mask |= VC5_PRIM_SRC_SHDN_EN_GBL_SHDN;
  814. if (sd)
  815. src_val |= VC5_PRIM_SRC_SHDN_EN_GBL_SHDN;
  816. } else if (ret != -EINVAL) {
  817. return dev_err_probe(&client->dev, ret,
  818. "could not read idt,shutdown\n");
  819. }
  820. ret = of_property_read_u32(client->dev.of_node,
  821. "idt,output-enable-active", &oe);
  822. if (!ret) {
  823. src_mask |= VC5_PRIM_SRC_SHDN_SP;
  824. if (oe)
  825. src_val |= VC5_PRIM_SRC_SHDN_SP;
  826. } else if (ret != -EINVAL) {
  827. return dev_err_probe(&client->dev, ret,
  828. "could not read idt,output-enable-active\n");
  829. }
  830. ret = regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, src_mask,
  831. src_val);
  832. if (ret)
  833. return ret;
  834. /* Register clock input mux */
  835. memset(&init, 0, sizeof(init));
  836. if (!IS_ERR(vc5->pin_xin)) {
  837. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  838. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  839. } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
  840. vc5->pin_xin = clk_register_fixed_rate(&client->dev,
  841. "internal-xtal", NULL,
  842. 0, 25000000);
  843. if (IS_ERR(vc5->pin_xin))
  844. return PTR_ERR(vc5->pin_xin);
  845. vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
  846. parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
  847. }
  848. if (!IS_ERR(vc5->pin_clkin)) {
  849. vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
  850. parent_names[init.num_parents++] =
  851. __clk_get_name(vc5->pin_clkin);
  852. }
  853. if (!init.num_parents)
  854. return dev_err_probe(&client->dev, -EINVAL,
  855. "no input clock specified!\n");
  856. /* Configure Optional Loading Capacitance for external XTAL */
  857. if (!(vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)) {
  858. ret = vc5_update_cap_load(client->dev.of_node, vc5);
  859. if (ret)
  860. goto err_clk_register;
  861. }
  862. init.name = kasprintf(GFP_KERNEL, "%pOFn.mux", client->dev.of_node);
  863. if (!init.name) {
  864. ret = -ENOMEM;
  865. goto err_clk;
  866. }
  867. init.ops = &vc5_mux_ops;
  868. init.flags = 0;
  869. init.parent_names = parent_names;
  870. vc5->clk_mux.init = &init;
  871. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
  872. if (ret)
  873. goto err_clk_register;
  874. kfree(init.name); /* clock framework made a copy of the name */
  875. if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL) {
  876. /* Register frequency doubler */
  877. memset(&init, 0, sizeof(init));
  878. init.name = kasprintf(GFP_KERNEL, "%pOFn.dbl",
  879. client->dev.of_node);
  880. if (!init.name) {
  881. ret = -ENOMEM;
  882. goto err_clk;
  883. }
  884. init.ops = &vc5_dbl_ops;
  885. init.flags = CLK_SET_RATE_PARENT;
  886. init.parent_names = parent_names;
  887. parent_names[0] = clk_hw_get_name(&vc5->clk_mux);
  888. init.num_parents = 1;
  889. vc5->clk_mul.init = &init;
  890. ret = devm_clk_hw_register(&client->dev, &vc5->clk_mul);
  891. if (ret)
  892. goto err_clk_register;
  893. kfree(init.name); /* clock framework made a copy of the name */
  894. }
  895. /* Register PFD */
  896. memset(&init, 0, sizeof(init));
  897. init.name = kasprintf(GFP_KERNEL, "%pOFn.pfd", client->dev.of_node);
  898. if (!init.name) {
  899. ret = -ENOMEM;
  900. goto err_clk;
  901. }
  902. init.ops = &vc5_pfd_ops;
  903. init.flags = CLK_SET_RATE_PARENT;
  904. init.parent_names = parent_names;
  905. if (vc5->chip_info->flags & VC5_HAS_PFD_FREQ_DBL)
  906. parent_names[0] = clk_hw_get_name(&vc5->clk_mul);
  907. else
  908. parent_names[0] = clk_hw_get_name(&vc5->clk_mux);
  909. init.num_parents = 1;
  910. vc5->clk_pfd.init = &init;
  911. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
  912. if (ret)
  913. goto err_clk_register;
  914. kfree(init.name); /* clock framework made a copy of the name */
  915. /* Register PLL */
  916. memset(&init, 0, sizeof(init));
  917. init.name = kasprintf(GFP_KERNEL, "%pOFn.pll", client->dev.of_node);
  918. if (!init.name) {
  919. ret = -ENOMEM;
  920. goto err_clk;
  921. }
  922. init.ops = &vc5_pll_ops;
  923. init.flags = CLK_SET_RATE_PARENT;
  924. init.parent_names = parent_names;
  925. parent_names[0] = clk_hw_get_name(&vc5->clk_pfd);
  926. init.num_parents = 1;
  927. vc5->clk_pll.num = 0;
  928. vc5->clk_pll.vc5 = vc5;
  929. vc5->clk_pll.hw.init = &init;
  930. ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
  931. if (ret)
  932. goto err_clk_register;
  933. kfree(init.name); /* clock framework made a copy of the name */
  934. /* Register FODs */
  935. for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
  936. idx = vc5_map_index_to_output(vc5->chip_info->model, n);
  937. memset(&init, 0, sizeof(init));
  938. init.name = kasprintf(GFP_KERNEL, "%pOFn.fod%d",
  939. client->dev.of_node, idx);
  940. if (!init.name) {
  941. ret = -ENOMEM;
  942. goto err_clk;
  943. }
  944. init.ops = &vc5_fod_ops;
  945. init.flags = CLK_SET_RATE_PARENT;
  946. init.parent_names = parent_names;
  947. parent_names[0] = clk_hw_get_name(&vc5->clk_pll.hw);
  948. init.num_parents = 1;
  949. vc5->clk_fod[n].num = idx;
  950. vc5->clk_fod[n].vc5 = vc5;
  951. vc5->clk_fod[n].hw.init = &init;
  952. ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
  953. if (ret)
  954. goto err_clk_register;
  955. kfree(init.name); /* clock framework made a copy of the name */
  956. }
  957. /* Register MUX-connected OUT0_I2C_SELB output */
  958. memset(&init, 0, sizeof(init));
  959. init.name = kasprintf(GFP_KERNEL, "%pOFn.out0_sel_i2cb",
  960. client->dev.of_node);
  961. if (!init.name) {
  962. ret = -ENOMEM;
  963. goto err_clk;
  964. }
  965. init.ops = &vc5_clk_out_ops;
  966. init.flags = CLK_SET_RATE_PARENT;
  967. init.parent_names = parent_names;
  968. parent_names[0] = clk_hw_get_name(&vc5->clk_mux);
  969. init.num_parents = 1;
  970. vc5->clk_out[0].num = idx;
  971. vc5->clk_out[0].vc5 = vc5;
  972. vc5->clk_out[0].hw.init = &init;
  973. ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
  974. if (ret)
  975. goto err_clk_register;
  976. kfree(init.name); /* clock framework made a copy of the name */
  977. /* Register FOD-connected OUTx outputs */
  978. for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
  979. idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
  980. parent_names[0] = clk_hw_get_name(&vc5->clk_fod[idx].hw);
  981. if (n == 1)
  982. parent_names[1] = clk_hw_get_name(&vc5->clk_mux);
  983. else
  984. parent_names[1] =
  985. clk_hw_get_name(&vc5->clk_out[n - 1].hw);
  986. memset(&init, 0, sizeof(init));
  987. init.name = kasprintf(GFP_KERNEL, "%pOFn.out%d",
  988. client->dev.of_node, idx + 1);
  989. if (!init.name) {
  990. ret = -ENOMEM;
  991. goto err_clk;
  992. }
  993. init.ops = &vc5_clk_out_ops;
  994. init.flags = CLK_SET_RATE_PARENT;
  995. init.parent_names = parent_names;
  996. init.num_parents = 2;
  997. vc5->clk_out[n].num = idx;
  998. vc5->clk_out[n].vc5 = vc5;
  999. vc5->clk_out[n].hw.init = &init;
  1000. ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[n].hw);
  1001. if (ret)
  1002. goto err_clk_register;
  1003. kfree(init.name); /* clock framework made a copy of the name */
  1004. /* Fetch Clock Output configuration from DT (if specified) */
  1005. ret = vc5_get_output_config(client, &vc5->clk_out[n]);
  1006. if (ret)
  1007. goto err_clk;
  1008. }
  1009. ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
  1010. if (ret) {
  1011. dev_err_probe(&client->dev, ret,
  1012. "unable to add clk provider\n");
  1013. goto err_clk;
  1014. }
  1015. return 0;
  1016. err_clk_register:
  1017. dev_err_probe(&client->dev, ret,
  1018. "unable to register %s\n", init.name);
  1019. kfree(init.name); /* clock framework made a copy of the name */
  1020. err_clk:
  1021. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  1022. clk_unregister_fixed_rate(vc5->pin_xin);
  1023. return ret;
  1024. }
  1025. static void vc5_remove(struct i2c_client *client)
  1026. {
  1027. struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
  1028. of_clk_del_provider(client->dev.of_node);
  1029. if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
  1030. clk_unregister_fixed_rate(vc5->pin_xin);
  1031. }
  1032. static int __maybe_unused vc5_suspend(struct device *dev)
  1033. {
  1034. struct vc5_driver_data *vc5 = dev_get_drvdata(dev);
  1035. regcache_cache_only(vc5->regmap, true);
  1036. regcache_mark_dirty(vc5->regmap);
  1037. return 0;
  1038. }
  1039. static int __maybe_unused vc5_resume(struct device *dev)
  1040. {
  1041. struct vc5_driver_data *vc5 = dev_get_drvdata(dev);
  1042. int ret;
  1043. regcache_cache_only(vc5->regmap, false);
  1044. ret = regcache_sync(vc5->regmap);
  1045. if (ret)
  1046. dev_err(dev, "Failed to restore register map: %d\n", ret);
  1047. return ret;
  1048. }
  1049. static const struct vc5_chip_info idt_5p49v5923_info = {
  1050. .model = IDT_VC5_5P49V5923,
  1051. .clk_fod_cnt = 2,
  1052. .clk_out_cnt = 3,
  1053. .flags = 0,
  1054. };
  1055. static const struct vc5_chip_info idt_5p49v5925_info = {
  1056. .model = IDT_VC5_5P49V5925,
  1057. .clk_fod_cnt = 4,
  1058. .clk_out_cnt = 5,
  1059. .flags = 0,
  1060. };
  1061. static const struct vc5_chip_info idt_5p49v5933_info = {
  1062. .model = IDT_VC5_5P49V5933,
  1063. .clk_fod_cnt = 2,
  1064. .clk_out_cnt = 3,
  1065. .flags = VC5_HAS_INTERNAL_XTAL,
  1066. };
  1067. static const struct vc5_chip_info idt_5p49v5935_info = {
  1068. .model = IDT_VC5_5P49V5935,
  1069. .clk_fod_cnt = 4,
  1070. .clk_out_cnt = 5,
  1071. .flags = VC5_HAS_INTERNAL_XTAL,
  1072. };
  1073. static const struct vc5_chip_info idt_5p49v6901_info = {
  1074. .model = IDT_VC6_5P49V6901,
  1075. .clk_fod_cnt = 4,
  1076. .clk_out_cnt = 5,
  1077. .flags = VC5_HAS_PFD_FREQ_DBL | VC5_HAS_BYPASS_SYNC_BIT,
  1078. };
  1079. static const struct vc5_chip_info idt_5p49v6965_info = {
  1080. .model = IDT_VC6_5P49V6965,
  1081. .clk_fod_cnt = 4,
  1082. .clk_out_cnt = 5,
  1083. .flags = VC5_HAS_BYPASS_SYNC_BIT,
  1084. };
  1085. static const struct vc5_chip_info idt_5p49v6975_info = {
  1086. .model = IDT_VC6_5P49V6975,
  1087. .clk_fod_cnt = 4,
  1088. .clk_out_cnt = 5,
  1089. .flags = VC5_HAS_BYPASS_SYNC_BIT | VC5_HAS_INTERNAL_XTAL,
  1090. };
  1091. static const struct i2c_device_id vc5_id[] = {
  1092. { "5p49v5923", .driver_data = (kernel_ulong_t)&idt_5p49v5923_info },
  1093. { "5p49v5925", .driver_data = (kernel_ulong_t)&idt_5p49v5925_info },
  1094. { "5p49v5933", .driver_data = (kernel_ulong_t)&idt_5p49v5933_info },
  1095. { "5p49v5935", .driver_data = (kernel_ulong_t)&idt_5p49v5935_info },
  1096. { "5p49v6901", .driver_data = (kernel_ulong_t)&idt_5p49v6901_info },
  1097. { "5p49v6965", .driver_data = (kernel_ulong_t)&idt_5p49v6965_info },
  1098. { "5p49v6975", .driver_data = (kernel_ulong_t)&idt_5p49v6975_info },
  1099. { }
  1100. };
  1101. MODULE_DEVICE_TABLE(i2c, vc5_id);
  1102. static const struct of_device_id clk_vc5_of_match[] = {
  1103. { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
  1104. { .compatible = "idt,5p49v5925", .data = &idt_5p49v5925_info },
  1105. { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
  1106. { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
  1107. { .compatible = "idt,5p49v6901", .data = &idt_5p49v6901_info },
  1108. { .compatible = "idt,5p49v6965", .data = &idt_5p49v6965_info },
  1109. { .compatible = "idt,5p49v6975", .data = &idt_5p49v6975_info },
  1110. { },
  1111. };
  1112. MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
  1113. static SIMPLE_DEV_PM_OPS(vc5_pm_ops, vc5_suspend, vc5_resume);
  1114. static struct i2c_driver vc5_driver = {
  1115. .driver = {
  1116. .name = "vc5",
  1117. .pm = &vc5_pm_ops,
  1118. .of_match_table = clk_vc5_of_match,
  1119. },
  1120. .probe_new = vc5_probe,
  1121. .remove = vc5_remove,
  1122. .id_table = vc5_id,
  1123. };
  1124. module_i2c_driver(vc5_driver);
  1125. MODULE_AUTHOR("Marek Vasut <[email protected]>");
  1126. MODULE_DESCRIPTION("IDT VersaClock 5 driver");
  1127. MODULE_LICENSE("GPL");