2 * ad2s1210.c support for the ADI Resolver to Digital Converters: AD2S1210
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>
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
25 #define DRV_NAME "ad2s1210"
27 #define AD2S1210_DEF_CONTROL 0x7E
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
38 #define AD2S1210_SET_ENRESOLUTION (AD2S1210_SET_ENRES1 | \
40 #define AD2S1210_SET_RESOLUTION (AD2S1210_SET_RES1 | AD2S1210_SET_RES0)
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
56 /* pin SAMPLE, A0, A1, RES0, RES1, is controlled by driver */
57 #define AD2S1210_SAA 3
58 #define AD2S1210_PN (AD2S1210_SAA + AD2S1210_RES)
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
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
80 static const unsigned int ad2s1210_resolution_value[] = { 10, 12, 14, 16 };
82 struct ad2s1210_state {
83 const struct ad2s1210_platform_data *pdata;
85 struct spi_device *sdev;
91 enum ad2s1210_mode mode;
92 u8 rx[2] ____cacheline_aligned;
93 u8 tx[2] ____cacheline_aligned;
96 static const int ad2s1210_mode_vals[4][2] = {
99 [MOD_CONFIG] = { 1, 0 },
101 static inline void ad2s1210_set_mode(enum ad2s1210_mode mode,
102 struct ad2s1210_state *st)
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]);
109 /* write 1 bytes (address or data) to the chip */
110 static int ad2s1210_config_write(struct ad2s1210_state *st, u8 data)
114 ad2s1210_set_mode(MOD_CONFIG, st);
116 ret = spi_write(st->sdev, st->tx, 1);
124 /* read value from one of the registers */
125 static int ad2s1210_config_read(struct ad2s1210_state *st,
126 unsigned char address)
128 struct spi_transfer xfer = {
133 struct spi_message msg;
136 ad2s1210_set_mode(MOD_CONFIG, st);
137 spi_message_init(&msg);
138 spi_message_add_tail(&xfer, &msg);
139 st->tx[0] = address | AD2S1210_MSB_IS_HIGH;
140 st->tx[1] = AD2S1210_REG_FAULT;
141 ret = spi_sync(st->sdev, &msg);
150 int ad2s1210_update_frequency_control_word(struct ad2s1210_state *st)
155 fcw = (unsigned char)(st->fexcit * (1 << 15) / st->fclkin);
156 if (fcw < AD2S1210_MIN_FCW || fcw > AD2S1210_MAX_FCW) {
157 pr_err("ad2s1210: FCW out of range\n");
161 ret = ad2s1210_config_write(st, AD2S1210_REG_EXCIT_FREQ);
165 return ad2s1210_config_write(st, fcw);
168 static unsigned char ad2s1210_read_resolution_pin(struct ad2s1210_state *st)
170 return ad2s1210_resolution_value[
171 (gpio_get_value(st->pdata->res[0]) << 1) |
172 gpio_get_value(st->pdata->res[1])];
175 static const int ad2s1210_res_pins[4][2] = {
176 { 0, 0 }, {0, 1}, {1, 0}, {1, 1}
179 static inline void ad2s1210_set_resolution_pin(struct ad2s1210_state *st)
181 gpio_set_value(st->pdata->res[0],
182 ad2s1210_res_pins[(st->resolution - 10)/2][0]);
183 gpio_set_value(st->pdata->res[1],
184 ad2s1210_res_pins[(st->resolution - 10)/2][1]);
187 static inline int ad2s1210_soft_reset(struct ad2s1210_state *st)
191 ret = ad2s1210_config_write(st, AD2S1210_REG_SOFT_RESET);
195 return ad2s1210_config_write(st, 0x0);
198 static ssize_t ad2s1210_store_softreset(struct device *dev,
199 struct device_attribute *attr,
203 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
206 mutex_lock(&st->lock);
207 ret = ad2s1210_soft_reset(st);
208 mutex_unlock(&st->lock);
210 return ret < 0 ? ret : len;
213 static ssize_t ad2s1210_show_fclkin(struct device *dev,
214 struct device_attribute *attr,
217 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
218 return sprintf(buf, "%d\n", st->fclkin);
221 static ssize_t ad2s1210_store_fclkin(struct device *dev,
222 struct device_attribute *attr,
226 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
227 unsigned long fclkin;
230 ret = strict_strtoul(buf, 10, &fclkin);
233 if (fclkin < AD2S1210_MIN_CLKIN || fclkin > AD2S1210_MAX_CLKIN) {
234 pr_err("ad2s1210: fclkin out of range\n");
238 mutex_lock(&st->lock);
241 ret = ad2s1210_update_frequency_control_word(st);
244 ret = ad2s1210_soft_reset(st);
246 mutex_unlock(&st->lock);
248 return ret < 0 ? ret : len;
251 static ssize_t ad2s1210_show_fexcit(struct device *dev,
252 struct device_attribute *attr,
255 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
256 return sprintf(buf, "%d\n", st->fexcit);
259 static ssize_t ad2s1210_store_fexcit(struct device *dev,
260 struct device_attribute *attr,
261 const char *buf, size_t len)
263 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
264 unsigned long fexcit;
267 ret = strict_strtoul(buf, 10, &fexcit);
270 if (fexcit < AD2S1210_MIN_EXCIT || fexcit > AD2S1210_MAX_EXCIT) {
271 pr_err("ad2s1210: excitation frequency out of range\n");
274 mutex_lock(&st->lock);
276 ret = ad2s1210_update_frequency_control_word(st);
279 ret = ad2s1210_soft_reset(st);
281 mutex_unlock(&st->lock);
283 return ret < 0 ? ret : len;
286 static ssize_t ad2s1210_show_control(struct device *dev,
287 struct device_attribute *attr,
290 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
292 mutex_lock(&st->lock);
293 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
294 mutex_unlock(&st->lock);
295 return ret < 0 ? ret : sprintf(buf, "0x%x\n", ret);
298 static ssize_t ad2s1210_store_control(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf, size_t len)
302 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
307 ret = strict_strtoul(buf, 16, &udata);
311 mutex_lock(&st->lock);
312 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
315 data = udata & AD2S1210_MSB_IS_LOW;
316 ret = ad2s1210_config_write(st, data);
320 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
323 if (ret & AD2S1210_MSB_IS_HIGH) {
325 pr_err("ad2s1210: write control register fail\n");
329 = ad2s1210_resolution_value[data & AD2S1210_SET_RESOLUTION];
330 if (st->pdata->gpioin) {
331 data = ad2s1210_read_resolution_pin(st);
332 if (data != st->resolution)
333 pr_warning("ad2s1210: resolution settings not match\n");
335 ad2s1210_set_resolution_pin(st);
338 st->hysteresis = !!(data & AD2S1210_ENABLE_HYSTERESIS);
341 mutex_unlock(&st->lock);
345 static ssize_t ad2s1210_show_resolution(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
349 return sprintf(buf, "%d\n", st->resolution);
352 static ssize_t ad2s1210_store_resolution(struct device *dev,
353 struct device_attribute *attr,
354 const char *buf, size_t len)
356 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
361 ret = strict_strtoul(buf, 10, &udata);
362 if (ret || udata < 10 || udata > 16) {
363 pr_err("ad2s1210: resolution out of range\n");
366 mutex_lock(&st->lock);
367 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
371 data &= ~AD2S1210_SET_RESOLUTION;
372 data |= (udata - 10) >> 1;
373 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
376 ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
379 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
383 if (data & AD2S1210_MSB_IS_HIGH) {
385 pr_err("ad2s1210: setting resolution fail\n");
389 = ad2s1210_resolution_value[data & AD2S1210_SET_RESOLUTION];
390 if (st->pdata->gpioin) {
391 data = ad2s1210_read_resolution_pin(st);
392 if (data != st->resolution)
393 pr_warning("ad2s1210: resolution settings not match\n");
395 ad2s1210_set_resolution_pin(st);
398 mutex_unlock(&st->lock);
402 /* read the fault register since last sample */
403 static ssize_t ad2s1210_show_fault(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
409 mutex_lock(&st->lock);
410 ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
411 mutex_unlock(&st->lock);
413 return ret ? ret : sprintf(buf, "0x%x\n", ret);
416 static ssize_t ad2s1210_clear_fault(struct device *dev,
417 struct device_attribute *attr,
421 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
424 mutex_lock(&st->lock);
425 gpio_set_value(st->pdata->sample, 0);
426 /* delay (2 * tck + 20) nano seconds */
428 gpio_set_value(st->pdata->sample, 1);
429 ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
432 gpio_set_value(st->pdata->sample, 0);
433 gpio_set_value(st->pdata->sample, 1);
435 mutex_unlock(&st->lock);
437 return ret < 0 ? ret : len;
440 static ssize_t ad2s1210_show_reg(struct device *dev,
441 struct device_attribute *attr,
444 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
445 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
448 mutex_lock(&st->lock);
449 ret = ad2s1210_config_read(st, iattr->address);
450 mutex_unlock(&st->lock);
452 return ret < 0 ? ret : sprintf(buf, "%d\n", ret);
455 static ssize_t ad2s1210_store_reg(struct device *dev,
456 struct device_attribute *attr, const char *buf, size_t len)
458 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
461 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
463 ret = strict_strtoul(buf, 10, &data);
466 mutex_lock(&st->lock);
467 ret = ad2s1210_config_write(st, iattr->address);
470 ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
472 mutex_unlock(&st->lock);
473 return ret < 0 ? ret : len;
476 static int ad2s1210_read_raw(struct iio_dev *indio_dev,
477 struct iio_chan_spec const *chan,
482 struct ad2s1210_state *st = iio_priv(indio_dev);
488 mutex_lock(&st->lock);
489 gpio_set_value(st->pdata->sample, 0);
490 /* delay (6 * tck + 20) nano seconds */
493 switch (chan->type) {
495 ad2s1210_set_mode(MOD_POS, st);
498 ad2s1210_set_mode(MOD_VEL, st);
506 ret = spi_read(st->sdev, st->rx, 2);
510 switch (chan->type) {
512 pos = be16_to_cpup((u16 *)st->rx);
514 pos >>= 16 - st->resolution;
519 negative = st->rx[0] & 0x80;
520 vel = be16_to_cpup((s16 *)st->rx);
521 vel >>= 16 - st->resolution;
523 negative = (0xffff >> st->resolution) << st->resolution;
530 mutex_unlock(&st->lock);
535 gpio_set_value(st->pdata->sample, 1);
536 /* delay (2 * tck + 20) nano seconds */
538 mutex_unlock(&st->lock);
542 static IIO_DEVICE_ATTR(reset, S_IWUSR,
543 NULL, ad2s1210_store_softreset, 0);
544 static IIO_DEVICE_ATTR(fclkin, S_IRUGO | S_IWUSR,
545 ad2s1210_show_fclkin, ad2s1210_store_fclkin, 0);
546 static IIO_DEVICE_ATTR(fexcit, S_IRUGO | S_IWUSR,
547 ad2s1210_show_fexcit, ad2s1210_store_fexcit, 0);
548 static IIO_DEVICE_ATTR(control, S_IRUGO | S_IWUSR,
549 ad2s1210_show_control, ad2s1210_store_control, 0);
550 static IIO_DEVICE_ATTR(bits, S_IRUGO | S_IWUSR,
551 ad2s1210_show_resolution, ad2s1210_store_resolution, 0);
552 static IIO_DEVICE_ATTR(fault, S_IRUGO | S_IWUSR,
553 ad2s1210_show_fault, ad2s1210_clear_fault, 0);
555 static IIO_DEVICE_ATTR(los_thrd, S_IRUGO | S_IWUSR,
556 ad2s1210_show_reg, ad2s1210_store_reg,
557 AD2S1210_REG_LOS_THRD);
558 static IIO_DEVICE_ATTR(dos_ovr_thrd, S_IRUGO | S_IWUSR,
559 ad2s1210_show_reg, ad2s1210_store_reg,
560 AD2S1210_REG_DOS_OVR_THRD);
561 static IIO_DEVICE_ATTR(dos_mis_thrd, S_IRUGO | S_IWUSR,
562 ad2s1210_show_reg, ad2s1210_store_reg,
563 AD2S1210_REG_DOS_MIS_THRD);
564 static IIO_DEVICE_ATTR(dos_rst_max_thrd, S_IRUGO | S_IWUSR,
565 ad2s1210_show_reg, ad2s1210_store_reg,
566 AD2S1210_REG_DOS_RST_MAX_THRD);
567 static IIO_DEVICE_ATTR(dos_rst_min_thrd, S_IRUGO | S_IWUSR,
568 ad2s1210_show_reg, ad2s1210_store_reg,
569 AD2S1210_REG_DOS_RST_MIN_THRD);
570 static IIO_DEVICE_ATTR(lot_high_thrd, S_IRUGO | S_IWUSR,
571 ad2s1210_show_reg, ad2s1210_store_reg,
572 AD2S1210_REG_LOT_HIGH_THRD);
573 static IIO_DEVICE_ATTR(lot_low_thrd, S_IRUGO | S_IWUSR,
574 ad2s1210_show_reg, ad2s1210_store_reg,
575 AD2S1210_REG_LOT_LOW_THRD);
578 static const struct iio_chan_spec ad2s1210_channels[] = {
583 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
585 .type = IIO_ANGL_VEL,
588 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
592 static struct attribute *ad2s1210_attributes[] = {
593 &iio_dev_attr_reset.dev_attr.attr,
594 &iio_dev_attr_fclkin.dev_attr.attr,
595 &iio_dev_attr_fexcit.dev_attr.attr,
596 &iio_dev_attr_control.dev_attr.attr,
597 &iio_dev_attr_bits.dev_attr.attr,
598 &iio_dev_attr_fault.dev_attr.attr,
599 &iio_dev_attr_los_thrd.dev_attr.attr,
600 &iio_dev_attr_dos_ovr_thrd.dev_attr.attr,
601 &iio_dev_attr_dos_mis_thrd.dev_attr.attr,
602 &iio_dev_attr_dos_rst_max_thrd.dev_attr.attr,
603 &iio_dev_attr_dos_rst_min_thrd.dev_attr.attr,
604 &iio_dev_attr_lot_high_thrd.dev_attr.attr,
605 &iio_dev_attr_lot_low_thrd.dev_attr.attr,
609 static const struct attribute_group ad2s1210_attribute_group = {
610 .attrs = ad2s1210_attributes,
613 static int ad2s1210_initial(struct ad2s1210_state *st)
618 mutex_lock(&st->lock);
619 if (st->pdata->gpioin)
620 st->resolution = ad2s1210_read_resolution_pin(st);
622 ad2s1210_set_resolution_pin(st);
624 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
627 data = AD2S1210_DEF_CONTROL & ~(AD2S1210_SET_RESOLUTION);
628 data |= (st->resolution - 10) >> 1;
629 ret = ad2s1210_config_write(st, data);
632 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
636 if (ret & AD2S1210_MSB_IS_HIGH) {
641 ret = ad2s1210_update_frequency_control_word(st);
644 ret = ad2s1210_soft_reset(st);
646 mutex_unlock(&st->lock);
650 static const struct iio_info ad2s1210_info = {
651 .read_raw = &ad2s1210_read_raw,
652 .attrs = &ad2s1210_attribute_group,
653 .driver_module = THIS_MODULE,
656 static int ad2s1210_setup_gpios(struct ad2s1210_state *st)
658 unsigned long flags = st->pdata->gpioin ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
659 struct gpio ad2s1210_gpios[] = {
660 { st->pdata->sample, GPIOF_DIR_IN, "sample" },
661 { st->pdata->a[0], flags, "a0" },
662 { st->pdata->a[1], flags, "a1" },
663 { st->pdata->res[0], flags, "res0" },
664 { st->pdata->res[0], flags, "res1" },
667 return gpio_request_array(ad2s1210_gpios, ARRAY_SIZE(ad2s1210_gpios));
670 static void ad2s1210_free_gpios(struct ad2s1210_state *st)
672 unsigned long flags = st->pdata->gpioin ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
673 struct gpio ad2s1210_gpios[] = {
674 { st->pdata->sample, GPIOF_DIR_IN, "sample" },
675 { st->pdata->a[0], flags, "a0" },
676 { st->pdata->a[1], flags, "a1" },
677 { st->pdata->res[0], flags, "res0" },
678 { st->pdata->res[0], flags, "res1" },
681 gpio_free_array(ad2s1210_gpios, ARRAY_SIZE(ad2s1210_gpios));
684 static int ad2s1210_probe(struct spi_device *spi)
686 struct iio_dev *indio_dev;
687 struct ad2s1210_state *st;
690 if (spi->dev.platform_data == NULL)
693 indio_dev = iio_device_alloc(sizeof(*st));
694 if (indio_dev == NULL) {
698 st = iio_priv(indio_dev);
699 st->pdata = spi->dev.platform_data;
700 ret = ad2s1210_setup_gpios(st);
704 spi_set_drvdata(spi, indio_dev);
706 mutex_init(&st->lock);
708 st->hysteresis = true;
709 st->mode = MOD_CONFIG;
711 st->fexcit = AD2S1210_DEF_EXCIT;
713 indio_dev->dev.parent = &spi->dev;
714 indio_dev->info = &ad2s1210_info;
715 indio_dev->modes = INDIO_DIRECT_MODE;
716 indio_dev->channels = ad2s1210_channels;
717 indio_dev->num_channels = ARRAY_SIZE(ad2s1210_channels);
718 indio_dev->name = spi_get_device_id(spi)->name;
720 ret = iio_device_register(indio_dev);
722 goto error_free_gpios;
724 st->fclkin = spi->max_speed_hz;
725 spi->mode = SPI_MODE_3;
727 ad2s1210_initial(st);
732 ad2s1210_free_gpios(st);
734 iio_device_free(indio_dev);
739 static int ad2s1210_remove(struct spi_device *spi)
741 struct iio_dev *indio_dev = spi_get_drvdata(spi);
743 iio_device_unregister(indio_dev);
744 ad2s1210_free_gpios(iio_priv(indio_dev));
745 iio_device_free(indio_dev);
750 static const struct spi_device_id ad2s1210_id[] = {
754 MODULE_DEVICE_TABLE(spi, ad2s1210_id);
756 static struct spi_driver ad2s1210_driver = {
759 .owner = THIS_MODULE,
761 .probe = ad2s1210_probe,
762 .remove = ad2s1210_remove,
763 .id_table = ad2s1210_id,
765 module_spi_driver(ad2s1210_driver);
767 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
768 MODULE_DESCRIPTION("Analog Devices AD2S1210 Resolver to Digital SPI driver");
769 MODULE_LICENSE("GPL v2");