ade7854-i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC Driver (I2C Bus)
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/i2c.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/iio/iio.h>
  14. #include "ade7854.h"
  15. static int ade7854_i2c_write_reg_8(struct device *dev,
  16. u16 reg_address,
  17. u8 value)
  18. {
  19. int ret;
  20. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  21. struct ade7854_state *st = iio_priv(indio_dev);
  22. mutex_lock(&st->buf_lock);
  23. st->tx[0] = (reg_address >> 8) & 0xFF;
  24. st->tx[1] = reg_address & 0xFF;
  25. st->tx[2] = value;
  26. ret = i2c_master_send(st->i2c, st->tx, 3);
  27. mutex_unlock(&st->buf_lock);
  28. return ret;
  29. }
  30. static int ade7854_i2c_write_reg_16(struct device *dev,
  31. u16 reg_address,
  32. u16 value)
  33. {
  34. int ret;
  35. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  36. struct ade7854_state *st = iio_priv(indio_dev);
  37. mutex_lock(&st->buf_lock);
  38. st->tx[0] = (reg_address >> 8) & 0xFF;
  39. st->tx[1] = reg_address & 0xFF;
  40. st->tx[2] = (value >> 8) & 0xFF;
  41. st->tx[3] = value & 0xFF;
  42. ret = i2c_master_send(st->i2c, st->tx, 4);
  43. mutex_unlock(&st->buf_lock);
  44. return ret;
  45. }
  46. static int ade7854_i2c_write_reg_24(struct device *dev,
  47. u16 reg_address,
  48. u32 value)
  49. {
  50. int ret;
  51. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  52. struct ade7854_state *st = iio_priv(indio_dev);
  53. mutex_lock(&st->buf_lock);
  54. st->tx[0] = (reg_address >> 8) & 0xFF;
  55. st->tx[1] = reg_address & 0xFF;
  56. st->tx[2] = (value >> 16) & 0xFF;
  57. st->tx[3] = (value >> 8) & 0xFF;
  58. st->tx[4] = value & 0xFF;
  59. ret = i2c_master_send(st->i2c, st->tx, 5);
  60. mutex_unlock(&st->buf_lock);
  61. return ret;
  62. }
  63. static int ade7854_i2c_write_reg_32(struct device *dev,
  64. u16 reg_address,
  65. u32 value)
  66. {
  67. int ret;
  68. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  69. struct ade7854_state *st = iio_priv(indio_dev);
  70. mutex_lock(&st->buf_lock);
  71. st->tx[0] = (reg_address >> 8) & 0xFF;
  72. st->tx[1] = reg_address & 0xFF;
  73. st->tx[2] = (value >> 24) & 0xFF;
  74. st->tx[3] = (value >> 16) & 0xFF;
  75. st->tx[4] = (value >> 8) & 0xFF;
  76. st->tx[5] = value & 0xFF;
  77. ret = i2c_master_send(st->i2c, st->tx, 6);
  78. mutex_unlock(&st->buf_lock);
  79. return ret;
  80. }
  81. static int ade7854_i2c_read_reg_8(struct device *dev,
  82. u16 reg_address,
  83. u8 *val)
  84. {
  85. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  86. struct ade7854_state *st = iio_priv(indio_dev);
  87. int ret;
  88. mutex_lock(&st->buf_lock);
  89. st->tx[0] = (reg_address >> 8) & 0xFF;
  90. st->tx[1] = reg_address & 0xFF;
  91. ret = i2c_master_send(st->i2c, st->tx, 2);
  92. if (ret)
  93. goto out;
  94. ret = i2c_master_recv(st->i2c, st->rx, 1);
  95. if (ret)
  96. goto out;
  97. *val = st->rx[0];
  98. out:
  99. mutex_unlock(&st->buf_lock);
  100. return ret;
  101. }
  102. static int ade7854_i2c_read_reg_16(struct device *dev,
  103. u16 reg_address,
  104. u16 *val)
  105. {
  106. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  107. struct ade7854_state *st = iio_priv(indio_dev);
  108. int ret;
  109. mutex_lock(&st->buf_lock);
  110. st->tx[0] = (reg_address >> 8) & 0xFF;
  111. st->tx[1] = reg_address & 0xFF;
  112. ret = i2c_master_send(st->i2c, st->tx, 2);
  113. if (ret)
  114. goto out;
  115. ret = i2c_master_recv(st->i2c, st->rx, 2);
  116. if (ret)
  117. goto out;
  118. *val = (st->rx[0] << 8) | st->rx[1];
  119. out:
  120. mutex_unlock(&st->buf_lock);
  121. return ret;
  122. }
  123. static int ade7854_i2c_read_reg_24(struct device *dev,
  124. u16 reg_address,
  125. u32 *val)
  126. {
  127. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  128. struct ade7854_state *st = iio_priv(indio_dev);
  129. int ret;
  130. mutex_lock(&st->buf_lock);
  131. st->tx[0] = (reg_address >> 8) & 0xFF;
  132. st->tx[1] = reg_address & 0xFF;
  133. ret = i2c_master_send(st->i2c, st->tx, 2);
  134. if (ret)
  135. goto out;
  136. ret = i2c_master_recv(st->i2c, st->rx, 3);
  137. if (ret)
  138. goto out;
  139. *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
  140. out:
  141. mutex_unlock(&st->buf_lock);
  142. return ret;
  143. }
  144. static int ade7854_i2c_read_reg_32(struct device *dev,
  145. u16 reg_address,
  146. u32 *val)
  147. {
  148. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  149. struct ade7854_state *st = iio_priv(indio_dev);
  150. int ret;
  151. mutex_lock(&st->buf_lock);
  152. st->tx[0] = (reg_address >> 8) & 0xFF;
  153. st->tx[1] = reg_address & 0xFF;
  154. ret = i2c_master_send(st->i2c, st->tx, 2);
  155. if (ret)
  156. goto out;
  157. ret = i2c_master_recv(st->i2c, st->rx, 3);
  158. if (ret)
  159. goto out;
  160. *val = (st->rx[0] << 24) | (st->rx[1] << 16) |
  161. (st->rx[2] << 8) | st->rx[3];
  162. out:
  163. mutex_unlock(&st->buf_lock);
  164. return ret;
  165. }
  166. static int ade7854_i2c_probe(struct i2c_client *client,
  167. const struct i2c_device_id *id)
  168. {
  169. struct ade7854_state *st;
  170. struct iio_dev *indio_dev;
  171. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
  172. if (!indio_dev)
  173. return -ENOMEM;
  174. st = iio_priv(indio_dev);
  175. i2c_set_clientdata(client, indio_dev);
  176. st->read_reg_8 = ade7854_i2c_read_reg_8;
  177. st->read_reg_16 = ade7854_i2c_read_reg_16;
  178. st->read_reg_24 = ade7854_i2c_read_reg_24;
  179. st->read_reg_32 = ade7854_i2c_read_reg_32;
  180. st->write_reg_8 = ade7854_i2c_write_reg_8;
  181. st->write_reg_16 = ade7854_i2c_write_reg_16;
  182. st->write_reg_24 = ade7854_i2c_write_reg_24;
  183. st->write_reg_32 = ade7854_i2c_write_reg_32;
  184. st->i2c = client;
  185. st->irq = client->irq;
  186. return ade7854_probe(indio_dev, &client->dev);
  187. }
  188. static int ade7854_i2c_remove(struct i2c_client *client)
  189. {
  190. return ade7854_remove(i2c_get_clientdata(client));
  191. }
  192. static const struct i2c_device_id ade7854_id[] = {
  193. { "ade7854", 0 },
  194. { "ade7858", 0 },
  195. { "ade7868", 0 },
  196. { "ade7878", 0 },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(i2c, ade7854_id);
  200. static struct i2c_driver ade7854_i2c_driver = {
  201. .driver = {
  202. .name = "ade7854",
  203. },
  204. .probe = ade7854_i2c_probe,
  205. .remove = ade7854_i2c_remove,
  206. .id_table = ade7854_id,
  207. };
  208. module_i2c_driver(ade7854_i2c_driver);
  209. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  210. MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC I2C Driver");
  211. MODULE_LICENSE("GPL v2");