omap_device.c 22 KB

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