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