diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/hwmon/adt7461.c | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'drivers/hwmon/adt7461.c')
| -rw-r--r-- | drivers/hwmon/adt7461.c | 823 |
1 files changed, 823 insertions, 0 deletions
diff --git a/drivers/hwmon/adt7461.c b/drivers/hwmon/adt7461.c new file mode 100644 index 00000000000..10ea2eb8066 --- /dev/null +++ b/drivers/hwmon/adt7461.c | |||
| @@ -0,0 +1,823 @@ | |||
| 1 | /* | ||
| 2 | * adt7461.c - Linux kernel modules for hardware | ||
| 3 | * monitoring | ||
| 4 | * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <linux/module.h> | ||
| 22 | #include <linux/init.h> | ||
| 23 | #include <linux/slab.h> | ||
| 24 | #include <linux/jiffies.h> | ||
| 25 | #include <linux/regulator/consumer.h> | ||
| 26 | #include <linux/i2c.h> | ||
| 27 | #include <linux/hwmon-sysfs.h> | ||
| 28 | #include <linux/hwmon.h> | ||
| 29 | #include <linux/err.h> | ||
| 30 | #include <linux/mutex.h> | ||
| 31 | #include <linux/sysfs.h> | ||
| 32 | #include <linux/delay.h> | ||
| 33 | #include <linux/gpio.h> | ||
| 34 | #include <linux/interrupt.h> | ||
| 35 | #include <linux/adt7461.h> | ||
| 36 | |||
| 37 | #define DRIVER_NAME "adt7461" | ||
| 38 | |||
| 39 | /* | ||
| 40 | * The ADT7461 registers | ||
| 41 | */ | ||
| 42 | |||
| 43 | #define ADT7461_REG_R_MAN_ID 0xFE | ||
| 44 | #define ADT7461_REG_R_CHIP_ID 0xFF | ||
| 45 | #define ADT7461_REG_R_CONFIG1 0x03 | ||
| 46 | #define ADT7461_REG_W_CONFIG1 0x09 | ||
| 47 | #define ADT7461_REG_R_CONVRATE 0x04 | ||
| 48 | #define ADT7461_REG_W_CONVRATE 0x0A | ||
| 49 | #define ADT7461_REG_R_STATUS 0x02 | ||
| 50 | #define ADT7461_REG_R_LOCAL_TEMP 0x00 | ||
| 51 | #define ADT7461_REG_R_LOCAL_HIGH 0x05 | ||
| 52 | #define ADT7461_REG_W_LOCAL_HIGH 0x0B | ||
| 53 | #define ADT7461_REG_R_LOCAL_LOW 0x06 | ||
| 54 | #define ADT7461_REG_W_LOCAL_LOW 0x0C | ||
| 55 | #define ADT7461_REG_R_LOCAL_CRIT 0x20 | ||
| 56 | #define ADT7461_REG_W_LOCAL_CRIT 0x20 | ||
| 57 | #define ADT7461_REG_R_REMOTE_TEMPH 0x01 | ||
| 58 | #define ADT7461_REG_R_REMOTE_TEMPL 0x10 | ||
| 59 | #define ADT7461_REG_R_REMOTE_OFFSH 0x11 | ||
| 60 | #define ADT7461_REG_W_REMOTE_OFFSH 0x11 | ||
| 61 | #define ADT7461_REG_R_REMOTE_OFFSL 0x12 | ||
| 62 | #define ADT7461_REG_W_REMOTE_OFFSL 0x12 | ||
| 63 | #define ADT7461_REG_R_REMOTE_HIGHH 0x07 | ||
| 64 | #define ADT7461_REG_W_REMOTE_HIGHH 0x0D | ||
| 65 | #define ADT7461_REG_R_REMOTE_HIGHL 0x13 | ||
| 66 | #define ADT7461_REG_W_REMOTE_HIGHL 0x13 | ||
| 67 | #define ADT7461_REG_R_REMOTE_LOWH 0x08 | ||
| 68 | #define ADT7461_REG_W_REMOTE_LOWH 0x0E | ||
| 69 | #define ADT7461_REG_R_REMOTE_LOWL 0x14 | ||
| 70 | #define ADT7461_REG_W_REMOTE_LOWL 0x14 | ||
| 71 | #define ADT7461_REG_R_REMOTE_CRIT 0x19 | ||
| 72 | #define ADT7461_REG_W_REMOTE_CRIT 0x19 | ||
| 73 | #define ADT7461_REG_R_TCRIT_HYST 0x21 | ||
| 74 | #define ADT7461_REG_W_TCRIT_HYST 0x21 | ||
| 75 | |||
| 76 | /* Configuration Register Bits */ | ||
| 77 | #define EXTENDED_RANGE_BIT BIT(2) | ||
| 78 | #define THERM2_BIT BIT(5) | ||
| 79 | #define STANDBY_BIT BIT(6) | ||
| 80 | #define ALERT_BIT BIT(7) | ||
| 81 | |||
| 82 | /* Max Temperature Measurements */ | ||
| 83 | #define EXTENDED_RANGE_OFFSET 64U | ||
| 84 | #define STANDARD_RANGE_MAX 127U | ||
| 85 | #define EXTENDED_RANGE_MAX (150U + EXTENDED_RANGE_OFFSET) | ||
| 86 | |||
| 87 | /* | ||
| 88 | * Device flags | ||
| 89 | */ | ||
| 90 | #define ADT7461_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */ | ||
| 91 | #define ADT7461_FLAG_THERM2 0x02 /* Pin 6 as Therm2 */ | ||
| 92 | |||
| 93 | /* | ||
| 94 | * Client data | ||
| 95 | */ | ||
| 96 | |||
| 97 | struct adt7461_data { | ||
| 98 | struct work_struct work; | ||
| 99 | struct i2c_client *client; | ||
| 100 | struct device *hwmon_dev; | ||
| 101 | struct mutex update_lock; | ||
| 102 | struct regulator *regulator; | ||
| 103 | char valid; /* zero until following fields are valid */ | ||
| 104 | unsigned long last_updated; /* in jiffies */ | ||
| 105 | int flags; | ||
| 106 | |||
| 107 | u8 config; /* configuration register value */ | ||
| 108 | u8 alert_alarms; /* Which alarm bits trigger ALERT# */ | ||
| 109 | |||
| 110 | /* registers values */ | ||
| 111 | s8 temp8[4]; /* 0: local low limit | ||
| 112 | 1: local high limit | ||
| 113 | 2: local critical limit | ||
| 114 | 3: remote critical limit */ | ||
| 115 | s16 temp11[5]; /* 0: remote input | ||
| 116 | 1: remote low limit | ||
| 117 | 2: remote high limit | ||
| 118 | 3: remote offset | ||
| 119 | 4: local input */ | ||
| 120 | u8 temp_hyst; | ||
| 121 | u8 alarms; /* bitvector */ | ||
| 122 | void (*alarm_fn)(bool raised); | ||
| 123 | int irq_gpio; | ||
| 124 | }; | ||
| 125 | |||
| 126 | /* | ||
| 127 | * Conversions | ||
| 128 | */ | ||
| 129 | |||
| 130 | static inline int temp_from_s8(s8 val) | ||
| 131 | { | ||
| 132 | return val * 1000; | ||
| 133 | } | ||
| 134 | |||
| 135 | static u8 hyst_to_reg(long val) | ||
| 136 | { | ||
| 137 | if (val <= 0) | ||
| 138 | return 0; | ||
| 139 | if (val >= 30500) | ||
| 140 | return 31; | ||
| 141 | return (val + 500) / 1000; | ||
| 142 | } | ||
| 143 | |||
| 144 | /* | ||
| 145 | * ADT7461 attempts to write values that are outside the range | ||
| 146 | * 0 < temp < 127 are treated as the boundary value. | ||
| 147 | * | ||
| 148 | * ADT7461 in "extended mode" operation uses unsigned integers offset by | ||
| 149 | * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC. | ||
| 150 | */ | ||
| 151 | static inline int temp_from_u8(struct adt7461_data *data, u8 val) | ||
| 152 | { | ||
| 153 | if (data->flags & ADT7461_FLAG_ADT7461_EXT) | ||
| 154 | return (val - 64) * 1000; | ||
| 155 | else | ||
| 156 | return temp_from_s8(val); | ||
| 157 | } | ||
| 158 | |||
| 159 | static inline int temp_from_u16(struct adt7461_data *data, u16 val) | ||
| 160 | { | ||
| 161 | if (data->flags & ADT7461_FLAG_ADT7461_EXT) | ||
| 162 | return (val - 0x4000) / 64 * 250; | ||
| 163 | else | ||
| 164 | return val / 32 * 125; | ||
| 165 | } | ||
| 166 | |||
| 167 | static u8 temp_to_u8(struct adt7461_data *data, long val) | ||
| 168 | { | ||
| 169 | if (data->flags & ADT7461_FLAG_ADT7461_EXT) { | ||
| 170 | if (val <= -64000) | ||
| 171 | return 0; | ||
| 172 | if (val >= 191000) | ||
| 173 | return 0xFF; | ||
| 174 | return (val + 500 + 64000) / 1000; | ||
| 175 | } else { | ||
| 176 | if (val <= 0) | ||
| 177 | return 0; | ||
| 178 | if (val >= 127000) | ||
| 179 | return 127; | ||
| 180 | return (val + 500) / 1000; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | static u16 temp_to_u16(struct adt7461_data *data, long val) | ||
| 185 | { | ||
| 186 | if (data->flags & ADT7461_FLAG_ADT7461_EXT) { | ||
| 187 | if (val <= -64000) | ||
| 188 | return 0; | ||
| 189 | if (val >= 191750) | ||
| 190 | return 0xFFC0; | ||
| 191 | return (val + 64000 + 125) / 250 * 64; | ||
| 192 | } else { | ||
| 193 | if (val <= 0) | ||
| 194 | return 0; | ||
| 195 | if (val >= 127750) | ||
| 196 | return 0x7FC0; | ||
| 197 | return (val + 125) / 250 * 64; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | static int adt7461_read_reg(struct i2c_client* client, u8 reg, u8 *value) | ||
| 202 | { | ||
| 203 | int err; | ||
| 204 | |||
| 205 | err = i2c_smbus_read_byte_data(client, reg); | ||
| 206 | if (err < 0) { | ||
| 207 | pr_err("adt7461_read_reg:Register %#02x read failed (%d)\n", | ||
| 208 | reg, err); | ||
| 209 | return err; | ||
| 210 | } | ||
| 211 | *value = err; | ||
| 212 | |||
| 213 | return 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | static int adt7461_read16(struct i2c_client *client, u8 regh, u8 regl, | ||
| 217 | u16 *value) | ||
| 218 | { | ||
| 219 | int err; | ||
| 220 | u8 oldh, newh, l; | ||
| 221 | |||
| 222 | /* | ||
| 223 | * There is a trick here. We have to read two registers to have the | ||
| 224 | * sensor temperature, but we have to beware a conversion could occur | ||
| 225 | * inbetween the readings. The datasheet says we should either use | ||
| 226 | * the one-shot conversion register, which we don't want to do | ||
| 227 | * (disables hardware monitoring) or monitor the busy bit, which is | ||
| 228 | * impossible (we can't read the values and monitor that bit at the | ||
| 229 | * exact same time). So the solution used here is to read the high | ||
| 230 | * byte once, then the low byte, then the high byte again. If the new | ||
| 231 | * high byte matches the old one, then we have a valid reading. Else | ||
| 232 | * we have to read the low byte again, and now we believe we have a | ||
| 233 | * correct reading. | ||
| 234 | */ | ||
| 235 | if ((err = adt7461_read_reg(client, regh, &oldh)) | ||
| 236 | || (err = adt7461_read_reg(client, regl, &l)) | ||
| 237 | || (err = adt7461_read_reg(client, regh, &newh))) | ||
| 238 | return err; | ||
| 239 | if (oldh != newh) { | ||
| 240 | err = adt7461_read_reg(client, regl, &l); | ||
| 241 | if (err) | ||
| 242 | return err; | ||
| 243 | } | ||
| 244 | *value = (newh << 8) | l; | ||
| 245 | |||
| 246 | return 0; | ||
| 247 | } | ||
| 248 | |||
| 249 | static struct adt7461_data *adt7461_update_device(struct device *dev) | ||
| 250 | { | ||
| 251 | struct i2c_client *client = to_i2c_client(dev); | ||
| 252 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 253 | |||
| 254 | mutex_lock(&data->update_lock); | ||
| 255 | |||
| 256 | if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10) | ||
| 257 | || !data->valid) { | ||
| 258 | u8 h, l; | ||
| 259 | |||
| 260 | adt7461_read_reg(client, ADT7461_REG_R_LOCAL_LOW, &data->temp8[0]); | ||
| 261 | adt7461_read_reg(client, ADT7461_REG_R_LOCAL_HIGH, &data->temp8[1]); | ||
| 262 | adt7461_read_reg(client, ADT7461_REG_R_LOCAL_CRIT, &data->temp8[2]); | ||
| 263 | adt7461_read_reg(client, ADT7461_REG_R_REMOTE_CRIT, &data->temp8[3]); | ||
| 264 | adt7461_read_reg(client, ADT7461_REG_R_TCRIT_HYST, &data->temp_hyst); | ||
| 265 | |||
| 266 | if (adt7461_read_reg(client, ADT7461_REG_R_LOCAL_TEMP, &h) == 0) | ||
| 267 | data->temp11[4] = h << 8; | ||
| 268 | |||
| 269 | adt7461_read16(client, ADT7461_REG_R_REMOTE_TEMPH, | ||
| 270 | ADT7461_REG_R_REMOTE_TEMPL, &data->temp11[0]); | ||
| 271 | |||
| 272 | if (adt7461_read_reg(client, ADT7461_REG_R_REMOTE_LOWH, &h) == 0) { | ||
| 273 | data->temp11[1] = h << 8; | ||
| 274 | if (adt7461_read_reg(client, ADT7461_REG_R_REMOTE_LOWL, &l) == 0) | ||
| 275 | data->temp11[1] |= l; | ||
| 276 | } | ||
| 277 | if (adt7461_read_reg(client, ADT7461_REG_R_REMOTE_HIGHH, &h) == 0) { | ||
| 278 | data->temp11[2] = h << 8; | ||
| 279 | if (adt7461_read_reg(client, ADT7461_REG_R_REMOTE_HIGHL, &l) == 0) | ||
| 280 | data->temp11[2] |= l; | ||
| 281 | } | ||
| 282 | |||
| 283 | if (adt7461_read_reg(client, ADT7461_REG_R_REMOTE_OFFSH, | ||
| 284 | &h) == 0 | ||
| 285 | && adt7461_read_reg(client, ADT7461_REG_R_REMOTE_OFFSL, | ||
| 286 | &l) == 0) | ||
| 287 | data->temp11[3] = (h << 8) | l; | ||
| 288 | adt7461_read_reg(client, ADT7461_REG_R_STATUS, &data->alarms); | ||
| 289 | |||
| 290 | /* Re-enable ALERT# output if relevant alarms are all clear */ | ||
| 291 | if (!(data->flags & ADT7461_FLAG_THERM2) | ||
| 292 | && (data->alarms & data->alert_alarms) == 0) { | ||
| 293 | u8 config; | ||
| 294 | |||
| 295 | adt7461_read_reg(client, ADT7461_REG_R_CONFIG1, &config); | ||
| 296 | if (config & 0x80) { | ||
| 297 | pr_err("adt7461_update_device:Re-enabling ALERT#\n"); | ||
| 298 | i2c_smbus_write_byte_data(client, | ||
| 299 | ADT7461_REG_W_CONFIG1, | ||
| 300 | config & ~ALERT_BIT); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | data->last_updated = jiffies; | ||
| 305 | data->valid = 1; | ||
| 306 | } | ||
| 307 | |||
| 308 | mutex_unlock(&data->update_lock); | ||
| 309 | |||
| 310 | return data; | ||
| 311 | } | ||
| 312 | |||
| 313 | /* | ||
| 314 | * Sysfs stuff | ||
| 315 | */ | ||
| 316 | |||
| 317 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, | ||
| 318 | char *buf) | ||
| 319 | { | ||
| 320 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 321 | struct adt7461_data *data = adt7461_update_device(dev); | ||
| 322 | int temp; | ||
| 323 | |||
| 324 | temp = temp_from_u8(data, data->temp8[attr->index]); | ||
| 325 | |||
| 326 | return sprintf(buf, "%d\n", temp); | ||
| 327 | } | ||
| 328 | |||
| 329 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, | ||
| 330 | const char *buf, size_t count) | ||
| 331 | { | ||
| 332 | static const u8 reg[4] = { | ||
| 333 | ADT7461_REG_W_LOCAL_LOW, | ||
| 334 | ADT7461_REG_W_LOCAL_HIGH, | ||
| 335 | ADT7461_REG_W_LOCAL_CRIT, | ||
| 336 | ADT7461_REG_W_REMOTE_CRIT, | ||
| 337 | }; | ||
| 338 | |||
| 339 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 340 | struct i2c_client *client = to_i2c_client(dev); | ||
| 341 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 342 | long val = simple_strtol(buf, NULL, 10); | ||
| 343 | int nr = attr->index; | ||
| 344 | |||
| 345 | mutex_lock(&data->update_lock); | ||
| 346 | data->temp8[nr] = temp_to_u8(data, val); | ||
| 347 | i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); | ||
| 348 | mutex_unlock(&data->update_lock); | ||
| 349 | return count; | ||
| 350 | } | ||
| 351 | |||
| 352 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | ||
| 353 | char *buf) | ||
| 354 | { | ||
| 355 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 356 | struct adt7461_data *data = adt7461_update_device(dev); | ||
| 357 | int temp; | ||
| 358 | |||
| 359 | temp = temp_from_u16(data, data->temp11[attr->index]); | ||
| 360 | |||
| 361 | return sprintf(buf, "%d\n", temp); | ||
| 362 | } | ||
| 363 | |||
| 364 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | ||
| 365 | const char *buf, size_t count) | ||
| 366 | { | ||
| 367 | static const u8 reg[6] = { | ||
| 368 | ADT7461_REG_W_REMOTE_LOWH, | ||
| 369 | ADT7461_REG_W_REMOTE_LOWL, | ||
| 370 | ADT7461_REG_W_REMOTE_HIGHH, | ||
| 371 | ADT7461_REG_W_REMOTE_HIGHL, | ||
| 372 | ADT7461_REG_W_REMOTE_OFFSH, | ||
| 373 | ADT7461_REG_W_REMOTE_OFFSL, | ||
| 374 | }; | ||
| 375 | |||
| 376 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 377 | struct i2c_client *client = to_i2c_client(dev); | ||
| 378 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 379 | long val = simple_strtol(buf, NULL, 10); | ||
| 380 | int nr = attr->index; | ||
| 381 | |||
| 382 | mutex_lock(&data->update_lock); | ||
| 383 | data->temp11[nr] = temp_to_u16(data, val); | ||
| 384 | |||
| 385 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], | ||
| 386 | data->temp11[nr] >> 8); | ||
| 387 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], | ||
| 388 | data->temp11[nr] & 0xff); | ||
| 389 | mutex_unlock(&data->update_lock); | ||
| 390 | return count; | ||
| 391 | } | ||
| 392 | |||
| 393 | static ssize_t show_temphyst(struct device *dev, | ||
| 394 | struct device_attribute *devattr, | ||
| 395 | char *buf) | ||
| 396 | { | ||
| 397 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 398 | struct adt7461_data *data = adt7461_update_device(dev); | ||
| 399 | int temp; | ||
| 400 | |||
| 401 | temp = temp_from_u8(data, data->temp8[attr->index]); | ||
| 402 | |||
| 403 | return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst)); | ||
| 404 | } | ||
| 405 | |||
| 406 | static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy, | ||
| 407 | const char *buf, size_t count) | ||
| 408 | { | ||
| 409 | struct i2c_client *client = to_i2c_client(dev); | ||
| 410 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 411 | long val = simple_strtol(buf, NULL, 10); | ||
| 412 | int temp; | ||
| 413 | |||
| 414 | mutex_lock(&data->update_lock); | ||
| 415 | temp = temp_from_u8(data, data->temp8[2]); | ||
| 416 | data->temp_hyst = hyst_to_reg(temp - val); | ||
| 417 | i2c_smbus_write_byte_data(client, ADT7461_REG_W_TCRIT_HYST, | ||
| 418 | data->temp_hyst); | ||
| 419 | mutex_unlock(&data->update_lock); | ||
| 420 | return count; | ||
| 421 | } | ||
| 422 | |||
| 423 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, | ||
| 424 | char *buf) | ||
| 425 | { | ||
| 426 | struct adt7461_data *data = adt7461_update_device(dev); | ||
| 427 | return sprintf(buf, "%d\n", data->alarms); | ||
| 428 | } | ||
| 429 | |||
| 430 | static ssize_t show_alarm(struct device *dev, struct device_attribute | ||
| 431 | *devattr, char *buf) | ||
| 432 | { | ||
| 433 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 434 | struct adt7461_data *data = adt7461_update_device(dev); | ||
| 435 | int bitnr = attr->index; | ||
| 436 | |||
| 437 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); | ||
| 438 | } | ||
| 439 | |||
| 440 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4); | ||
| 441 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); | ||
| 442 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, | ||
| 443 | set_temp8, 0); | ||
| 444 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | ||
| 445 | set_temp11, 1); | ||
| 446 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, | ||
| 447 | set_temp8, 1); | ||
| 448 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | ||
| 449 | set_temp11, 2); | ||
| 450 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, | ||
| 451 | set_temp8, 2); | ||
| 452 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, | ||
| 453 | set_temp8, 3); | ||
| 454 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst, | ||
| 455 | set_temphyst, 2); | ||
| 456 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); | ||
| 457 | static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, | ||
| 458 | set_temp11, 3); | ||
| 459 | |||
| 460 | /* Individual alarm files */ | ||
| 461 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); | ||
| 462 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | ||
| 463 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | ||
| 464 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | ||
| 465 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | ||
| 466 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); | ||
| 467 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | ||
| 468 | /* Raw alarm file for compatibility */ | ||
| 469 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | ||
| 470 | |||
| 471 | static struct attribute *adt7461_attributes[] = { | ||
| 472 | &sensor_dev_attr_temp1_input.dev_attr.attr, | ||
| 473 | &sensor_dev_attr_temp2_input.dev_attr.attr, | ||
| 474 | &sensor_dev_attr_temp1_min.dev_attr.attr, | ||
| 475 | &sensor_dev_attr_temp2_min.dev_attr.attr, | ||
| 476 | &sensor_dev_attr_temp1_max.dev_attr.attr, | ||
| 477 | &sensor_dev_attr_temp2_max.dev_attr.attr, | ||
| 478 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | ||
| 479 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | ||
| 480 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | ||
| 481 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | ||
| 482 | |||
| 483 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | ||
| 484 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | ||
| 485 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | ||
| 486 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | ||
| 487 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | ||
| 488 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | ||
| 489 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | ||
| 490 | &dev_attr_alarms.attr, | ||
| 491 | NULL | ||
| 492 | }; | ||
| 493 | |||
| 494 | static const struct attribute_group adt7461_group = { | ||
| 495 | .attrs = adt7461_attributes, | ||
| 496 | }; | ||
| 497 | |||
| 498 | static void adt7461_work_func(struct work_struct *work) | ||
| 499 | { | ||
| 500 | struct adt7461_data *data = | ||
| 501 | container_of(work, struct adt7461_data, work); | ||
| 502 | |||
| 503 | if (data->alarm_fn) { | ||
| 504 | /* Therm2 line is active low */ | ||
| 505 | data->alarm_fn(!gpio_get_value(data->irq_gpio)); | ||
| 506 | } | ||
| 507 | } | ||
| 508 | |||
| 509 | static irqreturn_t adt7461_irq(int irq, void *dev_id) | ||
| 510 | { | ||
| 511 | struct adt7461_data *data = dev_id; | ||
| 512 | schedule_work(&data->work); | ||
| 513 | |||
| 514 | return IRQ_HANDLED; | ||
| 515 | } | ||
| 516 | |||
| 517 | static void adt7461_regulator_enable(struct i2c_client *client) | ||
| 518 | { | ||
| 519 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 520 | |||
| 521 | data->regulator = regulator_get(NULL, "vdd_vcore_temp"); | ||
| 522 | if (IS_ERR_OR_NULL(data->regulator)) { | ||
| 523 | pr_err("adt7461_regulator_enable:Couldn't get regulator vdd_vcore_temp\n"); | ||
| 524 | data->regulator = NULL; | ||
| 525 | } else { | ||
| 526 | regulator_enable(data->regulator); | ||
| 527 | /* Optimal time to get the regulator turned on | ||
| 528 | * before initializing adt7461 chip*/ | ||
| 529 | mdelay(5); | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | static void adt7461_regulator_disable(struct i2c_client *client) | ||
| 534 | { | ||
| 535 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 536 | struct regulator *adt7461_reg = data->regulator; | ||
| 537 | int ret; | ||
| 538 | |||
| 539 | if (adt7461_reg) { | ||
| 540 | ret = regulator_is_enabled(adt7461_reg); | ||
| 541 | if (ret > 0) | ||
| 542 | regulator_disable(adt7461_reg); | ||
| 543 | regulator_put(adt7461_reg); | ||
| 544 | } | ||
| 545 | data->regulator = NULL; | ||
| 546 | } | ||
| 547 | |||
| 548 | static void adt7461_enable(struct i2c_client *client) | ||
| 549 | { | ||
| 550 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 551 | |||
| 552 | i2c_smbus_write_byte_data(client, ADT7461_REG_W_CONFIG1, | ||
| 553 | data->config & ~STANDBY_BIT); | ||
| 554 | } | ||
| 555 | |||
| 556 | static void adt7461_disable(struct i2c_client *client) | ||
| 557 | { | ||
| 558 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 559 | |||
| 560 | i2c_smbus_write_byte_data(client, ADT7461_REG_W_CONFIG1, | ||
| 561 | data->config | STANDBY_BIT); | ||
| 562 | } | ||
| 563 | |||
| 564 | static int adt7461_init_client(struct i2c_client *client) | ||
| 565 | { | ||
| 566 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 567 | struct adt7461_platform_data *pdata = client->dev.platform_data; | ||
| 568 | u8 config = 0; | ||
| 569 | u8 value; | ||
| 570 | int err; | ||
| 571 | |||
| 572 | if (!pdata || !pdata->supported_hwrev) | ||
| 573 | return -ENODEV; | ||
| 574 | |||
| 575 | data->irq_gpio = -1; | ||
| 576 | |||
| 577 | if (pdata->therm2) { | ||
| 578 | data->flags |= ADT7461_FLAG_THERM2; | ||
| 579 | |||
| 580 | if (gpio_is_valid(pdata->irq_gpio)) { | ||
| 581 | if (!IS_ERR(gpio_request(pdata->irq_gpio, "adt7461"))) { | ||
| 582 | gpio_direction_input(pdata->irq_gpio); | ||
| 583 | data->irq_gpio = pdata->irq_gpio; | ||
| 584 | } | ||
| 585 | } | ||
| 586 | } | ||
| 587 | |||
| 588 | if (pdata->ext_range) | ||
| 589 | data->flags |= ADT7461_FLAG_ADT7461_EXT; | ||
| 590 | |||
| 591 | adt7461_regulator_enable(client); | ||
| 592 | |||
| 593 | /* Start the conversions. */ | ||
| 594 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_CONVRATE, | ||
| 595 | pdata->conv_rate); | ||
| 596 | if (err < 0) | ||
| 597 | goto error; | ||
| 598 | |||
| 599 | /* External temperature h/w shutdown limit */ | ||
| 600 | value = temp_to_u8(data, pdata->shutdown_ext_limit * 1000); | ||
| 601 | err = i2c_smbus_write_byte_data(client, | ||
| 602 | ADT7461_REG_W_REMOTE_CRIT, value); | ||
| 603 | if (err < 0) | ||
| 604 | goto error; | ||
| 605 | |||
| 606 | /* Local temperature h/w shutdown limit */ | ||
| 607 | value = temp_to_u8(data, pdata->shutdown_local_limit * 1000); | ||
| 608 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_LOCAL_CRIT, | ||
| 609 | value); | ||
| 610 | if (err < 0) | ||
| 611 | goto error; | ||
| 612 | |||
| 613 | /* External Temperature Throttling limit */ | ||
| 614 | value = temp_to_u8(data, pdata->throttling_ext_limit * 1000); | ||
| 615 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_REMOTE_HIGHH, | ||
| 616 | value); | ||
| 617 | if (err < 0) | ||
| 618 | goto error; | ||
| 619 | |||
| 620 | /* Local Temperature Throttling limit */ | ||
| 621 | value = (data->flags & ADT7461_FLAG_ADT7461_EXT) ? | ||
| 622 | EXTENDED_RANGE_MAX : STANDARD_RANGE_MAX; | ||
| 623 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_LOCAL_HIGH, | ||
| 624 | value); | ||
| 625 | if (err < 0) | ||
| 626 | goto error; | ||
| 627 | |||
| 628 | /* Remote channel offset */ | ||
| 629 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_REMOTE_OFFSH, | ||
| 630 | pdata->offset); | ||
| 631 | if (err < 0) | ||
| 632 | goto error; | ||
| 633 | |||
| 634 | /* THERM hysteresis */ | ||
| 635 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_TCRIT_HYST, | ||
| 636 | pdata->hysteresis); | ||
| 637 | if (err < 0) | ||
| 638 | goto error; | ||
| 639 | |||
| 640 | if (data->flags & ADT7461_FLAG_THERM2) { | ||
| 641 | data->alarm_fn = pdata->alarm_fn; | ||
| 642 | config = (THERM2_BIT | STANDBY_BIT); | ||
| 643 | } else { | ||
| 644 | config = (~ALERT_BIT & ~THERM2_BIT & STANDBY_BIT); | ||
| 645 | } | ||
| 646 | |||
| 647 | err = i2c_smbus_write_byte_data(client, ADT7461_REG_W_CONFIG1, config); | ||
| 648 | if (err < 0) | ||
| 649 | goto error; | ||
| 650 | |||
| 651 | data->config = config; | ||
| 652 | return 0; | ||
| 653 | |||
| 654 | error: | ||
| 655 | pr_err("adt7461_init_client:Initialization failed!\n"); | ||
| 656 | return err; | ||
| 657 | } | ||
| 658 | |||
| 659 | static int adt7461_init_irq(struct adt7461_data *data) | ||
| 660 | { | ||
| 661 | INIT_WORK(&data->work, adt7461_work_func); | ||
| 662 | |||
| 663 | return request_irq(data->client->irq, adt7461_irq, IRQF_TRIGGER_RISING | | ||
| 664 | IRQF_TRIGGER_FALLING, DRIVER_NAME, data); | ||
| 665 | } | ||
| 666 | |||
| 667 | static int adt7461_probe(struct i2c_client *new_client, | ||
| 668 | const struct i2c_device_id *id) | ||
| 669 | { | ||
| 670 | struct adt7461_data *data; | ||
| 671 | int err; | ||
| 672 | |||
| 673 | data = kzalloc(sizeof(struct adt7461_data), GFP_KERNEL); | ||
| 674 | if (!data) | ||
| 675 | return -ENOMEM; | ||
| 676 | |||
| 677 | data->client = new_client; | ||
| 678 | i2c_set_clientdata(new_client, data); | ||
| 679 | mutex_init(&data->update_lock); | ||
| 680 | |||
| 681 | data->alert_alarms = 0x7c; | ||
| 682 | |||
| 683 | /* Initialize the ADT7461 chip */ | ||
| 684 | err = adt7461_init_client(new_client); | ||
| 685 | if (err < 0) | ||
| 686 | goto exit_free; | ||
| 687 | |||
| 688 | if (data->flags & ADT7461_FLAG_THERM2) { | ||
| 689 | err = adt7461_init_irq(data); | ||
| 690 | if (err < 0) | ||
| 691 | goto exit_free; | ||
| 692 | } | ||
| 693 | |||
| 694 | /* Register sysfs hooks */ | ||
| 695 | if ((err = sysfs_create_group(&new_client->dev.kobj, &adt7461_group))) | ||
| 696 | goto exit_free; | ||
| 697 | if ((err = device_create_file(&new_client->dev, | ||
| 698 | &sensor_dev_attr_temp2_offset.dev_attr))) | ||
| 699 | goto exit_remove_files; | ||
| 700 | |||
| 701 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | ||
| 702 | if (IS_ERR(data->hwmon_dev)) { | ||
| 703 | err = PTR_ERR(data->hwmon_dev); | ||
| 704 | goto exit_remove_files; | ||
| 705 | } | ||
| 706 | |||
| 707 | adt7461_enable(new_client); | ||
| 708 | return 0; | ||
| 709 | |||
| 710 | exit_remove_files: | ||
| 711 | sysfs_remove_group(&new_client->dev.kobj, &adt7461_group); | ||
| 712 | exit_free: | ||
| 713 | kfree(data); | ||
| 714 | return err; | ||
| 715 | } | ||
| 716 | |||
| 717 | static int adt7461_remove(struct i2c_client *client) | ||
| 718 | { | ||
| 719 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 720 | |||
| 721 | if (data->flags & ADT7461_FLAG_THERM2) { | ||
| 722 | free_irq(client->irq, data); | ||
| 723 | cancel_work_sync(&data->work); | ||
| 724 | } | ||
| 725 | if (gpio_is_valid(data->irq_gpio)) | ||
| 726 | gpio_free(data->irq_gpio); | ||
| 727 | hwmon_device_unregister(data->hwmon_dev); | ||
| 728 | sysfs_remove_group(&client->dev.kobj, &adt7461_group); | ||
| 729 | device_remove_file(&client->dev, | ||
| 730 | &sensor_dev_attr_temp2_offset.dev_attr); | ||
| 731 | adt7461_regulator_disable(client); | ||
| 732 | |||
| 733 | kfree(data); | ||
| 734 | return 0; | ||
| 735 | } | ||
| 736 | |||
| 737 | static void adt7461_alert(struct i2c_client *client, unsigned int flag) | ||
| 738 | { | ||
| 739 | struct adt7461_data *data = i2c_get_clientdata(client); | ||
| 740 | u8 config, alarms; | ||
| 741 | |||
| 742 | adt7461_read_reg(client, ADT7461_REG_R_STATUS, &alarms); | ||
| 743 | if ((alarms & 0x7f) == 0) { | ||
| 744 | pr_err("adt7461_alert:Everything OK\n"); | ||
| 745 | } else { | ||
| 746 | if (alarms & 0x61) | ||
| 747 | pr_err("adt7461_alert:temp%d out of range, please check!\n", 1); | ||
| 748 | if (alarms & 0x1a) | ||
| 749 | pr_err("adt7461_alert:temp%d out of range, please check!\n", 2); | ||
| 750 | if (alarms & 0x04) | ||
| 751 | pr_err("adt7461_alert:temp%d diode open, please check!\n", 2); | ||
| 752 | |||
| 753 | /* Disable ALERT# output, because these chips don't implement | ||
| 754 | SMBus alert correctly; they should only hold the alert line | ||
| 755 | low briefly. */ | ||
| 756 | if (!(data->flags & ADT7461_FLAG_THERM2) | ||
| 757 | && (alarms & data->alert_alarms)) { | ||
| 758 | pr_err("adt7461_alert:Disabling ALERT#\n"); | ||
| 759 | adt7461_read_reg(client, ADT7461_REG_R_CONFIG1, &config); | ||
| 760 | i2c_smbus_write_byte_data(client, ADT7461_REG_W_CONFIG1, | ||
| 761 | config | ALERT_BIT); | ||
| 762 | } | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | #ifdef CONFIG_PM | ||
| 767 | static int adt7461_suspend(struct i2c_client *client, pm_message_t state) | ||
| 768 | { | ||
| 769 | disable_irq(client->irq); | ||
| 770 | adt7461_disable(client); | ||
| 771 | |||
| 772 | return 0; | ||
| 773 | } | ||
| 774 | |||
| 775 | static int adt7461_resume(struct i2c_client *client) | ||
| 776 | { | ||
| 777 | adt7461_enable(client); | ||
| 778 | enable_irq(client->irq); | ||
| 779 | |||
| 780 | return 0; | ||
| 781 | } | ||
| 782 | #endif | ||
| 783 | |||
| 784 | /* | ||
| 785 | * Driver data | ||
| 786 | */ | ||
| 787 | static const struct i2c_device_id adt7461_id[] = { | ||
| 788 | { DRIVER_NAME, 0 }, | ||
| 789 | }; | ||
| 790 | |||
| 791 | MODULE_DEVICE_TABLE(i2c, adt7461_id); | ||
| 792 | |||
| 793 | static struct i2c_driver adt7461_driver = { | ||
| 794 | .class = I2C_CLASS_HWMON, | ||
| 795 | .driver = { | ||
| 796 | .name = DRIVER_NAME, | ||
| 797 | }, | ||
| 798 | .probe = adt7461_probe, | ||
| 799 | .remove = adt7461_remove, | ||
| 800 | .alert = adt7461_alert, | ||
| 801 | .id_table = adt7461_id, | ||
| 802 | #ifdef CONFIG_PM | ||
| 803 | .suspend = adt7461_suspend, | ||
| 804 | .resume = adt7461_resume, | ||
| 805 | #endif | ||
| 806 | }; | ||
| 807 | |||
| 808 | static int __init sensors_adt7461_init(void) | ||
| 809 | { | ||
| 810 | return i2c_add_driver(&adt7461_driver); | ||
| 811 | } | ||
| 812 | |||
| 813 | static void __exit sensors_adt7461_exit(void) | ||
| 814 | { | ||
| 815 | i2c_del_driver(&adt7461_driver); | ||
| 816 | } | ||
| 817 | |||
| 818 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | ||
| 819 | MODULE_DESCRIPTION("ADT7461 driver"); | ||
| 820 | MODULE_LICENSE("GPL"); | ||
| 821 | |||
| 822 | module_init(sensors_adt7461_init); | ||
| 823 | module_exit(sensors_adt7461_exit); | ||
