xref: /linux/drivers/hwmon/lm95241.c (revision 5c06d5689629803c84e5b23eef5023e7ac5493d7)
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 
20e8172a93SGuenter Roeck #include <linux/bitops.h>
214b2ea08bSGuenter Roeck #include <linux/err.h>
2206160327SDavide Rizzo #include <linux/i2c.h>
234b2ea08bSGuenter Roeck #include <linux/init.h>
244b2ea08bSGuenter Roeck #include <linux/jiffies.h>
2506160327SDavide Rizzo #include <linux/hwmon.h>
264b2ea08bSGuenter Roeck #include <linux/module.h>
2706160327SDavide Rizzo #include <linux/mutex.h>
284b2ea08bSGuenter Roeck #include <linux/slab.h>
2906160327SDavide Rizzo 
300f1deb4bSDavide Rizzo #define DEVNAME "lm95241"
310f1deb4bSDavide Rizzo 
3206160327SDavide Rizzo static const unsigned short normal_i2c[] = {
3306160327SDavide Rizzo 	0x19, 0x2a, 0x2b, I2C_CLIENT_END };
3406160327SDavide Rizzo 
3506160327SDavide Rizzo /* LM95241 registers */
3606160327SDavide Rizzo #define LM95241_REG_R_MAN_ID		0xFE
3706160327SDavide Rizzo #define LM95241_REG_R_CHIP_ID		0xFF
3806160327SDavide Rizzo #define LM95241_REG_R_STATUS		0x02
3906160327SDavide Rizzo #define LM95241_REG_RW_CONFIG		0x03
4006160327SDavide Rizzo #define LM95241_REG_RW_REM_FILTER	0x06
4106160327SDavide Rizzo #define LM95241_REG_RW_TRUTHERM		0x07
4206160327SDavide Rizzo #define LM95241_REG_W_ONE_SHOT		0x0F
4306160327SDavide Rizzo #define LM95241_REG_R_LOCAL_TEMPH	0x10
4406160327SDavide Rizzo #define LM95241_REG_R_REMOTE1_TEMPH	0x11
4506160327SDavide Rizzo #define LM95241_REG_R_REMOTE2_TEMPH	0x12
4606160327SDavide Rizzo #define LM95241_REG_R_LOCAL_TEMPL	0x20
4706160327SDavide Rizzo #define LM95241_REG_R_REMOTE1_TEMPL	0x21
4806160327SDavide Rizzo #define LM95241_REG_R_REMOTE2_TEMPL	0x22
4906160327SDavide Rizzo #define LM95241_REG_RW_REMOTE_MODEL	0x30
5006160327SDavide Rizzo 
5106160327SDavide Rizzo /* LM95241 specific bitfields */
52e8172a93SGuenter Roeck #define CFG_STOP	BIT(6)
5306160327SDavide Rizzo #define CFG_CR0076	0x00
54e8172a93SGuenter Roeck #define CFG_CR0182	BIT(4)
55e8172a93SGuenter Roeck #define CFG_CR1000	BIT(5)
56e8172a93SGuenter Roeck #define CFG_CR2700	(BIT(4) | BIT(5))
57e8172a93SGuenter Roeck #define CFG_CRMASK	(BIT(4) | BIT(5))
58e8172a93SGuenter Roeck #define R1MS_MASK	BIT(0)
59e8172a93SGuenter Roeck #define R2MS_MASK	BIT(2)
60e8172a93SGuenter Roeck #define R1DF_MASK	BIT(1)
61e8172a93SGuenter Roeck #define R2DF_MASK	BIT(2)
62e8172a93SGuenter Roeck #define R1FE_MASK	BIT(0)
63e8172a93SGuenter Roeck #define R2FE_MASK	BIT(2)
64e8172a93SGuenter Roeck #define R1DM		BIT(0)
65e8172a93SGuenter Roeck #define R2DM		BIT(1)
6606160327SDavide Rizzo #define TT1_SHIFT	0
6706160327SDavide Rizzo #define TT2_SHIFT	4
6806160327SDavide Rizzo #define TT_OFF		0
6906160327SDavide Rizzo #define TT_ON		1
7006160327SDavide Rizzo #define TT_MASK		7
718c1d0419SGuenter Roeck #define NATSEMI_MAN_ID	0x01
728c1d0419SGuenter Roeck #define LM95231_CHIP_ID	0xA1
738c1d0419SGuenter Roeck #define LM95241_CHIP_ID	0xA4
7406160327SDavide Rizzo 
750f1deb4bSDavide Rizzo static const u8 lm95241_reg_address[] = {
760f1deb4bSDavide Rizzo 	LM95241_REG_R_LOCAL_TEMPH,
770f1deb4bSDavide Rizzo 	LM95241_REG_R_LOCAL_TEMPL,
780f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE1_TEMPH,
790f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE1_TEMPL,
800f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE2_TEMPH,
810f1deb4bSDavide Rizzo 	LM95241_REG_R_REMOTE2_TEMPL
820f1deb4bSDavide Rizzo };
8306160327SDavide Rizzo 
8406160327SDavide Rizzo /* Client data (each client gets its own) */
8506160327SDavide Rizzo struct lm95241_data {
86f809621eSGuenter Roeck 	struct i2c_client *client;
8706160327SDavide Rizzo 	struct mutex update_lock;
88f48ccb26SGuenter Roeck 	unsigned long last_updated;	/* in jiffies */
89f48ccb26SGuenter Roeck 	unsigned long interval;		/* in milli-seconds */
9006160327SDavide Rizzo 	char valid;		/* zero until following fields are valid */
9106160327SDavide Rizzo 	/* registers values */
920f1deb4bSDavide Rizzo 	u8 temp[ARRAY_SIZE(lm95241_reg_address)];
93090a7f8eSGuenter Roeck 	u8 status, config, model, trutherm;
9406160327SDavide Rizzo };
9506160327SDavide Rizzo 
960f1deb4bSDavide Rizzo /* Conversions */
970c2a40e2SGuenter Roeck static int temp_from_reg_signed(u8 val_h, u8 val_l)
980f1deb4bSDavide Rizzo {
990c2a40e2SGuenter Roeck 	s16 val_hl = (val_h << 8) | val_l;
1000c2a40e2SGuenter Roeck 	return val_hl * 1000 / 256;
1010c2a40e2SGuenter Roeck }
1020c2a40e2SGuenter Roeck 
1030c2a40e2SGuenter Roeck static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
1040c2a40e2SGuenter Roeck {
1050c2a40e2SGuenter Roeck 	u16 val_hl = (val_h << 8) | val_l;
1060c2a40e2SGuenter Roeck 	return val_hl * 1000 / 256;
10706160327SDavide Rizzo }
1080f1deb4bSDavide Rizzo 
1090f1deb4bSDavide Rizzo static struct lm95241_data *lm95241_update_device(struct device *dev)
1100f1deb4bSDavide Rizzo {
111f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
112f809621eSGuenter Roeck 	struct i2c_client *client = data->client;
1130f1deb4bSDavide Rizzo 
1140f1deb4bSDavide Rizzo 	mutex_lock(&data->update_lock);
1150f1deb4bSDavide Rizzo 
116f48ccb26SGuenter Roeck 	if (time_after(jiffies, data->last_updated
117f48ccb26SGuenter Roeck 		       + msecs_to_jiffies(data->interval)) ||
1180f1deb4bSDavide Rizzo 	    !data->valid) {
1190f1deb4bSDavide Rizzo 		int i;
1200f1deb4bSDavide Rizzo 
121f809621eSGuenter Roeck 		dev_dbg(dev, "Updating lm95241 data.\n");
1220f1deb4bSDavide Rizzo 		for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
1230f1deb4bSDavide Rizzo 			data->temp[i]
1240f1deb4bSDavide Rizzo 			  = i2c_smbus_read_byte_data(client,
1250f1deb4bSDavide Rizzo 						     lm95241_reg_address[i]);
126090a7f8eSGuenter Roeck 
127090a7f8eSGuenter Roeck 		data->status = i2c_smbus_read_byte_data(client,
128090a7f8eSGuenter Roeck 							LM95241_REG_R_STATUS);
1290f1deb4bSDavide Rizzo 		data->last_updated = jiffies;
1300f1deb4bSDavide Rizzo 		data->valid = 1;
1310f1deb4bSDavide Rizzo 	}
1320f1deb4bSDavide Rizzo 
1330f1deb4bSDavide Rizzo 	mutex_unlock(&data->update_lock);
1340f1deb4bSDavide Rizzo 
1350f1deb4bSDavide Rizzo 	return data;
1360f1deb4bSDavide Rizzo }
1370f1deb4bSDavide Rizzo 
1383e904628SGuenter Roeck static int lm95241_read_chip(struct device *dev, u32 attr, int channel,
1393e904628SGuenter Roeck 			     long *val)
1403e904628SGuenter Roeck {
1413e904628SGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
1423e904628SGuenter Roeck 
1433e904628SGuenter Roeck 	switch (attr) {
1443e904628SGuenter Roeck 	case hwmon_chip_update_interval:
1453e904628SGuenter Roeck 		*val = data->interval;
1463e904628SGuenter Roeck 		return 0;
1473e904628SGuenter Roeck 	default:
1483e904628SGuenter Roeck 		return -EOPNOTSUPP;
1493e904628SGuenter Roeck 	}
1503e904628SGuenter Roeck }
1513e904628SGuenter Roeck 
1523e904628SGuenter Roeck static int lm95241_read_temp(struct device *dev, u32 attr, int channel,
1533e904628SGuenter Roeck 			     long *val)
1540f1deb4bSDavide Rizzo {
1550f1deb4bSDavide Rizzo 	struct lm95241_data *data = lm95241_update_device(dev);
1560f1deb4bSDavide Rizzo 
1573e904628SGuenter Roeck 	switch (attr) {
1583e904628SGuenter Roeck 	case hwmon_temp_input:
1593e904628SGuenter Roeck 		if (!channel || (data->config & BIT(channel - 1)))
1603e904628SGuenter Roeck 			*val = temp_from_reg_signed(data->temp[channel * 2],
1613e904628SGuenter Roeck 						data->temp[channel * 2 + 1]);
1620f1deb4bSDavide Rizzo 		else
1633e904628SGuenter Roeck 			*val = temp_from_reg_unsigned(data->temp[channel * 2],
1643e904628SGuenter Roeck 						data->temp[channel * 2 + 1]);
1653e904628SGuenter Roeck 		return 0;
1663e904628SGuenter Roeck 	case hwmon_temp_min:
1673e904628SGuenter Roeck 		if (channel == 1)
1683e904628SGuenter Roeck 			*val = (data->config & R1DF_MASK) ? -128000 : 0;
1690f1deb4bSDavide Rizzo 		else
1703e904628SGuenter Roeck 			*val = (data->config & R2DF_MASK) ? -128000 : 0;
1713e904628SGuenter Roeck 		return 0;
1723e904628SGuenter Roeck 	case hwmon_temp_max:
1733e904628SGuenter Roeck 		if (channel == 1)
1743e904628SGuenter Roeck 			*val = (data->config & R1DF_MASK) ? 127875 : 255875;
1753e904628SGuenter Roeck 		else
1763e904628SGuenter Roeck 			*val = (data->config & R2DF_MASK) ? 127875 : 255875;
1773e904628SGuenter Roeck 		return 0;
1783e904628SGuenter Roeck 	case hwmon_temp_type:
1793e904628SGuenter Roeck 		if (channel == 1)
1803e904628SGuenter Roeck 			*val = (data->model & R1MS_MASK) ? 1 : 2;
1813e904628SGuenter Roeck 		else
1823e904628SGuenter Roeck 			*val = (data->model & R2MS_MASK) ? 1 : 2;
1833e904628SGuenter Roeck 		return 0;
1843e904628SGuenter Roeck 	case hwmon_temp_fault:
1853e904628SGuenter Roeck 		if (channel == 1)
1863e904628SGuenter Roeck 			*val = !!(data->status & R1DM);
1873e904628SGuenter Roeck 		else
1883e904628SGuenter Roeck 			*val = !!(data->status & R2DM);
1893e904628SGuenter Roeck 		return 0;
1903e904628SGuenter Roeck 	default:
1913e904628SGuenter Roeck 		return -EOPNOTSUPP;
1923e904628SGuenter Roeck 	}
1930f1deb4bSDavide Rizzo }
19406160327SDavide Rizzo 
1953e904628SGuenter Roeck static int lm95241_read(struct device *dev, enum hwmon_sensor_types type,
1963e904628SGuenter Roeck 			u32 attr, int channel, long *val)
197090a7f8eSGuenter Roeck {
1983e904628SGuenter Roeck 	switch (type) {
1993e904628SGuenter Roeck 	case hwmon_chip:
2003e904628SGuenter Roeck 		return lm95241_read_chip(dev, attr, channel, val);
2013e904628SGuenter Roeck 	case hwmon_temp:
2023e904628SGuenter Roeck 		return lm95241_read_temp(dev, attr, channel, val);
2033e904628SGuenter Roeck 	default:
2043e904628SGuenter Roeck 		return -EOPNOTSUPP;
2053e904628SGuenter Roeck 	}
206090a7f8eSGuenter Roeck }
207090a7f8eSGuenter Roeck 
2083e904628SGuenter Roeck static int lm95241_write_chip(struct device *dev, u32 attr, int channel,
2093e904628SGuenter Roeck 			      long val)
21006160327SDavide Rizzo {
211f809621eSGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
212f48ccb26SGuenter Roeck 	int convrate;
213f48ccb26SGuenter Roeck 	u8 config;
2143e904628SGuenter Roeck 	int ret;
21561ec2da5SJean Delvare 
216f48ccb26SGuenter Roeck 	mutex_lock(&data->update_lock);
217f48ccb26SGuenter Roeck 
2183e904628SGuenter Roeck 	switch (attr) {
2193e904628SGuenter Roeck 	case hwmon_chip_update_interval:
220f48ccb26SGuenter Roeck 		config = data->config & ~CFG_CRMASK;
221f48ccb26SGuenter Roeck 		if (val < 130) {
222f48ccb26SGuenter Roeck 			convrate = 76;
223f48ccb26SGuenter Roeck 			config |= CFG_CR0076;
224f48ccb26SGuenter Roeck 		} else if (val < 590) {
225f48ccb26SGuenter Roeck 			convrate = 182;
226f48ccb26SGuenter Roeck 			config |= CFG_CR0182;
227f48ccb26SGuenter Roeck 		} else if (val < 1850) {
228f48ccb26SGuenter Roeck 			convrate = 1000;
229f48ccb26SGuenter Roeck 			config |= CFG_CR1000;
230f48ccb26SGuenter Roeck 		} else {
231f48ccb26SGuenter Roeck 			convrate = 2700;
232f48ccb26SGuenter Roeck 			config |= CFG_CR2700;
233f48ccb26SGuenter Roeck 		}
234f48ccb26SGuenter Roeck 		data->interval = convrate;
235f48ccb26SGuenter Roeck 		data->config = config;
2363e904628SGuenter Roeck 		ret = i2c_smbus_write_byte_data(data->client,
2373e904628SGuenter Roeck 						LM95241_REG_RW_CONFIG, config);
2383e904628SGuenter Roeck 		break;
2393e904628SGuenter Roeck 	default:
2403e904628SGuenter Roeck 		ret = -EOPNOTSUPP;
2413e904628SGuenter Roeck 		break;
2423e904628SGuenter Roeck 	}
243f48ccb26SGuenter Roeck 	mutex_unlock(&data->update_lock);
2443e904628SGuenter Roeck 	return ret;
24506160327SDavide Rizzo }
24606160327SDavide Rizzo 
2473e904628SGuenter Roeck static int lm95241_write_temp(struct device *dev, u32 attr, int channel,
2483e904628SGuenter Roeck 			      long val)
2493e904628SGuenter Roeck {
2503e904628SGuenter Roeck 	struct lm95241_data *data = dev_get_drvdata(dev);
2513e904628SGuenter Roeck 	struct i2c_client *client = data->client;
2523e904628SGuenter Roeck 	int ret;
25306160327SDavide Rizzo 
2543e904628SGuenter Roeck 	mutex_lock(&data->update_lock);
2553e904628SGuenter Roeck 
2563e904628SGuenter Roeck 	switch (attr) {
2573e904628SGuenter Roeck 	case hwmon_temp_min:
2583e904628SGuenter Roeck 		if (channel == 1) {
2593e904628SGuenter Roeck 			if (val < 0)
2603e904628SGuenter Roeck 				data->config |= R1DF_MASK;
2613e904628SGuenter Roeck 			else
2623e904628SGuenter Roeck 				data->config &= ~R1DF_MASK;
2633e904628SGuenter Roeck 		} else {
2643e904628SGuenter Roeck 			if (val < 0)
2653e904628SGuenter Roeck 				data->config |= R2DF_MASK;
2663e904628SGuenter Roeck 			else
2673e904628SGuenter Roeck 				data->config &= ~R2DF_MASK;
2683e904628SGuenter Roeck 		}
2693e904628SGuenter Roeck 		data->valid = 0;
2703e904628SGuenter Roeck 		ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
2713e904628SGuenter Roeck 						data->config);
2723e904628SGuenter Roeck 		break;
2733e904628SGuenter Roeck 	case hwmon_temp_max:
2743e904628SGuenter Roeck 		if (channel == 1) {
2753e904628SGuenter Roeck 			if (val <= 127875)
2763e904628SGuenter Roeck 				data->config |= R1DF_MASK;
2773e904628SGuenter Roeck 			else
2783e904628SGuenter Roeck 				data->config &= ~R1DF_MASK;
2793e904628SGuenter Roeck 		} else {
2803e904628SGuenter Roeck 			if (val <= 127875)
2813e904628SGuenter Roeck 				data->config |= R2DF_MASK;
2823e904628SGuenter Roeck 			else
2833e904628SGuenter Roeck 				data->config &= ~R2DF_MASK;
2843e904628SGuenter Roeck 		}
2853e904628SGuenter Roeck 		data->valid = 0;
2863e904628SGuenter Roeck 		ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
2873e904628SGuenter Roeck 						data->config);
2883e904628SGuenter Roeck 		break;
2893e904628SGuenter Roeck 	case hwmon_temp_type:
2903e904628SGuenter Roeck 		if (val != 1 && val != 2) {
2913e904628SGuenter Roeck 			ret = -EINVAL;
2923e904628SGuenter Roeck 			break;
2933e904628SGuenter Roeck 		}
2943e904628SGuenter Roeck 		if (channel == 1) {
2953e904628SGuenter Roeck 			data->trutherm &= ~(TT_MASK << TT1_SHIFT);
2963e904628SGuenter Roeck 			if (val == 1) {
2973e904628SGuenter Roeck 				data->model |= R1MS_MASK;
2983e904628SGuenter Roeck 				data->trutherm |= (TT_ON << TT1_SHIFT);
2993e904628SGuenter Roeck 			} else {
3003e904628SGuenter Roeck 				data->model &= ~R1MS_MASK;
3013e904628SGuenter Roeck 				data->trutherm |= (TT_OFF << TT1_SHIFT);
3023e904628SGuenter Roeck 			}
3033e904628SGuenter Roeck 		} else {
3043e904628SGuenter Roeck 			data->trutherm &= ~(TT_MASK << TT2_SHIFT);
3053e904628SGuenter Roeck 			if (val == 1) {
3063e904628SGuenter Roeck 				data->model |= R2MS_MASK;
3073e904628SGuenter Roeck 				data->trutherm |= (TT_ON << TT2_SHIFT);
3083e904628SGuenter Roeck 			} else {
3093e904628SGuenter Roeck 				data->model &= ~R2MS_MASK;
3103e904628SGuenter Roeck 				data->trutherm |= (TT_OFF << TT2_SHIFT);
3113e904628SGuenter Roeck 			}
3123e904628SGuenter Roeck 		}
3133e904628SGuenter Roeck 		ret = i2c_smbus_write_byte_data(client,
3143e904628SGuenter Roeck 						LM95241_REG_RW_REMOTE_MODEL,
3153e904628SGuenter Roeck 						data->model);
3163e904628SGuenter Roeck 		if (ret < 0)
3173e904628SGuenter Roeck 			break;
3183e904628SGuenter Roeck 		ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
3193e904628SGuenter Roeck 						data->trutherm);
3203e904628SGuenter Roeck 		break;
3213e904628SGuenter Roeck 	default:
3223e904628SGuenter Roeck 		ret = -EOPNOTSUPP;
3233e904628SGuenter Roeck 		break;
3243e904628SGuenter Roeck 	}
3253e904628SGuenter Roeck 
3263e904628SGuenter Roeck 	mutex_unlock(&data->update_lock);
3273e904628SGuenter Roeck 
3283e904628SGuenter Roeck 	return ret;
3293e904628SGuenter Roeck }
3303e904628SGuenter Roeck 
3313e904628SGuenter Roeck static int lm95241_write(struct device *dev, enum hwmon_sensor_types type,
3323e904628SGuenter Roeck 			 u32 attr, int channel, long val)
3333e904628SGuenter Roeck {
3343e904628SGuenter Roeck 	switch (type) {
3353e904628SGuenter Roeck 	case hwmon_chip:
3363e904628SGuenter Roeck 		return lm95241_write_chip(dev, attr, channel, val);
3373e904628SGuenter Roeck 	case hwmon_temp:
3383e904628SGuenter Roeck 		return lm95241_write_temp(dev, attr, channel, val);
3393e904628SGuenter Roeck 	default:
3403e904628SGuenter Roeck 		return -EOPNOTSUPP;
3413e904628SGuenter Roeck 	}
3423e904628SGuenter Roeck }
3433e904628SGuenter Roeck 
3443e904628SGuenter Roeck static umode_t lm95241_is_visible(const void *data,
3453e904628SGuenter Roeck 				  enum hwmon_sensor_types type,
3463e904628SGuenter Roeck 				  u32 attr, int channel)
3473e904628SGuenter Roeck {
3483e904628SGuenter Roeck 	switch (type) {
3493e904628SGuenter Roeck 	case hwmon_chip:
3503e904628SGuenter Roeck 		switch (attr) {
3513e904628SGuenter Roeck 		case hwmon_chip_update_interval:
352*5c06d568SGuenter Roeck 			return 0644;
3533e904628SGuenter Roeck 		}
3543e904628SGuenter Roeck 		break;
3553e904628SGuenter Roeck 	case hwmon_temp:
3563e904628SGuenter Roeck 		switch (attr) {
3573e904628SGuenter Roeck 		case hwmon_temp_input:
358*5c06d568SGuenter Roeck 			return 0444;
3593e904628SGuenter Roeck 		case hwmon_temp_fault:
360*5c06d568SGuenter Roeck 			return 0444;
3613e904628SGuenter Roeck 		case hwmon_temp_min:
3623e904628SGuenter Roeck 		case hwmon_temp_max:
3633e904628SGuenter Roeck 		case hwmon_temp_type:
364*5c06d568SGuenter Roeck 			return 0644;
3653e904628SGuenter Roeck 		}
3663e904628SGuenter Roeck 		break;
3673e904628SGuenter Roeck 	default:
3683e904628SGuenter Roeck 		break;
3693e904628SGuenter Roeck 	}
3703e904628SGuenter Roeck 	return 0;
3713e904628SGuenter Roeck }
37206160327SDavide Rizzo 
373797eaa4bSJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
374310ec792SJean Delvare static int lm95241_detect(struct i2c_client *new_client,
375797eaa4bSJean Delvare 			  struct i2c_board_info *info)
37606160327SDavide Rizzo {
377797eaa4bSJean Delvare 	struct i2c_adapter *adapter = new_client->adapter;
37852df6440SJean Delvare 	const char *name;
3798c1d0419SGuenter Roeck 	int mfg_id, chip_id;
38006160327SDavide Rizzo 
38106160327SDavide Rizzo 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
382797eaa4bSJean Delvare 		return -ENODEV;
38306160327SDavide Rizzo 
3848c1d0419SGuenter Roeck 	mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID);
3858c1d0419SGuenter Roeck 	if (mfg_id != NATSEMI_MAN_ID)
3868c1d0419SGuenter Roeck 		return -ENODEV;
3878c1d0419SGuenter Roeck 
3888c1d0419SGuenter Roeck 	chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID);
3898c1d0419SGuenter Roeck 	switch (chip_id) {
3908c1d0419SGuenter Roeck 	case LM95231_CHIP_ID:
3918c1d0419SGuenter Roeck 		name = "lm95231";
3928c1d0419SGuenter Roeck 		break;
3938c1d0419SGuenter Roeck 	case LM95241_CHIP_ID:
3948c1d0419SGuenter Roeck 		name = "lm95241";
3958c1d0419SGuenter Roeck 		break;
3968c1d0419SGuenter Roeck 	default:
397797eaa4bSJean Delvare 		return -ENODEV;
39806160327SDavide Rizzo 	}
39906160327SDavide Rizzo 
400797eaa4bSJean Delvare 	/* Fill the i2c board info */
401797eaa4bSJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
402797eaa4bSJean Delvare 	return 0;
403797eaa4bSJean Delvare }
40406160327SDavide Rizzo 
405f809621eSGuenter Roeck static void lm95241_init_client(struct i2c_client *client,
406f809621eSGuenter Roeck 				struct lm95241_data *data)
4070f1deb4bSDavide Rizzo {
408f48ccb26SGuenter Roeck 	data->interval = 1000;
409f48ccb26SGuenter Roeck 	data->config = CFG_CR1000;
4100f1deb4bSDavide Rizzo 	data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
4110f1deb4bSDavide Rizzo 
4120f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
4130f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
4140f1deb4bSDavide Rizzo 				  R1FE_MASK | R2FE_MASK);
4150f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
4160f1deb4bSDavide Rizzo 				  data->trutherm);
4170f1deb4bSDavide Rizzo 	i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
4180f1deb4bSDavide Rizzo 				  data->model);
4190f1deb4bSDavide Rizzo }
4200f1deb4bSDavide Rizzo 
4213e904628SGuenter Roeck static const u32 lm95241_chip_config[] = {
4223e904628SGuenter Roeck 	HWMON_C_UPDATE_INTERVAL,
4233e904628SGuenter Roeck 	0
4243e904628SGuenter Roeck };
4253e904628SGuenter Roeck 
4263e904628SGuenter Roeck static const struct hwmon_channel_info lm95241_chip = {
4273e904628SGuenter Roeck 	.type = hwmon_chip,
4283e904628SGuenter Roeck 	.config = lm95241_chip_config,
4293e904628SGuenter Roeck };
4303e904628SGuenter Roeck 
4313e904628SGuenter Roeck static const u32 lm95241_temp_config[] = {
4323e904628SGuenter Roeck 	HWMON_T_INPUT,
4333e904628SGuenter Roeck 	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_TYPE |
4343e904628SGuenter Roeck 		HWMON_T_FAULT,
4353e904628SGuenter Roeck 	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_TYPE |
4363e904628SGuenter Roeck 		HWMON_T_FAULT,
4373e904628SGuenter Roeck 	0
4383e904628SGuenter Roeck };
4393e904628SGuenter Roeck 
4403e904628SGuenter Roeck static const struct hwmon_channel_info lm95241_temp = {
4413e904628SGuenter Roeck 	.type = hwmon_temp,
4423e904628SGuenter Roeck 	.config = lm95241_temp_config,
4433e904628SGuenter Roeck };
4443e904628SGuenter Roeck 
4453e904628SGuenter Roeck static const struct hwmon_channel_info *lm95241_info[] = {
4463e904628SGuenter Roeck 	&lm95241_chip,
4473e904628SGuenter Roeck 	&lm95241_temp,
4483e904628SGuenter Roeck 	NULL
4493e904628SGuenter Roeck };
4503e904628SGuenter Roeck 
4513e904628SGuenter Roeck static const struct hwmon_ops lm95241_hwmon_ops = {
4523e904628SGuenter Roeck 	.is_visible = lm95241_is_visible,
4533e904628SGuenter Roeck 	.read = lm95241_read,
4543e904628SGuenter Roeck 	.write = lm95241_write,
4553e904628SGuenter Roeck };
4563e904628SGuenter Roeck 
4573e904628SGuenter Roeck static const struct hwmon_chip_info lm95241_chip_info = {
4583e904628SGuenter Roeck 	.ops = &lm95241_hwmon_ops,
4593e904628SGuenter Roeck 	.info = lm95241_info,
4603e904628SGuenter Roeck };
4613e904628SGuenter Roeck 
462f809621eSGuenter Roeck static int lm95241_probe(struct i2c_client *client,
463797eaa4bSJean Delvare 			 const struct i2c_device_id *id)
464797eaa4bSJean Delvare {
465f809621eSGuenter Roeck 	struct device *dev = &client->dev;
466797eaa4bSJean Delvare 	struct lm95241_data *data;
467f809621eSGuenter Roeck 	struct device *hwmon_dev;
468797eaa4bSJean Delvare 
469f809621eSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
4701487bf70SGuenter Roeck 	if (!data)
4711487bf70SGuenter Roeck 		return -ENOMEM;
472797eaa4bSJean Delvare 
473f809621eSGuenter Roeck 	data->client = client;
47406160327SDavide Rizzo 	mutex_init(&data->update_lock);
47506160327SDavide Rizzo 
47606160327SDavide Rizzo 	/* Initialize the LM95241 chip */
477f809621eSGuenter Roeck 	lm95241_init_client(client, data);
47806160327SDavide Rizzo 
4793e904628SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
480f809621eSGuenter Roeck 							   data,
4813e904628SGuenter Roeck 							   &lm95241_chip_info,
4823e904628SGuenter Roeck 							   NULL);
483f809621eSGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
48406160327SDavide Rizzo }
48506160327SDavide Rizzo 
486797eaa4bSJean Delvare /* Driver data (common to all clients) */
487797eaa4bSJean Delvare static const struct i2c_device_id lm95241_id[] = {
4888c1d0419SGuenter Roeck 	{ "lm95231", 0 },
4898c1d0419SGuenter Roeck 	{ "lm95241", 0 },
490797eaa4bSJean Delvare 	{ }
491797eaa4bSJean Delvare };
492797eaa4bSJean Delvare MODULE_DEVICE_TABLE(i2c, lm95241_id);
493797eaa4bSJean Delvare 
494797eaa4bSJean Delvare static struct i2c_driver lm95241_driver = {
495797eaa4bSJean Delvare 	.class		= I2C_CLASS_HWMON,
496797eaa4bSJean Delvare 	.driver = {
4970f1deb4bSDavide Rizzo 		.name	= DEVNAME,
498797eaa4bSJean Delvare 	},
499797eaa4bSJean Delvare 	.probe		= lm95241_probe,
500797eaa4bSJean Delvare 	.id_table	= lm95241_id,
501797eaa4bSJean Delvare 	.detect		= lm95241_detect,
502c3813d6aSJean Delvare 	.address_list	= normal_i2c,
503797eaa4bSJean Delvare };
504797eaa4bSJean Delvare 
505f0967eeaSAxel Lin module_i2c_driver(lm95241_driver);
50606160327SDavide Rizzo 
5070f1deb4bSDavide Rizzo MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
5080a8784e7SAxel Lin MODULE_DESCRIPTION("LM95231/LM95241 sensor driver");
50906160327SDavide Rizzo MODULE_LICENSE("GPL");
510