]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * lm63.c - driver for the National Semiconductor LM63 temperature sensor | |
3 | * with integrated fan control | |
2d45771e | 4 | * Copyright (C) 2004-2006 Jean Delvare <khali@linux-fr.org> |
1da177e4 LT |
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 | ||
1da177e4 LT |
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> | |
10c08f81 | 45 | #include <linux/hwmon-sysfs.h> |
943b0830 MH |
46 | #include <linux/hwmon.h> |
47 | #include <linux/err.h> | |
9a61bf63 | 48 | #include <linux/mutex.h> |
1da177e4 LT |
49 | |
50 | /* | |
51 | * Addresses to scan | |
52 | * Address is fully defined internally and cannot be changed. | |
53 | */ | |
54 | ||
55 | static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; | |
1da177e4 LT |
56 | |
57 | /* | |
58 | * Insmod parameters | |
59 | */ | |
60 | ||
f4b50261 | 61 | I2C_CLIENT_INSMOD_1(lm63); |
1da177e4 LT |
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 | |
44bbe87e | 103 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. |
1da177e4 | 104 | * For remote temperature, low and high limits, it uses signed 11-bit values |
44bbe87e | 105 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. |
1da177e4 LT |
106 | */ |
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 | ||
130 | static int lm63_attach_adapter(struct i2c_adapter *adapter); | |
131 | static int lm63_detach_client(struct i2c_client *client); | |
132 | ||
133 | static struct lm63_data *lm63_update_device(struct device *dev); | |
134 | ||
135 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind); | |
136 | static void lm63_init_client(struct i2c_client *client); | |
137 | ||
138 | /* | |
139 | * Driver data (common to all clients) | |
140 | */ | |
141 | ||
142 | static struct i2c_driver lm63_driver = { | |
cdaf7934 | 143 | .driver = { |
cdaf7934 LR |
144 | .name = "lm63", |
145 | }, | |
1da177e4 LT |
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 | ||
154 | struct lm63_data { | |
155 | struct i2c_client client; | |
943b0830 | 156 | struct class_device *class_dev; |
9a61bf63 | 157 | struct mutex update_lock; |
1da177e4 LT |
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; | |
bc51ae11 JD |
163 | u16 fan[2]; /* 0: input |
164 | 1: low limit */ | |
1da177e4 LT |
165 | u8 pwm1_freq; |
166 | u8 pwm1_value; | |
bc51ae11 JD |
167 | 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 */ | |
1da177e4 LT |
173 | u8 temp2_crit_hyst; |
174 | u8 alarms; | |
175 | }; | |
176 | ||
177 | /* | |
178 | * Sysfs callback functions and files | |
179 | */ | |
180 | ||
bc51ae11 JD |
181 | static 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])); | |
1da177e4 | 187 | } |
1da177e4 | 188 | |
bc51ae11 JD |
189 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
190 | const char *buf, size_t count) | |
1da177e4 LT |
191 | { |
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 | ||
9a61bf63 | 196 | mutex_lock(&data->update_lock); |
bc51ae11 | 197 | data->fan[1] = FAN_TO_REG(val); |
1da177e4 | 198 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
bc51ae11 | 199 | data->fan[1] & 0xFF); |
1da177e4 | 200 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
bc51ae11 | 201 | data->fan[1] >> 8); |
9a61bf63 | 202 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
203 | return count; |
204 | } | |
205 | ||
bc51ae11 JD |
206 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, |
207 | char *buf) | |
1da177e4 LT |
208 | { |
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 | ||
bc51ae11 JD |
215 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, |
216 | const char *buf, size_t count) | |
1da177e4 LT |
217 | { |
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); | |
9a61bf63 | 226 | mutex_lock(&data->update_lock); |
1da177e4 LT |
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); | |
9a61bf63 | 231 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
232 | return count; |
233 | } | |
234 | ||
bc51ae11 JD |
235 | static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy, |
236 | char *buf) | |
1da177e4 LT |
237 | { |
238 | struct lm63_data *data = lm63_update_device(dev); | |
239 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); | |
240 | } | |
241 | ||
bc51ae11 JD |
242 | static 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])); | |
1da177e4 | 248 | } |
bc51ae11 JD |
249 | |
250 | static 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 | ||
9a61bf63 | 257 | mutex_lock(&data->update_lock); |
bc51ae11 JD |
258 | data->temp8[1] = TEMP8_TO_REG(val); |
259 | i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]); | |
9a61bf63 | 260 | mutex_unlock(&data->update_lock); |
bc51ae11 | 261 | return count; |
1da177e4 | 262 | } |
bc51ae11 JD |
263 | |
264 | static 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])); | |
1da177e4 | 270 | } |
bc51ae11 JD |
271 | |
272 | static 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 | ||
9a61bf63 | 288 | mutex_lock(&data->update_lock); |
bc51ae11 JD |
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); | |
9a61bf63 | 294 | mutex_unlock(&data->update_lock); |
bc51ae11 | 295 | return count; |
1da177e4 | 296 | } |
1da177e4 LT |
297 | |
298 | /* Hysteresis register holds a relative value, while we want to present | |
299 | an absolute to user-space */ | |
bc51ae11 JD |
300 | static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, |
301 | char *buf) | |
1da177e4 LT |
302 | { |
303 | struct lm63_data *data = lm63_update_device(dev); | |
bc51ae11 | 304 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2]) |
1da177e4 LT |
305 | - 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 */ | |
bc51ae11 JD |
310 | static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, |
311 | const char *buf, size_t count) | |
1da177e4 LT |
312 | { |
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 | ||
9a61bf63 | 318 | mutex_lock(&data->update_lock); |
bc51ae11 | 319 | hyst = TEMP8_FROM_REG(data->temp8[2]) - val; |
1da177e4 LT |
320 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
321 | HYST_TO_REG(hyst)); | |
9a61bf63 | 322 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
323 | return count; |
324 | } | |
325 | ||
bc51ae11 JD |
326 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
327 | char *buf) | |
1da177e4 LT |
328 | { |
329 | struct lm63_data *data = lm63_update_device(dev); | |
330 | return sprintf(buf, "%u\n", data->alarms); | |
331 | } | |
332 | ||
2d45771e JD |
333 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, |
334 | char *buf) | |
335 | { | |
336 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
337 | struct lm63_data *data = lm63_update_device(dev); | |
338 | int bitnr = attr->index; | |
339 | ||
340 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
341 | } | |
342 | ||
bc51ae11 JD |
343 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
344 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, | |
345 | set_fan, 1); | |
1da177e4 LT |
346 | |
347 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); | |
348 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); | |
349 | ||
bc51ae11 JD |
350 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0); |
351 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, | |
352 | set_temp8, 1); | |
1da177e4 | 353 | |
bc51ae11 JD |
354 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
355 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | |
356 | set_temp11, 1); | |
357 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | |
358 | set_temp11, 2); | |
359 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2); | |
1da177e4 LT |
360 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
361 | set_temp2_crit_hyst); | |
362 | ||
2d45771e JD |
363 | /* Individual alarm files */ |
364 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); | |
365 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | |
366 | static SENSOR_DEVICE_ATTR(temp2_input_fault, S_IRUGO, show_alarm, NULL, 2); | |
367 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | |
368 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
369 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | |
370 | /* Raw alarm file for compatibility */ | |
1da177e4 LT |
371 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
372 | ||
373 | /* | |
374 | * Real code | |
375 | */ | |
376 | ||
377 | static int lm63_attach_adapter(struct i2c_adapter *adapter) | |
378 | { | |
379 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
380 | return 0; | |
2ed2dc3c | 381 | return i2c_probe(adapter, &addr_data, lm63_detect); |
1da177e4 LT |
382 | } |
383 | ||
384 | /* | |
385 | * The following function does more than just detection. If detection | |
386 | * succeeds, it also registers the new chip. | |
387 | */ | |
388 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind) | |
389 | { | |
390 | struct i2c_client *new_client; | |
391 | struct lm63_data *data; | |
392 | int err = 0; | |
393 | ||
394 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
395 | goto exit; | |
396 | ||
ba9c2e8d | 397 | if (!(data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL))) { |
1da177e4 LT |
398 | err = -ENOMEM; |
399 | goto exit; | |
400 | } | |
1da177e4 LT |
401 | |
402 | /* The common I2C client data is placed right before the | |
403 | LM63-specific data. */ | |
404 | new_client = &data->client; | |
405 | i2c_set_clientdata(new_client, data); | |
406 | new_client->addr = address; | |
407 | new_client->adapter = adapter; | |
408 | new_client->driver = &lm63_driver; | |
409 | new_client->flags = 0; | |
410 | ||
411 | /* Default to an LM63 if forced */ | |
412 | if (kind == 0) | |
413 | kind = lm63; | |
414 | ||
415 | if (kind < 0) { /* must identify */ | |
416 | u8 man_id, chip_id, reg_config1, reg_config2; | |
417 | u8 reg_alert_status, reg_alert_mask; | |
418 | ||
419 | man_id = i2c_smbus_read_byte_data(new_client, | |
420 | LM63_REG_MAN_ID); | |
421 | chip_id = i2c_smbus_read_byte_data(new_client, | |
422 | LM63_REG_CHIP_ID); | |
423 | reg_config1 = i2c_smbus_read_byte_data(new_client, | |
424 | LM63_REG_CONFIG1); | |
425 | reg_config2 = i2c_smbus_read_byte_data(new_client, | |
426 | LM63_REG_CONFIG2); | |
427 | reg_alert_status = i2c_smbus_read_byte_data(new_client, | |
428 | LM63_REG_ALERT_STATUS); | |
429 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, | |
430 | LM63_REG_ALERT_MASK); | |
431 | ||
432 | if (man_id == 0x01 /* National Semiconductor */ | |
433 | && chip_id == 0x41 /* LM63 */ | |
434 | && (reg_config1 & 0x18) == 0x00 | |
435 | && (reg_config2 & 0xF8) == 0x00 | |
436 | && (reg_alert_status & 0x20) == 0x00 | |
437 | && (reg_alert_mask & 0xA4) == 0xA4) { | |
438 | kind = lm63; | |
439 | } else { /* failed */ | |
440 | dev_dbg(&adapter->dev, "Unsupported chip " | |
441 | "(man_id=0x%02X, chip_id=0x%02X).\n", | |
442 | man_id, chip_id); | |
443 | goto exit_free; | |
444 | } | |
445 | } | |
446 | ||
447 | strlcpy(new_client->name, "lm63", I2C_NAME_SIZE); | |
448 | data->valid = 0; | |
9a61bf63 | 449 | mutex_init(&data->update_lock); |
1da177e4 LT |
450 | |
451 | /* Tell the I2C layer a new client has arrived */ | |
452 | if ((err = i2c_attach_client(new_client))) | |
453 | goto exit_free; | |
454 | ||
455 | /* Initialize the LM63 chip */ | |
456 | lm63_init_client(new_client); | |
457 | ||
458 | /* Register sysfs hooks */ | |
943b0830 MH |
459 | data->class_dev = hwmon_device_register(&new_client->dev); |
460 | if (IS_ERR(data->class_dev)) { | |
461 | err = PTR_ERR(data->class_dev); | |
462 | goto exit_detach; | |
463 | } | |
464 | ||
1da177e4 | 465 | if (data->config & 0x04) { /* tachometer enabled */ |
bc51ae11 JD |
466 | device_create_file(&new_client->dev, |
467 | &sensor_dev_attr_fan1_input.dev_attr); | |
468 | device_create_file(&new_client->dev, | |
469 | &sensor_dev_attr_fan1_min.dev_attr); | |
2d45771e JD |
470 | device_create_file(&new_client->dev, |
471 | &sensor_dev_attr_fan1_min_alarm.dev_attr); | |
1da177e4 LT |
472 | } |
473 | device_create_file(&new_client->dev, &dev_attr_pwm1); | |
474 | device_create_file(&new_client->dev, &dev_attr_pwm1_enable); | |
bc51ae11 JD |
475 | device_create_file(&new_client->dev, |
476 | &sensor_dev_attr_temp1_input.dev_attr); | |
477 | device_create_file(&new_client->dev, | |
478 | &sensor_dev_attr_temp2_input.dev_attr); | |
479 | device_create_file(&new_client->dev, | |
480 | &sensor_dev_attr_temp2_min.dev_attr); | |
481 | device_create_file(&new_client->dev, | |
482 | &sensor_dev_attr_temp1_max.dev_attr); | |
483 | device_create_file(&new_client->dev, | |
484 | &sensor_dev_attr_temp2_max.dev_attr); | |
485 | device_create_file(&new_client->dev, | |
486 | &sensor_dev_attr_temp2_crit.dev_attr); | |
1da177e4 | 487 | device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst); |
2d45771e JD |
488 | |
489 | device_create_file(&new_client->dev, | |
490 | &sensor_dev_attr_temp2_input_fault.dev_attr); | |
491 | device_create_file(&new_client->dev, | |
492 | &sensor_dev_attr_temp2_min_alarm.dev_attr); | |
493 | device_create_file(&new_client->dev, | |
494 | &sensor_dev_attr_temp1_max_alarm.dev_attr); | |
495 | device_create_file(&new_client->dev, | |
496 | &sensor_dev_attr_temp2_max_alarm.dev_attr); | |
497 | device_create_file(&new_client->dev, | |
498 | &sensor_dev_attr_temp2_crit_alarm.dev_attr); | |
1da177e4 LT |
499 | device_create_file(&new_client->dev, &dev_attr_alarms); |
500 | ||
501 | return 0; | |
502 | ||
943b0830 MH |
503 | exit_detach: |
504 | i2c_detach_client(new_client); | |
1da177e4 LT |
505 | exit_free: |
506 | kfree(data); | |
507 | exit: | |
508 | return err; | |
509 | } | |
510 | ||
511 | /* Idealy we shouldn't have to initialize anything, since the BIOS | |
512 | should have taken care of everything */ | |
513 | static void lm63_init_client(struct i2c_client *client) | |
514 | { | |
515 | struct lm63_data *data = i2c_get_clientdata(client); | |
516 | ||
517 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); | |
518 | data->config_fan = i2c_smbus_read_byte_data(client, | |
519 | LM63_REG_CONFIG_FAN); | |
520 | ||
521 | /* Start converting if needed */ | |
522 | if (data->config & 0x40) { /* standby */ | |
523 | dev_dbg(&client->dev, "Switching to operational mode"); | |
524 | data->config &= 0xA7; | |
525 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, | |
526 | data->config); | |
527 | } | |
528 | ||
529 | /* We may need pwm1_freq before ever updating the client data */ | |
530 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); | |
531 | if (data->pwm1_freq == 0) | |
532 | data->pwm1_freq = 1; | |
533 | ||
534 | /* Show some debug info about the LM63 configuration */ | |
535 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", | |
536 | (data->config & 0x04) ? "tachometer input" : | |
537 | "alert output"); | |
538 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", | |
539 | (data->config_fan & 0x08) ? "1.4" : "360", | |
540 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); | |
541 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", | |
542 | (data->config_fan & 0x10) ? "low" : "high", | |
543 | (data->config_fan & 0x20) ? "manual" : "auto"); | |
544 | } | |
545 | ||
546 | static int lm63_detach_client(struct i2c_client *client) | |
547 | { | |
943b0830 | 548 | struct lm63_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
549 | int err; |
550 | ||
943b0830 MH |
551 | hwmon_device_unregister(data->class_dev); |
552 | ||
7bef5594 | 553 | if ((err = i2c_detach_client(client))) |
1da177e4 | 554 | return err; |
1da177e4 | 555 | |
943b0830 | 556 | kfree(data); |
1da177e4 LT |
557 | return 0; |
558 | } | |
559 | ||
560 | static struct lm63_data *lm63_update_device(struct device *dev) | |
561 | { | |
562 | struct i2c_client *client = to_i2c_client(dev); | |
563 | struct lm63_data *data = i2c_get_clientdata(client); | |
564 | ||
9a61bf63 | 565 | mutex_lock(&data->update_lock); |
1da177e4 LT |
566 | |
567 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | |
568 | if (data->config & 0x04) { /* tachometer enabled */ | |
569 | /* order matters for fan1_input */ | |
bc51ae11 JD |
570 | data->fan[0] = i2c_smbus_read_byte_data(client, |
571 | LM63_REG_TACH_COUNT_LSB) & 0xFC; | |
572 | data->fan[0] |= i2c_smbus_read_byte_data(client, | |
573 | LM63_REG_TACH_COUNT_MSB) << 8; | |
574 | data->fan[1] = (i2c_smbus_read_byte_data(client, | |
575 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) | |
576 | | (i2c_smbus_read_byte_data(client, | |
577 | LM63_REG_TACH_LIMIT_MSB) << 8); | |
1da177e4 LT |
578 | } |
579 | ||
580 | data->pwm1_freq = i2c_smbus_read_byte_data(client, | |
581 | LM63_REG_PWM_FREQ); | |
582 | if (data->pwm1_freq == 0) | |
583 | data->pwm1_freq = 1; | |
584 | data->pwm1_value = i2c_smbus_read_byte_data(client, | |
585 | LM63_REG_PWM_VALUE); | |
586 | ||
bc51ae11 JD |
587 | data->temp8[0] = i2c_smbus_read_byte_data(client, |
588 | LM63_REG_LOCAL_TEMP); | |
589 | data->temp8[1] = i2c_smbus_read_byte_data(client, | |
590 | LM63_REG_LOCAL_HIGH); | |
1da177e4 LT |
591 | |
592 | /* order matters for temp2_input */ | |
bc51ae11 JD |
593 | data->temp11[0] = i2c_smbus_read_byte_data(client, |
594 | LM63_REG_REMOTE_TEMP_MSB) << 8; | |
595 | data->temp11[0] |= i2c_smbus_read_byte_data(client, | |
596 | LM63_REG_REMOTE_TEMP_LSB); | |
597 | data->temp11[1] = (i2c_smbus_read_byte_data(client, | |
1da177e4 LT |
598 | LM63_REG_REMOTE_LOW_MSB) << 8) |
599 | | i2c_smbus_read_byte_data(client, | |
600 | LM63_REG_REMOTE_LOW_LSB); | |
bc51ae11 JD |
601 | data->temp11[2] = (i2c_smbus_read_byte_data(client, |
602 | LM63_REG_REMOTE_HIGH_MSB) << 8) | |
603 | | i2c_smbus_read_byte_data(client, | |
604 | LM63_REG_REMOTE_HIGH_LSB); | |
605 | data->temp8[2] = i2c_smbus_read_byte_data(client, | |
606 | LM63_REG_REMOTE_TCRIT); | |
1da177e4 LT |
607 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, |
608 | LM63_REG_REMOTE_TCRIT_HYST); | |
609 | ||
610 | data->alarms = i2c_smbus_read_byte_data(client, | |
611 | LM63_REG_ALERT_STATUS) & 0x7F; | |
612 | ||
613 | data->last_updated = jiffies; | |
614 | data->valid = 1; | |
615 | } | |
616 | ||
9a61bf63 | 617 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
618 | |
619 | return data; | |
620 | } | |
621 | ||
622 | static int __init sensors_lm63_init(void) | |
623 | { | |
624 | return i2c_add_driver(&lm63_driver); | |
625 | } | |
626 | ||
627 | static void __exit sensors_lm63_exit(void) | |
628 | { | |
629 | i2c_del_driver(&lm63_driver); | |
630 | } | |
631 | ||
632 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | |
633 | MODULE_DESCRIPTION("LM63 driver"); | |
634 | MODULE_LICENSE("GPL"); | |
635 | ||
636 | module_init(sensors_lm63_init); | |
637 | module_exit(sensors_lm63_exit); |