omap_device.c 31 KB

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