omap_device.c 24 KB

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