core.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. /*
  2. * core.c -- Voltage/Current Regulator framework.
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. * Copyright 2008 SlimLogic Ltd.
  6. *
  7. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/mutex.h>
  20. #include <linux/suspend.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #define REGULATOR_VERSION "0.5"
  25. static DEFINE_MUTEX(regulator_list_mutex);
  26. static LIST_HEAD(regulator_list);
  27. static LIST_HEAD(regulator_map_list);
  28. /*
  29. * struct regulator_map
  30. *
  31. * Used to provide symbolic supply names to devices.
  32. */
  33. struct regulator_map {
  34. struct list_head list;
  35. struct device *dev;
  36. const char *supply;
  37. struct regulator_dev *regulator;
  38. };
  39. /*
  40. * struct regulator
  41. *
  42. * One for each consumer device.
  43. */
  44. struct regulator {
  45. struct device *dev;
  46. struct list_head list;
  47. int uA_load;
  48. int min_uV;
  49. int max_uV;
  50. int enabled; /* count of client enables */
  51. char *supply_name;
  52. struct device_attribute dev_attr;
  53. struct regulator_dev *rdev;
  54. };
  55. static int _regulator_is_enabled(struct regulator_dev *rdev);
  56. static int _regulator_disable(struct regulator_dev *rdev);
  57. static int _regulator_get_voltage(struct regulator_dev *rdev);
  58. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  59. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  60. static void _notifier_call_chain(struct regulator_dev *rdev,
  61. unsigned long event, void *data);
  62. /* gets the regulator for a given consumer device */
  63. static struct regulator *get_device_regulator(struct device *dev)
  64. {
  65. struct regulator *regulator = NULL;
  66. struct regulator_dev *rdev;
  67. mutex_lock(&regulator_list_mutex);
  68. list_for_each_entry(rdev, &regulator_list, list) {
  69. mutex_lock(&rdev->mutex);
  70. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  71. if (regulator->dev == dev) {
  72. mutex_unlock(&rdev->mutex);
  73. mutex_unlock(&regulator_list_mutex);
  74. return regulator;
  75. }
  76. }
  77. mutex_unlock(&rdev->mutex);
  78. }
  79. mutex_unlock(&regulator_list_mutex);
  80. return NULL;
  81. }
  82. /* Platform voltage constraint check */
  83. static int regulator_check_voltage(struct regulator_dev *rdev,
  84. int *min_uV, int *max_uV)
  85. {
  86. BUG_ON(*min_uV > *max_uV);
  87. if (!rdev->constraints) {
  88. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  89. rdev->desc->name);
  90. return -ENODEV;
  91. }
  92. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  93. printk(KERN_ERR "%s: operation not allowed for %s\n",
  94. __func__, rdev->desc->name);
  95. return -EPERM;
  96. }
  97. if (*max_uV > rdev->constraints->max_uV)
  98. *max_uV = rdev->constraints->max_uV;
  99. if (*min_uV < rdev->constraints->min_uV)
  100. *min_uV = rdev->constraints->min_uV;
  101. if (*min_uV > *max_uV)
  102. return -EINVAL;
  103. return 0;
  104. }
  105. /* current constraint check */
  106. static int regulator_check_current_limit(struct regulator_dev *rdev,
  107. int *min_uA, int *max_uA)
  108. {
  109. BUG_ON(*min_uA > *max_uA);
  110. if (!rdev->constraints) {
  111. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  112. rdev->desc->name);
  113. return -ENODEV;
  114. }
  115. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  116. printk(KERN_ERR "%s: operation not allowed for %s\n",
  117. __func__, rdev->desc->name);
  118. return -EPERM;
  119. }
  120. if (*max_uA > rdev->constraints->max_uA)
  121. *max_uA = rdev->constraints->max_uA;
  122. if (*min_uA < rdev->constraints->min_uA)
  123. *min_uA = rdev->constraints->min_uA;
  124. if (*min_uA > *max_uA)
  125. return -EINVAL;
  126. return 0;
  127. }
  128. /* operating mode constraint check */
  129. static int regulator_check_mode(struct regulator_dev *rdev, int mode)
  130. {
  131. switch (mode) {
  132. case REGULATOR_MODE_FAST:
  133. case REGULATOR_MODE_NORMAL:
  134. case REGULATOR_MODE_IDLE:
  135. case REGULATOR_MODE_STANDBY:
  136. break;
  137. default:
  138. return -EINVAL;
  139. }
  140. if (!rdev->constraints) {
  141. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  142. rdev->desc->name);
  143. return -ENODEV;
  144. }
  145. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  146. printk(KERN_ERR "%s: operation not allowed for %s\n",
  147. __func__, rdev->desc->name);
  148. return -EPERM;
  149. }
  150. if (!(rdev->constraints->valid_modes_mask & mode)) {
  151. printk(KERN_ERR "%s: invalid mode %x for %s\n",
  152. __func__, mode, rdev->desc->name);
  153. return -EINVAL;
  154. }
  155. return 0;
  156. }
  157. /* dynamic regulator mode switching constraint check */
  158. static int regulator_check_drms(struct regulator_dev *rdev)
  159. {
  160. if (!rdev->constraints) {
  161. printk(KERN_ERR "%s: no constraints for %s\n", __func__,
  162. rdev->desc->name);
  163. return -ENODEV;
  164. }
  165. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  166. printk(KERN_ERR "%s: operation not allowed for %s\n",
  167. __func__, rdev->desc->name);
  168. return -EPERM;
  169. }
  170. return 0;
  171. }
  172. static ssize_t device_requested_uA_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. struct regulator *regulator;
  176. regulator = get_device_regulator(dev);
  177. if (regulator == NULL)
  178. return 0;
  179. return sprintf(buf, "%d\n", regulator->uA_load);
  180. }
  181. static ssize_t regulator_uV_show(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. struct regulator_dev *rdev = dev_get_drvdata(dev);
  185. ssize_t ret;
  186. mutex_lock(&rdev->mutex);
  187. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  188. mutex_unlock(&rdev->mutex);
  189. return ret;
  190. }
  191. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  192. static ssize_t regulator_uA_show(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. struct regulator_dev *rdev = dev_get_drvdata(dev);
  196. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  197. }
  198. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  199. static ssize_t regulator_name_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct regulator_dev *rdev = dev_get_drvdata(dev);
  203. const char *name;
  204. if (rdev->constraints->name)
  205. name = rdev->constraints->name;
  206. else if (rdev->desc->name)
  207. name = rdev->desc->name;
  208. else
  209. name = "";
  210. return sprintf(buf, "%s\n", name);
  211. }
  212. static ssize_t regulator_print_opmode(char *buf, int mode)
  213. {
  214. switch (mode) {
  215. case REGULATOR_MODE_FAST:
  216. return sprintf(buf, "fast\n");
  217. case REGULATOR_MODE_NORMAL:
  218. return sprintf(buf, "normal\n");
  219. case REGULATOR_MODE_IDLE:
  220. return sprintf(buf, "idle\n");
  221. case REGULATOR_MODE_STANDBY:
  222. return sprintf(buf, "standby\n");
  223. }
  224. return sprintf(buf, "unknown\n");
  225. }
  226. static ssize_t regulator_opmode_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct regulator_dev *rdev = dev_get_drvdata(dev);
  230. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  231. }
  232. static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
  233. static ssize_t regulator_print_state(char *buf, int state)
  234. {
  235. if (state > 0)
  236. return sprintf(buf, "enabled\n");
  237. else if (state == 0)
  238. return sprintf(buf, "disabled\n");
  239. else
  240. return sprintf(buf, "unknown\n");
  241. }
  242. static ssize_t regulator_state_show(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct regulator_dev *rdev = dev_get_drvdata(dev);
  246. return regulator_print_state(buf, _regulator_is_enabled(rdev));
  247. }
  248. static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
  249. static ssize_t regulator_status_show(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct regulator_dev *rdev = dev_get_drvdata(dev);
  253. int status;
  254. char *label;
  255. status = rdev->desc->ops->get_status(rdev);
  256. if (status < 0)
  257. return status;
  258. switch (status) {
  259. case REGULATOR_STATUS_OFF:
  260. label = "off";
  261. break;
  262. case REGULATOR_STATUS_ON:
  263. label = "on";
  264. break;
  265. case REGULATOR_STATUS_ERROR:
  266. label = "error";
  267. break;
  268. case REGULATOR_STATUS_FAST:
  269. label = "fast";
  270. break;
  271. case REGULATOR_STATUS_NORMAL:
  272. label = "normal";
  273. break;
  274. case REGULATOR_STATUS_IDLE:
  275. label = "idle";
  276. break;
  277. case REGULATOR_STATUS_STANDBY:
  278. label = "standby";
  279. break;
  280. default:
  281. return -ERANGE;
  282. }
  283. return sprintf(buf, "%s\n", label);
  284. }
  285. static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
  286. static ssize_t regulator_min_uA_show(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. struct regulator_dev *rdev = dev_get_drvdata(dev);
  290. if (!rdev->constraints)
  291. return sprintf(buf, "constraint not defined\n");
  292. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  293. }
  294. static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
  295. static ssize_t regulator_max_uA_show(struct device *dev,
  296. struct device_attribute *attr, char *buf)
  297. {
  298. struct regulator_dev *rdev = dev_get_drvdata(dev);
  299. if (!rdev->constraints)
  300. return sprintf(buf, "constraint not defined\n");
  301. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  302. }
  303. static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
  304. static ssize_t regulator_min_uV_show(struct device *dev,
  305. struct device_attribute *attr, char *buf)
  306. {
  307. struct regulator_dev *rdev = dev_get_drvdata(dev);
  308. if (!rdev->constraints)
  309. return sprintf(buf, "constraint not defined\n");
  310. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  311. }
  312. static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
  313. static ssize_t regulator_max_uV_show(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. struct regulator_dev *rdev = dev_get_drvdata(dev);
  317. if (!rdev->constraints)
  318. return sprintf(buf, "constraint not defined\n");
  319. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  320. }
  321. static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
  322. static ssize_t regulator_total_uA_show(struct device *dev,
  323. struct device_attribute *attr, char *buf)
  324. {
  325. struct regulator_dev *rdev = dev_get_drvdata(dev);
  326. struct regulator *regulator;
  327. int uA = 0;
  328. mutex_lock(&rdev->mutex);
  329. list_for_each_entry(regulator, &rdev->consumer_list, list)
  330. uA += regulator->uA_load;
  331. mutex_unlock(&rdev->mutex);
  332. return sprintf(buf, "%d\n", uA);
  333. }
  334. static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
  335. static ssize_t regulator_num_users_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. struct regulator_dev *rdev = dev_get_drvdata(dev);
  339. return sprintf(buf, "%d\n", rdev->use_count);
  340. }
  341. static ssize_t regulator_type_show(struct device *dev,
  342. struct device_attribute *attr, char *buf)
  343. {
  344. struct regulator_dev *rdev = dev_get_drvdata(dev);
  345. switch (rdev->desc->type) {
  346. case REGULATOR_VOLTAGE:
  347. return sprintf(buf, "voltage\n");
  348. case REGULATOR_CURRENT:
  349. return sprintf(buf, "current\n");
  350. }
  351. return sprintf(buf, "unknown\n");
  352. }
  353. static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
  354. struct device_attribute *attr, char *buf)
  355. {
  356. struct regulator_dev *rdev = dev_get_drvdata(dev);
  357. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  358. }
  359. static DEVICE_ATTR(suspend_mem_microvolts, 0444,
  360. regulator_suspend_mem_uV_show, NULL);
  361. static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
  362. struct device_attribute *attr, char *buf)
  363. {
  364. struct regulator_dev *rdev = dev_get_drvdata(dev);
  365. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  366. }
  367. static DEVICE_ATTR(suspend_disk_microvolts, 0444,
  368. regulator_suspend_disk_uV_show, NULL);
  369. static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
  370. struct device_attribute *attr, char *buf)
  371. {
  372. struct regulator_dev *rdev = dev_get_drvdata(dev);
  373. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  374. }
  375. static DEVICE_ATTR(suspend_standby_microvolts, 0444,
  376. regulator_suspend_standby_uV_show, NULL);
  377. static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. struct regulator_dev *rdev = dev_get_drvdata(dev);
  381. return regulator_print_opmode(buf,
  382. rdev->constraints->state_mem.mode);
  383. }
  384. static DEVICE_ATTR(suspend_mem_mode, 0444,
  385. regulator_suspend_mem_mode_show, NULL);
  386. static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
  387. struct device_attribute *attr, char *buf)
  388. {
  389. struct regulator_dev *rdev = dev_get_drvdata(dev);
  390. return regulator_print_opmode(buf,
  391. rdev->constraints->state_disk.mode);
  392. }
  393. static DEVICE_ATTR(suspend_disk_mode, 0444,
  394. regulator_suspend_disk_mode_show, NULL);
  395. static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
  396. struct device_attribute *attr, char *buf)
  397. {
  398. struct regulator_dev *rdev = dev_get_drvdata(dev);
  399. return regulator_print_opmode(buf,
  400. rdev->constraints->state_standby.mode);
  401. }
  402. static DEVICE_ATTR(suspend_standby_mode, 0444,
  403. regulator_suspend_standby_mode_show, NULL);
  404. static ssize_t regulator_suspend_mem_state_show(struct device *dev,
  405. struct device_attribute *attr, char *buf)
  406. {
  407. struct regulator_dev *rdev = dev_get_drvdata(dev);
  408. return regulator_print_state(buf,
  409. rdev->constraints->state_mem.enabled);
  410. }
  411. static DEVICE_ATTR(suspend_mem_state, 0444,
  412. regulator_suspend_mem_state_show, NULL);
  413. static ssize_t regulator_suspend_disk_state_show(struct device *dev,
  414. struct device_attribute *attr, char *buf)
  415. {
  416. struct regulator_dev *rdev = dev_get_drvdata(dev);
  417. return regulator_print_state(buf,
  418. rdev->constraints->state_disk.enabled);
  419. }
  420. static DEVICE_ATTR(suspend_disk_state, 0444,
  421. regulator_suspend_disk_state_show, NULL);
  422. static ssize_t regulator_suspend_standby_state_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct regulator_dev *rdev = dev_get_drvdata(dev);
  426. return regulator_print_state(buf,
  427. rdev->constraints->state_standby.enabled);
  428. }
  429. static DEVICE_ATTR(suspend_standby_state, 0444,
  430. regulator_suspend_standby_state_show, NULL);
  431. /*
  432. * These are the only attributes are present for all regulators.
  433. * Other attributes are a function of regulator functionality.
  434. */
  435. static struct device_attribute regulator_dev_attrs[] = {
  436. __ATTR(name, 0444, regulator_name_show, NULL),
  437. __ATTR(num_users, 0444, regulator_num_users_show, NULL),
  438. __ATTR(type, 0444, regulator_type_show, NULL),
  439. __ATTR_NULL,
  440. };
  441. static void regulator_dev_release(struct device *dev)
  442. {
  443. struct regulator_dev *rdev = dev_get_drvdata(dev);
  444. kfree(rdev);
  445. }
  446. static struct class regulator_class = {
  447. .name = "regulator",
  448. .dev_release = regulator_dev_release,
  449. .dev_attrs = regulator_dev_attrs,
  450. };
  451. /* Calculate the new optimum regulator operating mode based on the new total
  452. * consumer load. All locks held by caller */
  453. static void drms_uA_update(struct regulator_dev *rdev)
  454. {
  455. struct regulator *sibling;
  456. int current_uA = 0, output_uV, input_uV, err;
  457. unsigned int mode;
  458. err = regulator_check_drms(rdev);
  459. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  460. !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
  461. return;
  462. /* get output voltage */
  463. output_uV = rdev->desc->ops->get_voltage(rdev);
  464. if (output_uV <= 0)
  465. return;
  466. /* get input voltage */
  467. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  468. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  469. else
  470. input_uV = rdev->constraints->input_uV;
  471. if (input_uV <= 0)
  472. return;
  473. /* calc total requested load */
  474. list_for_each_entry(sibling, &rdev->consumer_list, list)
  475. current_uA += sibling->uA_load;
  476. /* now get the optimum mode for our new total regulator load */
  477. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  478. output_uV, current_uA);
  479. /* check the new mode is allowed */
  480. err = regulator_check_mode(rdev, mode);
  481. if (err == 0)
  482. rdev->desc->ops->set_mode(rdev, mode);
  483. }
  484. static int suspend_set_state(struct regulator_dev *rdev,
  485. struct regulator_state *rstate)
  486. {
  487. int ret = 0;
  488. /* enable & disable are mandatory for suspend control */
  489. if (!rdev->desc->ops->set_suspend_enable ||
  490. !rdev->desc->ops->set_suspend_disable) {
  491. printk(KERN_ERR "%s: no way to set suspend state\n",
  492. __func__);
  493. return -EINVAL;
  494. }
  495. if (rstate->enabled)
  496. ret = rdev->desc->ops->set_suspend_enable(rdev);
  497. else
  498. ret = rdev->desc->ops->set_suspend_disable(rdev);
  499. if (ret < 0) {
  500. printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
  501. return ret;
  502. }
  503. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  504. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  505. if (ret < 0) {
  506. printk(KERN_ERR "%s: failed to set voltage\n",
  507. __func__);
  508. return ret;
  509. }
  510. }
  511. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  512. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  513. if (ret < 0) {
  514. printk(KERN_ERR "%s: failed to set mode\n", __func__);
  515. return ret;
  516. }
  517. }
  518. return ret;
  519. }
  520. /* locks held by caller */
  521. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  522. {
  523. if (!rdev->constraints)
  524. return -EINVAL;
  525. switch (state) {
  526. case PM_SUSPEND_STANDBY:
  527. return suspend_set_state(rdev,
  528. &rdev->constraints->state_standby);
  529. case PM_SUSPEND_MEM:
  530. return suspend_set_state(rdev,
  531. &rdev->constraints->state_mem);
  532. case PM_SUSPEND_MAX:
  533. return suspend_set_state(rdev,
  534. &rdev->constraints->state_disk);
  535. default:
  536. return -EINVAL;
  537. }
  538. }
  539. static void print_constraints(struct regulator_dev *rdev)
  540. {
  541. struct regulation_constraints *constraints = rdev->constraints;
  542. char buf[80];
  543. int count;
  544. if (rdev->desc->type == REGULATOR_VOLTAGE) {
  545. if (constraints->min_uV == constraints->max_uV)
  546. count = sprintf(buf, "%d mV ",
  547. constraints->min_uV / 1000);
  548. else
  549. count = sprintf(buf, "%d <--> %d mV ",
  550. constraints->min_uV / 1000,
  551. constraints->max_uV / 1000);
  552. } else {
  553. if (constraints->min_uA == constraints->max_uA)
  554. count = sprintf(buf, "%d mA ",
  555. constraints->min_uA / 1000);
  556. else
  557. count = sprintf(buf, "%d <--> %d mA ",
  558. constraints->min_uA / 1000,
  559. constraints->max_uA / 1000);
  560. }
  561. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  562. count += sprintf(buf + count, "fast ");
  563. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  564. count += sprintf(buf + count, "normal ");
  565. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  566. count += sprintf(buf + count, "idle ");
  567. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  568. count += sprintf(buf + count, "standby");
  569. printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
  570. }
  571. /**
  572. * set_machine_constraints - sets regulator constraints
  573. * @rdev: regulator source
  574. * @constraints: constraints to apply
  575. *
  576. * Allows platform initialisation code to define and constrain
  577. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  578. * Constraints *must* be set by platform code in order for some
  579. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  580. * set_mode.
  581. */
  582. static int set_machine_constraints(struct regulator_dev *rdev,
  583. struct regulation_constraints *constraints)
  584. {
  585. int ret = 0;
  586. const char *name;
  587. struct regulator_ops *ops = rdev->desc->ops;
  588. if (constraints->name)
  589. name = constraints->name;
  590. else if (rdev->desc->name)
  591. name = rdev->desc->name;
  592. else
  593. name = "regulator";
  594. /* constrain machine-level voltage specs to fit
  595. * the actual range supported by this regulator.
  596. */
  597. if (ops->list_voltage && rdev->desc->n_voltages) {
  598. int count = rdev->desc->n_voltages;
  599. int i;
  600. int min_uV = INT_MAX;
  601. int max_uV = INT_MIN;
  602. int cmin = constraints->min_uV;
  603. int cmax = constraints->max_uV;
  604. /* it's safe to autoconfigure fixed-voltage supplies */
  605. if (count == 1 && !cmin) {
  606. cmin = INT_MIN;
  607. cmax = INT_MAX;
  608. }
  609. /* voltage constraints are optional */
  610. if ((cmin == 0) && (cmax == 0))
  611. goto out;
  612. /* else require explicit machine-level constraints */
  613. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  614. pr_err("%s: %s '%s' voltage constraints\n",
  615. __func__, "invalid", name);
  616. ret = -EINVAL;
  617. goto out;
  618. }
  619. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  620. for (i = 0; i < count; i++) {
  621. int value;
  622. value = ops->list_voltage(rdev, i);
  623. if (value <= 0)
  624. continue;
  625. /* maybe adjust [min_uV..max_uV] */
  626. if (value >= cmin && value < min_uV)
  627. min_uV = value;
  628. if (value <= cmax && value > max_uV)
  629. max_uV = value;
  630. }
  631. /* final: [min_uV..max_uV] valid iff constraints valid */
  632. if (max_uV < min_uV) {
  633. pr_err("%s: %s '%s' voltage constraints\n",
  634. __func__, "unsupportable", name);
  635. ret = -EINVAL;
  636. goto out;
  637. }
  638. /* use regulator's subset of machine constraints */
  639. if (constraints->min_uV < min_uV) {
  640. pr_debug("%s: override '%s' %s, %d -> %d\n",
  641. __func__, name, "min_uV",
  642. constraints->min_uV, min_uV);
  643. constraints->min_uV = min_uV;
  644. }
  645. if (constraints->max_uV > max_uV) {
  646. pr_debug("%s: override '%s' %s, %d -> %d\n",
  647. __func__, name, "max_uV",
  648. constraints->max_uV, max_uV);
  649. constraints->max_uV = max_uV;
  650. }
  651. }
  652. rdev->constraints = constraints;
  653. /* do we need to apply the constraint voltage */
  654. if (rdev->constraints->apply_uV &&
  655. rdev->constraints->min_uV == rdev->constraints->max_uV &&
  656. ops->set_voltage) {
  657. ret = ops->set_voltage(rdev,
  658. rdev->constraints->min_uV, rdev->constraints->max_uV);
  659. if (ret < 0) {
  660. printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
  661. __func__,
  662. rdev->constraints->min_uV, name);
  663. rdev->constraints = NULL;
  664. goto out;
  665. }
  666. }
  667. /* are we enabled at boot time by firmware / bootloader */
  668. if (rdev->constraints->boot_on)
  669. rdev->use_count = 1;
  670. /* do we need to setup our suspend state */
  671. if (constraints->initial_state) {
  672. ret = suspend_prepare(rdev, constraints->initial_state);
  673. if (ret < 0) {
  674. printk(KERN_ERR "%s: failed to set suspend state for %s\n",
  675. __func__, name);
  676. rdev->constraints = NULL;
  677. goto out;
  678. }
  679. }
  680. if (constraints->initial_mode) {
  681. if (!ops->set_mode) {
  682. printk(KERN_ERR "%s: no set_mode operation for %s\n",
  683. __func__, name);
  684. ret = -EINVAL;
  685. goto out;
  686. }
  687. ret = ops->set_mode(rdev, constraints->initial_mode);
  688. if (ret < 0) {
  689. printk(KERN_ERR
  690. "%s: failed to set initial mode for %s: %d\n",
  691. __func__, name, ret);
  692. goto out;
  693. }
  694. }
  695. /* if always_on is set then turn the regulator on if it's not
  696. * already on. */
  697. if (constraints->always_on && ops->enable &&
  698. ((ops->is_enabled && !ops->is_enabled(rdev)) ||
  699. (!ops->is_enabled && !constraints->boot_on))) {
  700. ret = ops->enable(rdev);
  701. if (ret < 0) {
  702. printk(KERN_ERR "%s: failed to enable %s\n",
  703. __func__, name);
  704. rdev->constraints = NULL;
  705. goto out;
  706. }
  707. }
  708. print_constraints(rdev);
  709. out:
  710. return ret;
  711. }
  712. /**
  713. * set_supply - set regulator supply regulator
  714. * @rdev: regulator name
  715. * @supply_rdev: supply regulator name
  716. *
  717. * Called by platform initialisation code to set the supply regulator for this
  718. * regulator. This ensures that a regulators supply will also be enabled by the
  719. * core if it's child is enabled.
  720. */
  721. static int set_supply(struct regulator_dev *rdev,
  722. struct regulator_dev *supply_rdev)
  723. {
  724. int err;
  725. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  726. "supply");
  727. if (err) {
  728. printk(KERN_ERR
  729. "%s: could not add device link %s err %d\n",
  730. __func__, supply_rdev->dev.kobj.name, err);
  731. goto out;
  732. }
  733. rdev->supply = supply_rdev;
  734. list_add(&rdev->slist, &supply_rdev->supply_list);
  735. out:
  736. return err;
  737. }
  738. /**
  739. * set_consumer_device_supply: Bind a regulator to a symbolic supply
  740. * @rdev: regulator source
  741. * @consumer_dev: device the supply applies to
  742. * @supply: symbolic name for supply
  743. *
  744. * Allows platform initialisation code to map physical regulator
  745. * sources to symbolic names for supplies for use by devices. Devices
  746. * should use these symbolic names to request regulators, avoiding the
  747. * need to provide board-specific regulator names as platform data.
  748. */
  749. static int set_consumer_device_supply(struct regulator_dev *rdev,
  750. struct device *consumer_dev, const char *supply)
  751. {
  752. struct regulator_map *node;
  753. if (supply == NULL)
  754. return -EINVAL;
  755. list_for_each_entry(node, &regulator_map_list, list) {
  756. if (consumer_dev != node->dev)
  757. continue;
  758. if (strcmp(node->supply, supply) != 0)
  759. continue;
  760. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  761. dev_name(&node->regulator->dev),
  762. node->regulator->desc->name,
  763. supply,
  764. dev_name(&rdev->dev), rdev->desc->name);
  765. return -EBUSY;
  766. }
  767. node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
  768. if (node == NULL)
  769. return -ENOMEM;
  770. node->regulator = rdev;
  771. node->dev = consumer_dev;
  772. node->supply = supply;
  773. list_add(&node->list, &regulator_map_list);
  774. return 0;
  775. }
  776. static void unset_consumer_device_supply(struct regulator_dev *rdev,
  777. struct device *consumer_dev)
  778. {
  779. struct regulator_map *node, *n;
  780. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  781. if (rdev == node->regulator &&
  782. consumer_dev == node->dev) {
  783. list_del(&node->list);
  784. kfree(node);
  785. return;
  786. }
  787. }
  788. }
  789. static void unset_regulator_supplies(struct regulator_dev *rdev)
  790. {
  791. struct regulator_map *node, *n;
  792. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  793. if (rdev == node->regulator) {
  794. list_del(&node->list);
  795. kfree(node);
  796. return;
  797. }
  798. }
  799. }
  800. #define REG_STR_SIZE 32
  801. static struct regulator *create_regulator(struct regulator_dev *rdev,
  802. struct device *dev,
  803. const char *supply_name)
  804. {
  805. struct regulator *regulator;
  806. char buf[REG_STR_SIZE];
  807. int err, size;
  808. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  809. if (regulator == NULL)
  810. return NULL;
  811. mutex_lock(&rdev->mutex);
  812. regulator->rdev = rdev;
  813. list_add(&regulator->list, &rdev->consumer_list);
  814. if (dev) {
  815. /* create a 'requested_microamps_name' sysfs entry */
  816. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  817. supply_name);
  818. if (size >= REG_STR_SIZE)
  819. goto overflow_err;
  820. regulator->dev = dev;
  821. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  822. if (regulator->dev_attr.attr.name == NULL)
  823. goto attr_name_err;
  824. regulator->dev_attr.attr.owner = THIS_MODULE;
  825. regulator->dev_attr.attr.mode = 0444;
  826. regulator->dev_attr.show = device_requested_uA_show;
  827. err = device_create_file(dev, &regulator->dev_attr);
  828. if (err < 0) {
  829. printk(KERN_WARNING "%s: could not add regulator_dev"
  830. " load sysfs\n", __func__);
  831. goto attr_name_err;
  832. }
  833. /* also add a link to the device sysfs entry */
  834. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  835. dev->kobj.name, supply_name);
  836. if (size >= REG_STR_SIZE)
  837. goto attr_err;
  838. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  839. if (regulator->supply_name == NULL)
  840. goto attr_err;
  841. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  842. buf);
  843. if (err) {
  844. printk(KERN_WARNING
  845. "%s: could not add device link %s err %d\n",
  846. __func__, dev->kobj.name, err);
  847. device_remove_file(dev, &regulator->dev_attr);
  848. goto link_name_err;
  849. }
  850. }
  851. mutex_unlock(&rdev->mutex);
  852. return regulator;
  853. link_name_err:
  854. kfree(regulator->supply_name);
  855. attr_err:
  856. device_remove_file(regulator->dev, &regulator->dev_attr);
  857. attr_name_err:
  858. kfree(regulator->dev_attr.attr.name);
  859. overflow_err:
  860. list_del(&regulator->list);
  861. kfree(regulator);
  862. mutex_unlock(&rdev->mutex);
  863. return NULL;
  864. }
  865. /**
  866. * regulator_get - lookup and obtain a reference to a regulator.
  867. * @dev: device for regulator "consumer"
  868. * @id: Supply name or regulator ID.
  869. *
  870. * Returns a struct regulator corresponding to the regulator producer,
  871. * or IS_ERR() condition containing errno.
  872. *
  873. * Use of supply names configured via regulator_set_device_supply() is
  874. * strongly encouraged. It is recommended that the supply name used
  875. * should match the name used for the supply and/or the relevant
  876. * device pins in the datasheet.
  877. */
  878. struct regulator *regulator_get(struct device *dev, const char *id)
  879. {
  880. struct regulator_dev *rdev;
  881. struct regulator_map *map;
  882. struct regulator *regulator = ERR_PTR(-ENODEV);
  883. if (id == NULL) {
  884. printk(KERN_ERR "regulator: get() with no identifier\n");
  885. return regulator;
  886. }
  887. mutex_lock(&regulator_list_mutex);
  888. list_for_each_entry(map, &regulator_map_list, list) {
  889. if (dev == map->dev &&
  890. strcmp(map->supply, id) == 0) {
  891. rdev = map->regulator;
  892. goto found;
  893. }
  894. }
  895. printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
  896. id);
  897. mutex_unlock(&regulator_list_mutex);
  898. return regulator;
  899. found:
  900. if (!try_module_get(rdev->owner))
  901. goto out;
  902. regulator = create_regulator(rdev, dev, id);
  903. if (regulator == NULL) {
  904. regulator = ERR_PTR(-ENOMEM);
  905. module_put(rdev->owner);
  906. }
  907. out:
  908. mutex_unlock(&regulator_list_mutex);
  909. return regulator;
  910. }
  911. EXPORT_SYMBOL_GPL(regulator_get);
  912. /**
  913. * regulator_put - "free" the regulator source
  914. * @regulator: regulator source
  915. *
  916. * Note: drivers must ensure that all regulator_enable calls made on this
  917. * regulator source are balanced by regulator_disable calls prior to calling
  918. * this function.
  919. */
  920. void regulator_put(struct regulator *regulator)
  921. {
  922. struct regulator_dev *rdev;
  923. if (regulator == NULL || IS_ERR(regulator))
  924. return;
  925. mutex_lock(&regulator_list_mutex);
  926. rdev = regulator->rdev;
  927. if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
  928. regulator->supply_name))
  929. _regulator_disable(rdev);
  930. /* remove any sysfs entries */
  931. if (regulator->dev) {
  932. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  933. kfree(regulator->supply_name);
  934. device_remove_file(regulator->dev, &regulator->dev_attr);
  935. kfree(regulator->dev_attr.attr.name);
  936. }
  937. list_del(&regulator->list);
  938. kfree(regulator);
  939. module_put(rdev->owner);
  940. mutex_unlock(&regulator_list_mutex);
  941. }
  942. EXPORT_SYMBOL_GPL(regulator_put);
  943. /* locks held by regulator_enable() */
  944. static int _regulator_enable(struct regulator_dev *rdev)
  945. {
  946. int ret = -EINVAL;
  947. if (!rdev->constraints) {
  948. printk(KERN_ERR "%s: %s has no constraints\n",
  949. __func__, rdev->desc->name);
  950. return ret;
  951. }
  952. /* do we need to enable the supply regulator first */
  953. if (rdev->supply) {
  954. ret = _regulator_enable(rdev->supply);
  955. if (ret < 0) {
  956. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  957. __func__, rdev->desc->name, ret);
  958. return ret;
  959. }
  960. }
  961. /* check voltage and requested load before enabling */
  962. if (rdev->desc->ops->enable) {
  963. if (rdev->constraints &&
  964. (rdev->constraints->valid_ops_mask &
  965. REGULATOR_CHANGE_DRMS))
  966. drms_uA_update(rdev);
  967. ret = rdev->desc->ops->enable(rdev);
  968. if (ret < 0) {
  969. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  970. __func__, rdev->desc->name, ret);
  971. return ret;
  972. }
  973. rdev->use_count++;
  974. return ret;
  975. }
  976. return ret;
  977. }
  978. /**
  979. * regulator_enable - enable regulator output
  980. * @regulator: regulator source
  981. *
  982. * Request that the regulator be enabled with the regulator output at
  983. * the predefined voltage or current value. Calls to regulator_enable()
  984. * must be balanced with calls to regulator_disable().
  985. *
  986. * NOTE: the output value can be set by other drivers, boot loader or may be
  987. * hardwired in the regulator.
  988. */
  989. int regulator_enable(struct regulator *regulator)
  990. {
  991. struct regulator_dev *rdev = regulator->rdev;
  992. int ret = 0;
  993. mutex_lock(&rdev->mutex);
  994. if (regulator->enabled == 0)
  995. ret = _regulator_enable(rdev);
  996. else if (regulator->enabled < 0)
  997. ret = -EIO;
  998. if (ret == 0)
  999. regulator->enabled++;
  1000. mutex_unlock(&rdev->mutex);
  1001. return ret;
  1002. }
  1003. EXPORT_SYMBOL_GPL(regulator_enable);
  1004. /* locks held by regulator_disable() */
  1005. static int _regulator_disable(struct regulator_dev *rdev)
  1006. {
  1007. int ret = 0;
  1008. /* are we the last user and permitted to disable ? */
  1009. if (rdev->use_count == 1 && !rdev->constraints->always_on) {
  1010. /* we are last user */
  1011. if (rdev->desc->ops->disable) {
  1012. ret = rdev->desc->ops->disable(rdev);
  1013. if (ret < 0) {
  1014. printk(KERN_ERR "%s: failed to disable %s\n",
  1015. __func__, rdev->desc->name);
  1016. return ret;
  1017. }
  1018. }
  1019. /* decrease our supplies ref count and disable if required */
  1020. if (rdev->supply)
  1021. _regulator_disable(rdev->supply);
  1022. rdev->use_count = 0;
  1023. } else if (rdev->use_count > 1) {
  1024. if (rdev->constraints &&
  1025. (rdev->constraints->valid_ops_mask &
  1026. REGULATOR_CHANGE_DRMS))
  1027. drms_uA_update(rdev);
  1028. rdev->use_count--;
  1029. }
  1030. return ret;
  1031. }
  1032. /**
  1033. * regulator_disable - disable regulator output
  1034. * @regulator: regulator source
  1035. *
  1036. * Disable the regulator output voltage or current. Calls to
  1037. * regulator_enable() must be balanced with calls to
  1038. * regulator_disable().
  1039. *
  1040. * NOTE: this will only disable the regulator output if no other consumer
  1041. * devices have it enabled, the regulator device supports disabling and
  1042. * machine constraints permit this operation.
  1043. */
  1044. int regulator_disable(struct regulator *regulator)
  1045. {
  1046. struct regulator_dev *rdev = regulator->rdev;
  1047. int ret = 0;
  1048. mutex_lock(&rdev->mutex);
  1049. if (regulator->enabled == 1) {
  1050. ret = _regulator_disable(rdev);
  1051. if (ret == 0)
  1052. regulator->uA_load = 0;
  1053. } else if (WARN(regulator->enabled <= 0,
  1054. "unbalanced disables for supply %s\n",
  1055. regulator->supply_name))
  1056. ret = -EIO;
  1057. if (ret == 0)
  1058. regulator->enabled--;
  1059. mutex_unlock(&rdev->mutex);
  1060. return ret;
  1061. }
  1062. EXPORT_SYMBOL_GPL(regulator_disable);
  1063. /* locks held by regulator_force_disable() */
  1064. static int _regulator_force_disable(struct regulator_dev *rdev)
  1065. {
  1066. int ret = 0;
  1067. /* force disable */
  1068. if (rdev->desc->ops->disable) {
  1069. /* ah well, who wants to live forever... */
  1070. ret = rdev->desc->ops->disable(rdev);
  1071. if (ret < 0) {
  1072. printk(KERN_ERR "%s: failed to force disable %s\n",
  1073. __func__, rdev->desc->name);
  1074. return ret;
  1075. }
  1076. /* notify other consumers that power has been forced off */
  1077. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
  1078. NULL);
  1079. }
  1080. /* decrease our supplies ref count and disable if required */
  1081. if (rdev->supply)
  1082. _regulator_disable(rdev->supply);
  1083. rdev->use_count = 0;
  1084. return ret;
  1085. }
  1086. /**
  1087. * regulator_force_disable - force disable regulator output
  1088. * @regulator: regulator source
  1089. *
  1090. * Forcibly disable the regulator output voltage or current.
  1091. * NOTE: this *will* disable the regulator output even if other consumer
  1092. * devices have it enabled. This should be used for situations when device
  1093. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1094. */
  1095. int regulator_force_disable(struct regulator *regulator)
  1096. {
  1097. int ret;
  1098. mutex_lock(&regulator->rdev->mutex);
  1099. regulator->enabled = 0;
  1100. regulator->uA_load = 0;
  1101. ret = _regulator_force_disable(regulator->rdev);
  1102. mutex_unlock(&regulator->rdev->mutex);
  1103. return ret;
  1104. }
  1105. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1106. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1107. {
  1108. int ret;
  1109. mutex_lock(&rdev->mutex);
  1110. /* sanity check */
  1111. if (!rdev->desc->ops->is_enabled) {
  1112. ret = -EINVAL;
  1113. goto out;
  1114. }
  1115. ret = rdev->desc->ops->is_enabled(rdev);
  1116. out:
  1117. mutex_unlock(&rdev->mutex);
  1118. return ret;
  1119. }
  1120. /**
  1121. * regulator_is_enabled - is the regulator output enabled
  1122. * @regulator: regulator source
  1123. *
  1124. * Returns positive if the regulator driver backing the source/client
  1125. * has requested that the device be enabled, zero if it hasn't, else a
  1126. * negative errno code.
  1127. *
  1128. * Note that the device backing this regulator handle can have multiple
  1129. * users, so it might be enabled even if regulator_enable() was never
  1130. * called for this particular source.
  1131. */
  1132. int regulator_is_enabled(struct regulator *regulator)
  1133. {
  1134. return _regulator_is_enabled(regulator->rdev);
  1135. }
  1136. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1137. /**
  1138. * regulator_count_voltages - count regulator_list_voltage() selectors
  1139. * @regulator: regulator source
  1140. *
  1141. * Returns number of selectors, or negative errno. Selectors are
  1142. * numbered starting at zero, and typically correspond to bitfields
  1143. * in hardware registers.
  1144. */
  1145. int regulator_count_voltages(struct regulator *regulator)
  1146. {
  1147. struct regulator_dev *rdev = regulator->rdev;
  1148. return rdev->desc->n_voltages ? : -EINVAL;
  1149. }
  1150. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1151. /**
  1152. * regulator_list_voltage - enumerate supported voltages
  1153. * @regulator: regulator source
  1154. * @selector: identify voltage to list
  1155. * Context: can sleep
  1156. *
  1157. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1158. * zero if this selector code can't be used on this sytem, or a
  1159. * negative errno.
  1160. */
  1161. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1162. {
  1163. struct regulator_dev *rdev = regulator->rdev;
  1164. struct regulator_ops *ops = rdev->desc->ops;
  1165. int ret;
  1166. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1167. return -EINVAL;
  1168. mutex_lock(&rdev->mutex);
  1169. ret = ops->list_voltage(rdev, selector);
  1170. mutex_unlock(&rdev->mutex);
  1171. if (ret > 0) {
  1172. if (ret < rdev->constraints->min_uV)
  1173. ret = 0;
  1174. else if (ret > rdev->constraints->max_uV)
  1175. ret = 0;
  1176. }
  1177. return ret;
  1178. }
  1179. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1180. /**
  1181. * regulator_set_voltage - set regulator output voltage
  1182. * @regulator: regulator source
  1183. * @min_uV: Minimum required voltage in uV
  1184. * @max_uV: Maximum acceptable voltage in uV
  1185. *
  1186. * Sets a voltage regulator to the desired output voltage. This can be set
  1187. * during any regulator state. IOW, regulator can be disabled or enabled.
  1188. *
  1189. * If the regulator is enabled then the voltage will change to the new value
  1190. * immediately otherwise if the regulator is disabled the regulator will
  1191. * output at the new voltage when enabled.
  1192. *
  1193. * NOTE: If the regulator is shared between several devices then the lowest
  1194. * request voltage that meets the system constraints will be used.
  1195. * Regulator system constraints must be set for this regulator before
  1196. * calling this function otherwise this call will fail.
  1197. */
  1198. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1199. {
  1200. struct regulator_dev *rdev = regulator->rdev;
  1201. int ret;
  1202. mutex_lock(&rdev->mutex);
  1203. /* sanity check */
  1204. if (!rdev->desc->ops->set_voltage) {
  1205. ret = -EINVAL;
  1206. goto out;
  1207. }
  1208. /* constraints check */
  1209. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1210. if (ret < 0)
  1211. goto out;
  1212. regulator->min_uV = min_uV;
  1213. regulator->max_uV = max_uV;
  1214. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1215. out:
  1216. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1217. mutex_unlock(&rdev->mutex);
  1218. return ret;
  1219. }
  1220. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1221. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1222. {
  1223. /* sanity check */
  1224. if (rdev->desc->ops->get_voltage)
  1225. return rdev->desc->ops->get_voltage(rdev);
  1226. else
  1227. return -EINVAL;
  1228. }
  1229. /**
  1230. * regulator_get_voltage - get regulator output voltage
  1231. * @regulator: regulator source
  1232. *
  1233. * This returns the current regulator voltage in uV.
  1234. *
  1235. * NOTE: If the regulator is disabled it will return the voltage value. This
  1236. * function should not be used to determine regulator state.
  1237. */
  1238. int regulator_get_voltage(struct regulator *regulator)
  1239. {
  1240. int ret;
  1241. mutex_lock(&regulator->rdev->mutex);
  1242. ret = _regulator_get_voltage(regulator->rdev);
  1243. mutex_unlock(&regulator->rdev->mutex);
  1244. return ret;
  1245. }
  1246. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1247. /**
  1248. * regulator_set_current_limit - set regulator output current limit
  1249. * @regulator: regulator source
  1250. * @min_uA: Minimuum supported current in uA
  1251. * @max_uA: Maximum supported current in uA
  1252. *
  1253. * Sets current sink to the desired output current. This can be set during
  1254. * any regulator state. IOW, regulator can be disabled or enabled.
  1255. *
  1256. * If the regulator is enabled then the current will change to the new value
  1257. * immediately otherwise if the regulator is disabled the regulator will
  1258. * output at the new current when enabled.
  1259. *
  1260. * NOTE: Regulator system constraints must be set for this regulator before
  1261. * calling this function otherwise this call will fail.
  1262. */
  1263. int regulator_set_current_limit(struct regulator *regulator,
  1264. int min_uA, int max_uA)
  1265. {
  1266. struct regulator_dev *rdev = regulator->rdev;
  1267. int ret;
  1268. mutex_lock(&rdev->mutex);
  1269. /* sanity check */
  1270. if (!rdev->desc->ops->set_current_limit) {
  1271. ret = -EINVAL;
  1272. goto out;
  1273. }
  1274. /* constraints check */
  1275. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1276. if (ret < 0)
  1277. goto out;
  1278. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1279. out:
  1280. mutex_unlock(&rdev->mutex);
  1281. return ret;
  1282. }
  1283. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1284. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1285. {
  1286. int ret;
  1287. mutex_lock(&rdev->mutex);
  1288. /* sanity check */
  1289. if (!rdev->desc->ops->get_current_limit) {
  1290. ret = -EINVAL;
  1291. goto out;
  1292. }
  1293. ret = rdev->desc->ops->get_current_limit(rdev);
  1294. out:
  1295. mutex_unlock(&rdev->mutex);
  1296. return ret;
  1297. }
  1298. /**
  1299. * regulator_get_current_limit - get regulator output current
  1300. * @regulator: regulator source
  1301. *
  1302. * This returns the current supplied by the specified current sink in uA.
  1303. *
  1304. * NOTE: If the regulator is disabled it will return the current value. This
  1305. * function should not be used to determine regulator state.
  1306. */
  1307. int regulator_get_current_limit(struct regulator *regulator)
  1308. {
  1309. return _regulator_get_current_limit(regulator->rdev);
  1310. }
  1311. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1312. /**
  1313. * regulator_set_mode - set regulator operating mode
  1314. * @regulator: regulator source
  1315. * @mode: operating mode - one of the REGULATOR_MODE constants
  1316. *
  1317. * Set regulator operating mode to increase regulator efficiency or improve
  1318. * regulation performance.
  1319. *
  1320. * NOTE: Regulator system constraints must be set for this regulator before
  1321. * calling this function otherwise this call will fail.
  1322. */
  1323. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1324. {
  1325. struct regulator_dev *rdev = regulator->rdev;
  1326. int ret;
  1327. mutex_lock(&rdev->mutex);
  1328. /* sanity check */
  1329. if (!rdev->desc->ops->set_mode) {
  1330. ret = -EINVAL;
  1331. goto out;
  1332. }
  1333. /* constraints check */
  1334. ret = regulator_check_mode(rdev, mode);
  1335. if (ret < 0)
  1336. goto out;
  1337. ret = rdev->desc->ops->set_mode(rdev, mode);
  1338. out:
  1339. mutex_unlock(&rdev->mutex);
  1340. return ret;
  1341. }
  1342. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1343. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1344. {
  1345. int ret;
  1346. mutex_lock(&rdev->mutex);
  1347. /* sanity check */
  1348. if (!rdev->desc->ops->get_mode) {
  1349. ret = -EINVAL;
  1350. goto out;
  1351. }
  1352. ret = rdev->desc->ops->get_mode(rdev);
  1353. out:
  1354. mutex_unlock(&rdev->mutex);
  1355. return ret;
  1356. }
  1357. /**
  1358. * regulator_get_mode - get regulator operating mode
  1359. * @regulator: regulator source
  1360. *
  1361. * Get the current regulator operating mode.
  1362. */
  1363. unsigned int regulator_get_mode(struct regulator *regulator)
  1364. {
  1365. return _regulator_get_mode(regulator->rdev);
  1366. }
  1367. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1368. /**
  1369. * regulator_set_optimum_mode - set regulator optimum operating mode
  1370. * @regulator: regulator source
  1371. * @uA_load: load current
  1372. *
  1373. * Notifies the regulator core of a new device load. This is then used by
  1374. * DRMS (if enabled by constraints) to set the most efficient regulator
  1375. * operating mode for the new regulator loading.
  1376. *
  1377. * Consumer devices notify their supply regulator of the maximum power
  1378. * they will require (can be taken from device datasheet in the power
  1379. * consumption tables) when they change operational status and hence power
  1380. * state. Examples of operational state changes that can affect power
  1381. * consumption are :-
  1382. *
  1383. * o Device is opened / closed.
  1384. * o Device I/O is about to begin or has just finished.
  1385. * o Device is idling in between work.
  1386. *
  1387. * This information is also exported via sysfs to userspace.
  1388. *
  1389. * DRMS will sum the total requested load on the regulator and change
  1390. * to the most efficient operating mode if platform constraints allow.
  1391. *
  1392. * Returns the new regulator mode or error.
  1393. */
  1394. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1395. {
  1396. struct regulator_dev *rdev = regulator->rdev;
  1397. struct regulator *consumer;
  1398. int ret, output_uV, input_uV, total_uA_load = 0;
  1399. unsigned int mode;
  1400. mutex_lock(&rdev->mutex);
  1401. regulator->uA_load = uA_load;
  1402. ret = regulator_check_drms(rdev);
  1403. if (ret < 0)
  1404. goto out;
  1405. ret = -EINVAL;
  1406. /* sanity check */
  1407. if (!rdev->desc->ops->get_optimum_mode)
  1408. goto out;
  1409. /* get output voltage */
  1410. output_uV = rdev->desc->ops->get_voltage(rdev);
  1411. if (output_uV <= 0) {
  1412. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1413. __func__, rdev->desc->name);
  1414. goto out;
  1415. }
  1416. /* get input voltage */
  1417. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1418. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1419. else
  1420. input_uV = rdev->constraints->input_uV;
  1421. if (input_uV <= 0) {
  1422. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1423. __func__, rdev->desc->name);
  1424. goto out;
  1425. }
  1426. /* calc total requested load for this regulator */
  1427. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1428. total_uA_load += consumer->uA_load;
  1429. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1430. input_uV, output_uV,
  1431. total_uA_load);
  1432. ret = regulator_check_mode(rdev, mode);
  1433. if (ret < 0) {
  1434. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1435. " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
  1436. total_uA_load, input_uV, output_uV);
  1437. goto out;
  1438. }
  1439. ret = rdev->desc->ops->set_mode(rdev, mode);
  1440. if (ret < 0) {
  1441. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1442. __func__, mode, rdev->desc->name);
  1443. goto out;
  1444. }
  1445. ret = mode;
  1446. out:
  1447. mutex_unlock(&rdev->mutex);
  1448. return ret;
  1449. }
  1450. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1451. /**
  1452. * regulator_register_notifier - register regulator event notifier
  1453. * @regulator: regulator source
  1454. * @nb: notifier block
  1455. *
  1456. * Register notifier block to receive regulator events.
  1457. */
  1458. int regulator_register_notifier(struct regulator *regulator,
  1459. struct notifier_block *nb)
  1460. {
  1461. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1462. nb);
  1463. }
  1464. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1465. /**
  1466. * regulator_unregister_notifier - unregister regulator event notifier
  1467. * @regulator: regulator source
  1468. * @nb: notifier block
  1469. *
  1470. * Unregister regulator event notifier block.
  1471. */
  1472. int regulator_unregister_notifier(struct regulator *regulator,
  1473. struct notifier_block *nb)
  1474. {
  1475. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1476. nb);
  1477. }
  1478. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1479. /* notify regulator consumers and downstream regulator consumers.
  1480. * Note mutex must be held by caller.
  1481. */
  1482. static void _notifier_call_chain(struct regulator_dev *rdev,
  1483. unsigned long event, void *data)
  1484. {
  1485. struct regulator_dev *_rdev;
  1486. /* call rdev chain first */
  1487. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1488. /* now notify regulator we supply */
  1489. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1490. mutex_lock(&_rdev->mutex);
  1491. _notifier_call_chain(_rdev, event, data);
  1492. mutex_unlock(&_rdev->mutex);
  1493. }
  1494. }
  1495. /**
  1496. * regulator_bulk_get - get multiple regulator consumers
  1497. *
  1498. * @dev: Device to supply
  1499. * @num_consumers: Number of consumers to register
  1500. * @consumers: Configuration of consumers; clients are stored here.
  1501. *
  1502. * @return 0 on success, an errno on failure.
  1503. *
  1504. * This helper function allows drivers to get several regulator
  1505. * consumers in one operation. If any of the regulators cannot be
  1506. * acquired then any regulators that were allocated will be freed
  1507. * before returning to the caller.
  1508. */
  1509. int regulator_bulk_get(struct device *dev, int num_consumers,
  1510. struct regulator_bulk_data *consumers)
  1511. {
  1512. int i;
  1513. int ret;
  1514. for (i = 0; i < num_consumers; i++)
  1515. consumers[i].consumer = NULL;
  1516. for (i = 0; i < num_consumers; i++) {
  1517. consumers[i].consumer = regulator_get(dev,
  1518. consumers[i].supply);
  1519. if (IS_ERR(consumers[i].consumer)) {
  1520. dev_err(dev, "Failed to get supply '%s'\n",
  1521. consumers[i].supply);
  1522. ret = PTR_ERR(consumers[i].consumer);
  1523. consumers[i].consumer = NULL;
  1524. goto err;
  1525. }
  1526. }
  1527. return 0;
  1528. err:
  1529. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1530. regulator_put(consumers[i].consumer);
  1531. return ret;
  1532. }
  1533. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1534. /**
  1535. * regulator_bulk_enable - enable multiple regulator consumers
  1536. *
  1537. * @num_consumers: Number of consumers
  1538. * @consumers: Consumer data; clients are stored here.
  1539. * @return 0 on success, an errno on failure
  1540. *
  1541. * This convenience API allows consumers to enable multiple regulator
  1542. * clients in a single API call. If any consumers cannot be enabled
  1543. * then any others that were enabled will be disabled again prior to
  1544. * return.
  1545. */
  1546. int regulator_bulk_enable(int num_consumers,
  1547. struct regulator_bulk_data *consumers)
  1548. {
  1549. int i;
  1550. int ret;
  1551. for (i = 0; i < num_consumers; i++) {
  1552. ret = regulator_enable(consumers[i].consumer);
  1553. if (ret != 0)
  1554. goto err;
  1555. }
  1556. return 0;
  1557. err:
  1558. printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
  1559. for (i = 0; i < num_consumers; i++)
  1560. regulator_disable(consumers[i].consumer);
  1561. return ret;
  1562. }
  1563. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1564. /**
  1565. * regulator_bulk_disable - disable multiple regulator consumers
  1566. *
  1567. * @num_consumers: Number of consumers
  1568. * @consumers: Consumer data; clients are stored here.
  1569. * @return 0 on success, an errno on failure
  1570. *
  1571. * This convenience API allows consumers to disable multiple regulator
  1572. * clients in a single API call. If any consumers cannot be enabled
  1573. * then any others that were disabled will be disabled again prior to
  1574. * return.
  1575. */
  1576. int regulator_bulk_disable(int num_consumers,
  1577. struct regulator_bulk_data *consumers)
  1578. {
  1579. int i;
  1580. int ret;
  1581. for (i = 0; i < num_consumers; i++) {
  1582. ret = regulator_disable(consumers[i].consumer);
  1583. if (ret != 0)
  1584. goto err;
  1585. }
  1586. return 0;
  1587. err:
  1588. printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
  1589. for (i = 0; i < num_consumers; i++)
  1590. regulator_enable(consumers[i].consumer);
  1591. return ret;
  1592. }
  1593. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1594. /**
  1595. * regulator_bulk_free - free multiple regulator consumers
  1596. *
  1597. * @num_consumers: Number of consumers
  1598. * @consumers: Consumer data; clients are stored here.
  1599. *
  1600. * This convenience API allows consumers to free multiple regulator
  1601. * clients in a single API call.
  1602. */
  1603. void regulator_bulk_free(int num_consumers,
  1604. struct regulator_bulk_data *consumers)
  1605. {
  1606. int i;
  1607. for (i = 0; i < num_consumers; i++) {
  1608. regulator_put(consumers[i].consumer);
  1609. consumers[i].consumer = NULL;
  1610. }
  1611. }
  1612. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1613. /**
  1614. * regulator_notifier_call_chain - call regulator event notifier
  1615. * @rdev: regulator source
  1616. * @event: notifier block
  1617. * @data: callback-specific data.
  1618. *
  1619. * Called by regulator drivers to notify clients a regulator event has
  1620. * occurred. We also notify regulator clients downstream.
  1621. * Note lock must be held by caller.
  1622. */
  1623. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1624. unsigned long event, void *data)
  1625. {
  1626. _notifier_call_chain(rdev, event, data);
  1627. return NOTIFY_DONE;
  1628. }
  1629. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1630. /*
  1631. * To avoid cluttering sysfs (and memory) with useless state, only
  1632. * create attributes that can be meaningfully displayed.
  1633. */
  1634. static int add_regulator_attributes(struct regulator_dev *rdev)
  1635. {
  1636. struct device *dev = &rdev->dev;
  1637. struct regulator_ops *ops = rdev->desc->ops;
  1638. int status = 0;
  1639. /* some attributes need specific methods to be displayed */
  1640. if (ops->get_voltage) {
  1641. status = device_create_file(dev, &dev_attr_microvolts);
  1642. if (status < 0)
  1643. return status;
  1644. }
  1645. if (ops->get_current_limit) {
  1646. status = device_create_file(dev, &dev_attr_microamps);
  1647. if (status < 0)
  1648. return status;
  1649. }
  1650. if (ops->get_mode) {
  1651. status = device_create_file(dev, &dev_attr_opmode);
  1652. if (status < 0)
  1653. return status;
  1654. }
  1655. if (ops->is_enabled) {
  1656. status = device_create_file(dev, &dev_attr_state);
  1657. if (status < 0)
  1658. return status;
  1659. }
  1660. if (ops->get_status) {
  1661. status = device_create_file(dev, &dev_attr_status);
  1662. if (status < 0)
  1663. return status;
  1664. }
  1665. /* some attributes are type-specific */
  1666. if (rdev->desc->type == REGULATOR_CURRENT) {
  1667. status = device_create_file(dev, &dev_attr_requested_microamps);
  1668. if (status < 0)
  1669. return status;
  1670. }
  1671. /* all the other attributes exist to support constraints;
  1672. * don't show them if there are no constraints, or if the
  1673. * relevant supporting methods are missing.
  1674. */
  1675. if (!rdev->constraints)
  1676. return status;
  1677. /* constraints need specific supporting methods */
  1678. if (ops->set_voltage) {
  1679. status = device_create_file(dev, &dev_attr_min_microvolts);
  1680. if (status < 0)
  1681. return status;
  1682. status = device_create_file(dev, &dev_attr_max_microvolts);
  1683. if (status < 0)
  1684. return status;
  1685. }
  1686. if (ops->set_current_limit) {
  1687. status = device_create_file(dev, &dev_attr_min_microamps);
  1688. if (status < 0)
  1689. return status;
  1690. status = device_create_file(dev, &dev_attr_max_microamps);
  1691. if (status < 0)
  1692. return status;
  1693. }
  1694. /* suspend mode constraints need multiple supporting methods */
  1695. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1696. return status;
  1697. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1698. if (status < 0)
  1699. return status;
  1700. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1701. if (status < 0)
  1702. return status;
  1703. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1704. if (status < 0)
  1705. return status;
  1706. if (ops->set_suspend_voltage) {
  1707. status = device_create_file(dev,
  1708. &dev_attr_suspend_standby_microvolts);
  1709. if (status < 0)
  1710. return status;
  1711. status = device_create_file(dev,
  1712. &dev_attr_suspend_mem_microvolts);
  1713. if (status < 0)
  1714. return status;
  1715. status = device_create_file(dev,
  1716. &dev_attr_suspend_disk_microvolts);
  1717. if (status < 0)
  1718. return status;
  1719. }
  1720. if (ops->set_suspend_mode) {
  1721. status = device_create_file(dev,
  1722. &dev_attr_suspend_standby_mode);
  1723. if (status < 0)
  1724. return status;
  1725. status = device_create_file(dev,
  1726. &dev_attr_suspend_mem_mode);
  1727. if (status < 0)
  1728. return status;
  1729. status = device_create_file(dev,
  1730. &dev_attr_suspend_disk_mode);
  1731. if (status < 0)
  1732. return status;
  1733. }
  1734. return status;
  1735. }
  1736. /**
  1737. * regulator_register - register regulator
  1738. * @regulator_desc: regulator to register
  1739. * @dev: struct device for the regulator
  1740. * @init_data: platform provided init data, passed through by driver
  1741. * @driver_data: private regulator data
  1742. *
  1743. * Called by regulator drivers to register a regulator.
  1744. * Returns 0 on success.
  1745. */
  1746. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1747. struct device *dev, struct regulator_init_data *init_data,
  1748. void *driver_data)
  1749. {
  1750. static atomic_t regulator_no = ATOMIC_INIT(0);
  1751. struct regulator_dev *rdev;
  1752. int ret, i;
  1753. if (regulator_desc == NULL)
  1754. return ERR_PTR(-EINVAL);
  1755. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1756. return ERR_PTR(-EINVAL);
  1757. if (!regulator_desc->type == REGULATOR_VOLTAGE &&
  1758. !regulator_desc->type == REGULATOR_CURRENT)
  1759. return ERR_PTR(-EINVAL);
  1760. if (!init_data)
  1761. return ERR_PTR(-EINVAL);
  1762. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1763. if (rdev == NULL)
  1764. return ERR_PTR(-ENOMEM);
  1765. mutex_lock(&regulator_list_mutex);
  1766. mutex_init(&rdev->mutex);
  1767. rdev->reg_data = driver_data;
  1768. rdev->owner = regulator_desc->owner;
  1769. rdev->desc = regulator_desc;
  1770. INIT_LIST_HEAD(&rdev->consumer_list);
  1771. INIT_LIST_HEAD(&rdev->supply_list);
  1772. INIT_LIST_HEAD(&rdev->list);
  1773. INIT_LIST_HEAD(&rdev->slist);
  1774. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1775. /* preform any regulator specific init */
  1776. if (init_data->regulator_init) {
  1777. ret = init_data->regulator_init(rdev->reg_data);
  1778. if (ret < 0)
  1779. goto clean;
  1780. }
  1781. /* register with sysfs */
  1782. rdev->dev.class = &regulator_class;
  1783. rdev->dev.parent = dev;
  1784. dev_set_name(&rdev->dev, "regulator.%d",
  1785. atomic_inc_return(&regulator_no) - 1);
  1786. ret = device_register(&rdev->dev);
  1787. if (ret != 0)
  1788. goto clean;
  1789. dev_set_drvdata(&rdev->dev, rdev);
  1790. /* set regulator constraints */
  1791. ret = set_machine_constraints(rdev, &init_data->constraints);
  1792. if (ret < 0)
  1793. goto scrub;
  1794. /* add attributes supported by this regulator */
  1795. ret = add_regulator_attributes(rdev);
  1796. if (ret < 0)
  1797. goto scrub;
  1798. /* set supply regulator if it exists */
  1799. if (init_data->supply_regulator_dev) {
  1800. ret = set_supply(rdev,
  1801. dev_get_drvdata(init_data->supply_regulator_dev));
  1802. if (ret < 0)
  1803. goto scrub;
  1804. }
  1805. /* add consumers devices */
  1806. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1807. ret = set_consumer_device_supply(rdev,
  1808. init_data->consumer_supplies[i].dev,
  1809. init_data->consumer_supplies[i].supply);
  1810. if (ret < 0) {
  1811. for (--i; i >= 0; i--)
  1812. unset_consumer_device_supply(rdev,
  1813. init_data->consumer_supplies[i].dev);
  1814. goto scrub;
  1815. }
  1816. }
  1817. list_add(&rdev->list, &regulator_list);
  1818. out:
  1819. mutex_unlock(&regulator_list_mutex);
  1820. return rdev;
  1821. scrub:
  1822. device_unregister(&rdev->dev);
  1823. clean:
  1824. kfree(rdev);
  1825. rdev = ERR_PTR(ret);
  1826. goto out;
  1827. }
  1828. EXPORT_SYMBOL_GPL(regulator_register);
  1829. /**
  1830. * regulator_unregister - unregister regulator
  1831. * @rdev: regulator to unregister
  1832. *
  1833. * Called by regulator drivers to unregister a regulator.
  1834. */
  1835. void regulator_unregister(struct regulator_dev *rdev)
  1836. {
  1837. if (rdev == NULL)
  1838. return;
  1839. mutex_lock(&regulator_list_mutex);
  1840. unset_regulator_supplies(rdev);
  1841. list_del(&rdev->list);
  1842. if (rdev->supply)
  1843. sysfs_remove_link(&rdev->dev.kobj, "supply");
  1844. device_unregister(&rdev->dev);
  1845. mutex_unlock(&regulator_list_mutex);
  1846. }
  1847. EXPORT_SYMBOL_GPL(regulator_unregister);
  1848. /**
  1849. * regulator_suspend_prepare - prepare regulators for system wide suspend
  1850. * @state: system suspend state
  1851. *
  1852. * Configure each regulator with it's suspend operating parameters for state.
  1853. * This will usually be called by machine suspend code prior to supending.
  1854. */
  1855. int regulator_suspend_prepare(suspend_state_t state)
  1856. {
  1857. struct regulator_dev *rdev;
  1858. int ret = 0;
  1859. /* ON is handled by regulator active state */
  1860. if (state == PM_SUSPEND_ON)
  1861. return -EINVAL;
  1862. mutex_lock(&regulator_list_mutex);
  1863. list_for_each_entry(rdev, &regulator_list, list) {
  1864. mutex_lock(&rdev->mutex);
  1865. ret = suspend_prepare(rdev, state);
  1866. mutex_unlock(&rdev->mutex);
  1867. if (ret < 0) {
  1868. printk(KERN_ERR "%s: failed to prepare %s\n",
  1869. __func__, rdev->desc->name);
  1870. goto out;
  1871. }
  1872. }
  1873. out:
  1874. mutex_unlock(&regulator_list_mutex);
  1875. return ret;
  1876. }
  1877. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  1878. /**
  1879. * rdev_get_drvdata - get rdev regulator driver data
  1880. * @rdev: regulator
  1881. *
  1882. * Get rdev regulator driver private data. This call can be used in the
  1883. * regulator driver context.
  1884. */
  1885. void *rdev_get_drvdata(struct regulator_dev *rdev)
  1886. {
  1887. return rdev->reg_data;
  1888. }
  1889. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  1890. /**
  1891. * regulator_get_drvdata - get regulator driver data
  1892. * @regulator: regulator
  1893. *
  1894. * Get regulator driver private data. This call can be used in the consumer
  1895. * driver context when non API regulator specific functions need to be called.
  1896. */
  1897. void *regulator_get_drvdata(struct regulator *regulator)
  1898. {
  1899. return regulator->rdev->reg_data;
  1900. }
  1901. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  1902. /**
  1903. * regulator_set_drvdata - set regulator driver data
  1904. * @regulator: regulator
  1905. * @data: data
  1906. */
  1907. void regulator_set_drvdata(struct regulator *regulator, void *data)
  1908. {
  1909. regulator->rdev->reg_data = data;
  1910. }
  1911. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  1912. /**
  1913. * regulator_get_id - get regulator ID
  1914. * @rdev: regulator
  1915. */
  1916. int rdev_get_id(struct regulator_dev *rdev)
  1917. {
  1918. return rdev->desc->id;
  1919. }
  1920. EXPORT_SYMBOL_GPL(rdev_get_id);
  1921. struct device *rdev_get_dev(struct regulator_dev *rdev)
  1922. {
  1923. return &rdev->dev;
  1924. }
  1925. EXPORT_SYMBOL_GPL(rdev_get_dev);
  1926. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  1927. {
  1928. return reg_init_data->driver_data;
  1929. }
  1930. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  1931. static int __init regulator_init(void)
  1932. {
  1933. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  1934. return class_register(&regulator_class);
  1935. }
  1936. /* init early to allow our consumers to complete system booting */
  1937. core_initcall(regulator_init);