hwmon: (lm90) Rename temperature conversion functions to match usage
[deliverable/linux.git] / drivers / hwmon / lm90.c
CommitLineData
1da177e4
LT
1/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
6388a388 4 * Copyright (C) 2003-2008 Jean Delvare <khali@linux-fr.org>
1da177e4
LT
5 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
a874a10c 9 * temperature) and a 3-4 deg accuracy.
1da177e4
LT
10 *
11 * This driver also supports the LM89 and LM99, two other sensor chips
12 * made by National Semiconductor. Both have an increased remote
13 * temperature measurement accuracy (1 degree), and the LM99
14 * additionally shifts remote temperatures (measured and limits) by 16
15 * degrees, which allows for higher temperatures measurement. The
16 * driver doesn't handle it since it can be done easily in user-space.
44bbe87e 17 * Note that there is no way to differentiate between both chips.
1da177e4
LT
18 *
19 * This driver also supports the LM86, another sensor chip made by
20 * National Semiconductor. It is exactly similar to the LM90 except it
21 * has a higher accuracy.
1da177e4
LT
22 *
23 * This driver also supports the ADM1032, a sensor chip made by Analog
24 * Devices. That chip is similar to the LM90, with a few differences
a874a10c
JD
25 * that are not handled by this driver. Among others, it has a higher
26 * accuracy than the LM90, much like the LM86 does.
1da177e4
LT
27 *
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
a874a10c 29 * chips made by Maxim. These chips are similar to the LM86.
44bbe87e 30 * Note that there is no easy way to differentiate between the three
1da177e4 31 * variants. The extra address and features of the MAX6659 are not
69f2f96d
JD
32 * supported by this driver. These chips lack the remote temperature
33 * offset feature.
1da177e4 34 *
32c82a93
RB
35 * This driver also supports the MAX6680 and MAX6681, two other sensor
36 * chips made by Maxim. These are quite similar to the other Maxim
a874a10c
JD
37 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
38 * be treated identically.
32c82a93 39 *
23b2d477
NC
40 * This driver also supports the ADT7461 chip from Analog Devices.
41 * It's supported in both compatibility and extended mode. It is mostly
42 * compatible with LM90 except for a data format difference for the
43 * temperature value registers.
1da177e4
LT
44 *
45 * Since the LM90 was the first chipset supported by this driver, most
46 * comments will refer to this chipset, but are actually general and
47 * concern all supported chipsets, unless mentioned otherwise.
48 *
49 * This program is free software; you can redistribute it and/or modify
50 * it under the terms of the GNU General Public License as published by
51 * the Free Software Foundation; either version 2 of the License, or
52 * (at your option) any later version.
53 *
54 * This program is distributed in the hope that it will be useful,
55 * but WITHOUT ANY WARRANTY; without even the implied warranty of
56 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 * GNU General Public License for more details.
58 *
59 * You should have received a copy of the GNU General Public License
60 * along with this program; if not, write to the Free Software
61 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
62 */
63
1da177e4
LT
64#include <linux/module.h>
65#include <linux/init.h>
66#include <linux/slab.h>
67#include <linux/jiffies.h>
68#include <linux/i2c.h>
10c08f81 69#include <linux/hwmon-sysfs.h>
943b0830
MH
70#include <linux/hwmon.h>
71#include <linux/err.h>
9a61bf63 72#include <linux/mutex.h>
0e39e01c 73#include <linux/sysfs.h>
1da177e4
LT
74
75/*
76 * Addresses to scan
77 * Address is fully defined internally and cannot be changed except for
32c82a93 78 * MAX6659, MAX6680 and MAX6681.
90209b42
JD
79 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
80 * have address 0x4c.
81 * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
1da177e4 82 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
32c82a93
RB
83 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
84 * 0x4c, 0x4d or 0x4e.
1da177e4
LT
85 */
86
25e9c86d
MH
87static const unsigned short normal_i2c[] = {
88 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
1da177e4
LT
89
90/*
91 * Insmod parameters
92 */
93
32c82a93 94I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680);
1da177e4
LT
95
96/*
97 * The LM90 registers
98 */
99
100#define LM90_REG_R_MAN_ID 0xFE
101#define LM90_REG_R_CHIP_ID 0xFF
102#define LM90_REG_R_CONFIG1 0x03
103#define LM90_REG_W_CONFIG1 0x09
104#define LM90_REG_R_CONFIG2 0xBF
105#define LM90_REG_W_CONFIG2 0xBF
106#define LM90_REG_R_CONVRATE 0x04
107#define LM90_REG_W_CONVRATE 0x0A
108#define LM90_REG_R_STATUS 0x02
109#define LM90_REG_R_LOCAL_TEMP 0x00
110#define LM90_REG_R_LOCAL_HIGH 0x05
111#define LM90_REG_W_LOCAL_HIGH 0x0B
112#define LM90_REG_R_LOCAL_LOW 0x06
113#define LM90_REG_W_LOCAL_LOW 0x0C
114#define LM90_REG_R_LOCAL_CRIT 0x20
115#define LM90_REG_W_LOCAL_CRIT 0x20
116#define LM90_REG_R_REMOTE_TEMPH 0x01
117#define LM90_REG_R_REMOTE_TEMPL 0x10
118#define LM90_REG_R_REMOTE_OFFSH 0x11
119#define LM90_REG_W_REMOTE_OFFSH 0x11
120#define LM90_REG_R_REMOTE_OFFSL 0x12
121#define LM90_REG_W_REMOTE_OFFSL 0x12
122#define LM90_REG_R_REMOTE_HIGHH 0x07
123#define LM90_REG_W_REMOTE_HIGHH 0x0D
124#define LM90_REG_R_REMOTE_HIGHL 0x13
125#define LM90_REG_W_REMOTE_HIGHL 0x13
126#define LM90_REG_R_REMOTE_LOWH 0x08
127#define LM90_REG_W_REMOTE_LOWH 0x0E
128#define LM90_REG_R_REMOTE_LOWL 0x14
129#define LM90_REG_W_REMOTE_LOWL 0x14
130#define LM90_REG_R_REMOTE_CRIT 0x19
131#define LM90_REG_W_REMOTE_CRIT 0x19
132#define LM90_REG_R_TCRIT_HYST 0x21
133#define LM90_REG_W_TCRIT_HYST 0x21
134
f65e1708
JD
135/* MAX6657-specific registers */
136
137#define MAX6657_REG_R_LOCAL_TEMPL 0x11
138
23b2d477
NC
139/*
140 * Device flags
141 */
142#define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
143
1da177e4
LT
144/*
145 * Functions declaration
146 */
147
9b0e8526
JD
148static int lm90_detect(struct i2c_client *client, int kind,
149 struct i2c_board_info *info);
150static int lm90_probe(struct i2c_client *client,
151 const struct i2c_device_id *id);
1da177e4 152static void lm90_init_client(struct i2c_client *client);
9b0e8526 153static int lm90_remove(struct i2c_client *client);
1da177e4
LT
154static struct lm90_data *lm90_update_device(struct device *dev);
155
156/*
157 * Driver data (common to all clients)
158 */
159
9b0e8526
JD
160static const struct i2c_device_id lm90_id[] = {
161 { "adm1032", adm1032 },
162 { "adt7461", adt7461 },
163 { "lm90", lm90 },
164 { "lm86", lm86 },
165 { "lm89", lm99 },
166 { "lm99", lm99 }, /* Missing temperature offset */
167 { "max6657", max6657 },
168 { "max6658", max6657 },
169 { "max6659", max6657 },
170 { "max6680", max6680 },
171 { "max6681", max6680 },
172 { }
173};
174MODULE_DEVICE_TABLE(i2c, lm90_id);
175
1da177e4 176static struct i2c_driver lm90_driver = {
9b0e8526 177 .class = I2C_CLASS_HWMON,
cdaf7934 178 .driver = {
cdaf7934
LR
179 .name = "lm90",
180 },
9b0e8526
JD
181 .probe = lm90_probe,
182 .remove = lm90_remove,
183 .id_table = lm90_id,
184 .detect = lm90_detect,
185 .address_data = &addr_data,
1da177e4
LT
186};
187
188/*
189 * Client data (each client gets its own)
190 */
191
192struct lm90_data {
1beeffe4 193 struct device *hwmon_dev;
9a61bf63 194 struct mutex update_lock;
1da177e4
LT
195 char valid; /* zero until following fields are valid */
196 unsigned long last_updated; /* in jiffies */
197 int kind;
23b2d477 198 int flags;
1da177e4
LT
199
200 /* registers values */
f65e1708
JD
201 s8 temp8[4]; /* 0: local low limit
202 1: local high limit
203 2: local critical limit
204 3: remote critical limit */
205 s16 temp11[5]; /* 0: remote input
30d7394b 206 1: remote low limit
69f2f96d 207 2: remote high limit
f65e1708
JD
208 3: remote offset (except max6657)
209 4: local input */
1da177e4
LT
210 u8 temp_hyst;
211 u8 alarms; /* bitvector */
212};
213
cea50fe2
NC
214/*
215 * Conversions
216 * For local temperatures and limits, critical limits and the hysteresis
217 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
218 * For remote temperatures and limits, it uses signed 11-bit values with
219 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
220 */
221
9d4d3834 222static inline int temp_from_s8(s8 val)
cea50fe2
NC
223{
224 return val * 1000;
225}
226
9d4d3834 227static inline int temp_from_s16(s16 val)
cea50fe2
NC
228{
229 return val / 32 * 125;
230}
231
9d4d3834 232static s8 temp_to_s8(long val)
cea50fe2
NC
233{
234 if (val <= -128000)
235 return -128;
236 if (val >= 127000)
237 return 127;
238 if (val < 0)
239 return (val - 500) / 1000;
240 return (val + 500) / 1000;
241}
242
9d4d3834 243static s16 temp_to_s16(long val)
cea50fe2
NC
244{
245 if (val <= -128000)
246 return 0x8000;
247 if (val >= 127875)
248 return 0x7FE0;
249 if (val < 0)
250 return (val - 62) / 125 * 32;
251 return (val + 62) / 125 * 32;
252}
253
254static u8 hyst_to_reg(long val)
255{
256 if (val <= 0)
257 return 0;
258 if (val >= 30500)
259 return 31;
260 return (val + 500) / 1000;
261}
262
263/*
23b2d477
NC
264 * ADT7461 in compatibility mode is almost identical to LM90 except that
265 * attempts to write values that are outside the range 0 < temp < 127 are
266 * treated as the boundary value.
267 *
268 * ADT7461 in "extended mode" operation uses unsigned integers offset by
269 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
cea50fe2 270 */
9d4d3834 271static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
cea50fe2 272{
23b2d477
NC
273 if (data->flags & LM90_FLAG_ADT7461_EXT)
274 return (val - 64) * 1000;
275 else
9d4d3834 276 return temp_from_s8(val);
cea50fe2
NC
277}
278
9d4d3834 279static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
cea50fe2 280{
23b2d477
NC
281 if (data->flags & LM90_FLAG_ADT7461_EXT)
282 return (val - 0x4000) / 64 * 250;
283 else
9d4d3834 284 return temp_from_s16(val);
23b2d477
NC
285}
286
9d4d3834 287static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
23b2d477
NC
288{
289 if (data->flags & LM90_FLAG_ADT7461_EXT) {
290 if (val <= -64000)
291 return 0;
292 if (val >= 191000)
293 return 0xFF;
294 return (val + 500 + 64000) / 1000;
295 } else {
296 if (val <= 0)
297 return 0;
298 if (val >= 127000)
299 return 127;
300 return (val + 500) / 1000;
301 }
302}
303
9d4d3834 304static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
23b2d477
NC
305{
306 if (data->flags & LM90_FLAG_ADT7461_EXT) {
307 if (val <= -64000)
308 return 0;
309 if (val >= 191750)
310 return 0xFFC0;
311 return (val + 64000 + 125) / 250 * 64;
312 } else {
313 if (val <= 0)
314 return 0;
315 if (val >= 127750)
316 return 0x7FC0;
317 return (val + 125) / 250 * 64;
318 }
cea50fe2
NC
319}
320
1da177e4
LT
321/*
322 * Sysfs stuff
323 */
324
30d7394b
JD
325static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
326 char *buf)
327{
328 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
329 struct lm90_data *data = lm90_update_device(dev);
23b2d477
NC
330 int temp;
331
332 if (data->kind == adt7461)
9d4d3834 333 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
23b2d477 334 else
9d4d3834 335 temp = temp_from_s8(data->temp8[attr->index]);
23b2d477
NC
336
337 return sprintf(buf, "%d\n", temp);
30d7394b
JD
338}
339
340static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
341 const char *buf, size_t count)
342{
343 static const u8 reg[4] = {
344 LM90_REG_W_LOCAL_LOW,
345 LM90_REG_W_LOCAL_HIGH,
346 LM90_REG_W_LOCAL_CRIT,
347 LM90_REG_W_REMOTE_CRIT,
348 };
349
350 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
351 struct i2c_client *client = to_i2c_client(dev);
352 struct lm90_data *data = i2c_get_clientdata(client);
353 long val = simple_strtol(buf, NULL, 10);
354 int nr = attr->index;
355
9a61bf63 356 mutex_lock(&data->update_lock);
30d7394b 357 if (data->kind == adt7461)
9d4d3834 358 data->temp8[nr] = temp_to_u8_adt7461(data, val);
30d7394b 359 else
9d4d3834 360 data->temp8[nr] = temp_to_s8(val);
f65e1708 361 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
9a61bf63 362 mutex_unlock(&data->update_lock);
30d7394b 363 return count;
1da177e4 364}
30d7394b
JD
365
366static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
367 char *buf)
368{
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
370 struct lm90_data *data = lm90_update_device(dev);
23b2d477
NC
371 int temp;
372
373 if (data->kind == adt7461)
9d4d3834 374 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
23b2d477 375 else
9d4d3834 376 temp = temp_from_s16(data->temp11[attr->index]);
23b2d477
NC
377
378 return sprintf(buf, "%d\n", temp);
1da177e4 379}
30d7394b
JD
380
381static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
382 const char *buf, size_t count)
383{
69f2f96d 384 static const u8 reg[6] = {
30d7394b
JD
385 LM90_REG_W_REMOTE_LOWH,
386 LM90_REG_W_REMOTE_LOWL,
387 LM90_REG_W_REMOTE_HIGHH,
388 LM90_REG_W_REMOTE_HIGHL,
69f2f96d
JD
389 LM90_REG_W_REMOTE_OFFSH,
390 LM90_REG_W_REMOTE_OFFSL,
30d7394b
JD
391 };
392
393 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
394 struct i2c_client *client = to_i2c_client(dev);
395 struct lm90_data *data = i2c_get_clientdata(client);
396 long val = simple_strtol(buf, NULL, 10);
397 int nr = attr->index;
398
9a61bf63 399 mutex_lock(&data->update_lock);
30d7394b 400 if (data->kind == adt7461)
9d4d3834 401 data->temp11[nr] = temp_to_u16_adt7461(data, val);
5f502a83 402 else if (data->kind == max6657 || data->kind == max6680)
9d4d3834 403 data->temp11[nr] = temp_to_s8(val) << 8;
30d7394b 404 else
9d4d3834 405 data->temp11[nr] = temp_to_s16(val);
5f502a83 406
30d7394b
JD
407 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
408 data->temp11[nr] >> 8);
5f502a83
JD
409 if (data->kind != max6657 && data->kind != max6680)
410 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
411 data->temp11[nr] & 0xff);
9a61bf63 412 mutex_unlock(&data->update_lock);
30d7394b 413 return count;
1da177e4 414}
30d7394b
JD
415
416static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
417 char *buf)
418{
419 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
420 struct lm90_data *data = lm90_update_device(dev);
23b2d477
NC
421 int temp;
422
423 if (data->kind == adt7461)
9d4d3834 424 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
23b2d477 425 else
9d4d3834 426 temp = temp_from_s8(data->temp8[attr->index]);
23b2d477 427
9d4d3834 428 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
1da177e4 429}
1da177e4 430
30d7394b
JD
431static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
432 const char *buf, size_t count)
1da177e4
LT
433{
434 struct i2c_client *client = to_i2c_client(dev);
435 struct lm90_data *data = i2c_get_clientdata(client);
436 long val = simple_strtol(buf, NULL, 10);
437 long hyst;
438
9a61bf63 439 mutex_lock(&data->update_lock);
9d4d3834 440 hyst = temp_from_s8(data->temp8[2]) - val;
1da177e4 441 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
cea50fe2 442 hyst_to_reg(hyst));
9a61bf63 443 mutex_unlock(&data->update_lock);
1da177e4
LT
444 return count;
445}
446
30d7394b
JD
447static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
448 char *buf)
1da177e4
LT
449{
450 struct lm90_data *data = lm90_update_device(dev);
451 return sprintf(buf, "%d\n", data->alarms);
452}
453
2d45771e
JD
454static ssize_t show_alarm(struct device *dev, struct device_attribute
455 *devattr, char *buf)
456{
457 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
458 struct lm90_data *data = lm90_update_device(dev);
459 int bitnr = attr->index;
460
461 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
462}
463
f65e1708 464static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
30d7394b
JD
465static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
466static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
f65e1708 467 set_temp8, 0);
30d7394b
JD
468static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
469 set_temp11, 1);
470static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
f65e1708 471 set_temp8, 1);
30d7394b
JD
472static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
473 set_temp11, 2);
474static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
f65e1708 475 set_temp8, 2);
30d7394b 476static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
f65e1708 477 set_temp8, 3);
30d7394b 478static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
f65e1708
JD
479 set_temphyst, 2);
480static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
69f2f96d
JD
481static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
482 set_temp11, 3);
2d45771e
JD
483
484/* Individual alarm files */
485static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
486static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
7817a39e 487static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
2d45771e
JD
488static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
489static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
490static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
491static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
492/* Raw alarm file for compatibility */
1da177e4
LT
493static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
494
0e39e01c
JD
495static struct attribute *lm90_attributes[] = {
496 &sensor_dev_attr_temp1_input.dev_attr.attr,
497 &sensor_dev_attr_temp2_input.dev_attr.attr,
498 &sensor_dev_attr_temp1_min.dev_attr.attr,
499 &sensor_dev_attr_temp2_min.dev_attr.attr,
500 &sensor_dev_attr_temp1_max.dev_attr.attr,
501 &sensor_dev_attr_temp2_max.dev_attr.attr,
502 &sensor_dev_attr_temp1_crit.dev_attr.attr,
503 &sensor_dev_attr_temp2_crit.dev_attr.attr,
504 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
505 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
506
507 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
508 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
7817a39e 509 &sensor_dev_attr_temp2_fault.dev_attr.attr,
0e39e01c
JD
510 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
511 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
512 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
513 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
514 &dev_attr_alarms.attr,
515 NULL
516};
517
518static const struct attribute_group lm90_group = {
519 .attrs = lm90_attributes,
520};
521
c3df5806
JD
522/* pec used for ADM1032 only */
523static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
524 char *buf)
525{
526 struct i2c_client *client = to_i2c_client(dev);
527 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
528}
529
530static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
531 const char *buf, size_t count)
532{
533 struct i2c_client *client = to_i2c_client(dev);
534 long val = simple_strtol(buf, NULL, 10);
535
536 switch (val) {
537 case 0:
538 client->flags &= ~I2C_CLIENT_PEC;
539 break;
540 case 1:
541 client->flags |= I2C_CLIENT_PEC;
542 break;
543 default:
544 return -EINVAL;
545 }
546
547 return count;
548}
549
550static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
551
1da177e4
LT
552/*
553 * Real code
554 */
555
c3df5806 556/* The ADM1032 supports PEC but not on write byte transactions, so we need
0966415d 557 to explicitly ask for a transaction without PEC. */
c3df5806
JD
558static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
559{
560 return i2c_smbus_xfer(client->adapter, client->addr,
561 client->flags & ~I2C_CLIENT_PEC,
562 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
563}
564
565/* It is assumed that client->update_lock is held (unless we are in
566 detection or initialization steps). This matters when PEC is enabled,
567 because we don't want the address pointer to change between the write
568 byte and the read byte transactions. */
8256fe0f
JD
569static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
570{
571 int err;
572
c3df5806
JD
573 if (client->flags & I2C_CLIENT_PEC) {
574 err = adm1032_write_byte(client, reg);
575 if (err >= 0)
576 err = i2c_smbus_read_byte(client);
577 } else
578 err = i2c_smbus_read_byte_data(client, reg);
8256fe0f
JD
579
580 if (err < 0) {
581 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
582 reg, err);
583 return err;
584 }
585 *value = err;
586
587 return 0;
588}
589
9b0e8526
JD
590/* Return 0 if detection is successful, -ENODEV otherwise */
591static int lm90_detect(struct i2c_client *new_client, int kind,
592 struct i2c_board_info *info)
1da177e4 593{
9b0e8526
JD
594 struct i2c_adapter *adapter = new_client->adapter;
595 int address = new_client->addr;
1da177e4
LT
596 const char *name = "";
597
598 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
9b0e8526 599 return -ENODEV;
1da177e4
LT
600
601 /*
602 * Now we do the remaining detection. A negative kind means that
603 * the driver was loaded with no force parameter (default), so we
604 * must both detect and identify the chip. A zero kind means that
605 * the driver was loaded with the force parameter, the detection
606 * step shall be skipped. A positive kind means that the driver
607 * was loaded with the force parameter and a given kind of chip is
608 * requested, so both the detection and the identification steps
609 * are skipped.
610 */
611
612 /* Default to an LM90 if forced */
613 if (kind == 0)
614 kind = lm90;
615
616 if (kind < 0) { /* detection and identification */
e0ae87a4
JD
617 int man_id, chip_id, reg_config1, reg_convrate;
618
619 if ((man_id = i2c_smbus_read_byte_data(new_client,
620 LM90_REG_R_MAN_ID)) < 0
621 || (chip_id = i2c_smbus_read_byte_data(new_client,
622 LM90_REG_R_CHIP_ID)) < 0
623 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
624 LM90_REG_R_CONFIG1)) < 0
625 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
626 LM90_REG_R_CONVRATE)) < 0)
9b0e8526 627 return -ENODEV;
1da177e4 628
32c82a93
RB
629 if ((address == 0x4C || address == 0x4D)
630 && man_id == 0x01) { /* National Semiconductor */
e0ae87a4 631 int reg_config2;
1da177e4 632
e0ae87a4
JD
633 if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
634 LM90_REG_R_CONFIG2)) < 0)
9b0e8526 635 return -ENODEV;
1da177e4
LT
636
637 if ((reg_config1 & 0x2A) == 0x00
638 && (reg_config2 & 0xF8) == 0x00
639 && reg_convrate <= 0x09) {
640 if (address == 0x4C
641 && (chip_id & 0xF0) == 0x20) { /* LM90 */
642 kind = lm90;
643 } else
644 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
645 kind = lm99;
646 } else
647 if (address == 0x4C
648 && (chip_id & 0xF0) == 0x10) { /* LM86 */
649 kind = lm86;
650 }
651 }
652 } else
32c82a93
RB
653 if ((address == 0x4C || address == 0x4D)
654 && man_id == 0x41) { /* Analog Devices */
90209b42 655 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1da177e4
LT
656 && (reg_config1 & 0x3F) == 0x00
657 && reg_convrate <= 0x0A) {
658 kind = adm1032;
659 } else
90209b42 660 if (chip_id == 0x51 /* ADT7461 */
23b2d477 661 && (reg_config1 & 0x1B) == 0x00
1da177e4
LT
662 && reg_convrate <= 0x0A) {
663 kind = adt7461;
664 }
665 } else
666 if (man_id == 0x4D) { /* Maxim */
667 /*
32c82a93
RB
668 * The MAX6657, MAX6658 and MAX6659 do NOT have a
669 * chip_id register. Reading from that address will
670 * return the last read value, which in our case is
671 * those of the man_id register. Likewise, the config1
672 * register seems to lack a low nibble, so the value
673 * will be those of the previous read, so in our case
674 * those of the man_id register.
1da177e4
LT
675 */
676 if (chip_id == man_id
f5744e37 677 && (address == 0x4C || address == 0x4D)
1da177e4
LT
678 && (reg_config1 & 0x1F) == (man_id & 0x0F)
679 && reg_convrate <= 0x09) {
680 kind = max6657;
32c82a93
RB
681 } else
682 /* The chip_id register of the MAX6680 and MAX6681
683 * holds the revision of the chip.
684 * the lowest bit of the config1 register is unused
685 * and should return zero when read, so should the
686 * second to last bit of config1 (software reset)
687 */
688 if (chip_id == 0x01
689 && (reg_config1 & 0x03) == 0x00
690 && reg_convrate <= 0x07) {
691 kind = max6680;
1da177e4
LT
692 }
693 }
694
695 if (kind <= 0) { /* identification failed */
696 dev_info(&adapter->dev,
697 "Unsupported chip (man_id=0x%02X, "
698 "chip_id=0x%02X).\n", man_id, chip_id);
9b0e8526 699 return -ENODEV;
1da177e4
LT
700 }
701 }
702
9b0e8526 703 /* Fill the i2c board info */
1da177e4
LT
704 if (kind == lm90) {
705 name = "lm90";
706 } else if (kind == adm1032) {
707 name = "adm1032";
c3df5806
JD
708 /* The ADM1032 supports PEC, but only if combined
709 transactions are not used. */
710 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
9b0e8526 711 info->flags |= I2C_CLIENT_PEC;
1da177e4
LT
712 } else if (kind == lm99) {
713 name = "lm99";
714 } else if (kind == lm86) {
715 name = "lm86";
716 } else if (kind == max6657) {
717 name = "max6657";
32c82a93
RB
718 } else if (kind == max6680) {
719 name = "max6680";
1da177e4
LT
720 } else if (kind == adt7461) {
721 name = "adt7461";
722 }
9b0e8526
JD
723 strlcpy(info->type, name, I2C_NAME_SIZE);
724
725 return 0;
726}
727
728static int lm90_probe(struct i2c_client *new_client,
729 const struct i2c_device_id *id)
730{
731 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
732 struct lm90_data *data;
733 int err;
1da177e4 734
9b0e8526
JD
735 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
736 if (!data) {
737 err = -ENOMEM;
738 goto exit;
739 }
740 i2c_set_clientdata(new_client, data);
9a61bf63 741 mutex_init(&data->update_lock);
1da177e4 742
9b0e8526
JD
743 /* Set the device type */
744 data->kind = id->driver_data;
745 if (data->kind == adm1032) {
746 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
747 new_client->flags &= ~I2C_CLIENT_PEC;
748 }
1da177e4
LT
749
750 /* Initialize the LM90 chip */
751 lm90_init_client(new_client);
752
753 /* Register sysfs hooks */
0e39e01c 754 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
9b0e8526 755 goto exit_free;
0e39e01c
JD
756 if (new_client->flags & I2C_CLIENT_PEC) {
757 if ((err = device_create_file(&new_client->dev,
758 &dev_attr_pec)))
759 goto exit_remove_files;
760 }
69f2f96d
JD
761 if (data->kind != max6657) {
762 if ((err = device_create_file(&new_client->dev,
763 &sensor_dev_attr_temp2_offset.dev_attr)))
764 goto exit_remove_files;
765 }
0e39e01c 766
1beeffe4
TJ
767 data->hwmon_dev = hwmon_device_register(&new_client->dev);
768 if (IS_ERR(data->hwmon_dev)) {
769 err = PTR_ERR(data->hwmon_dev);
0e39e01c 770 goto exit_remove_files;
943b0830
MH
771 }
772
1da177e4
LT
773 return 0;
774
0e39e01c
JD
775exit_remove_files:
776 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
777 device_remove_file(&new_client->dev, &dev_attr_pec);
1da177e4
LT
778exit_free:
779 kfree(data);
780exit:
781 return err;
782}
783
784static void lm90_init_client(struct i2c_client *client)
785{
32c82a93
RB
786 u8 config, config_orig;
787 struct lm90_data *data = i2c_get_clientdata(client);
1da177e4
LT
788
789 /*
790 * Start the conversions.
791 */
792 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
793 5); /* 2 Hz */
8256fe0f
JD
794 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
795 dev_warn(&client->dev, "Initialization failed!\n");
796 return;
797 }
32c82a93
RB
798 config_orig = config;
799
23b2d477
NC
800 /* Check Temperature Range Select */
801 if (data->kind == adt7461) {
802 if (config & 0x04)
803 data->flags |= LM90_FLAG_ADT7461_EXT;
804 }
805
32c82a93
RB
806 /*
807 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
808 * 0.125 degree resolution) and range (0x08, extend range
809 * to -64 degree) mode for the remote temperature sensor.
810 */
811 if (data->kind == max6680) {
812 config |= 0x18;
813 }
814
815 config &= 0xBF; /* run */
816 if (config != config_orig) /* Only write if changed */
817 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1da177e4
LT
818}
819
9b0e8526 820static int lm90_remove(struct i2c_client *client)
1da177e4 821{
943b0830 822 struct lm90_data *data = i2c_get_clientdata(client);
1da177e4 823
1beeffe4 824 hwmon_device_unregister(data->hwmon_dev);
0e39e01c
JD
825 sysfs_remove_group(&client->dev.kobj, &lm90_group);
826 device_remove_file(&client->dev, &dev_attr_pec);
69f2f96d
JD
827 if (data->kind != max6657)
828 device_remove_file(&client->dev,
829 &sensor_dev_attr_temp2_offset.dev_attr);
943b0830 830
943b0830 831 kfree(data);
1da177e4
LT
832 return 0;
833}
834
6388a388
JD
835static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
836{
837 int err;
838 u8 oldh, newh, l;
839
840 /*
841 * There is a trick here. We have to read two registers to have the
842 * sensor temperature, but we have to beware a conversion could occur
843 * inbetween the readings. The datasheet says we should either use
844 * the one-shot conversion register, which we don't want to do
845 * (disables hardware monitoring) or monitor the busy bit, which is
846 * impossible (we can't read the values and monitor that bit at the
847 * exact same time). So the solution used here is to read the high
848 * byte once, then the low byte, then the high byte again. If the new
849 * high byte matches the old one, then we have a valid reading. Else
850 * we have to read the low byte again, and now we believe we have a
851 * correct reading.
852 */
853 if ((err = lm90_read_reg(client, regh, &oldh))
854 || (err = lm90_read_reg(client, regl, &l))
855 || (err = lm90_read_reg(client, regh, &newh)))
856 return err;
857 if (oldh != newh) {
858 err = lm90_read_reg(client, regl, &l);
859 if (err)
860 return err;
861 }
862 *value = (newh << 8) | l;
863
864 return 0;
865}
866
1da177e4
LT
867static struct lm90_data *lm90_update_device(struct device *dev)
868{
869 struct i2c_client *client = to_i2c_client(dev);
870 struct lm90_data *data = i2c_get_clientdata(client);
871
9a61bf63 872 mutex_lock(&data->update_lock);
1da177e4
LT
873
874 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
6388a388 875 u8 h, l;
1da177e4
LT
876
877 dev_dbg(&client->dev, "Updating lm90 data.\n");
f65e1708
JD
878 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
879 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
880 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
881 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
8256fe0f 882 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
1da177e4 883
f65e1708
JD
884 if (data->kind == max6657) {
885 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
886 MAX6657_REG_R_LOCAL_TEMPL,
887 &data->temp11[4]);
888 } else {
889 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
890 &h) == 0)
891 data->temp11[4] = h << 8;
892 }
6388a388
JD
893 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
894 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
895
5f502a83
JD
896 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
897 data->temp11[1] = h << 8;
898 if (data->kind != max6657 && data->kind != max6680
899 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
900 &l) == 0)
901 data->temp11[1] |= l;
902 }
903 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
904 data->temp11[2] = h << 8;
905 if (data->kind != max6657 && data->kind != max6680
906 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
907 &l) == 0)
908 data->temp11[2] |= l;
909 }
910
69f2f96d
JD
911 if (data->kind != max6657) {
912 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
6388a388 913 &h) == 0
69f2f96d
JD
914 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
915 &l) == 0)
6388a388 916 data->temp11[3] = (h << 8) | l;
69f2f96d 917 }
8256fe0f 918 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
1da177e4
LT
919
920 data->last_updated = jiffies;
921 data->valid = 1;
922 }
923
9a61bf63 924 mutex_unlock(&data->update_lock);
1da177e4
LT
925
926 return data;
927}
928
929static int __init sensors_lm90_init(void)
930{
931 return i2c_add_driver(&lm90_driver);
932}
933
934static void __exit sensors_lm90_exit(void)
935{
936 i2c_del_driver(&lm90_driver);
937}
938
939MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
940MODULE_DESCRIPTION("LM90/ADM1032 driver");
941MODULE_LICENSE("GPL");
942
943module_init(sensors_lm90_init);
944module_exit(sensors_lm90_exit);
This page took 0.548625 seconds and 5 git commands to generate.