18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for TI ADC128D818 System Monitor with Temperature Sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2014 Guenter Roeck 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Derived from lm80.c 88c2ecf20Sopenharmony_ci * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> 98c2ecf20Sopenharmony_ci * and Philip Edelbrock <phil@netroedge.com> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 158c2ecf20Sopenharmony_ci#include <linux/i2c.h> 168c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 178c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 188c2ecf20Sopenharmony_ci#include <linux/err.h> 198c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 208c2ecf20Sopenharmony_ci#include <linux/mutex.h> 218c2ecf20Sopenharmony_ci#include <linux/bitops.h> 228c2ecf20Sopenharmony_ci#include <linux/of.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* Addresses to scan 258c2ecf20Sopenharmony_ci * The chip also supports addresses 0x35..0x37. Don't scan those addresses 268c2ecf20Sopenharmony_ci * since they are also used by some EEPROMs, which may result in false 278c2ecf20Sopenharmony_ci * positives. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 308c2ecf20Sopenharmony_ci 0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END }; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* registers */ 338c2ecf20Sopenharmony_ci#define ADC128_REG_IN_MAX(nr) (0x2a + (nr) * 2) 348c2ecf20Sopenharmony_ci#define ADC128_REG_IN_MIN(nr) (0x2b + (nr) * 2) 358c2ecf20Sopenharmony_ci#define ADC128_REG_IN(nr) (0x20 + (nr)) 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define ADC128_REG_TEMP 0x27 388c2ecf20Sopenharmony_ci#define ADC128_REG_TEMP_MAX 0x38 398c2ecf20Sopenharmony_ci#define ADC128_REG_TEMP_HYST 0x39 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define ADC128_REG_CONFIG 0x00 428c2ecf20Sopenharmony_ci#define ADC128_REG_ALARM 0x01 438c2ecf20Sopenharmony_ci#define ADC128_REG_MASK 0x03 448c2ecf20Sopenharmony_ci#define ADC128_REG_CONV_RATE 0x07 458c2ecf20Sopenharmony_ci#define ADC128_REG_ONESHOT 0x09 468c2ecf20Sopenharmony_ci#define ADC128_REG_SHUTDOWN 0x0a 478c2ecf20Sopenharmony_ci#define ADC128_REG_CONFIG_ADV 0x0b 488c2ecf20Sopenharmony_ci#define ADC128_REG_BUSY_STATUS 0x0c 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define ADC128_REG_MAN_ID 0x3e 518c2ecf20Sopenharmony_ci#define ADC128_REG_DEV_ID 0x3f 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* No. of voltage entries in adc128_attrs */ 548c2ecf20Sopenharmony_ci#define ADC128_ATTR_NUM_VOLT (8 * 4) 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* Voltage inputs visible per operation mode */ 578c2ecf20Sopenharmony_cistatic const u8 num_inputs[] = { 7, 8, 4, 6 }; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistruct adc128_data { 608c2ecf20Sopenharmony_ci struct i2c_client *client; 618c2ecf20Sopenharmony_ci struct regulator *regulator; 628c2ecf20Sopenharmony_ci int vref; /* Reference voltage in mV */ 638c2ecf20Sopenharmony_ci struct mutex update_lock; 648c2ecf20Sopenharmony_ci u8 mode; /* Operation mode */ 658c2ecf20Sopenharmony_ci bool valid; /* true if following fields are valid */ 668c2ecf20Sopenharmony_ci unsigned long last_updated; /* In jiffies */ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci u16 in[3][8]; /* Register value, normalized to 12 bit 698c2ecf20Sopenharmony_ci * 0: input voltage 708c2ecf20Sopenharmony_ci * 1: min limit 718c2ecf20Sopenharmony_ci * 2: max limit 728c2ecf20Sopenharmony_ci */ 738c2ecf20Sopenharmony_ci s16 temp[3]; /* Register value, normalized to 9 bit 748c2ecf20Sopenharmony_ci * 0: sensor 1: limit 2: hyst 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_ci u8 alarms; /* alarm register value */ 778c2ecf20Sopenharmony_ci}; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic struct adc128_data *adc128_update_device(struct device *dev) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct adc128_data *data = dev_get_drvdata(dev); 828c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 838c2ecf20Sopenharmony_ci struct adc128_data *ret = data; 848c2ecf20Sopenharmony_ci int i, rv; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 898c2ecf20Sopenharmony_ci for (i = 0; i < num_inputs[data->mode]; i++) { 908c2ecf20Sopenharmony_ci rv = i2c_smbus_read_word_swapped(client, 918c2ecf20Sopenharmony_ci ADC128_REG_IN(i)); 928c2ecf20Sopenharmony_ci if (rv < 0) 938c2ecf20Sopenharmony_ci goto abort; 948c2ecf20Sopenharmony_ci data->in[0][i] = rv >> 4; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci rv = i2c_smbus_read_byte_data(client, 978c2ecf20Sopenharmony_ci ADC128_REG_IN_MIN(i)); 988c2ecf20Sopenharmony_ci if (rv < 0) 998c2ecf20Sopenharmony_ci goto abort; 1008c2ecf20Sopenharmony_ci data->in[1][i] = rv << 4; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci rv = i2c_smbus_read_byte_data(client, 1038c2ecf20Sopenharmony_ci ADC128_REG_IN_MAX(i)); 1048c2ecf20Sopenharmony_ci if (rv < 0) 1058c2ecf20Sopenharmony_ci goto abort; 1068c2ecf20Sopenharmony_ci data->in[2][i] = rv << 4; 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci if (data->mode != 1) { 1108c2ecf20Sopenharmony_ci rv = i2c_smbus_read_word_swapped(client, 1118c2ecf20Sopenharmony_ci ADC128_REG_TEMP); 1128c2ecf20Sopenharmony_ci if (rv < 0) 1138c2ecf20Sopenharmony_ci goto abort; 1148c2ecf20Sopenharmony_ci data->temp[0] = rv >> 7; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci rv = i2c_smbus_read_byte_data(client, 1178c2ecf20Sopenharmony_ci ADC128_REG_TEMP_MAX); 1188c2ecf20Sopenharmony_ci if (rv < 0) 1198c2ecf20Sopenharmony_ci goto abort; 1208c2ecf20Sopenharmony_ci data->temp[1] = rv << 1; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci rv = i2c_smbus_read_byte_data(client, 1238c2ecf20Sopenharmony_ci ADC128_REG_TEMP_HYST); 1248c2ecf20Sopenharmony_ci if (rv < 0) 1258c2ecf20Sopenharmony_ci goto abort; 1268c2ecf20Sopenharmony_ci data->temp[2] = rv << 1; 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM); 1308c2ecf20Sopenharmony_ci if (rv < 0) 1318c2ecf20Sopenharmony_ci goto abort; 1328c2ecf20Sopenharmony_ci data->alarms |= rv; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci data->last_updated = jiffies; 1358c2ecf20Sopenharmony_ci data->valid = true; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci goto done; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ciabort: 1408c2ecf20Sopenharmony_ci ret = ERR_PTR(rv); 1418c2ecf20Sopenharmony_ci data->valid = false; 1428c2ecf20Sopenharmony_cidone: 1438c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 1448c2ecf20Sopenharmony_ci return ret; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic ssize_t adc128_in_show(struct device *dev, 1488c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct adc128_data *data = adc128_update_device(dev); 1518c2ecf20Sopenharmony_ci int index = to_sensor_dev_attr_2(attr)->index; 1528c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr_2(attr)->nr; 1538c2ecf20Sopenharmony_ci int val; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (IS_ERR(data)) 1568c2ecf20Sopenharmony_ci return PTR_ERR(data); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095); 1598c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", val); 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic ssize_t adc128_in_store(struct device *dev, 1638c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 1648c2ecf20Sopenharmony_ci size_t count) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci struct adc128_data *data = dev_get_drvdata(dev); 1678c2ecf20Sopenharmony_ci int index = to_sensor_dev_attr_2(attr)->index; 1688c2ecf20Sopenharmony_ci int nr = to_sensor_dev_attr_2(attr)->nr; 1698c2ecf20Sopenharmony_ci u8 reg, regval; 1708c2ecf20Sopenharmony_ci long val; 1718c2ecf20Sopenharmony_ci int err; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 1748c2ecf20Sopenharmony_ci if (err < 0) 1758c2ecf20Sopenharmony_ci return err; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 1788c2ecf20Sopenharmony_ci /* 10 mV LSB on limit registers */ 1798c2ecf20Sopenharmony_ci regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255); 1808c2ecf20Sopenharmony_ci data->in[index][nr] = regval << 4; 1818c2ecf20Sopenharmony_ci reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr); 1828c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(data->client, reg, regval); 1838c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci return count; 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic ssize_t adc128_temp_show(struct device *dev, 1898c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci struct adc128_data *data = adc128_update_device(dev); 1928c2ecf20Sopenharmony_ci int index = to_sensor_dev_attr(attr)->index; 1938c2ecf20Sopenharmony_ci int temp; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci if (IS_ERR(data)) 1968c2ecf20Sopenharmony_ci return PTR_ERR(data); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci temp = sign_extend32(data->temp[index], 8); 1998c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */ 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic ssize_t adc128_temp_store(struct device *dev, 2038c2ecf20Sopenharmony_ci struct device_attribute *attr, 2048c2ecf20Sopenharmony_ci const char *buf, size_t count) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci struct adc128_data *data = dev_get_drvdata(dev); 2078c2ecf20Sopenharmony_ci int index = to_sensor_dev_attr(attr)->index; 2088c2ecf20Sopenharmony_ci long val; 2098c2ecf20Sopenharmony_ci int err; 2108c2ecf20Sopenharmony_ci s8 regval; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci err = kstrtol(buf, 10, &val); 2138c2ecf20Sopenharmony_ci if (err < 0) 2148c2ecf20Sopenharmony_ci return err; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 2178c2ecf20Sopenharmony_ci regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); 2188c2ecf20Sopenharmony_ci data->temp[index] = regval << 1; 2198c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(data->client, 2208c2ecf20Sopenharmony_ci index == 1 ? ADC128_REG_TEMP_MAX 2218c2ecf20Sopenharmony_ci : ADC128_REG_TEMP_HYST, 2228c2ecf20Sopenharmony_ci regval); 2238c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return count; 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic ssize_t adc128_alarm_show(struct device *dev, 2298c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci struct adc128_data *data = adc128_update_device(dev); 2328c2ecf20Sopenharmony_ci int mask = 1 << to_sensor_dev_attr(attr)->index; 2338c2ecf20Sopenharmony_ci u8 alarms; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci if (IS_ERR(data)) 2368c2ecf20Sopenharmony_ci return PTR_ERR(data); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci /* 2398c2ecf20Sopenharmony_ci * Clear an alarm after reporting it to user space. If it is still 2408c2ecf20Sopenharmony_ci * active, the next update sequence will set the alarm bit again. 2418c2ecf20Sopenharmony_ci */ 2428c2ecf20Sopenharmony_ci alarms = data->alarms; 2438c2ecf20Sopenharmony_ci data->alarms &= ~mask; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", !!(alarms & mask)); 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic umode_t adc128_is_visible(struct kobject *kobj, 2498c2ecf20Sopenharmony_ci struct attribute *attr, int index) 2508c2ecf20Sopenharmony_ci{ 2518c2ecf20Sopenharmony_ci struct device *dev = container_of(kobj, struct device, kobj); 2528c2ecf20Sopenharmony_ci struct adc128_data *data = dev_get_drvdata(dev); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (index < ADC128_ATTR_NUM_VOLT) { 2558c2ecf20Sopenharmony_ci /* Voltage, visible according to num_inputs[] */ 2568c2ecf20Sopenharmony_ci if (index >= num_inputs[data->mode] * 4) 2578c2ecf20Sopenharmony_ci return 0; 2588c2ecf20Sopenharmony_ci } else { 2598c2ecf20Sopenharmony_ci /* Temperature, visible if not in mode 1 */ 2608c2ecf20Sopenharmony_ci if (data->mode == 1) 2618c2ecf20Sopenharmony_ci return 0; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci return attr->mode; 2658c2ecf20Sopenharmony_ci} 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in0_input, adc128_in, 0, 0); 2688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in0_min, adc128_in, 0, 1); 2698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in0_max, adc128_in, 0, 2); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in1_input, adc128_in, 1, 0); 2728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in1_min, adc128_in, 1, 1); 2738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in1_max, adc128_in, 1, 2); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in2_input, adc128_in, 2, 0); 2768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in2_min, adc128_in, 2, 1); 2778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in2_max, adc128_in, 2, 2); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in3_input, adc128_in, 3, 0); 2808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in3_min, adc128_in, 3, 1); 2818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in3_max, adc128_in, 3, 2); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in4_input, adc128_in, 4, 0); 2848c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in4_min, adc128_in, 4, 1); 2858c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in4_max, adc128_in, 4, 2); 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in5_input, adc128_in, 5, 0); 2888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in5_min, adc128_in, 5, 1); 2898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in5_max, adc128_in, 5, 2); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in6_input, adc128_in, 6, 0); 2928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in6_min, adc128_in, 6, 1); 2938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in6_max, adc128_in, 6, 2); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in7_input, adc128_in, 7, 0); 2968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in7_min, adc128_in, 7, 1); 2978c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(in7_max, adc128_in, 7, 2); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, adc128_temp, 0); 3008c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, adc128_temp, 1); 3018c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adc128_temp, 2); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, adc128_alarm, 0); 3048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, adc128_alarm, 1); 3058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, adc128_alarm, 2); 3068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, adc128_alarm, 3); 3078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, adc128_alarm, 4); 3088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, adc128_alarm, 5); 3098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_alarm, adc128_alarm, 6); 3108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_alarm, adc128_alarm, 7); 3118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adc128_alarm, 7); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_cistatic struct attribute *adc128_attrs[] = { 3148c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_alarm.dev_attr.attr, 3158c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_input.dev_attr.attr, 3168c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_max.dev_attr.attr, 3178c2ecf20Sopenharmony_ci &sensor_dev_attr_in0_min.dev_attr.attr, 3188c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_alarm.dev_attr.attr, 3198c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_input.dev_attr.attr, 3208c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_max.dev_attr.attr, 3218c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_min.dev_attr.attr, 3228c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_alarm.dev_attr.attr, 3238c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_input.dev_attr.attr, 3248c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_max.dev_attr.attr, 3258c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_min.dev_attr.attr, 3268c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_alarm.dev_attr.attr, 3278c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_input.dev_attr.attr, 3288c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_max.dev_attr.attr, 3298c2ecf20Sopenharmony_ci &sensor_dev_attr_in3_min.dev_attr.attr, 3308c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_alarm.dev_attr.attr, 3318c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_input.dev_attr.attr, 3328c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_max.dev_attr.attr, 3338c2ecf20Sopenharmony_ci &sensor_dev_attr_in4_min.dev_attr.attr, 3348c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_alarm.dev_attr.attr, 3358c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_input.dev_attr.attr, 3368c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_max.dev_attr.attr, 3378c2ecf20Sopenharmony_ci &sensor_dev_attr_in5_min.dev_attr.attr, 3388c2ecf20Sopenharmony_ci &sensor_dev_attr_in6_alarm.dev_attr.attr, 3398c2ecf20Sopenharmony_ci &sensor_dev_attr_in6_input.dev_attr.attr, 3408c2ecf20Sopenharmony_ci &sensor_dev_attr_in6_max.dev_attr.attr, 3418c2ecf20Sopenharmony_ci &sensor_dev_attr_in6_min.dev_attr.attr, 3428c2ecf20Sopenharmony_ci &sensor_dev_attr_in7_alarm.dev_attr.attr, 3438c2ecf20Sopenharmony_ci &sensor_dev_attr_in7_input.dev_attr.attr, 3448c2ecf20Sopenharmony_ci &sensor_dev_attr_in7_max.dev_attr.attr, 3458c2ecf20Sopenharmony_ci &sensor_dev_attr_in7_min.dev_attr.attr, 3468c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 3478c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max.dev_attr.attr, 3488c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 3498c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 3508c2ecf20Sopenharmony_ci NULL 3518c2ecf20Sopenharmony_ci}; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic const struct attribute_group adc128_group = { 3548c2ecf20Sopenharmony_ci .attrs = adc128_attrs, 3558c2ecf20Sopenharmony_ci .is_visible = adc128_is_visible, 3568c2ecf20Sopenharmony_ci}; 3578c2ecf20Sopenharmony_ci__ATTRIBUTE_GROUPS(adc128); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic int adc128_detect(struct i2c_client *client, struct i2c_board_info *info) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci int man_id, dev_id; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, 3648c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_BYTE_DATA | 3658c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_WORD_DATA)) 3668c2ecf20Sopenharmony_ci return -ENODEV; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID); 3698c2ecf20Sopenharmony_ci dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID); 3708c2ecf20Sopenharmony_ci if (man_id != 0x01 || dev_id != 0x09) 3718c2ecf20Sopenharmony_ci return -ENODEV; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci /* Check unused bits for confirmation */ 3748c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4) 3758c2ecf20Sopenharmony_ci return -ENODEV; 3768c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe) 3778c2ecf20Sopenharmony_ci return -ENODEV; 3788c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe) 3798c2ecf20Sopenharmony_ci return -ENODEV; 3808c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe) 3818c2ecf20Sopenharmony_ci return -ENODEV; 3828c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8) 3838c2ecf20Sopenharmony_ci return -ENODEV; 3848c2ecf20Sopenharmony_ci if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc) 3858c2ecf20Sopenharmony_ci return -ENODEV; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci strlcpy(info->type, "adc128d818", I2C_NAME_SIZE); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci return 0; 3908c2ecf20Sopenharmony_ci} 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_cistatic int adc128_init_client(struct adc128_data *data) 3938c2ecf20Sopenharmony_ci{ 3948c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 3958c2ecf20Sopenharmony_ci int err; 3968c2ecf20Sopenharmony_ci u8 regval = 0x0; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci /* 3998c2ecf20Sopenharmony_ci * Reset chip to defaults. 4008c2ecf20Sopenharmony_ci * This makes most other initializations unnecessary. 4018c2ecf20Sopenharmony_ci */ 4028c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80); 4038c2ecf20Sopenharmony_ci if (err) 4048c2ecf20Sopenharmony_ci return err; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci /* Set operation mode, if non-default */ 4078c2ecf20Sopenharmony_ci if (data->mode != 0) 4088c2ecf20Sopenharmony_ci regval |= data->mode << 1; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci /* If external vref is selected, configure the chip to use it */ 4118c2ecf20Sopenharmony_ci if (data->regulator) 4128c2ecf20Sopenharmony_ci regval |= 0x01; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* Write advanced configuration register */ 4158c2ecf20Sopenharmony_ci if (regval != 0x0) { 4168c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV, 4178c2ecf20Sopenharmony_ci regval); 4188c2ecf20Sopenharmony_ci if (err) 4198c2ecf20Sopenharmony_ci return err; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci /* Start monitoring */ 4238c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01); 4248c2ecf20Sopenharmony_ci if (err) 4258c2ecf20Sopenharmony_ci return err; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci return 0; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistatic int adc128_probe(struct i2c_client *client) 4318c2ecf20Sopenharmony_ci{ 4328c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 4338c2ecf20Sopenharmony_ci struct regulator *regulator; 4348c2ecf20Sopenharmony_ci struct device *hwmon_dev; 4358c2ecf20Sopenharmony_ci struct adc128_data *data; 4368c2ecf20Sopenharmony_ci int err, vref; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL); 4398c2ecf20Sopenharmony_ci if (!data) 4408c2ecf20Sopenharmony_ci return -ENOMEM; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci /* vref is optional. If specified, is used as chip reference voltage */ 4438c2ecf20Sopenharmony_ci regulator = devm_regulator_get_optional(dev, "vref"); 4448c2ecf20Sopenharmony_ci if (!IS_ERR(regulator)) { 4458c2ecf20Sopenharmony_ci data->regulator = regulator; 4468c2ecf20Sopenharmony_ci err = regulator_enable(regulator); 4478c2ecf20Sopenharmony_ci if (err < 0) 4488c2ecf20Sopenharmony_ci return err; 4498c2ecf20Sopenharmony_ci vref = regulator_get_voltage(regulator); 4508c2ecf20Sopenharmony_ci if (vref < 0) { 4518c2ecf20Sopenharmony_ci err = vref; 4528c2ecf20Sopenharmony_ci goto error; 4538c2ecf20Sopenharmony_ci } 4548c2ecf20Sopenharmony_ci data->vref = DIV_ROUND_CLOSEST(vref, 1000); 4558c2ecf20Sopenharmony_ci } else { 4568c2ecf20Sopenharmony_ci data->vref = 2560; /* 2.56V, in mV */ 4578c2ecf20Sopenharmony_ci } 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci /* Operation mode is optional. If unspecified, keep current mode */ 4608c2ecf20Sopenharmony_ci if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) { 4618c2ecf20Sopenharmony_ci if (data->mode > 3) { 4628c2ecf20Sopenharmony_ci dev_err(dev, "invalid operation mode %d\n", 4638c2ecf20Sopenharmony_ci data->mode); 4648c2ecf20Sopenharmony_ci err = -EINVAL; 4658c2ecf20Sopenharmony_ci goto error; 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci } else { 4688c2ecf20Sopenharmony_ci err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV); 4698c2ecf20Sopenharmony_ci if (err < 0) 4708c2ecf20Sopenharmony_ci goto error; 4718c2ecf20Sopenharmony_ci data->mode = (err >> 1) & ADC128_REG_MASK; 4728c2ecf20Sopenharmony_ci } 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci data->client = client; 4758c2ecf20Sopenharmony_ci i2c_set_clientdata(client, data); 4768c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci /* Initialize the chip */ 4798c2ecf20Sopenharmony_ci err = adc128_init_client(data); 4808c2ecf20Sopenharmony_ci if (err < 0) 4818c2ecf20Sopenharmony_ci goto error; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 4848c2ecf20Sopenharmony_ci data, adc128_groups); 4858c2ecf20Sopenharmony_ci if (IS_ERR(hwmon_dev)) { 4868c2ecf20Sopenharmony_ci err = PTR_ERR(hwmon_dev); 4878c2ecf20Sopenharmony_ci goto error; 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci return 0; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cierror: 4938c2ecf20Sopenharmony_ci if (data->regulator) 4948c2ecf20Sopenharmony_ci regulator_disable(data->regulator); 4958c2ecf20Sopenharmony_ci return err; 4968c2ecf20Sopenharmony_ci} 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_cistatic int adc128_remove(struct i2c_client *client) 4998c2ecf20Sopenharmony_ci{ 5008c2ecf20Sopenharmony_ci struct adc128_data *data = i2c_get_clientdata(client); 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci if (data->regulator) 5038c2ecf20Sopenharmony_ci regulator_disable(data->regulator); 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci return 0; 5068c2ecf20Sopenharmony_ci} 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_cistatic const struct i2c_device_id adc128_id[] = { 5098c2ecf20Sopenharmony_ci { "adc128d818", 0 }, 5108c2ecf20Sopenharmony_ci { } 5118c2ecf20Sopenharmony_ci}; 5128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, adc128_id); 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused adc128_of_match[] = { 5158c2ecf20Sopenharmony_ci { .compatible = "ti,adc128d818" }, 5168c2ecf20Sopenharmony_ci { }, 5178c2ecf20Sopenharmony_ci}; 5188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, adc128_of_match); 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_cistatic struct i2c_driver adc128_driver = { 5218c2ecf20Sopenharmony_ci .class = I2C_CLASS_HWMON, 5228c2ecf20Sopenharmony_ci .driver = { 5238c2ecf20Sopenharmony_ci .name = "adc128d818", 5248c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(adc128_of_match), 5258c2ecf20Sopenharmony_ci }, 5268c2ecf20Sopenharmony_ci .probe_new = adc128_probe, 5278c2ecf20Sopenharmony_ci .remove = adc128_remove, 5288c2ecf20Sopenharmony_ci .id_table = adc128_id, 5298c2ecf20Sopenharmony_ci .detect = adc128_detect, 5308c2ecf20Sopenharmony_ci .address_list = normal_i2c, 5318c2ecf20Sopenharmony_ci}; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_cimodule_i2c_driver(adc128_driver); 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck"); 5368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for ADC128D818"); 5378c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 538