blob: 1a6dfb6df1e7ff121ce4903ab80f1b2e57ba2ef1 [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 */
101static int TempFromReg(u8 val_h, u8 val_l)
102{
103 if (val_h & 0x80)
104 return val_h - 0x100;
105 return val_h * 1000 + val_l * 1000 / 256;
Davide Rizzo06160322009-03-31 15:24:27 -0700106}
Davide Rizzo06160322009-03-31 15:24:27 -0700107
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800108static struct lm95241_data *lm95241_update_device(struct device *dev)
109{
110 struct i2c_client *client = to_i2c_client(dev);
111 struct lm95241_data *data = i2c_get_clientdata(client);
112
113 mutex_lock(&data->update_lock);
114
115 if (time_after(jiffies, data->last_updated + data->interval) ||
116 !data->valid) {
117 int i;
118
119 dev_dbg(&client->dev, "Updating lm95241 data.\n");
120 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
121 data->temp[i]
122 = i2c_smbus_read_byte_data(client,
123 lm95241_reg_address[i]);
124 data->last_updated = jiffies;
125 data->valid = 1;
126 }
127
128 mutex_unlock(&data->update_lock);
129
130 return data;
131}
132
133/* Sysfs stuff */
134static ssize_t show_input(struct device *dev, struct device_attribute *attr,
135 char *buf)
Davide Rizzo06160322009-03-31 15:24:27 -0700136{
137 struct lm95241_data *data = lm95241_update_device(dev);
138
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800139 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
140 TempFromReg(data->temp[to_sensor_dev_attr(attr)->index],
141 data->temp[to_sensor_dev_attr(attr)->index + 1]));
142}
143
144static ssize_t show_type(struct device *dev, struct device_attribute *attr,
145 char *buf)
146{
147 struct i2c_client *client = to_i2c_client(dev);
148 struct lm95241_data *data = i2c_get_clientdata(client);
149
150 return snprintf(buf, PAGE_SIZE - 1,
151 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
152}
153
154static ssize_t set_type(struct device *dev, struct device_attribute *attr,
155 const char *buf, size_t count)
156{
157 struct i2c_client *client = to_i2c_client(dev);
158 struct lm95241_data *data = i2c_get_clientdata(client);
159 unsigned long val;
160 int shift;
161 u8 mask = to_sensor_dev_attr(attr)->index;
162
163 if (strict_strtoul(buf, 10, &val) < 0)
164 return -EINVAL;
165 if (val != 1 && val != 2)
166 return -EINVAL;
167
168 shift = mask == R1MS_MASK ? TT1_SHIFT : TT2_SHIFT;
169
170 mutex_lock(&data->update_lock);
171
172 data->trutherm &= ~(TT_MASK << shift);
173 if (val == 1) {
174 data->model |= mask;
175 data->trutherm |= (TT_ON << shift);
176 } else {
177 data->model &= ~mask;
178 data->trutherm |= (TT_OFF << shift);
179 }
180 data->valid = 0;
181
182 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
183 data->model);
184 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
185 data->trutherm);
186
187 mutex_unlock(&data->update_lock);
188
189 return count;
190}
191
192static ssize_t show_min(struct device *dev, struct device_attribute *attr,
193 char *buf)
194{
195 struct i2c_client *client = to_i2c_client(dev);
196 struct lm95241_data *data = i2c_get_clientdata(client);
197
198 return snprintf(buf, PAGE_SIZE - 1,
199 data->config & to_sensor_dev_attr(attr)->index ?
200 "-127000\n" : "0\n");
201}
202
203static ssize_t set_min(struct device *dev, struct device_attribute *attr,
204 const char *buf, size_t count)
205{
206 struct i2c_client *client = to_i2c_client(dev);
207 struct lm95241_data *data = i2c_get_clientdata(client);
208 long val;
209
210 if (strict_strtol(buf, 10, &val) < 0)
211 return -EINVAL;
212 if (val < -128000)
213 return -EINVAL;
214
215 mutex_lock(&data->update_lock);
216
217 if (val < 0)
218 data->config |= to_sensor_dev_attr(attr)->index;
219 else
220 data->config &= ~to_sensor_dev_attr(attr)->index;
221 data->valid = 0;
222
223 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
224
225 mutex_unlock(&data->update_lock);
226
227 return count;
228}
229
230static ssize_t show_max(struct device *dev, struct device_attribute *attr,
231 char *buf)
232{
233 struct i2c_client *client = to_i2c_client(dev);
234 struct lm95241_data *data = i2c_get_clientdata(client);
235
236 return snprintf(buf, PAGE_SIZE - 1,
237 data->config & to_sensor_dev_attr(attr)->index ?
238 "127000\n" : "255000\n");
239}
240
241static ssize_t set_max(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
243{
244 struct i2c_client *client = to_i2c_client(dev);
245 struct lm95241_data *data = i2c_get_clientdata(client);
246 long val;
247
248 if (strict_strtol(buf, 10, &val) < 0)
249 return -EINVAL;
250 if (val >= 256000)
251 return -EINVAL;
252
253 mutex_lock(&data->update_lock);
254
255 if (val <= 127000)
256 data->config |= to_sensor_dev_attr(attr)->index;
257 else
258 data->config &= ~to_sensor_dev_attr(attr)->index;
259 data->valid = 0;
260
261 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
262
263 mutex_unlock(&data->update_lock);
264
265 return count;
266}
267
268static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
269 char *buf)
270{
271 struct lm95241_data *data = lm95241_update_device(dev);
272
273 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
274 / HZ);
Davide Rizzo06160322009-03-31 15:24:27 -0700275}
276
Guenter Roeckbc482bf2010-09-17 17:24:15 +0200277static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800278 const char *buf, size_t count)
Davide Rizzo06160322009-03-31 15:24:27 -0700279{
280 struct i2c_client *client = to_i2c_client(dev);
281 struct lm95241_data *data = i2c_get_clientdata(client);
Jean Delvare61ec2da2010-11-15 21:38:56 +0100282 unsigned long val;
Davide Rizzo06160322009-03-31 15:24:27 -0700283
Jean Delvare61ec2da2010-11-15 21:38:56 +0100284 if (strict_strtoul(buf, 10, &val) < 0)
285 return -EINVAL;
286
287 data->interval = val * HZ / 1000;
Davide Rizzo06160322009-03-31 15:24:27 -0700288
289 return count;
290}
291
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800292static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
293static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
294static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4);
295static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
296 R1MS_MASK);
297static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
298 R2MS_MASK);
299static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min,
300 R1DF_MASK);
301static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min,
302 R2DF_MASK);
303static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
304 R1DF_MASK);
305static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
306 R2DF_MASK);
Guenter Roeckbc482bf2010-09-17 17:24:15 +0200307static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
308 set_interval);
Davide Rizzo06160322009-03-31 15:24:27 -0700309
310static struct attribute *lm95241_attributes[] = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800311 &sensor_dev_attr_temp1_input.dev_attr.attr,
312 &sensor_dev_attr_temp2_input.dev_attr.attr,
313 &sensor_dev_attr_temp3_input.dev_attr.attr,
314 &sensor_dev_attr_temp2_type.dev_attr.attr,
315 &sensor_dev_attr_temp3_type.dev_attr.attr,
316 &sensor_dev_attr_temp2_min.dev_attr.attr,
317 &sensor_dev_attr_temp3_min.dev_attr.attr,
318 &sensor_dev_attr_temp2_max.dev_attr.attr,
319 &sensor_dev_attr_temp3_max.dev_attr.attr,
Guenter Roeckbc482bf2010-09-17 17:24:15 +0200320 &dev_attr_update_interval.attr,
Davide Rizzo06160322009-03-31 15:24:27 -0700321 NULL
322};
323
324static const struct attribute_group lm95241_group = {
325 .attrs = lm95241_attributes,
326};
327
Jean Delvare797eaa42009-04-07 15:32:59 +0200328/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100329static int lm95241_detect(struct i2c_client *new_client,
Jean Delvare797eaa42009-04-07 15:32:59 +0200330 struct i2c_board_info *info)
Davide Rizzo06160322009-03-31 15:24:27 -0700331{
Jean Delvare797eaa42009-04-07 15:32:59 +0200332 struct i2c_adapter *adapter = new_client->adapter;
333 int address = new_client->addr;
Jean Delvare52df6442009-12-09 20:35:57 +0100334 const char *name;
Davide Rizzo06160322009-03-31 15:24:27 -0700335
336 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare797eaa42009-04-07 15:32:59 +0200337 return -ENODEV;
Davide Rizzo06160322009-03-31 15:24:27 -0700338
Jean Delvare52df6442009-12-09 20:35:57 +0100339 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
340 == MANUFACTURER_ID)
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800341 && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
342 >= DEFAULT_REVISION)) {
343 name = DEVNAME;
Jean Delvare52df6442009-12-09 20:35:57 +0100344 } else {
345 dev_dbg(&adapter->dev, "LM95241 detection failed at 0x%02x\n",
346 address);
347 return -ENODEV;
Davide Rizzo06160322009-03-31 15:24:27 -0700348 }
349
Jean Delvare797eaa42009-04-07 15:32:59 +0200350 /* Fill the i2c board info */
Jean Delvare797eaa42009-04-07 15:32:59 +0200351 strlcpy(info->type, name, I2C_NAME_SIZE);
352 return 0;
353}
Davide Rizzo06160322009-03-31 15:24:27 -0700354
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800355static void lm95241_init_client(struct i2c_client *client)
356{
357 struct lm95241_data *data = i2c_get_clientdata(client);
358
359 data->interval = HZ; /* 1 sec default */
360 data->valid = 0;
361 data->config = CFG_CR0076;
362 data->model = 0;
363 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
364
365 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
366 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
367 R1FE_MASK | R2FE_MASK);
368 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
369 data->trutherm);
370 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
371 data->model);
372}
373
Jean Delvare797eaa42009-04-07 15:32:59 +0200374static int lm95241_probe(struct i2c_client *new_client,
375 const struct i2c_device_id *id)
376{
377 struct lm95241_data *data;
378 int err;
379
380 data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
381 if (!data) {
382 err = -ENOMEM;
383 goto exit;
384 }
385
386 i2c_set_clientdata(new_client, data);
Davide Rizzo06160322009-03-31 15:24:27 -0700387 mutex_init(&data->update_lock);
388
Davide Rizzo06160322009-03-31 15:24:27 -0700389 /* Initialize the LM95241 chip */
390 lm95241_init_client(new_client);
391
392 /* Register sysfs hooks */
393 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
394 if (err)
Jean Delvare797eaa42009-04-07 15:32:59 +0200395 goto exit_free;
Davide Rizzo06160322009-03-31 15:24:27 -0700396
397 data->hwmon_dev = hwmon_device_register(&new_client->dev);
398 if (IS_ERR(data->hwmon_dev)) {
399 err = PTR_ERR(data->hwmon_dev);
400 goto exit_remove_files;
401 }
402
403 return 0;
404
405exit_remove_files:
406 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
Davide Rizzo06160322009-03-31 15:24:27 -0700407exit_free:
408 kfree(data);
409exit:
410 return err;
411}
412
Jean Delvare797eaa42009-04-07 15:32:59 +0200413static int lm95241_remove(struct i2c_client *client)
Davide Rizzo06160322009-03-31 15:24:27 -0700414{
415 struct lm95241_data *data = i2c_get_clientdata(client);
Davide Rizzo06160322009-03-31 15:24:27 -0700416
417 hwmon_device_unregister(data->hwmon_dev);
418 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
419
Davide Rizzo06160322009-03-31 15:24:27 -0700420 kfree(data);
421 return 0;
422}
423
Jean Delvare797eaa42009-04-07 15:32:59 +0200424/* Driver data (common to all clients) */
425static const struct i2c_device_id lm95241_id[] = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800426 { DEVNAME, 0 },
Jean Delvare797eaa42009-04-07 15:32:59 +0200427 { }
428};
429MODULE_DEVICE_TABLE(i2c, lm95241_id);
430
431static struct i2c_driver lm95241_driver = {
432 .class = I2C_CLASS_HWMON,
433 .driver = {
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800434 .name = DEVNAME,
Jean Delvare797eaa42009-04-07 15:32:59 +0200435 },
436 .probe = lm95241_probe,
437 .remove = lm95241_remove,
438 .id_table = lm95241_id,
439 .detect = lm95241_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100440 .address_list = normal_i2c,
Jean Delvare797eaa42009-04-07 15:32:59 +0200441};
442
Davide Rizzo06160322009-03-31 15:24:27 -0700443static int __init sensors_lm95241_init(void)
444{
445 return i2c_add_driver(&lm95241_driver);
446}
447
448static void __exit sensors_lm95241_exit(void)
449{
450 i2c_del_driver(&lm95241_driver);
451}
452
Davide Rizzo0f1deb42010-11-18 07:23:00 -0800453MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
Davide Rizzo06160322009-03-31 15:24:27 -0700454MODULE_DESCRIPTION("LM95241 sensor driver");
455MODULE_LICENSE("GPL");
456
457module_init(sensors_lm95241_init);
458module_exit(sensors_lm95241_exit);