ad5504.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * AD5504, AD5501 High Voltage Digital to Analog Converter
  3. *
  4. * Copyright 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/bitops.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/events.h>
  21. #include <linux/iio/dac/ad5504.h>
  22. #define AD5504_RES_MASK GENMASK(11, 0)
  23. #define AD5504_CMD_READ BIT(15)
  24. #define AD5504_CMD_WRITE 0
  25. #define AD5504_ADDR(addr) ((addr) << 12)
  26. /* Registers */
  27. #define AD5504_ADDR_NOOP 0
  28. #define AD5504_ADDR_DAC(x) ((x) + 1)
  29. #define AD5504_ADDR_ALL_DAC 5
  30. #define AD5504_ADDR_CTRL 7
  31. /* Control Register */
  32. #define AD5504_DAC_PWR(ch) ((ch) << 2)
  33. #define AD5504_DAC_PWRDWN_MODE(mode) ((mode) << 6)
  34. #define AD5504_DAC_PWRDN_20K 0
  35. #define AD5504_DAC_PWRDN_3STATE 1
  36. /**
  37. * struct ad5446_state - driver instance specific data
  38. * @spi: spi_device
  39. * @reg: supply regulator
  40. * @vref_mv: actual reference voltage used
  41. * @pwr_down_mask power down mask
  42. * @pwr_down_mode current power down mode
  43. * @data: transfer buffer
  44. */
  45. struct ad5504_state {
  46. struct spi_device *spi;
  47. struct regulator *reg;
  48. unsigned short vref_mv;
  49. unsigned pwr_down_mask;
  50. unsigned pwr_down_mode;
  51. __be16 data[2] ____cacheline_aligned;
  52. };
  53. /**
  54. * ad5504_supported_device_ids:
  55. */
  56. enum ad5504_supported_device_ids {
  57. ID_AD5504,
  58. ID_AD5501,
  59. };
  60. static int ad5504_spi_write(struct ad5504_state *st, u8 addr, u16 val)
  61. {
  62. st->data[0] = cpu_to_be16(AD5504_CMD_WRITE | AD5504_ADDR(addr) |
  63. (val & AD5504_RES_MASK));
  64. return spi_write(st->spi, &st->data[0], 2);
  65. }
  66. static int ad5504_spi_read(struct ad5504_state *st, u8 addr)
  67. {
  68. int ret;
  69. struct spi_transfer t = {
  70. .tx_buf = &st->data[0],
  71. .rx_buf = &st->data[1],
  72. .len = 2,
  73. };
  74. st->data[0] = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr));
  75. ret = spi_sync_transfer(st->spi, &t, 1);
  76. if (ret < 0)
  77. return ret;
  78. return be16_to_cpu(st->data[1]) & AD5504_RES_MASK;
  79. }
  80. static int ad5504_read_raw(struct iio_dev *indio_dev,
  81. struct iio_chan_spec const *chan,
  82. int *val,
  83. int *val2,
  84. long m)
  85. {
  86. struct ad5504_state *st = iio_priv(indio_dev);
  87. int ret;
  88. switch (m) {
  89. case IIO_CHAN_INFO_RAW:
  90. ret = ad5504_spi_read(st, chan->address);
  91. if (ret < 0)
  92. return ret;
  93. *val = ret;
  94. return IIO_VAL_INT;
  95. case IIO_CHAN_INFO_SCALE:
  96. *val = st->vref_mv;
  97. *val2 = chan->scan_type.realbits;
  98. return IIO_VAL_FRACTIONAL_LOG2;
  99. }
  100. return -EINVAL;
  101. }
  102. static int ad5504_write_raw(struct iio_dev *indio_dev,
  103. struct iio_chan_spec const *chan,
  104. int val,
  105. int val2,
  106. long mask)
  107. {
  108. struct ad5504_state *st = iio_priv(indio_dev);
  109. switch (mask) {
  110. case IIO_CHAN_INFO_RAW:
  111. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  112. return -EINVAL;
  113. return ad5504_spi_write(st, chan->address, val);
  114. default:
  115. return -EINVAL;
  116. }
  117. }
  118. static const char * const ad5504_powerdown_modes[] = {
  119. "20kohm_to_gnd",
  120. "three_state",
  121. };
  122. static int ad5504_get_powerdown_mode(struct iio_dev *indio_dev,
  123. const struct iio_chan_spec *chan)
  124. {
  125. struct ad5504_state *st = iio_priv(indio_dev);
  126. return st->pwr_down_mode;
  127. }
  128. static int ad5504_set_powerdown_mode(struct iio_dev *indio_dev,
  129. const struct iio_chan_spec *chan, unsigned int mode)
  130. {
  131. struct ad5504_state *st = iio_priv(indio_dev);
  132. st->pwr_down_mode = mode;
  133. return 0;
  134. }
  135. static const struct iio_enum ad5504_powerdown_mode_enum = {
  136. .items = ad5504_powerdown_modes,
  137. .num_items = ARRAY_SIZE(ad5504_powerdown_modes),
  138. .get = ad5504_get_powerdown_mode,
  139. .set = ad5504_set_powerdown_mode,
  140. };
  141. static ssize_t ad5504_read_dac_powerdown(struct iio_dev *indio_dev,
  142. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  143. {
  144. struct ad5504_state *st = iio_priv(indio_dev);
  145. return sprintf(buf, "%d\n",
  146. !(st->pwr_down_mask & (1 << chan->channel)));
  147. }
  148. static ssize_t ad5504_write_dac_powerdown(struct iio_dev *indio_dev,
  149. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  150. size_t len)
  151. {
  152. bool pwr_down;
  153. int ret;
  154. struct ad5504_state *st = iio_priv(indio_dev);
  155. ret = strtobool(buf, &pwr_down);
  156. if (ret)
  157. return ret;
  158. if (pwr_down)
  159. st->pwr_down_mask |= (1 << chan->channel);
  160. else
  161. st->pwr_down_mask &= ~(1 << chan->channel);
  162. ret = ad5504_spi_write(st, AD5504_ADDR_CTRL,
  163. AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) |
  164. AD5504_DAC_PWR(st->pwr_down_mask));
  165. /* writes to the CTRL register must be followed by a NOOP */
  166. ad5504_spi_write(st, AD5504_ADDR_NOOP, 0);
  167. return ret ? ret : len;
  168. }
  169. static IIO_CONST_ATTR(temp0_thresh_rising_value, "110000");
  170. static IIO_CONST_ATTR(temp0_thresh_rising_en, "1");
  171. static struct attribute *ad5504_ev_attributes[] = {
  172. &iio_const_attr_temp0_thresh_rising_value.dev_attr.attr,
  173. &iio_const_attr_temp0_thresh_rising_en.dev_attr.attr,
  174. NULL,
  175. };
  176. static struct attribute_group ad5504_ev_attribute_group = {
  177. .attrs = ad5504_ev_attributes,
  178. };
  179. static irqreturn_t ad5504_event_handler(int irq, void *private)
  180. {
  181. iio_push_event(private,
  182. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  183. 0,
  184. IIO_EV_TYPE_THRESH,
  185. IIO_EV_DIR_RISING),
  186. iio_get_time_ns((struct iio_dev *)private));
  187. return IRQ_HANDLED;
  188. }
  189. static const struct iio_info ad5504_info = {
  190. .write_raw = ad5504_write_raw,
  191. .read_raw = ad5504_read_raw,
  192. .event_attrs = &ad5504_ev_attribute_group,
  193. .driver_module = THIS_MODULE,
  194. };
  195. static const struct iio_chan_spec_ext_info ad5504_ext_info[] = {
  196. {
  197. .name = "powerdown",
  198. .read = ad5504_read_dac_powerdown,
  199. .write = ad5504_write_dac_powerdown,
  200. .shared = IIO_SEPARATE,
  201. },
  202. IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
  203. &ad5504_powerdown_mode_enum),
  204. IIO_ENUM_AVAILABLE("powerdown_mode", &ad5504_powerdown_mode_enum),
  205. { },
  206. };
  207. #define AD5504_CHANNEL(_chan) { \
  208. .type = IIO_VOLTAGE, \
  209. .indexed = 1, \
  210. .output = 1, \
  211. .channel = (_chan), \
  212. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  213. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  214. .address = AD5504_ADDR_DAC(_chan), \
  215. .scan_type = { \
  216. .sign = 'u', \
  217. .realbits = 12, \
  218. .storagebits = 16, \
  219. }, \
  220. .ext_info = ad5504_ext_info, \
  221. }
  222. static const struct iio_chan_spec ad5504_channels[] = {
  223. AD5504_CHANNEL(0),
  224. AD5504_CHANNEL(1),
  225. AD5504_CHANNEL(2),
  226. AD5504_CHANNEL(3),
  227. };
  228. static int ad5504_probe(struct spi_device *spi)
  229. {
  230. struct ad5504_platform_data *pdata = spi->dev.platform_data;
  231. struct iio_dev *indio_dev;
  232. struct ad5504_state *st;
  233. struct regulator *reg;
  234. int ret, voltage_uv = 0;
  235. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  236. if (!indio_dev)
  237. return -ENOMEM;
  238. reg = devm_regulator_get(&spi->dev, "vcc");
  239. if (!IS_ERR(reg)) {
  240. ret = regulator_enable(reg);
  241. if (ret)
  242. return ret;
  243. ret = regulator_get_voltage(reg);
  244. if (ret < 0)
  245. goto error_disable_reg;
  246. voltage_uv = ret;
  247. }
  248. spi_set_drvdata(spi, indio_dev);
  249. st = iio_priv(indio_dev);
  250. if (voltage_uv)
  251. st->vref_mv = voltage_uv / 1000;
  252. else if (pdata)
  253. st->vref_mv = pdata->vref_mv;
  254. else
  255. dev_warn(&spi->dev, "reference voltage unspecified\n");
  256. st->reg = reg;
  257. st->spi = spi;
  258. indio_dev->dev.parent = &spi->dev;
  259. indio_dev->name = spi_get_device_id(st->spi)->name;
  260. indio_dev->info = &ad5504_info;
  261. if (spi_get_device_id(st->spi)->driver_data == ID_AD5501)
  262. indio_dev->num_channels = 1;
  263. else
  264. indio_dev->num_channels = 4;
  265. indio_dev->channels = ad5504_channels;
  266. indio_dev->modes = INDIO_DIRECT_MODE;
  267. if (spi->irq) {
  268. ret = devm_request_threaded_irq(&spi->dev, spi->irq,
  269. NULL,
  270. &ad5504_event_handler,
  271. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  272. spi_get_device_id(st->spi)->name,
  273. indio_dev);
  274. if (ret)
  275. goto error_disable_reg;
  276. }
  277. ret = iio_device_register(indio_dev);
  278. if (ret)
  279. goto error_disable_reg;
  280. return 0;
  281. error_disable_reg:
  282. if (!IS_ERR(reg))
  283. regulator_disable(reg);
  284. return ret;
  285. }
  286. static int ad5504_remove(struct spi_device *spi)
  287. {
  288. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  289. struct ad5504_state *st = iio_priv(indio_dev);
  290. iio_device_unregister(indio_dev);
  291. if (!IS_ERR(st->reg))
  292. regulator_disable(st->reg);
  293. return 0;
  294. }
  295. static const struct spi_device_id ad5504_id[] = {
  296. {"ad5504", ID_AD5504},
  297. {"ad5501", ID_AD5501},
  298. {}
  299. };
  300. MODULE_DEVICE_TABLE(spi, ad5504_id);
  301. static struct spi_driver ad5504_driver = {
  302. .driver = {
  303. .name = "ad5504",
  304. },
  305. .probe = ad5504_probe,
  306. .remove = ad5504_remove,
  307. .id_table = ad5504_id,
  308. };
  309. module_spi_driver(ad5504_driver);
  310. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  311. MODULE_DESCRIPTION("Analog Devices AD5501/AD5501 DAC");
  312. MODULE_LICENSE("GPL v2");