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