blob: be5c7095ecbb2648bf86a05583373b0e9a30a157 [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>
Jean Delvare10c08f82005-06-06 19:34:45 +020045#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040046#include <linux/hwmon.h>
47#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Addresses to scan
51 * Address is fully defined internally and cannot be changed.
52 */
53
54static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * Insmod parameters
58 */
59
Jean Delvaref4b50262005-07-31 21:49:03 +020060I2C_CLIENT_INSMOD_1(lm63);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * The LM63 registers
64 */
65
66#define LM63_REG_CONFIG1 0x03
67#define LM63_REG_CONFIG2 0xBF
68#define LM63_REG_CONFIG_FAN 0x4A
69
70#define LM63_REG_TACH_COUNT_MSB 0x47
71#define LM63_REG_TACH_COUNT_LSB 0x46
72#define LM63_REG_TACH_LIMIT_MSB 0x49
73#define LM63_REG_TACH_LIMIT_LSB 0x48
74
75#define LM63_REG_PWM_VALUE 0x4C
76#define LM63_REG_PWM_FREQ 0x4D
77
78#define LM63_REG_LOCAL_TEMP 0x00
79#define LM63_REG_LOCAL_HIGH 0x05
80
81#define LM63_REG_REMOTE_TEMP_MSB 0x01
82#define LM63_REG_REMOTE_TEMP_LSB 0x10
83#define LM63_REG_REMOTE_OFFSET_MSB 0x11
84#define LM63_REG_REMOTE_OFFSET_LSB 0x12
85#define LM63_REG_REMOTE_HIGH_MSB 0x07
86#define LM63_REG_REMOTE_HIGH_LSB 0x13
87#define LM63_REG_REMOTE_LOW_MSB 0x08
88#define LM63_REG_REMOTE_LOW_LSB 0x14
89#define LM63_REG_REMOTE_TCRIT 0x19
90#define LM63_REG_REMOTE_TCRIT_HYST 0x21
91
92#define LM63_REG_ALERT_STATUS 0x02
93#define LM63_REG_ALERT_MASK 0x16
94
95#define LM63_REG_MAN_ID 0xFE
96#define LM63_REG_CHIP_ID 0xFF
97
98/*
99 * Conversions and various macros
100 * For tachometer counts, the LM63 uses 16-bit values.
101 * For local temperature and high limit, remote critical limit and hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600102 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * For remote temperature, low and high limits, it uses signed 11-bit values
Steven Cole44bbe872005-05-03 18:21:25 -0600104 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
106
107#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
108 5400000 / (reg))
109#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
110 (5400000 / (val)) & 0xFFFC)
111#define TEMP8_FROM_REG(reg) ((reg) * 1000)
112#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
113 (val) >= 127000 ? 127 : \
114 (val) < 0 ? ((val) - 500) / 1000 : \
115 ((val) + 500) / 1000)
116#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
117#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
118 (val) >= 127875 ? 0x7FE0 : \
119 (val) < 0 ? ((val) - 62) / 125 * 32 : \
120 ((val) + 62) / 125 * 32)
121#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
122 (val) >= 127000 ? 127 : \
123 ((val) + 500) / 1000)
124
125/*
126 * Functions declaration
127 */
128
129static int lm63_attach_adapter(struct i2c_adapter *adapter);
130static int lm63_detach_client(struct i2c_client *client);
131
132static struct lm63_data *lm63_update_device(struct device *dev);
133
134static int lm63_detect(struct i2c_adapter *adapter, int address, int kind);
135static void lm63_init_client(struct i2c_client *client);
136
137/*
138 * Driver data (common to all clients)
139 */
140
141static struct i2c_driver lm63_driver = {
142 .owner = THIS_MODULE,
143 .name = "lm63",
144 .flags = I2C_DF_NOTIFY,
145 .attach_adapter = lm63_attach_adapter,
146 .detach_client = lm63_detach_client,
147};
148
149/*
150 * Client data (each client gets its own)
151 */
152
153struct lm63_data {
154 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400155 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct semaphore update_lock;
157 char valid; /* zero until following fields are valid */
158 unsigned long last_updated; /* in jiffies */
159
160 /* registers values */
161 u8 config, config_fan;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200162 u16 fan[2]; /* 0: input
163 1: low limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 u8 pwm1_freq;
165 u8 pwm1_value;
Jean Delvarebc51ae12005-06-05 20:32:27 +0200166 s8 temp8[3]; /* 0: local input
167 1: local high limit
168 2: remote critical limit */
169 s16 temp11[3]; /* 0: remote input
170 1: remote low limit
171 2: remote high limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 u8 temp2_crit_hyst;
173 u8 alarms;
174};
175
176/*
177 * Sysfs callback functions and files
178 */
179
Jean Delvarebc51ae12005-06-05 20:32:27 +0200180static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
181 char *buf)
182{
183 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184 struct lm63_data *data = lm63_update_device(dev);
185 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Jean Delvarebc51ae12005-06-05 20:32:27 +0200188static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
189 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct i2c_client *client = to_i2c_client(dev);
192 struct lm63_data *data = i2c_get_clientdata(client);
193 unsigned long val = simple_strtoul(buf, NULL, 10);
194
195 down(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200196 data->fan[1] = FAN_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200198 data->fan[1] & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
Jean Delvarebc51ae12005-06-05 20:32:27 +0200200 data->fan[1] >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 up(&data->update_lock);
202 return count;
203}
204
Jean Delvarebc51ae12005-06-05 20:32:27 +0200205static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
206 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct lm63_data *data = lm63_update_device(dev);
209 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
210 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
211 (2 * data->pwm1_freq));
212}
213
Jean Delvarebc51ae12005-06-05 20:32:27 +0200214static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
215 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct i2c_client *client = to_i2c_client(dev);
218 struct lm63_data *data = i2c_get_clientdata(client);
219 unsigned long val;
220
221 if (!(data->config_fan & 0x20)) /* register is read-only */
222 return -EPERM;
223
224 val = simple_strtoul(buf, NULL, 10);
225 down(&data->update_lock);
226 data->pwm1_value = val <= 0 ? 0 :
227 val >= 255 ? 2 * data->pwm1_freq :
228 (val * data->pwm1_freq * 2 + 127) / 255;
229 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
230 up(&data->update_lock);
231 return count;
232}
233
Jean Delvarebc51ae12005-06-05 20:32:27 +0200234static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
235 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct lm63_data *data = lm63_update_device(dev);
238 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
239}
240
Jean Delvarebc51ae12005-06-05 20:32:27 +0200241static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
242 char *buf)
243{
244 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
245 struct lm63_data *data = lm63_update_device(dev);
246 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jean Delvarebc51ae12005-06-05 20:32:27 +0200249static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
250 const char *buf, size_t count)
251{
252 struct i2c_client *client = to_i2c_client(dev);
253 struct lm63_data *data = i2c_get_clientdata(client);
254 long val = simple_strtol(buf, NULL, 10);
255
256 down(&data->update_lock);
257 data->temp8[1] = TEMP8_TO_REG(val);
258 i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
259 up(&data->update_lock);
260 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200262
263static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
264 char *buf)
265{
266 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
267 struct lm63_data *data = lm63_update_device(dev);
268 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Jean Delvarebc51ae12005-06-05 20:32:27 +0200270
271static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
272 const char *buf, size_t count)
273{
274 static const u8 reg[4] = {
275 LM63_REG_REMOTE_LOW_MSB,
276 LM63_REG_REMOTE_LOW_LSB,
277 LM63_REG_REMOTE_HIGH_MSB,
278 LM63_REG_REMOTE_HIGH_LSB,
279 };
280
281 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
282 struct i2c_client *client = to_i2c_client(dev);
283 struct lm63_data *data = i2c_get_clientdata(client);
284 long val = simple_strtol(buf, NULL, 10);
285 int nr = attr->index;
286
287 down(&data->update_lock);
288 data->temp11[nr] = TEMP11_TO_REG(val);
289 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
290 data->temp11[nr] >> 8);
291 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
292 data->temp11[nr] & 0xff);
293 up(&data->update_lock);
294 return count;
295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/* Hysteresis register holds a relative value, while we want to present
298 an absolute to user-space */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200299static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
300 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct lm63_data *data = lm63_update_device(dev);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200303 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 - TEMP8_FROM_REG(data->temp2_crit_hyst));
305}
306
307/* And now the other way around, user-space provides an absolute
308 hysteresis value and we have to store a relative one */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200309static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
310 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct i2c_client *client = to_i2c_client(dev);
313 struct lm63_data *data = i2c_get_clientdata(client);
314 long val = simple_strtol(buf, NULL, 10);
315 long hyst;
316
317 down(&data->update_lock);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200318 hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
320 HYST_TO_REG(hyst));
321 up(&data->update_lock);
322 return count;
323}
324
Jean Delvarebc51ae12005-06-05 20:32:27 +0200325static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
326 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct lm63_data *data = lm63_update_device(dev);
329 return sprintf(buf, "%u\n", data->alarms);
330}
331
Jean Delvarebc51ae12005-06-05 20:32:27 +0200332static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
333static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
334 set_fan, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
337static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
338
Jean Delvarebc51ae12005-06-05 20:32:27 +0200339static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
340static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
341 set_temp8, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jean Delvarebc51ae12005-06-05 20:32:27 +0200343static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
344static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
345 set_temp11, 1);
346static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
347 set_temp11, 2);
348static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
350 set_temp2_crit_hyst);
351
352static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
353
354/*
355 * Real code
356 */
357
358static int lm63_attach_adapter(struct i2c_adapter *adapter)
359{
360 if (!(adapter->class & I2C_CLASS_HWMON))
361 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200362 return i2c_probe(adapter, &addr_data, lm63_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/*
366 * The following function does more than just detection. If detection
367 * succeeds, it also registers the new chip.
368 */
369static int lm63_detect(struct i2c_adapter *adapter, int address, int kind)
370{
371 struct i2c_client *new_client;
372 struct lm63_data *data;
373 int err = 0;
374
375 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
376 goto exit;
377
378 if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) {
379 err = -ENOMEM;
380 goto exit;
381 }
382 memset(data, 0, sizeof(struct lm63_data));
383
384 /* The common I2C client data is placed right before the
385 LM63-specific data. */
386 new_client = &data->client;
387 i2c_set_clientdata(new_client, data);
388 new_client->addr = address;
389 new_client->adapter = adapter;
390 new_client->driver = &lm63_driver;
391 new_client->flags = 0;
392
393 /* Default to an LM63 if forced */
394 if (kind == 0)
395 kind = lm63;
396
397 if (kind < 0) { /* must identify */
398 u8 man_id, chip_id, reg_config1, reg_config2;
399 u8 reg_alert_status, reg_alert_mask;
400
401 man_id = i2c_smbus_read_byte_data(new_client,
402 LM63_REG_MAN_ID);
403 chip_id = i2c_smbus_read_byte_data(new_client,
404 LM63_REG_CHIP_ID);
405 reg_config1 = i2c_smbus_read_byte_data(new_client,
406 LM63_REG_CONFIG1);
407 reg_config2 = i2c_smbus_read_byte_data(new_client,
408 LM63_REG_CONFIG2);
409 reg_alert_status = i2c_smbus_read_byte_data(new_client,
410 LM63_REG_ALERT_STATUS);
411 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
412 LM63_REG_ALERT_MASK);
413
414 if (man_id == 0x01 /* National Semiconductor */
415 && chip_id == 0x41 /* LM63 */
416 && (reg_config1 & 0x18) == 0x00
417 && (reg_config2 & 0xF8) == 0x00
418 && (reg_alert_status & 0x20) == 0x00
419 && (reg_alert_mask & 0xA4) == 0xA4) {
420 kind = lm63;
421 } else { /* failed */
422 dev_dbg(&adapter->dev, "Unsupported chip "
423 "(man_id=0x%02X, chip_id=0x%02X).\n",
424 man_id, chip_id);
425 goto exit_free;
426 }
427 }
428
429 strlcpy(new_client->name, "lm63", I2C_NAME_SIZE);
430 data->valid = 0;
431 init_MUTEX(&data->update_lock);
432
433 /* Tell the I2C layer a new client has arrived */
434 if ((err = i2c_attach_client(new_client)))
435 goto exit_free;
436
437 /* Initialize the LM63 chip */
438 lm63_init_client(new_client);
439
440 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400441 data->class_dev = hwmon_device_register(&new_client->dev);
442 if (IS_ERR(data->class_dev)) {
443 err = PTR_ERR(data->class_dev);
444 goto exit_detach;
445 }
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (data->config & 0x04) { /* tachometer enabled */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200448 device_create_file(&new_client->dev,
449 &sensor_dev_attr_fan1_input.dev_attr);
450 device_create_file(&new_client->dev,
451 &sensor_dev_attr_fan1_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 device_create_file(&new_client->dev, &dev_attr_pwm1);
454 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200455 device_create_file(&new_client->dev,
456 &sensor_dev_attr_temp1_input.dev_attr);
457 device_create_file(&new_client->dev,
458 &sensor_dev_attr_temp2_input.dev_attr);
459 device_create_file(&new_client->dev,
460 &sensor_dev_attr_temp2_min.dev_attr);
461 device_create_file(&new_client->dev,
462 &sensor_dev_attr_temp1_max.dev_attr);
463 device_create_file(&new_client->dev,
464 &sensor_dev_attr_temp2_max.dev_attr);
465 device_create_file(&new_client->dev,
466 &sensor_dev_attr_temp2_crit.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
468 device_create_file(&new_client->dev, &dev_attr_alarms);
469
470 return 0;
471
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400472exit_detach:
473 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474exit_free:
475 kfree(data);
476exit:
477 return err;
478}
479
480/* Idealy we shouldn't have to initialize anything, since the BIOS
481 should have taken care of everything */
482static void lm63_init_client(struct i2c_client *client)
483{
484 struct lm63_data *data = i2c_get_clientdata(client);
485
486 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
487 data->config_fan = i2c_smbus_read_byte_data(client,
488 LM63_REG_CONFIG_FAN);
489
490 /* Start converting if needed */
491 if (data->config & 0x40) { /* standby */
492 dev_dbg(&client->dev, "Switching to operational mode");
493 data->config &= 0xA7;
494 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
495 data->config);
496 }
497
498 /* We may need pwm1_freq before ever updating the client data */
499 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
500 if (data->pwm1_freq == 0)
501 data->pwm1_freq = 1;
502
503 /* Show some debug info about the LM63 configuration */
504 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
505 (data->config & 0x04) ? "tachometer input" :
506 "alert output");
507 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
508 (data->config_fan & 0x08) ? "1.4" : "360",
509 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
510 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
511 (data->config_fan & 0x10) ? "low" : "high",
512 (data->config_fan & 0x20) ? "manual" : "auto");
513}
514
515static int lm63_detach_client(struct i2c_client *client)
516{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400517 struct lm63_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int err;
519
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400520 hwmon_device_unregister(data->class_dev);
521
Jean Delvare7bef5592005-07-27 22:14:49 +0200522 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400525 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return 0;
527}
528
529static struct lm63_data *lm63_update_device(struct device *dev)
530{
531 struct i2c_client *client = to_i2c_client(dev);
532 struct lm63_data *data = i2c_get_clientdata(client);
533
534 down(&data->update_lock);
535
536 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
537 if (data->config & 0x04) { /* tachometer enabled */
538 /* order matters for fan1_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200539 data->fan[0] = i2c_smbus_read_byte_data(client,
540 LM63_REG_TACH_COUNT_LSB) & 0xFC;
541 data->fan[0] |= i2c_smbus_read_byte_data(client,
542 LM63_REG_TACH_COUNT_MSB) << 8;
543 data->fan[1] = (i2c_smbus_read_byte_data(client,
544 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
545 | (i2c_smbus_read_byte_data(client,
546 LM63_REG_TACH_LIMIT_MSB) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 data->pwm1_freq = i2c_smbus_read_byte_data(client,
550 LM63_REG_PWM_FREQ);
551 if (data->pwm1_freq == 0)
552 data->pwm1_freq = 1;
553 data->pwm1_value = i2c_smbus_read_byte_data(client,
554 LM63_REG_PWM_VALUE);
555
Jean Delvarebc51ae12005-06-05 20:32:27 +0200556 data->temp8[0] = i2c_smbus_read_byte_data(client,
557 LM63_REG_LOCAL_TEMP);
558 data->temp8[1] = i2c_smbus_read_byte_data(client,
559 LM63_REG_LOCAL_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* order matters for temp2_input */
Jean Delvarebc51ae12005-06-05 20:32:27 +0200562 data->temp11[0] = i2c_smbus_read_byte_data(client,
563 LM63_REG_REMOTE_TEMP_MSB) << 8;
564 data->temp11[0] |= i2c_smbus_read_byte_data(client,
565 LM63_REG_REMOTE_TEMP_LSB);
566 data->temp11[1] = (i2c_smbus_read_byte_data(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 LM63_REG_REMOTE_LOW_MSB) << 8)
568 | i2c_smbus_read_byte_data(client,
569 LM63_REG_REMOTE_LOW_LSB);
Jean Delvarebc51ae12005-06-05 20:32:27 +0200570 data->temp11[2] = (i2c_smbus_read_byte_data(client,
571 LM63_REG_REMOTE_HIGH_MSB) << 8)
572 | i2c_smbus_read_byte_data(client,
573 LM63_REG_REMOTE_HIGH_LSB);
574 data->temp8[2] = i2c_smbus_read_byte_data(client,
575 LM63_REG_REMOTE_TCRIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
577 LM63_REG_REMOTE_TCRIT_HYST);
578
579 data->alarms = i2c_smbus_read_byte_data(client,
580 LM63_REG_ALERT_STATUS) & 0x7F;
581
582 data->last_updated = jiffies;
583 data->valid = 1;
584 }
585
586 up(&data->update_lock);
587
588 return data;
589}
590
591static int __init sensors_lm63_init(void)
592{
593 return i2c_add_driver(&lm63_driver);
594}
595
596static void __exit sensors_lm63_exit(void)
597{
598 i2c_del_driver(&lm63_driver);
599}
600
601MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
602MODULE_DESCRIPTION("LM63 driver");
603MODULE_LICENSE("GPL");
604
605module_init(sensors_lm63_init);
606module_exit(sensors_lm63_exit);