From 343cbfb5d41d63a3de1137508a56084310e69c36 Mon Sep 17 00:00:00 2001 From: Waveshare_Team Date: Sat, 28 Dec 2024 18:03:38 +0800 Subject: [PATCH 1/4] drivers/regulator : Add a regulator to Waveshare DSI-TOUCH series panels The regulator of the Waveshare DSI-TOUCH series panels is different. Add a new driver for this regulator. Signed-off-by: Waveshare_Team --- drivers/regulator/Kconfig | 10 + drivers/regulator/Makefile | 1 + drivers/regulator/waveshare-panel-regulator.c | 306 ++++++++++++++++++ 3 files changed, 317 insertions(+) create mode 100644 drivers/regulator/waveshare-panel-regulator.c diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index d6210b0c3bc52c..206bf28e0e170b 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -1107,6 +1107,16 @@ config REGULATOR_RASPBERRYPI_TOUCHSCREEN_V2 touchscreen unit. The regulator is used to enable power to the display and to control backlight. +config REGULATOR_WAVESHARE_TOUCHSCREEN + tristate "Waveshare touchscreen panel regulator" + depends on BACKLIGHT_CLASS_DEVICE + depends on I2C + select REGMAP_I2C + help + This driver supports regulator on the waveshare + touchscreen unit. The regulator is used to enable power to the + display and to control backlight. + config REGULATOR_RC5T583 tristate "RICOH RC5T583 Power regulators" depends on MFD_RC5T583 diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index 6a1cfb31fa4365..323dd4b912a412 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -131,6 +131,7 @@ obj-$(CONFIG_REGULATOR_PCF50633) += pcf50633-regulator.o obj-$(CONFIG_REGULATOR_RAA215300) += raa215300.o obj-$(CONFIG_REGULATOR_RASPBERRYPI_TOUCHSCREEN_ATTINY) += rpi-panel-attiny-regulator.o obj-$(CONFIG_REGULATOR_RASPBERRYPI_TOUCHSCREEN_V2) += rpi-panel-v2-regulator.o +obj-$(CONFIG_REGULATOR_WAVESHARE_TOUCHSCREEN) += waveshare-panel-regulator.o obj-$(CONFIG_REGULATOR_RC5T583) += rc5t583-regulator.o obj-$(CONFIG_REGULATOR_RK808) += rk808-regulator.o obj-$(CONFIG_REGULATOR_RN5T618) += rn5t618-regulator.o diff --git a/drivers/regulator/waveshare-panel-regulator.c b/drivers/regulator/waveshare-panel-regulator.c new file mode 100644 index 00000000000000..f0a1fe3e5ccc71 --- /dev/null +++ b/drivers/regulator/waveshare-panel-regulator.c @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Waveshare International Limited + * + * Based on rpi-panel-v2-regulator.c by Dave Stevenson + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* I2C registers of the microcontroller. */ +#define REG_TP 0x94 +#define REG_LCD 0x95 +#define REG_PWM 0x96 +#define REG_SIZE 0x97 +#define REG_ID 0x98 +#define REG_VERSION 0x99 + +#define NUM_GPIO 16 /* Treat BL_ENABLE, LCD_RESET, TP_RESET as GPIOs */ + +struct waveshare_panel_lcd { + struct mutex lock; + struct regmap *regmap; + u16 poweron_state; + u16 direction_state; + + struct gpio_chip gc; +}; + +static const struct regmap_config waveshare_panel_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = REG_PWM, +}; + +static int waveshare_panel_gpio_direction_in(struct gpio_chip *gc, + unsigned int offset) +{ + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); + + state->direction_state |= (1 << offset); + + return 0; +} + +static int waveshare_panel_gpio_direction_out(struct gpio_chip *gc, + unsigned int offset, int val) +{ + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); + u16 last_val; + + state->direction_state &= ~(1 << offset); + + last_val = state->poweron_state; + if (val) + last_val |= (1 << offset); + else + last_val &= ~(1 << offset); + + state->poweron_state = last_val; + + regmap_write(state->regmap, REG_TP, last_val >> 8); + regmap_write(state->regmap, REG_LCD, last_val & 0xff); + + return 0; +} + +static int waveshare_panel_gpio_get_direction(struct gpio_chip *gc, + unsigned int offset) +{ + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); + + if (state->direction_state & (1 << offset)) + return GPIO_LINE_DIRECTION_IN; + else + return GPIO_LINE_DIRECTION_OUT; +} + +static int waveshare_panel_gpio_get(struct gpio_chip *gc, unsigned int offset) +{ + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); + + if (state->poweron_state & (1 << offset)) + return 1; + else + return 0; +} + +static void waveshare_panel_gpio_set(struct gpio_chip *gc, unsigned int offset, + int value) +{ + struct waveshare_panel_lcd *state = gpiochip_get_data(gc); + u16 last_val; + + if (offset >= NUM_GPIO) + return; + + mutex_lock(&state->lock); + + last_val = state->poweron_state; + if (value) + last_val |= (1 << offset); + else + last_val &= ~(1 << offset); + + state->poweron_state = last_val; + + regmap_write(state->regmap, REG_TP, last_val >> 8); + regmap_write(state->regmap, REG_LCD, last_val & 0xff); + + mutex_unlock(&state->lock); +} + +static int waveshare_panel_update_status(struct backlight_device *bl) +{ + struct waveshare_panel_lcd *state = bl_get_data(bl); + int brightness = bl->props.brightness; + u16 last_val; + + if (bl->props.power != FB_BLANK_UNBLANK || + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) + brightness = 0; + + mutex_lock(&state->lock); + + last_val = state->poweron_state; + if (brightness) + last_val |= (1 << 2); // Enable BL_EN + else + last_val &= ~(1 << 2); // Disable BL_EN + + state->poweron_state = last_val; + + regmap_write(state->regmap, REG_TP, last_val >> 8); + regmap_write(state->regmap, REG_LCD, last_val & 0xff); + + mutex_unlock(&state->lock); + + return regmap_write(state->regmap, REG_PWM, brightness); +} + +static const struct backlight_ops waveshare_panel_bl = { + .update_status = waveshare_panel_update_status, +}; + +static int waveshare_panel_i2c_read(struct i2c_client *client, u8 reg, + unsigned int *buf) +{ + struct i2c_msg msgs[1]; + u8 addr_buf[1] = { reg }; + u8 data_buf[1] = { + 0, + }; + int ret; + + /* Write register address */ + msgs[0].addr = client->addr; + msgs[0].flags = 0; + msgs[0].len = ARRAY_SIZE(addr_buf); + msgs[0].buf = addr_buf; + + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); + if (ret != ARRAY_SIZE(msgs)) + return -EIO; + + usleep_range(5000, 10000); + + /* Read data from register */ + msgs[0].addr = client->addr; + msgs[0].flags = I2C_M_RD; + msgs[0].len = 1; + msgs[0].buf = data_buf; + + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); + if (ret != ARRAY_SIZE(msgs)) + return -EIO; + + *buf = data_buf[0]; + return 0; +} + +/* + * I2C driver interface functions + */ +static int waveshare_panel_i2c_probe(struct i2c_client *i2c) +{ + struct backlight_properties props = {}; + struct backlight_device *bl; + struct waveshare_panel_lcd *state; + struct regmap *regmap; + unsigned int data; + int ret; + + state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL); + if (!state) + return -ENOMEM; + + mutex_init(&state->lock); + i2c_set_clientdata(i2c, state); + + regmap = devm_regmap_init_i2c(i2c, &waveshare_panel_regmap_config); + if (IS_ERR(regmap)) { + ret = PTR_ERR(regmap); + dev_err(&i2c->dev, "Failed to allocate register map: %d\n", + ret); + goto error; + } + + ret = waveshare_panel_i2c_read(i2c, REG_ID, &data); + if (ret == 0) + dev_info(&i2c->dev, "waveshare panel hw id = 0x%x\n", data); + + ret = waveshare_panel_i2c_read(i2c, REG_SIZE, &data); + if (ret == 0) + dev_info(&i2c->dev, "waveshare panel size = %d\n", data); + + ret = waveshare_panel_i2c_read(i2c, REG_VERSION, &data); + if (ret == 0) + dev_info(&i2c->dev, "waveshare panel mcu version = 0x%x\n", + data); + + state->direction_state = 0; + state->poweron_state = (1 << 9) | (1 << 8) | (1 << 4) | + (1 << 0); // Enable VCC + regmap_write(regmap, REG_TP, state->poweron_state >> 8); + regmap_write(regmap, REG_LCD, state->poweron_state & 0xff); + msleep(20); + + state->regmap = regmap; + state->gc.parent = &i2c->dev; + state->gc.label = i2c->name; + state->gc.owner = THIS_MODULE; + state->gc.base = -1; + state->gc.ngpio = NUM_GPIO; + + state->gc.get = waveshare_panel_gpio_get; + state->gc.set = waveshare_panel_gpio_set; + state->gc.direction_input = waveshare_panel_gpio_direction_in; + state->gc.direction_output = waveshare_panel_gpio_direction_out; + state->gc.get_direction = waveshare_panel_gpio_get_direction; + state->gc.can_sleep = true; + + ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state); + if (ret) { + dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret); + goto error; + } + + props.type = BACKLIGHT_RAW; + props.max_brightness = 255; + bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev), + &i2c->dev, state, + &waveshare_panel_bl, &props); + if (IS_ERR(bl)) + return PTR_ERR(bl); + + bl->props.brightness = 255; + + return 0; + +error: + mutex_destroy(&state->lock); + return ret; +} + +static void waveshare_panel_i2c_remove(struct i2c_client *client) +{ + struct waveshare_panel_lcd *state = i2c_get_clientdata(client); + + mutex_destroy(&state->lock); +} + +static void waveshare_panel_i2c_shutdown(struct i2c_client *client) +{ + struct waveshare_panel_lcd *state = i2c_get_clientdata(client); + + regmap_write(state->regmap, REG_PWM, 0); +} + +static const struct of_device_id waveshare_panel_dt_ids[] = { + { .compatible = "waveshare,touchscreen-panel-regulator" }, + {}, +}; +MODULE_DEVICE_TABLE(of, waveshare_panel_dt_ids); + +static struct i2c_driver waveshare_panel_regulator_driver = { + .driver = { + .name = "waveshare_touchscreen", + .of_match_table = of_match_ptr(waveshare_panel_dt_ids), + }, + .probe = waveshare_panel_i2c_probe, + .remove = waveshare_panel_i2c_remove, + .shutdown = waveshare_panel_i2c_shutdown, +}; + +module_i2c_driver(waveshare_panel_regulator_driver); + +MODULE_AUTHOR("Waveshare Team "); +MODULE_DESCRIPTION("Regulator device driver for Waveshare touchscreen"); +MODULE_LICENSE("GPL"); From b70b5e83ab6c25e4a76377c28729db3b24339ada Mon Sep 17 00:00:00 2001 From: Waveshare_Team Date: Mon, 30 Dec 2024 09:30:38 +0800 Subject: [PATCH 2/4] drivers/gpu/drm/panel : Add the device for the Waveshare DSI-TOUCH series panels. the driver are provided for the Waveshare DSI-TOUCH series panels, modelled after the other Ilitek controller drivers, but not limited to Ilitek series controllers. The aim is to offer a more consistent operation and experience for the Waveshare DSI-TOUCH series panels. Signed-off-by: Waveshare_Team --- drivers/gpu/drm/panel/Kconfig | 10 + drivers/gpu/drm/panel/Makefile | 1 + .../gpu/drm/panel/panel-waveshare-dsi-v2.c | 1353 +++++++++++++++++ 3 files changed, 1364 insertions(+) create mode 100644 drivers/gpu/drm/panel/panel-waveshare-dsi-v2.c diff --git a/drivers/gpu/drm/panel/Kconfig b/drivers/gpu/drm/panel/Kconfig index 307dd2c0fae413..1989f5ed56cd54 100644 --- a/drivers/gpu/drm/panel/Kconfig +++ b/drivers/gpu/drm/panel/Kconfig @@ -848,6 +848,16 @@ config DRM_PANEL_WAVESHARE_TOUCHSCREEN DSI Touchscreens. To compile this driver as a module, choose M here. +config DRM_PANEL_WAVESHARE_TOUCHSCREEN_V2 + tristate "Waveshare touchscreen panels V2" + depends on DRM_MIPI_DSI + depends on I2C + depends on BACKLIGHT_CLASS_DEVICE + help + Say Y here if you want to enable support for the Waveshare + DSI Touchscreens. To compile this driver as a module, + choose M here. + config DRM_PANEL_WIDECHIPS_WS2401 tristate "Widechips WS2401 DPI panel driver" depends on SPI && GPIOLIB diff --git a/drivers/gpu/drm/panel/Makefile b/drivers/gpu/drm/panel/Makefile index 9dd9b350380273..e19d1c03a1c679 100644 --- a/drivers/gpu/drm/panel/Makefile +++ b/drivers/gpu/drm/panel/Makefile @@ -86,5 +86,6 @@ obj-$(CONFIG_DRM_PANEL_VISIONOX_RM69299) += panel-visionox-rm69299.o obj-$(CONFIG_DRM_PANEL_VISIONOX_VTDR6130) += panel-visionox-vtdr6130.o obj-$(CONFIG_DRM_PANEL_VISIONOX_R66451) += panel-visionox-r66451.o obj-$(CONFIG_DRM_PANEL_WAVESHARE_TOUCHSCREEN) += panel-waveshare-dsi.o +obj-$(CONFIG_DRM_PANEL_WAVESHARE_TOUCHSCREEN_V2) += panel-waveshare-dsi-v2.o obj-$(CONFIG_DRM_PANEL_WIDECHIPS_WS2401) += panel-widechips-ws2401.o obj-$(CONFIG_DRM_PANEL_XINPENG_XPP055C272) += panel-xinpeng-xpp055c272.o diff --git a/drivers/gpu/drm/panel/panel-waveshare-dsi-v2.c b/drivers/gpu/drm/panel/panel-waveshare-dsi-v2.c new file mode 100644 index 00000000000000..863bec39a291e2 --- /dev/null +++ b/drivers/gpu/drm/panel/panel-waveshare-dsi-v2.c @@ -0,0 +1,1353 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Waveshare International Limited + * + * Based on panel-raspberrypi-touchscreen by Broadcom + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include