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