blob: c466329b2ef4745100f00346c4f989958f42828e [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>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010029#include <linux/mutex.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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020039I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* adm1021 constants specified below */
42
43/* The adm1021 registers */
44/* Read-only */
45#define ADM1021_REG_TEMP 0x00
46#define ADM1021_REG_REMOTE_TEMP 0x01
47#define ADM1021_REG_STATUS 0x02
48#define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
49#define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */
50#define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */
51/* These use different addresses for reading/writing */
52#define ADM1021_REG_CONFIG_R 0x03
53#define ADM1021_REG_CONFIG_W 0x09
54#define ADM1021_REG_CONV_RATE_R 0x04
55#define ADM1021_REG_CONV_RATE_W 0x0A
56/* These are for the ADM1023's additional precision on the remote temp sensor */
57#define ADM1021_REG_REM_TEMP_PREC 0x010
58#define ADM1021_REG_REM_OFFSET 0x011
59#define ADM1021_REG_REM_OFFSET_PREC 0x012
60#define ADM1021_REG_REM_TOS_PREC 0x013
61#define ADM1021_REG_REM_THYST_PREC 0x014
62/* limits */
63#define ADM1021_REG_TOS_R 0x05
64#define ADM1021_REG_TOS_W 0x0B
65#define ADM1021_REG_REMOTE_TOS_R 0x07
66#define ADM1021_REG_REMOTE_TOS_W 0x0D
67#define ADM1021_REG_THYST_R 0x06
68#define ADM1021_REG_THYST_W 0x0C
69#define ADM1021_REG_REMOTE_THYST_R 0x08
70#define ADM1021_REG_REMOTE_THYST_W 0x0E
71/* write-only */
72#define ADM1021_REG_ONESHOT 0x0F
73
74
75/* Conversions. Rounding and limit checking is only done on the TO_REG
76 variants. Note that you should be a bit careful with which arguments
77 these macros are called: arguments may be evaluated more than once.
78 Fixing this is just not worth it. */
79/* Conversions note: 1021 uses normal integer signed-byte format*/
80#define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
81#define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
82
83/* Initial values */
84
85/* Note: Even though I left the low and high limits named os and hyst,
86they don't quite work like a thermostat the way the LM75 does. I.e.,
87a lower temp than THYST actually triggers an alarm instead of
88clearing it. Weird, ey? --Phil */
89
90/* Each client has this additional data */
91struct adm1021_data {
92 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040093 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 enum chips type;
95
Ingo Molnar9a61bf62006-01-18 23:19:26 +010096 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 char valid; /* !=0 if following fields are valid */
98 unsigned long last_updated; /* In jiffies */
99
100 u8 temp_max; /* Register values */
101 u8 temp_hyst;
102 u8 temp_input;
103 u8 remote_temp_max;
104 u8 remote_temp_hyst;
105 u8 remote_temp_input;
106 u8 alarms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* Special values for ADM1023 only */
108 u8 remote_temp_prec;
109 u8 remote_temp_os_prec;
110 u8 remote_temp_hyst_prec;
111 u8 remote_temp_offset;
112 u8 remote_temp_offset_prec;
113};
114
115static int adm1021_attach_adapter(struct i2c_adapter *adapter);
116static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
117static void adm1021_init_client(struct i2c_client *client);
118static int adm1021_detach_client(struct i2c_client *client);
119static int adm1021_read_value(struct i2c_client *client, u8 reg);
120static int adm1021_write_value(struct i2c_client *client, u8 reg,
121 u16 value);
122static struct adm1021_data *adm1021_update_device(struct device *dev);
123
124/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Jean Delvare02002962005-09-25 16:26:44 +0200125static int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127
128/* This is the driver that will be inserted */
129static struct i2c_driver adm1021_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100130 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100131 .name = "adm1021",
132 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .id = I2C_DRIVERID_ADM1021,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .attach_adapter = adm1021_attach_adapter,
135 .detach_client = adm1021_detach_client,
136};
137
138#define show(value) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400139static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{ \
141 struct adm1021_data *data = adm1021_update_device(dev); \
142 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
143}
144show(temp_max);
145show(temp_hyst);
146show(temp_input);
147show(remote_temp_max);
148show(remote_temp_hyst);
149show(remote_temp_input);
150
151#define show2(value) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400152static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{ \
154 struct adm1021_data *data = adm1021_update_device(dev); \
155 return sprintf(buf, "%d\n", data->value); \
156}
157show2(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159#define set(value, reg) \
Yani Ioannou74880c02005-05-17 06:41:12 -0400160static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{ \
162 struct i2c_client *client = to_i2c_client(dev); \
163 struct adm1021_data *data = i2c_get_clientdata(client); \
164 int temp = simple_strtoul(buf, NULL, 10); \
165 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100166 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 data->value = TEMP_TO_REG(temp); \
168 adm1021_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100169 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return count; \
171}
172set(temp_max, ADM1021_REG_TOS_W);
173set(temp_hyst, ADM1021_REG_THYST_W);
174set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
175set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
176
177static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
178static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
179static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
180static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
181static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
182static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
183static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185
186static int adm1021_attach_adapter(struct i2c_adapter *adapter)
187{
188 if (!(adapter->class & I2C_CLASS_HWMON))
189 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200190 return i2c_probe(adapter, &addr_data, adm1021_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200193static struct attribute *adm1021_attributes[] = {
194 &dev_attr_temp1_max.attr,
195 &dev_attr_temp1_min.attr,
196 &dev_attr_temp1_input.attr,
197 &dev_attr_temp2_max.attr,
198 &dev_attr_temp2_min.attr,
199 &dev_attr_temp2_input.attr,
200 &dev_attr_alarms.attr,
201 NULL
202};
203
204static const struct attribute_group adm1021_group = {
205 .attrs = adm1021_attributes,
206};
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
209{
210 int i;
211 struct i2c_client *new_client;
212 struct adm1021_data *data;
213 int err = 0;
214 const char *type_name = "";
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
217 goto error0;
218
219 /* OK. For now, we presume we have a valid client. We now create the
220 client structure, even though we cannot fill it completely yet.
221 But it allows us to access adm1021_{read,write}_value. */
222
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200223 if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err = -ENOMEM;
225 goto error0;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 new_client = &data->client;
229 i2c_set_clientdata(new_client, data);
230 new_client->addr = address;
231 new_client->adapter = adapter;
232 new_client->driver = &adm1021_driver;
233 new_client->flags = 0;
234
235 /* Now, we do the remaining detection. */
236 if (kind < 0) {
237 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
238 || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
239 || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
240 err = -ENODEV;
241 goto error1;
242 }
243 }
244
245 /* Determine the chip type. */
246 if (kind <= 0) {
247 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
248 if (i == 0x41)
249 if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
250 kind = adm1023;
251 else
252 kind = adm1021;
253 else if (i == 0x49)
254 kind = thmc10;
255 else if (i == 0x23)
256 kind = gl523sm;
257 else if ((i == 0x4d) &&
258 (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
259 kind = max1617a;
260 else if (i == 0x54)
261 kind = mc1066;
262 /* LM84 Mfr ID in a different place, and it has more unused bits */
263 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
264 && (kind == 0 /* skip extra detection */
265 || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
266 && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
267 kind = lm84;
268 else
269 kind = max1617;
270 }
271
272 if (kind == max1617) {
273 type_name = "max1617";
274 } else if (kind == max1617a) {
275 type_name = "max1617a";
276 } else if (kind == adm1021) {
277 type_name = "adm1021";
278 } else if (kind == adm1023) {
279 type_name = "adm1023";
280 } else if (kind == thmc10) {
281 type_name = "thmc10";
282 } else if (kind == lm84) {
283 type_name = "lm84";
284 } else if (kind == gl523sm) {
285 type_name = "gl523sm";
286 } else if (kind == mc1066) {
287 type_name = "mc1066";
288 }
289
290 /* Fill in the remaining client fields and put it into the global list */
291 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
292 data->type = kind;
293 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100294 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* Tell the I2C layer a new client has arrived */
297 if ((err = i2c_attach_client(new_client)))
298 goto error1;
299
300 /* Initialize the ADM1021 chip */
301 if (kind != lm84)
302 adm1021_init_client(new_client);
303
304 /* Register sysfs hooks */
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200305 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1021_group)))
306 goto error2;
307
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400308 data->class_dev = hwmon_device_register(&new_client->dev);
309 if (IS_ERR(data->class_dev)) {
310 err = PTR_ERR(data->class_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200311 goto error3;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400312 }
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200316error3:
317 sysfs_remove_group(&new_client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400318error2:
319 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320error1:
321 kfree(data);
322error0:
323 return err;
324}
325
326static void adm1021_init_client(struct i2c_client *client)
327{
328 /* Enable ADC and disable suspend mode */
329 adm1021_write_value(client, ADM1021_REG_CONFIG_W,
330 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
331 /* Set Conversion rate to 1/sec (this can be tinkered with) */
332 adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
333}
334
335static int adm1021_detach_client(struct i2c_client *client)
336{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400337 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
339
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400340 hwmon_device_unregister(data->class_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200341 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400342
Jean Delvare7bef5592005-07-27 22:14:49 +0200343 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100392 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
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)