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> 1356ca9db8SPaul Cercueil #include <linux/of.h> 1456ca9db8SPaul Cercueil #include <linux/spi/spi.h> 15b0570bceSMichael Hennerich #include <linux/acpi.h> 1656ca9db8SPaul Cercueil 1756ca9db8SPaul Cercueil #define AD5592R_GPIO_READBACK_EN BIT(10) 1856ca9db8SPaul Cercueil #define AD5592R_LDAC_READBACK_EN BIT(6) 1956ca9db8SPaul Cercueil 2038e442fcSSandhya Bankar static int ad5592r_spi_wnop_r16(struct ad5592r_state *st, __be16 *buf) 2156ca9db8SPaul Cercueil { 2256ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 2356ca9db8SPaul Cercueil struct spi_transfer t = { 2456ca9db8SPaul Cercueil .tx_buf = &st->spi_msg_nop, 2556ca9db8SPaul Cercueil .rx_buf = buf, 2656ca9db8SPaul Cercueil .len = 2 2756ca9db8SPaul Cercueil }; 2856ca9db8SPaul Cercueil 2956ca9db8SPaul Cercueil st->spi_msg_nop = 0; /* NOP */ 3056ca9db8SPaul Cercueil 3156ca9db8SPaul Cercueil return spi_sync_transfer(spi, &t, 1); 3256ca9db8SPaul Cercueil } 3356ca9db8SPaul Cercueil 3456ca9db8SPaul Cercueil static int ad5592r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value) 3556ca9db8SPaul Cercueil { 3656ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 3756ca9db8SPaul Cercueil 3856ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16(BIT(15) | (chan << 12) | value); 3956ca9db8SPaul Cercueil 4056ca9db8SPaul Cercueil return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 4156ca9db8SPaul Cercueil } 4256ca9db8SPaul Cercueil 4356ca9db8SPaul Cercueil static int ad5592r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value) 4456ca9db8SPaul Cercueil { 4556ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 4656ca9db8SPaul Cercueil int ret; 4756ca9db8SPaul Cercueil 4856ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16((AD5592R_REG_ADC_SEQ << 11) | BIT(chan)); 4956ca9db8SPaul Cercueil 5056ca9db8SPaul Cercueil ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 5156ca9db8SPaul Cercueil if (ret) 5256ca9db8SPaul Cercueil return ret; 5356ca9db8SPaul Cercueil 5456ca9db8SPaul Cercueil /* 5556ca9db8SPaul Cercueil * Invalid data: 5656ca9db8SPaul Cercueil * See Figure 40. Single-Channel ADC Conversion Sequence 5756ca9db8SPaul Cercueil */ 5856ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 5956ca9db8SPaul Cercueil if (ret) 6056ca9db8SPaul Cercueil return ret; 6156ca9db8SPaul Cercueil 6256ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 6356ca9db8SPaul Cercueil if (ret) 6456ca9db8SPaul Cercueil return ret; 6556ca9db8SPaul Cercueil 6656ca9db8SPaul Cercueil *value = be16_to_cpu(st->spi_msg); 6756ca9db8SPaul Cercueil 6856ca9db8SPaul Cercueil return 0; 6956ca9db8SPaul Cercueil } 7056ca9db8SPaul Cercueil 7156ca9db8SPaul Cercueil static int ad5592r_reg_write(struct ad5592r_state *st, u8 reg, u16 value) 7256ca9db8SPaul Cercueil { 7356ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 7456ca9db8SPaul Cercueil 7556ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16((reg << 11) | value); 7656ca9db8SPaul Cercueil 7756ca9db8SPaul Cercueil return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 7856ca9db8SPaul Cercueil } 7956ca9db8SPaul Cercueil 8056ca9db8SPaul Cercueil static int ad5592r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value) 8156ca9db8SPaul Cercueil { 8256ca9db8SPaul Cercueil struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 8356ca9db8SPaul Cercueil int ret; 8456ca9db8SPaul Cercueil 8556ca9db8SPaul Cercueil st->spi_msg = cpu_to_be16((AD5592R_REG_LDAC << 11) | 8656ca9db8SPaul Cercueil AD5592R_LDAC_READBACK_EN | (reg << 2)); 8756ca9db8SPaul Cercueil 8856ca9db8SPaul Cercueil ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 8956ca9db8SPaul Cercueil if (ret) 9056ca9db8SPaul Cercueil return ret; 9156ca9db8SPaul Cercueil 9256ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 9356ca9db8SPaul Cercueil if (ret) 9456ca9db8SPaul Cercueil return ret; 9556ca9db8SPaul Cercueil 9656ca9db8SPaul Cercueil *value = be16_to_cpu(st->spi_msg); 9756ca9db8SPaul Cercueil 9856ca9db8SPaul Cercueil return 0; 9956ca9db8SPaul Cercueil } 10056ca9db8SPaul Cercueil 101*256d4b83SChristophe JAILLET static int ad5592r_gpio_read(struct ad5592r_state *st, u8 *value) 10256ca9db8SPaul Cercueil { 10356ca9db8SPaul Cercueil int ret; 10456ca9db8SPaul Cercueil 10556ca9db8SPaul Cercueil ret = ad5592r_reg_write(st, AD5592R_REG_GPIO_IN_EN, 10656ca9db8SPaul Cercueil AD5592R_GPIO_READBACK_EN | st->gpio_in); 10756ca9db8SPaul Cercueil if (ret) 10856ca9db8SPaul Cercueil return ret; 10956ca9db8SPaul Cercueil 11056ca9db8SPaul Cercueil ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 11156ca9db8SPaul Cercueil if (ret) 11256ca9db8SPaul Cercueil return ret; 11356ca9db8SPaul Cercueil 11456ca9db8SPaul Cercueil *value = (u8) be16_to_cpu(st->spi_msg); 11556ca9db8SPaul Cercueil 11656ca9db8SPaul Cercueil return 0; 11756ca9db8SPaul Cercueil } 11856ca9db8SPaul Cercueil 11956ca9db8SPaul Cercueil static const struct ad5592r_rw_ops ad5592r_rw_ops = { 12056ca9db8SPaul Cercueil .write_dac = ad5592r_write_dac, 12156ca9db8SPaul Cercueil .read_adc = ad5592r_read_adc, 12256ca9db8SPaul Cercueil .reg_write = ad5592r_reg_write, 12356ca9db8SPaul Cercueil .reg_read = ad5592r_reg_read, 124*256d4b83SChristophe JAILLET .gpio_read = ad5592r_gpio_read, 12556ca9db8SPaul Cercueil }; 12656ca9db8SPaul Cercueil 12756ca9db8SPaul Cercueil static int ad5592r_spi_probe(struct spi_device *spi) 12856ca9db8SPaul Cercueil { 12956ca9db8SPaul Cercueil const struct spi_device_id *id = spi_get_device_id(spi); 13056ca9db8SPaul Cercueil 13156ca9db8SPaul Cercueil return ad5592r_probe(&spi->dev, id->name, &ad5592r_rw_ops); 13256ca9db8SPaul Cercueil } 13356ca9db8SPaul Cercueil 13456ca9db8SPaul Cercueil static int ad5592r_spi_remove(struct spi_device *spi) 13556ca9db8SPaul Cercueil { 13656ca9db8SPaul Cercueil return ad5592r_remove(&spi->dev); 13756ca9db8SPaul Cercueil } 13856ca9db8SPaul Cercueil 13956ca9db8SPaul Cercueil static const struct spi_device_id ad5592r_spi_ids[] = { 14056ca9db8SPaul Cercueil { .name = "ad5592r", }, 14156ca9db8SPaul Cercueil {} 14256ca9db8SPaul Cercueil }; 14356ca9db8SPaul Cercueil MODULE_DEVICE_TABLE(spi, ad5592r_spi_ids); 14456ca9db8SPaul Cercueil 14556ca9db8SPaul Cercueil static const struct of_device_id ad5592r_of_match[] = { 14656ca9db8SPaul Cercueil { .compatible = "adi,ad5592r", }, 14756ca9db8SPaul Cercueil {}, 14856ca9db8SPaul Cercueil }; 14956ca9db8SPaul Cercueil MODULE_DEVICE_TABLE(of, ad5592r_of_match); 15056ca9db8SPaul Cercueil 151b0570bceSMichael Hennerich static const struct acpi_device_id ad5592r_acpi_match[] = { 152b0570bceSMichael Hennerich {"ADS5592", }, 153b0570bceSMichael Hennerich { }, 154b0570bceSMichael Hennerich }; 155b0570bceSMichael Hennerich MODULE_DEVICE_TABLE(acpi, ad5592r_acpi_match); 156b0570bceSMichael Hennerich 15756ca9db8SPaul Cercueil static struct spi_driver ad5592r_spi_driver = { 15856ca9db8SPaul Cercueil .driver = { 15956ca9db8SPaul Cercueil .name = "ad5592r", 16056ca9db8SPaul Cercueil .of_match_table = of_match_ptr(ad5592r_of_match), 161b0570bceSMichael Hennerich .acpi_match_table = ACPI_PTR(ad5592r_acpi_match), 16256ca9db8SPaul Cercueil }, 16356ca9db8SPaul Cercueil .probe = ad5592r_spi_probe, 16456ca9db8SPaul Cercueil .remove = ad5592r_spi_remove, 16556ca9db8SPaul Cercueil .id_table = ad5592r_spi_ids, 16656ca9db8SPaul Cercueil }; 16756ca9db8SPaul Cercueil module_spi_driver(ad5592r_spi_driver); 16856ca9db8SPaul Cercueil 16956ca9db8SPaul Cercueil MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>"); 17056ca9db8SPaul Cercueil MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters"); 17156ca9db8SPaul Cercueil MODULE_LICENSE("GPL v2"); 172