blob: 9a8d7ca9b389c895fd9aac3432991cc2b2d741e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
Oleksij Rempel9292f052012-10-10 15:25:56 +02004 * Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01005 * Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvare71062ff2008-01-06 15:27:59 +010030#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010032#include <linux/mutex.h>
Jean Delvarea5ebe662006-09-24 21:24:46 +020033#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050035static const unsigned short normal_i2c[] = {
36 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * The MAX1619 registers
40 */
41
42#define MAX1619_REG_R_MAN_ID 0xFE
43#define MAX1619_REG_R_CHIP_ID 0xFF
44#define MAX1619_REG_R_CONFIG 0x03
45#define MAX1619_REG_W_CONFIG 0x09
46#define MAX1619_REG_R_CONVRATE 0x04
47#define MAX1619_REG_W_CONVRATE 0x0A
48#define MAX1619_REG_R_STATUS 0x02
49#define MAX1619_REG_R_LOCAL_TEMP 0x00
50#define MAX1619_REG_R_REMOTE_TEMP 0x01
51#define MAX1619_REG_R_REMOTE_HIGH 0x07
52#define MAX1619_REG_W_REMOTE_HIGH 0x0D
53#define MAX1619_REG_R_REMOTE_LOW 0x08
54#define MAX1619_REG_W_REMOTE_LOW 0x0E
55#define MAX1619_REG_R_REMOTE_CRIT 0x10
56#define MAX1619_REG_W_REMOTE_CRIT 0x12
57#define MAX1619_REG_R_TCRIT_HYST 0x11
58#define MAX1619_REG_W_TCRIT_HYST 0x13
59
60/*
Andrew Mortona80e8ee2008-10-17 17:51:16 +020061 * Conversions
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
Andrew Mortona80e8ee2008-10-17 17:51:16 +020064static int temp_from_reg(int val)
65{
66 return (val & 0x80 ? val-0x100 : val) * 1000;
67}
68
69static int temp_to_reg(int val)
70{
71 return (val < 0 ? val+0x100*1000 : val) / 1000;
72}
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Functions declaration
76 */
77
Jean Delvarec6d3f6f2008-07-16 19:30:15 +020078static int max1619_probe(struct i2c_client *client,
79 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +010080static int max1619_detect(struct i2c_client *client,
Jean Delvarec6d3f6f2008-07-16 19:30:15 +020081 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static void max1619_init_client(struct i2c_client *client);
Jean Delvarec6d3f6f2008-07-16 19:30:15 +020083static int max1619_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static struct max1619_data *max1619_update_device(struct device *dev);
85
86/*
87 * Driver data (common to all clients)
88 */
89
Jean Delvarec6d3f6f2008-07-16 19:30:15 +020090static const struct i2c_device_id max1619_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +010091 { "max1619", 0 },
Jean Delvarec6d3f6f2008-07-16 19:30:15 +020092 { }
93};
94MODULE_DEVICE_TABLE(i2c, max1619_id);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct i2c_driver max1619_driver = {
Jean Delvarec6d3f6f2008-07-16 19:30:15 +020097 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +010098 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +010099 .name = "max1619",
100 },
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200101 .probe = max1619_probe,
102 .remove = max1619_remove,
103 .id_table = max1619_id,
104 .detect = max1619_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100105 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108/*
109 * Client data (each client gets its own)
110 */
111
112struct max1619_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700113 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100114 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 char valid; /* zero until following fields are valid */
116 unsigned long last_updated; /* in jiffies */
117
118 /* registers values */
119 u8 temp_input1; /* local */
120 u8 temp_input2, temp_low2, temp_high2; /* remote */
121 u8 temp_crit2;
122 u8 temp_hyst2;
Guenter Roeck8958dfb2012-01-14 21:29:27 -0800123 u8 alarms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126/*
127 * Sysfs stuff
128 */
129
130#define show_temp(value) \
Guenter Roeck8958dfb2012-01-14 21:29:27 -0800131static ssize_t show_##value(struct device *dev, struct device_attribute *attr, \
132 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{ \
134 struct max1619_data *data = max1619_update_device(dev); \
Andrew Mortona80e8ee2008-10-17 17:51:16 +0200135 return sprintf(buf, "%d\n", temp_from_reg(data->value)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137show_temp(temp_input1);
138show_temp(temp_input2);
139show_temp(temp_low2);
140show_temp(temp_high2);
141show_temp(temp_crit2);
142show_temp(temp_hyst2);
143
144#define set_temp2(value, reg) \
Guenter Roeck8958dfb2012-01-14 21:29:27 -0800145static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
146 const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 size_t count) \
148{ \
149 struct i2c_client *client = to_i2c_client(dev); \
150 struct max1619_data *data = i2c_get_clientdata(client); \
Guenter Roeck8958dfb2012-01-14 21:29:27 -0800151 long val; \
152 int err = kstrtol(buf, 10, &val); \
153 if (err) \
154 return err; \
155\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100156 mutex_lock(&data->update_lock); \
Andrew Mortona80e8ee2008-10-17 17:51:16 +0200157 data->value = temp_to_reg(val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 i2c_smbus_write_byte_data(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100159 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return count; \
161}
162
163set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
164set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
165set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
166set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
167
Guenter Roeck8958dfb2012-01-14 21:29:27 -0800168static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
169 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct max1619_data *data = max1619_update_device(dev);
172 return sprintf(buf, "%d\n", data->alarms);
173}
174
Jean Delvare71062ff2008-01-06 15:27:59 +0100175static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
176 char *buf)
177{
178 int bitnr = to_sensor_dev_attr(attr)->index;
179 struct max1619_data *data = max1619_update_device(dev);
180 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
184static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
185static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
186 set_temp_low2);
187static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
188 set_temp_high2);
189static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
190 set_temp_crit2);
191static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
192 set_temp_hyst2);
193static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Jean Delvare71062ff2008-01-06 15:27:59 +0100194static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
195static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
196static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
197static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jean Delvarea5ebe662006-09-24 21:24:46 +0200199static struct attribute *max1619_attributes[] = {
200 &dev_attr_temp1_input.attr,
201 &dev_attr_temp2_input.attr,
202 &dev_attr_temp2_min.attr,
203 &dev_attr_temp2_max.attr,
204 &dev_attr_temp2_crit.attr,
205 &dev_attr_temp2_crit_hyst.attr,
206
207 &dev_attr_alarms.attr,
Jean Delvare71062ff2008-01-06 15:27:59 +0100208 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
209 &sensor_dev_attr_temp2_fault.dev_attr.attr,
210 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
211 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200212 NULL
213};
214
215static const struct attribute_group max1619_group = {
216 .attrs = max1619_attributes,
217};
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * Real code
221 */
222
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200223/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100224static int max1619_detect(struct i2c_client *client,
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200225 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Jean Delvare52df6442009-12-09 20:35:57 +0100227 struct i2c_adapter *adapter = client->adapter;
228 u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
Andrew Mortonb0020e32005-11-07 01:01:49 -0800229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200231 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jean Delvare52df6442009-12-09 20:35:57 +0100233 /* detection */
234 reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
235 reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
236 reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
237 if ((reg_config & 0x03) != 0x00
238 || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
239 dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
240 client->addr);
241 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Jean Delvare52df6442009-12-09 20:35:57 +0100244 /* identification */
245 man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
246 chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
247 if (man_id != 0x4D || chip_id != 0x04) {
248 dev_info(&adapter->dev,
249 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
250 man_id, chip_id);
251 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200254 strlcpy(info->type, "max1619", I2C_NAME_SIZE);
Andrew Mortonb0020e32005-11-07 01:01:49 -0800255
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200256 return 0;
257}
258
259static int max1619_probe(struct i2c_client *new_client,
260 const struct i2c_device_id *id)
261{
262 struct max1619_data *data;
263 int err;
264
Guenter Roeck215690a2012-06-02 09:58:12 -0700265 data = devm_kzalloc(&new_client->dev, sizeof(struct max1619_data),
266 GFP_KERNEL);
267 if (!data)
268 return -ENOMEM;
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200269
270 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100271 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Initialize the MAX1619 chip */
274 max1619_init_client(new_client);
275
276 /* Register sysfs hooks */
Guenter Roeck8958dfb2012-01-14 21:29:27 -0800277 err = sysfs_create_group(&new_client->dev.kobj, &max1619_group);
278 if (err)
Guenter Roeck215690a2012-06-02 09:58:12 -0700279 return err;
Jean Delvarea5ebe662006-09-24 21:24:46 +0200280
Tony Jones1beeffe2007-08-20 13:46:20 -0700281 data->hwmon_dev = hwmon_device_register(&new_client->dev);
282 if (IS_ERR(data->hwmon_dev)) {
283 err = PTR_ERR(data->hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200284 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400285 }
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288
Jean Delvarea5ebe662006-09-24 21:24:46 +0200289exit_remove_files:
290 sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return err;
292}
293
294static void max1619_init_client(struct i2c_client *client)
295{
296 u8 config;
297
298 /*
299 * Start the conversions.
300 */
301 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
302 5); /* 2 Hz */
303 config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
304 if (config & 0x40)
305 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
306 config & 0xBF); /* run */
307}
308
Jean Delvarec6d3f6f2008-07-16 19:30:15 +0200309static int max1619_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400311 struct max1619_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Tony Jones1beeffe2007-08-20 13:46:20 -0700313 hwmon_device_unregister(data->hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200314 sysfs_remove_group(&client->dev.kobj, &max1619_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static struct max1619_data *max1619_update_device(struct device *dev)
320{
321 struct i2c_client *client = to_i2c_client(dev);
322 struct max1619_data *data = i2c_get_clientdata(client);
Guenter Roeck628c6d22014-04-19 09:15:38 -0700323 int config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100325 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
328 dev_dbg(&client->dev, "Updating max1619 data.\n");
329 data->temp_input1 = i2c_smbus_read_byte_data(client,
330 MAX1619_REG_R_LOCAL_TEMP);
331 data->temp_input2 = i2c_smbus_read_byte_data(client,
332 MAX1619_REG_R_REMOTE_TEMP);
333 data->temp_high2 = i2c_smbus_read_byte_data(client,
334 MAX1619_REG_R_REMOTE_HIGH);
335 data->temp_low2 = i2c_smbus_read_byte_data(client,
336 MAX1619_REG_R_REMOTE_LOW);
337 data->temp_crit2 = i2c_smbus_read_byte_data(client,
338 MAX1619_REG_R_REMOTE_CRIT);
339 data->temp_hyst2 = i2c_smbus_read_byte_data(client,
340 MAX1619_REG_R_TCRIT_HYST);
341 data->alarms = i2c_smbus_read_byte_data(client,
342 MAX1619_REG_R_STATUS);
Guenter Roeck628c6d22014-04-19 09:15:38 -0700343 /* If OVERT polarity is low, reverse alarm bit */
344 config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
345 if (!(config & 0x20))
346 data->alarms ^= 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 data->last_updated = jiffies;
349 data->valid = 1;
350 }
351
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100352 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 return data;
355}
356
Axel Linf0967ee2012-01-20 15:38:18 +0800357module_i2c_driver(max1619_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jean Delvare7c81c60f2014-01-29 20:40:08 +0100359MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360MODULE_DESCRIPTION("MAX1619 sensor driver");
361MODULE_LICENSE("GPL");