core.c 98 KB

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