blob: 665612729cb9b15a2bebaa1cd0f3e5ecaa71363c [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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040027#include <linux/hwmon.h>
28#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30
31/* Addresses to scan */
32static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
33 0x29, 0x2a, 0x2b,
34 0x4c, 0x4d, 0x4e,
35 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020038I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* adm1021 constants specified below */
41
42/* The adm1021 registers */
43/* Read-only */
44#define ADM1021_REG_TEMP 0x00
45#define ADM1021_REG_REMOTE_TEMP 0x01
46#define ADM1021_REG_STATUS 0x02
47#define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
48#define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */
49#define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */
50/* These use different addresses for reading/writing */
51#define ADM1021_REG_CONFIG_R 0x03
52#define ADM1021_REG_CONFIG_W 0x09
53#define ADM1021_REG_CONV_RATE_R 0x04
54#define ADM1021_REG_CONV_RATE_W 0x0A
55/* These are for the ADM1023's additional precision on the remote temp sensor */
56#define ADM1021_REG_REM_TEMP_PREC 0x010
57#define ADM1021_REG_REM_OFFSET 0x011
58#define ADM1021_REG_REM_OFFSET_PREC 0x012
59#define ADM1021_REG_REM_TOS_PREC 0x013
60#define ADM1021_REG_REM_THYST_PREC 0x014
61/* limits */
62#define ADM1021_REG_TOS_R 0x05
63#define ADM1021_REG_TOS_W 0x0B
64#define ADM1021_REG_REMOTE_TOS_R 0x07
65#define ADM1021_REG_REMOTE_TOS_W 0x0D
66#define ADM1021_REG_THYST_R 0x06
67#define ADM1021_REG_THYST_W 0x0C
68#define ADM1021_REG_REMOTE_THYST_R 0x08
69#define ADM1021_REG_REMOTE_THYST_W 0x0E
70/* write-only */
71#define ADM1021_REG_ONESHOT 0x0F
72
73
74/* Conversions. Rounding and limit checking is only done on the TO_REG
75 variants. Note that you should be a bit careful with which arguments
76 these macros are called: arguments may be evaluated more than once.
77 Fixing this is just not worth it. */
78/* Conversions note: 1021 uses normal integer signed-byte format*/
79#define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
80#define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
81
82/* Initial values */
83
84/* Note: Even though I left the low and high limits named os and hyst,
85they don't quite work like a thermostat the way the LM75 does. I.e.,
86a lower temp than THYST actually triggers an alarm instead of
87clearing it. Weird, ey? --Phil */
88
89/* Each client has this additional data */
90struct adm1021_data {
91 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040092 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 enum chips type;
94
95 struct semaphore update_lock;
96 char valid; /* !=0 if following fields are valid */
97 unsigned long last_updated; /* In jiffies */
98
99 u8 temp_max; /* Register values */
100 u8 temp_hyst;
101 u8 temp_input;
102 u8 remote_temp_max;
103 u8 remote_temp_hyst;
104 u8 remote_temp_input;
105 u8 alarms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /* Special values for ADM1023 only */
107 u8 remote_temp_prec;
108 u8 remote_temp_os_prec;
109 u8 remote_temp_hyst_prec;
110 u8 remote_temp_offset;
111 u8 remote_temp_offset_prec;
112};
113
114static int adm1021_attach_adapter(struct i2c_adapter *adapter);
115static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
116static void adm1021_init_client(struct i2c_client *client);
117static int adm1021_detach_client(struct i2c_client *client);
118static int adm1021_read_value(struct i2c_client *client, u8 reg);
119static int adm1021_write_value(struct i2c_client *client, u8 reg,
120 u16 value);
121static struct adm1021_data *adm1021_update_device(struct device *dev);
122
123/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Jean Delvare02002962005-09-25 16:26:44 +0200124static int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126
127/* This is the driver that will be inserted */
128static struct i2c_driver adm1021_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100129 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100130 .name = "adm1021",
131 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .id = I2C_DRIVERID_ADM1021,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .attach_adapter = adm1021_attach_adapter,
134 .detach_client = adm1021_detach_client,
135};
136
137#define show(value) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400138static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{ \
140 struct adm1021_data *data = adm1021_update_device(dev); \
141 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
142}
143show(temp_max);
144show(temp_hyst);
145show(temp_input);
146show(remote_temp_max);
147show(remote_temp_hyst);
148show(remote_temp_input);
149
150#define show2(value) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400151static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{ \
153 struct adm1021_data *data = adm1021_update_device(dev); \
154 return sprintf(buf, "%d\n", data->value); \
155}
156show2(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#define set(value, reg) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400159static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{ \
161 struct i2c_client *client = to_i2c_client(dev); \
162 struct adm1021_data *data = i2c_get_clientdata(client); \
163 int temp = simple_strtoul(buf, NULL, 10); \
164 \
165 down(&data->update_lock); \
166 data->value = TEMP_TO_REG(temp); \
167 adm1021_write_value(client, reg, data->value); \
168 up(&data->update_lock); \
169 return count; \
170}
171set(temp_max, ADM1021_REG_TOS_W);
172set(temp_hyst, ADM1021_REG_THYST_W);
173set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
174set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
175
176static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
177static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
178static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
179static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
180static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
181static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
182static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184
185static int adm1021_attach_adapter(struct i2c_adapter *adapter)
186{
187 if (!(adapter->class & I2C_CLASS_HWMON))
188 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200189 return i2c_probe(adapter, &addr_data, adm1021_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
193{
194 int i;
195 struct i2c_client *new_client;
196 struct adm1021_data *data;
197 int err = 0;
198 const char *type_name = "";
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
201 goto error0;
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 adm1021_{read,write}_value. */
206
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200207 if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 err = -ENOMEM;
209 goto error0;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 new_client = &data->client;
213 i2c_set_clientdata(new_client, data);
214 new_client->addr = address;
215 new_client->adapter = adapter;
216 new_client->driver = &adm1021_driver;
217 new_client->flags = 0;
218
219 /* Now, we do the remaining detection. */
220 if (kind < 0) {
221 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
222 || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
223 || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
224 err = -ENODEV;
225 goto error1;
226 }
227 }
228
229 /* Determine the chip type. */
230 if (kind <= 0) {
231 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
232 if (i == 0x41)
233 if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
234 kind = adm1023;
235 else
236 kind = adm1021;
237 else if (i == 0x49)
238 kind = thmc10;
239 else if (i == 0x23)
240 kind = gl523sm;
241 else if ((i == 0x4d) &&
242 (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
243 kind = max1617a;
244 else if (i == 0x54)
245 kind = mc1066;
246 /* LM84 Mfr ID in a different place, and it has more unused bits */
247 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
248 && (kind == 0 /* skip extra detection */
249 || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
250 && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
251 kind = lm84;
252 else
253 kind = max1617;
254 }
255
256 if (kind == max1617) {
257 type_name = "max1617";
258 } else if (kind == max1617a) {
259 type_name = "max1617a";
260 } else if (kind == adm1021) {
261 type_name = "adm1021";
262 } else if (kind == adm1023) {
263 type_name = "adm1023";
264 } else if (kind == thmc10) {
265 type_name = "thmc10";
266 } else if (kind == lm84) {
267 type_name = "lm84";
268 } else if (kind == gl523sm) {
269 type_name = "gl523sm";
270 } else if (kind == mc1066) {
271 type_name = "mc1066";
272 }
273
274 /* Fill in the remaining client fields and put it into the global list */
275 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
276 data->type = kind;
277 data->valid = 0;
278 init_MUTEX(&data->update_lock);
279
280 /* Tell the I2C layer a new client has arrived */
281 if ((err = i2c_attach_client(new_client)))
282 goto error1;
283
284 /* Initialize the ADM1021 chip */
285 if (kind != lm84)
286 adm1021_init_client(new_client);
287
288 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400289 data->class_dev = hwmon_device_register(&new_client->dev);
290 if (IS_ERR(data->class_dev)) {
291 err = PTR_ERR(data->class_dev);
292 goto error2;
293 }
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 device_create_file(&new_client->dev, &dev_attr_temp1_max);
296 device_create_file(&new_client->dev, &dev_attr_temp1_min);
297 device_create_file(&new_client->dev, &dev_attr_temp1_input);
298 device_create_file(&new_client->dev, &dev_attr_temp2_max);
299 device_create_file(&new_client->dev, &dev_attr_temp2_min);
300 device_create_file(&new_client->dev, &dev_attr_temp2_input);
301 device_create_file(&new_client->dev, &dev_attr_alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 return 0;
304
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400305error2:
306 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307error1:
308 kfree(data);
309error0:
310 return err;
311}
312
313static void adm1021_init_client(struct i2c_client *client)
314{
315 /* Enable ADC and disable suspend mode */
316 adm1021_write_value(client, ADM1021_REG_CONFIG_W,
317 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
318 /* Set Conversion rate to 1/sec (this can be tinkered with) */
319 adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
320}
321
322static int adm1021_detach_client(struct i2c_client *client)
323{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400324 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int err;
326
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400327 hwmon_device_unregister(data->class_dev);
328
Jean Delvare7bef5592005-07-27 22:14:49 +0200329 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400332 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
336/* All registers are byte-sized */
337static int adm1021_read_value(struct i2c_client *client, u8 reg)
338{
339 return i2c_smbus_read_byte_data(client, reg);
340}
341
342static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
343{
344 if (!read_only)
345 return i2c_smbus_write_byte_data(client, reg, value);
346 return 0;
347}
348
349static struct adm1021_data *adm1021_update_device(struct device *dev)
350{
351 struct i2c_client *client = to_i2c_client(dev);
352 struct adm1021_data *data = i2c_get_clientdata(client);
353
354 down(&data->update_lock);
355
356 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
357 || !data->valid) {
358 dev_dbg(&client->dev, "Starting adm1021 update\n");
359
360 data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
361 data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
362 data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
363 data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
364 data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
365 data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
366 data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (data->type == adm1023) {
368 data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
369 data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
370 data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
371 data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
372 data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
373 }
374 data->last_updated = jiffies;
375 data->valid = 1;
376 }
377
378 up(&data->update_lock);
379
380 return data;
381}
382
383static int __init sensors_adm1021_init(void)
384{
385 return i2c_add_driver(&adm1021_driver);
386}
387
388static void __exit sensors_adm1021_exit(void)
389{
390 i2c_del_driver(&adm1021_driver);
391}
392
393MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
394 "Philip Edelbrock <phil@netroedge.com>");
395MODULE_DESCRIPTION("adm1021 driver");
396MODULE_LICENSE("GPL");
397
398module_param(read_only, bool, 0);
399MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
400
401module_init(sensors_adm1021_init)
402module_exit(sensors_adm1021_exit)