ad5398.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Voltage and current regulation for AD5398 and AD5821
  4. *
  5. * Copyright 2010 Analog Devices Inc.
  6. *
  7. * Enter bugs at http://blackfin.uclinux.org/
  8. */
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/slab.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/machine.h>
  16. #define AD5398_CURRENT_EN_MASK 0x8000
  17. struct ad5398_chip_info {
  18. struct i2c_client *client;
  19. int min_uA;
  20. int max_uA;
  21. unsigned int current_level;
  22. unsigned int current_mask;
  23. unsigned int current_offset;
  24. struct regulator_dev *rdev;
  25. };
  26. static int ad5398_calc_current(struct ad5398_chip_info *chip,
  27. unsigned selector)
  28. {
  29. unsigned range_uA = chip->max_uA - chip->min_uA;
  30. return chip->min_uA + (selector * range_uA / chip->current_level);
  31. }
  32. static int ad5398_read_reg(struct i2c_client *client, unsigned short *data)
  33. {
  34. unsigned short val;
  35. int ret;
  36. ret = i2c_master_recv(client, (char *)&val, 2);
  37. if (ret < 0) {
  38. dev_err(&client->dev, "I2C read error\n");
  39. return ret;
  40. }
  41. *data = be16_to_cpu(val);
  42. return ret;
  43. }
  44. static int ad5398_write_reg(struct i2c_client *client, const unsigned short data)
  45. {
  46. unsigned short val;
  47. int ret;
  48. val = cpu_to_be16(data);
  49. ret = i2c_master_send(client, (char *)&val, 2);
  50. if (ret != 2) {
  51. dev_err(&client->dev, "I2C write error\n");
  52. return ret < 0 ? ret : -EIO;
  53. }
  54. return 0;
  55. }
  56. static int ad5398_get_current_limit(struct regulator_dev *rdev)
  57. {
  58. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  59. struct i2c_client *client = chip->client;
  60. unsigned short data;
  61. int ret;
  62. ret = ad5398_read_reg(client, &data);
  63. if (ret < 0)
  64. return ret;
  65. ret = (data & chip->current_mask) >> chip->current_offset;
  66. return ad5398_calc_current(chip, ret);
  67. }
  68. static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
  69. {
  70. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  71. struct i2c_client *client = chip->client;
  72. unsigned range_uA = chip->max_uA - chip->min_uA;
  73. unsigned selector;
  74. unsigned short data;
  75. int ret;
  76. if (min_uA < chip->min_uA)
  77. min_uA = chip->min_uA;
  78. if (max_uA > chip->max_uA)
  79. max_uA = chip->max_uA;
  80. if (min_uA > chip->max_uA || max_uA < chip->min_uA)
  81. return -EINVAL;
  82. selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,
  83. range_uA);
  84. if (ad5398_calc_current(chip, selector) > max_uA)
  85. return -EINVAL;
  86. dev_dbg(&client->dev, "changing current %duA\n",
  87. ad5398_calc_current(chip, selector));
  88. /* read chip enable bit */
  89. ret = ad5398_read_reg(client, &data);
  90. if (ret < 0)
  91. return ret;
  92. /* prepare register data */
  93. selector = (selector << chip->current_offset) & chip->current_mask;
  94. data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK);
  95. /* write the new current value back as well as enable bit */
  96. ret = ad5398_write_reg(client, data);
  97. return ret;
  98. }
  99. static int ad5398_is_enabled(struct regulator_dev *rdev)
  100. {
  101. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  102. struct i2c_client *client = chip->client;
  103. unsigned short data;
  104. int ret;
  105. ret = ad5398_read_reg(client, &data);
  106. if (ret < 0)
  107. return ret;
  108. if (data & AD5398_CURRENT_EN_MASK)
  109. return 1;
  110. else
  111. return 0;
  112. }
  113. static int ad5398_enable(struct regulator_dev *rdev)
  114. {
  115. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  116. struct i2c_client *client = chip->client;
  117. unsigned short data;
  118. int ret;
  119. ret = ad5398_read_reg(client, &data);
  120. if (ret < 0)
  121. return ret;
  122. if (data & AD5398_CURRENT_EN_MASK)
  123. return 0;
  124. data |= AD5398_CURRENT_EN_MASK;
  125. ret = ad5398_write_reg(client, data);
  126. return ret;
  127. }
  128. static int ad5398_disable(struct regulator_dev *rdev)
  129. {
  130. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  131. struct i2c_client *client = chip->client;
  132. unsigned short data;
  133. int ret;
  134. ret = ad5398_read_reg(client, &data);
  135. if (ret < 0)
  136. return ret;
  137. if (!(data & AD5398_CURRENT_EN_MASK))
  138. return 0;
  139. data &= ~AD5398_CURRENT_EN_MASK;
  140. ret = ad5398_write_reg(client, data);
  141. return ret;
  142. }
  143. static const struct regulator_ops ad5398_ops = {
  144. .get_current_limit = ad5398_get_current_limit,
  145. .set_current_limit = ad5398_set_current_limit,
  146. .enable = ad5398_enable,
  147. .disable = ad5398_disable,
  148. .is_enabled = ad5398_is_enabled,
  149. };
  150. static const struct regulator_desc ad5398_reg = {
  151. .name = "isink",
  152. .id = 0,
  153. .ops = &ad5398_ops,
  154. .type = REGULATOR_CURRENT,
  155. .owner = THIS_MODULE,
  156. };
  157. struct ad5398_current_data_format {
  158. int current_bits;
  159. int current_offset;
  160. int min_uA;
  161. int max_uA;
  162. };
  163. static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};
  164. static const struct i2c_device_id ad5398_id[] = {
  165. { "ad5398", (kernel_ulong_t)&df_10_4_120 },
  166. { "ad5821", (kernel_ulong_t)&df_10_4_120 },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(i2c, ad5398_id);
  170. static int ad5398_probe(struct i2c_client *client,
  171. const struct i2c_device_id *id)
  172. {
  173. struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
  174. struct regulator_config config = { };
  175. struct ad5398_chip_info *chip;
  176. const struct ad5398_current_data_format *df =
  177. (struct ad5398_current_data_format *)id->driver_data;
  178. if (!init_data)
  179. return -EINVAL;
  180. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  181. if (!chip)
  182. return -ENOMEM;
  183. config.dev = &client->dev;
  184. config.init_data = init_data;
  185. config.driver_data = chip;
  186. chip->client = client;
  187. chip->min_uA = df->min_uA;
  188. chip->max_uA = df->max_uA;
  189. chip->current_level = 1 << df->current_bits;
  190. chip->current_offset = df->current_offset;
  191. chip->current_mask = (chip->current_level - 1) << chip->current_offset;
  192. chip->rdev = devm_regulator_register(&client->dev, &ad5398_reg,
  193. &config);
  194. if (IS_ERR(chip->rdev)) {
  195. dev_err(&client->dev, "failed to register %s %s\n",
  196. id->name, ad5398_reg.name);
  197. return PTR_ERR(chip->rdev);
  198. }
  199. i2c_set_clientdata(client, chip);
  200. dev_dbg(&client->dev, "%s regulator driver is registered.\n", id->name);
  201. return 0;
  202. }
  203. static struct i2c_driver ad5398_driver = {
  204. .probe = ad5398_probe,
  205. .driver = {
  206. .name = "ad5398",
  207. },
  208. .id_table = ad5398_id,
  209. };
  210. static int __init ad5398_init(void)
  211. {
  212. return i2c_add_driver(&ad5398_driver);
  213. }
  214. subsys_initcall(ad5398_init);
  215. static void __exit ad5398_exit(void)
  216. {
  217. i2c_del_driver(&ad5398_driver);
  218. }
  219. module_exit(ad5398_exit);
  220. MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
  221. MODULE_AUTHOR("Sonic Zhang");
  222. MODULE_LICENSE("GPL");