core.c 87 KB

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