clk-si5341.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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 long si5341_synth_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  582. unsigned long *parent_rate)
  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 (rate < f)
  590. return f;
  591. f = synth->data->freq_vco;
  592. do_div(f, SI5341_SYNTH_N_MIN);
  593. if (rate > f)
  594. return f;
  595. return rate;
  596. }
  597. static int si5341_synth_program(struct clk_si5341_synth *synth,
  598. u64 n_num, u32 n_den, bool is_integer)
  599. {
  600. int err;
  601. u8 index = synth->index;
  602. err = si5341_encode_44_32(synth->data->regmap,
  603. SI5341_SYNTH_N_NUM(index), n_num, n_den);
  604. err = regmap_update_bits(synth->data->regmap,
  605. SI5341_SYNTH_N_PIBYP, BIT(index), is_integer ? BIT(index) : 0);
  606. if (err < 0)
  607. return err;
  608. return regmap_write(synth->data->regmap,
  609. SI5341_SYNTH_N_UPD(index), 0x01);
  610. }
  611. static int si5341_synth_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  612. unsigned long parent_rate)
  613. {
  614. struct clk_si5341_synth *synth = to_clk_si5341_synth(hw);
  615. u64 n_num;
  616. u32 n_den;
  617. u32 r;
  618. u32 g;
  619. bool is_integer;
  620. n_num = synth->data->freq_vco;
  621. /* see if there's an integer solution */
  622. r = do_div(n_num, rate);
  623. is_integer = (r == 0);
  624. if (is_integer) {
  625. /* Integer divider equal to n_num */
  626. n_den = 1;
  627. } else {
  628. /* Calculate a fractional solution */
  629. g = gcd(r, rate);
  630. n_den = rate / g;
  631. n_num *= n_den;
  632. n_num += r / g;
  633. }
  634. dev_dbg(&synth->data->i2c_client->dev,
  635. "%s(%u): n=0x%llx d=0x%x %s\n", __func__,
  636. synth->index, n_num, n_den,
  637. is_integer ? "int" : "frac");
  638. return si5341_synth_program(synth, n_num, n_den, is_integer);
  639. }
  640. static const struct clk_ops si5341_synth_clk_ops = {
  641. .is_prepared = si5341_synth_clk_is_on,
  642. .prepare = si5341_synth_clk_prepare,
  643. .unprepare = si5341_synth_clk_unprepare,
  644. .recalc_rate = si5341_synth_clk_recalc_rate,
  645. .round_rate = si5341_synth_clk_round_rate,
  646. .set_rate = si5341_synth_clk_set_rate,
  647. };
  648. static int si5341_output_clk_is_on(struct clk_hw *hw)
  649. {
  650. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  651. int err;
  652. u32 val;
  653. err = regmap_read(output->data->regmap,
  654. SI5341_OUT_CONFIG(output), &val);
  655. if (err < 0)
  656. return err;
  657. /* Bit 0=PDN, 1=OE so only a value of 0x2 enables the output */
  658. return (val & 0x03) == SI5341_OUT_CFG_OE;
  659. }
  660. /* Disables and then powers down the output */
  661. static void si5341_output_clk_unprepare(struct clk_hw *hw)
  662. {
  663. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  664. regmap_update_bits(output->data->regmap,
  665. SI5341_OUT_CONFIG(output),
  666. SI5341_OUT_CFG_OE, 0);
  667. regmap_update_bits(output->data->regmap,
  668. SI5341_OUT_CONFIG(output),
  669. SI5341_OUT_CFG_PDN, SI5341_OUT_CFG_PDN);
  670. }
  671. /* Powers up and then enables the output */
  672. static int si5341_output_clk_prepare(struct clk_hw *hw)
  673. {
  674. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  675. int err;
  676. err = regmap_update_bits(output->data->regmap,
  677. SI5341_OUT_CONFIG(output),
  678. SI5341_OUT_CFG_PDN, 0);
  679. if (err < 0)
  680. return err;
  681. return regmap_update_bits(output->data->regmap,
  682. SI5341_OUT_CONFIG(output),
  683. SI5341_OUT_CFG_OE, SI5341_OUT_CFG_OE);
  684. }
  685. static unsigned long si5341_output_clk_recalc_rate(struct clk_hw *hw,
  686. unsigned long parent_rate)
  687. {
  688. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  689. int err;
  690. u32 val;
  691. u32 r_divider;
  692. u8 r[3];
  693. err = regmap_read(output->data->regmap,
  694. SI5341_OUT_CONFIG(output), &val);
  695. if (err < 0)
  696. return err;
  697. /* If SI5341_OUT_CFG_RDIV_FORCE2 is set, r_divider is 2 */
  698. if (val & SI5341_OUT_CFG_RDIV_FORCE2)
  699. return parent_rate / 2;
  700. err = regmap_bulk_read(output->data->regmap,
  701. SI5341_OUT_R_REG(output), r, 3);
  702. if (err < 0)
  703. return err;
  704. /* Calculate value as 24-bit integer*/
  705. r_divider = r[2] << 16 | r[1] << 8 | r[0];
  706. /* If Rx_REG is zero, the divider is disabled, so return a "0" rate */
  707. if (!r_divider)
  708. return 0;
  709. /* Divider is 2*(Rx_REG+1) */
  710. r_divider += 1;
  711. r_divider <<= 1;
  712. return parent_rate / r_divider;
  713. }
  714. static int si5341_output_clk_determine_rate(struct clk_hw *hw,
  715. struct clk_rate_request *req)
  716. {
  717. unsigned long rate = req->rate;
  718. unsigned long r;
  719. if (!rate)
  720. return 0;
  721. r = req->best_parent_rate >> 1;
  722. /* If rate is an even divisor, no changes to parent required */
  723. if (r && !(r % rate))
  724. return 0;
  725. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  726. if (rate > 200000000) {
  727. /* minimum r-divider is 2 */
  728. r = 2;
  729. } else {
  730. /* Take a parent frequency near 400 MHz */
  731. r = (400000000u / rate) & ~1;
  732. }
  733. req->best_parent_rate = r * rate;
  734. } else {
  735. /* We cannot change our parent's rate, report what we can do */
  736. r /= rate;
  737. rate = req->best_parent_rate / (r << 1);
  738. }
  739. req->rate = rate;
  740. return 0;
  741. }
  742. static int si5341_output_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  743. unsigned long parent_rate)
  744. {
  745. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  746. u32 r_div;
  747. int err;
  748. u8 r[3];
  749. if (!rate)
  750. return -EINVAL;
  751. /* Frequency divider is (r_div + 1) * 2 */
  752. r_div = (parent_rate / rate) >> 1;
  753. if (r_div <= 1)
  754. r_div = 0;
  755. else if (r_div >= BIT(24))
  756. r_div = BIT(24) - 1;
  757. else
  758. --r_div;
  759. /* For a value of "2", we set the "OUT0_RDIV_FORCE2" bit */
  760. err = regmap_update_bits(output->data->regmap,
  761. SI5341_OUT_CONFIG(output),
  762. SI5341_OUT_CFG_RDIV_FORCE2,
  763. (r_div == 0) ? SI5341_OUT_CFG_RDIV_FORCE2 : 0);
  764. if (err < 0)
  765. return err;
  766. /* Always write Rx_REG, because a zero value disables the divider */
  767. r[0] = r_div ? (r_div & 0xff) : 1;
  768. r[1] = (r_div >> 8) & 0xff;
  769. r[2] = (r_div >> 16) & 0xff;
  770. return regmap_bulk_write(output->data->regmap,
  771. SI5341_OUT_R_REG(output), r, 3);
  772. }
  773. static int si5341_output_reparent(struct clk_si5341_output *output, u8 index)
  774. {
  775. return regmap_update_bits(output->data->regmap,
  776. SI5341_OUT_MUX_SEL(output), 0x07, index);
  777. }
  778. static int si5341_output_set_parent(struct clk_hw *hw, u8 index)
  779. {
  780. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  781. if (index >= output->data->num_synth)
  782. return -EINVAL;
  783. return si5341_output_reparent(output, index);
  784. }
  785. static u8 si5341_output_get_parent(struct clk_hw *hw)
  786. {
  787. struct clk_si5341_output *output = to_clk_si5341_output(hw);
  788. u32 val;
  789. regmap_read(output->data->regmap, SI5341_OUT_MUX_SEL(output), &val);
  790. return val & 0x7;
  791. }
  792. static const struct clk_ops si5341_output_clk_ops = {
  793. .is_prepared = si5341_output_clk_is_on,
  794. .prepare = si5341_output_clk_prepare,
  795. .unprepare = si5341_output_clk_unprepare,
  796. .recalc_rate = si5341_output_clk_recalc_rate,
  797. .determine_rate = si5341_output_clk_determine_rate,
  798. .set_rate = si5341_output_clk_set_rate,
  799. .set_parent = si5341_output_set_parent,
  800. .get_parent = si5341_output_get_parent,
  801. };
  802. /*
  803. * The chip can be bought in a pre-programmed version, or one can program the
  804. * NVM in the chip to boot up in a preset mode. This routine tries to determine
  805. * if that's the case, or if we need to reset and program everything from
  806. * scratch. Returns negative error, or true/false.
  807. */
  808. static int si5341_is_programmed_already(struct clk_si5341 *data)
  809. {
  810. int err;
  811. u8 r[4];
  812. /* Read the PLL divider value, it must have a non-zero value */
  813. err = regmap_bulk_read(data->regmap, SI5341_PLL_M_DEN,
  814. r, ARRAY_SIZE(r));
  815. if (err < 0)
  816. return err;
  817. return !!get_unaligned_le32(r);
  818. }
  819. static struct clk_hw *
  820. of_clk_si5341_get(struct of_phandle_args *clkspec, void *_data)
  821. {
  822. struct clk_si5341 *data = _data;
  823. unsigned int idx = clkspec->args[1];
  824. unsigned int group = clkspec->args[0];
  825. switch (group) {
  826. case 0:
  827. if (idx >= data->num_outputs) {
  828. dev_err(&data->i2c_client->dev,
  829. "invalid output index %u\n", idx);
  830. return ERR_PTR(-EINVAL);
  831. }
  832. return &data->clk[idx].hw;
  833. case 1:
  834. if (idx >= data->num_synth) {
  835. dev_err(&data->i2c_client->dev,
  836. "invalid synthesizer index %u\n", idx);
  837. return ERR_PTR(-EINVAL);
  838. }
  839. return &data->synth[idx].hw;
  840. case 2:
  841. if (idx > 0) {
  842. dev_err(&data->i2c_client->dev,
  843. "invalid PLL index %u\n", idx);
  844. return ERR_PTR(-EINVAL);
  845. }
  846. return &data->hw;
  847. default:
  848. dev_err(&data->i2c_client->dev, "invalid group %u\n", group);
  849. return ERR_PTR(-EINVAL);
  850. }
  851. }
  852. static int si5341_probe_chip_id(struct clk_si5341 *data)
  853. {
  854. int err;
  855. u8 reg[4];
  856. u16 model;
  857. err = regmap_bulk_read(data->regmap, SI5341_PN_BASE, reg,
  858. ARRAY_SIZE(reg));
  859. if (err < 0) {
  860. dev_err(&data->i2c_client->dev, "Failed to read chip ID\n");
  861. return err;
  862. }
  863. model = get_unaligned_le16(reg);
  864. dev_info(&data->i2c_client->dev, "Chip: %x Grade: %u Rev: %u\n",
  865. model, reg[2], reg[3]);
  866. switch (model) {
  867. case 0x5340:
  868. data->num_outputs = SI5340_MAX_NUM_OUTPUTS;
  869. data->num_synth = SI5340_NUM_SYNTH;
  870. data->reg_output_offset = si5340_reg_output_offset;
  871. data->reg_rdiv_offset = si5340_reg_rdiv_offset;
  872. break;
  873. case 0x5341:
  874. data->num_outputs = SI5341_MAX_NUM_OUTPUTS;
  875. data->num_synth = SI5341_NUM_SYNTH;
  876. data->reg_output_offset = si5341_reg_output_offset;
  877. data->reg_rdiv_offset = si5341_reg_rdiv_offset;
  878. break;
  879. case 0x5342:
  880. data->num_outputs = SI5342_MAX_NUM_OUTPUTS;
  881. data->num_synth = SI5342_NUM_SYNTH;
  882. data->reg_output_offset = si5340_reg_output_offset;
  883. data->reg_rdiv_offset = si5340_reg_rdiv_offset;
  884. break;
  885. case 0x5344:
  886. data->num_outputs = SI5344_MAX_NUM_OUTPUTS;
  887. data->num_synth = SI5344_NUM_SYNTH;
  888. data->reg_output_offset = si5340_reg_output_offset;
  889. data->reg_rdiv_offset = si5340_reg_rdiv_offset;
  890. break;
  891. case 0x5345:
  892. data->num_outputs = SI5345_MAX_NUM_OUTPUTS;
  893. data->num_synth = SI5345_NUM_SYNTH;
  894. data->reg_output_offset = si5341_reg_output_offset;
  895. data->reg_rdiv_offset = si5341_reg_rdiv_offset;
  896. break;
  897. default:
  898. dev_err(&data->i2c_client->dev, "Model '%x' not supported\n",
  899. model);
  900. return -EINVAL;
  901. }
  902. data->chip_id = model;
  903. return 0;
  904. }
  905. /* Read active settings into the regmap cache for later reference */
  906. static int si5341_read_settings(struct clk_si5341 *data)
  907. {
  908. int err;
  909. u8 i;
  910. u8 r[10];
  911. err = regmap_bulk_read(data->regmap, SI5341_PLL_M_NUM, r, 10);
  912. if (err < 0)
  913. return err;
  914. err = regmap_bulk_read(data->regmap,
  915. SI5341_SYNTH_N_CLK_TO_OUTX_EN, r, 3);
  916. if (err < 0)
  917. return err;
  918. err = regmap_bulk_read(data->regmap,
  919. SI5341_SYNTH_N_CLK_DIS, r, 1);
  920. if (err < 0)
  921. return err;
  922. for (i = 0; i < data->num_synth; ++i) {
  923. err = regmap_bulk_read(data->regmap,
  924. SI5341_SYNTH_N_NUM(i), r, 10);
  925. if (err < 0)
  926. return err;
  927. }
  928. for (i = 0; i < data->num_outputs; ++i) {
  929. err = regmap_bulk_read(data->regmap,
  930. data->reg_output_offset[i], r, 4);
  931. if (err < 0)
  932. return err;
  933. err = regmap_bulk_read(data->regmap,
  934. data->reg_rdiv_offset[i], r, 3);
  935. if (err < 0)
  936. return err;
  937. }
  938. return 0;
  939. }
  940. static int si5341_write_multiple(struct clk_si5341 *data,
  941. const struct si5341_reg_default *values, unsigned int num_values)
  942. {
  943. unsigned int i;
  944. int res;
  945. for (i = 0; i < num_values; ++i) {
  946. res = regmap_write(data->regmap,
  947. values[i].address, values[i].value);
  948. if (res < 0) {
  949. dev_err(&data->i2c_client->dev,
  950. "Failed to write %#x:%#x\n",
  951. values[i].address, values[i].value);
  952. return res;
  953. }
  954. }
  955. return 0;
  956. }
  957. static const struct si5341_reg_default si5341_preamble[] = {
  958. { 0x0B25, 0x00 },
  959. { 0x0502, 0x01 },
  960. { 0x0505, 0x03 },
  961. { 0x0957, 0x17 },
  962. { 0x0B4E, 0x1A },
  963. };
  964. static const struct si5341_reg_default si5345_preamble[] = {
  965. { 0x0B25, 0x00 },
  966. { 0x0540, 0x01 },
  967. };
  968. static int si5341_send_preamble(struct clk_si5341 *data)
  969. {
  970. int res;
  971. u32 revision;
  972. /* For revision 2 and up, the values are slightly different */
  973. res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
  974. if (res < 0)
  975. return res;
  976. /* Write "preamble" as specified by datasheet */
  977. res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xD8 : 0xC0);
  978. if (res < 0)
  979. return res;
  980. /* The si5342..si5345 require a different preamble */
  981. if (data->chip_id > 0x5341)
  982. res = si5341_write_multiple(data,
  983. si5345_preamble, ARRAY_SIZE(si5345_preamble));
  984. else
  985. res = si5341_write_multiple(data,
  986. si5341_preamble, ARRAY_SIZE(si5341_preamble));
  987. if (res < 0)
  988. return res;
  989. /* Datasheet specifies a 300ms wait after sending the preamble */
  990. msleep(300);
  991. return 0;
  992. }
  993. /* Perform a soft reset and write post-amble */
  994. static int si5341_finalize_defaults(struct clk_si5341 *data)
  995. {
  996. int res;
  997. u32 revision;
  998. res = regmap_write(data->regmap, SI5341_IO_VDD_SEL,
  999. data->iovdd_33 ? 1 : 0);
  1000. if (res < 0)
  1001. return res;
  1002. res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
  1003. if (res < 0)
  1004. return res;
  1005. dev_dbg(&data->i2c_client->dev, "%s rev=%u\n", __func__, revision);
  1006. res = regmap_write(data->regmap, SI5341_SOFT_RST, 0x01);
  1007. if (res < 0)
  1008. return res;
  1009. /* The si5342..si5345 have an additional post-amble */
  1010. if (data->chip_id > 0x5341) {
  1011. res = regmap_write(data->regmap, 0x540, 0x0);
  1012. if (res < 0)
  1013. return res;
  1014. }
  1015. /* Datasheet does not explain these nameless registers */
  1016. res = regmap_write(data->regmap, 0xB24, revision < 2 ? 0xDB : 0xC3);
  1017. if (res < 0)
  1018. return res;
  1019. res = regmap_write(data->regmap, 0x0B25, 0x02);
  1020. if (res < 0)
  1021. return res;
  1022. return 0;
  1023. }
  1024. static const struct regmap_range si5341_regmap_volatile_range[] = {
  1025. regmap_reg_range(0x000C, 0x0012), /* Status */
  1026. regmap_reg_range(0x001C, 0x001E), /* reset, finc/fdec */
  1027. regmap_reg_range(0x00E2, 0x00FE), /* NVM, interrupts, device ready */
  1028. /* Update bits for P divider and synth config */
  1029. regmap_reg_range(SI5341_PX_UPD, SI5341_PX_UPD),
  1030. regmap_reg_range(SI5341_SYNTH_N_UPD(0), SI5341_SYNTH_N_UPD(0)),
  1031. regmap_reg_range(SI5341_SYNTH_N_UPD(1), SI5341_SYNTH_N_UPD(1)),
  1032. regmap_reg_range(SI5341_SYNTH_N_UPD(2), SI5341_SYNTH_N_UPD(2)),
  1033. regmap_reg_range(SI5341_SYNTH_N_UPD(3), SI5341_SYNTH_N_UPD(3)),
  1034. regmap_reg_range(SI5341_SYNTH_N_UPD(4), SI5341_SYNTH_N_UPD(4)),
  1035. };
  1036. static const struct regmap_access_table si5341_regmap_volatile = {
  1037. .yes_ranges = si5341_regmap_volatile_range,
  1038. .n_yes_ranges = ARRAY_SIZE(si5341_regmap_volatile_range),
  1039. };
  1040. /* Pages 0, 1, 2, 3, 9, A, B are valid, so there are 12 pages */
  1041. static const struct regmap_range_cfg si5341_regmap_ranges[] = {
  1042. {
  1043. .range_min = 0,
  1044. .range_max = SI5341_REGISTER_MAX,
  1045. .selector_reg = SI5341_PAGE,
  1046. .selector_mask = 0xff,
  1047. .selector_shift = 0,
  1048. .window_start = 0,
  1049. .window_len = 256,
  1050. },
  1051. };
  1052. static int si5341_wait_device_ready(struct i2c_client *client)
  1053. {
  1054. int count;
  1055. /* Datasheet warns: Any attempt to read or write any register other
  1056. * than DEVICE_READY before DEVICE_READY reads as 0x0F may corrupt the
  1057. * NVM programming and may corrupt the register contents, as they are
  1058. * read from NVM. Note that this includes accesses to the PAGE register.
  1059. * Also: DEVICE_READY is available on every register page, so no page
  1060. * change is needed to read it.
  1061. * Do this outside regmap to avoid automatic PAGE register access.
  1062. * May take up to 300ms to complete.
  1063. */
  1064. for (count = 0; count < 15; ++count) {
  1065. s32 result = i2c_smbus_read_byte_data(client,
  1066. SI5341_DEVICE_READY);
  1067. if (result < 0)
  1068. return result;
  1069. if (result == 0x0F)
  1070. return 0;
  1071. msleep(20);
  1072. }
  1073. dev_err(&client->dev, "timeout waiting for DEVICE_READY\n");
  1074. return -EIO;
  1075. }
  1076. static const struct regmap_config si5341_regmap_config = {
  1077. .reg_bits = 8,
  1078. .val_bits = 8,
  1079. .cache_type = REGCACHE_MAPLE,
  1080. .ranges = si5341_regmap_ranges,
  1081. .num_ranges = ARRAY_SIZE(si5341_regmap_ranges),
  1082. .max_register = SI5341_REGISTER_MAX,
  1083. .volatile_table = &si5341_regmap_volatile,
  1084. };
  1085. static int si5341_dt_parse_dt(struct clk_si5341 *data,
  1086. struct clk_si5341_output_config *config)
  1087. {
  1088. struct device_node *child;
  1089. struct device_node *np = data->i2c_client->dev.of_node;
  1090. u32 num;
  1091. u32 val;
  1092. memset(config, 0, sizeof(struct clk_si5341_output_config) *
  1093. SI5341_MAX_NUM_OUTPUTS);
  1094. for_each_child_of_node(np, child) {
  1095. if (of_property_read_u32(child, "reg", &num)) {
  1096. dev_err(&data->i2c_client->dev, "missing reg property of %s\n",
  1097. child->name);
  1098. goto put_child;
  1099. }
  1100. if (num >= SI5341_MAX_NUM_OUTPUTS) {
  1101. dev_err(&data->i2c_client->dev, "invalid clkout %d\n", num);
  1102. goto put_child;
  1103. }
  1104. if (!of_property_read_u32(child, "silabs,format", &val)) {
  1105. /* Set cm and ampl conservatively to 3v3 settings */
  1106. switch (val) {
  1107. case 1: /* normal differential */
  1108. config[num].out_cm_ampl_bits = 0x33;
  1109. break;
  1110. case 2: /* low-power differential */
  1111. config[num].out_cm_ampl_bits = 0x13;
  1112. break;
  1113. case 4: /* LVCMOS */
  1114. config[num].out_cm_ampl_bits = 0x33;
  1115. /* Set SI recommended impedance for LVCMOS */
  1116. config[num].out_format_drv_bits |= 0xc0;
  1117. break;
  1118. default:
  1119. dev_err(&data->i2c_client->dev,
  1120. "invalid silabs,format %u for %u\n",
  1121. val, num);
  1122. goto put_child;
  1123. }
  1124. config[num].out_format_drv_bits &= ~0x07;
  1125. config[num].out_format_drv_bits |= val & 0x07;
  1126. /* Always enable the SYNC feature */
  1127. config[num].out_format_drv_bits |= 0x08;
  1128. }
  1129. if (!of_property_read_u32(child, "silabs,common-mode", &val)) {
  1130. if (val > 0xf) {
  1131. dev_err(&data->i2c_client->dev,
  1132. "invalid silabs,common-mode %u\n",
  1133. val);
  1134. goto put_child;
  1135. }
  1136. config[num].out_cm_ampl_bits &= 0xf0;
  1137. config[num].out_cm_ampl_bits |= val & 0x0f;
  1138. }
  1139. if (!of_property_read_u32(child, "silabs,amplitude", &val)) {
  1140. if (val > 0xf) {
  1141. dev_err(&data->i2c_client->dev,
  1142. "invalid silabs,amplitude %u\n",
  1143. val);
  1144. goto put_child;
  1145. }
  1146. config[num].out_cm_ampl_bits &= 0x0f;
  1147. config[num].out_cm_ampl_bits |= (val << 4) & 0xf0;
  1148. }
  1149. if (of_property_read_bool(child, "silabs,disable-high"))
  1150. config[num].out_format_drv_bits |= 0x10;
  1151. config[num].synth_master =
  1152. of_property_read_bool(child, "silabs,synth-master");
  1153. config[num].always_on =
  1154. of_property_read_bool(child, "always-on");
  1155. config[num].vdd_sel_bits = 0x08;
  1156. if (data->clk[num].vddo_reg) {
  1157. int vdd = regulator_get_voltage(data->clk[num].vddo_reg);
  1158. switch (vdd) {
  1159. case 3300000:
  1160. config[num].vdd_sel_bits |= 0 << 4;
  1161. break;
  1162. case 1800000:
  1163. config[num].vdd_sel_bits |= 1 << 4;
  1164. break;
  1165. case 2500000:
  1166. config[num].vdd_sel_bits |= 2 << 4;
  1167. break;
  1168. default:
  1169. dev_err(&data->i2c_client->dev,
  1170. "unsupported vddo voltage %d for %s\n",
  1171. vdd, child->name);
  1172. goto put_child;
  1173. }
  1174. } else {
  1175. /* chip seems to default to 2.5V when not set */
  1176. dev_warn(&data->i2c_client->dev,
  1177. "no regulator set, defaulting vdd_sel to 2.5V for %s\n",
  1178. child->name);
  1179. config[num].vdd_sel_bits |= 2 << 4;
  1180. }
  1181. }
  1182. return 0;
  1183. put_child:
  1184. of_node_put(child);
  1185. return -EINVAL;
  1186. }
  1187. /*
  1188. * If not pre-configured, calculate and set the PLL configuration manually.
  1189. * For low-jitter performance, the PLL should be set such that the synthesizers
  1190. * only need integer division.
  1191. * Without any user guidance, we'll set the PLL to 14GHz, which still allows
  1192. * the chip to generate any frequency on its outputs, but jitter performance
  1193. * may be sub-optimal.
  1194. */
  1195. static int si5341_initialize_pll(struct clk_si5341 *data)
  1196. {
  1197. struct device_node *np = data->i2c_client->dev.of_node;
  1198. u32 m_num = 0;
  1199. u32 m_den = 0;
  1200. int sel;
  1201. if (of_property_read_u32(np, "silabs,pll-m-num", &m_num)) {
  1202. dev_err(&data->i2c_client->dev,
  1203. "PLL configuration requires silabs,pll-m-num\n");
  1204. }
  1205. if (of_property_read_u32(np, "silabs,pll-m-den", &m_den)) {
  1206. dev_err(&data->i2c_client->dev,
  1207. "PLL configuration requires silabs,pll-m-den\n");
  1208. }
  1209. if (!m_num || !m_den) {
  1210. dev_err(&data->i2c_client->dev,
  1211. "PLL configuration invalid, assume 14GHz\n");
  1212. sel = si5341_clk_get_selected_input(data);
  1213. if (sel < 0)
  1214. return sel;
  1215. m_den = clk_get_rate(data->input_clk[sel]) / 10;
  1216. m_num = 1400000000;
  1217. }
  1218. return si5341_encode_44_32(data->regmap,
  1219. SI5341_PLL_M_NUM, m_num, m_den);
  1220. }
  1221. static int si5341_clk_select_active_input(struct clk_si5341 *data)
  1222. {
  1223. int res;
  1224. int err;
  1225. int i;
  1226. res = si5341_clk_get_selected_input(data);
  1227. if (res < 0)
  1228. return res;
  1229. /* If the current register setting is invalid, pick the first input */
  1230. if (!data->input_clk[res]) {
  1231. dev_dbg(&data->i2c_client->dev,
  1232. "Input %d not connected, rerouting\n", res);
  1233. res = -ENODEV;
  1234. for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
  1235. if (data->input_clk[i]) {
  1236. res = i;
  1237. break;
  1238. }
  1239. }
  1240. if (res < 0) {
  1241. dev_err(&data->i2c_client->dev,
  1242. "No clock input available\n");
  1243. return res;
  1244. }
  1245. }
  1246. /* Make sure the selected clock is also enabled and routed */
  1247. err = si5341_clk_reparent(data, res);
  1248. if (err < 0)
  1249. return err;
  1250. err = clk_prepare_enable(data->input_clk[res]);
  1251. if (err < 0)
  1252. return err;
  1253. return res;
  1254. }
  1255. static ssize_t input_present_show(struct device *dev,
  1256. struct device_attribute *attr,
  1257. char *buf)
  1258. {
  1259. struct clk_si5341 *data = dev_get_drvdata(dev);
  1260. u32 status;
  1261. int res = regmap_read(data->regmap, SI5341_STATUS, &status);
  1262. if (res < 0)
  1263. return res;
  1264. res = !(status & SI5341_STATUS_LOSREF);
  1265. return sysfs_emit(buf, "%d\n", res);
  1266. }
  1267. static DEVICE_ATTR_RO(input_present);
  1268. static ssize_t input_present_sticky_show(struct device *dev,
  1269. struct device_attribute *attr,
  1270. char *buf)
  1271. {
  1272. struct clk_si5341 *data = dev_get_drvdata(dev);
  1273. u32 status;
  1274. int res = regmap_read(data->regmap, SI5341_STATUS_STICKY, &status);
  1275. if (res < 0)
  1276. return res;
  1277. res = !(status & SI5341_STATUS_LOSREF);
  1278. return sysfs_emit(buf, "%d\n", res);
  1279. }
  1280. static DEVICE_ATTR_RO(input_present_sticky);
  1281. static ssize_t pll_locked_show(struct device *dev,
  1282. struct device_attribute *attr,
  1283. char *buf)
  1284. {
  1285. struct clk_si5341 *data = dev_get_drvdata(dev);
  1286. u32 status;
  1287. int res = regmap_read(data->regmap, SI5341_STATUS, &status);
  1288. if (res < 0)
  1289. return res;
  1290. res = !(status & SI5341_STATUS_LOL);
  1291. return sysfs_emit(buf, "%d\n", res);
  1292. }
  1293. static DEVICE_ATTR_RO(pll_locked);
  1294. static ssize_t pll_locked_sticky_show(struct device *dev,
  1295. struct device_attribute *attr,
  1296. char *buf)
  1297. {
  1298. struct clk_si5341 *data = dev_get_drvdata(dev);
  1299. u32 status;
  1300. int res = regmap_read(data->regmap, SI5341_STATUS_STICKY, &status);
  1301. if (res < 0)
  1302. return res;
  1303. res = !(status & SI5341_STATUS_LOL);
  1304. return sysfs_emit(buf, "%d\n", res);
  1305. }
  1306. static DEVICE_ATTR_RO(pll_locked_sticky);
  1307. static ssize_t clear_sticky_store(struct device *dev,
  1308. struct device_attribute *attr,
  1309. const char *buf, size_t count)
  1310. {
  1311. struct clk_si5341 *data = dev_get_drvdata(dev);
  1312. long val;
  1313. if (kstrtol(buf, 10, &val))
  1314. return -EINVAL;
  1315. if (val) {
  1316. int res = regmap_write(data->regmap, SI5341_STATUS_STICKY, 0);
  1317. if (res < 0)
  1318. return res;
  1319. }
  1320. return count;
  1321. }
  1322. static DEVICE_ATTR_WO(clear_sticky);
  1323. static const struct attribute *si5341_attributes[] = {
  1324. &dev_attr_input_present.attr,
  1325. &dev_attr_input_present_sticky.attr,
  1326. &dev_attr_pll_locked.attr,
  1327. &dev_attr_pll_locked_sticky.attr,
  1328. &dev_attr_clear_sticky.attr,
  1329. NULL
  1330. };
  1331. static int si5341_probe(struct i2c_client *client)
  1332. {
  1333. struct clk_si5341 *data;
  1334. struct clk_init_data init;
  1335. struct clk *input;
  1336. const char *root_clock_name;
  1337. const char *synth_clock_names[SI5341_NUM_SYNTH] = { NULL };
  1338. int err;
  1339. unsigned int i;
  1340. struct clk_si5341_output_config config[SI5341_MAX_NUM_OUTPUTS];
  1341. bool initialization_required;
  1342. u32 status;
  1343. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  1344. if (!data)
  1345. return -ENOMEM;
  1346. data->i2c_client = client;
  1347. /* Must be done before otherwise touching hardware */
  1348. err = si5341_wait_device_ready(client);
  1349. if (err)
  1350. return err;
  1351. for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
  1352. input = devm_clk_get(&client->dev, si5341_input_clock_names[i]);
  1353. if (IS_ERR(input)) {
  1354. if (PTR_ERR(input) == -EPROBE_DEFER)
  1355. return -EPROBE_DEFER;
  1356. data->input_clk_name[i] = si5341_input_clock_names[i];
  1357. } else {
  1358. data->input_clk[i] = input;
  1359. data->input_clk_name[i] = __clk_get_name(input);
  1360. }
  1361. }
  1362. for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
  1363. char reg_name[10];
  1364. snprintf(reg_name, sizeof(reg_name), "vddo%d", i);
  1365. data->clk[i].vddo_reg = devm_regulator_get_optional(
  1366. &client->dev, reg_name);
  1367. if (IS_ERR(data->clk[i].vddo_reg)) {
  1368. err = PTR_ERR(data->clk[i].vddo_reg);
  1369. data->clk[i].vddo_reg = NULL;
  1370. if (err == -ENODEV)
  1371. continue;
  1372. goto cleanup;
  1373. } else {
  1374. err = regulator_enable(data->clk[i].vddo_reg);
  1375. if (err) {
  1376. dev_err(&client->dev,
  1377. "failed to enable %s regulator: %d\n",
  1378. reg_name, err);
  1379. data->clk[i].vddo_reg = NULL;
  1380. goto cleanup;
  1381. }
  1382. }
  1383. }
  1384. err = si5341_dt_parse_dt(data, config);
  1385. if (err)
  1386. goto cleanup;
  1387. if (of_property_read_string(client->dev.of_node, "clock-output-names",
  1388. &init.name))
  1389. init.name = client->dev.of_node->name;
  1390. root_clock_name = init.name;
  1391. data->regmap = devm_regmap_init_i2c(client, &si5341_regmap_config);
  1392. if (IS_ERR(data->regmap)) {
  1393. err = PTR_ERR(data->regmap);
  1394. goto cleanup;
  1395. }
  1396. i2c_set_clientdata(client, data);
  1397. err = si5341_probe_chip_id(data);
  1398. if (err < 0)
  1399. goto cleanup;
  1400. if (of_property_read_bool(client->dev.of_node, "silabs,reprogram")) {
  1401. initialization_required = true;
  1402. } else {
  1403. err = si5341_is_programmed_already(data);
  1404. if (err < 0)
  1405. goto cleanup;
  1406. initialization_required = !err;
  1407. }
  1408. data->xaxb_ext_clk = of_property_read_bool(client->dev.of_node,
  1409. "silabs,xaxb-ext-clk");
  1410. data->iovdd_33 = of_property_read_bool(client->dev.of_node,
  1411. "silabs,iovdd-33");
  1412. if (initialization_required) {
  1413. /* Populate the regmap cache in preparation for "cache only" */
  1414. err = si5341_read_settings(data);
  1415. if (err < 0)
  1416. goto cleanup;
  1417. err = si5341_send_preamble(data);
  1418. if (err < 0)
  1419. goto cleanup;
  1420. /*
  1421. * We intend to send all 'final' register values in a single
  1422. * transaction. So cache all register writes until we're done
  1423. * configuring.
  1424. */
  1425. regcache_cache_only(data->regmap, true);
  1426. /* Write the configuration pairs from the firmware blob */
  1427. err = si5341_write_multiple(data, si5341_reg_defaults,
  1428. ARRAY_SIZE(si5341_reg_defaults));
  1429. if (err < 0)
  1430. goto cleanup;
  1431. }
  1432. /* Input must be up and running at this point */
  1433. err = si5341_clk_select_active_input(data);
  1434. if (err < 0)
  1435. goto cleanup;
  1436. if (initialization_required) {
  1437. /* PLL configuration is required */
  1438. err = si5341_initialize_pll(data);
  1439. if (err < 0)
  1440. goto cleanup;
  1441. }
  1442. /* Register the PLL */
  1443. init.parent_names = data->input_clk_name;
  1444. init.num_parents = SI5341_NUM_INPUTS;
  1445. init.ops = &si5341_clk_ops;
  1446. init.flags = 0;
  1447. data->hw.init = &init;
  1448. err = devm_clk_hw_register(&client->dev, &data->hw);
  1449. if (err) {
  1450. dev_err(&client->dev, "clock registration failed\n");
  1451. goto cleanup;
  1452. }
  1453. init.num_parents = 1;
  1454. init.parent_names = &root_clock_name;
  1455. init.ops = &si5341_synth_clk_ops;
  1456. for (i = 0; i < data->num_synth; ++i) {
  1457. synth_clock_names[i] = devm_kasprintf(&client->dev, GFP_KERNEL,
  1458. "%s.N%u", client->dev.of_node->name, i);
  1459. if (!synth_clock_names[i]) {
  1460. err = -ENOMEM;
  1461. goto free_clk_names;
  1462. }
  1463. init.name = synth_clock_names[i];
  1464. data->synth[i].index = i;
  1465. data->synth[i].data = data;
  1466. data->synth[i].hw.init = &init;
  1467. err = devm_clk_hw_register(&client->dev, &data->synth[i].hw);
  1468. if (err) {
  1469. dev_err(&client->dev,
  1470. "synth N%u registration failed\n", i);
  1471. goto free_clk_names;
  1472. }
  1473. }
  1474. init.num_parents = data->num_synth;
  1475. init.parent_names = synth_clock_names;
  1476. init.ops = &si5341_output_clk_ops;
  1477. for (i = 0; i < data->num_outputs; ++i) {
  1478. init.name = kasprintf(GFP_KERNEL, "%s.%d",
  1479. client->dev.of_node->name, i);
  1480. if (!init.name) {
  1481. err = -ENOMEM;
  1482. goto free_clk_names;
  1483. }
  1484. init.flags = config[i].synth_master ? CLK_SET_RATE_PARENT : 0;
  1485. data->clk[i].index = i;
  1486. data->clk[i].data = data;
  1487. data->clk[i].hw.init = &init;
  1488. if (config[i].out_format_drv_bits & 0x07) {
  1489. regmap_write(data->regmap,
  1490. SI5341_OUT_FORMAT(&data->clk[i]),
  1491. config[i].out_format_drv_bits);
  1492. regmap_write(data->regmap,
  1493. SI5341_OUT_CM(&data->clk[i]),
  1494. config[i].out_cm_ampl_bits);
  1495. regmap_update_bits(data->regmap,
  1496. SI5341_OUT_MUX_SEL(&data->clk[i]),
  1497. SI5341_OUT_MUX_VDD_SEL_MASK,
  1498. config[i].vdd_sel_bits);
  1499. }
  1500. err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
  1501. kfree(init.name); /* clock framework made a copy of the name */
  1502. if (err) {
  1503. dev_err(&client->dev,
  1504. "output %u registration failed\n", i);
  1505. goto free_clk_names;
  1506. }
  1507. if (config[i].always_on)
  1508. clk_prepare(data->clk[i].hw.clk);
  1509. }
  1510. err = devm_of_clk_add_hw_provider(&client->dev, of_clk_si5341_get,
  1511. data);
  1512. if (err) {
  1513. dev_err(&client->dev, "unable to add clk provider\n");
  1514. goto free_clk_names;
  1515. }
  1516. if (initialization_required) {
  1517. /* Synchronize */
  1518. regcache_cache_only(data->regmap, false);
  1519. err = regcache_sync(data->regmap);
  1520. if (err < 0)
  1521. goto free_clk_names;
  1522. err = si5341_finalize_defaults(data);
  1523. if (err < 0)
  1524. goto free_clk_names;
  1525. }
  1526. /* wait for device to report input clock present and PLL lock */
  1527. err = regmap_read_poll_timeout(data->regmap, SI5341_STATUS, status,
  1528. !(status & (SI5341_STATUS_LOSREF | SI5341_STATUS_LOL)),
  1529. 10000, 250000);
  1530. if (err) {
  1531. dev_err(&client->dev, "Error waiting for input clock or PLL lock\n");
  1532. goto free_clk_names;
  1533. }
  1534. /* clear sticky alarm bits from initialization */
  1535. err = regmap_write(data->regmap, SI5341_STATUS_STICKY, 0);
  1536. if (err) {
  1537. dev_err(&client->dev, "unable to clear sticky status\n");
  1538. goto free_clk_names;
  1539. }
  1540. err = sysfs_create_files(&client->dev.kobj, si5341_attributes);
  1541. if (err)
  1542. dev_err(&client->dev, "unable to create sysfs files\n");
  1543. free_clk_names:
  1544. /* Free the names, clk framework makes copies */
  1545. for (i = 0; i < data->num_synth; ++i)
  1546. devm_kfree(&client->dev, (void *)synth_clock_names[i]);
  1547. cleanup:
  1548. if (err) {
  1549. for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
  1550. if (data->clk[i].vddo_reg)
  1551. regulator_disable(data->clk[i].vddo_reg);
  1552. }
  1553. }
  1554. return err;
  1555. }
  1556. static void si5341_remove(struct i2c_client *client)
  1557. {
  1558. struct clk_si5341 *data = i2c_get_clientdata(client);
  1559. int i;
  1560. sysfs_remove_files(&client->dev.kobj, si5341_attributes);
  1561. for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
  1562. if (data->clk[i].vddo_reg)
  1563. regulator_disable(data->clk[i].vddo_reg);
  1564. }
  1565. }
  1566. static const struct i2c_device_id si5341_id[] = {
  1567. { "si5340", 0 },
  1568. { "si5341", 1 },
  1569. { "si5342", 2 },
  1570. { "si5344", 4 },
  1571. { "si5345", 5 },
  1572. { }
  1573. };
  1574. MODULE_DEVICE_TABLE(i2c, si5341_id);
  1575. static const struct of_device_id clk_si5341_of_match[] = {
  1576. { .compatible = "silabs,si5340" },
  1577. { .compatible = "silabs,si5341" },
  1578. { .compatible = "silabs,si5342" },
  1579. { .compatible = "silabs,si5344" },
  1580. { .compatible = "silabs,si5345" },
  1581. { }
  1582. };
  1583. MODULE_DEVICE_TABLE(of, clk_si5341_of_match);
  1584. static struct i2c_driver si5341_driver = {
  1585. .driver = {
  1586. .name = "si5341",
  1587. .of_match_table = clk_si5341_of_match,
  1588. },
  1589. .probe = si5341_probe,
  1590. .remove = si5341_remove,
  1591. .id_table = si5341_id,
  1592. };
  1593. module_i2c_driver(si5341_driver);
  1594. MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
  1595. MODULE_DESCRIPTION("Si5341 driver");
  1596. MODULE_LICENSE("GPL");