dt-compat.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/clk-provider.h>
  3. #include <linux/clk/at91_pmc.h>
  4. #include <linux/of.h>
  5. #include <linux/mfd/syscon.h>
  6. #include <linux/regmap.h>
  7. #include <linux/slab.h>
  8. #include "pmc.h"
  9. #define MASTER_SOURCE_MAX 4
  10. #define PERIPHERAL_AT91RM9200 0
  11. #define PERIPHERAL_AT91SAM9X5 1
  12. #define PERIPHERAL_MAX 64
  13. #define PERIPHERAL_ID_MIN 2
  14. #define PROG_SOURCE_MAX 5
  15. #define PROG_ID_MAX 7
  16. #define SYSTEM_MAX_ID 31
  17. #define GCK_INDEX_DT_AUDIO_PLL 5
  18. static DEFINE_SPINLOCK(mck_lock);
  19. #ifdef CONFIG_HAVE_AT91_AUDIO_PLL
  20. static void __init of_sama5d2_clk_audio_pll_frac_setup(struct device_node *np)
  21. {
  22. struct clk_hw *hw;
  23. const char *name = np->name;
  24. const char *parent_name;
  25. struct regmap *regmap;
  26. struct device_node *parent_np;
  27. parent_np = of_get_parent(np);
  28. regmap = syscon_node_to_regmap(parent_np);
  29. of_node_put(parent_np);
  30. if (IS_ERR(regmap))
  31. return;
  32. parent_name = of_clk_get_parent_name(np, 0);
  33. hw = at91_clk_register_audio_pll_frac(regmap, name, parent_name);
  34. if (IS_ERR(hw))
  35. return;
  36. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  37. }
  38. CLK_OF_DECLARE(of_sama5d2_clk_audio_pll_frac_setup,
  39. "atmel,sama5d2-clk-audio-pll-frac",
  40. of_sama5d2_clk_audio_pll_frac_setup);
  41. static void __init of_sama5d2_clk_audio_pll_pad_setup(struct device_node *np)
  42. {
  43. struct clk_hw *hw;
  44. const char *name = np->name;
  45. const char *parent_name;
  46. struct regmap *regmap;
  47. struct device_node *parent_np;
  48. parent_np = of_get_parent(np);
  49. regmap = syscon_node_to_regmap(parent_np);
  50. of_node_put(parent_np);
  51. if (IS_ERR(regmap))
  52. return;
  53. parent_name = of_clk_get_parent_name(np, 0);
  54. hw = at91_clk_register_audio_pll_pad(regmap, name, parent_name);
  55. if (IS_ERR(hw))
  56. return;
  57. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  58. }
  59. CLK_OF_DECLARE(of_sama5d2_clk_audio_pll_pad_setup,
  60. "atmel,sama5d2-clk-audio-pll-pad",
  61. of_sama5d2_clk_audio_pll_pad_setup);
  62. static void __init of_sama5d2_clk_audio_pll_pmc_setup(struct device_node *np)
  63. {
  64. struct clk_hw *hw;
  65. const char *name = np->name;
  66. const char *parent_name;
  67. struct regmap *regmap;
  68. struct device_node *parent_np;
  69. parent_np = of_get_parent(np);
  70. regmap = syscon_node_to_regmap(parent_np);
  71. of_node_put(parent_np);
  72. if (IS_ERR(regmap))
  73. return;
  74. parent_name = of_clk_get_parent_name(np, 0);
  75. hw = at91_clk_register_audio_pll_pmc(regmap, name, parent_name);
  76. if (IS_ERR(hw))
  77. return;
  78. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  79. }
  80. CLK_OF_DECLARE(of_sama5d2_clk_audio_pll_pmc_setup,
  81. "atmel,sama5d2-clk-audio-pll-pmc",
  82. of_sama5d2_clk_audio_pll_pmc_setup);
  83. #endif /* CONFIG_HAVE_AT91_AUDIO_PLL */
  84. static const struct clk_pcr_layout dt_pcr_layout = {
  85. .offset = 0x10c,
  86. .cmd = BIT(12),
  87. .pid_mask = GENMASK(5, 0),
  88. .div_mask = GENMASK(17, 16),
  89. .gckcss_mask = GENMASK(10, 8),
  90. };
  91. #ifdef CONFIG_HAVE_AT91_GENERATED_CLK
  92. #define GENERATED_SOURCE_MAX 6
  93. #define GCK_ID_I2S0 54
  94. #define GCK_ID_I2S1 55
  95. #define GCK_ID_CLASSD 59
  96. static void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  97. {
  98. int num;
  99. u32 id;
  100. const char *name;
  101. struct clk_hw *hw;
  102. unsigned int num_parents;
  103. const char *parent_names[GENERATED_SOURCE_MAX];
  104. struct device_node *gcknp, *parent_np;
  105. struct clk_range range = CLK_RANGE(0, 0);
  106. struct regmap *regmap;
  107. num_parents = of_clk_get_parent_count(np);
  108. if (num_parents == 0 || num_parents > GENERATED_SOURCE_MAX)
  109. return;
  110. of_clk_parent_fill(np, parent_names, num_parents);
  111. num = of_get_child_count(np);
  112. if (!num || num > PERIPHERAL_MAX)
  113. return;
  114. parent_np = of_get_parent(np);
  115. regmap = syscon_node_to_regmap(parent_np);
  116. of_node_put(parent_np);
  117. if (IS_ERR(regmap))
  118. return;
  119. for_each_child_of_node(np, gcknp) {
  120. int chg_pid = INT_MIN;
  121. if (of_property_read_u32(gcknp, "reg", &id))
  122. continue;
  123. if (id < PERIPHERAL_ID_MIN || id >= PERIPHERAL_MAX)
  124. continue;
  125. if (of_property_read_string(np, "clock-output-names", &name))
  126. name = gcknp->name;
  127. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  128. &range);
  129. if (of_device_is_compatible(np, "atmel,sama5d2-clk-generated") &&
  130. (id == GCK_ID_I2S0 || id == GCK_ID_I2S1 ||
  131. id == GCK_ID_CLASSD))
  132. chg_pid = GCK_INDEX_DT_AUDIO_PLL;
  133. hw = at91_clk_register_generated(regmap, &pmc_pcr_lock,
  134. &dt_pcr_layout, name,
  135. parent_names, NULL,
  136. num_parents, id, &range,
  137. chg_pid);
  138. if (IS_ERR(hw))
  139. continue;
  140. of_clk_add_hw_provider(gcknp, of_clk_hw_simple_get, hw);
  141. }
  142. }
  143. CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  144. of_sama5d2_clk_generated_setup);
  145. #endif /* CONFIG_HAVE_AT91_GENERATED_CLK */
  146. #ifdef CONFIG_HAVE_AT91_H32MX
  147. static void __init of_sama5d4_clk_h32mx_setup(struct device_node *np)
  148. {
  149. struct clk_hw *hw;
  150. const char *name = np->name;
  151. const char *parent_name;
  152. struct regmap *regmap;
  153. struct device_node *parent_np;
  154. parent_np = of_get_parent(np);
  155. regmap = syscon_node_to_regmap(parent_np);
  156. of_node_put(parent_np);
  157. if (IS_ERR(regmap))
  158. return;
  159. parent_name = of_clk_get_parent_name(np, 0);
  160. hw = at91_clk_register_h32mx(regmap, name, parent_name);
  161. if (IS_ERR(hw))
  162. return;
  163. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  164. }
  165. CLK_OF_DECLARE(of_sama5d4_clk_h32mx_setup, "atmel,sama5d4-clk-h32mx",
  166. of_sama5d4_clk_h32mx_setup);
  167. #endif /* CONFIG_HAVE_AT91_H32MX */
  168. #ifdef CONFIG_HAVE_AT91_I2S_MUX_CLK
  169. #define I2S_BUS_NR 2
  170. static void __init of_sama5d2_clk_i2s_mux_setup(struct device_node *np)
  171. {
  172. struct regmap *regmap_sfr;
  173. u8 bus_id;
  174. const char *parent_names[2];
  175. struct device_node *i2s_mux_np;
  176. struct clk_hw *hw;
  177. int ret;
  178. regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
  179. if (IS_ERR(regmap_sfr))
  180. return;
  181. for_each_child_of_node(np, i2s_mux_np) {
  182. if (of_property_read_u8(i2s_mux_np, "reg", &bus_id))
  183. continue;
  184. if (bus_id > I2S_BUS_NR)
  185. continue;
  186. ret = of_clk_parent_fill(i2s_mux_np, parent_names, 2);
  187. if (ret != 2)
  188. continue;
  189. hw = at91_clk_i2s_mux_register(regmap_sfr, i2s_mux_np->name,
  190. parent_names, 2, bus_id);
  191. if (IS_ERR(hw))
  192. continue;
  193. of_clk_add_hw_provider(i2s_mux_np, of_clk_hw_simple_get, hw);
  194. }
  195. }
  196. CLK_OF_DECLARE(sama5d2_clk_i2s_mux, "atmel,sama5d2-clk-i2s-mux",
  197. of_sama5d2_clk_i2s_mux_setup);
  198. #endif /* CONFIG_HAVE_AT91_I2S_MUX_CLK */
  199. static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
  200. {
  201. struct clk_hw *hw;
  202. const char *name = np->name;
  203. const char *parent_name;
  204. struct regmap *regmap;
  205. bool bypass;
  206. struct device_node *parent_np;
  207. of_property_read_string(np, "clock-output-names", &name);
  208. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  209. parent_name = of_clk_get_parent_name(np, 0);
  210. parent_np = of_get_parent(np);
  211. regmap = syscon_node_to_regmap(parent_np);
  212. of_node_put(parent_np);
  213. if (IS_ERR(regmap))
  214. return;
  215. hw = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
  216. if (IS_ERR(hw))
  217. return;
  218. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  219. }
  220. CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
  221. of_at91rm9200_clk_main_osc_setup);
  222. static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
  223. {
  224. struct clk_hw *hw;
  225. u32 frequency = 0;
  226. u32 accuracy = 0;
  227. const char *name = np->name;
  228. struct regmap *regmap;
  229. struct device_node *parent_np;
  230. of_property_read_string(np, "clock-output-names", &name);
  231. of_property_read_u32(np, "clock-frequency", &frequency);
  232. of_property_read_u32(np, "clock-accuracy", &accuracy);
  233. parent_np = of_get_parent(np);
  234. regmap = syscon_node_to_regmap(parent_np);
  235. of_node_put(parent_np);
  236. if (IS_ERR(regmap))
  237. return;
  238. hw = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
  239. if (IS_ERR(hw))
  240. return;
  241. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  242. }
  243. CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
  244. of_at91sam9x5_clk_main_rc_osc_setup);
  245. static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
  246. {
  247. struct clk_hw *hw;
  248. const char *parent_name;
  249. const char *name = np->name;
  250. struct regmap *regmap;
  251. struct device_node *parent_np;
  252. parent_name = of_clk_get_parent_name(np, 0);
  253. of_property_read_string(np, "clock-output-names", &name);
  254. parent_np = of_get_parent(np);
  255. regmap = syscon_node_to_regmap(parent_np);
  256. of_node_put(parent_np);
  257. if (IS_ERR(regmap))
  258. return;
  259. hw = at91_clk_register_rm9200_main(regmap, name, parent_name);
  260. if (IS_ERR(hw))
  261. return;
  262. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  263. }
  264. CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
  265. of_at91rm9200_clk_main_setup);
  266. static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
  267. {
  268. struct clk_hw *hw;
  269. const char *parent_names[2];
  270. unsigned int num_parents;
  271. const char *name = np->name;
  272. struct regmap *regmap;
  273. struct device_node *parent_np;
  274. num_parents = of_clk_get_parent_count(np);
  275. if (num_parents == 0 || num_parents > 2)
  276. return;
  277. of_clk_parent_fill(np, parent_names, num_parents);
  278. parent_np = of_get_parent(np);
  279. regmap = syscon_node_to_regmap(parent_np);
  280. of_node_put(parent_np);
  281. if (IS_ERR(regmap))
  282. return;
  283. of_property_read_string(np, "clock-output-names", &name);
  284. hw = at91_clk_register_sam9x5_main(regmap, name, parent_names,
  285. num_parents);
  286. if (IS_ERR(hw))
  287. return;
  288. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  289. }
  290. CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
  291. of_at91sam9x5_clk_main_setup);
  292. static struct clk_master_characteristics * __init
  293. of_at91_clk_master_get_characteristics(struct device_node *np)
  294. {
  295. struct clk_master_characteristics *characteristics;
  296. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  297. if (!characteristics)
  298. return NULL;
  299. if (of_at91_get_clk_range(np, "atmel,clk-output-range", &characteristics->output))
  300. goto out_free_characteristics;
  301. of_property_read_u32_array(np, "atmel,clk-divisors",
  302. characteristics->divisors, 4);
  303. characteristics->have_div3_pres =
  304. of_property_read_bool(np, "atmel,master-clk-have-div3-pres");
  305. return characteristics;
  306. out_free_characteristics:
  307. kfree(characteristics);
  308. return NULL;
  309. }
  310. static void __init
  311. of_at91_clk_master_setup(struct device_node *np,
  312. const struct clk_master_layout *layout)
  313. {
  314. struct clk_hw *hw;
  315. unsigned int num_parents;
  316. const char *parent_names[MASTER_SOURCE_MAX];
  317. const char *name = np->name;
  318. struct clk_master_characteristics *characteristics;
  319. struct regmap *regmap;
  320. struct device_node *parent_np;
  321. num_parents = of_clk_get_parent_count(np);
  322. if (num_parents == 0 || num_parents > MASTER_SOURCE_MAX)
  323. return;
  324. of_clk_parent_fill(np, parent_names, num_parents);
  325. of_property_read_string(np, "clock-output-names", &name);
  326. characteristics = of_at91_clk_master_get_characteristics(np);
  327. if (!characteristics)
  328. return;
  329. parent_np = of_get_parent(np);
  330. regmap = syscon_node_to_regmap(parent_np);
  331. of_node_put(parent_np);
  332. if (IS_ERR(regmap))
  333. return;
  334. hw = at91_clk_register_master_pres(regmap, "masterck_pres", num_parents,
  335. parent_names, layout,
  336. characteristics, &mck_lock);
  337. if (IS_ERR(hw))
  338. goto out_free_characteristics;
  339. hw = at91_clk_register_master_div(regmap, name, "masterck_pres",
  340. layout, characteristics,
  341. &mck_lock, CLK_SET_RATE_GATE, 0);
  342. if (IS_ERR(hw))
  343. goto out_free_characteristics;
  344. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  345. return;
  346. out_free_characteristics:
  347. kfree(characteristics);
  348. }
  349. static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  350. {
  351. of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  352. }
  353. CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  354. of_at91rm9200_clk_master_setup);
  355. static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  356. {
  357. of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  358. }
  359. CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  360. of_at91sam9x5_clk_master_setup);
  361. static void __init
  362. of_at91_clk_periph_setup(struct device_node *np, u8 type)
  363. {
  364. int num;
  365. u32 id;
  366. struct clk_hw *hw;
  367. const char *parent_name;
  368. const char *name;
  369. struct device_node *periphclknp;
  370. struct regmap *regmap;
  371. struct device_node *parent_np;
  372. parent_name = of_clk_get_parent_name(np, 0);
  373. if (!parent_name)
  374. return;
  375. num = of_get_child_count(np);
  376. if (!num || num > PERIPHERAL_MAX)
  377. return;
  378. parent_np = of_get_parent(np);
  379. regmap = syscon_node_to_regmap(parent_np);
  380. of_node_put(parent_np);
  381. if (IS_ERR(regmap))
  382. return;
  383. for_each_child_of_node(np, periphclknp) {
  384. if (of_property_read_u32(periphclknp, "reg", &id))
  385. continue;
  386. if (id >= PERIPHERAL_MAX)
  387. continue;
  388. if (of_property_read_string(np, "clock-output-names", &name))
  389. name = periphclknp->name;
  390. if (type == PERIPHERAL_AT91RM9200) {
  391. hw = at91_clk_register_peripheral(regmap, name,
  392. parent_name, id);
  393. } else {
  394. struct clk_range range = CLK_RANGE(0, 0);
  395. of_at91_get_clk_range(periphclknp,
  396. "atmel,clk-output-range",
  397. &range);
  398. hw = at91_clk_register_sam9x5_peripheral(regmap,
  399. &pmc_pcr_lock,
  400. &dt_pcr_layout,
  401. name,
  402. parent_name,
  403. id, &range,
  404. INT_MIN);
  405. }
  406. if (IS_ERR(hw))
  407. continue;
  408. of_clk_add_hw_provider(periphclknp, of_clk_hw_simple_get, hw);
  409. }
  410. }
  411. static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  412. {
  413. of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  414. }
  415. CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  416. of_at91rm9200_clk_periph_setup);
  417. static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  418. {
  419. of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  420. }
  421. CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  422. of_at91sam9x5_clk_periph_setup);
  423. static struct clk_pll_characteristics * __init
  424. of_at91_clk_pll_get_characteristics(struct device_node *np)
  425. {
  426. int i;
  427. int offset;
  428. u32 tmp;
  429. int num_output;
  430. u32 num_cells;
  431. struct clk_range input;
  432. struct clk_range *output;
  433. u8 *out = NULL;
  434. u16 *icpll = NULL;
  435. struct clk_pll_characteristics *characteristics;
  436. if (of_at91_get_clk_range(np, "atmel,clk-input-range", &input))
  437. return NULL;
  438. if (of_property_read_u32(np, "#atmel,pll-clk-output-range-cells",
  439. &num_cells))
  440. return NULL;
  441. if (num_cells < 2 || num_cells > 4)
  442. return NULL;
  443. if (!of_get_property(np, "atmel,pll-clk-output-ranges", &tmp))
  444. return NULL;
  445. num_output = tmp / (sizeof(u32) * num_cells);
  446. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  447. if (!characteristics)
  448. return NULL;
  449. output = kcalloc(num_output, sizeof(*output), GFP_KERNEL);
  450. if (!output)
  451. goto out_free_characteristics;
  452. if (num_cells > 2) {
  453. out = kcalloc(num_output, sizeof(*out), GFP_KERNEL);
  454. if (!out)
  455. goto out_free_output;
  456. }
  457. if (num_cells > 3) {
  458. icpll = kcalloc(num_output, sizeof(*icpll), GFP_KERNEL);
  459. if (!icpll)
  460. goto out_free_output;
  461. }
  462. for (i = 0; i < num_output; i++) {
  463. offset = i * num_cells;
  464. if (of_property_read_u32_index(np,
  465. "atmel,pll-clk-output-ranges",
  466. offset, &tmp))
  467. goto out_free_output;
  468. output[i].min = tmp;
  469. if (of_property_read_u32_index(np,
  470. "atmel,pll-clk-output-ranges",
  471. offset + 1, &tmp))
  472. goto out_free_output;
  473. output[i].max = tmp;
  474. if (num_cells == 2)
  475. continue;
  476. if (of_property_read_u32_index(np,
  477. "atmel,pll-clk-output-ranges",
  478. offset + 2, &tmp))
  479. goto out_free_output;
  480. out[i] = tmp;
  481. if (num_cells == 3)
  482. continue;
  483. if (of_property_read_u32_index(np,
  484. "atmel,pll-clk-output-ranges",
  485. offset + 3, &tmp))
  486. goto out_free_output;
  487. icpll[i] = tmp;
  488. }
  489. characteristics->input = input;
  490. characteristics->num_output = num_output;
  491. characteristics->output = output;
  492. characteristics->out = out;
  493. characteristics->icpll = icpll;
  494. return characteristics;
  495. out_free_output:
  496. kfree(icpll);
  497. kfree(out);
  498. kfree(output);
  499. out_free_characteristics:
  500. kfree(characteristics);
  501. return NULL;
  502. }
  503. static void __init
  504. of_at91_clk_pll_setup(struct device_node *np,
  505. const struct clk_pll_layout *layout)
  506. {
  507. u32 id;
  508. struct clk_hw *hw;
  509. struct regmap *regmap;
  510. const char *parent_name;
  511. const char *name = np->name;
  512. struct device_node *parent_np;
  513. struct clk_pll_characteristics *characteristics;
  514. if (of_property_read_u32(np, "reg", &id))
  515. return;
  516. parent_name = of_clk_get_parent_name(np, 0);
  517. of_property_read_string(np, "clock-output-names", &name);
  518. parent_np = of_get_parent(np);
  519. regmap = syscon_node_to_regmap(parent_np);
  520. of_node_put(parent_np);
  521. if (IS_ERR(regmap))
  522. return;
  523. characteristics = of_at91_clk_pll_get_characteristics(np);
  524. if (!characteristics)
  525. return;
  526. hw = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  527. characteristics);
  528. if (IS_ERR(hw))
  529. goto out_free_characteristics;
  530. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  531. return;
  532. out_free_characteristics:
  533. kfree(characteristics);
  534. }
  535. static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  536. {
  537. of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  538. }
  539. CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  540. of_at91rm9200_clk_pll_setup);
  541. static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  542. {
  543. of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  544. }
  545. CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  546. of_at91sam9g45_clk_pll_setup);
  547. static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  548. {
  549. of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  550. }
  551. CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  552. of_at91sam9g20_clk_pllb_setup);
  553. static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  554. {
  555. of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  556. }
  557. CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  558. of_sama5d3_clk_pll_setup);
  559. static void __init
  560. of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  561. {
  562. struct clk_hw *hw;
  563. const char *parent_name;
  564. const char *name = np->name;
  565. struct regmap *regmap;
  566. struct device_node *parent_np;
  567. parent_name = of_clk_get_parent_name(np, 0);
  568. of_property_read_string(np, "clock-output-names", &name);
  569. parent_np = of_get_parent(np);
  570. regmap = syscon_node_to_regmap(parent_np);
  571. of_node_put(parent_np);
  572. if (IS_ERR(regmap))
  573. return;
  574. hw = at91_clk_register_plldiv(regmap, name, parent_name);
  575. if (IS_ERR(hw))
  576. return;
  577. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  578. }
  579. CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  580. of_at91sam9x5_clk_plldiv_setup);
  581. static void __init
  582. of_at91_clk_prog_setup(struct device_node *np,
  583. const struct clk_programmable_layout *layout,
  584. u32 *mux_table)
  585. {
  586. int num;
  587. u32 id;
  588. struct clk_hw *hw;
  589. unsigned int num_parents;
  590. const char *parent_names[PROG_SOURCE_MAX];
  591. const char *name;
  592. struct device_node *progclknp, *parent_np;
  593. struct regmap *regmap;
  594. num_parents = of_clk_get_parent_count(np);
  595. if (num_parents == 0 || num_parents > PROG_SOURCE_MAX)
  596. return;
  597. of_clk_parent_fill(np, parent_names, num_parents);
  598. num = of_get_child_count(np);
  599. if (!num || num > (PROG_ID_MAX + 1))
  600. return;
  601. parent_np = of_get_parent(np);
  602. regmap = syscon_node_to_regmap(parent_np);
  603. of_node_put(parent_np);
  604. if (IS_ERR(regmap))
  605. return;
  606. for_each_child_of_node(np, progclknp) {
  607. if (of_property_read_u32(progclknp, "reg", &id))
  608. continue;
  609. if (of_property_read_string(np, "clock-output-names", &name))
  610. name = progclknp->name;
  611. hw = at91_clk_register_programmable(regmap, name,
  612. parent_names, num_parents,
  613. id, layout, mux_table);
  614. if (IS_ERR(hw))
  615. continue;
  616. of_clk_add_hw_provider(progclknp, of_clk_hw_simple_get, hw);
  617. }
  618. }
  619. static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  620. {
  621. of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout, NULL);
  622. }
  623. CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  624. of_at91rm9200_clk_prog_setup);
  625. static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  626. {
  627. of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout, NULL);
  628. }
  629. CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  630. of_at91sam9g45_clk_prog_setup);
  631. static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  632. {
  633. of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout, NULL);
  634. }
  635. CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  636. of_at91sam9x5_clk_prog_setup);
  637. static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  638. {
  639. struct clk_hw *hw;
  640. const char *parent_names[2];
  641. unsigned int num_parents;
  642. const char *name = np->name;
  643. struct regmap *regmap;
  644. struct device_node *parent_np;
  645. num_parents = of_clk_get_parent_count(np);
  646. if (num_parents != 2)
  647. return;
  648. of_clk_parent_fill(np, parent_names, num_parents);
  649. parent_np = of_get_parent(np);
  650. regmap = syscon_node_to_regmap(parent_np);
  651. of_node_put(parent_np);
  652. if (IS_ERR(regmap))
  653. return;
  654. of_property_read_string(np, "clock-output-names", &name);
  655. hw = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  656. num_parents);
  657. if (IS_ERR(hw))
  658. return;
  659. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  660. }
  661. CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  662. of_at91sam9260_clk_slow_setup);
  663. #ifdef CONFIG_HAVE_AT91_SMD
  664. #define SMD_SOURCE_MAX 2
  665. static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  666. {
  667. struct clk_hw *hw;
  668. unsigned int num_parents;
  669. const char *parent_names[SMD_SOURCE_MAX];
  670. const char *name = np->name;
  671. struct regmap *regmap;
  672. struct device_node *parent_np;
  673. num_parents = of_clk_get_parent_count(np);
  674. if (num_parents == 0 || num_parents > SMD_SOURCE_MAX)
  675. return;
  676. of_clk_parent_fill(np, parent_names, num_parents);
  677. of_property_read_string(np, "clock-output-names", &name);
  678. parent_np = of_get_parent(np);
  679. regmap = syscon_node_to_regmap(parent_np);
  680. of_node_put(parent_np);
  681. if (IS_ERR(regmap))
  682. return;
  683. hw = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  684. num_parents);
  685. if (IS_ERR(hw))
  686. return;
  687. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  688. }
  689. CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  690. of_at91sam9x5_clk_smd_setup);
  691. #endif /* CONFIG_HAVE_AT91_SMD */
  692. static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  693. {
  694. int num;
  695. u32 id;
  696. struct clk_hw *hw;
  697. const char *name;
  698. struct device_node *sysclknp, *parent_np;
  699. const char *parent_name;
  700. struct regmap *regmap;
  701. num = of_get_child_count(np);
  702. if (num > (SYSTEM_MAX_ID + 1))
  703. return;
  704. parent_np = of_get_parent(np);
  705. regmap = syscon_node_to_regmap(parent_np);
  706. of_node_put(parent_np);
  707. if (IS_ERR(regmap))
  708. return;
  709. for_each_child_of_node(np, sysclknp) {
  710. if (of_property_read_u32(sysclknp, "reg", &id))
  711. continue;
  712. if (of_property_read_string(np, "clock-output-names", &name))
  713. name = sysclknp->name;
  714. parent_name = of_clk_get_parent_name(sysclknp, 0);
  715. hw = at91_clk_register_system(regmap, name, parent_name, id);
  716. if (IS_ERR(hw))
  717. continue;
  718. of_clk_add_hw_provider(sysclknp, of_clk_hw_simple_get, hw);
  719. }
  720. }
  721. CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  722. of_at91rm9200_clk_sys_setup);
  723. #ifdef CONFIG_HAVE_AT91_USB_CLK
  724. #define USB_SOURCE_MAX 2
  725. static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  726. {
  727. struct clk_hw *hw;
  728. unsigned int num_parents;
  729. const char *parent_names[USB_SOURCE_MAX];
  730. const char *name = np->name;
  731. struct regmap *regmap;
  732. struct device_node *parent_np;
  733. num_parents = of_clk_get_parent_count(np);
  734. if (num_parents == 0 || num_parents > USB_SOURCE_MAX)
  735. return;
  736. of_clk_parent_fill(np, parent_names, num_parents);
  737. of_property_read_string(np, "clock-output-names", &name);
  738. parent_np = of_get_parent(np);
  739. regmap = syscon_node_to_regmap(parent_np);
  740. of_node_put(parent_np);
  741. if (IS_ERR(regmap))
  742. return;
  743. hw = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  744. num_parents);
  745. if (IS_ERR(hw))
  746. return;
  747. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  748. }
  749. CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  750. of_at91sam9x5_clk_usb_setup);
  751. static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  752. {
  753. struct clk_hw *hw;
  754. const char *parent_name;
  755. const char *name = np->name;
  756. struct regmap *regmap;
  757. struct device_node *parent_np;
  758. parent_name = of_clk_get_parent_name(np, 0);
  759. if (!parent_name)
  760. return;
  761. of_property_read_string(np, "clock-output-names", &name);
  762. parent_np = of_get_parent(np);
  763. regmap = syscon_node_to_regmap(parent_np);
  764. of_node_put(parent_np);
  765. if (IS_ERR(regmap))
  766. return;
  767. hw = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  768. if (IS_ERR(hw))
  769. return;
  770. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  771. }
  772. CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  773. of_at91sam9n12_clk_usb_setup);
  774. static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  775. {
  776. struct clk_hw *hw;
  777. const char *parent_name;
  778. const char *name = np->name;
  779. u32 divisors[4] = {0, 0, 0, 0};
  780. struct regmap *regmap;
  781. struct device_node *parent_np;
  782. parent_name = of_clk_get_parent_name(np, 0);
  783. if (!parent_name)
  784. return;
  785. of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
  786. if (!divisors[0])
  787. return;
  788. of_property_read_string(np, "clock-output-names", &name);
  789. parent_np = of_get_parent(np);
  790. regmap = syscon_node_to_regmap(parent_np);
  791. of_node_put(parent_np);
  792. if (IS_ERR(regmap))
  793. return;
  794. hw = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  795. if (IS_ERR(hw))
  796. return;
  797. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  798. }
  799. CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  800. of_at91rm9200_clk_usb_setup);
  801. #endif /* CONFIG_HAVE_AT91_USB_CLK */
  802. #ifdef CONFIG_HAVE_AT91_UTMI
  803. static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  804. {
  805. struct clk_hw *hw;
  806. const char *parent_name;
  807. const char *name = np->name;
  808. struct regmap *regmap_pmc, *regmap_sfr;
  809. struct device_node *parent_np;
  810. parent_name = of_clk_get_parent_name(np, 0);
  811. of_property_read_string(np, "clock-output-names", &name);
  812. parent_np = of_get_parent(np);
  813. regmap_pmc = syscon_node_to_regmap(parent_np);
  814. of_node_put(parent_np);
  815. if (IS_ERR(regmap_pmc))
  816. return;
  817. /*
  818. * If the device supports different mainck rates, this value has to be
  819. * set in the UTMI Clock Trimming register.
  820. * - 9x5: mainck supports several rates but it is indicated that a
  821. * 12 MHz is needed in case of USB.
  822. * - sama5d3 and sama5d2: mainck supports several rates. Configuring
  823. * the FREQ field of the UTMI Clock Trimming register is mandatory.
  824. * - sama5d4: mainck is at 12 MHz.
  825. *
  826. * We only need to retrieve sama5d3 or sama5d2 sfr regmap.
  827. */
  828. regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d3-sfr");
  829. if (IS_ERR(regmap_sfr)) {
  830. regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
  831. if (IS_ERR(regmap_sfr))
  832. regmap_sfr = NULL;
  833. }
  834. hw = at91_clk_register_utmi(regmap_pmc, regmap_sfr, name, parent_name);
  835. if (IS_ERR(hw))
  836. return;
  837. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  838. }
  839. CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  840. of_at91sam9x5_clk_utmi_setup);
  841. #endif /* CONFIG_HAVE_AT91_UTMI */