helpers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * helpers.c -- Voltage/Current Regulator framework helper functions.
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. * Copyright 2008 SlimLogic Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/module.h>
  20. /**
  21. * regulator_is_enabled_regmap - standard is_enabled() for regmap users
  22. *
  23. * @rdev: regulator to operate on
  24. *
  25. * Regulators that use regmap for their register I/O can set the
  26. * enable_reg and enable_mask fields in their descriptor and then use
  27. * this as their is_enabled operation, saving some code.
  28. */
  29. int regulator_is_enabled_regmap(struct regulator_dev *rdev)
  30. {
  31. unsigned int val;
  32. int ret;
  33. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  34. if (ret != 0)
  35. return ret;
  36. if (rdev->desc->enable_is_inverted)
  37. return (val & rdev->desc->enable_mask) == 0;
  38. else
  39. return (val & rdev->desc->enable_mask) != 0;
  40. }
  41. EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
  42. /**
  43. * regulator_enable_regmap - standard enable() for regmap users
  44. *
  45. * @rdev: regulator to operate on
  46. *
  47. * Regulators that use regmap for their register I/O can set the
  48. * enable_reg and enable_mask fields in their descriptor and then use
  49. * this as their enable() operation, saving some code.
  50. */
  51. int regulator_enable_regmap(struct regulator_dev *rdev)
  52. {
  53. unsigned int val;
  54. if (rdev->desc->enable_is_inverted)
  55. val = 0;
  56. else
  57. val = rdev->desc->enable_mask;
  58. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  59. rdev->desc->enable_mask, val);
  60. }
  61. EXPORT_SYMBOL_GPL(regulator_enable_regmap);
  62. /**
  63. * regulator_disable_regmap - standard disable() for regmap users
  64. *
  65. * @rdev: regulator to operate on
  66. *
  67. * Regulators that use regmap for their register I/O can set the
  68. * enable_reg and enable_mask fields in their descriptor and then use
  69. * this as their disable() operation, saving some code.
  70. */
  71. int regulator_disable_regmap(struct regulator_dev *rdev)
  72. {
  73. unsigned int val;
  74. if (rdev->desc->enable_is_inverted)
  75. val = rdev->desc->enable_mask;
  76. else
  77. val = 0;
  78. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  79. rdev->desc->enable_mask, val);
  80. }
  81. EXPORT_SYMBOL_GPL(regulator_disable_regmap);
  82. /**
  83. * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
  84. *
  85. * @rdev: regulator to operate on
  86. *
  87. * Regulators that use regmap for their register I/O can set the
  88. * vsel_reg and vsel_mask fields in their descriptor and then use this
  89. * as their get_voltage_vsel operation, saving some code.
  90. */
  91. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
  92. {
  93. unsigned int val;
  94. int ret;
  95. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  96. if (ret != 0)
  97. return ret;
  98. val &= rdev->desc->vsel_mask;
  99. val >>= ffs(rdev->desc->vsel_mask) - 1;
  100. return val;
  101. }
  102. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
  103. /**
  104. * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
  105. *
  106. * @rdev: regulator to operate on
  107. * @sel: Selector to set
  108. *
  109. * Regulators that use regmap for their register I/O can set the
  110. * vsel_reg and vsel_mask fields in their descriptor and then use this
  111. * as their set_voltage_vsel operation, saving some code.
  112. */
  113. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
  114. {
  115. int ret;
  116. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  117. ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  118. rdev->desc->vsel_mask, sel);
  119. if (ret)
  120. return ret;
  121. if (rdev->desc->apply_bit)
  122. ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
  123. rdev->desc->apply_bit,
  124. rdev->desc->apply_bit);
  125. return ret;
  126. }
  127. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
  128. /**
  129. * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  130. *
  131. * @rdev: Regulator to operate on
  132. * @min_uV: Lower bound for voltage
  133. * @max_uV: Upper bound for voltage
  134. *
  135. * Drivers implementing set_voltage_sel() and list_voltage() can use
  136. * this as their map_voltage() operation. It will find a suitable
  137. * voltage by calling list_voltage() until it gets something in bounds
  138. * for the requested voltages.
  139. */
  140. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  141. int min_uV, int max_uV)
  142. {
  143. int best_val = INT_MAX;
  144. int selector = 0;
  145. int i, ret;
  146. /* Find the smallest voltage that falls within the specified
  147. * range.
  148. */
  149. for (i = 0; i < rdev->desc->n_voltages; i++) {
  150. ret = rdev->desc->ops->list_voltage(rdev, i);
  151. if (ret < 0)
  152. continue;
  153. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  154. best_val = ret;
  155. selector = i;
  156. }
  157. }
  158. if (best_val != INT_MAX)
  159. return selector;
  160. else
  161. return -EINVAL;
  162. }
  163. EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
  164. /**
  165. * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
  166. *
  167. * @rdev: Regulator to operate on
  168. * @min_uV: Lower bound for voltage
  169. * @max_uV: Upper bound for voltage
  170. *
  171. * Drivers that have ascendant voltage list can use this as their
  172. * map_voltage() operation.
  173. */
  174. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  175. int min_uV, int max_uV)
  176. {
  177. int i, ret;
  178. for (i = 0; i < rdev->desc->n_voltages; i++) {
  179. ret = rdev->desc->ops->list_voltage(rdev, i);
  180. if (ret < 0)
  181. continue;
  182. if (ret > max_uV)
  183. break;
  184. if (ret >= min_uV && ret <= max_uV)
  185. return i;
  186. }
  187. return -EINVAL;
  188. }
  189. EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
  190. /**
  191. * regulator_map_voltage_linear - map_voltage() for simple linear mappings
  192. *
  193. * @rdev: Regulator to operate on
  194. * @min_uV: Lower bound for voltage
  195. * @max_uV: Upper bound for voltage
  196. *
  197. * Drivers providing min_uV and uV_step in their regulator_desc can
  198. * use this as their map_voltage() operation.
  199. */
  200. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  201. int min_uV, int max_uV)
  202. {
  203. int ret, voltage;
  204. /* Allow uV_step to be 0 for fixed voltage */
  205. if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
  206. if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
  207. return 0;
  208. else
  209. return -EINVAL;
  210. }
  211. if (!rdev->desc->uV_step) {
  212. BUG_ON(!rdev->desc->uV_step);
  213. return -EINVAL;
  214. }
  215. if (min_uV < rdev->desc->min_uV)
  216. min_uV = rdev->desc->min_uV;
  217. ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  218. if (ret < 0)
  219. return ret;
  220. ret += rdev->desc->linear_min_sel;
  221. /* Map back into a voltage to verify we're still in bounds */
  222. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  223. if (voltage < min_uV || voltage > max_uV)
  224. return -EINVAL;
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
  228. /**
  229. * regulator_map_voltage_linear - map_voltage() for multiple linear ranges
  230. *
  231. * @rdev: Regulator to operate on
  232. * @min_uV: Lower bound for voltage
  233. * @max_uV: Upper bound for voltage
  234. *
  235. * Drivers providing linear_ranges in their descriptor can use this as
  236. * their map_voltage() callback.
  237. */
  238. int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
  239. int min_uV, int max_uV)
  240. {
  241. const struct regulator_linear_range *range;
  242. int ret = -EINVAL;
  243. int voltage, i;
  244. if (!rdev->desc->n_linear_ranges) {
  245. BUG_ON(!rdev->desc->n_linear_ranges);
  246. return -EINVAL;
  247. }
  248. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  249. int linear_max_uV;
  250. range = &rdev->desc->linear_ranges[i];
  251. linear_max_uV = range->min_uV +
  252. (range->max_sel - range->min_sel) * range->uV_step;
  253. if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
  254. continue;
  255. if (min_uV <= range->min_uV)
  256. min_uV = range->min_uV;
  257. /* range->uV_step == 0 means fixed voltage range */
  258. if (range->uV_step == 0) {
  259. ret = 0;
  260. } else {
  261. ret = DIV_ROUND_UP(min_uV - range->min_uV,
  262. range->uV_step);
  263. if (ret < 0)
  264. return ret;
  265. }
  266. ret += range->min_sel;
  267. break;
  268. }
  269. if (i == rdev->desc->n_linear_ranges)
  270. return -EINVAL;
  271. /* Map back into a voltage to verify we're still in bounds */
  272. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  273. if (voltage < min_uV || voltage > max_uV)
  274. return -EINVAL;
  275. return ret;
  276. }
  277. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
  278. /**
  279. * regulator_list_voltage_linear - List voltages with simple calculation
  280. *
  281. * @rdev: Regulator device
  282. * @selector: Selector to convert into a voltage
  283. *
  284. * Regulators with a simple linear mapping between voltages and
  285. * selectors can set min_uV and uV_step in the regulator descriptor
  286. * and then use this function as their list_voltage() operation,
  287. */
  288. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  289. unsigned int selector)
  290. {
  291. if (selector >= rdev->desc->n_voltages)
  292. return -EINVAL;
  293. if (selector < rdev->desc->linear_min_sel)
  294. return 0;
  295. selector -= rdev->desc->linear_min_sel;
  296. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  297. }
  298. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  299. /**
  300. * regulator_list_voltage_linear_range - List voltages for linear ranges
  301. *
  302. * @rdev: Regulator device
  303. * @selector: Selector to convert into a voltage
  304. *
  305. * Regulators with a series of simple linear mappings between voltages
  306. * and selectors can set linear_ranges in the regulator descriptor and
  307. * then use this function as their list_voltage() operation,
  308. */
  309. int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
  310. unsigned int selector)
  311. {
  312. const struct regulator_linear_range *range;
  313. int i;
  314. if (!rdev->desc->n_linear_ranges) {
  315. BUG_ON(!rdev->desc->n_linear_ranges);
  316. return -EINVAL;
  317. }
  318. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  319. range = &rdev->desc->linear_ranges[i];
  320. if (!(selector >= range->min_sel &&
  321. selector <= range->max_sel))
  322. continue;
  323. selector -= range->min_sel;
  324. return range->min_uV + (range->uV_step * selector);
  325. }
  326. return -EINVAL;
  327. }
  328. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
  329. /**
  330. * regulator_list_voltage_table - List voltages with table based mapping
  331. *
  332. * @rdev: Regulator device
  333. * @selector: Selector to convert into a voltage
  334. *
  335. * Regulators with table based mapping between voltages and
  336. * selectors can set volt_table in the regulator descriptor
  337. * and then use this function as their list_voltage() operation.
  338. */
  339. int regulator_list_voltage_table(struct regulator_dev *rdev,
  340. unsigned int selector)
  341. {
  342. if (!rdev->desc->volt_table) {
  343. BUG_ON(!rdev->desc->volt_table);
  344. return -EINVAL;
  345. }
  346. if (selector >= rdev->desc->n_voltages)
  347. return -EINVAL;
  348. return rdev->desc->volt_table[selector];
  349. }
  350. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  351. /**
  352. * regulator_set_bypass_regmap - Default set_bypass() using regmap
  353. *
  354. * @rdev: device to operate on.
  355. * @enable: state to set.
  356. */
  357. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
  358. {
  359. unsigned int val;
  360. if (enable)
  361. val = rdev->desc->bypass_mask;
  362. else
  363. val = 0;
  364. return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
  365. rdev->desc->bypass_mask, val);
  366. }
  367. EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
  368. /**
  369. * regulator_get_bypass_regmap - Default get_bypass() using regmap
  370. *
  371. * @rdev: device to operate on.
  372. * @enable: current state.
  373. */
  374. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
  375. {
  376. unsigned int val;
  377. int ret;
  378. ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
  379. if (ret != 0)
  380. return ret;
  381. *enable = val & rdev->desc->bypass_mask;
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);