]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lm63.c - driver for the National Semiconductor LM63 temperature sensor | |
3 | * with integrated fan control | |
4 | * Copyright (C) 2004-2008 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 | ||
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/hwmon-sysfs.h> | |
46 | #include <linux/hwmon.h> | |
47 | #include <linux/err.h> | |
48 | #include <linux/mutex.h> | |
49 | #include <linux/sysfs.h> | |
50 | ||
51 | /* | |
52 | * Addresses to scan | |
53 | * Address is fully defined internally and cannot be changed. | |
54 | */ | |
55 | ||
56 | static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; | |
57 | ||
58 | /* | |
59 | * The LM63 registers | |
60 | */ | |
61 | ||
62 | #define LM63_REG_CONFIG1 0x03 | |
63 | #define LM63_REG_CONFIG2 0xBF | |
64 | #define LM63_REG_CONFIG_FAN 0x4A | |
65 | ||
66 | #define LM63_REG_TACH_COUNT_MSB 0x47 | |
67 | #define LM63_REG_TACH_COUNT_LSB 0x46 | |
68 | #define LM63_REG_TACH_LIMIT_MSB 0x49 | |
69 | #define LM63_REG_TACH_LIMIT_LSB 0x48 | |
70 | ||
71 | #define LM63_REG_PWM_VALUE 0x4C | |
72 | #define LM63_REG_PWM_FREQ 0x4D | |
73 | ||
74 | #define LM63_REG_LOCAL_TEMP 0x00 | |
75 | #define LM63_REG_LOCAL_HIGH 0x05 | |
76 | ||
77 | #define LM63_REG_REMOTE_TEMP_MSB 0x01 | |
78 | #define LM63_REG_REMOTE_TEMP_LSB 0x10 | |
79 | #define LM63_REG_REMOTE_OFFSET_MSB 0x11 | |
80 | #define LM63_REG_REMOTE_OFFSET_LSB 0x12 | |
81 | #define LM63_REG_REMOTE_HIGH_MSB 0x07 | |
82 | #define LM63_REG_REMOTE_HIGH_LSB 0x13 | |
83 | #define LM63_REG_REMOTE_LOW_MSB 0x08 | |
84 | #define LM63_REG_REMOTE_LOW_LSB 0x14 | |
85 | #define LM63_REG_REMOTE_TCRIT 0x19 | |
86 | #define LM63_REG_REMOTE_TCRIT_HYST 0x21 | |
87 | ||
88 | #define LM63_REG_ALERT_STATUS 0x02 | |
89 | #define LM63_REG_ALERT_MASK 0x16 | |
90 | ||
91 | #define LM63_REG_MAN_ID 0xFE | |
92 | #define LM63_REG_CHIP_ID 0xFF | |
93 | ||
94 | /* | |
95 | * Conversions and various macros | |
96 | * For tachometer counts, the LM63 uses 16-bit values. | |
97 | * For local temperature and high limit, remote critical limit and hysteresis | |
98 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. | |
99 | * For remote temperature, low and high limits, it uses signed 11-bit values | |
100 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. | |
101 | */ | |
102 | ||
103 | #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ | |
104 | 5400000 / (reg)) | |
105 | #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ | |
106 | (5400000 / (val)) & 0xFFFC) | |
107 | #define TEMP8_FROM_REG(reg) ((reg) * 1000) | |
108 | #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \ | |
109 | (val) >= 127000 ? 127 : \ | |
110 | (val) < 0 ? ((val) - 500) / 1000 : \ | |
111 | ((val) + 500) / 1000) | |
112 | #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) | |
113 | #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \ | |
114 | (val) >= 127875 ? 0x7FE0 : \ | |
115 | (val) < 0 ? ((val) - 62) / 125 * 32 : \ | |
116 | ((val) + 62) / 125 * 32) | |
117 | #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ | |
118 | (val) >= 127000 ? 127 : \ | |
119 | ((val) + 500) / 1000) | |
120 | ||
121 | /* | |
122 | * Functions declaration | |
123 | */ | |
124 | ||
125 | static int lm63_probe(struct i2c_client *client, | |
126 | const struct i2c_device_id *id); | |
127 | static int lm63_remove(struct i2c_client *client); | |
128 | ||
129 | static struct lm63_data *lm63_update_device(struct device *dev); | |
130 | ||
131 | static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info); | |
132 | static void lm63_init_client(struct i2c_client *client); | |
133 | ||
134 | enum chips { lm63, lm64 }; | |
135 | ||
136 | /* | |
137 | * Driver data (common to all clients) | |
138 | */ | |
139 | ||
140 | static const struct i2c_device_id lm63_id[] = { | |
141 | { "lm63", lm63 }, | |
142 | { "lm64", lm64 }, | |
143 | { } | |
144 | }; | |
145 | MODULE_DEVICE_TABLE(i2c, lm63_id); | |
146 | ||
147 | static struct i2c_driver lm63_driver = { | |
148 | .class = I2C_CLASS_HWMON, | |
149 | .driver = { | |
150 | .name = "lm63", | |
151 | }, | |
152 | .probe = lm63_probe, | |
153 | .remove = lm63_remove, | |
154 | .id_table = lm63_id, | |
155 | .detect = lm63_detect, | |
156 | .address_list = normal_i2c, | |
157 | }; | |
158 | ||
159 | /* | |
160 | * Client data (each client gets its own) | |
161 | */ | |
162 | ||
163 | struct lm63_data { | |
164 | struct device *hwmon_dev; | |
165 | struct mutex update_lock; | |
166 | char valid; /* zero until following fields are valid */ | |
167 | unsigned long last_updated; /* in jiffies */ | |
168 | ||
169 | /* registers values */ | |
170 | u8 config, config_fan; | |
171 | u16 fan[2]; /* 0: input | |
172 | 1: low limit */ | |
173 | u8 pwm1_freq; | |
174 | u8 pwm1_value; | |
175 | s8 temp8[3]; /* 0: local input | |
176 | 1: local high limit | |
177 | 2: remote critical limit */ | |
178 | s16 temp11[3]; /* 0: remote input | |
179 | 1: remote low limit | |
180 | 2: remote high limit */ | |
181 | u8 temp2_crit_hyst; | |
182 | u8 alarms; | |
183 | }; | |
184 | ||
185 | /* | |
186 | * Sysfs callback functions and files | |
187 | */ | |
188 | ||
189 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, | |
190 | char *buf) | |
191 | { | |
192 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
193 | struct lm63_data *data = lm63_update_device(dev); | |
194 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); | |
195 | } | |
196 | ||
197 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, | |
198 | const char *buf, size_t count) | |
199 | { | |
200 | struct i2c_client *client = to_i2c_client(dev); | |
201 | struct lm63_data *data = i2c_get_clientdata(client); | |
202 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
203 | ||
204 | mutex_lock(&data->update_lock); | |
205 | data->fan[1] = FAN_TO_REG(val); | |
206 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, | |
207 | data->fan[1] & 0xFF); | |
208 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, | |
209 | data->fan[1] >> 8); | |
210 | mutex_unlock(&data->update_lock); | |
211 | return count; | |
212 | } | |
213 | ||
214 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, | |
215 | char *buf) | |
216 | { | |
217 | struct lm63_data *data = lm63_update_device(dev); | |
218 | return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ? | |
219 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / | |
220 | (2 * data->pwm1_freq)); | |
221 | } | |
222 | ||
223 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, | |
224 | const char *buf, size_t count) | |
225 | { | |
226 | struct i2c_client *client = to_i2c_client(dev); | |
227 | struct lm63_data *data = i2c_get_clientdata(client); | |
228 | unsigned long val; | |
229 | ||
230 | if (!(data->config_fan & 0x20)) /* register is read-only */ | |
231 | return -EPERM; | |
232 | ||
233 | val = simple_strtoul(buf, NULL, 10); | |
234 | mutex_lock(&data->update_lock); | |
235 | data->pwm1_value = val <= 0 ? 0 : | |
236 | val >= 255 ? 2 * data->pwm1_freq : | |
237 | (val * data->pwm1_freq * 2 + 127) / 255; | |
238 | i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value); | |
239 | mutex_unlock(&data->update_lock); | |
240 | return count; | |
241 | } | |
242 | ||
243 | static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy, | |
244 | char *buf) | |
245 | { | |
246 | struct lm63_data *data = lm63_update_device(dev); | |
247 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); | |
248 | } | |
249 | ||
250 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, | |
251 | char *buf) | |
252 | { | |
253 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
254 | struct lm63_data *data = lm63_update_device(dev); | |
255 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); | |
256 | } | |
257 | ||
258 | static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy, | |
259 | const char *buf, size_t count) | |
260 | { | |
261 | struct i2c_client *client = to_i2c_client(dev); | |
262 | struct lm63_data *data = i2c_get_clientdata(client); | |
263 | long val = simple_strtol(buf, NULL, 10); | |
264 | ||
265 | mutex_lock(&data->update_lock); | |
266 | data->temp8[1] = TEMP8_TO_REG(val); | |
267 | i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]); | |
268 | mutex_unlock(&data->update_lock); | |
269 | return count; | |
270 | } | |
271 | ||
272 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | |
273 | char *buf) | |
274 | { | |
275 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
276 | struct lm63_data *data = lm63_update_device(dev); | |
277 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index])); | |
278 | } | |
279 | ||
280 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | |
281 | const char *buf, size_t count) | |
282 | { | |
283 | static const u8 reg[4] = { | |
284 | LM63_REG_REMOTE_LOW_MSB, | |
285 | LM63_REG_REMOTE_LOW_LSB, | |
286 | LM63_REG_REMOTE_HIGH_MSB, | |
287 | LM63_REG_REMOTE_HIGH_LSB, | |
288 | }; | |
289 | ||
290 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
291 | struct i2c_client *client = to_i2c_client(dev); | |
292 | struct lm63_data *data = i2c_get_clientdata(client); | |
293 | long val = simple_strtol(buf, NULL, 10); | |
294 | int nr = attr->index; | |
295 | ||
296 | mutex_lock(&data->update_lock); | |
297 | data->temp11[nr] = TEMP11_TO_REG(val); | |
298 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], | |
299 | data->temp11[nr] >> 8); | |
300 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], | |
301 | data->temp11[nr] & 0xff); | |
302 | mutex_unlock(&data->update_lock); | |
303 | return count; | |
304 | } | |
305 | ||
306 | /* Hysteresis register holds a relative value, while we want to present | |
307 | an absolute to user-space */ | |
308 | static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, | |
309 | char *buf) | |
310 | { | |
311 | struct lm63_data *data = lm63_update_device(dev); | |
312 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2]) | |
313 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); | |
314 | } | |
315 | ||
316 | /* And now the other way around, user-space provides an absolute | |
317 | hysteresis value and we have to store a relative one */ | |
318 | static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, | |
319 | const char *buf, size_t count) | |
320 | { | |
321 | struct i2c_client *client = to_i2c_client(dev); | |
322 | struct lm63_data *data = i2c_get_clientdata(client); | |
323 | long val = simple_strtol(buf, NULL, 10); | |
324 | long hyst; | |
325 | ||
326 | mutex_lock(&data->update_lock); | |
327 | hyst = TEMP8_FROM_REG(data->temp8[2]) - val; | |
328 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, | |
329 | HYST_TO_REG(hyst)); | |
330 | mutex_unlock(&data->update_lock); | |
331 | return count; | |
332 | } | |
333 | ||
334 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, | |
335 | char *buf) | |
336 | { | |
337 | struct lm63_data *data = lm63_update_device(dev); | |
338 | return sprintf(buf, "%u\n", data->alarms); | |
339 | } | |
340 | ||
341 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, | |
342 | char *buf) | |
343 | { | |
344 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
345 | struct lm63_data *data = lm63_update_device(dev); | |
346 | int bitnr = attr->index; | |
347 | ||
348 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
349 | } | |
350 | ||
351 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); | |
352 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, | |
353 | set_fan, 1); | |
354 | ||
355 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); | |
356 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); | |
357 | ||
358 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0); | |
359 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, | |
360 | set_temp8, 1); | |
361 | ||
362 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); | |
363 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | |
364 | set_temp11, 1); | |
365 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | |
366 | set_temp11, 2); | |
367 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2); | |
368 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, | |
369 | set_temp2_crit_hyst); | |
370 | ||
371 | /* Individual alarm files */ | |
372 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); | |
373 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | |
374 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | |
375 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | |
376 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
377 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | |
378 | /* Raw alarm file for compatibility */ | |
379 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
380 | ||
381 | static struct attribute *lm63_attributes[] = { | |
382 | &dev_attr_pwm1.attr, | |
383 | &dev_attr_pwm1_enable.attr, | |
384 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
385 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
386 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
387 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
388 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
389 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
390 | &dev_attr_temp2_crit_hyst.attr, | |
391 | ||
392 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
393 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | |
394 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
395 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
396 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
397 | &dev_attr_alarms.attr, | |
398 | NULL | |
399 | }; | |
400 | ||
401 | static const struct attribute_group lm63_group = { | |
402 | .attrs = lm63_attributes, | |
403 | }; | |
404 | ||
405 | static struct attribute *lm63_attributes_fan1[] = { | |
406 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
407 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
408 | ||
409 | &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, | |
410 | NULL | |
411 | }; | |
412 | ||
413 | static const struct attribute_group lm63_group_fan1 = { | |
414 | .attrs = lm63_attributes_fan1, | |
415 | }; | |
416 | ||
417 | /* | |
418 | * Real code | |
419 | */ | |
420 | ||
421 | /* Return 0 if detection is successful, -ENODEV otherwise */ | |
422 | static int lm63_detect(struct i2c_client *new_client, | |
423 | struct i2c_board_info *info) | |
424 | { | |
425 | struct i2c_adapter *adapter = new_client->adapter; | |
426 | u8 man_id, chip_id, reg_config1, reg_config2; | |
427 | u8 reg_alert_status, reg_alert_mask; | |
428 | int address = new_client->addr; | |
429 | ||
430 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
431 | return -ENODEV; | |
432 | ||
433 | man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID); | |
434 | chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID); | |
435 | ||
436 | reg_config1 = i2c_smbus_read_byte_data(new_client, | |
437 | LM63_REG_CONFIG1); | |
438 | reg_config2 = i2c_smbus_read_byte_data(new_client, | |
439 | LM63_REG_CONFIG2); | |
440 | reg_alert_status = i2c_smbus_read_byte_data(new_client, | |
441 | LM63_REG_ALERT_STATUS); | |
442 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, | |
443 | LM63_REG_ALERT_MASK); | |
444 | ||
445 | if (man_id != 0x01 /* National Semiconductor */ | |
446 | || (reg_config1 & 0x18) != 0x00 | |
447 | || (reg_config2 & 0xF8) != 0x00 | |
448 | || (reg_alert_status & 0x20) != 0x00 | |
449 | || (reg_alert_mask & 0xA4) != 0xA4) { | |
450 | dev_dbg(&adapter->dev, | |
451 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", | |
452 | man_id, chip_id); | |
453 | return -ENODEV; | |
454 | } | |
455 | ||
456 | if (chip_id == 0x41 && address == 0x4c) | |
457 | strlcpy(info->type, "lm63", I2C_NAME_SIZE); | |
458 | else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e)) | |
459 | strlcpy(info->type, "lm64", I2C_NAME_SIZE); | |
460 | else | |
461 | return -ENODEV; | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
466 | static int lm63_probe(struct i2c_client *new_client, | |
467 | const struct i2c_device_id *id) | |
468 | { | |
469 | struct lm63_data *data; | |
470 | int err; | |
471 | ||
472 | data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL); | |
473 | if (!data) { | |
474 | err = -ENOMEM; | |
475 | goto exit; | |
476 | } | |
477 | ||
478 | i2c_set_clientdata(new_client, data); | |
479 | data->valid = 0; | |
480 | mutex_init(&data->update_lock); | |
481 | ||
482 | /* Initialize the LM63 chip */ | |
483 | lm63_init_client(new_client); | |
484 | ||
485 | /* Register sysfs hooks */ | |
486 | if ((err = sysfs_create_group(&new_client->dev.kobj, | |
487 | &lm63_group))) | |
488 | goto exit_free; | |
489 | if (data->config & 0x04) { /* tachometer enabled */ | |
490 | if ((err = sysfs_create_group(&new_client->dev.kobj, | |
491 | &lm63_group_fan1))) | |
492 | goto exit_remove_files; | |
493 | } | |
494 | ||
495 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | |
496 | if (IS_ERR(data->hwmon_dev)) { | |
497 | err = PTR_ERR(data->hwmon_dev); | |
498 | goto exit_remove_files; | |
499 | } | |
500 | ||
501 | return 0; | |
502 | ||
503 | exit_remove_files: | |
504 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group); | |
505 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1); | |
506 | exit_free: | |
507 | kfree(data); | |
508 | exit: | |
509 | return err; | |
510 | } | |
511 | ||
512 | /* Idealy we shouldn't have to initialize anything, since the BIOS | |
513 | should have taken care of everything */ | |
514 | static void lm63_init_client(struct i2c_client *client) | |
515 | { | |
516 | struct lm63_data *data = i2c_get_clientdata(client); | |
517 | ||
518 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); | |
519 | data->config_fan = i2c_smbus_read_byte_data(client, | |
520 | LM63_REG_CONFIG_FAN); | |
521 | ||
522 | /* Start converting if needed */ | |
523 | if (data->config & 0x40) { /* standby */ | |
524 | dev_dbg(&client->dev, "Switching to operational mode\n"); | |
525 | data->config &= 0xA7; | |
526 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, | |
527 | data->config); | |
528 | } | |
529 | ||
530 | /* We may need pwm1_freq before ever updating the client data */ | |
531 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); | |
532 | if (data->pwm1_freq == 0) | |
533 | data->pwm1_freq = 1; | |
534 | ||
535 | /* Show some debug info about the LM63 configuration */ | |
536 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", | |
537 | (data->config & 0x04) ? "tachometer input" : | |
538 | "alert output"); | |
539 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", | |
540 | (data->config_fan & 0x08) ? "1.4" : "360", | |
541 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); | |
542 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", | |
543 | (data->config_fan & 0x10) ? "low" : "high", | |
544 | (data->config_fan & 0x20) ? "manual" : "auto"); | |
545 | } | |
546 | ||
547 | static int lm63_remove(struct i2c_client *client) | |
548 | { | |
549 | struct lm63_data *data = i2c_get_clientdata(client); | |
550 | ||
551 | hwmon_device_unregister(data->hwmon_dev); | |
552 | sysfs_remove_group(&client->dev.kobj, &lm63_group); | |
553 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); | |
554 | ||
555 | kfree(data); | |
556 | return 0; | |
557 | } | |
558 | ||
559 | static struct lm63_data *lm63_update_device(struct device *dev) | |
560 | { | |
561 | struct i2c_client *client = to_i2c_client(dev); | |
562 | struct lm63_data *data = i2c_get_clientdata(client); | |
563 | ||
564 | mutex_lock(&data->update_lock); | |
565 | ||
566 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | |
567 | if (data->config & 0x04) { /* tachometer enabled */ | |
568 | /* order matters for fan1_input */ | |
569 | data->fan[0] = i2c_smbus_read_byte_data(client, | |
570 | LM63_REG_TACH_COUNT_LSB) & 0xFC; | |
571 | data->fan[0] |= i2c_smbus_read_byte_data(client, | |
572 | LM63_REG_TACH_COUNT_MSB) << 8; | |
573 | data->fan[1] = (i2c_smbus_read_byte_data(client, | |
574 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) | |
575 | | (i2c_smbus_read_byte_data(client, | |
576 | LM63_REG_TACH_LIMIT_MSB) << 8); | |
577 | } | |
578 | ||
579 | data->pwm1_freq = i2c_smbus_read_byte_data(client, | |
580 | LM63_REG_PWM_FREQ); | |
581 | if (data->pwm1_freq == 0) | |
582 | data->pwm1_freq = 1; | |
583 | data->pwm1_value = i2c_smbus_read_byte_data(client, | |
584 | LM63_REG_PWM_VALUE); | |
585 | ||
586 | data->temp8[0] = i2c_smbus_read_byte_data(client, | |
587 | LM63_REG_LOCAL_TEMP); | |
588 | data->temp8[1] = i2c_smbus_read_byte_data(client, | |
589 | LM63_REG_LOCAL_HIGH); | |
590 | ||
591 | /* order matters for temp2_input */ | |
592 | data->temp11[0] = i2c_smbus_read_byte_data(client, | |
593 | LM63_REG_REMOTE_TEMP_MSB) << 8; | |
594 | data->temp11[0] |= i2c_smbus_read_byte_data(client, | |
595 | LM63_REG_REMOTE_TEMP_LSB); | |
596 | data->temp11[1] = (i2c_smbus_read_byte_data(client, | |
597 | LM63_REG_REMOTE_LOW_MSB) << 8) | |
598 | | i2c_smbus_read_byte_data(client, | |
599 | LM63_REG_REMOTE_LOW_LSB); | |
600 | data->temp11[2] = (i2c_smbus_read_byte_data(client, | |
601 | LM63_REG_REMOTE_HIGH_MSB) << 8) | |
602 | | i2c_smbus_read_byte_data(client, | |
603 | LM63_REG_REMOTE_HIGH_LSB); | |
604 | data->temp8[2] = i2c_smbus_read_byte_data(client, | |
605 | LM63_REG_REMOTE_TCRIT); | |
606 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, | |
607 | LM63_REG_REMOTE_TCRIT_HYST); | |
608 | ||
609 | data->alarms = i2c_smbus_read_byte_data(client, | |
610 | LM63_REG_ALERT_STATUS) & 0x7F; | |
611 | ||
612 | data->last_updated = jiffies; | |
613 | data->valid = 1; | |
614 | } | |
615 | ||
616 | mutex_unlock(&data->update_lock); | |
617 | ||
618 | return data; | |
619 | } | |
620 | ||
621 | static int __init sensors_lm63_init(void) | |
622 | { | |
623 | return i2c_add_driver(&lm63_driver); | |
624 | } | |
625 | ||
626 | static void __exit sensors_lm63_exit(void) | |
627 | { | |
628 | i2c_del_driver(&lm63_driver); | |
629 | } | |
630 | ||
631 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | |
632 | MODULE_DESCRIPTION("LM63 driver"); | |
633 | MODULE_LICENSE("GPL"); | |
634 | ||
635 | module_init(sensors_lm63_init); | |
636 | module_exit(sensors_lm63_exit); |