clock_ops.c 11 KB

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