Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / drivers / hwmon / lm95241.c
CommitLineData
06160327 1/*
0f1deb4b 2 * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
06160327 3 *
0f1deb4b
DR
4 * 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:
06160327
DR
7 * 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
0f1deb4b
DR
35#define DEVNAME "lm95241"
36
06160327 37static const unsigned short normal_i2c[] = {
0f1deb4b 38 0x19, 0x2a, 0x2b, I2C_CLIENT_END };
06160327 39
06160327
DR
40/* 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
0f1deb4b 47#define LM95241_REG_W_ONE_SHOT 0x0F
06160327
DR
48#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
8c1d0419
GR
77#define NATSEMI_MAN_ID 0x01
78#define LM95231_CHIP_ID 0xA1
79#define LM95241_CHIP_ID 0xA4
06160327 80
0f1deb4b
DR
81static const u8 lm95241_reg_address[] = {
82 LM95241_REG_R_LOCAL_TEMPH,
83 LM95241_REG_R_LOCAL_TEMPL,
84 LM95241_REG_R_REMOTE1_TEMPH,
85 LM95241_REG_R_REMOTE1_TEMPL,
86 LM95241_REG_R_REMOTE2_TEMPH,
87 LM95241_REG_R_REMOTE2_TEMPL
88};
06160327 89
06160327
DR
90/* Client data (each client gets its own) */
91struct lm95241_data {
f809621e 92 struct i2c_client *client;
06160327 93 struct mutex update_lock;
0f1deb4b
DR
94 unsigned long last_updated, interval; /* in jiffies */
95 char valid; /* zero until following fields are valid */
06160327 96 /* registers values */
0f1deb4b 97 u8 temp[ARRAY_SIZE(lm95241_reg_address)];
06160327
DR
98 u8 config, model, trutherm;
99};
100
0f1deb4b 101/* Conversions */
0c2a40e2 102static int temp_from_reg_signed(u8 val_h, u8 val_l)
0f1deb4b 103{
0c2a40e2
GR
104 s16 val_hl = (val_h << 8) | val_l;
105 return val_hl * 1000 / 256;
106}
107
108static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
109{
110 u16 val_hl = (val_h << 8) | val_l;
111 return val_hl * 1000 / 256;
0f1deb4b
DR
112}
113
114static struct lm95241_data *lm95241_update_device(struct device *dev)
115{
f809621e
GR
116 struct lm95241_data *data = dev_get_drvdata(dev);
117 struct i2c_client *client = data->client;
0f1deb4b
DR
118
119 mutex_lock(&data->update_lock);
120
121 if (time_after(jiffies, data->last_updated + data->interval) ||
122 !data->valid) {
123 int i;
124
f809621e 125 dev_dbg(dev, "Updating lm95241 data.\n");
0f1deb4b
DR
126 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
127 data->temp[i]
128 = i2c_smbus_read_byte_data(client,
129 lm95241_reg_address[i]);
130 data->last_updated = jiffies;
131 data->valid = 1;
132 }
133
134 mutex_unlock(&data->update_lock);
135
136 return data;
137}
138
06160327 139/* Sysfs stuff */
0f1deb4b
DR
140static ssize_t show_input(struct device *dev, struct device_attribute *attr,
141 char *buf)
142{
143 struct lm95241_data *data = lm95241_update_device(dev);
0c2a40e2 144 int index = to_sensor_dev_attr(attr)->index;
0f1deb4b
DR
145
146 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
0c2a40e2
GR
147 index == 0 || (data->config & (1 << (index / 2))) ?
148 temp_from_reg_signed(data->temp[index], data->temp[index + 1]) :
149 temp_from_reg_unsigned(data->temp[index],
150 data->temp[index + 1]));
06160327 151}
06160327 152
0f1deb4b 153static ssize_t show_type(struct device *dev, struct device_attribute *attr,
06160327
DR
154 char *buf)
155{
f809621e 156 struct lm95241_data *data = dev_get_drvdata(dev);
06160327 157
0f1deb4b
DR
158 return snprintf(buf, PAGE_SIZE - 1,
159 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
06160327
DR
160}
161
0f1deb4b 162static ssize_t set_type(struct device *dev, struct device_attribute *attr,
06160327
DR
163 const char *buf, size_t count)
164{
f809621e
GR
165 struct lm95241_data *data = dev_get_drvdata(dev);
166 struct i2c_client *client = data->client;
61ec2da5 167 unsigned long val;
0f1deb4b
DR
168 int shift;
169 u8 mask = to_sensor_dev_attr(attr)->index;
06160327 170
179c4fdb 171 if (kstrtoul(buf, 10, &val) < 0)
61ec2da5 172 return -EINVAL;
0f1deb4b
DR
173 if (val != 1 && val != 2)
174 return -EINVAL;
61ec2da5 175
0f1deb4b
DR
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);
06160327
DR
196
197 return count;
198}
199
0f1deb4b
DR
200static ssize_t show_min(struct device *dev, struct device_attribute *attr,
201 char *buf)
202{
f809621e 203 struct lm95241_data *data = dev_get_drvdata(dev);
0f1deb4b
DR
204
205 return snprintf(buf, PAGE_SIZE - 1,
206 data->config & to_sensor_dev_attr(attr)->index ?
207 "-127000\n" : "0\n");
06160327 208}
0f1deb4b
DR
209
210static ssize_t set_min(struct device *dev, struct device_attribute *attr,
211 const char *buf, size_t count)
212{
f809621e 213 struct lm95241_data *data = dev_get_drvdata(dev);
0f1deb4b
DR
214 long val;
215
179c4fdb 216 if (kstrtol(buf, 10, &val) < 0)
0f1deb4b
DR
217 return -EINVAL;
218 if (val < -128000)
219 return -EINVAL;
220
221 mutex_lock(&data->update_lock);
222
223 if (val < 0)
224 data->config |= to_sensor_dev_attr(attr)->index;
225 else
226 data->config &= ~to_sensor_dev_attr(attr)->index;
227 data->valid = 0;
228
f809621e
GR
229 i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
230 data->config);
0f1deb4b
DR
231
232 mutex_unlock(&data->update_lock);
233
234 return count;
06160327 235}
0f1deb4b
DR
236
237static ssize_t show_max(struct device *dev, struct device_attribute *attr,
238 char *buf)
239{
f809621e 240 struct lm95241_data *data = dev_get_drvdata(dev);
0f1deb4b
DR
241
242 return snprintf(buf, PAGE_SIZE - 1,
243 data->config & to_sensor_dev_attr(attr)->index ?
244 "127000\n" : "255000\n");
06160327 245}
0f1deb4b
DR
246
247static ssize_t set_max(struct device *dev, struct device_attribute *attr,
248 const char *buf, size_t count)
249{
f809621e 250 struct lm95241_data *data = dev_get_drvdata(dev);
0f1deb4b
DR
251 long val;
252
179c4fdb 253 if (kstrtol(buf, 10, &val) < 0)
0f1deb4b
DR
254 return -EINVAL;
255 if (val >= 256000)
256 return -EINVAL;
257
258 mutex_lock(&data->update_lock);
259
260 if (val <= 127000)
261 data->config |= to_sensor_dev_attr(attr)->index;
262 else
263 data->config &= ~to_sensor_dev_attr(attr)->index;
264 data->valid = 0;
265
f809621e
GR
266 i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
267 data->config);
0f1deb4b
DR
268
269 mutex_unlock(&data->update_lock);
270
271 return count;
06160327 272}
0f1deb4b
DR
273
274static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
275 char *buf)
276{
277 struct lm95241_data *data = lm95241_update_device(dev);
278
279 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
280 / HZ);
06160327 281}
0f1deb4b
DR
282
283static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
284 const char *buf, size_t count)
285{
f809621e 286 struct lm95241_data *data = dev_get_drvdata(dev);
0f1deb4b
DR
287 unsigned long val;
288
179c4fdb 289 if (kstrtoul(buf, 10, &val) < 0)
0f1deb4b
DR
290 return -EINVAL;
291
292 data->interval = val * HZ / 1000;
293
294 return count;
06160327 295}
0f1deb4b
DR
296
297static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
298static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
299static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4);
300static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
301 R1MS_MASK);
302static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
303 R2MS_MASK);
304static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min,
305 R1DF_MASK);
306static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min,
307 R2DF_MASK);
308static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
309 R1DF_MASK);
310static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
311 R2DF_MASK);
bc482bf0
GR
312static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
313 set_interval);
06160327 314
f809621e 315static struct attribute *lm95241_attrs[] = {
0f1deb4b
DR
316 &sensor_dev_attr_temp1_input.dev_attr.attr,
317 &sensor_dev_attr_temp2_input.dev_attr.attr,
318 &sensor_dev_attr_temp3_input.dev_attr.attr,
319 &sensor_dev_attr_temp2_type.dev_attr.attr,
320 &sensor_dev_attr_temp3_type.dev_attr.attr,
321 &sensor_dev_attr_temp2_min.dev_attr.attr,
322 &sensor_dev_attr_temp3_min.dev_attr.attr,
323 &sensor_dev_attr_temp2_max.dev_attr.attr,
324 &sensor_dev_attr_temp3_max.dev_attr.attr,
bc482bf0 325 &dev_attr_update_interval.attr,
06160327
DR
326 NULL
327};
f809621e 328ATTRIBUTE_GROUPS(lm95241);
06160327 329
797eaa4b 330/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 331static int lm95241_detect(struct i2c_client *new_client,
797eaa4b 332 struct i2c_board_info *info)
06160327 333{
797eaa4b 334 struct i2c_adapter *adapter = new_client->adapter;
52df6440 335 const char *name;
8c1d0419 336 int mfg_id, chip_id;
06160327
DR
337
338 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
797eaa4b 339 return -ENODEV;
06160327 340
8c1d0419
GR
341 mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID);
342 if (mfg_id != NATSEMI_MAN_ID)
343 return -ENODEV;
344
345 chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID);
346 switch (chip_id) {
347 case LM95231_CHIP_ID:
348 name = "lm95231";
349 break;
350 case LM95241_CHIP_ID:
351 name = "lm95241";
352 break;
353 default:
52df6440 354 return -ENODEV;
06160327
DR
355 }
356
797eaa4b 357 /* Fill the i2c board info */
797eaa4b
JD
358 strlcpy(info->type, name, I2C_NAME_SIZE);
359 return 0;
360}
06160327 361
f809621e
GR
362static void lm95241_init_client(struct i2c_client *client,
363 struct lm95241_data *data)
0f1deb4b 364{
0f1deb4b 365 data->interval = HZ; /* 1 sec default */
0f1deb4b 366 data->config = CFG_CR0076;
0f1deb4b
DR
367 data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
368
369 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
370 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
371 R1FE_MASK | R2FE_MASK);
372 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
373 data->trutherm);
374 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
375 data->model);
376}
377
f809621e 378static int lm95241_probe(struct i2c_client *client,
797eaa4b
JD
379 const struct i2c_device_id *id)
380{
f809621e 381 struct device *dev = &client->dev;
797eaa4b 382 struct lm95241_data *data;
f809621e 383 struct device *hwmon_dev;
06160327 384
f809621e 385 data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
1487bf70
GR
386 if (!data)
387 return -ENOMEM;
797eaa4b 388
f809621e 389 data->client = client;
797eaa4b 390 mutex_init(&data->update_lock);
06160327
DR
391
392 /* Initialize the LM95241 chip */
f809621e 393 lm95241_init_client(client, data);
06160327 394
f809621e
GR
395 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
396 data,
397 lm95241_groups);
398 return PTR_ERR_OR_ZERO(hwmon_dev);
06160327
DR
399}
400
797eaa4b
JD
401/* Driver data (common to all clients) */
402static const struct i2c_device_id lm95241_id[] = {
8c1d0419
GR
403 { "lm95231", 0 },
404 { "lm95241", 0 },
797eaa4b
JD
405 { }
406};
407MODULE_DEVICE_TABLE(i2c, lm95241_id);
408
409static struct i2c_driver lm95241_driver = {
410 .class = I2C_CLASS_HWMON,
411 .driver = {
0f1deb4b 412 .name = DEVNAME,
797eaa4b
JD
413 },
414 .probe = lm95241_probe,
797eaa4b
JD
415 .id_table = lm95241_id,
416 .detect = lm95241_detect,
c3813d6a 417 .address_list = normal_i2c,
797eaa4b
JD
418};
419
f0967eea 420module_i2c_driver(lm95241_driver);
06160327 421
0f1deb4b 422MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
06160327
DR
423MODULE_DESCRIPTION("LM95241 sensor driver");
424MODULE_LICENSE("GPL");