clkgen-mux.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * clkgen-mux.c: ST GEN-MUX Clock driver
  3. *
  4. * Copyright (C) 2014 STMicroelectronics (R&D) Limited
  5. *
  6. * Authors: Stephen Gallimore <stephen.gallimore@st.com>
  7. * Pankaj Dev <pankaj.dev@st.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/of_address.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. static DEFINE_SPINLOCK(clkgena_divmux_lock);
  20. static DEFINE_SPINLOCK(clkgenf_lock);
  21. static const char ** __init clkgen_mux_get_parents(struct device_node *np,
  22. int *num_parents)
  23. {
  24. const char **parents;
  25. int nparents;
  26. nparents = of_clk_get_parent_count(np);
  27. if (WARN_ON(nparents <= 0))
  28. return ERR_PTR(-EINVAL);
  29. parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
  30. if (!parents)
  31. return ERR_PTR(-ENOMEM);
  32. *num_parents = of_clk_parent_fill(np, parents, nparents);
  33. return parents;
  34. }
  35. /**
  36. * DOC: Clock mux with a programmable divider on each of its three inputs.
  37. * The mux has an input setting which effectively gates its output.
  38. *
  39. * Traits of this clock:
  40. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  41. * enable - clk_enable and clk_disable are functional & control gating
  42. * rate - set rate is supported
  43. * parent - set/get parent
  44. */
  45. #define NUM_INPUTS 3
  46. struct clkgena_divmux {
  47. struct clk_hw hw;
  48. /* Subclassed mux and divider structures */
  49. struct clk_mux mux;
  50. struct clk_divider div[NUM_INPUTS];
  51. /* Enable/running feedback register bits for each input */
  52. void __iomem *feedback_reg[NUM_INPUTS];
  53. int feedback_bit_idx;
  54. u8 muxsel;
  55. };
  56. #define to_clkgena_divmux(_hw) container_of(_hw, struct clkgena_divmux, hw)
  57. struct clkgena_divmux_data {
  58. int num_outputs;
  59. int mux_offset;
  60. int mux_offset2;
  61. int mux_start_bit;
  62. int div_offsets[NUM_INPUTS];
  63. int fb_offsets[NUM_INPUTS];
  64. int fb_start_bit_idx;
  65. };
  66. #define CKGAX_CLKOPSRC_SWITCH_OFF 0x3
  67. static int clkgena_divmux_is_running(struct clkgena_divmux *mux)
  68. {
  69. u32 regval = readl(mux->feedback_reg[mux->muxsel]);
  70. u32 running = regval & BIT(mux->feedback_bit_idx);
  71. return !!running;
  72. }
  73. static int clkgena_divmux_enable(struct clk_hw *hw)
  74. {
  75. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  76. struct clk_hw *mux_hw = &genamux->mux.hw;
  77. unsigned long timeout;
  78. int ret = 0;
  79. __clk_hw_set_clk(mux_hw, hw);
  80. ret = clk_mux_ops.set_parent(mux_hw, genamux->muxsel);
  81. if (ret)
  82. return ret;
  83. timeout = jiffies + msecs_to_jiffies(10);
  84. while (!clkgena_divmux_is_running(genamux)) {
  85. if (time_after(jiffies, timeout))
  86. return -ETIMEDOUT;
  87. cpu_relax();
  88. }
  89. return 0;
  90. }
  91. static void clkgena_divmux_disable(struct clk_hw *hw)
  92. {
  93. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  94. struct clk_hw *mux_hw = &genamux->mux.hw;
  95. __clk_hw_set_clk(mux_hw, hw);
  96. clk_mux_ops.set_parent(mux_hw, CKGAX_CLKOPSRC_SWITCH_OFF);
  97. }
  98. static int clkgena_divmux_is_enabled(struct clk_hw *hw)
  99. {
  100. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  101. struct clk_hw *mux_hw = &genamux->mux.hw;
  102. __clk_hw_set_clk(mux_hw, hw);
  103. return (s8)clk_mux_ops.get_parent(mux_hw) > 0;
  104. }
  105. static u8 clkgena_divmux_get_parent(struct clk_hw *hw)
  106. {
  107. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  108. struct clk_hw *mux_hw = &genamux->mux.hw;
  109. __clk_hw_set_clk(mux_hw, hw);
  110. genamux->muxsel = clk_mux_ops.get_parent(mux_hw);
  111. if ((s8)genamux->muxsel < 0) {
  112. pr_debug("%s: %s: Invalid parent, setting to default.\n",
  113. __func__, clk_hw_get_name(hw));
  114. genamux->muxsel = 0;
  115. }
  116. return genamux->muxsel;
  117. }
  118. static int clkgena_divmux_set_parent(struct clk_hw *hw, u8 index)
  119. {
  120. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  121. if (index >= CKGAX_CLKOPSRC_SWITCH_OFF)
  122. return -EINVAL;
  123. genamux->muxsel = index;
  124. /*
  125. * If the mux is already enabled, call enable directly to set the
  126. * new mux position and wait for it to start running again. Otherwise
  127. * do nothing.
  128. */
  129. if (clkgena_divmux_is_enabled(hw))
  130. clkgena_divmux_enable(hw);
  131. return 0;
  132. }
  133. static unsigned long clkgena_divmux_recalc_rate(struct clk_hw *hw,
  134. unsigned long parent_rate)
  135. {
  136. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  137. struct clk_hw *div_hw = &genamux->div[genamux->muxsel].hw;
  138. __clk_hw_set_clk(div_hw, hw);
  139. return clk_divider_ops.recalc_rate(div_hw, parent_rate);
  140. }
  141. static int clkgena_divmux_set_rate(struct clk_hw *hw, unsigned long rate,
  142. unsigned long parent_rate)
  143. {
  144. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  145. struct clk_hw *div_hw = &genamux->div[genamux->muxsel].hw;
  146. __clk_hw_set_clk(div_hw, hw);
  147. return clk_divider_ops.set_rate(div_hw, rate, parent_rate);
  148. }
  149. static long clkgena_divmux_round_rate(struct clk_hw *hw, unsigned long rate,
  150. unsigned long *prate)
  151. {
  152. struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
  153. struct clk_hw *div_hw = &genamux->div[genamux->muxsel].hw;
  154. __clk_hw_set_clk(div_hw, hw);
  155. return clk_divider_ops.round_rate(div_hw, rate, prate);
  156. }
  157. static const struct clk_ops clkgena_divmux_ops = {
  158. .enable = clkgena_divmux_enable,
  159. .disable = clkgena_divmux_disable,
  160. .is_enabled = clkgena_divmux_is_enabled,
  161. .get_parent = clkgena_divmux_get_parent,
  162. .set_parent = clkgena_divmux_set_parent,
  163. .round_rate = clkgena_divmux_round_rate,
  164. .recalc_rate = clkgena_divmux_recalc_rate,
  165. .set_rate = clkgena_divmux_set_rate,
  166. };
  167. /**
  168. * clk_register_genamux - register a genamux clock with the clock framework
  169. */
  170. static struct clk * __init clk_register_genamux(const char *name,
  171. const char **parent_names, u8 num_parents,
  172. void __iomem *reg,
  173. const struct clkgena_divmux_data *muxdata,
  174. u32 idx)
  175. {
  176. /*
  177. * Fixed constants across all ClockgenA variants
  178. */
  179. const int mux_width = 2;
  180. const int divider_width = 5;
  181. struct clkgena_divmux *genamux;
  182. struct clk *clk;
  183. struct clk_init_data init;
  184. int i;
  185. genamux = kzalloc(sizeof(*genamux), GFP_KERNEL);
  186. if (!genamux)
  187. return ERR_PTR(-ENOMEM);
  188. init.name = name;
  189. init.ops = &clkgena_divmux_ops;
  190. init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE;
  191. init.parent_names = parent_names;
  192. init.num_parents = num_parents;
  193. genamux->mux.lock = &clkgena_divmux_lock;
  194. genamux->mux.mask = BIT(mux_width) - 1;
  195. genamux->mux.shift = muxdata->mux_start_bit + (idx * mux_width);
  196. if (genamux->mux.shift > 31) {
  197. /*
  198. * We have spilled into the second mux register so
  199. * adjust the register address and the bit shift accordingly
  200. */
  201. genamux->mux.reg = reg + muxdata->mux_offset2;
  202. genamux->mux.shift -= 32;
  203. } else {
  204. genamux->mux.reg = reg + muxdata->mux_offset;
  205. }
  206. for (i = 0; i < NUM_INPUTS; i++) {
  207. /*
  208. * Divider config for each input
  209. */
  210. void __iomem *divbase = reg + muxdata->div_offsets[i];
  211. genamux->div[i].width = divider_width;
  212. genamux->div[i].reg = divbase + (idx * sizeof(u32));
  213. /*
  214. * Mux enabled/running feedback register for each input.
  215. */
  216. genamux->feedback_reg[i] = reg + muxdata->fb_offsets[i];
  217. }
  218. genamux->feedback_bit_idx = muxdata->fb_start_bit_idx + idx;
  219. genamux->hw.init = &init;
  220. clk = clk_register(NULL, &genamux->hw);
  221. if (IS_ERR(clk)) {
  222. kfree(genamux);
  223. goto err;
  224. }
  225. pr_debug("%s: parent %s rate %lu\n",
  226. __clk_get_name(clk),
  227. __clk_get_name(clk_get_parent(clk)),
  228. clk_get_rate(clk));
  229. err:
  230. return clk;
  231. }
  232. static struct clkgena_divmux_data st_divmux_c65hs = {
  233. .num_outputs = 4,
  234. .mux_offset = 0x14,
  235. .mux_start_bit = 0,
  236. .div_offsets = { 0x800, 0x900, 0xb00 },
  237. .fb_offsets = { 0x18, 0x1c, 0x20 },
  238. .fb_start_bit_idx = 0,
  239. };
  240. static struct clkgena_divmux_data st_divmux_c65ls = {
  241. .num_outputs = 14,
  242. .mux_offset = 0x14,
  243. .mux_offset2 = 0x24,
  244. .mux_start_bit = 8,
  245. .div_offsets = { 0x810, 0xa10, 0xb10 },
  246. .fb_offsets = { 0x18, 0x1c, 0x20 },
  247. .fb_start_bit_idx = 4,
  248. };
  249. static struct clkgena_divmux_data st_divmux_c32odf0 = {
  250. .num_outputs = 8,
  251. .mux_offset = 0x1c,
  252. .mux_start_bit = 0,
  253. .div_offsets = { 0x800, 0x900, 0xa60 },
  254. .fb_offsets = { 0x2c, 0x24, 0x28 },
  255. .fb_start_bit_idx = 0,
  256. };
  257. static struct clkgena_divmux_data st_divmux_c32odf1 = {
  258. .num_outputs = 8,
  259. .mux_offset = 0x1c,
  260. .mux_start_bit = 16,
  261. .div_offsets = { 0x820, 0x980, 0xa80 },
  262. .fb_offsets = { 0x2c, 0x24, 0x28 },
  263. .fb_start_bit_idx = 8,
  264. };
  265. static struct clkgena_divmux_data st_divmux_c32odf2 = {
  266. .num_outputs = 8,
  267. .mux_offset = 0x20,
  268. .mux_start_bit = 0,
  269. .div_offsets = { 0x840, 0xa20, 0xb10 },
  270. .fb_offsets = { 0x2c, 0x24, 0x28 },
  271. .fb_start_bit_idx = 16,
  272. };
  273. static struct clkgena_divmux_data st_divmux_c32odf3 = {
  274. .num_outputs = 8,
  275. .mux_offset = 0x20,
  276. .mux_start_bit = 16,
  277. .div_offsets = { 0x860, 0xa40, 0xb30 },
  278. .fb_offsets = { 0x2c, 0x24, 0x28 },
  279. .fb_start_bit_idx = 24,
  280. };
  281. static const struct of_device_id clkgena_divmux_of_match[] = {
  282. {
  283. .compatible = "st,clkgena-divmux-c65-hs",
  284. .data = &st_divmux_c65hs,
  285. },
  286. {
  287. .compatible = "st,clkgena-divmux-c65-ls",
  288. .data = &st_divmux_c65ls,
  289. },
  290. {
  291. .compatible = "st,clkgena-divmux-c32-odf0",
  292. .data = &st_divmux_c32odf0,
  293. },
  294. {
  295. .compatible = "st,clkgena-divmux-c32-odf1",
  296. .data = &st_divmux_c32odf1,
  297. },
  298. {
  299. .compatible = "st,clkgena-divmux-c32-odf2",
  300. .data = &st_divmux_c32odf2,
  301. },
  302. {
  303. .compatible = "st,clkgena-divmux-c32-odf3",
  304. .data = &st_divmux_c32odf3,
  305. },
  306. {}
  307. };
  308. static void __iomem * __init clkgen_get_register_base(struct device_node *np)
  309. {
  310. struct device_node *pnode;
  311. void __iomem *reg;
  312. pnode = of_get_parent(np);
  313. if (!pnode)
  314. return NULL;
  315. reg = of_iomap(pnode, 0);
  316. of_node_put(pnode);
  317. return reg;
  318. }
  319. static void __init st_of_clkgena_divmux_setup(struct device_node *np)
  320. {
  321. const struct of_device_id *match;
  322. const struct clkgena_divmux_data *data;
  323. struct clk_onecell_data *clk_data;
  324. void __iomem *reg;
  325. const char **parents;
  326. int num_parents = 0, i;
  327. match = of_match_node(clkgena_divmux_of_match, np);
  328. if (WARN_ON(!match))
  329. return;
  330. data = match->data;
  331. reg = clkgen_get_register_base(np);
  332. if (!reg)
  333. return;
  334. parents = clkgen_mux_get_parents(np, &num_parents);
  335. if (IS_ERR(parents))
  336. goto err_parents;
  337. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  338. if (!clk_data)
  339. goto err_alloc;
  340. clk_data->clk_num = data->num_outputs;
  341. clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *),
  342. GFP_KERNEL);
  343. if (!clk_data->clks)
  344. goto err_alloc_clks;
  345. for (i = 0; i < clk_data->clk_num; i++) {
  346. struct clk *clk;
  347. const char *clk_name;
  348. if (of_property_read_string_index(np, "clock-output-names",
  349. i, &clk_name))
  350. break;
  351. /*
  352. * If we read an empty clock name then the output is unused
  353. */
  354. if (*clk_name == '\0')
  355. continue;
  356. clk = clk_register_genamux(clk_name, parents, num_parents,
  357. reg, data, i);
  358. if (IS_ERR(clk))
  359. goto err;
  360. clk_data->clks[i] = clk;
  361. }
  362. kfree(parents);
  363. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  364. return;
  365. err:
  366. kfree(clk_data->clks);
  367. err_alloc_clks:
  368. kfree(clk_data);
  369. err_alloc:
  370. kfree(parents);
  371. err_parents:
  372. iounmap(reg);
  373. }
  374. CLK_OF_DECLARE(clkgenadivmux, "st,clkgena-divmux", st_of_clkgena_divmux_setup);
  375. struct clkgena_prediv_data {
  376. u32 offset;
  377. u8 shift;
  378. struct clk_div_table *table;
  379. };
  380. static struct clk_div_table prediv_table16[] = {
  381. { .val = 0, .div = 1 },
  382. { .val = 1, .div = 16 },
  383. { .div = 0 },
  384. };
  385. static struct clkgena_prediv_data prediv_c65_data = {
  386. .offset = 0x4c,
  387. .shift = 31,
  388. .table = prediv_table16,
  389. };
  390. static struct clkgena_prediv_data prediv_c32_data = {
  391. .offset = 0x50,
  392. .shift = 1,
  393. .table = prediv_table16,
  394. };
  395. static const struct of_device_id clkgena_prediv_of_match[] = {
  396. { .compatible = "st,clkgena-prediv-c65", .data = &prediv_c65_data },
  397. { .compatible = "st,clkgena-prediv-c32", .data = &prediv_c32_data },
  398. {}
  399. };
  400. static void __init st_of_clkgena_prediv_setup(struct device_node *np)
  401. {
  402. const struct of_device_id *match;
  403. void __iomem *reg;
  404. const char *parent_name, *clk_name;
  405. struct clk *clk;
  406. const struct clkgena_prediv_data *data;
  407. match = of_match_node(clkgena_prediv_of_match, np);
  408. if (!match) {
  409. pr_err("%s: No matching data\n", __func__);
  410. return;
  411. }
  412. data = match->data;
  413. reg = clkgen_get_register_base(np);
  414. if (!reg)
  415. return;
  416. parent_name = of_clk_get_parent_name(np, 0);
  417. if (!parent_name)
  418. goto err;
  419. if (of_property_read_string_index(np, "clock-output-names",
  420. 0, &clk_name))
  421. goto err;
  422. clk = clk_register_divider_table(NULL, clk_name, parent_name,
  423. CLK_GET_RATE_NOCACHE,
  424. reg + data->offset, data->shift, 1,
  425. 0, data->table, NULL);
  426. if (IS_ERR(clk))
  427. goto err;
  428. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  429. pr_debug("%s: parent %s rate %u\n",
  430. __clk_get_name(clk),
  431. __clk_get_name(clk_get_parent(clk)),
  432. (unsigned int)clk_get_rate(clk));
  433. return;
  434. err:
  435. iounmap(reg);
  436. }
  437. CLK_OF_DECLARE(clkgenaprediv, "st,clkgena-prediv", st_of_clkgena_prediv_setup);
  438. struct clkgen_mux_data {
  439. u32 offset;
  440. u8 shift;
  441. u8 width;
  442. spinlock_t *lock;
  443. unsigned long clk_flags;
  444. u8 mux_flags;
  445. };
  446. static struct clkgen_mux_data clkgen_mux_c_vcc_hd_416 = {
  447. .offset = 0,
  448. .shift = 0,
  449. .width = 1,
  450. };
  451. static struct clkgen_mux_data clkgen_mux_f_vcc_fvdp_416 = {
  452. .offset = 0,
  453. .shift = 0,
  454. .width = 1,
  455. };
  456. static struct clkgen_mux_data clkgen_mux_f_vcc_hva_416 = {
  457. .offset = 0,
  458. .shift = 0,
  459. .width = 1,
  460. };
  461. static struct clkgen_mux_data clkgen_mux_f_vcc_hd_416 = {
  462. .offset = 0,
  463. .shift = 16,
  464. .width = 1,
  465. .lock = &clkgenf_lock,
  466. };
  467. static struct clkgen_mux_data clkgen_mux_c_vcc_sd_416 = {
  468. .offset = 0,
  469. .shift = 17,
  470. .width = 1,
  471. .lock = &clkgenf_lock,
  472. };
  473. static struct clkgen_mux_data stih415_a9_mux_data = {
  474. .offset = 0,
  475. .shift = 1,
  476. .width = 2,
  477. };
  478. static struct clkgen_mux_data stih416_a9_mux_data = {
  479. .offset = 0,
  480. .shift = 0,
  481. .width = 2,
  482. };
  483. static struct clkgen_mux_data stih407_a9_mux_data = {
  484. .offset = 0x1a4,
  485. .shift = 0,
  486. .width = 2,
  487. };
  488. static const struct of_device_id mux_of_match[] = {
  489. {
  490. .compatible = "st,stih416-clkgenc-vcc-hd",
  491. .data = &clkgen_mux_c_vcc_hd_416,
  492. },
  493. {
  494. .compatible = "st,stih416-clkgenf-vcc-fvdp",
  495. .data = &clkgen_mux_f_vcc_fvdp_416,
  496. },
  497. {
  498. .compatible = "st,stih416-clkgenf-vcc-hva",
  499. .data = &clkgen_mux_f_vcc_hva_416,
  500. },
  501. {
  502. .compatible = "st,stih416-clkgenf-vcc-hd",
  503. .data = &clkgen_mux_f_vcc_hd_416,
  504. },
  505. {
  506. .compatible = "st,stih416-clkgenf-vcc-sd",
  507. .data = &clkgen_mux_c_vcc_sd_416,
  508. },
  509. {
  510. .compatible = "st,stih415-clkgen-a9-mux",
  511. .data = &stih415_a9_mux_data,
  512. },
  513. {
  514. .compatible = "st,stih416-clkgen-a9-mux",
  515. .data = &stih416_a9_mux_data,
  516. },
  517. {
  518. .compatible = "st,stih407-clkgen-a9-mux",
  519. .data = &stih407_a9_mux_data,
  520. },
  521. {}
  522. };
  523. static void __init st_of_clkgen_mux_setup(struct device_node *np)
  524. {
  525. const struct of_device_id *match;
  526. struct clk *clk;
  527. void __iomem *reg;
  528. const char **parents;
  529. int num_parents;
  530. const struct clkgen_mux_data *data;
  531. match = of_match_node(mux_of_match, np);
  532. if (!match) {
  533. pr_err("%s: No matching data\n", __func__);
  534. return;
  535. }
  536. data = match->data;
  537. reg = of_iomap(np, 0);
  538. if (!reg) {
  539. pr_err("%s: Failed to get base address\n", __func__);
  540. return;
  541. }
  542. parents = clkgen_mux_get_parents(np, &num_parents);
  543. if (IS_ERR(parents)) {
  544. pr_err("%s: Failed to get parents (%ld)\n",
  545. __func__, PTR_ERR(parents));
  546. goto err_parents;
  547. }
  548. clk = clk_register_mux(NULL, np->name, parents, num_parents,
  549. data->clk_flags | CLK_SET_RATE_PARENT,
  550. reg + data->offset,
  551. data->shift, data->width, data->mux_flags,
  552. data->lock);
  553. if (IS_ERR(clk))
  554. goto err;
  555. pr_debug("%s: parent %s rate %u\n",
  556. __clk_get_name(clk),
  557. __clk_get_name(clk_get_parent(clk)),
  558. (unsigned int)clk_get_rate(clk));
  559. kfree(parents);
  560. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  561. return;
  562. err:
  563. kfree(parents);
  564. err_parents:
  565. iounmap(reg);
  566. }
  567. CLK_OF_DECLARE(clkgen_mux, "st,clkgen-mux", st_of_clkgen_mux_setup);
  568. #define VCC_MAX_CHANNELS 16
  569. #define VCC_GATE_OFFSET 0x0
  570. #define VCC_MUX_OFFSET 0x4
  571. #define VCC_DIV_OFFSET 0x8
  572. struct clkgen_vcc_data {
  573. spinlock_t *lock;
  574. unsigned long clk_flags;
  575. };
  576. static struct clkgen_vcc_data st_clkgenc_vcc_416 = {
  577. .clk_flags = CLK_SET_RATE_PARENT,
  578. };
  579. static struct clkgen_vcc_data st_clkgenf_vcc_416 = {
  580. .lock = &clkgenf_lock,
  581. };
  582. static const struct of_device_id vcc_of_match[] = {
  583. { .compatible = "st,stih416-clkgenc", .data = &st_clkgenc_vcc_416 },
  584. { .compatible = "st,stih416-clkgenf", .data = &st_clkgenf_vcc_416 },
  585. {}
  586. };
  587. static void __init st_of_clkgen_vcc_setup(struct device_node *np)
  588. {
  589. const struct of_device_id *match;
  590. void __iomem *reg;
  591. const char **parents;
  592. int num_parents, i;
  593. struct clk_onecell_data *clk_data;
  594. const struct clkgen_vcc_data *data;
  595. match = of_match_node(vcc_of_match, np);
  596. if (WARN_ON(!match))
  597. return;
  598. data = match->data;
  599. reg = of_iomap(np, 0);
  600. if (!reg)
  601. return;
  602. parents = clkgen_mux_get_parents(np, &num_parents);
  603. if (IS_ERR(parents))
  604. goto err_parents;
  605. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  606. if (!clk_data)
  607. goto err_alloc;
  608. clk_data->clk_num = VCC_MAX_CHANNELS;
  609. clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *),
  610. GFP_KERNEL);
  611. if (!clk_data->clks)
  612. goto err_alloc_clks;
  613. for (i = 0; i < clk_data->clk_num; i++) {
  614. struct clk *clk;
  615. const char *clk_name;
  616. struct clk_gate *gate;
  617. struct clk_divider *div;
  618. struct clk_mux *mux;
  619. if (of_property_read_string_index(np, "clock-output-names",
  620. i, &clk_name))
  621. break;
  622. /*
  623. * If we read an empty clock name then the output is unused
  624. */
  625. if (*clk_name == '\0')
  626. continue;
  627. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  628. if (!gate)
  629. goto err;
  630. div = kzalloc(sizeof(*div), GFP_KERNEL);
  631. if (!div) {
  632. kfree(gate);
  633. goto err;
  634. }
  635. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  636. if (!mux) {
  637. kfree(gate);
  638. kfree(div);
  639. goto err;
  640. }
  641. gate->reg = reg + VCC_GATE_OFFSET;
  642. gate->bit_idx = i;
  643. gate->flags = CLK_GATE_SET_TO_DISABLE;
  644. gate->lock = data->lock;
  645. div->reg = reg + VCC_DIV_OFFSET;
  646. div->shift = 2 * i;
  647. div->width = 2;
  648. div->flags = CLK_DIVIDER_POWER_OF_TWO |
  649. CLK_DIVIDER_ROUND_CLOSEST;
  650. mux->reg = reg + VCC_MUX_OFFSET;
  651. mux->shift = 2 * i;
  652. mux->mask = 0x3;
  653. clk = clk_register_composite(NULL, clk_name, parents,
  654. num_parents,
  655. &mux->hw, &clk_mux_ops,
  656. &div->hw, &clk_divider_ops,
  657. &gate->hw, &clk_gate_ops,
  658. data->clk_flags |
  659. CLK_GET_RATE_NOCACHE);
  660. if (IS_ERR(clk)) {
  661. kfree(gate);
  662. kfree(div);
  663. kfree(mux);
  664. goto err;
  665. }
  666. pr_debug("%s: parent %s rate %u\n",
  667. __clk_get_name(clk),
  668. __clk_get_name(clk_get_parent(clk)),
  669. (unsigned int)clk_get_rate(clk));
  670. clk_data->clks[i] = clk;
  671. }
  672. kfree(parents);
  673. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  674. return;
  675. err:
  676. for (i = 0; i < clk_data->clk_num; i++) {
  677. struct clk_composite *composite;
  678. if (!clk_data->clks[i])
  679. continue;
  680. composite = container_of(__clk_get_hw(clk_data->clks[i]),
  681. struct clk_composite, hw);
  682. kfree(container_of(composite->gate_hw, struct clk_gate, hw));
  683. kfree(container_of(composite->rate_hw, struct clk_divider, hw));
  684. kfree(container_of(composite->mux_hw, struct clk_mux, hw));
  685. }
  686. kfree(clk_data->clks);
  687. err_alloc_clks:
  688. kfree(clk_data);
  689. err_alloc:
  690. kfree(parents);
  691. err_parents:
  692. iounmap(reg);
  693. }
  694. CLK_OF_DECLARE(clkgen_vcc, "st,clkgen-vcc", st_of_clkgen_vcc_setup);