omap_device.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 ret = 0;
  195. int i;
  196. for (i = 0; i < od->hwmods_cnt; i++)
  197. ret |= omap_hwmod_enable(od->hwmods[i]);
  198. return ret;
  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 ret = 0;
  209. int i;
  210. for (i = 0; i < od->hwmods_cnt; i++)
  211. ret |= omap_hwmod_idle(od->hwmods[i]);
  212. return ret;
  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. return ret;
  512. return omap_device_idle(pdev);
  513. }
  514. static int _od_runtime_resume(struct device *dev)
  515. {
  516. struct platform_device *pdev = to_platform_device(dev);
  517. int ret;
  518. ret = omap_device_enable(pdev);
  519. if (ret)
  520. return ret;
  521. return pm_generic_runtime_resume(dev);
  522. }
  523. static int _od_fail_runtime_suspend(struct device *dev)
  524. {
  525. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  526. return -ENODEV;
  527. }
  528. static int _od_fail_runtime_resume(struct device *dev)
  529. {
  530. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  531. return -ENODEV;
  532. }
  533. #endif
  534. #ifdef CONFIG_SUSPEND
  535. static int _od_suspend_noirq(struct device *dev)
  536. {
  537. struct platform_device *pdev = to_platform_device(dev);
  538. struct omap_device *od = to_omap_device(pdev);
  539. int ret;
  540. /* Don't attempt late suspend on a driver that is not bound */
  541. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
  542. return 0;
  543. ret = pm_generic_suspend_noirq(dev);
  544. if (!ret && !pm_runtime_status_suspended(dev)) {
  545. if (pm_generic_runtime_suspend(dev) == 0) {
  546. pm_runtime_set_suspended(dev);
  547. omap_device_idle(pdev);
  548. od->flags |= OMAP_DEVICE_SUSPENDED;
  549. }
  550. }
  551. return ret;
  552. }
  553. static int _od_resume_noirq(struct device *dev)
  554. {
  555. struct platform_device *pdev = to_platform_device(dev);
  556. struct omap_device *od = to_omap_device(pdev);
  557. if (od->flags & OMAP_DEVICE_SUSPENDED) {
  558. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  559. omap_device_enable(pdev);
  560. /*
  561. * XXX: we run before core runtime pm has resumed itself. At
  562. * this point in time, we just restore the runtime pm state and
  563. * considering symmetric operations in resume, we donot expect
  564. * to fail. If we failed, something changed in core runtime_pm
  565. * framework OR some device driver messed things up, hence, WARN
  566. */
  567. WARN(pm_runtime_set_active(dev),
  568. "Could not set %s runtime state active\n", dev_name(dev));
  569. pm_generic_runtime_resume(dev);
  570. }
  571. return pm_generic_resume_noirq(dev);
  572. }
  573. #else
  574. #define _od_suspend_noirq NULL
  575. #define _od_resume_noirq NULL
  576. #endif
  577. struct dev_pm_domain omap_device_fail_pm_domain = {
  578. .ops = {
  579. SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
  580. _od_fail_runtime_resume, NULL)
  581. }
  582. };
  583. struct dev_pm_domain omap_device_pm_domain = {
  584. .ops = {
  585. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  586. NULL)
  587. USE_PLATFORM_PM_SLEEP_OPS
  588. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
  589. _od_resume_noirq)
  590. }
  591. };
  592. /**
  593. * omap_device_register - register an omap_device with one omap_hwmod
  594. * @od: struct omap_device * to register
  595. *
  596. * Register the omap_device structure. This currently just calls
  597. * platform_device_register() on the underlying platform_device.
  598. * Returns the return value of platform_device_register().
  599. */
  600. int omap_device_register(struct platform_device *pdev)
  601. {
  602. pr_debug("omap_device: %s: registering\n", pdev->name);
  603. pdev->dev.pm_domain = &omap_device_pm_domain;
  604. return platform_device_add(pdev);
  605. }
  606. /* Public functions for use by device drivers through struct platform_data */
  607. /**
  608. * omap_device_enable - fully activate an omap_device
  609. * @od: struct omap_device * to activate
  610. *
  611. * Do whatever is necessary for the hwmods underlying omap_device @od
  612. * to be accessible and ready to operate. This generally involves
  613. * enabling clocks, setting SYSCONFIG registers; and in the future may
  614. * involve remuxing pins. Device drivers should call this function
  615. * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
  616. * the omap_device is already enabled, or passes along the return
  617. * value of _omap_device_enable_hwmods().
  618. */
  619. int omap_device_enable(struct platform_device *pdev)
  620. {
  621. int ret;
  622. struct omap_device *od;
  623. od = to_omap_device(pdev);
  624. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  625. dev_warn(&pdev->dev,
  626. "omap_device: %s() called from invalid state %d\n",
  627. __func__, od->_state);
  628. return -EINVAL;
  629. }
  630. ret = _omap_device_enable_hwmods(od);
  631. if (ret == 0)
  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. if (ret == 0)
  657. od->_state = OMAP_DEVICE_STATE_IDLE;
  658. return ret;
  659. }
  660. /**
  661. * omap_device_assert_hardreset - set a device's hardreset line
  662. * @pdev: struct platform_device * to reset
  663. * @name: const char * name of the reset line
  664. *
  665. * Set the hardreset line identified by @name on the IP blocks
  666. * associated with the hwmods backing the platform_device @pdev. All
  667. * of the hwmods associated with @pdev must have the same hardreset
  668. * line linked to them for this to work. Passes along the return value
  669. * of omap_hwmod_assert_hardreset() in the event of any failure, or
  670. * returns 0 upon success.
  671. */
  672. int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
  673. {
  674. struct omap_device *od = to_omap_device(pdev);
  675. int ret = 0;
  676. int i;
  677. for (i = 0; i < od->hwmods_cnt; i++) {
  678. ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
  679. if (ret)
  680. break;
  681. }
  682. return ret;
  683. }
  684. /**
  685. * omap_device_deassert_hardreset - release a device's hardreset line
  686. * @pdev: struct platform_device * to reset
  687. * @name: const char * name of the reset line
  688. *
  689. * Release the hardreset line identified by @name on the IP blocks
  690. * associated with the hwmods backing the platform_device @pdev. All
  691. * of the hwmods associated with @pdev must have the same hardreset
  692. * line linked to them for this to work. Passes along the return
  693. * value of omap_hwmod_deassert_hardreset() in the event of any
  694. * failure, or returns 0 upon success.
  695. */
  696. int omap_device_deassert_hardreset(struct platform_device *pdev,
  697. const char *name)
  698. {
  699. struct omap_device *od = to_omap_device(pdev);
  700. int ret = 0;
  701. int i;
  702. for (i = 0; i < od->hwmods_cnt; i++) {
  703. ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
  704. if (ret)
  705. break;
  706. }
  707. return ret;
  708. }
  709. /**
  710. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  711. * device pointer.
  712. * @oh_name: name of the hwmod device
  713. *
  714. * Returns back a struct device * pointer associated with a hwmod
  715. * device represented by a hwmod_name
  716. */
  717. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  718. {
  719. struct omap_hwmod *oh;
  720. if (!oh_name) {
  721. WARN(1, "%s: no hwmod name!\n", __func__);
  722. return ERR_PTR(-EINVAL);
  723. }
  724. oh = omap_hwmod_lookup(oh_name);
  725. if (!oh) {
  726. WARN(1, "%s: no hwmod for %s\n", __func__,
  727. oh_name);
  728. return ERR_PTR(-ENODEV);
  729. }
  730. if (!oh->od) {
  731. WARN(1, "%s: no omap_device for %s\n", __func__,
  732. oh_name);
  733. return ERR_PTR(-ENODEV);
  734. }
  735. return &oh->od->pdev->dev;
  736. }
  737. static struct notifier_block platform_nb = {
  738. .notifier_call = _omap_device_notifier_call,
  739. };
  740. static int __init omap_device_init(void)
  741. {
  742. bus_register_notifier(&platform_bus_type, &platform_nb);
  743. return 0;
  744. }
  745. omap_core_initcall(omap_device_init);
  746. /**
  747. * omap_device_late_idle - idle devices without drivers
  748. * @dev: struct device * associated with omap_device
  749. * @data: unused
  750. *
  751. * Check the driver bound status of this device, and idle it
  752. * if there is no driver attached.
  753. */
  754. static int __init omap_device_late_idle(struct device *dev, void *data)
  755. {
  756. struct platform_device *pdev = to_platform_device(dev);
  757. struct omap_device *od = to_omap_device(pdev);
  758. int i;
  759. if (!od)
  760. return 0;
  761. /*
  762. * If omap_device state is enabled, but has no driver bound,
  763. * idle it.
  764. */
  765. /*
  766. * Some devices (like memory controllers) are always kept
  767. * enabled, and should not be idled even with no drivers.
  768. */
  769. for (i = 0; i < od->hwmods_cnt; i++)
  770. if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
  771. return 0;
  772. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
  773. od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
  774. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  775. dev_warn(dev, "%s: enabled but no driver. Idling\n",
  776. __func__);
  777. omap_device_idle(pdev);
  778. }
  779. }
  780. return 0;
  781. }
  782. static int __init omap_device_late_init(void)
  783. {
  784. bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
  785. WARN(!of_have_populated_dt(),
  786. "legacy booting deprecated, please update to boot with .dts\n");
  787. return 0;
  788. }
  789. omap_late_initcall_sync(omap_device_late_init);