omap_device.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 "omap_device.h"
  38. #include "omap_hwmod.h"
  39. /* Private functions */
  40. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  41. const char *clk_name)
  42. {
  43. struct clk *r;
  44. struct clk_lookup *l;
  45. if (!clk_alias || !clk_name)
  46. return;
  47. dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
  48. r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
  49. if (!IS_ERR(r)) {
  50. dev_warn(&od->pdev->dev,
  51. "alias %s already exists\n", clk_alias);
  52. clk_put(r);
  53. return;
  54. }
  55. r = clk_get(NULL, clk_name);
  56. if (IS_ERR(r)) {
  57. dev_err(&od->pdev->dev,
  58. "clk_get for %s failed\n", clk_name);
  59. return;
  60. }
  61. l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
  62. if (!l) {
  63. dev_err(&od->pdev->dev,
  64. "clkdev_alloc for %s failed\n", clk_alias);
  65. return;
  66. }
  67. clkdev_add(l);
  68. }
  69. /**
  70. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  71. * and main clock
  72. * @od: struct omap_device *od
  73. * @oh: struct omap_hwmod *oh
  74. *
  75. * For the main clock and every optional clock present per hwmod per
  76. * omap_device, this function adds an entry in the clkdev table of the
  77. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  78. *
  79. * The function is called from inside omap_device_build_ss(), after
  80. * omap_device_register.
  81. *
  82. * This allows drivers to get a pointer to its optional clocks based on its role
  83. * by calling clk_get(<dev*>, <role>).
  84. * In the case of the main clock, a "fck" alias is used.
  85. *
  86. * No return value.
  87. */
  88. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  89. struct omap_hwmod *oh)
  90. {
  91. int i;
  92. _add_clkdev(od, "fck", oh->main_clk);
  93. for (i = 0; i < oh->opt_clks_cnt; i++)
  94. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  95. }
  96. /**
  97. * omap_device_build_from_dt - build an omap_device with multiple hwmods
  98. * @pdev_name: name of the platform_device driver to use
  99. * @pdev_id: this platform_device's connection ID
  100. * @oh: ptr to the single omap_hwmod that backs this omap_device
  101. * @pdata: platform_data ptr to associate with the platform_device
  102. * @pdata_len: amount of memory pointed to by @pdata
  103. *
  104. * Function for building an omap_device already registered from device-tree
  105. *
  106. * Returns 0 or PTR_ERR() on error.
  107. */
  108. static int omap_device_build_from_dt(struct platform_device *pdev)
  109. {
  110. struct omap_hwmod **hwmods;
  111. struct omap_device *od;
  112. struct omap_hwmod *oh;
  113. struct device_node *node = pdev->dev.of_node;
  114. const char *oh_name;
  115. int oh_cnt, i, ret = 0;
  116. oh_cnt = of_property_count_strings(node, "ti,hwmods");
  117. if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
  118. dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
  119. return -ENODEV;
  120. }
  121. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  122. if (!hwmods) {
  123. ret = -ENOMEM;
  124. goto odbfd_exit;
  125. }
  126. for (i = 0; i < oh_cnt; i++) {
  127. of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
  128. oh = omap_hwmod_lookup(oh_name);
  129. if (!oh) {
  130. dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
  131. oh_name);
  132. ret = -EINVAL;
  133. goto odbfd_exit1;
  134. }
  135. hwmods[i] = oh;
  136. }
  137. od = omap_device_alloc(pdev, hwmods, oh_cnt);
  138. if (!od) {
  139. dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
  140. oh_name);
  141. ret = PTR_ERR(od);
  142. goto odbfd_exit1;
  143. }
  144. /* Fix up missing resource names */
  145. for (i = 0; i < pdev->num_resources; i++) {
  146. struct resource *r = &pdev->resource[i];
  147. if (r->name == NULL)
  148. r->name = dev_name(&pdev->dev);
  149. }
  150. if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
  151. omap_device_disable_idle_on_suspend(pdev);
  152. pdev->dev.pm_domain = &omap_device_pm_domain;
  153. odbfd_exit1:
  154. kfree(hwmods);
  155. odbfd_exit:
  156. return ret;
  157. }
  158. static int _omap_device_notifier_call(struct notifier_block *nb,
  159. unsigned long event, void *dev)
  160. {
  161. struct platform_device *pdev = to_platform_device(dev);
  162. struct omap_device *od;
  163. switch (event) {
  164. case BUS_NOTIFY_DEL_DEVICE:
  165. if (pdev->archdata.od)
  166. omap_device_delete(pdev->archdata.od);
  167. break;
  168. case BUS_NOTIFY_ADD_DEVICE:
  169. if (pdev->dev.of_node)
  170. omap_device_build_from_dt(pdev);
  171. /* fall through */
  172. default:
  173. od = to_omap_device(pdev);
  174. if (od)
  175. od->_driver_status = event;
  176. }
  177. return NOTIFY_DONE;
  178. }
  179. /**
  180. * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  181. * @od: struct omap_device *od
  182. *
  183. * Enable all underlying hwmods. Returns 0.
  184. */
  185. static int _omap_device_enable_hwmods(struct omap_device *od)
  186. {
  187. int i;
  188. for (i = 0; i < od->hwmods_cnt; i++)
  189. omap_hwmod_enable(od->hwmods[i]);
  190. /* XXX pass along return value here? */
  191. return 0;
  192. }
  193. /**
  194. * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  195. * @od: struct omap_device *od
  196. *
  197. * Idle all underlying hwmods. Returns 0.
  198. */
  199. static int _omap_device_idle_hwmods(struct omap_device *od)
  200. {
  201. int i;
  202. for (i = 0; i < od->hwmods_cnt; i++)
  203. omap_hwmod_idle(od->hwmods[i]);
  204. /* XXX pass along return value here? */
  205. return 0;
  206. }
  207. /* Public functions for use by core code */
  208. /**
  209. * omap_device_get_context_loss_count - get lost context count
  210. * @od: struct omap_device *
  211. *
  212. * Using the primary hwmod, query the context loss count for this
  213. * device.
  214. *
  215. * Callers should consider context for this device lost any time this
  216. * function returns a value different than the value the caller got
  217. * the last time it called this function.
  218. *
  219. * If any hwmods exist for the omap_device assoiated with @pdev,
  220. * return the context loss counter for that hwmod, otherwise return
  221. * zero.
  222. */
  223. int omap_device_get_context_loss_count(struct platform_device *pdev)
  224. {
  225. struct omap_device *od;
  226. u32 ret = 0;
  227. od = to_omap_device(pdev);
  228. if (od->hwmods_cnt)
  229. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  230. return ret;
  231. }
  232. /**
  233. * omap_device_count_resources - count number of struct resource entries needed
  234. * @od: struct omap_device *
  235. * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
  236. *
  237. * Count the number of struct resource entries needed for this
  238. * omap_device @od. Used by omap_device_build_ss() to determine how
  239. * much memory to allocate before calling
  240. * omap_device_fill_resources(). Returns the count.
  241. */
  242. static int omap_device_count_resources(struct omap_device *od,
  243. unsigned long flags)
  244. {
  245. int c = 0;
  246. int i;
  247. for (i = 0; i < od->hwmods_cnt; i++)
  248. c += omap_hwmod_count_resources(od->hwmods[i], flags);
  249. pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
  250. od->pdev->name, c, od->hwmods_cnt);
  251. return c;
  252. }
  253. /**
  254. * omap_device_fill_resources - fill in array of struct resource
  255. * @od: struct omap_device *
  256. * @res: pointer to an array of struct resource to be filled in
  257. *
  258. * Populate one or more empty struct resource pointed to by @res with
  259. * the resource data for this omap_device @od. Used by
  260. * omap_device_build_ss() after calling omap_device_count_resources().
  261. * Ideally this function would not be needed at all. If omap_device
  262. * replaces platform_device, then we can specify our own
  263. * get_resource()/ get_irq()/etc functions that use the underlying
  264. * omap_hwmod information. Or if platform_device is extended to use
  265. * subarchitecture-specific function pointers, the various
  266. * platform_device functions can simply call omap_device internal
  267. * functions to get device resources. Hacking around the existing
  268. * platform_device code wastes memory. Returns 0.
  269. */
  270. static int omap_device_fill_resources(struct omap_device *od,
  271. struct resource *res)
  272. {
  273. int i, r;
  274. for (i = 0; i < od->hwmods_cnt; i++) {
  275. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  276. res += r;
  277. }
  278. return 0;
  279. }
  280. /**
  281. * _od_fill_dma_resources - fill in array of struct resource with dma resources
  282. * @od: struct omap_device *
  283. * @res: pointer to an array of struct resource to be filled in
  284. *
  285. * Populate one or more empty struct resource pointed to by @res with
  286. * the dma resource data for this omap_device @od. Used by
  287. * omap_device_alloc() after calling omap_device_count_resources().
  288. *
  289. * Ideally this function would not be needed at all. If we have
  290. * mechanism to get dma resources from DT.
  291. *
  292. * Returns 0.
  293. */
  294. static int _od_fill_dma_resources(struct omap_device *od,
  295. struct resource *res)
  296. {
  297. int i, r;
  298. for (i = 0; i < od->hwmods_cnt; i++) {
  299. r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
  300. res += r;
  301. }
  302. return 0;
  303. }
  304. /**
  305. * omap_device_alloc - allocate an omap_device
  306. * @pdev: platform_device that will be included in this omap_device
  307. * @oh: ptr to the single omap_hwmod that backs this omap_device
  308. * @pdata: platform_data ptr to associate with the platform_device
  309. * @pdata_len: amount of memory pointed to by @pdata
  310. *
  311. * Convenience function for allocating an omap_device structure and filling
  312. * hwmods, and resources.
  313. *
  314. * Returns an struct omap_device pointer or ERR_PTR() on error;
  315. */
  316. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  317. struct omap_hwmod **ohs, int oh_cnt)
  318. {
  319. int ret = -ENOMEM;
  320. struct omap_device *od;
  321. struct resource *res = NULL;
  322. int i, res_count;
  323. struct omap_hwmod **hwmods;
  324. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  325. if (!od) {
  326. ret = -ENOMEM;
  327. goto oda_exit1;
  328. }
  329. od->hwmods_cnt = oh_cnt;
  330. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  331. if (!hwmods)
  332. goto oda_exit2;
  333. od->hwmods = hwmods;
  334. od->pdev = pdev;
  335. /*
  336. * Non-DT Boot:
  337. * Here, pdev->num_resources = 0, and we should get all the
  338. * resources from hwmod.
  339. *
  340. * DT Boot:
  341. * OF framework will construct the resource structure (currently
  342. * does for MEM & IRQ resource) and we should respect/use these
  343. * resources, killing hwmod dependency.
  344. * If pdev->num_resources > 0, we assume that MEM & IRQ resources
  345. * have been allocated by OF layer already (through DTB).
  346. * As preparation for the future we examine the OF provided resources
  347. * to see if we have DMA resources provided already. In this case
  348. * there is no need to update the resources for the device, we use the
  349. * OF provided ones.
  350. *
  351. * TODO: Once DMA resource is available from OF layer, we should
  352. * kill filling any resources from hwmod.
  353. */
  354. if (!pdev->num_resources) {
  355. /* Count all resources for the device */
  356. res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
  357. IORESOURCE_DMA |
  358. IORESOURCE_MEM);
  359. } else {
  360. /* Take a look if we already have DMA resource via DT */
  361. for (i = 0; i < pdev->num_resources; i++) {
  362. struct resource *r = &pdev->resource[i];
  363. /* We have it, no need to touch the resources */
  364. if (r->flags == IORESOURCE_DMA)
  365. goto have_everything;
  366. }
  367. /* Count only DMA resources for the device */
  368. res_count = omap_device_count_resources(od, IORESOURCE_DMA);
  369. /* The device has no DMA resource, no need for update */
  370. if (!res_count)
  371. goto have_everything;
  372. res_count += pdev->num_resources;
  373. }
  374. /* Allocate resources memory to account for new resources */
  375. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  376. if (!res)
  377. goto oda_exit3;
  378. if (!pdev->num_resources) {
  379. dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
  380. __func__, res_count);
  381. omap_device_fill_resources(od, res);
  382. } else {
  383. dev_dbg(&pdev->dev,
  384. "%s: appending %d DMA resources from hwmod\n",
  385. __func__, res_count - pdev->num_resources);
  386. memcpy(res, pdev->resource,
  387. sizeof(struct resource) * pdev->num_resources);
  388. _od_fill_dma_resources(od, &res[pdev->num_resources]);
  389. }
  390. ret = platform_device_add_resources(pdev, res, res_count);
  391. kfree(res);
  392. if (ret)
  393. goto oda_exit3;
  394. have_everything:
  395. pdev->archdata.od = od;
  396. for (i = 0; i < oh_cnt; i++) {
  397. hwmods[i]->od = od;
  398. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  399. }
  400. return od;
  401. oda_exit3:
  402. kfree(hwmods);
  403. oda_exit2:
  404. kfree(od);
  405. oda_exit1:
  406. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  407. return ERR_PTR(ret);
  408. }
  409. void omap_device_delete(struct omap_device *od)
  410. {
  411. if (!od)
  412. return;
  413. od->pdev->archdata.od = NULL;
  414. kfree(od->hwmods);
  415. kfree(od);
  416. }
  417. /**
  418. * omap_device_build - build and register an omap_device with one omap_hwmod
  419. * @pdev_name: name of the platform_device driver to use
  420. * @pdev_id: this platform_device's connection ID
  421. * @oh: ptr to the single omap_hwmod that backs this omap_device
  422. * @pdata: platform_data ptr to associate with the platform_device
  423. * @pdata_len: amount of memory pointed to by @pdata
  424. *
  425. * Convenience function for building and registering a single
  426. * omap_device record, which in turn builds and registers a
  427. * platform_device record. See omap_device_build_ss() for more
  428. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  429. * passes along the return value of omap_device_build_ss().
  430. */
  431. struct platform_device __init *omap_device_build(const char *pdev_name,
  432. int pdev_id,
  433. struct omap_hwmod *oh,
  434. void *pdata, int pdata_len)
  435. {
  436. struct omap_hwmod *ohs[] = { oh };
  437. if (!oh)
  438. return ERR_PTR(-EINVAL);
  439. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  440. pdata_len);
  441. }
  442. /**
  443. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  444. * @pdev_name: name of the platform_device driver to use
  445. * @pdev_id: this platform_device's connection ID
  446. * @oh: ptr to the single omap_hwmod that backs this omap_device
  447. * @pdata: platform_data ptr to associate with the platform_device
  448. * @pdata_len: amount of memory pointed to by @pdata
  449. *
  450. * Convenience function for building and registering an omap_device
  451. * subsystem record. Subsystem records consist of multiple
  452. * omap_hwmods. This function in turn builds and registers a
  453. * platform_device record. Returns an ERR_PTR() on error, or passes
  454. * along the return value of omap_device_register().
  455. */
  456. struct platform_device __init *omap_device_build_ss(const char *pdev_name,
  457. int pdev_id,
  458. struct omap_hwmod **ohs,
  459. int oh_cnt, void *pdata,
  460. int pdata_len)
  461. {
  462. int ret = -ENOMEM;
  463. struct platform_device *pdev;
  464. struct omap_device *od;
  465. if (!ohs || oh_cnt == 0 || !pdev_name)
  466. return ERR_PTR(-EINVAL);
  467. if (!pdata && pdata_len > 0)
  468. return ERR_PTR(-EINVAL);
  469. pdev = platform_device_alloc(pdev_name, pdev_id);
  470. if (!pdev) {
  471. ret = -ENOMEM;
  472. goto odbs_exit;
  473. }
  474. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  475. if (pdev->id != -1)
  476. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  477. else
  478. dev_set_name(&pdev->dev, "%s", pdev->name);
  479. od = omap_device_alloc(pdev, ohs, oh_cnt);
  480. if (IS_ERR(od))
  481. goto odbs_exit1;
  482. ret = platform_device_add_data(pdev, pdata, pdata_len);
  483. if (ret)
  484. goto odbs_exit2;
  485. ret = omap_device_register(pdev);
  486. if (ret)
  487. goto odbs_exit2;
  488. return pdev;
  489. odbs_exit2:
  490. omap_device_delete(od);
  491. odbs_exit1:
  492. platform_device_put(pdev);
  493. odbs_exit:
  494. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  495. return ERR_PTR(ret);
  496. }
  497. #ifdef CONFIG_PM_RUNTIME
  498. static int _od_runtime_suspend(struct device *dev)
  499. {
  500. struct platform_device *pdev = to_platform_device(dev);
  501. int ret;
  502. ret = pm_generic_runtime_suspend(dev);
  503. if (!ret)
  504. omap_device_idle(pdev);
  505. return ret;
  506. }
  507. static int _od_runtime_idle(struct device *dev)
  508. {
  509. return pm_generic_runtime_idle(dev);
  510. }
  511. static int _od_runtime_resume(struct device *dev)
  512. {
  513. struct platform_device *pdev = to_platform_device(dev);
  514. omap_device_enable(pdev);
  515. return pm_generic_runtime_resume(dev);
  516. }
  517. #endif
  518. #ifdef CONFIG_SUSPEND
  519. static int _od_suspend_noirq(struct device *dev)
  520. {
  521. struct platform_device *pdev = to_platform_device(dev);
  522. struct omap_device *od = to_omap_device(pdev);
  523. int ret;
  524. /* Don't attempt late suspend on a driver that is not bound */
  525. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
  526. return 0;
  527. ret = pm_generic_suspend_noirq(dev);
  528. if (!ret && !pm_runtime_status_suspended(dev)) {
  529. if (pm_generic_runtime_suspend(dev) == 0) {
  530. if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
  531. omap_device_idle(pdev);
  532. od->flags |= OMAP_DEVICE_SUSPENDED;
  533. }
  534. }
  535. return ret;
  536. }
  537. static int _od_resume_noirq(struct device *dev)
  538. {
  539. struct platform_device *pdev = to_platform_device(dev);
  540. struct omap_device *od = to_omap_device(pdev);
  541. if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
  542. !pm_runtime_status_suspended(dev)) {
  543. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  544. if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
  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. _od_runtime_idle)
  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 (IS_ERR_OR_NULL(oh)) {
  695. WARN(1, "%s: no hwmod for %s\n", __func__,
  696. oh_name);
  697. return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
  698. }
  699. if (IS_ERR_OR_NULL(oh->od)) {
  700. WARN(1, "%s: no omap_device for %s\n", __func__,
  701. oh_name);
  702. return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
  703. }
  704. if (IS_ERR_OR_NULL(oh->od->pdev))
  705. return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
  706. return &oh->od->pdev->dev;
  707. }
  708. static struct notifier_block platform_nb = {
  709. .notifier_call = _omap_device_notifier_call,
  710. };
  711. static int __init omap_device_init(void)
  712. {
  713. bus_register_notifier(&platform_bus_type, &platform_nb);
  714. return 0;
  715. }
  716. core_initcall(omap_device_init);
  717. /**
  718. * omap_device_late_idle - idle devices without drivers
  719. * @dev: struct device * associated with omap_device
  720. * @data: unused
  721. *
  722. * Check the driver bound status of this device, and idle it
  723. * if there is no driver attached.
  724. */
  725. static int __init omap_device_late_idle(struct device *dev, void *data)
  726. {
  727. struct platform_device *pdev = to_platform_device(dev);
  728. struct omap_device *od = to_omap_device(pdev);
  729. if (!od)
  730. return 0;
  731. /*
  732. * If omap_device state is enabled, but has no driver bound,
  733. * idle it.
  734. */
  735. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) {
  736. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  737. dev_warn(dev, "%s: enabled but no driver. Idling\n",
  738. __func__);
  739. omap_device_idle(pdev);
  740. }
  741. }
  742. return 0;
  743. }
  744. static int __init omap_device_late_init(void)
  745. {
  746. bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
  747. return 0;
  748. }
  749. late_initcall(omap_device_late_init);