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)  * emc6w201.c - Hardware monitoring driver for the SMSC EMC6W201
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2011  Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Addresses to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * The EMC6W201 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define EMC6W201_REG_IN(nr)		(0x20 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define EMC6W201_REG_TEMP(nr)		(0x26 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define EMC6W201_REG_FAN(nr)		(0x2C + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define EMC6W201_REG_COMPANY		0x3E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define EMC6W201_REG_VERSTEP		0x3F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define EMC6W201_REG_CONFIG		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define EMC6W201_REG_IN_LOW(nr)		(0x4A + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define EMC6W201_REG_IN_HIGH(nr)	(0x4B + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define EMC6W201_REG_TEMP_LOW(nr)	(0x56 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define EMC6W201_REG_TEMP_HIGH(nr)	(0x57 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define EMC6W201_REG_FAN_MIN(nr)	(0x62 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) enum subfeature { input, min, max };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * Per-device data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct emc6w201_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	/* registers values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u8 in[3][6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	s8 temp[3][6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u16 fan[2][5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * Combine LSB and MSB registers in a single value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * Locking: must be called with data->update_lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static u16 emc6w201_read16(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int lsb, msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	lsb = i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	msb = i2c_smbus_read_byte_data(client, reg + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (unlikely(lsb < 0 || msb < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			16, "read", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return 0xFFFF;	/* Arbitrary value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return (msb << 8) | lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * Write 16-bit value to LSB and MSB registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * Locking: must be called with data->update_lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static int emc6w201_write16(struct i2c_client *client, u8 reg, u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	err = i2c_smbus_write_byte_data(client, reg, val & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (likely(!err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		err = i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (unlikely(err < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			16, "write", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /* Read 8-bit value from register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static u8 emc6w201_read8(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	val = i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (unlikely(val < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			8, "read", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return 0x00;	/* Arbitrary value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Write 8-bit value to register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int emc6w201_write8(struct i2c_client *client, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	err = i2c_smbus_write_byte_data(client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (unlikely(err < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			8, "write", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static struct emc6w201_data *emc6w201_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct emc6w201_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		for (nr = 0; nr < 6; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			data->in[input][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				emc6w201_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 						EMC6W201_REG_IN(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			data->in[min][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 				emc6w201_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 						EMC6W201_REG_IN_LOW(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			data->in[max][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				emc6w201_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 						EMC6W201_REG_IN_HIGH(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		for (nr = 0; nr < 6; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			data->temp[input][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				emc6w201_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 						EMC6W201_REG_TEMP(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			data->temp[min][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				emc6w201_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 						EMC6W201_REG_TEMP_LOW(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			data->temp[max][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				emc6w201_read8(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 						EMC6W201_REG_TEMP_HIGH(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		for (nr = 0; nr < 5; nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			data->fan[input][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				emc6w201_read16(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 						EMC6W201_REG_FAN(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			data->fan[min][nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				emc6w201_read16(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 						EMC6W201_REG_FAN_MIN(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * Sysfs callback functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static const s16 nominal_mv[6] = { 2500, 1500, 3300, 5000, 1500, 1500 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static ssize_t in_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct emc6w201_data *data = emc6w201_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int sf = to_sensor_dev_attr_2(devattr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int nr = to_sensor_dev_attr_2(devattr)->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return sprintf(buf, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		       (unsigned)data->in[sf][nr] * nominal_mv[nr] / 0xC0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static ssize_t in_store(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct emc6w201_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int sf = to_sensor_dev_attr_2(devattr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	int nr = to_sensor_dev_attr_2(devattr)->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	val = clamp_val(val, 0, 255 * nominal_mv[nr] / 192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	val = DIV_ROUND_CLOSEST(val * 192, nominal_mv[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			  : EMC6W201_REG_IN_HIGH(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	data->in[sf][nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	err = emc6w201_write8(client, reg, data->in[sf][nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return err < 0 ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct emc6w201_data *data = emc6w201_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int sf = to_sensor_dev_attr_2(devattr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int nr = to_sensor_dev_attr_2(devattr)->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return sprintf(buf, "%d\n", (int)data->temp[sf][nr] * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static ssize_t temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			  struct device_attribute *devattr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct emc6w201_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	int sf = to_sensor_dev_attr_2(devattr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int nr = to_sensor_dev_attr_2(devattr)->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	val = clamp_val(val, -127000, 127000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	val = DIV_ROUND_CLOSEST(val, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			  : EMC6W201_REG_TEMP_HIGH(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	data->temp[sf][nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	err = emc6w201_write8(client, reg, data->temp[sf][nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return err < 0 ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct emc6w201_data *data = emc6w201_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int sf = to_sensor_dev_attr_2(devattr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int nr = to_sensor_dev_attr_2(devattr)->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	unsigned rpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (data->fan[sf][nr] == 0 || data->fan[sf][nr] == 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		rpm = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		rpm = 5400000U / data->fan[sf][nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return sprintf(buf, "%u\n", rpm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static ssize_t fan_store(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct emc6w201_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	int sf = to_sensor_dev_attr_2(devattr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int nr = to_sensor_dev_attr_2(devattr)->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		val = 0xFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		val = DIV_ROUND_CLOSEST(5400000U, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		val = clamp_val(val, 0, 0xFFFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	data->fan[sf][nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	err = emc6w201_write16(client, EMC6W201_REG_FAN_MIN(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			       data->fan[sf][nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	return err < 0 ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, 1, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, 1, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, 5, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, 5, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, 5, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 1, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 2, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 2, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 3, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 3, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 4, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 4, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 5, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static SENSOR_DEVICE_ATTR_2_RW(temp6_min, temp, 5, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, 0, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, 0, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, 1, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, 1, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static SENSOR_DEVICE_ATTR_2_RO(fan3_input, fan, 2, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan, 2, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static SENSOR_DEVICE_ATTR_2_RO(fan4_input, fan, 3, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static SENSOR_DEVICE_ATTR_2_RW(fan4_min, fan, 3, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static SENSOR_DEVICE_ATTR_2_RO(fan5_input, fan, 4, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static SENSOR_DEVICE_ATTR_2_RW(fan5_min, fan, 4, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static struct attribute *emc6w201_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	&sensor_dev_attr_in0_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	&sensor_dev_attr_in0_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	&sensor_dev_attr_in0_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	&sensor_dev_attr_in1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	&sensor_dev_attr_in1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	&sensor_dev_attr_in2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	&sensor_dev_attr_in2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	&sensor_dev_attr_in3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	&sensor_dev_attr_in3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	&sensor_dev_attr_in3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	&sensor_dev_attr_in4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	&sensor_dev_attr_in4_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	&sensor_dev_attr_in4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	&sensor_dev_attr_in5_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	&sensor_dev_attr_in5_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	&sensor_dev_attr_in5_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	&sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	&sensor_dev_attr_temp2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	&sensor_dev_attr_temp3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	&sensor_dev_attr_temp3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	&sensor_dev_attr_temp3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	&sensor_dev_attr_temp4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	&sensor_dev_attr_temp4_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	&sensor_dev_attr_temp4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	&sensor_dev_attr_temp5_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	&sensor_dev_attr_temp5_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	&sensor_dev_attr_temp5_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	&sensor_dev_attr_temp6_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	&sensor_dev_attr_temp6_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	&sensor_dev_attr_temp6_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	&sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	&sensor_dev_attr_fan1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	&sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	&sensor_dev_attr_fan2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	&sensor_dev_attr_fan3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	&sensor_dev_attr_fan3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	&sensor_dev_attr_fan4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	&sensor_dev_attr_fan4_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	&sensor_dev_attr_fan5_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	&sensor_dev_attr_fan5_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ATTRIBUTE_GROUPS(emc6w201);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * Driver interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int emc6w201_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			   struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	int company, verstep, config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/* Identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	company = i2c_smbus_read_byte_data(client, EMC6W201_REG_COMPANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (company != 0x5C)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	verstep = i2c_smbus_read_byte_data(client, EMC6W201_REG_VERSTEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (verstep < 0 || (verstep & 0xF0) != 0xB0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if ((verstep & 0x0F) > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		dev_dbg(&client->dev, "Unknown EMC6W201 stepping %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			verstep & 0x0F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	/* Check configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	config = i2c_smbus_read_byte_data(client, EMC6W201_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (config < 0 || (config & 0xF4) != 0x04)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (!(config & 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		dev_err(&client->dev, "Monitoring not enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	strlcpy(info->type, "emc6w201", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static int emc6w201_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct emc6w201_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	data = devm_kzalloc(dev, sizeof(struct emc6w201_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 							   data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 							   emc6w201_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static const struct i2c_device_id emc6w201_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	{ "emc6w201", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) MODULE_DEVICE_TABLE(i2c, emc6w201_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static struct i2c_driver emc6w201_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		.name	= "emc6w201",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	.probe_new	= emc6w201_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	.id_table	= emc6w201_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	.detect		= emc6w201_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) module_i2c_driver(emc6w201_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) MODULE_DESCRIPTION("SMSC EMC6W201 hardware monitoring driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) MODULE_LICENSE("GPL");