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