omap_device.c 32 KB

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