sonic-buildimage/platform/nephos/sonic-platform-modules-cig/cs6436-56p/modules/x86-64-cig-cs6436-56p-fan.c
michealylj1 397b552a76 [Devices] Add new device CIG CS6436-56P (#2587)
* Add new device CIG CS6436-56P

* Delete minigraph.xml

It isn't necessary in the current system, just delete it

* Update qos.json.j2

* Update port_config.ini

Add the speed column. The cmd to show interface status as:

root@switch1:~# show interface status             
  Interface            Lanes    Speed    MTU         Alias    Oper    Admin    Type    Asym PFC
-----------  ---------------  -------  -----  ------------  ------  -------  ------  ----------
  Ethernet0                8      25G   9100   Ethernet1/1      up       up     SFP         N/A
  Ethernet1                9      25G   9100   Ethernet2/1      up       up     SFP         N/A
  Ethernet2               10      25G   9100   Ethernet3/1    down     down     N/A         N/A
  Ethernet3               11      25G   9100   Ethernet4/1    down     down     N/A         N/A
  Ethernet4               12      25G   9100   Ethernet5/1    down     down     N/A         N/A
  Ethernet5               13      25G   9100   Ethernet6/1    down     down     N/A         N/A
  Ethernet6               14      25G   9100   Ethernet7/1    down     down     N/A         N/A
  Ethernet7               15      25G   9100   Ethernet8/1    down     down     N/A         N/A
  Ethernet8               16      25G   9100   Ethernet9/1    down     down     N/A         N/A
  Ethernet9               17      25G   9100  Ethernet10/1    down     down     N/A         N/A
 Ethernet10               18      25G   9100  Ethernet11/1    down     down     N/A         N/A
 Ethernet11               19      25G   9100  Ethernet12/1    down     down     N/A         N/A
 Ethernet12               20      25G   9100  Ethernet13/1    down     down     N/A         N/A
 Ethernet13               21      25G   9100  Ethernet14/1    down     down     N/A         N/A
 Ethernet14               22      25G   9100  Ethernet15/1    down     down     N/A         N/A
 Ethernet15               23      25G   9100  Ethernet16/1    down     down     N/A         N/A
 Ethernet16               32      25G   9100  Ethernet17/1    down     down     N/A         N/A
 Ethernet17               33      25G   9100  Ethernet18/1    down     down     N/A         N/A
 Ethernet18               34      25G   9100  Ethernet19/1    down     down     N/A         N/A
 Ethernet19               35      25G   9100  Ethernet20/1    down     down     N/A         N/A
 Ethernet20               40      25G   9100  Ethernet21/1    down     down     N/A         N/A
 Ethernet21               41      25G   9100  Ethernet22/1    down     down     N/A         N/A
 Ethernet22               42      25G   9100  Ethernet23/1    down     down     N/A         N/A
 Ethernet23               43      25G   9100  Ethernet24/1    down     down     N/A         N/A
 Ethernet24               48      25G   9100  Ethernet25/1    down     down     N/A         N/A
 Ethernet25               49      25G   9100  Ethernet26/1    down     down     N/A         N/A
 Ethernet26               50      25G   9100  Ethernet27/1    down     down     N/A         N/A
 Ethernet27               51      25G   9100  Ethernet28/1    down     down     N/A         N/A
 Ethernet28               56      25G   9100  Ethernet29/1    down     down     N/A         N/A
 Ethernet29               57      25G   9100  Ethernet30/1    down     down     N/A         N/A
 Ethernet30               58      25G   9100  Ethernet31/1    down     down     N/A         N/A
 Ethernet31               59      25G   9100  Ethernet32/1    down     down     N/A         N/A
 Ethernet32               64      25G   9100  Ethernet33/1    down     down     N/A         N/A
 Ethernet33               65      25G   9100  Ethernet34/1    down     down     N/A         N/A
 Ethernet34               66      25G   9100  Ethernet35/1    down     down     N/A         N/A
 Ethernet35               67      25G   9100  Ethernet36/1    down     down     N/A         N/A
 Ethernet36               68      25G   9100  Ethernet37/1    down     down     N/A         N/A
 Ethernet37               69      25G   9100  Ethernet38/1    down     down     N/A         N/A
 Ethernet38               70      25G   9100  Ethernet39/1    down     down     N/A         N/A
 Ethernet39               71      25G   9100  Ethernet40/1    down     down     N/A         N/A
 Ethernet40               72      25G   9100  Ethernet41/1    down     down     N/A         N/A
 Ethernet41               73      25G   9100  Ethernet42/1    down     down     N/A         N/A
 Ethernet42               74      25G   9100  Ethernet43/1    down     down     N/A         N/A
 Ethernet43               75      25G   9100  Ethernet44/1    down     down     N/A         N/A
 Ethernet44               76      25G   9100  Ethernet45/1    down     down     N/A         N/A
 Ethernet45               77      25G   9100  Ethernet46/1    down     down     N/A         N/A
 Ethernet46               78      25G   9100  Ethernet47/1    down     down     N/A         N/A
 Ethernet47               79      25G   9100  Ethernet48/1    down     down     N/A         N/A
 Ethernet48      84,85,86,87     100G   9100  Ethernet49/1      up       up  QSFP28         N/A
 Ethernet49      80,81,82,83     100G   9100  Ethernet50/1      up       up  QSFP28         N/A
 Ethernet50      92,93,94,95     100G   9100  Ethernet51/1    down     down     N/A         N/A
 Ethernet51      88,89,90,91     100G   9100  Ethernet52/1    down     down     N/A         N/A
 Ethernet52  108,109,110,111     100G   9100  Ethernet53/1    down     down     N/A         N/A
 Ethernet53  104,105,106,107     100G   9100  Ethernet54/1    down     down     N/A         N/A
 Ethernet54  116,117,118,119     100G   9100  Ethernet55/1    down     down     N/A         N/A
 Ethernet55  112,113,114,115     100G   9100  Ethernet56/1    down     down     N/A         N/A
root@switch1:~#
2019-04-18 16:54:31 +00:00

452 lines
13 KiB
C
Executable File

/*
* A hwmon driver for the CIG cs6436-56P fan
*
* Copyright (C) 2018 Cambridge, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/sysfs.h>
#include <linux/slab.h>
#include <linux/dmi.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
#include <linux/kthread.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#define FAN_SPEED_DUTY_TO_CPLD_STEP 10
static struct cs6436_56p_fan_data *cs6436_56p_fan_update_device(struct device *dev);
static ssize_t fan_show_value(struct device *dev, struct device_attribute *da, char *buf);
static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
const char *buf, size_t count);
extern int cig_cpld_write_register(u8 reg_off, u8 val);
extern int cig_cpld_read_register(u8 reg_off, u8 *val);
/* fan related data, the index should match sysfs_fan_attributes
*/
static const u8 fan_reg[] = {
0x41, /* fan enable/disable */
0x40, /* fan PWM(for all fan) */
0x42, /* front fan 1 speed(rpm) */
0x44, /* front fan 2 speed(rpm) */
0x46, /* front fan 3 speed(rpm) */
0x48, /* front fan 4 speed(rpm) */
0x4a, /* front fan 5 speed(rpm) */
0x43, /* rear fan 1 speed(rpm) */
0x45, /* rear fan 2 speed(rpm) */
0x47, /* rear fan 3 speed(rpm) */
0x49, /* rear fan 4 speed(rpm) */
0x4b, /* rear fan 5 speed(rpm) */
};
/* Each client has this additional data */
struct cs6436_56p_fan_data {
struct platform_device *pdev;
struct device *hwmon_dev;
struct mutex update_lock;
char valid; /* != 0 if registers are valid */
unsigned long last_updated; /* In jiffies */
u8 reg_val[ARRAY_SIZE(fan_reg)]; /* Register value */
};
static struct cs6436_56p_fan_data *fan_data = NULL;
enum fan_id {
FAN1_ID,
FAN2_ID,
FAN3_ID,
FAN4_ID,
FAN5_ID,
};
enum sysfs_fan_attributes {
FAN_STATE_REG,
FAN_DUTY_CYCLE_PERCENTAGE, /* Only one CPLD register to control duty cycle for all fans */
FAN1_FRONT_SPEED_RPM,
FAN2_FRONT_SPEED_RPM,
FAN3_FRONT_SPEED_RPM,
FAN4_FRONT_SPEED_RPM,
FAN5_FRONT_SPEED_RPM,
FAN1_REAR_SPEED_RPM,
FAN2_REAR_SPEED_RPM,
FAN3_REAR_SPEED_RPM,
FAN4_REAR_SPEED_RPM,
FAN5_REAR_SPEED_RPM,
FAN1_STATE,
FAN2_STATE,
FAN3_STATE,
FAN4_STATE,
FAN5_STATE,
FAN1_FAULT,
FAN2_FAULT,
FAN3_FAULT,
FAN4_FAULT,
FAN5_FAULT,
};
/* Define attributes
*/
#define DECLARE_FAN_STATE_SENSOR_DEV_ATTR(index) \
static SENSOR_DEVICE_ATTR(fan##index##_state, S_IRUGO, fan_show_value, NULL, FAN##index##_STATE)
#define DECLARE_FAN_STATE_ATTR(index) &sensor_dev_attr_fan##index##_state.dev_attr.attr
#define DECLARE_FAN_FAULT_SENSOR_DEV_ATTR(index) \
static SENSOR_DEVICE_ATTR(fan##index##_fault, S_IRUGO, fan_show_value, NULL, FAN##index##_FAULT)
#define DECLARE_FAN_FAULT_ATTR(index) &sensor_dev_attr_fan##index##_fault.dev_attr.attr
#define DECLARE_FAN_DUTY_CYCLE_SENSOR_DEV_ATTR(index) \
static SENSOR_DEVICE_ATTR(fan##index##_duty_cycle_percentage, S_IWUSR | S_IRUGO, fan_show_value, set_duty_cycle, FAN##index##_DUTY_CYCLE_PERCENTAGE)
#define DECLARE_FAN_DUTY_CYCLE_ATTR(index) &sensor_dev_attr_fan##index##_duty_cycle_percentage.dev_attr.attr
#define DECLARE_FAN_SPEED_RPM_SENSOR_DEV_ATTR(index) \
static SENSOR_DEVICE_ATTR(fan##index##_front_speed_rpm, S_IRUGO, fan_show_value, NULL, FAN##index##_FRONT_SPEED_RPM);\
static SENSOR_DEVICE_ATTR(fan##index##_rear_speed_rpm, S_IRUGO, fan_show_value, NULL, FAN##index##_REAR_SPEED_RPM)
#define DECLARE_FAN_SPEED_RPM_ATTR(index) &sensor_dev_attr_fan##index##_front_speed_rpm.dev_attr.attr, \
&sensor_dev_attr_fan##index##_rear_speed_rpm.dev_attr.attr
/* 5 fan state attributes in this platform */
DECLARE_FAN_STATE_SENSOR_DEV_ATTR(1);
DECLARE_FAN_STATE_SENSOR_DEV_ATTR(2);
DECLARE_FAN_STATE_SENSOR_DEV_ATTR(3);
DECLARE_FAN_STATE_SENSOR_DEV_ATTR(4);
DECLARE_FAN_STATE_SENSOR_DEV_ATTR(5);
/* 5 fan fault attributes in this platform */
DECLARE_FAN_FAULT_SENSOR_DEV_ATTR(1);
DECLARE_FAN_FAULT_SENSOR_DEV_ATTR(2);
DECLARE_FAN_FAULT_SENSOR_DEV_ATTR(3);
DECLARE_FAN_FAULT_SENSOR_DEV_ATTR(4);
DECLARE_FAN_FAULT_SENSOR_DEV_ATTR(5);
/* 5 fan speed(rpm) attributes in this platform */
DECLARE_FAN_SPEED_RPM_SENSOR_DEV_ATTR(1);
DECLARE_FAN_SPEED_RPM_SENSOR_DEV_ATTR(2);
DECLARE_FAN_SPEED_RPM_SENSOR_DEV_ATTR(3);
DECLARE_FAN_SPEED_RPM_SENSOR_DEV_ATTR(4);
DECLARE_FAN_SPEED_RPM_SENSOR_DEV_ATTR(5);
/* 1 fan duty cycle attribute in this platform */
DECLARE_FAN_DUTY_CYCLE_SENSOR_DEV_ATTR();
static struct attribute *cs6436_56p_fan_attributes[] = {
/* fan related attributes */
DECLARE_FAN_STATE_ATTR(1),
DECLARE_FAN_STATE_ATTR(2),
DECLARE_FAN_STATE_ATTR(3),
DECLARE_FAN_STATE_ATTR(4),
DECLARE_FAN_STATE_ATTR(5),
DECLARE_FAN_FAULT_ATTR(1),
DECLARE_FAN_FAULT_ATTR(2),
DECLARE_FAN_FAULT_ATTR(3),
DECLARE_FAN_FAULT_ATTR(4),
DECLARE_FAN_FAULT_ATTR(5),
DECLARE_FAN_SPEED_RPM_ATTR(1),
DECLARE_FAN_SPEED_RPM_ATTR(2),
DECLARE_FAN_SPEED_RPM_ATTR(3),
DECLARE_FAN_SPEED_RPM_ATTR(4),
DECLARE_FAN_SPEED_RPM_ATTR(5),
DECLARE_FAN_DUTY_CYCLE_ATTR(),
NULL
};
#define FAN_MAX_DUTY_CYCLE 100
#define FAN_REG_VAL_TO_SPEED_RPM_STEP 100
/* fan utility functions
*/
static u32 reg_val_to_duty_cycle(u8 reg_val)
{
if (reg_val
== 0xFF) {
return 100;
}
return ((u32)(reg_val) * 100)/ 255;
}
static u8 duty_cycle_to_reg_val(u8 duty_cycle)
{
if (duty_cycle >= FAN_MAX_DUTY_CYCLE) {
return 0xFF;
}
return 255 / 10 * (duty_cycle / 10);
}
static u32 reg_val_to_speed_rpm(u8 reg_val)
{
return (u32)reg_val * FAN_REG_VAL_TO_SPEED_RPM_STEP;
}
static u8 reg_val_to_is_state(u8 reg_val, enum fan_id id)
{
u8 mask = (1 << id);
reg_val &= mask;
return reg_val ? 0 : 1;
}
static u8 is_fan_fault(struct cs6436_56p_fan_data *data, enum fan_id id)
{
u8 ret = 1;
int front_fan_index = FAN1_FRONT_SPEED_RPM + id;
int rear_fan_index = FAN1_REAR_SPEED_RPM + id;
/* Check if the speed of front or rear fan is ZERO,
*/
if (reg_val_to_speed_rpm(data->reg_val[front_fan_index]) &&
reg_val_to_speed_rpm(data->reg_val[rear_fan_index])) {
ret = 0;
}
return ret;
}
static ssize_t set_duty_cycle(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{
int error, value;
error = kstrtoint(buf, 10, &value);
if (error)
return error;
if (value <= 0 || value > FAN_MAX_DUTY_CYCLE)
return -EINVAL;
cig_cpld_write_register(fan_reg[FAN_DUTY_CYCLE_PERCENTAGE], duty_cycle_to_reg_val(value));
return count;
}
static ssize_t fan_show_value(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
cs6436_56p_fan_update_device(dev);
struct cs6436_56p_fan_data *data = fan_data;
ssize_t ret = 0;
if (data->valid) {
switch (attr->index) {
case FAN1_STATE:
case FAN2_STATE:
case FAN3_STATE:
case FAN4_STATE:
case FAN5_STATE:
//printk("FAN_STATE_REG: 0x%x\n", data->reg_val[FAN_STATE_REG]);
//printk("index: %d\n", attr->index);
ret = sprintf(buf, "%d\n",
reg_val_to_is_state(data->reg_val[FAN_STATE_REG],
attr->index - FAN1_STATE));
break;
case FAN_DUTY_CYCLE_PERCENTAGE:
{
u32 duty_cycle = reg_val_to_duty_cycle(data->reg_val[FAN_DUTY_CYCLE_PERCENTAGE]);
ret = sprintf(buf, "%u\n", duty_cycle);
break;
}
case FAN1_FRONT_SPEED_RPM:
case FAN2_FRONT_SPEED_RPM:
case FAN3_FRONT_SPEED_RPM:
case FAN4_FRONT_SPEED_RPM:
case FAN5_FRONT_SPEED_RPM:
case FAN1_REAR_SPEED_RPM:
case FAN2_REAR_SPEED_RPM:
case FAN3_REAR_SPEED_RPM:
case FAN4_REAR_SPEED_RPM:
case FAN5_REAR_SPEED_RPM:
// printk("FAN_seed_REG: 0x%x\n", data->reg_val[attr->index]);
// printk("index: %d\n", attr->index);
ret = sprintf(buf, "%u\n", reg_val_to_speed_rpm(data->reg_val[attr->index]));
break;
case FAN1_FAULT:
case FAN2_FAULT:
case FAN3_FAULT:
case FAN4_FAULT:
case FAN5_FAULT:
ret = sprintf(buf, "%d\n", is_fan_fault(data, attr->index - FAN1_FAULT));
break;
default:
break;
}
}
return ret;
}
static const struct attribute_group cs6436_56p_fan_group = {
.attrs = cs6436_56p_fan_attributes,
};
static struct cs6436_56p_fan_data *cs6436_56p_fan_update_device(struct device *dev)
{
struct cs6436_56p_fan_data *data = fan_data;
mutex_lock(&data->update_lock);
if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
!data->valid) {
int i;
data->valid = 0;
/* Update fan data
*/
for (i = 0; i < ARRAY_SIZE(data->reg_val); i++) {
u8 status;
(void)cig_cpld_read_register(fan_reg[i], &status);
if (status < 0) {
data->valid = 0;
mutex_unlock(&data->update_lock);
return data;
}
else {
data->reg_val[i] = status;
}
}
data->last_updated = jiffies;
data->valid = 1;
}
mutex_unlock(&data->update_lock);
return data;
}
static int cs6436_56p_fan_probe(struct platform_device *pdev)
{
int status = -1;
/* Register sysfs hooks */
status = sysfs_create_group(&pdev->dev.kobj, &cs6436_56p_fan_group);
if (status) {
goto exit;
}
fan_data->hwmon_dev = hwmon_device_register(&pdev->dev);
if (IS_ERR(fan_data->hwmon_dev)) {
status = PTR_ERR(fan_data->hwmon_dev);
goto exit_remove;
}
dev_info(&pdev->dev, "cs6436_56p_fan\n");
return 0;
exit_remove:
sysfs_remove_group(&pdev->dev.kobj, &cs6436_56p_fan_group);
exit:
return status;
}
static int cs6436_56p_fan_remove(struct platform_device *pdev)
{
hwmon_device_unregister(fan_data->hwmon_dev);
sysfs_remove_group(&fan_data->pdev->dev.kobj, &cs6436_56p_fan_group);
return 0;
}
#define DRVNAME "cs6436_56p_fan"
static struct platform_driver cs6436_56p_fan_driver = {
.probe = cs6436_56p_fan_probe,
.remove = cs6436_56p_fan_remove,
.driver = {
.name = DRVNAME,
.owner = THIS_MODULE,
},
};
static int __init cs6436_56p_fan_init(void)
{
int ret;
cig_cpld_write_register(0x40, duty_cycle_to_reg_val(50));
ret = platform_driver_register(&cs6436_56p_fan_driver);
if (ret < 0) {
goto exit;
}
fan_data = kzalloc(sizeof(struct cs6436_56p_fan_data), GFP_KERNEL);
if (!fan_data) {
ret = -ENOMEM;
platform_driver_unregister(&cs6436_56p_fan_driver);
goto exit;
}
mutex_init(&fan_data->update_lock);
fan_data->valid = 0;
fan_data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0);
if (IS_ERR(fan_data->pdev)) {
ret = PTR_ERR(fan_data->pdev);
platform_driver_unregister(&cs6436_56p_fan_driver);
kfree(fan_data);
goto exit;
}
exit:
return ret;
}
static void __exit cs6436_56p_fan_exit(void)
{
platform_device_unregister(fan_data->pdev);
platform_driver_unregister(&cs6436_56p_fan_driver);
kfree(fan_data);
}
MODULE_AUTHOR("CIG");
MODULE_DESCRIPTION("cs6436_56p_fan driver");
MODULE_LICENSE("GPL");
module_init(cs6436_56p_fan_init);
module_exit(cs6436_56p_fan_exit);
MODULE_AUTHOR("Zhang Peng <zhangpeng@cigtech.com>");
MODULE_DESCRIPTION("cs6436_56p_fan driver");
MODULE_LICENSE("GPL");