clock_ops.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
  4. *
  5. * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/io.h>
  10. #include <linux/pm.h>
  11. #include <linux/pm_clock.h>
  12. #include <linux/clk.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/of_clk.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/pm_domain.h>
  18. #include <linux/pm_runtime.h>
  19. #ifdef CONFIG_PM_CLK
  20. enum pce_status {
  21. PCE_STATUS_NONE = 0,
  22. PCE_STATUS_ACQUIRED,
  23. PCE_STATUS_ENABLED,
  24. PCE_STATUS_ERROR,
  25. };
  26. struct pm_clock_entry {
  27. struct list_head node;
  28. char *con_id;
  29. struct clk *clk;
  30. enum pce_status status;
  31. };
  32. /**
  33. * pm_clk_enable - Enable a clock, reporting any errors
  34. * @dev: The device for the given clock
  35. * @ce: PM clock entry corresponding to the clock.
  36. */
  37. static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
  38. {
  39. int ret;
  40. if (ce->status < PCE_STATUS_ERROR) {
  41. ret = clk_enable(ce->clk);
  42. if (!ret)
  43. ce->status = PCE_STATUS_ENABLED;
  44. else
  45. dev_err(dev, "%s: failed to enable clk %p, error %d\n",
  46. __func__, ce->clk, ret);
  47. }
  48. }
  49. /**
  50. * pm_clk_acquire - Acquire a device clock.
  51. * @dev: Device whose clock is to be acquired.
  52. * @ce: PM clock entry corresponding to the clock.
  53. */
  54. static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
  55. {
  56. if (!ce->clk)
  57. ce->clk = clk_get(dev, ce->con_id);
  58. if (IS_ERR(ce->clk)) {
  59. ce->status = PCE_STATUS_ERROR;
  60. } else {
  61. if (clk_prepare(ce->clk)) {
  62. ce->status = PCE_STATUS_ERROR;
  63. dev_err(dev, "clk_prepare() failed\n");
  64. } else {
  65. ce->status = PCE_STATUS_ACQUIRED;
  66. dev_dbg(dev,
  67. "Clock %pC con_id %s managed by runtime PM.\n",
  68. ce->clk, ce->con_id);
  69. }
  70. }
  71. }
  72. static int __pm_clk_add(struct device *dev, const char *con_id,
  73. struct clk *clk)
  74. {
  75. struct pm_subsys_data *psd = dev_to_psd(dev);
  76. struct pm_clock_entry *ce;
  77. if (!psd)
  78. return -EINVAL;
  79. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  80. if (!ce)
  81. return -ENOMEM;
  82. if (con_id) {
  83. ce->con_id = kstrdup(con_id, GFP_KERNEL);
  84. if (!ce->con_id) {
  85. kfree(ce);
  86. return -ENOMEM;
  87. }
  88. } else {
  89. if (IS_ERR(clk)) {
  90. kfree(ce);
  91. return -ENOENT;
  92. }
  93. ce->clk = clk;
  94. }
  95. pm_clk_acquire(dev, ce);
  96. spin_lock_irq(&psd->lock);
  97. list_add_tail(&ce->node, &psd->clock_list);
  98. spin_unlock_irq(&psd->lock);
  99. return 0;
  100. }
  101. /**
  102. * pm_clk_add - Start using a device clock for power management.
  103. * @dev: Device whose clock is going to be used for power management.
  104. * @con_id: Connection ID of the clock.
  105. *
  106. * Add the clock represented by @con_id to the list of clocks used for
  107. * the power management of @dev.
  108. */
  109. int pm_clk_add(struct device *dev, const char *con_id)
  110. {
  111. return __pm_clk_add(dev, con_id, NULL);
  112. }
  113. EXPORT_SYMBOL_GPL(pm_clk_add);
  114. /**
  115. * pm_clk_add_clk - Start using a device clock for power management.
  116. * @dev: Device whose clock is going to be used for power management.
  117. * @clk: Clock pointer
  118. *
  119. * Add the clock to the list of clocks used for the power management of @dev.
  120. * The power-management code will take control of the clock reference, so
  121. * callers should not call clk_put() on @clk after this function sucessfully
  122. * returned.
  123. */
  124. int pm_clk_add_clk(struct device *dev, struct clk *clk)
  125. {
  126. return __pm_clk_add(dev, NULL, clk);
  127. }
  128. EXPORT_SYMBOL_GPL(pm_clk_add_clk);
  129. /**
  130. * of_pm_clk_add_clk - Start using a device clock for power management.
  131. * @dev: Device whose clock is going to be used for power management.
  132. * @name: Name of clock that is going to be used for power management.
  133. *
  134. * Add the clock described in the 'clocks' device-tree node that matches
  135. * with the 'name' provided, to the list of clocks used for the power
  136. * management of @dev. On success, returns 0. Returns a negative error
  137. * code if the clock is not found or cannot be added.
  138. */
  139. int of_pm_clk_add_clk(struct device *dev, const char *name)
  140. {
  141. struct clk *clk;
  142. int ret;
  143. if (!dev || !dev->of_node || !name)
  144. return -EINVAL;
  145. clk = of_clk_get_by_name(dev->of_node, name);
  146. if (IS_ERR(clk))
  147. return PTR_ERR(clk);
  148. ret = pm_clk_add_clk(dev, clk);
  149. if (ret) {
  150. clk_put(clk);
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(of_pm_clk_add_clk);
  156. /**
  157. * of_pm_clk_add_clks - Start using device clock(s) for power management.
  158. * @dev: Device whose clock(s) is going to be used for power management.
  159. *
  160. * Add a series of clocks described in the 'clocks' device-tree node for
  161. * a device to the list of clocks used for the power management of @dev.
  162. * On success, returns the number of clocks added. Returns a negative
  163. * error code if there are no clocks in the device node for the device
  164. * or if adding a clock fails.
  165. */
  166. int of_pm_clk_add_clks(struct device *dev)
  167. {
  168. struct clk **clks;
  169. int i, count;
  170. int ret;
  171. if (!dev || !dev->of_node)
  172. return -EINVAL;
  173. count = of_clk_get_parent_count(dev->of_node);
  174. if (count <= 0)
  175. return -ENODEV;
  176. clks = kcalloc(count, sizeof(*clks), GFP_KERNEL);
  177. if (!clks)
  178. return -ENOMEM;
  179. for (i = 0; i < count; i++) {
  180. clks[i] = of_clk_get(dev->of_node, i);
  181. if (IS_ERR(clks[i])) {
  182. ret = PTR_ERR(clks[i]);
  183. goto error;
  184. }
  185. ret = pm_clk_add_clk(dev, clks[i]);
  186. if (ret) {
  187. clk_put(clks[i]);
  188. goto error;
  189. }
  190. }
  191. kfree(clks);
  192. return i;
  193. error:
  194. while (i--)
  195. pm_clk_remove_clk(dev, clks[i]);
  196. kfree(clks);
  197. return ret;
  198. }
  199. EXPORT_SYMBOL_GPL(of_pm_clk_add_clks);
  200. /**
  201. * __pm_clk_remove - Destroy PM clock entry.
  202. * @ce: PM clock entry to destroy.
  203. */
  204. static void __pm_clk_remove(struct pm_clock_entry *ce)
  205. {
  206. if (!ce)
  207. return;
  208. if (ce->status < PCE_STATUS_ERROR) {
  209. if (ce->status == PCE_STATUS_ENABLED)
  210. clk_disable(ce->clk);
  211. if (ce->status >= PCE_STATUS_ACQUIRED) {
  212. clk_unprepare(ce->clk);
  213. clk_put(ce->clk);
  214. }
  215. }
  216. kfree(ce->con_id);
  217. kfree(ce);
  218. }
  219. /**
  220. * pm_clk_remove - Stop using a device clock for power management.
  221. * @dev: Device whose clock should not be used for PM any more.
  222. * @con_id: Connection ID of the clock.
  223. *
  224. * Remove the clock represented by @con_id from the list of clocks used for
  225. * the power management of @dev.
  226. */
  227. void pm_clk_remove(struct device *dev, const char *con_id)
  228. {
  229. struct pm_subsys_data *psd = dev_to_psd(dev);
  230. struct pm_clock_entry *ce;
  231. if (!psd)
  232. return;
  233. spin_lock_irq(&psd->lock);
  234. list_for_each_entry(ce, &psd->clock_list, node) {
  235. if (!con_id && !ce->con_id)
  236. goto remove;
  237. else if (!con_id || !ce->con_id)
  238. continue;
  239. else if (!strcmp(con_id, ce->con_id))
  240. goto remove;
  241. }
  242. spin_unlock_irq(&psd->lock);
  243. return;
  244. remove:
  245. list_del(&ce->node);
  246. spin_unlock_irq(&psd->lock);
  247. __pm_clk_remove(ce);
  248. }
  249. EXPORT_SYMBOL_GPL(pm_clk_remove);
  250. /**
  251. * pm_clk_remove_clk - Stop using a device clock for power management.
  252. * @dev: Device whose clock should not be used for PM any more.
  253. * @clk: Clock pointer
  254. *
  255. * Remove the clock pointed to by @clk from the list of clocks used for
  256. * the power management of @dev.
  257. */
  258. void pm_clk_remove_clk(struct device *dev, struct clk *clk)
  259. {
  260. struct pm_subsys_data *psd = dev_to_psd(dev);
  261. struct pm_clock_entry *ce;
  262. if (!psd || !clk)
  263. return;
  264. spin_lock_irq(&psd->lock);
  265. list_for_each_entry(ce, &psd->clock_list, node) {
  266. if (clk == ce->clk)
  267. goto remove;
  268. }
  269. spin_unlock_irq(&psd->lock);
  270. return;
  271. remove:
  272. list_del(&ce->node);
  273. spin_unlock_irq(&psd->lock);
  274. __pm_clk_remove(ce);
  275. }
  276. EXPORT_SYMBOL_GPL(pm_clk_remove_clk);
  277. /**
  278. * pm_clk_init - Initialize a device's list of power management clocks.
  279. * @dev: Device to initialize the list of PM clocks for.
  280. *
  281. * Initialize the lock and clock_list members of the device's pm_subsys_data
  282. * object.
  283. */
  284. void pm_clk_init(struct device *dev)
  285. {
  286. struct pm_subsys_data *psd = dev_to_psd(dev);
  287. if (psd)
  288. INIT_LIST_HEAD(&psd->clock_list);
  289. }
  290. EXPORT_SYMBOL_GPL(pm_clk_init);
  291. /**
  292. * pm_clk_create - Create and initialize a device's list of PM clocks.
  293. * @dev: Device to create and initialize the list of PM clocks for.
  294. *
  295. * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
  296. * members and make the @dev's power.subsys_data field point to it.
  297. */
  298. int pm_clk_create(struct device *dev)
  299. {
  300. return dev_pm_get_subsys_data(dev);
  301. }
  302. EXPORT_SYMBOL_GPL(pm_clk_create);
  303. /**
  304. * pm_clk_destroy - Destroy a device's list of power management clocks.
  305. * @dev: Device to destroy the list of PM clocks for.
  306. *
  307. * Clear the @dev's power.subsys_data field, remove the list of clock entries
  308. * from the struct pm_subsys_data object pointed to by it before and free
  309. * that object.
  310. */
  311. void pm_clk_destroy(struct device *dev)
  312. {
  313. struct pm_subsys_data *psd = dev_to_psd(dev);
  314. struct pm_clock_entry *ce, *c;
  315. struct list_head list;
  316. if (!psd)
  317. return;
  318. INIT_LIST_HEAD(&list);
  319. spin_lock_irq(&psd->lock);
  320. list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
  321. list_move(&ce->node, &list);
  322. spin_unlock_irq(&psd->lock);
  323. dev_pm_put_subsys_data(dev);
  324. list_for_each_entry_safe_reverse(ce, c, &list, node) {
  325. list_del(&ce->node);
  326. __pm_clk_remove(ce);
  327. }
  328. }
  329. EXPORT_SYMBOL_GPL(pm_clk_destroy);
  330. /**
  331. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  332. * @dev: Device to disable the clocks for.
  333. */
  334. int pm_clk_suspend(struct device *dev)
  335. {
  336. struct pm_subsys_data *psd = dev_to_psd(dev);
  337. struct pm_clock_entry *ce;
  338. unsigned long flags;
  339. dev_dbg(dev, "%s()\n", __func__);
  340. if (!psd)
  341. return 0;
  342. spin_lock_irqsave(&psd->lock, flags);
  343. list_for_each_entry_reverse(ce, &psd->clock_list, node) {
  344. if (ce->status < PCE_STATUS_ERROR) {
  345. if (ce->status == PCE_STATUS_ENABLED)
  346. clk_disable(ce->clk);
  347. ce->status = PCE_STATUS_ACQUIRED;
  348. }
  349. }
  350. spin_unlock_irqrestore(&psd->lock, flags);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL_GPL(pm_clk_suspend);
  354. /**
  355. * pm_clk_resume - Enable clocks in a device's PM clock list.
  356. * @dev: Device to enable the clocks for.
  357. */
  358. int pm_clk_resume(struct device *dev)
  359. {
  360. struct pm_subsys_data *psd = dev_to_psd(dev);
  361. struct pm_clock_entry *ce;
  362. unsigned long flags;
  363. dev_dbg(dev, "%s()\n", __func__);
  364. if (!psd)
  365. return 0;
  366. spin_lock_irqsave(&psd->lock, flags);
  367. list_for_each_entry(ce, &psd->clock_list, node)
  368. __pm_clk_enable(dev, ce);
  369. spin_unlock_irqrestore(&psd->lock, flags);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL_GPL(pm_clk_resume);
  373. /**
  374. * pm_clk_notify - Notify routine for device addition and removal.
  375. * @nb: Notifier block object this function is a member of.
  376. * @action: Operation being carried out by the caller.
  377. * @data: Device the routine is being run for.
  378. *
  379. * For this function to work, @nb must be a member of an object of type
  380. * struct pm_clk_notifier_block containing all of the requisite data.
  381. * Specifically, the pm_domain member of that object is copied to the device's
  382. * pm_domain field and its con_ids member is used to populate the device's list
  383. * of PM clocks, depending on @action.
  384. *
  385. * If the device's pm_domain field is already populated with a value different
  386. * from the one stored in the struct pm_clk_notifier_block object, the function
  387. * does nothing.
  388. */
  389. static int pm_clk_notify(struct notifier_block *nb,
  390. unsigned long action, void *data)
  391. {
  392. struct pm_clk_notifier_block *clknb;
  393. struct device *dev = data;
  394. char **con_id;
  395. int error;
  396. dev_dbg(dev, "%s() %ld\n", __func__, action);
  397. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  398. switch (action) {
  399. case BUS_NOTIFY_ADD_DEVICE:
  400. if (dev->pm_domain)
  401. break;
  402. error = pm_clk_create(dev);
  403. if (error)
  404. break;
  405. dev_pm_domain_set(dev, clknb->pm_domain);
  406. if (clknb->con_ids[0]) {
  407. for (con_id = clknb->con_ids; *con_id; con_id++)
  408. pm_clk_add(dev, *con_id);
  409. } else {
  410. pm_clk_add(dev, NULL);
  411. }
  412. break;
  413. case BUS_NOTIFY_DEL_DEVICE:
  414. if (dev->pm_domain != clknb->pm_domain)
  415. break;
  416. dev_pm_domain_set(dev, NULL);
  417. pm_clk_destroy(dev);
  418. break;
  419. }
  420. return 0;
  421. }
  422. int pm_clk_runtime_suspend(struct device *dev)
  423. {
  424. int ret;
  425. dev_dbg(dev, "%s\n", __func__);
  426. ret = pm_generic_runtime_suspend(dev);
  427. if (ret) {
  428. dev_err(dev, "failed to suspend device\n");
  429. return ret;
  430. }
  431. ret = pm_clk_suspend(dev);
  432. if (ret) {
  433. dev_err(dev, "failed to suspend clock\n");
  434. pm_generic_runtime_resume(dev);
  435. return ret;
  436. }
  437. return 0;
  438. }
  439. EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend);
  440. int pm_clk_runtime_resume(struct device *dev)
  441. {
  442. int ret;
  443. dev_dbg(dev, "%s\n", __func__);
  444. ret = pm_clk_resume(dev);
  445. if (ret) {
  446. dev_err(dev, "failed to resume clock\n");
  447. return ret;
  448. }
  449. return pm_generic_runtime_resume(dev);
  450. }
  451. EXPORT_SYMBOL_GPL(pm_clk_runtime_resume);
  452. #else /* !CONFIG_PM_CLK */
  453. /**
  454. * enable_clock - Enable a device clock.
  455. * @dev: Device whose clock is to be enabled.
  456. * @con_id: Connection ID of the clock.
  457. */
  458. static void enable_clock(struct device *dev, const char *con_id)
  459. {
  460. struct clk *clk;
  461. clk = clk_get(dev, con_id);
  462. if (!IS_ERR(clk)) {
  463. clk_prepare_enable(clk);
  464. clk_put(clk);
  465. dev_info(dev, "Runtime PM disabled, clock forced on.\n");
  466. }
  467. }
  468. /**
  469. * disable_clock - Disable a device clock.
  470. * @dev: Device whose clock is to be disabled.
  471. * @con_id: Connection ID of the clock.
  472. */
  473. static void disable_clock(struct device *dev, const char *con_id)
  474. {
  475. struct clk *clk;
  476. clk = clk_get(dev, con_id);
  477. if (!IS_ERR(clk)) {
  478. clk_disable_unprepare(clk);
  479. clk_put(clk);
  480. dev_info(dev, "Runtime PM disabled, clock forced off.\n");
  481. }
  482. }
  483. /**
  484. * pm_clk_notify - Notify routine for device addition and removal.
  485. * @nb: Notifier block object this function is a member of.
  486. * @action: Operation being carried out by the caller.
  487. * @data: Device the routine is being run for.
  488. *
  489. * For this function to work, @nb must be a member of an object of type
  490. * struct pm_clk_notifier_block containing all of the requisite data.
  491. * Specifically, the con_ids member of that object is used to enable or disable
  492. * the device's clocks, depending on @action.
  493. */
  494. static int pm_clk_notify(struct notifier_block *nb,
  495. unsigned long action, void *data)
  496. {
  497. struct pm_clk_notifier_block *clknb;
  498. struct device *dev = data;
  499. char **con_id;
  500. dev_dbg(dev, "%s() %ld\n", __func__, action);
  501. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  502. switch (action) {
  503. case BUS_NOTIFY_BIND_DRIVER:
  504. if (clknb->con_ids[0]) {
  505. for (con_id = clknb->con_ids; *con_id; con_id++)
  506. enable_clock(dev, *con_id);
  507. } else {
  508. enable_clock(dev, NULL);
  509. }
  510. break;
  511. case BUS_NOTIFY_DRIVER_NOT_BOUND:
  512. case BUS_NOTIFY_UNBOUND_DRIVER:
  513. if (clknb->con_ids[0]) {
  514. for (con_id = clknb->con_ids; *con_id; con_id++)
  515. disable_clock(dev, *con_id);
  516. } else {
  517. disable_clock(dev, NULL);
  518. }
  519. break;
  520. }
  521. return 0;
  522. }
  523. #endif /* !CONFIG_PM_CLK */
  524. /**
  525. * pm_clk_add_notifier - Add bus type notifier for power management clocks.
  526. * @bus: Bus type to add the notifier to.
  527. * @clknb: Notifier to be added to the given bus type.
  528. *
  529. * The nb member of @clknb is not expected to be initialized and its
  530. * notifier_call member will be replaced with pm_clk_notify(). However,
  531. * the remaining members of @clknb should be populated prior to calling this
  532. * routine.
  533. */
  534. void pm_clk_add_notifier(struct bus_type *bus,
  535. struct pm_clk_notifier_block *clknb)
  536. {
  537. if (!bus || !clknb)
  538. return;
  539. clknb->nb.notifier_call = pm_clk_notify;
  540. bus_register_notifier(bus, &clknb->nb);
  541. }
  542. EXPORT_SYMBOL_GPL(pm_clk_add_notifier);