omap_device.c 22 KB

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