blob: f5583aedfb597cf927e3e39969b80e5e0184085e [file] [log] [blame]
Michael Hennerichb5a49482010-11-22 11:15:23 +01001/*
2 * AD5446 SPI DAC driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/workqueue.h>
11#include <linux/device.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/sysfs.h>
15#include <linux/list.h>
16#include <linux/spi/spi.h>
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +010017#include <linux/i2c.h>
Michael Hennerichb5a49482010-11-22 11:15:23 +010018#include <linux/regulator/consumer.h>
19#include <linux/err.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040020#include <linux/module.h>
Michael Hennerichb5a49482010-11-22 11:15:23 +010021
Jonathan Cameron06458e22012-04-25 15:54:58 +010022#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
Michael Hennerichb5a49482010-11-22 11:15:23 +010024
Jean-Francois Dagenais2e15c902012-08-21 15:28:00 +010025#define MODE_PWRDWN_1k 0x1
26#define MODE_PWRDWN_100k 0x2
27#define MODE_PWRDWN_TRISTATE 0x3
28
29/**
30 * struct ad5446_state - driver instance specific data
31 * @spi: spi_device
32 * @chip_info: chip model specific constants, available modes etc
33 * @reg: supply regulator
34 * @vref_mv: actual reference voltage used
35 */
36
37struct ad5446_state {
38 struct device *dev;
39 const struct ad5446_chip_info *chip_info;
40 struct regulator *reg;
41 unsigned short vref_mv;
42 unsigned cached_val;
43 unsigned pwr_down_mode;
44 unsigned pwr_down;
45};
46
47/**
48 * struct ad5446_chip_info - chip specific information
49 * @channel: channel spec for the DAC
50 * @int_vref_mv: AD5620/40/60: the internal reference voltage
51 * @write: chip specific helper function to write to the register
52 */
53
54struct ad5446_chip_info {
55 struct iio_chan_spec channel;
56 u16 int_vref_mv;
57 int (*write)(struct ad5446_state *st, unsigned val);
58};
Michael Hennerichb5a49482010-11-22 11:15:23 +010059
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020060static const char * const ad5446_powerdown_modes[] = {
Lars-Peter Clausen09d48aa2012-06-04 11:36:13 +020061 "1kohm_to_gnd", "100kohm_to_gnd", "three_state"
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020062};
Michael Hennerichbbed4dc2011-03-10 13:26:47 +010063
Lars-Peter Clausen09d48aa2012-06-04 11:36:13 +020064static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev,
65 const struct iio_chan_spec *chan, unsigned int mode)
Michael Hennerichbbed4dc2011-03-10 13:26:47 +010066{
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020067 struct ad5446_state *st = iio_priv(indio_dev);
68
Lars-Peter Clausen09d48aa2012-06-04 11:36:13 +020069 st->pwr_down_mode = mode + 1;
70
71 return 0;
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020072}
73
Lars-Peter Clausen09d48aa2012-06-04 11:36:13 +020074static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev,
75 const struct iio_chan_spec *chan)
76{
77 struct ad5446_state *st = iio_priv(indio_dev);
78
79 return st->pwr_down_mode - 1;
80}
81
82static const struct iio_enum ad5446_powerdown_mode_enum = {
83 .items = ad5446_powerdown_modes,
84 .num_items = ARRAY_SIZE(ad5446_powerdown_modes),
85 .get = ad5446_get_powerdown_mode,
86 .set = ad5446_set_powerdown_mode,
87};
88
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020089static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev,
Michael Hennerichfc6d1132012-04-27 10:58:36 +020090 uintptr_t private,
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020091 const struct iio_chan_spec *chan,
92 char *buf)
93{
Jonathan Cameron638e59f2011-10-06 17:14:38 +010094 struct ad5446_state *st = iio_priv(indio_dev);
Michael Hennerichbbed4dc2011-03-10 13:26:47 +010095
96 return sprintf(buf, "%d\n", st->pwr_down);
97}
98
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +020099static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev,
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200100 uintptr_t private,
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200101 const struct iio_chan_spec *chan,
Michael Hennerichbbed4dc2011-03-10 13:26:47 +0100102 const char *buf, size_t len)
103{
Jonathan Cameron638e59f2011-10-06 17:14:38 +0100104 struct ad5446_state *st = iio_priv(indio_dev);
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200105 unsigned int shift;
106 unsigned int val;
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200107 bool powerdown;
Michael Hennerichbbed4dc2011-03-10 13:26:47 +0100108 int ret;
109
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200110 ret = strtobool(buf, &powerdown);
Michael Hennerichbbed4dc2011-03-10 13:26:47 +0100111 if (ret)
112 return ret;
113
Jonathan Cameron638e59f2011-10-06 17:14:38 +0100114 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200115 st->pwr_down = powerdown;
Michael Hennerichbbed4dc2011-03-10 13:26:47 +0100116
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200117 if (st->pwr_down) {
118 shift = chan->scan_type.realbits + chan->scan_type.shift;
119 val = st->pwr_down_mode << shift;
120 } else {
121 val = st->cached_val;
122 }
Michael Hennerichbbed4dc2011-03-10 13:26:47 +0100123
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200124 ret = st->chip_info->write(st, val);
Jonathan Cameron638e59f2011-10-06 17:14:38 +0100125 mutex_unlock(&indio_dev->mlock);
Michael Hennerichbbed4dc2011-03-10 13:26:47 +0100126
127 return ret ? ret : len;
128}
129
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100130static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200131 {
132 .name = "powerdown",
133 .read = ad5446_read_dac_powerdown,
134 .write = ad5446_write_dac_powerdown,
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200135 },
Lars-Peter Clausen09d48aa2012-06-04 11:36:13 +0200136 IIO_ENUM("powerdown_mode", false, &ad5446_powerdown_mode_enum),
137 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum),
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200138 { },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100139};
140
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200141#define _AD5446_CHANNEL(bits, storage, shift, ext) { \
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100142 .type = IIO_VOLTAGE, \
143 .indexed = 1, \
144 .output = 1, \
145 .channel = 0, \
Jonathan Cameron09f4eb42012-04-15 17:41:19 +0100146 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
147 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200148 .scan_type = IIO_ST('u', (bits), (storage), (shift)), \
149 .ext_info = (ext), \
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100150}
151
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200152#define AD5446_CHANNEL(bits, storage, shift) \
153 _AD5446_CHANNEL(bits, storage, shift, NULL)
154
155#define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100156 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200157
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100158static int ad5446_read_raw(struct iio_dev *indio_dev,
159 struct iio_chan_spec const *chan,
160 int *val,
161 int *val2,
162 long m)
163{
164 struct ad5446_state *st = iio_priv(indio_dev);
165 unsigned long scale_uv;
166
167 switch (m) {
168 case IIO_CHAN_INFO_RAW:
169 *val = st->cached_val;
170 return IIO_VAL_INT;
171 case IIO_CHAN_INFO_SCALE:
172 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
173 *val = scale_uv / 1000;
174 *val2 = (scale_uv % 1000) * 1000;
175 return IIO_VAL_INT_PLUS_MICRO;
176
177 }
178 return -EINVAL;
179}
180
181static int ad5446_write_raw(struct iio_dev *indio_dev,
182 struct iio_chan_spec const *chan,
183 int val,
184 int val2,
185 long mask)
186{
187 struct ad5446_state *st = iio_priv(indio_dev);
188 int ret = 0;
189
190 switch (mask) {
191 case IIO_CHAN_INFO_RAW:
192 if (val >= (1 << chan->scan_type.realbits) || val < 0)
193 return -EINVAL;
194
195 val <<= chan->scan_type.shift;
196 mutex_lock(&indio_dev->mlock);
197 st->cached_val = val;
198 if (!st->pwr_down)
199 ret = st->chip_info->write(st, val);
200 mutex_unlock(&indio_dev->mlock);
201 break;
202 default:
203 ret = -EINVAL;
204 }
205
206 return ret;
207}
208
209static const struct iio_info ad5446_info = {
210 .read_raw = ad5446_read_raw,
211 .write_raw = ad5446_write_raw,
212 .driver_module = THIS_MODULE,
213};
214
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800215static int ad5446_probe(struct device *dev, const char *name,
216 const struct ad5446_chip_info *chip_info)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100217{
218 struct ad5446_state *st;
219 struct iio_dev *indio_dev;
220 struct regulator *reg;
221 int ret, voltage_uv = 0;
222
223 reg = regulator_get(dev, "vcc");
224 if (!IS_ERR(reg)) {
225 ret = regulator_enable(reg);
226 if (ret)
227 goto error_put_reg;
228
Axel Lin13e57ee2012-12-18 03:33:00 +0000229 ret = regulator_get_voltage(reg);
230 if (ret < 0)
231 goto error_disable_reg;
232
233 voltage_uv = ret;
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100234 }
235
236 indio_dev = iio_device_alloc(sizeof(*st));
237 if (indio_dev == NULL) {
238 ret = -ENOMEM;
239 goto error_disable_reg;
240 }
241 st = iio_priv(indio_dev);
242 st->chip_info = chip_info;
243
244 dev_set_drvdata(dev, indio_dev);
245 st->reg = reg;
246 st->dev = dev;
247
248 /* Establish that the iio_dev is a child of the device */
249 indio_dev->dev.parent = dev;
250 indio_dev->name = name;
251 indio_dev->info = &ad5446_info;
252 indio_dev->modes = INDIO_DIRECT_MODE;
253 indio_dev->channels = &st->chip_info->channel;
254 indio_dev->num_channels = 1;
255
256 st->pwr_down_mode = MODE_PWRDWN_1k;
257
258 if (st->chip_info->int_vref_mv)
259 st->vref_mv = st->chip_info->int_vref_mv;
260 else if (voltage_uv)
261 st->vref_mv = voltage_uv / 1000;
262 else
263 dev_warn(dev, "reference voltage unspecified\n");
264
265 ret = iio_device_register(indio_dev);
266 if (ret)
267 goto error_free_device;
268
269 return 0;
270
271error_free_device:
272 iio_device_free(indio_dev);
273error_disable_reg:
274 if (!IS_ERR(reg))
275 regulator_disable(reg);
276error_put_reg:
277 if (!IS_ERR(reg))
278 regulator_put(reg);
279
280 return ret;
281}
282
283static int ad5446_remove(struct device *dev)
284{
285 struct iio_dev *indio_dev = dev_get_drvdata(dev);
286 struct ad5446_state *st = iio_priv(indio_dev);
287
288 iio_device_unregister(indio_dev);
289 if (!IS_ERR(st->reg)) {
290 regulator_disable(st->reg);
291 regulator_put(st->reg);
292 }
293 iio_device_free(indio_dev);
294
295 return 0;
296}
297
298#if IS_ENABLED(CONFIG_SPI_MASTER)
299
300static int ad5446_write(struct ad5446_state *st, unsigned val)
301{
302 struct spi_device *spi = to_spi_device(st->dev);
303 __be16 data = cpu_to_be16(val);
304
305 return spi_write(spi, &data, sizeof(data));
306}
307
308static int ad5660_write(struct ad5446_state *st, unsigned val)
309{
310 struct spi_device *spi = to_spi_device(st->dev);
311 uint8_t data[3];
312
313 data[0] = (val >> 16) & 0xFF;
314 data[1] = (val >> 8) & 0xFF;
315 data[2] = val & 0xFF;
316
317 return spi_write(spi, data, sizeof(data));
318}
319
320/**
321 * ad5446_supported_spi_device_ids:
322 * The AD5620/40/60 parts are available in different fixed internal reference
323 * voltage options. The actual part numbers may look differently
324 * (and a bit cryptic), however this style is used to make clear which
325 * parts are supported here.
326 */
327enum ad5446_supported_spi_device_ids {
Lars-Peter Clausen2fafbce2012-09-04 10:10:00 +0100328 ID_AD5300,
329 ID_AD5310,
330 ID_AD5320,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100331 ID_AD5444,
332 ID_AD5446,
333 ID_AD5450,
334 ID_AD5451,
335 ID_AD5541A,
336 ID_AD5512A,
337 ID_AD5553,
338 ID_AD5601,
339 ID_AD5611,
340 ID_AD5621,
341 ID_AD5620_2500,
342 ID_AD5620_1250,
343 ID_AD5640_2500,
344 ID_AD5640_1250,
345 ID_AD5660_2500,
346 ID_AD5660_1250,
347 ID_AD5662,
348};
349
350static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
Lars-Peter Clausen2fafbce2012-09-04 10:10:00 +0100351 [ID_AD5300] = {
352 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
353 .write = ad5446_write,
354 },
355 [ID_AD5310] = {
356 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
357 .write = ad5446_write,
358 },
359 [ID_AD5320] = {
360 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
361 .write = ad5446_write,
362 },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100363 [ID_AD5444] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100364 .channel = AD5446_CHANNEL(12, 16, 2),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200365 .write = ad5446_write,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100366 },
367 [ID_AD5446] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100368 .channel = AD5446_CHANNEL(14, 16, 0),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200369 .write = ad5446_write,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100370 },
Lars-Peter Clausen779c0c42012-06-26 10:45:43 +0200371 [ID_AD5450] = {
372 .channel = AD5446_CHANNEL(8, 16, 6),
373 .write = ad5446_write,
374 },
375 [ID_AD5451] = {
376 .channel = AD5446_CHANNEL(10, 16, 4),
377 .write = ad5446_write,
378 },
Michael Hennerich67d1c1f2011-04-15 13:51:07 +0200379 [ID_AD5541A] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100380 .channel = AD5446_CHANNEL(16, 16, 0),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200381 .write = ad5446_write,
Michael Hennerich67d1c1f2011-04-15 13:51:07 +0200382 },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100383 [ID_AD5512A] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100384 .channel = AD5446_CHANNEL(12, 16, 4),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200385 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100386 },
Michael Hennerich07722682011-02-23 10:45:47 +0100387 [ID_AD5553] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100388 .channel = AD5446_CHANNEL(14, 16, 0),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200389 .write = ad5446_write,
Michael Hennerich07722682011-02-23 10:45:47 +0100390 },
Michael Hennerich2b615352011-03-10 13:26:48 +0100391 [ID_AD5601] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200392 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200393 .write = ad5446_write,
Michael Hennerich2b615352011-03-10 13:26:48 +0100394 },
395 [ID_AD5611] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200396 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200397 .write = ad5446_write,
Michael Hennerich2b615352011-03-10 13:26:48 +0100398 },
399 [ID_AD5621] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200400 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200401 .write = ad5446_write,
Michael Hennerich2b615352011-03-10 13:26:48 +0100402 },
Michael Hennerichd8462632010-11-23 11:14:16 +0100403 [ID_AD5620_2500] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200404 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
Michael Hennerichd8462632010-11-23 11:14:16 +0100405 .int_vref_mv = 2500,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200406 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100407 },
408 [ID_AD5620_1250] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200409 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
Michael Hennerichd8462632010-11-23 11:14:16 +0100410 .int_vref_mv = 1250,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200411 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100412 },
413 [ID_AD5640_2500] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200414 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100415 .int_vref_mv = 2500,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200416 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100417 },
418 [ID_AD5640_1250] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200419 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100420 .int_vref_mv = 1250,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200421 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100422 },
423 [ID_AD5660_2500] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200424 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100425 .int_vref_mv = 2500,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200426 .write = ad5660_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100427 },
428 [ID_AD5660_1250] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200429 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100430 .int_vref_mv = 1250,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200431 .write = ad5660_write,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100432 },
Lars-Peter Clausen18e5ab32012-04-25 09:45:01 +0200433 [ID_AD5662] = {
434 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
435 .write = ad5660_write,
436 },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100437};
438
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100439static const struct spi_device_id ad5446_spi_ids[] = {
Lars-Peter Clausen2fafbce2012-09-04 10:10:00 +0100440 {"ad5300", ID_AD5300},
441 {"ad5310", ID_AD5310},
442 {"ad5320", ID_AD5320},
Michael Hennerichb5a49482010-11-22 11:15:23 +0100443 {"ad5444", ID_AD5444},
444 {"ad5446", ID_AD5446},
Lars-Peter Clausen779c0c42012-06-26 10:45:43 +0200445 {"ad5450", ID_AD5450},
446 {"ad5451", ID_AD5451},
447 {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
448 {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
Michael Hennerichb5a49482010-11-22 11:15:23 +0100449 {"ad5512a", ID_AD5512A},
Michael Hennerich67d1c1f2011-04-15 13:51:07 +0200450 {"ad5541a", ID_AD5541A},
Lars-Peter Clausen11a7df42012-04-25 09:44:57 +0200451 {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
452 {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
Michael Hennerichbd51c0b2011-03-10 13:26:46 +0100453 {"ad5553", ID_AD5553},
Michael Hennerich2b615352011-03-10 13:26:48 +0100454 {"ad5601", ID_AD5601},
455 {"ad5611", ID_AD5611},
456 {"ad5621", ID_AD5621},
Michael Hennerichd8462632010-11-23 11:14:16 +0100457 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
458 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
459 {"ad5640-2500", ID_AD5640_2500},
460 {"ad5640-1250", ID_AD5640_1250},
461 {"ad5660-2500", ID_AD5660_2500},
462 {"ad5660-1250", ID_AD5660_1250},
Lars-Peter Clausen18e5ab32012-04-25 09:45:01 +0200463 {"ad5662", ID_AD5662},
Michael Hennerichb5a49482010-11-22 11:15:23 +0100464 {}
465};
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100466MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
Michael Hennerichb5a49482010-11-22 11:15:23 +0100467
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800468static int ad5446_spi_probe(struct spi_device *spi)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100469{
470 const struct spi_device_id *id = spi_get_device_id(spi);
471
472 return ad5446_probe(&spi->dev, id->name,
473 &ad5446_spi_chip_info[id->driver_data]);
474}
475
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800476static int ad5446_spi_remove(struct spi_device *spi)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100477{
478 return ad5446_remove(&spi->dev);
479}
480
481static struct spi_driver ad5446_spi_driver = {
Michael Hennerichb5a49482010-11-22 11:15:23 +0100482 .driver = {
483 .name = "ad5446",
Michael Hennerichb5a49482010-11-22 11:15:23 +0100484 .owner = THIS_MODULE,
485 },
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100486 .probe = ad5446_spi_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800487 .remove = ad5446_spi_remove,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100488 .id_table = ad5446_spi_ids,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100489};
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100490
491static int __init ad5446_spi_register_driver(void)
492{
493 return spi_register_driver(&ad5446_spi_driver);
494}
495
496static void ad5446_spi_unregister_driver(void)
497{
498 spi_unregister_driver(&ad5446_spi_driver);
499}
500
501#else
502
503static inline int ad5446_spi_register_driver(void) { return 0; }
504static inline void ad5446_spi_unregister_driver(void) { }
505
506#endif
507
508#if IS_ENABLED(CONFIG_I2C)
509
510static int ad5622_write(struct ad5446_state *st, unsigned val)
511{
512 struct i2c_client *client = to_i2c_client(st->dev);
513 __be16 data = cpu_to_be16(val);
514
515 return i2c_master_send(client, (char *)&data, sizeof(data));
516}
517
518/**
519 * ad5446_supported_i2c_device_ids:
520 * The AD5620/40/60 parts are available in different fixed internal reference
521 * voltage options. The actual part numbers may look differently
522 * (and a bit cryptic), however this style is used to make clear which
523 * parts are supported here.
524 */
525enum ad5446_supported_i2c_device_ids {
526 ID_AD5602,
527 ID_AD5612,
528 ID_AD5622,
529};
530
531static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
532 [ID_AD5602] = {
533 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
534 .write = ad5622_write,
535 },
536 [ID_AD5612] = {
537 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
538 .write = ad5622_write,
539 },
540 [ID_AD5622] = {
541 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
542 .write = ad5622_write,
543 },
544};
545
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800546static int ad5446_i2c_probe(struct i2c_client *i2c,
547 const struct i2c_device_id *id)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100548{
549 return ad5446_probe(&i2c->dev, id->name,
550 &ad5446_i2c_chip_info[id->driver_data]);
551}
552
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800553static int ad5446_i2c_remove(struct i2c_client *i2c)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100554{
555 return ad5446_remove(&i2c->dev);
556}
557
558static const struct i2c_device_id ad5446_i2c_ids[] = {
Lars-Peter Clausenbf832382012-09-04 10:10:00 +0100559 {"ad5301", ID_AD5602},
560 {"ad5311", ID_AD5612},
561 {"ad5321", ID_AD5622},
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100562 {"ad5602", ID_AD5602},
563 {"ad5612", ID_AD5612},
564 {"ad5622", ID_AD5622},
565 {}
566};
567MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
568
569static struct i2c_driver ad5446_i2c_driver = {
570 .driver = {
571 .name = "ad5446",
572 .owner = THIS_MODULE,
573 },
574 .probe = ad5446_i2c_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800575 .remove = ad5446_i2c_remove,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100576 .id_table = ad5446_i2c_ids,
577};
578
579static int __init ad5446_i2c_register_driver(void)
580{
581 return i2c_add_driver(&ad5446_i2c_driver);
582}
583
584static void __exit ad5446_i2c_unregister_driver(void)
585{
586 i2c_del_driver(&ad5446_i2c_driver);
587}
588
589#else
590
591static inline int ad5446_i2c_register_driver(void) { return 0; }
592static inline void ad5446_i2c_unregister_driver(void) { }
593
594#endif
595
596static int __init ad5446_init(void)
597{
598 int ret;
599
600 ret = ad5446_spi_register_driver();
601 if (ret)
602 return ret;
603
604 ret = ad5446_i2c_register_driver();
605 if (ret) {
606 ad5446_spi_unregister_driver();
607 return ret;
608 }
609
610 return 0;
611}
612module_init(ad5446_init);
613
614static void __exit ad5446_exit(void)
615{
616 ad5446_i2c_unregister_driver();
617 ad5446_spi_unregister_driver();
618}
619module_exit(ad5446_exit);
Michael Hennerichb5a49482010-11-22 11:15:23 +0100620
621MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
622MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
623MODULE_LICENSE("GPL v2");