18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
48c2ecf20Sopenharmony_ci *	      monitoring
58c2ecf20Sopenharmony_ci * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
68c2ecf20Sopenharmony_ci * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
78c2ecf20Sopenharmony_ci * Philip Edelbrock <phil@netroedge.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
158c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Addresses to scan */
238c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Insmod parameters */
268c2ecf20Sopenharmony_cienum chips { thmc50, adm1022 };
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic unsigned short adm1022_temp3[16];
298c2ecf20Sopenharmony_cistatic unsigned int adm1022_temp3_num;
308c2ecf20Sopenharmony_cimodule_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
318c2ecf20Sopenharmony_ciMODULE_PARM_DESC(adm1022_temp3,
328c2ecf20Sopenharmony_ci		 "List of adapter,address pairs to enable 3rd temperature (ADM1022 only)");
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Many THMC50 constants specified below */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* The THMC50 registers */
378c2ecf20Sopenharmony_ci#define THMC50_REG_CONF				0x40
388c2ecf20Sopenharmony_ci#define THMC50_REG_COMPANY_ID			0x3E
398c2ecf20Sopenharmony_ci#define THMC50_REG_DIE_CODE			0x3F
408c2ecf20Sopenharmony_ci#define THMC50_REG_ANALOG_OUT			0x19
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * The mirror status register cannot be used as
438c2ecf20Sopenharmony_ci * reading it does not clear alarms.
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci#define THMC50_REG_INTR				0x41
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
488c2ecf20Sopenharmony_cistatic const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
498c2ecf20Sopenharmony_cistatic const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
508c2ecf20Sopenharmony_cistatic const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
518c2ecf20Sopenharmony_cistatic const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define THMC50_REG_CONF_nFANOFF			0x20
548c2ecf20Sopenharmony_ci#define THMC50_REG_CONF_PROGRAMMED		0x08
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Each client has this additional data */
578c2ecf20Sopenharmony_cistruct thmc50_data {
588c2ecf20Sopenharmony_ci	struct i2c_client *client;
598c2ecf20Sopenharmony_ci	const struct attribute_group *groups[3];
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	struct mutex update_lock;
628c2ecf20Sopenharmony_ci	enum chips type;
638c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
648c2ecf20Sopenharmony_ci	char has_temp3;		/* !=0 if it is ADM1022 in temp3 mode */
658c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Register values */
688c2ecf20Sopenharmony_ci	s8 temp_input[3];
698c2ecf20Sopenharmony_ci	s8 temp_max[3];
708c2ecf20Sopenharmony_ci	s8 temp_min[3];
718c2ecf20Sopenharmony_ci	s8 temp_critical[3];
728c2ecf20Sopenharmony_ci	u8 analog_out;
738c2ecf20Sopenharmony_ci	u8 alarms;
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic struct thmc50_data *thmc50_update_device(struct device *dev)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct thmc50_data *data = dev_get_drvdata(dev);
798c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
808c2ecf20Sopenharmony_ci	int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + timeout)
858c2ecf20Sopenharmony_ci	    || !data->valid) {
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci		int temps = data->has_temp3 ? 3 : 2;
888c2ecf20Sopenharmony_ci		int i;
898c2ecf20Sopenharmony_ci		int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci		prog &= THMC50_REG_CONF_PROGRAMMED;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		for (i = 0; i < temps; i++) {
948c2ecf20Sopenharmony_ci			data->temp_input[i] = i2c_smbus_read_byte_data(client,
958c2ecf20Sopenharmony_ci						THMC50_REG_TEMP[i]);
968c2ecf20Sopenharmony_ci			data->temp_max[i] = i2c_smbus_read_byte_data(client,
978c2ecf20Sopenharmony_ci						THMC50_REG_TEMP_MAX[i]);
988c2ecf20Sopenharmony_ci			data->temp_min[i] = i2c_smbus_read_byte_data(client,
998c2ecf20Sopenharmony_ci						THMC50_REG_TEMP_MIN[i]);
1008c2ecf20Sopenharmony_ci			data->temp_critical[i] =
1018c2ecf20Sopenharmony_ci				i2c_smbus_read_byte_data(client,
1028c2ecf20Sopenharmony_ci					prog ? THMC50_REG_TEMP_CRITICAL[i]
1038c2ecf20Sopenharmony_ci					     : THMC50_REG_TEMP_DEFAULT[i]);
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci		data->analog_out =
1068c2ecf20Sopenharmony_ci		    i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
1078c2ecf20Sopenharmony_ci		data->alarms =
1088c2ecf20Sopenharmony_ci		    i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
1098c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
1108c2ecf20Sopenharmony_ci		data->valid = 1;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return data;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic ssize_t analog_out_show(struct device *dev,
1198c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct thmc50_data *data = thmc50_update_device(dev);
1228c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->analog_out);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic ssize_t analog_out_store(struct device *dev,
1268c2ecf20Sopenharmony_ci				struct device_attribute *attr,
1278c2ecf20Sopenharmony_ci				const char *buf, size_t count)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct thmc50_data *data = dev_get_drvdata(dev);
1308c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1318c2ecf20Sopenharmony_ci	int config;
1328c2ecf20Sopenharmony_ci	unsigned long tmp;
1338c2ecf20Sopenharmony_ci	int err;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &tmp);
1368c2ecf20Sopenharmony_ci	if (err)
1378c2ecf20Sopenharmony_ci		return err;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1408c2ecf20Sopenharmony_ci	data->analog_out = clamp_val(tmp, 0, 255);
1418c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
1428c2ecf20Sopenharmony_ci				  data->analog_out);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
1458c2ecf20Sopenharmony_ci	if (data->analog_out == 0)
1468c2ecf20Sopenharmony_ci		config &= ~THMC50_REG_CONF_nFANOFF;
1478c2ecf20Sopenharmony_ci	else
1488c2ecf20Sopenharmony_ci		config |= THMC50_REG_CONF_nFANOFF;
1498c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1528c2ecf20Sopenharmony_ci	return count;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* There is only one PWM mode = DC */
1568c2ecf20Sopenharmony_cistatic ssize_t pwm_mode_show(struct device *dev,
1578c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	return sprintf(buf, "0\n");
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/* Temperatures */
1638c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *attr,
1648c2ecf20Sopenharmony_ci			 char *buf)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
1678c2ecf20Sopenharmony_ci	struct thmc50_data *data = thmc50_update_device(dev);
1688c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic ssize_t temp_min_show(struct device *dev,
1728c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
1758c2ecf20Sopenharmony_ci	struct thmc50_data *data = thmc50_update_device(dev);
1768c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic ssize_t temp_min_store(struct device *dev,
1808c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
1818c2ecf20Sopenharmony_ci			      size_t count)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
1848c2ecf20Sopenharmony_ci	struct thmc50_data *data = dev_get_drvdata(dev);
1858c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1868c2ecf20Sopenharmony_ci	long val;
1878c2ecf20Sopenharmony_ci	int err;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
1908c2ecf20Sopenharmony_ci	if (err)
1918c2ecf20Sopenharmony_ci		return err;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1948c2ecf20Sopenharmony_ci	data->temp_min[nr] = clamp_val(val / 1000, -128, 127);
1958c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
1968c2ecf20Sopenharmony_ci				  data->temp_min[nr]);
1978c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1988c2ecf20Sopenharmony_ci	return count;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic ssize_t temp_max_show(struct device *dev,
2028c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2058c2ecf20Sopenharmony_ci	struct thmc50_data *data = thmc50_update_device(dev);
2068c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev,
2108c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
2118c2ecf20Sopenharmony_ci			      size_t count)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2148c2ecf20Sopenharmony_ci	struct thmc50_data *data = dev_get_drvdata(dev);
2158c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2168c2ecf20Sopenharmony_ci	long val;
2178c2ecf20Sopenharmony_ci	int err;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
2208c2ecf20Sopenharmony_ci	if (err)
2218c2ecf20Sopenharmony_ci		return err;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2248c2ecf20Sopenharmony_ci	data->temp_max[nr] = clamp_val(val / 1000, -128, 127);
2258c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
2268c2ecf20Sopenharmony_ci				  data->temp_max[nr]);
2278c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2288c2ecf20Sopenharmony_ci	return count;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic ssize_t temp_critical_show(struct device *dev,
2328c2ecf20Sopenharmony_ci				  struct device_attribute *attr, char *buf)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2358c2ecf20Sopenharmony_ci	struct thmc50_data *data = thmc50_update_device(dev);
2368c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
2408c2ecf20Sopenharmony_ci			  char *buf)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
2438c2ecf20Sopenharmony_ci	struct thmc50_data *data = thmc50_update_device(dev);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
2498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
2508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
2518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_crit, temp_critical, 0);
2528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
2538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
2548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
2558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_crit, temp_critical, 1);
2568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
2578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
2588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
2598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_crit, temp_critical, 2);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 0);
2628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
2638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 1);
2648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 7);
2658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1, analog_out, 0);
2688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic struct attribute *thmc50_attributes[] = {
2718c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
2728c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
2738c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
2748c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit.dev_attr.attr,
2758c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
2768c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
2778c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
2788c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
2798c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit.dev_attr.attr,
2808c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
2818c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
2828c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1.dev_attr.attr,
2838c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
2848c2ecf20Sopenharmony_ci	NULL
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic const struct attribute_group thmc50_group = {
2888c2ecf20Sopenharmony_ci	.attrs = thmc50_attributes,
2898c2ecf20Sopenharmony_ci};
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci/* for ADM1022 3rd temperature mode */
2928c2ecf20Sopenharmony_cistatic struct attribute *temp3_attributes[] = {
2938c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
2948c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min.dev_attr.attr,
2958c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
2968c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_crit.dev_attr.attr,
2978c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
2988c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_fault.dev_attr.attr,
2998c2ecf20Sopenharmony_ci	NULL
3008c2ecf20Sopenharmony_ci};
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic const struct attribute_group temp3_group = {
3038c2ecf20Sopenharmony_ci	.attrs = temp3_attributes,
3048c2ecf20Sopenharmony_ci};
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
3078c2ecf20Sopenharmony_cistatic int thmc50_detect(struct i2c_client *client,
3088c2ecf20Sopenharmony_ci			 struct i2c_board_info *info)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	unsigned company;
3118c2ecf20Sopenharmony_ci	unsigned revision;
3128c2ecf20Sopenharmony_ci	unsigned config;
3138c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
3148c2ecf20Sopenharmony_ci	const char *type_name;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
3178c2ecf20Sopenharmony_ci		pr_debug("thmc50: detect failed, smbus byte data not supported!\n");
3188c2ecf20Sopenharmony_ci		return -ENODEV;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
3228c2ecf20Sopenharmony_ci		 client->addr, i2c_adapter_id(client->adapter));
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
3258c2ecf20Sopenharmony_ci	revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
3268c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
3278c2ecf20Sopenharmony_ci	if (revision < 0xc0 || (config & 0x10))
3288c2ecf20Sopenharmony_ci		return -ENODEV;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	if (company == 0x41) {
3318c2ecf20Sopenharmony_ci		int id = i2c_adapter_id(client->adapter);
3328c2ecf20Sopenharmony_ci		int i;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci		type_name = "adm1022";
3358c2ecf20Sopenharmony_ci		for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
3368c2ecf20Sopenharmony_ci			if (adm1022_temp3[i] == id &&
3378c2ecf20Sopenharmony_ci			    adm1022_temp3[i + 1] == client->addr) {
3388c2ecf20Sopenharmony_ci				/* enable 2nd remote temp */
3398c2ecf20Sopenharmony_ci				config |= (1 << 7);
3408c2ecf20Sopenharmony_ci				i2c_smbus_write_byte_data(client,
3418c2ecf20Sopenharmony_ci							  THMC50_REG_CONF,
3428c2ecf20Sopenharmony_ci							  config);
3438c2ecf20Sopenharmony_ci				break;
3448c2ecf20Sopenharmony_ci			}
3458c2ecf20Sopenharmony_ci	} else if (company == 0x49) {
3468c2ecf20Sopenharmony_ci		type_name = "thmc50";
3478c2ecf20Sopenharmony_ci	} else {
3488c2ecf20Sopenharmony_ci		pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
3498c2ecf20Sopenharmony_ci		return -ENODEV;
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
3538c2ecf20Sopenharmony_ci		 type_name, (revision >> 4) - 0xc, revision & 0xf);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	strlcpy(info->type, type_name, I2C_NAME_SIZE);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return 0;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic void thmc50_init_client(struct thmc50_data *data)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3638c2ecf20Sopenharmony_ci	int config;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	data->analog_out = i2c_smbus_read_byte_data(client,
3668c2ecf20Sopenharmony_ci						    THMC50_REG_ANALOG_OUT);
3678c2ecf20Sopenharmony_ci	/* set up to at least 1 */
3688c2ecf20Sopenharmony_ci	if (data->analog_out == 0) {
3698c2ecf20Sopenharmony_ci		data->analog_out = 1;
3708c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
3718c2ecf20Sopenharmony_ci					  data->analog_out);
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
3748c2ecf20Sopenharmony_ci	config |= 0x1;	/* start the chip if it is in standby mode */
3758c2ecf20Sopenharmony_ci	if (data->type == adm1022 && (config & (1 << 7)))
3768c2ecf20Sopenharmony_ci		data->has_temp3 = 1;
3778c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic const struct i2c_device_id thmc50_id[];
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic int thmc50_probe(struct i2c_client *client)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
3858c2ecf20Sopenharmony_ci	struct thmc50_data *data;
3868c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
3878c2ecf20Sopenharmony_ci	int idx = 0;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct thmc50_data), GFP_KERNEL);
3908c2ecf20Sopenharmony_ci	if (!data)
3918c2ecf20Sopenharmony_ci		return -ENOMEM;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	data->client = client;
3948c2ecf20Sopenharmony_ci	data->type = i2c_match_id(thmc50_id, client)->driver_data;
3958c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	thmc50_init_client(data);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* sysfs hooks */
4008c2ecf20Sopenharmony_ci	data->groups[idx++] = &thmc50_group;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* Register additional ADM1022 sysfs hooks */
4038c2ecf20Sopenharmony_ci	if (data->has_temp3)
4048c2ecf20Sopenharmony_ci		data->groups[idx++] = &temp3_group;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
4078c2ecf20Sopenharmony_ci							   data, data->groups);
4088c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic const struct i2c_device_id thmc50_id[] = {
4128c2ecf20Sopenharmony_ci	{ "adm1022", adm1022 },
4138c2ecf20Sopenharmony_ci	{ "thmc50", thmc50 },
4148c2ecf20Sopenharmony_ci	{ }
4158c2ecf20Sopenharmony_ci};
4168c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, thmc50_id);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic struct i2c_driver thmc50_driver = {
4198c2ecf20Sopenharmony_ci	.class = I2C_CLASS_HWMON,
4208c2ecf20Sopenharmony_ci	.driver = {
4218c2ecf20Sopenharmony_ci		.name = "thmc50",
4228c2ecf20Sopenharmony_ci	},
4238c2ecf20Sopenharmony_ci	.probe_new = thmc50_probe,
4248c2ecf20Sopenharmony_ci	.id_table = thmc50_id,
4258c2ecf20Sopenharmony_ci	.detect = thmc50_detect,
4268c2ecf20Sopenharmony_ci	.address_list = normal_i2c,
4278c2ecf20Sopenharmony_ci};
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cimodule_i2c_driver(thmc50_driver);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
4328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("THMC50 driver");
433