omap_device.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. /* Fix up missing resource names */
  322. for (i = 0; i < pdev->num_resources; i++) {
  323. struct resource *r = &pdev->resource[i];
  324. if (r->name == NULL)
  325. r->name = dev_name(&pdev->dev);
  326. }
  327. if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
  328. omap_device_disable_idle_on_suspend(pdev);
  329. pdev->dev.pm_domain = &omap_device_pm_domain;
  330. odbfd_exit1:
  331. kfree(hwmods);
  332. odbfd_exit:
  333. return ret;
  334. }
  335. static int _omap_device_notifier_call(struct notifier_block *nb,
  336. unsigned long event, void *dev)
  337. {
  338. struct platform_device *pdev = to_platform_device(dev);
  339. switch (event) {
  340. case BUS_NOTIFY_ADD_DEVICE:
  341. if (pdev->dev.of_node)
  342. omap_device_build_from_dt(pdev);
  343. break;
  344. case BUS_NOTIFY_DEL_DEVICE:
  345. if (pdev->archdata.od)
  346. omap_device_delete(pdev->archdata.od);
  347. break;
  348. }
  349. return NOTIFY_DONE;
  350. }
  351. /* Public functions for use by core code */
  352. /**
  353. * omap_device_get_context_loss_count - get lost context count
  354. * @od: struct omap_device *
  355. *
  356. * Using the primary hwmod, query the context loss count for this
  357. * device.
  358. *
  359. * Callers should consider context for this device lost any time this
  360. * function returns a value different than the value the caller got
  361. * the last time it called this function.
  362. *
  363. * If any hwmods exist for the omap_device assoiated with @pdev,
  364. * return the context loss counter for that hwmod, otherwise return
  365. * zero.
  366. */
  367. int omap_device_get_context_loss_count(struct platform_device *pdev)
  368. {
  369. struct omap_device *od;
  370. u32 ret = 0;
  371. od = to_omap_device(pdev);
  372. if (od->hwmods_cnt)
  373. ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
  374. return ret;
  375. }
  376. /**
  377. * omap_device_count_resources - count number of struct resource entries needed
  378. * @od: struct omap_device *
  379. *
  380. * Count the number of struct resource entries needed for this
  381. * omap_device @od. Used by omap_device_build_ss() to determine how
  382. * much memory to allocate before calling
  383. * omap_device_fill_resources(). Returns the count.
  384. */
  385. static int omap_device_count_resources(struct omap_device *od)
  386. {
  387. int c = 0;
  388. int i;
  389. for (i = 0; i < od->hwmods_cnt; i++)
  390. c += omap_hwmod_count_resources(od->hwmods[i]);
  391. pr_debug("omap_device: %s: counted %d total resources across %d "
  392. "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
  393. return c;
  394. }
  395. /**
  396. * omap_device_fill_resources - fill in array of struct resource
  397. * @od: struct omap_device *
  398. * @res: pointer to an array of struct resource to be filled in
  399. *
  400. * Populate one or more empty struct resource pointed to by @res with
  401. * the resource data for this omap_device @od. Used by
  402. * omap_device_build_ss() after calling omap_device_count_resources().
  403. * Ideally this function would not be needed at all. If omap_device
  404. * replaces platform_device, then we can specify our own
  405. * get_resource()/ get_irq()/etc functions that use the underlying
  406. * omap_hwmod information. Or if platform_device is extended to use
  407. * subarchitecture-specific function pointers, the various
  408. * platform_device functions can simply call omap_device internal
  409. * functions to get device resources. Hacking around the existing
  410. * platform_device code wastes memory. Returns 0.
  411. */
  412. static int omap_device_fill_resources(struct omap_device *od,
  413. struct resource *res)
  414. {
  415. int i, r;
  416. for (i = 0; i < od->hwmods_cnt; i++) {
  417. r = omap_hwmod_fill_resources(od->hwmods[i], res);
  418. res += r;
  419. }
  420. return 0;
  421. }
  422. /**
  423. * _od_fill_dma_resources - fill in array of struct resource with dma resources
  424. * @od: struct omap_device *
  425. * @res: pointer to an array of struct resource to be filled in
  426. *
  427. * Populate one or more empty struct resource pointed to by @res with
  428. * the dma resource data for this omap_device @od. Used by
  429. * omap_device_alloc() after calling omap_device_count_resources().
  430. *
  431. * Ideally this function would not be needed at all. If we have
  432. * mechanism to get dma resources from DT.
  433. *
  434. * Returns 0.
  435. */
  436. static int _od_fill_dma_resources(struct omap_device *od,
  437. struct resource *res)
  438. {
  439. int i, r;
  440. for (i = 0; i < od->hwmods_cnt; i++) {
  441. r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
  442. res += r;
  443. }
  444. return 0;
  445. }
  446. /**
  447. * omap_device_alloc - allocate an omap_device
  448. * @pdev: platform_device that will be included in this omap_device
  449. * @oh: ptr to the single omap_hwmod that backs this omap_device
  450. * @pdata: platform_data ptr to associate with the platform_device
  451. * @pdata_len: amount of memory pointed to by @pdata
  452. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  453. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  454. *
  455. * Convenience function for allocating an omap_device structure and filling
  456. * hwmods, resources and pm_latency attributes.
  457. *
  458. * Returns an struct omap_device pointer or ERR_PTR() on error;
  459. */
  460. struct omap_device *omap_device_alloc(struct platform_device *pdev,
  461. struct omap_hwmod **ohs, int oh_cnt,
  462. struct omap_device_pm_latency *pm_lats,
  463. int pm_lats_cnt)
  464. {
  465. int ret = -ENOMEM;
  466. struct omap_device *od;
  467. struct resource *res = NULL;
  468. int i, res_count;
  469. struct omap_hwmod **hwmods;
  470. od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
  471. if (!od) {
  472. ret = -ENOMEM;
  473. goto oda_exit1;
  474. }
  475. od->hwmods_cnt = oh_cnt;
  476. hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
  477. if (!hwmods)
  478. goto oda_exit2;
  479. od->hwmods = hwmods;
  480. od->pdev = pdev;
  481. res_count = omap_device_count_resources(od);
  482. /*
  483. * DT Boot:
  484. * OF framework will construct the resource structure (currently
  485. * does for MEM & IRQ resource) and we should respect/use these
  486. * resources, killing hwmod dependency.
  487. * If pdev->num_resources > 0, we assume that MEM & IRQ resources
  488. * have been allocated by OF layer already (through DTB).
  489. *
  490. * Non-DT Boot:
  491. * Here, pdev->num_resources = 0, and we should get all the
  492. * resources from hwmod.
  493. *
  494. * TODO: Once DMA resource is available from OF layer, we should
  495. * kill filling any resources from hwmod.
  496. */
  497. if (res_count > pdev->num_resources) {
  498. /* Allocate resources memory to account for new resources */
  499. res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
  500. if (!res)
  501. goto oda_exit3;
  502. /*
  503. * If pdev->num_resources > 0, then assume that,
  504. * MEM and IRQ resources will only come from DT and only
  505. * fill DMA resource from hwmod layer.
  506. */
  507. if (pdev->num_resources && pdev->resource) {
  508. dev_dbg(&pdev->dev, "%s(): resources already allocated %d\n",
  509. __func__, res_count);
  510. memcpy(res, pdev->resource,
  511. sizeof(struct resource) * pdev->num_resources);
  512. _od_fill_dma_resources(od, &res[pdev->num_resources]);
  513. } else {
  514. dev_dbg(&pdev->dev, "%s(): using resources from hwmod %d\n",
  515. __func__, res_count);
  516. omap_device_fill_resources(od, res);
  517. }
  518. ret = platform_device_add_resources(pdev, res, res_count);
  519. kfree(res);
  520. if (ret)
  521. goto oda_exit3;
  522. }
  523. if (!pm_lats) {
  524. pm_lats = omap_default_latency;
  525. pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
  526. }
  527. od->pm_lats_cnt = pm_lats_cnt;
  528. od->pm_lats = kmemdup(pm_lats,
  529. sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
  530. GFP_KERNEL);
  531. if (!od->pm_lats)
  532. goto oda_exit3;
  533. pdev->archdata.od = od;
  534. for (i = 0; i < oh_cnt; i++) {
  535. hwmods[i]->od = od;
  536. _add_hwmod_clocks_clkdev(od, hwmods[i]);
  537. }
  538. return od;
  539. oda_exit3:
  540. kfree(hwmods);
  541. oda_exit2:
  542. kfree(od);
  543. oda_exit1:
  544. dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
  545. return ERR_PTR(ret);
  546. }
  547. void omap_device_delete(struct omap_device *od)
  548. {
  549. if (!od)
  550. return;
  551. od->pdev->archdata.od = NULL;
  552. kfree(od->pm_lats);
  553. kfree(od->hwmods);
  554. kfree(od);
  555. }
  556. /**
  557. * omap_device_build - build and register an omap_device with one omap_hwmod
  558. * @pdev_name: name of the platform_device driver to use
  559. * @pdev_id: this platform_device's connection ID
  560. * @oh: ptr to the single omap_hwmod that backs this omap_device
  561. * @pdata: platform_data ptr to associate with the platform_device
  562. * @pdata_len: amount of memory pointed to by @pdata
  563. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  564. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  565. * @is_early_device: should the device be registered as an early device or not
  566. *
  567. * Convenience function for building and registering a single
  568. * omap_device record, which in turn builds and registers a
  569. * platform_device record. See omap_device_build_ss() for more
  570. * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
  571. * passes along the return value of omap_device_build_ss().
  572. */
  573. struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
  574. struct omap_hwmod *oh, void *pdata,
  575. int pdata_len,
  576. struct omap_device_pm_latency *pm_lats,
  577. int pm_lats_cnt, int is_early_device)
  578. {
  579. struct omap_hwmod *ohs[] = { oh };
  580. if (!oh)
  581. return ERR_PTR(-EINVAL);
  582. return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
  583. pdata_len, pm_lats, pm_lats_cnt,
  584. is_early_device);
  585. }
  586. /**
  587. * omap_device_build_ss - build and register an omap_device with multiple hwmods
  588. * @pdev_name: name of the platform_device driver to use
  589. * @pdev_id: this platform_device's connection ID
  590. * @oh: ptr to the single omap_hwmod that backs this omap_device
  591. * @pdata: platform_data ptr to associate with the platform_device
  592. * @pdata_len: amount of memory pointed to by @pdata
  593. * @pm_lats: pointer to a omap_device_pm_latency array for this device
  594. * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
  595. * @is_early_device: should the device be registered as an early device or not
  596. *
  597. * Convenience function for building and registering an omap_device
  598. * subsystem record. Subsystem records consist of multiple
  599. * omap_hwmods. This function in turn builds and registers a
  600. * platform_device record. Returns an ERR_PTR() on error, or passes
  601. * along the return value of omap_device_register().
  602. */
  603. struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
  604. struct omap_hwmod **ohs, int oh_cnt,
  605. void *pdata, int pdata_len,
  606. struct omap_device_pm_latency *pm_lats,
  607. int pm_lats_cnt, int is_early_device)
  608. {
  609. int ret = -ENOMEM;
  610. struct platform_device *pdev;
  611. struct omap_device *od;
  612. if (!ohs || oh_cnt == 0 || !pdev_name)
  613. return ERR_PTR(-EINVAL);
  614. if (!pdata && pdata_len > 0)
  615. return ERR_PTR(-EINVAL);
  616. pdev = platform_device_alloc(pdev_name, pdev_id);
  617. if (!pdev) {
  618. ret = -ENOMEM;
  619. goto odbs_exit;
  620. }
  621. /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
  622. if (pdev->id != -1)
  623. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  624. else
  625. dev_set_name(&pdev->dev, "%s", pdev->name);
  626. od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
  627. if (!od)
  628. goto odbs_exit1;
  629. ret = platform_device_add_data(pdev, pdata, pdata_len);
  630. if (ret)
  631. goto odbs_exit2;
  632. if (is_early_device)
  633. ret = omap_early_device_register(pdev);
  634. else
  635. ret = omap_device_register(pdev);
  636. if (ret)
  637. goto odbs_exit2;
  638. return pdev;
  639. odbs_exit2:
  640. omap_device_delete(od);
  641. odbs_exit1:
  642. platform_device_put(pdev);
  643. odbs_exit:
  644. pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
  645. return ERR_PTR(ret);
  646. }
  647. /**
  648. * omap_early_device_register - register an omap_device as an early platform
  649. * device.
  650. * @od: struct omap_device * to register
  651. *
  652. * Register the omap_device structure. This currently just calls
  653. * platform_early_add_device() on the underlying platform_device.
  654. * Returns 0 by default.
  655. */
  656. static int __init omap_early_device_register(struct platform_device *pdev)
  657. {
  658. struct platform_device *devices[1];
  659. devices[0] = pdev;
  660. early_platform_add_devices(devices, 1);
  661. return 0;
  662. }
  663. #ifdef CONFIG_PM_RUNTIME
  664. static int _od_runtime_suspend(struct device *dev)
  665. {
  666. struct platform_device *pdev = to_platform_device(dev);
  667. int ret;
  668. ret = pm_generic_runtime_suspend(dev);
  669. if (!ret)
  670. omap_device_idle(pdev);
  671. return ret;
  672. }
  673. static int _od_runtime_idle(struct device *dev)
  674. {
  675. return pm_generic_runtime_idle(dev);
  676. }
  677. static int _od_runtime_resume(struct device *dev)
  678. {
  679. struct platform_device *pdev = to_platform_device(dev);
  680. omap_device_enable(pdev);
  681. return pm_generic_runtime_resume(dev);
  682. }
  683. #endif
  684. #ifdef CONFIG_SUSPEND
  685. static int _od_suspend_noirq(struct device *dev)
  686. {
  687. struct platform_device *pdev = to_platform_device(dev);
  688. struct omap_device *od = to_omap_device(pdev);
  689. int ret;
  690. ret = pm_generic_suspend_noirq(dev);
  691. if (!ret && !pm_runtime_status_suspended(dev)) {
  692. if (pm_generic_runtime_suspend(dev) == 0) {
  693. if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
  694. omap_device_idle(pdev);
  695. od->flags |= OMAP_DEVICE_SUSPENDED;
  696. }
  697. }
  698. return ret;
  699. }
  700. static int _od_resume_noirq(struct device *dev)
  701. {
  702. struct platform_device *pdev = to_platform_device(dev);
  703. struct omap_device *od = to_omap_device(pdev);
  704. if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
  705. !pm_runtime_status_suspended(dev)) {
  706. od->flags &= ~OMAP_DEVICE_SUSPENDED;
  707. if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
  708. omap_device_enable(pdev);
  709. pm_generic_runtime_resume(dev);
  710. }
  711. return pm_generic_resume_noirq(dev);
  712. }
  713. #else
  714. #define _od_suspend_noirq NULL
  715. #define _od_resume_noirq NULL
  716. #endif
  717. struct dev_pm_domain omap_device_pm_domain = {
  718. .ops = {
  719. SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
  720. _od_runtime_idle)
  721. USE_PLATFORM_PM_SLEEP_OPS
  722. .suspend_noirq = _od_suspend_noirq,
  723. .resume_noirq = _od_resume_noirq,
  724. }
  725. };
  726. /**
  727. * omap_device_register - register an omap_device with one omap_hwmod
  728. * @od: struct omap_device * to register
  729. *
  730. * Register the omap_device structure. This currently just calls
  731. * platform_device_register() on the underlying platform_device.
  732. * Returns the return value of platform_device_register().
  733. */
  734. int omap_device_register(struct platform_device *pdev)
  735. {
  736. pr_debug("omap_device: %s: registering\n", pdev->name);
  737. pdev->dev.pm_domain = &omap_device_pm_domain;
  738. return platform_device_add(pdev);
  739. }
  740. /* Public functions for use by device drivers through struct platform_data */
  741. /**
  742. * omap_device_enable - fully activate an omap_device
  743. * @od: struct omap_device * to activate
  744. *
  745. * Do whatever is necessary for the hwmods underlying omap_device @od
  746. * to be accessible and ready to operate. This generally involves
  747. * enabling clocks, setting SYSCONFIG registers; and in the future may
  748. * involve remuxing pins. Device drivers should call this function
  749. * (through platform_data function pointers) where they would normally
  750. * enable clocks, etc. Returns -EINVAL if called when the omap_device
  751. * is already enabled, or passes along the return value of
  752. * _omap_device_activate().
  753. */
  754. int omap_device_enable(struct platform_device *pdev)
  755. {
  756. int ret;
  757. struct omap_device *od;
  758. od = to_omap_device(pdev);
  759. if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
  760. dev_warn(&pdev->dev,
  761. "omap_device: %s() called from invalid state %d\n",
  762. __func__, od->_state);
  763. return -EINVAL;
  764. }
  765. /* Enable everything if we're enabling this device from scratch */
  766. if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
  767. od->pm_lat_level = od->pm_lats_cnt;
  768. ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
  769. od->dev_wakeup_lat = 0;
  770. od->_dev_wakeup_lat_limit = UINT_MAX;
  771. od->_state = OMAP_DEVICE_STATE_ENABLED;
  772. return ret;
  773. }
  774. /**
  775. * omap_device_idle - idle an omap_device
  776. * @od: struct omap_device * to idle
  777. *
  778. * Idle omap_device @od by calling as many .deactivate_func() entries
  779. * in the omap_device's pm_lats table as is possible without exceeding
  780. * the device's maximum wakeup latency limit, pm_lat_limit. Device
  781. * drivers should call this function (through platform_data function
  782. * pointers) where they would normally disable clocks after operations
  783. * complete, etc.. Returns -EINVAL if the omap_device is not
  784. * currently enabled, or passes along the return value of
  785. * _omap_device_deactivate().
  786. */
  787. int omap_device_idle(struct platform_device *pdev)
  788. {
  789. int ret;
  790. struct omap_device *od;
  791. od = to_omap_device(pdev);
  792. if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
  793. dev_warn(&pdev->dev,
  794. "omap_device: %s() called from invalid state %d\n",
  795. __func__, od->_state);
  796. return -EINVAL;
  797. }
  798. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  799. od->_state = OMAP_DEVICE_STATE_IDLE;
  800. return ret;
  801. }
  802. /**
  803. * omap_device_shutdown - shut down an omap_device
  804. * @od: struct omap_device * to shut down
  805. *
  806. * Shut down omap_device @od by calling all .deactivate_func() entries
  807. * in the omap_device's pm_lats table and then shutting down all of
  808. * the underlying omap_hwmods. Used when a device is being "removed"
  809. * or a device driver is being unloaded. Returns -EINVAL if the
  810. * omap_device is not currently enabled or idle, or passes along the
  811. * return value of _omap_device_deactivate().
  812. */
  813. int omap_device_shutdown(struct platform_device *pdev)
  814. {
  815. int ret, i;
  816. struct omap_device *od;
  817. od = to_omap_device(pdev);
  818. if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
  819. od->_state != OMAP_DEVICE_STATE_IDLE) {
  820. dev_warn(&pdev->dev,
  821. "omap_device: %s() called from invalid state %d\n",
  822. __func__, od->_state);
  823. return -EINVAL;
  824. }
  825. ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
  826. for (i = 0; i < od->hwmods_cnt; i++)
  827. omap_hwmod_shutdown(od->hwmods[i]);
  828. od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
  829. return ret;
  830. }
  831. /**
  832. * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
  833. * @od: struct omap_device *
  834. *
  835. * When a device's maximum wakeup latency limit changes, call some of
  836. * the .activate_func or .deactivate_func function pointers in the
  837. * omap_device's pm_lats array to ensure that the device's maximum
  838. * wakeup latency is less than or equal to the new latency limit.
  839. * Intended to be called by OMAP PM code whenever a device's maximum
  840. * wakeup latency limit changes (e.g., via
  841. * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
  842. * done (e.g., if the omap_device is not currently idle, or if the
  843. * wakeup latency is already current with the new limit) or passes
  844. * along the return value of _omap_device_deactivate() or
  845. * _omap_device_activate().
  846. */
  847. int omap_device_align_pm_lat(struct platform_device *pdev,
  848. u32 new_wakeup_lat_limit)
  849. {
  850. int ret = -EINVAL;
  851. struct omap_device *od;
  852. od = to_omap_device(pdev);
  853. if (new_wakeup_lat_limit == od->dev_wakeup_lat)
  854. return 0;
  855. od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
  856. if (od->_state != OMAP_DEVICE_STATE_IDLE)
  857. return 0;
  858. else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
  859. ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
  860. else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
  861. ret = _omap_device_activate(od, USE_WAKEUP_LAT);
  862. return ret;
  863. }
  864. /**
  865. * omap_device_get_pwrdm - return the powerdomain * associated with @od
  866. * @od: struct omap_device *
  867. *
  868. * Return the powerdomain associated with the first underlying
  869. * omap_hwmod for this omap_device. Intended for use by core OMAP PM
  870. * code. Returns NULL on error or a struct powerdomain * upon
  871. * success.
  872. */
  873. struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
  874. {
  875. /*
  876. * XXX Assumes that all omap_hwmod powerdomains are identical.
  877. * This may not necessarily be true. There should be a sanity
  878. * check in here to WARN() if any difference appears.
  879. */
  880. if (!od->hwmods_cnt)
  881. return NULL;
  882. return omap_hwmod_get_pwrdm(od->hwmods[0]);
  883. }
  884. /**
  885. * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
  886. * @od: struct omap_device *
  887. *
  888. * Return the MPU's virtual address for the base of the hwmod, from
  889. * the ioremap() that the hwmod code does. Only valid if there is one
  890. * hwmod associated with this device. Returns NULL if there are zero
  891. * or more than one hwmods associated with this omap_device;
  892. * otherwise, passes along the return value from
  893. * omap_hwmod_get_mpu_rt_va().
  894. */
  895. void __iomem *omap_device_get_rt_va(struct omap_device *od)
  896. {
  897. if (od->hwmods_cnt != 1)
  898. return NULL;
  899. return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
  900. }
  901. /**
  902. * omap_device_get_by_hwmod_name() - convert a hwmod name to
  903. * device pointer.
  904. * @oh_name: name of the hwmod device
  905. *
  906. * Returns back a struct device * pointer associated with a hwmod
  907. * device represented by a hwmod_name
  908. */
  909. struct device *omap_device_get_by_hwmod_name(const char *oh_name)
  910. {
  911. struct omap_hwmod *oh;
  912. if (!oh_name) {
  913. WARN(1, "%s: no hwmod name!\n", __func__);
  914. return ERR_PTR(-EINVAL);
  915. }
  916. oh = omap_hwmod_lookup(oh_name);
  917. if (IS_ERR_OR_NULL(oh)) {
  918. WARN(1, "%s: no hwmod for %s\n", __func__,
  919. oh_name);
  920. return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
  921. }
  922. if (IS_ERR_OR_NULL(oh->od)) {
  923. WARN(1, "%s: no omap_device for %s\n", __func__,
  924. oh_name);
  925. return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
  926. }
  927. if (IS_ERR_OR_NULL(oh->od->pdev))
  928. return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
  929. return &oh->od->pdev->dev;
  930. }
  931. EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
  932. /*
  933. * Public functions intended for use in omap_device_pm_latency
  934. * .activate_func and .deactivate_func function pointers
  935. */
  936. /**
  937. * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
  938. * @od: struct omap_device *od
  939. *
  940. * Enable all underlying hwmods. Returns 0.
  941. */
  942. int omap_device_enable_hwmods(struct omap_device *od)
  943. {
  944. int i;
  945. for (i = 0; i < od->hwmods_cnt; i++)
  946. omap_hwmod_enable(od->hwmods[i]);
  947. /* XXX pass along return value here? */
  948. return 0;
  949. }
  950. /**
  951. * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
  952. * @od: struct omap_device *od
  953. *
  954. * Idle all underlying hwmods. Returns 0.
  955. */
  956. int omap_device_idle_hwmods(struct omap_device *od)
  957. {
  958. int i;
  959. for (i = 0; i < od->hwmods_cnt; i++)
  960. omap_hwmod_idle(od->hwmods[i]);
  961. /* XXX pass along return value here? */
  962. return 0;
  963. }
  964. /**
  965. * omap_device_disable_clocks - disable all main and interface clocks
  966. * @od: struct omap_device *od
  967. *
  968. * Disable the main functional clock and interface clock for all of the
  969. * omap_hwmods associated with the omap_device. Returns 0.
  970. */
  971. int omap_device_disable_clocks(struct omap_device *od)
  972. {
  973. int i;
  974. for (i = 0; i < od->hwmods_cnt; i++)
  975. omap_hwmod_disable_clocks(od->hwmods[i]);
  976. /* XXX pass along return value here? */
  977. return 0;
  978. }
  979. /**
  980. * omap_device_enable_clocks - enable all main and interface clocks
  981. * @od: struct omap_device *od
  982. *
  983. * Enable the main functional clock and interface clock for all of the
  984. * omap_hwmods associated with the omap_device. Returns 0.
  985. */
  986. int omap_device_enable_clocks(struct omap_device *od)
  987. {
  988. int i;
  989. for (i = 0; i < od->hwmods_cnt; i++)
  990. omap_hwmod_enable_clocks(od->hwmods[i]);
  991. /* XXX pass along return value here? */
  992. return 0;
  993. }
  994. static struct notifier_block platform_nb = {
  995. .notifier_call = _omap_device_notifier_call,
  996. };
  997. static int __init omap_device_init(void)
  998. {
  999. bus_register_notifier(&platform_bus_type, &platform_nb);
  1000. return 0;
  1001. }
  1002. core_initcall(omap_device_init);