core.c 108 KB

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