omap_device.c 31 KB

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