18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
48c2ecf20Sopenharmony_ci *	      monitoring
58c2ecf20Sopenharmony_ci * Christian W. Zuckschwerdt  <zany@triq.net>  2000-11-23
68c2ecf20Sopenharmony_ci * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
78c2ecf20Sopenharmony_ci * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
88c2ecf20Sopenharmony_ci * the help of Jean Delvare <jdelvare@suse.de>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * The DS1621 device is a digital temperature/thermometer with 9-bit
118c2ecf20Sopenharmony_ci * resolution, a thermal alarm output (Tout), and user-defined minimum
128c2ecf20Sopenharmony_ci * and maximum temperature thresholds (TH and TL).
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The DS1625, DS1631, DS1721, and DS1731 are pin compatible with the DS1621
158c2ecf20Sopenharmony_ci * and similar in operation, with slight variations as noted in the device
168c2ecf20Sopenharmony_ci * datasheets (please refer to www.maximintegrated.com for specific
178c2ecf20Sopenharmony_ci * device information).
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Since the DS1621 was the first chipset supported by this driver,
208c2ecf20Sopenharmony_ci * most comments will refer to this chipset, but are actually general
218c2ecf20Sopenharmony_ci * and concern all supported chipsets, unless mentioned otherwise.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci#include <linux/init.h>
268c2ecf20Sopenharmony_ci#include <linux/slab.h>
278c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
288c2ecf20Sopenharmony_ci#include <linux/i2c.h>
298c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
308c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
318c2ecf20Sopenharmony_ci#include <linux/err.h>
328c2ecf20Sopenharmony_ci#include <linux/mutex.h>
338c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
348c2ecf20Sopenharmony_ci#include <linux/kernel.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Supported devices */
378c2ecf20Sopenharmony_cienum chips { ds1621, ds1625, ds1631, ds1721, ds1731 };
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* Insmod parameters */
408c2ecf20Sopenharmony_cistatic int polarity = -1;
418c2ecf20Sopenharmony_cimodule_param(polarity, int, 0);
428c2ecf20Sopenharmony_ciMODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/*
458c2ecf20Sopenharmony_ci * The Configuration/Status register
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * - DS1621:
488c2ecf20Sopenharmony_ci *   7    6    5    4    3    2    1    0
498c2ecf20Sopenharmony_ci * |Done|THF |TLF |NVB | X  | X  |POL |1SHOT|
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * - DS1625:
528c2ecf20Sopenharmony_ci *   7    6    5    4    3    2    1    0
538c2ecf20Sopenharmony_ci * |Done|THF |TLF |NVB | 1  | 0  |POL |1SHOT|
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * - DS1631, DS1731:
568c2ecf20Sopenharmony_ci *   7    6    5    4    3    2    1    0
578c2ecf20Sopenharmony_ci * |Done|THF |TLF |NVB | R1 | R0 |POL |1SHOT|
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * - DS1721:
608c2ecf20Sopenharmony_ci *   7    6    5    4    3    2    1    0
618c2ecf20Sopenharmony_ci * |Done| X  | X  | U  | R1 | R0 |POL |1SHOT|
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * Where:
648c2ecf20Sopenharmony_ci * - 'X' is Reserved
658c2ecf20Sopenharmony_ci * - 'U' is Undefined
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci#define DS1621_REG_CONFIG_NVB		0x10
688c2ecf20Sopenharmony_ci#define DS1621_REG_CONFIG_RESOL		0x0C
698c2ecf20Sopenharmony_ci#define DS1621_REG_CONFIG_POLARITY	0x02
708c2ecf20Sopenharmony_ci#define DS1621_REG_CONFIG_1SHOT		0x01
718c2ecf20Sopenharmony_ci#define DS1621_REG_CONFIG_DONE		0x80
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define DS1621_REG_CONFIG_RESOL_SHIFT	2
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* ds1721 conversion rates: {C/LSB, time(ms), resolution bit setting} */
768c2ecf20Sopenharmony_cistatic const unsigned short ds1721_convrates[] = {
778c2ecf20Sopenharmony_ci	94,	/*  9-bits (0.5,  93.75, RES[0..1] = 0 */
788c2ecf20Sopenharmony_ci	188,	/* 10-bits (0.25, 187.5, RES[0..1] = 1 */
798c2ecf20Sopenharmony_ci	375,	/* 11-bits (0.125,  375, RES[0..1] = 2 */
808c2ecf20Sopenharmony_ci	750,	/* 12-bits (0.0625, 750, RES[0..1] = 3 */
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define DS1621_CONVERSION_MAX	750
848c2ecf20Sopenharmony_ci#define DS1625_CONVERSION_MAX	500
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define DS1621_TEMP_MAX	125000
878c2ecf20Sopenharmony_ci#define DS1621_TEMP_MIN	(-55000)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* The DS1621 temperature registers */
908c2ecf20Sopenharmony_cistatic const u8 DS1621_REG_TEMP[3] = {
918c2ecf20Sopenharmony_ci	0xAA,		/* input, word, RO */
928c2ecf20Sopenharmony_ci	0xA2,		/* min, word, RW */
938c2ecf20Sopenharmony_ci	0xA1,		/* max, word, RW */
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci#define DS1621_REG_CONF			0xAC /* byte, RW */
968c2ecf20Sopenharmony_ci#define DS1621_COM_START		0xEE /* no data */
978c2ecf20Sopenharmony_ci#define DS1721_COM_START		0x51 /* no data */
988c2ecf20Sopenharmony_ci#define DS1621_COM_STOP			0x22 /* no data */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* The DS1621 configuration register */
1018c2ecf20Sopenharmony_ci#define DS1621_ALARM_TEMP_HIGH		0x40
1028c2ecf20Sopenharmony_ci#define DS1621_ALARM_TEMP_LOW		0x20
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* Conversions */
1058c2ecf20Sopenharmony_ci#define ALARMS_FROM_REG(val) ((val) & \
1068c2ecf20Sopenharmony_ci			(DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* Each client has this additional data */
1098c2ecf20Sopenharmony_cistruct ds1621_data {
1108c2ecf20Sopenharmony_ci	struct i2c_client *client;
1118c2ecf20Sopenharmony_ci	struct mutex update_lock;
1128c2ecf20Sopenharmony_ci	char valid;			/* !=0 if following fields are valid */
1138c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
1148c2ecf20Sopenharmony_ci	enum chips kind;		/* device type */
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	u16 temp[3];			/* Register values, word */
1178c2ecf20Sopenharmony_ci	u8 conf;			/* Register encoding, combined */
1188c2ecf20Sopenharmony_ci	u8 zbits;			/* Resolution encoded as number of
1198c2ecf20Sopenharmony_ci					 * zero bits */
1208c2ecf20Sopenharmony_ci	u16 update_interval;		/* Conversion rate in milliseconds */
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline int DS1621_TEMP_FROM_REG(u16 reg)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return DIV_ROUND_CLOSEST(((s16)reg / 16) * 625, 10);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/*
1298c2ecf20Sopenharmony_ci * TEMP: 0.001C/bit (-55C to +125C)
1308c2ecf20Sopenharmony_ci * REG:
1318c2ecf20Sopenharmony_ci *  - 1621, 1625: 0.5C/bit, 7 zero-bits
1328c2ecf20Sopenharmony_ci *  - 1631, 1721, 1731: 0.0625C/bit, 4 zero-bits
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_cistatic inline u16 DS1621_TEMP_TO_REG(long temp, u8 zbits)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	temp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
1378c2ecf20Sopenharmony_ci	temp = DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
1388c2ecf20Sopenharmony_ci	return temp;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic void ds1621_init_client(struct ds1621_data *data,
1428c2ecf20Sopenharmony_ci			       struct i2c_client *client)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	u8 conf, new_conf, sreg, resol;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
1478c2ecf20Sopenharmony_ci	/* switch to continuous conversion mode */
1488c2ecf20Sopenharmony_ci	new_conf &= ~DS1621_REG_CONFIG_1SHOT;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/* setup output polarity */
1518c2ecf20Sopenharmony_ci	if (polarity == 0)
1528c2ecf20Sopenharmony_ci		new_conf &= ~DS1621_REG_CONFIG_POLARITY;
1538c2ecf20Sopenharmony_ci	else if (polarity == 1)
1548c2ecf20Sopenharmony_ci		new_conf |= DS1621_REG_CONFIG_POLARITY;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (conf != new_conf)
1578c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	switch (data->kind) {
1608c2ecf20Sopenharmony_ci	case ds1625:
1618c2ecf20Sopenharmony_ci		data->update_interval = DS1625_CONVERSION_MAX;
1628c2ecf20Sopenharmony_ci		data->zbits = 7;
1638c2ecf20Sopenharmony_ci		sreg = DS1621_COM_START;
1648c2ecf20Sopenharmony_ci		break;
1658c2ecf20Sopenharmony_ci	case ds1631:
1668c2ecf20Sopenharmony_ci	case ds1721:
1678c2ecf20Sopenharmony_ci	case ds1731:
1688c2ecf20Sopenharmony_ci		resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
1698c2ecf20Sopenharmony_ci			 DS1621_REG_CONFIG_RESOL_SHIFT;
1708c2ecf20Sopenharmony_ci		data->update_interval = ds1721_convrates[resol];
1718c2ecf20Sopenharmony_ci		data->zbits = 7 - resol;
1728c2ecf20Sopenharmony_ci		sreg = DS1721_COM_START;
1738c2ecf20Sopenharmony_ci		break;
1748c2ecf20Sopenharmony_ci	default:
1758c2ecf20Sopenharmony_ci		data->update_interval = DS1621_CONVERSION_MAX;
1768c2ecf20Sopenharmony_ci		data->zbits = 7;
1778c2ecf20Sopenharmony_ci		sreg = DS1621_COM_START;
1788c2ecf20Sopenharmony_ci		break;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* start conversion */
1828c2ecf20Sopenharmony_ci	i2c_smbus_write_byte(client, sreg);
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic struct ds1621_data *ds1621_update_client(struct device *dev)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct ds1621_data *data = dev_get_drvdata(dev);
1888c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1898c2ecf20Sopenharmony_ci	u8 new_conf;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + data->update_interval) ||
1948c2ecf20Sopenharmony_ci	    !data->valid) {
1958c2ecf20Sopenharmony_ci		int i;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Starting ds1621 update\n");
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci		data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(data->temp); i++)
2028c2ecf20Sopenharmony_ci			data->temp[i] = i2c_smbus_read_word_swapped(client,
2038c2ecf20Sopenharmony_ci							 DS1621_REG_TEMP[i]);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		/* reset alarms if necessary */
2068c2ecf20Sopenharmony_ci		new_conf = data->conf;
2078c2ecf20Sopenharmony_ci		if (data->temp[0] > data->temp[1])	/* input > min */
2088c2ecf20Sopenharmony_ci			new_conf &= ~DS1621_ALARM_TEMP_LOW;
2098c2ecf20Sopenharmony_ci		if (data->temp[0] < data->temp[2])	/* input < max */
2108c2ecf20Sopenharmony_ci			new_conf &= ~DS1621_ALARM_TEMP_HIGH;
2118c2ecf20Sopenharmony_ci		if (data->conf != new_conf)
2128c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
2138c2ecf20Sopenharmony_ci						  new_conf);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
2168c2ecf20Sopenharmony_ci		data->valid = 1;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return data;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *da,
2258c2ecf20Sopenharmony_ci			 char *buf)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
2288c2ecf20Sopenharmony_ci	struct ds1621_data *data = ds1621_update_client(dev);
2298c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
2308c2ecf20Sopenharmony_ci		       DS1621_TEMP_FROM_REG(data->temp[attr->index]));
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic ssize_t temp_store(struct device *dev, struct device_attribute *da,
2348c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
2378c2ecf20Sopenharmony_ci	struct ds1621_data *data = dev_get_drvdata(dev);
2388c2ecf20Sopenharmony_ci	long val;
2398c2ecf20Sopenharmony_ci	int err;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
2428c2ecf20Sopenharmony_ci	if (err)
2438c2ecf20Sopenharmony_ci		return err;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2468c2ecf20Sopenharmony_ci	data->temp[attr->index] = DS1621_TEMP_TO_REG(val, data->zbits);
2478c2ecf20Sopenharmony_ci	i2c_smbus_write_word_swapped(data->client, DS1621_REG_TEMP[attr->index],
2488c2ecf20Sopenharmony_ci				     data->temp[attr->index]);
2498c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2508c2ecf20Sopenharmony_ci	return count;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *da,
2548c2ecf20Sopenharmony_ci			   char *buf)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct ds1621_data *data = ds1621_update_client(dev);
2578c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *da,
2618c2ecf20Sopenharmony_ci			  char *buf)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
2648c2ecf20Sopenharmony_ci	struct ds1621_data *data = ds1621_update_client(dev);
2658c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(data->conf & attr->index));
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic ssize_t update_interval_show(struct device *dev,
2698c2ecf20Sopenharmony_ci				    struct device_attribute *da, char *buf)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct ds1621_data *data = dev_get_drvdata(dev);
2728c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%hu\n", data->update_interval);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic ssize_t update_interval_store(struct device *dev,
2768c2ecf20Sopenharmony_ci				     struct device_attribute *da,
2778c2ecf20Sopenharmony_ci				     const char *buf, size_t count)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct ds1621_data *data = dev_get_drvdata(dev);
2808c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2818c2ecf20Sopenharmony_ci	unsigned long convrate;
2828c2ecf20Sopenharmony_ci	s32 err;
2838c2ecf20Sopenharmony_ci	int resol = 0;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &convrate);
2868c2ecf20Sopenharmony_ci	if (err)
2878c2ecf20Sopenharmony_ci		return err;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* Convert rate into resolution bits */
2908c2ecf20Sopenharmony_ci	while (resol < (ARRAY_SIZE(ds1721_convrates) - 1) &&
2918c2ecf20Sopenharmony_ci	       convrate > ds1721_convrates[resol])
2928c2ecf20Sopenharmony_ci		resol++;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2958c2ecf20Sopenharmony_ci	data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
2968c2ecf20Sopenharmony_ci	data->conf &= ~DS1621_REG_CONFIG_RESOL;
2978c2ecf20Sopenharmony_ci	data->conf |= (resol << DS1621_REG_CONFIG_RESOL_SHIFT);
2988c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, DS1621_REG_CONF, data->conf);
2998c2ecf20Sopenharmony_ci	data->update_interval = ds1721_convrates[resol];
3008c2ecf20Sopenharmony_ci	data->zbits = 7 - resol;
3018c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return count;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
3078c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(update_interval);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
3108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 1);
3118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
3128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, DS1621_ALARM_TEMP_LOW);
3138c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, DS1621_ALARM_TEMP_HIGH);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic struct attribute *ds1621_attributes[] = {
3168c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
3178c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
3188c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
3198c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
3208c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
3218c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
3228c2ecf20Sopenharmony_ci	&dev_attr_update_interval.attr,
3238c2ecf20Sopenharmony_ci	NULL
3248c2ecf20Sopenharmony_ci};
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic umode_t ds1621_attribute_visible(struct kobject *kobj,
3278c2ecf20Sopenharmony_ci					struct attribute *attr, int index)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct device *dev = container_of(kobj, struct device, kobj);
3308c2ecf20Sopenharmony_ci	struct ds1621_data *data = dev_get_drvdata(dev);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (attr == &dev_attr_update_interval.attr)
3338c2ecf20Sopenharmony_ci		if (data->kind == ds1621 || data->kind == ds1625)
3348c2ecf20Sopenharmony_ci			/* shhh, we're hiding update_interval */
3358c2ecf20Sopenharmony_ci			return 0;
3368c2ecf20Sopenharmony_ci	return attr->mode;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic const struct attribute_group ds1621_group = {
3408c2ecf20Sopenharmony_ci	.attrs = ds1621_attributes,
3418c2ecf20Sopenharmony_ci	.is_visible = ds1621_attribute_visible
3428c2ecf20Sopenharmony_ci};
3438c2ecf20Sopenharmony_ci__ATTRIBUTE_GROUPS(ds1621);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic const struct i2c_device_id ds1621_id[];
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int ds1621_probe(struct i2c_client *client)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct ds1621_data *data;
3508c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	data = devm_kzalloc(&client->dev, sizeof(struct ds1621_data),
3538c2ecf20Sopenharmony_ci			    GFP_KERNEL);
3548c2ecf20Sopenharmony_ci	if (!data)
3558c2ecf20Sopenharmony_ci		return -ENOMEM;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	data->kind = i2c_match_id(ds1621_id, client)->driver_data;
3608c2ecf20Sopenharmony_ci	data->client = client;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	/* Initialize the DS1621 chip */
3638c2ecf20Sopenharmony_ci	ds1621_init_client(data, client);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
3668c2ecf20Sopenharmony_ci							   client->name, data,
3678c2ecf20Sopenharmony_ci							   ds1621_groups);
3688c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic const struct i2c_device_id ds1621_id[] = {
3728c2ecf20Sopenharmony_ci	{ "ds1621", ds1621 },
3738c2ecf20Sopenharmony_ci	{ "ds1625", ds1625 },
3748c2ecf20Sopenharmony_ci	{ "ds1631", ds1631 },
3758c2ecf20Sopenharmony_ci	{ "ds1721", ds1721 },
3768c2ecf20Sopenharmony_ci	{ "ds1731", ds1731 },
3778c2ecf20Sopenharmony_ci	{ }
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ds1621_id);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/* This is the driver that will be inserted */
3828c2ecf20Sopenharmony_cistatic struct i2c_driver ds1621_driver = {
3838c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
3848c2ecf20Sopenharmony_ci	.driver = {
3858c2ecf20Sopenharmony_ci		.name	= "ds1621",
3868c2ecf20Sopenharmony_ci	},
3878c2ecf20Sopenharmony_ci	.probe_new	= ds1621_probe,
3888c2ecf20Sopenharmony_ci	.id_table	= ds1621_id,
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cimodule_i2c_driver(ds1621_driver);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ciMODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
3948c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DS1621 driver");
3958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
396