core.c 67 KB

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