clkgen-fsyn.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics R&D Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. /*
  10. * Authors:
  11. * Stephen Gallimore <stephen.gallimore@st.com>,
  12. * Pankaj Dev <pankaj.dev@st.com>.
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/of_address.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include "clkgen.h"
  19. /*
  20. * Maximum input clock to the PLL before we divide it down by 2
  21. * although in reality in actual systems this has never been seen to
  22. * be used.
  23. */
  24. #define QUADFS_NDIV_THRESHOLD 30000000
  25. #define PLL_BW_GOODREF (0L)
  26. #define PLL_BW_VBADREF (1L)
  27. #define PLL_BW_BADREF (2L)
  28. #define PLL_BW_VGOODREF (3L)
  29. #define QUADFS_MAX_CHAN 4
  30. struct stm_fs {
  31. unsigned long ndiv;
  32. unsigned long mdiv;
  33. unsigned long pe;
  34. unsigned long sdiv;
  35. unsigned long nsdiv;
  36. };
  37. static const struct stm_fs fs216c65_rtbl[] = {
  38. { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 312.5 Khz */
  39. { .mdiv = 0x17, .pe = 0x25ed, .sdiv = 0x1, .nsdiv = 0 }, /* 27 MHz */
  40. { .mdiv = 0x1a, .pe = 0x7b36, .sdiv = 0x2, .nsdiv = 1 }, /* 36.87 MHz */
  41. { .mdiv = 0x13, .pe = 0x0, .sdiv = 0x2, .nsdiv = 1 }, /* 48 MHz */
  42. { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x1, .nsdiv = 1 }, /* 108 MHz */
  43. };
  44. static const struct stm_fs fs432c65_rtbl[] = {
  45. { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 625 Khz */
  46. { .mdiv = 0x13, .pe = 0x777c, .sdiv = 0x4, .nsdiv = 1 }, /* 25.175 MHz */
  47. { .mdiv = 0x19, .pe = 0x4d35, .sdiv = 0x2, .nsdiv = 0 }, /* 25.200 MHz */
  48. { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x4, .nsdiv = 1 }, /* 27.000 MHz */
  49. { .mdiv = 0x17, .pe = 0x28f5, .sdiv = 0x2, .nsdiv = 0 }, /* 27.027 MHz */
  50. { .mdiv = 0x16, .pe = 0x3359, .sdiv = 0x2, .nsdiv = 0 }, /* 28.320 MHz */
  51. { .mdiv = 0x1f, .pe = 0x2083, .sdiv = 0x3, .nsdiv = 1 }, /* 30.240 MHz */
  52. { .mdiv = 0x1e, .pe = 0x430d, .sdiv = 0x3, .nsdiv = 1 }, /* 31.500 MHz */
  53. { .mdiv = 0x17, .pe = 0x0, .sdiv = 0x3, .nsdiv = 1 }, /* 40.000 MHz */
  54. { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x1, .nsdiv = 0 }, /* 49.500 MHz */
  55. { .mdiv = 0x13, .pe = 0x6667, .sdiv = 0x3, .nsdiv = 1 }, /* 50.000 MHz */
  56. { .mdiv = 0x10, .pe = 0x1ee6, .sdiv = 0x3, .nsdiv = 1 }, /* 57.284 MHz */
  57. { .mdiv = 0x1d, .pe = 0x3b14, .sdiv = 0x2, .nsdiv = 1 }, /* 65.000 MHz */
  58. { .mdiv = 0x12, .pe = 0x7c65, .sdiv = 0x1, .nsdiv = 0 }, /* 71.000 MHz */
  59. { .mdiv = 0x19, .pe = 0xecd, .sdiv = 0x2, .nsdiv = 1 }, /* 74.176 MHz */
  60. { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x2, .nsdiv = 1 }, /* 74.250 MHz */
  61. { .mdiv = 0x19, .pe = 0x3334, .sdiv = 0x2, .nsdiv = 1 }, /* 75.000 MHz */
  62. { .mdiv = 0x18, .pe = 0x5138, .sdiv = 0x2, .nsdiv = 1 }, /* 78.800 MHz */
  63. { .mdiv = 0x1d, .pe = 0x77d, .sdiv = 0x0, .nsdiv = 0 }, /* 85.500 MHz */
  64. { .mdiv = 0x1c, .pe = 0x13d5, .sdiv = 0x0, .nsdiv = 0 }, /* 88.750 MHz */
  65. { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x2, .nsdiv = 1 }, /* 108.000 MHz */
  66. { .mdiv = 0x17, .pe = 0x28f5, .sdiv = 0x0, .nsdiv = 0 }, /* 108.108 MHz */
  67. { .mdiv = 0x10, .pe = 0x6e26, .sdiv = 0x2, .nsdiv = 1 }, /* 118.963 MHz */
  68. { .mdiv = 0x15, .pe = 0x3e63, .sdiv = 0x0, .nsdiv = 0 }, /* 119.000 MHz */
  69. { .mdiv = 0x1c, .pe = 0x471d, .sdiv = 0x1, .nsdiv = 1 }, /* 135.000 MHz */
  70. { .mdiv = 0x19, .pe = 0xecd, .sdiv = 0x1, .nsdiv = 1 }, /* 148.352 MHz */
  71. { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x1, .nsdiv = 1 }, /* 148.500 MHz */
  72. { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x0, .nsdiv = 1 }, /* 297 MHz */
  73. };
  74. static const struct stm_fs fs660c32_rtbl[] = {
  75. { .mdiv = 0x14, .pe = 0x376b, .sdiv = 0x4, .nsdiv = 1 }, /* 25.175 MHz */
  76. { .mdiv = 0x14, .pe = 0x30c3, .sdiv = 0x4, .nsdiv = 1 }, /* 25.200 MHz */
  77. { .mdiv = 0x10, .pe = 0x71c7, .sdiv = 0x4, .nsdiv = 1 }, /* 27.000 MHz */
  78. { .mdiv = 0x00, .pe = 0x47af, .sdiv = 0x3, .nsdiv = 0 }, /* 27.027 MHz */
  79. { .mdiv = 0x0e, .pe = 0x4e1a, .sdiv = 0x4, .nsdiv = 1 }, /* 28.320 MHz */
  80. { .mdiv = 0x0b, .pe = 0x534d, .sdiv = 0x4, .nsdiv = 1 }, /* 30.240 MHz */
  81. { .mdiv = 0x17, .pe = 0x6fbf, .sdiv = 0x2, .nsdiv = 0 }, /* 31.500 MHz */
  82. { .mdiv = 0x01, .pe = 0x0, .sdiv = 0x4, .nsdiv = 1 }, /* 40.000 MHz */
  83. { .mdiv = 0x15, .pe = 0x2aab, .sdiv = 0x3, .nsdiv = 1 }, /* 49.500 MHz */
  84. { .mdiv = 0x14, .pe = 0x6666, .sdiv = 0x3, .nsdiv = 1 }, /* 50.000 MHz */
  85. { .mdiv = 0x1d, .pe = 0x395f, .sdiv = 0x1, .nsdiv = 0 }, /* 57.284 MHz */
  86. { .mdiv = 0x08, .pe = 0x4ec5, .sdiv = 0x3, .nsdiv = 1 }, /* 65.000 MHz */
  87. { .mdiv = 0x05, .pe = 0x1770, .sdiv = 0x3, .nsdiv = 1 }, /* 71.000 MHz */
  88. { .mdiv = 0x03, .pe = 0x4ba7, .sdiv = 0x3, .nsdiv = 1 }, /* 74.176 MHz */
  89. { .mdiv = 0x0f, .pe = 0x3426, .sdiv = 0x1, .nsdiv = 0 }, /* 74.250 MHz */
  90. { .mdiv = 0x0e, .pe = 0x7777, .sdiv = 0x1, .nsdiv = 0 }, /* 75.000 MHz */
  91. { .mdiv = 0x01, .pe = 0x4053, .sdiv = 0x3, .nsdiv = 1 }, /* 78.800 MHz */
  92. { .mdiv = 0x09, .pe = 0x15b5, .sdiv = 0x1, .nsdiv = 0 }, /* 85.500 MHz */
  93. { .mdiv = 0x1b, .pe = 0x3f19, .sdiv = 0x2, .nsdiv = 1 }, /* 88.750 MHz */
  94. { .mdiv = 0x10, .pe = 0x71c7, .sdiv = 0x2, .nsdiv = 1 }, /* 108.000 MHz */
  95. { .mdiv = 0x00, .pe = 0x47af, .sdiv = 0x1, .nsdiv = 0 }, /* 108.108 MHz */
  96. { .mdiv = 0x0c, .pe = 0x3118, .sdiv = 0x2, .nsdiv = 1 }, /* 118.963 MHz */
  97. { .mdiv = 0x0c, .pe = 0x2f54, .sdiv = 0x2, .nsdiv = 1 }, /* 119.000 MHz */
  98. { .mdiv = 0x07, .pe = 0xe39, .sdiv = 0x2, .nsdiv = 1 }, /* 135.000 MHz */
  99. { .mdiv = 0x03, .pe = 0x4ba7, .sdiv = 0x2, .nsdiv = 1 }, /* 148.352 MHz */
  100. { .mdiv = 0x0f, .pe = 0x3426, .sdiv = 0x0, .nsdiv = 0 }, /* 148.500 MHz */
  101. { .mdiv = 0x03, .pe = 0x4ba7, .sdiv = 0x1, .nsdiv = 1 }, /* 296.704 MHz */
  102. { .mdiv = 0x03, .pe = 0x471c, .sdiv = 0x1, .nsdiv = 1 }, /* 297.000 MHz */
  103. { .mdiv = 0x00, .pe = 0x295f, .sdiv = 0x1, .nsdiv = 1 }, /* 326.700 MHz */
  104. { .mdiv = 0x1f, .pe = 0x3633, .sdiv = 0x0, .nsdiv = 1 }, /* 333.000 MHz */
  105. { .mdiv = 0x1c, .pe = 0x0, .sdiv = 0x0, .nsdiv = 1 }, /* 352.000 Mhz */
  106. };
  107. struct clkgen_quadfs_data {
  108. bool reset_present;
  109. bool bwfilter_present;
  110. bool lockstatus_present;
  111. bool powerup_polarity;
  112. bool standby_polarity;
  113. bool nsdiv_present;
  114. bool nrst_present;
  115. struct clkgen_field ndiv;
  116. struct clkgen_field ref_bw;
  117. struct clkgen_field nreset;
  118. struct clkgen_field npda;
  119. struct clkgen_field lock_status;
  120. struct clkgen_field nrst[QUADFS_MAX_CHAN];
  121. struct clkgen_field nsb[QUADFS_MAX_CHAN];
  122. struct clkgen_field en[QUADFS_MAX_CHAN];
  123. struct clkgen_field mdiv[QUADFS_MAX_CHAN];
  124. struct clkgen_field pe[QUADFS_MAX_CHAN];
  125. struct clkgen_field sdiv[QUADFS_MAX_CHAN];
  126. struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
  127. const struct clk_ops *pll_ops;
  128. const struct stm_fs *rtbl;
  129. u8 rtbl_cnt;
  130. int (*get_rate)(unsigned long , const struct stm_fs *,
  131. unsigned long *);
  132. };
  133. static const struct clk_ops st_quadfs_pll_c65_ops;
  134. static const struct clk_ops st_quadfs_pll_c32_ops;
  135. static const struct clk_ops st_quadfs_fs216c65_ops;
  136. static const struct clk_ops st_quadfs_fs432c65_ops;
  137. static const struct clk_ops st_quadfs_fs660c32_ops;
  138. static int clk_fs216c65_get_rate(unsigned long, const struct stm_fs *,
  139. unsigned long *);
  140. static int clk_fs432c65_get_rate(unsigned long, const struct stm_fs *,
  141. unsigned long *);
  142. static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *,
  143. unsigned long *);
  144. /*
  145. * Values for all of the standalone instances of this clock
  146. * generator found in STiH415 and STiH416 SYSCFG register banks. Note
  147. * that the individual channel standby control bits (nsb) are in the
  148. * first register along with the PLL control bits.
  149. */
  150. static const struct clkgen_quadfs_data st_fs216c65_416 = {
  151. /* 416 specific */
  152. .npda = CLKGEN_FIELD(0x0, 0x1, 14),
  153. .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
  154. CLKGEN_FIELD(0x0, 0x1, 11),
  155. CLKGEN_FIELD(0x0, 0x1, 12),
  156. CLKGEN_FIELD(0x0, 0x1, 13) },
  157. .nsdiv_present = true,
  158. .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
  159. CLKGEN_FIELD(0x0, 0x1, 19),
  160. CLKGEN_FIELD(0x0, 0x1, 20),
  161. CLKGEN_FIELD(0x0, 0x1, 21) },
  162. .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
  163. CLKGEN_FIELD(0x14, 0x1f, 0),
  164. CLKGEN_FIELD(0x24, 0x1f, 0),
  165. CLKGEN_FIELD(0x34, 0x1f, 0) },
  166. .en = { CLKGEN_FIELD(0x10, 0x1, 0),
  167. CLKGEN_FIELD(0x20, 0x1, 0),
  168. CLKGEN_FIELD(0x30, 0x1, 0),
  169. CLKGEN_FIELD(0x40, 0x1, 0) },
  170. .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
  171. .bwfilter_present = true,
  172. .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
  173. .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
  174. CLKGEN_FIELD(0x18, 0xffff, 0),
  175. CLKGEN_FIELD(0x28, 0xffff, 0),
  176. CLKGEN_FIELD(0x38, 0xffff, 0) },
  177. .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
  178. CLKGEN_FIELD(0x1C, 0x7, 0),
  179. CLKGEN_FIELD(0x2C, 0x7, 0),
  180. CLKGEN_FIELD(0x3C, 0x7, 0) },
  181. .pll_ops = &st_quadfs_pll_c65_ops,
  182. .rtbl = fs216c65_rtbl,
  183. .rtbl_cnt = ARRAY_SIZE(fs216c65_rtbl),
  184. .get_rate = clk_fs216c65_get_rate,
  185. };
  186. static const struct clkgen_quadfs_data st_fs432c65_416 = {
  187. .npda = CLKGEN_FIELD(0x0, 0x1, 14),
  188. .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
  189. CLKGEN_FIELD(0x0, 0x1, 11),
  190. CLKGEN_FIELD(0x0, 0x1, 12),
  191. CLKGEN_FIELD(0x0, 0x1, 13) },
  192. .nsdiv_present = true,
  193. .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
  194. CLKGEN_FIELD(0x0, 0x1, 19),
  195. CLKGEN_FIELD(0x0, 0x1, 20),
  196. CLKGEN_FIELD(0x0, 0x1, 21) },
  197. .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
  198. CLKGEN_FIELD(0x14, 0x1f, 0),
  199. CLKGEN_FIELD(0x24, 0x1f, 0),
  200. CLKGEN_FIELD(0x34, 0x1f, 0) },
  201. .en = { CLKGEN_FIELD(0x10, 0x1, 0),
  202. CLKGEN_FIELD(0x20, 0x1, 0),
  203. CLKGEN_FIELD(0x30, 0x1, 0),
  204. CLKGEN_FIELD(0x40, 0x1, 0) },
  205. .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
  206. .bwfilter_present = true,
  207. .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
  208. .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
  209. CLKGEN_FIELD(0x18, 0xffff, 0),
  210. CLKGEN_FIELD(0x28, 0xffff, 0),
  211. CLKGEN_FIELD(0x38, 0xffff, 0) },
  212. .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
  213. CLKGEN_FIELD(0x1C, 0x7, 0),
  214. CLKGEN_FIELD(0x2C, 0x7, 0),
  215. CLKGEN_FIELD(0x3C, 0x7, 0) },
  216. .pll_ops = &st_quadfs_pll_c65_ops,
  217. .rtbl = fs432c65_rtbl,
  218. .rtbl_cnt = ARRAY_SIZE(fs432c65_rtbl),
  219. .get_rate = clk_fs432c65_get_rate,
  220. };
  221. static const struct clkgen_quadfs_data st_fs660c32_E_416 = {
  222. .npda = CLKGEN_FIELD(0x0, 0x1, 14),
  223. .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
  224. CLKGEN_FIELD(0x0, 0x1, 11),
  225. CLKGEN_FIELD(0x0, 0x1, 12),
  226. CLKGEN_FIELD(0x0, 0x1, 13) },
  227. .nsdiv_present = true,
  228. .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
  229. CLKGEN_FIELD(0x0, 0x1, 19),
  230. CLKGEN_FIELD(0x0, 0x1, 20),
  231. CLKGEN_FIELD(0x0, 0x1, 21) },
  232. .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
  233. CLKGEN_FIELD(0x14, 0x1f, 0),
  234. CLKGEN_FIELD(0x24, 0x1f, 0),
  235. CLKGEN_FIELD(0x34, 0x1f, 0) },
  236. .en = { CLKGEN_FIELD(0x10, 0x1, 0),
  237. CLKGEN_FIELD(0x20, 0x1, 0),
  238. CLKGEN_FIELD(0x30, 0x1, 0),
  239. CLKGEN_FIELD(0x40, 0x1, 0) },
  240. .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
  241. .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
  242. CLKGEN_FIELD(0x18, 0x7fff, 0),
  243. CLKGEN_FIELD(0x28, 0x7fff, 0),
  244. CLKGEN_FIELD(0x38, 0x7fff, 0) },
  245. .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
  246. CLKGEN_FIELD(0x1C, 0xf, 0),
  247. CLKGEN_FIELD(0x2C, 0xf, 0),
  248. CLKGEN_FIELD(0x3C, 0xf, 0) },
  249. .lockstatus_present = true,
  250. .lock_status = CLKGEN_FIELD(0xAC, 0x1, 0),
  251. .pll_ops = &st_quadfs_pll_c32_ops,
  252. .rtbl = fs660c32_rtbl,
  253. .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
  254. .get_rate = clk_fs660c32_dig_get_rate,
  255. };
  256. static const struct clkgen_quadfs_data st_fs660c32_F_416 = {
  257. .npda = CLKGEN_FIELD(0x0, 0x1, 14),
  258. .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
  259. CLKGEN_FIELD(0x0, 0x1, 11),
  260. CLKGEN_FIELD(0x0, 0x1, 12),
  261. CLKGEN_FIELD(0x0, 0x1, 13) },
  262. .nsdiv_present = true,
  263. .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
  264. CLKGEN_FIELD(0x0, 0x1, 19),
  265. CLKGEN_FIELD(0x0, 0x1, 20),
  266. CLKGEN_FIELD(0x0, 0x1, 21) },
  267. .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
  268. CLKGEN_FIELD(0x14, 0x1f, 0),
  269. CLKGEN_FIELD(0x24, 0x1f, 0),
  270. CLKGEN_FIELD(0x34, 0x1f, 0) },
  271. .en = { CLKGEN_FIELD(0x10, 0x1, 0),
  272. CLKGEN_FIELD(0x20, 0x1, 0),
  273. CLKGEN_FIELD(0x30, 0x1, 0),
  274. CLKGEN_FIELD(0x40, 0x1, 0) },
  275. .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
  276. .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
  277. CLKGEN_FIELD(0x18, 0x7fff, 0),
  278. CLKGEN_FIELD(0x28, 0x7fff, 0),
  279. CLKGEN_FIELD(0x38, 0x7fff, 0) },
  280. .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
  281. CLKGEN_FIELD(0x1C, 0xf, 0),
  282. CLKGEN_FIELD(0x2C, 0xf, 0),
  283. CLKGEN_FIELD(0x3C, 0xf, 0) },
  284. .lockstatus_present = true,
  285. .lock_status = CLKGEN_FIELD(0xEC, 0x1, 0),
  286. .pll_ops = &st_quadfs_pll_c32_ops,
  287. .rtbl = fs660c32_rtbl,
  288. .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
  289. .get_rate = clk_fs660c32_dig_get_rate,
  290. };
  291. static const struct clkgen_quadfs_data st_fs660c32_C = {
  292. .nrst_present = true,
  293. .nrst = { CLKGEN_FIELD(0x2f0, 0x1, 0),
  294. CLKGEN_FIELD(0x2f0, 0x1, 1),
  295. CLKGEN_FIELD(0x2f0, 0x1, 2),
  296. CLKGEN_FIELD(0x2f0, 0x1, 3) },
  297. .npda = CLKGEN_FIELD(0x2f0, 0x1, 12),
  298. .nsb = { CLKGEN_FIELD(0x2f0, 0x1, 8),
  299. CLKGEN_FIELD(0x2f0, 0x1, 9),
  300. CLKGEN_FIELD(0x2f0, 0x1, 10),
  301. CLKGEN_FIELD(0x2f0, 0x1, 11) },
  302. .nsdiv_present = true,
  303. .nsdiv = { CLKGEN_FIELD(0x304, 0x1, 24),
  304. CLKGEN_FIELD(0x308, 0x1, 24),
  305. CLKGEN_FIELD(0x30c, 0x1, 24),
  306. CLKGEN_FIELD(0x310, 0x1, 24) },
  307. .mdiv = { CLKGEN_FIELD(0x304, 0x1f, 15),
  308. CLKGEN_FIELD(0x308, 0x1f, 15),
  309. CLKGEN_FIELD(0x30c, 0x1f, 15),
  310. CLKGEN_FIELD(0x310, 0x1f, 15) },
  311. .en = { CLKGEN_FIELD(0x2fc, 0x1, 0),
  312. CLKGEN_FIELD(0x2fc, 0x1, 1),
  313. CLKGEN_FIELD(0x2fc, 0x1, 2),
  314. CLKGEN_FIELD(0x2fc, 0x1, 3) },
  315. .ndiv = CLKGEN_FIELD(0x2f4, 0x7, 16),
  316. .pe = { CLKGEN_FIELD(0x304, 0x7fff, 0),
  317. CLKGEN_FIELD(0x308, 0x7fff, 0),
  318. CLKGEN_FIELD(0x30c, 0x7fff, 0),
  319. CLKGEN_FIELD(0x310, 0x7fff, 0) },
  320. .sdiv = { CLKGEN_FIELD(0x304, 0xf, 20),
  321. CLKGEN_FIELD(0x308, 0xf, 20),
  322. CLKGEN_FIELD(0x30c, 0xf, 20),
  323. CLKGEN_FIELD(0x310, 0xf, 20) },
  324. .lockstatus_present = true,
  325. .lock_status = CLKGEN_FIELD(0x2f0, 0x1, 24),
  326. .powerup_polarity = 1,
  327. .standby_polarity = 1,
  328. .pll_ops = &st_quadfs_pll_c32_ops,
  329. .rtbl = fs660c32_rtbl,
  330. .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
  331. .get_rate = clk_fs660c32_dig_get_rate,
  332. };
  333. static const struct clkgen_quadfs_data st_fs660c32_D = {
  334. .nrst_present = true,
  335. .nrst = { CLKGEN_FIELD(0x2a0, 0x1, 0),
  336. CLKGEN_FIELD(0x2a0, 0x1, 1),
  337. CLKGEN_FIELD(0x2a0, 0x1, 2),
  338. CLKGEN_FIELD(0x2a0, 0x1, 3) },
  339. .ndiv = CLKGEN_FIELD(0x2a4, 0x7, 16),
  340. .pe = { CLKGEN_FIELD(0x2b4, 0x7fff, 0),
  341. CLKGEN_FIELD(0x2b8, 0x7fff, 0),
  342. CLKGEN_FIELD(0x2bc, 0x7fff, 0),
  343. CLKGEN_FIELD(0x2c0, 0x7fff, 0) },
  344. .sdiv = { CLKGEN_FIELD(0x2b4, 0xf, 20),
  345. CLKGEN_FIELD(0x2b8, 0xf, 20),
  346. CLKGEN_FIELD(0x2bc, 0xf, 20),
  347. CLKGEN_FIELD(0x2c0, 0xf, 20) },
  348. .npda = CLKGEN_FIELD(0x2a0, 0x1, 12),
  349. .nsb = { CLKGEN_FIELD(0x2a0, 0x1, 8),
  350. CLKGEN_FIELD(0x2a0, 0x1, 9),
  351. CLKGEN_FIELD(0x2a0, 0x1, 10),
  352. CLKGEN_FIELD(0x2a0, 0x1, 11) },
  353. .nsdiv_present = true,
  354. .nsdiv = { CLKGEN_FIELD(0x2b4, 0x1, 24),
  355. CLKGEN_FIELD(0x2b8, 0x1, 24),
  356. CLKGEN_FIELD(0x2bc, 0x1, 24),
  357. CLKGEN_FIELD(0x2c0, 0x1, 24) },
  358. .mdiv = { CLKGEN_FIELD(0x2b4, 0x1f, 15),
  359. CLKGEN_FIELD(0x2b8, 0x1f, 15),
  360. CLKGEN_FIELD(0x2bc, 0x1f, 15),
  361. CLKGEN_FIELD(0x2c0, 0x1f, 15) },
  362. .en = { CLKGEN_FIELD(0x2ac, 0x1, 0),
  363. CLKGEN_FIELD(0x2ac, 0x1, 1),
  364. CLKGEN_FIELD(0x2ac, 0x1, 2),
  365. CLKGEN_FIELD(0x2ac, 0x1, 3) },
  366. .lockstatus_present = true,
  367. .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24),
  368. .powerup_polarity = 1,
  369. .standby_polarity = 1,
  370. .pll_ops = &st_quadfs_pll_c32_ops,
  371. .rtbl = fs660c32_rtbl,
  372. .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
  373. .get_rate = clk_fs660c32_dig_get_rate,};
  374. /**
  375. * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
  376. *
  377. * Traits of this clock:
  378. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  379. * enable - clk_enable and clk_disable are functional & control the Fsyn
  380. * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
  381. * parent - fixed parent. No clk_set_parent support
  382. */
  383. /**
  384. * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
  385. * its parent clock, found inside a type of
  386. * ST quad channel frequency synthesizer block
  387. *
  388. * @hw: handle between common and hardware-specific interfaces.
  389. * @ndiv: regmap field for the ndiv control.
  390. * @regs_base: base address of the configuration registers.
  391. * @lock: spinlock.
  392. *
  393. */
  394. struct st_clk_quadfs_pll {
  395. struct clk_hw hw;
  396. void __iomem *regs_base;
  397. spinlock_t *lock;
  398. struct clkgen_quadfs_data *data;
  399. u32 ndiv;
  400. };
  401. #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
  402. static int quadfs_pll_enable(struct clk_hw *hw)
  403. {
  404. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  405. unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
  406. if (pll->lock)
  407. spin_lock_irqsave(pll->lock, flags);
  408. /*
  409. * Bring block out of reset if we have reset control.
  410. */
  411. if (pll->data->reset_present)
  412. CLKGEN_WRITE(pll, nreset, 1);
  413. /*
  414. * Use a fixed input clock noise bandwidth filter for the moment
  415. */
  416. if (pll->data->bwfilter_present)
  417. CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
  418. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  419. /*
  420. * Power up the PLL
  421. */
  422. CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity);
  423. if (pll->lock)
  424. spin_unlock_irqrestore(pll->lock, flags);
  425. if (pll->data->lockstatus_present)
  426. while (!CLKGEN_READ(pll, lock_status)) {
  427. if (time_after(jiffies, timeout))
  428. return -ETIMEDOUT;
  429. cpu_relax();
  430. }
  431. return 0;
  432. }
  433. static void quadfs_pll_disable(struct clk_hw *hw)
  434. {
  435. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  436. unsigned long flags = 0;
  437. if (pll->lock)
  438. spin_lock_irqsave(pll->lock, flags);
  439. /*
  440. * Powerdown the PLL and then put block into soft reset if we have
  441. * reset control.
  442. */
  443. CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity);
  444. if (pll->data->reset_present)
  445. CLKGEN_WRITE(pll, nreset, 0);
  446. if (pll->lock)
  447. spin_unlock_irqrestore(pll->lock, flags);
  448. }
  449. static int quadfs_pll_is_enabled(struct clk_hw *hw)
  450. {
  451. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  452. u32 npda = CLKGEN_READ(pll, npda);
  453. return pll->data->powerup_polarity ? !npda : !!npda;
  454. }
  455. static int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
  456. unsigned long *rate)
  457. {
  458. unsigned long nd = fs->ndiv + 16; /* ndiv value */
  459. *rate = input * nd;
  460. return 0;
  461. }
  462. static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
  463. unsigned long parent_rate)
  464. {
  465. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  466. unsigned long rate = 0;
  467. struct stm_fs params;
  468. params.ndiv = CLKGEN_READ(pll, ndiv);
  469. if (clk_fs660c32_vco_get_rate(parent_rate, &params, &rate))
  470. pr_err("%s:%s error calculating rate\n",
  471. clk_hw_get_name(hw), __func__);
  472. pll->ndiv = params.ndiv;
  473. return rate;
  474. }
  475. static int clk_fs660c32_vco_get_params(unsigned long input,
  476. unsigned long output, struct stm_fs *fs)
  477. {
  478. /* Formula
  479. VCO frequency = (fin x ndiv) / pdiv
  480. ndiv = VCOfreq * pdiv / fin
  481. */
  482. unsigned long pdiv = 1, n;
  483. /* Output clock range: 384Mhz to 660Mhz */
  484. if (output < 384000000 || output > 660000000)
  485. return -EINVAL;
  486. if (input > 40000000)
  487. /* This means that PDIV would be 2 instead of 1.
  488. Not supported today. */
  489. return -EINVAL;
  490. input /= 1000;
  491. output /= 1000;
  492. n = output * pdiv / input;
  493. if (n < 16)
  494. n = 16;
  495. fs->ndiv = n - 16; /* Converting formula value to reg value */
  496. return 0;
  497. }
  498. static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw, unsigned long rate
  499. , unsigned long *prate)
  500. {
  501. struct stm_fs params;
  502. if (!clk_fs660c32_vco_get_params(*prate, rate, &params))
  503. clk_fs660c32_vco_get_rate(*prate, &params, &rate);
  504. pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
  505. __func__, clk_hw_get_name(hw),
  506. rate, (unsigned int)params.sdiv,
  507. (unsigned int)params.mdiv,
  508. (unsigned int)params.pe, (unsigned int)params.nsdiv);
  509. return rate;
  510. }
  511. static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
  512. unsigned long parent_rate)
  513. {
  514. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  515. struct stm_fs params;
  516. long hwrate = 0;
  517. unsigned long flags = 0;
  518. if (!rate || !parent_rate)
  519. return -EINVAL;
  520. if (!clk_fs660c32_vco_get_params(parent_rate, rate, &params))
  521. clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
  522. pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
  523. __func__, clk_hw_get_name(hw),
  524. hwrate, (unsigned int)params.ndiv);
  525. if (!hwrate)
  526. return -EINVAL;
  527. pll->ndiv = params.ndiv;
  528. if (pll->lock)
  529. spin_lock_irqsave(pll->lock, flags);
  530. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  531. if (pll->lock)
  532. spin_unlock_irqrestore(pll->lock, flags);
  533. return 0;
  534. }
  535. static const struct clk_ops st_quadfs_pll_c65_ops = {
  536. .enable = quadfs_pll_enable,
  537. .disable = quadfs_pll_disable,
  538. .is_enabled = quadfs_pll_is_enabled,
  539. };
  540. static const struct clk_ops st_quadfs_pll_c32_ops = {
  541. .enable = quadfs_pll_enable,
  542. .disable = quadfs_pll_disable,
  543. .is_enabled = quadfs_pll_is_enabled,
  544. .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
  545. .round_rate = quadfs_pll_fs660c32_round_rate,
  546. .set_rate = quadfs_pll_fs660c32_set_rate,
  547. };
  548. static struct clk * __init st_clk_register_quadfs_pll(
  549. const char *name, const char *parent_name,
  550. struct clkgen_quadfs_data *quadfs, void __iomem *reg,
  551. spinlock_t *lock)
  552. {
  553. struct st_clk_quadfs_pll *pll;
  554. struct clk *clk;
  555. struct clk_init_data init;
  556. /*
  557. * Sanity check required pointers.
  558. */
  559. if (WARN_ON(!name || !parent_name))
  560. return ERR_PTR(-EINVAL);
  561. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  562. if (!pll)
  563. return ERR_PTR(-ENOMEM);
  564. init.name = name;
  565. init.ops = quadfs->pll_ops;
  566. init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE;
  567. init.parent_names = &parent_name;
  568. init.num_parents = 1;
  569. pll->data = quadfs;
  570. pll->regs_base = reg;
  571. pll->lock = lock;
  572. pll->hw.init = &init;
  573. clk = clk_register(NULL, &pll->hw);
  574. if (IS_ERR(clk))
  575. kfree(pll);
  576. return clk;
  577. }
  578. /**
  579. * DOC: A digital frequency synthesizer
  580. *
  581. * Traits of this clock:
  582. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  583. * enable - clk_enable and clk_disable are functional
  584. * rate - set rate is functional
  585. * parent - fixed parent. No clk_set_parent support
  586. */
  587. /**
  588. * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
  589. * frequency synthesizer (fsynth) block.
  590. *
  591. * @hw: handle between common and hardware-specific interfaces
  592. *
  593. * @nsb: regmap field in the output control register for the digital
  594. * standby of this fsynth channel. This control is active low so
  595. * the channel is in standby when the control bit is cleared.
  596. *
  597. * @nsdiv: regmap field in the output control register for
  598. * for the optional divide by 3 of this fsynth channel. This control
  599. * is active low so the divide by 3 is active when the control bit is
  600. * cleared and the divide is bypassed when the bit is set.
  601. */
  602. struct st_clk_quadfs_fsynth {
  603. struct clk_hw hw;
  604. void __iomem *regs_base;
  605. spinlock_t *lock;
  606. struct clkgen_quadfs_data *data;
  607. u32 chan;
  608. /*
  609. * Cached hardware values from set_rate so we can program the
  610. * hardware in enable. There are two reasons for this:
  611. *
  612. * 1. The registers may not be writable until the parent has been
  613. * enabled.
  614. *
  615. * 2. It restores the clock rate when a driver does an enable
  616. * on PM restore, after a suspend to RAM has lost the hardware
  617. * setup.
  618. */
  619. u32 md;
  620. u32 pe;
  621. u32 sdiv;
  622. u32 nsdiv;
  623. };
  624. #define to_quadfs_fsynth(_hw) \
  625. container_of(_hw, struct st_clk_quadfs_fsynth, hw)
  626. static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
  627. {
  628. /*
  629. * Pulse the program enable register lsb to make the hardware take
  630. * notice of the new md/pe values with a glitchless transition.
  631. */
  632. CLKGEN_WRITE(fs, en[fs->chan], 1);
  633. CLKGEN_WRITE(fs, en[fs->chan], 0);
  634. }
  635. static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
  636. {
  637. unsigned long flags = 0;
  638. /*
  639. * Ensure the md/pe parameters are ignored while we are
  640. * reprogramming them so we can get a glitchless change
  641. * when fine tuning the speed of a running clock.
  642. */
  643. CLKGEN_WRITE(fs, en[fs->chan], 0);
  644. CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
  645. CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
  646. CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
  647. if (fs->lock)
  648. spin_lock_irqsave(fs->lock, flags);
  649. if (fs->data->nsdiv_present)
  650. CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
  651. if (fs->lock)
  652. spin_unlock_irqrestore(fs->lock, flags);
  653. }
  654. static int quadfs_fsynth_enable(struct clk_hw *hw)
  655. {
  656. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  657. unsigned long flags = 0;
  658. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  659. quadfs_fsynth_program_rate(fs);
  660. if (fs->lock)
  661. spin_lock_irqsave(fs->lock, flags);
  662. CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
  663. if (fs->data->nrst_present)
  664. CLKGEN_WRITE(fs, nrst[fs->chan], 0);
  665. if (fs->lock)
  666. spin_unlock_irqrestore(fs->lock, flags);
  667. quadfs_fsynth_program_enable(fs);
  668. return 0;
  669. }
  670. static void quadfs_fsynth_disable(struct clk_hw *hw)
  671. {
  672. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  673. unsigned long flags = 0;
  674. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  675. if (fs->lock)
  676. spin_lock_irqsave(fs->lock, flags);
  677. CLKGEN_WRITE(fs, nsb[fs->chan], fs->data->standby_polarity);
  678. if (fs->lock)
  679. spin_unlock_irqrestore(fs->lock, flags);
  680. }
  681. static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
  682. {
  683. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  684. u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
  685. pr_debug("%s: %s enable bit = 0x%x\n",
  686. __func__, clk_hw_get_name(hw), nsb);
  687. return fs->data->standby_polarity ? !nsb : !!nsb;
  688. }
  689. #define P15 (uint64_t)(1 << 15)
  690. static int clk_fs216c65_get_rate(unsigned long input, const struct stm_fs *fs,
  691. unsigned long *rate)
  692. {
  693. uint64_t res;
  694. unsigned long ns;
  695. unsigned long nd = 8; /* ndiv stuck at 0 => val = 8 */
  696. unsigned long s;
  697. long m;
  698. m = fs->mdiv - 32;
  699. s = 1 << (fs->sdiv + 1);
  700. ns = (fs->nsdiv ? 1 : 3);
  701. res = (uint64_t)(s * ns * P15 * (uint64_t)(m + 33));
  702. res = res - (s * ns * fs->pe);
  703. *rate = div64_u64(P15 * nd * input * 32, res);
  704. return 0;
  705. }
  706. static int clk_fs432c65_get_rate(unsigned long input, const struct stm_fs *fs,
  707. unsigned long *rate)
  708. {
  709. uint64_t res;
  710. unsigned long nd = 16; /* ndiv value; stuck at 0 (30Mhz input) */
  711. long m;
  712. unsigned long sd;
  713. unsigned long ns;
  714. m = fs->mdiv - 32;
  715. sd = 1 << (fs->sdiv + 1);
  716. ns = (fs->nsdiv ? 1 : 3);
  717. res = (uint64_t)(sd * ns * P15 * (uint64_t)(m + 33));
  718. res = res - (sd * ns * fs->pe);
  719. *rate = div64_u64(P15 * nd * input * 32, res);
  720. return 0;
  721. }
  722. #define P20 (uint64_t)(1 << 20)
  723. static int clk_fs660c32_dig_get_rate(unsigned long input,
  724. const struct stm_fs *fs, unsigned long *rate)
  725. {
  726. unsigned long s = (1 << fs->sdiv);
  727. unsigned long ns;
  728. uint64_t res;
  729. /*
  730. * 'nsdiv' is a register value ('BIN') which is translated
  731. * to a decimal value according to following rules.
  732. *
  733. * nsdiv ns.dec
  734. * 0 3
  735. * 1 1
  736. */
  737. ns = (fs->nsdiv == 1) ? 1 : 3;
  738. res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
  739. *rate = (unsigned long)div64_u64(input * P20 * 32, res);
  740. return 0;
  741. }
  742. static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
  743. struct stm_fs *params)
  744. {
  745. /*
  746. * Get the initial hardware values for recalc_rate
  747. */
  748. params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
  749. params->pe = CLKGEN_READ(fs, pe[fs->chan]);
  750. params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
  751. if (fs->data->nsdiv_present)
  752. params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
  753. else
  754. params->nsdiv = 1;
  755. /*
  756. * If All are NULL then assume no clock rate is programmed.
  757. */
  758. if (!params->mdiv && !params->pe && !params->sdiv)
  759. return 1;
  760. fs->md = params->mdiv;
  761. fs->pe = params->pe;
  762. fs->sdiv = params->sdiv;
  763. fs->nsdiv = params->nsdiv;
  764. return 0;
  765. }
  766. static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
  767. unsigned long prate, struct stm_fs *params)
  768. {
  769. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  770. int (*clk_fs_get_rate)(unsigned long ,
  771. const struct stm_fs *, unsigned long *);
  772. struct stm_fs prev_params;
  773. unsigned long prev_rate, rate = 0;
  774. unsigned long diff_rate, prev_diff_rate = ~0;
  775. int index;
  776. clk_fs_get_rate = fs->data->get_rate;
  777. for (index = 0; index < fs->data->rtbl_cnt; index++) {
  778. prev_rate = rate;
  779. *params = fs->data->rtbl[index];
  780. prev_params = *params;
  781. clk_fs_get_rate(prate, &fs->data->rtbl[index], &rate);
  782. diff_rate = abs(drate - rate);
  783. if (diff_rate > prev_diff_rate) {
  784. rate = prev_rate;
  785. *params = prev_params;
  786. break;
  787. }
  788. prev_diff_rate = diff_rate;
  789. if (drate == rate)
  790. return rate;
  791. }
  792. if (index == fs->data->rtbl_cnt)
  793. *params = prev_params;
  794. return rate;
  795. }
  796. static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
  797. unsigned long parent_rate)
  798. {
  799. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  800. unsigned long rate = 0;
  801. struct stm_fs params;
  802. int (*clk_fs_get_rate)(unsigned long ,
  803. const struct stm_fs *, unsigned long *);
  804. clk_fs_get_rate = fs->data->get_rate;
  805. if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
  806. return 0;
  807. if (clk_fs_get_rate(parent_rate, &params, &rate)) {
  808. pr_err("%s:%s error calculating rate\n",
  809. clk_hw_get_name(hw), __func__);
  810. }
  811. pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
  812. return rate;
  813. }
  814. static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
  815. unsigned long *prate)
  816. {
  817. struct stm_fs params;
  818. rate = quadfs_find_best_rate(hw, rate, *prate, &params);
  819. pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
  820. __func__, clk_hw_get_name(hw),
  821. rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
  822. (unsigned int)params.pe, (unsigned int)params.nsdiv);
  823. return rate;
  824. }
  825. static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
  826. struct stm_fs *params)
  827. {
  828. fs->md = params->mdiv;
  829. fs->pe = params->pe;
  830. fs->sdiv = params->sdiv;
  831. fs->nsdiv = params->nsdiv;
  832. /*
  833. * In some integrations you can only change the fsynth programming when
  834. * the parent entity containing it is enabled.
  835. */
  836. quadfs_fsynth_program_rate(fs);
  837. quadfs_fsynth_program_enable(fs);
  838. }
  839. static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
  840. unsigned long parent_rate)
  841. {
  842. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  843. struct stm_fs params;
  844. long hwrate;
  845. int uninitialized_var(i);
  846. if (!rate || !parent_rate)
  847. return -EINVAL;
  848. memset(&params, 0, sizeof(struct stm_fs));
  849. hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
  850. if (!hwrate)
  851. return -EINVAL;
  852. quadfs_program_and_enable(fs, &params);
  853. return 0;
  854. }
  855. static const struct clk_ops st_quadfs_ops = {
  856. .enable = quadfs_fsynth_enable,
  857. .disable = quadfs_fsynth_disable,
  858. .is_enabled = quadfs_fsynth_is_enabled,
  859. .round_rate = quadfs_round_rate,
  860. .set_rate = quadfs_set_rate,
  861. .recalc_rate = quadfs_recalc_rate,
  862. };
  863. static struct clk * __init st_clk_register_quadfs_fsynth(
  864. const char *name, const char *parent_name,
  865. struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
  866. spinlock_t *lock)
  867. {
  868. struct st_clk_quadfs_fsynth *fs;
  869. struct clk *clk;
  870. struct clk_init_data init;
  871. /*
  872. * Sanity check required pointers, note that nsdiv3 is optional.
  873. */
  874. if (WARN_ON(!name || !parent_name))
  875. return ERR_PTR(-EINVAL);
  876. fs = kzalloc(sizeof(*fs), GFP_KERNEL);
  877. if (!fs)
  878. return ERR_PTR(-ENOMEM);
  879. init.name = name;
  880. init.ops = &st_quadfs_ops;
  881. init.flags = CLK_GET_RATE_NOCACHE | CLK_IS_BASIC;
  882. init.parent_names = &parent_name;
  883. init.num_parents = 1;
  884. fs->data = quadfs;
  885. fs->regs_base = reg;
  886. fs->chan = chan;
  887. fs->lock = lock;
  888. fs->hw.init = &init;
  889. clk = clk_register(NULL, &fs->hw);
  890. if (IS_ERR(clk))
  891. kfree(fs);
  892. return clk;
  893. }
  894. static const struct of_device_id quadfs_of_match[] = {
  895. {
  896. .compatible = "st,stih416-quadfs216",
  897. .data = &st_fs216c65_416
  898. },
  899. {
  900. .compatible = "st,stih416-quadfs432",
  901. .data = &st_fs432c65_416
  902. },
  903. {
  904. .compatible = "st,stih416-quadfs660-E",
  905. .data = &st_fs660c32_E_416
  906. },
  907. {
  908. .compatible = "st,stih416-quadfs660-F",
  909. .data = &st_fs660c32_F_416
  910. },
  911. {
  912. .compatible = "st,stih407-quadfs660-C",
  913. .data = &st_fs660c32_C
  914. },
  915. {
  916. .compatible = "st,stih407-quadfs660-D",
  917. .data = &st_fs660c32_D
  918. },
  919. {}
  920. };
  921. static void __init st_of_create_quadfs_fsynths(
  922. struct device_node *np, const char *pll_name,
  923. struct clkgen_quadfs_data *quadfs, void __iomem *reg,
  924. spinlock_t *lock)
  925. {
  926. struct clk_onecell_data *clk_data;
  927. int fschan;
  928. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  929. if (!clk_data)
  930. return;
  931. clk_data->clk_num = QUADFS_MAX_CHAN;
  932. clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *),
  933. GFP_KERNEL);
  934. if (!clk_data->clks) {
  935. kfree(clk_data);
  936. return;
  937. }
  938. for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
  939. struct clk *clk;
  940. const char *clk_name;
  941. if (of_property_read_string_index(np, "clock-output-names",
  942. fschan, &clk_name)) {
  943. break;
  944. }
  945. /*
  946. * If we read an empty clock name then the channel is unused
  947. */
  948. if (*clk_name == '\0')
  949. continue;
  950. clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
  951. quadfs, reg, fschan, lock);
  952. /*
  953. * If there was an error registering this clock output, clean
  954. * up and move on to the next one.
  955. */
  956. if (!IS_ERR(clk)) {
  957. clk_data->clks[fschan] = clk;
  958. pr_debug("%s: parent %s rate %u\n",
  959. __clk_get_name(clk),
  960. __clk_get_name(clk_get_parent(clk)),
  961. (unsigned int)clk_get_rate(clk));
  962. }
  963. }
  964. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  965. }
  966. static void __init st_of_quadfs_setup(struct device_node *np)
  967. {
  968. const struct of_device_id *match;
  969. struct clk *clk;
  970. const char *pll_name, *clk_parent_name;
  971. void __iomem *reg;
  972. spinlock_t *lock;
  973. match = of_match_node(quadfs_of_match, np);
  974. if (WARN_ON(!match))
  975. return;
  976. reg = of_iomap(np, 0);
  977. if (!reg)
  978. return;
  979. clk_parent_name = of_clk_get_parent_name(np, 0);
  980. if (!clk_parent_name)
  981. return;
  982. pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name);
  983. if (!pll_name)
  984. return;
  985. lock = kzalloc(sizeof(*lock), GFP_KERNEL);
  986. if (!lock)
  987. goto err_exit;
  988. spin_lock_init(lock);
  989. clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name,
  990. (struct clkgen_quadfs_data *) match->data, reg, lock);
  991. if (IS_ERR(clk))
  992. goto err_exit;
  993. else
  994. pr_debug("%s: parent %s rate %u\n",
  995. __clk_get_name(clk),
  996. __clk_get_name(clk_get_parent(clk)),
  997. (unsigned int)clk_get_rate(clk));
  998. st_of_create_quadfs_fsynths(np, pll_name,
  999. (struct clkgen_quadfs_data *)match->data,
  1000. reg, lock);
  1001. err_exit:
  1002. kfree(pll_name); /* No longer need local copy of the PLL name */
  1003. }
  1004. CLK_OF_DECLARE(quadfs, "st,quadfs", st_of_quadfs_setup);