core.c 97 KB

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