1 /* 2 * AD5592R Digital <-> Analog converters driver 3 * 4 * Copyright 2015-2016 Analog Devices Inc. 5 * Author: Paul Cercueil <paul.cercueil@analog.com> 6 * 7 * Licensed under the GPL-2. 8 */ 9 10 #include "ad5592r-base.h" 11 12 #include <linux/bitops.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/spi/spi.h> 16 #include <linux/acpi.h> 17 18 #define AD5592R_GPIO_READBACK_EN BIT(10) 19 #define AD5592R_LDAC_READBACK_EN BIT(6) 20 21 static int ad5592r_spi_wnop_r16(struct ad5592r_state *st, __be16 *buf) 22 { 23 struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 24 struct spi_transfer t = { 25 .tx_buf = &st->spi_msg_nop, 26 .rx_buf = buf, 27 .len = 2 28 }; 29 30 st->spi_msg_nop = 0; /* NOP */ 31 32 return spi_sync_transfer(spi, &t, 1); 33 } 34 35 static int ad5592r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value) 36 { 37 struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 38 39 st->spi_msg = cpu_to_be16(BIT(15) | (chan << 12) | value); 40 41 return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 42 } 43 44 static int ad5592r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value) 45 { 46 struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 47 int ret; 48 49 st->spi_msg = cpu_to_be16((AD5592R_REG_ADC_SEQ << 11) | BIT(chan)); 50 51 ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 52 if (ret) 53 return ret; 54 55 /* 56 * Invalid data: 57 * See Figure 40. Single-Channel ADC Conversion Sequence 58 */ 59 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 60 if (ret) 61 return ret; 62 63 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 64 if (ret) 65 return ret; 66 67 *value = be16_to_cpu(st->spi_msg); 68 69 return 0; 70 } 71 72 static int ad5592r_reg_write(struct ad5592r_state *st, u8 reg, u16 value) 73 { 74 struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 75 76 st->spi_msg = cpu_to_be16((reg << 11) | value); 77 78 return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 79 } 80 81 static int ad5592r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value) 82 { 83 struct spi_device *spi = container_of(st->dev, struct spi_device, dev); 84 int ret; 85 86 st->spi_msg = cpu_to_be16((AD5592R_REG_LDAC << 11) | 87 AD5592R_LDAC_READBACK_EN | (reg << 2)); 88 89 ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); 90 if (ret) 91 return ret; 92 93 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 94 if (ret) 95 return ret; 96 97 *value = be16_to_cpu(st->spi_msg); 98 99 return 0; 100 } 101 102 static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value) 103 { 104 int ret; 105 106 ret = ad5592r_reg_write(st, AD5592R_REG_GPIO_IN_EN, 107 AD5592R_GPIO_READBACK_EN | st->gpio_in); 108 if (ret) 109 return ret; 110 111 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); 112 if (ret) 113 return ret; 114 115 *value = (u8) be16_to_cpu(st->spi_msg); 116 117 return 0; 118 } 119 120 static const struct ad5592r_rw_ops ad5592r_rw_ops = { 121 .write_dac = ad5592r_write_dac, 122 .read_adc = ad5592r_read_adc, 123 .reg_write = ad5592r_reg_write, 124 .reg_read = ad5592r_reg_read, 125 .gpio_read = ad5593r_gpio_read, 126 }; 127 128 static int ad5592r_spi_probe(struct spi_device *spi) 129 { 130 const struct spi_device_id *id = spi_get_device_id(spi); 131 132 return ad5592r_probe(&spi->dev, id->name, &ad5592r_rw_ops); 133 } 134 135 static int ad5592r_spi_remove(struct spi_device *spi) 136 { 137 return ad5592r_remove(&spi->dev); 138 } 139 140 static const struct spi_device_id ad5592r_spi_ids[] = { 141 { .name = "ad5592r", }, 142 {} 143 }; 144 MODULE_DEVICE_TABLE(spi, ad5592r_spi_ids); 145 146 static const struct of_device_id ad5592r_of_match[] = { 147 { .compatible = "adi,ad5592r", }, 148 {}, 149 }; 150 MODULE_DEVICE_TABLE(of, ad5592r_of_match); 151 152 static const struct acpi_device_id ad5592r_acpi_match[] = { 153 {"ADS5592", }, 154 { }, 155 }; 156 MODULE_DEVICE_TABLE(acpi, ad5592r_acpi_match); 157 158 static struct spi_driver ad5592r_spi_driver = { 159 .driver = { 160 .name = "ad5592r", 161 .of_match_table = of_match_ptr(ad5592r_of_match), 162 .acpi_match_table = ACPI_PTR(ad5592r_acpi_match), 163 }, 164 .probe = ad5592r_spi_probe, 165 .remove = ad5592r_spi_remove, 166 .id_table = ad5592r_spi_ids, 167 }; 168 module_spi_driver(ad5592r_spi_driver); 169 170 MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>"); 171 MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters"); 172 MODULE_LICENSE("GPL v2"); 173