xref: /openbmc/linux/drivers/hwmon/jc42.c (revision d397276b0c833b0089cb19d94fbc35a43ada58a7)
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
602ccc8731SJean Delvare #define JC42_CFG_HYST_MASK	(0x03 << 9)
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 */
671bd612a2SGuenter Roeck #define ATMEL_MANID		0x001f  /* Atmel */
684453d736SGuenter Roeck #define MAX_MANID		0x004d  /* Maxim */
694453d736SGuenter Roeck #define IDT_MANID		0x00b3  /* IDT */
704453d736SGuenter Roeck #define MCP_MANID		0x0054  /* Microchip */
714453d736SGuenter Roeck #define NXP_MANID		0x1131  /* NXP Semiconductors */
724453d736SGuenter Roeck #define ONS_MANID		0x1b09  /* ON Semiconductor */
734453d736SGuenter Roeck #define STM_MANID		0x104a  /* ST Microelectronics */
744453d736SGuenter Roeck 
754453d736SGuenter Roeck /* Supported chips */
764453d736SGuenter Roeck 
774453d736SGuenter Roeck /* Analog Devices */
784453d736SGuenter Roeck #define ADT7408_DEVID		0x0801
794453d736SGuenter Roeck #define ADT7408_DEVID_MASK	0xffff
804453d736SGuenter Roeck 
811bd612a2SGuenter Roeck /* Atmel */
821bd612a2SGuenter Roeck #define AT30TS00_DEVID		0x8201
831bd612a2SGuenter Roeck #define AT30TS00_DEVID_MASK	0xffff
841bd612a2SGuenter Roeck 
854453d736SGuenter Roeck /* IDT */
864453d736SGuenter Roeck #define TS3000B3_DEVID		0x2903  /* Also matches TSE2002B3 */
874453d736SGuenter Roeck #define TS3000B3_DEVID_MASK	0xffff
884453d736SGuenter Roeck 
891bd612a2SGuenter Roeck #define TS3000GB2_DEVID		0x2912  /* Also matches TSE2002GB2 */
901bd612a2SGuenter Roeck #define TS3000GB2_DEVID_MASK	0xffff
911bd612a2SGuenter Roeck 
924453d736SGuenter Roeck /* Maxim */
934453d736SGuenter Roeck #define MAX6604_DEVID		0x3e00
944453d736SGuenter Roeck #define MAX6604_DEVID_MASK	0xffff
954453d736SGuenter Roeck 
964453d736SGuenter Roeck /* Microchip */
971bd612a2SGuenter Roeck #define MCP9804_DEVID		0x0200
981bd612a2SGuenter Roeck #define MCP9804_DEVID_MASK	0xfffc
991bd612a2SGuenter Roeck 
1004453d736SGuenter Roeck #define MCP98242_DEVID		0x2000
1014453d736SGuenter Roeck #define MCP98242_DEVID_MASK	0xfffc
1024453d736SGuenter Roeck 
1034453d736SGuenter Roeck #define MCP98243_DEVID		0x2100
1044453d736SGuenter Roeck #define MCP98243_DEVID_MASK	0xfffc
1054453d736SGuenter Roeck 
106d4768280SGuenter Roeck #define MCP98244_DEVID		0x2200
107d4768280SGuenter Roeck #define MCP98244_DEVID_MASK	0xfffc
108d4768280SGuenter Roeck 
1094453d736SGuenter Roeck #define MCP9843_DEVID		0x0000	/* Also matches mcp9805 */
1104453d736SGuenter Roeck #define MCP9843_DEVID_MASK	0xfffe
1114453d736SGuenter Roeck 
1124453d736SGuenter Roeck /* NXP */
1134453d736SGuenter Roeck #define SE97_DEVID		0xa200
1144453d736SGuenter Roeck #define SE97_DEVID_MASK		0xfffc
1154453d736SGuenter Roeck 
1164453d736SGuenter Roeck #define SE98_DEVID		0xa100
1174453d736SGuenter Roeck #define SE98_DEVID_MASK		0xfffc
1184453d736SGuenter Roeck 
1194453d736SGuenter Roeck /* ON Semiconductor */
1204453d736SGuenter Roeck #define CAT6095_DEVID		0x0800	/* Also matches CAT34TS02 */
1214453d736SGuenter Roeck #define CAT6095_DEVID_MASK	0xffe0
1224453d736SGuenter Roeck 
1234453d736SGuenter Roeck /* ST Microelectronics */
1244453d736SGuenter Roeck #define STTS424_DEVID		0x0101
1254453d736SGuenter Roeck #define STTS424_DEVID_MASK	0xffff
1264453d736SGuenter Roeck 
1274453d736SGuenter Roeck #define STTS424E_DEVID		0x0000
1284453d736SGuenter Roeck #define STTS424E_DEVID_MASK	0xfffe
1294453d736SGuenter Roeck 
1304de86126SJean Delvare #define STTS2002_DEVID		0x0300
1314de86126SJean Delvare #define STTS2002_DEVID_MASK	0xffff
1324de86126SJean Delvare 
1334de86126SJean Delvare #define STTS3000_DEVID		0x0200
1344de86126SJean Delvare #define STTS3000_DEVID_MASK	0xffff
1354de86126SJean Delvare 
1364453d736SGuenter Roeck static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 };
1374453d736SGuenter Roeck 
1384453d736SGuenter Roeck struct jc42_chips {
1394453d736SGuenter Roeck 	u16 manid;
1404453d736SGuenter Roeck 	u16 devid;
1414453d736SGuenter Roeck 	u16 devid_mask;
1424453d736SGuenter Roeck };
1434453d736SGuenter Roeck 
1444453d736SGuenter Roeck static struct jc42_chips jc42_chips[] = {
1454453d736SGuenter Roeck 	{ ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK },
1461bd612a2SGuenter Roeck 	{ ATMEL_MANID, AT30TS00_DEVID, AT30TS00_DEVID_MASK },
1474453d736SGuenter Roeck 	{ IDT_MANID, TS3000B3_DEVID, TS3000B3_DEVID_MASK },
1481bd612a2SGuenter Roeck 	{ IDT_MANID, TS3000GB2_DEVID, TS3000GB2_DEVID_MASK },
1494453d736SGuenter Roeck 	{ MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK },
1501bd612a2SGuenter Roeck 	{ MCP_MANID, MCP9804_DEVID, MCP9804_DEVID_MASK },
1514453d736SGuenter Roeck 	{ MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK },
1524453d736SGuenter Roeck 	{ MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK },
153d4768280SGuenter Roeck 	{ MCP_MANID, MCP98244_DEVID, MCP98244_DEVID_MASK },
1544453d736SGuenter Roeck 	{ MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK },
1554453d736SGuenter Roeck 	{ NXP_MANID, SE97_DEVID, SE97_DEVID_MASK },
1564453d736SGuenter Roeck 	{ ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK },
1574453d736SGuenter Roeck 	{ NXP_MANID, SE98_DEVID, SE98_DEVID_MASK },
1584453d736SGuenter Roeck 	{ STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK },
1594453d736SGuenter Roeck 	{ STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK },
1604de86126SJean Delvare 	{ STM_MANID, STTS2002_DEVID, STTS2002_DEVID_MASK },
1614de86126SJean Delvare 	{ STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK },
1624453d736SGuenter Roeck };
1634453d736SGuenter Roeck 
1644453d736SGuenter Roeck /* Each client has this additional data */
1654453d736SGuenter Roeck struct jc42_data {
16662f9a57cSGuenter Roeck 	struct i2c_client *client;
1674453d736SGuenter Roeck 	struct mutex	update_lock;	/* protect register access */
1684453d736SGuenter Roeck 	bool		extended;	/* true if extended range supported */
1694453d736SGuenter Roeck 	bool		valid;
1704453d736SGuenter Roeck 	unsigned long	last_updated;	/* In jiffies */
1714453d736SGuenter Roeck 	u16		orig_config;	/* original configuration */
1724453d736SGuenter Roeck 	u16		config;		/* current configuration */
1734453d736SGuenter Roeck 	u16		temp_input;	/* Temperatures */
1744453d736SGuenter Roeck 	u16		temp_crit;
1754453d736SGuenter Roeck 	u16		temp_min;
1764453d736SGuenter Roeck 	u16		temp_max;
1774453d736SGuenter Roeck };
1784453d736SGuenter Roeck 
1794453d736SGuenter Roeck #define JC42_TEMP_MIN_EXTENDED	(-40000)
1804453d736SGuenter Roeck #define JC42_TEMP_MIN		0
1814453d736SGuenter Roeck #define JC42_TEMP_MAX		125000
1824453d736SGuenter Roeck 
1834453d736SGuenter Roeck static u16 jc42_temp_to_reg(int temp, bool extended)
1844453d736SGuenter Roeck {
1852a844c14SGuenter Roeck 	int ntemp = clamp_val(temp,
1864453d736SGuenter Roeck 			      extended ? JC42_TEMP_MIN_EXTENDED :
1874453d736SGuenter Roeck 			      JC42_TEMP_MIN, JC42_TEMP_MAX);
1884453d736SGuenter Roeck 
1894453d736SGuenter Roeck 	/* convert from 0.001 to 0.0625 resolution */
1904453d736SGuenter Roeck 	return (ntemp * 2 / 125) & 0x1fff;
1914453d736SGuenter Roeck }
1924453d736SGuenter Roeck 
1934453d736SGuenter Roeck static int jc42_temp_from_reg(s16 reg)
1944453d736SGuenter Roeck {
1954453d736SGuenter Roeck 	reg &= 0x1fff;
1964453d736SGuenter Roeck 
1974453d736SGuenter Roeck 	/* sign extend register */
1984453d736SGuenter Roeck 	if (reg & 0x1000)
1994453d736SGuenter Roeck 		reg |= 0xf000;
2004453d736SGuenter Roeck 
2014453d736SGuenter Roeck 	/* convert from 0.0625 to 0.001 resolution */
2024453d736SGuenter Roeck 	return reg * 125 / 2;
2034453d736SGuenter Roeck }
2044453d736SGuenter Roeck 
205*d397276bSGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev)
206*d397276bSGuenter Roeck {
207*d397276bSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
208*d397276bSGuenter Roeck 	struct i2c_client *client = data->client;
209*d397276bSGuenter Roeck 	struct jc42_data *ret = data;
210*d397276bSGuenter Roeck 	int val;
211*d397276bSGuenter Roeck 
212*d397276bSGuenter Roeck 	mutex_lock(&data->update_lock);
213*d397276bSGuenter Roeck 
214*d397276bSGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
215*d397276bSGuenter Roeck 		val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP);
216*d397276bSGuenter Roeck 		if (val < 0) {
217*d397276bSGuenter Roeck 			ret = ERR_PTR(val);
218*d397276bSGuenter Roeck 			goto abort;
219*d397276bSGuenter Roeck 		}
220*d397276bSGuenter Roeck 		data->temp_input = val;
221*d397276bSGuenter Roeck 
222*d397276bSGuenter Roeck 		val = i2c_smbus_read_word_swapped(client,
223*d397276bSGuenter Roeck 						  JC42_REG_TEMP_CRITICAL);
224*d397276bSGuenter Roeck 		if (val < 0) {
225*d397276bSGuenter Roeck 			ret = ERR_PTR(val);
226*d397276bSGuenter Roeck 			goto abort;
227*d397276bSGuenter Roeck 		}
228*d397276bSGuenter Roeck 		data->temp_crit = val;
229*d397276bSGuenter Roeck 
230*d397276bSGuenter Roeck 		val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP_LOWER);
231*d397276bSGuenter Roeck 		if (val < 0) {
232*d397276bSGuenter Roeck 			ret = ERR_PTR(val);
233*d397276bSGuenter Roeck 			goto abort;
234*d397276bSGuenter Roeck 		}
235*d397276bSGuenter Roeck 		data->temp_min = val;
236*d397276bSGuenter Roeck 
237*d397276bSGuenter Roeck 		val = i2c_smbus_read_word_swapped(client, JC42_REG_TEMP_UPPER);
238*d397276bSGuenter Roeck 		if (val < 0) {
239*d397276bSGuenter Roeck 			ret = ERR_PTR(val);
240*d397276bSGuenter Roeck 			goto abort;
241*d397276bSGuenter Roeck 		}
242*d397276bSGuenter Roeck 		data->temp_max = val;
243*d397276bSGuenter Roeck 
244*d397276bSGuenter Roeck 		data->last_updated = jiffies;
245*d397276bSGuenter Roeck 		data->valid = true;
246*d397276bSGuenter Roeck 	}
247*d397276bSGuenter Roeck abort:
248*d397276bSGuenter Roeck 	mutex_unlock(&data->update_lock);
249*d397276bSGuenter Roeck 	return ret;
250*d397276bSGuenter Roeck }
251*d397276bSGuenter 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);
2822ccc8731SJean Delvare 	hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
2832ccc8731SJean Delvare 			       >> JC42_CFG_HYST_SHIFT];
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);
2972ccc8731SJean Delvare 	hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
2982ccc8731SJean Delvare 			       >> JC42_CFG_HYST_SHIFT];
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 {									\
30862f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);			\
3094453d736SGuenter Roeck 	int err, ret = count;						\
3104453d736SGuenter Roeck 	long val;							\
311179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val) < 0)					\
3124453d736SGuenter Roeck 		return -EINVAL;						\
3134453d736SGuenter Roeck 	mutex_lock(&data->update_lock);					\
3144453d736SGuenter Roeck 	data->value = jc42_temp_to_reg(val, data->extended);		\
31562f9a57cSGuenter Roeck 	err = i2c_smbus_write_word_swapped(data->client, reg, data->value); \
3164453d736SGuenter Roeck 	if (err < 0)							\
3174453d736SGuenter Roeck 		ret = err;						\
3184453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);				\
3194453d736SGuenter Roeck 	return ret;							\
3204453d736SGuenter Roeck }
3214453d736SGuenter Roeck 
3224453d736SGuenter Roeck set(temp_min, JC42_REG_TEMP_LOWER);
3234453d736SGuenter Roeck set(temp_max, JC42_REG_TEMP_UPPER);
3244453d736SGuenter Roeck set(temp_crit, JC42_REG_TEMP_CRITICAL);
3254453d736SGuenter Roeck 
3265d577dbaSGuenter Roeck /*
3275d577dbaSGuenter Roeck  * JC42.4 compliant chips only support four hysteresis values.
3285d577dbaSGuenter Roeck  * Pick best choice and go from there.
3295d577dbaSGuenter Roeck  */
3304453d736SGuenter Roeck static ssize_t set_temp_crit_hyst(struct device *dev,
3314453d736SGuenter Roeck 				  struct device_attribute *attr,
3324453d736SGuenter Roeck 				  const char *buf, size_t count)
3334453d736SGuenter Roeck {
33462f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
335e8667296SClemens Ladisch 	unsigned long val;
3364453d736SGuenter Roeck 	int diff, hyst;
3374453d736SGuenter Roeck 	int err;
3384453d736SGuenter Roeck 	int ret = count;
3394453d736SGuenter Roeck 
340179c4fdbSFrans Meulenbroeks 	if (kstrtoul(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);
3552ccc8731SJean Delvare 	data->config = (data->config & ~JC42_CFG_HYST_MASK)
3564453d736SGuenter Roeck 	  | (hyst << JC42_CFG_HYST_SHIFT);
35762f9a57cSGuenter Roeck 	err = i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
35890f4102cSJean Delvare 					   data->config);
3594453d736SGuenter Roeck 	if (err < 0)
3604453d736SGuenter Roeck 		ret = err;
3614453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);
3624453d736SGuenter Roeck 	return ret;
3634453d736SGuenter Roeck }
3644453d736SGuenter Roeck 
3654453d736SGuenter Roeck static ssize_t show_alarm(struct device *dev,
3664453d736SGuenter Roeck 			  struct device_attribute *attr, char *buf)
3674453d736SGuenter Roeck {
3684453d736SGuenter Roeck 	u16 bit = to_sensor_dev_attr(attr)->index;
3694453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
3704453d736SGuenter Roeck 	u16 val;
3714453d736SGuenter Roeck 
3724453d736SGuenter Roeck 	if (IS_ERR(data))
3734453d736SGuenter Roeck 		return PTR_ERR(data);
3744453d736SGuenter Roeck 
3754453d736SGuenter Roeck 	val = data->temp_input;
3764453d736SGuenter Roeck 	if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY))
3774453d736SGuenter Roeck 		val = 0;
3784453d736SGuenter Roeck 	return sprintf(buf, "%u\n", (val >> bit) & 1);
3794453d736SGuenter Roeck }
3804453d736SGuenter Roeck 
3814453d736SGuenter Roeck static DEVICE_ATTR(temp1_input, S_IRUGO,
3824453d736SGuenter Roeck 		   show_temp_input, NULL);
3832c6315daSClemens Ladisch static DEVICE_ATTR(temp1_crit, S_IRUGO,
3844453d736SGuenter Roeck 		   show_temp_crit, set_temp_crit);
3852c6315daSClemens Ladisch static DEVICE_ATTR(temp1_min, S_IRUGO,
3864453d736SGuenter Roeck 		   show_temp_min, set_temp_min);
3872c6315daSClemens Ladisch static DEVICE_ATTR(temp1_max, S_IRUGO,
3884453d736SGuenter Roeck 		   show_temp_max, set_temp_max);
3894453d736SGuenter Roeck 
3902c6315daSClemens Ladisch static DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
3914453d736SGuenter Roeck 		   show_temp_crit_hyst, set_temp_crit_hyst);
3924453d736SGuenter Roeck static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
3934453d736SGuenter Roeck 		   show_temp_max_hyst, NULL);
3944453d736SGuenter Roeck 
3954453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
3964453d736SGuenter Roeck 			  JC42_ALARM_CRIT_BIT);
3974453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
3984453d736SGuenter Roeck 			  JC42_ALARM_MIN_BIT);
3994453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
4004453d736SGuenter Roeck 			  JC42_ALARM_MAX_BIT);
4014453d736SGuenter Roeck 
4024453d736SGuenter Roeck static struct attribute *jc42_attributes[] = {
4034453d736SGuenter Roeck 	&dev_attr_temp1_input.attr,
4044453d736SGuenter Roeck 	&dev_attr_temp1_crit.attr,
4054453d736SGuenter Roeck 	&dev_attr_temp1_min.attr,
4064453d736SGuenter Roeck 	&dev_attr_temp1_max.attr,
4074453d736SGuenter Roeck 	&dev_attr_temp1_crit_hyst.attr,
4084453d736SGuenter Roeck 	&dev_attr_temp1_max_hyst.attr,
4094453d736SGuenter Roeck 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
4104453d736SGuenter Roeck 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
4114453d736SGuenter Roeck 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
4124453d736SGuenter Roeck 	NULL
4134453d736SGuenter Roeck };
4144453d736SGuenter Roeck 
415587a1f16SAl Viro static umode_t jc42_attribute_mode(struct kobject *kobj,
4162c6315daSClemens Ladisch 				  struct attribute *attr, int index)
4172c6315daSClemens Ladisch {
4182c6315daSClemens Ladisch 	struct device *dev = container_of(kobj, struct device, kobj);
41962f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
4202c6315daSClemens Ladisch 	unsigned int config = data->config;
4212c6315daSClemens Ladisch 	bool readonly;
4222c6315daSClemens Ladisch 
4232c6315daSClemens Ladisch 	if (attr == &dev_attr_temp1_crit.attr)
4242c6315daSClemens Ladisch 		readonly = config & JC42_CFG_TCRIT_LOCK;
4252c6315daSClemens Ladisch 	else if (attr == &dev_attr_temp1_min.attr ||
4262c6315daSClemens Ladisch 		 attr == &dev_attr_temp1_max.attr)
4272c6315daSClemens Ladisch 		readonly = config & JC42_CFG_EVENT_LOCK;
4282c6315daSClemens Ladisch 	else if (attr == &dev_attr_temp1_crit_hyst.attr)
4292c6315daSClemens Ladisch 		readonly = config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK);
4302c6315daSClemens Ladisch 	else
4312c6315daSClemens Ladisch 		readonly = true;
4322c6315daSClemens Ladisch 
4332c6315daSClemens Ladisch 	return S_IRUGO | (readonly ? 0 : S_IWUSR);
4342c6315daSClemens Ladisch }
4352c6315daSClemens Ladisch 
4364453d736SGuenter Roeck static const struct attribute_group jc42_group = {
4374453d736SGuenter Roeck 	.attrs = jc42_attributes,
4382c6315daSClemens Ladisch 	.is_visible = jc42_attribute_mode,
4394453d736SGuenter Roeck };
44062f9a57cSGuenter Roeck __ATTRIBUTE_GROUPS(jc42);
4414453d736SGuenter Roeck 
4424453d736SGuenter Roeck /* Return 0 if detection is successful, -ENODEV otherwise */
443f15df57dSGuenter Roeck static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info)
4444453d736SGuenter Roeck {
445f15df57dSGuenter Roeck 	struct i2c_adapter *adapter = client->adapter;
4464453d736SGuenter Roeck 	int i, config, cap, manid, devid;
4474453d736SGuenter Roeck 
4484453d736SGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
4494453d736SGuenter Roeck 				     I2C_FUNC_SMBUS_WORD_DATA))
4504453d736SGuenter Roeck 		return -ENODEV;
4514453d736SGuenter Roeck 
452f15df57dSGuenter Roeck 	cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
453f15df57dSGuenter Roeck 	config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
454f15df57dSGuenter Roeck 	manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID);
455f15df57dSGuenter Roeck 	devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID);
4564453d736SGuenter Roeck 
4574453d736SGuenter Roeck 	if (cap < 0 || config < 0 || manid < 0 || devid < 0)
4584453d736SGuenter Roeck 		return -ENODEV;
4594453d736SGuenter Roeck 
4604453d736SGuenter Roeck 	if ((cap & 0xff00) || (config & 0xf800))
4614453d736SGuenter Roeck 		return -ENODEV;
4624453d736SGuenter Roeck 
4634453d736SGuenter Roeck 	for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) {
4644453d736SGuenter Roeck 		struct jc42_chips *chip = &jc42_chips[i];
4654453d736SGuenter Roeck 		if (manid == chip->manid &&
4664453d736SGuenter Roeck 		    (devid & chip->devid_mask) == chip->devid) {
4674453d736SGuenter Roeck 			strlcpy(info->type, "jc42", I2C_NAME_SIZE);
4684453d736SGuenter Roeck 			return 0;
4694453d736SGuenter Roeck 		}
4704453d736SGuenter Roeck 	}
4714453d736SGuenter Roeck 	return -ENODEV;
4724453d736SGuenter Roeck }
4734453d736SGuenter Roeck 
474f15df57dSGuenter Roeck static int jc42_probe(struct i2c_client *client, const struct i2c_device_id *id)
4754453d736SGuenter Roeck {
476f15df57dSGuenter Roeck 	struct device *dev = &client->dev;
47762f9a57cSGuenter Roeck 	struct device *hwmon_dev;
47862f9a57cSGuenter Roeck 	struct jc42_data *data;
47962f9a57cSGuenter Roeck 	int config, cap;
4804453d736SGuenter Roeck 
481f15df57dSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL);
482f15df57dSGuenter Roeck 	if (!data)
483f15df57dSGuenter Roeck 		return -ENOMEM;
4844453d736SGuenter Roeck 
48562f9a57cSGuenter Roeck 	data->client = client;
486f15df57dSGuenter Roeck 	i2c_set_clientdata(client, data);
4874453d736SGuenter Roeck 	mutex_init(&data->update_lock);
4884453d736SGuenter Roeck 
489f15df57dSGuenter Roeck 	cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
490f15df57dSGuenter Roeck 	if (cap < 0)
491f15df57dSGuenter Roeck 		return cap;
492f15df57dSGuenter Roeck 
4934453d736SGuenter Roeck 	data->extended = !!(cap & JC42_CAP_RANGE);
4944453d736SGuenter Roeck 
495f15df57dSGuenter Roeck 	config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
496f15df57dSGuenter Roeck 	if (config < 0)
497f15df57dSGuenter Roeck 		return config;
498f15df57dSGuenter Roeck 
4994453d736SGuenter Roeck 	data->orig_config = config;
5004453d736SGuenter Roeck 	if (config & JC42_CFG_SHUTDOWN) {
5014453d736SGuenter Roeck 		config &= ~JC42_CFG_SHUTDOWN;
502f15df57dSGuenter Roeck 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
5034453d736SGuenter Roeck 	}
5044453d736SGuenter Roeck 	data->config = config;
5054453d736SGuenter Roeck 
50662f9a57cSGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
50762f9a57cSGuenter Roeck 							   data,
50862f9a57cSGuenter Roeck 							   jc42_groups);
509650a2c02SFengguang Wu 	return PTR_ERR_OR_ZERO(hwmon_dev);
5104453d736SGuenter Roeck }
5114453d736SGuenter Roeck 
5124453d736SGuenter Roeck static int jc42_remove(struct i2c_client *client)
5134453d736SGuenter Roeck {
5144453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
5155953e276SJean Delvare 
5165953e276SJean Delvare 	/* Restore original configuration except hysteresis */
5175953e276SJean Delvare 	if ((data->config & ~JC42_CFG_HYST_MASK) !=
5185953e276SJean Delvare 	    (data->orig_config & ~JC42_CFG_HYST_MASK)) {
5195953e276SJean Delvare 		int config;
5205953e276SJean Delvare 
5215953e276SJean Delvare 		config = (data->orig_config & ~JC42_CFG_HYST_MASK)
5225953e276SJean Delvare 		  | (data->config & JC42_CFG_HYST_MASK);
5235953e276SJean Delvare 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
5245953e276SJean Delvare 	}
5254453d736SGuenter Roeck 	return 0;
5264453d736SGuenter Roeck }
5274453d736SGuenter Roeck 
528*d397276bSGuenter Roeck #ifdef CONFIG_PM
529*d397276bSGuenter Roeck 
530*d397276bSGuenter Roeck static int jc42_suspend(struct device *dev)
5314453d736SGuenter Roeck {
53262f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
5334453d736SGuenter Roeck 
534*d397276bSGuenter Roeck 	data->config |= JC42_CFG_SHUTDOWN;
535*d397276bSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
536*d397276bSGuenter Roeck 				     data->config);
537*d397276bSGuenter Roeck 	return 0;
538*d397276bSGuenter Roeck }
5394453d736SGuenter Roeck 
540*d397276bSGuenter Roeck static int jc42_resume(struct device *dev)
541*d397276bSGuenter Roeck {
542*d397276bSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
5434453d736SGuenter Roeck 
544*d397276bSGuenter Roeck 	data->config &= ~JC42_CFG_SHUTDOWN;
545*d397276bSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
546*d397276bSGuenter Roeck 				     data->config);
547*d397276bSGuenter Roeck 	return 0;
5484453d736SGuenter Roeck }
5494453d736SGuenter Roeck 
550*d397276bSGuenter Roeck static const struct dev_pm_ops jc42_dev_pm_ops = {
551*d397276bSGuenter Roeck 	.suspend = jc42_suspend,
552*d397276bSGuenter Roeck 	.resume = jc42_resume,
553*d397276bSGuenter Roeck };
5544453d736SGuenter Roeck 
555*d397276bSGuenter Roeck #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops)
556*d397276bSGuenter Roeck #else
557*d397276bSGuenter Roeck #define JC42_DEV_PM_OPS NULL
558*d397276bSGuenter Roeck #endif /* CONFIG_PM */
5594453d736SGuenter Roeck 
560*d397276bSGuenter Roeck static const struct i2c_device_id jc42_id[] = {
561*d397276bSGuenter Roeck 	{ "jc42", 0 },
562*d397276bSGuenter Roeck 	{ }
563*d397276bSGuenter Roeck };
564*d397276bSGuenter Roeck MODULE_DEVICE_TABLE(i2c, jc42_id);
565*d397276bSGuenter Roeck 
566*d397276bSGuenter Roeck static struct i2c_driver jc42_driver = {
567*d397276bSGuenter Roeck 	.class		= I2C_CLASS_SPD,
568*d397276bSGuenter Roeck 	.driver = {
569*d397276bSGuenter Roeck 		.name	= "jc42",
570*d397276bSGuenter Roeck 		.pm = JC42_DEV_PM_OPS,
571*d397276bSGuenter Roeck 	},
572*d397276bSGuenter Roeck 	.probe		= jc42_probe,
573*d397276bSGuenter Roeck 	.remove		= jc42_remove,
574*d397276bSGuenter Roeck 	.id_table	= jc42_id,
575*d397276bSGuenter Roeck 	.detect		= jc42_detect,
576*d397276bSGuenter Roeck 	.address_list	= normal_i2c,
577*d397276bSGuenter Roeck };
5784453d736SGuenter Roeck 
579f0967eeaSAxel Lin module_i2c_driver(jc42_driver);
5804453d736SGuenter Roeck 
581bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
5824453d736SGuenter Roeck MODULE_DESCRIPTION("JC42 driver");
5834453d736SGuenter Roeck MODULE_LICENSE("GPL");
584