core.c 105 KB

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