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 },
102 static inline void ad2s1210_set_mode(enum ad2s1210_mode mode,
103 struct ad2s1210_state *st)
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]);
110 /* write 1 bytes (address or data) to the chip */
111 static int ad2s1210_config_write(struct ad2s1210_state *st, u8 data)
115 ad2s1210_set_mode(MOD_CONFIG, st);
117 ret = spi_write(st->sdev, st->tx, 1);
125 /* read value from one of the registers */
126 static int ad2s1210_config_read(struct ad2s1210_state *st,
127 unsigned char address)
129 struct spi_transfer xfers[] = {
132 .rx_buf = &st->rx[0],
133 .tx_buf = &st->tx[0],
137 .rx_buf = &st->rx[1],
138 .tx_buf = &st->tx[1],
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);
155 int ad2s1210_update_frequency_control_word(struct ad2s1210_state *st)
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");
166 ret = ad2s1210_config_write(st, AD2S1210_REG_EXCIT_FREQ);
170 return ad2s1210_config_write(st, fcw);
173 static unsigned char ad2s1210_read_resolution_pin(struct ad2s1210_state *st)
175 return ad2s1210_resolution_value[
176 (gpio_get_value(st->pdata->res[0]) << 1) |
177 gpio_get_value(st->pdata->res[1])];
180 static const int ad2s1210_res_pins[4][2] = {
181 { 0, 0 }, {0, 1}, {1, 0}, {1, 1}
184 static inline void ad2s1210_set_resolution_pin(struct ad2s1210_state *st)
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]);
192 static inline int ad2s1210_soft_reset(struct ad2s1210_state *st)
196 ret = ad2s1210_config_write(st, AD2S1210_REG_SOFT_RESET);
200 return ad2s1210_config_write(st, 0x0);
203 static ssize_t ad2s1210_show_fclkin(struct device *dev,
204 struct device_attribute *attr,
207 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
209 return sprintf(buf, "%u\n", st->fclkin);
212 static ssize_t ad2s1210_store_fclkin(struct device *dev,
213 struct device_attribute *attr,
217 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
221 ret = kstrtouint(buf, 10, &fclkin);
224 if (fclkin < AD2S1210_MIN_CLKIN || fclkin > AD2S1210_MAX_CLKIN) {
225 dev_err(dev, "ad2s1210: fclkin out of range\n");
229 mutex_lock(&st->lock);
232 ret = ad2s1210_update_frequency_control_word(st);
235 ret = ad2s1210_soft_reset(st);
237 mutex_unlock(&st->lock);
239 return ret < 0 ? ret : len;
242 static ssize_t ad2s1210_show_fexcit(struct device *dev,
243 struct device_attribute *attr,
246 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
248 return sprintf(buf, "%u\n", st->fexcit);
251 static ssize_t ad2s1210_store_fexcit(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t len)
255 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
259 ret = kstrtouint(buf, 10, &fexcit);
262 if (fexcit < AD2S1210_MIN_EXCIT || fexcit > AD2S1210_MAX_EXCIT) {
264 "ad2s1210: excitation frequency out of range\n");
267 mutex_lock(&st->lock);
269 ret = ad2s1210_update_frequency_control_word(st);
272 ret = ad2s1210_soft_reset(st);
274 mutex_unlock(&st->lock);
276 return ret < 0 ? ret : len;
279 static ssize_t ad2s1210_show_control(struct device *dev,
280 struct device_attribute *attr,
283 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
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);
292 static ssize_t ad2s1210_store_control(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t len)
296 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
301 ret = kstrtou8(buf, 16, &udata);
305 mutex_lock(&st->lock);
306 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
309 data = udata & AD2S1210_MSB_IS_LOW;
310 ret = ad2s1210_config_write(st, data);
314 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
317 if (ret & AD2S1210_MSB_IS_HIGH) {
320 "ad2s1210: write control register fail\n");
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");
330 ad2s1210_set_resolution_pin(st);
333 st->hysteresis = !!(data & AD2S1210_ENABLE_HYSTERESIS);
336 mutex_unlock(&st->lock);
340 static ssize_t ad2s1210_show_resolution(struct device *dev,
341 struct device_attribute *attr,
344 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
346 return sprintf(buf, "%d\n", st->resolution);
349 static ssize_t ad2s1210_store_resolution(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf, size_t len)
353 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
358 ret = kstrtou8(buf, 10, &udata);
359 if (ret || udata < 10 || udata > 16) {
360 dev_err(dev, "ad2s1210: resolution out of range\n");
363 mutex_lock(&st->lock);
364 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
368 data &= ~AD2S1210_SET_RESOLUTION;
369 data |= (udata - 10) >> 1;
370 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
373 ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
376 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
380 if (data & AD2S1210_MSB_IS_HIGH) {
382 dev_err(dev, "ad2s1210: setting resolution fail\n");
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");
392 ad2s1210_set_resolution_pin(st);
396 mutex_unlock(&st->lock);
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)
404 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
407 mutex_lock(&st->lock);
408 ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
409 mutex_unlock(&st->lock);
411 return ret ? ret : sprintf(buf, "0x%x\n", ret);
414 static ssize_t ad2s1210_clear_fault(struct device *dev,
415 struct device_attribute *attr,
419 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
422 mutex_lock(&st->lock);
423 gpio_set_value(st->pdata->sample, 0);
424 /* delay (2 * tck + 20) nano seconds */
426 gpio_set_value(st->pdata->sample, 1);
427 ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
430 gpio_set_value(st->pdata->sample, 0);
431 gpio_set_value(st->pdata->sample, 1);
433 mutex_unlock(&st->lock);
435 return ret < 0 ? ret : len;
438 static ssize_t ad2s1210_show_reg(struct device *dev,
439 struct device_attribute *attr,
442 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
443 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
446 mutex_lock(&st->lock);
447 ret = ad2s1210_config_read(st, iattr->address);
448 mutex_unlock(&st->lock);
450 return ret < 0 ? ret : sprintf(buf, "%d\n", ret);
453 static ssize_t ad2s1210_store_reg(struct device *dev,
454 struct device_attribute *attr,
455 const char *buf, size_t len)
457 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
460 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
462 ret = kstrtou8(buf, 10, &data);
465 mutex_lock(&st->lock);
466 ret = ad2s1210_config_write(st, iattr->address);
469 ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
471 mutex_unlock(&st->lock);
472 return ret < 0 ? ret : len;
475 static int ad2s1210_read_raw(struct iio_dev *indio_dev,
476 struct iio_chan_spec const *chan,
481 struct ad2s1210_state *st = iio_priv(indio_dev);
487 mutex_lock(&st->lock);
488 gpio_set_value(st->pdata->sample, 0);
489 /* delay (6 * tck + 20) nano seconds */
492 switch (chan->type) {
494 ad2s1210_set_mode(MOD_POS, st);
497 ad2s1210_set_mode(MOD_VEL, st);
505 ret = spi_read(st->sdev, st->rx, 2);
509 switch (chan->type) {
511 pos = be16_to_cpup((__be16 *)st->rx);
513 pos >>= 16 - st->resolution;
518 negative = st->rx[0] & 0x80;
519 vel = be16_to_cpup((__be16 *)st->rx);
520 vel >>= 16 - st->resolution;
522 negative = (0xffff >> st->resolution) << st->resolution;
529 mutex_unlock(&st->lock);
534 gpio_set_value(st->pdata->sample, 1);
535 /* delay (2 * tck + 20) nano seconds */
537 mutex_unlock(&st->lock);
541 static IIO_DEVICE_ATTR(fclkin, 0644,
542 ad2s1210_show_fclkin, ad2s1210_store_fclkin, 0);
543 static IIO_DEVICE_ATTR(fexcit, 0644,
544 ad2s1210_show_fexcit, ad2s1210_store_fexcit, 0);
545 static IIO_DEVICE_ATTR(control, 0644,
546 ad2s1210_show_control, ad2s1210_store_control, 0);
547 static IIO_DEVICE_ATTR(bits, 0644,
548 ad2s1210_show_resolution, ad2s1210_store_resolution, 0);
549 static IIO_DEVICE_ATTR(fault, 0644,
550 ad2s1210_show_fault, ad2s1210_clear_fault, 0);
552 static IIO_DEVICE_ATTR(los_thrd, 0644,
553 ad2s1210_show_reg, ad2s1210_store_reg,
554 AD2S1210_REG_LOS_THRD);
555 static IIO_DEVICE_ATTR(dos_ovr_thrd, 0644,
556 ad2s1210_show_reg, ad2s1210_store_reg,
557 AD2S1210_REG_DOS_OVR_THRD);
558 static IIO_DEVICE_ATTR(dos_mis_thrd, 0644,
559 ad2s1210_show_reg, ad2s1210_store_reg,
560 AD2S1210_REG_DOS_MIS_THRD);
561 static IIO_DEVICE_ATTR(dos_rst_max_thrd, 0644,
562 ad2s1210_show_reg, ad2s1210_store_reg,
563 AD2S1210_REG_DOS_RST_MAX_THRD);
564 static IIO_DEVICE_ATTR(dos_rst_min_thrd, 0644,
565 ad2s1210_show_reg, ad2s1210_store_reg,
566 AD2S1210_REG_DOS_RST_MIN_THRD);
567 static IIO_DEVICE_ATTR(lot_high_thrd, 0644,
568 ad2s1210_show_reg, ad2s1210_store_reg,
569 AD2S1210_REG_LOT_HIGH_THRD);
570 static IIO_DEVICE_ATTR(lot_low_thrd, 0644,
571 ad2s1210_show_reg, ad2s1210_store_reg,
572 AD2S1210_REG_LOT_LOW_THRD);
574 static const struct iio_chan_spec ad2s1210_channels[] = {
579 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
581 .type = IIO_ANGL_VEL,
584 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
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,
604 static const struct attribute_group ad2s1210_attribute_group = {
605 .attrs = ad2s1210_attributes,
608 static int ad2s1210_initial(struct ad2s1210_state *st)
613 mutex_lock(&st->lock);
614 if (st->pdata->gpioin)
615 st->resolution = ad2s1210_read_resolution_pin(st);
617 ad2s1210_set_resolution_pin(st);
619 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
622 data = AD2S1210_DEF_CONTROL & ~(AD2S1210_SET_RESOLUTION);
623 data |= (st->resolution - 10) >> 1;
624 ret = ad2s1210_config_write(st, data);
627 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
631 if (ret & AD2S1210_MSB_IS_HIGH) {
636 ret = ad2s1210_update_frequency_control_word(st);
639 ret = ad2s1210_soft_reset(st);
641 mutex_unlock(&st->lock);
645 static const struct iio_info ad2s1210_info = {
646 .read_raw = ad2s1210_read_raw,
647 .attrs = &ad2s1210_attribute_group,
648 .driver_module = THIS_MODULE,
651 static int ad2s1210_setup_gpios(struct ad2s1210_state *st)
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" },
662 return gpio_request_array(ad2s1210_gpios, ARRAY_SIZE(ad2s1210_gpios));
665 static void ad2s1210_free_gpios(struct ad2s1210_state *st)
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" },
676 gpio_free_array(ad2s1210_gpios, ARRAY_SIZE(ad2s1210_gpios));
679 static int ad2s1210_probe(struct spi_device *spi)
681 struct iio_dev *indio_dev;
682 struct ad2s1210_state *st;
685 if (!spi->dev.platform_data)
688 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
691 st = iio_priv(indio_dev);
692 st->pdata = spi->dev.platform_data;
693 ret = ad2s1210_setup_gpios(st);
697 spi_set_drvdata(spi, indio_dev);
699 mutex_init(&st->lock);
701 st->hysteresis = true;
702 st->mode = MOD_CONFIG;
704 st->fexcit = AD2S1210_DEF_EXCIT;
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;
713 ret = iio_device_register(indio_dev);
715 goto error_free_gpios;
717 st->fclkin = spi->max_speed_hz;
718 spi->mode = SPI_MODE_3;
720 ad2s1210_initial(st);
725 ad2s1210_free_gpios(st);
729 static int ad2s1210_remove(struct spi_device *spi)
731 struct iio_dev *indio_dev = spi_get_drvdata(spi);
733 iio_device_unregister(indio_dev);
734 ad2s1210_free_gpios(iio_priv(indio_dev));
739 static const struct spi_device_id ad2s1210_id[] = {
743 MODULE_DEVICE_TABLE(spi, ad2s1210_id);
745 static struct spi_driver ad2s1210_driver = {
749 .probe = ad2s1210_probe,
750 .remove = ad2s1210_remove,
751 .id_table = ad2s1210_id,
753 module_spi_driver(ad2s1210_driver);
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");