xref: /linux/drivers/hwmon/ds1621.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2     ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Christian W. Zuckschwerdt  <zany@triq.net>  2000-11-23
5     based on lm75.c by Frodo Looijaard <frodol@dds.nl>
6     Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
7     the help of Jean Delvare <khali@linux-fr.org>
8 
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13 
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/err.h>
31 #include <linux/mutex.h>
32 #include "lm75.h"
33 
34 /* Addresses to scan */
35 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
36 					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
37 
38 /* Insmod parameters */
39 I2C_CLIENT_INSMOD_1(ds1621);
40 static int polarity = -1;
41 module_param(polarity, int, 0);
42 MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
43 
44 /* Many DS1621 constants specified below */
45 /* Config register used for detection         */
46 /*  7    6    5    4    3    2    1    0      */
47 /* |Done|THF |TLF |NVB | X  | X  |POL |1SHOT| */
48 #define DS1621_REG_CONFIG_NVB		0x10
49 #define DS1621_REG_CONFIG_POLARITY	0x02
50 #define DS1621_REG_CONFIG_1SHOT		0x01
51 #define DS1621_REG_CONFIG_DONE		0x80
52 
53 /* The DS1621 registers */
54 #define DS1621_REG_TEMP			0xAA /* word, RO */
55 #define DS1621_REG_TEMP_MIN		0xA1 /* word, RW */
56 #define DS1621_REG_TEMP_MAX		0xA2 /* word, RW */
57 #define DS1621_REG_CONF			0xAC /* byte, RW */
58 #define DS1621_COM_START		0xEE /* no data */
59 #define DS1621_COM_STOP			0x22 /* no data */
60 
61 /* The DS1621 configuration register */
62 #define DS1621_ALARM_TEMP_HIGH		0x40
63 #define DS1621_ALARM_TEMP_LOW		0x20
64 
65 /* Conversions. Rounding and limit checking is only done on the TO_REG
66    variants. Note that you should be a bit careful with which arguments
67    these macros are called: arguments may be evaluated more than once.
68    Fixing this is just not worth it. */
69 #define ALARMS_FROM_REG(val) ((val) & \
70                               (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
71 
72 /* Each client has this additional data */
73 struct ds1621_data {
74 	struct i2c_client client;
75 	struct class_device *class_dev;
76 	struct mutex update_lock;
77 	char valid;			/* !=0 if following fields are valid */
78 	unsigned long last_updated;	/* In jiffies */
79 
80 	u16 temp, temp_min, temp_max;	/* Register values, word */
81 	u8 conf;			/* Register encoding, combined */
82 };
83 
84 static int ds1621_attach_adapter(struct i2c_adapter *adapter);
85 static int ds1621_detect(struct i2c_adapter *adapter, int address,
86 			 int kind);
87 static void ds1621_init_client(struct i2c_client *client);
88 static int ds1621_detach_client(struct i2c_client *client);
89 static struct ds1621_data *ds1621_update_client(struct device *dev);
90 
91 /* This is the driver that will be inserted */
92 static struct i2c_driver ds1621_driver = {
93 	.driver = {
94 		.name	= "ds1621",
95 	},
96 	.id		= I2C_DRIVERID_DS1621,
97 	.attach_adapter	= ds1621_attach_adapter,
98 	.detach_client	= ds1621_detach_client,
99 };
100 
101 /* All registers are word-sized, except for the configuration register.
102    DS1621 uses a high-byte first convention, which is exactly opposite to
103    the usual practice. */
104 static int ds1621_read_value(struct i2c_client *client, u8 reg)
105 {
106 	if (reg == DS1621_REG_CONF)
107 		return i2c_smbus_read_byte_data(client, reg);
108 	else
109 		return swab16(i2c_smbus_read_word_data(client, reg));
110 }
111 
112 /* All registers are word-sized, except for the configuration register.
113    DS1621 uses a high-byte first convention, which is exactly opposite to
114    the usual practice. */
115 static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
116 {
117 	if (reg == DS1621_REG_CONF)
118 		return i2c_smbus_write_byte_data(client, reg, value);
119 	else
120 		return i2c_smbus_write_word_data(client, reg, swab16(value));
121 }
122 
123 static void ds1621_init_client(struct i2c_client *client)
124 {
125 	int reg = ds1621_read_value(client, DS1621_REG_CONF);
126 	/* switch to continuous conversion mode */
127 	reg &= ~ DS1621_REG_CONFIG_1SHOT;
128 
129 	/* setup output polarity */
130 	if (polarity == 0)
131 		reg &= ~DS1621_REG_CONFIG_POLARITY;
132 	else if (polarity == 1)
133 		reg |= DS1621_REG_CONFIG_POLARITY;
134 
135 	ds1621_write_value(client, DS1621_REG_CONF, reg);
136 
137 	/* start conversion */
138 	i2c_smbus_write_byte(client, DS1621_COM_START);
139 }
140 
141 #define show(value)							\
142 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)		\
143 {									\
144 	struct ds1621_data *data = ds1621_update_client(dev);		\
145 	return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value));	\
146 }
147 
148 show(temp);
149 show(temp_min);
150 show(temp_max);
151 
152 #define set_temp(suffix, value, reg)					\
153 static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf,	\
154 				 size_t count)				\
155 {									\
156 	struct i2c_client *client = to_i2c_client(dev);			\
157 	struct ds1621_data *data = ds1621_update_client(dev);		\
158 	u16 val = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10));	\
159 									\
160 	mutex_lock(&data->update_lock);					\
161 	data->value = val;						\
162 	ds1621_write_value(client, reg, data->value);			\
163 	mutex_unlock(&data->update_lock);				\
164 	return count;							\
165 }
166 
167 set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
168 set_temp(max, temp_max, DS1621_REG_TEMP_MAX);
169 
170 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
171 {
172 	struct ds1621_data *data = ds1621_update_client(dev);
173 	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
174 }
175 
176 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
177 static DEVICE_ATTR(temp1_input, S_IRUGO , show_temp, NULL);
178 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO , show_temp_min, set_temp_min);
179 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
180 
181 
182 static int ds1621_attach_adapter(struct i2c_adapter *adapter)
183 {
184 	if (!(adapter->class & I2C_CLASS_HWMON))
185 		return 0;
186 	return i2c_probe(adapter, &addr_data, ds1621_detect);
187 }
188 
189 /* This function is called by i2c_probe */
190 static int ds1621_detect(struct i2c_adapter *adapter, int address,
191 			 int kind)
192 {
193 	int conf, temp;
194 	struct i2c_client *new_client;
195 	struct ds1621_data *data;
196 	int err = 0;
197 
198 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
199 				     | I2C_FUNC_SMBUS_WORD_DATA
200 				     | I2C_FUNC_SMBUS_WRITE_BYTE))
201 		goto exit;
202 
203 	/* OK. For now, we presume we have a valid client. We now create the
204 	   client structure, even though we cannot fill it completely yet.
205 	   But it allows us to access ds1621_{read,write}_value. */
206 	if (!(data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
207 		err = -ENOMEM;
208 		goto exit;
209 	}
210 
211 	new_client = &data->client;
212 	i2c_set_clientdata(new_client, data);
213 	new_client->addr = address;
214 	new_client->adapter = adapter;
215 	new_client->driver = &ds1621_driver;
216 	new_client->flags = 0;
217 
218 
219 	/* Now, we do the remaining detection. It is lousy. */
220 	if (kind < 0) {
221 		/* The NVB bit should be low if no EEPROM write has been
222 		   requested during the latest 10ms, which is highly
223 		   improbable in our case. */
224 		conf = ds1621_read_value(new_client, DS1621_REG_CONF);
225 		if (conf & DS1621_REG_CONFIG_NVB)
226 			goto exit_free;
227 		/* The 7 lowest bits of a temperature should always be 0. */
228 		temp = ds1621_read_value(new_client, DS1621_REG_TEMP);
229 		if (temp & 0x007f)
230 			goto exit_free;
231 		temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MIN);
232 		if (temp & 0x007f)
233 			goto exit_free;
234 		temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MAX);
235 		if (temp & 0x007f)
236 			goto exit_free;
237 	}
238 
239 	/* Determine the chip type - only one kind supported! */
240 	if (kind <= 0)
241 		kind = ds1621;
242 
243 	/* Fill in remaining client fields and put it into the global list */
244 	strlcpy(new_client->name, "ds1621", I2C_NAME_SIZE);
245 	data->valid = 0;
246 	mutex_init(&data->update_lock);
247 
248 	/* Tell the I2C layer a new client has arrived */
249 	if ((err = i2c_attach_client(new_client)))
250 		goto exit_free;
251 
252 	/* Initialize the DS1621 chip */
253 	ds1621_init_client(new_client);
254 
255 	/* Register sysfs hooks */
256 	data->class_dev = hwmon_device_register(&new_client->dev);
257 	if (IS_ERR(data->class_dev)) {
258 		err = PTR_ERR(data->class_dev);
259 		goto exit_detach;
260 	}
261 
262 	device_create_file(&new_client->dev, &dev_attr_alarms);
263 	device_create_file(&new_client->dev, &dev_attr_temp1_input);
264 	device_create_file(&new_client->dev, &dev_attr_temp1_min);
265 	device_create_file(&new_client->dev, &dev_attr_temp1_max);
266 
267 	return 0;
268 
269 /* OK, this is not exactly good programming practice, usually. But it is
270    very code-efficient in this case. */
271       exit_detach:
272 	i2c_detach_client(new_client);
273       exit_free:
274 	kfree(data);
275       exit:
276 	return err;
277 }
278 
279 static int ds1621_detach_client(struct i2c_client *client)
280 {
281 	struct ds1621_data *data = i2c_get_clientdata(client);
282 	int err;
283 
284 	hwmon_device_unregister(data->class_dev);
285 
286 	if ((err = i2c_detach_client(client)))
287 		return err;
288 
289 	kfree(data);
290 
291 	return 0;
292 }
293 
294 
295 static struct ds1621_data *ds1621_update_client(struct device *dev)
296 {
297 	struct i2c_client *client = to_i2c_client(dev);
298 	struct ds1621_data *data = i2c_get_clientdata(client);
299 	u8 new_conf;
300 
301 	mutex_lock(&data->update_lock);
302 
303 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
304 	    || !data->valid) {
305 
306 		dev_dbg(&client->dev, "Starting ds1621 update\n");
307 
308 		data->conf = ds1621_read_value(client, DS1621_REG_CONF);
309 
310 		data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
311 
312 		data->temp_min = ds1621_read_value(client,
313 		                                    DS1621_REG_TEMP_MIN);
314 		data->temp_max = ds1621_read_value(client,
315 						    DS1621_REG_TEMP_MAX);
316 
317 		/* reset alarms if necessary */
318 		new_conf = data->conf;
319 		if (data->temp < data->temp_min)
320 			new_conf &= ~DS1621_ALARM_TEMP_LOW;
321 		if (data->temp > data->temp_max)
322 			new_conf &= ~DS1621_ALARM_TEMP_HIGH;
323 		if (data->conf != new_conf)
324 			ds1621_write_value(client, DS1621_REG_CONF,
325 					   new_conf);
326 
327 		data->last_updated = jiffies;
328 		data->valid = 1;
329 	}
330 
331 	mutex_unlock(&data->update_lock);
332 
333 	return data;
334 }
335 
336 static int __init ds1621_init(void)
337 {
338 	return i2c_add_driver(&ds1621_driver);
339 }
340 
341 static void __exit ds1621_exit(void)
342 {
343 	i2c_del_driver(&ds1621_driver);
344 }
345 
346 
347 MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
348 MODULE_DESCRIPTION("DS1621 driver");
349 MODULE_LICENSE("GPL");
350 
351 module_init(ds1621_init);
352 module_exit(ds1621_exit);
353