core.c 98 KB

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