omap_device.c 25 KB

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