2 * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
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>
21 #define DRV_NAME "ad2s90"
26 struct spi_device
*sdev
;
31 static ssize_t
ad2s90_show_angular(struct device
*dev
,
32 struct device_attribute
*attr
, char *buf
)
34 struct spi_message msg
;
35 struct spi_transfer xfer
;
39 struct iio_dev
*idev
= dev_get_drvdata(dev
);
40 struct ad2s90_state
*st
= idev
->dev_data
;
45 mutex_lock(&st
->lock
);
47 spi_message_init(&msg
);
48 spi_message_add_tail(&xfer
, &msg
);
49 ret
= spi_sync(st
->sdev
, &msg
);
52 val
= (((u16
)(st
->rx
[0])) << 4) | ((st
->rx
[1] & 0xF0) >> 4);
53 len
= sprintf(buf
, "%d\n", val
);
55 mutex_unlock(&st
->lock
);
57 return ret
? ret
: len
;
60 #define IIO_DEV_ATTR_SIMPLE_RESOLVER(_show) \
61 IIO_DEVICE_ATTR(angular, S_IRUGO, _show, NULL, 0)
63 static IIO_CONST_ATTR(description
,
64 "Low Cost, Complete 12-Bit Resolver-to-Digital Converter");
65 static IIO_DEV_ATTR_SIMPLE_RESOLVER(ad2s90_show_angular
);
67 static struct attribute
*ad2s90_attributes
[] = {
68 &iio_const_attr_description
.dev_attr
.attr
,
69 &iio_dev_attr_angular
.dev_attr
.attr
,
73 static const struct attribute_group ad2s90_attribute_group
= {
75 .attrs
= ad2s90_attributes
,
78 static int __devinit
ad2s90_probe(struct spi_device
*spi
)
80 struct ad2s90_state
*st
;
83 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
88 spi_set_drvdata(spi
, st
);
90 mutex_init(&st
->lock
);
93 st
->idev
= iio_allocate_device();
94 if (st
->idev
== NULL
) {
98 st
->idev
->dev
.parent
= &spi
->dev
;
99 st
->idev
->num_interrupt_lines
= 0;
100 st
->idev
->event_attrs
= NULL
;
102 st
->idev
->attrs
= &ad2s90_attribute_group
;
103 st
->idev
->dev_data
= (void *)(st
);
104 st
->idev
->driver_module
= THIS_MODULE
;
105 st
->idev
->modes
= INDIO_DIRECT_MODE
;
107 ret
= iio_device_register(st
->idev
);
111 /* need 600ns between CS and the first falling edge of SCLK */
112 spi
->max_speed_hz
= 830000;
113 spi
->mode
= SPI_MODE_3
;
119 iio_free_device(st
->idev
);
126 static int __devexit
ad2s90_remove(struct spi_device
*spi
)
128 struct ad2s90_state
*st
= spi_get_drvdata(spi
);
130 iio_device_unregister(st
->idev
);
136 static struct spi_driver ad2s90_driver
= {
139 .owner
= THIS_MODULE
,
141 .probe
= ad2s90_probe
,
142 .remove
= __devexit_p(ad2s90_remove
),
145 static __init
int ad2s90_spi_init(void)
147 return spi_register_driver(&ad2s90_driver
);
149 module_init(ad2s90_spi_init
);
151 static __exit
void ad2s90_spi_exit(void)
153 spi_unregister_driver(&ad2s90_driver
);
155 module_exit(ad2s90_spi_exit
);
157 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
158 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
159 MODULE_LICENSE("GPL v2");