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