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