clock_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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_domain.h>
  18. #include <linux/pm_runtime.h>
  19. #ifdef CONFIG_PM_CLK
  20. enum pce_status {
  21. PCE_STATUS_NONE = 0,
  22. PCE_STATUS_ACQUIRED,
  23. PCE_STATUS_ENABLED,
  24. PCE_STATUS_ERROR,
  25. };
  26. struct pm_clock_entry {
  27. struct list_head node;
  28. char *con_id;
  29. struct clk *clk;
  30. enum pce_status status;
  31. };
  32. /**
  33. * pm_clk_enable - Enable a clock, reporting any errors
  34. * @dev: The device for the given clock
  35. * @ce: PM clock entry corresponding to the clock.
  36. */
  37. static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
  38. {
  39. int ret;
  40. if (ce->status < PCE_STATUS_ERROR) {
  41. ret = clk_enable(ce->clk);
  42. if (!ret)
  43. ce->status = PCE_STATUS_ENABLED;
  44. else
  45. dev_err(dev, "%s: failed to enable clk %p, error %d\n",
  46. __func__, ce->clk, ret);
  47. }
  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)) {
  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. * The power-management code will take control of the clock reference, so
  117. * callers should not call clk_put() on @clk after this function sucessfully
  118. * returned.
  119. */
  120. int pm_clk_add_clk(struct device *dev, struct clk *clk)
  121. {
  122. return __pm_clk_add(dev, NULL, clk);
  123. }
  124. /**
  125. * of_pm_clk_add_clks - Start using device clock(s) for power management.
  126. * @dev: Device whose clock(s) is going to be used for power management.
  127. *
  128. * Add a series of clocks described in the 'clocks' device-tree node for
  129. * a device to the list of clocks used for the power management of @dev.
  130. * On success, returns the number of clocks added. Returns a negative
  131. * error code if there are no clocks in the device node for the device
  132. * or if adding a clock fails.
  133. */
  134. int of_pm_clk_add_clks(struct device *dev)
  135. {
  136. struct clk **clks;
  137. unsigned int i, count;
  138. int ret;
  139. if (!dev || !dev->of_node)
  140. return -EINVAL;
  141. count = of_count_phandle_with_args(dev->of_node, "clocks",
  142. "#clock-cells");
  143. if (count == 0)
  144. return -ENODEV;
  145. clks = kcalloc(count, sizeof(*clks), GFP_KERNEL);
  146. if (!clks)
  147. return -ENOMEM;
  148. for (i = 0; i < count; i++) {
  149. clks[i] = of_clk_get(dev->of_node, i);
  150. if (IS_ERR(clks[i])) {
  151. ret = PTR_ERR(clks[i]);
  152. goto error;
  153. }
  154. ret = pm_clk_add_clk(dev, clks[i]);
  155. if (ret) {
  156. clk_put(clks[i]);
  157. goto error;
  158. }
  159. }
  160. kfree(clks);
  161. return i;
  162. error:
  163. while (i--)
  164. pm_clk_remove_clk(dev, clks[i]);
  165. kfree(clks);
  166. return ret;
  167. }
  168. /**
  169. * __pm_clk_remove - Destroy PM clock entry.
  170. * @ce: PM clock entry to destroy.
  171. */
  172. static void __pm_clk_remove(struct pm_clock_entry *ce)
  173. {
  174. if (!ce)
  175. return;
  176. if (ce->status < PCE_STATUS_ERROR) {
  177. if (ce->status == PCE_STATUS_ENABLED)
  178. clk_disable(ce->clk);
  179. if (ce->status >= PCE_STATUS_ACQUIRED) {
  180. clk_unprepare(ce->clk);
  181. clk_put(ce->clk);
  182. }
  183. }
  184. kfree(ce->con_id);
  185. kfree(ce);
  186. }
  187. /**
  188. * pm_clk_remove - Stop using a device clock for power management.
  189. * @dev: Device whose clock should not be used for PM any more.
  190. * @con_id: Connection ID of the clock.
  191. *
  192. * Remove the clock represented by @con_id from the list of clocks used for
  193. * the power management of @dev.
  194. */
  195. void pm_clk_remove(struct device *dev, const char *con_id)
  196. {
  197. struct pm_subsys_data *psd = dev_to_psd(dev);
  198. struct pm_clock_entry *ce;
  199. if (!psd)
  200. return;
  201. spin_lock_irq(&psd->lock);
  202. list_for_each_entry(ce, &psd->clock_list, node) {
  203. if (!con_id && !ce->con_id)
  204. goto remove;
  205. else if (!con_id || !ce->con_id)
  206. continue;
  207. else if (!strcmp(con_id, ce->con_id))
  208. goto remove;
  209. }
  210. spin_unlock_irq(&psd->lock);
  211. return;
  212. remove:
  213. list_del(&ce->node);
  214. spin_unlock_irq(&psd->lock);
  215. __pm_clk_remove(ce);
  216. }
  217. /**
  218. * pm_clk_remove_clk - Stop using a device clock for power management.
  219. * @dev: Device whose clock should not be used for PM any more.
  220. * @clk: Clock pointer
  221. *
  222. * Remove the clock pointed to by @clk from the list of clocks used for
  223. * the power management of @dev.
  224. */
  225. void pm_clk_remove_clk(struct device *dev, struct clk *clk)
  226. {
  227. struct pm_subsys_data *psd = dev_to_psd(dev);
  228. struct pm_clock_entry *ce;
  229. if (!psd || !clk)
  230. return;
  231. spin_lock_irq(&psd->lock);
  232. list_for_each_entry(ce, &psd->clock_list, node) {
  233. if (clk == ce->clk)
  234. goto remove;
  235. }
  236. spin_unlock_irq(&psd->lock);
  237. return;
  238. remove:
  239. list_del(&ce->node);
  240. spin_unlock_irq(&psd->lock);
  241. __pm_clk_remove(ce);
  242. }
  243. /**
  244. * pm_clk_init - Initialize a device's list of power management clocks.
  245. * @dev: Device to initialize the list of PM clocks for.
  246. *
  247. * Initialize the lock and clock_list members of the device's pm_subsys_data
  248. * object.
  249. */
  250. void pm_clk_init(struct device *dev)
  251. {
  252. struct pm_subsys_data *psd = dev_to_psd(dev);
  253. if (psd)
  254. INIT_LIST_HEAD(&psd->clock_list);
  255. }
  256. /**
  257. * pm_clk_create - Create and initialize a device's list of PM clocks.
  258. * @dev: Device to create and initialize the list of PM clocks for.
  259. *
  260. * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
  261. * members and make the @dev's power.subsys_data field point to it.
  262. */
  263. int pm_clk_create(struct device *dev)
  264. {
  265. return dev_pm_get_subsys_data(dev);
  266. }
  267. /**
  268. * pm_clk_destroy - Destroy a device's list of power management clocks.
  269. * @dev: Device to destroy the list of PM clocks for.
  270. *
  271. * Clear the @dev's power.subsys_data field, remove the list of clock entries
  272. * from the struct pm_subsys_data object pointed to by it before and free
  273. * that object.
  274. */
  275. void pm_clk_destroy(struct device *dev)
  276. {
  277. struct pm_subsys_data *psd = dev_to_psd(dev);
  278. struct pm_clock_entry *ce, *c;
  279. struct list_head list;
  280. if (!psd)
  281. return;
  282. INIT_LIST_HEAD(&list);
  283. spin_lock_irq(&psd->lock);
  284. list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
  285. list_move(&ce->node, &list);
  286. spin_unlock_irq(&psd->lock);
  287. dev_pm_put_subsys_data(dev);
  288. list_for_each_entry_safe_reverse(ce, c, &list, node) {
  289. list_del(&ce->node);
  290. __pm_clk_remove(ce);
  291. }
  292. }
  293. /**
  294. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  295. * @dev: Device to disable the clocks for.
  296. */
  297. int pm_clk_suspend(struct device *dev)
  298. {
  299. struct pm_subsys_data *psd = dev_to_psd(dev);
  300. struct pm_clock_entry *ce;
  301. unsigned long flags;
  302. dev_dbg(dev, "%s()\n", __func__);
  303. if (!psd)
  304. return 0;
  305. spin_lock_irqsave(&psd->lock, flags);
  306. list_for_each_entry_reverse(ce, &psd->clock_list, node) {
  307. if (ce->status < PCE_STATUS_ERROR) {
  308. if (ce->status == PCE_STATUS_ENABLED)
  309. clk_disable(ce->clk);
  310. ce->status = PCE_STATUS_ACQUIRED;
  311. }
  312. }
  313. spin_unlock_irqrestore(&psd->lock, flags);
  314. return 0;
  315. }
  316. /**
  317. * pm_clk_resume - Enable clocks in a device's PM clock list.
  318. * @dev: Device to enable the clocks for.
  319. */
  320. int pm_clk_resume(struct device *dev)
  321. {
  322. struct pm_subsys_data *psd = dev_to_psd(dev);
  323. struct pm_clock_entry *ce;
  324. unsigned long flags;
  325. dev_dbg(dev, "%s()\n", __func__);
  326. if (!psd)
  327. return 0;
  328. spin_lock_irqsave(&psd->lock, flags);
  329. list_for_each_entry(ce, &psd->clock_list, node)
  330. __pm_clk_enable(dev, ce);
  331. spin_unlock_irqrestore(&psd->lock, flags);
  332. return 0;
  333. }
  334. /**
  335. * pm_clk_notify - Notify routine for device addition and removal.
  336. * @nb: Notifier block object this function is a member of.
  337. * @action: Operation being carried out by the caller.
  338. * @data: Device the routine is being run for.
  339. *
  340. * For this function to work, @nb must be a member of an object of type
  341. * struct pm_clk_notifier_block containing all of the requisite data.
  342. * Specifically, the pm_domain member of that object is copied to the device's
  343. * pm_domain field and its con_ids member is used to populate the device's list
  344. * of PM clocks, depending on @action.
  345. *
  346. * If the device's pm_domain field is already populated with a value different
  347. * from the one stored in the struct pm_clk_notifier_block object, the function
  348. * does nothing.
  349. */
  350. static int pm_clk_notify(struct notifier_block *nb,
  351. unsigned long action, void *data)
  352. {
  353. struct pm_clk_notifier_block *clknb;
  354. struct device *dev = data;
  355. char **con_id;
  356. int error;
  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_ADD_DEVICE:
  361. if (dev->pm_domain)
  362. break;
  363. error = pm_clk_create(dev);
  364. if (error)
  365. break;
  366. dev_pm_domain_set(dev, clknb->pm_domain);
  367. if (clknb->con_ids[0]) {
  368. for (con_id = clknb->con_ids; *con_id; con_id++)
  369. pm_clk_add(dev, *con_id);
  370. } else {
  371. pm_clk_add(dev, NULL);
  372. }
  373. break;
  374. case BUS_NOTIFY_DEL_DEVICE:
  375. if (dev->pm_domain != clknb->pm_domain)
  376. break;
  377. dev_pm_domain_set(dev, NULL);
  378. pm_clk_destroy(dev);
  379. break;
  380. }
  381. return 0;
  382. }
  383. int pm_clk_runtime_suspend(struct device *dev)
  384. {
  385. int ret;
  386. dev_dbg(dev, "%s\n", __func__);
  387. ret = pm_generic_runtime_suspend(dev);
  388. if (ret) {
  389. dev_err(dev, "failed to suspend device\n");
  390. return ret;
  391. }
  392. ret = pm_clk_suspend(dev);
  393. if (ret) {
  394. dev_err(dev, "failed to suspend clock\n");
  395. pm_generic_runtime_resume(dev);
  396. return ret;
  397. }
  398. return 0;
  399. }
  400. int pm_clk_runtime_resume(struct device *dev)
  401. {
  402. int ret;
  403. dev_dbg(dev, "%s\n", __func__);
  404. ret = pm_clk_resume(dev);
  405. if (ret) {
  406. dev_err(dev, "failed to resume clock\n");
  407. return ret;
  408. }
  409. return pm_generic_runtime_resume(dev);
  410. }
  411. #else /* !CONFIG_PM_CLK */
  412. /**
  413. * enable_clock - Enable a device clock.
  414. * @dev: Device whose clock is to be enabled.
  415. * @con_id: Connection ID of the clock.
  416. */
  417. static void enable_clock(struct device *dev, const char *con_id)
  418. {
  419. struct clk *clk;
  420. clk = clk_get(dev, con_id);
  421. if (!IS_ERR(clk)) {
  422. clk_prepare_enable(clk);
  423. clk_put(clk);
  424. dev_info(dev, "Runtime PM disabled, clock forced on.\n");
  425. }
  426. }
  427. /**
  428. * disable_clock - Disable a device clock.
  429. * @dev: Device whose clock is to be disabled.
  430. * @con_id: Connection ID of the clock.
  431. */
  432. static void disable_clock(struct device *dev, const char *con_id)
  433. {
  434. struct clk *clk;
  435. clk = clk_get(dev, con_id);
  436. if (!IS_ERR(clk)) {
  437. clk_disable_unprepare(clk);
  438. clk_put(clk);
  439. dev_info(dev, "Runtime PM disabled, clock forced off.\n");
  440. }
  441. }
  442. /**
  443. * pm_clk_notify - Notify routine for device addition and removal.
  444. * @nb: Notifier block object this function is a member of.
  445. * @action: Operation being carried out by the caller.
  446. * @data: Device the routine is being run for.
  447. *
  448. * For this function to work, @nb must be a member of an object of type
  449. * struct pm_clk_notifier_block containing all of the requisite data.
  450. * Specifically, the con_ids member of that object is used to enable or disable
  451. * the device's clocks, depending on @action.
  452. */
  453. static int pm_clk_notify(struct notifier_block *nb,
  454. unsigned long action, void *data)
  455. {
  456. struct pm_clk_notifier_block *clknb;
  457. struct device *dev = data;
  458. char **con_id;
  459. dev_dbg(dev, "%s() %ld\n", __func__, action);
  460. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  461. switch (action) {
  462. case BUS_NOTIFY_BIND_DRIVER:
  463. if (clknb->con_ids[0]) {
  464. for (con_id = clknb->con_ids; *con_id; con_id++)
  465. enable_clock(dev, *con_id);
  466. } else {
  467. enable_clock(dev, NULL);
  468. }
  469. break;
  470. case BUS_NOTIFY_DRIVER_NOT_BOUND:
  471. case BUS_NOTIFY_UNBOUND_DRIVER:
  472. if (clknb->con_ids[0]) {
  473. for (con_id = clknb->con_ids; *con_id; con_id++)
  474. disable_clock(dev, *con_id);
  475. } else {
  476. disable_clock(dev, NULL);
  477. }
  478. break;
  479. }
  480. return 0;
  481. }
  482. #endif /* !CONFIG_PM_CLK */
  483. /**
  484. * pm_clk_add_notifier - Add bus type notifier for power management clocks.
  485. * @bus: Bus type to add the notifier to.
  486. * @clknb: Notifier to be added to the given bus type.
  487. *
  488. * The nb member of @clknb is not expected to be initialized and its
  489. * notifier_call member will be replaced with pm_clk_notify(). However,
  490. * the remaining members of @clknb should be populated prior to calling this
  491. * routine.
  492. */
  493. void pm_clk_add_notifier(struct bus_type *bus,
  494. struct pm_clk_notifier_block *clknb)
  495. {
  496. if (!bus || !clknb)
  497. return;
  498. clknb->nb.notifier_call = pm_clk_notify;
  499. bus_register_notifier(bus, &clknb->nb);
  500. }