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 const struct iio_info ad2s90_info
= {
79 .attrs
= &ad2s90_attribute_group
,
80 .driver_module
= THIS_MODULE
,
83 static int __devinit
ad2s90_probe(struct spi_device
*spi
)
85 struct ad2s90_state
*st
;
88 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
93 spi_set_drvdata(spi
, st
);
95 mutex_init(&st
->lock
);
98 st
->idev
= iio_allocate_device(0);
99 if (st
->idev
== NULL
) {
103 st
->idev
->dev
.parent
= &spi
->dev
;
105 st
->idev
->info
= &ad2s90_info
;
106 st
->idev
->dev_data
= (void *)(st
);
107 st
->idev
->modes
= INDIO_DIRECT_MODE
;
109 ret
= iio_device_register(st
->idev
);
113 /* need 600ns between CS and the first falling edge of SCLK */
114 spi
->max_speed_hz
= 830000;
115 spi
->mode
= SPI_MODE_3
;
121 iio_free_device(st
->idev
);
128 static int __devexit
ad2s90_remove(struct spi_device
*spi
)
130 struct ad2s90_state
*st
= spi_get_drvdata(spi
);
132 iio_device_unregister(st
->idev
);
138 static struct spi_driver ad2s90_driver
= {
141 .owner
= THIS_MODULE
,
143 .probe
= ad2s90_probe
,
144 .remove
= __devexit_p(ad2s90_remove
),
147 static __init
int ad2s90_spi_init(void)
149 return spi_register_driver(&ad2s90_driver
);
151 module_init(ad2s90_spi_init
);
153 static __exit
void ad2s90_spi_exit(void)
155 spi_unregister_driver(&ad2s90_driver
);
157 module_exit(ad2s90_spi_exit
);
159 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
160 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
161 MODULE_LICENSE("GPL v2");