Linux Audio

Check our new training course

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