clk-peripheral.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  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, struct clk_hw *parent_hw,
  82. u32 id)
  83. {
  84. struct clk_peripheral *periph;
  85. struct clk_init_data init = {};
  86. struct clk_hw *hw;
  87. int ret;
  88. if (!name || !(parent_name || parent_hw) || id > PERIPHERAL_ID_MAX)
  89. return ERR_PTR(-EINVAL);
  90. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  91. if (!periph)
  92. return ERR_PTR(-ENOMEM);
  93. init.name = name;
  94. init.ops = &peripheral_ops;
  95. if (parent_hw)
  96. init.parent_hws = (const struct clk_hw **)&parent_hw;
  97. else
  98. init.parent_names = &parent_name;
  99. init.num_parents = 1;
  100. init.flags = 0;
  101. periph->id = id;
  102. periph->hw.init = &init;
  103. periph->regmap = regmap;
  104. hw = &periph->hw;
  105. ret = clk_hw_register(NULL, &periph->hw);
  106. if (ret) {
  107. kfree(periph);
  108. hw = ERR_PTR(ret);
  109. }
  110. return hw;
  111. }
  112. static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  113. {
  114. struct clk_hw *parent;
  115. unsigned long parent_rate;
  116. int shift = 0;
  117. if (!periph->auto_div)
  118. return;
  119. if (periph->range.max) {
  120. parent = clk_hw_get_parent_by_index(&periph->hw, 0);
  121. parent_rate = clk_hw_get_rate(parent);
  122. if (!parent_rate)
  123. return;
  124. for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
  125. if (parent_rate >> shift <= periph->range.max)
  126. break;
  127. }
  128. }
  129. periph->auto_div = false;
  130. periph->div = shift;
  131. }
  132. static int clk_sam9x5_peripheral_set(struct clk_sam9x5_peripheral *periph,
  133. unsigned int status)
  134. {
  135. unsigned long flags;
  136. unsigned int enable = status ? AT91_PMC_PCR_EN : 0;
  137. if (periph->id < PERIPHERAL_ID_MIN)
  138. return 0;
  139. spin_lock_irqsave(periph->lock, flags);
  140. regmap_write(periph->regmap, periph->layout->offset,
  141. (periph->id & periph->layout->pid_mask));
  142. regmap_update_bits(periph->regmap, periph->layout->offset,
  143. periph->layout->div_mask | periph->layout->cmd |
  144. enable,
  145. field_prep(periph->layout->div_mask, periph->div) |
  146. periph->layout->cmd | enable);
  147. spin_unlock_irqrestore(periph->lock, flags);
  148. return 0;
  149. }
  150. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  151. {
  152. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  153. return clk_sam9x5_peripheral_set(periph, 1);
  154. }
  155. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  156. {
  157. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  158. unsigned long flags;
  159. if (periph->id < PERIPHERAL_ID_MIN)
  160. return;
  161. spin_lock_irqsave(periph->lock, flags);
  162. regmap_write(periph->regmap, periph->layout->offset,
  163. (periph->id & periph->layout->pid_mask));
  164. regmap_update_bits(periph->regmap, periph->layout->offset,
  165. AT91_PMC_PCR_EN | periph->layout->cmd,
  166. periph->layout->cmd);
  167. spin_unlock_irqrestore(periph->lock, flags);
  168. }
  169. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  170. {
  171. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  172. unsigned long flags;
  173. unsigned int status;
  174. if (periph->id < PERIPHERAL_ID_MIN)
  175. return 1;
  176. spin_lock_irqsave(periph->lock, flags);
  177. regmap_write(periph->regmap, periph->layout->offset,
  178. (periph->id & periph->layout->pid_mask));
  179. regmap_read(periph->regmap, periph->layout->offset, &status);
  180. spin_unlock_irqrestore(periph->lock, flags);
  181. return !!(status & AT91_PMC_PCR_EN);
  182. }
  183. static unsigned long
  184. clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  185. unsigned long parent_rate)
  186. {
  187. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  188. unsigned long flags;
  189. unsigned int status;
  190. if (periph->id < PERIPHERAL_ID_MIN)
  191. return parent_rate;
  192. spin_lock_irqsave(periph->lock, flags);
  193. regmap_write(periph->regmap, periph->layout->offset,
  194. (periph->id & periph->layout->pid_mask));
  195. regmap_read(periph->regmap, periph->layout->offset, &status);
  196. spin_unlock_irqrestore(periph->lock, flags);
  197. if (status & AT91_PMC_PCR_EN) {
  198. periph->div = field_get(periph->layout->div_mask, status);
  199. periph->auto_div = false;
  200. } else {
  201. clk_sam9x5_peripheral_autodiv(periph);
  202. }
  203. return parent_rate >> periph->div;
  204. }
  205. static void clk_sam9x5_peripheral_best_diff(struct clk_rate_request *req,
  206. struct clk_hw *parent,
  207. unsigned long parent_rate,
  208. u32 shift, long *best_diff,
  209. long *best_rate)
  210. {
  211. unsigned long tmp_rate = parent_rate >> shift;
  212. unsigned long tmp_diff = abs(req->rate - tmp_rate);
  213. if (*best_diff < 0 || *best_diff >= tmp_diff) {
  214. *best_rate = tmp_rate;
  215. *best_diff = tmp_diff;
  216. req->best_parent_rate = parent_rate;
  217. req->best_parent_hw = parent;
  218. }
  219. }
  220. static int clk_sam9x5_peripheral_determine_rate(struct clk_hw *hw,
  221. struct clk_rate_request *req)
  222. {
  223. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  224. struct clk_hw *parent = clk_hw_get_parent(hw);
  225. unsigned long parent_rate = clk_hw_get_rate(parent);
  226. unsigned long tmp_rate;
  227. long best_rate = LONG_MIN;
  228. long best_diff = LONG_MIN;
  229. u32 shift;
  230. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  231. return parent_rate;
  232. /* Fist step: check the available dividers. */
  233. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  234. tmp_rate = parent_rate >> shift;
  235. if (periph->range.max && tmp_rate > periph->range.max)
  236. continue;
  237. clk_sam9x5_peripheral_best_diff(req, parent, parent_rate,
  238. shift, &best_diff, &best_rate);
  239. if (!best_diff || best_rate <= req->rate)
  240. break;
  241. }
  242. if (periph->chg_pid < 0)
  243. goto end;
  244. /* Step two: try to request rate from parent. */
  245. parent = clk_hw_get_parent_by_index(hw, periph->chg_pid);
  246. if (!parent)
  247. goto end;
  248. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  249. struct clk_rate_request req_parent;
  250. clk_hw_forward_rate_request(hw, req, parent, &req_parent, req->rate << shift);
  251. if (__clk_determine_rate(parent, &req_parent))
  252. continue;
  253. clk_sam9x5_peripheral_best_diff(req, parent, req_parent.rate,
  254. shift, &best_diff, &best_rate);
  255. if (!best_diff)
  256. break;
  257. }
  258. end:
  259. if (best_rate < 0 ||
  260. (periph->range.max && best_rate > periph->range.max))
  261. return -EINVAL;
  262. pr_debug("PCK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  263. __func__, best_rate,
  264. __clk_get_name((req->best_parent_hw)->clk),
  265. req->best_parent_rate);
  266. req->rate = best_rate;
  267. return 0;
  268. }
  269. static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
  270. unsigned long rate,
  271. unsigned long *parent_rate)
  272. {
  273. int shift = 0;
  274. unsigned long best_rate;
  275. unsigned long best_diff;
  276. unsigned long cur_rate = *parent_rate;
  277. unsigned long cur_diff;
  278. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  279. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
  280. return *parent_rate;
  281. if (periph->range.max) {
  282. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  283. cur_rate = *parent_rate >> shift;
  284. if (cur_rate <= periph->range.max)
  285. break;
  286. }
  287. }
  288. if (rate >= cur_rate)
  289. return cur_rate;
  290. best_diff = cur_rate - rate;
  291. best_rate = cur_rate;
  292. for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  293. cur_rate = *parent_rate >> shift;
  294. if (cur_rate < rate)
  295. cur_diff = rate - cur_rate;
  296. else
  297. cur_diff = cur_rate - rate;
  298. if (cur_diff < best_diff) {
  299. best_diff = cur_diff;
  300. best_rate = cur_rate;
  301. }
  302. if (!best_diff || cur_rate < rate)
  303. break;
  304. }
  305. return best_rate;
  306. }
  307. static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
  308. unsigned long rate,
  309. unsigned long parent_rate)
  310. {
  311. int shift;
  312. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  313. if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
  314. if (parent_rate == rate)
  315. return 0;
  316. else
  317. return -EINVAL;
  318. }
  319. if (periph->range.max && rate > periph->range.max)
  320. return -EINVAL;
  321. for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
  322. if (parent_rate >> shift == rate) {
  323. periph->auto_div = false;
  324. periph->div = shift;
  325. return 0;
  326. }
  327. }
  328. return -EINVAL;
  329. }
  330. static int clk_sam9x5_peripheral_save_context(struct clk_hw *hw)
  331. {
  332. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  333. periph->pms.status = clk_sam9x5_peripheral_is_enabled(hw);
  334. return 0;
  335. }
  336. static void clk_sam9x5_peripheral_restore_context(struct clk_hw *hw)
  337. {
  338. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  339. if (periph->pms.status)
  340. clk_sam9x5_peripheral_set(periph, periph->pms.status);
  341. }
  342. static const struct clk_ops sam9x5_peripheral_ops = {
  343. .enable = clk_sam9x5_peripheral_enable,
  344. .disable = clk_sam9x5_peripheral_disable,
  345. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  346. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  347. .round_rate = clk_sam9x5_peripheral_round_rate,
  348. .set_rate = clk_sam9x5_peripheral_set_rate,
  349. .save_context = clk_sam9x5_peripheral_save_context,
  350. .restore_context = clk_sam9x5_peripheral_restore_context,
  351. };
  352. static const struct clk_ops sam9x5_peripheral_chg_ops = {
  353. .enable = clk_sam9x5_peripheral_enable,
  354. .disable = clk_sam9x5_peripheral_disable,
  355. .is_enabled = clk_sam9x5_peripheral_is_enabled,
  356. .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
  357. .determine_rate = clk_sam9x5_peripheral_determine_rate,
  358. .set_rate = clk_sam9x5_peripheral_set_rate,
  359. .save_context = clk_sam9x5_peripheral_save_context,
  360. .restore_context = clk_sam9x5_peripheral_restore_context,
  361. };
  362. struct clk_hw * __init
  363. at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  364. const struct clk_pcr_layout *layout,
  365. const char *name, const char *parent_name,
  366. struct clk_hw *parent_hw,
  367. u32 id, const struct clk_range *range,
  368. int chg_pid, unsigned long flags)
  369. {
  370. struct clk_sam9x5_peripheral *periph;
  371. struct clk_init_data init = {};
  372. struct clk_hw *hw;
  373. int ret;
  374. if (!name || !(parent_name || parent_hw))
  375. return ERR_PTR(-EINVAL);
  376. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  377. if (!periph)
  378. return ERR_PTR(-ENOMEM);
  379. init.name = name;
  380. if (parent_hw)
  381. init.parent_hws = (const struct clk_hw **)&parent_hw;
  382. else
  383. init.parent_names = &parent_name;
  384. init.num_parents = 1;
  385. init.flags = flags;
  386. if (chg_pid < 0) {
  387. init.ops = &sam9x5_peripheral_ops;
  388. } else {
  389. init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  390. CLK_SET_RATE_PARENT;
  391. init.ops = &sam9x5_peripheral_chg_ops;
  392. }
  393. periph->id = id;
  394. periph->hw.init = &init;
  395. periph->div = 0;
  396. periph->regmap = regmap;
  397. periph->lock = lock;
  398. if (layout->div_mask)
  399. periph->auto_div = true;
  400. periph->layout = layout;
  401. periph->range = *range;
  402. periph->chg_pid = chg_pid;
  403. hw = &periph->hw;
  404. ret = clk_hw_register(NULL, &periph->hw);
  405. if (ret) {
  406. kfree(periph);
  407. hw = ERR_PTR(ret);
  408. } else {
  409. clk_sam9x5_peripheral_autodiv(periph);
  410. }
  411. return hw;
  412. }