2 * AD5446 SPI DAC driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
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>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
26 static void ad5446_store_sample(struct ad5446_state
*st
, unsigned val
)
28 st
->data
.d16
= cpu_to_be16(AD5446_LOAD
|
29 (val
<< st
->chip_info
->left_shift
));
32 static void ad5542_store_sample(struct ad5446_state
*st
, unsigned val
)
34 st
->data
.d16
= cpu_to_be16(val
<< st
->chip_info
->left_shift
);
37 static void ad5620_store_sample(struct ad5446_state
*st
, unsigned val
)
39 st
->data
.d16
= cpu_to_be16(AD5620_LOAD
|
40 (val
<< st
->chip_info
->left_shift
));
43 static void ad5660_store_sample(struct ad5446_state
*st
, unsigned val
)
46 st
->data
.d24
[0] = (val
>> 16) & 0xFF;
47 st
->data
.d24
[1] = (val
>> 8) & 0xFF;
48 st
->data
.d24
[2] = val
& 0xFF;
51 static void ad5620_store_pwr_down(struct ad5446_state
*st
, unsigned mode
)
53 st
->data
.d16
= cpu_to_be16(mode
<< 14);
56 static void ad5660_store_pwr_down(struct ad5446_state
*st
, unsigned mode
)
58 unsigned val
= mode
<< 16;
60 st
->data
.d24
[0] = (val
>> 16) & 0xFF;
61 st
->data
.d24
[1] = (val
>> 8) & 0xFF;
62 st
->data
.d24
[2] = val
& 0xFF;
65 static ssize_t
ad5446_write(struct device
*dev
,
66 struct device_attribute
*attr
,
70 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
71 struct ad5446_state
*st
= dev_info
->dev_data
;
75 ret
= strict_strtol(buf
, 10, &val
);
79 if (val
> RES_MASK(st
->chip_info
->bits
)) {
84 mutex_lock(&dev_info
->mlock
);
86 st
->chip_info
->store_sample(st
, val
);
87 ret
= spi_sync(st
->spi
, &st
->msg
);
88 mutex_unlock(&dev_info
->mlock
);
91 return ret
? ret
: len
;
94 static IIO_DEV_ATTR_OUT_RAW(0, ad5446_write
, 0);
96 static ssize_t
ad5446_show_scale(struct device
*dev
,
97 struct device_attribute
*attr
,
100 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
101 struct ad5446_state
*st
= iio_dev_get_devdata(dev_info
);
102 /* Corresponds to Vref / 2^(bits) */
103 unsigned int scale_uv
= (st
->vref_mv
* 1000) >> st
->chip_info
->bits
;
105 return sprintf(buf
, "%d.%03d\n", scale_uv
/ 1000, scale_uv
% 1000);
107 static IIO_DEVICE_ATTR(out_scale
, S_IRUGO
, ad5446_show_scale
, NULL
, 0);
109 static ssize_t
ad5446_show_name(struct device
*dev
,
110 struct device_attribute
*attr
,
113 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
114 struct ad5446_state
*st
= iio_dev_get_devdata(dev_info
);
116 return sprintf(buf
, "%s\n", spi_get_device_id(st
->spi
)->name
);
118 static IIO_DEVICE_ATTR(name
, S_IRUGO
, ad5446_show_name
, NULL
, 0);
120 static ssize_t
ad5446_write_powerdown_mode(struct device
*dev
,
121 struct device_attribute
*attr
,
122 const char *buf
, size_t len
)
124 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
125 struct ad5446_state
*st
= dev_info
->dev_data
;
127 if (sysfs_streq(buf
, "1kohm_to_gnd"))
128 st
->pwr_down_mode
= MODE_PWRDWN_1k
;
129 else if (sysfs_streq(buf
, "100kohm_to_gnd"))
130 st
->pwr_down_mode
= MODE_PWRDWN_100k
;
131 else if (sysfs_streq(buf
, "three_state"))
132 st
->pwr_down_mode
= MODE_PWRDWN_TRISTATE
;
139 static ssize_t
ad5446_read_powerdown_mode(struct device
*dev
,
140 struct device_attribute
*attr
, char *buf
)
142 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
143 struct ad5446_state
*st
= dev_info
->dev_data
;
145 char mode
[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
147 return sprintf(buf
, "%s\n", mode
[st
->pwr_down_mode
]);
150 static ssize_t
ad5446_read_dac_powerdown(struct device
*dev
,
151 struct device_attribute
*attr
,
154 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
155 struct ad5446_state
*st
= dev_info
->dev_data
;
157 return sprintf(buf
, "%d\n", st
->pwr_down
);
160 static ssize_t
ad5446_write_dac_powerdown(struct device
*dev
,
161 struct device_attribute
*attr
,
162 const char *buf
, size_t len
)
164 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
165 struct ad5446_state
*st
= dev_info
->dev_data
;
166 unsigned long readin
;
169 ret
= strict_strtol(buf
, 10, &readin
);
176 mutex_lock(&dev_info
->mlock
);
177 st
->pwr_down
= readin
;
180 st
->chip_info
->store_pwr_down(st
, st
->pwr_down_mode
);
182 st
->chip_info
->store_sample(st
, st
->cached_val
);
184 ret
= spi_sync(st
->spi
, &st
->msg
);
185 mutex_unlock(&dev_info
->mlock
);
187 return ret
? ret
: len
;
190 static IIO_DEVICE_ATTR(out_powerdown_mode
, S_IRUGO
| S_IWUSR
,
191 ad5446_read_powerdown_mode
,
192 ad5446_write_powerdown_mode
, 0);
194 static IIO_CONST_ATTR(out_powerdown_mode_available
,
195 "1kohm_to_gnd 100kohm_to_gnd three_state");
197 static IIO_DEVICE_ATTR(out0_powerdown
, S_IRUGO
| S_IWUSR
,
198 ad5446_read_dac_powerdown
,
199 ad5446_write_dac_powerdown
, 0);
201 static struct attribute
*ad5446_attributes
[] = {
202 &iio_dev_attr_out0_raw
.dev_attr
.attr
,
203 &iio_dev_attr_out_scale
.dev_attr
.attr
,
204 &iio_dev_attr_out0_powerdown
.dev_attr
.attr
,
205 &iio_dev_attr_out_powerdown_mode
.dev_attr
.attr
,
206 &iio_const_attr_out_powerdown_mode_available
.dev_attr
.attr
,
207 &iio_dev_attr_name
.dev_attr
.attr
,
211 static mode_t
ad5446_attr_is_visible(struct kobject
*kobj
,
212 struct attribute
*attr
, int n
)
214 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
215 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
216 struct ad5446_state
*st
= iio_dev_get_devdata(dev_info
);
218 mode_t mode
= attr
->mode
;
220 if (!st
->chip_info
->store_pwr_down
&&
221 (attr
== &iio_dev_attr_out0_powerdown
.dev_attr
.attr
||
222 attr
== &iio_dev_attr_out_powerdown_mode
.dev_attr
.attr
||
224 &iio_const_attr_out_powerdown_mode_available
.dev_attr
.attr
))
230 static const struct attribute_group ad5446_attribute_group
= {
231 .attrs
= ad5446_attributes
,
232 .is_visible
= ad5446_attr_is_visible
,
235 static const struct ad5446_chip_info ad5446_chip_info_tbl
[] = {
240 .store_sample
= ad5446_store_sample
,
246 .store_sample
= ad5446_store_sample
,
252 .store_sample
= ad5542_store_sample
,
258 .store_sample
= ad5542_store_sample
,
264 .store_sample
= ad5542_store_sample
,
270 .store_sample
= ad5542_store_sample
,
276 .store_sample
= ad5542_store_sample
,
282 .store_sample
= ad5542_store_sample
,
283 .store_pwr_down
= ad5620_store_pwr_down
,
289 .store_sample
= ad5542_store_sample
,
290 .store_pwr_down
= ad5620_store_pwr_down
,
296 .store_sample
= ad5542_store_sample
,
297 .store_pwr_down
= ad5620_store_pwr_down
,
304 .store_sample
= ad5620_store_sample
,
305 .store_pwr_down
= ad5620_store_pwr_down
,
312 .store_sample
= ad5620_store_sample
,
313 .store_pwr_down
= ad5620_store_pwr_down
,
320 .store_sample
= ad5620_store_sample
,
321 .store_pwr_down
= ad5620_store_pwr_down
,
328 .store_sample
= ad5620_store_sample
,
329 .store_pwr_down
= ad5620_store_pwr_down
,
336 .store_sample
= ad5660_store_sample
,
337 .store_pwr_down
= ad5660_store_pwr_down
,
344 .store_sample
= ad5660_store_sample
,
345 .store_pwr_down
= ad5660_store_pwr_down
,
349 static int __devinit
ad5446_probe(struct spi_device
*spi
)
351 struct ad5446_state
*st
;
352 int ret
, voltage_uv
= 0;
354 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
360 st
->reg
= regulator_get(&spi
->dev
, "vcc");
361 if (!IS_ERR(st
->reg
)) {
362 ret
= regulator_enable(st
->reg
);
366 voltage_uv
= regulator_get_voltage(st
->reg
);
370 &ad5446_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
372 spi_set_drvdata(spi
, st
);
376 st
->indio_dev
= iio_allocate_device(0);
377 if (st
->indio_dev
== NULL
) {
379 goto error_disable_reg
;
382 /* Estabilish that the iio_dev is a child of the spi device */
383 st
->indio_dev
->dev
.parent
= &spi
->dev
;
384 st
->indio_dev
->attrs
= &ad5446_attribute_group
;
385 st
->indio_dev
->dev_data
= (void *)(st
);
386 st
->indio_dev
->driver_module
= THIS_MODULE
;
387 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
389 /* Setup default message */
391 st
->xfer
.tx_buf
= &st
->data
;
392 st
->xfer
.len
= st
->chip_info
->storagebits
/ 8;
394 spi_message_init(&st
->msg
);
395 spi_message_add_tail(&st
->xfer
, &st
->msg
);
397 switch (spi_get_device_id(spi
)->driver_data
) {
404 st
->vref_mv
= st
->chip_info
->int_vref_mv
;
408 st
->vref_mv
= voltage_uv
/ 1000;
411 "reference voltage unspecified\n");
414 ret
= iio_device_register(st
->indio_dev
);
416 goto error_free_device
;
421 iio_free_device(st
->indio_dev
);
423 if (!IS_ERR(st
->reg
))
424 regulator_disable(st
->reg
);
426 if (!IS_ERR(st
->reg
))
427 regulator_put(st
->reg
);
433 static int ad5446_remove(struct spi_device
*spi
)
435 struct ad5446_state
*st
= spi_get_drvdata(spi
);
436 struct iio_dev
*indio_dev
= st
->indio_dev
;
438 iio_device_unregister(indio_dev
);
439 if (!IS_ERR(st
->reg
)) {
440 regulator_disable(st
->reg
);
441 regulator_put(st
->reg
);
447 static const struct spi_device_id ad5446_id
[] = {
448 {"ad5444", ID_AD5444
},
449 {"ad5446", ID_AD5446
},
450 {"ad5512a", ID_AD5512A
},
451 {"ad5541a", ID_AD5541A
},
452 {"ad5542a", ID_AD5542A
},
453 {"ad5543", ID_AD5543
},
454 {"ad5553", ID_AD5553
},
455 {"ad5601", ID_AD5601
},
456 {"ad5611", ID_AD5611
},
457 {"ad5621", ID_AD5621
},
458 {"ad5620-2500", ID_AD5620_2500
}, /* AD5620/40/60: */
459 {"ad5620-1250", ID_AD5620_1250
}, /* part numbers may look differently */
460 {"ad5640-2500", ID_AD5640_2500
},
461 {"ad5640-1250", ID_AD5640_1250
},
462 {"ad5660-2500", ID_AD5660_2500
},
463 {"ad5660-1250", ID_AD5660_1250
},
467 static struct spi_driver ad5446_driver
= {
470 .bus
= &spi_bus_type
,
471 .owner
= THIS_MODULE
,
473 .probe
= ad5446_probe
,
474 .remove
= __devexit_p(ad5446_remove
),
475 .id_table
= ad5446_id
,
478 static int __init
ad5446_init(void)
480 return spi_register_driver(&ad5446_driver
);
482 module_init(ad5446_init
);
484 static void __exit
ad5446_exit(void)
486 spi_unregister_driver(&ad5446_driver
);
488 module_exit(ad5446_exit
);
490 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
491 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
492 MODULE_LICENSE("GPL v2");
493 MODULE_ALIAS("spi:ad5446");