blob: 14ea3dbf490222b4582496a0a93f232a6cb7cc25 [file] [log] [blame]
Barry Song6790e292010-10-27 21:44:07 -04001/*
2 * AD5624R, AD5644R, AD5664R Digital to analog convertors spi driver
3 *
Michael Hennerich14f88f12011-03-09 16:01:45 +01004 * Copyright 2010-2011 Analog Devices Inc.
Barry Song6790e292010-10-27 21:44:07 -04005 *
Michael Hennerich14f88f12011-03-09 16:01:45 +01006 * Licensed under the GPL-2.
Barry Song6790e292010-10-27 21:44:07 -04007 */
8
9#include <linux/interrupt.h>
Barry Song6790e292010-10-27 21:44:07 -040010#include <linux/fs.h>
11#include <linux/device.h>
12#include <linux/kernel.h>
13#include <linux/spi/spi.h>
14#include <linux/slab.h>
15#include <linux/sysfs.h>
Michael Hennerich14f88f12011-03-09 16:01:45 +010016#include <linux/regulator/consumer.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040017#include <linux/module.h>
Barry Song6790e292010-10-27 21:44:07 -040018
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
Lars-Peter Clausencadb6952012-06-04 11:36:19 +020021
Barry Song6790e292010-10-27 21:44:07 -040022#include "ad5624r.h"
23
Michael Hennerichdf9cd102010-11-19 15:16:44 +010024static int ad5624r_spi_write(struct spi_device *spi,
25 u8 cmd, u8 addr, u16 val, u8 len)
Barry Song6790e292010-10-27 21:44:07 -040026{
Barry Song6790e292010-10-27 21:44:07 -040027 u32 data;
28 u8 msg[3];
29
30 /*
Michael Hennerich14f88f12011-03-09 16:01:45 +010031 * The input shift register is 24 bits wide. The first two bits are
32 * don't care bits. The next three are the command bits, C2 to C0,
33 * followed by the 3-bit DAC address, A2 to A0, and then the
34 * 16-, 14-, 12-bit data-word. The data-word comprises the 16-,
35 * 14-, 12-bit input code followed by 0, 2, or 4 don't care bits,
36 * for the AD5664R, AD5644R, and AD5624R, respectively.
Barry Song6790e292010-10-27 21:44:07 -040037 */
38 data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << (16 - len));
39 msg[0] = data >> 16;
40 msg[1] = data >> 8;
41 msg[2] = data;
42
Michael Hennerichdf9cd102010-11-19 15:16:44 +010043 return spi_write(spi, msg, 3);
Barry Song6790e292010-10-27 21:44:07 -040044}
45
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010046static int ad5624r_read_raw(struct iio_dev *indio_dev,
47 struct iio_chan_spec const *chan,
48 int *val,
49 int *val2,
50 long m)
Barry Song6790e292010-10-27 21:44:07 -040051{
Jonathan Cameron3ff24202011-06-27 13:07:31 +010052 struct ad5624r_state *st = iio_priv(indio_dev);
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010053 unsigned long scale_uv;
Barry Song6790e292010-10-27 21:44:07 -040054
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010055 switch (m) {
56 case IIO_CHAN_INFO_SCALE:
57 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
58 *val = scale_uv / 1000;
59 *val2 = (scale_uv % 1000) * 1000;
60 return IIO_VAL_INT_PLUS_MICRO;
Barry Song6790e292010-10-27 21:44:07 -040061
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010062 }
63 return -EINVAL;
64}
65
66static int ad5624r_write_raw(struct iio_dev *indio_dev,
67 struct iio_chan_spec const *chan,
68 int val,
69 int val2,
70 long mask)
71{
72 struct ad5624r_state *st = iio_priv(indio_dev);
73 int ret;
74
75 switch (mask) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +010076 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +010077 if (val >= (1 << chan->scan_type.realbits) || val < 0)
78 return -EINVAL;
79
80 return ad5624r_spi_write(st->us,
81 AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
82 chan->address, val,
83 chan->scan_type.shift);
84 default:
85 ret = -EINVAL;
86 }
87
88 return -EINVAL;
Barry Song6790e292010-10-27 21:44:07 -040089}
90
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +020091static const char * const ad5624r_powerdown_modes[] = {
92 "1kohm_to_gnd",
93 "100kohm_to_gnd",
94 "three_state"
95};
96
97static int ad5624r_get_powerdown_mode(struct iio_dev *indio_dev,
98 const struct iio_chan_spec *chan)
Barry Song6790e292010-10-27 21:44:07 -040099{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100100 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400101
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200102 return st->pwr_down_mode;
Barry Song6790e292010-10-27 21:44:07 -0400103}
104
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200105static int ad5624r_set_powerdown_mode(struct iio_dev *indio_dev,
106 const struct iio_chan_spec *chan, unsigned int mode)
Barry Song6790e292010-10-27 21:44:07 -0400107{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100108 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400109
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200110 st->pwr_down_mode = mode;
Barry Song6790e292010-10-27 21:44:07 -0400111
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200112 return 0;
Barry Song6790e292010-10-27 21:44:07 -0400113}
114
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200115static const struct iio_enum ad5624r_powerdown_mode_enum = {
116 .items = ad5624r_powerdown_modes,
117 .num_items = ARRAY_SIZE(ad5624r_powerdown_modes),
118 .get = ad5624r_get_powerdown_mode,
119 .set = ad5624r_set_powerdown_mode,
120};
121
122static ssize_t ad5624r_read_dac_powerdown(struct iio_dev *indio_dev,
123 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
Barry Song6790e292010-10-27 21:44:07 -0400124{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100125 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400126
Michael Hennerich14f88f12011-03-09 16:01:45 +0100127 return sprintf(buf, "%d\n",
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200128 !!(st->pwr_down_mask & (1 << chan->channel)));
Barry Song6790e292010-10-27 21:44:07 -0400129}
130
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200131static ssize_t ad5624r_write_dac_powerdown(struct iio_dev *indio_dev,
132 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
133 size_t len)
Barry Song6790e292010-10-27 21:44:07 -0400134{
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200135 bool pwr_down;
Barry Song6790e292010-10-27 21:44:07 -0400136 int ret;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100137 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400138
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200139 ret = strtobool(buf, &pwr_down);
Barry Song6790e292010-10-27 21:44:07 -0400140 if (ret)
141 return ret;
142
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200143 if (pwr_down)
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200144 st->pwr_down_mask |= (1 << chan->channel);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100145 else
Lars-Peter Clausen24905942012-06-04 11:36:23 +0200146 st->pwr_down_mask &= ~(1 << chan->channel);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100147
Michael Hennerichdf9cd102010-11-19 15:16:44 +0100148 ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
Michael Hennerich14f88f12011-03-09 16:01:45 +0100149 (st->pwr_down_mode << 4) |
150 st->pwr_down_mask, 16);
Barry Song6790e292010-10-27 21:44:07 -0400151
152 return ret ? ret : len;
153}
154
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100155static const struct iio_info ad5624r_info = {
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +0100156 .write_raw = ad5624r_write_raw,
157 .read_raw = ad5624r_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100158 .driver_module = THIS_MODULE,
159};
160
Lars-Peter Clausen66eb9052012-06-04 11:36:16 +0200161static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
162 {
163 .name = "powerdown",
164 .read = ad5624r_read_dac_powerdown,
165 .write = ad5624r_write_dac_powerdown,
166 },
167 IIO_ENUM("powerdown_mode", true, &ad5624r_powerdown_mode_enum),
168 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5624r_powerdown_mode_enum),
169 { },
170};
171
172#define AD5624R_CHANNEL(_chan, _bits) { \
173 .type = IIO_VOLTAGE, \
174 .indexed = 1, \
175 .output = 1, \
176 .channel = (_chan), \
177 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
178 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
179 .address = (_chan), \
180 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \
181 .ext_info = ad5624r_ext_info, \
182}
183
184#define DECLARE_AD5624R_CHANNELS(_name, _bits) \
185 const struct iio_chan_spec _name##_channels[] = { \
186 AD5624R_CHANNEL(0, _bits), \
187 AD5624R_CHANNEL(1, _bits), \
188 AD5624R_CHANNEL(2, _bits), \
189 AD5624R_CHANNEL(3, _bits), \
190}
191
192static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
193static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
194static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
195
196static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
197 [ID_AD5624R3] = {
198 .channels = ad5624r_channels,
199 .int_vref_mv = 1250,
200 },
201 [ID_AD5624R5] = {
202 .channels = ad5624r_channels,
203 .int_vref_mv = 2500,
204 },
205 [ID_AD5644R3] = {
206 .channels = ad5644r_channels,
207 .int_vref_mv = 1250,
208 },
209 [ID_AD5644R5] = {
210 .channels = ad5644r_channels,
211 .int_vref_mv = 2500,
212 },
213 [ID_AD5664R3] = {
214 .channels = ad5664r_channels,
215 .int_vref_mv = 1250,
216 },
217 [ID_AD5664R5] = {
218 .channels = ad5664r_channels,
219 .int_vref_mv = 2500,
220 },
221};
222
Barry Song6790e292010-10-27 21:44:07 -0400223static int __devinit ad5624r_probe(struct spi_device *spi)
224{
Barry Song6790e292010-10-27 21:44:07 -0400225 struct ad5624r_state *st;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100226 struct iio_dev *indio_dev;
Michael Hennerich14f88f12011-03-09 16:01:45 +0100227 int ret, voltage_uv = 0;
Barry Song6790e292010-10-27 21:44:07 -0400228
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200229 indio_dev = iio_device_alloc(sizeof(*st));
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100230 if (indio_dev == NULL) {
231 ret = -ENOMEM;
Jonathan Cameron26a54792011-08-30 12:41:19 +0100232 goto error_ret;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100233 }
234 st = iio_priv(indio_dev);
Jonathan Cameron26a54792011-08-30 12:41:19 +0100235 st->reg = regulator_get(&spi->dev, "vcc");
236 if (!IS_ERR(st->reg)) {
237 ret = regulator_enable(st->reg);
238 if (ret)
239 goto error_put_reg;
240
Axel Lin8434e782012-12-14 12:07:00 +0000241 ret = regulator_get_voltage(st->reg);
242 if (ret < 0)
243 goto error_disable_reg;
244
245 voltage_uv = ret;
Jonathan Cameron26a54792011-08-30 12:41:19 +0100246 }
247
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100248 spi_set_drvdata(spi, indio_dev);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100249 st->chip_info =
250 &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
251
252 if (voltage_uv)
253 st->vref_mv = voltage_uv / 1000;
254 else
255 st->vref_mv = st->chip_info->int_vref_mv;
Barry Song6790e292010-10-27 21:44:07 -0400256
257 st->us = spi;
Barry Song6790e292010-10-27 21:44:07 -0400258
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100259 indio_dev->dev.parent = &spi->dev;
260 indio_dev->name = spi_get_device_id(spi)->name;
261 indio_dev->info = &ad5624r_info;
262 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausen275de9f2011-11-15 16:31:24 +0100263 indio_dev->channels = st->chip_info->channels;
264 indio_dev->num_channels = AD5624R_DAC_CHANNELS;
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100265
Jonathan Cameron3e394402011-08-30 12:41:18 +0100266 ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
267 !!voltage_uv, 16);
Barry Song6790e292010-10-27 21:44:07 -0400268 if (ret)
Jonathan Cameron26a54792011-08-30 12:41:19 +0100269 goto error_disable_reg;
Barry Song6790e292010-10-27 21:44:07 -0400270
Jonathan Cameron3e394402011-08-30 12:41:18 +0100271 ret = iio_device_register(indio_dev);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100272 if (ret)
Jonathan Cameron26a54792011-08-30 12:41:19 +0100273 goto error_disable_reg;
Barry Song6790e292010-10-27 21:44:07 -0400274
275 return 0;
276
Michael Hennerich14f88f12011-03-09 16:01:45 +0100277error_disable_reg:
Jonathan Cameron26a54792011-08-30 12:41:19 +0100278 if (!IS_ERR(st->reg))
279 regulator_disable(st->reg);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100280error_put_reg:
Jonathan Cameron26a54792011-08-30 12:41:19 +0100281 if (!IS_ERR(st->reg))
282 regulator_put(st->reg);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200283 iio_device_free(indio_dev);
Jonathan Cameron26a54792011-08-30 12:41:19 +0100284error_ret:
Michael Hennerich14f88f12011-03-09 16:01:45 +0100285
Barry Song6790e292010-10-27 21:44:07 -0400286 return ret;
287}
288
289static int __devexit ad5624r_remove(struct spi_device *spi)
290{
Jonathan Cameron3ff24202011-06-27 13:07:31 +0100291 struct iio_dev *indio_dev = spi_get_drvdata(spi);
292 struct ad5624r_state *st = iio_priv(indio_dev);
Barry Song6790e292010-10-27 21:44:07 -0400293
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100294 iio_device_unregister(indio_dev);
Jonathan Cameron26a54792011-08-30 12:41:19 +0100295 if (!IS_ERR(st->reg)) {
296 regulator_disable(st->reg);
297 regulator_put(st->reg);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100298 }
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200299 iio_device_free(indio_dev);
Michael Hennerich14f88f12011-03-09 16:01:45 +0100300
Barry Song6790e292010-10-27 21:44:07 -0400301 return 0;
302}
303
Michael Hennerichece30c12010-11-19 15:16:45 +0100304static const struct spi_device_id ad5624r_id[] = {
Michael Hennerich14f88f12011-03-09 16:01:45 +0100305 {"ad5624r3", ID_AD5624R3},
306 {"ad5644r3", ID_AD5644R3},
307 {"ad5664r3", ID_AD5664R3},
308 {"ad5624r5", ID_AD5624R5},
309 {"ad5644r5", ID_AD5644R5},
310 {"ad5664r5", ID_AD5664R5},
Michael Hennerichece30c12010-11-19 15:16:45 +0100311 {}
312};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100313MODULE_DEVICE_TABLE(spi, ad5624r_id);
Michael Hennerichece30c12010-11-19 15:16:45 +0100314
Barry Song6790e292010-10-27 21:44:07 -0400315static struct spi_driver ad5624r_driver = {
316 .driver = {
Michael Hennerich24d60502010-11-19 15:16:46 +0100317 .name = "ad5624r",
318 .owner = THIS_MODULE,
319 },
Barry Song6790e292010-10-27 21:44:07 -0400320 .probe = ad5624r_probe,
321 .remove = __devexit_p(ad5624r_remove),
Michael Hennerichece30c12010-11-19 15:16:45 +0100322 .id_table = ad5624r_id,
Barry Song6790e292010-10-27 21:44:07 -0400323};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100324module_spi_driver(ad5624r_driver);
Barry Song6790e292010-10-27 21:44:07 -0400325
326MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
327MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
328MODULE_LICENSE("GPL v2");