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_clock.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_acquire - Acquire a device clock.
  31. * @dev: Device whose clock is to be acquired.
  32. * @ce: PM clock entry corresponding to the clock.
  33. */
  34. static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
  35. {
  36. ce->clk = clk_get(dev, ce->con_id);
  37. if (IS_ERR(ce->clk)) {
  38. ce->status = PCE_STATUS_ERROR;
  39. } else {
  40. ce->status = PCE_STATUS_ACQUIRED;
  41. dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
  42. }
  43. }
  44. /**
  45. * pm_clk_add - Start using a device clock for power management.
  46. * @dev: Device whose clock is going to be used for power management.
  47. * @con_id: Connection ID of the clock.
  48. *
  49. * Add the clock represented by @con_id to the list of clocks used for
  50. * the power management of @dev.
  51. */
  52. int pm_clk_add(struct device *dev, const char *con_id)
  53. {
  54. struct pm_subsys_data *psd = dev_to_psd(dev);
  55. struct pm_clock_entry *ce;
  56. if (!psd)
  57. return -EINVAL;
  58. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  59. if (!ce) {
  60. dev_err(dev, "Not enough memory for clock entry.\n");
  61. return -ENOMEM;
  62. }
  63. if (con_id) {
  64. ce->con_id = kstrdup(con_id, GFP_KERNEL);
  65. if (!ce->con_id) {
  66. dev_err(dev,
  67. "Not enough memory for clock connection ID.\n");
  68. kfree(ce);
  69. return -ENOMEM;
  70. }
  71. }
  72. pm_clk_acquire(dev, ce);
  73. spin_lock_irq(&psd->lock);
  74. list_add_tail(&ce->node, &psd->clock_list);
  75. spin_unlock_irq(&psd->lock);
  76. return 0;
  77. }
  78. /**
  79. * __pm_clk_remove - Destroy PM clock entry.
  80. * @ce: PM clock entry to destroy.
  81. */
  82. static void __pm_clk_remove(struct pm_clock_entry *ce)
  83. {
  84. if (!ce)
  85. return;
  86. if (ce->status < PCE_STATUS_ERROR) {
  87. if (ce->status == PCE_STATUS_ENABLED)
  88. clk_disable(ce->clk);
  89. if (ce->status >= PCE_STATUS_ACQUIRED)
  90. clk_put(ce->clk);
  91. }
  92. kfree(ce->con_id);
  93. kfree(ce);
  94. }
  95. /**
  96. * pm_clk_remove - Stop using a device clock for power management.
  97. * @dev: Device whose clock should not be used for PM any more.
  98. * @con_id: Connection ID of the clock.
  99. *
  100. * Remove the clock represented by @con_id from the list of clocks used for
  101. * the power management of @dev.
  102. */
  103. void pm_clk_remove(struct device *dev, const char *con_id)
  104. {
  105. struct pm_subsys_data *psd = dev_to_psd(dev);
  106. struct pm_clock_entry *ce;
  107. if (!psd)
  108. return;
  109. spin_lock_irq(&psd->lock);
  110. list_for_each_entry(ce, &psd->clock_list, node) {
  111. if (!con_id && !ce->con_id)
  112. goto remove;
  113. else if (!con_id || !ce->con_id)
  114. continue;
  115. else if (!strcmp(con_id, ce->con_id))
  116. goto remove;
  117. }
  118. spin_unlock_irq(&psd->lock);
  119. return;
  120. remove:
  121. list_del(&ce->node);
  122. spin_unlock_irq(&psd->lock);
  123. __pm_clk_remove(ce);
  124. }
  125. /**
  126. * pm_clk_init - Initialize a device's list of power management clocks.
  127. * @dev: Device to initialize the list of PM clocks for.
  128. *
  129. * Initialize the lock and clock_list members of the device's pm_subsys_data
  130. * object.
  131. */
  132. void pm_clk_init(struct device *dev)
  133. {
  134. struct pm_subsys_data *psd = dev_to_psd(dev);
  135. if (psd)
  136. INIT_LIST_HEAD(&psd->clock_list);
  137. }
  138. /**
  139. * pm_clk_create - Create and initialize a device's list of PM clocks.
  140. * @dev: Device to create and initialize the list of PM clocks for.
  141. *
  142. * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
  143. * members and make the @dev's power.subsys_data field point to it.
  144. */
  145. int pm_clk_create(struct device *dev)
  146. {
  147. int ret = dev_pm_get_subsys_data(dev);
  148. return ret < 0 ? ret : 0;
  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. clk_disable(ce->clk);
  194. ce->status = PCE_STATUS_ACQUIRED;
  195. }
  196. }
  197. spin_unlock_irqrestore(&psd->lock, flags);
  198. return 0;
  199. }
  200. /**
  201. * pm_clk_resume - Enable clocks in a device's PM clock list.
  202. * @dev: Device to enable the clocks for.
  203. */
  204. int pm_clk_resume(struct device *dev)
  205. {
  206. struct pm_subsys_data *psd = dev_to_psd(dev);
  207. struct pm_clock_entry *ce;
  208. unsigned long flags;
  209. dev_dbg(dev, "%s()\n", __func__);
  210. if (!psd)
  211. return 0;
  212. spin_lock_irqsave(&psd->lock, flags);
  213. list_for_each_entry(ce, &psd->clock_list, node) {
  214. if (ce->status < PCE_STATUS_ERROR) {
  215. clk_enable(ce->clk);
  216. ce->status = PCE_STATUS_ENABLED;
  217. }
  218. }
  219. spin_unlock_irqrestore(&psd->lock, flags);
  220. return 0;
  221. }
  222. /**
  223. * pm_clk_notify - Notify routine for device addition and removal.
  224. * @nb: Notifier block object this function is a member of.
  225. * @action: Operation being carried out by the caller.
  226. * @data: Device the routine is being run for.
  227. *
  228. * For this function to work, @nb must be a member of an object of type
  229. * struct pm_clk_notifier_block containing all of the requisite data.
  230. * Specifically, the pm_domain member of that object is copied to the device's
  231. * pm_domain field and its con_ids member is used to populate the device's list
  232. * of PM clocks, depending on @action.
  233. *
  234. * If the device's pm_domain field is already populated with a value different
  235. * from the one stored in the struct pm_clk_notifier_block object, the function
  236. * does nothing.
  237. */
  238. static int pm_clk_notify(struct notifier_block *nb,
  239. unsigned long action, void *data)
  240. {
  241. struct pm_clk_notifier_block *clknb;
  242. struct device *dev = data;
  243. char **con_id;
  244. int error;
  245. dev_dbg(dev, "%s() %ld\n", __func__, action);
  246. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  247. switch (action) {
  248. case BUS_NOTIFY_ADD_DEVICE:
  249. if (dev->pm_domain)
  250. break;
  251. error = pm_clk_create(dev);
  252. if (error)
  253. break;
  254. dev->pm_domain = clknb->pm_domain;
  255. if (clknb->con_ids[0]) {
  256. for (con_id = clknb->con_ids; *con_id; con_id++)
  257. pm_clk_add(dev, *con_id);
  258. } else {
  259. pm_clk_add(dev, NULL);
  260. }
  261. break;
  262. case BUS_NOTIFY_DEL_DEVICE:
  263. if (dev->pm_domain != clknb->pm_domain)
  264. break;
  265. dev->pm_domain = NULL;
  266. pm_clk_destroy(dev);
  267. break;
  268. }
  269. return 0;
  270. }
  271. #else /* !CONFIG_PM_RUNTIME */
  272. #ifdef CONFIG_PM
  273. /**
  274. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  275. * @dev: Device to disable the clocks for.
  276. */
  277. int pm_clk_suspend(struct device *dev)
  278. {
  279. struct pm_subsys_data *psd = dev_to_psd(dev);
  280. struct pm_clock_entry *ce;
  281. unsigned long flags;
  282. dev_dbg(dev, "%s()\n", __func__);
  283. /* If there is no driver, the clocks are already disabled. */
  284. if (!psd || !dev->driver)
  285. return 0;
  286. spin_lock_irqsave(&psd->lock, flags);
  287. list_for_each_entry_reverse(ce, &psd->clock_list, node)
  288. clk_disable(ce->clk);
  289. spin_unlock_irqrestore(&psd->lock, flags);
  290. return 0;
  291. }
  292. /**
  293. * pm_clk_resume - Enable clocks in a device's PM clock list.
  294. * @dev: Device to enable the clocks for.
  295. */
  296. int pm_clk_resume(struct device *dev)
  297. {
  298. struct pm_subsys_data *psd = dev_to_psd(dev);
  299. struct pm_clock_entry *ce;
  300. unsigned long flags;
  301. dev_dbg(dev, "%s()\n", __func__);
  302. /* If there is no driver, the clocks should remain disabled. */
  303. if (!psd || !dev->driver)
  304. return 0;
  305. spin_lock_irqsave(&psd->lock, flags);
  306. list_for_each_entry(ce, &psd->clock_list, node)
  307. clk_enable(ce->clk);
  308. spin_unlock_irqrestore(&psd->lock, flags);
  309. return 0;
  310. }
  311. #endif /* 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_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(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_RUNTIME */
  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. }