2 * ad2s1210.c support for the ADI Resolver to Digital Converters: AD2S1210
4 * Copyright (c) 2010-2010 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/types.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
24 #define DRV_NAME "ad2s1210"
26 #define AD2S1210_DEF_CONTROL 0x7E
28 #define AD2S1210_MSB_IS_HIGH 0x80
29 #define AD2S1210_MSB_IS_LOW 0x7F
30 #define AD2S1210_PHASE_LOCK_RANGE_44 0x20
31 #define AD2S1210_ENABLE_HYSTERESIS 0x10
32 #define AD2S1210_SET_ENRES1 0x08
33 #define AD2S1210_SET_ENRES0 0x04
34 #define AD2S1210_SET_RES1 0x02
35 #define AD2S1210_SET_RES0 0x01
37 #define AD2S1210_SET_ENRESOLUTION (AD2S1210_SET_ENRES1 | \
39 #define AD2S1210_SET_RESOLUTION (AD2S1210_SET_RES1 | AD2S1210_SET_RES0)
41 #define AD2S1210_REG_POSITION 0x80
42 #define AD2S1210_REG_VELOCITY 0x82
43 #define AD2S1210_REG_LOS_THRD 0x88
44 #define AD2S1210_REG_DOS_OVR_THRD 0x89
45 #define AD2S1210_REG_DOS_MIS_THRD 0x8A
46 #define AD2S1210_REG_DOS_RST_MAX_THRD 0x8B
47 #define AD2S1210_REG_DOS_RST_MIN_THRD 0x8C
48 #define AD2S1210_REG_LOT_HIGH_THRD 0x8D
49 #define AD2S1210_REG_LOT_LOW_THRD 0x8E
50 #define AD2S1210_REG_EXCIT_FREQ 0x91
51 #define AD2S1210_REG_CONTROL 0x92
52 #define AD2S1210_REG_SOFT_RESET 0xF0
53 #define AD2S1210_REG_FAULT 0xFF
55 /* pin SAMPLE, A0, A1, RES0, RES1, is controlled by driver */
56 #define AD2S1210_SAA 3
57 #define AD2S1210_PN (AD2S1210_SAA + AD2S1210_RES)
59 #define AD2S1210_MIN_CLKIN 6144000
60 #define AD2S1210_MAX_CLKIN 10240000
61 #define AD2S1210_MIN_EXCIT 2000
62 #define AD2S1210_MAX_EXCIT 20000
63 #define AD2S1210_MIN_FCW 0x4
64 #define AD2S1210_MAX_FCW 0x50
66 /* default input clock on serial interface */
67 #define AD2S1210_DEF_CLKIN 8192000
68 /* clock period in nano second */
69 #define AD2S1210_DEF_TCK (1000000000/AD2S1210_DEF_CLKIN)
70 #define AD2S1210_DEF_EXCIT 10000
79 static const unsigned int ad2s1210_resolution_value
[] = { 10, 12, 14, 16 };
81 struct ad2s1210_state
{
82 const struct ad2s1210_platform_data
*pdata
;
84 struct spi_device
*sdev
;
90 enum ad2s1210_mode mode
;
91 u8 rx
[2] ____cacheline_aligned
;
92 u8 tx
[2] ____cacheline_aligned
;
95 static const int ad2s1210_mode_vals
[4][2] = {
98 [MOD_CONFIG
] = { 1, 0 },
100 static inline void ad2s1210_set_mode(enum ad2s1210_mode mode
,
101 struct ad2s1210_state
*st
)
103 gpio_set_value(st
->pdata
->a
[0], ad2s1210_mode_vals
[mode
][0]);
104 gpio_set_value(st
->pdata
->a
[1], ad2s1210_mode_vals
[mode
][1]);
108 /* write 1 bytes (address or data) to the chip */
109 static int ad2s1210_config_write(struct ad2s1210_state
*st
, u8 data
)
113 ad2s1210_set_mode(MOD_CONFIG
, st
);
115 ret
= spi_write(st
->sdev
, st
->tx
, 1);
123 /* read value from one of the registers */
124 static int ad2s1210_config_read(struct ad2s1210_state
*st
,
125 unsigned char address
)
127 struct spi_transfer xfer
= {
132 struct spi_message msg
;
135 ad2s1210_set_mode(MOD_CONFIG
, st
);
136 spi_message_init(&msg
);
137 spi_message_add_tail(&xfer
, &msg
);
138 st
->tx
[0] = address
| AD2S1210_MSB_IS_HIGH
;
139 st
->tx
[1] = AD2S1210_REG_FAULT
;
140 ret
= spi_sync(st
->sdev
, &msg
);
149 int ad2s1210_update_frequency_control_word(struct ad2s1210_state
*st
)
154 fcw
= (unsigned char)(st
->fexcit
* (1 << 15) / st
->fclkin
);
155 if (fcw
< AD2S1210_MIN_FCW
|| fcw
> AD2S1210_MAX_FCW
) {
156 pr_err("ad2s1210: FCW out of range\n");
160 ret
= ad2s1210_config_write(st
, AD2S1210_REG_EXCIT_FREQ
);
164 return ad2s1210_config_write(st
, fcw
);
167 static unsigned char ad2s1210_read_resolution_pin(struct ad2s1210_state
*st
)
169 return ad2s1210_resolution_value
[
170 (gpio_get_value(st
->pdata
->res
[0]) << 1) |
171 gpio_get_value(st
->pdata
->res
[1])];
174 static const int ad2s1210_res_pins
[4][2] = {
175 { 0, 0 }, {0, 1}, {1, 0}, {1, 1}
178 static inline void ad2s1210_set_resolution_pin(struct ad2s1210_state
*st
)
180 gpio_set_value(st
->pdata
->res
[0],
181 ad2s1210_res_pins
[(st
->resolution
- 10)/2][0]);
182 gpio_set_value(st
->pdata
->res
[1],
183 ad2s1210_res_pins
[(st
->resolution
- 10)/2][1]);
186 static inline int ad2s1210_soft_reset(struct ad2s1210_state
*st
)
190 ret
= ad2s1210_config_write(st
, AD2S1210_REG_SOFT_RESET
);
194 return ad2s1210_config_write(st
, 0x0);
198 /* return the OLD DATA since last spi bus write */
199 static ssize_t
ad2s1210_show_raw(struct device
*dev
,
200 struct device_attribute
*attr
,
203 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
206 mutex_lock(&st
->lock
);
208 ret
= sprintf(buf
, "0x%x\n", st
->rx
[0]);
209 st
->old_data
= false;
211 mutex_unlock(&st
->lock
);
216 static ssize_t
ad2s1210_store_raw(struct device
*dev
,
217 struct device_attribute
*attr
,
221 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
226 ret
= strict_strtoul(buf
, 16, &udata
);
231 mutex_lock(&st
->lock
);
232 ret
= ad2s1210_config_write(st
, data
);
233 mutex_unlock(&st
->lock
);
235 return ret
< 0 ? ret
: len
;
238 static ssize_t
ad2s1210_store_softreset(struct device
*dev
,
239 struct device_attribute
*attr
,
243 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
246 mutex_lock(&st
->lock
);
247 ret
= ad2s1210_soft_reset(st
);
248 mutex_unlock(&st
->lock
);
250 return ret
< 0 ? ret
: len
;
253 static ssize_t
ad2s1210_show_fclkin(struct device
*dev
,
254 struct device_attribute
*attr
,
257 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
258 return sprintf(buf
, "%d\n", st
->fclkin
);
261 static ssize_t
ad2s1210_store_fclkin(struct device
*dev
,
262 struct device_attribute
*attr
,
266 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
267 unsigned long fclkin
;
270 ret
= strict_strtoul(buf
, 10, &fclkin
);
273 if (fclkin
< AD2S1210_MIN_CLKIN
|| fclkin
> AD2S1210_MAX_CLKIN
) {
274 pr_err("ad2s1210: fclkin out of range\n");
278 mutex_lock(&st
->lock
);
281 ret
= ad2s1210_update_frequency_control_word(st
);
284 ret
= ad2s1210_soft_reset(st
);
286 mutex_unlock(&st
->lock
);
288 return ret
< 0 ? ret
: len
;
291 static ssize_t
ad2s1210_show_fexcit(struct device
*dev
,
292 struct device_attribute
*attr
,
295 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
296 return sprintf(buf
, "%d\n", st
->fexcit
);
299 static ssize_t
ad2s1210_store_fexcit(struct device
*dev
,
300 struct device_attribute
*attr
,
301 const char *buf
, size_t len
)
303 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
304 unsigned long fexcit
;
307 ret
= strict_strtoul(buf
, 10, &fexcit
);
310 if (fexcit
< AD2S1210_MIN_EXCIT
|| fexcit
> AD2S1210_MAX_EXCIT
) {
311 pr_err("ad2s1210: excitation frequency out of range\n");
314 mutex_lock(&st
->lock
);
316 ret
= ad2s1210_update_frequency_control_word(st
);
319 ret
= ad2s1210_soft_reset(st
);
321 mutex_unlock(&st
->lock
);
323 return ret
< 0 ? ret
: len
;
326 static ssize_t
ad2s1210_show_control(struct device
*dev
,
327 struct device_attribute
*attr
,
330 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
332 mutex_lock(&st
->lock
);
333 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
334 mutex_unlock(&st
->lock
);
335 return ret
< 0 ? ret
: sprintf(buf
, "0x%x\n", ret
);
338 static ssize_t
ad2s1210_store_control(struct device
*dev
,
339 struct device_attribute
*attr
,
340 const char *buf
, size_t len
)
342 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
347 ret
= strict_strtoul(buf
, 16, &udata
);
351 mutex_lock(&st
->lock
);
352 ret
= ad2s1210_config_write(st
, AD2S1210_REG_CONTROL
);
355 data
= udata
& AD2S1210_MSB_IS_LOW
;
356 ret
= ad2s1210_config_write(st
, data
);
360 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
363 if (ret
& AD2S1210_MSB_IS_HIGH
) {
365 pr_err("ad2s1210: write control register fail\n");
369 = ad2s1210_resolution_value
[data
& AD2S1210_SET_RESOLUTION
];
370 if (st
->pdata
->gpioin
) {
371 data
= ad2s1210_read_resolution_pin(st
);
372 if (data
!= st
->resolution
)
373 pr_warning("ad2s1210: resolution settings not match\n");
375 ad2s1210_set_resolution_pin(st
);
378 st
->hysteresis
= !!(data
& AD2S1210_ENABLE_HYSTERESIS
);
381 mutex_unlock(&st
->lock
);
385 static ssize_t
ad2s1210_show_resolution(struct device
*dev
,
386 struct device_attribute
*attr
, char *buf
)
388 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
389 return sprintf(buf
, "%d\n", st
->resolution
);
392 static ssize_t
ad2s1210_store_resolution(struct device
*dev
,
393 struct device_attribute
*attr
,
394 const char *buf
, size_t len
)
396 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
401 ret
= strict_strtoul(buf
, 10, &udata
);
402 if (ret
|| udata
< 10 || udata
> 16) {
403 pr_err("ad2s1210: resolution out of range\n");
406 mutex_lock(&st
->lock
);
407 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
411 data
&= ~AD2S1210_SET_RESOLUTION
;
412 data
|= (udata
- 10) >> 1;
413 ret
= ad2s1210_config_write(st
, AD2S1210_REG_CONTROL
);
416 ret
= ad2s1210_config_write(st
, data
& AD2S1210_MSB_IS_LOW
);
419 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
423 if (data
& AD2S1210_MSB_IS_HIGH
) {
425 pr_err("ad2s1210: setting resolution fail\n");
429 = ad2s1210_resolution_value
[data
& AD2S1210_SET_RESOLUTION
];
430 if (st
->pdata
->gpioin
) {
431 data
= ad2s1210_read_resolution_pin(st
);
432 if (data
!= st
->resolution
)
433 pr_warning("ad2s1210: resolution settings not match\n");
435 ad2s1210_set_resolution_pin(st
);
438 mutex_unlock(&st
->lock
);
442 /* read the fault register since last sample */
443 static ssize_t
ad2s1210_show_fault(struct device
*dev
,
444 struct device_attribute
*attr
, char *buf
)
446 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
449 mutex_lock(&st
->lock
);
450 ret
= ad2s1210_config_read(st
, AD2S1210_REG_FAULT
);
451 mutex_unlock(&st
->lock
);
453 return ret
? ret
: sprintf(buf
, "0x%x\n", ret
);
456 static ssize_t
ad2s1210_clear_fault(struct device
*dev
,
457 struct device_attribute
*attr
,
461 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
464 mutex_lock(&st
->lock
);
465 gpio_set_value(st
->pdata
->sample
, 0);
466 /* delay (2 * tck + 20) nano seconds */
468 gpio_set_value(st
->pdata
->sample
, 1);
469 ret
= ad2s1210_config_read(st
, AD2S1210_REG_FAULT
);
472 gpio_set_value(st
->pdata
->sample
, 0);
473 gpio_set_value(st
->pdata
->sample
, 1);
475 mutex_unlock(&st
->lock
);
477 return ret
< 0 ? ret
: len
;
480 static ssize_t
ad2s1210_show_reg(struct device
*dev
,
481 struct device_attribute
*attr
,
484 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
485 struct iio_dev_attr
*iattr
= to_iio_dev_attr(attr
);
488 mutex_lock(&st
->lock
);
489 ret
= ad2s1210_config_read(st
, iattr
->address
);
490 mutex_unlock(&st
->lock
);
492 return ret
< 0 ? ret
: sprintf(buf
, "%d\n", ret
);
495 static ssize_t
ad2s1210_store_reg(struct device
*dev
,
496 struct device_attribute
*attr
, const char *buf
, size_t len
)
498 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
501 struct iio_dev_attr
*iattr
= to_iio_dev_attr(attr
);
503 ret
= strict_strtoul(buf
, 10, &data
);
506 mutex_lock(&st
->lock
);
507 ret
= ad2s1210_config_write(st
, iattr
->address
);
510 ret
= ad2s1210_config_write(st
, data
& AD2S1210_MSB_IS_LOW
);
512 mutex_unlock(&st
->lock
);
513 return ret
< 0 ? ret
: len
;
516 static ssize_t
ad2s1210_show_pos(struct device
*dev
,
517 struct device_attribute
*attr
,
523 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
525 mutex_lock(&st
->lock
);
526 gpio_set_value(st
->pdata
->sample
, 0);
527 /* delay (6 * tck + 20) nano seconds */
530 ad2s1210_set_mode(MOD_POS
, st
);
531 ret
= spi_read(st
->sdev
, st
->rx
, 2);
534 pos
= be16_to_cpup((u16
*)st
->rx
);
536 pos
>>= 16 - st
->resolution
;
537 len
= sprintf(buf
, "%d\n", pos
);
539 gpio_set_value(st
->pdata
->sample
, 1);
540 /* delay (2 * tck + 20) nano seconds */
542 mutex_unlock(&st
->lock
);
544 return ret
< 0 ? ret
: len
;
547 static ssize_t
ad2s1210_show_vel(struct device
*dev
,
548 struct device_attribute
*attr
,
551 unsigned short negative
;
555 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
557 mutex_lock(&st
->lock
);
558 gpio_set_value(st
->pdata
->sample
, 0);
559 /* delay (6 * tck + 20) nano seconds */
562 ad2s1210_set_mode(MOD_VEL
, st
);
563 ret
= spi_read(st
->sdev
, st
->rx
, 2);
566 negative
= st
->rx
[0] & 0x80;
567 vel
= be16_to_cpup((s16
*)st
->rx
);
568 vel
>>= 16 - st
->resolution
;
570 negative
= (0xffff >> st
->resolution
) << st
->resolution
;
573 len
= sprintf(buf
, "%d\n", vel
);
575 gpio_set_value(st
->pdata
->sample
, 1);
576 /* delay (2 * tck + 20) nano seconds */
578 mutex_unlock(&st
->lock
);
580 return ret
< 0 ? ret
: len
;
583 static IIO_DEVICE_ATTR(raw_io
, S_IRUGO
| S_IWUSR
,
584 ad2s1210_show_raw
, ad2s1210_store_raw
, 0);
585 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
586 NULL
, ad2s1210_store_softreset
, 0);
587 static IIO_DEVICE_ATTR(fclkin
, S_IRUGO
| S_IWUSR
,
588 ad2s1210_show_fclkin
, ad2s1210_store_fclkin
, 0);
589 static IIO_DEVICE_ATTR(fexcit
, S_IRUGO
| S_IWUSR
,
590 ad2s1210_show_fexcit
, ad2s1210_store_fexcit
, 0);
591 static IIO_DEVICE_ATTR(control
, S_IRUGO
| S_IWUSR
,
592 ad2s1210_show_control
, ad2s1210_store_control
, 0);
593 static IIO_DEVICE_ATTR(bits
, S_IRUGO
| S_IWUSR
,
594 ad2s1210_show_resolution
, ad2s1210_store_resolution
, 0);
595 static IIO_DEVICE_ATTR(fault
, S_IRUGO
| S_IWUSR
,
596 ad2s1210_show_fault
, ad2s1210_clear_fault
, 0);
597 static IIO_DEVICE_ATTR(pos
, S_IRUGO
, ad2s1210_show_pos
, NULL
, 0);
598 static IIO_DEVICE_ATTR(vel
, S_IRUGO
, ad2s1210_show_vel
, NULL
, 0);
599 static IIO_DEVICE_ATTR(los_thrd
, S_IRUGO
| S_IWUSR
,
600 ad2s1210_show_reg
, ad2s1210_store_reg
,
601 AD2S1210_REG_LOS_THRD
);
602 static IIO_DEVICE_ATTR(dos_ovr_thrd
, S_IRUGO
| S_IWUSR
,
603 ad2s1210_show_reg
, ad2s1210_store_reg
,
604 AD2S1210_REG_DOS_OVR_THRD
);
605 static IIO_DEVICE_ATTR(dos_mis_thrd
, S_IRUGO
| S_IWUSR
,
606 ad2s1210_show_reg
, ad2s1210_store_reg
,
607 AD2S1210_REG_DOS_MIS_THRD
);
608 static IIO_DEVICE_ATTR(dos_rst_max_thrd
, S_IRUGO
| S_IWUSR
,
609 ad2s1210_show_reg
, ad2s1210_store_reg
,
610 AD2S1210_REG_DOS_RST_MAX_THRD
);
611 static IIO_DEVICE_ATTR(dos_rst_min_thrd
, S_IRUGO
| S_IWUSR
,
612 ad2s1210_show_reg
, ad2s1210_store_reg
,
613 AD2S1210_REG_DOS_RST_MIN_THRD
);
614 static IIO_DEVICE_ATTR(lot_high_thrd
, S_IRUGO
| S_IWUSR
,
615 ad2s1210_show_reg
, ad2s1210_store_reg
,
616 AD2S1210_REG_LOT_HIGH_THRD
);
617 static IIO_DEVICE_ATTR(lot_low_thrd
, S_IRUGO
| S_IWUSR
,
618 ad2s1210_show_reg
, ad2s1210_store_reg
,
619 AD2S1210_REG_LOT_LOW_THRD
);
621 static struct attribute
*ad2s1210_attributes
[] = {
622 &iio_dev_attr_raw_io
.dev_attr
.attr
,
623 &iio_dev_attr_reset
.dev_attr
.attr
,
624 &iio_dev_attr_fclkin
.dev_attr
.attr
,
625 &iio_dev_attr_fexcit
.dev_attr
.attr
,
626 &iio_dev_attr_control
.dev_attr
.attr
,
627 &iio_dev_attr_bits
.dev_attr
.attr
,
628 &iio_dev_attr_fault
.dev_attr
.attr
,
629 &iio_dev_attr_pos
.dev_attr
.attr
,
630 &iio_dev_attr_vel
.dev_attr
.attr
,
631 &iio_dev_attr_los_thrd
.dev_attr
.attr
,
632 &iio_dev_attr_dos_ovr_thrd
.dev_attr
.attr
,
633 &iio_dev_attr_dos_mis_thrd
.dev_attr
.attr
,
634 &iio_dev_attr_dos_rst_max_thrd
.dev_attr
.attr
,
635 &iio_dev_attr_dos_rst_min_thrd
.dev_attr
.attr
,
636 &iio_dev_attr_lot_high_thrd
.dev_attr
.attr
,
637 &iio_dev_attr_lot_low_thrd
.dev_attr
.attr
,
641 static const struct attribute_group ad2s1210_attribute_group
= {
643 .attrs
= ad2s1210_attributes
,
646 static int __devinit
ad2s1210_initial(struct ad2s1210_state
*st
)
651 mutex_lock(&st
->lock
);
652 if (st
->pdata
->gpioin
)
653 st
->resolution
= ad2s1210_read_resolution_pin(st
);
655 ad2s1210_set_resolution_pin(st
);
657 ret
= ad2s1210_config_write(st
, AD2S1210_REG_CONTROL
);
660 data
= AD2S1210_DEF_CONTROL
& ~(AD2S1210_SET_RESOLUTION
);
661 data
|= (st
->resolution
- 10) >> 1;
662 ret
= ad2s1210_config_write(st
, data
);
665 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
669 if (ret
& AD2S1210_MSB_IS_HIGH
) {
674 ret
= ad2s1210_update_frequency_control_word(st
);
677 ret
= ad2s1210_soft_reset(st
);
679 mutex_unlock(&st
->lock
);
683 static const struct iio_info ad2s1210_info
= {
684 .attrs
= &ad2s1210_attribute_group
,
685 .driver_module
= THIS_MODULE
,
688 static int ad2s1210_setup_gpios(struct ad2s1210_state
*st
)
691 unsigned long flags
= st
->pdata
->gpioin
? GPIOF_DIR_IN
: GPIOF_DIR_OUT
;
693 ret
= gpio_request_one(st
->pdata
->sample
, GPIOF_DIR_IN
, "sample");
696 ret
= gpio_request_one(st
->pdata
->a
[0], flags
, "a0");
698 goto error_free_sample
;
699 ret
= gpio_request_one(st
->pdata
->a
[1], flags
, "a1");
702 ret
= gpio_request_one(st
->pdata
->res
[1], flags
, "res0");
705 ret
= gpio_request_one(st
->pdata
->res
[1], flags
, "res1");
707 goto error_free_res0
;
711 gpio_free(st
->pdata
->res
[0]);
713 gpio_free(st
->pdata
->a
[1]);
715 gpio_free(st
->pdata
->a
[0]);
717 gpio_free(st
->pdata
->sample
);
722 static void ad2s1210_free_gpios(struct ad2s1210_state
*st
)
724 gpio_free(st
->pdata
->res
[1]);
725 gpio_free(st
->pdata
->res
[0]);
726 gpio_free(st
->pdata
->a
[1]);
727 gpio_free(st
->pdata
->a
[0]);
728 gpio_free(st
->pdata
->sample
);
731 static int __devinit
ad2s1210_probe(struct spi_device
*spi
)
733 struct iio_dev
*indio_dev
;
734 struct ad2s1210_state
*st
;
737 if (spi
->dev
.platform_data
== NULL
)
740 indio_dev
= iio_allocate_device(sizeof(*st
));
741 if (indio_dev
== NULL
) {
745 st
= iio_priv(indio_dev
);
746 st
->pdata
= spi
->dev
.platform_data
;
747 ret
= ad2s1210_setup_gpios(st
);
751 spi_set_drvdata(spi
, indio_dev
);
753 mutex_init(&st
->lock
);
755 st
->hysteresis
= true;
756 st
->mode
= MOD_CONFIG
;
758 st
->fexcit
= AD2S1210_DEF_EXCIT
;
760 indio_dev
->dev
.parent
= &spi
->dev
;
761 indio_dev
->info
= &ad2s1210_info
;
762 indio_dev
->modes
= INDIO_DIRECT_MODE
;
764 ret
= iio_device_register(indio_dev
);
766 goto error_free_gpios
;
768 st
->fclkin
= spi
->max_speed_hz
;
769 spi
->mode
= SPI_MODE_3
;
771 ad2s1210_initial(st
);
776 ad2s1210_free_gpios(st
);
778 iio_free_device(indio_dev
);
783 static int __devexit
ad2s1210_remove(struct spi_device
*spi
)
785 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
786 struct ad2s1210_state
*st
= iio_priv(indio_dev
);
787 iio_device_unregister(indio_dev
);
788 ad2s1210_free_gpios(st
);
793 static struct spi_driver ad2s1210_driver
= {
796 .owner
= THIS_MODULE
,
798 .probe
= ad2s1210_probe
,
799 .remove
= __devexit_p(ad2s1210_remove
),
802 static __init
int ad2s1210_spi_init(void)
804 return spi_register_driver(&ad2s1210_driver
);
806 module_init(ad2s1210_spi_init
);
808 static __exit
void ad2s1210_spi_exit(void)
810 spi_unregister_driver(&ad2s1210_driver
);
812 module_exit(ad2s1210_spi_exit
);
814 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
815 MODULE_DESCRIPTION("Analog Devices AD2S1210 Resolver to Digital SPI driver");
816 MODULE_LICENSE("GPL v2");