core.c 93 KB

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