[platform][Broadcom]Accton, fix building error of kernel modules. (#2197)
* [platform]accton, fix building error of kernel modules at platform broadcom. Signed-off-by: roy_lee <roy_lee@accton.com> * Refine codes by formatter. Signed-off-by: roy_lee <roy_lee@accton.com>
This commit is contained in:
parent
251a850a50
commit
483bd9bbd8
262
platform/broadcom/sonic-platform-modules-accton/as7712-32x/modules/accton_as7712_32x_fan.c
Normal file → Executable file
262
platform/broadcom/sonic-platform-modules-accton/as7712-32x/modules/accton_as7712_32x_fan.c
Normal file → Executable file
@ -49,8 +49,7 @@ static ssize_t get_enable(struct device *dev, struct device_attribute *da, char
|
||||
static ssize_t set_enable(struct device *dev, struct device_attribute *da,
|
||||
const char *buf, size_t count);
|
||||
static ssize_t get_sys_temp(struct device *dev, struct device_attribute *da, char *buf);
|
||||
extern int accton_i2c_cpld_read(unsigned short cpld_addr, u8 reg);
|
||||
extern int accton_i2c_cpld_write(unsigned short cpld_addr, u8 reg, u8 value);
|
||||
|
||||
|
||||
/* fan related data, the index should match sysfs_fan_attributes
|
||||
*/
|
||||
@ -291,7 +290,6 @@ static ssize_t set_enable(struct device *dev, struct device_attribute *da,
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
static ssize_t get_enable(struct device *dev, struct device_attribute *da,
|
||||
char *buf)
|
||||
{
|
||||
@ -299,6 +297,7 @@ static ssize_t get_enable(struct device *dev, struct device_attribute *da,
|
||||
|
||||
return sprintf(buf, "%u\n", data->enable);
|
||||
}
|
||||
|
||||
static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
@ -319,180 +318,6 @@ static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Due to this struct is declared at lm75.c, it cannot be include
|
||||
* under Sonic environment. I duplicate it from lm75.c.
|
||||
*/
|
||||
struct lm75_data {
|
||||
struct i2c_client *client;
|
||||
struct device *hwmon_dev;
|
||||
struct thermal_zone_device *tz;
|
||||
struct mutex update_lock;
|
||||
u8 orig_conf;
|
||||
u8 resolution; /* In bits, between 9 and 12 */
|
||||
u8 resolution_limits;
|
||||
char valid; /* !=0 if registers are valid */
|
||||
unsigned long last_updated; /* In jiffies */
|
||||
unsigned long sample_time; /* In jiffies */
|
||||
s16 temp[3]; /* Register values,
|
||||
0 = input
|
||||
1 = max
|
||||
2 = hyst */
|
||||
};
|
||||
|
||||
/*Copied from lm75.c*/
|
||||
static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
|
||||
{
|
||||
return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
|
||||
}
|
||||
|
||||
/*Get hwmon_dev from i2c_client, set hwmon_dev = NULL is failed.*/
|
||||
static struct device * get_hwmon_dev(
|
||||
struct i2c_client *client)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
if(data)
|
||||
{
|
||||
if( data->valid == 1 && data->hwmon_dev)
|
||||
{
|
||||
return data->hwmon_dev;
|
||||
}
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* To find hwmon index by opening hwmon under that i2c address.
|
||||
*/
|
||||
static int find_hwmon_index_by_FileOpen(
|
||||
int bus_nr,
|
||||
unsigned short addr,
|
||||
OUT int *index)
|
||||
{
|
||||
#define MAX_HWMON_DEVICE (10) /* Find hwmon device in 0~10*/
|
||||
struct file *sfd;
|
||||
char client_name[96];
|
||||
int i=0;
|
||||
|
||||
do {
|
||||
snprintf(client_name, sizeof(client_name),
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/hwmon%d/temp1_input",
|
||||
bus_nr, addr, i);
|
||||
|
||||
sfd = filp_open(client_name, O_RDONLY, 0);
|
||||
i++;
|
||||
} while( IS_ERR(sfd) && i < MAX_HWMON_DEVICE);
|
||||
|
||||
if (IS_ERR(sfd)) {
|
||||
pr_err("Failed to open file(%s)#%d\r\n", client_name, __LINE__);
|
||||
return -ENOENT;
|
||||
}
|
||||
filp_close(sfd, 0);
|
||||
*index = i - 1;
|
||||
return 0;
|
||||
|
||||
#undef MAX_HWMON_DEVICE
|
||||
}
|
||||
|
||||
static int get_temp_file_path(
|
||||
int bus_nr, unsigned short addr,
|
||||
struct device *hwmon_dev
|
||||
,char *path, int max_len)
|
||||
{
|
||||
|
||||
if(hwmon_dev && strlen(dev_name(hwmon_dev)))
|
||||
{
|
||||
snprintf(path, max_len,
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/%s/temp1_input",
|
||||
bus_nr, addr, dev_name(hwmon_dev));
|
||||
}
|
||||
else
|
||||
{
|
||||
int i=0;
|
||||
if(find_hwmon_index_by_FileOpen( bus_nr, addr, &i))
|
||||
{
|
||||
return -EIO;
|
||||
}
|
||||
snprintf(path, max_len,
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/hwmon%d/temp1_input",
|
||||
bus_nr, addr, i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*File read the dev file at user space.*/
|
||||
static int read_devfile_temp1_input(
|
||||
struct device *dev,
|
||||
int bus_nr,
|
||||
unsigned short addr,
|
||||
struct device *hwmon_dev,
|
||||
int *miniCelsius)
|
||||
{
|
||||
struct file *sfd;
|
||||
char buffer[96];
|
||||
char devfile[96];
|
||||
int rc, status;
|
||||
int rdlen, value;
|
||||
mm_segment_t old_fs;
|
||||
|
||||
rc = 0;
|
||||
get_temp_file_path(bus_nr, addr, hwmon_dev, devfile, sizeof(devfile));
|
||||
sfd = filp_open(devfile, O_RDONLY, 0);
|
||||
if (IS_ERR(sfd)) {
|
||||
pr_err("Failed to open file(%s)#%d\r\n", devfile, __LINE__);
|
||||
return -ENOENT;
|
||||
}
|
||||
dev_dbg(dev, "Found device:%s\n",devfile);
|
||||
|
||||
if(!(sfd->f_op) || !(sfd->f_op->read) ) {
|
||||
pr_err("file %s cann't readable ?\n",devfile);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
old_fs = get_fs();
|
||||
set_fs(KERNEL_DS);
|
||||
rdlen = sfd->f_op->read(sfd, buffer, sizeof(buffer), &sfd->f_pos);
|
||||
if (rdlen == 0) {
|
||||
pr_err( "File(%s) empty!\n", devfile);
|
||||
rc = -EIO;
|
||||
goto exit;
|
||||
}
|
||||
status = sscanf(buffer, "%d", &value);
|
||||
if (status != 1) {
|
||||
rc = -EIO;
|
||||
goto exit;
|
||||
}
|
||||
*miniCelsius = value;
|
||||
dev_dbg(dev,"found sensors: %d @i2c %d-%04x\n", value, bus_nr, addr);
|
||||
|
||||
exit:
|
||||
set_fs(old_fs);
|
||||
filp_close(sfd, 0);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static u8 is_lm75_data_due(struct i2c_client *client)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
if (time_after(jiffies, data->last_updated + data->sample_time))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int get_lm75_temp(struct i2c_client *client, int *miniCelsius)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
*miniCelsius = lm75_reg_to_mc(data->temp[0], data->resolution);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool lm75_addr_mached(unsigned short addr)
|
||||
{
|
||||
int i;
|
||||
@ -506,51 +331,84 @@ static bool lm75_addr_mached(unsigned short addr)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Struct and define are copied from drivers/hwmon/hwmon.c. */
|
||||
struct hwmon_device {
|
||||
const char *name;
|
||||
struct device dev;
|
||||
const struct hwmon_chip_info *chip;
|
||||
|
||||
struct attribute_group group;
|
||||
const struct attribute_group **groups;
|
||||
};
|
||||
#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
|
||||
|
||||
/*Find the 1st valid dev of all childs, it supposes to have only 1 child.*/
|
||||
static int is_valid_dev(struct device *dev, void *data)
|
||||
{
|
||||
struct hwmon_device *hwmon_dev;
|
||||
|
||||
if(dev) {
|
||||
hwmon_dev = to_hwmon_device(dev);
|
||||
if(hwmon_dev) {
|
||||
int ret;
|
||||
long t;
|
||||
ret = hwmon_dev->chip->ops->read(dev, hwmon_temp, hwmon_temp_input,
|
||||
0, &t);
|
||||
return !ret;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hwmon_get_temp(struct device *dev, long *mini_cel)
|
||||
{
|
||||
struct hwmon_device *hwmon_dev = to_hwmon_device(dev);
|
||||
int ret = 0;
|
||||
|
||||
if(hwmon_dev) {
|
||||
long t;
|
||||
ret = hwmon_dev->chip->ops->read(dev, hwmon_temp, hwmon_temp_input,
|
||||
0, &t);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
*mini_cel = t;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Find devices under i2c_bus which with driver = lm75.
|
||||
* Use the device to find its descendent hwmon_dev and read the temperature.
|
||||
*/
|
||||
static int _find_lm75_device(struct device *dev, void *data)
|
||||
{
|
||||
struct device_driver *driver;
|
||||
struct as7712_32x_fan_data *prv = data;
|
||||
char *driver_name = THERMAL_SENSORS_DRIVER;
|
||||
struct i2c_client *client;
|
||||
|
||||
driver = dev->driver;
|
||||
if (driver && driver->name &&
|
||||
strcmp(driver->name, driver_name) == 0)
|
||||
{
|
||||
struct i2c_client *client;
|
||||
|
||||
client = to_i2c_client(dev);
|
||||
if (client)
|
||||
{
|
||||
/*cannot use "struct i2c_adapter *adap = to_i2c_adapter(dev);"*/
|
||||
struct i2c_adapter *adap = client->adapter;
|
||||
int miniCelsius = 0;
|
||||
long miniCelsius = 0;
|
||||
struct device *child_dev;
|
||||
|
||||
if (! lm75_addr_mached(client->addr))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!adap) {
|
||||
return -ENXIO;
|
||||
}
|
||||
child_dev = device_find_child(dev, NULL, is_valid_dev);
|
||||
if(child_dev) {
|
||||
int ret = hwmon_get_temp(child_dev, &miniCelsius);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
/* If the data is not updated, read them from devfile
|
||||
to drive them updateing data from chip.*/
|
||||
if (is_lm75_data_due(client))
|
||||
{
|
||||
struct device *hwmon_dev;
|
||||
|
||||
hwmon_dev = get_hwmon_dev(client);
|
||||
if(0 == read_devfile_temp1_input(dev, adap->nr,
|
||||
client->addr, hwmon_dev, &miniCelsius))
|
||||
{
|
||||
prv->system_temp += miniCelsius;
|
||||
prv->sensors_found++;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
get_lm75_temp(client, &miniCelsius);
|
||||
prv->system_temp += miniCelsius;
|
||||
prv->sensors_found++;
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
obj-m:= accton_as7716_32x_cpld1.o accton_as7716_32x_fan.o \
|
||||
accton_as7716_32x_leds.o accton_as7716_32x_psu.o cpr_4011_4mxx.o ym2651y.o \
|
||||
optoe.o accton_i2c_cpld.o
|
||||
accton_i2c_cpld.o
|
||||
|
||||
else
|
||||
ifeq (,$(KERNEL_SRC))
|
||||
|
261
platform/broadcom/sonic-platform-modules-accton/as7716-32x/modules/accton_as7716_32x_fan.c
Executable file → Normal file
261
platform/broadcom/sonic-platform-modules-accton/as7716-32x/modules/accton_as7716_32x_fan.c
Executable file → Normal file
@ -34,9 +34,6 @@
|
||||
|
||||
#define DRVNAME "as7716_32x_fan"
|
||||
|
||||
#define NUM_THERMAL_SENSORS (3) /* Get sum of this number of sensors.*/
|
||||
#define THERMAL_SENSORS_DRIVER "lm75"
|
||||
|
||||
#define IN
|
||||
#define OUT
|
||||
|
||||
@ -47,9 +44,6 @@ static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
static ssize_t get_enable(struct device *dev, struct device_attribute *da, char *buf);
|
||||
static ssize_t set_enable(struct device *dev, struct device_attribute *da,
|
||||
const char *buf, size_t count);
|
||||
static ssize_t get_sys_temp(struct device *dev, struct device_attribute *da, char *buf);
|
||||
extern int as7716_32x_cpld_read(unsigned short cpld_addr, u8 reg);
|
||||
extern int as7716_32x_cpld_write(unsigned short cpld_addr, u8 reg, u8 value);
|
||||
|
||||
/* fan related data, the index should match sysfs_fan_attributes
|
||||
*/
|
||||
@ -79,8 +73,6 @@ struct as7716_32x_fan_data {
|
||||
unsigned long last_updated; /* In jiffies */
|
||||
u8 reg_val[ARRAY_SIZE(fan_reg)]; /* Register value */
|
||||
u8 enable;
|
||||
int system_temp; /*In unit of mini-Celsius*/
|
||||
int sensors_found;
|
||||
};
|
||||
|
||||
enum fan_id {
|
||||
@ -149,12 +141,6 @@ enum sysfs_fan_attributes {
|
||||
&sensor_dev_attr_pwm##index.dev_attr.attr, \
|
||||
&sensor_dev_attr_pwm##index##_enable.dev_attr.attr
|
||||
|
||||
#define DECLARE_FAN_SYSTEM_TEMP_SENSOR_DEV_ATTR() \
|
||||
static SENSOR_DEVICE_ATTR(sys_temp, S_IRUGO, get_sys_temp, NULL, FAN_DUTY_CYCLE_PERCENTAGE)
|
||||
|
||||
#define DECLARE_FAN_SYSTEM_TEMP_ATTR() &sensor_dev_attr_sys_temp.dev_attr.attr
|
||||
|
||||
|
||||
#define DECLARE_FAN_PRESENT_SENSOR_DEV_ATTR(index) \
|
||||
static SENSOR_DEVICE_ATTR(fan##index##_present, S_IRUGO, fan_show_value, NULL, FAN##index##_PRESENT)
|
||||
#define DECLARE_FAN_PRESENT_ATTR(index) &sensor_dev_attr_fan##index##_present.dev_attr.attr
|
||||
@ -200,8 +186,6 @@ DECLARE_FAN_DIRECTION_SENSOR_DEV_ATTR(6);
|
||||
|
||||
/* 1 fan duty cycle attribute in this platform */
|
||||
DECLARE_FAN_DUTY_CYCLE_SENSOR_DEV_ATTR(1);
|
||||
/* System temperature for fancontrol */
|
||||
DECLARE_FAN_SYSTEM_TEMP_SENSOR_DEV_ATTR();
|
||||
|
||||
static struct attribute *as7716_32x_fan_attributes[] = {
|
||||
/* fan related attributes */
|
||||
@ -230,7 +214,6 @@ static struct attribute *as7716_32x_fan_attributes[] = {
|
||||
DECLARE_FAN_DIRECTION_ATTR(5),
|
||||
DECLARE_FAN_DIRECTION_ATTR(6),
|
||||
DECLARE_FAN_DUTY_CYCLE_ATTR(1),
|
||||
DECLARE_FAN_SYSTEM_TEMP_ATTR(),
|
||||
NULL
|
||||
};
|
||||
|
||||
@ -328,6 +311,7 @@ static ssize_t get_enable(struct device *dev, struct device_attribute *da,
|
||||
|
||||
return sprintf(buf, "%u\n", data->enable);
|
||||
}
|
||||
|
||||
static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
@ -346,249 +330,6 @@ static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Due to this struct is declared at lm75.c, it cannot be include
|
||||
* under Sonic environment. I duplicate it from lm75.c.
|
||||
*/
|
||||
struct lm75_data {
|
||||
struct i2c_client *client;
|
||||
struct device *hwmon_dev;
|
||||
struct thermal_zone_device *tz;
|
||||
struct mutex update_lock;
|
||||
u8 orig_conf;
|
||||
u8 resolution; /* In bits, between 9 and 12 */
|
||||
u8 resolution_limits;
|
||||
char valid; /* !=0 if registers are valid */
|
||||
unsigned long last_updated; /* In jiffies */
|
||||
unsigned long sample_time; /* In jiffies */
|
||||
s16 temp[3]; /* Register values,
|
||||
0 = input
|
||||
1 = max
|
||||
2 = hyst */
|
||||
};
|
||||
|
||||
/*Copied from lm75.c*/
|
||||
static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
|
||||
{
|
||||
return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
|
||||
}
|
||||
|
||||
/*Get hwmon_dev from i2c_client, set hwmon_dev = NULL is failed.*/
|
||||
static struct device * get_hwmon_dev(
|
||||
struct i2c_client *client)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
if(data)
|
||||
{
|
||||
if( data->valid == 1 && data->hwmon_dev)
|
||||
{
|
||||
return data->hwmon_dev;
|
||||
}
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* To find hwmon index by opening hwmon under that i2c address.
|
||||
*/
|
||||
static int find_hwmon_index_by_FileOpen(
|
||||
int bus_nr,
|
||||
unsigned short addr,
|
||||
OUT int *index)
|
||||
{
|
||||
#define MAX_HWMON_DEVICE (10) /* Find hwmon device in 0~10*/
|
||||
struct file *sfd;
|
||||
char client_name[96];
|
||||
int i=0;
|
||||
|
||||
do {
|
||||
snprintf(client_name, sizeof(client_name),
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/hwmon%d/temp1_input",
|
||||
bus_nr, addr, i);
|
||||
|
||||
sfd = filp_open(client_name, O_RDONLY, 0);
|
||||
i++;
|
||||
} while( IS_ERR(sfd) && i < MAX_HWMON_DEVICE);
|
||||
|
||||
if (IS_ERR(sfd)) {
|
||||
pr_err("Failed to open file(%s)#%d\r\n", client_name, __LINE__);
|
||||
return -ENOENT;
|
||||
}
|
||||
filp_close(sfd, 0);
|
||||
*index = i - 1;
|
||||
return 0;
|
||||
|
||||
#undef MAX_HWMON_DEVICE
|
||||
}
|
||||
|
||||
static int get_temp_file_path(
|
||||
int bus_nr, unsigned short addr,
|
||||
struct device *hwmon_dev
|
||||
,char *path, int max_len)
|
||||
{
|
||||
|
||||
if(hwmon_dev && strlen(dev_name(hwmon_dev)))
|
||||
{
|
||||
snprintf(path, max_len,
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/%s/temp1_input",
|
||||
bus_nr, addr, dev_name(hwmon_dev));
|
||||
}
|
||||
else
|
||||
{
|
||||
int i=0;
|
||||
if(find_hwmon_index_by_FileOpen( bus_nr, addr, &i))
|
||||
{
|
||||
return -EIO;
|
||||
}
|
||||
snprintf(path, max_len,
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/hwmon%d/temp1_input",
|
||||
bus_nr, addr, i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*File read the dev file at user space.*/
|
||||
static int read_devfile_temp1_input(
|
||||
struct device *dev,
|
||||
int bus_nr,
|
||||
unsigned short addr,
|
||||
struct device *hwmon_dev,
|
||||
int *miniCelsius)
|
||||
{
|
||||
struct file *sfd;
|
||||
char buffer[96];
|
||||
char devfile[96];
|
||||
int rc, status;
|
||||
int rdlen, value;
|
||||
mm_segment_t old_fs;
|
||||
|
||||
rc = 0;
|
||||
get_temp_file_path(bus_nr, addr, hwmon_dev, devfile, sizeof(devfile));
|
||||
sfd = filp_open(devfile, O_RDONLY, 0);
|
||||
if (IS_ERR(sfd)) {
|
||||
pr_err("Failed to open file(%s)#%d\r\n", devfile, __LINE__);
|
||||
return -ENOENT;
|
||||
}
|
||||
dev_dbg(dev, "Found device:%s\n",devfile);
|
||||
|
||||
if(!(sfd->f_op) || !(sfd->f_op->read) ) {
|
||||
pr_err("file %s cann't readable ?\n",devfile);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
old_fs = get_fs();
|
||||
set_fs(KERNEL_DS);
|
||||
rdlen = sfd->f_op->read(sfd, buffer, sizeof(buffer), &sfd->f_pos);
|
||||
if (rdlen == 0) {
|
||||
pr_err( "File(%s) empty!\n", devfile);
|
||||
rc = -EIO;
|
||||
goto exit;
|
||||
}
|
||||
status = sscanf(buffer, "%d", &value);
|
||||
if (status != 1) {
|
||||
rc = -EIO;
|
||||
goto exit;
|
||||
}
|
||||
*miniCelsius = value;
|
||||
dev_dbg(dev,"found sensors: %d @i2c %d-%04x\n", value, bus_nr, addr);
|
||||
|
||||
exit:
|
||||
set_fs(old_fs);
|
||||
filp_close(sfd, 0);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static u8 is_lm75_data_due(struct i2c_client *client)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
if (time_after(jiffies, data->last_updated + data->sample_time))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int get_lm75_temp(struct i2c_client *client, int *miniCelsius)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
*miniCelsius = lm75_reg_to_mc(data->temp[0], data->resolution);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _find_lm75_device(struct device *dev, void *data)
|
||||
{
|
||||
struct device_driver *driver;
|
||||
struct as7716_32x_fan_data *prv = data;
|
||||
char *driver_name = THERMAL_SENSORS_DRIVER;
|
||||
|
||||
driver = dev->driver;
|
||||
if (driver && driver->name &&
|
||||
strcmp(driver->name, driver_name) == 0)
|
||||
{
|
||||
struct i2c_client *client;
|
||||
client = to_i2c_client(dev);
|
||||
if (client)
|
||||
{
|
||||
/*cannot use "struct i2c_adapter *adap = to_i2c_adapter(dev);"*/
|
||||
struct i2c_adapter *adap = client->adapter;
|
||||
int miniCelsius = 0;
|
||||
|
||||
|
||||
if (!adap) {
|
||||
return -ENXIO;
|
||||
}
|
||||
|
||||
/* If the data is not updated, read them from devfile
|
||||
to drive them updateing data from chip.*/
|
||||
if (is_lm75_data_due(client))
|
||||
{
|
||||
struct device *hwmon_dev;
|
||||
|
||||
hwmon_dev = get_hwmon_dev(client);
|
||||
if(0 == read_devfile_temp1_input(dev, adap->nr,
|
||||
client->addr, hwmon_dev, &miniCelsius))
|
||||
{
|
||||
prv->system_temp += miniCelsius;
|
||||
prv->sensors_found++;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
get_lm75_temp(client, &miniCelsius);
|
||||
prv->system_temp += miniCelsius;
|
||||
prv->sensors_found++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*Find all lm75 devices and return sum of temperatures.*/
|
||||
static ssize_t get_sys_temp(struct device *dev, struct device_attribute *da,
|
||||
char *buf)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
struct as7716_32x_fan_data *data = as7716_32x_fan_update_device(dev);
|
||||
|
||||
data->system_temp=0;
|
||||
data->sensors_found=0;
|
||||
i2c_for_each_dev(data, _find_lm75_device);
|
||||
if (NUM_THERMAL_SENSORS != data->sensors_found)
|
||||
{
|
||||
dev_dbg(dev,"only %d of %d temps are found\n",
|
||||
data->sensors_found, NUM_THERMAL_SENSORS);
|
||||
data->system_temp = 0;
|
||||
}
|
||||
ret = sprintf(buf, "%d\n",data->system_temp);
|
||||
return ret;
|
||||
}
|
||||
static ssize_t fan_show_value(struct device *dev, struct device_attribute *da,
|
||||
char *buf)
|
||||
{
|
||||
|
4
platform/broadcom/sonic-platform-modules-accton/as7716-32xb/modules/accton_as7716_32xb_cpld1.c
Executable file → Normal file
4
platform/broadcom/sonic-platform-modules-accton/as7716-32xb/modules/accton_as7716_32xb_cpld1.c
Executable file → Normal file
@ -65,7 +65,7 @@ static int as7716_32xb_cpld_write_internal(struct i2c_client *client, u8 reg, u8
|
||||
static ssize_t sfp_value_show(struct device *dev, struct device_attribute *da,
|
||||
char *buf);
|
||||
static ssize_t sfp_value_store(struct device *dev, struct device_attribute *da,
|
||||
char *buf, size_t size);
|
||||
const char *buf, size_t size);
|
||||
|
||||
|
||||
#define PORT_NUM_MAX 32
|
||||
@ -496,7 +496,7 @@ static int sfp_array_index_get(int attr_idx)
|
||||
|
||||
|
||||
static ssize_t sfp_value_store(struct device *dev, struct device_attribute *da,
|
||||
char *buf, size_t size)
|
||||
const char *buf, size_t size)
|
||||
{
|
||||
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
|
||||
struct i2c_client *client = to_i2c_client(dev);
|
||||
|
263
platform/broadcom/sonic-platform-modules-accton/as7716-32xb/modules/accton_as7716_32xb_fan.c
Executable file → Normal file
263
platform/broadcom/sonic-platform-modules-accton/as7716-32xb/modules/accton_as7716_32xb_fan.c
Executable file → Normal file
@ -47,13 +47,11 @@ static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
static ssize_t get_enable(struct device *dev, struct device_attribute *da, char *buf);
|
||||
static ssize_t set_enable(struct device *dev, struct device_attribute *da,
|
||||
const char *buf, size_t count);
|
||||
static ssize_t get_sys_temp(struct device *dev, struct device_attribute *da, char *buf);
|
||||
|
||||
|
||||
static ssize_t fan_value_show(struct device *dev, struct device_attribute *da,
|
||||
char *buf);
|
||||
static ssize_t fan_value_store(struct device *dev, struct device_attribute *da,
|
||||
char *buf, size_t size);
|
||||
const char *buf, size_t size);
|
||||
|
||||
/* fan related data, the index should match sysfs_fan_attributes
|
||||
*/
|
||||
@ -85,8 +83,6 @@ struct as7716_32xb_fan_data {
|
||||
u8 reg_val[ARRAY_SIZE(fan_reg)]; /* Register value */
|
||||
u8 enable;
|
||||
u8 duty_cycle;
|
||||
int system_temp; /*In unit of mini-Celsius*/
|
||||
int sensors_found;
|
||||
unsigned int present[FAN_NUM_MAX];
|
||||
unsigned int front_speed_rpm[FAN_NUM_MAX];
|
||||
unsigned int rear_speed_rpm[FAN_NUM_MAX];
|
||||
@ -162,13 +158,6 @@ enum sysfs_fan_attributes {
|
||||
&sensor_dev_attr_pwm##index.dev_attr.attr, \
|
||||
&sensor_dev_attr_pwm##index##_enable.dev_attr.attr
|
||||
|
||||
#define DECLARE_FAN_SYSTEM_TEMP_SENSOR_DEV_ATTR() \
|
||||
static SENSOR_DEVICE_ATTR(sys_temp, S_IWUSR|S_IRUGO, get_sys_temp, NULL, FAN_DUTY_CYCLE_PERCENTAGE)
|
||||
|
||||
#define DECLARE_FAN_SYSTEM_TEMP_ATTR() &sensor_dev_attr_sys_temp.dev_attr.attr
|
||||
|
||||
|
||||
|
||||
#define DECLARE_FAN_PRESENT_SENSOR_DEV_ATTR(index) \
|
||||
static SENSOR_DEVICE_ATTR(fan##index##_present, S_IWUSR|S_IRUGO, fan_value_show, fan_value_store, FAN##index##_PRESENT)
|
||||
#define DECLARE_FAN_PRESENT_ATTR(index) &sensor_dev_attr_fan##index##_present.dev_attr.attr
|
||||
@ -215,8 +204,6 @@ DECLARE_FAN_DIRECTION_SENSOR_DEV_ATTR(6);
|
||||
|
||||
/* 1 fan duty cycle attribute in this platform */
|
||||
DECLARE_FAN_DUTY_CYCLE_SENSOR_DEV_ATTR(1);
|
||||
/* System temperature for fancontrol */
|
||||
DECLARE_FAN_SYSTEM_TEMP_SENSOR_DEV_ATTR();
|
||||
|
||||
static struct attribute *as7716_32xb_fan_attributes[] = {
|
||||
/* fan related attributes */
|
||||
@ -245,7 +232,6 @@ static struct attribute *as7716_32xb_fan_attributes[] = {
|
||||
DECLARE_FAN_DIRECTION_ATTR(5),
|
||||
DECLARE_FAN_DIRECTION_ATTR(6),
|
||||
DECLARE_FAN_DUTY_CYCLE_ATTR(1),
|
||||
DECLARE_FAN_SYSTEM_TEMP_ATTR(),
|
||||
NULL
|
||||
};
|
||||
|
||||
@ -363,251 +349,6 @@ static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Due to this struct is declared at lm75.c, it cannot be include
|
||||
* under Sonic environment. I duplicate it from lm75.c.
|
||||
*/
|
||||
struct lm75_data {
|
||||
struct i2c_client *client;
|
||||
struct device *hwmon_dev;
|
||||
struct thermal_zone_device *tz;
|
||||
struct mutex update_lock;
|
||||
u8 orig_conf;
|
||||
u8 resolution; /* In bits, between 9 and 12 */
|
||||
u8 resolution_limits;
|
||||
char valid; /* !=0 if registers are valid */
|
||||
unsigned long last_updated; /* In jiffies */
|
||||
unsigned long sample_time; /* In jiffies */
|
||||
s16 temp[3]; /* Register values,
|
||||
0 = input
|
||||
1 = max
|
||||
2 = hyst */
|
||||
};
|
||||
|
||||
/*Copied from lm75.c*/
|
||||
static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
|
||||
{
|
||||
return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
|
||||
}
|
||||
|
||||
/*Get hwmon_dev from i2c_client, set hwmon_dev = NULL is failed.*/
|
||||
static struct device * get_hwmon_dev(
|
||||
struct i2c_client *client)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
if(data)
|
||||
{
|
||||
if( data->valid == 1 && data->hwmon_dev)
|
||||
{
|
||||
return data->hwmon_dev;
|
||||
}
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* To find hwmon index by opening hwmon under that i2c address.
|
||||
*/
|
||||
static int find_hwmon_index_by_FileOpen(
|
||||
int bus_nr,
|
||||
unsigned short addr,
|
||||
OUT int *index)
|
||||
{
|
||||
#define MAX_HWMON_DEVICE (10) /* Find hwmon device in 0~10*/
|
||||
struct file *sfd;
|
||||
char client_name[96];
|
||||
int i=0;
|
||||
|
||||
do {
|
||||
snprintf(client_name, sizeof(client_name),
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/hwmon%d/temp1_input",
|
||||
bus_nr, addr, i);
|
||||
|
||||
sfd = filp_open(client_name, O_RDONLY, 0);
|
||||
i++;
|
||||
} while( IS_ERR(sfd) && i < MAX_HWMON_DEVICE);
|
||||
|
||||
if (IS_ERR(sfd)) {
|
||||
pr_err("Failed to open file(%s)#%d\r\n", client_name, __LINE__);
|
||||
return -ENOENT;
|
||||
}
|
||||
filp_close(sfd, 0);
|
||||
*index = i - 1;
|
||||
return 0;
|
||||
|
||||
#undef MAX_HWMON_DEVICE
|
||||
}
|
||||
|
||||
static int get_temp_file_path(
|
||||
int bus_nr, unsigned short addr,
|
||||
struct device *hwmon_dev
|
||||
,char *path, int max_len)
|
||||
{
|
||||
|
||||
if(hwmon_dev && strlen(dev_name(hwmon_dev)))
|
||||
{
|
||||
snprintf(path, max_len,
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/%s/temp1_input",
|
||||
bus_nr, addr, dev_name(hwmon_dev));
|
||||
}
|
||||
else
|
||||
{
|
||||
int i=0;
|
||||
if(find_hwmon_index_by_FileOpen( bus_nr, addr, &i))
|
||||
{
|
||||
return -EIO;
|
||||
}
|
||||
snprintf(path, max_len,
|
||||
"/sys/bus/i2c/devices/%d-%04x/hwmon/hwmon%d/temp1_input",
|
||||
bus_nr, addr, i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*File read the dev file at user space.*/
|
||||
static int read_devfile_temp1_input(
|
||||
struct device *dev,
|
||||
int bus_nr,
|
||||
unsigned short addr,
|
||||
struct device *hwmon_dev,
|
||||
int *miniCelsius)
|
||||
{
|
||||
struct file *sfd;
|
||||
char buffer[96];
|
||||
char devfile[96];
|
||||
int rc, status;
|
||||
int rdlen, value;
|
||||
mm_segment_t old_fs;
|
||||
|
||||
rc = 0;
|
||||
get_temp_file_path(bus_nr, addr, hwmon_dev, devfile, sizeof(devfile));
|
||||
sfd = filp_open(devfile, O_RDONLY, 0);
|
||||
if (IS_ERR(sfd)) {
|
||||
pr_err("Failed to open file(%s)#%d\r\n", devfile, __LINE__);
|
||||
return -ENOENT;
|
||||
}
|
||||
dev_dbg(dev, "Found device:%s\n",devfile);
|
||||
|
||||
if(!(sfd->f_op) || !(sfd->f_op->read) ) {
|
||||
pr_err("file %s cann't readable ?\n",devfile);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
old_fs = get_fs();
|
||||
set_fs(KERNEL_DS);
|
||||
rdlen = sfd->f_op->read(sfd, buffer, sizeof(buffer), &sfd->f_pos);
|
||||
if (rdlen == 0) {
|
||||
pr_err( "File(%s) empty!\n", devfile);
|
||||
rc = -EIO;
|
||||
goto exit;
|
||||
}
|
||||
status = sscanf(buffer, "%d", &value);
|
||||
if (status != 1) {
|
||||
rc = -EIO;
|
||||
goto exit;
|
||||
}
|
||||
*miniCelsius = value;
|
||||
dev_dbg(dev,"found sensors: %d @i2c %d-%04x\n", value, bus_nr, addr);
|
||||
|
||||
exit:
|
||||
set_fs(old_fs);
|
||||
filp_close(sfd, 0);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static u8 is_lm75_data_due(struct i2c_client *client)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
if (time_after(jiffies, data->last_updated + data->sample_time))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int get_lm75_temp(struct i2c_client *client, int *miniCelsius)
|
||||
{
|
||||
struct lm75_data *data = NULL;
|
||||
|
||||
data = i2c_get_clientdata(client);
|
||||
*miniCelsius = lm75_reg_to_mc(data->temp[0], data->resolution);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _find_lm75_device(struct device *dev, void *data)
|
||||
{
|
||||
struct device_driver *driver;
|
||||
struct as7716_32xb_fan_data *prv = data;
|
||||
char *driver_name = THERMAL_SENSORS_DRIVER;
|
||||
|
||||
driver = dev->driver;
|
||||
if (driver && driver->name &&
|
||||
strcmp(driver->name, driver_name) == 0)
|
||||
{
|
||||
struct i2c_client *client;
|
||||
client = to_i2c_client(dev);
|
||||
if (client)
|
||||
{
|
||||
/*cannot use "struct i2c_adapter *adap = to_i2c_adapter(dev);"*/
|
||||
struct i2c_adapter *adap = client->adapter;
|
||||
int miniCelsius = 0;
|
||||
|
||||
|
||||
if (!adap) {
|
||||
return -ENXIO;
|
||||
}
|
||||
|
||||
/* If the data is not updated, read them from devfile
|
||||
to drive them updateing data from chip.*/
|
||||
if (is_lm75_data_due(client))
|
||||
{
|
||||
struct device *hwmon_dev;
|
||||
|
||||
hwmon_dev = get_hwmon_dev(client);
|
||||
if(0 == read_devfile_temp1_input(dev, adap->nr,
|
||||
client->addr, hwmon_dev, &miniCelsius))
|
||||
{
|
||||
prv->system_temp += miniCelsius;
|
||||
prv->sensors_found++;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
get_lm75_temp(client, &miniCelsius);
|
||||
prv->system_temp += miniCelsius;
|
||||
prv->sensors_found++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*Find all lm75 devices and return sum of temperatures.*/
|
||||
static ssize_t get_sys_temp(struct device *dev, struct device_attribute *da,
|
||||
char *buf)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
struct as7716_32xb_fan_data *data = as7716_32xb_fan_update_device(dev);
|
||||
|
||||
data->system_temp=0;
|
||||
data->sensors_found=0;
|
||||
i2c_for_each_dev(data, _find_lm75_device);
|
||||
if (NUM_THERMAL_SENSORS != data->sensors_found)
|
||||
{
|
||||
dev_dbg(dev,"only %d of %d temps are found\n",
|
||||
data->sensors_found, NUM_THERMAL_SENSORS);
|
||||
data->system_temp = 0;
|
||||
}
|
||||
ret = sprintf(buf, "%d\n",data->system_temp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int fan_array_index_get(int attr_idx)
|
||||
{
|
||||
switch(attr_idx)
|
||||
@ -654,7 +395,7 @@ static int fan_array_index_get(int attr_idx)
|
||||
}
|
||||
|
||||
static ssize_t fan_value_store(struct device *dev, struct device_attribute *da,
|
||||
char *buf, size_t size)
|
||||
const char *buf, size_t size)
|
||||
{
|
||||
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
|
||||
struct i2c_client *client = to_i2c_client(dev);
|
||||
|
Reference in New Issue
Block a user