Commit | Line | Data |
---|---|---|
f46d9f15 GY |
1 | /* |
2 | * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90 | |
3 | * | |
4 | * Copyright (c) 2010-2010 Analog Devices Inc. | |
5 | * | |
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. | |
9 | * | |
10 | */ | |
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 | ||
18 | #include "../iio.h" | |
19 | #include "../sysfs.h" | |
20 | ||
21 | #define DRV_NAME "ad2s90" | |
22 | ||
23 | struct ad2s90_state { | |
24 | struct mutex lock; | |
25 | struct iio_dev *idev; | |
26 | struct spi_device *sdev; | |
58f08b0a | 27 | u8 rx[2] ____cacheline_aligned; |
f46d9f15 GY |
28 | }; |
29 | ||
30 | static ssize_t ad2s90_show_angular(struct device *dev, | |
31 | struct device_attribute *attr, char *buf) | |
32 | { | |
f46d9f15 GY |
33 | int ret; |
34 | ssize_t len = 0; | |
35 | u16 val; | |
58f08b0a | 36 | struct ad2s90_state *st = iio_priv(dev_get_drvdata(dev)); |
f46d9f15 | 37 | |
f46d9f15 | 38 | mutex_lock(&st->lock); |
58f08b0a | 39 | ret = spi_read(st->sdev, st->rx, 2); |
f46d9f15 GY |
40 | if (ret) |
41 | goto error_ret; | |
42 | val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4); | |
43 | len = sprintf(buf, "%d\n", val); | |
44 | error_ret: | |
45 | mutex_unlock(&st->lock); | |
46 | ||
47 | return ret ? ret : len; | |
48 | } | |
49 | ||
50 | #define IIO_DEV_ATTR_SIMPLE_RESOLVER(_show) \ | |
51 | IIO_DEVICE_ATTR(angular, S_IRUGO, _show, NULL, 0) | |
52 | ||
f46d9f15 GY |
53 | static IIO_DEV_ATTR_SIMPLE_RESOLVER(ad2s90_show_angular); |
54 | ||
55 | static struct attribute *ad2s90_attributes[] = { | |
f46d9f15 GY |
56 | &iio_dev_attr_angular.dev_attr.attr, |
57 | NULL, | |
58 | }; | |
59 | ||
60 | static const struct attribute_group ad2s90_attribute_group = { | |
61 | .name = DRV_NAME, | |
62 | .attrs = ad2s90_attributes, | |
63 | }; | |
64 | ||
6fe8135f JC |
65 | static const struct iio_info ad2s90_info = { |
66 | .attrs = &ad2s90_attribute_group, | |
67 | .driver_module = THIS_MODULE, | |
68 | }; | |
69 | ||
f46d9f15 GY |
70 | static int __devinit ad2s90_probe(struct spi_device *spi) |
71 | { | |
58f08b0a | 72 | struct iio_dev *indio_dev; |
f46d9f15 GY |
73 | struct ad2s90_state *st; |
74 | int ret = 0; | |
75 | ||
58f08b0a JC |
76 | indio_dev = iio_allocate_device(sizeof(*st)); |
77 | if (indio_dev == NULL) { | |
f46d9f15 GY |
78 | ret = -ENOMEM; |
79 | goto error_ret; | |
80 | } | |
58f08b0a JC |
81 | st = iio_priv(indio_dev); |
82 | spi_set_drvdata(spi, indio_dev); | |
f46d9f15 GY |
83 | |
84 | mutex_init(&st->lock); | |
85 | st->sdev = spi; | |
58f08b0a JC |
86 | indio_dev->dev.parent = &spi->dev; |
87 | indio_dev->info = &ad2s90_info; | |
88 | indio_dev->modes = INDIO_DIRECT_MODE; | |
f46d9f15 GY |
89 | |
90 | ret = iio_device_register(st->idev); | |
91 | if (ret) | |
92 | goto error_free_dev; | |
93 | ||
94 | /* need 600ns between CS and the first falling edge of SCLK */ | |
95 | spi->max_speed_hz = 830000; | |
96 | spi->mode = SPI_MODE_3; | |
97 | spi_setup(spi); | |
98 | ||
99 | return 0; | |
100 | ||
101 | error_free_dev: | |
102 | iio_free_device(st->idev); | |
f46d9f15 GY |
103 | error_ret: |
104 | return ret; | |
105 | } | |
106 | ||
107 | static int __devexit ad2s90_remove(struct spi_device *spi) | |
108 | { | |
58f08b0a | 109 | iio_device_unregister(spi_get_drvdata(spi)); |
f46d9f15 GY |
110 | |
111 | return 0; | |
112 | } | |
113 | ||
114 | static struct spi_driver ad2s90_driver = { | |
115 | .driver = { | |
116 | .name = DRV_NAME, | |
117 | .owner = THIS_MODULE, | |
118 | }, | |
119 | .probe = ad2s90_probe, | |
120 | .remove = __devexit_p(ad2s90_remove), | |
121 | }; | |
122 | ||
123 | static __init int ad2s90_spi_init(void) | |
124 | { | |
125 | return spi_register_driver(&ad2s90_driver); | |
126 | } | |
127 | module_init(ad2s90_spi_init); | |
128 | ||
129 | static __exit void ad2s90_spi_exit(void) | |
130 | { | |
131 | spi_unregister_driver(&ad2s90_driver); | |
132 | } | |
133 | module_exit(ad2s90_spi_exit); | |
134 | ||
135 | MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>"); | |
136 | MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver"); | |
137 | MODULE_LICENSE("GPL v2"); |