1/*
2 * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
3 *             monitoring
4 * Copyright (C) 2003-2004 Alexey Fisher <fishor@mail.ru>
5 *                         Jean Delvare <khali@linux-fr.org>
6 *
7 * Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
8 * It reports up to two temperatures (its own plus up to
9 * one external one). Complete datasheet can be
10 * obtained from Maxim's website at:
11 *   http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
34#include <linux/hwmon.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
37#include <linux/sysfs.h>
38
39static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
40					0x29, 0x2a, 0x2b,
41					0x4c, 0x4d, 0x4e,
42					I2C_CLIENT_END };
43
44/*
45 * Insmod parameters
46 */
47
48I2C_CLIENT_INSMOD_1(max1619);
49
50/*
51 * The MAX1619 registers
52 */
53
54#define MAX1619_REG_R_MAN_ID		0xFE
55#define MAX1619_REG_R_CHIP_ID		0xFF
56#define MAX1619_REG_R_CONFIG		0x03
57#define MAX1619_REG_W_CONFIG		0x09
58#define MAX1619_REG_R_CONVRATE		0x04
59#define MAX1619_REG_W_CONVRATE		0x0A
60#define MAX1619_REG_R_STATUS		0x02
61#define MAX1619_REG_R_LOCAL_TEMP	0x00
62#define MAX1619_REG_R_REMOTE_TEMP	0x01
63#define MAX1619_REG_R_REMOTE_HIGH	0x07
64#define MAX1619_REG_W_REMOTE_HIGH	0x0D
65#define MAX1619_REG_R_REMOTE_LOW	0x08
66#define MAX1619_REG_W_REMOTE_LOW	0x0E
67#define MAX1619_REG_R_REMOTE_CRIT	0x10
68#define MAX1619_REG_W_REMOTE_CRIT	0x12
69#define MAX1619_REG_R_TCRIT_HYST	0x11
70#define MAX1619_REG_W_TCRIT_HYST	0x13
71
72/*
73 * Conversions and various macros
74 */
75
76#define TEMP_FROM_REG(val)	((val & 0x80 ? val-0x100 : val) * 1000)
77#define TEMP_TO_REG(val)	((val < 0 ? val+0x100*1000 : val) / 1000)
78
79/*
80 * Functions declaration
81 */
82
83static int max1619_attach_adapter(struct i2c_adapter *adapter);
84static int max1619_detect(struct i2c_adapter *adapter, int address,
85	int kind);
86static void max1619_init_client(struct i2c_client *client);
87static int max1619_detach_client(struct i2c_client *client);
88static struct max1619_data *max1619_update_device(struct device *dev);
89
90/*
91 * Driver data (common to all clients)
92 */
93
94static struct i2c_driver max1619_driver = {
95	.driver = {
96		.name	= "max1619",
97	},
98	.attach_adapter	= max1619_attach_adapter,
99	.detach_client	= max1619_detach_client,
100};
101
102/*
103 * Client data (each client gets its own)
104 */
105
106struct max1619_data {
107	struct i2c_client client;
108	struct class_device *class_dev;
109	struct mutex update_lock;
110	char valid; /* zero until following fields are valid */
111	unsigned long last_updated; /* in jiffies */
112
113	/* registers values */
114	u8 temp_input1; /* local */
115	u8 temp_input2, temp_low2, temp_high2; /* remote */
116	u8 temp_crit2;
117	u8 temp_hyst2;
118	u8 alarms;
119};
120
121/*
122 * Sysfs stuff
123 */
124
125#define show_temp(value) \
126static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
127{ \
128	struct max1619_data *data = max1619_update_device(dev); \
129	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
130}
131show_temp(temp_input1);
132show_temp(temp_input2);
133show_temp(temp_low2);
134show_temp(temp_high2);
135show_temp(temp_crit2);
136show_temp(temp_hyst2);
137
138#define set_temp2(value, reg) \
139static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
140	size_t count) \
141{ \
142	struct i2c_client *client = to_i2c_client(dev); \
143	struct max1619_data *data = i2c_get_clientdata(client); \
144	long val = simple_strtol(buf, NULL, 10); \
145 \
146	mutex_lock(&data->update_lock); \
147	data->value = TEMP_TO_REG(val); \
148	i2c_smbus_write_byte_data(client, reg, data->value); \
149	mutex_unlock(&data->update_lock); \
150	return count; \
151}
152
153set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
154set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
155set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
156set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
157
158static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
159{
160	struct max1619_data *data = max1619_update_device(dev);
161	return sprintf(buf, "%d\n", data->alarms);
162}
163
164static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
165static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
166static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
167	set_temp_low2);
168static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
169	set_temp_high2);
170static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
171	set_temp_crit2);
172static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
173	set_temp_hyst2);
174static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
175
176static struct attribute *max1619_attributes[] = {
177	&dev_attr_temp1_input.attr,
178	&dev_attr_temp2_input.attr,
179	&dev_attr_temp2_min.attr,
180	&dev_attr_temp2_max.attr,
181	&dev_attr_temp2_crit.attr,
182	&dev_attr_temp2_crit_hyst.attr,
183
184	&dev_attr_alarms.attr,
185	NULL
186};
187
188static const struct attribute_group max1619_group = {
189	.attrs = max1619_attributes,
190};
191
192/*
193 * Real code
194 */
195
196static int max1619_attach_adapter(struct i2c_adapter *adapter)
197{
198	if (!(adapter->class & I2C_CLASS_HWMON))
199		return 0;
200	return i2c_probe(adapter, &addr_data, max1619_detect);
201}
202
203/*
204 * The following function does more than just detection. If detection
205 * succeeds, it also registers the new chip.
206 */
207static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
208{
209	struct i2c_client *new_client;
210	struct max1619_data *data;
211	int err = 0;
212	const char *name = "";
213	u8 reg_config=0, reg_convrate=0, reg_status=0;
214
215	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
216		goto exit;
217
218	if (!(data = kzalloc(sizeof(struct max1619_data), GFP_KERNEL))) {
219		err = -ENOMEM;
220		goto exit;
221	}
222
223	/* The common I2C client data is placed right before the
224	   MAX1619-specific data. */
225	new_client = &data->client;
226	i2c_set_clientdata(new_client, data);
227	new_client->addr = address;
228	new_client->adapter = adapter;
229	new_client->driver = &max1619_driver;
230	new_client->flags = 0;
231
232	/*
233	 * Now we do the remaining detection. A negative kind means that
234	 * the driver was loaded with no force parameter (default), so we
235	 * must both detect and identify the chip. A zero kind means that
236	 * the driver was loaded with the force parameter, the detection
237	 * step shall be skipped. A positive kind means that the driver
238	 * was loaded with the force parameter and a given kind of chip is
239	 * requested, so both the detection and the identification steps
240	 * are skipped.
241	 */
242	if (kind < 0) { /* detection */
243		reg_config = i2c_smbus_read_byte_data(new_client,
244			      MAX1619_REG_R_CONFIG);
245		reg_convrate = i2c_smbus_read_byte_data(new_client,
246			       MAX1619_REG_R_CONVRATE);
247		reg_status = i2c_smbus_read_byte_data(new_client,
248				MAX1619_REG_R_STATUS);
249		if ((reg_config & 0x03) != 0x00
250		 || reg_convrate > 0x07 || (reg_status & 0x61 ) !=0x00) {
251			dev_dbg(&adapter->dev,
252				"MAX1619 detection failed at 0x%02x.\n",
253				address);
254			goto exit_free;
255		}
256	}
257
258	if (kind <= 0) { /* identification */
259		u8 man_id, chip_id;
260
261		man_id = i2c_smbus_read_byte_data(new_client,
262			 MAX1619_REG_R_MAN_ID);
263		chip_id = i2c_smbus_read_byte_data(new_client,
264			  MAX1619_REG_R_CHIP_ID);
265
266		if ((man_id == 0x4D) && (chip_id == 0x04))
267			kind = max1619;
268
269		if (kind <= 0) { /* identification failed */
270			dev_info(&adapter->dev,
271			    "Unsupported chip (man_id=0x%02X, "
272			    "chip_id=0x%02X).\n", man_id, chip_id);
273			goto exit_free;
274		}
275	}
276
277	if (kind == max1619)
278		name = "max1619";
279
280	/* We can fill in the remaining client fields */
281	strlcpy(new_client->name, name, I2C_NAME_SIZE);
282	data->valid = 0;
283	mutex_init(&data->update_lock);
284
285	/* Tell the I2C layer a new client has arrived */
286	if ((err = i2c_attach_client(new_client)))
287		goto exit_free;
288
289	/* Initialize the MAX1619 chip */
290	max1619_init_client(new_client);
291
292	/* Register sysfs hooks */
293	if ((err = sysfs_create_group(&new_client->dev.kobj, &max1619_group)))
294		goto exit_detach;
295
296	data->class_dev = hwmon_device_register(&new_client->dev);
297	if (IS_ERR(data->class_dev)) {
298		err = PTR_ERR(data->class_dev);
299		goto exit_remove_files;
300	}
301
302	return 0;
303
304exit_remove_files:
305	sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
306exit_detach:
307	i2c_detach_client(new_client);
308exit_free:
309	kfree(data);
310exit:
311	return err;
312}
313
314static void max1619_init_client(struct i2c_client *client)
315{
316	u8 config;
317
318	/*
319	 * Start the conversions.
320	 */
321	i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
322				  5); /* 2 Hz */
323	config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
324	if (config & 0x40)
325		i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
326					  config & 0xBF); /* run */
327}
328
329static int max1619_detach_client(struct i2c_client *client)
330{
331	struct max1619_data *data = i2c_get_clientdata(client);
332	int err;
333
334	hwmon_device_unregister(data->class_dev);
335	sysfs_remove_group(&client->dev.kobj, &max1619_group);
336
337	if ((err = i2c_detach_client(client)))
338		return err;
339
340	kfree(data);
341	return 0;
342}
343
344static struct max1619_data *max1619_update_device(struct device *dev)
345{
346	struct i2c_client *client = to_i2c_client(dev);
347	struct max1619_data *data = i2c_get_clientdata(client);
348
349	mutex_lock(&data->update_lock);
350
351	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
352		dev_dbg(&client->dev, "Updating max1619 data.\n");
353		data->temp_input1 = i2c_smbus_read_byte_data(client,
354					MAX1619_REG_R_LOCAL_TEMP);
355		data->temp_input2 = i2c_smbus_read_byte_data(client,
356					MAX1619_REG_R_REMOTE_TEMP);
357		data->temp_high2 = i2c_smbus_read_byte_data(client,
358					MAX1619_REG_R_REMOTE_HIGH);
359		data->temp_low2 = i2c_smbus_read_byte_data(client,
360					MAX1619_REG_R_REMOTE_LOW);
361		data->temp_crit2 = i2c_smbus_read_byte_data(client,
362					MAX1619_REG_R_REMOTE_CRIT);
363		data->temp_hyst2 = i2c_smbus_read_byte_data(client,
364					MAX1619_REG_R_TCRIT_HYST);
365		data->alarms = i2c_smbus_read_byte_data(client,
366					MAX1619_REG_R_STATUS);
367
368		data->last_updated = jiffies;
369		data->valid = 1;
370	}
371
372	mutex_unlock(&data->update_lock);
373
374	return data;
375}
376
377static int __init sensors_max1619_init(void)
378{
379	return i2c_add_driver(&max1619_driver);
380}
381
382static void __exit sensors_max1619_exit(void)
383{
384	i2c_del_driver(&max1619_driver);
385}
386
387MODULE_AUTHOR("Alexey Fisher <fishor@mail.ru> and "
388	"Jean Delvare <khali@linux-fr.org>");
389MODULE_DESCRIPTION("MAX1619 sensor driver");
390MODULE_LICENSE("GPL");
391
392module_init(sensors_max1619_init);
393module_exit(sensors_max1619_exit);
394