da9210-regulator.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * da9210-regulator.c - Regulator device driver for DA9210
  3. * Copyright (C) 2013 Dialog Semiconductor Ltd.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/slab.h>
  27. #include <linux/regulator/driver.h>
  28. #include <linux/regulator/machine.h>
  29. #include <linux/regulator/of_regulator.h>
  30. #include <linux/regmap.h>
  31. #include "da9210-regulator.h"
  32. struct da9210 {
  33. struct regulator_dev *rdev;
  34. struct regmap *regmap;
  35. };
  36. static const struct regmap_config da9210_regmap_config = {
  37. .reg_bits = 8,
  38. .val_bits = 8,
  39. };
  40. static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
  41. int max_uA);
  42. static int da9210_get_current_limit(struct regulator_dev *rdev);
  43. static struct regulator_ops da9210_buck_ops = {
  44. .enable = regulator_enable_regmap,
  45. .disable = regulator_disable_regmap,
  46. .is_enabled = regulator_is_enabled_regmap,
  47. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  48. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  49. .list_voltage = regulator_list_voltage_linear,
  50. .set_current_limit = da9210_set_current_limit,
  51. .get_current_limit = da9210_get_current_limit,
  52. };
  53. /* Default limits measured in millivolts and milliamps */
  54. #define DA9210_MIN_MV 300
  55. #define DA9210_MAX_MV 1570
  56. #define DA9210_STEP_MV 10
  57. /* Current limits for buck (uA) indices corresponds with register values */
  58. static const int da9210_buck_limits[] = {
  59. 1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
  60. 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
  61. };
  62. static const struct regulator_desc da9210_reg = {
  63. .name = "DA9210",
  64. .id = 0,
  65. .ops = &da9210_buck_ops,
  66. .type = REGULATOR_VOLTAGE,
  67. .n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
  68. .min_uV = (DA9210_MIN_MV * 1000),
  69. .uV_step = (DA9210_STEP_MV * 1000),
  70. .vsel_reg = DA9210_REG_VBUCK_A,
  71. .vsel_mask = DA9210_VBUCK_MASK,
  72. .enable_reg = DA9210_REG_BUCK_CONT,
  73. .enable_mask = DA9210_BUCK_EN,
  74. .owner = THIS_MODULE,
  75. };
  76. static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
  77. int max_uA)
  78. {
  79. struct da9210 *chip = rdev_get_drvdata(rdev);
  80. unsigned int sel;
  81. int i;
  82. /* search for closest to maximum */
  83. for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) {
  84. if (min_uA <= da9210_buck_limits[i] &&
  85. max_uA >= da9210_buck_limits[i]) {
  86. sel = i;
  87. sel = sel << DA9210_BUCK_ILIM_SHIFT;
  88. return regmap_update_bits(chip->regmap,
  89. DA9210_REG_BUCK_ILIM,
  90. DA9210_BUCK_ILIM_MASK, sel);
  91. }
  92. }
  93. return -EINVAL;
  94. }
  95. static int da9210_get_current_limit(struct regulator_dev *rdev)
  96. {
  97. struct da9210 *chip = rdev_get_drvdata(rdev);
  98. unsigned int data;
  99. unsigned int sel;
  100. int ret;
  101. ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data);
  102. if (ret < 0)
  103. return ret;
  104. /* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */
  105. sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT;
  106. return da9210_buck_limits[sel];
  107. }
  108. static irqreturn_t da9210_irq_handler(int irq, void *data)
  109. {
  110. struct da9210 *chip = data;
  111. unsigned int val, handled = 0;
  112. int error, ret = IRQ_NONE;
  113. error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val);
  114. if (error < 0)
  115. goto error_i2c;
  116. if (val & DA9210_E_OVCURR) {
  117. regulator_notifier_call_chain(chip->rdev,
  118. REGULATOR_EVENT_OVER_CURRENT,
  119. NULL);
  120. handled |= DA9210_E_OVCURR;
  121. }
  122. if (val & DA9210_E_NPWRGOOD) {
  123. regulator_notifier_call_chain(chip->rdev,
  124. REGULATOR_EVENT_UNDER_VOLTAGE,
  125. NULL);
  126. handled |= DA9210_E_NPWRGOOD;
  127. }
  128. if (val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT)) {
  129. regulator_notifier_call_chain(chip->rdev,
  130. REGULATOR_EVENT_OVER_TEMP, NULL);
  131. handled |= val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT);
  132. }
  133. if (val & DA9210_E_VMAX) {
  134. regulator_notifier_call_chain(chip->rdev,
  135. REGULATOR_EVENT_REGULATION_OUT,
  136. NULL);
  137. handled |= DA9210_E_VMAX;
  138. }
  139. if (handled) {
  140. /* Clear handled events */
  141. error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled);
  142. if (error < 0)
  143. goto error_i2c;
  144. ret = IRQ_HANDLED;
  145. }
  146. return ret;
  147. error_i2c:
  148. dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error);
  149. return ret;
  150. }
  151. /*
  152. * I2C driver interface functions
  153. */
  154. static int da9210_i2c_probe(struct i2c_client *i2c,
  155. const struct i2c_device_id *id)
  156. {
  157. struct da9210 *chip;
  158. struct device *dev = &i2c->dev;
  159. struct da9210_pdata *pdata = dev_get_platdata(dev);
  160. struct regulator_dev *rdev = NULL;
  161. struct regulator_config config = { };
  162. int error;
  163. chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
  164. if (!chip)
  165. return -ENOMEM;
  166. chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
  167. if (IS_ERR(chip->regmap)) {
  168. error = PTR_ERR(chip->regmap);
  169. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  170. error);
  171. return error;
  172. }
  173. config.dev = &i2c->dev;
  174. config.init_data = pdata ? &pdata->da9210_constraints :
  175. of_get_regulator_init_data(dev, dev->of_node, &da9210_reg);
  176. config.driver_data = chip;
  177. config.regmap = chip->regmap;
  178. config.of_node = dev->of_node;
  179. /* Mask all interrupt sources to deassert interrupt line */
  180. error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0);
  181. if (!error)
  182. error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0);
  183. if (error) {
  184. dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error);
  185. return error;
  186. }
  187. rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
  188. if (IS_ERR(rdev)) {
  189. dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
  190. return PTR_ERR(rdev);
  191. }
  192. chip->rdev = rdev;
  193. if (i2c->irq) {
  194. error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  195. da9210_irq_handler,
  196. IRQF_TRIGGER_LOW |
  197. IRQF_ONESHOT | IRQF_SHARED,
  198. "da9210", chip);
  199. if (error) {
  200. dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n",
  201. i2c->irq, error);
  202. return error;
  203. }
  204. error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B,
  205. DA9210_M_OVCURR | DA9210_M_NPWRGOOD |
  206. DA9210_M_TEMP_WARN |
  207. DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0);
  208. if (error < 0) {
  209. dev_err(&i2c->dev, "Failed to update mask reg: %d\n",
  210. error);
  211. return error;
  212. }
  213. } else {
  214. dev_warn(&i2c->dev, "No IRQ configured\n");
  215. }
  216. i2c_set_clientdata(i2c, chip);
  217. return 0;
  218. }
  219. static const struct i2c_device_id da9210_i2c_id[] = {
  220. {"da9210", 0},
  221. {},
  222. };
  223. MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
  224. static struct i2c_driver da9210_regulator_driver = {
  225. .driver = {
  226. .name = "da9210",
  227. },
  228. .probe = da9210_i2c_probe,
  229. .id_table = da9210_i2c_id,
  230. };
  231. module_i2c_driver(da9210_regulator_driver);
  232. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  233. MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
  234. MODULE_LICENSE("GPL v2");