[as7816-64x] Add support for OOM driver

This commit is contained in:
Brandon Chuang
2018-01-05 10:26:36 +08:00
parent 6359a48a65
commit b9611cfe0d
6 changed files with 839 additions and 1667 deletions

View File

@@ -0,0 +1,670 @@
/*
* A hwmon driver for the as7816_64x_cpld
*
* Copyright (C) 2018 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/list.h>
static LIST_HEAD(cpld_client_list);
static struct mutex list_lock;
struct cpld_client_node {
struct i2c_client *client;
struct list_head list;
};
#define I2C_RW_RETRY_COUNT 10
#define I2C_RW_RETRY_INTERVAL 60 /* ms */
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,
char *buf);
static ssize_t access(struct device *dev, struct device_attribute *da,
const char *buf, size_t count);
static ssize_t show_version(struct device *dev, struct device_attribute *da,
char *buf);
static int as7816_64x_cpld_read_internal(struct i2c_client *client, u8 reg);
static int as7816_64x_cpld_write_internal(struct i2c_client *client, u8 reg, u8 value);
struct as7816_64x_cpld_data {
struct device *hwmon_dev;
struct mutex update_lock;
};
/* Addresses scanned for as7816_64x_cpld
*/
static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
#define TRANSCEIVER_PRESENT_ATTR_ID(index) MODULE_PRESENT_##index
enum as7816_64x_cpld_sysfs_attributes {
CPLD_VERSION,
ACCESS,
MODULE_PRESENT_ALL,
/* transceiver attributes */
TRANSCEIVER_PRESENT_ATTR_ID(1),
TRANSCEIVER_PRESENT_ATTR_ID(2),
TRANSCEIVER_PRESENT_ATTR_ID(3),
TRANSCEIVER_PRESENT_ATTR_ID(4),
TRANSCEIVER_PRESENT_ATTR_ID(5),
TRANSCEIVER_PRESENT_ATTR_ID(6),
TRANSCEIVER_PRESENT_ATTR_ID(7),
TRANSCEIVER_PRESENT_ATTR_ID(8),
TRANSCEIVER_PRESENT_ATTR_ID(9),
TRANSCEIVER_PRESENT_ATTR_ID(10),
TRANSCEIVER_PRESENT_ATTR_ID(11),
TRANSCEIVER_PRESENT_ATTR_ID(12),
TRANSCEIVER_PRESENT_ATTR_ID(13),
TRANSCEIVER_PRESENT_ATTR_ID(14),
TRANSCEIVER_PRESENT_ATTR_ID(15),
TRANSCEIVER_PRESENT_ATTR_ID(16),
TRANSCEIVER_PRESENT_ATTR_ID(17),
TRANSCEIVER_PRESENT_ATTR_ID(18),
TRANSCEIVER_PRESENT_ATTR_ID(19),
TRANSCEIVER_PRESENT_ATTR_ID(20),
TRANSCEIVER_PRESENT_ATTR_ID(21),
TRANSCEIVER_PRESENT_ATTR_ID(22),
TRANSCEIVER_PRESENT_ATTR_ID(23),
TRANSCEIVER_PRESENT_ATTR_ID(24),
TRANSCEIVER_PRESENT_ATTR_ID(25),
TRANSCEIVER_PRESENT_ATTR_ID(26),
TRANSCEIVER_PRESENT_ATTR_ID(27),
TRANSCEIVER_PRESENT_ATTR_ID(28),
TRANSCEIVER_PRESENT_ATTR_ID(29),
TRANSCEIVER_PRESENT_ATTR_ID(30),
TRANSCEIVER_PRESENT_ATTR_ID(31),
TRANSCEIVER_PRESENT_ATTR_ID(32),
TRANSCEIVER_PRESENT_ATTR_ID(33),
TRANSCEIVER_PRESENT_ATTR_ID(34),
TRANSCEIVER_PRESENT_ATTR_ID(35),
TRANSCEIVER_PRESENT_ATTR_ID(36),
TRANSCEIVER_PRESENT_ATTR_ID(37),
TRANSCEIVER_PRESENT_ATTR_ID(38),
TRANSCEIVER_PRESENT_ATTR_ID(39),
TRANSCEIVER_PRESENT_ATTR_ID(40),
TRANSCEIVER_PRESENT_ATTR_ID(41),
TRANSCEIVER_PRESENT_ATTR_ID(42),
TRANSCEIVER_PRESENT_ATTR_ID(43),
TRANSCEIVER_PRESENT_ATTR_ID(44),
TRANSCEIVER_PRESENT_ATTR_ID(45),
TRANSCEIVER_PRESENT_ATTR_ID(46),
TRANSCEIVER_PRESENT_ATTR_ID(47),
TRANSCEIVER_PRESENT_ATTR_ID(48),
TRANSCEIVER_PRESENT_ATTR_ID(49),
TRANSCEIVER_PRESENT_ATTR_ID(50),
TRANSCEIVER_PRESENT_ATTR_ID(51),
TRANSCEIVER_PRESENT_ATTR_ID(52),
TRANSCEIVER_PRESENT_ATTR_ID(53),
TRANSCEIVER_PRESENT_ATTR_ID(54),
TRANSCEIVER_PRESENT_ATTR_ID(55),
TRANSCEIVER_PRESENT_ATTR_ID(56),
TRANSCEIVER_PRESENT_ATTR_ID(57),
TRANSCEIVER_PRESENT_ATTR_ID(58),
TRANSCEIVER_PRESENT_ATTR_ID(59),
TRANSCEIVER_PRESENT_ATTR_ID(60),
TRANSCEIVER_PRESENT_ATTR_ID(61),
TRANSCEIVER_PRESENT_ATTR_ID(62),
TRANSCEIVER_PRESENT_ATTR_ID(63),
TRANSCEIVER_PRESENT_ATTR_ID(64),
};
/* sysfs attributes for hwmon
*/
/* transceiver attributes */
#define DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(index) \
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
static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, CPLD_VERSION);
static SENSOR_DEVICE_ATTR(access, S_IWUSR, NULL, access, ACCESS);
/* transceiver attributes */
static SENSOR_DEVICE_ATTR(module_present_all, S_IRUGO, show_present_all, NULL, MODULE_PRESENT_ALL);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(1);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(2);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(3);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(4);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(5);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(6);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(7);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(8);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(9);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(10);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(11);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(12);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(13);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(14);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(15);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(16);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(17);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(18);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(19);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(20);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(21);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(22);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(23);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(24);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(25);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(26);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(27);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(28);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(29);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(30);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(31);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(32);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(33);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(34);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(35);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(36);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(37);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(38);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(39);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(40);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(41);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(42);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(43);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(44);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(45);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(46);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(47);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(48);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(49);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(50);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(51);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(52);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(53);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(54);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(55);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(56);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(57);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(58);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(59);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(60);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(61);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(62);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(63);
DECLARE_TRANSCEIVER_SENSOR_DEVICE_ATTR(64);
static struct attribute *as7816_64x_cpld_attributes[] = {
&sensor_dev_attr_version.dev_attr.attr,
&sensor_dev_attr_access.dev_attr.attr,
/* transceiver attributes */
&sensor_dev_attr_module_present_all.dev_attr.attr,
DECLARE_TRANSCEIVER_ATTR(1),
DECLARE_TRANSCEIVER_ATTR(2),
DECLARE_TRANSCEIVER_ATTR(3),
DECLARE_TRANSCEIVER_ATTR(4),
DECLARE_TRANSCEIVER_ATTR(5),
DECLARE_TRANSCEIVER_ATTR(6),
DECLARE_TRANSCEIVER_ATTR(7),
DECLARE_TRANSCEIVER_ATTR(8),
DECLARE_TRANSCEIVER_ATTR(9),
DECLARE_TRANSCEIVER_ATTR(10),
DECLARE_TRANSCEIVER_ATTR(11),
DECLARE_TRANSCEIVER_ATTR(12),
DECLARE_TRANSCEIVER_ATTR(13),
DECLARE_TRANSCEIVER_ATTR(14),
DECLARE_TRANSCEIVER_ATTR(15),
DECLARE_TRANSCEIVER_ATTR(16),
DECLARE_TRANSCEIVER_ATTR(17),
DECLARE_TRANSCEIVER_ATTR(18),
DECLARE_TRANSCEIVER_ATTR(19),
DECLARE_TRANSCEIVER_ATTR(20),
DECLARE_TRANSCEIVER_ATTR(21),
DECLARE_TRANSCEIVER_ATTR(22),
DECLARE_TRANSCEIVER_ATTR(23),
DECLARE_TRANSCEIVER_ATTR(24),
DECLARE_TRANSCEIVER_ATTR(25),
DECLARE_TRANSCEIVER_ATTR(26),
DECLARE_TRANSCEIVER_ATTR(27),
DECLARE_TRANSCEIVER_ATTR(28),
DECLARE_TRANSCEIVER_ATTR(29),
DECLARE_TRANSCEIVER_ATTR(30),
DECLARE_TRANSCEIVER_ATTR(31),
DECLARE_TRANSCEIVER_ATTR(32),
DECLARE_TRANSCEIVER_ATTR(33),
DECLARE_TRANSCEIVER_ATTR(34),
DECLARE_TRANSCEIVER_ATTR(35),
DECLARE_TRANSCEIVER_ATTR(36),
DECLARE_TRANSCEIVER_ATTR(37),
DECLARE_TRANSCEIVER_ATTR(38),
DECLARE_TRANSCEIVER_ATTR(39),
DECLARE_TRANSCEIVER_ATTR(40),
DECLARE_TRANSCEIVER_ATTR(41),
DECLARE_TRANSCEIVER_ATTR(42),
DECLARE_TRANSCEIVER_ATTR(43),
DECLARE_TRANSCEIVER_ATTR(44),
DECLARE_TRANSCEIVER_ATTR(45),
DECLARE_TRANSCEIVER_ATTR(46),
DECLARE_TRANSCEIVER_ATTR(47),
DECLARE_TRANSCEIVER_ATTR(48),
DECLARE_TRANSCEIVER_ATTR(49),
DECLARE_TRANSCEIVER_ATTR(50),
DECLARE_TRANSCEIVER_ATTR(51),
DECLARE_TRANSCEIVER_ATTR(52),
DECLARE_TRANSCEIVER_ATTR(53),
DECLARE_TRANSCEIVER_ATTR(54),
DECLARE_TRANSCEIVER_ATTR(55),
DECLARE_TRANSCEIVER_ATTR(56),
DECLARE_TRANSCEIVER_ATTR(57),
DECLARE_TRANSCEIVER_ATTR(58),
DECLARE_TRANSCEIVER_ATTR(59),
DECLARE_TRANSCEIVER_ATTR(60),
DECLARE_TRANSCEIVER_ATTR(61),
DECLARE_TRANSCEIVER_ATTR(62),
DECLARE_TRANSCEIVER_ATTR(63),
DECLARE_TRANSCEIVER_ATTR(64),
NULL
};
static const struct attribute_group as7816_64x_cpld_group = {
.attrs = as7816_64x_cpld_attributes,
};
static ssize_t show_present_all(struct device *dev, struct device_attribute *da,
char *buf)
{
int i, status;
u8 values[8] = {0};
u8 regs[] = {0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77};
struct i2c_client *client = to_i2c_client(dev);
struct as7816_64x_cpld_data *data = i2c_get_clientdata(client);
mutex_lock(&data->update_lock);
for (i = 0; i < ARRAY_SIZE(regs); i++) {
status = as7816_64x_cpld_read_internal(client, regs[i]);
if (status < 0) {
goto exit;
}
values[i] = ~(u8)status;
}
mutex_unlock(&data->update_lock);
/* Return values 1 -> 64 in order */
return sprintf(buf, "%.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
values[0], values[1], values[2], values[3],
values[4], values[5], values[6], values[7]);
exit:
mutex_unlock(&data->update_lock);
return status;
}
static ssize_t show_present(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;
u8 reg = 0, mask = 0;
switch (attr->index) {
case MODULE_PRESENT_1 ... MODULE_PRESENT_8:
reg = 0x70;
mask = 0x1 << (attr->index - MODULE_PRESENT_1);
break;
case MODULE_PRESENT_9 ... MODULE_PRESENT_16:
reg = 0x71;
mask = 0x1 << (attr->index - MODULE_PRESENT_9);
break;
case MODULE_PRESENT_17 ... MODULE_PRESENT_24:
reg = 0x72;
mask = 0x1 << (attr->index - MODULE_PRESENT_17);
break;
case MODULE_PRESENT_25 ... MODULE_PRESENT_32:
reg = 0x73;
mask = 0x1 << (attr->index - MODULE_PRESENT_25);
break;
case MODULE_PRESENT_33 ... MODULE_PRESENT_40:
reg = 0x74;
mask = 0x1 << (attr->index - MODULE_PRESENT_33);
break;
case MODULE_PRESENT_41 ... MODULE_PRESENT_48:
reg = 0x75;
mask = 0x1 << (attr->index - MODULE_PRESENT_41);
break;
case MODULE_PRESENT_49 ... MODULE_PRESENT_56:
reg = 0x76;
mask = 0x1 << (attr->index - MODULE_PRESENT_49);
break;
case MODULE_PRESENT_57 ... MODULE_PRESENT_64:
reg = 0x77;
mask = 0x1 << (attr->index - MODULE_PRESENT_57);
break;
default:
return 0;
}
mutex_lock(&data->update_lock);
status = as7816_64x_cpld_read_internal(client, reg);
if (unlikely(status < 0)) {
goto exit;
}
mutex_unlock(&data->update_lock);
return sprintf(buf, "%d\n", !(status & mask));
exit:
mutex_unlock(&data->update_lock);
return status;
}
static ssize_t show_version(struct device *dev, struct device_attribute *da,
char *buf)
{
u8 reg = 0, mask = 0;
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;
switch (attr->index) {
case CPLD_VERSION:
reg = 0x1;
mask = 0xFF;
break;
default:
break;
}
mutex_lock(&data->update_lock);
status = as7816_64x_cpld_read_internal(client, reg);
if (unlikely(status < 0)) {
goto exit;
}
mutex_unlock(&data->update_lock);
return sprintf(buf, "%d\n", (status & mask));
exit:
mutex_unlock(&data->update_lock);
return status;
}
static ssize_t access(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{
int status;
u32 addr, val;
struct i2c_client *client = to_i2c_client(dev);
struct as7816_64x_cpld_data *data = i2c_get_clientdata(client);
if (sscanf(buf, "0x%x 0x%x", &addr, &val) != 2) {
return -EINVAL;
}
if (addr > 0xFF || val > 0xFF) {
return -EINVAL;
}
mutex_lock(&data->update_lock);
status = as7816_64x_cpld_write_internal(client, addr, val);
if (unlikely(status < 0)) {
goto exit;
}
mutex_unlock(&data->update_lock);
return count;
exit:
mutex_unlock(&data->update_lock);
return status;
}
static int as7816_64x_cpld_read_internal(struct i2c_client *client, u8 reg)
{
int status = 0, retry = I2C_RW_RETRY_COUNT;
while (retry) {
status = i2c_smbus_read_byte_data(client, reg);
if (unlikely(status < 0)) {
msleep(I2C_RW_RETRY_INTERVAL);
retry--;
continue;
}
break;
}
return status;
}
static int as7816_64x_cpld_write_internal(struct i2c_client *client, u8 reg, u8 value)
{
int status = 0, retry = I2C_RW_RETRY_COUNT;
while (retry) {
status = i2c_smbus_write_byte_data(client, reg, value);
if (unlikely(status < 0)) {
msleep(I2C_RW_RETRY_INTERVAL);
retry--;
continue;
}
break;
}
return status;
}
static void as7816_64x_cpld_add_client(struct i2c_client *client)
{
struct cpld_client_node *node = kzalloc(sizeof(struct cpld_client_node), GFP_KERNEL);
if (!node) {
dev_dbg(&client->dev, "Can't allocate cpld_client_node (0x%x)\n", client->addr);
return;
}
node->client = client;
mutex_lock(&list_lock);
list_add(&node->list, &cpld_client_list);
mutex_unlock(&list_lock);
}
static void as7816_64x_cpld_remove_client(struct i2c_client *client)
{
struct list_head *list_node = NULL;
struct cpld_client_node *cpld_node = NULL;
int found = 0;
mutex_lock(&list_lock);
list_for_each(list_node, &cpld_client_list)
{
cpld_node = list_entry(list_node, struct cpld_client_node, list);
if (cpld_node->client == client) {
found = 1;
break;
}
}
if (found) {
list_del(list_node);
kfree(cpld_node);
}
mutex_unlock(&list_lock);
}
static int as7816_64x_cpld_probe(struct i2c_client *client,
const struct i2c_device_id *dev_id)
{
int status;
struct as7816_64x_cpld_data *data = NULL;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
dev_dbg(&client->dev, "i2c_check_functionality failed (0x%x)\n", client->addr);
status = -EIO;
goto exit;
}
data = kzalloc(sizeof(struct as7816_64x_cpld_data), GFP_KERNEL);
if (!data) {
status = -ENOMEM;
goto exit;
}
i2c_set_clientdata(client, 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_cpld_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;
}
as7816_64x_cpld_add_client(client);
dev_info(&client->dev, "%s: cpld '%s'\n",
dev_name(data->hwmon_dev), client->name);
return 0;
exit_remove:
sysfs_remove_group(&client->dev.kobj, &as7816_64x_cpld_group);
exit_free:
kfree(data);
exit:
return status;
}
static int as7816_64x_cpld_remove(struct i2c_client *client)
{
struct as7816_64x_cpld_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &as7816_64x_cpld_group);
kfree(data);
as7816_64x_cpld_remove_client(client);
return 0;
}
int as7816_64x_cpld_read(unsigned short cpld_addr, u8 reg)
{
struct list_head *list_node = NULL;
struct cpld_client_node *cpld_node = NULL;
int ret = -EPERM;
mutex_lock(&list_lock);
list_for_each(list_node, &cpld_client_list)
{
cpld_node = list_entry(list_node, struct cpld_client_node, list);
if (cpld_node->client->addr == cpld_addr) {
ret = i2c_smbus_read_byte_data(cpld_node->client, reg);
break;
}
}
mutex_unlock(&list_lock);
return ret;
}
EXPORT_SYMBOL(as7816_64x_cpld_read);
int as7816_64x_cpld_write(unsigned short cpld_addr, u8 reg, u8 value)
{
struct list_head *list_node = NULL;
struct cpld_client_node *cpld_node = NULL;
int ret = -EIO;
mutex_lock(&list_lock);
list_for_each(list_node, &cpld_client_list)
{
cpld_node = list_entry(list_node, struct cpld_client_node, list);
if (cpld_node->client->addr == cpld_addr) {
ret = i2c_smbus_write_byte_data(cpld_node->client, reg, value);
break;
}
}
mutex_unlock(&list_lock);
return ret;
}
EXPORT_SYMBOL(as7816_64x_cpld_write);
static const struct i2c_device_id as7816_64x_cpld_id[] = {
{ "as7816_64x_cpld1", 0 },
{}
};
MODULE_DEVICE_TABLE(i2c, as7816_64x_cpld_id);
static struct i2c_driver as7816_64x_cpld_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "as7816_64x_cpld1",
},
.probe = as7816_64x_cpld_probe,
.remove = as7816_64x_cpld_remove,
.id_table = as7816_64x_cpld_id,
.address_list = normal_i2c,
};
static int __init as7816_64x_cpld_init(void)
{
mutex_init(&list_lock);
return i2c_add_driver(&as7816_64x_cpld_driver);
}
static void __exit as7816_64x_cpld_exit(void)
{
i2c_del_driver(&as7816_64x_cpld_driver);
}
module_init(as7816_64x_cpld_init);
module_exit(as7816_64x_cpld_exit);
MODULE_AUTHOR("Brandon Chuang <brandon_chuang@accton.com.tw>");
MODULE_DESCRIPTION("as7816_64x_cpld driver");
MODULE_LICENSE("GPL");

View File

@@ -30,8 +30,8 @@
#include <linux/slab.h>
#include <linux/dmi.h>
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);
extern int as7816_64x_cpld_read (unsigned short cpld_addr, u8 reg);
extern int as7816_64x_cpld_write(unsigned short cpld_addr, u8 reg, u8 value);
extern void led_classdev_unregister(struct led_classdev *led_cdev);
extern int led_classdev_register(struct device *parent, struct led_classdev *led_cdev);
@@ -185,12 +185,12 @@ static u8 led_light_mode_to_reg_val(enum led_type type,
static int as7816_64x_led_read_value(u8 reg)
{
return accton_i2c_cpld_read(LED_CNTRLER_I2C_ADDRESS, reg);
return as7816_64x_cpld_read(LED_CNTRLER_I2C_ADDRESS, reg);
}
static int as7816_64x_led_write_value(u8 reg, u8 value)
{
return accton_i2c_cpld_write(LED_CNTRLER_I2C_ADDRESS, reg, value);
return as7816_64x_cpld_write(LED_CNTRLER_I2C_ADDRESS, reg, value);
}
static void as7816_64x_led_update(void)

View File

@@ -42,7 +42,7 @@
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 accton_i2c_cpld_read (unsigned short cpld_addr, u8 reg);
extern int as7816_64x_cpld_read (unsigned short cpld_addr, u8 reg);
/* Addresses scanned
*/
@@ -200,7 +200,7 @@ static struct as7816_64x_psu_data *as7816_64x_psu_update_device(struct device *d
dev_dbg(&client->dev, "Starting as7816_64x update\n");
/* Read psu status */
status = accton_i2c_cpld_read(PSU_STATUS_I2C_ADDR, PSU_STATUS_I2C_REG_OFFSET);
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);

View File

@@ -40,8 +40,11 @@ static const int port_bus_index[NUM_OF_SFP_PORT] = {
81, 82, 83, 84, 25, 26, 27, 28
};
#define QSFP_BUS_INDEX(port) (port_bus_index[port])
#define QSFP_PORT_FORMAT "/sys/bus/i2c/devices/%d-0050/%s"
#define PORT_BUS_INDEX(port) (port_bus_index[port])
#define PORT_FORMAT "/sys/bus/i2c/devices/%d-0050/%s"
#define MODULE_PRESENT_FORMAT "/sys/bus/i2c/devices/19-0060/module_present_%d"
#define MODULE_PRESENT_ALL_ATTR "/sys/bus/i2c/devices/19-0060/module_present_all"
/************************************************************
*
@@ -59,7 +62,7 @@ int
onlp_sfpi_bitmap_get(onlp_sfp_bitmap_t* bmap)
{
/*
* Ports {0, 16}
* Ports {0, 64}
*/
int p;
AIM_BITMAP_CLR_ALL(bmap);
@@ -80,7 +83,8 @@ onlp_sfpi_is_present(int port)
* Return < 0 if error.
*/
int present;
if (onlp_file_read_int(&present, QSFP_PORT_FORMAT, QSFP_BUS_INDEX(port), "sfp_is_present") < 0) {
if (onlp_file_read_int(&present, MODULE_PRESENT_FORMAT, (port+1)) < 0) {
AIM_LOG_ERROR("Unable to read present status from port(%d)\r\n", port);
return ONLP_STATUS_E_INTERNAL;
}
@@ -92,11 +96,9 @@ int
onlp_sfpi_presence_bitmap_get(onlp_sfp_bitmap_t* dst)
{
uint32_t bytes[8];
char path[64] = {0};
FILE* fp;
sprintf(path, QSFP_PORT_FORMAT, QSFP_BUS_INDEX(0), "sfp_is_present_all");
fp = fopen(path, "r");
fp = fopen(MODULE_PRESENT_ALL_ATTR, "r");
if(fp == NULL) {
AIM_LOG_ERROR("Unable to open the sfp_is_present_all device file.");
@@ -114,7 +116,7 @@ onlp_sfpi_presence_bitmap_get(onlp_sfp_bitmap_t* dst)
/* Convert to 64 bit integer in port order */
int i = 0;
uint32_t presence_all = 0 ;
uint64_t presence_all = 0 ;
for(i = AIM_ARRAYSIZE(bytes)-1; i >= 0; i--) {
presence_all <<= 8;
presence_all |= bytes[i];
@@ -132,42 +134,51 @@ onlp_sfpi_presence_bitmap_get(onlp_sfp_bitmap_t* dst)
int
onlp_sfpi_eeprom_read(int port, uint8_t data[256])
{
/*
* Read the SFP eeprom into data[]
*
* Return MISSING if SFP is missing.
* Return OK if eeprom is read
*/
int size = 0;
if(onlp_file_read(data, 256, &size, QSFP_PORT_FORMAT, QSFP_BUS_INDEX(port), "sfp_eeprom") == ONLP_STATUS_OK) {
if(size == 256) {
return ONLP_STATUS_OK;
}
if(onlp_file_read(data, 256, &size, PORT_FORMAT, PORT_BUS_INDEX(port), "eeprom") != ONLP_STATUS_OK) {
AIM_LOG_ERROR("Unable to read eeprom from port(%d)\r\n", port);
return ONLP_STATUS_E_INTERNAL;
}
return ONLP_STATUS_E_INTERNAL;
if(size != 256) {
return ONLP_STATUS_E_INTERNAL;
}
return ONLP_STATUS_OK;
}
int
onlp_sfpi_dev_readb(int port, uint8_t devaddr, uint8_t addr)
{
int bus = QSFP_BUS_INDEX(port);
int bus = PORT_BUS_INDEX(port);
return onlp_i2c_readb(bus, devaddr, addr, ONLP_I2C_F_FORCE);
}
int
onlp_sfpi_dev_writeb(int port, uint8_t devaddr, uint8_t addr, uint8_t value)
{
int bus = QSFP_BUS_INDEX(port);
int bus = PORT_BUS_INDEX(port);
return onlp_i2c_writeb(bus, devaddr, addr, value, ONLP_I2C_F_FORCE);
}
int
onlp_sfpi_dev_readw(int port, uint8_t devaddr, uint8_t addr)
{
int bus = QSFP_BUS_INDEX(port);
int bus = PORT_BUS_INDEX(port);
return onlp_i2c_readw(bus, devaddr, addr, ONLP_I2C_F_FORCE);
}
int
onlp_sfpi_dev_writew(int port, uint8_t devaddr, uint8_t addr, uint16_t value)
{
int bus = QSFP_BUS_INDEX(port);
int bus = PORT_BUS_INDEX(port);
return onlp_i2c_writew(bus, devaddr, addr, value, ONLP_I2C_F_FORCE);
}

View File

@@ -8,10 +8,11 @@ class OnlPlatform_x86_64_accton_as7816_64x_r0(OnlPlatformAccton,
SYS_OBJECT_ID=".7816.64"
def baseconfig(self):
self.insmod("ym2651y")
self.insmod('optoe')
self.insmod('ym2651y')
self.insmod('accton_i2c_cpld')
self.insmod_platform()
for m in [ 'fan', 'cpld1', 'psu', 'leds' ]:
self.insmod("x86-64-accton-as7816-64x-%s.ko" % m)
########### initialize I2C bus 0 ###########
self.new_i2c_devices([
@@ -43,7 +44,7 @@ class OnlPlatform_x86_64_accton_as7816_64x_r0(OnlPlatformAccton,
('lm75', 0x4e, 17),
#initiate CPLD
('accton_i2c_cpld', 0x60, 19),
('as7816_64x_cpld1', 0x60, 19),
('accton_i2c_cpld', 0x62, 20),
('accton_i2c_cpld', 0x64, 21),
('accton_i2c_cpld', 0x66, 22),
@@ -58,72 +59,138 @@ class OnlPlatform_x86_64_accton_as7816_64x_r0(OnlPlatformAccton,
('pca9548', 0x76, 2),
# initialize QSFP port 1-64
('as7816_64x_port61', 0x50, 25),
('as7816_64x_port62', 0x50, 26),
('as7816_64x_port63', 0x50, 27),
('as7816_64x_port64', 0x50, 28),
('as7816_64x_port55', 0x50, 29),
('as7816_64x_port56', 0x50, 30),
('as7816_64x_port53', 0x50, 31),
('as7816_64x_port54', 0x50, 32),
('as7816_64x_port9', 0x50, 33),
('as7816_64x_port10', 0x50, 34),
('as7816_64x_port11', 0x50, 35),
('as7816_64x_port12', 0x50, 36),
('as7816_64x_port1', 0x50, 37),
('as7816_64x_port2', 0x50, 38),
('as7816_64x_port3', 0x50, 39),
('as7816_64x_port4', 0x50, 40),
('as7816_64x_port6', 0x50, 41),
('as7816_64x_port5', 0x50, 42),
('as7816_64x_port8', 0x50, 43),
('as7816_64x_port7', 0x50, 44),
('as7816_64x_port13', 0x50, 45),
('as7816_64x_port14', 0x50, 46),
('as7816_64x_port15', 0x50, 47),
('as7816_64x_port16', 0x50, 48),
('as7816_64x_port17', 0x50, 49),
('as7816_64x_port18', 0x50, 50),
('as7816_64x_port19', 0x50, 51),
('as7816_64x_port20', 0x50, 52),
('as7816_64x_port25', 0x50, 53),
('as7816_64x_port26', 0x50, 54),
('as7816_64x_port27', 0x50, 55),
('as7816_64x_port28', 0x50, 56),
('as7816_64x_port29', 0x50, 57),
('as7816_64x_port30', 0x50, 58),
('as7816_64x_port31', 0x50, 59),
('as7816_64x_port32', 0x50, 60),
('as7816_64x_port21', 0x50, 61),
('as7816_64x_port22', 0x50, 62),
('as7816_64x_port23', 0x50, 63),
('as7816_64x_port24', 0x50, 64),
('as7816_64x_port41', 0x50, 65),
('as7816_64x_port42', 0x50, 66),
('as7816_64x_port43', 0x50, 67),
('as7816_64x_port44', 0x50, 68),
('as7816_64x_port33', 0x50, 69),
('as7816_64x_port34', 0x50, 70),
('as7816_64x_port35', 0x50, 71),
('as7816_64x_port36', 0x50, 72),
('as7816_64x_port45', 0x50, 73),
('as7816_64x_port46', 0x50, 74),
('as7816_64x_port47', 0x50, 75),
('as7816_64x_port48', 0x50, 76),
('as7816_64x_port37', 0x50, 77),
('as7816_64x_port38', 0x50, 78),
('as7816_64x_port39', 0x50, 79),
('as7816_64x_port40', 0x50, 80),
('as7816_64x_port57', 0x50, 81),
('as7816_64x_port58', 0x50, 82),
('as7816_64x_port59', 0x50, 83),
('as7816_64x_port60', 0x50, 84),
('as7816_64x_port49', 0x50, 85),
('as7816_64x_port50', 0x50, 86),
('as7816_64x_port51', 0x50, 87),
('as7816_64x_port52', 0x50, 88),
('optoe1', 0x50, 25),
('optoe1', 0x50, 26),
('optoe1', 0x50, 27),
('optoe1', 0x50, 28),
('optoe1', 0x50, 29),
('optoe1', 0x50, 30),
('optoe1', 0x50, 31),
('optoe1', 0x50, 32),
('optoe1', 0x50, 33),
('optoe1', 0x50, 34),
('optoe1', 0x50, 35),
('optoe1', 0x50, 36),
('optoe1', 0x50, 37),
('optoe1', 0x50, 38),
('optoe1', 0x50, 39),
('optoe1', 0x50, 40),
('optoe1', 0x50, 41),
('optoe1', 0x50, 42),
('optoe1', 0x50, 43),
('optoe1', 0x50, 44),
('optoe1', 0x50, 45),
('optoe1', 0x50, 46),
('optoe1', 0x50, 47),
('optoe1', 0x50, 48),
('optoe1', 0x50, 49),
('optoe1', 0x50, 50),
('optoe1', 0x50, 51),
('optoe1', 0x50, 52),
('optoe1', 0x50, 53),
('optoe1', 0x50, 54),
('optoe1', 0x50, 55),
('optoe1', 0x50, 56),
('optoe1', 0x50, 57),
('optoe1', 0x50, 58),
('optoe1', 0x50, 59),
('optoe1', 0x50, 60),
('optoe1', 0x50, 61),
('optoe1', 0x50, 62),
('optoe1', 0x50, 63),
('optoe1', 0x50, 64),
('optoe1', 0x50, 65),
('optoe1', 0x50, 66),
('optoe1', 0x50, 67),
('optoe1', 0x50, 68),
('optoe1', 0x50, 69),
('optoe1', 0x50, 70),
('optoe1', 0x50, 71),
('optoe1', 0x50, 72),
('optoe1', 0x50, 73),
('optoe1', 0x50, 74),
('optoe1', 0x50, 75),
('optoe1', 0x50, 76),
('optoe1', 0x50, 77),
('optoe1', 0x50, 78),
('optoe1', 0x50, 79),
('optoe1', 0x50, 80),
('optoe1', 0x50, 81),
('optoe1', 0x50, 82),
('optoe1', 0x50, 83),
('optoe1', 0x50, 84),
('optoe1', 0x50, 85),
('optoe1', 0x50, 86),
('optoe1', 0x50, 87),
('optoe1', 0x50, 88),
('24c02', 0x56, 0),
])
subprocess.call('echo port61 > /sys/bus/i2c/devices/25-0050/port_name', shell=True)
subprocess.call('echo port62 > /sys/bus/i2c/devices/26-0050/port_name', shell=True)
subprocess.call('echo port63 > /sys/bus/i2c/devices/27-0050/port_name', shell=True)
subprocess.call('echo port64 > /sys/bus/i2c/devices/28-0050/port_name', shell=True)
subprocess.call('echo port55 > /sys/bus/i2c/devices/29-0050/port_name', shell=True)
subprocess.call('echo port56 > /sys/bus/i2c/devices/30-0050/port_name', shell=True)
subprocess.call('echo port53 > /sys/bus/i2c/devices/31-0050/port_name', shell=True)
subprocess.call('echo port54 > /sys/bus/i2c/devices/32-0050/port_name', shell=True)
subprocess.call('echo port9 > /sys/bus/i2c/devices/33-0050/port_name', shell=True)
subprocess.call('echo port10 > /sys/bus/i2c/devices/34-0050/port_name', shell=True)
subprocess.call('echo port11 > /sys/bus/i2c/devices/35-0050/port_name', shell=True)
subprocess.call('echo port12 > /sys/bus/i2c/devices/36-0050/port_name', shell=True)
subprocess.call('echo port1 > /sys/bus/i2c/devices/37-0050/port_name', shell=True)
subprocess.call('echo port2 > /sys/bus/i2c/devices/38-0050/port_name', shell=True)
subprocess.call('echo port3 > /sys/bus/i2c/devices/39-0050/port_name', shell=True)
subprocess.call('echo port4 > /sys/bus/i2c/devices/40-0050/port_name', shell=True)
subprocess.call('echo port6 > /sys/bus/i2c/devices/41-0050/port_name', shell=True)
subprocess.call('echo port5 > /sys/bus/i2c/devices/42-0050/port_name', shell=True)
subprocess.call('echo port8 > /sys/bus/i2c/devices/43-0050/port_name', shell=True)
subprocess.call('echo port7 > /sys/bus/i2c/devices/44-0050/port_name', shell=True)
subprocess.call('echo port13 > /sys/bus/i2c/devices/45-0050/port_name', shell=True)
subprocess.call('echo port14 > /sys/bus/i2c/devices/46-0050/port_name', shell=True)
subprocess.call('echo port15 > /sys/bus/i2c/devices/47-0050/port_name', shell=True)
subprocess.call('echo port16 > /sys/bus/i2c/devices/48-0050/port_name', shell=True)
subprocess.call('echo port17 > /sys/bus/i2c/devices/49-0050/port_name', shell=True)
subprocess.call('echo port18 > /sys/bus/i2c/devices/50-0050/port_name', shell=True)
subprocess.call('echo port19 > /sys/bus/i2c/devices/51-0050/port_name', shell=True)
subprocess.call('echo port20 > /sys/bus/i2c/devices/52-0050/port_name', shell=True)
subprocess.call('echo port25 > /sys/bus/i2c/devices/53-0050/port_name', shell=True)
subprocess.call('echo port26 > /sys/bus/i2c/devices/54-0050/port_name', shell=True)
subprocess.call('echo port27 > /sys/bus/i2c/devices/55-0050/port_name', shell=True)
subprocess.call('echo port28 > /sys/bus/i2c/devices/56-0050/port_name', shell=True)
subprocess.call('echo port29 > /sys/bus/i2c/devices/57-0050/port_name', shell=True)
subprocess.call('echo port30 > /sys/bus/i2c/devices/58-0050/port_name', shell=True)
subprocess.call('echo port31 > /sys/bus/i2c/devices/59-0050/port_name', shell=True)
subprocess.call('echo port32 > /sys/bus/i2c/devices/60-0050/port_name', shell=True)
subprocess.call('echo port21 > /sys/bus/i2c/devices/61-0050/port_name', shell=True)
subprocess.call('echo port22 > /sys/bus/i2c/devices/62-0050/port_name', shell=True)
subprocess.call('echo port23 > /sys/bus/i2c/devices/63-0050/port_name', shell=True)
subprocess.call('echo port24 > /sys/bus/i2c/devices/64-0050/port_name', shell=True)
subprocess.call('echo port41 > /sys/bus/i2c/devices/65-0050/port_name', shell=True)
subprocess.call('echo port42 > /sys/bus/i2c/devices/66-0050/port_name', shell=True)
subprocess.call('echo port43 > /sys/bus/i2c/devices/67-0050/port_name', shell=True)
subprocess.call('echo port44 > /sys/bus/i2c/devices/68-0050/port_name', shell=True)
subprocess.call('echo port33 > /sys/bus/i2c/devices/69-0050/port_name', shell=True)
subprocess.call('echo port34 > /sys/bus/i2c/devices/70-0050/port_name', shell=True)
subprocess.call('echo port35 > /sys/bus/i2c/devices/71-0050/port_name', shell=True)
subprocess.call('echo port36 > /sys/bus/i2c/devices/72-0050/port_name', shell=True)
subprocess.call('echo port45 > /sys/bus/i2c/devices/73-0050/port_name', shell=True)
subprocess.call('echo port46 > /sys/bus/i2c/devices/74-0050/port_name', shell=True)
subprocess.call('echo port47 > /sys/bus/i2c/devices/75-0050/port_name', shell=True)
subprocess.call('echo port48 > /sys/bus/i2c/devices/76-0050/port_name', shell=True)
subprocess.call('echo port37 > /sys/bus/i2c/devices/77-0050/port_name', shell=True)
subprocess.call('echo port38 > /sys/bus/i2c/devices/78-0050/port_name', shell=True)
subprocess.call('echo port39 > /sys/bus/i2c/devices/79-0050/port_name', shell=True)
subprocess.call('echo port40 > /sys/bus/i2c/devices/80-0050/port_name', shell=True)
subprocess.call('echo port57 > /sys/bus/i2c/devices/81-0050/port_name', shell=True)
subprocess.call('echo port58 > /sys/bus/i2c/devices/82-0050/port_name', shell=True)
subprocess.call('echo port59 > /sys/bus/i2c/devices/83-0050/port_name', shell=True)
subprocess.call('echo port60 > /sys/bus/i2c/devices/84-0050/port_name', shell=True)
subprocess.call('echo port49 > /sys/bus/i2c/devices/85-0050/port_name', shell=True)
subprocess.call('echo port50 > /sys/bus/i2c/devices/86-0050/port_name', shell=True)
subprocess.call('echo port51 > /sys/bus/i2c/devices/87-0050/port_name', shell=True)
subprocess.call('echo port52 > /sys/bus/i2c/devices/88-0050/port_name', shell=True)
return True