core.c 93 KB

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