omap_device.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * omap_device implementation
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. * Paul Walmsley, Kevin Hilman
  6. *
  7. * Developed in collaboration with (alphabetical order): Benoit
  8. * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
  9. * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
  10. * Woodruff
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * This code provides a consistent interface for OMAP device drivers
  17. * to control power management and interconnect properties of their
  18. * devices.
  19. *
  20. * In the medium- to long-term, this code should either be
  21. * a) implemented via arch-specific pointers in platform_data
  22. * or
  23. * b) implemented as a proper omap_bus/omap_device in Linux, no more
  24. * platform_data func pointers
  25. *
  26. *
  27. * Guidelines for usage by driver authors:
  28. *
  29. * 1. These functions are intended to be used by device drivers via
  30. * function pointers in struct platform_data. As an example,
  31. * omap_device_enable() should be passed to the driver as
  32. *
  33. * struct foo_driver_platform_data {
  34. * ...
  35. * int (*device_enable)(struct platform_device *pdev);
  36. * ...
  37. * }
  38. *
  39. * Note that the generic "device_enable" name is used, rather than
  40. * "omap_device_enable". This is so other architectures can pass in their
  41. * own enable/disable functions here.
  42. *
  43. * This should be populated during device setup:
  44. *
  45. * ...
  46. * pdata->device_enable = omap_device_enable;
  47. * ...
  48. *
  49. * 2. Drivers should first check to ensure the function pointer is not null
  50. * before calling it, as in:
  51. *
  52. * if (pdata->device_enable)
  53. * pdata->device_enable(pdev);
  54. *
  55. * This allows other architectures that don't use similar device_enable()/
  56. * device_shutdown() functions to execute normally.
  57. *
  58. * ...
  59. *
  60. * Suggested usage by device drivers:
  61. *
  62. * During device initialization:
  63. * device_enable()
  64. *
  65. * During device idle:
  66. * (save remaining device context if necessary)
  67. * device_idle();
  68. *
  69. * During device resume:
  70. * device_enable();
  71. * (restore context if necessary)
  72. *
  73. * During device shutdown:
  74. * device_shutdown()
  75. * (device must be reinitialized at this point to use it again)
  76. *
  77. */
  78. #undef DEBUG
  79. #include <linux/kernel.h>
  80. #include <linux/platform_device.h>
  81. #include <linux/slab.h>
  82. #include <linux/err.h>
  83. #include <linux/io.h>
  84. #include <plat/omap_device.h>
  85. #include <plat/omap_hwmod.h>
  86. /* These parameters are passed to _omap_device_{de,}activate() */
  87. #define USE_WAKEUP_LAT 0
  88. #define IGNORE_WAKEUP_LAT 1
  89. /*
  90. * OMAP_DEVICE_MAGIC: used to determine whether a struct omap_device
  91. * obtained via container_of() is in fact a struct omap_device
  92. */
  93. #define OMAP_DEVICE_MAGIC 0xf00dcafe
  94. /* Private functions */
  95. /**
  96. * _omap_device_activate - increase device readiness
  97. * @od: struct omap_device *
  98. * @ignore_lat: increase to latency target (0) or full readiness (1)?
  99. *
  100. * Increase readiness of omap_device @od (thus decreasing device
  101. * wakeup latency, but consuming more power). If @ignore_lat is
  102. * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
  103. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  104. * latency is greater than the requested maximum wakeup latency, step
  105. * backwards in the omap_device_pm_latency table to ensure the
  106. * device's maximum wakeup latency is less than or equal to the
  107. * requested maximum wakeup latency. Returns 0.
  108. */
  109. static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
  110. {
  111. struct timespec a, b, c;
  112. pr_debug("omap_device: %s: activating\n", od->pdev.name);
  113. while (od->pm_lat_level > 0) {
  114. struct omap_device_pm_latency *odpl;
  115. unsigned long long act_lat = 0;
  116. od->pm_lat_level--;
  117. odpl = od->pm_lats + od->pm_lat_level;
  118. if (!ignore_lat &&
  119. (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
  120. break;
  121. read_persistent_clock(&a);
  122. /* XXX check return code */
  123. odpl->activate_func(od);
  124. read_persistent_clock(&b);
  125. c = timespec_sub(b, a);
  126. act_lat = timespec_to_ns(&c);
  127. pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
  128. "%llu nsec\n", od->pdev.name, od->pm_lat_level,
  129. act_lat);
  130. if (act_lat > odpl->activate_lat) {
  131. odpl->activate_lat_worst = act_lat;
  132. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  133. odpl->activate_lat = act_lat;
  134. pr_warning("omap_device: %s.%d: new worst case "
  135. "activate latency %d: %llu\n",
  136. od->pdev.name, od->pdev.id,
  137. od->pm_lat_level, act_lat);
  138. } else
  139. pr_warning("omap_device: %s.%d: activate "
  140. "latency %d higher than exptected. "
  141. "(%llu > %d)\n",
  142. od->pdev.name, od->pdev.id,
  143. od->pm_lat_level, act_lat,
  144. odpl->activate_lat);
  145. }
  146. od->dev_wakeup_lat -= odpl->activate_lat;
  147. }
  148. return 0;
  149. }
  150. /**
  151. * _omap_device_deactivate - decrease device readiness
  152. * @od: struct omap_device *
  153. * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
  154. *
  155. * Decrease readiness of omap_device @od (thus increasing device
  156. * wakeup latency, but conserving power). If @ignore_lat is
  157. * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
  158. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  159. * latency is less than the requested maximum wakeup latency, step
  160. * forwards in the omap_device_pm_latency table to ensure the device's
  161. * maximum wakeup latency is less than or equal to the requested
  162. * maximum wakeup latency. Returns 0.
  163. */
  164. static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
  165. {
  166. struct timespec a, b, c;
  167. pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
  168. while (od->pm_lat_level < od->pm_lats_cnt) {
  169. struct omap_device_pm_latency *odpl;
  170. unsigned long long deact_lat = 0;
  171. odpl = od->pm_lats + od->pm_lat_level;
  172. if (!ignore_lat &&
  173. ((od->dev_wakeup_lat + odpl->activate_lat) >
  174. od->_dev_wakeup_lat_limit))
  175. break;
  176. read_persistent_clock(&a);
  177. /* XXX check return code */
  178. odpl->deactivate_func(od);
  179. read_persistent_clock(&b);
  180. c = timespec_sub(b, a);
  181. deact_lat = timespec_to_ns(&c);
  182. pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
  183. "%llu nsec\n", od->pdev.name, od->pm_lat_level,
  184. deact_lat);
  185. if (deact_lat > odpl->deactivate_lat) {
  186. odpl->deactivate_lat_worst = deact_lat;
  187. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  188. odpl->deactivate_lat = deact_lat;
  189. pr_warning("omap_device: %s.%d: new worst case "
  190. "deactivate latency %d: %llu\n",
  191. od->pdev.name, od->pdev.id,
  192. od->pm_lat_level, deact_lat);
  193. } else
  194. pr_warning("omap_device: %s.%d: deactivate "
  195. "latency %d higher than exptected. "
  196. "(%llu > %d)\n",
  197. od->pdev.name, od->pdev.id,
  198. od->pm_lat_level, deact_lat,
  199. odpl->deactivate_lat);
  200. }
  201. od->dev_wakeup_lat += odpl->activate_lat;
  202. od->pm_lat_level++;
  203. }
  204. return 0;
  205. }
  206. static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
  207. {
  208. return container_of(pdev, struct omap_device, pdev);
  209. }
  210. /* Public functions for use by core code */
  211. /**
  212. * omap_device_count_resources - count number of struct resource entries needed
  213. * @od: struct omap_device *
  214. *
  215. * Count the number of struct resource entries needed for this
  216. * omap_device @od. Used by omap_device_build_ss() to determine how
  217. * much memory to allocate before calling
  218. * omap_device_fill_resources(). Returns the count.
  219. */
  220. int omap_device_count_resources(struct omap_device *od)
  221. {
  222. struct omap_hwmod *oh;
  223. int c = 0;
  224. int i;
  225. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
  226. c += omap_hwmod_count_resources(oh);
  227. pr_debug("omap_device: %s: counted %d total resources across %d "
  228. "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
  229. return c;
  230. }
  231. /**
  232. * omap_device_fill_resources - fill in array of struct resource
  233. * @od: struct omap_device *
  234. * @res: pointer to an array of struct resource to be filled in
  235. *
  236. * Populate one or more empty struct resource pointed to by @res with
  237. * the resource data for this omap_device @od. Used by
  238. * omap_device_build_ss() after calling omap_device_count_resources().
  239. * Ideally this function would not be needed at all. If omap_device
  240. * replaces platform_device, then we can specify our own
  241. * get_resource()/ get_irq()/etc functions that use the underlying
  242. * omap_hwmod information. Or if platform_device is extended to use
  243. * subarchitecture-specific function pointers, the various
  244. * platform_device functions can simply call omap_device internal
  245. * functions to get device resources. Hacking around the existing
  246. * platform_device code wastes memory. Returns 0.
  247. */
  248. int omap_device_fill_resources(struct omap_device *od, struct resource *res)
  249. {
  250. struct omap_hwmod *oh;
  251. int c = 0;
  252. int i, r;
  253. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++) {
  254. r = omap_hwmod_fill_resources(oh, res);
  255. res += r;
  256. c += r;
  257. }
  258. return 0;
  259. }
  260. /**
  261. * omap_device_build - build and register an omap_device with one omap_hwmod
  262. * @pdev_name: name of the platform_device driver to use
  263. * @pdev_id: this platform_device's connection ID
  264. * @oh: ptr to the single omap_hwmod that backs this omap_device
  265. * @pdata: platform_data ptr to associate with the platform_device
  266. * @pdata_len: amount of memory pointed to by @pdata
  267. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  268. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  269. * @is_early_device: should the device be registered as an early device or not
  270. *
  271. * Convenience function for building and registering a single
  272. * omap_device record, which in turn builds and registers a
  273. * platform_device record. See omap_device_build_ss() for more
  274. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  275. * passes along the return value of omap_device_build_ss().
  276. */
  277. struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
  278. struct omap_hwmod *oh, void *pdata,
  279. int pdata_len,
  280. struct omap_device_pm_latency *pm_lats,
  281. int pm_lats_cnt, int is_early_device)
  282. {
  283. struct omap_hwmod *ohs[] = { oh };
  284. if (!oh)
  285. return ERR_PTR(-EINVAL);
  286. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  287. pdata_len, pm_lats, pm_lats_cnt,
  288. is_early_device);
  289. }
  290. /**
  291. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  292. * @pdev_name: name of the platform_device driver to use
  293. * @pdev_id: this platform_device's connection ID
  294. * @oh: ptr to the single omap_hwmod that backs this omap_device
  295. * @pdata: platform_data ptr to associate with the platform_device
  296. * @pdata_len: amount of memory pointed to by @pdata
  297. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  298. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  299. * @is_early_device: should the device be registered as an early device or not
  300. *
  301. * Convenience function for building and registering an omap_device
  302. * subsystem record. Subsystem records consist of multiple
  303. * omap_hwmods. This function in turn builds and registers a
  304. * platform_device record. Returns an ERR_PTR() on error, or passes
  305. * along the return value of omap_device_register().
  306. */
  307. struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
  308. struct omap_hwmod **ohs, int oh_cnt,
  309. void *pdata, int pdata_len,
  310. struct omap_device_pm_latency *pm_lats,
  311. int pm_lats_cnt, int is_early_device)
  312. {
  313. int ret = -ENOMEM;
  314. struct omap_device *od;
  315. char *pdev_name2;
  316. struct resource *res = NULL;
  317. int i, res_count;
  318. struct omap_hwmod **hwmods;
  319. if (!ohs || oh_cnt == 0 || !pdev_name)
  320. return ERR_PTR(-EINVAL);
  321. if (!pdata && pdata_len > 0)
  322. return ERR_PTR(-EINVAL);
  323. pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
  324. oh_cnt);
  325. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  326. if (!od)
  327. return ERR_PTR(-ENOMEM);
  328. od->hwmods_cnt = oh_cnt;
  329. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
  330. GFP_KERNEL);
  331. if (!hwmods)
  332. goto odbs_exit1;
  333. memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
  334. od->hwmods = hwmods;
  335. pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
  336. if (!pdev_name2)
  337. goto odbs_exit2;
  338. strcpy(pdev_name2, pdev_name);
  339. od->pdev.name = pdev_name2;
  340. od->pdev.id = pdev_id;
  341. res_count = omap_device_count_resources(od);
  342. if (res_count > 0) {
  343. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  344. if (!res)
  345. goto odbs_exit3;
  346. }
  347. omap_device_fill_resources(od, res);
  348. od->pdev.num_resources = res_count;
  349. od->pdev.resource = res;
  350. ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
  351. if (ret)
  352. goto odbs_exit4;
  353. od->pm_lats = pm_lats;
  354. od->pm_lats_cnt = pm_lats_cnt;
  355. od->magic = OMAP_DEVICE_MAGIC;
  356. if (is_early_device)
  357. ret = omap_early_device_register(od);
  358. else
  359. ret = omap_device_register(od);
  360. for (i = 0; i < oh_cnt; i++)
  361. hwmods[i]->od = od;
  362. if (ret)
  363. goto odbs_exit4;
  364. return od;
  365. odbs_exit4:
  366. kfree(res);
  367. odbs_exit3:
  368. kfree(pdev_name2);
  369. odbs_exit2:
  370. kfree(hwmods);
  371. odbs_exit1:
  372. kfree(od);
  373. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  374. return ERR_PTR(ret);
  375. }
  376. /**
  377. * omap_early_device_register - register an omap_device as an early platform
  378. * device.
  379. * @od: struct omap_device * to register
  380. *
  381. * Register the omap_device structure. This currently just calls
  382. * platform_early_add_device() on the underlying platform_device.
  383. * Returns 0 by default.
  384. */
  385. int omap_early_device_register(struct omap_device *od)
  386. {
  387. struct platform_device *devices[1];
  388. devices[0] = &(od->pdev);
  389. early_platform_add_devices(devices, 1);
  390. return 0;
  391. }
  392. /**
  393. * omap_device_register - register an omap_device with one omap_hwmod
  394. * @od: struct omap_device * to register
  395. *
  396. * Register the omap_device structure. This currently just calls
  397. * platform_device_register() on the underlying platform_device.
  398. * Returns the return value of platform_device_register().
  399. */
  400. int omap_device_register(struct omap_device *od)
  401. {
  402. pr_debug("omap_device: %s: registering\n", od->pdev.name);
  403. return platform_device_register(&od->pdev);
  404. }
  405. /* Public functions for use by device drivers through struct platform_data */
  406. /**
  407. * omap_device_enable - fully activate an omap_device
  408. * @od: struct omap_device * to activate
  409. *
  410. * Do whatever is necessary for the hwmods underlying omap_device @od
  411. * to be accessible and ready to operate. This generally involves
  412. * enabling clocks, setting SYSCONFIG registers; and in the future may
  413. * involve remuxing pins. Device drivers should call this function
  414. * (through platform_data function pointers) where they would normally
  415. * enable clocks, etc. Returns -EINVAL if called when the omap_device
  416. * is already enabled, or passes along the return value of
  417. * _omap_device_activate().
  418. */
  419. int omap_device_enable(struct platform_device *pdev)
  420. {
  421. int ret;
  422. struct omap_device *od;
  423. od = _find_by_pdev(pdev);
  424. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  425. WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
  426. od->pdev.name, od->pdev.id, __func__, od->_state);
  427. return -EINVAL;
  428. }
  429. /* Enable everything if we're enabling this device from scratch */
  430. if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
  431. od->pm_lat_level = od->pm_lats_cnt;
  432. ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
  433. od->dev_wakeup_lat = 0;
  434. od->_dev_wakeup_lat_limit = UINT_MAX;
  435. od->_state = OMAP_DEVICE_STATE_ENABLED;
  436. return ret;
  437. }
  438. /**
  439. * omap_device_idle - idle an omap_device
  440. * @od: struct omap_device * to idle
  441. *
  442. * Idle omap_device @od by calling as many .deactivate_func() entries
  443. * in the omap_device's pm_lats table as is possible without exceeding
  444. * the device's maximum wakeup latency limit, pm_lat_limit. Device
  445. * drivers should call this function (through platform_data function
  446. * pointers) where they would normally disable clocks after operations
  447. * complete, etc.. Returns -EINVAL if the omap_device is not
  448. * currently enabled, or passes along the return value of
  449. * _omap_device_deactivate().
  450. */
  451. int omap_device_idle(struct platform_device *pdev)
  452. {
  453. int ret;
  454. struct omap_device *od;
  455. od = _find_by_pdev(pdev);
  456. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  457. WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
  458. od->pdev.name, od->pdev.id, __func__, od->_state);
  459. return -EINVAL;
  460. }
  461. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  462. od->_state = OMAP_DEVICE_STATE_IDLE;
  463. return ret;
  464. }
  465. /**
  466. * omap_device_shutdown - shut down an omap_device
  467. * @od: struct omap_device * to shut down
  468. *
  469. * Shut down omap_device @od by calling all .deactivate_func() entries
  470. * in the omap_device's pm_lats table and then shutting down all of
  471. * the underlying omap_hwmods. Used when a device is being "removed"
  472. * or a device driver is being unloaded. Returns -EINVAL if the
  473. * omap_device is not currently enabled or idle, or passes along the
  474. * return value of _omap_device_deactivate().
  475. */
  476. int omap_device_shutdown(struct platform_device *pdev)
  477. {
  478. int ret, i;
  479. struct omap_device *od;
  480. struct omap_hwmod *oh;
  481. od = _find_by_pdev(pdev);
  482. if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
  483. od->_state != OMAP_DEVICE_STATE_IDLE) {
  484. WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
  485. od->pdev.name, od->pdev.id, __func__, od->_state);
  486. return -EINVAL;
  487. }
  488. ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
  489. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
  490. omap_hwmod_shutdown(oh);
  491. od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
  492. return ret;
  493. }
  494. /**
  495. * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
  496. * @od: struct omap_device *
  497. *
  498. * When a device's maximum wakeup latency limit changes, call some of
  499. * the .activate_func or .deactivate_func function pointers in the
  500. * omap_device's pm_lats array to ensure that the device's maximum
  501. * wakeup latency is less than or equal to the new latency limit.
  502. * Intended to be called by OMAP PM code whenever a device's maximum
  503. * wakeup latency limit changes (e.g., via
  504. * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
  505. * done (e.g., if the omap_device is not currently idle, or if the
  506. * wakeup latency is already current with the new limit) or passes
  507. * along the return value of _omap_device_deactivate() or
  508. * _omap_device_activate().
  509. */
  510. int omap_device_align_pm_lat(struct platform_device *pdev,
  511. u32 new_wakeup_lat_limit)
  512. {
  513. int ret = -EINVAL;
  514. struct omap_device *od;
  515. od = _find_by_pdev(pdev);
  516. if (new_wakeup_lat_limit == od->dev_wakeup_lat)
  517. return 0;
  518. od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
  519. if (od->_state != OMAP_DEVICE_STATE_IDLE)
  520. return 0;
  521. else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
  522. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  523. else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
  524. ret = _omap_device_activate(od, USE_WAKEUP_LAT);
  525. return ret;
  526. }
  527. /**
  528. * omap_device_is_valid - Check if pointer is a valid omap_device
  529. * @od: struct omap_device *
  530. *
  531. * Return whether struct omap_device pointer @od points to a valid
  532. * omap_device.
  533. */
  534. bool omap_device_is_valid(struct omap_device *od)
  535. {
  536. return (od && od->magic == OMAP_DEVICE_MAGIC);
  537. }
  538. /**
  539. * omap_device_get_pwrdm - return the powerdomain * associated with @od
  540. * @od: struct omap_device *
  541. *
  542. * Return the powerdomain associated with the first underlying
  543. * omap_hwmod for this omap_device. Intended for use by core OMAP PM
  544. * code. Returns NULL on error or a struct powerdomain * upon
  545. * success.
  546. */
  547. struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
  548. {
  549. /*
  550. * XXX Assumes that all omap_hwmod powerdomains are identical.
  551. * This may not necessarily be true. There should be a sanity
  552. * check in here to WARN() if any difference appears.
  553. */
  554. if (!od->hwmods_cnt)
  555. return NULL;
  556. return omap_hwmod_get_pwrdm(od->hwmods[0]);
  557. }
  558. /**
  559. * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
  560. * @od: struct omap_device *
  561. *
  562. * Return the MPU's virtual address for the base of the hwmod, from
  563. * the ioremap() that the hwmod code does. Only valid if there is one
  564. * hwmod associated with this device. Returns NULL if there are zero
  565. * or more than one hwmods associated with this omap_device;
  566. * otherwise, passes along the return value from
  567. * omap_hwmod_get_mpu_rt_va().
  568. */
  569. void __iomem *omap_device_get_rt_va(struct omap_device *od)
  570. {
  571. if (od->hwmods_cnt != 1)
  572. return NULL;
  573. return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
  574. }
  575. /*
  576. * Public functions intended for use in omap_device_pm_latency
  577. * .activate_func and .deactivate_func function pointers
  578. */
  579. /**
  580. * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  581. * @od: struct omap_device *od
  582. *
  583. * Enable all underlying hwmods. Returns 0.
  584. */
  585. int omap_device_enable_hwmods(struct omap_device *od)
  586. {
  587. struct omap_hwmod *oh;
  588. int i;
  589. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
  590. omap_hwmod_enable(oh);
  591. /* XXX pass along return value here? */
  592. return 0;
  593. }
  594. /**
  595. * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  596. * @od: struct omap_device *od
  597. *
  598. * Idle all underlying hwmods. Returns 0.
  599. */
  600. int omap_device_idle_hwmods(struct omap_device *od)
  601. {
  602. struct omap_hwmod *oh;
  603. int i;
  604. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
  605. omap_hwmod_idle(oh);
  606. /* XXX pass along return value here? */
  607. return 0;
  608. }
  609. /**
  610. * omap_device_disable_clocks - disable all main and interface clocks
  611. * @od: struct omap_device *od
  612. *
  613. * Disable the main functional clock and interface clock for all of the
  614. * omap_hwmods associated with the omap_device. Returns 0.
  615. */
  616. int omap_device_disable_clocks(struct omap_device *od)
  617. {
  618. struct omap_hwmod *oh;
  619. int i;
  620. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
  621. omap_hwmod_disable_clocks(oh);
  622. /* XXX pass along return value here? */
  623. return 0;
  624. }
  625. /**
  626. * omap_device_enable_clocks - enable all main and interface clocks
  627. * @od: struct omap_device *od
  628. *
  629. * Enable the main functional clock and interface clock for all of the
  630. * omap_hwmods associated with the omap_device. Returns 0.
  631. */
  632. int omap_device_enable_clocks(struct omap_device *od)
  633. {
  634. struct omap_hwmod *oh;
  635. int i;
  636. for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
  637. omap_hwmod_enable_clocks(oh);
  638. /* XXX pass along return value here? */
  639. return 0;
  640. }