ad5398.c 6.3 KB

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