]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/max1619.c
hwmon: (max1619) Drop FSF address
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / max1619.c
CommitLineData
1da177e4
LT
1/*
2 * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
9292f055 4 * Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
7c81c60f 5 * Jean Delvare <jdelvare@suse.de>
1da177e4
LT
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.
1da177e4
LT
22 */
23
1da177e4
LT
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>
943b0830 29#include <linux/hwmon.h>
71062ffc 30#include <linux/hwmon-sysfs.h>
943b0830 31#include <linux/err.h>
9a61bf63 32#include <linux/mutex.h>
a5ebe668 33#include <linux/sysfs.h>
1da177e4 34
25e9c86d
MH
35static const unsigned short normal_i2c[] = {
36 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
1da177e4 37
1da177e4
LT
38/*
39 * 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/*
a80e8ee6 61 * Conversions
1da177e4
LT
62 */
63
a80e8ee6
AM
64static 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}
1da177e4
LT
73
74/*
75 * Functions declaration
76 */
77
c6d3f6fa
JD
78static int max1619_probe(struct i2c_client *client,
79 const struct i2c_device_id *id);
310ec792 80static int max1619_detect(struct i2c_client *client,
c6d3f6fa 81 struct i2c_board_info *info);
1da177e4 82static void max1619_init_client(struct i2c_client *client);
c6d3f6fa 83static int max1619_remove(struct i2c_client *client);
1da177e4
LT
84static struct max1619_data *max1619_update_device(struct device *dev);
85
86/*
87 * Driver data (common to all clients)
88 */
89
c6d3f6fa 90static const struct i2c_device_id max1619_id[] = {
1f86df49 91 { "max1619", 0 },
c6d3f6fa
JD
92 { }
93};
94MODULE_DEVICE_TABLE(i2c, max1619_id);
95
1da177e4 96static struct i2c_driver max1619_driver = {
c6d3f6fa 97 .class = I2C_CLASS_HWMON,
cdaf7934 98 .driver = {
cdaf7934
LR
99 .name = "max1619",
100 },
c6d3f6fa
JD
101 .probe = max1619_probe,
102 .remove = max1619_remove,
103 .id_table = max1619_id,
104 .detect = max1619_detect,
c3813d6a 105 .address_list = normal_i2c,
1da177e4
LT
106};
107
108/*
109 * Client data (each client gets its own)
110 */
111
112struct max1619_data {
1beeffe4 113 struct device *hwmon_dev;
9a61bf63 114 struct mutex update_lock;
1da177e4
LT
115 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;
8958dfb7 123 u8 alarms;
1da177e4
LT
124};
125
126/*
127 * Sysfs stuff
128 */
129
130#define show_temp(value) \
8958dfb7
GR
131static ssize_t show_##value(struct device *dev, struct device_attribute *attr, \
132 char *buf) \
1da177e4
LT
133{ \
134 struct max1619_data *data = max1619_update_device(dev); \
a80e8ee6 135 return sprintf(buf, "%d\n", temp_from_reg(data->value)); \
1da177e4
LT
136}
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) \
8958dfb7
GR
145static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
146 const char *buf, \
1da177e4
LT
147 size_t count) \
148{ \
149 struct i2c_client *client = to_i2c_client(dev); \
150 struct max1619_data *data = i2c_get_clientdata(client); \
8958dfb7
GR
151 long val; \
152 int err = kstrtol(buf, 10, &val); \
153 if (err) \
154 return err; \
155\
9a61bf63 156 mutex_lock(&data->update_lock); \
a80e8ee6 157 data->value = temp_to_reg(val); \
1da177e4 158 i2c_smbus_write_byte_data(client, reg, data->value); \
9a61bf63 159 mutex_unlock(&data->update_lock); \
1da177e4
LT
160 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
8958dfb7
GR
168static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
169 char *buf)
1da177e4
LT
170{
171 struct max1619_data *data = max1619_update_device(dev);
172 return sprintf(buf, "%d\n", data->alarms);
173}
174
71062ffc
JD
175static 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
1da177e4
LT
183static 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);
71062ffc
JD
194static 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);
1da177e4 198
a5ebe668
JD
199static 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,
71062ffc
JD
208 &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,
a5ebe668
JD
212 NULL
213};
214
215static const struct attribute_group max1619_group = {
216 .attrs = max1619_attributes,
217};
218
1da177e4
LT
219/*
220 * Real code
221 */
222
c6d3f6fa 223/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 224static int max1619_detect(struct i2c_client *client,
c6d3f6fa 225 struct i2c_board_info *info)
1da177e4 226{
52df6440
JD
227 struct i2c_adapter *adapter = client->adapter;
228 u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
b0020e3f 229
1da177e4 230 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
c6d3f6fa 231 return -ENODEV;
1da177e4 232
52df6440
JD
233 /* 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;
1da177e4
LT
242 }
243
52df6440
JD
244 /* 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;
b0020e3f 252 }
1da177e4 253
c6d3f6fa
JD
254 strlcpy(info->type, "max1619", I2C_NAME_SIZE);
255
256 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
215690a4
GR
265 data = devm_kzalloc(&new_client->dev, sizeof(struct max1619_data),
266 GFP_KERNEL);
267 if (!data)
268 return -ENOMEM;
1da177e4 269
c6d3f6fa 270 i2c_set_clientdata(new_client, data);
9a61bf63 271 mutex_init(&data->update_lock);
1da177e4 272
1da177e4
LT
273 /* Initialize the MAX1619 chip */
274 max1619_init_client(new_client);
275
276 /* Register sysfs hooks */
8958dfb7
GR
277 err = sysfs_create_group(&new_client->dev.kobj, &max1619_group);
278 if (err)
215690a4 279 return err;
a5ebe668 280
1beeffe4
TJ
281 data->hwmon_dev = hwmon_device_register(&new_client->dev);
282 if (IS_ERR(data->hwmon_dev)) {
283 err = PTR_ERR(data->hwmon_dev);
a5ebe668 284 goto exit_remove_files;
943b0830
MH
285 }
286
1da177e4
LT
287 return 0;
288
a5ebe668
JD
289exit_remove_files:
290 sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
1da177e4
LT
291 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
c6d3f6fa 309static int max1619_remove(struct i2c_client *client)
1da177e4 310{
943b0830 311 struct max1619_data *data = i2c_get_clientdata(client);
1da177e4 312
1beeffe4 313 hwmon_device_unregister(data->hwmon_dev);
a5ebe668 314 sysfs_remove_group(&client->dev.kobj, &max1619_group);
943b0830 315
1da177e4
LT
316 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);
628c6d27 323 int config;
1da177e4 324
9a61bf63 325 mutex_lock(&data->update_lock);
1da177e4
LT
326
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);
628c6d27
GR
343 /* 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;
1da177e4
LT
347
348 data->last_updated = jiffies;
349 data->valid = 1;
350 }
351
9a61bf63 352 mutex_unlock(&data->update_lock);
1da177e4
LT
353
354 return data;
355}
356
f0967eea 357module_i2c_driver(max1619_driver);
1da177e4 358
7c81c60f 359MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
1da177e4
LT
360MODULE_DESCRIPTION("MAX1619 sensor driver");
361MODULE_LICENSE("GPL");