Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  3 *          monitoring
  4 * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
  5 *
  6 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
  7 * a sensor chip made by National Semiconductor. It reports up to four
  8 * temperatures (its own plus up to three external ones) with a 1 deg
  9 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
 10 * from National's website at:
 11 *   http://www.national.com/pf/LM/LM83.html
 12 * Since the datasheet omits to give the chip stepping code, I give it
 13 * here: 0x03 (at register 0xff).
 14 *
 15 * Also supports the LM82 temp sensor, which is basically a stripped down
 16 * model of the LM83.  Datasheet is here:
 17 * http://www.national.com/pf/LM/LM82.html
 18 *
 19 * This program is free software; you can redistribute it and/or modify
 20 * it under the terms of the GNU General Public License as published by
 21 * the Free Software Foundation; either version 2 of the License, or
 22 * (at your option) any later version.
 23 *
 24 * This program is distributed in the hope that it will be useful,
 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 27 * GNU General Public License for more details.
 28 *
 29 * You should have received a copy of the GNU General Public License
 30 * along with this program; if not, write to the Free Software
 31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 32 */
 33
 34#include <linux/module.h>
 35#include <linux/init.h>
 36#include <linux/slab.h>
 37#include <linux/jiffies.h>
 38#include <linux/i2c.h>
 39#include <linux/hwmon-sysfs.h>
 40#include <linux/hwmon.h>
 41#include <linux/err.h>
 42#include <linux/mutex.h>
 43#include <linux/sysfs.h>
 44
 45/*
 46 * Addresses to scan
 47 * Address is selected using 2 three-level pins, resulting in 9 possible
 48 * addresses.
 49 */
 50
 51static const unsigned short normal_i2c[] = {
 52	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
 53
 54enum chips { lm83, lm82 };
 55
 56/*
 57 * The LM83 registers
 58 * Manufacturer ID is 0x01 for National Semiconductor.
 59 */
 60
 61#define LM83_REG_R_MAN_ID		0xFE
 62#define LM83_REG_R_CHIP_ID		0xFF
 63#define LM83_REG_R_CONFIG		0x03
 64#define LM83_REG_W_CONFIG		0x09
 65#define LM83_REG_R_STATUS1		0x02
 66#define LM83_REG_R_STATUS2		0x35
 67#define LM83_REG_R_LOCAL_TEMP		0x00
 68#define LM83_REG_R_LOCAL_HIGH		0x05
 69#define LM83_REG_W_LOCAL_HIGH		0x0B
 70#define LM83_REG_R_REMOTE1_TEMP		0x30
 71#define LM83_REG_R_REMOTE1_HIGH		0x38
 72#define LM83_REG_W_REMOTE1_HIGH		0x50
 73#define LM83_REG_R_REMOTE2_TEMP		0x01
 74#define LM83_REG_R_REMOTE2_HIGH		0x07
 75#define LM83_REG_W_REMOTE2_HIGH		0x0D
 76#define LM83_REG_R_REMOTE3_TEMP		0x31
 77#define LM83_REG_R_REMOTE3_HIGH		0x3A
 78#define LM83_REG_W_REMOTE3_HIGH		0x52
 79#define LM83_REG_R_TCRIT		0x42
 80#define LM83_REG_W_TCRIT		0x5A
 81
 82/*
 83 * Conversions and various macros
 84 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
 85 */
 86
 87#define TEMP_FROM_REG(val)	((val) * 1000)
 88#define TEMP_TO_REG(val)	((val) <= -128000 ? -128 : \
 89				 (val) >= 127000 ? 127 : \
 90				 (val) < 0 ? ((val) - 500) / 1000 : \
 91				 ((val) + 500) / 1000)
 92
 93static const u8 LM83_REG_R_TEMP[] = {
 94	LM83_REG_R_LOCAL_TEMP,
 95	LM83_REG_R_REMOTE1_TEMP,
 96	LM83_REG_R_REMOTE2_TEMP,
 97	LM83_REG_R_REMOTE3_TEMP,
 98	LM83_REG_R_LOCAL_HIGH,
 99	LM83_REG_R_REMOTE1_HIGH,
100	LM83_REG_R_REMOTE2_HIGH,
101	LM83_REG_R_REMOTE3_HIGH,
102	LM83_REG_R_TCRIT,
103};
104
105static const u8 LM83_REG_W_HIGH[] = {
106	LM83_REG_W_LOCAL_HIGH,
107	LM83_REG_W_REMOTE1_HIGH,
108	LM83_REG_W_REMOTE2_HIGH,
109	LM83_REG_W_REMOTE3_HIGH,
110	LM83_REG_W_TCRIT,
111};
112
113/*
114 * Functions declaration
115 */
116
117static int lm83_detect(struct i2c_client *new_client,
118		       struct i2c_board_info *info);
119static int lm83_probe(struct i2c_client *client,
120		      const struct i2c_device_id *id);
121static int lm83_remove(struct i2c_client *client);
122static struct lm83_data *lm83_update_device(struct device *dev);
123
124/*
125 * Driver data (common to all clients)
126 */
127
128static const struct i2c_device_id lm83_id[] = {
129	{ "lm83", lm83 },
130	{ "lm82", lm82 },
131	{ }
132};
133MODULE_DEVICE_TABLE(i2c, lm83_id);
134
135static struct i2c_driver lm83_driver = {
136	.class		= I2C_CLASS_HWMON,
137	.driver = {
138		.name	= "lm83",
139	},
140	.probe		= lm83_probe,
141	.remove		= lm83_remove,
142	.id_table	= lm83_id,
143	.detect		= lm83_detect,
144	.address_list	= normal_i2c,
145};
146
147/*
148 * Client data (each client gets its own)
149 */
150
151struct lm83_data {
152	struct device *hwmon_dev;
 
153	struct mutex update_lock;
154	char valid; /* zero until following fields are valid */
155	unsigned long last_updated; /* in jiffies */
156
157	/* registers values */
158	s8 temp[9];	/* 0..3: input 1-4,
159			   4..7: high limit 1-4,
160			   8   : critical limit */
161	u16 alarms; /* bitvector, combined */
162};
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164/*
165 * Sysfs stuff
166 */
167
168static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
169			 char *buf)
170{
171	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
172	struct lm83_data *data = lm83_update_device(dev);
173	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
174}
175
176static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
177			const char *buf, size_t count)
 
178{
179	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
180	struct i2c_client *client = to_i2c_client(dev);
181	struct lm83_data *data = i2c_get_clientdata(client);
182	long val;
183	int nr = attr->index;
184	int err;
185
186	err = kstrtol(buf, 10, &val);
187	if (err < 0)
188		return err;
189
190	mutex_lock(&data->update_lock);
191	data->temp[nr] = TEMP_TO_REG(val);
192	i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
193				  data->temp[nr]);
194	mutex_unlock(&data->update_lock);
195	return count;
196}
197
198static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
199			   char *buf)
200{
201	struct lm83_data *data = lm83_update_device(dev);
202	return sprintf(buf, "%d\n", data->alarms);
203}
204
205static ssize_t show_alarm(struct device *dev, struct device_attribute
206			  *devattr, char *buf)
207{
208	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209	struct lm83_data *data = lm83_update_device(dev);
210	int bitnr = attr->index;
211
212	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
213}
214
215static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
216static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
217static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
218static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
219static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
220	set_temp, 4);
221static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
222	set_temp, 5);
223static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
224	set_temp, 6);
225static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
226	set_temp, 7);
227static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
228static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
229static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
230	set_temp, 8);
231static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
232
233/* Individual alarm files */
234static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
235static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
236static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
237static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
238static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
239static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
240static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
241static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
242static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
243static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
244static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
245/* Raw alarm file for compatibility */
246static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
247
248static struct attribute *lm83_attributes[] = {
249	&sensor_dev_attr_temp1_input.dev_attr.attr,
250	&sensor_dev_attr_temp3_input.dev_attr.attr,
251	&sensor_dev_attr_temp1_max.dev_attr.attr,
252	&sensor_dev_attr_temp3_max.dev_attr.attr,
253	&sensor_dev_attr_temp1_crit.dev_attr.attr,
254	&sensor_dev_attr_temp3_crit.dev_attr.attr,
255
256	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
257	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
258	&sensor_dev_attr_temp3_fault.dev_attr.attr,
259	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
260	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
261	&dev_attr_alarms.attr,
262	NULL
263};
264
265static const struct attribute_group lm83_group = {
266	.attrs = lm83_attributes,
267};
268
269static struct attribute *lm83_attributes_opt[] = {
270	&sensor_dev_attr_temp2_input.dev_attr.attr,
271	&sensor_dev_attr_temp4_input.dev_attr.attr,
272	&sensor_dev_attr_temp2_max.dev_attr.attr,
273	&sensor_dev_attr_temp4_max.dev_attr.attr,
274	&sensor_dev_attr_temp2_crit.dev_attr.attr,
275	&sensor_dev_attr_temp4_crit.dev_attr.attr,
276
277	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
278	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
279	&sensor_dev_attr_temp4_fault.dev_attr.attr,
280	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
281	&sensor_dev_attr_temp2_fault.dev_attr.attr,
282	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
283	NULL
284};
285
286static const struct attribute_group lm83_group_opt = {
287	.attrs = lm83_attributes_opt,
288};
289
290/*
291 * Real code
292 */
293
294/* Return 0 if detection is successful, -ENODEV otherwise */
295static int lm83_detect(struct i2c_client *new_client,
296		       struct i2c_board_info *info)
297{
298	struct i2c_adapter *adapter = new_client->adapter;
299	const char *name;
300	u8 man_id, chip_id;
301
302	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
303		return -ENODEV;
304
305	/* Detection */
306	if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
307	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
308	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
309		dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
310			new_client->addr);
311		return -ENODEV;
312	}
313
314	/* Identification */
315	man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
316	if (man_id != 0x01)	/* National Semiconductor */
317		return -ENODEV;
318
319	chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
320	switch (chip_id) {
321	case 0x03:
322		name = "lm83";
323		break;
324	case 0x01:
325		name = "lm82";
326		break;
327	default:
328		/* identification failed */
329		dev_info(&adapter->dev,
330			 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
331			 man_id, chip_id);
332		return -ENODEV;
333	}
334
335	strlcpy(info->type, name, I2C_NAME_SIZE);
336
337	return 0;
338}
339
340static int lm83_probe(struct i2c_client *new_client,
341		      const struct i2c_device_id *id)
342{
 
343	struct lm83_data *data;
344	int err;
345
346	data = devm_kzalloc(&new_client->dev, sizeof(struct lm83_data),
347			    GFP_KERNEL);
348	if (!data)
349		return -ENOMEM;
350
351	i2c_set_clientdata(new_client, data);
352	mutex_init(&data->update_lock);
353
354	/*
355	 * Register sysfs hooks
356	 * The LM82 can only monitor one external diode which is
357	 * at the same register as the LM83 temp3 entry - so we
358	 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
359	 */
360
361	err = sysfs_create_group(&new_client->dev.kobj, &lm83_group);
362	if (err)
363		return err;
364
365	if (id->driver_data == lm83) {
366		err = sysfs_create_group(&new_client->dev.kobj,
367					 &lm83_group_opt);
368		if (err)
369			goto exit_remove_files;
370	}
371
372	data->hwmon_dev = hwmon_device_register(&new_client->dev);
373	if (IS_ERR(data->hwmon_dev)) {
374		err = PTR_ERR(data->hwmon_dev);
375		goto exit_remove_files;
376	}
377
378	return 0;
379
380exit_remove_files:
381	sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
382	sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
383	return err;
384}
385
386static int lm83_remove(struct i2c_client *client)
387{
388	struct lm83_data *data = i2c_get_clientdata(client);
389
390	hwmon_device_unregister(data->hwmon_dev);
391	sysfs_remove_group(&client->dev.kobj, &lm83_group);
392	sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
393
394	return 0;
395}
396
397static struct lm83_data *lm83_update_device(struct device *dev)
398{
399	struct i2c_client *client = to_i2c_client(dev);
400	struct lm83_data *data = i2c_get_clientdata(client);
401
402	mutex_lock(&data->update_lock);
403
404	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
405		int nr;
406
407		dev_dbg(&client->dev, "Updating lm83 data.\n");
408		for (nr = 0; nr < 9; nr++) {
409			data->temp[nr] =
410			    i2c_smbus_read_byte_data(client,
411			    LM83_REG_R_TEMP[nr]);
412		}
413		data->alarms =
414		    i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
415		    + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
416		    << 8);
417
418		data->last_updated = jiffies;
419		data->valid = 1;
420	}
421
422	mutex_unlock(&data->update_lock);
 
 
 
 
 
423
424	return data;
425}
 
 
 
 
 
 
 
 
426
427module_i2c_driver(lm83_driver);
428
429MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
430MODULE_DESCRIPTION("LM83 driver");
431MODULE_LICENSE("GPL");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  4 *          monitoring
  5 * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
  6 *
  7 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
  8 * a sensor chip made by National Semiconductor. It reports up to four
  9 * temperatures (its own plus up to three external ones) with a 1 deg
 10 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
 11 * from National's website at:
 12 *   http://www.national.com/pf/LM/LM83.html
 13 * Since the datasheet omits to give the chip stepping code, I give it
 14 * here: 0x03 (at register 0xff).
 15 *
 16 * Also supports the LM82 temp sensor, which is basically a stripped down
 17 * model of the LM83.  Datasheet is here:
 18 * http://www.national.com/pf/LM/LM82.html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 19 */
 20
 21#include <linux/module.h>
 22#include <linux/init.h>
 23#include <linux/slab.h>
 24#include <linux/jiffies.h>
 25#include <linux/i2c.h>
 26#include <linux/hwmon-sysfs.h>
 27#include <linux/hwmon.h>
 28#include <linux/err.h>
 29#include <linux/mutex.h>
 30#include <linux/sysfs.h>
 31
 32/*
 33 * Addresses to scan
 34 * Address is selected using 2 three-level pins, resulting in 9 possible
 35 * addresses.
 36 */
 37
 38static const unsigned short normal_i2c[] = {
 39	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
 40
 41enum chips { lm83, lm82 };
 42
 43/*
 44 * The LM83 registers
 45 * Manufacturer ID is 0x01 for National Semiconductor.
 46 */
 47
 48#define LM83_REG_R_MAN_ID		0xFE
 49#define LM83_REG_R_CHIP_ID		0xFF
 50#define LM83_REG_R_CONFIG		0x03
 51#define LM83_REG_W_CONFIG		0x09
 52#define LM83_REG_R_STATUS1		0x02
 53#define LM83_REG_R_STATUS2		0x35
 54#define LM83_REG_R_LOCAL_TEMP		0x00
 55#define LM83_REG_R_LOCAL_HIGH		0x05
 56#define LM83_REG_W_LOCAL_HIGH		0x0B
 57#define LM83_REG_R_REMOTE1_TEMP		0x30
 58#define LM83_REG_R_REMOTE1_HIGH		0x38
 59#define LM83_REG_W_REMOTE1_HIGH		0x50
 60#define LM83_REG_R_REMOTE2_TEMP		0x01
 61#define LM83_REG_R_REMOTE2_HIGH		0x07
 62#define LM83_REG_W_REMOTE2_HIGH		0x0D
 63#define LM83_REG_R_REMOTE3_TEMP		0x31
 64#define LM83_REG_R_REMOTE3_HIGH		0x3A
 65#define LM83_REG_W_REMOTE3_HIGH		0x52
 66#define LM83_REG_R_TCRIT		0x42
 67#define LM83_REG_W_TCRIT		0x5A
 68
 69/*
 70 * Conversions and various macros
 71 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
 72 */
 73
 74#define TEMP_FROM_REG(val)	((val) * 1000)
 75#define TEMP_TO_REG(val)	((val) <= -128000 ? -128 : \
 76				 (val) >= 127000 ? 127 : \
 77				 (val) < 0 ? ((val) - 500) / 1000 : \
 78				 ((val) + 500) / 1000)
 79
 80static const u8 LM83_REG_R_TEMP[] = {
 81	LM83_REG_R_LOCAL_TEMP,
 82	LM83_REG_R_REMOTE1_TEMP,
 83	LM83_REG_R_REMOTE2_TEMP,
 84	LM83_REG_R_REMOTE3_TEMP,
 85	LM83_REG_R_LOCAL_HIGH,
 86	LM83_REG_R_REMOTE1_HIGH,
 87	LM83_REG_R_REMOTE2_HIGH,
 88	LM83_REG_R_REMOTE3_HIGH,
 89	LM83_REG_R_TCRIT,
 90};
 91
 92static const u8 LM83_REG_W_HIGH[] = {
 93	LM83_REG_W_LOCAL_HIGH,
 94	LM83_REG_W_REMOTE1_HIGH,
 95	LM83_REG_W_REMOTE2_HIGH,
 96	LM83_REG_W_REMOTE3_HIGH,
 97	LM83_REG_W_TCRIT,
 98};
 99
100/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101 * Client data (each client gets its own)
102 */
103
104struct lm83_data {
105	struct i2c_client *client;
106	const struct attribute_group *groups[3];
107	struct mutex update_lock;
108	char valid; /* zero until following fields are valid */
109	unsigned long last_updated; /* in jiffies */
110
111	/* registers values */
112	s8 temp[9];	/* 0..3: input 1-4,
113			   4..7: high limit 1-4,
114			   8   : critical limit */
115	u16 alarms; /* bitvector, combined */
116};
117
118static struct lm83_data *lm83_update_device(struct device *dev)
119{
120	struct lm83_data *data = dev_get_drvdata(dev);
121	struct i2c_client *client = data->client;
122
123	mutex_lock(&data->update_lock);
124
125	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
126		int nr;
127
128		dev_dbg(&client->dev, "Updating lm83 data.\n");
129		for (nr = 0; nr < 9; nr++) {
130			data->temp[nr] =
131			    i2c_smbus_read_byte_data(client,
132			    LM83_REG_R_TEMP[nr]);
133		}
134		data->alarms =
135		    i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
136		    + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
137		    << 8);
138
139		data->last_updated = jiffies;
140		data->valid = 1;
141	}
142
143	mutex_unlock(&data->update_lock);
144
145	return data;
146}
147
148/*
149 * Sysfs stuff
150 */
151
152static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
153			 char *buf)
154{
155	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
156	struct lm83_data *data = lm83_update_device(dev);
157	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
158}
159
160static ssize_t temp_store(struct device *dev,
161			  struct device_attribute *devattr, const char *buf,
162			  size_t count)
163{
164	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
165	struct lm83_data *data = dev_get_drvdata(dev);
166	struct i2c_client *client = data->client;
167	long val;
168	int nr = attr->index;
169	int err;
170
171	err = kstrtol(buf, 10, &val);
172	if (err < 0)
173		return err;
174
175	mutex_lock(&data->update_lock);
176	data->temp[nr] = TEMP_TO_REG(val);
177	i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
178				  data->temp[nr]);
179	mutex_unlock(&data->update_lock);
180	return count;
181}
182
183static ssize_t alarms_show(struct device *dev, struct device_attribute *dummy,
184			   char *buf)
185{
186	struct lm83_data *data = lm83_update_device(dev);
187	return sprintf(buf, "%d\n", data->alarms);
188}
189
190static ssize_t alarm_show(struct device *dev,
191			  struct device_attribute *devattr, char *buf)
192{
193	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194	struct lm83_data *data = lm83_update_device(dev);
195	int bitnr = attr->index;
196
197	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
198}
199
200static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
201static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
202static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
203static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
204static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 4);
205static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 5);
206static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 6);
207static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 7);
208static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp, 8);
209static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp, 8);
210static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 8);
211static SENSOR_DEVICE_ATTR_RO(temp4_crit, temp, 8);
 
 
 
 
 
212
213/* Individual alarm files */
214static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 0);
215static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 1);
216static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
217static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 4);
218static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
219static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 8);
220static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, alarm, 9);
221static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, 10);
222static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, 12);
223static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 13);
224static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 15);
225/* Raw alarm file for compatibility */
226static DEVICE_ATTR_RO(alarms);
227
228static struct attribute *lm83_attributes[] = {
229	&sensor_dev_attr_temp1_input.dev_attr.attr,
230	&sensor_dev_attr_temp3_input.dev_attr.attr,
231	&sensor_dev_attr_temp1_max.dev_attr.attr,
232	&sensor_dev_attr_temp3_max.dev_attr.attr,
233	&sensor_dev_attr_temp1_crit.dev_attr.attr,
234	&sensor_dev_attr_temp3_crit.dev_attr.attr,
235
236	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
237	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
238	&sensor_dev_attr_temp3_fault.dev_attr.attr,
239	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
240	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
241	&dev_attr_alarms.attr,
242	NULL
243};
244
245static const struct attribute_group lm83_group = {
246	.attrs = lm83_attributes,
247};
248
249static struct attribute *lm83_attributes_opt[] = {
250	&sensor_dev_attr_temp2_input.dev_attr.attr,
251	&sensor_dev_attr_temp4_input.dev_attr.attr,
252	&sensor_dev_attr_temp2_max.dev_attr.attr,
253	&sensor_dev_attr_temp4_max.dev_attr.attr,
254	&sensor_dev_attr_temp2_crit.dev_attr.attr,
255	&sensor_dev_attr_temp4_crit.dev_attr.attr,
256
257	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
258	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
259	&sensor_dev_attr_temp4_fault.dev_attr.attr,
260	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
261	&sensor_dev_attr_temp2_fault.dev_attr.attr,
262	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
263	NULL
264};
265
266static const struct attribute_group lm83_group_opt = {
267	.attrs = lm83_attributes_opt,
268};
269
270/*
271 * Real code
272 */
273
274/* Return 0 if detection is successful, -ENODEV otherwise */
275static int lm83_detect(struct i2c_client *new_client,
276		       struct i2c_board_info *info)
277{
278	struct i2c_adapter *adapter = new_client->adapter;
279	const char *name;
280	u8 man_id, chip_id;
281
282	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
283		return -ENODEV;
284
285	/* Detection */
286	if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
287	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
288	    (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
289		dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
290			new_client->addr);
291		return -ENODEV;
292	}
293
294	/* Identification */
295	man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
296	if (man_id != 0x01)	/* National Semiconductor */
297		return -ENODEV;
298
299	chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
300	switch (chip_id) {
301	case 0x03:
302		name = "lm83";
303		break;
304	case 0x01:
305		name = "lm82";
306		break;
307	default:
308		/* identification failed */
309		dev_info(&adapter->dev,
310			 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
311			 man_id, chip_id);
312		return -ENODEV;
313	}
314
315	strlcpy(info->type, name, I2C_NAME_SIZE);
316
317	return 0;
318}
319
320static int lm83_probe(struct i2c_client *new_client,
321		      const struct i2c_device_id *id)
322{
323	struct device *hwmon_dev;
324	struct lm83_data *data;
 
325
326	data = devm_kzalloc(&new_client->dev, sizeof(struct lm83_data),
327			    GFP_KERNEL);
328	if (!data)
329		return -ENOMEM;
330
331	data->client = new_client;
332	mutex_init(&data->update_lock);
333
334	/*
335	 * Register sysfs hooks
336	 * The LM82 can only monitor one external diode which is
337	 * at the same register as the LM83 temp3 entry - so we
338	 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
339	 */
340	data->groups[0] = &lm83_group;
341	if (id->driver_data == lm83)
342		data->groups[1] = &lm83_group_opt;
343
344	hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
345							   new_client->name,
346							   data, data->groups);
347	return PTR_ERR_OR_ZERO(hwmon_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348}
349
350/*
351 * Driver data (common to all clients)
352 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
354static const struct i2c_device_id lm83_id[] = {
355	{ "lm83", lm83 },
356	{ "lm82", lm82 },
357	{ }
358};
359MODULE_DEVICE_TABLE(i2c, lm83_id);
360
361static struct i2c_driver lm83_driver = {
362	.class		= I2C_CLASS_HWMON,
363	.driver = {
364		.name	= "lm83",
365	},
366	.probe		= lm83_probe,
367	.id_table	= lm83_id,
368	.detect		= lm83_detect,
369	.address_list	= normal_i2c,
370};
371
372module_i2c_driver(lm83_driver);
373
374MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
375MODULE_DESCRIPTION("LM83 driver");
376MODULE_LICENSE("GPL");