omap_device.c 26 KB

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