omap_device.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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/notifier.h>
  38. #include "common.h"
  39. #include "soc.h"
  40. #include "omap_device.h"
  41. #include "omap_hwmod.h"
  42. /* Private functions */
  43. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  44. const char *clk_name)
  45. {
  46. struct clk *r;
  47. int rc;
  48. if (!clk_alias || !clk_name)
  49. return;
  50. dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
  51. r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
  52. if (!IS_ERR(r)) {
  53. dev_dbg(&od->pdev->dev,
  54. "alias %s already exists\n", clk_alias);
  55. clk_put(r);
  56. return;
  57. }
  58. r = clk_get_sys(NULL, clk_name);
  59. if (IS_ERR(r) && of_have_populated_dt()) {
  60. struct of_phandle_args clkspec;
  61. clkspec.np = of_find_node_by_name(NULL, clk_name);
  62. r = of_clk_get_from_provider(&clkspec);
  63. rc = clk_register_clkdev(r, clk_alias,
  64. dev_name(&od->pdev->dev));
  65. } else {
  66. rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev),
  67. clk_name, NULL);
  68. }
  69. if (rc) {
  70. if (rc == -ENODEV || rc == -ENOMEM)
  71. dev_err(&od->pdev->dev,
  72. "clkdev_alloc for %s failed\n", clk_alias);
  73. else
  74. dev_err(&od->pdev->dev,
  75. "clk_get for %s failed\n", clk_name);
  76. }
  77. }
  78. /**
  79. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  80. * and main clock
  81. * @od: struct omap_device *od
  82. * @oh: struct omap_hwmod *oh
  83. *
  84. * For the main clock and every optional clock present per hwmod per
  85. * omap_device, this function adds an entry in the clkdev table of the
  86. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  87. *
  88. * The function is called from inside omap_device_build_ss(), after
  89. * omap_device_register.
  90. *
  91. * This allows drivers to get a pointer to its optional clocks based on its role
  92. * by calling clk_get(<dev*>, <role>).
  93. * In the case of the main clock, a "fck" alias is used.
  94. *
  95. * No return value.
  96. */
  97. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  98. struct omap_hwmod *oh)
  99. {
  100. int i;
  101. _add_clkdev(od, "fck", oh->main_clk);
  102. for (i = 0; i < oh->opt_clks_cnt; i++)
  103. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  104. }
  105. /**
  106. * omap_device_build_from_dt - build an omap_device with multiple hwmods
  107. * @pdev_name: name of the platform_device driver to use
  108. * @pdev_id: this platform_device's connection ID
  109. * @oh: ptr to the single omap_hwmod that backs this omap_device
  110. * @pdata: platform_data ptr to associate with the platform_device
  111. * @pdata_len: amount of memory pointed to by @pdata
  112. *
  113. * Function for building an omap_device already registered from device-tree
  114. *
  115. * Returns 0 or PTR_ERR() on error.
  116. */
  117. static int omap_device_build_from_dt(struct platform_device *pdev)
  118. {
  119. struct omap_hwmod **hwmods;
  120. struct omap_device *od;
  121. struct omap_hwmod *oh;
  122. struct device_node *node = pdev->dev.of_node;
  123. const char *oh_name;
  124. int oh_cnt, i, ret = 0;
  125. bool device_active = false;
  126. oh_cnt = of_property_count_strings(node, "ti,hwmods");
  127. if (oh_cnt <= 0) {
  128. dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
  129. return -ENODEV;
  130. }
  131. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  132. if (!hwmods) {
  133. ret = -ENOMEM;
  134. goto odbfd_exit;
  135. }
  136. for (i = 0; i < oh_cnt; i++) {
  137. of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
  138. oh = omap_hwmod_lookup(oh_name);
  139. if (!oh) {
  140. dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
  141. oh_name);
  142. ret = -EINVAL;
  143. goto odbfd_exit1;
  144. }
  145. hwmods[i] = oh;
  146. if (oh->flags & HWMOD_INIT_NO_IDLE)
  147. device_active = true;
  148. }
  149. od = omap_device_alloc(pdev, hwmods, oh_cnt);
  150. if (IS_ERR(od)) {
  151. dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
  152. oh_name);
  153. ret = PTR_ERR(od);
  154. goto odbfd_exit1;
  155. }
  156. /* Fix up missing resource names */
  157. for (i = 0; i < pdev->num_resources; i++) {
  158. struct resource *r = &pdev->resource[i];
  159. if (r->name == NULL)
  160. r->name = dev_name(&pdev->dev);
  161. }
  162. dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
  163. if (device_active) {
  164. omap_device_enable(pdev);
  165. pm_runtime_set_active(&pdev->dev);
  166. }
  167. odbfd_exit1:
  168. kfree(hwmods);
  169. odbfd_exit:
  170. /* if data/we are at fault.. load up a fail handler */
  171. if (ret)
  172. dev_pm_domain_set(&pdev->dev, &omap_device_fail_pm_domain);
  173. return ret;
  174. }
  175. static int _omap_device_notifier_call(struct notifier_block *nb,
  176. unsigned long event, void *dev)
  177. {
  178. struct platform_device *pdev = to_platform_device(dev);
  179. struct omap_device *od;
  180. int err;
  181. switch (event) {
  182. case BUS_NOTIFY_REMOVED_DEVICE:
  183. if (pdev->archdata.od)
  184. omap_device_delete(pdev->archdata.od);
  185. break;
  186. case BUS_NOTIFY_UNBOUND_DRIVER:
  187. od = to_omap_device(pdev);
  188. if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED)) {
  189. dev_info(dev, "enabled after unload, idling\n");
  190. err = omap_device_idle(pdev);
  191. if (err)
  192. dev_err(dev, "failed to idle\n");
  193. }
  194. break;
  195. case BUS_NOTIFY_BIND_DRIVER:
  196. od = to_omap_device(pdev);
  197. if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED) &&
  198. pm_runtime_status_suspended(dev)) {
  199. od->_driver_status = BUS_NOTIFY_BIND_DRIVER;
  200. pm_runtime_set_active(dev);
  201. }
  202. break;
  203. case BUS_NOTIFY_ADD_DEVICE:
  204. if (pdev->dev.of_node)
  205. omap_device_build_from_dt(pdev);
  206. omap_auxdata_legacy_init(dev);
  207. /* fall through */
  208. default:
  209. od = to_omap_device(pdev);
  210. if (od)
  211. od->_driver_status = event;
  212. }
  213. return NOTIFY_DONE;
  214. }
  215. /**
  216. * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  217. * @od: struct omap_device *od
  218. *
  219. * Enable all underlying hwmods. Returns 0.
  220. */
  221. static int _omap_device_enable_hwmods(struct omap_device *od)
  222. {
  223. int ret = 0;
  224. int i;
  225. for (i = 0; i < od->hwmods_cnt; i++)
  226. ret |= omap_hwmod_enable(od->hwmods[i]);
  227. return ret;
  228. }
  229. /**
  230. * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  231. * @od: struct omap_device *od
  232. *
  233. * Idle all underlying hwmods. Returns 0.
  234. */
  235. static int _omap_device_idle_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_idle(od->hwmods[i]);
  241. return ret;
  242. }
  243. /* Public functions for use by core code */
  244. /**
  245. * omap_device_get_context_loss_count - get lost context count
  246. * @od: struct omap_device *
  247. *
  248. * Using the primary hwmod, query the context loss count for this
  249. * device.
  250. *
  251. * Callers should consider context for this device lost any time this
  252. * function returns a value different than the value the caller got
  253. * the last time it called this function.
  254. *
  255. * If any hwmods exist for the omap_device associated with @pdev,
  256. * return the context loss counter for that hwmod, otherwise return
  257. * zero.
  258. */
  259. int omap_device_get_context_loss_count(struct platform_device *pdev)
  260. {
  261. struct omap_device *od;
  262. u32 ret = 0;
  263. od = to_omap_device(pdev);
  264. if (od->hwmods_cnt)
  265. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  266. return ret;
  267. }
  268. /**
  269. * omap_device_count_resources - count number of struct resource entries needed
  270. * @od: struct omap_device *
  271. * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
  272. *
  273. * Count the number of struct resource entries needed for this
  274. * omap_device @od. Used by omap_device_build_ss() to determine how
  275. * much memory to allocate before calling
  276. * omap_device_fill_resources(). Returns the count.
  277. */
  278. static int omap_device_count_resources(struct omap_device *od,
  279. unsigned long flags)
  280. {
  281. int c = 0;
  282. int i;
  283. for (i = 0; i < od->hwmods_cnt; i++)
  284. c += omap_hwmod_count_resources(od->hwmods[i], flags);
  285. pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
  286. od->pdev->name, c, od->hwmods_cnt);
  287. return c;
  288. }
  289. /**
  290. * omap_device_fill_resources - fill in array of struct resource
  291. * @od: struct omap_device *
  292. * @res: pointer to an array of struct resource to be filled in
  293. *
  294. * Populate one or more empty struct resource pointed to by @res with
  295. * the resource data for this omap_device @od. Used by
  296. * omap_device_build_ss() after calling omap_device_count_resources().
  297. * Ideally this function would not be needed at all. If omap_device
  298. * replaces platform_device, then we can specify our own
  299. * get_resource()/ get_irq()/etc functions that use the underlying
  300. * omap_hwmod information. Or if platform_device is extended to use
  301. * subarchitecture-specific function pointers, the various
  302. * platform_device functions can simply call omap_device internal
  303. * functions to get device resources. Hacking around the existing
  304. * platform_device code wastes memory. Returns 0.
  305. */
  306. static int omap_device_fill_resources(struct omap_device *od,
  307. struct resource *res)
  308. {
  309. int i, r;
  310. for (i = 0; i < od->hwmods_cnt; i++) {
  311. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  312. res += r;
  313. }
  314. return 0;
  315. }
  316. /**
  317. * _od_fill_dma_resources - fill in array of struct resource with dma resources
  318. * @od: struct omap_device *
  319. * @res: pointer to an array of struct resource to be filled in
  320. *
  321. * Populate one or more empty struct resource pointed to by @res with
  322. * the dma resource data for this omap_device @od. Used by
  323. * omap_device_alloc() after calling omap_device_count_resources().
  324. *
  325. * Ideally this function would not be needed at all. If we have
  326. * mechanism to get dma resources from DT.
  327. *
  328. * Returns 0.
  329. */
  330. static int _od_fill_dma_resources(struct omap_device *od,
  331. struct resource *res)
  332. {
  333. int i, r;
  334. for (i = 0; i < od->hwmods_cnt; i++) {
  335. r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
  336. res += r;
  337. }
  338. return 0;
  339. }
  340. /**
  341. * omap_device_alloc - allocate an omap_device
  342. * @pdev: platform_device that will be included in this omap_device
  343. * @oh: ptr to the single omap_hwmod that backs this omap_device
  344. * @pdata: platform_data ptr to associate with the platform_device
  345. * @pdata_len: amount of memory pointed to by @pdata
  346. *
  347. * Convenience function for allocating an omap_device structure and filling
  348. * hwmods, and resources.
  349. *
  350. * Returns an struct omap_device pointer or ERR_PTR() on error;
  351. */
  352. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  353. struct omap_hwmod **ohs, int oh_cnt)
  354. {
  355. int ret = -ENOMEM;
  356. struct omap_device *od;
  357. struct resource *res = NULL;
  358. int i, res_count;
  359. struct omap_hwmod **hwmods;
  360. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  361. if (!od) {
  362. ret = -ENOMEM;
  363. goto oda_exit1;
  364. }
  365. od->hwmods_cnt = oh_cnt;
  366. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  367. if (!hwmods)
  368. goto oda_exit2;
  369. od->hwmods = hwmods;
  370. od->pdev = pdev;
  371. /*
  372. * Non-DT Boot:
  373. * Here, pdev->num_resources = 0, and we should get all the
  374. * resources from hwmod.
  375. *
  376. * DT Boot:
  377. * OF framework will construct the resource structure (currently
  378. * does for MEM & IRQ resource) and we should respect/use these
  379. * resources, killing hwmod dependency.
  380. * If pdev->num_resources > 0, we assume that MEM & IRQ resources
  381. * have been allocated by OF layer already (through DTB).
  382. * As preparation for the future we examine the OF provided resources
  383. * to see if we have DMA resources provided already. In this case
  384. * there is no need to update the resources for the device, we use the
  385. * OF provided ones.
  386. *
  387. * TODO: Once DMA resource is available from OF layer, we should
  388. * kill filling any resources from hwmod.
  389. */
  390. if (!pdev->num_resources) {
  391. /* Count all resources for the device */
  392. res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
  393. IORESOURCE_DMA |
  394. IORESOURCE_MEM);
  395. } else {
  396. /* Take a look if we already have DMA resource via DT */
  397. for (i = 0; i < pdev->num_resources; i++) {
  398. struct resource *r = &pdev->resource[i];
  399. /* We have it, no need to touch the resources */
  400. if (r->flags == IORESOURCE_DMA)
  401. goto have_everything;
  402. }
  403. /* Count only DMA resources for the device */
  404. res_count = omap_device_count_resources(od, IORESOURCE_DMA);
  405. /* The device has no DMA resource, no need for update */
  406. if (!res_count)
  407. goto have_everything;
  408. res_count += pdev->num_resources;
  409. }
  410. /* Allocate resources memory to account for new resources */
  411. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  412. if (!res)
  413. goto oda_exit3;
  414. if (!pdev->num_resources) {
  415. dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
  416. __func__, res_count);
  417. omap_device_fill_resources(od, res);
  418. } else {
  419. dev_dbg(&pdev->dev,
  420. "%s: appending %d DMA resources from hwmod\n",
  421. __func__, res_count - pdev->num_resources);
  422. memcpy(res, pdev->resource,
  423. sizeof(struct resource) * pdev->num_resources);
  424. _od_fill_dma_resources(od, &res[pdev->num_resources]);
  425. }
  426. ret = platform_device_add_resources(pdev, res, res_count);
  427. kfree(res);
  428. if (ret)
  429. goto oda_exit3;
  430. have_everything:
  431. pdev->archdata.od = od;
  432. for (i = 0; i < oh_cnt; i++) {
  433. hwmods[i]->od = od;
  434. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  435. }
  436. return od;
  437. oda_exit3:
  438. kfree(hwmods);
  439. oda_exit2:
  440. kfree(od);
  441. oda_exit1:
  442. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  443. return ERR_PTR(ret);
  444. }
  445. void omap_device_delete(struct omap_device *od)
  446. {
  447. if (!od)
  448. return;
  449. od->pdev->archdata.od = NULL;
  450. kfree(od->hwmods);
  451. kfree(od);
  452. }
  453. /**
  454. * omap_device_build - build and register an omap_device with one omap_hwmod
  455. * @pdev_name: name of the platform_device driver to use
  456. * @pdev_id: this platform_device's connection ID
  457. * @oh: ptr to the single omap_hwmod that backs this omap_device
  458. * @pdata: platform_data ptr to associate with the platform_device
  459. * @pdata_len: amount of memory pointed to by @pdata
  460. *
  461. * Convenience function for building and registering a single
  462. * omap_device record, which in turn builds and registers a
  463. * platform_device record. See omap_device_build_ss() for more
  464. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  465. * passes along the return value of omap_device_build_ss().
  466. */
  467. struct platform_device __init *omap_device_build(const char *pdev_name,
  468. int pdev_id,
  469. struct omap_hwmod *oh,
  470. void *pdata, int pdata_len)
  471. {
  472. struct omap_hwmod *ohs[] = { oh };
  473. if (!oh)
  474. return ERR_PTR(-EINVAL);
  475. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  476. pdata_len);
  477. }
  478. /**
  479. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  480. * @pdev_name: name of the platform_device driver to use
  481. * @pdev_id: this platform_device's connection ID
  482. * @oh: ptr to the single omap_hwmod that backs this omap_device
  483. * @pdata: platform_data ptr to associate with the platform_device
  484. * @pdata_len: amount of memory pointed to by @pdata
  485. *
  486. * Convenience function for building and registering an omap_device
  487. * subsystem record. Subsystem records consist of multiple
  488. * omap_hwmods. This function in turn builds and registers a
  489. * platform_device record. Returns an ERR_PTR() on error, or passes
  490. * along the return value of omap_device_register().
  491. */
  492. struct platform_device __init *omap_device_build_ss(const char *pdev_name,
  493. int pdev_id,
  494. struct omap_hwmod **ohs,
  495. int oh_cnt, void *pdata,
  496. int pdata_len)
  497. {
  498. int ret = -ENOMEM;
  499. struct platform_device *pdev;
  500. struct omap_device *od;
  501. if (!ohs || oh_cnt == 0 || !pdev_name)
  502. return ERR_PTR(-EINVAL);
  503. if (!pdata && pdata_len > 0)
  504. return ERR_PTR(-EINVAL);
  505. pdev = platform_device_alloc(pdev_name, pdev_id);
  506. if (!pdev) {
  507. ret = -ENOMEM;
  508. goto odbs_exit;
  509. }
  510. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  511. if (pdev->id != -1)
  512. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  513. else
  514. dev_set_name(&pdev->dev, "%s", pdev->name);
  515. od = omap_device_alloc(pdev, ohs, oh_cnt);
  516. if (IS_ERR(od))
  517. goto odbs_exit1;
  518. ret = platform_device_add_data(pdev, pdata, pdata_len);
  519. if (ret)
  520. goto odbs_exit2;
  521. ret = omap_device_register(pdev);
  522. if (ret)
  523. goto odbs_exit2;
  524. return pdev;
  525. odbs_exit2:
  526. omap_device_delete(od);
  527. odbs_exit1:
  528. platform_device_put(pdev);
  529. odbs_exit:
  530. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  531. return ERR_PTR(ret);
  532. }
  533. #ifdef CONFIG_PM
  534. static int _od_runtime_suspend(struct device *dev)
  535. {
  536. struct platform_device *pdev = to_platform_device(dev);
  537. int ret;
  538. ret = pm_generic_runtime_suspend(dev);
  539. if (ret)
  540. return ret;
  541. return omap_device_idle(pdev);
  542. }
  543. static int _od_runtime_resume(struct device *dev)
  544. {
  545. struct platform_device *pdev = to_platform_device(dev);
  546. int ret;
  547. ret = omap_device_enable(pdev);
  548. if (ret) {
  549. dev_err(dev, "use pm_runtime_put_sync_suspend() in driver?\n");
  550. return ret;
  551. }
  552. return pm_generic_runtime_resume(dev);
  553. }
  554. static int _od_fail_runtime_suspend(struct device *dev)
  555. {
  556. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  557. return -ENODEV;
  558. }
  559. static int _od_fail_runtime_resume(struct device *dev)
  560. {
  561. dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
  562. return -ENODEV;
  563. }
  564. #endif
  565. #ifdef CONFIG_SUSPEND
  566. static int _od_suspend_noirq(struct device *dev)
  567. {
  568. struct platform_device *pdev = to_platform_device(dev);
  569. struct omap_device *od = to_omap_device(pdev);
  570. int ret;
  571. /* Don't attempt late suspend on a driver that is not bound */
  572. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
  573. return 0;
  574. ret = pm_generic_suspend_noirq(dev);
  575. if (!ret && !pm_runtime_status_suspended(dev)) {
  576. if (pm_generic_runtime_suspend(dev) == 0) {
  577. pm_runtime_set_suspended(dev);
  578. omap_device_idle(pdev);
  579. od->flags |= OMAP_DEVICE_SUSPENDED;
  580. }
  581. }
  582. return ret;
  583. }
  584. static int _od_resume_noirq(struct device *dev)
  585. {
  586. struct platform_device *pdev = to_platform_device(dev);
  587. struct omap_device *od = to_omap_device(pdev);
  588. if (od->flags & OMAP_DEVICE_SUSPENDED) {
  589. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  590. omap_device_enable(pdev);
  591. /*
  592. * XXX: we run before core runtime pm has resumed itself. At
  593. * this point in time, we just restore the runtime pm state and
  594. * considering symmetric operations in resume, we donot expect
  595. * to fail. If we failed, something changed in core runtime_pm
  596. * framework OR some device driver messed things up, hence, WARN
  597. */
  598. WARN(pm_runtime_set_active(dev),
  599. "Could not set %s runtime state active\n", dev_name(dev));
  600. pm_generic_runtime_resume(dev);
  601. }
  602. return pm_generic_resume_noirq(dev);
  603. }
  604. #else
  605. #define _od_suspend_noirq NULL
  606. #define _od_resume_noirq NULL
  607. #endif
  608. struct dev_pm_domain omap_device_fail_pm_domain = {
  609. .ops = {
  610. SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
  611. _od_fail_runtime_resume, NULL)
  612. }
  613. };
  614. struct dev_pm_domain omap_device_pm_domain = {
  615. .ops = {
  616. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  617. NULL)
  618. USE_PLATFORM_PM_SLEEP_OPS
  619. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
  620. _od_resume_noirq)
  621. }
  622. };
  623. /**
  624. * omap_device_register - register an omap_device with one omap_hwmod
  625. * @od: struct omap_device * to register
  626. *
  627. * Register the omap_device structure. This currently just calls
  628. * platform_device_register() on the underlying platform_device.
  629. * Returns the return value of platform_device_register().
  630. */
  631. int omap_device_register(struct platform_device *pdev)
  632. {
  633. pr_debug("omap_device: %s: registering\n", pdev->name);
  634. dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
  635. return platform_device_add(pdev);
  636. }
  637. /* Public functions for use by device drivers through struct platform_data */
  638. /**
  639. * omap_device_enable - fully activate an omap_device
  640. * @od: struct omap_device * to activate
  641. *
  642. * Do whatever is necessary for the hwmods underlying omap_device @od
  643. * to be accessible and ready to operate. This generally involves
  644. * enabling clocks, setting SYSCONFIG registers; and in the future may
  645. * involve remuxing pins. Device drivers should call this function
  646. * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
  647. * the omap_device is already enabled, or passes along the return
  648. * value of _omap_device_enable_hwmods().
  649. */
  650. int omap_device_enable(struct platform_device *pdev)
  651. {
  652. int ret;
  653. struct omap_device *od;
  654. od = to_omap_device(pdev);
  655. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  656. dev_warn(&pdev->dev,
  657. "omap_device: %s() called from invalid state %d\n",
  658. __func__, od->_state);
  659. return -EINVAL;
  660. }
  661. ret = _omap_device_enable_hwmods(od);
  662. if (ret == 0)
  663. od->_state = OMAP_DEVICE_STATE_ENABLED;
  664. return ret;
  665. }
  666. /**
  667. * omap_device_idle - idle an omap_device
  668. * @od: struct omap_device * to idle
  669. *
  670. * Idle omap_device @od. Device drivers call this function indirectly
  671. * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not
  672. * currently enabled, or passes along the return value of
  673. * _omap_device_idle_hwmods().
  674. */
  675. int omap_device_idle(struct platform_device *pdev)
  676. {
  677. int ret;
  678. struct omap_device *od;
  679. od = to_omap_device(pdev);
  680. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  681. dev_warn(&pdev->dev,
  682. "omap_device: %s() called from invalid state %d\n",
  683. __func__, od->_state);
  684. return -EINVAL;
  685. }
  686. ret = _omap_device_idle_hwmods(od);
  687. if (ret == 0)
  688. od->_state = OMAP_DEVICE_STATE_IDLE;
  689. return ret;
  690. }
  691. /**
  692. * omap_device_assert_hardreset - set a device's hardreset line
  693. * @pdev: struct platform_device * to reset
  694. * @name: const char * name of the reset line
  695. *
  696. * Set the hardreset line identified by @name on the IP blocks
  697. * associated with the hwmods backing the platform_device @pdev. All
  698. * of the hwmods associated with @pdev must have the same hardreset
  699. * line linked to them for this to work. Passes along the return value
  700. * of omap_hwmod_assert_hardreset() in the event of any failure, or
  701. * returns 0 upon success.
  702. */
  703. int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
  704. {
  705. struct omap_device *od = to_omap_device(pdev);
  706. int ret = 0;
  707. int i;
  708. for (i = 0; i < od->hwmods_cnt; i++) {
  709. ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
  710. if (ret)
  711. break;
  712. }
  713. return ret;
  714. }
  715. /**
  716. * omap_device_deassert_hardreset - release a device's hardreset line
  717. * @pdev: struct platform_device * to reset
  718. * @name: const char * name of the reset line
  719. *
  720. * Release the hardreset line identified by @name on the IP blocks
  721. * associated with the hwmods backing the platform_device @pdev. All
  722. * of the hwmods associated with @pdev must have the same hardreset
  723. * line linked to them for this to work. Passes along the return
  724. * value of omap_hwmod_deassert_hardreset() in the event of any
  725. * failure, or returns 0 upon success.
  726. */
  727. int omap_device_deassert_hardreset(struct platform_device *pdev,
  728. const char *name)
  729. {
  730. struct omap_device *od = to_omap_device(pdev);
  731. int ret = 0;
  732. int i;
  733. for (i = 0; i < od->hwmods_cnt; i++) {
  734. ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
  735. if (ret)
  736. break;
  737. }
  738. return ret;
  739. }
  740. /**
  741. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  742. * device pointer.
  743. * @oh_name: name of the hwmod device
  744. *
  745. * Returns back a struct device * pointer associated with a hwmod
  746. * device represented by a hwmod_name
  747. */
  748. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  749. {
  750. struct omap_hwmod *oh;
  751. if (!oh_name) {
  752. WARN(1, "%s: no hwmod name!\n", __func__);
  753. return ERR_PTR(-EINVAL);
  754. }
  755. oh = omap_hwmod_lookup(oh_name);
  756. if (!oh) {
  757. WARN(1, "%s: no hwmod for %s\n", __func__,
  758. oh_name);
  759. return ERR_PTR(-ENODEV);
  760. }
  761. if (!oh->od) {
  762. WARN(1, "%s: no omap_device for %s\n", __func__,
  763. oh_name);
  764. return ERR_PTR(-ENODEV);
  765. }
  766. return &oh->od->pdev->dev;
  767. }
  768. static struct notifier_block platform_nb = {
  769. .notifier_call = _omap_device_notifier_call,
  770. };
  771. static int __init omap_device_init(void)
  772. {
  773. bus_register_notifier(&platform_bus_type, &platform_nb);
  774. return 0;
  775. }
  776. omap_postcore_initcall(omap_device_init);
  777. /**
  778. * omap_device_late_idle - idle devices without drivers
  779. * @dev: struct device * associated with omap_device
  780. * @data: unused
  781. *
  782. * Check the driver bound status of this device, and idle it
  783. * if there is no driver attached.
  784. */
  785. static int __init omap_device_late_idle(struct device *dev, void *data)
  786. {
  787. struct platform_device *pdev = to_platform_device(dev);
  788. struct omap_device *od = to_omap_device(pdev);
  789. int i;
  790. if (!od)
  791. return 0;
  792. /*
  793. * If omap_device state is enabled, but has no driver bound,
  794. * idle it.
  795. */
  796. /*
  797. * Some devices (like memory controllers) are always kept
  798. * enabled, and should not be idled even with no drivers.
  799. */
  800. for (i = 0; i < od->hwmods_cnt; i++)
  801. if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
  802. return 0;
  803. if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
  804. od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
  805. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  806. dev_warn(dev, "%s: enabled but no driver. Idling\n",
  807. __func__);
  808. omap_device_idle(pdev);
  809. }
  810. }
  811. return 0;
  812. }
  813. static int __init omap_device_late_init(void)
  814. {
  815. bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
  816. WARN(!of_have_populated_dt(),
  817. "legacy booting deprecated, please update to boot with .dts\n");
  818. return 0;
  819. }
  820. omap_late_initcall_sync(omap_device_late_init);