blob: a483d96e4cefb5379568d11db1ea3076e06cb375 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/i2c-sensor.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040028#include <linux/hwmon.h>
29#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31
32/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
34 0x29, 0x2a, 0x2b,
35 0x4c, 0x4d, 0x4e,
36 I2C_CLIENT_END };
37static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
38
39/* Insmod parameters */
40SENSORS_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066);
41
42/* adm1021 constants specified below */
43
44/* The adm1021 registers */
45/* Read-only */
46#define ADM1021_REG_TEMP 0x00
47#define ADM1021_REG_REMOTE_TEMP 0x01
48#define ADM1021_REG_STATUS 0x02
49#define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
50#define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */
51#define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */
52/* These use different addresses for reading/writing */
53#define ADM1021_REG_CONFIG_R 0x03
54#define ADM1021_REG_CONFIG_W 0x09
55#define ADM1021_REG_CONV_RATE_R 0x04
56#define ADM1021_REG_CONV_RATE_W 0x0A
57/* These are for the ADM1023's additional precision on the remote temp sensor */
58#define ADM1021_REG_REM_TEMP_PREC 0x010
59#define ADM1021_REG_REM_OFFSET 0x011
60#define ADM1021_REG_REM_OFFSET_PREC 0x012
61#define ADM1021_REG_REM_TOS_PREC 0x013
62#define ADM1021_REG_REM_THYST_PREC 0x014
63/* limits */
64#define ADM1021_REG_TOS_R 0x05
65#define ADM1021_REG_TOS_W 0x0B
66#define ADM1021_REG_REMOTE_TOS_R 0x07
67#define ADM1021_REG_REMOTE_TOS_W 0x0D
68#define ADM1021_REG_THYST_R 0x06
69#define ADM1021_REG_THYST_W 0x0C
70#define ADM1021_REG_REMOTE_THYST_R 0x08
71#define ADM1021_REG_REMOTE_THYST_W 0x0E
72/* write-only */
73#define ADM1021_REG_ONESHOT 0x0F
74
75
76/* Conversions. Rounding and limit checking is only done on the TO_REG
77 variants. Note that you should be a bit careful with which arguments
78 these macros are called: arguments may be evaluated more than once.
79 Fixing this is just not worth it. */
80/* Conversions note: 1021 uses normal integer signed-byte format*/
81#define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
82#define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
83
84/* Initial values */
85
86/* Note: Even though I left the low and high limits named os and hyst,
87they don't quite work like a thermostat the way the LM75 does. I.e.,
88a lower temp than THYST actually triggers an alarm instead of
89clearing it. Weird, ey? --Phil */
90
91/* Each client has this additional data */
92struct adm1021_data {
93 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040094 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 enum chips type;
96
97 struct semaphore update_lock;
98 char valid; /* !=0 if following fields are valid */
99 unsigned long last_updated; /* In jiffies */
100
101 u8 temp_max; /* Register values */
102 u8 temp_hyst;
103 u8 temp_input;
104 u8 remote_temp_max;
105 u8 remote_temp_hyst;
106 u8 remote_temp_input;
107 u8 alarms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* Special values for ADM1023 only */
109 u8 remote_temp_prec;
110 u8 remote_temp_os_prec;
111 u8 remote_temp_hyst_prec;
112 u8 remote_temp_offset;
113 u8 remote_temp_offset_prec;
114};
115
116static int adm1021_attach_adapter(struct i2c_adapter *adapter);
117static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
118static void adm1021_init_client(struct i2c_client *client);
119static int adm1021_detach_client(struct i2c_client *client);
120static int adm1021_read_value(struct i2c_client *client, u8 reg);
121static int adm1021_write_value(struct i2c_client *client, u8 reg,
122 u16 value);
123static struct adm1021_data *adm1021_update_device(struct device *dev);
124
125/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
126static int read_only = 0;
127
128
129/* This is the driver that will be inserted */
130static struct i2c_driver adm1021_driver = {
131 .owner = THIS_MODULE,
132 .name = "adm1021",
133 .id = I2C_DRIVERID_ADM1021,
134 .flags = I2C_DF_NOTIFY,
135 .attach_adapter = adm1021_attach_adapter,
136 .detach_client = adm1021_detach_client,
137};
138
139#define show(value) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400140static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{ \
142 struct adm1021_data *data = adm1021_update_device(dev); \
143 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
144}
145show(temp_max);
146show(temp_hyst);
147show(temp_input);
148show(remote_temp_max);
149show(remote_temp_hyst);
150show(remote_temp_input);
151
152#define show2(value) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400153static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{ \
155 struct adm1021_data *data = adm1021_update_device(dev); \
156 return sprintf(buf, "%d\n", data->value); \
157}
158show2(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#define set(value, reg) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400161static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{ \
163 struct i2c_client *client = to_i2c_client(dev); \
164 struct adm1021_data *data = i2c_get_clientdata(client); \
165 int temp = simple_strtoul(buf, NULL, 10); \
166 \
167 down(&data->update_lock); \
168 data->value = TEMP_TO_REG(temp); \
169 adm1021_write_value(client, reg, data->value); \
170 up(&data->update_lock); \
171 return count; \
172}
173set(temp_max, ADM1021_REG_TOS_W);
174set(temp_hyst, ADM1021_REG_THYST_W);
175set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
176set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
177
178static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
179static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
180static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
181static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
182static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
183static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
184static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186
187static int adm1021_attach_adapter(struct i2c_adapter *adapter)
188{
189 if (!(adapter->class & I2C_CLASS_HWMON))
190 return 0;
191 return i2c_detect(adapter, &addr_data, adm1021_detect);
192}
193
194static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
195{
196 int i;
197 struct i2c_client *new_client;
198 struct adm1021_data *data;
199 int err = 0;
200 const char *type_name = "";
201
202 /* Make sure we aren't probing the ISA bus!! This is just a safety check
203 at this moment; i2c_detect really won't call us. */
204#ifdef DEBUG
205 if (i2c_is_isa_adapter(adapter)) {
206 dev_dbg(&adapter->dev, "adm1021_detect called for an ISA bus adapter?!?\n");
207 return 0;
208 }
209#endif
210
211 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
212 goto error0;
213
214 /* OK. For now, we presume we have a valid client. We now create the
215 client structure, even though we cannot fill it completely yet.
216 But it allows us to access adm1021_{read,write}_value. */
217
218 if (!(data = kmalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
219 err = -ENOMEM;
220 goto error0;
221 }
222 memset(data, 0, sizeof(struct adm1021_data));
223
224 new_client = &data->client;
225 i2c_set_clientdata(new_client, data);
226 new_client->addr = address;
227 new_client->adapter = adapter;
228 new_client->driver = &adm1021_driver;
229 new_client->flags = 0;
230
231 /* Now, we do the remaining detection. */
232 if (kind < 0) {
233 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
234 || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
235 || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
236 err = -ENODEV;
237 goto error1;
238 }
239 }
240
241 /* Determine the chip type. */
242 if (kind <= 0) {
243 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
244 if (i == 0x41)
245 if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
246 kind = adm1023;
247 else
248 kind = adm1021;
249 else if (i == 0x49)
250 kind = thmc10;
251 else if (i == 0x23)
252 kind = gl523sm;
253 else if ((i == 0x4d) &&
254 (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
255 kind = max1617a;
256 else if (i == 0x54)
257 kind = mc1066;
258 /* LM84 Mfr ID in a different place, and it has more unused bits */
259 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
260 && (kind == 0 /* skip extra detection */
261 || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
262 && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
263 kind = lm84;
264 else
265 kind = max1617;
266 }
267
268 if (kind == max1617) {
269 type_name = "max1617";
270 } else if (kind == max1617a) {
271 type_name = "max1617a";
272 } else if (kind == adm1021) {
273 type_name = "adm1021";
274 } else if (kind == adm1023) {
275 type_name = "adm1023";
276 } else if (kind == thmc10) {
277 type_name = "thmc10";
278 } else if (kind == lm84) {
279 type_name = "lm84";
280 } else if (kind == gl523sm) {
281 type_name = "gl523sm";
282 } else if (kind == mc1066) {
283 type_name = "mc1066";
284 }
285
286 /* Fill in the remaining client fields and put it into the global list */
287 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
288 data->type = kind;
289 data->valid = 0;
290 init_MUTEX(&data->update_lock);
291
292 /* Tell the I2C layer a new client has arrived */
293 if ((err = i2c_attach_client(new_client)))
294 goto error1;
295
296 /* Initialize the ADM1021 chip */
297 if (kind != lm84)
298 adm1021_init_client(new_client);
299
300 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400301 data->class_dev = hwmon_device_register(&new_client->dev);
302 if (IS_ERR(data->class_dev)) {
303 err = PTR_ERR(data->class_dev);
304 goto error2;
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 device_create_file(&new_client->dev, &dev_attr_temp1_max);
308 device_create_file(&new_client->dev, &dev_attr_temp1_min);
309 device_create_file(&new_client->dev, &dev_attr_temp1_input);
310 device_create_file(&new_client->dev, &dev_attr_temp2_max);
311 device_create_file(&new_client->dev, &dev_attr_temp2_min);
312 device_create_file(&new_client->dev, &dev_attr_temp2_input);
313 device_create_file(&new_client->dev, &dev_attr_alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 return 0;
316
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400317error2:
318 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319error1:
320 kfree(data);
321error0:
322 return err;
323}
324
325static void adm1021_init_client(struct i2c_client *client)
326{
327 /* Enable ADC and disable suspend mode */
328 adm1021_write_value(client, ADM1021_REG_CONFIG_W,
329 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
330 /* Set Conversion rate to 1/sec (this can be tinkered with) */
331 adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
332}
333
334static int adm1021_detach_client(struct i2c_client *client)
335{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400336 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int err;
338
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400339 hwmon_device_unregister(data->class_dev);
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if ((err = i2c_detach_client(client))) {
342 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
343 return err;
344 }
345
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400346 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
350/* All registers are byte-sized */
351static int adm1021_read_value(struct i2c_client *client, u8 reg)
352{
353 return i2c_smbus_read_byte_data(client, reg);
354}
355
356static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
357{
358 if (!read_only)
359 return i2c_smbus_write_byte_data(client, reg, value);
360 return 0;
361}
362
363static struct adm1021_data *adm1021_update_device(struct device *dev)
364{
365 struct i2c_client *client = to_i2c_client(dev);
366 struct adm1021_data *data = i2c_get_clientdata(client);
367
368 down(&data->update_lock);
369
370 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
371 || !data->valid) {
372 dev_dbg(&client->dev, "Starting adm1021 update\n");
373
374 data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
375 data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
376 data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
377 data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
378 data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
379 data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
380 data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (data->type == adm1023) {
382 data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
383 data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
384 data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
385 data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
386 data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
387 }
388 data->last_updated = jiffies;
389 data->valid = 1;
390 }
391
392 up(&data->update_lock);
393
394 return data;
395}
396
397static int __init sensors_adm1021_init(void)
398{
399 return i2c_add_driver(&adm1021_driver);
400}
401
402static void __exit sensors_adm1021_exit(void)
403{
404 i2c_del_driver(&adm1021_driver);
405}
406
407MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
408 "Philip Edelbrock <phil@netroedge.com>");
409MODULE_DESCRIPTION("adm1021 driver");
410MODULE_LICENSE("GPL");
411
412module_param(read_only, bool, 0);
413MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
414
415module_init(sensors_adm1021_init)
416module_exit(sensors_adm1021_exit)