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>
19 #include <linux/module.h>
25 #define DRV_NAME "ad2s1210"
27 #define AD2S1210_DEF_CONTROL 0x7E
29 #define AD2S1210_MSB_IS_HIGH 0x80
30 #define AD2S1210_MSB_IS_LOW 0x7F
31 #define AD2S1210_PHASE_LOCK_RANGE_44 0x20
32 #define AD2S1210_ENABLE_HYSTERESIS 0x10
33 #define AD2S1210_SET_ENRES1 0x08
34 #define AD2S1210_SET_ENRES0 0x04
35 #define AD2S1210_SET_RES1 0x02
36 #define AD2S1210_SET_RES0 0x01
38 #define AD2S1210_SET_ENRESOLUTION (AD2S1210_SET_ENRES1 | \
40 #define AD2S1210_SET_RESOLUTION (AD2S1210_SET_RES1 | AD2S1210_SET_RES0)
42 #define AD2S1210_REG_POSITION 0x80
43 #define AD2S1210_REG_VELOCITY 0x82
44 #define AD2S1210_REG_LOS_THRD 0x88
45 #define AD2S1210_REG_DOS_OVR_THRD 0x89
46 #define AD2S1210_REG_DOS_MIS_THRD 0x8A
47 #define AD2S1210_REG_DOS_RST_MAX_THRD 0x8B
48 #define AD2S1210_REG_DOS_RST_MIN_THRD 0x8C
49 #define AD2S1210_REG_LOT_HIGH_THRD 0x8D
50 #define AD2S1210_REG_LOT_LOW_THRD 0x8E
51 #define AD2S1210_REG_EXCIT_FREQ 0x91
52 #define AD2S1210_REG_CONTROL 0x92
53 #define AD2S1210_REG_SOFT_RESET 0xF0
54 #define AD2S1210_REG_FAULT 0xFF
56 /* pin SAMPLE, A0, A1, RES0, RES1, is controlled by driver */
57 #define AD2S1210_SAA 3
58 #define AD2S1210_PN (AD2S1210_SAA + AD2S1210_RES)
60 #define AD2S1210_MIN_CLKIN 6144000
61 #define AD2S1210_MAX_CLKIN 10240000
62 #define AD2S1210_MIN_EXCIT 2000
63 #define AD2S1210_MAX_EXCIT 20000
64 #define AD2S1210_MIN_FCW 0x4
65 #define AD2S1210_MAX_FCW 0x50
67 /* default input clock on serial interface */
68 #define AD2S1210_DEF_CLKIN 8192000
69 /* clock period in nano second */
70 #define AD2S1210_DEF_TCK (1000000000/AD2S1210_DEF_CLKIN)
71 #define AD2S1210_DEF_EXCIT 10000
80 static const unsigned int ad2s1210_resolution_value
[] = { 10, 12, 14, 16 };
82 struct ad2s1210_state
{
83 const struct ad2s1210_platform_data
*pdata
;
85 struct spi_device
*sdev
;
91 enum ad2s1210_mode mode
;
92 u8 rx
[2] ____cacheline_aligned
;
93 u8 tx
[2] ____cacheline_aligned
;
96 static const int ad2s1210_mode_vals
[4][2] = {
99 [MOD_CONFIG
] = { 1, 0 },
101 static inline void ad2s1210_set_mode(enum ad2s1210_mode mode
,
102 struct ad2s1210_state
*st
)
104 gpio_set_value(st
->pdata
->a
[0], ad2s1210_mode_vals
[mode
][0]);
105 gpio_set_value(st
->pdata
->a
[1], ad2s1210_mode_vals
[mode
][1]);
109 /* write 1 bytes (address or data) to the chip */
110 static int ad2s1210_config_write(struct ad2s1210_state
*st
, u8 data
)
114 ad2s1210_set_mode(MOD_CONFIG
, st
);
116 ret
= spi_write(st
->sdev
, st
->tx
, 1);
124 /* read value from one of the registers */
125 static int ad2s1210_config_read(struct ad2s1210_state
*st
,
126 unsigned char address
)
128 struct spi_transfer xfer
= {
133 struct spi_message msg
;
136 ad2s1210_set_mode(MOD_CONFIG
, st
);
137 spi_message_init(&msg
);
138 spi_message_add_tail(&xfer
, &msg
);
139 st
->tx
[0] = address
| AD2S1210_MSB_IS_HIGH
;
140 st
->tx
[1] = AD2S1210_REG_FAULT
;
141 ret
= spi_sync(st
->sdev
, &msg
);
150 int ad2s1210_update_frequency_control_word(struct ad2s1210_state
*st
)
155 fcw
= (unsigned char)(st
->fexcit
* (1 << 15) / st
->fclkin
);
156 if (fcw
< AD2S1210_MIN_FCW
|| fcw
> AD2S1210_MAX_FCW
) {
157 pr_err("ad2s1210: FCW out of range\n");
161 ret
= ad2s1210_config_write(st
, AD2S1210_REG_EXCIT_FREQ
);
165 return ad2s1210_config_write(st
, fcw
);
168 static unsigned char ad2s1210_read_resolution_pin(struct ad2s1210_state
*st
)
170 return ad2s1210_resolution_value
[
171 (gpio_get_value(st
->pdata
->res
[0]) << 1) |
172 gpio_get_value(st
->pdata
->res
[1])];
175 static const int ad2s1210_res_pins
[4][2] = {
176 { 0, 0 }, {0, 1}, {1, 0}, {1, 1}
179 static inline void ad2s1210_set_resolution_pin(struct ad2s1210_state
*st
)
181 gpio_set_value(st
->pdata
->res
[0],
182 ad2s1210_res_pins
[(st
->resolution
- 10)/2][0]);
183 gpio_set_value(st
->pdata
->res
[1],
184 ad2s1210_res_pins
[(st
->resolution
- 10)/2][1]);
187 static inline int ad2s1210_soft_reset(struct ad2s1210_state
*st
)
191 ret
= ad2s1210_config_write(st
, AD2S1210_REG_SOFT_RESET
);
195 return ad2s1210_config_write(st
, 0x0);
199 /* return the OLD DATA since last spi bus write */
200 static ssize_t
ad2s1210_show_raw(struct device
*dev
,
201 struct device_attribute
*attr
,
204 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
207 mutex_lock(&st
->lock
);
209 ret
= sprintf(buf
, "0x%x\n", st
->rx
[0]);
210 st
->old_data
= false;
212 mutex_unlock(&st
->lock
);
217 static ssize_t
ad2s1210_store_raw(struct device
*dev
,
218 struct device_attribute
*attr
,
222 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
227 ret
= strict_strtoul(buf
, 16, &udata
);
232 mutex_lock(&st
->lock
);
233 ret
= ad2s1210_config_write(st
, data
);
234 mutex_unlock(&st
->lock
);
236 return ret
< 0 ? ret
: len
;
239 static ssize_t
ad2s1210_store_softreset(struct device
*dev
,
240 struct device_attribute
*attr
,
244 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
247 mutex_lock(&st
->lock
);
248 ret
= ad2s1210_soft_reset(st
);
249 mutex_unlock(&st
->lock
);
251 return ret
< 0 ? ret
: len
;
254 static ssize_t
ad2s1210_show_fclkin(struct device
*dev
,
255 struct device_attribute
*attr
,
258 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
259 return sprintf(buf
, "%d\n", st
->fclkin
);
262 static ssize_t
ad2s1210_store_fclkin(struct device
*dev
,
263 struct device_attribute
*attr
,
267 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
268 unsigned long fclkin
;
271 ret
= strict_strtoul(buf
, 10, &fclkin
);
274 if (fclkin
< AD2S1210_MIN_CLKIN
|| fclkin
> AD2S1210_MAX_CLKIN
) {
275 pr_err("ad2s1210: fclkin out of range\n");
279 mutex_lock(&st
->lock
);
282 ret
= ad2s1210_update_frequency_control_word(st
);
285 ret
= ad2s1210_soft_reset(st
);
287 mutex_unlock(&st
->lock
);
289 return ret
< 0 ? ret
: len
;
292 static ssize_t
ad2s1210_show_fexcit(struct device
*dev
,
293 struct device_attribute
*attr
,
296 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
297 return sprintf(buf
, "%d\n", st
->fexcit
);
300 static ssize_t
ad2s1210_store_fexcit(struct device
*dev
,
301 struct device_attribute
*attr
,
302 const char *buf
, size_t len
)
304 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
305 unsigned long fexcit
;
308 ret
= strict_strtoul(buf
, 10, &fexcit
);
311 if (fexcit
< AD2S1210_MIN_EXCIT
|| fexcit
> AD2S1210_MAX_EXCIT
) {
312 pr_err("ad2s1210: excitation frequency out of range\n");
315 mutex_lock(&st
->lock
);
317 ret
= ad2s1210_update_frequency_control_word(st
);
320 ret
= ad2s1210_soft_reset(st
);
322 mutex_unlock(&st
->lock
);
324 return ret
< 0 ? ret
: len
;
327 static ssize_t
ad2s1210_show_control(struct device
*dev
,
328 struct device_attribute
*attr
,
331 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
333 mutex_lock(&st
->lock
);
334 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
335 mutex_unlock(&st
->lock
);
336 return ret
< 0 ? ret
: sprintf(buf
, "0x%x\n", ret
);
339 static ssize_t
ad2s1210_store_control(struct device
*dev
,
340 struct device_attribute
*attr
,
341 const char *buf
, size_t len
)
343 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
348 ret
= strict_strtoul(buf
, 16, &udata
);
352 mutex_lock(&st
->lock
);
353 ret
= ad2s1210_config_write(st
, AD2S1210_REG_CONTROL
);
356 data
= udata
& AD2S1210_MSB_IS_LOW
;
357 ret
= ad2s1210_config_write(st
, data
);
361 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
364 if (ret
& AD2S1210_MSB_IS_HIGH
) {
366 pr_err("ad2s1210: write control register fail\n");
370 = ad2s1210_resolution_value
[data
& AD2S1210_SET_RESOLUTION
];
371 if (st
->pdata
->gpioin
) {
372 data
= ad2s1210_read_resolution_pin(st
);
373 if (data
!= st
->resolution
)
374 pr_warning("ad2s1210: resolution settings not match\n");
376 ad2s1210_set_resolution_pin(st
);
379 st
->hysteresis
= !!(data
& AD2S1210_ENABLE_HYSTERESIS
);
382 mutex_unlock(&st
->lock
);
386 static ssize_t
ad2s1210_show_resolution(struct device
*dev
,
387 struct device_attribute
*attr
, char *buf
)
389 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
390 return sprintf(buf
, "%d\n", st
->resolution
);
393 static ssize_t
ad2s1210_store_resolution(struct device
*dev
,
394 struct device_attribute
*attr
,
395 const char *buf
, size_t len
)
397 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
402 ret
= strict_strtoul(buf
, 10, &udata
);
403 if (ret
|| udata
< 10 || udata
> 16) {
404 pr_err("ad2s1210: resolution out of range\n");
407 mutex_lock(&st
->lock
);
408 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
412 data
&= ~AD2S1210_SET_RESOLUTION
;
413 data
|= (udata
- 10) >> 1;
414 ret
= ad2s1210_config_write(st
, AD2S1210_REG_CONTROL
);
417 ret
= ad2s1210_config_write(st
, data
& AD2S1210_MSB_IS_LOW
);
420 ret
= ad2s1210_config_read(st
, AD2S1210_REG_CONTROL
);
424 if (data
& AD2S1210_MSB_IS_HIGH
) {
426 pr_err("ad2s1210: setting resolution fail\n");
430 = ad2s1210_resolution_value
[data
& AD2S1210_SET_RESOLUTION
];
431 if (st
->pdata
->gpioin
) {
432 data
= ad2s1210_read_resolution_pin(st
);
433 if (data
!= st
->resolution
)
434 pr_warning("ad2s1210: resolution settings not match\n");
436 ad2s1210_set_resolution_pin(st
);
439 mutex_unlock(&st
->lock
);
443 /* read the fault register since last sample */
444 static ssize_t
ad2s1210_show_fault(struct device
*dev
,
445 struct device_attribute
*attr
, char *buf
)
447 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
450 mutex_lock(&st
->lock
);
451 ret
= ad2s1210_config_read(st
, AD2S1210_REG_FAULT
);
452 mutex_unlock(&st
->lock
);
454 return ret
? ret
: sprintf(buf
, "0x%x\n", ret
);
457 static ssize_t
ad2s1210_clear_fault(struct device
*dev
,
458 struct device_attribute
*attr
,
462 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
465 mutex_lock(&st
->lock
);
466 gpio_set_value(st
->pdata
->sample
, 0);
467 /* delay (2 * tck + 20) nano seconds */
469 gpio_set_value(st
->pdata
->sample
, 1);
470 ret
= ad2s1210_config_read(st
, AD2S1210_REG_FAULT
);
473 gpio_set_value(st
->pdata
->sample
, 0);
474 gpio_set_value(st
->pdata
->sample
, 1);
476 mutex_unlock(&st
->lock
);
478 return ret
< 0 ? ret
: len
;
481 static ssize_t
ad2s1210_show_reg(struct device
*dev
,
482 struct device_attribute
*attr
,
485 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
486 struct iio_dev_attr
*iattr
= to_iio_dev_attr(attr
);
489 mutex_lock(&st
->lock
);
490 ret
= ad2s1210_config_read(st
, iattr
->address
);
491 mutex_unlock(&st
->lock
);
493 return ret
< 0 ? ret
: sprintf(buf
, "%d\n", ret
);
496 static ssize_t
ad2s1210_store_reg(struct device
*dev
,
497 struct device_attribute
*attr
, const char *buf
, size_t len
)
499 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
502 struct iio_dev_attr
*iattr
= to_iio_dev_attr(attr
);
504 ret
= strict_strtoul(buf
, 10, &data
);
507 mutex_lock(&st
->lock
);
508 ret
= ad2s1210_config_write(st
, iattr
->address
);
511 ret
= ad2s1210_config_write(st
, data
& AD2S1210_MSB_IS_LOW
);
513 mutex_unlock(&st
->lock
);
514 return ret
< 0 ? ret
: len
;
517 static ssize_t
ad2s1210_show_pos(struct device
*dev
,
518 struct device_attribute
*attr
,
524 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
526 mutex_lock(&st
->lock
);
527 gpio_set_value(st
->pdata
->sample
, 0);
528 /* delay (6 * tck + 20) nano seconds */
531 ad2s1210_set_mode(MOD_POS
, st
);
532 ret
= spi_read(st
->sdev
, st
->rx
, 2);
535 pos
= be16_to_cpup((u16
*)st
->rx
);
537 pos
>>= 16 - st
->resolution
;
538 len
= sprintf(buf
, "%d\n", pos
);
540 gpio_set_value(st
->pdata
->sample
, 1);
541 /* delay (2 * tck + 20) nano seconds */
543 mutex_unlock(&st
->lock
);
545 return ret
< 0 ? ret
: len
;
548 static ssize_t
ad2s1210_show_vel(struct device
*dev
,
549 struct device_attribute
*attr
,
552 unsigned short negative
;
556 struct ad2s1210_state
*st
= iio_priv(dev_get_drvdata(dev
));
558 mutex_lock(&st
->lock
);
559 gpio_set_value(st
->pdata
->sample
, 0);
560 /* delay (6 * tck + 20) nano seconds */
563 ad2s1210_set_mode(MOD_VEL
, st
);
564 ret
= spi_read(st
->sdev
, st
->rx
, 2);
567 negative
= st
->rx
[0] & 0x80;
568 vel
= be16_to_cpup((s16
*)st
->rx
);
569 vel
>>= 16 - st
->resolution
;
571 negative
= (0xffff >> st
->resolution
) << st
->resolution
;
574 len
= sprintf(buf
, "%d\n", vel
);
576 gpio_set_value(st
->pdata
->sample
, 1);
577 /* delay (2 * tck + 20) nano seconds */
579 mutex_unlock(&st
->lock
);
581 return ret
< 0 ? ret
: len
;
584 static IIO_DEVICE_ATTR(raw_io
, S_IRUGO
| S_IWUSR
,
585 ad2s1210_show_raw
, ad2s1210_store_raw
, 0);
586 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
587 NULL
, ad2s1210_store_softreset
, 0);
588 static IIO_DEVICE_ATTR(fclkin
, S_IRUGO
| S_IWUSR
,
589 ad2s1210_show_fclkin
, ad2s1210_store_fclkin
, 0);
590 static IIO_DEVICE_ATTR(fexcit
, S_IRUGO
| S_IWUSR
,
591 ad2s1210_show_fexcit
, ad2s1210_store_fexcit
, 0);
592 static IIO_DEVICE_ATTR(control
, S_IRUGO
| S_IWUSR
,
593 ad2s1210_show_control
, ad2s1210_store_control
, 0);
594 static IIO_DEVICE_ATTR(bits
, S_IRUGO
| S_IWUSR
,
595 ad2s1210_show_resolution
, ad2s1210_store_resolution
, 0);
596 static IIO_DEVICE_ATTR(fault
, S_IRUGO
| S_IWUSR
,
597 ad2s1210_show_fault
, ad2s1210_clear_fault
, 0);
598 static IIO_DEVICE_ATTR(pos
, S_IRUGO
, ad2s1210_show_pos
, NULL
, 0);
599 static IIO_DEVICE_ATTR(vel
, S_IRUGO
, ad2s1210_show_vel
, NULL
, 0);
600 static IIO_DEVICE_ATTR(los_thrd
, S_IRUGO
| S_IWUSR
,
601 ad2s1210_show_reg
, ad2s1210_store_reg
,
602 AD2S1210_REG_LOS_THRD
);
603 static IIO_DEVICE_ATTR(dos_ovr_thrd
, S_IRUGO
| S_IWUSR
,
604 ad2s1210_show_reg
, ad2s1210_store_reg
,
605 AD2S1210_REG_DOS_OVR_THRD
);
606 static IIO_DEVICE_ATTR(dos_mis_thrd
, S_IRUGO
| S_IWUSR
,
607 ad2s1210_show_reg
, ad2s1210_store_reg
,
608 AD2S1210_REG_DOS_MIS_THRD
);
609 static IIO_DEVICE_ATTR(dos_rst_max_thrd
, S_IRUGO
| S_IWUSR
,
610 ad2s1210_show_reg
, ad2s1210_store_reg
,
611 AD2S1210_REG_DOS_RST_MAX_THRD
);
612 static IIO_DEVICE_ATTR(dos_rst_min_thrd
, S_IRUGO
| S_IWUSR
,
613 ad2s1210_show_reg
, ad2s1210_store_reg
,
614 AD2S1210_REG_DOS_RST_MIN_THRD
);
615 static IIO_DEVICE_ATTR(lot_high_thrd
, S_IRUGO
| S_IWUSR
,
616 ad2s1210_show_reg
, ad2s1210_store_reg
,
617 AD2S1210_REG_LOT_HIGH_THRD
);
618 static IIO_DEVICE_ATTR(lot_low_thrd
, S_IRUGO
| S_IWUSR
,
619 ad2s1210_show_reg
, ad2s1210_store_reg
,
620 AD2S1210_REG_LOT_LOW_THRD
);
622 static struct attribute
*ad2s1210_attributes
[] = {
623 &iio_dev_attr_raw_io
.dev_attr
.attr
,
624 &iio_dev_attr_reset
.dev_attr
.attr
,
625 &iio_dev_attr_fclkin
.dev_attr
.attr
,
626 &iio_dev_attr_fexcit
.dev_attr
.attr
,
627 &iio_dev_attr_control
.dev_attr
.attr
,
628 &iio_dev_attr_bits
.dev_attr
.attr
,
629 &iio_dev_attr_fault
.dev_attr
.attr
,
630 &iio_dev_attr_pos
.dev_attr
.attr
,
631 &iio_dev_attr_vel
.dev_attr
.attr
,
632 &iio_dev_attr_los_thrd
.dev_attr
.attr
,
633 &iio_dev_attr_dos_ovr_thrd
.dev_attr
.attr
,
634 &iio_dev_attr_dos_mis_thrd
.dev_attr
.attr
,
635 &iio_dev_attr_dos_rst_max_thrd
.dev_attr
.attr
,
636 &iio_dev_attr_dos_rst_min_thrd
.dev_attr
.attr
,
637 &iio_dev_attr_lot_high_thrd
.dev_attr
.attr
,
638 &iio_dev_attr_lot_low_thrd
.dev_attr
.attr
,
642 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
);
788 ad2s1210_free_gpios(st
);
789 iio_device_unregister(indio_dev
);
794 static struct spi_driver ad2s1210_driver
= {
797 .owner
= THIS_MODULE
,
799 .probe
= ad2s1210_probe
,
800 .remove
= __devexit_p(ad2s1210_remove
),
803 static __init
int ad2s1210_spi_init(void)
805 return spi_register_driver(&ad2s1210_driver
);
807 module_init(ad2s1210_spi_init
);
809 static __exit
void ad2s1210_spi_exit(void)
811 spi_unregister_driver(&ad2s1210_driver
);
813 module_exit(ad2s1210_spi_exit
);
815 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
816 MODULE_DESCRIPTION("Analog Devices AD2S1210 Resolver to Digital SPI driver");
817 MODULE_LICENSE("GPL v2");