Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ads7871 - driver for TI ADS7871 A/D converter |
| 3 | * |
| 4 | * Copyright (c) 2010 Paul Thomas <pthomas8589@gmail.com> |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 or |
| 13 | * later as publishhed by the Free Software Foundation. |
| 14 | * |
| 15 | * You need to have something like this in struct spi_board_info |
| 16 | * { |
| 17 | * .modalias = "ads7871", |
| 18 | * .max_speed_hz = 2*1000*1000, |
| 19 | * .chip_select = 0, |
| 20 | * .bus_num = 1, |
| 21 | * }, |
| 22 | */ |
| 23 | |
| 24 | /*From figure 18 in the datasheet*/ |
| 25 | /*Register addresses*/ |
| 26 | #define REG_LS_BYTE 0 /*A/D Output Data, LS Byte*/ |
| 27 | #define REG_MS_BYTE 1 /*A/D Output Data, MS Byte*/ |
| 28 | #define REG_PGA_VALID 2 /*PGA Valid Register*/ |
| 29 | #define REG_AD_CONTROL 3 /*A/D Control Register*/ |
| 30 | #define REG_GAIN_MUX 4 /*Gain/Mux Register*/ |
| 31 | #define REG_IO_STATE 5 /*Digital I/O State Register*/ |
| 32 | #define REG_IO_CONTROL 6 /*Digital I/O Control Register*/ |
| 33 | #define REG_OSC_CONTROL 7 /*Rev/Oscillator Control Register*/ |
| 34 | #define REG_SER_CONTROL 24 /*Serial Interface Control Register*/ |
| 35 | #define REG_ID 31 /*ID Register*/ |
| 36 | |
Guenter Roeck | 10775d1 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 37 | /* |
| 38 | * From figure 17 in the datasheet |
| 39 | * These bits get ORed with the address to form |
| 40 | * the instruction byte |
| 41 | */ |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 42 | /*Instruction Bit masks*/ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 43 | #define INST_MODE_BM (1 << 7) |
| 44 | #define INST_READ_BM (1 << 6) |
| 45 | #define INST_16BIT_BM (1 << 5) |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 46 | |
| 47 | /*From figure 18 in the datasheet*/ |
| 48 | /*bit masks for Rev/Oscillator Control Register*/ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 49 | #define MUX_CNV_BV 7 |
| 50 | #define MUX_CNV_BM (1 << MUX_CNV_BV) |
| 51 | #define MUX_M3_BM (1 << 3) /*M3 selects single ended*/ |
| 52 | #define MUX_G_BV 4 /*allows for reg = (gain << MUX_G_BV) | ...*/ |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 53 | |
| 54 | /*From figure 18 in the datasheet*/ |
| 55 | /*bit masks for Rev/Oscillator Control Register*/ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 56 | #define OSC_OSCR_BM (1 << 5) |
| 57 | #define OSC_OSCE_BM (1 << 4) |
| 58 | #define OSC_REFE_BM (1 << 3) |
| 59 | #define OSC_BUFE_BM (1 << 2) |
| 60 | #define OSC_R2V_BM (1 << 1) |
| 61 | #define OSC_RBG_BM (1 << 0) |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 62 | |
| 63 | #include <linux/module.h> |
| 64 | #include <linux/init.h> |
| 65 | #include <linux/spi/spi.h> |
| 66 | #include <linux/hwmon.h> |
| 67 | #include <linux/hwmon-sysfs.h> |
| 68 | #include <linux/err.h> |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 69 | #include <linux/delay.h> |
| 70 | |
| 71 | #define DEVICE_NAME "ads7871" |
| 72 | |
| 73 | struct ads7871_data { |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 74 | struct spi_device *spi; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static int ads7871_read_reg8(struct spi_device *spi, int reg) |
| 78 | { |
| 79 | int ret; |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 80 | reg = reg | INST_READ_BM; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 81 | ret = spi_w8r8(spi, reg); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | static int ads7871_read_reg16(struct spi_device *spi, int reg) |
| 86 | { |
| 87 | int ret; |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 88 | reg = reg | INST_READ_BM | INST_16BIT_BM; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 89 | ret = spi_w8r16(spi, reg); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val) |
| 94 | { |
| 95 | u8 tmp[2] = {reg, val}; |
| 96 | return spi_write(spi, tmp, sizeof(tmp)); |
| 97 | } |
| 98 | |
| 99 | static ssize_t show_voltage(struct device *dev, |
| 100 | struct device_attribute *da, char *buf) |
| 101 | { |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 102 | struct ads7871_data *pdata = dev_get_drvdata(dev); |
| 103 | struct spi_device *spi = pdata->spi; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 104 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 105 | int ret, val, i = 0; |
| 106 | uint8_t channel, mux_cnv; |
| 107 | |
| 108 | channel = attr->index; |
Guenter Roeck | 10775d1 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 109 | /* |
| 110 | * TODO: add support for conversions |
| 111 | * other than single ended with a gain of 1 |
| 112 | */ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 113 | /*MUX_M3_BM forces single ended*/ |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 114 | /*This is also where the gain of the PGA would be set*/ |
| 115 | ads7871_write_reg8(spi, REG_GAIN_MUX, |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 116 | (MUX_CNV_BM | MUX_M3_BM | channel)); |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 117 | |
| 118 | ret = ads7871_read_reg8(spi, REG_GAIN_MUX); |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 119 | mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV); |
Guenter Roeck | 10775d1 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 120 | /* |
| 121 | * on 400MHz arm9 platform the conversion |
| 122 | * is already done when we do this test |
| 123 | */ |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 124 | while ((i < 2) && mux_cnv) { |
| 125 | i++; |
| 126 | ret = ads7871_read_reg8(spi, REG_GAIN_MUX); |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 127 | mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV); |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 128 | msleep_interruptible(1); |
| 129 | } |
| 130 | |
| 131 | if (mux_cnv == 0) { |
| 132 | val = ads7871_read_reg16(spi, REG_LS_BYTE); |
| 133 | /*result in volts*10000 = (val/8192)*2.5*10000*/ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 134 | val = ((val >> 2) * 25000) / 8192; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 135 | return sprintf(buf, "%d\n", val); |
| 136 | } else { |
| 137 | return -1; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_voltage, NULL, 0); |
| 142 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 1); |
| 143 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 2); |
| 144 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 3); |
| 145 | static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 4); |
| 146 | static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 5); |
| 147 | static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 6); |
| 148 | static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 7); |
| 149 | |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 150 | static struct attribute *ads7871_attrs[] = { |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 151 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 152 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 153 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 154 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 155 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 156 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 157 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 158 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 159 | NULL |
| 160 | }; |
| 161 | |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 162 | ATTRIBUTE_GROUPS(ads7871); |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 163 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 164 | static int ads7871_probe(struct spi_device *spi) |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 165 | { |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 166 | struct device *dev = &spi->dev; |
| 167 | int ret; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 168 | uint8_t val; |
| 169 | struct ads7871_data *pdata; |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 170 | struct device *hwmon_dev; |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 171 | |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 172 | /* Configure the SPI bus */ |
| 173 | spi->mode = (SPI_MODE_0); |
| 174 | spi->bits_per_word = 8; |
| 175 | spi_setup(spi); |
| 176 | |
| 177 | ads7871_write_reg8(spi, REG_SER_CONTROL, 0); |
| 178 | ads7871_write_reg8(spi, REG_AD_CONTROL, 0); |
| 179 | |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 180 | val = (OSC_OSCR_BM | OSC_OSCE_BM | OSC_REFE_BM | OSC_BUFE_BM); |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 181 | ads7871_write_reg8(spi, REG_OSC_CONTROL, val); |
| 182 | ret = ads7871_read_reg8(spi, REG_OSC_CONTROL); |
| 183 | |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 184 | dev_dbg(dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret); |
Guenter Roeck | 10775d1 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 185 | /* |
| 186 | * because there is no other error checking on an SPI bus |
| 187 | * we need to make sure we really have a chip |
| 188 | */ |
Guenter Roeck | 33fd2b8 | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 189 | if (val != ret) |
| 190 | return -ENODEV; |
Axel Lin | c12c507 | 2010-08-25 15:42:10 +0200 | [diff] [blame] | 191 | |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 192 | pdata = devm_kzalloc(dev, sizeof(struct ads7871_data), GFP_KERNEL); |
Guenter Roeck | 33fd2b8 | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 193 | if (!pdata) |
| 194 | return -ENOMEM; |
Axel Lin | c12c507 | 2010-08-25 15:42:10 +0200 | [diff] [blame] | 195 | |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 196 | pdata->spi = spi; |
Axel Lin | c12c507 | 2010-08-25 15:42:10 +0200 | [diff] [blame] | 197 | |
Axel Lin | 68f86c7 | 2014-07-02 20:31:21 +0800 | [diff] [blame] | 198 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, spi->modalias, |
| 199 | pdata, |
| 200 | ads7871_groups); |
| 201 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static struct spi_driver ads7871_driver = { |
| 205 | .driver = { |
| 206 | .name = DEVICE_NAME, |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 207 | }, |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 208 | .probe = ads7871_probe, |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Axel Lin | 91efffe | 2012-01-20 15:53:47 +0800 | [diff] [blame] | 211 | module_spi_driver(ads7871_driver); |
Paul Thomas | e0c70b8 | 2010-05-24 14:33:38 -0700 | [diff] [blame] | 212 | |
| 213 | MODULE_AUTHOR("Paul Thomas <pthomas8589@gmail.com>"); |
| 214 | MODULE_DESCRIPTION("TI ADS7871 A/D driver"); |
| 215 | MODULE_LICENSE("GPL"); |