clk-si5341.c 47 KB

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