xref: /openbmc/linux/drivers/hwmon/jc42.c (revision 99b981b23b8585ab8cd86a24178f03f17f2f6109)
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/err.h>
324453d736SGuenter Roeck #include <linux/mutex.h>
33803decceSGuenter Roeck #include <linux/of.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 */
68175c490cSGuenter Roeck #define ATMEL_MANID2		0x1114	/* Atmel */
694453d736SGuenter Roeck #define MAX_MANID		0x004d  /* Maxim */
704453d736SGuenter Roeck #define IDT_MANID		0x00b3  /* IDT */
714453d736SGuenter Roeck #define MCP_MANID		0x0054  /* Microchip */
724453d736SGuenter Roeck #define NXP_MANID		0x1131  /* NXP Semiconductors */
734453d736SGuenter Roeck #define ONS_MANID		0x1b09  /* ON Semiconductor */
744453d736SGuenter Roeck #define STM_MANID		0x104a  /* ST Microelectronics */
75568003ceSGuenter Roeck #define GT_MANID		0x1c68	/* Giantec */
76568003ceSGuenter Roeck #define GT_MANID2		0x132d	/* Giantec, 2nd mfg ID */
774453d736SGuenter Roeck 
784453d736SGuenter Roeck /* Supported chips */
794453d736SGuenter Roeck 
804453d736SGuenter Roeck /* Analog Devices */
814453d736SGuenter Roeck #define ADT7408_DEVID		0x0801
824453d736SGuenter Roeck #define ADT7408_DEVID_MASK	0xffff
834453d736SGuenter Roeck 
841bd612a2SGuenter Roeck /* Atmel */
851bd612a2SGuenter Roeck #define AT30TS00_DEVID		0x8201
861bd612a2SGuenter Roeck #define AT30TS00_DEVID_MASK	0xffff
871bd612a2SGuenter Roeck 
88175c490cSGuenter Roeck #define AT30TSE004_DEVID	0x2200
89175c490cSGuenter Roeck #define AT30TSE004_DEVID_MASK	0xffff
90175c490cSGuenter Roeck 
91568003ceSGuenter Roeck /* Giantec */
92568003ceSGuenter Roeck #define GT30TS00_DEVID		0x2200
93568003ceSGuenter Roeck #define GT30TS00_DEVID_MASK	0xff00
94568003ceSGuenter Roeck 
95568003ceSGuenter Roeck #define GT34TS02_DEVID		0x3300
96568003ceSGuenter Roeck #define GT34TS02_DEVID_MASK	0xff00
97568003ceSGuenter Roeck 
984453d736SGuenter Roeck /* IDT */
990ea2f1dbSGuenter Roeck #define TSE2004_DEVID		0x2200
1000ea2f1dbSGuenter Roeck #define TSE2004_DEVID_MASK	0xff00
1014453d736SGuenter Roeck 
1020ea2f1dbSGuenter Roeck #define TS3000_DEVID		0x2900  /* Also matches TSE2002 */
1030ea2f1dbSGuenter Roeck #define TS3000_DEVID_MASK	0xff00
1040ea2f1dbSGuenter Roeck 
1050ea2f1dbSGuenter Roeck #define TS3001_DEVID		0x3000
1060ea2f1dbSGuenter Roeck #define TS3001_DEVID_MASK	0xff00
1071bd612a2SGuenter Roeck 
1084453d736SGuenter Roeck /* Maxim */
1094453d736SGuenter Roeck #define MAX6604_DEVID		0x3e00
1104453d736SGuenter Roeck #define MAX6604_DEVID_MASK	0xffff
1114453d736SGuenter Roeck 
1124453d736SGuenter Roeck /* Microchip */
1131bd612a2SGuenter Roeck #define MCP9804_DEVID		0x0200
1141bd612a2SGuenter Roeck #define MCP9804_DEVID_MASK	0xfffc
1151bd612a2SGuenter Roeck 
116a31887dcSAlison Schofield #define MCP9808_DEVID		0x0400
117a31887dcSAlison Schofield #define MCP9808_DEVID_MASK	0xfffc
118a31887dcSAlison Schofield 
1194453d736SGuenter Roeck #define MCP98242_DEVID		0x2000
1204453d736SGuenter Roeck #define MCP98242_DEVID_MASK	0xfffc
1214453d736SGuenter Roeck 
1224453d736SGuenter Roeck #define MCP98243_DEVID		0x2100
1234453d736SGuenter Roeck #define MCP98243_DEVID_MASK	0xfffc
1244453d736SGuenter Roeck 
125d4768280SGuenter Roeck #define MCP98244_DEVID		0x2200
126d4768280SGuenter Roeck #define MCP98244_DEVID_MASK	0xfffc
127d4768280SGuenter Roeck 
1284453d736SGuenter Roeck #define MCP9843_DEVID		0x0000	/* Also matches mcp9805 */
1294453d736SGuenter Roeck #define MCP9843_DEVID_MASK	0xfffe
1304453d736SGuenter Roeck 
1314453d736SGuenter Roeck /* NXP */
1324453d736SGuenter Roeck #define SE97_DEVID		0xa200
1334453d736SGuenter Roeck #define SE97_DEVID_MASK		0xfffc
1344453d736SGuenter Roeck 
1354453d736SGuenter Roeck #define SE98_DEVID		0xa100
1364453d736SGuenter Roeck #define SE98_DEVID_MASK		0xfffc
1374453d736SGuenter Roeck 
1384453d736SGuenter Roeck /* ON Semiconductor */
1394453d736SGuenter Roeck #define CAT6095_DEVID		0x0800	/* Also matches CAT34TS02 */
1404453d736SGuenter Roeck #define CAT6095_DEVID_MASK	0xffe0
1414453d736SGuenter Roeck 
142*99b981b2SGuenter Roeck #define CAT34TS02C_DEVID	0x0a00
143*99b981b2SGuenter Roeck #define CAT34TS02C_DEVID_MASK	0xfff0
144*99b981b2SGuenter Roeck 
145568003ceSGuenter Roeck #define CAT34TS04_DEVID		0x2200
146568003ceSGuenter Roeck #define CAT34TS04_DEVID_MASK	0xfff0
147568003ceSGuenter Roeck 
1484453d736SGuenter Roeck /* ST Microelectronics */
1494453d736SGuenter Roeck #define STTS424_DEVID		0x0101
1504453d736SGuenter Roeck #define STTS424_DEVID_MASK	0xffff
1514453d736SGuenter Roeck 
1524453d736SGuenter Roeck #define STTS424E_DEVID		0x0000
1534453d736SGuenter Roeck #define STTS424E_DEVID_MASK	0xfffe
1544453d736SGuenter Roeck 
1554de86126SJean Delvare #define STTS2002_DEVID		0x0300
1564de86126SJean Delvare #define STTS2002_DEVID_MASK	0xffff
1574de86126SJean Delvare 
158175c490cSGuenter Roeck #define STTS2004_DEVID		0x2201
159175c490cSGuenter Roeck #define STTS2004_DEVID_MASK	0xffff
160175c490cSGuenter Roeck 
1614de86126SJean Delvare #define STTS3000_DEVID		0x0200
1624de86126SJean Delvare #define STTS3000_DEVID_MASK	0xffff
1634de86126SJean Delvare 
1644453d736SGuenter Roeck static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 };
1654453d736SGuenter Roeck 
1664453d736SGuenter Roeck struct jc42_chips {
1674453d736SGuenter Roeck 	u16 manid;
1684453d736SGuenter Roeck 	u16 devid;
1694453d736SGuenter Roeck 	u16 devid_mask;
1704453d736SGuenter Roeck };
1714453d736SGuenter Roeck 
1724453d736SGuenter Roeck static struct jc42_chips jc42_chips[] = {
1734453d736SGuenter Roeck 	{ ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK },
1741bd612a2SGuenter Roeck 	{ ATMEL_MANID, AT30TS00_DEVID, AT30TS00_DEVID_MASK },
175175c490cSGuenter Roeck 	{ ATMEL_MANID2, AT30TSE004_DEVID, AT30TSE004_DEVID_MASK },
176568003ceSGuenter Roeck 	{ GT_MANID, GT30TS00_DEVID, GT30TS00_DEVID_MASK },
177568003ceSGuenter Roeck 	{ GT_MANID2, GT34TS02_DEVID, GT34TS02_DEVID_MASK },
1780ea2f1dbSGuenter Roeck 	{ IDT_MANID, TSE2004_DEVID, TSE2004_DEVID_MASK },
1790ea2f1dbSGuenter Roeck 	{ IDT_MANID, TS3000_DEVID, TS3000_DEVID_MASK },
1800ea2f1dbSGuenter Roeck 	{ IDT_MANID, TS3001_DEVID, TS3001_DEVID_MASK },
1814453d736SGuenter Roeck 	{ MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK },
1821bd612a2SGuenter Roeck 	{ MCP_MANID, MCP9804_DEVID, MCP9804_DEVID_MASK },
183a31887dcSAlison Schofield 	{ MCP_MANID, MCP9808_DEVID, MCP9808_DEVID_MASK },
1844453d736SGuenter Roeck 	{ MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK },
1854453d736SGuenter Roeck 	{ MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK },
186d4768280SGuenter Roeck 	{ MCP_MANID, MCP98244_DEVID, MCP98244_DEVID_MASK },
1874453d736SGuenter Roeck 	{ MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK },
1884453d736SGuenter Roeck 	{ NXP_MANID, SE97_DEVID, SE97_DEVID_MASK },
1894453d736SGuenter Roeck 	{ ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK },
190*99b981b2SGuenter Roeck 	{ ONS_MANID, CAT34TS02C_DEVID, CAT34TS02C_DEVID_MASK },
191568003ceSGuenter Roeck 	{ ONS_MANID, CAT34TS04_DEVID, CAT34TS04_DEVID_MASK },
1924453d736SGuenter Roeck 	{ NXP_MANID, SE98_DEVID, SE98_DEVID_MASK },
1934453d736SGuenter Roeck 	{ STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK },
1944453d736SGuenter Roeck 	{ STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK },
1954de86126SJean Delvare 	{ STM_MANID, STTS2002_DEVID, STTS2002_DEVID_MASK },
196175c490cSGuenter Roeck 	{ STM_MANID, STTS2004_DEVID, STTS2004_DEVID_MASK },
1974de86126SJean Delvare 	{ STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK },
1984453d736SGuenter Roeck };
1994453d736SGuenter Roeck 
20010192bc6SGuenter Roeck enum temp_index {
20110192bc6SGuenter Roeck 	t_input = 0,
20210192bc6SGuenter Roeck 	t_crit,
20310192bc6SGuenter Roeck 	t_min,
20410192bc6SGuenter Roeck 	t_max,
20510192bc6SGuenter Roeck 	t_num_temp
20610192bc6SGuenter Roeck };
20710192bc6SGuenter Roeck 
20810192bc6SGuenter Roeck static const u8 temp_regs[t_num_temp] = {
20910192bc6SGuenter Roeck 	[t_input] = JC42_REG_TEMP,
21010192bc6SGuenter Roeck 	[t_crit] = JC42_REG_TEMP_CRITICAL,
21110192bc6SGuenter Roeck 	[t_min] = JC42_REG_TEMP_LOWER,
21210192bc6SGuenter Roeck 	[t_max] = JC42_REG_TEMP_UPPER,
21310192bc6SGuenter Roeck };
21410192bc6SGuenter Roeck 
2154453d736SGuenter Roeck /* Each client has this additional data */
2164453d736SGuenter Roeck struct jc42_data {
21762f9a57cSGuenter Roeck 	struct i2c_client *client;
2184453d736SGuenter Roeck 	struct mutex	update_lock;	/* protect register access */
2194453d736SGuenter Roeck 	bool		extended;	/* true if extended range supported */
2204453d736SGuenter Roeck 	bool		valid;
2214453d736SGuenter Roeck 	unsigned long	last_updated;	/* In jiffies */
2224453d736SGuenter Roeck 	u16		orig_config;	/* original configuration */
2234453d736SGuenter Roeck 	u16		config;		/* current configuration */
22410192bc6SGuenter Roeck 	u16		temp[t_num_temp];/* Temperatures */
2254453d736SGuenter Roeck };
2264453d736SGuenter Roeck 
2274453d736SGuenter Roeck #define JC42_TEMP_MIN_EXTENDED	(-40000)
2284453d736SGuenter Roeck #define JC42_TEMP_MIN		0
2294453d736SGuenter Roeck #define JC42_TEMP_MAX		125000
2304453d736SGuenter Roeck 
2313a05633bSGuenter Roeck static u16 jc42_temp_to_reg(long temp, bool extended)
2324453d736SGuenter Roeck {
2332a844c14SGuenter Roeck 	int ntemp = clamp_val(temp,
2344453d736SGuenter Roeck 			      extended ? JC42_TEMP_MIN_EXTENDED :
2354453d736SGuenter Roeck 			      JC42_TEMP_MIN, JC42_TEMP_MAX);
2364453d736SGuenter Roeck 
2374453d736SGuenter Roeck 	/* convert from 0.001 to 0.0625 resolution */
2384453d736SGuenter Roeck 	return (ntemp * 2 / 125) & 0x1fff;
2394453d736SGuenter Roeck }
2404453d736SGuenter Roeck 
2414453d736SGuenter Roeck static int jc42_temp_from_reg(s16 reg)
2424453d736SGuenter Roeck {
243bca6a1adSGuenter Roeck 	reg = sign_extend32(reg, 12);
2444453d736SGuenter Roeck 
2454453d736SGuenter Roeck 	/* convert from 0.0625 to 0.001 resolution */
2464453d736SGuenter Roeck 	return reg * 125 / 2;
2474453d736SGuenter Roeck }
2484453d736SGuenter Roeck 
249d397276bSGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev)
250d397276bSGuenter Roeck {
251d397276bSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
252d397276bSGuenter Roeck 	struct i2c_client *client = data->client;
253d397276bSGuenter Roeck 	struct jc42_data *ret = data;
25410192bc6SGuenter Roeck 	int i, val;
255d397276bSGuenter Roeck 
256d397276bSGuenter Roeck 	mutex_lock(&data->update_lock);
257d397276bSGuenter Roeck 
258d397276bSGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
25910192bc6SGuenter Roeck 		for (i = 0; i < t_num_temp; i++) {
26010192bc6SGuenter Roeck 			val = i2c_smbus_read_word_swapped(client, temp_regs[i]);
261d397276bSGuenter Roeck 			if (val < 0) {
262d397276bSGuenter Roeck 				ret = ERR_PTR(val);
263d397276bSGuenter Roeck 				goto abort;
264d397276bSGuenter Roeck 			}
26510192bc6SGuenter Roeck 			data->temp[i] = val;
266d397276bSGuenter Roeck 		}
267d397276bSGuenter Roeck 		data->last_updated = jiffies;
268d397276bSGuenter Roeck 		data->valid = true;
269d397276bSGuenter Roeck 	}
270d397276bSGuenter Roeck abort:
271d397276bSGuenter Roeck 	mutex_unlock(&data->update_lock);
272d397276bSGuenter Roeck 	return ret;
273d397276bSGuenter Roeck }
274d397276bSGuenter Roeck 
275fcc448cfSGuenter Roeck static int jc42_read(struct device *dev, enum hwmon_sensor_types type,
276fcc448cfSGuenter Roeck 		     u32 attr, int channel, long *val)
27710192bc6SGuenter Roeck {
2784453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
2794453d736SGuenter Roeck 	int temp, hyst;
2804453d736SGuenter Roeck 
2814453d736SGuenter Roeck 	if (IS_ERR(data))
2824453d736SGuenter Roeck 		return PTR_ERR(data);
2834453d736SGuenter Roeck 
284fcc448cfSGuenter Roeck 	switch (attr) {
285fcc448cfSGuenter Roeck 	case hwmon_temp_input:
286fcc448cfSGuenter Roeck 		*val = jc42_temp_from_reg(data->temp[t_input]);
287fcc448cfSGuenter Roeck 		return 0;
288fcc448cfSGuenter Roeck 	case hwmon_temp_min:
289fcc448cfSGuenter Roeck 		*val = jc42_temp_from_reg(data->temp[t_min]);
290fcc448cfSGuenter Roeck 		return 0;
291fcc448cfSGuenter Roeck 	case hwmon_temp_max:
292fcc448cfSGuenter Roeck 		*val = jc42_temp_from_reg(data->temp[t_max]);
293fcc448cfSGuenter Roeck 		return 0;
294fcc448cfSGuenter Roeck 	case hwmon_temp_crit:
295fcc448cfSGuenter Roeck 		*val = jc42_temp_from_reg(data->temp[t_crit]);
296fcc448cfSGuenter Roeck 		return 0;
297fcc448cfSGuenter Roeck 	case hwmon_temp_max_hyst:
298fcc448cfSGuenter Roeck 		temp = jc42_temp_from_reg(data->temp[t_max]);
2992ccc8731SJean Delvare 		hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
3002ccc8731SJean Delvare 						>> JC42_CFG_HYST_SHIFT];
301fcc448cfSGuenter Roeck 		*val = temp - hyst;
302fcc448cfSGuenter Roeck 		return 0;
303fcc448cfSGuenter Roeck 	case hwmon_temp_crit_hyst:
304fcc448cfSGuenter Roeck 		temp = jc42_temp_from_reg(data->temp[t_crit]);
305fcc448cfSGuenter Roeck 		hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
306fcc448cfSGuenter Roeck 						>> JC42_CFG_HYST_SHIFT];
307fcc448cfSGuenter Roeck 		*val = temp - hyst;
308fcc448cfSGuenter Roeck 		return 0;
309fcc448cfSGuenter Roeck 	case hwmon_temp_min_alarm:
310fcc448cfSGuenter Roeck 		*val = (data->temp[t_input] >> JC42_ALARM_MIN_BIT) & 1;
311fcc448cfSGuenter Roeck 		return 0;
312fcc448cfSGuenter Roeck 	case hwmon_temp_max_alarm:
313fcc448cfSGuenter Roeck 		*val = (data->temp[t_input] >> JC42_ALARM_MAX_BIT) & 1;
314fcc448cfSGuenter Roeck 		return 0;
315fcc448cfSGuenter Roeck 	case hwmon_temp_crit_alarm:
316fcc448cfSGuenter Roeck 		*val = (data->temp[t_input] >> JC42_ALARM_CRIT_BIT) & 1;
317fcc448cfSGuenter Roeck 		return 0;
318fcc448cfSGuenter Roeck 	default:
319fcc448cfSGuenter Roeck 		return -EOPNOTSUPP;
320fcc448cfSGuenter Roeck 	}
3214453d736SGuenter Roeck }
3224453d736SGuenter Roeck 
323fcc448cfSGuenter Roeck static int jc42_write(struct device *dev, enum hwmon_sensor_types type,
324fcc448cfSGuenter Roeck 		      u32 attr, int channel, long val)
3254453d736SGuenter Roeck {
32610192bc6SGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
327fcc448cfSGuenter Roeck 	struct i2c_client *client = data->client;
328fcc448cfSGuenter Roeck 	int diff, hyst;
329fcc448cfSGuenter Roeck 	int ret;
3304453d736SGuenter Roeck 
33110192bc6SGuenter Roeck 	mutex_lock(&data->update_lock);
3324453d736SGuenter Roeck 
333fcc448cfSGuenter Roeck 	switch (attr) {
334fcc448cfSGuenter Roeck 	case hwmon_temp_min:
335fcc448cfSGuenter Roeck 		data->temp[t_min] = jc42_temp_to_reg(val, data->extended);
336fcc448cfSGuenter Roeck 		ret = i2c_smbus_write_word_swapped(client, temp_regs[t_min],
337fcc448cfSGuenter Roeck 						   data->temp[t_min]);
338fcc448cfSGuenter Roeck 		break;
339fcc448cfSGuenter Roeck 	case hwmon_temp_max:
340fcc448cfSGuenter Roeck 		data->temp[t_max] = jc42_temp_to_reg(val, data->extended);
341fcc448cfSGuenter Roeck 		ret = i2c_smbus_write_word_swapped(client, temp_regs[t_max],
342fcc448cfSGuenter Roeck 						   data->temp[t_max]);
343fcc448cfSGuenter Roeck 		break;
344fcc448cfSGuenter Roeck 	case hwmon_temp_crit:
345fcc448cfSGuenter Roeck 		data->temp[t_crit] = jc42_temp_to_reg(val, data->extended);
346fcc448cfSGuenter Roeck 		ret = i2c_smbus_write_word_swapped(client, temp_regs[t_crit],
347fcc448cfSGuenter Roeck 						   data->temp[t_crit]);
348fcc448cfSGuenter Roeck 		break;
349fcc448cfSGuenter Roeck 	case hwmon_temp_crit_hyst:
3505d577dbaSGuenter Roeck 		/*
3515d577dbaSGuenter Roeck 		 * JC42.4 compliant chips only support four hysteresis values.
3525d577dbaSGuenter Roeck 		 * Pick best choice and go from there.
3535d577dbaSGuenter Roeck 		 */
354fcc448cfSGuenter Roeck 		val = clamp_val(val, (data->extended ? JC42_TEMP_MIN_EXTENDED
355fcc448cfSGuenter Roeck 						     : JC42_TEMP_MIN) - 6000,
356fcc448cfSGuenter Roeck 				JC42_TEMP_MAX);
35710192bc6SGuenter Roeck 		diff = jc42_temp_from_reg(data->temp[t_crit]) - val;
3584453d736SGuenter Roeck 		hyst = 0;
3594453d736SGuenter Roeck 		if (diff > 0) {
3604453d736SGuenter Roeck 			if (diff < 2250)
3614453d736SGuenter Roeck 				hyst = 1;	/* 1.5 degrees C */
3624453d736SGuenter Roeck 			else if (diff < 4500)
3634453d736SGuenter Roeck 				hyst = 2;	/* 3.0 degrees C */
3644453d736SGuenter Roeck 			else
3654453d736SGuenter Roeck 				hyst = 3;	/* 6.0 degrees C */
3664453d736SGuenter Roeck 		}
367fcc448cfSGuenter Roeck 		data->config = (data->config & ~JC42_CFG_HYST_MASK) |
368fcc448cfSGuenter Roeck 				(hyst << JC42_CFG_HYST_SHIFT);
369fcc448cfSGuenter Roeck 		ret = i2c_smbus_write_word_swapped(data->client,
370fcc448cfSGuenter Roeck 						   JC42_REG_CONFIG,
37190f4102cSJean Delvare 						   data->config);
372fcc448cfSGuenter Roeck 		break;
373fcc448cfSGuenter Roeck 	default:
374fcc448cfSGuenter Roeck 		ret = -EOPNOTSUPP;
375fcc448cfSGuenter Roeck 		break;
376fcc448cfSGuenter Roeck 	}
377fcc448cfSGuenter Roeck 
3784453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);
379fcc448cfSGuenter Roeck 
3804453d736SGuenter Roeck 	return ret;
3814453d736SGuenter Roeck }
3824453d736SGuenter Roeck 
383fcc448cfSGuenter Roeck static umode_t jc42_is_visible(const void *_data, enum hwmon_sensor_types type,
384fcc448cfSGuenter Roeck 			       u32 attr, int channel)
3854453d736SGuenter Roeck {
386fcc448cfSGuenter Roeck 	const struct jc42_data *data = _data;
3872c6315daSClemens Ladisch 	unsigned int config = data->config;
388fcc448cfSGuenter Roeck 	umode_t mode = S_IRUGO;
3892c6315daSClemens Ladisch 
390fcc448cfSGuenter Roeck 	switch (attr) {
391fcc448cfSGuenter Roeck 	case hwmon_temp_min:
392fcc448cfSGuenter Roeck 	case hwmon_temp_max:
393fcc448cfSGuenter Roeck 		if (!(config & JC42_CFG_EVENT_LOCK))
394fcc448cfSGuenter Roeck 			mode |= S_IWUSR;
395fcc448cfSGuenter Roeck 		break;
396fcc448cfSGuenter Roeck 	case hwmon_temp_crit:
397fcc448cfSGuenter Roeck 		if (!(config & JC42_CFG_TCRIT_LOCK))
398fcc448cfSGuenter Roeck 			mode |= S_IWUSR;
399fcc448cfSGuenter Roeck 		break;
400fcc448cfSGuenter Roeck 	case hwmon_temp_crit_hyst:
401fcc448cfSGuenter Roeck 		if (!(config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK)))
402fcc448cfSGuenter Roeck 			mode |= S_IWUSR;
403fcc448cfSGuenter Roeck 		break;
404fcc448cfSGuenter Roeck 	case hwmon_temp_input:
405fcc448cfSGuenter Roeck 	case hwmon_temp_max_hyst:
406fcc448cfSGuenter Roeck 	case hwmon_temp_min_alarm:
407fcc448cfSGuenter Roeck 	case hwmon_temp_max_alarm:
408fcc448cfSGuenter Roeck 	case hwmon_temp_crit_alarm:
409fcc448cfSGuenter Roeck 		break;
410fcc448cfSGuenter Roeck 	default:
411fcc448cfSGuenter Roeck 		mode = 0;
412fcc448cfSGuenter Roeck 		break;
4132c6315daSClemens Ladisch 	}
414fcc448cfSGuenter Roeck 	return mode;
415fcc448cfSGuenter Roeck }
4164453d736SGuenter Roeck 
4174453d736SGuenter Roeck /* Return 0 if detection is successful, -ENODEV otherwise */
418f15df57dSGuenter Roeck static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info)
4194453d736SGuenter Roeck {
420f15df57dSGuenter Roeck 	struct i2c_adapter *adapter = client->adapter;
4214453d736SGuenter Roeck 	int i, config, cap, manid, devid;
4224453d736SGuenter Roeck 
4234453d736SGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
4244453d736SGuenter Roeck 				     I2C_FUNC_SMBUS_WORD_DATA))
4254453d736SGuenter Roeck 		return -ENODEV;
4264453d736SGuenter Roeck 
427f15df57dSGuenter Roeck 	cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
428f15df57dSGuenter Roeck 	config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
429f15df57dSGuenter Roeck 	manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID);
430f15df57dSGuenter Roeck 	devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID);
4314453d736SGuenter Roeck 
4324453d736SGuenter Roeck 	if (cap < 0 || config < 0 || manid < 0 || devid < 0)
4334453d736SGuenter Roeck 		return -ENODEV;
4344453d736SGuenter Roeck 
4354453d736SGuenter Roeck 	if ((cap & 0xff00) || (config & 0xf800))
4364453d736SGuenter Roeck 		return -ENODEV;
4374453d736SGuenter Roeck 
4384453d736SGuenter Roeck 	for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) {
4394453d736SGuenter Roeck 		struct jc42_chips *chip = &jc42_chips[i];
4404453d736SGuenter Roeck 		if (manid == chip->manid &&
4414453d736SGuenter Roeck 		    (devid & chip->devid_mask) == chip->devid) {
4424453d736SGuenter Roeck 			strlcpy(info->type, "jc42", I2C_NAME_SIZE);
4434453d736SGuenter Roeck 			return 0;
4444453d736SGuenter Roeck 		}
4454453d736SGuenter Roeck 	}
4464453d736SGuenter Roeck 	return -ENODEV;
4474453d736SGuenter Roeck }
4484453d736SGuenter Roeck 
449fcc448cfSGuenter Roeck static const u32 jc42_temp_config[] = {
450fcc448cfSGuenter Roeck 	HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_CRIT |
451fcc448cfSGuenter Roeck 	HWMON_T_MAX_HYST | HWMON_T_CRIT_HYST |
452fcc448cfSGuenter Roeck 	HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM,
453fcc448cfSGuenter Roeck 	0
454fcc448cfSGuenter Roeck };
455fcc448cfSGuenter Roeck 
456fcc448cfSGuenter Roeck static const struct hwmon_channel_info jc42_temp = {
457fcc448cfSGuenter Roeck 	.type = hwmon_temp,
458fcc448cfSGuenter Roeck 	.config = jc42_temp_config,
459fcc448cfSGuenter Roeck };
460fcc448cfSGuenter Roeck 
461fcc448cfSGuenter Roeck static const struct hwmon_channel_info *jc42_info[] = {
462fcc448cfSGuenter Roeck 	&jc42_temp,
463fcc448cfSGuenter Roeck 	NULL
464fcc448cfSGuenter Roeck };
465fcc448cfSGuenter Roeck 
466fcc448cfSGuenter Roeck static const struct hwmon_ops jc42_hwmon_ops = {
467fcc448cfSGuenter Roeck 	.is_visible = jc42_is_visible,
468fcc448cfSGuenter Roeck 	.read = jc42_read,
469fcc448cfSGuenter Roeck 	.write = jc42_write,
470fcc448cfSGuenter Roeck };
471fcc448cfSGuenter Roeck 
472fcc448cfSGuenter Roeck static const struct hwmon_chip_info jc42_chip_info = {
473fcc448cfSGuenter Roeck 	.ops = &jc42_hwmon_ops,
474fcc448cfSGuenter Roeck 	.info = jc42_info,
475fcc448cfSGuenter Roeck };
476fcc448cfSGuenter Roeck 
477f15df57dSGuenter Roeck static int jc42_probe(struct i2c_client *client, const struct i2c_device_id *id)
4784453d736SGuenter Roeck {
479f15df57dSGuenter Roeck 	struct device *dev = &client->dev;
48062f9a57cSGuenter Roeck 	struct device *hwmon_dev;
48162f9a57cSGuenter Roeck 	struct jc42_data *data;
48262f9a57cSGuenter Roeck 	int config, cap;
4834453d736SGuenter Roeck 
484f15df57dSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL);
485f15df57dSGuenter Roeck 	if (!data)
486f15df57dSGuenter Roeck 		return -ENOMEM;
4874453d736SGuenter Roeck 
48862f9a57cSGuenter Roeck 	data->client = client;
489f15df57dSGuenter Roeck 	i2c_set_clientdata(client, data);
4904453d736SGuenter Roeck 	mutex_init(&data->update_lock);
4914453d736SGuenter Roeck 
492f15df57dSGuenter Roeck 	cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
493f15df57dSGuenter Roeck 	if (cap < 0)
494f15df57dSGuenter Roeck 		return cap;
495f15df57dSGuenter Roeck 
4964453d736SGuenter Roeck 	data->extended = !!(cap & JC42_CAP_RANGE);
4974453d736SGuenter Roeck 
498f15df57dSGuenter Roeck 	config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
499f15df57dSGuenter Roeck 	if (config < 0)
500f15df57dSGuenter Roeck 		return config;
501f15df57dSGuenter Roeck 
5024453d736SGuenter Roeck 	data->orig_config = config;
5034453d736SGuenter Roeck 	if (config & JC42_CFG_SHUTDOWN) {
5044453d736SGuenter Roeck 		config &= ~JC42_CFG_SHUTDOWN;
505f15df57dSGuenter Roeck 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
5064453d736SGuenter Roeck 	}
5074453d736SGuenter Roeck 	data->config = config;
5084453d736SGuenter Roeck 
509fcc448cfSGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
510fcc448cfSGuenter Roeck 							 data, &jc42_chip_info,
511fcc448cfSGuenter Roeck 							 NULL);
512650a2c02SFengguang Wu 	return PTR_ERR_OR_ZERO(hwmon_dev);
5134453d736SGuenter Roeck }
5144453d736SGuenter Roeck 
5154453d736SGuenter Roeck static int jc42_remove(struct i2c_client *client)
5164453d736SGuenter Roeck {
5174453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
5185953e276SJean Delvare 
5195953e276SJean Delvare 	/* Restore original configuration except hysteresis */
5205953e276SJean Delvare 	if ((data->config & ~JC42_CFG_HYST_MASK) !=
5215953e276SJean Delvare 	    (data->orig_config & ~JC42_CFG_HYST_MASK)) {
5225953e276SJean Delvare 		int config;
5235953e276SJean Delvare 
5245953e276SJean Delvare 		config = (data->orig_config & ~JC42_CFG_HYST_MASK)
5255953e276SJean Delvare 		  | (data->config & JC42_CFG_HYST_MASK);
5265953e276SJean Delvare 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
5275953e276SJean Delvare 	}
5284453d736SGuenter Roeck 	return 0;
5294453d736SGuenter Roeck }
5304453d736SGuenter Roeck 
531d397276bSGuenter Roeck #ifdef CONFIG_PM
532d397276bSGuenter Roeck 
533d397276bSGuenter Roeck static int jc42_suspend(struct device *dev)
5344453d736SGuenter Roeck {
53562f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
5364453d736SGuenter Roeck 
537d397276bSGuenter Roeck 	data->config |= JC42_CFG_SHUTDOWN;
538d397276bSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
539d397276bSGuenter Roeck 				     data->config);
540d397276bSGuenter Roeck 	return 0;
541d397276bSGuenter Roeck }
5424453d736SGuenter Roeck 
543d397276bSGuenter Roeck static int jc42_resume(struct device *dev)
544d397276bSGuenter Roeck {
545d397276bSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
5464453d736SGuenter Roeck 
547d397276bSGuenter Roeck 	data->config &= ~JC42_CFG_SHUTDOWN;
548d397276bSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
549d397276bSGuenter Roeck 				     data->config);
550d397276bSGuenter Roeck 	return 0;
5514453d736SGuenter Roeck }
5524453d736SGuenter Roeck 
553d397276bSGuenter Roeck static const struct dev_pm_ops jc42_dev_pm_ops = {
554d397276bSGuenter Roeck 	.suspend = jc42_suspend,
555d397276bSGuenter Roeck 	.resume = jc42_resume,
556d397276bSGuenter Roeck };
5574453d736SGuenter Roeck 
558d397276bSGuenter Roeck #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops)
559d397276bSGuenter Roeck #else
560d397276bSGuenter Roeck #define JC42_DEV_PM_OPS NULL
561d397276bSGuenter Roeck #endif /* CONFIG_PM */
5624453d736SGuenter Roeck 
563d397276bSGuenter Roeck static const struct i2c_device_id jc42_id[] = {
564d397276bSGuenter Roeck 	{ "jc42", 0 },
565d397276bSGuenter Roeck 	{ }
566d397276bSGuenter Roeck };
567d397276bSGuenter Roeck MODULE_DEVICE_TABLE(i2c, jc42_id);
568d397276bSGuenter Roeck 
569803decceSGuenter Roeck #ifdef CONFIG_OF
570803decceSGuenter Roeck static const struct of_device_id jc42_of_ids[] = {
571803decceSGuenter Roeck 	{ .compatible = "jedec,jc-42.4-temp", },
572803decceSGuenter Roeck 	{ }
573803decceSGuenter Roeck };
574803decceSGuenter Roeck MODULE_DEVICE_TABLE(of, jc42_of_ids);
575803decceSGuenter Roeck #endif
576803decceSGuenter Roeck 
577d397276bSGuenter Roeck static struct i2c_driver jc42_driver = {
578eacc48ceSAlison Schofield 	.class		= I2C_CLASS_SPD | I2C_CLASS_HWMON,
579d397276bSGuenter Roeck 	.driver = {
580d397276bSGuenter Roeck 		.name	= "jc42",
581d397276bSGuenter Roeck 		.pm = JC42_DEV_PM_OPS,
582803decceSGuenter Roeck 		.of_match_table = of_match_ptr(jc42_of_ids),
583d397276bSGuenter Roeck 	},
584d397276bSGuenter Roeck 	.probe		= jc42_probe,
585d397276bSGuenter Roeck 	.remove		= jc42_remove,
586d397276bSGuenter Roeck 	.id_table	= jc42_id,
587d397276bSGuenter Roeck 	.detect		= jc42_detect,
588d397276bSGuenter Roeck 	.address_list	= normal_i2c,
589d397276bSGuenter Roeck };
5904453d736SGuenter Roeck 
591f0967eeaSAxel Lin module_i2c_driver(jc42_driver);
5924453d736SGuenter Roeck 
593bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
5944453d736SGuenter Roeck MODULE_DESCRIPTION("JC42 driver");
5954453d736SGuenter Roeck MODULE_LICENSE("GPL");
596