core.c 97 KB

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