core.c 78 KB

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