clk-versaclock5.c 37 KB

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