clock_ops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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(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. int ret = dev_pm_get_subsys_data(dev);
  149. return ret < 0 ? ret : 0;
  150. }
  151. /**
  152. * pm_clk_destroy - Destroy a device's list of power management clocks.
  153. * @dev: Device to destroy the list of PM clocks for.
  154. *
  155. * Clear the @dev's power.subsys_data field, remove the list of clock entries
  156. * from the struct pm_subsys_data object pointed to by it before and free
  157. * that object.
  158. */
  159. void pm_clk_destroy(struct device *dev)
  160. {
  161. struct pm_subsys_data *psd = dev_to_psd(dev);
  162. struct pm_clock_entry *ce, *c;
  163. struct list_head list;
  164. if (!psd)
  165. return;
  166. INIT_LIST_HEAD(&list);
  167. spin_lock_irq(&psd->lock);
  168. list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
  169. list_move(&ce->node, &list);
  170. spin_unlock_irq(&psd->lock);
  171. dev_pm_put_subsys_data(dev);
  172. list_for_each_entry_safe_reverse(ce, c, &list, node) {
  173. list_del(&ce->node);
  174. __pm_clk_remove(ce);
  175. }
  176. }
  177. #endif /* CONFIG_PM */
  178. #ifdef CONFIG_PM_RUNTIME
  179. /**
  180. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  181. * @dev: Device to disable the clocks for.
  182. */
  183. int pm_clk_suspend(struct device *dev)
  184. {
  185. struct pm_subsys_data *psd = dev_to_psd(dev);
  186. struct pm_clock_entry *ce;
  187. unsigned long flags;
  188. dev_dbg(dev, "%s()\n", __func__);
  189. if (!psd)
  190. return 0;
  191. spin_lock_irqsave(&psd->lock, flags);
  192. list_for_each_entry_reverse(ce, &psd->clock_list, node) {
  193. if (ce->status < PCE_STATUS_ERROR) {
  194. if (ce->status == PCE_STATUS_ENABLED)
  195. clk_disable(ce->clk);
  196. ce->status = PCE_STATUS_ACQUIRED;
  197. }
  198. }
  199. spin_unlock_irqrestore(&psd->lock, flags);
  200. return 0;
  201. }
  202. /**
  203. * pm_clk_resume - Enable clocks in a device's PM clock list.
  204. * @dev: Device to enable the clocks for.
  205. */
  206. int pm_clk_resume(struct device *dev)
  207. {
  208. struct pm_subsys_data *psd = dev_to_psd(dev);
  209. struct pm_clock_entry *ce;
  210. unsigned long flags;
  211. dev_dbg(dev, "%s()\n", __func__);
  212. if (!psd)
  213. return 0;
  214. spin_lock_irqsave(&psd->lock, flags);
  215. list_for_each_entry(ce, &psd->clock_list, node) {
  216. if (ce->status < PCE_STATUS_ERROR) {
  217. clk_enable(ce->clk);
  218. ce->status = PCE_STATUS_ENABLED;
  219. }
  220. }
  221. spin_unlock_irqrestore(&psd->lock, flags);
  222. return 0;
  223. }
  224. /**
  225. * pm_clk_notify - Notify routine for device addition and removal.
  226. * @nb: Notifier block object this function is a member of.
  227. * @action: Operation being carried out by the caller.
  228. * @data: Device the routine is being run for.
  229. *
  230. * For this function to work, @nb must be a member of an object of type
  231. * struct pm_clk_notifier_block containing all of the requisite data.
  232. * Specifically, the pm_domain member of that object is copied to the device's
  233. * pm_domain field and its con_ids member is used to populate the device's list
  234. * of PM clocks, depending on @action.
  235. *
  236. * If the device's pm_domain field is already populated with a value different
  237. * from the one stored in the struct pm_clk_notifier_block object, the function
  238. * does nothing.
  239. */
  240. static int pm_clk_notify(struct notifier_block *nb,
  241. unsigned long action, void *data)
  242. {
  243. struct pm_clk_notifier_block *clknb;
  244. struct device *dev = data;
  245. char **con_id;
  246. int error;
  247. dev_dbg(dev, "%s() %ld\n", __func__, action);
  248. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  249. switch (action) {
  250. case BUS_NOTIFY_ADD_DEVICE:
  251. if (dev->pm_domain)
  252. break;
  253. error = pm_clk_create(dev);
  254. if (error)
  255. break;
  256. dev->pm_domain = clknb->pm_domain;
  257. if (clknb->con_ids[0]) {
  258. for (con_id = clknb->con_ids; *con_id; con_id++)
  259. pm_clk_add(dev, *con_id);
  260. } else {
  261. pm_clk_add(dev, NULL);
  262. }
  263. break;
  264. case BUS_NOTIFY_DEL_DEVICE:
  265. if (dev->pm_domain != clknb->pm_domain)
  266. break;
  267. dev->pm_domain = NULL;
  268. pm_clk_destroy(dev);
  269. break;
  270. }
  271. return 0;
  272. }
  273. #else /* !CONFIG_PM_RUNTIME */
  274. #ifdef CONFIG_PM
  275. /**
  276. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  277. * @dev: Device to disable the clocks for.
  278. */
  279. int pm_clk_suspend(struct device *dev)
  280. {
  281. struct pm_subsys_data *psd = dev_to_psd(dev);
  282. struct pm_clock_entry *ce;
  283. unsigned long flags;
  284. dev_dbg(dev, "%s()\n", __func__);
  285. /* If there is no driver, the clocks are already disabled. */
  286. if (!psd || !dev->driver)
  287. return 0;
  288. spin_lock_irqsave(&psd->lock, flags);
  289. list_for_each_entry_reverse(ce, &psd->clock_list, node)
  290. clk_disable(ce->clk);
  291. spin_unlock_irqrestore(&psd->lock, flags);
  292. return 0;
  293. }
  294. /**
  295. * pm_clk_resume - Enable clocks in a device's PM clock list.
  296. * @dev: Device to enable the clocks for.
  297. */
  298. int pm_clk_resume(struct device *dev)
  299. {
  300. struct pm_subsys_data *psd = dev_to_psd(dev);
  301. struct pm_clock_entry *ce;
  302. unsigned long flags;
  303. dev_dbg(dev, "%s()\n", __func__);
  304. /* If there is no driver, the clocks should remain disabled. */
  305. if (!psd || !dev->driver)
  306. return 0;
  307. spin_lock_irqsave(&psd->lock, flags);
  308. list_for_each_entry(ce, &psd->clock_list, node)
  309. clk_enable(ce->clk);
  310. spin_unlock_irqrestore(&psd->lock, flags);
  311. return 0;
  312. }
  313. #endif /* CONFIG_PM */
  314. /**
  315. * enable_clock - Enable a device clock.
  316. * @dev: Device whose clock is to be enabled.
  317. * @con_id: Connection ID of the clock.
  318. */
  319. static void enable_clock(struct device *dev, const char *con_id)
  320. {
  321. struct clk *clk;
  322. clk = clk_get(dev, con_id);
  323. if (!IS_ERR(clk)) {
  324. clk_enable(clk);
  325. clk_put(clk);
  326. dev_info(dev, "Runtime PM disabled, clock forced on.\n");
  327. }
  328. }
  329. /**
  330. * disable_clock - Disable a device clock.
  331. * @dev: Device whose clock is to be disabled.
  332. * @con_id: Connection ID of the clock.
  333. */
  334. static void disable_clock(struct device *dev, const char *con_id)
  335. {
  336. struct clk *clk;
  337. clk = clk_get(dev, con_id);
  338. if (!IS_ERR(clk)) {
  339. clk_disable(clk);
  340. clk_put(clk);
  341. dev_info(dev, "Runtime PM disabled, clock forced off.\n");
  342. }
  343. }
  344. /**
  345. * pm_clk_notify - Notify routine for device addition and removal.
  346. * @nb: Notifier block object this function is a member of.
  347. * @action: Operation being carried out by the caller.
  348. * @data: Device the routine is being run for.
  349. *
  350. * For this function to work, @nb must be a member of an object of type
  351. * struct pm_clk_notifier_block containing all of the requisite data.
  352. * Specifically, the con_ids member of that object is used to enable or disable
  353. * the device's clocks, depending on @action.
  354. */
  355. static int pm_clk_notify(struct notifier_block *nb,
  356. unsigned long action, void *data)
  357. {
  358. struct pm_clk_notifier_block *clknb;
  359. struct device *dev = data;
  360. char **con_id;
  361. dev_dbg(dev, "%s() %ld\n", __func__, action);
  362. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  363. switch (action) {
  364. case BUS_NOTIFY_BIND_DRIVER:
  365. if (clknb->con_ids[0]) {
  366. for (con_id = clknb->con_ids; *con_id; con_id++)
  367. enable_clock(dev, *con_id);
  368. } else {
  369. enable_clock(dev, NULL);
  370. }
  371. break;
  372. case BUS_NOTIFY_UNBOUND_DRIVER:
  373. if (clknb->con_ids[0]) {
  374. for (con_id = clknb->con_ids; *con_id; con_id++)
  375. disable_clock(dev, *con_id);
  376. } else {
  377. disable_clock(dev, NULL);
  378. }
  379. break;
  380. }
  381. return 0;
  382. }
  383. #endif /* !CONFIG_PM_RUNTIME */
  384. /**
  385. * pm_clk_add_notifier - Add bus type notifier for power management clocks.
  386. * @bus: Bus type to add the notifier to.
  387. * @clknb: Notifier to be added to the given bus type.
  388. *
  389. * The nb member of @clknb is not expected to be initialized and its
  390. * notifier_call member will be replaced with pm_clk_notify(). However,
  391. * the remaining members of @clknb should be populated prior to calling this
  392. * routine.
  393. */
  394. void pm_clk_add_notifier(struct bus_type *bus,
  395. struct pm_clk_notifier_block *clknb)
  396. {
  397. if (!bus || !clknb)
  398. return;
  399. clknb->nb.notifier_call = pm_clk_notify;
  400. bus_register_notifier(bus, &clknb->nb);
  401. }