ad5398.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 < 0)
  52. dev_err(&client->dev, "I2C write error\n");
  53. return ret;
  54. }
  55. static int ad5398_get_current_limit(struct regulator_dev *rdev)
  56. {
  57. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  58. struct i2c_client *client = chip->client;
  59. unsigned short data;
  60. int ret;
  61. ret = ad5398_read_reg(client, &data);
  62. if (ret < 0)
  63. return ret;
  64. ret = (data & chip->current_mask) >> chip->current_offset;
  65. return ad5398_calc_current(chip, ret);
  66. }
  67. static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
  68. {
  69. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  70. struct i2c_client *client = chip->client;
  71. unsigned range_uA = chip->max_uA - chip->min_uA;
  72. unsigned selector;
  73. unsigned short data;
  74. int ret;
  75. if (min_uA > chip->max_uA || min_uA < chip->min_uA)
  76. return -EINVAL;
  77. if (max_uA > chip->max_uA || max_uA < chip->min_uA)
  78. return -EINVAL;
  79. selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,
  80. range_uA);
  81. if (ad5398_calc_current(chip, selector) > max_uA)
  82. return -EINVAL;
  83. dev_dbg(&client->dev, "changing current %dmA\n",
  84. ad5398_calc_current(chip, selector) / 1000);
  85. /* read chip enable bit */
  86. ret = ad5398_read_reg(client, &data);
  87. if (ret < 0)
  88. return ret;
  89. /* prepare register data */
  90. selector = (selector << chip->current_offset) & chip->current_mask;
  91. data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK);
  92. /* write the new current value back as well as enable bit */
  93. ret = ad5398_write_reg(client, data);
  94. return ret;
  95. }
  96. static int ad5398_is_enabled(struct regulator_dev *rdev)
  97. {
  98. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  99. struct i2c_client *client = chip->client;
  100. unsigned short data;
  101. int ret;
  102. ret = ad5398_read_reg(client, &data);
  103. if (ret < 0)
  104. return ret;
  105. if (data & AD5398_CURRENT_EN_MASK)
  106. return 1;
  107. else
  108. return 0;
  109. }
  110. static int ad5398_enable(struct regulator_dev *rdev)
  111. {
  112. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  113. struct i2c_client *client = chip->client;
  114. unsigned short data;
  115. int ret;
  116. ret = ad5398_read_reg(client, &data);
  117. if (ret < 0)
  118. return ret;
  119. if (data & AD5398_CURRENT_EN_MASK)
  120. return 0;
  121. data |= AD5398_CURRENT_EN_MASK;
  122. ret = ad5398_write_reg(client, data);
  123. return ret;
  124. }
  125. static int ad5398_disable(struct regulator_dev *rdev)
  126. {
  127. struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  128. struct i2c_client *client = chip->client;
  129. unsigned short data;
  130. int ret;
  131. ret = ad5398_read_reg(client, &data);
  132. if (ret < 0)
  133. return ret;
  134. if (!(data & AD5398_CURRENT_EN_MASK))
  135. return 0;
  136. data &= ~AD5398_CURRENT_EN_MASK;
  137. ret = ad5398_write_reg(client, data);
  138. return ret;
  139. }
  140. static struct regulator_ops ad5398_ops = {
  141. .get_current_limit = ad5398_get_current_limit,
  142. .set_current_limit = ad5398_set_current_limit,
  143. .enable = ad5398_enable,
  144. .disable = ad5398_disable,
  145. .is_enabled = ad5398_is_enabled,
  146. };
  147. static struct regulator_desc ad5398_reg = {
  148. .name = "isink",
  149. .id = 0,
  150. .ops = &ad5398_ops,
  151. .type = REGULATOR_CURRENT,
  152. .owner = THIS_MODULE,
  153. };
  154. struct ad5398_current_data_format {
  155. int current_bits;
  156. int current_offset;
  157. int min_uA;
  158. int max_uA;
  159. };
  160. static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};
  161. static const struct i2c_device_id ad5398_id[] = {
  162. { "ad5398", (kernel_ulong_t)&df_10_4_120 },
  163. { "ad5821", (kernel_ulong_t)&df_10_4_120 },
  164. { }
  165. };
  166. MODULE_DEVICE_TABLE(i2c, ad5398_id);
  167. static int __devinit ad5398_probe(struct i2c_client *client,
  168. const struct i2c_device_id *id)
  169. {
  170. struct regulator_init_data *init_data = client->dev.platform_data;
  171. struct ad5398_chip_info *chip;
  172. const struct ad5398_current_data_format *df =
  173. (struct ad5398_current_data_format *)id->driver_data;
  174. int ret;
  175. if (!init_data)
  176. return -EINVAL;
  177. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  178. if (!chip)
  179. return -ENOMEM;
  180. chip->client = client;
  181. chip->min_uA = df->min_uA;
  182. chip->max_uA = df->max_uA;
  183. chip->current_level = 1 << df->current_bits;
  184. chip->current_offset = df->current_offset;
  185. chip->current_mask = (chip->current_level - 1) << chip->current_offset;
  186. chip->rdev = regulator_register(&ad5398_reg, &client->dev,
  187. init_data, chip, NULL);
  188. if (IS_ERR(chip->rdev)) {
  189. ret = PTR_ERR(chip->rdev);
  190. dev_err(&client->dev, "failed to register %s %s\n",
  191. id->name, ad5398_reg.name);
  192. goto err;
  193. }
  194. i2c_set_clientdata(client, chip);
  195. dev_dbg(&client->dev, "%s regulator driver is registered.\n", id->name);
  196. return 0;
  197. err:
  198. kfree(chip);
  199. return ret;
  200. }
  201. static int __devexit ad5398_remove(struct i2c_client *client)
  202. {
  203. struct ad5398_chip_info *chip = i2c_get_clientdata(client);
  204. regulator_unregister(chip->rdev);
  205. kfree(chip);
  206. return 0;
  207. }
  208. static struct i2c_driver ad5398_driver = {
  209. .probe = ad5398_probe,
  210. .remove = __devexit_p(ad5398_remove),
  211. .driver = {
  212. .name = "ad5398",
  213. },
  214. .id_table = ad5398_id,
  215. };
  216. static int __init ad5398_init(void)
  217. {
  218. return i2c_add_driver(&ad5398_driver);
  219. }
  220. subsys_initcall(ad5398_init);
  221. static void __exit ad5398_exit(void)
  222. {
  223. i2c_del_driver(&ad5398_driver);
  224. }
  225. module_exit(ad5398_exit);
  226. MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
  227. MODULE_AUTHOR("Sonic Zhang");
  228. MODULE_LICENSE("GPL");
  229. MODULE_ALIAS("i2c:ad5398-regulator");