Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * f75375s.c - driver for the Fintek F75375/SP, F75373 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *             F75387SG/RG hardware monitoring features
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2006-2007  Riku Voipio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Datasheets available at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * f75375:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * f75373:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * f75387:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/f75375s.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* Addresses to scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) enum chips { f75373, f75375, f75387 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /* Fintek F75375 registers  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define F75375_REG_CONFIG0		0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define F75375_REG_CONFIG1		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define F75375_REG_CONFIG2		0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define F75375_REG_CONFIG3		0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define F75375_REG_ADDR			0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define F75375_REG_INTR			0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define F75375_CHIP_ID			0x5A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define F75375_REG_VERSION		0x5C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define F75375_REG_VENDOR		0x5D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define F75375_REG_FAN_TIMER		0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define F75375_REG_VOLT(nr)		(0x10 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define F75375_REG_VOLT_HIGH(nr)	(0x20 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define F75375_REG_VOLT_LOW(nr)		(0x21 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define F75375_REG_TEMP(nr)		(0x14 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define F75387_REG_TEMP11_LSB(nr)	(0x1a + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define F75375_REG_TEMP_HIGH(nr)	(0x28 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define F75375_REG_TEMP_HYST(nr)	(0x29 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define F75375_REG_FAN(nr)		(0x16 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define F75375_REG_FAN_MIN(nr)		(0x2C + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define F75375_REG_FAN_FULL(nr)		(0x70 + (nr) * 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define F75375_REG_FAN_PWM_DUTY(nr)	(0x76 + (nr) * 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define F75375_REG_FAN_PWM_CLOCK(nr)	(0x7D + (nr) * 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define F75375_REG_FAN_EXP(nr)		(0x74 + (nr) * 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define F75375_REG_FAN_B_TEMP(nr, step)	((0xA0 + (nr) * 0x10) + (step))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define F75375_REG_FAN_B_SPEED(nr, step) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	((0xA5 + (nr) * 0x10) + (step) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define F75375_REG_PWM1_RAISE_DUTY	0x69
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define F75375_REG_PWM2_RAISE_DUTY	0x6A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define F75375_REG_PWM1_DROP_DUTY	0x6B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define F75375_REG_PWM2_DROP_DUTY	0x6C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define F75375_FAN_CTRL_LINEAR(nr)	(4 + nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define F75387_FAN_CTRL_LINEAR(nr)	(1 + ((nr) * 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define FAN_CTRL_MODE(nr)		(4 + ((nr) * 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define F75387_FAN_DUTY_MODE(nr)	(2 + ((nr) * 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define F75387_FAN_MANU_MODE(nr)	((nr) * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * Data structures and manipulation thereof
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) struct f75375_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	unsigned short addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct mutex update_lock; /* protect register access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	char valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	unsigned long last_updated;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unsigned long last_limits;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/* Register values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u8 in[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u8 in_max[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u8 in_min[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u16 fan[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u16 fan_min[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u16 fan_max[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	u16 fan_target[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	u8 fan_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	u8 pwm[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	u8 pwm_mode[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	u8 pwm_enable[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * f75387: For remote temperature reading, it uses signed 11-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * values with LSB = 0.125 degree Celsius, left-justified in 16-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 * registers. For original 8-bit temp readings, the LSB just is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	s16 temp11[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	s8 temp_high[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	s8 temp_max_hyst[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int f75375_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			 struct i2c_board_info *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int f75375_probe(struct i2c_client *client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int f75375_remove(struct i2c_client *client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static const struct i2c_device_id f75375_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	{ "f75373", f75373 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	{ "f75375", f75375 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	{ "f75387", f75387 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) MODULE_DEVICE_TABLE(i2c, f75375_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static struct i2c_driver f75375_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	.class = I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		.name = "f75375",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	.probe_new = f75375_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	.remove = f75375_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.id_table = f75375_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.detect = f75375_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.address_list = normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static inline int f75375_read8(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* in most cases, should be called while holding update_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return (i2c_smbus_read_byte_data(client, reg) << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		| i2c_smbus_read_byte_data(client, reg + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static inline void f75375_write8(struct i2c_client *client, u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		u8 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	i2c_smbus_write_byte_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static inline void f75375_write16(struct i2c_client *client, u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void f75375_write_pwm(struct i2c_client *client, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (data->kind == f75387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		f75375_write16(client, F75375_REG_FAN_EXP(nr), data->pwm[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			      data->pwm[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static struct f75375_data *f75375_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/* Limit registers cache is refreshed after 60 seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (time_after(jiffies, data->last_limits + 60 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		|| !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		for (nr = 0; nr < 2; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			data->temp_high[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 				f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			data->temp_max_hyst[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				f75375_read8(client, F75375_REG_TEMP_HYST(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			data->fan_max[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 				f75375_read16(client, F75375_REG_FAN_FULL(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			data->fan_min[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				f75375_read16(client, F75375_REG_FAN_MIN(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			data->fan_target[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				f75375_read16(client, F75375_REG_FAN_EXP(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		for (nr = 0; nr < 4; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			data->in_max[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			data->in_min[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				f75375_read8(client, F75375_REG_VOLT_LOW(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		data->last_limits = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* Measurement registers cache is refreshed after 2 second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (time_after(jiffies, data->last_updated + 2 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		|| !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		for (nr = 0; nr < 2; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			data->pwm[nr] =	f75375_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				F75375_REG_FAN_PWM_DUTY(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			/* assign MSB, therefore shift it by 8 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			data->temp11[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				f75375_read8(client, F75375_REG_TEMP(nr)) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			if (data->kind == f75387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				/* merge F75387's temperature LSB (11-bit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 				data->temp11[nr] |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 					f75375_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 						     F75387_REG_TEMP11_LSB(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			data->fan[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				f75375_read16(client, F75375_REG_FAN(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		for (nr = 0; nr < 4; nr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			data->in[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				f75375_read8(client, F75375_REG_VOLT(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static inline u16 rpm_from_reg(u16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (reg == 0 || reg == 0xffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return 1500000 / reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static inline u16 rpm_to_reg(int rpm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (rpm < 367 || rpm > 0xffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return 1500000 / rpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static bool duty_mode_enabled(u8 pwm_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	switch (pwm_enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	case 0: /* Manual, duty mode (full speed) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	case 1: /* Manual, duty mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	case 4: /* Auto, duty mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	case 2: /* Auto, speed mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	case 3: /* Manual, speed mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static bool auto_mode_enabled(u8 pwm_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	switch (pwm_enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	case 0: /* Manual, duty mode (full speed) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	case 1: /* Manual, duty mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	case 3: /* Manual, speed mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	case 2: /* Auto, speed mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	case 4: /* Auto, duty mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	data->fan_min[nr] = rpm_to_reg(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (auto_mode_enabled(data->pwm_enable[nr]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (data->kind == f75387 && duty_mode_enabled(data->pwm_enable[nr]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	data->fan_target[nr] = rpm_to_reg(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (auto_mode_enabled(data->pwm_enable[nr]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	    !duty_mode_enabled(data->pwm_enable[nr]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	data->pwm[nr] = clamp_val(val, 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	f75375_write_pwm(client, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		*attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	return sprintf(buf, "%d\n", data->pwm_enable[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	u8 fanmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (val < 0 || val > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (data->kind == f75387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		/* For now, deny dangerous toggling of duty mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		if (duty_mode_enabled(data->pwm_enable[nr]) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				duty_mode_enabled(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		/* clear each fanX_mode bit before setting them properly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		case 0: /* full speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			data->pwm[nr] = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		case 1: /* PWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			fanmode  |= (1 << F75387_FAN_MANU_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			fanmode  |= (1 << F75387_FAN_DUTY_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		case 2: /* Automatic, speed mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		case 3: /* fan speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		case 4: /* Automatic, pwm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		/* clear each fanX_mode bit before setting them properly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		fanmode &= ~(3 << FAN_CTRL_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		case 0: /* full speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			fanmode  |= (3 << FAN_CTRL_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			data->pwm[nr] = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		case 1: /* PWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			fanmode  |= (3 << FAN_CTRL_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		case 2: /* AUTOMATIC*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			fanmode  |= (1 << FAN_CTRL_MODE(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		case 3: /* fan speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		case 4: /* Automatic pwm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	data->pwm_enable[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		f75375_write_pwm(client, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	err = set_pwm_enable_direct(client, nr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return err ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	u8 conf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	char reg, ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (!(val == 0 || val == 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	/* F75373 does not support DC (linear voltage) fan control mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (data->kind == f75373 && val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	/* take care for different registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	if (data->kind == f75387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		reg = F75375_REG_FAN_TIMER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		ctrl = F75387_FAN_CTRL_LINEAR(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		reg = F75375_REG_CONFIG1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		ctrl = F75375_FAN_CTRL_LINEAR(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	conf = f75375_read8(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	conf &= ~(1 << ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		conf |= (1 << ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	f75375_write8(client, reg, conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	data->pwm_mode[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static ssize_t show_pwm(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		*attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return sprintf(buf, "%d\n", data->pwm[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		*attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	return sprintf(buf, "%d\n", data->pwm_mode[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) #define VOLT_FROM_REG(val) ((val) * 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) #define VOLT_TO_REG(val) ((val) / 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static ssize_t show_in(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	data->in_max[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	data->in_min[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) #define TEMP_FROM_REG(val) ((val) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) #define TEMP_TO_REG(val) ((val) / 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static ssize_t show_temp_max_hyst(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	struct f75375_data *data = f75375_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	val = clamp_val(TEMP_TO_REG(val), 0, 127);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	data->temp_high[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static ssize_t set_temp_max_hyst(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	val = clamp_val(TEMP_TO_REG(val), 0, 127);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	data->temp_max_hyst[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	f75375_write8(client, F75375_REG_TEMP_HYST(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		data->temp_max_hyst[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) #define show_fan(thing) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			char *buf)\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) {\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	int nr = to_sensor_dev_attr(attr)->index;\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	struct f75375_data *data = f75375_update_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) show_fan(fan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) show_fan(fan_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) show_fan(fan_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) show_fan(fan_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	show_in_max, set_in_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	show_in_min, set_in_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	show_in_max, set_in_max, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	show_in_min, set_in_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	show_in_max, set_in_max, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	show_in_min, set_in_min, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	show_in_max, set_in_max, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	show_in_min, set_in_min, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	show_temp_max_hyst, set_temp_max_hyst, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	show_temp_max, set_temp_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	show_temp_max_hyst, set_temp_max_hyst, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	show_temp_max, set_temp_max, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	show_fan_min, set_fan_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	show_fan_target, set_fan_target, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	show_fan_min, set_fan_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	show_fan_target, set_fan_target, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	show_pwm, set_pwm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	show_pwm_enable, set_pwm_enable, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	show_pwm_mode, set_pwm_mode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	show_pwm, set_pwm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	show_pwm_enable, set_pwm_enable, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	show_pwm_mode, set_pwm_mode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static struct attribute *f75375_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	&sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	&sensor_dev_attr_fan1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	&sensor_dev_attr_fan1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	&sensor_dev_attr_fan1_target.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	&sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	&sensor_dev_attr_fan2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	&sensor_dev_attr_fan2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	&sensor_dev_attr_fan2_target.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	&sensor_dev_attr_pwm1.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	&sensor_dev_attr_pwm2.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	&sensor_dev_attr_pwm2_mode.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	&sensor_dev_attr_in0_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	&sensor_dev_attr_in0_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	&sensor_dev_attr_in0_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	&sensor_dev_attr_in1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	&sensor_dev_attr_in1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	&sensor_dev_attr_in2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	&sensor_dev_attr_in2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	&sensor_dev_attr_in3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	&sensor_dev_attr_in3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	&sensor_dev_attr_in3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) static const struct attribute_group f75375_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	.attrs = f75375_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) static void f75375_init(struct i2c_client *client, struct f75375_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		struct f75375s_platform_data *f75375s_pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	if (!f75375s_pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		u8 conf, mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		conf = f75375_read8(client, F75375_REG_CONFIG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		mode = f75375_read8(client, F75375_REG_FAN_TIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		for (nr = 0; nr < 2; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 			if (data->kind == f75387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 				bool manu, duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 				if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 					data->pwm_mode[nr] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 				manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 				duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 				if (!manu && duty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 					/* auto, pwm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 					data->pwm_enable[nr] = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 				else if (manu && !duty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 					/* manual, speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 					data->pwm_enable[nr] = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 				else if (!manu && !duty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 					/* automatic, speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 					data->pwm_enable[nr] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 					/* manual, pwm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 					data->pwm_enable[nr] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 				if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 					data->pwm_mode[nr] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 				switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 				case 0:		/* speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 					data->pwm_enable[nr] = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 				case 1:		/* automatic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 					data->pwm_enable[nr] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 				default:	/* manual */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 					data->pwm_enable[nr] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	for (nr = 0; nr < 2; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 		    !duty_mode_enabled(f75375s_pdata->pwm_enable[nr]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		data->pwm[nr] = clamp_val(f75375s_pdata->pwm[nr], 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		f75375_write_pwm(client, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) static int f75375_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	struct f75375_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	struct f75375s_platform_data *f75375s_pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 			dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 				I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	data = devm_kzalloc(&client->dev, sizeof(struct f75375_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	data->kind = i2c_match_id(f75375_id, client)->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	err = sysfs_create_group(&client->dev.kobj, &f75375_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	if (data->kind != f75373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 		err = sysfs_chmod_file(&client->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 			&sensor_dev_attr_pwm1_mode.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 			S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 			goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 		err = sysfs_chmod_file(&client->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 			&sensor_dev_attr_pwm2_mode.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 			S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 			goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	data->hwmon_dev = hwmon_device_register(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	if (IS_ERR(data->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 		err = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 		goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	f75375_init(client, data, f75375s_pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) exit_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	sysfs_remove_group(&client->dev.kobj, &f75375_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) static int f75375_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	struct f75375_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	sysfs_remove_group(&client->dev.kobj, &f75375_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) static int f75375_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 			 struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	u16 vendid, chipid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	u8 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	vendid = f75375_read16(client, F75375_REG_VENDOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	chipid = f75375_read16(client, F75375_CHIP_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	if (vendid != 0x1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	if (chipid == 0x0306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 		name = "f75375";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	else if (chipid == 0x0204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 		name = "f75373";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	else if (chipid == 0x0410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 		name = "f75387";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	version = f75375_read8(client, F75375_REG_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	strlcpy(info->type, name, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) module_i2c_driver(f75375_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) MODULE_AUTHOR("Riku Voipio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");