clk-peripheral.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_ID_MIN 2
  19. #define PERIPHERAL_ID_MAX 31
  20. #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
  21. #define PERIPHERAL_RSHIFT_MASK 0x3
  22. #define PERIPHERAL_RSHIFT(val) (((val) >> 16) & PERIPHERAL_RSHIFT_MASK)
  23. #define PERIPHERAL_MAX_SHIFT 3
  24. struct clk_peripheral {
  25. struct clk_hw hw;
  26. struct regmap *regmap;
  27. u32 id;
  28. };
  29. #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
  30. struct clk_sam9x5_peripheral {
  31. struct clk_hw hw;
  32. struct regmap *regmap;
  33. struct clk_range range;
  34. spinlock_t *lock;
  35. u32 id;
  36. u32 div;
  37. bool auto_div;
  38. };
  39. #define to_clk_sam9x5_peripheral(hw) \
  40. container_of(hw, struct clk_sam9x5_peripheral, hw)
  41. static int clk_peripheral_enable(struct clk_hw *hw)
  42. {
  43. struct clk_peripheral *periph = to_clk_peripheral(hw);
  44. int offset = AT91_PMC_PCER;
  45. u32 id = periph->id;
  46. if (id < PERIPHERAL_ID_MIN)
  47. return 0;
  48. if (id > PERIPHERAL_ID_MAX)
  49. offset = AT91_PMC_PCER1;
  50. regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  51. return 0;
  52. }
  53. static void clk_peripheral_disable(struct clk_hw *hw)
  54. {
  55. struct clk_peripheral *periph = to_clk_peripheral(hw);
  56. int offset = AT91_PMC_PCDR;
  57. u32 id = periph->id;
  58. if (id < PERIPHERAL_ID_MIN)
  59. return;
  60. if (id > PERIPHERAL_ID_MAX)
  61. offset = AT91_PMC_PCDR1;
  62. regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  63. }
  64. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  65. {
  66. struct clk_peripheral *periph = to_clk_peripheral(hw);
  67. int offset = AT91_PMC_PCSR;
  68. unsigned int status;
  69. u32 id = periph->id;
  70. if (id < PERIPHERAL_ID_MIN)
  71. return 1;
  72. if (id > PERIPHERAL_ID_MAX)
  73. offset = AT91_PMC_PCSR1;
  74. regmap_read(periph->regmap, offset, &status);
  75. return status & PERIPHERAL_MASK(id) ? 1 : 0;
  76. }
  77. static const struct clk_ops peripheral_ops = {
  78. .enable = clk_peripheral_enable,
  79. .disable = clk_peripheral_disable,
  80. .is_enabled = clk_peripheral_is_enabled,
  81. };
  82. struct clk_hw * __init
  83. at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  84. const char *parent_name, u32 id)
  85. {
  86. struct clk_peripheral *periph;
  87. struct clk_init_data init;
  88. struct clk_hw *hw;
  89. int ret;
  90. if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  91. return ERR_PTR(-EINVAL);
  92. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  93. if (!periph)
  94. return ERR_PTR(-ENOMEM);
  95. init.name = name;
  96. init.ops = &peripheral_ops;
  97. init.parent_names = (parent_name ? &parent_name : NULL);
  98. init.num_parents = (parent_name ? 1 : 0);
  99. init.flags = 0;
  100. periph->id = id;
  101. periph->hw.init = &init;
  102. periph->regmap = regmap;
  103. hw = &periph->hw;
  104. ret = clk_hw_register(NULL, &periph->hw);
  105. if (ret) {
  106. kfree(periph);
  107. hw = ERR_PTR(ret);
  108. }
  109. return hw;
  110. }
  111. static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  112. {
  113. struct clk_hw *parent;
  114. unsigned long parent_rate;
  115. int shift = 0;
  116. if (!periph->auto_div)
  117. return;
  118. if (periph->range.max) {
  119. parent = clk_hw_get_parent_by_index(&periph->hw, 0);
  120. parent_rate = clk_hw_get_rate(parent);
  121. if (!parent_rate)
  122. return;
  123. for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
  124. if (parent_rate >> shift <= periph->range.max)
  125. break;
  126. }
  127. }
  128. periph->auto_div = false;
  129. periph->div = shift;
  130. }
  131. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  132. {
  133. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  134. unsigned long flags;
  135. if (periph->id < PERIPHERAL_ID_MIN)
  136. return 0;
  137. spin_lock_irqsave(periph->lock, flags);
  138. regmap_write(periph->regmap, AT91_PMC_PCR,
  139. (periph->id & AT91_PMC_PCR_PID_MASK));
  140. regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  141. AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  142. AT91_PMC_PCR_EN,
  143. AT91_PMC_PCR_DIV(periph->div) |
  144. AT91_PMC_PCR_CMD |
  145. AT91_PMC_PCR_EN);
  146. spin_unlock_irqrestore(periph->lock, flags);
  147. return 0;
  148. }
  149. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  150. {
  151. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  152. unsigned long flags;
  153. if (periph->id < PERIPHERAL_ID_MIN)
  154. return;
  155. spin_lock_irqsave(periph->lock, flags);
  156. regmap_write(periph->regmap, AT91_PMC_PCR,
  157. (periph->id & AT91_PMC_PCR_PID_MASK));
  158. regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  159. AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  160. AT91_PMC_PCR_CMD);
  161. spin_unlock_irqrestore(periph->lock, flags);
  162. }
  163. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  164. {
  165. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  166. unsigned long flags;
  167. unsigned int status;
  168. if (periph->id < PERIPHERAL_ID_MIN)
  169. return 1;
  170. spin_lock_irqsave(periph->lock, flags);
  171. regmap_write(periph->regmap, AT91_PMC_PCR,
  172. (periph->id & AT91_PMC_PCR_PID_MASK));
  173. regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  174. spin_unlock_irqrestore(periph->lock, flags);
  175. return status & AT91_PMC_PCR_EN ? 1 : 0;
  176. }
  177. static unsigned long
  178. clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  179. unsigned long parent_rate)
  180. {
  181. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  182. unsigned long flags;
  183. unsigned int status;
  184. if (periph->id < PERIPHERAL_ID_MIN)
  185. return parent_rate;
  186. spin_lock_irqsave(periph->lock, flags);
  187. regmap_write(periph->regmap, AT91_PMC_PCR,
  188. (periph->id & AT91_PMC_PCR_PID_MASK));
  189. regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  190. spin_unlock_irqrestore(periph->lock, flags);
  191. if (status & AT91_PMC_PCR_EN) {
  192. periph->div = PERIPHERAL_RSHIFT(status);
  193. periph->auto_div = false;
  194. } else {
  195. clk_sam9x5_peripheral_autodiv(periph);
  196. }
  197. return parent_rate >> periph->div;
  198. }
  199. static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
  200. unsigned long rate,
  201. unsigned long *parent_rate)
  202. {
  203. int shift = 0;
  204. unsigned long best_rate;
  205. unsigned long best_diff;
  206. unsigned long cur_rate = *parent_rate;
  207. unsigned long cur_diff;
  208. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  209. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  210. return *parent_rate;
  211. if (periph->range.max) {
  212. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  213. cur_rate = *parent_rate >> shift;
  214. if (cur_rate <= periph->range.max)
  215. break;
  216. }
  217. }
  218. if (rate >= cur_rate)
  219. return cur_rate;
  220. best_diff = cur_rate - rate;
  221. best_rate = cur_rate;
  222. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  223. cur_rate = *parent_rate >> shift;
  224. if (cur_rate < rate)
  225. cur_diff = rate - cur_rate;
  226. else
  227. cur_diff = cur_rate - rate;
  228. if (cur_diff < best_diff) {
  229. best_diff = cur_diff;
  230. best_rate = cur_rate;
  231. }
  232. if (!best_diff || cur_rate < rate)
  233. break;
  234. }
  235. return best_rate;
  236. }
  237. static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
  238. unsigned long rate,
  239. unsigned long parent_rate)
  240. {
  241. int shift;
  242. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  243. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
  244. if (parent_rate == rate)
  245. return 0;
  246. else
  247. return -EINVAL;
  248. }
  249. if (periph->range.max && rate > periph->range.max)
  250. return -EINVAL;
  251. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  252. if (parent_rate >> shift == rate) {
  253. periph->auto_div = false;
  254. periph->div = shift;
  255. return 0;
  256. }
  257. }
  258. return -EINVAL;
  259. }
  260. static const struct clk_ops sam9x5_peripheral_ops = {
  261. .enable = clk_sam9x5_peripheral_enable,
  262. .disable = clk_sam9x5_peripheral_disable,
  263. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  264. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  265. .round_rate = clk_sam9x5_peripheral_round_rate,
  266. .set_rate = clk_sam9x5_peripheral_set_rate,
  267. };
  268. struct clk_hw * __init
  269. at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  270. const char *name, const char *parent_name,
  271. u32 id, const struct clk_range *range)
  272. {
  273. struct clk_sam9x5_peripheral *periph;
  274. struct clk_init_data init;
  275. struct clk_hw *hw;
  276. int ret;
  277. if (!name || !parent_name)
  278. return ERR_PTR(-EINVAL);
  279. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  280. if (!periph)
  281. return ERR_PTR(-ENOMEM);
  282. init.name = name;
  283. init.ops = &sam9x5_peripheral_ops;
  284. init.parent_names = (parent_name ? &parent_name : NULL);
  285. init.num_parents = (parent_name ? 1 : 0);
  286. init.flags = 0;
  287. periph->id = id;
  288. periph->hw.init = &init;
  289. periph->div = 0;
  290. periph->regmap = regmap;
  291. periph->lock = lock;
  292. periph->auto_div = true;
  293. periph->range = *range;
  294. hw = &periph->hw;
  295. ret = clk_hw_register(NULL, &periph->hw);
  296. if (ret) {
  297. kfree(periph);
  298. hw = ERR_PTR(ret);
  299. } else {
  300. clk_sam9x5_peripheral_autodiv(periph);
  301. pmc_register_id(id);
  302. }
  303. return hw;
  304. }