dt-compat.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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, 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, NULL, 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, NULL);
  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, NULL,
  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, NULL, 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", NULL,
  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, NULL, id);
  393. } else {
  394. struct clk_range range = CLK_RANGE(0, 0);
  395. unsigned long flags = 0;
  396. of_at91_get_clk_range(periphclknp,
  397. "atmel,clk-output-range",
  398. &range);
  399. /*
  400. * mpddr_clk feed DDR controller and is enabled by
  401. * bootloader thus we need to keep it enabled in case
  402. * there is no Linux consumer for it.
  403. */
  404. if (!strcmp(periphclknp->name, "mpddr_clk"))
  405. flags = CLK_IS_CRITICAL;
  406. hw = at91_clk_register_sam9x5_peripheral(regmap,
  407. &pmc_pcr_lock,
  408. &dt_pcr_layout,
  409. name,
  410. parent_name,
  411. NULL,
  412. id, &range,
  413. INT_MIN,
  414. flags);
  415. }
  416. if (IS_ERR(hw))
  417. continue;
  418. of_clk_add_hw_provider(periphclknp, of_clk_hw_simple_get, hw);
  419. }
  420. }
  421. static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  422. {
  423. of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  424. }
  425. CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  426. of_at91rm9200_clk_periph_setup);
  427. static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  428. {
  429. of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  430. }
  431. CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  432. of_at91sam9x5_clk_periph_setup);
  433. static struct clk_pll_characteristics * __init
  434. of_at91_clk_pll_get_characteristics(struct device_node *np)
  435. {
  436. int i;
  437. int offset;
  438. u32 tmp;
  439. int num_output;
  440. u32 num_cells;
  441. struct clk_range input;
  442. struct clk_range *output;
  443. u8 *out = NULL;
  444. u16 *icpll = NULL;
  445. struct clk_pll_characteristics *characteristics;
  446. if (of_at91_get_clk_range(np, "atmel,clk-input-range", &input))
  447. return NULL;
  448. if (of_property_read_u32(np, "#atmel,pll-clk-output-range-cells",
  449. &num_cells))
  450. return NULL;
  451. if (num_cells < 2 || num_cells > 4)
  452. return NULL;
  453. num_output = of_property_count_u32_elems(np, "atmel,pll-clk-output-ranges");
  454. if (num_output <= 0)
  455. return NULL;
  456. num_output /= num_cells;
  457. characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL);
  458. if (!characteristics)
  459. return NULL;
  460. output = kcalloc(num_output, sizeof(*output), GFP_KERNEL);
  461. if (!output)
  462. goto out_free_characteristics;
  463. if (num_cells > 2) {
  464. out = kcalloc(num_output, sizeof(*out), GFP_KERNEL);
  465. if (!out)
  466. goto out_free_output;
  467. }
  468. if (num_cells > 3) {
  469. icpll = kcalloc(num_output, sizeof(*icpll), GFP_KERNEL);
  470. if (!icpll)
  471. goto out_free_output;
  472. }
  473. for (i = 0; i < num_output; i++) {
  474. offset = i * num_cells;
  475. if (of_property_read_u32_index(np,
  476. "atmel,pll-clk-output-ranges",
  477. offset, &tmp))
  478. goto out_free_output;
  479. output[i].min = tmp;
  480. if (of_property_read_u32_index(np,
  481. "atmel,pll-clk-output-ranges",
  482. offset + 1, &tmp))
  483. goto out_free_output;
  484. output[i].max = tmp;
  485. if (num_cells == 2)
  486. continue;
  487. if (of_property_read_u32_index(np,
  488. "atmel,pll-clk-output-ranges",
  489. offset + 2, &tmp))
  490. goto out_free_output;
  491. out[i] = tmp;
  492. if (num_cells == 3)
  493. continue;
  494. if (of_property_read_u32_index(np,
  495. "atmel,pll-clk-output-ranges",
  496. offset + 3, &tmp))
  497. goto out_free_output;
  498. icpll[i] = tmp;
  499. }
  500. characteristics->input = input;
  501. characteristics->num_output = num_output;
  502. characteristics->output = output;
  503. characteristics->out = out;
  504. characteristics->icpll = icpll;
  505. return characteristics;
  506. out_free_output:
  507. kfree(icpll);
  508. kfree(out);
  509. kfree(output);
  510. out_free_characteristics:
  511. kfree(characteristics);
  512. return NULL;
  513. }
  514. static void __init
  515. of_at91_clk_pll_setup(struct device_node *np,
  516. const struct clk_pll_layout *layout)
  517. {
  518. u32 id;
  519. struct clk_hw *hw;
  520. struct regmap *regmap;
  521. const char *parent_name;
  522. const char *name = np->name;
  523. struct device_node *parent_np;
  524. struct clk_pll_characteristics *characteristics;
  525. if (of_property_read_u32(np, "reg", &id))
  526. return;
  527. parent_name = of_clk_get_parent_name(np, 0);
  528. of_property_read_string(np, "clock-output-names", &name);
  529. parent_np = of_get_parent(np);
  530. regmap = syscon_node_to_regmap(parent_np);
  531. of_node_put(parent_np);
  532. if (IS_ERR(regmap))
  533. return;
  534. characteristics = of_at91_clk_pll_get_characteristics(np);
  535. if (!characteristics)
  536. return;
  537. hw = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  538. characteristics);
  539. if (IS_ERR(hw))
  540. goto out_free_characteristics;
  541. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  542. return;
  543. out_free_characteristics:
  544. kfree(characteristics);
  545. }
  546. static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  547. {
  548. of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  549. }
  550. CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  551. of_at91rm9200_clk_pll_setup);
  552. static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  553. {
  554. of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  555. }
  556. CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  557. of_at91sam9g45_clk_pll_setup);
  558. static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  559. {
  560. of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  561. }
  562. CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  563. of_at91sam9g20_clk_pllb_setup);
  564. static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  565. {
  566. of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  567. }
  568. CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  569. of_sama5d3_clk_pll_setup);
  570. static void __init
  571. of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  572. {
  573. struct clk_hw *hw;
  574. const char *parent_name;
  575. const char *name = np->name;
  576. struct regmap *regmap;
  577. struct device_node *parent_np;
  578. parent_name = of_clk_get_parent_name(np, 0);
  579. of_property_read_string(np, "clock-output-names", &name);
  580. parent_np = of_get_parent(np);
  581. regmap = syscon_node_to_regmap(parent_np);
  582. of_node_put(parent_np);
  583. if (IS_ERR(regmap))
  584. return;
  585. hw = at91_clk_register_plldiv(regmap, name, parent_name);
  586. if (IS_ERR(hw))
  587. return;
  588. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  589. }
  590. CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  591. of_at91sam9x5_clk_plldiv_setup);
  592. static void __init
  593. of_at91_clk_prog_setup(struct device_node *np,
  594. const struct clk_programmable_layout *layout,
  595. u32 *mux_table)
  596. {
  597. int num;
  598. u32 id;
  599. struct clk_hw *hw;
  600. unsigned int num_parents;
  601. const char *parent_names[PROG_SOURCE_MAX];
  602. const char *name;
  603. struct device_node *progclknp, *parent_np;
  604. struct regmap *regmap;
  605. num_parents = of_clk_get_parent_count(np);
  606. if (num_parents == 0 || num_parents > PROG_SOURCE_MAX)
  607. return;
  608. of_clk_parent_fill(np, parent_names, num_parents);
  609. num = of_get_child_count(np);
  610. if (!num || num > (PROG_ID_MAX + 1))
  611. return;
  612. parent_np = of_get_parent(np);
  613. regmap = syscon_node_to_regmap(parent_np);
  614. of_node_put(parent_np);
  615. if (IS_ERR(regmap))
  616. return;
  617. for_each_child_of_node(np, progclknp) {
  618. if (of_property_read_u32(progclknp, "reg", &id))
  619. continue;
  620. if (of_property_read_string(np, "clock-output-names", &name))
  621. name = progclknp->name;
  622. hw = at91_clk_register_programmable(regmap, name,
  623. parent_names, NULL, num_parents,
  624. id, layout, mux_table);
  625. if (IS_ERR(hw))
  626. continue;
  627. of_clk_add_hw_provider(progclknp, of_clk_hw_simple_get, hw);
  628. }
  629. }
  630. static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  631. {
  632. of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout, NULL);
  633. }
  634. CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  635. of_at91rm9200_clk_prog_setup);
  636. static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  637. {
  638. of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout, NULL);
  639. }
  640. CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  641. of_at91sam9g45_clk_prog_setup);
  642. static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  643. {
  644. of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout, NULL);
  645. }
  646. CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  647. of_at91sam9x5_clk_prog_setup);
  648. static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  649. {
  650. struct clk_hw *hw;
  651. const char *parent_names[2];
  652. unsigned int num_parents;
  653. const char *name = np->name;
  654. struct regmap *regmap;
  655. struct device_node *parent_np;
  656. num_parents = of_clk_get_parent_count(np);
  657. if (num_parents != 2)
  658. return;
  659. of_clk_parent_fill(np, parent_names, num_parents);
  660. parent_np = of_get_parent(np);
  661. regmap = syscon_node_to_regmap(parent_np);
  662. of_node_put(parent_np);
  663. if (IS_ERR(regmap))
  664. return;
  665. of_property_read_string(np, "clock-output-names", &name);
  666. hw = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  667. num_parents);
  668. if (IS_ERR(hw))
  669. return;
  670. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  671. }
  672. CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  673. of_at91sam9260_clk_slow_setup);
  674. #ifdef CONFIG_HAVE_AT91_SMD
  675. #define SMD_SOURCE_MAX 2
  676. static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  677. {
  678. struct clk_hw *hw;
  679. unsigned int num_parents;
  680. const char *parent_names[SMD_SOURCE_MAX];
  681. const char *name = np->name;
  682. struct regmap *regmap;
  683. struct device_node *parent_np;
  684. num_parents = of_clk_get_parent_count(np);
  685. if (num_parents == 0 || num_parents > SMD_SOURCE_MAX)
  686. return;
  687. of_clk_parent_fill(np, parent_names, num_parents);
  688. of_property_read_string(np, "clock-output-names", &name);
  689. parent_np = of_get_parent(np);
  690. regmap = syscon_node_to_regmap(parent_np);
  691. of_node_put(parent_np);
  692. if (IS_ERR(regmap))
  693. return;
  694. hw = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  695. num_parents);
  696. if (IS_ERR(hw))
  697. return;
  698. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  699. }
  700. CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  701. of_at91sam9x5_clk_smd_setup);
  702. #endif /* CONFIG_HAVE_AT91_SMD */
  703. static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  704. {
  705. int num;
  706. u32 id;
  707. struct clk_hw *hw;
  708. const char *name;
  709. struct device_node *sysclknp, *parent_np;
  710. const char *parent_name;
  711. struct regmap *regmap;
  712. num = of_get_child_count(np);
  713. if (num > (SYSTEM_MAX_ID + 1))
  714. return;
  715. parent_np = of_get_parent(np);
  716. regmap = syscon_node_to_regmap(parent_np);
  717. of_node_put(parent_np);
  718. if (IS_ERR(regmap))
  719. return;
  720. for_each_child_of_node(np, sysclknp) {
  721. unsigned long flags = 0;
  722. if (of_property_read_u32(sysclknp, "reg", &id))
  723. continue;
  724. if (of_property_read_string(np, "clock-output-names", &name))
  725. name = sysclknp->name;
  726. parent_name = of_clk_get_parent_name(sysclknp, 0);
  727. /*
  728. * ddrck feeds DDR controller and is enabled by bootloader thus
  729. * we need to keep it enabled in case there is no Linux consumer
  730. * for it.
  731. */
  732. if (!strcmp(sysclknp->name, "ddrck"))
  733. flags = CLK_IS_CRITICAL;
  734. hw = at91_clk_register_system(regmap, name, parent_name, NULL,
  735. id, flags);
  736. if (IS_ERR(hw))
  737. continue;
  738. of_clk_add_hw_provider(sysclknp, of_clk_hw_simple_get, hw);
  739. }
  740. }
  741. CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  742. of_at91rm9200_clk_sys_setup);
  743. #ifdef CONFIG_HAVE_AT91_USB_CLK
  744. #define USB_SOURCE_MAX 2
  745. static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  746. {
  747. struct clk_hw *hw;
  748. unsigned int num_parents;
  749. const char *parent_names[USB_SOURCE_MAX];
  750. const char *name = np->name;
  751. struct regmap *regmap;
  752. struct device_node *parent_np;
  753. num_parents = of_clk_get_parent_count(np);
  754. if (num_parents == 0 || num_parents > USB_SOURCE_MAX)
  755. return;
  756. of_clk_parent_fill(np, parent_names, num_parents);
  757. of_property_read_string(np, "clock-output-names", &name);
  758. parent_np = of_get_parent(np);
  759. regmap = syscon_node_to_regmap(parent_np);
  760. of_node_put(parent_np);
  761. if (IS_ERR(regmap))
  762. return;
  763. hw = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  764. num_parents);
  765. if (IS_ERR(hw))
  766. return;
  767. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  768. }
  769. CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  770. of_at91sam9x5_clk_usb_setup);
  771. static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  772. {
  773. struct clk_hw *hw;
  774. const char *parent_name;
  775. const char *name = np->name;
  776. struct regmap *regmap;
  777. struct device_node *parent_np;
  778. parent_name = of_clk_get_parent_name(np, 0);
  779. if (!parent_name)
  780. return;
  781. of_property_read_string(np, "clock-output-names", &name);
  782. parent_np = of_get_parent(np);
  783. regmap = syscon_node_to_regmap(parent_np);
  784. of_node_put(parent_np);
  785. if (IS_ERR(regmap))
  786. return;
  787. hw = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  788. if (IS_ERR(hw))
  789. return;
  790. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  791. }
  792. CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  793. of_at91sam9n12_clk_usb_setup);
  794. static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  795. {
  796. struct clk_hw *hw;
  797. const char *parent_name;
  798. const char *name = np->name;
  799. u32 divisors[4] = {0, 0, 0, 0};
  800. struct regmap *regmap;
  801. struct device_node *parent_np;
  802. parent_name = of_clk_get_parent_name(np, 0);
  803. if (!parent_name)
  804. return;
  805. of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
  806. if (!divisors[0])
  807. return;
  808. of_property_read_string(np, "clock-output-names", &name);
  809. parent_np = of_get_parent(np);
  810. regmap = syscon_node_to_regmap(parent_np);
  811. of_node_put(parent_np);
  812. if (IS_ERR(regmap))
  813. return;
  814. hw = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  815. if (IS_ERR(hw))
  816. return;
  817. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  818. }
  819. CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  820. of_at91rm9200_clk_usb_setup);
  821. #endif /* CONFIG_HAVE_AT91_USB_CLK */
  822. #ifdef CONFIG_HAVE_AT91_UTMI
  823. static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  824. {
  825. struct clk_hw *hw;
  826. const char *parent_name;
  827. const char *name = np->name;
  828. struct regmap *regmap_pmc, *regmap_sfr;
  829. struct device_node *parent_np;
  830. parent_name = of_clk_get_parent_name(np, 0);
  831. of_property_read_string(np, "clock-output-names", &name);
  832. parent_np = of_get_parent(np);
  833. regmap_pmc = syscon_node_to_regmap(parent_np);
  834. of_node_put(parent_np);
  835. if (IS_ERR(regmap_pmc))
  836. return;
  837. /*
  838. * If the device supports different mainck rates, this value has to be
  839. * set in the UTMI Clock Trimming register.
  840. * - 9x5: mainck supports several rates but it is indicated that a
  841. * 12 MHz is needed in case of USB.
  842. * - sama5d3 and sama5d2: mainck supports several rates. Configuring
  843. * the FREQ field of the UTMI Clock Trimming register is mandatory.
  844. * - sama5d4: mainck is at 12 MHz.
  845. *
  846. * We only need to retrieve sama5d3 or sama5d2 sfr regmap.
  847. */
  848. regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d3-sfr");
  849. if (IS_ERR(regmap_sfr)) {
  850. regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
  851. if (IS_ERR(regmap_sfr))
  852. regmap_sfr = NULL;
  853. }
  854. hw = at91_clk_register_utmi(regmap_pmc, regmap_sfr, name, parent_name, NULL);
  855. if (IS_ERR(hw))
  856. return;
  857. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  858. }
  859. CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  860. of_at91sam9x5_clk_utmi_setup);
  861. #endif /* CONFIG_HAVE_AT91_UTMI */