da9210-regulator.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/regulator/driver.h>
  26. #include <linux/regulator/machine.h>
  27. #include <linux/of_device.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/regmap.h>
  30. #include "da9210-regulator.h"
  31. struct da9210 {
  32. struct regulator_dev *rdev;
  33. struct regmap *regmap;
  34. };
  35. static const struct regmap_config da9210_regmap_config = {
  36. .reg_bits = 8,
  37. .val_bits = 8,
  38. };
  39. static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
  40. int max_uA);
  41. static int da9210_get_current_limit(struct regulator_dev *rdev);
  42. static const struct regulator_ops da9210_buck_ops = {
  43. .enable = regulator_enable_regmap,
  44. .disable = regulator_disable_regmap,
  45. .is_enabled = regulator_is_enabled_regmap,
  46. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  47. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  48. .list_voltage = regulator_list_voltage_linear,
  49. .set_current_limit = da9210_set_current_limit,
  50. .get_current_limit = da9210_get_current_limit,
  51. };
  52. /* Default limits measured in millivolts and milliamps */
  53. #define DA9210_MIN_MV 300
  54. #define DA9210_MAX_MV 1570
  55. #define DA9210_STEP_MV 10
  56. /* Current limits for buck (uA) indices corresponds with register values */
  57. static const int da9210_buck_limits[] = {
  58. 1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
  59. 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
  60. };
  61. static const struct regulator_desc da9210_reg = {
  62. .name = "DA9210",
  63. .id = 0,
  64. .ops = &da9210_buck_ops,
  65. .type = REGULATOR_VOLTAGE,
  66. .n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
  67. .min_uV = (DA9210_MIN_MV * 1000),
  68. .uV_step = (DA9210_STEP_MV * 1000),
  69. .vsel_reg = DA9210_REG_VBUCK_A,
  70. .vsel_mask = DA9210_VBUCK_MASK,
  71. .enable_reg = DA9210_REG_BUCK_CONT,
  72. .enable_mask = DA9210_BUCK_EN,
  73. .owner = THIS_MODULE,
  74. };
  75. static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
  76. int max_uA)
  77. {
  78. struct da9210 *chip = rdev_get_drvdata(rdev);
  79. unsigned int sel;
  80. int i;
  81. /* search for closest to maximum */
  82. for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) {
  83. if (min_uA <= da9210_buck_limits[i] &&
  84. max_uA >= da9210_buck_limits[i]) {
  85. sel = i;
  86. sel = sel << DA9210_BUCK_ILIM_SHIFT;
  87. return regmap_update_bits(chip->regmap,
  88. DA9210_REG_BUCK_ILIM,
  89. DA9210_BUCK_ILIM_MASK, sel);
  90. }
  91. }
  92. return -EINVAL;
  93. }
  94. static int da9210_get_current_limit(struct regulator_dev *rdev)
  95. {
  96. struct da9210 *chip = rdev_get_drvdata(rdev);
  97. unsigned int data;
  98. unsigned int sel;
  99. int ret;
  100. ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data);
  101. if (ret < 0)
  102. return ret;
  103. /* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */
  104. sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT;
  105. return da9210_buck_limits[sel];
  106. }
  107. static irqreturn_t da9210_irq_handler(int irq, void *data)
  108. {
  109. struct da9210 *chip = data;
  110. unsigned int val, handled = 0;
  111. int error, ret = IRQ_NONE;
  112. error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val);
  113. if (error < 0)
  114. goto error_i2c;
  115. mutex_lock(&chip->rdev->mutex);
  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. mutex_unlock(&chip->rdev->mutex);
  140. if (handled) {
  141. /* Clear handled events */
  142. error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled);
  143. if (error < 0)
  144. goto error_i2c;
  145. ret = IRQ_HANDLED;
  146. }
  147. return ret;
  148. error_i2c:
  149. dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error);
  150. return ret;
  151. }
  152. /*
  153. * I2C driver interface functions
  154. */
  155. static const struct of_device_id da9210_dt_ids[] = {
  156. { .compatible = "dlg,da9210", },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(of, da9210_dt_ids);
  160. static int da9210_i2c_probe(struct i2c_client *i2c,
  161. const struct i2c_device_id *id)
  162. {
  163. struct da9210 *chip;
  164. struct device *dev = &i2c->dev;
  165. struct da9210_pdata *pdata = dev_get_platdata(dev);
  166. struct regulator_dev *rdev = NULL;
  167. struct regulator_config config = { };
  168. int error;
  169. const struct of_device_id *match;
  170. if (i2c->dev.of_node && !pdata) {
  171. match = of_match_device(of_match_ptr(da9210_dt_ids),
  172. &i2c->dev);
  173. if (!match) {
  174. dev_err(&i2c->dev, "Error: No device match found\n");
  175. return -ENODEV;
  176. }
  177. }
  178. chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
  179. if (!chip)
  180. return -ENOMEM;
  181. chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
  182. if (IS_ERR(chip->regmap)) {
  183. error = PTR_ERR(chip->regmap);
  184. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  185. error);
  186. return error;
  187. }
  188. config.dev = &i2c->dev;
  189. config.init_data = pdata ? &pdata->da9210_constraints :
  190. of_get_regulator_init_data(dev, dev->of_node, &da9210_reg);
  191. config.driver_data = chip;
  192. config.regmap = chip->regmap;
  193. config.of_node = dev->of_node;
  194. /* Mask all interrupt sources to deassert interrupt line */
  195. error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0);
  196. if (!error)
  197. error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0);
  198. if (error) {
  199. dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error);
  200. return error;
  201. }
  202. rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
  203. if (IS_ERR(rdev)) {
  204. dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
  205. return PTR_ERR(rdev);
  206. }
  207. chip->rdev = rdev;
  208. if (i2c->irq) {
  209. error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
  210. da9210_irq_handler,
  211. IRQF_TRIGGER_LOW |
  212. IRQF_ONESHOT | IRQF_SHARED,
  213. "da9210", chip);
  214. if (error) {
  215. dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n",
  216. i2c->irq, error);
  217. return error;
  218. }
  219. error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B,
  220. DA9210_M_OVCURR | DA9210_M_NPWRGOOD |
  221. DA9210_M_TEMP_WARN |
  222. DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0);
  223. if (error < 0) {
  224. dev_err(&i2c->dev, "Failed to update mask reg: %d\n",
  225. error);
  226. return error;
  227. }
  228. } else {
  229. dev_warn(&i2c->dev, "No IRQ configured\n");
  230. }
  231. i2c_set_clientdata(i2c, chip);
  232. return 0;
  233. }
  234. static const struct i2c_device_id da9210_i2c_id[] = {
  235. {"da9210", 0},
  236. {},
  237. };
  238. MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
  239. static struct i2c_driver da9210_regulator_driver = {
  240. .driver = {
  241. .name = "da9210",
  242. .of_match_table = of_match_ptr(da9210_dt_ids),
  243. },
  244. .probe = da9210_i2c_probe,
  245. .id_table = da9210_i2c_id,
  246. };
  247. module_i2c_driver(da9210_regulator_driver);
  248. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  249. MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
  250. MODULE_LICENSE("GPL v2");