blob: a400d4c4416bc6ca2d87328733a5a2f59daab4a0 [file] [log] [blame]
Alexander Steinfffd80c2011-06-28 15:11:23 +00001/*
2 * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
3 *
4 * The LM95245 is a sensor chip made by National Semiconductors.
5 * It reports up to two temperatures (its own plus an external one).
6 * Complete datasheet can be obtained from National's website at:
7 * http://www.national.com/ds.cgi/LM/LM95245.pdf
8 *
9 * This driver is based on lm95241.c
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
31#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
33#include <linux/err.h>
34#include <linux/mutex.h>
35#include <linux/sysfs.h>
36
37#define DEVNAME "lm95245"
38
39static const unsigned short normal_i2c[] = {
40 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
41
42/* LM95245 registers */
43/* general registers */
44#define LM95245_REG_RW_CONFIG1 0x03
45#define LM95245_REG_RW_CONVERS_RATE 0x04
46#define LM95245_REG_W_ONE_SHOT 0x0F
47
48/* diode configuration */
49#define LM95245_REG_RW_CONFIG2 0xBF
50#define LM95245_REG_RW_REMOTE_OFFH 0x11
51#define LM95245_REG_RW_REMOTE_OFFL 0x12
52
53/* status registers */
54#define LM95245_REG_R_STATUS1 0x02
55#define LM95245_REG_R_STATUS2 0x33
56
57/* limit registers */
58#define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
59#define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
60#define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
61#define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
62
63/* temperature signed */
64#define LM95245_REG_R_LOCAL_TEMPH_S 0x00
65#define LM95245_REG_R_LOCAL_TEMPL_S 0x30
66#define LM95245_REG_R_REMOTE_TEMPH_S 0x01
67#define LM95245_REG_R_REMOTE_TEMPL_S 0x10
68/* temperature unsigned */
69#define LM95245_REG_R_REMOTE_TEMPH_U 0x31
70#define LM95245_REG_R_REMOTE_TEMPL_U 0x32
71
72/* id registers */
73#define LM95245_REG_R_MAN_ID 0xFE
74#define LM95245_REG_R_CHIP_ID 0xFF
75
76/* LM95245 specific bitfields */
77#define CFG_STOP 0x40
78#define CFG_REMOTE_TCRIT_MASK 0x10
79#define CFG_REMOTE_OS_MASK 0x08
80#define CFG_LOCAL_TCRIT_MASK 0x04
81#define CFG_LOCAL_OS_MASK 0x02
82
83#define CFG2_OS_A0 0x40
84#define CFG2_DIODE_FAULT_OS 0x20
85#define CFG2_DIODE_FAULT_TCRIT 0x10
86#define CFG2_REMOTE_TT 0x08
87#define CFG2_REMOTE_FILTER_DIS 0x00
88#define CFG2_REMOTE_FILTER_EN 0x06
89
90/* conversation rate in ms */
91#define RATE_CR0063 0x00
92#define RATE_CR0364 0x01
93#define RATE_CR1000 0x02
94#define RATE_CR2500 0x03
95
96#define STATUS1_DIODE_FAULT 0x04
97#define STATUS1_RTCRIT 0x02
98#define STATUS1_LOC 0x01
99
100#define MANUFACTURER_ID 0x01
101#define DEFAULT_REVISION 0xB3
102
103static const u8 lm95245_reg_address[] = {
104 LM95245_REG_R_LOCAL_TEMPH_S,
105 LM95245_REG_R_LOCAL_TEMPL_S,
106 LM95245_REG_R_REMOTE_TEMPH_S,
107 LM95245_REG_R_REMOTE_TEMPL_S,
108 LM95245_REG_R_REMOTE_TEMPH_U,
109 LM95245_REG_R_REMOTE_TEMPL_U,
110 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
111 LM95245_REG_RW_REMOTE_TCRIT_LIMIT,
112 LM95245_REG_RW_COMMON_HYSTERESIS,
113 LM95245_REG_R_STATUS1,
114};
115
116/* Client data (each client gets its own) */
117struct lm95245_data {
118 struct device *hwmon_dev;
119 struct mutex update_lock;
120 unsigned long last_updated; /* in jiffies */
121 unsigned long interval; /* in msecs */
122 bool valid; /* zero until following fields are valid */
123 /* registers values */
124 u8 regs[ARRAY_SIZE(lm95245_reg_address)];
125 u8 config1, config2;
126};
127
128/* Conversions */
129static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
130{
131 return val_h * 1000 + val_l * 1000 / 256;
132}
133
134static int temp_from_reg_signed(u8 val_h, u8 val_l)
135{
136 if (val_h & 0x80)
137 return (val_h - 0x100) * 1000;
138 return temp_from_reg_unsigned(val_h, val_l);
139}
140
141static struct lm95245_data *lm95245_update_device(struct device *dev)
142{
143 struct i2c_client *client = to_i2c_client(dev);
144 struct lm95245_data *data = i2c_get_clientdata(client);
145
146 mutex_lock(&data->update_lock);
147
148 if (time_after(jiffies, data->last_updated
149 + msecs_to_jiffies(data->interval)) || !data->valid) {
150 int i;
151
152 dev_dbg(&client->dev, "Updating lm95245 data.\n");
153 for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++)
154 data->regs[i]
155 = i2c_smbus_read_byte_data(client,
156 lm95245_reg_address[i]);
157 data->last_updated = jiffies;
158 data->valid = 1;
159 }
160
161 mutex_unlock(&data->update_lock);
162
163 return data;
164}
165
166static unsigned long lm95245_read_conversion_rate(struct i2c_client *client)
167{
168 int rate;
169 unsigned long interval;
170
171 rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE);
172
173 switch (rate) {
174 case RATE_CR0063:
175 interval = 63;
176 break;
177 case RATE_CR0364:
178 interval = 364;
179 break;
180 case RATE_CR1000:
181 interval = 1000;
182 break;
183 case RATE_CR2500:
184 default:
185 interval = 2500;
186 break;
187 }
188
189 return interval;
190}
191
192static unsigned long lm95245_set_conversion_rate(struct i2c_client *client,
193 unsigned long interval)
194{
195 int rate;
196
197 if (interval <= 63) {
198 interval = 63;
199 rate = RATE_CR0063;
200 } else if (interval <= 364) {
201 interval = 364;
202 rate = RATE_CR0364;
203 } else if (interval <= 1000) {
204 interval = 1000;
205 rate = RATE_CR1000;
206 } else {
207 interval = 2500;
208 rate = RATE_CR2500;
209 }
210
211 i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate);
212
213 return interval;
214}
215
216/* Sysfs stuff */
217static ssize_t show_input(struct device *dev, struct device_attribute *attr,
218 char *buf)
219{
220 struct lm95245_data *data = lm95245_update_device(dev);
221 int temp;
222 int index = to_sensor_dev_attr(attr)->index;
223
224 /*
225 * Index 0 (Local temp) is always signed
226 * Index 2 (Remote temp) has both signed and unsigned data
227 * use signed calculation for remote if signed bit is set
228 */
229 if (index == 0 || data->regs[index] & 0x80)
230 temp = temp_from_reg_signed(data->regs[index],
231 data->regs[index + 1]);
232 else
233 temp = temp_from_reg_unsigned(data->regs[index + 2],
234 data->regs[index + 3]);
235
236 return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp);
237}
238
239static ssize_t show_limit(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 struct lm95245_data *data = lm95245_update_device(dev);
243 int index = to_sensor_dev_attr(attr)->index;
244
245 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
246 data->regs[index] * 1000);
247}
248
249static ssize_t set_limit(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t count)
251{
252 struct i2c_client *client = to_i2c_client(dev);
253 struct lm95245_data *data = i2c_get_clientdata(client);
254 int index = to_sensor_dev_attr(attr)->index;
255 unsigned long val;
256
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100257 if (kstrtoul(buf, 10, &val) < 0)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000258 return -EINVAL;
259
260 val /= 1000;
261
Guenter Roeck2a844c12013-01-09 08:09:34 -0800262 val = clamp_val(val, 0, (index == 6 ? 127 : 255));
Alexander Steinfffd80c2011-06-28 15:11:23 +0000263
264 mutex_lock(&data->update_lock);
265
266 data->valid = 0;
267
268 i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val);
269
270 mutex_unlock(&data->update_lock);
271
272 return count;
273}
274
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800275static ssize_t show_crit_hyst(struct device *dev, struct device_attribute *attr,
276 char *buf)
277{
278 struct lm95245_data *data = lm95245_update_device(dev);
279 int index = to_sensor_dev_attr(attr)->index;
280 int hyst = data->regs[index] - data->regs[8];
281
282 return snprintf(buf, PAGE_SIZE - 1, "%d\n", hyst * 1000);
283}
284
Alexander Steinfffd80c2011-06-28 15:11:23 +0000285static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t count)
287{
288 struct i2c_client *client = to_i2c_client(dev);
289 struct lm95245_data *data = i2c_get_clientdata(client);
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800290 int index = to_sensor_dev_attr(attr)->index;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000291 unsigned long val;
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800292 int hyst, limit;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000293
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100294 if (kstrtoul(buf, 10, &val) < 0)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000295 return -EINVAL;
296
Alexander Steinfffd80c2011-06-28 15:11:23 +0000297 mutex_lock(&data->update_lock);
298
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800299 limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]);
300 hyst = limit - val / 1000;
301 hyst = clamp_val(hyst, 0, 31);
302 data->regs[8] = hyst;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000303
304 /* shared crit hysteresis */
305 i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS,
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800306 hyst);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000307
308 mutex_unlock(&data->update_lock);
309
310 return count;
311}
312
313static ssize_t show_type(struct device *dev, struct device_attribute *attr,
314 char *buf)
315{
316 struct i2c_client *client = to_i2c_client(dev);
317 struct lm95245_data *data = i2c_get_clientdata(client);
318
319 return snprintf(buf, PAGE_SIZE - 1,
320 data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n");
321}
322
323static ssize_t set_type(struct device *dev, struct device_attribute *attr,
324 const char *buf, size_t count)
325{
326 struct i2c_client *client = to_i2c_client(dev);
327 struct lm95245_data *data = i2c_get_clientdata(client);
328 unsigned long val;
329
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100330 if (kstrtoul(buf, 10, &val) < 0)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000331 return -EINVAL;
332 if (val != 1 && val != 2)
333 return -EINVAL;
334
335 mutex_lock(&data->update_lock);
336
337 if (val == 1)
338 data->config2 |= CFG2_REMOTE_TT;
339 else
340 data->config2 &= ~CFG2_REMOTE_TT;
341
342 data->valid = 0;
343
344 i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2,
345 data->config2);
346
347 mutex_unlock(&data->update_lock);
348
349 return count;
350}
351
352static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
353 char *buf)
354{
355 struct lm95245_data *data = lm95245_update_device(dev);
356 int index = to_sensor_dev_attr(attr)->index;
357
358 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
359 !!(data->regs[9] & index));
360}
361
362static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
363 char *buf)
364{
365 struct lm95245_data *data = lm95245_update_device(dev);
366
367 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
368}
369
370static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
371 const char *buf, size_t count)
372{
373 struct i2c_client *client = to_i2c_client(dev);
374 struct lm95245_data *data = i2c_get_clientdata(client);
375 unsigned long val;
376
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100377 if (kstrtoul(buf, 10, &val) < 0)
Alexander Steinfffd80c2011-06-28 15:11:23 +0000378 return -EINVAL;
379
380 mutex_lock(&data->update_lock);
381
382 data->interval = lm95245_set_conversion_rate(client, val);
383
384 mutex_unlock(&data->update_lock);
385
386 return count;
387}
388
389static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
390static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit,
391 set_limit, 6);
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800392static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst,
393 set_crit_hyst, 6);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000394static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
395 STATUS1_LOC);
396
397static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
398static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit,
399 set_limit, 7);
Guenter Roeck408c15ea2014-02-02 18:29:28 -0800400static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst,
401 set_crit_hyst, 7);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000402static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
403 STATUS1_RTCRIT);
404static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type,
405 set_type, 0);
406static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
407 STATUS1_DIODE_FAULT);
408
409static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
410 set_interval);
411
412static struct attribute *lm95245_attributes[] = {
413 &sensor_dev_attr_temp1_input.dev_attr.attr,
414 &sensor_dev_attr_temp1_crit.dev_attr.attr,
415 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
416 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
417 &sensor_dev_attr_temp2_input.dev_attr.attr,
418 &sensor_dev_attr_temp2_crit.dev_attr.attr,
419 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
420 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
421 &sensor_dev_attr_temp2_type.dev_attr.attr,
422 &sensor_dev_attr_temp2_fault.dev_attr.attr,
423 &dev_attr_update_interval.attr,
424 NULL
425};
426
427static const struct attribute_group lm95245_group = {
428 .attrs = lm95245_attributes,
429};
430
431/* Return 0 if detection is successful, -ENODEV otherwise */
432static int lm95245_detect(struct i2c_client *new_client,
433 struct i2c_board_info *info)
434{
435 struct i2c_adapter *adapter = new_client->adapter;
436
437 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
438 return -ENODEV;
439
440 if (i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID)
441 != MANUFACTURER_ID
442 || i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID)
443 != DEFAULT_REVISION)
444 return -ENODEV;
445
446 strlcpy(info->type, DEVNAME, I2C_NAME_SIZE);
447 return 0;
448}
449
450static void lm95245_init_client(struct i2c_client *client)
451{
452 struct lm95245_data *data = i2c_get_clientdata(client);
453
Alexander Steinfffd80c2011-06-28 15:11:23 +0000454 data->interval = lm95245_read_conversion_rate(client);
455
456 data->config1 = i2c_smbus_read_byte_data(client,
457 LM95245_REG_RW_CONFIG1);
458 data->config2 = i2c_smbus_read_byte_data(client,
459 LM95245_REG_RW_CONFIG2);
460
461 if (data->config1 & CFG_STOP) {
462 /* Clear the standby bit */
463 data->config1 &= ~CFG_STOP;
464 i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1,
465 data->config1);
466 }
467}
468
469static int lm95245_probe(struct i2c_client *new_client,
470 const struct i2c_device_id *id)
471{
472 struct lm95245_data *data;
473 int err;
474
Guenter Roecka8dd9462012-06-02 09:58:12 -0700475 data = devm_kzalloc(&new_client->dev, sizeof(struct lm95245_data),
476 GFP_KERNEL);
477 if (!data)
478 return -ENOMEM;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000479
480 i2c_set_clientdata(new_client, data);
481 mutex_init(&data->update_lock);
482
483 /* Initialize the LM95245 chip */
484 lm95245_init_client(new_client);
485
486 /* Register sysfs hooks */
487 err = sysfs_create_group(&new_client->dev.kobj, &lm95245_group);
488 if (err)
Guenter Roecka8dd9462012-06-02 09:58:12 -0700489 return err;
Alexander Steinfffd80c2011-06-28 15:11:23 +0000490
491 data->hwmon_dev = hwmon_device_register(&new_client->dev);
492 if (IS_ERR(data->hwmon_dev)) {
493 err = PTR_ERR(data->hwmon_dev);
494 goto exit_remove_files;
495 }
496
497 return 0;
498
499exit_remove_files:
500 sysfs_remove_group(&new_client->dev.kobj, &lm95245_group);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000501 return err;
502}
503
504static int lm95245_remove(struct i2c_client *client)
505{
506 struct lm95245_data *data = i2c_get_clientdata(client);
507
508 hwmon_device_unregister(data->hwmon_dev);
509 sysfs_remove_group(&client->dev.kobj, &lm95245_group);
510
Alexander Steinfffd80c2011-06-28 15:11:23 +0000511 return 0;
512}
513
514/* Driver data (common to all clients) */
515static const struct i2c_device_id lm95245_id[] = {
516 { DEVNAME, 0 },
517 { }
518};
519MODULE_DEVICE_TABLE(i2c, lm95245_id);
520
521static struct i2c_driver lm95245_driver = {
522 .class = I2C_CLASS_HWMON,
523 .driver = {
524 .name = DEVNAME,
525 },
526 .probe = lm95245_probe,
527 .remove = lm95245_remove,
528 .id_table = lm95245_id,
529 .detect = lm95245_detect,
530 .address_list = normal_i2c,
531};
532
Axel Linf0967ee2012-01-20 15:38:18 +0800533module_i2c_driver(lm95245_driver);
Alexander Steinfffd80c2011-06-28 15:11:23 +0000534
535MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
536MODULE_DESCRIPTION("LM95245 sensor driver");
537MODULE_LICENSE("GPL");