core.c 76 KB

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