2 * ad2s1200.c simple support for the ADI Resolver to Digital Converters:
5 * Copyright (c) 2010-2010 Analog Devices Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/delay.h>
19 #include <linux/gpio.h>
20 #include <linux/module.h>
25 #define DRV_NAME "ad2s1200"
27 /* input pin sample and rdvel is controlled by driver */
30 /* input clock on serial interface */
31 #define AD2S1200_HZ 8192000
32 /* clock period in nano second */
33 #define AD2S1200_TSCLK (1000000000/AD2S1200_HZ)
35 struct ad2s1200_state
{
37 struct spi_device
*sdev
;
40 u8 rx
[2] ____cacheline_aligned
;
43 static int ad2s1200_read_raw(struct iio_dev
*indio_dev
,
44 struct iio_chan_spec
const *chan
,
51 struct ad2s1200_state
*st
= iio_priv(indio_dev
);
53 mutex_lock(&st
->lock
);
54 gpio_set_value(st
->sample
, 0);
55 /* delay (6 * AD2S1200_TSCLK + 20) nano seconds */
57 gpio_set_value(st
->sample
, 1);
58 gpio_set_value(st
->rdvel
, !!(chan
->type
== IIO_ANGL
));
59 ret
= spi_read(st
->sdev
, st
->rx
, 2);
61 mutex_unlock(&st
->lock
);
67 *val
= (((u16
)(st
->rx
[0])) << 4) | ((st
->rx
[1] & 0xF0) >> 4);
70 vel
= (((s16
)(st
->rx
[0])) << 4) | ((st
->rx
[1] & 0xF0) >> 4);
71 vel
= (vel
<< 4) >> 4;
74 mutex_unlock(&st
->lock
);
77 /* delay (2 * AD2S1200_TSCLK + 20) ns for sample pulse */
79 mutex_unlock(&st
->lock
);
83 static const struct iio_chan_spec ad2s1200_channels
[] = {
95 static const struct iio_info ad2s1200_info
= {
96 .read_raw
= &ad2s1200_read_raw
,
97 .driver_module
= THIS_MODULE
,
100 static int __devinit
ad2s1200_probe(struct spi_device
*spi
)
102 struct ad2s1200_state
*st
;
103 struct iio_dev
*indio_dev
;
105 unsigned short *pins
= spi
->dev
.platform_data
;
107 for (pn
= 0; pn
< AD2S1200_PN
; pn
++)
108 if (gpio_request_one(pins
[pn
], GPIOF_DIR_OUT
, DRV_NAME
)) {
109 pr_err("%s: request gpio pin %d failed\n",
113 indio_dev
= iio_allocate_device(sizeof(*st
));
114 if (indio_dev
== NULL
) {
118 spi_set_drvdata(spi
, indio_dev
);
119 st
= iio_priv(indio_dev
);
120 mutex_init(&st
->lock
);
122 st
->sample
= pins
[0];
125 indio_dev
->dev
.parent
= &spi
->dev
;
126 indio_dev
->info
= &ad2s1200_info
;
127 indio_dev
->modes
= INDIO_DIRECT_MODE
;
128 indio_dev
->channels
= ad2s1200_channels
;
129 indio_dev
->num_channels
= ARRAY_SIZE(ad2s1200_channels
);
130 indio_dev
->name
= spi_get_device_id(spi
)->name
;
132 ret
= iio_device_register(indio_dev
);
136 spi
->max_speed_hz
= AD2S1200_HZ
;
137 spi
->mode
= SPI_MODE_3
;
143 iio_free_device(indio_dev
);
145 for (--pn
; pn
>= 0; pn
--)
150 static int __devexit
ad2s1200_remove(struct spi_device
*spi
)
152 iio_device_unregister(spi_get_drvdata(spi
));
153 iio_free_device(spi_get_drvdata(spi
));
158 static const struct spi_device_id ad2s1200_id
[] = {
163 MODULE_DEVICE_TABLE(spi
, ad2s1200_id
);
165 static struct spi_driver ad2s1200_driver
= {
168 .owner
= THIS_MODULE
,
170 .probe
= ad2s1200_probe
,
171 .remove
= __devexit_p(ad2s1200_remove
),
172 .id_table
= ad2s1200_id
,
174 module_spi_driver(ad2s1200_driver
);
176 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
177 MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
178 MODULE_LICENSE("GPL v2");