da9210-regulator.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/slab.h>
  25. #include <linux/regulator/driver.h>
  26. #include <linux/regulator/machine.h>
  27. #include <linux/regmap.h>
  28. #include "da9210-regulator.h"
  29. struct da9210 {
  30. struct regulator_dev *rdev;
  31. struct regmap *regmap;
  32. };
  33. static const struct regmap_config da9210_regmap_config = {
  34. .reg_bits = 8,
  35. .val_bits = 8,
  36. };
  37. static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
  38. int max_uA);
  39. static int da9210_get_current_limit(struct regulator_dev *rdev);
  40. static struct regulator_ops da9210_buck_ops = {
  41. .enable = regulator_enable_regmap,
  42. .disable = regulator_disable_regmap,
  43. .is_enabled = regulator_is_enabled_regmap,
  44. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  45. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  46. .list_voltage = regulator_list_voltage_linear,
  47. .set_current_limit = da9210_set_current_limit,
  48. .get_current_limit = da9210_get_current_limit,
  49. };
  50. /* Default limits measured in millivolts and milliamps */
  51. #define DA9210_MIN_MV 300
  52. #define DA9210_MAX_MV 1570
  53. #define DA9210_STEP_MV 10
  54. /* Current limits for buck (uA) indices corresponds with register values */
  55. static const int da9210_buck_limits[] = {
  56. 1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
  57. 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
  58. };
  59. static const struct regulator_desc da9210_reg = {
  60. .name = "DA9210",
  61. .id = 0,
  62. .ops = &da9210_buck_ops,
  63. .type = REGULATOR_VOLTAGE,
  64. .n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
  65. .min_uV = (DA9210_MIN_MV * 1000),
  66. .uV_step = (DA9210_STEP_MV * 1000),
  67. .vsel_reg = DA9210_REG_VBUCK_A,
  68. .vsel_mask = DA9210_VBUCK_MASK,
  69. .enable_reg = DA9210_REG_BUCK_CONT,
  70. .enable_mask = DA9210_BUCK_EN,
  71. .owner = THIS_MODULE,
  72. };
  73. static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
  74. int max_uA)
  75. {
  76. struct da9210 *chip = rdev_get_drvdata(rdev);
  77. unsigned int sel;
  78. int i;
  79. /* search for closest to maximum */
  80. for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) {
  81. if (min_uA <= da9210_buck_limits[i] &&
  82. max_uA >= da9210_buck_limits[i]) {
  83. sel = i;
  84. sel = sel << DA9210_BUCK_ILIM_SHIFT;
  85. return regmap_update_bits(chip->regmap,
  86. DA9210_REG_BUCK_ILIM,
  87. DA9210_BUCK_ILIM_MASK, sel);
  88. }
  89. }
  90. return -EINVAL;
  91. }
  92. static int da9210_get_current_limit(struct regulator_dev *rdev)
  93. {
  94. struct da9210 *chip = rdev_get_drvdata(rdev);
  95. unsigned int data;
  96. unsigned int sel;
  97. int ret;
  98. ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data);
  99. if (ret < 0)
  100. return ret;
  101. /* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */
  102. sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT;
  103. return da9210_buck_limits[sel];
  104. }
  105. /*
  106. * I2C driver interface functions
  107. */
  108. static int da9210_i2c_probe(struct i2c_client *i2c,
  109. const struct i2c_device_id *id)
  110. {
  111. struct da9210 *chip;
  112. struct da9210_pdata *pdata = i2c->dev.platform_data;
  113. struct regulator_dev *rdev = NULL;
  114. struct regulator_config config = { };
  115. int error;
  116. chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
  117. if (NULL == chip) {
  118. dev_err(&i2c->dev,
  119. "Cannot kzalloc memory for regulator structure\n");
  120. return -ENOMEM;
  121. }
  122. chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
  123. if (IS_ERR(chip->regmap)) {
  124. error = PTR_ERR(chip->regmap);
  125. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  126. error);
  127. return error;
  128. }
  129. config.dev = &i2c->dev;
  130. if (pdata)
  131. config.init_data = &pdata->da9210_constraints;
  132. config.driver_data = chip;
  133. config.regmap = chip->regmap;
  134. rdev = regulator_register(&da9210_reg, &config);
  135. if (IS_ERR(rdev)) {
  136. dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
  137. return PTR_ERR(rdev);
  138. }
  139. chip->rdev = rdev;
  140. i2c_set_clientdata(i2c, chip);
  141. return 0;
  142. }
  143. static int da9210_i2c_remove(struct i2c_client *i2c)
  144. {
  145. struct da9210 *chip = i2c_get_clientdata(i2c);
  146. regulator_unregister(chip->rdev);
  147. return 0;
  148. }
  149. static const struct i2c_device_id da9210_i2c_id[] = {
  150. {"da9210", 0},
  151. {},
  152. };
  153. MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
  154. static struct i2c_driver da9210_regulator_driver = {
  155. .driver = {
  156. .name = "da9210",
  157. .owner = THIS_MODULE,
  158. },
  159. .probe = da9210_i2c_probe,
  160. .remove = da9210_i2c_remove,
  161. .id_table = da9210_i2c_id,
  162. };
  163. module_i2c_driver(da9210_regulator_driver);
  164. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  165. MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
  166. MODULE_LICENSE("GPL v2");