ad5624r_spi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * AD5624R, AD5644R, AD5664R Digital to analog convertors spi driver
  3. *
  4. * Copyright 2010-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/fs.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/slab.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/module.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #include "ad5624r.h"
  20. static int ad5624r_spi_write(struct spi_device *spi,
  21. u8 cmd, u8 addr, u16 val, u8 shift)
  22. {
  23. u32 data;
  24. u8 msg[3];
  25. /*
  26. * The input shift register is 24 bits wide. The first two bits are
  27. * don't care bits. The next three are the command bits, C2 to C0,
  28. * followed by the 3-bit DAC address, A2 to A0, and then the
  29. * 16-, 14-, 12-bit data-word. The data-word comprises the 16-,
  30. * 14-, 12-bit input code followed by 0, 2, or 4 don't care bits,
  31. * for the AD5664R, AD5644R, and AD5624R, respectively.
  32. */
  33. data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << shift);
  34. msg[0] = data >> 16;
  35. msg[1] = data >> 8;
  36. msg[2] = data;
  37. return spi_write(spi, msg, 3);
  38. }
  39. static int ad5624r_read_raw(struct iio_dev *indio_dev,
  40. struct iio_chan_spec const *chan,
  41. int *val,
  42. int *val2,
  43. long m)
  44. {
  45. struct ad5624r_state *st = iio_priv(indio_dev);
  46. switch (m) {
  47. case IIO_CHAN_INFO_SCALE:
  48. *val = st->vref_mv;
  49. *val2 = chan->scan_type.realbits;
  50. return IIO_VAL_FRACTIONAL_LOG2;
  51. }
  52. return -EINVAL;
  53. }
  54. static int ad5624r_write_raw(struct iio_dev *indio_dev,
  55. struct iio_chan_spec const *chan,
  56. int val,
  57. int val2,
  58. long mask)
  59. {
  60. struct ad5624r_state *st = iio_priv(indio_dev);
  61. switch (mask) {
  62. case IIO_CHAN_INFO_RAW:
  63. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  64. return -EINVAL;
  65. return ad5624r_spi_write(st->us,
  66. AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
  67. chan->address, val,
  68. chan->scan_type.shift);
  69. default:
  70. return -EINVAL;
  71. }
  72. }
  73. static const char * const ad5624r_powerdown_modes[] = {
  74. "1kohm_to_gnd",
  75. "100kohm_to_gnd",
  76. "three_state"
  77. };
  78. static int ad5624r_get_powerdown_mode(struct iio_dev *indio_dev,
  79. const struct iio_chan_spec *chan)
  80. {
  81. struct ad5624r_state *st = iio_priv(indio_dev);
  82. return st->pwr_down_mode;
  83. }
  84. static int ad5624r_set_powerdown_mode(struct iio_dev *indio_dev,
  85. const struct iio_chan_spec *chan, unsigned int mode)
  86. {
  87. struct ad5624r_state *st = iio_priv(indio_dev);
  88. st->pwr_down_mode = mode;
  89. return 0;
  90. }
  91. static const struct iio_enum ad5624r_powerdown_mode_enum = {
  92. .items = ad5624r_powerdown_modes,
  93. .num_items = ARRAY_SIZE(ad5624r_powerdown_modes),
  94. .get = ad5624r_get_powerdown_mode,
  95. .set = ad5624r_set_powerdown_mode,
  96. };
  97. static ssize_t ad5624r_read_dac_powerdown(struct iio_dev *indio_dev,
  98. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  99. {
  100. struct ad5624r_state *st = iio_priv(indio_dev);
  101. return sprintf(buf, "%d\n",
  102. !!(st->pwr_down_mask & (1 << chan->channel)));
  103. }
  104. static ssize_t ad5624r_write_dac_powerdown(struct iio_dev *indio_dev,
  105. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  106. size_t len)
  107. {
  108. bool pwr_down;
  109. int ret;
  110. struct ad5624r_state *st = iio_priv(indio_dev);
  111. ret = strtobool(buf, &pwr_down);
  112. if (ret)
  113. return ret;
  114. if (pwr_down)
  115. st->pwr_down_mask |= (1 << chan->channel);
  116. else
  117. st->pwr_down_mask &= ~(1 << chan->channel);
  118. ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
  119. (st->pwr_down_mode << 4) |
  120. st->pwr_down_mask, 16);
  121. return ret ? ret : len;
  122. }
  123. static const struct iio_info ad5624r_info = {
  124. .write_raw = ad5624r_write_raw,
  125. .read_raw = ad5624r_read_raw,
  126. .driver_module = THIS_MODULE,
  127. };
  128. static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
  129. {
  130. .name = "powerdown",
  131. .read = ad5624r_read_dac_powerdown,
  132. .write = ad5624r_write_dac_powerdown,
  133. .shared = IIO_SEPARATE,
  134. },
  135. IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
  136. &ad5624r_powerdown_mode_enum),
  137. IIO_ENUM_AVAILABLE("powerdown_mode", &ad5624r_powerdown_mode_enum),
  138. { },
  139. };
  140. #define AD5624R_CHANNEL(_chan, _bits) { \
  141. .type = IIO_VOLTAGE, \
  142. .indexed = 1, \
  143. .output = 1, \
  144. .channel = (_chan), \
  145. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  146. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  147. .address = (_chan), \
  148. .scan_type = { \
  149. .sign = 'u', \
  150. .realbits = (_bits), \
  151. .storagebits = 16, \
  152. .shift = 16 - (_bits), \
  153. }, \
  154. .ext_info = ad5624r_ext_info, \
  155. }
  156. #define DECLARE_AD5624R_CHANNELS(_name, _bits) \
  157. const struct iio_chan_spec _name##_channels[] = { \
  158. AD5624R_CHANNEL(0, _bits), \
  159. AD5624R_CHANNEL(1, _bits), \
  160. AD5624R_CHANNEL(2, _bits), \
  161. AD5624R_CHANNEL(3, _bits), \
  162. }
  163. static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
  164. static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
  165. static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
  166. static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
  167. [ID_AD5624R3] = {
  168. .channels = ad5624r_channels,
  169. .int_vref_mv = 1250,
  170. },
  171. [ID_AD5624R5] = {
  172. .channels = ad5624r_channels,
  173. .int_vref_mv = 2500,
  174. },
  175. [ID_AD5644R3] = {
  176. .channels = ad5644r_channels,
  177. .int_vref_mv = 1250,
  178. },
  179. [ID_AD5644R5] = {
  180. .channels = ad5644r_channels,
  181. .int_vref_mv = 2500,
  182. },
  183. [ID_AD5664R3] = {
  184. .channels = ad5664r_channels,
  185. .int_vref_mv = 1250,
  186. },
  187. [ID_AD5664R5] = {
  188. .channels = ad5664r_channels,
  189. .int_vref_mv = 2500,
  190. },
  191. };
  192. static int ad5624r_probe(struct spi_device *spi)
  193. {
  194. struct ad5624r_state *st;
  195. struct iio_dev *indio_dev;
  196. int ret, voltage_uv = 0;
  197. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  198. if (!indio_dev)
  199. return -ENOMEM;
  200. st = iio_priv(indio_dev);
  201. st->reg = devm_regulator_get(&spi->dev, "vcc");
  202. if (!IS_ERR(st->reg)) {
  203. ret = regulator_enable(st->reg);
  204. if (ret)
  205. return ret;
  206. ret = regulator_get_voltage(st->reg);
  207. if (ret < 0)
  208. goto error_disable_reg;
  209. voltage_uv = ret;
  210. }
  211. spi_set_drvdata(spi, indio_dev);
  212. st->chip_info =
  213. &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
  214. if (voltage_uv)
  215. st->vref_mv = voltage_uv / 1000;
  216. else
  217. st->vref_mv = st->chip_info->int_vref_mv;
  218. st->us = spi;
  219. indio_dev->dev.parent = &spi->dev;
  220. indio_dev->name = spi_get_device_id(spi)->name;
  221. indio_dev->info = &ad5624r_info;
  222. indio_dev->modes = INDIO_DIRECT_MODE;
  223. indio_dev->channels = st->chip_info->channels;
  224. indio_dev->num_channels = AD5624R_DAC_CHANNELS;
  225. ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
  226. !!voltage_uv, 16);
  227. if (ret)
  228. goto error_disable_reg;
  229. ret = iio_device_register(indio_dev);
  230. if (ret)
  231. goto error_disable_reg;
  232. return 0;
  233. error_disable_reg:
  234. if (!IS_ERR(st->reg))
  235. regulator_disable(st->reg);
  236. return ret;
  237. }
  238. static int ad5624r_remove(struct spi_device *spi)
  239. {
  240. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  241. struct ad5624r_state *st = iio_priv(indio_dev);
  242. iio_device_unregister(indio_dev);
  243. if (!IS_ERR(st->reg))
  244. regulator_disable(st->reg);
  245. return 0;
  246. }
  247. static const struct spi_device_id ad5624r_id[] = {
  248. {"ad5624r3", ID_AD5624R3},
  249. {"ad5644r3", ID_AD5644R3},
  250. {"ad5664r3", ID_AD5664R3},
  251. {"ad5624r5", ID_AD5624R5},
  252. {"ad5644r5", ID_AD5644R5},
  253. {"ad5664r5", ID_AD5664R5},
  254. {}
  255. };
  256. MODULE_DEVICE_TABLE(spi, ad5624r_id);
  257. static struct spi_driver ad5624r_driver = {
  258. .driver = {
  259. .name = "ad5624r",
  260. },
  261. .probe = ad5624r_probe,
  262. .remove = ad5624r_remove,
  263. .id_table = ad5624r_id,
  264. };
  265. module_spi_driver(ad5624r_driver);
  266. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  267. MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
  268. MODULE_LICENSE("GPL v2");