clk-peripheral.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Boris BREZILLON <[email protected]>
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/clkdev.h>
  8. #include <linux/clk/at91_pmc.h>
  9. #include <linux/of.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/regmap.h>
  12. #include "pmc.h"
  13. DEFINE_SPINLOCK(pmc_pcr_lock);
  14. #define PERIPHERAL_ID_MIN 2
  15. #define PERIPHERAL_ID_MAX 31
  16. #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
  17. #define PERIPHERAL_MAX_SHIFT 3
  18. struct clk_peripheral {
  19. struct clk_hw hw;
  20. struct regmap *regmap;
  21. u32 id;
  22. };
  23. #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
  24. struct clk_sam9x5_peripheral {
  25. struct clk_hw hw;
  26. struct regmap *regmap;
  27. struct clk_range range;
  28. spinlock_t *lock;
  29. u32 id;
  30. u32 div;
  31. const struct clk_pcr_layout *layout;
  32. struct at91_clk_pms pms;
  33. bool auto_div;
  34. int chg_pid;
  35. };
  36. #define to_clk_sam9x5_peripheral(hw) \
  37. container_of(hw, struct clk_sam9x5_peripheral, hw)
  38. static int clk_peripheral_enable(struct clk_hw *hw)
  39. {
  40. struct clk_peripheral *periph = to_clk_peripheral(hw);
  41. int offset = AT91_PMC_PCER;
  42. u32 id = periph->id;
  43. if (id < PERIPHERAL_ID_MIN)
  44. return 0;
  45. if (id > PERIPHERAL_ID_MAX)
  46. offset = AT91_PMC_PCER1;
  47. regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  48. return 0;
  49. }
  50. static void clk_peripheral_disable(struct clk_hw *hw)
  51. {
  52. struct clk_peripheral *periph = to_clk_peripheral(hw);
  53. int offset = AT91_PMC_PCDR;
  54. u32 id = periph->id;
  55. if (id < PERIPHERAL_ID_MIN)
  56. return;
  57. if (id > PERIPHERAL_ID_MAX)
  58. offset = AT91_PMC_PCDR1;
  59. regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  60. }
  61. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  62. {
  63. struct clk_peripheral *periph = to_clk_peripheral(hw);
  64. int offset = AT91_PMC_PCSR;
  65. unsigned int status;
  66. u32 id = periph->id;
  67. if (id < PERIPHERAL_ID_MIN)
  68. return 1;
  69. if (id > PERIPHERAL_ID_MAX)
  70. offset = AT91_PMC_PCSR1;
  71. regmap_read(periph->regmap, offset, &status);
  72. return status & PERIPHERAL_MASK(id) ? 1 : 0;
  73. }
  74. static const struct clk_ops peripheral_ops = {
  75. .enable = clk_peripheral_enable,
  76. .disable = clk_peripheral_disable,
  77. .is_enabled = clk_peripheral_is_enabled,
  78. };
  79. struct clk_hw * __init
  80. at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  81. const char *parent_name, u32 id)
  82. {
  83. struct clk_peripheral *periph;
  84. struct clk_init_data init;
  85. struct clk_hw *hw;
  86. int ret;
  87. if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  88. return ERR_PTR(-EINVAL);
  89. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  90. if (!periph)
  91. return ERR_PTR(-ENOMEM);
  92. init.name = name;
  93. init.ops = &peripheral_ops;
  94. init.parent_names = &parent_name;
  95. init.num_parents = 1;
  96. init.flags = 0;
  97. periph->id = id;
  98. periph->hw.init = &init;
  99. periph->regmap = regmap;
  100. hw = &periph->hw;
  101. ret = clk_hw_register(NULL, &periph->hw);
  102. if (ret) {
  103. kfree(periph);
  104. hw = ERR_PTR(ret);
  105. }
  106. return hw;
  107. }
  108. static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  109. {
  110. struct clk_hw *parent;
  111. unsigned long parent_rate;
  112. int shift = 0;
  113. if (!periph->auto_div)
  114. return;
  115. if (periph->range.max) {
  116. parent = clk_hw_get_parent_by_index(&periph->hw, 0);
  117. parent_rate = clk_hw_get_rate(parent);
  118. if (!parent_rate)
  119. return;
  120. for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
  121. if (parent_rate >> shift <= periph->range.max)
  122. break;
  123. }
  124. }
  125. periph->auto_div = false;
  126. periph->div = shift;
  127. }
  128. static int clk_sam9x5_peripheral_set(struct clk_sam9x5_peripheral *periph,
  129. unsigned int status)
  130. {
  131. unsigned long flags;
  132. unsigned int enable = status ? AT91_PMC_PCR_EN : 0;
  133. if (periph->id < PERIPHERAL_ID_MIN)
  134. return 0;
  135. spin_lock_irqsave(periph->lock, flags);
  136. regmap_write(periph->regmap, periph->layout->offset,
  137. (periph->id & periph->layout->pid_mask));
  138. regmap_update_bits(periph->regmap, periph->layout->offset,
  139. periph->layout->div_mask | periph->layout->cmd |
  140. enable,
  141. field_prep(periph->layout->div_mask, periph->div) |
  142. periph->layout->cmd | enable);
  143. spin_unlock_irqrestore(periph->lock, flags);
  144. return 0;
  145. }
  146. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  147. {
  148. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  149. return clk_sam9x5_peripheral_set(periph, 1);
  150. }
  151. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  152. {
  153. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  154. unsigned long flags;
  155. if (periph->id < PERIPHERAL_ID_MIN)
  156. return;
  157. spin_lock_irqsave(periph->lock, flags);
  158. regmap_write(periph->regmap, periph->layout->offset,
  159. (periph->id & periph->layout->pid_mask));
  160. regmap_update_bits(periph->regmap, periph->layout->offset,
  161. AT91_PMC_PCR_EN | periph->layout->cmd,
  162. periph->layout->cmd);
  163. spin_unlock_irqrestore(periph->lock, flags);
  164. }
  165. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  166. {
  167. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  168. unsigned long flags;
  169. unsigned int status;
  170. if (periph->id < PERIPHERAL_ID_MIN)
  171. return 1;
  172. spin_lock_irqsave(periph->lock, flags);
  173. regmap_write(periph->regmap, periph->layout->offset,
  174. (periph->id & periph->layout->pid_mask));
  175. regmap_read(periph->regmap, periph->layout->offset, &status);
  176. spin_unlock_irqrestore(periph->lock, flags);
  177. return !!(status & AT91_PMC_PCR_EN);
  178. }
  179. static unsigned long
  180. clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  181. unsigned long parent_rate)
  182. {
  183. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  184. unsigned long flags;
  185. unsigned int status;
  186. if (periph->id < PERIPHERAL_ID_MIN)
  187. return parent_rate;
  188. spin_lock_irqsave(periph->lock, flags);
  189. regmap_write(periph->regmap, periph->layout->offset,
  190. (periph->id & periph->layout->pid_mask));
  191. regmap_read(periph->regmap, periph->layout->offset, &status);
  192. spin_unlock_irqrestore(periph->lock, flags);
  193. if (status & AT91_PMC_PCR_EN) {
  194. periph->div = field_get(periph->layout->div_mask, status);
  195. periph->auto_div = false;
  196. } else {
  197. clk_sam9x5_peripheral_autodiv(periph);
  198. }
  199. return parent_rate >> periph->div;
  200. }
  201. static void clk_sam9x5_peripheral_best_diff(struct clk_rate_request *req,
  202. struct clk_hw *parent,
  203. unsigned long parent_rate,
  204. u32 shift, long *best_diff,
  205. long *best_rate)
  206. {
  207. unsigned long tmp_rate = parent_rate >> shift;
  208. unsigned long tmp_diff = abs(req->rate - tmp_rate);
  209. if (*best_diff < 0 || *best_diff >= tmp_diff) {
  210. *best_rate = tmp_rate;
  211. *best_diff = tmp_diff;
  212. req->best_parent_rate = parent_rate;
  213. req->best_parent_hw = parent;
  214. }
  215. }
  216. static int clk_sam9x5_peripheral_determine_rate(struct clk_hw *hw,
  217. struct clk_rate_request *req)
  218. {
  219. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  220. struct clk_hw *parent = clk_hw_get_parent(hw);
  221. unsigned long parent_rate = clk_hw_get_rate(parent);
  222. unsigned long tmp_rate;
  223. long best_rate = LONG_MIN;
  224. long best_diff = LONG_MIN;
  225. u32 shift;
  226. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  227. return parent_rate;
  228. /* Fist step: check the available dividers. */
  229. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  230. tmp_rate = parent_rate >> shift;
  231. if (periph->range.max && tmp_rate > periph->range.max)
  232. continue;
  233. clk_sam9x5_peripheral_best_diff(req, parent, parent_rate,
  234. shift, &best_diff, &best_rate);
  235. if (!best_diff || best_rate <= req->rate)
  236. break;
  237. }
  238. if (periph->chg_pid < 0)
  239. goto end;
  240. /* Step two: try to request rate from parent. */
  241. parent = clk_hw_get_parent_by_index(hw, periph->chg_pid);
  242. if (!parent)
  243. goto end;
  244. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  245. struct clk_rate_request req_parent;
  246. clk_hw_forward_rate_request(hw, req, parent, &req_parent, req->rate << shift);
  247. if (__clk_determine_rate(parent, &req_parent))
  248. continue;
  249. clk_sam9x5_peripheral_best_diff(req, parent, req_parent.rate,
  250. shift, &best_diff, &best_rate);
  251. if (!best_diff)
  252. break;
  253. }
  254. end:
  255. if (best_rate < 0 ||
  256. (periph->range.max && best_rate > periph->range.max))
  257. return -EINVAL;
  258. pr_debug("PCK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  259. __func__, best_rate,
  260. __clk_get_name((req->best_parent_hw)->clk),
  261. req->best_parent_rate);
  262. req->rate = best_rate;
  263. return 0;
  264. }
  265. static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
  266. unsigned long rate,
  267. unsigned long *parent_rate)
  268. {
  269. int shift = 0;
  270. unsigned long best_rate;
  271. unsigned long best_diff;
  272. unsigned long cur_rate = *parent_rate;
  273. unsigned long cur_diff;
  274. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  275. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  276. return *parent_rate;
  277. if (periph->range.max) {
  278. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  279. cur_rate = *parent_rate >> shift;
  280. if (cur_rate <= periph->range.max)
  281. break;
  282. }
  283. }
  284. if (rate >= cur_rate)
  285. return cur_rate;
  286. best_diff = cur_rate - rate;
  287. best_rate = cur_rate;
  288. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  289. cur_rate = *parent_rate >> shift;
  290. if (cur_rate < rate)
  291. cur_diff = rate - cur_rate;
  292. else
  293. cur_diff = cur_rate - rate;
  294. if (cur_diff < best_diff) {
  295. best_diff = cur_diff;
  296. best_rate = cur_rate;
  297. }
  298. if (!best_diff || cur_rate < rate)
  299. break;
  300. }
  301. return best_rate;
  302. }
  303. static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
  304. unsigned long rate,
  305. unsigned long parent_rate)
  306. {
  307. int shift;
  308. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  309. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
  310. if (parent_rate == rate)
  311. return 0;
  312. else
  313. return -EINVAL;
  314. }
  315. if (periph->range.max && rate > periph->range.max)
  316. return -EINVAL;
  317. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  318. if (parent_rate >> shift == rate) {
  319. periph->auto_div = false;
  320. periph->div = shift;
  321. return 0;
  322. }
  323. }
  324. return -EINVAL;
  325. }
  326. static int clk_sam9x5_peripheral_save_context(struct clk_hw *hw)
  327. {
  328. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  329. periph->pms.status = clk_sam9x5_peripheral_is_enabled(hw);
  330. return 0;
  331. }
  332. static void clk_sam9x5_peripheral_restore_context(struct clk_hw *hw)
  333. {
  334. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  335. if (periph->pms.status)
  336. clk_sam9x5_peripheral_set(periph, periph->pms.status);
  337. }
  338. static const struct clk_ops sam9x5_peripheral_ops = {
  339. .enable = clk_sam9x5_peripheral_enable,
  340. .disable = clk_sam9x5_peripheral_disable,
  341. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  342. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  343. .round_rate = clk_sam9x5_peripheral_round_rate,
  344. .set_rate = clk_sam9x5_peripheral_set_rate,
  345. .save_context = clk_sam9x5_peripheral_save_context,
  346. .restore_context = clk_sam9x5_peripheral_restore_context,
  347. };
  348. static const struct clk_ops sam9x5_peripheral_chg_ops = {
  349. .enable = clk_sam9x5_peripheral_enable,
  350. .disable = clk_sam9x5_peripheral_disable,
  351. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  352. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  353. .determine_rate = clk_sam9x5_peripheral_determine_rate,
  354. .set_rate = clk_sam9x5_peripheral_set_rate,
  355. .save_context = clk_sam9x5_peripheral_save_context,
  356. .restore_context = clk_sam9x5_peripheral_restore_context,
  357. };
  358. struct clk_hw * __init
  359. at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  360. const struct clk_pcr_layout *layout,
  361. const char *name, const char *parent_name,
  362. u32 id, const struct clk_range *range,
  363. int chg_pid)
  364. {
  365. struct clk_sam9x5_peripheral *periph;
  366. struct clk_init_data init;
  367. struct clk_hw *hw;
  368. int ret;
  369. if (!name || !parent_name)
  370. return ERR_PTR(-EINVAL);
  371. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  372. if (!periph)
  373. return ERR_PTR(-ENOMEM);
  374. init.name = name;
  375. init.parent_names = &parent_name;
  376. init.num_parents = 1;
  377. if (chg_pid < 0) {
  378. init.flags = 0;
  379. init.ops = &sam9x5_peripheral_ops;
  380. } else {
  381. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  382. CLK_SET_RATE_PARENT;
  383. init.ops = &sam9x5_peripheral_chg_ops;
  384. }
  385. periph->id = id;
  386. periph->hw.init = &init;
  387. periph->div = 0;
  388. periph->regmap = regmap;
  389. periph->lock = lock;
  390. if (layout->div_mask)
  391. periph->auto_div = true;
  392. periph->layout = layout;
  393. periph->range = *range;
  394. periph->chg_pid = chg_pid;
  395. hw = &periph->hw;
  396. ret = clk_hw_register(NULL, &periph->hw);
  397. if (ret) {
  398. kfree(periph);
  399. hw = ERR_PTR(ret);
  400. } else {
  401. clk_sam9x5_peripheral_autodiv(periph);
  402. }
  403. return hw;
  404. }