core.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  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. static int has_full_constraints;
  29. /*
  30. * struct regulator_map
  31. *
  32. * Used to provide symbolic supply names to devices.
  33. */
  34. struct regulator_map {
  35. struct list_head list;
  36. const char *dev_name; /* The dev_name() for the consumer */
  37. const char *supply;
  38. struct regulator_dev *regulator;
  39. };
  40. /*
  41. * struct regulator
  42. *
  43. * One for each consumer device.
  44. */
  45. struct regulator {
  46. struct device *dev;
  47. struct list_head list;
  48. int uA_load;
  49. int min_uV;
  50. int max_uV;
  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. and the constraints are used by list_voltage. */
  606. if (count == 1 && !cmin) {
  607. cmin = 1;
  608. cmax = INT_MAX;
  609. constraints->min_uV = cmin;
  610. constraints->max_uV = cmax;
  611. }
  612. /* voltage constraints are optional */
  613. if ((cmin == 0) && (cmax == 0))
  614. goto out;
  615. /* else require explicit machine-level constraints */
  616. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  617. pr_err("%s: %s '%s' voltage constraints\n",
  618. __func__, "invalid", name);
  619. ret = -EINVAL;
  620. goto out;
  621. }
  622. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  623. for (i = 0; i < count; i++) {
  624. int value;
  625. value = ops->list_voltage(rdev, i);
  626. if (value <= 0)
  627. continue;
  628. /* maybe adjust [min_uV..max_uV] */
  629. if (value >= cmin && value < min_uV)
  630. min_uV = value;
  631. if (value <= cmax && value > max_uV)
  632. max_uV = value;
  633. }
  634. /* final: [min_uV..max_uV] valid iff constraints valid */
  635. if (max_uV < min_uV) {
  636. pr_err("%s: %s '%s' voltage constraints\n",
  637. __func__, "unsupportable", name);
  638. ret = -EINVAL;
  639. goto out;
  640. }
  641. /* use regulator's subset of machine constraints */
  642. if (constraints->min_uV < min_uV) {
  643. pr_debug("%s: override '%s' %s, %d -> %d\n",
  644. __func__, name, "min_uV",
  645. constraints->min_uV, min_uV);
  646. constraints->min_uV = min_uV;
  647. }
  648. if (constraints->max_uV > max_uV) {
  649. pr_debug("%s: override '%s' %s, %d -> %d\n",
  650. __func__, name, "max_uV",
  651. constraints->max_uV, max_uV);
  652. constraints->max_uV = max_uV;
  653. }
  654. }
  655. rdev->constraints = constraints;
  656. /* do we need to apply the constraint voltage */
  657. if (rdev->constraints->apply_uV &&
  658. rdev->constraints->min_uV == rdev->constraints->max_uV &&
  659. ops->set_voltage) {
  660. ret = ops->set_voltage(rdev,
  661. rdev->constraints->min_uV, rdev->constraints->max_uV);
  662. if (ret < 0) {
  663. printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
  664. __func__,
  665. rdev->constraints->min_uV, name);
  666. rdev->constraints = NULL;
  667. goto out;
  668. }
  669. }
  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 the constraints say the regulator should be on at this point
  696. * and we have control then make sure it is enabled.
  697. */
  698. if ((constraints->always_on || constraints->boot_on) && ops->enable) {
  699. ret = ops->enable(rdev);
  700. if (ret < 0) {
  701. printk(KERN_ERR "%s: failed to enable %s\n",
  702. __func__, name);
  703. rdev->constraints = NULL;
  704. goto out;
  705. }
  706. }
  707. print_constraints(rdev);
  708. out:
  709. return ret;
  710. }
  711. /**
  712. * set_supply - set regulator supply regulator
  713. * @rdev: regulator name
  714. * @supply_rdev: supply regulator name
  715. *
  716. * Called by platform initialisation code to set the supply regulator for this
  717. * regulator. This ensures that a regulators supply will also be enabled by the
  718. * core if it's child is enabled.
  719. */
  720. static int set_supply(struct regulator_dev *rdev,
  721. struct regulator_dev *supply_rdev)
  722. {
  723. int err;
  724. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  725. "supply");
  726. if (err) {
  727. printk(KERN_ERR
  728. "%s: could not add device link %s err %d\n",
  729. __func__, supply_rdev->dev.kobj.name, err);
  730. goto out;
  731. }
  732. rdev->supply = supply_rdev;
  733. list_add(&rdev->slist, &supply_rdev->supply_list);
  734. out:
  735. return err;
  736. }
  737. /**
  738. * set_consumer_device_supply: Bind a regulator to a symbolic supply
  739. * @rdev: regulator source
  740. * @consumer_dev: device the supply applies to
  741. * @consumer_dev_name: dev_name() string for device 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. * Only one of consumer_dev and consumer_dev_name may be specified.
  750. */
  751. static int set_consumer_device_supply(struct regulator_dev *rdev,
  752. struct device *consumer_dev, const char *consumer_dev_name,
  753. const char *supply)
  754. {
  755. struct regulator_map *node;
  756. if (consumer_dev && consumer_dev_name)
  757. return -EINVAL;
  758. if (!consumer_dev_name && consumer_dev)
  759. consumer_dev_name = dev_name(consumer_dev);
  760. if (supply == NULL)
  761. return -EINVAL;
  762. list_for_each_entry(node, &regulator_map_list, list) {
  763. if (consumer_dev_name != node->dev_name)
  764. continue;
  765. if (strcmp(node->supply, supply) != 0)
  766. continue;
  767. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  768. dev_name(&node->regulator->dev),
  769. node->regulator->desc->name,
  770. supply,
  771. dev_name(&rdev->dev), rdev->desc->name);
  772. return -EBUSY;
  773. }
  774. node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
  775. if (node == NULL)
  776. return -ENOMEM;
  777. node->regulator = rdev;
  778. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  779. node->supply = supply;
  780. if (node->dev_name == NULL) {
  781. kfree(node);
  782. return -ENOMEM;
  783. }
  784. list_add(&node->list, &regulator_map_list);
  785. return 0;
  786. }
  787. static void unset_consumer_device_supply(struct regulator_dev *rdev,
  788. const char *consumer_dev_name, struct device *consumer_dev)
  789. {
  790. struct regulator_map *node, *n;
  791. if (consumer_dev && !consumer_dev_name)
  792. consumer_dev_name = dev_name(consumer_dev);
  793. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  794. if (rdev != node->regulator)
  795. continue;
  796. if (consumer_dev_name && node->dev_name &&
  797. strcmp(consumer_dev_name, node->dev_name))
  798. continue;
  799. list_del(&node->list);
  800. kfree(node->dev_name);
  801. kfree(node);
  802. return;
  803. }
  804. }
  805. static void unset_regulator_supplies(struct regulator_dev *rdev)
  806. {
  807. struct regulator_map *node, *n;
  808. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  809. if (rdev == node->regulator) {
  810. list_del(&node->list);
  811. kfree(node->dev_name);
  812. kfree(node);
  813. return;
  814. }
  815. }
  816. }
  817. #define REG_STR_SIZE 32
  818. static struct regulator *create_regulator(struct regulator_dev *rdev,
  819. struct device *dev,
  820. const char *supply_name)
  821. {
  822. struct regulator *regulator;
  823. char buf[REG_STR_SIZE];
  824. int err, size;
  825. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  826. if (regulator == NULL)
  827. return NULL;
  828. mutex_lock(&rdev->mutex);
  829. regulator->rdev = rdev;
  830. list_add(&regulator->list, &rdev->consumer_list);
  831. if (dev) {
  832. /* create a 'requested_microamps_name' sysfs entry */
  833. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  834. supply_name);
  835. if (size >= REG_STR_SIZE)
  836. goto overflow_err;
  837. regulator->dev = dev;
  838. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  839. if (regulator->dev_attr.attr.name == NULL)
  840. goto attr_name_err;
  841. regulator->dev_attr.attr.owner = THIS_MODULE;
  842. regulator->dev_attr.attr.mode = 0444;
  843. regulator->dev_attr.show = device_requested_uA_show;
  844. err = device_create_file(dev, &regulator->dev_attr);
  845. if (err < 0) {
  846. printk(KERN_WARNING "%s: could not add regulator_dev"
  847. " load sysfs\n", __func__);
  848. goto attr_name_err;
  849. }
  850. /* also add a link to the device sysfs entry */
  851. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  852. dev->kobj.name, supply_name);
  853. if (size >= REG_STR_SIZE)
  854. goto attr_err;
  855. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  856. if (regulator->supply_name == NULL)
  857. goto attr_err;
  858. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  859. buf);
  860. if (err) {
  861. printk(KERN_WARNING
  862. "%s: could not add device link %s err %d\n",
  863. __func__, dev->kobj.name, err);
  864. device_remove_file(dev, &regulator->dev_attr);
  865. goto link_name_err;
  866. }
  867. }
  868. mutex_unlock(&rdev->mutex);
  869. return regulator;
  870. link_name_err:
  871. kfree(regulator->supply_name);
  872. attr_err:
  873. device_remove_file(regulator->dev, &regulator->dev_attr);
  874. attr_name_err:
  875. kfree(regulator->dev_attr.attr.name);
  876. overflow_err:
  877. list_del(&regulator->list);
  878. kfree(regulator);
  879. mutex_unlock(&rdev->mutex);
  880. return NULL;
  881. }
  882. /**
  883. * regulator_get - lookup and obtain a reference to a regulator.
  884. * @dev: device for regulator "consumer"
  885. * @id: Supply name or regulator ID.
  886. *
  887. * Returns a struct regulator corresponding to the regulator producer,
  888. * or IS_ERR() condition containing errno.
  889. *
  890. * Use of supply names configured via regulator_set_device_supply() is
  891. * strongly encouraged. It is recommended that the supply name used
  892. * should match the name used for the supply and/or the relevant
  893. * device pins in the datasheet.
  894. */
  895. struct regulator *regulator_get(struct device *dev, const char *id)
  896. {
  897. struct regulator_dev *rdev;
  898. struct regulator_map *map;
  899. struct regulator *regulator = ERR_PTR(-ENODEV);
  900. const char *devname = NULL;
  901. if (id == NULL) {
  902. printk(KERN_ERR "regulator: get() with no identifier\n");
  903. return regulator;
  904. }
  905. if (dev)
  906. devname = dev_name(dev);
  907. mutex_lock(&regulator_list_mutex);
  908. list_for_each_entry(map, &regulator_map_list, list) {
  909. /* If the mapping has a device set up it must match */
  910. if (map->dev_name &&
  911. (!devname || strcmp(map->dev_name, devname)))
  912. continue;
  913. if (strcmp(map->supply, id) == 0) {
  914. rdev = map->regulator;
  915. goto found;
  916. }
  917. }
  918. mutex_unlock(&regulator_list_mutex);
  919. return regulator;
  920. found:
  921. if (!try_module_get(rdev->owner))
  922. goto out;
  923. regulator = create_regulator(rdev, dev, id);
  924. if (regulator == NULL) {
  925. regulator = ERR_PTR(-ENOMEM);
  926. module_put(rdev->owner);
  927. }
  928. out:
  929. mutex_unlock(&regulator_list_mutex);
  930. return regulator;
  931. }
  932. EXPORT_SYMBOL_GPL(regulator_get);
  933. /**
  934. * regulator_put - "free" the regulator source
  935. * @regulator: regulator source
  936. *
  937. * Note: drivers must ensure that all regulator_enable calls made on this
  938. * regulator source are balanced by regulator_disable calls prior to calling
  939. * this function.
  940. */
  941. void regulator_put(struct regulator *regulator)
  942. {
  943. struct regulator_dev *rdev;
  944. if (regulator == NULL || IS_ERR(regulator))
  945. return;
  946. mutex_lock(&regulator_list_mutex);
  947. rdev = regulator->rdev;
  948. /* remove any sysfs entries */
  949. if (regulator->dev) {
  950. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  951. kfree(regulator->supply_name);
  952. device_remove_file(regulator->dev, &regulator->dev_attr);
  953. kfree(regulator->dev_attr.attr.name);
  954. }
  955. list_del(&regulator->list);
  956. kfree(regulator);
  957. module_put(rdev->owner);
  958. mutex_unlock(&regulator_list_mutex);
  959. }
  960. EXPORT_SYMBOL_GPL(regulator_put);
  961. /* locks held by regulator_enable() */
  962. static int _regulator_enable(struct regulator_dev *rdev)
  963. {
  964. int ret = -EINVAL;
  965. if (!rdev->constraints) {
  966. printk(KERN_ERR "%s: %s has no constraints\n",
  967. __func__, rdev->desc->name);
  968. return ret;
  969. }
  970. /* do we need to enable the supply regulator first */
  971. if (rdev->supply) {
  972. ret = _regulator_enable(rdev->supply);
  973. if (ret < 0) {
  974. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  975. __func__, rdev->desc->name, ret);
  976. return ret;
  977. }
  978. }
  979. /* check voltage and requested load before enabling */
  980. if (rdev->desc->ops->enable) {
  981. if (rdev->constraints &&
  982. (rdev->constraints->valid_ops_mask &
  983. REGULATOR_CHANGE_DRMS))
  984. drms_uA_update(rdev);
  985. ret = rdev->desc->ops->enable(rdev);
  986. if (ret < 0) {
  987. printk(KERN_ERR "%s: failed to enable %s: %d\n",
  988. __func__, rdev->desc->name, ret);
  989. return ret;
  990. }
  991. rdev->use_count++;
  992. return ret;
  993. }
  994. return ret;
  995. }
  996. /**
  997. * regulator_enable - enable regulator output
  998. * @regulator: regulator source
  999. *
  1000. * Request that the regulator be enabled with the regulator output at
  1001. * the predefined voltage or current value. Calls to regulator_enable()
  1002. * must be balanced with calls to regulator_disable().
  1003. *
  1004. * NOTE: the output value can be set by other drivers, boot loader or may be
  1005. * hardwired in the regulator.
  1006. */
  1007. int regulator_enable(struct regulator *regulator)
  1008. {
  1009. struct regulator_dev *rdev = regulator->rdev;
  1010. int ret = 0;
  1011. mutex_lock(&rdev->mutex);
  1012. ret = _regulator_enable(rdev);
  1013. mutex_unlock(&rdev->mutex);
  1014. return ret;
  1015. }
  1016. EXPORT_SYMBOL_GPL(regulator_enable);
  1017. /* locks held by regulator_disable() */
  1018. static int _regulator_disable(struct regulator_dev *rdev)
  1019. {
  1020. int ret = 0;
  1021. if (WARN(rdev->use_count <= 0,
  1022. "unbalanced disables for %s\n",
  1023. rdev->desc->name))
  1024. return -EIO;
  1025. /* are we the last user and permitted to disable ? */
  1026. if (rdev->use_count == 1 && !rdev->constraints->always_on) {
  1027. /* we are last user */
  1028. if (rdev->desc->ops->disable) {
  1029. ret = rdev->desc->ops->disable(rdev);
  1030. if (ret < 0) {
  1031. printk(KERN_ERR "%s: failed to disable %s\n",
  1032. __func__, rdev->desc->name);
  1033. return ret;
  1034. }
  1035. }
  1036. /* decrease our supplies ref count and disable if required */
  1037. if (rdev->supply)
  1038. _regulator_disable(rdev->supply);
  1039. rdev->use_count = 0;
  1040. } else if (rdev->use_count > 1) {
  1041. if (rdev->constraints &&
  1042. (rdev->constraints->valid_ops_mask &
  1043. REGULATOR_CHANGE_DRMS))
  1044. drms_uA_update(rdev);
  1045. rdev->use_count--;
  1046. }
  1047. return ret;
  1048. }
  1049. /**
  1050. * regulator_disable - disable regulator output
  1051. * @regulator: regulator source
  1052. *
  1053. * Disable the regulator output voltage or current. Calls to
  1054. * regulator_enable() must be balanced with calls to
  1055. * regulator_disable().
  1056. *
  1057. * NOTE: this will only disable the regulator output if no other consumer
  1058. * devices have it enabled, the regulator device supports disabling and
  1059. * machine constraints permit this operation.
  1060. */
  1061. int regulator_disable(struct regulator *regulator)
  1062. {
  1063. struct regulator_dev *rdev = regulator->rdev;
  1064. int ret = 0;
  1065. mutex_lock(&rdev->mutex);
  1066. ret = _regulator_disable(rdev);
  1067. mutex_unlock(&rdev->mutex);
  1068. return ret;
  1069. }
  1070. EXPORT_SYMBOL_GPL(regulator_disable);
  1071. /* locks held by regulator_force_disable() */
  1072. static int _regulator_force_disable(struct regulator_dev *rdev)
  1073. {
  1074. int ret = 0;
  1075. /* force disable */
  1076. if (rdev->desc->ops->disable) {
  1077. /* ah well, who wants to live forever... */
  1078. ret = rdev->desc->ops->disable(rdev);
  1079. if (ret < 0) {
  1080. printk(KERN_ERR "%s: failed to force disable %s\n",
  1081. __func__, rdev->desc->name);
  1082. return ret;
  1083. }
  1084. /* notify other consumers that power has been forced off */
  1085. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
  1086. NULL);
  1087. }
  1088. /* decrease our supplies ref count and disable if required */
  1089. if (rdev->supply)
  1090. _regulator_disable(rdev->supply);
  1091. rdev->use_count = 0;
  1092. return ret;
  1093. }
  1094. /**
  1095. * regulator_force_disable - force disable regulator output
  1096. * @regulator: regulator source
  1097. *
  1098. * Forcibly disable the regulator output voltage or current.
  1099. * NOTE: this *will* disable the regulator output even if other consumer
  1100. * devices have it enabled. This should be used for situations when device
  1101. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1102. */
  1103. int regulator_force_disable(struct regulator *regulator)
  1104. {
  1105. int ret;
  1106. mutex_lock(&regulator->rdev->mutex);
  1107. regulator->uA_load = 0;
  1108. ret = _regulator_force_disable(regulator->rdev);
  1109. mutex_unlock(&regulator->rdev->mutex);
  1110. return ret;
  1111. }
  1112. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1113. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1114. {
  1115. int ret;
  1116. mutex_lock(&rdev->mutex);
  1117. /* sanity check */
  1118. if (!rdev->desc->ops->is_enabled) {
  1119. ret = -EINVAL;
  1120. goto out;
  1121. }
  1122. ret = rdev->desc->ops->is_enabled(rdev);
  1123. out:
  1124. mutex_unlock(&rdev->mutex);
  1125. return ret;
  1126. }
  1127. /**
  1128. * regulator_is_enabled - is the regulator output enabled
  1129. * @regulator: regulator source
  1130. *
  1131. * Returns positive if the regulator driver backing the source/client
  1132. * has requested that the device be enabled, zero if it hasn't, else a
  1133. * negative errno code.
  1134. *
  1135. * Note that the device backing this regulator handle can have multiple
  1136. * users, so it might be enabled even if regulator_enable() was never
  1137. * called for this particular source.
  1138. */
  1139. int regulator_is_enabled(struct regulator *regulator)
  1140. {
  1141. return _regulator_is_enabled(regulator->rdev);
  1142. }
  1143. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1144. /**
  1145. * regulator_count_voltages - count regulator_list_voltage() selectors
  1146. * @regulator: regulator source
  1147. *
  1148. * Returns number of selectors, or negative errno. Selectors are
  1149. * numbered starting at zero, and typically correspond to bitfields
  1150. * in hardware registers.
  1151. */
  1152. int regulator_count_voltages(struct regulator *regulator)
  1153. {
  1154. struct regulator_dev *rdev = regulator->rdev;
  1155. return rdev->desc->n_voltages ? : -EINVAL;
  1156. }
  1157. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1158. /**
  1159. * regulator_list_voltage - enumerate supported voltages
  1160. * @regulator: regulator source
  1161. * @selector: identify voltage to list
  1162. * Context: can sleep
  1163. *
  1164. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1165. * zero if this selector code can't be used on this sytem, or a
  1166. * negative errno.
  1167. */
  1168. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1169. {
  1170. struct regulator_dev *rdev = regulator->rdev;
  1171. struct regulator_ops *ops = rdev->desc->ops;
  1172. int ret;
  1173. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1174. return -EINVAL;
  1175. mutex_lock(&rdev->mutex);
  1176. ret = ops->list_voltage(rdev, selector);
  1177. mutex_unlock(&rdev->mutex);
  1178. if (ret > 0) {
  1179. if (ret < rdev->constraints->min_uV)
  1180. ret = 0;
  1181. else if (ret > rdev->constraints->max_uV)
  1182. ret = 0;
  1183. }
  1184. return ret;
  1185. }
  1186. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1187. /**
  1188. * regulator_set_voltage - set regulator output voltage
  1189. * @regulator: regulator source
  1190. * @min_uV: Minimum required voltage in uV
  1191. * @max_uV: Maximum acceptable voltage in uV
  1192. *
  1193. * Sets a voltage regulator to the desired output voltage. This can be set
  1194. * during any regulator state. IOW, regulator can be disabled or enabled.
  1195. *
  1196. * If the regulator is enabled then the voltage will change to the new value
  1197. * immediately otherwise if the regulator is disabled the regulator will
  1198. * output at the new voltage when enabled.
  1199. *
  1200. * NOTE: If the regulator is shared between several devices then the lowest
  1201. * request voltage that meets the system constraints will be used.
  1202. * Regulator system constraints must be set for this regulator before
  1203. * calling this function otherwise this call will fail.
  1204. */
  1205. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1206. {
  1207. struct regulator_dev *rdev = regulator->rdev;
  1208. int ret;
  1209. mutex_lock(&rdev->mutex);
  1210. /* sanity check */
  1211. if (!rdev->desc->ops->set_voltage) {
  1212. ret = -EINVAL;
  1213. goto out;
  1214. }
  1215. /* constraints check */
  1216. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1217. if (ret < 0)
  1218. goto out;
  1219. regulator->min_uV = min_uV;
  1220. regulator->max_uV = max_uV;
  1221. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
  1222. out:
  1223. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
  1224. mutex_unlock(&rdev->mutex);
  1225. return ret;
  1226. }
  1227. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1228. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1229. {
  1230. /* sanity check */
  1231. if (rdev->desc->ops->get_voltage)
  1232. return rdev->desc->ops->get_voltage(rdev);
  1233. else
  1234. return -EINVAL;
  1235. }
  1236. /**
  1237. * regulator_get_voltage - get regulator output voltage
  1238. * @regulator: regulator source
  1239. *
  1240. * This returns the current regulator voltage in uV.
  1241. *
  1242. * NOTE: If the regulator is disabled it will return the voltage value. This
  1243. * function should not be used to determine regulator state.
  1244. */
  1245. int regulator_get_voltage(struct regulator *regulator)
  1246. {
  1247. int ret;
  1248. mutex_lock(&regulator->rdev->mutex);
  1249. ret = _regulator_get_voltage(regulator->rdev);
  1250. mutex_unlock(&regulator->rdev->mutex);
  1251. return ret;
  1252. }
  1253. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1254. /**
  1255. * regulator_set_current_limit - set regulator output current limit
  1256. * @regulator: regulator source
  1257. * @min_uA: Minimuum supported current in uA
  1258. * @max_uA: Maximum supported current in uA
  1259. *
  1260. * Sets current sink to the desired output current. This can be set during
  1261. * any regulator state. IOW, regulator can be disabled or enabled.
  1262. *
  1263. * If the regulator is enabled then the current will change to the new value
  1264. * immediately otherwise if the regulator is disabled the regulator will
  1265. * output at the new current when enabled.
  1266. *
  1267. * NOTE: Regulator system constraints must be set for this regulator before
  1268. * calling this function otherwise this call will fail.
  1269. */
  1270. int regulator_set_current_limit(struct regulator *regulator,
  1271. int min_uA, int max_uA)
  1272. {
  1273. struct regulator_dev *rdev = regulator->rdev;
  1274. int ret;
  1275. mutex_lock(&rdev->mutex);
  1276. /* sanity check */
  1277. if (!rdev->desc->ops->set_current_limit) {
  1278. ret = -EINVAL;
  1279. goto out;
  1280. }
  1281. /* constraints check */
  1282. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1283. if (ret < 0)
  1284. goto out;
  1285. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1286. out:
  1287. mutex_unlock(&rdev->mutex);
  1288. return ret;
  1289. }
  1290. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1291. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1292. {
  1293. int ret;
  1294. mutex_lock(&rdev->mutex);
  1295. /* sanity check */
  1296. if (!rdev->desc->ops->get_current_limit) {
  1297. ret = -EINVAL;
  1298. goto out;
  1299. }
  1300. ret = rdev->desc->ops->get_current_limit(rdev);
  1301. out:
  1302. mutex_unlock(&rdev->mutex);
  1303. return ret;
  1304. }
  1305. /**
  1306. * regulator_get_current_limit - get regulator output current
  1307. * @regulator: regulator source
  1308. *
  1309. * This returns the current supplied by the specified current sink in uA.
  1310. *
  1311. * NOTE: If the regulator is disabled it will return the current value. This
  1312. * function should not be used to determine regulator state.
  1313. */
  1314. int regulator_get_current_limit(struct regulator *regulator)
  1315. {
  1316. return _regulator_get_current_limit(regulator->rdev);
  1317. }
  1318. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1319. /**
  1320. * regulator_set_mode - set regulator operating mode
  1321. * @regulator: regulator source
  1322. * @mode: operating mode - one of the REGULATOR_MODE constants
  1323. *
  1324. * Set regulator operating mode to increase regulator efficiency or improve
  1325. * regulation performance.
  1326. *
  1327. * NOTE: Regulator system constraints must be set for this regulator before
  1328. * calling this function otherwise this call will fail.
  1329. */
  1330. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1331. {
  1332. struct regulator_dev *rdev = regulator->rdev;
  1333. int ret;
  1334. mutex_lock(&rdev->mutex);
  1335. /* sanity check */
  1336. if (!rdev->desc->ops->set_mode) {
  1337. ret = -EINVAL;
  1338. goto out;
  1339. }
  1340. /* constraints check */
  1341. ret = regulator_check_mode(rdev, mode);
  1342. if (ret < 0)
  1343. goto out;
  1344. ret = rdev->desc->ops->set_mode(rdev, mode);
  1345. out:
  1346. mutex_unlock(&rdev->mutex);
  1347. return ret;
  1348. }
  1349. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1350. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1351. {
  1352. int ret;
  1353. mutex_lock(&rdev->mutex);
  1354. /* sanity check */
  1355. if (!rdev->desc->ops->get_mode) {
  1356. ret = -EINVAL;
  1357. goto out;
  1358. }
  1359. ret = rdev->desc->ops->get_mode(rdev);
  1360. out:
  1361. mutex_unlock(&rdev->mutex);
  1362. return ret;
  1363. }
  1364. /**
  1365. * regulator_get_mode - get regulator operating mode
  1366. * @regulator: regulator source
  1367. *
  1368. * Get the current regulator operating mode.
  1369. */
  1370. unsigned int regulator_get_mode(struct regulator *regulator)
  1371. {
  1372. return _regulator_get_mode(regulator->rdev);
  1373. }
  1374. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1375. /**
  1376. * regulator_set_optimum_mode - set regulator optimum operating mode
  1377. * @regulator: regulator source
  1378. * @uA_load: load current
  1379. *
  1380. * Notifies the regulator core of a new device load. This is then used by
  1381. * DRMS (if enabled by constraints) to set the most efficient regulator
  1382. * operating mode for the new regulator loading.
  1383. *
  1384. * Consumer devices notify their supply regulator of the maximum power
  1385. * they will require (can be taken from device datasheet in the power
  1386. * consumption tables) when they change operational status and hence power
  1387. * state. Examples of operational state changes that can affect power
  1388. * consumption are :-
  1389. *
  1390. * o Device is opened / closed.
  1391. * o Device I/O is about to begin or has just finished.
  1392. * o Device is idling in between work.
  1393. *
  1394. * This information is also exported via sysfs to userspace.
  1395. *
  1396. * DRMS will sum the total requested load on the regulator and change
  1397. * to the most efficient operating mode if platform constraints allow.
  1398. *
  1399. * Returns the new regulator mode or error.
  1400. */
  1401. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1402. {
  1403. struct regulator_dev *rdev = regulator->rdev;
  1404. struct regulator *consumer;
  1405. int ret, output_uV, input_uV, total_uA_load = 0;
  1406. unsigned int mode;
  1407. mutex_lock(&rdev->mutex);
  1408. regulator->uA_load = uA_load;
  1409. ret = regulator_check_drms(rdev);
  1410. if (ret < 0)
  1411. goto out;
  1412. ret = -EINVAL;
  1413. /* sanity check */
  1414. if (!rdev->desc->ops->get_optimum_mode)
  1415. goto out;
  1416. /* get output voltage */
  1417. output_uV = rdev->desc->ops->get_voltage(rdev);
  1418. if (output_uV <= 0) {
  1419. printk(KERN_ERR "%s: invalid output voltage found for %s\n",
  1420. __func__, rdev->desc->name);
  1421. goto out;
  1422. }
  1423. /* get input voltage */
  1424. if (rdev->supply && rdev->supply->desc->ops->get_voltage)
  1425. input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
  1426. else
  1427. input_uV = rdev->constraints->input_uV;
  1428. if (input_uV <= 0) {
  1429. printk(KERN_ERR "%s: invalid input voltage found for %s\n",
  1430. __func__, rdev->desc->name);
  1431. goto out;
  1432. }
  1433. /* calc total requested load for this regulator */
  1434. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1435. total_uA_load += consumer->uA_load;
  1436. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1437. input_uV, output_uV,
  1438. total_uA_load);
  1439. ret = regulator_check_mode(rdev, mode);
  1440. if (ret < 0) {
  1441. printk(KERN_ERR "%s: failed to get optimum mode for %s @"
  1442. " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
  1443. total_uA_load, input_uV, output_uV);
  1444. goto out;
  1445. }
  1446. ret = rdev->desc->ops->set_mode(rdev, mode);
  1447. if (ret < 0) {
  1448. printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
  1449. __func__, mode, rdev->desc->name);
  1450. goto out;
  1451. }
  1452. ret = mode;
  1453. out:
  1454. mutex_unlock(&rdev->mutex);
  1455. return ret;
  1456. }
  1457. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1458. /**
  1459. * regulator_register_notifier - register regulator event notifier
  1460. * @regulator: regulator source
  1461. * @nb: notifier block
  1462. *
  1463. * Register notifier block to receive regulator events.
  1464. */
  1465. int regulator_register_notifier(struct regulator *regulator,
  1466. struct notifier_block *nb)
  1467. {
  1468. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1469. nb);
  1470. }
  1471. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1472. /**
  1473. * regulator_unregister_notifier - unregister regulator event notifier
  1474. * @regulator: regulator source
  1475. * @nb: notifier block
  1476. *
  1477. * Unregister regulator event notifier block.
  1478. */
  1479. int regulator_unregister_notifier(struct regulator *regulator,
  1480. struct notifier_block *nb)
  1481. {
  1482. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1483. nb);
  1484. }
  1485. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1486. /* notify regulator consumers and downstream regulator consumers.
  1487. * Note mutex must be held by caller.
  1488. */
  1489. static void _notifier_call_chain(struct regulator_dev *rdev,
  1490. unsigned long event, void *data)
  1491. {
  1492. struct regulator_dev *_rdev;
  1493. /* call rdev chain first */
  1494. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1495. /* now notify regulator we supply */
  1496. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1497. mutex_lock(&_rdev->mutex);
  1498. _notifier_call_chain(_rdev, event, data);
  1499. mutex_unlock(&_rdev->mutex);
  1500. }
  1501. }
  1502. /**
  1503. * regulator_bulk_get - get multiple regulator consumers
  1504. *
  1505. * @dev: Device to supply
  1506. * @num_consumers: Number of consumers to register
  1507. * @consumers: Configuration of consumers; clients are stored here.
  1508. *
  1509. * @return 0 on success, an errno on failure.
  1510. *
  1511. * This helper function allows drivers to get several regulator
  1512. * consumers in one operation. If any of the regulators cannot be
  1513. * acquired then any regulators that were allocated will be freed
  1514. * before returning to the caller.
  1515. */
  1516. int regulator_bulk_get(struct device *dev, int num_consumers,
  1517. struct regulator_bulk_data *consumers)
  1518. {
  1519. int i;
  1520. int ret;
  1521. for (i = 0; i < num_consumers; i++)
  1522. consumers[i].consumer = NULL;
  1523. for (i = 0; i < num_consumers; i++) {
  1524. consumers[i].consumer = regulator_get(dev,
  1525. consumers[i].supply);
  1526. if (IS_ERR(consumers[i].consumer)) {
  1527. dev_err(dev, "Failed to get supply '%s'\n",
  1528. consumers[i].supply);
  1529. ret = PTR_ERR(consumers[i].consumer);
  1530. consumers[i].consumer = NULL;
  1531. goto err;
  1532. }
  1533. }
  1534. return 0;
  1535. err:
  1536. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1537. regulator_put(consumers[i].consumer);
  1538. return ret;
  1539. }
  1540. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1541. /**
  1542. * regulator_bulk_enable - enable multiple regulator consumers
  1543. *
  1544. * @num_consumers: Number of consumers
  1545. * @consumers: Consumer data; clients are stored here.
  1546. * @return 0 on success, an errno on failure
  1547. *
  1548. * This convenience API allows consumers to enable multiple regulator
  1549. * clients in a single API call. If any consumers cannot be enabled
  1550. * then any others that were enabled will be disabled again prior to
  1551. * return.
  1552. */
  1553. int regulator_bulk_enable(int num_consumers,
  1554. struct regulator_bulk_data *consumers)
  1555. {
  1556. int i;
  1557. int ret;
  1558. for (i = 0; i < num_consumers; i++) {
  1559. ret = regulator_enable(consumers[i].consumer);
  1560. if (ret != 0)
  1561. goto err;
  1562. }
  1563. return 0;
  1564. err:
  1565. printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
  1566. for (i = 0; i < num_consumers; i++)
  1567. regulator_disable(consumers[i].consumer);
  1568. return ret;
  1569. }
  1570. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1571. /**
  1572. * regulator_bulk_disable - disable multiple regulator consumers
  1573. *
  1574. * @num_consumers: Number of consumers
  1575. * @consumers: Consumer data; clients are stored here.
  1576. * @return 0 on success, an errno on failure
  1577. *
  1578. * This convenience API allows consumers to disable multiple regulator
  1579. * clients in a single API call. If any consumers cannot be enabled
  1580. * then any others that were disabled will be disabled again prior to
  1581. * return.
  1582. */
  1583. int regulator_bulk_disable(int num_consumers,
  1584. struct regulator_bulk_data *consumers)
  1585. {
  1586. int i;
  1587. int ret;
  1588. for (i = 0; i < num_consumers; i++) {
  1589. ret = regulator_disable(consumers[i].consumer);
  1590. if (ret != 0)
  1591. goto err;
  1592. }
  1593. return 0;
  1594. err:
  1595. printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
  1596. for (i = 0; i < num_consumers; i++)
  1597. regulator_enable(consumers[i].consumer);
  1598. return ret;
  1599. }
  1600. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1601. /**
  1602. * regulator_bulk_free - free multiple regulator consumers
  1603. *
  1604. * @num_consumers: Number of consumers
  1605. * @consumers: Consumer data; clients are stored here.
  1606. *
  1607. * This convenience API allows consumers to free multiple regulator
  1608. * clients in a single API call.
  1609. */
  1610. void regulator_bulk_free(int num_consumers,
  1611. struct regulator_bulk_data *consumers)
  1612. {
  1613. int i;
  1614. for (i = 0; i < num_consumers; i++) {
  1615. regulator_put(consumers[i].consumer);
  1616. consumers[i].consumer = NULL;
  1617. }
  1618. }
  1619. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  1620. /**
  1621. * regulator_notifier_call_chain - call regulator event notifier
  1622. * @rdev: regulator source
  1623. * @event: notifier block
  1624. * @data: callback-specific data.
  1625. *
  1626. * Called by regulator drivers to notify clients a regulator event has
  1627. * occurred. We also notify regulator clients downstream.
  1628. * Note lock must be held by caller.
  1629. */
  1630. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  1631. unsigned long event, void *data)
  1632. {
  1633. _notifier_call_chain(rdev, event, data);
  1634. return NOTIFY_DONE;
  1635. }
  1636. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  1637. /**
  1638. * regulator_mode_to_status - convert a regulator mode into a status
  1639. *
  1640. * @mode: Mode to convert
  1641. *
  1642. * Convert a regulator mode into a status.
  1643. */
  1644. int regulator_mode_to_status(unsigned int mode)
  1645. {
  1646. switch (mode) {
  1647. case REGULATOR_MODE_FAST:
  1648. return REGULATOR_STATUS_FAST;
  1649. case REGULATOR_MODE_NORMAL:
  1650. return REGULATOR_STATUS_NORMAL;
  1651. case REGULATOR_MODE_IDLE:
  1652. return REGULATOR_STATUS_IDLE;
  1653. case REGULATOR_STATUS_STANDBY:
  1654. return REGULATOR_STATUS_STANDBY;
  1655. default:
  1656. return 0;
  1657. }
  1658. }
  1659. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  1660. /*
  1661. * To avoid cluttering sysfs (and memory) with useless state, only
  1662. * create attributes that can be meaningfully displayed.
  1663. */
  1664. static int add_regulator_attributes(struct regulator_dev *rdev)
  1665. {
  1666. struct device *dev = &rdev->dev;
  1667. struct regulator_ops *ops = rdev->desc->ops;
  1668. int status = 0;
  1669. /* some attributes need specific methods to be displayed */
  1670. if (ops->get_voltage) {
  1671. status = device_create_file(dev, &dev_attr_microvolts);
  1672. if (status < 0)
  1673. return status;
  1674. }
  1675. if (ops->get_current_limit) {
  1676. status = device_create_file(dev, &dev_attr_microamps);
  1677. if (status < 0)
  1678. return status;
  1679. }
  1680. if (ops->get_mode) {
  1681. status = device_create_file(dev, &dev_attr_opmode);
  1682. if (status < 0)
  1683. return status;
  1684. }
  1685. if (ops->is_enabled) {
  1686. status = device_create_file(dev, &dev_attr_state);
  1687. if (status < 0)
  1688. return status;
  1689. }
  1690. if (ops->get_status) {
  1691. status = device_create_file(dev, &dev_attr_status);
  1692. if (status < 0)
  1693. return status;
  1694. }
  1695. /* some attributes are type-specific */
  1696. if (rdev->desc->type == REGULATOR_CURRENT) {
  1697. status = device_create_file(dev, &dev_attr_requested_microamps);
  1698. if (status < 0)
  1699. return status;
  1700. }
  1701. /* all the other attributes exist to support constraints;
  1702. * don't show them if there are no constraints, or if the
  1703. * relevant supporting methods are missing.
  1704. */
  1705. if (!rdev->constraints)
  1706. return status;
  1707. /* constraints need specific supporting methods */
  1708. if (ops->set_voltage) {
  1709. status = device_create_file(dev, &dev_attr_min_microvolts);
  1710. if (status < 0)
  1711. return status;
  1712. status = device_create_file(dev, &dev_attr_max_microvolts);
  1713. if (status < 0)
  1714. return status;
  1715. }
  1716. if (ops->set_current_limit) {
  1717. status = device_create_file(dev, &dev_attr_min_microamps);
  1718. if (status < 0)
  1719. return status;
  1720. status = device_create_file(dev, &dev_attr_max_microamps);
  1721. if (status < 0)
  1722. return status;
  1723. }
  1724. /* suspend mode constraints need multiple supporting methods */
  1725. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  1726. return status;
  1727. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  1728. if (status < 0)
  1729. return status;
  1730. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  1731. if (status < 0)
  1732. return status;
  1733. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  1734. if (status < 0)
  1735. return status;
  1736. if (ops->set_suspend_voltage) {
  1737. status = device_create_file(dev,
  1738. &dev_attr_suspend_standby_microvolts);
  1739. if (status < 0)
  1740. return status;
  1741. status = device_create_file(dev,
  1742. &dev_attr_suspend_mem_microvolts);
  1743. if (status < 0)
  1744. return status;
  1745. status = device_create_file(dev,
  1746. &dev_attr_suspend_disk_microvolts);
  1747. if (status < 0)
  1748. return status;
  1749. }
  1750. if (ops->set_suspend_mode) {
  1751. status = device_create_file(dev,
  1752. &dev_attr_suspend_standby_mode);
  1753. if (status < 0)
  1754. return status;
  1755. status = device_create_file(dev,
  1756. &dev_attr_suspend_mem_mode);
  1757. if (status < 0)
  1758. return status;
  1759. status = device_create_file(dev,
  1760. &dev_attr_suspend_disk_mode);
  1761. if (status < 0)
  1762. return status;
  1763. }
  1764. return status;
  1765. }
  1766. /**
  1767. * regulator_register - register regulator
  1768. * @regulator_desc: regulator to register
  1769. * @dev: struct device for the regulator
  1770. * @init_data: platform provided init data, passed through by driver
  1771. * @driver_data: private regulator data
  1772. *
  1773. * Called by regulator drivers to register a regulator.
  1774. * Returns 0 on success.
  1775. */
  1776. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  1777. struct device *dev, struct regulator_init_data *init_data,
  1778. void *driver_data)
  1779. {
  1780. static atomic_t regulator_no = ATOMIC_INIT(0);
  1781. struct regulator_dev *rdev;
  1782. int ret, i;
  1783. if (regulator_desc == NULL)
  1784. return ERR_PTR(-EINVAL);
  1785. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  1786. return ERR_PTR(-EINVAL);
  1787. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  1788. regulator_desc->type != REGULATOR_CURRENT)
  1789. return ERR_PTR(-EINVAL);
  1790. if (!init_data)
  1791. return ERR_PTR(-EINVAL);
  1792. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  1793. if (rdev == NULL)
  1794. return ERR_PTR(-ENOMEM);
  1795. mutex_lock(&regulator_list_mutex);
  1796. mutex_init(&rdev->mutex);
  1797. rdev->reg_data = driver_data;
  1798. rdev->owner = regulator_desc->owner;
  1799. rdev->desc = regulator_desc;
  1800. INIT_LIST_HEAD(&rdev->consumer_list);
  1801. INIT_LIST_HEAD(&rdev->supply_list);
  1802. INIT_LIST_HEAD(&rdev->list);
  1803. INIT_LIST_HEAD(&rdev->slist);
  1804. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  1805. /* preform any regulator specific init */
  1806. if (init_data->regulator_init) {
  1807. ret = init_data->regulator_init(rdev->reg_data);
  1808. if (ret < 0)
  1809. goto clean;
  1810. }
  1811. /* register with sysfs */
  1812. rdev->dev.class = &regulator_class;
  1813. rdev->dev.parent = dev;
  1814. dev_set_name(&rdev->dev, "regulator.%d",
  1815. atomic_inc_return(&regulator_no) - 1);
  1816. ret = device_register(&rdev->dev);
  1817. if (ret != 0)
  1818. goto clean;
  1819. dev_set_drvdata(&rdev->dev, rdev);
  1820. /* set regulator constraints */
  1821. ret = set_machine_constraints(rdev, &init_data->constraints);
  1822. if (ret < 0)
  1823. goto scrub;
  1824. /* add attributes supported by this regulator */
  1825. ret = add_regulator_attributes(rdev);
  1826. if (ret < 0)
  1827. goto scrub;
  1828. /* set supply regulator if it exists */
  1829. if (init_data->supply_regulator_dev) {
  1830. ret = set_supply(rdev,
  1831. dev_get_drvdata(init_data->supply_regulator_dev));
  1832. if (ret < 0)
  1833. goto scrub;
  1834. }
  1835. /* add consumers devices */
  1836. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  1837. ret = set_consumer_device_supply(rdev,
  1838. init_data->consumer_supplies[i].dev,
  1839. init_data->consumer_supplies[i].dev_name,
  1840. init_data->consumer_supplies[i].supply);
  1841. if (ret < 0) {
  1842. for (--i; i >= 0; i--)
  1843. unset_consumer_device_supply(rdev,
  1844. init_data->consumer_supplies[i].dev_name,
  1845. init_data->consumer_supplies[i].dev);
  1846. goto scrub;
  1847. }
  1848. }
  1849. list_add(&rdev->list, &regulator_list);
  1850. out:
  1851. mutex_unlock(&regulator_list_mutex);
  1852. return rdev;
  1853. scrub:
  1854. device_unregister(&rdev->dev);
  1855. /* device core frees rdev */
  1856. rdev = ERR_PTR(ret);
  1857. goto out;
  1858. clean:
  1859. kfree(rdev);
  1860. rdev = ERR_PTR(ret);
  1861. goto out;
  1862. }
  1863. EXPORT_SYMBOL_GPL(regulator_register);
  1864. /**
  1865. * regulator_unregister - unregister regulator
  1866. * @rdev: regulator to unregister
  1867. *
  1868. * Called by regulator drivers to unregister a regulator.
  1869. */
  1870. void regulator_unregister(struct regulator_dev *rdev)
  1871. {
  1872. if (rdev == NULL)
  1873. return;
  1874. mutex_lock(&regulator_list_mutex);
  1875. unset_regulator_supplies(rdev);
  1876. list_del(&rdev->list);
  1877. if (rdev->supply)
  1878. sysfs_remove_link(&rdev->dev.kobj, "supply");
  1879. device_unregister(&rdev->dev);
  1880. mutex_unlock(&regulator_list_mutex);
  1881. }
  1882. EXPORT_SYMBOL_GPL(regulator_unregister);
  1883. /**
  1884. * regulator_suspend_prepare - prepare regulators for system wide suspend
  1885. * @state: system suspend state
  1886. *
  1887. * Configure each regulator with it's suspend operating parameters for state.
  1888. * This will usually be called by machine suspend code prior to supending.
  1889. */
  1890. int regulator_suspend_prepare(suspend_state_t state)
  1891. {
  1892. struct regulator_dev *rdev;
  1893. int ret = 0;
  1894. /* ON is handled by regulator active state */
  1895. if (state == PM_SUSPEND_ON)
  1896. return -EINVAL;
  1897. mutex_lock(&regulator_list_mutex);
  1898. list_for_each_entry(rdev, &regulator_list, list) {
  1899. mutex_lock(&rdev->mutex);
  1900. ret = suspend_prepare(rdev, state);
  1901. mutex_unlock(&rdev->mutex);
  1902. if (ret < 0) {
  1903. printk(KERN_ERR "%s: failed to prepare %s\n",
  1904. __func__, rdev->desc->name);
  1905. goto out;
  1906. }
  1907. }
  1908. out:
  1909. mutex_unlock(&regulator_list_mutex);
  1910. return ret;
  1911. }
  1912. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  1913. /**
  1914. * regulator_has_full_constraints - the system has fully specified constraints
  1915. *
  1916. * Calling this function will cause the regulator API to disable all
  1917. * regulators which have a zero use count and don't have an always_on
  1918. * constraint in a late_initcall.
  1919. *
  1920. * The intention is that this will become the default behaviour in a
  1921. * future kernel release so users are encouraged to use this facility
  1922. * now.
  1923. */
  1924. void regulator_has_full_constraints(void)
  1925. {
  1926. has_full_constraints = 1;
  1927. }
  1928. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  1929. /**
  1930. * rdev_get_drvdata - get rdev regulator driver data
  1931. * @rdev: regulator
  1932. *
  1933. * Get rdev regulator driver private data. This call can be used in the
  1934. * regulator driver context.
  1935. */
  1936. void *rdev_get_drvdata(struct regulator_dev *rdev)
  1937. {
  1938. return rdev->reg_data;
  1939. }
  1940. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  1941. /**
  1942. * regulator_get_drvdata - get regulator driver data
  1943. * @regulator: regulator
  1944. *
  1945. * Get regulator driver private data. This call can be used in the consumer
  1946. * driver context when non API regulator specific functions need to be called.
  1947. */
  1948. void *regulator_get_drvdata(struct regulator *regulator)
  1949. {
  1950. return regulator->rdev->reg_data;
  1951. }
  1952. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  1953. /**
  1954. * regulator_set_drvdata - set regulator driver data
  1955. * @regulator: regulator
  1956. * @data: data
  1957. */
  1958. void regulator_set_drvdata(struct regulator *regulator, void *data)
  1959. {
  1960. regulator->rdev->reg_data = data;
  1961. }
  1962. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  1963. /**
  1964. * regulator_get_id - get regulator ID
  1965. * @rdev: regulator
  1966. */
  1967. int rdev_get_id(struct regulator_dev *rdev)
  1968. {
  1969. return rdev->desc->id;
  1970. }
  1971. EXPORT_SYMBOL_GPL(rdev_get_id);
  1972. struct device *rdev_get_dev(struct regulator_dev *rdev)
  1973. {
  1974. return &rdev->dev;
  1975. }
  1976. EXPORT_SYMBOL_GPL(rdev_get_dev);
  1977. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  1978. {
  1979. return reg_init_data->driver_data;
  1980. }
  1981. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  1982. static int __init regulator_init(void)
  1983. {
  1984. printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
  1985. return class_register(&regulator_class);
  1986. }
  1987. /* init early to allow our consumers to complete system booting */
  1988. core_initcall(regulator_init);
  1989. static int __init regulator_init_complete(void)
  1990. {
  1991. struct regulator_dev *rdev;
  1992. struct regulator_ops *ops;
  1993. struct regulation_constraints *c;
  1994. int enabled, ret;
  1995. const char *name;
  1996. mutex_lock(&regulator_list_mutex);
  1997. /* If we have a full configuration then disable any regulators
  1998. * which are not in use or always_on. This will become the
  1999. * default behaviour in the future.
  2000. */
  2001. list_for_each_entry(rdev, &regulator_list, list) {
  2002. ops = rdev->desc->ops;
  2003. c = rdev->constraints;
  2004. if (c->name)
  2005. name = c->name;
  2006. else if (rdev->desc->name)
  2007. name = rdev->desc->name;
  2008. else
  2009. name = "regulator";
  2010. if (!ops->disable || c->always_on)
  2011. continue;
  2012. mutex_lock(&rdev->mutex);
  2013. if (rdev->use_count)
  2014. goto unlock;
  2015. /* If we can't read the status assume it's on. */
  2016. if (ops->is_enabled)
  2017. enabled = ops->is_enabled(rdev);
  2018. else
  2019. enabled = 1;
  2020. if (!enabled)
  2021. goto unlock;
  2022. if (has_full_constraints) {
  2023. /* We log since this may kill the system if it
  2024. * goes wrong. */
  2025. printk(KERN_INFO "%s: disabling %s\n",
  2026. __func__, name);
  2027. ret = ops->disable(rdev);
  2028. if (ret != 0) {
  2029. printk(KERN_ERR
  2030. "%s: couldn't disable %s: %d\n",
  2031. __func__, name, ret);
  2032. }
  2033. } else {
  2034. /* The intention is that in future we will
  2035. * assume that full constraints are provided
  2036. * so warn even if we aren't going to do
  2037. * anything here.
  2038. */
  2039. printk(KERN_WARNING
  2040. "%s: incomplete constraints, leaving %s on\n",
  2041. __func__, name);
  2042. }
  2043. unlock:
  2044. mutex_unlock(&rdev->mutex);
  2045. }
  2046. mutex_unlock(&regulator_list_mutex);
  2047. return 0;
  2048. }
  2049. late_initcall(regulator_init_complete);