GNU Linux-libre 4.9.332-gnu1
[releases.git] / drivers / staging / iio / resolver / ad2s1210.c
1 /*
2  * ad2s1210.c support for the ADI Resolver to Digital Converters: AD2S1210
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 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include "ad2s1210.h"
24
25 #define DRV_NAME "ad2s1210"
26
27 #define AD2S1210_DEF_CONTROL            0x7E
28
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
37
38 #define AD2S1210_SET_ENRESOLUTION       (AD2S1210_SET_ENRES1 |  \
39                                          AD2S1210_SET_ENRES0)
40 #define AD2S1210_SET_RESOLUTION         (AD2S1210_SET_RES1 | AD2S1210_SET_RES0)
41
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
55
56 /* pin SAMPLE, A0, A1, RES0, RES1, is controlled by driver */
57 #define AD2S1210_SAA            3
58 #define AD2S1210_PN             (AD2S1210_SAA + AD2S1210_RES)
59
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
66
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
72
73 enum ad2s1210_mode {
74         MOD_POS = 0,
75         MOD_VEL,
76         MOD_CONFIG,
77         MOD_RESERVED,
78 };
79
80 static const unsigned int ad2s1210_resolution_value[] = { 10, 12, 14, 16 };
81
82 struct ad2s1210_state {
83         const struct ad2s1210_platform_data *pdata;
84         struct mutex lock;
85         struct spi_device *sdev;
86         unsigned int fclkin;
87         unsigned int fexcit;
88         bool hysteresis;
89         bool old_data;
90         u8 resolution;
91         enum ad2s1210_mode mode;
92         u8 rx[2] ____cacheline_aligned;
93         u8 tx[2] ____cacheline_aligned;
94 };
95
96 static const int ad2s1210_mode_vals[4][2] = {
97         [MOD_POS] = { 0, 0 },
98         [MOD_VEL] = { 0, 1 },
99         [MOD_CONFIG] = { 1, 0 },
100 };
101
102 static inline void ad2s1210_set_mode(enum ad2s1210_mode mode,
103                                      struct ad2s1210_state *st)
104 {
105         gpio_set_value(st->pdata->a[0], ad2s1210_mode_vals[mode][0]);
106         gpio_set_value(st->pdata->a[1], ad2s1210_mode_vals[mode][1]);
107         st->mode = mode;
108 }
109
110 /* write 1 bytes (address or data) to the chip */
111 static int ad2s1210_config_write(struct ad2s1210_state *st, u8 data)
112 {
113         int ret;
114
115         ad2s1210_set_mode(MOD_CONFIG, st);
116         st->tx[0] = data;
117         ret = spi_write(st->sdev, st->tx, 1);
118         if (ret < 0)
119                 return ret;
120         st->old_data = true;
121
122         return 0;
123 }
124
125 /* read value from one of the registers */
126 static int ad2s1210_config_read(struct ad2s1210_state *st,
127                                 unsigned char address)
128 {
129         struct spi_transfer xfers[] = {
130                 {
131                         .len = 1,
132                         .rx_buf = &st->rx[0],
133                         .tx_buf = &st->tx[0],
134                         .cs_change = 1,
135                 }, {
136                         .len = 1,
137                         .rx_buf = &st->rx[1],
138                         .tx_buf = &st->tx[1],
139                 },
140         };
141         int ret = 0;
142
143         ad2s1210_set_mode(MOD_CONFIG, st);
144         st->tx[0] = address | AD2S1210_MSB_IS_HIGH;
145         st->tx[1] = AD2S1210_REG_FAULT;
146         ret = spi_sync_transfer(st->sdev, xfers, 2);
147         if (ret < 0)
148                 return ret;
149         st->old_data = true;
150
151         return st->rx[1];
152 }
153
154 static inline
155 int ad2s1210_update_frequency_control_word(struct ad2s1210_state *st)
156 {
157         int ret;
158         unsigned char fcw;
159
160         fcw = (unsigned char)(st->fexcit * (1 << 15) / st->fclkin);
161         if (fcw < AD2S1210_MIN_FCW || fcw > AD2S1210_MAX_FCW) {
162                 dev_err(&st->sdev->dev, "ad2s1210: FCW out of range\n");
163                 return -ERANGE;
164         }
165
166         ret = ad2s1210_config_write(st, AD2S1210_REG_EXCIT_FREQ);
167         if (ret < 0)
168                 return ret;
169
170         return ad2s1210_config_write(st, fcw);
171 }
172
173 static unsigned char ad2s1210_read_resolution_pin(struct ad2s1210_state *st)
174 {
175         return ad2s1210_resolution_value[
176                 (gpio_get_value(st->pdata->res[0]) << 1) |
177                 gpio_get_value(st->pdata->res[1])];
178 }
179
180 static const int ad2s1210_res_pins[4][2] = {
181         { 0, 0 }, {0, 1}, {1, 0}, {1, 1}
182 };
183
184 static inline void ad2s1210_set_resolution_pin(struct ad2s1210_state *st)
185 {
186         gpio_set_value(st->pdata->res[0],
187                        ad2s1210_res_pins[(st->resolution - 10) / 2][0]);
188         gpio_set_value(st->pdata->res[1],
189                        ad2s1210_res_pins[(st->resolution - 10) / 2][1]);
190 }
191
192 static inline int ad2s1210_soft_reset(struct ad2s1210_state *st)
193 {
194         int ret;
195
196         ret = ad2s1210_config_write(st, AD2S1210_REG_SOFT_RESET);
197         if (ret < 0)
198                 return ret;
199
200         return ad2s1210_config_write(st, 0x0);
201 }
202
203 static ssize_t ad2s1210_show_fclkin(struct device *dev,
204                                     struct device_attribute *attr,
205                                     char *buf)
206 {
207         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
208
209         return sprintf(buf, "%u\n", st->fclkin);
210 }
211
212 static ssize_t ad2s1210_store_fclkin(struct device *dev,
213                                      struct device_attribute *attr,
214                                      const char *buf,
215                                      size_t len)
216 {
217         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
218         unsigned int fclkin;
219         int ret;
220
221         ret = kstrtouint(buf, 10, &fclkin);
222         if (ret)
223                 return ret;
224         if (fclkin < AD2S1210_MIN_CLKIN || fclkin > AD2S1210_MAX_CLKIN) {
225                 dev_err(dev, "ad2s1210: fclkin out of range\n");
226                 return -EINVAL;
227         }
228
229         mutex_lock(&st->lock);
230         st->fclkin = fclkin;
231
232         ret = ad2s1210_update_frequency_control_word(st);
233         if (ret < 0)
234                 goto error_ret;
235         ret = ad2s1210_soft_reset(st);
236 error_ret:
237         mutex_unlock(&st->lock);
238
239         return ret < 0 ? ret : len;
240 }
241
242 static ssize_t ad2s1210_show_fexcit(struct device *dev,
243                                     struct device_attribute *attr,
244                                     char *buf)
245 {
246         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
247
248         return sprintf(buf, "%u\n", st->fexcit);
249 }
250
251 static ssize_t ad2s1210_store_fexcit(struct device *dev,
252                                      struct device_attribute *attr,
253                                      const char *buf, size_t len)
254 {
255         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
256         unsigned int fexcit;
257         int ret;
258
259         ret = kstrtouint(buf, 10, &fexcit);
260         if (ret < 0)
261                 return ret;
262         if (fexcit < AD2S1210_MIN_EXCIT || fexcit > AD2S1210_MAX_EXCIT) {
263                 dev_err(dev,
264                         "ad2s1210: excitation frequency out of range\n");
265                 return -EINVAL;
266         }
267         mutex_lock(&st->lock);
268         st->fexcit = fexcit;
269         ret = ad2s1210_update_frequency_control_word(st);
270         if (ret < 0)
271                 goto error_ret;
272         ret = ad2s1210_soft_reset(st);
273 error_ret:
274         mutex_unlock(&st->lock);
275
276         return ret < 0 ? ret : len;
277 }
278
279 static ssize_t ad2s1210_show_control(struct device *dev,
280                                      struct device_attribute *attr,
281                                      char *buf)
282 {
283         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
284         int ret;
285
286         mutex_lock(&st->lock);
287         ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
288         mutex_unlock(&st->lock);
289         return ret < 0 ? ret : sprintf(buf, "0x%x\n", ret);
290 }
291
292 static ssize_t ad2s1210_store_control(struct device *dev,
293                                       struct device_attribute *attr,
294                                       const char *buf, size_t len)
295 {
296         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
297         unsigned char udata;
298         unsigned char data;
299         int ret;
300
301         ret = kstrtou8(buf, 16, &udata);
302         if (ret)
303                 return -EINVAL;
304
305         mutex_lock(&st->lock);
306         ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
307         if (ret < 0)
308                 goto error_ret;
309         data = udata & AD2S1210_MSB_IS_LOW;
310         ret = ad2s1210_config_write(st, data);
311         if (ret < 0)
312                 goto error_ret;
313
314         ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
315         if (ret < 0)
316                 goto error_ret;
317         if (ret & AD2S1210_MSB_IS_HIGH) {
318                 ret = -EIO;
319                 dev_err(dev,
320                         "ad2s1210: write control register fail\n");
321                 goto error_ret;
322         }
323         st->resolution
324                 = ad2s1210_resolution_value[data & AD2S1210_SET_RESOLUTION];
325         if (st->pdata->gpioin) {
326                 data = ad2s1210_read_resolution_pin(st);
327                 if (data != st->resolution)
328                         dev_warn(dev, "ad2s1210: resolution settings not match\n");
329         } else {
330                 ad2s1210_set_resolution_pin(st);
331         }
332         ret = len;
333         st->hysteresis = !!(data & AD2S1210_ENABLE_HYSTERESIS);
334
335 error_ret:
336         mutex_unlock(&st->lock);
337         return ret;
338 }
339
340 static ssize_t ad2s1210_show_resolution(struct device *dev,
341                                         struct device_attribute *attr,
342                                         char *buf)
343 {
344         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
345
346         return sprintf(buf, "%d\n", st->resolution);
347 }
348
349 static ssize_t ad2s1210_store_resolution(struct device *dev,
350                                          struct device_attribute *attr,
351                                          const char *buf, size_t len)
352 {
353         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
354         unsigned char data;
355         unsigned char udata;
356         int ret;
357
358         ret = kstrtou8(buf, 10, &udata);
359         if (ret || udata < 10 || udata > 16) {
360                 dev_err(dev, "ad2s1210: resolution out of range\n");
361                 return -EINVAL;
362         }
363         mutex_lock(&st->lock);
364         ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
365         if (ret < 0)
366                 goto error_ret;
367         data = ret;
368         data &= ~AD2S1210_SET_RESOLUTION;
369         data |= (udata - 10) >> 1;
370         ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
371         if (ret < 0)
372                 goto error_ret;
373         ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
374         if (ret < 0)
375                 goto error_ret;
376         ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
377         if (ret < 0)
378                 goto error_ret;
379         data = ret;
380         if (data & AD2S1210_MSB_IS_HIGH) {
381                 ret = -EIO;
382                 dev_err(dev, "ad2s1210: setting resolution fail\n");
383                 goto error_ret;
384         }
385         st->resolution
386                 = ad2s1210_resolution_value[data & AD2S1210_SET_RESOLUTION];
387         if (st->pdata->gpioin) {
388                 data = ad2s1210_read_resolution_pin(st);
389                 if (data != st->resolution)
390                         dev_warn(dev, "ad2s1210: resolution settings not match\n");
391         } else {
392                 ad2s1210_set_resolution_pin(st);
393         }
394         ret = len;
395 error_ret:
396         mutex_unlock(&st->lock);
397         return ret;
398 }
399
400 /* read the fault register since last sample */
401 static ssize_t ad2s1210_show_fault(struct device *dev,
402                                    struct device_attribute *attr, char *buf)
403 {
404         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
405         int ret;
406
407         mutex_lock(&st->lock);
408         ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
409         mutex_unlock(&st->lock);
410
411         return ret ? ret : sprintf(buf, "0x%x\n", ret);
412 }
413
414 static ssize_t ad2s1210_clear_fault(struct device *dev,
415                                     struct device_attribute *attr,
416                                     const char *buf,
417                                     size_t len)
418 {
419         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
420         int ret;
421
422         mutex_lock(&st->lock);
423         gpio_set_value(st->pdata->sample, 0);
424         /* delay (2 * tck + 20) nano seconds */
425         udelay(1);
426         gpio_set_value(st->pdata->sample, 1);
427         ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
428         if (ret < 0)
429                 goto error_ret;
430         gpio_set_value(st->pdata->sample, 0);
431         gpio_set_value(st->pdata->sample, 1);
432 error_ret:
433         mutex_unlock(&st->lock);
434
435         return ret < 0 ? ret : len;
436 }
437
438 static ssize_t ad2s1210_show_reg(struct device *dev,
439                                  struct device_attribute *attr,
440                                  char *buf)
441 {
442         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
443         struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
444         int ret;
445
446         mutex_lock(&st->lock);
447         ret = ad2s1210_config_read(st, iattr->address);
448         mutex_unlock(&st->lock);
449
450         return ret < 0 ? ret : sprintf(buf, "%d\n", ret);
451 }
452
453 static ssize_t ad2s1210_store_reg(struct device *dev,
454                                   struct device_attribute *attr,
455                                   const char *buf, size_t len)
456 {
457         struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
458         unsigned char data;
459         int ret;
460         struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
461
462         ret = kstrtou8(buf, 10, &data);
463         if (ret)
464                 return -EINVAL;
465         mutex_lock(&st->lock);
466         ret = ad2s1210_config_write(st, iattr->address);
467         if (ret < 0)
468                 goto error_ret;
469         ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
470 error_ret:
471         mutex_unlock(&st->lock);
472         return ret < 0 ? ret : len;
473 }
474
475 static int ad2s1210_read_raw(struct iio_dev *indio_dev,
476                              struct iio_chan_spec const *chan,
477                              int *val,
478                              int *val2,
479                              long m)
480 {
481         struct ad2s1210_state *st = iio_priv(indio_dev);
482         u16 negative;
483         int ret = 0;
484         u16 pos;
485         s16 vel;
486
487         mutex_lock(&st->lock);
488         gpio_set_value(st->pdata->sample, 0);
489         /* delay (6 * tck + 20) nano seconds */
490         udelay(1);
491
492         switch (chan->type) {
493         case IIO_ANGL:
494                 ad2s1210_set_mode(MOD_POS, st);
495                 break;
496         case IIO_ANGL_VEL:
497                 ad2s1210_set_mode(MOD_VEL, st);
498                 break;
499         default:
500                ret = -EINVAL;
501                break;
502         }
503         if (ret < 0)
504                 goto error_ret;
505         ret = spi_read(st->sdev, st->rx, 2);
506         if (ret < 0)
507                 goto error_ret;
508
509         switch (chan->type) {
510         case IIO_ANGL:
511                 pos = be16_to_cpup((__be16 *)st->rx);
512                 if (st->hysteresis)
513                         pos >>= 16 - st->resolution;
514                 *val = pos;
515                 ret = IIO_VAL_INT;
516                 break;
517         case IIO_ANGL_VEL:
518                 negative = st->rx[0] & 0x80;
519                 vel = be16_to_cpup((__be16 *)st->rx);
520                 vel >>= 16 - st->resolution;
521                 if (vel & 0x8000) {
522                         negative = (0xffff >> st->resolution) << st->resolution;
523                         vel |= negative;
524                 }
525                 *val = vel;
526                 ret = IIO_VAL_INT;
527                 break;
528         default:
529                 mutex_unlock(&st->lock);
530                 return -EINVAL;
531         }
532
533 error_ret:
534         gpio_set_value(st->pdata->sample, 1);
535         /* delay (2 * tck + 20) nano seconds */
536         udelay(1);
537         mutex_unlock(&st->lock);
538         return ret;
539 }
540
541 static IIO_DEVICE_ATTR(fclkin, S_IRUGO | S_IWUSR,
542                        ad2s1210_show_fclkin, ad2s1210_store_fclkin, 0);
543 static IIO_DEVICE_ATTR(fexcit, S_IRUGO | S_IWUSR,
544                        ad2s1210_show_fexcit,    ad2s1210_store_fexcit, 0);
545 static IIO_DEVICE_ATTR(control, S_IRUGO | S_IWUSR,
546                        ad2s1210_show_control, ad2s1210_store_control, 0);
547 static IIO_DEVICE_ATTR(bits, S_IRUGO | S_IWUSR,
548                        ad2s1210_show_resolution, ad2s1210_store_resolution, 0);
549 static IIO_DEVICE_ATTR(fault, S_IRUGO | S_IWUSR,
550                        ad2s1210_show_fault, ad2s1210_clear_fault, 0);
551
552 static IIO_DEVICE_ATTR(los_thrd, S_IRUGO | S_IWUSR,
553                        ad2s1210_show_reg, ad2s1210_store_reg,
554                        AD2S1210_REG_LOS_THRD);
555 static IIO_DEVICE_ATTR(dos_ovr_thrd, S_IRUGO | S_IWUSR,
556                        ad2s1210_show_reg, ad2s1210_store_reg,
557                        AD2S1210_REG_DOS_OVR_THRD);
558 static IIO_DEVICE_ATTR(dos_mis_thrd, S_IRUGO | S_IWUSR,
559                        ad2s1210_show_reg, ad2s1210_store_reg,
560                        AD2S1210_REG_DOS_MIS_THRD);
561 static IIO_DEVICE_ATTR(dos_rst_max_thrd, S_IRUGO | S_IWUSR,
562                        ad2s1210_show_reg, ad2s1210_store_reg,
563                        AD2S1210_REG_DOS_RST_MAX_THRD);
564 static IIO_DEVICE_ATTR(dos_rst_min_thrd, S_IRUGO | S_IWUSR,
565                        ad2s1210_show_reg, ad2s1210_store_reg,
566                        AD2S1210_REG_DOS_RST_MIN_THRD);
567 static IIO_DEVICE_ATTR(lot_high_thrd, S_IRUGO | S_IWUSR,
568                        ad2s1210_show_reg, ad2s1210_store_reg,
569                        AD2S1210_REG_LOT_HIGH_THRD);
570 static IIO_DEVICE_ATTR(lot_low_thrd, S_IRUGO | S_IWUSR,
571                        ad2s1210_show_reg, ad2s1210_store_reg,
572                        AD2S1210_REG_LOT_LOW_THRD);
573
574 static const struct iio_chan_spec ad2s1210_channels[] = {
575         {
576                 .type = IIO_ANGL,
577                 .indexed = 1,
578                 .channel = 0,
579                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
580         }, {
581                 .type = IIO_ANGL_VEL,
582                 .indexed = 1,
583                 .channel = 0,
584                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
585         }
586 };
587
588 static struct attribute *ad2s1210_attributes[] = {
589         &iio_dev_attr_fclkin.dev_attr.attr,
590         &iio_dev_attr_fexcit.dev_attr.attr,
591         &iio_dev_attr_control.dev_attr.attr,
592         &iio_dev_attr_bits.dev_attr.attr,
593         &iio_dev_attr_fault.dev_attr.attr,
594         &iio_dev_attr_los_thrd.dev_attr.attr,
595         &iio_dev_attr_dos_ovr_thrd.dev_attr.attr,
596         &iio_dev_attr_dos_mis_thrd.dev_attr.attr,
597         &iio_dev_attr_dos_rst_max_thrd.dev_attr.attr,
598         &iio_dev_attr_dos_rst_min_thrd.dev_attr.attr,
599         &iio_dev_attr_lot_high_thrd.dev_attr.attr,
600         &iio_dev_attr_lot_low_thrd.dev_attr.attr,
601         NULL,
602 };
603
604 static const struct attribute_group ad2s1210_attribute_group = {
605         .attrs = ad2s1210_attributes,
606 };
607
608 static int ad2s1210_initial(struct ad2s1210_state *st)
609 {
610         unsigned char data;
611         int ret;
612
613         mutex_lock(&st->lock);
614         if (st->pdata->gpioin)
615                 st->resolution = ad2s1210_read_resolution_pin(st);
616         else
617                 ad2s1210_set_resolution_pin(st);
618
619         ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
620         if (ret < 0)
621                 goto error_ret;
622         data = AD2S1210_DEF_CONTROL & ~(AD2S1210_SET_RESOLUTION);
623         data |= (st->resolution - 10) >> 1;
624         ret = ad2s1210_config_write(st, data);
625         if (ret < 0)
626                 goto error_ret;
627         ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
628         if (ret < 0)
629                 goto error_ret;
630
631         if (ret & AD2S1210_MSB_IS_HIGH) {
632                 ret = -EIO;
633                 goto error_ret;
634         }
635
636         ret = ad2s1210_update_frequency_control_word(st);
637         if (ret < 0)
638                 goto error_ret;
639         ret = ad2s1210_soft_reset(st);
640 error_ret:
641         mutex_unlock(&st->lock);
642         return ret;
643 }
644
645 static const struct iio_info ad2s1210_info = {
646         .read_raw = ad2s1210_read_raw,
647         .attrs = &ad2s1210_attribute_group,
648         .driver_module = THIS_MODULE,
649 };
650
651 static int ad2s1210_setup_gpios(struct ad2s1210_state *st)
652 {
653         unsigned long flags = st->pdata->gpioin ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
654         struct gpio ad2s1210_gpios[] = {
655                 { st->pdata->sample, GPIOF_DIR_IN, "sample" },
656                 { st->pdata->a[0], flags, "a0" },
657                 { st->pdata->a[1], flags, "a1" },
658                 { st->pdata->res[0], flags, "res0" },
659                 { st->pdata->res[0], flags, "res1" },
660         };
661
662         return gpio_request_array(ad2s1210_gpios, ARRAY_SIZE(ad2s1210_gpios));
663 }
664
665 static void ad2s1210_free_gpios(struct ad2s1210_state *st)
666 {
667         unsigned long flags = st->pdata->gpioin ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
668         struct gpio ad2s1210_gpios[] = {
669                 { st->pdata->sample, GPIOF_DIR_IN, "sample" },
670                 { st->pdata->a[0], flags, "a0" },
671                 { st->pdata->a[1], flags, "a1" },
672                 { st->pdata->res[0], flags, "res0" },
673                 { st->pdata->res[0], flags, "res1" },
674         };
675
676         gpio_free_array(ad2s1210_gpios, ARRAY_SIZE(ad2s1210_gpios));
677 }
678
679 static int ad2s1210_probe(struct spi_device *spi)
680 {
681         struct iio_dev *indio_dev;
682         struct ad2s1210_state *st;
683         int ret;
684
685         if (!spi->dev.platform_data)
686                 return -EINVAL;
687
688         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
689         if (!indio_dev)
690                 return -ENOMEM;
691         st = iio_priv(indio_dev);
692         st->pdata = spi->dev.platform_data;
693         ret = ad2s1210_setup_gpios(st);
694         if (ret < 0)
695                 return ret;
696
697         spi_set_drvdata(spi, indio_dev);
698
699         mutex_init(&st->lock);
700         st->sdev = spi;
701         st->hysteresis = true;
702         st->mode = MOD_CONFIG;
703         st->resolution = 12;
704         st->fexcit = AD2S1210_DEF_EXCIT;
705
706         indio_dev->dev.parent = &spi->dev;
707         indio_dev->info = &ad2s1210_info;
708         indio_dev->modes = INDIO_DIRECT_MODE;
709         indio_dev->channels = ad2s1210_channels;
710         indio_dev->num_channels = ARRAY_SIZE(ad2s1210_channels);
711         indio_dev->name = spi_get_device_id(spi)->name;
712
713         ret = iio_device_register(indio_dev);
714         if (ret)
715                 goto error_free_gpios;
716
717         st->fclkin = spi->max_speed_hz;
718         spi->mode = SPI_MODE_3;
719         spi_setup(spi);
720         ad2s1210_initial(st);
721
722         return 0;
723
724 error_free_gpios:
725         ad2s1210_free_gpios(st);
726         return ret;
727 }
728
729 static int ad2s1210_remove(struct spi_device *spi)
730 {
731         struct iio_dev *indio_dev = spi_get_drvdata(spi);
732
733         iio_device_unregister(indio_dev);
734         ad2s1210_free_gpios(iio_priv(indio_dev));
735
736         return 0;
737 }
738
739 static const struct spi_device_id ad2s1210_id[] = {
740         { "ad2s1210" },
741         {}
742 };
743 MODULE_DEVICE_TABLE(spi, ad2s1210_id);
744
745 static struct spi_driver ad2s1210_driver = {
746         .driver = {
747                 .name = DRV_NAME,
748         },
749         .probe = ad2s1210_probe,
750         .remove = ad2s1210_remove,
751         .id_table = ad2s1210_id,
752 };
753 module_spi_driver(ad2s1210_driver);
754
755 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
756 MODULE_DESCRIPTION("Analog Devices AD2S1210 Resolver to Digital SPI driver");
757 MODULE_LICENSE("GPL v2");