core.c 97 KB

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