xref: /linux/drivers/hwmon/lm95241.c (revision f48ccb26e264b112afc087d562fa2d68e2e1174b)
106160327SDavide Rizzo /*
20f1deb4bSDavide Rizzo  * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
306160327SDavide Rizzo  *
40f1deb4bSDavide Rizzo  * The LM95241 is a sensor chip made by National Semiconductors.
50f1deb4bSDavide Rizzo  * It reports up to three temperatures (its own plus up to two external ones).
60f1deb4bSDavide Rizzo  * Complete datasheet can be obtained from National's website at:
706160327SDavide Rizzo  *   http://www.national.com/ds.cgi/LM/LM95241.pdf
806160327SDavide Rizzo  *
906160327SDavide Rizzo  * This program is free software; you can redistribute it and/or modify
1006160327SDavide Rizzo  * it under the terms of the GNU General Public License as published by
1106160327SDavide Rizzo  * the Free Software Foundation; either version 2 of the License, or
1206160327SDavide Rizzo  * (at your option) any later version.
1306160327SDavide Rizzo  *
1406160327SDavide Rizzo  * This program is distributed in the hope that it will be useful,
1506160327SDavide Rizzo  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1606160327SDavide Rizzo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1706160327SDavide Rizzo  * GNU General Public License for more details.
1806160327SDavide Rizzo  *
1906160327SDavide Rizzo  * You should have received a copy of the GNU General Public License
2006160327SDavide Rizzo  * along with this program; if not, write to the Free Software
2106160327SDavide Rizzo  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2206160327SDavide Rizzo  */
2306160327SDavide Rizzo 
2406160327SDavide Rizzo #include <linux/module.h>
2506160327SDavide Rizzo #include <linux/init.h>
2606160327SDavide Rizzo #include <linux/slab.h>
2706160327SDavide Rizzo #include <linux/jiffies.h>
2806160327SDavide Rizzo #include <linux/i2c.h>
2906160327SDavide Rizzo #include <linux/hwmon.h>
3006160327SDavide Rizzo #include <linux/hwmon-sysfs.h>
3106160327SDavide Rizzo #include <linux/err.h>
3206160327SDavide Rizzo #include <linux/mutex.h>
3306160327SDavide Rizzo #include <linux/sysfs.h>
3406160327SDavide Rizzo 
350f1deb4bSDavide Rizzo #define DEVNAME "lm95241"
360f1deb4bSDavide Rizzo 
3706160327SDavide Rizzo static const unsigned short normal_i2c[] = {
3806160327SDavide Rizzo 	0x19, 0x2a, 0x2b, I2C_CLIENT_END };
3906160327SDavide Rizzo 
4006160327SDavide Rizzo /* LM95241 registers */
4106160327SDavide Rizzo #define LM95241_REG_R_MAN_ID		0xFE
4206160327SDavide Rizzo #define LM95241_REG_R_CHIP_ID		0xFF
4306160327SDavide Rizzo #define LM95241_REG_R_STATUS		0x02
4406160327SDavide Rizzo #define LM95241_REG_RW_CONFIG		0x03
4506160327SDavide Rizzo #define LM95241_REG_RW_REM_FILTER	0x06
4606160327SDavide Rizzo #define LM95241_REG_RW_TRUTHERM		0x07
4706160327SDavide Rizzo #define LM95241_REG_W_ONE_SHOT		0x0F
4806160327SDavide Rizzo #define LM95241_REG_R_LOCAL_TEMPH	0x10
4906160327SDavide Rizzo #define LM95241_REG_R_REMOTE1_TEMPH	0x11
5006160327SDavide Rizzo #define LM95241_REG_R_REMOTE2_TEMPH	0x12
5106160327SDavide Rizzo #define LM95241_REG_R_LOCAL_TEMPL	0x20
5206160327SDavide Rizzo #define LM95241_REG_R_REMOTE1_TEMPL	0x21
5306160327SDavide Rizzo #define LM95241_REG_R_REMOTE2_TEMPL	0x22
5406160327SDavide Rizzo #define LM95241_REG_RW_REMOTE_MODEL	0x30
5506160327SDavide Rizzo 
5606160327SDavide Rizzo /* LM95241 specific bitfields */
5706160327SDavide Rizzo #define CFG_STOP 0x40
5806160327SDavide Rizzo #define CFG_CR0076 0x00
5906160327SDavide Rizzo #define CFG_CR0182 0x10
6006160327SDavide Rizzo #define CFG_CR1000 0x20
6106160327SDavide Rizzo #define CFG_CR2700 0x30
62*f48ccb26SGuenter Roeck #define CFG_CRMASK 0x30
6306160327SDavide Rizzo #define R1MS_SHIFT 0
6406160327SDavide Rizzo #define R2MS_SHIFT 2
6506160327SDavide Rizzo #define R1MS_MASK (0x01 << (R1MS_SHIFT))
6606160327SDavide Rizzo #define R2MS_MASK (0x01 << (R2MS_SHIFT))
6706160327SDavide Rizzo #define R1DF_SHIFT 1
6806160327SDavide Rizzo #define R2DF_SHIFT 2
6906160327SDavide Rizzo #define R1DF_MASK (0x01 << (R1DF_SHIFT))
7006160327SDavide Rizzo #define R2DF_MASK (0x01 << (R2DF_SHIFT))
7106160327SDavide Rizzo #define R1FE_MASK 0x01
7206160327SDavide Rizzo #define R2FE_MASK 0x05
7306160327SDavide Rizzo #define TT1_SHIFT 0
7406160327SDavide Rizzo #define TT2_SHIFT 4
7506160327SDavide Rizzo #define TT_OFF 0
7606160327SDavide Rizzo #define TT_ON 1
7706160327SDavide Rizzo #define TT_MASK 7
788c1d0419SGuenter Roeck #define NATSEMI_MAN_ID	0x01
798c1d0419SGuenter Roeck #define LM95231_CHIP_ID	0xA1
808c1d0419SGuenter Roeck #define LM95241_CHIP_ID	0xA4
8106160327SDavide Rizzo 
820f1deb4bSDavide Rizzo static const u8 lm95241_reg_address[] = {
830f1deb4bSDavide Rizzo 	LM95241_REG_R_LOCAL_TEMPH,
840f1deb4bSDavide Rizzo 	LM95241_REG_R_LOCAL_TEMPL,
850f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE1_TEMPH,
860f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE1_TEMPL,
870f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE2_TEMPH,
880f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE2_TEMPL
890f1deb4bSDavide Rizzo };
9006160327SDavide Rizzo 
9106160327SDavide Rizzo /* Client data (each client gets its own) */
9206160327SDavide Rizzo struct lm95241_data {
93f809621eSGuenter Roeck 	struct i2c_client *client;
9406160327SDavide Rizzo 	struct mutex update_lock;
95*f48ccb26SGuenter Roeck 	unsigned long last_updated;	/* in jiffies */
96*f48ccb26SGuenter Roeck 	unsigned long interval;		/* in milli-seconds */
9706160327SDavide Rizzo 	char valid;		/* zero until following fields are valid */
9806160327SDavide Rizzo 	/* registers values */
990f1deb4bSDavide Rizzo 	u8 temp[ARRAY_SIZE(lm95241_reg_address)];
10006160327SDavide Rizzo 	u8 config, model, trutherm;
10106160327SDavide Rizzo };
10206160327SDavide Rizzo 
1030f1deb4bSDavide Rizzo /* Conversions */
1040c2a40e2SGuenter Roeck static int temp_from_reg_signed(u8 val_h, u8 val_l)
1050f1deb4bSDavide Rizzo {
1060c2a40e2SGuenter Roeck 	s16 val_hl = (val_h << 8) | val_l;
1070c2a40e2SGuenter Roeck 	return val_hl * 1000 / 256;
1080c2a40e2SGuenter Roeck }
1090c2a40e2SGuenter Roeck 
1100c2a40e2SGuenter Roeck static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
1110c2a40e2SGuenter Roeck {
1120c2a40e2SGuenter Roeck 	u16 val_hl = (val_h << 8) | val_l;
1130c2a40e2SGuenter Roeck 	return val_hl * 1000 / 256;
11406160327SDavide Rizzo }
1150f1deb4bSDavide Rizzo 
1160f1deb4bSDavide Rizzo static struct lm95241_data *lm95241_update_device(struct device *dev)
1170f1deb4bSDavide Rizzo {
118f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
119f809621eSGuenter Roeck 	struct i2c_client *client = data->client;
1200f1deb4bSDavide Rizzo 
1210f1deb4bSDavide Rizzo 	mutex_lock(&data->update_lock);
1220f1deb4bSDavide Rizzo 
123*f48ccb26SGuenter Roeck 	if (time_after(jiffies, data->last_updated
124*f48ccb26SGuenter Roeck 		       + msecs_to_jiffies(data->interval)) ||
1250f1deb4bSDavide Rizzo 	    !data->valid) {
1260f1deb4bSDavide Rizzo 		int i;
1270f1deb4bSDavide Rizzo 
128f809621eSGuenter Roeck 		dev_dbg(dev, "Updating lm95241 data.\n");
1290f1deb4bSDavide Rizzo 		for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
1300f1deb4bSDavide Rizzo 			data->temp[i]
1310f1deb4bSDavide Rizzo 			  = i2c_smbus_read_byte_data(client,
1320f1deb4bSDavide Rizzo 						     lm95241_reg_address[i]);
1330f1deb4bSDavide Rizzo 		data->last_updated = jiffies;
1340f1deb4bSDavide Rizzo 		data->valid = 1;
1350f1deb4bSDavide Rizzo 	}
1360f1deb4bSDavide Rizzo 
1370f1deb4bSDavide Rizzo 	mutex_unlock(&data->update_lock);
1380f1deb4bSDavide Rizzo 
1390f1deb4bSDavide Rizzo 	return data;
1400f1deb4bSDavide Rizzo }
1410f1deb4bSDavide Rizzo 
1420f1deb4bSDavide Rizzo /* Sysfs stuff */
1430f1deb4bSDavide Rizzo static ssize_t show_input(struct device *dev, struct device_attribute *attr,
1440f1deb4bSDavide Rizzo 			  char *buf)
1450f1deb4bSDavide Rizzo {
1460f1deb4bSDavide Rizzo 	struct lm95241_data *data = lm95241_update_device(dev);
1470c2a40e2SGuenter Roeck 	int index = to_sensor_dev_attr(attr)->index;
1480f1deb4bSDavide Rizzo 
1490f1deb4bSDavide Rizzo 	return snprintf(buf, PAGE_SIZE - 1, "%d\n",
1500c2a40e2SGuenter Roeck 			index == 0 || (data->config & (1 << (index / 2))) ?
1510c2a40e2SGuenter Roeck 		temp_from_reg_signed(data->temp[index], data->temp[index + 1]) :
1520c2a40e2SGuenter Roeck 		temp_from_reg_unsigned(data->temp[index],
1530c2a40e2SGuenter Roeck 				       data->temp[index + 1]));
1540f1deb4bSDavide Rizzo }
1550f1deb4bSDavide Rizzo 
1560f1deb4bSDavide Rizzo static ssize_t show_type(struct device *dev, struct device_attribute *attr,
1570f1deb4bSDavide Rizzo 			 char *buf)
1580f1deb4bSDavide Rizzo {
159f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
1600f1deb4bSDavide Rizzo 
1610f1deb4bSDavide Rizzo 	return snprintf(buf, PAGE_SIZE - 1,
1620f1deb4bSDavide Rizzo 		data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
1630f1deb4bSDavide Rizzo }
1640f1deb4bSDavide Rizzo 
1650f1deb4bSDavide Rizzo static ssize_t set_type(struct device *dev, struct device_attribute *attr,
1660f1deb4bSDavide Rizzo 			const char *buf, size_t count)
1670f1deb4bSDavide Rizzo {
168f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
169f809621eSGuenter Roeck 	struct i2c_client *client = data->client;
1700f1deb4bSDavide Rizzo 	unsigned long val;
1710f1deb4bSDavide Rizzo 	int shift;
1720f1deb4bSDavide Rizzo 	u8 mask = to_sensor_dev_attr(attr)->index;
1730f1deb4bSDavide Rizzo 
174179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1750f1deb4bSDavide Rizzo 		return -EINVAL;
1760f1deb4bSDavide Rizzo 	if (val != 1 && val != 2)
1770f1deb4bSDavide Rizzo 		return -EINVAL;
1780f1deb4bSDavide Rizzo 
1790f1deb4bSDavide Rizzo 	shift = mask == R1MS_MASK ? TT1_SHIFT : TT2_SHIFT;
1800f1deb4bSDavide Rizzo 
1810f1deb4bSDavide Rizzo 	mutex_lock(&data->update_lock);
1820f1deb4bSDavide Rizzo 
1830f1deb4bSDavide Rizzo 	data->trutherm &= ~(TT_MASK << shift);
1840f1deb4bSDavide Rizzo 	if (val == 1) {
1850f1deb4bSDavide Rizzo 		data->model |= mask;
1860f1deb4bSDavide Rizzo 		data->trutherm |= (TT_ON << shift);
1870f1deb4bSDavide Rizzo 	} else {
1880f1deb4bSDavide Rizzo 		data->model &= ~mask;
1890f1deb4bSDavide Rizzo 		data->trutherm |= (TT_OFF << shift);
1900f1deb4bSDavide Rizzo 	}
1910f1deb4bSDavide Rizzo 	data->valid = 0;
1920f1deb4bSDavide Rizzo 
1930f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
1940f1deb4bSDavide Rizzo 				  data->model);
1950f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
1960f1deb4bSDavide Rizzo 				  data->trutherm);
1970f1deb4bSDavide Rizzo 
1980f1deb4bSDavide Rizzo 	mutex_unlock(&data->update_lock);
1990f1deb4bSDavide Rizzo 
2000f1deb4bSDavide Rizzo 	return count;
2010f1deb4bSDavide Rizzo }
2020f1deb4bSDavide Rizzo 
2030f1deb4bSDavide Rizzo static ssize_t show_min(struct device *dev, struct device_attribute *attr,
2040f1deb4bSDavide Rizzo 			char *buf)
2050f1deb4bSDavide Rizzo {
206f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
2070f1deb4bSDavide Rizzo 
2080f1deb4bSDavide Rizzo 	return snprintf(buf, PAGE_SIZE - 1,
2090f1deb4bSDavide Rizzo 			data->config & to_sensor_dev_attr(attr)->index ?
2100f1deb4bSDavide Rizzo 			"-127000\n" : "0\n");
2110f1deb4bSDavide Rizzo }
2120f1deb4bSDavide Rizzo 
2130f1deb4bSDavide Rizzo static ssize_t set_min(struct device *dev, struct device_attribute *attr,
2140f1deb4bSDavide Rizzo 		       const char *buf, size_t count)
2150f1deb4bSDavide Rizzo {
216f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
2170f1deb4bSDavide Rizzo 	long val;
2180f1deb4bSDavide Rizzo 
219179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val) < 0)
2200f1deb4bSDavide Rizzo 		return -EINVAL;
2210f1deb4bSDavide Rizzo 	if (val < -128000)
2220f1deb4bSDavide Rizzo 		return -EINVAL;
2230f1deb4bSDavide Rizzo 
2240f1deb4bSDavide Rizzo 	mutex_lock(&data->update_lock);
2250f1deb4bSDavide Rizzo 
2260f1deb4bSDavide Rizzo 	if (val < 0)
2270f1deb4bSDavide Rizzo 		data->config |= to_sensor_dev_attr(attr)->index;
2280f1deb4bSDavide Rizzo 	else
2290f1deb4bSDavide Rizzo 		data->config &= ~to_sensor_dev_attr(attr)->index;
2300f1deb4bSDavide Rizzo 	data->valid = 0;
2310f1deb4bSDavide Rizzo 
232f809621eSGuenter Roeck 	i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
233f809621eSGuenter Roeck 				  data->config);
2340f1deb4bSDavide Rizzo 
2350f1deb4bSDavide Rizzo 	mutex_unlock(&data->update_lock);
2360f1deb4bSDavide Rizzo 
2370f1deb4bSDavide Rizzo 	return count;
2380f1deb4bSDavide Rizzo }
2390f1deb4bSDavide Rizzo 
2400f1deb4bSDavide Rizzo static ssize_t show_max(struct device *dev, struct device_attribute *attr,
2410f1deb4bSDavide Rizzo 			char *buf)
2420f1deb4bSDavide Rizzo {
243f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
2440f1deb4bSDavide Rizzo 
2450f1deb4bSDavide Rizzo 	return snprintf(buf, PAGE_SIZE - 1,
2460f1deb4bSDavide Rizzo 			data->config & to_sensor_dev_attr(attr)->index ?
2470f1deb4bSDavide Rizzo 			"127000\n" : "255000\n");
2480f1deb4bSDavide Rizzo }
2490f1deb4bSDavide Rizzo 
2500f1deb4bSDavide Rizzo static ssize_t set_max(struct device *dev, struct device_attribute *attr,
2510f1deb4bSDavide Rizzo 		       const char *buf, size_t count)
2520f1deb4bSDavide Rizzo {
253f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
2540f1deb4bSDavide Rizzo 	long val;
2550f1deb4bSDavide Rizzo 
256179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val) < 0)
2570f1deb4bSDavide Rizzo 		return -EINVAL;
2580f1deb4bSDavide Rizzo 	if (val >= 256000)
2590f1deb4bSDavide Rizzo 		return -EINVAL;
2600f1deb4bSDavide Rizzo 
2610f1deb4bSDavide Rizzo 	mutex_lock(&data->update_lock);
2620f1deb4bSDavide Rizzo 
2630f1deb4bSDavide Rizzo 	if (val <= 127000)
2640f1deb4bSDavide Rizzo 		data->config |= to_sensor_dev_attr(attr)->index;
2650f1deb4bSDavide Rizzo 	else
2660f1deb4bSDavide Rizzo 		data->config &= ~to_sensor_dev_attr(attr)->index;
2670f1deb4bSDavide Rizzo 	data->valid = 0;
2680f1deb4bSDavide Rizzo 
269f809621eSGuenter Roeck 	i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
270f809621eSGuenter Roeck 				  data->config);
2710f1deb4bSDavide Rizzo 
2720f1deb4bSDavide Rizzo 	mutex_unlock(&data->update_lock);
2730f1deb4bSDavide Rizzo 
2740f1deb4bSDavide Rizzo 	return count;
2750f1deb4bSDavide Rizzo }
27606160327SDavide Rizzo 
277bc482bf0SGuenter Roeck static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
27806160327SDavide Rizzo 			     char *buf)
27906160327SDavide Rizzo {
28006160327SDavide Rizzo 	struct lm95241_data *data = lm95241_update_device(dev);
28106160327SDavide Rizzo 
282*f48ccb26SGuenter Roeck 	return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
28306160327SDavide Rizzo }
28406160327SDavide Rizzo 
285bc482bf0SGuenter Roeck static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
28606160327SDavide Rizzo 			    const char *buf, size_t count)
28706160327SDavide Rizzo {
288f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
28961ec2da5SJean Delvare 	unsigned long val;
290*f48ccb26SGuenter Roeck 	int convrate;
291*f48ccb26SGuenter Roeck 	u8 config;
29206160327SDavide Rizzo 
293179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
29461ec2da5SJean Delvare 		return -EINVAL;
29561ec2da5SJean Delvare 
296*f48ccb26SGuenter Roeck 	mutex_lock(&data->update_lock);
297*f48ccb26SGuenter Roeck 
298*f48ccb26SGuenter Roeck 	config = data->config & ~CFG_CRMASK;
299*f48ccb26SGuenter Roeck 
300*f48ccb26SGuenter Roeck 	if (val < 130) {
301*f48ccb26SGuenter Roeck 		convrate = 76;
302*f48ccb26SGuenter Roeck 		config |= CFG_CR0076;
303*f48ccb26SGuenter Roeck 	} else if (val < 590) {
304*f48ccb26SGuenter Roeck 		convrate = 182;
305*f48ccb26SGuenter Roeck 		config |= CFG_CR0182;
306*f48ccb26SGuenter Roeck 	} else if (val < 1850) {
307*f48ccb26SGuenter Roeck 		convrate = 1000;
308*f48ccb26SGuenter Roeck 		config |= CFG_CR1000;
309*f48ccb26SGuenter Roeck 	} else {
310*f48ccb26SGuenter Roeck 		convrate = 2700;
311*f48ccb26SGuenter Roeck 		config |= CFG_CR2700;
312*f48ccb26SGuenter Roeck 	}
313*f48ccb26SGuenter Roeck 
314*f48ccb26SGuenter Roeck 	data->interval = convrate;
315*f48ccb26SGuenter Roeck 	data->config = config;
316*f48ccb26SGuenter Roeck 	i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
317*f48ccb26SGuenter Roeck 				  config);
318*f48ccb26SGuenter Roeck 	mutex_unlock(&data->update_lock);
31906160327SDavide Rizzo 
32006160327SDavide Rizzo 	return count;
32106160327SDavide Rizzo }
32206160327SDavide Rizzo 
3230f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
3240f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
3250f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4);
3260f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
3270f1deb4bSDavide Rizzo 			  R1MS_MASK);
3280f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
3290f1deb4bSDavide Rizzo 			  R2MS_MASK);
3300f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min,
3310f1deb4bSDavide Rizzo 			  R1DF_MASK);
3320f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min,
3330f1deb4bSDavide Rizzo 			  R2DF_MASK);
3340f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
3350f1deb4bSDavide Rizzo 			  R1DF_MASK);
3360f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
3370f1deb4bSDavide Rizzo 			  R2DF_MASK);
338bc482bf0SGuenter Roeck static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
339bc482bf0SGuenter Roeck 		   set_interval);
34006160327SDavide Rizzo 
341f809621eSGuenter Roeck static struct attribute *lm95241_attrs[] = {
3420f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3430f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3440f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3450f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp2_type.dev_attr.attr,
3460f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp3_type.dev_attr.attr,
3470f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp2_min.dev_attr.attr,
3480f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp3_min.dev_attr.attr,
3490f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp2_max.dev_attr.attr,
3500f1deb4bSDavide Rizzo 	&sensor_dev_attr_temp3_max.dev_attr.attr,
351bc482bf0SGuenter Roeck 	&dev_attr_update_interval.attr,
35206160327SDavide Rizzo 	NULL
35306160327SDavide Rizzo };
354f809621eSGuenter Roeck ATTRIBUTE_GROUPS(lm95241);
35506160327SDavide Rizzo 
356797eaa4bSJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
357310ec792SJean Delvare static int lm95241_detect(struct i2c_client *new_client,
358797eaa4bSJean Delvare 			  struct i2c_board_info *info)
35906160327SDavide Rizzo {
360797eaa4bSJean Delvare 	struct i2c_adapter *adapter = new_client->adapter;
36152df6440SJean Delvare 	const char *name;
3628c1d0419SGuenter Roeck 	int mfg_id, chip_id;
36306160327SDavide Rizzo 
36406160327SDavide Rizzo 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
365797eaa4bSJean Delvare 		return -ENODEV;
36606160327SDavide Rizzo 
3678c1d0419SGuenter Roeck 	mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID);
3688c1d0419SGuenter Roeck 	if (mfg_id != NATSEMI_MAN_ID)
3698c1d0419SGuenter Roeck 		return -ENODEV;
3708c1d0419SGuenter Roeck 
3718c1d0419SGuenter Roeck 	chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID);
3728c1d0419SGuenter Roeck 	switch (chip_id) {
3738c1d0419SGuenter Roeck 	case LM95231_CHIP_ID:
3748c1d0419SGuenter Roeck 		name = "lm95231";
3758c1d0419SGuenter Roeck 		break;
3768c1d0419SGuenter Roeck 	case LM95241_CHIP_ID:
3778c1d0419SGuenter Roeck 		name = "lm95241";
3788c1d0419SGuenter Roeck 		break;
3798c1d0419SGuenter Roeck 	default:
380797eaa4bSJean Delvare 		return -ENODEV;
38106160327SDavide Rizzo 	}
38206160327SDavide Rizzo 
383797eaa4bSJean Delvare 	/* Fill the i2c board info */
384797eaa4bSJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
385797eaa4bSJean Delvare 	return 0;
386797eaa4bSJean Delvare }
38706160327SDavide Rizzo 
388f809621eSGuenter Roeck static void lm95241_init_client(struct i2c_client *client,
389f809621eSGuenter Roeck 				struct lm95241_data *data)
3900f1deb4bSDavide Rizzo {
391*f48ccb26SGuenter Roeck 	data->interval = 1000;
392*f48ccb26SGuenter Roeck 	data->config = CFG_CR1000;
3930f1deb4bSDavide Rizzo 	data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
3940f1deb4bSDavide Rizzo 
3950f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
3960f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
3970f1deb4bSDavide Rizzo 				  R1FE_MASK | R2FE_MASK);
3980f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
3990f1deb4bSDavide Rizzo 				  data->trutherm);
4000f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
4010f1deb4bSDavide Rizzo 				  data->model);
4020f1deb4bSDavide Rizzo }
4030f1deb4bSDavide Rizzo 
404f809621eSGuenter Roeck static int lm95241_probe(struct i2c_client *client,
405797eaa4bSJean Delvare 			 const struct i2c_device_id *id)
406797eaa4bSJean Delvare {
407f809621eSGuenter Roeck 	struct device *dev = &client->dev;
408797eaa4bSJean Delvare 	struct lm95241_data *data;
409f809621eSGuenter Roeck 	struct device *hwmon_dev;
410797eaa4bSJean Delvare 
411f809621eSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
4121487bf70SGuenter Roeck 	if (!data)
4131487bf70SGuenter Roeck 		return -ENOMEM;
414797eaa4bSJean Delvare 
415f809621eSGuenter Roeck 	data->client = client;
41606160327SDavide Rizzo 	mutex_init(&data->update_lock);
41706160327SDavide Rizzo 
41806160327SDavide Rizzo 	/* Initialize the LM95241 chip */
419f809621eSGuenter Roeck 	lm95241_init_client(client, data);
42006160327SDavide Rizzo 
421f809621eSGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
422f809621eSGuenter Roeck 							   data,
423f809621eSGuenter Roeck 							   lm95241_groups);
424f809621eSGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
42506160327SDavide Rizzo }
42606160327SDavide Rizzo 
427797eaa4bSJean Delvare /* Driver data (common to all clients) */
428797eaa4bSJean Delvare static const struct i2c_device_id lm95241_id[] = {
4298c1d0419SGuenter Roeck 	{ "lm95231", 0 },
4308c1d0419SGuenter Roeck 	{ "lm95241", 0 },
431797eaa4bSJean Delvare 	{ }
432797eaa4bSJean Delvare };
433797eaa4bSJean Delvare MODULE_DEVICE_TABLE(i2c, lm95241_id);
434797eaa4bSJean Delvare 
435797eaa4bSJean Delvare static struct i2c_driver lm95241_driver = {
436797eaa4bSJean Delvare 	.class		= I2C_CLASS_HWMON,
437797eaa4bSJean Delvare 	.driver = {
4380f1deb4bSDavide Rizzo 		.name	= DEVNAME,
439797eaa4bSJean Delvare 	},
440797eaa4bSJean Delvare 	.probe		= lm95241_probe,
441797eaa4bSJean Delvare 	.id_table	= lm95241_id,
442797eaa4bSJean Delvare 	.detect		= lm95241_detect,
443c3813d6aSJean Delvare 	.address_list	= normal_i2c,
444797eaa4bSJean Delvare };
445797eaa4bSJean Delvare 
446f0967eeaSAxel Lin module_i2c_driver(lm95241_driver);
44706160327SDavide Rizzo 
4480f1deb4bSDavide Rizzo MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
44906160327SDavide Rizzo MODULE_DESCRIPTION("LM95241 sensor driver");
45006160327SDavide Rizzo MODULE_LICENSE("GPL");
451