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 | |
| 62 | struct adc128_data { |
| 63 | struct i2c_client *client; |
| 64 | struct regulator *regulator; |
| 65 | int vref; /* Reference voltage in mV */ |
| 66 | struct mutex update_lock; |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame^] | 67 | u8 mode; /* Operation mode */ |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 68 | bool valid; /* true if following fields are valid */ |
| 69 | unsigned long last_updated; /* In jiffies */ |
| 70 | |
| 71 | u16 in[3][7]; /* Register value, normalized to 12 bit |
| 72 | * 0: input voltage |
| 73 | * 1: min limit |
| 74 | * 2: max limit |
| 75 | */ |
| 76 | s16 temp[3]; /* Register value, normalized to 9 bit |
| 77 | * 0: sensor 1: limit 2: hyst |
| 78 | */ |
| 79 | u8 alarms; /* alarm register value */ |
| 80 | }; |
| 81 | |
| 82 | static struct adc128_data *adc128_update_device(struct device *dev) |
| 83 | { |
| 84 | struct adc128_data *data = dev_get_drvdata(dev); |
| 85 | struct i2c_client *client = data->client; |
| 86 | struct adc128_data *ret = data; |
| 87 | int i, rv; |
| 88 | |
| 89 | mutex_lock(&data->update_lock); |
| 90 | |
| 91 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 92 | for (i = 0; i < 7; i++) { |
| 93 | rv = i2c_smbus_read_word_swapped(client, |
| 94 | ADC128_REG_IN(i)); |
| 95 | if (rv < 0) |
| 96 | goto abort; |
| 97 | data->in[0][i] = rv >> 4; |
| 98 | |
| 99 | rv = i2c_smbus_read_byte_data(client, |
| 100 | ADC128_REG_IN_MIN(i)); |
| 101 | if (rv < 0) |
| 102 | goto abort; |
| 103 | data->in[1][i] = rv << 4; |
| 104 | |
| 105 | rv = i2c_smbus_read_byte_data(client, |
| 106 | ADC128_REG_IN_MAX(i)); |
| 107 | if (rv < 0) |
| 108 | goto abort; |
| 109 | data->in[2][i] = rv << 4; |
| 110 | } |
| 111 | |
| 112 | rv = i2c_smbus_read_word_swapped(client, ADC128_REG_TEMP); |
| 113 | if (rv < 0) |
| 114 | goto abort; |
| 115 | data->temp[0] = rv >> 7; |
| 116 | |
| 117 | rv = i2c_smbus_read_byte_data(client, ADC128_REG_TEMP_MAX); |
| 118 | if (rv < 0) |
| 119 | goto abort; |
| 120 | data->temp[1] = rv << 1; |
| 121 | |
| 122 | rv = i2c_smbus_read_byte_data(client, ADC128_REG_TEMP_HYST); |
| 123 | if (rv < 0) |
| 124 | goto abort; |
| 125 | data->temp[2] = rv << 1; |
| 126 | |
| 127 | rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM); |
| 128 | if (rv < 0) |
| 129 | goto abort; |
| 130 | data->alarms |= rv; |
| 131 | |
| 132 | data->last_updated = jiffies; |
| 133 | data->valid = true; |
| 134 | } |
| 135 | goto done; |
| 136 | |
| 137 | abort: |
| 138 | ret = ERR_PTR(rv); |
| 139 | data->valid = false; |
| 140 | done: |
| 141 | mutex_unlock(&data->update_lock); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static ssize_t adc128_show_in(struct device *dev, struct device_attribute *attr, |
| 146 | char *buf) |
| 147 | { |
| 148 | struct adc128_data *data = adc128_update_device(dev); |
| 149 | int index = to_sensor_dev_attr_2(attr)->index; |
| 150 | int nr = to_sensor_dev_attr_2(attr)->nr; |
| 151 | int val; |
| 152 | |
| 153 | if (IS_ERR(data)) |
| 154 | return PTR_ERR(data); |
| 155 | |
| 156 | val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095); |
| 157 | return sprintf(buf, "%d\n", val); |
| 158 | } |
| 159 | |
| 160 | static ssize_t adc128_set_in(struct device *dev, struct device_attribute *attr, |
| 161 | const char *buf, size_t count) |
| 162 | { |
| 163 | struct adc128_data *data = dev_get_drvdata(dev); |
| 164 | int index = to_sensor_dev_attr_2(attr)->index; |
| 165 | int nr = to_sensor_dev_attr_2(attr)->nr; |
| 166 | u8 reg, regval; |
| 167 | long val; |
| 168 | int err; |
| 169 | |
| 170 | err = kstrtol(buf, 10, &val); |
| 171 | if (err < 0) |
| 172 | return err; |
| 173 | |
| 174 | mutex_lock(&data->update_lock); |
| 175 | /* 10 mV LSB on limit registers */ |
| 176 | regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255); |
| 177 | data->in[index][nr] = regval << 4; |
| 178 | reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr); |
| 179 | i2c_smbus_write_byte_data(data->client, reg, regval); |
| 180 | mutex_unlock(&data->update_lock); |
| 181 | |
| 182 | return count; |
| 183 | } |
| 184 | |
| 185 | static ssize_t adc128_show_temp(struct device *dev, |
| 186 | struct device_attribute *attr, char *buf) |
| 187 | { |
| 188 | struct adc128_data *data = adc128_update_device(dev); |
| 189 | int index = to_sensor_dev_attr(attr)->index; |
| 190 | int temp; |
| 191 | |
| 192 | if (IS_ERR(data)) |
| 193 | return PTR_ERR(data); |
| 194 | |
Rasmus Villemoes | 2c3b118 | 2015-01-22 23:44:14 +0100 | [diff] [blame] | 195 | temp = sign_extend32(data->temp[index], 8); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 196 | return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */ |
| 197 | } |
| 198 | |
| 199 | static ssize_t adc128_set_temp(struct device *dev, |
| 200 | struct device_attribute *attr, |
| 201 | const char *buf, size_t count) |
| 202 | { |
| 203 | struct adc128_data *data = dev_get_drvdata(dev); |
| 204 | int index = to_sensor_dev_attr(attr)->index; |
| 205 | long val; |
| 206 | int err; |
| 207 | s8 regval; |
| 208 | |
| 209 | err = kstrtol(buf, 10, &val); |
| 210 | if (err < 0) |
| 211 | return err; |
| 212 | |
| 213 | mutex_lock(&data->update_lock); |
| 214 | regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); |
| 215 | data->temp[index] = regval << 1; |
| 216 | i2c_smbus_write_byte_data(data->client, |
| 217 | index == 1 ? ADC128_REG_TEMP_MAX |
| 218 | : ADC128_REG_TEMP_HYST, |
| 219 | regval); |
| 220 | mutex_unlock(&data->update_lock); |
| 221 | |
| 222 | return count; |
| 223 | } |
| 224 | |
| 225 | static ssize_t adc128_show_alarm(struct device *dev, |
| 226 | struct device_attribute *attr, char *buf) |
| 227 | { |
| 228 | struct adc128_data *data = adc128_update_device(dev); |
| 229 | int mask = 1 << to_sensor_dev_attr(attr)->index; |
| 230 | u8 alarms; |
| 231 | |
| 232 | if (IS_ERR(data)) |
| 233 | return PTR_ERR(data); |
| 234 | |
| 235 | /* |
| 236 | * Clear an alarm after reporting it to user space. If it is still |
| 237 | * active, the next update sequence will set the alarm bit again. |
| 238 | */ |
| 239 | alarms = data->alarms; |
| 240 | data->alarms &= ~mask; |
| 241 | |
| 242 | return sprintf(buf, "%u\n", !!(alarms & mask)); |
| 243 | } |
| 244 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 245 | static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, |
| 246 | adc128_show_in, NULL, 0, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 247 | static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, |
| 248 | adc128_show_in, adc128_set_in, 0, 1); |
| 249 | static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, |
| 250 | adc128_show_in, adc128_set_in, 0, 2); |
| 251 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 252 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, |
| 253 | adc128_show_in, NULL, 1, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 254 | static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, |
| 255 | adc128_show_in, adc128_set_in, 1, 1); |
| 256 | static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, |
| 257 | adc128_show_in, adc128_set_in, 1, 2); |
| 258 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 259 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, |
| 260 | adc128_show_in, NULL, 2, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 261 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, |
| 262 | adc128_show_in, adc128_set_in, 2, 1); |
| 263 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, |
| 264 | adc128_show_in, adc128_set_in, 2, 2); |
| 265 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 266 | static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, |
| 267 | adc128_show_in, NULL, 3, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 268 | static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, |
| 269 | adc128_show_in, adc128_set_in, 3, 1); |
| 270 | static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, |
| 271 | adc128_show_in, adc128_set_in, 3, 2); |
| 272 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 273 | static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, |
| 274 | adc128_show_in, NULL, 4, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 275 | static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, |
| 276 | adc128_show_in, adc128_set_in, 4, 1); |
| 277 | static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, |
| 278 | adc128_show_in, adc128_set_in, 4, 2); |
| 279 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 280 | static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, |
| 281 | adc128_show_in, NULL, 5, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 282 | static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, |
| 283 | adc128_show_in, adc128_set_in, 5, 1); |
| 284 | static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, |
| 285 | adc128_show_in, adc128_set_in, 5, 2); |
| 286 | |
Guenter Roeck | 7fe7381 | 2014-07-07 07:10:10 -0700 | [diff] [blame] | 287 | static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, |
| 288 | adc128_show_in, NULL, 6, 0); |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 289 | static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, |
| 290 | adc128_show_in, adc128_set_in, 6, 1); |
| 291 | static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, |
| 292 | adc128_show_in, adc128_set_in, 6, 2); |
| 293 | |
| 294 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adc128_show_temp, NULL, 0); |
| 295 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 296 | adc128_show_temp, adc128_set_temp, 1); |
| 297 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 298 | adc128_show_temp, adc128_set_temp, 2); |
| 299 | |
| 300 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, adc128_show_alarm, NULL, 0); |
| 301 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, adc128_show_alarm, NULL, 1); |
| 302 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, adc128_show_alarm, NULL, 2); |
| 303 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, adc128_show_alarm, NULL, 3); |
| 304 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, adc128_show_alarm, NULL, 4); |
| 305 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, adc128_show_alarm, NULL, 5); |
| 306 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, adc128_show_alarm, NULL, 6); |
| 307 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adc128_show_alarm, NULL, 7); |
| 308 | |
| 309 | static struct attribute *adc128_attrs[] = { |
| 310 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 311 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 312 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 313 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 314 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 315 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 316 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 317 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 318 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 319 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 320 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 321 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 322 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 323 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 324 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 325 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 326 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 327 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 328 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 329 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 330 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 331 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 332 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 333 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 334 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 335 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 336 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 337 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 338 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 339 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 340 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
| 341 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 342 | NULL |
| 343 | }; |
| 344 | ATTRIBUTE_GROUPS(adc128); |
| 345 | |
| 346 | static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info) |
| 347 | { |
| 348 | int man_id, dev_id; |
| 349 | |
| 350 | if (!i2c_check_functionality(client->adapter, |
| 351 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 352 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 353 | return -ENODEV; |
| 354 | |
| 355 | man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID); |
| 356 | dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID); |
| 357 | if (man_id != 0x01 || dev_id != 0x09) |
| 358 | return -ENODEV; |
| 359 | |
| 360 | /* Check unused bits for confirmation */ |
| 361 | if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4) |
| 362 | return -ENODEV; |
| 363 | if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe) |
| 364 | return -ENODEV; |
| 365 | if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe) |
| 366 | return -ENODEV; |
| 367 | if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe) |
| 368 | return -ENODEV; |
| 369 | if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8) |
| 370 | return -ENODEV; |
| 371 | if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc) |
| 372 | return -ENODEV; |
| 373 | |
| 374 | strlcpy(info->type, "adc128d818", I2C_NAME_SIZE); |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int adc128_init_client(struct adc128_data *data) |
| 380 | { |
| 381 | struct i2c_client *client = data->client; |
| 382 | int err; |
| 383 | |
| 384 | /* |
| 385 | * Reset chip to defaults. |
| 386 | * This makes most other initializations unnecessary. |
| 387 | */ |
| 388 | err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80); |
| 389 | if (err) |
| 390 | return err; |
| 391 | |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame^] | 392 | /* Set operation mode, if non-default */ |
| 393 | if (data->mode != 0) { |
| 394 | err = i2c_smbus_write_byte_data(client, |
| 395 | ADC128_REG_CONFIG_ADV, |
| 396 | data->mode << 1); |
| 397 | if (err) |
| 398 | return err; |
| 399 | } |
| 400 | |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 401 | /* Start monitoring */ |
| 402 | err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01); |
| 403 | if (err) |
| 404 | return err; |
| 405 | |
| 406 | /* If external vref is selected, configure the chip to use it */ |
| 407 | if (data->regulator) { |
| 408 | err = i2c_smbus_write_byte_data(client, |
| 409 | ADC128_REG_CONFIG_ADV, 0x01); |
| 410 | if (err) |
| 411 | return err; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int adc128_probe(struct i2c_client *client, |
| 418 | const struct i2c_device_id *id) |
| 419 | { |
| 420 | struct device *dev = &client->dev; |
| 421 | struct regulator *regulator; |
| 422 | struct device *hwmon_dev; |
| 423 | struct adc128_data *data; |
| 424 | int err, vref; |
| 425 | |
| 426 | data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL); |
| 427 | if (!data) |
| 428 | return -ENOMEM; |
| 429 | |
| 430 | /* vref is optional. If specified, is used as chip reference voltage */ |
| 431 | regulator = devm_regulator_get_optional(dev, "vref"); |
| 432 | if (!IS_ERR(regulator)) { |
| 433 | data->regulator = regulator; |
| 434 | err = regulator_enable(regulator); |
| 435 | if (err < 0) |
| 436 | return err; |
| 437 | vref = regulator_get_voltage(regulator); |
| 438 | if (vref < 0) { |
| 439 | err = vref; |
| 440 | goto error; |
| 441 | } |
| 442 | data->vref = DIV_ROUND_CLOSEST(vref, 1000); |
| 443 | } else { |
| 444 | data->vref = 2560; /* 2.56V, in mV */ |
| 445 | } |
| 446 | |
Alexander Koch | a45923c | 2017-01-06 11:38:15 +0100 | [diff] [blame^] | 447 | /* Operation mode is optional and defaults to mode 0 */ |
| 448 | if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) { |
| 449 | /* Currently only mode 0 supported */ |
| 450 | if (data->mode != 0) { |
| 451 | dev_err(dev, "unsupported operation mode %d\n", |
| 452 | data->mode); |
| 453 | err = -EINVAL; |
| 454 | goto error; |
| 455 | } |
| 456 | } else { |
| 457 | data->mode = 0; |
| 458 | } |
| 459 | |
Guenter Roeck | b4c9c1a | 2014-01-24 22:25:33 -0800 | [diff] [blame] | 460 | data->client = client; |
| 461 | i2c_set_clientdata(client, data); |
| 462 | mutex_init(&data->update_lock); |
| 463 | |
| 464 | /* Initialize the chip */ |
| 465 | err = adc128_init_client(data); |
| 466 | if (err < 0) |
| 467 | goto error; |
| 468 | |
| 469 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 470 | data, adc128_groups); |
| 471 | if (IS_ERR(hwmon_dev)) { |
| 472 | err = PTR_ERR(hwmon_dev); |
| 473 | goto error; |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | |
| 478 | error: |
| 479 | if (data->regulator) |
| 480 | regulator_disable(data->regulator); |
| 481 | return err; |
| 482 | } |
| 483 | |
| 484 | static int adc128_remove(struct i2c_client *client) |
| 485 | { |
| 486 | struct adc128_data *data = i2c_get_clientdata(client); |
| 487 | |
| 488 | if (data->regulator) |
| 489 | regulator_disable(data->regulator); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static const struct i2c_device_id adc128_id[] = { |
| 495 | { "adc128d818", 0 }, |
| 496 | { } |
| 497 | }; |
| 498 | MODULE_DEVICE_TABLE(i2c, adc128_id); |
| 499 | |
| 500 | static struct i2c_driver adc128_driver = { |
| 501 | .class = I2C_CLASS_HWMON, |
| 502 | .driver = { |
| 503 | .name = "adc128d818", |
| 504 | }, |
| 505 | .probe = adc128_probe, |
| 506 | .remove = adc128_remove, |
| 507 | .id_table = adc128_id, |
| 508 | .detect = adc128_detect, |
| 509 | .address_list = normal_i2c, |
| 510 | }; |
| 511 | |
| 512 | module_i2c_driver(adc128_driver); |
| 513 | |
| 514 | MODULE_AUTHOR("Guenter Roeck"); |
| 515 | MODULE_DESCRIPTION("Driver for ADC128D818"); |
| 516 | MODULE_LICENSE("GPL"); |