1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
256ca9db8SPaul Cercueil /*
356ca9db8SPaul Cercueil * AD5592R Digital <-> Analog converters driver
456ca9db8SPaul Cercueil *
556ca9db8SPaul Cercueil * Copyright 2015-2016 Analog Devices Inc.
656ca9db8SPaul Cercueil * Author: Paul Cercueil <paul.cercueil@analog.com>
756ca9db8SPaul Cercueil */
856ca9db8SPaul Cercueil
956ca9db8SPaul Cercueil #include "ad5592r-base.h"
1056ca9db8SPaul Cercueil
1156ca9db8SPaul Cercueil #include <linux/bitops.h>
1256ca9db8SPaul Cercueil #include <linux/module.h>
1323636b47SJonathan Cameron #include <linux/mod_devicetable.h>
1456ca9db8SPaul Cercueil #include <linux/spi/spi.h>
1556ca9db8SPaul Cercueil
1656ca9db8SPaul Cercueil #define AD5592R_GPIO_READBACK_EN BIT(10)
1756ca9db8SPaul Cercueil #define AD5592R_LDAC_READBACK_EN BIT(6)
1856ca9db8SPaul Cercueil
ad5592r_spi_wnop_r16(struct ad5592r_state * st,__be16 * buf)1938e442fcSSandhya Bankar static int ad5592r_spi_wnop_r16(struct ad5592r_state *st, __be16 *buf)
2056ca9db8SPaul Cercueil {
2156ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
2256ca9db8SPaul Cercueil struct spi_transfer t = {
2356ca9db8SPaul Cercueil .tx_buf = &st->spi_msg_nop,
2456ca9db8SPaul Cercueil .rx_buf = buf,
2556ca9db8SPaul Cercueil .len = 2
2656ca9db8SPaul Cercueil };
2756ca9db8SPaul Cercueil
2856ca9db8SPaul Cercueil st->spi_msg_nop = 0; /* NOP */
2956ca9db8SPaul Cercueil
3056ca9db8SPaul Cercueil return spi_sync_transfer(spi, &t, 1);
3156ca9db8SPaul Cercueil }
3256ca9db8SPaul Cercueil
ad5592r_write_dac(struct ad5592r_state * st,unsigned chan,u16 value)3356ca9db8SPaul Cercueil static int ad5592r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
3456ca9db8SPaul Cercueil {
3556ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
3656ca9db8SPaul Cercueil
3756ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16(BIT(15) | (chan << 12) | value);
3856ca9db8SPaul Cercueil
3956ca9db8SPaul Cercueil return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
4056ca9db8SPaul Cercueil }
4156ca9db8SPaul Cercueil
ad5592r_read_adc(struct ad5592r_state * st,unsigned chan,u16 * value)4256ca9db8SPaul Cercueil static int ad5592r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
4356ca9db8SPaul Cercueil {
4456ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
4556ca9db8SPaul Cercueil int ret;
4656ca9db8SPaul Cercueil
4756ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16((AD5592R_REG_ADC_SEQ << 11) | BIT(chan));
4856ca9db8SPaul Cercueil
4956ca9db8SPaul Cercueil ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
5056ca9db8SPaul Cercueil if (ret)
5156ca9db8SPaul Cercueil return ret;
5256ca9db8SPaul Cercueil
5356ca9db8SPaul Cercueil /*
5456ca9db8SPaul Cercueil * Invalid data:
5556ca9db8SPaul Cercueil * See Figure 40. Single-Channel ADC Conversion Sequence
5656ca9db8SPaul Cercueil */
5756ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
5856ca9db8SPaul Cercueil if (ret)
5956ca9db8SPaul Cercueil return ret;
6056ca9db8SPaul Cercueil
6156ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
6256ca9db8SPaul Cercueil if (ret)
6356ca9db8SPaul Cercueil return ret;
6456ca9db8SPaul Cercueil
6556ca9db8SPaul Cercueil *value = be16_to_cpu(st->spi_msg);
6656ca9db8SPaul Cercueil
6756ca9db8SPaul Cercueil return 0;
6856ca9db8SPaul Cercueil }
6956ca9db8SPaul Cercueil
ad5592r_reg_write(struct ad5592r_state * st,u8 reg,u16 value)7056ca9db8SPaul Cercueil static int ad5592r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
7156ca9db8SPaul Cercueil {
7256ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
7356ca9db8SPaul Cercueil
7456ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16((reg << 11) | value);
7556ca9db8SPaul Cercueil
7656ca9db8SPaul Cercueil return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
7756ca9db8SPaul Cercueil }
7856ca9db8SPaul Cercueil
ad5592r_reg_read(struct ad5592r_state * st,u8 reg,u16 * value)7956ca9db8SPaul Cercueil static int ad5592r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
8056ca9db8SPaul Cercueil {
8156ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
8256ca9db8SPaul Cercueil int ret;
8356ca9db8SPaul Cercueil
8456ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16((AD5592R_REG_LDAC << 11) |
8556ca9db8SPaul Cercueil AD5592R_LDAC_READBACK_EN | (reg << 2));
8656ca9db8SPaul Cercueil
8756ca9db8SPaul Cercueil ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
8856ca9db8SPaul Cercueil if (ret)
8956ca9db8SPaul Cercueil return ret;
9056ca9db8SPaul Cercueil
9156ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
9256ca9db8SPaul Cercueil if (ret)
9356ca9db8SPaul Cercueil return ret;
9456ca9db8SPaul Cercueil
9556ca9db8SPaul Cercueil *value = be16_to_cpu(st->spi_msg);
9656ca9db8SPaul Cercueil
9756ca9db8SPaul Cercueil return 0;
9856ca9db8SPaul Cercueil }
9956ca9db8SPaul Cercueil
ad5592r_gpio_read(struct ad5592r_state * st,u8 * value)100256d4b83SChristophe JAILLET static int ad5592r_gpio_read(struct ad5592r_state *st, u8 *value)
10156ca9db8SPaul Cercueil {
10256ca9db8SPaul Cercueil int ret;
10356ca9db8SPaul Cercueil
10456ca9db8SPaul Cercueil ret = ad5592r_reg_write(st, AD5592R_REG_GPIO_IN_EN,
10556ca9db8SPaul Cercueil AD5592R_GPIO_READBACK_EN | st->gpio_in);
10656ca9db8SPaul Cercueil if (ret)
10756ca9db8SPaul Cercueil return ret;
10856ca9db8SPaul Cercueil
10956ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
11056ca9db8SPaul Cercueil if (ret)
11156ca9db8SPaul Cercueil return ret;
11256ca9db8SPaul Cercueil
11356ca9db8SPaul Cercueil *value = (u8) be16_to_cpu(st->spi_msg);
11456ca9db8SPaul Cercueil
11556ca9db8SPaul Cercueil return 0;
11656ca9db8SPaul Cercueil }
11756ca9db8SPaul Cercueil
11856ca9db8SPaul Cercueil static const struct ad5592r_rw_ops ad5592r_rw_ops = {
11956ca9db8SPaul Cercueil .write_dac = ad5592r_write_dac,
12056ca9db8SPaul Cercueil .read_adc = ad5592r_read_adc,
12156ca9db8SPaul Cercueil .reg_write = ad5592r_reg_write,
12256ca9db8SPaul Cercueil .reg_read = ad5592r_reg_read,
123256d4b83SChristophe JAILLET .gpio_read = ad5592r_gpio_read,
12456ca9db8SPaul Cercueil };
12556ca9db8SPaul Cercueil
ad5592r_spi_probe(struct spi_device * spi)12656ca9db8SPaul Cercueil static int ad5592r_spi_probe(struct spi_device *spi)
12756ca9db8SPaul Cercueil {
12856ca9db8SPaul Cercueil const struct spi_device_id *id = spi_get_device_id(spi);
12956ca9db8SPaul Cercueil
13056ca9db8SPaul Cercueil return ad5592r_probe(&spi->dev, id->name, &ad5592r_rw_ops);
13156ca9db8SPaul Cercueil }
13256ca9db8SPaul Cercueil
ad5592r_spi_remove(struct spi_device * spi)133a0386bbaSUwe Kleine-König static void ad5592r_spi_remove(struct spi_device *spi)
13456ca9db8SPaul Cercueil {
13572ba4505SUwe Kleine-König ad5592r_remove(&spi->dev);
13656ca9db8SPaul Cercueil }
13756ca9db8SPaul Cercueil
13856ca9db8SPaul Cercueil static const struct spi_device_id ad5592r_spi_ids[] = {
13956ca9db8SPaul Cercueil { .name = "ad5592r", },
14056ca9db8SPaul Cercueil {}
14156ca9db8SPaul Cercueil };
14256ca9db8SPaul Cercueil MODULE_DEVICE_TABLE(spi, ad5592r_spi_ids);
14356ca9db8SPaul Cercueil
14456ca9db8SPaul Cercueil static const struct of_device_id ad5592r_of_match[] = {
14556ca9db8SPaul Cercueil { .compatible = "adi,ad5592r", },
14656ca9db8SPaul Cercueil {},
14756ca9db8SPaul Cercueil };
14856ca9db8SPaul Cercueil MODULE_DEVICE_TABLE(of, ad5592r_of_match);
14956ca9db8SPaul Cercueil
150b0570bceSMichael Hennerich static const struct acpi_device_id ad5592r_acpi_match[] = {
151b0570bceSMichael Hennerich {"ADS5592", },
152b0570bceSMichael Hennerich { },
153b0570bceSMichael Hennerich };
154b0570bceSMichael Hennerich MODULE_DEVICE_TABLE(acpi, ad5592r_acpi_match);
155b0570bceSMichael Hennerich
15656ca9db8SPaul Cercueil static struct spi_driver ad5592r_spi_driver = {
15756ca9db8SPaul Cercueil .driver = {
15856ca9db8SPaul Cercueil .name = "ad5592r",
15923636b47SJonathan Cameron .of_match_table = ad5592r_of_match,
16023636b47SJonathan Cameron .acpi_match_table = ad5592r_acpi_match,
16156ca9db8SPaul Cercueil },
16256ca9db8SPaul Cercueil .probe = ad5592r_spi_probe,
16356ca9db8SPaul Cercueil .remove = ad5592r_spi_remove,
16456ca9db8SPaul Cercueil .id_table = ad5592r_spi_ids,
16556ca9db8SPaul Cercueil };
16656ca9db8SPaul Cercueil module_spi_driver(ad5592r_spi_driver);
16756ca9db8SPaul Cercueil
16856ca9db8SPaul Cercueil MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
16956ca9db8SPaul Cercueil MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
17056ca9db8SPaul Cercueil MODULE_LICENSE("GPL v2");
171*4dcd7384SJonathan Cameron MODULE_IMPORT_NS(IIO_AD5592R);
172