core.c 97 KB

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