mmc: sdhci: Move sdhci_runtime_pm_bus_off|on() to avoid pre-definition
[linux/fpc-iii.git] / drivers / hwmon / lm95245.c
blobe7aef4561c83f82e32efaf98016c3393f5fd4628
1 /*
2 * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
4 * The LM95245 is a sensor chip made by TI / National Semiconductor.
5 * It reports up to two temperatures (its own plus an external one).
7 * This driver is based on lm95241.c
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.
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.
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.
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>
35 static const unsigned short normal_i2c[] = {
36 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
38 /* LM95245 registers */
39 /* general registers */
40 #define LM95245_REG_RW_CONFIG1 0x03
41 #define LM95245_REG_RW_CONVERS_RATE 0x04
42 #define LM95245_REG_W_ONE_SHOT 0x0F
44 /* diode configuration */
45 #define LM95245_REG_RW_CONFIG2 0xBF
46 #define LM95245_REG_RW_REMOTE_OFFH 0x11
47 #define LM95245_REG_RW_REMOTE_OFFL 0x12
49 /* status registers */
50 #define LM95245_REG_R_STATUS1 0x02
51 #define LM95245_REG_R_STATUS2 0x33
53 /* limit registers */
54 #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
55 #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
56 #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
57 #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
59 /* temperature signed */
60 #define LM95245_REG_R_LOCAL_TEMPH_S 0x00
61 #define LM95245_REG_R_LOCAL_TEMPL_S 0x30
62 #define LM95245_REG_R_REMOTE_TEMPH_S 0x01
63 #define LM95245_REG_R_REMOTE_TEMPL_S 0x10
64 /* temperature unsigned */
65 #define LM95245_REG_R_REMOTE_TEMPH_U 0x31
66 #define LM95245_REG_R_REMOTE_TEMPL_U 0x32
68 /* id registers */
69 #define LM95245_REG_R_MAN_ID 0xFE
70 #define LM95245_REG_R_CHIP_ID 0xFF
72 /* LM95245 specific bitfields */
73 #define CFG_STOP 0x40
74 #define CFG_REMOTE_TCRIT_MASK 0x10
75 #define CFG_REMOTE_OS_MASK 0x08
76 #define CFG_LOCAL_TCRIT_MASK 0x04
77 #define CFG_LOCAL_OS_MASK 0x02
79 #define CFG2_OS_A0 0x40
80 #define CFG2_DIODE_FAULT_OS 0x20
81 #define CFG2_DIODE_FAULT_TCRIT 0x10
82 #define CFG2_REMOTE_TT 0x08
83 #define CFG2_REMOTE_FILTER_DIS 0x00
84 #define CFG2_REMOTE_FILTER_EN 0x06
86 /* conversation rate in ms */
87 #define RATE_CR0063 0x00
88 #define RATE_CR0364 0x01
89 #define RATE_CR1000 0x02
90 #define RATE_CR2500 0x03
92 #define STATUS1_DIODE_FAULT 0x04
93 #define STATUS1_RTCRIT 0x02
94 #define STATUS1_LOC 0x01
96 #define MANUFACTURER_ID 0x01
97 #define LM95235_REVISION 0xB1
98 #define LM95245_REVISION 0xB3
100 static const u8 lm95245_reg_address[] = {
101 LM95245_REG_R_LOCAL_TEMPH_S,
102 LM95245_REG_R_LOCAL_TEMPL_S,
103 LM95245_REG_R_REMOTE_TEMPH_S,
104 LM95245_REG_R_REMOTE_TEMPL_S,
105 LM95245_REG_R_REMOTE_TEMPH_U,
106 LM95245_REG_R_REMOTE_TEMPL_U,
107 LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
108 LM95245_REG_RW_REMOTE_TCRIT_LIMIT,
109 LM95245_REG_RW_COMMON_HYSTERESIS,
110 LM95245_REG_R_STATUS1,
113 /* Client data (each client gets its own) */
114 struct lm95245_data {
115 struct i2c_client *client;
116 struct mutex update_lock;
117 unsigned long last_updated; /* in jiffies */
118 unsigned long interval; /* in msecs */
119 bool valid; /* zero until following fields are valid */
120 /* registers values */
121 u8 regs[ARRAY_SIZE(lm95245_reg_address)];
122 u8 config1, config2;
125 /* Conversions */
126 static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
128 return val_h * 1000 + val_l * 1000 / 256;
131 static int temp_from_reg_signed(u8 val_h, u8 val_l)
133 if (val_h & 0x80)
134 return (val_h - 0x100) * 1000;
135 return temp_from_reg_unsigned(val_h, val_l);
138 static struct lm95245_data *lm95245_update_device(struct device *dev)
140 struct lm95245_data *data = dev_get_drvdata(dev);
141 struct i2c_client *client = data->client;
143 mutex_lock(&data->update_lock);
145 if (time_after(jiffies, data->last_updated
146 + msecs_to_jiffies(data->interval)) || !data->valid) {
147 int i;
149 for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++)
150 data->regs[i]
151 = i2c_smbus_read_byte_data(client,
152 lm95245_reg_address[i]);
153 data->last_updated = jiffies;
154 data->valid = 1;
157 mutex_unlock(&data->update_lock);
159 return data;
162 static unsigned long lm95245_read_conversion_rate(struct i2c_client *client)
164 int rate;
165 unsigned long interval;
167 rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE);
169 switch (rate) {
170 case RATE_CR0063:
171 interval = 63;
172 break;
173 case RATE_CR0364:
174 interval = 364;
175 break;
176 case RATE_CR1000:
177 interval = 1000;
178 break;
179 case RATE_CR2500:
180 default:
181 interval = 2500;
182 break;
185 return interval;
188 static unsigned long lm95245_set_conversion_rate(struct i2c_client *client,
189 unsigned long interval)
191 int rate;
193 if (interval <= 63) {
194 interval = 63;
195 rate = RATE_CR0063;
196 } else if (interval <= 364) {
197 interval = 364;
198 rate = RATE_CR0364;
199 } else if (interval <= 1000) {
200 interval = 1000;
201 rate = RATE_CR1000;
202 } else {
203 interval = 2500;
204 rate = RATE_CR2500;
207 i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate);
209 return interval;
212 /* Sysfs stuff */
213 static ssize_t show_input(struct device *dev, struct device_attribute *attr,
214 char *buf)
216 struct lm95245_data *data = lm95245_update_device(dev);
217 int temp;
218 int index = to_sensor_dev_attr(attr)->index;
221 * Index 0 (Local temp) is always signed
222 * Index 2 (Remote temp) has both signed and unsigned data
223 * use signed calculation for remote if signed bit is set
225 if (index == 0 || data->regs[index] & 0x80)
226 temp = temp_from_reg_signed(data->regs[index],
227 data->regs[index + 1]);
228 else
229 temp = temp_from_reg_unsigned(data->regs[index + 2],
230 data->regs[index + 3]);
232 return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp);
235 static ssize_t show_limit(struct device *dev, struct device_attribute *attr,
236 char *buf)
238 struct lm95245_data *data = lm95245_update_device(dev);
239 int index = to_sensor_dev_attr(attr)->index;
241 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
242 data->regs[index] * 1000);
245 static ssize_t set_limit(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
248 struct lm95245_data *data = dev_get_drvdata(dev);
249 int index = to_sensor_dev_attr(attr)->index;
250 struct i2c_client *client = data->client;
251 unsigned long val;
253 if (kstrtoul(buf, 10, &val) < 0)
254 return -EINVAL;
256 val /= 1000;
258 val = clamp_val(val, 0, (index == 6 ? 127 : 255));
260 mutex_lock(&data->update_lock);
262 data->valid = 0;
264 i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val);
266 mutex_unlock(&data->update_lock);
268 return count;
271 static ssize_t show_crit_hyst(struct device *dev, struct device_attribute *attr,
272 char *buf)
274 struct lm95245_data *data = lm95245_update_device(dev);
275 int index = to_sensor_dev_attr(attr)->index;
276 int hyst = data->regs[index] - data->regs[8];
278 return snprintf(buf, PAGE_SIZE - 1, "%d\n", hyst * 1000);
281 static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr,
282 const char *buf, size_t count)
284 struct lm95245_data *data = dev_get_drvdata(dev);
285 int index = to_sensor_dev_attr(attr)->index;
286 struct i2c_client *client = data->client;
287 unsigned long val;
288 int hyst, limit;
290 if (kstrtoul(buf, 10, &val) < 0)
291 return -EINVAL;
293 mutex_lock(&data->update_lock);
295 limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]);
296 hyst = limit - val / 1000;
297 hyst = clamp_val(hyst, 0, 31);
298 data->regs[8] = hyst;
300 /* shared crit hysteresis */
301 i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS,
302 hyst);
304 mutex_unlock(&data->update_lock);
306 return count;
309 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
310 char *buf)
312 struct lm95245_data *data = dev_get_drvdata(dev);
314 return snprintf(buf, PAGE_SIZE - 1,
315 data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n");
318 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
319 const char *buf, size_t count)
321 struct lm95245_data *data = dev_get_drvdata(dev);
322 struct i2c_client *client = data->client;
323 unsigned long val;
325 if (kstrtoul(buf, 10, &val) < 0)
326 return -EINVAL;
327 if (val != 1 && val != 2)
328 return -EINVAL;
330 mutex_lock(&data->update_lock);
332 if (val == 1)
333 data->config2 |= CFG2_REMOTE_TT;
334 else
335 data->config2 &= ~CFG2_REMOTE_TT;
337 data->valid = 0;
339 i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2,
340 data->config2);
342 mutex_unlock(&data->update_lock);
344 return count;
347 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
348 char *buf)
350 struct lm95245_data *data = lm95245_update_device(dev);
351 int index = to_sensor_dev_attr(attr)->index;
353 return snprintf(buf, PAGE_SIZE - 1, "%d\n",
354 !!(data->regs[9] & index));
357 static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
358 char *buf)
360 struct lm95245_data *data = lm95245_update_device(dev);
362 return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
365 static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
366 const char *buf, size_t count)
368 struct lm95245_data *data = dev_get_drvdata(dev);
369 struct i2c_client *client = data->client;
370 unsigned long val;
372 if (kstrtoul(buf, 10, &val) < 0)
373 return -EINVAL;
375 mutex_lock(&data->update_lock);
377 data->interval = lm95245_set_conversion_rate(client, val);
379 mutex_unlock(&data->update_lock);
381 return count;
384 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
385 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit,
386 set_limit, 6);
387 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst,
388 set_crit_hyst, 6);
389 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
390 STATUS1_LOC);
392 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
393 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit,
394 set_limit, 7);
395 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_crit_hyst, NULL, 7);
396 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
397 STATUS1_RTCRIT);
398 static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type,
399 set_type, 0);
400 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
401 STATUS1_DIODE_FAULT);
403 static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
404 set_interval);
406 static struct attribute *lm95245_attrs[] = {
407 &sensor_dev_attr_temp1_input.dev_attr.attr,
408 &sensor_dev_attr_temp1_crit.dev_attr.attr,
409 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
410 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
411 &sensor_dev_attr_temp2_input.dev_attr.attr,
412 &sensor_dev_attr_temp2_crit.dev_attr.attr,
413 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
414 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
415 &sensor_dev_attr_temp2_type.dev_attr.attr,
416 &sensor_dev_attr_temp2_fault.dev_attr.attr,
417 &dev_attr_update_interval.attr,
418 NULL
420 ATTRIBUTE_GROUPS(lm95245);
422 /* Return 0 if detection is successful, -ENODEV otherwise */
423 static int lm95245_detect(struct i2c_client *new_client,
424 struct i2c_board_info *info)
426 struct i2c_adapter *adapter = new_client->adapter;
427 int address = new_client->addr;
428 const char *name;
429 int rev, id;
431 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
432 return -ENODEV;
434 id = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID);
435 if (id != MANUFACTURER_ID)
436 return -ENODEV;
438 rev = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID);
439 switch (rev) {
440 case LM95235_REVISION:
441 if (address != 0x18 && address != 0x29 && address != 0x4c)
442 return -ENODEV;
443 name = "lm95235";
444 break;
445 case LM95245_REVISION:
446 name = "lm95245";
447 break;
448 default:
449 return -ENODEV;
452 strlcpy(info->type, name, I2C_NAME_SIZE);
453 return 0;
456 static void lm95245_init_client(struct i2c_client *client,
457 struct lm95245_data *data)
459 data->interval = lm95245_read_conversion_rate(client);
461 data->config1 = i2c_smbus_read_byte_data(client,
462 LM95245_REG_RW_CONFIG1);
463 data->config2 = i2c_smbus_read_byte_data(client,
464 LM95245_REG_RW_CONFIG2);
466 if (data->config1 & CFG_STOP) {
467 /* Clear the standby bit */
468 data->config1 &= ~CFG_STOP;
469 i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1,
470 data->config1);
474 static int lm95245_probe(struct i2c_client *client,
475 const struct i2c_device_id *id)
477 struct device *dev = &client->dev;
478 struct lm95245_data *data;
479 struct device *hwmon_dev;
481 data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
482 if (!data)
483 return -ENOMEM;
485 data->client = client;
486 mutex_init(&data->update_lock);
488 /* Initialize the LM95245 chip */
489 lm95245_init_client(client, data);
491 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
492 data,
493 lm95245_groups);
494 return PTR_ERR_OR_ZERO(hwmon_dev);
497 /* Driver data (common to all clients) */
498 static const struct i2c_device_id lm95245_id[] = {
499 { "lm95235", 0 },
500 { "lm95245", 0 },
503 MODULE_DEVICE_TABLE(i2c, lm95245_id);
505 static struct i2c_driver lm95245_driver = {
506 .class = I2C_CLASS_HWMON,
507 .driver = {
508 .name = "lm95245",
510 .probe = lm95245_probe,
511 .id_table = lm95245_id,
512 .detect = lm95245_detect,
513 .address_list = normal_i2c,
516 module_i2c_driver(lm95245_driver);
518 MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
519 MODULE_DESCRIPTION("LM95235/LM95245 sensor driver");
520 MODULE_LICENSE("GPL");