core.c 92 KB

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