da8xx-cfgchip.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Clock driver for DA8xx/AM17xx/AM18xx/OMAP-L13x CFGCHIP
  4. *
  5. * Copyright (C) 2018 David Lechner <david@lechnology.com>
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/clk.h>
  9. #include <linux/clkdev.h>
  10. #include <linux/init.h>
  11. #include <linux/mfd/da8xx-cfgchip.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_data/clk-da8xx-cfgchip.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. /* --- Gate clocks --- */
  20. #define DA8XX_GATE_CLOCK_IS_DIV4P5 BIT(1)
  21. struct da8xx_cfgchip_gate_clk_info {
  22. const char *name;
  23. u32 cfgchip;
  24. u32 bit;
  25. u32 flags;
  26. };
  27. struct da8xx_cfgchip_gate_clk {
  28. struct clk_hw hw;
  29. struct regmap *regmap;
  30. u32 reg;
  31. u32 mask;
  32. };
  33. #define to_da8xx_cfgchip_gate_clk(_hw) \
  34. container_of((_hw), struct da8xx_cfgchip_gate_clk, hw)
  35. static int da8xx_cfgchip_gate_clk_enable(struct clk_hw *hw)
  36. {
  37. struct da8xx_cfgchip_gate_clk *clk = to_da8xx_cfgchip_gate_clk(hw);
  38. return regmap_write_bits(clk->regmap, clk->reg, clk->mask, clk->mask);
  39. }
  40. static void da8xx_cfgchip_gate_clk_disable(struct clk_hw *hw)
  41. {
  42. struct da8xx_cfgchip_gate_clk *clk = to_da8xx_cfgchip_gate_clk(hw);
  43. regmap_write_bits(clk->regmap, clk->reg, clk->mask, 0);
  44. }
  45. static int da8xx_cfgchip_gate_clk_is_enabled(struct clk_hw *hw)
  46. {
  47. struct da8xx_cfgchip_gate_clk *clk = to_da8xx_cfgchip_gate_clk(hw);
  48. unsigned int val;
  49. regmap_read(clk->regmap, clk->reg, &val);
  50. return !!(val & clk->mask);
  51. }
  52. static unsigned long da8xx_cfgchip_div4p5_recalc_rate(struct clk_hw *hw,
  53. unsigned long parent_rate)
  54. {
  55. /* this clock divides by 4.5 */
  56. return parent_rate * 2 / 9;
  57. }
  58. static const struct clk_ops da8xx_cfgchip_gate_clk_ops = {
  59. .enable = da8xx_cfgchip_gate_clk_enable,
  60. .disable = da8xx_cfgchip_gate_clk_disable,
  61. .is_enabled = da8xx_cfgchip_gate_clk_is_enabled,
  62. };
  63. static const struct clk_ops da8xx_cfgchip_div4p5_clk_ops = {
  64. .enable = da8xx_cfgchip_gate_clk_enable,
  65. .disable = da8xx_cfgchip_gate_clk_disable,
  66. .is_enabled = da8xx_cfgchip_gate_clk_is_enabled,
  67. .recalc_rate = da8xx_cfgchip_div4p5_recalc_rate,
  68. };
  69. static struct da8xx_cfgchip_gate_clk * __init
  70. da8xx_cfgchip_gate_clk_register(struct device *dev,
  71. const struct da8xx_cfgchip_gate_clk_info *info,
  72. struct regmap *regmap)
  73. {
  74. struct clk *parent;
  75. const char *parent_name;
  76. struct da8xx_cfgchip_gate_clk *gate;
  77. struct clk_init_data init;
  78. int ret;
  79. parent = devm_clk_get(dev, NULL);
  80. if (IS_ERR(parent))
  81. return ERR_CAST(parent);
  82. parent_name = __clk_get_name(parent);
  83. gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
  84. if (!gate)
  85. return ERR_PTR(-ENOMEM);
  86. init.name = info->name;
  87. if (info->flags & DA8XX_GATE_CLOCK_IS_DIV4P5)
  88. init.ops = &da8xx_cfgchip_div4p5_clk_ops;
  89. else
  90. init.ops = &da8xx_cfgchip_gate_clk_ops;
  91. init.parent_names = &parent_name;
  92. init.num_parents = 1;
  93. init.flags = 0;
  94. gate->hw.init = &init;
  95. gate->regmap = regmap;
  96. gate->reg = info->cfgchip;
  97. gate->mask = info->bit;
  98. ret = devm_clk_hw_register(dev, &gate->hw);
  99. if (ret < 0)
  100. return ERR_PTR(ret);
  101. return gate;
  102. }
  103. static const struct da8xx_cfgchip_gate_clk_info da8xx_tbclksync_info __initconst = {
  104. .name = "ehrpwm_tbclk",
  105. .cfgchip = CFGCHIP(1),
  106. .bit = CFGCHIP1_TBCLKSYNC,
  107. };
  108. static int __init da8xx_cfgchip_register_tbclk(struct device *dev,
  109. struct regmap *regmap)
  110. {
  111. struct da8xx_cfgchip_gate_clk *gate;
  112. gate = da8xx_cfgchip_gate_clk_register(dev, &da8xx_tbclksync_info,
  113. regmap);
  114. if (IS_ERR(gate))
  115. return PTR_ERR(gate);
  116. clk_hw_register_clkdev(&gate->hw, "tbclk", "ehrpwm.0");
  117. clk_hw_register_clkdev(&gate->hw, "tbclk", "ehrpwm.1");
  118. return 0;
  119. }
  120. static const struct da8xx_cfgchip_gate_clk_info da8xx_div4p5ena_info __initconst = {
  121. .name = "div4.5",
  122. .cfgchip = CFGCHIP(3),
  123. .bit = CFGCHIP3_DIV45PENA,
  124. .flags = DA8XX_GATE_CLOCK_IS_DIV4P5,
  125. };
  126. static int __init da8xx_cfgchip_register_div4p5(struct device *dev,
  127. struct regmap *regmap)
  128. {
  129. struct da8xx_cfgchip_gate_clk *gate;
  130. gate = da8xx_cfgchip_gate_clk_register(dev, &da8xx_div4p5ena_info, regmap);
  131. if (IS_ERR(gate))
  132. return PTR_ERR(gate);
  133. return 0;
  134. }
  135. static int __init
  136. of_da8xx_cfgchip_gate_clk_init(struct device *dev,
  137. const struct da8xx_cfgchip_gate_clk_info *info,
  138. struct regmap *regmap)
  139. {
  140. struct da8xx_cfgchip_gate_clk *gate;
  141. gate = da8xx_cfgchip_gate_clk_register(dev, info, regmap);
  142. if (IS_ERR(gate))
  143. return PTR_ERR(gate);
  144. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, gate);
  145. }
  146. static int __init of_da8xx_tbclksync_init(struct device *dev,
  147. struct regmap *regmap)
  148. {
  149. return of_da8xx_cfgchip_gate_clk_init(dev, &da8xx_tbclksync_info, regmap);
  150. }
  151. static int __init of_da8xx_div4p5ena_init(struct device *dev,
  152. struct regmap *regmap)
  153. {
  154. return of_da8xx_cfgchip_gate_clk_init(dev, &da8xx_div4p5ena_info, regmap);
  155. }
  156. /* --- MUX clocks --- */
  157. struct da8xx_cfgchip_mux_clk_info {
  158. const char *name;
  159. const char *parent0;
  160. const char *parent1;
  161. u32 cfgchip;
  162. u32 bit;
  163. };
  164. struct da8xx_cfgchip_mux_clk {
  165. struct clk_hw hw;
  166. struct regmap *regmap;
  167. u32 reg;
  168. u32 mask;
  169. };
  170. #define to_da8xx_cfgchip_mux_clk(_hw) \
  171. container_of((_hw), struct da8xx_cfgchip_mux_clk, hw)
  172. static int da8xx_cfgchip_mux_clk_set_parent(struct clk_hw *hw, u8 index)
  173. {
  174. struct da8xx_cfgchip_mux_clk *clk = to_da8xx_cfgchip_mux_clk(hw);
  175. unsigned int val = index ? clk->mask : 0;
  176. return regmap_write_bits(clk->regmap, clk->reg, clk->mask, val);
  177. }
  178. static u8 da8xx_cfgchip_mux_clk_get_parent(struct clk_hw *hw)
  179. {
  180. struct da8xx_cfgchip_mux_clk *clk = to_da8xx_cfgchip_mux_clk(hw);
  181. unsigned int val;
  182. regmap_read(clk->regmap, clk->reg, &val);
  183. return (val & clk->mask) ? 1 : 0;
  184. }
  185. static const struct clk_ops da8xx_cfgchip_mux_clk_ops = {
  186. .set_parent = da8xx_cfgchip_mux_clk_set_parent,
  187. .get_parent = da8xx_cfgchip_mux_clk_get_parent,
  188. };
  189. static struct da8xx_cfgchip_mux_clk * __init
  190. da8xx_cfgchip_mux_clk_register(struct device *dev,
  191. const struct da8xx_cfgchip_mux_clk_info *info,
  192. struct regmap *regmap)
  193. {
  194. const char * const parent_names[] = { info->parent0, info->parent1 };
  195. struct da8xx_cfgchip_mux_clk *mux;
  196. struct clk_init_data init;
  197. int ret;
  198. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  199. if (!mux)
  200. return ERR_PTR(-ENOMEM);
  201. init.name = info->name;
  202. init.ops = &da8xx_cfgchip_mux_clk_ops;
  203. init.parent_names = parent_names;
  204. init.num_parents = 2;
  205. init.flags = 0;
  206. mux->hw.init = &init;
  207. mux->regmap = regmap;
  208. mux->reg = info->cfgchip;
  209. mux->mask = info->bit;
  210. ret = devm_clk_hw_register(dev, &mux->hw);
  211. if (ret < 0)
  212. return ERR_PTR(ret);
  213. return mux;
  214. }
  215. static const struct da8xx_cfgchip_mux_clk_info da850_async1_info __initconst = {
  216. .name = "async1",
  217. .parent0 = "pll0_sysclk3",
  218. .parent1 = "div4.5",
  219. .cfgchip = CFGCHIP(3),
  220. .bit = CFGCHIP3_EMA_CLKSRC,
  221. };
  222. static int __init da8xx_cfgchip_register_async1(struct device *dev,
  223. struct regmap *regmap)
  224. {
  225. struct da8xx_cfgchip_mux_clk *mux;
  226. mux = da8xx_cfgchip_mux_clk_register(dev, &da850_async1_info, regmap);
  227. if (IS_ERR(mux))
  228. return PTR_ERR(mux);
  229. clk_hw_register_clkdev(&mux->hw, "async1", "da850-psc0");
  230. return 0;
  231. }
  232. static const struct da8xx_cfgchip_mux_clk_info da850_async3_info __initconst = {
  233. .name = "async3",
  234. .parent0 = "pll0_sysclk2",
  235. .parent1 = "pll1_sysclk2",
  236. .cfgchip = CFGCHIP(3),
  237. .bit = CFGCHIP3_ASYNC3_CLKSRC,
  238. };
  239. static int __init da850_cfgchip_register_async3(struct device *dev,
  240. struct regmap *regmap)
  241. {
  242. struct da8xx_cfgchip_mux_clk *mux;
  243. struct clk_hw *parent;
  244. mux = da8xx_cfgchip_mux_clk_register(dev, &da850_async3_info, regmap);
  245. if (IS_ERR(mux))
  246. return PTR_ERR(mux);
  247. clk_hw_register_clkdev(&mux->hw, "async3", "da850-psc1");
  248. /* pll1_sysclk2 is not affected by CPU scaling, so use it for async3 */
  249. parent = clk_hw_get_parent_by_index(&mux->hw, 1);
  250. if (parent)
  251. clk_set_parent(mux->hw.clk, parent->clk);
  252. else
  253. dev_warn(dev, "Failed to find async3 parent clock\n");
  254. return 0;
  255. }
  256. static int __init
  257. of_da8xx_cfgchip_init_mux_clock(struct device *dev,
  258. const struct da8xx_cfgchip_mux_clk_info *info,
  259. struct regmap *regmap)
  260. {
  261. struct da8xx_cfgchip_mux_clk *mux;
  262. mux = da8xx_cfgchip_mux_clk_register(dev, info, regmap);
  263. if (IS_ERR(mux))
  264. return PTR_ERR(mux);
  265. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, &mux->hw);
  266. }
  267. static int __init of_da850_async1_init(struct device *dev, struct regmap *regmap)
  268. {
  269. return of_da8xx_cfgchip_init_mux_clock(dev, &da850_async1_info, regmap);
  270. }
  271. static int __init of_da850_async3_init(struct device *dev, struct regmap *regmap)
  272. {
  273. return of_da8xx_cfgchip_init_mux_clock(dev, &da850_async3_info, regmap);
  274. }
  275. /* --- USB 2.0 PHY clock --- */
  276. struct da8xx_usb0_clk48 {
  277. struct clk_hw hw;
  278. struct clk *fck;
  279. struct regmap *regmap;
  280. };
  281. #define to_da8xx_usb0_clk48(_hw) \
  282. container_of((_hw), struct da8xx_usb0_clk48, hw)
  283. static int da8xx_usb0_clk48_prepare(struct clk_hw *hw)
  284. {
  285. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  286. /* The USB 2.0 PSC clock is only needed temporarily during the USB 2.0
  287. * PHY clock enable, but since clk_prepare() can't be called in an
  288. * atomic context (i.e. in clk_enable()), we have to prepare it here.
  289. */
  290. return clk_prepare(usb0->fck);
  291. }
  292. static void da8xx_usb0_clk48_unprepare(struct clk_hw *hw)
  293. {
  294. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  295. clk_unprepare(usb0->fck);
  296. }
  297. static int da8xx_usb0_clk48_enable(struct clk_hw *hw)
  298. {
  299. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  300. unsigned int mask, val;
  301. int ret;
  302. /* Locking the USB 2.O PLL requires that the USB 2.O PSC is enabled
  303. * temporaily. It can be turned back off once the PLL is locked.
  304. */
  305. clk_enable(usb0->fck);
  306. /* Turn on the USB 2.0 PHY, but just the PLL, and not OTG. The USB 1.1
  307. * PHY may use the USB 2.0 PLL clock without USB 2.0 OTG being used.
  308. */
  309. mask = CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN | CFGCHIP2_PHY_PLLON;
  310. val = CFGCHIP2_PHY_PLLON;
  311. regmap_write_bits(usb0->regmap, CFGCHIP(2), mask, val);
  312. ret = regmap_read_poll_timeout(usb0->regmap, CFGCHIP(2), val,
  313. val & CFGCHIP2_PHYCLKGD, 0, 500000);
  314. clk_disable(usb0->fck);
  315. return ret;
  316. }
  317. static void da8xx_usb0_clk48_disable(struct clk_hw *hw)
  318. {
  319. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  320. unsigned int val;
  321. val = CFGCHIP2_PHYPWRDN;
  322. regmap_write_bits(usb0->regmap, CFGCHIP(2), val, val);
  323. }
  324. static int da8xx_usb0_clk48_is_enabled(struct clk_hw *hw)
  325. {
  326. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  327. unsigned int val;
  328. regmap_read(usb0->regmap, CFGCHIP(2), &val);
  329. return !!(val & CFGCHIP2_PHYCLKGD);
  330. }
  331. static unsigned long da8xx_usb0_clk48_recalc_rate(struct clk_hw *hw,
  332. unsigned long parent_rate)
  333. {
  334. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  335. unsigned int mask, val;
  336. /* The parent clock rate must be one of the following */
  337. mask = CFGCHIP2_REFFREQ_MASK;
  338. switch (parent_rate) {
  339. case 12000000:
  340. val = CFGCHIP2_REFFREQ_12MHZ;
  341. break;
  342. case 13000000:
  343. val = CFGCHIP2_REFFREQ_13MHZ;
  344. break;
  345. case 19200000:
  346. val = CFGCHIP2_REFFREQ_19_2MHZ;
  347. break;
  348. case 20000000:
  349. val = CFGCHIP2_REFFREQ_20MHZ;
  350. break;
  351. case 24000000:
  352. val = CFGCHIP2_REFFREQ_24MHZ;
  353. break;
  354. case 26000000:
  355. val = CFGCHIP2_REFFREQ_26MHZ;
  356. break;
  357. case 38400000:
  358. val = CFGCHIP2_REFFREQ_38_4MHZ;
  359. break;
  360. case 40000000:
  361. val = CFGCHIP2_REFFREQ_40MHZ;
  362. break;
  363. case 48000000:
  364. val = CFGCHIP2_REFFREQ_48MHZ;
  365. break;
  366. default:
  367. return 0;
  368. }
  369. regmap_write_bits(usb0->regmap, CFGCHIP(2), mask, val);
  370. /* USB 2.0 PLL always supplies 48MHz */
  371. return 48000000;
  372. }
  373. static long da8xx_usb0_clk48_round_rate(struct clk_hw *hw, unsigned long rate,
  374. unsigned long *parent_rate)
  375. {
  376. return 48000000;
  377. }
  378. static int da8xx_usb0_clk48_set_parent(struct clk_hw *hw, u8 index)
  379. {
  380. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  381. return regmap_write_bits(usb0->regmap, CFGCHIP(2),
  382. CFGCHIP2_USB2PHYCLKMUX,
  383. index ? CFGCHIP2_USB2PHYCLKMUX : 0);
  384. }
  385. static u8 da8xx_usb0_clk48_get_parent(struct clk_hw *hw)
  386. {
  387. struct da8xx_usb0_clk48 *usb0 = to_da8xx_usb0_clk48(hw);
  388. unsigned int val;
  389. regmap_read(usb0->regmap, CFGCHIP(2), &val);
  390. return (val & CFGCHIP2_USB2PHYCLKMUX) ? 1 : 0;
  391. }
  392. static const struct clk_ops da8xx_usb0_clk48_ops = {
  393. .prepare = da8xx_usb0_clk48_prepare,
  394. .unprepare = da8xx_usb0_clk48_unprepare,
  395. .enable = da8xx_usb0_clk48_enable,
  396. .disable = da8xx_usb0_clk48_disable,
  397. .is_enabled = da8xx_usb0_clk48_is_enabled,
  398. .recalc_rate = da8xx_usb0_clk48_recalc_rate,
  399. .round_rate = da8xx_usb0_clk48_round_rate,
  400. .set_parent = da8xx_usb0_clk48_set_parent,
  401. .get_parent = da8xx_usb0_clk48_get_parent,
  402. };
  403. static struct da8xx_usb0_clk48 *
  404. da8xx_cfgchip_register_usb0_clk48(struct device *dev,
  405. struct regmap *regmap)
  406. {
  407. const char * const parent_names[] = { "usb_refclkin", "pll0_auxclk" };
  408. struct clk *fck_clk;
  409. struct da8xx_usb0_clk48 *usb0;
  410. struct clk_init_data init;
  411. int ret;
  412. fck_clk = devm_clk_get(dev, "fck");
  413. if (IS_ERR(fck_clk)) {
  414. if (PTR_ERR(fck_clk) != -EPROBE_DEFER)
  415. dev_err(dev, "Missing fck clock\n");
  416. return ERR_CAST(fck_clk);
  417. }
  418. usb0 = devm_kzalloc(dev, sizeof(*usb0), GFP_KERNEL);
  419. if (!usb0)
  420. return ERR_PTR(-ENOMEM);
  421. init.name = "usb0_clk48";
  422. init.ops = &da8xx_usb0_clk48_ops;
  423. init.parent_names = parent_names;
  424. init.num_parents = 2;
  425. usb0->hw.init = &init;
  426. usb0->fck = fck_clk;
  427. usb0->regmap = regmap;
  428. ret = devm_clk_hw_register(dev, &usb0->hw);
  429. if (ret < 0)
  430. return ERR_PTR(ret);
  431. return usb0;
  432. }
  433. /* --- USB 1.1 PHY clock --- */
  434. struct da8xx_usb1_clk48 {
  435. struct clk_hw hw;
  436. struct regmap *regmap;
  437. };
  438. #define to_da8xx_usb1_clk48(_hw) \
  439. container_of((_hw), struct da8xx_usb1_clk48, hw)
  440. static int da8xx_usb1_clk48_set_parent(struct clk_hw *hw, u8 index)
  441. {
  442. struct da8xx_usb1_clk48 *usb1 = to_da8xx_usb1_clk48(hw);
  443. return regmap_write_bits(usb1->regmap, CFGCHIP(2),
  444. CFGCHIP2_USB1PHYCLKMUX,
  445. index ? CFGCHIP2_USB1PHYCLKMUX : 0);
  446. }
  447. static u8 da8xx_usb1_clk48_get_parent(struct clk_hw *hw)
  448. {
  449. struct da8xx_usb1_clk48 *usb1 = to_da8xx_usb1_clk48(hw);
  450. unsigned int val;
  451. regmap_read(usb1->regmap, CFGCHIP(2), &val);
  452. return (val & CFGCHIP2_USB1PHYCLKMUX) ? 1 : 0;
  453. }
  454. static const struct clk_ops da8xx_usb1_clk48_ops = {
  455. .set_parent = da8xx_usb1_clk48_set_parent,
  456. .get_parent = da8xx_usb1_clk48_get_parent,
  457. };
  458. /**
  459. * da8xx_cfgchip_register_usb1_clk48 - Register a new USB 1.1 PHY clock
  460. * @regmap: The CFGCHIP regmap
  461. */
  462. static struct da8xx_usb1_clk48 *
  463. da8xx_cfgchip_register_usb1_clk48(struct device *dev,
  464. struct regmap *regmap)
  465. {
  466. const char * const parent_names[] = { "usb0_clk48", "usb_refclkin" };
  467. struct da8xx_usb1_clk48 *usb1;
  468. struct clk_init_data init;
  469. int ret;
  470. usb1 = devm_kzalloc(dev, sizeof(*usb1), GFP_KERNEL);
  471. if (!usb1)
  472. return ERR_PTR(-ENOMEM);
  473. init.name = "usb1_clk48";
  474. init.ops = &da8xx_usb1_clk48_ops;
  475. init.parent_names = parent_names;
  476. init.num_parents = 2;
  477. usb1->hw.init = &init;
  478. usb1->regmap = regmap;
  479. ret = devm_clk_hw_register(dev, &usb1->hw);
  480. if (ret < 0)
  481. return ERR_PTR(ret);
  482. return usb1;
  483. }
  484. static int da8xx_cfgchip_register_usb_phy_clk(struct device *dev,
  485. struct regmap *regmap)
  486. {
  487. struct da8xx_usb0_clk48 *usb0;
  488. struct da8xx_usb1_clk48 *usb1;
  489. struct clk_hw *parent;
  490. usb0 = da8xx_cfgchip_register_usb0_clk48(dev, regmap);
  491. if (IS_ERR(usb0))
  492. return PTR_ERR(usb0);
  493. /*
  494. * All existing boards use pll0_auxclk as the parent and new boards
  495. * should use device tree, so hard-coding the value (1) here.
  496. */
  497. parent = clk_hw_get_parent_by_index(&usb0->hw, 1);
  498. if (parent)
  499. clk_set_parent(usb0->hw.clk, parent->clk);
  500. else
  501. dev_warn(dev, "Failed to find usb0 parent clock\n");
  502. usb1 = da8xx_cfgchip_register_usb1_clk48(dev, regmap);
  503. if (IS_ERR(usb1))
  504. return PTR_ERR(usb1);
  505. /*
  506. * All existing boards use usb0_clk48 as the parent and new boards
  507. * should use device tree, so hard-coding the value (0) here.
  508. */
  509. parent = clk_hw_get_parent_by_index(&usb1->hw, 0);
  510. if (parent)
  511. clk_set_parent(usb1->hw.clk, parent->clk);
  512. else
  513. dev_warn(dev, "Failed to find usb1 parent clock\n");
  514. clk_hw_register_clkdev(&usb0->hw, "usb0_clk48", "da8xx-usb-phy");
  515. clk_hw_register_clkdev(&usb1->hw, "usb1_clk48", "da8xx-usb-phy");
  516. return 0;
  517. }
  518. static int of_da8xx_usb_phy_clk_init(struct device *dev, struct regmap *regmap)
  519. {
  520. struct clk_hw_onecell_data *clk_data;
  521. struct da8xx_usb0_clk48 *usb0;
  522. struct da8xx_usb1_clk48 *usb1;
  523. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, 2),
  524. GFP_KERNEL);
  525. if (!clk_data)
  526. return -ENOMEM;
  527. clk_data->num = 2;
  528. usb0 = da8xx_cfgchip_register_usb0_clk48(dev, regmap);
  529. if (IS_ERR(usb0)) {
  530. if (PTR_ERR(usb0) == -EPROBE_DEFER)
  531. return -EPROBE_DEFER;
  532. dev_warn(dev, "Failed to register usb0_clk48 (%ld)\n",
  533. PTR_ERR(usb0));
  534. clk_data->hws[0] = ERR_PTR(-ENOENT);
  535. } else {
  536. clk_data->hws[0] = &usb0->hw;
  537. }
  538. usb1 = da8xx_cfgchip_register_usb1_clk48(dev, regmap);
  539. if (IS_ERR(usb1)) {
  540. if (PTR_ERR(usb1) == -EPROBE_DEFER)
  541. return -EPROBE_DEFER;
  542. dev_warn(dev, "Failed to register usb1_clk48 (%ld)\n",
  543. PTR_ERR(usb1));
  544. clk_data->hws[1] = ERR_PTR(-ENOENT);
  545. } else {
  546. clk_data->hws[1] = &usb1->hw;
  547. }
  548. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
  549. }
  550. /* --- platform device --- */
  551. static const struct of_device_id da8xx_cfgchip_of_match[] = {
  552. {
  553. .compatible = "ti,da830-tbclksync",
  554. .data = of_da8xx_tbclksync_init,
  555. },
  556. {
  557. .compatible = "ti,da830-div4p5ena",
  558. .data = of_da8xx_div4p5ena_init,
  559. },
  560. {
  561. .compatible = "ti,da850-async1-clksrc",
  562. .data = of_da850_async1_init,
  563. },
  564. {
  565. .compatible = "ti,da850-async3-clksrc",
  566. .data = of_da850_async3_init,
  567. },
  568. {
  569. .compatible = "ti,da830-usb-phy-clocks",
  570. .data = of_da8xx_usb_phy_clk_init,
  571. },
  572. { }
  573. };
  574. static const struct platform_device_id da8xx_cfgchip_id_table[] = {
  575. {
  576. .name = "da830-tbclksync",
  577. .driver_data = (kernel_ulong_t)da8xx_cfgchip_register_tbclk,
  578. },
  579. {
  580. .name = "da830-div4p5ena",
  581. .driver_data = (kernel_ulong_t)da8xx_cfgchip_register_div4p5,
  582. },
  583. {
  584. .name = "da850-async1-clksrc",
  585. .driver_data = (kernel_ulong_t)da8xx_cfgchip_register_async1,
  586. },
  587. {
  588. .name = "da850-async3-clksrc",
  589. .driver_data = (kernel_ulong_t)da850_cfgchip_register_async3,
  590. },
  591. {
  592. .name = "da830-usb-phy-clks",
  593. .driver_data = (kernel_ulong_t)da8xx_cfgchip_register_usb_phy_clk,
  594. },
  595. { }
  596. };
  597. typedef int (*da8xx_cfgchip_init)(struct device *dev, struct regmap *regmap);
  598. static int da8xx_cfgchip_probe(struct platform_device *pdev)
  599. {
  600. struct device *dev = &pdev->dev;
  601. struct da8xx_cfgchip_clk_platform_data *pdata = dev->platform_data;
  602. const struct of_device_id *of_id;
  603. da8xx_cfgchip_init clk_init = NULL;
  604. struct regmap *regmap = NULL;
  605. of_id = of_match_device(da8xx_cfgchip_of_match, dev);
  606. if (of_id) {
  607. struct device_node *parent;
  608. clk_init = of_id->data;
  609. parent = of_get_parent(dev->of_node);
  610. regmap = syscon_node_to_regmap(parent);
  611. of_node_put(parent);
  612. } else if (pdev->id_entry && pdata) {
  613. clk_init = (void *)pdev->id_entry->driver_data;
  614. regmap = pdata->cfgchip;
  615. }
  616. if (!clk_init) {
  617. dev_err(dev, "unable to find driver data\n");
  618. return -EINVAL;
  619. }
  620. if (IS_ERR_OR_NULL(regmap)) {
  621. dev_err(dev, "no regmap for CFGCHIP syscon\n");
  622. return regmap ? PTR_ERR(regmap) : -ENOENT;
  623. }
  624. return clk_init(dev, regmap);
  625. }
  626. static struct platform_driver da8xx_cfgchip_driver = {
  627. .probe = da8xx_cfgchip_probe,
  628. .driver = {
  629. .name = "da8xx-cfgchip-clk",
  630. .of_match_table = da8xx_cfgchip_of_match,
  631. },
  632. .id_table = da8xx_cfgchip_id_table,
  633. };
  634. static int __init da8xx_cfgchip_driver_init(void)
  635. {
  636. return platform_driver_register(&da8xx_cfgchip_driver);
  637. }
  638. /* has to be postcore_initcall because PSC devices depend on the async3 clock */
  639. postcore_initcall(da8xx_cfgchip_driver_init);