core.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154
  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. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  780. GFP_KERNEL);
  781. if (!rdev->constraints)
  782. return -ENOMEM;
  783. ret = machine_constraints_voltage(rdev, rdev->constraints);
  784. if (ret != 0)
  785. goto out;
  786. /* do we need to setup our suspend state */
  787. if (constraints->initial_state) {
  788. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  789. if (ret < 0) {
  790. rdev_err(rdev, "failed to set suspend state\n");
  791. goto out;
  792. }
  793. }
  794. if (constraints->initial_mode) {
  795. if (!ops->set_mode) {
  796. rdev_err(rdev, "no set_mode operation\n");
  797. ret = -EINVAL;
  798. goto out;
  799. }
  800. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  801. if (ret < 0) {
  802. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  803. goto out;
  804. }
  805. }
  806. /* If the constraints say the regulator should be on at this point
  807. * and we have control then make sure it is enabled.
  808. */
  809. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  810. ops->enable) {
  811. ret = ops->enable(rdev);
  812. if (ret < 0) {
  813. rdev_err(rdev, "failed to enable\n");
  814. goto out;
  815. }
  816. }
  817. print_constraints(rdev);
  818. return 0;
  819. out:
  820. kfree(rdev->constraints);
  821. rdev->constraints = NULL;
  822. return ret;
  823. }
  824. /**
  825. * set_supply - set regulator supply regulator
  826. * @rdev: regulator name
  827. * @supply_rdev: supply regulator name
  828. *
  829. * Called by platform initialisation code to set the supply regulator for this
  830. * regulator. This ensures that a regulators supply will also be enabled by the
  831. * core if it's child is enabled.
  832. */
  833. static int set_supply(struct regulator_dev *rdev,
  834. struct regulator_dev *supply_rdev)
  835. {
  836. int err;
  837. rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
  838. rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
  839. if (IS_ERR(rdev->supply)) {
  840. err = PTR_ERR(rdev->supply);
  841. rdev->supply = NULL;
  842. return err;
  843. }
  844. return 0;
  845. }
  846. /**
  847. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  848. * @rdev: regulator source
  849. * @consumer_dev: device the supply applies to
  850. * @consumer_dev_name: dev_name() string for device supply applies to
  851. * @supply: symbolic name for supply
  852. *
  853. * Allows platform initialisation code to map physical regulator
  854. * sources to symbolic names for supplies for use by devices. Devices
  855. * should use these symbolic names to request regulators, avoiding the
  856. * need to provide board-specific regulator names as platform data.
  857. *
  858. * Only one of consumer_dev and consumer_dev_name may be specified.
  859. */
  860. static int set_consumer_device_supply(struct regulator_dev *rdev,
  861. struct device *consumer_dev, const char *consumer_dev_name,
  862. const char *supply)
  863. {
  864. struct regulator_map *node;
  865. int has_dev;
  866. if (consumer_dev && consumer_dev_name)
  867. return -EINVAL;
  868. if (!consumer_dev_name && consumer_dev)
  869. consumer_dev_name = dev_name(consumer_dev);
  870. if (supply == NULL)
  871. return -EINVAL;
  872. if (consumer_dev_name != NULL)
  873. has_dev = 1;
  874. else
  875. has_dev = 0;
  876. list_for_each_entry(node, &regulator_map_list, list) {
  877. if (node->dev_name && consumer_dev_name) {
  878. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  879. continue;
  880. } else if (node->dev_name || consumer_dev_name) {
  881. continue;
  882. }
  883. if (strcmp(node->supply, supply) != 0)
  884. continue;
  885. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  886. dev_name(&node->regulator->dev),
  887. node->regulator->desc->name,
  888. supply,
  889. dev_name(&rdev->dev), rdev_get_name(rdev));
  890. return -EBUSY;
  891. }
  892. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  893. if (node == NULL)
  894. return -ENOMEM;
  895. node->regulator = rdev;
  896. node->supply = supply;
  897. if (has_dev) {
  898. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  899. if (node->dev_name == NULL) {
  900. kfree(node);
  901. return -ENOMEM;
  902. }
  903. }
  904. list_add(&node->list, &regulator_map_list);
  905. return 0;
  906. }
  907. static void unset_regulator_supplies(struct regulator_dev *rdev)
  908. {
  909. struct regulator_map *node, *n;
  910. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  911. if (rdev == node->regulator) {
  912. list_del(&node->list);
  913. kfree(node->dev_name);
  914. kfree(node);
  915. }
  916. }
  917. }
  918. #define REG_STR_SIZE 64
  919. static struct regulator *create_regulator(struct regulator_dev *rdev,
  920. struct device *dev,
  921. const char *supply_name)
  922. {
  923. struct regulator *regulator;
  924. char buf[REG_STR_SIZE];
  925. int err, size;
  926. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  927. if (regulator == NULL)
  928. return NULL;
  929. mutex_lock(&rdev->mutex);
  930. regulator->rdev = rdev;
  931. list_add(&regulator->list, &rdev->consumer_list);
  932. if (dev) {
  933. /* create a 'requested_microamps_name' sysfs entry */
  934. size = scnprintf(buf, REG_STR_SIZE,
  935. "microamps_requested_%s-%s",
  936. dev_name(dev), supply_name);
  937. if (size >= REG_STR_SIZE)
  938. goto overflow_err;
  939. regulator->dev = dev;
  940. sysfs_attr_init(&regulator->dev_attr.attr);
  941. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  942. if (regulator->dev_attr.attr.name == NULL)
  943. goto attr_name_err;
  944. regulator->dev_attr.attr.mode = 0444;
  945. regulator->dev_attr.show = device_requested_uA_show;
  946. err = device_create_file(dev, &regulator->dev_attr);
  947. if (err < 0) {
  948. rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
  949. goto attr_name_err;
  950. }
  951. /* also add a link to the device sysfs entry */
  952. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  953. dev->kobj.name, supply_name);
  954. if (size >= REG_STR_SIZE)
  955. goto attr_err;
  956. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  957. if (regulator->supply_name == NULL)
  958. goto attr_err;
  959. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  960. buf);
  961. if (err) {
  962. rdev_warn(rdev, "could not add device link %s err %d\n",
  963. dev->kobj.name, err);
  964. goto link_name_err;
  965. }
  966. } else {
  967. regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
  968. if (regulator->supply_name == NULL)
  969. goto attr_err;
  970. }
  971. #ifdef CONFIG_DEBUG_FS
  972. regulator->debugfs = debugfs_create_dir(regulator->supply_name,
  973. rdev->debugfs);
  974. if (IS_ERR_OR_NULL(regulator->debugfs)) {
  975. rdev_warn(rdev, "Failed to create debugfs directory\n");
  976. regulator->debugfs = NULL;
  977. } else {
  978. debugfs_create_u32("uA_load", 0444, regulator->debugfs,
  979. &regulator->uA_load);
  980. debugfs_create_u32("min_uV", 0444, regulator->debugfs,
  981. &regulator->min_uV);
  982. debugfs_create_u32("max_uV", 0444, regulator->debugfs,
  983. &regulator->max_uV);
  984. }
  985. #endif
  986. mutex_unlock(&rdev->mutex);
  987. return regulator;
  988. link_name_err:
  989. kfree(regulator->supply_name);
  990. attr_err:
  991. device_remove_file(regulator->dev, &regulator->dev_attr);
  992. attr_name_err:
  993. kfree(regulator->dev_attr.attr.name);
  994. overflow_err:
  995. list_del(&regulator->list);
  996. kfree(regulator);
  997. mutex_unlock(&rdev->mutex);
  998. return NULL;
  999. }
  1000. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  1001. {
  1002. if (!rdev->desc->ops->enable_time)
  1003. return 0;
  1004. return rdev->desc->ops->enable_time(rdev);
  1005. }
  1006. static struct regulator_dev *regulator_dev_lookup(struct device *dev,
  1007. const char *supply)
  1008. {
  1009. struct regulator_dev *r;
  1010. struct device_node *node;
  1011. /* first do a dt based lookup */
  1012. if (dev && dev->of_node) {
  1013. node = of_get_regulator(dev, supply);
  1014. if (node)
  1015. list_for_each_entry(r, &regulator_list, list)
  1016. if (r->dev.parent &&
  1017. node == r->dev.of_node)
  1018. return r;
  1019. }
  1020. /* if not found, try doing it non-dt way */
  1021. list_for_each_entry(r, &regulator_list, list)
  1022. if (strcmp(rdev_get_name(r), supply) == 0)
  1023. return r;
  1024. return NULL;
  1025. }
  1026. /* Internal regulator request function */
  1027. static struct regulator *_regulator_get(struct device *dev, const char *id,
  1028. int exclusive)
  1029. {
  1030. struct regulator_dev *rdev;
  1031. struct regulator_map *map;
  1032. struct regulator *regulator = ERR_PTR(-ENODEV);
  1033. const char *devname = NULL;
  1034. int ret;
  1035. if (id == NULL) {
  1036. pr_err("get() with no identifier\n");
  1037. return regulator;
  1038. }
  1039. if (dev)
  1040. devname = dev_name(dev);
  1041. mutex_lock(&regulator_list_mutex);
  1042. rdev = regulator_dev_lookup(dev, id);
  1043. if (rdev)
  1044. goto found;
  1045. list_for_each_entry(map, &regulator_map_list, list) {
  1046. /* If the mapping has a device set up it must match */
  1047. if (map->dev_name &&
  1048. (!devname || strcmp(map->dev_name, devname)))
  1049. continue;
  1050. if (strcmp(map->supply, id) == 0) {
  1051. rdev = map->regulator;
  1052. goto found;
  1053. }
  1054. }
  1055. if (board_wants_dummy_regulator) {
  1056. rdev = dummy_regulator_rdev;
  1057. goto found;
  1058. }
  1059. #ifdef CONFIG_REGULATOR_DUMMY
  1060. if (!devname)
  1061. devname = "deviceless";
  1062. /* If the board didn't flag that it was fully constrained then
  1063. * substitute in a dummy regulator so consumers can continue.
  1064. */
  1065. if (!has_full_constraints) {
  1066. pr_warn("%s supply %s not found, using dummy regulator\n",
  1067. devname, id);
  1068. rdev = dummy_regulator_rdev;
  1069. goto found;
  1070. }
  1071. #endif
  1072. mutex_unlock(&regulator_list_mutex);
  1073. return regulator;
  1074. found:
  1075. if (rdev->exclusive) {
  1076. regulator = ERR_PTR(-EPERM);
  1077. goto out;
  1078. }
  1079. if (exclusive && rdev->open_count) {
  1080. regulator = ERR_PTR(-EBUSY);
  1081. goto out;
  1082. }
  1083. if (!try_module_get(rdev->owner))
  1084. goto out;
  1085. regulator = create_regulator(rdev, dev, id);
  1086. if (regulator == NULL) {
  1087. regulator = ERR_PTR(-ENOMEM);
  1088. module_put(rdev->owner);
  1089. }
  1090. rdev->open_count++;
  1091. if (exclusive) {
  1092. rdev->exclusive = 1;
  1093. ret = _regulator_is_enabled(rdev);
  1094. if (ret > 0)
  1095. rdev->use_count = 1;
  1096. else
  1097. rdev->use_count = 0;
  1098. }
  1099. out:
  1100. mutex_unlock(&regulator_list_mutex);
  1101. return regulator;
  1102. }
  1103. /**
  1104. * regulator_get - lookup and obtain a reference to a regulator.
  1105. * @dev: device for regulator "consumer"
  1106. * @id: Supply name or regulator ID.
  1107. *
  1108. * Returns a struct regulator corresponding to the regulator producer,
  1109. * or IS_ERR() condition containing errno.
  1110. *
  1111. * Use of supply names configured via regulator_set_device_supply() is
  1112. * strongly encouraged. It is recommended that the supply name used
  1113. * should match the name used for the supply and/or the relevant
  1114. * device pins in the datasheet.
  1115. */
  1116. struct regulator *regulator_get(struct device *dev, const char *id)
  1117. {
  1118. return _regulator_get(dev, id, 0);
  1119. }
  1120. EXPORT_SYMBOL_GPL(regulator_get);
  1121. /**
  1122. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1123. * @dev: device for regulator "consumer"
  1124. * @id: Supply name or regulator ID.
  1125. *
  1126. * Returns a struct regulator corresponding to the regulator producer,
  1127. * or IS_ERR() condition containing errno. Other consumers will be
  1128. * unable to obtain this reference is held and the use count for the
  1129. * regulator will be initialised to reflect the current state of the
  1130. * regulator.
  1131. *
  1132. * This is intended for use by consumers which cannot tolerate shared
  1133. * use of the regulator such as those which need to force the
  1134. * regulator off for correct operation of the hardware they are
  1135. * controlling.
  1136. *
  1137. * Use of supply names configured via regulator_set_device_supply() is
  1138. * strongly encouraged. It is recommended that the supply name used
  1139. * should match the name used for the supply and/or the relevant
  1140. * device pins in the datasheet.
  1141. */
  1142. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1143. {
  1144. return _regulator_get(dev, id, 1);
  1145. }
  1146. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1147. /**
  1148. * regulator_put - "free" the regulator source
  1149. * @regulator: regulator source
  1150. *
  1151. * Note: drivers must ensure that all regulator_enable calls made on this
  1152. * regulator source are balanced by regulator_disable calls prior to calling
  1153. * this function.
  1154. */
  1155. void regulator_put(struct regulator *regulator)
  1156. {
  1157. struct regulator_dev *rdev;
  1158. if (regulator == NULL || IS_ERR(regulator))
  1159. return;
  1160. mutex_lock(&regulator_list_mutex);
  1161. rdev = regulator->rdev;
  1162. #ifdef CONFIG_DEBUG_FS
  1163. debugfs_remove_recursive(regulator->debugfs);
  1164. #endif
  1165. /* remove any sysfs entries */
  1166. if (regulator->dev) {
  1167. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1168. device_remove_file(regulator->dev, &regulator->dev_attr);
  1169. kfree(regulator->dev_attr.attr.name);
  1170. }
  1171. kfree(regulator->supply_name);
  1172. list_del(&regulator->list);
  1173. kfree(regulator);
  1174. rdev->open_count--;
  1175. rdev->exclusive = 0;
  1176. module_put(rdev->owner);
  1177. mutex_unlock(&regulator_list_mutex);
  1178. }
  1179. EXPORT_SYMBOL_GPL(regulator_put);
  1180. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1181. {
  1182. if (!rdev->constraints)
  1183. return 0;
  1184. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1185. return 1;
  1186. else
  1187. return 0;
  1188. }
  1189. /* locks held by regulator_enable() */
  1190. static int _regulator_enable(struct regulator_dev *rdev)
  1191. {
  1192. int ret, delay;
  1193. /* check voltage and requested load before enabling */
  1194. if (rdev->constraints &&
  1195. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1196. drms_uA_update(rdev);
  1197. if (rdev->use_count == 0) {
  1198. /* The regulator may on if it's not switchable or left on */
  1199. ret = _regulator_is_enabled(rdev);
  1200. if (ret == -EINVAL || ret == 0) {
  1201. if (!_regulator_can_change_status(rdev))
  1202. return -EPERM;
  1203. if (!rdev->desc->ops->enable)
  1204. return -EINVAL;
  1205. /* Query before enabling in case configuration
  1206. * dependent. */
  1207. ret = _regulator_get_enable_time(rdev);
  1208. if (ret >= 0) {
  1209. delay = ret;
  1210. } else {
  1211. rdev_warn(rdev, "enable_time() failed: %d\n",
  1212. ret);
  1213. delay = 0;
  1214. }
  1215. trace_regulator_enable(rdev_get_name(rdev));
  1216. /* Allow the regulator to ramp; it would be useful
  1217. * to extend this for bulk operations so that the
  1218. * regulators can ramp together. */
  1219. ret = rdev->desc->ops->enable(rdev);
  1220. if (ret < 0)
  1221. return ret;
  1222. trace_regulator_enable_delay(rdev_get_name(rdev));
  1223. if (delay >= 1000) {
  1224. mdelay(delay / 1000);
  1225. udelay(delay % 1000);
  1226. } else if (delay) {
  1227. udelay(delay);
  1228. }
  1229. trace_regulator_enable_complete(rdev_get_name(rdev));
  1230. } else if (ret < 0) {
  1231. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1232. return ret;
  1233. }
  1234. /* Fallthrough on positive return values - already enabled */
  1235. }
  1236. rdev->use_count++;
  1237. return 0;
  1238. }
  1239. /**
  1240. * regulator_enable - enable regulator output
  1241. * @regulator: regulator source
  1242. *
  1243. * Request that the regulator be enabled with the regulator output at
  1244. * the predefined voltage or current value. Calls to regulator_enable()
  1245. * must be balanced with calls to regulator_disable().
  1246. *
  1247. * NOTE: the output value can be set by other drivers, boot loader or may be
  1248. * hardwired in the regulator.
  1249. */
  1250. int regulator_enable(struct regulator *regulator)
  1251. {
  1252. struct regulator_dev *rdev = regulator->rdev;
  1253. int ret = 0;
  1254. if (rdev->supply) {
  1255. ret = regulator_enable(rdev->supply);
  1256. if (ret != 0)
  1257. return ret;
  1258. }
  1259. mutex_lock(&rdev->mutex);
  1260. ret = _regulator_enable(rdev);
  1261. mutex_unlock(&rdev->mutex);
  1262. if (ret != 0 && rdev->supply)
  1263. regulator_disable(rdev->supply);
  1264. return ret;
  1265. }
  1266. EXPORT_SYMBOL_GPL(regulator_enable);
  1267. /* locks held by regulator_disable() */
  1268. static int _regulator_disable(struct regulator_dev *rdev)
  1269. {
  1270. int ret = 0;
  1271. if (WARN(rdev->use_count <= 0,
  1272. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1273. return -EIO;
  1274. /* are we the last user and permitted to disable ? */
  1275. if (rdev->use_count == 1 &&
  1276. (rdev->constraints && !rdev->constraints->always_on)) {
  1277. /* we are last user */
  1278. if (_regulator_can_change_status(rdev) &&
  1279. rdev->desc->ops->disable) {
  1280. trace_regulator_disable(rdev_get_name(rdev));
  1281. ret = rdev->desc->ops->disable(rdev);
  1282. if (ret < 0) {
  1283. rdev_err(rdev, "failed to disable\n");
  1284. return ret;
  1285. }
  1286. trace_regulator_disable_complete(rdev_get_name(rdev));
  1287. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1288. NULL);
  1289. }
  1290. rdev->use_count = 0;
  1291. } else if (rdev->use_count > 1) {
  1292. if (rdev->constraints &&
  1293. (rdev->constraints->valid_ops_mask &
  1294. REGULATOR_CHANGE_DRMS))
  1295. drms_uA_update(rdev);
  1296. rdev->use_count--;
  1297. }
  1298. return ret;
  1299. }
  1300. /**
  1301. * regulator_disable - disable regulator output
  1302. * @regulator: regulator source
  1303. *
  1304. * Disable the regulator output voltage or current. Calls to
  1305. * regulator_enable() must be balanced with calls to
  1306. * regulator_disable().
  1307. *
  1308. * NOTE: this will only disable the regulator output if no other consumer
  1309. * devices have it enabled, the regulator device supports disabling and
  1310. * machine constraints permit this operation.
  1311. */
  1312. int regulator_disable(struct regulator *regulator)
  1313. {
  1314. struct regulator_dev *rdev = regulator->rdev;
  1315. int ret = 0;
  1316. mutex_lock(&rdev->mutex);
  1317. ret = _regulator_disable(rdev);
  1318. mutex_unlock(&rdev->mutex);
  1319. if (ret == 0 && rdev->supply)
  1320. regulator_disable(rdev->supply);
  1321. return ret;
  1322. }
  1323. EXPORT_SYMBOL_GPL(regulator_disable);
  1324. /* locks held by regulator_force_disable() */
  1325. static int _regulator_force_disable(struct regulator_dev *rdev)
  1326. {
  1327. int ret = 0;
  1328. /* force disable */
  1329. if (rdev->desc->ops->disable) {
  1330. /* ah well, who wants to live forever... */
  1331. ret = rdev->desc->ops->disable(rdev);
  1332. if (ret < 0) {
  1333. rdev_err(rdev, "failed to force disable\n");
  1334. return ret;
  1335. }
  1336. /* notify other consumers that power has been forced off */
  1337. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1338. REGULATOR_EVENT_DISABLE, NULL);
  1339. }
  1340. return ret;
  1341. }
  1342. /**
  1343. * regulator_force_disable - force disable regulator output
  1344. * @regulator: regulator source
  1345. *
  1346. * Forcibly disable the regulator output voltage or current.
  1347. * NOTE: this *will* disable the regulator output even if other consumer
  1348. * devices have it enabled. This should be used for situations when device
  1349. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1350. */
  1351. int regulator_force_disable(struct regulator *regulator)
  1352. {
  1353. struct regulator_dev *rdev = regulator->rdev;
  1354. int ret;
  1355. mutex_lock(&rdev->mutex);
  1356. regulator->uA_load = 0;
  1357. ret = _regulator_force_disable(regulator->rdev);
  1358. mutex_unlock(&rdev->mutex);
  1359. if (rdev->supply)
  1360. while (rdev->open_count--)
  1361. regulator_disable(rdev->supply);
  1362. return ret;
  1363. }
  1364. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1365. static void regulator_disable_work(struct work_struct *work)
  1366. {
  1367. struct regulator_dev *rdev = container_of(work, struct regulator_dev,
  1368. disable_work.work);
  1369. int count, i, ret;
  1370. mutex_lock(&rdev->mutex);
  1371. BUG_ON(!rdev->deferred_disables);
  1372. count = rdev->deferred_disables;
  1373. rdev->deferred_disables = 0;
  1374. for (i = 0; i < count; i++) {
  1375. ret = _regulator_disable(rdev);
  1376. if (ret != 0)
  1377. rdev_err(rdev, "Deferred disable failed: %d\n", ret);
  1378. }
  1379. mutex_unlock(&rdev->mutex);
  1380. if (rdev->supply) {
  1381. for (i = 0; i < count; i++) {
  1382. ret = regulator_disable(rdev->supply);
  1383. if (ret != 0) {
  1384. rdev_err(rdev,
  1385. "Supply disable failed: %d\n", ret);
  1386. }
  1387. }
  1388. }
  1389. }
  1390. /**
  1391. * regulator_disable_deferred - disable regulator output with delay
  1392. * @regulator: regulator source
  1393. * @ms: miliseconds until the regulator is disabled
  1394. *
  1395. * Execute regulator_disable() on the regulator after a delay. This
  1396. * is intended for use with devices that require some time to quiesce.
  1397. *
  1398. * NOTE: this will only disable the regulator output if no other consumer
  1399. * devices have it enabled, the regulator device supports disabling and
  1400. * machine constraints permit this operation.
  1401. */
  1402. int regulator_disable_deferred(struct regulator *regulator, int ms)
  1403. {
  1404. struct regulator_dev *rdev = regulator->rdev;
  1405. int ret;
  1406. mutex_lock(&rdev->mutex);
  1407. rdev->deferred_disables++;
  1408. mutex_unlock(&rdev->mutex);
  1409. ret = schedule_delayed_work(&rdev->disable_work,
  1410. msecs_to_jiffies(ms));
  1411. if (ret < 0)
  1412. return ret;
  1413. else
  1414. return 0;
  1415. }
  1416. EXPORT_SYMBOL_GPL(regulator_disable_deferred);
  1417. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1418. {
  1419. /* If we don't know then assume that the regulator is always on */
  1420. if (!rdev->desc->ops->is_enabled)
  1421. return 1;
  1422. return rdev->desc->ops->is_enabled(rdev);
  1423. }
  1424. /**
  1425. * regulator_is_enabled - is the regulator output enabled
  1426. * @regulator: regulator source
  1427. *
  1428. * Returns positive if the regulator driver backing the source/client
  1429. * has requested that the device be enabled, zero if it hasn't, else a
  1430. * negative errno code.
  1431. *
  1432. * Note that the device backing this regulator handle can have multiple
  1433. * users, so it might be enabled even if regulator_enable() was never
  1434. * called for this particular source.
  1435. */
  1436. int regulator_is_enabled(struct regulator *regulator)
  1437. {
  1438. int ret;
  1439. mutex_lock(&regulator->rdev->mutex);
  1440. ret = _regulator_is_enabled(regulator->rdev);
  1441. mutex_unlock(&regulator->rdev->mutex);
  1442. return ret;
  1443. }
  1444. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1445. /**
  1446. * regulator_count_voltages - count regulator_list_voltage() selectors
  1447. * @regulator: regulator source
  1448. *
  1449. * Returns number of selectors, or negative errno. Selectors are
  1450. * numbered starting at zero, and typically correspond to bitfields
  1451. * in hardware registers.
  1452. */
  1453. int regulator_count_voltages(struct regulator *regulator)
  1454. {
  1455. struct regulator_dev *rdev = regulator->rdev;
  1456. return rdev->desc->n_voltages ? : -EINVAL;
  1457. }
  1458. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1459. /**
  1460. * regulator_list_voltage - enumerate supported voltages
  1461. * @regulator: regulator source
  1462. * @selector: identify voltage to list
  1463. * Context: can sleep
  1464. *
  1465. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1466. * zero if this selector code can't be used on this system, or a
  1467. * negative errno.
  1468. */
  1469. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1470. {
  1471. struct regulator_dev *rdev = regulator->rdev;
  1472. struct regulator_ops *ops = rdev->desc->ops;
  1473. int ret;
  1474. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1475. return -EINVAL;
  1476. mutex_lock(&rdev->mutex);
  1477. ret = ops->list_voltage(rdev, selector);
  1478. mutex_unlock(&rdev->mutex);
  1479. if (ret > 0) {
  1480. if (ret < rdev->constraints->min_uV)
  1481. ret = 0;
  1482. else if (ret > rdev->constraints->max_uV)
  1483. ret = 0;
  1484. }
  1485. return ret;
  1486. }
  1487. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1488. /**
  1489. * regulator_is_supported_voltage - check if a voltage range can be supported
  1490. *
  1491. * @regulator: Regulator to check.
  1492. * @min_uV: Minimum required voltage in uV.
  1493. * @max_uV: Maximum required voltage in uV.
  1494. *
  1495. * Returns a boolean or a negative error code.
  1496. */
  1497. int regulator_is_supported_voltage(struct regulator *regulator,
  1498. int min_uV, int max_uV)
  1499. {
  1500. int i, voltages, ret;
  1501. ret = regulator_count_voltages(regulator);
  1502. if (ret < 0)
  1503. return ret;
  1504. voltages = ret;
  1505. for (i = 0; i < voltages; i++) {
  1506. ret = regulator_list_voltage(regulator, i);
  1507. if (ret >= min_uV && ret <= max_uV)
  1508. return 1;
  1509. }
  1510. return 0;
  1511. }
  1512. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1513. int min_uV, int max_uV)
  1514. {
  1515. int ret;
  1516. int delay = 0;
  1517. unsigned int selector;
  1518. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1519. min_uV += rdev->constraints->uV_offset;
  1520. max_uV += rdev->constraints->uV_offset;
  1521. if (rdev->desc->ops->set_voltage) {
  1522. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1523. &selector);
  1524. if (rdev->desc->ops->list_voltage)
  1525. selector = rdev->desc->ops->list_voltage(rdev,
  1526. selector);
  1527. else
  1528. selector = -1;
  1529. } else if (rdev->desc->ops->set_voltage_sel) {
  1530. int best_val = INT_MAX;
  1531. int i;
  1532. selector = 0;
  1533. /* Find the smallest voltage that falls within the specified
  1534. * range.
  1535. */
  1536. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1537. ret = rdev->desc->ops->list_voltage(rdev, i);
  1538. if (ret < 0)
  1539. continue;
  1540. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1541. best_val = ret;
  1542. selector = i;
  1543. }
  1544. }
  1545. /*
  1546. * If we can't obtain the old selector there is not enough
  1547. * info to call set_voltage_time_sel().
  1548. */
  1549. if (rdev->desc->ops->set_voltage_time_sel &&
  1550. rdev->desc->ops->get_voltage_sel) {
  1551. unsigned int old_selector = 0;
  1552. ret = rdev->desc->ops->get_voltage_sel(rdev);
  1553. if (ret < 0)
  1554. return ret;
  1555. old_selector = ret;
  1556. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  1557. old_selector, selector);
  1558. }
  1559. if (best_val != INT_MAX) {
  1560. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  1561. selector = best_val;
  1562. } else {
  1563. ret = -EINVAL;
  1564. }
  1565. } else {
  1566. ret = -EINVAL;
  1567. }
  1568. /* Insert any necessary delays */
  1569. if (delay >= 1000) {
  1570. mdelay(delay / 1000);
  1571. udelay(delay % 1000);
  1572. } else if (delay) {
  1573. udelay(delay);
  1574. }
  1575. if (ret == 0)
  1576. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1577. NULL);
  1578. trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
  1579. return ret;
  1580. }
  1581. /**
  1582. * regulator_set_voltage - set regulator output voltage
  1583. * @regulator: regulator source
  1584. * @min_uV: Minimum required voltage in uV
  1585. * @max_uV: Maximum acceptable voltage in uV
  1586. *
  1587. * Sets a voltage regulator to the desired output voltage. This can be set
  1588. * during any regulator state. IOW, regulator can be disabled or enabled.
  1589. *
  1590. * If the regulator is enabled then the voltage will change to the new value
  1591. * immediately otherwise if the regulator is disabled the regulator will
  1592. * output at the new voltage when enabled.
  1593. *
  1594. * NOTE: If the regulator is shared between several devices then the lowest
  1595. * request voltage that meets the system constraints will be used.
  1596. * Regulator system constraints must be set for this regulator before
  1597. * calling this function otherwise this call will fail.
  1598. */
  1599. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1600. {
  1601. struct regulator_dev *rdev = regulator->rdev;
  1602. int ret = 0;
  1603. mutex_lock(&rdev->mutex);
  1604. /* If we're setting the same range as last time the change
  1605. * should be a noop (some cpufreq implementations use the same
  1606. * voltage for multiple frequencies, for example).
  1607. */
  1608. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1609. goto out;
  1610. /* sanity check */
  1611. if (!rdev->desc->ops->set_voltage &&
  1612. !rdev->desc->ops->set_voltage_sel) {
  1613. ret = -EINVAL;
  1614. goto out;
  1615. }
  1616. /* constraints check */
  1617. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1618. if (ret < 0)
  1619. goto out;
  1620. regulator->min_uV = min_uV;
  1621. regulator->max_uV = max_uV;
  1622. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1623. if (ret < 0)
  1624. goto out;
  1625. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1626. out:
  1627. mutex_unlock(&rdev->mutex);
  1628. return ret;
  1629. }
  1630. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1631. /**
  1632. * regulator_set_voltage_time - get raise/fall time
  1633. * @regulator: regulator source
  1634. * @old_uV: starting voltage in microvolts
  1635. * @new_uV: target voltage in microvolts
  1636. *
  1637. * Provided with the starting and ending voltage, this function attempts to
  1638. * calculate the time in microseconds required to rise or fall to this new
  1639. * voltage.
  1640. */
  1641. int regulator_set_voltage_time(struct regulator *regulator,
  1642. int old_uV, int new_uV)
  1643. {
  1644. struct regulator_dev *rdev = regulator->rdev;
  1645. struct regulator_ops *ops = rdev->desc->ops;
  1646. int old_sel = -1;
  1647. int new_sel = -1;
  1648. int voltage;
  1649. int i;
  1650. /* Currently requires operations to do this */
  1651. if (!ops->list_voltage || !ops->set_voltage_time_sel
  1652. || !rdev->desc->n_voltages)
  1653. return -EINVAL;
  1654. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1655. /* We only look for exact voltage matches here */
  1656. voltage = regulator_list_voltage(regulator, i);
  1657. if (voltage < 0)
  1658. return -EINVAL;
  1659. if (voltage == 0)
  1660. continue;
  1661. if (voltage == old_uV)
  1662. old_sel = i;
  1663. if (voltage == new_uV)
  1664. new_sel = i;
  1665. }
  1666. if (old_sel < 0 || new_sel < 0)
  1667. return -EINVAL;
  1668. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  1669. }
  1670. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  1671. /**
  1672. * regulator_sync_voltage - re-apply last regulator output voltage
  1673. * @regulator: regulator source
  1674. *
  1675. * Re-apply the last configured voltage. This is intended to be used
  1676. * where some external control source the consumer is cooperating with
  1677. * has caused the configured voltage to change.
  1678. */
  1679. int regulator_sync_voltage(struct regulator *regulator)
  1680. {
  1681. struct regulator_dev *rdev = regulator->rdev;
  1682. int ret, min_uV, max_uV;
  1683. mutex_lock(&rdev->mutex);
  1684. if (!rdev->desc->ops->set_voltage &&
  1685. !rdev->desc->ops->set_voltage_sel) {
  1686. ret = -EINVAL;
  1687. goto out;
  1688. }
  1689. /* This is only going to work if we've had a voltage configured. */
  1690. if (!regulator->min_uV && !regulator->max_uV) {
  1691. ret = -EINVAL;
  1692. goto out;
  1693. }
  1694. min_uV = regulator->min_uV;
  1695. max_uV = regulator->max_uV;
  1696. /* This should be a paranoia check... */
  1697. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1698. if (ret < 0)
  1699. goto out;
  1700. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1701. if (ret < 0)
  1702. goto out;
  1703. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1704. out:
  1705. mutex_unlock(&rdev->mutex);
  1706. return ret;
  1707. }
  1708. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  1709. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1710. {
  1711. int sel, ret;
  1712. if (rdev->desc->ops->get_voltage_sel) {
  1713. sel = rdev->desc->ops->get_voltage_sel(rdev);
  1714. if (sel < 0)
  1715. return sel;
  1716. ret = rdev->desc->ops->list_voltage(rdev, sel);
  1717. } else if (rdev->desc->ops->get_voltage) {
  1718. ret = rdev->desc->ops->get_voltage(rdev);
  1719. } else {
  1720. return -EINVAL;
  1721. }
  1722. if (ret < 0)
  1723. return ret;
  1724. return ret - rdev->constraints->uV_offset;
  1725. }
  1726. /**
  1727. * regulator_get_voltage - get regulator output voltage
  1728. * @regulator: regulator source
  1729. *
  1730. * This returns the current regulator voltage in uV.
  1731. *
  1732. * NOTE: If the regulator is disabled it will return the voltage value. This
  1733. * function should not be used to determine regulator state.
  1734. */
  1735. int regulator_get_voltage(struct regulator *regulator)
  1736. {
  1737. int ret;
  1738. mutex_lock(&regulator->rdev->mutex);
  1739. ret = _regulator_get_voltage(regulator->rdev);
  1740. mutex_unlock(&regulator->rdev->mutex);
  1741. return ret;
  1742. }
  1743. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1744. /**
  1745. * regulator_set_current_limit - set regulator output current limit
  1746. * @regulator: regulator source
  1747. * @min_uA: Minimuum supported current in uA
  1748. * @max_uA: Maximum supported current in uA
  1749. *
  1750. * Sets current sink to the desired output current. This can be set during
  1751. * any regulator state. IOW, regulator can be disabled or enabled.
  1752. *
  1753. * If the regulator is enabled then the current will change to the new value
  1754. * immediately otherwise if the regulator is disabled the regulator will
  1755. * output at the new current when enabled.
  1756. *
  1757. * NOTE: Regulator system constraints must be set for this regulator before
  1758. * calling this function otherwise this call will fail.
  1759. */
  1760. int regulator_set_current_limit(struct regulator *regulator,
  1761. int min_uA, int max_uA)
  1762. {
  1763. struct regulator_dev *rdev = regulator->rdev;
  1764. int ret;
  1765. mutex_lock(&rdev->mutex);
  1766. /* sanity check */
  1767. if (!rdev->desc->ops->set_current_limit) {
  1768. ret = -EINVAL;
  1769. goto out;
  1770. }
  1771. /* constraints check */
  1772. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1773. if (ret < 0)
  1774. goto out;
  1775. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1776. out:
  1777. mutex_unlock(&rdev->mutex);
  1778. return ret;
  1779. }
  1780. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1781. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1782. {
  1783. int ret;
  1784. mutex_lock(&rdev->mutex);
  1785. /* sanity check */
  1786. if (!rdev->desc->ops->get_current_limit) {
  1787. ret = -EINVAL;
  1788. goto out;
  1789. }
  1790. ret = rdev->desc->ops->get_current_limit(rdev);
  1791. out:
  1792. mutex_unlock(&rdev->mutex);
  1793. return ret;
  1794. }
  1795. /**
  1796. * regulator_get_current_limit - get regulator output current
  1797. * @regulator: regulator source
  1798. *
  1799. * This returns the current supplied by the specified current sink in uA.
  1800. *
  1801. * NOTE: If the regulator is disabled it will return the current value. This
  1802. * function should not be used to determine regulator state.
  1803. */
  1804. int regulator_get_current_limit(struct regulator *regulator)
  1805. {
  1806. return _regulator_get_current_limit(regulator->rdev);
  1807. }
  1808. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1809. /**
  1810. * regulator_set_mode - set regulator operating mode
  1811. * @regulator: regulator source
  1812. * @mode: operating mode - one of the REGULATOR_MODE constants
  1813. *
  1814. * Set regulator operating mode to increase regulator efficiency or improve
  1815. * regulation performance.
  1816. *
  1817. * NOTE: Regulator system constraints must be set for this regulator before
  1818. * calling this function otherwise this call will fail.
  1819. */
  1820. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1821. {
  1822. struct regulator_dev *rdev = regulator->rdev;
  1823. int ret;
  1824. int regulator_curr_mode;
  1825. mutex_lock(&rdev->mutex);
  1826. /* sanity check */
  1827. if (!rdev->desc->ops->set_mode) {
  1828. ret = -EINVAL;
  1829. goto out;
  1830. }
  1831. /* return if the same mode is requested */
  1832. if (rdev->desc->ops->get_mode) {
  1833. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1834. if (regulator_curr_mode == mode) {
  1835. ret = 0;
  1836. goto out;
  1837. }
  1838. }
  1839. /* constraints check */
  1840. ret = regulator_mode_constrain(rdev, &mode);
  1841. if (ret < 0)
  1842. goto out;
  1843. ret = rdev->desc->ops->set_mode(rdev, mode);
  1844. out:
  1845. mutex_unlock(&rdev->mutex);
  1846. return ret;
  1847. }
  1848. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1849. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1850. {
  1851. int ret;
  1852. mutex_lock(&rdev->mutex);
  1853. /* sanity check */
  1854. if (!rdev->desc->ops->get_mode) {
  1855. ret = -EINVAL;
  1856. goto out;
  1857. }
  1858. ret = rdev->desc->ops->get_mode(rdev);
  1859. out:
  1860. mutex_unlock(&rdev->mutex);
  1861. return ret;
  1862. }
  1863. /**
  1864. * regulator_get_mode - get regulator operating mode
  1865. * @regulator: regulator source
  1866. *
  1867. * Get the current regulator operating mode.
  1868. */
  1869. unsigned int regulator_get_mode(struct regulator *regulator)
  1870. {
  1871. return _regulator_get_mode(regulator->rdev);
  1872. }
  1873. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1874. /**
  1875. * regulator_set_optimum_mode - set regulator optimum operating mode
  1876. * @regulator: regulator source
  1877. * @uA_load: load current
  1878. *
  1879. * Notifies the regulator core of a new device load. This is then used by
  1880. * DRMS (if enabled by constraints) to set the most efficient regulator
  1881. * operating mode for the new regulator loading.
  1882. *
  1883. * Consumer devices notify their supply regulator of the maximum power
  1884. * they will require (can be taken from device datasheet in the power
  1885. * consumption tables) when they change operational status and hence power
  1886. * state. Examples of operational state changes that can affect power
  1887. * consumption are :-
  1888. *
  1889. * o Device is opened / closed.
  1890. * o Device I/O is about to begin or has just finished.
  1891. * o Device is idling in between work.
  1892. *
  1893. * This information is also exported via sysfs to userspace.
  1894. *
  1895. * DRMS will sum the total requested load on the regulator and change
  1896. * to the most efficient operating mode if platform constraints allow.
  1897. *
  1898. * Returns the new regulator mode or error.
  1899. */
  1900. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1901. {
  1902. struct regulator_dev *rdev = regulator->rdev;
  1903. struct regulator *consumer;
  1904. int ret, output_uV, input_uV, total_uA_load = 0;
  1905. unsigned int mode;
  1906. mutex_lock(&rdev->mutex);
  1907. /*
  1908. * first check to see if we can set modes at all, otherwise just
  1909. * tell the consumer everything is OK.
  1910. */
  1911. regulator->uA_load = uA_load;
  1912. ret = regulator_check_drms(rdev);
  1913. if (ret < 0) {
  1914. ret = 0;
  1915. goto out;
  1916. }
  1917. if (!rdev->desc->ops->get_optimum_mode)
  1918. goto out;
  1919. /*
  1920. * we can actually do this so any errors are indicators of
  1921. * potential real failure.
  1922. */
  1923. ret = -EINVAL;
  1924. /* get output voltage */
  1925. output_uV = _regulator_get_voltage(rdev);
  1926. if (output_uV <= 0) {
  1927. rdev_err(rdev, "invalid output voltage found\n");
  1928. goto out;
  1929. }
  1930. /* get input voltage */
  1931. input_uV = 0;
  1932. if (rdev->supply)
  1933. input_uV = regulator_get_voltage(rdev->supply);
  1934. if (input_uV <= 0)
  1935. input_uV = rdev->constraints->input_uV;
  1936. if (input_uV <= 0) {
  1937. rdev_err(rdev, "invalid input voltage found\n");
  1938. goto out;
  1939. }
  1940. /* calc total requested load for this regulator */
  1941. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1942. total_uA_load += consumer->uA_load;
  1943. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1944. input_uV, output_uV,
  1945. total_uA_load);
  1946. ret = regulator_mode_constrain(rdev, &mode);
  1947. if (ret < 0) {
  1948. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  1949. total_uA_load, input_uV, output_uV);
  1950. goto out;
  1951. }
  1952. ret = rdev->desc->ops->set_mode(rdev, mode);
  1953. if (ret < 0) {
  1954. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  1955. goto out;
  1956. }
  1957. ret = mode;
  1958. out:
  1959. mutex_unlock(&rdev->mutex);
  1960. return ret;
  1961. }
  1962. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1963. /**
  1964. * regulator_register_notifier - register regulator event notifier
  1965. * @regulator: regulator source
  1966. * @nb: notifier block
  1967. *
  1968. * Register notifier block to receive regulator events.
  1969. */
  1970. int regulator_register_notifier(struct regulator *regulator,
  1971. struct notifier_block *nb)
  1972. {
  1973. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1974. nb);
  1975. }
  1976. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1977. /**
  1978. * regulator_unregister_notifier - unregister regulator event notifier
  1979. * @regulator: regulator source
  1980. * @nb: notifier block
  1981. *
  1982. * Unregister regulator event notifier block.
  1983. */
  1984. int regulator_unregister_notifier(struct regulator *regulator,
  1985. struct notifier_block *nb)
  1986. {
  1987. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1988. nb);
  1989. }
  1990. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1991. /* notify regulator consumers and downstream regulator consumers.
  1992. * Note mutex must be held by caller.
  1993. */
  1994. static void _notifier_call_chain(struct regulator_dev *rdev,
  1995. unsigned long event, void *data)
  1996. {
  1997. /* call rdev chain first */
  1998. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1999. }
  2000. /**
  2001. * regulator_bulk_get - get multiple regulator consumers
  2002. *
  2003. * @dev: Device to supply
  2004. * @num_consumers: Number of consumers to register
  2005. * @consumers: Configuration of consumers; clients are stored here.
  2006. *
  2007. * @return 0 on success, an errno on failure.
  2008. *
  2009. * This helper function allows drivers to get several regulator
  2010. * consumers in one operation. If any of the regulators cannot be
  2011. * acquired then any regulators that were allocated will be freed
  2012. * before returning to the caller.
  2013. */
  2014. int regulator_bulk_get(struct device *dev, int num_consumers,
  2015. struct regulator_bulk_data *consumers)
  2016. {
  2017. int i;
  2018. int ret;
  2019. for (i = 0; i < num_consumers; i++)
  2020. consumers[i].consumer = NULL;
  2021. for (i = 0; i < num_consumers; i++) {
  2022. consumers[i].consumer = regulator_get(dev,
  2023. consumers[i].supply);
  2024. if (IS_ERR(consumers[i].consumer)) {
  2025. ret = PTR_ERR(consumers[i].consumer);
  2026. dev_err(dev, "Failed to get supply '%s': %d\n",
  2027. consumers[i].supply, ret);
  2028. consumers[i].consumer = NULL;
  2029. goto err;
  2030. }
  2031. }
  2032. return 0;
  2033. err:
  2034. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  2035. regulator_put(consumers[i].consumer);
  2036. return ret;
  2037. }
  2038. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  2039. static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
  2040. {
  2041. struct regulator_bulk_data *bulk = data;
  2042. bulk->ret = regulator_enable(bulk->consumer);
  2043. }
  2044. /**
  2045. * regulator_bulk_enable - enable multiple regulator consumers
  2046. *
  2047. * @num_consumers: Number of consumers
  2048. * @consumers: Consumer data; clients are stored here.
  2049. * @return 0 on success, an errno on failure
  2050. *
  2051. * This convenience API allows consumers to enable multiple regulator
  2052. * clients in a single API call. If any consumers cannot be enabled
  2053. * then any others that were enabled will be disabled again prior to
  2054. * return.
  2055. */
  2056. int regulator_bulk_enable(int num_consumers,
  2057. struct regulator_bulk_data *consumers)
  2058. {
  2059. LIST_HEAD(async_domain);
  2060. int i;
  2061. int ret = 0;
  2062. for (i = 0; i < num_consumers; i++)
  2063. async_schedule_domain(regulator_bulk_enable_async,
  2064. &consumers[i], &async_domain);
  2065. async_synchronize_full_domain(&async_domain);
  2066. /* If any consumer failed we need to unwind any that succeeded */
  2067. for (i = 0; i < num_consumers; i++) {
  2068. if (consumers[i].ret != 0) {
  2069. ret = consumers[i].ret;
  2070. goto err;
  2071. }
  2072. }
  2073. return 0;
  2074. err:
  2075. for (i = 0; i < num_consumers; i++)
  2076. if (consumers[i].ret == 0)
  2077. regulator_disable(consumers[i].consumer);
  2078. else
  2079. pr_err("Failed to enable %s: %d\n",
  2080. consumers[i].supply, consumers[i].ret);
  2081. return ret;
  2082. }
  2083. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  2084. /**
  2085. * regulator_bulk_disable - disable multiple regulator consumers
  2086. *
  2087. * @num_consumers: Number of consumers
  2088. * @consumers: Consumer data; clients are stored here.
  2089. * @return 0 on success, an errno on failure
  2090. *
  2091. * This convenience API allows consumers to disable multiple regulator
  2092. * clients in a single API call. If any consumers cannot be enabled
  2093. * then any others that were disabled will be disabled again prior to
  2094. * return.
  2095. */
  2096. int regulator_bulk_disable(int num_consumers,
  2097. struct regulator_bulk_data *consumers)
  2098. {
  2099. int i;
  2100. int ret;
  2101. for (i = 0; i < num_consumers; i++) {
  2102. ret = regulator_disable(consumers[i].consumer);
  2103. if (ret != 0)
  2104. goto err;
  2105. }
  2106. return 0;
  2107. err:
  2108. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  2109. for (--i; i >= 0; --i)
  2110. regulator_enable(consumers[i].consumer);
  2111. return ret;
  2112. }
  2113. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  2114. /**
  2115. * regulator_bulk_free - free multiple regulator consumers
  2116. *
  2117. * @num_consumers: Number of consumers
  2118. * @consumers: Consumer data; clients are stored here.
  2119. *
  2120. * This convenience API allows consumers to free multiple regulator
  2121. * clients in a single API call.
  2122. */
  2123. void regulator_bulk_free(int num_consumers,
  2124. struct regulator_bulk_data *consumers)
  2125. {
  2126. int i;
  2127. for (i = 0; i < num_consumers; i++) {
  2128. regulator_put(consumers[i].consumer);
  2129. consumers[i].consumer = NULL;
  2130. }
  2131. }
  2132. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2133. /**
  2134. * regulator_notifier_call_chain - call regulator event notifier
  2135. * @rdev: regulator source
  2136. * @event: notifier block
  2137. * @data: callback-specific data.
  2138. *
  2139. * Called by regulator drivers to notify clients a regulator event has
  2140. * occurred. We also notify regulator clients downstream.
  2141. * Note lock must be held by caller.
  2142. */
  2143. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2144. unsigned long event, void *data)
  2145. {
  2146. _notifier_call_chain(rdev, event, data);
  2147. return NOTIFY_DONE;
  2148. }
  2149. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2150. /**
  2151. * regulator_mode_to_status - convert a regulator mode into a status
  2152. *
  2153. * @mode: Mode to convert
  2154. *
  2155. * Convert a regulator mode into a status.
  2156. */
  2157. int regulator_mode_to_status(unsigned int mode)
  2158. {
  2159. switch (mode) {
  2160. case REGULATOR_MODE_FAST:
  2161. return REGULATOR_STATUS_FAST;
  2162. case REGULATOR_MODE_NORMAL:
  2163. return REGULATOR_STATUS_NORMAL;
  2164. case REGULATOR_MODE_IDLE:
  2165. return REGULATOR_STATUS_IDLE;
  2166. case REGULATOR_STATUS_STANDBY:
  2167. return REGULATOR_STATUS_STANDBY;
  2168. default:
  2169. return 0;
  2170. }
  2171. }
  2172. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2173. /*
  2174. * To avoid cluttering sysfs (and memory) with useless state, only
  2175. * create attributes that can be meaningfully displayed.
  2176. */
  2177. static int add_regulator_attributes(struct regulator_dev *rdev)
  2178. {
  2179. struct device *dev = &rdev->dev;
  2180. struct regulator_ops *ops = rdev->desc->ops;
  2181. int status = 0;
  2182. /* some attributes need specific methods to be displayed */
  2183. if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
  2184. (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
  2185. status = device_create_file(dev, &dev_attr_microvolts);
  2186. if (status < 0)
  2187. return status;
  2188. }
  2189. if (ops->get_current_limit) {
  2190. status = device_create_file(dev, &dev_attr_microamps);
  2191. if (status < 0)
  2192. return status;
  2193. }
  2194. if (ops->get_mode) {
  2195. status = device_create_file(dev, &dev_attr_opmode);
  2196. if (status < 0)
  2197. return status;
  2198. }
  2199. if (ops->is_enabled) {
  2200. status = device_create_file(dev, &dev_attr_state);
  2201. if (status < 0)
  2202. return status;
  2203. }
  2204. if (ops->get_status) {
  2205. status = device_create_file(dev, &dev_attr_status);
  2206. if (status < 0)
  2207. return status;
  2208. }
  2209. /* some attributes are type-specific */
  2210. if (rdev->desc->type == REGULATOR_CURRENT) {
  2211. status = device_create_file(dev, &dev_attr_requested_microamps);
  2212. if (status < 0)
  2213. return status;
  2214. }
  2215. /* all the other attributes exist to support constraints;
  2216. * don't show them if there are no constraints, or if the
  2217. * relevant supporting methods are missing.
  2218. */
  2219. if (!rdev->constraints)
  2220. return status;
  2221. /* constraints need specific supporting methods */
  2222. if (ops->set_voltage || ops->set_voltage_sel) {
  2223. status = device_create_file(dev, &dev_attr_min_microvolts);
  2224. if (status < 0)
  2225. return status;
  2226. status = device_create_file(dev, &dev_attr_max_microvolts);
  2227. if (status < 0)
  2228. return status;
  2229. }
  2230. if (ops->set_current_limit) {
  2231. status = device_create_file(dev, &dev_attr_min_microamps);
  2232. if (status < 0)
  2233. return status;
  2234. status = device_create_file(dev, &dev_attr_max_microamps);
  2235. if (status < 0)
  2236. return status;
  2237. }
  2238. /* suspend mode constraints need multiple supporting methods */
  2239. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  2240. return status;
  2241. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2242. if (status < 0)
  2243. return status;
  2244. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2245. if (status < 0)
  2246. return status;
  2247. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2248. if (status < 0)
  2249. return status;
  2250. if (ops->set_suspend_voltage) {
  2251. status = device_create_file(dev,
  2252. &dev_attr_suspend_standby_microvolts);
  2253. if (status < 0)
  2254. return status;
  2255. status = device_create_file(dev,
  2256. &dev_attr_suspend_mem_microvolts);
  2257. if (status < 0)
  2258. return status;
  2259. status = device_create_file(dev,
  2260. &dev_attr_suspend_disk_microvolts);
  2261. if (status < 0)
  2262. return status;
  2263. }
  2264. if (ops->set_suspend_mode) {
  2265. status = device_create_file(dev,
  2266. &dev_attr_suspend_standby_mode);
  2267. if (status < 0)
  2268. return status;
  2269. status = device_create_file(dev,
  2270. &dev_attr_suspend_mem_mode);
  2271. if (status < 0)
  2272. return status;
  2273. status = device_create_file(dev,
  2274. &dev_attr_suspend_disk_mode);
  2275. if (status < 0)
  2276. return status;
  2277. }
  2278. return status;
  2279. }
  2280. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2281. {
  2282. #ifdef CONFIG_DEBUG_FS
  2283. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2284. if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
  2285. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2286. rdev->debugfs = NULL;
  2287. return;
  2288. }
  2289. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2290. &rdev->use_count);
  2291. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2292. &rdev->open_count);
  2293. #endif
  2294. }
  2295. /**
  2296. * regulator_register - register regulator
  2297. * @regulator_desc: regulator to register
  2298. * @dev: struct device for the regulator
  2299. * @init_data: platform provided init data, passed through by driver
  2300. * @driver_data: private regulator data
  2301. *
  2302. * Called by regulator drivers to register a regulator.
  2303. * Returns 0 on success.
  2304. */
  2305. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  2306. struct device *dev, const struct regulator_init_data *init_data,
  2307. void *driver_data, struct device_node *of_node)
  2308. {
  2309. static atomic_t regulator_no = ATOMIC_INIT(0);
  2310. struct regulator_dev *rdev;
  2311. int ret, i;
  2312. const char *supply = NULL;
  2313. if (regulator_desc == NULL)
  2314. return ERR_PTR(-EINVAL);
  2315. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2316. return ERR_PTR(-EINVAL);
  2317. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2318. regulator_desc->type != REGULATOR_CURRENT)
  2319. return ERR_PTR(-EINVAL);
  2320. if (!init_data)
  2321. return ERR_PTR(-EINVAL);
  2322. /* Only one of each should be implemented */
  2323. WARN_ON(regulator_desc->ops->get_voltage &&
  2324. regulator_desc->ops->get_voltage_sel);
  2325. WARN_ON(regulator_desc->ops->set_voltage &&
  2326. regulator_desc->ops->set_voltage_sel);
  2327. /* If we're using selectors we must implement list_voltage. */
  2328. if (regulator_desc->ops->get_voltage_sel &&
  2329. !regulator_desc->ops->list_voltage) {
  2330. return ERR_PTR(-EINVAL);
  2331. }
  2332. if (regulator_desc->ops->set_voltage_sel &&
  2333. !regulator_desc->ops->list_voltage) {
  2334. return ERR_PTR(-EINVAL);
  2335. }
  2336. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2337. if (rdev == NULL)
  2338. return ERR_PTR(-ENOMEM);
  2339. mutex_lock(&regulator_list_mutex);
  2340. mutex_init(&rdev->mutex);
  2341. rdev->reg_data = driver_data;
  2342. rdev->owner = regulator_desc->owner;
  2343. rdev->desc = regulator_desc;
  2344. INIT_LIST_HEAD(&rdev->consumer_list);
  2345. INIT_LIST_HEAD(&rdev->list);
  2346. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2347. INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
  2348. /* preform any regulator specific init */
  2349. if (init_data->regulator_init) {
  2350. ret = init_data->regulator_init(rdev->reg_data);
  2351. if (ret < 0)
  2352. goto clean;
  2353. }
  2354. /* register with sysfs */
  2355. rdev->dev.class = &regulator_class;
  2356. rdev->dev.of_node = of_node;
  2357. rdev->dev.parent = dev;
  2358. dev_set_name(&rdev->dev, "regulator.%d",
  2359. atomic_inc_return(&regulator_no) - 1);
  2360. ret = device_register(&rdev->dev);
  2361. if (ret != 0) {
  2362. put_device(&rdev->dev);
  2363. goto clean;
  2364. }
  2365. dev_set_drvdata(&rdev->dev, rdev);
  2366. /* set regulator constraints */
  2367. ret = set_machine_constraints(rdev, &init_data->constraints);
  2368. if (ret < 0)
  2369. goto scrub;
  2370. /* add attributes supported by this regulator */
  2371. ret = add_regulator_attributes(rdev);
  2372. if (ret < 0)
  2373. goto scrub;
  2374. if (init_data->supply_regulator)
  2375. supply = init_data->supply_regulator;
  2376. else if (regulator_desc->supply_name)
  2377. supply = regulator_desc->supply_name;
  2378. if (supply) {
  2379. struct regulator_dev *r;
  2380. r = regulator_dev_lookup(dev, supply);
  2381. if (!r) {
  2382. dev_err(dev, "Failed to find supply %s\n", supply);
  2383. ret = -ENODEV;
  2384. goto scrub;
  2385. }
  2386. ret = set_supply(rdev, r);
  2387. if (ret < 0)
  2388. goto scrub;
  2389. }
  2390. /* add consumers devices */
  2391. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2392. ret = set_consumer_device_supply(rdev,
  2393. init_data->consumer_supplies[i].dev,
  2394. init_data->consumer_supplies[i].dev_name,
  2395. init_data->consumer_supplies[i].supply);
  2396. if (ret < 0) {
  2397. dev_err(dev, "Failed to set supply %s\n",
  2398. init_data->consumer_supplies[i].supply);
  2399. goto unset_supplies;
  2400. }
  2401. }
  2402. list_add(&rdev->list, &regulator_list);
  2403. rdev_init_debugfs(rdev);
  2404. out:
  2405. mutex_unlock(&regulator_list_mutex);
  2406. return rdev;
  2407. unset_supplies:
  2408. unset_regulator_supplies(rdev);
  2409. scrub:
  2410. kfree(rdev->constraints);
  2411. device_unregister(&rdev->dev);
  2412. /* device core frees rdev */
  2413. rdev = ERR_PTR(ret);
  2414. goto out;
  2415. clean:
  2416. kfree(rdev);
  2417. rdev = ERR_PTR(ret);
  2418. goto out;
  2419. }
  2420. EXPORT_SYMBOL_GPL(regulator_register);
  2421. /**
  2422. * regulator_unregister - unregister regulator
  2423. * @rdev: regulator to unregister
  2424. *
  2425. * Called by regulator drivers to unregister a regulator.
  2426. */
  2427. void regulator_unregister(struct regulator_dev *rdev)
  2428. {
  2429. if (rdev == NULL)
  2430. return;
  2431. mutex_lock(&regulator_list_mutex);
  2432. #ifdef CONFIG_DEBUG_FS
  2433. debugfs_remove_recursive(rdev->debugfs);
  2434. #endif
  2435. flush_work_sync(&rdev->disable_work.work);
  2436. WARN_ON(rdev->open_count);
  2437. unset_regulator_supplies(rdev);
  2438. list_del(&rdev->list);
  2439. if (rdev->supply)
  2440. regulator_put(rdev->supply);
  2441. device_unregister(&rdev->dev);
  2442. kfree(rdev->constraints);
  2443. mutex_unlock(&regulator_list_mutex);
  2444. }
  2445. EXPORT_SYMBOL_GPL(regulator_unregister);
  2446. /**
  2447. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2448. * @state: system suspend state
  2449. *
  2450. * Configure each regulator with it's suspend operating parameters for state.
  2451. * This will usually be called by machine suspend code prior to supending.
  2452. */
  2453. int regulator_suspend_prepare(suspend_state_t state)
  2454. {
  2455. struct regulator_dev *rdev;
  2456. int ret = 0;
  2457. /* ON is handled by regulator active state */
  2458. if (state == PM_SUSPEND_ON)
  2459. return -EINVAL;
  2460. mutex_lock(&regulator_list_mutex);
  2461. list_for_each_entry(rdev, &regulator_list, list) {
  2462. mutex_lock(&rdev->mutex);
  2463. ret = suspend_prepare(rdev, state);
  2464. mutex_unlock(&rdev->mutex);
  2465. if (ret < 0) {
  2466. rdev_err(rdev, "failed to prepare\n");
  2467. goto out;
  2468. }
  2469. }
  2470. out:
  2471. mutex_unlock(&regulator_list_mutex);
  2472. return ret;
  2473. }
  2474. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2475. /**
  2476. * regulator_suspend_finish - resume regulators from system wide suspend
  2477. *
  2478. * Turn on regulators that might be turned off by regulator_suspend_prepare
  2479. * and that should be turned on according to the regulators properties.
  2480. */
  2481. int regulator_suspend_finish(void)
  2482. {
  2483. struct regulator_dev *rdev;
  2484. int ret = 0, error;
  2485. mutex_lock(&regulator_list_mutex);
  2486. list_for_each_entry(rdev, &regulator_list, list) {
  2487. struct regulator_ops *ops = rdev->desc->ops;
  2488. mutex_lock(&rdev->mutex);
  2489. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  2490. ops->enable) {
  2491. error = ops->enable(rdev);
  2492. if (error)
  2493. ret = error;
  2494. } else {
  2495. if (!has_full_constraints)
  2496. goto unlock;
  2497. if (!ops->disable)
  2498. goto unlock;
  2499. if (ops->is_enabled && !ops->is_enabled(rdev))
  2500. goto unlock;
  2501. error = ops->disable(rdev);
  2502. if (error)
  2503. ret = error;
  2504. }
  2505. unlock:
  2506. mutex_unlock(&rdev->mutex);
  2507. }
  2508. mutex_unlock(&regulator_list_mutex);
  2509. return ret;
  2510. }
  2511. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  2512. /**
  2513. * regulator_has_full_constraints - the system has fully specified constraints
  2514. *
  2515. * Calling this function will cause the regulator API to disable all
  2516. * regulators which have a zero use count and don't have an always_on
  2517. * constraint in a late_initcall.
  2518. *
  2519. * The intention is that this will become the default behaviour in a
  2520. * future kernel release so users are encouraged to use this facility
  2521. * now.
  2522. */
  2523. void regulator_has_full_constraints(void)
  2524. {
  2525. has_full_constraints = 1;
  2526. }
  2527. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2528. /**
  2529. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2530. *
  2531. * Calling this function will cause the regulator API to provide a
  2532. * dummy regulator to consumers if no physical regulator is found,
  2533. * allowing most consumers to proceed as though a regulator were
  2534. * configured. This allows systems such as those with software
  2535. * controllable regulators for the CPU core only to be brought up more
  2536. * readily.
  2537. */
  2538. void regulator_use_dummy_regulator(void)
  2539. {
  2540. board_wants_dummy_regulator = true;
  2541. }
  2542. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2543. /**
  2544. * rdev_get_drvdata - get rdev regulator driver data
  2545. * @rdev: regulator
  2546. *
  2547. * Get rdev regulator driver private data. This call can be used in the
  2548. * regulator driver context.
  2549. */
  2550. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2551. {
  2552. return rdev->reg_data;
  2553. }
  2554. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2555. /**
  2556. * regulator_get_drvdata - get regulator driver data
  2557. * @regulator: regulator
  2558. *
  2559. * Get regulator driver private data. This call can be used in the consumer
  2560. * driver context when non API regulator specific functions need to be called.
  2561. */
  2562. void *regulator_get_drvdata(struct regulator *regulator)
  2563. {
  2564. return regulator->rdev->reg_data;
  2565. }
  2566. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2567. /**
  2568. * regulator_set_drvdata - set regulator driver data
  2569. * @regulator: regulator
  2570. * @data: data
  2571. */
  2572. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2573. {
  2574. regulator->rdev->reg_data = data;
  2575. }
  2576. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2577. /**
  2578. * regulator_get_id - get regulator ID
  2579. * @rdev: regulator
  2580. */
  2581. int rdev_get_id(struct regulator_dev *rdev)
  2582. {
  2583. return rdev->desc->id;
  2584. }
  2585. EXPORT_SYMBOL_GPL(rdev_get_id);
  2586. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2587. {
  2588. return &rdev->dev;
  2589. }
  2590. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2591. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2592. {
  2593. return reg_init_data->driver_data;
  2594. }
  2595. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2596. #ifdef CONFIG_DEBUG_FS
  2597. static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
  2598. size_t count, loff_t *ppos)
  2599. {
  2600. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2601. ssize_t len, ret = 0;
  2602. struct regulator_map *map;
  2603. if (!buf)
  2604. return -ENOMEM;
  2605. list_for_each_entry(map, &regulator_map_list, list) {
  2606. len = snprintf(buf + ret, PAGE_SIZE - ret,
  2607. "%s -> %s.%s\n",
  2608. rdev_get_name(map->regulator), map->dev_name,
  2609. map->supply);
  2610. if (len >= 0)
  2611. ret += len;
  2612. if (ret > PAGE_SIZE) {
  2613. ret = PAGE_SIZE;
  2614. break;
  2615. }
  2616. }
  2617. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  2618. kfree(buf);
  2619. return ret;
  2620. }
  2621. static const struct file_operations supply_map_fops = {
  2622. .read = supply_map_read_file,
  2623. .llseek = default_llseek,
  2624. };
  2625. #endif
  2626. static int __init regulator_init(void)
  2627. {
  2628. int ret;
  2629. ret = class_register(&regulator_class);
  2630. #ifdef CONFIG_DEBUG_FS
  2631. debugfs_root = debugfs_create_dir("regulator", NULL);
  2632. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2633. pr_warn("regulator: Failed to create debugfs directory\n");
  2634. debugfs_root = NULL;
  2635. }
  2636. if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
  2637. NULL, &supply_map_fops)))
  2638. pr_warn("regulator: Failed to create supplies debugfs\n");
  2639. #endif
  2640. regulator_dummy_init();
  2641. return ret;
  2642. }
  2643. /* init early to allow our consumers to complete system booting */
  2644. core_initcall(regulator_init);
  2645. static int __init regulator_init_complete(void)
  2646. {
  2647. struct regulator_dev *rdev;
  2648. struct regulator_ops *ops;
  2649. struct regulation_constraints *c;
  2650. int enabled, ret;
  2651. mutex_lock(&regulator_list_mutex);
  2652. /* If we have a full configuration then disable any regulators
  2653. * which are not in use or always_on. This will become the
  2654. * default behaviour in the future.
  2655. */
  2656. list_for_each_entry(rdev, &regulator_list, list) {
  2657. ops = rdev->desc->ops;
  2658. c = rdev->constraints;
  2659. if (!ops->disable || (c && c->always_on))
  2660. continue;
  2661. mutex_lock(&rdev->mutex);
  2662. if (rdev->use_count)
  2663. goto unlock;
  2664. /* If we can't read the status assume it's on. */
  2665. if (ops->is_enabled)
  2666. enabled = ops->is_enabled(rdev);
  2667. else
  2668. enabled = 1;
  2669. if (!enabled)
  2670. goto unlock;
  2671. if (has_full_constraints) {
  2672. /* We log since this may kill the system if it
  2673. * goes wrong. */
  2674. rdev_info(rdev, "disabling\n");
  2675. ret = ops->disable(rdev);
  2676. if (ret != 0) {
  2677. rdev_err(rdev, "couldn't disable: %d\n", ret);
  2678. }
  2679. } else {
  2680. /* The intention is that in future we will
  2681. * assume that full constraints are provided
  2682. * so warn even if we aren't going to do
  2683. * anything here.
  2684. */
  2685. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  2686. }
  2687. unlock:
  2688. mutex_unlock(&rdev->mutex);
  2689. }
  2690. mutex_unlock(&regulator_list_mutex);
  2691. return 0;
  2692. }
  2693. late_initcall(regulator_init_complete);