omap_device.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 <linux/clk.h>
  85. #include <linux/clkdev.h>
  86. #include <linux/pm_runtime.h>
  87. #include <plat/omap_device.h>
  88. #include <plat/omap_hwmod.h>
  89. #include <plat/clock.h>
  90. /* These parameters are passed to _omap_device_{de,}activate() */
  91. #define USE_WAKEUP_LAT 0
  92. #define IGNORE_WAKEUP_LAT 1
  93. /* Private functions */
  94. /**
  95. * _omap_device_activate - increase device readiness
  96. * @od: struct omap_device *
  97. * @ignore_lat: increase to latency target (0) or full readiness (1)?
  98. *
  99. * Increase readiness of omap_device @od (thus decreasing device
  100. * wakeup latency, but consuming more power). If @ignore_lat is
  101. * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
  102. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  103. * latency is greater than the requested maximum wakeup latency, step
  104. * backwards in the omap_device_pm_latency table to ensure the
  105. * device's maximum wakeup latency is less than or equal to the
  106. * requested maximum wakeup latency. Returns 0.
  107. */
  108. static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
  109. {
  110. struct timespec a, b, c;
  111. pr_debug("omap_device: %s: activating\n", od->pdev.name);
  112. while (od->pm_lat_level > 0) {
  113. struct omap_device_pm_latency *odpl;
  114. unsigned long long act_lat = 0;
  115. od->pm_lat_level--;
  116. odpl = od->pm_lats + od->pm_lat_level;
  117. if (!ignore_lat &&
  118. (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
  119. break;
  120. read_persistent_clock(&a);
  121. /* XXX check return code */
  122. odpl->activate_func(od);
  123. read_persistent_clock(&b);
  124. c = timespec_sub(b, a);
  125. act_lat = timespec_to_ns(&c);
  126. pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
  127. "%llu nsec\n", od->pdev.name, od->pm_lat_level,
  128. act_lat);
  129. if (act_lat > odpl->activate_lat) {
  130. odpl->activate_lat_worst = act_lat;
  131. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  132. odpl->activate_lat = act_lat;
  133. pr_warning("omap_device: %s.%d: new worst case "
  134. "activate latency %d: %llu\n",
  135. od->pdev.name, od->pdev.id,
  136. od->pm_lat_level, act_lat);
  137. } else
  138. pr_warning("omap_device: %s.%d: activate "
  139. "latency %d higher than exptected. "
  140. "(%llu > %d)\n",
  141. od->pdev.name, od->pdev.id,
  142. od->pm_lat_level, act_lat,
  143. odpl->activate_lat);
  144. }
  145. od->dev_wakeup_lat -= odpl->activate_lat;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * _omap_device_deactivate - decrease device readiness
  151. * @od: struct omap_device *
  152. * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
  153. *
  154. * Decrease readiness of omap_device @od (thus increasing device
  155. * wakeup latency, but conserving power). If @ignore_lat is
  156. * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
  157. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  158. * latency is less than the requested maximum wakeup latency, step
  159. * forwards in the omap_device_pm_latency table to ensure the device's
  160. * maximum wakeup latency is less than or equal to the requested
  161. * maximum wakeup latency. Returns 0.
  162. */
  163. static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
  164. {
  165. struct timespec a, b, c;
  166. pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
  167. while (od->pm_lat_level < od->pm_lats_cnt) {
  168. struct omap_device_pm_latency *odpl;
  169. unsigned long long deact_lat = 0;
  170. odpl = od->pm_lats + od->pm_lat_level;
  171. if (!ignore_lat &&
  172. ((od->dev_wakeup_lat + odpl->activate_lat) >
  173. od->_dev_wakeup_lat_limit))
  174. break;
  175. read_persistent_clock(&a);
  176. /* XXX check return code */
  177. odpl->deactivate_func(od);
  178. read_persistent_clock(&b);
  179. c = timespec_sub(b, a);
  180. deact_lat = timespec_to_ns(&c);
  181. pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
  182. "%llu nsec\n", od->pdev.name, od->pm_lat_level,
  183. deact_lat);
  184. if (deact_lat > odpl->deactivate_lat) {
  185. odpl->deactivate_lat_worst = deact_lat;
  186. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  187. odpl->deactivate_lat = deact_lat;
  188. pr_warning("omap_device: %s.%d: new worst case "
  189. "deactivate latency %d: %llu\n",
  190. od->pdev.name, od->pdev.id,
  191. od->pm_lat_level, deact_lat);
  192. } else
  193. pr_warning("omap_device: %s.%d: deactivate "
  194. "latency %d higher than exptected. "
  195. "(%llu > %d)\n",
  196. od->pdev.name, od->pdev.id,
  197. od->pm_lat_level, deact_lat,
  198. odpl->deactivate_lat);
  199. }
  200. od->dev_wakeup_lat += odpl->activate_lat;
  201. od->pm_lat_level++;
  202. }
  203. return 0;
  204. }
  205. /**
  206. * _add_optional_clock_clkdev - Add clkdev entry for hwmod optional clocks
  207. * @od: struct omap_device *od
  208. *
  209. * For every optional clock present per hwmod per omap_device, this function
  210. * adds an entry in the clkdev table of the form <dev-id=dev_name, con-id=role>
  211. * if it does not exist already.
  212. *
  213. * The function is called from inside omap_device_build_ss(), after
  214. * omap_device_register.
  215. *
  216. * This allows drivers to get a pointer to its optional clocks based on its role
  217. * by calling clk_get(<dev*>, <role>).
  218. *
  219. * No return value.
  220. */
  221. static void _add_optional_clock_clkdev(struct omap_device *od,
  222. struct omap_hwmod *oh)
  223. {
  224. int i;
  225. for (i = 0; i < oh->opt_clks_cnt; i++) {
  226. struct omap_hwmod_opt_clk *oc;
  227. struct clk *r;
  228. struct clk_lookup *l;
  229. oc = &oh->opt_clks[i];
  230. if (!oc->_clk)
  231. continue;
  232. r = clk_get_sys(dev_name(&od->pdev.dev), oc->role);
  233. if (!IS_ERR(r))
  234. continue; /* clkdev entry exists */
  235. r = omap_clk_get_by_name((char *)oc->clk);
  236. if (IS_ERR(r)) {
  237. pr_err("omap_device: %s: omap_clk_get_by_name for %s failed\n",
  238. dev_name(&od->pdev.dev), oc->clk);
  239. continue;
  240. }
  241. l = clkdev_alloc(r, oc->role, dev_name(&od->pdev.dev));
  242. if (!l) {
  243. pr_err("omap_device: %s: clkdev_alloc for %s failed\n",
  244. dev_name(&od->pdev.dev), oc->role);
  245. return;
  246. }
  247. clkdev_add(l);
  248. }
  249. }
  250. /* Public functions for use by core code */
  251. /**
  252. * omap_device_get_context_loss_count - get lost context count
  253. * @od: struct omap_device *
  254. *
  255. * Using the primary hwmod, query the context loss count for this
  256. * device.
  257. *
  258. * Callers should consider context for this device lost any time this
  259. * function returns a value different than the value the caller got
  260. * the last time it called this function.
  261. *
  262. * If any hwmods exist for the omap_device assoiated with @pdev,
  263. * return the context loss counter for that hwmod, otherwise return
  264. * zero.
  265. */
  266. u32 omap_device_get_context_loss_count(struct platform_device *pdev)
  267. {
  268. struct omap_device *od;
  269. u32 ret = 0;
  270. od = to_omap_device(pdev);
  271. if (od->hwmods_cnt)
  272. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  273. return ret;
  274. }
  275. /**
  276. * omap_device_count_resources - count number of struct resource entries needed
  277. * @od: struct omap_device *
  278. *
  279. * Count the number of struct resource entries needed for this
  280. * omap_device @od. Used by omap_device_build_ss() to determine how
  281. * much memory to allocate before calling
  282. * omap_device_fill_resources(). Returns the count.
  283. */
  284. int omap_device_count_resources(struct omap_device *od)
  285. {
  286. int c = 0;
  287. int i;
  288. for (i = 0; i < od->hwmods_cnt; i++)
  289. c += omap_hwmod_count_resources(od->hwmods[i]);
  290. pr_debug("omap_device: %s: counted %d total resources across %d "
  291. "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
  292. return c;
  293. }
  294. /**
  295. * omap_device_fill_resources - fill in array of struct resource
  296. * @od: struct omap_device *
  297. * @res: pointer to an array of struct resource to be filled in
  298. *
  299. * Populate one or more empty struct resource pointed to by @res with
  300. * the resource data for this omap_device @od. Used by
  301. * omap_device_build_ss() after calling omap_device_count_resources().
  302. * Ideally this function would not be needed at all. If omap_device
  303. * replaces platform_device, then we can specify our own
  304. * get_resource()/ get_irq()/etc functions that use the underlying
  305. * omap_hwmod information. Or if platform_device is extended to use
  306. * subarchitecture-specific function pointers, the various
  307. * platform_device functions can simply call omap_device internal
  308. * functions to get device resources. Hacking around the existing
  309. * platform_device code wastes memory. Returns 0.
  310. */
  311. int omap_device_fill_resources(struct omap_device *od, struct resource *res)
  312. {
  313. int c = 0;
  314. int i, r;
  315. for (i = 0; i < od->hwmods_cnt; i++) {
  316. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  317. res += r;
  318. c += r;
  319. }
  320. return 0;
  321. }
  322. /**
  323. * omap_device_build - build and register an omap_device with one omap_hwmod
  324. * @pdev_name: name of the platform_device driver to use
  325. * @pdev_id: this platform_device's connection ID
  326. * @oh: ptr to the single omap_hwmod that backs this omap_device
  327. * @pdata: platform_data ptr to associate with the platform_device
  328. * @pdata_len: amount of memory pointed to by @pdata
  329. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  330. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  331. * @is_early_device: should the device be registered as an early device or not
  332. *
  333. * Convenience function for building and registering a single
  334. * omap_device record, which in turn builds and registers a
  335. * platform_device record. See omap_device_build_ss() for more
  336. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  337. * passes along the return value of omap_device_build_ss().
  338. */
  339. struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
  340. struct omap_hwmod *oh, void *pdata,
  341. int pdata_len,
  342. struct omap_device_pm_latency *pm_lats,
  343. int pm_lats_cnt, int is_early_device)
  344. {
  345. struct omap_hwmod *ohs[] = { oh };
  346. if (!oh)
  347. return ERR_PTR(-EINVAL);
  348. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  349. pdata_len, pm_lats, pm_lats_cnt,
  350. is_early_device);
  351. }
  352. /**
  353. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  354. * @pdev_name: name of the platform_device driver to use
  355. * @pdev_id: this platform_device's connection ID
  356. * @oh: ptr to the single omap_hwmod that backs this omap_device
  357. * @pdata: platform_data ptr to associate with the platform_device
  358. * @pdata_len: amount of memory pointed to by @pdata
  359. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  360. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  361. * @is_early_device: should the device be registered as an early device or not
  362. *
  363. * Convenience function for building and registering an omap_device
  364. * subsystem record. Subsystem records consist of multiple
  365. * omap_hwmods. This function in turn builds and registers a
  366. * platform_device record. Returns an ERR_PTR() on error, or passes
  367. * along the return value of omap_device_register().
  368. */
  369. struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
  370. struct omap_hwmod **ohs, int oh_cnt,
  371. void *pdata, int pdata_len,
  372. struct omap_device_pm_latency *pm_lats,
  373. int pm_lats_cnt, int is_early_device)
  374. {
  375. int ret = -ENOMEM;
  376. struct omap_device *od;
  377. char *pdev_name2;
  378. struct resource *res = NULL;
  379. int i, res_count;
  380. struct omap_hwmod **hwmods;
  381. if (!ohs || oh_cnt == 0 || !pdev_name)
  382. return ERR_PTR(-EINVAL);
  383. if (!pdata && pdata_len > 0)
  384. return ERR_PTR(-EINVAL);
  385. pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
  386. oh_cnt);
  387. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  388. if (!od)
  389. return ERR_PTR(-ENOMEM);
  390. od->hwmods_cnt = oh_cnt;
  391. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
  392. GFP_KERNEL);
  393. if (!hwmods)
  394. goto odbs_exit1;
  395. memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
  396. od->hwmods = hwmods;
  397. pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
  398. if (!pdev_name2)
  399. goto odbs_exit2;
  400. strcpy(pdev_name2, pdev_name);
  401. od->pdev.name = pdev_name2;
  402. od->pdev.id = pdev_id;
  403. res_count = omap_device_count_resources(od);
  404. if (res_count > 0) {
  405. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  406. if (!res)
  407. goto odbs_exit3;
  408. }
  409. omap_device_fill_resources(od, res);
  410. od->pdev.num_resources = res_count;
  411. od->pdev.resource = res;
  412. ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
  413. if (ret)
  414. goto odbs_exit4;
  415. od->pm_lats = pm_lats;
  416. od->pm_lats_cnt = pm_lats_cnt;
  417. if (is_early_device)
  418. ret = omap_early_device_register(od);
  419. else
  420. ret = omap_device_register(od);
  421. for (i = 0; i < oh_cnt; i++) {
  422. hwmods[i]->od = od;
  423. _add_optional_clock_clkdev(od, hwmods[i]);
  424. }
  425. if (ret)
  426. goto odbs_exit4;
  427. return od;
  428. odbs_exit4:
  429. kfree(res);
  430. odbs_exit3:
  431. kfree(pdev_name2);
  432. odbs_exit2:
  433. kfree(hwmods);
  434. odbs_exit1:
  435. kfree(od);
  436. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  437. return ERR_PTR(ret);
  438. }
  439. /**
  440. * omap_early_device_register - register an omap_device as an early platform
  441. * device.
  442. * @od: struct omap_device * to register
  443. *
  444. * Register the omap_device structure. This currently just calls
  445. * platform_early_add_device() on the underlying platform_device.
  446. * Returns 0 by default.
  447. */
  448. int omap_early_device_register(struct omap_device *od)
  449. {
  450. struct platform_device *devices[1];
  451. devices[0] = &(od->pdev);
  452. early_platform_add_devices(devices, 1);
  453. return 0;
  454. }
  455. static int _od_runtime_suspend(struct device *dev)
  456. {
  457. struct platform_device *pdev = to_platform_device(dev);
  458. int ret;
  459. ret = pm_generic_runtime_suspend(dev);
  460. if (!ret)
  461. omap_device_idle(pdev);
  462. return ret;
  463. }
  464. static int _od_runtime_idle(struct device *dev)
  465. {
  466. return pm_generic_runtime_idle(dev);
  467. }
  468. static int _od_runtime_resume(struct device *dev)
  469. {
  470. struct platform_device *pdev = to_platform_device(dev);
  471. omap_device_enable(pdev);
  472. return pm_generic_runtime_resume(dev);
  473. }
  474. static struct dev_power_domain omap_device_power_domain = {
  475. .ops = {
  476. .runtime_suspend = _od_runtime_suspend,
  477. .runtime_idle = _od_runtime_idle,
  478. .runtime_resume = _od_runtime_resume,
  479. USE_PLATFORM_PM_SLEEP_OPS
  480. }
  481. };
  482. /**
  483. * omap_device_register - register an omap_device with one omap_hwmod
  484. * @od: struct omap_device * to register
  485. *
  486. * Register the omap_device structure. This currently just calls
  487. * platform_device_register() on the underlying platform_device.
  488. * Returns the return value of platform_device_register().
  489. */
  490. int omap_device_register(struct omap_device *od)
  491. {
  492. pr_debug("omap_device: %s: registering\n", od->pdev.name);
  493. od->pdev.dev.parent = &omap_device_parent;
  494. od->pdev.dev.pwr_domain = &omap_device_power_domain;
  495. return platform_device_register(&od->pdev);
  496. }
  497. /* Public functions for use by device drivers through struct platform_data */
  498. /**
  499. * omap_device_enable - fully activate an omap_device
  500. * @od: struct omap_device * to activate
  501. *
  502. * Do whatever is necessary for the hwmods underlying omap_device @od
  503. * to be accessible and ready to operate. This generally involves
  504. * enabling clocks, setting SYSCONFIG registers; and in the future may
  505. * involve remuxing pins. Device drivers should call this function
  506. * (through platform_data function pointers) where they would normally
  507. * enable clocks, etc. Returns -EINVAL if called when the omap_device
  508. * is already enabled, or passes along the return value of
  509. * _omap_device_activate().
  510. */
  511. int omap_device_enable(struct platform_device *pdev)
  512. {
  513. int ret;
  514. struct omap_device *od;
  515. od = to_omap_device(pdev);
  516. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  517. WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
  518. od->pdev.name, od->pdev.id, __func__, od->_state);
  519. return -EINVAL;
  520. }
  521. /* Enable everything if we're enabling this device from scratch */
  522. if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
  523. od->pm_lat_level = od->pm_lats_cnt;
  524. ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
  525. od->dev_wakeup_lat = 0;
  526. od->_dev_wakeup_lat_limit = UINT_MAX;
  527. od->_state = OMAP_DEVICE_STATE_ENABLED;
  528. return ret;
  529. }
  530. /**
  531. * omap_device_idle - idle an omap_device
  532. * @od: struct omap_device * to idle
  533. *
  534. * Idle omap_device @od by calling as many .deactivate_func() entries
  535. * in the omap_device's pm_lats table as is possible without exceeding
  536. * the device's maximum wakeup latency limit, pm_lat_limit. Device
  537. * drivers should call this function (through platform_data function
  538. * pointers) where they would normally disable clocks after operations
  539. * complete, etc.. Returns -EINVAL if the omap_device is not
  540. * currently enabled, or passes along the return value of
  541. * _omap_device_deactivate().
  542. */
  543. int omap_device_idle(struct platform_device *pdev)
  544. {
  545. int ret;
  546. struct omap_device *od;
  547. od = to_omap_device(pdev);
  548. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  549. WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
  550. od->pdev.name, od->pdev.id, __func__, od->_state);
  551. return -EINVAL;
  552. }
  553. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  554. od->_state = OMAP_DEVICE_STATE_IDLE;
  555. return ret;
  556. }
  557. /**
  558. * omap_device_shutdown - shut down an omap_device
  559. * @od: struct omap_device * to shut down
  560. *
  561. * Shut down omap_device @od by calling all .deactivate_func() entries
  562. * in the omap_device's pm_lats table and then shutting down all of
  563. * the underlying omap_hwmods. Used when a device is being "removed"
  564. * or a device driver is being unloaded. Returns -EINVAL if the
  565. * omap_device is not currently enabled or idle, or passes along the
  566. * return value of _omap_device_deactivate().
  567. */
  568. int omap_device_shutdown(struct platform_device *pdev)
  569. {
  570. int ret, i;
  571. struct omap_device *od;
  572. od = to_omap_device(pdev);
  573. if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
  574. od->_state != OMAP_DEVICE_STATE_IDLE) {
  575. WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
  576. od->pdev.name, od->pdev.id, __func__, od->_state);
  577. return -EINVAL;
  578. }
  579. ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
  580. for (i = 0; i < od->hwmods_cnt; i++)
  581. omap_hwmod_shutdown(od->hwmods[i]);
  582. od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
  583. return ret;
  584. }
  585. /**
  586. * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
  587. * @od: struct omap_device *
  588. *
  589. * When a device's maximum wakeup latency limit changes, call some of
  590. * the .activate_func or .deactivate_func function pointers in the
  591. * omap_device's pm_lats array to ensure that the device's maximum
  592. * wakeup latency is less than or equal to the new latency limit.
  593. * Intended to be called by OMAP PM code whenever a device's maximum
  594. * wakeup latency limit changes (e.g., via
  595. * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
  596. * done (e.g., if the omap_device is not currently idle, or if the
  597. * wakeup latency is already current with the new limit) or passes
  598. * along the return value of _omap_device_deactivate() or
  599. * _omap_device_activate().
  600. */
  601. int omap_device_align_pm_lat(struct platform_device *pdev,
  602. u32 new_wakeup_lat_limit)
  603. {
  604. int ret = -EINVAL;
  605. struct omap_device *od;
  606. od = to_omap_device(pdev);
  607. if (new_wakeup_lat_limit == od->dev_wakeup_lat)
  608. return 0;
  609. od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
  610. if (od->_state != OMAP_DEVICE_STATE_IDLE)
  611. return 0;
  612. else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
  613. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  614. else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
  615. ret = _omap_device_activate(od, USE_WAKEUP_LAT);
  616. return ret;
  617. }
  618. /**
  619. * omap_device_get_pwrdm - return the powerdomain * associated with @od
  620. * @od: struct omap_device *
  621. *
  622. * Return the powerdomain associated with the first underlying
  623. * omap_hwmod for this omap_device. Intended for use by core OMAP PM
  624. * code. Returns NULL on error or a struct powerdomain * upon
  625. * success.
  626. */
  627. struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
  628. {
  629. /*
  630. * XXX Assumes that all omap_hwmod powerdomains are identical.
  631. * This may not necessarily be true. There should be a sanity
  632. * check in here to WARN() if any difference appears.
  633. */
  634. if (!od->hwmods_cnt)
  635. return NULL;
  636. return omap_hwmod_get_pwrdm(od->hwmods[0]);
  637. }
  638. /**
  639. * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
  640. * @od: struct omap_device *
  641. *
  642. * Return the MPU's virtual address for the base of the hwmod, from
  643. * the ioremap() that the hwmod code does. Only valid if there is one
  644. * hwmod associated with this device. Returns NULL if there are zero
  645. * or more than one hwmods associated with this omap_device;
  646. * otherwise, passes along the return value from
  647. * omap_hwmod_get_mpu_rt_va().
  648. */
  649. void __iomem *omap_device_get_rt_va(struct omap_device *od)
  650. {
  651. if (od->hwmods_cnt != 1)
  652. return NULL;
  653. return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
  654. }
  655. /*
  656. * Public functions intended for use in omap_device_pm_latency
  657. * .activate_func and .deactivate_func function pointers
  658. */
  659. /**
  660. * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  661. * @od: struct omap_device *od
  662. *
  663. * Enable all underlying hwmods. Returns 0.
  664. */
  665. int omap_device_enable_hwmods(struct omap_device *od)
  666. {
  667. int i;
  668. for (i = 0; i < od->hwmods_cnt; i++)
  669. omap_hwmod_enable(od->hwmods[i]);
  670. /* XXX pass along return value here? */
  671. return 0;
  672. }
  673. /**
  674. * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  675. * @od: struct omap_device *od
  676. *
  677. * Idle all underlying hwmods. Returns 0.
  678. */
  679. int omap_device_idle_hwmods(struct omap_device *od)
  680. {
  681. int i;
  682. for (i = 0; i < od->hwmods_cnt; i++)
  683. omap_hwmod_idle(od->hwmods[i]);
  684. /* XXX pass along return value here? */
  685. return 0;
  686. }
  687. /**
  688. * omap_device_disable_clocks - disable all main and interface clocks
  689. * @od: struct omap_device *od
  690. *
  691. * Disable the main functional clock and interface clock for all of the
  692. * omap_hwmods associated with the omap_device. Returns 0.
  693. */
  694. int omap_device_disable_clocks(struct omap_device *od)
  695. {
  696. int i;
  697. for (i = 0; i < od->hwmods_cnt; i++)
  698. omap_hwmod_disable_clocks(od->hwmods[i]);
  699. /* XXX pass along return value here? */
  700. return 0;
  701. }
  702. /**
  703. * omap_device_enable_clocks - enable all main and interface clocks
  704. * @od: struct omap_device *od
  705. *
  706. * Enable the main functional clock and interface clock for all of the
  707. * omap_hwmods associated with the omap_device. Returns 0.
  708. */
  709. int omap_device_enable_clocks(struct omap_device *od)
  710. {
  711. int i;
  712. for (i = 0; i < od->hwmods_cnt; i++)
  713. omap_hwmod_enable_clocks(od->hwmods[i]);
  714. /* XXX pass along return value here? */
  715. return 0;
  716. }
  717. struct device omap_device_parent = {
  718. .init_name = "omap",
  719. .parent = &platform_bus,
  720. };
  721. static int __init omap_device_init(void)
  722. {
  723. return device_register(&omap_device_parent);
  724. }
  725. core_initcall(omap_device_init);