omap_device.c 35 KB

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