174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 24453d736SGuenter Roeck /* 34453d736SGuenter Roeck * jc42.c - driver for Jedec JC42.4 compliant temperature sensors 44453d736SGuenter Roeck * 54453d736SGuenter Roeck * Copyright (c) 2010 Ericsson AB. 64453d736SGuenter Roeck * 74453d736SGuenter Roeck * Derived from lm77.c by Andras BALI <drewie@freemail.hu>. 84453d736SGuenter Roeck * 94453d736SGuenter Roeck * JC42.4 compliant temperature sensors are typically used on memory modules. 104453d736SGuenter Roeck */ 114453d736SGuenter Roeck 1268615eb0SPeter Rosin #include <linux/bitops.h> 134453d736SGuenter Roeck #include <linux/module.h> 144453d736SGuenter Roeck #include <linux/init.h> 154453d736SGuenter Roeck #include <linux/slab.h> 164453d736SGuenter Roeck #include <linux/jiffies.h> 174453d736SGuenter Roeck #include <linux/i2c.h> 184453d736SGuenter Roeck #include <linux/hwmon.h> 194453d736SGuenter Roeck #include <linux/err.h> 204453d736SGuenter Roeck #include <linux/mutex.h> 21803decceSGuenter Roeck #include <linux/of.h> 224453d736SGuenter Roeck 234453d736SGuenter Roeck /* Addresses to scan */ 244453d736SGuenter Roeck static const unsigned short normal_i2c[] = { 254453d736SGuenter Roeck 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, I2C_CLIENT_END }; 264453d736SGuenter Roeck 274453d736SGuenter Roeck /* JC42 registers. All registers are 16 bit. */ 284453d736SGuenter Roeck #define JC42_REG_CAP 0x00 294453d736SGuenter Roeck #define JC42_REG_CONFIG 0x01 304453d736SGuenter Roeck #define JC42_REG_TEMP_UPPER 0x02 314453d736SGuenter Roeck #define JC42_REG_TEMP_LOWER 0x03 324453d736SGuenter Roeck #define JC42_REG_TEMP_CRITICAL 0x04 334453d736SGuenter Roeck #define JC42_REG_TEMP 0x05 344453d736SGuenter Roeck #define JC42_REG_MANID 0x06 354453d736SGuenter Roeck #define JC42_REG_DEVICEID 0x07 3668615eb0SPeter Rosin #define JC42_REG_SMBUS 0x22 /* NXP and Atmel, possibly others? */ 374453d736SGuenter Roeck 384453d736SGuenter Roeck /* Status bits in temperature register */ 394453d736SGuenter Roeck #define JC42_ALARM_CRIT_BIT 15 404453d736SGuenter Roeck #define JC42_ALARM_MAX_BIT 14 414453d736SGuenter Roeck #define JC42_ALARM_MIN_BIT 13 424453d736SGuenter Roeck 434453d736SGuenter Roeck /* Configuration register defines */ 444453d736SGuenter Roeck #define JC42_CFG_CRIT_ONLY (1 << 2) 452c6315daSClemens Ladisch #define JC42_CFG_TCRIT_LOCK (1 << 6) 462c6315daSClemens Ladisch #define JC42_CFG_EVENT_LOCK (1 << 7) 474453d736SGuenter Roeck #define JC42_CFG_SHUTDOWN (1 << 8) 484453d736SGuenter Roeck #define JC42_CFG_HYST_SHIFT 9 492ccc8731SJean Delvare #define JC42_CFG_HYST_MASK (0x03 << 9) 504453d736SGuenter Roeck 514453d736SGuenter Roeck /* Capabilities */ 524453d736SGuenter Roeck #define JC42_CAP_RANGE (1 << 2) 534453d736SGuenter Roeck 544453d736SGuenter Roeck /* Manufacturer IDs */ 554453d736SGuenter Roeck #define ADT_MANID 0x11d4 /* Analog Devices */ 561bd612a2SGuenter Roeck #define ATMEL_MANID 0x001f /* Atmel */ 57175c490cSGuenter Roeck #define ATMEL_MANID2 0x1114 /* Atmel */ 584453d736SGuenter Roeck #define MAX_MANID 0x004d /* Maxim */ 594453d736SGuenter Roeck #define IDT_MANID 0x00b3 /* IDT */ 604453d736SGuenter Roeck #define MCP_MANID 0x0054 /* Microchip */ 614453d736SGuenter Roeck #define NXP_MANID 0x1131 /* NXP Semiconductors */ 624453d736SGuenter Roeck #define ONS_MANID 0x1b09 /* ON Semiconductor */ 634453d736SGuenter Roeck #define STM_MANID 0x104a /* ST Microelectronics */ 64568003ceSGuenter Roeck #define GT_MANID 0x1c68 /* Giantec */ 65568003ceSGuenter Roeck #define GT_MANID2 0x132d /* Giantec, 2nd mfg ID */ 664453d736SGuenter Roeck 6768615eb0SPeter Rosin /* SMBUS register */ 6868615eb0SPeter Rosin #define SMBUS_STMOUT BIT(7) /* SMBus time-out, active low */ 6968615eb0SPeter Rosin 704453d736SGuenter Roeck /* Supported chips */ 714453d736SGuenter Roeck 724453d736SGuenter Roeck /* Analog Devices */ 734453d736SGuenter Roeck #define ADT7408_DEVID 0x0801 744453d736SGuenter Roeck #define ADT7408_DEVID_MASK 0xffff 754453d736SGuenter Roeck 761bd612a2SGuenter Roeck /* Atmel */ 771bd612a2SGuenter Roeck #define AT30TS00_DEVID 0x8201 781bd612a2SGuenter Roeck #define AT30TS00_DEVID_MASK 0xffff 791bd612a2SGuenter Roeck 80175c490cSGuenter Roeck #define AT30TSE004_DEVID 0x2200 81175c490cSGuenter Roeck #define AT30TSE004_DEVID_MASK 0xffff 82175c490cSGuenter Roeck 83568003ceSGuenter Roeck /* Giantec */ 84568003ceSGuenter Roeck #define GT30TS00_DEVID 0x2200 85568003ceSGuenter Roeck #define GT30TS00_DEVID_MASK 0xff00 86568003ceSGuenter Roeck 87568003ceSGuenter Roeck #define GT34TS02_DEVID 0x3300 88568003ceSGuenter Roeck #define GT34TS02_DEVID_MASK 0xff00 89568003ceSGuenter Roeck 904453d736SGuenter Roeck /* IDT */ 910ea2f1dbSGuenter Roeck #define TSE2004_DEVID 0x2200 920ea2f1dbSGuenter Roeck #define TSE2004_DEVID_MASK 0xff00 934453d736SGuenter Roeck 940ea2f1dbSGuenter Roeck #define TS3000_DEVID 0x2900 /* Also matches TSE2002 */ 950ea2f1dbSGuenter Roeck #define TS3000_DEVID_MASK 0xff00 960ea2f1dbSGuenter Roeck 970ea2f1dbSGuenter Roeck #define TS3001_DEVID 0x3000 980ea2f1dbSGuenter Roeck #define TS3001_DEVID_MASK 0xff00 991bd612a2SGuenter Roeck 1004453d736SGuenter Roeck /* Maxim */ 1014453d736SGuenter Roeck #define MAX6604_DEVID 0x3e00 1024453d736SGuenter Roeck #define MAX6604_DEVID_MASK 0xffff 1034453d736SGuenter Roeck 1044453d736SGuenter Roeck /* Microchip */ 1051bd612a2SGuenter Roeck #define MCP9804_DEVID 0x0200 1061bd612a2SGuenter Roeck #define MCP9804_DEVID_MASK 0xfffc 1071bd612a2SGuenter Roeck 108a31887dcSAlison Schofield #define MCP9808_DEVID 0x0400 109a31887dcSAlison Schofield #define MCP9808_DEVID_MASK 0xfffc 110a31887dcSAlison Schofield 1114453d736SGuenter Roeck #define MCP98242_DEVID 0x2000 1124453d736SGuenter Roeck #define MCP98242_DEVID_MASK 0xfffc 1134453d736SGuenter Roeck 1144453d736SGuenter Roeck #define MCP98243_DEVID 0x2100 1154453d736SGuenter Roeck #define MCP98243_DEVID_MASK 0xfffc 1164453d736SGuenter Roeck 117d4768280SGuenter Roeck #define MCP98244_DEVID 0x2200 118d4768280SGuenter Roeck #define MCP98244_DEVID_MASK 0xfffc 119d4768280SGuenter Roeck 1204453d736SGuenter Roeck #define MCP9843_DEVID 0x0000 /* Also matches mcp9805 */ 1214453d736SGuenter Roeck #define MCP9843_DEVID_MASK 0xfffe 1224453d736SGuenter Roeck 1234453d736SGuenter Roeck /* NXP */ 1244453d736SGuenter Roeck #define SE97_DEVID 0xa200 1254453d736SGuenter Roeck #define SE97_DEVID_MASK 0xfffc 1264453d736SGuenter Roeck 1274453d736SGuenter Roeck #define SE98_DEVID 0xa100 1284453d736SGuenter Roeck #define SE98_DEVID_MASK 0xfffc 1294453d736SGuenter Roeck 1304453d736SGuenter Roeck /* ON Semiconductor */ 1314453d736SGuenter Roeck #define CAT6095_DEVID 0x0800 /* Also matches CAT34TS02 */ 1324453d736SGuenter Roeck #define CAT6095_DEVID_MASK 0xffe0 1334453d736SGuenter Roeck 13499b981b2SGuenter Roeck #define CAT34TS02C_DEVID 0x0a00 13599b981b2SGuenter Roeck #define CAT34TS02C_DEVID_MASK 0xfff0 13699b981b2SGuenter Roeck 137568003ceSGuenter Roeck #define CAT34TS04_DEVID 0x2200 138568003ceSGuenter Roeck #define CAT34TS04_DEVID_MASK 0xfff0 139568003ceSGuenter Roeck 1404453d736SGuenter Roeck /* ST Microelectronics */ 1414453d736SGuenter Roeck #define STTS424_DEVID 0x0101 1424453d736SGuenter Roeck #define STTS424_DEVID_MASK 0xffff 1434453d736SGuenter Roeck 1444453d736SGuenter Roeck #define STTS424E_DEVID 0x0000 1454453d736SGuenter Roeck #define STTS424E_DEVID_MASK 0xfffe 1464453d736SGuenter Roeck 1474de86126SJean Delvare #define STTS2002_DEVID 0x0300 1484de86126SJean Delvare #define STTS2002_DEVID_MASK 0xffff 1494de86126SJean Delvare 150175c490cSGuenter Roeck #define STTS2004_DEVID 0x2201 151175c490cSGuenter Roeck #define STTS2004_DEVID_MASK 0xffff 152175c490cSGuenter Roeck 1534de86126SJean Delvare #define STTS3000_DEVID 0x0200 1544de86126SJean Delvare #define STTS3000_DEVID_MASK 0xffff 1554de86126SJean Delvare 1564453d736SGuenter Roeck static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 }; 1574453d736SGuenter Roeck 1584453d736SGuenter Roeck struct jc42_chips { 1594453d736SGuenter Roeck u16 manid; 1604453d736SGuenter Roeck u16 devid; 1614453d736SGuenter Roeck u16 devid_mask; 1624453d736SGuenter Roeck }; 1634453d736SGuenter Roeck 1644453d736SGuenter Roeck static struct jc42_chips jc42_chips[] = { 1654453d736SGuenter Roeck { ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK }, 1661bd612a2SGuenter Roeck { ATMEL_MANID, AT30TS00_DEVID, AT30TS00_DEVID_MASK }, 167175c490cSGuenter Roeck { ATMEL_MANID2, AT30TSE004_DEVID, AT30TSE004_DEVID_MASK }, 168568003ceSGuenter Roeck { GT_MANID, GT30TS00_DEVID, GT30TS00_DEVID_MASK }, 169568003ceSGuenter Roeck { GT_MANID2, GT34TS02_DEVID, GT34TS02_DEVID_MASK }, 1700ea2f1dbSGuenter Roeck { IDT_MANID, TSE2004_DEVID, TSE2004_DEVID_MASK }, 1710ea2f1dbSGuenter Roeck { IDT_MANID, TS3000_DEVID, TS3000_DEVID_MASK }, 1720ea2f1dbSGuenter Roeck { IDT_MANID, TS3001_DEVID, TS3001_DEVID_MASK }, 1734453d736SGuenter Roeck { MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK }, 1741bd612a2SGuenter Roeck { MCP_MANID, MCP9804_DEVID, MCP9804_DEVID_MASK }, 175a31887dcSAlison Schofield { MCP_MANID, MCP9808_DEVID, MCP9808_DEVID_MASK }, 1764453d736SGuenter Roeck { MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK }, 1774453d736SGuenter Roeck { MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK }, 178d4768280SGuenter Roeck { MCP_MANID, MCP98244_DEVID, MCP98244_DEVID_MASK }, 1794453d736SGuenter Roeck { MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK }, 1804453d736SGuenter Roeck { NXP_MANID, SE97_DEVID, SE97_DEVID_MASK }, 1814453d736SGuenter Roeck { ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK }, 18299b981b2SGuenter Roeck { ONS_MANID, CAT34TS02C_DEVID, CAT34TS02C_DEVID_MASK }, 183568003ceSGuenter Roeck { ONS_MANID, CAT34TS04_DEVID, CAT34TS04_DEVID_MASK }, 1844453d736SGuenter Roeck { NXP_MANID, SE98_DEVID, SE98_DEVID_MASK }, 1854453d736SGuenter Roeck { STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK }, 1864453d736SGuenter Roeck { STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK }, 1874de86126SJean Delvare { STM_MANID, STTS2002_DEVID, STTS2002_DEVID_MASK }, 188175c490cSGuenter Roeck { STM_MANID, STTS2004_DEVID, STTS2004_DEVID_MASK }, 1894de86126SJean Delvare { STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK }, 1904453d736SGuenter Roeck }; 1914453d736SGuenter Roeck 19210192bc6SGuenter Roeck enum temp_index { 19310192bc6SGuenter Roeck t_input = 0, 19410192bc6SGuenter Roeck t_crit, 19510192bc6SGuenter Roeck t_min, 19610192bc6SGuenter Roeck t_max, 19710192bc6SGuenter Roeck t_num_temp 19810192bc6SGuenter Roeck }; 19910192bc6SGuenter Roeck 20010192bc6SGuenter Roeck static const u8 temp_regs[t_num_temp] = { 20110192bc6SGuenter Roeck [t_input] = JC42_REG_TEMP, 20210192bc6SGuenter Roeck [t_crit] = JC42_REG_TEMP_CRITICAL, 20310192bc6SGuenter Roeck [t_min] = JC42_REG_TEMP_LOWER, 20410192bc6SGuenter Roeck [t_max] = JC42_REG_TEMP_UPPER, 20510192bc6SGuenter Roeck }; 20610192bc6SGuenter Roeck 2074453d736SGuenter Roeck /* Each client has this additional data */ 2084453d736SGuenter Roeck struct jc42_data { 20962f9a57cSGuenter Roeck struct i2c_client *client; 2104453d736SGuenter Roeck struct mutex update_lock; /* protect register access */ 2114453d736SGuenter Roeck bool extended; /* true if extended range supported */ 2124453d736SGuenter Roeck bool valid; 2134453d736SGuenter Roeck unsigned long last_updated; /* In jiffies */ 2144453d736SGuenter Roeck u16 orig_config; /* original configuration */ 2154453d736SGuenter Roeck u16 config; /* current configuration */ 21610192bc6SGuenter Roeck u16 temp[t_num_temp];/* Temperatures */ 2174453d736SGuenter Roeck }; 2184453d736SGuenter Roeck 2194453d736SGuenter Roeck #define JC42_TEMP_MIN_EXTENDED (-40000) 2204453d736SGuenter Roeck #define JC42_TEMP_MIN 0 2214453d736SGuenter Roeck #define JC42_TEMP_MAX 125000 2224453d736SGuenter Roeck 2233a05633bSGuenter Roeck static u16 jc42_temp_to_reg(long temp, bool extended) 2244453d736SGuenter Roeck { 2252a844c14SGuenter Roeck int ntemp = clamp_val(temp, 2264453d736SGuenter Roeck extended ? JC42_TEMP_MIN_EXTENDED : 2274453d736SGuenter Roeck JC42_TEMP_MIN, JC42_TEMP_MAX); 2284453d736SGuenter Roeck 2294453d736SGuenter Roeck /* convert from 0.001 to 0.0625 resolution */ 2304453d736SGuenter Roeck return (ntemp * 2 / 125) & 0x1fff; 2314453d736SGuenter Roeck } 2324453d736SGuenter Roeck 2334453d736SGuenter Roeck static int jc42_temp_from_reg(s16 reg) 2344453d736SGuenter Roeck { 235bca6a1adSGuenter Roeck reg = sign_extend32(reg, 12); 2364453d736SGuenter Roeck 2374453d736SGuenter Roeck /* convert from 0.0625 to 0.001 resolution */ 2384453d736SGuenter Roeck return reg * 125 / 2; 2394453d736SGuenter Roeck } 2404453d736SGuenter Roeck 241d397276bSGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev) 242d397276bSGuenter Roeck { 243d397276bSGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 244d397276bSGuenter Roeck struct i2c_client *client = data->client; 245d397276bSGuenter Roeck struct jc42_data *ret = data; 24610192bc6SGuenter Roeck int i, val; 247d397276bSGuenter Roeck 248d397276bSGuenter Roeck mutex_lock(&data->update_lock); 249d397276bSGuenter Roeck 250d397276bSGuenter Roeck if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 25110192bc6SGuenter Roeck for (i = 0; i < t_num_temp; i++) { 25210192bc6SGuenter Roeck val = i2c_smbus_read_word_swapped(client, temp_regs[i]); 253d397276bSGuenter Roeck if (val < 0) { 254d397276bSGuenter Roeck ret = ERR_PTR(val); 255d397276bSGuenter Roeck goto abort; 256d397276bSGuenter Roeck } 25710192bc6SGuenter Roeck data->temp[i] = val; 258d397276bSGuenter Roeck } 259d397276bSGuenter Roeck data->last_updated = jiffies; 260d397276bSGuenter Roeck data->valid = true; 261d397276bSGuenter Roeck } 262d397276bSGuenter Roeck abort: 263d397276bSGuenter Roeck mutex_unlock(&data->update_lock); 264d397276bSGuenter Roeck return ret; 265d397276bSGuenter Roeck } 266d397276bSGuenter Roeck 267fcc448cfSGuenter Roeck static int jc42_read(struct device *dev, enum hwmon_sensor_types type, 268fcc448cfSGuenter Roeck u32 attr, int channel, long *val) 26910192bc6SGuenter Roeck { 2704453d736SGuenter Roeck struct jc42_data *data = jc42_update_device(dev); 2714453d736SGuenter Roeck int temp, hyst; 2724453d736SGuenter Roeck 2734453d736SGuenter Roeck if (IS_ERR(data)) 2744453d736SGuenter Roeck return PTR_ERR(data); 2754453d736SGuenter Roeck 276fcc448cfSGuenter Roeck switch (attr) { 277fcc448cfSGuenter Roeck case hwmon_temp_input: 278fcc448cfSGuenter Roeck *val = jc42_temp_from_reg(data->temp[t_input]); 279fcc448cfSGuenter Roeck return 0; 280fcc448cfSGuenter Roeck case hwmon_temp_min: 281fcc448cfSGuenter Roeck *val = jc42_temp_from_reg(data->temp[t_min]); 282fcc448cfSGuenter Roeck return 0; 283fcc448cfSGuenter Roeck case hwmon_temp_max: 284fcc448cfSGuenter Roeck *val = jc42_temp_from_reg(data->temp[t_max]); 285fcc448cfSGuenter Roeck return 0; 286fcc448cfSGuenter Roeck case hwmon_temp_crit: 287fcc448cfSGuenter Roeck *val = jc42_temp_from_reg(data->temp[t_crit]); 288fcc448cfSGuenter Roeck return 0; 289fcc448cfSGuenter Roeck case hwmon_temp_max_hyst: 290fcc448cfSGuenter Roeck temp = jc42_temp_from_reg(data->temp[t_max]); 2912ccc8731SJean Delvare hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK) 2922ccc8731SJean Delvare >> JC42_CFG_HYST_SHIFT]; 293fcc448cfSGuenter Roeck *val = temp - hyst; 294fcc448cfSGuenter Roeck return 0; 295fcc448cfSGuenter Roeck case hwmon_temp_crit_hyst: 296fcc448cfSGuenter Roeck temp = jc42_temp_from_reg(data->temp[t_crit]); 297fcc448cfSGuenter Roeck hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK) 298fcc448cfSGuenter Roeck >> JC42_CFG_HYST_SHIFT]; 299fcc448cfSGuenter Roeck *val = temp - hyst; 300fcc448cfSGuenter Roeck return 0; 301fcc448cfSGuenter Roeck case hwmon_temp_min_alarm: 302fcc448cfSGuenter Roeck *val = (data->temp[t_input] >> JC42_ALARM_MIN_BIT) & 1; 303fcc448cfSGuenter Roeck return 0; 304fcc448cfSGuenter Roeck case hwmon_temp_max_alarm: 305fcc448cfSGuenter Roeck *val = (data->temp[t_input] >> JC42_ALARM_MAX_BIT) & 1; 306fcc448cfSGuenter Roeck return 0; 307fcc448cfSGuenter Roeck case hwmon_temp_crit_alarm: 308fcc448cfSGuenter Roeck *val = (data->temp[t_input] >> JC42_ALARM_CRIT_BIT) & 1; 309fcc448cfSGuenter Roeck return 0; 310fcc448cfSGuenter Roeck default: 311fcc448cfSGuenter Roeck return -EOPNOTSUPP; 312fcc448cfSGuenter Roeck } 3134453d736SGuenter Roeck } 3144453d736SGuenter Roeck 315fcc448cfSGuenter Roeck static int jc42_write(struct device *dev, enum hwmon_sensor_types type, 316fcc448cfSGuenter Roeck u32 attr, int channel, long val) 3174453d736SGuenter Roeck { 31810192bc6SGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 319fcc448cfSGuenter Roeck struct i2c_client *client = data->client; 320fcc448cfSGuenter Roeck int diff, hyst; 321fcc448cfSGuenter Roeck int ret; 3224453d736SGuenter Roeck 32310192bc6SGuenter Roeck mutex_lock(&data->update_lock); 3244453d736SGuenter Roeck 325fcc448cfSGuenter Roeck switch (attr) { 326fcc448cfSGuenter Roeck case hwmon_temp_min: 327fcc448cfSGuenter Roeck data->temp[t_min] = jc42_temp_to_reg(val, data->extended); 328fcc448cfSGuenter Roeck ret = i2c_smbus_write_word_swapped(client, temp_regs[t_min], 329fcc448cfSGuenter Roeck data->temp[t_min]); 330fcc448cfSGuenter Roeck break; 331fcc448cfSGuenter Roeck case hwmon_temp_max: 332fcc448cfSGuenter Roeck data->temp[t_max] = jc42_temp_to_reg(val, data->extended); 333fcc448cfSGuenter Roeck ret = i2c_smbus_write_word_swapped(client, temp_regs[t_max], 334fcc448cfSGuenter Roeck data->temp[t_max]); 335fcc448cfSGuenter Roeck break; 336fcc448cfSGuenter Roeck case hwmon_temp_crit: 337fcc448cfSGuenter Roeck data->temp[t_crit] = jc42_temp_to_reg(val, data->extended); 338fcc448cfSGuenter Roeck ret = i2c_smbus_write_word_swapped(client, temp_regs[t_crit], 339fcc448cfSGuenter Roeck data->temp[t_crit]); 340fcc448cfSGuenter Roeck break; 341fcc448cfSGuenter Roeck case hwmon_temp_crit_hyst: 3425d577dbaSGuenter Roeck /* 3435d577dbaSGuenter Roeck * JC42.4 compliant chips only support four hysteresis values. 3445d577dbaSGuenter Roeck * Pick best choice and go from there. 3455d577dbaSGuenter Roeck */ 346fcc448cfSGuenter Roeck val = clamp_val(val, (data->extended ? JC42_TEMP_MIN_EXTENDED 347fcc448cfSGuenter Roeck : JC42_TEMP_MIN) - 6000, 348fcc448cfSGuenter Roeck JC42_TEMP_MAX); 34910192bc6SGuenter Roeck diff = jc42_temp_from_reg(data->temp[t_crit]) - val; 3504453d736SGuenter Roeck hyst = 0; 3514453d736SGuenter Roeck if (diff > 0) { 3524453d736SGuenter Roeck if (diff < 2250) 3534453d736SGuenter Roeck hyst = 1; /* 1.5 degrees C */ 3544453d736SGuenter Roeck else if (diff < 4500) 3554453d736SGuenter Roeck hyst = 2; /* 3.0 degrees C */ 3564453d736SGuenter Roeck else 3574453d736SGuenter Roeck hyst = 3; /* 6.0 degrees C */ 3584453d736SGuenter Roeck } 359fcc448cfSGuenter Roeck data->config = (data->config & ~JC42_CFG_HYST_MASK) | 360fcc448cfSGuenter Roeck (hyst << JC42_CFG_HYST_SHIFT); 361fcc448cfSGuenter Roeck ret = i2c_smbus_write_word_swapped(data->client, 362fcc448cfSGuenter Roeck JC42_REG_CONFIG, 36390f4102cSJean Delvare data->config); 364fcc448cfSGuenter Roeck break; 365fcc448cfSGuenter Roeck default: 366fcc448cfSGuenter Roeck ret = -EOPNOTSUPP; 367fcc448cfSGuenter Roeck break; 368fcc448cfSGuenter Roeck } 369fcc448cfSGuenter Roeck 3704453d736SGuenter Roeck mutex_unlock(&data->update_lock); 371fcc448cfSGuenter Roeck 3724453d736SGuenter Roeck return ret; 3734453d736SGuenter Roeck } 3744453d736SGuenter Roeck 375fcc448cfSGuenter Roeck static umode_t jc42_is_visible(const void *_data, enum hwmon_sensor_types type, 376fcc448cfSGuenter Roeck u32 attr, int channel) 3774453d736SGuenter Roeck { 378fcc448cfSGuenter Roeck const struct jc42_data *data = _data; 3792c6315daSClemens Ladisch unsigned int config = data->config; 3804820d511SGuenter Roeck umode_t mode = 0444; 3812c6315daSClemens Ladisch 382fcc448cfSGuenter Roeck switch (attr) { 383fcc448cfSGuenter Roeck case hwmon_temp_min: 384fcc448cfSGuenter Roeck case hwmon_temp_max: 385fcc448cfSGuenter Roeck if (!(config & JC42_CFG_EVENT_LOCK)) 3864820d511SGuenter Roeck mode |= 0200; 387fcc448cfSGuenter Roeck break; 388fcc448cfSGuenter Roeck case hwmon_temp_crit: 389fcc448cfSGuenter Roeck if (!(config & JC42_CFG_TCRIT_LOCK)) 3904820d511SGuenter Roeck mode |= 0200; 391fcc448cfSGuenter Roeck break; 392fcc448cfSGuenter Roeck case hwmon_temp_crit_hyst: 393fcc448cfSGuenter Roeck if (!(config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK))) 3944820d511SGuenter Roeck mode |= 0200; 395fcc448cfSGuenter Roeck break; 396fcc448cfSGuenter Roeck case hwmon_temp_input: 397fcc448cfSGuenter Roeck case hwmon_temp_max_hyst: 398fcc448cfSGuenter Roeck case hwmon_temp_min_alarm: 399fcc448cfSGuenter Roeck case hwmon_temp_max_alarm: 400fcc448cfSGuenter Roeck case hwmon_temp_crit_alarm: 401fcc448cfSGuenter Roeck break; 402fcc448cfSGuenter Roeck default: 403fcc448cfSGuenter Roeck mode = 0; 404fcc448cfSGuenter Roeck break; 4052c6315daSClemens Ladisch } 406fcc448cfSGuenter Roeck return mode; 407fcc448cfSGuenter Roeck } 4084453d736SGuenter Roeck 4094453d736SGuenter Roeck /* Return 0 if detection is successful, -ENODEV otherwise */ 410f15df57dSGuenter Roeck static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info) 4114453d736SGuenter Roeck { 412f15df57dSGuenter Roeck struct i2c_adapter *adapter = client->adapter; 4134453d736SGuenter Roeck int i, config, cap, manid, devid; 4144453d736SGuenter Roeck 4154453d736SGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 4164453d736SGuenter Roeck I2C_FUNC_SMBUS_WORD_DATA)) 4174453d736SGuenter Roeck return -ENODEV; 4184453d736SGuenter Roeck 419f15df57dSGuenter Roeck cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP); 420f15df57dSGuenter Roeck config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG); 421f15df57dSGuenter Roeck manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID); 422f15df57dSGuenter Roeck devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID); 4234453d736SGuenter Roeck 4244453d736SGuenter Roeck if (cap < 0 || config < 0 || manid < 0 || devid < 0) 4254453d736SGuenter Roeck return -ENODEV; 4264453d736SGuenter Roeck 4274453d736SGuenter Roeck if ((cap & 0xff00) || (config & 0xf800)) 4284453d736SGuenter Roeck return -ENODEV; 4294453d736SGuenter Roeck 4304453d736SGuenter Roeck for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) { 4314453d736SGuenter Roeck struct jc42_chips *chip = &jc42_chips[i]; 4324453d736SGuenter Roeck if (manid == chip->manid && 4334453d736SGuenter Roeck (devid & chip->devid_mask) == chip->devid) { 4344453d736SGuenter Roeck strlcpy(info->type, "jc42", I2C_NAME_SIZE); 4354453d736SGuenter Roeck return 0; 4364453d736SGuenter Roeck } 4374453d736SGuenter Roeck } 4384453d736SGuenter Roeck return -ENODEV; 4394453d736SGuenter Roeck } 4404453d736SGuenter Roeck 441fcc448cfSGuenter Roeck static const struct hwmon_channel_info *jc42_info[] = { 4421eade10fSGuenter Roeck HWMON_CHANNEL_INFO(temp, 4431eade10fSGuenter Roeck HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | 4441eade10fSGuenter Roeck HWMON_T_CRIT | HWMON_T_MAX_HYST | 4451eade10fSGuenter Roeck HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM | 4461eade10fSGuenter Roeck HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM), 447fcc448cfSGuenter Roeck NULL 448fcc448cfSGuenter Roeck }; 449fcc448cfSGuenter Roeck 450fcc448cfSGuenter Roeck static const struct hwmon_ops jc42_hwmon_ops = { 451fcc448cfSGuenter Roeck .is_visible = jc42_is_visible, 452fcc448cfSGuenter Roeck .read = jc42_read, 453fcc448cfSGuenter Roeck .write = jc42_write, 454fcc448cfSGuenter Roeck }; 455fcc448cfSGuenter Roeck 456fcc448cfSGuenter Roeck static const struct hwmon_chip_info jc42_chip_info = { 457fcc448cfSGuenter Roeck .ops = &jc42_hwmon_ops, 458fcc448cfSGuenter Roeck .info = jc42_info, 459fcc448cfSGuenter Roeck }; 460fcc448cfSGuenter Roeck 461*67487038SStephen Kitt static int jc42_probe(struct i2c_client *client) 4624453d736SGuenter Roeck { 463f15df57dSGuenter Roeck struct device *dev = &client->dev; 46462f9a57cSGuenter Roeck struct device *hwmon_dev; 46562f9a57cSGuenter Roeck struct jc42_data *data; 46662f9a57cSGuenter Roeck int config, cap; 4674453d736SGuenter Roeck 468f15df57dSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL); 469f15df57dSGuenter Roeck if (!data) 470f15df57dSGuenter Roeck return -ENOMEM; 4714453d736SGuenter Roeck 47262f9a57cSGuenter Roeck data->client = client; 473f15df57dSGuenter Roeck i2c_set_clientdata(client, data); 4744453d736SGuenter Roeck mutex_init(&data->update_lock); 4754453d736SGuenter Roeck 476f15df57dSGuenter Roeck cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP); 477f15df57dSGuenter Roeck if (cap < 0) 478f15df57dSGuenter Roeck return cap; 479f15df57dSGuenter Roeck 4804453d736SGuenter Roeck data->extended = !!(cap & JC42_CAP_RANGE); 4814453d736SGuenter Roeck 48268615eb0SPeter Rosin if (device_property_read_bool(dev, "smbus-timeout-disable")) { 48368615eb0SPeter Rosin int smbus; 48468615eb0SPeter Rosin 48568615eb0SPeter Rosin /* 48668615eb0SPeter Rosin * Not all chips support this register, but from a 48768615eb0SPeter Rosin * quick read of various datasheets no chip appears 48868615eb0SPeter Rosin * incompatible with the below attempt to disable 48968615eb0SPeter Rosin * the timeout. And the whole thing is opt-in... 49068615eb0SPeter Rosin */ 49168615eb0SPeter Rosin smbus = i2c_smbus_read_word_swapped(client, JC42_REG_SMBUS); 49268615eb0SPeter Rosin if (smbus < 0) 49368615eb0SPeter Rosin return smbus; 49468615eb0SPeter Rosin i2c_smbus_write_word_swapped(client, JC42_REG_SMBUS, 49568615eb0SPeter Rosin smbus | SMBUS_STMOUT); 49668615eb0SPeter Rosin } 49768615eb0SPeter Rosin 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 509c843b382SSascha Hauer hwmon_dev = devm_hwmon_device_register_with_info(dev, "jc42", 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 }, 584*67487038SStephen Kitt .probe_new = 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