Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2    adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
  3		monitoring
  4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
  5    Philip Edelbrock <phil@netroedge.com>
  6
  7    This program is free software; you can redistribute it and/or modify
  8    it under the terms of the GNU General Public License as published by
  9    the Free Software Foundation; either version 2 of the License, or
 10    (at your option) any later version.
 11
 12    This program is distributed in the hope that it will be useful,
 13    but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15    GNU General Public License for more details.
 16
 17    You should have received a copy of the GNU General Public License
 18    along with this program; if not, write to the Free Software
 19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 20*/
 21
 22#include <linux/module.h>
 23#include <linux/init.h>
 24#include <linux/slab.h>
 25#include <linux/jiffies.h>
 26#include <linux/i2c.h>
 27#include <linux/hwmon.h>
 28#include <linux/hwmon-sysfs.h>
 29#include <linux/err.h>
 30#include <linux/mutex.h>
 31
 32
 33/* Addresses to scan */
 34static const unsigned short normal_i2c[] = {
 35	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
 36
 37enum chips {
 38	adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
 39
 40/* adm1021 constants specified below */
 41
 42/* The adm1021 registers */
 43/* Read-only */
 44/* For nr in 0-1 */
 45#define ADM1021_REG_TEMP(nr)		(nr)
 46#define ADM1021_REG_STATUS		0x02
 47/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
 48#define ADM1021_REG_MAN_ID		0xFE
 49/* ADM1021 = 0x0X, ADM1023 = 0x3X */
 50#define ADM1021_REG_DEV_ID		0xFF
 51/* These use different addresses for reading/writing */
 52#define ADM1021_REG_CONFIG_R		0x03
 53#define ADM1021_REG_CONFIG_W		0x09
 54#define ADM1021_REG_CONV_RATE_R		0x04
 55#define ADM1021_REG_CONV_RATE_W		0x0A
 56/* These are for the ADM1023's additional precision on the remote temp sensor */
 57#define ADM1023_REG_REM_TEMP_PREC	0x10
 58#define ADM1023_REG_REM_OFFSET		0x11
 59#define ADM1023_REG_REM_OFFSET_PREC	0x12
 60#define ADM1023_REG_REM_TOS_PREC	0x13
 61#define ADM1023_REG_REM_THYST_PREC	0x14
 62/* limits */
 63/* For nr in 0-1 */
 64#define ADM1021_REG_TOS_R(nr)		(0x05 + 2 * (nr))
 65#define ADM1021_REG_TOS_W(nr)		(0x0B + 2 * (nr))
 66#define ADM1021_REG_THYST_R(nr)		(0x06 + 2 * (nr))
 67#define ADM1021_REG_THYST_W(nr)		(0x0C + 2 * (nr))
 68/* write-only */
 69#define ADM1021_REG_ONESHOT		0x0F
 70
 71/* Initial values */
 72
 73/* Note: Even though I left the low and high limits named os and hyst,
 74they don't quite work like a thermostat the way the LM75 does.  I.e.,
 75a lower temp than THYST actually triggers an alarm instead of
 76clearing it.  Weird, ey?   --Phil  */
 
 
 77
 78/* Each client has this additional data */
 79struct adm1021_data {
 80	struct device *hwmon_dev;
 81	enum chips type;
 82
 
 
 83	struct mutex update_lock;
 84	char valid;		/* !=0 if following fields are valid */
 85	char low_power;		/* !=0 if device in low power mode */
 86	unsigned long last_updated;	/* In jiffies */
 87
 88	int temp_max[2];		/* Register values */
 89	int temp_min[2];
 90	int temp[2];
 91	u8 alarms;
 92	/* Special values for ADM1023 only */
 93	u8 remote_temp_offset;
 94	u8 remote_temp_offset_prec;
 95};
 96
 97static int adm1021_probe(struct i2c_client *client,
 98			 const struct i2c_device_id *id);
 99static int adm1021_detect(struct i2c_client *client,
100			  struct i2c_board_info *info);
101static void adm1021_init_client(struct i2c_client *client);
102static int adm1021_remove(struct i2c_client *client);
103static struct adm1021_data *adm1021_update_device(struct device *dev);
104
105/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
106static int read_only;
107
 
 
 
 
108
109static const struct i2c_device_id adm1021_id[] = {
110	{ "adm1021", adm1021 },
111	{ "adm1023", adm1023 },
112	{ "max1617", max1617 },
113	{ "max1617a", max1617a },
114	{ "thmc10", thmc10 },
115	{ "lm84", lm84 },
116	{ "gl523sm", gl523sm },
117	{ "mc1066", mc1066 },
118	{ }
119};
120MODULE_DEVICE_TABLE(i2c, adm1021_id);
121
122/* This is the driver that will be inserted */
123static struct i2c_driver adm1021_driver = {
124	.class		= I2C_CLASS_HWMON,
125	.driver = {
126		.name	= "adm1021",
127	},
128	.probe		= adm1021_probe,
129	.remove		= adm1021_remove,
130	.id_table	= adm1021_id,
131	.detect		= adm1021_detect,
132	.address_list	= normal_i2c,
133};
134
135static ssize_t show_temp(struct device *dev,
136			 struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137{
138	int index = to_sensor_dev_attr(devattr)->index;
139	struct adm1021_data *data = adm1021_update_device(dev);
140
141	return sprintf(buf, "%d\n", data->temp[index]);
142}
143
144static ssize_t show_temp_max(struct device *dev,
145			     struct device_attribute *devattr, char *buf)
146{
147	int index = to_sensor_dev_attr(devattr)->index;
148	struct adm1021_data *data = adm1021_update_device(dev);
149
150	return sprintf(buf, "%d\n", data->temp_max[index]);
151}
152
153static ssize_t show_temp_min(struct device *dev,
154			     struct device_attribute *devattr, char *buf)
155{
156	int index = to_sensor_dev_attr(devattr)->index;
157	struct adm1021_data *data = adm1021_update_device(dev);
158
159	return sprintf(buf, "%d\n", data->temp_min[index]);
160}
161
162static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
163			  char *buf)
164{
165	int index = to_sensor_dev_attr(attr)->index;
166	struct adm1021_data *data = adm1021_update_device(dev);
167	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
168}
169
170static ssize_t show_alarms(struct device *dev,
171			   struct device_attribute *attr,
172			   char *buf)
173{
174	struct adm1021_data *data = adm1021_update_device(dev);
175	return sprintf(buf, "%u\n", data->alarms);
176}
177
178static ssize_t set_temp_max(struct device *dev,
179			    struct device_attribute *devattr,
180			    const char *buf, size_t count)
181{
182	int index = to_sensor_dev_attr(devattr)->index;
183	struct i2c_client *client = to_i2c_client(dev);
184	struct adm1021_data *data = i2c_get_clientdata(client);
185	long temp = simple_strtol(buf, NULL, 10) / 1000;
 
 
 
 
 
 
186
187	mutex_lock(&data->update_lock);
188	data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
 
189	if (!read_only)
190		i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
191					  data->temp_max[index]);
192	mutex_unlock(&data->update_lock);
193
194	return count;
195}
196
197static ssize_t set_temp_min(struct device *dev,
198			    struct device_attribute *devattr,
199			    const char *buf, size_t count)
200{
201	int index = to_sensor_dev_attr(devattr)->index;
202	struct i2c_client *client = to_i2c_client(dev);
203	struct adm1021_data *data = i2c_get_clientdata(client);
204	long temp = simple_strtol(buf, NULL, 10) / 1000;
 
 
 
 
 
 
205
206	mutex_lock(&data->update_lock);
207	data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
 
208	if (!read_only)
209		i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
210					  data->temp_min[index]);
211	mutex_unlock(&data->update_lock);
212
213	return count;
214}
215
216static ssize_t show_low_power(struct device *dev,
217			      struct device_attribute *devattr, char *buf)
218{
219	struct adm1021_data *data = adm1021_update_device(dev);
220	return sprintf(buf, "%d\n", data->low_power);
221}
222
223static ssize_t set_low_power(struct device *dev,
224			     struct device_attribute *devattr,
225			     const char *buf, size_t count)
226{
227	struct i2c_client *client = to_i2c_client(dev);
228	struct adm1021_data *data = i2c_get_clientdata(client);
229	int low_power = simple_strtol(buf, NULL, 10) != 0;
 
 
 
 
 
 
 
230
231	mutex_lock(&data->update_lock);
232	if (low_power != data->low_power) {
233		int config = i2c_smbus_read_byte_data(
234			client, ADM1021_REG_CONFIG_R);
235		data->low_power = low_power;
236		i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
237			(config & 0xBF) | (low_power << 6));
238	}
239	mutex_unlock(&data->update_lock);
240
241	return count;
242}
243
244
245static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
246static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
247			  set_temp_max, 0);
248static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
249			  set_temp_min, 0);
250static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
251static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
252			  set_temp_max, 1);
253static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
254			  set_temp_min, 1);
255static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
256static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
257static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
258static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
259static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
260
261static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
262static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
263
264static struct attribute *adm1021_attributes[] = {
265	&sensor_dev_attr_temp1_max.dev_attr.attr,
266	&sensor_dev_attr_temp1_min.dev_attr.attr,
267	&sensor_dev_attr_temp1_input.dev_attr.attr,
268	&sensor_dev_attr_temp2_max.dev_attr.attr,
269	&sensor_dev_attr_temp2_min.dev_attr.attr,
270	&sensor_dev_attr_temp2_input.dev_attr.attr,
271	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
272	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
273	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
274	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
275	&sensor_dev_attr_temp2_fault.dev_attr.attr,
276	&dev_attr_alarms.attr,
277	&dev_attr_low_power.attr,
278	NULL
279};
280
281static const struct attribute_group adm1021_group = {
282	.attrs = adm1021_attributes,
283};
284
 
 
 
 
 
 
 
 
 
 
 
 
285/* Return 0 if detection is successful, -ENODEV otherwise */
286static int adm1021_detect(struct i2c_client *client,
287			  struct i2c_board_info *info)
288{
289	struct i2c_adapter *adapter = client->adapter;
290	const char *type_name;
291	int conv_rate, status, config, man_id, dev_id;
292
293	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
294		pr_debug("adm1021: detect failed, "
295			 "smbus byte data not supported!\n");
296		return -ENODEV;
297	}
298
299	status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
300	conv_rate = i2c_smbus_read_byte_data(client,
301					     ADM1021_REG_CONV_RATE_R);
302	config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
303
304	/* Check unused bits */
305	if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
306		pr_debug("adm1021: detect failed, chip not detected!\n");
307		return -ENODEV;
308	}
309
310	/* Determine the chip type. */
311	man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
312	dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
313
314	if (man_id == 0x4d && dev_id == 0x01)
 
 
 
 
 
 
 
 
 
 
 
 
 
315		type_name = "max1617a";
316	else if (man_id == 0x41) {
317		if ((dev_id & 0xF0) == 0x30)
318			type_name = "adm1023";
319		else
320			type_name = "adm1021";
 
 
321	} else if (man_id == 0x49)
322		type_name = "thmc10";
323	else if (man_id == 0x23)
324		type_name = "gl523sm";
325	else if (man_id == 0x54)
326		type_name = "mc1066";
327	/* LM84 Mfr ID in a different place, and it has more unused bits */
328	else if (conv_rate == 0x00
329		 && (config & 0x7F) == 0x00
330		 && (status & 0xAB) == 0x00)
331		type_name = "lm84";
332	else
333		type_name = "max1617";
334
335	pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
336		 type_name, i2c_adapter_id(adapter), client->addr);
337	strlcpy(info->type, type_name, I2C_NAME_SIZE);
338
339	return 0;
340}
341
342static int adm1021_probe(struct i2c_client *client,
343			 const struct i2c_device_id *id)
344{
345	struct adm1021_data *data;
346	int err;
347
348	data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
349	if (!data) {
350		pr_debug("adm1021: detect failed, kzalloc failed!\n");
351		err = -ENOMEM;
352		goto error0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353	}
354
355	i2c_set_clientdata(client, data);
356	data->type = id->driver_data;
357	mutex_init(&data->update_lock);
358
359	/* Initialize the ADM1021 chip */
360	if (data->type != lm84 && !read_only)
361		adm1021_init_client(client);
362
363	/* Register sysfs hooks */
364	if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
365		goto error1;
366
367	data->hwmon_dev = hwmon_device_register(&client->dev);
368	if (IS_ERR(data->hwmon_dev)) {
369		err = PTR_ERR(data->hwmon_dev);
370		goto error3;
371	}
372
373	return 0;
374
375error3:
376	sysfs_remove_group(&client->dev.kobj, &adm1021_group);
377error1:
378	kfree(data);
379error0:
380	return err;
381}
382
383static void adm1021_init_client(struct i2c_client *client)
384{
385	/* Enable ADC and disable suspend mode */
386	i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
387		i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
388	/* Set Conversion rate to 1/sec (this can be tinkered with) */
389	i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
390}
391
392static int adm1021_remove(struct i2c_client *client)
393{
394	struct adm1021_data *data = i2c_get_clientdata(client);
395
396	hwmon_device_unregister(data->hwmon_dev);
397	sysfs_remove_group(&client->dev.kobj, &adm1021_group);
398
399	kfree(data);
400	return 0;
401}
402
403static struct adm1021_data *adm1021_update_device(struct device *dev)
404{
405	struct i2c_client *client = to_i2c_client(dev);
406	struct adm1021_data *data = i2c_get_clientdata(client);
 
407
408	mutex_lock(&data->update_lock);
 
 
409
410	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
411	    || !data->valid) {
412		int i;
413
414		dev_dbg(&client->dev, "Starting adm1021 update\n");
 
 
415
416		for (i = 0; i < 2; i++) {
417			data->temp[i] = 1000 *
418				(s8) i2c_smbus_read_byte_data(
419					client, ADM1021_REG_TEMP(i));
420			data->temp_max[i] = 1000 *
421				(s8) i2c_smbus_read_byte_data(
422					client, ADM1021_REG_TOS_R(i));
423			data->temp_min[i] = 1000 *
424				(s8) i2c_smbus_read_byte_data(
425					client, ADM1021_REG_THYST_R(i));
426		}
427		data->alarms = i2c_smbus_read_byte_data(client,
428						ADM1021_REG_STATUS) & 0x7c;
429		if (data->type == adm1023) {
430			/* The ADM1023 provides 3 extra bits of precision for
431			 * the remote sensor in extra registers. */
432			data->temp[1] += 125 * (i2c_smbus_read_byte_data(
433				client, ADM1023_REG_REM_TEMP_PREC) >> 5);
434			data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
435				client, ADM1023_REG_REM_TOS_PREC) >> 5);
436			data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
437				client, ADM1023_REG_REM_THYST_PREC) >> 5);
438			data->remote_temp_offset =
439				i2c_smbus_read_byte_data(client,
440						ADM1023_REG_REM_OFFSET);
441			data->remote_temp_offset_prec =
442				i2c_smbus_read_byte_data(client,
443						ADM1023_REG_REM_OFFSET_PREC);
444		}
445		data->last_updated = jiffies;
446		data->valid = 1;
447	}
448
449	mutex_unlock(&data->update_lock);
 
450
451	return data;
452}
453
454static int __init sensors_adm1021_init(void)
455{
456	return i2c_add_driver(&adm1021_driver);
457}
 
 
 
 
 
 
 
 
458
459static void __exit sensors_adm1021_exit(void)
460{
461	i2c_del_driver(&adm1021_driver);
462}
 
 
 
 
 
 
463
464MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
 
 
465		"Philip Edelbrock <phil@netroedge.com>");
466MODULE_DESCRIPTION("adm1021 driver");
467MODULE_LICENSE("GPL");
468
469module_param(read_only, bool, 0);
470MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
471
472module_init(sensors_adm1021_init)
473module_exit(sensors_adm1021_exit)
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
  4 *	       monitoring
  5 * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
  6 *			     Philip Edelbrock <phil@netroedge.com>
  7 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8
  9#include <linux/module.h>
 10#include <linux/init.h>
 11#include <linux/slab.h>
 12#include <linux/jiffies.h>
 13#include <linux/i2c.h>
 14#include <linux/hwmon.h>
 15#include <linux/hwmon-sysfs.h>
 16#include <linux/err.h>
 17#include <linux/mutex.h>
 18
 19
 20/* Addresses to scan */
 21static const unsigned short normal_i2c[] = {
 22	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
 23
 24enum chips {
 25	adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
 26
 27/* adm1021 constants specified below */
 28
 29/* The adm1021 registers */
 30/* Read-only */
 31/* For nr in 0-1 */
 32#define ADM1021_REG_TEMP(nr)		(nr)
 33#define ADM1021_REG_STATUS		0x02
 34/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
 35#define ADM1021_REG_MAN_ID		0xFE
 36/* ADM1021 = 0x0X, ADM1023 = 0x3X */
 37#define ADM1021_REG_DEV_ID		0xFF
 38/* These use different addresses for reading/writing */
 39#define ADM1021_REG_CONFIG_R		0x03
 40#define ADM1021_REG_CONFIG_W		0x09
 41#define ADM1021_REG_CONV_RATE_R		0x04
 42#define ADM1021_REG_CONV_RATE_W		0x0A
 43/* These are for the ADM1023's additional precision on the remote temp sensor */
 44#define ADM1023_REG_REM_TEMP_PREC	0x10
 45#define ADM1023_REG_REM_OFFSET		0x11
 46#define ADM1023_REG_REM_OFFSET_PREC	0x12
 47#define ADM1023_REG_REM_TOS_PREC	0x13
 48#define ADM1023_REG_REM_THYST_PREC	0x14
 49/* limits */
 50/* For nr in 0-1 */
 51#define ADM1021_REG_TOS_R(nr)		(0x05 + 2 * (nr))
 52#define ADM1021_REG_TOS_W(nr)		(0x0B + 2 * (nr))
 53#define ADM1021_REG_THYST_R(nr)		(0x06 + 2 * (nr))
 54#define ADM1021_REG_THYST_W(nr)		(0x0C + 2 * (nr))
 55/* write-only */
 56#define ADM1021_REG_ONESHOT		0x0F
 57
 58/* Initial values */
 59
 60/*
 61 * Note: Even though I left the low and high limits named os and hyst,
 62 * they don't quite work like a thermostat the way the LM75 does.  I.e.,
 63 * a lower temp than THYST actually triggers an alarm instead of
 64 * clearing it.  Weird, ey?   --Phil
 65 */
 66
 67/* Each client has this additional data */
 68struct adm1021_data {
 69	struct i2c_client *client;
 70	enum chips type;
 71
 72	const struct attribute_group *groups[3];
 73
 74	struct mutex update_lock;
 75	bool valid;		/* true if following fields are valid */
 76	char low_power;		/* !=0 if device in low power mode */
 77	unsigned long last_updated;	/* In jiffies */
 78
 79	int temp_max[2];		/* Register values */
 80	int temp_min[2];
 81	int temp[2];
 82	u8 alarms;
 83	/* Special values for ADM1023 only */
 84	u8 remote_temp_offset;
 85	u8 remote_temp_offset_prec;
 86};
 87
 
 
 
 
 
 
 
 
 88/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
 89static bool read_only;
 90
 91static struct adm1021_data *adm1021_update_device(struct device *dev)
 92{
 93	struct adm1021_data *data = dev_get_drvdata(dev);
 94	struct i2c_client *client = data->client;
 95
 96	mutex_lock(&data->update_lock);
 
 
 
 
 
 
 
 
 
 
 
 97
 98	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
 99	    || !data->valid) {
100		int i;
101
102		dev_dbg(dev, "Starting adm1021 update\n");
 
 
 
 
 
 
 
103
104		for (i = 0; i < 2; i++) {
105			data->temp[i] = 1000 *
106				(s8) i2c_smbus_read_byte_data(
107					client, ADM1021_REG_TEMP(i));
108			data->temp_max[i] = 1000 *
109				(s8) i2c_smbus_read_byte_data(
110					client, ADM1021_REG_TOS_R(i));
111			if (data->type != lm84) {
112				data->temp_min[i] = 1000 *
113				  (s8) i2c_smbus_read_byte_data(client,
114							ADM1021_REG_THYST_R(i));
115			}
116		}
117		data->alarms = i2c_smbus_read_byte_data(client,
118						ADM1021_REG_STATUS) & 0x7c;
119		if (data->type == adm1023) {
120			/*
121			 * The ADM1023 provides 3 extra bits of precision for
122			 * the remote sensor in extra registers.
123			 */
124			data->temp[1] += 125 * (i2c_smbus_read_byte_data(
125				client, ADM1023_REG_REM_TEMP_PREC) >> 5);
126			data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
127				client, ADM1023_REG_REM_TOS_PREC) >> 5);
128			data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
129				client, ADM1023_REG_REM_THYST_PREC) >> 5);
130			data->remote_temp_offset =
131				i2c_smbus_read_byte_data(client,
132						ADM1023_REG_REM_OFFSET);
133			data->remote_temp_offset_prec =
134				i2c_smbus_read_byte_data(client,
135						ADM1023_REG_REM_OFFSET_PREC);
136		}
137		data->last_updated = jiffies;
138		data->valid = true;
139	}
140
141	mutex_unlock(&data->update_lock);
142
143	return data;
144}
145
146static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
147			 char *buf)
148{
149	int index = to_sensor_dev_attr(devattr)->index;
150	struct adm1021_data *data = adm1021_update_device(dev);
151
152	return sprintf(buf, "%d\n", data->temp[index]);
153}
154
155static ssize_t temp_max_show(struct device *dev,
156			     struct device_attribute *devattr, char *buf)
157{
158	int index = to_sensor_dev_attr(devattr)->index;
159	struct adm1021_data *data = adm1021_update_device(dev);
160
161	return sprintf(buf, "%d\n", data->temp_max[index]);
162}
163
164static ssize_t temp_min_show(struct device *dev,
165			     struct device_attribute *devattr, char *buf)
166{
167	int index = to_sensor_dev_attr(devattr)->index;
168	struct adm1021_data *data = adm1021_update_device(dev);
169
170	return sprintf(buf, "%d\n", data->temp_min[index]);
171}
172
173static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
174			  char *buf)
175{
176	int index = to_sensor_dev_attr(attr)->index;
177	struct adm1021_data *data = adm1021_update_device(dev);
178	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
179}
180
181static ssize_t alarms_show(struct device *dev,
182			   struct device_attribute *attr,
183			   char *buf)
184{
185	struct adm1021_data *data = adm1021_update_device(dev);
186	return sprintf(buf, "%u\n", data->alarms);
187}
188
189static ssize_t temp_max_store(struct device *dev,
190			      struct device_attribute *devattr,
191			      const char *buf, size_t count)
192{
193	int index = to_sensor_dev_attr(devattr)->index;
194	struct adm1021_data *data = dev_get_drvdata(dev);
195	struct i2c_client *client = data->client;
196	long temp;
197	int reg_val, err;
198
199	err = kstrtol(buf, 10, &temp);
200	if (err)
201		return err;
202	temp /= 1000;
203
204	mutex_lock(&data->update_lock);
205	reg_val = clamp_val(temp, -128, 127);
206	data->temp_max[index] = reg_val * 1000;
207	if (!read_only)
208		i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
209					  reg_val);
210	mutex_unlock(&data->update_lock);
211
212	return count;
213}
214
215static ssize_t temp_min_store(struct device *dev,
216			      struct device_attribute *devattr,
217			      const char *buf, size_t count)
218{
219	int index = to_sensor_dev_attr(devattr)->index;
220	struct adm1021_data *data = dev_get_drvdata(dev);
221	struct i2c_client *client = data->client;
222	long temp;
223	int reg_val, err;
224
225	err = kstrtol(buf, 10, &temp);
226	if (err)
227		return err;
228	temp /= 1000;
229
230	mutex_lock(&data->update_lock);
231	reg_val = clamp_val(temp, -128, 127);
232	data->temp_min[index] = reg_val * 1000;
233	if (!read_only)
234		i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
235					  reg_val);
236	mutex_unlock(&data->update_lock);
237
238	return count;
239}
240
241static ssize_t low_power_show(struct device *dev,
242			      struct device_attribute *devattr, char *buf)
243{
244	struct adm1021_data *data = adm1021_update_device(dev);
245	return sprintf(buf, "%d\n", data->low_power);
246}
247
248static ssize_t low_power_store(struct device *dev,
249			       struct device_attribute *devattr,
250			       const char *buf, size_t count)
251{
252	struct adm1021_data *data = dev_get_drvdata(dev);
253	struct i2c_client *client = data->client;
254	char low_power;
255	unsigned long val;
256	int err;
257
258	err = kstrtoul(buf, 10, &val);
259	if (err)
260		return err;
261	low_power = val != 0;
262
263	mutex_lock(&data->update_lock);
264	if (low_power != data->low_power) {
265		int config = i2c_smbus_read_byte_data(
266			client, ADM1021_REG_CONFIG_R);
267		data->low_power = low_power;
268		i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
269			(config & 0xBF) | (low_power << 6));
270	}
271	mutex_unlock(&data->update_lock);
272
273	return count;
274}
275
276
277static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
278static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
279static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
280static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
281static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
282static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
283static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
284static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 5);
285static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
286static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
287static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
 
 
 
 
288
289static DEVICE_ATTR_RO(alarms);
290static DEVICE_ATTR_RW(low_power);
291
292static struct attribute *adm1021_attributes[] = {
293	&sensor_dev_attr_temp1_max.dev_attr.attr,
 
294	&sensor_dev_attr_temp1_input.dev_attr.attr,
295	&sensor_dev_attr_temp2_max.dev_attr.attr,
 
296	&sensor_dev_attr_temp2_input.dev_attr.attr,
297	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
 
298	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
 
299	&sensor_dev_attr_temp2_fault.dev_attr.attr,
300	&dev_attr_alarms.attr,
301	&dev_attr_low_power.attr,
302	NULL
303};
304
305static const struct attribute_group adm1021_group = {
306	.attrs = adm1021_attributes,
307};
308
309static struct attribute *adm1021_min_attributes[] = {
310	&sensor_dev_attr_temp1_min.dev_attr.attr,
311	&sensor_dev_attr_temp2_min.dev_attr.attr,
312	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
313	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
314	NULL
315};
316
317static const struct attribute_group adm1021_min_group = {
318	.attrs = adm1021_min_attributes,
319};
320
321/* Return 0 if detection is successful, -ENODEV otherwise */
322static int adm1021_detect(struct i2c_client *client,
323			  struct i2c_board_info *info)
324{
325	struct i2c_adapter *adapter = client->adapter;
326	const char *type_name;
327	int reg, conv_rate, status, config, man_id, dev_id;
328
329	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
330		pr_debug("detect failed, smbus byte data not supported!\n");
 
331		return -ENODEV;
332	}
333
334	status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
335	conv_rate = i2c_smbus_read_byte_data(client,
336					     ADM1021_REG_CONV_RATE_R);
337	config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
338
339	/* Check unused bits */
340	if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
341		pr_debug("detect failed, chip not detected!\n");
342		return -ENODEV;
343	}
344
345	/* Determine the chip type. */
346	man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
347	dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
348
349	if (man_id < 0 || dev_id < 0)
350		return -ENODEV;
351
352	if (man_id == 0x4d && dev_id == 0x01) {
353		/*
354		 * dev_id 0x01 matches MAX6680, MAX6695, MAX6696, and possibly
355		 * others. Read register which is unsupported on MAX1617 but
356		 * exists on all those chips and compare with the dev_id
357		 * register. If it matches, it may be a MAX1617A.
358		 */
359		reg = i2c_smbus_read_byte_data(client,
360					       ADM1023_REG_REM_TEMP_PREC);
361		if (reg != dev_id)
362			return -ENODEV;
363		type_name = "max1617a";
364	} else if (man_id == 0x41) {
365		if ((dev_id & 0xF0) == 0x30)
366			type_name = "adm1023";
367		else if ((dev_id & 0xF0) == 0x00)
368			type_name = "adm1021";
369		else
370			return -ENODEV;
371	} else if (man_id == 0x49)
372		type_name = "thmc10";
373	else if (man_id == 0x23)
374		type_name = "gl523sm";
375	else if (man_id == 0x54)
376		type_name = "mc1066";
377	else {
378		int lte, rte, lhi, rhi, llo, rlo;
 
 
 
 
 
379
380		/* extra checks for LM84 and MAX1617 to avoid misdetections */
 
 
381
382		llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
383		rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
384
385		/* fail if any of the additional register reads failed */
386		if (llo < 0 || rlo < 0)
387			return -ENODEV;
388
389		lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
390		rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
391		lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
392		rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
393
394		/*
395		 * Fail for negative temperatures and negative high limits.
396		 * This check also catches read errors on the tested registers.
397		 */
398		if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
399			return -ENODEV;
400
401		/* fail if all registers hold the same value */
402		if (lte == rte && lte == lhi && lte == rhi && lte == llo
403		    && lte == rlo)
404			return -ENODEV;
405
406		/*
407		 * LM84 Mfr ID is in a different place,
408		 * and it has more unused bits. Registers at 0xfe and 0xff
409		 * are undefined and return the most recently read value,
410		 * here the value of the configuration register.
411		 */
412		if (conv_rate == 0x00
413		    && man_id == config && dev_id == config
414		    && (config & 0x7F) == 0x00
415		    && (status & 0xAB) == 0x00) {
416			type_name = "lm84";
417		} else {
418			if ((config & 0x3f) || (status & 0x03))
419				return -ENODEV;
420			/* fail if low limits are larger than high limits */
421			if ((s8)llo > lhi || (s8)rlo > rhi)
422				return -ENODEV;
423			type_name = "max1617";
424		}
425	}
426
427	pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
428		 type_name, i2c_adapter_id(adapter), client->addr);
429	strscpy(info->type, type_name, I2C_NAME_SIZE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
431	return 0;
 
 
 
 
 
 
 
432}
433
434static void adm1021_init_client(struct i2c_client *client)
435{
436	/* Enable ADC and disable suspend mode */
437	i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
438		i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
439	/* Set Conversion rate to 1/sec (this can be tinkered with) */
440	i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
441}
442
443static const struct i2c_device_id adm1021_id[];
 
 
 
 
 
444
445static int adm1021_probe(struct i2c_client *client)
 
 
 
 
446{
447	struct device *dev = &client->dev;
448	struct adm1021_data *data;
449	struct device *hwmon_dev;
450
451	data = devm_kzalloc(dev, sizeof(struct adm1021_data), GFP_KERNEL);
452	if (!data)
453		return -ENOMEM;
454
455	data->client = client;
456	data->type = i2c_match_id(adm1021_id, client)->driver_data;
457	mutex_init(&data->update_lock);
458
459	/* Initialize the ADM1021 chip */
460	if (data->type != lm84 && !read_only)
461		adm1021_init_client(client);
462
463	data->groups[0] = &adm1021_group;
464	if (data->type != lm84)
465		data->groups[1] = &adm1021_min_group;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
467	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
468							   data, data->groups);
469
470	return PTR_ERR_OR_ZERO(hwmon_dev);
471}
472
473static const struct i2c_device_id adm1021_id[] = {
474	{ "adm1021", adm1021 },
475	{ "adm1023", adm1023 },
476	{ "max1617", max1617 },
477	{ "max1617a", max1617a },
478	{ "thmc10", thmc10 },
479	{ "lm84", lm84 },
480	{ "gl523sm", gl523sm },
481	{ "mc1066", mc1066 },
482	{ }
483};
484MODULE_DEVICE_TABLE(i2c, adm1021_id);
485
486static struct i2c_driver adm1021_driver = {
487	.class		= I2C_CLASS_HWMON,
488	.driver = {
489		.name	= "adm1021",
490	},
491	.probe_new	= adm1021_probe,
492	.id_table	= adm1021_id,
493	.detect		= adm1021_detect,
494	.address_list	= normal_i2c,
495};
496
497module_i2c_driver(adm1021_driver);
498
499MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
500		"Philip Edelbrock <phil@netroedge.com>");
501MODULE_DESCRIPTION("adm1021 driver");
502MODULE_LICENSE("GPL");
503
504module_param(read_only, bool, 0);
505MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");