clock_ops.c 11 KB

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