core.c 98 KB

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