omap_device.c 31 KB

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