18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * tc654.c - Linux kernel modules for fan speed controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Allied Telesis Labs NZ
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/bitops.h>
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
118c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/mutex.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/util_macros.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cienum tc654_regs {
218c2ecf20Sopenharmony_ci	TC654_REG_RPM1 = 0x00,	/* RPM Output 1 */
228c2ecf20Sopenharmony_ci	TC654_REG_RPM2 = 0x01,	/* RPM Output 2 */
238c2ecf20Sopenharmony_ci	TC654_REG_FAN_FAULT1 = 0x02,	/* Fan Fault 1 Threshold */
248c2ecf20Sopenharmony_ci	TC654_REG_FAN_FAULT2 = 0x03,	/* Fan Fault 2 Threshold */
258c2ecf20Sopenharmony_ci	TC654_REG_CONFIG = 0x04,	/* Configuration */
268c2ecf20Sopenharmony_ci	TC654_REG_STATUS = 0x05,	/* Status */
278c2ecf20Sopenharmony_ci	TC654_REG_DUTY_CYCLE = 0x06,	/* Fan Speed Duty Cycle */
288c2ecf20Sopenharmony_ci	TC654_REG_MFR_ID = 0x07,	/* Manufacturer Identification */
298c2ecf20Sopenharmony_ci	TC654_REG_VER_ID = 0x08,	/* Version Identification */
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Macros to easily index the registers */
338c2ecf20Sopenharmony_ci#define TC654_REG_RPM(idx)		(TC654_REG_RPM1 + (idx))
348c2ecf20Sopenharmony_ci#define TC654_REG_FAN_FAULT(idx)	(TC654_REG_FAN_FAULT1 + (idx))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Config register bits */
378c2ecf20Sopenharmony_ci#define TC654_REG_CONFIG_RES		BIT(6)	/* Resolution Selection */
388c2ecf20Sopenharmony_ci#define TC654_REG_CONFIG_DUTYC		BIT(5)	/* Duty Cycle Control */
398c2ecf20Sopenharmony_ci#define TC654_REG_CONFIG_SDM		BIT(0)	/* Shutdown Mode */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Status register bits */
428c2ecf20Sopenharmony_ci#define TC654_REG_STATUS_F2F		BIT(1)	/* Fan 2 Fault */
438c2ecf20Sopenharmony_ci#define TC654_REG_STATUS_F1F		BIT(0)	/* Fan 1 Fault */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* RPM resolution for RPM Output registers */
468c2ecf20Sopenharmony_ci#define TC654_HIGH_RPM_RESOLUTION	25	/* 25 RPM resolution */
478c2ecf20Sopenharmony_ci#define TC654_LOW_RPM_RESOLUTION	50	/* 50 RPM resolution */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Convert to the fan fault RPM threshold from register value */
508c2ecf20Sopenharmony_ci#define TC654_FAN_FAULT_FROM_REG(val)	((val) * 50)	/* 50 RPM resolution */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* Convert to register value from the fan fault RPM threshold */
538c2ecf20Sopenharmony_ci#define TC654_FAN_FAULT_TO_REG(val)	(((val) / 50) & 0xff)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Register data is read (and cached) at most once per second. */
568c2ecf20Sopenharmony_ci#define TC654_UPDATE_INTERVAL		HZ
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct tc654_data {
598c2ecf20Sopenharmony_ci	struct i2c_client *client;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	/* update mutex */
628c2ecf20Sopenharmony_ci	struct mutex update_lock;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* tc654 register cache */
658c2ecf20Sopenharmony_ci	bool valid;
668c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* in jiffies */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	u8 rpm_output[2];	/* The fan RPM data for fans 1 and 2 is then
698c2ecf20Sopenharmony_ci				 * written to registers RPM1 and RPM2
708c2ecf20Sopenharmony_ci				 */
718c2ecf20Sopenharmony_ci	u8 fan_fault[2];	/* The Fan Fault Threshold Registers are used to
728c2ecf20Sopenharmony_ci				 * set the fan fault threshold levels for fan 1
738c2ecf20Sopenharmony_ci				 * and fan 2
748c2ecf20Sopenharmony_ci				 */
758c2ecf20Sopenharmony_ci	u8 config;	/* The Configuration Register is an 8-bit read/
768c2ecf20Sopenharmony_ci			 * writable multi-function control register
778c2ecf20Sopenharmony_ci			 *   7: Fan Fault Clear
788c2ecf20Sopenharmony_ci			 *      1 = Clear Fan Fault
798c2ecf20Sopenharmony_ci			 *      0 = Normal Operation (default)
808c2ecf20Sopenharmony_ci			 *   6: Resolution Selection for RPM Output Registers
818c2ecf20Sopenharmony_ci			 *      RPM Output Registers (RPM1 and RPM2) will be
828c2ecf20Sopenharmony_ci			 *      set for
838c2ecf20Sopenharmony_ci			 *      1 = 25 RPM (9-bit) resolution
848c2ecf20Sopenharmony_ci			 *      0 = 50 RPM (8-bit) resolution (default)
858c2ecf20Sopenharmony_ci			 *   5: Duty Cycle Control Method
868c2ecf20Sopenharmony_ci			 *      The V OUT duty cycle will be controlled via
878c2ecf20Sopenharmony_ci			 *      1 = the SMBus interface.
888c2ecf20Sopenharmony_ci			 *      0 = via the V IN analog input pin. (default)
898c2ecf20Sopenharmony_ci			 * 4,3: Fan 2 Pulses Per Rotation
908c2ecf20Sopenharmony_ci			 *      00 = 1
918c2ecf20Sopenharmony_ci			 *      01 = 2 (default)
928c2ecf20Sopenharmony_ci			 *      10 = 4
938c2ecf20Sopenharmony_ci			 *      11 = 8
948c2ecf20Sopenharmony_ci			 * 2,1: Fan 1 Pulses Per Rotation
958c2ecf20Sopenharmony_ci			 *      00 = 1
968c2ecf20Sopenharmony_ci			 *      01 = 2 (default)
978c2ecf20Sopenharmony_ci			 *      10 = 4
988c2ecf20Sopenharmony_ci			 *      11 = 8
998c2ecf20Sopenharmony_ci			 *   0: Shutdown Mode
1008c2ecf20Sopenharmony_ci			 *      1 = Shutdown mode.
1018c2ecf20Sopenharmony_ci			 *      0 = Normal operation. (default)
1028c2ecf20Sopenharmony_ci			 */
1038c2ecf20Sopenharmony_ci	u8 status;	/* The Status register provides all the information
1048c2ecf20Sopenharmony_ci			 * about what is going on within the TC654/TC655
1058c2ecf20Sopenharmony_ci			 * devices.
1068c2ecf20Sopenharmony_ci			 * 7,6: Unimplemented, Read as '0'
1078c2ecf20Sopenharmony_ci			 *   5: Over-Temperature Fault Condition
1088c2ecf20Sopenharmony_ci			 *      1 = Over-Temperature condition has occurred
1098c2ecf20Sopenharmony_ci			 *      0 = Normal operation. V IN is less than 2.6V
1108c2ecf20Sopenharmony_ci			 *   4: RPM2 Counter Overflow
1118c2ecf20Sopenharmony_ci			 *      1 = Fault condition
1128c2ecf20Sopenharmony_ci			 *      0 = Normal operation
1138c2ecf20Sopenharmony_ci			 *   3: RPM1 Counter Overflow
1148c2ecf20Sopenharmony_ci			 *      1 = Fault condition
1158c2ecf20Sopenharmony_ci			 *      0 = Normal operation
1168c2ecf20Sopenharmony_ci			 *   2: V IN Input Status
1178c2ecf20Sopenharmony_ci			 *      1 = V IN is open
1188c2ecf20Sopenharmony_ci			 *      0 = Normal operation. voltage present at V IN
1198c2ecf20Sopenharmony_ci			 *   1: Fan 2 Fault
1208c2ecf20Sopenharmony_ci			 *      1 = Fault condition
1218c2ecf20Sopenharmony_ci			 *      0 = Normal operation
1228c2ecf20Sopenharmony_ci			 *   0: Fan 1 Fault
1238c2ecf20Sopenharmony_ci			 *      1 = Fault condition
1248c2ecf20Sopenharmony_ci			 *      0 = Normal operation
1258c2ecf20Sopenharmony_ci			 */
1268c2ecf20Sopenharmony_ci	u8 duty_cycle;	/* The DUTY_CYCLE register is a 4-bit read/
1278c2ecf20Sopenharmony_ci			 * writable register used to control the duty
1288c2ecf20Sopenharmony_ci			 * cycle of the V OUT output.
1298c2ecf20Sopenharmony_ci			 */
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* helper to grab and cache data, at most one time per second */
1338c2ecf20Sopenharmony_cistatic struct tc654_data *tc654_update_client(struct device *dev)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct tc654_data *data = dev_get_drvdata(dev);
1368c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1378c2ecf20Sopenharmony_ci	int ret = 0;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1408c2ecf20Sopenharmony_ci	if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
1418c2ecf20Sopenharmony_ci	    likely(data->valid))
1428c2ecf20Sopenharmony_ci		goto out;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
1458c2ecf20Sopenharmony_ci	if (ret < 0)
1468c2ecf20Sopenharmony_ci		goto out;
1478c2ecf20Sopenharmony_ci	data->rpm_output[0] = ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
1508c2ecf20Sopenharmony_ci	if (ret < 0)
1518c2ecf20Sopenharmony_ci		goto out;
1528c2ecf20Sopenharmony_ci	data->rpm_output[1] = ret;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
1558c2ecf20Sopenharmony_ci	if (ret < 0)
1568c2ecf20Sopenharmony_ci		goto out;
1578c2ecf20Sopenharmony_ci	data->fan_fault[0] = ret;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
1608c2ecf20Sopenharmony_ci	if (ret < 0)
1618c2ecf20Sopenharmony_ci		goto out;
1628c2ecf20Sopenharmony_ci	data->fan_fault[1] = ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
1658c2ecf20Sopenharmony_ci	if (ret < 0)
1668c2ecf20Sopenharmony_ci		goto out;
1678c2ecf20Sopenharmony_ci	data->config = ret;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
1708c2ecf20Sopenharmony_ci	if (ret < 0)
1718c2ecf20Sopenharmony_ci		goto out;
1728c2ecf20Sopenharmony_ci	data->status = ret;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
1758c2ecf20Sopenharmony_ci	if (ret < 0)
1768c2ecf20Sopenharmony_ci		goto out;
1778c2ecf20Sopenharmony_ci	data->duty_cycle = ret & 0x0f;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	data->last_updated = jiffies;
1808c2ecf20Sopenharmony_ci	data->valid = true;
1818c2ecf20Sopenharmony_ciout:
1828c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (ret < 0)		/* upon error, encode it in return value */
1858c2ecf20Sopenharmony_ci		data = ERR_PTR(ret);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return data;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/*
1918c2ecf20Sopenharmony_ci * sysfs attributes
1928c2ecf20Sopenharmony_ci */
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic ssize_t fan_show(struct device *dev, struct device_attribute *da,
1958c2ecf20Sopenharmony_ci			char *buf)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(da)->index;
1988c2ecf20Sopenharmony_ci	struct tc654_data *data = tc654_update_client(dev);
1998c2ecf20Sopenharmony_ci	int val;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2028c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (data->config & TC654_REG_CONFIG_RES)
2058c2ecf20Sopenharmony_ci		val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
2068c2ecf20Sopenharmony_ci	else
2078c2ecf20Sopenharmony_ci		val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", val);
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
2138c2ecf20Sopenharmony_ci			    char *buf)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(da)->index;
2168c2ecf20Sopenharmony_ci	struct tc654_data *data = tc654_update_client(dev);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2198c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
2228c2ecf20Sopenharmony_ci		       TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
2268c2ecf20Sopenharmony_ci			     const char *buf, size_t count)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(da)->index;
2298c2ecf20Sopenharmony_ci	struct tc654_data *data = dev_get_drvdata(dev);
2308c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2318c2ecf20Sopenharmony_ci	unsigned long val;
2328c2ecf20Sopenharmony_ci	int ret;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
2358c2ecf20Sopenharmony_ci		return -EINVAL;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	val = clamp_val(val, 0, 12750);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	data->fan_fault[nr] = TC654_FAN_FAULT_TO_REG(val);
2428c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, TC654_REG_FAN_FAULT(nr),
2438c2ecf20Sopenharmony_ci					data->fan_fault[nr]);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2468c2ecf20Sopenharmony_ci	return ret < 0 ? ret : count;
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
2508c2ecf20Sopenharmony_ci			      char *buf)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(da)->index;
2538c2ecf20Sopenharmony_ci	struct tc654_data *data = tc654_update_client(dev);
2548c2ecf20Sopenharmony_ci	int val;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2578c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	if (nr == 0)
2608c2ecf20Sopenharmony_ci		val = !!(data->status & TC654_REG_STATUS_F1F);
2618c2ecf20Sopenharmony_ci	else
2628c2ecf20Sopenharmony_ci		val = !!(data->status & TC654_REG_STATUS_F2F);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", val);
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic ssize_t fan_pulses_show(struct device *dev,
2708c2ecf20Sopenharmony_ci			       struct device_attribute *da, char *buf)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(da)->index;
2738c2ecf20Sopenharmony_ci	struct tc654_data *data = tc654_update_client(dev);
2748c2ecf20Sopenharmony_ci	u8 val;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (IS_ERR(data))
2778c2ecf20Sopenharmony_ci		return PTR_ERR(data);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
2808c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", val);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic ssize_t fan_pulses_store(struct device *dev,
2848c2ecf20Sopenharmony_ci				struct device_attribute *da, const char *buf,
2858c2ecf20Sopenharmony_ci				size_t count)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(da)->index;
2888c2ecf20Sopenharmony_ci	struct tc654_data *data = dev_get_drvdata(dev);
2898c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2908c2ecf20Sopenharmony_ci	u8 config;
2918c2ecf20Sopenharmony_ci	unsigned long val;
2928c2ecf20Sopenharmony_ci	int ret;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
2958c2ecf20Sopenharmony_ci		return -EINVAL;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	switch (val) {
2988c2ecf20Sopenharmony_ci	case 1:
2998c2ecf20Sopenharmony_ci		config = 0;
3008c2ecf20Sopenharmony_ci		break;
3018c2ecf20Sopenharmony_ci	case 2:
3028c2ecf20Sopenharmony_ci		config = 1;
3038c2ecf20Sopenharmony_ci		break;
3048c2ecf20Sopenharmony_ci	case 4:
3058c2ecf20Sopenharmony_ci		config = 2;
3068c2ecf20Sopenharmony_ci		break;
3078c2ecf20Sopenharmony_ci	case 8:
3088c2ecf20Sopenharmony_ci		config = 3;
3098c2ecf20Sopenharmony_ci		break;
3108c2ecf20Sopenharmony_ci	default:
3118c2ecf20Sopenharmony_ci		return -EINVAL;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	data->config &= ~(0x03 << TC654_FAN_PULSE_SHIFT[nr]);
3178c2ecf20Sopenharmony_ci	data->config |= (config << TC654_FAN_PULSE_SHIFT[nr]);
3188c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3218c2ecf20Sopenharmony_ci	return ret < 0 ? ret : count;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
3258c2ecf20Sopenharmony_ci			     char *buf)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	struct tc654_data *data = tc654_update_client(dev);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3308c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
3368c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct tc654_data *data = dev_get_drvdata(dev);
3398c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3408c2ecf20Sopenharmony_ci	unsigned long val;
3418c2ecf20Sopenharmony_ci	int ret;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
3448c2ecf20Sopenharmony_ci		return -EINVAL;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (val != 0 && val != 1)
3478c2ecf20Sopenharmony_ci		return -EINVAL;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (val)
3528c2ecf20Sopenharmony_ci		data->config |= TC654_REG_CONFIG_DUTYC;
3538c2ecf20Sopenharmony_ci	else
3548c2ecf20Sopenharmony_ci		data->config &= ~TC654_REG_CONFIG_DUTYC;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3598c2ecf20Sopenharmony_ci	return ret < 0 ? ret : count;
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic const int tc654_pwm_map[16] = { 77,  88, 102, 112, 124, 136, 148, 160,
3638c2ecf20Sopenharmony_ci				      172, 184, 196, 207, 219, 231, 243, 255};
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic ssize_t pwm_show(struct device *dev, struct device_attribute *da,
3668c2ecf20Sopenharmony_ci			char *buf)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	struct tc654_data *data = tc654_update_client(dev);
3698c2ecf20Sopenharmony_ci	int pwm;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (IS_ERR(data))
3728c2ecf20Sopenharmony_ci		return PTR_ERR(data);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (data->config & TC654_REG_CONFIG_SDM)
3758c2ecf20Sopenharmony_ci		pwm = 0;
3768c2ecf20Sopenharmony_ci	else
3778c2ecf20Sopenharmony_ci		pwm = tc654_pwm_map[data->duty_cycle];
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", pwm);
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic ssize_t pwm_store(struct device *dev, struct device_attribute *da,
3838c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	struct tc654_data *data = dev_get_drvdata(dev);
3868c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3878c2ecf20Sopenharmony_ci	unsigned long val;
3888c2ecf20Sopenharmony_ci	int ret;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &val))
3918c2ecf20Sopenharmony_ci		return -EINVAL;
3928c2ecf20Sopenharmony_ci	if (val > 255)
3938c2ecf20Sopenharmony_ci		return -EINVAL;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (val == 0)
3988c2ecf20Sopenharmony_ci		data->config |= TC654_REG_CONFIG_SDM;
3998c2ecf20Sopenharmony_ci	else
4008c2ecf20Sopenharmony_ci		data->config &= ~TC654_REG_CONFIG_SDM;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	data->duty_cycle = find_closest(val, tc654_pwm_map,
4038c2ecf20Sopenharmony_ci					ARRAY_SIZE(tc654_pwm_map));
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
4068c2ecf20Sopenharmony_ci	if (ret < 0)
4078c2ecf20Sopenharmony_ci		goto out;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
4108c2ecf20Sopenharmony_ci					data->duty_cycle);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ciout:
4138c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4148c2ecf20Sopenharmony_ci	return ret < 0 ? ret : count;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
4188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
4198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
4208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
4218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
4228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
4238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
4248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
4258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
4268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci/* Driver data */
4298c2ecf20Sopenharmony_cistatic struct attribute *tc654_attrs[] = {
4308c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
4318c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
4328c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_min.dev_attr.attr,
4338c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_min.dev_attr.attr,
4348c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
4358c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
4368c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_pulses.dev_attr.attr,
4378c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_pulses.dev_attr.attr,
4388c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
4398c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1.dev_attr.attr,
4408c2ecf20Sopenharmony_ci	NULL
4418c2ecf20Sopenharmony_ci};
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(tc654);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci/*
4468c2ecf20Sopenharmony_ci * device probe and removal
4478c2ecf20Sopenharmony_ci */
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic int tc654_probe(struct i2c_client *client)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
4528c2ecf20Sopenharmony_ci	struct tc654_data *data;
4538c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
4548c2ecf20Sopenharmony_ci	int ret;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
4578c2ecf20Sopenharmony_ci		return -ENODEV;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
4608c2ecf20Sopenharmony_ci	if (!data)
4618c2ecf20Sopenharmony_ci		return -ENOMEM;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	data->client = client;
4648c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
4678c2ecf20Sopenharmony_ci	if (ret < 0)
4688c2ecf20Sopenharmony_ci		return ret;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	data->config = ret;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	hwmon_dev =
4738c2ecf20Sopenharmony_ci	    devm_hwmon_device_register_with_groups(dev, client->name, data,
4748c2ecf20Sopenharmony_ci						   tc654_groups);
4758c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic const struct i2c_device_id tc654_id[] = {
4798c2ecf20Sopenharmony_ci	{"tc654", 0},
4808c2ecf20Sopenharmony_ci	{"tc655", 0},
4818c2ecf20Sopenharmony_ci	{}
4828c2ecf20Sopenharmony_ci};
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tc654_id);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic struct i2c_driver tc654_driver = {
4878c2ecf20Sopenharmony_ci	.driver = {
4888c2ecf20Sopenharmony_ci		   .name = "tc654",
4898c2ecf20Sopenharmony_ci		   },
4908c2ecf20Sopenharmony_ci	.probe_new = tc654_probe,
4918c2ecf20Sopenharmony_ci	.id_table = tc654_id,
4928c2ecf20Sopenharmony_ci};
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cimodule_i2c_driver(tc654_driver);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ciMODULE_AUTHOR("Allied Telesis Labs");
4978c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Microchip TC654/TC655 driver");
4988c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
499