1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for the Analog Devices digital potentiometers (SPI bus) 4 * 5 * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc. 6 */ 7 8 #include <linux/spi/spi.h> 9 #include <linux/module.h> 10 11 #include "ad525x_dpot.h" 12 13 /* SPI bus functions */ 14 static int write8(void *client, u8 val) 15 { 16 u8 data = val; 17 18 return spi_write(client, &data, 1); 19 } 20 21 static int write16(void *client, u8 reg, u8 val) 22 { 23 u8 data[2] = {reg, val}; 24 25 return spi_write(client, data, 2); 26 } 27 28 static int write24(void *client, u8 reg, u16 val) 29 { 30 u8 data[3] = {reg, val >> 8, val}; 31 32 return spi_write(client, data, 3); 33 } 34 35 static int read8(void *client) 36 { 37 int ret; 38 u8 data; 39 40 ret = spi_read(client, &data, 1); 41 if (ret < 0) 42 return ret; 43 44 return data; 45 } 46 47 static int read16(void *client, u8 reg) 48 { 49 int ret; 50 u8 buf_rx[2]; 51 52 write16(client, reg, 0); 53 ret = spi_read(client, buf_rx, 2); 54 if (ret < 0) 55 return ret; 56 57 return (buf_rx[0] << 8) | buf_rx[1]; 58 } 59 60 static int read24(void *client, u8 reg) 61 { 62 int ret; 63 u8 buf_rx[3]; 64 65 write24(client, reg, 0); 66 ret = spi_read(client, buf_rx, 3); 67 if (ret < 0) 68 return ret; 69 70 return (buf_rx[1] << 8) | buf_rx[2]; 71 } 72 73 static const struct ad_dpot_bus_ops bops = { 74 .read_d8 = read8, 75 .read_r8d8 = read16, 76 .read_r8d16 = read24, 77 .write_d8 = write8, 78 .write_r8d8 = write16, 79 .write_r8d16 = write24, 80 }; 81 static int ad_dpot_spi_probe(struct spi_device *spi) 82 { 83 struct ad_dpot_bus_data bdata = { 84 .client = spi, 85 .bops = &bops, 86 }; 87 88 return ad_dpot_probe(&spi->dev, &bdata, 89 spi_get_device_id(spi)->driver_data, 90 spi_get_device_id(spi)->name); 91 } 92 93 static int ad_dpot_spi_remove(struct spi_device *spi) 94 { 95 return ad_dpot_remove(&spi->dev); 96 } 97 98 static const struct spi_device_id ad_dpot_spi_id[] = { 99 {"ad5160", AD5160_ID}, 100 {"ad5161", AD5161_ID}, 101 {"ad5162", AD5162_ID}, 102 {"ad5165", AD5165_ID}, 103 {"ad5200", AD5200_ID}, 104 {"ad5201", AD5201_ID}, 105 {"ad5203", AD5203_ID}, 106 {"ad5204", AD5204_ID}, 107 {"ad5206", AD5206_ID}, 108 {"ad5207", AD5207_ID}, 109 {"ad5231", AD5231_ID}, 110 {"ad5232", AD5232_ID}, 111 {"ad5233", AD5233_ID}, 112 {"ad5235", AD5235_ID}, 113 {"ad5260", AD5260_ID}, 114 {"ad5262", AD5262_ID}, 115 {"ad5263", AD5263_ID}, 116 {"ad5290", AD5290_ID}, 117 {"ad5291", AD5291_ID}, 118 {"ad5292", AD5292_ID}, 119 {"ad5293", AD5293_ID}, 120 {"ad7376", AD7376_ID}, 121 {"ad8400", AD8400_ID}, 122 {"ad8402", AD8402_ID}, 123 {"ad8403", AD8403_ID}, 124 {"adn2850", ADN2850_ID}, 125 {"ad5270", AD5270_ID}, 126 {"ad5271", AD5271_ID}, 127 {} 128 }; 129 MODULE_DEVICE_TABLE(spi, ad_dpot_spi_id); 130 131 static struct spi_driver ad_dpot_spi_driver = { 132 .driver = { 133 .name = "ad_dpot", 134 }, 135 .probe = ad_dpot_spi_probe, 136 .remove = ad_dpot_spi_remove, 137 .id_table = ad_dpot_spi_id, 138 }; 139 140 module_spi_driver(ad_dpot_spi_driver); 141 142 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 143 MODULE_DESCRIPTION("digital potentiometer SPI bus driver"); 144 MODULE_LICENSE("GPL"); 145 MODULE_ALIAS("spi:ad_dpot"); 146