omap_device.c 31 KB

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