core.c 109 KB

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