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 | /* |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 3 | * adm1021.c - Part of lm_sensors, Linux kernel modules for hardware |
| 4 | * monitoring |
| 5 | * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and |
| 6 | * Philip Edelbrock <phil@netroedge.com> |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/jiffies.h> |
| 13 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 14 | #include <linux/hwmon.h> |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 15 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 16 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 21 | static const unsigned short normal_i2c[] = { |
| 22 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 24 | enum chips { |
| 25 | adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* adm1021 constants specified below */ |
| 28 | |
| 29 | /* The adm1021 registers */ |
| 30 | /* Read-only */ |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 31 | /* For nr in 0-1 */ |
| 32 | #define ADM1021_REG_TEMP(nr) (nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #define ADM1021_REG_STATUS 0x02 |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 34 | /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */ |
| 35 | #define ADM1021_REG_MAN_ID 0xFE |
| 36 | /* ADM1021 = 0x0X, ADM1023 = 0x3X */ |
| 37 | #define ADM1021_REG_DEV_ID 0xFF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* These use different addresses for reading/writing */ |
| 39 | #define ADM1021_REG_CONFIG_R 0x03 |
| 40 | #define ADM1021_REG_CONFIG_W 0x09 |
| 41 | #define ADM1021_REG_CONV_RATE_R 0x04 |
| 42 | #define ADM1021_REG_CONV_RATE_W 0x0A |
| 43 | /* These are for the ADM1023's additional precision on the remote temp sensor */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 44 | #define ADM1023_REG_REM_TEMP_PREC 0x10 |
| 45 | #define ADM1023_REG_REM_OFFSET 0x11 |
| 46 | #define ADM1023_REG_REM_OFFSET_PREC 0x12 |
| 47 | #define ADM1023_REG_REM_TOS_PREC 0x13 |
| 48 | #define ADM1023_REG_REM_THYST_PREC 0x14 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* limits */ |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 50 | /* For nr in 0-1 */ |
| 51 | #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr)) |
| 52 | #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr)) |
| 53 | #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr)) |
| 54 | #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* write-only */ |
| 56 | #define ADM1021_REG_ONESHOT 0x0F |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* Initial values */ |
| 59 | |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 60 | /* |
| 61 | * Note: Even though I left the low and high limits named os and hyst, |
| 62 | * they don't quite work like a thermostat the way the LM75 does. I.e., |
| 63 | * a lower temp than THYST actually triggers an alarm instead of |
| 64 | * clearing it. Weird, ey? --Phil |
| 65 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | /* Each client has this additional data */ |
| 68 | struct adm1021_data { |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 69 | struct i2c_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | enum chips type; |
| 71 | |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 72 | const struct attribute_group *groups[3]; |
| 73 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 74 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | char valid; /* !=0 if following fields are valid */ |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 76 | char low_power; /* !=0 if device in low power mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | unsigned long last_updated; /* In jiffies */ |
| 78 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 79 | int temp_max[2]; /* Register values */ |
| 80 | int temp_min[2]; |
| 81 | int temp[2]; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 82 | u8 alarms; |
| 83 | /* Special values for ADM1023 only */ |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 84 | u8 remote_temp_offset; |
| 85 | u8 remote_temp_offset_prec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 89 | static bool read_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Axel Lin | 48995bc | 2014-07-03 21:45:33 +0800 | [diff] [blame] | 91 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
| 92 | { |
| 93 | struct adm1021_data *data = dev_get_drvdata(dev); |
| 94 | struct i2c_client *client = data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Axel Lin | 48995bc | 2014-07-03 21:45:33 +0800 | [diff] [blame] | 96 | mutex_lock(&data->update_lock); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 97 | |
Axel Lin | 48995bc | 2014-07-03 21:45:33 +0800 | [diff] [blame] | 98 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 99 | || !data->valid) { |
| 100 | int i; |
| 101 | |
| 102 | dev_dbg(dev, "Starting adm1021 update\n"); |
| 103 | |
| 104 | for (i = 0; i < 2; i++) { |
| 105 | data->temp[i] = 1000 * |
| 106 | (s8) i2c_smbus_read_byte_data( |
| 107 | client, ADM1021_REG_TEMP(i)); |
| 108 | data->temp_max[i] = 1000 * |
| 109 | (s8) i2c_smbus_read_byte_data( |
| 110 | client, ADM1021_REG_TOS_R(i)); |
| 111 | if (data->type != lm84) { |
| 112 | data->temp_min[i] = 1000 * |
| 113 | (s8) i2c_smbus_read_byte_data(client, |
| 114 | ADM1021_REG_THYST_R(i)); |
| 115 | } |
| 116 | } |
| 117 | data->alarms = i2c_smbus_read_byte_data(client, |
| 118 | ADM1021_REG_STATUS) & 0x7c; |
| 119 | if (data->type == adm1023) { |
| 120 | /* |
| 121 | * The ADM1023 provides 3 extra bits of precision for |
| 122 | * the remote sensor in extra registers. |
| 123 | */ |
| 124 | data->temp[1] += 125 * (i2c_smbus_read_byte_data( |
| 125 | client, ADM1023_REG_REM_TEMP_PREC) >> 5); |
| 126 | data->temp_max[1] += 125 * (i2c_smbus_read_byte_data( |
| 127 | client, ADM1023_REG_REM_TOS_PREC) >> 5); |
| 128 | data->temp_min[1] += 125 * (i2c_smbus_read_byte_data( |
| 129 | client, ADM1023_REG_REM_THYST_PREC) >> 5); |
| 130 | data->remote_temp_offset = |
| 131 | i2c_smbus_read_byte_data(client, |
| 132 | ADM1023_REG_REM_OFFSET); |
| 133 | data->remote_temp_offset_prec = |
| 134 | i2c_smbus_read_byte_data(client, |
| 135 | ADM1023_REG_REM_OFFSET_PREC); |
| 136 | } |
| 137 | data->last_updated = jiffies; |
| 138 | data->valid = 1; |
| 139 | } |
| 140 | |
| 141 | mutex_unlock(&data->update_lock); |
| 142 | |
| 143 | return data; |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 146 | static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, |
| 147 | char *buf) |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 148 | { |
| 149 | int index = to_sensor_dev_attr(devattr)->index; |
| 150 | struct adm1021_data *data = adm1021_update_device(dev); |
| 151 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 152 | return sprintf(buf, "%d\n", data->temp[index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 154 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 155 | static ssize_t temp_max_show(struct device *dev, |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 156 | struct device_attribute *devattr, char *buf) |
| 157 | { |
| 158 | int index = to_sensor_dev_attr(devattr)->index; |
| 159 | struct adm1021_data *data = adm1021_update_device(dev); |
| 160 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 161 | return sprintf(buf, "%d\n", data->temp_max[index]); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 164 | static ssize_t temp_min_show(struct device *dev, |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 165 | struct device_attribute *devattr, char *buf) |
| 166 | { |
| 167 | int index = to_sensor_dev_attr(devattr)->index; |
| 168 | struct adm1021_data *data = adm1021_update_device(dev); |
| 169 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 170 | return sprintf(buf, "%d\n", data->temp_min[index]); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 173 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 174 | char *buf) |
| 175 | { |
| 176 | int index = to_sensor_dev_attr(attr)->index; |
| 177 | struct adm1021_data *data = adm1021_update_device(dev); |
| 178 | return sprintf(buf, "%u\n", (data->alarms >> index) & 1); |
| 179 | } |
| 180 | |
Julia Lawall | 6b41013 | 2016-12-22 13:04:30 +0100 | [diff] [blame] | 181 | static ssize_t alarms_show(struct device *dev, |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 182 | struct device_attribute *attr, |
| 183 | char *buf) |
| 184 | { |
| 185 | struct adm1021_data *data = adm1021_update_device(dev); |
| 186 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 189 | static ssize_t temp_max_store(struct device *dev, |
| 190 | struct device_attribute *devattr, |
| 191 | const char *buf, size_t count) |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 192 | { |
| 193 | int index = to_sensor_dev_attr(devattr)->index; |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 194 | struct adm1021_data *data = dev_get_drvdata(dev); |
| 195 | struct i2c_client *client = data->client; |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 196 | long temp; |
Axel Lin | c024044 | 2014-07-03 22:45:45 +0800 | [diff] [blame] | 197 | int reg_val, err; |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 198 | |
| 199 | err = kstrtol(buf, 10, &temp); |
| 200 | if (err) |
| 201 | return err; |
| 202 | temp /= 1000; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 203 | |
| 204 | mutex_lock(&data->update_lock); |
Axel Lin | c024044 | 2014-07-03 22:45:45 +0800 | [diff] [blame] | 205 | reg_val = clamp_val(temp, -128, 127); |
| 206 | data->temp_max[index] = reg_val * 1000; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 207 | if (!read_only) |
| 208 | i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index), |
Axel Lin | c024044 | 2014-07-03 22:45:45 +0800 | [diff] [blame] | 209 | reg_val); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 210 | mutex_unlock(&data->update_lock); |
| 211 | |
| 212 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 215 | static ssize_t temp_min_store(struct device *dev, |
| 216 | struct device_attribute *devattr, |
| 217 | const char *buf, size_t count) |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 218 | { |
| 219 | int index = to_sensor_dev_attr(devattr)->index; |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 220 | struct adm1021_data *data = dev_get_drvdata(dev); |
| 221 | struct i2c_client *client = data->client; |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 222 | long temp; |
Axel Lin | c024044 | 2014-07-03 22:45:45 +0800 | [diff] [blame] | 223 | int reg_val, err; |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 224 | |
| 225 | err = kstrtol(buf, 10, &temp); |
| 226 | if (err) |
| 227 | return err; |
| 228 | temp /= 1000; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 229 | |
| 230 | mutex_lock(&data->update_lock); |
Axel Lin | c024044 | 2014-07-03 22:45:45 +0800 | [diff] [blame] | 231 | reg_val = clamp_val(temp, -128, 127); |
| 232 | data->temp_min[index] = reg_val * 1000; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 233 | if (!read_only) |
| 234 | i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index), |
Axel Lin | c024044 | 2014-07-03 22:45:45 +0800 | [diff] [blame] | 235 | reg_val); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 236 | mutex_unlock(&data->update_lock); |
| 237 | |
| 238 | return count; |
| 239 | } |
| 240 | |
Julia Lawall | 6b41013 | 2016-12-22 13:04:30 +0100 | [diff] [blame] | 241 | static ssize_t low_power_show(struct device *dev, |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 242 | struct device_attribute *devattr, char *buf) |
| 243 | { |
| 244 | struct adm1021_data *data = adm1021_update_device(dev); |
| 245 | return sprintf(buf, "%d\n", data->low_power); |
| 246 | } |
| 247 | |
Julia Lawall | 6b41013 | 2016-12-22 13:04:30 +0100 | [diff] [blame] | 248 | static ssize_t low_power_store(struct device *dev, |
| 249 | struct device_attribute *devattr, |
| 250 | const char *buf, size_t count) |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 251 | { |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 252 | struct adm1021_data *data = dev_get_drvdata(dev); |
| 253 | struct i2c_client *client = data->client; |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 254 | char low_power; |
| 255 | unsigned long val; |
| 256 | int err; |
| 257 | |
| 258 | err = kstrtoul(buf, 10, &val); |
| 259 | if (err) |
| 260 | return err; |
| 261 | low_power = val != 0; |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 262 | |
| 263 | mutex_lock(&data->update_lock); |
| 264 | if (low_power != data->low_power) { |
| 265 | int config = i2c_smbus_read_byte_data( |
| 266 | client, ADM1021_REG_CONFIG_R); |
| 267 | data->low_power = low_power; |
| 268 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
| 269 | (config & 0xBF) | (low_power << 6)); |
| 270 | } |
| 271 | mutex_unlock(&data->update_lock); |
| 272 | |
| 273 | return count; |
| 274 | } |
| 275 | |
| 276 | |
Guenter Roeck | cfc5bca | 2018-12-10 14:02:00 -0800 | [diff] [blame] | 277 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); |
| 278 | static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); |
| 279 | static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0); |
| 280 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); |
| 281 | static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); |
| 282 | static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1); |
| 283 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6); |
| 284 | static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 5); |
| 285 | static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4); |
| 286 | static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3); |
| 287 | static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2); |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 288 | |
Julia Lawall | 6b41013 | 2016-12-22 13:04:30 +0100 | [diff] [blame] | 289 | static DEVICE_ATTR_RO(alarms); |
| 290 | static DEVICE_ATTR_RW(low_power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 292 | static struct attribute *adm1021_attributes[] = { |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 293 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 294 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 295 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 296 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 297 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 298 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 299 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 300 | &dev_attr_alarms.attr, |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 301 | &dev_attr_low_power.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 302 | NULL |
| 303 | }; |
| 304 | |
| 305 | static const struct attribute_group adm1021_group = { |
| 306 | .attrs = adm1021_attributes, |
| 307 | }; |
| 308 | |
Guenter Roeck | 68146a5 | 2013-06-06 10:30:10 -0700 | [diff] [blame] | 309 | static struct attribute *adm1021_min_attributes[] = { |
| 310 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 311 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 312 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 313 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 314 | NULL |
| 315 | }; |
| 316 | |
| 317 | static const struct attribute_group adm1021_min_group = { |
| 318 | .attrs = adm1021_min_attributes, |
| 319 | }; |
| 320 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 321 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 322 | static int adm1021_detect(struct i2c_client *client, |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 323 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 325 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 326 | const char *type_name; |
| 327 | int conv_rate, status, config, man_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 329 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 330 | pr_debug("detect failed, smbus byte data not supported!\n"); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 331 | return -ENODEV; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 334 | status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); |
| 335 | conv_rate = i2c_smbus_read_byte_data(client, |
| 336 | ADM1021_REG_CONV_RATE_R); |
| 337 | config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 339 | /* Check unused bits */ |
| 340 | if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 341 | pr_debug("detect failed, chip not detected!\n"); |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 342 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* Determine the chip type. */ |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 346 | man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
| 347 | dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Guenter Roeck | 591bfcf | 2013-06-05 14:09:30 -0700 | [diff] [blame] | 349 | if (man_id < 0 || dev_id < 0) |
| 350 | return -ENODEV; |
| 351 | |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 352 | if (man_id == 0x4d && dev_id == 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | type_name = "max1617a"; |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 354 | else if (man_id == 0x41) { |
| 355 | if ((dev_id & 0xF0) == 0x30) |
| 356 | type_name = "adm1023"; |
Guenter Roeck | 591bfcf | 2013-06-05 14:09:30 -0700 | [diff] [blame] | 357 | else if ((dev_id & 0xF0) == 0x00) |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 358 | type_name = "adm1021"; |
Guenter Roeck | 591bfcf | 2013-06-05 14:09:30 -0700 | [diff] [blame] | 359 | else |
| 360 | return -ENODEV; |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 361 | } else if (man_id == 0x49) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | type_name = "thmc10"; |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 363 | else if (man_id == 0x23) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | type_name = "gl523sm"; |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 365 | else if (man_id == 0x54) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | type_name = "mc1066"; |
Guenter Roeck | 591bfcf | 2013-06-05 14:09:30 -0700 | [diff] [blame] | 367 | else { |
| 368 | int lte, rte, lhi, rhi, llo, rlo; |
| 369 | |
| 370 | /* extra checks for LM84 and MAX1617 to avoid misdetections */ |
| 371 | |
| 372 | llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0)); |
| 373 | rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1)); |
| 374 | |
| 375 | /* fail if any of the additional register reads failed */ |
| 376 | if (llo < 0 || rlo < 0) |
| 377 | return -ENODEV; |
| 378 | |
| 379 | lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0)); |
| 380 | rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1)); |
| 381 | lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0)); |
| 382 | rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1)); |
| 383 | |
| 384 | /* |
| 385 | * Fail for negative temperatures and negative high limits. |
| 386 | * This check also catches read errors on the tested registers. |
| 387 | */ |
| 388 | if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0) |
| 389 | return -ENODEV; |
| 390 | |
| 391 | /* fail if all registers hold the same value */ |
| 392 | if (lte == rte && lte == lhi && lte == rhi && lte == llo |
| 393 | && lte == rlo) |
| 394 | return -ENODEV; |
| 395 | |
| 396 | /* |
| 397 | * LM84 Mfr ID is in a different place, |
| 398 | * and it has more unused bits. |
| 399 | */ |
| 400 | if (conv_rate == 0x00 |
| 401 | && (config & 0x7F) == 0x00 |
| 402 | && (status & 0xAB) == 0x00) { |
| 403 | type_name = "lm84"; |
| 404 | } else { |
| 405 | /* fail if low limits are larger than high limits */ |
| 406 | if ((s8)llo > lhi || (s8)rlo > rhi) |
| 407 | return -ENODEV; |
| 408 | type_name = "max1617"; |
| 409 | } |
| 410 | } |
Jean Delvare | 8007ea35 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 411 | |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 412 | pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n", |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 413 | type_name, i2c_adapter_id(adapter), client->addr); |
| 414 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
Axel Lin | 48995bc | 2014-07-03 21:45:33 +0800 | [diff] [blame] | 419 | static void adm1021_init_client(struct i2c_client *client) |
| 420 | { |
| 421 | /* Enable ADC and disable suspend mode */ |
| 422 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
| 423 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); |
| 424 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
| 425 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
| 426 | } |
| 427 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 428 | static int adm1021_probe(struct i2c_client *client, |
| 429 | const struct i2c_device_id *id) |
| 430 | { |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 431 | struct device *dev = &client->dev; |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 432 | struct adm1021_data *data; |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 433 | struct device *hwmon_dev; |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 434 | |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 435 | data = devm_kzalloc(dev, sizeof(struct adm1021_data), GFP_KERNEL); |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 436 | if (!data) |
Guenter Roeck | 06ce97d | 2012-06-02 09:57:58 -0700 | [diff] [blame] | 437 | return -ENOMEM; |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 438 | |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 439 | data->client = client; |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 440 | data->type = id->driver_data; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 441 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /* Initialize the ADM1021 chip */ |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 444 | if (data->type != lm84 && !read_only) |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 445 | adm1021_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 447 | data->groups[0] = &adm1021_group; |
| 448 | if (data->type != lm84) |
| 449 | data->groups[1] = &adm1021_min_group; |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 450 | |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 451 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 452 | data, data->groups); |
Guenter Roeck | 68146a5 | 2013-06-06 10:30:10 -0700 | [diff] [blame] | 453 | |
Guenter Roeck | c669ec8 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 454 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Axel Lin | 48995bc | 2014-07-03 21:45:33 +0800 | [diff] [blame] | 457 | static const struct i2c_device_id adm1021_id[] = { |
| 458 | { "adm1021", adm1021 }, |
| 459 | { "adm1023", adm1023 }, |
| 460 | { "max1617", max1617 }, |
| 461 | { "max1617a", max1617a }, |
| 462 | { "thmc10", thmc10 }, |
| 463 | { "lm84", lm84 }, |
| 464 | { "gl523sm", gl523sm }, |
| 465 | { "mc1066", mc1066 }, |
| 466 | { } |
| 467 | }; |
| 468 | MODULE_DEVICE_TABLE(i2c, adm1021_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Axel Lin | 48995bc | 2014-07-03 21:45:33 +0800 | [diff] [blame] | 470 | static struct i2c_driver adm1021_driver = { |
| 471 | .class = I2C_CLASS_HWMON, |
| 472 | .driver = { |
| 473 | .name = "adm1021", |
| 474 | }, |
| 475 | .probe = adm1021_probe, |
| 476 | .id_table = adm1021_id, |
| 477 | .detect = adm1021_detect, |
| 478 | .address_list = normal_i2c, |
| 479 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 481 | module_i2c_driver(adm1021_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 483 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | "Philip Edelbrock <phil@netroedge.com>"); |
| 485 | MODULE_DESCRIPTION("adm1021 driver"); |
| 486 | MODULE_LICENSE("GPL"); |
| 487 | |
| 488 | module_param(read_only, bool, 0); |
| 489 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |