blob: d3b464b74ced3b743969e2f0377523b0883243f2 [file] [log] [blame]
Davide Rizzo06160322009-03-31 15:24:27 -07001/*
Davide Rizzo0f1deb42010-11-18 07:23:00 -08002 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
Davide Rizzo06160322009-03-31 15:24:27 -07003 *
Davide Rizzo0f1deb42010-11-18 07:23:00 -08004 * The LM95241 is a sensor chip made by National Semiconductors.
5 * It reports up to three temperatures (its own plus up to two external ones).
6 * Complete datasheet can be obtained from National's website at:
Davide Rizzo06160322009-03-31 15:24:27 -07007 * http://www.national.com/ds.cgi/LM/LM95241.pdf
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31#include <linux/err.h>
32#include <linux/mutex.h>
33#include <linux/sysfs.h>
34
Davide Rizzo0f1deb42010-11-18 07:23:00 -080035#define DEVNAME "lm95241"
36
Davide Rizzo06160322009-03-31 15:24:27 -070037static const unsigned short normal_i2c[] = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -080038 0x19, 0x2a, 0x2b, I2C_CLIENT_END };
Davide Rizzo06160322009-03-31 15:24:27 -070039
Davide Rizzo06160322009-03-31 15:24:27 -070040/* LM95241 registers */
41#define LM95241_REG_R_MAN_ID 0xFE
42#define LM95241_REG_R_CHIP_ID 0xFF
43#define LM95241_REG_R_STATUS 0x02
44#define LM95241_REG_RW_CONFIG 0x03
45#define LM95241_REG_RW_REM_FILTER 0x06
46#define LM95241_REG_RW_TRUTHERM 0x07
Davide Rizzo0f1deb42010-11-18 07:23:00 -080047#define LM95241_REG_W_ONE_SHOT 0x0F
Davide Rizzo06160322009-03-31 15:24:27 -070048#define LM95241_REG_R_LOCAL_TEMPH 0x10
49#define LM95241_REG_R_REMOTE1_TEMPH 0x11
50#define LM95241_REG_R_REMOTE2_TEMPH 0x12
51#define LM95241_REG_R_LOCAL_TEMPL 0x20
52#define LM95241_REG_R_REMOTE1_TEMPL 0x21
53#define LM95241_REG_R_REMOTE2_TEMPL 0x22
54#define LM95241_REG_RW_REMOTE_MODEL 0x30
55
56/* LM95241 specific bitfields */
57#define CFG_STOP 0x40
58#define CFG_CR0076 0x00
59#define CFG_CR0182 0x10
60#define CFG_CR1000 0x20
61#define CFG_CR2700 0x30
62#define R1MS_SHIFT 0
63#define R2MS_SHIFT 2
64#define R1MS_MASK (0x01 << (R1MS_SHIFT))
65#define R2MS_MASK (0x01 << (R2MS_SHIFT))
66#define R1DF_SHIFT 1
67#define R2DF_SHIFT 2
68#define R1DF_MASK (0x01 << (R1DF_SHIFT))
69#define R2DF_MASK (0x01 << (R2DF_SHIFT))
70#define R1FE_MASK 0x01
71#define R2FE_MASK 0x05
72#define TT1_SHIFT 0
73#define TT2_SHIFT 4
74#define TT_OFF 0
75#define TT_ON 1
76#define TT_MASK 7
77#define MANUFACTURER_ID 0x01
78#define DEFAULT_REVISION 0xA4
79
Davide Rizzo0f1deb42010-11-18 07:23:00 -080080static const u8 lm95241_reg_address[] = {
81 LM95241_REG_R_LOCAL_TEMPH,
82 LM95241_REG_R_LOCAL_TEMPL,
83 LM95241_REG_R_REMOTE1_TEMPH,
84 LM95241_REG_R_REMOTE1_TEMPL,
85 LM95241_REG_R_REMOTE2_TEMPH,
86 LM95241_REG_R_REMOTE2_TEMPL
87};
Davide Rizzo06160322009-03-31 15:24:27 -070088
Davide Rizzo06160322009-03-31 15:24:27 -070089/* Client data (each client gets its own) */
90struct lm95241_data {
Davide Rizzo06160322009-03-31 15:24:27 -070091 struct device *hwmon_dev;
92 struct mutex update_lock;
Davide Rizzo0f1deb42010-11-18 07:23:00 -080093 unsigned long last_updated, interval; /* in jiffies */
94 char valid; /* zero until following fields are valid */
Davide Rizzo06160322009-03-31 15:24:27 -070095 /* registers values */
Davide Rizzo0f1deb42010-11-18 07:23:00 -080096 u8 temp[ARRAY_SIZE(lm95241_reg_address)];
Davide Rizzo06160322009-03-31 15:24:27 -070097 u8 config, model, trutherm;
98};
99
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800100/* Conversions */
Guenter Roeck0c2a40e2011-06-30 02:09:37 -0700101static int temp_from_reg_signed(u8 val_h, u8 val_l)
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800102{
Guenter Roeck0c2a40e2011-06-30 02:09:37 -0700103 s16 val_hl = (val_h << 8) | val_l;
104 return val_hl * 1000 / 256;
105}
106
107static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
108{
109 u16 val_hl = (val_h << 8) | val_l;
110 return val_hl * 1000 / 256;
Davide Rizzo06160322009-03-31 15:24:27 -0700111}
Davide Rizzo06160322009-03-31 15:24:27 -0700112
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800113static struct lm95241_data *lm95241_update_device(struct device *dev)
114{
115 struct i2c_client *client = to_i2c_client(dev);
116 struct lm95241_data *data = i2c_get_clientdata(client);
117
118 mutex_lock(&data->update_lock);
119
120 if (time_after(jiffies, data->last_updated + data->interval) ||
121 !data->valid) {
122 int i;
123
124 dev_dbg(&client->dev, "Updating lm95241 data.\n");
125 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
126 data->temp[i]
127 = i2c_smbus_read_byte_data(client,
128 lm95241_reg_address[i]);
129 data->last_updated = jiffies;
130 data->valid = 1;
131 }
132
133 mutex_unlock(&data->update_lock);
134
135 return data;
136}
137
138/* Sysfs stuff */
139static ssize_t show_input(struct device *dev, struct device_attribute *attr,
140 char *buf)
Davide Rizzo06160322009-03-31 15:24:27 -0700141{
142 struct lm95241_data *data = lm95241_update_device(dev);
Guenter Roeck0c2a40e2011-06-30 02:09:37 -0700143 int index = to_sensor_dev_attr(attr)->index;
Davide Rizzo06160322009-03-31 15:24:27 -0700144
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800145 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
Guenter Roeck0c2a40e2011-06-30 02:09:37 -0700146 index == 0 || (data->config & (1 << (index / 2))) ?
147 temp_from_reg_signed(data->temp[index], data->temp[index + 1]) :
148 temp_from_reg_unsigned(data->temp[index],
149 data->temp[index + 1]));
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800150}
151
152static ssize_t show_type(struct device *dev, struct device_attribute *attr,
153 char *buf)
154{
155 struct i2c_client *client = to_i2c_client(dev);
156 struct lm95241_data *data = i2c_get_clientdata(client);
157
158 return snprintf(buf, PAGE_SIZE - 1,
159 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
160}
161
162static ssize_t set_type(struct device *dev, struct device_attribute *attr,
163 const char *buf, size_t count)
164{
165 struct i2c_client *client = to_i2c_client(dev);
166 struct lm95241_data *data = i2c_get_clientdata(client);
167 unsigned long val;
168 int shift;
169 u8 mask = to_sensor_dev_attr(attr)->index;
170
171 if (strict_strtoul(buf, 10, &val) < 0)
172 return -EINVAL;
173 if (val != 1 && val != 2)
174 return -EINVAL;
175
176 shift = mask == R1MS_MASK ? TT1_SHIFT : TT2_SHIFT;
177
178 mutex_lock(&data->update_lock);
179
180 data->trutherm &= ~(TT_MASK << shift);
181 if (val == 1) {
182 data->model |= mask;
183 data->trutherm |= (TT_ON << shift);
184 } else {
185 data->model &= ~mask;
186 data->trutherm |= (TT_OFF << shift);
187 }
188 data->valid = 0;
189
190 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
191 data->model);
192 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
193 data->trutherm);
194
195 mutex_unlock(&data->update_lock);
196
197 return count;
198}
199
200static ssize_t show_min(struct device *dev, struct device_attribute *attr,
201 char *buf)
202{
203 struct i2c_client *client = to_i2c_client(dev);
204 struct lm95241_data *data = i2c_get_clientdata(client);
205
206 return snprintf(buf, PAGE_SIZE - 1,
207 data->config & to_sensor_dev_attr(attr)->index ?
208 "-127000\n" : "0\n");
209}
210
211static ssize_t set_min(struct device *dev, struct device_attribute *attr,
212 const char *buf, size_t count)
213{
214 struct i2c_client *client = to_i2c_client(dev);
215 struct lm95241_data *data = i2c_get_clientdata(client);
216 long val;
217
218 if (strict_strtol(buf, 10, &val) < 0)
219 return -EINVAL;
220 if (val < -128000)
221 return -EINVAL;
222
223 mutex_lock(&data->update_lock);
224
225 if (val < 0)
226 data->config |= to_sensor_dev_attr(attr)->index;
227 else
228 data->config &= ~to_sensor_dev_attr(attr)->index;
229 data->valid = 0;
230
231 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
232
233 mutex_unlock(&data->update_lock);
234
235 return count;
236}
237
238static ssize_t show_max(struct device *dev, struct device_attribute *attr,
239 char *buf)
240{
241 struct i2c_client *client = to_i2c_client(dev);
242 struct lm95241_data *data = i2c_get_clientdata(client);
243
244 return snprintf(buf, PAGE_SIZE - 1,
245 data->config & to_sensor_dev_attr(attr)->index ?
246 "127000\n" : "255000\n");
247}
248
249static ssize_t set_max(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 lm95241_data *data = i2c_get_clientdata(client);
254 long val;
255
256 if (strict_strtol(buf, 10, &val) < 0)
257 return -EINVAL;
258 if (val >= 256000)
259 return -EINVAL;
260
261 mutex_lock(&data->update_lock);
262
263 if (val <= 127000)
264 data->config |= to_sensor_dev_attr(attr)->index;
265 else
266 data->config &= ~to_sensor_dev_attr(attr)->index;
267 data->valid = 0;
268
269 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
270
271 mutex_unlock(&data->update_lock);
272
273 return count;
274}
275
276static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
277 char *buf)
278{
279 struct lm95241_data *data = lm95241_update_device(dev);
280
281 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
282 / HZ);
Davide Rizzo06160322009-03-31 15:24:27 -0700283}
284
Guenter Roeckbc482bf2010-09-17 17:24:15 +0200285static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800286 const char *buf, size_t count)
Davide Rizzo06160322009-03-31 15:24:27 -0700287{
288 struct i2c_client *client = to_i2c_client(dev);
289 struct lm95241_data *data = i2c_get_clientdata(client);
Jean Delvare61ec2da2010-11-15 21:38:56 +0100290 unsigned long val;
Davide Rizzo06160322009-03-31 15:24:27 -0700291
Jean Delvare61ec2da2010-11-15 21:38:56 +0100292 if (strict_strtoul(buf, 10, &val) < 0)
293 return -EINVAL;
294
295 data->interval = val * HZ / 1000;
Davide Rizzo06160322009-03-31 15:24:27 -0700296
297 return count;
298}
299
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800300static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
301static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
302static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4);
303static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
304 R1MS_MASK);
305static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
306 R2MS_MASK);
307static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min,
308 R1DF_MASK);
309static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min,
310 R2DF_MASK);
311static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
312 R1DF_MASK);
313static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
314 R2DF_MASK);
Guenter Roeckbc482bf2010-09-17 17:24:15 +0200315static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
316 set_interval);
Davide Rizzo06160322009-03-31 15:24:27 -0700317
318static struct attribute *lm95241_attributes[] = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800319 &sensor_dev_attr_temp1_input.dev_attr.attr,
320 &sensor_dev_attr_temp2_input.dev_attr.attr,
321 &sensor_dev_attr_temp3_input.dev_attr.attr,
322 &sensor_dev_attr_temp2_type.dev_attr.attr,
323 &sensor_dev_attr_temp3_type.dev_attr.attr,
324 &sensor_dev_attr_temp2_min.dev_attr.attr,
325 &sensor_dev_attr_temp3_min.dev_attr.attr,
326 &sensor_dev_attr_temp2_max.dev_attr.attr,
327 &sensor_dev_attr_temp3_max.dev_attr.attr,
Guenter Roeckbc482bf2010-09-17 17:24:15 +0200328 &dev_attr_update_interval.attr,
Davide Rizzo06160322009-03-31 15:24:27 -0700329 NULL
330};
331
332static const struct attribute_group lm95241_group = {
333 .attrs = lm95241_attributes,
334};
335
Jean Delvare797eaa42009-04-07 15:32:59 +0200336/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100337static int lm95241_detect(struct i2c_client *new_client,
Jean Delvare797eaa42009-04-07 15:32:59 +0200338 struct i2c_board_info *info)
Davide Rizzo06160322009-03-31 15:24:27 -0700339{
Jean Delvare797eaa42009-04-07 15:32:59 +0200340 struct i2c_adapter *adapter = new_client->adapter;
341 int address = new_client->addr;
Jean Delvare52df6442009-12-09 20:35:57 +0100342 const char *name;
Davide Rizzo06160322009-03-31 15:24:27 -0700343
344 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare797eaa42009-04-07 15:32:59 +0200345 return -ENODEV;
Davide Rizzo06160322009-03-31 15:24:27 -0700346
Jean Delvare52df6442009-12-09 20:35:57 +0100347 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
348 == MANUFACTURER_ID)
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800349 && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
Guenter Roeck27739e62011-06-27 11:22:46 -0700350 == DEFAULT_REVISION)) {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800351 name = DEVNAME;
Jean Delvare52df6442009-12-09 20:35:57 +0100352 } else {
353 dev_dbg(&adapter->dev, "LM95241 detection failed at 0x%02x\n",
354 address);
355 return -ENODEV;
Davide Rizzo06160322009-03-31 15:24:27 -0700356 }
357
Jean Delvare797eaa42009-04-07 15:32:59 +0200358 /* Fill the i2c board info */
Jean Delvare797eaa42009-04-07 15:32:59 +0200359 strlcpy(info->type, name, I2C_NAME_SIZE);
360 return 0;
361}
Davide Rizzo06160322009-03-31 15:24:27 -0700362
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800363static void lm95241_init_client(struct i2c_client *client)
364{
365 struct lm95241_data *data = i2c_get_clientdata(client);
366
367 data->interval = HZ; /* 1 sec default */
368 data->valid = 0;
369 data->config = CFG_CR0076;
370 data->model = 0;
371 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
372
373 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
374 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
375 R1FE_MASK | R2FE_MASK);
376 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
377 data->trutherm);
378 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
379 data->model);
380}
381
Jean Delvare797eaa42009-04-07 15:32:59 +0200382static int lm95241_probe(struct i2c_client *new_client,
383 const struct i2c_device_id *id)
384{
385 struct lm95241_data *data;
386 int err;
387
388 data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
389 if (!data) {
390 err = -ENOMEM;
391 goto exit;
392 }
393
394 i2c_set_clientdata(new_client, data);
Davide Rizzo06160322009-03-31 15:24:27 -0700395 mutex_init(&data->update_lock);
396
Davide Rizzo06160322009-03-31 15:24:27 -0700397 /* Initialize the LM95241 chip */
398 lm95241_init_client(new_client);
399
400 /* Register sysfs hooks */
401 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
402 if (err)
Jean Delvare797eaa42009-04-07 15:32:59 +0200403 goto exit_free;
Davide Rizzo06160322009-03-31 15:24:27 -0700404
405 data->hwmon_dev = hwmon_device_register(&new_client->dev);
406 if (IS_ERR(data->hwmon_dev)) {
407 err = PTR_ERR(data->hwmon_dev);
408 goto exit_remove_files;
409 }
410
411 return 0;
412
413exit_remove_files:
414 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
Davide Rizzo06160322009-03-31 15:24:27 -0700415exit_free:
416 kfree(data);
417exit:
418 return err;
419}
420
Jean Delvare797eaa42009-04-07 15:32:59 +0200421static int lm95241_remove(struct i2c_client *client)
Davide Rizzo06160322009-03-31 15:24:27 -0700422{
423 struct lm95241_data *data = i2c_get_clientdata(client);
Davide Rizzo06160322009-03-31 15:24:27 -0700424
425 hwmon_device_unregister(data->hwmon_dev);
426 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
427
Davide Rizzo06160322009-03-31 15:24:27 -0700428 kfree(data);
429 return 0;
430}
431
Jean Delvare797eaa42009-04-07 15:32:59 +0200432/* Driver data (common to all clients) */
433static const struct i2c_device_id lm95241_id[] = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800434 { DEVNAME, 0 },
Jean Delvare797eaa42009-04-07 15:32:59 +0200435 { }
436};
437MODULE_DEVICE_TABLE(i2c, lm95241_id);
438
439static struct i2c_driver lm95241_driver = {
440 .class = I2C_CLASS_HWMON,
441 .driver = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800442 .name = DEVNAME,
Jean Delvare797eaa42009-04-07 15:32:59 +0200443 },
444 .probe = lm95241_probe,
445 .remove = lm95241_remove,
446 .id_table = lm95241_id,
447 .detect = lm95241_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100448 .address_list = normal_i2c,
Jean Delvare797eaa42009-04-07 15:32:59 +0200449};
450
Davide Rizzo06160322009-03-31 15:24:27 -0700451static int __init sensors_lm95241_init(void)
452{
453 return i2c_add_driver(&lm95241_driver);
454}
455
456static void __exit sensors_lm95241_exit(void)
457{
458 i2c_del_driver(&lm95241_driver);
459}
460
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800461MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
Davide Rizzo06160322009-03-31 15:24:27 -0700462MODULE_DESCRIPTION("LM95241 sensor driver");
463MODULE_LICENSE("GPL");
464
465module_init(sensors_lm95241_init);
466module_exit(sensors_lm95241_exit);