Skip to content

Commit

Permalink
[platform][Broadcom]Accton, fix building error of kernel modules. (#2197
Browse files Browse the repository at this point in the history
)

* [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>
  • Loading branch information
roylee123 authored and lguohan committed Oct 26, 2018
1 parent 251a850 commit 483bd9b
Show file tree
Hide file tree
Showing 5 changed files with 913 additions and 1,573 deletions.
248 changes: 53 additions & 195 deletions platform/broadcom/sonic-platform-modules-accton/as7712-32x/modules/accton_as7712_32x_fan.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -291,14 +290,14 @@ 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)
{
struct as7712_32x_fan_data *data = as7712_32x_fan_update_device(dev);

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)
{
Expand All @@ -319,238 +318,97 @@ 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)
static bool lm75_addr_mached(unsigned short addr)
{
struct lm75_data *data = NULL;
int i;
unsigned short addrs[] = THERMAL_SENSORS_ADDRS;

data = i2c_get_clientdata(client);
if(data)
for (i = 0; i < ARRAY_SIZE(addrs); i++)
{
if( data->valid == 1 && data->hwmon_dev)
{
return data->hwmon_dev;
}

if( addr == addrs[i])
return 1;
}
return NULL;
return 0;
}

/* 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;
/* 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;

#undef MAX_HWMON_DEVICE
}
struct attribute_group group;
const struct attribute_group **groups;
};
#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)

static int get_temp_file_path(
int bus_nr, unsigned short addr,
struct device *hwmon_dev
,char *path, int max_len)
/*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)
{

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;
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;
}
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)
static int hwmon_get_temp(struct device *dev, long *mini_cel)
{
struct lm75_data *data = NULL;
struct hwmon_device *hwmon_dev = to_hwmon_device(dev);
int ret = 0;

data = i2c_get_clientdata(client);
*miniCelsius = lm75_reg_to_mc(data->temp[0], data->resolution);
if(hwmon_dev) {
long t;
ret = hwmon_dev->chip->ops->read(dev, hwmon_temp, hwmon_temp_input,
0, &t);
if (ret < 0)
return ret;

return 0;
}

static bool lm75_addr_mached(unsigned short addr)
{
int i;
unsigned short addrs[] = THERMAL_SENSORS_ADDRS;

for (i = 0; i < ARRAY_SIZE(addrs); i++)
{
if( addr == addrs[i])
return 1;
*mini_cel = t;
}
return 0;
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;
}

/* 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++;
}
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;

}
else
{
get_lm75_temp(client, &miniCelsius);
prv->system_temp += miniCelsius;
prv->sensors_found++;

Expand Down
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
Loading

0 comments on commit 483bd9b

Please sign in to comment.