core.c 103 KB

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