core.c 93 KB

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