core.c 107 KB

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