clock_ops.c 12 KB

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