core.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496
  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/debugfs.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/async.h>
  21. #include <linux/err.h>
  22. #include <linux/mutex.h>
  23. #include <linux/suspend.h>
  24. #include <linux/delay.h>
  25. #include <linux/of.h>
  26. #include <linux/regmap.h>
  27. #include <linux/regulator/of_regulator.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/module.h>
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/regulator.h>
  34. #include "dummy.h"
  35. #define rdev_crit(rdev, fmt, ...) \
  36. pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  37. #define rdev_err(rdev, fmt, ...) \
  38. pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  39. #define rdev_warn(rdev, fmt, ...) \
  40. pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  41. #define rdev_info(rdev, fmt, ...) \
  42. pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  43. #define rdev_dbg(rdev, fmt, ...) \
  44. pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  45. static DEFINE_MUTEX(regulator_list_mutex);
  46. static LIST_HEAD(regulator_list);
  47. static LIST_HEAD(regulator_map_list);
  48. static bool has_full_constraints;
  49. static bool board_wants_dummy_regulator;
  50. static struct dentry *debugfs_root;
  51. /*
  52. * struct regulator_map
  53. *
  54. * Used to provide symbolic supply names to devices.
  55. */
  56. struct regulator_map {
  57. struct list_head list;
  58. const char *dev_name; /* The dev_name() for the consumer */
  59. const char *supply;
  60. struct regulator_dev *regulator;
  61. };
  62. /*
  63. * struct regulator
  64. *
  65. * One for each consumer device.
  66. */
  67. struct regulator {
  68. struct device *dev;
  69. struct list_head list;
  70. unsigned int always_on:1;
  71. int uA_load;
  72. int min_uV;
  73. int max_uV;
  74. char *supply_name;
  75. struct device_attribute dev_attr;
  76. struct regulator_dev *rdev;
  77. struct dentry *debugfs;
  78. };
  79. static int _regulator_is_enabled(struct regulator_dev *rdev);
  80. static int _regulator_disable(struct regulator_dev *rdev);
  81. static int _regulator_get_voltage(struct regulator_dev *rdev);
  82. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  83. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  84. static void _notifier_call_chain(struct regulator_dev *rdev,
  85. unsigned long event, void *data);
  86. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  87. int min_uV, int max_uV);
  88. static struct regulator *create_regulator(struct regulator_dev *rdev,
  89. struct device *dev,
  90. const char *supply_name);
  91. static const char *rdev_get_name(struct regulator_dev *rdev)
  92. {
  93. if (rdev->constraints && rdev->constraints->name)
  94. return rdev->constraints->name;
  95. else if (rdev->desc->name)
  96. return rdev->desc->name;
  97. else
  98. return "";
  99. }
  100. /**
  101. * of_get_regulator - get a regulator device node based on supply name
  102. * @dev: Device pointer for the consumer (of regulator) device
  103. * @supply: regulator supply name
  104. *
  105. * Extract the regulator device node corresponding to the supply name.
  106. * retruns the device node corresponding to the regulator if found, else
  107. * returns NULL.
  108. */
  109. static struct device_node *of_get_regulator(struct device *dev, const char *supply)
  110. {
  111. struct device_node *regnode = NULL;
  112. char prop_name[32]; /* 32 is max size of property name */
  113. dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
  114. snprintf(prop_name, 32, "%s-supply", supply);
  115. regnode = of_parse_phandle(dev->of_node, prop_name, 0);
  116. if (!regnode) {
  117. dev_dbg(dev, "Looking up %s property in node %s failed",
  118. prop_name, dev->of_node->full_name);
  119. return NULL;
  120. }
  121. return regnode;
  122. }
  123. static int _regulator_can_change_status(struct regulator_dev *rdev)
  124. {
  125. if (!rdev->constraints)
  126. return 0;
  127. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  128. return 1;
  129. else
  130. return 0;
  131. }
  132. /* Platform voltage constraint check */
  133. static int regulator_check_voltage(struct regulator_dev *rdev,
  134. int *min_uV, int *max_uV)
  135. {
  136. BUG_ON(*min_uV > *max_uV);
  137. if (!rdev->constraints) {
  138. rdev_err(rdev, "no constraints\n");
  139. return -ENODEV;
  140. }
  141. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  142. rdev_err(rdev, "operation not allowed\n");
  143. return -EPERM;
  144. }
  145. if (*max_uV > rdev->constraints->max_uV)
  146. *max_uV = rdev->constraints->max_uV;
  147. if (*min_uV < rdev->constraints->min_uV)
  148. *min_uV = rdev->constraints->min_uV;
  149. if (*min_uV > *max_uV) {
  150. rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
  151. *min_uV, *max_uV);
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. /* Make sure we select a voltage that suits the needs of all
  157. * regulator consumers
  158. */
  159. static int regulator_check_consumers(struct regulator_dev *rdev,
  160. int *min_uV, int *max_uV)
  161. {
  162. struct regulator *regulator;
  163. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  164. /*
  165. * Assume consumers that didn't say anything are OK
  166. * with anything in the constraint range.
  167. */
  168. if (!regulator->min_uV && !regulator->max_uV)
  169. continue;
  170. if (*max_uV > regulator->max_uV)
  171. *max_uV = regulator->max_uV;
  172. if (*min_uV < regulator->min_uV)
  173. *min_uV = regulator->min_uV;
  174. }
  175. if (*min_uV > *max_uV)
  176. return -EINVAL;
  177. return 0;
  178. }
  179. /* current constraint check */
  180. static int regulator_check_current_limit(struct regulator_dev *rdev,
  181. int *min_uA, int *max_uA)
  182. {
  183. BUG_ON(*min_uA > *max_uA);
  184. if (!rdev->constraints) {
  185. rdev_err(rdev, "no constraints\n");
  186. return -ENODEV;
  187. }
  188. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  189. rdev_err(rdev, "operation not allowed\n");
  190. return -EPERM;
  191. }
  192. if (*max_uA > rdev->constraints->max_uA)
  193. *max_uA = rdev->constraints->max_uA;
  194. if (*min_uA < rdev->constraints->min_uA)
  195. *min_uA = rdev->constraints->min_uA;
  196. if (*min_uA > *max_uA) {
  197. rdev_err(rdev, "unsupportable current range: %d-%duA\n",
  198. *min_uA, *max_uA);
  199. return -EINVAL;
  200. }
  201. return 0;
  202. }
  203. /* operating mode constraint check */
  204. static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
  205. {
  206. switch (*mode) {
  207. case REGULATOR_MODE_FAST:
  208. case REGULATOR_MODE_NORMAL:
  209. case REGULATOR_MODE_IDLE:
  210. case REGULATOR_MODE_STANDBY:
  211. break;
  212. default:
  213. rdev_err(rdev, "invalid mode %x specified\n", *mode);
  214. return -EINVAL;
  215. }
  216. if (!rdev->constraints) {
  217. rdev_err(rdev, "no constraints\n");
  218. return -ENODEV;
  219. }
  220. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  221. rdev_err(rdev, "operation not allowed\n");
  222. return -EPERM;
  223. }
  224. /* The modes are bitmasks, the most power hungry modes having
  225. * the lowest values. If the requested mode isn't supported
  226. * try higher modes. */
  227. while (*mode) {
  228. if (rdev->constraints->valid_modes_mask & *mode)
  229. return 0;
  230. *mode /= 2;
  231. }
  232. return -EINVAL;
  233. }
  234. /* dynamic regulator mode switching constraint check */
  235. static int regulator_check_drms(struct regulator_dev *rdev)
  236. {
  237. if (!rdev->constraints) {
  238. rdev_err(rdev, "no constraints\n");
  239. return -ENODEV;
  240. }
  241. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  242. rdev_err(rdev, "operation not allowed\n");
  243. return -EPERM;
  244. }
  245. return 0;
  246. }
  247. static ssize_t regulator_uV_show(struct device *dev,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. struct regulator_dev *rdev = dev_get_drvdata(dev);
  251. ssize_t ret;
  252. mutex_lock(&rdev->mutex);
  253. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  254. mutex_unlock(&rdev->mutex);
  255. return ret;
  256. }
  257. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  258. static ssize_t regulator_uA_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct regulator_dev *rdev = dev_get_drvdata(dev);
  262. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  263. }
  264. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  265. static ssize_t regulator_name_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. struct regulator_dev *rdev = dev_get_drvdata(dev);
  269. return sprintf(buf, "%s\n", rdev_get_name(rdev));
  270. }
  271. static ssize_t regulator_print_opmode(char *buf, int mode)
  272. {
  273. switch (mode) {
  274. case REGULATOR_MODE_FAST:
  275. return sprintf(buf, "fast\n");
  276. case REGULATOR_MODE_NORMAL:
  277. return sprintf(buf, "normal\n");
  278. case REGULATOR_MODE_IDLE:
  279. return sprintf(buf, "idle\n");
  280. case REGULATOR_MODE_STANDBY:
  281. return sprintf(buf, "standby\n");
  282. }
  283. return sprintf(buf, "unknown\n");
  284. }
  285. static ssize_t regulator_opmode_show(struct device *dev,
  286. struct device_attribute *attr, char *buf)
  287. {
  288. struct regulator_dev *rdev = dev_get_drvdata(dev);
  289. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  290. }
  291. static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
  292. static ssize_t regulator_print_state(char *buf, int state)
  293. {
  294. if (state > 0)
  295. return sprintf(buf, "enabled\n");
  296. else if (state == 0)
  297. return sprintf(buf, "disabled\n");
  298. else
  299. return sprintf(buf, "unknown\n");
  300. }
  301. static ssize_t regulator_state_show(struct device *dev,
  302. struct device_attribute *attr, char *buf)
  303. {
  304. struct regulator_dev *rdev = dev_get_drvdata(dev);
  305. ssize_t ret;
  306. mutex_lock(&rdev->mutex);
  307. ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
  308. mutex_unlock(&rdev->mutex);
  309. return ret;
  310. }
  311. static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
  312. static ssize_t regulator_status_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct regulator_dev *rdev = dev_get_drvdata(dev);
  316. int status;
  317. char *label;
  318. status = rdev->desc->ops->get_status(rdev);
  319. if (status < 0)
  320. return status;
  321. switch (status) {
  322. case REGULATOR_STATUS_OFF:
  323. label = "off";
  324. break;
  325. case REGULATOR_STATUS_ON:
  326. label = "on";
  327. break;
  328. case REGULATOR_STATUS_ERROR:
  329. label = "error";
  330. break;
  331. case REGULATOR_STATUS_FAST:
  332. label = "fast";
  333. break;
  334. case REGULATOR_STATUS_NORMAL:
  335. label = "normal";
  336. break;
  337. case REGULATOR_STATUS_IDLE:
  338. label = "idle";
  339. break;
  340. case REGULATOR_STATUS_STANDBY:
  341. label = "standby";
  342. break;
  343. default:
  344. return -ERANGE;
  345. }
  346. return sprintf(buf, "%s\n", label);
  347. }
  348. static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
  349. static ssize_t regulator_min_uA_show(struct device *dev,
  350. struct device_attribute *attr, char *buf)
  351. {
  352. struct regulator_dev *rdev = dev_get_drvdata(dev);
  353. if (!rdev->constraints)
  354. return sprintf(buf, "constraint not defined\n");
  355. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  356. }
  357. static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
  358. static ssize_t regulator_max_uA_show(struct device *dev,
  359. struct device_attribute *attr, char *buf)
  360. {
  361. struct regulator_dev *rdev = dev_get_drvdata(dev);
  362. if (!rdev->constraints)
  363. return sprintf(buf, "constraint not defined\n");
  364. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  365. }
  366. static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
  367. static ssize_t regulator_min_uV_show(struct device *dev,
  368. struct device_attribute *attr, char *buf)
  369. {
  370. struct regulator_dev *rdev = dev_get_drvdata(dev);
  371. if (!rdev->constraints)
  372. return sprintf(buf, "constraint not defined\n");
  373. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  374. }
  375. static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
  376. static ssize_t regulator_max_uV_show(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct regulator_dev *rdev = dev_get_drvdata(dev);
  380. if (!rdev->constraints)
  381. return sprintf(buf, "constraint not defined\n");
  382. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  383. }
  384. static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
  385. static ssize_t regulator_total_uA_show(struct device *dev,
  386. struct device_attribute *attr, char *buf)
  387. {
  388. struct regulator_dev *rdev = dev_get_drvdata(dev);
  389. struct regulator *regulator;
  390. int uA = 0;
  391. mutex_lock(&rdev->mutex);
  392. list_for_each_entry(regulator, &rdev->consumer_list, list)
  393. uA += regulator->uA_load;
  394. mutex_unlock(&rdev->mutex);
  395. return sprintf(buf, "%d\n", uA);
  396. }
  397. static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
  398. static ssize_t regulator_num_users_show(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. struct regulator_dev *rdev = dev_get_drvdata(dev);
  402. return sprintf(buf, "%d\n", rdev->use_count);
  403. }
  404. static ssize_t regulator_type_show(struct device *dev,
  405. struct device_attribute *attr, char *buf)
  406. {
  407. struct regulator_dev *rdev = dev_get_drvdata(dev);
  408. switch (rdev->desc->type) {
  409. case REGULATOR_VOLTAGE:
  410. return sprintf(buf, "voltage\n");
  411. case REGULATOR_CURRENT:
  412. return sprintf(buf, "current\n");
  413. }
  414. return sprintf(buf, "unknown\n");
  415. }
  416. static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
  417. struct device_attribute *attr, char *buf)
  418. {
  419. struct regulator_dev *rdev = dev_get_drvdata(dev);
  420. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  421. }
  422. static DEVICE_ATTR(suspend_mem_microvolts, 0444,
  423. regulator_suspend_mem_uV_show, NULL);
  424. static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
  425. struct device_attribute *attr, char *buf)
  426. {
  427. struct regulator_dev *rdev = dev_get_drvdata(dev);
  428. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  429. }
  430. static DEVICE_ATTR(suspend_disk_microvolts, 0444,
  431. regulator_suspend_disk_uV_show, NULL);
  432. static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. struct regulator_dev *rdev = dev_get_drvdata(dev);
  436. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  437. }
  438. static DEVICE_ATTR(suspend_standby_microvolts, 0444,
  439. regulator_suspend_standby_uV_show, NULL);
  440. static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. struct regulator_dev *rdev = dev_get_drvdata(dev);
  444. return regulator_print_opmode(buf,
  445. rdev->constraints->state_mem.mode);
  446. }
  447. static DEVICE_ATTR(suspend_mem_mode, 0444,
  448. regulator_suspend_mem_mode_show, NULL);
  449. static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
  450. struct device_attribute *attr, char *buf)
  451. {
  452. struct regulator_dev *rdev = dev_get_drvdata(dev);
  453. return regulator_print_opmode(buf,
  454. rdev->constraints->state_disk.mode);
  455. }
  456. static DEVICE_ATTR(suspend_disk_mode, 0444,
  457. regulator_suspend_disk_mode_show, NULL);
  458. static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct regulator_dev *rdev = dev_get_drvdata(dev);
  462. return regulator_print_opmode(buf,
  463. rdev->constraints->state_standby.mode);
  464. }
  465. static DEVICE_ATTR(suspend_standby_mode, 0444,
  466. regulator_suspend_standby_mode_show, NULL);
  467. static ssize_t regulator_suspend_mem_state_show(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. struct regulator_dev *rdev = dev_get_drvdata(dev);
  471. return regulator_print_state(buf,
  472. rdev->constraints->state_mem.enabled);
  473. }
  474. static DEVICE_ATTR(suspend_mem_state, 0444,
  475. regulator_suspend_mem_state_show, NULL);
  476. static ssize_t regulator_suspend_disk_state_show(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. struct regulator_dev *rdev = dev_get_drvdata(dev);
  480. return regulator_print_state(buf,
  481. rdev->constraints->state_disk.enabled);
  482. }
  483. static DEVICE_ATTR(suspend_disk_state, 0444,
  484. regulator_suspend_disk_state_show, NULL);
  485. static ssize_t regulator_suspend_standby_state_show(struct device *dev,
  486. struct device_attribute *attr, char *buf)
  487. {
  488. struct regulator_dev *rdev = dev_get_drvdata(dev);
  489. return regulator_print_state(buf,
  490. rdev->constraints->state_standby.enabled);
  491. }
  492. static DEVICE_ATTR(suspend_standby_state, 0444,
  493. regulator_suspend_standby_state_show, NULL);
  494. /*
  495. * These are the only attributes are present for all regulators.
  496. * Other attributes are a function of regulator functionality.
  497. */
  498. static struct device_attribute regulator_dev_attrs[] = {
  499. __ATTR(name, 0444, regulator_name_show, NULL),
  500. __ATTR(num_users, 0444, regulator_num_users_show, NULL),
  501. __ATTR(type, 0444, regulator_type_show, NULL),
  502. __ATTR_NULL,
  503. };
  504. static void regulator_dev_release(struct device *dev)
  505. {
  506. struct regulator_dev *rdev = dev_get_drvdata(dev);
  507. kfree(rdev);
  508. }
  509. static struct class regulator_class = {
  510. .name = "regulator",
  511. .dev_release = regulator_dev_release,
  512. .dev_attrs = regulator_dev_attrs,
  513. };
  514. /* Calculate the new optimum regulator operating mode based on the new total
  515. * consumer load. All locks held by caller */
  516. static void drms_uA_update(struct regulator_dev *rdev)
  517. {
  518. struct regulator *sibling;
  519. int current_uA = 0, output_uV, input_uV, err;
  520. unsigned int mode;
  521. err = regulator_check_drms(rdev);
  522. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  523. (!rdev->desc->ops->get_voltage &&
  524. !rdev->desc->ops->get_voltage_sel) ||
  525. !rdev->desc->ops->set_mode)
  526. return;
  527. /* get output voltage */
  528. output_uV = _regulator_get_voltage(rdev);
  529. if (output_uV <= 0)
  530. return;
  531. /* get input voltage */
  532. input_uV = 0;
  533. if (rdev->supply)
  534. input_uV = regulator_get_voltage(rdev->supply);
  535. if (input_uV <= 0)
  536. input_uV = rdev->constraints->input_uV;
  537. if (input_uV <= 0)
  538. return;
  539. /* calc total requested load */
  540. list_for_each_entry(sibling, &rdev->consumer_list, list)
  541. current_uA += sibling->uA_load;
  542. /* now get the optimum mode for our new total regulator load */
  543. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  544. output_uV, current_uA);
  545. /* check the new mode is allowed */
  546. err = regulator_mode_constrain(rdev, &mode);
  547. if (err == 0)
  548. rdev->desc->ops->set_mode(rdev, mode);
  549. }
  550. static int suspend_set_state(struct regulator_dev *rdev,
  551. struct regulator_state *rstate)
  552. {
  553. int ret = 0;
  554. /* If we have no suspend mode configration don't set anything;
  555. * only warn if the driver implements set_suspend_voltage or
  556. * set_suspend_mode callback.
  557. */
  558. if (!rstate->enabled && !rstate->disabled) {
  559. if (rdev->desc->ops->set_suspend_voltage ||
  560. rdev->desc->ops->set_suspend_mode)
  561. rdev_warn(rdev, "No configuration\n");
  562. return 0;
  563. }
  564. if (rstate->enabled && rstate->disabled) {
  565. rdev_err(rdev, "invalid configuration\n");
  566. return -EINVAL;
  567. }
  568. if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
  569. ret = rdev->desc->ops->set_suspend_enable(rdev);
  570. else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
  571. ret = rdev->desc->ops->set_suspend_disable(rdev);
  572. else /* OK if set_suspend_enable or set_suspend_disable is NULL */
  573. ret = 0;
  574. if (ret < 0) {
  575. rdev_err(rdev, "failed to enabled/disable\n");
  576. return ret;
  577. }
  578. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  579. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  580. if (ret < 0) {
  581. rdev_err(rdev, "failed to set voltage\n");
  582. return ret;
  583. }
  584. }
  585. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  586. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  587. if (ret < 0) {
  588. rdev_err(rdev, "failed to set mode\n");
  589. return ret;
  590. }
  591. }
  592. return ret;
  593. }
  594. /* locks held by caller */
  595. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  596. {
  597. if (!rdev->constraints)
  598. return -EINVAL;
  599. switch (state) {
  600. case PM_SUSPEND_STANDBY:
  601. return suspend_set_state(rdev,
  602. &rdev->constraints->state_standby);
  603. case PM_SUSPEND_MEM:
  604. return suspend_set_state(rdev,
  605. &rdev->constraints->state_mem);
  606. case PM_SUSPEND_MAX:
  607. return suspend_set_state(rdev,
  608. &rdev->constraints->state_disk);
  609. default:
  610. return -EINVAL;
  611. }
  612. }
  613. static void print_constraints(struct regulator_dev *rdev)
  614. {
  615. struct regulation_constraints *constraints = rdev->constraints;
  616. char buf[80] = "";
  617. int count = 0;
  618. int ret;
  619. if (constraints->min_uV && constraints->max_uV) {
  620. if (constraints->min_uV == constraints->max_uV)
  621. count += sprintf(buf + count, "%d mV ",
  622. constraints->min_uV / 1000);
  623. else
  624. count += sprintf(buf + count, "%d <--> %d mV ",
  625. constraints->min_uV / 1000,
  626. constraints->max_uV / 1000);
  627. }
  628. if (!constraints->min_uV ||
  629. constraints->min_uV != constraints->max_uV) {
  630. ret = _regulator_get_voltage(rdev);
  631. if (ret > 0)
  632. count += sprintf(buf + count, "at %d mV ", ret / 1000);
  633. }
  634. if (constraints->uV_offset)
  635. count += sprintf(buf, "%dmV offset ",
  636. constraints->uV_offset / 1000);
  637. if (constraints->min_uA && constraints->max_uA) {
  638. if (constraints->min_uA == constraints->max_uA)
  639. count += sprintf(buf + count, "%d mA ",
  640. constraints->min_uA / 1000);
  641. else
  642. count += sprintf(buf + count, "%d <--> %d mA ",
  643. constraints->min_uA / 1000,
  644. constraints->max_uA / 1000);
  645. }
  646. if (!constraints->min_uA ||
  647. constraints->min_uA != constraints->max_uA) {
  648. ret = _regulator_get_current_limit(rdev);
  649. if (ret > 0)
  650. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  651. }
  652. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  653. count += sprintf(buf + count, "fast ");
  654. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  655. count += sprintf(buf + count, "normal ");
  656. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  657. count += sprintf(buf + count, "idle ");
  658. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  659. count += sprintf(buf + count, "standby");
  660. rdev_info(rdev, "%s\n", buf);
  661. if ((constraints->min_uV != constraints->max_uV) &&
  662. !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
  663. rdev_warn(rdev,
  664. "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
  665. }
  666. static int machine_constraints_voltage(struct regulator_dev *rdev,
  667. struct regulation_constraints *constraints)
  668. {
  669. struct regulator_ops *ops = rdev->desc->ops;
  670. int ret;
  671. /* do we need to apply the constraint voltage */
  672. if (rdev->constraints->apply_uV &&
  673. rdev->constraints->min_uV == rdev->constraints->max_uV) {
  674. ret = _regulator_do_set_voltage(rdev,
  675. rdev->constraints->min_uV,
  676. rdev->constraints->max_uV);
  677. if (ret < 0) {
  678. rdev_err(rdev, "failed to apply %duV constraint\n",
  679. rdev->constraints->min_uV);
  680. return ret;
  681. }
  682. }
  683. /* constrain machine-level voltage specs to fit
  684. * the actual range supported by this regulator.
  685. */
  686. if (ops->list_voltage && rdev->desc->n_voltages) {
  687. int count = rdev->desc->n_voltages;
  688. int i;
  689. int min_uV = INT_MAX;
  690. int max_uV = INT_MIN;
  691. int cmin = constraints->min_uV;
  692. int cmax = constraints->max_uV;
  693. /* it's safe to autoconfigure fixed-voltage supplies
  694. and the constraints are used by list_voltage. */
  695. if (count == 1 && !cmin) {
  696. cmin = 1;
  697. cmax = INT_MAX;
  698. constraints->min_uV = cmin;
  699. constraints->max_uV = cmax;
  700. }
  701. /* voltage constraints are optional */
  702. if ((cmin == 0) && (cmax == 0))
  703. return 0;
  704. /* else require explicit machine-level constraints */
  705. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  706. rdev_err(rdev, "invalid voltage constraints\n");
  707. return -EINVAL;
  708. }
  709. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  710. for (i = 0; i < count; i++) {
  711. int value;
  712. value = ops->list_voltage(rdev, i);
  713. if (value <= 0)
  714. continue;
  715. /* maybe adjust [min_uV..max_uV] */
  716. if (value >= cmin && value < min_uV)
  717. min_uV = value;
  718. if (value <= cmax && value > max_uV)
  719. max_uV = value;
  720. }
  721. /* final: [min_uV..max_uV] valid iff constraints valid */
  722. if (max_uV < min_uV) {
  723. rdev_err(rdev, "unsupportable voltage constraints\n");
  724. return -EINVAL;
  725. }
  726. /* use regulator's subset of machine constraints */
  727. if (constraints->min_uV < min_uV) {
  728. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  729. constraints->min_uV, min_uV);
  730. constraints->min_uV = min_uV;
  731. }
  732. if (constraints->max_uV > max_uV) {
  733. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  734. constraints->max_uV, max_uV);
  735. constraints->max_uV = max_uV;
  736. }
  737. }
  738. return 0;
  739. }
  740. /**
  741. * set_machine_constraints - sets regulator constraints
  742. * @rdev: regulator source
  743. * @constraints: constraints to apply
  744. *
  745. * Allows platform initialisation code to define and constrain
  746. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  747. * Constraints *must* be set by platform code in order for some
  748. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  749. * set_mode.
  750. */
  751. static int set_machine_constraints(struct regulator_dev *rdev,
  752. const struct regulation_constraints *constraints)
  753. {
  754. int ret = 0;
  755. struct regulator_ops *ops = rdev->desc->ops;
  756. if (constraints)
  757. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  758. GFP_KERNEL);
  759. else
  760. rdev->constraints = kzalloc(sizeof(*constraints),
  761. GFP_KERNEL);
  762. if (!rdev->constraints)
  763. return -ENOMEM;
  764. ret = machine_constraints_voltage(rdev, rdev->constraints);
  765. if (ret != 0)
  766. goto out;
  767. /* do we need to setup our suspend state */
  768. if (rdev->constraints->initial_state) {
  769. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  770. if (ret < 0) {
  771. rdev_err(rdev, "failed to set suspend state\n");
  772. goto out;
  773. }
  774. }
  775. if (rdev->constraints->initial_mode) {
  776. if (!ops->set_mode) {
  777. rdev_err(rdev, "no set_mode operation\n");
  778. ret = -EINVAL;
  779. goto out;
  780. }
  781. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  782. if (ret < 0) {
  783. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  784. goto out;
  785. }
  786. }
  787. /* If the constraints say the regulator should be on at this point
  788. * and we have control then make sure it is enabled.
  789. */
  790. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  791. ops->enable) {
  792. ret = ops->enable(rdev);
  793. if (ret < 0) {
  794. rdev_err(rdev, "failed to enable\n");
  795. goto out;
  796. }
  797. }
  798. print_constraints(rdev);
  799. return 0;
  800. out:
  801. kfree(rdev->constraints);
  802. rdev->constraints = NULL;
  803. return ret;
  804. }
  805. /**
  806. * set_supply - set regulator supply regulator
  807. * @rdev: regulator name
  808. * @supply_rdev: supply regulator name
  809. *
  810. * Called by platform initialisation code to set the supply regulator for this
  811. * regulator. This ensures that a regulators supply will also be enabled by the
  812. * core if it's child is enabled.
  813. */
  814. static int set_supply(struct regulator_dev *rdev,
  815. struct regulator_dev *supply_rdev)
  816. {
  817. int err;
  818. rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
  819. rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
  820. if (rdev->supply == NULL) {
  821. err = -ENOMEM;
  822. return err;
  823. }
  824. return 0;
  825. }
  826. /**
  827. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  828. * @rdev: regulator source
  829. * @consumer_dev_name: dev_name() string for device supply applies to
  830. * @supply: symbolic name for supply
  831. *
  832. * Allows platform initialisation code to map physical regulator
  833. * sources to symbolic names for supplies for use by devices. Devices
  834. * should use these symbolic names to request regulators, avoiding the
  835. * need to provide board-specific regulator names as platform data.
  836. */
  837. static int set_consumer_device_supply(struct regulator_dev *rdev,
  838. const char *consumer_dev_name,
  839. const char *supply)
  840. {
  841. struct regulator_map *node;
  842. int has_dev;
  843. if (supply == NULL)
  844. return -EINVAL;
  845. if (consumer_dev_name != NULL)
  846. has_dev = 1;
  847. else
  848. has_dev = 0;
  849. list_for_each_entry(node, &regulator_map_list, list) {
  850. if (node->dev_name && consumer_dev_name) {
  851. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  852. continue;
  853. } else if (node->dev_name || consumer_dev_name) {
  854. continue;
  855. }
  856. if (strcmp(node->supply, supply) != 0)
  857. continue;
  858. pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
  859. consumer_dev_name,
  860. dev_name(&node->regulator->dev),
  861. node->regulator->desc->name,
  862. supply,
  863. dev_name(&rdev->dev), rdev_get_name(rdev));
  864. return -EBUSY;
  865. }
  866. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  867. if (node == NULL)
  868. return -ENOMEM;
  869. node->regulator = rdev;
  870. node->supply = supply;
  871. if (has_dev) {
  872. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  873. if (node->dev_name == NULL) {
  874. kfree(node);
  875. return -ENOMEM;
  876. }
  877. }
  878. list_add(&node->list, &regulator_map_list);
  879. return 0;
  880. }
  881. static void unset_regulator_supplies(struct regulator_dev *rdev)
  882. {
  883. struct regulator_map *node, *n;
  884. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  885. if (rdev == node->regulator) {
  886. list_del(&node->list);
  887. kfree(node->dev_name);
  888. kfree(node);
  889. }
  890. }
  891. }
  892. #define REG_STR_SIZE 64
  893. static struct regulator *create_regulator(struct regulator_dev *rdev,
  894. struct device *dev,
  895. const char *supply_name)
  896. {
  897. struct regulator *regulator;
  898. char buf[REG_STR_SIZE];
  899. int err, size;
  900. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  901. if (regulator == NULL)
  902. return NULL;
  903. mutex_lock(&rdev->mutex);
  904. regulator->rdev = rdev;
  905. list_add(&regulator->list, &rdev->consumer_list);
  906. if (dev) {
  907. /* Add a link to the device sysfs entry */
  908. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  909. dev->kobj.name, supply_name);
  910. if (size >= REG_STR_SIZE)
  911. goto overflow_err;
  912. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  913. if (regulator->supply_name == NULL)
  914. goto overflow_err;
  915. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  916. buf);
  917. if (err) {
  918. rdev_warn(rdev, "could not add device link %s err %d\n",
  919. dev->kobj.name, err);
  920. /* non-fatal */
  921. }
  922. } else {
  923. regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
  924. if (regulator->supply_name == NULL)
  925. goto overflow_err;
  926. }
  927. regulator->debugfs = debugfs_create_dir(regulator->supply_name,
  928. rdev->debugfs);
  929. if (!regulator->debugfs) {
  930. rdev_warn(rdev, "Failed to create debugfs directory\n");
  931. } else {
  932. debugfs_create_u32("uA_load", 0444, regulator->debugfs,
  933. &regulator->uA_load);
  934. debugfs_create_u32("min_uV", 0444, regulator->debugfs,
  935. &regulator->min_uV);
  936. debugfs_create_u32("max_uV", 0444, regulator->debugfs,
  937. &regulator->max_uV);
  938. }
  939. /*
  940. * Check now if the regulator is an always on regulator - if
  941. * it is then we don't need to do nearly so much work for
  942. * enable/disable calls.
  943. */
  944. if (!_regulator_can_change_status(rdev) &&
  945. _regulator_is_enabled(rdev))
  946. regulator->always_on = true;
  947. mutex_unlock(&rdev->mutex);
  948. return regulator;
  949. overflow_err:
  950. list_del(&regulator->list);
  951. kfree(regulator);
  952. mutex_unlock(&rdev->mutex);
  953. return NULL;
  954. }
  955. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  956. {
  957. if (!rdev->desc->ops->enable_time)
  958. return 0;
  959. return rdev->desc->ops->enable_time(rdev);
  960. }
  961. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  962. const char *supply,
  963. int *ret)
  964. {
  965. struct regulator_dev *r;
  966. struct device_node *node;
  967. struct regulator_map *map;
  968. const char *devname = NULL;
  969. /* first do a dt based lookup */
  970. if (dev && dev->of_node) {
  971. node = of_get_regulator(dev, supply);
  972. if (node) {
  973. list_for_each_entry(r, &regulator_list, list)
  974. if (r->dev.parent &&
  975. node == r->dev.of_node)
  976. return r;
  977. } else {
  978. /*
  979. * If we couldn't even get the node then it's
  980. * not just that the device didn't register
  981. * yet, there's no node and we'll never
  982. * succeed.
  983. */
  984. *ret = -ENODEV;
  985. }
  986. }
  987. /* if not found, try doing it non-dt way */
  988. if (dev)
  989. devname = dev_name(dev);
  990. list_for_each_entry(r, &regulator_list, list)
  991. if (strcmp(rdev_get_name(r), supply) == 0)
  992. return r;
  993. list_for_each_entry(map, &regulator_map_list, list) {
  994. /* If the mapping has a device set up it must match */
  995. if (map->dev_name &&
  996. (!devname || strcmp(map->dev_name, devname)))
  997. continue;
  998. if (strcmp(map->supply, supply) == 0)
  999. return map->regulator;
  1000. }
  1001. return NULL;
  1002. }
  1003. /* Internal regulator request function */
  1004. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1005. int exclusive)
  1006. {
  1007. struct regulator_dev *rdev;
  1008. struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
  1009. const char *devname = NULL;
  1010. int ret;
  1011. if (id == NULL) {
  1012. pr_err("get() with no identifier\n");
  1013. return regulator;
  1014. }
  1015. if (dev)
  1016. devname = dev_name(dev);
  1017. mutex_lock(&regulator_list_mutex);
  1018. rdev = regulator_dev_lookup(dev, id, &ret);
  1019. if (rdev)
  1020. goto found;
  1021. if (board_wants_dummy_regulator) {
  1022. rdev = dummy_regulator_rdev;
  1023. goto found;
  1024. }
  1025. #ifdef CONFIG_REGULATOR_DUMMY
  1026. if (!devname)
  1027. devname = "deviceless";
  1028. /* If the board didn't flag that it was fully constrained then
  1029. * substitute in a dummy regulator so consumers can continue.
  1030. */
  1031. if (!has_full_constraints) {
  1032. pr_warn("%s supply %s not found, using dummy regulator\n",
  1033. devname, id);
  1034. rdev = dummy_regulator_rdev;
  1035. goto found;
  1036. }
  1037. #endif
  1038. mutex_unlock(&regulator_list_mutex);
  1039. return regulator;
  1040. found:
  1041. if (rdev->exclusive) {
  1042. regulator = ERR_PTR(-EPERM);
  1043. goto out;
  1044. }
  1045. if (exclusive && rdev->open_count) {
  1046. regulator = ERR_PTR(-EBUSY);
  1047. goto out;
  1048. }
  1049. if (!try_module_get(rdev->owner))
  1050. goto out;
  1051. regulator = create_regulator(rdev, dev, id);
  1052. if (regulator == NULL) {
  1053. regulator = ERR_PTR(-ENOMEM);
  1054. module_put(rdev->owner);
  1055. goto out;
  1056. }
  1057. rdev->open_count++;
  1058. if (exclusive) {
  1059. rdev->exclusive = 1;
  1060. ret = _regulator_is_enabled(rdev);
  1061. if (ret > 0)
  1062. rdev->use_count = 1;
  1063. else
  1064. rdev->use_count = 0;
  1065. }
  1066. out:
  1067. mutex_unlock(&regulator_list_mutex);
  1068. return regulator;
  1069. }
  1070. /**
  1071. * regulator_get - lookup and obtain a reference to a regulator.
  1072. * @dev: device for regulator "consumer"
  1073. * @id: Supply name or regulator ID.
  1074. *
  1075. * Returns a struct regulator corresponding to the regulator producer,
  1076. * or IS_ERR() condition containing errno.
  1077. *
  1078. * Use of supply names configured via regulator_set_device_supply() is
  1079. * strongly encouraged. It is recommended that the supply name used
  1080. * should match the name used for the supply and/or the relevant
  1081. * device pins in the datasheet.
  1082. */
  1083. struct regulator *regulator_get(struct device *dev, const char *id)
  1084. {
  1085. return _regulator_get(dev, id, 0);
  1086. }
  1087. EXPORT_SYMBOL_GPL(regulator_get);
  1088. static void devm_regulator_release(struct device *dev, void *res)
  1089. {
  1090. regulator_put(*(struct regulator **)res);
  1091. }
  1092. /**
  1093. * devm_regulator_get - Resource managed regulator_get()
  1094. * @dev: device for regulator "consumer"
  1095. * @id: Supply name or regulator ID.
  1096. *
  1097. * Managed regulator_get(). Regulators returned from this function are
  1098. * automatically regulator_put() on driver detach. See regulator_get() for more
  1099. * information.
  1100. */
  1101. struct regulator *devm_regulator_get(struct device *dev, const char *id)
  1102. {
  1103. struct regulator **ptr, *regulator;
  1104. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  1105. if (!ptr)
  1106. return ERR_PTR(-ENOMEM);
  1107. regulator = regulator_get(dev, id);
  1108. if (!IS_ERR(regulator)) {
  1109. *ptr = regulator;
  1110. devres_add(dev, ptr);
  1111. } else {
  1112. devres_free(ptr);
  1113. }
  1114. return regulator;
  1115. }
  1116. EXPORT_SYMBOL_GPL(devm_regulator_get);
  1117. /**
  1118. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1119. * @dev: device for regulator "consumer"
  1120. * @id: Supply name or regulator ID.
  1121. *
  1122. * Returns a struct regulator corresponding to the regulator producer,
  1123. * or IS_ERR() condition containing errno. Other consumers will be
  1124. * unable to obtain this reference is held and the use count for the
  1125. * regulator will be initialised to reflect the current state of the
  1126. * regulator.
  1127. *
  1128. * This is intended for use by consumers which cannot tolerate shared
  1129. * use of the regulator such as those which need to force the
  1130. * regulator off for correct operation of the hardware they are
  1131. * controlling.
  1132. *
  1133. * Use of supply names configured via regulator_set_device_supply() is
  1134. * strongly encouraged. It is recommended that the supply name used
  1135. * should match the name used for the supply and/or the relevant
  1136. * device pins in the datasheet.
  1137. */
  1138. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1139. {
  1140. return _regulator_get(dev, id, 1);
  1141. }
  1142. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1143. /**
  1144. * regulator_put - "free" the regulator source
  1145. * @regulator: regulator source
  1146. *
  1147. * Note: drivers must ensure that all regulator_enable calls made on this
  1148. * regulator source are balanced by regulator_disable calls prior to calling
  1149. * this function.
  1150. */
  1151. void regulator_put(struct regulator *regulator)
  1152. {
  1153. struct regulator_dev *rdev;
  1154. if (regulator == NULL || IS_ERR(regulator))
  1155. return;
  1156. mutex_lock(&regulator_list_mutex);
  1157. rdev = regulator->rdev;
  1158. debugfs_remove_recursive(regulator->debugfs);
  1159. /* remove any sysfs entries */
  1160. if (regulator->dev) {
  1161. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1162. device_remove_file(regulator->dev, &regulator->dev_attr);
  1163. kfree(regulator->dev_attr.attr.name);
  1164. }
  1165. kfree(regulator->supply_name);
  1166. list_del(&regulator->list);
  1167. kfree(regulator);
  1168. rdev->open_count--;
  1169. rdev->exclusive = 0;
  1170. module_put(rdev->owner);
  1171. mutex_unlock(&regulator_list_mutex);
  1172. }
  1173. EXPORT_SYMBOL_GPL(regulator_put);
  1174. static int devm_regulator_match(struct device *dev, void *res, void *data)
  1175. {
  1176. struct regulator **r = res;
  1177. if (!r || !*r) {
  1178. WARN_ON(!r || !*r);
  1179. return 0;
  1180. }
  1181. return *r == data;
  1182. }
  1183. /**
  1184. * devm_regulator_put - Resource managed regulator_put()
  1185. * @regulator: regulator to free
  1186. *
  1187. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  1188. * this function will not need to be called and the resource management
  1189. * code will ensure that the resource is freed.
  1190. */
  1191. void devm_regulator_put(struct regulator *regulator)
  1192. {
  1193. int rc;
  1194. rc = devres_release(regulator->dev, devm_regulator_release,
  1195. devm_regulator_match, regulator);
  1196. if (rc != 0)
  1197. WARN_ON(rc);
  1198. }
  1199. EXPORT_SYMBOL_GPL(devm_regulator_put);
  1200. /* locks held by regulator_enable() */
  1201. static int _regulator_enable(struct regulator_dev *rdev)
  1202. {
  1203. int ret, delay;
  1204. /* check voltage and requested load before enabling */
  1205. if (rdev->constraints &&
  1206. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1207. drms_uA_update(rdev);
  1208. if (rdev->use_count == 0) {
  1209. /* The regulator may on if it's not switchable or left on */
  1210. ret = _regulator_is_enabled(rdev);
  1211. if (ret == -EINVAL || ret == 0) {
  1212. if (!_regulator_can_change_status(rdev))
  1213. return -EPERM;
  1214. if (!rdev->desc->ops->enable)
  1215. return -EINVAL;
  1216. /* Query before enabling in case configuration
  1217. * dependent. */
  1218. ret = _regulator_get_enable_time(rdev);
  1219. if (ret >= 0) {
  1220. delay = ret;
  1221. } else {
  1222. rdev_warn(rdev, "enable_time() failed: %d\n",
  1223. ret);
  1224. delay = 0;
  1225. }
  1226. trace_regulator_enable(rdev_get_name(rdev));
  1227. /* Allow the regulator to ramp; it would be useful
  1228. * to extend this for bulk operations so that the
  1229. * regulators can ramp together. */
  1230. ret = rdev->desc->ops->enable(rdev);
  1231. if (ret < 0)
  1232. return ret;
  1233. trace_regulator_enable_delay(rdev_get_name(rdev));
  1234. if (delay >= 1000) {
  1235. mdelay(delay / 1000);
  1236. udelay(delay % 1000);
  1237. } else if (delay) {
  1238. udelay(delay);
  1239. }
  1240. trace_regulator_enable_complete(rdev_get_name(rdev));
  1241. } else if (ret < 0) {
  1242. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1243. return ret;
  1244. }
  1245. /* Fallthrough on positive return values - already enabled */
  1246. }
  1247. rdev->use_count++;
  1248. return 0;
  1249. }
  1250. /**
  1251. * regulator_enable - enable regulator output
  1252. * @regulator: regulator source
  1253. *
  1254. * Request that the regulator be enabled with the regulator output at
  1255. * the predefined voltage or current value. Calls to regulator_enable()
  1256. * must be balanced with calls to regulator_disable().
  1257. *
  1258. * NOTE: the output value can be set by other drivers, boot loader or may be
  1259. * hardwired in the regulator.
  1260. */
  1261. int regulator_enable(struct regulator *regulator)
  1262. {
  1263. struct regulator_dev *rdev = regulator->rdev;
  1264. int ret = 0;
  1265. if (regulator->always_on)
  1266. return 0;
  1267. if (rdev->supply) {
  1268. ret = regulator_enable(rdev->supply);
  1269. if (ret != 0)
  1270. return ret;
  1271. }
  1272. mutex_lock(&rdev->mutex);
  1273. ret = _regulator_enable(rdev);
  1274. mutex_unlock(&rdev->mutex);
  1275. if (ret != 0 && rdev->supply)
  1276. regulator_disable(rdev->supply);
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL_GPL(regulator_enable);
  1280. /* locks held by regulator_disable() */
  1281. static int _regulator_disable(struct regulator_dev *rdev)
  1282. {
  1283. int ret = 0;
  1284. if (WARN(rdev->use_count <= 0,
  1285. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1286. return -EIO;
  1287. /* are we the last user and permitted to disable ? */
  1288. if (rdev->use_count == 1 &&
  1289. (rdev->constraints && !rdev->constraints->always_on)) {
  1290. /* we are last user */
  1291. if (_regulator_can_change_status(rdev) &&
  1292. rdev->desc->ops->disable) {
  1293. trace_regulator_disable(rdev_get_name(rdev));
  1294. ret = rdev->desc->ops->disable(rdev);
  1295. if (ret < 0) {
  1296. rdev_err(rdev, "failed to disable\n");
  1297. return ret;
  1298. }
  1299. trace_regulator_disable_complete(rdev_get_name(rdev));
  1300. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1301. NULL);
  1302. }
  1303. rdev->use_count = 0;
  1304. } else if (rdev->use_count > 1) {
  1305. if (rdev->constraints &&
  1306. (rdev->constraints->valid_ops_mask &
  1307. REGULATOR_CHANGE_DRMS))
  1308. drms_uA_update(rdev);
  1309. rdev->use_count--;
  1310. }
  1311. return ret;
  1312. }
  1313. /**
  1314. * regulator_disable - disable regulator output
  1315. * @regulator: regulator source
  1316. *
  1317. * Disable the regulator output voltage or current. Calls to
  1318. * regulator_enable() must be balanced with calls to
  1319. * regulator_disable().
  1320. *
  1321. * NOTE: this will only disable the regulator output if no other consumer
  1322. * devices have it enabled, the regulator device supports disabling and
  1323. * machine constraints permit this operation.
  1324. */
  1325. int regulator_disable(struct regulator *regulator)
  1326. {
  1327. struct regulator_dev *rdev = regulator->rdev;
  1328. int ret = 0;
  1329. if (regulator->always_on)
  1330. return 0;
  1331. mutex_lock(&rdev->mutex);
  1332. ret = _regulator_disable(rdev);
  1333. mutex_unlock(&rdev->mutex);
  1334. if (ret == 0 && rdev->supply)
  1335. regulator_disable(rdev->supply);
  1336. return ret;
  1337. }
  1338. EXPORT_SYMBOL_GPL(regulator_disable);
  1339. /* locks held by regulator_force_disable() */
  1340. static int _regulator_force_disable(struct regulator_dev *rdev)
  1341. {
  1342. int ret = 0;
  1343. /* force disable */
  1344. if (rdev->desc->ops->disable) {
  1345. /* ah well, who wants to live forever... */
  1346. ret = rdev->desc->ops->disable(rdev);
  1347. if (ret < 0) {
  1348. rdev_err(rdev, "failed to force disable\n");
  1349. return ret;
  1350. }
  1351. /* notify other consumers that power has been forced off */
  1352. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1353. REGULATOR_EVENT_DISABLE, NULL);
  1354. }
  1355. return ret;
  1356. }
  1357. /**
  1358. * regulator_force_disable - force disable regulator output
  1359. * @regulator: regulator source
  1360. *
  1361. * Forcibly disable the regulator output voltage or current.
  1362. * NOTE: this *will* disable the regulator output even if other consumer
  1363. * devices have it enabled. This should be used for situations when device
  1364. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1365. */
  1366. int regulator_force_disable(struct regulator *regulator)
  1367. {
  1368. struct regulator_dev *rdev = regulator->rdev;
  1369. int ret;
  1370. mutex_lock(&rdev->mutex);
  1371. regulator->uA_load = 0;
  1372. ret = _regulator_force_disable(regulator->rdev);
  1373. mutex_unlock(&rdev->mutex);
  1374. if (rdev->supply)
  1375. while (rdev->open_count--)
  1376. regulator_disable(rdev->supply);
  1377. return ret;
  1378. }
  1379. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1380. static void regulator_disable_work(struct work_struct *work)
  1381. {
  1382. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1383. disable_work.work);
  1384. int count, i, ret;
  1385. mutex_lock(&rdev->mutex);
  1386. BUG_ON(!rdev->deferred_disables);
  1387. count = rdev->deferred_disables;
  1388. rdev->deferred_disables = 0;
  1389. for (i = 0; i < count; i++) {
  1390. ret = _regulator_disable(rdev);
  1391. if (ret != 0)
  1392. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1393. }
  1394. mutex_unlock(&rdev->mutex);
  1395. if (rdev->supply) {
  1396. for (i = 0; i < count; i++) {
  1397. ret = regulator_disable(rdev->supply);
  1398. if (ret != 0) {
  1399. rdev_err(rdev,
  1400. "Supply disable failed: %d\n", ret);
  1401. }
  1402. }
  1403. }
  1404. }
  1405. /**
  1406. * regulator_disable_deferred - disable regulator output with delay
  1407. * @regulator: regulator source
  1408. * @ms: miliseconds until the regulator is disabled
  1409. *
  1410. * Execute regulator_disable() on the regulator after a delay. This
  1411. * is intended for use with devices that require some time to quiesce.
  1412. *
  1413. * NOTE: this will only disable the regulator output if no other consumer
  1414. * devices have it enabled, the regulator device supports disabling and
  1415. * machine constraints permit this operation.
  1416. */
  1417. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1418. {
  1419. struct regulator_dev *rdev = regulator->rdev;
  1420. int ret;
  1421. if (regulator->always_on)
  1422. return 0;
  1423. mutex_lock(&rdev->mutex);
  1424. rdev->deferred_disables++;
  1425. mutex_unlock(&rdev->mutex);
  1426. ret = schedule_delayed_work(&rdev->disable_work,
  1427. msecs_to_jiffies(ms));
  1428. if (ret < 0)
  1429. return ret;
  1430. else
  1431. return 0;
  1432. }
  1433. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1434. /**
  1435. * regulator_is_enabled_regmap - standard is_enabled() for regmap users
  1436. *
  1437. * @rdev: regulator to operate on
  1438. *
  1439. * Regulators that use regmap for their register I/O can set the
  1440. * enable_reg and enable_mask fields in their descriptor and then use
  1441. * this as their is_enabled operation, saving some code.
  1442. */
  1443. int regulator_is_enabled_regmap(struct regulator_dev *rdev)
  1444. {
  1445. unsigned int val;
  1446. int ret;
  1447. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  1448. if (ret != 0)
  1449. return ret;
  1450. return (val & rdev->desc->enable_mask) != 0;
  1451. }
  1452. EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
  1453. /**
  1454. * regulator_enable_regmap - standard enable() for regmap users
  1455. *
  1456. * @rdev: regulator to operate on
  1457. *
  1458. * Regulators that use regmap for their register I/O can set the
  1459. * enable_reg and enable_mask fields in their descriptor and then use
  1460. * this as their enable() operation, saving some code.
  1461. */
  1462. int regulator_enable_regmap(struct regulator_dev *rdev)
  1463. {
  1464. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  1465. rdev->desc->enable_mask,
  1466. rdev->desc->enable_mask);
  1467. }
  1468. EXPORT_SYMBOL_GPL(regulator_enable_regmap);
  1469. /**
  1470. * regulator_disable_regmap - standard disable() for regmap users
  1471. *
  1472. * @rdev: regulator to operate on
  1473. *
  1474. * Regulators that use regmap for their register I/O can set the
  1475. * enable_reg and enable_mask fields in their descriptor and then use
  1476. * this as their disable() operation, saving some code.
  1477. */
  1478. int regulator_disable_regmap(struct regulator_dev *rdev)
  1479. {
  1480. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  1481. rdev->desc->enable_mask, 0);
  1482. }
  1483. EXPORT_SYMBOL_GPL(regulator_disable_regmap);
  1484. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1485. {
  1486. /* If we don't know then assume that the regulator is always on */
  1487. if (!rdev->desc->ops->is_enabled)
  1488. return 1;
  1489. return rdev->desc->ops->is_enabled(rdev);
  1490. }
  1491. /**
  1492. * regulator_is_enabled - is the regulator output enabled
  1493. * @regulator: regulator source
  1494. *
  1495. * Returns positive if the regulator driver backing the source/client
  1496. * has requested that the device be enabled, zero if it hasn't, else a
  1497. * negative errno code.
  1498. *
  1499. * Note that the device backing this regulator handle can have multiple
  1500. * users, so it might be enabled even if regulator_enable() was never
  1501. * called for this particular source.
  1502. */
  1503. int regulator_is_enabled(struct regulator *regulator)
  1504. {
  1505. int ret;
  1506. if (regulator->always_on)
  1507. return 1;
  1508. mutex_lock(&regulator->rdev->mutex);
  1509. ret = _regulator_is_enabled(regulator->rdev);
  1510. mutex_unlock(&regulator->rdev->mutex);
  1511. return ret;
  1512. }
  1513. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1514. /**
  1515. * regulator_count_voltages - count regulator_list_voltage() selectors
  1516. * @regulator: regulator source
  1517. *
  1518. * Returns number of selectors, or negative errno. Selectors are
  1519. * numbered starting at zero, and typically correspond to bitfields
  1520. * in hardware registers.
  1521. */
  1522. int regulator_count_voltages(struct regulator *regulator)
  1523. {
  1524. struct regulator_dev *rdev = regulator->rdev;
  1525. return rdev->desc->n_voltages ? : -EINVAL;
  1526. }
  1527. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1528. /**
  1529. * regulator_list_voltage_linear - List voltages with simple calculation
  1530. *
  1531. * @rdev: Regulator device
  1532. * @selector: Selector to convert into a voltage
  1533. *
  1534. * Regulators with a simple linear mapping between voltages and
  1535. * selectors can set min_uV and uV_step in the regulator descriptor
  1536. * and then use this function as their list_voltage() operation,
  1537. */
  1538. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  1539. unsigned int selector)
  1540. {
  1541. if (selector >= rdev->desc->n_voltages)
  1542. return -EINVAL;
  1543. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  1544. }
  1545. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  1546. /**
  1547. * regulator_list_voltage_table - List voltages with table based mapping
  1548. *
  1549. * @rdev: Regulator device
  1550. * @selector: Selector to convert into a voltage
  1551. *
  1552. * Regulators with table based mapping between voltages and
  1553. * selectors can set volt_table in the regulator descriptor
  1554. * and then use this function as their list_voltage() operation.
  1555. */
  1556. int regulator_list_voltage_table(struct regulator_dev *rdev,
  1557. unsigned int selector)
  1558. {
  1559. if (!rdev->desc->volt_table) {
  1560. BUG_ON(!rdev->desc->volt_table);
  1561. return -EINVAL;
  1562. }
  1563. if (selector >= rdev->desc->n_voltages)
  1564. return -EINVAL;
  1565. return rdev->desc->volt_table[selector];
  1566. }
  1567. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  1568. /**
  1569. * regulator_list_voltage - enumerate supported voltages
  1570. * @regulator: regulator source
  1571. * @selector: identify voltage to list
  1572. * Context: can sleep
  1573. *
  1574. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1575. * zero if this selector code can't be used on this system, or a
  1576. * negative errno.
  1577. */
  1578. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1579. {
  1580. struct regulator_dev *rdev = regulator->rdev;
  1581. struct regulator_ops *ops = rdev->desc->ops;
  1582. int ret;
  1583. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1584. return -EINVAL;
  1585. mutex_lock(&rdev->mutex);
  1586. ret = ops->list_voltage(rdev, selector);
  1587. mutex_unlock(&rdev->mutex);
  1588. if (ret > 0) {
  1589. if (ret < rdev->constraints->min_uV)
  1590. ret = 0;
  1591. else if (ret > rdev->constraints->max_uV)
  1592. ret = 0;
  1593. }
  1594. return ret;
  1595. }
  1596. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1597. /**
  1598. * regulator_is_supported_voltage - check if a voltage range can be supported
  1599. *
  1600. * @regulator: Regulator to check.
  1601. * @min_uV: Minimum required voltage in uV.
  1602. * @max_uV: Maximum required voltage in uV.
  1603. *
  1604. * Returns a boolean or a negative error code.
  1605. */
  1606. int regulator_is_supported_voltage(struct regulator *regulator,
  1607. int min_uV, int max_uV)
  1608. {
  1609. int i, voltages, ret;
  1610. ret = regulator_count_voltages(regulator);
  1611. if (ret < 0)
  1612. return ret;
  1613. voltages = ret;
  1614. for (i = 0; i < voltages; i++) {
  1615. ret = regulator_list_voltage(regulator, i);
  1616. if (ret >= min_uV && ret <= max_uV)
  1617. return 1;
  1618. }
  1619. return 0;
  1620. }
  1621. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  1622. /**
  1623. * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
  1624. *
  1625. * @rdev: regulator to operate on
  1626. *
  1627. * Regulators that use regmap for their register I/O can set the
  1628. * vsel_reg and vsel_mask fields in their descriptor and then use this
  1629. * as their get_voltage_vsel operation, saving some code.
  1630. */
  1631. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
  1632. {
  1633. unsigned int val;
  1634. int ret;
  1635. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  1636. if (ret != 0)
  1637. return ret;
  1638. val &= rdev->desc->vsel_mask;
  1639. val >>= ffs(rdev->desc->vsel_mask) - 1;
  1640. return val;
  1641. }
  1642. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
  1643. /**
  1644. * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
  1645. *
  1646. * @rdev: regulator to operate on
  1647. * @sel: Selector to set
  1648. *
  1649. * Regulators that use regmap for their register I/O can set the
  1650. * vsel_reg and vsel_mask fields in their descriptor and then use this
  1651. * as their set_voltage_vsel operation, saving some code.
  1652. */
  1653. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
  1654. {
  1655. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  1656. return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  1657. rdev->desc->vsel_mask, sel);
  1658. }
  1659. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
  1660. /**
  1661. * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  1662. *
  1663. * @rdev: Regulator to operate on
  1664. * @min_uV: Lower bound for voltage
  1665. * @max_uV: Upper bound for voltage
  1666. *
  1667. * Drivers implementing set_voltage_sel() and list_voltage() can use
  1668. * this as their map_voltage() operation. It will find a suitable
  1669. * voltage by calling list_voltage() until it gets something in bounds
  1670. * for the requested voltages.
  1671. */
  1672. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  1673. int min_uV, int max_uV)
  1674. {
  1675. int best_val = INT_MAX;
  1676. int selector = 0;
  1677. int i, ret;
  1678. /* Find the smallest voltage that falls within the specified
  1679. * range.
  1680. */
  1681. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1682. ret = rdev->desc->ops->list_voltage(rdev, i);
  1683. if (ret < 0)
  1684. continue;
  1685. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1686. best_val = ret;
  1687. selector = i;
  1688. }
  1689. }
  1690. if (best_val != INT_MAX)
  1691. return selector;
  1692. else
  1693. return -EINVAL;
  1694. }
  1695. EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
  1696. /**
  1697. * regulator_map_voltage_linear - map_voltage() for simple linear mappings
  1698. *
  1699. * @rdev: Regulator to operate on
  1700. * @min_uV: Lower bound for voltage
  1701. * @max_uV: Upper bound for voltage
  1702. *
  1703. * Drivers providing min_uV and uV_step in their regulator_desc can
  1704. * use this as their map_voltage() operation.
  1705. */
  1706. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  1707. int min_uV, int max_uV)
  1708. {
  1709. int ret, voltage;
  1710. if (!rdev->desc->uV_step) {
  1711. BUG_ON(!rdev->desc->uV_step);
  1712. return -EINVAL;
  1713. }
  1714. ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  1715. if (ret < 0)
  1716. return ret;
  1717. /* Map back into a voltage to verify we're still in bounds */
  1718. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  1719. if (voltage < min_uV || voltage > max_uV)
  1720. return -EINVAL;
  1721. return ret;
  1722. }
  1723. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
  1724. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1725. int min_uV, int max_uV)
  1726. {
  1727. int ret;
  1728. int delay = 0;
  1729. int best_val;
  1730. unsigned int selector;
  1731. int old_selector = -1;
  1732. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1733. min_uV += rdev->constraints->uV_offset;
  1734. max_uV += rdev->constraints->uV_offset;
  1735. /*
  1736. * If we can't obtain the old selector there is not enough
  1737. * info to call set_voltage_time_sel().
  1738. */
  1739. if (_regulator_is_enabled(rdev) &&
  1740. rdev->desc->ops->set_voltage_time_sel &&
  1741. rdev->desc->ops->get_voltage_sel) {
  1742. old_selector = rdev->desc->ops->get_voltage_sel(rdev);
  1743. if (old_selector < 0)
  1744. return old_selector;
  1745. }
  1746. if (rdev->desc->ops->set_voltage) {
  1747. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1748. &selector);
  1749. } else if (rdev->desc->ops->set_voltage_sel) {
  1750. if (rdev->desc->ops->map_voltage) {
  1751. ret = rdev->desc->ops->map_voltage(rdev, min_uV,
  1752. max_uV);
  1753. } else {
  1754. if (rdev->desc->ops->list_voltage ==
  1755. regulator_list_voltage_linear)
  1756. ret = regulator_map_voltage_linear(rdev,
  1757. min_uV, max_uV);
  1758. else
  1759. ret = regulator_map_voltage_iterate(rdev,
  1760. min_uV, max_uV);
  1761. }
  1762. if (ret >= 0) {
  1763. selector = ret;
  1764. ret = rdev->desc->ops->set_voltage_sel(rdev, ret);
  1765. }
  1766. } else {
  1767. ret = -EINVAL;
  1768. }
  1769. if (rdev->desc->ops->list_voltage)
  1770. best_val = rdev->desc->ops->list_voltage(rdev, selector);
  1771. else
  1772. best_val = _regulator_get_voltage(rdev);
  1773. /* Call set_voltage_time_sel if successfully obtained old_selector */
  1774. if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
  1775. rdev->desc->ops->set_voltage_time_sel) {
  1776. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  1777. old_selector, selector);
  1778. if (delay < 0) {
  1779. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
  1780. delay);
  1781. delay = 0;
  1782. }
  1783. /* Insert any necessary delays */
  1784. if (delay >= 1000) {
  1785. mdelay(delay / 1000);
  1786. udelay(delay % 1000);
  1787. } else if (delay) {
  1788. udelay(delay);
  1789. }
  1790. }
  1791. if (ret == 0 && best_val >= 0)
  1792. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1793. (void *)best_val);
  1794. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  1795. return ret;
  1796. }
  1797. /**
  1798. * regulator_set_voltage - set regulator output voltage
  1799. * @regulator: regulator source
  1800. * @min_uV: Minimum required voltage in uV
  1801. * @max_uV: Maximum acceptable voltage in uV
  1802. *
  1803. * Sets a voltage regulator to the desired output voltage. This can be set
  1804. * during any regulator state. IOW, regulator can be disabled or enabled.
  1805. *
  1806. * If the regulator is enabled then the voltage will change to the new value
  1807. * immediately otherwise if the regulator is disabled the regulator will
  1808. * output at the new voltage when enabled.
  1809. *
  1810. * NOTE: If the regulator is shared between several devices then the lowest
  1811. * request voltage that meets the system constraints will be used.
  1812. * Regulator system constraints must be set for this regulator before
  1813. * calling this function otherwise this call will fail.
  1814. */
  1815. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1816. {
  1817. struct regulator_dev *rdev = regulator->rdev;
  1818. int ret = 0;
  1819. mutex_lock(&rdev->mutex);
  1820. /* If we're setting the same range as last time the change
  1821. * should be a noop (some cpufreq implementations use the same
  1822. * voltage for multiple frequencies, for example).
  1823. */
  1824. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1825. goto out;
  1826. /* sanity check */
  1827. if (!rdev->desc->ops->set_voltage &&
  1828. !rdev->desc->ops->set_voltage_sel) {
  1829. ret = -EINVAL;
  1830. goto out;
  1831. }
  1832. /* constraints check */
  1833. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1834. if (ret < 0)
  1835. goto out;
  1836. regulator->min_uV = min_uV;
  1837. regulator->max_uV = max_uV;
  1838. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1839. if (ret < 0)
  1840. goto out;
  1841. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1842. out:
  1843. mutex_unlock(&rdev->mutex);
  1844. return ret;
  1845. }
  1846. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1847. /**
  1848. * regulator_set_voltage_time - get raise/fall time
  1849. * @regulator: regulator source
  1850. * @old_uV: starting voltage in microvolts
  1851. * @new_uV: target voltage in microvolts
  1852. *
  1853. * Provided with the starting and ending voltage, this function attempts to
  1854. * calculate the time in microseconds required to rise or fall to this new
  1855. * voltage.
  1856. */
  1857. int regulator_set_voltage_time(struct regulator *regulator,
  1858. int old_uV, int new_uV)
  1859. {
  1860. struct regulator_dev *rdev = regulator->rdev;
  1861. struct regulator_ops *ops = rdev->desc->ops;
  1862. int old_sel = -1;
  1863. int new_sel = -1;
  1864. int voltage;
  1865. int i;
  1866. /* Currently requires operations to do this */
  1867. if (!ops->list_voltage || !ops->set_voltage_time_sel
  1868. || !rdev->desc->n_voltages)
  1869. return -EINVAL;
  1870. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1871. /* We only look for exact voltage matches here */
  1872. voltage = regulator_list_voltage(regulator, i);
  1873. if (voltage < 0)
  1874. return -EINVAL;
  1875. if (voltage == 0)
  1876. continue;
  1877. if (voltage == old_uV)
  1878. old_sel = i;
  1879. if (voltage == new_uV)
  1880. new_sel = i;
  1881. }
  1882. if (old_sel < 0 || new_sel < 0)
  1883. return -EINVAL;
  1884. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  1885. }
  1886. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  1887. /**
  1888. * regulator_sync_voltage - re-apply last regulator output voltage
  1889. * @regulator: regulator source
  1890. *
  1891. * Re-apply the last configured voltage. This is intended to be used
  1892. * where some external control source the consumer is cooperating with
  1893. * has caused the configured voltage to change.
  1894. */
  1895. int regulator_sync_voltage(struct regulator *regulator)
  1896. {
  1897. struct regulator_dev *rdev = regulator->rdev;
  1898. int ret, min_uV, max_uV;
  1899. mutex_lock(&rdev->mutex);
  1900. if (!rdev->desc->ops->set_voltage &&
  1901. !rdev->desc->ops->set_voltage_sel) {
  1902. ret = -EINVAL;
  1903. goto out;
  1904. }
  1905. /* This is only going to work if we've had a voltage configured. */
  1906. if (!regulator->min_uV && !regulator->max_uV) {
  1907. ret = -EINVAL;
  1908. goto out;
  1909. }
  1910. min_uV = regulator->min_uV;
  1911. max_uV = regulator->max_uV;
  1912. /* This should be a paranoia check... */
  1913. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1914. if (ret < 0)
  1915. goto out;
  1916. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1917. if (ret < 0)
  1918. goto out;
  1919. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1920. out:
  1921. mutex_unlock(&rdev->mutex);
  1922. return ret;
  1923. }
  1924. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  1925. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1926. {
  1927. int sel, ret;
  1928. if (rdev->desc->ops->get_voltage_sel) {
  1929. sel = rdev->desc->ops->get_voltage_sel(rdev);
  1930. if (sel < 0)
  1931. return sel;
  1932. ret = rdev->desc->ops->list_voltage(rdev, sel);
  1933. } else if (rdev->desc->ops->get_voltage) {
  1934. ret = rdev->desc->ops->get_voltage(rdev);
  1935. } else {
  1936. return -EINVAL;
  1937. }
  1938. if (ret < 0)
  1939. return ret;
  1940. return ret - rdev->constraints->uV_offset;
  1941. }
  1942. /**
  1943. * regulator_get_voltage - get regulator output voltage
  1944. * @regulator: regulator source
  1945. *
  1946. * This returns the current regulator voltage in uV.
  1947. *
  1948. * NOTE: If the regulator is disabled it will return the voltage value. This
  1949. * function should not be used to determine regulator state.
  1950. */
  1951. int regulator_get_voltage(struct regulator *regulator)
  1952. {
  1953. int ret;
  1954. mutex_lock(&regulator->rdev->mutex);
  1955. ret = _regulator_get_voltage(regulator->rdev);
  1956. mutex_unlock(&regulator->rdev->mutex);
  1957. return ret;
  1958. }
  1959. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1960. /**
  1961. * regulator_set_current_limit - set regulator output current limit
  1962. * @regulator: regulator source
  1963. * @min_uA: Minimuum supported current in uA
  1964. * @max_uA: Maximum supported current in uA
  1965. *
  1966. * Sets current sink to the desired output current. This can be set during
  1967. * any regulator state. IOW, regulator can be disabled or enabled.
  1968. *
  1969. * If the regulator is enabled then the current will change to the new value
  1970. * immediately otherwise if the regulator is disabled the regulator will
  1971. * output at the new current when enabled.
  1972. *
  1973. * NOTE: Regulator system constraints must be set for this regulator before
  1974. * calling this function otherwise this call will fail.
  1975. */
  1976. int regulator_set_current_limit(struct regulator *regulator,
  1977. int min_uA, int max_uA)
  1978. {
  1979. struct regulator_dev *rdev = regulator->rdev;
  1980. int ret;
  1981. mutex_lock(&rdev->mutex);
  1982. /* sanity check */
  1983. if (!rdev->desc->ops->set_current_limit) {
  1984. ret = -EINVAL;
  1985. goto out;
  1986. }
  1987. /* constraints check */
  1988. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1989. if (ret < 0)
  1990. goto out;
  1991. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1992. out:
  1993. mutex_unlock(&rdev->mutex);
  1994. return ret;
  1995. }
  1996. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1997. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1998. {
  1999. int ret;
  2000. mutex_lock(&rdev->mutex);
  2001. /* sanity check */
  2002. if (!rdev->desc->ops->get_current_limit) {
  2003. ret = -EINVAL;
  2004. goto out;
  2005. }
  2006. ret = rdev->desc->ops->get_current_limit(rdev);
  2007. out:
  2008. mutex_unlock(&rdev->mutex);
  2009. return ret;
  2010. }
  2011. /**
  2012. * regulator_get_current_limit - get regulator output current
  2013. * @regulator: regulator source
  2014. *
  2015. * This returns the current supplied by the specified current sink in uA.
  2016. *
  2017. * NOTE: If the regulator is disabled it will return the current value. This
  2018. * function should not be used to determine regulator state.
  2019. */
  2020. int regulator_get_current_limit(struct regulator *regulator)
  2021. {
  2022. return _regulator_get_current_limit(regulator->rdev);
  2023. }
  2024. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2025. /**
  2026. * regulator_set_mode - set regulator operating mode
  2027. * @regulator: regulator source
  2028. * @mode: operating mode - one of the REGULATOR_MODE constants
  2029. *
  2030. * Set regulator operating mode to increase regulator efficiency or improve
  2031. * regulation performance.
  2032. *
  2033. * NOTE: Regulator system constraints must be set for this regulator before
  2034. * calling this function otherwise this call will fail.
  2035. */
  2036. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2037. {
  2038. struct regulator_dev *rdev = regulator->rdev;
  2039. int ret;
  2040. int regulator_curr_mode;
  2041. mutex_lock(&rdev->mutex);
  2042. /* sanity check */
  2043. if (!rdev->desc->ops->set_mode) {
  2044. ret = -EINVAL;
  2045. goto out;
  2046. }
  2047. /* return if the same mode is requested */
  2048. if (rdev->desc->ops->get_mode) {
  2049. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2050. if (regulator_curr_mode == mode) {
  2051. ret = 0;
  2052. goto out;
  2053. }
  2054. }
  2055. /* constraints check */
  2056. ret = regulator_mode_constrain(rdev, &mode);
  2057. if (ret < 0)
  2058. goto out;
  2059. ret = rdev->desc->ops->set_mode(rdev, mode);
  2060. out:
  2061. mutex_unlock(&rdev->mutex);
  2062. return ret;
  2063. }
  2064. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2065. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2066. {
  2067. int ret;
  2068. mutex_lock(&rdev->mutex);
  2069. /* sanity check */
  2070. if (!rdev->desc->ops->get_mode) {
  2071. ret = -EINVAL;
  2072. goto out;
  2073. }
  2074. ret = rdev->desc->ops->get_mode(rdev);
  2075. out:
  2076. mutex_unlock(&rdev->mutex);
  2077. return ret;
  2078. }
  2079. /**
  2080. * regulator_get_mode - get regulator operating mode
  2081. * @regulator: regulator source
  2082. *
  2083. * Get the current regulator operating mode.
  2084. */
  2085. unsigned int regulator_get_mode(struct regulator *regulator)
  2086. {
  2087. return _regulator_get_mode(regulator->rdev);
  2088. }
  2089. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2090. /**
  2091. * regulator_set_optimum_mode - set regulator optimum operating mode
  2092. * @regulator: regulator source
  2093. * @uA_load: load current
  2094. *
  2095. * Notifies the regulator core of a new device load. This is then used by
  2096. * DRMS (if enabled by constraints) to set the most efficient regulator
  2097. * operating mode for the new regulator loading.
  2098. *
  2099. * Consumer devices notify their supply regulator of the maximum power
  2100. * they will require (can be taken from device datasheet in the power
  2101. * consumption tables) when they change operational status and hence power
  2102. * state. Examples of operational state changes that can affect power
  2103. * consumption are :-
  2104. *
  2105. * o Device is opened / closed.
  2106. * o Device I/O is about to begin or has just finished.
  2107. * o Device is idling in between work.
  2108. *
  2109. * This information is also exported via sysfs to userspace.
  2110. *
  2111. * DRMS will sum the total requested load on the regulator and change
  2112. * to the most efficient operating mode if platform constraints allow.
  2113. *
  2114. * Returns the new regulator mode or error.
  2115. */
  2116. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  2117. {
  2118. struct regulator_dev *rdev = regulator->rdev;
  2119. struct regulator *consumer;
  2120. int ret, output_uV, input_uV, total_uA_load = 0;
  2121. unsigned int mode;
  2122. mutex_lock(&rdev->mutex);
  2123. /*
  2124. * first check to see if we can set modes at all, otherwise just
  2125. * tell the consumer everything is OK.
  2126. */
  2127. regulator->uA_load = uA_load;
  2128. ret = regulator_check_drms(rdev);
  2129. if (ret < 0) {
  2130. ret = 0;
  2131. goto out;
  2132. }
  2133. if (!rdev->desc->ops->get_optimum_mode)
  2134. goto out;
  2135. /*
  2136. * we can actually do this so any errors are indicators of
  2137. * potential real failure.
  2138. */
  2139. ret = -EINVAL;
  2140. if (!rdev->desc->ops->set_mode)
  2141. goto out;
  2142. /* get output voltage */
  2143. output_uV = _regulator_get_voltage(rdev);
  2144. if (output_uV <= 0) {
  2145. rdev_err(rdev, "invalid output voltage found\n");
  2146. goto out;
  2147. }
  2148. /* get input voltage */
  2149. input_uV = 0;
  2150. if (rdev->supply)
  2151. input_uV = regulator_get_voltage(rdev->supply);
  2152. if (input_uV <= 0)
  2153. input_uV = rdev->constraints->input_uV;
  2154. if (input_uV <= 0) {
  2155. rdev_err(rdev, "invalid input voltage found\n");
  2156. goto out;
  2157. }
  2158. /* calc total requested load for this regulator */
  2159. list_for_each_entry(consumer, &rdev->consumer_list, list)
  2160. total_uA_load += consumer->uA_load;
  2161. mode = rdev->desc->ops->get_optimum_mode(rdev,
  2162. input_uV, output_uV,
  2163. total_uA_load);
  2164. ret = regulator_mode_constrain(rdev, &mode);
  2165. if (ret < 0) {
  2166. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  2167. total_uA_load, input_uV, output_uV);
  2168. goto out;
  2169. }
  2170. ret = rdev->desc->ops->set_mode(rdev, mode);
  2171. if (ret < 0) {
  2172. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  2173. goto out;
  2174. }
  2175. ret = mode;
  2176. out:
  2177. mutex_unlock(&rdev->mutex);
  2178. return ret;
  2179. }
  2180. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  2181. /**
  2182. * regulator_register_notifier - register regulator event notifier
  2183. * @regulator: regulator source
  2184. * @nb: notifier block
  2185. *
  2186. * Register notifier block to receive regulator events.
  2187. */
  2188. int regulator_register_notifier(struct regulator *regulator,
  2189. struct notifier_block *nb)
  2190. {
  2191. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2192. nb);
  2193. }
  2194. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2195. /**
  2196. * regulator_unregister_notifier - unregister regulator event notifier
  2197. * @regulator: regulator source
  2198. * @nb: notifier block
  2199. *
  2200. * Unregister regulator event notifier block.
  2201. */
  2202. int regulator_unregister_notifier(struct regulator *regulator,
  2203. struct notifier_block *nb)
  2204. {
  2205. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2206. nb);
  2207. }
  2208. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2209. /* notify regulator consumers and downstream regulator consumers.
  2210. * Note mutex must be held by caller.
  2211. */
  2212. static void _notifier_call_chain(struct regulator_dev *rdev,
  2213. unsigned long event, void *data)
  2214. {
  2215. /* call rdev chain first */
  2216. blocking_notifier_call_chain(&rdev->notifier, event, data);
  2217. }
  2218. /**
  2219. * regulator_bulk_get - get multiple regulator consumers
  2220. *
  2221. * @dev: Device to supply
  2222. * @num_consumers: Number of consumers to register
  2223. * @consumers: Configuration of consumers; clients are stored here.
  2224. *
  2225. * @return 0 on success, an errno on failure.
  2226. *
  2227. * This helper function allows drivers to get several regulator
  2228. * consumers in one operation. If any of the regulators cannot be
  2229. * acquired then any regulators that were allocated will be freed
  2230. * before returning to the caller.
  2231. */
  2232. int regulator_bulk_get(struct device *dev, int num_consumers,
  2233. struct regulator_bulk_data *consumers)
  2234. {
  2235. int i;
  2236. int ret;
  2237. for (i = 0; i < num_consumers; i++)
  2238. consumers[i].consumer = NULL;
  2239. for (i = 0; i < num_consumers; i++) {
  2240. consumers[i].consumer = regulator_get(dev,
  2241. consumers[i].supply);
  2242. if (IS_ERR(consumers[i].consumer)) {
  2243. ret = PTR_ERR(consumers[i].consumer);
  2244. dev_err(dev, "Failed to get supply '%s': %d\n",
  2245. consumers[i].supply, ret);
  2246. consumers[i].consumer = NULL;
  2247. goto err;
  2248. }
  2249. }
  2250. return 0;
  2251. err:
  2252. while (--i >= 0)
  2253. regulator_put(consumers[i].consumer);
  2254. return ret;
  2255. }
  2256. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2257. /**
  2258. * devm_regulator_bulk_get - managed get multiple regulator consumers
  2259. *
  2260. * @dev: Device to supply
  2261. * @num_consumers: Number of consumers to register
  2262. * @consumers: Configuration of consumers; clients are stored here.
  2263. *
  2264. * @return 0 on success, an errno on failure.
  2265. *
  2266. * This helper function allows drivers to get several regulator
  2267. * consumers in one operation with management, the regulators will
  2268. * automatically be freed when the device is unbound. If any of the
  2269. * regulators cannot be acquired then any regulators that were
  2270. * allocated will be freed before returning to the caller.
  2271. */
  2272. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  2273. struct regulator_bulk_data *consumers)
  2274. {
  2275. int i;
  2276. int ret;
  2277. for (i = 0; i < num_consumers; i++)
  2278. consumers[i].consumer = NULL;
  2279. for (i = 0; i < num_consumers; i++) {
  2280. consumers[i].consumer = devm_regulator_get(dev,
  2281. consumers[i].supply);
  2282. if (IS_ERR(consumers[i].consumer)) {
  2283. ret = PTR_ERR(consumers[i].consumer);
  2284. dev_err(dev, "Failed to get supply '%s': %d\n",
  2285. consumers[i].supply, ret);
  2286. consumers[i].consumer = NULL;
  2287. goto err;
  2288. }
  2289. }
  2290. return 0;
  2291. err:
  2292. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  2293. devm_regulator_put(consumers[i].consumer);
  2294. return ret;
  2295. }
  2296. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  2297. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2298. {
  2299. struct regulator_bulk_data *bulk = data;
  2300. bulk->ret = regulator_enable(bulk->consumer);
  2301. }
  2302. /**
  2303. * regulator_bulk_enable - enable multiple regulator consumers
  2304. *
  2305. * @num_consumers: Number of consumers
  2306. * @consumers: Consumer data; clients are stored here.
  2307. * @return 0 on success, an errno on failure
  2308. *
  2309. * This convenience API allows consumers to enable multiple regulator
  2310. * clients in a single API call. If any consumers cannot be enabled
  2311. * then any others that were enabled will be disabled again prior to
  2312. * return.
  2313. */
  2314. int regulator_bulk_enable(int num_consumers,
  2315. struct regulator_bulk_data *consumers)
  2316. {
  2317. LIST_HEAD(async_domain);
  2318. int i;
  2319. int ret = 0;
  2320. for (i = 0; i < num_consumers; i++) {
  2321. if (consumers[i].consumer->always_on)
  2322. consumers[i].ret = 0;
  2323. else
  2324. async_schedule_domain(regulator_bulk_enable_async,
  2325. &consumers[i], &async_domain);
  2326. }
  2327. async_synchronize_full_domain(&async_domain);
  2328. /* If any consumer failed we need to unwind any that succeeded */
  2329. for (i = 0; i < num_consumers; i++) {
  2330. if (consumers[i].ret != 0) {
  2331. ret = consumers[i].ret;
  2332. goto err;
  2333. }
  2334. }
  2335. return 0;
  2336. err:
  2337. pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
  2338. while (--i >= 0)
  2339. regulator_disable(consumers[i].consumer);
  2340. return ret;
  2341. }
  2342. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2343. /**
  2344. * regulator_bulk_disable - disable multiple regulator consumers
  2345. *
  2346. * @num_consumers: Number of consumers
  2347. * @consumers: Consumer data; clients are stored here.
  2348. * @return 0 on success, an errno on failure
  2349. *
  2350. * This convenience API allows consumers to disable multiple regulator
  2351. * clients in a single API call. If any consumers cannot be disabled
  2352. * then any others that were disabled will be enabled again prior to
  2353. * return.
  2354. */
  2355. int regulator_bulk_disable(int num_consumers,
  2356. struct regulator_bulk_data *consumers)
  2357. {
  2358. int i;
  2359. int ret, r;
  2360. for (i = num_consumers - 1; i >= 0; --i) {
  2361. ret = regulator_disable(consumers[i].consumer);
  2362. if (ret != 0)
  2363. goto err;
  2364. }
  2365. return 0;
  2366. err:
  2367. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2368. for (++i; i < num_consumers; ++i) {
  2369. r = regulator_enable(consumers[i].consumer);
  2370. if (r != 0)
  2371. pr_err("Failed to reename %s: %d\n",
  2372. consumers[i].supply, r);
  2373. }
  2374. return ret;
  2375. }
  2376. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2377. /**
  2378. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2379. *
  2380. * @num_consumers: Number of consumers
  2381. * @consumers: Consumer data; clients are stored here.
  2382. * @return 0 on success, an errno on failure
  2383. *
  2384. * This convenience API allows consumers to forcibly disable multiple regulator
  2385. * clients in a single API call.
  2386. * NOTE: This should be used for situations when device damage will
  2387. * likely occur if the regulators are not disabled (e.g. over temp).
  2388. * Although regulator_force_disable function call for some consumers can
  2389. * return error numbers, the function is called for all consumers.
  2390. */
  2391. int regulator_bulk_force_disable(int num_consumers,
  2392. struct regulator_bulk_data *consumers)
  2393. {
  2394. int i;
  2395. int ret;
  2396. for (i = 0; i < num_consumers; i++)
  2397. consumers[i].ret =
  2398. regulator_force_disable(consumers[i].consumer);
  2399. for (i = 0; i < num_consumers; i++) {
  2400. if (consumers[i].ret != 0) {
  2401. ret = consumers[i].ret;
  2402. goto out;
  2403. }
  2404. }
  2405. return 0;
  2406. out:
  2407. return ret;
  2408. }
  2409. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2410. /**
  2411. * regulator_bulk_free - free multiple regulator consumers
  2412. *
  2413. * @num_consumers: Number of consumers
  2414. * @consumers: Consumer data; clients are stored here.
  2415. *
  2416. * This convenience API allows consumers to free multiple regulator
  2417. * clients in a single API call.
  2418. */
  2419. void regulator_bulk_free(int num_consumers,
  2420. struct regulator_bulk_data *consumers)
  2421. {
  2422. int i;
  2423. for (i = 0; i < num_consumers; i++) {
  2424. regulator_put(consumers[i].consumer);
  2425. consumers[i].consumer = NULL;
  2426. }
  2427. }
  2428. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2429. /**
  2430. * regulator_notifier_call_chain - call regulator event notifier
  2431. * @rdev: regulator source
  2432. * @event: notifier block
  2433. * @data: callback-specific data.
  2434. *
  2435. * Called by regulator drivers to notify clients a regulator event has
  2436. * occurred. We also notify regulator clients downstream.
  2437. * Note lock must be held by caller.
  2438. */
  2439. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2440. unsigned long event, void *data)
  2441. {
  2442. _notifier_call_chain(rdev, event, data);
  2443. return NOTIFY_DONE;
  2444. }
  2445. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2446. /**
  2447. * regulator_mode_to_status - convert a regulator mode into a status
  2448. *
  2449. * @mode: Mode to convert
  2450. *
  2451. * Convert a regulator mode into a status.
  2452. */
  2453. int regulator_mode_to_status(unsigned int mode)
  2454. {
  2455. switch (mode) {
  2456. case REGULATOR_MODE_FAST:
  2457. return REGULATOR_STATUS_FAST;
  2458. case REGULATOR_MODE_NORMAL:
  2459. return REGULATOR_STATUS_NORMAL;
  2460. case REGULATOR_MODE_IDLE:
  2461. return REGULATOR_STATUS_IDLE;
  2462. case REGULATOR_STATUS_STANDBY:
  2463. return REGULATOR_STATUS_STANDBY;
  2464. default:
  2465. return 0;
  2466. }
  2467. }
  2468. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2469. /*
  2470. * To avoid cluttering sysfs (and memory) with useless state, only
  2471. * create attributes that can be meaningfully displayed.
  2472. */
  2473. static int add_regulator_attributes(struct regulator_dev *rdev)
  2474. {
  2475. struct device *dev = &rdev->dev;
  2476. struct regulator_ops *ops = rdev->desc->ops;
  2477. int status = 0;
  2478. /* some attributes need specific methods to be displayed */
  2479. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2480. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
  2481. status = device_create_file(dev, &dev_attr_microvolts);
  2482. if (status < 0)
  2483. return status;
  2484. }
  2485. if (ops->get_current_limit) {
  2486. status = device_create_file(dev, &dev_attr_microamps);
  2487. if (status < 0)
  2488. return status;
  2489. }
  2490. if (ops->get_mode) {
  2491. status = device_create_file(dev, &dev_attr_opmode);
  2492. if (status < 0)
  2493. return status;
  2494. }
  2495. if (ops->is_enabled) {
  2496. status = device_create_file(dev, &dev_attr_state);
  2497. if (status < 0)
  2498. return status;
  2499. }
  2500. if (ops->get_status) {
  2501. status = device_create_file(dev, &dev_attr_status);
  2502. if (status < 0)
  2503. return status;
  2504. }
  2505. /* some attributes are type-specific */
  2506. if (rdev->desc->type == REGULATOR_CURRENT) {
  2507. status = device_create_file(dev, &dev_attr_requested_microamps);
  2508. if (status < 0)
  2509. return status;
  2510. }
  2511. /* all the other attributes exist to support constraints;
  2512. * don't show them if there are no constraints, or if the
  2513. * relevant supporting methods are missing.
  2514. */
  2515. if (!rdev->constraints)
  2516. return status;
  2517. /* constraints need specific supporting methods */
  2518. if (ops->set_voltage || ops->set_voltage_sel) {
  2519. status = device_create_file(dev, &dev_attr_min_microvolts);
  2520. if (status < 0)
  2521. return status;
  2522. status = device_create_file(dev, &dev_attr_max_microvolts);
  2523. if (status < 0)
  2524. return status;
  2525. }
  2526. if (ops->set_current_limit) {
  2527. status = device_create_file(dev, &dev_attr_min_microamps);
  2528. if (status < 0)
  2529. return status;
  2530. status = device_create_file(dev, &dev_attr_max_microamps);
  2531. if (status < 0)
  2532. return status;
  2533. }
  2534. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2535. if (status < 0)
  2536. return status;
  2537. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2538. if (status < 0)
  2539. return status;
  2540. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2541. if (status < 0)
  2542. return status;
  2543. if (ops->set_suspend_voltage) {
  2544. status = device_create_file(dev,
  2545. &dev_attr_suspend_standby_microvolts);
  2546. if (status < 0)
  2547. return status;
  2548. status = device_create_file(dev,
  2549. &dev_attr_suspend_mem_microvolts);
  2550. if (status < 0)
  2551. return status;
  2552. status = device_create_file(dev,
  2553. &dev_attr_suspend_disk_microvolts);
  2554. if (status < 0)
  2555. return status;
  2556. }
  2557. if (ops->set_suspend_mode) {
  2558. status = device_create_file(dev,
  2559. &dev_attr_suspend_standby_mode);
  2560. if (status < 0)
  2561. return status;
  2562. status = device_create_file(dev,
  2563. &dev_attr_suspend_mem_mode);
  2564. if (status < 0)
  2565. return status;
  2566. status = device_create_file(dev,
  2567. &dev_attr_suspend_disk_mode);
  2568. if (status < 0)
  2569. return status;
  2570. }
  2571. return status;
  2572. }
  2573. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2574. {
  2575. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2576. if (!rdev->debugfs) {
  2577. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2578. return;
  2579. }
  2580. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2581. &rdev->use_count);
  2582. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2583. &rdev->open_count);
  2584. }
  2585. /**
  2586. * regulator_register - register regulator
  2587. * @regulator_desc: regulator to register
  2588. * @config: runtime configuration for regulator
  2589. *
  2590. * Called by regulator drivers to register a regulator.
  2591. * Returns 0 on success.
  2592. */
  2593. struct regulator_dev *
  2594. regulator_register(const struct regulator_desc *regulator_desc,
  2595. const struct regulator_config *config)
  2596. {
  2597. const struct regulation_constraints *constraints = NULL;
  2598. const struct regulator_init_data *init_data;
  2599. static atomic_t regulator_no = ATOMIC_INIT(0);
  2600. struct regulator_dev *rdev;
  2601. struct device *dev;
  2602. int ret, i;
  2603. const char *supply = NULL;
  2604. if (regulator_desc == NULL || config == NULL)
  2605. return ERR_PTR(-EINVAL);
  2606. dev = config->dev;
  2607. WARN_ON(!dev);
  2608. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2609. return ERR_PTR(-EINVAL);
  2610. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2611. regulator_desc->type != REGULATOR_CURRENT)
  2612. return ERR_PTR(-EINVAL);
  2613. /* Only one of each should be implemented */
  2614. WARN_ON(regulator_desc->ops->get_voltage &&
  2615. regulator_desc->ops->get_voltage_sel);
  2616. WARN_ON(regulator_desc->ops->set_voltage &&
  2617. regulator_desc->ops->set_voltage_sel);
  2618. /* If we're using selectors we must implement list_voltage. */
  2619. if (regulator_desc->ops->get_voltage_sel &&
  2620. !regulator_desc->ops->list_voltage) {
  2621. return ERR_PTR(-EINVAL);
  2622. }
  2623. if (regulator_desc->ops->set_voltage_sel &&
  2624. !regulator_desc->ops->list_voltage) {
  2625. return ERR_PTR(-EINVAL);
  2626. }
  2627. init_data = config->init_data;
  2628. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2629. if (rdev == NULL)
  2630. return ERR_PTR(-ENOMEM);
  2631. mutex_lock(&regulator_list_mutex);
  2632. mutex_init(&rdev->mutex);
  2633. rdev->reg_data = config->driver_data;
  2634. rdev->owner = regulator_desc->owner;
  2635. rdev->desc = regulator_desc;
  2636. if (config->regmap)
  2637. rdev->regmap = config->regmap;
  2638. else
  2639. rdev->regmap = dev_get_regmap(dev, NULL);
  2640. INIT_LIST_HEAD(&rdev->consumer_list);
  2641. INIT_LIST_HEAD(&rdev->list);
  2642. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2643. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  2644. /* preform any regulator specific init */
  2645. if (init_data && init_data->regulator_init) {
  2646. ret = init_data->regulator_init(rdev->reg_data);
  2647. if (ret < 0)
  2648. goto clean;
  2649. }
  2650. /* register with sysfs */
  2651. rdev->dev.class = &regulator_class;
  2652. rdev->dev.of_node = config->of_node;
  2653. rdev->dev.parent = dev;
  2654. dev_set_name(&rdev->dev, "regulator.%d",
  2655. atomic_inc_return(&regulator_no) - 1);
  2656. ret = device_register(&rdev->dev);
  2657. if (ret != 0) {
  2658. put_device(&rdev->dev);
  2659. goto clean;
  2660. }
  2661. dev_set_drvdata(&rdev->dev, rdev);
  2662. /* set regulator constraints */
  2663. if (init_data)
  2664. constraints = &init_data->constraints;
  2665. ret = set_machine_constraints(rdev, constraints);
  2666. if (ret < 0)
  2667. goto scrub;
  2668. /* add attributes supported by this regulator */
  2669. ret = add_regulator_attributes(rdev);
  2670. if (ret < 0)
  2671. goto scrub;
  2672. if (init_data && init_data->supply_regulator)
  2673. supply = init_data->supply_regulator;
  2674. else if (regulator_desc->supply_name)
  2675. supply = regulator_desc->supply_name;
  2676. if (supply) {
  2677. struct regulator_dev *r;
  2678. r = regulator_dev_lookup(dev, supply, &ret);
  2679. if (!r) {
  2680. dev_err(dev, "Failed to find supply %s\n", supply);
  2681. ret = -EPROBE_DEFER;
  2682. goto scrub;
  2683. }
  2684. ret = set_supply(rdev, r);
  2685. if (ret < 0)
  2686. goto scrub;
  2687. /* Enable supply if rail is enabled */
  2688. if (_regulator_is_enabled(rdev)) {
  2689. ret = regulator_enable(rdev->supply);
  2690. if (ret < 0)
  2691. goto scrub;
  2692. }
  2693. }
  2694. /* add consumers devices */
  2695. if (init_data) {
  2696. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2697. ret = set_consumer_device_supply(rdev,
  2698. init_data->consumer_supplies[i].dev_name,
  2699. init_data->consumer_supplies[i].supply);
  2700. if (ret < 0) {
  2701. dev_err(dev, "Failed to set supply %s\n",
  2702. init_data->consumer_supplies[i].supply);
  2703. goto unset_supplies;
  2704. }
  2705. }
  2706. }
  2707. list_add(&rdev->list, &regulator_list);
  2708. rdev_init_debugfs(rdev);
  2709. out:
  2710. mutex_unlock(&regulator_list_mutex);
  2711. return rdev;
  2712. unset_supplies:
  2713. unset_regulator_supplies(rdev);
  2714. scrub:
  2715. if (rdev->supply)
  2716. regulator_put(rdev->supply);
  2717. kfree(rdev->constraints);
  2718. device_unregister(&rdev->dev);
  2719. /* device core frees rdev */
  2720. rdev = ERR_PTR(ret);
  2721. goto out;
  2722. clean:
  2723. kfree(rdev);
  2724. rdev = ERR_PTR(ret);
  2725. goto out;
  2726. }
  2727. EXPORT_SYMBOL_GPL(regulator_register);
  2728. /**
  2729. * regulator_unregister - unregister regulator
  2730. * @rdev: regulator to unregister
  2731. *
  2732. * Called by regulator drivers to unregister a regulator.
  2733. */
  2734. void regulator_unregister(struct regulator_dev *rdev)
  2735. {
  2736. if (rdev == NULL)
  2737. return;
  2738. if (rdev->supply)
  2739. regulator_put(rdev->supply);
  2740. mutex_lock(&regulator_list_mutex);
  2741. debugfs_remove_recursive(rdev->debugfs);
  2742. flush_work_sync(&rdev->disable_work.work);
  2743. WARN_ON(rdev->open_count);
  2744. unset_regulator_supplies(rdev);
  2745. list_del(&rdev->list);
  2746. kfree(rdev->constraints);
  2747. device_unregister(&rdev->dev);
  2748. mutex_unlock(&regulator_list_mutex);
  2749. }
  2750. EXPORT_SYMBOL_GPL(regulator_unregister);
  2751. /**
  2752. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2753. * @state: system suspend state
  2754. *
  2755. * Configure each regulator with it's suspend operating parameters for state.
  2756. * This will usually be called by machine suspend code prior to supending.
  2757. */
  2758. int regulator_suspend_prepare(suspend_state_t state)
  2759. {
  2760. struct regulator_dev *rdev;
  2761. int ret = 0;
  2762. /* ON is handled by regulator active state */
  2763. if (state == PM_SUSPEND_ON)
  2764. return -EINVAL;
  2765. mutex_lock(&regulator_list_mutex);
  2766. list_for_each_entry(rdev, &regulator_list, list) {
  2767. mutex_lock(&rdev->mutex);
  2768. ret = suspend_prepare(rdev, state);
  2769. mutex_unlock(&rdev->mutex);
  2770. if (ret < 0) {
  2771. rdev_err(rdev, "failed to prepare\n");
  2772. goto out;
  2773. }
  2774. }
  2775. out:
  2776. mutex_unlock(&regulator_list_mutex);
  2777. return ret;
  2778. }
  2779. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2780. /**
  2781. * regulator_suspend_finish - resume regulators from system wide suspend
  2782. *
  2783. * Turn on regulators that might be turned off by regulator_suspend_prepare
  2784. * and that should be turned on according to the regulators properties.
  2785. */
  2786. int regulator_suspend_finish(void)
  2787. {
  2788. struct regulator_dev *rdev;
  2789. int ret = 0, error;
  2790. mutex_lock(&regulator_list_mutex);
  2791. list_for_each_entry(rdev, &regulator_list, list) {
  2792. struct regulator_ops *ops = rdev->desc->ops;
  2793. mutex_lock(&rdev->mutex);
  2794. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  2795. ops->enable) {
  2796. error = ops->enable(rdev);
  2797. if (error)
  2798. ret = error;
  2799. } else {
  2800. if (!has_full_constraints)
  2801. goto unlock;
  2802. if (!ops->disable)
  2803. goto unlock;
  2804. if (!_regulator_is_enabled(rdev))
  2805. goto unlock;
  2806. error = ops->disable(rdev);
  2807. if (error)
  2808. ret = error;
  2809. }
  2810. unlock:
  2811. mutex_unlock(&rdev->mutex);
  2812. }
  2813. mutex_unlock(&regulator_list_mutex);
  2814. return ret;
  2815. }
  2816. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  2817. /**
  2818. * regulator_has_full_constraints - the system has fully specified constraints
  2819. *
  2820. * Calling this function will cause the regulator API to disable all
  2821. * regulators which have a zero use count and don't have an always_on
  2822. * constraint in a late_initcall.
  2823. *
  2824. * The intention is that this will become the default behaviour in a
  2825. * future kernel release so users are encouraged to use this facility
  2826. * now.
  2827. */
  2828. void regulator_has_full_constraints(void)
  2829. {
  2830. has_full_constraints = 1;
  2831. }
  2832. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2833. /**
  2834. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2835. *
  2836. * Calling this function will cause the regulator API to provide a
  2837. * dummy regulator to consumers if no physical regulator is found,
  2838. * allowing most consumers to proceed as though a regulator were
  2839. * configured. This allows systems such as those with software
  2840. * controllable regulators for the CPU core only to be brought up more
  2841. * readily.
  2842. */
  2843. void regulator_use_dummy_regulator(void)
  2844. {
  2845. board_wants_dummy_regulator = true;
  2846. }
  2847. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2848. /**
  2849. * rdev_get_drvdata - get rdev regulator driver data
  2850. * @rdev: regulator
  2851. *
  2852. * Get rdev regulator driver private data. This call can be used in the
  2853. * regulator driver context.
  2854. */
  2855. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2856. {
  2857. return rdev->reg_data;
  2858. }
  2859. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2860. /**
  2861. * regulator_get_drvdata - get regulator driver data
  2862. * @regulator: regulator
  2863. *
  2864. * Get regulator driver private data. This call can be used in the consumer
  2865. * driver context when non API regulator specific functions need to be called.
  2866. */
  2867. void *regulator_get_drvdata(struct regulator *regulator)
  2868. {
  2869. return regulator->rdev->reg_data;
  2870. }
  2871. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2872. /**
  2873. * regulator_set_drvdata - set regulator driver data
  2874. * @regulator: regulator
  2875. * @data: data
  2876. */
  2877. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2878. {
  2879. regulator->rdev->reg_data = data;
  2880. }
  2881. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2882. /**
  2883. * regulator_get_id - get regulator ID
  2884. * @rdev: regulator
  2885. */
  2886. int rdev_get_id(struct regulator_dev *rdev)
  2887. {
  2888. return rdev->desc->id;
  2889. }
  2890. EXPORT_SYMBOL_GPL(rdev_get_id);
  2891. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2892. {
  2893. return &rdev->dev;
  2894. }
  2895. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2896. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2897. {
  2898. return reg_init_data->driver_data;
  2899. }
  2900. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2901. #ifdef CONFIG_DEBUG_FS
  2902. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  2903. size_t count, loff_t *ppos)
  2904. {
  2905. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2906. ssize_t len, ret = 0;
  2907. struct regulator_map *map;
  2908. if (!buf)
  2909. return -ENOMEM;
  2910. list_for_each_entry(map, &regulator_map_list, list) {
  2911. len = snprintf(buf + ret, PAGE_SIZE - ret,
  2912. "%s -> %s.%s\n",
  2913. rdev_get_name(map->regulator), map->dev_name,
  2914. map->supply);
  2915. if (len >= 0)
  2916. ret += len;
  2917. if (ret > PAGE_SIZE) {
  2918. ret = PAGE_SIZE;
  2919. break;
  2920. }
  2921. }
  2922. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  2923. kfree(buf);
  2924. return ret;
  2925. }
  2926. #endif
  2927. static const struct file_operations supply_map_fops = {
  2928. #ifdef CONFIG_DEBUG_FS
  2929. .read = supply_map_read_file,
  2930. .llseek = default_llseek,
  2931. #endif
  2932. };
  2933. static int __init regulator_init(void)
  2934. {
  2935. int ret;
  2936. ret = class_register(&regulator_class);
  2937. debugfs_root = debugfs_create_dir("regulator", NULL);
  2938. if (!debugfs_root)
  2939. pr_warn("regulator: Failed to create debugfs directory\n");
  2940. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  2941. &supply_map_fops);
  2942. regulator_dummy_init();
  2943. return ret;
  2944. }
  2945. /* init early to allow our consumers to complete system booting */
  2946. core_initcall(regulator_init);
  2947. static int __init regulator_init_complete(void)
  2948. {
  2949. struct regulator_dev *rdev;
  2950. struct regulator_ops *ops;
  2951. struct regulation_constraints *c;
  2952. int enabled, ret;
  2953. mutex_lock(&regulator_list_mutex);
  2954. /* If we have a full configuration then disable any regulators
  2955. * which are not in use or always_on. This will become the
  2956. * default behaviour in the future.
  2957. */
  2958. list_for_each_entry(rdev, &regulator_list, list) {
  2959. ops = rdev->desc->ops;
  2960. c = rdev->constraints;
  2961. if (!ops->disable || (c && c->always_on))
  2962. continue;
  2963. mutex_lock(&rdev->mutex);
  2964. if (rdev->use_count)
  2965. goto unlock;
  2966. /* If we can't read the status assume it's on. */
  2967. if (ops->is_enabled)
  2968. enabled = ops->is_enabled(rdev);
  2969. else
  2970. enabled = 1;
  2971. if (!enabled)
  2972. goto unlock;
  2973. if (has_full_constraints) {
  2974. /* We log since this may kill the system if it
  2975. * goes wrong. */
  2976. rdev_info(rdev, "disabling\n");
  2977. ret = ops->disable(rdev);
  2978. if (ret != 0) {
  2979. rdev_err(rdev, "couldn't disable: %d\n", ret);
  2980. }
  2981. } else {
  2982. /* The intention is that in future we will
  2983. * assume that full constraints are provided
  2984. * so warn even if we aren't going to do
  2985. * anything here.
  2986. */
  2987. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  2988. }
  2989. unlock:
  2990. mutex_unlock(&rdev->mutex);
  2991. }
  2992. mutex_unlock(&regulator_list_mutex);
  2993. return 0;
  2994. }
  2995. late_initcall(regulator_init_complete);