clock_ops.c 11 KB

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