1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#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/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/err.h>
30#include <linux/mutex.h>
31
32
33
34static const unsigned short normal_i2c[] = {
35 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
36
37enum chips {
38 adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
39
40
41
42
43
44
45#define ADM1021_REG_TEMP(nr) (nr)
46#define ADM1021_REG_STATUS 0x02
47
48#define ADM1021_REG_MAN_ID 0xFE
49
50#define ADM1021_REG_DEV_ID 0xFF
51
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
57#define ADM1023_REG_REM_TEMP_PREC 0x10
58#define ADM1023_REG_REM_OFFSET 0x11
59#define ADM1023_REG_REM_OFFSET_PREC 0x12
60#define ADM1023_REG_REM_TOS_PREC 0x13
61#define ADM1023_REG_REM_THYST_PREC 0x14
62
63
64#define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
65#define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
66#define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
67#define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
68
69#define ADM1021_REG_ONESHOT 0x0F
70
71
72
73
74
75
76
77
78
79
80
81struct adm1021_data {
82 struct device *hwmon_dev;
83 enum chips type;
84
85 struct mutex update_lock;
86 char valid;
87 char low_power;
88 unsigned long last_updated;
89
90 int temp_max[2];
91 int temp_min[2];
92 int temp[2];
93 u8 alarms;
94
95 u8 remote_temp_offset;
96 u8 remote_temp_offset_prec;
97};
98
99static int adm1021_probe(struct i2c_client *client,
100 const struct i2c_device_id *id);
101static int adm1021_detect(struct i2c_client *client,
102 struct i2c_board_info *info);
103static void adm1021_init_client(struct i2c_client *client);
104static int adm1021_remove(struct i2c_client *client);
105static struct adm1021_data *adm1021_update_device(struct device *dev);
106
107
108static bool read_only;
109
110
111static const struct i2c_device_id adm1021_id[] = {
112 { "adm1021", adm1021 },
113 { "adm1023", adm1023 },
114 { "max1617", max1617 },
115 { "max1617a", max1617a },
116 { "thmc10", thmc10 },
117 { "lm84", lm84 },
118 { "gl523sm", gl523sm },
119 { "mc1066", mc1066 },
120 { }
121};
122MODULE_DEVICE_TABLE(i2c, adm1021_id);
123
124
125static struct i2c_driver adm1021_driver = {
126 .class = I2C_CLASS_HWMON,
127 .driver = {
128 .name = "adm1021",
129 },
130 .probe = adm1021_probe,
131 .remove = adm1021_remove,
132 .id_table = adm1021_id,
133 .detect = adm1021_detect,
134 .address_list = normal_i2c,
135};
136
137static ssize_t show_temp(struct device *dev,
138 struct device_attribute *devattr, char *buf)
139{
140 int index = to_sensor_dev_attr(devattr)->index;
141 struct adm1021_data *data = adm1021_update_device(dev);
142
143 return sprintf(buf, "%d\n", data->temp[index]);
144}
145
146static ssize_t show_temp_max(struct device *dev,
147 struct device_attribute *devattr, char *buf)
148{
149 int index = to_sensor_dev_attr(devattr)->index;
150 struct adm1021_data *data = adm1021_update_device(dev);
151
152 return sprintf(buf, "%d\n", data->temp_max[index]);
153}
154
155static ssize_t show_temp_min(struct device *dev,
156 struct device_attribute *devattr, char *buf)
157{
158 int index = to_sensor_dev_attr(devattr)->index;
159 struct adm1021_data *data = adm1021_update_device(dev);
160
161 return sprintf(buf, "%d\n", data->temp_min[index]);
162}
163
164static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
165 char *buf)
166{
167 int index = to_sensor_dev_attr(attr)->index;
168 struct adm1021_data *data = adm1021_update_device(dev);
169 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
170}
171
172static ssize_t show_alarms(struct device *dev,
173 struct device_attribute *attr,
174 char *buf)
175{
176 struct adm1021_data *data = adm1021_update_device(dev);
177 return sprintf(buf, "%u\n", data->alarms);
178}
179
180static ssize_t set_temp_max(struct device *dev,
181 struct device_attribute *devattr,
182 const char *buf, size_t count)
183{
184 int index = to_sensor_dev_attr(devattr)->index;
185 struct i2c_client *client = to_i2c_client(dev);
186 struct adm1021_data *data = i2c_get_clientdata(client);
187 long temp;
188 int err;
189
190 err = kstrtol(buf, 10, &temp);
191 if (err)
192 return err;
193 temp /= 1000;
194
195 mutex_lock(&data->update_lock);
196 data->temp_max[index] = clamp_val(temp, -128, 127);
197 if (!read_only)
198 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
199 data->temp_max[index]);
200 mutex_unlock(&data->update_lock);
201
202 return count;
203}
204
205static ssize_t set_temp_min(struct device *dev,
206 struct device_attribute *devattr,
207 const char *buf, size_t count)
208{
209 int index = to_sensor_dev_attr(devattr)->index;
210 struct i2c_client *client = to_i2c_client(dev);
211 struct adm1021_data *data = i2c_get_clientdata(client);
212 long temp;
213 int err;
214
215 err = kstrtol(buf, 10, &temp);
216 if (err)
217 return err;
218 temp /= 1000;
219
220 mutex_lock(&data->update_lock);
221 data->temp_min[index] = clamp_val(temp, -128, 127);
222 if (!read_only)
223 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
224 data->temp_min[index]);
225 mutex_unlock(&data->update_lock);
226
227 return count;
228}
229
230static ssize_t show_low_power(struct device *dev,
231 struct device_attribute *devattr, char *buf)
232{
233 struct adm1021_data *data = adm1021_update_device(dev);
234 return sprintf(buf, "%d\n", data->low_power);
235}
236
237static ssize_t set_low_power(struct device *dev,
238 struct device_attribute *devattr,
239 const char *buf, size_t count)
240{
241 struct i2c_client *client = to_i2c_client(dev);
242 struct adm1021_data *data = i2c_get_clientdata(client);
243 char low_power;
244 unsigned long val;
245 int err;
246
247 err = kstrtoul(buf, 10, &val);
248 if (err)
249 return err;
250 low_power = val != 0;
251
252 mutex_lock(&data->update_lock);
253 if (low_power != data->low_power) {
254 int config = i2c_smbus_read_byte_data(
255 client, ADM1021_REG_CONFIG_R);
256 data->low_power = low_power;
257 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
258 (config & 0xBF) | (low_power << 6));
259 }
260 mutex_unlock(&data->update_lock);
261
262 return count;
263}
264
265
266static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
267static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
268 set_temp_max, 0);
269static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
270 set_temp_min, 0);
271static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
272static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
273 set_temp_max, 1);
274static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
275 set_temp_min, 1);
276static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
277static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
278static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
279static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
280static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
281
282static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
283static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
284
285static struct attribute *adm1021_attributes[] = {
286 &sensor_dev_attr_temp1_max.dev_attr.attr,
287 &sensor_dev_attr_temp1_min.dev_attr.attr,
288 &sensor_dev_attr_temp1_input.dev_attr.attr,
289 &sensor_dev_attr_temp2_max.dev_attr.attr,
290 &sensor_dev_attr_temp2_min.dev_attr.attr,
291 &sensor_dev_attr_temp2_input.dev_attr.attr,
292 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
293 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
294 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
295 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
296 &sensor_dev_attr_temp2_fault.dev_attr.attr,
297 &dev_attr_alarms.attr,
298 &dev_attr_low_power.attr,
299 NULL
300};
301
302static const struct attribute_group adm1021_group = {
303 .attrs = adm1021_attributes,
304};
305
306
307static int adm1021_detect(struct i2c_client *client,
308 struct i2c_board_info *info)
309{
310 struct i2c_adapter *adapter = client->adapter;
311 const char *type_name;
312 int conv_rate, status, config, man_id, dev_id;
313
314 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
315 pr_debug("detect failed, smbus byte data not supported!\n");
316 return -ENODEV;
317 }
318
319 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
320 conv_rate = i2c_smbus_read_byte_data(client,
321 ADM1021_REG_CONV_RATE_R);
322 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
323
324
325 if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
326 pr_debug("detect failed, chip not detected!\n");
327 return -ENODEV;
328 }
329
330
331 man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
332 dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
333
334 if (man_id < 0 || dev_id < 0)
335 return -ENODEV;
336
337 if (man_id == 0x4d && dev_id == 0x01)
338 type_name = "max1617a";
339 else if (man_id == 0x41) {
340 if ((dev_id & 0xF0) == 0x30)
341 type_name = "adm1023";
342 else if ((dev_id & 0xF0) == 0x00)
343 type_name = "adm1021";
344 else
345 return -ENODEV;
346 } else if (man_id == 0x49)
347 type_name = "thmc10";
348 else if (man_id == 0x23)
349 type_name = "gl523sm";
350 else if (man_id == 0x54)
351 type_name = "mc1066";
352 else {
353 int lte, rte, lhi, rhi, llo, rlo;
354
355
356
357 llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
358 rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
359
360
361 if (llo < 0 || rlo < 0)
362 return -ENODEV;
363
364 lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
365 rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
366 lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
367 rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
368
369
370
371
372
373 if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
374 return -ENODEV;
375
376
377 if (lte == rte && lte == lhi && lte == rhi && lte == llo
378 && lte == rlo)
379 return -ENODEV;
380
381
382
383
384
385 if (conv_rate == 0x00
386 && (config & 0x7F) == 0x00
387 && (status & 0xAB) == 0x00) {
388 type_name = "lm84";
389 } else {
390
391 if ((s8)llo > lhi || (s8)rlo > rhi)
392 return -ENODEV;
393 type_name = "max1617";
394 }
395 }
396
397 pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
398 type_name, i2c_adapter_id(adapter), client->addr);
399 strlcpy(info->type, type_name, I2C_NAME_SIZE);
400
401 return 0;
402}
403
404static int adm1021_probe(struct i2c_client *client,
405 const struct i2c_device_id *id)
406{
407 struct adm1021_data *data;
408 int err;
409
410 data = devm_kzalloc(&client->dev, sizeof(struct adm1021_data),
411 GFP_KERNEL);
412 if (!data)
413 return -ENOMEM;
414
415 i2c_set_clientdata(client, data);
416 data->type = id->driver_data;
417 mutex_init(&data->update_lock);
418
419
420 if (data->type != lm84 && !read_only)
421 adm1021_init_client(client);
422
423
424 err = sysfs_create_group(&client->dev.kobj, &adm1021_group);
425 if (err)
426 return err;
427
428 data->hwmon_dev = hwmon_device_register(&client->dev);
429 if (IS_ERR(data->hwmon_dev)) {
430 err = PTR_ERR(data->hwmon_dev);
431 goto error;
432 }
433
434 return 0;
435
436error:
437 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
438 return err;
439}
440
441static void adm1021_init_client(struct i2c_client *client)
442{
443
444 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
445 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
446
447 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
448}
449
450static int adm1021_remove(struct i2c_client *client)
451{
452 struct adm1021_data *data = i2c_get_clientdata(client);
453
454 hwmon_device_unregister(data->hwmon_dev);
455 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
456
457 return 0;
458}
459
460static struct adm1021_data *adm1021_update_device(struct device *dev)
461{
462 struct i2c_client *client = to_i2c_client(dev);
463 struct adm1021_data *data = i2c_get_clientdata(client);
464
465 mutex_lock(&data->update_lock);
466
467 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
468 || !data->valid) {
469 int i;
470
471 dev_dbg(&client->dev, "Starting adm1021 update\n");
472
473 for (i = 0; i < 2; i++) {
474 data->temp[i] = 1000 *
475 (s8) i2c_smbus_read_byte_data(
476 client, ADM1021_REG_TEMP(i));
477 data->temp_max[i] = 1000 *
478 (s8) i2c_smbus_read_byte_data(
479 client, ADM1021_REG_TOS_R(i));
480 data->temp_min[i] = 1000 *
481 (s8) i2c_smbus_read_byte_data(
482 client, ADM1021_REG_THYST_R(i));
483 }
484 data->alarms = i2c_smbus_read_byte_data(client,
485 ADM1021_REG_STATUS) & 0x7c;
486 if (data->type == adm1023) {
487
488
489
490
491 data->temp[1] += 125 * (i2c_smbus_read_byte_data(
492 client, ADM1023_REG_REM_TEMP_PREC) >> 5);
493 data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
494 client, ADM1023_REG_REM_TOS_PREC) >> 5);
495 data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
496 client, ADM1023_REG_REM_THYST_PREC) >> 5);
497 data->remote_temp_offset =
498 i2c_smbus_read_byte_data(client,
499 ADM1023_REG_REM_OFFSET);
500 data->remote_temp_offset_prec =
501 i2c_smbus_read_byte_data(client,
502 ADM1023_REG_REM_OFFSET_PREC);
503 }
504 data->last_updated = jiffies;
505 data->valid = 1;
506 }
507
508 mutex_unlock(&data->update_lock);
509
510 return data;
511}
512
513module_i2c_driver(adm1021_driver);
514
515MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
516 "Philip Edelbrock <phil@netroedge.com>");
517MODULE_DESCRIPTION("adm1021 driver");
518MODULE_LICENSE("GPL");
519
520module_param(read_only, bool, 0);
521MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
522