core.c 127 KB

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