/*
 * Accton AS5835 CPLD3 Driver
 *
 * Copyright (c) 2019 Cumulus Networks, Inc.  All rights reserved.
 * Author: David Yen <dhyen@cumulusnetworks.com>
 *
 * 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.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/i2c-mux.h>
#include <linux/platform_data/at24.h>
#include <linux/i2c/sff-8436.h>
#include <linux/i2c/pca954x.h>
#include <linux/platform_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/cumulus-platform.h>

#include "platform_defs.h"
#include "platform_bitfield.h"
#include "accton_as5835.h"

#define DRIVER_NAME	   AS5835_CPLD3_NAME
#define DRIVER_VERSION	   "0.1"

/* bitfield accessor functions */

#define cpld_read_reg  bf_i2c_read_reg
#define cpld_write_reg bf_i2c_write_reg

/* cpld register bitfields with enum-like values */

/* CPLD registers */

cpld_bf_ro(cpld_version, ACCTON_AS5835_CPLD3_VERSION_REG,
	   ACCTON_AS5835_CPLD3_VERSION, NULL, 0)

cpld_bf_ro(qsfp_54_49_interrupt, ACCTON_AS5835_CPLD3_QSFP_MODULE_INT_REG,
	   ACCTON_AS5835_CPLD3_QSFP_MODULE_INT, NULL, BF_COMPLEMENT)
cpld_bf_ro(qsfp_54_49_present, ACCTON_AS5835_CPLD3_QSFP_PRESENT_REG,
	   ACCTON_AS5835_CPLD3_QSFP_PRESENT, NULL, BF_COMPLEMENT)
cpld_bf_rw(qsfp_54_49_reset, ACCTON_AS5835_CPLD3_QSFP_MOD_RST_REG,
	   ACCTON_AS5835_CPLD3_QSFP_MOD_RST, NULL, BF_COMPLEMENT)
cpld_bf_ro(qsfp_54_49_lpmode, ACCTON_AS5835_CPLD3_QSFP_LPMODE_REG,
	   ACCTON_AS5835_CPLD3_QSFP_LPMODE, NULL, BF_COMPLEMENT)

/* special case for sfp registers */

struct sfp_info {
	int reg;
	int active_low;
};

static struct sfp_info sfp_reg[] = {
	{
		.reg = ACCTON_AS5835_CPLD3_SFP_PRESENT_REG,
		.active_low = 1,
	},
	{
		.reg = ACCTON_AS5835_CPLD3_SFP_TX_FAULT_REG,
		.active_low = 0,
	},
	{
		.reg = ACCTON_AS5835_CPLD3_SFP_TX_DISABLE_REG,
		.active_low = 0,
	},
	{
		.reg = ACCTON_AS5835_CPLD3_SFP_RX_LOS_REG,
		.active_low = 0,
	},
};

static ssize_t sfp_show(struct device *dev, struct device_attribute *dattr,
			char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(dattr);
	struct i2c_client *client = to_i2c_client(dev);
	int idx = attr->index;
	int reg = sfp_reg[idx].reg;
	int i;
	s32 tmp;
	u64 val = 0;

	for (i = 0; i < NUM_CPLD3_SFP_REGS; i++) {
		tmp = i2c_smbus_read_byte_data(client, (reg + i));
		if (tmp < 0) {
			pr_err(DRIVER_NAME ": CPLD3 read error - reg: 0x%02X\n",
			       reg);
			return -EINVAL;
		}
		if (sfp_reg[idx].active_low)
			tmp = ~tmp;
		val |= ((u64)tmp & 0xff) << i * 8;
	}
	return sprintf(buf, "0x%012llx\n", val & 0x03ff);
}

static ssize_t sfp_store(struct device *dev, struct device_attribute *dattr,
			 const char *buf, size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(dattr);
	struct i2c_client *client = to_i2c_client(dev);
	int idx = attr->index;
	int reg = sfp_reg[idx].reg;
	int ret;
	u64 val = 0;
	int i;
	u8 tmp;

	ret = kstrtou64(buf, 0, &val);
	if (ret < 0)
		return ret;

	for (i = 0; i < NUM_CPLD3_SFP_REGS; i++) {
		tmp = val >> (i * 8) & 0xff;
		if (sfp_reg[idx].active_low)
			tmp = ~tmp;
		ret = i2c_smbus_write_byte_data(client, (reg + i), tmp);
		if (ret) {
			pr_err(DRIVER_NAME ": CPLD3 write error - reg: 0x%02X\n",
			       reg);
			return -EINVAL;
		}
	}
	return count;
}

static SENSOR_DEVICE_ATTR_RO(sfp_48_39_present,    sfp_show, 0);
static SENSOR_DEVICE_ATTR_RO(sfp_48_39_tx_fault,   sfp_show, 1);
static SENSOR_DEVICE_ATTR_RW(sfp_48_39_tx_disable, sfp_show, sfp_store, 2);
static SENSOR_DEVICE_ATTR_RO(sfp_48_39_rx_los,     sfp_show, 3);

/* sysfs registration */

static struct attribute *cpld_attrs[] = {
	&cpld_cpld_version.attr,

	&sensor_dev_attr_sfp_48_39_present.dev_attr.attr,
	&sensor_dev_attr_sfp_48_39_tx_fault.dev_attr.attr,
	&sensor_dev_attr_sfp_48_39_tx_disable.dev_attr.attr,
	&sensor_dev_attr_sfp_48_39_rx_los.dev_attr.attr,

	&cpld_qsfp_54_49_interrupt.attr,
	&cpld_qsfp_54_49_present.attr,
	&cpld_qsfp_54_49_reset.attr,
	&cpld_qsfp_54_49_lpmode.attr,

	NULL,
};

static struct attribute_group cpld_attr_group = {
	.attrs = cpld_attrs,
};

static int cpld_probe(struct i2c_client *client,
		      const struct i2c_device_id *id)
{
	s32 temp;
	int ret = 0;
	struct kobject *kobj = &client->dev.kobj;

	temp = i2c_smbus_read_byte_data(client,
					ACCTON_AS5835_CPLD3_VERSION_REG);
	if (temp < 0) {
		pr_err(DRIVER_NAME ": read CPLD3 version register error: %d\n",
		       ret);
		return temp;
	}

	ret = sysfs_create_group(kobj, &cpld_attr_group);
	if (ret)
		pr_err(DRIVER_NAME ": failed to create sysfs group for cpld device\n");

	pr_info(DRIVER_NAME ": device probed, CPLD3 rev: %d\n",
		(int)GET_FIELD(temp, ACCTON_AS5835_CPLD3_VERSION));

	return ret;
}

static int cpld_remove(struct i2c_client *client)
{
	struct kobject *kobj = &client->dev.kobj;

	sysfs_remove_group(kobj, &cpld_attr_group);

	return 0;
}

static const struct i2c_device_id cpld_i2c_ids[] = {
	{
		.name = DRIVER_NAME,
	},
	{ /* end of list */ },
};

MODULE_DEVICE_TABLE(i2c, cpld_i2c_ids);

static struct i2c_driver cpld_driver = {
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},
	.probe = cpld_probe,
	.remove = cpld_remove,
	.id_table = cpld_i2c_ids,
};

/* module init and exit */

static int __init cpld_init(void)
{
	int ret;

	ret = i2c_add_driver(&cpld_driver);
	if (ret) {
		pr_err(DRIVER_NAME ": driver failed to load\n");
		return ret;
	}

	pr_info(DRIVER_NAME ": version " DRIVER_VERSION " loaded\n");
	return 0;
}

static void __exit cpld_exit(void)
{
	i2c_del_driver(&cpld_driver);
	pr_info(DRIVER_NAME ": unloaded\n");
}

module_init(cpld_init);
module_exit(cpld_exit);

MODULE_AUTHOR("David Yen (dhyen@cumulusnetworks.com)");
MODULE_DESCRIPTION("Accton AS5835 CPLD3 Driver");
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");
