xref: /linux/drivers/hwmon/ds1621.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28d5d45fbSJean Delvare /*
319f2c059SGuenter Roeck  * ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
419f2c059SGuenter Roeck  *	      monitoring
519f2c059SGuenter Roeck  * Christian W. Zuckschwerdt  <zany@triq.net>  2000-11-23
619f2c059SGuenter Roeck  * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
719f2c059SGuenter Roeck  * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
87c81c60fSJean Delvare  * the help of Jean Delvare <jdelvare@suse.de>
919f2c059SGuenter Roeck  *
10cd6c8a42SRobert Coulson  * The DS1621 device is a digital temperature/thermometer with 9-bit
11cd6c8a42SRobert Coulson  * resolution, a thermal alarm output (Tout), and user-defined minimum
12cd6c8a42SRobert Coulson  * and maximum temperature thresholds (TH and TL).
13cd6c8a42SRobert Coulson  *
14260f81ffSRobert Coulson  * The DS1625, DS1631, DS1721, and DS1731 are pin compatible with the DS1621
15260f81ffSRobert Coulson  * and similar in operation, with slight variations as noted in the device
16cd6c8a42SRobert Coulson  * datasheets (please refer to www.maximintegrated.com for specific
17cd6c8a42SRobert Coulson  * device information).
18cd6c8a42SRobert Coulson  *
19cd6c8a42SRobert Coulson  * Since the DS1621 was the first chipset supported by this driver,
20cd6c8a42SRobert Coulson  * most comments will refer to this chipset, but are actually general
21cd6c8a42SRobert Coulson  * and concern all supported chipsets, unless mentioned otherwise.
228d5d45fbSJean Delvare  */
238d5d45fbSJean Delvare 
248d5d45fbSJean Delvare #include <linux/module.h>
258d5d45fbSJean Delvare #include <linux/init.h>
268d5d45fbSJean Delvare #include <linux/slab.h>
278d5d45fbSJean Delvare #include <linux/jiffies.h>
288d5d45fbSJean Delvare #include <linux/i2c.h>
29943b0830SMark M. Hoffman #include <linux/hwmon.h>
3046a2e71cSJean Delvare #include <linux/hwmon-sysfs.h>
31943b0830SMark M. Hoffman #include <linux/err.h>
329a61bf63SIngo Molnar #include <linux/mutex.h>
33a5ebe668SJean Delvare #include <linux/sysfs.h>
34cd6c8a42SRobert Coulson #include <linux/kernel.h>
358d5d45fbSJean Delvare 
36cd6c8a42SRobert Coulson /* Supported devices */
37260f81ffSRobert Coulson enum chips { ds1621, ds1625, ds1631, ds1721, ds1731 };
38cd6c8a42SRobert Coulson 
398d5d45fbSJean Delvare /* Insmod parameters */
408d5d45fbSJean Delvare static int polarity = -1;
418d5d45fbSJean Delvare module_param(polarity, int, 0);
428d5d45fbSJean Delvare MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
438d5d45fbSJean Delvare 
44cd6c8a42SRobert Coulson /*
45cd6c8a42SRobert Coulson  * The Configuration/Status register
46cd6c8a42SRobert Coulson  *
47cd6c8a42SRobert Coulson  * - DS1621:
48cd6c8a42SRobert Coulson  *   7    6    5    4    3    2    1    0
49cd6c8a42SRobert Coulson  * |Done|THF |TLF |NVB | X  | X  |POL |1SHOT|
50cd6c8a42SRobert Coulson  *
51cd6c8a42SRobert Coulson  * - DS1625:
52cd6c8a42SRobert Coulson  *   7    6    5    4    3    2    1    0
53cd6c8a42SRobert Coulson  * |Done|THF |TLF |NVB | 1  | 0  |POL |1SHOT|
54cd6c8a42SRobert Coulson  *
55260f81ffSRobert Coulson  * - DS1631, DS1731:
5679c1cc1cSRobert Coulson  *   7    6    5    4    3    2    1    0
5779c1cc1cSRobert Coulson  * |Done|THF |TLF |NVB | R1 | R0 |POL |1SHOT|
5879c1cc1cSRobert Coulson  *
59cd6c8a42SRobert Coulson  * - DS1721:
60cd6c8a42SRobert Coulson  *   7    6    5    4    3    2    1    0
61cd6c8a42SRobert Coulson  * |Done| X  | X  | U  | R1 | R0 |POL |1SHOT|
62cd6c8a42SRobert Coulson  *
63cd6c8a42SRobert Coulson  * Where:
64cd6c8a42SRobert Coulson  * - 'X' is Reserved
65cd6c8a42SRobert Coulson  * - 'U' is Undefined
66cd6c8a42SRobert Coulson  */
678d5d45fbSJean Delvare #define DS1621_REG_CONFIG_NVB		0x10
68cd6c8a42SRobert Coulson #define DS1621_REG_CONFIG_RESOL		0x0C
698d5d45fbSJean Delvare #define DS1621_REG_CONFIG_POLARITY	0x02
708d5d45fbSJean Delvare #define DS1621_REG_CONFIG_1SHOT		0x01
718d5d45fbSJean Delvare #define DS1621_REG_CONFIG_DONE		0x80
728d5d45fbSJean Delvare 
73cd6c8a42SRobert Coulson #define DS1621_REG_CONFIG_RESOL_SHIFT	2
74cd6c8a42SRobert Coulson 
75cd6c8a42SRobert Coulson /* ds1721 conversion rates: {C/LSB, time(ms), resolution bit setting} */
76cd6c8a42SRobert Coulson static const unsigned short ds1721_convrates[] = {
77cd6c8a42SRobert Coulson 	94,	/*  9-bits (0.5,  93.75, RES[0..1] = 0 */
78cd6c8a42SRobert Coulson 	188,	/* 10-bits (0.25, 187.5, RES[0..1] = 1 */
79cd6c8a42SRobert Coulson 	375,	/* 11-bits (0.125,  375, RES[0..1] = 2 */
80cd6c8a42SRobert Coulson 	750,	/* 12-bits (0.0625, 750, RES[0..1] = 3 */
81cd6c8a42SRobert Coulson };
82cd6c8a42SRobert Coulson 
83cd6c8a42SRobert Coulson #define DS1621_CONVERSION_MAX	750
84cd6c8a42SRobert Coulson #define DS1625_CONVERSION_MAX	500
85cd6c8a42SRobert Coulson 
86cd6c8a42SRobert Coulson #define DS1621_TEMP_MAX	125000
87cd6c8a42SRobert Coulson #define DS1621_TEMP_MIN	(-55000)
88cd6c8a42SRobert Coulson 
89cd6c8a42SRobert Coulson /* The DS1621 temperature registers */
9046a2e71cSJean Delvare static const u8 DS1621_REG_TEMP[3] = {
9146a2e71cSJean Delvare 	0xAA,		/* input, word, RO */
9246a2e71cSJean Delvare 	0xA2,		/* min, word, RW */
9346a2e71cSJean Delvare 	0xA1,		/* max, word, RW */
9446a2e71cSJean Delvare };
958d5d45fbSJean Delvare #define DS1621_REG_CONF			0xAC /* byte, RW */
968d5d45fbSJean Delvare #define DS1621_COM_START		0xEE /* no data */
97cd6c8a42SRobert Coulson #define DS1721_COM_START		0x51 /* no data */
988d5d45fbSJean Delvare #define DS1621_COM_STOP			0x22 /* no data */
998d5d45fbSJean Delvare 
1008d5d45fbSJean Delvare /* The DS1621 configuration register */
1018d5d45fbSJean Delvare #define DS1621_ALARM_TEMP_HIGH		0x40
1028d5d45fbSJean Delvare #define DS1621_ALARM_TEMP_LOW		0x20
1038d5d45fbSJean Delvare 
10475819f01SJean Delvare /* Conversions */
1058d5d45fbSJean Delvare #define ALARMS_FROM_REG(val) ((val) & \
1068d5d45fbSJean Delvare 			(DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
1078d5d45fbSJean Delvare 
1088d5d45fbSJean Delvare /* Each client has this additional data */
1098d5d45fbSJean Delvare struct ds1621_data {
1108269b75aSGuenter Roeck 	struct i2c_client *client;
1119a61bf63SIngo Molnar 	struct mutex update_lock;
112952a11caSPaul Fertser 	bool valid;			/* true if following fields are valid */
1138d5d45fbSJean Delvare 	unsigned long last_updated;	/* In jiffies */
114cd6c8a42SRobert Coulson 	enum chips kind;		/* device type */
1158d5d45fbSJean Delvare 
11646a2e71cSJean Delvare 	u16 temp[3];			/* Register values, word */
1178d5d45fbSJean Delvare 	u8 conf;			/* Register encoding, combined */
118a50d9a4dSGuenter Roeck 	u8 zbits;			/* Resolution encoded as number of
119a50d9a4dSGuenter Roeck 					 * zero bits */
120cd6c8a42SRobert Coulson 	u16 update_interval;		/* Conversion rate in milliseconds */
1218d5d45fbSJean Delvare };
1228d5d45fbSJean Delvare 
DS1621_TEMP_FROM_REG(u16 reg)123cd6c8a42SRobert Coulson static inline int DS1621_TEMP_FROM_REG(u16 reg)
124cd6c8a42SRobert Coulson {
125cd6c8a42SRobert Coulson 	return DIV_ROUND_CLOSEST(((s16)reg / 16) * 625, 10);
126cd6c8a42SRobert Coulson }
127cd6c8a42SRobert Coulson 
128cd6c8a42SRobert Coulson /*
129cd6c8a42SRobert Coulson  * TEMP: 0.001C/bit (-55C to +125C)
130cd6c8a42SRobert Coulson  * REG:
131a50d9a4dSGuenter Roeck  *  - 1621, 1625: 0.5C/bit, 7 zero-bits
132a50d9a4dSGuenter Roeck  *  - 1631, 1721, 1731: 0.0625C/bit, 4 zero-bits
133cd6c8a42SRobert Coulson  */
DS1621_TEMP_TO_REG(long temp,u8 zbits)134a50d9a4dSGuenter Roeck static inline u16 DS1621_TEMP_TO_REG(long temp, u8 zbits)
135cd6c8a42SRobert Coulson {
136a50d9a4dSGuenter Roeck 	temp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
137a50d9a4dSGuenter Roeck 	temp = DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
138a50d9a4dSGuenter Roeck 	return temp;
139cd6c8a42SRobert Coulson }
140cd6c8a42SRobert Coulson 
ds1621_init_client(struct ds1621_data * data,struct i2c_client * client)1418269b75aSGuenter Roeck static void ds1621_init_client(struct ds1621_data *data,
1428269b75aSGuenter Roeck 			       struct i2c_client *client)
1438d5d45fbSJean Delvare {
144cd6c8a42SRobert Coulson 	u8 conf, new_conf, sreg, resol;
145e4879e28SJean Delvare 
146e4879e28SJean Delvare 	new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
1478d5d45fbSJean Delvare 	/* switch to continuous conversion mode */
148e4879e28SJean Delvare 	new_conf &= ~DS1621_REG_CONFIG_1SHOT;
1498d5d45fbSJean Delvare 
1508d5d45fbSJean Delvare 	/* setup output polarity */
1518d5d45fbSJean Delvare 	if (polarity == 0)
152e4879e28SJean Delvare 		new_conf &= ~DS1621_REG_CONFIG_POLARITY;
1538d5d45fbSJean Delvare 	else if (polarity == 1)
154e4879e28SJean Delvare 		new_conf |= DS1621_REG_CONFIG_POLARITY;
1558d5d45fbSJean Delvare 
156e4879e28SJean Delvare 	if (conf != new_conf)
157e4879e28SJean Delvare 		i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
1588d5d45fbSJean Delvare 
159cd6c8a42SRobert Coulson 	switch (data->kind) {
160cd6c8a42SRobert Coulson 	case ds1625:
161cd6c8a42SRobert Coulson 		data->update_interval = DS1625_CONVERSION_MAX;
162a50d9a4dSGuenter Roeck 		data->zbits = 7;
163cd6c8a42SRobert Coulson 		sreg = DS1621_COM_START;
164cd6c8a42SRobert Coulson 		break;
16579c1cc1cSRobert Coulson 	case ds1631:
166cd6c8a42SRobert Coulson 	case ds1721:
167260f81ffSRobert Coulson 	case ds1731:
168cd6c8a42SRobert Coulson 		resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
169cd6c8a42SRobert Coulson 			 DS1621_REG_CONFIG_RESOL_SHIFT;
170cd6c8a42SRobert Coulson 		data->update_interval = ds1721_convrates[resol];
171a50d9a4dSGuenter Roeck 		data->zbits = 7 - resol;
172cd6c8a42SRobert Coulson 		sreg = DS1721_COM_START;
173cd6c8a42SRobert Coulson 		break;
174cd6c8a42SRobert Coulson 	default:
175cd6c8a42SRobert Coulson 		data->update_interval = DS1621_CONVERSION_MAX;
176a50d9a4dSGuenter Roeck 		data->zbits = 7;
177cd6c8a42SRobert Coulson 		sreg = DS1621_COM_START;
178cd6c8a42SRobert Coulson 		break;
179cd6c8a42SRobert Coulson 	}
180cd6c8a42SRobert Coulson 
1818d5d45fbSJean Delvare 	/* start conversion */
182cd6c8a42SRobert Coulson 	i2c_smbus_write_byte(client, sreg);
1838d5d45fbSJean Delvare }
1848d5d45fbSJean Delvare 
ds1621_update_client(struct device * dev)1859202add6SJean Delvare static struct ds1621_data *ds1621_update_client(struct device *dev)
1869202add6SJean Delvare {
1878269b75aSGuenter Roeck 	struct ds1621_data *data = dev_get_drvdata(dev);
1888269b75aSGuenter Roeck 	struct i2c_client *client = data->client;
1899202add6SJean Delvare 	u8 new_conf;
1909202add6SJean Delvare 
1919202add6SJean Delvare 	mutex_lock(&data->update_lock);
1929202add6SJean Delvare 
193cd6c8a42SRobert Coulson 	if (time_after(jiffies, data->last_updated + data->update_interval) ||
194cd6c8a42SRobert Coulson 	    !data->valid) {
1959202add6SJean Delvare 		int i;
1969202add6SJean Delvare 
1979202add6SJean Delvare 		dev_dbg(&client->dev, "Starting ds1621 update\n");
1989202add6SJean Delvare 
199594592dcSJean Delvare 		data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
2009202add6SJean Delvare 
2019202add6SJean Delvare 		for (i = 0; i < ARRAY_SIZE(data->temp); i++)
20290f4102cSJean Delvare 			data->temp[i] = i2c_smbus_read_word_swapped(client,
2039202add6SJean Delvare 							 DS1621_REG_TEMP[i]);
2049202add6SJean Delvare 
2059202add6SJean Delvare 		/* reset alarms if necessary */
2069202add6SJean Delvare 		new_conf = data->conf;
2079202add6SJean Delvare 		if (data->temp[0] > data->temp[1])	/* input > min */
2089202add6SJean Delvare 			new_conf &= ~DS1621_ALARM_TEMP_LOW;
2099202add6SJean Delvare 		if (data->temp[0] < data->temp[2])	/* input < max */
2109202add6SJean Delvare 			new_conf &= ~DS1621_ALARM_TEMP_HIGH;
2119202add6SJean Delvare 		if (data->conf != new_conf)
212594592dcSJean Delvare 			i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
2139202add6SJean Delvare 						  new_conf);
2149202add6SJean Delvare 
2159202add6SJean Delvare 		data->last_updated = jiffies;
216952a11caSPaul Fertser 		data->valid = true;
2179202add6SJean Delvare 	}
2189202add6SJean Delvare 
2199202add6SJean Delvare 	mutex_unlock(&data->update_lock);
2209202add6SJean Delvare 
2219202add6SJean Delvare 	return data;
2229202add6SJean Delvare }
2239202add6SJean Delvare 
temp_show(struct device * dev,struct device_attribute * da,char * buf)2240ad85c07SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *da,
22546a2e71cSJean Delvare 			 char *buf)
22646a2e71cSJean Delvare {
22746a2e71cSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
22846a2e71cSJean Delvare 	struct ds1621_data *data = ds1621_update_client(dev);
22946a2e71cSJean Delvare 	return sprintf(buf, "%d\n",
230cd6c8a42SRobert Coulson 		       DS1621_TEMP_FROM_REG(data->temp[attr->index]));
2318d5d45fbSJean Delvare }
2328d5d45fbSJean Delvare 
temp_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)2330ad85c07SGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *da,
23446a2e71cSJean Delvare 			  const char *buf, size_t count)
23546a2e71cSJean Delvare {
23646a2e71cSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
2378269b75aSGuenter Roeck 	struct ds1621_data *data = dev_get_drvdata(dev);
23819f2c059SGuenter Roeck 	long val;
23919f2c059SGuenter Roeck 	int err;
24019f2c059SGuenter Roeck 
24119f2c059SGuenter Roeck 	err = kstrtol(buf, 10, &val);
24219f2c059SGuenter Roeck 	if (err)
24319f2c059SGuenter Roeck 		return err;
2448d5d45fbSJean Delvare 
24546a2e71cSJean Delvare 	mutex_lock(&data->update_lock);
246a50d9a4dSGuenter Roeck 	data->temp[attr->index] = DS1621_TEMP_TO_REG(val, data->zbits);
2478269b75aSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, DS1621_REG_TEMP[attr->index],
24846a2e71cSJean Delvare 				     data->temp[attr->index]);
24946a2e71cSJean Delvare 	mutex_unlock(&data->update_lock);
25046a2e71cSJean Delvare 	return count;
2518d5d45fbSJean Delvare }
2528d5d45fbSJean Delvare 
alarms_show(struct device * dev,struct device_attribute * da,char * buf)253345d52faSJulia Lawall static ssize_t alarms_show(struct device *dev, struct device_attribute *da,
25446a2e71cSJean Delvare 			   char *buf)
2558d5d45fbSJean Delvare {
2568d5d45fbSJean Delvare 	struct ds1621_data *data = ds1621_update_client(dev);
2578d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
2588d5d45fbSJean Delvare }
2598d5d45fbSJean Delvare 
alarm_show(struct device * dev,struct device_attribute * da,char * buf)2600ad85c07SGuenter Roeck static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
26187f0f31bSJean Delvare 			  char *buf)
26287f0f31bSJean Delvare {
26387f0f31bSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
26487f0f31bSJean Delvare 	struct ds1621_data *data = ds1621_update_client(dev);
26587f0f31bSJean Delvare 	return sprintf(buf, "%d\n", !!(data->conf & attr->index));
26687f0f31bSJean Delvare }
26787f0f31bSJean Delvare 
update_interval_show(struct device * dev,struct device_attribute * da,char * buf)268345d52faSJulia Lawall static ssize_t update_interval_show(struct device *dev,
269345d52faSJulia Lawall 				    struct device_attribute *da, char *buf)
2703a8fe331SRobert Coulson {
2718269b75aSGuenter Roeck 	struct ds1621_data *data = dev_get_drvdata(dev);
2724e6104b1Sye xingchen 	return sysfs_emit(buf, "%hu\n", data->update_interval);
2733a8fe331SRobert Coulson }
2743a8fe331SRobert Coulson 
update_interval_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)275345d52faSJulia Lawall static ssize_t update_interval_store(struct device *dev,
276345d52faSJulia Lawall 				     struct device_attribute *da,
2773a8fe331SRobert Coulson 				     const char *buf, size_t count)
2783a8fe331SRobert Coulson {
2798269b75aSGuenter Roeck 	struct ds1621_data *data = dev_get_drvdata(dev);
2808269b75aSGuenter Roeck 	struct i2c_client *client = data->client;
2813a8fe331SRobert Coulson 	unsigned long convrate;
2823a8fe331SRobert Coulson 	s32 err;
2833a8fe331SRobert Coulson 	int resol = 0;
2843a8fe331SRobert Coulson 
2853a8fe331SRobert Coulson 	err = kstrtoul(buf, 10, &convrate);
2863a8fe331SRobert Coulson 	if (err)
2873a8fe331SRobert Coulson 		return err;
2883a8fe331SRobert Coulson 
2893a8fe331SRobert Coulson 	/* Convert rate into resolution bits */
2903a8fe331SRobert Coulson 	while (resol < (ARRAY_SIZE(ds1721_convrates) - 1) &&
2913a8fe331SRobert Coulson 	       convrate > ds1721_convrates[resol])
2923a8fe331SRobert Coulson 		resol++;
2933a8fe331SRobert Coulson 
2943a8fe331SRobert Coulson 	mutex_lock(&data->update_lock);
2953a8fe331SRobert Coulson 	data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
2963a8fe331SRobert Coulson 	data->conf &= ~DS1621_REG_CONFIG_RESOL;
2973a8fe331SRobert Coulson 	data->conf |= (resol << DS1621_REG_CONFIG_RESOL_SHIFT);
2983a8fe331SRobert Coulson 	i2c_smbus_write_byte_data(client, DS1621_REG_CONF, data->conf);
2993a8fe331SRobert Coulson 	data->update_interval = ds1721_convrates[resol];
30039c627a0SRobert Coulson 	data->zbits = 7 - resol;
3013a8fe331SRobert Coulson 	mutex_unlock(&data->update_lock);
3023a8fe331SRobert Coulson 
3033a8fe331SRobert Coulson 	return count;
3043a8fe331SRobert Coulson }
3053a8fe331SRobert Coulson 
306345d52faSJulia Lawall static DEVICE_ATTR_RO(alarms);
307345d52faSJulia Lawall static DEVICE_ATTR_RW(update_interval);
3083a8fe331SRobert Coulson 
3090ad85c07SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
3100ad85c07SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 1);
3110ad85c07SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
3120ad85c07SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, DS1621_ALARM_TEMP_LOW);
3130ad85c07SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, DS1621_ALARM_TEMP_HIGH);
3148d5d45fbSJean Delvare 
315a5ebe668SJean Delvare static struct attribute *ds1621_attributes[] = {
31646a2e71cSJean Delvare 	&sensor_dev_attr_temp1_input.dev_attr.attr,
31746a2e71cSJean Delvare 	&sensor_dev_attr_temp1_min.dev_attr.attr,
31846a2e71cSJean Delvare 	&sensor_dev_attr_temp1_max.dev_attr.attr,
31987f0f31bSJean Delvare 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
32087f0f31bSJean Delvare 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
321a5ebe668SJean Delvare 	&dev_attr_alarms.attr,
3223a8fe331SRobert Coulson 	&dev_attr_update_interval.attr,
323a5ebe668SJean Delvare 	NULL
324a5ebe668SJean Delvare };
325a5ebe668SJean Delvare 
ds1621_attribute_visible(struct kobject * kobj,struct attribute * attr,int index)3263a8fe331SRobert Coulson static umode_t ds1621_attribute_visible(struct kobject *kobj,
3273a8fe331SRobert Coulson 					struct attribute *attr, int index)
3283a8fe331SRobert Coulson {
329f3e3464eSTian Tao 	struct device *dev = kobj_to_dev(kobj);
3308269b75aSGuenter Roeck 	struct ds1621_data *data = dev_get_drvdata(dev);
3313a8fe331SRobert Coulson 
3323a8fe331SRobert Coulson 	if (attr == &dev_attr_update_interval.attr)
33379c1cc1cSRobert Coulson 		if (data->kind == ds1621 || data->kind == ds1625)
3343a8fe331SRobert Coulson 			/* shhh, we're hiding update_interval */
3353a8fe331SRobert Coulson 			return 0;
3363a8fe331SRobert Coulson 	return attr->mode;
3373a8fe331SRobert Coulson }
3383a8fe331SRobert Coulson 
339a5ebe668SJean Delvare static const struct attribute_group ds1621_group = {
340a5ebe668SJean Delvare 	.attrs = ds1621_attributes,
3413a8fe331SRobert Coulson 	.is_visible = ds1621_attribute_visible
342a5ebe668SJean Delvare };
3430d36dce0SGuenter Roeck __ATTRIBUTE_GROUPS(ds1621);
3448269b75aSGuenter Roeck 
ds1621_probe(struct i2c_client * client)34567487038SStephen Kitt static int ds1621_probe(struct i2c_client *client)
34670313eabSJean Delvare {
34770313eabSJean Delvare 	struct ds1621_data *data;
3488269b75aSGuenter Roeck 	struct device *hwmon_dev;
34970313eabSJean Delvare 
350e20b4b38SGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(struct ds1621_data),
351e20b4b38SGuenter Roeck 			    GFP_KERNEL);
352e20b4b38SGuenter Roeck 	if (!data)
353e20b4b38SGuenter Roeck 		return -ENOMEM;
35470313eabSJean Delvare 
3559a61bf63SIngo Molnar 	mutex_init(&data->update_lock);
3568d5d45fbSJean Delvare 
357*698d692cSAndrew Davis 	data->kind = (uintptr_t)i2c_get_match_data(client);
3588269b75aSGuenter Roeck 	data->client = client;
359cd6c8a42SRobert Coulson 
3608d5d45fbSJean Delvare 	/* Initialize the DS1621 chip */
3618269b75aSGuenter Roeck 	ds1621_init_client(data, client);
3628d5d45fbSJean Delvare 
3630d36dce0SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
3648269b75aSGuenter Roeck 							   client->name, data,
3658269b75aSGuenter Roeck 							   ds1621_groups);
36631ab1ad7SFengguang Wu 	return PTR_ERR_OR_ZERO(hwmon_dev);
3678d5d45fbSJean Delvare }
3688d5d45fbSJean Delvare 
3699202add6SJean Delvare static const struct i2c_device_id ds1621_id[] = {
370cd6c8a42SRobert Coulson 	{ "ds1621", ds1621 },
371cd6c8a42SRobert Coulson 	{ "ds1625", ds1625 },
37279c1cc1cSRobert Coulson 	{ "ds1631", ds1631 },
373cd6c8a42SRobert Coulson 	{ "ds1721", ds1721 },
374260f81ffSRobert Coulson 	{ "ds1731", ds1731 },
3759202add6SJean Delvare 	{ }
3769202add6SJean Delvare };
3779202add6SJean Delvare MODULE_DEVICE_TABLE(i2c, ds1621_id);
3788d5d45fbSJean Delvare 
3799202add6SJean Delvare /* This is the driver that will be inserted */
3809202add6SJean Delvare static struct i2c_driver ds1621_driver = {
3819202add6SJean Delvare 	.driver = {
3829202add6SJean Delvare 		.name	= "ds1621",
3839202add6SJean Delvare 	},
3841975d167SUwe Kleine-König 	.probe		= ds1621_probe,
3859202add6SJean Delvare 	.id_table	= ds1621_id,
3869202add6SJean Delvare };
3878d5d45fbSJean Delvare 
388f0967eeaSAxel Lin module_i2c_driver(ds1621_driver);
3898d5d45fbSJean Delvare 
3908d5d45fbSJean Delvare MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
3918d5d45fbSJean Delvare MODULE_DESCRIPTION("DS1621 driver");
3928d5d45fbSJean Delvare MODULE_LICENSE("GPL");
393