omap_device.c 23 KB

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