Skip to content

Commit 1c2f0a3

Browse files
Waveshare_Teampelwell
authored andcommitted
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 <support@waveshare.com>
1 parent eb460eb commit 1c2f0a3

File tree

3 files changed

+337
-0
lines changed

3 files changed

+337
-0
lines changed

drivers/regulator/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,16 @@ config REGULATOR_RASPBERRYPI_TOUCHSCREEN_V2
11521152
touchscreen unit. The regulator is used to enable power to the
11531153
display and to control backlight.
11541154

1155+
config REGULATOR_WAVESHARE_TOUCHSCREEN
1156+
tristate "Waveshare touchscreen panel regulator"
1157+
depends on BACKLIGHT_CLASS_DEVICE
1158+
depends on I2C
1159+
select REGMAP_I2C
1160+
help
1161+
This driver supports regulator on the waveshare
1162+
touchscreen unit. The regulator is used to enable power to the
1163+
display and to control backlight.
1164+
11551165
config REGULATOR_RC5T583
11561166
tristate "RICOH RC5T583 Power regulators"
11571167
depends on MFD_RC5T583

drivers/regulator/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ obj-$(CONFIG_REGULATOR_PCF50633) += pcf50633-regulator.o
136136
obj-$(CONFIG_REGULATOR_RAA215300) += raa215300.o
137137
obj-$(CONFIG_REGULATOR_RASPBERRYPI_TOUCHSCREEN_ATTINY) += rpi-panel-attiny-regulator.o
138138
obj-$(CONFIG_REGULATOR_RASPBERRYPI_TOUCHSCREEN_V2) += rpi-panel-v2-regulator.o
139+
obj-$(CONFIG_REGULATOR_WAVESHARE_TOUCHSCREEN) += waveshare-panel-regulator.o
139140
obj-$(CONFIG_REGULATOR_RC5T583) += rc5t583-regulator.o
140141
obj-$(CONFIG_REGULATOR_RK808) += rk808-regulator.o
141142
obj-$(CONFIG_REGULATOR_RN5T618) += rn5t618-regulator.o
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2024 Waveshare International Limited
4+
*
5+
* Based on rpi-panel-v2-regulator.c by Dave Stevenson <dave.stevenson@raspberrypi.com>
6+
*/
7+
8+
#include <linux/backlight.h>
9+
#include <linux/err.h>
10+
#include <linux/gpio.h>
11+
#include <linux/gpio/driver.h>
12+
#include <linux/module.h>
13+
#include <linux/regmap.h>
14+
#include <linux/regulator/driver.h>
15+
#include <linux/of.h>
16+
17+
/* I2C registers of the microcontroller. */
18+
#define REG_TP 0x94
19+
#define REG_LCD 0x95
20+
#define REG_PWM 0x96
21+
#define REG_SIZE 0x97
22+
#define REG_ID 0x98
23+
#define REG_VERSION 0x99
24+
25+
#define NUM_GPIO 16 /* Treat BL_ENABLE, LCD_RESET, TP_RESET as GPIOs */
26+
27+
struct waveshare_panel_lcd {
28+
struct mutex dir_lock;
29+
struct mutex pwr_lock;
30+
struct regmap *regmap;
31+
u16 poweron_state;
32+
u16 direction_state;
33+
34+
struct gpio_chip gc;
35+
struct gpio_desc *enable;
36+
};
37+
38+
static const struct regmap_config waveshare_panel_regmap_config = {
39+
.reg_bits = 8,
40+
.val_bits = 8,
41+
.max_register = REG_PWM,
42+
};
43+
44+
static int waveshare_panel_gpio_direction_in(struct gpio_chip *gc,
45+
unsigned int offset)
46+
{
47+
struct waveshare_panel_lcd *state = gpiochip_get_data(gc);
48+
49+
mutex_lock(&state->dir_lock);
50+
state->direction_state |= BIT(offset);
51+
mutex_unlock(&state->dir_lock);
52+
53+
return 0;
54+
}
55+
56+
static int waveshare_panel_gpio_direction_out(struct gpio_chip *gc,
57+
unsigned int offset, int val)
58+
{
59+
struct waveshare_panel_lcd *state = gpiochip_get_data(gc);
60+
u16 last_val;
61+
62+
mutex_lock(&state->dir_lock);
63+
state->direction_state &= ~BIT(offset);
64+
mutex_unlock(&state->dir_lock);
65+
66+
mutex_lock(&state->pwr_lock);
67+
last_val = state->poweron_state;
68+
if (val)
69+
last_val |= BIT(offset);
70+
else
71+
last_val &= ~BIT(offset);
72+
73+
state->poweron_state = last_val;
74+
mutex_unlock(&state->pwr_lock);
75+
76+
regmap_write(state->regmap, REG_TP, last_val >> 8);
77+
regmap_write(state->regmap, REG_LCD, last_val & 0xff);
78+
79+
return 0;
80+
}
81+
82+
static int waveshare_panel_gpio_get_direction(struct gpio_chip *gc,
83+
unsigned int offset)
84+
{
85+
struct waveshare_panel_lcd *state = gpiochip_get_data(gc);
86+
u16 last_val;
87+
88+
mutex_lock(&state->dir_lock);
89+
last_val = state->direction_state;
90+
mutex_unlock(&state->dir_lock);
91+
92+
if (last_val & BIT(offset))
93+
return GPIO_LINE_DIRECTION_IN;
94+
else
95+
return GPIO_LINE_DIRECTION_OUT;
96+
}
97+
98+
static int waveshare_panel_gpio_get(struct gpio_chip *gc, unsigned int offset)
99+
{
100+
struct waveshare_panel_lcd *state = gpiochip_get_data(gc);
101+
u16 pwr_state;
102+
103+
mutex_lock(&state->pwr_lock);
104+
pwr_state = state->poweron_state & BIT(offset);
105+
mutex_unlock(&state->pwr_lock);
106+
107+
if (pwr_state)
108+
return 1;
109+
else
110+
return 0;
111+
}
112+
113+
static void waveshare_panel_gpio_set(struct gpio_chip *gc, unsigned int offset,
114+
int value)
115+
{
116+
struct waveshare_panel_lcd *state = gpiochip_get_data(gc);
117+
u16 last_val;
118+
119+
if (offset >= NUM_GPIO)
120+
return;
121+
122+
mutex_lock(&state->pwr_lock);
123+
124+
last_val = state->poweron_state;
125+
if (value)
126+
last_val |= BIT(offset);
127+
else
128+
last_val &= ~BIT(offset);
129+
130+
state->poweron_state = last_val;
131+
132+
regmap_write(state->regmap, REG_TP, last_val >> 8);
133+
regmap_write(state->regmap, REG_LCD, last_val & 0xff);
134+
135+
mutex_unlock(&state->pwr_lock);
136+
}
137+
138+
static int waveshare_panel_update_status(struct backlight_device *bl)
139+
{
140+
struct waveshare_panel_lcd *state = bl_get_data(bl);
141+
int brightness = bl->props.brightness;
142+
143+
if (bl->props.power != FB_BLANK_UNBLANK ||
144+
bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
145+
brightness = 0;
146+
147+
if (state->enable) {
148+
if (brightness)
149+
gpiod_set_value_cansleep(state->enable,
150+
1); // Enable BL_EN
151+
else
152+
gpiod_set_value_cansleep(state->enable,
153+
0); // Disable BL_EN
154+
}
155+
156+
return regmap_write(state->regmap, REG_PWM, brightness);
157+
}
158+
159+
static const struct backlight_ops waveshare_panel_bl = {
160+
.update_status = waveshare_panel_update_status,
161+
};
162+
163+
static int waveshare_panel_i2c_read(struct i2c_client *client, u8 reg,
164+
unsigned int *buf)
165+
{
166+
struct i2c_msg msgs[1];
167+
u8 addr_buf[1] = { reg };
168+
u8 data_buf[1] = {
169+
0,
170+
};
171+
int ret;
172+
173+
/* Write register address */
174+
msgs[0].addr = client->addr;
175+
msgs[0].flags = 0;
176+
msgs[0].len = ARRAY_SIZE(addr_buf);
177+
msgs[0].buf = addr_buf;
178+
179+
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
180+
if (ret != ARRAY_SIZE(msgs))
181+
return -EIO;
182+
183+
usleep_range(5000, 10000);
184+
185+
/* Read data from register */
186+
msgs[0].addr = client->addr;
187+
msgs[0].flags = I2C_M_RD;
188+
msgs[0].len = 1;
189+
msgs[0].buf = data_buf;
190+
191+
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
192+
if (ret != ARRAY_SIZE(msgs))
193+
return -EIO;
194+
195+
*buf = data_buf[0];
196+
return 0;
197+
}
198+
199+
/*
200+
* I2C driver interface functions
201+
*/
202+
static int waveshare_panel_i2c_probe(struct i2c_client *i2c)
203+
{
204+
struct backlight_properties props = {};
205+
struct backlight_device *bl;
206+
struct waveshare_panel_lcd *state;
207+
struct regmap *regmap;
208+
unsigned int data;
209+
int ret;
210+
211+
state = devm_kzalloc(&i2c->dev, sizeof(*state), GFP_KERNEL);
212+
if (!state)
213+
return -ENOMEM;
214+
215+
mutex_init(&state->dir_lock);
216+
mutex_init(&state->pwr_lock);
217+
218+
i2c_set_clientdata(i2c, state);
219+
220+
regmap = devm_regmap_init_i2c(i2c, &waveshare_panel_regmap_config);
221+
if (IS_ERR(regmap)) {
222+
ret = PTR_ERR(regmap);
223+
dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
224+
ret);
225+
goto error;
226+
}
227+
228+
ret = waveshare_panel_i2c_read(i2c, REG_ID, &data);
229+
if (ret == 0)
230+
dev_info(&i2c->dev, "waveshare panel hw id = 0x%x\n", data);
231+
232+
ret = waveshare_panel_i2c_read(i2c, REG_SIZE, &data);
233+
if (ret == 0)
234+
dev_info(&i2c->dev, "waveshare panel size = %d\n", data);
235+
236+
ret = waveshare_panel_i2c_read(i2c, REG_VERSION, &data);
237+
if (ret == 0)
238+
dev_info(&i2c->dev, "waveshare panel mcu version = 0x%x\n",
239+
data);
240+
241+
state->direction_state = 0;
242+
state->poweron_state = BIT(9) | BIT(8) | BIT(4) | BIT(0); // Enable VCC
243+
regmap_write(regmap, REG_TP, state->poweron_state >> 8);
244+
regmap_write(regmap, REG_LCD, state->poweron_state & 0xff);
245+
msleep(20);
246+
247+
state->regmap = regmap;
248+
state->gc.parent = &i2c->dev;
249+
state->gc.label = i2c->name;
250+
state->gc.owner = THIS_MODULE;
251+
state->gc.base = -1;
252+
state->gc.ngpio = NUM_GPIO;
253+
254+
state->gc.get = waveshare_panel_gpio_get;
255+
state->gc.set = waveshare_panel_gpio_set;
256+
state->gc.direction_input = waveshare_panel_gpio_direction_in;
257+
state->gc.direction_output = waveshare_panel_gpio_direction_out;
258+
state->gc.get_direction = waveshare_panel_gpio_get_direction;
259+
state->gc.can_sleep = true;
260+
261+
ret = devm_gpiochip_add_data(&i2c->dev, &state->gc, state);
262+
if (ret) {
263+
dev_err(&i2c->dev, "Failed to create gpiochip: %d\n", ret);
264+
goto error;
265+
}
266+
267+
state->enable =
268+
devm_gpiod_get_optional(&i2c->dev, "enable", GPIOD_OUT_LOW);
269+
if (IS_ERR(state->enable))
270+
return dev_err_probe(&i2c->dev, PTR_ERR(state->enable),
271+
"Couldn't get our enable GPIO\n");
272+
273+
props.type = BACKLIGHT_RAW;
274+
props.max_brightness = 255;
275+
bl = devm_backlight_device_register(&i2c->dev, dev_name(&i2c->dev),
276+
&i2c->dev, state,
277+
&waveshare_panel_bl, &props);
278+
if (IS_ERR(bl))
279+
return PTR_ERR(bl);
280+
281+
bl->props.brightness = 255;
282+
283+
return 0;
284+
285+
error:
286+
mutex_destroy(&state->dir_lock);
287+
mutex_destroy(&state->pwr_lock);
288+
return ret;
289+
}
290+
291+
static void waveshare_panel_i2c_remove(struct i2c_client *client)
292+
{
293+
struct waveshare_panel_lcd *state = i2c_get_clientdata(client);
294+
295+
mutex_destroy(&state->dir_lock);
296+
mutex_destroy(&state->pwr_lock);
297+
}
298+
299+
static void waveshare_panel_i2c_shutdown(struct i2c_client *client)
300+
{
301+
struct waveshare_panel_lcd *state = i2c_get_clientdata(client);
302+
303+
regmap_write(state->regmap, REG_PWM, 0);
304+
}
305+
306+
static const struct of_device_id waveshare_panel_dt_ids[] = {
307+
{ .compatible = "waveshare,touchscreen-panel-regulator" },
308+
{},
309+
};
310+
MODULE_DEVICE_TABLE(of, waveshare_panel_dt_ids);
311+
312+
static struct i2c_driver waveshare_panel_regulator_driver = {
313+
.driver = {
314+
.name = "waveshare_touchscreen",
315+
.of_match_table = of_match_ptr(waveshare_panel_dt_ids),
316+
},
317+
.probe = waveshare_panel_i2c_probe,
318+
.remove = waveshare_panel_i2c_remove,
319+
.shutdown = waveshare_panel_i2c_shutdown,
320+
};
321+
322+
module_i2c_driver(waveshare_panel_regulator_driver);
323+
324+
MODULE_AUTHOR("Waveshare Team <support@waveshare.com>");
325+
MODULE_DESCRIPTION("Regulator device driver for Waveshare touchscreen");
326+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)