1 /*
2  * AD5760, AD5780, AD5781, AD5790, AD5791 Voltage Output Digital to Analog
3  * Converter
4  *
5  * Copyright 2011 Analog Devices Inc.
6  *
7  * Licensed under the GPL-2.
8  */
9 
10 #include <linux/interrupt.h>
11 #include <linux/fs.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/module.h>
19 
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "dac.h"
23 #include "ad5791.h"
24 
ad5791_spi_write(struct spi_device * spi,u8 addr,u32 val)25 static int ad5791_spi_write(struct spi_device *spi, u8 addr, u32 val)
26 {
27 	union {
28 		u32 d32;
29 		u8 d8[4];
30 	} data;
31 
32 	data.d32 = cpu_to_be32(AD5791_CMD_WRITE |
33 			      AD5791_ADDR(addr) |
34 			      (val & AD5791_DAC_MASK));
35 
36 	return spi_write(spi, &data.d8[1], 3);
37 }
38 
ad5791_spi_read(struct spi_device * spi,u8 addr,u32 * val)39 static int ad5791_spi_read(struct spi_device *spi, u8 addr, u32 *val)
40 {
41 	union {
42 		u32 d32;
43 		u8 d8[4];
44 	} data[3];
45 	int ret;
46 	struct spi_message msg;
47 	struct spi_transfer xfers[] = {
48 		{
49 			.tx_buf = &data[0].d8[1],
50 			.bits_per_word = 8,
51 			.len = 3,
52 			.cs_change = 1,
53 		}, {
54 			.tx_buf = &data[1].d8[1],
55 			.rx_buf = &data[2].d8[1],
56 			.bits_per_word = 8,
57 			.len = 3,
58 		},
59 	};
60 
61 	data[0].d32 = cpu_to_be32(AD5791_CMD_READ |
62 			      AD5791_ADDR(addr));
63 	data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP));
64 
65 	spi_message_init(&msg);
66 	spi_message_add_tail(&xfers[0], &msg);
67 	spi_message_add_tail(&xfers[1], &msg);
68 	ret = spi_sync(spi, &msg);
69 
70 	*val = be32_to_cpu(data[2].d32);
71 
72 	return ret;
73 }
74 
75 #define AD5791_CHAN(bits, shift) {			\
76 	.type = IIO_VOLTAGE,				\
77 	.output = 1,					\
78 	.indexed = 1,					\
79 	.address = AD5791_ADDR_DAC0,			\
80 	.channel = 0,					\
81 	.info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT | \
82 		IIO_CHAN_INFO_OFFSET_SHARED_BIT,	\
83 	.scan_type = IIO_ST('u', bits, 24, shift)	\
84 }
85 
86 static const struct iio_chan_spec ad5791_channels[] = {
87 	[ID_AD5760] = AD5791_CHAN(16, 4),
88 	[ID_AD5780] = AD5791_CHAN(18, 2),
89 	[ID_AD5781] = AD5791_CHAN(18, 2),
90 	[ID_AD5791] = AD5791_CHAN(20, 0)
91 };
92 
ad5791_read_powerdown_mode(struct device * dev,struct device_attribute * attr,char * buf)93 static ssize_t ad5791_read_powerdown_mode(struct device *dev,
94 				      struct device_attribute *attr, char *buf)
95 {
96 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
97 	struct ad5791_state *st = iio_priv(indio_dev);
98 
99 	const char mode[][14] = {"6kohm_to_gnd", "three_state"};
100 
101 	return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
102 }
103 
ad5791_write_powerdown_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)104 static ssize_t ad5791_write_powerdown_mode(struct device *dev,
105 				       struct device_attribute *attr,
106 				       const char *buf, size_t len)
107 {
108 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
109 	struct ad5791_state *st = iio_priv(indio_dev);
110 	int ret;
111 
112 	if (sysfs_streq(buf, "6kohm_to_gnd"))
113 		st->pwr_down_mode = AD5791_DAC_PWRDN_6K;
114 	else if (sysfs_streq(buf, "three_state"))
115 		st->pwr_down_mode = AD5791_DAC_PWRDN_3STATE;
116 	else
117 		ret = -EINVAL;
118 
119 	return ret ? ret : len;
120 }
121 
ad5791_read_dac_powerdown(struct device * dev,struct device_attribute * attr,char * buf)122 static ssize_t ad5791_read_dac_powerdown(struct device *dev,
123 					   struct device_attribute *attr,
124 					   char *buf)
125 {
126 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
127 	struct ad5791_state *st = iio_priv(indio_dev);
128 
129 	return sprintf(buf, "%d\n", st->pwr_down);
130 }
131 
ad5791_write_dac_powerdown(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)132 static ssize_t ad5791_write_dac_powerdown(struct device *dev,
133 					    struct device_attribute *attr,
134 					    const char *buf, size_t len)
135 {
136 	long readin;
137 	int ret;
138 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
139 	struct ad5791_state *st = iio_priv(indio_dev);
140 
141 	ret = strict_strtol(buf, 10, &readin);
142 	if (ret)
143 		return ret;
144 
145 	if (readin == 0) {
146 		st->pwr_down = false;
147 		st->ctrl &= ~(AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
148 	} else if (readin == 1) {
149 		st->pwr_down = true;
150 		if (st->pwr_down_mode == AD5791_DAC_PWRDN_6K)
151 			st->ctrl |= AD5791_CTRL_OPGND;
152 		else if (st->pwr_down_mode == AD5791_DAC_PWRDN_3STATE)
153 			st->ctrl |= AD5791_CTRL_DACTRI;
154 	} else
155 		ret = -EINVAL;
156 
157 	ret = ad5791_spi_write(st->spi, AD5791_ADDR_CTRL, st->ctrl);
158 
159 	return ret ? ret : len;
160 }
161 
162 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO |
163 			S_IWUSR, ad5791_read_powerdown_mode,
164 			ad5791_write_powerdown_mode, 0);
165 
166 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
167 			"6kohm_to_gnd three_state");
168 
169 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr)		\
170 	IIO_DEVICE_ATTR(out_voltage##_num##_powerdown,			\
171 			S_IRUGO | S_IWUSR, _show, _store, _addr)
172 
173 static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5791_read_dac_powerdown,
174 				   ad5791_write_dac_powerdown, 0);
175 
176 static struct attribute *ad5791_attributes[] = {
177 	&iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
178 	&iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
179 	&iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
180 	NULL,
181 };
182 
183 static const struct attribute_group ad5791_attribute_group = {
184 	.attrs = ad5791_attributes,
185 };
186 
ad5791_get_lin_comp(unsigned int span)187 static int ad5791_get_lin_comp(unsigned int span)
188 {
189 	if (span <= 10000)
190 		return AD5791_LINCOMP_0_10;
191 	else if (span <= 12000)
192 		return AD5791_LINCOMP_10_12;
193 	else if (span <= 16000)
194 		return AD5791_LINCOMP_12_16;
195 	else if (span <= 19000)
196 		return AD5791_LINCOMP_16_19;
197 	else
198 		return AD5791_LINCOMP_19_20;
199 }
200 
ad5780_get_lin_comp(unsigned int span)201 static int ad5780_get_lin_comp(unsigned int span)
202 {
203 	if (span <= 10000)
204 		return AD5780_LINCOMP_0_10;
205 	else
206 		return AD5780_LINCOMP_10_20;
207 }
208 static const struct ad5791_chip_info ad5791_chip_info_tbl[] = {
209 	[ID_AD5760] = {
210 		.get_lin_comp = ad5780_get_lin_comp,
211 	},
212 	[ID_AD5780] = {
213 		.get_lin_comp = ad5780_get_lin_comp,
214 	},
215 	[ID_AD5781] = {
216 		.get_lin_comp = ad5791_get_lin_comp,
217 	},
218 	[ID_AD5791] = {
219 		.get_lin_comp = ad5791_get_lin_comp,
220 	},
221 };
222 
ad5791_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)223 static int ad5791_read_raw(struct iio_dev *indio_dev,
224 			   struct iio_chan_spec const *chan,
225 			   int *val,
226 			   int *val2,
227 			   long m)
228 {
229 	struct ad5791_state *st = iio_priv(indio_dev);
230 	u64 val64;
231 	int ret;
232 
233 	switch (m) {
234 	case 0:
235 		ret = ad5791_spi_read(st->spi, chan->address, val);
236 		if (ret)
237 			return ret;
238 		*val &= AD5791_DAC_MASK;
239 		*val >>= chan->scan_type.shift;
240 		return IIO_VAL_INT;
241 	case IIO_CHAN_INFO_SCALE:
242 		*val = 0;
243 		*val2 = (((u64)st->vref_mv) * 1000000ULL) >> chan->scan_type.realbits;
244 		return IIO_VAL_INT_PLUS_MICRO;
245 	case IIO_CHAN_INFO_OFFSET:
246 		val64 = (((u64)st->vref_neg_mv) << chan->scan_type.realbits);
247 		do_div(val64, st->vref_mv);
248 		*val = -val64;
249 		return IIO_VAL_INT;
250 	default:
251 		return -EINVAL;
252 	}
253 
254 };
255 
256 
ad5791_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)257 static int ad5791_write_raw(struct iio_dev *indio_dev,
258 			    struct iio_chan_spec const *chan,
259 			    int val,
260 			    int val2,
261 			    long mask)
262 {
263 	struct ad5791_state *st = iio_priv(indio_dev);
264 
265 	switch (mask) {
266 	case 0:
267 		val &= AD5791_RES_MASK(chan->scan_type.realbits);
268 		val <<= chan->scan_type.shift;
269 
270 		return ad5791_spi_write(st->spi, chan->address, val);
271 
272 	default:
273 		return -EINVAL;
274 	}
275 }
276 
277 static const struct iio_info ad5791_info = {
278 	.read_raw = &ad5791_read_raw,
279 	.write_raw = &ad5791_write_raw,
280 	.attrs = &ad5791_attribute_group,
281 	.driver_module = THIS_MODULE,
282 };
283 
ad5791_probe(struct spi_device * spi)284 static int __devinit ad5791_probe(struct spi_device *spi)
285 {
286 	struct ad5791_platform_data *pdata = spi->dev.platform_data;
287 	struct iio_dev *indio_dev;
288 	struct ad5791_state *st;
289 	int ret, pos_voltage_uv = 0, neg_voltage_uv = 0;
290 
291 	indio_dev = iio_allocate_device(sizeof(*st));
292 	if (indio_dev == NULL) {
293 		ret = -ENOMEM;
294 		goto error_ret;
295 	}
296 	st = iio_priv(indio_dev);
297 	st->reg_vdd = regulator_get(&spi->dev, "vdd");
298 	if (!IS_ERR(st->reg_vdd)) {
299 		ret = regulator_enable(st->reg_vdd);
300 		if (ret)
301 			goto error_put_reg_pos;
302 
303 		pos_voltage_uv = regulator_get_voltage(st->reg_vdd);
304 	}
305 
306 	st->reg_vss = regulator_get(&spi->dev, "vss");
307 	if (!IS_ERR(st->reg_vss)) {
308 		ret = regulator_enable(st->reg_vss);
309 		if (ret)
310 			goto error_put_reg_neg;
311 
312 		neg_voltage_uv = regulator_get_voltage(st->reg_vss);
313 	}
314 
315 	st->pwr_down = true;
316 	st->spi = spi;
317 
318 	if (!IS_ERR(st->reg_vss) && !IS_ERR(st->reg_vdd)) {
319 		st->vref_mv = (pos_voltage_uv + neg_voltage_uv) / 1000;
320 		st->vref_neg_mv = neg_voltage_uv / 1000;
321 	} else if (pdata) {
322 		st->vref_mv = pdata->vref_pos_mv + pdata->vref_neg_mv;
323 		st->vref_neg_mv = pdata->vref_neg_mv;
324 	} else {
325 		dev_warn(&spi->dev, "reference voltage unspecified\n");
326 	}
327 
328 	ret = ad5791_spi_write(spi, AD5791_ADDR_SW_CTRL, AD5791_SWCTRL_RESET);
329 	if (ret)
330 		goto error_disable_reg_neg;
331 
332 	st->chip_info =	&ad5791_chip_info_tbl[spi_get_device_id(spi)
333 					      ->driver_data];
334 
335 
336 	st->ctrl = AD5761_CTRL_LINCOMP(st->chip_info->get_lin_comp(st->vref_mv))
337 		  | ((pdata && pdata->use_rbuf_gain2) ? 0 : AD5791_CTRL_RBUF) |
338 		  AD5791_CTRL_BIN2SC;
339 
340 	ret = ad5791_spi_write(spi, AD5791_ADDR_CTRL, st->ctrl |
341 		AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
342 	if (ret)
343 		goto error_disable_reg_neg;
344 
345 	spi_set_drvdata(spi, indio_dev);
346 	indio_dev->dev.parent = &spi->dev;
347 	indio_dev->info = &ad5791_info;
348 	indio_dev->modes = INDIO_DIRECT_MODE;
349 	indio_dev->channels
350 		= &ad5791_channels[spi_get_device_id(spi)->driver_data];
351 	indio_dev->num_channels = 1;
352 	indio_dev->name = spi_get_device_id(st->spi)->name;
353 	ret = iio_device_register(indio_dev);
354 	if (ret)
355 		goto error_disable_reg_neg;
356 
357 	return 0;
358 
359 error_disable_reg_neg:
360 	if (!IS_ERR(st->reg_vss))
361 		regulator_disable(st->reg_vss);
362 error_put_reg_neg:
363 	if (!IS_ERR(st->reg_vss))
364 		regulator_put(st->reg_vss);
365 
366 	if (!IS_ERR(st->reg_vdd))
367 		regulator_disable(st->reg_vdd);
368 error_put_reg_pos:
369 	if (!IS_ERR(st->reg_vdd))
370 		regulator_put(st->reg_vdd);
371 	iio_free_device(indio_dev);
372 error_ret:
373 
374 	return ret;
375 }
376 
ad5791_remove(struct spi_device * spi)377 static int __devexit ad5791_remove(struct spi_device *spi)
378 {
379 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
380 	struct ad5791_state *st = iio_priv(indio_dev);
381 
382 	iio_device_unregister(indio_dev);
383 	if (!IS_ERR(st->reg_vdd)) {
384 		regulator_disable(st->reg_vdd);
385 		regulator_put(st->reg_vdd);
386 	}
387 
388 	if (!IS_ERR(st->reg_vss)) {
389 		regulator_disable(st->reg_vss);
390 		regulator_put(st->reg_vss);
391 	}
392 	iio_free_device(indio_dev);
393 
394 	return 0;
395 }
396 
397 static const struct spi_device_id ad5791_id[] = {
398 	{"ad5760", ID_AD5760},
399 	{"ad5780", ID_AD5780},
400 	{"ad5781", ID_AD5781},
401 	{"ad5790", ID_AD5791},
402 	{"ad5791", ID_AD5791},
403 	{}
404 };
405 MODULE_DEVICE_TABLE(spi, ad5791_id);
406 
407 static struct spi_driver ad5791_driver = {
408 	.driver = {
409 		   .name = "ad5791",
410 		   .owner = THIS_MODULE,
411 		   },
412 	.probe = ad5791_probe,
413 	.remove = __devexit_p(ad5791_remove),
414 	.id_table = ad5791_id,
415 };
416 module_spi_driver(ad5791_driver);
417 
418 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
419 MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC");
420 MODULE_LICENSE("GPL v2");
421