blob: 736126d73aa25e430e594ef76e0e3f58cbbb6eaf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
Jean Delvarebc51ae12005-06-05 20:32:27 +02004 * Copyright (C) 2004-2005 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Torvalds1da177e2005-04-16 15:20:36 -070040#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>
Jean Delvare10c08f82005-06-06 19:34:45 +020046#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/hwmon.h>
48#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Addresses to scan
52 * Address is fully defined internally and cannot be changed.
53 */
54
55static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * Insmod parameters
59 */
60
61SENSORS_INSMOD_1(lm63);
62
63/*
64 * The LM63 registers
65 */
66
67#define LM63_REG_CONFIG1 0x03
68#define LM63_REG_CONFIG2 0xBF
69#define LM63_REG_CONFIG_FAN 0x4A
70
71#define LM63_REG_TACH_COUNT_MSB 0x47
72#define LM63_REG_TACH_COUNT_LSB 0x46
73#define LM63_REG_TACH_LIMIT_MSB 0x49
74#define LM63_REG_TACH_LIMIT_LSB 0x48
75
76#define LM63_REG_PWM_VALUE 0x4C
77#define LM63_REG_PWM_FREQ 0x4D
78
79#define LM63_REG_LOCAL_TEMP 0x00
80#define LM63_REG_LOCAL_HIGH 0x05
81
82#define LM63_REG_REMOTE_TEMP_MSB 0x01
83#define LM63_REG_REMOTE_TEMP_LSB 0x10
84#define LM63_REG_REMOTE_OFFSET_MSB 0x11
85#define LM63_REG_REMOTE_OFFSET_LSB 0x12
86#define LM63_REG_REMOTE_HIGH_MSB 0x07
87#define LM63_REG_REMOTE_HIGH_LSB 0x13
88#define LM63_REG_REMOTE_LOW_MSB 0x08
89#define LM63_REG_REMOTE_LOW_LSB 0x14
90#define LM63_REG_REMOTE_TCRIT 0x19
91#define LM63_REG_REMOTE_TCRIT_HYST 0x21
92
93#define LM63_REG_ALERT_STATUS 0x02
94#define LM63_REG_ALERT_MASK 0x16
95
96#define LM63_REG_MAN_ID 0xFE
97#define LM63_REG_CHIP_ID 0xFF
98
99/*
100 * Conversions and various macros
101 * For tachometer counts, the LM63 uses 16-bit values.
102 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600103 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600105 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107
108#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
109 5400000 / (reg))
110#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
111 (5400000 / (val)) & 0xFFFC)
112#define TEMP8_FROM_REG(reg) ((reg) * 1000)
113#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
114 (val) >= 127000 ? 127 : \
115 (val) < 0 ? ((val) - 500) / 1000 : \
116 ((val) + 500) / 1000)
117#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
118#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
119 (val) >= 127875 ? 0x7FE0 : \
120 (val) < 0 ? ((val) - 62) / 125 * 32 : \
121 ((val) + 62) / 125 * 32)
122#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
123 (val) >= 127000 ? 127 : \
124 ((val) + 500) / 1000)
125
126/*
127 * Functions declaration
128 */
129
130static int lm63_attach_adapter(struct i2c_adapter *adapter);
131static int lm63_detach_client(struct i2c_client *client);
132
133static struct lm63_data *lm63_update_device(struct device *dev);
134
135static int lm63_detect(struct i2c_adapter *adapter, int address, int kind);
136static void lm63_init_client(struct i2c_client *client);
137
138/*
139 * Driver data (common to all clients)
140 */
141
142static struct i2c_driver lm63_driver = {
143 .owner = THIS_MODULE,
144 .name = "lm63",
145 .flags = I2C_DF_NOTIFY,
146 .attach_adapter = lm63_attach_adapter,
147 .detach_client = lm63_detach_client,
148};
149
150/*
151 * Client data (each client gets its own)
152 */
153
154struct lm63_data {
155 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400156 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct semaphore update_lock;
158 char valid; /* zero until following fields are valid */
159 unsigned long last_updated; /* in jiffies */
160
161 /* registers values */
162 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200163 u16 fan[2]; /* 0: input
164 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 u8 pwm1_freq;
166 u8 pwm1_value;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200167 s8 temp8[3]; /* 0: local input
168 1: local high limit
169 2: remote critical limit */
170 s16 temp11[3]; /* 0: remote input
171 1: remote low limit
172 2: remote high limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 u8 temp2_crit_hyst;
174 u8 alarms;
175};
176
177/*
178 * Sysfs callback functions and files
179 */
180
Jean Delvarebc51ae12005-06-05 20:32:27 +0200181static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
182 char *buf)
183{
184 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
185 struct lm63_data *data = lm63_update_device(dev);
186 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jean Delvarebc51ae12005-06-05 20:32:27 +0200189static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
190 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct i2c_client *client = to_i2c_client(dev);
193 struct lm63_data *data = i2c_get_clientdata(client);
194 unsigned long val = simple_strtoul(buf, NULL, 10);
195
196 down(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200197 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200199 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200201 data->fan[1] >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 up(&data->update_lock);
203 return count;
204}
205
Jean Delvarebc51ae12005-06-05 20:32:27 +0200206static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
207 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct lm63_data *data = lm63_update_device(dev);
210 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
211 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
212 (2 * data->pwm1_freq));
213}
214
Jean Delvarebc51ae12005-06-05 20:32:27 +0200215static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
216 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct i2c_client *client = to_i2c_client(dev);
219 struct lm63_data *data = i2c_get_clientdata(client);
220 unsigned long val;
221
222 if (!(data->config_fan & 0x20)) /* register is read-only */
223 return -EPERM;
224
225 val = simple_strtoul(buf, NULL, 10);
226 down(&data->update_lock);
227 data->pwm1_value = val <= 0 ? 0 :
228 val >= 255 ? 2 * data->pwm1_freq :
229 (val * data->pwm1_freq * 2 + 127) / 255;
230 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
231 up(&data->update_lock);
232 return count;
233}
234
Jean Delvarebc51ae12005-06-05 20:32:27 +0200235static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
236 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct lm63_data *data = lm63_update_device(dev);
239 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
240}
241
Jean Delvarebc51ae12005-06-05 20:32:27 +0200242static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
243 char *buf)
244{
245 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
246 struct lm63_data *data = lm63_update_device(dev);
247 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jean Delvarebc51ae12005-06-05 20:32:27 +0200250static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
251 const char *buf, size_t count)
252{
253 struct i2c_client *client = to_i2c_client(dev);
254 struct lm63_data *data = i2c_get_clientdata(client);
255 long val = simple_strtol(buf, NULL, 10);
256
257 down(&data->update_lock);
258 data->temp8[1] = TEMP8_TO_REG(val);
259 i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
260 up(&data->update_lock);
261 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200263
264static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
265 char *buf)
266{
267 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
268 struct lm63_data *data = lm63_update_device(dev);
269 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200271
272static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
273 const char *buf, size_t count)
274{
275 static const u8 reg[4] = {
276 LM63_REG_REMOTE_LOW_MSB,
277 LM63_REG_REMOTE_LOW_LSB,
278 LM63_REG_REMOTE_HIGH_MSB,
279 LM63_REG_REMOTE_HIGH_LSB,
280 };
281
282 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
283 struct i2c_client *client = to_i2c_client(dev);
284 struct lm63_data *data = i2c_get_clientdata(client);
285 long val = simple_strtol(buf, NULL, 10);
286 int nr = attr->index;
287
288 down(&data->update_lock);
289 data->temp11[nr] = TEMP11_TO_REG(val);
290 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
291 data->temp11[nr] >> 8);
292 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
293 data->temp11[nr] & 0xff);
294 up(&data->update_lock);
295 return count;
296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/* Hysteresis register holds a relative value, while we want to present
299 an absolute to user-space */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200300static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
301 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct lm63_data *data = lm63_update_device(dev);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200304 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 - TEMP8_FROM_REG(data->temp2_crit_hyst));
306}
307
308/* And now the other way around, user-space provides an absolute
309 hysteresis value and we have to store a relative one */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200310static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
311 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct i2c_client *client = to_i2c_client(dev);
314 struct lm63_data *data = i2c_get_clientdata(client);
315 long val = simple_strtol(buf, NULL, 10);
316 long hyst;
317
318 down(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200319 hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
321 HYST_TO_REG(hyst));
322 up(&data->update_lock);
323 return count;
324}
325
Jean Delvarebc51ae12005-06-05 20:32:27 +0200326static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
327 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct lm63_data *data = lm63_update_device(dev);
330 return sprintf(buf, "%u\n", data->alarms);
331}
332
Jean Delvarebc51ae12005-06-05 20:32:27 +0200333static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
334static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
335 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
338static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
339
Jean Delvarebc51ae12005-06-05 20:32:27 +0200340static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
341static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
342 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jean Delvarebc51ae12005-06-05 20:32:27 +0200344static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
345static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
346 set_temp11, 1);
347static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
348 set_temp11, 2);
349static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
351 set_temp2_crit_hyst);
352
353static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
354
355/*
356 * Real code
357 */
358
359static int lm63_attach_adapter(struct i2c_adapter *adapter)
360{
361 if (!(adapter->class & I2C_CLASS_HWMON))
362 return 0;
363 return i2c_detect(adapter, &addr_data, lm63_detect);
364}
365
366/*
367 * The following function does more than just detection. If detection
368 * succeeds, it also registers the new chip.
369 */
370static int lm63_detect(struct i2c_adapter *adapter, int address, int kind)
371{
372 struct i2c_client *new_client;
373 struct lm63_data *data;
374 int err = 0;
375
376 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
377 goto exit;
378
379 if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) {
380 err = -ENOMEM;
381 goto exit;
382 }
383 memset(data, 0, sizeof(struct lm63_data));
384
385 /* The common I2C client data is placed right before the
386 LM63-specific data. */
387 new_client = &data->client;
388 i2c_set_clientdata(new_client, data);
389 new_client->addr = address;
390 new_client->adapter = adapter;
391 new_client->driver = &lm63_driver;
392 new_client->flags = 0;
393
394 /* Default to an LM63 if forced */
395 if (kind == 0)
396 kind = lm63;
397
398 if (kind < 0) { /* must identify */
399 u8 man_id, chip_id, reg_config1, reg_config2;
400 u8 reg_alert_status, reg_alert_mask;
401
402 man_id = i2c_smbus_read_byte_data(new_client,
403 LM63_REG_MAN_ID);
404 chip_id = i2c_smbus_read_byte_data(new_client,
405 LM63_REG_CHIP_ID);
406 reg_config1 = i2c_smbus_read_byte_data(new_client,
407 LM63_REG_CONFIG1);
408 reg_config2 = i2c_smbus_read_byte_data(new_client,
409 LM63_REG_CONFIG2);
410 reg_alert_status = i2c_smbus_read_byte_data(new_client,
411 LM63_REG_ALERT_STATUS);
412 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
413 LM63_REG_ALERT_MASK);
414
415 if (man_id == 0x01 /* National Semiconductor */
416 && chip_id == 0x41 /* LM63 */
417 && (reg_config1 & 0x18) == 0x00
418 && (reg_config2 & 0xF8) == 0x00
419 && (reg_alert_status & 0x20) == 0x00
420 && (reg_alert_mask & 0xA4) == 0xA4) {
421 kind = lm63;
422 } else { /* failed */
423 dev_dbg(&adapter->dev, "Unsupported chip "
424 "(man_id=0x%02X, chip_id=0x%02X).\n",
425 man_id, chip_id);
426 goto exit_free;
427 }
428 }
429
430 strlcpy(new_client->name, "lm63", I2C_NAME_SIZE);
431 data->valid = 0;
432 init_MUTEX(&data->update_lock);
433
434 /* Tell the I2C layer a new client has arrived */
435 if ((err = i2c_attach_client(new_client)))
436 goto exit_free;
437
438 /* Initialize the LM63 chip */
439 lm63_init_client(new_client);
440
441 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400442 data->class_dev = hwmon_device_register(&new_client->dev);
443 if (IS_ERR(data->class_dev)) {
444 err = PTR_ERR(data->class_dev);
445 goto exit_detach;
446 }
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (data->config & 0x04) { /* tachometer enabled */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200449 device_create_file(&new_client->dev,
450 &sensor_dev_attr_fan1_input.dev_attr);
451 device_create_file(&new_client->dev,
452 &sensor_dev_attr_fan1_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 device_create_file(&new_client->dev, &dev_attr_pwm1);
455 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200456 device_create_file(&new_client->dev,
457 &sensor_dev_attr_temp1_input.dev_attr);
458 device_create_file(&new_client->dev,
459 &sensor_dev_attr_temp2_input.dev_attr);
460 device_create_file(&new_client->dev,
461 &sensor_dev_attr_temp2_min.dev_attr);
462 device_create_file(&new_client->dev,
463 &sensor_dev_attr_temp1_max.dev_attr);
464 device_create_file(&new_client->dev,
465 &sensor_dev_attr_temp2_max.dev_attr);
466 device_create_file(&new_client->dev,
467 &sensor_dev_attr_temp2_crit.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
469 device_create_file(&new_client->dev, &dev_attr_alarms);
470
471 return 0;
472
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400473exit_detach:
474 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475exit_free:
476 kfree(data);
477exit:
478 return err;
479}
480
481/* Idealy we shouldn't have to initialize anything, since the BIOS
482 should have taken care of everything */
483static void lm63_init_client(struct i2c_client *client)
484{
485 struct lm63_data *data = i2c_get_clientdata(client);
486
487 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
488 data->config_fan = i2c_smbus_read_byte_data(client,
489 LM63_REG_CONFIG_FAN);
490
491 /* Start converting if needed */
492 if (data->config & 0x40) { /* standby */
493 dev_dbg(&client->dev, "Switching to operational mode");
494 data->config &= 0xA7;
495 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
496 data->config);
497 }
498
499 /* We may need pwm1_freq before ever updating the client data */
500 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
501 if (data->pwm1_freq == 0)
502 data->pwm1_freq = 1;
503
504 /* Show some debug info about the LM63 configuration */
505 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
506 (data->config & 0x04) ? "tachometer input" :
507 "alert output");
508 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
509 (data->config_fan & 0x08) ? "1.4" : "360",
510 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
511 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
512 (data->config_fan & 0x10) ? "low" : "high",
513 (data->config_fan & 0x20) ? "manual" : "auto");
514}
515
516static int lm63_detach_client(struct i2c_client *client)
517{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400518 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int err;
520
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400521 hwmon_device_unregister(data->class_dev);
522
Jean Delvare7bef5592005-07-27 22:14:49 +0200523 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400526 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528}
529
530static struct lm63_data *lm63_update_device(struct device *dev)
531{
532 struct i2c_client *client = to_i2c_client(dev);
533 struct lm63_data *data = i2c_get_clientdata(client);
534
535 down(&data->update_lock);
536
537 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
538 if (data->config & 0x04) { /* tachometer enabled */
539 /* order matters for fan1_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200540 data->fan[0] = i2c_smbus_read_byte_data(client,
541 LM63_REG_TACH_COUNT_LSB) & 0xFC;
542 data->fan[0] |= i2c_smbus_read_byte_data(client,
543 LM63_REG_TACH_COUNT_MSB) << 8;
544 data->fan[1] = (i2c_smbus_read_byte_data(client,
545 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
546 | (i2c_smbus_read_byte_data(client,
547 LM63_REG_TACH_LIMIT_MSB) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 data->pwm1_freq = i2c_smbus_read_byte_data(client,
551 LM63_REG_PWM_FREQ);
552 if (data->pwm1_freq == 0)
553 data->pwm1_freq = 1;
554 data->pwm1_value = i2c_smbus_read_byte_data(client,
555 LM63_REG_PWM_VALUE);
556
Jean Delvarebc51ae12005-06-05 20:32:27 +0200557 data->temp8[0] = i2c_smbus_read_byte_data(client,
558 LM63_REG_LOCAL_TEMP);
559 data->temp8[1] = i2c_smbus_read_byte_data(client,
560 LM63_REG_LOCAL_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /* order matters for temp2_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200563 data->temp11[0] = i2c_smbus_read_byte_data(client,
564 LM63_REG_REMOTE_TEMP_MSB) << 8;
565 data->temp11[0] |= i2c_smbus_read_byte_data(client,
566 LM63_REG_REMOTE_TEMP_LSB);
567 data->temp11[1] = (i2c_smbus_read_byte_data(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 LM63_REG_REMOTE_LOW_MSB) << 8)
569 | i2c_smbus_read_byte_data(client,
570 LM63_REG_REMOTE_LOW_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200571 data->temp11[2] = (i2c_smbus_read_byte_data(client,
572 LM63_REG_REMOTE_HIGH_MSB) << 8)
573 | i2c_smbus_read_byte_data(client,
574 LM63_REG_REMOTE_HIGH_LSB);
575 data->temp8[2] = i2c_smbus_read_byte_data(client,
576 LM63_REG_REMOTE_TCRIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
578 LM63_REG_REMOTE_TCRIT_HYST);
579
580 data->alarms = i2c_smbus_read_byte_data(client,
581 LM63_REG_ALERT_STATUS) & 0x7F;
582
583 data->last_updated = jiffies;
584 data->valid = 1;
585 }
586
587 up(&data->update_lock);
588
589 return data;
590}
591
592static int __init sensors_lm63_init(void)
593{
594 return i2c_add_driver(&lm63_driver);
595}
596
597static void __exit sensors_lm63_exit(void)
598{
599 i2c_del_driver(&lm63_driver);
600}
601
602MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
603MODULE_DESCRIPTION("LM63 driver");
604MODULE_LICENSE("GPL");
605
606module_init(sensors_lm63_init);
607module_exit(sensors_lm63_exit);