Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Driver for TI ADC128D818 System Monitor with Temperature Sensor |
| 4 | * |
| 5 | * Copyright (c) 2014 Guenter Roeck |
| 6 | * |
| 7 | * Derived from lm80.c |
| 8 | * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
| 9 | * and Philip Edelbrock <phil@netroedge.com> |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/jiffies.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/hwmon-sysfs.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/regulator/consumer.h> |
| 20 | #include <linux/mutex.h> |
Rasmus Villemoes | 2c3b118 | 2015-01-22 23:44:14 +0100 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame] | 22 | #include <linux/of.h> |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 23 | |
| 24 | /* Addresses to scan |
| 25 | * The chip also supports addresses 0x35..0x37. Don't scan those addresses |
| 26 | * since they are also used by some EEPROMs, which may result in false |
| 27 | * positives. |
| 28 | */ |
| 29 | static const unsigned short normal_i2c[] = { |
| 30 | 0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END }; |
| 31 | |
| 32 | /* registers */ |
| 33 | #define ADC128_REG_IN_MAX(nr) (0x2a + (nr) * 2) |
| 34 | #define ADC128_REG_IN_MIN(nr) (0x2b + (nr) * 2) |
| 35 | #define ADC128_REG_IN(nr) (0x20 + (nr)) |
| 36 | |
| 37 | #define ADC128_REG_TEMP 0x27 |
| 38 | #define ADC128_REG_TEMP_MAX 0x38 |
| 39 | #define ADC128_REG_TEMP_HYST 0x39 |
| 40 | |
| 41 | #define ADC128_REG_CONFIG 0x00 |
| 42 | #define ADC128_REG_ALARM 0x01 |
| 43 | #define ADC128_REG_MASK 0x03 |
| 44 | #define ADC128_REG_CONV_RATE 0x07 |
| 45 | #define ADC128_REG_ONESHOT 0x09 |
| 46 | #define ADC128_REG_SHUTDOWN 0x0a |
| 47 | #define ADC128_REG_CONFIG_ADV 0x0b |
| 48 | #define ADC128_REG_BUSY_STATUS 0x0c |
| 49 | |
| 50 | #define ADC128_REG_MAN_ID 0x3e |
| 51 | #define ADC128_REG_DEV_ID 0x3f |
| 52 | |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 53 | /* No. of voltage entries in adc128_attrs */ |
| 54 | #define ADC128_ATTR_NUM_VOLT (8 * 4) |
| 55 | |
| 56 | /* Voltage inputs visible per operation mode */ |
| 57 | static const u8 num_inputs[] = { 7, 8, 4, 6 }; |
| 58 | |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 59 | struct adc128_data { |
| 60 | struct i2c_client *client; |
| 61 | struct regulator *regulator; |
| 62 | int vref; /* Reference voltage in mV */ |
| 63 | struct mutex update_lock; |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame] | 64 | u8 mode; /* Operation mode */ |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 65 | bool valid; /* true if following fields are valid */ |
| 66 | unsigned long last_updated; /* In jiffies */ |
| 67 | |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 68 | u16 in[3][8]; /* Register value, normalized to 12 bit |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 69 | * 0: input voltage |
| 70 | * 1: min limit |
| 71 | * 2: max limit |
| 72 | */ |
| 73 | s16 temp[3]; /* Register value, normalized to 9 bit |
| 74 | * 0: sensor 1: limit 2: hyst |
| 75 | */ |
| 76 | u8 alarms; /* alarm register value */ |
| 77 | }; |
| 78 | |
| 79 | static struct adc128_data *adc128_update_device(struct device *dev) |
| 80 | { |
| 81 | struct adc128_data *data = dev_get_drvdata(dev); |
| 82 | struct i2c_client *client = data->client; |
| 83 | struct adc128_data *ret = data; |
| 84 | int i, rv; |
| 85 | |
| 86 | mutex_lock(&data->update_lock); |
| 87 | |
| 88 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 89 | for (i = 0; i < num_inputs[data->mode]; i++) { |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 90 | rv = i2c_smbus_read_word_swapped(client, |
| 91 | ADC128_REG_IN(i)); |
| 92 | if (rv < 0) |
| 93 | goto abort; |
| 94 | data->in[0][i] = rv >> 4; |
| 95 | |
| 96 | rv = i2c_smbus_read_byte_data(client, |
| 97 | ADC128_REG_IN_MIN(i)); |
| 98 | if (rv < 0) |
| 99 | goto abort; |
| 100 | data->in[1][i] = rv << 4; |
| 101 | |
| 102 | rv = i2c_smbus_read_byte_data(client, |
| 103 | ADC128_REG_IN_MAX(i)); |
| 104 | if (rv < 0) |
| 105 | goto abort; |
| 106 | data->in[2][i] = rv << 4; |
| 107 | } |
| 108 | |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 109 | if (data->mode != 1) { |
| 110 | rv = i2c_smbus_read_word_swapped(client, |
| 111 | ADC128_REG_TEMP); |
| 112 | if (rv < 0) |
| 113 | goto abort; |
| 114 | data->temp[0] = rv >> 7; |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 115 | |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 116 | rv = i2c_smbus_read_byte_data(client, |
| 117 | ADC128_REG_TEMP_MAX); |
| 118 | if (rv < 0) |
| 119 | goto abort; |
| 120 | data->temp[1] = rv << 1; |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 121 | |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 122 | rv = i2c_smbus_read_byte_data(client, |
| 123 | ADC128_REG_TEMP_HYST); |
| 124 | if (rv < 0) |
| 125 | goto abort; |
| 126 | data->temp[2] = rv << 1; |
| 127 | } |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 128 | |
| 129 | rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM); |
| 130 | if (rv < 0) |
| 131 | goto abort; |
| 132 | data->alarms |= rv; |
| 133 | |
| 134 | data->last_updated = jiffies; |
| 135 | data->valid = true; |
| 136 | } |
| 137 | goto done; |
| 138 | |
| 139 | abort: |
| 140 | ret = ERR_PTR(rv); |
| 141 | data->valid = false; |
| 142 | done: |
| 143 | mutex_unlock(&data->update_lock); |
| 144 | return ret; |
| 145 | } |
| 146 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 147 | static ssize_t adc128_in_show(struct device *dev, |
| 148 | struct device_attribute *attr, char *buf) |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 149 | { |
| 150 | struct adc128_data *data = adc128_update_device(dev); |
| 151 | int index = to_sensor_dev_attr_2(attr)->index; |
| 152 | int nr = to_sensor_dev_attr_2(attr)->nr; |
| 153 | int val; |
| 154 | |
| 155 | if (IS_ERR(data)) |
| 156 | return PTR_ERR(data); |
| 157 | |
| 158 | val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095); |
| 159 | return sprintf(buf, "%d\n", val); |
| 160 | } |
| 161 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 162 | static ssize_t adc128_in_store(struct device *dev, |
| 163 | struct device_attribute *attr, const char *buf, |
| 164 | size_t count) |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 165 | { |
| 166 | struct adc128_data *data = dev_get_drvdata(dev); |
| 167 | int index = to_sensor_dev_attr_2(attr)->index; |
| 168 | int nr = to_sensor_dev_attr_2(attr)->nr; |
| 169 | u8 reg, regval; |
| 170 | long val; |
| 171 | int err; |
| 172 | |
| 173 | err = kstrtol(buf, 10, &val); |
| 174 | if (err < 0) |
| 175 | return err; |
| 176 | |
| 177 | mutex_lock(&data->update_lock); |
| 178 | /* 10 mV LSB on limit registers */ |
| 179 | regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255); |
| 180 | data->in[index][nr] = regval << 4; |
| 181 | reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr); |
| 182 | i2c_smbus_write_byte_data(data->client, reg, regval); |
| 183 | mutex_unlock(&data->update_lock); |
| 184 | |
| 185 | return count; |
| 186 | } |
| 187 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 188 | static ssize_t adc128_temp_show(struct device *dev, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 189 | struct device_attribute *attr, char *buf) |
| 190 | { |
| 191 | struct adc128_data *data = adc128_update_device(dev); |
| 192 | int index = to_sensor_dev_attr(attr)->index; |
| 193 | int temp; |
| 194 | |
| 195 | if (IS_ERR(data)) |
| 196 | return PTR_ERR(data); |
| 197 | |
Rasmus Villemoes | 2c3b118 | 2015-01-22 23:44:14 +0100 | [diff] [blame] | 198 | temp = sign_extend32(data->temp[index], 8); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 199 | return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */ |
| 200 | } |
| 201 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 202 | static ssize_t adc128_temp_store(struct device *dev, |
| 203 | struct device_attribute *attr, |
| 204 | const char *buf, size_t count) |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 205 | { |
| 206 | struct adc128_data *data = dev_get_drvdata(dev); |
| 207 | int index = to_sensor_dev_attr(attr)->index; |
| 208 | long val; |
| 209 | int err; |
| 210 | s8 regval; |
| 211 | |
| 212 | err = kstrtol(buf, 10, &val); |
| 213 | if (err < 0) |
| 214 | return err; |
| 215 | |
| 216 | mutex_lock(&data->update_lock); |
| 217 | regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); |
| 218 | data->temp[index] = regval << 1; |
| 219 | i2c_smbus_write_byte_data(data->client, |
| 220 | index == 1 ? ADC128_REG_TEMP_MAX |
| 221 | : ADC128_REG_TEMP_HYST, |
| 222 | regval); |
| 223 | mutex_unlock(&data->update_lock); |
| 224 | |
| 225 | return count; |
| 226 | } |
| 227 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 228 | static ssize_t adc128_alarm_show(struct device *dev, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 229 | struct device_attribute *attr, char *buf) |
| 230 | { |
| 231 | struct adc128_data *data = adc128_update_device(dev); |
| 232 | int mask = 1 << to_sensor_dev_attr(attr)->index; |
| 233 | u8 alarms; |
| 234 | |
| 235 | if (IS_ERR(data)) |
| 236 | return PTR_ERR(data); |
| 237 | |
| 238 | /* |
| 239 | * Clear an alarm after reporting it to user space. If it is still |
| 240 | * active, the next update sequence will set the alarm bit again. |
| 241 | */ |
| 242 | alarms = data->alarms; |
| 243 | data->alarms &= ~mask; |
| 244 | |
| 245 | return sprintf(buf, "%u\n", !!(alarms & mask)); |
| 246 | } |
| 247 | |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 248 | static umode_t adc128_is_visible(struct kobject *kobj, |
| 249 | struct attribute *attr, int index) |
| 250 | { |
| 251 | struct device *dev = container_of(kobj, struct device, kobj); |
| 252 | struct adc128_data *data = dev_get_drvdata(dev); |
| 253 | |
| 254 | if (index < ADC128_ATTR_NUM_VOLT) { |
| 255 | /* Voltage, visible according to num_inputs[] */ |
| 256 | if (index >= num_inputs[data->mode] * 4) |
| 257 | return 0; |
| 258 | } else { |
| 259 | /* Temperature, visible if not in mode 1 */ |
| 260 | if (data->mode == 1) |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | return attr->mode; |
| 265 | } |
| 266 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 267 | static SENSOR_DEVICE_ATTR_2_RO(in0_input, adc128_in, 0, 0); |
| 268 | static SENSOR_DEVICE_ATTR_2_RW(in0_min, adc128_in, 0, 1); |
| 269 | static SENSOR_DEVICE_ATTR_2_RW(in0_max, adc128_in, 0, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 270 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 271 | static SENSOR_DEVICE_ATTR_2_RO(in1_input, adc128_in, 1, 0); |
| 272 | static SENSOR_DEVICE_ATTR_2_RW(in1_min, adc128_in, 1, 1); |
| 273 | static SENSOR_DEVICE_ATTR_2_RW(in1_max, adc128_in, 1, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 274 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 275 | static SENSOR_DEVICE_ATTR_2_RO(in2_input, adc128_in, 2, 0); |
| 276 | static SENSOR_DEVICE_ATTR_2_RW(in2_min, adc128_in, 2, 1); |
| 277 | static SENSOR_DEVICE_ATTR_2_RW(in2_max, adc128_in, 2, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 278 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 279 | static SENSOR_DEVICE_ATTR_2_RO(in3_input, adc128_in, 3, 0); |
| 280 | static SENSOR_DEVICE_ATTR_2_RW(in3_min, adc128_in, 3, 1); |
| 281 | static SENSOR_DEVICE_ATTR_2_RW(in3_max, adc128_in, 3, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 282 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 283 | static SENSOR_DEVICE_ATTR_2_RO(in4_input, adc128_in, 4, 0); |
| 284 | static SENSOR_DEVICE_ATTR_2_RW(in4_min, adc128_in, 4, 1); |
| 285 | static SENSOR_DEVICE_ATTR_2_RW(in4_max, adc128_in, 4, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 286 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 287 | static SENSOR_DEVICE_ATTR_2_RO(in5_input, adc128_in, 5, 0); |
| 288 | static SENSOR_DEVICE_ATTR_2_RW(in5_min, adc128_in, 5, 1); |
| 289 | static SENSOR_DEVICE_ATTR_2_RW(in5_max, adc128_in, 5, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 290 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 291 | static SENSOR_DEVICE_ATTR_2_RO(in6_input, adc128_in, 6, 0); |
| 292 | static SENSOR_DEVICE_ATTR_2_RW(in6_min, adc128_in, 6, 1); |
| 293 | static SENSOR_DEVICE_ATTR_2_RW(in6_max, adc128_in, 6, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 294 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 295 | static SENSOR_DEVICE_ATTR_2_RO(in7_input, adc128_in, 7, 0); |
| 296 | static SENSOR_DEVICE_ATTR_2_RW(in7_min, adc128_in, 7, 1); |
| 297 | static SENSOR_DEVICE_ATTR_2_RW(in7_max, adc128_in, 7, 2); |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 298 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 299 | static SENSOR_DEVICE_ATTR_RO(temp1_input, adc128_temp, 0); |
| 300 | static SENSOR_DEVICE_ATTR_RW(temp1_max, adc128_temp, 1); |
| 301 | static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adc128_temp, 2); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 302 | |
Guenter Roeck | 0594462 | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 303 | static SENSOR_DEVICE_ATTR_RO(in0_alarm, adc128_alarm, 0); |
| 304 | static SENSOR_DEVICE_ATTR_RO(in1_alarm, adc128_alarm, 1); |
| 305 | static SENSOR_DEVICE_ATTR_RO(in2_alarm, adc128_alarm, 2); |
| 306 | static SENSOR_DEVICE_ATTR_RO(in3_alarm, adc128_alarm, 3); |
| 307 | static SENSOR_DEVICE_ATTR_RO(in4_alarm, adc128_alarm, 4); |
| 308 | static SENSOR_DEVICE_ATTR_RO(in5_alarm, adc128_alarm, 5); |
| 309 | static SENSOR_DEVICE_ATTR_RO(in6_alarm, adc128_alarm, 6); |
| 310 | static SENSOR_DEVICE_ATTR_RO(in7_alarm, adc128_alarm, 7); |
| 311 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adc128_alarm, 7); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 312 | |
| 313 | static struct attribute *adc128_attrs[] = { |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 314 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 315 | &sensor_dev_attr_in0_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 316 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 317 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 318 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 319 | &sensor_dev_attr_in1_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 320 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 321 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 322 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 323 | &sensor_dev_attr_in2_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 324 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 325 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 326 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 327 | &sensor_dev_attr_in3_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 328 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 329 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 330 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 331 | &sensor_dev_attr_in4_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 332 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 333 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 334 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 335 | &sensor_dev_attr_in5_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 336 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 337 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 338 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 339 | &sensor_dev_attr_in6_input.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 340 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 341 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 342 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
| 343 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 344 | &sensor_dev_attr_in7_max.dev_attr.attr, |
| 345 | &sensor_dev_attr_in7_min.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 346 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 347 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 348 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 349 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 350 | NULL |
| 351 | }; |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 352 | |
Arvind Yadav | 33d62d1 | 2017-07-05 10:52:13 +0530 | [diff] [blame] | 353 | static const struct attribute_group adc128_group = { |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 354 | .attrs = adc128_attrs, |
| 355 | .is_visible = adc128_is_visible, |
| 356 | }; |
| 357 | __ATTRIBUTE_GROUPS(adc128); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 358 | |
| 359 | static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info) |
| 360 | { |
| 361 | int man_id, dev_id; |
| 362 | |
| 363 | if (!i2c_check_functionality(client->adapter, |
| 364 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 365 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 366 | return -ENODEV; |
| 367 | |
| 368 | man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID); |
| 369 | dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID); |
| 370 | if (man_id != 0x01 || dev_id != 0x09) |
| 371 | return -ENODEV; |
| 372 | |
| 373 | /* Check unused bits for confirmation */ |
| 374 | if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4) |
| 375 | return -ENODEV; |
| 376 | if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe) |
| 377 | return -ENODEV; |
| 378 | if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe) |
| 379 | return -ENODEV; |
| 380 | if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe) |
| 381 | return -ENODEV; |
| 382 | if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8) |
| 383 | return -ENODEV; |
| 384 | if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc) |
| 385 | return -ENODEV; |
| 386 | |
| 387 | strlcpy(info->type, "adc128d818", I2C_NAME_SIZE); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static int adc128_init_client(struct adc128_data *data) |
| 393 | { |
| 394 | struct i2c_client *client = data->client; |
| 395 | int err; |
| 396 | |
| 397 | /* |
| 398 | * Reset chip to defaults. |
| 399 | * This makes most other initializations unnecessary. |
| 400 | */ |
| 401 | err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80); |
| 402 | if (err) |
| 403 | return err; |
| 404 | |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame] | 405 | /* Set operation mode, if non-default */ |
| 406 | if (data->mode != 0) { |
| 407 | err = i2c_smbus_write_byte_data(client, |
| 408 | ADC128_REG_CONFIG_ADV, |
| 409 | data->mode << 1); |
| 410 | if (err) |
| 411 | return err; |
| 412 | } |
| 413 | |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 414 | /* Start monitoring */ |
| 415 | err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01); |
| 416 | if (err) |
| 417 | return err; |
| 418 | |
| 419 | /* If external vref is selected, configure the chip to use it */ |
| 420 | if (data->regulator) { |
| 421 | err = i2c_smbus_write_byte_data(client, |
| 422 | ADC128_REG_CONFIG_ADV, 0x01); |
| 423 | if (err) |
| 424 | return err; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int adc128_probe(struct i2c_client *client, |
| 431 | const struct i2c_device_id *id) |
| 432 | { |
| 433 | struct device *dev = &client->dev; |
| 434 | struct regulator *regulator; |
| 435 | struct device *hwmon_dev; |
| 436 | struct adc128_data *data; |
| 437 | int err, vref; |
| 438 | |
| 439 | data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL); |
| 440 | if (!data) |
| 441 | return -ENOMEM; |
| 442 | |
| 443 | /* vref is optional. If specified, is used as chip reference voltage */ |
| 444 | regulator = devm_regulator_get_optional(dev, "vref"); |
| 445 | if (!IS_ERR(regulator)) { |
| 446 | data->regulator = regulator; |
| 447 | err = regulator_enable(regulator); |
| 448 | if (err < 0) |
| 449 | return err; |
| 450 | vref = regulator_get_voltage(regulator); |
| 451 | if (vref < 0) { |
| 452 | err = vref; |
| 453 | goto error; |
| 454 | } |
| 455 | data->vref = DIV_ROUND_CLOSEST(vref, 1000); |
| 456 | } else { |
| 457 | data->vref = 2560; /* 2.56V, in mV */ |
| 458 | } |
| 459 | |
Alexander Koch | c72eeab | 2017-01-06 11:38:17 +0100 | [diff] [blame] | 460 | /* Operation mode is optional. If unspecified, keep current mode */ |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame] | 461 | if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) { |
Alexander Koch | 4e1796c | 2017-01-06 11:38:16 +0100 | [diff] [blame] | 462 | if (data->mode > 3) { |
| 463 | dev_err(dev, "invalid operation mode %d\n", |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame] | 464 | data->mode); |
| 465 | err = -EINVAL; |
| 466 | goto error; |
| 467 | } |
| 468 | } else { |
Alexander Koch | c72eeab | 2017-01-06 11:38:17 +0100 | [diff] [blame] | 469 | err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV); |
| 470 | if (err < 0) |
| 471 | goto error; |
| 472 | data->mode = (err >> 1) & ADC128_REG_MASK; |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 475 | data->client = client; |
| 476 | i2c_set_clientdata(client, data); |
| 477 | mutex_init(&data->update_lock); |
| 478 | |
| 479 | /* Initialize the chip */ |
| 480 | err = adc128_init_client(data); |
| 481 | if (err < 0) |
| 482 | goto error; |
| 483 | |
| 484 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 485 | data, adc128_groups); |
| 486 | if (IS_ERR(hwmon_dev)) { |
| 487 | err = PTR_ERR(hwmon_dev); |
| 488 | goto error; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | |
| 493 | error: |
| 494 | if (data->regulator) |
| 495 | regulator_disable(data->regulator); |
| 496 | return err; |
| 497 | } |
| 498 | |
| 499 | static int adc128_remove(struct i2c_client *client) |
| 500 | { |
| 501 | struct adc128_data *data = i2c_get_clientdata(client); |
| 502 | |
| 503 | if (data->regulator) |
| 504 | regulator_disable(data->regulator); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static const struct i2c_device_id adc128_id[] = { |
| 510 | { "adc128d818", 0 }, |
| 511 | { } |
| 512 | }; |
| 513 | MODULE_DEVICE_TABLE(i2c, adc128_id); |
| 514 | |
Guenter Roeck | 462d7e7 | 2019-04-04 06:23:59 -0700 | [diff] [blame] | 515 | static const struct of_device_id __maybe_unused adc128_of_match[] = { |
Javier Martinez Canillas | d593e66 | 2017-02-24 10:12:55 -0300 | [diff] [blame] | 516 | { .compatible = "ti,adc128d818" }, |
| 517 | { }, |
| 518 | }; |
| 519 | MODULE_DEVICE_TABLE(of, adc128_of_match); |
| 520 | |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 521 | static struct i2c_driver adc128_driver = { |
| 522 | .class = I2C_CLASS_HWMON, |
| 523 | .driver = { |
| 524 | .name = "adc128d818", |
Javier Martinez Canillas | d593e66 | 2017-02-24 10:12:55 -0300 | [diff] [blame] | 525 | .of_match_table = of_match_ptr(adc128_of_match), |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 526 | }, |
| 527 | .probe = adc128_probe, |
| 528 | .remove = adc128_remove, |
| 529 | .id_table = adc128_id, |
| 530 | .detect = adc128_detect, |
| 531 | .address_list = normal_i2c, |
| 532 | }; |
| 533 | |
| 534 | module_i2c_driver(adc128_driver); |
| 535 | |
| 536 | MODULE_AUTHOR("Guenter Roeck"); |
| 537 | MODULE_DESCRIPTION("Driver for ADC128D818"); |
| 538 | MODULE_LICENSE("GPL"); |