helpers.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. val &= rdev->desc->enable_mask;
  37. if (rdev->desc->enable_is_inverted) {
  38. if (rdev->desc->enable_val)
  39. return val != rdev->desc->enable_val;
  40. return val == 0;
  41. } else {
  42. if (rdev->desc->enable_val)
  43. return val == rdev->desc->enable_val;
  44. return val != 0;
  45. }
  46. }
  47. EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
  48. /**
  49. * regulator_enable_regmap - standard enable() for regmap users
  50. *
  51. * @rdev: regulator to operate on
  52. *
  53. * Regulators that use regmap for their register I/O can set the
  54. * enable_reg and enable_mask fields in their descriptor and then use
  55. * this as their enable() operation, saving some code.
  56. */
  57. int regulator_enable_regmap(struct regulator_dev *rdev)
  58. {
  59. unsigned int val;
  60. if (rdev->desc->enable_is_inverted) {
  61. val = rdev->desc->disable_val;
  62. } else {
  63. val = rdev->desc->enable_val;
  64. if (!val)
  65. val = rdev->desc->enable_mask;
  66. }
  67. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  68. rdev->desc->enable_mask, val);
  69. }
  70. EXPORT_SYMBOL_GPL(regulator_enable_regmap);
  71. /**
  72. * regulator_disable_regmap - standard disable() for regmap users
  73. *
  74. * @rdev: regulator to operate on
  75. *
  76. * Regulators that use regmap for their register I/O can set the
  77. * enable_reg and enable_mask fields in their descriptor and then use
  78. * this as their disable() operation, saving some code.
  79. */
  80. int regulator_disable_regmap(struct regulator_dev *rdev)
  81. {
  82. unsigned int val;
  83. if (rdev->desc->enable_is_inverted) {
  84. val = rdev->desc->enable_val;
  85. if (!val)
  86. val = rdev->desc->enable_mask;
  87. } else {
  88. val = rdev->desc->disable_val;
  89. }
  90. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  91. rdev->desc->enable_mask, val);
  92. }
  93. EXPORT_SYMBOL_GPL(regulator_disable_regmap);
  94. /**
  95. * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
  96. *
  97. * @rdev: regulator to operate on
  98. *
  99. * Regulators that use regmap for their register I/O can set the
  100. * vsel_reg and vsel_mask fields in their descriptor and then use this
  101. * as their get_voltage_vsel operation, saving some code.
  102. */
  103. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
  104. {
  105. unsigned int val;
  106. int ret;
  107. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  108. if (ret != 0)
  109. return ret;
  110. val &= rdev->desc->vsel_mask;
  111. val >>= ffs(rdev->desc->vsel_mask) - 1;
  112. return val;
  113. }
  114. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
  115. /**
  116. * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
  117. *
  118. * @rdev: regulator to operate on
  119. * @sel: Selector to set
  120. *
  121. * Regulators that use regmap for their register I/O can set the
  122. * vsel_reg and vsel_mask fields in their descriptor and then use this
  123. * as their set_voltage_vsel operation, saving some code.
  124. */
  125. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
  126. {
  127. int ret;
  128. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  129. ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  130. rdev->desc->vsel_mask, sel);
  131. if (ret)
  132. return ret;
  133. if (rdev->desc->apply_bit)
  134. ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
  135. rdev->desc->apply_bit,
  136. rdev->desc->apply_bit);
  137. return ret;
  138. }
  139. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
  140. /**
  141. * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  142. *
  143. * @rdev: Regulator to operate on
  144. * @min_uV: Lower bound for voltage
  145. * @max_uV: Upper bound for voltage
  146. *
  147. * Drivers implementing set_voltage_sel() and list_voltage() can use
  148. * this as their map_voltage() operation. It will find a suitable
  149. * voltage by calling list_voltage() until it gets something in bounds
  150. * for the requested voltages.
  151. */
  152. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  153. int min_uV, int max_uV)
  154. {
  155. int best_val = INT_MAX;
  156. int selector = 0;
  157. int i, ret;
  158. /* Find the smallest voltage that falls within the specified
  159. * range.
  160. */
  161. for (i = 0; i < rdev->desc->n_voltages; i++) {
  162. ret = rdev->desc->ops->list_voltage(rdev, i);
  163. if (ret < 0)
  164. continue;
  165. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  166. best_val = ret;
  167. selector = i;
  168. }
  169. }
  170. if (best_val != INT_MAX)
  171. return selector;
  172. else
  173. return -EINVAL;
  174. }
  175. EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
  176. /**
  177. * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
  178. *
  179. * @rdev: Regulator to operate on
  180. * @min_uV: Lower bound for voltage
  181. * @max_uV: Upper bound for voltage
  182. *
  183. * Drivers that have ascendant voltage list can use this as their
  184. * map_voltage() operation.
  185. */
  186. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  187. int min_uV, int max_uV)
  188. {
  189. int i, ret;
  190. for (i = 0; i < rdev->desc->n_voltages; i++) {
  191. ret = rdev->desc->ops->list_voltage(rdev, i);
  192. if (ret < 0)
  193. continue;
  194. if (ret > max_uV)
  195. break;
  196. if (ret >= min_uV && ret <= max_uV)
  197. return i;
  198. }
  199. return -EINVAL;
  200. }
  201. EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
  202. /**
  203. * regulator_map_voltage_linear - map_voltage() for simple linear mappings
  204. *
  205. * @rdev: Regulator to operate on
  206. * @min_uV: Lower bound for voltage
  207. * @max_uV: Upper bound for voltage
  208. *
  209. * Drivers providing min_uV and uV_step in their regulator_desc can
  210. * use this as their map_voltage() operation.
  211. */
  212. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  213. int min_uV, int max_uV)
  214. {
  215. int ret, voltage;
  216. /* Allow uV_step to be 0 for fixed voltage */
  217. if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
  218. if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
  219. return 0;
  220. else
  221. return -EINVAL;
  222. }
  223. if (!rdev->desc->uV_step) {
  224. BUG_ON(!rdev->desc->uV_step);
  225. return -EINVAL;
  226. }
  227. if (min_uV < rdev->desc->min_uV)
  228. min_uV = rdev->desc->min_uV;
  229. ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  230. if (ret < 0)
  231. return ret;
  232. ret += rdev->desc->linear_min_sel;
  233. /* Map back into a voltage to verify we're still in bounds */
  234. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  235. if (voltage < min_uV || voltage > max_uV)
  236. return -EINVAL;
  237. return ret;
  238. }
  239. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
  240. /**
  241. * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
  242. *
  243. * @rdev: Regulator to operate on
  244. * @min_uV: Lower bound for voltage
  245. * @max_uV: Upper bound for voltage
  246. *
  247. * Drivers providing linear_ranges in their descriptor can use this as
  248. * their map_voltage() callback.
  249. */
  250. int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
  251. int min_uV, int max_uV)
  252. {
  253. const struct regulator_linear_range *range;
  254. int ret = -EINVAL;
  255. int voltage, i;
  256. if (!rdev->desc->n_linear_ranges) {
  257. BUG_ON(!rdev->desc->n_linear_ranges);
  258. return -EINVAL;
  259. }
  260. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  261. int linear_max_uV;
  262. range = &rdev->desc->linear_ranges[i];
  263. linear_max_uV = range->min_uV +
  264. (range->max_sel - range->min_sel) * range->uV_step;
  265. if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
  266. continue;
  267. if (min_uV <= range->min_uV)
  268. min_uV = range->min_uV;
  269. /* range->uV_step == 0 means fixed voltage range */
  270. if (range->uV_step == 0) {
  271. ret = 0;
  272. } else {
  273. ret = DIV_ROUND_UP(min_uV - range->min_uV,
  274. range->uV_step);
  275. if (ret < 0)
  276. return ret;
  277. }
  278. ret += range->min_sel;
  279. break;
  280. }
  281. if (i == rdev->desc->n_linear_ranges)
  282. return -EINVAL;
  283. /* Map back into a voltage to verify we're still in bounds */
  284. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  285. if (voltage < min_uV || voltage > max_uV)
  286. return -EINVAL;
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
  290. /**
  291. * regulator_list_voltage_linear - List voltages with simple calculation
  292. *
  293. * @rdev: Regulator device
  294. * @selector: Selector to convert into a voltage
  295. *
  296. * Regulators with a simple linear mapping between voltages and
  297. * selectors can set min_uV and uV_step in the regulator descriptor
  298. * and then use this function as their list_voltage() operation,
  299. */
  300. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  301. unsigned int selector)
  302. {
  303. if (selector >= rdev->desc->n_voltages)
  304. return -EINVAL;
  305. if (selector < rdev->desc->linear_min_sel)
  306. return 0;
  307. selector -= rdev->desc->linear_min_sel;
  308. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  309. }
  310. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  311. /**
  312. * regulator_list_voltage_linear_range - List voltages for linear ranges
  313. *
  314. * @rdev: Regulator device
  315. * @selector: Selector to convert into a voltage
  316. *
  317. * Regulators with a series of simple linear mappings between voltages
  318. * and selectors can set linear_ranges in the regulator descriptor and
  319. * then use this function as their list_voltage() operation,
  320. */
  321. int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
  322. unsigned int selector)
  323. {
  324. const struct regulator_linear_range *range;
  325. int i;
  326. if (!rdev->desc->n_linear_ranges) {
  327. BUG_ON(!rdev->desc->n_linear_ranges);
  328. return -EINVAL;
  329. }
  330. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  331. range = &rdev->desc->linear_ranges[i];
  332. if (!(selector >= range->min_sel &&
  333. selector <= range->max_sel))
  334. continue;
  335. selector -= range->min_sel;
  336. return range->min_uV + (range->uV_step * selector);
  337. }
  338. return -EINVAL;
  339. }
  340. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
  341. /**
  342. * regulator_list_voltage_table - List voltages with table based mapping
  343. *
  344. * @rdev: Regulator device
  345. * @selector: Selector to convert into a voltage
  346. *
  347. * Regulators with table based mapping between voltages and
  348. * selectors can set volt_table in the regulator descriptor
  349. * and then use this function as their list_voltage() operation.
  350. */
  351. int regulator_list_voltage_table(struct regulator_dev *rdev,
  352. unsigned int selector)
  353. {
  354. if (!rdev->desc->volt_table) {
  355. BUG_ON(!rdev->desc->volt_table);
  356. return -EINVAL;
  357. }
  358. if (selector >= rdev->desc->n_voltages)
  359. return -EINVAL;
  360. return rdev->desc->volt_table[selector];
  361. }
  362. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  363. /**
  364. * regulator_set_bypass_regmap - Default set_bypass() using regmap
  365. *
  366. * @rdev: device to operate on.
  367. * @enable: state to set.
  368. */
  369. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
  370. {
  371. unsigned int val;
  372. if (enable) {
  373. val = rdev->desc->bypass_val_on;
  374. if (!val)
  375. val = rdev->desc->bypass_mask;
  376. } else {
  377. val = rdev->desc->bypass_val_off;
  378. }
  379. return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
  380. rdev->desc->bypass_mask, val);
  381. }
  382. EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
  383. /**
  384. * regulator_get_bypass_regmap - Default get_bypass() using regmap
  385. *
  386. * @rdev: device to operate on.
  387. * @enable: current state.
  388. */
  389. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
  390. {
  391. unsigned int val;
  392. unsigned int val_on = rdev->desc->bypass_val_on;
  393. int ret;
  394. ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
  395. if (ret != 0)
  396. return ret;
  397. if (!val_on)
  398. val_on = rdev->desc->bypass_mask;
  399. *enable = (val & rdev->desc->bypass_mask) == val_on;
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
  403. /**
  404. * regulator_set_active_discharge_regmap - Default set_active_discharge()
  405. * using regmap
  406. *
  407. * @rdev: device to operate on.
  408. * @enable: state to set, 0 to disable and 1 to enable.
  409. */
  410. int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
  411. bool enable)
  412. {
  413. unsigned int val;
  414. if (enable)
  415. val = rdev->desc->active_discharge_on;
  416. else
  417. val = rdev->desc->active_discharge_off;
  418. return regmap_update_bits(rdev->regmap,
  419. rdev->desc->active_discharge_reg,
  420. rdev->desc->active_discharge_mask, val);
  421. }
  422. EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);