core.c 93 KB

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