omap_device.c 23 KB

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