clock_ops.c 15 KB

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