omap_device.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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/export.h>
  81. #include <linux/platform_device.h>
  82. #include <linux/slab.h>
  83. #include <linux/err.h>
  84. #include <linux/io.h>
  85. #include <linux/clk.h>
  86. #include <linux/clkdev.h>
  87. #include <linux/pm_runtime.h>
  88. #include <linux/of.h>
  89. #include <linux/notifier.h>
  90. #include <plat/omap_device.h>
  91. #include <plat/omap_hwmod.h>
  92. #include <plat/clock.h>
  93. /* These parameters are passed to _omap_device_{de,}activate() */
  94. #define USE_WAKEUP_LAT 0
  95. #define IGNORE_WAKEUP_LAT 1
  96. static int omap_early_device_register(struct platform_device *pdev);
  97. static struct omap_device_pm_latency omap_default_latency[] = {
  98. {
  99. .deactivate_func = omap_device_idle_hwmods,
  100. .activate_func = omap_device_enable_hwmods,
  101. .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
  102. }
  103. };
  104. /* Private functions */
  105. /**
  106. * _omap_device_activate - increase device readiness
  107. * @od: struct omap_device *
  108. * @ignore_lat: increase to latency target (0) or full readiness (1)?
  109. *
  110. * Increase readiness of omap_device @od (thus decreasing device
  111. * wakeup latency, but consuming more power). If @ignore_lat is
  112. * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
  113. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  114. * latency is greater than the requested maximum wakeup latency, step
  115. * backwards in the omap_device_pm_latency table to ensure the
  116. * device's maximum wakeup latency is less than or equal to the
  117. * requested maximum wakeup latency. Returns 0.
  118. */
  119. static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
  120. {
  121. struct timespec a, b, c;
  122. dev_dbg(&od->pdev->dev, "omap_device: activating\n");
  123. while (od->pm_lat_level > 0) {
  124. struct omap_device_pm_latency *odpl;
  125. unsigned long long act_lat = 0;
  126. od->pm_lat_level--;
  127. odpl = od->pm_lats + od->pm_lat_level;
  128. if (!ignore_lat &&
  129. (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
  130. break;
  131. read_persistent_clock(&a);
  132. /* XXX check return code */
  133. odpl->activate_func(od);
  134. read_persistent_clock(&b);
  135. c = timespec_sub(b, a);
  136. act_lat = timespec_to_ns(&c);
  137. dev_dbg(&od->pdev->dev,
  138. "omap_device: pm_lat %d: activate: elapsed time "
  139. "%llu nsec\n", od->pm_lat_level, act_lat);
  140. if (act_lat > odpl->activate_lat) {
  141. odpl->activate_lat_worst = act_lat;
  142. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  143. odpl->activate_lat = act_lat;
  144. dev_dbg(&od->pdev->dev,
  145. "new worst case activate latency "
  146. "%d: %llu\n",
  147. od->pm_lat_level, act_lat);
  148. } else
  149. dev_warn(&od->pdev->dev,
  150. "activate latency %d "
  151. "higher than exptected. (%llu > %d)\n",
  152. od->pm_lat_level, act_lat,
  153. odpl->activate_lat);
  154. }
  155. od->dev_wakeup_lat -= odpl->activate_lat;
  156. }
  157. return 0;
  158. }
  159. /**
  160. * _omap_device_deactivate - decrease device readiness
  161. * @od: struct omap_device *
  162. * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
  163. *
  164. * Decrease readiness of omap_device @od (thus increasing device
  165. * wakeup latency, but conserving power). If @ignore_lat is
  166. * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
  167. * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
  168. * latency is less than the requested maximum wakeup latency, step
  169. * forwards in the omap_device_pm_latency table to ensure the device's
  170. * maximum wakeup latency is less than or equal to the requested
  171. * maximum wakeup latency. Returns 0.
  172. */
  173. static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
  174. {
  175. struct timespec a, b, c;
  176. dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
  177. while (od->pm_lat_level < od->pm_lats_cnt) {
  178. struct omap_device_pm_latency *odpl;
  179. unsigned long long deact_lat = 0;
  180. odpl = od->pm_lats + od->pm_lat_level;
  181. if (!ignore_lat &&
  182. ((od->dev_wakeup_lat + odpl->activate_lat) >
  183. od->_dev_wakeup_lat_limit))
  184. break;
  185. read_persistent_clock(&a);
  186. /* XXX check return code */
  187. odpl->deactivate_func(od);
  188. read_persistent_clock(&b);
  189. c = timespec_sub(b, a);
  190. deact_lat = timespec_to_ns(&c);
  191. dev_dbg(&od->pdev->dev,
  192. "omap_device: pm_lat %d: deactivate: elapsed time "
  193. "%llu nsec\n", od->pm_lat_level, deact_lat);
  194. if (deact_lat > odpl->deactivate_lat) {
  195. odpl->deactivate_lat_worst = deact_lat;
  196. if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
  197. odpl->deactivate_lat = deact_lat;
  198. dev_dbg(&od->pdev->dev,
  199. "new worst case deactivate latency "
  200. "%d: %llu\n",
  201. od->pm_lat_level, deact_lat);
  202. } else
  203. dev_warn(&od->pdev->dev,
  204. "deactivate latency %d "
  205. "higher than exptected. (%llu > %d)\n",
  206. od->pm_lat_level, deact_lat,
  207. odpl->deactivate_lat);
  208. }
  209. od->dev_wakeup_lat += odpl->activate_lat;
  210. od->pm_lat_level++;
  211. }
  212. return 0;
  213. }
  214. static void _add_clkdev(struct omap_device *od, const char *clk_alias,
  215. const char *clk_name)
  216. {
  217. struct clk *r;
  218. struct clk_lookup *l;
  219. if (!clk_alias || !clk_name)
  220. return;
  221. dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
  222. r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
  223. if (!IS_ERR(r)) {
  224. dev_warn(&od->pdev->dev,
  225. "alias %s already exists\n", clk_alias);
  226. clk_put(r);
  227. return;
  228. }
  229. r = omap_clk_get_by_name(clk_name);
  230. if (IS_ERR(r)) {
  231. dev_err(&od->pdev->dev,
  232. "omap_clk_get_by_name for %s failed\n", clk_name);
  233. return;
  234. }
  235. l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
  236. if (!l) {
  237. dev_err(&od->pdev->dev,
  238. "clkdev_alloc for %s failed\n", clk_alias);
  239. return;
  240. }
  241. clkdev_add(l);
  242. }
  243. /**
  244. * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
  245. * and main clock
  246. * @od: struct omap_device *od
  247. * @oh: struct omap_hwmod *oh
  248. *
  249. * For the main clock and every optional clock present per hwmod per
  250. * omap_device, this function adds an entry in the clkdev table of the
  251. * form <dev-id=dev_name, con-id=role> if it does not exist already.
  252. *
  253. * The function is called from inside omap_device_build_ss(), after
  254. * omap_device_register.
  255. *
  256. * This allows drivers to get a pointer to its optional clocks based on its role
  257. * by calling clk_get(<dev*>, <role>).
  258. * In the case of the main clock, a "fck" alias is used.
  259. *
  260. * No return value.
  261. */
  262. static void _add_hwmod_clocks_clkdev(struct omap_device *od,
  263. struct omap_hwmod *oh)
  264. {
  265. int i;
  266. _add_clkdev(od, "fck", oh->main_clk);
  267. for (i = 0; i < oh->opt_clks_cnt; i++)
  268. _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
  269. }
  270. /**
  271. * omap_device_build_from_dt - build an omap_device with multiple hwmods
  272. * @pdev_name: name of the platform_device driver to use
  273. * @pdev_id: this platform_device's connection ID
  274. * @oh: ptr to the single omap_hwmod that backs this omap_device
  275. * @pdata: platform_data ptr to associate with the platform_device
  276. * @pdata_len: amount of memory pointed to by @pdata
  277. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  278. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  279. * @is_early_device: should the device be registered as an early device or not
  280. *
  281. * Function for building an omap_device already registered from device-tree
  282. *
  283. * Returns 0 or PTR_ERR() on error.
  284. */
  285. static int omap_device_build_from_dt(struct platform_device *pdev)
  286. {
  287. struct omap_hwmod **hwmods;
  288. struct omap_device *od;
  289. struct omap_hwmod *oh;
  290. struct device_node *node = pdev->dev.of_node;
  291. const char *oh_name;
  292. int oh_cnt, i, ret = 0;
  293. oh_cnt = of_property_count_strings(node, "ti,hwmods");
  294. if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
  295. dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
  296. return -ENODEV;
  297. }
  298. hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  299. if (!hwmods) {
  300. ret = -ENOMEM;
  301. goto odbfd_exit;
  302. }
  303. for (i = 0; i < oh_cnt; i++) {
  304. of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
  305. oh = omap_hwmod_lookup(oh_name);
  306. if (!oh) {
  307. dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
  308. oh_name);
  309. ret = -EINVAL;
  310. goto odbfd_exit1;
  311. }
  312. hwmods[i] = oh;
  313. }
  314. od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
  315. if (!od) {
  316. dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
  317. oh_name);
  318. ret = PTR_ERR(od);
  319. goto odbfd_exit1;
  320. }
  321. if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
  322. omap_device_disable_idle_on_suspend(pdev);
  323. pdev->dev.pm_domain = &omap_device_pm_domain;
  324. odbfd_exit1:
  325. kfree(hwmods);
  326. odbfd_exit:
  327. return ret;
  328. }
  329. static int _omap_device_notifier_call(struct notifier_block *nb,
  330. unsigned long event, void *dev)
  331. {
  332. struct platform_device *pdev = to_platform_device(dev);
  333. struct omap_device *od;
  334. switch (event) {
  335. case BUS_NOTIFY_DEL_DEVICE:
  336. if (pdev->archdata.od)
  337. omap_device_delete(pdev->archdata.od);
  338. break;
  339. case BUS_NOTIFY_ADD_DEVICE:
  340. if (pdev->dev.of_node)
  341. omap_device_build_from_dt(pdev);
  342. /* fall through */
  343. default:
  344. od = to_omap_device(pdev);
  345. if (od)
  346. od->_driver_status = event;
  347. }
  348. return NOTIFY_DONE;
  349. }
  350. /* Public functions for use by core code */
  351. /**
  352. * omap_device_get_context_loss_count - get lost context count
  353. * @od: struct omap_device *
  354. *
  355. * Using the primary hwmod, query the context loss count for this
  356. * device.
  357. *
  358. * Callers should consider context for this device lost any time this
  359. * function returns a value different than the value the caller got
  360. * the last time it called this function.
  361. *
  362. * If any hwmods exist for the omap_device assoiated with @pdev,
  363. * return the context loss counter for that hwmod, otherwise return
  364. * zero.
  365. */
  366. int omap_device_get_context_loss_count(struct platform_device *pdev)
  367. {
  368. struct omap_device *od;
  369. u32 ret = 0;
  370. od = to_omap_device(pdev);
  371. if (od->hwmods_cnt)
  372. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  373. return ret;
  374. }
  375. /**
  376. * omap_device_count_resources - count number of struct resource entries needed
  377. * @od: struct omap_device *
  378. *
  379. * Count the number of struct resource entries needed for this
  380. * omap_device @od. Used by omap_device_build_ss() to determine how
  381. * much memory to allocate before calling
  382. * omap_device_fill_resources(). Returns the count.
  383. */
  384. static int omap_device_count_resources(struct omap_device *od)
  385. {
  386. int c = 0;
  387. int i;
  388. for (i = 0; i < od->hwmods_cnt; i++)
  389. c += omap_hwmod_count_resources(od->hwmods[i]);
  390. pr_debug("omap_device: %s: counted %d total resources across %d "
  391. "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
  392. return c;
  393. }
  394. /**
  395. * omap_device_fill_resources - fill in array of struct resource
  396. * @od: struct omap_device *
  397. * @res: pointer to an array of struct resource to be filled in
  398. *
  399. * Populate one or more empty struct resource pointed to by @res with
  400. * the resource data for this omap_device @od. Used by
  401. * omap_device_build_ss() after calling omap_device_count_resources().
  402. * Ideally this function would not be needed at all. If omap_device
  403. * replaces platform_device, then we can specify our own
  404. * get_resource()/ get_irq()/etc functions that use the underlying
  405. * omap_hwmod information. Or if platform_device is extended to use
  406. * subarchitecture-specific function pointers, the various
  407. * platform_device functions can simply call omap_device internal
  408. * functions to get device resources. Hacking around the existing
  409. * platform_device code wastes memory. Returns 0.
  410. */
  411. static int omap_device_fill_resources(struct omap_device *od,
  412. struct resource *res)
  413. {
  414. int i, r;
  415. for (i = 0; i < od->hwmods_cnt; i++) {
  416. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  417. res += r;
  418. }
  419. return 0;
  420. }
  421. /**
  422. * omap_device_alloc - allocate an omap_device
  423. * @pdev: platform_device that will be included in this omap_device
  424. * @oh: ptr to the single omap_hwmod that backs this omap_device
  425. * @pdata: platform_data ptr to associate with the platform_device
  426. * @pdata_len: amount of memory pointed to by @pdata
  427. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  428. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  429. *
  430. * Convenience function for allocating an omap_device structure and filling
  431. * hwmods, resources and pm_latency attributes.
  432. *
  433. * Returns an struct omap_device pointer or ERR_PTR() on error;
  434. */
  435. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  436. struct omap_hwmod **ohs, int oh_cnt,
  437. struct omap_device_pm_latency *pm_lats,
  438. int pm_lats_cnt)
  439. {
  440. int ret = -ENOMEM;
  441. struct omap_device *od;
  442. struct resource *res = NULL;
  443. int i, res_count;
  444. struct omap_hwmod **hwmods;
  445. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  446. if (!od) {
  447. ret = -ENOMEM;
  448. goto oda_exit1;
  449. }
  450. od->hwmods_cnt = oh_cnt;
  451. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  452. if (!hwmods)
  453. goto oda_exit2;
  454. od->hwmods = hwmods;
  455. od->pdev = pdev;
  456. /*
  457. * HACK: Ideally the resources from DT should match, and hwmod
  458. * should just add the missing ones. Since the name is not
  459. * properly populated by DT, stick to hwmod resources only.
  460. */
  461. if (pdev->num_resources && pdev->resource)
  462. dev_warn(&pdev->dev, "%s(): resources already allocated %d\n",
  463. __func__, pdev->num_resources);
  464. res_count = omap_device_count_resources(od);
  465. if (res_count > 0) {
  466. dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n",
  467. __func__, res_count);
  468. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  469. if (!res)
  470. goto oda_exit3;
  471. omap_device_fill_resources(od, res);
  472. ret = platform_device_add_resources(pdev, res, res_count);
  473. kfree(res);
  474. if (ret)
  475. goto oda_exit3;
  476. }
  477. if (!pm_lats) {
  478. pm_lats = omap_default_latency;
  479. pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
  480. }
  481. od->pm_lats_cnt = pm_lats_cnt;
  482. od->pm_lats = kmemdup(pm_lats,
  483. sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
  484. GFP_KERNEL);
  485. if (!od->pm_lats)
  486. goto oda_exit3;
  487. pdev->archdata.od = od;
  488. for (i = 0; i < oh_cnt; i++) {
  489. hwmods[i]->od = od;
  490. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  491. }
  492. return od;
  493. oda_exit3:
  494. kfree(hwmods);
  495. oda_exit2:
  496. kfree(od);
  497. oda_exit1:
  498. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  499. return ERR_PTR(ret);
  500. }
  501. void omap_device_delete(struct omap_device *od)
  502. {
  503. if (!od)
  504. return;
  505. od->pdev->archdata.od = NULL;
  506. kfree(od->pm_lats);
  507. kfree(od->hwmods);
  508. kfree(od);
  509. }
  510. /**
  511. * omap_device_build - build and register an omap_device with one omap_hwmod
  512. * @pdev_name: name of the platform_device driver to use
  513. * @pdev_id: this platform_device's connection ID
  514. * @oh: ptr to the single omap_hwmod that backs this omap_device
  515. * @pdata: platform_data ptr to associate with the platform_device
  516. * @pdata_len: amount of memory pointed to by @pdata
  517. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  518. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  519. * @is_early_device: should the device be registered as an early device or not
  520. *
  521. * Convenience function for building and registering a single
  522. * omap_device record, which in turn builds and registers a
  523. * platform_device record. See omap_device_build_ss() for more
  524. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  525. * passes along the return value of omap_device_build_ss().
  526. */
  527. struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
  528. struct omap_hwmod *oh, void *pdata,
  529. int pdata_len,
  530. struct omap_device_pm_latency *pm_lats,
  531. int pm_lats_cnt, int is_early_device)
  532. {
  533. struct omap_hwmod *ohs[] = { oh };
  534. if (!oh)
  535. return ERR_PTR(-EINVAL);
  536. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  537. pdata_len, pm_lats, pm_lats_cnt,
  538. is_early_device);
  539. }
  540. /**
  541. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  542. * @pdev_name: name of the platform_device driver to use
  543. * @pdev_id: this platform_device's connection ID
  544. * @oh: ptr to the single omap_hwmod that backs this omap_device
  545. * @pdata: platform_data ptr to associate with the platform_device
  546. * @pdata_len: amount of memory pointed to by @pdata
  547. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  548. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  549. * @is_early_device: should the device be registered as an early device or not
  550. *
  551. * Convenience function for building and registering an omap_device
  552. * subsystem record. Subsystem records consist of multiple
  553. * omap_hwmods. This function in turn builds and registers a
  554. * platform_device record. Returns an ERR_PTR() on error, or passes
  555. * along the return value of omap_device_register().
  556. */
  557. struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
  558. struct omap_hwmod **ohs, int oh_cnt,
  559. void *pdata, int pdata_len,
  560. struct omap_device_pm_latency *pm_lats,
  561. int pm_lats_cnt, int is_early_device)
  562. {
  563. int ret = -ENOMEM;
  564. struct platform_device *pdev;
  565. struct omap_device *od;
  566. if (!ohs || oh_cnt == 0 || !pdev_name)
  567. return ERR_PTR(-EINVAL);
  568. if (!pdata && pdata_len > 0)
  569. return ERR_PTR(-EINVAL);
  570. pdev = platform_device_alloc(pdev_name, pdev_id);
  571. if (!pdev) {
  572. ret = -ENOMEM;
  573. goto odbs_exit;
  574. }
  575. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  576. if (pdev->id != -1)
  577. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  578. else
  579. dev_set_name(&pdev->dev, "%s", pdev->name);
  580. od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
  581. if (!od)
  582. goto odbs_exit1;
  583. ret = platform_device_add_data(pdev, pdata, pdata_len);
  584. if (ret)
  585. goto odbs_exit2;
  586. if (is_early_device)
  587. ret = omap_early_device_register(pdev);
  588. else
  589. ret = omap_device_register(pdev);
  590. if (ret)
  591. goto odbs_exit2;
  592. return pdev;
  593. odbs_exit2:
  594. omap_device_delete(od);
  595. odbs_exit1:
  596. platform_device_put(pdev);
  597. odbs_exit:
  598. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  599. return ERR_PTR(ret);
  600. }
  601. /**
  602. * omap_early_device_register - register an omap_device as an early platform
  603. * device.
  604. * @od: struct omap_device * to register
  605. *
  606. * Register the omap_device structure. This currently just calls
  607. * platform_early_add_device() on the underlying platform_device.
  608. * Returns 0 by default.
  609. */
  610. static int __init omap_early_device_register(struct platform_device *pdev)
  611. {
  612. struct platform_device *devices[1];
  613. devices[0] = pdev;
  614. early_platform_add_devices(devices, 1);
  615. return 0;
  616. }
  617. #ifdef CONFIG_PM_RUNTIME
  618. static int _od_runtime_suspend(struct device *dev)
  619. {
  620. struct platform_device *pdev = to_platform_device(dev);
  621. int ret;
  622. ret = pm_generic_runtime_suspend(dev);
  623. if (!ret)
  624. omap_device_idle(pdev);
  625. return ret;
  626. }
  627. static int _od_runtime_idle(struct device *dev)
  628. {
  629. return pm_generic_runtime_idle(dev);
  630. }
  631. static int _od_runtime_resume(struct device *dev)
  632. {
  633. struct platform_device *pdev = to_platform_device(dev);
  634. omap_device_enable(pdev);
  635. return pm_generic_runtime_resume(dev);
  636. }
  637. #endif
  638. #ifdef CONFIG_SUSPEND
  639. static int _od_suspend_noirq(struct device *dev)
  640. {
  641. struct platform_device *pdev = to_platform_device(dev);
  642. struct omap_device *od = to_omap_device(pdev);
  643. int ret;
  644. ret = pm_generic_suspend_noirq(dev);
  645. if (!ret && !pm_runtime_status_suspended(dev)) {
  646. if (pm_generic_runtime_suspend(dev) == 0) {
  647. if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
  648. omap_device_idle(pdev);
  649. od->flags |= OMAP_DEVICE_SUSPENDED;
  650. }
  651. }
  652. return ret;
  653. }
  654. static int _od_resume_noirq(struct device *dev)
  655. {
  656. struct platform_device *pdev = to_platform_device(dev);
  657. struct omap_device *od = to_omap_device(pdev);
  658. if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
  659. !pm_runtime_status_suspended(dev)) {
  660. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  661. if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
  662. omap_device_enable(pdev);
  663. pm_generic_runtime_resume(dev);
  664. }
  665. return pm_generic_resume_noirq(dev);
  666. }
  667. #else
  668. #define _od_suspend_noirq NULL
  669. #define _od_resume_noirq NULL
  670. #endif
  671. struct dev_pm_domain omap_device_pm_domain = {
  672. .ops = {
  673. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  674. _od_runtime_idle)
  675. USE_PLATFORM_PM_SLEEP_OPS
  676. .suspend_noirq = _od_suspend_noirq,
  677. .resume_noirq = _od_resume_noirq,
  678. }
  679. };
  680. /**
  681. * omap_device_register - register an omap_device with one omap_hwmod
  682. * @od: struct omap_device * to register
  683. *
  684. * Register the omap_device structure. This currently just calls
  685. * platform_device_register() on the underlying platform_device.
  686. * Returns the return value of platform_device_register().
  687. */
  688. int omap_device_register(struct platform_device *pdev)
  689. {
  690. pr_debug("omap_device: %s: registering\n", pdev->name);
  691. pdev->dev.pm_domain = &omap_device_pm_domain;
  692. return platform_device_add(pdev);
  693. }
  694. /* Public functions for use by device drivers through struct platform_data */
  695. /**
  696. * omap_device_enable - fully activate an omap_device
  697. * @od: struct omap_device * to activate
  698. *
  699. * Do whatever is necessary for the hwmods underlying omap_device @od
  700. * to be accessible and ready to operate. This generally involves
  701. * enabling clocks, setting SYSCONFIG registers; and in the future may
  702. * involve remuxing pins. Device drivers should call this function
  703. * (through platform_data function pointers) where they would normally
  704. * enable clocks, etc. Returns -EINVAL if called when the omap_device
  705. * is already enabled, or passes along the return value of
  706. * _omap_device_activate().
  707. */
  708. int omap_device_enable(struct platform_device *pdev)
  709. {
  710. int ret;
  711. struct omap_device *od;
  712. od = to_omap_device(pdev);
  713. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  714. dev_warn(&pdev->dev,
  715. "omap_device: %s() called from invalid state %d\n",
  716. __func__, od->_state);
  717. return -EINVAL;
  718. }
  719. /* Enable everything if we're enabling this device from scratch */
  720. if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
  721. od->pm_lat_level = od->pm_lats_cnt;
  722. ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
  723. od->dev_wakeup_lat = 0;
  724. od->_dev_wakeup_lat_limit = UINT_MAX;
  725. od->_state = OMAP_DEVICE_STATE_ENABLED;
  726. return ret;
  727. }
  728. /**
  729. * omap_device_idle - idle an omap_device
  730. * @od: struct omap_device * to idle
  731. *
  732. * Idle omap_device @od by calling as many .deactivate_func() entries
  733. * in the omap_device's pm_lats table as is possible without exceeding
  734. * the device's maximum wakeup latency limit, pm_lat_limit. Device
  735. * drivers should call this function (through platform_data function
  736. * pointers) where they would normally disable clocks after operations
  737. * complete, etc.. Returns -EINVAL if the omap_device is not
  738. * currently enabled, or passes along the return value of
  739. * _omap_device_deactivate().
  740. */
  741. int omap_device_idle(struct platform_device *pdev)
  742. {
  743. int ret;
  744. struct omap_device *od;
  745. od = to_omap_device(pdev);
  746. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  747. dev_warn(&pdev->dev,
  748. "omap_device: %s() called from invalid state %d\n",
  749. __func__, od->_state);
  750. return -EINVAL;
  751. }
  752. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  753. od->_state = OMAP_DEVICE_STATE_IDLE;
  754. return ret;
  755. }
  756. /**
  757. * omap_device_shutdown - shut down an omap_device
  758. * @od: struct omap_device * to shut down
  759. *
  760. * Shut down omap_device @od by calling all .deactivate_func() entries
  761. * in the omap_device's pm_lats table and then shutting down all of
  762. * the underlying omap_hwmods. Used when a device is being "removed"
  763. * or a device driver is being unloaded. Returns -EINVAL if the
  764. * omap_device is not currently enabled or idle, or passes along the
  765. * return value of _omap_device_deactivate().
  766. */
  767. int omap_device_shutdown(struct platform_device *pdev)
  768. {
  769. int ret, i;
  770. struct omap_device *od;
  771. od = to_omap_device(pdev);
  772. if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
  773. od->_state != OMAP_DEVICE_STATE_IDLE) {
  774. dev_warn(&pdev->dev,
  775. "omap_device: %s() called from invalid state %d\n",
  776. __func__, od->_state);
  777. return -EINVAL;
  778. }
  779. ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
  780. for (i = 0; i < od->hwmods_cnt; i++)
  781. omap_hwmod_shutdown(od->hwmods[i]);
  782. od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
  783. return ret;
  784. }
  785. /**
  786. * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
  787. * @od: struct omap_device *
  788. *
  789. * When a device's maximum wakeup latency limit changes, call some of
  790. * the .activate_func or .deactivate_func function pointers in the
  791. * omap_device's pm_lats array to ensure that the device's maximum
  792. * wakeup latency is less than or equal to the new latency limit.
  793. * Intended to be called by OMAP PM code whenever a device's maximum
  794. * wakeup latency limit changes (e.g., via
  795. * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
  796. * done (e.g., if the omap_device is not currently idle, or if the
  797. * wakeup latency is already current with the new limit) or passes
  798. * along the return value of _omap_device_deactivate() or
  799. * _omap_device_activate().
  800. */
  801. int omap_device_align_pm_lat(struct platform_device *pdev,
  802. u32 new_wakeup_lat_limit)
  803. {
  804. int ret = -EINVAL;
  805. struct omap_device *od;
  806. od = to_omap_device(pdev);
  807. if (new_wakeup_lat_limit == od->dev_wakeup_lat)
  808. return 0;
  809. od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
  810. if (od->_state != OMAP_DEVICE_STATE_IDLE)
  811. return 0;
  812. else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
  813. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  814. else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
  815. ret = _omap_device_activate(od, USE_WAKEUP_LAT);
  816. return ret;
  817. }
  818. /**
  819. * omap_device_get_pwrdm - return the powerdomain * associated with @od
  820. * @od: struct omap_device *
  821. *
  822. * Return the powerdomain associated with the first underlying
  823. * omap_hwmod for this omap_device. Intended for use by core OMAP PM
  824. * code. Returns NULL on error or a struct powerdomain * upon
  825. * success.
  826. */
  827. struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
  828. {
  829. /*
  830. * XXX Assumes that all omap_hwmod powerdomains are identical.
  831. * This may not necessarily be true. There should be a sanity
  832. * check in here to WARN() if any difference appears.
  833. */
  834. if (!od->hwmods_cnt)
  835. return NULL;
  836. return omap_hwmod_get_pwrdm(od->hwmods[0]);
  837. }
  838. /**
  839. * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
  840. * @od: struct omap_device *
  841. *
  842. * Return the MPU's virtual address for the base of the hwmod, from
  843. * the ioremap() that the hwmod code does. Only valid if there is one
  844. * hwmod associated with this device. Returns NULL if there are zero
  845. * or more than one hwmods associated with this omap_device;
  846. * otherwise, passes along the return value from
  847. * omap_hwmod_get_mpu_rt_va().
  848. */
  849. void __iomem *omap_device_get_rt_va(struct omap_device *od)
  850. {
  851. if (od->hwmods_cnt != 1)
  852. return NULL;
  853. return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
  854. }
  855. /**
  856. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  857. * device pointer.
  858. * @oh_name: name of the hwmod device
  859. *
  860. * Returns back a struct device * pointer associated with a hwmod
  861. * device represented by a hwmod_name
  862. */
  863. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  864. {
  865. struct omap_hwmod *oh;
  866. if (!oh_name) {
  867. WARN(1, "%s: no hwmod name!\n", __func__);
  868. return ERR_PTR(-EINVAL);
  869. }
  870. oh = omap_hwmod_lookup(oh_name);
  871. if (IS_ERR_OR_NULL(oh)) {
  872. WARN(1, "%s: no hwmod for %s\n", __func__,
  873. oh_name);
  874. return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
  875. }
  876. if (IS_ERR_OR_NULL(oh->od)) {
  877. WARN(1, "%s: no omap_device for %s\n", __func__,
  878. oh_name);
  879. return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
  880. }
  881. if (IS_ERR_OR_NULL(oh->od->pdev))
  882. return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
  883. return &oh->od->pdev->dev;
  884. }
  885. EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
  886. /*
  887. * Public functions intended for use in omap_device_pm_latency
  888. * .activate_func and .deactivate_func function pointers
  889. */
  890. /**
  891. * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  892. * @od: struct omap_device *od
  893. *
  894. * Enable all underlying hwmods. Returns 0.
  895. */
  896. int omap_device_enable_hwmods(struct omap_device *od)
  897. {
  898. int i;
  899. for (i = 0; i < od->hwmods_cnt; i++)
  900. omap_hwmod_enable(od->hwmods[i]);
  901. /* XXX pass along return value here? */
  902. return 0;
  903. }
  904. /**
  905. * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  906. * @od: struct omap_device *od
  907. *
  908. * Idle all underlying hwmods. Returns 0.
  909. */
  910. int omap_device_idle_hwmods(struct omap_device *od)
  911. {
  912. int i;
  913. for (i = 0; i < od->hwmods_cnt; i++)
  914. omap_hwmod_idle(od->hwmods[i]);
  915. /* XXX pass along return value here? */
  916. return 0;
  917. }
  918. /**
  919. * omap_device_disable_clocks - disable all main and interface clocks
  920. * @od: struct omap_device *od
  921. *
  922. * Disable the main functional clock and interface clock for all of the
  923. * omap_hwmods associated with the omap_device. Returns 0.
  924. */
  925. int omap_device_disable_clocks(struct omap_device *od)
  926. {
  927. int i;
  928. for (i = 0; i < od->hwmods_cnt; i++)
  929. omap_hwmod_disable_clocks(od->hwmods[i]);
  930. /* XXX pass along return value here? */
  931. return 0;
  932. }
  933. /**
  934. * omap_device_enable_clocks - enable all main and interface clocks
  935. * @od: struct omap_device *od
  936. *
  937. * Enable the main functional clock and interface clock for all of the
  938. * omap_hwmods associated with the omap_device. Returns 0.
  939. */
  940. int omap_device_enable_clocks(struct omap_device *od)
  941. {
  942. int i;
  943. for (i = 0; i < od->hwmods_cnt; i++)
  944. omap_hwmod_enable_clocks(od->hwmods[i]);
  945. /* XXX pass along return value here? */
  946. return 0;
  947. }
  948. static struct notifier_block platform_nb = {
  949. .notifier_call = _omap_device_notifier_call,
  950. };
  951. static int __init omap_device_init(void)
  952. {
  953. bus_register_notifier(&platform_bus_type, &platform_nb);
  954. return 0;
  955. }
  956. core_initcall(omap_device_init);