clock_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
  3. *
  4. * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  5. *
  6. * This file is released under the GPLv2.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/pm.h>
  12. #include <linux/pm_clock.h>
  13. #include <linux/clk.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/pm_runtime.h>
  18. #ifdef CONFIG_PM
  19. enum pce_status {
  20. PCE_STATUS_NONE = 0,
  21. PCE_STATUS_ACQUIRED,
  22. PCE_STATUS_ENABLED,
  23. PCE_STATUS_ERROR,
  24. };
  25. struct pm_clock_entry {
  26. struct list_head node;
  27. char *con_id;
  28. struct clk *clk;
  29. enum pce_status status;
  30. };
  31. /**
  32. * pm_clk_enable - Enable a clock, reporting any errors
  33. * @dev: The device for the given clock
  34. * @ce: PM clock entry corresponding to the clock.
  35. */
  36. static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
  37. {
  38. int ret;
  39. if (ce->status < PCE_STATUS_ERROR) {
  40. ret = clk_enable(ce->clk);
  41. if (!ret)
  42. ce->status = PCE_STATUS_ENABLED;
  43. else
  44. dev_err(dev, "%s: failed to enable clk %p, error %d\n",
  45. __func__, ce->clk, ret);
  46. }
  47. }
  48. /**
  49. * pm_clk_acquire - Acquire a device clock.
  50. * @dev: Device whose clock is to be acquired.
  51. * @ce: PM clock entry corresponding to the clock.
  52. */
  53. static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
  54. {
  55. if (!ce->clk)
  56. ce->clk = clk_get(dev, ce->con_id);
  57. if (IS_ERR(ce->clk)) {
  58. ce->status = PCE_STATUS_ERROR;
  59. } else {
  60. clk_prepare(ce->clk);
  61. ce->status = PCE_STATUS_ACQUIRED;
  62. dev_dbg(dev, "Clock %pC con_id %s managed by runtime PM.\n",
  63. ce->clk, ce->con_id);
  64. }
  65. }
  66. static int __pm_clk_add(struct device *dev, const char *con_id,
  67. struct clk *clk)
  68. {
  69. struct pm_subsys_data *psd = dev_to_psd(dev);
  70. struct pm_clock_entry *ce;
  71. if (!psd)
  72. return -EINVAL;
  73. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  74. if (!ce)
  75. return -ENOMEM;
  76. if (con_id) {
  77. ce->con_id = kstrdup(con_id, GFP_KERNEL);
  78. if (!ce->con_id) {
  79. dev_err(dev,
  80. "Not enough memory for clock connection ID.\n");
  81. kfree(ce);
  82. return -ENOMEM;
  83. }
  84. } else {
  85. if (IS_ERR(clk) || !__clk_get(clk)) {
  86. kfree(ce);
  87. return -ENOENT;
  88. }
  89. ce->clk = clk;
  90. }
  91. pm_clk_acquire(dev, ce);
  92. spin_lock_irq(&psd->lock);
  93. list_add_tail(&ce->node, &psd->clock_list);
  94. spin_unlock_irq(&psd->lock);
  95. return 0;
  96. }
  97. /**
  98. * pm_clk_add - Start using a device clock for power management.
  99. * @dev: Device whose clock is going to be used for power management.
  100. * @con_id: Connection ID of the clock.
  101. *
  102. * Add the clock represented by @con_id to the list of clocks used for
  103. * the power management of @dev.
  104. */
  105. int pm_clk_add(struct device *dev, const char *con_id)
  106. {
  107. return __pm_clk_add(dev, con_id, NULL);
  108. }
  109. /**
  110. * pm_clk_add_clk - Start using a device clock for power management.
  111. * @dev: Device whose clock is going to be used for power management.
  112. * @clk: Clock pointer
  113. *
  114. * Add the clock to the list of clocks used for the power management of @dev.
  115. * It will increment refcount on clock pointer, use clk_put() on it when done.
  116. */
  117. int pm_clk_add_clk(struct device *dev, struct clk *clk)
  118. {
  119. return __pm_clk_add(dev, NULL, clk);
  120. }
  121. /**
  122. * __pm_clk_remove - Destroy PM clock entry.
  123. * @ce: PM clock entry to destroy.
  124. */
  125. static void __pm_clk_remove(struct pm_clock_entry *ce)
  126. {
  127. if (!ce)
  128. return;
  129. if (ce->status < PCE_STATUS_ERROR) {
  130. if (ce->status == PCE_STATUS_ENABLED)
  131. clk_disable(ce->clk);
  132. if (ce->status >= PCE_STATUS_ACQUIRED) {
  133. clk_unprepare(ce->clk);
  134. clk_put(ce->clk);
  135. }
  136. }
  137. kfree(ce->con_id);
  138. kfree(ce);
  139. }
  140. /**
  141. * pm_clk_remove - Stop using a device clock for power management.
  142. * @dev: Device whose clock should not be used for PM any more.
  143. * @con_id: Connection ID of the clock.
  144. *
  145. * Remove the clock represented by @con_id from the list of clocks used for
  146. * the power management of @dev.
  147. */
  148. void pm_clk_remove(struct device *dev, const char *con_id)
  149. {
  150. struct pm_subsys_data *psd = dev_to_psd(dev);
  151. struct pm_clock_entry *ce;
  152. if (!psd)
  153. return;
  154. spin_lock_irq(&psd->lock);
  155. list_for_each_entry(ce, &psd->clock_list, node) {
  156. if (!con_id && !ce->con_id)
  157. goto remove;
  158. else if (!con_id || !ce->con_id)
  159. continue;
  160. else if (!strcmp(con_id, ce->con_id))
  161. goto remove;
  162. }
  163. spin_unlock_irq(&psd->lock);
  164. return;
  165. remove:
  166. list_del(&ce->node);
  167. spin_unlock_irq(&psd->lock);
  168. __pm_clk_remove(ce);
  169. }
  170. /**
  171. * pm_clk_init - Initialize a device's list of power management clocks.
  172. * @dev: Device to initialize the list of PM clocks for.
  173. *
  174. * Initialize the lock and clock_list members of the device's pm_subsys_data
  175. * object.
  176. */
  177. void pm_clk_init(struct device *dev)
  178. {
  179. struct pm_subsys_data *psd = dev_to_psd(dev);
  180. if (psd)
  181. INIT_LIST_HEAD(&psd->clock_list);
  182. }
  183. /**
  184. * pm_clk_create - Create and initialize a device's list of PM clocks.
  185. * @dev: Device to create and initialize the list of PM clocks for.
  186. *
  187. * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
  188. * members and make the @dev's power.subsys_data field point to it.
  189. */
  190. int pm_clk_create(struct device *dev)
  191. {
  192. return dev_pm_get_subsys_data(dev);
  193. }
  194. /**
  195. * pm_clk_destroy - Destroy a device's list of power management clocks.
  196. * @dev: Device to destroy the list of PM clocks for.
  197. *
  198. * Clear the @dev's power.subsys_data field, remove the list of clock entries
  199. * from the struct pm_subsys_data object pointed to by it before and free
  200. * that object.
  201. */
  202. void pm_clk_destroy(struct device *dev)
  203. {
  204. struct pm_subsys_data *psd = dev_to_psd(dev);
  205. struct pm_clock_entry *ce, *c;
  206. struct list_head list;
  207. if (!psd)
  208. return;
  209. INIT_LIST_HEAD(&list);
  210. spin_lock_irq(&psd->lock);
  211. list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
  212. list_move(&ce->node, &list);
  213. spin_unlock_irq(&psd->lock);
  214. dev_pm_put_subsys_data(dev);
  215. list_for_each_entry_safe_reverse(ce, c, &list, node) {
  216. list_del(&ce->node);
  217. __pm_clk_remove(ce);
  218. }
  219. }
  220. /**
  221. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  222. * @dev: Device to disable the clocks for.
  223. */
  224. int pm_clk_suspend(struct device *dev)
  225. {
  226. struct pm_subsys_data *psd = dev_to_psd(dev);
  227. struct pm_clock_entry *ce;
  228. unsigned long flags;
  229. dev_dbg(dev, "%s()\n", __func__);
  230. if (!psd)
  231. return 0;
  232. spin_lock_irqsave(&psd->lock, flags);
  233. list_for_each_entry_reverse(ce, &psd->clock_list, node) {
  234. if (ce->status < PCE_STATUS_ERROR) {
  235. if (ce->status == PCE_STATUS_ENABLED)
  236. clk_disable(ce->clk);
  237. ce->status = PCE_STATUS_ACQUIRED;
  238. }
  239. }
  240. spin_unlock_irqrestore(&psd->lock, flags);
  241. return 0;
  242. }
  243. /**
  244. * pm_clk_resume - Enable clocks in a device's PM clock list.
  245. * @dev: Device to enable the clocks for.
  246. */
  247. int pm_clk_resume(struct device *dev)
  248. {
  249. struct pm_subsys_data *psd = dev_to_psd(dev);
  250. struct pm_clock_entry *ce;
  251. unsigned long flags;
  252. dev_dbg(dev, "%s()\n", __func__);
  253. if (!psd)
  254. return 0;
  255. spin_lock_irqsave(&psd->lock, flags);
  256. list_for_each_entry(ce, &psd->clock_list, node)
  257. __pm_clk_enable(dev, ce);
  258. spin_unlock_irqrestore(&psd->lock, flags);
  259. return 0;
  260. }
  261. /**
  262. * pm_clk_notify - Notify routine for device addition and removal.
  263. * @nb: Notifier block object this function is a member of.
  264. * @action: Operation being carried out by the caller.
  265. * @data: Device the routine is being run for.
  266. *
  267. * For this function to work, @nb must be a member of an object of type
  268. * struct pm_clk_notifier_block containing all of the requisite data.
  269. * Specifically, the pm_domain member of that object is copied to the device's
  270. * pm_domain field and its con_ids member is used to populate the device's list
  271. * of PM clocks, depending on @action.
  272. *
  273. * If the device's pm_domain field is already populated with a value different
  274. * from the one stored in the struct pm_clk_notifier_block object, the function
  275. * does nothing.
  276. */
  277. static int pm_clk_notify(struct notifier_block *nb,
  278. unsigned long action, void *data)
  279. {
  280. struct pm_clk_notifier_block *clknb;
  281. struct device *dev = data;
  282. char **con_id;
  283. int error;
  284. dev_dbg(dev, "%s() %ld\n", __func__, action);
  285. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  286. switch (action) {
  287. case BUS_NOTIFY_ADD_DEVICE:
  288. if (dev->pm_domain)
  289. break;
  290. error = pm_clk_create(dev);
  291. if (error)
  292. break;
  293. dev->pm_domain = clknb->pm_domain;
  294. if (clknb->con_ids[0]) {
  295. for (con_id = clknb->con_ids; *con_id; con_id++)
  296. pm_clk_add(dev, *con_id);
  297. } else {
  298. pm_clk_add(dev, NULL);
  299. }
  300. break;
  301. case BUS_NOTIFY_DEL_DEVICE:
  302. if (dev->pm_domain != clknb->pm_domain)
  303. break;
  304. dev->pm_domain = NULL;
  305. pm_clk_destroy(dev);
  306. break;
  307. }
  308. return 0;
  309. }
  310. int pm_clk_runtime_suspend(struct device *dev)
  311. {
  312. int ret;
  313. dev_dbg(dev, "%s\n", __func__);
  314. ret = pm_generic_runtime_suspend(dev);
  315. if (ret) {
  316. dev_err(dev, "failed to suspend device\n");
  317. return ret;
  318. }
  319. ret = pm_clk_suspend(dev);
  320. if (ret) {
  321. dev_err(dev, "failed to suspend clock\n");
  322. pm_generic_runtime_resume(dev);
  323. return ret;
  324. }
  325. return 0;
  326. }
  327. int pm_clk_runtime_resume(struct device *dev)
  328. {
  329. int ret;
  330. dev_dbg(dev, "%s\n", __func__);
  331. ret = pm_clk_resume(dev);
  332. if (ret) {
  333. dev_err(dev, "failed to resume clock\n");
  334. return ret;
  335. }
  336. return pm_generic_runtime_resume(dev);
  337. }
  338. #else /* !CONFIG_PM */
  339. /**
  340. * enable_clock - Enable a device clock.
  341. * @dev: Device whose clock is to be enabled.
  342. * @con_id: Connection ID of the clock.
  343. */
  344. static void enable_clock(struct device *dev, const char *con_id)
  345. {
  346. struct clk *clk;
  347. clk = clk_get(dev, con_id);
  348. if (!IS_ERR(clk)) {
  349. clk_prepare_enable(clk);
  350. clk_put(clk);
  351. dev_info(dev, "Runtime PM disabled, clock forced on.\n");
  352. }
  353. }
  354. /**
  355. * disable_clock - Disable a device clock.
  356. * @dev: Device whose clock is to be disabled.
  357. * @con_id: Connection ID of the clock.
  358. */
  359. static void disable_clock(struct device *dev, const char *con_id)
  360. {
  361. struct clk *clk;
  362. clk = clk_get(dev, con_id);
  363. if (!IS_ERR(clk)) {
  364. clk_disable_unprepare(clk);
  365. clk_put(clk);
  366. dev_info(dev, "Runtime PM disabled, clock forced off.\n");
  367. }
  368. }
  369. /**
  370. * pm_clk_notify - Notify routine for device addition and removal.
  371. * @nb: Notifier block object this function is a member of.
  372. * @action: Operation being carried out by the caller.
  373. * @data: Device the routine is being run for.
  374. *
  375. * For this function to work, @nb must be a member of an object of type
  376. * struct pm_clk_notifier_block containing all of the requisite data.
  377. * Specifically, the con_ids member of that object is used to enable or disable
  378. * the device's clocks, depending on @action.
  379. */
  380. static int pm_clk_notify(struct notifier_block *nb,
  381. unsigned long action, void *data)
  382. {
  383. struct pm_clk_notifier_block *clknb;
  384. struct device *dev = data;
  385. char **con_id;
  386. dev_dbg(dev, "%s() %ld\n", __func__, action);
  387. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  388. switch (action) {
  389. case BUS_NOTIFY_BIND_DRIVER:
  390. if (clknb->con_ids[0]) {
  391. for (con_id = clknb->con_ids; *con_id; con_id++)
  392. enable_clock(dev, *con_id);
  393. } else {
  394. enable_clock(dev, NULL);
  395. }
  396. break;
  397. case BUS_NOTIFY_UNBOUND_DRIVER:
  398. if (clknb->con_ids[0]) {
  399. for (con_id = clknb->con_ids; *con_id; con_id++)
  400. disable_clock(dev, *con_id);
  401. } else {
  402. disable_clock(dev, NULL);
  403. }
  404. break;
  405. }
  406. return 0;
  407. }
  408. #endif /* !CONFIG_PM */
  409. /**
  410. * pm_clk_add_notifier - Add bus type notifier for power management clocks.
  411. * @bus: Bus type to add the notifier to.
  412. * @clknb: Notifier to be added to the given bus type.
  413. *
  414. * The nb member of @clknb is not expected to be initialized and its
  415. * notifier_call member will be replaced with pm_clk_notify(). However,
  416. * the remaining members of @clknb should be populated prior to calling this
  417. * routine.
  418. */
  419. void pm_clk_add_notifier(struct bus_type *bus,
  420. struct pm_clk_notifier_block *clknb)
  421. {
  422. if (!bus || !clknb)
  423. return;
  424. clknb->nb.notifier_call = pm_clk_notify;
  425. bus_register_notifier(bus, &clknb->nb);
  426. }