Skip to content

Commit

Permalink
add fan power monitor driver
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjingwu committed Apr 21, 2023
1 parent db6e488 commit 6e24242
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 114 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

#include "fan_interface.h"

struct fan_driver_clx {
struct fan_driver_clx
{
struct fan_fn_if fan_if;
//private
// private
void __iomem *fan_base;
};

#define FAN_BASE_ADDRESS (0x0300)
#define FAN_BASE_ADDRESS (0x0300)

//register define
#define FAN_VERSION_ADDR (0x4)
// register define
#define FAN_VERSION_ADDR (0x4)

enum hwmon_fan_offset
{
Expand Down Expand Up @@ -43,7 +44,7 @@ enum hwmon_fan_offset
FAN_AIR_DIRECTION_OFFSET = 0x14,
FAN_LED1_CONTROL_OFFSET = 0x15,
FAN_LED2_CONTROL_OFFSET = 0x16,
FAN_USB_EN_OFFSET =0x17,
FAN_USB_EN_OFFSET = 0x17,
FAN_EEPROM_SELECT_OFFSET = 0x20,
FAN_EEPROM_IIC_SPEED_OFFSET,
FAN_EEPROM_IIC_REG_OFFSET,
Expand All @@ -55,21 +56,23 @@ enum hwmon_fan_offset
FAN_EEPROM_IIC_STATUS_OFFSET
};

enum user_fan_led_state {
enum user_fan_led_state
{
USER_FAN_LED_DARK,
USER_FAN_LED_GREEN,
USER_FAN_LED_YELLOW,
USER_FAN_LED_RED,
USER_FAN_LED_NOT_SUPPORT
};
/*
*extract the value from FAN_LED2_CONTROL_OFFSET FAN_LED1_CONTROL_OFFSET, and mapping is as below
* 00 DARK
* 01 GREEN
* 10 RED
* 11 YELLOW
*/
enum dev_fan_led_state {
*extract the value from FAN_LED2_CONTROL_OFFSET FAN_LED1_CONTROL_OFFSET, and mapping is as below
* 00 DARK
* 01 GREEN
* 10 RED
* 11 YELLOW
*/
enum dev_fan_led_state
{
DEV_FAN_LED_DARK,
DEV_FAN_LED_GREEN,
DEV_FAN_LED_RED,
Expand All @@ -82,4 +85,16 @@ enum dev_fan_led_state {
#define FAN_EEPROM_TX_FINISH_MASK (0x80)
#define FAN_EEPROM_TX_ERROR_MASK (0x40)

#define FAN_VMON_CHIP_ADDR 0x30
#define FAN_VMON_VENDOR_ID_REG 0x00
#define FAN_VMON_STAT_REG 0x30
#define FAN_VMON_BANK_SEL_REG 0xf0
#define FAN_VMON_CTL_REG 0x10
#define FAN_VMON_MISC_REG 0x11
#define FAN_VMON_VIN_CH_EN_REG 0x1e
#define FAN_VMON_VRANGE_MULT_REG 0x1f
#define FAN_VMON_OFF_STAT_REG 0x32
#define FAN_VMON_VIN_LVL_BASE_REG 0x40
#define FAN_VMON_ACT_CPLD_REG 0x11

#endif //_DRV_FAN_CLX_H_
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ int32_t clx_i2c_write(int bus, int addr, int offset, uint8_t *buf, uint32_t size
}
EXPORT_SYMBOL_GPL(clx_i2c_write);

int32_t clx_i2c_write_word(int bus, int addr, int offset, uint16_t *buf)
{
int rv = DRIVER_ERR;
struct i2c_adapter *i2c_adap;
union i2c_smbus_data data;
uint8_t retris = I2C_RETRIES_TIMES;

i2c_adap = i2c_get_adapter(bus);
if (i2c_adap == NULL)
{
LOG_ERR(CLX_DRIVER_TYPES_PLT, "get i2c bus[%d] adapter fail\r\n", bus);
return DRIVER_ERR;
}

data.word = *buf;

while (retris)
{
rv = i2c_smbus_xfer(i2c_adap, addr, 0, I2C_SMBUS_WRITE, offset, I2C_SMBUS_WORD_DATA, &data);

if (rv < 0)
{
LOG_DBG(CLX_DRIVER_TYPES_PLT, "i2c dev[bus=%d addr=0x%x offset=0x%x] transfer fail, rv=%d\r\n", bus, addr, offset, rv);
rv = DRIVER_ERR;
usleep_range(3000, 3500);
retris--;
}
else
{
rv = DRIVER_OK;
break;
}
}

i2c_put_adapter(i2c_adap);
return rv;
}

EXPORT_SYMBOL_GPL(clx_i2c_write_word);

int32_t clx_i2c_mux_read(int bus, int addr, int offset, uint8_t *buf, uint32_t size)
{
int i, rv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "device_driver_common.h"
#include "fan_sysfs.h"
#include "fan_interface.h"

/********************************************fan**********************************************/
static ssize_t clx_get_fan_loglevel(char *buf, size_t count)
{
Expand All @@ -25,13 +24,16 @@ static ssize_t clx_set_fan_loglevel(const char *buf, size_t count)
int loglevel = 0;
unsigned int base = 16;

if (buf[1] == 'x') {
if (buf[1] == 'x')
{
base = 16;
}
else {
else
{
base = 10;
}
if (kstrtouint(buf, base, &loglevel)) {
if (kstrtouint(buf, base, &loglevel))
{
return -EINVAL;
}
g_dev_loglevel[CLX_DRIVER_TYPES_FAN] = loglevel;
Expand Down Expand Up @@ -78,7 +80,6 @@ static int clx_get_fan_number(void)
return fan_dev->get_fan_number(fan_dev);
}


static int clx_get_fan_motor_number(unsigned int fan_index)
{
struct fan_fn_if *fan_dev = get_fan();
Expand Down Expand Up @@ -272,6 +273,16 @@ static int clx_set_fan_led_status(unsigned int fan_index, int status)
return fan_dev->set_fan_led_status(fan_dev, fan_index, status);
}

static ssize_t clx_get_fan_vmon(unsigned int fan_index, char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

FAN_DEV_VALID(fan_dev);
FAN_DEV_VALID(fan_dev->get_fan_vmon);
FAN_INDEX_MAPPING(fan_index);
return fan_dev->get_fan_vmon(fan_dev, fan_index, buf, count);
}

/*
* clx_get_fan_direction - Used to get fan air flow direction,
* filled the value to buf, air flow direction define as below:
Expand Down Expand Up @@ -309,14 +320,14 @@ static ssize_t clx_get_fan_direction(unsigned int fan_index, char *buf, size_t c
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_get_fan_motor_speed(unsigned int fan_index, unsigned int motor_index,
char *buf, size_t count)
char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

FAN_DEV_VALID(fan_dev);
FAN_DEV_VALID(fan_dev->get_fan_motor_speed);
FAN_INDEX_MAPPING(fan_index);
return fan_dev->get_fan_motor_speed(fan_dev,fan_index, motor_index, buf, count);
return fan_dev->get_fan_motor_speed(fan_dev, fan_index, motor_index, buf, count);
}

/*
Expand All @@ -332,14 +343,14 @@ static ssize_t clx_get_fan_motor_speed(unsigned int fan_index, unsigned int moto
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_get_fan_motor_speed_tolerance(unsigned int fan_index, unsigned int motor_index,
char *buf, size_t count)
char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

FAN_DEV_VALID(fan_dev);
FAN_DEV_VALID(fan_dev->get_fan_motor_speed_tolerance);
FAN_INDEX_MAPPING(fan_index);
return fan_dev->get_fan_motor_speed_tolerance(fan_dev,fan_index, motor_index, buf, count);
return fan_dev->get_fan_motor_speed_tolerance(fan_dev, fan_index, motor_index, buf, count);
}

/*
Expand All @@ -355,14 +366,14 @@ static ssize_t clx_get_fan_motor_speed_tolerance(unsigned int fan_index, unsigne
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_get_fan_motor_speed_target(unsigned int fan_index, unsigned int motor_index,
char *buf, size_t count)
char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

FAN_DEV_VALID(fan_dev);
FAN_DEV_VALID(fan_dev->get_fan_motor_speed_target);
FAN_INDEX_MAPPING(fan_index);
return fan_dev->get_fan_motor_speed_target(fan_dev,fan_index, motor_index, buf, count);
return fan_dev->get_fan_motor_speed_target(fan_dev, fan_index, motor_index, buf, count);
}

/*
Expand All @@ -378,7 +389,7 @@ static ssize_t clx_get_fan_motor_speed_target(unsigned int fan_index, unsigned i
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_get_fan_motor_speed_max(unsigned int fan_index, unsigned int motor_index,
char *buf, size_t count)
char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

Expand All @@ -401,7 +412,7 @@ static ssize_t clx_get_fan_motor_speed_max(unsigned int fan_index, unsigned int
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_get_fan_motor_speed_min(unsigned int fan_index, unsigned int motor_index,
char *buf, size_t count)
char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

Expand All @@ -424,7 +435,7 @@ static ssize_t clx_get_fan_motor_speed_min(unsigned int fan_index, unsigned int
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_get_fan_motor_ratio(unsigned int fan_index, unsigned int motor_index,
char *buf, size_t count)
char *buf, size_t count)
{
struct fan_fn_if *fan_dev = get_fan();

Expand All @@ -444,7 +455,7 @@ static ssize_t clx_get_fan_motor_ratio(unsigned int fan_index, unsigned int moto
* otherwise it returns a negative value on failed.
*/
static int clx_set_fan_motor_ratio(unsigned int fan_index, unsigned int motor_index,
int ratio)
int ratio)
{
struct fan_fn_if *fan_dev = get_fan();

Expand Down Expand Up @@ -488,7 +499,7 @@ static int clx_get_fan_eeprom_size(unsigned int fan_index)
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_read_fan_eeprom_data(unsigned int fan_index, char *buf, loff_t offset,
size_t count)
size_t count)
{
int ret;
struct fan_fn_if *fan_dev = get_fan();
Expand Down Expand Up @@ -516,7 +527,7 @@ static ssize_t clx_read_fan_eeprom_data(unsigned int fan_index, char *buf, loff_
* otherwise it returns a negative value on failed.
*/
static ssize_t clx_write_fan_eeprom_data(unsigned int fan_index, char *buf, loff_t offset,
size_t count)
size_t count)
{
int ret;
struct fan_fn_if *fan_dev = get_fan();
Expand Down Expand Up @@ -544,7 +555,7 @@ static struct s3ip_sysfs_fan_drivers_s drivers = {
.get_debug = clx_get_fan_debug,
.set_debug = clx_set_fan_debug,
.get_fan_eeprom_wp = clx_get_fan_eeprom_wp,
.set_fan_eeprom_wp = clx_set_fan_eeprom_wp,
.set_fan_eeprom_wp = clx_set_fan_eeprom_wp,
.get_fan_number = clx_get_fan_number,
.get_fan_motor_number = clx_get_fan_motor_number,
.get_fan_vendor_name = clx_get_fan_vendor_name,
Expand All @@ -555,6 +566,7 @@ static struct s3ip_sysfs_fan_drivers_s drivers = {
.get_fan_status = clx_get_fan_status,
.get_fan_led_status = clx_get_fan_led_status,
.set_fan_led_status = clx_set_fan_led_status,
.get_fan_vmon = clx_get_fan_vmon,
.get_fan_direction = clx_get_fan_direction,
.get_fan_motor_speed = clx_get_fan_motor_speed,
.get_fan_motor_speed_tolerance = clx_get_fan_motor_speed_tolerance,
Expand All @@ -574,13 +586,15 @@ static int __init fan_dev_drv_init(void)

LOG_INFO(CLX_DRIVER_TYPES_FAN, "fan_init...\n");
ret = fan_if_create_driver();
if (ret != 0) {
if (ret != 0)
{
LOG_ERR(CLX_DRIVER_TYPES_FAN, "fan if create err, ret %d.\n", ret);
return ret;
}

ret = s3ip_sysfs_fan_drivers_register(&drivers);
if (ret < 0) {
if (ret < 0)
{
LOG_ERR(CLX_DRIVER_TYPES_FAN, "fan drivers register err, ret %d.\n", ret);
return ret;
}
Expand Down
Loading

0 comments on commit 6e24242

Please sign in to comment.