18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * max6639.c - Support for Maxim MAX6639
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * based on the initial MAX6639 support from semptian.net
108c2ecf20Sopenharmony_ci * by He Changqing <hechangqing@semptian.com>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
198c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
208c2ecf20Sopenharmony_ci#include <linux/err.h>
218c2ecf20Sopenharmony_ci#include <linux/mutex.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_data/max6639.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Addresses to scan */
258c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* The MAX6639 registers, valid channel numbers: 0, 1 */
288c2ecf20Sopenharmony_ci#define MAX6639_REG_TEMP(ch)			(0x00 + (ch))
298c2ecf20Sopenharmony_ci#define MAX6639_REG_STATUS			0x02
308c2ecf20Sopenharmony_ci#define MAX6639_REG_OUTPUT_MASK			0x03
318c2ecf20Sopenharmony_ci#define MAX6639_REG_GCONFIG			0x04
328c2ecf20Sopenharmony_ci#define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch))
338c2ecf20Sopenharmony_ci#define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch))
348c2ecf20Sopenharmony_ci#define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch))
358c2ecf20Sopenharmony_ci#define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch))
368c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4)
378c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4)
388c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4)
398c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4)
408c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch))
418c2ecf20Sopenharmony_ci#define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch))
428c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch))
438c2ecf20Sopenharmony_ci#define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch))
448c2ecf20Sopenharmony_ci#define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch))
458c2ecf20Sopenharmony_ci#define MAX6639_REG_DEVID			0x3D
468c2ecf20Sopenharmony_ci#define MAX6639_REG_MANUID			0x3E
478c2ecf20Sopenharmony_ci#define MAX6639_REG_DEVREV			0x3F
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Register bits */
508c2ecf20Sopenharmony_ci#define MAX6639_GCONFIG_STANDBY			0x80
518c2ecf20Sopenharmony_ci#define MAX6639_GCONFIG_POR			0x40
528c2ecf20Sopenharmony_ci#define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20
538c2ecf20Sopenharmony_ci#define MAX6639_GCONFIG_CH2_LOCAL		0x10
548c2ecf20Sopenharmony_ci#define MAX6639_GCONFIG_PWM_FREQ_HI		0x08
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define MAX6639_FAN_CONFIG1_PWM			0x80
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \
638c2ecf20Sopenharmony_ci				0 : (rpm_ranges[rpm_range] * 30) / (val))
648c2ecf20Sopenharmony_ci#define TEMP_LIMIT_TO_REG(val)	clamp_val((val) / 1000, 0, 255)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * Client data (each client gets its own)
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_cistruct max6639_data {
708c2ecf20Sopenharmony_ci	struct i2c_client *client;
718c2ecf20Sopenharmony_ci	struct mutex update_lock;
728c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
738c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* Register values sampled regularly */
768c2ecf20Sopenharmony_ci	u16 temp[2];		/* Temperature, in 1/8 C, 0..255 C */
778c2ecf20Sopenharmony_ci	bool temp_fault[2];	/* Detected temperature diode failure */
788c2ecf20Sopenharmony_ci	u8 fan[2];		/* Register value: TACH count for fans >=30 */
798c2ecf20Sopenharmony_ci	u8 status;		/* Detected channel alarms and fan failures */
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Register values only written to */
828c2ecf20Sopenharmony_ci	u8 pwm[2];		/* Register value: Duty cycle 0..120 */
838c2ecf20Sopenharmony_ci	u8 temp_therm[2];	/* THERM Temperature, 0..255 C (->_max) */
848c2ecf20Sopenharmony_ci	u8 temp_alert[2];	/* ALERT Temperature, 0..255 C (->_crit) */
858c2ecf20Sopenharmony_ci	u8 temp_ot[2];		/* OT Temperature, 0..255 C (->_emergency) */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* Register values initialized only once */
888c2ecf20Sopenharmony_ci	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
898c2ecf20Sopenharmony_ci	u8 rpm_range;		/* Index in above rpm_ranges table */
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic struct max6639_data *max6639_update_device(struct device *dev)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
958c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
968c2ecf20Sopenharmony_ci	struct max6639_data *ret = data;
978c2ecf20Sopenharmony_ci	int i;
988c2ecf20Sopenharmony_ci	int status_reg;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
1038c2ecf20Sopenharmony_ci		int res;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Starting max6639 update\n");
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		status_reg = i2c_smbus_read_byte_data(client,
1088c2ecf20Sopenharmony_ci						      MAX6639_REG_STATUS);
1098c2ecf20Sopenharmony_ci		if (status_reg < 0) {
1108c2ecf20Sopenharmony_ci			ret = ERR_PTR(status_reg);
1118c2ecf20Sopenharmony_ci			goto abort;
1128c2ecf20Sopenharmony_ci		}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci		data->status = status_reg;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
1178c2ecf20Sopenharmony_ci			res = i2c_smbus_read_byte_data(client,
1188c2ecf20Sopenharmony_ci					MAX6639_REG_FAN_CNT(i));
1198c2ecf20Sopenharmony_ci			if (res < 0) {
1208c2ecf20Sopenharmony_ci				ret = ERR_PTR(res);
1218c2ecf20Sopenharmony_ci				goto abort;
1228c2ecf20Sopenharmony_ci			}
1238c2ecf20Sopenharmony_ci			data->fan[i] = res;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			res = i2c_smbus_read_byte_data(client,
1268c2ecf20Sopenharmony_ci					MAX6639_REG_TEMP_EXT(i));
1278c2ecf20Sopenharmony_ci			if (res < 0) {
1288c2ecf20Sopenharmony_ci				ret = ERR_PTR(res);
1298c2ecf20Sopenharmony_ci				goto abort;
1308c2ecf20Sopenharmony_ci			}
1318c2ecf20Sopenharmony_ci			data->temp[i] = res >> 5;
1328c2ecf20Sopenharmony_ci			data->temp_fault[i] = res & 0x01;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci			res = i2c_smbus_read_byte_data(client,
1358c2ecf20Sopenharmony_ci					MAX6639_REG_TEMP(i));
1368c2ecf20Sopenharmony_ci			if (res < 0) {
1378c2ecf20Sopenharmony_ci				ret = ERR_PTR(res);
1388c2ecf20Sopenharmony_ci				goto abort;
1398c2ecf20Sopenharmony_ci			}
1408c2ecf20Sopenharmony_ci			data->temp[i] |= res << 3;
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
1448c2ecf20Sopenharmony_ci		data->valid = 1;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ciabort:
1478c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return ret;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic ssize_t temp_input_show(struct device *dev,
1538c2ecf20Sopenharmony_ci			       struct device_attribute *dev_attr, char *buf)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	long temp;
1568c2ecf20Sopenharmony_ci	struct max6639_data *data = max6639_update_device(dev);
1578c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (IS_ERR(data))
1608c2ecf20Sopenharmony_ci		return PTR_ERR(data);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	temp = data->temp[attr->index] * 125;
1638c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", temp);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic ssize_t temp_fault_show(struct device *dev,
1678c2ecf20Sopenharmony_ci			       struct device_attribute *dev_attr, char *buf)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct max6639_data *data = max6639_update_device(dev);
1708c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	if (IS_ERR(data))
1738c2ecf20Sopenharmony_ci		return PTR_ERR(data);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic ssize_t temp_max_show(struct device *dev,
1798c2ecf20Sopenharmony_ci			     struct device_attribute *dev_attr, char *buf)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1828c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev,
1888c2ecf20Sopenharmony_ci			      struct device_attribute *dev_attr,
1898c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1928c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
1938c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1948c2ecf20Sopenharmony_ci	unsigned long val;
1958c2ecf20Sopenharmony_ci	int res;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	res = kstrtoul(buf, 10, &val);
1988c2ecf20Sopenharmony_ci	if (res)
1998c2ecf20Sopenharmony_ci		return res;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2028c2ecf20Sopenharmony_ci	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
2038c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client,
2048c2ecf20Sopenharmony_ci				  MAX6639_REG_THERM_LIMIT(attr->index),
2058c2ecf20Sopenharmony_ci				  data->temp_therm[attr->index]);
2068c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2078c2ecf20Sopenharmony_ci	return count;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic ssize_t temp_crit_show(struct device *dev,
2118c2ecf20Sopenharmony_ci			      struct device_attribute *dev_attr, char *buf)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2148c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic ssize_t temp_crit_store(struct device *dev,
2208c2ecf20Sopenharmony_ci			       struct device_attribute *dev_attr,
2218c2ecf20Sopenharmony_ci			       const char *buf, size_t count)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2248c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
2258c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2268c2ecf20Sopenharmony_ci	unsigned long val;
2278c2ecf20Sopenharmony_ci	int res;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	res = kstrtoul(buf, 10, &val);
2308c2ecf20Sopenharmony_ci	if (res)
2318c2ecf20Sopenharmony_ci		return res;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2348c2ecf20Sopenharmony_ci	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
2358c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client,
2368c2ecf20Sopenharmony_ci				  MAX6639_REG_ALERT_LIMIT(attr->index),
2378c2ecf20Sopenharmony_ci				  data->temp_alert[attr->index]);
2388c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2398c2ecf20Sopenharmony_ci	return count;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic ssize_t temp_emergency_show(struct device *dev,
2438c2ecf20Sopenharmony_ci				   struct device_attribute *dev_attr,
2448c2ecf20Sopenharmony_ci				   char *buf)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2478c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic ssize_t temp_emergency_store(struct device *dev,
2538c2ecf20Sopenharmony_ci				    struct device_attribute *dev_attr,
2548c2ecf20Sopenharmony_ci				    const char *buf, size_t count)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2578c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
2588c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2598c2ecf20Sopenharmony_ci	unsigned long val;
2608c2ecf20Sopenharmony_ci	int res;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	res = kstrtoul(buf, 10, &val);
2638c2ecf20Sopenharmony_ci	if (res)
2648c2ecf20Sopenharmony_ci		return res;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2678c2ecf20Sopenharmony_ci	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
2688c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client,
2698c2ecf20Sopenharmony_ci				  MAX6639_REG_OT_LIMIT(attr->index),
2708c2ecf20Sopenharmony_ci				  data->temp_ot[attr->index]);
2718c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2728c2ecf20Sopenharmony_ci	return count;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr,
2768c2ecf20Sopenharmony_ci			char *buf)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2798c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic ssize_t pwm_store(struct device *dev,
2858c2ecf20Sopenharmony_ci			 struct device_attribute *dev_attr, const char *buf,
2868c2ecf20Sopenharmony_ci			 size_t count)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2898c2ecf20Sopenharmony_ci	struct max6639_data *data = dev_get_drvdata(dev);
2908c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2918c2ecf20Sopenharmony_ci	unsigned long val;
2928c2ecf20Sopenharmony_ci	int res;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	res = kstrtoul(buf, 10, &val);
2958c2ecf20Sopenharmony_ci	if (res)
2968c2ecf20Sopenharmony_ci		return res;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	val = clamp_val(val, 0, 255);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3018c2ecf20Sopenharmony_ci	data->pwm[attr->index] = (u8)(val * 120 / 255);
3028c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client,
3038c2ecf20Sopenharmony_ci				  MAX6639_REG_TARGTDUTY(attr->index),
3048c2ecf20Sopenharmony_ci				  data->pwm[attr->index]);
3058c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3068c2ecf20Sopenharmony_ci	return count;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic ssize_t fan_input_show(struct device *dev,
3108c2ecf20Sopenharmony_ci			      struct device_attribute *dev_attr, char *buf)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	struct max6639_data *data = max6639_update_device(dev);
3138c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3168c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
3198c2ecf20Sopenharmony_ci		       data->rpm_range));
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev,
3238c2ecf20Sopenharmony_ci			  struct device_attribute *dev_attr, char *buf)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct max6639_data *data = max6639_update_device(dev);
3268c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3298c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
3358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
3368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
3378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
3388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
3398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
3408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0);
3418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1);
3428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0);
3438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1);
3448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
3458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
3468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
3478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
3488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1);
3498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0);
3508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3);
3518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2);
3528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7);
3538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6);
3548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5);
3558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic struct attribute *max6639_attrs[] = {
3598c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
3608c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
3618c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_fault.dev_attr.attr,
3628c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
3638c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
3648c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
3658c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3668c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3678c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3688c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3698c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1.dev_attr.attr,
3708c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2.dev_attr.attr,
3718c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
3728c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
3738c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_fault.dev_attr.attr,
3748c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_fault.dev_attr.attr,
3758c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
3768c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
3778c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
3788c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
3798c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
3808c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
3818c2ecf20Sopenharmony_ci	NULL
3828c2ecf20Sopenharmony_ci};
3838c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(max6639);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci/*
3868c2ecf20Sopenharmony_ci *  returns respective index in rpm_ranges table
3878c2ecf20Sopenharmony_ci *  1 by default on invalid range
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_cistatic int rpm_range_to_reg(int range)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	int i;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
3948c2ecf20Sopenharmony_ci		if (rpm_ranges[i] == range)
3958c2ecf20Sopenharmony_ci			return i;
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return 1; /* default: 4000 RPM */
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int max6639_init_client(struct i2c_client *client,
4028c2ecf20Sopenharmony_ci			       struct max6639_data *data)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	struct max6639_platform_data *max6639_info =
4058c2ecf20Sopenharmony_ci		dev_get_platdata(&client->dev);
4068c2ecf20Sopenharmony_ci	int i;
4078c2ecf20Sopenharmony_ci	int rpm_range = 1; /* default: 4000 RPM */
4088c2ecf20Sopenharmony_ci	int err;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/* Reset chip to default values, see below for GCONFIG setup */
4118c2ecf20Sopenharmony_ci	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
4128c2ecf20Sopenharmony_ci				  MAX6639_GCONFIG_POR);
4138c2ecf20Sopenharmony_ci	if (err)
4148c2ecf20Sopenharmony_ci		goto exit;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	/* Fans pulse per revolution is 2 by default */
4178c2ecf20Sopenharmony_ci	if (max6639_info && max6639_info->ppr > 0 &&
4188c2ecf20Sopenharmony_ci			max6639_info->ppr < 5)
4198c2ecf20Sopenharmony_ci		data->ppr = max6639_info->ppr;
4208c2ecf20Sopenharmony_ci	else
4218c2ecf20Sopenharmony_ci		data->ppr = 2;
4228c2ecf20Sopenharmony_ci	data->ppr -= 1;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	if (max6639_info)
4258c2ecf20Sopenharmony_ci		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
4268c2ecf20Sopenharmony_ci	data->rpm_range = rpm_range;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci		/* Set Fan pulse per revolution */
4318c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4328c2ecf20Sopenharmony_ci				MAX6639_REG_FAN_PPR(i),
4338c2ecf20Sopenharmony_ci				data->ppr << 6);
4348c2ecf20Sopenharmony_ci		if (err)
4358c2ecf20Sopenharmony_ci			goto exit;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci		/* Fans config PWM, RPM */
4388c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4398c2ecf20Sopenharmony_ci			MAX6639_REG_FAN_CONFIG1(i),
4408c2ecf20Sopenharmony_ci			MAX6639_FAN_CONFIG1_PWM | rpm_range);
4418c2ecf20Sopenharmony_ci		if (err)
4428c2ecf20Sopenharmony_ci			goto exit;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci		/* Fans PWM polarity high by default */
4458c2ecf20Sopenharmony_ci		if (max6639_info && max6639_info->pwm_polarity == 0)
4468c2ecf20Sopenharmony_ci			err = i2c_smbus_write_byte_data(client,
4478c2ecf20Sopenharmony_ci				MAX6639_REG_FAN_CONFIG2a(i), 0x00);
4488c2ecf20Sopenharmony_ci		else
4498c2ecf20Sopenharmony_ci			err = i2c_smbus_write_byte_data(client,
4508c2ecf20Sopenharmony_ci				MAX6639_REG_FAN_CONFIG2a(i), 0x02);
4518c2ecf20Sopenharmony_ci		if (err)
4528c2ecf20Sopenharmony_ci			goto exit;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		/*
4558c2ecf20Sopenharmony_ci		 * /THERM full speed enable,
4568c2ecf20Sopenharmony_ci		 * PWM frequency 25kHz, see also GCONFIG below
4578c2ecf20Sopenharmony_ci		 */
4588c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4598c2ecf20Sopenharmony_ci			MAX6639_REG_FAN_CONFIG3(i),
4608c2ecf20Sopenharmony_ci			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
4618c2ecf20Sopenharmony_ci		if (err)
4628c2ecf20Sopenharmony_ci			goto exit;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci		/* Max. temp. 80C/90C/100C */
4658c2ecf20Sopenharmony_ci		data->temp_therm[i] = 80;
4668c2ecf20Sopenharmony_ci		data->temp_alert[i] = 90;
4678c2ecf20Sopenharmony_ci		data->temp_ot[i] = 100;
4688c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4698c2ecf20Sopenharmony_ci				MAX6639_REG_THERM_LIMIT(i),
4708c2ecf20Sopenharmony_ci				data->temp_therm[i]);
4718c2ecf20Sopenharmony_ci		if (err)
4728c2ecf20Sopenharmony_ci			goto exit;
4738c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4748c2ecf20Sopenharmony_ci				MAX6639_REG_ALERT_LIMIT(i),
4758c2ecf20Sopenharmony_ci				data->temp_alert[i]);
4768c2ecf20Sopenharmony_ci		if (err)
4778c2ecf20Sopenharmony_ci			goto exit;
4788c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4798c2ecf20Sopenharmony_ci				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
4808c2ecf20Sopenharmony_ci		if (err)
4818c2ecf20Sopenharmony_ci			goto exit;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci		/* PWM 120/120 (i.e. 100%) */
4848c2ecf20Sopenharmony_ci		data->pwm[i] = 120;
4858c2ecf20Sopenharmony_ci		err = i2c_smbus_write_byte_data(client,
4868c2ecf20Sopenharmony_ci				MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
4878c2ecf20Sopenharmony_ci		if (err)
4888c2ecf20Sopenharmony_ci			goto exit;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci	/* Start monitoring */
4918c2ecf20Sopenharmony_ci	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
4928c2ecf20Sopenharmony_ci		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
4938c2ecf20Sopenharmony_ci		MAX6639_GCONFIG_PWM_FREQ_HI);
4948c2ecf20Sopenharmony_ciexit:
4958c2ecf20Sopenharmony_ci	return err;
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
4998c2ecf20Sopenharmony_cistatic int max6639_detect(struct i2c_client *client,
5008c2ecf20Sopenharmony_ci			  struct i2c_board_info *info)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
5038c2ecf20Sopenharmony_ci	int dev_id, manu_id;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
5068c2ecf20Sopenharmony_ci		return -ENODEV;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	/* Actual detection via device and manufacturer ID */
5098c2ecf20Sopenharmony_ci	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
5108c2ecf20Sopenharmony_ci	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
5118c2ecf20Sopenharmony_ci	if (dev_id != 0x58 || manu_id != 0x4D)
5128c2ecf20Sopenharmony_ci		return -ENODEV;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	strlcpy(info->type, "max6639", I2C_NAME_SIZE);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	return 0;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic int max6639_probe(struct i2c_client *client)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
5228c2ecf20Sopenharmony_ci	struct max6639_data *data;
5238c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
5248c2ecf20Sopenharmony_ci	int err;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
5278c2ecf20Sopenharmony_ci	if (!data)
5288c2ecf20Sopenharmony_ci		return -ENOMEM;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	data->client = client;
5318c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Initialize the max6639 chip */
5348c2ecf20Sopenharmony_ci	err = max6639_init_client(client, data);
5358c2ecf20Sopenharmony_ci	if (err < 0)
5368c2ecf20Sopenharmony_ci		return err;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
5398c2ecf20Sopenharmony_ci							   data,
5408c2ecf20Sopenharmony_ci							   max6639_groups);
5418c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
5458c2ecf20Sopenharmony_cistatic int max6639_suspend(struct device *dev)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
5488c2ecf20Sopenharmony_ci	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
5498c2ecf20Sopenharmony_ci	if (data < 0)
5508c2ecf20Sopenharmony_ci		return data;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client,
5538c2ecf20Sopenharmony_ci			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic int max6639_resume(struct device *dev)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
5598c2ecf20Sopenharmony_ci	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
5608c2ecf20Sopenharmony_ci	if (data < 0)
5618c2ecf20Sopenharmony_ci		return data;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client,
5648c2ecf20Sopenharmony_ci			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
5658c2ecf20Sopenharmony_ci}
5668c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic const struct i2c_device_id max6639_id[] = {
5698c2ecf20Sopenharmony_ci	{"max6639", 0},
5708c2ecf20Sopenharmony_ci	{ }
5718c2ecf20Sopenharmony_ci};
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max6639_id);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_cistatic struct i2c_driver max6639_driver = {
5788c2ecf20Sopenharmony_ci	.class = I2C_CLASS_HWMON,
5798c2ecf20Sopenharmony_ci	.driver = {
5808c2ecf20Sopenharmony_ci		   .name = "max6639",
5818c2ecf20Sopenharmony_ci		   .pm = &max6639_pm_ops,
5828c2ecf20Sopenharmony_ci		   },
5838c2ecf20Sopenharmony_ci	.probe_new = max6639_probe,
5848c2ecf20Sopenharmony_ci	.id_table = max6639_id,
5858c2ecf20Sopenharmony_ci	.detect = max6639_detect,
5868c2ecf20Sopenharmony_ci	.address_list = normal_i2c,
5878c2ecf20Sopenharmony_ci};
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_cimodule_i2c_driver(max6639_driver);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ciMODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
5928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("max6639 driver");
5938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
594