blob: 1263b0e5ad84fda9dfdd841bbc23498d19918937 [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,
Jonathan Cameron37044322013-09-08 14:57:00 +0100135 .shared = IIO_SEPARATE,
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200136 },
Jonathan Cameron37044322013-09-08 14:57:00 +0100137 IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5446_powerdown_mode_enum),
Lars-Peter Clausen09d48aa2012-06-04 11:36:13 +0200138 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum),
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200139 { },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100140};
141
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200142#define _AD5446_CHANNEL(bits, storage, shift, ext) { \
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100143 .type = IIO_VOLTAGE, \
144 .indexed = 1, \
145 .output = 1, \
146 .channel = 0, \
Jonathan Cameron2f6a4a42013-02-27 19:10:35 +0000147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
148 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200149 .scan_type = IIO_ST('u', (bits), (storage), (shift)), \
150 .ext_info = (ext), \
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100151}
152
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200153#define AD5446_CHANNEL(bits, storage, shift) \
154 _AD5446_CHANNEL(bits, storage, shift, NULL)
155
156#define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100157 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200158
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100159static int ad5446_read_raw(struct iio_dev *indio_dev,
160 struct iio_chan_spec const *chan,
161 int *val,
162 int *val2,
163 long m)
164{
165 struct ad5446_state *st = iio_priv(indio_dev);
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100166
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:
Lars-Peter Clausen0a99b602013-09-28 10:31:00 +0100172 *val = st->vref_mv;
173 *val2 = chan->scan_type.realbits;
174 return IIO_VAL_FRACTIONAL_LOG2;
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100175 }
176 return -EINVAL;
177}
178
179static int ad5446_write_raw(struct iio_dev *indio_dev,
180 struct iio_chan_spec const *chan,
181 int val,
182 int val2,
183 long mask)
184{
185 struct ad5446_state *st = iio_priv(indio_dev);
186 int ret = 0;
187
188 switch (mask) {
189 case IIO_CHAN_INFO_RAW:
190 if (val >= (1 << chan->scan_type.realbits) || val < 0)
191 return -EINVAL;
192
193 val <<= chan->scan_type.shift;
194 mutex_lock(&indio_dev->mlock);
195 st->cached_val = val;
196 if (!st->pwr_down)
197 ret = st->chip_info->write(st, val);
198 mutex_unlock(&indio_dev->mlock);
199 break;
200 default:
201 ret = -EINVAL;
202 }
203
204 return ret;
205}
206
207static const struct iio_info ad5446_info = {
208 .read_raw = ad5446_read_raw,
209 .write_raw = ad5446_write_raw,
210 .driver_module = THIS_MODULE,
211};
212
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800213static int ad5446_probe(struct device *dev, const char *name,
214 const struct ad5446_chip_info *chip_info)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100215{
216 struct ad5446_state *st;
217 struct iio_dev *indio_dev;
218 struct regulator *reg;
219 int ret, voltage_uv = 0;
220
Sachin Kamatba727292013-08-19 12:38:00 +0100221 reg = devm_regulator_get(dev, "vcc");
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100222 if (!IS_ERR(reg)) {
223 ret = regulator_enable(reg);
224 if (ret)
Sachin Kamatba727292013-08-19 12:38:00 +0100225 return ret;
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100226
Axel Lin13e57ee2012-12-18 03:33:00 +0000227 ret = regulator_get_voltage(reg);
228 if (ret < 0)
229 goto error_disable_reg;
230
231 voltage_uv = ret;
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100232 }
233
Sachin Kamatba727292013-08-19 12:38:00 +0100234 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100235 if (indio_dev == NULL) {
236 ret = -ENOMEM;
237 goto error_disable_reg;
238 }
239 st = iio_priv(indio_dev);
240 st->chip_info = chip_info;
241
242 dev_set_drvdata(dev, indio_dev);
243 st->reg = reg;
244 st->dev = dev;
245
246 /* Establish that the iio_dev is a child of the device */
247 indio_dev->dev.parent = dev;
248 indio_dev->name = name;
249 indio_dev->info = &ad5446_info;
250 indio_dev->modes = INDIO_DIRECT_MODE;
251 indio_dev->channels = &st->chip_info->channel;
252 indio_dev->num_channels = 1;
253
254 st->pwr_down_mode = MODE_PWRDWN_1k;
255
256 if (st->chip_info->int_vref_mv)
257 st->vref_mv = st->chip_info->int_vref_mv;
258 else if (voltage_uv)
259 st->vref_mv = voltage_uv / 1000;
260 else
261 dev_warn(dev, "reference voltage unspecified\n");
262
263 ret = iio_device_register(indio_dev);
264 if (ret)
Sachin Kamatba727292013-08-19 12:38:00 +0100265 goto error_disable_reg;
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100266
267 return 0;
268
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100269error_disable_reg:
270 if (!IS_ERR(reg))
271 regulator_disable(reg);
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100272 return ret;
273}
274
275static int ad5446_remove(struct device *dev)
276{
277 struct iio_dev *indio_dev = dev_get_drvdata(dev);
278 struct ad5446_state *st = iio_priv(indio_dev);
279
280 iio_device_unregister(indio_dev);
Sachin Kamatba727292013-08-19 12:38:00 +0100281 if (!IS_ERR(st->reg))
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100282 regulator_disable(st->reg);
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100283
284 return 0;
285}
286
287#if IS_ENABLED(CONFIG_SPI_MASTER)
288
289static int ad5446_write(struct ad5446_state *st, unsigned val)
290{
291 struct spi_device *spi = to_spi_device(st->dev);
292 __be16 data = cpu_to_be16(val);
293
294 return spi_write(spi, &data, sizeof(data));
295}
296
297static int ad5660_write(struct ad5446_state *st, unsigned val)
298{
299 struct spi_device *spi = to_spi_device(st->dev);
300 uint8_t data[3];
301
302 data[0] = (val >> 16) & 0xFF;
303 data[1] = (val >> 8) & 0xFF;
304 data[2] = val & 0xFF;
305
306 return spi_write(spi, data, sizeof(data));
307}
308
309/**
310 * ad5446_supported_spi_device_ids:
311 * The AD5620/40/60 parts are available in different fixed internal reference
312 * voltage options. The actual part numbers may look differently
313 * (and a bit cryptic), however this style is used to make clear which
314 * parts are supported here.
315 */
316enum ad5446_supported_spi_device_ids {
Lars-Peter Clausen2fafbce2012-09-04 10:10:00 +0100317 ID_AD5300,
318 ID_AD5310,
319 ID_AD5320,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100320 ID_AD5444,
321 ID_AD5446,
322 ID_AD5450,
323 ID_AD5451,
324 ID_AD5541A,
325 ID_AD5512A,
326 ID_AD5553,
327 ID_AD5601,
328 ID_AD5611,
329 ID_AD5621,
Aida Mynzhasova4fa2a9e2013-09-23 15:19:00 +0100330 ID_AD5641,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100331 ID_AD5620_2500,
332 ID_AD5620_1250,
333 ID_AD5640_2500,
334 ID_AD5640_1250,
335 ID_AD5660_2500,
336 ID_AD5660_1250,
337 ID_AD5662,
338};
339
340static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
Lars-Peter Clausen2fafbce2012-09-04 10:10:00 +0100341 [ID_AD5300] = {
342 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
343 .write = ad5446_write,
344 },
345 [ID_AD5310] = {
346 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
347 .write = ad5446_write,
348 },
349 [ID_AD5320] = {
350 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
351 .write = ad5446_write,
352 },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100353 [ID_AD5444] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100354 .channel = AD5446_CHANNEL(12, 16, 2),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200355 .write = ad5446_write,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100356 },
357 [ID_AD5446] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100358 .channel = AD5446_CHANNEL(14, 16, 0),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200359 .write = ad5446_write,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100360 },
Lars-Peter Clausen779c0c42012-06-26 10:45:43 +0200361 [ID_AD5450] = {
362 .channel = AD5446_CHANNEL(8, 16, 6),
363 .write = ad5446_write,
364 },
365 [ID_AD5451] = {
366 .channel = AD5446_CHANNEL(10, 16, 4),
367 .write = ad5446_write,
368 },
Michael Hennerich67d1c1f42011-04-15 13:51:07 +0200369 [ID_AD5541A] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100370 .channel = AD5446_CHANNEL(16, 16, 0),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200371 .write = ad5446_write,
Michael Hennerich67d1c1f42011-04-15 13:51:07 +0200372 },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100373 [ID_AD5512A] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100374 .channel = AD5446_CHANNEL(12, 16, 4),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200375 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100376 },
Michael Hennerich07722682011-02-23 10:45:47 +0100377 [ID_AD5553] = {
Lars-Peter Clausen33ad6b22011-11-15 16:31:22 +0100378 .channel = AD5446_CHANNEL(14, 16, 0),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200379 .write = ad5446_write,
Michael Hennerich07722682011-02-23 10:45:47 +0100380 },
Michael Hennerich2b615352011-03-10 13:26:48 +0100381 [ID_AD5601] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200382 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200383 .write = ad5446_write,
Michael Hennerich2b615352011-03-10 13:26:48 +0100384 },
385 [ID_AD5611] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200386 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200387 .write = ad5446_write,
Michael Hennerich2b615352011-03-10 13:26:48 +0100388 },
389 [ID_AD5621] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200390 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200391 .write = ad5446_write,
Michael Hennerich2b615352011-03-10 13:26:48 +0100392 },
Aida Mynzhasova4fa2a9e2013-09-23 15:19:00 +0100393 [ID_AD5641] = {
394 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
395 .write = ad5446_write,
396 },
Michael Hennerichd8462632010-11-23 11:14:16 +0100397 [ID_AD5620_2500] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200398 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
Michael Hennerichd8462632010-11-23 11:14:16 +0100399 .int_vref_mv = 2500,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200400 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100401 },
402 [ID_AD5620_1250] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200403 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
Michael Hennerichd8462632010-11-23 11:14:16 +0100404 .int_vref_mv = 1250,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200405 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100406 },
407 [ID_AD5640_2500] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200408 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100409 .int_vref_mv = 2500,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200410 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100411 },
412 [ID_AD5640_1250] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200413 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100414 .int_vref_mv = 1250,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200415 .write = ad5446_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100416 },
417 [ID_AD5660_2500] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200418 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100419 .int_vref_mv = 2500,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200420 .write = ad5660_write,
Michael Hennerichd8462632010-11-23 11:14:16 +0100421 },
422 [ID_AD5660_1250] = {
Lars-Peter Clausen83f0f572012-04-25 09:44:58 +0200423 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
Michael Hennerichd8462632010-11-23 11:14:16 +0100424 .int_vref_mv = 1250,
Lars-Peter Clausencae329e2012-04-25 09:44:59 +0200425 .write = ad5660_write,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100426 },
Lars-Peter Clausen18e5ab32012-04-25 09:45:01 +0200427 [ID_AD5662] = {
428 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
429 .write = ad5660_write,
430 },
Michael Hennerichb5a49482010-11-22 11:15:23 +0100431};
432
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100433static const struct spi_device_id ad5446_spi_ids[] = {
Lars-Peter Clausen2fafbce2012-09-04 10:10:00 +0100434 {"ad5300", ID_AD5300},
435 {"ad5310", ID_AD5310},
436 {"ad5320", ID_AD5320},
Michael Hennerichb5a49482010-11-22 11:15:23 +0100437 {"ad5444", ID_AD5444},
438 {"ad5446", ID_AD5446},
Lars-Peter Clausen779c0c42012-06-26 10:45:43 +0200439 {"ad5450", ID_AD5450},
440 {"ad5451", ID_AD5451},
441 {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
442 {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
Michael Hennerichb5a49482010-11-22 11:15:23 +0100443 {"ad5512a", ID_AD5512A},
Michael Hennerich67d1c1f42011-04-15 13:51:07 +0200444 {"ad5541a", ID_AD5541A},
Lars-Peter Clausen11a7df42012-04-25 09:44:57 +0200445 {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
446 {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
Michael Hennerichbd51c0b2011-03-10 13:26:46 +0100447 {"ad5553", ID_AD5553},
Michael Hennerich2b615352011-03-10 13:26:48 +0100448 {"ad5601", ID_AD5601},
449 {"ad5611", ID_AD5611},
450 {"ad5621", ID_AD5621},
Aida Mynzhasova4fa2a9e2013-09-23 15:19:00 +0100451 {"ad5641", ID_AD5641},
Michael Hennerichd8462632010-11-23 11:14:16 +0100452 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
453 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
454 {"ad5640-2500", ID_AD5640_2500},
455 {"ad5640-1250", ID_AD5640_1250},
456 {"ad5660-2500", ID_AD5660_2500},
457 {"ad5660-1250", ID_AD5660_1250},
Lars-Peter Clausen18e5ab32012-04-25 09:45:01 +0200458 {"ad5662", ID_AD5662},
Michael Hennerichb5a49482010-11-22 11:15:23 +0100459 {}
460};
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100461MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
Michael Hennerichb5a49482010-11-22 11:15:23 +0100462
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800463static int ad5446_spi_probe(struct spi_device *spi)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100464{
465 const struct spi_device_id *id = spi_get_device_id(spi);
466
467 return ad5446_probe(&spi->dev, id->name,
468 &ad5446_spi_chip_info[id->driver_data]);
469}
470
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800471static int ad5446_spi_remove(struct spi_device *spi)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100472{
473 return ad5446_remove(&spi->dev);
474}
475
476static struct spi_driver ad5446_spi_driver = {
Michael Hennerichb5a49482010-11-22 11:15:23 +0100477 .driver = {
478 .name = "ad5446",
Michael Hennerichb5a49482010-11-22 11:15:23 +0100479 .owner = THIS_MODULE,
480 },
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100481 .probe = ad5446_spi_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800482 .remove = ad5446_spi_remove,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100483 .id_table = ad5446_spi_ids,
Michael Hennerichb5a49482010-11-22 11:15:23 +0100484};
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100485
486static int __init ad5446_spi_register_driver(void)
487{
488 return spi_register_driver(&ad5446_spi_driver);
489}
490
491static void ad5446_spi_unregister_driver(void)
492{
493 spi_unregister_driver(&ad5446_spi_driver);
494}
495
496#else
497
498static inline int ad5446_spi_register_driver(void) { return 0; }
499static inline void ad5446_spi_unregister_driver(void) { }
500
501#endif
502
503#if IS_ENABLED(CONFIG_I2C)
504
505static int ad5622_write(struct ad5446_state *st, unsigned val)
506{
507 struct i2c_client *client = to_i2c_client(st->dev);
508 __be16 data = cpu_to_be16(val);
509
510 return i2c_master_send(client, (char *)&data, sizeof(data));
511}
512
513/**
514 * ad5446_supported_i2c_device_ids:
515 * The AD5620/40/60 parts are available in different fixed internal reference
516 * voltage options. The actual part numbers may look differently
517 * (and a bit cryptic), however this style is used to make clear which
518 * parts are supported here.
519 */
520enum ad5446_supported_i2c_device_ids {
521 ID_AD5602,
522 ID_AD5612,
523 ID_AD5622,
524};
525
526static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
527 [ID_AD5602] = {
528 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
529 .write = ad5622_write,
530 },
531 [ID_AD5612] = {
532 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
533 .write = ad5622_write,
534 },
535 [ID_AD5622] = {
536 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
537 .write = ad5622_write,
538 },
539};
540
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800541static int ad5446_i2c_probe(struct i2c_client *i2c,
542 const struct i2c_device_id *id)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100543{
544 return ad5446_probe(&i2c->dev, id->name,
545 &ad5446_i2c_chip_info[id->driver_data]);
546}
547
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800548static int ad5446_i2c_remove(struct i2c_client *i2c)
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100549{
550 return ad5446_remove(&i2c->dev);
551}
552
553static const struct i2c_device_id ad5446_i2c_ids[] = {
Lars-Peter Clausenbf832382012-09-04 10:10:00 +0100554 {"ad5301", ID_AD5602},
555 {"ad5311", ID_AD5612},
556 {"ad5321", ID_AD5622},
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100557 {"ad5602", ID_AD5602},
558 {"ad5612", ID_AD5612},
559 {"ad5622", ID_AD5622},
560 {}
561};
562MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
563
564static struct i2c_driver ad5446_i2c_driver = {
565 .driver = {
566 .name = "ad5446",
567 .owner = THIS_MODULE,
568 },
569 .probe = ad5446_i2c_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800570 .remove = ad5446_i2c_remove,
Jean-Francois Dagenais3ec36a22012-08-21 15:28:00 +0100571 .id_table = ad5446_i2c_ids,
572};
573
574static int __init ad5446_i2c_register_driver(void)
575{
576 return i2c_add_driver(&ad5446_i2c_driver);
577}
578
579static void __exit ad5446_i2c_unregister_driver(void)
580{
581 i2c_del_driver(&ad5446_i2c_driver);
582}
583
584#else
585
586static inline int ad5446_i2c_register_driver(void) { return 0; }
587static inline void ad5446_i2c_unregister_driver(void) { }
588
589#endif
590
591static int __init ad5446_init(void)
592{
593 int ret;
594
595 ret = ad5446_spi_register_driver();
596 if (ret)
597 return ret;
598
599 ret = ad5446_i2c_register_driver();
600 if (ret) {
601 ad5446_spi_unregister_driver();
602 return ret;
603 }
604
605 return 0;
606}
607module_init(ad5446_init);
608
609static void __exit ad5446_exit(void)
610{
611 ad5446_i2c_unregister_driver();
612 ad5446_spi_unregister_driver();
613}
614module_exit(ad5446_exit);
Michael Hennerichb5a49482010-11-22 11:15:23 +0100615
616MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
617MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
618MODULE_LICENSE("GPL v2");