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