[as7816-64x] Replace psu driver with at24

This commit is contained in:
Brandon Chuang
2018-03-29 11:41:07 +08:00
parent 95615b571e
commit 9a11cd0281
6 changed files with 75 additions and 270 deletions

View File

@@ -45,6 +45,8 @@ struct cpld_client_node {
#define I2C_RW_RETRY_COUNT 10
#define I2C_RW_RETRY_INTERVAL 60 /* ms */
static ssize_t show_psu(struct device *dev, struct device_attribute *da,
char *buf);
static ssize_t show_present(struct device *dev, struct device_attribute *da,
char *buf);
static ssize_t show_present_all(struct device *dev, struct device_attribute *da,
@@ -66,6 +68,8 @@ struct as7816_64x_cpld_data {
static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
#define TRANSCEIVER_PRESENT_ATTR_ID(index) MODULE_PRESENT_##index
#define PSU_PRESENT_ATTR_ID(index) PSU##index##_PRESENT
#define PSU_POWERGOOD_ATTR_ID(index) PSU##index##_POWER_GOOD
enum as7816_64x_cpld_sysfs_attributes {
CPLD_VERSION,
@@ -136,6 +140,12 @@ enum as7816_64x_cpld_sysfs_attributes {
TRANSCEIVER_PRESENT_ATTR_ID(62),
TRANSCEIVER_PRESENT_ATTR_ID(63),
TRANSCEIVER_PRESENT_ATTR_ID(64),
/* psu attributes */
PSU_PRESENT_ATTR_ID(1),
PSU_PRESENT_ATTR_ID(2),
PSU_POWERGOOD_ATTR_ID(1),
PSU_POWERGOOD_ATTR_ID(2),
};
/* sysfs attributes for hwmon
@@ -146,6 +156,14 @@ enum as7816_64x_cpld_sysfs_attributes {
static SENSOR_DEVICE_ATTR(module_present_##index, S_IRUGO, show_present, NULL, MODULE_PRESENT_##index)
#define DECLARE_TRANSCEIVER_ATTR(index) &sensor_dev_attr_module_present_##index.dev_attr.attr
/* psu attributes */
#define DECLARE_PSU_SENSOR_DEVICE_ATTR(index) \
static SENSOR_DEVICE_ATTR(psu##index##_present, S_IRUGO, show_psu, NULL, PSU##index##_PRESENT); \
static SENSOR_DEVICE_ATTR(psu##index##_power_good, S_IRUGO, show_psu, NULL, PSU##index##_POWER_GOOD);
#define DECLARE_PSU_ATTR(index) \
&sensor_dev_attr_psu##index##_present.dev_attr.attr, \
&sensor_dev_attr_psu##index##_power_good.dev_attr.attr
static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, CPLD_VERSION);
static SENSOR_DEVICE_ATTR(access, S_IWUSR, NULL, access, ACCESS);
/* transceiver attributes */
@@ -215,6 +233,10 @@ DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(62);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(63);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(64);
/* psu attributes*/
DECLARE_PSU_SENSOR_DEVICE_ATTR(1);
DECLARE_PSU_SENSOR_DEVICE_ATTR(2);
static struct attribute *as7816_64x_cpld_attributes[] = {
&sensor_dev_attr_version.dev_attr.attr,
&sensor_dev_attr_access.dev_attr.attr,
@@ -284,6 +306,10 @@ static struct attribute *as7816_64x_cpld_attributes[] = {
DECLARE_TRANSCEIVER_ATTR(62),
DECLARE_TRANSCEIVER_ATTR(63),
DECLARE_TRANSCEIVER_ATTR(64),
/* psu attributes*/
DECLARE_PSU_ATTR(1),
DECLARE_PSU_ATTR(2),
NULL
};
@@ -291,6 +317,42 @@ static const struct attribute_group as7816_64x_cpld_group = {
.attrs = as7816_64x_cpld_attributes,
};
static ssize_t show_psu(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct i2c_client *client = to_i2c_client(dev);
struct as7816_64x_cpld_data *data = i2c_get_clientdata(client);
int status = 0, value = 0;
mutex_lock(&data->update_lock);
status = as7816_64x_cpld_read_internal(client, 0x03);
if (unlikely(status < 0)) {
goto exit;
}
mutex_unlock(&data->update_lock);
switch (attr->index) {
case PSU1_PRESENT:
case PSU2_PRESENT:
value = !(status & BIT(attr->index - PSU1_PRESENT));
break;
case PSU1_POWER_GOOD:
case PSU2_POWER_GOOD:
value = !!(status & BIT(attr->index - PSU1_POWER_GOOD + 2));
break;
default:
return 0;
}
return sprintf(buf, "%d\n", value);
exit:
mutex_unlock(&data->update_lock);
return status;
}
static ssize_t show_present_all(struct device *dev, struct device_attribute *da,
char *buf)
{

View File

@@ -1,239 +0,0 @@
/*
* An hwmon driver for accton as7816_64x Power Module
*
* Copyright (C) 2014 Accton Technology Corporation.
* Brandon Chuang <brandon_chuang@accton.com.tw>
*
* Based on ad7414.c
* Copyright 2006 Stefan Roese <sr at denx.de>, DENX Software Engineering
*
* 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/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/delay.h>
#include <linux/dmi.h>
#define PSU_STATUS_I2C_ADDR 0x60
#define PSU_STATUS_I2C_REG_OFFSET 0x03
#define IS_POWER_GOOD(id, value) (!!(value & BIT(2+id)))
#define IS_PRESENT(id, value) (!(value & BIT(id)))
static ssize_t show_status(struct device *dev, struct device_attribute *da, char *buf);
static struct as7816_64x_psu_data *as7816_64x_psu_update_device(struct device *dev);
extern int as7816_64x_cpld_read (unsigned short cpld_addr, u8 reg);
/* Addresses scanned
*/
static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
/* Each client has this additional data
*/
struct as7816_64x_psu_data {
struct device *hwmon_dev;
struct mutex update_lock;
char valid; /* !=0 if registers are valid */
unsigned long last_updated; /* In jiffies */
u8 index; /* PSU index */
u8 status; /* Status(present/power_good) register read from CPLD */
};
enum as7816_64x_psu_sysfs_attributes {
PSU_PRESENT,
PSU_POWER_GOOD
};
/* sysfs attributes for hwmon
*/
static SENSOR_DEVICE_ATTR(psu_present, S_IRUGO, show_status, NULL, PSU_PRESENT);
static SENSOR_DEVICE_ATTR(psu_power_good, S_IRUGO, show_status, NULL, PSU_POWER_GOOD);
static struct attribute *as7816_64x_psu_attributes[] = {
&sensor_dev_attr_psu_present.dev_attr.attr,
&sensor_dev_attr_psu_power_good.dev_attr.attr,
NULL
};
static ssize_t show_status(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct as7816_64x_psu_data *data = as7816_64x_psu_update_device(dev);
u8 status = 0;
if (!data->valid) {
return -EIO;
}
if (attr->index == PSU_PRESENT) {
status = IS_PRESENT(data->index, data->status);
}
else { /* PSU_POWER_GOOD */
status = IS_POWER_GOOD(data->index, data->status);
}
return sprintf(buf, "%d\n", status);
}
static const struct attribute_group as7816_64x_psu_group = {
.attrs = as7816_64x_psu_attributes,
};
static int as7816_64x_psu_probe(struct i2c_client *client,
const struct i2c_device_id *dev_id)
{
struct as7816_64x_psu_data *data;
int status;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
status = -EIO;
goto exit;
}
data = kzalloc(sizeof(struct as7816_64x_psu_data), GFP_KERNEL);
if (!data) {
status = -ENOMEM;
goto exit;
}
i2c_set_clientdata(client, data);
data->valid = 0;
data->index = dev_id->driver_data;
mutex_init(&data->update_lock);
dev_info(&client->dev, "chip found\n");
/* Register sysfs hooks */
status = sysfs_create_group(&client->dev.kobj, &as7816_64x_psu_group);
if (status) {
goto exit_free;
}
data->hwmon_dev = hwmon_device_register(&client->dev);
if (IS_ERR(data->hwmon_dev)) {
status = PTR_ERR(data->hwmon_dev);
goto exit_remove;
}
dev_info(&client->dev, "%s: psu '%s'\n",
dev_name(data->hwmon_dev), client->name);
return 0;
exit_remove:
sysfs_remove_group(&client->dev.kobj, &as7816_64x_psu_group);
exit_free:
kfree(data);
exit:
return status;
}
static int as7816_64x_psu_remove(struct i2c_client *client)
{
struct as7816_64x_psu_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &as7816_64x_psu_group);
kfree(data);
return 0;
}
enum psu_index
{
as7816_64x_psu1,
as7816_64x_psu2
};
static const struct i2c_device_id as7816_64x_psu_id[] = {
{ "as7816_64x_psu1", as7816_64x_psu1 },
{ "as7816_64x_psu2", as7816_64x_psu2 },
{}
};
MODULE_DEVICE_TABLE(i2c, as7816_64x_psu_id);
static struct i2c_driver as7816_64x_psu_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "as7816_64x_psu",
},
.probe = as7816_64x_psu_probe,
.remove = as7816_64x_psu_remove,
.id_table = as7816_64x_psu_id,
.address_list = normal_i2c,
};
static struct as7816_64x_psu_data *as7816_64x_psu_update_device(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct as7816_64x_psu_data *data = i2c_get_clientdata(client);
mutex_lock(&data->update_lock);
if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
|| !data->valid) {
int status;
data->valid = 0;
dev_dbg(&client->dev, "Starting as7816_64x update\n");
/* Read psu status */
status = as7816_64x_cpld_read(PSU_STATUS_I2C_ADDR, PSU_STATUS_I2C_REG_OFFSET);
if (status < 0) {
dev_dbg(&client->dev, "cpld reg (0x%x) err %d\n", PSU_STATUS_I2C_ADDR, status);
goto exit;
}
else {
data->status = status;
}
data->last_updated = jiffies;
data->valid = 1;
}
exit:
mutex_unlock(&data->update_lock);
return data;
}
static int __init as7816_64x_psu_init(void)
{
return i2c_add_driver(&as7816_64x_psu_driver);
}
static void __exit as7816_64x_psu_exit(void)
{
i2c_del_driver(&as7816_64x_psu_driver);
}
module_init(as7816_64x_psu_init);
module_exit(as7816_64x_psu_exit);
MODULE_AUTHOR("Brandon Chuang <brandon_chuang@accton.com.tw>");
MODULE_DESCRIPTION("as7816_64x_psu driver");
MODULE_LICENSE("GPL");

View File

@@ -68,9 +68,8 @@ psu_type_t psu_type_get(int id, char* modelname, int modelname_len)
/* Check if the psu is power good
*/
prefix = (id == PSU1_ID) ? PSU1_AC_EEPROM_PREFIX : PSU2_AC_EEPROM_PREFIX;
if (onlp_file_read_int(&value, "%s%s", prefix, "psu_power_good") < 0) {
AIM_LOG_ERROR("Unable to read status from file(%s%s)\r\n", prefix, "psu_power_good");
if (onlp_file_read_int(&value, PSU_POWERGOOD_FORMAT, id) < 0) {
AIM_LOG_ERROR("Unable to read present status from PSU(%d)\r\n", index);
return ONLP_STATUS_E_INTERNAL;
}

View File

@@ -42,11 +42,8 @@
#define PSU1_AC_PMBUS_NODE(node) PSU1_AC_PMBUS_PREFIX#node
#define PSU2_AC_PMBUS_NODE(node) PSU2_AC_PMBUS_PREFIX#node
#define PSU1_AC_EEPROM_PREFIX "/sys/bus/i2c/devices/10-0053/"
#define PSU2_AC_EEPROM_PREFIX "/sys/bus/i2c/devices/9-0050/"
#define PSU1_AC_EEPROM_NODE(node) PSU1_AC_EEPROM_PREFIX#node
#define PSU2_AC_EEPROM_NODE(node) PSU2_AC_EEPROM_PREFIX#node
#define PSU_PRESENT_FORMAT "/sys/bus/i2c/devices/19-0060/psu%d_present"
#define PSU_POWERGOOD_FORMAT "/sys/bus/i2c/devices/19-0060/psu%d_power_good"
#define FAN_BOARD_PATH "/sys/bus/i2c/devices/17-0068/"
#define FAN_NODE(node) FAN_BOARD_PATH#node

View File

@@ -37,21 +37,6 @@
} \
} while(0)
static int
psu_status_info_get(int id, char *node, int *value)
{
char *prefix = NULL;
*value = 0;
prefix = (id == PSU1_ID) ? PSU1_AC_EEPROM_PREFIX : PSU2_AC_EEPROM_PREFIX;
if (onlp_file_read_int(value, "%s%s", prefix, node) < 0) {
AIM_LOG_ERROR("Unable to read status from file(%s%s)\r\n", prefix, node);
return ONLP_STATUS_E_INTERNAL;
}
return 0;
}
int
onlp_psui_init(void)
{
@@ -124,9 +109,9 @@ onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info)
memset(info, 0, sizeof(onlp_psu_info_t));
*info = pinfo[index]; /* Set the onlp_oid_hdr_t */
/* Get the present state */
if (psu_status_info_get(index, "psu_present", &val) != 0) {
printf("Unable to read PSU(%d) node(psu_present)\r\n", index);
if (onlp_file_read_int(&val, PSU_PRESENT_FORMAT, index) < 0) {
AIM_LOG_ERROR("Unable to read present status from PSU(%d)\r\n", index);
return ONLP_STATUS_E_INTERNAL;
}
if (val != PSU_STATUS_PRESENT) {
@@ -137,8 +122,9 @@ onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info)
/* Get power good status */
if (psu_status_info_get(index, "psu_power_good", &val) != 0) {
printf("Unable to read PSU(%d) node(psu_power_good)\r\n", index);
if (onlp_file_read_int(&val, PSU_POWERGOOD_FORMAT, index) < 0) {
AIM_LOG_ERROR("Unable to read power status from PSU(%d)\r\n", index);
return ONLP_STATUS_E_INTERNAL;
}
if (val != PSU_STATUS_POWER_GOOD) {

View File

@@ -11,7 +11,7 @@ class OnlPlatform_x86_64_accton_as7816_64x_r0(OnlPlatformAccton,
self.insmod('optoe')
self.insmod('ym2651y')
self.insmod('accton_i2c_cpld')
for m in [ 'fan', 'cpld1', 'psu', 'leds' ]:
for m in [ 'fan', 'cpld1', 'leds' ]:
self.insmod("x86-64-accton-as7816-64x-%s.ko" % m)
########### initialize I2C bus 0 ###########
@@ -25,11 +25,11 @@ class OnlPlatform_x86_64_accton_as7816_64x_r0(OnlPlatformAccton,
('pca9548', 0x73, 1),
# initiate PSU-1
('as7816_64x_psu1', 0x53, 10),
('24c02', 0x53, 10),
('ym2851', 0x5b, 10),
# initiate PSU-2
('as7816_64x_psu2', 0x50, 9),
('24c02', 0x50, 9),
('ym2851', 0x58, 9),
# initiate chassis fan