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 { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 82 | struct device *hwmon_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | enum chips type; |
| 84 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 85 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | char valid; /* !=0 if following fields are valid */ |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 87 | char low_power; /* !=0 if device in low power mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | unsigned long last_updated; /* In jiffies */ |
| 89 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 90 | int temp_max[2]; /* Register values */ |
| 91 | int temp_min[2]; |
| 92 | int temp[2]; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 93 | u8 alarms; |
| 94 | /* Special values for ADM1023 only */ |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 95 | u8 remote_temp_offset; |
| 96 | u8 remote_temp_offset_prec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 99 | static int adm1021_probe(struct i2c_client *client, |
| 100 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 101 | static int adm1021_detect(struct i2c_client *client, |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 102 | struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static void adm1021_init_client(struct i2c_client *client); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 104 | static int adm1021_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | static struct adm1021_data *adm1021_update_device(struct device *dev); |
| 106 | |
| 107 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 108 | static bool read_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 111 | static const struct i2c_device_id adm1021_id[] = { |
| 112 | { "adm1021", adm1021 }, |
| 113 | { "adm1023", adm1023 }, |
| 114 | { "max1617", max1617 }, |
| 115 | { "max1617a", max1617a }, |
| 116 | { "thmc10", thmc10 }, |
| 117 | { "lm84", lm84 }, |
| 118 | { "gl523sm", gl523sm }, |
| 119 | { "mc1066", mc1066 }, |
| 120 | { } |
| 121 | }; |
| 122 | MODULE_DEVICE_TABLE(i2c, adm1021_id); |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* This is the driver that will be inserted */ |
| 125 | static struct i2c_driver adm1021_driver = { |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 126 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 127 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 128 | .name = "adm1021", |
| 129 | }, |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 130 | .probe = adm1021_probe, |
| 131 | .remove = adm1021_remove, |
| 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; |
| 185 | struct i2c_client *client = to_i2c_client(dev); |
| 186 | struct adm1021_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 187 | long temp; |
| 188 | int err; |
| 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); |
| 196 | data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127); |
| 197 | if (!read_only) |
| 198 | i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index), |
| 199 | data->temp_max[index]); |
| 200 | mutex_unlock(&data->update_lock); |
| 201 | |
| 202 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 205 | static ssize_t set_temp_min(struct device *dev, |
| 206 | struct device_attribute *devattr, |
| 207 | const char *buf, size_t count) |
| 208 | { |
| 209 | int index = to_sensor_dev_attr(devattr)->index; |
| 210 | struct i2c_client *client = to_i2c_client(dev); |
| 211 | struct adm1021_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 212 | long temp; |
| 213 | int err; |
| 214 | |
| 215 | err = kstrtol(buf, 10, &temp); |
| 216 | if (err) |
| 217 | return err; |
| 218 | temp /= 1000; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 219 | |
| 220 | mutex_lock(&data->update_lock); |
| 221 | data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127); |
| 222 | if (!read_only) |
| 223 | i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index), |
| 224 | data->temp_min[index]); |
| 225 | mutex_unlock(&data->update_lock); |
| 226 | |
| 227 | return count; |
| 228 | } |
| 229 | |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 230 | static ssize_t show_low_power(struct device *dev, |
| 231 | struct device_attribute *devattr, char *buf) |
| 232 | { |
| 233 | struct adm1021_data *data = adm1021_update_device(dev); |
| 234 | return sprintf(buf, "%d\n", data->low_power); |
| 235 | } |
| 236 | |
| 237 | static ssize_t set_low_power(struct device *dev, |
| 238 | struct device_attribute *devattr, |
| 239 | const char *buf, size_t count) |
| 240 | { |
| 241 | struct i2c_client *client = to_i2c_client(dev); |
| 242 | struct adm1021_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 243 | char low_power; |
| 244 | unsigned long val; |
| 245 | int err; |
| 246 | |
| 247 | err = kstrtoul(buf, 10, &val); |
| 248 | if (err) |
| 249 | return err; |
| 250 | low_power = val != 0; |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 251 | |
| 252 | mutex_lock(&data->update_lock); |
| 253 | if (low_power != data->low_power) { |
| 254 | int config = i2c_smbus_read_byte_data( |
| 255 | client, ADM1021_REG_CONFIG_R); |
| 256 | data->low_power = low_power; |
| 257 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
| 258 | (config & 0xBF) | (low_power << 6)); |
| 259 | } |
| 260 | mutex_unlock(&data->update_lock); |
| 261 | |
| 262 | return count; |
| 263 | } |
| 264 | |
| 265 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 266 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 267 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 268 | set_temp_max, 0); |
| 269 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 270 | set_temp_min, 0); |
| 271 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 272 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 273 | set_temp_max, 1); |
| 274 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 275 | set_temp_min, 1); |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 276 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 277 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 278 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 279 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 280 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 283 | 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] | 284 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 285 | static struct attribute *adm1021_attributes[] = { |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 286 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 287 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 288 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 289 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 290 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 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, |
| 293 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 294 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 295 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 296 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 297 | &dev_attr_alarms.attr, |
Michael Abbott | 905ffdc | 2009-09-21 17:04:47 -0700 | [diff] [blame] | 298 | &dev_attr_low_power.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 299 | NULL |
| 300 | }; |
| 301 | |
| 302 | static const struct attribute_group adm1021_group = { |
| 303 | .attrs = adm1021_attributes, |
| 304 | }; |
| 305 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 306 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 307 | static int adm1021_detect(struct i2c_client *client, |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 308 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 310 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 311 | const char *type_name; |
| 312 | int conv_rate, status, config, man_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 314 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 315 | pr_debug("adm1021: detect failed, " |
| 316 | "smbus byte data not supported!\n"); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 317 | return -ENODEV; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 320 | status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); |
| 321 | conv_rate = i2c_smbus_read_byte_data(client, |
| 322 | ADM1021_REG_CONV_RATE_R); |
| 323 | config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 325 | /* Check unused bits */ |
| 326 | if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) { |
| 327 | pr_debug("adm1021: detect failed, chip not detected!\n"); |
| 328 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* Determine the chip type. */ |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 332 | man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
| 333 | dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 335 | if (man_id == 0x4d && dev_id == 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | type_name = "max1617a"; |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 337 | else if (man_id == 0x41) { |
| 338 | if ((dev_id & 0xF0) == 0x30) |
| 339 | type_name = "adm1023"; |
| 340 | else |
| 341 | type_name = "adm1021"; |
| 342 | } else if (man_id == 0x49) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | type_name = "thmc10"; |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 344 | else if (man_id == 0x23) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | type_name = "gl523sm"; |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 346 | else if (man_id == 0x54) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | type_name = "mc1066"; |
Jean Delvare | 8007ea3 | 2009-12-09 20:35:51 +0100 | [diff] [blame] | 348 | /* LM84 Mfr ID in a different place, and it has more unused bits */ |
| 349 | else if (conv_rate == 0x00 |
| 350 | && (config & 0x7F) == 0x00 |
| 351 | && (status & 0xAB) == 0x00) |
| 352 | type_name = "lm84"; |
| 353 | else |
| 354 | type_name = "max1617"; |
| 355 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 356 | pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n", |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 357 | type_name, i2c_adapter_id(adapter), client->addr); |
| 358 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int adm1021_probe(struct i2c_client *client, |
| 364 | const struct i2c_device_id *id) |
| 365 | { |
| 366 | struct adm1021_data *data; |
| 367 | int err; |
| 368 | |
| 369 | data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL); |
| 370 | if (!data) { |
| 371 | pr_debug("adm1021: detect failed, kzalloc failed!\n"); |
| 372 | err = -ENOMEM; |
| 373 | goto error0; |
| 374 | } |
| 375 | |
| 376 | i2c_set_clientdata(client, data); |
| 377 | data->type = id->driver_data; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 378 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* Initialize the ADM1021 chip */ |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 381 | if (data->type != lm84 && !read_only) |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 382 | adm1021_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | /* Register sysfs hooks */ |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 385 | err = sysfs_create_group(&client->dev.kobj, &adm1021_group); |
| 386 | if (err) |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 387 | goto error1; |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 388 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 389 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 390 | if (IS_ERR(data->hwmon_dev)) { |
| 391 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 392 | goto error3; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return 0; |
| 396 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 397 | error3: |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 398 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | error1: |
| 400 | kfree(data); |
| 401 | error0: |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | static void adm1021_init_client(struct i2c_client *client) |
| 406 | { |
| 407 | /* Enable ADC and disable suspend mode */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 408 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
| 409 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 411 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 414 | static int adm1021_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 416 | struct adm1021_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 418 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 419 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 420 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 421 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
| 426 | { |
| 427 | struct i2c_client *client = to_i2c_client(dev); |
| 428 | struct adm1021_data *data = i2c_get_clientdata(client); |
| 429 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 430 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 433 | || !data->valid) { |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 434 | int i; |
| 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | dev_dbg(&client->dev, "Starting adm1021 update\n"); |
| 437 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 438 | for (i = 0; i < 2; i++) { |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 439 | data->temp[i] = 1000 * |
| 440 | (s8) i2c_smbus_read_byte_data( |
| 441 | client, ADM1021_REG_TEMP(i)); |
| 442 | data->temp_max[i] = 1000 * |
| 443 | (s8) i2c_smbus_read_byte_data( |
| 444 | client, ADM1021_REG_TOS_R(i)); |
| 445 | data->temp_min[i] = 1000 * |
| 446 | (s8) i2c_smbus_read_byte_data( |
| 447 | client, ADM1021_REG_THYST_R(i)); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 448 | } |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 449 | data->alarms = i2c_smbus_read_byte_data(client, |
| 450 | ADM1021_REG_STATUS) & 0x7c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (data->type == adm1023) { |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 452 | /* |
| 453 | * The ADM1023 provides 3 extra bits of precision for |
| 454 | * the remote sensor in extra registers. |
| 455 | */ |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame] | 456 | data->temp[1] += 125 * (i2c_smbus_read_byte_data( |
| 457 | client, ADM1023_REG_REM_TEMP_PREC) >> 5); |
| 458 | data->temp_max[1] += 125 * (i2c_smbus_read_byte_data( |
| 459 | client, ADM1023_REG_REM_TOS_PREC) >> 5); |
| 460 | data->temp_min[1] += 125 * (i2c_smbus_read_byte_data( |
| 461 | client, ADM1023_REG_REM_THYST_PREC) >> 5); |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 462 | data->remote_temp_offset = |
| 463 | i2c_smbus_read_byte_data(client, |
| 464 | ADM1023_REG_REM_OFFSET); |
| 465 | data->remote_temp_offset_prec = |
| 466 | i2c_smbus_read_byte_data(client, |
| 467 | ADM1023_REG_REM_OFFSET_PREC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | data->last_updated = jiffies; |
| 470 | data->valid = 1; |
| 471 | } |
| 472 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 473 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | return data; |
| 476 | } |
| 477 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 478 | module_i2c_driver(adm1021_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Guenter Roeck | 21d2a8f | 2012-01-14 12:45:47 -0800 | [diff] [blame] | 480 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | "Philip Edelbrock <phil@netroedge.com>"); |
| 482 | MODULE_DESCRIPTION("adm1021 driver"); |
| 483 | MODULE_LICENSE("GPL"); |
| 484 | |
| 485 | module_param(read_only, bool, 0); |
| 486 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |