blob: 906b896cf1d0fa6616ce5a8750f54f8e1937bf75 [file] [log] [blame]
Davide Rizzo06160322009-03-31 15:24:27 -07001/*
2 * lm95241.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2008 Davide Rizzo <elpa-rizzo@gmail.com>
5 *
6 * Based on the max1619 driver. The LM95241 is a sensor chip made by National
7 * Semiconductors.
8 * It reports up to three temperatures (its own plus up to
9 * two external ones). Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/ds.cgi/LM/LM95241.pdf
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
37#include <linux/sysfs.h>
38
39static const unsigned short normal_i2c[] = {
40 0x19, 0x2a, 0x2b, I2C_CLIENT_END};
41
42/* Insmod parameters */
43I2C_CLIENT_INSMOD_1(lm95241);
44
45/* LM95241 registers */
46#define LM95241_REG_R_MAN_ID 0xFE
47#define LM95241_REG_R_CHIP_ID 0xFF
48#define LM95241_REG_R_STATUS 0x02
49#define LM95241_REG_RW_CONFIG 0x03
50#define LM95241_REG_RW_REM_FILTER 0x06
51#define LM95241_REG_RW_TRUTHERM 0x07
52#define LM95241_REG_W_ONE_SHOT 0x0F
53#define LM95241_REG_R_LOCAL_TEMPH 0x10
54#define LM95241_REG_R_REMOTE1_TEMPH 0x11
55#define LM95241_REG_R_REMOTE2_TEMPH 0x12
56#define LM95241_REG_R_LOCAL_TEMPL 0x20
57#define LM95241_REG_R_REMOTE1_TEMPL 0x21
58#define LM95241_REG_R_REMOTE2_TEMPL 0x22
59#define LM95241_REG_RW_REMOTE_MODEL 0x30
60
61/* LM95241 specific bitfields */
62#define CFG_STOP 0x40
63#define CFG_CR0076 0x00
64#define CFG_CR0182 0x10
65#define CFG_CR1000 0x20
66#define CFG_CR2700 0x30
67#define R1MS_SHIFT 0
68#define R2MS_SHIFT 2
69#define R1MS_MASK (0x01 << (R1MS_SHIFT))
70#define R2MS_MASK (0x01 << (R2MS_SHIFT))
71#define R1DF_SHIFT 1
72#define R2DF_SHIFT 2
73#define R1DF_MASK (0x01 << (R1DF_SHIFT))
74#define R2DF_MASK (0x01 << (R2DF_SHIFT))
75#define R1FE_MASK 0x01
76#define R2FE_MASK 0x05
77#define TT1_SHIFT 0
78#define TT2_SHIFT 4
79#define TT_OFF 0
80#define TT_ON 1
81#define TT_MASK 7
82#define MANUFACTURER_ID 0x01
83#define DEFAULT_REVISION 0xA4
84
85/* Conversions and various macros */
86#define TEMP_FROM_REG(val_h, val_l) (((val_h) & 0x80 ? (val_h) - 0x100 : \
87 (val_h)) * 1000 + (val_l) * 1000 / 256)
88
89/* Functions declaration */
Davide Rizzo06160322009-03-31 15:24:27 -070090static void lm95241_init_client(struct i2c_client *client);
Davide Rizzo06160322009-03-31 15:24:27 -070091static struct lm95241_data *lm95241_update_device(struct device *dev);
92
Davide Rizzo06160322009-03-31 15:24:27 -070093/* Client data (each client gets its own) */
94struct lm95241_data {
Davide Rizzo06160322009-03-31 15:24:27 -070095 struct device *hwmon_dev;
96 struct mutex update_lock;
97 unsigned long last_updated, rate; /* in jiffies */
98 char valid; /* zero until following fields are valid */
99 /* registers values */
100 u8 local_h, local_l; /* local */
101 u8 remote1_h, remote1_l; /* remote1 */
102 u8 remote2_h, remote2_l; /* remote2 */
103 u8 config, model, trutherm;
104};
105
106/* Sysfs stuff */
107#define show_temp(value) \
108static ssize_t show_##value(struct device *dev, \
109 struct device_attribute *attr, char *buf) \
110{ \
111 struct lm95241_data *data = lm95241_update_device(dev); \
112 snprintf(buf, PAGE_SIZE - 1, "%d\n", \
113 TEMP_FROM_REG(data->value##_h, data->value##_l)); \
114 return strlen(buf); \
115}
116show_temp(local);
117show_temp(remote1);
118show_temp(remote2);
119
120static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
121 char *buf)
122{
123 struct lm95241_data *data = lm95241_update_device(dev);
124
125 snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->rate / HZ);
126 return strlen(buf);
127}
128
129static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
130 const char *buf, size_t count)
131{
132 struct i2c_client *client = to_i2c_client(dev);
133 struct lm95241_data *data = i2c_get_clientdata(client);
134
135 strict_strtol(buf, 10, &data->rate);
136 data->rate = data->rate * HZ / 1000;
137
138 return count;
139}
140
141#define show_type(flag) \
142static ssize_t show_type##flag(struct device *dev, \
143 struct device_attribute *attr, char *buf) \
144{ \
145 struct i2c_client *client = to_i2c_client(dev); \
146 struct lm95241_data *data = i2c_get_clientdata(client); \
147\
148 snprintf(buf, PAGE_SIZE - 1, \
149 data->model & R##flag##MS_MASK ? "1\n" : "2\n"); \
150 return strlen(buf); \
151}
152show_type(1);
153show_type(2);
154
155#define show_min(flag) \
156static ssize_t show_min##flag(struct device *dev, \
157 struct device_attribute *attr, char *buf) \
158{ \
159 struct i2c_client *client = to_i2c_client(dev); \
160 struct lm95241_data *data = i2c_get_clientdata(client); \
161\
162 snprintf(buf, PAGE_SIZE - 1, \
163 data->config & R##flag##DF_MASK ? \
164 "-127000\n" : "0\n"); \
165 return strlen(buf); \
166}
167show_min(1);
168show_min(2);
169
170#define show_max(flag) \
171static ssize_t show_max##flag(struct device *dev, \
172 struct device_attribute *attr, char *buf) \
173{ \
174 struct i2c_client *client = to_i2c_client(dev); \
175 struct lm95241_data *data = i2c_get_clientdata(client); \
176\
177 snprintf(buf, PAGE_SIZE - 1, \
178 data->config & R##flag##DF_MASK ? \
179 "127000\n" : "255000\n"); \
180 return strlen(buf); \
181}
182show_max(1);
183show_max(2);
184
185#define set_type(flag) \
186static ssize_t set_type##flag(struct device *dev, \
187 struct device_attribute *attr, \
188 const char *buf, size_t count) \
189{ \
190 struct i2c_client *client = to_i2c_client(dev); \
191 struct lm95241_data *data = i2c_get_clientdata(client); \
192\
193 long val; \
194 strict_strtol(buf, 10, &val); \
195\
196 if ((val == 1) || (val == 2)) { \
197\
198 mutex_lock(&data->update_lock); \
199\
200 data->trutherm &= ~(TT_MASK << TT##flag##_SHIFT); \
201 if (val == 1) { \
202 data->model |= R##flag##MS_MASK; \
203 data->trutherm |= (TT_ON << TT##flag##_SHIFT); \
204 } \
205 else { \
206 data->model &= ~R##flag##MS_MASK; \
207 data->trutherm |= (TT_OFF << TT##flag##_SHIFT); \
208 } \
209\
210 data->valid = 0; \
211\
212 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, \
213 data->model); \
214 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, \
215 data->trutherm); \
216\
217 mutex_unlock(&data->update_lock); \
218\
219 } \
220 return count; \
221}
222set_type(1);
223set_type(2);
224
225#define set_min(flag) \
226static ssize_t set_min##flag(struct device *dev, \
227 struct device_attribute *devattr, const char *buf, size_t count) \
228{ \
229 struct i2c_client *client = to_i2c_client(dev); \
230 struct lm95241_data *data = i2c_get_clientdata(client); \
231\
232 long val; \
233 strict_strtol(buf, 10, &val); \
234\
235 mutex_lock(&data->update_lock); \
236\
237 if (val < 0) \
238 data->config |= R##flag##DF_MASK; \
239 else \
240 data->config &= ~R##flag##DF_MASK; \
241\
242 data->valid = 0; \
243\
244 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
245 data->config); \
246\
247 mutex_unlock(&data->update_lock); \
248\
249 return count; \
250}
251set_min(1);
252set_min(2);
253
254#define set_max(flag) \
255static ssize_t set_max##flag(struct device *dev, \
256 struct device_attribute *devattr, const char *buf, size_t count) \
257{ \
258 struct i2c_client *client = to_i2c_client(dev); \
259 struct lm95241_data *data = i2c_get_clientdata(client); \
260\
261 long val; \
262 strict_strtol(buf, 10, &val); \
263\
264 mutex_lock(&data->update_lock); \
265\
266 if (val <= 127000) \
267 data->config |= R##flag##DF_MASK; \
268 else \
269 data->config &= ~R##flag##DF_MASK; \
270\
271 data->valid = 0; \
272\
273 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
274 data->config); \
275\
276 mutex_unlock(&data->update_lock); \
277\
278 return count; \
279}
280set_max(1);
281set_max(2);
282
283static DEVICE_ATTR(temp1_input, S_IRUGO, show_local, NULL);
284static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote1, NULL);
285static DEVICE_ATTR(temp3_input, S_IRUGO, show_remote2, NULL);
286static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type1, set_type1);
287static DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type2, set_type2);
288static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min1, set_min1);
289static DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min2, set_min2);
290static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max1, set_max1);
291static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max2, set_max2);
292static DEVICE_ATTR(rate, S_IWUSR | S_IRUGO, show_rate, set_rate);
293
294static struct attribute *lm95241_attributes[] = {
295 &dev_attr_temp1_input.attr,
296 &dev_attr_temp2_input.attr,
297 &dev_attr_temp3_input.attr,
298 &dev_attr_temp2_type.attr,
299 &dev_attr_temp3_type.attr,
300 &dev_attr_temp2_min.attr,
301 &dev_attr_temp3_min.attr,
302 &dev_attr_temp2_max.attr,
303 &dev_attr_temp3_max.attr,
304 &dev_attr_rate.attr,
305 NULL
306};
307
308static const struct attribute_group lm95241_group = {
309 .attrs = lm95241_attributes,
310};
311
Jean Delvare797eaa42009-04-07 15:32:59 +0200312/* Return 0 if detection is successful, -ENODEV otherwise */
313static int lm95241_detect(struct i2c_client *new_client, int kind,
314 struct i2c_board_info *info)
Davide Rizzo06160322009-03-31 15:24:27 -0700315{
Jean Delvare797eaa42009-04-07 15:32:59 +0200316 struct i2c_adapter *adapter = new_client->adapter;
317 int address = new_client->addr;
Jean Delvare52df6442009-12-09 20:35:57 +0100318 const char *name;
Davide Rizzo06160322009-03-31 15:24:27 -0700319
320 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare797eaa42009-04-07 15:32:59 +0200321 return -ENODEV;
Davide Rizzo06160322009-03-31 15:24:27 -0700322
Jean Delvare52df6442009-12-09 20:35:57 +0100323 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
324 == MANUFACTURER_ID)
325 && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
326 >= DEFAULT_REVISION)) {
327 name = "lm95241";
328 } else {
329 dev_dbg(&adapter->dev, "LM95241 detection failed at 0x%02x\n",
330 address);
331 return -ENODEV;
Davide Rizzo06160322009-03-31 15:24:27 -0700332 }
333
Jean Delvare797eaa42009-04-07 15:32:59 +0200334 /* Fill the i2c board info */
Jean Delvare797eaa42009-04-07 15:32:59 +0200335 strlcpy(info->type, name, I2C_NAME_SIZE);
336 return 0;
337}
Davide Rizzo06160322009-03-31 15:24:27 -0700338
Jean Delvare797eaa42009-04-07 15:32:59 +0200339static int lm95241_probe(struct i2c_client *new_client,
340 const struct i2c_device_id *id)
341{
342 struct lm95241_data *data;
343 int err;
344
345 data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
346 if (!data) {
347 err = -ENOMEM;
348 goto exit;
349 }
350
351 i2c_set_clientdata(new_client, data);
Davide Rizzo06160322009-03-31 15:24:27 -0700352 mutex_init(&data->update_lock);
353
Davide Rizzo06160322009-03-31 15:24:27 -0700354 /* Initialize the LM95241 chip */
355 lm95241_init_client(new_client);
356
357 /* Register sysfs hooks */
358 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
359 if (err)
Jean Delvare797eaa42009-04-07 15:32:59 +0200360 goto exit_free;
Davide Rizzo06160322009-03-31 15:24:27 -0700361
362 data->hwmon_dev = hwmon_device_register(&new_client->dev);
363 if (IS_ERR(data->hwmon_dev)) {
364 err = PTR_ERR(data->hwmon_dev);
365 goto exit_remove_files;
366 }
367
368 return 0;
369
370exit_remove_files:
371 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
Davide Rizzo06160322009-03-31 15:24:27 -0700372exit_free:
373 kfree(data);
374exit:
375 return err;
376}
377
378static void lm95241_init_client(struct i2c_client *client)
379{
380 struct lm95241_data *data = i2c_get_clientdata(client);
381
382 data->rate = HZ; /* 1 sec default */
383 data->valid = 0;
384 data->config = CFG_CR0076;
385 data->model = 0;
386 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
387
388 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
389 data->config);
390 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
391 R1FE_MASK | R2FE_MASK);
392 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
393 data->trutherm);
394 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
395 data->model);
396}
397
Jean Delvare797eaa42009-04-07 15:32:59 +0200398static int lm95241_remove(struct i2c_client *client)
Davide Rizzo06160322009-03-31 15:24:27 -0700399{
400 struct lm95241_data *data = i2c_get_clientdata(client);
Davide Rizzo06160322009-03-31 15:24:27 -0700401
402 hwmon_device_unregister(data->hwmon_dev);
403 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
404
Jean Delvare797eaa42009-04-07 15:32:59 +0200405 i2c_set_clientdata(client, NULL);
Davide Rizzo06160322009-03-31 15:24:27 -0700406 kfree(data);
407 return 0;
408}
409
410static struct lm95241_data *lm95241_update_device(struct device *dev)
411{
412 struct i2c_client *client = to_i2c_client(dev);
413 struct lm95241_data *data = i2c_get_clientdata(client);
414
415 mutex_lock(&data->update_lock);
416
417 if (time_after(jiffies, data->last_updated + data->rate) ||
418 !data->valid) {
419 dev_dbg(&client->dev, "Updating lm95241 data.\n");
420 data->local_h =
421 i2c_smbus_read_byte_data(client,
422 LM95241_REG_R_LOCAL_TEMPH);
423 data->local_l =
424 i2c_smbus_read_byte_data(client,
425 LM95241_REG_R_LOCAL_TEMPL);
426 data->remote1_h =
427 i2c_smbus_read_byte_data(client,
428 LM95241_REG_R_REMOTE1_TEMPH);
429 data->remote1_l =
430 i2c_smbus_read_byte_data(client,
431 LM95241_REG_R_REMOTE1_TEMPL);
432 data->remote2_h =
433 i2c_smbus_read_byte_data(client,
434 LM95241_REG_R_REMOTE2_TEMPH);
435 data->remote2_l =
436 i2c_smbus_read_byte_data(client,
437 LM95241_REG_R_REMOTE2_TEMPL);
438 data->last_updated = jiffies;
439 data->valid = 1;
440 }
441
442 mutex_unlock(&data->update_lock);
443
444 return data;
445}
446
Jean Delvare797eaa42009-04-07 15:32:59 +0200447/* Driver data (common to all clients) */
448static const struct i2c_device_id lm95241_id[] = {
449 { "lm95241", lm95241 },
450 { }
451};
452MODULE_DEVICE_TABLE(i2c, lm95241_id);
453
454static struct i2c_driver lm95241_driver = {
455 .class = I2C_CLASS_HWMON,
456 .driver = {
457 .name = "lm95241",
458 },
459 .probe = lm95241_probe,
460 .remove = lm95241_remove,
461 .id_table = lm95241_id,
462 .detect = lm95241_detect,
463 .address_data = &addr_data,
464};
465
Davide Rizzo06160322009-03-31 15:24:27 -0700466static int __init sensors_lm95241_init(void)
467{
468 return i2c_add_driver(&lm95241_driver);
469}
470
471static void __exit sensors_lm95241_exit(void)
472{
473 i2c_del_driver(&lm95241_driver);
474}
475
476MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
477MODULE_DESCRIPTION("LM95241 sensor driver");
478MODULE_LICENSE("GPL");
479
480module_init(sensors_lm95241_init);
481module_exit(sensors_lm95241_exit);