Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives |
| 3 | * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
| 5 | * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> |
| 6 | |
| 7 | * Derived from the lm83 driver by Jean Delvare |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/hwmon.h> |
| 19 | #include <linux/hwmon-sysfs.h> |
| 20 | #include <linux/err.h> |
| 21 | |
| 22 | /* Indexes for the sysfs hooks */ |
| 23 | |
| 24 | #define INPUT 0 |
| 25 | #define MIN 1 |
| 26 | #define MAX 2 |
| 27 | #define CONTROL 3 |
| 28 | #define OFFSET 3 |
| 29 | #define AUTOMIN 4 |
| 30 | #define THERM 5 |
| 31 | #define HYSTERSIS 6 |
| 32 | |
| 33 | /* These are unique identifiers for the sysfs functions - unlike the |
| 34 | numbers above, these are not also indexes into an array |
| 35 | */ |
| 36 | |
| 37 | #define ALARM 9 |
| 38 | #define FAULT 10 |
| 39 | |
| 40 | /* 7475 Common Registers */ |
| 41 | |
| 42 | #define REG_VOLTAGE_BASE 0x21 |
| 43 | #define REG_TEMP_BASE 0x25 |
| 44 | #define REG_TACH_BASE 0x28 |
| 45 | #define REG_PWM_BASE 0x30 |
| 46 | #define REG_PWM_MAX_BASE 0x38 |
| 47 | |
| 48 | #define REG_DEVID 0x3D |
| 49 | #define REG_VENDID 0x3E |
| 50 | |
| 51 | #define REG_STATUS1 0x41 |
| 52 | #define REG_STATUS2 0x42 |
| 53 | |
| 54 | #define REG_VOLTAGE_MIN_BASE 0x46 |
| 55 | #define REG_VOLTAGE_MAX_BASE 0x47 |
| 56 | |
| 57 | #define REG_TEMP_MIN_BASE 0x4E |
| 58 | #define REG_TEMP_MAX_BASE 0x4F |
| 59 | |
| 60 | #define REG_TACH_MIN_BASE 0x54 |
| 61 | |
| 62 | #define REG_PWM_CONFIG_BASE 0x5C |
| 63 | |
| 64 | #define REG_TEMP_TRANGE_BASE 0x5F |
| 65 | |
| 66 | #define REG_PWM_MIN_BASE 0x64 |
| 67 | |
| 68 | #define REG_TEMP_TMIN_BASE 0x67 |
| 69 | #define REG_TEMP_THERM_BASE 0x6A |
| 70 | |
| 71 | #define REG_REMOTE1_HYSTERSIS 0x6D |
| 72 | #define REG_REMOTE2_HYSTERSIS 0x6E |
| 73 | |
| 74 | #define REG_TEMP_OFFSET_BASE 0x70 |
| 75 | |
| 76 | #define REG_EXTEND1 0x76 |
| 77 | #define REG_EXTEND2 0x77 |
| 78 | #define REG_CONFIG5 0x7C |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame^] | 79 | #define REG_CONFIG4 0x7D |
| 80 | |
| 81 | #define CONFIG4_MAXDUTY 0x08 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 82 | |
| 83 | #define CONFIG5_TWOSCOMP 0x01 |
| 84 | #define CONFIG5_TEMPOFFSET 0x02 |
| 85 | |
| 86 | /* ADT7475 Settings */ |
| 87 | |
| 88 | #define ADT7475_VOLTAGE_COUNT 2 |
| 89 | #define ADT7475_TEMP_COUNT 3 |
| 90 | #define ADT7475_TACH_COUNT 4 |
| 91 | #define ADT7475_PWM_COUNT 3 |
| 92 | |
| 93 | /* Macro to read the registers */ |
| 94 | |
| 95 | #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) |
| 96 | |
| 97 | /* Macros to easily index the registers */ |
| 98 | |
| 99 | #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) |
| 100 | #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) |
| 101 | |
| 102 | #define PWM_REG(idx) (REG_PWM_BASE + (idx)) |
| 103 | #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) |
| 104 | #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) |
| 105 | #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) |
| 106 | |
| 107 | #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) |
| 108 | #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) |
| 109 | #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) |
| 110 | |
| 111 | #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) |
| 112 | #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) |
| 113 | #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) |
| 114 | #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) |
| 115 | #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) |
| 116 | #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) |
| 117 | #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) |
| 118 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 119 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 120 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 121 | I2C_CLIENT_INSMOD_2(adt7473, adt7475); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 122 | |
| 123 | static const struct i2c_device_id adt7475_id[] = { |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 124 | { "adt7473", adt7473 }, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 125 | { "adt7475", adt7475 }, |
| 126 | { } |
| 127 | }; |
| 128 | MODULE_DEVICE_TABLE(i2c, adt7475_id); |
| 129 | |
| 130 | struct adt7475_data { |
| 131 | struct device *hwmon_dev; |
| 132 | struct mutex lock; |
| 133 | |
| 134 | unsigned long measure_updated; |
| 135 | unsigned long limits_updated; |
| 136 | char valid; |
| 137 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame^] | 138 | u8 config4; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 139 | u8 config5; |
| 140 | u16 alarms; |
| 141 | u16 voltage[3][3]; |
| 142 | u16 temp[7][3]; |
| 143 | u16 tach[2][4]; |
| 144 | u8 pwm[4][3]; |
| 145 | u8 range[3]; |
| 146 | u8 pwmctl[3]; |
| 147 | u8 pwmchan[3]; |
| 148 | }; |
| 149 | |
| 150 | static struct i2c_driver adt7475_driver; |
| 151 | static struct adt7475_data *adt7475_update_device(struct device *dev); |
| 152 | static void adt7475_read_hystersis(struct i2c_client *client); |
| 153 | static void adt7475_read_pwm(struct i2c_client *client, int index); |
| 154 | |
| 155 | /* Given a temp value, convert it to register value */ |
| 156 | |
| 157 | static inline u16 temp2reg(struct adt7475_data *data, long val) |
| 158 | { |
| 159 | u16 ret; |
| 160 | |
| 161 | if (!(data->config5 & CONFIG5_TWOSCOMP)) { |
| 162 | val = SENSORS_LIMIT(val, -64000, 191000); |
| 163 | ret = (val + 64500) / 1000; |
| 164 | } else { |
| 165 | val = SENSORS_LIMIT(val, -128000, 127000); |
| 166 | if (val < -500) |
| 167 | ret = (256500 + val) / 1000; |
| 168 | else |
| 169 | ret = (val + 500) / 1000; |
| 170 | } |
| 171 | |
| 172 | return ret << 2; |
| 173 | } |
| 174 | |
| 175 | /* Given a register value, convert it to a real temp value */ |
| 176 | |
| 177 | static inline int reg2temp(struct adt7475_data *data, u16 reg) |
| 178 | { |
| 179 | if (data->config5 & CONFIG5_TWOSCOMP) { |
| 180 | if (reg >= 512) |
| 181 | return (reg - 1024) * 250; |
| 182 | else |
| 183 | return reg * 250; |
| 184 | } else |
| 185 | return (reg - 256) * 250; |
| 186 | } |
| 187 | |
| 188 | static inline int tach2rpm(u16 tach) |
| 189 | { |
| 190 | if (tach == 0 || tach == 0xFFFF) |
| 191 | return 0; |
| 192 | |
| 193 | return (90000 * 60) / tach; |
| 194 | } |
| 195 | |
| 196 | static inline u16 rpm2tach(unsigned long rpm) |
| 197 | { |
| 198 | if (rpm == 0) |
| 199 | return 0; |
| 200 | |
| 201 | return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF); |
| 202 | } |
| 203 | |
| 204 | static inline int reg2vcc(u16 reg) |
| 205 | { |
| 206 | return (4296 * reg) / 1000; |
| 207 | } |
| 208 | |
| 209 | static inline int reg2vccp(u16 reg) |
| 210 | { |
| 211 | return (2929 * reg) / 1000; |
| 212 | } |
| 213 | |
| 214 | static inline u16 vcc2reg(long vcc) |
| 215 | { |
| 216 | vcc = SENSORS_LIMIT(vcc, 0, 4396); |
| 217 | return (vcc * 1000) / 4296; |
| 218 | } |
| 219 | |
| 220 | static inline u16 vccp2reg(long vcc) |
| 221 | { |
| 222 | vcc = SENSORS_LIMIT(vcc, 0, 2998); |
| 223 | return (vcc * 1000) / 2929; |
| 224 | } |
| 225 | |
| 226 | static u16 adt7475_read_word(struct i2c_client *client, int reg) |
| 227 | { |
| 228 | u16 val; |
| 229 | |
| 230 | val = i2c_smbus_read_byte_data(client, reg); |
| 231 | val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8); |
| 232 | |
| 233 | return val; |
| 234 | } |
| 235 | |
| 236 | static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) |
| 237 | { |
| 238 | i2c_smbus_write_byte_data(client, reg + 1, val >> 8); |
| 239 | i2c_smbus_write_byte_data(client, reg, val & 0xFF); |
| 240 | } |
| 241 | |
| 242 | /* Find the nearest value in a table - used for pwm frequency and |
| 243 | auto temp range */ |
| 244 | static int find_nearest(long val, const int *array, int size) |
| 245 | { |
| 246 | int i; |
| 247 | |
| 248 | if (val < array[0]) |
| 249 | return 0; |
| 250 | |
| 251 | if (val > array[size - 1]) |
| 252 | return size - 1; |
| 253 | |
| 254 | for (i = 0; i < size - 1; i++) { |
| 255 | int a, b; |
| 256 | |
| 257 | if (val > array[i + 1]) |
| 258 | continue; |
| 259 | |
| 260 | a = val - array[i]; |
| 261 | b = array[i + 1] - val; |
| 262 | |
| 263 | return (a <= b) ? i : i + 1; |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, |
| 270 | char *buf) |
| 271 | { |
| 272 | struct adt7475_data *data = adt7475_update_device(dev); |
| 273 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 274 | unsigned short val; |
| 275 | |
| 276 | switch (sattr->nr) { |
| 277 | case ALARM: |
| 278 | return sprintf(buf, "%d\n", |
| 279 | (data->alarms >> (sattr->index + 1)) & 1); |
| 280 | default: |
| 281 | val = data->voltage[sattr->nr][sattr->index]; |
| 282 | return sprintf(buf, "%d\n", |
| 283 | sattr->index == |
| 284 | 0 ? reg2vccp(val) : reg2vcc(val)); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, |
| 289 | const char *buf, size_t count) |
| 290 | { |
| 291 | |
| 292 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 293 | struct i2c_client *client = to_i2c_client(dev); |
| 294 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 295 | unsigned char reg; |
| 296 | long val; |
| 297 | |
| 298 | if (strict_strtol(buf, 10, &val)) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | mutex_lock(&data->lock); |
| 302 | |
| 303 | data->voltage[sattr->nr][sattr->index] = |
| 304 | sattr->index ? vcc2reg(val) : vccp2reg(val); |
| 305 | |
| 306 | if (sattr->nr == MIN) |
| 307 | reg = VOLTAGE_MIN_REG(sattr->index); |
| 308 | else |
| 309 | reg = VOLTAGE_MAX_REG(sattr->index); |
| 310 | |
| 311 | i2c_smbus_write_byte_data(client, reg, |
| 312 | data->voltage[sattr->nr][sattr->index] >> 2); |
| 313 | mutex_unlock(&data->lock); |
| 314 | |
| 315 | return count; |
| 316 | } |
| 317 | |
| 318 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 319 | char *buf) |
| 320 | { |
| 321 | struct adt7475_data *data = adt7475_update_device(dev); |
| 322 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 323 | int out; |
| 324 | |
| 325 | switch (sattr->nr) { |
| 326 | case HYSTERSIS: |
| 327 | mutex_lock(&data->lock); |
| 328 | out = data->temp[sattr->nr][sattr->index]; |
| 329 | if (sattr->index != 1) |
| 330 | out = (out >> 4) & 0xF; |
| 331 | else |
| 332 | out = (out & 0xF); |
| 333 | /* Show the value as an absolute number tied to |
| 334 | * THERM */ |
| 335 | out = reg2temp(data, data->temp[THERM][sattr->index]) - |
| 336 | out * 1000; |
| 337 | mutex_unlock(&data->lock); |
| 338 | break; |
| 339 | |
| 340 | case OFFSET: |
| 341 | /* Offset is always 2's complement, regardless of the |
| 342 | * setting in CONFIG5 */ |
| 343 | mutex_lock(&data->lock); |
| 344 | out = (s8)data->temp[sattr->nr][sattr->index]; |
| 345 | if (data->config5 & CONFIG5_TEMPOFFSET) |
| 346 | out *= 1000; |
| 347 | else |
| 348 | out *= 500; |
| 349 | mutex_unlock(&data->lock); |
| 350 | break; |
| 351 | |
| 352 | case ALARM: |
| 353 | out = (data->alarms >> (sattr->index + 4)) & 1; |
| 354 | break; |
| 355 | |
| 356 | case FAULT: |
| 357 | /* Note - only for remote1 and remote2 */ |
Jean Delvare | cf312e0 | 2009-11-16 12:45:39 +0100 | [diff] [blame] | 358 | out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 359 | break; |
| 360 | |
| 361 | default: |
| 362 | /* All other temp values are in the configured format */ |
| 363 | out = reg2temp(data, data->temp[sattr->nr][sattr->index]); |
| 364 | } |
| 365 | |
| 366 | return sprintf(buf, "%d\n", out); |
| 367 | } |
| 368 | |
| 369 | static ssize_t set_temp(struct device *dev, struct device_attribute *attr, |
| 370 | const char *buf, size_t count) |
| 371 | { |
| 372 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 373 | struct i2c_client *client = to_i2c_client(dev); |
| 374 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 375 | unsigned char reg = 0; |
| 376 | u8 out; |
| 377 | int temp; |
| 378 | long val; |
| 379 | |
| 380 | if (strict_strtol(buf, 10, &val)) |
| 381 | return -EINVAL; |
| 382 | |
| 383 | mutex_lock(&data->lock); |
| 384 | |
| 385 | /* We need the config register in all cases for temp <-> reg conv. */ |
| 386 | data->config5 = adt7475_read(REG_CONFIG5); |
| 387 | |
| 388 | switch (sattr->nr) { |
| 389 | case OFFSET: |
| 390 | if (data->config5 & CONFIG5_TEMPOFFSET) { |
| 391 | val = SENSORS_LIMIT(val, -63000, 127000); |
| 392 | out = data->temp[OFFSET][sattr->index] = val / 1000; |
| 393 | } else { |
| 394 | val = SENSORS_LIMIT(val, -63000, 64000); |
| 395 | out = data->temp[OFFSET][sattr->index] = val / 500; |
| 396 | } |
| 397 | break; |
| 398 | |
| 399 | case HYSTERSIS: |
| 400 | /* The value will be given as an absolute value, turn it |
| 401 | into an offset based on THERM */ |
| 402 | |
| 403 | /* Read fresh THERM and HYSTERSIS values from the chip */ |
| 404 | data->temp[THERM][sattr->index] = |
| 405 | adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; |
| 406 | adt7475_read_hystersis(client); |
| 407 | |
| 408 | temp = reg2temp(data, data->temp[THERM][sattr->index]); |
| 409 | val = SENSORS_LIMIT(val, temp - 15000, temp); |
| 410 | val = (temp - val) / 1000; |
| 411 | |
| 412 | if (sattr->index != 1) { |
| 413 | data->temp[HYSTERSIS][sattr->index] &= 0xF0; |
| 414 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; |
| 415 | } else { |
| 416 | data->temp[HYSTERSIS][sattr->index] &= 0x0F; |
| 417 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); |
| 418 | } |
| 419 | |
| 420 | out = data->temp[HYSTERSIS][sattr->index]; |
| 421 | break; |
| 422 | |
| 423 | default: |
| 424 | data->temp[sattr->nr][sattr->index] = temp2reg(data, val); |
| 425 | |
| 426 | /* We maintain an extra 2 digits of precision for simplicity |
| 427 | * - shift those back off before writing the value */ |
| 428 | out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); |
| 429 | } |
| 430 | |
| 431 | switch (sattr->nr) { |
| 432 | case MIN: |
| 433 | reg = TEMP_MIN_REG(sattr->index); |
| 434 | break; |
| 435 | case MAX: |
| 436 | reg = TEMP_MAX_REG(sattr->index); |
| 437 | break; |
| 438 | case OFFSET: |
| 439 | reg = TEMP_OFFSET_REG(sattr->index); |
| 440 | break; |
| 441 | case AUTOMIN: |
| 442 | reg = TEMP_TMIN_REG(sattr->index); |
| 443 | break; |
| 444 | case THERM: |
| 445 | reg = TEMP_THERM_REG(sattr->index); |
| 446 | break; |
| 447 | case HYSTERSIS: |
| 448 | if (sattr->index != 2) |
| 449 | reg = REG_REMOTE1_HYSTERSIS; |
| 450 | else |
| 451 | reg = REG_REMOTE2_HYSTERSIS; |
| 452 | |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | i2c_smbus_write_byte_data(client, reg, out); |
| 457 | |
| 458 | mutex_unlock(&data->lock); |
| 459 | return count; |
| 460 | } |
| 461 | |
| 462 | /* Table of autorange values - the user will write the value in millidegrees, |
| 463 | and we'll convert it */ |
| 464 | static const int autorange_table[] = { |
| 465 | 2000, 2500, 3330, 4000, 5000, 6670, 8000, |
| 466 | 10000, 13330, 16000, 20000, 26670, 32000, 40000, |
| 467 | 53330, 80000 |
| 468 | }; |
| 469 | |
| 470 | static ssize_t show_point2(struct device *dev, struct device_attribute *attr, |
| 471 | char *buf) |
| 472 | { |
| 473 | struct adt7475_data *data = adt7475_update_device(dev); |
| 474 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 475 | int out, val; |
| 476 | |
| 477 | mutex_lock(&data->lock); |
| 478 | out = (data->range[sattr->index] >> 4) & 0x0F; |
| 479 | val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
| 480 | mutex_unlock(&data->lock); |
| 481 | |
| 482 | return sprintf(buf, "%d\n", val + autorange_table[out]); |
| 483 | } |
| 484 | |
| 485 | static ssize_t set_point2(struct device *dev, struct device_attribute *attr, |
| 486 | const char *buf, size_t count) |
| 487 | { |
| 488 | struct i2c_client *client = to_i2c_client(dev); |
| 489 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 490 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 491 | int temp; |
| 492 | long val; |
| 493 | |
| 494 | if (strict_strtol(buf, 10, &val)) |
| 495 | return -EINVAL; |
| 496 | |
| 497 | mutex_lock(&data->lock); |
| 498 | |
| 499 | /* Get a fresh copy of the needed registers */ |
| 500 | data->config5 = adt7475_read(REG_CONFIG5); |
| 501 | data->temp[AUTOMIN][sattr->index] = |
| 502 | adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; |
| 503 | data->range[sattr->index] = |
| 504 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 505 | |
| 506 | /* The user will write an absolute value, so subtract the start point |
| 507 | to figure the range */ |
| 508 | temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
| 509 | val = SENSORS_LIMIT(val, temp + autorange_table[0], |
| 510 | temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); |
| 511 | val -= temp; |
| 512 | |
| 513 | /* Find the nearest table entry to what the user wrote */ |
| 514 | val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table)); |
| 515 | |
| 516 | data->range[sattr->index] &= ~0xF0; |
| 517 | data->range[sattr->index] |= val << 4; |
| 518 | |
| 519 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 520 | data->range[sattr->index]); |
| 521 | |
| 522 | mutex_unlock(&data->lock); |
| 523 | return count; |
| 524 | } |
| 525 | |
| 526 | static ssize_t show_tach(struct device *dev, struct device_attribute *attr, |
| 527 | char *buf) |
| 528 | { |
| 529 | struct adt7475_data *data = adt7475_update_device(dev); |
| 530 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 531 | int out; |
| 532 | |
| 533 | if (sattr->nr == ALARM) |
| 534 | out = (data->alarms >> (sattr->index + 10)) & 1; |
| 535 | else |
| 536 | out = tach2rpm(data->tach[sattr->nr][sattr->index]); |
| 537 | |
| 538 | return sprintf(buf, "%d\n", out); |
| 539 | } |
| 540 | |
| 541 | static ssize_t set_tach(struct device *dev, struct device_attribute *attr, |
| 542 | const char *buf, size_t count) |
| 543 | { |
| 544 | |
| 545 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 546 | struct i2c_client *client = to_i2c_client(dev); |
| 547 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 548 | unsigned long val; |
| 549 | |
| 550 | if (strict_strtoul(buf, 10, &val)) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | mutex_lock(&data->lock); |
| 554 | |
| 555 | data->tach[MIN][sattr->index] = rpm2tach(val); |
| 556 | |
| 557 | adt7475_write_word(client, TACH_MIN_REG(sattr->index), |
| 558 | data->tach[MIN][sattr->index]); |
| 559 | |
| 560 | mutex_unlock(&data->lock); |
| 561 | return count; |
| 562 | } |
| 563 | |
| 564 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 565 | char *buf) |
| 566 | { |
| 567 | struct adt7475_data *data = adt7475_update_device(dev); |
| 568 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 569 | |
| 570 | return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); |
| 571 | } |
| 572 | |
| 573 | static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, |
| 574 | char *buf) |
| 575 | { |
| 576 | struct adt7475_data *data = adt7475_update_device(dev); |
| 577 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 578 | |
| 579 | return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); |
| 580 | } |
| 581 | |
| 582 | static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, |
| 583 | char *buf) |
| 584 | { |
| 585 | struct adt7475_data *data = adt7475_update_device(dev); |
| 586 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 587 | |
| 588 | return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); |
| 589 | } |
| 590 | |
| 591 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 592 | const char *buf, size_t count) |
| 593 | { |
| 594 | |
| 595 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 596 | struct i2c_client *client = to_i2c_client(dev); |
| 597 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 598 | unsigned char reg = 0; |
| 599 | long val; |
| 600 | |
| 601 | if (strict_strtol(buf, 10, &val)) |
| 602 | return -EINVAL; |
| 603 | |
| 604 | mutex_lock(&data->lock); |
| 605 | |
| 606 | switch (sattr->nr) { |
| 607 | case INPUT: |
| 608 | /* Get a fresh value for CONTROL */ |
| 609 | data->pwm[CONTROL][sattr->index] = |
| 610 | adt7475_read(PWM_CONFIG_REG(sattr->index)); |
| 611 | |
| 612 | /* If we are not in manual mode, then we shouldn't allow |
| 613 | * the user to set the pwm speed */ |
| 614 | if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { |
| 615 | mutex_unlock(&data->lock); |
| 616 | return count; |
| 617 | } |
| 618 | |
| 619 | reg = PWM_REG(sattr->index); |
| 620 | break; |
| 621 | |
| 622 | case MIN: |
| 623 | reg = PWM_MIN_REG(sattr->index); |
| 624 | break; |
| 625 | |
| 626 | case MAX: |
| 627 | reg = PWM_MAX_REG(sattr->index); |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF); |
| 632 | i2c_smbus_write_byte_data(client, reg, |
| 633 | data->pwm[sattr->nr][sattr->index]); |
| 634 | |
| 635 | mutex_unlock(&data->lock); |
| 636 | |
| 637 | return count; |
| 638 | } |
| 639 | |
| 640 | /* Called by set_pwmctrl and set_pwmchan */ |
| 641 | |
| 642 | static int hw_set_pwm(struct i2c_client *client, int index, |
| 643 | unsigned int pwmctl, unsigned int pwmchan) |
| 644 | { |
| 645 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 646 | long val = 0; |
| 647 | |
| 648 | switch (pwmctl) { |
| 649 | case 0: |
| 650 | val = 0x03; /* Run at full speed */ |
| 651 | break; |
| 652 | case 1: |
| 653 | val = 0x07; /* Manual mode */ |
| 654 | break; |
| 655 | case 2: |
| 656 | switch (pwmchan) { |
| 657 | case 1: |
| 658 | /* Remote1 controls PWM */ |
| 659 | val = 0x00; |
| 660 | break; |
| 661 | case 2: |
| 662 | /* local controls PWM */ |
| 663 | val = 0x01; |
| 664 | break; |
| 665 | case 4: |
| 666 | /* remote2 controls PWM */ |
| 667 | val = 0x02; |
| 668 | break; |
| 669 | case 6: |
| 670 | /* local/remote2 control PWM */ |
| 671 | val = 0x05; |
| 672 | break; |
| 673 | case 7: |
| 674 | /* All three control PWM */ |
| 675 | val = 0x06; |
| 676 | break; |
| 677 | default: |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | break; |
| 681 | default: |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | data->pwmctl[index] = pwmctl; |
| 686 | data->pwmchan[index] = pwmchan; |
| 687 | |
| 688 | data->pwm[CONTROL][index] &= ~0xE0; |
| 689 | data->pwm[CONTROL][index] |= (val & 7) << 5; |
| 690 | |
| 691 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 692 | data->pwm[CONTROL][index]); |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, |
| 698 | const char *buf, size_t count) |
| 699 | { |
| 700 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 701 | struct i2c_client *client = to_i2c_client(dev); |
| 702 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 703 | int r; |
| 704 | long val; |
| 705 | |
| 706 | if (strict_strtol(buf, 10, &val)) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | mutex_lock(&data->lock); |
| 710 | /* Read Modify Write PWM values */ |
| 711 | adt7475_read_pwm(client, sattr->index); |
| 712 | r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); |
| 713 | if (r) |
| 714 | count = r; |
| 715 | mutex_unlock(&data->lock); |
| 716 | |
| 717 | return count; |
| 718 | } |
| 719 | |
| 720 | static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, |
| 721 | const char *buf, size_t count) |
| 722 | { |
| 723 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 724 | struct i2c_client *client = to_i2c_client(dev); |
| 725 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 726 | int r; |
| 727 | long val; |
| 728 | |
| 729 | if (strict_strtol(buf, 10, &val)) |
| 730 | return -EINVAL; |
| 731 | |
| 732 | mutex_lock(&data->lock); |
| 733 | /* Read Modify Write PWM values */ |
| 734 | adt7475_read_pwm(client, sattr->index); |
| 735 | r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); |
| 736 | if (r) |
| 737 | count = r; |
| 738 | mutex_unlock(&data->lock); |
| 739 | |
| 740 | return count; |
| 741 | } |
| 742 | |
| 743 | /* List of frequencies for the PWM */ |
| 744 | static const int pwmfreq_table[] = { |
| 745 | 11, 14, 22, 29, 35, 44, 58, 88 |
| 746 | }; |
| 747 | |
| 748 | static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, |
| 749 | char *buf) |
| 750 | { |
| 751 | struct adt7475_data *data = adt7475_update_device(dev); |
| 752 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 753 | |
| 754 | return sprintf(buf, "%d\n", |
| 755 | pwmfreq_table[data->range[sattr->index] & 7]); |
| 756 | } |
| 757 | |
| 758 | static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, |
| 759 | const char *buf, size_t count) |
| 760 | { |
| 761 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 762 | struct i2c_client *client = to_i2c_client(dev); |
| 763 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 764 | int out; |
| 765 | long val; |
| 766 | |
| 767 | if (strict_strtol(buf, 10, &val)) |
| 768 | return -EINVAL; |
| 769 | |
| 770 | out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); |
| 771 | |
| 772 | mutex_lock(&data->lock); |
| 773 | |
| 774 | data->range[sattr->index] = |
| 775 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 776 | data->range[sattr->index] &= ~7; |
| 777 | data->range[sattr->index] |= out; |
| 778 | |
| 779 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 780 | data->range[sattr->index]); |
| 781 | |
| 782 | mutex_unlock(&data->lock); |
| 783 | return count; |
| 784 | } |
| 785 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame^] | 786 | static ssize_t show_pwm_at_crit(struct device *dev, |
| 787 | struct device_attribute *devattr, char *buf) |
| 788 | { |
| 789 | struct adt7475_data *data = adt7475_update_device(dev); |
| 790 | return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY)); |
| 791 | } |
| 792 | |
| 793 | static ssize_t set_pwm_at_crit(struct device *dev, |
| 794 | struct device_attribute *devattr, |
| 795 | const char *buf, size_t count) |
| 796 | { |
| 797 | struct i2c_client *client = to_i2c_client(dev); |
| 798 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 799 | long val; |
| 800 | |
| 801 | if (strict_strtol(buf, 10, &val)) |
| 802 | return -EINVAL; |
| 803 | if (val != 0 && val != 1) |
| 804 | return -EINVAL; |
| 805 | |
| 806 | mutex_lock(&data->lock); |
| 807 | data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4); |
| 808 | if (val) |
| 809 | data->config4 |= CONFIG4_MAXDUTY; |
| 810 | else |
| 811 | data->config4 &= ~CONFIG4_MAXDUTY; |
| 812 | i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4); |
| 813 | mutex_unlock(&data->lock); |
| 814 | |
| 815 | return count; |
| 816 | } |
| 817 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 818 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 0); |
| 819 | static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, |
| 820 | set_voltage, MAX, 0); |
| 821 | static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, |
| 822 | set_voltage, MIN, 0); |
| 823 | static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); |
| 824 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 1); |
| 825 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, |
| 826 | set_voltage, MAX, 1); |
| 827 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, |
| 828 | set_voltage, MIN, 1); |
| 829 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); |
| 830 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); |
| 831 | static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); |
| 832 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); |
| 833 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 834 | MAX, 0); |
| 835 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 836 | MIN, 0); |
| 837 | static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, |
| 838 | set_temp, OFFSET, 0); |
| 839 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 840 | show_temp, set_temp, AUTOMIN, 0); |
| 841 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 842 | show_point2, set_point2, 0, 0); |
| 843 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 844 | THERM, 0); |
| 845 | static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 846 | set_temp, HYSTERSIS, 0); |
| 847 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); |
| 848 | static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); |
| 849 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 850 | MAX, 1); |
| 851 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 852 | MIN, 1); |
| 853 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, |
| 854 | set_temp, OFFSET, 1); |
| 855 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 856 | show_temp, set_temp, AUTOMIN, 1); |
| 857 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 858 | show_point2, set_point2, 0, 1); |
| 859 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 860 | THERM, 1); |
| 861 | static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 862 | set_temp, HYSTERSIS, 1); |
| 863 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); |
| 864 | static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); |
| 865 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); |
| 866 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 867 | MAX, 2); |
| 868 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 869 | MIN, 2); |
| 870 | static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, |
| 871 | set_temp, OFFSET, 2); |
| 872 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 873 | show_temp, set_temp, AUTOMIN, 2); |
| 874 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 875 | show_point2, set_point2, 0, 2); |
| 876 | static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 877 | THERM, 2); |
| 878 | static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 879 | set_temp, HYSTERSIS, 2); |
| 880 | static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); |
| 881 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 882 | MIN, 0); |
| 883 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); |
| 884 | static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); |
| 885 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 886 | MIN, 1); |
| 887 | static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); |
| 888 | static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); |
| 889 | static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 890 | MIN, 2); |
| 891 | static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); |
| 892 | static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); |
| 893 | static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 894 | MIN, 3); |
| 895 | static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); |
| 896 | static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 897 | 0); |
| 898 | static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 899 | set_pwmfreq, INPUT, 0); |
| 900 | static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 901 | set_pwmctrl, INPUT, 0); |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 902 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 903 | show_pwmchan, set_pwmchan, INPUT, 0); |
| 904 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 905 | set_pwm, MIN, 0); |
| 906 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 907 | set_pwm, MAX, 0); |
| 908 | static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 909 | 1); |
| 910 | static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 911 | set_pwmfreq, INPUT, 1); |
| 912 | static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 913 | set_pwmctrl, INPUT, 1); |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 914 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 915 | show_pwmchan, set_pwmchan, INPUT, 1); |
| 916 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 917 | set_pwm, MIN, 1); |
| 918 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 919 | set_pwm, MAX, 1); |
| 920 | static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 921 | 2); |
| 922 | static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 923 | set_pwmfreq, INPUT, 2); |
| 924 | static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 925 | set_pwmctrl, INPUT, 2); |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 926 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 927 | show_pwmchan, set_pwmchan, INPUT, 2); |
| 928 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 929 | set_pwm, MIN, 2); |
| 930 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 931 | set_pwm, MAX, 2); |
| 932 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame^] | 933 | /* Non-standard name, might need revisiting */ |
| 934 | static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO, |
| 935 | show_pwm_at_crit, set_pwm_at_crit); |
| 936 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 937 | static struct attribute *adt7475_attrs[] = { |
| 938 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 939 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 940 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 941 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 942 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 943 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 944 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 945 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 946 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 947 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 948 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 949 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 950 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 951 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 952 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, |
| 953 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, |
| 954 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 955 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 956 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 957 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 958 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 959 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 960 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 961 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, |
| 962 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, |
| 963 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 964 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 965 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 966 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 967 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 968 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 969 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 970 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
| 971 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, |
| 972 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, |
| 973 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 974 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 975 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 976 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 977 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 978 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 979 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 980 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 981 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 982 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 983 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 984 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 985 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 986 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
| 987 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 988 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 989 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 990 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 991 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 992 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
| 993 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 994 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 995 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 996 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 997 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 998 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
| 999 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1000 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
| 1001 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1002 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1003 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1004 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame^] | 1005 | &dev_attr_pwm_use_point2_pwm_at_crit.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1006 | NULL, |
| 1007 | }; |
| 1008 | |
| 1009 | struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; |
| 1010 | |
| 1011 | static int adt7475_detect(struct i2c_client *client, int kind, |
| 1012 | struct i2c_board_info *info) |
| 1013 | { |
| 1014 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1015 | int vendid, devid; |
| 1016 | const char *name; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1017 | |
| 1018 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 1019 | return -ENODEV; |
| 1020 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1021 | vendid = adt7475_read(REG_VENDID); |
| 1022 | devid = adt7475_read(REG_DEVID); |
| 1023 | |
| 1024 | if (vendid == 0x41 && devid == 0x73) |
| 1025 | name = "adt7473"; |
| 1026 | else if (vendid == 0x41 && devid == 0x75 && client->addr == 0x2e) |
| 1027 | name = "adt7475"; |
| 1028 | else { |
| 1029 | dev_dbg(&adapter->dev, |
| 1030 | "Couldn't detect an ADT7473 or ADT7475 part at " |
| 1031 | "0x%02x\n", (unsigned int)client->addr); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1032 | return -ENODEV; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1035 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | static int adt7475_probe(struct i2c_client *client, |
| 1041 | const struct i2c_device_id *id) |
| 1042 | { |
| 1043 | struct adt7475_data *data; |
| 1044 | int i, ret = 0; |
| 1045 | |
| 1046 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1047 | if (data == NULL) |
| 1048 | return -ENOMEM; |
| 1049 | |
| 1050 | mutex_init(&data->lock); |
| 1051 | i2c_set_clientdata(client, data); |
| 1052 | |
| 1053 | /* Call adt7475_read_pwm for all pwm's as this will reprogram any |
| 1054 | pwm's which are disabled to manual mode with 0% duty cycle */ |
| 1055 | for (i = 0; i < ADT7475_PWM_COUNT; i++) |
| 1056 | adt7475_read_pwm(client, i); |
| 1057 | |
| 1058 | ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); |
| 1059 | if (ret) |
| 1060 | goto efree; |
| 1061 | |
| 1062 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 1063 | if (IS_ERR(data->hwmon_dev)) { |
| 1064 | ret = PTR_ERR(data->hwmon_dev); |
| 1065 | goto eremove; |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | |
| 1070 | eremove: |
| 1071 | sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); |
| 1072 | efree: |
| 1073 | kfree(data); |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
| 1077 | static int adt7475_remove(struct i2c_client *client) |
| 1078 | { |
| 1079 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1080 | |
| 1081 | hwmon_device_unregister(data->hwmon_dev); |
| 1082 | sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); |
| 1083 | kfree(data); |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static struct i2c_driver adt7475_driver = { |
| 1089 | .class = I2C_CLASS_HWMON, |
| 1090 | .driver = { |
| 1091 | .name = "adt7475", |
| 1092 | }, |
| 1093 | .probe = adt7475_probe, |
| 1094 | .remove = adt7475_remove, |
| 1095 | .id_table = adt7475_id, |
| 1096 | .detect = adt7475_detect, |
| 1097 | .address_data = &addr_data, |
| 1098 | }; |
| 1099 | |
| 1100 | static void adt7475_read_hystersis(struct i2c_client *client) |
| 1101 | { |
| 1102 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1103 | |
| 1104 | data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); |
| 1105 | data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; |
| 1106 | data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); |
| 1107 | } |
| 1108 | |
| 1109 | static void adt7475_read_pwm(struct i2c_client *client, int index) |
| 1110 | { |
| 1111 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1112 | unsigned int v; |
| 1113 | |
| 1114 | data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); |
| 1115 | |
| 1116 | /* Figure out the internal value for pwmctrl and pwmchan |
| 1117 | based on the current settings */ |
| 1118 | v = (data->pwm[CONTROL][index] >> 5) & 7; |
| 1119 | |
| 1120 | if (v == 3) |
| 1121 | data->pwmctl[index] = 0; |
| 1122 | else if (v == 7) |
| 1123 | data->pwmctl[index] = 1; |
| 1124 | else if (v == 4) { |
| 1125 | /* The fan is disabled - we don't want to |
| 1126 | support that, so change to manual mode and |
| 1127 | set the duty cycle to 0 instead |
| 1128 | */ |
| 1129 | data->pwm[INPUT][index] = 0; |
| 1130 | data->pwm[CONTROL][index] &= ~0xE0; |
| 1131 | data->pwm[CONTROL][index] |= (7 << 5); |
| 1132 | |
| 1133 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1134 | data->pwm[INPUT][index]); |
| 1135 | |
| 1136 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1137 | data->pwm[CONTROL][index]); |
| 1138 | |
| 1139 | data->pwmctl[index] = 1; |
| 1140 | } else { |
| 1141 | data->pwmctl[index] = 2; |
| 1142 | |
| 1143 | switch (v) { |
| 1144 | case 0: |
| 1145 | data->pwmchan[index] = 1; |
| 1146 | break; |
| 1147 | case 1: |
| 1148 | data->pwmchan[index] = 2; |
| 1149 | break; |
| 1150 | case 2: |
| 1151 | data->pwmchan[index] = 4; |
| 1152 | break; |
| 1153 | case 5: |
| 1154 | data->pwmchan[index] = 6; |
| 1155 | break; |
| 1156 | case 6: |
| 1157 | data->pwmchan[index] = 7; |
| 1158 | break; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | static struct adt7475_data *adt7475_update_device(struct device *dev) |
| 1164 | { |
| 1165 | struct i2c_client *client = to_i2c_client(dev); |
| 1166 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1167 | u8 ext; |
| 1168 | int i; |
| 1169 | |
| 1170 | mutex_lock(&data->lock); |
| 1171 | |
| 1172 | /* Measurement values update every 2 seconds */ |
| 1173 | if (time_after(jiffies, data->measure_updated + HZ * 2) || |
| 1174 | !data->valid) { |
| 1175 | data->alarms = adt7475_read(REG_STATUS2) << 8; |
| 1176 | data->alarms |= adt7475_read(REG_STATUS1); |
| 1177 | |
| 1178 | ext = adt7475_read(REG_EXTEND1); |
| 1179 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) |
| 1180 | data->voltage[INPUT][i] = |
| 1181 | (adt7475_read(VOLTAGE_REG(i)) << 2) | |
| 1182 | ((ext >> ((i + 1) * 2)) & 3); |
| 1183 | |
| 1184 | ext = adt7475_read(REG_EXTEND2); |
| 1185 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) |
| 1186 | data->temp[INPUT][i] = |
| 1187 | (adt7475_read(TEMP_REG(i)) << 2) | |
| 1188 | ((ext >> ((i + 1) * 2)) & 3); |
| 1189 | |
| 1190 | for (i = 0; i < ADT7475_TACH_COUNT; i++) |
| 1191 | data->tach[INPUT][i] = |
| 1192 | adt7475_read_word(client, TACH_REG(i)); |
| 1193 | |
| 1194 | /* Updated by hw when in auto mode */ |
| 1195 | for (i = 0; i < ADT7475_PWM_COUNT; i++) |
| 1196 | data->pwm[INPUT][i] = adt7475_read(PWM_REG(i)); |
| 1197 | |
| 1198 | data->measure_updated = jiffies; |
| 1199 | } |
| 1200 | |
| 1201 | /* Limits and settings, should never change update every 60 seconds */ |
Jean Delvare | 56e35ee | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1202 | if (time_after(jiffies, data->limits_updated + HZ * 60) || |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1203 | !data->valid) { |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame^] | 1204 | data->config4 = adt7475_read(REG_CONFIG4); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1205 | data->config5 = adt7475_read(REG_CONFIG5); |
| 1206 | |
| 1207 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
| 1208 | /* Adjust values so they match the input precision */ |
| 1209 | data->voltage[MIN][i] = |
| 1210 | adt7475_read(VOLTAGE_MIN_REG(i)) << 2; |
| 1211 | data->voltage[MAX][i] = |
| 1212 | adt7475_read(VOLTAGE_MAX_REG(i)) << 2; |
| 1213 | } |
| 1214 | |
| 1215 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) { |
| 1216 | /* Adjust values so they match the input precision */ |
| 1217 | data->temp[MIN][i] = |
| 1218 | adt7475_read(TEMP_MIN_REG(i)) << 2; |
| 1219 | data->temp[MAX][i] = |
| 1220 | adt7475_read(TEMP_MAX_REG(i)) << 2; |
| 1221 | data->temp[AUTOMIN][i] = |
| 1222 | adt7475_read(TEMP_TMIN_REG(i)) << 2; |
| 1223 | data->temp[THERM][i] = |
| 1224 | adt7475_read(TEMP_THERM_REG(i)) << 2; |
| 1225 | data->temp[OFFSET][i] = |
| 1226 | adt7475_read(TEMP_OFFSET_REG(i)); |
| 1227 | } |
| 1228 | adt7475_read_hystersis(client); |
| 1229 | |
| 1230 | for (i = 0; i < ADT7475_TACH_COUNT; i++) |
| 1231 | data->tach[MIN][i] = |
| 1232 | adt7475_read_word(client, TACH_MIN_REG(i)); |
| 1233 | |
| 1234 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
| 1235 | data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i)); |
| 1236 | data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i)); |
| 1237 | /* Set the channel and control information */ |
| 1238 | adt7475_read_pwm(client, i); |
| 1239 | } |
| 1240 | |
| 1241 | data->range[0] = adt7475_read(TEMP_TRANGE_REG(0)); |
| 1242 | data->range[1] = adt7475_read(TEMP_TRANGE_REG(1)); |
| 1243 | data->range[2] = adt7475_read(TEMP_TRANGE_REG(2)); |
| 1244 | |
| 1245 | data->limits_updated = jiffies; |
| 1246 | data->valid = 1; |
| 1247 | } |
| 1248 | |
| 1249 | mutex_unlock(&data->lock); |
| 1250 | |
| 1251 | return data; |
| 1252 | } |
| 1253 | |
| 1254 | static int __init sensors_adt7475_init(void) |
| 1255 | { |
| 1256 | return i2c_add_driver(&adt7475_driver); |
| 1257 | } |
| 1258 | |
| 1259 | static void __exit sensors_adt7475_exit(void) |
| 1260 | { |
| 1261 | i2c_del_driver(&adt7475_driver); |
| 1262 | } |
| 1263 | |
| 1264 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); |
| 1265 | MODULE_DESCRIPTION("adt7475 driver"); |
| 1266 | MODULE_LICENSE("GPL"); |
| 1267 | |
| 1268 | module_init(sensors_adt7475_init); |
| 1269 | module_exit(sensors_adt7475_exit); |