clkgen-fsyn.c 34 KB

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