Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm63.c - driver for the National Semiconductor LM63 temperature sensor |
| 3 | * with integrated fan control |
| 4 | * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org> |
| 5 | * Based on the lm90 driver. |
| 6 | * |
| 7 | * The LM63 is a sensor chip made by National Semiconductor. It measures |
| 8 | * two temperatures (its own and one external one) and the speed of one |
| 9 | * fan, those speed it can additionally control. Complete datasheet can be |
| 10 | * obtained from National's website at: |
| 11 | * http://www.national.com/pf/LM/LM63.html |
| 12 | * |
| 13 | * The LM63 is basically an LM86 with fan speed monitoring and control |
| 14 | * capabilities added. It misses some of the LM86 features though: |
| 15 | * - No low limit for local temperature. |
| 16 | * - No critical limit for local temperature. |
| 17 | * - Critical limit for remote temperature can be changed only once. We |
| 18 | * will consider that the critical limit is read-only. |
| 19 | * |
| 20 | * The datasheet isn't very clear about what the tachometer reading is. |
| 21 | * I had a explanation from National Semiconductor though. The two lower |
| 22 | * bits of the read value have to be masked out. The value is still 16 bit |
| 23 | * in width. |
| 24 | * |
| 25 | * This program is free software; you can redistribute it and/or modify |
| 26 | * it under the terms of the GNU General Public License as published by |
| 27 | * the Free Software Foundation; either version 2 of the License, or |
| 28 | * (at your option) any later version. |
| 29 | * |
| 30 | * This program is distributed in the hope that it will be useful, |
| 31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 | * GNU General Public License for more details. |
| 34 | * |
| 35 | * You should have received a copy of the GNU General Public License |
| 36 | * along with this program; if not, write to the Free Software |
| 37 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 38 | */ |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/module.h> |
| 41 | #include <linux/init.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/jiffies.h> |
| 44 | #include <linux/i2c.h> |
| 45 | #include <linux/i2c-sensor.h> |
| 46 | |
| 47 | /* |
| 48 | * Addresses to scan |
| 49 | * Address is fully defined internally and cannot be changed. |
| 50 | */ |
| 51 | |
| 52 | static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; |
| 53 | static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; |
| 54 | |
| 55 | /* |
| 56 | * Insmod parameters |
| 57 | */ |
| 58 | |
| 59 | SENSORS_INSMOD_1(lm63); |
| 60 | |
| 61 | /* |
| 62 | * The LM63 registers |
| 63 | */ |
| 64 | |
| 65 | #define LM63_REG_CONFIG1 0x03 |
| 66 | #define LM63_REG_CONFIG2 0xBF |
| 67 | #define LM63_REG_CONFIG_FAN 0x4A |
| 68 | |
| 69 | #define LM63_REG_TACH_COUNT_MSB 0x47 |
| 70 | #define LM63_REG_TACH_COUNT_LSB 0x46 |
| 71 | #define LM63_REG_TACH_LIMIT_MSB 0x49 |
| 72 | #define LM63_REG_TACH_LIMIT_LSB 0x48 |
| 73 | |
| 74 | #define LM63_REG_PWM_VALUE 0x4C |
| 75 | #define LM63_REG_PWM_FREQ 0x4D |
| 76 | |
| 77 | #define LM63_REG_LOCAL_TEMP 0x00 |
| 78 | #define LM63_REG_LOCAL_HIGH 0x05 |
| 79 | |
| 80 | #define LM63_REG_REMOTE_TEMP_MSB 0x01 |
| 81 | #define LM63_REG_REMOTE_TEMP_LSB 0x10 |
| 82 | #define LM63_REG_REMOTE_OFFSET_MSB 0x11 |
| 83 | #define LM63_REG_REMOTE_OFFSET_LSB 0x12 |
| 84 | #define LM63_REG_REMOTE_HIGH_MSB 0x07 |
| 85 | #define LM63_REG_REMOTE_HIGH_LSB 0x13 |
| 86 | #define LM63_REG_REMOTE_LOW_MSB 0x08 |
| 87 | #define LM63_REG_REMOTE_LOW_LSB 0x14 |
| 88 | #define LM63_REG_REMOTE_TCRIT 0x19 |
| 89 | #define LM63_REG_REMOTE_TCRIT_HYST 0x21 |
| 90 | |
| 91 | #define LM63_REG_ALERT_STATUS 0x02 |
| 92 | #define LM63_REG_ALERT_MASK 0x16 |
| 93 | |
| 94 | #define LM63_REG_MAN_ID 0xFE |
| 95 | #define LM63_REG_CHIP_ID 0xFF |
| 96 | |
| 97 | /* |
| 98 | * Conversions and various macros |
| 99 | * For tachometer counts, the LM63 uses 16-bit values. |
| 100 | * For local temperature and high limit, remote critical limit and hysteresis |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame^] | 101 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * For remote temperature, low and high limits, it uses signed 11-bit values |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame^] | 103 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ |
| 105 | |
| 106 | #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ |
| 107 | 5400000 / (reg)) |
| 108 | #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ |
| 109 | (5400000 / (val)) & 0xFFFC) |
| 110 | #define TEMP8_FROM_REG(reg) ((reg) * 1000) |
| 111 | #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \ |
| 112 | (val) >= 127000 ? 127 : \ |
| 113 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 114 | ((val) + 500) / 1000) |
| 115 | #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) |
| 116 | #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \ |
| 117 | (val) >= 127875 ? 0x7FE0 : \ |
| 118 | (val) < 0 ? ((val) - 62) / 125 * 32 : \ |
| 119 | ((val) + 62) / 125 * 32) |
| 120 | #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ |
| 121 | (val) >= 127000 ? 127 : \ |
| 122 | ((val) + 500) / 1000) |
| 123 | |
| 124 | /* |
| 125 | * Functions declaration |
| 126 | */ |
| 127 | |
| 128 | static int lm63_attach_adapter(struct i2c_adapter *adapter); |
| 129 | static int lm63_detach_client(struct i2c_client *client); |
| 130 | |
| 131 | static struct lm63_data *lm63_update_device(struct device *dev); |
| 132 | |
| 133 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind); |
| 134 | static void lm63_init_client(struct i2c_client *client); |
| 135 | |
| 136 | /* |
| 137 | * Driver data (common to all clients) |
| 138 | */ |
| 139 | |
| 140 | static struct i2c_driver lm63_driver = { |
| 141 | .owner = THIS_MODULE, |
| 142 | .name = "lm63", |
| 143 | .flags = I2C_DF_NOTIFY, |
| 144 | .attach_adapter = lm63_attach_adapter, |
| 145 | .detach_client = lm63_detach_client, |
| 146 | }; |
| 147 | |
| 148 | /* |
| 149 | * Client data (each client gets its own) |
| 150 | */ |
| 151 | |
| 152 | struct lm63_data { |
| 153 | struct i2c_client client; |
| 154 | struct semaphore update_lock; |
| 155 | char valid; /* zero until following fields are valid */ |
| 156 | unsigned long last_updated; /* in jiffies */ |
| 157 | |
| 158 | /* registers values */ |
| 159 | u8 config, config_fan; |
| 160 | u16 fan1_input; |
| 161 | u16 fan1_low; |
| 162 | u8 pwm1_freq; |
| 163 | u8 pwm1_value; |
| 164 | s8 temp1_input; |
| 165 | s8 temp1_high; |
| 166 | s16 temp2_input; |
| 167 | s16 temp2_high; |
| 168 | s16 temp2_low; |
| 169 | s8 temp2_crit; |
| 170 | u8 temp2_crit_hyst; |
| 171 | u8 alarms; |
| 172 | }; |
| 173 | |
| 174 | /* |
| 175 | * Sysfs callback functions and files |
| 176 | */ |
| 177 | |
| 178 | #define show_fan(value) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 179 | static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { \ |
| 181 | struct lm63_data *data = lm63_update_device(dev); \ |
| 182 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->value)); \ |
| 183 | } |
| 184 | show_fan(fan1_input); |
| 185 | show_fan(fan1_low); |
| 186 | |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 187 | static ssize_t set_fan1_low(struct device *dev, struct device_attribute *attr, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | size_t count) |
| 189 | { |
| 190 | struct i2c_client *client = to_i2c_client(dev); |
| 191 | struct lm63_data *data = i2c_get_clientdata(client); |
| 192 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 193 | |
| 194 | down(&data->update_lock); |
| 195 | data->fan1_low = FAN_TO_REG(val); |
| 196 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
| 197 | data->fan1_low & 0xFF); |
| 198 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
| 199 | data->fan1_low >> 8); |
| 200 | up(&data->update_lock); |
| 201 | return count; |
| 202 | } |
| 203 | |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 204 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | struct lm63_data *data = lm63_update_device(dev); |
| 207 | return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ? |
| 208 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / |
| 209 | (2 * data->pwm1_freq)); |
| 210 | } |
| 211 | |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 212 | static ssize_t set_pwm1(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] | 213 | { |
| 214 | struct i2c_client *client = to_i2c_client(dev); |
| 215 | struct lm63_data *data = i2c_get_clientdata(client); |
| 216 | unsigned long val; |
| 217 | |
| 218 | if (!(data->config_fan & 0x20)) /* register is read-only */ |
| 219 | return -EPERM; |
| 220 | |
| 221 | val = simple_strtoul(buf, NULL, 10); |
| 222 | down(&data->update_lock); |
| 223 | data->pwm1_value = val <= 0 ? 0 : |
| 224 | val >= 255 ? 2 * data->pwm1_freq : |
| 225 | (val * data->pwm1_freq * 2 + 127) / 255; |
| 226 | i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value); |
| 227 | up(&data->update_lock); |
| 228 | return count; |
| 229 | } |
| 230 | |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 231 | static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | struct lm63_data *data = lm63_update_device(dev); |
| 234 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); |
| 235 | } |
| 236 | |
| 237 | #define show_temp8(value) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 238 | static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { \ |
| 240 | struct lm63_data *data = lm63_update_device(dev); \ |
| 241 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->value)); \ |
| 242 | } |
| 243 | #define show_temp11(value) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 244 | static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { \ |
| 246 | struct lm63_data *data = lm63_update_device(dev); \ |
| 247 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->value)); \ |
| 248 | } |
| 249 | show_temp8(temp1_input); |
| 250 | show_temp8(temp1_high); |
| 251 | show_temp11(temp2_input); |
| 252 | show_temp11(temp2_high); |
| 253 | show_temp11(temp2_low); |
| 254 | show_temp8(temp2_crit); |
| 255 | |
| 256 | #define set_temp8(value, reg) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 257 | static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | size_t count) \ |
| 259 | { \ |
| 260 | struct i2c_client *client = to_i2c_client(dev); \ |
| 261 | struct lm63_data *data = i2c_get_clientdata(client); \ |
| 262 | long val = simple_strtol(buf, NULL, 10); \ |
| 263 | \ |
| 264 | down(&data->update_lock); \ |
| 265 | data->value = TEMP8_TO_REG(val); \ |
| 266 | i2c_smbus_write_byte_data(client, reg, data->value); \ |
| 267 | up(&data->update_lock); \ |
| 268 | return count; \ |
| 269 | } |
| 270 | #define set_temp11(value, reg_msb, reg_lsb) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 271 | static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | size_t count) \ |
| 273 | { \ |
| 274 | struct i2c_client *client = to_i2c_client(dev); \ |
| 275 | struct lm63_data *data = i2c_get_clientdata(client); \ |
| 276 | long val = simple_strtol(buf, NULL, 10); \ |
| 277 | \ |
| 278 | down(&data->update_lock); \ |
| 279 | data->value = TEMP11_TO_REG(val); \ |
| 280 | i2c_smbus_write_byte_data(client, reg_msb, data->value >> 8); \ |
| 281 | i2c_smbus_write_byte_data(client, reg_lsb, data->value & 0xff); \ |
| 282 | up(&data->update_lock); \ |
| 283 | return count; \ |
| 284 | } |
| 285 | set_temp8(temp1_high, LM63_REG_LOCAL_HIGH); |
| 286 | set_temp11(temp2_high, LM63_REG_REMOTE_HIGH_MSB, LM63_REG_REMOTE_HIGH_LSB); |
| 287 | set_temp11(temp2_low, LM63_REG_REMOTE_LOW_MSB, LM63_REG_REMOTE_LOW_LSB); |
| 288 | |
| 289 | /* Hysteresis register holds a relative value, while we want to present |
| 290 | an absolute to user-space */ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 291 | static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
| 293 | struct lm63_data *data = lm63_update_device(dev); |
| 294 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp2_crit) |
| 295 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); |
| 296 | } |
| 297 | |
| 298 | /* And now the other way around, user-space provides an absolute |
| 299 | hysteresis value and we have to store a relative one */ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 300 | static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | size_t count) |
| 302 | { |
| 303 | struct i2c_client *client = to_i2c_client(dev); |
| 304 | struct lm63_data *data = i2c_get_clientdata(client); |
| 305 | long val = simple_strtol(buf, NULL, 10); |
| 306 | long hyst; |
| 307 | |
| 308 | down(&data->update_lock); |
| 309 | hyst = TEMP8_FROM_REG(data->temp2_crit) - val; |
| 310 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
| 311 | HYST_TO_REG(hyst)); |
| 312 | up(&data->update_lock); |
| 313 | return count; |
| 314 | } |
| 315 | |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 316 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
| 318 | struct lm63_data *data = lm63_update_device(dev); |
| 319 | return sprintf(buf, "%u\n", data->alarms); |
| 320 | } |
| 321 | |
| 322 | static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan1_input, NULL); |
| 323 | static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan1_low, |
| 324 | set_fan1_low); |
| 325 | |
| 326 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); |
| 327 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); |
| 328 | |
| 329 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL); |
| 330 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_high, |
| 331 | set_temp1_high); |
| 332 | |
| 333 | static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp2_input, NULL); |
| 334 | static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp2_low, |
| 335 | set_temp2_low); |
| 336 | static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp2_high, |
| 337 | set_temp2_high); |
| 338 | static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp2_crit, NULL); |
| 339 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
| 340 | set_temp2_crit_hyst); |
| 341 | |
| 342 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 343 | |
| 344 | /* |
| 345 | * Real code |
| 346 | */ |
| 347 | |
| 348 | static int lm63_attach_adapter(struct i2c_adapter *adapter) |
| 349 | { |
| 350 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 351 | return 0; |
| 352 | return i2c_detect(adapter, &addr_data, lm63_detect); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * The following function does more than just detection. If detection |
| 357 | * succeeds, it also registers the new chip. |
| 358 | */ |
| 359 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind) |
| 360 | { |
| 361 | struct i2c_client *new_client; |
| 362 | struct lm63_data *data; |
| 363 | int err = 0; |
| 364 | |
| 365 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 366 | goto exit; |
| 367 | |
| 368 | if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) { |
| 369 | err = -ENOMEM; |
| 370 | goto exit; |
| 371 | } |
| 372 | memset(data, 0, sizeof(struct lm63_data)); |
| 373 | |
| 374 | /* The common I2C client data is placed right before the |
| 375 | LM63-specific data. */ |
| 376 | new_client = &data->client; |
| 377 | i2c_set_clientdata(new_client, data); |
| 378 | new_client->addr = address; |
| 379 | new_client->adapter = adapter; |
| 380 | new_client->driver = &lm63_driver; |
| 381 | new_client->flags = 0; |
| 382 | |
| 383 | /* Default to an LM63 if forced */ |
| 384 | if (kind == 0) |
| 385 | kind = lm63; |
| 386 | |
| 387 | if (kind < 0) { /* must identify */ |
| 388 | u8 man_id, chip_id, reg_config1, reg_config2; |
| 389 | u8 reg_alert_status, reg_alert_mask; |
| 390 | |
| 391 | man_id = i2c_smbus_read_byte_data(new_client, |
| 392 | LM63_REG_MAN_ID); |
| 393 | chip_id = i2c_smbus_read_byte_data(new_client, |
| 394 | LM63_REG_CHIP_ID); |
| 395 | reg_config1 = i2c_smbus_read_byte_data(new_client, |
| 396 | LM63_REG_CONFIG1); |
| 397 | reg_config2 = i2c_smbus_read_byte_data(new_client, |
| 398 | LM63_REG_CONFIG2); |
| 399 | reg_alert_status = i2c_smbus_read_byte_data(new_client, |
| 400 | LM63_REG_ALERT_STATUS); |
| 401 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, |
| 402 | LM63_REG_ALERT_MASK); |
| 403 | |
| 404 | if (man_id == 0x01 /* National Semiconductor */ |
| 405 | && chip_id == 0x41 /* LM63 */ |
| 406 | && (reg_config1 & 0x18) == 0x00 |
| 407 | && (reg_config2 & 0xF8) == 0x00 |
| 408 | && (reg_alert_status & 0x20) == 0x00 |
| 409 | && (reg_alert_mask & 0xA4) == 0xA4) { |
| 410 | kind = lm63; |
| 411 | } else { /* failed */ |
| 412 | dev_dbg(&adapter->dev, "Unsupported chip " |
| 413 | "(man_id=0x%02X, chip_id=0x%02X).\n", |
| 414 | man_id, chip_id); |
| 415 | goto exit_free; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | strlcpy(new_client->name, "lm63", I2C_NAME_SIZE); |
| 420 | data->valid = 0; |
| 421 | init_MUTEX(&data->update_lock); |
| 422 | |
| 423 | /* Tell the I2C layer a new client has arrived */ |
| 424 | if ((err = i2c_attach_client(new_client))) |
| 425 | goto exit_free; |
| 426 | |
| 427 | /* Initialize the LM63 chip */ |
| 428 | lm63_init_client(new_client); |
| 429 | |
| 430 | /* Register sysfs hooks */ |
| 431 | if (data->config & 0x04) { /* tachometer enabled */ |
| 432 | device_create_file(&new_client->dev, &dev_attr_fan1_input); |
| 433 | device_create_file(&new_client->dev, &dev_attr_fan1_min); |
| 434 | } |
| 435 | device_create_file(&new_client->dev, &dev_attr_pwm1); |
| 436 | device_create_file(&new_client->dev, &dev_attr_pwm1_enable); |
| 437 | device_create_file(&new_client->dev, &dev_attr_temp1_input); |
| 438 | device_create_file(&new_client->dev, &dev_attr_temp2_input); |
| 439 | device_create_file(&new_client->dev, &dev_attr_temp2_min); |
| 440 | device_create_file(&new_client->dev, &dev_attr_temp1_max); |
| 441 | device_create_file(&new_client->dev, &dev_attr_temp2_max); |
| 442 | device_create_file(&new_client->dev, &dev_attr_temp2_crit); |
| 443 | device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst); |
| 444 | device_create_file(&new_client->dev, &dev_attr_alarms); |
| 445 | |
| 446 | return 0; |
| 447 | |
| 448 | exit_free: |
| 449 | kfree(data); |
| 450 | exit: |
| 451 | return err; |
| 452 | } |
| 453 | |
| 454 | /* Idealy we shouldn't have to initialize anything, since the BIOS |
| 455 | should have taken care of everything */ |
| 456 | static void lm63_init_client(struct i2c_client *client) |
| 457 | { |
| 458 | struct lm63_data *data = i2c_get_clientdata(client); |
| 459 | |
| 460 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); |
| 461 | data->config_fan = i2c_smbus_read_byte_data(client, |
| 462 | LM63_REG_CONFIG_FAN); |
| 463 | |
| 464 | /* Start converting if needed */ |
| 465 | if (data->config & 0x40) { /* standby */ |
| 466 | dev_dbg(&client->dev, "Switching to operational mode"); |
| 467 | data->config &= 0xA7; |
| 468 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, |
| 469 | data->config); |
| 470 | } |
| 471 | |
| 472 | /* We may need pwm1_freq before ever updating the client data */ |
| 473 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); |
| 474 | if (data->pwm1_freq == 0) |
| 475 | data->pwm1_freq = 1; |
| 476 | |
| 477 | /* Show some debug info about the LM63 configuration */ |
| 478 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", |
| 479 | (data->config & 0x04) ? "tachometer input" : |
| 480 | "alert output"); |
| 481 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", |
| 482 | (data->config_fan & 0x08) ? "1.4" : "360", |
| 483 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); |
| 484 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", |
| 485 | (data->config_fan & 0x10) ? "low" : "high", |
| 486 | (data->config_fan & 0x20) ? "manual" : "auto"); |
| 487 | } |
| 488 | |
| 489 | static int lm63_detach_client(struct i2c_client *client) |
| 490 | { |
| 491 | int err; |
| 492 | |
| 493 | if ((err = i2c_detach_client(client))) { |
| 494 | dev_err(&client->dev, "Client deregistration failed, " |
| 495 | "client not detached\n"); |
| 496 | return err; |
| 497 | } |
| 498 | |
| 499 | kfree(i2c_get_clientdata(client)); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static struct lm63_data *lm63_update_device(struct device *dev) |
| 504 | { |
| 505 | struct i2c_client *client = to_i2c_client(dev); |
| 506 | struct lm63_data *data = i2c_get_clientdata(client); |
| 507 | |
| 508 | down(&data->update_lock); |
| 509 | |
| 510 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 511 | if (data->config & 0x04) { /* tachometer enabled */ |
| 512 | /* order matters for fan1_input */ |
| 513 | data->fan1_input = i2c_smbus_read_byte_data(client, |
| 514 | LM63_REG_TACH_COUNT_LSB) & 0xFC; |
| 515 | data->fan1_input |= i2c_smbus_read_byte_data(client, |
| 516 | LM63_REG_TACH_COUNT_MSB) << 8; |
| 517 | data->fan1_low = (i2c_smbus_read_byte_data(client, |
| 518 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) |
| 519 | | (i2c_smbus_read_byte_data(client, |
| 520 | LM63_REG_TACH_LIMIT_MSB) << 8); |
| 521 | } |
| 522 | |
| 523 | data->pwm1_freq = i2c_smbus_read_byte_data(client, |
| 524 | LM63_REG_PWM_FREQ); |
| 525 | if (data->pwm1_freq == 0) |
| 526 | data->pwm1_freq = 1; |
| 527 | data->pwm1_value = i2c_smbus_read_byte_data(client, |
| 528 | LM63_REG_PWM_VALUE); |
| 529 | |
| 530 | data->temp1_input = i2c_smbus_read_byte_data(client, |
| 531 | LM63_REG_LOCAL_TEMP); |
| 532 | data->temp1_high = i2c_smbus_read_byte_data(client, |
| 533 | LM63_REG_LOCAL_HIGH); |
| 534 | |
| 535 | /* order matters for temp2_input */ |
| 536 | data->temp2_input = i2c_smbus_read_byte_data(client, |
| 537 | LM63_REG_REMOTE_TEMP_MSB) << 8; |
| 538 | data->temp2_input |= i2c_smbus_read_byte_data(client, |
| 539 | LM63_REG_REMOTE_TEMP_LSB); |
| 540 | data->temp2_high = (i2c_smbus_read_byte_data(client, |
| 541 | LM63_REG_REMOTE_HIGH_MSB) << 8) |
| 542 | | i2c_smbus_read_byte_data(client, |
| 543 | LM63_REG_REMOTE_HIGH_LSB); |
| 544 | data->temp2_low = (i2c_smbus_read_byte_data(client, |
| 545 | LM63_REG_REMOTE_LOW_MSB) << 8) |
| 546 | | i2c_smbus_read_byte_data(client, |
| 547 | LM63_REG_REMOTE_LOW_LSB); |
| 548 | data->temp2_crit = i2c_smbus_read_byte_data(client, |
| 549 | LM63_REG_REMOTE_TCRIT); |
| 550 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, |
| 551 | LM63_REG_REMOTE_TCRIT_HYST); |
| 552 | |
| 553 | data->alarms = i2c_smbus_read_byte_data(client, |
| 554 | LM63_REG_ALERT_STATUS) & 0x7F; |
| 555 | |
| 556 | data->last_updated = jiffies; |
| 557 | data->valid = 1; |
| 558 | } |
| 559 | |
| 560 | up(&data->update_lock); |
| 561 | |
| 562 | return data; |
| 563 | } |
| 564 | |
| 565 | static int __init sensors_lm63_init(void) |
| 566 | { |
| 567 | return i2c_add_driver(&lm63_driver); |
| 568 | } |
| 569 | |
| 570 | static void __exit sensors_lm63_exit(void) |
| 571 | { |
| 572 | i2c_del_driver(&lm63_driver); |
| 573 | } |
| 574 | |
| 575 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 576 | MODULE_DESCRIPTION("LM63 driver"); |
| 577 | MODULE_LICENSE("GPL"); |
| 578 | |
| 579 | module_init(sensors_lm63_init); |
| 580 | module_exit(sensors_lm63_exit); |