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