clock_ops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
  4. *
  5. * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/io.h>
  10. #include <linux/pm.h>
  11. #include <linux/pm_clock.h>
  12. #include <linux/clk.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/of_clk.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_PREPARED,
  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. bool enabled_when_prepared;
  33. };
  34. /**
  35. * pm_clk_list_lock - ensure exclusive access for modifying the PM clock
  36. * entry list.
  37. * @psd: pm_subsys_data instance corresponding to the PM clock entry list
  38. * and clk_op_might_sleep count to be modified.
  39. *
  40. * Get exclusive access before modifying the PM clock entry list and the
  41. * clock_op_might_sleep count to guard against concurrent modifications.
  42. * This also protects against a concurrent clock_op_might_sleep and PM clock
  43. * entry list usage in pm_clk_suspend()/pm_clk_resume() that may or may not
  44. * happen in atomic context, hence both the mutex and the spinlock must be
  45. * taken here.
  46. */
  47. static void pm_clk_list_lock(struct pm_subsys_data *psd)
  48. __acquires(&psd->lock)
  49. {
  50. mutex_lock(&psd->clock_mutex);
  51. spin_lock_irq(&psd->lock);
  52. }
  53. /**
  54. * pm_clk_list_unlock - counterpart to pm_clk_list_lock().
  55. * @psd: the same pm_subsys_data instance previously passed to
  56. * pm_clk_list_lock().
  57. */
  58. static void pm_clk_list_unlock(struct pm_subsys_data *psd)
  59. __releases(&psd->lock)
  60. {
  61. spin_unlock_irq(&psd->lock);
  62. mutex_unlock(&psd->clock_mutex);
  63. }
  64. /**
  65. * pm_clk_op_lock - ensure exclusive access for performing clock operations.
  66. * @psd: pm_subsys_data instance corresponding to the PM clock entry list
  67. * and clk_op_might_sleep count being used.
  68. * @flags: stored irq flags.
  69. * @fn: string for the caller function's name.
  70. *
  71. * This is used by pm_clk_suspend() and pm_clk_resume() to guard
  72. * against concurrent modifications to the clock entry list and the
  73. * clock_op_might_sleep count. If clock_op_might_sleep is != 0 then
  74. * only the mutex can be locked and those functions can only be used in
  75. * non atomic context. If clock_op_might_sleep == 0 then these functions
  76. * may be used in any context and only the spinlock can be locked.
  77. * Returns -EINVAL if called in atomic context when clock ops might sleep.
  78. */
  79. static int pm_clk_op_lock(struct pm_subsys_data *psd, unsigned long *flags,
  80. const char *fn)
  81. /* sparse annotations don't work here as exit state isn't static */
  82. {
  83. bool atomic_context = in_atomic() || irqs_disabled();
  84. try_again:
  85. spin_lock_irqsave(&psd->lock, *flags);
  86. if (!psd->clock_op_might_sleep) {
  87. /* the __release is there to work around sparse limitations */
  88. __release(&psd->lock);
  89. return 0;
  90. }
  91. /* bail out if in atomic context */
  92. if (atomic_context) {
  93. pr_err("%s: atomic context with clock_ops_might_sleep = %d",
  94. fn, psd->clock_op_might_sleep);
  95. spin_unlock_irqrestore(&psd->lock, *flags);
  96. might_sleep();
  97. return -EPERM;
  98. }
  99. /* we must switch to the mutex */
  100. spin_unlock_irqrestore(&psd->lock, *flags);
  101. mutex_lock(&psd->clock_mutex);
  102. /*
  103. * There was a possibility for psd->clock_op_might_sleep
  104. * to become 0 above. Keep the mutex only if not the case.
  105. */
  106. if (likely(psd->clock_op_might_sleep))
  107. return 0;
  108. mutex_unlock(&psd->clock_mutex);
  109. goto try_again;
  110. }
  111. /**
  112. * pm_clk_op_unlock - counterpart to pm_clk_op_lock().
  113. * @psd: the same pm_subsys_data instance previously passed to
  114. * pm_clk_op_lock().
  115. * @flags: irq flags provided by pm_clk_op_lock().
  116. */
  117. static void pm_clk_op_unlock(struct pm_subsys_data *psd, unsigned long *flags)
  118. /* sparse annotations don't work here as entry state isn't static */
  119. {
  120. if (psd->clock_op_might_sleep) {
  121. mutex_unlock(&psd->clock_mutex);
  122. } else {
  123. /* the __acquire is there to work around sparse limitations */
  124. __acquire(&psd->lock);
  125. spin_unlock_irqrestore(&psd->lock, *flags);
  126. }
  127. }
  128. /**
  129. * __pm_clk_enable - Enable a clock, reporting any errors
  130. * @dev: The device for the given clock
  131. * @ce: PM clock entry corresponding to the clock.
  132. */
  133. static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
  134. {
  135. int ret;
  136. switch (ce->status) {
  137. case PCE_STATUS_ACQUIRED:
  138. ret = clk_prepare_enable(ce->clk);
  139. break;
  140. case PCE_STATUS_PREPARED:
  141. ret = clk_enable(ce->clk);
  142. break;
  143. default:
  144. return;
  145. }
  146. if (!ret)
  147. ce->status = PCE_STATUS_ENABLED;
  148. else
  149. dev_err(dev, "%s: failed to enable clk %p, error %d\n",
  150. __func__, ce->clk, ret);
  151. }
  152. /**
  153. * pm_clk_acquire - Acquire a device clock.
  154. * @dev: Device whose clock is to be acquired.
  155. * @ce: PM clock entry corresponding to the clock.
  156. */
  157. static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
  158. {
  159. if (!ce->clk)
  160. ce->clk = clk_get(dev, ce->con_id);
  161. if (IS_ERR(ce->clk)) {
  162. ce->status = PCE_STATUS_ERROR;
  163. return;
  164. } else if (clk_is_enabled_when_prepared(ce->clk)) {
  165. /* we defer preparing the clock in that case */
  166. ce->status = PCE_STATUS_ACQUIRED;
  167. ce->enabled_when_prepared = true;
  168. } else if (clk_prepare(ce->clk)) {
  169. ce->status = PCE_STATUS_ERROR;
  170. dev_err(dev, "clk_prepare() failed\n");
  171. return;
  172. } else {
  173. ce->status = PCE_STATUS_PREPARED;
  174. }
  175. dev_dbg(dev, "Clock %pC con_id %s managed by runtime PM.\n",
  176. ce->clk, ce->con_id);
  177. }
  178. static int __pm_clk_add(struct device *dev, const char *con_id,
  179. struct clk *clk)
  180. {
  181. struct pm_subsys_data *psd = dev_to_psd(dev);
  182. struct pm_clock_entry *ce;
  183. if (!psd)
  184. return -EINVAL;
  185. ce = kzalloc_obj(*ce);
  186. if (!ce)
  187. return -ENOMEM;
  188. if (con_id) {
  189. ce->con_id = kstrdup(con_id, GFP_KERNEL);
  190. if (!ce->con_id) {
  191. kfree(ce);
  192. return -ENOMEM;
  193. }
  194. } else {
  195. if (IS_ERR(clk)) {
  196. kfree(ce);
  197. return -ENOENT;
  198. }
  199. ce->clk = clk;
  200. }
  201. pm_clk_acquire(dev, ce);
  202. pm_clk_list_lock(psd);
  203. list_add_tail(&ce->node, &psd->clock_list);
  204. if (ce->enabled_when_prepared)
  205. psd->clock_op_might_sleep++;
  206. pm_clk_list_unlock(psd);
  207. return 0;
  208. }
  209. /**
  210. * pm_clk_add - Start using a device clock for power management.
  211. * @dev: Device whose clock is going to be used for power management.
  212. * @con_id: Connection ID of the clock.
  213. *
  214. * Add the clock represented by @con_id to the list of clocks used for
  215. * the power management of @dev.
  216. */
  217. int pm_clk_add(struct device *dev, const char *con_id)
  218. {
  219. return __pm_clk_add(dev, con_id, NULL);
  220. }
  221. EXPORT_SYMBOL_GPL(pm_clk_add);
  222. /**
  223. * pm_clk_add_clk - Start using a device clock for power management.
  224. * @dev: Device whose clock is going to be used for power management.
  225. * @clk: Clock pointer
  226. *
  227. * Add the clock to the list of clocks used for the power management of @dev.
  228. * The power-management code will take control of the clock reference, so
  229. * callers should not call clk_put() on @clk after this function sucessfully
  230. * returned.
  231. */
  232. int pm_clk_add_clk(struct device *dev, struct clk *clk)
  233. {
  234. return __pm_clk_add(dev, NULL, clk);
  235. }
  236. EXPORT_SYMBOL_GPL(pm_clk_add_clk);
  237. /**
  238. * of_pm_clk_add_clks - Start using device clock(s) for power management.
  239. * @dev: Device whose clock(s) is going to be used for power management.
  240. *
  241. * Add a series of clocks described in the 'clocks' device-tree node for
  242. * a device to the list of clocks used for the power management of @dev.
  243. * On success, returns the number of clocks added. Returns a negative
  244. * error code if there are no clocks in the device node for the device
  245. * or if adding a clock fails.
  246. */
  247. int of_pm_clk_add_clks(struct device *dev)
  248. {
  249. struct clk **clks;
  250. int i, count;
  251. int ret;
  252. if (!dev || !dev->of_node)
  253. return -EINVAL;
  254. count = of_clk_get_parent_count(dev->of_node);
  255. if (count <= 0)
  256. return -ENODEV;
  257. clks = kzalloc_objs(*clks, count);
  258. if (!clks)
  259. return -ENOMEM;
  260. for (i = 0; i < count; i++) {
  261. clks[i] = of_clk_get(dev->of_node, i);
  262. if (IS_ERR(clks[i])) {
  263. ret = PTR_ERR(clks[i]);
  264. goto error;
  265. }
  266. ret = pm_clk_add_clk(dev, clks[i]);
  267. if (ret) {
  268. clk_put(clks[i]);
  269. goto error;
  270. }
  271. }
  272. kfree(clks);
  273. return i;
  274. error:
  275. while (i--)
  276. pm_clk_remove_clk(dev, clks[i]);
  277. kfree(clks);
  278. return ret;
  279. }
  280. EXPORT_SYMBOL_GPL(of_pm_clk_add_clks);
  281. /**
  282. * __pm_clk_remove - Destroy PM clock entry.
  283. * @ce: PM clock entry to destroy.
  284. */
  285. static void __pm_clk_remove(struct pm_clock_entry *ce)
  286. {
  287. if (!ce)
  288. return;
  289. switch (ce->status) {
  290. case PCE_STATUS_ENABLED:
  291. clk_disable(ce->clk);
  292. fallthrough;
  293. case PCE_STATUS_PREPARED:
  294. clk_unprepare(ce->clk);
  295. fallthrough;
  296. case PCE_STATUS_ACQUIRED:
  297. case PCE_STATUS_ERROR:
  298. if (!IS_ERR(ce->clk))
  299. clk_put(ce->clk);
  300. break;
  301. default:
  302. break;
  303. }
  304. kfree(ce->con_id);
  305. kfree(ce);
  306. }
  307. /**
  308. * pm_clk_remove_clk - Stop using a device clock for power management.
  309. * @dev: Device whose clock should not be used for PM any more.
  310. * @clk: Clock pointer
  311. *
  312. * Remove the clock pointed to by @clk from the list of clocks used for
  313. * the power management of @dev.
  314. */
  315. void pm_clk_remove_clk(struct device *dev, struct clk *clk)
  316. {
  317. struct pm_subsys_data *psd = dev_to_psd(dev);
  318. struct pm_clock_entry *ce;
  319. if (!psd || !clk)
  320. return;
  321. pm_clk_list_lock(psd);
  322. list_for_each_entry(ce, &psd->clock_list, node) {
  323. if (clk == ce->clk)
  324. goto remove;
  325. }
  326. pm_clk_list_unlock(psd);
  327. return;
  328. remove:
  329. list_del(&ce->node);
  330. if (ce->enabled_when_prepared)
  331. psd->clock_op_might_sleep--;
  332. pm_clk_list_unlock(psd);
  333. __pm_clk_remove(ce);
  334. }
  335. EXPORT_SYMBOL_GPL(pm_clk_remove_clk);
  336. /**
  337. * pm_clk_init - Initialize a device's list of power management clocks.
  338. * @dev: Device to initialize the list of PM clocks for.
  339. *
  340. * Initialize the lock and clock_list members of the device's pm_subsys_data
  341. * object, set the count of clocks that might sleep to 0.
  342. */
  343. void pm_clk_init(struct device *dev)
  344. {
  345. struct pm_subsys_data *psd = dev_to_psd(dev);
  346. if (psd) {
  347. INIT_LIST_HEAD(&psd->clock_list);
  348. mutex_init(&psd->clock_mutex);
  349. psd->clock_op_might_sleep = 0;
  350. }
  351. }
  352. EXPORT_SYMBOL_GPL(pm_clk_init);
  353. /**
  354. * pm_clk_create - Create and initialize a device's list of PM clocks.
  355. * @dev: Device to create and initialize the list of PM clocks for.
  356. *
  357. * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
  358. * members and make the @dev's power.subsys_data field point to it.
  359. */
  360. int pm_clk_create(struct device *dev)
  361. {
  362. return dev_pm_get_subsys_data(dev);
  363. }
  364. EXPORT_SYMBOL_GPL(pm_clk_create);
  365. /**
  366. * pm_clk_destroy - Destroy a device's list of power management clocks.
  367. * @dev: Device to destroy the list of PM clocks for.
  368. *
  369. * Clear the @dev's power.subsys_data field, remove the list of clock entries
  370. * from the struct pm_subsys_data object pointed to by it before and free
  371. * that object.
  372. */
  373. void pm_clk_destroy(struct device *dev)
  374. {
  375. struct pm_subsys_data *psd = dev_to_psd(dev);
  376. struct pm_clock_entry *ce, *c;
  377. struct list_head list;
  378. if (!psd)
  379. return;
  380. INIT_LIST_HEAD(&list);
  381. pm_clk_list_lock(psd);
  382. list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
  383. list_move(&ce->node, &list);
  384. psd->clock_op_might_sleep = 0;
  385. pm_clk_list_unlock(psd);
  386. dev_pm_put_subsys_data(dev);
  387. list_for_each_entry_safe_reverse(ce, c, &list, node) {
  388. list_del(&ce->node);
  389. __pm_clk_remove(ce);
  390. }
  391. }
  392. EXPORT_SYMBOL_GPL(pm_clk_destroy);
  393. static void pm_clk_destroy_action(void *data)
  394. {
  395. pm_clk_destroy(data);
  396. }
  397. int devm_pm_clk_create(struct device *dev)
  398. {
  399. int ret;
  400. ret = pm_clk_create(dev);
  401. if (ret)
  402. return ret;
  403. return devm_add_action_or_reset(dev, pm_clk_destroy_action, dev);
  404. }
  405. EXPORT_SYMBOL_GPL(devm_pm_clk_create);
  406. /**
  407. * pm_clk_suspend - Disable clocks in a device's PM clock list.
  408. * @dev: Device to disable the clocks for.
  409. */
  410. int pm_clk_suspend(struct device *dev)
  411. {
  412. struct pm_subsys_data *psd = dev_to_psd(dev);
  413. struct pm_clock_entry *ce;
  414. unsigned long flags;
  415. int ret;
  416. dev_dbg(dev, "%s()\n", __func__);
  417. if (!psd)
  418. return 0;
  419. ret = pm_clk_op_lock(psd, &flags, __func__);
  420. if (ret)
  421. return ret;
  422. list_for_each_entry_reverse(ce, &psd->clock_list, node) {
  423. if (ce->status == PCE_STATUS_ENABLED) {
  424. if (ce->enabled_when_prepared) {
  425. clk_disable_unprepare(ce->clk);
  426. ce->status = PCE_STATUS_ACQUIRED;
  427. } else {
  428. clk_disable(ce->clk);
  429. ce->status = PCE_STATUS_PREPARED;
  430. }
  431. }
  432. }
  433. pm_clk_op_unlock(psd, &flags);
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(pm_clk_suspend);
  437. /**
  438. * pm_clk_resume - Enable clocks in a device's PM clock list.
  439. * @dev: Device to enable the clocks for.
  440. */
  441. int pm_clk_resume(struct device *dev)
  442. {
  443. struct pm_subsys_data *psd = dev_to_psd(dev);
  444. struct pm_clock_entry *ce;
  445. unsigned long flags;
  446. int ret;
  447. dev_dbg(dev, "%s()\n", __func__);
  448. if (!psd)
  449. return 0;
  450. ret = pm_clk_op_lock(psd, &flags, __func__);
  451. if (ret)
  452. return ret;
  453. list_for_each_entry(ce, &psd->clock_list, node)
  454. __pm_clk_enable(dev, ce);
  455. pm_clk_op_unlock(psd, &flags);
  456. return 0;
  457. }
  458. EXPORT_SYMBOL_GPL(pm_clk_resume);
  459. /**
  460. * pm_clk_notify - Notify routine for device addition and removal.
  461. * @nb: Notifier block object this function is a member of.
  462. * @action: Operation being carried out by the caller.
  463. * @data: Device the routine is being run for.
  464. *
  465. * For this function to work, @nb must be a member of an object of type
  466. * struct pm_clk_notifier_block containing all of the requisite data.
  467. * Specifically, the pm_domain member of that object is copied to the device's
  468. * pm_domain field and its con_ids member is used to populate the device's list
  469. * of PM clocks, depending on @action.
  470. *
  471. * If the device's pm_domain field is already populated with a value different
  472. * from the one stored in the struct pm_clk_notifier_block object, the function
  473. * does nothing.
  474. */
  475. static int pm_clk_notify(struct notifier_block *nb,
  476. unsigned long action, void *data)
  477. {
  478. struct pm_clk_notifier_block *clknb;
  479. struct device *dev = data;
  480. char **con_id;
  481. int error;
  482. dev_dbg(dev, "%s() %ld\n", __func__, action);
  483. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  484. switch (action) {
  485. case BUS_NOTIFY_ADD_DEVICE:
  486. if (dev->pm_domain)
  487. break;
  488. error = pm_clk_create(dev);
  489. if (error)
  490. break;
  491. dev_pm_domain_set(dev, clknb->pm_domain);
  492. if (clknb->con_ids[0]) {
  493. for (con_id = clknb->con_ids; *con_id; con_id++)
  494. pm_clk_add(dev, *con_id);
  495. } else {
  496. pm_clk_add(dev, NULL);
  497. }
  498. break;
  499. case BUS_NOTIFY_DEL_DEVICE:
  500. if (dev->pm_domain != clknb->pm_domain)
  501. break;
  502. dev_pm_domain_set(dev, NULL);
  503. pm_clk_destroy(dev);
  504. break;
  505. }
  506. return 0;
  507. }
  508. int pm_clk_runtime_suspend(struct device *dev)
  509. {
  510. int ret;
  511. dev_dbg(dev, "%s\n", __func__);
  512. ret = pm_generic_runtime_suspend(dev);
  513. if (ret) {
  514. dev_err(dev, "failed to suspend device\n");
  515. return ret;
  516. }
  517. ret = pm_clk_suspend(dev);
  518. if (ret) {
  519. dev_err(dev, "failed to suspend clock\n");
  520. pm_generic_runtime_resume(dev);
  521. return ret;
  522. }
  523. return 0;
  524. }
  525. EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend);
  526. int pm_clk_runtime_resume(struct device *dev)
  527. {
  528. int ret;
  529. dev_dbg(dev, "%s\n", __func__);
  530. ret = pm_clk_resume(dev);
  531. if (ret) {
  532. dev_err(dev, "failed to resume clock\n");
  533. return ret;
  534. }
  535. return pm_generic_runtime_resume(dev);
  536. }
  537. EXPORT_SYMBOL_GPL(pm_clk_runtime_resume);
  538. #else /* !CONFIG_PM_CLK */
  539. /**
  540. * enable_clock - Enable a device clock.
  541. * @dev: Device whose clock is to be enabled.
  542. * @con_id: Connection ID of the clock.
  543. */
  544. static void enable_clock(struct device *dev, const char *con_id)
  545. {
  546. struct clk *clk;
  547. clk = clk_get(dev, con_id);
  548. if (!IS_ERR(clk)) {
  549. clk_prepare_enable(clk);
  550. clk_put(clk);
  551. dev_info(dev, "Runtime PM disabled, clock forced on.\n");
  552. }
  553. }
  554. /**
  555. * disable_clock - Disable a device clock.
  556. * @dev: Device whose clock is to be disabled.
  557. * @con_id: Connection ID of the clock.
  558. */
  559. static void disable_clock(struct device *dev, const char *con_id)
  560. {
  561. struct clk *clk;
  562. clk = clk_get(dev, con_id);
  563. if (!IS_ERR(clk)) {
  564. clk_disable_unprepare(clk);
  565. clk_put(clk);
  566. dev_info(dev, "Runtime PM disabled, clock forced off.\n");
  567. }
  568. }
  569. /**
  570. * pm_clk_notify - Notify routine for device addition and removal.
  571. * @nb: Notifier block object this function is a member of.
  572. * @action: Operation being carried out by the caller.
  573. * @data: Device the routine is being run for.
  574. *
  575. * For this function to work, @nb must be a member of an object of type
  576. * struct pm_clk_notifier_block containing all of the requisite data.
  577. * Specifically, the con_ids member of that object is used to enable or disable
  578. * the device's clocks, depending on @action.
  579. */
  580. static int pm_clk_notify(struct notifier_block *nb,
  581. unsigned long action, void *data)
  582. {
  583. struct pm_clk_notifier_block *clknb;
  584. struct device *dev = data;
  585. char **con_id;
  586. dev_dbg(dev, "%s() %ld\n", __func__, action);
  587. clknb = container_of(nb, struct pm_clk_notifier_block, nb);
  588. switch (action) {
  589. case BUS_NOTIFY_BIND_DRIVER:
  590. if (clknb->con_ids[0]) {
  591. for (con_id = clknb->con_ids; *con_id; con_id++)
  592. enable_clock(dev, *con_id);
  593. } else {
  594. enable_clock(dev, NULL);
  595. }
  596. break;
  597. case BUS_NOTIFY_DRIVER_NOT_BOUND:
  598. case BUS_NOTIFY_UNBOUND_DRIVER:
  599. if (clknb->con_ids[0]) {
  600. for (con_id = clknb->con_ids; *con_id; con_id++)
  601. disable_clock(dev, *con_id);
  602. } else {
  603. disable_clock(dev, NULL);
  604. }
  605. break;
  606. }
  607. return 0;
  608. }
  609. #endif /* !CONFIG_PM_CLK */
  610. /**
  611. * pm_clk_add_notifier - Add bus type notifier for power management clocks.
  612. * @bus: Bus type to add the notifier to.
  613. * @clknb: Notifier to be added to the given bus type.
  614. *
  615. * The nb member of @clknb is not expected to be initialized and its
  616. * notifier_call member will be replaced with pm_clk_notify(). However,
  617. * the remaining members of @clknb should be populated prior to calling this
  618. * routine.
  619. */
  620. void pm_clk_add_notifier(const struct bus_type *bus,
  621. struct pm_clk_notifier_block *clknb)
  622. {
  623. if (!bus || !clknb)
  624. return;
  625. clknb->nb.notifier_call = pm_clk_notify;
  626. bus_register_notifier(bus, &clknb->nb);
  627. }
  628. EXPORT_SYMBOL_GPL(pm_clk_add_notifier);