core.c 80 KB

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