18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* tmp401.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
58c2ecf20Sopenharmony_ci * Preliminary tmp411 support by:
68c2ecf20Sopenharmony_ci * Gabriel Konat, Sander Leget, Wouter Willems
78c2ecf20Sopenharmony_ci * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Cleanup and support for TMP431 and TMP432 by Guenter Roeck
108c2ecf20Sopenharmony_ci * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Note this IC is in some aspect similar to the LM90, but it has quite a
178c2ecf20Sopenharmony_ci * few differences too, for example the local temp has a higher resolution
188c2ecf20Sopenharmony_ci * and thus has 16 bits registers for its value and limit instead of 8 bits.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/init.h>
238c2ecf20Sopenharmony_ci#include <linux/bitops.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
268c2ecf20Sopenharmony_ci#include <linux/i2c.h>
278c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
288c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
298c2ecf20Sopenharmony_ci#include <linux/err.h>
308c2ecf20Sopenharmony_ci#include <linux/mutex.h>
318c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Addresses to scan */
348c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d,
358c2ecf20Sopenharmony_ci	0x4e, 0x4f, I2C_CLIENT_END };
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cienum chips { tmp401, tmp411, tmp431, tmp432, tmp435, tmp461 };
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * The TMP401 registers, note some registers have different addresses for
418c2ecf20Sopenharmony_ci * reading and writing
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#define TMP401_STATUS				0x02
448c2ecf20Sopenharmony_ci#define TMP401_CONFIG_READ			0x03
458c2ecf20Sopenharmony_ci#define TMP401_CONFIG_WRITE			0x09
468c2ecf20Sopenharmony_ci#define TMP401_CONVERSION_RATE_READ		0x04
478c2ecf20Sopenharmony_ci#define TMP401_CONVERSION_RATE_WRITE		0x0A
488c2ecf20Sopenharmony_ci#define TMP401_TEMP_CRIT_HYST			0x21
498c2ecf20Sopenharmony_ci#define TMP401_MANUFACTURER_ID_REG		0xFE
508c2ecf20Sopenharmony_ci#define TMP401_DEVICE_ID_REG			0xFF
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const u8 TMP401_TEMP_MSB_READ[7][2] = {
538c2ecf20Sopenharmony_ci	{ 0x00, 0x01 },	/* temp */
548c2ecf20Sopenharmony_ci	{ 0x06, 0x08 },	/* low limit */
558c2ecf20Sopenharmony_ci	{ 0x05, 0x07 },	/* high limit */
568c2ecf20Sopenharmony_ci	{ 0x20, 0x19 },	/* therm (crit) limit */
578c2ecf20Sopenharmony_ci	{ 0x30, 0x34 },	/* lowest */
588c2ecf20Sopenharmony_ci	{ 0x32, 0x36 },	/* highest */
598c2ecf20Sopenharmony_ci	{ 0, 0x11 },	/* offset */
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic const u8 TMP401_TEMP_MSB_WRITE[7][2] = {
638c2ecf20Sopenharmony_ci	{ 0, 0 },	/* temp (unused) */
648c2ecf20Sopenharmony_ci	{ 0x0C, 0x0E },	/* low limit */
658c2ecf20Sopenharmony_ci	{ 0x0B, 0x0D },	/* high limit */
668c2ecf20Sopenharmony_ci	{ 0x20, 0x19 },	/* therm (crit) limit */
678c2ecf20Sopenharmony_ci	{ 0x30, 0x34 },	/* lowest */
688c2ecf20Sopenharmony_ci	{ 0x32, 0x36 },	/* highest */
698c2ecf20Sopenharmony_ci	{ 0, 0x11 },	/* offset */
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic const u8 TMP432_TEMP_MSB_READ[4][3] = {
738c2ecf20Sopenharmony_ci	{ 0x00, 0x01, 0x23 },	/* temp */
748c2ecf20Sopenharmony_ci	{ 0x06, 0x08, 0x16 },	/* low limit */
758c2ecf20Sopenharmony_ci	{ 0x05, 0x07, 0x15 },	/* high limit */
768c2ecf20Sopenharmony_ci	{ 0x20, 0x19, 0x1A },	/* therm (crit) limit */
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic const u8 TMP432_TEMP_MSB_WRITE[4][3] = {
808c2ecf20Sopenharmony_ci	{ 0, 0, 0 },		/* temp  - unused */
818c2ecf20Sopenharmony_ci	{ 0x0C, 0x0E, 0x16 },	/* low limit */
828c2ecf20Sopenharmony_ci	{ 0x0B, 0x0D, 0x15 },	/* high limit */
838c2ecf20Sopenharmony_ci	{ 0x20, 0x19, 0x1A },	/* therm (crit) limit */
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */
878c2ecf20Sopenharmony_cistatic const u8 TMP432_STATUS_REG[] = {
888c2ecf20Sopenharmony_ci	0x1b, 0x36, 0x35, 0x37 };
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* Flags */
918c2ecf20Sopenharmony_ci#define TMP401_CONFIG_RANGE			BIT(2)
928c2ecf20Sopenharmony_ci#define TMP401_CONFIG_SHUTDOWN			BIT(6)
938c2ecf20Sopenharmony_ci#define TMP401_STATUS_LOCAL_CRIT		BIT(0)
948c2ecf20Sopenharmony_ci#define TMP401_STATUS_REMOTE_CRIT		BIT(1)
958c2ecf20Sopenharmony_ci#define TMP401_STATUS_REMOTE_OPEN		BIT(2)
968c2ecf20Sopenharmony_ci#define TMP401_STATUS_REMOTE_LOW		BIT(3)
978c2ecf20Sopenharmony_ci#define TMP401_STATUS_REMOTE_HIGH		BIT(4)
988c2ecf20Sopenharmony_ci#define TMP401_STATUS_LOCAL_LOW			BIT(5)
998c2ecf20Sopenharmony_ci#define TMP401_STATUS_LOCAL_HIGH		BIT(6)
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* On TMP432, each status has its own register */
1028c2ecf20Sopenharmony_ci#define TMP432_STATUS_LOCAL			BIT(0)
1038c2ecf20Sopenharmony_ci#define TMP432_STATUS_REMOTE1			BIT(1)
1048c2ecf20Sopenharmony_ci#define TMP432_STATUS_REMOTE2			BIT(2)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* Manufacturer / Device ID's */
1078c2ecf20Sopenharmony_ci#define TMP401_MANUFACTURER_ID			0x55
1088c2ecf20Sopenharmony_ci#define TMP401_DEVICE_ID			0x11
1098c2ecf20Sopenharmony_ci#define TMP411A_DEVICE_ID			0x12
1108c2ecf20Sopenharmony_ci#define TMP411B_DEVICE_ID			0x13
1118c2ecf20Sopenharmony_ci#define TMP411C_DEVICE_ID			0x10
1128c2ecf20Sopenharmony_ci#define TMP431_DEVICE_ID			0x31
1138c2ecf20Sopenharmony_ci#define TMP432_DEVICE_ID			0x32
1148c2ecf20Sopenharmony_ci#define TMP435_DEVICE_ID			0x35
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/*
1178c2ecf20Sopenharmony_ci * Driver data (common to all clients)
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const struct i2c_device_id tmp401_id[] = {
1218c2ecf20Sopenharmony_ci	{ "tmp401", tmp401 },
1228c2ecf20Sopenharmony_ci	{ "tmp411", tmp411 },
1238c2ecf20Sopenharmony_ci	{ "tmp431", tmp431 },
1248c2ecf20Sopenharmony_ci	{ "tmp432", tmp432 },
1258c2ecf20Sopenharmony_ci	{ "tmp435", tmp435 },
1268c2ecf20Sopenharmony_ci	{ "tmp461", tmp461 },
1278c2ecf20Sopenharmony_ci	{ }
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tmp401_id);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/*
1328c2ecf20Sopenharmony_ci * Client data (each client gets its own)
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct tmp401_data {
1368c2ecf20Sopenharmony_ci	struct i2c_client *client;
1378c2ecf20Sopenharmony_ci	const struct attribute_group *groups[3];
1388c2ecf20Sopenharmony_ci	struct mutex update_lock;
1398c2ecf20Sopenharmony_ci	char valid; /* zero until following fields are valid */
1408c2ecf20Sopenharmony_ci	unsigned long last_updated; /* in jiffies */
1418c2ecf20Sopenharmony_ci	enum chips kind;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	unsigned int update_interval;	/* in milliseconds */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* register values */
1468c2ecf20Sopenharmony_ci	u8 status[4];
1478c2ecf20Sopenharmony_ci	u8 config;
1488c2ecf20Sopenharmony_ci	u16 temp[7][3];
1498c2ecf20Sopenharmony_ci	u8 temp_crit_hyst;
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * Sysfs attr show / store functions
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int tmp401_register_to_temp(u16 reg, u8 config)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	int temp = reg;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	if (config & TMP401_CONFIG_RANGE)
1618c2ecf20Sopenharmony_ci		temp -= 64 * 256;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return DIV_ROUND_CLOSEST(temp * 125, 32);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic u16 tmp401_temp_to_register(long temp, u8 config, int zbits)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	if (config & TMP401_CONFIG_RANGE) {
1698c2ecf20Sopenharmony_ci		temp = clamp_val(temp, -64000, 191000);
1708c2ecf20Sopenharmony_ci		temp += 64000;
1718c2ecf20Sopenharmony_ci	} else
1728c2ecf20Sopenharmony_ci		temp = clamp_val(temp, 0, 127000);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int tmp401_update_device_reg16(struct i2c_client *client,
1788c2ecf20Sopenharmony_ci				      struct tmp401_data *data)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	int i, j, val;
1818c2ecf20Sopenharmony_ci	int num_regs = data->kind == tmp411 ? 6 : 4;
1828c2ecf20Sopenharmony_ci	int num_sensors = data->kind == tmp432 ? 3 : 2;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	for (i = 0; i < num_sensors; i++) {		/* local / r1 / r2 */
1858c2ecf20Sopenharmony_ci		for (j = 0; j < num_regs; j++) {	/* temp / low / ... */
1868c2ecf20Sopenharmony_ci			u8 regaddr;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci			regaddr = data->kind == tmp432 ?
1898c2ecf20Sopenharmony_ci						TMP432_TEMP_MSB_READ[j][i] :
1908c2ecf20Sopenharmony_ci						TMP401_TEMP_MSB_READ[j][i];
1918c2ecf20Sopenharmony_ci			if (j == 3) { /* crit is msb only */
1928c2ecf20Sopenharmony_ci				val = i2c_smbus_read_byte_data(client, regaddr);
1938c2ecf20Sopenharmony_ci			} else {
1948c2ecf20Sopenharmony_ci				val = i2c_smbus_read_word_swapped(client,
1958c2ecf20Sopenharmony_ci								  regaddr);
1968c2ecf20Sopenharmony_ci			}
1978c2ecf20Sopenharmony_ci			if (val < 0)
1988c2ecf20Sopenharmony_ci				return val;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci			data->temp[j][i] = j == 3 ? val << 8 : val;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci	return 0;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic struct tmp401_data *tmp401_update_device(struct device *dev)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct tmp401_data *data = dev_get_drvdata(dev);
2098c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2108c2ecf20Sopenharmony_ci	struct tmp401_data *ret = data;
2118c2ecf20Sopenharmony_ci	int i, val;
2128c2ecf20Sopenharmony_ci	unsigned long next_update;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	next_update = data->last_updated +
2178c2ecf20Sopenharmony_ci		      msecs_to_jiffies(data->update_interval);
2188c2ecf20Sopenharmony_ci	if (time_after(jiffies, next_update) || !data->valid) {
2198c2ecf20Sopenharmony_ci		if (data->kind != tmp432) {
2208c2ecf20Sopenharmony_ci			/*
2218c2ecf20Sopenharmony_ci			 * The driver uses the TMP432 status format internally.
2228c2ecf20Sopenharmony_ci			 * Convert status to TMP432 format for other chips.
2238c2ecf20Sopenharmony_ci			 */
2248c2ecf20Sopenharmony_ci			val = i2c_smbus_read_byte_data(client, TMP401_STATUS);
2258c2ecf20Sopenharmony_ci			if (val < 0) {
2268c2ecf20Sopenharmony_ci				ret = ERR_PTR(val);
2278c2ecf20Sopenharmony_ci				goto abort;
2288c2ecf20Sopenharmony_ci			}
2298c2ecf20Sopenharmony_ci			data->status[0] =
2308c2ecf20Sopenharmony_ci			  (val & TMP401_STATUS_REMOTE_OPEN) >> 1;
2318c2ecf20Sopenharmony_ci			data->status[1] =
2328c2ecf20Sopenharmony_ci			  ((val & TMP401_STATUS_REMOTE_LOW) >> 2) |
2338c2ecf20Sopenharmony_ci			  ((val & TMP401_STATUS_LOCAL_LOW) >> 5);
2348c2ecf20Sopenharmony_ci			data->status[2] =
2358c2ecf20Sopenharmony_ci			  ((val & TMP401_STATUS_REMOTE_HIGH) >> 3) |
2368c2ecf20Sopenharmony_ci			  ((val & TMP401_STATUS_LOCAL_HIGH) >> 6);
2378c2ecf20Sopenharmony_ci			data->status[3] = val & (TMP401_STATUS_LOCAL_CRIT
2388c2ecf20Sopenharmony_ci						| TMP401_STATUS_REMOTE_CRIT);
2398c2ecf20Sopenharmony_ci		} else {
2408c2ecf20Sopenharmony_ci			for (i = 0; i < ARRAY_SIZE(data->status); i++) {
2418c2ecf20Sopenharmony_ci				val = i2c_smbus_read_byte_data(client,
2428c2ecf20Sopenharmony_ci							TMP432_STATUS_REG[i]);
2438c2ecf20Sopenharmony_ci				if (val < 0) {
2448c2ecf20Sopenharmony_ci					ret = ERR_PTR(val);
2458c2ecf20Sopenharmony_ci					goto abort;
2468c2ecf20Sopenharmony_ci				}
2478c2ecf20Sopenharmony_ci				data->status[i] = val;
2488c2ecf20Sopenharmony_ci			}
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci		val = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
2528c2ecf20Sopenharmony_ci		if (val < 0) {
2538c2ecf20Sopenharmony_ci			ret = ERR_PTR(val);
2548c2ecf20Sopenharmony_ci			goto abort;
2558c2ecf20Sopenharmony_ci		}
2568c2ecf20Sopenharmony_ci		data->config = val;
2578c2ecf20Sopenharmony_ci		val = tmp401_update_device_reg16(client, data);
2588c2ecf20Sopenharmony_ci		if (val < 0) {
2598c2ecf20Sopenharmony_ci			ret = ERR_PTR(val);
2608c2ecf20Sopenharmony_ci			goto abort;
2618c2ecf20Sopenharmony_ci		}
2628c2ecf20Sopenharmony_ci		val = i2c_smbus_read_byte_data(client, TMP401_TEMP_CRIT_HYST);
2638c2ecf20Sopenharmony_ci		if (val < 0) {
2648c2ecf20Sopenharmony_ci			ret = ERR_PTR(val);
2658c2ecf20Sopenharmony_ci			goto abort;
2668c2ecf20Sopenharmony_ci		}
2678c2ecf20Sopenharmony_ci		data->temp_crit_hyst = val;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
2708c2ecf20Sopenharmony_ci		data->valid = 1;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ciabort:
2748c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2758c2ecf20Sopenharmony_ci	return ret;
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
2798c2ecf20Sopenharmony_ci			 char *buf)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr_2(devattr)->nr;
2828c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr_2(devattr)->index;
2838c2ecf20Sopenharmony_ci	struct tmp401_data *data = tmp401_update_device(dev);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2868c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
2898c2ecf20Sopenharmony_ci		tmp401_register_to_temp(data->temp[nr][index], data->config));
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic ssize_t temp_crit_hyst_show(struct device *dev,
2938c2ecf20Sopenharmony_ci				   struct device_attribute *devattr,
2948c2ecf20Sopenharmony_ci				   char *buf)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	int temp, index = to_sensor_dev_attr(devattr)->index;
2978c2ecf20Sopenharmony_ci	struct tmp401_data *data = tmp401_update_device(dev);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3008c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3038c2ecf20Sopenharmony_ci	temp = tmp401_register_to_temp(data->temp[3][index], data->config);
3048c2ecf20Sopenharmony_ci	temp -= data->temp_crit_hyst * 1000;
3058c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", temp);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic ssize_t status_show(struct device *dev,
3118c2ecf20Sopenharmony_ci			   struct device_attribute *devattr, char *buf)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr_2(devattr)->nr;
3148c2ecf20Sopenharmony_ci	int mask = to_sensor_dev_attr_2(devattr)->index;
3158c2ecf20Sopenharmony_ci	struct tmp401_data *data = tmp401_update_device(dev);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3188c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(data->status[nr] & mask));
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic ssize_t temp_store(struct device *dev,
3248c2ecf20Sopenharmony_ci			  struct device_attribute *devattr, const char *buf,
3258c2ecf20Sopenharmony_ci			  size_t count)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr_2(devattr)->nr;
3288c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr_2(devattr)->index;
3298c2ecf20Sopenharmony_ci	struct tmp401_data *data = dev_get_drvdata(dev);
3308c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3318c2ecf20Sopenharmony_ci	long val;
3328c2ecf20Sopenharmony_ci	u16 reg;
3338c2ecf20Sopenharmony_ci	u8 regaddr;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	if (kstrtol(buf, 10, &val))
3368c2ecf20Sopenharmony_ci		return -EINVAL;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index]
3438c2ecf20Sopenharmony_ci				       : TMP401_TEMP_MSB_WRITE[nr][index];
3448c2ecf20Sopenharmony_ci	if (nr == 3) { /* crit is msb only */
3458c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, regaddr, reg >> 8);
3468c2ecf20Sopenharmony_ci	} else {
3478c2ecf20Sopenharmony_ci		/* Hardware expects big endian data --> use _swapped */
3488c2ecf20Sopenharmony_ci		i2c_smbus_write_word_swapped(client, regaddr, reg);
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci	data->temp[nr][index] = reg;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return count;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic ssize_t temp_crit_hyst_store(struct device *dev,
3588c2ecf20Sopenharmony_ci				    struct device_attribute *devattr,
3598c2ecf20Sopenharmony_ci				    const char *buf, size_t count)
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	int temp, index = to_sensor_dev_attr(devattr)->index;
3628c2ecf20Sopenharmony_ci	struct tmp401_data *data = tmp401_update_device(dev);
3638c2ecf20Sopenharmony_ci	long val;
3648c2ecf20Sopenharmony_ci	u8 reg;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3678c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (kstrtol(buf, 10, &val))
3708c2ecf20Sopenharmony_ci		return -EINVAL;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	if (data->config & TMP401_CONFIG_RANGE)
3738c2ecf20Sopenharmony_ci		val = clamp_val(val, -64000, 191000);
3748c2ecf20Sopenharmony_ci	else
3758c2ecf20Sopenharmony_ci		val = clamp_val(val, 0, 127000);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3788c2ecf20Sopenharmony_ci	temp = tmp401_register_to_temp(data->temp[3][index], data->config);
3798c2ecf20Sopenharmony_ci	val = clamp_val(val, temp - 255000, temp);
3808c2ecf20Sopenharmony_ci	reg = ((temp - val) + 500) / 1000;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(data->client, TMP401_TEMP_CRIT_HYST,
3838c2ecf20Sopenharmony_ci				  reg);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	data->temp_crit_hyst = reg;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	return count;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci/*
3938c2ecf20Sopenharmony_ci * Resets the historical measurements of minimum and maximum temperatures.
3948c2ecf20Sopenharmony_ci * This is done by writing any value to any of the minimum/maximum registers
3958c2ecf20Sopenharmony_ci * (0x30-0x37).
3968c2ecf20Sopenharmony_ci */
3978c2ecf20Sopenharmony_cistatic ssize_t reset_temp_history_store(struct device *dev,
3988c2ecf20Sopenharmony_ci					struct device_attribute *devattr,
3998c2ecf20Sopenharmony_ci					const char *buf, size_t count)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	struct tmp401_data *data = dev_get_drvdata(dev);
4028c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
4038c2ecf20Sopenharmony_ci	long val;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if (kstrtol(buf, 10, &val))
4068c2ecf20Sopenharmony_ci		return -EINVAL;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	if (val != 1) {
4098c2ecf20Sopenharmony_ci		dev_err(dev,
4108c2ecf20Sopenharmony_ci			"temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
4118c2ecf20Sopenharmony_ci			val);
4128c2ecf20Sopenharmony_ci		return -EINVAL;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4158c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, TMP401_TEMP_MSB_WRITE[5][0], val);
4168c2ecf20Sopenharmony_ci	data->valid = 0;
4178c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	return count;
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic ssize_t update_interval_show(struct device *dev,
4238c2ecf20Sopenharmony_ci				    struct device_attribute *attr, char *buf)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct tmp401_data *data = dev_get_drvdata(dev);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->update_interval);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic ssize_t update_interval_store(struct device *dev,
4318c2ecf20Sopenharmony_ci				     struct device_attribute *attr,
4328c2ecf20Sopenharmony_ci				     const char *buf, size_t count)
4338c2ecf20Sopenharmony_ci{
4348c2ecf20Sopenharmony_ci	struct tmp401_data *data = dev_get_drvdata(dev);
4358c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
4368c2ecf20Sopenharmony_ci	unsigned long val;
4378c2ecf20Sopenharmony_ci	int err, rate;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
4408c2ecf20Sopenharmony_ci	if (err)
4418c2ecf20Sopenharmony_ci		return err;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/*
4448c2ecf20Sopenharmony_ci	 * For valid rates, interval can be calculated as
4458c2ecf20Sopenharmony_ci	 *	interval = (1 << (7 - rate)) * 125;
4468c2ecf20Sopenharmony_ci	 * Rounded rate is therefore
4478c2ecf20Sopenharmony_ci	 *	rate = 7 - __fls(interval * 4 / (125 * 3));
4488c2ecf20Sopenharmony_ci	 * Use clamp_val() to avoid overflows, and to ensure valid input
4498c2ecf20Sopenharmony_ci	 * for __fls.
4508c2ecf20Sopenharmony_ci	 */
4518c2ecf20Sopenharmony_ci	val = clamp_val(val, 125, 16000);
4528c2ecf20Sopenharmony_ci	rate = 7 - __fls(val * 4 / (125 * 3));
4538c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4548c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate);
4558c2ecf20Sopenharmony_ci	data->update_interval = (1 << (7 - rate)) * 125;
4568c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return count;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
4628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 1, 0);
4638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 2, 0);
4648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 3, 0);
4658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_crit_hyst, 0);
4668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, status, 1,
4678c2ecf20Sopenharmony_ci			       TMP432_STATUS_LOCAL);
4688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, status, 2,
4698c2ecf20Sopenharmony_ci			       TMP432_STATUS_LOCAL);
4708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, status, 3,
4718c2ecf20Sopenharmony_ci			       TMP432_STATUS_LOCAL);
4728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
4738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, 1);
4748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 2, 1);
4758c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 3, 1);
4768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, temp_crit_hyst, 1);
4778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_fault, status, 0, TMP432_STATUS_REMOTE1);
4788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, status, 1,
4798c2ecf20Sopenharmony_ci			       TMP432_STATUS_REMOTE1);
4808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, status, 2,
4818c2ecf20Sopenharmony_ci			       TMP432_STATUS_REMOTE1);
4828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, status, 3,
4838c2ecf20Sopenharmony_ci			       TMP432_STATUS_REMOTE1);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(update_interval);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic struct attribute *tmp401_attributes[] = {
4888c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
4898c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
4908c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
4918c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit.dev_attr.attr,
4928c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
4938c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
4948c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
4958c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
4988c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
4998c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
5008c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit.dev_attr.attr,
5018c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
5028c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
5038c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
5048c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
5058c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	&dev_attr_update_interval.attr,
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	NULL
5108c2ecf20Sopenharmony_ci};
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic const struct attribute_group tmp401_group = {
5138c2ecf20Sopenharmony_ci	.attrs = tmp401_attributes,
5148c2ecf20Sopenharmony_ci};
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci/*
5178c2ecf20Sopenharmony_ci * Additional features of the TMP411 chip.
5188c2ecf20Sopenharmony_ci * The TMP411 stores the minimum and maximum
5198c2ecf20Sopenharmony_ci * temperature measured since power-on, chip-reset, or
5208c2ecf20Sopenharmony_ci * minimum and maximum register reset for both the local
5218c2ecf20Sopenharmony_ci * and remote channels.
5228c2ecf20Sopenharmony_ci */
5238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_lowest, temp, 4, 0);
5248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_highest, temp, 5, 0);
5258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_lowest, temp, 4, 1);
5268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp2_highest, temp, 5, 1);
5278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_WO(temp_reset_history, reset_temp_history, 0);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_cistatic struct attribute *tmp411_attributes[] = {
5308c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_highest.dev_attr.attr,
5318c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_lowest.dev_attr.attr,
5328c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_highest.dev_attr.attr,
5338c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_lowest.dev_attr.attr,
5348c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp_reset_history.dev_attr.attr,
5358c2ecf20Sopenharmony_ci	NULL
5368c2ecf20Sopenharmony_ci};
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic const struct attribute_group tmp411_group = {
5398c2ecf20Sopenharmony_ci	.attrs = tmp411_attributes,
5408c2ecf20Sopenharmony_ci};
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0, 2);
5438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 1, 2);
5448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, 2);
5458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 3, 2);
5468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, temp_crit_hyst, 2);
5478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_fault, status, 0, TMP432_STATUS_REMOTE2);
5488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, status, 1,
5498c2ecf20Sopenharmony_ci			       TMP432_STATUS_REMOTE2);
5508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, status, 2,
5518c2ecf20Sopenharmony_ci			       TMP432_STATUS_REMOTE2);
5528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, status, 3,
5538c2ecf20Sopenharmony_ci			       TMP432_STATUS_REMOTE2);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic struct attribute *tmp432_attributes[] = {
5568c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
5578c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min.dev_attr.attr,
5588c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
5598c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit.dev_attr.attr,
5608c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
5618c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_fault.dev_attr.attr,
5628c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
5638c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
5648c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	NULL
5678c2ecf20Sopenharmony_ci};
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic const struct attribute_group tmp432_group = {
5708c2ecf20Sopenharmony_ci	.attrs = tmp432_attributes,
5718c2ecf20Sopenharmony_ci};
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci/*
5748c2ecf20Sopenharmony_ci * Additional features of the TMP461 chip.
5758c2ecf20Sopenharmony_ci * The TMP461 temperature offset for the remote channel.
5768c2ecf20Sopenharmony_ci */
5778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, 6, 1);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic struct attribute *tmp461_attributes[] = {
5808c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_offset.dev_attr.attr,
5818c2ecf20Sopenharmony_ci	NULL
5828c2ecf20Sopenharmony_ci};
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic const struct attribute_group tmp461_group = {
5858c2ecf20Sopenharmony_ci	.attrs = tmp461_attributes,
5868c2ecf20Sopenharmony_ci};
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci/*
5898c2ecf20Sopenharmony_ci * Begin non sysfs callback code (aka Real code)
5908c2ecf20Sopenharmony_ci */
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic int tmp401_init_client(struct tmp401_data *data,
5938c2ecf20Sopenharmony_ci			      struct i2c_client *client)
5948c2ecf20Sopenharmony_ci{
5958c2ecf20Sopenharmony_ci	int config, config_orig, status = 0;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/* Set the conversion rate to 2 Hz */
5988c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
5998c2ecf20Sopenharmony_ci	data->update_interval = 500;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	/* Start conversions (disable shutdown if necessary) */
6028c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
6038c2ecf20Sopenharmony_ci	if (config < 0)
6048c2ecf20Sopenharmony_ci		return config;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	config_orig = config;
6078c2ecf20Sopenharmony_ci	config &= ~TMP401_CONFIG_SHUTDOWN;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	if (config != config_orig)
6108c2ecf20Sopenharmony_ci		status = i2c_smbus_write_byte_data(client,
6118c2ecf20Sopenharmony_ci						   TMP401_CONFIG_WRITE,
6128c2ecf20Sopenharmony_ci						   config);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	return status;
6158c2ecf20Sopenharmony_ci}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_cistatic int tmp401_detect(struct i2c_client *client,
6188c2ecf20Sopenharmony_ci			 struct i2c_board_info *info)
6198c2ecf20Sopenharmony_ci{
6208c2ecf20Sopenharmony_ci	enum chips kind;
6218c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
6228c2ecf20Sopenharmony_ci	u8 reg;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
6258c2ecf20Sopenharmony_ci		return -ENODEV;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	/* Detect and identify the chip */
6288c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
6298c2ecf20Sopenharmony_ci	if (reg != TMP401_MANUFACTURER_ID)
6308c2ecf20Sopenharmony_ci		return -ENODEV;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	switch (reg) {
6358c2ecf20Sopenharmony_ci	case TMP401_DEVICE_ID:
6368c2ecf20Sopenharmony_ci		if (client->addr != 0x4c)
6378c2ecf20Sopenharmony_ci			return -ENODEV;
6388c2ecf20Sopenharmony_ci		kind = tmp401;
6398c2ecf20Sopenharmony_ci		break;
6408c2ecf20Sopenharmony_ci	case TMP411A_DEVICE_ID:
6418c2ecf20Sopenharmony_ci		if (client->addr != 0x4c)
6428c2ecf20Sopenharmony_ci			return -ENODEV;
6438c2ecf20Sopenharmony_ci		kind = tmp411;
6448c2ecf20Sopenharmony_ci		break;
6458c2ecf20Sopenharmony_ci	case TMP411B_DEVICE_ID:
6468c2ecf20Sopenharmony_ci		if (client->addr != 0x4d)
6478c2ecf20Sopenharmony_ci			return -ENODEV;
6488c2ecf20Sopenharmony_ci		kind = tmp411;
6498c2ecf20Sopenharmony_ci		break;
6508c2ecf20Sopenharmony_ci	case TMP411C_DEVICE_ID:
6518c2ecf20Sopenharmony_ci		if (client->addr != 0x4e)
6528c2ecf20Sopenharmony_ci			return -ENODEV;
6538c2ecf20Sopenharmony_ci		kind = tmp411;
6548c2ecf20Sopenharmony_ci		break;
6558c2ecf20Sopenharmony_ci	case TMP431_DEVICE_ID:
6568c2ecf20Sopenharmony_ci		if (client->addr != 0x4c && client->addr != 0x4d)
6578c2ecf20Sopenharmony_ci			return -ENODEV;
6588c2ecf20Sopenharmony_ci		kind = tmp431;
6598c2ecf20Sopenharmony_ci		break;
6608c2ecf20Sopenharmony_ci	case TMP432_DEVICE_ID:
6618c2ecf20Sopenharmony_ci		if (client->addr != 0x4c && client->addr != 0x4d)
6628c2ecf20Sopenharmony_ci			return -ENODEV;
6638c2ecf20Sopenharmony_ci		kind = tmp432;
6648c2ecf20Sopenharmony_ci		break;
6658c2ecf20Sopenharmony_ci	case TMP435_DEVICE_ID:
6668c2ecf20Sopenharmony_ci		kind = tmp435;
6678c2ecf20Sopenharmony_ci		break;
6688c2ecf20Sopenharmony_ci	default:
6698c2ecf20Sopenharmony_ci		return -ENODEV;
6708c2ecf20Sopenharmony_ci	}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
6738c2ecf20Sopenharmony_ci	if (reg & 0x1b)
6748c2ecf20Sopenharmony_ci		return -ENODEV;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
6778c2ecf20Sopenharmony_ci	/* Datasheet says: 0x1-0x6 */
6788c2ecf20Sopenharmony_ci	if (reg > 15)
6798c2ecf20Sopenharmony_ci		return -ENODEV;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	return 0;
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_cistatic int tmp401_probe(struct i2c_client *client)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	static const char * const names[] = {
6898c2ecf20Sopenharmony_ci		"TMP401", "TMP411", "TMP431", "TMP432", "TMP435", "TMP461"
6908c2ecf20Sopenharmony_ci	};
6918c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
6928c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
6938c2ecf20Sopenharmony_ci	struct tmp401_data *data;
6948c2ecf20Sopenharmony_ci	int groups = 0, status;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
6978c2ecf20Sopenharmony_ci	if (!data)
6988c2ecf20Sopenharmony_ci		return -ENOMEM;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	data->client = client;
7018c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
7028c2ecf20Sopenharmony_ci	data->kind = i2c_match_id(tmp401_id, client)->driver_data;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	/* Initialize the TMP401 chip */
7058c2ecf20Sopenharmony_ci	status = tmp401_init_client(data, client);
7068c2ecf20Sopenharmony_ci	if (status < 0)
7078c2ecf20Sopenharmony_ci		return status;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	/* Register sysfs hooks */
7108c2ecf20Sopenharmony_ci	data->groups[groups++] = &tmp401_group;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	/* Register additional tmp411 sysfs hooks */
7138c2ecf20Sopenharmony_ci	if (data->kind == tmp411)
7148c2ecf20Sopenharmony_ci		data->groups[groups++] = &tmp411_group;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	/* Register additional tmp432 sysfs hooks */
7178c2ecf20Sopenharmony_ci	if (data->kind == tmp432)
7188c2ecf20Sopenharmony_ci		data->groups[groups++] = &tmp432_group;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	if (data->kind == tmp461)
7218c2ecf20Sopenharmony_ci		data->groups[groups++] = &tmp461_group;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
7248c2ecf20Sopenharmony_ci							   data, data->groups);
7258c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon_dev))
7268c2ecf20Sopenharmony_ci		return PTR_ERR(hwmon_dev);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	return 0;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused tmp4xx_of_match[] = {
7348c2ecf20Sopenharmony_ci	{ .compatible = "ti,tmp401", },
7358c2ecf20Sopenharmony_ci	{ .compatible = "ti,tmp411", },
7368c2ecf20Sopenharmony_ci	{ .compatible = "ti,tmp431", },
7378c2ecf20Sopenharmony_ci	{ .compatible = "ti,tmp432", },
7388c2ecf20Sopenharmony_ci	{ .compatible = "ti,tmp435", },
7398c2ecf20Sopenharmony_ci	{ },
7408c2ecf20Sopenharmony_ci};
7418c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tmp4xx_of_match);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_cistatic struct i2c_driver tmp401_driver = {
7448c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
7458c2ecf20Sopenharmony_ci	.driver = {
7468c2ecf20Sopenharmony_ci		.name	= "tmp401",
7478c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(tmp4xx_of_match),
7488c2ecf20Sopenharmony_ci	},
7498c2ecf20Sopenharmony_ci	.probe_new	= tmp401_probe,
7508c2ecf20Sopenharmony_ci	.id_table	= tmp401_id,
7518c2ecf20Sopenharmony_ci	.detect		= tmp401_detect,
7528c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
7538c2ecf20Sopenharmony_ci};
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_cimodule_i2c_driver(tmp401_driver);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
7588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
7598c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
760