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