Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 1 | /* |
| 2 | * adm9240.c Part of lm_sensors, Linux kernel modules for hardware |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 3 | * monitoring |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl> |
| 6 | * Philip Edelbrock <phil@netroedge.com> |
| 7 | * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl> |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 8 | * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 9 | * guidance from Jean Delvare |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 10 | * |
| 11 | * Driver supports Analog Devices ADM9240 |
| 12 | * Dallas Semiconductor DS1780 |
| 13 | * National Semiconductor LM81 |
| 14 | * |
| 15 | * ADM9240 is the reference, DS1780 and LM81 are register compatibles |
| 16 | * |
| 17 | * Voltage Six inputs are scaled by chip, VID also reported |
| 18 | * Temperature Chip temperature to 0.5'C, maximum and max_hysteris |
| 19 | * Fans 2 fans, low speed alarm, automatic fan clock divider |
| 20 | * Alarms 16-bit map of active alarms |
| 21 | * Analog Out 0..1250 mV output |
| 22 | * |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 23 | * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm' |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 24 | * |
| 25 | * Test hardware: Intel SE440BX-2 desktop motherboard --Grant |
| 26 | * |
| 27 | * LM81 extended temp reading not implemented |
| 28 | * |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License as published by |
| 31 | * the Free Software Foundation; either version 2 of the License, or |
| 32 | * (at your option) any later version. |
| 33 | * |
| 34 | * This program is distributed in the hope that it will be useful, |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | * GNU General Public License for more details. |
| 38 | * |
| 39 | * You should have received a copy of the GNU General Public License |
| 40 | * along with this program; if not, write to the Free Software |
| 41 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 42 | */ |
| 43 | |
| 44 | #include <linux/init.h> |
| 45 | #include <linux/module.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/i2c.h> |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 48 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 49 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 50 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 51 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 52 | #include <linux/mutex.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 53 | #include <linux/jiffies.h> |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 54 | |
| 55 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 56 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 57 | I2C_CLIENT_END }; |
| 58 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 59 | enum chips { adm9240, ds1780, lm81 }; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 60 | |
| 61 | /* ADM9240 registers */ |
| 62 | #define ADM9240_REG_MAN_ID 0x3e |
| 63 | #define ADM9240_REG_DIE_REV 0x3f |
| 64 | #define ADM9240_REG_CONFIG 0x40 |
| 65 | |
| 66 | #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */ |
| 67 | #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2) |
| 68 | #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2) |
| 69 | #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */ |
| 70 | #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 71 | #define ADM9240_REG_INT(nr) (0x41 + (nr)) |
| 72 | #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr)) |
| 73 | #define ADM9240_REG_TEMP 0x27 |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 74 | #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 75 | #define ADM9240_REG_ANALOG_OUT 0x19 |
| 76 | #define ADM9240_REG_CHASSIS_CLEAR 0x46 |
| 77 | #define ADM9240_REG_VID_FAN_DIV 0x47 |
| 78 | #define ADM9240_REG_I2C_ADDR 0x48 |
| 79 | #define ADM9240_REG_VID4 0x49 |
| 80 | #define ADM9240_REG_TEMP_CONF 0x4b |
| 81 | |
| 82 | /* generalised scaling with integer rounding */ |
| 83 | static inline int SCALE(long val, int mul, int div) |
| 84 | { |
| 85 | if (val < 0) |
| 86 | return (val * mul - div / 2) / div; |
| 87 | else |
| 88 | return (val * mul + div / 2) / div; |
| 89 | } |
| 90 | |
| 91 | /* adm9240 internally scales voltage measurements */ |
| 92 | static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 }; |
| 93 | |
| 94 | static inline unsigned int IN_FROM_REG(u8 reg, int n) |
| 95 | { |
| 96 | return SCALE(reg, nom_mv[n], 192); |
| 97 | } |
| 98 | |
| 99 | static inline u8 IN_TO_REG(unsigned long val, int n) |
| 100 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 101 | return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* temperature range: -40..125, 127 disables temperature alarm */ |
| 105 | static inline s8 TEMP_TO_REG(long val) |
| 106 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 107 | return clamp_val(SCALE(val, 1, 1000), -40, 127); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* two fans, each with low fan speed limit */ |
| 111 | static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) |
| 112 | { |
| 113 | if (!reg) /* error */ |
| 114 | return -1; |
| 115 | |
| 116 | if (reg == 255) |
| 117 | return 0; |
| 118 | |
| 119 | return SCALE(1350000, 1, reg * div); |
| 120 | } |
| 121 | |
| 122 | /* analog out 0..1250mV */ |
| 123 | static inline u8 AOUT_TO_REG(unsigned long val) |
| 124 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 125 | return clamp_val(SCALE(val, 255, 1250), 0, 255); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static inline unsigned int AOUT_FROM_REG(u8 reg) |
| 129 | { |
| 130 | return SCALE(reg, 1250, 255); |
| 131 | } |
| 132 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 133 | /* per client data */ |
| 134 | struct adm9240_data { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 135 | struct i2c_client *client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 136 | struct mutex update_lock; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 137 | char valid; |
| 138 | unsigned long last_updated_measure; |
| 139 | unsigned long last_updated_config; |
| 140 | |
| 141 | u8 in[6]; /* ro in0_input */ |
| 142 | u8 in_max[6]; /* rw in0_max */ |
| 143 | u8 in_min[6]; /* rw in0_min */ |
| 144 | u8 fan[2]; /* ro fan1_input */ |
| 145 | u8 fan_min[2]; /* rw fan1_min */ |
| 146 | u8 fan_div[2]; /* rw fan1_div, read-only accessor */ |
| 147 | s16 temp; /* ro temp1_input, 9-bit sign-extended */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 148 | s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 149 | u16 alarms; /* ro alarms */ |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 150 | u8 aout; /* rw aout_output */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 151 | u8 vid; /* ro vid */ |
| 152 | u8 vrm; /* -- vrm set on startup, no accessor */ |
| 153 | }; |
| 154 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 155 | /* write new fan div, callers must hold data->update_lock */ |
| 156 | static void adm9240_write_fan_div(struct i2c_client *client, int nr, |
| 157 | u8 fan_div) |
| 158 | { |
| 159 | u8 reg, old, shift = (nr + 2) * 2; |
| 160 | |
| 161 | reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
| 162 | old = (reg >> shift) & 3; |
| 163 | reg &= ~(3 << shift); |
| 164 | reg |= (fan_div << shift); |
| 165 | i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg); |
| 166 | dev_dbg(&client->dev, |
| 167 | "fan%d clock divider changed from %u to %u\n", |
| 168 | nr + 1, 1 << old, 1 << fan_div); |
| 169 | } |
| 170 | |
| 171 | static struct adm9240_data *adm9240_update_device(struct device *dev) |
| 172 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 173 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 174 | struct i2c_client *client = data->client; |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 175 | int i; |
| 176 | |
| 177 | mutex_lock(&data->update_lock); |
| 178 | |
| 179 | /* minimum measurement cycle: 1.75 seconds */ |
| 180 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) |
| 181 | || !data->valid) { |
| 182 | |
| 183 | for (i = 0; i < 6; i++) { /* read voltages */ |
| 184 | data->in[i] = i2c_smbus_read_byte_data(client, |
| 185 | ADM9240_REG_IN(i)); |
| 186 | } |
| 187 | data->alarms = i2c_smbus_read_byte_data(client, |
| 188 | ADM9240_REG_INT(0)) | |
| 189 | i2c_smbus_read_byte_data(client, |
| 190 | ADM9240_REG_INT(1)) << 8; |
| 191 | |
| 192 | /* |
| 193 | * read temperature: assume temperature changes less than |
| 194 | * 0.5'C per two measurement cycles thus ignore possible |
| 195 | * but unlikely aliasing error on lsb reading. --Grant |
| 196 | */ |
Chris Packham | 667f4ba | 2016-10-05 10:40:54 +1300 | [diff] [blame] | 197 | data->temp = (i2c_smbus_read_byte_data(client, |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 198 | ADM9240_REG_TEMP) << 8) | |
| 199 | i2c_smbus_read_byte_data(client, |
Chris Packham | 667f4ba | 2016-10-05 10:40:54 +1300 | [diff] [blame] | 200 | ADM9240_REG_TEMP_CONF); |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 201 | |
| 202 | for (i = 0; i < 2; i++) { /* read fans */ |
| 203 | data->fan[i] = i2c_smbus_read_byte_data(client, |
| 204 | ADM9240_REG_FAN(i)); |
| 205 | |
| 206 | /* adjust fan clock divider on overflow */ |
| 207 | if (data->valid && data->fan[i] == 255 && |
| 208 | data->fan_div[i] < 3) { |
| 209 | |
| 210 | adm9240_write_fan_div(client, i, |
| 211 | ++data->fan_div[i]); |
| 212 | |
| 213 | /* adjust fan_min if active, but not to 0 */ |
| 214 | if (data->fan_min[i] < 255 && |
| 215 | data->fan_min[i] >= 2) |
| 216 | data->fan_min[i] /= 2; |
| 217 | } |
| 218 | } |
| 219 | data->last_updated_measure = jiffies; |
| 220 | } |
| 221 | |
| 222 | /* minimum config reading cycle: 300 seconds */ |
| 223 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) |
| 224 | || !data->valid) { |
| 225 | |
| 226 | for (i = 0; i < 6; i++) { |
| 227 | data->in_min[i] = i2c_smbus_read_byte_data(client, |
| 228 | ADM9240_REG_IN_MIN(i)); |
| 229 | data->in_max[i] = i2c_smbus_read_byte_data(client, |
| 230 | ADM9240_REG_IN_MAX(i)); |
| 231 | } |
| 232 | for (i = 0; i < 2; i++) { |
| 233 | data->fan_min[i] = i2c_smbus_read_byte_data(client, |
| 234 | ADM9240_REG_FAN_MIN(i)); |
| 235 | } |
| 236 | data->temp_max[0] = i2c_smbus_read_byte_data(client, |
| 237 | ADM9240_REG_TEMP_MAX(0)); |
| 238 | data->temp_max[1] = i2c_smbus_read_byte_data(client, |
| 239 | ADM9240_REG_TEMP_MAX(1)); |
| 240 | |
| 241 | /* read fan divs and 5-bit VID */ |
| 242 | i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
| 243 | data->fan_div[0] = (i >> 4) & 3; |
| 244 | data->fan_div[1] = (i >> 6) & 3; |
| 245 | data->vid = i & 0x0f; |
| 246 | data->vid |= (i2c_smbus_read_byte_data(client, |
| 247 | ADM9240_REG_VID4) & 1) << 4; |
| 248 | /* read analog out */ |
| 249 | data->aout = i2c_smbus_read_byte_data(client, |
| 250 | ADM9240_REG_ANALOG_OUT); |
| 251 | |
| 252 | data->last_updated_config = jiffies; |
| 253 | data->valid = 1; |
| 254 | } |
| 255 | mutex_unlock(&data->update_lock); |
| 256 | return data; |
| 257 | } |
| 258 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 259 | /*** sysfs accessors ***/ |
| 260 | |
| 261 | /* temperature */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 262 | static ssize_t show_temp(struct device *dev, struct device_attribute *dummy, |
| 263 | char *buf) |
| 264 | { |
| 265 | struct adm9240_data *data = adm9240_update_device(dev); |
Chris Packham | 667f4ba | 2016-10-05 10:40:54 +1300 | [diff] [blame] | 266 | return sprintf(buf, "%d\n", data->temp / 128 * 500); /* 9-bit value */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 267 | } |
| 268 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 269 | static ssize_t show_max(struct device *dev, struct device_attribute *devattr, |
| 270 | char *buf) |
| 271 | { |
| 272 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 273 | struct adm9240_data *data = adm9240_update_device(dev); |
| 274 | return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); |
| 275 | } |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 276 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 277 | static ssize_t set_max(struct device *dev, struct device_attribute *devattr, |
| 278 | const char *buf, size_t count) |
| 279 | { |
| 280 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 281 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 282 | struct i2c_client *client = data->client; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 283 | long val; |
| 284 | int err; |
| 285 | |
| 286 | err = kstrtol(buf, 10, &val); |
| 287 | if (err) |
| 288 | return err; |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 289 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 290 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 291 | data->temp_max[attr->index] = TEMP_TO_REG(val); |
| 292 | i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index), |
| 293 | data->temp_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 294 | mutex_unlock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 295 | return count; |
| 296 | } |
| 297 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 298 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 299 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 300 | show_max, set_max, 0); |
| 301 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 302 | show_max, set_max, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 303 | |
| 304 | /* voltage */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 305 | static ssize_t show_in(struct device *dev, struct device_attribute *devattr, |
| 306 | char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 307 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 308 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 309 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 310 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], |
| 311 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 312 | } |
| 313 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 314 | static ssize_t show_in_min(struct device *dev, |
| 315 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 316 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 317 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 318 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 319 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], |
| 320 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 321 | } |
| 322 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 323 | static ssize_t show_in_max(struct device *dev, |
| 324 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 325 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 326 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 327 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 328 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], |
| 329 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 330 | } |
| 331 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 332 | static ssize_t set_in_min(struct device *dev, |
| 333 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 334 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 335 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 336 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 337 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 338 | struct i2c_client *client = data->client; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 339 | unsigned long val; |
| 340 | int err; |
| 341 | |
| 342 | err = kstrtoul(buf, 10, &val); |
| 343 | if (err) |
| 344 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 345 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 346 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 347 | data->in_min[attr->index] = IN_TO_REG(val, attr->index); |
| 348 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index), |
| 349 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 350 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 351 | return count; |
| 352 | } |
| 353 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 354 | static ssize_t set_in_max(struct device *dev, |
| 355 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 356 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 357 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 358 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 359 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 360 | struct i2c_client *client = data->client; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 361 | unsigned long val; |
| 362 | int err; |
| 363 | |
| 364 | err = kstrtoul(buf, 10, &val); |
| 365 | if (err) |
| 366 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 367 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 368 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 369 | data->in_max[attr->index] = IN_TO_REG(val, attr->index); |
| 370 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index), |
| 371 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 372 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 373 | return count; |
| 374 | } |
| 375 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 376 | #define vin(nr) \ |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 377 | static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 378 | show_in, NULL, nr); \ |
| 379 | static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \ |
| 380 | show_in_min, set_in_min, nr); \ |
| 381 | static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \ |
| 382 | show_in_max, set_in_max, nr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 383 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 384 | vin(0); |
| 385 | vin(1); |
| 386 | vin(2); |
| 387 | vin(3); |
| 388 | vin(4); |
| 389 | vin(5); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 390 | |
| 391 | /* fans */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 392 | static ssize_t show_fan(struct device *dev, |
| 393 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 394 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 395 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 396 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 397 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], |
| 398 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 399 | } |
| 400 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 401 | static ssize_t show_fan_min(struct device *dev, |
| 402 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 403 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 404 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 405 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 406 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], |
| 407 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 408 | } |
| 409 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 410 | static ssize_t show_fan_div(struct device *dev, |
| 411 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 412 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 413 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 414 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 415 | return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 416 | } |
| 417 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 418 | /* |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 419 | * set fan speed low limit: |
| 420 | * |
| 421 | * - value is zero: disable fan speed low limit alarm |
| 422 | * |
| 423 | * - value is below fan speed measurement range: enable fan speed low |
| 424 | * limit alarm to be asserted while fan speed too slow to measure |
| 425 | * |
| 426 | * - otherwise: select fan clock divider to suit fan speed low limit, |
| 427 | * measurement code may adjust registers to ensure fan speed reading |
| 428 | */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 429 | static ssize_t set_fan_min(struct device *dev, |
| 430 | struct device_attribute *devattr, |
| 431 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 432 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 433 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 434 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 435 | struct i2c_client *client = data->client; |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 436 | int nr = attr->index; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 437 | u8 new_div; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 438 | unsigned long val; |
| 439 | int err; |
| 440 | |
| 441 | err = kstrtoul(buf, 10, &val); |
| 442 | if (err) |
| 443 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 444 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 445 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 446 | |
| 447 | if (!val) { |
| 448 | data->fan_min[nr] = 255; |
| 449 | new_div = data->fan_div[nr]; |
| 450 | |
| 451 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", |
| 452 | nr + 1); |
| 453 | |
| 454 | } else if (val < 1350000 / (8 * 254)) { |
| 455 | new_div = 3; |
| 456 | data->fan_min[nr] = 254; |
| 457 | |
| 458 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", |
| 459 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); |
| 460 | |
| 461 | } else { |
| 462 | unsigned int new_min = 1350000 / val; |
| 463 | |
| 464 | new_div = 0; |
| 465 | while (new_min > 192 && new_div < 3) { |
| 466 | new_div++; |
| 467 | new_min /= 2; |
| 468 | } |
| 469 | if (!new_min) /* keep > 0 */ |
| 470 | new_min++; |
| 471 | |
| 472 | data->fan_min[nr] = new_min; |
| 473 | |
| 474 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", |
| 475 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); |
| 476 | } |
| 477 | |
| 478 | if (new_div != data->fan_div[nr]) { |
| 479 | data->fan_div[nr] = new_div; |
| 480 | adm9240_write_fan_div(client, nr, new_div); |
| 481 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 482 | i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr), |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 483 | data->fan_min[nr]); |
| 484 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 485 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 486 | return count; |
| 487 | } |
| 488 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 489 | #define fan(nr) \ |
| 490 | static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \ |
| 491 | show_fan, NULL, nr - 1); \ |
| 492 | static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \ |
| 493 | show_fan_div, NULL, nr - 1); \ |
| 494 | static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \ |
| 495 | show_fan_min, set_fan_min, nr - 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 496 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 497 | fan(1); |
| 498 | fan(2); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 499 | |
| 500 | /* alarms */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 501 | static ssize_t show_alarms(struct device *dev, |
| 502 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 503 | { |
| 504 | struct adm9240_data *data = adm9240_update_device(dev); |
| 505 | return sprintf(buf, "%u\n", data->alarms); |
| 506 | } |
| 507 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 508 | |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 509 | static ssize_t show_alarm(struct device *dev, |
| 510 | struct device_attribute *attr, char *buf) |
| 511 | { |
| 512 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 513 | struct adm9240_data *data = adm9240_update_device(dev); |
| 514 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 515 | } |
| 516 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 517 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 518 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 519 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 520 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 521 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 522 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 523 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 524 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 525 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 526 | /* vid */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 527 | static ssize_t show_vid(struct device *dev, |
| 528 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 529 | { |
| 530 | struct adm9240_data *data = adm9240_update_device(dev); |
| 531 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 532 | } |
| 533 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 534 | |
| 535 | /* analog output */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 536 | static ssize_t show_aout(struct device *dev, |
| 537 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 538 | { |
| 539 | struct adm9240_data *data = adm9240_update_device(dev); |
| 540 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 541 | } |
| 542 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 543 | static ssize_t set_aout(struct device *dev, |
| 544 | struct device_attribute *attr, |
| 545 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 546 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 547 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 548 | struct i2c_client *client = data->client; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 549 | long val; |
| 550 | int err; |
| 551 | |
| 552 | err = kstrtol(buf, 10, &val); |
| 553 | if (err) |
| 554 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 555 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 556 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 557 | data->aout = AOUT_TO_REG(val); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 558 | i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 559 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 560 | return count; |
| 561 | } |
| 562 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 563 | |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 564 | static ssize_t chassis_clear(struct device *dev, |
| 565 | struct device_attribute *attr, |
| 566 | const char *buf, size_t count) |
| 567 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 568 | struct adm9240_data *data = dev_get_drvdata(dev); |
| 569 | struct i2c_client *client = data->client; |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 570 | unsigned long val; |
| 571 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 572 | if (kstrtoul(buf, 10, &val) || val != 0) |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 573 | return -EINVAL; |
| 574 | |
| 575 | mutex_lock(&data->update_lock); |
| 576 | i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80); |
| 577 | data->valid = 0; /* Force cache refresh */ |
| 578 | mutex_unlock(&data->update_lock); |
| 579 | dev_dbg(&client->dev, "chassis intrusion latch cleared\n"); |
| 580 | |
| 581 | return count; |
| 582 | } |
| 583 | static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm, |
| 584 | chassis_clear, 12); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 585 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 586 | static struct attribute *adm9240_attrs[] = { |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 587 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 588 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 589 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 590 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 591 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 592 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 593 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 594 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 595 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 596 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 597 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 598 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 599 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 600 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 601 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 602 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 603 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 604 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 605 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 606 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 607 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 608 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 609 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 610 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 611 | &dev_attr_temp1_input.attr, |
| 612 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 613 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 614 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 615 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 616 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 617 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 618 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 619 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 620 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 621 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 622 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 623 | &dev_attr_alarms.attr, |
| 624 | &dev_attr_aout_output.attr, |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 625 | &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 626 | &dev_attr_cpu0_vid.attr, |
| 627 | NULL |
| 628 | }; |
| 629 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 630 | ATTRIBUTE_GROUPS(adm9240); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 631 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 632 | |
| 633 | /*** sensor chip detect and driver install ***/ |
| 634 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 635 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 636 | static int adm9240_detect(struct i2c_client *new_client, |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 637 | struct i2c_board_info *info) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 638 | { |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 639 | struct i2c_adapter *adapter = new_client->adapter; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 640 | const char *name = ""; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 641 | int address = new_client->addr; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 642 | u8 man_id, die_rev; |
| 643 | |
| 644 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 645 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 646 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 647 | /* verify chip: reg address should match i2c address */ |
| 648 | if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) |
| 649 | != address) { |
| 650 | dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n", |
| 651 | address); |
| 652 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 653 | } |
| 654 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 655 | /* check known chip manufacturer */ |
| 656 | man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID); |
| 657 | if (man_id == 0x23) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 658 | name = "adm9240"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 659 | } else if (man_id == 0xda) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 660 | name = "ds1780"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 661 | } else if (man_id == 0x01) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 662 | name = "lm81"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 663 | } else { |
| 664 | dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n", |
| 665 | man_id); |
| 666 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 667 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 668 | |
| 669 | /* successful detect, print chip info */ |
| 670 | die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV); |
| 671 | dev_info(&adapter->dev, "found %s revision %u\n", |
| 672 | man_id == 0x23 ? "ADM9240" : |
| 673 | man_id == 0xda ? "DS1780" : "LM81", die_rev); |
| 674 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 675 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 676 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 680 | static void adm9240_init_client(struct i2c_client *client) |
| 681 | { |
| 682 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 683 | u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG); |
| 684 | u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3; |
| 685 | |
| 686 | data->vrm = vid_which_vrm(); /* need this to report vid as mV */ |
| 687 | |
| 688 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, |
| 689 | data->vrm % 10); |
| 690 | |
| 691 | if (conf & 1) { /* measurement cycle running: report state */ |
| 692 | |
| 693 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", |
| 694 | conf, mode); |
| 695 | |
| 696 | } else { /* cold start: open limits before starting chip */ |
| 697 | int i; |
| 698 | |
| 699 | for (i = 0; i < 6; i++) { |
| 700 | i2c_smbus_write_byte_data(client, |
| 701 | ADM9240_REG_IN_MIN(i), 0); |
| 702 | i2c_smbus_write_byte_data(client, |
| 703 | ADM9240_REG_IN_MAX(i), 255); |
| 704 | } |
| 705 | i2c_smbus_write_byte_data(client, |
| 706 | ADM9240_REG_FAN_MIN(0), 255); |
| 707 | i2c_smbus_write_byte_data(client, |
| 708 | ADM9240_REG_FAN_MIN(1), 255); |
| 709 | i2c_smbus_write_byte_data(client, |
| 710 | ADM9240_REG_TEMP_MAX(0), 127); |
| 711 | i2c_smbus_write_byte_data(client, |
| 712 | ADM9240_REG_TEMP_MAX(1), 127); |
| 713 | |
| 714 | /* start measurement cycle */ |
| 715 | i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1); |
| 716 | |
| 717 | dev_info(&client->dev, |
| 718 | "cold start: config was 0x%02x mode %u\n", conf, mode); |
| 719 | } |
| 720 | } |
| 721 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 722 | static int adm9240_probe(struct i2c_client *new_client, |
| 723 | const struct i2c_device_id *id) |
| 724 | { |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 725 | struct device *dev = &new_client->dev; |
| 726 | struct device *hwmon_dev; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 727 | struct adm9240_data *data; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 728 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 729 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
Guenter Roeck | e5ebb77 | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 730 | if (!data) |
| 731 | return -ENOMEM; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 732 | |
| 733 | i2c_set_clientdata(new_client, data); |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 734 | data->client = new_client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 735 | mutex_init(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 736 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 737 | adm9240_init_client(new_client); |
| 738 | |
Axel Lin | 715f69b | 2014-07-03 22:01:48 +0800 | [diff] [blame] | 739 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 740 | new_client->name, |
| 741 | data, |
| 742 | adm9240_groups); |
| 743 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 744 | } |
| 745 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 746 | static const struct i2c_device_id adm9240_id[] = { |
| 747 | { "adm9240", adm9240 }, |
| 748 | { "ds1780", ds1780 }, |
| 749 | { "lm81", lm81 }, |
| 750 | { } |
| 751 | }; |
| 752 | MODULE_DEVICE_TABLE(i2c, adm9240_id); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 753 | |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 754 | static struct i2c_driver adm9240_driver = { |
| 755 | .class = I2C_CLASS_HWMON, |
| 756 | .driver = { |
| 757 | .name = "adm9240", |
| 758 | }, |
| 759 | .probe = adm9240_probe, |
Axel Lin | 4341fc3 | 2014-07-03 21:59:49 +0800 | [diff] [blame] | 760 | .id_table = adm9240_id, |
| 761 | .detect = adm9240_detect, |
| 762 | .address_list = normal_i2c, |
| 763 | }; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 764 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 765 | module_i2c_driver(adm9240_driver); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 766 | |
| 767 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 768 | "Grant Coady <gcoady.lk@gmail.com> and others"); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 769 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); |
| 770 | MODULE_LICENSE("GPL"); |