core.c 65 KB

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