core.c 79 KB

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