blob: 97d2c5111f4389b7804bab5253262f5532b80271 [file] [log] [blame]
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +01001/*
2 * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392
3 * multi-channel Digital to Analog Converters driver
4 *
5 * Copyright 2011 Analog Devices Inc.
6 *
7 * Licensed under the GPL-2.
8 */
9
10#include <linux/device.h>
11#include <linux/err.h>
12#include <linux/i2c.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/spi/spi.h>
16#include <linux/slab.h>
17#include <linux/sysfs.h>
18#include <linux/regmap.h>
19#include <linux/regulator/consumer.h>
20
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +010023
24#define AD5380_REG_DATA(x) (((x) << 2) | 3)
25#define AD5380_REG_OFFSET(x) (((x) << 2) | 2)
26#define AD5380_REG_GAIN(x) (((x) << 2) | 1)
27#define AD5380_REG_SF_PWR_DOWN (8 << 2)
28#define AD5380_REG_SF_PWR_UP (9 << 2)
29#define AD5380_REG_SF_CTRL (12 << 2)
30
31#define AD5380_CTRL_PWR_DOWN_MODE_OFFSET 13
32#define AD5380_CTRL_INT_VREF_2V5 BIT(12)
33#define AD5380_CTRL_INT_VREF_EN BIT(10)
34
35/**
36 * struct ad5380_chip_info - chip specific information
37 * @channel_template: channel specification template
38 * @num_channels: number of channels
39 * @int_vref: internal vref in uV
40*/
41
42struct ad5380_chip_info {
43 struct iio_chan_spec channel_template;
44 unsigned int num_channels;
45 unsigned int int_vref;
46};
47
48/**
49 * struct ad5380_state - driver instance specific data
50 * @regmap: regmap instance used by the device
51 * @chip_info: chip model specific constants, available modes etc
52 * @vref_reg: vref supply regulator
53 * @vref: actual reference voltage used in uA
54 * @pwr_down: whether the chip is currently in power down mode
55 */
56
57struct ad5380_state {
58 struct regmap *regmap;
59 const struct ad5380_chip_info *chip_info;
60 struct regulator *vref_reg;
61 int vref;
62 bool pwr_down;
63};
64
65enum ad5380_type {
66 ID_AD5380_3,
67 ID_AD5380_5,
68 ID_AD5381_3,
69 ID_AD5381_5,
70 ID_AD5382_3,
71 ID_AD5382_5,
72 ID_AD5383_3,
73 ID_AD5383_5,
74 ID_AD5390_3,
75 ID_AD5390_5,
76 ID_AD5391_3,
77 ID_AD5391_5,
78 ID_AD5392_3,
79 ID_AD5392_5,
80};
81
Lars-Peter Clausen868e08622012-06-04 11:36:14 +020082static ssize_t ad5380_read_dac_powerdown(struct iio_dev *indio_dev,
83 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +010084{
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +010085 struct ad5380_state *st = iio_priv(indio_dev);
86
87 return sprintf(buf, "%d\n", st->pwr_down);
88}
89
Lars-Peter Clausen868e08622012-06-04 11:36:14 +020090static ssize_t ad5380_write_dac_powerdown(struct iio_dev *indio_dev,
91 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
92 size_t len)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +010093{
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +010094 struct ad5380_state *st = iio_priv(indio_dev);
95 bool pwr_down;
96 int ret;
97
98 ret = strtobool(buf, &pwr_down);
99 if (ret)
100 return ret;
101
102 mutex_lock(&indio_dev->mlock);
103
104 if (pwr_down)
105 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
106 else
107 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_UP, 0);
108
109 st->pwr_down = pwr_down;
110
111 mutex_unlock(&indio_dev->mlock);
112
113 return ret ? ret : len;
114}
115
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200116static const char * const ad5380_powerdown_modes[] = {
117 "100kohm_to_gnd",
118 "three_state",
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100119};
120
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200121static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
122 const struct iio_chan_spec *chan)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100123{
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100124 struct ad5380_state *st = iio_priv(indio_dev);
125 unsigned int mode;
126 int ret;
127
128 ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
129 if (ret)
130 return ret;
131
132 mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
133
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200134 return mode;
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100135}
136
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200137static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
138 const struct iio_chan_spec *chan, unsigned int mode)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100139{
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100140 struct ad5380_state *st = iio_priv(indio_dev);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100141 int ret;
142
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100143 ret = regmap_update_bits(st->regmap, AD5380_REG_SF_CTRL,
144 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200145 mode << AD5380_CTRL_PWR_DOWN_MODE_OFFSET);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100146
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200147 return ret;
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100148}
149
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200150static const struct iio_enum ad5380_powerdown_mode_enum = {
151 .items = ad5380_powerdown_modes,
152 .num_items = ARRAY_SIZE(ad5380_powerdown_modes),
153 .get = ad5380_get_powerdown_mode,
154 .set = ad5380_set_powerdown_mode,
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100155};
156
157static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
158 long info)
159{
160 switch (info) {
161 case 0:
162 return AD5380_REG_DATA(chan->address);
163 case IIO_CHAN_INFO_CALIBBIAS:
164 return AD5380_REG_OFFSET(chan->address);
165 case IIO_CHAN_INFO_CALIBSCALE:
166 return AD5380_REG_GAIN(chan->address);
167 default:
168 break;
169 }
170
171 return 0;
172}
173
174static int ad5380_write_raw(struct iio_dev *indio_dev,
175 struct iio_chan_spec const *chan, int val, int val2, long info)
176{
177 const unsigned int max_val = (1 << chan->scan_type.realbits);
178 struct ad5380_state *st = iio_priv(indio_dev);
179
180 switch (info) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +0100181 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100182 case IIO_CHAN_INFO_CALIBSCALE:
183 if (val >= max_val || val < 0)
184 return -EINVAL;
185
186 return regmap_write(st->regmap,
187 ad5380_info_to_reg(chan, info),
188 val << chan->scan_type.shift);
189 case IIO_CHAN_INFO_CALIBBIAS:
190 val += (1 << chan->scan_type.realbits) / 2;
191 if (val >= max_val || val < 0)
192 return -EINVAL;
193
194 return regmap_write(st->regmap,
195 AD5380_REG_OFFSET(chan->address),
196 val << chan->scan_type.shift);
197 default:
198 break;
199 }
200 return -EINVAL;
201}
202
203static int ad5380_read_raw(struct iio_dev *indio_dev,
204 struct iio_chan_spec const *chan, int *val, int *val2, long info)
205{
206 struct ad5380_state *st = iio_priv(indio_dev);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100207 int ret;
208
209 switch (info) {
Jonathan Cameron09f4eb42012-04-15 17:41:19 +0100210 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100211 case IIO_CHAN_INFO_CALIBSCALE:
212 ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
213 val);
214 if (ret)
215 return ret;
216 *val >>= chan->scan_type.shift;
217 return IIO_VAL_INT;
218 case IIO_CHAN_INFO_CALIBBIAS:
219 ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
220 val);
221 if (ret)
222 return ret;
223 *val >>= chan->scan_type.shift;
224 val -= (1 << chan->scan_type.realbits) / 2;
225 return IIO_VAL_INT;
226 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100227 *val = 2 * st->vref;
228 *val2 = chan->scan_type.realbits;
229 return IIO_VAL_FRACTIONAL_LOG2;
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100230 default:
231 break;
232 }
233
234 return -EINVAL;
235}
236
237static const struct iio_info ad5380_info = {
238 .read_raw = ad5380_read_raw,
239 .write_raw = ad5380_write_raw,
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100240 .driver_module = THIS_MODULE,
241};
242
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200243static struct iio_chan_spec_ext_info ad5380_ext_info[] = {
244 {
245 .name = "powerdown",
246 .read = ad5380_read_dac_powerdown,
247 .write = ad5380_write_dac_powerdown,
Jonathan Cameron37044322013-09-08 14:57:00 +0100248 .shared = IIO_SEPARATE,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200249 },
Jonathan Cameron37044322013-09-08 14:57:00 +0100250 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
251 &ad5380_powerdown_mode_enum),
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200252 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5380_powerdown_mode_enum),
253 { },
254};
255
256#define AD5380_CHANNEL(_bits) { \
257 .type = IIO_VOLTAGE, \
258 .indexed = 1, \
259 .output = 1, \
Jonathan Cameronc69e1392013-02-27 19:09:55 +0000260 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
261 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
262 BIT(IIO_CHAN_INFO_CALIBBIAS), \
263 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameronda9b1a22013-12-11 18:45:00 +0000264 .scan_type = { \
265 .sign = 'u', \
266 .realbits = (_bits), \
267 .storagebits = 16, \
268 .shift = 14 - (_bits), \
269 }, \
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200270 .ext_info = ad5380_ext_info, \
271}
272
273static const struct ad5380_chip_info ad5380_chip_info_tbl[] = {
274 [ID_AD5380_3] = {
275 .channel_template = AD5380_CHANNEL(14),
276 .num_channels = 40,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100277 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200278 },
279 [ID_AD5380_5] = {
280 .channel_template = AD5380_CHANNEL(14),
281 .num_channels = 40,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100282 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200283 },
284 [ID_AD5381_3] = {
285 .channel_template = AD5380_CHANNEL(12),
286 .num_channels = 16,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100287 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200288 },
289 [ID_AD5381_5] = {
290 .channel_template = AD5380_CHANNEL(12),
291 .num_channels = 16,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100292 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200293 },
294 [ID_AD5382_3] = {
295 .channel_template = AD5380_CHANNEL(14),
296 .num_channels = 32,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100297 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200298 },
299 [ID_AD5382_5] = {
300 .channel_template = AD5380_CHANNEL(14),
301 .num_channels = 32,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100302 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200303 },
304 [ID_AD5383_3] = {
305 .channel_template = AD5380_CHANNEL(12),
306 .num_channels = 32,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100307 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200308 },
309 [ID_AD5383_5] = {
310 .channel_template = AD5380_CHANNEL(12),
311 .num_channels = 32,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100312 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200313 },
314 [ID_AD5390_3] = {
315 .channel_template = AD5380_CHANNEL(14),
316 .num_channels = 16,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100317 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200318 },
319 [ID_AD5390_5] = {
320 .channel_template = AD5380_CHANNEL(14),
321 .num_channels = 16,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100322 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200323 },
324 [ID_AD5391_3] = {
325 .channel_template = AD5380_CHANNEL(12),
326 .num_channels = 16,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100327 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200328 },
329 [ID_AD5391_5] = {
330 .channel_template = AD5380_CHANNEL(12),
331 .num_channels = 16,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100332 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200333 },
334 [ID_AD5392_3] = {
335 .channel_template = AD5380_CHANNEL(14),
336 .num_channels = 8,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100337 .int_vref = 1250,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200338 },
339 [ID_AD5392_5] = {
340 .channel_template = AD5380_CHANNEL(14),
341 .num_channels = 8,
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100342 .int_vref = 2500,
Lars-Peter Clausen868e08622012-06-04 11:36:14 +0200343 },
344};
345
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800346static int ad5380_alloc_channels(struct iio_dev *indio_dev)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100347{
348 struct ad5380_state *st = iio_priv(indio_dev);
349 struct iio_chan_spec *channels;
350 unsigned int i;
351
Axel Lin09f993e2012-02-11 09:00:48 +0800352 channels = kcalloc(st->chip_info->num_channels,
353 sizeof(struct iio_chan_spec), GFP_KERNEL);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100354
355 if (!channels)
356 return -ENOMEM;
357
358 for (i = 0; i < st->chip_info->num_channels; ++i) {
359 channels[i] = st->chip_info->channel_template;
360 channels[i].channel = i;
361 channels[i].address = i;
362 }
363
364 indio_dev->channels = channels;
365
366 return 0;
367}
368
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800369static int ad5380_probe(struct device *dev, struct regmap *regmap,
370 enum ad5380_type type, const char *name)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100371{
372 struct iio_dev *indio_dev;
373 struct ad5380_state *st;
374 unsigned int ctrl = 0;
375 int ret;
376
Sachin Kamatc815ad32013-08-19 12:38:00 +0100377 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100378 if (indio_dev == NULL) {
379 dev_err(dev, "Failed to allocate iio device\n");
Sachin Kamatc815ad32013-08-19 12:38:00 +0100380 return -ENOMEM;
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100381 }
382
383 st = iio_priv(indio_dev);
384 dev_set_drvdata(dev, indio_dev);
385
386 st->chip_info = &ad5380_chip_info_tbl[type];
387 st->regmap = regmap;
388
389 indio_dev->dev.parent = dev;
390 indio_dev->name = name;
391 indio_dev->info = &ad5380_info;
392 indio_dev->modes = INDIO_DIRECT_MODE;
393 indio_dev->num_channels = st->chip_info->num_channels;
394
395 ret = ad5380_alloc_channels(indio_dev);
396 if (ret) {
397 dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
Sachin Kamatc815ad32013-08-19 12:38:00 +0100398 return ret;
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100399 }
400
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100401 if (st->chip_info->int_vref == 2500)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100402 ctrl |= AD5380_CTRL_INT_VREF_2V5;
403
Sachin Kamatc815ad32013-08-19 12:38:00 +0100404 st->vref_reg = devm_regulator_get(dev, "vref");
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100405 if (!IS_ERR(st->vref_reg)) {
406 ret = regulator_enable(st->vref_reg);
407 if (ret) {
408 dev_err(dev, "Failed to enable vref regulators: %d\n",
409 ret);
410 goto error_free_reg;
411 }
412
Axel Lin272cc9c2012-12-14 07:48:00 +0000413 ret = regulator_get_voltage(st->vref_reg);
414 if (ret < 0)
415 goto error_disable_reg;
416
Lars-Peter Clausen4f2c1882013-09-28 10:31:00 +0100417 st->vref = ret / 1000;
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100418 } else {
419 st->vref = st->chip_info->int_vref;
420 ctrl |= AD5380_CTRL_INT_VREF_EN;
421 }
422
423 ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl);
424 if (ret) {
425 dev_err(dev, "Failed to write to device: %d\n", ret);
426 goto error_disable_reg;
427 }
428
429 ret = iio_device_register(indio_dev);
430 if (ret) {
431 dev_err(dev, "Failed to register iio device: %d\n", ret);
432 goto error_disable_reg;
433 }
434
435 return 0;
436
437error_disable_reg:
438 if (!IS_ERR(st->vref_reg))
439 regulator_disable(st->vref_reg);
440error_free_reg:
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100441 kfree(indio_dev->channels);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100442
443 return ret;
444}
445
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800446static int ad5380_remove(struct device *dev)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100447{
448 struct iio_dev *indio_dev = dev_get_drvdata(dev);
449 struct ad5380_state *st = iio_priv(indio_dev);
450
451 iio_device_unregister(indio_dev);
452
453 kfree(indio_dev->channels);
454
455 if (!IS_ERR(st->vref_reg)) {
456 regulator_disable(st->vref_reg);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100457 }
458
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100459 return 0;
460}
461
462static bool ad5380_reg_false(struct device *dev, unsigned int reg)
463{
464 return false;
465}
466
467static const struct regmap_config ad5380_regmap_config = {
468 .reg_bits = 10,
469 .val_bits = 14,
470
471 .max_register = AD5380_REG_DATA(40),
472 .cache_type = REGCACHE_RBTREE,
473
474 .volatile_reg = ad5380_reg_false,
475 .readable_reg = ad5380_reg_false,
476};
477
478#if IS_ENABLED(CONFIG_SPI_MASTER)
479
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800480static int ad5380_spi_probe(struct spi_device *spi)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100481{
482 const struct spi_device_id *id = spi_get_device_id(spi);
483 struct regmap *regmap;
484
Axel Lind03fcfe2012-06-15 16:40:21 +0800485 regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100486
487 if (IS_ERR(regmap))
488 return PTR_ERR(regmap);
489
490 return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
491}
492
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800493static int ad5380_spi_remove(struct spi_device *spi)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100494{
495 return ad5380_remove(&spi->dev);
496}
497
498static const struct spi_device_id ad5380_spi_ids[] = {
499 { "ad5380-3", ID_AD5380_3 },
500 { "ad5380-5", ID_AD5380_5 },
501 { "ad5381-3", ID_AD5381_3 },
502 { "ad5381-5", ID_AD5381_5 },
503 { "ad5382-3", ID_AD5382_3 },
504 { "ad5382-5", ID_AD5382_5 },
505 { "ad5383-3", ID_AD5383_3 },
506 { "ad5383-5", ID_AD5383_5 },
507 { "ad5384-3", ID_AD5380_3 },
508 { "ad5384-5", ID_AD5380_5 },
509 { "ad5390-3", ID_AD5390_3 },
510 { "ad5390-5", ID_AD5390_5 },
511 { "ad5391-3", ID_AD5391_3 },
512 { "ad5391-5", ID_AD5391_5 },
513 { "ad5392-3", ID_AD5392_3 },
514 { "ad5392-5", ID_AD5392_5 },
515 { }
516};
517MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
518
519static struct spi_driver ad5380_spi_driver = {
520 .driver = {
521 .name = "ad5380",
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100522 },
523 .probe = ad5380_spi_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800524 .remove = ad5380_spi_remove,
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100525 .id_table = ad5380_spi_ids,
526};
527
528static inline int ad5380_spi_register_driver(void)
529{
530 return spi_register_driver(&ad5380_spi_driver);
531}
532
533static inline void ad5380_spi_unregister_driver(void)
534{
535 spi_unregister_driver(&ad5380_spi_driver);
536}
537
538#else
539
540static inline int ad5380_spi_register_driver(void)
541{
542 return 0;
543}
544
545static inline void ad5380_spi_unregister_driver(void)
546{
547}
548
549#endif
550
551#if IS_ENABLED(CONFIG_I2C)
552
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800553static int ad5380_i2c_probe(struct i2c_client *i2c,
554 const struct i2c_device_id *id)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100555{
556 struct regmap *regmap;
557
Axel Lind03fcfe2012-06-15 16:40:21 +0800558 regmap = devm_regmap_init_i2c(i2c, &ad5380_regmap_config);
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100559
560 if (IS_ERR(regmap))
561 return PTR_ERR(regmap);
562
563 return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name);
564}
565
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800566static int ad5380_i2c_remove(struct i2c_client *i2c)
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100567{
568 return ad5380_remove(&i2c->dev);
569}
570
571static const struct i2c_device_id ad5380_i2c_ids[] = {
572 { "ad5380-3", ID_AD5380_3 },
573 { "ad5380-5", ID_AD5380_5 },
574 { "ad5381-3", ID_AD5381_3 },
575 { "ad5381-5", ID_AD5381_5 },
576 { "ad5382-3", ID_AD5382_3 },
577 { "ad5382-5", ID_AD5382_5 },
578 { "ad5383-3", ID_AD5383_3 },
579 { "ad5383-5", ID_AD5383_5 },
580 { "ad5384-3", ID_AD5380_3 },
581 { "ad5384-5", ID_AD5380_5 },
582 { "ad5390-3", ID_AD5390_3 },
583 { "ad5390-5", ID_AD5390_5 },
584 { "ad5391-3", ID_AD5391_3 },
585 { "ad5391-5", ID_AD5391_5 },
586 { "ad5392-3", ID_AD5392_3 },
587 { "ad5392-5", ID_AD5392_5 },
588 { }
589};
590MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids);
591
592static struct i2c_driver ad5380_i2c_driver = {
593 .driver = {
594 .name = "ad5380",
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100595 },
596 .probe = ad5380_i2c_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800597 .remove = ad5380_i2c_remove,
Lars-Peter Clausenc03f2c52011-12-06 10:07:02 +0100598 .id_table = ad5380_i2c_ids,
599};
600
601static inline int ad5380_i2c_register_driver(void)
602{
603 return i2c_add_driver(&ad5380_i2c_driver);
604}
605
606static inline void ad5380_i2c_unregister_driver(void)
607{
608 i2c_del_driver(&ad5380_i2c_driver);
609}
610
611#else
612
613static inline int ad5380_i2c_register_driver(void)
614{
615 return 0;
616}
617
618static inline void ad5380_i2c_unregister_driver(void)
619{
620}
621
622#endif
623
624static int __init ad5380_spi_init(void)
625{
626 int ret;
627
628 ret = ad5380_spi_register_driver();
629 if (ret)
630 return ret;
631
632 ret = ad5380_i2c_register_driver();
633 if (ret) {
634 ad5380_spi_unregister_driver();
635 return ret;
636 }
637
638 return 0;
639}
640module_init(ad5380_spi_init);
641
642static void __exit ad5380_spi_exit(void)
643{
644 ad5380_i2c_unregister_driver();
645 ad5380_spi_unregister_driver();
646
647}
648module_exit(ad5380_spi_exit);
649
650MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
651MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
652MODULE_LICENSE("GPL v2");