clk-si5341.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Silicon Labs Si5340, Si5341, Si5342, Si5344 and Si5345
  4. * Copyright (C) 2019 Topic Embedded Products
  5. * Author: Mike Looijmans <mike.looijmans@topic.nl>
  6. *
  7. * The Si5341 has 10 outputs and 5 synthesizers.
  8. * The Si5340 is a smaller version of the Si5341 with only 4 outputs.
  9. * The Si5345 is similar to the Si5341, with the addition of fractional input
  10. * dividers and automatic input selection.
  11. * The Si5342 and Si5344 are smaller versions of the Si5345.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/delay.h>
  16. #include <linux/gcd.h>
  17. #include <linux/math64.h>
  18. #include <linux/i2c.h>
  19. #include <linux/module.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <linux/unaligned.h>
  24. #define SI5341_NUM_INPUTS 4
  25. #define SI5340_MAX_NUM_OUTPUTS 4
  26. #define SI5341_MAX_NUM_OUTPUTS 10
  27. #define SI5342_MAX_NUM_OUTPUTS 2
  28. #define SI5344_MAX_NUM_OUTPUTS 4
  29. #define SI5345_MAX_NUM_OUTPUTS 10
  30. #define SI5340_NUM_SYNTH 4
  31. #define SI5341_NUM_SYNTH 5
  32. #define SI5342_NUM_SYNTH 2
  33. #define SI5344_NUM_SYNTH 4
  34. #define SI5345_NUM_SYNTH 5
  35. /* Range of the synthesizer fractional divider */
  36. #define SI5341_SYNTH_N_MIN 10
  37. #define SI5341_SYNTH_N_MAX 4095
  38. /* The chip can get its input clock from 3 input pins or an XTAL */
  39. /* There is one PLL running at 13500–14256 MHz */
  40. #define SI5341_PLL_VCO_MIN 13500000000ull
  41. #define SI5341_PLL_VCO_MAX 14256000000ull
  42. /* The 5 frequency synthesizers obtain their input from the PLL */
  43. struct clk_si5341_synth {
  44. struct clk_hw hw;
  45. struct clk_si5341 *data;
  46. u8 index;
  47. };
  48. #define to_clk_si5341_synth(_hw) \
  49. container_of(_hw, struct clk_si5341_synth, hw)
  50. /* The output stages can be connected to any synth (full mux) */
  51. struct clk_si5341_output {
  52. struct clk_hw hw;
  53. struct clk_si5341 *data;
  54. struct regulator *vddo_reg;
  55. u8 index;
  56. };
  57. #define to_clk_si5341_output(_hw) \
  58. container_of(_hw, struct clk_si5341_output, hw)
  59. struct clk_si5341 {
  60. struct clk_hw hw;
  61. struct regmap *regmap;
  62. struct i2c_client *i2c_client;
  63. struct clk_si5341_synth synth[SI5341_NUM_SYNTH];
  64. struct clk_si5341_output clk[SI5341_MAX_NUM_OUTPUTS];
  65. struct clk *input_clk[SI5341_NUM_INPUTS];
  66. const char *input_clk_name[SI5341_NUM_INPUTS];
  67. const u16 *reg_output_offset;
  68. const u16 *reg_rdiv_offset;
  69. u64 freq_vco; /* 13500–14256 MHz */
  70. u8 num_outputs;
  71. u8 num_synth;
  72. u16 chip_id;
  73. bool xaxb_ext_clk;
  74. bool iovdd_33;
  75. };
  76. #define to_clk_si5341(_hw) container_of(_hw, struct clk_si5341, hw)
  77. struct clk_si5341_output_config {
  78. u8 out_format_drv_bits;
  79. u8 out_cm_ampl_bits;
  80. u8 vdd_sel_bits;
  81. bool synth_master;
  82. bool always_on;
  83. };
  84. #define SI5341_PAGE 0x0001
  85. #define SI5341_PN_BASE 0x0002
  86. #define SI5341_DEVICE_REV 0x0005
  87. #define SI5341_STATUS 0x000C
  88. #define SI5341_LOS 0x000D
  89. #define SI5341_STATUS_STICKY 0x0011
  90. #define SI5341_LOS_STICKY 0x0012
  91. #define SI5341_SOFT_RST 0x001C
  92. #define SI5341_IN_SEL 0x0021
  93. #define SI5341_DEVICE_READY 0x00FE
  94. #define SI5341_XAXB_CFG 0x090E
  95. #define SI5341_IO_VDD_SEL 0x0943
  96. #define SI5341_IN_EN 0x0949
  97. #define SI5341_INX_TO_PFD_EN 0x094A
  98. /* Status bits */
  99. #define SI5341_STATUS_SYSINCAL BIT(0)
  100. #define SI5341_STATUS_LOSXAXB BIT(1)
  101. #define SI5341_STATUS_LOSREF BIT(2)
  102. #define SI5341_STATUS_LOL BIT(3)
  103. /* Input selection */
  104. #define SI5341_IN_SEL_MASK 0x06
  105. #define SI5341_IN_SEL_SHIFT 1
  106. #define SI5341_IN_SEL_REGCTRL 0x01
  107. #define SI5341_INX_TO_PFD_SHIFT 4
  108. /* XTAL config bits */
  109. #define SI5341_XAXB_CFG_EXTCLK_EN BIT(0)
  110. #define SI5341_XAXB_CFG_PDNB BIT(1)
  111. /* Input dividers (48-bit) */
  112. #define SI5341_IN_PDIV(x) (0x0208 + ((x) * 10))
  113. #define SI5341_IN_PSET(x) (0x020E + ((x) * 10))
  114. #define SI5341_PX_UPD 0x0230
  115. /* PLL configuration */
  116. #define SI5341_PLL_M_NUM 0x0235
  117. #define SI5341_PLL_M_DEN 0x023B
  118. /* Output configuration */
  119. #define SI5341_OUT_CONFIG(output) \
  120. ((output)->data->reg_output_offset[(output)->index])
  121. #define SI5341_OUT_FORMAT(output) (SI5341_OUT_CONFIG(output) + 1)
  122. #define SI5341_OUT_CM(output) (SI5341_OUT_CONFIG(output) + 2)
  123. #define SI5341_OUT_MUX_SEL(output) (SI5341_OUT_CONFIG(output) + 3)
  124. #define SI5341_OUT_R_REG(output) \
  125. ((output)->data->reg_rdiv_offset[(output)->index])
  126. #define SI5341_OUT_MUX_VDD_SEL_MASK 0x38
  127. /* Synthesize N divider */
  128. #define SI5341_SYNTH_N_NUM(x) (0x0302 + ((x) * 11))
  129. #define SI5341_SYNTH_N_DEN(x) (0x0308 + ((x) * 11))
  130. #define SI5341_SYNTH_N_UPD(x) (0x030C + ((x) * 11))
  131. /* Synthesizer output enable, phase bypass, power mode */
  132. #define SI5341_SYNTH_N_CLK_TO_OUTX_EN 0x0A03
  133. #define SI5341_SYNTH_N_PIBYP 0x0A04
  134. #define SI5341_SYNTH_N_PDNB 0x0A05
  135. #define SI5341_SYNTH_N_CLK_DIS 0x0B4A
  136. #define SI5341_REGISTER_MAX 0xBFF
  137. /* SI5341_OUT_CONFIG bits */
  138. #define SI5341_OUT_CFG_PDN BIT(0)
  139. #define SI5341_OUT_CFG_OE BIT(1)
  140. #define SI5341_OUT_CFG_RDIV_FORCE2 BIT(2)
  141. /* Static configuration (to be moved to firmware) */
  142. struct si5341_reg_default {
  143. u16 address;
  144. u8 value;
  145. };
  146. static const char * const si5341_input_clock_names[] = {
  147. "in0", "in1", "in2", "xtal"
  148. };
  149. /* Output configuration registers 0..9 are not quite logically organized */
  150. /* Also for si5345 */
  151. static const u16 si5341_reg_output_offset[] = {
  152. 0x0108,
  153. 0x010D,
  154. 0x0112,
  155. 0x0117,
  156. 0x011C,
  157. 0x0121,
  158. 0x0126,
  159. 0x012B,
  160. 0x0130,
  161. 0x013A,
  162. };
  163. /* for si5340, si5342 and si5344 */
  164. static const u16 si5340_reg_output_offset[] = {
  165. 0x0112,
  166. 0x0117,
  167. 0x0126,
  168. 0x012B,
  169. };
  170. /* The location of the R divider registers */
  171. static const u16 si5341_reg_rdiv_offset[] = {
  172. 0x024A,
  173. 0x024D,
  174. 0x0250,
  175. 0x0253,
  176. 0x0256,
  177. 0x0259,
  178. 0x025C,
  179. 0x025F,
  180. 0x0262,
  181. 0x0268,
  182. };
  183. static const u16 si5340_reg_rdiv_offset[] = {
  184. 0x0250,
  185. 0x0253,
  186. 0x025C,
  187. 0x025F,
  188. };
  189. /*
  190. * Programming sequence from ClockBuilder, settings to initialize the system
  191. * using only the XTAL input, without pre-divider.
  192. * This also contains settings that aren't mentioned anywhere in the datasheet.
  193. * The "known" settings like synth and output configuration are done later.
  194. */
  195. static const struct si5341_reg_default si5341_reg_defaults[] = {
  196. { 0x0017, 0x3A }, /* INT mask (disable interrupts) */
  197. { 0x0018, 0xFF }, /* INT mask */
  198. { 0x0021, 0x0F }, /* Select XTAL as input */
  199. { 0x0022, 0x00 }, /* Not in datasheet */
  200. { 0x002B, 0x02 }, /* SPI config */
  201. { 0x002C, 0x20 }, /* LOS enable for XTAL */
  202. { 0x002D, 0x00 }, /* LOS timing */
  203. { 0x002E, 0x00 },
  204. { 0x002F, 0x00 },
  205. { 0x0030, 0x00 },
  206. { 0x0031, 0x00 },
  207. { 0x0032, 0x00 },
  208. { 0x0033, 0x00 },
  209. { 0x0034, 0x00 },
  210. { 0x0035, 0x00 },
  211. { 0x0036, 0x00 },
  212. { 0x0037, 0x00 },
  213. { 0x0038, 0x00 }, /* LOS setting (thresholds) */
  214. { 0x0039, 0x00 },
  215. { 0x003A, 0x00 },
  216. { 0x003B, 0x00 },
  217. { 0x003C, 0x00 },
  218. { 0x003D, 0x00 }, /* LOS setting (thresholds) end */
  219. { 0x0041, 0x00 }, /* LOS0_DIV_SEL */
  220. { 0x0042, 0x00 }, /* LOS1_DIV_SEL */
  221. { 0x0043, 0x00 }, /* LOS2_DIV_SEL */
  222. { 0x0044, 0x00 }, /* LOS3_DIV_SEL */
  223. { 0x009E, 0x00 }, /* Not in datasheet */
  224. { 0x0102, 0x01 }, /* Enable outputs */
  225. { 0x013F, 0x00 }, /* Not in datasheet */
  226. { 0x0140, 0x00 }, /* Not in datasheet */
  227. { 0x0141, 0x40 }, /* OUT LOS */
  228. { 0x0202, 0x00 }, /* XAXB_FREQ_OFFSET (=0)*/
  229. { 0x0203, 0x00 },
  230. { 0x0204, 0x00 },
  231. { 0x0205, 0x00 },
  232. { 0x0206, 0x00 }, /* PXAXB (2^x) */
  233. { 0x0208, 0x00 }, /* Px divider setting (usually 0) */
  234. { 0x0209, 0x00 },
  235. { 0x020A, 0x00 },
  236. { 0x020B, 0x00 },
  237. { 0x020C, 0x00 },
  238. { 0x020D, 0x00 },
  239. { 0x020E, 0x00 },
  240. { 0x020F, 0x00 },
  241. { 0x0210, 0x00 },
  242. { 0x0211, 0x00 },
  243. { 0x0212, 0x00 },
  244. { 0x0213, 0x00 },
  245. { 0x0214, 0x00 },
  246. { 0x0215, 0x00 },
  247. { 0x0216, 0x00 },
  248. { 0x0217, 0x00 },
  249. { 0x0218, 0x00 },
  250. { 0x0219, 0x00 },
  251. { 0x021A, 0x00 },
  252. { 0x021B, 0x00 },
  253. { 0x021C, 0x00 },
  254. { 0x021D, 0x00 },
  255. { 0x021E, 0x00 },
  256. { 0x021F, 0x00 },
  257. { 0x0220, 0x00 },
  258. { 0x0221, 0x00 },
  259. { 0x0222, 0x00 },
  260. { 0x0223, 0x00 },
  261. { 0x0224, 0x00 },
  262. { 0x0225, 0x00 },
  263. { 0x0226, 0x00 },
  264. { 0x0227, 0x00 },
  265. { 0x0228, 0x00 },
  266. { 0x0229, 0x00 },
  267. { 0x022A, 0x00 },
  268. { 0x022B, 0x00 },
  269. { 0x022C, 0x00 },
  270. { 0x022D, 0x00 },
  271. { 0x022E, 0x00 },
  272. { 0x022F, 0x00 }, /* Px divider setting (usually 0) end */
  273. { 0x026B, 0x00 }, /* DESIGN_ID (ASCII string) */
  274. { 0x026C, 0x00 },
  275. { 0x026D, 0x00 },
  276. { 0x026E, 0x00 },
  277. { 0x026F, 0x00 },
  278. { 0x0270, 0x00 },
  279. { 0x0271, 0x00 },
  280. { 0x0272, 0x00 }, /* DESIGN_ID (ASCII string) end */
  281. { 0x0339, 0x1F }, /* N_FSTEP_MSK */
  282. { 0x033B, 0x00 }, /* Nx_FSTEPW (Frequency step) */
  283. { 0x033C, 0x00 },
  284. { 0x033D, 0x00 },
  285. { 0x033E, 0x00 },
  286. { 0x033F, 0x00 },
  287. { 0x0340, 0x00 },
  288. { 0x0341, 0x00 },
  289. { 0x0342, 0x00 },
  290. { 0x0343, 0x00 },
  291. { 0x0344, 0x00 },
  292. { 0x0345, 0x00 },
  293. { 0x0346, 0x00 },
  294. { 0x0347, 0x00 },
  295. { 0x0348, 0x00 },
  296. { 0x0349, 0x00 },
  297. { 0x034A, 0x00 },
  298. { 0x034B, 0x00 },
  299. { 0x034C, 0x00 },
  300. { 0x034D, 0x00 },
  301. { 0x034E, 0x00 },
  302. { 0x034F, 0x00 },
  303. { 0x0350, 0x00 },
  304. { 0x0351, 0x00 },
  305. { 0x0352, 0x00 },
  306. { 0x0353, 0x00 },
  307. { 0x0354, 0x00 },
  308. { 0x0355, 0x00 },
  309. { 0x0356, 0x00 },
  310. { 0x0357, 0x00 },
  311. { 0x0358, 0x00 }, /* Nx_FSTEPW (Frequency step) end */
  312. { 0x0359, 0x00 }, /* Nx_DELAY */
  313. { 0x035A, 0x00 },
  314. { 0x035B, 0x00 },
  315. { 0x035C, 0x00 },
  316. { 0x035D, 0x00 },
  317. { 0x035E, 0x00 },
  318. { 0x035F, 0x00 },
  319. { 0x0360, 0x00 },
  320. { 0x0361, 0x00 },
  321. { 0x0362, 0x00 }, /* Nx_DELAY end */
  322. { 0x0802, 0x00 }, /* Not in datasheet */
  323. { 0x0803, 0x00 }, /* Not in datasheet */
  324. { 0x0804, 0x00 }, /* Not in datasheet */
  325. { 0x090E, 0x02 }, /* XAXB_EXTCLK_EN=0 XAXB_PDNB=1 (use XTAL) */
  326. { 0x091C, 0x04 }, /* ZDM_EN=4 (Normal mode) */
  327. { 0x0949, 0x00 }, /* IN_EN (disable input clocks) */
  328. { 0x094A, 0x00 }, /* INx_TO_PFD_EN (disabled) */
  329. { 0x0A02, 0x00 }, /* Not in datasheet */
  330. { 0x0B44, 0x0F }, /* PDIV_ENB (datasheet does not mention what it is) */
  331. { 0x0B57, 0x10 }, /* VCO_RESET_CALCODE (not described in datasheet) */
  332. { 0x0B58, 0x05 }, /* VCO_RESET_CALCODE (not described in datasheet) */
  333. };
  334. /* Read and interpret a 44-bit followed by a 32-bit value in the regmap */
  335. static int si5341_decode_44_32(struct regmap *regmap, unsigned int reg,
  336. u64 *val1, u32 *val2)
  337. {
  338. int err;
  339. u8 r[10];
  340. err = regmap_bulk_read(regmap, reg, r, 10);
  341. if (err < 0)
  342. return err;
  343. *val1 = ((u64)((r[5] & 0x0f) << 8 | r[4]) << 32) |
  344. (get_unaligned_le32(r));
  345. *val2 = get_unaligned_le32(&r[6]);
  346. return 0;
  347. }
  348. static int si5341_encode_44_32(struct regmap *regmap, unsigned int reg,
  349. u64 n_num, u32 n_den)
  350. {
  351. u8 r[10];
  352. /* Shift left as far as possible without overflowing */
  353. while (!(n_num & BIT_ULL(43)) && !(n_den & BIT(31))) {
  354. n_num <<= 1;
  355. n_den <<= 1;
  356. }
  357. /* 44 bits (6 bytes) numerator */
  358. put_unaligned_le32(n_num, r);
  359. r[4] = (n_num >> 32) & 0xff;
  360. r[5] = (n_num >> 40) & 0x0f;
  361. /* 32 bits denominator */
  362. put_unaligned_le32(n_den, &r[6]);
  363. /* Program the fraction */
  364. return regmap_bulk_write(regmap, reg, r, sizeof(r));
  365. }
  366. /* VCO, we assume it runs at a constant frequency */
  367. static unsigned long si5341_clk_recalc_rate(struct clk_hw *hw,
  368. unsigned long parent_rate)
  369. {
  370. struct clk_si5341 *data = to_clk_si5341(hw);
  371. int err;
  372. u64 res;
  373. u64 m_num;
  374. u32 m_den;
  375. unsigned int shift;
  376. /* Assume that PDIV is not being used, just read the PLL setting */
  377. err = si5341_decode_44_32(data->regmap, SI5341_PLL_M_NUM,
  378. &m_num, &m_den);
  379. if (err < 0)
  380. return 0;
  381. if (!m_num || !m_den)
  382. return 0;
  383. /*
  384. * Though m_num is 64-bit, only the upper bits are actually used. While
  385. * calculating m_num and m_den, they are shifted as far as possible to
  386. * the left. To avoid 96-bit division here, we just shift them back so
  387. * we can do with just 64 bits.
  388. */
  389. shift = 0;
  390. res = m_num;
  391. while (res & 0xffff00000000ULL) {
  392. ++shift;
  393. res >>= 1;
  394. }
  395. res *= parent_rate;
  396. do_div(res, (m_den >> shift));
  397. /* We cannot return the actual frequency in 32 bit, store it locally */
  398. data->freq_vco = res;
  399. /* Report kHz since the value is out of range */
  400. do_div(res, 1000);
  401. return (unsigned long)res;
  402. }
  403. static int si5341_clk_get_selected_input(struct clk_si5341 *data)
  404. {
  405. int err;
  406. u32 val;
  407. err = regmap_read(data->regmap, SI5341_IN_SEL, &val);
  408. if (err < 0)
  409. return err;
  410. return (val & SI5341_IN_SEL_MASK) >> SI5341_IN_SEL_SHIFT;
  411. }
  412. static u8 si5341_clk_get_parent(struct clk_hw *hw)
  413. {
  414. struct clk_si5341 *data = to_clk_si5341(hw);
  415. int res = si5341_clk_get_selected_input(data);
  416. if (res < 0)
  417. return 0; /* Apparently we cannot report errors */
  418. return res;
  419. }
  420. static int si5341_clk_reparent(struct clk_si5341 *data, u8 index)
  421. {
  422. int err;
  423. u8 val;
  424. val = (index << SI5341_IN_SEL_SHIFT) & SI5341_IN_SEL_MASK;
  425. /* Enable register-based input selection */
  426. val |= SI5341_IN_SEL_REGCTRL;
  427. err = regmap_update_bits(data->regmap,
  428. SI5341_IN_SEL, SI5341_IN_SEL_REGCTRL | SI5341_IN_SEL_MASK, val);
  429. if (err < 0)
  430. return err;
  431. if (index < 3) {
  432. /* Enable input buffer for selected input */
  433. err = regmap_update_bits(data->regmap,
  434. SI5341_IN_EN, 0x07, BIT(index));
  435. if (err < 0)
  436. return err;
  437. /* Enables the input to phase detector */
  438. err = regmap_update_bits(data->regmap, SI5341_INX_TO_PFD_EN,
  439. 0x7 << SI5341_INX_TO_PFD_SHIFT,
  440. BIT(index + SI5341_INX_TO_PFD_SHIFT));
  441. if (err < 0)
  442. return err;
  443. /* Power down XTAL oscillator and buffer */
  444. err = regmap_update_bits(data->regmap, SI5341_XAXB_CFG,
  445. SI5341_XAXB_CFG_PDNB, 0);
  446. if (err < 0)
  447. return err;
  448. /*
  449. * Set the P divider to "1". There's no explanation in the
  450. * datasheet of these registers, but the clockbuilder software
  451. * programs a "1" when the input is being used.
  452. */
  453. err = regmap_write(data->regmap, SI5341_IN_PDIV(index), 1);
  454. if (err < 0)
  455. return err;
  456. err = regmap_write(data->regmap, SI5341_IN_PSET(index), 1);
  457. if (err < 0)
  458. return err;
  459. /* Set update PDIV bit */
  460. err = regmap_write(data->regmap, SI5341_PX_UPD, BIT(index));
  461. if (err < 0)
  462. return err;
  463. } else {
  464. /* Disable all input buffers */
  465. err = regmap_update_bits(data->regmap, SI5341_IN_EN, 0x07, 0);
  466. if (err < 0)
  467. return err;
  468. /* Disable input to phase detector */
  469. err = regmap_update_bits(data->regmap, SI5341_INX_TO_PFD_EN,
  470. 0x7 << SI5341_INX_TO_PFD_SHIFT, 0);
  471. if (err < 0)
  472. return err;
  473. /* Power up XTAL oscillator and buffer, select clock mode */
  474. err = regmap_update_bits(data->regmap, SI5341_XAXB_CFG,
  475. SI5341_XAXB_CFG_PDNB | SI5341_XAXB_CFG_EXTCLK_EN,
  476. SI5341_XAXB_CFG_PDNB | (data->xaxb_ext_clk ?
  477. SI5341_XAXB_CFG_EXTCLK_EN : 0));
  478. if (err < 0)
  479. return err;
  480. }
  481. return 0;
  482. }
  483. static int si5341_clk_set_parent(struct clk_hw *hw, u8 index)
  484. {
  485. struct clk_si5341 *data = to_clk_si5341(hw);
  486. return si5341_clk_reparent(data, index);
  487. }
  488. static const struct clk_ops si5341_clk_ops = {
  489. .determine_rate = clk_hw_determine_rate_no_reparent,
  490. .set_parent = si5341_clk_set_parent,
  491. .get_parent = si5341_clk_get_parent,
  492. .recalc_rate = si5341_clk_recalc_rate,
  493. };
  494. /* Synthesizers, there are 5 synthesizers that connect to any of the outputs */
  495. /* The synthesizer is on if all power and enable bits are set */
  496. static int si5341_synth_clk_is_on(struct clk_hw *hw)
  497. {
  498. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  499. int err;
  500. u32 val;
  501. u8 index = synth->index;
  502. err = regmap_read(synth->data->regmap,
  503. SI5341_SYNTH_N_CLK_TO_OUTX_EN, &val);
  504. if (err < 0)
  505. return 0;
  506. if (!(val & BIT(index)))
  507. return 0;
  508. err = regmap_read(synth->data->regmap, SI5341_SYNTH_N_PDNB, &val);
  509. if (err < 0)
  510. return 0;
  511. if (!(val & BIT(index)))
  512. return 0;
  513. /* This bit must be 0 for the synthesizer to receive clock input */
  514. err = regmap_read(synth->data->regmap, SI5341_SYNTH_N_CLK_DIS, &val);
  515. if (err < 0)
  516. return 0;
  517. return !(val & BIT(index));
  518. }
  519. static void si5341_synth_clk_unprepare(struct clk_hw *hw)
  520. {
  521. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  522. u8 index = synth->index; /* In range 0..5 */
  523. u8 mask = BIT(index);
  524. /* Disable output */
  525. regmap_update_bits(synth->data->regmap,
  526. SI5341_SYNTH_N_CLK_TO_OUTX_EN, mask, 0);
  527. /* Power down */
  528. regmap_update_bits(synth->data->regmap,
  529. SI5341_SYNTH_N_PDNB, mask, 0);
  530. /* Disable clock input to synth (set to 1 to disable) */
  531. regmap_update_bits(synth->data->regmap,
  532. SI5341_SYNTH_N_CLK_DIS, mask, mask);
  533. }
  534. static int si5341_synth_clk_prepare(struct clk_hw *hw)
  535. {
  536. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  537. int err;
  538. u8 index = synth->index;
  539. u8 mask = BIT(index);
  540. /* Power up */
  541. err = regmap_update_bits(synth->data->regmap,
  542. SI5341_SYNTH_N_PDNB, mask, mask);
  543. if (err < 0)
  544. return err;
  545. /* Enable clock input to synth (set bit to 0 to enable) */
  546. err = regmap_update_bits(synth->data->regmap,
  547. SI5341_SYNTH_N_CLK_DIS, mask, 0);
  548. if (err < 0)
  549. return err;
  550. /* Enable output */
  551. return regmap_update_bits(synth->data->regmap,
  552. SI5341_SYNTH_N_CLK_TO_OUTX_EN, mask, mask);
  553. }
  554. /* Synth clock frequency: Fvco * n_den / n_den, with Fvco in 13500-14256 MHz */
  555. static unsigned long si5341_synth_clk_recalc_rate(struct clk_hw *hw,
  556. unsigned long parent_rate)
  557. {
  558. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  559. u64 f;
  560. u64 n_num;
  561. u32 n_den;
  562. int err;
  563. err = si5341_decode_44_32(synth->data->regmap,
  564. SI5341_SYNTH_N_NUM(synth->index), &n_num, &n_den);
  565. if (err < 0)
  566. return err;
  567. /* Check for bogus/uninitialized settings */
  568. if (!n_num || !n_den)
  569. return 0;
  570. /*
  571. * n_num and n_den are shifted left as much as possible, so to prevent
  572. * overflow in 64-bit math, we shift n_den 4 bits to the right
  573. */
  574. f = synth->data->freq_vco;
  575. f *= n_den >> 4;
  576. /* Now we need to do 64-bit division: f/n_num */
  577. /* And compensate for the 4 bits we dropped */
  578. f = div64_u64(f, (n_num >> 4));
  579. return f;
  580. }
  581. static int si5341_synth_clk_determine_rate(struct clk_hw *hw,
  582. struct clk_rate_request *req)
  583. {
  584. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  585. u64 f;
  586. /* The synthesizer accuracy is such that anything in range will work */
  587. f = synth->data->freq_vco;
  588. do_div(f, SI5341_SYNTH_N_MAX);
  589. if (req->rate < f) {
  590. req->rate = f;
  591. return 0;
  592. }
  593. f = synth->data->freq_vco;
  594. do_div(f, SI5341_SYNTH_N_MIN);
  595. if (req->rate > f) {
  596. req->rate = f;
  597. return 0;
  598. }
  599. return 0;
  600. }
  601. static int si5341_synth_program(struct clk_si5341_synth *synth,
  602. u64 n_num, u32 n_den, bool is_integer)
  603. {
  604. int err;
  605. u8 index = synth->index;
  606. err = si5341_encode_44_32(synth->data->regmap,
  607. SI5341_SYNTH_N_NUM(index), n_num, n_den);
  608. err = regmap_update_bits(synth->data->regmap,
  609. SI5341_SYNTH_N_PIBYP, BIT(index), is_integer ? BIT(index) : 0);
  610. if (err < 0)
  611. return err;
  612. return regmap_write(synth->data->regmap,
  613. SI5341_SYNTH_N_UPD(index), 0x01);
  614. }
  615. static int si5341_synth_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  616. unsigned long parent_rate)
  617. {
  618. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  619. u64 n_num;
  620. u32 n_den;
  621. u32 r;
  622. u32 g;
  623. bool is_integer;
  624. n_num = synth->data->freq_vco;
  625. /* see if there's an integer solution */
  626. r = do_div(n_num, rate);
  627. is_integer = (r == 0);
  628. if (is_integer) {
  629. /* Integer divider equal to n_num */
  630. n_den = 1;
  631. } else {
  632. /* Calculate a fractional solution */
  633. g = gcd(r, rate);
  634. n_den = rate / g;
  635. n_num *= n_den;
  636. n_num += r / g;
  637. }
  638. dev_dbg(&synth->data->i2c_client->dev,
  639. "%s(%u): n=0x%llx d=0x%x %s\n", __func__,
  640. synth->index, n_num, n_den,
  641. is_integer ? "int" : "frac");
  642. return si5341_synth_program(synth, n_num, n_den, is_integer);
  643. }
  644. static const struct clk_ops si5341_synth_clk_ops = {
  645. .is_prepared = si5341_synth_clk_is_on,
  646. .prepare = si5341_synth_clk_prepare,
  647. .unprepare = si5341_synth_clk_unprepare,
  648. .recalc_rate = si5341_synth_clk_recalc_rate,
  649. .determine_rate = si5341_synth_clk_determine_rate,
  650. .set_rate = si5341_synth_clk_set_rate,
  651. };
  652. static int si5341_output_clk_is_on(struct clk_hw *hw)
  653. {
  654. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  655. int err;
  656. u32 val;
  657. err = regmap_read(output->data->regmap,
  658. SI5341_OUT_CONFIG(output), &val);
  659. if (err < 0)
  660. return err;
  661. /* Bit 0=PDN, 1=OE so only a value of 0x2 enables the output */
  662. return (val & 0x03) == SI5341_OUT_CFG_OE;
  663. }
  664. /* Disables and then powers down the output */
  665. static void si5341_output_clk_unprepare(struct clk_hw *hw)
  666. {
  667. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  668. regmap_update_bits(output->data->regmap,
  669. SI5341_OUT_CONFIG(output),
  670. SI5341_OUT_CFG_OE, 0);
  671. regmap_update_bits(output->data->regmap,
  672. SI5341_OUT_CONFIG(output),
  673. SI5341_OUT_CFG_PDN, SI5341_OUT_CFG_PDN);
  674. }
  675. /* Powers up and then enables the output */
  676. static int si5341_output_clk_prepare(struct clk_hw *hw)
  677. {
  678. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  679. int err;
  680. err = regmap_update_bits(output->data->regmap,
  681. SI5341_OUT_CONFIG(output),
  682. SI5341_OUT_CFG_PDN, 0);
  683. if (err < 0)
  684. return err;
  685. return regmap_update_bits(output->data->regmap,
  686. SI5341_OUT_CONFIG(output),
  687. SI5341_OUT_CFG_OE, SI5341_OUT_CFG_OE);
  688. }
  689. static unsigned long si5341_output_clk_recalc_rate(struct clk_hw *hw,
  690. unsigned long parent_rate)
  691. {
  692. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  693. int err;
  694. u32 val;
  695. u32 r_divider;
  696. u8 r[3];
  697. err = regmap_read(output->data->regmap,
  698. SI5341_OUT_CONFIG(output), &val);
  699. if (err < 0)
  700. return err;
  701. /* If SI5341_OUT_CFG_RDIV_FORCE2 is set, r_divider is 2 */
  702. if (val & SI5341_OUT_CFG_RDIV_FORCE2)
  703. return parent_rate / 2;
  704. err = regmap_bulk_read(output->data->regmap,
  705. SI5341_OUT_R_REG(output), r, 3);
  706. if (err < 0)
  707. return err;
  708. /* Calculate value as 24-bit integer*/
  709. r_divider = r[2] << 16 | r[1] << 8 | r[0];
  710. /* If Rx_REG is zero, the divider is disabled, so return a "0" rate */
  711. if (!r_divider)
  712. return 0;
  713. /* Divider is 2*(Rx_REG+1) */
  714. r_divider += 1;
  715. r_divider <<= 1;
  716. return parent_rate / r_divider;
  717. }
  718. static int si5341_output_clk_determine_rate(struct clk_hw *hw,
  719. struct clk_rate_request *req)
  720. {
  721. unsigned long rate = req->rate;
  722. unsigned long r;
  723. if (!rate)
  724. return 0;
  725. r = req->best_parent_rate >> 1;
  726. /* If rate is an even divisor, no changes to parent required */
  727. if (r && !(r % rate))
  728. return 0;
  729. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  730. if (rate > 200000000) {
  731. /* minimum r-divider is 2 */
  732. r = 2;
  733. } else {
  734. /* Take a parent frequency near 400 MHz */
  735. r = (400000000u / rate) & ~1;
  736. }
  737. req->best_parent_rate = r * rate;
  738. } else {
  739. /* We cannot change our parent's rate, report what we can do */
  740. r /= rate;
  741. rate = req->best_parent_rate / (r << 1);
  742. }
  743. req->rate = rate;
  744. return 0;
  745. }
  746. static int si5341_output_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  747. unsigned long parent_rate)
  748. {
  749. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  750. u32 r_div;
  751. int err;
  752. u8 r[3];
  753. if (!rate)
  754. return -EINVAL;
  755. /* Frequency divider is (r_div + 1) * 2 */
  756. r_div = (parent_rate / rate) >> 1;
  757. if (r_div <= 1)
  758. r_div = 0;
  759. else if (r_div >= BIT(24))
  760. r_div = BIT(24) - 1;
  761. else
  762. --r_div;
  763. /* For a value of "2", we set the "OUT0_RDIV_FORCE2" bit */
  764. err = regmap_update_bits(output->data->regmap,
  765. SI5341_OUT_CONFIG(output),
  766. SI5341_OUT_CFG_RDIV_FORCE2,
  767. (r_div == 0) ? SI5341_OUT_CFG_RDIV_FORCE2 : 0);
  768. if (err < 0)
  769. return err;
  770. /* Always write Rx_REG, because a zero value disables the divider */
  771. r[0] = r_div ? (r_div & 0xff) : 1;
  772. r[1] = (r_div >> 8) & 0xff;
  773. r[2] = (r_div >> 16) & 0xff;
  774. return regmap_bulk_write(output->data->regmap,
  775. SI5341_OUT_R_REG(output), r, 3);
  776. }
  777. static int si5341_output_reparent(struct clk_si5341_output *output, u8 index)
  778. {
  779. return regmap_update_bits(output->data->regmap,
  780. SI5341_OUT_MUX_SEL(output), 0x07, index);
  781. }
  782. static int si5341_output_set_parent(struct clk_hw *hw, u8 index)
  783. {
  784. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  785. if (index >= output->data->num_synth)
  786. return -EINVAL;
  787. return si5341_output_reparent(output, index);
  788. }
  789. static u8 si5341_output_get_parent(struct clk_hw *hw)
  790. {
  791. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  792. u32 val;
  793. regmap_read(output->data->regmap, SI5341_OUT_MUX_SEL(output), &val);
  794. return val & 0x7;
  795. }
  796. static const struct clk_ops si5341_output_clk_ops = {
  797. .is_prepared = si5341_output_clk_is_on,
  798. .prepare = si5341_output_clk_prepare,
  799. .unprepare = si5341_output_clk_unprepare,
  800. .recalc_rate = si5341_output_clk_recalc_rate,
  801. .determine_rate = si5341_output_clk_determine_rate,
  802. .set_rate = si5341_output_clk_set_rate,
  803. .set_parent = si5341_output_set_parent,
  804. .get_parent = si5341_output_get_parent,
  805. };
  806. /*
  807. * The chip can be bought in a pre-programmed version, or one can program the
  808. * NVM in the chip to boot up in a preset mode. This routine tries to determine
  809. * if that's the case, or if we need to reset and program everything from
  810. * scratch. Returns negative error, or true/false.
  811. */
  812. static int si5341_is_programmed_already(struct clk_si5341 *data)
  813. {
  814. int err;
  815. u8 r[4];
  816. /* Read the PLL divider value, it must have a non-zero value */
  817. err = regmap_bulk_read(data->regmap, SI5341_PLL_M_DEN,
  818. r, ARRAY_SIZE(r));
  819. if (err < 0)
  820. return err;
  821. return !!get_unaligned_le32(r);
  822. }
  823. static struct clk_hw *
  824. of_clk_si5341_get(struct of_phandle_args *clkspec, void *_data)
  825. {
  826. struct clk_si5341 *data = _data;
  827. unsigned int idx = clkspec->args[1];
  828. unsigned int group = clkspec->args[0];
  829. switch (group) {
  830. case 0:
  831. if (idx >= data->num_outputs) {
  832. dev_err(&data->i2c_client->dev,
  833. "invalid output index %u\n", idx);
  834. return ERR_PTR(-EINVAL);
  835. }
  836. return &data->clk[idx].hw;
  837. case 1:
  838. if (idx >= data->num_synth) {
  839. dev_err(&data->i2c_client->dev,
  840. "invalid synthesizer index %u\n", idx);
  841. return ERR_PTR(-EINVAL);
  842. }
  843. return &data->synth[idx].hw;
  844. case 2:
  845. if (idx > 0) {
  846. dev_err(&data->i2c_client->dev,
  847. "invalid PLL index %u\n", idx);
  848. return ERR_PTR(-EINVAL);
  849. }
  850. return &data->hw;
  851. default:
  852. dev_err(&data->i2c_client->dev, "invalid group %u\n", group);
  853. return ERR_PTR(-EINVAL);
  854. }
  855. }
  856. static int si5341_probe_chip_id(struct clk_si5341 *data)
  857. {
  858. int err;
  859. u8 reg[4];
  860. u16 model;
  861. err = regmap_bulk_read(data->regmap, SI5341_PN_BASE, reg,
  862. ARRAY_SIZE(reg));
  863. if (err < 0) {
  864. dev_err(&data->i2c_client->dev, "Failed to read chip ID\n");
  865. return err;
  866. }
  867. model = get_unaligned_le16(reg);
  868. dev_info(&data->i2c_client->dev, "Chip: %x Grade: %u Rev: %u\n",
  869. model, reg[2], reg[3]);
  870. switch (model) {
  871. case 0x5340:
  872. data->num_outputs = SI5340_MAX_NUM_OUTPUTS;
  873. data->num_synth = SI5340_NUM_SYNTH;
  874. data->reg_output_offset = si5340_reg_output_offset;
  875. data->reg_rdiv_offset = si5340_reg_rdiv_offset;
  876. break;
  877. case 0x5341:
  878. data->num_outputs = SI5341_MAX_NUM_OUTPUTS;
  879. data->num_synth = SI5341_NUM_SYNTH;
  880. data->reg_output_offset = si5341_reg_output_offset;
  881. data->reg_rdiv_offset = si5341_reg_rdiv_offset;
  882. break;
  883. case 0x5342:
  884. data->num_outputs = SI5342_MAX_NUM_OUTPUTS;
  885. data->num_synth = SI5342_NUM_SYNTH;
  886. data->reg_output_offset = si5340_reg_output_offset;
  887. data->reg_rdiv_offset = si5340_reg_rdiv_offset;
  888. break;
  889. case 0x5344:
  890. data->num_outputs = SI5344_MAX_NUM_OUTPUTS;
  891. data->num_synth = SI5344_NUM_SYNTH;
  892. data->reg_output_offset = si5340_reg_output_offset;
  893. data->reg_rdiv_offset = si5340_reg_rdiv_offset;
  894. break;
  895. case 0x5345:
  896. data->num_outputs = SI5345_MAX_NUM_OUTPUTS;
  897. data->num_synth = SI5345_NUM_SYNTH;
  898. data->reg_output_offset = si5341_reg_output_offset;
  899. data->reg_rdiv_offset = si5341_reg_rdiv_offset;
  900. break;
  901. default:
  902. dev_err(&data->i2c_client->dev, "Model '%x' not supported\n",
  903. model);
  904. return -EINVAL;
  905. }
  906. data->chip_id = model;
  907. return 0;
  908. }
  909. /* Read active settings into the regmap cache for later reference */
  910. static int si5341_read_settings(struct clk_si5341 *data)
  911. {
  912. int err;
  913. u8 i;
  914. u8 r[10];
  915. err = regmap_bulk_read(data->regmap, SI5341_PLL_M_NUM, r, 10);
  916. if (err < 0)
  917. return err;
  918. err = regmap_bulk_read(data->regmap,
  919. SI5341_SYNTH_N_CLK_TO_OUTX_EN, r, 3);
  920. if (err < 0)
  921. return err;
  922. err = regmap_bulk_read(data->regmap,
  923. SI5341_SYNTH_N_CLK_DIS, r, 1);
  924. if (err < 0)
  925. return err;
  926. for (i = 0; i < data->num_synth; ++i) {
  927. err = regmap_bulk_read(data->regmap,
  928. SI5341_SYNTH_N_NUM(i), r, 10);
  929. if (err < 0)
  930. return err;
  931. }
  932. for (i = 0; i < data->num_outputs; ++i) {
  933. err = regmap_bulk_read(data->regmap,
  934. data->reg_output_offset[i], r, 4);
  935. if (err < 0)
  936. return err;
  937. err = regmap_bulk_read(data->regmap,
  938. data->reg_rdiv_offset[i], r, 3);
  939. if (err < 0)
  940. return err;
  941. }
  942. return 0;
  943. }
  944. static int si5341_write_multiple(struct clk_si5341 *data,
  945. const struct si5341_reg_default *values, unsigned int num_values)
  946. {
  947. unsigned int i;
  948. int res;
  949. for (i = 0; i < num_values; ++i) {
  950. res = regmap_write(data->regmap,
  951. values[i].address, values[i].value);
  952. if (res < 0) {
  953. dev_err(&data->i2c_client->dev,
  954. "Failed to write %#x:%#x\n",
  955. values[i].address, values[i].value);
  956. return res;
  957. }
  958. }
  959. return 0;
  960. }
  961. static const struct si5341_reg_default si5341_preamble[] = {
  962. { 0x0B25, 0x00 },
  963. { 0x0502, 0x01 },
  964. { 0x0505, 0x03 },
  965. { 0x0957, 0x17 },
  966. { 0x0B4E, 0x1A },
  967. };
  968. static const struct si5341_reg_default si5345_preamble[] = {
  969. { 0x0B25, 0x00 },
  970. { 0x0540, 0x01 },
  971. };
  972. static int si5341_send_preamble(struct clk_si5341 *data)
  973. {
  974. int res;
  975. u32 revision;
  976. /* For revision 2 and up, the values are slightly different */
  977. res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
  978. if (res < 0)
  979. return res;
  980. /* Write "preamble" as specified by datasheet */
  981. res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xD8 : 0xC0);
  982. if (res < 0)
  983. return res;
  984. /* The si5342..si5345 require a different preamble */
  985. if (data->chip_id > 0x5341)
  986. res = si5341_write_multiple(data,
  987. si5345_preamble, ARRAY_SIZE(si5345_preamble));
  988. else
  989. res = si5341_write_multiple(data,
  990. si5341_preamble, ARRAY_SIZE(si5341_preamble));
  991. if (res < 0)
  992. return res;
  993. /* Datasheet specifies a 300ms wait after sending the preamble */
  994. msleep(300);
  995. return 0;
  996. }
  997. /* Perform a soft reset and write post-amble */
  998. static int si5341_finalize_defaults(struct clk_si5341 *data)
  999. {
  1000. int res;
  1001. u32 revision;
  1002. res = regmap_write(data->regmap, SI5341_IO_VDD_SEL,
  1003. data->iovdd_33 ? 1 : 0);
  1004. if (res < 0)
  1005. return res;
  1006. res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
  1007. if (res < 0)
  1008. return res;
  1009. dev_dbg(&data->i2c_client->dev, "%s rev=%u\n", __func__, revision);
  1010. res = regmap_write(data->regmap, SI5341_SOFT_RST, 0x01);
  1011. if (res < 0)
  1012. return res;
  1013. /* The si5342..si5345 have an additional post-amble */
  1014. if (data->chip_id > 0x5341) {
  1015. res = regmap_write(data->regmap, 0x540, 0x0);
  1016. if (res < 0)
  1017. return res;
  1018. }
  1019. /* Datasheet does not explain these nameless registers */
  1020. res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xDB : 0xC3);
  1021. if (res < 0)
  1022. return res;
  1023. res = regmap_write(data->regmap, 0x0B25, 0x02);
  1024. if (res < 0)
  1025. return res;
  1026. return 0;
  1027. }
  1028. static const struct regmap_range si5341_regmap_volatile_range[] = {
  1029. regmap_reg_range(0x000C, 0x0012), /* Status */
  1030. regmap_reg_range(0x001C, 0x001E), /* reset, finc/fdec */
  1031. regmap_reg_range(0x00E2, 0x00FE), /* NVM, interrupts, device ready */
  1032. /* Update bits for P divider and synth config */
  1033. regmap_reg_range(SI5341_PX_UPD, SI5341_PX_UPD),
  1034. regmap_reg_range(SI5341_SYNTH_N_UPD(0), SI5341_SYNTH_N_UPD(0)),
  1035. regmap_reg_range(SI5341_SYNTH_N_UPD(1), SI5341_SYNTH_N_UPD(1)),
  1036. regmap_reg_range(SI5341_SYNTH_N_UPD(2), SI5341_SYNTH_N_UPD(2)),
  1037. regmap_reg_range(SI5341_SYNTH_N_UPD(3), SI5341_SYNTH_N_UPD(3)),
  1038. regmap_reg_range(SI5341_SYNTH_N_UPD(4), SI5341_SYNTH_N_UPD(4)),
  1039. };
  1040. static const struct regmap_access_table si5341_regmap_volatile = {
  1041. .yes_ranges = si5341_regmap_volatile_range,
  1042. .n_yes_ranges = ARRAY_SIZE(si5341_regmap_volatile_range),
  1043. };
  1044. /* Pages 0, 1, 2, 3, 9, A, B are valid, so there are 12 pages */
  1045. static const struct regmap_range_cfg si5341_regmap_ranges[] = {
  1046. {
  1047. .range_min = 0,
  1048. .range_max = SI5341_REGISTER_MAX,
  1049. .selector_reg = SI5341_PAGE,
  1050. .selector_mask = 0xff,
  1051. .selector_shift = 0,
  1052. .window_start = 0,
  1053. .window_len = 256,
  1054. },
  1055. };
  1056. static int si5341_wait_device_ready(struct i2c_client *client)
  1057. {
  1058. int count;
  1059. /* Datasheet warns: Any attempt to read or write any register other
  1060. * than DEVICE_READY before DEVICE_READY reads as 0x0F may corrupt the
  1061. * NVM programming and may corrupt the register contents, as they are
  1062. * read from NVM. Note that this includes accesses to the PAGE register.
  1063. * Also: DEVICE_READY is available on every register page, so no page
  1064. * change is needed to read it.
  1065. * Do this outside regmap to avoid automatic PAGE register access.
  1066. * May take up to 300ms to complete.
  1067. */
  1068. for (count = 0; count < 15; ++count) {
  1069. s32 result = i2c_smbus_read_byte_data(client,
  1070. SI5341_DEVICE_READY);
  1071. if (result < 0)
  1072. return result;
  1073. if (result == 0x0F)
  1074. return 0;
  1075. msleep(20);
  1076. }
  1077. dev_err(&client->dev, "timeout waiting for DEVICE_READY\n");
  1078. return -EIO;
  1079. }
  1080. static const struct regmap_config si5341_regmap_config = {
  1081. .reg_bits = 8,
  1082. .val_bits = 8,
  1083. .cache_type = REGCACHE_MAPLE,
  1084. .ranges = si5341_regmap_ranges,
  1085. .num_ranges = ARRAY_SIZE(si5341_regmap_ranges),
  1086. .max_register = SI5341_REGISTER_MAX,
  1087. .volatile_table = &si5341_regmap_volatile,
  1088. };
  1089. static int si5341_dt_parse_dt(struct clk_si5341 *data,
  1090. struct clk_si5341_output_config *config)
  1091. {
  1092. struct device_node *child;
  1093. struct device_node *np = data->i2c_client->dev.of_node;
  1094. u32 num;
  1095. u32 val;
  1096. memset(config, 0, sizeof(struct clk_si5341_output_config) *
  1097. SI5341_MAX_NUM_OUTPUTS);
  1098. for_each_child_of_node(np, child) {
  1099. if (of_property_read_u32(child, "reg", &num)) {
  1100. dev_err(&data->i2c_client->dev, "missing reg property of %s\n",
  1101. child->name);
  1102. goto put_child;
  1103. }
  1104. if (num >= SI5341_MAX_NUM_OUTPUTS) {
  1105. dev_err(&data->i2c_client->dev, "invalid clkout %d\n", num);
  1106. goto put_child;
  1107. }
  1108. if (!of_property_read_u32(child, "silabs,format", &val)) {
  1109. /* Set cm and ampl conservatively to 3v3 settings */
  1110. switch (val) {
  1111. case 1: /* normal differential */
  1112. config[num].out_cm_ampl_bits = 0x33;
  1113. break;
  1114. case 2: /* low-power differential */
  1115. config[num].out_cm_ampl_bits = 0x13;
  1116. break;
  1117. case 4: /* LVCMOS */
  1118. config[num].out_cm_ampl_bits = 0x33;
  1119. /* Set SI recommended impedance for LVCMOS */
  1120. config[num].out_format_drv_bits |= 0xc0;
  1121. break;
  1122. default:
  1123. dev_err(&data->i2c_client->dev,
  1124. "invalid silabs,format %u for %u\n",
  1125. val, num);
  1126. goto put_child;
  1127. }
  1128. config[num].out_format_drv_bits &= ~0x07;
  1129. config[num].out_format_drv_bits |= val & 0x07;
  1130. /* Always enable the SYNC feature */
  1131. config[num].out_format_drv_bits |= 0x08;
  1132. }
  1133. if (!of_property_read_u32(child, "silabs,common-mode", &val)) {
  1134. if (val > 0xf) {
  1135. dev_err(&data->i2c_client->dev,
  1136. "invalid silabs,common-mode %u\n",
  1137. val);
  1138. goto put_child;
  1139. }
  1140. config[num].out_cm_ampl_bits &= 0xf0;
  1141. config[num].out_cm_ampl_bits |= val & 0x0f;
  1142. }
  1143. if (!of_property_read_u32(child, "silabs,amplitude", &val)) {
  1144. if (val > 0xf) {
  1145. dev_err(&data->i2c_client->dev,
  1146. "invalid silabs,amplitude %u\n",
  1147. val);
  1148. goto put_child;
  1149. }
  1150. config[num].out_cm_ampl_bits &= 0x0f;
  1151. config[num].out_cm_ampl_bits |= (val << 4) & 0xf0;
  1152. }
  1153. if (of_property_read_bool(child, "silabs,disable-high"))
  1154. config[num].out_format_drv_bits |= 0x10;
  1155. config[num].synth_master =
  1156. of_property_read_bool(child, "silabs,synth-master");
  1157. config[num].always_on =
  1158. of_property_read_bool(child, "always-on");
  1159. config[num].vdd_sel_bits = 0x08;
  1160. if (data->clk[num].vddo_reg) {
  1161. int vdd = regulator_get_voltage(data->clk[num].vddo_reg);
  1162. switch (vdd) {
  1163. case 3300000:
  1164. config[num].vdd_sel_bits |= 0 << 4;
  1165. break;
  1166. case 1800000:
  1167. config[num].vdd_sel_bits |= 1 << 4;
  1168. break;
  1169. case 2500000:
  1170. config[num].vdd_sel_bits |= 2 << 4;
  1171. break;
  1172. default:
  1173. dev_err(&data->i2c_client->dev,
  1174. "unsupported vddo voltage %d for %s\n",
  1175. vdd, child->name);
  1176. goto put_child;
  1177. }
  1178. } else {
  1179. /* chip seems to default to 2.5V when not set */
  1180. dev_warn(&data->i2c_client->dev,
  1181. "no regulator set, defaulting vdd_sel to 2.5V for %s\n",
  1182. child->name);
  1183. config[num].vdd_sel_bits |= 2 << 4;
  1184. }
  1185. }
  1186. return 0;
  1187. put_child:
  1188. of_node_put(child);
  1189. return -EINVAL;
  1190. }
  1191. /*
  1192. * If not pre-configured, calculate and set the PLL configuration manually.
  1193. * For low-jitter performance, the PLL should be set such that the synthesizers
  1194. * only need integer division.
  1195. * Without any user guidance, we'll set the PLL to 14GHz, which still allows
  1196. * the chip to generate any frequency on its outputs, but jitter performance
  1197. * may be sub-optimal.
  1198. */
  1199. static int si5341_initialize_pll(struct clk_si5341 *data)
  1200. {
  1201. struct device_node *np = data->i2c_client->dev.of_node;
  1202. u32 m_num = 0;
  1203. u32 m_den = 0;
  1204. int sel;
  1205. if (of_property_read_u32(np, "silabs,pll-m-num", &m_num)) {
  1206. dev_err(&data->i2c_client->dev,
  1207. "PLL configuration requires silabs,pll-m-num\n");
  1208. }
  1209. if (of_property_read_u32(np, "silabs,pll-m-den", &m_den)) {
  1210. dev_err(&data->i2c_client->dev,
  1211. "PLL configuration requires silabs,pll-m-den\n");
  1212. }
  1213. if (!m_num || !m_den) {
  1214. dev_err(&data->i2c_client->dev,
  1215. "PLL configuration invalid, assume 14GHz\n");
  1216. sel = si5341_clk_get_selected_input(data);
  1217. if (sel < 0)
  1218. return sel;
  1219. m_den = clk_get_rate(data->input_clk[sel]) / 10;
  1220. m_num = 1400000000;
  1221. }
  1222. return si5341_encode_44_32(data->regmap,
  1223. SI5341_PLL_M_NUM, m_num, m_den);
  1224. }
  1225. static int si5341_clk_select_active_input(struct clk_si5341 *data)
  1226. {
  1227. int res;
  1228. int err;
  1229. int i;
  1230. res = si5341_clk_get_selected_input(data);
  1231. if (res < 0)
  1232. return res;
  1233. /* If the current register setting is invalid, pick the first input */
  1234. if (!data->input_clk[res]) {
  1235. dev_dbg(&data->i2c_client->dev,
  1236. "Input %d not connected, rerouting\n", res);
  1237. res = -ENODEV;
  1238. for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
  1239. if (data->input_clk[i]) {
  1240. res = i;
  1241. break;
  1242. }
  1243. }
  1244. if (res < 0) {
  1245. dev_err(&data->i2c_client->dev,
  1246. "No clock input available\n");
  1247. return res;
  1248. }
  1249. }
  1250. /* Make sure the selected clock is also enabled and routed */
  1251. err = si5341_clk_reparent(data, res);
  1252. if (err < 0)
  1253. return err;
  1254. err = clk_prepare_enable(data->input_clk[res]);
  1255. if (err < 0)
  1256. return err;
  1257. return res;
  1258. }
  1259. static ssize_t input_present_show(struct device *dev,
  1260. struct device_attribute *attr,
  1261. char *buf)
  1262. {
  1263. struct clk_si5341 *data = dev_get_drvdata(dev);
  1264. u32 status;
  1265. int res = regmap_read(data->regmap, SI5341_STATUS, &status);
  1266. if (res < 0)
  1267. return res;
  1268. res = !(status & SI5341_STATUS_LOSREF);
  1269. return sysfs_emit(buf, "%d\n", res);
  1270. }
  1271. static DEVICE_ATTR_RO(input_present);
  1272. static ssize_t input_present_sticky_show(struct device *dev,
  1273. struct device_attribute *attr,
  1274. char *buf)
  1275. {
  1276. struct clk_si5341 *data = dev_get_drvdata(dev);
  1277. u32 status;
  1278. int res = regmap_read(data->regmap, SI5341_STATUS_STICKY, &status);
  1279. if (res < 0)
  1280. return res;
  1281. res = !(status & SI5341_STATUS_LOSREF);
  1282. return sysfs_emit(buf, "%d\n", res);
  1283. }
  1284. static DEVICE_ATTR_RO(input_present_sticky);
  1285. static ssize_t pll_locked_show(struct device *dev,
  1286. struct device_attribute *attr,
  1287. char *buf)
  1288. {
  1289. struct clk_si5341 *data = dev_get_drvdata(dev);
  1290. u32 status;
  1291. int res = regmap_read(data->regmap, SI5341_STATUS, &status);
  1292. if (res < 0)
  1293. return res;
  1294. res = !(status & SI5341_STATUS_LOL);
  1295. return sysfs_emit(buf, "%d\n", res);
  1296. }
  1297. static DEVICE_ATTR_RO(pll_locked);
  1298. static ssize_t pll_locked_sticky_show(struct device *dev,
  1299. struct device_attribute *attr,
  1300. char *buf)
  1301. {
  1302. struct clk_si5341 *data = dev_get_drvdata(dev);
  1303. u32 status;
  1304. int res = regmap_read(data->regmap, SI5341_STATUS_STICKY, &status);
  1305. if (res < 0)
  1306. return res;
  1307. res = !(status & SI5341_STATUS_LOL);
  1308. return sysfs_emit(buf, "%d\n", res);
  1309. }
  1310. static DEVICE_ATTR_RO(pll_locked_sticky);
  1311. static ssize_t clear_sticky_store(struct device *dev,
  1312. struct device_attribute *attr,
  1313. const char *buf, size_t count)
  1314. {
  1315. struct clk_si5341 *data = dev_get_drvdata(dev);
  1316. long val;
  1317. if (kstrtol(buf, 10, &val))
  1318. return -EINVAL;
  1319. if (val) {
  1320. int res = regmap_write(data->regmap, SI5341_STATUS_STICKY, 0);
  1321. if (res < 0)
  1322. return res;
  1323. }
  1324. return count;
  1325. }
  1326. static DEVICE_ATTR_WO(clear_sticky);
  1327. static const struct attribute *si5341_attributes[] = {
  1328. &dev_attr_input_present.attr,
  1329. &dev_attr_input_present_sticky.attr,
  1330. &dev_attr_pll_locked.attr,
  1331. &dev_attr_pll_locked_sticky.attr,
  1332. &dev_attr_clear_sticky.attr,
  1333. NULL
  1334. };
  1335. static int si5341_probe(struct i2c_client *client)
  1336. {
  1337. struct clk_si5341 *data;
  1338. struct clk_init_data init;
  1339. struct clk *input;
  1340. const char *root_clock_name;
  1341. const char *synth_clock_names[SI5341_NUM_SYNTH] = { NULL };
  1342. int err;
  1343. unsigned int i;
  1344. struct clk_si5341_output_config config[SI5341_MAX_NUM_OUTPUTS];
  1345. bool initialization_required;
  1346. u32 status;
  1347. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  1348. if (!data)
  1349. return -ENOMEM;
  1350. data->i2c_client = client;
  1351. /* Must be done before otherwise touching hardware */
  1352. err = si5341_wait_device_ready(client);
  1353. if (err)
  1354. return err;
  1355. for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
  1356. input = devm_clk_get(&client->dev, si5341_input_clock_names[i]);
  1357. if (IS_ERR(input)) {
  1358. if (PTR_ERR(input) == -EPROBE_DEFER)
  1359. return -EPROBE_DEFER;
  1360. data->input_clk_name[i] = si5341_input_clock_names[i];
  1361. } else {
  1362. data->input_clk[i] = input;
  1363. data->input_clk_name[i] = __clk_get_name(input);
  1364. }
  1365. }
  1366. for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
  1367. char reg_name[10];
  1368. snprintf(reg_name, sizeof(reg_name), "vddo%d", i);
  1369. data->clk[i].vddo_reg = devm_regulator_get_optional(
  1370. &client->dev, reg_name);
  1371. if (IS_ERR(data->clk[i].vddo_reg)) {
  1372. err = PTR_ERR(data->clk[i].vddo_reg);
  1373. data->clk[i].vddo_reg = NULL;
  1374. if (err == -ENODEV)
  1375. continue;
  1376. goto cleanup;
  1377. } else {
  1378. err = regulator_enable(data->clk[i].vddo_reg);
  1379. if (err) {
  1380. dev_err(&client->dev,
  1381. "failed to enable %s regulator: %d\n",
  1382. reg_name, err);
  1383. data->clk[i].vddo_reg = NULL;
  1384. goto cleanup;
  1385. }
  1386. }
  1387. }
  1388. err = si5341_dt_parse_dt(data, config);
  1389. if (err)
  1390. goto cleanup;
  1391. if (of_property_read_string(client->dev.of_node, "clock-output-names",
  1392. &init.name))
  1393. init.name = client->dev.of_node->name;
  1394. root_clock_name = init.name;
  1395. data->regmap = devm_regmap_init_i2c(client, &si5341_regmap_config);
  1396. if (IS_ERR(data->regmap)) {
  1397. err = PTR_ERR(data->regmap);
  1398. goto cleanup;
  1399. }
  1400. i2c_set_clientdata(client, data);
  1401. err = si5341_probe_chip_id(data);
  1402. if (err < 0)
  1403. goto cleanup;
  1404. if (of_property_read_bool(client->dev.of_node, "silabs,reprogram")) {
  1405. initialization_required = true;
  1406. } else {
  1407. err = si5341_is_programmed_already(data);
  1408. if (err < 0)
  1409. goto cleanup;
  1410. initialization_required = !err;
  1411. }
  1412. data->xaxb_ext_clk = of_property_read_bool(client->dev.of_node,
  1413. "silabs,xaxb-ext-clk");
  1414. data->iovdd_33 = of_property_read_bool(client->dev.of_node,
  1415. "silabs,iovdd-33");
  1416. if (initialization_required) {
  1417. /* Populate the regmap cache in preparation for "cache only" */
  1418. err = si5341_read_settings(data);
  1419. if (err < 0)
  1420. goto cleanup;
  1421. err = si5341_send_preamble(data);
  1422. if (err < 0)
  1423. goto cleanup;
  1424. /*
  1425. * We intend to send all 'final' register values in a single
  1426. * transaction. So cache all register writes until we're done
  1427. * configuring.
  1428. */
  1429. regcache_cache_only(data->regmap, true);
  1430. /* Write the configuration pairs from the firmware blob */
  1431. err = si5341_write_multiple(data, si5341_reg_defaults,
  1432. ARRAY_SIZE(si5341_reg_defaults));
  1433. if (err < 0)
  1434. goto cleanup;
  1435. }
  1436. /* Input must be up and running at this point */
  1437. err = si5341_clk_select_active_input(data);
  1438. if (err < 0)
  1439. goto cleanup;
  1440. if (initialization_required) {
  1441. /* PLL configuration is required */
  1442. err = si5341_initialize_pll(data);
  1443. if (err < 0)
  1444. goto cleanup;
  1445. }
  1446. /* Register the PLL */
  1447. init.parent_names = data->input_clk_name;
  1448. init.num_parents = SI5341_NUM_INPUTS;
  1449. init.ops = &si5341_clk_ops;
  1450. init.flags = 0;
  1451. data->hw.init = &init;
  1452. err = devm_clk_hw_register(&client->dev, &data->hw);
  1453. if (err) {
  1454. dev_err(&client->dev, "clock registration failed\n");
  1455. goto cleanup;
  1456. }
  1457. init.num_parents = 1;
  1458. init.parent_names = &root_clock_name;
  1459. init.ops = &si5341_synth_clk_ops;
  1460. for (i = 0; i < data->num_synth; ++i) {
  1461. synth_clock_names[i] = devm_kasprintf(&client->dev, GFP_KERNEL,
  1462. "%s.N%u", client->dev.of_node->name, i);
  1463. if (!synth_clock_names[i]) {
  1464. err = -ENOMEM;
  1465. goto free_clk_names;
  1466. }
  1467. init.name = synth_clock_names[i];
  1468. data->synth[i].index = i;
  1469. data->synth[i].data = data;
  1470. data->synth[i].hw.init = &init;
  1471. err = devm_clk_hw_register(&client->dev, &data->synth[i].hw);
  1472. if (err) {
  1473. dev_err(&client->dev,
  1474. "synth N%u registration failed\n", i);
  1475. goto free_clk_names;
  1476. }
  1477. }
  1478. init.num_parents = data->num_synth;
  1479. init.parent_names = synth_clock_names;
  1480. init.ops = &si5341_output_clk_ops;
  1481. for (i = 0; i < data->num_outputs; ++i) {
  1482. init.name = kasprintf(GFP_KERNEL, "%s.%d",
  1483. client->dev.of_node->name, i);
  1484. if (!init.name) {
  1485. err = -ENOMEM;
  1486. goto free_clk_names;
  1487. }
  1488. init.flags = config[i].synth_master ? CLK_SET_RATE_PARENT : 0;
  1489. data->clk[i].index = i;
  1490. data->clk[i].data = data;
  1491. data->clk[i].hw.init = &init;
  1492. if (config[i].out_format_drv_bits & 0x07) {
  1493. regmap_write(data->regmap,
  1494. SI5341_OUT_FORMAT(&data->clk[i]),
  1495. config[i].out_format_drv_bits);
  1496. regmap_write(data->regmap,
  1497. SI5341_OUT_CM(&data->clk[i]),
  1498. config[i].out_cm_ampl_bits);
  1499. regmap_update_bits(data->regmap,
  1500. SI5341_OUT_MUX_SEL(&data->clk[i]),
  1501. SI5341_OUT_MUX_VDD_SEL_MASK,
  1502. config[i].vdd_sel_bits);
  1503. }
  1504. err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
  1505. kfree(init.name); /* clock framework made a copy of the name */
  1506. if (err) {
  1507. dev_err(&client->dev,
  1508. "output %u registration failed\n", i);
  1509. goto free_clk_names;
  1510. }
  1511. if (config[i].always_on)
  1512. clk_prepare(data->clk[i].hw.clk);
  1513. }
  1514. err = devm_of_clk_add_hw_provider(&client->dev, of_clk_si5341_get,
  1515. data);
  1516. if (err) {
  1517. dev_err(&client->dev, "unable to add clk provider\n");
  1518. goto free_clk_names;
  1519. }
  1520. if (initialization_required) {
  1521. /* Synchronize */
  1522. regcache_cache_only(data->regmap, false);
  1523. err = regcache_sync(data->regmap);
  1524. if (err < 0)
  1525. goto free_clk_names;
  1526. err = si5341_finalize_defaults(data);
  1527. if (err < 0)
  1528. goto free_clk_names;
  1529. }
  1530. /* wait for device to report input clock present and PLL lock */
  1531. err = regmap_read_poll_timeout(data->regmap, SI5341_STATUS, status,
  1532. !(status & (SI5341_STATUS_LOSREF | SI5341_STATUS_LOL)),
  1533. 10000, 250000);
  1534. if (err) {
  1535. dev_err(&client->dev, "Error waiting for input clock or PLL lock\n");
  1536. goto free_clk_names;
  1537. }
  1538. /* clear sticky alarm bits from initialization */
  1539. err = regmap_write(data->regmap, SI5341_STATUS_STICKY, 0);
  1540. if (err) {
  1541. dev_err(&client->dev, "unable to clear sticky status\n");
  1542. goto free_clk_names;
  1543. }
  1544. err = sysfs_create_files(&client->dev.kobj, si5341_attributes);
  1545. if (err)
  1546. dev_err(&client->dev, "unable to create sysfs files\n");
  1547. free_clk_names:
  1548. /* Free the names, clk framework makes copies */
  1549. for (i = 0; i < data->num_synth; ++i)
  1550. devm_kfree(&client->dev, (void *)synth_clock_names[i]);
  1551. cleanup:
  1552. if (err) {
  1553. for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
  1554. if (data->clk[i].vddo_reg)
  1555. regulator_disable(data->clk[i].vddo_reg);
  1556. }
  1557. }
  1558. return err;
  1559. }
  1560. static void si5341_remove(struct i2c_client *client)
  1561. {
  1562. struct clk_si5341 *data = i2c_get_clientdata(client);
  1563. int i;
  1564. sysfs_remove_files(&client->dev.kobj, si5341_attributes);
  1565. for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
  1566. if (data->clk[i].vddo_reg)
  1567. regulator_disable(data->clk[i].vddo_reg);
  1568. }
  1569. }
  1570. static const struct i2c_device_id si5341_id[] = {
  1571. { "si5340", 0 },
  1572. { "si5341", 1 },
  1573. { "si5342", 2 },
  1574. { "si5344", 4 },
  1575. { "si5345", 5 },
  1576. { }
  1577. };
  1578. MODULE_DEVICE_TABLE(i2c, si5341_id);
  1579. static const struct of_device_id clk_si5341_of_match[] = {
  1580. { .compatible = "silabs,si5340" },
  1581. { .compatible = "silabs,si5341" },
  1582. { .compatible = "silabs,si5342" },
  1583. { .compatible = "silabs,si5344" },
  1584. { .compatible = "silabs,si5345" },
  1585. { }
  1586. };
  1587. MODULE_DEVICE_TABLE(of, clk_si5341_of_match);
  1588. static struct i2c_driver si5341_driver = {
  1589. .driver = {
  1590. .name = "si5341",
  1591. .of_match_table = clk_si5341_of_match,
  1592. },
  1593. .probe = si5341_probe,
  1594. .remove = si5341_remove,
  1595. .id_table = si5341_id,
  1596. };
  1597. module_i2c_driver(si5341_driver);
  1598. MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
  1599. MODULE_DESCRIPTION("Si5341 driver");
  1600. MODULE_LICENSE("GPL");