omap_device.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 either be
  21. * a) implemented via arch-specific pointers in platform_data
  22. * or
  23. * b) implemented as a proper omap_bus/omap_device in Linux, no more
  24. * platform_data func pointers
  25. *
  26. *
  27. * Guidelines for usage by driver authors:
  28. *
  29. * 1. These functions are intended to be used by device drivers via
  30. * function pointers in struct platform_data. As an example,
  31. * omap_device_enable() should be passed to the driver as
  32. *
  33. * struct foo_driver_platform_data {
  34. * ...
  35. * int (*device_enable)(struct platform_device *pdev);
  36. * ...
  37. * }
  38. *
  39. * Note that the generic "device_enable" name is used, rather than
  40. * "omap_device_enable". This is so other architectures can pass in their
  41. * own enable/disable functions here.
  42. *
  43. * This should be populated during device setup:
  44. *
  45. * ...
  46. * pdata->device_enable = omap_device_enable;
  47. * ...
  48. *
  49. * 2. Drivers should first check to ensure the function pointer is not null
  50. * before calling it, as in:
  51. *
  52. * if (pdata->device_enable)
  53. * pdata->device_enable(pdev);
  54. *
  55. * This allows other architectures that don't use similar device_enable()/
  56. * device_shutdown() functions to execute normally.
  57. *
  58. * ...
  59. *
  60. * Suggested usage by device drivers:
  61. *
  62. * During device initialization:
  63. * device_enable()
  64. *
  65. * During device idle:
  66. * (save remaining device context if necessary)
  67. * device_idle();
  68. *
  69. * During device resume:
  70. * device_enable();
  71. * (restore context if necessary)
  72. *
  73. * During device shutdown:
  74. * device_shutdown()
  75. * (device must be reinitialized at this point to use it again)
  76. *
  77. */
  78. #undef DEBUG
  79. #include <linux/kernel.h>
  80. #include <linux/platform_device.h>
  81. #include <linux/slab.h>
  82. #include <linux/err.h>
  83. #include <linux/io.h>
  84. #include <linux/clk.h>
  85. #include <linux/clkdev.h>
  86. #include <linux/pm_runtime.h>
  87. #include <plat/omap_device.h>
  88. #include <plat/omap_hwmod.h>
  89. #include <plat/clock.h>
  90. /* These parameters are passed to _omap_device_{de,}activate() */
  91. #define USE_WAKEUP_LAT 0
  92. #define IGNORE_WAKEUP_LAT 1
  93. /* Private functions */
  94. /**
  95. * _omap_device_activate - increase device readiness
  96. * @od: struct omap_device *
  97. * @ignore_lat: increase to latency target (0) or full readiness (1)?
  98. *
  99. * Increase readiness of omap_device @od (thus decreasing device
  100. * wakeup latency, but consuming more power). If @ignore_lat is
  101. * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
  102. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  103. * latency is greater than the requested maximum wakeup latency, step
  104. * backwards in the omap_device_pm_latency table to ensure the
  105. * device's maximum wakeup latency is less than or equal to the
  106. * requested maximum wakeup latency. Returns 0.
  107. */
  108. static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
  109. {
  110. struct timespec a, b, c;
  111. dev_dbg(&od->pdev.dev, "omap_device: activating\n");
  112. while (od->pm_lat_level > 0) {
  113. struct omap_device_pm_latency *odpl;
  114. unsigned long long act_lat = 0;
  115. od->pm_lat_level--;
  116. odpl = od->pm_lats + od->pm_lat_level;
  117. if (!ignore_lat &&
  118. (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
  119. break;
  120. read_persistent_clock(&a);
  121. /* XXX check return code */
  122. odpl->activate_func(od);
  123. read_persistent_clock(&b);
  124. c = timespec_sub(b, a);
  125. act_lat = timespec_to_ns(&c);
  126. dev_dbg(&od->pdev.dev,
  127. "omap_device: pm_lat %d: activate: elapsed time "
  128. "%llu nsec\n", od->pm_lat_level, act_lat);
  129. if (act_lat > odpl->activate_lat) {
  130. odpl->activate_lat_worst = act_lat;
  131. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  132. odpl->activate_lat = act_lat;
  133. dev_warn(&od->pdev.dev,
  134. "new worst case activate latency "
  135. "%d: %llu\n",
  136. od->pm_lat_level, act_lat);
  137. } else
  138. dev_warn(&od->pdev.dev,
  139. "activate latency %d "
  140. "higher than exptected. (%llu > %d)\n",
  141. od->pm_lat_level, act_lat,
  142. odpl->activate_lat);
  143. }
  144. od->dev_wakeup_lat -= odpl->activate_lat;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * _omap_device_deactivate - decrease device readiness
  150. * @od: struct omap_device *
  151. * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
  152. *
  153. * Decrease readiness of omap_device @od (thus increasing device
  154. * wakeup latency, but conserving power). If @ignore_lat is
  155. * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
  156. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  157. * latency is less than the requested maximum wakeup latency, step
  158. * forwards in the omap_device_pm_latency table to ensure the device's
  159. * maximum wakeup latency is less than or equal to the requested
  160. * maximum wakeup latency. Returns 0.
  161. */
  162. static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
  163. {
  164. struct timespec a, b, c;
  165. dev_dbg(&od->pdev.dev, "omap_device: deactivating\n");
  166. while (od->pm_lat_level < od->pm_lats_cnt) {
  167. struct omap_device_pm_latency *odpl;
  168. unsigned long long deact_lat = 0;
  169. odpl = od->pm_lats + od->pm_lat_level;
  170. if (!ignore_lat &&
  171. ((od->dev_wakeup_lat + odpl->activate_lat) >
  172. od->_dev_wakeup_lat_limit))
  173. break;
  174. read_persistent_clock(&a);
  175. /* XXX check return code */
  176. odpl->deactivate_func(od);
  177. read_persistent_clock(&b);
  178. c = timespec_sub(b, a);
  179. deact_lat = timespec_to_ns(&c);
  180. dev_dbg(&od->pdev.dev,
  181. "omap_device: pm_lat %d: deactivate: elapsed time "
  182. "%llu nsec\n", od->pm_lat_level, deact_lat);
  183. if (deact_lat > odpl->deactivate_lat) {
  184. odpl->deactivate_lat_worst = deact_lat;
  185. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  186. odpl->deactivate_lat = deact_lat;
  187. dev_warn(&od->pdev.dev,
  188. "new worst case deactivate latency "
  189. "%d: %llu\n",
  190. od->pm_lat_level, deact_lat);
  191. } else
  192. dev_warn(&od->pdev.dev,
  193. "deactivate latency %d "
  194. "higher than exptected. (%llu > %d)\n",
  195. od->pm_lat_level, deact_lat,
  196. odpl->deactivate_lat);
  197. }
  198. od->dev_wakeup_lat += odpl->activate_lat;
  199. od->pm_lat_level++;
  200. }
  201. return 0;
  202. }
  203. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  204. const char *clk_name)
  205. {
  206. struct clk *r;
  207. struct clk_lookup *l;
  208. if (!clk_alias || !clk_name)
  209. return;
  210. dev_dbg(&od->pdev.dev, "Creating %s -> %s\n", clk_alias, clk_name);
  211. r = clk_get_sys(dev_name(&od->pdev.dev), clk_alias);
  212. if (!IS_ERR(r)) {
  213. dev_warn(&od->pdev.dev,
  214. "alias %s already exists\n", clk_alias);
  215. clk_put(r);
  216. return;
  217. }
  218. r = omap_clk_get_by_name(clk_name);
  219. if (IS_ERR(r)) {
  220. dev_err(&od->pdev.dev,
  221. "omap_clk_get_by_name for %s failed\n", clk_name);
  222. return;
  223. }
  224. l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev.dev));
  225. if (!l) {
  226. dev_err(&od->pdev.dev,
  227. "clkdev_alloc for %s failed\n", clk_alias);
  228. return;
  229. }
  230. clkdev_add(l);
  231. }
  232. /**
  233. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  234. * and main clock
  235. * @od: struct omap_device *od
  236. * @oh: struct omap_hwmod *oh
  237. *
  238. * For the main clock and every optional clock present per hwmod per
  239. * omap_device, this function adds an entry in the clkdev table of the
  240. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  241. *
  242. * The function is called from inside omap_device_build_ss(), after
  243. * omap_device_register.
  244. *
  245. * This allows drivers to get a pointer to its optional clocks based on its role
  246. * by calling clk_get(<dev*>, <role>).
  247. * In the case of the main clock, a "fck" alias is used.
  248. *
  249. * No return value.
  250. */
  251. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  252. struct omap_hwmod *oh)
  253. {
  254. int i;
  255. _add_clkdev(od, "fck", oh->main_clk);
  256. for (i = 0; i < oh->opt_clks_cnt; i++)
  257. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  258. }
  259. /* Public functions for use by core code */
  260. /**
  261. * omap_device_get_context_loss_count - get lost context count
  262. * @od: struct omap_device *
  263. *
  264. * Using the primary hwmod, query the context loss count for this
  265. * device.
  266. *
  267. * Callers should consider context for this device lost any time this
  268. * function returns a value different than the value the caller got
  269. * the last time it called this function.
  270. *
  271. * If any hwmods exist for the omap_device assoiated with @pdev,
  272. * return the context loss counter for that hwmod, otherwise return
  273. * zero.
  274. */
  275. u32 omap_device_get_context_loss_count(struct platform_device *pdev)
  276. {
  277. struct omap_device *od;
  278. u32 ret = 0;
  279. od = to_omap_device(pdev);
  280. if (od->hwmods_cnt)
  281. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  282. return ret;
  283. }
  284. /**
  285. * omap_device_count_resources - count number of struct resource entries needed
  286. * @od: struct omap_device *
  287. *
  288. * Count the number of struct resource entries needed for this
  289. * omap_device @od. Used by omap_device_build_ss() to determine how
  290. * much memory to allocate before calling
  291. * omap_device_fill_resources(). Returns the count.
  292. */
  293. int omap_device_count_resources(struct omap_device *od)
  294. {
  295. int c = 0;
  296. int i;
  297. for (i = 0; i < od->hwmods_cnt; i++)
  298. c += omap_hwmod_count_resources(od->hwmods[i]);
  299. pr_debug("omap_device: %s: counted %d total resources across %d "
  300. "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
  301. return c;
  302. }
  303. /**
  304. * omap_device_fill_resources - fill in array of struct resource
  305. * @od: struct omap_device *
  306. * @res: pointer to an array of struct resource to be filled in
  307. *
  308. * Populate one or more empty struct resource pointed to by @res with
  309. * the resource data for this omap_device @od. Used by
  310. * omap_device_build_ss() after calling omap_device_count_resources().
  311. * Ideally this function would not be needed at all. If omap_device
  312. * replaces platform_device, then we can specify our own
  313. * get_resource()/ get_irq()/etc functions that use the underlying
  314. * omap_hwmod information. Or if platform_device is extended to use
  315. * subarchitecture-specific function pointers, the various
  316. * platform_device functions can simply call omap_device internal
  317. * functions to get device resources. Hacking around the existing
  318. * platform_device code wastes memory. Returns 0.
  319. */
  320. int omap_device_fill_resources(struct omap_device *od, struct resource *res)
  321. {
  322. int c = 0;
  323. int i, r;
  324. for (i = 0; i < od->hwmods_cnt; i++) {
  325. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  326. res += r;
  327. c += r;
  328. }
  329. return 0;
  330. }
  331. /**
  332. * omap_device_build - build and register an omap_device with one omap_hwmod
  333. * @pdev_name: name of the platform_device driver to use
  334. * @pdev_id: this platform_device's connection ID
  335. * @oh: ptr to the single omap_hwmod that backs this omap_device
  336. * @pdata: platform_data ptr to associate with the platform_device
  337. * @pdata_len: amount of memory pointed to by @pdata
  338. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  339. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  340. * @is_early_device: should the device be registered as an early device or not
  341. *
  342. * Convenience function for building and registering a single
  343. * omap_device record, which in turn builds and registers a
  344. * platform_device record. See omap_device_build_ss() for more
  345. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  346. * passes along the return value of omap_device_build_ss().
  347. */
  348. struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
  349. struct omap_hwmod *oh, void *pdata,
  350. int pdata_len,
  351. struct omap_device_pm_latency *pm_lats,
  352. int pm_lats_cnt, int is_early_device)
  353. {
  354. struct omap_hwmod *ohs[] = { oh };
  355. if (!oh)
  356. return ERR_PTR(-EINVAL);
  357. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  358. pdata_len, pm_lats, pm_lats_cnt,
  359. is_early_device);
  360. }
  361. /**
  362. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  363. * @pdev_name: name of the platform_device driver to use
  364. * @pdev_id: this platform_device's connection ID
  365. * @oh: ptr to the single omap_hwmod that backs this omap_device
  366. * @pdata: platform_data ptr to associate with the platform_device
  367. * @pdata_len: amount of memory pointed to by @pdata
  368. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  369. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  370. * @is_early_device: should the device be registered as an early device or not
  371. *
  372. * Convenience function for building and registering an omap_device
  373. * subsystem record. Subsystem records consist of multiple
  374. * omap_hwmods. This function in turn builds and registers a
  375. * platform_device record. Returns an ERR_PTR() on error, or passes
  376. * along the return value of omap_device_register().
  377. */
  378. struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
  379. struct omap_hwmod **ohs, int oh_cnt,
  380. void *pdata, int pdata_len,
  381. struct omap_device_pm_latency *pm_lats,
  382. int pm_lats_cnt, int is_early_device)
  383. {
  384. int ret = -ENOMEM;
  385. struct omap_device *od;
  386. char *pdev_name2;
  387. struct resource *res = NULL;
  388. int i, res_count;
  389. struct omap_hwmod **hwmods;
  390. if (!ohs || oh_cnt == 0 || !pdev_name)
  391. return ERR_PTR(-EINVAL);
  392. if (!pdata && pdata_len > 0)
  393. return ERR_PTR(-EINVAL);
  394. pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
  395. oh_cnt);
  396. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  397. if (!od)
  398. return ERR_PTR(-ENOMEM);
  399. od->hwmods_cnt = oh_cnt;
  400. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
  401. GFP_KERNEL);
  402. if (!hwmods)
  403. goto odbs_exit1;
  404. memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
  405. od->hwmods = hwmods;
  406. pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
  407. if (!pdev_name2)
  408. goto odbs_exit2;
  409. strcpy(pdev_name2, pdev_name);
  410. od->pdev.name = pdev_name2;
  411. od->pdev.id = pdev_id;
  412. res_count = omap_device_count_resources(od);
  413. if (res_count > 0) {
  414. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  415. if (!res)
  416. goto odbs_exit3;
  417. }
  418. omap_device_fill_resources(od, res);
  419. od->pdev.num_resources = res_count;
  420. od->pdev.resource = res;
  421. ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
  422. if (ret)
  423. goto odbs_exit4;
  424. od->pm_lats = pm_lats;
  425. od->pm_lats_cnt = pm_lats_cnt;
  426. if (is_early_device)
  427. ret = omap_early_device_register(od);
  428. else
  429. ret = omap_device_register(od);
  430. for (i = 0; i < oh_cnt; i++) {
  431. hwmods[i]->od = od;
  432. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  433. }
  434. if (ret)
  435. goto odbs_exit4;
  436. return od;
  437. odbs_exit4:
  438. kfree(res);
  439. odbs_exit3:
  440. kfree(pdev_name2);
  441. odbs_exit2:
  442. kfree(hwmods);
  443. odbs_exit1:
  444. kfree(od);
  445. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  446. return ERR_PTR(ret);
  447. }
  448. /**
  449. * omap_early_device_register - register an omap_device as an early platform
  450. * device.
  451. * @od: struct omap_device * to register
  452. *
  453. * Register the omap_device structure. This currently just calls
  454. * platform_early_add_device() on the underlying platform_device.
  455. * Returns 0 by default.
  456. */
  457. int omap_early_device_register(struct omap_device *od)
  458. {
  459. struct platform_device *devices[1];
  460. devices[0] = &(od->pdev);
  461. early_platform_add_devices(devices, 1);
  462. return 0;
  463. }
  464. #ifdef CONFIG_PM_RUNTIME
  465. static int _od_runtime_suspend(struct device *dev)
  466. {
  467. struct platform_device *pdev = to_platform_device(dev);
  468. int ret;
  469. ret = pm_generic_runtime_suspend(dev);
  470. if (!ret)
  471. omap_device_idle(pdev);
  472. return ret;
  473. }
  474. static int _od_runtime_idle(struct device *dev)
  475. {
  476. return pm_generic_runtime_idle(dev);
  477. }
  478. static int _od_runtime_resume(struct device *dev)
  479. {
  480. struct platform_device *pdev = to_platform_device(dev);
  481. omap_device_enable(pdev);
  482. return pm_generic_runtime_resume(dev);
  483. }
  484. #endif
  485. #ifdef CONFIG_SUSPEND
  486. static int _od_suspend_noirq(struct device *dev)
  487. {
  488. struct platform_device *pdev = to_platform_device(dev);
  489. struct omap_device *od = to_omap_device(pdev);
  490. int ret;
  491. if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
  492. return pm_generic_suspend_noirq(dev);
  493. ret = pm_generic_suspend_noirq(dev);
  494. if (!ret && !pm_runtime_status_suspended(dev)) {
  495. if (pm_generic_runtime_suspend(dev) == 0) {
  496. omap_device_idle(pdev);
  497. od->flags |= OMAP_DEVICE_SUSPENDED;
  498. }
  499. }
  500. return ret;
  501. }
  502. static int _od_resume_noirq(struct device *dev)
  503. {
  504. struct platform_device *pdev = to_platform_device(dev);
  505. struct omap_device *od = to_omap_device(pdev);
  506. if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
  507. return pm_generic_resume_noirq(dev);
  508. if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
  509. !pm_runtime_status_suspended(dev)) {
  510. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  511. omap_device_enable(pdev);
  512. pm_generic_runtime_resume(dev);
  513. }
  514. return pm_generic_resume_noirq(dev);
  515. }
  516. #else
  517. #define _od_suspend_noirq NULL
  518. #define _od_resume_noirq NULL
  519. #endif
  520. static struct dev_pm_domain omap_device_pm_domain = {
  521. .ops = {
  522. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  523. _od_runtime_idle)
  524. USE_PLATFORM_PM_SLEEP_OPS
  525. .suspend_noirq = _od_suspend_noirq,
  526. .resume_noirq = _od_resume_noirq,
  527. }
  528. };
  529. /**
  530. * omap_device_register - register an omap_device with one omap_hwmod
  531. * @od: struct omap_device * to register
  532. *
  533. * Register the omap_device structure. This currently just calls
  534. * platform_device_register() on the underlying platform_device.
  535. * Returns the return value of platform_device_register().
  536. */
  537. int omap_device_register(struct omap_device *od)
  538. {
  539. pr_debug("omap_device: %s: registering\n", od->pdev.name);
  540. od->pdev.dev.parent = &omap_device_parent;
  541. od->pdev.dev.pm_domain = &omap_device_pm_domain;
  542. return platform_device_register(&od->pdev);
  543. }
  544. /* Public functions for use by device drivers through struct platform_data */
  545. /**
  546. * omap_device_enable - fully activate an omap_device
  547. * @od: struct omap_device * to activate
  548. *
  549. * Do whatever is necessary for the hwmods underlying omap_device @od
  550. * to be accessible and ready to operate. This generally involves
  551. * enabling clocks, setting SYSCONFIG registers; and in the future may
  552. * involve remuxing pins. Device drivers should call this function
  553. * (through platform_data function pointers) where they would normally
  554. * enable clocks, etc. Returns -EINVAL if called when the omap_device
  555. * is already enabled, or passes along the return value of
  556. * _omap_device_activate().
  557. */
  558. int omap_device_enable(struct platform_device *pdev)
  559. {
  560. int ret;
  561. struct omap_device *od;
  562. od = to_omap_device(pdev);
  563. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  564. dev_warn(&pdev->dev,
  565. "omap_device: %s() called from invalid state %d\n",
  566. __func__, od->_state);
  567. return -EINVAL;
  568. }
  569. /* Enable everything if we're enabling this device from scratch */
  570. if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
  571. od->pm_lat_level = od->pm_lats_cnt;
  572. ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
  573. od->dev_wakeup_lat = 0;
  574. od->_dev_wakeup_lat_limit = UINT_MAX;
  575. od->_state = OMAP_DEVICE_STATE_ENABLED;
  576. return ret;
  577. }
  578. /**
  579. * omap_device_idle - idle an omap_device
  580. * @od: struct omap_device * to idle
  581. *
  582. * Idle omap_device @od by calling as many .deactivate_func() entries
  583. * in the omap_device's pm_lats table as is possible without exceeding
  584. * the device's maximum wakeup latency limit, pm_lat_limit. Device
  585. * drivers should call this function (through platform_data function
  586. * pointers) where they would normally disable clocks after operations
  587. * complete, etc.. Returns -EINVAL if the omap_device is not
  588. * currently enabled, or passes along the return value of
  589. * _omap_device_deactivate().
  590. */
  591. int omap_device_idle(struct platform_device *pdev)
  592. {
  593. int ret;
  594. struct omap_device *od;
  595. od = to_omap_device(pdev);
  596. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  597. dev_warn(&pdev->dev,
  598. "omap_device: %s() called from invalid state %d\n",
  599. __func__, od->_state);
  600. return -EINVAL;
  601. }
  602. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  603. od->_state = OMAP_DEVICE_STATE_IDLE;
  604. return ret;
  605. }
  606. /**
  607. * omap_device_shutdown - shut down an omap_device
  608. * @od: struct omap_device * to shut down
  609. *
  610. * Shut down omap_device @od by calling all .deactivate_func() entries
  611. * in the omap_device's pm_lats table and then shutting down all of
  612. * the underlying omap_hwmods. Used when a device is being "removed"
  613. * or a device driver is being unloaded. Returns -EINVAL if the
  614. * omap_device is not currently enabled or idle, or passes along the
  615. * return value of _omap_device_deactivate().
  616. */
  617. int omap_device_shutdown(struct platform_device *pdev)
  618. {
  619. int ret, i;
  620. struct omap_device *od;
  621. od = to_omap_device(pdev);
  622. if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
  623. od->_state != OMAP_DEVICE_STATE_IDLE) {
  624. dev_warn(&pdev->dev,
  625. "omap_device: %s() called from invalid state %d\n",
  626. __func__, od->_state);
  627. return -EINVAL;
  628. }
  629. ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
  630. for (i = 0; i < od->hwmods_cnt; i++)
  631. omap_hwmod_shutdown(od->hwmods[i]);
  632. od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
  633. return ret;
  634. }
  635. /**
  636. * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
  637. * @od: struct omap_device *
  638. *
  639. * When a device's maximum wakeup latency limit changes, call some of
  640. * the .activate_func or .deactivate_func function pointers in the
  641. * omap_device's pm_lats array to ensure that the device's maximum
  642. * wakeup latency is less than or equal to the new latency limit.
  643. * Intended to be called by OMAP PM code whenever a device's maximum
  644. * wakeup latency limit changes (e.g., via
  645. * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
  646. * done (e.g., if the omap_device is not currently idle, or if the
  647. * wakeup latency is already current with the new limit) or passes
  648. * along the return value of _omap_device_deactivate() or
  649. * _omap_device_activate().
  650. */
  651. int omap_device_align_pm_lat(struct platform_device *pdev,
  652. u32 new_wakeup_lat_limit)
  653. {
  654. int ret = -EINVAL;
  655. struct omap_device *od;
  656. od = to_omap_device(pdev);
  657. if (new_wakeup_lat_limit == od->dev_wakeup_lat)
  658. return 0;
  659. od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
  660. if (od->_state != OMAP_DEVICE_STATE_IDLE)
  661. return 0;
  662. else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
  663. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  664. else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
  665. ret = _omap_device_activate(od, USE_WAKEUP_LAT);
  666. return ret;
  667. }
  668. /**
  669. * omap_device_get_pwrdm - return the powerdomain * associated with @od
  670. * @od: struct omap_device *
  671. *
  672. * Return the powerdomain associated with the first underlying
  673. * omap_hwmod for this omap_device. Intended for use by core OMAP PM
  674. * code. Returns NULL on error or a struct powerdomain * upon
  675. * success.
  676. */
  677. struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
  678. {
  679. /*
  680. * XXX Assumes that all omap_hwmod powerdomains are identical.
  681. * This may not necessarily be true. There should be a sanity
  682. * check in here to WARN() if any difference appears.
  683. */
  684. if (!od->hwmods_cnt)
  685. return NULL;
  686. return omap_hwmod_get_pwrdm(od->hwmods[0]);
  687. }
  688. /**
  689. * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
  690. * @od: struct omap_device *
  691. *
  692. * Return the MPU's virtual address for the base of the hwmod, from
  693. * the ioremap() that the hwmod code does. Only valid if there is one
  694. * hwmod associated with this device. Returns NULL if there are zero
  695. * or more than one hwmods associated with this omap_device;
  696. * otherwise, passes along the return value from
  697. * omap_hwmod_get_mpu_rt_va().
  698. */
  699. void __iomem *omap_device_get_rt_va(struct omap_device *od)
  700. {
  701. if (od->hwmods_cnt != 1)
  702. return NULL;
  703. return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
  704. }
  705. /*
  706. * Public functions intended for use in omap_device_pm_latency
  707. * .activate_func and .deactivate_func function pointers
  708. */
  709. /**
  710. * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  711. * @od: struct omap_device *od
  712. *
  713. * Enable all underlying hwmods. Returns 0.
  714. */
  715. int omap_device_enable_hwmods(struct omap_device *od)
  716. {
  717. int i;
  718. for (i = 0; i < od->hwmods_cnt; i++)
  719. omap_hwmod_enable(od->hwmods[i]);
  720. /* XXX pass along return value here? */
  721. return 0;
  722. }
  723. /**
  724. * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  725. * @od: struct omap_device *od
  726. *
  727. * Idle all underlying hwmods. Returns 0.
  728. */
  729. int omap_device_idle_hwmods(struct omap_device *od)
  730. {
  731. int i;
  732. for (i = 0; i < od->hwmods_cnt; i++)
  733. omap_hwmod_idle(od->hwmods[i]);
  734. /* XXX pass along return value here? */
  735. return 0;
  736. }
  737. /**
  738. * omap_device_disable_clocks - disable all main and interface clocks
  739. * @od: struct omap_device *od
  740. *
  741. * Disable the main functional clock and interface clock for all of the
  742. * omap_hwmods associated with the omap_device. Returns 0.
  743. */
  744. int omap_device_disable_clocks(struct omap_device *od)
  745. {
  746. int i;
  747. for (i = 0; i < od->hwmods_cnt; i++)
  748. omap_hwmod_disable_clocks(od->hwmods[i]);
  749. /* XXX pass along return value here? */
  750. return 0;
  751. }
  752. /**
  753. * omap_device_enable_clocks - enable all main and interface clocks
  754. * @od: struct omap_device *od
  755. *
  756. * Enable the main functional clock and interface clock for all of the
  757. * omap_hwmods associated with the omap_device. Returns 0.
  758. */
  759. int omap_device_enable_clocks(struct omap_device *od)
  760. {
  761. int i;
  762. for (i = 0; i < od->hwmods_cnt; i++)
  763. omap_hwmod_enable_clocks(od->hwmods[i]);
  764. /* XXX pass along return value here? */
  765. return 0;
  766. }
  767. struct device omap_device_parent = {
  768. .init_name = "omap",
  769. .parent = &platform_bus,
  770. };
  771. static int __init omap_device_init(void)
  772. {
  773. return device_register(&omap_device_parent);
  774. }
  775. core_initcall(omap_device_init);