1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * AD5672R, AD5674R, AD5676, AD5676R, AD5679R, 4 * AD5681R, AD5682R, AD5683, AD5683R, AD5684, 5 * AD5684R, AD5685R, AD5686, AD5686R 6 * Digital to analog converters driver 7 * 8 * Copyright 2018 Analog Devices Inc. 9 */ 10 11 #include <linux/array_size.h> 12 #include <linux/errno.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/module.h> 15 #include <linux/spi/spi.h> 16 17 #include <asm/byteorder.h> 18 19 #include "ad5686.h" 20 21 static int ad5686_spi_write(struct ad5686_state *st, 22 u8 cmd, u8 addr, u16 val) 23 { 24 struct spi_device *spi = to_spi_device(st->dev); 25 u8 tx_len, *buf; 26 27 switch (st->chip_info->regmap_type) { 28 case AD5310_REGMAP: 29 st->data[0].d16 = cpu_to_be16(AD5310_CMD(cmd) | 30 val); 31 buf = &st->data[0].d8[0]; 32 tx_len = 2; 33 break; 34 case AD5683_REGMAP: 35 st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | 36 AD5683_DATA(val)); 37 buf = &st->data[0].d8[1]; 38 tx_len = 3; 39 break; 40 case AD5686_REGMAP: 41 st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | 42 AD5686_ADDR(addr) | 43 val); 44 buf = &st->data[0].d8[1]; 45 tx_len = 3; 46 break; 47 default: 48 return -EINVAL; 49 } 50 51 return spi_write(spi, buf, tx_len); 52 } 53 54 static int ad5686_spi_read(struct ad5686_state *st, u8 addr) 55 { 56 struct spi_transfer t[] = { 57 { 58 .tx_buf = &st->data[0].d8[1], 59 .len = 3, 60 .cs_change = 1, 61 }, { 62 .tx_buf = &st->data[1].d8[1], 63 .rx_buf = &st->data[2].d8[1], 64 .len = 3, 65 }, 66 }; 67 struct spi_device *spi = to_spi_device(st->dev); 68 u8 cmd = 0; 69 int ret; 70 71 switch (st->chip_info->regmap_type) { 72 case AD5310_REGMAP: 73 return -ENOTSUPP; 74 case AD5683_REGMAP: 75 cmd = AD5686_CMD_READBACK_ENABLE_V2; 76 break; 77 case AD5686_REGMAP: 78 cmd = AD5686_CMD_READBACK_ENABLE; 79 break; 80 default: 81 return -EINVAL; 82 } 83 84 st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | 85 AD5686_ADDR(addr)); 86 st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP)); 87 88 ret = spi_sync_transfer(spi, t, ARRAY_SIZE(t)); 89 if (ret < 0) 90 return ret; 91 92 return be32_to_cpu(st->data[2].d32); 93 } 94 95 static const struct ad5686_bus_ops ad5686_spi_ops = { 96 .write = ad5686_spi_write, 97 .read = ad5686_spi_read, 98 }; 99 100 static int ad5686_spi_probe(struct spi_device *spi) 101 { 102 return ad5686_probe(&spi->dev, spi_get_device_match_data(spi), 103 spi->modalias, &ad5686_spi_ops); 104 } 105 106 static const struct spi_device_id ad5686_spi_id[] = { 107 { .name = "ad5310r", .driver_data = (kernel_ulong_t)&ad5310r_chip_info }, 108 { .name = "ad5672r", .driver_data = (kernel_ulong_t)&ad5672r_chip_info }, 109 { .name = "ad5674r", .driver_data = (kernel_ulong_t)&ad5674r_chip_info }, 110 { .name = "ad5676", .driver_data = (kernel_ulong_t)&ad5676_chip_info }, 111 { .name = "ad5676r", .driver_data = (kernel_ulong_t)&ad5676r_chip_info }, 112 { .name = "ad5679r", .driver_data = (kernel_ulong_t)&ad5679r_chip_info }, 113 { .name = "ad5681r", .driver_data = (kernel_ulong_t)&ad5681r_chip_info }, 114 { .name = "ad5682r", .driver_data = (kernel_ulong_t)&ad5682r_chip_info }, 115 { .name = "ad5683", .driver_data = (kernel_ulong_t)&ad5683_chip_info }, 116 { .name = "ad5683r", .driver_data = (kernel_ulong_t)&ad5683r_chip_info }, 117 { .name = "ad5684", .driver_data = (kernel_ulong_t)&ad5684_chip_info }, 118 { .name = "ad5684r", .driver_data = (kernel_ulong_t)&ad5684r_chip_info }, 119 { .name = "ad5685", .driver_data = (kernel_ulong_t)&ad5685r_chip_info }, /* nonexistent */ 120 { .name = "ad5685r", .driver_data = (kernel_ulong_t)&ad5685r_chip_info }, 121 { .name = "ad5686", .driver_data = (kernel_ulong_t)&ad5686_chip_info }, 122 { .name = "ad5686r", .driver_data = (kernel_ulong_t)&ad5686r_chip_info }, 123 { } 124 }; 125 MODULE_DEVICE_TABLE(spi, ad5686_spi_id); 126 127 static const struct of_device_id ad5686_of_match[] = { 128 { .compatible = "adi,ad5310r", .data = &ad5310r_chip_info }, 129 { .compatible = "adi,ad5672r", .data = &ad5672r_chip_info }, 130 { .compatible = "adi,ad5674r", .data = &ad5674r_chip_info }, 131 { .compatible = "adi,ad5676", .data = &ad5676_chip_info }, 132 { .compatible = "adi,ad5676r", .data = &ad5676r_chip_info }, 133 { .compatible = "adi,ad5679r", .data = &ad5679r_chip_info }, 134 { .compatible = "adi,ad5681r", .data = &ad5681r_chip_info }, 135 { .compatible = "adi,ad5682r", .data = &ad5682r_chip_info }, 136 { .compatible = "adi,ad5683", .data = &ad5683_chip_info }, 137 { .compatible = "adi,ad5683r", .data = &ad5683r_chip_info }, 138 { .compatible = "adi,ad5684", .data = &ad5684_chip_info }, 139 { .compatible = "adi,ad5684r", .data = &ad5684r_chip_info }, 140 { .compatible = "adi,ad5685r", .data = &ad5685r_chip_info }, 141 { .compatible = "adi,ad5686", .data = &ad5686_chip_info }, 142 { .compatible = "adi,ad5686r", .data = &ad5686r_chip_info }, 143 { } 144 }; 145 MODULE_DEVICE_TABLE(of, ad5686_of_match); 146 147 static struct spi_driver ad5686_spi_driver = { 148 .driver = { 149 .name = "ad5686", 150 .of_match_table = ad5686_of_match, 151 }, 152 .probe = ad5686_spi_probe, 153 .id_table = ad5686_spi_id, 154 }; 155 156 module_spi_driver(ad5686_spi_driver); 157 158 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>"); 159 MODULE_DESCRIPTION("Analog Devices AD5686 and similar multi-channel DACs"); 160 MODULE_LICENSE("GPL v2"); 161 MODULE_IMPORT_NS("IIO_AD5686"); 162