core.c 64 KB

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