clock_ops.c 12 KB

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