Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * adm1025.c |
| 4 | * |
| 5 | * Copyright (C) 2000 Chen-Yuan Wu <gwu@esoft.com> |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 6 | * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6 |
| 9 | * voltages (including its own power source) and up to two temperatures |
| 10 | * (its own plus up to one external one). Voltages are scaled internally |
| 11 | * (which is not the common way) with ratios such that the nominal value |
| 12 | * of each voltage correspond to a register value of 192 (which means a |
| 13 | * resolution of about 0.5% of the nominal value). Temperature values are |
| 14 | * reported with a 1 deg resolution and a 3 deg accuracy. Complete |
| 15 | * datasheet can be obtained from Analog's website at: |
Alexander A. Klimov | 0303d06 | 2020-07-13 16:00:28 +0200 | [diff] [blame] | 16 | * https://www.onsemi.com/PowerSolutions/product.do?id=ADM1025 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 18 | * This driver also supports the ADM1025A, which differs from the ADM1025 |
| 19 | * only in that it has "open-drain VID inputs while the ADM1025 has |
| 20 | * on-chip 100k pull-ups on the VID inputs". It doesn't make any |
| 21 | * difference for us. |
| 22 | * |
| 23 | * This driver also supports the NE1619, a sensor chip made by Philips. |
| 24 | * That chip is similar to the ADM1025A, with a few differences. The only |
| 25 | * difference that matters to us is that the NE1619 has only two possible |
| 26 | * addresses while the ADM1025A has a third one. Complete datasheet can be |
| 27 | * obtained from Philips's website at: |
| 28 | * http://www.semiconductors.philips.com/pip/NE1619DS.html |
| 29 | * |
| 30 | * Since the ADM1025 was the first chipset supported by this driver, most |
| 31 | * comments will refer to this chipset, but are actually general and |
| 32 | * concern all supported chipsets, unless mentioned otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/jiffies.h> |
| 39 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 40 | #include <linux/hwmon.h> |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 41 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 42 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 43 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 44 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Addresses to scan |
| 48 | * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e. |
| 49 | * NE1619 has two possible addresses: 0x2c and 0x2d. |
| 50 | */ |
| 51 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 52 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 54 | enum chips { adm1025, ne1619 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * The ADM1025 registers |
| 58 | */ |
| 59 | |
| 60 | #define ADM1025_REG_MAN_ID 0x3E |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 61 | #define ADM1025_REG_CHIP_ID 0x3F |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #define ADM1025_REG_CONFIG 0x40 |
| 63 | #define ADM1025_REG_STATUS1 0x41 |
| 64 | #define ADM1025_REG_STATUS2 0x42 |
| 65 | #define ADM1025_REG_IN(nr) (0x20 + (nr)) |
| 66 | #define ADM1025_REG_IN_MAX(nr) (0x2B + (nr) * 2) |
| 67 | #define ADM1025_REG_IN_MIN(nr) (0x2C + (nr) * 2) |
| 68 | #define ADM1025_REG_TEMP(nr) (0x26 + (nr)) |
| 69 | #define ADM1025_REG_TEMP_HIGH(nr) (0x37 + (nr) * 2) |
| 70 | #define ADM1025_REG_TEMP_LOW(nr) (0x38 + (nr) * 2) |
| 71 | #define ADM1025_REG_VID 0x47 |
| 72 | #define ADM1025_REG_VID4 0x49 |
| 73 | |
| 74 | /* |
| 75 | * Conversions and various macros |
| 76 | * The ADM1025 uses signed 8-bit values for temperatures. |
| 77 | */ |
| 78 | |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 79 | static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 81 | #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192) |
| 82 | #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \ |
Guenter Roeck | 7cb53e2 | 2016-12-03 11:07:14 -0800 | [diff] [blame] | 83 | (val) >= (scale) * 255 / 192 ? 255 : \ |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 84 | ((val) * 192 + (scale) / 2) / (scale)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | #define TEMP_FROM_REG(reg) ((reg) * 1000) |
| 87 | #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \ |
| 88 | (val) >= 126500 ? 127 : \ |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 89 | (((val) < 0 ? (val) - 500 : \ |
| 90 | (val) + 500) / 1000)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * Client data (each client gets its own) |
| 94 | */ |
| 95 | |
| 96 | struct adm1025_data { |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 97 | struct i2c_client *client; |
| 98 | const struct attribute_group *groups[3]; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 99 | struct mutex update_lock; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 100 | bool valid; /* false until following fields are valid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | unsigned long last_updated; /* in jiffies */ |
| 102 | |
| 103 | u8 in[6]; /* register value */ |
| 104 | u8 in_max[6]; /* register value */ |
| 105 | u8 in_min[6]; /* register value */ |
| 106 | s8 temp[2]; /* register value */ |
| 107 | s8 temp_min[2]; /* register value */ |
| 108 | s8 temp_max[2]; /* register value */ |
| 109 | u16 alarms; /* register values, combined */ |
| 110 | u8 vid; /* register values, combined */ |
| 111 | u8 vrm; |
| 112 | }; |
| 113 | |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 114 | static struct adm1025_data *adm1025_update_device(struct device *dev) |
| 115 | { |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 116 | struct adm1025_data *data = dev_get_drvdata(dev); |
| 117 | struct i2c_client *client = data->client; |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 118 | |
| 119 | mutex_lock(&data->update_lock); |
| 120 | |
| 121 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { |
| 122 | int i; |
| 123 | |
| 124 | dev_dbg(&client->dev, "Updating data.\n"); |
| 125 | for (i = 0; i < 6; i++) { |
| 126 | data->in[i] = i2c_smbus_read_byte_data(client, |
| 127 | ADM1025_REG_IN(i)); |
| 128 | data->in_min[i] = i2c_smbus_read_byte_data(client, |
| 129 | ADM1025_REG_IN_MIN(i)); |
| 130 | data->in_max[i] = i2c_smbus_read_byte_data(client, |
| 131 | ADM1025_REG_IN_MAX(i)); |
| 132 | } |
| 133 | for (i = 0; i < 2; i++) { |
| 134 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 135 | ADM1025_REG_TEMP(i)); |
| 136 | data->temp_min[i] = i2c_smbus_read_byte_data(client, |
| 137 | ADM1025_REG_TEMP_LOW(i)); |
| 138 | data->temp_max[i] = i2c_smbus_read_byte_data(client, |
| 139 | ADM1025_REG_TEMP_HIGH(i)); |
| 140 | } |
| 141 | data->alarms = i2c_smbus_read_byte_data(client, |
| 142 | ADM1025_REG_STATUS1) |
| 143 | | (i2c_smbus_read_byte_data(client, |
| 144 | ADM1025_REG_STATUS2) << 8); |
| 145 | data->vid = (i2c_smbus_read_byte_data(client, |
| 146 | ADM1025_REG_VID) & 0x0f) |
| 147 | | ((i2c_smbus_read_byte_data(client, |
| 148 | ADM1025_REG_VID4) & 0x01) << 4); |
| 149 | |
| 150 | data->last_updated = jiffies; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 151 | data->valid = true; |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | mutex_unlock(&data->update_lock); |
| 155 | |
| 156 | return data; |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Sysfs stuff |
| 161 | */ |
| 162 | |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 163 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 164 | in_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 165 | { |
| 166 | int index = to_sensor_dev_attr(attr)->index; |
| 167 | struct adm1025_data *data = adm1025_update_device(dev); |
| 168 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[index], |
| 169 | in_scale[index])); |
| 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 172 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 173 | in_min_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 174 | { |
| 175 | int index = to_sensor_dev_attr(attr)->index; |
| 176 | struct adm1025_data *data = adm1025_update_device(dev); |
| 177 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[index], |
| 178 | in_scale[index])); |
| 179 | } |
| 180 | |
| 181 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 182 | in_max_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 183 | { |
| 184 | int index = to_sensor_dev_attr(attr)->index; |
| 185 | struct adm1025_data *data = adm1025_update_device(dev); |
| 186 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[index], |
| 187 | in_scale[index])); |
| 188 | } |
| 189 | |
| 190 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 191 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 192 | { |
| 193 | int index = to_sensor_dev_attr(attr)->index; |
| 194 | struct adm1025_data *data = adm1025_update_device(dev); |
| 195 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[index])); |
| 196 | } |
| 197 | |
| 198 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 199 | temp_min_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 200 | { |
| 201 | int index = to_sensor_dev_attr(attr)->index; |
| 202 | struct adm1025_data *data = adm1025_update_device(dev); |
| 203 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[index])); |
| 204 | } |
| 205 | |
| 206 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 207 | temp_max_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 208 | { |
| 209 | int index = to_sensor_dev_attr(attr)->index; |
| 210 | struct adm1025_data *data = adm1025_update_device(dev); |
| 211 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index])); |
| 212 | } |
| 213 | |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 214 | static ssize_t in_min_store(struct device *dev, struct device_attribute *attr, |
| 215 | const char *buf, size_t count) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 216 | { |
| 217 | int index = to_sensor_dev_attr(attr)->index; |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 218 | struct adm1025_data *data = dev_get_drvdata(dev); |
| 219 | struct i2c_client *client = data->client; |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 220 | long val; |
| 221 | int err; |
| 222 | |
| 223 | err = kstrtol(buf, 10, &val); |
| 224 | if (err) |
| 225 | return err; |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 226 | |
| 227 | mutex_lock(&data->update_lock); |
| 228 | data->in_min[index] = IN_TO_REG(val, in_scale[index]); |
| 229 | i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(index), |
| 230 | data->in_min[index]); |
| 231 | mutex_unlock(&data->update_lock); |
| 232 | return count; |
| 233 | } |
| 234 | |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 235 | static ssize_t in_max_store(struct device *dev, struct device_attribute *attr, |
| 236 | const char *buf, size_t count) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 237 | { |
| 238 | int index = to_sensor_dev_attr(attr)->index; |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 239 | struct adm1025_data *data = dev_get_drvdata(dev); |
| 240 | struct i2c_client *client = data->client; |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 241 | long val; |
| 242 | int err; |
| 243 | |
| 244 | err = kstrtol(buf, 10, &val); |
| 245 | if (err) |
| 246 | return err; |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 247 | |
| 248 | mutex_lock(&data->update_lock); |
| 249 | data->in_max[index] = IN_TO_REG(val, in_scale[index]); |
| 250 | i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(index), |
| 251 | data->in_max[index]); |
| 252 | mutex_unlock(&data->update_lock); |
| 253 | return count; |
| 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 256 | static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0); |
| 257 | static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0); |
| 258 | static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0); |
| 259 | static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1); |
| 260 | static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1); |
| 261 | static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1); |
| 262 | static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2); |
| 263 | static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2); |
| 264 | static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2); |
| 265 | static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3); |
| 266 | static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3); |
| 267 | static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3); |
| 268 | static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4); |
| 269 | static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4); |
| 270 | static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4); |
| 271 | static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5); |
| 272 | static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5); |
| 273 | static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 275 | static ssize_t temp_min_store(struct device *dev, |
| 276 | struct device_attribute *attr, const char *buf, |
| 277 | size_t count) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 278 | { |
| 279 | int index = to_sensor_dev_attr(attr)->index; |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 280 | struct adm1025_data *data = dev_get_drvdata(dev); |
| 281 | struct i2c_client *client = data->client; |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 282 | long val; |
| 283 | int err; |
| 284 | |
| 285 | err = kstrtol(buf, 10, &val); |
| 286 | if (err) |
| 287 | return err; |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 288 | |
| 289 | mutex_lock(&data->update_lock); |
| 290 | data->temp_min[index] = TEMP_TO_REG(val); |
| 291 | i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(index), |
| 292 | data->temp_min[index]); |
| 293 | mutex_unlock(&data->update_lock); |
| 294 | return count; |
| 295 | } |
| 296 | |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 297 | static ssize_t temp_max_store(struct device *dev, |
| 298 | struct device_attribute *attr, const char *buf, |
| 299 | size_t count) |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 300 | { |
| 301 | int index = to_sensor_dev_attr(attr)->index; |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 302 | struct adm1025_data *data = dev_get_drvdata(dev); |
| 303 | struct i2c_client *client = data->client; |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 304 | long val; |
| 305 | int err; |
| 306 | |
| 307 | err = kstrtol(buf, 10, &val); |
| 308 | if (err) |
| 309 | return err; |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 310 | |
| 311 | mutex_lock(&data->update_lock); |
| 312 | data->temp_max[index] = TEMP_TO_REG(val); |
| 313 | i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(index), |
| 314 | data->temp_max[index]); |
| 315 | mutex_unlock(&data->update_lock); |
| 316 | return count; |
| 317 | } |
| 318 | |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 319 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); |
| 320 | static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0); |
| 321 | static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); |
| 322 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); |
| 323 | static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1); |
| 324 | static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 326 | static ssize_t |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 327 | alarms_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | struct adm1025_data *data = adm1025_update_device(dev); |
| 330 | return sprintf(buf, "%u\n", data->alarms); |
| 331 | } |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 332 | static DEVICE_ATTR_RO(alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Jean Delvare | bb08130 | 2007-10-10 21:11:52 +0200 | [diff] [blame] | 334 | static ssize_t |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 335 | alarm_show(struct device *dev, struct device_attribute *attr, char *buf) |
Jean Delvare | bb08130 | 2007-10-10 21:11:52 +0200 | [diff] [blame] | 336 | { |
| 337 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 338 | struct adm1025_data *data = adm1025_update_device(dev); |
| 339 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 340 | } |
Guenter Roeck | 66efcd2 | 2019-01-21 18:33:47 -0800 | [diff] [blame] | 341 | static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); |
| 342 | static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); |
| 343 | static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); |
| 344 | static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); |
| 345 | static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8); |
| 346 | static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9); |
| 347 | static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 5); |
| 348 | static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 4); |
| 349 | static SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14); |
Jean Delvare | bb08130 | 2007-10-10 21:11:52 +0200 | [diff] [blame] | 350 | |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 351 | static ssize_t |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 352 | cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct adm1025_data *data = adm1025_update_device(dev); |
| 355 | return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); |
| 356 | } |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 357 | static DEVICE_ATTR_RO(cpu0_vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 359 | static ssize_t |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 360 | vrm_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 362 | struct adm1025_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return sprintf(buf, "%u\n", data->vrm); |
| 364 | } |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 365 | static ssize_t vrm_store(struct device *dev, struct device_attribute *attr, |
| 366 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 368 | struct adm1025_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 369 | unsigned long val; |
| 370 | int err; |
| 371 | |
| 372 | err = kstrtoul(buf, 10, &val); |
| 373 | if (err) |
| 374 | return err; |
| 375 | |
Axel Lin | 9c8ae72 | 2014-08-06 08:19:43 +0800 | [diff] [blame] | 376 | if (val > 255) |
| 377 | return -EINVAL; |
| 378 | |
Guenter Roeck | 2b22de5 | 2012-01-14 12:49:22 -0800 | [diff] [blame] | 379 | data->vrm = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return count; |
| 381 | } |
Julia Lawall | 41919a5 | 2016-12-22 13:05:28 +0100 | [diff] [blame] | 382 | static DEVICE_ATTR_RW(vrm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Real code |
| 386 | */ |
| 387 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 388 | static struct attribute *adm1025_attributes[] = { |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 389 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 390 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 391 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 392 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 393 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 394 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 395 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 396 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 397 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 398 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 399 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 400 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 401 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 402 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 403 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | bb08130 | 2007-10-10 21:11:52 +0200 | [diff] [blame] | 404 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 405 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 406 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 407 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 408 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 409 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 410 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 411 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 412 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 413 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 414 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
Jean Delvare | bb08130 | 2007-10-10 21:11:52 +0200 | [diff] [blame] | 415 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 416 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 417 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 418 | &dev_attr_alarms.attr, |
| 419 | &dev_attr_cpu0_vid.attr, |
| 420 | &dev_attr_vrm.attr, |
| 421 | NULL |
| 422 | }; |
| 423 | |
| 424 | static const struct attribute_group adm1025_group = { |
| 425 | .attrs = adm1025_attributes, |
| 426 | }; |
| 427 | |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 428 | static struct attribute *adm1025_attributes_in4[] = { |
Jean Delvare | d8543e7 | 2007-10-10 21:11:01 +0200 | [diff] [blame] | 429 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 430 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 431 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | bb08130 | 2007-10-10 21:11:52 +0200 | [diff] [blame] | 432 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 433 | NULL |
| 434 | }; |
| 435 | |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 436 | static const struct attribute_group adm1025_group_in4 = { |
| 437 | .attrs = adm1025_attributes_in4, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 438 | }; |
| 439 | |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 440 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 441 | static int adm1025_detect(struct i2c_client *client, |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 442 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 444 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 9560672 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 445 | const char *name; |
| 446 | u8 man_id, chip_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 449 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Jean Delvare | 9560672 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 451 | /* Check for unused bits */ |
| 452 | if ((i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG) & 0x80) |
| 453 | || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS1) & 0xC0) |
| 454 | || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS2) & 0xBC)) { |
| 455 | dev_dbg(&adapter->dev, "ADM1025 detection failed at 0x%02x\n", |
| 456 | client->addr); |
| 457 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Jean Delvare | 9560672 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 460 | /* Identification */ |
| 461 | chip_id = i2c_smbus_read_byte_data(client, ADM1025_REG_CHIP_ID); |
| 462 | if ((chip_id & 0xF0) != 0x20) |
| 463 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Jean Delvare | 9560672 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 465 | man_id = i2c_smbus_read_byte_data(client, ADM1025_REG_MAN_ID); |
| 466 | if (man_id == 0x41) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | name = "adm1025"; |
Jean Delvare | 9560672 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 468 | else if (man_id == 0xA1 && client->addr != 0x2E) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | name = "ne1619"; |
Jean Delvare | 9560672 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 470 | else |
| 471 | return -ENODEV; |
| 472 | |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 473 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 478 | static void adm1025_init_client(struct i2c_client *client) |
| 479 | { |
| 480 | u8 reg; |
| 481 | struct adm1025_data *data = i2c_get_clientdata(client); |
| 482 | int i; |
| 483 | |
| 484 | data->vrm = vid_which_vrm(); |
| 485 | |
| 486 | /* |
| 487 | * Set high limits |
| 488 | * Usually we avoid setting limits on driver init, but it happens |
| 489 | * that the ADM1025 comes with stupid default limits (all registers |
| 490 | * set to 0). In case the chip has not gone through any limit |
| 491 | * setting yet, we better set the high limits to the max so that |
| 492 | * no alarm triggers. |
| 493 | */ |
| 494 | for (i = 0; i < 6; i++) { |
| 495 | reg = i2c_smbus_read_byte_data(client, |
| 496 | ADM1025_REG_IN_MAX(i)); |
| 497 | if (reg == 0) |
| 498 | i2c_smbus_write_byte_data(client, |
| 499 | ADM1025_REG_IN_MAX(i), |
| 500 | 0xFF); |
| 501 | } |
| 502 | for (i = 0; i < 2; i++) { |
| 503 | reg = i2c_smbus_read_byte_data(client, |
| 504 | ADM1025_REG_TEMP_HIGH(i)); |
| 505 | if (reg == 0) |
| 506 | i2c_smbus_write_byte_data(client, |
| 507 | ADM1025_REG_TEMP_HIGH(i), |
| 508 | 0x7F); |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Start the conversions |
| 513 | */ |
| 514 | reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG); |
| 515 | if (!(reg & 0x01)) |
| 516 | i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG, |
| 517 | (reg&0x7E)|0x01); |
| 518 | } |
| 519 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 520 | static int adm1025_probe(struct i2c_client *client) |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 521 | { |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 522 | struct device *dev = &client->dev; |
| 523 | struct device *hwmon_dev; |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 524 | struct adm1025_data *data; |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 525 | u8 config; |
| 526 | |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 527 | data = devm_kzalloc(dev, sizeof(struct adm1025_data), GFP_KERNEL); |
Guenter Roeck | 6702c99 | 2012-06-02 09:57:58 -0700 | [diff] [blame] | 528 | if (!data) |
| 529 | return -ENOMEM; |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 530 | |
| 531 | i2c_set_clientdata(client, data); |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 532 | data->client = client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 533 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /* Initialize the ADM1025 chip */ |
Jean Delvare | 4e95279 | 2007-10-10 21:14:11 +0200 | [diff] [blame] | 536 | adm1025_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 538 | /* sysfs hooks */ |
| 539 | data->groups[0] = &adm1025_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* Pin 11 is either in4 (+12V) or VID4 */ |
Jean Delvare | 7dbafe0 | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 541 | config = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG); |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 542 | if (!(config & 0x20)) |
| 543 | data->groups[1] = &adm1025_group_in4; |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 544 | |
Axel Lin | aab3f57 | 2014-07-03 21:49:27 +0800 | [diff] [blame] | 545 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 546 | data, data->groups); |
| 547 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 550 | static const struct i2c_device_id adm1025_id[] = { |
| 551 | { "adm1025", adm1025 }, |
| 552 | { "ne1619", ne1619 }, |
| 553 | { } |
| 554 | }; |
| 555 | MODULE_DEVICE_TABLE(i2c, adm1025_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 557 | static struct i2c_driver adm1025_driver = { |
| 558 | .class = I2C_CLASS_HWMON, |
| 559 | .driver = { |
| 560 | .name = "adm1025", |
| 561 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 562 | .probe_new = adm1025_probe, |
Axel Lin | 851a7af | 2014-07-03 21:48:15 +0800 | [diff] [blame] | 563 | .id_table = adm1025_id, |
| 564 | .detect = adm1025_detect, |
| 565 | .address_list = normal_i2c, |
| 566 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 568 | module_i2c_driver(adm1025_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 570 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | MODULE_DESCRIPTION("ADM1025 driver"); |
| 572 | MODULE_LICENSE("GPL"); |