1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ads7871 - driver for TI ADS7871 A/D converter 4 * 5 * Copyright (c) 2010 Paul Thomas <pthomas8589@gmail.com> 6 * 7 * You need to have something like this in struct spi_board_info 8 * { 9 * .modalias = "ads7871", 10 * .max_speed_hz = 2*1000*1000, 11 * .chip_select = 0, 12 * .bus_num = 1, 13 * }, 14 */ 15 16 /*From figure 18 in the datasheet*/ 17 /*Register addresses*/ 18 #define REG_LS_BYTE 0 /*A/D Output Data, LS Byte*/ 19 #define REG_MS_BYTE 1 /*A/D Output Data, MS Byte*/ 20 #define REG_PGA_VALID 2 /*PGA Valid Register*/ 21 #define REG_AD_CONTROL 3 /*A/D Control Register*/ 22 #define REG_GAIN_MUX 4 /*Gain/Mux Register*/ 23 #define REG_IO_STATE 5 /*Digital I/O State Register*/ 24 #define REG_IO_CONTROL 6 /*Digital I/O Control Register*/ 25 #define REG_OSC_CONTROL 7 /*Rev/Oscillator Control Register*/ 26 #define REG_SER_CONTROL 24 /*Serial Interface Control Register*/ 27 #define REG_ID 31 /*ID Register*/ 28 29 /* 30 * From figure 17 in the datasheet 31 * These bits get ORed with the address to form 32 * the instruction byte 33 */ 34 /*Instruction Bit masks*/ 35 #define INST_MODE_BM (1 << 7) 36 #define INST_READ_BM (1 << 6) 37 #define INST_16BIT_BM (1 << 5) 38 39 /*From figure 18 in the datasheet*/ 40 /*bit masks for Rev/Oscillator Control Register*/ 41 #define MUX_CNV_BV 7 42 #define MUX_CNV_BM (1 << MUX_CNV_BV) 43 #define MUX_M3_BM (1 << 3) /*M3 selects single ended*/ 44 #define MUX_G_BV 4 /*allows for reg = (gain << MUX_G_BV) | ...*/ 45 46 /*From figure 18 in the datasheet*/ 47 /*bit masks for Rev/Oscillator Control Register*/ 48 #define OSC_OSCR_BM (1 << 5) 49 #define OSC_OSCE_BM (1 << 4) 50 #define OSC_REFE_BM (1 << 3) 51 #define OSC_BUFE_BM (1 << 2) 52 #define OSC_R2V_BM (1 << 1) 53 #define OSC_RBG_BM (1 << 0) 54 55 #include <linux/module.h> 56 #include <linux/init.h> 57 #include <linux/spi/spi.h> 58 #include <linux/hwmon.h> 59 #include <linux/err.h> 60 #include <linux/delay.h> 61 62 #define DEVICE_NAME "ads7871" 63 64 struct ads7871_data { 65 struct spi_device *spi; 66 u8 tx_buf[2] ____cacheline_aligned; 67 }; 68 69 static umode_t ads7871_is_visible(const void *data, 70 enum hwmon_sensor_types type, 71 u32 attr, int channel) 72 { 73 if (type == hwmon_in && attr == hwmon_in_input) 74 return 0444; 75 76 return 0; 77 } 78 79 static int ads7871_read_reg8(struct spi_device *spi, int reg) 80 { 81 int ret; 82 reg = reg | INST_READ_BM; 83 ret = spi_w8r8(spi, reg); 84 return ret; 85 } 86 87 static int ads7871_read_reg16(struct spi_device *spi, int reg) 88 { 89 int ret; 90 91 reg = reg | INST_READ_BM | INST_16BIT_BM; 92 ret = spi_w8r16(spi, reg); 93 if (ret < 0) 94 return ret; 95 96 return le16_to_cpu((__force __le16)ret); 97 } 98 99 static int ads7871_write_reg8(struct ads7871_data *pdata, int reg, u8 val) 100 { 101 pdata->tx_buf[0] = reg; 102 pdata->tx_buf[1] = val; 103 104 return spi_write(pdata->spi, pdata->tx_buf, 2); 105 } 106 107 static int ads7871_read(struct device *dev, enum hwmon_sensor_types type, 108 u32 attr, int channel, long *val) 109 { 110 struct ads7871_data *pdata = dev_get_drvdata(dev); 111 struct spi_device *spi = pdata->spi; 112 int ret, raw_val, i = 0; 113 u8 mux_cnv; 114 115 if (type != hwmon_in || attr != hwmon_in_input) 116 return -EOPNOTSUPP; 117 /* 118 * TODO: add support for conversions 119 * other than single ended with a gain of 1 120 */ 121 /*MUX_M3_BM forces single ended*/ 122 /*This is also where the gain of the PGA would be set*/ 123 ret = ads7871_write_reg8(pdata, REG_GAIN_MUX, 124 (MUX_CNV_BM | MUX_M3_BM | channel)); 125 if (ret < 0) 126 return ret; 127 128 ret = ads7871_read_reg8(spi, REG_GAIN_MUX); 129 if (ret < 0) 130 return ret; 131 132 mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV); 133 /* 134 * on 400MHz arm9 platform the conversion 135 * is already done when we do this test 136 */ 137 while ((i < 2) && mux_cnv) { 138 i++; 139 ret = ads7871_read_reg8(spi, REG_GAIN_MUX); 140 if (ret < 0) 141 return ret; 142 mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV); 143 msleep_interruptible(1); 144 } 145 146 if (mux_cnv == 0) { 147 raw_val = ads7871_read_reg16(spi, REG_LS_BYTE); 148 if (raw_val < 0) 149 return raw_val; 150 151 /* 152 * Use (s16) to ensure the sign bit is preserved during the shift. 153 * Report millivolts (2.5V = 2500mV). 154 */ 155 *val = ((s16)raw_val >> 2) * 2500 / 8192; 156 return 0; 157 } 158 159 return -ETIMEDOUT; 160 } 161 162 static const struct hwmon_channel_info * const ads7871_info[] = { 163 HWMON_CHANNEL_INFO(in, 164 HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT, 165 HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT), 166 NULL 167 }; 168 169 static const struct hwmon_ops ads7871_hwmon_ops = { 170 .is_visible = ads7871_is_visible, 171 .read = ads7871_read, 172 }; 173 174 static const struct hwmon_chip_info ads7871_chip_info = { 175 .ops = &ads7871_hwmon_ops, 176 .info = ads7871_info, 177 }; 178 179 static int ads7871_probe(struct spi_device *spi) 180 { 181 struct device *dev = &spi->dev; 182 int ret; 183 uint8_t val; 184 struct ads7871_data *pdata; 185 struct device *hwmon_dev; 186 187 /* Configure the SPI bus */ 188 spi->mode = (SPI_MODE_0); 189 spi->bits_per_word = 8; 190 spi_setup(spi); 191 192 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 193 if (!pdata) 194 return -ENOMEM; 195 196 pdata->spi = spi; 197 198 ads7871_write_reg8(pdata, REG_SER_CONTROL, 0); 199 ads7871_write_reg8(pdata, REG_AD_CONTROL, 0); 200 201 val = (OSC_OSCR_BM | OSC_OSCE_BM | OSC_REFE_BM | OSC_BUFE_BM); 202 ads7871_write_reg8(pdata, REG_OSC_CONTROL, val); 203 ret = ads7871_read_reg8(spi, REG_OSC_CONTROL); 204 205 dev_dbg(dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret); 206 /* 207 * because there is no other error checking on an SPI bus 208 * we need to make sure we really have a chip 209 */ 210 if (val != ret) 211 return -ENODEV; 212 213 hwmon_dev = devm_hwmon_device_register_with_info(dev, spi->modalias, 214 pdata, 215 &ads7871_chip_info, 216 NULL); 217 return PTR_ERR_OR_ZERO(hwmon_dev); 218 } 219 220 static struct spi_driver ads7871_driver = { 221 .driver = { 222 .name = DEVICE_NAME, 223 }, 224 .probe = ads7871_probe, 225 }; 226 227 module_spi_driver(ads7871_driver); 228 229 MODULE_AUTHOR("Paul Thomas <pthomas8589@gmail.com>"); 230 MODULE_DESCRIPTION("TI ADS7871 A/D driver"); 231 MODULE_LICENSE("GPL"); 232