clock_ops.c 11 KB

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