da9210-regulator.c 5.2 KB

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