omap_device.c 21 KB

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