xref: /openbmc/linux/drivers/hwmon/jc42.c (revision 90f4102ce59226954edbe960b2434d8b3da5f086)
14453d736SGuenter Roeck /*
24453d736SGuenter Roeck  * jc42.c - driver for Jedec JC42.4 compliant temperature sensors
34453d736SGuenter Roeck  *
44453d736SGuenter Roeck  * Copyright (c) 2010  Ericsson AB.
54453d736SGuenter Roeck  *
64453d736SGuenter Roeck  * Derived from lm77.c by Andras BALI <drewie@freemail.hu>.
74453d736SGuenter Roeck  *
84453d736SGuenter Roeck  * JC42.4 compliant temperature sensors are typically used on memory modules.
94453d736SGuenter Roeck  *
104453d736SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
114453d736SGuenter Roeck  * it under the terms of the GNU General Public License as published by
124453d736SGuenter Roeck  * the Free Software Foundation; either version 2 of the License, or
134453d736SGuenter Roeck  * (at your option) any later version.
144453d736SGuenter Roeck  *
154453d736SGuenter Roeck  * This program is distributed in the hope that it will be useful,
164453d736SGuenter Roeck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
174453d736SGuenter Roeck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
184453d736SGuenter Roeck  * GNU General Public License for more details.
194453d736SGuenter Roeck  *
204453d736SGuenter Roeck  * You should have received a copy of the GNU General Public License
214453d736SGuenter Roeck  * along with this program; if not, write to the Free Software
224453d736SGuenter Roeck  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
234453d736SGuenter Roeck  */
244453d736SGuenter Roeck 
254453d736SGuenter Roeck #include <linux/module.h>
264453d736SGuenter Roeck #include <linux/init.h>
274453d736SGuenter Roeck #include <linux/slab.h>
284453d736SGuenter Roeck #include <linux/jiffies.h>
294453d736SGuenter Roeck #include <linux/i2c.h>
304453d736SGuenter Roeck #include <linux/hwmon.h>
314453d736SGuenter Roeck #include <linux/hwmon-sysfs.h>
324453d736SGuenter Roeck #include <linux/err.h>
334453d736SGuenter Roeck #include <linux/mutex.h>
344453d736SGuenter Roeck 
354453d736SGuenter Roeck /* Addresses to scan */
364453d736SGuenter Roeck static const unsigned short normal_i2c[] = {
374453d736SGuenter Roeck 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, I2C_CLIENT_END };
384453d736SGuenter Roeck 
394453d736SGuenter Roeck /* JC42 registers. All registers are 16 bit. */
404453d736SGuenter Roeck #define JC42_REG_CAP		0x00
414453d736SGuenter Roeck #define JC42_REG_CONFIG		0x01
424453d736SGuenter Roeck #define JC42_REG_TEMP_UPPER	0x02
434453d736SGuenter Roeck #define JC42_REG_TEMP_LOWER	0x03
444453d736SGuenter Roeck #define JC42_REG_TEMP_CRITICAL	0x04
454453d736SGuenter Roeck #define JC42_REG_TEMP		0x05
464453d736SGuenter Roeck #define JC42_REG_MANID		0x06
474453d736SGuenter Roeck #define JC42_REG_DEVICEID	0x07
484453d736SGuenter Roeck 
494453d736SGuenter Roeck /* Status bits in temperature register */
504453d736SGuenter Roeck #define JC42_ALARM_CRIT_BIT	15
514453d736SGuenter Roeck #define JC42_ALARM_MAX_BIT	14
524453d736SGuenter Roeck #define JC42_ALARM_MIN_BIT	13
534453d736SGuenter Roeck 
544453d736SGuenter Roeck /* Configuration register defines */
554453d736SGuenter Roeck #define JC42_CFG_CRIT_ONLY	(1 << 2)
562c6315daSClemens Ladisch #define JC42_CFG_TCRIT_LOCK	(1 << 6)
572c6315daSClemens Ladisch #define JC42_CFG_EVENT_LOCK	(1 << 7)
584453d736SGuenter Roeck #define JC42_CFG_SHUTDOWN	(1 << 8)
594453d736SGuenter Roeck #define JC42_CFG_HYST_SHIFT	9
604453d736SGuenter Roeck #define JC42_CFG_HYST_MASK	0x03
614453d736SGuenter Roeck 
624453d736SGuenter Roeck /* Capabilities */
634453d736SGuenter Roeck #define JC42_CAP_RANGE		(1 << 2)
644453d736SGuenter Roeck 
654453d736SGuenter Roeck /* Manufacturer IDs */
664453d736SGuenter Roeck #define ADT_MANID		0x11d4  /* Analog Devices */
674453d736SGuenter Roeck #define MAX_MANID		0x004d  /* Maxim */
684453d736SGuenter Roeck #define IDT_MANID		0x00b3  /* IDT */
694453d736SGuenter Roeck #define MCP_MANID		0x0054  /* Microchip */
704453d736SGuenter Roeck #define NXP_MANID		0x1131  /* NXP Semiconductors */
714453d736SGuenter Roeck #define ONS_MANID		0x1b09  /* ON Semiconductor */
724453d736SGuenter Roeck #define STM_MANID		0x104a  /* ST Microelectronics */
734453d736SGuenter Roeck 
744453d736SGuenter Roeck /* Supported chips */
754453d736SGuenter Roeck 
764453d736SGuenter Roeck /* Analog Devices */
774453d736SGuenter Roeck #define ADT7408_DEVID		0x0801
784453d736SGuenter Roeck #define ADT7408_DEVID_MASK	0xffff
794453d736SGuenter Roeck 
804453d736SGuenter Roeck /* IDT */
814453d736SGuenter Roeck #define TS3000B3_DEVID		0x2903  /* Also matches TSE2002B3 */
824453d736SGuenter Roeck #define TS3000B3_DEVID_MASK	0xffff
834453d736SGuenter Roeck 
844453d736SGuenter Roeck /* Maxim */
854453d736SGuenter Roeck #define MAX6604_DEVID		0x3e00
864453d736SGuenter Roeck #define MAX6604_DEVID_MASK	0xffff
874453d736SGuenter Roeck 
884453d736SGuenter Roeck /* Microchip */
894453d736SGuenter Roeck #define MCP98242_DEVID		0x2000
904453d736SGuenter Roeck #define MCP98242_DEVID_MASK	0xfffc
914453d736SGuenter Roeck 
924453d736SGuenter Roeck #define MCP98243_DEVID		0x2100
934453d736SGuenter Roeck #define MCP98243_DEVID_MASK	0xfffc
944453d736SGuenter Roeck 
954453d736SGuenter Roeck #define MCP9843_DEVID		0x0000	/* Also matches mcp9805 */
964453d736SGuenter Roeck #define MCP9843_DEVID_MASK	0xfffe
974453d736SGuenter Roeck 
984453d736SGuenter Roeck /* NXP */
994453d736SGuenter Roeck #define SE97_DEVID		0xa200
1004453d736SGuenter Roeck #define SE97_DEVID_MASK		0xfffc
1014453d736SGuenter Roeck 
1024453d736SGuenter Roeck #define SE98_DEVID		0xa100
1034453d736SGuenter Roeck #define SE98_DEVID_MASK		0xfffc
1044453d736SGuenter Roeck 
1054453d736SGuenter Roeck /* ON Semiconductor */
1064453d736SGuenter Roeck #define CAT6095_DEVID		0x0800	/* Also matches CAT34TS02 */
1074453d736SGuenter Roeck #define CAT6095_DEVID_MASK	0xffe0
1084453d736SGuenter Roeck 
1094453d736SGuenter Roeck /* ST Microelectronics */
1104453d736SGuenter Roeck #define STTS424_DEVID		0x0101
1114453d736SGuenter Roeck #define STTS424_DEVID_MASK	0xffff
1124453d736SGuenter Roeck 
1134453d736SGuenter Roeck #define STTS424E_DEVID		0x0000
1144453d736SGuenter Roeck #define STTS424E_DEVID_MASK	0xfffe
1154453d736SGuenter Roeck 
1164453d736SGuenter Roeck static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 };
1174453d736SGuenter Roeck 
1184453d736SGuenter Roeck struct jc42_chips {
1194453d736SGuenter Roeck 	u16 manid;
1204453d736SGuenter Roeck 	u16 devid;
1214453d736SGuenter Roeck 	u16 devid_mask;
1224453d736SGuenter Roeck };
1234453d736SGuenter Roeck 
1244453d736SGuenter Roeck static struct jc42_chips jc42_chips[] = {
1254453d736SGuenter Roeck 	{ ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK },
1264453d736SGuenter Roeck 	{ IDT_MANID, TS3000B3_DEVID, TS3000B3_DEVID_MASK },
1274453d736SGuenter Roeck 	{ MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK },
1284453d736SGuenter Roeck 	{ MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK },
1294453d736SGuenter Roeck 	{ MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK },
1304453d736SGuenter Roeck 	{ MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK },
1314453d736SGuenter Roeck 	{ NXP_MANID, SE97_DEVID, SE97_DEVID_MASK },
1324453d736SGuenter Roeck 	{ ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK },
1334453d736SGuenter Roeck 	{ NXP_MANID, SE98_DEVID, SE98_DEVID_MASK },
1344453d736SGuenter Roeck 	{ STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK },
1354453d736SGuenter Roeck 	{ STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK },
1364453d736SGuenter Roeck };
1374453d736SGuenter Roeck 
1384453d736SGuenter Roeck /* Each client has this additional data */
1394453d736SGuenter Roeck struct jc42_data {
1404453d736SGuenter Roeck 	struct device	*hwmon_dev;
1414453d736SGuenter Roeck 	struct mutex	update_lock;	/* protect register access */
1424453d736SGuenter Roeck 	bool		extended;	/* true if extended range supported */
1434453d736SGuenter Roeck 	bool		valid;
1444453d736SGuenter Roeck 	unsigned long	last_updated;	/* In jiffies */
1454453d736SGuenter Roeck 	u16		orig_config;	/* original configuration */
1464453d736SGuenter Roeck 	u16		config;		/* current configuration */
1474453d736SGuenter Roeck 	u16		temp_input;	/* Temperatures */
1484453d736SGuenter Roeck 	u16		temp_crit;
1494453d736SGuenter Roeck 	u16		temp_min;
1504453d736SGuenter Roeck 	u16		temp_max;
1514453d736SGuenter Roeck };
1524453d736SGuenter Roeck 
1534453d736SGuenter Roeck static int jc42_probe(struct i2c_client *client,
1544453d736SGuenter Roeck 		      const struct i2c_device_id *id);
1554453d736SGuenter Roeck static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info);
1564453d736SGuenter Roeck static int jc42_remove(struct i2c_client *client);
1574453d736SGuenter Roeck 
1584453d736SGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev);
1594453d736SGuenter Roeck 
1604453d736SGuenter Roeck static const struct i2c_device_id jc42_id[] = {
1614453d736SGuenter Roeck 	{ "adt7408", 0 },
1624453d736SGuenter Roeck 	{ "cat94ts02", 0 },
1634453d736SGuenter Roeck 	{ "cat6095", 0 },
1644453d736SGuenter Roeck 	{ "jc42", 0 },
1654453d736SGuenter Roeck 	{ "max6604", 0 },
1664453d736SGuenter Roeck 	{ "mcp9805", 0 },
1674453d736SGuenter Roeck 	{ "mcp98242", 0 },
1684453d736SGuenter Roeck 	{ "mcp98243", 0 },
1694453d736SGuenter Roeck 	{ "mcp9843", 0 },
1704453d736SGuenter Roeck 	{ "se97", 0 },
1714453d736SGuenter Roeck 	{ "se97b", 0 },
1724453d736SGuenter Roeck 	{ "se98", 0 },
1734453d736SGuenter Roeck 	{ "stts424", 0 },
1744453d736SGuenter Roeck 	{ "tse2002b3", 0 },
1754453d736SGuenter Roeck 	{ "ts3000b3", 0 },
1764453d736SGuenter Roeck 	{ }
1774453d736SGuenter Roeck };
1784453d736SGuenter Roeck MODULE_DEVICE_TABLE(i2c, jc42_id);
1794453d736SGuenter Roeck 
1804453d736SGuenter Roeck #ifdef CONFIG_PM
1814453d736SGuenter Roeck 
1824453d736SGuenter Roeck static int jc42_suspend(struct device *dev)
1834453d736SGuenter Roeck {
1844453d736SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);
1854453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
1864453d736SGuenter Roeck 
1874453d736SGuenter Roeck 	data->config |= JC42_CFG_SHUTDOWN;
188*90f4102cSJean Delvare 	i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, data->config);
1894453d736SGuenter Roeck 	return 0;
1904453d736SGuenter Roeck }
1914453d736SGuenter Roeck 
1924453d736SGuenter Roeck static int jc42_resume(struct device *dev)
1934453d736SGuenter Roeck {
1944453d736SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);
1954453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
1964453d736SGuenter Roeck 
1974453d736SGuenter Roeck 	data->config &= ~JC42_CFG_SHUTDOWN;
198*90f4102cSJean Delvare 	i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, data->config);
1994453d736SGuenter Roeck 	return 0;
2004453d736SGuenter Roeck }
2014453d736SGuenter Roeck 
2024453d736SGuenter Roeck static const struct dev_pm_ops jc42_dev_pm_ops = {
2034453d736SGuenter Roeck 	.suspend = jc42_suspend,
2044453d736SGuenter Roeck 	.resume = jc42_resume,
2054453d736SGuenter Roeck };
2064453d736SGuenter Roeck 
2074453d736SGuenter Roeck #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops)
2084453d736SGuenter Roeck #else
2094453d736SGuenter Roeck #define JC42_DEV_PM_OPS NULL
2104453d736SGuenter Roeck #endif /* CONFIG_PM */
2114453d736SGuenter Roeck 
2124453d736SGuenter Roeck /* This is the driver that will be inserted */
2134453d736SGuenter Roeck static struct i2c_driver jc42_driver = {
214774466adSJean Delvare 	.class		= I2C_CLASS_SPD,
2154453d736SGuenter Roeck 	.driver = {
2164453d736SGuenter Roeck 		.name	= "jc42",
2174453d736SGuenter Roeck 		.pm = JC42_DEV_PM_OPS,
2184453d736SGuenter Roeck 	},
2194453d736SGuenter Roeck 	.probe		= jc42_probe,
2204453d736SGuenter Roeck 	.remove		= jc42_remove,
2214453d736SGuenter Roeck 	.id_table	= jc42_id,
2224453d736SGuenter Roeck 	.detect		= jc42_detect,
2234453d736SGuenter Roeck 	.address_list	= normal_i2c,
2244453d736SGuenter Roeck };
2254453d736SGuenter Roeck 
2264453d736SGuenter Roeck #define JC42_TEMP_MIN_EXTENDED	(-40000)
2274453d736SGuenter Roeck #define JC42_TEMP_MIN		0
2284453d736SGuenter Roeck #define JC42_TEMP_MAX		125000
2294453d736SGuenter Roeck 
2304453d736SGuenter Roeck static u16 jc42_temp_to_reg(int temp, bool extended)
2314453d736SGuenter Roeck {
2324453d736SGuenter Roeck 	int ntemp = SENSORS_LIMIT(temp,
2334453d736SGuenter Roeck 				  extended ? JC42_TEMP_MIN_EXTENDED :
2344453d736SGuenter Roeck 				  JC42_TEMP_MIN, JC42_TEMP_MAX);
2354453d736SGuenter Roeck 
2364453d736SGuenter Roeck 	/* convert from 0.001 to 0.0625 resolution */
2374453d736SGuenter Roeck 	return (ntemp * 2 / 125) & 0x1fff;
2384453d736SGuenter Roeck }
2394453d736SGuenter Roeck 
2404453d736SGuenter Roeck static int jc42_temp_from_reg(s16 reg)
2414453d736SGuenter Roeck {
2424453d736SGuenter Roeck 	reg &= 0x1fff;
2434453d736SGuenter Roeck 
2444453d736SGuenter Roeck 	/* sign extend register */
2454453d736SGuenter Roeck 	if (reg & 0x1000)
2464453d736SGuenter Roeck 		reg |= 0xf000;
2474453d736SGuenter Roeck 
2484453d736SGuenter Roeck 	/* convert from 0.0625 to 0.001 resolution */
2494453d736SGuenter Roeck 	return reg * 125 / 2;
2504453d736SGuenter Roeck }
2514453d736SGuenter Roeck 
2524453d736SGuenter Roeck /* sysfs stuff */
2534453d736SGuenter Roeck 
2544453d736SGuenter Roeck /* read routines for temperature limits */
2554453d736SGuenter Roeck #define show(value)	\
2564453d736SGuenter Roeck static ssize_t show_##value(struct device *dev,				\
2574453d736SGuenter Roeck 			    struct device_attribute *attr,		\
2584453d736SGuenter Roeck 			    char *buf)					\
2594453d736SGuenter Roeck {									\
2604453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);		\
2614453d736SGuenter Roeck 	if (IS_ERR(data))						\
2624453d736SGuenter Roeck 		return PTR_ERR(data);					\
2634453d736SGuenter Roeck 	return sprintf(buf, "%d\n", jc42_temp_from_reg(data->value));	\
2644453d736SGuenter Roeck }
2654453d736SGuenter Roeck 
2664453d736SGuenter Roeck show(temp_input);
2674453d736SGuenter Roeck show(temp_crit);
2684453d736SGuenter Roeck show(temp_min);
2694453d736SGuenter Roeck show(temp_max);
2704453d736SGuenter Roeck 
2714453d736SGuenter Roeck /* read routines for hysteresis values */
2724453d736SGuenter Roeck static ssize_t show_temp_crit_hyst(struct device *dev,
2734453d736SGuenter Roeck 				   struct device_attribute *attr, char *buf)
2744453d736SGuenter Roeck {
2754453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
2764453d736SGuenter Roeck 	int temp, hyst;
2774453d736SGuenter Roeck 
2784453d736SGuenter Roeck 	if (IS_ERR(data))
2794453d736SGuenter Roeck 		return PTR_ERR(data);
2804453d736SGuenter Roeck 
2814453d736SGuenter Roeck 	temp = jc42_temp_from_reg(data->temp_crit);
2824453d736SGuenter Roeck 	hyst = jc42_hysteresis[(data->config >> JC42_CFG_HYST_SHIFT)
2834453d736SGuenter Roeck 			       & JC42_CFG_HYST_MASK];
2844453d736SGuenter Roeck 	return sprintf(buf, "%d\n", temp - hyst);
2854453d736SGuenter Roeck }
2864453d736SGuenter Roeck 
2874453d736SGuenter Roeck static ssize_t show_temp_max_hyst(struct device *dev,
2884453d736SGuenter Roeck 				  struct device_attribute *attr, char *buf)
2894453d736SGuenter Roeck {
2904453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
2914453d736SGuenter Roeck 	int temp, hyst;
2924453d736SGuenter Roeck 
2934453d736SGuenter Roeck 	if (IS_ERR(data))
2944453d736SGuenter Roeck 		return PTR_ERR(data);
2954453d736SGuenter Roeck 
2964453d736SGuenter Roeck 	temp = jc42_temp_from_reg(data->temp_max);
2974453d736SGuenter Roeck 	hyst = jc42_hysteresis[(data->config >> JC42_CFG_HYST_SHIFT)
2984453d736SGuenter Roeck 			       & JC42_CFG_HYST_MASK];
2994453d736SGuenter Roeck 	return sprintf(buf, "%d\n", temp - hyst);
3004453d736SGuenter Roeck }
3014453d736SGuenter Roeck 
3024453d736SGuenter Roeck /* write routines */
3034453d736SGuenter Roeck #define set(value, reg)	\
3044453d736SGuenter Roeck static ssize_t set_##value(struct device *dev,				\
3054453d736SGuenter Roeck 			   struct device_attribute *attr,		\
3064453d736SGuenter Roeck 			   const char *buf, size_t count)		\
3074453d736SGuenter Roeck {									\
3084453d736SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);			\
3094453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);		\
3104453d736SGuenter Roeck 	int err, ret = count;						\
3114453d736SGuenter Roeck 	long val;							\
3124453d736SGuenter Roeck 	if (strict_strtol(buf, 10, &val) < 0)				\
3134453d736SGuenter Roeck 		return -EINVAL;						\
3144453d736SGuenter Roeck 	mutex_lock(&data->update_lock);					\
3154453d736SGuenter Roeck 	data->value = jc42_temp_to_reg(val, data->extended);		\
316*90f4102cSJean Delvare 	err = i2c_smbus_write_word_swapped(client, reg, data->value);	\
3174453d736SGuenter Roeck 	if (err < 0)							\
3184453d736SGuenter Roeck 		ret = err;						\
3194453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);				\
3204453d736SGuenter Roeck 	return ret;							\
3214453d736SGuenter Roeck }
3224453d736SGuenter Roeck 
3234453d736SGuenter Roeck set(temp_min, JC42_REG_TEMP_LOWER);
3244453d736SGuenter Roeck set(temp_max, JC42_REG_TEMP_UPPER);
3254453d736SGuenter Roeck set(temp_crit, JC42_REG_TEMP_CRITICAL);
3264453d736SGuenter Roeck 
3274453d736SGuenter Roeck /* JC42.4 compliant chips only support four hysteresis values.
3284453d736SGuenter Roeck  * Pick best choice and go from there. */
3294453d736SGuenter Roeck static ssize_t set_temp_crit_hyst(struct device *dev,
3304453d736SGuenter Roeck 				  struct device_attribute *attr,
3314453d736SGuenter Roeck 				  const char *buf, size_t count)
3324453d736SGuenter Roeck {
3334453d736SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);
3344453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
335e8667296SClemens Ladisch 	unsigned long val;
3364453d736SGuenter Roeck 	int diff, hyst;
3374453d736SGuenter Roeck 	int err;
3384453d736SGuenter Roeck 	int ret = count;
3394453d736SGuenter Roeck 
3404453d736SGuenter Roeck 	if (strict_strtoul(buf, 10, &val) < 0)
3414453d736SGuenter Roeck 		return -EINVAL;
3424453d736SGuenter Roeck 
3434453d736SGuenter Roeck 	diff = jc42_temp_from_reg(data->temp_crit) - val;
3444453d736SGuenter Roeck 	hyst = 0;
3454453d736SGuenter Roeck 	if (diff > 0) {
3464453d736SGuenter Roeck 		if (diff < 2250)
3474453d736SGuenter Roeck 			hyst = 1;	/* 1.5 degrees C */
3484453d736SGuenter Roeck 		else if (diff < 4500)
3494453d736SGuenter Roeck 			hyst = 2;	/* 3.0 degrees C */
3504453d736SGuenter Roeck 		else
3514453d736SGuenter Roeck 			hyst = 3;	/* 6.0 degrees C */
3524453d736SGuenter Roeck 	}
3534453d736SGuenter Roeck 
3544453d736SGuenter Roeck 	mutex_lock(&data->update_lock);
3554453d736SGuenter Roeck 	data->config = (data->config
3564453d736SGuenter Roeck 			& ~(JC42_CFG_HYST_MASK << JC42_CFG_HYST_SHIFT))
3574453d736SGuenter Roeck 	  | (hyst << JC42_CFG_HYST_SHIFT);
358*90f4102cSJean Delvare 	err = i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG,
359*90f4102cSJean Delvare 					   data->config);
3604453d736SGuenter Roeck 	if (err < 0)
3614453d736SGuenter Roeck 		ret = err;
3624453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);
3634453d736SGuenter Roeck 	return ret;
3644453d736SGuenter Roeck }
3654453d736SGuenter Roeck 
3664453d736SGuenter Roeck static ssize_t show_alarm(struct device *dev,
3674453d736SGuenter Roeck 			  struct device_attribute *attr, char *buf)
3684453d736SGuenter Roeck {
3694453d736SGuenter Roeck 	u16 bit = to_sensor_dev_attr(attr)->index;
3704453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
3714453d736SGuenter Roeck 	u16 val;
3724453d736SGuenter Roeck 
3734453d736SGuenter Roeck 	if (IS_ERR(data))
3744453d736SGuenter Roeck 		return PTR_ERR(data);
3754453d736SGuenter Roeck 
3764453d736SGuenter Roeck 	val = data->temp_input;
3774453d736SGuenter Roeck 	if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY))
3784453d736SGuenter Roeck 		val = 0;
3794453d736SGuenter Roeck 	return sprintf(buf, "%u\n", (val >> bit) & 1);
3804453d736SGuenter Roeck }
3814453d736SGuenter Roeck 
3824453d736SGuenter Roeck static DEVICE_ATTR(temp1_input, S_IRUGO,
3834453d736SGuenter Roeck 		   show_temp_input, NULL);
3842c6315daSClemens Ladisch static DEVICE_ATTR(temp1_crit, S_IRUGO,
3854453d736SGuenter Roeck 		   show_temp_crit, set_temp_crit);
3862c6315daSClemens Ladisch static DEVICE_ATTR(temp1_min, S_IRUGO,
3874453d736SGuenter Roeck 		   show_temp_min, set_temp_min);
3882c6315daSClemens Ladisch static DEVICE_ATTR(temp1_max, S_IRUGO,
3894453d736SGuenter Roeck 		   show_temp_max, set_temp_max);
3904453d736SGuenter Roeck 
3912c6315daSClemens Ladisch static DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
3924453d736SGuenter Roeck 		   show_temp_crit_hyst, set_temp_crit_hyst);
3934453d736SGuenter Roeck static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
3944453d736SGuenter Roeck 		   show_temp_max_hyst, NULL);
3954453d736SGuenter Roeck 
3964453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
3974453d736SGuenter Roeck 			  JC42_ALARM_CRIT_BIT);
3984453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
3994453d736SGuenter Roeck 			  JC42_ALARM_MIN_BIT);
4004453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
4014453d736SGuenter Roeck 			  JC42_ALARM_MAX_BIT);
4024453d736SGuenter Roeck 
4034453d736SGuenter Roeck static struct attribute *jc42_attributes[] = {
4044453d736SGuenter Roeck 	&dev_attr_temp1_input.attr,
4054453d736SGuenter Roeck 	&dev_attr_temp1_crit.attr,
4064453d736SGuenter Roeck 	&dev_attr_temp1_min.attr,
4074453d736SGuenter Roeck 	&dev_attr_temp1_max.attr,
4084453d736SGuenter Roeck 	&dev_attr_temp1_crit_hyst.attr,
4094453d736SGuenter Roeck 	&dev_attr_temp1_max_hyst.attr,
4104453d736SGuenter Roeck 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
4114453d736SGuenter Roeck 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
4124453d736SGuenter Roeck 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
4134453d736SGuenter Roeck 	NULL
4144453d736SGuenter Roeck };
4154453d736SGuenter Roeck 
4162c6315daSClemens Ladisch static mode_t jc42_attribute_mode(struct kobject *kobj,
4172c6315daSClemens Ladisch 				  struct attribute *attr, int index)
4182c6315daSClemens Ladisch {
4192c6315daSClemens Ladisch 	struct device *dev = container_of(kobj, struct device, kobj);
4202c6315daSClemens Ladisch 	struct i2c_client *client = to_i2c_client(dev);
4212c6315daSClemens Ladisch 	struct jc42_data *data = i2c_get_clientdata(client);
4222c6315daSClemens Ladisch 	unsigned int config = data->config;
4232c6315daSClemens Ladisch 	bool readonly;
4242c6315daSClemens Ladisch 
4252c6315daSClemens Ladisch 	if (attr == &dev_attr_temp1_crit.attr)
4262c6315daSClemens Ladisch 		readonly = config & JC42_CFG_TCRIT_LOCK;
4272c6315daSClemens Ladisch 	else if (attr == &dev_attr_temp1_min.attr ||
4282c6315daSClemens Ladisch 		 attr == &dev_attr_temp1_max.attr)
4292c6315daSClemens Ladisch 		readonly = config & JC42_CFG_EVENT_LOCK;
4302c6315daSClemens Ladisch 	else if (attr == &dev_attr_temp1_crit_hyst.attr)
4312c6315daSClemens Ladisch 		readonly = config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK);
4322c6315daSClemens Ladisch 	else
4332c6315daSClemens Ladisch 		readonly = true;
4342c6315daSClemens Ladisch 
4352c6315daSClemens Ladisch 	return S_IRUGO | (readonly ? 0 : S_IWUSR);
4362c6315daSClemens Ladisch }
4372c6315daSClemens Ladisch 
4384453d736SGuenter Roeck static const struct attribute_group jc42_group = {
4394453d736SGuenter Roeck 	.attrs = jc42_attributes,
4402c6315daSClemens Ladisch 	.is_visible = jc42_attribute_mode,
4414453d736SGuenter Roeck };
4424453d736SGuenter Roeck 
4434453d736SGuenter Roeck /* Return 0 if detection is successful, -ENODEV otherwise */
4444453d736SGuenter Roeck static int jc42_detect(struct i2c_client *new_client,
4454453d736SGuenter Roeck 		       struct i2c_board_info *info)
4464453d736SGuenter Roeck {
4474453d736SGuenter Roeck 	struct i2c_adapter *adapter = new_client->adapter;
4484453d736SGuenter Roeck 	int i, config, cap, manid, devid;
4494453d736SGuenter Roeck 
4504453d736SGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
4514453d736SGuenter Roeck 				     I2C_FUNC_SMBUS_WORD_DATA))
4524453d736SGuenter Roeck 		return -ENODEV;
4534453d736SGuenter Roeck 
454*90f4102cSJean Delvare 	cap = i2c_smbus_read_word_swapped(new_client, JC42_REG_CAP);
455*90f4102cSJean Delvare 	config = i2c_smbus_read_word_swapped(new_client, JC42_REG_CONFIG);
456*90f4102cSJean Delvare 	manid = i2c_smbus_read_word_swapped(new_client, JC42_REG_MANID);
457*90f4102cSJean Delvare 	devid = i2c_smbus_read_word_swapped(new_client, JC42_REG_DEVICEID);
4584453d736SGuenter Roeck 
4594453d736SGuenter Roeck 	if (cap < 0 || config < 0 || manid < 0 || devid < 0)
4604453d736SGuenter Roeck 		return -ENODEV;
4614453d736SGuenter Roeck 
4624453d736SGuenter Roeck 	if ((cap & 0xff00) || (config & 0xf800))
4634453d736SGuenter Roeck 		return -ENODEV;
4644453d736SGuenter Roeck 
4654453d736SGuenter Roeck 	for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) {
4664453d736SGuenter Roeck 		struct jc42_chips *chip = &jc42_chips[i];
4674453d736SGuenter Roeck 		if (manid == chip->manid &&
4684453d736SGuenter Roeck 		    (devid & chip->devid_mask) == chip->devid) {
4694453d736SGuenter Roeck 			strlcpy(info->type, "jc42", I2C_NAME_SIZE);
4704453d736SGuenter Roeck 			return 0;
4714453d736SGuenter Roeck 		}
4724453d736SGuenter Roeck 	}
4734453d736SGuenter Roeck 	return -ENODEV;
4744453d736SGuenter Roeck }
4754453d736SGuenter Roeck 
4764453d736SGuenter Roeck static int jc42_probe(struct i2c_client *new_client,
4774453d736SGuenter Roeck 		      const struct i2c_device_id *id)
4784453d736SGuenter Roeck {
4794453d736SGuenter Roeck 	struct jc42_data *data;
4804453d736SGuenter Roeck 	int config, cap, err;
4814453d736SGuenter Roeck 
4824453d736SGuenter Roeck 	data = kzalloc(sizeof(struct jc42_data), GFP_KERNEL);
4834453d736SGuenter Roeck 	if (!data) {
4844453d736SGuenter Roeck 		err = -ENOMEM;
4854453d736SGuenter Roeck 		goto exit;
4864453d736SGuenter Roeck 	}
4874453d736SGuenter Roeck 
4884453d736SGuenter Roeck 	i2c_set_clientdata(new_client, data);
4894453d736SGuenter Roeck 	mutex_init(&data->update_lock);
4904453d736SGuenter Roeck 
491*90f4102cSJean Delvare 	cap = i2c_smbus_read_word_swapped(new_client, JC42_REG_CAP);
4924453d736SGuenter Roeck 	if (cap < 0) {
4934453d736SGuenter Roeck 		err = -EINVAL;
4944453d736SGuenter Roeck 		goto exit_free;
4954453d736SGuenter Roeck 	}
4964453d736SGuenter Roeck 	data->extended = !!(cap & JC42_CAP_RANGE);
4974453d736SGuenter Roeck 
498*90f4102cSJean Delvare 	config = i2c_smbus_read_word_swapped(new_client, JC42_REG_CONFIG);
4994453d736SGuenter Roeck 	if (config < 0) {
5004453d736SGuenter Roeck 		err = -EINVAL;
5014453d736SGuenter Roeck 		goto exit_free;
5024453d736SGuenter Roeck 	}
5034453d736SGuenter Roeck 	data->orig_config = config;
5044453d736SGuenter Roeck 	if (config & JC42_CFG_SHUTDOWN) {
5054453d736SGuenter Roeck 		config &= ~JC42_CFG_SHUTDOWN;
506*90f4102cSJean Delvare 		i2c_smbus_write_word_swapped(new_client, JC42_REG_CONFIG,
507*90f4102cSJean Delvare 					     config);
5084453d736SGuenter Roeck 	}
5094453d736SGuenter Roeck 	data->config = config;
5104453d736SGuenter Roeck 
5114453d736SGuenter Roeck 	/* Register sysfs hooks */
5124453d736SGuenter Roeck 	err = sysfs_create_group(&new_client->dev.kobj, &jc42_group);
5134453d736SGuenter Roeck 	if (err)
5144453d736SGuenter Roeck 		goto exit_free;
5154453d736SGuenter Roeck 
5164453d736SGuenter Roeck 	data->hwmon_dev = hwmon_device_register(&new_client->dev);
5174453d736SGuenter Roeck 	if (IS_ERR(data->hwmon_dev)) {
5184453d736SGuenter Roeck 		err = PTR_ERR(data->hwmon_dev);
5194453d736SGuenter Roeck 		goto exit_remove;
5204453d736SGuenter Roeck 	}
5214453d736SGuenter Roeck 
5224453d736SGuenter Roeck 	return 0;
5234453d736SGuenter Roeck 
5244453d736SGuenter Roeck exit_remove:
5254453d736SGuenter Roeck 	sysfs_remove_group(&new_client->dev.kobj, &jc42_group);
5264453d736SGuenter Roeck exit_free:
5274453d736SGuenter Roeck 	kfree(data);
5284453d736SGuenter Roeck exit:
5294453d736SGuenter Roeck 	return err;
5304453d736SGuenter Roeck }
5314453d736SGuenter Roeck 
5324453d736SGuenter Roeck static int jc42_remove(struct i2c_client *client)
5334453d736SGuenter Roeck {
5344453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
5354453d736SGuenter Roeck 	hwmon_device_unregister(data->hwmon_dev);
5364453d736SGuenter Roeck 	sysfs_remove_group(&client->dev.kobj, &jc42_group);
5374453d736SGuenter Roeck 	if (data->config != data->orig_config)
538*90f4102cSJean Delvare 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG,
539*90f4102cSJean Delvare 					     data->orig_config);
5404453d736SGuenter Roeck 	kfree(data);
5414453d736SGuenter Roeck 	return 0;
5424453d736SGuenter Roeck }
5434453d736SGuenter Roeck 
5444453d736SGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev)
5454453d736SGuenter Roeck {
5464453d736SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);
5474453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
5484453d736SGuenter Roeck 	struct jc42_data *ret = data;
5494453d736SGuenter Roeck 	int val;
5504453d736SGuenter Roeck 
5514453d736SGuenter Roeck 	mutex_lock(&data->update_lock);
5524453d736SGuenter Roeck 
5534453d736SGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
554*90f4102cSJean Delvare 		val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP);
5554453d736SGuenter Roeck 		if (val < 0) {
5564453d736SGuenter Roeck 			ret = ERR_PTR(val);
5574453d736SGuenter Roeck 			goto abort;
5584453d736SGuenter Roeck 		}
5594453d736SGuenter Roeck 		data->temp_input = val;
5604453d736SGuenter Roeck 
561*90f4102cSJean Delvare 		val = i2c_smbus_read_word_swapped(client,
562*90f4102cSJean Delvare 						  JC42_REG_TEMP_CRITICAL);
5634453d736SGuenter Roeck 		if (val < 0) {
5644453d736SGuenter Roeck 			ret = ERR_PTR(val);
5654453d736SGuenter Roeck 			goto abort;
5664453d736SGuenter Roeck 		}
5674453d736SGuenter Roeck 		data->temp_crit = val;
5684453d736SGuenter Roeck 
569*90f4102cSJean Delvare 		val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP_LOWER);
5704453d736SGuenter Roeck 		if (val < 0) {
5714453d736SGuenter Roeck 			ret = ERR_PTR(val);
5724453d736SGuenter Roeck 			goto abort;
5734453d736SGuenter Roeck 		}
5744453d736SGuenter Roeck 		data->temp_min = val;
5754453d736SGuenter Roeck 
576*90f4102cSJean Delvare 		val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP_UPPER);
5774453d736SGuenter Roeck 		if (val < 0) {
5784453d736SGuenter Roeck 			ret = ERR_PTR(val);
5794453d736SGuenter Roeck 			goto abort;
5804453d736SGuenter Roeck 		}
5814453d736SGuenter Roeck 		data->temp_max = val;
5824453d736SGuenter Roeck 
5834453d736SGuenter Roeck 		data->last_updated = jiffies;
5844453d736SGuenter Roeck 		data->valid = true;
5854453d736SGuenter Roeck 	}
5864453d736SGuenter Roeck abort:
5874453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);
5884453d736SGuenter Roeck 	return ret;
5894453d736SGuenter Roeck }
5904453d736SGuenter Roeck 
5914453d736SGuenter Roeck static int __init sensors_jc42_init(void)
5924453d736SGuenter Roeck {
5934453d736SGuenter Roeck 	return i2c_add_driver(&jc42_driver);
5944453d736SGuenter Roeck }
5954453d736SGuenter Roeck 
5964453d736SGuenter Roeck static void __exit sensors_jc42_exit(void)
5974453d736SGuenter Roeck {
5984453d736SGuenter Roeck 	i2c_del_driver(&jc42_driver);
5994453d736SGuenter Roeck }
6004453d736SGuenter Roeck 
6014453d736SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
6024453d736SGuenter Roeck MODULE_DESCRIPTION("JC42 driver");
6034453d736SGuenter Roeck MODULE_LICENSE("GPL");
6044453d736SGuenter Roeck 
6054453d736SGuenter Roeck module_init(sensors_jc42_init);
6064453d736SGuenter Roeck module_exit(sensors_jc42_exit);
607