omap_device.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 be implemented as a
  21. * proper omap_bus/omap_device in Linux, no more platform_data func
  22. * pointers
  23. *
  24. *
  25. */
  26. #undef DEBUG
  27. #include <linux/kernel.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/err.h>
  31. #include <linux/io.h>
  32. #include <linux/clk.h>
  33. #include <linux/clkdev.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/of.h>
  36. #include <linux/notifier.h>
  37. #include "common.h"
  38. #include "soc.h"
  39. #include "omap_device.h"
  40. #include "omap_hwmod.h"
  41. /* Private functions */
  42. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  43. const char *clk_name)
  44. {
  45. struct clk *r;
  46. struct clk_lookup *l;
  47. if (!clk_alias || !clk_name)
  48. return;
  49. dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
  50. r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
  51. if (!IS_ERR(r)) {
  52. dev_dbg(&od->pdev->dev,
  53. "alias %s already exists\n", clk_alias);
  54. clk_put(r);
  55. return;
  56. }
  57. r = clk_get(NULL, clk_name);
  58. if (IS_ERR(r)) {
  59. dev_err(&od->pdev->dev,
  60. "clk_get for %s failed\n", clk_name);
  61. return;
  62. }
  63. l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
  64. if (!l) {
  65. dev_err(&od->pdev->dev,
  66. "clkdev_alloc for %s failed\n", clk_alias);
  67. return;
  68. }
  69. clkdev_add(l);
  70. }
  71. /**
  72. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  73. * and main clock
  74. * @od: struct omap_device *od
  75. * @oh: struct omap_hwmod *oh
  76. *
  77. * For the main clock and every optional clock present per hwmod per
  78. * omap_device, this function adds an entry in the clkdev table of the
  79. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  80. *
  81. * The function is called from inside omap_device_build_ss(), after
  82. * omap_device_register.
  83. *
  84. * This allows drivers to get a pointer to its optional clocks based on its role
  85. * by calling clk_get(<dev*>, <role>).
  86. * In the case of the main clock, a "fck" alias is used.
  87. *
  88. * No return value.
  89. */
  90. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  91. struct omap_hwmod *oh)
  92. {
  93. int i;
  94. _add_clkdev(od, "fck", oh->main_clk);
  95. for (i = 0; i < oh->opt_clks_cnt; i++)
  96. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  97. }
  98. /**
  99. * omap_device_build_from_dt - build an omap_device with multiple hwmods
  100. * @pdev_name: name of the platform_device driver to use
  101. * @pdev_id: this platform_device's connection ID
  102. * @oh: ptr to the single omap_hwmod that backs this omap_device
  103. * @pdata: platform_data ptr to associate with the platform_device
  104. * @pdata_len: amount of memory pointed to by @pdata
  105. *
  106. * Function for building an omap_device already registered from device-tree
  107. *
  108. * Returns 0 or PTR_ERR() on error.
  109. */
  110. static int omap_device_build_from_dt(struct platform_device *pdev)
  111. {
  112. struct omap_hwmod **hwmods;
  113. struct omap_device *od;
  114. struct omap_hwmod *oh;
  115. struct device_node *node = pdev->dev.of_node;
  116. const char *oh_name;
  117. int oh_cnt, i, ret = 0;
  118. bool device_active = false;
  119. oh_cnt = of_property_count_strings(node, "ti,hwmods");
  120. if (oh_cnt <= 0) {
  121. dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
  122. return -ENODEV;
  123. }
  124. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  125. if (!hwmods) {
  126. ret = -ENOMEM;
  127. goto odbfd_exit;
  128. }
  129. for (i = 0; i < oh_cnt; i++) {
  130. of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
  131. oh = omap_hwmod_lookup(oh_name);
  132. if (!oh) {
  133. dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
  134. oh_name);
  135. ret = -EINVAL;
  136. goto odbfd_exit1;
  137. }
  138. hwmods[i] = oh;
  139. if (oh->flags & HWMOD_INIT_NO_IDLE)
  140. device_active = true;
  141. }
  142. od = omap_device_alloc(pdev, hwmods, oh_cnt);
  143. if (IS_ERR(od)) {
  144. dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
  145. oh_name);
  146. ret = PTR_ERR(od);
  147. goto odbfd_exit1;
  148. }
  149. /* Fix up missing resource names */
  150. for (i = 0; i < pdev->num_resources; i++) {
  151. struct resource *r = &pdev->resource[i];
  152. if (r->name == NULL)
  153. r->name = dev_name(&pdev->dev);
  154. }
  155. pdev->dev.pm_domain = &omap_device_pm_domain;
  156. if (device_active) {
  157. omap_device_enable(pdev);
  158. pm_runtime_set_active(&pdev->dev);
  159. }
  160. odbfd_exit1:
  161. kfree(hwmods);
  162. odbfd_exit:
  163. /* if data/we are at fault.. load up a fail handler */
  164. if (ret)
  165. pdev->dev.pm_domain = &omap_device_fail_pm_domain;
  166. return ret;
  167. }
  168. static int _omap_device_notifier_call(struct notifier_block *nb,
  169. unsigned long event, void *dev)
  170. {
  171. struct platform_device *pdev = to_platform_device(dev);
  172. struct omap_device *od;
  173. switch (event) {
  174. case BUS_NOTIFY_DEL_DEVICE:
  175. if (pdev->archdata.od)
  176. omap_device_delete(pdev->archdata.od);
  177. break;
  178. case BUS_NOTIFY_ADD_DEVICE:
  179. if (pdev->dev.of_node)
  180. omap_device_build_from_dt(pdev);
  181. omap_auxdata_legacy_init(dev);
  182. /* fall through */
  183. default:
  184. od = to_omap_device(pdev);
  185. if (od)
  186. od->_driver_status = event;
  187. }
  188. return NOTIFY_DONE;
  189. }
  190. /**
  191. * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  192. * @od: struct omap_device *od
  193. *
  194. * Enable all underlying hwmods. Returns 0.
  195. */
  196. static int _omap_device_enable_hwmods(struct omap_device *od)
  197. {
  198. int i;
  199. for (i = 0; i < od->hwmods_cnt; i++)
  200. omap_hwmod_enable(od->hwmods[i]);
  201. /* XXX pass along return value here? */
  202. return 0;
  203. }
  204. /**
  205. * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  206. * @od: struct omap_device *od
  207. *
  208. * Idle all underlying hwmods. Returns 0.
  209. */
  210. static int _omap_device_idle_hwmods(struct omap_device *od)
  211. {
  212. int i;
  213. for (i = 0; i < od->hwmods_cnt; i++)
  214. omap_hwmod_idle(od->hwmods[i]);
  215. /* XXX pass along return value here? */
  216. return 0;
  217. }
  218. /* Public functions for use by core code */
  219. /**
  220. * omap_device_get_context_loss_count - get lost context count
  221. * @od: struct omap_device *
  222. *
  223. * Using the primary hwmod, query the context loss count for this
  224. * device.
  225. *
  226. * Callers should consider context for this device lost any time this
  227. * function returns a value different than the value the caller got
  228. * the last time it called this function.
  229. *
  230. * If any hwmods exist for the omap_device assoiated with @pdev,
  231. * return the context loss counter for that hwmod, otherwise return
  232. * zero.
  233. */
  234. int omap_device_get_context_loss_count(struct platform_device *pdev)
  235. {
  236. struct omap_device *od;
  237. u32 ret = 0;
  238. od = to_omap_device(pdev);
  239. if (od->hwmods_cnt)
  240. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  241. return ret;
  242. }
  243. /**
  244. * omap_device_count_resources - count number of struct resource entries needed
  245. * @od: struct omap_device *
  246. * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
  247. *
  248. * Count the number of struct resource entries needed for this
  249. * omap_device @od. Used by omap_device_build_ss() to determine how
  250. * much memory to allocate before calling
  251. * omap_device_fill_resources(). Returns the count.
  252. */
  253. static int omap_device_count_resources(struct omap_device *od,
  254. unsigned long flags)
  255. {
  256. int c = 0;
  257. int i;
  258. for (i = 0; i < od->hwmods_cnt; i++)
  259. c += omap_hwmod_count_resources(od->hwmods[i], flags);
  260. pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
  261. od->pdev->name, c, od->hwmods_cnt);
  262. return c;
  263. }
  264. /**
  265. * omap_device_fill_resources - fill in array of struct resource
  266. * @od: struct omap_device *
  267. * @res: pointer to an array of struct resource to be filled in
  268. *
  269. * Populate one or more empty struct resource pointed to by @res with
  270. * the resource data for this omap_device @od. Used by
  271. * omap_device_build_ss() after calling omap_device_count_resources().
  272. * Ideally this function would not be needed at all. If omap_device
  273. * replaces platform_device, then we can specify our own
  274. * get_resource()/ get_irq()/etc functions that use the underlying
  275. * omap_hwmod information. Or if platform_device is extended to use
  276. * subarchitecture-specific function pointers, the various
  277. * platform_device functions can simply call omap_device internal
  278. * functions to get device resources. Hacking around the existing
  279. * platform_device code wastes memory. Returns 0.
  280. */
  281. static int omap_device_fill_resources(struct omap_device *od,
  282. struct resource *res)
  283. {
  284. int i, r;
  285. for (i = 0; i < od->hwmods_cnt; i++) {
  286. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  287. res += r;
  288. }
  289. return 0;
  290. }
  291. /**
  292. * _od_fill_dma_resources - fill in array of struct resource with dma resources
  293. * @od: struct omap_device *
  294. * @res: pointer to an array of struct resource to be filled in
  295. *
  296. * Populate one or more empty struct resource pointed to by @res with
  297. * the dma resource data for this omap_device @od. Used by
  298. * omap_device_alloc() after calling omap_device_count_resources().
  299. *
  300. * Ideally this function would not be needed at all. If we have
  301. * mechanism to get dma resources from DT.
  302. *
  303. * Returns 0.
  304. */
  305. static int _od_fill_dma_resources(struct omap_device *od,
  306. struct resource *res)
  307. {
  308. int i, r;
  309. for (i = 0; i < od->hwmods_cnt; i++) {
  310. r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
  311. res += r;
  312. }
  313. return 0;
  314. }
  315. /**
  316. * omap_device_alloc - allocate an omap_device
  317. * @pdev: platform_device that will be included in this omap_device
  318. * @oh: ptr to the single omap_hwmod that backs this omap_device
  319. * @pdata: platform_data ptr to associate with the platform_device
  320. * @pdata_len: amount of memory pointed to by @pdata
  321. *
  322. * Convenience function for allocating an omap_device structure and filling
  323. * hwmods, and resources.
  324. *
  325. * Returns an struct omap_device pointer or ERR_PTR() on error;
  326. */
  327. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  328. struct omap_hwmod **ohs, int oh_cnt)
  329. {
  330. int ret = -ENOMEM;
  331. struct omap_device *od;
  332. struct resource *res = NULL;
  333. int i, res_count;
  334. struct omap_hwmod **hwmods;
  335. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  336. if (!od) {
  337. ret = -ENOMEM;
  338. goto oda_exit1;
  339. }
  340. od->hwmods_cnt = oh_cnt;
  341. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  342. if (!hwmods)
  343. goto oda_exit2;
  344. od->hwmods = hwmods;
  345. od->pdev = pdev;
  346. /*
  347. * Non-DT Boot:
  348. * Here, pdev->num_resources = 0, and we should get all the
  349. * resources from hwmod.
  350. *
  351. * DT Boot:
  352. * OF framework will construct the resource structure (currently
  353. * does for MEM & IRQ resource) and we should respect/use these
  354. * resources, killing hwmod dependency.
  355. * If pdev->num_resources > 0, we assume that MEM & IRQ resources
  356. * have been allocated by OF layer already (through DTB).
  357. * As preparation for the future we examine the OF provided resources
  358. * to see if we have DMA resources provided already. In this case
  359. * there is no need to update the resources for the device, we use the
  360. * OF provided ones.
  361. *
  362. * TODO: Once DMA resource is available from OF layer, we should
  363. * kill filling any resources from hwmod.
  364. */
  365. if (!pdev->num_resources) {
  366. /* Count all resources for the device */
  367. res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
  368. IORESOURCE_DMA |
  369. IORESOURCE_MEM);
  370. } else {
  371. /* Take a look if we already have DMA resource via DT */
  372. for (i = 0; i < pdev->num_resources; i++) {
  373. struct resource *r = &pdev->resource[i];
  374. /* We have it, no need to touch the resources */
  375. if (r->flags == IORESOURCE_DMA)
  376. goto have_everything;
  377. }
  378. /* Count only DMA resources for the device */
  379. res_count = omap_device_count_resources(od, IORESOURCE_DMA);
  380. /* The device has no DMA resource, no need for update */
  381. if (!res_count)
  382. goto have_everything;
  383. res_count += pdev->num_resources;
  384. }
  385. /* Allocate resources memory to account for new resources */
  386. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  387. if (!res)
  388. goto oda_exit3;
  389. if (!pdev->num_resources) {
  390. dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
  391. __func__, res_count);
  392. omap_device_fill_resources(od, res);
  393. } else {
  394. dev_dbg(&pdev->dev,
  395. "%s: appending %d DMA resources from hwmod\n",
  396. __func__, res_count - pdev->num_resources);
  397. memcpy(res, pdev->resource,
  398. sizeof(struct resource) * pdev->num_resources);
  399. _od_fill_dma_resources(od, &res[pdev->num_resources]);
  400. }
  401. ret = platform_device_add_resources(pdev, res, res_count);
  402. kfree(res);
  403. if (ret)
  404. goto oda_exit3;
  405. have_everything:
  406. pdev->archdata.od = od;
  407. for (i = 0; i < oh_cnt; i++) {
  408. hwmods[i]->od = od;
  409. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  410. }
  411. return od;
  412. oda_exit3:
  413. kfree(hwmods);
  414. oda_exit2:
  415. kfree(od);
  416. oda_exit1:
  417. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  418. return ERR_PTR(ret);
  419. }
  420. void omap_device_delete(struct omap_device *od)
  421. {
  422. if (!od)
  423. return;
  424. od->pdev->archdata.od = NULL;
  425. kfree(od->hwmods);
  426. kfree(od);
  427. }
  428. /**
  429. * omap_device_build - build and register an omap_device with one omap_hwmod
  430. * @pdev_name: name of the platform_device driver to use
  431. * @pdev_id: this platform_device's connection ID
  432. * @oh: ptr to the single omap_hwmod that backs this omap_device
  433. * @pdata: platform_data ptr to associate with the platform_device
  434. * @pdata_len: amount of memory pointed to by @pdata
  435. *
  436. * Convenience function for building and registering a single
  437. * omap_device record, which in turn builds and registers a
  438. * platform_device record. See omap_device_build_ss() for more
  439. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  440. * passes along the return value of omap_device_build_ss().
  441. */
  442. struct platform_device __init *omap_device_build(const char *pdev_name,
  443. int pdev_id,
  444. struct omap_hwmod *oh,
  445. void *pdata, int pdata_len)
  446. {
  447. struct omap_hwmod *ohs[] = { oh };
  448. if (!oh)
  449. return ERR_PTR(-EINVAL);
  450. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  451. pdata_len);
  452. }
  453. /**
  454. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  455. * @pdev_name: name of the platform_device driver to use
  456. * @pdev_id: this platform_device's connection ID
  457. * @oh: ptr to the single omap_hwmod that backs this omap_device
  458. * @pdata: platform_data ptr to associate with the platform_device
  459. * @pdata_len: amount of memory pointed to by @pdata
  460. *
  461. * Convenience function for building and registering an omap_device
  462. * subsystem record. Subsystem records consist of multiple
  463. * omap_hwmods. This function in turn builds and registers a
  464. * platform_device record. Returns an ERR_PTR() on error, or passes
  465. * along the return value of omap_device_register().
  466. */
  467. struct platform_device __init *omap_device_build_ss(const char *pdev_name,
  468. int pdev_id,
  469. struct omap_hwmod **ohs,
  470. int oh_cnt, void *pdata,
  471. int pdata_len)
  472. {
  473. int ret = -ENOMEM;
  474. struct platform_device *pdev;
  475. struct omap_device *od;
  476. if (!ohs || oh_cnt == 0 || !pdev_name)
  477. return ERR_PTR(-EINVAL);
  478. if (!pdata && pdata_len > 0)
  479. return ERR_PTR(-EINVAL);
  480. pdev = platform_device_alloc(pdev_name, pdev_id);
  481. if (!pdev) {
  482. ret = -ENOMEM;
  483. goto odbs_exit;
  484. }
  485. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  486. if (pdev->id != -1)
  487. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  488. else
  489. dev_set_name(&pdev->dev, "%s", pdev->name);
  490. od = omap_device_alloc(pdev, ohs, oh_cnt);
  491. if (IS_ERR(od))
  492. goto odbs_exit1;
  493. ret = platform_device_add_data(pdev, pdata, pdata_len);
  494. if (ret)
  495. goto odbs_exit2;
  496. ret = omap_device_register(pdev);
  497. if (ret)
  498. goto odbs_exit2;
  499. return pdev;
  500. odbs_exit2:
  501. omap_device_delete(od);
  502. odbs_exit1:
  503. platform_device_put(pdev);
  504. odbs_exit:
  505. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  506. return ERR_PTR(ret);
  507. }
  508. #ifdef CONFIG_PM_RUNTIME
  509. static int _od_runtime_suspend(struct device *dev)
  510. {
  511. struct platform_device *pdev = to_platform_device(dev);
  512. int ret;
  513. ret = pm_generic_runtime_suspend(dev);
  514. if (!ret)
  515. omap_device_idle(pdev);
  516. return ret;
  517. }
  518. static int _od_runtime_resume(struct device *dev)
  519. {
  520. struct platform_device *pdev = to_platform_device(dev);
  521. omap_device_enable(pdev);
  522. return pm_generic_runtime_resume(dev);
  523. }
  524. static int _od_fail_runtime_suspend(struct device *dev)
  525. {
  526. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  527. return -ENODEV;
  528. }
  529. static int _od_fail_runtime_resume(struct device *dev)
  530. {
  531. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  532. return -ENODEV;
  533. }
  534. #endif
  535. #ifdef CONFIG_SUSPEND
  536. static int _od_suspend_noirq(struct device *dev)
  537. {
  538. struct platform_device *pdev = to_platform_device(dev);
  539. struct omap_device *od = to_omap_device(pdev);
  540. int ret;
  541. /* Don't attempt late suspend on a driver that is not bound */
  542. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
  543. return 0;
  544. ret = pm_generic_suspend_noirq(dev);
  545. if (!ret && !pm_runtime_status_suspended(dev)) {
  546. if (pm_generic_runtime_suspend(dev) == 0) {
  547. pm_runtime_set_suspended(dev);
  548. omap_device_idle(pdev);
  549. od->flags |= OMAP_DEVICE_SUSPENDED;
  550. }
  551. }
  552. return ret;
  553. }
  554. static int _od_resume_noirq(struct device *dev)
  555. {
  556. struct platform_device *pdev = to_platform_device(dev);
  557. struct omap_device *od = to_omap_device(pdev);
  558. if (od->flags & OMAP_DEVICE_SUSPENDED) {
  559. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  560. omap_device_enable(pdev);
  561. /*
  562. * XXX: we run before core runtime pm has resumed itself. At
  563. * this point in time, we just restore the runtime pm state and
  564. * considering symmetric operations in resume, we donot expect
  565. * to fail. If we failed, something changed in core runtime_pm
  566. * framework OR some device driver messed things up, hence, WARN
  567. */
  568. WARN(pm_runtime_set_active(dev),
  569. "Could not set %s runtime state active\n", dev_name(dev));
  570. pm_generic_runtime_resume(dev);
  571. }
  572. return pm_generic_resume_noirq(dev);
  573. }
  574. #else
  575. #define _od_suspend_noirq NULL
  576. #define _od_resume_noirq NULL
  577. #endif
  578. struct dev_pm_domain omap_device_fail_pm_domain = {
  579. .ops = {
  580. SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
  581. _od_fail_runtime_resume, NULL)
  582. }
  583. };
  584. struct dev_pm_domain omap_device_pm_domain = {
  585. .ops = {
  586. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  587. NULL)
  588. USE_PLATFORM_PM_SLEEP_OPS
  589. .suspend_noirq = _od_suspend_noirq,
  590. .resume_noirq = _od_resume_noirq,
  591. }
  592. };
  593. /**
  594. * omap_device_register - register an omap_device with one omap_hwmod
  595. * @od: struct omap_device * to register
  596. *
  597. * Register the omap_device structure. This currently just calls
  598. * platform_device_register() on the underlying platform_device.
  599. * Returns the return value of platform_device_register().
  600. */
  601. int omap_device_register(struct platform_device *pdev)
  602. {
  603. pr_debug("omap_device: %s: registering\n", pdev->name);
  604. pdev->dev.pm_domain = &omap_device_pm_domain;
  605. return platform_device_add(pdev);
  606. }
  607. /* Public functions for use by device drivers through struct platform_data */
  608. /**
  609. * omap_device_enable - fully activate an omap_device
  610. * @od: struct omap_device * to activate
  611. *
  612. * Do whatever is necessary for the hwmods underlying omap_device @od
  613. * to be accessible and ready to operate. This generally involves
  614. * enabling clocks, setting SYSCONFIG registers; and in the future may
  615. * involve remuxing pins. Device drivers should call this function
  616. * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
  617. * the omap_device is already enabled, or passes along the return
  618. * value of _omap_device_enable_hwmods().
  619. */
  620. int omap_device_enable(struct platform_device *pdev)
  621. {
  622. int ret;
  623. struct omap_device *od;
  624. od = to_omap_device(pdev);
  625. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  626. dev_warn(&pdev->dev,
  627. "omap_device: %s() called from invalid state %d\n",
  628. __func__, od->_state);
  629. return -EINVAL;
  630. }
  631. ret = _omap_device_enable_hwmods(od);
  632. od->_state = OMAP_DEVICE_STATE_ENABLED;
  633. return ret;
  634. }
  635. /**
  636. * omap_device_idle - idle an omap_device
  637. * @od: struct omap_device * to idle
  638. *
  639. * Idle omap_device @od. Device drivers call this function indirectly
  640. * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not
  641. * currently enabled, or passes along the return value of
  642. * _omap_device_idle_hwmods().
  643. */
  644. int omap_device_idle(struct platform_device *pdev)
  645. {
  646. int ret;
  647. struct omap_device *od;
  648. od = to_omap_device(pdev);
  649. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  650. dev_warn(&pdev->dev,
  651. "omap_device: %s() called from invalid state %d\n",
  652. __func__, od->_state);
  653. return -EINVAL;
  654. }
  655. ret = _omap_device_idle_hwmods(od);
  656. od->_state = OMAP_DEVICE_STATE_IDLE;
  657. return ret;
  658. }
  659. /**
  660. * omap_device_assert_hardreset - set a device's hardreset line
  661. * @pdev: struct platform_device * to reset
  662. * @name: const char * name of the reset line
  663. *
  664. * Set the hardreset line identified by @name on the IP blocks
  665. * associated with the hwmods backing the platform_device @pdev. All
  666. * of the hwmods associated with @pdev must have the same hardreset
  667. * line linked to them for this to work. Passes along the return value
  668. * of omap_hwmod_assert_hardreset() in the event of any failure, or
  669. * returns 0 upon success.
  670. */
  671. int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
  672. {
  673. struct omap_device *od = to_omap_device(pdev);
  674. int ret = 0;
  675. int i;
  676. for (i = 0; i < od->hwmods_cnt; i++) {
  677. ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
  678. if (ret)
  679. break;
  680. }
  681. return ret;
  682. }
  683. /**
  684. * omap_device_deassert_hardreset - release a device's hardreset line
  685. * @pdev: struct platform_device * to reset
  686. * @name: const char * name of the reset line
  687. *
  688. * Release the hardreset line identified by @name on the IP blocks
  689. * associated with the hwmods backing the platform_device @pdev. All
  690. * of the hwmods associated with @pdev must have the same hardreset
  691. * line linked to them for this to work. Passes along the return
  692. * value of omap_hwmod_deassert_hardreset() in the event of any
  693. * failure, or returns 0 upon success.
  694. */
  695. int omap_device_deassert_hardreset(struct platform_device *pdev,
  696. const char *name)
  697. {
  698. struct omap_device *od = to_omap_device(pdev);
  699. int ret = 0;
  700. int i;
  701. for (i = 0; i < od->hwmods_cnt; i++) {
  702. ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
  703. if (ret)
  704. break;
  705. }
  706. return ret;
  707. }
  708. /**
  709. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  710. * device pointer.
  711. * @oh_name: name of the hwmod device
  712. *
  713. * Returns back a struct device * pointer associated with a hwmod
  714. * device represented by a hwmod_name
  715. */
  716. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  717. {
  718. struct omap_hwmod *oh;
  719. if (!oh_name) {
  720. WARN(1, "%s: no hwmod name!\n", __func__);
  721. return ERR_PTR(-EINVAL);
  722. }
  723. oh = omap_hwmod_lookup(oh_name);
  724. if (!oh) {
  725. WARN(1, "%s: no hwmod for %s\n", __func__,
  726. oh_name);
  727. return ERR_PTR(-ENODEV);
  728. }
  729. if (!oh->od) {
  730. WARN(1, "%s: no omap_device for %s\n", __func__,
  731. oh_name);
  732. return ERR_PTR(-ENODEV);
  733. }
  734. return &oh->od->pdev->dev;
  735. }
  736. static struct notifier_block platform_nb = {
  737. .notifier_call = _omap_device_notifier_call,
  738. };
  739. static int __init omap_device_init(void)
  740. {
  741. bus_register_notifier(&platform_bus_type, &platform_nb);
  742. return 0;
  743. }
  744. omap_core_initcall(omap_device_init);
  745. /**
  746. * omap_device_late_idle - idle devices without drivers
  747. * @dev: struct device * associated with omap_device
  748. * @data: unused
  749. *
  750. * Check the driver bound status of this device, and idle it
  751. * if there is no driver attached.
  752. */
  753. static int __init omap_device_late_idle(struct device *dev, void *data)
  754. {
  755. struct platform_device *pdev = to_platform_device(dev);
  756. struct omap_device *od = to_omap_device(pdev);
  757. int i;
  758. if (!od)
  759. return 0;
  760. /*
  761. * If omap_device state is enabled, but has no driver bound,
  762. * idle it.
  763. */
  764. /*
  765. * Some devices (like memory controllers) are always kept
  766. * enabled, and should not be idled even with no drivers.
  767. */
  768. for (i = 0; i < od->hwmods_cnt; i++)
  769. if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
  770. return 0;
  771. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) {
  772. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  773. dev_warn(dev, "%s: enabled but no driver. Idling\n",
  774. __func__);
  775. omap_device_idle(pdev);
  776. }
  777. }
  778. return 0;
  779. }
  780. static int __init omap_device_late_init(void)
  781. {
  782. bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
  783. return 0;
  784. }
  785. omap_late_initcall_sync(omap_device_late_init);