^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) * tc654.c - Linux kernel modules for fan speed controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016 Allied Telesis Labs NZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/util_macros.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum tc654_regs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) TC654_REG_RPM1 = 0x00, /* RPM Output 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) TC654_REG_RPM2 = 0x01, /* RPM Output 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) TC654_REG_FAN_FAULT1 = 0x02, /* Fan Fault 1 Threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) TC654_REG_FAN_FAULT2 = 0x03, /* Fan Fault 2 Threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) TC654_REG_CONFIG = 0x04, /* Configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) TC654_REG_STATUS = 0x05, /* Status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) TC654_REG_DUTY_CYCLE = 0x06, /* Fan Speed Duty Cycle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) TC654_REG_MFR_ID = 0x07, /* Manufacturer Identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) TC654_REG_VER_ID = 0x08, /* Version Identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Macros to easily index the registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define TC654_REG_RPM(idx) (TC654_REG_RPM1 + (idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define TC654_REG_FAN_FAULT(idx) (TC654_REG_FAN_FAULT1 + (idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Config register bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define TC654_REG_CONFIG_RES BIT(6) /* Resolution Selection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define TC654_REG_CONFIG_DUTYC BIT(5) /* Duty Cycle Control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define TC654_REG_CONFIG_SDM BIT(0) /* Shutdown Mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Status register bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define TC654_REG_STATUS_F2F BIT(1) /* Fan 2 Fault */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define TC654_REG_STATUS_F1F BIT(0) /* Fan 1 Fault */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* RPM resolution for RPM Output registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define TC654_HIGH_RPM_RESOLUTION 25 /* 25 RPM resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define TC654_LOW_RPM_RESOLUTION 50 /* 50 RPM resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* Convert to the fan fault RPM threshold from register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define TC654_FAN_FAULT_FROM_REG(val) ((val) * 50) /* 50 RPM resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Convert to register value from the fan fault RPM threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Register data is read (and cached) at most once per second. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define TC654_UPDATE_INTERVAL HZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct tc654_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* update mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* tc654 register cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) bool valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 rpm_output[2]; /* The fan RPM data for fans 1 and 2 is then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * written to registers RPM1 and RPM2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u8 fan_fault[2]; /* The Fan Fault Threshold Registers are used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * set the fan fault threshold levels for fan 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * and fan 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u8 config; /* The Configuration Register is an 8-bit read/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * writable multi-function control register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * 7: Fan Fault Clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * 1 = Clear Fan Fault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * 0 = Normal Operation (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * 6: Resolution Selection for RPM Output Registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * RPM Output Registers (RPM1 and RPM2) will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * set for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * 1 = 25 RPM (9-bit) resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * 0 = 50 RPM (8-bit) resolution (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * 5: Duty Cycle Control Method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * The V OUT duty cycle will be controlled via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * 1 = the SMBus interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * 0 = via the V IN analog input pin. (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * 4,3: Fan 2 Pulses Per Rotation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * 00 = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * 01 = 2 (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * 10 = 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * 11 = 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * 2,1: Fan 1 Pulses Per Rotation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * 00 = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * 01 = 2 (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * 10 = 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * 11 = 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * 0: Shutdown Mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * 1 = Shutdown mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * 0 = Normal operation. (default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) u8 status; /* The Status register provides all the information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * about what is going on within the TC654/TC655
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * 7,6: Unimplemented, Read as '0'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * 5: Over-Temperature Fault Condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * 1 = Over-Temperature condition has occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * 0 = Normal operation. V IN is less than 2.6V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * 4: RPM2 Counter Overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * 1 = Fault condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * 0 = Normal operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * 3: RPM1 Counter Overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * 1 = Fault condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * 0 = Normal operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * 2: V IN Input Status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * 1 = V IN is open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * 0 = Normal operation. voltage present at V IN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * 1: Fan 2 Fault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * 1 = Fault condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * 0 = Normal operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * 0: Fan 1 Fault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * 1 = Fault condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * 0 = Normal operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) u8 duty_cycle; /* The DUTY_CYCLE register is a 4-bit read/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * writable register used to control the duty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * cycle of the V OUT output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* helper to grab and cache data, at most one time per second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static struct tc654_data *tc654_update_client(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct tc654_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) likely(data->valid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) data->rpm_output[0] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) data->rpm_output[1] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) data->fan_fault[0] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) data->fan_fault[1] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) data->config = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) data->status = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) data->duty_cycle = ret & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) data->valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ret < 0) /* upon error, encode it in return value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) data = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return data;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * sysfs attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static ssize_t fan_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int nr = to_sensor_dev_attr(da)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct tc654_data *data = tc654_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (data->config & TC654_REG_CONFIG_RES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return sprintf(buf, "%d\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int nr = to_sensor_dev_attr(da)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct tc654_data *data = tc654_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int nr = to_sensor_dev_attr(da)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct tc654_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (kstrtoul(buf, 10, &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) val = clamp_val(val, 0, 12750);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) data->fan_fault[nr] = TC654_FAN_FAULT_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = i2c_smbus_write_byte_data(client, TC654_REG_FAN_FAULT(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) data->fan_fault[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret < 0 ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int nr = to_sensor_dev_attr(da)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct tc654_data *data = tc654_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) val = !!(data->status & TC654_REG_STATUS_F1F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) val = !!(data->status & TC654_REG_STATUS_F2F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return sprintf(buf, "%d\n", val);
^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) static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static ssize_t fan_pulses_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct device_attribute *da, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int nr = to_sensor_dev_attr(da)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct tc654_data *data = tc654_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return sprintf(buf, "%d\n", val);
^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) static ssize_t fan_pulses_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct device_attribute *da, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 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(da)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct tc654_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) u8 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (kstrtoul(buf, 10, &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) config = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) config = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) config = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) config = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) data->config &= ~(0x03 << TC654_FAN_PULSE_SHIFT[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) data->config |= (config << TC654_FAN_PULSE_SHIFT[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return ret < 0 ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct tc654_data *data = tc654_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct tc654_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (kstrtoul(buf, 10, &val))
^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) if (val != 0 && val != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) data->config |= TC654_REG_CONFIG_DUTYC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) data->config &= ~TC654_REG_CONFIG_DUTYC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return ret < 0 ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static const int tc654_pwm_map[16] = { 77, 88, 102, 112, 124, 136, 148, 160,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 172, 184, 196, 207, 219, 231, 243, 255};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static ssize_t pwm_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct tc654_data *data = tc654_update_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (data->config & TC654_REG_CONFIG_SDM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) pwm = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) pwm = tc654_pwm_map[data->duty_cycle];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return sprintf(buf, "%d\n", pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct tc654_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (kstrtoul(buf, 10, &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (val > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) data->config |= TC654_REG_CONFIG_SDM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) data->config &= ~TC654_REG_CONFIG_SDM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) data->duty_cycle = find_closest(val, tc654_pwm_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ARRAY_SIZE(tc654_pwm_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) data->duty_cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return ret < 0 ? ret : count;
^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) static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) /* Driver data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static struct attribute *tc654_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) &sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) &sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) &sensor_dev_attr_fan1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) &sensor_dev_attr_fan2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) &sensor_dev_attr_fan1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) &sensor_dev_attr_fan2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) &sensor_dev_attr_fan1_pulses.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) &sensor_dev_attr_fan2_pulses.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) &sensor_dev_attr_pwm1_mode.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) &sensor_dev_attr_pwm1.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ATTRIBUTE_GROUPS(tc654);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * device probe and removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static int tc654_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct tc654_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) data->config = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) hwmon_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) devm_hwmon_device_register_with_groups(dev, client->name, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) tc654_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static const struct i2c_device_id tc654_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {"tc654", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {"tc655", 0},
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) MODULE_DEVICE_TABLE(i2c, tc654_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static struct i2c_driver tc654_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) .name = "tc654",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) .probe_new = tc654_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) .id_table = tc654_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) module_i2c_driver(tc654_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) MODULE_AUTHOR("Allied Telesis Labs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) MODULE_LICENSE("GPL");