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 | |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 484 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 485 | char *buf) |
| 486 | { |
| 487 | struct w83781d_data *data = w83781d_update_device(dev); |
| 488 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 489 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 490 | } |
| 491 | |
| 492 | /* The W83781D has a single alarm bit for temp2 and temp3 */ |
| 493 | static ssize_t show_temp3_alarm(struct device *dev, |
| 494 | struct device_attribute *attr, char *buf) |
| 495 | { |
| 496 | struct w83781d_data *data = w83781d_update_device(dev); |
| 497 | int bitnr = (data->type == w83781d) ? 5 : 13; |
| 498 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 499 | } |
| 500 | |
| 501 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 502 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 503 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 504 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 505 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 506 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 507 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10); |
| 508 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16); |
| 509 | static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17); |
| 510 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 511 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 512 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 513 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 514 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 515 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0); |
| 516 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 517 | 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] | 518 | { |
| 519 | struct w83781d_data *data = w83781d_update_device(dev); |
| 520 | return sprintf(buf, "%ld\n", |
| 521 | (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type)); |
| 522 | } |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 523 | 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] | 524 | { |
| 525 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 474d00a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 526 | return sprintf(buf, "%ld\n", (long)data->beep_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 530 | store_beep_mask(struct device *dev, struct device_attribute *attr, |
| 531 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 533 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 534 | u32 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | val = simple_strtoul(buf, NULL, 10); |
| 537 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 538 | mutex_lock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 539 | data->beep_mask = BEEP_MASK_TO_REG(val, data->type); |
| 540 | w83781d_write_value(data, W83781D_REG_BEEP_INTS1, |
| 541 | data->beep_mask & 0xff); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 542 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 543 | ((data->beep_mask >> 8) & 0x7f) |
| 544 | | data->beep_enable << 7); |
| 545 | if (data->type != w83781d && data->type != as99127f) { |
| 546 | w83781d_write_value(data, W83781D_REG_BEEP_INTS3, |
| 547 | ((data->beep_mask) >> 16) & 0xff); |
| 548 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 549 | mutex_unlock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return count; |
| 552 | } |
| 553 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 554 | static ssize_t |
| 555 | store_beep_enable(struct device *dev, struct device_attribute *attr, |
| 556 | const char *buf, size_t count) |
| 557 | { |
| 558 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 559 | u32 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 561 | val = simple_strtoul(buf, NULL, 10); |
| 562 | if (val != 0 && val != 1) |
| 563 | return -EINVAL; |
| 564 | |
| 565 | mutex_lock(&data->update_lock); |
| 566 | data->beep_enable = val; |
| 567 | val = w83781d_read_value(data, W83781D_REG_BEEP_INTS2) & 0x7f; |
| 568 | val |= data->beep_enable << 7; |
| 569 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, val); |
| 570 | mutex_unlock(&data->update_lock); |
| 571 | |
| 572 | return count; |
| 573 | } |
| 574 | |
| 575 | static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR, |
| 576 | show_beep_mask, store_beep_mask); |
| 577 | static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR, |
| 578 | show_beep_enable, store_beep_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 580 | static ssize_t show_beep(struct device *dev, struct device_attribute *attr, |
| 581 | char *buf) |
| 582 | { |
| 583 | struct w83781d_data *data = w83781d_update_device(dev); |
| 584 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 585 | return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1); |
| 586 | } |
| 587 | |
| 588 | static ssize_t |
| 589 | store_beep(struct device *dev, struct device_attribute *attr, |
| 590 | const char *buf, size_t count) |
| 591 | { |
| 592 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 593 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 594 | unsigned long bit; |
| 595 | u8 reg; |
| 596 | |
| 597 | bit = simple_strtoul(buf, NULL, 10); |
| 598 | if (bit & ~1) |
| 599 | return -EINVAL; |
| 600 | |
| 601 | mutex_lock(&data->update_lock); |
| 602 | if (bit) |
| 603 | data->beep_mask |= (1 << bitnr); |
| 604 | else |
| 605 | data->beep_mask &= ~(1 << bitnr); |
| 606 | |
| 607 | if (bitnr < 8) { |
| 608 | reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1); |
| 609 | if (bit) |
| 610 | reg |= (1 << bitnr); |
| 611 | else |
| 612 | reg &= ~(1 << bitnr); |
| 613 | w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg); |
| 614 | } else if (bitnr < 16) { |
| 615 | reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2); |
| 616 | if (bit) |
| 617 | reg |= (1 << (bitnr - 8)); |
| 618 | else |
| 619 | reg &= ~(1 << (bitnr - 8)); |
| 620 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg); |
| 621 | } else { |
| 622 | reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3); |
| 623 | if (bit) |
| 624 | reg |= (1 << (bitnr - 16)); |
| 625 | else |
| 626 | reg &= ~(1 << (bitnr - 16)); |
| 627 | w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg); |
| 628 | } |
| 629 | mutex_unlock(&data->update_lock); |
| 630 | |
| 631 | return count; |
| 632 | } |
| 633 | |
| 634 | /* The W83781D has a single beep bit for temp2 and temp3 */ |
| 635 | static ssize_t show_temp3_beep(struct device *dev, |
| 636 | struct device_attribute *attr, char *buf) |
| 637 | { |
| 638 | struct w83781d_data *data = w83781d_update_device(dev); |
| 639 | int bitnr = (data->type == w83781d) ? 5 : 13; |
| 640 | return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1); |
| 641 | } |
| 642 | |
| 643 | static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, |
| 644 | show_beep, store_beep, 0); |
| 645 | static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, |
| 646 | show_beep, store_beep, 1); |
| 647 | static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, |
| 648 | show_beep, store_beep, 2); |
| 649 | static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, |
| 650 | show_beep, store_beep, 3); |
| 651 | static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, |
| 652 | show_beep, store_beep, 8); |
| 653 | static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR, |
| 654 | show_beep, store_beep, 9); |
| 655 | static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR, |
| 656 | show_beep, store_beep, 10); |
| 657 | static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR, |
| 658 | show_beep, store_beep, 16); |
| 659 | static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR, |
| 660 | show_beep, store_beep, 17); |
| 661 | static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, |
| 662 | show_beep, store_beep, 6); |
| 663 | static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, |
| 664 | show_beep, store_beep, 7); |
| 665 | static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR, |
| 666 | show_beep, store_beep, 11); |
| 667 | static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, |
| 668 | show_beep, store_beep, 4); |
| 669 | static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, |
| 670 | show_beep, store_beep, 5); |
| 671 | static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, |
| 672 | show_temp3_beep, store_beep, 13); |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 675 | show_fan_div(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 677 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | struct w83781d_data *data = w83781d_update_device(dev); |
| 679 | return sprintf(buf, "%ld\n", |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 680 | (long) DIV_FROM_REG(data->fan_div[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Note: we save and restore the fan minimum here, because its value is |
| 684 | determined in part by the fan divisor. This follows the principle of |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 685 | 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] | 686 | because the divisor changed. */ |
| 687 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 688 | store_fan_div(struct device *dev, struct device_attribute *da, |
| 689 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 691 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 692 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | unsigned long min; |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 694 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | u8 reg; |
| 696 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 697 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 698 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
| 700 | /* Save fan_min */ |
| 701 | min = FAN_FROM_REG(data->fan_min[nr], |
| 702 | DIV_FROM_REG(data->fan_div[nr])); |
| 703 | |
| 704 | data->fan_div[nr] = DIV_TO_REG(val, data->type); |
| 705 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 706 | 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] | 707 | & (nr==0 ? 0xcf : 0x3f)) |
| 708 | | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6)); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 709 | 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] | 710 | |
| 711 | /* w83781d and as99127f don't have extended divisor bits */ |
| 712 | if (data->type != w83781d && data->type != as99127f) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 713 | reg = (w83781d_read_value(data, W83781D_REG_VBAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | & ~(1 << (5 + nr))) |
| 715 | | ((data->fan_div[nr] & 0x04) << (3 + nr)); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 716 | w83781d_write_value(data, W83781D_REG_VBAT, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* Restore fan_min */ |
| 720 | 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] | 721 | 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] | 722 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 723 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return count; |
| 725 | } |
| 726 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 727 | static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, |
| 728 | show_fan_div, store_fan_div, 0); |
| 729 | static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, |
| 730 | show_fan_div, store_fan_div, 1); |
| 731 | static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, |
| 732 | show_fan_div, store_fan_div, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 735 | show_pwm(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 737 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 739 | return sprintf(buf, "%d\n", (int)data->pwm[attr->index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 743 | show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
| 745 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 746 | return sprintf(buf, "%d\n", (int)data->pwm2_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 750 | store_pwm(struct device *dev, struct device_attribute *da, const char *buf, |
| 751 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 753 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 754 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 755 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | u32 val; |
| 757 | |
| 758 | val = simple_strtoul(buf, NULL, 10); |
| 759 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 760 | mutex_lock(&data->update_lock); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 761 | data->pwm[nr] = SENSORS_LIMIT(val, 0, 255); |
| 762 | w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 763 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | return count; |
| 765 | } |
| 766 | |
| 767 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 768 | store_pwm2_enable(struct device *dev, struct device_attribute *da, |
| 769 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 771 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | u32 val, reg; |
| 773 | |
| 774 | val = simple_strtoul(buf, NULL, 10); |
| 775 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 776 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
| 778 | switch (val) { |
| 779 | case 0: |
| 780 | case 1: |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 781 | reg = w83781d_read_value(data, W83781D_REG_PWMCLK12); |
| 782 | w83781d_write_value(data, W83781D_REG_PWMCLK12, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | (reg & 0xf7) | (val << 3)); |
| 784 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 785 | reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG); |
| 786 | w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | (reg & 0xef) | (!val << 4)); |
| 788 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 789 | data->pwm2_enable = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | break; |
| 791 | |
| 792 | default: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 793 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | return -EINVAL; |
| 795 | } |
| 796 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 797 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return count; |
| 799 | } |
| 800 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 801 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0); |
| 802 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1); |
| 803 | static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2); |
| 804 | static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3); |
| 805 | /* only PWM2 can be enabled/disabled */ |
| 806 | static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, |
| 807 | show_pwm2_enable, store_pwm2_enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 810 | show_sensor(struct device *dev, struct device_attribute *da, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 812 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | struct w83781d_data *data = w83781d_update_device(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 814 | return sprintf(buf, "%d\n", (int)data->sens[attr->index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | static ssize_t |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 818 | store_sensor(struct device *dev, struct device_attribute *da, |
| 819 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 821 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 822 | struct w83781d_data *data = dev_get_drvdata(dev); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 823 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | u32 val, tmp; |
| 825 | |
| 826 | val = simple_strtoul(buf, NULL, 10); |
| 827 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 828 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | switch (val) { |
| 831 | case 1: /* PII/Celeron diode */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 832 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
| 833 | w83781d_write_value(data, W83781D_REG_SCFG1, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 834 | tmp | BIT_SCFG1[nr]); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 835 | tmp = w83781d_read_value(data, W83781D_REG_SCFG2); |
| 836 | w83781d_write_value(data, W83781D_REG_SCFG2, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 837 | tmp | BIT_SCFG2[nr]); |
| 838 | data->sens[nr] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | break; |
| 840 | case 2: /* 3904 */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 841 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
| 842 | w83781d_write_value(data, W83781D_REG_SCFG1, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 843 | tmp | BIT_SCFG1[nr]); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 844 | tmp = w83781d_read_value(data, W83781D_REG_SCFG2); |
| 845 | w83781d_write_value(data, W83781D_REG_SCFG2, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 846 | tmp & ~BIT_SCFG2[nr]); |
| 847 | data->sens[nr] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | break; |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 849 | case W83781D_DEFAULT_BETA: |
| 850 | dev_warn(dev, "Sensor type %d is deprecated, please use 4 " |
| 851 | "instead\n", W83781D_DEFAULT_BETA); |
| 852 | /* fall through */ |
| 853 | case 4: /* thermistor */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 854 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
| 855 | w83781d_write_value(data, W83781D_REG_SCFG1, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 856 | tmp & ~BIT_SCFG1[nr]); |
| 857 | data->sens[nr] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | break; |
| 859 | default: |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 860 | dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n", |
| 861 | (long) val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 865 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | return count; |
| 867 | } |
| 868 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 869 | static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, |
| 870 | show_sensor, store_sensor, 0); |
| 871 | static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, |
Mark M. Hoffman | 393cdad | 2007-08-09 08:12:46 -0400 | [diff] [blame] | 872 | show_sensor, store_sensor, 1); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 873 | static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, |
Mark M. Hoffman | 393cdad | 2007-08-09 08:12:46 -0400 | [diff] [blame] | 874 | show_sensor, store_sensor, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 876 | /* I2C devices get this name attribute automatically, but for ISA devices |
| 877 | we must create it by ourselves. */ |
| 878 | static ssize_t |
| 879 | show_name(struct device *dev, struct device_attribute *devattr, char *buf) |
| 880 | { |
| 881 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 882 | return sprintf(buf, "%s\n", data->client.name); |
| 883 | } |
| 884 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | /* This function is called when: |
| 887 | * w83781d_driver is inserted (when this module is loaded), for each |
| 888 | available adapter |
| 889 | * when a new adapter is inserted (and w83781d_driver is still present) */ |
| 890 | static int |
| 891 | w83781d_attach_adapter(struct i2c_adapter *adapter) |
| 892 | { |
| 893 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 894 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 895 | return i2c_probe(adapter, &addr_data, w83781d_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* Assumes that adapter is of I2C, not ISA variety. |
| 899 | * OTHERWISE DON'T CALL THIS |
| 900 | */ |
| 901 | static int |
| 902 | w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind, |
| 903 | struct i2c_client *new_client) |
| 904 | { |
| 905 | int i, val1 = 0, id; |
| 906 | int err; |
| 907 | const char *client_name = ""; |
| 908 | struct w83781d_data *data = i2c_get_clientdata(new_client); |
| 909 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 910 | data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (!(data->lm75[0])) { |
| 912 | err = -ENOMEM; |
| 913 | goto ERROR_SC_0; |
| 914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
| 916 | id = i2c_adapter_id(adapter); |
| 917 | |
| 918 | if (force_subclients[0] == id && force_subclients[1] == address) { |
| 919 | for (i = 2; i <= 3; i++) { |
| 920 | if (force_subclients[i] < 0x48 || |
| 921 | force_subclients[i] > 0x4f) { |
| 922 | dev_err(&new_client->dev, "Invalid subclient " |
| 923 | "address %d; must be 0x48-0x4f\n", |
| 924 | force_subclients[i]); |
| 925 | err = -EINVAL; |
| 926 | goto ERROR_SC_1; |
| 927 | } |
| 928 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 929 | w83781d_write_value(data, W83781D_REG_I2C_SUBADDR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | (force_subclients[2] & 0x07) | |
| 931 | ((force_subclients[3] & 0x07) << 4)); |
| 932 | data->lm75[0]->addr = force_subclients[2]; |
| 933 | } else { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 934 | val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | data->lm75[0]->addr = 0x48 + (val1 & 0x07); |
| 936 | } |
| 937 | |
| 938 | if (kind != w83783s) { |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 939 | data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | if (!(data->lm75[1])) { |
| 941 | err = -ENOMEM; |
| 942 | goto ERROR_SC_1; |
| 943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | if (force_subclients[0] == id && |
| 946 | force_subclients[1] == address) { |
| 947 | data->lm75[1]->addr = force_subclients[3]; |
| 948 | } else { |
| 949 | data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07); |
| 950 | } |
| 951 | if (data->lm75[0]->addr == data->lm75[1]->addr) { |
| 952 | dev_err(&new_client->dev, |
| 953 | "Duplicate addresses 0x%x for subclients.\n", |
| 954 | data->lm75[0]->addr); |
| 955 | err = -EBUSY; |
| 956 | goto ERROR_SC_2; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (kind == w83781d) |
| 961 | client_name = "w83781d subclient"; |
| 962 | else if (kind == w83782d) |
| 963 | client_name = "w83782d subclient"; |
| 964 | else if (kind == w83783s) |
| 965 | client_name = "w83783s subclient"; |
| 966 | else if (kind == w83627hf) |
| 967 | client_name = "w83627hf subclient"; |
| 968 | else if (kind == as99127f) |
| 969 | client_name = "as99127f subclient"; |
| 970 | |
| 971 | for (i = 0; i <= 1; i++) { |
| 972 | /* store all data in w83781d */ |
| 973 | i2c_set_clientdata(data->lm75[i], NULL); |
| 974 | data->lm75[i]->adapter = adapter; |
| 975 | data->lm75[i]->driver = &w83781d_driver; |
| 976 | data->lm75[i]->flags = 0; |
| 977 | strlcpy(data->lm75[i]->name, client_name, |
| 978 | I2C_NAME_SIZE); |
| 979 | if ((err = i2c_attach_client(data->lm75[i]))) { |
| 980 | dev_err(&new_client->dev, "Subclient %d " |
| 981 | "registration at address 0x%x " |
| 982 | "failed.\n", i, data->lm75[i]->addr); |
| 983 | if (i == 1) |
| 984 | goto ERROR_SC_3; |
| 985 | goto ERROR_SC_2; |
| 986 | } |
| 987 | if (kind == w83783s) |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | return 0; |
| 992 | |
| 993 | /* Undo inits in case of errors */ |
| 994 | ERROR_SC_3: |
| 995 | i2c_detach_client(data->lm75[0]); |
| 996 | ERROR_SC_2: |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 997 | kfree(data->lm75[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | ERROR_SC_1: |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 999 | kfree(data->lm75[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | ERROR_SC_0: |
| 1001 | return err; |
| 1002 | } |
| 1003 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1004 | #define IN_UNIT_ATTRS(X) \ |
| 1005 | &sensor_dev_attr_in##X##_input.dev_attr.attr, \ |
| 1006 | &sensor_dev_attr_in##X##_min.dev_attr.attr, \ |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1007 | &sensor_dev_attr_in##X##_max.dev_attr.attr, \ |
| 1008 | &sensor_dev_attr_in##X##_alarm.dev_attr.attr, \ |
| 1009 | &sensor_dev_attr_in##X##_beep.dev_attr.attr |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1010 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1011 | #define FAN_UNIT_ATTRS(X) \ |
| 1012 | &sensor_dev_attr_fan##X##_input.dev_attr.attr, \ |
| 1013 | &sensor_dev_attr_fan##X##_min.dev_attr.attr, \ |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1014 | &sensor_dev_attr_fan##X##_div.dev_attr.attr, \ |
| 1015 | &sensor_dev_attr_fan##X##_alarm.dev_attr.attr, \ |
| 1016 | &sensor_dev_attr_fan##X##_beep.dev_attr.attr |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1017 | |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1018 | #define TEMP_UNIT_ATTRS(X) \ |
| 1019 | &sensor_dev_attr_temp##X##_input.dev_attr.attr, \ |
| 1020 | &sensor_dev_attr_temp##X##_max.dev_attr.attr, \ |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1021 | &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr, \ |
| 1022 | &sensor_dev_attr_temp##X##_alarm.dev_attr.attr, \ |
| 1023 | &sensor_dev_attr_temp##X##_beep.dev_attr.attr |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1024 | |
| 1025 | static struct attribute* w83781d_attributes[] = { |
| 1026 | IN_UNIT_ATTRS(0), |
| 1027 | IN_UNIT_ATTRS(2), |
| 1028 | IN_UNIT_ATTRS(3), |
| 1029 | IN_UNIT_ATTRS(4), |
| 1030 | IN_UNIT_ATTRS(5), |
| 1031 | IN_UNIT_ATTRS(6), |
| 1032 | FAN_UNIT_ATTRS(1), |
| 1033 | FAN_UNIT_ATTRS(2), |
| 1034 | FAN_UNIT_ATTRS(3), |
| 1035 | TEMP_UNIT_ATTRS(1), |
| 1036 | TEMP_UNIT_ATTRS(2), |
| 1037 | &dev_attr_cpu0_vid.attr, |
| 1038 | &dev_attr_vrm.attr, |
| 1039 | &dev_attr_alarms.attr, |
| 1040 | &dev_attr_beep_mask.attr, |
| 1041 | &dev_attr_beep_enable.attr, |
| 1042 | NULL |
| 1043 | }; |
| 1044 | static const struct attribute_group w83781d_group = { |
| 1045 | .attrs = w83781d_attributes, |
| 1046 | }; |
| 1047 | |
| 1048 | static struct attribute *w83781d_attributes_opt[] = { |
| 1049 | IN_UNIT_ATTRS(1), |
| 1050 | IN_UNIT_ATTRS(7), |
| 1051 | IN_UNIT_ATTRS(8), |
| 1052 | TEMP_UNIT_ATTRS(3), |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1053 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1054 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1055 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1056 | &sensor_dev_attr_pwm4.dev_attr.attr, |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1057 | &dev_attr_pwm2_enable.attr, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1058 | &sensor_dev_attr_temp1_type.dev_attr.attr, |
| 1059 | &sensor_dev_attr_temp2_type.dev_attr.attr, |
| 1060 | &sensor_dev_attr_temp3_type.dev_attr.attr, |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1061 | NULL |
| 1062 | }; |
| 1063 | static const struct attribute_group w83781d_group_opt = { |
| 1064 | .attrs = w83781d_attributes_opt, |
| 1065 | }; |
| 1066 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1067 | /* No clean up is done on error, it's up to the caller */ |
| 1068 | static int |
| 1069 | w83781d_create_files(struct device *dev, int kind, int is_isa) |
| 1070 | { |
| 1071 | int err; |
| 1072 | |
| 1073 | if ((err = sysfs_create_group(&dev->kobj, &w83781d_group))) |
| 1074 | return err; |
| 1075 | |
| 1076 | if (kind != w83783s) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1077 | if ((err = device_create_file(dev, |
| 1078 | &sensor_dev_attr_in1_input.dev_attr)) |
| 1079 | || (err = device_create_file(dev, |
| 1080 | &sensor_dev_attr_in1_min.dev_attr)) |
| 1081 | || (err = device_create_file(dev, |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1082 | &sensor_dev_attr_in1_max.dev_attr)) |
| 1083 | || (err = device_create_file(dev, |
| 1084 | &sensor_dev_attr_in1_alarm.dev_attr)) |
| 1085 | || (err = device_create_file(dev, |
| 1086 | &sensor_dev_attr_in1_beep.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1087 | return err; |
| 1088 | } |
| 1089 | if (kind != as99127f && kind != w83781d && kind != w83783s) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1090 | if ((err = device_create_file(dev, |
| 1091 | &sensor_dev_attr_in7_input.dev_attr)) |
| 1092 | || (err = device_create_file(dev, |
| 1093 | &sensor_dev_attr_in7_min.dev_attr)) |
| 1094 | || (err = device_create_file(dev, |
| 1095 | &sensor_dev_attr_in7_max.dev_attr)) |
| 1096 | || (err = device_create_file(dev, |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1097 | &sensor_dev_attr_in7_alarm.dev_attr)) |
| 1098 | || (err = device_create_file(dev, |
| 1099 | &sensor_dev_attr_in7_beep.dev_attr)) |
| 1100 | || (err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1101 | &sensor_dev_attr_in8_input.dev_attr)) |
| 1102 | || (err = device_create_file(dev, |
| 1103 | &sensor_dev_attr_in8_min.dev_attr)) |
| 1104 | || (err = device_create_file(dev, |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1105 | &sensor_dev_attr_in8_max.dev_attr)) |
| 1106 | || (err = device_create_file(dev, |
| 1107 | &sensor_dev_attr_in8_alarm.dev_attr)) |
| 1108 | || (err = device_create_file(dev, |
| 1109 | &sensor_dev_attr_in8_beep.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1110 | return err; |
| 1111 | } |
| 1112 | if (kind != w83783s) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1113 | if ((err = device_create_file(dev, |
| 1114 | &sensor_dev_attr_temp3_input.dev_attr)) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1115 | || (err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1116 | &sensor_dev_attr_temp3_max.dev_attr)) |
| 1117 | || (err = device_create_file(dev, |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1118 | &sensor_dev_attr_temp3_max_hyst.dev_attr)) |
| 1119 | || (err = device_create_file(dev, |
| 1120 | &sensor_dev_attr_temp3_alarm.dev_attr)) |
| 1121 | || (err = device_create_file(dev, |
| 1122 | &sensor_dev_attr_temp3_beep.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1123 | return err; |
Jean Delvare | 7d4a137 | 2007-10-08 18:29:43 +0200 | [diff] [blame^] | 1124 | |
| 1125 | if (kind != w83781d) |
| 1126 | err = sysfs_chmod_file(&dev->kobj, |
| 1127 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1128 | S_IRUGO | S_IWUSR); |
| 1129 | if (err) |
| 1130 | return err; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | if (kind != w83781d && kind != as99127f) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1134 | if ((err = device_create_file(dev, |
| 1135 | &sensor_dev_attr_pwm1.dev_attr)) |
| 1136 | || (err = device_create_file(dev, |
| 1137 | &sensor_dev_attr_pwm2.dev_attr)) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1138 | || (err = device_create_file(dev, &dev_attr_pwm2_enable))) |
| 1139 | return err; |
| 1140 | } |
| 1141 | if (kind == w83782d && !is_isa) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1142 | if ((err = device_create_file(dev, |
| 1143 | &sensor_dev_attr_pwm3.dev_attr)) |
| 1144 | || (err = device_create_file(dev, |
| 1145 | &sensor_dev_attr_pwm4.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1146 | return err; |
| 1147 | } |
| 1148 | |
| 1149 | if (kind != as99127f && kind != w83781d) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1150 | if ((err = device_create_file(dev, |
| 1151 | &sensor_dev_attr_temp1_type.dev_attr)) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1152 | || (err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1153 | &sensor_dev_attr_temp2_type.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1154 | return err; |
| 1155 | if (kind != w83783s) { |
| 1156 | if ((err = device_create_file(dev, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1157 | &sensor_dev_attr_temp3_type.dev_attr))) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1158 | return err; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | if (is_isa) { |
| 1163 | err = device_create_file(&pdev->dev, &dev_attr_name); |
| 1164 | if (err) |
| 1165 | return err; |
| 1166 | } |
| 1167 | |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | static int |
| 1172 | w83781d_detect(struct i2c_adapter *adapter, int address, int kind) |
| 1173 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1174 | int val1 = 0, val2; |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1175 | struct i2c_client *client; |
| 1176 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | struct w83781d_data *data; |
| 1178 | int err; |
| 1179 | const char *client_name = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | enum vendor { winbond, asus } vendid; |
| 1181 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1182 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | err = -EINVAL; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1184 | goto ERROR1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* OK. For now, we presume we have a valid client. We now create the |
| 1188 | client structure, even though we cannot fill it completely yet. |
| 1189 | But it allows us to access w83781d_{read,write}_value. */ |
| 1190 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 1191 | if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | err = -ENOMEM; |
| 1193 | goto ERROR1; |
| 1194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1196 | client = &data->client; |
| 1197 | i2c_set_clientdata(client, data); |
| 1198 | client->addr = address; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1199 | mutex_init(&data->lock); |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1200 | client->adapter = adapter; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1201 | client->driver = &w83781d_driver; |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1202 | dev = &client->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | /* Now, we do the remaining detection. */ |
| 1205 | |
| 1206 | /* The w8378?d may be stuck in some other bank than bank 0. This may |
| 1207 | make reading other information impossible. Specify a force=... or |
| 1208 | force_*=... parameter, and the Winbond will be reset to the right |
| 1209 | bank. */ |
| 1210 | if (kind < 0) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1211 | if (w83781d_read_value(data, W83781D_REG_CONFIG) & 0x80) { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1212 | dev_dbg(&adapter->dev, "Detection of w83781d chip " |
| 1213 | "failed at step 3\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | err = -ENODEV; |
| 1215 | goto ERROR2; |
| 1216 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1217 | val1 = w83781d_read_value(data, W83781D_REG_BANK); |
| 1218 | val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | /* Check for Winbond or Asus ID if in bank 0 */ |
| 1220 | if ((!(val1 & 0x07)) && |
| 1221 | (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3)) |
| 1222 | || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1223 | dev_dbg(&adapter->dev, "Detection of w83781d chip " |
| 1224 | "failed at step 4\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | err = -ENODEV; |
| 1226 | goto ERROR2; |
| 1227 | } |
| 1228 | /* If Winbond SMBus, check address at 0x48. |
| 1229 | Asus doesn't support, except for as99127f rev.2 */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1230 | if ((!(val1 & 0x80) && (val2 == 0xa3)) || |
| 1231 | ((val1 & 0x80) && (val2 == 0x5c))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | if (w83781d_read_value |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1233 | (data, W83781D_REG_I2C_ADDR) != address) { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1234 | dev_dbg(&adapter->dev, "Detection of w83781d " |
| 1235 | "chip failed at step 5\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | err = -ENODEV; |
| 1237 | goto ERROR2; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /* We have either had a force parameter, or we have already detected the |
| 1243 | Winbond. Put it now into bank 0 and Vendor ID High Byte */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1244 | w83781d_write_value(data, W83781D_REG_BANK, |
| 1245 | (w83781d_read_value(data, W83781D_REG_BANK) |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1246 | & 0x78) | 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
| 1248 | /* Determine the chip type. */ |
| 1249 | if (kind <= 0) { |
| 1250 | /* get vendor ID */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1251 | val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | if (val2 == 0x5c) |
| 1253 | vendid = winbond; |
| 1254 | else if (val2 == 0x12) |
| 1255 | vendid = asus; |
| 1256 | else { |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1257 | dev_dbg(&adapter->dev, "w83781d chip vendor is " |
| 1258 | "neither Winbond nor Asus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | err = -ENODEV; |
| 1260 | goto ERROR2; |
| 1261 | } |
| 1262 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1263 | val1 = w83781d_read_value(data, W83781D_REG_WCHIPID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond) |
| 1265 | kind = w83781d; |
| 1266 | else if (val1 == 0x30 && vendid == winbond) |
| 1267 | kind = w83782d; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1268 | else if (val1 == 0x40 && vendid == winbond && address == 0x2d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | kind = w83783s; |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1270 | else if (val1 == 0x21 && vendid == winbond) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | kind = w83627hf; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1272 | else if (val1 == 0x31 && address >= 0x28) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | kind = as99127f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | else { |
| 1275 | if (kind == 0) |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1276 | dev_warn(&adapter->dev, "Ignoring 'force' " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | "parameter for unknown chip at " |
Jean Delvare | bd452e6 | 2006-10-13 17:03:42 +0200 | [diff] [blame] | 1278 | "address 0x%02x\n", address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | err = -EINVAL; |
| 1280 | goto ERROR2; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | if (kind == w83781d) { |
| 1285 | client_name = "w83781d"; |
| 1286 | } else if (kind == w83782d) { |
| 1287 | client_name = "w83782d"; |
| 1288 | } else if (kind == w83783s) { |
| 1289 | client_name = "w83783s"; |
| 1290 | } else if (kind == w83627hf) { |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1291 | client_name = "w83627hf"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } else if (kind == as99127f) { |
| 1293 | client_name = "as99127f"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /* Fill in the remaining client fields and put into the global list */ |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1297 | strlcpy(client->name, client_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | data->type = kind; |
| 1299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | /* Tell the I2C layer a new client has arrived */ |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1301 | if ((err = i2c_attach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | goto ERROR2; |
| 1303 | |
| 1304 | /* attach secondary i2c lm75-like clients */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1305 | if ((err = w83781d_detect_subclients(adapter, address, |
| 1306 | kind, client))) |
| 1307 | goto ERROR3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
| 1309 | /* Initialize the chip */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1310 | w83781d_init_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | /* Register sysfs hooks */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1313 | err = w83781d_create_files(dev, kind, 0); |
| 1314 | if (err) |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1315 | goto ERROR4; |
| 1316 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1317 | data->hwmon_dev = hwmon_device_register(dev); |
| 1318 | if (IS_ERR(data->hwmon_dev)) { |
| 1319 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1320 | goto ERROR4; |
| 1321 | } |
| 1322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | return 0; |
| 1324 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1325 | ERROR4: |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1326 | sysfs_remove_group(&dev->kobj, &w83781d_group); |
| 1327 | sysfs_remove_group(&dev->kobj, &w83781d_group_opt); |
| 1328 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1329 | if (data->lm75[1]) { |
| 1330 | i2c_detach_client(data->lm75[1]); |
| 1331 | kfree(data->lm75[1]); |
| 1332 | } |
| 1333 | if (data->lm75[0]) { |
| 1334 | i2c_detach_client(data->lm75[0]); |
| 1335 | kfree(data->lm75[0]); |
| 1336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | ERROR3: |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1338 | i2c_detach_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | ERROR2: |
| 1340 | kfree(data); |
| 1341 | ERROR1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | return err; |
| 1343 | } |
| 1344 | |
| 1345 | static int |
| 1346 | w83781d_detach_client(struct i2c_client *client) |
| 1347 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1348 | struct w83781d_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | int err; |
| 1350 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1351 | /* main client */ |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1352 | if (data) { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1353 | hwmon_device_unregister(data->hwmon_dev); |
Jim Cromie | 311ce2e | 2006-09-24 21:22:52 +0200 | [diff] [blame] | 1354 | sysfs_remove_group(&client->dev.kobj, &w83781d_group); |
| 1355 | sysfs_remove_group(&client->dev.kobj, &w83781d_group_opt); |
| 1356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 1358 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1361 | /* main client */ |
| 1362 | if (data) |
| 1363 | kfree(data); |
| 1364 | |
| 1365 | /* subclient */ |
| 1366 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1372 | static int __devinit |
| 1373 | w83781d_isa_probe(struct platform_device *pdev) |
| 1374 | { |
| 1375 | int err, reg; |
| 1376 | struct w83781d_data *data; |
| 1377 | struct resource *res; |
| 1378 | const char *name; |
| 1379 | |
| 1380 | /* Reserve the ISA region */ |
| 1381 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 1382 | if (!request_region(res->start, W83781D_EXTENT, "w83781d")) { |
| 1383 | err = -EBUSY; |
| 1384 | goto exit; |
| 1385 | } |
| 1386 | |
| 1387 | if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) { |
| 1388 | err = -ENOMEM; |
| 1389 | goto exit_release_region; |
| 1390 | } |
| 1391 | mutex_init(&data->lock); |
| 1392 | data->client.addr = res->start; |
| 1393 | i2c_set_clientdata(&data->client, data); |
| 1394 | platform_set_drvdata(pdev, data); |
| 1395 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1396 | reg = w83781d_read_value(data, W83781D_REG_WCHIPID); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1397 | switch (reg) { |
| 1398 | case 0x21: |
| 1399 | data->type = w83627hf; |
| 1400 | name = "w83627hf"; |
| 1401 | break; |
| 1402 | case 0x30: |
| 1403 | data->type = w83782d; |
| 1404 | name = "w83782d"; |
| 1405 | break; |
| 1406 | default: |
| 1407 | data->type = w83781d; |
| 1408 | name = "w83781d"; |
| 1409 | } |
| 1410 | strlcpy(data->client.name, name, I2C_NAME_SIZE); |
| 1411 | |
| 1412 | /* Initialize the W83781D chip */ |
| 1413 | w83781d_init_device(&pdev->dev); |
| 1414 | |
| 1415 | /* Register sysfs hooks */ |
| 1416 | err = w83781d_create_files(&pdev->dev, data->type, 1); |
| 1417 | if (err) |
| 1418 | goto exit_remove_files; |
| 1419 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1420 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 1421 | if (IS_ERR(data->hwmon_dev)) { |
| 1422 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1423 | goto exit_remove_files; |
| 1424 | } |
| 1425 | |
| 1426 | return 0; |
| 1427 | |
| 1428 | exit_remove_files: |
| 1429 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group); |
| 1430 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt); |
| 1431 | device_remove_file(&pdev->dev, &dev_attr_name); |
| 1432 | kfree(data); |
| 1433 | exit_release_region: |
| 1434 | release_region(res->start, W83781D_EXTENT); |
| 1435 | exit: |
| 1436 | return err; |
| 1437 | } |
| 1438 | |
| 1439 | static int __devexit |
| 1440 | w83781d_isa_remove(struct platform_device *pdev) |
| 1441 | { |
| 1442 | struct w83781d_data *data = platform_get_drvdata(pdev); |
| 1443 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1444 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1445 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group); |
| 1446 | sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt); |
| 1447 | device_remove_file(&pdev->dev, &dev_attr_name); |
| 1448 | release_region(data->client.addr, W83781D_EXTENT); |
| 1449 | kfree(data); |
| 1450 | |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | /* The SMBus locks itself, usually, but nothing may access the Winbond between |
| 1455 | bank switches. ISA access must always be locked explicitly! |
| 1456 | We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks, |
| 1457 | would slow down the W83781D access and should not be necessary. |
| 1458 | There are some ugly typecasts here, but the good news is - they should |
| 1459 | nowhere else be necessary! */ |
| 1460 | static int |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1461 | w83781d_read_value(struct w83781d_data *data, u16 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1463 | struct i2c_client *client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | int res, word_sized, bank; |
| 1465 | struct i2c_client *cl; |
| 1466 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1467 | mutex_lock(&data->lock); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1468 | if (!client->driver) { /* ISA device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | word_sized = (((reg & 0xff00) == 0x100) |
| 1470 | || ((reg & 0xff00) == 0x200)) |
| 1471 | && (((reg & 0x00ff) == 0x50) |
| 1472 | || ((reg & 0x00ff) == 0x53) |
| 1473 | || ((reg & 0x00ff) == 0x55)); |
| 1474 | if (reg & 0xff00) { |
| 1475 | outb_p(W83781D_REG_BANK, |
| 1476 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1477 | outb_p(reg >> 8, |
| 1478 | client->addr + W83781D_DATA_REG_OFFSET); |
| 1479 | } |
| 1480 | outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET); |
| 1481 | res = inb_p(client->addr + W83781D_DATA_REG_OFFSET); |
| 1482 | if (word_sized) { |
| 1483 | outb_p((reg & 0xff) + 1, |
| 1484 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1485 | res = |
| 1486 | (res << 8) + inb_p(client->addr + |
| 1487 | W83781D_DATA_REG_OFFSET); |
| 1488 | } |
| 1489 | if (reg & 0xff00) { |
| 1490 | outb_p(W83781D_REG_BANK, |
| 1491 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1492 | outb_p(0, client->addr + W83781D_DATA_REG_OFFSET); |
| 1493 | } |
| 1494 | } else { |
| 1495 | bank = (reg >> 8) & 0x0f; |
| 1496 | if (bank > 2) |
| 1497 | /* switch banks */ |
| 1498 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, |
| 1499 | bank); |
| 1500 | if (bank == 0 || bank > 2) { |
| 1501 | res = i2c_smbus_read_byte_data(client, reg & 0xff); |
| 1502 | } else { |
| 1503 | /* switch to subclient */ |
| 1504 | cl = data->lm75[bank - 1]; |
| 1505 | /* convert from ISA to LM75 I2C addresses */ |
| 1506 | switch (reg & 0xff) { |
| 1507 | case 0x50: /* TEMP */ |
| 1508 | res = swab16(i2c_smbus_read_word_data(cl, 0)); |
| 1509 | break; |
| 1510 | case 0x52: /* CONFIG */ |
| 1511 | res = i2c_smbus_read_byte_data(cl, 1); |
| 1512 | break; |
| 1513 | case 0x53: /* HYST */ |
| 1514 | res = swab16(i2c_smbus_read_word_data(cl, 2)); |
| 1515 | break; |
| 1516 | case 0x55: /* OVER */ |
| 1517 | default: |
| 1518 | res = swab16(i2c_smbus_read_word_data(cl, 3)); |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | if (bank > 2) |
| 1523 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0); |
| 1524 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1525 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return res; |
| 1527 | } |
| 1528 | |
| 1529 | static int |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1530 | w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1532 | struct i2c_client *client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | int word_sized, bank; |
| 1534 | struct i2c_client *cl; |
| 1535 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1536 | mutex_lock(&data->lock); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1537 | if (!client->driver) { /* ISA device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | word_sized = (((reg & 0xff00) == 0x100) |
| 1539 | || ((reg & 0xff00) == 0x200)) |
| 1540 | && (((reg & 0x00ff) == 0x53) |
| 1541 | || ((reg & 0x00ff) == 0x55)); |
| 1542 | if (reg & 0xff00) { |
| 1543 | outb_p(W83781D_REG_BANK, |
| 1544 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1545 | outb_p(reg >> 8, |
| 1546 | client->addr + W83781D_DATA_REG_OFFSET); |
| 1547 | } |
| 1548 | outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET); |
| 1549 | if (word_sized) { |
| 1550 | outb_p(value >> 8, |
| 1551 | client->addr + W83781D_DATA_REG_OFFSET); |
| 1552 | outb_p((reg & 0xff) + 1, |
| 1553 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1554 | } |
| 1555 | outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET); |
| 1556 | if (reg & 0xff00) { |
| 1557 | outb_p(W83781D_REG_BANK, |
| 1558 | client->addr + W83781D_ADDR_REG_OFFSET); |
| 1559 | outb_p(0, client->addr + W83781D_DATA_REG_OFFSET); |
| 1560 | } |
| 1561 | } else { |
| 1562 | bank = (reg >> 8) & 0x0f; |
| 1563 | if (bank > 2) |
| 1564 | /* switch banks */ |
| 1565 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, |
| 1566 | bank); |
| 1567 | if (bank == 0 || bank > 2) { |
| 1568 | i2c_smbus_write_byte_data(client, reg & 0xff, |
| 1569 | value & 0xff); |
| 1570 | } else { |
| 1571 | /* switch to subclient */ |
| 1572 | cl = data->lm75[bank - 1]; |
| 1573 | /* convert from ISA to LM75 I2C addresses */ |
| 1574 | switch (reg & 0xff) { |
| 1575 | case 0x52: /* CONFIG */ |
| 1576 | i2c_smbus_write_byte_data(cl, 1, value & 0xff); |
| 1577 | break; |
| 1578 | case 0x53: /* HYST */ |
| 1579 | i2c_smbus_write_word_data(cl, 2, swab16(value)); |
| 1580 | break; |
| 1581 | case 0x55: /* OVER */ |
| 1582 | i2c_smbus_write_word_data(cl, 3, swab16(value)); |
| 1583 | break; |
| 1584 | } |
| 1585 | } |
| 1586 | if (bank > 2) |
| 1587 | i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0); |
| 1588 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1589 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | return 0; |
| 1591 | } |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | static void |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1594 | w83781d_init_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1596 | struct w83781d_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | int i, p; |
| 1598 | int type = data->type; |
| 1599 | u8 tmp; |
| 1600 | |
Jean Delvare | f5f8d38 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1601 | if (type == w83627hf) |
| 1602 | dev_info(dev, "The W83627HF chip is better supported by the " |
| 1603 | "w83627hf driver, support will be dropped from the " |
| 1604 | "w83781d driver soon\n"); |
| 1605 | |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1606 | if (reset && type != as99127f) { /* this resets registers we don't have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | documentation for on the as99127f */ |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1608 | /* Resetting the chip has been the default for a long time, |
| 1609 | but it causes the BIOS initializations (fan clock dividers, |
| 1610 | thermal sensor types...) to be lost, so it is now optional. |
| 1611 | It might even go away if nobody reports it as being useful, |
| 1612 | as I see very little reason why this would be needed at |
| 1613 | all. */ |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1614 | dev_info(dev, "If reset=1 solved a problem you were " |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1615 | "having, please report!\n"); |
| 1616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | /* save these registers */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1618 | i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG); |
| 1619 | p = w83781d_read_value(data, W83781D_REG_PWMCLK12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | /* Reset all except Watchdog values and last conversion values |
| 1621 | This sets fan-divs to 2, among others */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1622 | w83781d_write_value(data, W83781D_REG_CONFIG, 0x80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | /* Restore the registers and disable power-on abnormal beep. |
| 1624 | This saves FAN 1/2/3 input/output values set by BIOS. */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1625 | w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80); |
| 1626 | w83781d_write_value(data, W83781D_REG_PWMCLK12, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | /* Disable master beep-enable (reset turns it on). |
| 1628 | Individual beep_mask should be reset to off but for some reason |
| 1629 | disabling this bit helps some people not get beeped */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1630 | w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1633 | /* Disable power-on abnormal beep, as advised by the datasheet. |
| 1634 | Already done if reset=1. */ |
| 1635 | if (init && !reset && type != as99127f) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1636 | i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG); |
| 1637 | w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80); |
Jean Delvare | fabddcd | 2006-02-05 23:26:51 +0100 | [diff] [blame] | 1638 | } |
| 1639 | |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 1640 | data->vrm = vid_which_vrm(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
| 1642 | if ((type != w83781d) && (type != as99127f)) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1643 | tmp = w83781d_read_value(data, W83781D_REG_SCFG1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | for (i = 1; i <= 3; i++) { |
| 1645 | if (!(tmp & BIT_SCFG1[i - 1])) { |
Jean Delvare | b26f933 | 2007-08-16 14:30:01 +0200 | [diff] [blame] | 1646 | data->sens[i - 1] = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } else { |
| 1648 | if (w83781d_read_value |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1649 | (data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | W83781D_REG_SCFG2) & BIT_SCFG2[i - 1]) |
| 1651 | data->sens[i - 1] = 1; |
| 1652 | else |
| 1653 | data->sens[i - 1] = 2; |
| 1654 | } |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1655 | if (type == w83783s && i == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | break; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | if (init && type != as99127f) { |
| 1661 | /* Enable temp2 */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1662 | tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | if (tmp & 0x01) { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1664 | dev_warn(dev, "Enabling temp2, readings " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | "might not make sense\n"); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1666 | w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | tmp & 0xfe); |
| 1668 | } |
| 1669 | |
| 1670 | /* Enable temp3 */ |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1671 | if (type != w83783s) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1672 | tmp = w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | W83781D_REG_TEMP3_CONFIG); |
| 1674 | if (tmp & 0x01) { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1675 | dev_warn(dev, "Enabling temp3, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | "readings might not make sense\n"); |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1677 | w83781d_write_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | W83781D_REG_TEMP3_CONFIG, tmp & 0xfe); |
| 1679 | } |
| 1680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* Start monitoring */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1684 | w83781d_write_value(data, W83781D_REG_CONFIG, |
| 1685 | (w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | W83781D_REG_CONFIG) & 0xf7) |
| 1687 | | 0x01); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1688 | |
| 1689 | /* A few vars need to be filled upon startup */ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1690 | for (i = 0; i < 3; i++) { |
| 1691 | data->fan_min[i] = w83781d_read_value(data, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1692 | W83781D_REG_FAN_MIN(i)); |
| 1693 | } |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1694 | |
| 1695 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | static struct w83781d_data *w83781d_update_device(struct device *dev) |
| 1699 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1700 | struct w83781d_data *data = dev_get_drvdata(dev); |
| 1701 | struct i2c_client *client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | int i; |
| 1703 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1704 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | |
| 1706 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 1707 | || !data->valid) { |
| 1708 | dev_dbg(dev, "Starting device update\n"); |
| 1709 | |
| 1710 | for (i = 0; i <= 8; i++) { |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1711 | if (data->type == w83783s && i == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | continue; /* 783S has no in1 */ |
| 1713 | data->in[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1714 | w83781d_read_value(data, W83781D_REG_IN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | data->in_min[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1716 | w83781d_read_value(data, W83781D_REG_IN_MIN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | data->in_max[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1718 | w83781d_read_value(data, W83781D_REG_IN_MAX(i)); |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1719 | if ((data->type != w83782d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | && (data->type != w83627hf) && (i == 6)) |
| 1721 | break; |
| 1722 | } |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1723 | for (i = 0; i < 3; i++) { |
| 1724 | data->fan[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1725 | w83781d_read_value(data, W83781D_REG_FAN(i)); |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1726 | data->fan_min[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1727 | w83781d_read_value(data, W83781D_REG_FAN_MIN(i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | } |
| 1729 | if (data->type != w83781d && data->type != as99127f) { |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1730 | for (i = 0; i < 4; i++) { |
| 1731 | data->pwm[i] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1732 | w83781d_read_value(data, |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1733 | W83781D_REG_PWM[i]); |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1734 | if ((data->type != w83782d || !client->driver) |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1735 | && i == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | break; |
| 1737 | } |
| 1738 | /* Only PWM2 can be disabled */ |
Jean Delvare | 3487533 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1739 | data->pwm2_enable = (w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | W83781D_REG_PWMCLK12) & 0x08) >> 3; |
| 1741 | } |
| 1742 | |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1743 | data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | data->temp_max = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1745 | w83781d_read_value(data, W83781D_REG_TEMP_OVER(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | data->temp_max_hyst = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1747 | w83781d_read_value(data, W83781D_REG_TEMP_HYST(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | data->temp_add[0] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1749 | w83781d_read_value(data, W83781D_REG_TEMP(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | data->temp_max_add[0] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1751 | w83781d_read_value(data, W83781D_REG_TEMP_OVER(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | data->temp_max_hyst_add[0] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1753 | w83781d_read_value(data, W83781D_REG_TEMP_HYST(2)); |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1754 | if (data->type != w83783s) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | data->temp_add[1] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1756 | w83781d_read_value(data, W83781D_REG_TEMP(3)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | data->temp_max_add[1] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1758 | w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | W83781D_REG_TEMP_OVER(3)); |
| 1760 | data->temp_max_hyst_add[1] = |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1761 | w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | W83781D_REG_TEMP_HYST(3)); |
| 1763 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1764 | i = w83781d_read_value(data, W83781D_REG_VID_FANDIV); |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1765 | data->vid = i & 0x0f; |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1766 | data->vid |= (w83781d_read_value(data, |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1767 | W83781D_REG_CHIPID) & 0x01) << 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | data->fan_div[0] = (i >> 4) & 0x03; |
| 1769 | data->fan_div[1] = (i >> 6) & 0x03; |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1770 | data->fan_div[2] = (w83781d_read_value(data, |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1771 | W83781D_REG_PIN) >> 6) & 0x03; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | if ((data->type != w83781d) && (data->type != as99127f)) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1773 | i = w83781d_read_value(data, W83781D_REG_VBAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | data->fan_div[0] |= (i >> 3) & 0x04; |
| 1775 | data->fan_div[1] |= (i >> 4) & 0x04; |
Jean Delvare | 7c7a530 | 2005-06-16 19:24:14 +0200 | [diff] [blame] | 1776 | data->fan_div[2] |= (i >> 5) & 0x04; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | if ((data->type == w83782d) || (data->type == w83627hf)) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1779 | data->alarms = w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1780 | W83782D_REG_ALARM1) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1781 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1782 | W83782D_REG_ALARM2) << 8) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1783 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1784 | W83782D_REG_ALARM3) << 16); |
| 1785 | } else if (data->type == w83783s) { |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1786 | data->alarms = w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1787 | W83782D_REG_ALARM1) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1788 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1789 | W83782D_REG_ALARM2) << 8); |
| 1790 | } else { |
| 1791 | /* No real-time status registers, fall back to |
| 1792 | interrupt status registers */ |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1793 | data->alarms = w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1794 | W83781D_REG_ALARM1) |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1795 | | (w83781d_read_value(data, |
Jean Delvare | c7f5d7e | 2006-02-05 23:13:48 +0100 | [diff] [blame] | 1796 | W83781D_REG_ALARM2) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | } |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1798 | i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | data->beep_enable = i >> 7; |
| 1800 | data->beep_mask = ((i & 0x7f) << 8) + |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1801 | w83781d_read_value(data, W83781D_REG_BEEP_INTS1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | if ((data->type != w83781d) && (data->type != as99127f)) { |
| 1803 | data->beep_mask |= |
Jean Delvare | 31b8dc4 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 1804 | w83781d_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | W83781D_REG_BEEP_INTS3) << 16; |
| 1806 | } |
| 1807 | data->last_updated = jiffies; |
| 1808 | data->valid = 1; |
| 1809 | } |
| 1810 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1811 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
| 1813 | return data; |
| 1814 | } |
| 1815 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1816 | /* return 1 if a supported chip is found, 0 otherwise */ |
| 1817 | static int __init |
| 1818 | w83781d_isa_found(unsigned short address) |
| 1819 | { |
| 1820 | int val, save, found = 0; |
| 1821 | |
| 1822 | if (!request_region(address, W83781D_EXTENT, "w83781d")) |
| 1823 | return 0; |
| 1824 | |
| 1825 | #define REALLY_SLOW_IO |
| 1826 | /* We need the timeouts for at least some W83781D-like |
| 1827 | chips. But only if we read 'undefined' registers. */ |
| 1828 | val = inb_p(address + 1); |
| 1829 | if (inb_p(address + 2) != val |
| 1830 | || inb_p(address + 3) != val |
| 1831 | || inb_p(address + 7) != val) { |
| 1832 | pr_debug("w83781d: Detection failed at step 1\n"); |
| 1833 | goto release; |
| 1834 | } |
| 1835 | #undef REALLY_SLOW_IO |
| 1836 | |
| 1837 | /* We should be able to change the 7 LSB of the address port. The |
| 1838 | MSB (busy flag) should be clear initially, set after the write. */ |
| 1839 | save = inb_p(address + W83781D_ADDR_REG_OFFSET); |
| 1840 | if (save & 0x80) { |
| 1841 | pr_debug("w83781d: Detection failed at step 2\n"); |
| 1842 | goto release; |
| 1843 | } |
| 1844 | val = ~save & 0x7f; |
| 1845 | outb_p(val, address + W83781D_ADDR_REG_OFFSET); |
| 1846 | if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) { |
| 1847 | outb_p(save, address + W83781D_ADDR_REG_OFFSET); |
| 1848 | pr_debug("w83781d: Detection failed at step 3\n"); |
| 1849 | goto release; |
| 1850 | } |
| 1851 | |
| 1852 | /* We found a device, now see if it could be a W83781D */ |
| 1853 | outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET); |
| 1854 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1855 | if (val & 0x80) { |
| 1856 | pr_debug("w83781d: Detection failed at step 4\n"); |
| 1857 | goto release; |
| 1858 | } |
| 1859 | outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET); |
| 1860 | save = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1861 | outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET); |
| 1862 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1863 | if ((!(save & 0x80) && (val != 0xa3)) |
| 1864 | || ((save & 0x80) && (val != 0x5c))) { |
| 1865 | pr_debug("w83781d: Detection failed at step 5\n"); |
| 1866 | goto release; |
| 1867 | } |
| 1868 | outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET); |
| 1869 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1870 | if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */ |
| 1871 | pr_debug("w83781d: Detection failed at step 6\n"); |
| 1872 | goto release; |
| 1873 | } |
| 1874 | |
| 1875 | /* The busy flag should be clear again */ |
| 1876 | if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) { |
| 1877 | pr_debug("w83781d: Detection failed at step 7\n"); |
| 1878 | goto release; |
| 1879 | } |
| 1880 | |
| 1881 | /* Determine the chip type */ |
| 1882 | outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET); |
| 1883 | save = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1884 | outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET); |
| 1885 | outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET); |
| 1886 | val = inb_p(address + W83781D_DATA_REG_OFFSET); |
| 1887 | if ((val & 0xfe) == 0x10 /* W83781D */ |
| 1888 | || val == 0x30 /* W83782D */ |
| 1889 | || val == 0x21) /* W83627HF */ |
| 1890 | found = 1; |
| 1891 | |
| 1892 | if (found) |
| 1893 | pr_info("w83781d: Found a %s chip at %#x\n", |
| 1894 | val == 0x21 ? "W83627HF" : |
| 1895 | val == 0x30 ? "W83782D" : "W83781D", (int)address); |
| 1896 | |
| 1897 | release: |
| 1898 | release_region(address, W83781D_EXTENT); |
| 1899 | return found; |
| 1900 | } |
| 1901 | |
| 1902 | static int __init |
| 1903 | w83781d_isa_device_add(unsigned short address) |
| 1904 | { |
| 1905 | struct resource res = { |
| 1906 | .start = address, |
Jean Delvare | 15bde2f | 2007-08-29 10:39:57 +0200 | [diff] [blame] | 1907 | .end = address + W83781D_EXTENT - 1, |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1908 | .name = "w83781d", |
| 1909 | .flags = IORESOURCE_IO, |
| 1910 | }; |
| 1911 | int err; |
| 1912 | |
| 1913 | pdev = platform_device_alloc("w83781d", address); |
| 1914 | if (!pdev) { |
| 1915 | err = -ENOMEM; |
| 1916 | printk(KERN_ERR "w83781d: Device allocation failed\n"); |
| 1917 | goto exit; |
| 1918 | } |
| 1919 | |
| 1920 | err = platform_device_add_resources(pdev, &res, 1); |
| 1921 | if (err) { |
| 1922 | printk(KERN_ERR "w83781d: Device resource addition failed " |
| 1923 | "(%d)\n", err); |
| 1924 | goto exit_device_put; |
| 1925 | } |
| 1926 | |
| 1927 | err = platform_device_add(pdev); |
| 1928 | if (err) { |
| 1929 | printk(KERN_ERR "w83781d: Device addition failed (%d)\n", |
| 1930 | err); |
| 1931 | goto exit_device_put; |
| 1932 | } |
| 1933 | |
| 1934 | return 0; |
| 1935 | |
| 1936 | exit_device_put: |
| 1937 | platform_device_put(pdev); |
| 1938 | exit: |
| 1939 | pdev = NULL; |
| 1940 | return err; |
| 1941 | } |
| 1942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | static int __init |
| 1944 | sensors_w83781d_init(void) |
| 1945 | { |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1946 | int res; |
| 1947 | |
| 1948 | res = i2c_add_driver(&w83781d_driver); |
| 1949 | if (res) |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1950 | goto exit; |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1951 | |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1952 | if (w83781d_isa_found(isa_address)) { |
| 1953 | res = platform_driver_register(&w83781d_isa_driver); |
| 1954 | if (res) |
| 1955 | goto exit_unreg_i2c_driver; |
| 1956 | |
| 1957 | /* Sets global pdev as a side effect */ |
| 1958 | res = w83781d_isa_device_add(isa_address); |
| 1959 | if (res) |
| 1960 | goto exit_unreg_isa_driver; |
| 1961 | } |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1962 | |
| 1963 | return 0; |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1964 | |
| 1965 | exit_unreg_isa_driver: |
| 1966 | platform_driver_unregister(&w83781d_isa_driver); |
| 1967 | exit_unreg_i2c_driver: |
| 1968 | i2c_del_driver(&w83781d_driver); |
| 1969 | exit: |
| 1970 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | static void __exit |
| 1974 | sensors_w83781d_exit(void) |
| 1975 | { |
Jean Delvare | 7666c13 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 1976 | if (pdev) { |
| 1977 | platform_device_unregister(pdev); |
| 1978 | platform_driver_unregister(&w83781d_isa_driver); |
| 1979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | i2c_del_driver(&w83781d_driver); |
| 1981 | } |
| 1982 | |
| 1983 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " |
| 1984 | "Philip Edelbrock <phil@netroedge.com>, " |
| 1985 | "and Mark Studebaker <mdsxyz123@yahoo.com>"); |
| 1986 | MODULE_DESCRIPTION("W83781D driver"); |
| 1987 | MODULE_LICENSE("GPL"); |
| 1988 | |
| 1989 | module_init(sensors_w83781d_init); |
| 1990 | module_exit(sensors_w83781d_exit); |