omap_device.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * omap_device implementation
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. * Paul Walmsley, Kevin Hilman
  6. *
  7. * Developed in collaboration with (alphabetical order): Benoit
  8. * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
  9. * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
  10. * Woodruff
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * This code provides a consistent interface for OMAP device drivers
  17. * to control power management and interconnect properties of their
  18. * devices.
  19. *
  20. * In the medium- to long-term, this code should be implemented as a
  21. * proper omap_bus/omap_device in Linux, no more platform_data func
  22. * pointers
  23. *
  24. *
  25. */
  26. #undef DEBUG
  27. #include <linux/kernel.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/err.h>
  31. #include <linux/io.h>
  32. #include <linux/clk.h>
  33. #include <linux/clkdev.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/of.h>
  36. #include <linux/notifier.h>
  37. #include "soc.h"
  38. #include "omap_device.h"
  39. #include "omap_hwmod.h"
  40. /* Private functions */
  41. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  42. const char *clk_name)
  43. {
  44. struct clk *r;
  45. struct clk_lookup *l;
  46. if (!clk_alias || !clk_name)
  47. return;
  48. dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
  49. r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
  50. if (!IS_ERR(r)) {
  51. dev_warn(&od->pdev->dev,
  52. "alias %s already exists\n", clk_alias);
  53. clk_put(r);
  54. return;
  55. }
  56. r = clk_get(NULL, clk_name);
  57. if (IS_ERR(r)) {
  58. dev_err(&od->pdev->dev,
  59. "clk_get for %s failed\n", clk_name);
  60. return;
  61. }
  62. l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
  63. if (!l) {
  64. dev_err(&od->pdev->dev,
  65. "clkdev_alloc for %s failed\n", clk_alias);
  66. return;
  67. }
  68. clkdev_add(l);
  69. }
  70. /**
  71. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  72. * and main clock
  73. * @od: struct omap_device *od
  74. * @oh: struct omap_hwmod *oh
  75. *
  76. * For the main clock and every optional clock present per hwmod per
  77. * omap_device, this function adds an entry in the clkdev table of the
  78. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  79. *
  80. * The function is called from inside omap_device_build_ss(), after
  81. * omap_device_register.
  82. *
  83. * This allows drivers to get a pointer to its optional clocks based on its role
  84. * by calling clk_get(<dev*>, <role>).
  85. * In the case of the main clock, a "fck" alias is used.
  86. *
  87. * No return value.
  88. */
  89. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  90. struct omap_hwmod *oh)
  91. {
  92. int i;
  93. _add_clkdev(od, "fck", oh->main_clk);
  94. for (i = 0; i < oh->opt_clks_cnt; i++)
  95. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  96. }
  97. /**
  98. * omap_device_build_from_dt - build an omap_device with multiple hwmods
  99. * @pdev_name: name of the platform_device driver to use
  100. * @pdev_id: this platform_device's connection ID
  101. * @oh: ptr to the single omap_hwmod that backs this omap_device
  102. * @pdata: platform_data ptr to associate with the platform_device
  103. * @pdata_len: amount of memory pointed to by @pdata
  104. *
  105. * Function for building an omap_device already registered from device-tree
  106. *
  107. * Returns 0 or PTR_ERR() on error.
  108. */
  109. static int omap_device_build_from_dt(struct platform_device *pdev)
  110. {
  111. struct omap_hwmod **hwmods;
  112. struct omap_device *od;
  113. struct omap_hwmod *oh;
  114. struct device_node *node = pdev->dev.of_node;
  115. const char *oh_name;
  116. int oh_cnt, i, ret = 0;
  117. oh_cnt = of_property_count_strings(node, "ti,hwmods");
  118. if (oh_cnt <= 0) {
  119. dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
  120. return -ENODEV;
  121. }
  122. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  123. if (!hwmods) {
  124. ret = -ENOMEM;
  125. goto odbfd_exit;
  126. }
  127. for (i = 0; i < oh_cnt; i++) {
  128. of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
  129. oh = omap_hwmod_lookup(oh_name);
  130. if (!oh) {
  131. dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
  132. oh_name);
  133. ret = -EINVAL;
  134. goto odbfd_exit1;
  135. }
  136. hwmods[i] = oh;
  137. }
  138. od = omap_device_alloc(pdev, hwmods, oh_cnt);
  139. if (!od) {
  140. dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
  141. oh_name);
  142. ret = PTR_ERR(od);
  143. goto odbfd_exit1;
  144. }
  145. /* Fix up missing resource names */
  146. for (i = 0; i < pdev->num_resources; i++) {
  147. struct resource *r = &pdev->resource[i];
  148. if (r->name == NULL)
  149. r->name = dev_name(&pdev->dev);
  150. }
  151. pdev->dev.pm_domain = &omap_device_pm_domain;
  152. odbfd_exit1:
  153. kfree(hwmods);
  154. odbfd_exit:
  155. return ret;
  156. }
  157. static int _omap_device_notifier_call(struct notifier_block *nb,
  158. unsigned long event, void *dev)
  159. {
  160. struct platform_device *pdev = to_platform_device(dev);
  161. struct omap_device *od;
  162. switch (event) {
  163. case BUS_NOTIFY_DEL_DEVICE:
  164. if (pdev->archdata.od)
  165. omap_device_delete(pdev->archdata.od);
  166. break;
  167. case BUS_NOTIFY_ADD_DEVICE:
  168. if (pdev->dev.of_node)
  169. omap_device_build_from_dt(pdev);
  170. /* fall through */
  171. default:
  172. od = to_omap_device(pdev);
  173. if (od)
  174. od->_driver_status = event;
  175. }
  176. return NOTIFY_DONE;
  177. }
  178. /**
  179. * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  180. * @od: struct omap_device *od
  181. *
  182. * Enable all underlying hwmods. Returns 0.
  183. */
  184. static int _omap_device_enable_hwmods(struct omap_device *od)
  185. {
  186. int i;
  187. for (i = 0; i < od->hwmods_cnt; i++)
  188. omap_hwmod_enable(od->hwmods[i]);
  189. /* XXX pass along return value here? */
  190. return 0;
  191. }
  192. /**
  193. * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  194. * @od: struct omap_device *od
  195. *
  196. * Idle all underlying hwmods. Returns 0.
  197. */
  198. static int _omap_device_idle_hwmods(struct omap_device *od)
  199. {
  200. int i;
  201. for (i = 0; i < od->hwmods_cnt; i++)
  202. omap_hwmod_idle(od->hwmods[i]);
  203. /* XXX pass along return value here? */
  204. return 0;
  205. }
  206. /* Public functions for use by core code */
  207. /**
  208. * omap_device_get_context_loss_count - get lost context count
  209. * @od: struct omap_device *
  210. *
  211. * Using the primary hwmod, query the context loss count for this
  212. * device.
  213. *
  214. * Callers should consider context for this device lost any time this
  215. * function returns a value different than the value the caller got
  216. * the last time it called this function.
  217. *
  218. * If any hwmods exist for the omap_device assoiated with @pdev,
  219. * return the context loss counter for that hwmod, otherwise return
  220. * zero.
  221. */
  222. int omap_device_get_context_loss_count(struct platform_device *pdev)
  223. {
  224. struct omap_device *od;
  225. u32 ret = 0;
  226. od = to_omap_device(pdev);
  227. if (od->hwmods_cnt)
  228. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  229. return ret;
  230. }
  231. /**
  232. * omap_device_count_resources - count number of struct resource entries needed
  233. * @od: struct omap_device *
  234. * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
  235. *
  236. * Count the number of struct resource entries needed for this
  237. * omap_device @od. Used by omap_device_build_ss() to determine how
  238. * much memory to allocate before calling
  239. * omap_device_fill_resources(). Returns the count.
  240. */
  241. static int omap_device_count_resources(struct omap_device *od,
  242. unsigned long flags)
  243. {
  244. int c = 0;
  245. int i;
  246. for (i = 0; i < od->hwmods_cnt; i++)
  247. c += omap_hwmod_count_resources(od->hwmods[i], flags);
  248. pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
  249. od->pdev->name, c, od->hwmods_cnt);
  250. return c;
  251. }
  252. /**
  253. * omap_device_fill_resources - fill in array of struct resource
  254. * @od: struct omap_device *
  255. * @res: pointer to an array of struct resource to be filled in
  256. *
  257. * Populate one or more empty struct resource pointed to by @res with
  258. * the resource data for this omap_device @od. Used by
  259. * omap_device_build_ss() after calling omap_device_count_resources().
  260. * Ideally this function would not be needed at all. If omap_device
  261. * replaces platform_device, then we can specify our own
  262. * get_resource()/ get_irq()/etc functions that use the underlying
  263. * omap_hwmod information. Or if platform_device is extended to use
  264. * subarchitecture-specific function pointers, the various
  265. * platform_device functions can simply call omap_device internal
  266. * functions to get device resources. Hacking around the existing
  267. * platform_device code wastes memory. Returns 0.
  268. */
  269. static int omap_device_fill_resources(struct omap_device *od,
  270. struct resource *res)
  271. {
  272. int i, r;
  273. for (i = 0; i < od->hwmods_cnt; i++) {
  274. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  275. res += r;
  276. }
  277. return 0;
  278. }
  279. /**
  280. * _od_fill_dma_resources - fill in array of struct resource with dma resources
  281. * @od: struct omap_device *
  282. * @res: pointer to an array of struct resource to be filled in
  283. *
  284. * Populate one or more empty struct resource pointed to by @res with
  285. * the dma resource data for this omap_device @od. Used by
  286. * omap_device_alloc() after calling omap_device_count_resources().
  287. *
  288. * Ideally this function would not be needed at all. If we have
  289. * mechanism to get dma resources from DT.
  290. *
  291. * Returns 0.
  292. */
  293. static int _od_fill_dma_resources(struct omap_device *od,
  294. struct resource *res)
  295. {
  296. int i, r;
  297. for (i = 0; i < od->hwmods_cnt; i++) {
  298. r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
  299. res += r;
  300. }
  301. return 0;
  302. }
  303. /**
  304. * omap_device_alloc - allocate an omap_device
  305. * @pdev: platform_device that will be included in this omap_device
  306. * @oh: ptr to the single omap_hwmod that backs this omap_device
  307. * @pdata: platform_data ptr to associate with the platform_device
  308. * @pdata_len: amount of memory pointed to by @pdata
  309. *
  310. * Convenience function for allocating an omap_device structure and filling
  311. * hwmods, and resources.
  312. *
  313. * Returns an struct omap_device pointer or ERR_PTR() on error;
  314. */
  315. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  316. struct omap_hwmod **ohs, int oh_cnt)
  317. {
  318. int ret = -ENOMEM;
  319. struct omap_device *od;
  320. struct resource *res = NULL;
  321. int i, res_count;
  322. struct omap_hwmod **hwmods;
  323. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  324. if (!od) {
  325. ret = -ENOMEM;
  326. goto oda_exit1;
  327. }
  328. od->hwmods_cnt = oh_cnt;
  329. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  330. if (!hwmods)
  331. goto oda_exit2;
  332. od->hwmods = hwmods;
  333. od->pdev = pdev;
  334. /*
  335. * Non-DT Boot:
  336. * Here, pdev->num_resources = 0, and we should get all the
  337. * resources from hwmod.
  338. *
  339. * DT Boot:
  340. * OF framework will construct the resource structure (currently
  341. * does for MEM & IRQ resource) and we should respect/use these
  342. * resources, killing hwmod dependency.
  343. * If pdev->num_resources > 0, we assume that MEM & IRQ resources
  344. * have been allocated by OF layer already (through DTB).
  345. * As preparation for the future we examine the OF provided resources
  346. * to see if we have DMA resources provided already. In this case
  347. * there is no need to update the resources for the device, we use the
  348. * OF provided ones.
  349. *
  350. * TODO: Once DMA resource is available from OF layer, we should
  351. * kill filling any resources from hwmod.
  352. */
  353. if (!pdev->num_resources) {
  354. /* Count all resources for the device */
  355. res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
  356. IORESOURCE_DMA |
  357. IORESOURCE_MEM);
  358. } else {
  359. /* Take a look if we already have DMA resource via DT */
  360. for (i = 0; i < pdev->num_resources; i++) {
  361. struct resource *r = &pdev->resource[i];
  362. /* We have it, no need to touch the resources */
  363. if (r->flags == IORESOURCE_DMA)
  364. goto have_everything;
  365. }
  366. /* Count only DMA resources for the device */
  367. res_count = omap_device_count_resources(od, IORESOURCE_DMA);
  368. /* The device has no DMA resource, no need for update */
  369. if (!res_count)
  370. goto have_everything;
  371. res_count += pdev->num_resources;
  372. }
  373. /* Allocate resources memory to account for new resources */
  374. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  375. if (!res)
  376. goto oda_exit3;
  377. if (!pdev->num_resources) {
  378. dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
  379. __func__, res_count);
  380. omap_device_fill_resources(od, res);
  381. } else {
  382. dev_dbg(&pdev->dev,
  383. "%s: appending %d DMA resources from hwmod\n",
  384. __func__, res_count - pdev->num_resources);
  385. memcpy(res, pdev->resource,
  386. sizeof(struct resource) * pdev->num_resources);
  387. _od_fill_dma_resources(od, &res[pdev->num_resources]);
  388. }
  389. ret = platform_device_add_resources(pdev, res, res_count);
  390. kfree(res);
  391. if (ret)
  392. goto oda_exit3;
  393. have_everything:
  394. pdev->archdata.od = od;
  395. for (i = 0; i < oh_cnt; i++) {
  396. hwmods[i]->od = od;
  397. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  398. }
  399. return od;
  400. oda_exit3:
  401. kfree(hwmods);
  402. oda_exit2:
  403. kfree(od);
  404. oda_exit1:
  405. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  406. return ERR_PTR(ret);
  407. }
  408. void omap_device_delete(struct omap_device *od)
  409. {
  410. if (!od)
  411. return;
  412. od->pdev->archdata.od = NULL;
  413. kfree(od->hwmods);
  414. kfree(od);
  415. }
  416. /**
  417. * omap_device_build - build and register an omap_device with one omap_hwmod
  418. * @pdev_name: name of the platform_device driver to use
  419. * @pdev_id: this platform_device's connection ID
  420. * @oh: ptr to the single omap_hwmod that backs this omap_device
  421. * @pdata: platform_data ptr to associate with the platform_device
  422. * @pdata_len: amount of memory pointed to by @pdata
  423. *
  424. * Convenience function for building and registering a single
  425. * omap_device record, which in turn builds and registers a
  426. * platform_device record. See omap_device_build_ss() for more
  427. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  428. * passes along the return value of omap_device_build_ss().
  429. */
  430. struct platform_device __init *omap_device_build(const char *pdev_name,
  431. int pdev_id,
  432. struct omap_hwmod *oh,
  433. void *pdata, int pdata_len)
  434. {
  435. struct omap_hwmod *ohs[] = { oh };
  436. if (!oh)
  437. return ERR_PTR(-EINVAL);
  438. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  439. pdata_len);
  440. }
  441. /**
  442. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  443. * @pdev_name: name of the platform_device driver to use
  444. * @pdev_id: this platform_device's connection ID
  445. * @oh: ptr to the single omap_hwmod that backs this omap_device
  446. * @pdata: platform_data ptr to associate with the platform_device
  447. * @pdata_len: amount of memory pointed to by @pdata
  448. *
  449. * Convenience function for building and registering an omap_device
  450. * subsystem record. Subsystem records consist of multiple
  451. * omap_hwmods. This function in turn builds and registers a
  452. * platform_device record. Returns an ERR_PTR() on error, or passes
  453. * along the return value of omap_device_register().
  454. */
  455. struct platform_device __init *omap_device_build_ss(const char *pdev_name,
  456. int pdev_id,
  457. struct omap_hwmod **ohs,
  458. int oh_cnt, void *pdata,
  459. int pdata_len)
  460. {
  461. int ret = -ENOMEM;
  462. struct platform_device *pdev;
  463. struct omap_device *od;
  464. if (!ohs || oh_cnt == 0 || !pdev_name)
  465. return ERR_PTR(-EINVAL);
  466. if (!pdata && pdata_len > 0)
  467. return ERR_PTR(-EINVAL);
  468. pdev = platform_device_alloc(pdev_name, pdev_id);
  469. if (!pdev) {
  470. ret = -ENOMEM;
  471. goto odbs_exit;
  472. }
  473. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  474. if (pdev->id != -1)
  475. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  476. else
  477. dev_set_name(&pdev->dev, "%s", pdev->name);
  478. od = omap_device_alloc(pdev, ohs, oh_cnt);
  479. if (IS_ERR(od))
  480. goto odbs_exit1;
  481. ret = platform_device_add_data(pdev, pdata, pdata_len);
  482. if (ret)
  483. goto odbs_exit2;
  484. ret = omap_device_register(pdev);
  485. if (ret)
  486. goto odbs_exit2;
  487. return pdev;
  488. odbs_exit2:
  489. omap_device_delete(od);
  490. odbs_exit1:
  491. platform_device_put(pdev);
  492. odbs_exit:
  493. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  494. return ERR_PTR(ret);
  495. }
  496. #ifdef CONFIG_PM_RUNTIME
  497. static int _od_runtime_suspend(struct device *dev)
  498. {
  499. struct platform_device *pdev = to_platform_device(dev);
  500. int ret;
  501. ret = pm_generic_runtime_suspend(dev);
  502. if (!ret)
  503. omap_device_idle(pdev);
  504. return ret;
  505. }
  506. static int _od_runtime_resume(struct device *dev)
  507. {
  508. struct platform_device *pdev = to_platform_device(dev);
  509. omap_device_enable(pdev);
  510. return pm_generic_runtime_resume(dev);
  511. }
  512. #endif
  513. #ifdef CONFIG_SUSPEND
  514. static int _od_suspend_noirq(struct device *dev)
  515. {
  516. struct platform_device *pdev = to_platform_device(dev);
  517. struct omap_device *od = to_omap_device(pdev);
  518. int ret;
  519. /* Don't attempt late suspend on a driver that is not bound */
  520. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
  521. return 0;
  522. ret = pm_generic_suspend_noirq(dev);
  523. if (!ret && !pm_runtime_status_suspended(dev)) {
  524. if (pm_generic_runtime_suspend(dev) == 0) {
  525. omap_device_idle(pdev);
  526. od->flags |= OMAP_DEVICE_SUSPENDED;
  527. }
  528. }
  529. return ret;
  530. }
  531. static int _od_resume_noirq(struct device *dev)
  532. {
  533. struct platform_device *pdev = to_platform_device(dev);
  534. struct omap_device *od = to_omap_device(pdev);
  535. if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
  536. !pm_runtime_status_suspended(dev)) {
  537. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  538. omap_device_enable(pdev);
  539. pm_generic_runtime_resume(dev);
  540. }
  541. return pm_generic_resume_noirq(dev);
  542. }
  543. #else
  544. #define _od_suspend_noirq NULL
  545. #define _od_resume_noirq NULL
  546. #endif
  547. struct dev_pm_domain omap_device_pm_domain = {
  548. .ops = {
  549. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  550. NULL)
  551. USE_PLATFORM_PM_SLEEP_OPS
  552. .suspend_noirq = _od_suspend_noirq,
  553. .resume_noirq = _od_resume_noirq,
  554. }
  555. };
  556. /**
  557. * omap_device_register - register an omap_device with one omap_hwmod
  558. * @od: struct omap_device * to register
  559. *
  560. * Register the omap_device structure. This currently just calls
  561. * platform_device_register() on the underlying platform_device.
  562. * Returns the return value of platform_device_register().
  563. */
  564. int omap_device_register(struct platform_device *pdev)
  565. {
  566. pr_debug("omap_device: %s: registering\n", pdev->name);
  567. pdev->dev.pm_domain = &omap_device_pm_domain;
  568. return platform_device_add(pdev);
  569. }
  570. /* Public functions for use by device drivers through struct platform_data */
  571. /**
  572. * omap_device_enable - fully activate an omap_device
  573. * @od: struct omap_device * to activate
  574. *
  575. * Do whatever is necessary for the hwmods underlying omap_device @od
  576. * to be accessible and ready to operate. This generally involves
  577. * enabling clocks, setting SYSCONFIG registers; and in the future may
  578. * involve remuxing pins. Device drivers should call this function
  579. * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
  580. * the omap_device is already enabled, or passes along the return
  581. * value of _omap_device_enable_hwmods().
  582. */
  583. int omap_device_enable(struct platform_device *pdev)
  584. {
  585. int ret;
  586. struct omap_device *od;
  587. od = to_omap_device(pdev);
  588. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  589. dev_warn(&pdev->dev,
  590. "omap_device: %s() called from invalid state %d\n",
  591. __func__, od->_state);
  592. return -EINVAL;
  593. }
  594. ret = _omap_device_enable_hwmods(od);
  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. od->_state = OMAP_DEVICE_STATE_IDLE;
  620. return ret;
  621. }
  622. /**
  623. * omap_device_assert_hardreset - set a device's hardreset line
  624. * @pdev: struct platform_device * to reset
  625. * @name: const char * name of the reset line
  626. *
  627. * Set the hardreset line identified by @name on the IP blocks
  628. * associated with the hwmods backing the platform_device @pdev. All
  629. * of the hwmods associated with @pdev must have the same hardreset
  630. * line linked to them for this to work. Passes along the return value
  631. * of omap_hwmod_assert_hardreset() in the event of any failure, or
  632. * returns 0 upon success.
  633. */
  634. int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
  635. {
  636. struct omap_device *od = to_omap_device(pdev);
  637. int ret = 0;
  638. int i;
  639. for (i = 0; i < od->hwmods_cnt; i++) {
  640. ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
  641. if (ret)
  642. break;
  643. }
  644. return ret;
  645. }
  646. /**
  647. * omap_device_deassert_hardreset - release a device's hardreset line
  648. * @pdev: struct platform_device * to reset
  649. * @name: const char * name of the reset line
  650. *
  651. * Release the hardreset line identified by @name on the IP blocks
  652. * associated with the hwmods backing the platform_device @pdev. All
  653. * of the hwmods associated with @pdev must have the same hardreset
  654. * line linked to them for this to work. Passes along the return
  655. * value of omap_hwmod_deassert_hardreset() in the event of any
  656. * failure, or returns 0 upon success.
  657. */
  658. int omap_device_deassert_hardreset(struct platform_device *pdev,
  659. const char *name)
  660. {
  661. struct omap_device *od = to_omap_device(pdev);
  662. int ret = 0;
  663. int i;
  664. for (i = 0; i < od->hwmods_cnt; i++) {
  665. ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
  666. if (ret)
  667. break;
  668. }
  669. return ret;
  670. }
  671. /**
  672. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  673. * device pointer.
  674. * @oh_name: name of the hwmod device
  675. *
  676. * Returns back a struct device * pointer associated with a hwmod
  677. * device represented by a hwmod_name
  678. */
  679. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  680. {
  681. struct omap_hwmod *oh;
  682. if (!oh_name) {
  683. WARN(1, "%s: no hwmod name!\n", __func__);
  684. return ERR_PTR(-EINVAL);
  685. }
  686. oh = omap_hwmod_lookup(oh_name);
  687. if (!oh) {
  688. WARN(1, "%s: no hwmod for %s\n", __func__,
  689. oh_name);
  690. return ERR_PTR(-ENODEV);
  691. }
  692. if (!oh->od) {
  693. WARN(1, "%s: no omap_device for %s\n", __func__,
  694. oh_name);
  695. return ERR_PTR(-ENODEV);
  696. }
  697. return &oh->od->pdev->dev;
  698. }
  699. static struct notifier_block platform_nb = {
  700. .notifier_call = _omap_device_notifier_call,
  701. };
  702. static int __init omap_device_init(void)
  703. {
  704. bus_register_notifier(&platform_bus_type, &platform_nb);
  705. return 0;
  706. }
  707. omap_core_initcall(omap_device_init);
  708. /**
  709. * omap_device_late_idle - idle devices without drivers
  710. * @dev: struct device * associated with omap_device
  711. * @data: unused
  712. *
  713. * Check the driver bound status of this device, and idle it
  714. * if there is no driver attached.
  715. */
  716. static int __init omap_device_late_idle(struct device *dev, void *data)
  717. {
  718. struct platform_device *pdev = to_platform_device(dev);
  719. struct omap_device *od = to_omap_device(pdev);
  720. if (!od)
  721. return 0;
  722. /*
  723. * If omap_device state is enabled, but has no driver bound,
  724. * idle it.
  725. */
  726. if (od->_driver_status != BUS_NOTIFY_BOUND_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);