clock_ops.c 12 KB

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