clk-peripheral.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_hw * __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_init_data init;
  91. struct clk_hw *hw;
  92. int ret;
  93. if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  94. return ERR_PTR(-EINVAL);
  95. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  96. if (!periph)
  97. return ERR_PTR(-ENOMEM);
  98. init.name = name;
  99. init.ops = &peripheral_ops;
  100. init.parent_names = (parent_name ? &parent_name : NULL);
  101. init.num_parents = (parent_name ? 1 : 0);
  102. init.flags = 0;
  103. periph->id = id;
  104. periph->hw.init = &init;
  105. periph->regmap = regmap;
  106. hw = &periph->hw;
  107. ret = clk_hw_register(NULL, &periph->hw);
  108. if (ret) {
  109. kfree(periph);
  110. hw = ERR_PTR(ret);
  111. }
  112. return hw;
  113. }
  114. static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  115. {
  116. struct clk_hw *parent;
  117. unsigned long parent_rate;
  118. int shift = 0;
  119. if (!periph->auto_div)
  120. return;
  121. if (periph->range.max) {
  122. parent = clk_hw_get_parent_by_index(&periph->hw, 0);
  123. parent_rate = clk_hw_get_rate(parent);
  124. if (!parent_rate)
  125. return;
  126. for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
  127. if (parent_rate >> shift <= periph->range.max)
  128. break;
  129. }
  130. }
  131. periph->auto_div = false;
  132. periph->div = shift;
  133. }
  134. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  135. {
  136. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  137. unsigned long flags;
  138. if (periph->id < PERIPHERAL_ID_MIN)
  139. return 0;
  140. spin_lock_irqsave(periph->lock, flags);
  141. regmap_write(periph->regmap, AT91_PMC_PCR,
  142. (periph->id & AT91_PMC_PCR_PID_MASK));
  143. regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  144. AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  145. AT91_PMC_PCR_EN,
  146. AT91_PMC_PCR_DIV(periph->div) |
  147. AT91_PMC_PCR_CMD |
  148. AT91_PMC_PCR_EN);
  149. spin_unlock_irqrestore(periph->lock, flags);
  150. return 0;
  151. }
  152. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  153. {
  154. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  155. unsigned long flags;
  156. if (periph->id < PERIPHERAL_ID_MIN)
  157. return;
  158. spin_lock_irqsave(periph->lock, flags);
  159. regmap_write(periph->regmap, AT91_PMC_PCR,
  160. (periph->id & AT91_PMC_PCR_PID_MASK));
  161. regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  162. AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  163. AT91_PMC_PCR_CMD);
  164. spin_unlock_irqrestore(periph->lock, flags);
  165. }
  166. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  167. {
  168. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  169. unsigned long flags;
  170. unsigned int status;
  171. if (periph->id < PERIPHERAL_ID_MIN)
  172. return 1;
  173. spin_lock_irqsave(periph->lock, flags);
  174. regmap_write(periph->regmap, AT91_PMC_PCR,
  175. (periph->id & AT91_PMC_PCR_PID_MASK));
  176. regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  177. spin_unlock_irqrestore(periph->lock, flags);
  178. return status & AT91_PMC_PCR_EN ? 1 : 0;
  179. }
  180. static unsigned long
  181. clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  182. unsigned long parent_rate)
  183. {
  184. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  185. unsigned long flags;
  186. unsigned int status;
  187. if (periph->id < PERIPHERAL_ID_MIN)
  188. return parent_rate;
  189. spin_lock_irqsave(periph->lock, flags);
  190. regmap_write(periph->regmap, AT91_PMC_PCR,
  191. (periph->id & AT91_PMC_PCR_PID_MASK));
  192. regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  193. spin_unlock_irqrestore(periph->lock, flags);
  194. if (status & AT91_PMC_PCR_EN) {
  195. periph->div = PERIPHERAL_RSHIFT(status);
  196. periph->auto_div = false;
  197. } else {
  198. clk_sam9x5_peripheral_autodiv(periph);
  199. }
  200. return parent_rate >> periph->div;
  201. }
  202. static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
  203. unsigned long rate,
  204. unsigned long *parent_rate)
  205. {
  206. int shift = 0;
  207. unsigned long best_rate;
  208. unsigned long best_diff;
  209. unsigned long cur_rate = *parent_rate;
  210. unsigned long cur_diff;
  211. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  212. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  213. return *parent_rate;
  214. if (periph->range.max) {
  215. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  216. cur_rate = *parent_rate >> shift;
  217. if (cur_rate <= periph->range.max)
  218. break;
  219. }
  220. }
  221. if (rate >= cur_rate)
  222. return cur_rate;
  223. best_diff = cur_rate - rate;
  224. best_rate = cur_rate;
  225. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  226. cur_rate = *parent_rate >> shift;
  227. if (cur_rate < rate)
  228. cur_diff = rate - cur_rate;
  229. else
  230. cur_diff = cur_rate - rate;
  231. if (cur_diff < best_diff) {
  232. best_diff = cur_diff;
  233. best_rate = cur_rate;
  234. }
  235. if (!best_diff || cur_rate < rate)
  236. break;
  237. }
  238. return best_rate;
  239. }
  240. static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
  241. unsigned long rate,
  242. unsigned long parent_rate)
  243. {
  244. int shift;
  245. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  246. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
  247. if (parent_rate == rate)
  248. return 0;
  249. else
  250. return -EINVAL;
  251. }
  252. if (periph->range.max && rate > periph->range.max)
  253. return -EINVAL;
  254. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  255. if (parent_rate >> shift == rate) {
  256. periph->auto_div = false;
  257. periph->div = shift;
  258. return 0;
  259. }
  260. }
  261. return -EINVAL;
  262. }
  263. static const struct clk_ops sam9x5_peripheral_ops = {
  264. .enable = clk_sam9x5_peripheral_enable,
  265. .disable = clk_sam9x5_peripheral_disable,
  266. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  267. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  268. .round_rate = clk_sam9x5_peripheral_round_rate,
  269. .set_rate = clk_sam9x5_peripheral_set_rate,
  270. };
  271. static struct clk_hw * __init
  272. at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  273. const char *name, const char *parent_name,
  274. u32 id, const struct clk_range *range)
  275. {
  276. struct clk_sam9x5_peripheral *periph;
  277. struct clk_init_data init;
  278. struct clk_hw *hw;
  279. int ret;
  280. if (!name || !parent_name)
  281. return ERR_PTR(-EINVAL);
  282. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  283. if (!periph)
  284. return ERR_PTR(-ENOMEM);
  285. init.name = name;
  286. init.ops = &sam9x5_peripheral_ops;
  287. init.parent_names = (parent_name ? &parent_name : NULL);
  288. init.num_parents = (parent_name ? 1 : 0);
  289. init.flags = 0;
  290. periph->id = id;
  291. periph->hw.init = &init;
  292. periph->div = 0;
  293. periph->regmap = regmap;
  294. periph->lock = lock;
  295. periph->auto_div = true;
  296. periph->range = *range;
  297. hw = &periph->hw;
  298. ret = clk_hw_register(NULL, &periph->hw);
  299. if (ret) {
  300. kfree(periph);
  301. hw = ERR_PTR(ret);
  302. } else
  303. clk_sam9x5_peripheral_autodiv(periph);
  304. return hw;
  305. }
  306. static void __init
  307. of_at91_clk_periph_setup(struct device_node *np, u8 type)
  308. {
  309. int num;
  310. u32 id;
  311. struct clk_hw *hw;
  312. const char *parent_name;
  313. const char *name;
  314. struct device_node *periphclknp;
  315. struct regmap *regmap;
  316. parent_name = of_clk_get_parent_name(np, 0);
  317. if (!parent_name)
  318. return;
  319. num = of_get_child_count(np);
  320. if (!num || num > PERIPHERAL_MAX)
  321. return;
  322. regmap = syscon_node_to_regmap(of_get_parent(np));
  323. if (IS_ERR(regmap))
  324. return;
  325. for_each_child_of_node(np, periphclknp) {
  326. if (of_property_read_u32(periphclknp, "reg", &id))
  327. continue;
  328. if (id >= PERIPHERAL_MAX)
  329. continue;
  330. if (of_property_read_string(np, "clock-output-names", &name))
  331. name = periphclknp->name;
  332. if (type == PERIPHERAL_AT91RM9200) {
  333. hw = at91_clk_register_peripheral(regmap, name,
  334. parent_name, id);
  335. } else {
  336. struct clk_range range = CLK_RANGE(0, 0);
  337. of_at91_get_clk_range(periphclknp,
  338. "atmel,clk-output-range",
  339. &range);
  340. hw = at91_clk_register_sam9x5_peripheral(regmap,
  341. &pmc_pcr_lock,
  342. name,
  343. parent_name,
  344. id, &range);
  345. }
  346. if (IS_ERR(hw))
  347. continue;
  348. of_clk_add_hw_provider(periphclknp, of_clk_hw_simple_get, hw);
  349. }
  350. }
  351. static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  352. {
  353. of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  354. }
  355. CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  356. of_at91rm9200_clk_periph_setup);
  357. static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  358. {
  359. of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  360. }
  361. CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  362. of_at91sam9x5_clk_periph_setup);