clk-peripheral.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/regmap.h>
  16. #include "pmc.h"
  17. DEFINE_SPINLOCK(pmc_pcr_lock);
  18. #define PERIPHERAL_MAX 64
  19. #define PERIPHERAL_AT91RM9200 0
  20. #define PERIPHERAL_AT91SAM9X5 1
  21. #define PERIPHERAL_ID_MIN 2
  22. #define PERIPHERAL_ID_MAX 31
  23. #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
  24. #define PERIPHERAL_RSHIFT_MASK 0x3
  25. #define PERIPHERAL_RSHIFT(val) (((val) >> 16) & PERIPHERAL_RSHIFT_MASK)
  26. #define PERIPHERAL_MAX_SHIFT 3
  27. struct clk_peripheral {
  28. struct clk_hw hw;
  29. struct regmap *regmap;
  30. u32 id;
  31. };
  32. #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
  33. struct clk_sam9x5_peripheral {
  34. struct clk_hw hw;
  35. struct regmap *regmap;
  36. struct clk_range range;
  37. spinlock_t *lock;
  38. u32 id;
  39. u32 div;
  40. bool auto_div;
  41. };
  42. #define to_clk_sam9x5_peripheral(hw) \
  43. container_of(hw, struct clk_sam9x5_peripheral, hw)
  44. static int clk_peripheral_enable(struct clk_hw *hw)
  45. {
  46. struct clk_peripheral *periph = to_clk_peripheral(hw);
  47. int offset = AT91_PMC_PCER;
  48. u32 id = periph->id;
  49. if (id < PERIPHERAL_ID_MIN)
  50. return 0;
  51. if (id > PERIPHERAL_ID_MAX)
  52. offset = AT91_PMC_PCER1;
  53. regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  54. return 0;
  55. }
  56. static void clk_peripheral_disable(struct clk_hw *hw)
  57. {
  58. struct clk_peripheral *periph = to_clk_peripheral(hw);
  59. int offset = AT91_PMC_PCDR;
  60. u32 id = periph->id;
  61. if (id < PERIPHERAL_ID_MIN)
  62. return;
  63. if (id > PERIPHERAL_ID_MAX)
  64. offset = AT91_PMC_PCDR1;
  65. regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  66. }
  67. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  68. {
  69. struct clk_peripheral *periph = to_clk_peripheral(hw);
  70. int offset = AT91_PMC_PCSR;
  71. unsigned int status;
  72. u32 id = periph->id;
  73. if (id < PERIPHERAL_ID_MIN)
  74. return 1;
  75. if (id > PERIPHERAL_ID_MAX)
  76. offset = AT91_PMC_PCSR1;
  77. regmap_read(periph->regmap, offset, &status);
  78. return status & PERIPHERAL_MASK(id) ? 1 : 0;
  79. }
  80. static const struct clk_ops peripheral_ops = {
  81. .enable = clk_peripheral_enable,
  82. .disable = clk_peripheral_disable,
  83. .is_enabled = clk_peripheral_is_enabled,
  84. };
  85. static struct clk * __init
  86. at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  87. const char *parent_name, u32 id)
  88. {
  89. struct clk_peripheral *periph;
  90. struct clk *clk = NULL;
  91. struct clk_init_data init;
  92. if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  93. return ERR_PTR(-EINVAL);
  94. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  95. if (!periph)
  96. return ERR_PTR(-ENOMEM);
  97. init.name = name;
  98. init.ops = &peripheral_ops;
  99. init.parent_names = (parent_name ? &parent_name : NULL);
  100. init.num_parents = (parent_name ? 1 : 0);
  101. init.flags = 0;
  102. periph->id = id;
  103. periph->hw.init = &init;
  104. periph->regmap = regmap;
  105. clk = clk_register(NULL, &periph->hw);
  106. if (IS_ERR(clk))
  107. kfree(periph);
  108. return clk;
  109. }
  110. static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  111. {
  112. struct clk_hw *parent;
  113. unsigned long parent_rate;
  114. int shift = 0;
  115. if (!periph->auto_div)
  116. return;
  117. if (periph->range.max) {
  118. parent = clk_hw_get_parent_by_index(&periph->hw, 0);
  119. parent_rate = clk_hw_get_rate(parent);
  120. if (!parent_rate)
  121. return;
  122. for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
  123. if (parent_rate >> shift <= periph->range.max)
  124. break;
  125. }
  126. }
  127. periph->auto_div = false;
  128. periph->div = shift;
  129. }
  130. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  131. {
  132. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  133. unsigned long flags;
  134. if (periph->id < PERIPHERAL_ID_MIN)
  135. return 0;
  136. spin_lock_irqsave(periph->lock, flags);
  137. regmap_write(periph->regmap, AT91_PMC_PCR,
  138. (periph->id & AT91_PMC_PCR_PID_MASK));
  139. regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  140. AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  141. AT91_PMC_PCR_EN,
  142. AT91_PMC_PCR_DIV(periph->div) |
  143. AT91_PMC_PCR_CMD |
  144. AT91_PMC_PCR_EN);
  145. spin_unlock_irqrestore(periph->lock, flags);
  146. return 0;
  147. }
  148. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  149. {
  150. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  151. unsigned long flags;
  152. if (periph->id < PERIPHERAL_ID_MIN)
  153. return;
  154. spin_lock_irqsave(periph->lock, flags);
  155. regmap_write(periph->regmap, AT91_PMC_PCR,
  156. (periph->id & AT91_PMC_PCR_PID_MASK));
  157. regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  158. AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  159. AT91_PMC_PCR_CMD);
  160. spin_unlock_irqrestore(periph->lock, flags);
  161. }
  162. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  163. {
  164. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  165. unsigned long flags;
  166. unsigned int status;
  167. if (periph->id < PERIPHERAL_ID_MIN)
  168. return 1;
  169. spin_lock_irqsave(periph->lock, flags);
  170. regmap_write(periph->regmap, AT91_PMC_PCR,
  171. (periph->id & AT91_PMC_PCR_PID_MASK));
  172. regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  173. spin_unlock_irqrestore(periph->lock, flags);
  174. return status & AT91_PMC_PCR_EN ? 1 : 0;
  175. }
  176. static unsigned long
  177. clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  178. unsigned long parent_rate)
  179. {
  180. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  181. unsigned long flags;
  182. unsigned int status;
  183. if (periph->id < PERIPHERAL_ID_MIN)
  184. return parent_rate;
  185. spin_lock_irqsave(periph->lock, flags);
  186. regmap_write(periph->regmap, AT91_PMC_PCR,
  187. (periph->id & AT91_PMC_PCR_PID_MASK));
  188. regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  189. spin_unlock_irqrestore(periph->lock, flags);
  190. if (status & AT91_PMC_PCR_EN) {
  191. periph->div = PERIPHERAL_RSHIFT(status);
  192. periph->auto_div = false;
  193. } else {
  194. clk_sam9x5_peripheral_autodiv(periph);
  195. }
  196. return parent_rate >> periph->div;
  197. }
  198. static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
  199. unsigned long rate,
  200. unsigned long *parent_rate)
  201. {
  202. int shift = 0;
  203. unsigned long best_rate;
  204. unsigned long best_diff;
  205. unsigned long cur_rate = *parent_rate;
  206. unsigned long cur_diff;
  207. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  208. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  209. return *parent_rate;
  210. if (periph->range.max) {
  211. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  212. cur_rate = *parent_rate >> shift;
  213. if (cur_rate <= periph->range.max)
  214. break;
  215. }
  216. }
  217. if (rate >= cur_rate)
  218. return cur_rate;
  219. best_diff = cur_rate - rate;
  220. best_rate = cur_rate;
  221. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  222. cur_rate = *parent_rate >> shift;
  223. if (cur_rate < rate)
  224. cur_diff = rate - cur_rate;
  225. else
  226. cur_diff = cur_rate - rate;
  227. if (cur_diff < best_diff) {
  228. best_diff = cur_diff;
  229. best_rate = cur_rate;
  230. }
  231. if (!best_diff || cur_rate < rate)
  232. break;
  233. }
  234. return best_rate;
  235. }
  236. static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
  237. unsigned long rate,
  238. unsigned long parent_rate)
  239. {
  240. int shift;
  241. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  242. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
  243. if (parent_rate == rate)
  244. return 0;
  245. else
  246. return -EINVAL;
  247. }
  248. if (periph->range.max && rate > periph->range.max)
  249. return -EINVAL;
  250. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  251. if (parent_rate >> shift == rate) {
  252. periph->auto_div = false;
  253. periph->div = shift;
  254. return 0;
  255. }
  256. }
  257. return -EINVAL;
  258. }
  259. static const struct clk_ops sam9x5_peripheral_ops = {
  260. .enable = clk_sam9x5_peripheral_enable,
  261. .disable = clk_sam9x5_peripheral_disable,
  262. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  263. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  264. .round_rate = clk_sam9x5_peripheral_round_rate,
  265. .set_rate = clk_sam9x5_peripheral_set_rate,
  266. };
  267. static struct clk * __init
  268. at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  269. const char *name, const char *parent_name,
  270. u32 id, const struct clk_range *range)
  271. {
  272. struct clk_sam9x5_peripheral *periph;
  273. struct clk *clk = NULL;
  274. struct clk_init_data init;
  275. if (!name || !parent_name)
  276. return ERR_PTR(-EINVAL);
  277. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  278. if (!periph)
  279. return ERR_PTR(-ENOMEM);
  280. init.name = name;
  281. init.ops = &sam9x5_peripheral_ops;
  282. init.parent_names = (parent_name ? &parent_name : NULL);
  283. init.num_parents = (parent_name ? 1 : 0);
  284. init.flags = 0;
  285. periph->id = id;
  286. periph->hw.init = &init;
  287. periph->div = 0;
  288. periph->regmap = regmap;
  289. periph->lock = lock;
  290. periph->auto_div = true;
  291. periph->range = *range;
  292. clk = clk_register(NULL, &periph->hw);
  293. if (IS_ERR(clk))
  294. kfree(periph);
  295. else
  296. clk_sam9x5_peripheral_autodiv(periph);
  297. return clk;
  298. }
  299. static void __init
  300. of_at91_clk_periph_setup(struct device_node *np, u8 type)
  301. {
  302. int num;
  303. u32 id;
  304. struct clk *clk;
  305. const char *parent_name;
  306. const char *name;
  307. struct device_node *periphclknp;
  308. struct regmap *regmap;
  309. parent_name = of_clk_get_parent_name(np, 0);
  310. if (!parent_name)
  311. return;
  312. num = of_get_child_count(np);
  313. if (!num || num > PERIPHERAL_MAX)
  314. return;
  315. regmap = syscon_node_to_regmap(of_get_parent(np));
  316. if (IS_ERR(regmap))
  317. return;
  318. for_each_child_of_node(np, periphclknp) {
  319. if (of_property_read_u32(periphclknp, "reg", &id))
  320. continue;
  321. if (id >= PERIPHERAL_MAX)
  322. continue;
  323. if (of_property_read_string(np, "clock-output-names", &name))
  324. name = periphclknp->name;
  325. if (type == PERIPHERAL_AT91RM9200) {
  326. clk = at91_clk_register_peripheral(regmap, name,
  327. parent_name, id);
  328. } else {
  329. struct clk_range range = CLK_RANGE(0, 0);
  330. of_at91_get_clk_range(periphclknp,
  331. "atmel,clk-output-range",
  332. &range);
  333. clk = at91_clk_register_sam9x5_peripheral(regmap,
  334. &pmc_pcr_lock,
  335. name,
  336. parent_name,
  337. id, &range);
  338. }
  339. if (IS_ERR(clk))
  340. continue;
  341. of_clk_add_provider(periphclknp, of_clk_src_simple_get, clk);
  342. }
  343. }
  344. static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  345. {
  346. of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  347. }
  348. CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  349. of_at91rm9200_clk_periph_setup);
  350. static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  351. {
  352. of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  353. }
  354. CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  355. of_at91sam9x5_clk_periph_setup);