/*
 * Lenovo NE2580 CPLD1 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 "lenovo_ne2580.h"

#define DRIVER_NAME	   NE2580_CPLD1_NAME
#define DRIVER_VERSION	   "1.0"

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

static const char * const green_led_values[] = {
	PLATFORM_LED_OFF,
	PLATFORM_LED_GREEN,
	PLATFORM_LED_GREEN_SLOW_BLINKING,
	PLATFORM_LED_GREEN_BLINKING,
};

static const char * const blue_led_values[] = {
	PLATFORM_LED_OFF,
	PLATFORM_LED_BLUE,
	PLATFORM_LED_BLUE_SLOW_BLINKING,
	PLATFORM_LED_BLUE_BLINKING,
};

static const char * const system_direction_values[] = {
	"B2F",
	"F2B",
};

/* CPLD registers */

cpld_bt_ro(cpld1_system_airflow, LENOVO_NE2580_CPLD1_PCB_VERSION_REG,
	   LENOVO_NE2580_CPLD1_SYS_AIR_DIR, system_direction_values, 0)
cpld_bf_ro(cpld1_triton_model, LENOVO_NE2580_CPLD1_PCB_VERSION_REG,
	   LENOVO_NE2580_CPLD1_TRITON_MODEL_SEL, NULL, 0)
cpld_bf_ro(hw_version, LENOVO_NE2580_CPLD1_PCB_VERSION_REG,
	   LENOVO_NE2580_CPLD1_HW_VERSION, NULL, 0)

cpld_bt_rw(cpld1_system_ready, LENOVO_NE2580_CPLD1_SWITCH_READY_REG,
	   LENOVO_NE2580_CPLD1_SYS_RDY, NULL, 0)

cpld_bf_rw(led_system, LENOVO_NE2580_CPLD1_SYSTEM_STATUS_LED_CONTROL_REG,
	   LENOVO_NE2580_CPLD1_SVR_LED, blue_led_values, 0)
cpld_bf_rw(led_psu, LENOVO_NE2580_CPLD1_SYSTEM_STATUS_LED_CONTROL_REG,
	   LENOVO_NE2580_CPLD1_PWR_LED, green_led_values, 0)
cpld_bf_rw(led_fan, LENOVO_NE2580_CPLD1_SYSTEM_STATUS_LED_CONTROL_REG,
	   LENOVO_NE2580_CPLD1_FAN_LED, green_led_values, 0)
cpld_bf_rw(led_stack, LENOVO_NE2580_CPLD1_SYSTEM_STATUS_LED_CONTROL_REG,
	   LENOVO_NE2580_CPLD1_STK_LED, green_led_values, 0)

cpld_bt_rw(reset_bcm56873, LENOVO_NE2580_CPLD1_SYSTEM_RESET_CONTROL_2_REG,
	   LENOVO_NE2580_CPLD1_PLTRST_BCM56873_N, NULL, BF_COMPLEMENT)
cpld_bt_rw(reset_pcie, LENOVO_NE2580_CPLD1_SYSTEM_RESET_CONTROL_2_REG,
	   LENOVO_NE2580_CPLD1_PCIE_RESET_L, NULL, BF_COMPLEMENT)

cpld_bt_rw(reason_temp, LENOVO_NE2580_CPLD1_RESET_CAUSE_REG,
	   LENOVO_NE2580_CPLD1_SYS_THERMAL_TRIP, NULL, 0)
cpld_bt_rw(reason_panic, LENOVO_NE2580_CPLD1_RESET_CAUSE_REG,
	   LENOVO_NE2580_CPLD1_PANIC, NULL, 0)
cpld_bt_rw(reason_button, LENOVO_NE2580_CPLD1_RESET_CAUSE_REG,
	   LENOVO_NE2580_CPLD1_RB_RST, NULL, 0)
cpld_bt_rw(reason_software, LENOVO_NE2580_CPLD1_RESET_CAUSE_REG,
	   LENOVO_NE2580_CPLD1_SW_RST, NULL, 0)
cpld_bt_rw(reason_watchdog, LENOVO_NE2580_CPLD1_RESET_CAUSE_REG,
	   LENOVO_NE2580_CPLD1_WD_RST, NULL, 0)
cpld_bt_rw(reason_poweroff, LENOVO_NE2580_CPLD1_RESET_CAUSE_REG,
	   LENOVO_NE2580_CPLD1_PO_RST, NULL, 0)

cpld_bf_ro(wd_counter, LENOVO_NE2580_CPLD1_WATCHDOG_CURRENT_COUNTER_REG,
	   LENOVO_NE2580_CPLD1_WATCHDOG_CURRENT_COUNTER, NULL, 0)
cpld_bf_rw(wd_timer, LENOVO_NE2580_CPLD1_WATCHDOG_TIMER_REG,
	   LENOVO_NE2580_CPLD1_WATCHDOG_TIMER, NULL, 0)
cpld_bt_rw(wd_init, LENOVO_NE2580_CPLD1_WATCHDOG_ENABLE_REG,
	   LENOVO_NE2580_CPLD1_WD_CLR, NULL, 0)
cpld_bt_rw(wd_enable, LENOVO_NE2580_CPLD1_WATCHDOG_ENABLE_REG,
	   LENOVO_NE2580_CPLD1_WD_EN, NULL, 0)

cpld_bt_rw(system_reset_mode, LENOVO_NE2580_CPLD1_SYSTEM_RESET_CONTROL_6_REG,
	   LENOVO_NE2580_CPLD1_SYSTEM_RST_SEL, NULL, 0)

cpld_bf_rw(panic_code, LENOVO_NE2580_CPLD1_PANIC_REG,
	   LENOVO_NE2580_CPLD1_PANIC_CODE, NULL, 0)

/* special case for cpld version register */

static ssize_t cpld1_version_show(struct device *dev,
				  struct device_attribute *dattr,
				  char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	s32 temp;
	int reg = LENOVO_NE2580_CPLD1_VERSION_REG;

	temp = i2c_smbus_read_byte_data(client, reg);
	if (temp < 0) {
		pr_err("CPLD read error - addr: 0x%02X, offset: 0x%02X\n",
		       client->addr, reg);
		return -EINVAL;
	}

	return sprintf(buf, "%d.%d\n",
		       (int)GET_FIELD(temp, LENOVO_NE2580_CPLD1_MAJOR_VERSION),
		       (int)GET_FIELD(temp, LENOVO_NE2580_CPLD1_MINOR_VERSION));
}

static SENSOR_DEVICE_ATTR_RO(cpld1_version, cpld1_version_show, 0);

/* special case for cpld date registers */

static ssize_t cpld1_date_show(struct device *dev,
			       struct device_attribute *dattr,
			       char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	s32 temp1;
	int reg1 = LENOVO_NE2580_CPLD1_RELEASE_DATE_1_REG;
	s32 temp2;
	int reg2 = LENOVO_NE2580_CPLD1_RELEASE_DATE_2_REG;

	temp1 = i2c_smbus_read_byte_data(client, reg1);
	if (temp1 < 0) {
		pr_err("CPLD read error - addr: 0x%02X, offset: 0x%02X\n",
		       client->addr, reg1);
		return -EINVAL;
	}
	temp2 = i2c_smbus_read_byte_data(client, reg2);
	if (temp2 < 0) {
		pr_err("CPLD read error - addr: 0x%02X, offset: 0x%02X\n",
		       client->addr, reg2);
		return -EINVAL;
	}

	return sprintf(buf, "%d/%d/%d\n",
		       (int)GET_FIELD(temp1, LENOVO_NE2580_CPLD1_RELEASE_MONTH),
		       (int)GET_FIELD(temp2, LENOVO_NE2580_CPLD1_RELEASE_DATE),
		       (int)GET_FIELD(temp1, LENOVO_NE2580_CPLD1_RELEASE_YEAR) + 2014);
}

static SENSOR_DEVICE_ATTR_RO(cpld1_date, cpld1_date_show, 0);

/* sysfs registration */

static struct attribute *cpld_attrs[] = {
	&cpld_cpld1_system_airflow.attr,
	&cpld_cpld1_triton_model.attr,
	&cpld_hw_version.attr,
	&cpld_cpld1_system_ready.attr,
	&cpld_led_system.attr,
	&cpld_led_psu.attr,
	&cpld_led_fan.attr,
	&cpld_led_stack.attr,
	&cpld_reset_bcm56873.attr,
	&cpld_reset_pcie.attr,
	&cpld_reason_temp.attr,
	&cpld_reason_panic.attr,
	&cpld_reason_button.attr,
	&cpld_reason_software.attr,
	&cpld_reason_watchdog.attr,
	&cpld_reason_poweroff.attr,
	&cpld_wd_counter.attr,
	&cpld_wd_timer.attr,
	&cpld_wd_enable.attr,
	&cpld_wd_init.attr,
	&cpld_system_reset_mode.attr,
	&cpld_panic_code.attr,

	&sensor_dev_attr_cpld1_version.dev_attr.attr,
	&sensor_dev_attr_cpld1_date.dev_attr.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,
					LENOVO_NE2580_CPLD1_VERSION_REG);
	if (temp < 0) {
		pr_err(DRIVER_NAME ": read CPLD1 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, CPLD1 rev: %d.%d\n",
		(int)GET_FIELD(temp, LENOVO_NE2580_CPLD1_MAJOR_VERSION),
		(int)GET_FIELD(temp, LENOVO_NE2580_CPLD1_MINOR_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("Lenovo NE2580 CPLD1 Driver");
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");
