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
9 #include <linux/interrupt.h>
10 #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>
16 #include <linux/regulator/consumer.h>
17 #include <linux/module.h>
18
19 #include "../iio.h"
20 #include "../sysfs.h"
21 #include "dac.h"
22 #include "ad5624r.h"
23
24 #define AD5624R_CHANNEL(_chan, _bits) { \
25 .type = IIO_VOLTAGE, \
26 .indexed = 1, \
27 .output = 1, \
28 .channel = (_chan), \
29 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \
30 .address = (_chan), \
31 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \
32 }
33
34 #define DECLARE_AD5624R_CHANNELS(_name, _bits) \
35 const struct iio_chan_spec _name##_channels[] = { \
36 AD5624R_CHANNEL(0, _bits), \
37 AD5624R_CHANNEL(1, _bits), \
38 AD5624R_CHANNEL(2, _bits), \
39 AD5624R_CHANNEL(3, _bits), \
40 }
41
42 static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
43 static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
44 static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
45
46 static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
47 [ID_AD5624R3] = {
48 .channels = ad5624r_channels,
49 .int_vref_mv = 1250,
50 },
51 [ID_AD5624R5] = {
52 .channels = ad5624r_channels,
53 .int_vref_mv = 2500,
54 },
55 [ID_AD5644R3] = {
56 .channels = ad5644r_channels,
57 .int_vref_mv = 1250,
58 },
59 [ID_AD5644R5] = {
60 .channels = ad5644r_channels,
61 .int_vref_mv = 2500,
62 },
63 [ID_AD5664R3] = {
64 .channels = ad5664r_channels,
65 .int_vref_mv = 1250,
66 },
67 [ID_AD5664R5] = {
68 .channels = ad5664r_channels,
69 .int_vref_mv = 2500,
70 },
71 };
72
ad5624r_spi_write(struct spi_device * spi,u8 cmd,u8 addr,u16 val,u8 len)73 static int ad5624r_spi_write(struct spi_device *spi,
74 u8 cmd, u8 addr, u16 val, u8 len)
75 {
76 u32 data;
77 u8 msg[3];
78
79 /*
80 * The input shift register is 24 bits wide. The first two bits are
81 * don't care bits. The next three are the command bits, C2 to C0,
82 * followed by the 3-bit DAC address, A2 to A0, and then the
83 * 16-, 14-, 12-bit data-word. The data-word comprises the 16-,
84 * 14-, 12-bit input code followed by 0, 2, or 4 don't care bits,
85 * for the AD5664R, AD5644R, and AD5624R, respectively.
86 */
87 data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << (16 - len));
88 msg[0] = data >> 16;
89 msg[1] = data >> 8;
90 msg[2] = data;
91
92 return spi_write(spi, msg, 3);
93 }
94
ad5624r_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)95 static int ad5624r_read_raw(struct iio_dev *indio_dev,
96 struct iio_chan_spec const *chan,
97 int *val,
98 int *val2,
99 long m)
100 {
101 struct ad5624r_state *st = iio_priv(indio_dev);
102 unsigned long scale_uv;
103
104 switch (m) {
105 case IIO_CHAN_INFO_SCALE:
106 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
107 *val = scale_uv / 1000;
108 *val2 = (scale_uv % 1000) * 1000;
109 return IIO_VAL_INT_PLUS_MICRO;
110
111 }
112 return -EINVAL;
113 }
114
ad5624r_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)115 static int ad5624r_write_raw(struct iio_dev *indio_dev,
116 struct iio_chan_spec const *chan,
117 int val,
118 int val2,
119 long mask)
120 {
121 struct ad5624r_state *st = iio_priv(indio_dev);
122 int ret;
123
124 switch (mask) {
125 case 0:
126 if (val >= (1 << chan->scan_type.realbits) || val < 0)
127 return -EINVAL;
128
129 return ad5624r_spi_write(st->us,
130 AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
131 chan->address, val,
132 chan->scan_type.shift);
133 default:
134 ret = -EINVAL;
135 }
136
137 return -EINVAL;
138 }
139
ad5624r_read_powerdown_mode(struct device * dev,struct device_attribute * attr,char * buf)140 static ssize_t ad5624r_read_powerdown_mode(struct device *dev,
141 struct device_attribute *attr, char *buf)
142 {
143 struct iio_dev *indio_dev = dev_get_drvdata(dev);
144 struct ad5624r_state *st = iio_priv(indio_dev);
145
146 char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
147
148 return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
149 }
150
ad5624r_write_powerdown_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)151 static ssize_t ad5624r_write_powerdown_mode(struct device *dev,
152 struct device_attribute *attr,
153 const char *buf, size_t len)
154 {
155 struct iio_dev *indio_dev = dev_get_drvdata(dev);
156 struct ad5624r_state *st = iio_priv(indio_dev);
157 int ret;
158
159 if (sysfs_streq(buf, "1kohm_to_gnd"))
160 st->pwr_down_mode = AD5624R_LDAC_PWRDN_1K;
161 else if (sysfs_streq(buf, "100kohm_to_gnd"))
162 st->pwr_down_mode = AD5624R_LDAC_PWRDN_100K;
163 else if (sysfs_streq(buf, "three_state"))
164 st->pwr_down_mode = AD5624R_LDAC_PWRDN_3STATE;
165 else
166 ret = -EINVAL;
167
168 return ret ? ret : len;
169 }
170
ad5624r_read_dac_powerdown(struct device * dev,struct device_attribute * attr,char * buf)171 static ssize_t ad5624r_read_dac_powerdown(struct device *dev,
172 struct device_attribute *attr,
173 char *buf)
174 {
175 struct iio_dev *indio_dev = dev_get_drvdata(dev);
176 struct ad5624r_state *st = iio_priv(indio_dev);
177 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
178
179 return sprintf(buf, "%d\n",
180 !!(st->pwr_down_mask & (1 << this_attr->address)));
181 }
182
ad5624r_write_dac_powerdown(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)183 static ssize_t ad5624r_write_dac_powerdown(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t len)
186 {
187 long readin;
188 int ret;
189 struct iio_dev *indio_dev = dev_get_drvdata(dev);
190 struct ad5624r_state *st = iio_priv(indio_dev);
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
192
193 ret = strict_strtol(buf, 10, &readin);
194 if (ret)
195 return ret;
196
197 if (readin == 1)
198 st->pwr_down_mask |= (1 << this_attr->address);
199 else if (!readin)
200 st->pwr_down_mask &= ~(1 << this_attr->address);
201 else
202 ret = -EINVAL;
203
204 ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
205 (st->pwr_down_mode << 4) |
206 st->pwr_down_mask, 16);
207
208 return ret ? ret : len;
209 }
210
211 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO |
212 S_IWUSR, ad5624r_read_powerdown_mode,
213 ad5624r_write_powerdown_mode, 0);
214
215 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
216 "1kohm_to_gnd 100kohm_to_gnd three_state");
217
218 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \
219 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
220 S_IRUGO | S_IWUSR, _show, _store, _addr)
221
222 static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5624r_read_dac_powerdown,
223 ad5624r_write_dac_powerdown, 0);
224 static IIO_DEV_ATTR_DAC_POWERDOWN(1, ad5624r_read_dac_powerdown,
225 ad5624r_write_dac_powerdown, 1);
226 static IIO_DEV_ATTR_DAC_POWERDOWN(2, ad5624r_read_dac_powerdown,
227 ad5624r_write_dac_powerdown, 2);
228 static IIO_DEV_ATTR_DAC_POWERDOWN(3, ad5624r_read_dac_powerdown,
229 ad5624r_write_dac_powerdown, 3);
230
231 static struct attribute *ad5624r_attributes[] = {
232 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
233 &iio_dev_attr_out_voltage1_powerdown.dev_attr.attr,
234 &iio_dev_attr_out_voltage2_powerdown.dev_attr.attr,
235 &iio_dev_attr_out_voltage3_powerdown.dev_attr.attr,
236 &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
237 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
238 NULL,
239 };
240
241 static const struct attribute_group ad5624r_attribute_group = {
242 .attrs = ad5624r_attributes,
243 };
244
245 static const struct iio_info ad5624r_info = {
246 .write_raw = ad5624r_write_raw,
247 .read_raw = ad5624r_read_raw,
248 .attrs = &ad5624r_attribute_group,
249 .driver_module = THIS_MODULE,
250 };
251
ad5624r_probe(struct spi_device * spi)252 static int __devinit ad5624r_probe(struct spi_device *spi)
253 {
254 struct ad5624r_state *st;
255 struct iio_dev *indio_dev;
256 int ret, voltage_uv = 0;
257
258 indio_dev = iio_allocate_device(sizeof(*st));
259 if (indio_dev == NULL) {
260 ret = -ENOMEM;
261 goto error_ret;
262 }
263 st = iio_priv(indio_dev);
264 st->reg = regulator_get(&spi->dev, "vcc");
265 if (!IS_ERR(st->reg)) {
266 ret = regulator_enable(st->reg);
267 if (ret)
268 goto error_put_reg;
269
270 voltage_uv = regulator_get_voltage(st->reg);
271 }
272
273 spi_set_drvdata(spi, indio_dev);
274 st->chip_info =
275 &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
276
277 if (voltage_uv)
278 st->vref_mv = voltage_uv / 1000;
279 else
280 st->vref_mv = st->chip_info->int_vref_mv;
281
282 st->us = spi;
283
284 indio_dev->dev.parent = &spi->dev;
285 indio_dev->name = spi_get_device_id(spi)->name;
286 indio_dev->info = &ad5624r_info;
287 indio_dev->modes = INDIO_DIRECT_MODE;
288 indio_dev->channels = st->chip_info->channels;
289 indio_dev->num_channels = AD5624R_DAC_CHANNELS;
290
291 ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
292 !!voltage_uv, 16);
293 if (ret)
294 goto error_disable_reg;
295
296 ret = iio_device_register(indio_dev);
297 if (ret)
298 goto error_disable_reg;
299
300 return 0;
301
302 error_disable_reg:
303 if (!IS_ERR(st->reg))
304 regulator_disable(st->reg);
305 error_put_reg:
306 if (!IS_ERR(st->reg))
307 regulator_put(st->reg);
308 iio_free_device(indio_dev);
309 error_ret:
310
311 return ret;
312 }
313
ad5624r_remove(struct spi_device * spi)314 static int __devexit ad5624r_remove(struct spi_device *spi)
315 {
316 struct iio_dev *indio_dev = spi_get_drvdata(spi);
317 struct ad5624r_state *st = iio_priv(indio_dev);
318
319 iio_device_unregister(indio_dev);
320 if (!IS_ERR(st->reg)) {
321 regulator_disable(st->reg);
322 regulator_put(st->reg);
323 }
324 iio_free_device(indio_dev);
325
326 return 0;
327 }
328
329 static const struct spi_device_id ad5624r_id[] = {
330 {"ad5624r3", ID_AD5624R3},
331 {"ad5644r3", ID_AD5644R3},
332 {"ad5664r3", ID_AD5664R3},
333 {"ad5624r5", ID_AD5624R5},
334 {"ad5644r5", ID_AD5644R5},
335 {"ad5664r5", ID_AD5664R5},
336 {}
337 };
338 MODULE_DEVICE_TABLE(spi, ad5624r_id);
339
340 static struct spi_driver ad5624r_driver = {
341 .driver = {
342 .name = "ad5624r",
343 .owner = THIS_MODULE,
344 },
345 .probe = ad5624r_probe,
346 .remove = __devexit_p(ad5624r_remove),
347 .id_table = ad5624r_id,
348 };
349 module_spi_driver(ad5624r_driver);
350
351 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
352 MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
353 MODULE_LICENSE("GPL v2");
354