clock_ops.c 11 KB

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