omap_device.c 29 KB

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