1 // SPDX-License-Identifier: GPL-2.0
3 * ad2s1210.c support for the ADI Resolver to Digital Converters: AD2S1210
5 * Copyright (c) 2010-2010 Analog Devices Inc.
7 #include <linux/types.h>
8 #include <linux/mutex.h>
9 #include <linux/device.h>
11 #include <linux/spi/spi.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/delay.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define DRV_NAME "ad2s1210"
23 #define AD2S1210_DEF_CONTROL 0x7E
25 #define AD2S1210_MSB_IS_HIGH 0x80
26 #define AD2S1210_MSB_IS_LOW 0x7F
27 #define AD2S1210_PHASE_LOCK_RANGE_44 0x20
28 #define AD2S1210_ENABLE_HYSTERESIS 0x10
29 #define AD2S1210_SET_ENRES1 0x08
30 #define AD2S1210_SET_ENRES0 0x04
31 #define AD2S1210_SET_RES1 0x02
32 #define AD2S1210_SET_RES0 0x01
34 #define AD2S1210_SET_RESOLUTION (AD2S1210_SET_RES1 | AD2S1210_SET_RES0)
36 #define AD2S1210_REG_POSITION 0x80
37 #define AD2S1210_REG_VELOCITY 0x82
38 #define AD2S1210_REG_LOS_THRD 0x88
39 #define AD2S1210_REG_DOS_OVR_THRD 0x89
40 #define AD2S1210_REG_DOS_MIS_THRD 0x8A
41 #define AD2S1210_REG_DOS_RST_MAX_THRD 0x8B
42 #define AD2S1210_REG_DOS_RST_MIN_THRD 0x8C
43 #define AD2S1210_REG_LOT_HIGH_THRD 0x8D
44 #define AD2S1210_REG_LOT_LOW_THRD 0x8E
45 #define AD2S1210_REG_EXCIT_FREQ 0x91
46 #define AD2S1210_REG_CONTROL 0x92
47 #define AD2S1210_REG_SOFT_RESET 0xF0
48 #define AD2S1210_REG_FAULT 0xFF
50 #define AD2S1210_MIN_CLKIN 6144000
51 #define AD2S1210_MAX_CLKIN 10240000
52 #define AD2S1210_MIN_EXCIT 2000
53 #define AD2S1210_MAX_EXCIT 20000
54 #define AD2S1210_MIN_FCW 0x4
55 #define AD2S1210_MAX_FCW 0x50
57 #define AD2S1210_DEF_EXCIT 10000
74 struct ad2s1210_gpio {
79 static const struct ad2s1210_gpio gpios[] = {
80 [AD2S1210_SAMPLE] = { .name = "adi,sample", .flags = GPIOD_OUT_LOW },
81 [AD2S1210_A0] = { .name = "adi,a0", .flags = GPIOD_OUT_LOW },
82 [AD2S1210_A1] = { .name = "adi,a1", .flags = GPIOD_OUT_LOW },
83 [AD2S1210_RES0] = { .name = "adi,res0", .flags = GPIOD_OUT_LOW },
84 [AD2S1210_RES1] = { .name = "adi,res1", .flags = GPIOD_OUT_LOW },
87 static const unsigned int ad2s1210_resolution_value[] = { 10, 12, 14, 16 };
89 struct ad2s1210_state {
91 struct spi_device *sdev;
92 struct gpio_desc *gpios[5];
97 enum ad2s1210_mode mode;
98 u8 rx[2] __aligned(IIO_DMA_MINALIGN);
102 static const int ad2s1210_mode_vals[4][2] = {
103 [MOD_POS] = { 0, 0 },
104 [MOD_VEL] = { 0, 1 },
105 [MOD_CONFIG] = { 1, 1 },
108 static inline void ad2s1210_set_mode(enum ad2s1210_mode mode,
109 struct ad2s1210_state *st)
111 gpiod_set_value(st->gpios[AD2S1210_A0], ad2s1210_mode_vals[mode][0]);
112 gpiod_set_value(st->gpios[AD2S1210_A1], ad2s1210_mode_vals[mode][1]);
116 /* write 1 bytes (address or data) to the chip */
117 static int ad2s1210_config_write(struct ad2s1210_state *st, u8 data)
121 ad2s1210_set_mode(MOD_CONFIG, st);
123 ret = spi_write(st->sdev, st->tx, 1);
130 /* read value from one of the registers */
131 static int ad2s1210_config_read(struct ad2s1210_state *st,
132 unsigned char address)
134 struct spi_transfer xfers[] = {
137 .rx_buf = &st->rx[0],
138 .tx_buf = &st->tx[0],
142 .rx_buf = &st->rx[1],
143 .tx_buf = &st->tx[1],
148 ad2s1210_set_mode(MOD_CONFIG, st);
149 st->tx[0] = address | AD2S1210_MSB_IS_HIGH;
150 st->tx[1] = AD2S1210_REG_FAULT;
151 ret = spi_sync_transfer(st->sdev, xfers, 2);
159 int ad2s1210_update_frequency_control_word(struct ad2s1210_state *st)
164 fcw = (unsigned char)(st->fexcit * (1 << 15) / st->fclkin);
165 if (fcw < AD2S1210_MIN_FCW || fcw > AD2S1210_MAX_FCW) {
166 dev_err(&st->sdev->dev, "ad2s1210: FCW out of range\n");
170 ret = ad2s1210_config_write(st, AD2S1210_REG_EXCIT_FREQ);
174 return ad2s1210_config_write(st, fcw);
177 static const int ad2s1210_res_pins[4][2] = {
178 { 0, 0 }, {0, 1}, {1, 0}, {1, 1}
181 static inline void ad2s1210_set_resolution_pin(struct ad2s1210_state *st)
183 gpiod_set_value(st->gpios[AD2S1210_RES0],
184 ad2s1210_res_pins[(st->resolution - 10) / 2][0]);
185 gpiod_set_value(st->gpios[AD2S1210_RES1],
186 ad2s1210_res_pins[(st->resolution - 10) / 2][1]);
189 static inline int ad2s1210_soft_reset(struct ad2s1210_state *st)
193 ret = ad2s1210_config_write(st, AD2S1210_REG_SOFT_RESET);
197 return ad2s1210_config_write(st, 0x0);
200 static ssize_t ad2s1210_show_fclkin(struct device *dev,
201 struct device_attribute *attr,
204 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
206 return sprintf(buf, "%u\n", st->fclkin);
209 static ssize_t ad2s1210_store_fclkin(struct device *dev,
210 struct device_attribute *attr,
214 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
218 ret = kstrtouint(buf, 10, &fclkin);
221 if (fclkin < AD2S1210_MIN_CLKIN || fclkin > AD2S1210_MAX_CLKIN) {
222 dev_err(dev, "ad2s1210: fclkin out of range\n");
226 mutex_lock(&st->lock);
229 ret = ad2s1210_update_frequency_control_word(st);
232 ret = ad2s1210_soft_reset(st);
234 mutex_unlock(&st->lock);
236 return ret < 0 ? ret : len;
239 static ssize_t ad2s1210_show_fexcit(struct device *dev,
240 struct device_attribute *attr,
243 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
245 return sprintf(buf, "%u\n", st->fexcit);
248 static ssize_t ad2s1210_store_fexcit(struct device *dev,
249 struct device_attribute *attr,
250 const char *buf, size_t len)
252 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
256 ret = kstrtouint(buf, 10, &fexcit);
259 if (fexcit < AD2S1210_MIN_EXCIT || fexcit > AD2S1210_MAX_EXCIT) {
261 "ad2s1210: excitation frequency out of range\n");
264 mutex_lock(&st->lock);
266 ret = ad2s1210_update_frequency_control_word(st);
269 ret = ad2s1210_soft_reset(st);
271 mutex_unlock(&st->lock);
273 return ret < 0 ? ret : len;
276 static ssize_t ad2s1210_show_control(struct device *dev,
277 struct device_attribute *attr,
280 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
283 mutex_lock(&st->lock);
284 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
285 mutex_unlock(&st->lock);
286 return ret < 0 ? ret : sprintf(buf, "0x%x\n", ret);
289 static ssize_t ad2s1210_store_control(struct device *dev,
290 struct device_attribute *attr,
291 const char *buf, size_t len)
293 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
298 ret = kstrtou8(buf, 16, &udata);
302 mutex_lock(&st->lock);
303 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
306 data = udata & AD2S1210_MSB_IS_LOW;
307 ret = ad2s1210_config_write(st, data);
311 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
314 if (ret & AD2S1210_MSB_IS_HIGH) {
317 "ad2s1210: write control register fail\n");
321 ad2s1210_resolution_value[data & AD2S1210_SET_RESOLUTION];
322 ad2s1210_set_resolution_pin(st);
324 st->hysteresis = !!(data & AD2S1210_ENABLE_HYSTERESIS);
327 mutex_unlock(&st->lock);
331 static ssize_t ad2s1210_show_resolution(struct device *dev,
332 struct device_attribute *attr,
335 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
337 return sprintf(buf, "%d\n", st->resolution);
340 static ssize_t ad2s1210_store_resolution(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf, size_t len)
344 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
349 ret = kstrtou8(buf, 10, &udata);
350 if (ret || udata < 10 || udata > 16) {
351 dev_err(dev, "ad2s1210: resolution out of range\n");
354 mutex_lock(&st->lock);
355 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
359 data &= ~AD2S1210_SET_RESOLUTION;
360 data |= (udata - 10) >> 1;
361 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
364 ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
367 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
371 if (data & AD2S1210_MSB_IS_HIGH) {
373 dev_err(dev, "ad2s1210: setting resolution fail\n");
377 ad2s1210_resolution_value[data & AD2S1210_SET_RESOLUTION];
378 ad2s1210_set_resolution_pin(st);
381 mutex_unlock(&st->lock);
385 /* read the fault register since last sample */
386 static ssize_t ad2s1210_show_fault(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
392 mutex_lock(&st->lock);
393 ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
394 mutex_unlock(&st->lock);
396 return ret ? ret : sprintf(buf, "0x%x\n", ret);
399 static ssize_t ad2s1210_clear_fault(struct device *dev,
400 struct device_attribute *attr,
404 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
407 mutex_lock(&st->lock);
408 gpiod_set_value(st->gpios[AD2S1210_SAMPLE], 0);
409 /* delay (2 * tck + 20) nano seconds */
411 gpiod_set_value(st->gpios[AD2S1210_SAMPLE], 1);
412 ret = ad2s1210_config_read(st, AD2S1210_REG_FAULT);
415 gpiod_set_value(st->gpios[AD2S1210_SAMPLE], 0);
416 gpiod_set_value(st->gpios[AD2S1210_SAMPLE], 1);
418 mutex_unlock(&st->lock);
420 return ret < 0 ? ret : len;
423 static ssize_t ad2s1210_show_reg(struct device *dev,
424 struct device_attribute *attr,
427 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
428 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
431 mutex_lock(&st->lock);
432 ret = ad2s1210_config_read(st, iattr->address);
433 mutex_unlock(&st->lock);
435 return ret < 0 ? ret : sprintf(buf, "%d\n", ret);
438 static ssize_t ad2s1210_store_reg(struct device *dev,
439 struct device_attribute *attr,
440 const char *buf, size_t len)
442 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
445 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
447 ret = kstrtou8(buf, 10, &data);
450 mutex_lock(&st->lock);
451 ret = ad2s1210_config_write(st, iattr->address);
454 ret = ad2s1210_config_write(st, data & AD2S1210_MSB_IS_LOW);
456 mutex_unlock(&st->lock);
457 return ret < 0 ? ret : len;
460 static int ad2s1210_read_raw(struct iio_dev *indio_dev,
461 struct iio_chan_spec const *chan,
466 struct ad2s1210_state *st = iio_priv(indio_dev);
472 mutex_lock(&st->lock);
473 gpiod_set_value(st->gpios[AD2S1210_SAMPLE], 0);
474 /* delay (6 * tck + 20) nano seconds */
477 switch (chan->type) {
479 ad2s1210_set_mode(MOD_POS, st);
482 ad2s1210_set_mode(MOD_VEL, st);
490 ret = spi_read(st->sdev, st->rx, 2);
494 switch (chan->type) {
496 pos = be16_to_cpup((__be16 *)st->rx);
498 pos >>= 16 - st->resolution;
503 vel = be16_to_cpup((__be16 *)st->rx);
504 vel >>= 16 - st->resolution;
506 negative = (0xffff >> st->resolution) << st->resolution;
513 mutex_unlock(&st->lock);
518 gpiod_set_value(st->gpios[AD2S1210_SAMPLE], 1);
519 /* delay (2 * tck + 20) nano seconds */
521 mutex_unlock(&st->lock);
525 static IIO_DEVICE_ATTR(fclkin, 0644,
526 ad2s1210_show_fclkin, ad2s1210_store_fclkin, 0);
527 static IIO_DEVICE_ATTR(fexcit, 0644,
528 ad2s1210_show_fexcit, ad2s1210_store_fexcit, 0);
529 static IIO_DEVICE_ATTR(control, 0644,
530 ad2s1210_show_control, ad2s1210_store_control, 0);
531 static IIO_DEVICE_ATTR(bits, 0644,
532 ad2s1210_show_resolution, ad2s1210_store_resolution, 0);
533 static IIO_DEVICE_ATTR(fault, 0644,
534 ad2s1210_show_fault, ad2s1210_clear_fault, 0);
536 static IIO_DEVICE_ATTR(los_thrd, 0644,
537 ad2s1210_show_reg, ad2s1210_store_reg,
538 AD2S1210_REG_LOS_THRD);
539 static IIO_DEVICE_ATTR(dos_ovr_thrd, 0644,
540 ad2s1210_show_reg, ad2s1210_store_reg,
541 AD2S1210_REG_DOS_OVR_THRD);
542 static IIO_DEVICE_ATTR(dos_mis_thrd, 0644,
543 ad2s1210_show_reg, ad2s1210_store_reg,
544 AD2S1210_REG_DOS_MIS_THRD);
545 static IIO_DEVICE_ATTR(dos_rst_max_thrd, 0644,
546 ad2s1210_show_reg, ad2s1210_store_reg,
547 AD2S1210_REG_DOS_RST_MAX_THRD);
548 static IIO_DEVICE_ATTR(dos_rst_min_thrd, 0644,
549 ad2s1210_show_reg, ad2s1210_store_reg,
550 AD2S1210_REG_DOS_RST_MIN_THRD);
551 static IIO_DEVICE_ATTR(lot_high_thrd, 0644,
552 ad2s1210_show_reg, ad2s1210_store_reg,
553 AD2S1210_REG_LOT_HIGH_THRD);
554 static IIO_DEVICE_ATTR(lot_low_thrd, 0644,
555 ad2s1210_show_reg, ad2s1210_store_reg,
556 AD2S1210_REG_LOT_LOW_THRD);
558 static const struct iio_chan_spec ad2s1210_channels[] = {
563 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
565 .type = IIO_ANGL_VEL,
568 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
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,
588 static const struct attribute_group ad2s1210_attribute_group = {
589 .attrs = ad2s1210_attributes,
592 static int ad2s1210_initial(struct ad2s1210_state *st)
597 mutex_lock(&st->lock);
598 ad2s1210_set_resolution_pin(st);
600 ret = ad2s1210_config_write(st, AD2S1210_REG_CONTROL);
603 data = AD2S1210_DEF_CONTROL & ~(AD2S1210_SET_RESOLUTION);
604 data |= (st->resolution - 10) >> 1;
605 ret = ad2s1210_config_write(st, data);
608 ret = ad2s1210_config_read(st, AD2S1210_REG_CONTROL);
612 if (ret & AD2S1210_MSB_IS_HIGH) {
617 ret = ad2s1210_update_frequency_control_word(st);
620 ret = ad2s1210_soft_reset(st);
622 mutex_unlock(&st->lock);
626 static const struct iio_info ad2s1210_info = {
627 .read_raw = ad2s1210_read_raw,
628 .attrs = &ad2s1210_attribute_group,
631 static int ad2s1210_setup_gpios(struct ad2s1210_state *st)
633 struct spi_device *spi = st->sdev;
636 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
637 st->gpios[i] = devm_gpiod_get(&spi->dev, gpios[i].name,
639 if (IS_ERR(st->gpios[i])) {
640 ret = PTR_ERR(st->gpios[i]);
642 "ad2s1210: failed to request %s GPIO: %d\n",
651 static int ad2s1210_probe(struct spi_device *spi)
653 struct iio_dev *indio_dev;
654 struct ad2s1210_state *st;
657 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
660 st = iio_priv(indio_dev);
661 ret = ad2s1210_setup_gpios(st);
665 spi_set_drvdata(spi, indio_dev);
667 mutex_init(&st->lock);
669 st->hysteresis = true;
670 st->mode = MOD_CONFIG;
672 st->fexcit = AD2S1210_DEF_EXCIT;
674 indio_dev->info = &ad2s1210_info;
675 indio_dev->modes = INDIO_DIRECT_MODE;
676 indio_dev->channels = ad2s1210_channels;
677 indio_dev->num_channels = ARRAY_SIZE(ad2s1210_channels);
678 indio_dev->name = spi_get_device_id(spi)->name;
680 ret = devm_iio_device_register(&spi->dev, indio_dev);
684 st->fclkin = spi->max_speed_hz;
685 spi->mode = SPI_MODE_3;
687 ad2s1210_initial(st);
692 static const struct of_device_id ad2s1210_of_match[] = {
693 { .compatible = "adi,ad2s1210", },
696 MODULE_DEVICE_TABLE(of, ad2s1210_of_match);
698 static const struct spi_device_id ad2s1210_id[] = {
702 MODULE_DEVICE_TABLE(spi, ad2s1210_id);
704 static struct spi_driver ad2s1210_driver = {
707 .of_match_table = of_match_ptr(ad2s1210_of_match),
709 .probe = ad2s1210_probe,
710 .id_table = ad2s1210_id,
712 module_spi_driver(ad2s1210_driver);
715 MODULE_DESCRIPTION("Analog Devices AD2S1210 Resolver to Digital SPI driver");
716 MODULE_LICENSE("GPL v2");