omap_device.c 35 KB

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