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