core.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066
  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_info(rdev, "%s\n", buf);
  709. if ((constraints->min_uV != constraints->max_uV) &&
  710. !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
  711. rdev_warn(rdev,
  712. "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
  713. }
  714. static int machine_constraints_voltage(struct regulator_dev *rdev,
  715. struct regulation_constraints *constraints)
  716. {
  717. 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. /* Locks 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. kfree(regulator->supply_name);
  1284. list_del(&regulator->list);
  1285. kfree(regulator);
  1286. rdev->open_count--;
  1287. rdev->exclusive = 0;
  1288. module_put(rdev->owner);
  1289. }
  1290. /**
  1291. * regulator_put - "free" the regulator source
  1292. * @regulator: regulator source
  1293. *
  1294. * Note: drivers must ensure that all regulator_enable calls made on this
  1295. * regulator source are balanced by regulator_disable calls prior to calling
  1296. * this function.
  1297. */
  1298. void regulator_put(struct regulator *regulator)
  1299. {
  1300. mutex_lock(&regulator_list_mutex);
  1301. _regulator_put(regulator);
  1302. mutex_unlock(&regulator_list_mutex);
  1303. }
  1304. EXPORT_SYMBOL_GPL(regulator_put);
  1305. /**
  1306. * regulator_register_supply_alias - Provide device alias for supply lookup
  1307. *
  1308. * @dev: device that will be given as the regulator "consumer"
  1309. * @id: Supply name or regulator ID
  1310. * @alias_dev: device that should be used to lookup the supply
  1311. * @alias_id: Supply name or regulator ID that should be used to lookup the
  1312. * supply
  1313. *
  1314. * All lookups for id on dev will instead be conducted for alias_id on
  1315. * alias_dev.
  1316. */
  1317. int regulator_register_supply_alias(struct device *dev, const char *id,
  1318. struct device *alias_dev,
  1319. const char *alias_id)
  1320. {
  1321. struct regulator_supply_alias *map;
  1322. map = regulator_find_supply_alias(dev, id);
  1323. if (map)
  1324. return -EEXIST;
  1325. map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
  1326. if (!map)
  1327. return -ENOMEM;
  1328. map->src_dev = dev;
  1329. map->src_supply = id;
  1330. map->alias_dev = alias_dev;
  1331. map->alias_supply = alias_id;
  1332. list_add(&map->list, &regulator_supply_alias_list);
  1333. pr_info("Adding alias for supply %s,%s -> %s,%s\n",
  1334. id, dev_name(dev), alias_id, dev_name(alias_dev));
  1335. return 0;
  1336. }
  1337. EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
  1338. /**
  1339. * regulator_unregister_supply_alias - Remove device alias
  1340. *
  1341. * @dev: device that will be given as the regulator "consumer"
  1342. * @id: Supply name or regulator ID
  1343. *
  1344. * Remove a lookup alias if one exists for id on dev.
  1345. */
  1346. void regulator_unregister_supply_alias(struct device *dev, const char *id)
  1347. {
  1348. struct regulator_supply_alias *map;
  1349. map = regulator_find_supply_alias(dev, id);
  1350. if (map) {
  1351. list_del(&map->list);
  1352. kfree(map);
  1353. }
  1354. }
  1355. EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
  1356. /**
  1357. * regulator_bulk_register_supply_alias - register multiple aliases
  1358. *
  1359. * @dev: device that will be given as the regulator "consumer"
  1360. * @id: List of supply names or regulator IDs
  1361. * @alias_dev: device that should be used to lookup the supply
  1362. * @alias_id: List of supply names or regulator IDs that should be used to
  1363. * lookup the supply
  1364. * @num_id: Number of aliases to register
  1365. *
  1366. * @return 0 on success, an errno on failure.
  1367. *
  1368. * This helper function allows drivers to register several supply
  1369. * aliases in one operation. If any of the aliases cannot be
  1370. * registered any aliases that were registered will be removed
  1371. * before returning to the caller.
  1372. */
  1373. int regulator_bulk_register_supply_alias(struct device *dev,
  1374. const char *const *id,
  1375. struct device *alias_dev,
  1376. const char *const *alias_id,
  1377. int num_id)
  1378. {
  1379. int i;
  1380. int ret;
  1381. for (i = 0; i < num_id; ++i) {
  1382. ret = regulator_register_supply_alias(dev, id[i], alias_dev,
  1383. alias_id[i]);
  1384. if (ret < 0)
  1385. goto err;
  1386. }
  1387. return 0;
  1388. err:
  1389. dev_err(dev,
  1390. "Failed to create supply alias %s,%s -> %s,%s\n",
  1391. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  1392. while (--i >= 0)
  1393. regulator_unregister_supply_alias(dev, id[i]);
  1394. return ret;
  1395. }
  1396. EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
  1397. /**
  1398. * regulator_bulk_unregister_supply_alias - unregister multiple aliases
  1399. *
  1400. * @dev: device that will be given as the regulator "consumer"
  1401. * @id: List of supply names or regulator IDs
  1402. * @num_id: Number of aliases to unregister
  1403. *
  1404. * This helper function allows drivers to unregister several supply
  1405. * aliases in one operation.
  1406. */
  1407. void regulator_bulk_unregister_supply_alias(struct device *dev,
  1408. const char *const *id,
  1409. int num_id)
  1410. {
  1411. int i;
  1412. for (i = 0; i < num_id; ++i)
  1413. regulator_unregister_supply_alias(dev, id[i]);
  1414. }
  1415. EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
  1416. /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
  1417. static int regulator_ena_gpio_request(struct regulator_dev *rdev,
  1418. const struct regulator_config *config)
  1419. {
  1420. struct regulator_enable_gpio *pin;
  1421. struct gpio_desc *gpiod;
  1422. int ret;
  1423. gpiod = gpio_to_desc(config->ena_gpio);
  1424. list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
  1425. if (pin->gpiod == gpiod) {
  1426. rdev_dbg(rdev, "GPIO %d is already used\n",
  1427. config->ena_gpio);
  1428. goto update_ena_gpio_to_rdev;
  1429. }
  1430. }
  1431. ret = gpio_request_one(config->ena_gpio,
  1432. GPIOF_DIR_OUT | config->ena_gpio_flags,
  1433. rdev_get_name(rdev));
  1434. if (ret)
  1435. return ret;
  1436. pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
  1437. if (pin == NULL) {
  1438. gpio_free(config->ena_gpio);
  1439. return -ENOMEM;
  1440. }
  1441. pin->gpiod = gpiod;
  1442. pin->ena_gpio_invert = config->ena_gpio_invert;
  1443. list_add(&pin->list, &regulator_ena_gpio_list);
  1444. update_ena_gpio_to_rdev:
  1445. pin->request_count++;
  1446. rdev->ena_pin = pin;
  1447. return 0;
  1448. }
  1449. static void regulator_ena_gpio_free(struct regulator_dev *rdev)
  1450. {
  1451. struct regulator_enable_gpio *pin, *n;
  1452. if (!rdev->ena_pin)
  1453. return;
  1454. /* Free the GPIO only in case of no use */
  1455. list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
  1456. if (pin->gpiod == rdev->ena_pin->gpiod) {
  1457. if (pin->request_count <= 1) {
  1458. pin->request_count = 0;
  1459. gpiod_put(pin->gpiod);
  1460. list_del(&pin->list);
  1461. kfree(pin);
  1462. } else {
  1463. pin->request_count--;
  1464. }
  1465. }
  1466. }
  1467. }
  1468. /**
  1469. * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
  1470. * @rdev: regulator_dev structure
  1471. * @enable: enable GPIO at initial use?
  1472. *
  1473. * GPIO is enabled in case of initial use. (enable_count is 0)
  1474. * GPIO is disabled when it is not shared any more. (enable_count <= 1)
  1475. */
  1476. static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
  1477. {
  1478. struct regulator_enable_gpio *pin = rdev->ena_pin;
  1479. if (!pin)
  1480. return -EINVAL;
  1481. if (enable) {
  1482. /* Enable GPIO at initial use */
  1483. if (pin->enable_count == 0)
  1484. gpiod_set_value_cansleep(pin->gpiod,
  1485. !pin->ena_gpio_invert);
  1486. pin->enable_count++;
  1487. } else {
  1488. if (pin->enable_count > 1) {
  1489. pin->enable_count--;
  1490. return 0;
  1491. }
  1492. /* Disable GPIO if not used */
  1493. if (pin->enable_count <= 1) {
  1494. gpiod_set_value_cansleep(pin->gpiod,
  1495. pin->ena_gpio_invert);
  1496. pin->enable_count = 0;
  1497. }
  1498. }
  1499. return 0;
  1500. }
  1501. /**
  1502. * _regulator_enable_delay - a delay helper function
  1503. * @delay: time to delay in microseconds
  1504. *
  1505. * Delay for the requested amount of time as per the guidelines in:
  1506. *
  1507. * Documentation/timers/timers-howto.txt
  1508. *
  1509. * The assumption here is that regulators will never be enabled in
  1510. * atomic context and therefore sleeping functions can be used.
  1511. */
  1512. static void _regulator_enable_delay(unsigned int delay)
  1513. {
  1514. unsigned int ms = delay / 1000;
  1515. unsigned int us = delay % 1000;
  1516. if (ms > 0) {
  1517. /*
  1518. * For small enough values, handle super-millisecond
  1519. * delays in the usleep_range() call below.
  1520. */
  1521. if (ms < 20)
  1522. us += ms * 1000;
  1523. else
  1524. msleep(ms);
  1525. }
  1526. /*
  1527. * Give the scheduler some room to coalesce with any other
  1528. * wakeup sources. For delays shorter than 10 us, don't even
  1529. * bother setting up high-resolution timers and just busy-
  1530. * loop.
  1531. */
  1532. if (us >= 10)
  1533. usleep_range(us, us + 100);
  1534. else
  1535. udelay(us);
  1536. }
  1537. static int _regulator_do_enable(struct regulator_dev *rdev)
  1538. {
  1539. int ret, delay;
  1540. /* Query before enabling in case configuration dependent. */
  1541. ret = _regulator_get_enable_time(rdev);
  1542. if (ret >= 0) {
  1543. delay = ret;
  1544. } else {
  1545. rdev_warn(rdev, "enable_time() failed: %d\n", ret);
  1546. delay = 0;
  1547. }
  1548. trace_regulator_enable(rdev_get_name(rdev));
  1549. if (rdev->desc->off_on_delay) {
  1550. /* if needed, keep a distance of off_on_delay from last time
  1551. * this regulator was disabled.
  1552. */
  1553. unsigned long start_jiffy = jiffies;
  1554. unsigned long intended, max_delay, remaining;
  1555. max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
  1556. intended = rdev->last_off_jiffy + max_delay;
  1557. if (time_before(start_jiffy, intended)) {
  1558. /* calc remaining jiffies to deal with one-time
  1559. * timer wrapping.
  1560. * in case of multiple timer wrapping, either it can be
  1561. * detected by out-of-range remaining, or it cannot be
  1562. * detected and we gets a panelty of
  1563. * _regulator_enable_delay().
  1564. */
  1565. remaining = intended - start_jiffy;
  1566. if (remaining <= max_delay)
  1567. _regulator_enable_delay(
  1568. jiffies_to_usecs(remaining));
  1569. }
  1570. }
  1571. if (rdev->ena_pin) {
  1572. ret = regulator_ena_gpio_ctrl(rdev, true);
  1573. if (ret < 0)
  1574. return ret;
  1575. rdev->ena_gpio_state = 1;
  1576. } else if (rdev->desc->ops->enable) {
  1577. ret = rdev->desc->ops->enable(rdev);
  1578. if (ret < 0)
  1579. return ret;
  1580. } else {
  1581. return -EINVAL;
  1582. }
  1583. /* Allow the regulator to ramp; it would be useful to extend
  1584. * this for bulk operations so that the regulators can ramp
  1585. * together. */
  1586. trace_regulator_enable_delay(rdev_get_name(rdev));
  1587. _regulator_enable_delay(delay);
  1588. trace_regulator_enable_complete(rdev_get_name(rdev));
  1589. return 0;
  1590. }
  1591. /* locks held by regulator_enable() */
  1592. static int _regulator_enable(struct regulator_dev *rdev)
  1593. {
  1594. int ret;
  1595. /* check voltage and requested load before enabling */
  1596. if (rdev->constraints &&
  1597. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1598. drms_uA_update(rdev);
  1599. if (rdev->use_count == 0) {
  1600. /* The regulator may on if it's not switchable or left on */
  1601. ret = _regulator_is_enabled(rdev);
  1602. if (ret == -EINVAL || ret == 0) {
  1603. if (!_regulator_can_change_status(rdev))
  1604. return -EPERM;
  1605. ret = _regulator_do_enable(rdev);
  1606. if (ret < 0)
  1607. return ret;
  1608. } else if (ret < 0) {
  1609. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1610. return ret;
  1611. }
  1612. /* Fallthrough on positive return values - already enabled */
  1613. }
  1614. rdev->use_count++;
  1615. return 0;
  1616. }
  1617. /**
  1618. * regulator_enable - enable regulator output
  1619. * @regulator: regulator source
  1620. *
  1621. * Request that the regulator be enabled with the regulator output at
  1622. * the predefined voltage or current value. Calls to regulator_enable()
  1623. * must be balanced with calls to regulator_disable().
  1624. *
  1625. * NOTE: the output value can be set by other drivers, boot loader or may be
  1626. * hardwired in the regulator.
  1627. */
  1628. int regulator_enable(struct regulator *regulator)
  1629. {
  1630. struct regulator_dev *rdev = regulator->rdev;
  1631. int ret = 0;
  1632. if (regulator->always_on)
  1633. return 0;
  1634. if (rdev->supply) {
  1635. ret = regulator_enable(rdev->supply);
  1636. if (ret != 0)
  1637. return ret;
  1638. }
  1639. mutex_lock(&rdev->mutex);
  1640. ret = _regulator_enable(rdev);
  1641. mutex_unlock(&rdev->mutex);
  1642. if (ret != 0 && rdev->supply)
  1643. regulator_disable(rdev->supply);
  1644. return ret;
  1645. }
  1646. EXPORT_SYMBOL_GPL(regulator_enable);
  1647. static int _regulator_do_disable(struct regulator_dev *rdev)
  1648. {
  1649. int ret;
  1650. trace_regulator_disable(rdev_get_name(rdev));
  1651. if (rdev->ena_pin) {
  1652. ret = regulator_ena_gpio_ctrl(rdev, false);
  1653. if (ret < 0)
  1654. return ret;
  1655. rdev->ena_gpio_state = 0;
  1656. } else if (rdev->desc->ops->disable) {
  1657. ret = rdev->desc->ops->disable(rdev);
  1658. if (ret != 0)
  1659. return ret;
  1660. }
  1661. /* cares about last_off_jiffy only if off_on_delay is required by
  1662. * device.
  1663. */
  1664. if (rdev->desc->off_on_delay)
  1665. rdev->last_off_jiffy = jiffies;
  1666. trace_regulator_disable_complete(rdev_get_name(rdev));
  1667. return 0;
  1668. }
  1669. /* locks held by regulator_disable() */
  1670. static int _regulator_disable(struct regulator_dev *rdev)
  1671. {
  1672. int ret = 0;
  1673. if (WARN(rdev->use_count <= 0,
  1674. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1675. return -EIO;
  1676. /* are we the last user and permitted to disable ? */
  1677. if (rdev->use_count == 1 &&
  1678. (rdev->constraints && !rdev->constraints->always_on)) {
  1679. /* we are last user */
  1680. if (_regulator_can_change_status(rdev)) {
  1681. ret = _regulator_do_disable(rdev);
  1682. if (ret < 0) {
  1683. rdev_err(rdev, "failed to disable\n");
  1684. return ret;
  1685. }
  1686. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1687. NULL);
  1688. }
  1689. rdev->use_count = 0;
  1690. } else if (rdev->use_count > 1) {
  1691. if (rdev->constraints &&
  1692. (rdev->constraints->valid_ops_mask &
  1693. REGULATOR_CHANGE_DRMS))
  1694. drms_uA_update(rdev);
  1695. rdev->use_count--;
  1696. }
  1697. return ret;
  1698. }
  1699. /**
  1700. * regulator_disable - disable regulator output
  1701. * @regulator: regulator source
  1702. *
  1703. * Disable the regulator output voltage or current. Calls to
  1704. * regulator_enable() must be balanced with calls to
  1705. * regulator_disable().
  1706. *
  1707. * NOTE: this will only disable the regulator output if no other consumer
  1708. * devices have it enabled, the regulator device supports disabling and
  1709. * machine constraints permit this operation.
  1710. */
  1711. int regulator_disable(struct regulator *regulator)
  1712. {
  1713. struct regulator_dev *rdev = regulator->rdev;
  1714. int ret = 0;
  1715. if (regulator->always_on)
  1716. return 0;
  1717. mutex_lock(&rdev->mutex);
  1718. ret = _regulator_disable(rdev);
  1719. mutex_unlock(&rdev->mutex);
  1720. if (ret == 0 && rdev->supply)
  1721. regulator_disable(rdev->supply);
  1722. return ret;
  1723. }
  1724. EXPORT_SYMBOL_GPL(regulator_disable);
  1725. /* locks held by regulator_force_disable() */
  1726. static int _regulator_force_disable(struct regulator_dev *rdev)
  1727. {
  1728. int ret = 0;
  1729. ret = _regulator_do_disable(rdev);
  1730. if (ret < 0) {
  1731. rdev_err(rdev, "failed to force disable\n");
  1732. return ret;
  1733. }
  1734. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1735. REGULATOR_EVENT_DISABLE, NULL);
  1736. return 0;
  1737. }
  1738. /**
  1739. * regulator_force_disable - force disable regulator output
  1740. * @regulator: regulator source
  1741. *
  1742. * Forcibly disable the regulator output voltage or current.
  1743. * NOTE: this *will* disable the regulator output even if other consumer
  1744. * devices have it enabled. This should be used for situations when device
  1745. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1746. */
  1747. int regulator_force_disable(struct regulator *regulator)
  1748. {
  1749. struct regulator_dev *rdev = regulator->rdev;
  1750. int ret;
  1751. mutex_lock(&rdev->mutex);
  1752. regulator->uA_load = 0;
  1753. ret = _regulator_force_disable(regulator->rdev);
  1754. mutex_unlock(&rdev->mutex);
  1755. if (rdev->supply)
  1756. while (rdev->open_count--)
  1757. regulator_disable(rdev->supply);
  1758. return ret;
  1759. }
  1760. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1761. static void regulator_disable_work(struct work_struct *work)
  1762. {
  1763. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1764. disable_work.work);
  1765. int count, i, ret;
  1766. mutex_lock(&rdev->mutex);
  1767. BUG_ON(!rdev->deferred_disables);
  1768. count = rdev->deferred_disables;
  1769. rdev->deferred_disables = 0;
  1770. for (i = 0; i < count; i++) {
  1771. ret = _regulator_disable(rdev);
  1772. if (ret != 0)
  1773. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1774. }
  1775. mutex_unlock(&rdev->mutex);
  1776. if (rdev->supply) {
  1777. for (i = 0; i < count; i++) {
  1778. ret = regulator_disable(rdev->supply);
  1779. if (ret != 0) {
  1780. rdev_err(rdev,
  1781. "Supply disable failed: %d\n", ret);
  1782. }
  1783. }
  1784. }
  1785. }
  1786. /**
  1787. * regulator_disable_deferred - disable regulator output with delay
  1788. * @regulator: regulator source
  1789. * @ms: miliseconds until the regulator is disabled
  1790. *
  1791. * Execute regulator_disable() on the regulator after a delay. This
  1792. * is intended for use with devices that require some time to quiesce.
  1793. *
  1794. * NOTE: this will only disable the regulator output if no other consumer
  1795. * devices have it enabled, the regulator device supports disabling and
  1796. * machine constraints permit this operation.
  1797. */
  1798. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1799. {
  1800. struct regulator_dev *rdev = regulator->rdev;
  1801. int ret;
  1802. if (regulator->always_on)
  1803. return 0;
  1804. if (!ms)
  1805. return regulator_disable(regulator);
  1806. mutex_lock(&rdev->mutex);
  1807. rdev->deferred_disables++;
  1808. mutex_unlock(&rdev->mutex);
  1809. ret = queue_delayed_work(system_power_efficient_wq,
  1810. &rdev->disable_work,
  1811. msecs_to_jiffies(ms));
  1812. if (ret < 0)
  1813. return ret;
  1814. else
  1815. return 0;
  1816. }
  1817. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1818. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1819. {
  1820. /* A GPIO control always takes precedence */
  1821. if (rdev->ena_pin)
  1822. return rdev->ena_gpio_state;
  1823. /* If we don't know then assume that the regulator is always on */
  1824. if (!rdev->desc->ops->is_enabled)
  1825. return 1;
  1826. return rdev->desc->ops->is_enabled(rdev);
  1827. }
  1828. /**
  1829. * regulator_is_enabled - is the regulator output enabled
  1830. * @regulator: regulator source
  1831. *
  1832. * Returns positive if the regulator driver backing the source/client
  1833. * has requested that the device be enabled, zero if it hasn't, else a
  1834. * negative errno code.
  1835. *
  1836. * Note that the device backing this regulator handle can have multiple
  1837. * users, so it might be enabled even if regulator_enable() was never
  1838. * called for this particular source.
  1839. */
  1840. int regulator_is_enabled(struct regulator *regulator)
  1841. {
  1842. int ret;
  1843. if (regulator->always_on)
  1844. return 1;
  1845. mutex_lock(&regulator->rdev->mutex);
  1846. ret = _regulator_is_enabled(regulator->rdev);
  1847. mutex_unlock(&regulator->rdev->mutex);
  1848. return ret;
  1849. }
  1850. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1851. /**
  1852. * regulator_can_change_voltage - check if regulator can change voltage
  1853. * @regulator: regulator source
  1854. *
  1855. * Returns positive if the regulator driver backing the source/client
  1856. * can change its voltage, false otherwise. Useful for detecting fixed
  1857. * or dummy regulators and disabling voltage change logic in the client
  1858. * driver.
  1859. */
  1860. int regulator_can_change_voltage(struct regulator *regulator)
  1861. {
  1862. struct regulator_dev *rdev = regulator->rdev;
  1863. if (rdev->constraints &&
  1864. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  1865. if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
  1866. return 1;
  1867. if (rdev->desc->continuous_voltage_range &&
  1868. rdev->constraints->min_uV && rdev->constraints->max_uV &&
  1869. rdev->constraints->min_uV != rdev->constraints->max_uV)
  1870. return 1;
  1871. }
  1872. return 0;
  1873. }
  1874. EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
  1875. /**
  1876. * regulator_count_voltages - count regulator_list_voltage() selectors
  1877. * @regulator: regulator source
  1878. *
  1879. * Returns number of selectors, or negative errno. Selectors are
  1880. * numbered starting at zero, and typically correspond to bitfields
  1881. * in hardware registers.
  1882. */
  1883. int regulator_count_voltages(struct regulator *regulator)
  1884. {
  1885. struct regulator_dev *rdev = regulator->rdev;
  1886. if (rdev->desc->n_voltages)
  1887. return rdev->desc->n_voltages;
  1888. if (!rdev->supply)
  1889. return -EINVAL;
  1890. return regulator_count_voltages(rdev->supply);
  1891. }
  1892. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1893. /**
  1894. * regulator_list_voltage - enumerate supported voltages
  1895. * @regulator: regulator source
  1896. * @selector: identify voltage to list
  1897. * Context: can sleep
  1898. *
  1899. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1900. * zero if this selector code can't be used on this system, or a
  1901. * negative errno.
  1902. */
  1903. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1904. {
  1905. struct regulator_dev *rdev = regulator->rdev;
  1906. const struct regulator_ops *ops = rdev->desc->ops;
  1907. int ret;
  1908. if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
  1909. return rdev->desc->fixed_uV;
  1910. if (ops->list_voltage) {
  1911. if (selector >= rdev->desc->n_voltages)
  1912. return -EINVAL;
  1913. mutex_lock(&rdev->mutex);
  1914. ret = ops->list_voltage(rdev, selector);
  1915. mutex_unlock(&rdev->mutex);
  1916. } else if (rdev->supply) {
  1917. ret = regulator_list_voltage(rdev->supply, selector);
  1918. } else {
  1919. return -EINVAL;
  1920. }
  1921. if (ret > 0) {
  1922. if (ret < rdev->constraints->min_uV)
  1923. ret = 0;
  1924. else if (ret > rdev->constraints->max_uV)
  1925. ret = 0;
  1926. }
  1927. return ret;
  1928. }
  1929. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1930. /**
  1931. * regulator_get_regmap - get the regulator's register map
  1932. * @regulator: regulator source
  1933. *
  1934. * Returns the register map for the given regulator, or an ERR_PTR value
  1935. * if the regulator doesn't use regmap.
  1936. */
  1937. struct regmap *regulator_get_regmap(struct regulator *regulator)
  1938. {
  1939. struct regmap *map = regulator->rdev->regmap;
  1940. return map ? map : ERR_PTR(-EOPNOTSUPP);
  1941. }
  1942. /**
  1943. * regulator_get_hardware_vsel_register - get the HW voltage selector register
  1944. * @regulator: regulator source
  1945. * @vsel_reg: voltage selector register, output parameter
  1946. * @vsel_mask: mask for voltage selector bitfield, output parameter
  1947. *
  1948. * Returns the hardware register offset and bitmask used for setting the
  1949. * regulator voltage. This might be useful when configuring voltage-scaling
  1950. * hardware or firmware that can make I2C requests behind the kernel's back,
  1951. * for example.
  1952. *
  1953. * On success, the output parameters @vsel_reg and @vsel_mask are filled in
  1954. * and 0 is returned, otherwise a negative errno is returned.
  1955. */
  1956. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  1957. unsigned *vsel_reg,
  1958. unsigned *vsel_mask)
  1959. {
  1960. struct regulator_dev *rdev = regulator->rdev;
  1961. const struct regulator_ops *ops = rdev->desc->ops;
  1962. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  1963. return -EOPNOTSUPP;
  1964. *vsel_reg = rdev->desc->vsel_reg;
  1965. *vsel_mask = rdev->desc->vsel_mask;
  1966. return 0;
  1967. }
  1968. EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
  1969. /**
  1970. * regulator_list_hardware_vsel - get the HW-specific register value for a selector
  1971. * @regulator: regulator source
  1972. * @selector: identify voltage to list
  1973. *
  1974. * Converts the selector to a hardware-specific voltage selector that can be
  1975. * directly written to the regulator registers. The address of the voltage
  1976. * register can be determined by calling @regulator_get_hardware_vsel_register.
  1977. *
  1978. * On error a negative errno is returned.
  1979. */
  1980. int regulator_list_hardware_vsel(struct regulator *regulator,
  1981. unsigned selector)
  1982. {
  1983. struct regulator_dev *rdev = regulator->rdev;
  1984. const struct regulator_ops *ops = rdev->desc->ops;
  1985. if (selector >= rdev->desc->n_voltages)
  1986. return -EINVAL;
  1987. if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
  1988. return -EOPNOTSUPP;
  1989. return selector;
  1990. }
  1991. EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
  1992. /**
  1993. * regulator_get_linear_step - return the voltage step size between VSEL values
  1994. * @regulator: regulator source
  1995. *
  1996. * Returns the voltage step size between VSEL values for linear
  1997. * regulators, or return 0 if the regulator isn't a linear regulator.
  1998. */
  1999. unsigned int regulator_get_linear_step(struct regulator *regulator)
  2000. {
  2001. struct regulator_dev *rdev = regulator->rdev;
  2002. return rdev->desc->uV_step;
  2003. }
  2004. EXPORT_SYMBOL_GPL(regulator_get_linear_step);
  2005. /**
  2006. * regulator_is_supported_voltage - check if a voltage range can be supported
  2007. *
  2008. * @regulator: Regulator to check.
  2009. * @min_uV: Minimum required voltage in uV.
  2010. * @max_uV: Maximum required voltage in uV.
  2011. *
  2012. * Returns a boolean or a negative error code.
  2013. */
  2014. int regulator_is_supported_voltage(struct regulator *regulator,
  2015. int min_uV, int max_uV)
  2016. {
  2017. struct regulator_dev *rdev = regulator->rdev;
  2018. int i, voltages, ret;
  2019. /* If we can't change voltage check the current voltage */
  2020. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  2021. ret = regulator_get_voltage(regulator);
  2022. if (ret >= 0)
  2023. return min_uV <= ret && ret <= max_uV;
  2024. else
  2025. return ret;
  2026. }
  2027. /* Any voltage within constrains range is fine? */
  2028. if (rdev->desc->continuous_voltage_range)
  2029. return min_uV >= rdev->constraints->min_uV &&
  2030. max_uV <= rdev->constraints->max_uV;
  2031. ret = regulator_count_voltages(regulator);
  2032. if (ret < 0)
  2033. return ret;
  2034. voltages = ret;
  2035. for (i = 0; i < voltages; i++) {
  2036. ret = regulator_list_voltage(regulator, i);
  2037. if (ret >= min_uV && ret <= max_uV)
  2038. return 1;
  2039. }
  2040. return 0;
  2041. }
  2042. EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
  2043. static int _regulator_call_set_voltage(struct regulator_dev *rdev,
  2044. int min_uV, int max_uV,
  2045. unsigned *selector)
  2046. {
  2047. struct pre_voltage_change_data data;
  2048. int ret;
  2049. data.old_uV = _regulator_get_voltage(rdev);
  2050. data.min_uV = min_uV;
  2051. data.max_uV = max_uV;
  2052. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2053. &data);
  2054. if (ret & NOTIFY_STOP_MASK)
  2055. return -EINVAL;
  2056. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
  2057. if (ret >= 0)
  2058. return ret;
  2059. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2060. (void *)data.old_uV);
  2061. return ret;
  2062. }
  2063. static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
  2064. int uV, unsigned selector)
  2065. {
  2066. struct pre_voltage_change_data data;
  2067. int ret;
  2068. data.old_uV = _regulator_get_voltage(rdev);
  2069. data.min_uV = uV;
  2070. data.max_uV = uV;
  2071. ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
  2072. &data);
  2073. if (ret & NOTIFY_STOP_MASK)
  2074. return -EINVAL;
  2075. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  2076. if (ret >= 0)
  2077. return ret;
  2078. _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
  2079. (void *)data.old_uV);
  2080. return ret;
  2081. }
  2082. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  2083. int min_uV, int max_uV)
  2084. {
  2085. int ret;
  2086. int delay = 0;
  2087. int best_val = 0;
  2088. unsigned int selector;
  2089. int old_selector = -1;
  2090. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  2091. min_uV += rdev->constraints->uV_offset;
  2092. max_uV += rdev->constraints->uV_offset;
  2093. /*
  2094. * If we can't obtain the old selector there is not enough
  2095. * info to call set_voltage_time_sel().
  2096. */
  2097. if (_regulator_is_enabled(rdev) &&
  2098. rdev->desc->ops->set_voltage_time_sel &&
  2099. rdev->desc->ops->get_voltage_sel) {
  2100. old_selector = rdev->desc->ops->get_voltage_sel(rdev);
  2101. if (old_selector < 0)
  2102. return old_selector;
  2103. }
  2104. if (rdev->desc->ops->set_voltage) {
  2105. ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
  2106. &selector);
  2107. if (ret >= 0) {
  2108. if (rdev->desc->ops->list_voltage)
  2109. best_val = rdev->desc->ops->list_voltage(rdev,
  2110. selector);
  2111. else
  2112. best_val = _regulator_get_voltage(rdev);
  2113. }
  2114. } else if (rdev->desc->ops->set_voltage_sel) {
  2115. if (rdev->desc->ops->map_voltage) {
  2116. ret = rdev->desc->ops->map_voltage(rdev, min_uV,
  2117. max_uV);
  2118. } else {
  2119. if (rdev->desc->ops->list_voltage ==
  2120. regulator_list_voltage_linear)
  2121. ret = regulator_map_voltage_linear(rdev,
  2122. min_uV, max_uV);
  2123. else if (rdev->desc->ops->list_voltage ==
  2124. regulator_list_voltage_linear_range)
  2125. ret = regulator_map_voltage_linear_range(rdev,
  2126. min_uV, max_uV);
  2127. else
  2128. ret = regulator_map_voltage_iterate(rdev,
  2129. min_uV, max_uV);
  2130. }
  2131. if (ret >= 0) {
  2132. best_val = rdev->desc->ops->list_voltage(rdev, ret);
  2133. if (min_uV <= best_val && max_uV >= best_val) {
  2134. selector = ret;
  2135. if (old_selector == selector)
  2136. ret = 0;
  2137. else
  2138. ret = _regulator_call_set_voltage_sel(
  2139. rdev, best_val, selector);
  2140. } else {
  2141. ret = -EINVAL;
  2142. }
  2143. }
  2144. } else {
  2145. ret = -EINVAL;
  2146. }
  2147. /* Call set_voltage_time_sel if successfully obtained old_selector */
  2148. if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
  2149. && old_selector != selector) {
  2150. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  2151. old_selector, selector);
  2152. if (delay < 0) {
  2153. rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
  2154. delay);
  2155. delay = 0;
  2156. }
  2157. /* Insert any necessary delays */
  2158. if (delay >= 1000) {
  2159. mdelay(delay / 1000);
  2160. udelay(delay % 1000);
  2161. } else if (delay) {
  2162. udelay(delay);
  2163. }
  2164. }
  2165. if (ret == 0 && best_val >= 0) {
  2166. unsigned long data = best_val;
  2167. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  2168. (void *)data);
  2169. }
  2170. trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
  2171. return ret;
  2172. }
  2173. /**
  2174. * regulator_set_voltage - set regulator output voltage
  2175. * @regulator: regulator source
  2176. * @min_uV: Minimum required voltage in uV
  2177. * @max_uV: Maximum acceptable voltage in uV
  2178. *
  2179. * Sets a voltage regulator to the desired output voltage. This can be set
  2180. * during any regulator state. IOW, regulator can be disabled or enabled.
  2181. *
  2182. * If the regulator is enabled then the voltage will change to the new value
  2183. * immediately otherwise if the regulator is disabled the regulator will
  2184. * output at the new voltage when enabled.
  2185. *
  2186. * NOTE: If the regulator is shared between several devices then the lowest
  2187. * request voltage that meets the system constraints will be used.
  2188. * Regulator system constraints must be set for this regulator before
  2189. * calling this function otherwise this call will fail.
  2190. */
  2191. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  2192. {
  2193. struct regulator_dev *rdev = regulator->rdev;
  2194. int ret = 0;
  2195. int old_min_uV, old_max_uV;
  2196. int current_uV;
  2197. mutex_lock(&rdev->mutex);
  2198. /* If we're setting the same range as last time the change
  2199. * should be a noop (some cpufreq implementations use the same
  2200. * voltage for multiple frequencies, for example).
  2201. */
  2202. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  2203. goto out;
  2204. /* If we're trying to set a range that overlaps the current voltage,
  2205. * return succesfully even though the regulator does not support
  2206. * changing the voltage.
  2207. */
  2208. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  2209. current_uV = _regulator_get_voltage(rdev);
  2210. if (min_uV <= current_uV && current_uV <= max_uV) {
  2211. regulator->min_uV = min_uV;
  2212. regulator->max_uV = max_uV;
  2213. goto out;
  2214. }
  2215. }
  2216. /* sanity check */
  2217. if (!rdev->desc->ops->set_voltage &&
  2218. !rdev->desc->ops->set_voltage_sel) {
  2219. ret = -EINVAL;
  2220. goto out;
  2221. }
  2222. /* constraints check */
  2223. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2224. if (ret < 0)
  2225. goto out;
  2226. /* restore original values in case of error */
  2227. old_min_uV = regulator->min_uV;
  2228. old_max_uV = regulator->max_uV;
  2229. regulator->min_uV = min_uV;
  2230. regulator->max_uV = max_uV;
  2231. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2232. if (ret < 0)
  2233. goto out2;
  2234. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2235. if (ret < 0)
  2236. goto out2;
  2237. out:
  2238. mutex_unlock(&rdev->mutex);
  2239. return ret;
  2240. out2:
  2241. regulator->min_uV = old_min_uV;
  2242. regulator->max_uV = old_max_uV;
  2243. mutex_unlock(&rdev->mutex);
  2244. return ret;
  2245. }
  2246. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  2247. /**
  2248. * regulator_set_voltage_time - get raise/fall time
  2249. * @regulator: regulator source
  2250. * @old_uV: starting voltage in microvolts
  2251. * @new_uV: target voltage in microvolts
  2252. *
  2253. * Provided with the starting and ending voltage, this function attempts to
  2254. * calculate the time in microseconds required to rise or fall to this new
  2255. * voltage.
  2256. */
  2257. int regulator_set_voltage_time(struct regulator *regulator,
  2258. int old_uV, int new_uV)
  2259. {
  2260. struct regulator_dev *rdev = regulator->rdev;
  2261. const struct regulator_ops *ops = rdev->desc->ops;
  2262. int old_sel = -1;
  2263. int new_sel = -1;
  2264. int voltage;
  2265. int i;
  2266. /* Currently requires operations to do this */
  2267. if (!ops->list_voltage || !ops->set_voltage_time_sel
  2268. || !rdev->desc->n_voltages)
  2269. return -EINVAL;
  2270. for (i = 0; i < rdev->desc->n_voltages; i++) {
  2271. /* We only look for exact voltage matches here */
  2272. voltage = regulator_list_voltage(regulator, i);
  2273. if (voltage < 0)
  2274. return -EINVAL;
  2275. if (voltage == 0)
  2276. continue;
  2277. if (voltage == old_uV)
  2278. old_sel = i;
  2279. if (voltage == new_uV)
  2280. new_sel = i;
  2281. }
  2282. if (old_sel < 0 || new_sel < 0)
  2283. return -EINVAL;
  2284. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  2285. }
  2286. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  2287. /**
  2288. * regulator_set_voltage_time_sel - get raise/fall time
  2289. * @rdev: regulator source device
  2290. * @old_selector: selector for starting voltage
  2291. * @new_selector: selector for target voltage
  2292. *
  2293. * Provided with the starting and target voltage selectors, this function
  2294. * returns time in microseconds required to rise or fall to this new voltage
  2295. *
  2296. * Drivers providing ramp_delay in regulation_constraints can use this as their
  2297. * set_voltage_time_sel() operation.
  2298. */
  2299. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  2300. unsigned int old_selector,
  2301. unsigned int new_selector)
  2302. {
  2303. unsigned int ramp_delay = 0;
  2304. int old_volt, new_volt;
  2305. if (rdev->constraints->ramp_delay)
  2306. ramp_delay = rdev->constraints->ramp_delay;
  2307. else if (rdev->desc->ramp_delay)
  2308. ramp_delay = rdev->desc->ramp_delay;
  2309. if (ramp_delay == 0) {
  2310. rdev_warn(rdev, "ramp_delay not set\n");
  2311. return 0;
  2312. }
  2313. /* sanity check */
  2314. if (!rdev->desc->ops->list_voltage)
  2315. return -EINVAL;
  2316. old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
  2317. new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
  2318. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  2319. }
  2320. EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
  2321. /**
  2322. * regulator_sync_voltage - re-apply last regulator output voltage
  2323. * @regulator: regulator source
  2324. *
  2325. * Re-apply the last configured voltage. This is intended to be used
  2326. * where some external control source the consumer is cooperating with
  2327. * has caused the configured voltage to change.
  2328. */
  2329. int regulator_sync_voltage(struct regulator *regulator)
  2330. {
  2331. struct regulator_dev *rdev = regulator->rdev;
  2332. int ret, min_uV, max_uV;
  2333. mutex_lock(&rdev->mutex);
  2334. if (!rdev->desc->ops->set_voltage &&
  2335. !rdev->desc->ops->set_voltage_sel) {
  2336. ret = -EINVAL;
  2337. goto out;
  2338. }
  2339. /* This is only going to work if we've had a voltage configured. */
  2340. if (!regulator->min_uV && !regulator->max_uV) {
  2341. ret = -EINVAL;
  2342. goto out;
  2343. }
  2344. min_uV = regulator->min_uV;
  2345. max_uV = regulator->max_uV;
  2346. /* This should be a paranoia check... */
  2347. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  2348. if (ret < 0)
  2349. goto out;
  2350. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  2351. if (ret < 0)
  2352. goto out;
  2353. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  2354. out:
  2355. mutex_unlock(&rdev->mutex);
  2356. return ret;
  2357. }
  2358. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  2359. static int _regulator_get_voltage(struct regulator_dev *rdev)
  2360. {
  2361. int sel, ret;
  2362. if (rdev->desc->ops->get_voltage_sel) {
  2363. sel = rdev->desc->ops->get_voltage_sel(rdev);
  2364. if (sel < 0)
  2365. return sel;
  2366. ret = rdev->desc->ops->list_voltage(rdev, sel);
  2367. } else if (rdev->desc->ops->get_voltage) {
  2368. ret = rdev->desc->ops->get_voltage(rdev);
  2369. } else if (rdev->desc->ops->list_voltage) {
  2370. ret = rdev->desc->ops->list_voltage(rdev, 0);
  2371. } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
  2372. ret = rdev->desc->fixed_uV;
  2373. } else if (rdev->supply) {
  2374. ret = regulator_get_voltage(rdev->supply);
  2375. } else {
  2376. return -EINVAL;
  2377. }
  2378. if (ret < 0)
  2379. return ret;
  2380. return ret - rdev->constraints->uV_offset;
  2381. }
  2382. /**
  2383. * regulator_get_voltage - get regulator output voltage
  2384. * @regulator: regulator source
  2385. *
  2386. * This returns the current regulator voltage in uV.
  2387. *
  2388. * NOTE: If the regulator is disabled it will return the voltage value. This
  2389. * function should not be used to determine regulator state.
  2390. */
  2391. int regulator_get_voltage(struct regulator *regulator)
  2392. {
  2393. int ret;
  2394. mutex_lock(&regulator->rdev->mutex);
  2395. ret = _regulator_get_voltage(regulator->rdev);
  2396. mutex_unlock(&regulator->rdev->mutex);
  2397. return ret;
  2398. }
  2399. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  2400. /**
  2401. * regulator_set_current_limit - set regulator output current limit
  2402. * @regulator: regulator source
  2403. * @min_uA: Minimum supported current in uA
  2404. * @max_uA: Maximum supported current in uA
  2405. *
  2406. * Sets current sink to the desired output current. This can be set during
  2407. * any regulator state. IOW, regulator can be disabled or enabled.
  2408. *
  2409. * If the regulator is enabled then the current will change to the new value
  2410. * immediately otherwise if the regulator is disabled the regulator will
  2411. * output at the new current when enabled.
  2412. *
  2413. * NOTE: Regulator system constraints must be set for this regulator before
  2414. * calling this function otherwise this call will fail.
  2415. */
  2416. int regulator_set_current_limit(struct regulator *regulator,
  2417. int min_uA, int max_uA)
  2418. {
  2419. struct regulator_dev *rdev = regulator->rdev;
  2420. int ret;
  2421. mutex_lock(&rdev->mutex);
  2422. /* sanity check */
  2423. if (!rdev->desc->ops->set_current_limit) {
  2424. ret = -EINVAL;
  2425. goto out;
  2426. }
  2427. /* constraints check */
  2428. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  2429. if (ret < 0)
  2430. goto out;
  2431. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  2432. out:
  2433. mutex_unlock(&rdev->mutex);
  2434. return ret;
  2435. }
  2436. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  2437. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  2438. {
  2439. int ret;
  2440. mutex_lock(&rdev->mutex);
  2441. /* sanity check */
  2442. if (!rdev->desc->ops->get_current_limit) {
  2443. ret = -EINVAL;
  2444. goto out;
  2445. }
  2446. ret = rdev->desc->ops->get_current_limit(rdev);
  2447. out:
  2448. mutex_unlock(&rdev->mutex);
  2449. return ret;
  2450. }
  2451. /**
  2452. * regulator_get_current_limit - get regulator output current
  2453. * @regulator: regulator source
  2454. *
  2455. * This returns the current supplied by the specified current sink in uA.
  2456. *
  2457. * NOTE: If the regulator is disabled it will return the current value. This
  2458. * function should not be used to determine regulator state.
  2459. */
  2460. int regulator_get_current_limit(struct regulator *regulator)
  2461. {
  2462. return _regulator_get_current_limit(regulator->rdev);
  2463. }
  2464. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  2465. /**
  2466. * regulator_set_mode - set regulator operating mode
  2467. * @regulator: regulator source
  2468. * @mode: operating mode - one of the REGULATOR_MODE constants
  2469. *
  2470. * Set regulator operating mode to increase regulator efficiency or improve
  2471. * regulation performance.
  2472. *
  2473. * NOTE: Regulator system constraints must be set for this regulator before
  2474. * calling this function otherwise this call will fail.
  2475. */
  2476. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  2477. {
  2478. struct regulator_dev *rdev = regulator->rdev;
  2479. int ret;
  2480. int regulator_curr_mode;
  2481. mutex_lock(&rdev->mutex);
  2482. /* sanity check */
  2483. if (!rdev->desc->ops->set_mode) {
  2484. ret = -EINVAL;
  2485. goto out;
  2486. }
  2487. /* return if the same mode is requested */
  2488. if (rdev->desc->ops->get_mode) {
  2489. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  2490. if (regulator_curr_mode == mode) {
  2491. ret = 0;
  2492. goto out;
  2493. }
  2494. }
  2495. /* constraints check */
  2496. ret = regulator_mode_constrain(rdev, &mode);
  2497. if (ret < 0)
  2498. goto out;
  2499. ret = rdev->desc->ops->set_mode(rdev, mode);
  2500. out:
  2501. mutex_unlock(&rdev->mutex);
  2502. return ret;
  2503. }
  2504. EXPORT_SYMBOL_GPL(regulator_set_mode);
  2505. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  2506. {
  2507. int ret;
  2508. mutex_lock(&rdev->mutex);
  2509. /* sanity check */
  2510. if (!rdev->desc->ops->get_mode) {
  2511. ret = -EINVAL;
  2512. goto out;
  2513. }
  2514. ret = rdev->desc->ops->get_mode(rdev);
  2515. out:
  2516. mutex_unlock(&rdev->mutex);
  2517. return ret;
  2518. }
  2519. /**
  2520. * regulator_get_mode - get regulator operating mode
  2521. * @regulator: regulator source
  2522. *
  2523. * Get the current regulator operating mode.
  2524. */
  2525. unsigned int regulator_get_mode(struct regulator *regulator)
  2526. {
  2527. return _regulator_get_mode(regulator->rdev);
  2528. }
  2529. EXPORT_SYMBOL_GPL(regulator_get_mode);
  2530. /**
  2531. * regulator_set_optimum_mode - set regulator optimum operating mode
  2532. * @regulator: regulator source
  2533. * @uA_load: load current
  2534. *
  2535. * Notifies the regulator core of a new device load. This is then used by
  2536. * DRMS (if enabled by constraints) to set the most efficient regulator
  2537. * operating mode for the new regulator loading.
  2538. *
  2539. * Consumer devices notify their supply regulator of the maximum power
  2540. * they will require (can be taken from device datasheet in the power
  2541. * consumption tables) when they change operational status and hence power
  2542. * state. Examples of operational state changes that can affect power
  2543. * consumption are :-
  2544. *
  2545. * o Device is opened / closed.
  2546. * o Device I/O is about to begin or has just finished.
  2547. * o Device is idling in between work.
  2548. *
  2549. * This information is also exported via sysfs to userspace.
  2550. *
  2551. * DRMS will sum the total requested load on the regulator and change
  2552. * to the most efficient operating mode if platform constraints allow.
  2553. *
  2554. * Returns the new regulator mode or error.
  2555. */
  2556. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  2557. {
  2558. struct regulator_dev *rdev = regulator->rdev;
  2559. struct regulator *consumer;
  2560. int ret, output_uV, input_uV = 0, total_uA_load = 0;
  2561. unsigned int mode;
  2562. if (rdev->supply)
  2563. input_uV = regulator_get_voltage(rdev->supply);
  2564. mutex_lock(&rdev->mutex);
  2565. /*
  2566. * first check to see if we can set modes at all, otherwise just
  2567. * tell the consumer everything is OK.
  2568. */
  2569. regulator->uA_load = uA_load;
  2570. ret = regulator_check_drms(rdev);
  2571. if (ret < 0) {
  2572. ret = 0;
  2573. goto out;
  2574. }
  2575. if (!rdev->desc->ops->get_optimum_mode)
  2576. goto out;
  2577. /*
  2578. * we can actually do this so any errors are indicators of
  2579. * potential real failure.
  2580. */
  2581. ret = -EINVAL;
  2582. if (!rdev->desc->ops->set_mode)
  2583. goto out;
  2584. /* get output voltage */
  2585. output_uV = _regulator_get_voltage(rdev);
  2586. if (output_uV <= 0) {
  2587. rdev_err(rdev, "invalid output voltage found\n");
  2588. goto out;
  2589. }
  2590. /* No supply? Use constraint voltage */
  2591. if (input_uV <= 0)
  2592. input_uV = rdev->constraints->input_uV;
  2593. if (input_uV <= 0) {
  2594. rdev_err(rdev, "invalid input voltage found\n");
  2595. goto out;
  2596. }
  2597. /* calc total requested load for this regulator */
  2598. list_for_each_entry(consumer, &rdev->consumer_list, list)
  2599. total_uA_load += consumer->uA_load;
  2600. mode = rdev->desc->ops->get_optimum_mode(rdev,
  2601. input_uV, output_uV,
  2602. total_uA_load);
  2603. ret = regulator_mode_constrain(rdev, &mode);
  2604. if (ret < 0) {
  2605. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  2606. total_uA_load, input_uV, output_uV);
  2607. goto out;
  2608. }
  2609. ret = rdev->desc->ops->set_mode(rdev, mode);
  2610. if (ret < 0) {
  2611. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  2612. goto out;
  2613. }
  2614. ret = mode;
  2615. out:
  2616. mutex_unlock(&rdev->mutex);
  2617. return ret;
  2618. }
  2619. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  2620. /**
  2621. * regulator_allow_bypass - allow the regulator to go into bypass mode
  2622. *
  2623. * @regulator: Regulator to configure
  2624. * @enable: enable or disable bypass mode
  2625. *
  2626. * Allow the regulator to go into bypass mode if all other consumers
  2627. * for the regulator also enable bypass mode and the machine
  2628. * constraints allow this. Bypass mode means that the regulator is
  2629. * simply passing the input directly to the output with no regulation.
  2630. */
  2631. int regulator_allow_bypass(struct regulator *regulator, bool enable)
  2632. {
  2633. struct regulator_dev *rdev = regulator->rdev;
  2634. int ret = 0;
  2635. if (!rdev->desc->ops->set_bypass)
  2636. return 0;
  2637. if (rdev->constraints &&
  2638. !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
  2639. return 0;
  2640. mutex_lock(&rdev->mutex);
  2641. if (enable && !regulator->bypass) {
  2642. rdev->bypass_count++;
  2643. if (rdev->bypass_count == rdev->open_count) {
  2644. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2645. if (ret != 0)
  2646. rdev->bypass_count--;
  2647. }
  2648. } else if (!enable && regulator->bypass) {
  2649. rdev->bypass_count--;
  2650. if (rdev->bypass_count != rdev->open_count) {
  2651. ret = rdev->desc->ops->set_bypass(rdev, enable);
  2652. if (ret != 0)
  2653. rdev->bypass_count++;
  2654. }
  2655. }
  2656. if (ret == 0)
  2657. regulator->bypass = enable;
  2658. mutex_unlock(&rdev->mutex);
  2659. return ret;
  2660. }
  2661. EXPORT_SYMBOL_GPL(regulator_allow_bypass);
  2662. /**
  2663. * regulator_register_notifier - register regulator event notifier
  2664. * @regulator: regulator source
  2665. * @nb: notifier block
  2666. *
  2667. * Register notifier block to receive regulator events.
  2668. */
  2669. int regulator_register_notifier(struct regulator *regulator,
  2670. struct notifier_block *nb)
  2671. {
  2672. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  2673. nb);
  2674. }
  2675. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  2676. /**
  2677. * regulator_unregister_notifier - unregister regulator event notifier
  2678. * @regulator: regulator source
  2679. * @nb: notifier block
  2680. *
  2681. * Unregister regulator event notifier block.
  2682. */
  2683. int regulator_unregister_notifier(struct regulator *regulator,
  2684. struct notifier_block *nb)
  2685. {
  2686. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  2687. nb);
  2688. }
  2689. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  2690. /* notify regulator consumers and downstream regulator consumers.
  2691. * Note mutex must be held by caller.
  2692. */
  2693. static int _notifier_call_chain(struct regulator_dev *rdev,
  2694. unsigned long event, void *data)
  2695. {
  2696. /* call rdev chain first */
  2697. return blocking_notifier_call_chain(&rdev->notifier, event, data);
  2698. }
  2699. /**
  2700. * regulator_bulk_get - get multiple regulator consumers
  2701. *
  2702. * @dev: Device to supply
  2703. * @num_consumers: Number of consumers to register
  2704. * @consumers: Configuration of consumers; clients are stored here.
  2705. *
  2706. * @return 0 on success, an errno on failure.
  2707. *
  2708. * This helper function allows drivers to get several regulator
  2709. * consumers in one operation. If any of the regulators cannot be
  2710. * acquired then any regulators that were allocated will be freed
  2711. * before returning to the caller.
  2712. */
  2713. int regulator_bulk_get(struct device *dev, int num_consumers,
  2714. struct regulator_bulk_data *consumers)
  2715. {
  2716. int i;
  2717. int ret;
  2718. for (i = 0; i < num_consumers; i++)
  2719. consumers[i].consumer = NULL;
  2720. for (i = 0; i < num_consumers; i++) {
  2721. consumers[i].consumer = regulator_get(dev,
  2722. consumers[i].supply);
  2723. if (IS_ERR(consumers[i].consumer)) {
  2724. ret = PTR_ERR(consumers[i].consumer);
  2725. dev_err(dev, "Failed to get supply '%s': %d\n",
  2726. consumers[i].supply, ret);
  2727. consumers[i].consumer = NULL;
  2728. goto err;
  2729. }
  2730. }
  2731. return 0;
  2732. err:
  2733. while (--i >= 0)
  2734. regulator_put(consumers[i].consumer);
  2735. return ret;
  2736. }
  2737. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2738. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2739. {
  2740. struct regulator_bulk_data *bulk = data;
  2741. bulk->ret = regulator_enable(bulk->consumer);
  2742. }
  2743. /**
  2744. * regulator_bulk_enable - enable multiple regulator consumers
  2745. *
  2746. * @num_consumers: Number of consumers
  2747. * @consumers: Consumer data; clients are stored here.
  2748. * @return 0 on success, an errno on failure
  2749. *
  2750. * This convenience API allows consumers to enable multiple regulator
  2751. * clients in a single API call. If any consumers cannot be enabled
  2752. * then any others that were enabled will be disabled again prior to
  2753. * return.
  2754. */
  2755. int regulator_bulk_enable(int num_consumers,
  2756. struct regulator_bulk_data *consumers)
  2757. {
  2758. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  2759. int i;
  2760. int ret = 0;
  2761. for (i = 0; i < num_consumers; i++) {
  2762. if (consumers[i].consumer->always_on)
  2763. consumers[i].ret = 0;
  2764. else
  2765. async_schedule_domain(regulator_bulk_enable_async,
  2766. &consumers[i], &async_domain);
  2767. }
  2768. async_synchronize_full_domain(&async_domain);
  2769. /* If any consumer failed we need to unwind any that succeeded */
  2770. for (i = 0; i < num_consumers; i++) {
  2771. if (consumers[i].ret != 0) {
  2772. ret = consumers[i].ret;
  2773. goto err;
  2774. }
  2775. }
  2776. return 0;
  2777. err:
  2778. for (i = 0; i < num_consumers; i++) {
  2779. if (consumers[i].ret < 0)
  2780. pr_err("Failed to enable %s: %d\n", consumers[i].supply,
  2781. consumers[i].ret);
  2782. else
  2783. regulator_disable(consumers[i].consumer);
  2784. }
  2785. return ret;
  2786. }
  2787. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2788. /**
  2789. * regulator_bulk_disable - disable multiple regulator consumers
  2790. *
  2791. * @num_consumers: Number of consumers
  2792. * @consumers: Consumer data; clients are stored here.
  2793. * @return 0 on success, an errno on failure
  2794. *
  2795. * This convenience API allows consumers to disable multiple regulator
  2796. * clients in a single API call. If any consumers cannot be disabled
  2797. * then any others that were disabled will be enabled again prior to
  2798. * return.
  2799. */
  2800. int regulator_bulk_disable(int num_consumers,
  2801. struct regulator_bulk_data *consumers)
  2802. {
  2803. int i;
  2804. int ret, r;
  2805. for (i = num_consumers - 1; i >= 0; --i) {
  2806. ret = regulator_disable(consumers[i].consumer);
  2807. if (ret != 0)
  2808. goto err;
  2809. }
  2810. return 0;
  2811. err:
  2812. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2813. for (++i; i < num_consumers; ++i) {
  2814. r = regulator_enable(consumers[i].consumer);
  2815. if (r != 0)
  2816. pr_err("Failed to reename %s: %d\n",
  2817. consumers[i].supply, r);
  2818. }
  2819. return ret;
  2820. }
  2821. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2822. /**
  2823. * regulator_bulk_force_disable - force disable multiple regulator consumers
  2824. *
  2825. * @num_consumers: Number of consumers
  2826. * @consumers: Consumer data; clients are stored here.
  2827. * @return 0 on success, an errno on failure
  2828. *
  2829. * This convenience API allows consumers to forcibly disable multiple regulator
  2830. * clients in a single API call.
  2831. * NOTE: This should be used for situations when device damage will
  2832. * likely occur if the regulators are not disabled (e.g. over temp).
  2833. * Although regulator_force_disable function call for some consumers can
  2834. * return error numbers, the function is called for all consumers.
  2835. */
  2836. int regulator_bulk_force_disable(int num_consumers,
  2837. struct regulator_bulk_data *consumers)
  2838. {
  2839. int i;
  2840. int ret;
  2841. for (i = 0; i < num_consumers; i++)
  2842. consumers[i].ret =
  2843. regulator_force_disable(consumers[i].consumer);
  2844. for (i = 0; i < num_consumers; i++) {
  2845. if (consumers[i].ret != 0) {
  2846. ret = consumers[i].ret;
  2847. goto out;
  2848. }
  2849. }
  2850. return 0;
  2851. out:
  2852. return ret;
  2853. }
  2854. EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
  2855. /**
  2856. * regulator_bulk_free - free multiple regulator consumers
  2857. *
  2858. * @num_consumers: Number of consumers
  2859. * @consumers: Consumer data; clients are stored here.
  2860. *
  2861. * This convenience API allows consumers to free multiple regulator
  2862. * clients in a single API call.
  2863. */
  2864. void regulator_bulk_free(int num_consumers,
  2865. struct regulator_bulk_data *consumers)
  2866. {
  2867. int i;
  2868. for (i = 0; i < num_consumers; i++) {
  2869. regulator_put(consumers[i].consumer);
  2870. consumers[i].consumer = NULL;
  2871. }
  2872. }
  2873. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2874. /**
  2875. * regulator_notifier_call_chain - call regulator event notifier
  2876. * @rdev: regulator source
  2877. * @event: notifier block
  2878. * @data: callback-specific data.
  2879. *
  2880. * Called by regulator drivers to notify clients a regulator event has
  2881. * occurred. We also notify regulator clients downstream.
  2882. * Note lock must be held by caller.
  2883. */
  2884. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2885. unsigned long event, void *data)
  2886. {
  2887. _notifier_call_chain(rdev, event, data);
  2888. return NOTIFY_DONE;
  2889. }
  2890. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2891. /**
  2892. * regulator_mode_to_status - convert a regulator mode into a status
  2893. *
  2894. * @mode: Mode to convert
  2895. *
  2896. * Convert a regulator mode into a status.
  2897. */
  2898. int regulator_mode_to_status(unsigned int mode)
  2899. {
  2900. switch (mode) {
  2901. case REGULATOR_MODE_FAST:
  2902. return REGULATOR_STATUS_FAST;
  2903. case REGULATOR_MODE_NORMAL:
  2904. return REGULATOR_STATUS_NORMAL;
  2905. case REGULATOR_MODE_IDLE:
  2906. return REGULATOR_STATUS_IDLE;
  2907. case REGULATOR_MODE_STANDBY:
  2908. return REGULATOR_STATUS_STANDBY;
  2909. default:
  2910. return REGULATOR_STATUS_UNDEFINED;
  2911. }
  2912. }
  2913. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2914. /*
  2915. * To avoid cluttering sysfs (and memory) with useless state, only
  2916. * create attributes that can be meaningfully displayed.
  2917. */
  2918. static int add_regulator_attributes(struct regulator_dev *rdev)
  2919. {
  2920. struct device *dev = &rdev->dev;
  2921. const struct regulator_ops *ops = rdev->desc->ops;
  2922. int status = 0;
  2923. /* some attributes need specific methods to be displayed */
  2924. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2925. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
  2926. (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
  2927. (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1))) {
  2928. status = device_create_file(dev, &dev_attr_microvolts);
  2929. if (status < 0)
  2930. return status;
  2931. }
  2932. if (ops->get_current_limit) {
  2933. status = device_create_file(dev, &dev_attr_microamps);
  2934. if (status < 0)
  2935. return status;
  2936. }
  2937. if (ops->get_mode) {
  2938. status = device_create_file(dev, &dev_attr_opmode);
  2939. if (status < 0)
  2940. return status;
  2941. }
  2942. if (rdev->ena_pin || ops->is_enabled) {
  2943. status = device_create_file(dev, &dev_attr_state);
  2944. if (status < 0)
  2945. return status;
  2946. }
  2947. if (ops->get_status) {
  2948. status = device_create_file(dev, &dev_attr_status);
  2949. if (status < 0)
  2950. return status;
  2951. }
  2952. if (ops->get_bypass) {
  2953. status = device_create_file(dev, &dev_attr_bypass);
  2954. if (status < 0)
  2955. return status;
  2956. }
  2957. /* some attributes are type-specific */
  2958. if (rdev->desc->type == REGULATOR_CURRENT) {
  2959. status = device_create_file(dev, &dev_attr_requested_microamps);
  2960. if (status < 0)
  2961. return status;
  2962. }
  2963. /* all the other attributes exist to support constraints;
  2964. * don't show them if there are no constraints, or if the
  2965. * relevant supporting methods are missing.
  2966. */
  2967. if (!rdev->constraints)
  2968. return status;
  2969. /* constraints need specific supporting methods */
  2970. if (ops->set_voltage || ops->set_voltage_sel) {
  2971. status = device_create_file(dev, &dev_attr_min_microvolts);
  2972. if (status < 0)
  2973. return status;
  2974. status = device_create_file(dev, &dev_attr_max_microvolts);
  2975. if (status < 0)
  2976. return status;
  2977. }
  2978. if (ops->set_current_limit) {
  2979. status = device_create_file(dev, &dev_attr_min_microamps);
  2980. if (status < 0)
  2981. return status;
  2982. status = device_create_file(dev, &dev_attr_max_microamps);
  2983. if (status < 0)
  2984. return status;
  2985. }
  2986. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2987. if (status < 0)
  2988. return status;
  2989. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2990. if (status < 0)
  2991. return status;
  2992. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2993. if (status < 0)
  2994. return status;
  2995. if (ops->set_suspend_voltage) {
  2996. status = device_create_file(dev,
  2997. &dev_attr_suspend_standby_microvolts);
  2998. if (status < 0)
  2999. return status;
  3000. status = device_create_file(dev,
  3001. &dev_attr_suspend_mem_microvolts);
  3002. if (status < 0)
  3003. return status;
  3004. status = device_create_file(dev,
  3005. &dev_attr_suspend_disk_microvolts);
  3006. if (status < 0)
  3007. return status;
  3008. }
  3009. if (ops->set_suspend_mode) {
  3010. status = device_create_file(dev,
  3011. &dev_attr_suspend_standby_mode);
  3012. if (status < 0)
  3013. return status;
  3014. status = device_create_file(dev,
  3015. &dev_attr_suspend_mem_mode);
  3016. if (status < 0)
  3017. return status;
  3018. status = device_create_file(dev,
  3019. &dev_attr_suspend_disk_mode);
  3020. if (status < 0)
  3021. return status;
  3022. }
  3023. return status;
  3024. }
  3025. static void rdev_init_debugfs(struct regulator_dev *rdev)
  3026. {
  3027. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  3028. if (!rdev->debugfs) {
  3029. rdev_warn(rdev, "Failed to create debugfs directory\n");
  3030. return;
  3031. }
  3032. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  3033. &rdev->use_count);
  3034. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  3035. &rdev->open_count);
  3036. debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
  3037. &rdev->bypass_count);
  3038. }
  3039. /**
  3040. * regulator_register - register regulator
  3041. * @regulator_desc: regulator to register
  3042. * @config: runtime configuration for regulator
  3043. *
  3044. * Called by regulator drivers to register a regulator.
  3045. * Returns a valid pointer to struct regulator_dev on success
  3046. * or an ERR_PTR() on error.
  3047. */
  3048. struct regulator_dev *
  3049. regulator_register(const struct regulator_desc *regulator_desc,
  3050. const struct regulator_config *config)
  3051. {
  3052. const struct regulation_constraints *constraints = NULL;
  3053. const struct regulator_init_data *init_data;
  3054. static atomic_t regulator_no = ATOMIC_INIT(0);
  3055. struct regulator_dev *rdev;
  3056. struct device *dev;
  3057. int ret, i;
  3058. const char *supply = NULL;
  3059. if (regulator_desc == NULL || config == NULL)
  3060. return ERR_PTR(-EINVAL);
  3061. dev = config->dev;
  3062. WARN_ON(!dev);
  3063. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  3064. return ERR_PTR(-EINVAL);
  3065. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  3066. regulator_desc->type != REGULATOR_CURRENT)
  3067. return ERR_PTR(-EINVAL);
  3068. /* Only one of each should be implemented */
  3069. WARN_ON(regulator_desc->ops->get_voltage &&
  3070. regulator_desc->ops->get_voltage_sel);
  3071. WARN_ON(regulator_desc->ops->set_voltage &&
  3072. regulator_desc->ops->set_voltage_sel);
  3073. /* If we're using selectors we must implement list_voltage. */
  3074. if (regulator_desc->ops->get_voltage_sel &&
  3075. !regulator_desc->ops->list_voltage) {
  3076. return ERR_PTR(-EINVAL);
  3077. }
  3078. if (regulator_desc->ops->set_voltage_sel &&
  3079. !regulator_desc->ops->list_voltage) {
  3080. return ERR_PTR(-EINVAL);
  3081. }
  3082. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  3083. if (rdev == NULL)
  3084. return ERR_PTR(-ENOMEM);
  3085. init_data = regulator_of_get_init_data(dev, regulator_desc,
  3086. &rdev->dev.of_node);
  3087. if (!init_data) {
  3088. init_data = config->init_data;
  3089. rdev->dev.of_node = of_node_get(config->of_node);
  3090. }
  3091. mutex_lock(&regulator_list_mutex);
  3092. mutex_init(&rdev->mutex);
  3093. rdev->reg_data = config->driver_data;
  3094. rdev->owner = regulator_desc->owner;
  3095. rdev->desc = regulator_desc;
  3096. if (config->regmap)
  3097. rdev->regmap = config->regmap;
  3098. else if (dev_get_regmap(dev, NULL))
  3099. rdev->regmap = dev_get_regmap(dev, NULL);
  3100. else if (dev->parent)
  3101. rdev->regmap = dev_get_regmap(dev->parent, NULL);
  3102. INIT_LIST_HEAD(&rdev->consumer_list);
  3103. INIT_LIST_HEAD(&rdev->list);
  3104. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  3105. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  3106. /* preform any regulator specific init */
  3107. if (init_data && init_data->regulator_init) {
  3108. ret = init_data->regulator_init(rdev->reg_data);
  3109. if (ret < 0)
  3110. goto clean;
  3111. }
  3112. /* register with sysfs */
  3113. rdev->dev.class = &regulator_class;
  3114. rdev->dev.parent = dev;
  3115. dev_set_name(&rdev->dev, "regulator.%d",
  3116. atomic_inc_return(&regulator_no) - 1);
  3117. ret = device_register(&rdev->dev);
  3118. if (ret != 0) {
  3119. put_device(&rdev->dev);
  3120. goto clean;
  3121. }
  3122. dev_set_drvdata(&rdev->dev, rdev);
  3123. if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
  3124. ret = regulator_ena_gpio_request(rdev, config);
  3125. if (ret != 0) {
  3126. rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
  3127. config->ena_gpio, ret);
  3128. goto wash;
  3129. }
  3130. if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
  3131. rdev->ena_gpio_state = 1;
  3132. if (config->ena_gpio_invert)
  3133. rdev->ena_gpio_state = !rdev->ena_gpio_state;
  3134. }
  3135. /* set regulator constraints */
  3136. if (init_data)
  3137. constraints = &init_data->constraints;
  3138. ret = set_machine_constraints(rdev, constraints);
  3139. if (ret < 0)
  3140. goto scrub;
  3141. /* add attributes supported by this regulator */
  3142. ret = add_regulator_attributes(rdev);
  3143. if (ret < 0)
  3144. goto scrub;
  3145. if (init_data && init_data->supply_regulator)
  3146. supply = init_data->supply_regulator;
  3147. else if (regulator_desc->supply_name)
  3148. supply = regulator_desc->supply_name;
  3149. if (supply) {
  3150. struct regulator_dev *r;
  3151. r = regulator_dev_lookup(dev, supply, &ret);
  3152. if (ret == -ENODEV) {
  3153. /*
  3154. * No supply was specified for this regulator and
  3155. * there will never be one.
  3156. */
  3157. ret = 0;
  3158. goto add_dev;
  3159. } else if (!r) {
  3160. dev_err(dev, "Failed to find supply %s\n", supply);
  3161. ret = -EPROBE_DEFER;
  3162. goto scrub;
  3163. }
  3164. ret = set_supply(rdev, r);
  3165. if (ret < 0)
  3166. goto scrub;
  3167. /* Enable supply if rail is enabled */
  3168. if (_regulator_is_enabled(rdev)) {
  3169. ret = regulator_enable(rdev->supply);
  3170. if (ret < 0)
  3171. goto scrub;
  3172. }
  3173. }
  3174. add_dev:
  3175. /* add consumers devices */
  3176. if (init_data) {
  3177. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  3178. ret = set_consumer_device_supply(rdev,
  3179. init_data->consumer_supplies[i].dev_name,
  3180. init_data->consumer_supplies[i].supply);
  3181. if (ret < 0) {
  3182. dev_err(dev, "Failed to set supply %s\n",
  3183. init_data->consumer_supplies[i].supply);
  3184. goto unset_supplies;
  3185. }
  3186. }
  3187. }
  3188. list_add(&rdev->list, &regulator_list);
  3189. rdev_init_debugfs(rdev);
  3190. out:
  3191. mutex_unlock(&regulator_list_mutex);
  3192. return rdev;
  3193. unset_supplies:
  3194. unset_regulator_supplies(rdev);
  3195. scrub:
  3196. if (rdev->supply)
  3197. _regulator_put(rdev->supply);
  3198. regulator_ena_gpio_free(rdev);
  3199. kfree(rdev->constraints);
  3200. wash:
  3201. device_unregister(&rdev->dev);
  3202. /* device core frees rdev */
  3203. rdev = ERR_PTR(ret);
  3204. goto out;
  3205. clean:
  3206. kfree(rdev);
  3207. rdev = ERR_PTR(ret);
  3208. goto out;
  3209. }
  3210. EXPORT_SYMBOL_GPL(regulator_register);
  3211. /**
  3212. * regulator_unregister - unregister regulator
  3213. * @rdev: regulator to unregister
  3214. *
  3215. * Called by regulator drivers to unregister a regulator.
  3216. */
  3217. void regulator_unregister(struct regulator_dev *rdev)
  3218. {
  3219. if (rdev == NULL)
  3220. return;
  3221. if (rdev->supply) {
  3222. while (rdev->use_count--)
  3223. regulator_disable(rdev->supply);
  3224. regulator_put(rdev->supply);
  3225. }
  3226. mutex_lock(&regulator_list_mutex);
  3227. debugfs_remove_recursive(rdev->debugfs);
  3228. flush_work(&rdev->disable_work.work);
  3229. WARN_ON(rdev->open_count);
  3230. unset_regulator_supplies(rdev);
  3231. list_del(&rdev->list);
  3232. kfree(rdev->constraints);
  3233. regulator_ena_gpio_free(rdev);
  3234. of_node_put(rdev->dev.of_node);
  3235. device_unregister(&rdev->dev);
  3236. mutex_unlock(&regulator_list_mutex);
  3237. }
  3238. EXPORT_SYMBOL_GPL(regulator_unregister);
  3239. /**
  3240. * regulator_suspend_prepare - prepare regulators for system wide suspend
  3241. * @state: system suspend state
  3242. *
  3243. * Configure each regulator with it's suspend operating parameters for state.
  3244. * This will usually be called by machine suspend code prior to supending.
  3245. */
  3246. int regulator_suspend_prepare(suspend_state_t state)
  3247. {
  3248. struct regulator_dev *rdev;
  3249. int ret = 0;
  3250. /* ON is handled by regulator active state */
  3251. if (state == PM_SUSPEND_ON)
  3252. return -EINVAL;
  3253. mutex_lock(&regulator_list_mutex);
  3254. list_for_each_entry(rdev, &regulator_list, list) {
  3255. mutex_lock(&rdev->mutex);
  3256. ret = suspend_prepare(rdev, state);
  3257. mutex_unlock(&rdev->mutex);
  3258. if (ret < 0) {
  3259. rdev_err(rdev, "failed to prepare\n");
  3260. goto out;
  3261. }
  3262. }
  3263. out:
  3264. mutex_unlock(&regulator_list_mutex);
  3265. return ret;
  3266. }
  3267. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  3268. /**
  3269. * regulator_suspend_finish - resume regulators from system wide suspend
  3270. *
  3271. * Turn on regulators that might be turned off by regulator_suspend_prepare
  3272. * and that should be turned on according to the regulators properties.
  3273. */
  3274. int regulator_suspend_finish(void)
  3275. {
  3276. struct regulator_dev *rdev;
  3277. int ret = 0, error;
  3278. mutex_lock(&regulator_list_mutex);
  3279. list_for_each_entry(rdev, &regulator_list, list) {
  3280. mutex_lock(&rdev->mutex);
  3281. if (rdev->use_count > 0 || rdev->constraints->always_on) {
  3282. error = _regulator_do_enable(rdev);
  3283. if (error)
  3284. ret = error;
  3285. } else {
  3286. if (!have_full_constraints())
  3287. goto unlock;
  3288. if (!_regulator_is_enabled(rdev))
  3289. goto unlock;
  3290. error = _regulator_do_disable(rdev);
  3291. if (error)
  3292. ret = error;
  3293. }
  3294. unlock:
  3295. mutex_unlock(&rdev->mutex);
  3296. }
  3297. mutex_unlock(&regulator_list_mutex);
  3298. return ret;
  3299. }
  3300. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  3301. /**
  3302. * regulator_has_full_constraints - the system has fully specified constraints
  3303. *
  3304. * Calling this function will cause the regulator API to disable all
  3305. * regulators which have a zero use count and don't have an always_on
  3306. * constraint in a late_initcall.
  3307. *
  3308. * The intention is that this will become the default behaviour in a
  3309. * future kernel release so users are encouraged to use this facility
  3310. * now.
  3311. */
  3312. void regulator_has_full_constraints(void)
  3313. {
  3314. has_full_constraints = 1;
  3315. }
  3316. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  3317. /**
  3318. * rdev_get_drvdata - get rdev regulator driver data
  3319. * @rdev: regulator
  3320. *
  3321. * Get rdev regulator driver private data. This call can be used in the
  3322. * regulator driver context.
  3323. */
  3324. void *rdev_get_drvdata(struct regulator_dev *rdev)
  3325. {
  3326. return rdev->reg_data;
  3327. }
  3328. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  3329. /**
  3330. * regulator_get_drvdata - get regulator driver data
  3331. * @regulator: regulator
  3332. *
  3333. * Get regulator driver private data. This call can be used in the consumer
  3334. * driver context when non API regulator specific functions need to be called.
  3335. */
  3336. void *regulator_get_drvdata(struct regulator *regulator)
  3337. {
  3338. return regulator->rdev->reg_data;
  3339. }
  3340. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  3341. /**
  3342. * regulator_set_drvdata - set regulator driver data
  3343. * @regulator: regulator
  3344. * @data: data
  3345. */
  3346. void regulator_set_drvdata(struct regulator *regulator, void *data)
  3347. {
  3348. regulator->rdev->reg_data = data;
  3349. }
  3350. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  3351. /**
  3352. * regulator_get_id - get regulator ID
  3353. * @rdev: regulator
  3354. */
  3355. int rdev_get_id(struct regulator_dev *rdev)
  3356. {
  3357. return rdev->desc->id;
  3358. }
  3359. EXPORT_SYMBOL_GPL(rdev_get_id);
  3360. struct device *rdev_get_dev(struct regulator_dev *rdev)
  3361. {
  3362. return &rdev->dev;
  3363. }
  3364. EXPORT_SYMBOL_GPL(rdev_get_dev);
  3365. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  3366. {
  3367. return reg_init_data->driver_data;
  3368. }
  3369. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  3370. #ifdef CONFIG_DEBUG_FS
  3371. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  3372. size_t count, loff_t *ppos)
  3373. {
  3374. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3375. ssize_t len, ret = 0;
  3376. struct regulator_map *map;
  3377. if (!buf)
  3378. return -ENOMEM;
  3379. list_for_each_entry(map, &regulator_map_list, list) {
  3380. len = snprintf(buf + ret, PAGE_SIZE - ret,
  3381. "%s -> %s.%s\n",
  3382. rdev_get_name(map->regulator), map->dev_name,
  3383. map->supply);
  3384. if (len >= 0)
  3385. ret += len;
  3386. if (ret > PAGE_SIZE) {
  3387. ret = PAGE_SIZE;
  3388. break;
  3389. }
  3390. }
  3391. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  3392. kfree(buf);
  3393. return ret;
  3394. }
  3395. #endif
  3396. static const struct file_operations supply_map_fops = {
  3397. #ifdef CONFIG_DEBUG_FS
  3398. .read = supply_map_read_file,
  3399. .llseek = default_llseek,
  3400. #endif
  3401. };
  3402. static int __init regulator_init(void)
  3403. {
  3404. int ret;
  3405. ret = class_register(&regulator_class);
  3406. debugfs_root = debugfs_create_dir("regulator", NULL);
  3407. if (!debugfs_root)
  3408. pr_warn("regulator: Failed to create debugfs directory\n");
  3409. debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
  3410. &supply_map_fops);
  3411. regulator_dummy_init();
  3412. return ret;
  3413. }
  3414. /* init early to allow our consumers to complete system booting */
  3415. core_initcall(regulator_init);
  3416. static int __init regulator_init_complete(void)
  3417. {
  3418. struct regulator_dev *rdev;
  3419. const struct regulator_ops *ops;
  3420. struct regulation_constraints *c;
  3421. int enabled, ret;
  3422. /*
  3423. * Since DT doesn't provide an idiomatic mechanism for
  3424. * enabling full constraints and since it's much more natural
  3425. * with DT to provide them just assume that a DT enabled
  3426. * system has full constraints.
  3427. */
  3428. if (of_have_populated_dt())
  3429. has_full_constraints = true;
  3430. mutex_lock(&regulator_list_mutex);
  3431. /* If we have a full configuration then disable any regulators
  3432. * we have permission to change the status for and which are
  3433. * not in use or always_on. This is effectively the default
  3434. * for DT and ACPI as they have full constraints.
  3435. */
  3436. list_for_each_entry(rdev, &regulator_list, list) {
  3437. ops = rdev->desc->ops;
  3438. c = rdev->constraints;
  3439. if (c && c->always_on)
  3440. continue;
  3441. if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
  3442. continue;
  3443. mutex_lock(&rdev->mutex);
  3444. if (rdev->use_count)
  3445. goto unlock;
  3446. /* If we can't read the status assume it's on. */
  3447. if (ops->is_enabled)
  3448. enabled = ops->is_enabled(rdev);
  3449. else
  3450. enabled = 1;
  3451. if (!enabled)
  3452. goto unlock;
  3453. if (have_full_constraints()) {
  3454. /* We log since this may kill the system if it
  3455. * goes wrong. */
  3456. rdev_info(rdev, "disabling\n");
  3457. ret = _regulator_do_disable(rdev);
  3458. if (ret != 0)
  3459. rdev_err(rdev, "couldn't disable: %d\n", ret);
  3460. } else {
  3461. /* The intention is that in future we will
  3462. * assume that full constraints are provided
  3463. * so warn even if we aren't going to do
  3464. * anything here.
  3465. */
  3466. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  3467. }
  3468. unlock:
  3469. mutex_unlock(&rdev->mutex);
  3470. }
  3471. mutex_unlock(&regulator_list_mutex);
  3472. return 0;
  3473. }
  3474. late_initcall_sync(regulator_init_complete);