omap_device.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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_domain.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/of.h>
  37. #include <linux/of_address.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/notifier.h>
  40. #include "common.h"
  41. #include "soc.h"
  42. #include "omap_device.h"
  43. #include "omap_hwmod.h"
  44. /* Private functions */
  45. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  46. const char *clk_name)
  47. {
  48. struct clk *r;
  49. int rc;
  50. if (!clk_alias || !clk_name)
  51. return;
  52. dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
  53. r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
  54. if (!IS_ERR(r)) {
  55. dev_dbg(&od->pdev->dev,
  56. "alias %s already exists\n", clk_alias);
  57. clk_put(r);
  58. return;
  59. }
  60. r = clk_get_sys(NULL, clk_name);
  61. if (IS_ERR(r)) {
  62. struct of_phandle_args clkspec;
  63. clkspec.np = of_find_node_by_name(NULL, clk_name);
  64. r = of_clk_get_from_provider(&clkspec);
  65. rc = clk_register_clkdev(r, clk_alias,
  66. dev_name(&od->pdev->dev));
  67. } else {
  68. rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev),
  69. clk_name, NULL);
  70. }
  71. if (rc) {
  72. if (rc == -ENODEV || rc == -ENOMEM)
  73. dev_err(&od->pdev->dev,
  74. "clkdev_alloc for %s failed\n", clk_alias);
  75. else
  76. dev_err(&od->pdev->dev,
  77. "clk_get for %s failed\n", clk_name);
  78. }
  79. }
  80. /**
  81. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  82. * and main clock
  83. * @od: struct omap_device *od
  84. * @oh: struct omap_hwmod *oh
  85. *
  86. * For the main clock and every optional clock present per hwmod per
  87. * omap_device, this function adds an entry in the clkdev table of the
  88. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  89. *
  90. * The function is called from inside omap_device_build_ss(), after
  91. * omap_device_register.
  92. *
  93. * This allows drivers to get a pointer to its optional clocks based on its role
  94. * by calling clk_get(<dev*>, <role>).
  95. * In the case of the main clock, a "fck" alias is used.
  96. *
  97. * No return value.
  98. */
  99. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  100. struct omap_hwmod *oh)
  101. {
  102. int i;
  103. _add_clkdev(od, "fck", oh->main_clk);
  104. for (i = 0; i < oh->opt_clks_cnt; i++)
  105. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  106. }
  107. /**
  108. * omap_device_build_from_dt - build an omap_device with multiple hwmods
  109. * @pdev_name: name of the platform_device driver to use
  110. * @pdev_id: this platform_device's connection ID
  111. * @oh: ptr to the single omap_hwmod that backs this omap_device
  112. * @pdata: platform_data ptr to associate with the platform_device
  113. * @pdata_len: amount of memory pointed to by @pdata
  114. *
  115. * Function for building an omap_device already registered from device-tree
  116. *
  117. * Returns 0 or PTR_ERR() on error.
  118. */
  119. static int omap_device_build_from_dt(struct platform_device *pdev)
  120. {
  121. struct omap_hwmod **hwmods;
  122. struct omap_device *od;
  123. struct omap_hwmod *oh;
  124. struct device_node *node = pdev->dev.of_node;
  125. const char *oh_name;
  126. int oh_cnt, i, ret = 0;
  127. bool device_active = false;
  128. oh_cnt = of_property_count_strings(node, "ti,hwmods");
  129. if (oh_cnt <= 0) {
  130. dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
  131. return -ENODEV;
  132. }
  133. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  134. if (!hwmods) {
  135. ret = -ENOMEM;
  136. goto odbfd_exit;
  137. }
  138. for (i = 0; i < oh_cnt; i++) {
  139. of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
  140. oh = omap_hwmod_lookup(oh_name);
  141. if (!oh) {
  142. dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
  143. oh_name);
  144. ret = -EINVAL;
  145. goto odbfd_exit1;
  146. }
  147. hwmods[i] = oh;
  148. if (oh->flags & HWMOD_INIT_NO_IDLE)
  149. device_active = true;
  150. }
  151. od = omap_device_alloc(pdev, hwmods, oh_cnt);
  152. if (IS_ERR(od)) {
  153. dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
  154. oh_name);
  155. ret = PTR_ERR(od);
  156. goto odbfd_exit1;
  157. }
  158. /* Fix up missing resource names */
  159. for (i = 0; i < pdev->num_resources; i++) {
  160. struct resource *r = &pdev->resource[i];
  161. if (r->name == NULL)
  162. r->name = dev_name(&pdev->dev);
  163. }
  164. dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
  165. if (device_active) {
  166. omap_device_enable(pdev);
  167. pm_runtime_set_active(&pdev->dev);
  168. }
  169. odbfd_exit1:
  170. kfree(hwmods);
  171. odbfd_exit:
  172. /* if data/we are at fault.. load up a fail handler */
  173. if (ret)
  174. dev_pm_domain_set(&pdev->dev, &omap_device_fail_pm_domain);
  175. return ret;
  176. }
  177. static int _omap_device_notifier_call(struct notifier_block *nb,
  178. unsigned long event, void *dev)
  179. {
  180. struct platform_device *pdev = to_platform_device(dev);
  181. struct omap_device *od;
  182. int err;
  183. switch (event) {
  184. case BUS_NOTIFY_REMOVED_DEVICE:
  185. if (pdev->archdata.od)
  186. omap_device_delete(pdev->archdata.od);
  187. break;
  188. case BUS_NOTIFY_UNBOUND_DRIVER:
  189. od = to_omap_device(pdev);
  190. if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED)) {
  191. dev_info(dev, "enabled after unload, idling\n");
  192. err = omap_device_idle(pdev);
  193. if (err)
  194. dev_err(dev, "failed to idle\n");
  195. }
  196. break;
  197. case BUS_NOTIFY_BIND_DRIVER:
  198. od = to_omap_device(pdev);
  199. if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED) &&
  200. pm_runtime_status_suspended(dev)) {
  201. od->_driver_status = BUS_NOTIFY_BIND_DRIVER;
  202. pm_runtime_set_active(dev);
  203. }
  204. break;
  205. case BUS_NOTIFY_ADD_DEVICE:
  206. if (pdev->dev.of_node)
  207. omap_device_build_from_dt(pdev);
  208. omap_auxdata_legacy_init(dev);
  209. /* fall through */
  210. default:
  211. od = to_omap_device(pdev);
  212. if (od)
  213. od->_driver_status = event;
  214. }
  215. return NOTIFY_DONE;
  216. }
  217. /**
  218. * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  219. * @od: struct omap_device *od
  220. *
  221. * Enable all underlying hwmods. Returns 0.
  222. */
  223. static int _omap_device_enable_hwmods(struct omap_device *od)
  224. {
  225. int ret = 0;
  226. int i;
  227. for (i = 0; i < od->hwmods_cnt; i++)
  228. ret |= omap_hwmod_enable(od->hwmods[i]);
  229. return ret;
  230. }
  231. /**
  232. * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  233. * @od: struct omap_device *od
  234. *
  235. * Idle all underlying hwmods. Returns 0.
  236. */
  237. static int _omap_device_idle_hwmods(struct omap_device *od)
  238. {
  239. int ret = 0;
  240. int i;
  241. for (i = 0; i < od->hwmods_cnt; i++)
  242. ret |= omap_hwmod_idle(od->hwmods[i]);
  243. return ret;
  244. }
  245. /* Public functions for use by core code */
  246. /**
  247. * omap_device_get_context_loss_count - get lost context count
  248. * @od: struct omap_device *
  249. *
  250. * Using the primary hwmod, query the context loss count for this
  251. * device.
  252. *
  253. * Callers should consider context for this device lost any time this
  254. * function returns a value different than the value the caller got
  255. * the last time it called this function.
  256. *
  257. * If any hwmods exist for the omap_device associated with @pdev,
  258. * return the context loss counter for that hwmod, otherwise return
  259. * zero.
  260. */
  261. int omap_device_get_context_loss_count(struct platform_device *pdev)
  262. {
  263. struct omap_device *od;
  264. u32 ret = 0;
  265. od = to_omap_device(pdev);
  266. if (od->hwmods_cnt)
  267. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  268. return ret;
  269. }
  270. /**
  271. * omap_device_alloc - allocate an omap_device
  272. * @pdev: platform_device that will be included in this omap_device
  273. * @oh: ptr to the single omap_hwmod that backs this omap_device
  274. * @pdata: platform_data ptr to associate with the platform_device
  275. * @pdata_len: amount of memory pointed to by @pdata
  276. *
  277. * Convenience function for allocating an omap_device structure and filling
  278. * hwmods, and resources.
  279. *
  280. * Returns an struct omap_device pointer or ERR_PTR() on error;
  281. */
  282. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  283. struct omap_hwmod **ohs, int oh_cnt)
  284. {
  285. int ret = -ENOMEM;
  286. struct omap_device *od;
  287. int i;
  288. struct omap_hwmod **hwmods;
  289. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  290. if (!od) {
  291. ret = -ENOMEM;
  292. goto oda_exit1;
  293. }
  294. od->hwmods_cnt = oh_cnt;
  295. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  296. if (!hwmods)
  297. goto oda_exit2;
  298. od->hwmods = hwmods;
  299. od->pdev = pdev;
  300. pdev->archdata.od = od;
  301. for (i = 0; i < oh_cnt; i++) {
  302. hwmods[i]->od = od;
  303. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  304. }
  305. return od;
  306. oda_exit2:
  307. kfree(od);
  308. oda_exit1:
  309. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  310. return ERR_PTR(ret);
  311. }
  312. void omap_device_delete(struct omap_device *od)
  313. {
  314. if (!od)
  315. return;
  316. od->pdev->archdata.od = NULL;
  317. kfree(od->hwmods);
  318. kfree(od);
  319. }
  320. /**
  321. * omap_device_copy_resources - Add legacy IO and IRQ resources
  322. * @oh: interconnect target module
  323. * @pdev: platform device to copy resources to
  324. *
  325. * We still have legacy DMA and smartreflex needing resources.
  326. * Let's populate what they need until we can eventually just
  327. * remove this function. Note that there should be no need to
  328. * call this from omap_device_build_from_dt(), nor should there
  329. * be any need to call it for other devices.
  330. */
  331. static int
  332. omap_device_copy_resources(struct omap_hwmod *oh,
  333. struct platform_device *pdev)
  334. {
  335. struct device_node *np, *child;
  336. struct property *prop;
  337. struct resource *res;
  338. const char *name;
  339. int error, irq = 0;
  340. if (!oh || !oh->od || !oh->od->pdev) {
  341. error = -EINVAL;
  342. goto error;
  343. }
  344. np = oh->od->pdev->dev.of_node;
  345. if (!np) {
  346. error = -ENODEV;
  347. goto error;
  348. }
  349. res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
  350. if (!res)
  351. return -ENOMEM;
  352. /* Do we have a dts range for the interconnect target module? */
  353. error = omap_hwmod_parse_module_range(oh, np, res);
  354. /* No ranges, rely on device reg entry */
  355. if (error)
  356. error = of_address_to_resource(np, 0, res);
  357. if (error)
  358. goto free;
  359. /* SmartReflex needs first IO resource name to be "mpu" */
  360. res[0].name = "mpu";
  361. /*
  362. * We may have a configured "ti,sysc" interconnect target with a
  363. * dts child with the interrupt. If so use the first child's
  364. * first interrupt for "ti-hwmods" legacy support.
  365. */
  366. of_property_for_each_string(np, "compatible", prop, name)
  367. if (!strncmp("ti,sysc-", name, 8))
  368. break;
  369. child = of_get_next_available_child(np, NULL);
  370. if (name)
  371. irq = irq_of_parse_and_map(child, 0);
  372. if (!irq)
  373. irq = irq_of_parse_and_map(np, 0);
  374. if (!irq)
  375. goto free;
  376. /* Legacy DMA code needs interrupt name to be "0" */
  377. res[1].start = irq;
  378. res[1].end = irq;
  379. res[1].flags = IORESOURCE_IRQ;
  380. res[1].name = "0";
  381. error = platform_device_add_resources(pdev, res, 2);
  382. free:
  383. kfree(res);
  384. error:
  385. WARN(error, "%s: %s device %s failed: %i\n",
  386. __func__, oh->name, dev_name(&pdev->dev),
  387. error);
  388. return error;
  389. }
  390. /**
  391. * omap_device_build - build and register an omap_device with one omap_hwmod
  392. * @pdev_name: name of the platform_device driver to use
  393. * @pdev_id: this platform_device's connection ID
  394. * @oh: ptr to the single omap_hwmod that backs this omap_device
  395. * @pdata: platform_data ptr to associate with the platform_device
  396. * @pdata_len: amount of memory pointed to by @pdata
  397. *
  398. * Convenience function for building and registering a single
  399. * omap_device record, which in turn builds and registers a
  400. * platform_device record. See omap_device_build_ss() for more
  401. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  402. * passes along the return value of omap_device_build_ss().
  403. */
  404. struct platform_device __init *omap_device_build(const char *pdev_name,
  405. int pdev_id,
  406. struct omap_hwmod *oh,
  407. void *pdata, int pdata_len)
  408. {
  409. int ret = -ENOMEM;
  410. struct platform_device *pdev;
  411. struct omap_device *od;
  412. if (!oh || !pdev_name)
  413. return ERR_PTR(-EINVAL);
  414. if (!pdata && pdata_len > 0)
  415. return ERR_PTR(-EINVAL);
  416. if (strncmp(oh->name, "smartreflex", 11) &&
  417. strncmp(oh->name, "dma", 3)) {
  418. pr_warn("%s need to update %s to probe with dt\na",
  419. __func__, pdev_name);
  420. ret = -ENODEV;
  421. goto odbs_exit;
  422. }
  423. pdev = platform_device_alloc(pdev_name, pdev_id);
  424. if (!pdev) {
  425. ret = -ENOMEM;
  426. goto odbs_exit;
  427. }
  428. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  429. if (pdev->id != -1)
  430. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  431. else
  432. dev_set_name(&pdev->dev, "%s", pdev->name);
  433. /*
  434. * Must be called before omap_device_alloc() as oh->od
  435. * only contains the currently registered omap_device
  436. * and will get overwritten by omap_device_alloc().
  437. */
  438. ret = omap_device_copy_resources(oh, pdev);
  439. if (ret)
  440. goto odbs_exit1;
  441. od = omap_device_alloc(pdev, &oh, 1);
  442. if (IS_ERR(od))
  443. goto odbs_exit1;
  444. ret = platform_device_add_data(pdev, pdata, pdata_len);
  445. if (ret)
  446. goto odbs_exit2;
  447. ret = omap_device_register(pdev);
  448. if (ret)
  449. goto odbs_exit2;
  450. return pdev;
  451. odbs_exit2:
  452. omap_device_delete(od);
  453. odbs_exit1:
  454. platform_device_put(pdev);
  455. odbs_exit:
  456. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  457. return ERR_PTR(ret);
  458. }
  459. #ifdef CONFIG_PM
  460. static int _od_runtime_suspend(struct device *dev)
  461. {
  462. struct platform_device *pdev = to_platform_device(dev);
  463. int ret;
  464. ret = pm_generic_runtime_suspend(dev);
  465. if (ret)
  466. return ret;
  467. return omap_device_idle(pdev);
  468. }
  469. static int _od_runtime_resume(struct device *dev)
  470. {
  471. struct platform_device *pdev = to_platform_device(dev);
  472. int ret;
  473. ret = omap_device_enable(pdev);
  474. if (ret) {
  475. dev_err(dev, "use pm_runtime_put_sync_suspend() in driver?\n");
  476. return ret;
  477. }
  478. return pm_generic_runtime_resume(dev);
  479. }
  480. static int _od_fail_runtime_suspend(struct device *dev)
  481. {
  482. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  483. return -ENODEV;
  484. }
  485. static int _od_fail_runtime_resume(struct device *dev)
  486. {
  487. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  488. return -ENODEV;
  489. }
  490. #endif
  491. #ifdef CONFIG_SUSPEND
  492. static int _od_suspend_noirq(struct device *dev)
  493. {
  494. struct platform_device *pdev = to_platform_device(dev);
  495. struct omap_device *od = to_omap_device(pdev);
  496. int ret;
  497. /* Don't attempt late suspend on a driver that is not bound */
  498. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
  499. return 0;
  500. ret = pm_generic_suspend_noirq(dev);
  501. if (!ret && !pm_runtime_status_suspended(dev)) {
  502. if (pm_generic_runtime_suspend(dev) == 0) {
  503. omap_device_idle(pdev);
  504. od->flags |= OMAP_DEVICE_SUSPENDED;
  505. }
  506. }
  507. return ret;
  508. }
  509. static int _od_resume_noirq(struct device *dev)
  510. {
  511. struct platform_device *pdev = to_platform_device(dev);
  512. struct omap_device *od = to_omap_device(pdev);
  513. if (od->flags & OMAP_DEVICE_SUSPENDED) {
  514. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  515. omap_device_enable(pdev);
  516. pm_generic_runtime_resume(dev);
  517. }
  518. return pm_generic_resume_noirq(dev);
  519. }
  520. #else
  521. #define _od_suspend_noirq NULL
  522. #define _od_resume_noirq NULL
  523. #endif
  524. struct dev_pm_domain omap_device_fail_pm_domain = {
  525. .ops = {
  526. SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
  527. _od_fail_runtime_resume, NULL)
  528. }
  529. };
  530. struct dev_pm_domain omap_device_pm_domain = {
  531. .ops = {
  532. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  533. NULL)
  534. USE_PLATFORM_PM_SLEEP_OPS
  535. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
  536. _od_resume_noirq)
  537. }
  538. };
  539. /**
  540. * omap_device_register - register an omap_device with one omap_hwmod
  541. * @od: struct omap_device * to register
  542. *
  543. * Register the omap_device structure. This currently just calls
  544. * platform_device_register() on the underlying platform_device.
  545. * Returns the return value of platform_device_register().
  546. */
  547. int omap_device_register(struct platform_device *pdev)
  548. {
  549. pr_debug("omap_device: %s: registering\n", pdev->name);
  550. dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
  551. return platform_device_add(pdev);
  552. }
  553. /* Public functions for use by device drivers through struct platform_data */
  554. /**
  555. * omap_device_enable - fully activate an omap_device
  556. * @od: struct omap_device * to activate
  557. *
  558. * Do whatever is necessary for the hwmods underlying omap_device @od
  559. * to be accessible and ready to operate. This generally involves
  560. * enabling clocks, setting SYSCONFIG registers; and in the future may
  561. * involve remuxing pins. Device drivers should call this function
  562. * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
  563. * the omap_device is already enabled, or passes along the return
  564. * value of _omap_device_enable_hwmods().
  565. */
  566. int omap_device_enable(struct platform_device *pdev)
  567. {
  568. int ret;
  569. struct omap_device *od;
  570. od = to_omap_device(pdev);
  571. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  572. dev_warn(&pdev->dev,
  573. "omap_device: %s() called from invalid state %d\n",
  574. __func__, od->_state);
  575. return -EINVAL;
  576. }
  577. ret = _omap_device_enable_hwmods(od);
  578. if (ret == 0)
  579. od->_state = OMAP_DEVICE_STATE_ENABLED;
  580. return ret;
  581. }
  582. /**
  583. * omap_device_idle - idle an omap_device
  584. * @od: struct omap_device * to idle
  585. *
  586. * Idle omap_device @od. Device drivers call this function indirectly
  587. * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not
  588. * currently enabled, or passes along the return value of
  589. * _omap_device_idle_hwmods().
  590. */
  591. int omap_device_idle(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_idle_hwmods(od);
  603. if (ret == 0)
  604. od->_state = OMAP_DEVICE_STATE_IDLE;
  605. return ret;
  606. }
  607. /**
  608. * omap_device_assert_hardreset - set a device's hardreset line
  609. * @pdev: struct platform_device * to reset
  610. * @name: const char * name of the reset line
  611. *
  612. * Set the hardreset line identified by @name on the IP blocks
  613. * associated with the hwmods backing the platform_device @pdev. All
  614. * of the hwmods associated with @pdev must have the same hardreset
  615. * line linked to them for this to work. Passes along the return value
  616. * of omap_hwmod_assert_hardreset() in the event of any failure, or
  617. * returns 0 upon success.
  618. */
  619. int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
  620. {
  621. struct omap_device *od = to_omap_device(pdev);
  622. int ret = 0;
  623. int i;
  624. for (i = 0; i < od->hwmods_cnt; i++) {
  625. ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
  626. if (ret)
  627. break;
  628. }
  629. return ret;
  630. }
  631. /**
  632. * omap_device_deassert_hardreset - release a device's hardreset line
  633. * @pdev: struct platform_device * to reset
  634. * @name: const char * name of the reset line
  635. *
  636. * Release the hardreset line identified by @name on the IP blocks
  637. * associated with the hwmods backing the platform_device @pdev. All
  638. * of the hwmods associated with @pdev must have the same hardreset
  639. * line linked to them for this to work. Passes along the return
  640. * value of omap_hwmod_deassert_hardreset() in the event of any
  641. * failure, or returns 0 upon success.
  642. */
  643. int omap_device_deassert_hardreset(struct platform_device *pdev,
  644. const char *name)
  645. {
  646. struct omap_device *od = to_omap_device(pdev);
  647. int ret = 0;
  648. int i;
  649. for (i = 0; i < od->hwmods_cnt; i++) {
  650. ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
  651. if (ret)
  652. break;
  653. }
  654. return ret;
  655. }
  656. /**
  657. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  658. * device pointer.
  659. * @oh_name: name of the hwmod device
  660. *
  661. * Returns back a struct device * pointer associated with a hwmod
  662. * device represented by a hwmod_name
  663. */
  664. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  665. {
  666. struct omap_hwmod *oh;
  667. if (!oh_name) {
  668. WARN(1, "%s: no hwmod name!\n", __func__);
  669. return ERR_PTR(-EINVAL);
  670. }
  671. oh = omap_hwmod_lookup(oh_name);
  672. if (!oh) {
  673. WARN(1, "%s: no hwmod for %s\n", __func__,
  674. oh_name);
  675. return ERR_PTR(-ENODEV);
  676. }
  677. if (!oh->od) {
  678. WARN(1, "%s: no omap_device for %s\n", __func__,
  679. oh_name);
  680. return ERR_PTR(-ENODEV);
  681. }
  682. return &oh->od->pdev->dev;
  683. }
  684. static struct notifier_block platform_nb = {
  685. .notifier_call = _omap_device_notifier_call,
  686. };
  687. static int __init omap_device_init(void)
  688. {
  689. bus_register_notifier(&platform_bus_type, &platform_nb);
  690. return 0;
  691. }
  692. omap_postcore_initcall(omap_device_init);
  693. /**
  694. * omap_device_late_idle - idle devices without drivers
  695. * @dev: struct device * associated with omap_device
  696. * @data: unused
  697. *
  698. * Check the driver bound status of this device, and idle it
  699. * if there is no driver attached.
  700. */
  701. static int __init omap_device_late_idle(struct device *dev, void *data)
  702. {
  703. struct platform_device *pdev = to_platform_device(dev);
  704. struct omap_device *od = to_omap_device(pdev);
  705. int i;
  706. if (!od)
  707. return 0;
  708. /*
  709. * If omap_device state is enabled, but has no driver bound,
  710. * idle it.
  711. */
  712. /*
  713. * Some devices (like memory controllers) are always kept
  714. * enabled, and should not be idled even with no drivers.
  715. */
  716. for (i = 0; i < od->hwmods_cnt; i++)
  717. if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
  718. return 0;
  719. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
  720. od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
  721. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  722. dev_warn(dev, "%s: enabled but no driver. Idling\n",
  723. __func__);
  724. omap_device_idle(pdev);
  725. }
  726. }
  727. return 0;
  728. }
  729. static int __init omap_device_late_init(void)
  730. {
  731. bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
  732. return 0;
  733. }
  734. omap_late_initcall_sync(omap_device_late_init);