core.c 105 KB

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