clk-versaclock5.c 37 KB

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