]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/hwmon/lm63.c
910599ac31ca50adf06c93c4b390a220e9be1867
[mirror_ubuntu-jammy-kernel.git] / drivers / hwmon / lm63.c
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 #include <linux/types.h>
51
52 /*
53 * Addresses to scan
54 * Address is fully defined internally and cannot be changed except for
55 * LM64 which has one pin dedicated to address selection.
56 * LM63 and LM96163 have address 0x4c.
57 * LM64 can have address 0x18 or 0x4e.
58 */
59
60 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
61
62 /*
63 * The LM63 registers
64 */
65
66 #define LM63_REG_CONFIG1 0x03
67 #define LM63_REG_CONVRATE 0x04
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 #define LM63_REG_LUT_TEMP_HYST 0x4F
79 #define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
80 #define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
81
82 #define LM63_REG_LOCAL_TEMP 0x00
83 #define LM63_REG_LOCAL_HIGH 0x05
84
85 #define LM63_REG_REMOTE_TEMP_MSB 0x01
86 #define LM63_REG_REMOTE_TEMP_LSB 0x10
87 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
88 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
89 #define LM63_REG_REMOTE_HIGH_MSB 0x07
90 #define LM63_REG_REMOTE_HIGH_LSB 0x13
91 #define LM63_REG_REMOTE_LOW_MSB 0x08
92 #define LM63_REG_REMOTE_LOW_LSB 0x14
93 #define LM63_REG_REMOTE_TCRIT 0x19
94 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
95
96 #define LM63_REG_ALERT_STATUS 0x02
97 #define LM63_REG_ALERT_MASK 0x16
98
99 #define LM63_REG_MAN_ID 0xFE
100 #define LM63_REG_CHIP_ID 0xFF
101
102 #define LM96163_REG_TRUTHERM 0x30
103 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
104 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
105 #define LM96163_REG_CONFIG_ENHANCED 0x45
106
107 #define LM63_MAX_CONVRATE 9
108
109 #define LM63_MAX_CONVRATE_HZ 32
110 #define LM96163_MAX_CONVRATE_HZ 26
111
112 /*
113 * Conversions and various macros
114 * For tachometer counts, the LM63 uses 16-bit values.
115 * For local temperature and high limit, remote critical limit and hysteresis
116 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
117 * For remote temperature, low and high limits, it uses signed 11-bit values
118 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
119 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
120 * than the register reading. Remote temperature setpoints have to be
121 * adapted accordingly.
122 */
123
124 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
125 5400000 / (reg))
126 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
127 (5400000 / (val)) & 0xFFFC)
128 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
129 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
130 (val) >= 127000 ? 127 : \
131 (val) < 0 ? ((val) - 500) / 1000 : \
132 ((val) + 500) / 1000)
133 #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
134 (val) >= 255000 ? 255 : \
135 ((val) + 500) / 1000)
136 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
137 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
138 (val) >= 127875 ? 0x7FE0 : \
139 (val) < 0 ? ((val) - 62) / 125 * 32 : \
140 ((val) + 62) / 125 * 32)
141 #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 255875 ? 0xFFE0 : \
143 ((val) + 62) / 125 * 32)
144 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
145 (val) >= 127000 ? 127 : \
146 ((val) + 500) / 1000)
147
148 #define UPDATE_INTERVAL(max, rate) \
149 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
150
151 enum chips { lm63, lm64, lm96163 };
152
153 /*
154 * Client data (each client gets its own)
155 */
156
157 struct lm63_data {
158 struct device *hwmon_dev;
159 struct mutex update_lock;
160 char valid; /* zero until following fields are valid */
161 char lut_valid; /* zero until lut fields are valid */
162 unsigned long last_updated; /* in jiffies */
163 unsigned long lut_last_updated; /* in jiffies */
164 enum chips kind;
165 int temp2_offset;
166
167 int update_interval; /* in milliseconds */
168 int max_convrate_hz;
169 int lut_size; /* 8 or 12 */
170
171 /* registers values */
172 u8 config, config_fan;
173 u16 fan[2]; /* 0: input
174 1: low limit */
175 u8 pwm1_freq;
176 u8 pwm1[13]; /* 0: current output
177 1-12: lookup table */
178 s8 temp8[15]; /* 0: local input
179 1: local high limit
180 2: remote critical limit
181 3-14: lookup table */
182 s16 temp11[4]; /* 0: remote input
183 1: remote low limit
184 2: remote high limit
185 3: remote offset */
186 u16 temp11u; /* remote input (unsigned) */
187 u8 temp2_crit_hyst;
188 u8 lut_temp_hyst;
189 u8 alarms;
190 bool pwm_highres;
191 bool lut_temp_highres;
192 bool remote_unsigned; /* true if unsigned remote upper limits */
193 bool trutherm;
194 };
195
196 static inline int temp8_from_reg(struct lm63_data *data, int nr)
197 {
198 if (data->remote_unsigned)
199 return TEMP8_FROM_REG((u8)data->temp8[nr]);
200 return TEMP8_FROM_REG(data->temp8[nr]);
201 }
202
203 static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
204 {
205 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
206 }
207
208 /*
209 * Update the lookup table register cache.
210 * client->update_lock must be held when calling this function.
211 */
212 static void lm63_update_lut(struct i2c_client *client)
213 {
214 struct lm63_data *data = i2c_get_clientdata(client);
215 int i;
216
217 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
218 !data->lut_valid) {
219 for (i = 0; i < data->lut_size; i++) {
220 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
221 LM63_REG_LUT_PWM(i));
222 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
223 LM63_REG_LUT_TEMP(i));
224 }
225 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
226 LM63_REG_LUT_TEMP_HYST);
227
228 data->lut_last_updated = jiffies;
229 data->lut_valid = 1;
230 }
231 }
232
233 static struct lm63_data *lm63_update_device(struct device *dev)
234 {
235 struct i2c_client *client = to_i2c_client(dev);
236 struct lm63_data *data = i2c_get_clientdata(client);
237 unsigned long next_update;
238
239 mutex_lock(&data->update_lock);
240
241 next_update = data->last_updated
242 + msecs_to_jiffies(data->update_interval) + 1;
243
244 if (time_after(jiffies, next_update) || !data->valid) {
245 if (data->config & 0x04) { /* tachometer enabled */
246 /* order matters for fan1_input */
247 data->fan[0] = i2c_smbus_read_byte_data(client,
248 LM63_REG_TACH_COUNT_LSB) & 0xFC;
249 data->fan[0] |= i2c_smbus_read_byte_data(client,
250 LM63_REG_TACH_COUNT_MSB) << 8;
251 data->fan[1] = (i2c_smbus_read_byte_data(client,
252 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
253 | (i2c_smbus_read_byte_data(client,
254 LM63_REG_TACH_LIMIT_MSB) << 8);
255 }
256
257 data->pwm1_freq = i2c_smbus_read_byte_data(client,
258 LM63_REG_PWM_FREQ);
259 if (data->pwm1_freq == 0)
260 data->pwm1_freq = 1;
261 data->pwm1[0] = i2c_smbus_read_byte_data(client,
262 LM63_REG_PWM_VALUE);
263
264 data->temp8[0] = i2c_smbus_read_byte_data(client,
265 LM63_REG_LOCAL_TEMP);
266 data->temp8[1] = i2c_smbus_read_byte_data(client,
267 LM63_REG_LOCAL_HIGH);
268
269 /* order matters for temp2_input */
270 data->temp11[0] = i2c_smbus_read_byte_data(client,
271 LM63_REG_REMOTE_TEMP_MSB) << 8;
272 data->temp11[0] |= i2c_smbus_read_byte_data(client,
273 LM63_REG_REMOTE_TEMP_LSB);
274 data->temp11[1] = (i2c_smbus_read_byte_data(client,
275 LM63_REG_REMOTE_LOW_MSB) << 8)
276 | i2c_smbus_read_byte_data(client,
277 LM63_REG_REMOTE_LOW_LSB);
278 data->temp11[2] = (i2c_smbus_read_byte_data(client,
279 LM63_REG_REMOTE_HIGH_MSB) << 8)
280 | i2c_smbus_read_byte_data(client,
281 LM63_REG_REMOTE_HIGH_LSB);
282 data->temp11[3] = (i2c_smbus_read_byte_data(client,
283 LM63_REG_REMOTE_OFFSET_MSB) << 8)
284 | i2c_smbus_read_byte_data(client,
285 LM63_REG_REMOTE_OFFSET_LSB);
286
287 if (data->kind == lm96163)
288 data->temp11u = (i2c_smbus_read_byte_data(client,
289 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
290 | i2c_smbus_read_byte_data(client,
291 LM96163_REG_REMOTE_TEMP_U_LSB);
292
293 data->temp8[2] = i2c_smbus_read_byte_data(client,
294 LM63_REG_REMOTE_TCRIT);
295 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
296 LM63_REG_REMOTE_TCRIT_HYST);
297
298 data->alarms = i2c_smbus_read_byte_data(client,
299 LM63_REG_ALERT_STATUS) & 0x7F;
300
301 data->last_updated = jiffies;
302 data->valid = 1;
303 }
304
305 lm63_update_lut(client);
306
307 mutex_unlock(&data->update_lock);
308
309 return data;
310 }
311
312 /*
313 * Trip points in the lookup table should be in ascending order for both
314 * temperatures and PWM output values.
315 */
316 static int lm63_lut_looks_bad(struct i2c_client *client)
317 {
318 struct lm63_data *data = i2c_get_clientdata(client);
319 int i;
320
321 mutex_lock(&data->update_lock);
322 lm63_update_lut(client);
323
324 for (i = 1; i < data->lut_size; i++) {
325 if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
326 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
327 dev_warn(&client->dev,
328 "Lookup table doesn't look sane (check entries %d and %d)\n",
329 i, i + 1);
330 break;
331 }
332 }
333 mutex_unlock(&data->update_lock);
334
335 return i == data->lut_size ? 0 : 1;
336 }
337
338 /*
339 * Sysfs callback functions and files
340 */
341
342 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
343 char *buf)
344 {
345 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
346 struct lm63_data *data = lm63_update_device(dev);
347 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
348 }
349
350 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
351 const char *buf, size_t count)
352 {
353 struct i2c_client *client = to_i2c_client(dev);
354 struct lm63_data *data = i2c_get_clientdata(client);
355 unsigned long val;
356 int err;
357
358 err = kstrtoul(buf, 10, &val);
359 if (err)
360 return err;
361
362 mutex_lock(&data->update_lock);
363 data->fan[1] = FAN_TO_REG(val);
364 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
365 data->fan[1] & 0xFF);
366 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
367 data->fan[1] >> 8);
368 mutex_unlock(&data->update_lock);
369 return count;
370 }
371
372 static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
373 char *buf)
374 {
375 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
376 struct lm63_data *data = lm63_update_device(dev);
377 int nr = attr->index;
378 int pwm;
379
380 if (data->pwm_highres)
381 pwm = data->pwm1[nr];
382 else
383 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
384 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
385 (2 * data->pwm1_freq);
386
387 return sprintf(buf, "%d\n", pwm);
388 }
389
390 static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
391 const char *buf, size_t count)
392 {
393 struct i2c_client *client = to_i2c_client(dev);
394 struct lm63_data *data = i2c_get_clientdata(client);
395 unsigned long val;
396 int err;
397
398 if (!(data->config_fan & 0x20)) /* register is read-only */
399 return -EPERM;
400
401 err = kstrtoul(buf, 10, &val);
402 if (err)
403 return err;
404
405 val = SENSORS_LIMIT(val, 0, 255);
406 mutex_lock(&data->update_lock);
407 data->pwm1[0] = data->pwm_highres ? val :
408 (val * data->pwm1_freq * 2 + 127) / 255;
409 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1[0]);
410 mutex_unlock(&data->update_lock);
411 return count;
412 }
413
414 static ssize_t show_pwm1_enable(struct device *dev,
415 struct device_attribute *dummy, char *buf)
416 {
417 struct lm63_data *data = lm63_update_device(dev);
418 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
419 }
420
421 static ssize_t set_pwm1_enable(struct device *dev,
422 struct device_attribute *dummy,
423 const char *buf, size_t count)
424 {
425 struct i2c_client *client = to_i2c_client(dev);
426 struct lm63_data *data = i2c_get_clientdata(client);
427 unsigned long val;
428 int err;
429
430 err = kstrtoul(buf, 10, &val);
431 if (err)
432 return err;
433 if (val < 1 || val > 2)
434 return -EINVAL;
435
436 /*
437 * Only let the user switch to automatic mode if the lookup table
438 * looks sane.
439 */
440 if (val == 2 && lm63_lut_looks_bad(client))
441 return -EPERM;
442
443 mutex_lock(&data->update_lock);
444 data->config_fan = i2c_smbus_read_byte_data(client,
445 LM63_REG_CONFIG_FAN);
446 if (val == 1)
447 data->config_fan |= 0x20;
448 else
449 data->config_fan &= ~0x20;
450 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
451 data->config_fan);
452 mutex_unlock(&data->update_lock);
453 return count;
454 }
455
456 /*
457 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
458 * For remote sensor registers temp2_offset has to be considered,
459 * for local sensor it must not.
460 * So we need separate 8bit accessors for local and remote sensor.
461 */
462 static ssize_t show_local_temp8(struct device *dev,
463 struct device_attribute *devattr,
464 char *buf)
465 {
466 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
467 struct lm63_data *data = lm63_update_device(dev);
468 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
469 }
470
471 static ssize_t show_remote_temp8(struct device *dev,
472 struct device_attribute *devattr,
473 char *buf)
474 {
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
476 struct lm63_data *data = lm63_update_device(dev);
477 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
478 + data->temp2_offset);
479 }
480
481 static ssize_t show_lut_temp(struct device *dev,
482 struct device_attribute *devattr,
483 char *buf)
484 {
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 struct lm63_data *data = lm63_update_device(dev);
487 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
488 + data->temp2_offset);
489 }
490
491 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
492 const char *buf, size_t count)
493 {
494 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
495 struct i2c_client *client = to_i2c_client(dev);
496 struct lm63_data *data = i2c_get_clientdata(client);
497 int nr = attr->index;
498 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
499 long val;
500 int err;
501 int temp;
502
503 err = kstrtol(buf, 10, &val);
504 if (err)
505 return err;
506
507 mutex_lock(&data->update_lock);
508 if (nr == 2) {
509 if (data->remote_unsigned)
510 temp = TEMP8U_TO_REG(val - data->temp2_offset);
511 else
512 temp = TEMP8_TO_REG(val - data->temp2_offset);
513 } else {
514 temp = TEMP8_TO_REG(val);
515 }
516 data->temp8[nr] = temp;
517 i2c_smbus_write_byte_data(client, reg, temp);
518 mutex_unlock(&data->update_lock);
519 return count;
520 }
521
522 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
523 char *buf)
524 {
525 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
526 struct lm63_data *data = lm63_update_device(dev);
527 int nr = attr->index;
528 int temp;
529
530 if (!nr) {
531 /*
532 * Use unsigned temperature unless its value is zero.
533 * If it is zero, use signed temperature.
534 */
535 if (data->temp11u)
536 temp = TEMP11_FROM_REG(data->temp11u);
537 else
538 temp = TEMP11_FROM_REG(data->temp11[nr]);
539 } else {
540 if (data->remote_unsigned && nr == 2)
541 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
542 else
543 temp = TEMP11_FROM_REG(data->temp11[nr]);
544 }
545 return sprintf(buf, "%d\n", temp + data->temp2_offset);
546 }
547
548 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
549 const char *buf, size_t count)
550 {
551 static const u8 reg[6] = {
552 LM63_REG_REMOTE_LOW_MSB,
553 LM63_REG_REMOTE_LOW_LSB,
554 LM63_REG_REMOTE_HIGH_MSB,
555 LM63_REG_REMOTE_HIGH_LSB,
556 LM63_REG_REMOTE_OFFSET_MSB,
557 LM63_REG_REMOTE_OFFSET_LSB,
558 };
559
560 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
561 struct i2c_client *client = to_i2c_client(dev);
562 struct lm63_data *data = i2c_get_clientdata(client);
563 long val;
564 int err;
565 int nr = attr->index;
566
567 err = kstrtol(buf, 10, &val);
568 if (err)
569 return err;
570
571 mutex_lock(&data->update_lock);
572 if (data->remote_unsigned && nr == 2)
573 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
574 else
575 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
576
577 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
578 data->temp11[nr] >> 8);
579 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
580 data->temp11[nr] & 0xff);
581 mutex_unlock(&data->update_lock);
582 return count;
583 }
584
585 /*
586 * Hysteresis register holds a relative value, while we want to present
587 * an absolute to user-space
588 */
589 static ssize_t show_temp2_crit_hyst(struct device *dev,
590 struct device_attribute *dummy, char *buf)
591 {
592 struct lm63_data *data = lm63_update_device(dev);
593 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
594 + data->temp2_offset
595 - TEMP8_FROM_REG(data->temp2_crit_hyst));
596 }
597
598 static ssize_t show_lut_temp_hyst(struct device *dev,
599 struct device_attribute *devattr, char *buf)
600 {
601 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
602 struct lm63_data *data = lm63_update_device(dev);
603
604 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
605 + data->temp2_offset
606 - TEMP8_FROM_REG(data->lut_temp_hyst));
607 }
608
609 /*
610 * And now the other way around, user-space provides an absolute
611 * hysteresis value and we have to store a relative one
612 */
613 static ssize_t set_temp2_crit_hyst(struct device *dev,
614 struct device_attribute *dummy,
615 const char *buf, size_t count)
616 {
617 struct i2c_client *client = to_i2c_client(dev);
618 struct lm63_data *data = i2c_get_clientdata(client);
619 long val;
620 int err;
621 long hyst;
622
623 err = kstrtol(buf, 10, &val);
624 if (err)
625 return err;
626
627 mutex_lock(&data->update_lock);
628 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
629 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
630 HYST_TO_REG(hyst));
631 mutex_unlock(&data->update_lock);
632 return count;
633 }
634
635 /*
636 * Set conversion rate.
637 * client->update_lock must be held when calling this function.
638 */
639 static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
640 unsigned int interval)
641 {
642 int i;
643 unsigned int update_interval;
644
645 /* Shift calculations to avoid rounding errors */
646 interval <<= 6;
647
648 /* find the nearest update rate */
649 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
650 / data->max_convrate_hz;
651 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
652 if (interval >= update_interval * 3 / 4)
653 break;
654
655 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
656 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
657 }
658
659 static ssize_t show_update_interval(struct device *dev,
660 struct device_attribute *attr, char *buf)
661 {
662 struct lm63_data *data = dev_get_drvdata(dev);
663
664 return sprintf(buf, "%u\n", data->update_interval);
665 }
666
667 static ssize_t set_update_interval(struct device *dev,
668 struct device_attribute *attr,
669 const char *buf, size_t count)
670 {
671 struct i2c_client *client = to_i2c_client(dev);
672 struct lm63_data *data = i2c_get_clientdata(client);
673 unsigned long val;
674 int err;
675
676 err = kstrtoul(buf, 10, &val);
677 if (err)
678 return err;
679
680 mutex_lock(&data->update_lock);
681 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
682 mutex_unlock(&data->update_lock);
683
684 return count;
685 }
686
687 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
688 char *buf)
689 {
690 struct i2c_client *client = to_i2c_client(dev);
691 struct lm63_data *data = i2c_get_clientdata(client);
692
693 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
694 }
695
696 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
698 {
699 struct i2c_client *client = to_i2c_client(dev);
700 struct lm63_data *data = i2c_get_clientdata(client);
701 unsigned long val;
702 int ret;
703 u8 reg;
704
705 ret = kstrtoul(buf, 10, &val);
706 if (ret < 0)
707 return ret;
708 if (val != 1 && val != 2)
709 return -EINVAL;
710
711 mutex_lock(&data->update_lock);
712 data->trutherm = val == 1;
713 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
714 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
715 reg | (data->trutherm ? 0x02 : 0x00));
716 data->valid = 0;
717 mutex_unlock(&data->update_lock);
718
719 return count;
720 }
721
722 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
723 char *buf)
724 {
725 struct lm63_data *data = lm63_update_device(dev);
726 return sprintf(buf, "%u\n", data->alarms);
727 }
728
729 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
730 char *buf)
731 {
732 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
733 struct lm63_data *data = lm63_update_device(dev);
734 int bitnr = attr->index;
735
736 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
737 }
738
739 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
740 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
741 set_fan, 1);
742
743 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
744 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
745 show_pwm1_enable, set_pwm1_enable);
746 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO, show_pwm1, NULL, 1);
747 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IRUGO,
748 show_lut_temp, NULL, 3);
749 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
750 show_lut_temp_hyst, NULL, 3);
751 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO, show_pwm1, NULL, 2);
752 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO,
753 show_lut_temp, NULL, 4);
754 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
755 show_lut_temp_hyst, NULL, 4);
756 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO, show_pwm1, NULL, 3);
757 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IRUGO,
758 show_lut_temp, NULL, 5);
759 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
760 show_lut_temp_hyst, NULL, 5);
761 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO, show_pwm1, NULL, 4);
762 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IRUGO,
763 show_lut_temp, NULL, 6);
764 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
765 show_lut_temp_hyst, NULL, 6);
766 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm1, NULL, 5);
767 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IRUGO,
768 show_lut_temp, NULL, 7);
769 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
770 show_lut_temp_hyst, NULL, 7);
771 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IRUGO, show_pwm1, NULL, 6);
772 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IRUGO,
773 show_lut_temp, NULL, 8);
774 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
775 show_lut_temp_hyst, NULL, 8);
776 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IRUGO, show_pwm1, NULL, 7);
777 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IRUGO,
778 show_lut_temp, NULL, 9);
779 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
780 show_lut_temp_hyst, NULL, 9);
781 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IRUGO, show_pwm1, NULL, 8);
782 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IRUGO,
783 show_lut_temp, NULL, 10);
784 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
785 show_lut_temp_hyst, NULL, 10);
786 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IRUGO, show_pwm1, NULL, 9);
787 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IRUGO,
788 show_lut_temp, NULL, 11);
789 static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
790 show_lut_temp_hyst, NULL, 11);
791 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IRUGO, show_pwm1, NULL, 10);
792 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IRUGO,
793 show_lut_temp, NULL, 12);
794 static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
795 show_lut_temp_hyst, NULL, 12);
796 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IRUGO, show_pwm1, NULL, 11);
797 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IRUGO,
798 show_lut_temp, NULL, 13);
799 static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
800 show_lut_temp_hyst, NULL, 13);
801 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IRUGO, show_pwm1, NULL, 12);
802 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IRUGO,
803 show_lut_temp, NULL, 14);
804 static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
805 show_lut_temp_hyst, NULL, 14);
806
807 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
808 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
809 set_temp8, 1);
810
811 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
812 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
813 set_temp11, 1);
814 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
815 set_temp11, 2);
816 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
817 set_temp11, 3);
818 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
819 set_temp8, 2);
820 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
821 set_temp2_crit_hyst);
822
823 static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
824
825 /* Individual alarm files */
826 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
827 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
828 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
829 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
830 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
831 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
832 /* Raw alarm file for compatibility */
833 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
834
835 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
836 set_update_interval);
837
838 static struct attribute *lm63_attributes[] = {
839 &sensor_dev_attr_pwm1.dev_attr.attr,
840 &dev_attr_pwm1_enable.attr,
841 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
842 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
843 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
844 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
845 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
846 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
847 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
848 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
849 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
850 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
851 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
852 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
853 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
854 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
855 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
856 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
857 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
858 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
859 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
860 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
861 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
862 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
863 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
864 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
865
866 &sensor_dev_attr_temp1_input.dev_attr.attr,
867 &sensor_dev_attr_temp2_input.dev_attr.attr,
868 &sensor_dev_attr_temp2_min.dev_attr.attr,
869 &sensor_dev_attr_temp1_max.dev_attr.attr,
870 &sensor_dev_attr_temp2_max.dev_attr.attr,
871 &sensor_dev_attr_temp2_offset.dev_attr.attr,
872 &sensor_dev_attr_temp2_crit.dev_attr.attr,
873 &dev_attr_temp2_crit_hyst.attr,
874
875 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
876 &sensor_dev_attr_temp2_fault.dev_attr.attr,
877 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
878 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
879 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
880 &dev_attr_alarms.attr,
881 &dev_attr_update_interval.attr,
882 NULL
883 };
884
885 static struct attribute *lm63_attributes_extra_lut[] = {
886 &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
887 &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
888 &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
889 &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
890 &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
891 &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
892 &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
893 &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
894 &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
895 &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
896 &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
897 &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
898 NULL
899 };
900
901 static const struct attribute_group lm63_group_extra_lut = {
902 .attrs = lm63_attributes_extra_lut,
903 };
904
905 /*
906 * On LM63, temp2_crit can be set only once, which should be job
907 * of the bootloader.
908 * On LM64, temp2_crit can always be set.
909 * On LM96163, temp2_crit can be set if bit 1 of the configuration
910 * register is true.
911 */
912 static umode_t lm63_attribute_mode(struct kobject *kobj,
913 struct attribute *attr, int index)
914 {
915 struct device *dev = container_of(kobj, struct device, kobj);
916 struct i2c_client *client = to_i2c_client(dev);
917 struct lm63_data *data = i2c_get_clientdata(client);
918
919 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
920 && (data->kind == lm64 ||
921 (data->kind == lm96163 && (data->config & 0x02))))
922 return attr->mode | S_IWUSR;
923
924 return attr->mode;
925 }
926
927 static const struct attribute_group lm63_group = {
928 .is_visible = lm63_attribute_mode,
929 .attrs = lm63_attributes,
930 };
931
932 static struct attribute *lm63_attributes_fan1[] = {
933 &sensor_dev_attr_fan1_input.dev_attr.attr,
934 &sensor_dev_attr_fan1_min.dev_attr.attr,
935
936 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
937 NULL
938 };
939
940 static const struct attribute_group lm63_group_fan1 = {
941 .attrs = lm63_attributes_fan1,
942 };
943
944 /*
945 * Real code
946 */
947
948 /* Return 0 if detection is successful, -ENODEV otherwise */
949 static int lm63_detect(struct i2c_client *client,
950 struct i2c_board_info *info)
951 {
952 struct i2c_adapter *adapter = client->adapter;
953 u8 man_id, chip_id, reg_config1, reg_config2;
954 u8 reg_alert_status, reg_alert_mask;
955 int address = client->addr;
956
957 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
958 return -ENODEV;
959
960 man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
961 chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
962
963 reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
964 reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
965 reg_alert_status = i2c_smbus_read_byte_data(client,
966 LM63_REG_ALERT_STATUS);
967 reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
968
969 if (man_id != 0x01 /* National Semiconductor */
970 || (reg_config1 & 0x18) != 0x00
971 || (reg_config2 & 0xF8) != 0x00
972 || (reg_alert_status & 0x20) != 0x00
973 || (reg_alert_mask & 0xA4) != 0xA4) {
974 dev_dbg(&adapter->dev,
975 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
976 man_id, chip_id);
977 return -ENODEV;
978 }
979
980 if (chip_id == 0x41 && address == 0x4c)
981 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
982 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
983 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
984 else if (chip_id == 0x49 && address == 0x4c)
985 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
986 else
987 return -ENODEV;
988
989 return 0;
990 }
991
992 /*
993 * Ideally we shouldn't have to initialize anything, since the BIOS
994 * should have taken care of everything
995 */
996 static void lm63_init_client(struct i2c_client *client)
997 {
998 struct lm63_data *data = i2c_get_clientdata(client);
999 u8 convrate;
1000
1001 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
1002 data->config_fan = i2c_smbus_read_byte_data(client,
1003 LM63_REG_CONFIG_FAN);
1004
1005 /* Start converting if needed */
1006 if (data->config & 0x40) { /* standby */
1007 dev_dbg(&client->dev, "Switching to operational mode\n");
1008 data->config &= 0xA7;
1009 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
1010 data->config);
1011 }
1012 /* Tachometer is always enabled on LM64 */
1013 if (data->kind == lm64)
1014 data->config |= 0x04;
1015
1016 /* We may need pwm1_freq before ever updating the client data */
1017 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
1018 if (data->pwm1_freq == 0)
1019 data->pwm1_freq = 1;
1020
1021 switch (data->kind) {
1022 case lm63:
1023 case lm64:
1024 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
1025 data->lut_size = 8;
1026 break;
1027 case lm96163:
1028 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
1029 data->lut_size = 12;
1030 data->trutherm
1031 = i2c_smbus_read_byte_data(client,
1032 LM96163_REG_TRUTHERM) & 0x02;
1033 break;
1034 }
1035 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
1036 if (unlikely(convrate > LM63_MAX_CONVRATE))
1037 convrate = LM63_MAX_CONVRATE;
1038 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
1039 convrate);
1040
1041 /*
1042 * For LM96163, check if high resolution PWM
1043 * and unsigned temperature format is enabled.
1044 */
1045 if (data->kind == lm96163) {
1046 u8 config_enhanced
1047 = i2c_smbus_read_byte_data(client,
1048 LM96163_REG_CONFIG_ENHANCED);
1049 if (config_enhanced & 0x20)
1050 data->lut_temp_highres = true;
1051 if ((config_enhanced & 0x10)
1052 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
1053 data->pwm_highres = true;
1054 if (config_enhanced & 0x08)
1055 data->remote_unsigned = true;
1056 }
1057
1058 /* Show some debug info about the LM63 configuration */
1059 if (data->kind == lm63)
1060 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
1061 (data->config & 0x04) ? "tachometer input" :
1062 "alert output");
1063 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
1064 (data->config_fan & 0x08) ? "1.4" : "360",
1065 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
1066 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
1067 (data->config_fan & 0x10) ? "low" : "high",
1068 (data->config_fan & 0x20) ? "manual" : "auto");
1069 }
1070
1071 static int lm63_probe(struct i2c_client *client,
1072 const struct i2c_device_id *id)
1073 {
1074 struct lm63_data *data;
1075 int err;
1076
1077 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
1078 if (!data) {
1079 err = -ENOMEM;
1080 goto exit;
1081 }
1082
1083 i2c_set_clientdata(client, data);
1084 data->valid = 0;
1085 mutex_init(&data->update_lock);
1086
1087 /* Set the device type */
1088 data->kind = id->driver_data;
1089 if (data->kind == lm64)
1090 data->temp2_offset = 16000;
1091
1092 /* Initialize chip */
1093 lm63_init_client(client);
1094
1095 /* Register sysfs hooks */
1096 err = sysfs_create_group(&client->dev.kobj, &lm63_group);
1097 if (err)
1098 goto exit_free;
1099 if (data->config & 0x04) { /* tachometer enabled */
1100 err = sysfs_create_group(&client->dev.kobj, &lm63_group_fan1);
1101 if (err)
1102 goto exit_remove_files;
1103 }
1104 if (data->kind == lm96163) {
1105 err = device_create_file(&client->dev, &dev_attr_temp2_type);
1106 if (err)
1107 goto exit_remove_files;
1108
1109 err = sysfs_create_group(&client->dev.kobj,
1110 &lm63_group_extra_lut);
1111 if (err)
1112 goto exit_remove_files;
1113 }
1114
1115 data->hwmon_dev = hwmon_device_register(&client->dev);
1116 if (IS_ERR(data->hwmon_dev)) {
1117 err = PTR_ERR(data->hwmon_dev);
1118 goto exit_remove_files;
1119 }
1120
1121 return 0;
1122
1123 exit_remove_files:
1124 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1125 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1126 if (data->kind == lm96163) {
1127 device_remove_file(&client->dev, &dev_attr_temp2_type);
1128 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1129 }
1130 exit_free:
1131 kfree(data);
1132 exit:
1133 return err;
1134 }
1135
1136 static int lm63_remove(struct i2c_client *client)
1137 {
1138 struct lm63_data *data = i2c_get_clientdata(client);
1139
1140 hwmon_device_unregister(data->hwmon_dev);
1141 sysfs_remove_group(&client->dev.kobj, &lm63_group);
1142 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
1143 if (data->kind == lm96163) {
1144 device_remove_file(&client->dev, &dev_attr_temp2_type);
1145 sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut);
1146 }
1147
1148 kfree(data);
1149 return 0;
1150 }
1151
1152 /*
1153 * Driver data (common to all clients)
1154 */
1155
1156 static const struct i2c_device_id lm63_id[] = {
1157 { "lm63", lm63 },
1158 { "lm64", lm64 },
1159 { "lm96163", lm96163 },
1160 { }
1161 };
1162 MODULE_DEVICE_TABLE(i2c, lm63_id);
1163
1164 static struct i2c_driver lm63_driver = {
1165 .class = I2C_CLASS_HWMON,
1166 .driver = {
1167 .name = "lm63",
1168 },
1169 .probe = lm63_probe,
1170 .remove = lm63_remove,
1171 .id_table = lm63_id,
1172 .detect = lm63_detect,
1173 .address_list = normal_i2c,
1174 };
1175
1176 module_i2c_driver(lm63_driver);
1177
1178 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1179 MODULE_DESCRIPTION("LM63 driver");
1180 MODULE_LICENSE("GPL");