clock_ops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/device.h>
  11. #include <linux/io.h>
  12. #include <linux/pm.h>
  13. #include <linux/pm_clock.h>
  14. #include <linux/clk.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_acquire - Acquire a device clock.
  32. * @dev: Device whose clock is to be acquired.
  33. * @ce: PM clock entry corresponding to the clock.
  34. */
  35. static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
  36. {
  37. ce->clk = clk_get(dev, ce->con_id);
  38. if (IS_ERR(ce->clk)) {
  39. ce->status = PCE_STATUS_ERROR;
  40. } else {
  41. ce->status = PCE_STATUS_ACQUIRED;
  42. dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
  43. }
  44. }
  45. /**
  46. * pm_clk_add - Start using a device clock for power management.
  47. * @dev: Device whose clock is going to be used for power management.
  48. * @con_id: Connection ID of the clock.
  49. *
  50. * Add the clock represented by @con_id to the list of clocks used for
  51. * the power management of @dev.
  52. */
  53. int pm_clk_add(struct device *dev, const char *con_id)
  54. {
  55. struct pm_subsys_data *psd = dev_to_psd(dev);
  56. struct pm_clock_entry *ce;
  57. if (!psd)
  58. return -EINVAL;
  59. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  60. if (!ce) {
  61. dev_err(dev, "Not enough memory for clock entry.\n");
  62. return -ENOMEM;
  63. }
  64. if (con_id) {
  65. ce->con_id = kstrdup(con_id, GFP_KERNEL);
  66. if (!ce->con_id) {
  67. dev_err(dev,
  68. "Not enough memory for clock connection ID.\n");
  69. kfree(ce);
  70. return -ENOMEM;
  71. }
  72. }
  73. pm_clk_acquire(dev, ce);
  74. spin_lock_irq(&psd->lock);
  75. list_add_tail(&ce->node, &psd->clock_list);
  76. spin_unlock_irq(&psd->lock);
  77. return 0;
  78. }
  79. /**
  80. * __pm_clk_remove - Destroy PM clock entry.
  81. * @ce: PM clock entry to destroy.
  82. */
  83. static void __pm_clk_remove(struct pm_clock_entry *ce)
  84. {
  85. if (!ce)
  86. return;
  87. if (ce->status < PCE_STATUS_ERROR) {
  88. if (ce->status == PCE_STATUS_ENABLED)
  89. clk_disable_unprepare(ce->clk);
  90. if (ce->status >= PCE_STATUS_ACQUIRED)
  91. clk_put(ce->clk);
  92. }
  93. kfree(ce->con_id);
  94. kfree(ce);
  95. }
  96. /**
  97. * pm_clk_remove - Stop using a device clock for power management.
  98. * @dev: Device whose clock should not be used for PM any more.
  99. * @con_id: Connection ID of the clock.
  100. *
  101. * Remove the clock represented by @con_id from the list of clocks used for
  102. * the power management of @dev.
  103. */
  104. void pm_clk_remove(struct device *dev, const char *con_id)
  105. {
  106. struct pm_subsys_data *psd = dev_to_psd(dev);
  107. struct pm_clock_entry *ce;
  108. if (!psd)
  109. return;
  110. spin_lock_irq(&psd->lock);
  111. list_for_each_entry(ce, &psd->clock_list, node) {
  112. if (!con_id && !ce->con_id)
  113. goto remove;
  114. else if (!con_id || !ce->con_id)
  115. continue;
  116. else if (!strcmp(con_id, ce->con_id))
  117. goto remove;
  118. }
  119. spin_unlock_irq(&psd->lock);
  120. return;
  121. remove:
  122. list_del(&ce->node);
  123. spin_unlock_irq(&psd->lock);
  124. __pm_clk_remove(ce);
  125. }
  126. /**
  127. * pm_clk_init - Initialize a device's list of power management clocks.
  128. * @dev: Device to initialize the list of PM clocks for.
  129. *
  130. * Initialize the lock and clock_list members of the device's pm_subsys_data
  131. * object.
  132. */
  133. void pm_clk_init(struct device *dev)
  134. {
  135. struct pm_subsys_data *psd = dev_to_psd(dev);
  136. if (psd)
  137. INIT_LIST_HEAD(&psd->clock_list);
  138. }
  139. /**
  140. * pm_clk_create - Create and initialize a device's list of PM clocks.
  141. * @dev: Device to create and initialize the list of PM clocks for.
  142. *
  143. * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
  144. * members and make the @dev's power.subsys_data field point to it.
  145. */
  146. int pm_clk_create(struct device *dev)
  147. {
  148. return dev_pm_get_subsys_data(dev);
  149. }
  150. /**
  151. * pm_clk_destroy - Destroy a device's list of power management clocks.
  152. * @dev: Device to destroy the list of PM clocks for.
  153. *
  154. * Clear the @dev's power.subsys_data field, remove the list of clock entries
  155. * from the struct pm_subsys_data object pointed to by it before and free
  156. * that object.
  157. */
  158. void pm_clk_destroy(struct device *dev)
  159. {
  160. struct pm_subsys_data *psd = dev_to_psd(dev);
  161. struct pm_clock_entry *ce, *c;
  162. struct list_head list;
  163. if (!psd)
  164. return;
  165. INIT_LIST_HEAD(&list);
  166. spin_lock_irq(&psd->lock);
  167. list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
  168. list_move(&ce->node, &list);
  169. spin_unlock_irq(&psd->lock);
  170. dev_pm_put_subsys_data(dev);
  171. list_for_each_entry_safe_reverse(ce, c, &list, node) {
  172. list_del(&ce->node);
  173. __pm_clk_remove(ce);
  174. }
  175. }
  176. #endif /* CONFIG_PM */
  177. #ifdef CONFIG_PM_RUNTIME
  178. /**
  179. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  180. * @dev: Device to disable the clocks for.
  181. */
  182. int pm_clk_suspend(struct device *dev)
  183. {
  184. struct pm_subsys_data *psd = dev_to_psd(dev);
  185. struct pm_clock_entry *ce;
  186. unsigned long flags;
  187. dev_dbg(dev, "%s()\n", __func__);
  188. if (!psd)
  189. return 0;
  190. spin_lock_irqsave(&psd->lock, flags);
  191. list_for_each_entry_reverse(ce, &psd->clock_list, node) {
  192. if (ce->status < PCE_STATUS_ERROR) {
  193. if (ce->status == PCE_STATUS_ENABLED)
  194. clk_disable(ce->clk);
  195. ce->status = PCE_STATUS_ACQUIRED;
  196. }
  197. }
  198. spin_unlock_irqrestore(&psd->lock, flags);
  199. return 0;
  200. }
  201. /**
  202. * pm_clk_resume - Enable clocks in a device's PM clock list.
  203. * @dev: Device to enable the clocks for.
  204. */
  205. int pm_clk_resume(struct device *dev)
  206. {
  207. struct pm_subsys_data *psd = dev_to_psd(dev);
  208. struct pm_clock_entry *ce;
  209. unsigned long flags;
  210. dev_dbg(dev, "%s()\n", __func__);
  211. if (!psd)
  212. return 0;
  213. spin_lock_irqsave(&psd->lock, flags);
  214. list_for_each_entry(ce, &psd->clock_list, node) {
  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_prepare_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_unprepare(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. }