#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/types.h>
#include <linux/regmap.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/workqueue.h>

#define SN65DSI83_EN 119
static struct i2c_client *lt9211_client;

int HDMI_WriteI2C_Byte(uint8_t reg, uint8_t val)
{
	bool  flag;
	flag = i2c_smbus_write_byte_data(lt9211_client, reg, val);
	return flag;
}

int HDMI_ReadI2C_Byte(uint8_t reg)
{
	u32 flag = 0;

	flag = i2c_smbus_read_byte_data(lt9211_client, reg);
	return flag;
}


typedef struct {
    uint8_t reg;   // 寄存器地址
    uint8_t val;   // 要写入的值
} reg_cfg_t;

const reg_cfg_t sn65dsi83_config[] = {
    {0x09, 0x00},
    {0x0A, 0x03},
    {0x0B, 0x10},   // DSI_CLK_DIVIDER=2 -> /3: 156MHz DDR / 3 = 52MHz LVDS
    {0x0D, 0x00},
    {0x10, 0x26},
    {0x11, 0x00},
    {0x12, 0x1d},   // CHA_DSI_CLK_RANGE: 49.5MHz*24bpp/4lane/2 = 148.5MHz -> 145~150MHz 档
    {0x13, 0x00},
    {0x18, 0x7a},   // DE+/HS+/VS+ (dtsi 是 hsync-active=1/vsync-active=1), 单链路, 24bpp, VESA/SPWG  7a jeida 18 vesa
    {0x19, 0x00},
    {0x1A, 0x23},
    {0x1B, 0x00},
    {0x20, 0x00},
    {0x21, 0x04},
    {0x22, 0x00},
    {0x23, 0x00},
    {0x24, 0x58},   // CHA_VERTICAL_DISPLAY_SIZE = vactive 600
    {0x25, 0x02},
    {0x26, 0x00},
    {0x27, 0x00},
    {0x28, 0x21},   // CHA_SYNC_DELAY = 33 (mainline: 32+1 个像素时钟)
    {0x29, 0x00},
    {0x2A, 0x00},
    {0x2B, 0x00},
    {0x2C, 0x0a},
    {0x2D, 0x00},
    {0x2E, 0x00},
    {0x2F, 0x00},
    {0x30, 0x03},
    {0x31, 0x00},
    {0x32, 0x00},
    {0x33, 0x00},
    {0x34, 0x96},
    {0x35, 0x00},
    {0x36, 0x14},   // CHA_VERTICAL_BACK_PORCH = vback-porch 20
    {0x37, 0x00},
    {0x38, 0xa0},   // CHA_HORIZONTAL_FRONT_PORCH = hfront-porch 160
    {0x39, 0x00},
    {0x3A, 0x0c},   // CHA_VERTICAL_FRONT_PORCH = vfront-porch 12
    {0x3B, 0x00},
    {0x3C, 0x00},
    {0x3D, 0x00},
    {0x3E, 0x00}
};


const int config_count = sizeof(sn65dsi83_config) / sizeof(sn65dsi83_config[0]);
static int mipitolvds_init(void)
{
    int ret;
    // 申请
    ret = gpio_request(SN65DSI83_EN, "my_gpio_name");
    if (ret) {
        pr_err("gpio request failed\n");
        return ret;
    }
    // 设置为输出并拉高
    ret = gpio_direction_output(SN65DSI83_EN, 0);
    if (ret) {
        gpio_free(SN65DSI83_EN);
        pr_err("set output failed\n");
        return ret;
    }
    mdelay(10);
    gpio_set_value(SN65DSI83_EN, 1);
    mdelay(10);   // seq 4: EN 拉高后必须等 10ms 让内部 LDO/振荡器稳定再访问 CSR

    for (int i = 0; i < config_count; i++) {
        HDMI_WriteI2C_Byte(sn65dsi83_config[i].reg, sn65dsi83_config[i].val);
        //i2c_write_byte(device_addr, sn65dsi83_config[i].reg, sn65dsi83_config[i].val);
    }

    HDMI_WriteI2C_Byte(0x0d, 0x01);   // seq 6: PLL_EN = 1
    mdelay(10);
    HDMI_WriteI2C_Byte(0x09, 0x01);   // seq 7: SOFT_RESET = 1
    mdelay(10);
    HDMI_WriteI2C_Byte(0xe5, 0xff);   // seq 10: 清错误寄存器(写 1 清对应位)
    mdelay(1);

    ret = HDMI_ReadI2C_Byte(0xe5);
    printk("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwww     the ret is %x       \r\n  ",ret);
    return 0;
}

/*
 * TI 的初始化顺序要求先有稳定的 DSI HS 时钟,再配桥片、再放视频流。
 * 但本驱动是在 i2c probe(约 5.9s)里跑完全部初始化的,那时 VOP/DSI 还没起来
 * (约 9.6s),桥片会锁到噪声上,而且失锁后不会自愈 —— 表现为开机花屏。
 * 这里在 DSI 出流之后补一次 PLL_EN + SOFT_RESET,读 0xE5 确认干净为止。
 * 固定延时会随内核启动时序漂移,所以带重试;跑通或次数用尽即停,不常驻轮询。
 */
#define SN65_RETRY_MAX      10
#define SN65_RETRY_FIRST_MS 5000
#define SN65_RETRY_GAP_MS   2000

static struct delayed_work sn65_relink_work;
static int sn65_retry_left;

static void sn65dsi83_relink(struct work_struct *work)
{
    int ret;

    HDMI_WriteI2C_Byte(0x0d, 0x00);   // PLL_EN = 0
    msleep(5);
    HDMI_WriteI2C_Byte(0x09, 0x00);   // 清 SOFT_RESET
    HDMI_WriteI2C_Byte(0x0d, 0x01);   // PLL_EN = 1
    msleep(10);
    HDMI_WriteI2C_Byte(0x09, 0x01);   // SOFT_RESET = 1
    msleep(10);
    HDMI_WriteI2C_Byte(0xe5, 0xff);   // 清错误
    msleep(20);                        // 等几帧,同步错误才会 latch

    ret = HDMI_ReadI2C_Byte(0xe5);
    if (ret == 0) {
        printk("sn65dsi83: link ok, 0xe5=0 (%d retries left)\n", sn65_retry_left);
        return;
    }

    if (--sn65_retry_left > 0) {
        schedule_delayed_work(&sn65_relink_work,
                              msecs_to_jiffies(SN65_RETRY_GAP_MS));
        return;
    }

    printk("sn65dsi83: give up, 0xe5=%x\n", ret);
}
static int lt9211_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct device *dev = &client->dev;
	lt9211_client = client;
    printk("mipi to lvds driver is start--------------------------------");
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		dev_err(dev, "device doesn't support I2C\n");
		return -ENODEV;
	}

    mipitolvds_init();

    sn65_retry_left = SN65_RETRY_MAX;
    INIT_DELAYED_WORK(&sn65_relink_work, sn65dsi83_relink);
    schedule_delayed_work(&sn65_relink_work,
                          msecs_to_jiffies(SN65_RETRY_FIRST_MS));

	return 0;
}

static void lt9211_remove(struct i2c_client *client)
{
	cancel_delayed_work_sync(&sn65_relink_work);
	return ;
}

static struct i2c_device_id lt9211_id[] = {
	{ "lontium,lt9211", 0 },
	{}
};
MODULE_DEVICE_TABLE(i2c, lt9211_id);

static const struct of_device_id lt9211_match_table[] = {
	{ .compatible = "lontium,lt9211" },
	{ }
};
MODULE_DEVICE_TABLE(of, lt9211_match_table);

static struct i2c_driver lt9211_driver = {
	.driver = {
		.name = "lt9211",
		.of_match_table = lt9211_match_table,
	},
	.probe = lt9211_probe,
	.remove = lt9211_remove,
	.id_table = lt9211_id,
};
module_i2c_driver(lt9211_driver);

MODULE_DESCRIPTION("Lontium lt9211 MIPI-DSI to MIPI-CSI");
MODULE_LICENSE("GPL v2");