Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | w83781d.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring |
| 4 | Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 5 | Philip Edelbrock <phil@netroedge.com>, |
| 6 | and Mark Studebaker <mdsxyz123@yahoo.com> |
| 7 | Copyright (c) 2007 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | Supports following chips: |
| 26 | |
| 27 | Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA |
| 28 | as99127f 7 3 0 3 0x31 0x12c3 yes no |
| 29 | as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no |
| 30 | w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes |
| 31 | w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes |
| 33 | w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | */ |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/jiffies.h> |
| 41 | #include <linux/i2c.h> |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 42 | #include <linux/platform_device.h> |
| 43 | #include <linux/ioport.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 44 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 45 | #include <linux/hwmon-vid.h> |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 46 | #include <linux/hwmon-sysfs.h> |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 47 | #include <linux/sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 48 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 49 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <asm/io.h> |
| 51 | #include "lm75.h" |
| 52 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 53 | /* ISA device, if found */ |
| 54 | static struct platform_device *pdev; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /* Addresses to scan */ |
| 57 | static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, |
| 58 | 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, |
| 59 | 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END }; |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 60 | static unsigned short isa_address = 0x290; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 63 | I2C_CLIENT_INSMOD_5(w83781d, w83782d, w83783s, w83627hf, as99127f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: " |
| 65 | "{bus, clientaddr, subclientaddr1, subclientaddr2}"); |
| 66 | |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 67 | static int reset; |
| 68 | module_param(reset, bool, 0); |
| 69 | MODULE_PARM_DESC(reset, "Set to one to reset chip on load"); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | static int init = 1; |
| 72 | module_param(init, bool, 0); |
| 73 | MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization"); |
| 74 | |
| 75 | /* Constants specified below */ |
| 76 | |
| 77 | /* Length of ISA address segment */ |
| 78 | #define W83781D_EXTENT 8 |
| 79 | |
| 80 | /* Where are the ISA address/data registers relative to the base address */ |
| 81 | #define W83781D_ADDR_REG_OFFSET 5 |
| 82 | #define W83781D_DATA_REG_OFFSET 6 |
| 83 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 84 | /* The device registers */ |
| 85 | /* in nr from 0 to 8 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \ |
| 87 | (0x554 + (((nr) - 7) * 2))) |
| 88 | #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \ |
| 89 | (0x555 + (((nr) - 7) * 2))) |
| 90 | #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \ |
| 91 | (0x550 + (nr) - 7)) |
| 92 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 93 | /* fan nr from 0 to 2 */ |
| 94 | #define W83781D_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 95 | #define W83781D_REG_FAN(nr) (0x28 + (nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | #define W83781D_REG_BANK 0x4E |
| 98 | #define W83781D_REG_TEMP2_CONFIG 0x152 |
| 99 | #define W83781D_REG_TEMP3_CONFIG 0x252 |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 100 | /* temp nr from 1 to 3 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \ |
| 102 | ((nr == 2) ? (0x0150) : \ |
| 103 | (0x27))) |
| 104 | #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \ |
| 105 | ((nr == 2) ? (0x153) : \ |
| 106 | (0x3A))) |
| 107 | #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \ |
| 108 | ((nr == 2) ? (0x155) : \ |
| 109 | (0x39))) |
| 110 | |
| 111 | #define W83781D_REG_CONFIG 0x40 |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 112 | |
| 113 | /* Interrupt status (W83781D, AS99127F) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #define W83781D_REG_ALARM1 0x41 |
| 115 | #define W83781D_REG_ALARM2 0x42 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 117 | /* Real-time status (W83782D, W83783S, W83627HF) */ |
| 118 | #define W83782D_REG_ALARM1 0x459 |
| 119 | #define W83782D_REG_ALARM2 0x45A |
| 120 | #define W83782D_REG_ALARM3 0x45B |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #define W83781D_REG_BEEP_CONFIG 0x4D |
| 123 | #define W83781D_REG_BEEP_INTS1 0x56 |
| 124 | #define W83781D_REG_BEEP_INTS2 0x57 |
| 125 | #define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */ |
| 126 | |
| 127 | #define W83781D_REG_VID_FANDIV 0x47 |
| 128 | |
| 129 | #define W83781D_REG_CHIPID 0x49 |
| 130 | #define W83781D_REG_WCHIPID 0x58 |
| 131 | #define W83781D_REG_CHIPMAN 0x4F |
| 132 | #define W83781D_REG_PIN 0x4B |
| 133 | |
| 134 | /* 782D/783S only */ |
| 135 | #define W83781D_REG_VBAT 0x5D |
| 136 | |
| 137 | /* PWM 782D (1-4) and 783S (1-2) only */ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 138 | static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | #define W83781D_REG_PWMCLK12 0x5C |
| 140 | #define W83781D_REG_PWMCLK34 0x45C |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | #define W83781D_REG_I2C_ADDR 0x48 |
| 143 | #define W83781D_REG_I2C_SUBADDR 0x4A |
| 144 | |
| 145 | /* The following are undocumented in the data sheets however we |
| 146 | received the information in an email from Winbond tech support */ |
| 147 | /* Sensor selection - not on 781d */ |
| 148 | #define W83781D_REG_SCFG1 0x5D |
| 149 | static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 }; |
| 150 | |
| 151 | #define W83781D_REG_SCFG2 0x59 |
| 152 | static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 }; |
| 153 | |
| 154 | #define W83781D_DEFAULT_BETA 3435 |
| 155 | |
| 156 | /* RT Table registers */ |
| 157 | #define W83781D_REG_RT_IDX 0x50 |
| 158 | #define W83781D_REG_RT_VAL 0x51 |
| 159 | |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 160 | /* Conversions */ |
| 161 | #define IN_TO_REG(val) SENSORS_LIMIT(((val) + 8) / 16, 0, 255) |
| 162 | #define IN_FROM_REG(val) ((val) * 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | static inline u8 |
| 165 | FAN_TO_REG(long rpm, int div) |
| 166 | { |
| 167 | if (rpm == 0) |
| 168 | return 255; |
| 169 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); |
| 170 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
| 171 | } |
| 172 | |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 173 | static inline long |
| 174 | FAN_FROM_REG(u8 val, int div) |
| 175 | { |
| 176 | if (val == 0) |
| 177 | return -1; |
| 178 | if (val == 255) |
| 179 | return 0; |
| 180 | return 1350000 / (val * div); |
| 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 183 | #define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -127, 128) |
| 184 | #define TEMP_FROM_REG(val) ((val) * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | #define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \ |
| 187 | (val) ^ 0x7fff : (val)) |
| 188 | #define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \ |
| 189 | (~(val)) & 0x7fff : (val) & 0xffffff) |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | #define DIV_FROM_REG(val) (1 << (val)) |
| 192 | |
| 193 | static inline u8 |
| 194 | DIV_TO_REG(long val, enum chips type) |
| 195 | { |
| 196 | int i; |
| 197 | val = SENSORS_LIMIT(val, 1, |
| 198 | ((type == w83781d |
| 199 | || type == as99127f) ? 8 : 128)) >> 1; |
Grant Coady | abc0192 | 2005-05-12 13:41:51 +1000 | [diff] [blame] | 200 | for (i = 0; i < 7; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (val == 0) |
| 202 | break; |
| 203 | val >>= 1; |
| 204 | } |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 205 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* There are some complications in a module like this. First off, W83781D chips |
| 209 | may be both present on the SMBus and the ISA bus, and we have to handle |
| 210 | those cases separately at some places. Second, there might be several |
| 211 | W83781D chips available (well, actually, that is probably never done; but |
| 212 | it is a clean illustration of how to handle a case like that). Finally, |
| 213 | a specific chip may be attached to *both* ISA and SMBus, and we would |
| 214 | not like to detect it double. Fortunately, in the case of the W83781D at |
| 215 | least, a register tells us what SMBus address we are on, so that helps |
| 216 | a bit - except if there could be more than one SMBus. Groan. No solution |
| 217 | for this yet. */ |
| 218 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 219 | /* For ISA chips, we abuse the i2c_client addr and name fields. We also use |
| 220 | the driver field to differentiate between I2C and ISA chips. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | struct w83781d_data { |
| 222 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 223 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 224 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | enum chips type; |
| 226 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 227 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | char valid; /* !=0 if following fields are valid */ |
| 229 | unsigned long last_updated; /* In jiffies */ |
| 230 | |
| 231 | struct i2c_client *lm75[2]; /* for secondary I2C addresses */ |
| 232 | /* array of 2 pointers to subclients */ |
| 233 | |
| 234 | u8 in[9]; /* Register value - 8 & 9 for 782D only */ |
| 235 | u8 in_max[9]; /* Register value - 8 & 9 for 782D only */ |
| 236 | u8 in_min[9]; /* Register value - 8 & 9 for 782D only */ |
| 237 | u8 fan[3]; /* Register value */ |
| 238 | u8 fan_min[3]; /* Register value */ |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 239 | s8 temp; /* Register value */ |
| 240 | s8 temp_max; /* Register value */ |
| 241 | s8 temp_max_hyst; /* Register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | u16 temp_add[2]; /* Register value */ |
| 243 | u16 temp_max_add[2]; /* Register value */ |
| 244 | u16 temp_max_hyst_add[2]; /* Register value */ |
| 245 | u8 fan_div[3]; /* Register encoding, shifted right */ |
| 246 | u8 vid; /* Register encoding, combined */ |
| 247 | u32 alarms; /* Register encoding, combined */ |
| 248 | u32 beep_mask; /* Register encoding, combined */ |
| 249 | u8 beep_enable; /* Boolean */ |
| 250 | u8 pwm[4]; /* Register value */ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 251 | u8 pwm2_enable; /* Boolean */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | u16 sens[3]; /* 782D/783S only. |
| 253 | 1 = pentium diode; 2 = 3904 diode; |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 254 | 4 = thermistor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | u8 vrm; |
| 256 | }; |
| 257 | |
| 258 | static int w83781d_attach_adapter(struct i2c_adapter *adapter); |
| 259 | static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind); |
| 260 | static int w83781d_detach_client(struct i2c_client *client); |
| 261 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 262 | static int __devinit w83781d_isa_probe(struct platform_device *pdev); |
| 263 | static int __devexit w83781d_isa_remove(struct platform_device *pdev); |
| 264 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 265 | static int w83781d_read_value(struct w83781d_data *data, u16 reg); |
| 266 | static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | static struct w83781d_data *w83781d_update_device(struct device *dev); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 268 | static void w83781d_init_device(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | static struct i2c_driver w83781d_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 271 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 272 | .name = "w83781d", |
| 273 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | .id = I2C_DRIVERID_W83781D, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | .attach_adapter = w83781d_attach_adapter, |
| 276 | .detach_client = w83781d_detach_client, |
| 277 | }; |
| 278 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 279 | static struct platform_driver w83781d_isa_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 280 | .driver = { |
Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 281 | .owner = THIS_MODULE, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 282 | .name = "w83781d", |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 283 | }, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 284 | .probe = w83781d_isa_probe, |
| 285 | .remove = w83781d_isa_remove, |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | /* following are the sysfs callback functions */ |
| 290 | #define show_in_reg(reg) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 291 | static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \ |
| 292 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 294 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | struct w83781d_data *data = w83781d_update_device(dev); \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 296 | return sprintf(buf, "%ld\n", \ |
| 297 | (long)IN_FROM_REG(data->reg[attr->index])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | show_in_reg(in); |
| 300 | show_in_reg(in_min); |
| 301 | show_in_reg(in_max); |
| 302 | |
| 303 | #define store_in_reg(REG, reg) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 304 | static ssize_t store_in_##reg (struct device *dev, struct device_attribute \ |
| 305 | *da, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 307 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 308 | struct w83781d_data *data = dev_get_drvdata(dev); \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 309 | int nr = attr->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | u32 val; \ |
| 311 | \ |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 312 | val = simple_strtoul(buf, NULL, 10); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 314 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | data->in_##reg[nr] = IN_TO_REG(val); \ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 316 | w83781d_write_value(data, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 318 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return count; \ |
| 320 | } |
| 321 | store_in_reg(MIN, min); |
| 322 | store_in_reg(MAX, max); |
| 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | #define sysfs_in_offsets(offset) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 325 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 326 | show_in, NULL, offset); \ |
| 327 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 328 | show_in_min, store_in_min, offset); \ |
| 329 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 330 | show_in_max, store_in_max, offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | sysfs_in_offsets(0); |
| 333 | sysfs_in_offsets(1); |
| 334 | sysfs_in_offsets(2); |
| 335 | sysfs_in_offsets(3); |
| 336 | sysfs_in_offsets(4); |
| 337 | sysfs_in_offsets(5); |
| 338 | sysfs_in_offsets(6); |
| 339 | sysfs_in_offsets(7); |
| 340 | sysfs_in_offsets(8); |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | #define show_fan_reg(reg) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 343 | static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \ |
| 344 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 346 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | struct w83781d_data *data = w83781d_update_device(dev); \ |
| 348 | return sprintf(buf,"%ld\n", \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 349 | FAN_FROM_REG(data->reg[attr->index], \ |
| 350 | DIV_FROM_REG(data->fan_div[attr->index]))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | show_fan_reg(fan); |
| 353 | show_fan_reg(fan_min); |
| 354 | |
| 355 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 356 | store_fan_min(struct device *dev, struct device_attribute *da, |
| 357 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 359 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 360 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 361 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | u32 val; |
| 363 | |
| 364 | val = simple_strtoul(buf, NULL, 10); |
| 365 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 366 | mutex_lock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 367 | data->fan_min[nr] = |
| 368 | FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 369 | w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 370 | data->fan_min[nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 372 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return count; |
| 374 | } |
| 375 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 376 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
| 377 | static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR, |
| 378 | show_fan_min, store_fan_min, 0); |
| 379 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); |
| 380 | static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR, |
| 381 | show_fan_min, store_fan_min, 1); |
| 382 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); |
| 383 | static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR, |
| 384 | show_fan_min, store_fan_min, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | #define show_temp_reg(reg) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 387 | static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \ |
| 388 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 390 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | struct w83781d_data *data = w83781d_update_device(dev); \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 392 | int nr = attr->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (nr >= 2) { /* TEMP2 and TEMP3 */ \ |
| 394 | return sprintf(buf,"%d\n", \ |
| 395 | LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \ |
| 396 | } else { /* TEMP1 */ \ |
| 397 | return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \ |
| 398 | } \ |
| 399 | } |
| 400 | show_temp_reg(temp); |
| 401 | show_temp_reg(temp_max); |
| 402 | show_temp_reg(temp_max_hyst); |
| 403 | |
| 404 | #define store_temp_reg(REG, reg) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 405 | static ssize_t store_temp_##reg (struct device *dev, \ |
| 406 | struct device_attribute *da, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 408 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 409 | struct w83781d_data *data = dev_get_drvdata(dev); \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 410 | int nr = attr->index; \ |
Christian Hohnstaedt | 5bfedac | 2007-08-16 11:40:10 +0200 | [diff] [blame] | 411 | long val; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | \ |
| 413 | val = simple_strtol(buf, NULL, 10); \ |
| 414 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 415 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | \ |
| 417 | if (nr >= 2) { /* TEMP2 and TEMP3 */ \ |
| 418 | data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 419 | w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | data->temp_##reg##_add[nr-2]); \ |
| 421 | } else { /* TEMP1 */ \ |
| 422 | data->temp_##reg = TEMP_TO_REG(val); \ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 423 | w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | data->temp_##reg); \ |
| 425 | } \ |
| 426 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 427 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return count; \ |
| 429 | } |
| 430 | store_temp_reg(OVER, max); |
| 431 | store_temp_reg(HYST, max_hyst); |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | #define sysfs_temp_offsets(offset) \ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 434 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 435 | show_temp, NULL, offset); \ |
| 436 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 437 | show_temp_max, store_temp_max, offset); \ |
| 438 | static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \ |
| 439 | show_temp_max_hyst, store_temp_max_hyst, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | sysfs_temp_offsets(1); |
| 442 | sysfs_temp_offsets(2); |
| 443 | sysfs_temp_offsets(3); |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 446 | show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | struct w83781d_data *data = w83781d_update_device(dev); |
| 449 | return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm)); |
| 450 | } |
| 451 | |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 452 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 455 | show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame^] | 457 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return sprintf(buf, "%ld\n", (long) data->vrm); |
| 459 | } |
| 460 | |
| 461 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 462 | store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 464 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | u32 val; |
| 466 | |
| 467 | val = simple_strtoul(buf, NULL, 10); |
| 468 | data->vrm = val; |
| 469 | |
| 470 | return count; |
| 471 | } |
| 472 | |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 473 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); |
| 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 476 | show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 68188ba | 2005-05-16 18:52:38 +0200 | [diff] [blame] | 479 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 482 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL); |
| 483 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 484 | static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | struct w83781d_data *data = w83781d_update_device(dev); |
| 487 | return sprintf(buf, "%ld\n", |
| 488 | (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type)); |
| 489 | } |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 490 | static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 493 | return sprintf(buf, "%ld\n", (long)data->beep_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 497 | store_beep_mask(struct device *dev, struct device_attribute *attr, |
| 498 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 500 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 501 | u32 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | val = simple_strtoul(buf, NULL, 10); |
| 504 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 505 | mutex_lock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 506 | data->beep_mask = BEEP_MASK_TO_REG(val, data->type); |
| 507 | w83781d_write_value(data, W83781D_REG_BEEP_INTS1, |
| 508 | data->beep_mask & 0xff); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 509 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 510 | ((data->beep_mask >> 8) & 0x7f) |
| 511 | | data->beep_enable << 7); |
| 512 | if (data->type != w83781d && data->type != as99127f) { |
| 513 | w83781d_write_value(data, W83781D_REG_BEEP_INTS3, |
| 514 | ((data->beep_mask) >> 16) & 0xff); |
| 515 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 516 | mutex_unlock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | return count; |
| 519 | } |
| 520 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 521 | static ssize_t |
| 522 | store_beep_enable(struct device *dev, struct device_attribute *attr, |
| 523 | const char *buf, size_t count) |
| 524 | { |
| 525 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 526 | u32 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 528 | val = simple_strtoul(buf, NULL, 10); |
| 529 | if (val != 0 && val != 1) |
| 530 | return -EINVAL; |
| 531 | |
| 532 | mutex_lock(&data->update_lock); |
| 533 | data->beep_enable = val; |
| 534 | val = w83781d_read_value(data, W83781D_REG_BEEP_INTS2) & 0x7f; |
| 535 | val |= data->beep_enable << 7; |
| 536 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, val); |
| 537 | mutex_unlock(&data->update_lock); |
| 538 | |
| 539 | return count; |
| 540 | } |
| 541 | |
| 542 | static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR, |
| 543 | show_beep_mask, store_beep_mask); |
| 544 | static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR, |
| 545 | show_beep_enable, store_beep_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 548 | show_fan_div(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 550 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | struct w83781d_data *data = w83781d_update_device(dev); |
| 552 | return sprintf(buf, "%ld\n", |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 553 | (long) DIV_FROM_REG(data->fan_div[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Note: we save and restore the fan minimum here, because its value is |
| 557 | determined in part by the fan divisor. This follows the principle of |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 558 | least surprise; the user doesn't expect the fan minimum to change just |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | because the divisor changed. */ |
| 560 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 561 | store_fan_div(struct device *dev, struct device_attribute *da, |
| 562 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 564 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 565 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | unsigned long min; |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 567 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | u8 reg; |
| 569 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 570 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 571 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | /* Save fan_min */ |
| 574 | min = FAN_FROM_REG(data->fan_min[nr], |
| 575 | DIV_FROM_REG(data->fan_div[nr])); |
| 576 | |
| 577 | data->fan_div[nr] = DIV_TO_REG(val, data->type); |
| 578 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 579 | reg = (w83781d_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | & (nr==0 ? 0xcf : 0x3f)) |
| 581 | | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6)); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 582 | w83781d_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | /* w83781d and as99127f don't have extended divisor bits */ |
| 585 | if (data->type != w83781d && data->type != as99127f) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 586 | reg = (w83781d_read_value(data, W83781D_REG_VBAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | & ~(1 << (5 + nr))) |
| 588 | | ((data->fan_div[nr] & 0x04) << (3 + nr)); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 589 | w83781d_write_value(data, W83781D_REG_VBAT, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* Restore fan_min */ |
| 593 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 594 | w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 596 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | return count; |
| 598 | } |
| 599 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 600 | static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, |
| 601 | show_fan_div, store_fan_div, 0); |
| 602 | static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, |
| 603 | show_fan_div, store_fan_div, 1); |
| 604 | static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, |
| 605 | show_fan_div, store_fan_div, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 608 | show_pwm(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 610 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 612 | return sprintf(buf, "%d\n", (int)data->pwm[attr->index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 616 | show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
| 618 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 619 | return sprintf(buf, "%d\n", (int)data->pwm2_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 623 | store_pwm(struct device *dev, struct device_attribute *da, const char *buf, |
| 624 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 626 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 627 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 628 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | u32 val; |
| 630 | |
| 631 | val = simple_strtoul(buf, NULL, 10); |
| 632 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 633 | mutex_lock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 634 | data->pwm[nr] = SENSORS_LIMIT(val, 0, 255); |
| 635 | w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 636 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | return count; |
| 638 | } |
| 639 | |
| 640 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 641 | store_pwm2_enable(struct device *dev, struct device_attribute *da, |
| 642 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 644 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | u32 val, reg; |
| 646 | |
| 647 | val = simple_strtoul(buf, NULL, 10); |
| 648 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 649 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
| 651 | switch (val) { |
| 652 | case 0: |
| 653 | case 1: |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 654 | reg = w83781d_read_value(data, W83781D_REG_PWMCLK12); |
| 655 | w83781d_write_value(data, W83781D_REG_PWMCLK12, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | (reg & 0xf7) | (val << 3)); |
| 657 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 658 | reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG); |
| 659 | w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | (reg & 0xef) | (!val << 4)); |
| 661 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 662 | data->pwm2_enable = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | default: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 666 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | return -EINVAL; |
| 668 | } |
| 669 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 670 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | return count; |
| 672 | } |
| 673 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 674 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0); |
| 675 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1); |
| 676 | static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2); |
| 677 | static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3); |
| 678 | /* only PWM2 can be enabled/disabled */ |
| 679 | static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, |
| 680 | show_pwm2_enable, store_pwm2_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 683 | show_sensor(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 685 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 687 | return sprintf(buf, "%d\n", (int)data->sens[attr->index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 691 | store_sensor(struct device *dev, struct device_attribute *da, |
| 692 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 694 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 695 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 696 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | u32 val, tmp; |
| 698 | |
| 699 | val = simple_strtoul(buf, NULL, 10); |
| 700 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 701 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | switch (val) { |
| 704 | case 1: /* PII/Celeron diode */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 705 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
| 706 | w83781d_write_value(data, W83781D_REG_SCFG1, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 707 | tmp | BIT_SCFG1[nr]); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 708 | tmp = w83781d_read_value(data, W83781D_REG_SCFG2); |
| 709 | w83781d_write_value(data, W83781D_REG_SCFG2, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 710 | tmp | BIT_SCFG2[nr]); |
| 711 | data->sens[nr] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | break; |
| 713 | case 2: /* 3904 */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 714 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
| 715 | w83781d_write_value(data, W83781D_REG_SCFG1, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 716 | tmp | BIT_SCFG1[nr]); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 717 | tmp = w83781d_read_value(data, W83781D_REG_SCFG2); |
| 718 | w83781d_write_value(data, W83781D_REG_SCFG2, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 719 | tmp & ~BIT_SCFG2[nr]); |
| 720 | data->sens[nr] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | break; |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 722 | case W83781D_DEFAULT_BETA: |
| 723 | dev_warn(dev, "Sensor type %d is deprecated, please use 4 " |
| 724 | "instead\n", W83781D_DEFAULT_BETA); |
| 725 | /* fall through */ |
| 726 | case 4: /* thermistor */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 727 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
| 728 | w83781d_write_value(data, W83781D_REG_SCFG1, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 729 | tmp & ~BIT_SCFG1[nr]); |
| 730 | data->sens[nr] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | break; |
| 732 | default: |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 733 | dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n", |
| 734 | (long) val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 738 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | return count; |
| 740 | } |
| 741 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 742 | static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, |
| 743 | show_sensor, store_sensor, 0); |
| 744 | static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, |
Mark M. Hoffman | 393cdad | 2007-08-09 08:12:46 -0400 | [diff] [blame] | 745 | show_sensor, store_sensor, 1); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 746 | static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, |
Mark M. Hoffman | 393cdad | 2007-08-09 08:12:46 -0400 | [diff] [blame] | 747 | show_sensor, store_sensor, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 749 | /* I2C devices get this name attribute automatically, but for ISA devices |
| 750 | we must create it by ourselves. */ |
| 751 | static ssize_t |
| 752 | show_name(struct device *dev, struct device_attribute *devattr, char *buf) |
| 753 | { |
| 754 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 755 | return sprintf(buf, "%s\n", data->client.name); |
| 756 | } |
| 757 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | /* This function is called when: |
| 760 | * w83781d_driver is inserted (when this module is loaded), for each |
| 761 | available adapter |
| 762 | * when a new adapter is inserted (and w83781d_driver is still present) */ |
| 763 | static int |
| 764 | w83781d_attach_adapter(struct i2c_adapter *adapter) |
| 765 | { |
| 766 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 767 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 768 | return i2c_probe(adapter, &addr_data, w83781d_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* Assumes that adapter is of I2C, not ISA variety. |
| 772 | * OTHERWISE DON'T CALL THIS |
| 773 | */ |
| 774 | static int |
| 775 | w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind, |
| 776 | struct i2c_client *new_client) |
| 777 | { |
| 778 | int i, val1 = 0, id; |
| 779 | int err; |
| 780 | const char *client_name = ""; |
| 781 | struct w83781d_data *data = i2c_get_clientdata(new_client); |
| 782 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 783 | data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (!(data->lm75[0])) { |
| 785 | err = -ENOMEM; |
| 786 | goto ERROR_SC_0; |
| 787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | id = i2c_adapter_id(adapter); |
| 790 | |
| 791 | if (force_subclients[0] == id && force_subclients[1] == address) { |
| 792 | for (i = 2; i <= 3; i++) { |
| 793 | if (force_subclients[i] < 0x48 || |
| 794 | force_subclients[i] > 0x4f) { |
| 795 | dev_err(&new_client->dev, "Invalid subclient " |
| 796 | "address %d; must be 0x48-0x4f\n", |
| 797 | force_subclients[i]); |
| 798 | err = -EINVAL; |
| 799 | goto ERROR_SC_1; |
| 800 | } |
| 801 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 802 | w83781d_write_value(data, W83781D_REG_I2C_SUBADDR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | (force_subclients[2] & 0x07) | |
| 804 | ((force_subclients[3] & 0x07) << 4)); |
| 805 | data->lm75[0]->addr = force_subclients[2]; |
| 806 | } else { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 807 | val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | data->lm75[0]->addr = 0x48 + (val1 & 0x07); |
| 809 | } |
| 810 | |
| 811 | if (kind != w83783s) { |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 812 | data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | if (!(data->lm75[1])) { |
| 814 | err = -ENOMEM; |
| 815 | goto ERROR_SC_1; |
| 816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | if (force_subclients[0] == id && |
| 819 | force_subclients[1] == address) { |
| 820 | data->lm75[1]->addr = force_subclients[3]; |
| 821 | } else { |
| 822 | data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07); |
| 823 | } |
| 824 | if (data->lm75[0]->addr == data->lm75[1]->addr) { |
| 825 | dev_err(&new_client->dev, |
| 826 | "Duplicate addresses 0x%x for subclients.\n", |
| 827 | data->lm75[0]->addr); |
| 828 | err = -EBUSY; |
| 829 | goto ERROR_SC_2; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (kind == w83781d) |
| 834 | client_name = "w83781d subclient"; |
| 835 | else if (kind == w83782d) |
| 836 | client_name = "w83782d subclient"; |
| 837 | else if (kind == w83783s) |
| 838 | client_name = "w83783s subclient"; |
| 839 | else if (kind == w83627hf) |
| 840 | client_name = "w83627hf subclient"; |
| 841 | else if (kind == as99127f) |
| 842 | client_name = "as99127f subclient"; |
| 843 | |
| 844 | for (i = 0; i <= 1; i++) { |
| 845 | /* store all data in w83781d */ |
| 846 | i2c_set_clientdata(data->lm75[i], NULL); |
| 847 | data->lm75[i]->adapter = adapter; |
| 848 | data->lm75[i]->driver = &w83781d_driver; |
| 849 | data->lm75[i]->flags = 0; |
| 850 | strlcpy(data->lm75[i]->name, client_name, |
| 851 | I2C_NAME_SIZE); |
| 852 | if ((err = i2c_attach_client(data->lm75[i]))) { |
| 853 | dev_err(&new_client->dev, "Subclient %d " |
| 854 | "registration at address 0x%x " |
| 855 | "failed.\n", i, data->lm75[i]->addr); |
| 856 | if (i == 1) |
| 857 | goto ERROR_SC_3; |
| 858 | goto ERROR_SC_2; |
| 859 | } |
| 860 | if (kind == w83783s) |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | |
| 866 | /* Undo inits in case of errors */ |
| 867 | ERROR_SC_3: |
| 868 | i2c_detach_client(data->lm75[0]); |
| 869 | ERROR_SC_2: |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 870 | kfree(data->lm75[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | ERROR_SC_1: |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 872 | kfree(data->lm75[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | ERROR_SC_0: |
| 874 | return err; |
| 875 | } |
| 876 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 877 | #define IN_UNIT_ATTRS(X) \ |
| 878 | &sensor_dev_attr_in##X##_input.dev_attr.attr, \ |
| 879 | &sensor_dev_attr_in##X##_min.dev_attr.attr, \ |
| 880 | &sensor_dev_attr_in##X##_max.dev_attr.attr |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 881 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 882 | #define FAN_UNIT_ATTRS(X) \ |
| 883 | &sensor_dev_attr_fan##X##_input.dev_attr.attr, \ |
| 884 | &sensor_dev_attr_fan##X##_min.dev_attr.attr, \ |
| 885 | &sensor_dev_attr_fan##X##_div.dev_attr.attr |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 886 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 887 | #define TEMP_UNIT_ATTRS(X) \ |
| 888 | &sensor_dev_attr_temp##X##_input.dev_attr.attr, \ |
| 889 | &sensor_dev_attr_temp##X##_max.dev_attr.attr, \ |
| 890 | &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 891 | |
| 892 | static struct attribute* w83781d_attributes[] = { |
| 893 | IN_UNIT_ATTRS(0), |
| 894 | IN_UNIT_ATTRS(2), |
| 895 | IN_UNIT_ATTRS(3), |
| 896 | IN_UNIT_ATTRS(4), |
| 897 | IN_UNIT_ATTRS(5), |
| 898 | IN_UNIT_ATTRS(6), |
| 899 | FAN_UNIT_ATTRS(1), |
| 900 | FAN_UNIT_ATTRS(2), |
| 901 | FAN_UNIT_ATTRS(3), |
| 902 | TEMP_UNIT_ATTRS(1), |
| 903 | TEMP_UNIT_ATTRS(2), |
| 904 | &dev_attr_cpu0_vid.attr, |
| 905 | &dev_attr_vrm.attr, |
| 906 | &dev_attr_alarms.attr, |
| 907 | &dev_attr_beep_mask.attr, |
| 908 | &dev_attr_beep_enable.attr, |
| 909 | NULL |
| 910 | }; |
| 911 | static const struct attribute_group w83781d_group = { |
| 912 | .attrs = w83781d_attributes, |
| 913 | }; |
| 914 | |
| 915 | static struct attribute *w83781d_attributes_opt[] = { |
| 916 | IN_UNIT_ATTRS(1), |
| 917 | IN_UNIT_ATTRS(7), |
| 918 | IN_UNIT_ATTRS(8), |
| 919 | TEMP_UNIT_ATTRS(3), |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 920 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 921 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 922 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 923 | &sensor_dev_attr_pwm4.dev_attr.attr, |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 924 | &dev_attr_pwm2_enable.attr, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 925 | &sensor_dev_attr_temp1_type.dev_attr.attr, |
| 926 | &sensor_dev_attr_temp2_type.dev_attr.attr, |
| 927 | &sensor_dev_attr_temp3_type.dev_attr.attr, |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 928 | NULL |
| 929 | }; |
| 930 | static const struct attribute_group w83781d_group_opt = { |
| 931 | .attrs = w83781d_attributes_opt, |
| 932 | }; |
| 933 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 934 | /* No clean up is done on error, it's up to the caller */ |
| 935 | static int |
| 936 | w83781d_create_files(struct device *dev, int kind, int is_isa) |
| 937 | { |
| 938 | int err; |
| 939 | |
| 940 | if ((err = sysfs_create_group(&dev->kobj, &w83781d_group))) |
| 941 | return err; |
| 942 | |
| 943 | if (kind != w83783s) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 944 | if ((err = device_create_file(dev, |
| 945 | &sensor_dev_attr_in1_input.dev_attr)) |
| 946 | || (err = device_create_file(dev, |
| 947 | &sensor_dev_attr_in1_min.dev_attr)) |
| 948 | || (err = device_create_file(dev, |
| 949 | &sensor_dev_attr_in1_max.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 950 | return err; |
| 951 | } |
| 952 | if (kind != as99127f && kind != w83781d && kind != w83783s) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 953 | if ((err = device_create_file(dev, |
| 954 | &sensor_dev_attr_in7_input.dev_attr)) |
| 955 | || (err = device_create_file(dev, |
| 956 | &sensor_dev_attr_in7_min.dev_attr)) |
| 957 | || (err = device_create_file(dev, |
| 958 | &sensor_dev_attr_in7_max.dev_attr)) |
| 959 | || (err = device_create_file(dev, |
| 960 | &sensor_dev_attr_in8_input.dev_attr)) |
| 961 | || (err = device_create_file(dev, |
| 962 | &sensor_dev_attr_in8_min.dev_attr)) |
| 963 | || (err = device_create_file(dev, |
| 964 | &sensor_dev_attr_in8_max.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 965 | return err; |
| 966 | } |
| 967 | if (kind != w83783s) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 968 | if ((err = device_create_file(dev, |
| 969 | &sensor_dev_attr_temp3_input.dev_attr)) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 970 | || (err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 971 | &sensor_dev_attr_temp3_max.dev_attr)) |
| 972 | || (err = device_create_file(dev, |
| 973 | &sensor_dev_attr_temp3_max_hyst.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 974 | return err; |
| 975 | } |
| 976 | |
| 977 | if (kind != w83781d && kind != as99127f) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 978 | if ((err = device_create_file(dev, |
| 979 | &sensor_dev_attr_pwm1.dev_attr)) |
| 980 | || (err = device_create_file(dev, |
| 981 | &sensor_dev_attr_pwm2.dev_attr)) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 982 | || (err = device_create_file(dev, &dev_attr_pwm2_enable))) |
| 983 | return err; |
| 984 | } |
| 985 | if (kind == w83782d && !is_isa) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 986 | if ((err = device_create_file(dev, |
| 987 | &sensor_dev_attr_pwm3.dev_attr)) |
| 988 | || (err = device_create_file(dev, |
| 989 | &sensor_dev_attr_pwm4.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 990 | return err; |
| 991 | } |
| 992 | |
| 993 | if (kind != as99127f && kind != w83781d) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 994 | if ((err = device_create_file(dev, |
| 995 | &sensor_dev_attr_temp1_type.dev_attr)) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 996 | || (err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 997 | &sensor_dev_attr_temp2_type.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 998 | return err; |
| 999 | if (kind != w83783s) { |
| 1000 | if ((err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1001 | &sensor_dev_attr_temp3_type.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1002 | return err; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (is_isa) { |
| 1007 | err = device_create_file(&pdev->dev, &dev_attr_name); |
| 1008 | if (err) |
| 1009 | return err; |
| 1010 | } |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | static int |
| 1016 | w83781d_detect(struct i2c_adapter *adapter, int address, int kind) |
| 1017 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1018 | int val1 = 0, val2; |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1019 | struct i2c_client *client; |
| 1020 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | struct w83781d_data *data; |
| 1022 | int err; |
| 1023 | const char *client_name = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | enum vendor { winbond, asus } vendid; |
| 1025 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1026 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | err = -EINVAL; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1028 | goto ERROR1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* OK. For now, we presume we have a valid client. We now create the |
| 1032 | client structure, even though we cannot fill it completely yet. |
| 1033 | But it allows us to access w83781d_{read,write}_value. */ |
| 1034 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 1035 | if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | err = -ENOMEM; |
| 1037 | goto ERROR1; |
| 1038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1040 | client = &data->client; |
| 1041 | i2c_set_clientdata(client, data); |
| 1042 | client->addr = address; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1043 | mutex_init(&data->lock); |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1044 | client->adapter = adapter; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1045 | client->driver = &w83781d_driver; |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1046 | dev = &client->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | /* Now, we do the remaining detection. */ |
| 1049 | |
| 1050 | /* The w8378?d may be stuck in some other bank than bank 0. This may |
| 1051 | make reading other information impossible. Specify a force=... or |
| 1052 | force_*=... parameter, and the Winbond will be reset to the right |
| 1053 | bank. */ |
| 1054 | if (kind < 0) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1055 | if (w83781d_read_value(data, W83781D_REG_CONFIG) & 0x80) { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1056 | dev_dbg(&adapter->dev, "Detection of w83781d chip " |
| 1057 | "failed at step 3\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | err = -ENODEV; |
| 1059 | goto ERROR2; |
| 1060 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1061 | val1 = w83781d_read_value(data, W83781D_REG_BANK); |
| 1062 | val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | /* Check for Winbond or Asus ID if in bank 0 */ |
| 1064 | if ((!(val1 & 0x07)) && |
| 1065 | (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3)) |
| 1066 | || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1067 | dev_dbg(&adapter->dev, "Detection of w83781d chip " |
| 1068 | "failed at step 4\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | err = -ENODEV; |
| 1070 | goto ERROR2; |
| 1071 | } |
| 1072 | /* If Winbond SMBus, check address at 0x48. |
| 1073 | Asus doesn't support, except for as99127f rev.2 */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1074 | if ((!(val1 & 0x80) && (val2 == 0xa3)) || |
| 1075 | ((val1 & 0x80) && (val2 == 0x5c))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | if (w83781d_read_value |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1077 | (data, W83781D_REG_I2C_ADDR) != address) { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1078 | dev_dbg(&adapter->dev, "Detection of w83781d " |
| 1079 | "chip failed at step 5\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | err = -ENODEV; |
| 1081 | goto ERROR2; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | /* We have either had a force parameter, or we have already detected the |
| 1087 | Winbond. Put it now into bank 0 and Vendor ID High Byte */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1088 | w83781d_write_value(data, W83781D_REG_BANK, |
| 1089 | (w83781d_read_value(data, W83781D_REG_BANK) |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1090 | & 0x78) | 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* Determine the chip type. */ |
| 1093 | if (kind <= 0) { |
| 1094 | /* get vendor ID */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1095 | val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (val2 == 0x5c) |
| 1097 | vendid = winbond; |
| 1098 | else if (val2 == 0x12) |
| 1099 | vendid = asus; |
| 1100 | else { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1101 | dev_dbg(&adapter->dev, "w83781d chip vendor is " |
| 1102 | "neither Winbond nor Asus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | err = -ENODEV; |
| 1104 | goto ERROR2; |
| 1105 | } |
| 1106 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1107 | val1 = w83781d_read_value(data, W83781D_REG_WCHIPID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond) |
| 1109 | kind = w83781d; |
| 1110 | else if (val1 == 0x30 && vendid == winbond) |
| 1111 | kind = w83782d; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1112 | else if (val1 == 0x40 && vendid == winbond && address == 0x2d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | kind = w83783s; |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1114 | else if (val1 == 0x21 && vendid == winbond) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | kind = w83627hf; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1116 | else if (val1 == 0x31 && address >= 0x28) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | kind = as99127f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | else { |
| 1119 | if (kind == 0) |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1120 | dev_warn(&adapter->dev, "Ignoring 'force' " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | "parameter for unknown chip at " |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1122 | "address 0x%02x\n", address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | err = -EINVAL; |
| 1124 | goto ERROR2; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | if (kind == w83781d) { |
| 1129 | client_name = "w83781d"; |
| 1130 | } else if (kind == w83782d) { |
| 1131 | client_name = "w83782d"; |
| 1132 | } else if (kind == w83783s) { |
| 1133 | client_name = "w83783s"; |
| 1134 | } else if (kind == w83627hf) { |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1135 | client_name = "w83627hf"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } else if (kind == as99127f) { |
| 1137 | client_name = "as99127f"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | /* Fill in the remaining client fields and put into the global list */ |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1141 | strlcpy(client->name, client_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | data->type = kind; |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | /* Tell the I2C layer a new client has arrived */ |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1145 | if ((err = i2c_attach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | goto ERROR2; |
| 1147 | |
| 1148 | /* attach secondary i2c lm75-like clients */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1149 | if ((err = w83781d_detect_subclients(adapter, address, |
| 1150 | kind, client))) |
| 1151 | goto ERROR3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
| 1153 | /* Initialize the chip */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1154 | w83781d_init_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
| 1156 | /* Register sysfs hooks */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1157 | err = w83781d_create_files(dev, kind, 0); |
| 1158 | if (err) |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1159 | goto ERROR4; |
| 1160 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1161 | data->hwmon_dev = hwmon_device_register(dev); |
| 1162 | if (IS_ERR(data->hwmon_dev)) { |
| 1163 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1164 | goto ERROR4; |
| 1165 | } |
| 1166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | return 0; |
| 1168 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1169 | ERROR4: |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1170 | sysfs_remove_group(&dev->kobj, &w83781d_group); |
| 1171 | sysfs_remove_group(&dev->kobj, &w83781d_group_opt); |
| 1172 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1173 | if (data->lm75[1]) { |
| 1174 | i2c_detach_client(data->lm75[1]); |
| 1175 | kfree(data->lm75[1]); |
| 1176 | } |
| 1177 | if (data->lm75[0]) { |
| 1178 | i2c_detach_client(data->lm75[0]); |
| 1179 | kfree(data->lm75[0]); |
| 1180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | ERROR3: |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1182 | i2c_detach_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | ERROR2: |
| 1184 | kfree(data); |
| 1185 | ERROR1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return err; |
| 1187 | } |
| 1188 | |
| 1189 | static int |
| 1190 | w83781d_detach_client(struct i2c_client *client) |
| 1191 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1192 | struct w83781d_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | int err; |
| 1194 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1195 | /* main client */ |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1196 | if (data) { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1197 | hwmon_device_unregister(data->hwmon_dev); |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1198 | sysfs_remove_group(&client->dev.kobj, &w83781d_group); |
| 1199 | sysfs_remove_group(&client->dev.kobj, &w83781d_group_opt); |
| 1200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 1202 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1205 | /* main client */ |
| 1206 | if (data) |
| 1207 | kfree(data); |
| 1208 | |
| 1209 | /* subclient */ |
| 1210 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1216 | static int __devinit |
| 1217 | w83781d_isa_probe(struct platform_device *pdev) |
| 1218 | { |
| 1219 | int err, reg; |
| 1220 | struct w83781d_data *data; |
| 1221 | struct resource *res; |
| 1222 | const char *name; |
| 1223 | |
| 1224 | /* Reserve the ISA region */ |
| 1225 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 1226 | if (!request_region(res->start, W83781D_EXTENT, "w83781d")) { |
| 1227 | err = -EBUSY; |
| 1228 | goto exit; |
| 1229 | } |
| 1230 | |
| 1231 | if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) { |
| 1232 | err = -ENOMEM; |
| 1233 | goto exit_release_region; |
| 1234 | } |
| 1235 | mutex_init(&data->lock); |
| 1236 | data->client.addr = res->start; |
| 1237 | i2c_set_clientdata(&data->client, data); |
| 1238 | platform_set_drvdata(pdev, data); |
| 1239 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1240 | reg = w83781d_read_value(data, W83781D_REG_WCHIPID); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1241 | switch (reg) { |
| 1242 | case 0x21: |
| 1243 | data->type = w83627hf; |
| 1244 | name = "w83627hf"; |
| 1245 | break; |
| 1246 | case 0x30: |
| 1247 | data->type = w83782d; |
| 1248 | name = "w83782d"; |
| 1249 | break; |
| 1250 | default: |
| 1251 | data->type = w83781d; |
| 1252 | name = "w83781d"; |
| 1253 | } |
| 1254 | strlcpy(data->client.name, name, I2C_NAME_SIZE); |
| 1255 | |
| 1256 | /* Initialize the W83781D chip */ |
| 1257 | w83781d_init_device(&pdev->dev); |
| 1258 | |
| 1259 | /* Register sysfs hooks */ |
| 1260 | err = w83781d_create_files(&pdev->dev, data->type, 1); |
| 1261 | if (err) |
| 1262 | goto exit_remove_files; |
| 1263 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1264 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 1265 | if (IS_ERR(data->hwmon_dev)) { |
| 1266 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1267 | goto exit_remove_files; |
| 1268 | } |
| 1269 | |
| 1270 | return 0; |
| 1271 | |
| 1272 | exit_remove_files: |
| 1273 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group); |
| 1274 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt); |
| 1275 | device_remove_file(&pdev->dev, &dev_attr_name); |
| 1276 | kfree(data); |
| 1277 | exit_release_region: |
| 1278 | release_region(res->start, W83781D_EXTENT); |
| 1279 | exit: |
| 1280 | return err; |
| 1281 | } |
| 1282 | |
| 1283 | static int __devexit |
| 1284 | w83781d_isa_remove(struct platform_device *pdev) |
| 1285 | { |
| 1286 | struct w83781d_data *data = platform_get_drvdata(pdev); |
| 1287 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1288 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1289 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group); |
| 1290 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt); |
| 1291 | device_remove_file(&pdev->dev, &dev_attr_name); |
| 1292 | release_region(data->client.addr, W83781D_EXTENT); |
| 1293 | kfree(data); |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | /* The SMBus locks itself, usually, but nothing may access the Winbond between |
| 1299 | bank switches. ISA access must always be locked explicitly! |
| 1300 | We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks, |
| 1301 | would slow down the W83781D access and should not be necessary. |
| 1302 | There are some ugly typecasts here, but the good news is - they should |
| 1303 | nowhere else be necessary! */ |
| 1304 | static int |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1305 | w83781d_read_value(struct w83781d_data *data, u16 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1307 | struct i2c_client *client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | int res, word_sized, bank; |
| 1309 | struct i2c_client *cl; |
| 1310 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1311 | mutex_lock(&data->lock); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1312 | if (!client->driver) { /* ISA device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | word_sized = (((reg & 0xff00) == 0x100) |
| 1314 | || ((reg & 0xff00) == 0x200)) |
| 1315 | && (((reg & 0x00ff) == 0x50) |
| 1316 | || ((reg & 0x00ff) == 0x53) |
| 1317 | || ((reg & 0x00ff) == 0x55)); |
| 1318 | if (reg & 0xff00) { |
| 1319 | outb_p(W83781D_REG_BANK, |
| 1320 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1321 | outb_p(reg >> 8, |
| 1322 | client->addr + W83781D_DATA_REG_OFFSET); |
| 1323 | } |
| 1324 | outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET); |
| 1325 | res = inb_p(client->addr + W83781D_DATA_REG_OFFSET); |
| 1326 | if (word_sized) { |
| 1327 | outb_p((reg & 0xff) + 1, |
| 1328 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1329 | res = |
| 1330 | (res << 8) + inb_p(client->addr + |
| 1331 | W83781D_DATA_REG_OFFSET); |
| 1332 | } |
| 1333 | if (reg & 0xff00) { |
| 1334 | outb_p(W83781D_REG_BANK, |
| 1335 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1336 | outb_p(0, client->addr + W83781D_DATA_REG_OFFSET); |
| 1337 | } |
| 1338 | } else { |
| 1339 | bank = (reg >> 8) & 0x0f; |
| 1340 | if (bank > 2) |
| 1341 | /* switch banks */ |
| 1342 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, |
| 1343 | bank); |
| 1344 | if (bank == 0 || bank > 2) { |
| 1345 | res = i2c_smbus_read_byte_data(client, reg & 0xff); |
| 1346 | } else { |
| 1347 | /* switch to subclient */ |
| 1348 | cl = data->lm75[bank - 1]; |
| 1349 | /* convert from ISA to LM75 I2C addresses */ |
| 1350 | switch (reg & 0xff) { |
| 1351 | case 0x50: /* TEMP */ |
| 1352 | res = swab16(i2c_smbus_read_word_data(cl, 0)); |
| 1353 | break; |
| 1354 | case 0x52: /* CONFIG */ |
| 1355 | res = i2c_smbus_read_byte_data(cl, 1); |
| 1356 | break; |
| 1357 | case 0x53: /* HYST */ |
| 1358 | res = swab16(i2c_smbus_read_word_data(cl, 2)); |
| 1359 | break; |
| 1360 | case 0x55: /* OVER */ |
| 1361 | default: |
| 1362 | res = swab16(i2c_smbus_read_word_data(cl, 3)); |
| 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | if (bank > 2) |
| 1367 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0); |
| 1368 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1369 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | return res; |
| 1371 | } |
| 1372 | |
| 1373 | static int |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1374 | w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1376 | struct i2c_client *client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | int word_sized, bank; |
| 1378 | struct i2c_client *cl; |
| 1379 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1380 | mutex_lock(&data->lock); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1381 | if (!client->driver) { /* ISA device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | word_sized = (((reg & 0xff00) == 0x100) |
| 1383 | || ((reg & 0xff00) == 0x200)) |
| 1384 | && (((reg & 0x00ff) == 0x53) |
| 1385 | || ((reg & 0x00ff) == 0x55)); |
| 1386 | if (reg & 0xff00) { |
| 1387 | outb_p(W83781D_REG_BANK, |
| 1388 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1389 | outb_p(reg >> 8, |
| 1390 | client->addr + W83781D_DATA_REG_OFFSET); |
| 1391 | } |
| 1392 | outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET); |
| 1393 | if (word_sized) { |
| 1394 | outb_p(value >> 8, |
| 1395 | client->addr + W83781D_DATA_REG_OFFSET); |
| 1396 | outb_p((reg & 0xff) + 1, |
| 1397 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1398 | } |
| 1399 | outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET); |
| 1400 | if (reg & 0xff00) { |
| 1401 | outb_p(W83781D_REG_BANK, |
| 1402 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1403 | outb_p(0, client->addr + W83781D_DATA_REG_OFFSET); |
| 1404 | } |
| 1405 | } else { |
| 1406 | bank = (reg >> 8) & 0x0f; |
| 1407 | if (bank > 2) |
| 1408 | /* switch banks */ |
| 1409 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, |
| 1410 | bank); |
| 1411 | if (bank == 0 || bank > 2) { |
| 1412 | i2c_smbus_write_byte_data(client, reg & 0xff, |
| 1413 | value & 0xff); |
| 1414 | } else { |
| 1415 | /* switch to subclient */ |
| 1416 | cl = data->lm75[bank - 1]; |
| 1417 | /* convert from ISA to LM75 I2C addresses */ |
| 1418 | switch (reg & 0xff) { |
| 1419 | case 0x52: /* CONFIG */ |
| 1420 | i2c_smbus_write_byte_data(cl, 1, value & 0xff); |
| 1421 | break; |
| 1422 | case 0x53: /* HYST */ |
| 1423 | i2c_smbus_write_word_data(cl, 2, swab16(value)); |
| 1424 | break; |
| 1425 | case 0x55: /* OVER */ |
| 1426 | i2c_smbus_write_word_data(cl, 3, swab16(value)); |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | if (bank > 2) |
| 1431 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0); |
| 1432 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1433 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | return 0; |
| 1435 | } |
| 1436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | static void |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1438 | w83781d_init_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1440 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | int i, p; |
| 1442 | int type = data->type; |
| 1443 | u8 tmp; |
| 1444 | |
Jean Delvare | f5f8d38 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1445 | if (type == w83627hf) |
| 1446 | dev_info(dev, "The W83627HF chip is better supported by the " |
| 1447 | "w83627hf driver, support will be dropped from the " |
| 1448 | "w83781d driver soon\n"); |
| 1449 | |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1450 | if (reset && type != as99127f) { /* this resets registers we don't have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | documentation for on the as99127f */ |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1452 | /* Resetting the chip has been the default for a long time, |
| 1453 | but it causes the BIOS initializations (fan clock dividers, |
| 1454 | thermal sensor types...) to be lost, so it is now optional. |
| 1455 | It might even go away if nobody reports it as being useful, |
| 1456 | as I see very little reason why this would be needed at |
| 1457 | all. */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1458 | dev_info(dev, "If reset=1 solved a problem you were " |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1459 | "having, please report!\n"); |
| 1460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | /* save these registers */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1462 | i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG); |
| 1463 | p = w83781d_read_value(data, W83781D_REG_PWMCLK12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | /* Reset all except Watchdog values and last conversion values |
| 1465 | This sets fan-divs to 2, among others */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1466 | w83781d_write_value(data, W83781D_REG_CONFIG, 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* Restore the registers and disable power-on abnormal beep. |
| 1468 | This saves FAN 1/2/3 input/output values set by BIOS. */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1469 | w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80); |
| 1470 | w83781d_write_value(data, W83781D_REG_PWMCLK12, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | /* Disable master beep-enable (reset turns it on). |
| 1472 | Individual beep_mask should be reset to off but for some reason |
| 1473 | disabling this bit helps some people not get beeped */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1474 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1477 | /* Disable power-on abnormal beep, as advised by the datasheet. |
| 1478 | Already done if reset=1. */ |
| 1479 | if (init && !reset && type != as99127f) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1480 | i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG); |
| 1481 | w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80); |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 1484 | data->vrm = vid_which_vrm(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
| 1486 | if ((type != w83781d) && (type != as99127f)) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1487 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | for (i = 1; i <= 3; i++) { |
| 1489 | if (!(tmp & BIT_SCFG1[i - 1])) { |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 1490 | data->sens[i - 1] = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | } else { |
| 1492 | if (w83781d_read_value |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1493 | (data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | W83781D_REG_SCFG2) & BIT_SCFG2[i - 1]) |
| 1495 | data->sens[i - 1] = 1; |
| 1496 | else |
| 1497 | data->sens[i - 1] = 2; |
| 1498 | } |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1499 | if (type == w83783s && i == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | if (init && type != as99127f) { |
| 1505 | /* Enable temp2 */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1506 | tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | if (tmp & 0x01) { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1508 | dev_warn(dev, "Enabling temp2, readings " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | "might not make sense\n"); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1510 | w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | tmp & 0xfe); |
| 1512 | } |
| 1513 | |
| 1514 | /* Enable temp3 */ |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1515 | if (type != w83783s) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1516 | tmp = w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | W83781D_REG_TEMP3_CONFIG); |
| 1518 | if (tmp & 0x01) { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1519 | dev_warn(dev, "Enabling temp3, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | "readings might not make sense\n"); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1521 | w83781d_write_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | W83781D_REG_TEMP3_CONFIG, tmp & 0xfe); |
| 1523 | } |
| 1524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | /* Start monitoring */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1528 | w83781d_write_value(data, W83781D_REG_CONFIG, |
| 1529 | (w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | W83781D_REG_CONFIG) & 0xf7) |
| 1531 | | 0x01); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1532 | |
| 1533 | /* A few vars need to be filled upon startup */ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1534 | for (i = 0; i < 3; i++) { |
| 1535 | data->fan_min[i] = w83781d_read_value(data, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1536 | W83781D_REG_FAN_MIN(i)); |
| 1537 | } |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1538 | |
| 1539 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | static struct w83781d_data *w83781d_update_device(struct device *dev) |
| 1543 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1544 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 1545 | struct i2c_client *client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | int i; |
| 1547 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1548 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
| 1550 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 1551 | || !data->valid) { |
| 1552 | dev_dbg(dev, "Starting device update\n"); |
| 1553 | |
| 1554 | for (i = 0; i <= 8; i++) { |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1555 | if (data->type == w83783s && i == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | continue; /* 783S has no in1 */ |
| 1557 | data->in[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1558 | w83781d_read_value(data, W83781D_REG_IN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | data->in_min[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1560 | w83781d_read_value(data, W83781D_REG_IN_MIN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | data->in_max[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1562 | w83781d_read_value(data, W83781D_REG_IN_MAX(i)); |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1563 | if ((data->type != w83782d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | && (data->type != w83627hf) && (i == 6)) |
| 1565 | break; |
| 1566 | } |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1567 | for (i = 0; i < 3; i++) { |
| 1568 | data->fan[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1569 | w83781d_read_value(data, W83781D_REG_FAN(i)); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1570 | data->fan_min[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1571 | w83781d_read_value(data, W83781D_REG_FAN_MIN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | } |
| 1573 | if (data->type != w83781d && data->type != as99127f) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1574 | for (i = 0; i < 4; i++) { |
| 1575 | data->pwm[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1576 | w83781d_read_value(data, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1577 | W83781D_REG_PWM[i]); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1578 | if ((data->type != w83782d || !client->driver) |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1579 | && i == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | break; |
| 1581 | } |
| 1582 | /* Only PWM2 can be disabled */ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1583 | data->pwm2_enable = (w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | W83781D_REG_PWMCLK12) & 0x08) >> 3; |
| 1585 | } |
| 1586 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1587 | data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | data->temp_max = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1589 | w83781d_read_value(data, W83781D_REG_TEMP_OVER(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | data->temp_max_hyst = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1591 | w83781d_read_value(data, W83781D_REG_TEMP_HYST(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | data->temp_add[0] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1593 | w83781d_read_value(data, W83781D_REG_TEMP(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | data->temp_max_add[0] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1595 | w83781d_read_value(data, W83781D_REG_TEMP_OVER(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | data->temp_max_hyst_add[0] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1597 | w83781d_read_value(data, W83781D_REG_TEMP_HYST(2)); |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1598 | if (data->type != w83783s) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | data->temp_add[1] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1600 | w83781d_read_value(data, W83781D_REG_TEMP(3)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | data->temp_max_add[1] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1602 | w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | W83781D_REG_TEMP_OVER(3)); |
| 1604 | data->temp_max_hyst_add[1] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1605 | w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | W83781D_REG_TEMP_HYST(3)); |
| 1607 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1608 | i = w83781d_read_value(data, W83781D_REG_VID_FANDIV); |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1609 | data->vid = i & 0x0f; |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1610 | data->vid |= (w83781d_read_value(data, |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1611 | W83781D_REG_CHIPID) & 0x01) << 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | data->fan_div[0] = (i >> 4) & 0x03; |
| 1613 | data->fan_div[1] = (i >> 6) & 0x03; |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1614 | data->fan_div[2] = (w83781d_read_value(data, |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1615 | W83781D_REG_PIN) >> 6) & 0x03; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | if ((data->type != w83781d) && (data->type != as99127f)) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1617 | i = w83781d_read_value(data, W83781D_REG_VBAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | data->fan_div[0] |= (i >> 3) & 0x04; |
| 1619 | data->fan_div[1] |= (i >> 4) & 0x04; |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1620 | data->fan_div[2] |= (i >> 5) & 0x04; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | if ((data->type == w83782d) || (data->type == w83627hf)) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1623 | data->alarms = w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1624 | W83782D_REG_ALARM1) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1625 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1626 | W83782D_REG_ALARM2) << 8) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1627 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1628 | W83782D_REG_ALARM3) << 16); |
| 1629 | } else if (data->type == w83783s) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1630 | data->alarms = w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1631 | W83782D_REG_ALARM1) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1632 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1633 | W83782D_REG_ALARM2) << 8); |
| 1634 | } else { |
| 1635 | /* No real-time status registers, fall back to |
| 1636 | interrupt status registers */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1637 | data->alarms = w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1638 | W83781D_REG_ALARM1) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1639 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1640 | W83781D_REG_ALARM2) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1642 | i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | data->beep_enable = i >> 7; |
| 1644 | data->beep_mask = ((i & 0x7f) << 8) + |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1645 | w83781d_read_value(data, W83781D_REG_BEEP_INTS1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | if ((data->type != w83781d) && (data->type != as99127f)) { |
| 1647 | data->beep_mask |= |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1648 | w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | W83781D_REG_BEEP_INTS3) << 16; |
| 1650 | } |
| 1651 | data->last_updated = jiffies; |
| 1652 | data->valid = 1; |
| 1653 | } |
| 1654 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1655 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
| 1657 | return data; |
| 1658 | } |
| 1659 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1660 | /* return 1 if a supported chip is found, 0 otherwise */ |
| 1661 | static int __init |
| 1662 | w83781d_isa_found(unsigned short address) |
| 1663 | { |
| 1664 | int val, save, found = 0; |
| 1665 | |
| 1666 | if (!request_region(address, W83781D_EXTENT, "w83781d")) |
| 1667 | return 0; |
| 1668 | |
| 1669 | #define REALLY_SLOW_IO |
| 1670 | /* We need the timeouts for at least some W83781D-like |
| 1671 | chips. But only if we read 'undefined' registers. */ |
| 1672 | val = inb_p(address + 1); |
| 1673 | if (inb_p(address + 2) != val |
| 1674 | || inb_p(address + 3) != val |
| 1675 | || inb_p(address + 7) != val) { |
| 1676 | pr_debug("w83781d: Detection failed at step 1\n"); |
| 1677 | goto release; |
| 1678 | } |
| 1679 | #undef REALLY_SLOW_IO |
| 1680 | |
| 1681 | /* We should be able to change the 7 LSB of the address port. The |
| 1682 | MSB (busy flag) should be clear initially, set after the write. */ |
| 1683 | save = inb_p(address + W83781D_ADDR_REG_OFFSET); |
| 1684 | if (save & 0x80) { |
| 1685 | pr_debug("w83781d: Detection failed at step 2\n"); |
| 1686 | goto release; |
| 1687 | } |
| 1688 | val = ~save & 0x7f; |
| 1689 | outb_p(val, address + W83781D_ADDR_REG_OFFSET); |
| 1690 | if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) { |
| 1691 | outb_p(save, address + W83781D_ADDR_REG_OFFSET); |
| 1692 | pr_debug("w83781d: Detection failed at step 3\n"); |
| 1693 | goto release; |
| 1694 | } |
| 1695 | |
| 1696 | /* We found a device, now see if it could be a W83781D */ |
| 1697 | outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET); |
| 1698 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1699 | if (val & 0x80) { |
| 1700 | pr_debug("w83781d: Detection failed at step 4\n"); |
| 1701 | goto release; |
| 1702 | } |
| 1703 | outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET); |
| 1704 | save = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1705 | outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET); |
| 1706 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1707 | if ((!(save & 0x80) && (val != 0xa3)) |
| 1708 | || ((save & 0x80) && (val != 0x5c))) { |
| 1709 | pr_debug("w83781d: Detection failed at step 5\n"); |
| 1710 | goto release; |
| 1711 | } |
| 1712 | outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET); |
| 1713 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1714 | if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */ |
| 1715 | pr_debug("w83781d: Detection failed at step 6\n"); |
| 1716 | goto release; |
| 1717 | } |
| 1718 | |
| 1719 | /* The busy flag should be clear again */ |
| 1720 | if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) { |
| 1721 | pr_debug("w83781d: Detection failed at step 7\n"); |
| 1722 | goto release; |
| 1723 | } |
| 1724 | |
| 1725 | /* Determine the chip type */ |
| 1726 | outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET); |
| 1727 | save = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1728 | outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET); |
| 1729 | outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET); |
| 1730 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1731 | if ((val & 0xfe) == 0x10 /* W83781D */ |
| 1732 | || val == 0x30 /* W83782D */ |
| 1733 | || val == 0x21) /* W83627HF */ |
| 1734 | found = 1; |
| 1735 | |
| 1736 | if (found) |
| 1737 | pr_info("w83781d: Found a %s chip at %#x\n", |
| 1738 | val == 0x21 ? "W83627HF" : |
| 1739 | val == 0x30 ? "W83782D" : "W83781D", (int)address); |
| 1740 | |
| 1741 | release: |
| 1742 | release_region(address, W83781D_EXTENT); |
| 1743 | return found; |
| 1744 | } |
| 1745 | |
| 1746 | static int __init |
| 1747 | w83781d_isa_device_add(unsigned short address) |
| 1748 | { |
| 1749 | struct resource res = { |
| 1750 | .start = address, |
Jean Delvare | 15bde2f | 2007-08-29 10:39:57 +0200 | [diff] [blame] | 1751 | .end = address + W83781D_EXTENT - 1, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1752 | .name = "w83781d", |
| 1753 | .flags = IORESOURCE_IO, |
| 1754 | }; |
| 1755 | int err; |
| 1756 | |
| 1757 | pdev = platform_device_alloc("w83781d", address); |
| 1758 | if (!pdev) { |
| 1759 | err = -ENOMEM; |
| 1760 | printk(KERN_ERR "w83781d: Device allocation failed\n"); |
| 1761 | goto exit; |
| 1762 | } |
| 1763 | |
| 1764 | err = platform_device_add_resources(pdev, &res, 1); |
| 1765 | if (err) { |
| 1766 | printk(KERN_ERR "w83781d: Device resource addition failed " |
| 1767 | "(%d)\n", err); |
| 1768 | goto exit_device_put; |
| 1769 | } |
| 1770 | |
| 1771 | err = platform_device_add(pdev); |
| 1772 | if (err) { |
| 1773 | printk(KERN_ERR "w83781d: Device addition failed (%d)\n", |
| 1774 | err); |
| 1775 | goto exit_device_put; |
| 1776 | } |
| 1777 | |
| 1778 | return 0; |
| 1779 | |
| 1780 | exit_device_put: |
| 1781 | platform_device_put(pdev); |
| 1782 | exit: |
| 1783 | pdev = NULL; |
| 1784 | return err; |
| 1785 | } |
| 1786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | static int __init |
| 1788 | sensors_w83781d_init(void) |
| 1789 | { |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1790 | int res; |
| 1791 | |
| 1792 | res = i2c_add_driver(&w83781d_driver); |
| 1793 | if (res) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1794 | goto exit; |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1795 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1796 | if (w83781d_isa_found(isa_address)) { |
| 1797 | res = platform_driver_register(&w83781d_isa_driver); |
| 1798 | if (res) |
| 1799 | goto exit_unreg_i2c_driver; |
| 1800 | |
| 1801 | /* Sets global pdev as a side effect */ |
| 1802 | res = w83781d_isa_device_add(isa_address); |
| 1803 | if (res) |
| 1804 | goto exit_unreg_isa_driver; |
| 1805 | } |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1806 | |
| 1807 | return 0; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1808 | |
| 1809 | exit_unreg_isa_driver: |
| 1810 | platform_driver_unregister(&w83781d_isa_driver); |
| 1811 | exit_unreg_i2c_driver: |
| 1812 | i2c_del_driver(&w83781d_driver); |
| 1813 | exit: |
| 1814 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | static void __exit |
| 1818 | sensors_w83781d_exit(void) |
| 1819 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1820 | if (pdev) { |
| 1821 | platform_device_unregister(pdev); |
| 1822 | platform_driver_unregister(&w83781d_isa_driver); |
| 1823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | i2c_del_driver(&w83781d_driver); |
| 1825 | } |
| 1826 | |
| 1827 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " |
| 1828 | "Philip Edelbrock <phil@netroedge.com>, " |
| 1829 | "and Mark Studebaker <mdsxyz123@yahoo.com>"); |
| 1830 | MODULE_DESCRIPTION("W83781D driver"); |
| 1831 | MODULE_LICENSE("GPL"); |
| 1832 | |
| 1833 | module_init(sensors_w83781d_init); |
| 1834 | module_exit(sensors_w83781d_exit); |