1 From bf7ce2d1c54900ac19fef0bf0e317732682820e5 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Tue, 1 Feb 2022 15:27:01 +0000
4 Subject: [PATCH] drm/panel/panel-sitronix-st7701: Support SPI config
7 The ST7701 supports numerous different interface mechanisms for
8 MIPI DSI, RGB, or SPI. The driver was only implementing DSI input,
9 so add RGB parallel input with SPI configuration.
11 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
13 drivers/gpu/drm/panel/panel-sitronix-st7701.c | 374 ++++++++++++++++--
14 1 file changed, 351 insertions(+), 23 deletions(-)
16 --- a/drivers/gpu/drm/panel/panel-sitronix-st7701.c
17 +++ b/drivers/gpu/drm/panel/panel-sitronix-st7701.c
19 #include <drm/drm_mipi_dsi.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
22 +#include <drm/drm_print.h>
24 #include <linux/bitfield.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/delay.h>
27 +#include <linux/media-bus-format.h>
28 #include <linux/module.h>
29 #include <linux/of_device.h>
30 #include <linux/regulator/consumer.h>
31 +#include <linux/spi/spi.h>
33 #include <video/mipi_display.h>
35 +#define SPI_DATA_FLAG 0x100
37 /* Command2 BKx selection command */
38 #define DSI_CMD2BKX_SEL 0xFF
41 #define DSI_CMD2BK1_SEL 0x11
42 #define DSI_CMD2BK3_SEL 0x13
43 #define DSI_CMD2BKX_SEL_NONE 0x00
44 +#define SPI_CMD2BK3_SEL (SPI_DATA_FLAG | DSI_CMD2BK3_SEL)
45 +#define SPI_CMD2BK1_SEL (SPI_DATA_FLAG | DSI_CMD2BK1_SEL)
46 +#define SPI_CMD2BK0_SEL (SPI_DATA_FLAG | DSI_CMD2BK0_SEL)
47 +#define SPI_CMD2BKX_SEL_NONE (SPI_DATA_FLAG | DSI_CMD2BKX_SEL_NONE)
49 /* Command2, BK0 bytes */
50 #define DSI_CMD2_BK0_GAMCTRL_AJ_MASK GENMASK(7, 6)
51 @@ -112,11 +121,23 @@ enum op_bias {
57 +enum st7701_ctrl_if {
62 struct st7701_panel_desc {
63 const struct drm_display_mode *mode;
65 enum mipi_dsi_pixel_format format;
66 + u32 mediabus_format;
67 unsigned int panel_sleep_delay;
68 + void (*init_sequence)(struct st7701 *st7701);
69 + unsigned int conn_type;
70 + enum st7701_ctrl_if interface;
73 /* TFT matrix driver configuration, panel specific. */
74 const u8 pv_gamma[16]; /* Positive voltage gamma control */
75 @@ -142,6 +163,9 @@ struct st7701_panel_desc {
77 struct drm_panel panel;
78 struct mipi_dsi_device *dsi;
79 + struct spi_device *spi;
80 + const struct device *dev;
82 const struct st7701_panel_desc *desc;
84 struct regulator_bulk_data supplies[2];
85 @@ -191,7 +215,23 @@ static u8 st7701_vgls_map(struct st7701
89 -static void st7701_init_sequence(struct st7701 *st7701)
90 +#define ST7701_SPI(st7701, seq...) \
92 + const u16 d[] = { seq }; \
93 + struct spi_transfer xfer = { }; \
94 + struct spi_message spi; \
96 + spi_message_init(&spi); \
99 + xfer.bits_per_word = 9; \
100 + xfer.len = sizeof(u16) * ARRAY_SIZE(d); \
102 + spi_message_add_tail(&xfer, &spi); \
103 + spi_sync((st7701)->spi, &spi); \
106 +static void ts8550b_init_sequence(struct st7701 *st7701)
108 const struct st7701_panel_desc *desc = st7701->desc;
109 const struct drm_display_mode *mode = desc->mode;
110 @@ -404,6 +444,111 @@ static void dmt028vghmcmi_1a_gip_sequenc
111 ST7701_DSI(st7701, 0x3A, 0x70);
114 +static void txw210001b0_init_sequence(struct st7701 *st7701)
116 + ST7701_SPI(st7701, MIPI_DCS_SOFT_RESET);
118 + usleep_range(5000, 7000);
120 + ST7701_SPI(st7701, DSI_CMD2BKX_SEL,
121 + 0x177, 0x101, 0x100, 0x100, SPI_CMD2BK0_SEL);
123 + ST7701_SPI(st7701, DSI_CMD2_BK0_LNESET, 0x13B, 0x100);
125 + ST7701_SPI(st7701, DSI_CMD2_BK0_PORCTRL, 0x10B, 0x102);
127 + ST7701_SPI(st7701, DSI_CMD2_BK0_INVSEL, 0x100, 0x102);
129 + ST7701_SPI(st7701, 0xCC, 0x110);
133 + * Positive Voltage Gamma Control
135 + ST7701_SPI(st7701, DSI_CMD2_BK0_PVGAMCTRL,
136 + 0x102, 0x113, 0x11B, 0x10D, 0x110, 0x105, 0x108, 0x107,
137 + 0x107, 0x124, 0x104, 0x111, 0x10E, 0x12C, 0x133, 0x11D);
139 + /* Negative Voltage Gamma Control */
140 + ST7701_SPI(st7701, DSI_CMD2_BK0_NVGAMCTRL,
141 + 0x105, 0x113, 0x11B, 0x10D, 0x111, 0x105, 0x108, 0x107,
142 + 0x107, 0x124, 0x104, 0x111, 0x10E, 0x12C, 0x133, 0x11D);
144 + ST7701_SPI(st7701, DSI_CMD2BKX_SEL,
145 + 0x177, 0x101, 0x100, 0x100, SPI_CMD2BK1_SEL);
147 + ST7701_SPI(st7701, DSI_CMD2_BK1_VRHS, 0x15D);
149 + ST7701_SPI(st7701, DSI_CMD2_BK1_VCOM, 0x143);
151 + ST7701_SPI(st7701, DSI_CMD2_BK1_VGHSS, 0x181);
153 + ST7701_SPI(st7701, DSI_CMD2_BK1_TESTCMD, 0x180);
155 + ST7701_SPI(st7701, DSI_CMD2_BK1_VGLS, 0x143);
157 + ST7701_SPI(st7701, DSI_CMD2_BK1_PWCTLR1, 0x185);
159 + ST7701_SPI(st7701, DSI_CMD2_BK1_PWCTLR2, 0x120);
161 + ST7701_SPI(st7701, DSI_CMD2_BK1_SPD1, 0x178);
163 + ST7701_SPI(st7701, DSI_CMD2_BK1_SPD2, 0x178);
165 + ST7701_SPI(st7701, DSI_CMD2_BK1_MIPISET1, 0x188);
167 + ST7701_SPI(st7701, 0xE0, 0x100, 0x100, 0x102);
169 + ST7701_SPI(st7701, 0xE1,
170 + 0x103, 0x1A0, 0x100, 0x100, 0x104, 0x1A0, 0x100, 0x100,
171 + 0x100, 0x120, 0x120);
173 + ST7701_SPI(st7701, 0xE2,
174 + 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100,
175 + 0x100, 0x100, 0x100, 0x100, 0x100);
177 + ST7701_SPI(st7701, 0xE3, 0x100, 0x100, 0x111, 0x100);
179 + ST7701_SPI(st7701, 0xE4, 0x122, 0x100);
181 + ST7701_SPI(st7701, 0xE5,
182 + 0x105, 0x1EC, 0x1A0, 0x1A0, 0x107, 0x1EE, 0x1A0, 0x1A0,
183 + 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100);
185 + ST7701_SPI(st7701, 0xE6, 0x100, 0x100, 0x111, 0x100);
187 + ST7701_SPI(st7701, 0xE7, 0x122, 0x100);
189 + ST7701_SPI(st7701, 0xE8,
190 + 0x106, 0x1ED, 0x1A0, 0x1A0, 0x108, 0x1EF, 0x1A0, 0x1A0,
191 + 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100);
193 + ST7701_SPI(st7701, 0xEB,
194 + 0x100, 0x100, 0x140, 0x140, 0x100, 0x100, 0x100);
196 + ST7701_SPI(st7701, 0xED,
197 + 0x1FF, 0x1FF, 0x1FF, 0x1BA, 0x10A, 0x1BF, 0x145, 0x1FF,
198 + 0x1FF, 0x154, 0x1FB, 0x1A0, 0x1AB, 0x1FF, 0x1FF, 0x1FF);
200 + ST7701_SPI(st7701, 0xEF, 0x110, 0x10D, 0x104, 0x108, 0x13F, 0x11F);
202 + ST7701_SPI(st7701, DSI_CMD2BKX_SEL,
203 + 0x177, 0x101, 0x100, 0x100, SPI_CMD2BK3_SEL);
205 + ST7701_SPI(st7701, 0xEF, 0x108);
207 + ST7701_SPI(st7701, DSI_CMD2BKX_SEL,
208 + 0x177, 0x101, 0x100, 0x100, SPI_CMD2BKX_SEL_NONE);
210 + ST7701_SPI(st7701, 0xCD, 0x108); /* RGB format COLCTRL */
212 + ST7701_SPI(st7701, 0x36, 0x108); /* MadCtl */
214 + ST7701_SPI(st7701, 0x3A, 0x166); /* Colmod */
216 + ST7701_SPI(st7701, MIPI_DCS_EXIT_SLEEP_MODE);
219 static int st7701_prepare(struct drm_panel *panel)
221 struct st7701 *st7701 = panel_to_st7701(panel);
222 @@ -420,7 +565,7 @@ static int st7701_prepare(struct drm_pan
223 gpiod_set_value(st7701->reset, 1);
226 - st7701_init_sequence(st7701);
227 + st7701->desc->init_sequence(st7701);
229 if (st7701->desc->gip_sequence)
230 st7701->desc->gip_sequence(st7701);
231 @@ -436,7 +581,15 @@ static int st7701_enable(struct drm_pane
233 struct st7701 *st7701 = panel_to_st7701(panel);
235 - ST7701_DSI(st7701, MIPI_DCS_SET_DISPLAY_ON, 0x00);
236 + switch (st7701->desc->interface) {
237 + case ST7701_CTRL_DSI:
238 + ST7701_DSI(st7701, MIPI_DCS_SET_DISPLAY_ON, 0x00);
240 + case ST7701_CTRL_SPI:
241 + ST7701_SPI(st7701, MIPI_DCS_SET_DISPLAY_ON);
248 @@ -445,7 +598,14 @@ static int st7701_disable(struct drm_pan
250 struct st7701 *st7701 = panel_to_st7701(panel);
252 - ST7701_DSI(st7701, MIPI_DCS_SET_DISPLAY_OFF, 0x00);
253 + switch (st7701->desc->interface) {
254 + case ST7701_CTRL_DSI:
255 + ST7701_DSI(st7701, MIPI_DCS_SET_DISPLAY_OFF, 0x00);
257 + case ST7701_CTRL_SPI:
258 + ST7701_SPI(st7701, MIPI_DCS_SET_DISPLAY_OFF);
264 @@ -454,7 +614,14 @@ static int st7701_unprepare(struct drm_p
266 struct st7701 *st7701 = panel_to_st7701(panel);
268 - ST7701_DSI(st7701, MIPI_DCS_ENTER_SLEEP_MODE, 0x00);
269 + switch (st7701->desc->interface) {
270 + case ST7701_CTRL_DSI:
271 + ST7701_DSI(st7701, MIPI_DCS_ENTER_SLEEP_MODE, 0x00);
273 + case ST7701_CTRL_SPI:
274 + ST7701_SPI(st7701, MIPI_DCS_ENTER_SLEEP_MODE);
278 msleep(st7701->sleep_delay);
280 @@ -485,7 +652,7 @@ static int st7701_get_modes(struct drm_p
282 mode = drm_mode_duplicate(connector->dev, desc_mode);
284 - dev_err(&st7701->dsi->dev, "failed to add mode %ux%u@%u\n",
285 + dev_err(st7701->dev, "failed to add mode %ux%u@%u\n",
286 desc_mode->hdisplay, desc_mode->vdisplay,
287 drm_mode_vrefresh(desc_mode));
289 @@ -494,9 +661,18 @@ static int st7701_get_modes(struct drm_p
290 drm_mode_set_name(mode);
291 drm_mode_probed_add(connector, mode);
293 + if (st7701->desc->mediabus_format)
294 + drm_display_info_set_bus_formats(&connector->display_info,
295 + &st7701->desc->mediabus_format,
297 + connector->display_info.bus_flags = 0;
299 connector->display_info.width_mm = desc_mode->width_mm;
300 connector->display_info.height_mm = desc_mode->height_mm;
302 + if (st7701->desc->bus_flags)
303 + connector->display_info.bus_flags = st7701->desc->bus_flags;
308 @@ -532,6 +708,9 @@ static const struct st7701_panel_desc ts
310 .format = MIPI_DSI_FMT_RGB888,
311 .panel_sleep_delay = 80, /* panel need extra 80ms for sleep out cmd */
312 + .init_sequence = ts8550b_init_sequence,
313 + .conn_type = DRM_MODE_CONNECTOR_DSI,
314 + .interface = ST7701_CTRL_DSI,
317 CFIELD_PREP(DSI_CMD2_BK0_GAMCTRL_AJ_MASK, 0) |
318 @@ -708,38 +887,74 @@ static const struct st7701_panel_desc dm
319 .gip_sequence = dmt028vghmcmi_1a_gip_sequence,
322 -static int st7701_dsi_probe(struct mipi_dsi_device *dsi)
323 +static const struct drm_display_mode txw210001b0_mode = {
327 + .hsync_start = 480 + 10,
328 + .hsync_end = 480 + 10 + 16,
329 + .htotal = 480 + 10 + 16 + 56,
332 + .vsync_start = 480 + 15,
333 + .vsync_end = 480 + 15 + 60,
334 + .vtotal = 480 + 15 + 60 + 15,
338 + .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
340 + .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
343 +static const struct st7701_panel_desc txw210001b0_desc = {
344 + .mode = &txw210001b0_mode,
345 + .mediabus_format = MEDIA_BUS_FMT_RGB888_1X24,
346 + .init_sequence = txw210001b0_init_sequence,
347 + .conn_type = DRM_MODE_CONNECTOR_DPI,
348 + .interface = ST7701_CTRL_SPI,
349 + .bus_flags = DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
352 +static const struct st7701_panel_desc hyperpixel2r_desc = {
353 + .mode = &txw210001b0_mode,
354 + .mediabus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
355 + .init_sequence = txw210001b0_init_sequence,
356 + .conn_type = DRM_MODE_CONNECTOR_DPI,
357 + .interface = ST7701_CTRL_SPI,
358 + .bus_flags = DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
361 +static int st7701_probe(struct device *dev, struct st7701 **ret_st7701)
363 const struct st7701_panel_desc *desc;
364 struct st7701 *st7701;
367 - st7701 = devm_kzalloc(&dsi->dev, sizeof(*st7701), GFP_KERNEL);
368 + st7701 = devm_kzalloc(dev, sizeof(*st7701), GFP_KERNEL);
372 - desc = of_device_get_match_data(&dsi->dev);
373 - dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
374 - MIPI_DSI_MODE_LPM | MIPI_DSI_CLOCK_NON_CONTINUOUS;
375 - dsi->format = desc->format;
376 - dsi->lanes = desc->lanes;
377 + desc = of_device_get_match_data(dev);
381 st7701->supplies[0].supply = "VCC";
382 st7701->supplies[1].supply = "IOVCC";
384 - ret = devm_regulator_bulk_get(&dsi->dev, ARRAY_SIZE(st7701->supplies),
385 + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st7701->supplies),
390 - st7701->reset = devm_gpiod_get(&dsi->dev, "reset", GPIOD_OUT_LOW);
391 + st7701->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
392 if (IS_ERR(st7701->reset)) {
393 - dev_err(&dsi->dev, "Couldn't get our reset GPIO\n");
394 + dev_err(dev, "Couldn't get our reset GPIO\n");
395 return PTR_ERR(st7701->reset);
398 - drm_panel_init(&st7701->panel, &dsi->dev, &st7701_funcs,
399 - DRM_MODE_CONNECTOR_DSI);
400 + drm_panel_init(&st7701->panel, dev, &st7701_funcs,
404 * Once sleep out has been issued, ST7701 IC required to wait 120ms
405 @@ -758,9 +973,30 @@ static int st7701_dsi_probe(struct mipi_
407 drm_panel_add(&st7701->panel);
409 + st7701->desc = desc;
412 + *ret_st7701 = st7701;
417 +static int st7701_dsi_probe(struct mipi_dsi_device *dsi)
419 + struct st7701 *st7701;
422 + ret = st7701_probe(&dsi->dev, &st7701);
427 + dsi->mode_flags = MIPI_DSI_MODE_VIDEO;
428 + dsi->format = st7701->desc->format;
429 + dsi->lanes = st7701->desc->lanes;
431 mipi_dsi_set_drvdata(dsi, st7701);
433 - st7701->desc = desc;
435 ret = mipi_dsi_attach(dsi);
437 @@ -781,22 +1017,114 @@ static void st7701_dsi_remove(struct mip
438 drm_panel_remove(&st7701->panel);
441 -static const struct of_device_id st7701_of_match[] = {
442 +static const struct of_device_id st7701_dsi_of_match[] = {
443 { .compatible = "densitron,dmt028vghmcmi-1a", .data = &dmt028vghmcmi_1a_desc },
444 { .compatible = "techstar,ts8550b", .data = &ts8550b_desc },
447 -MODULE_DEVICE_TABLE(of, st7701_of_match);
448 +MODULE_DEVICE_TABLE(of, st7701_dsi_of_match);
450 static struct mipi_dsi_driver st7701_dsi_driver = {
451 .probe = st7701_dsi_probe,
452 .remove = st7701_dsi_remove,
455 - .of_match_table = st7701_of_match,
456 + .of_match_table = st7701_dsi_of_match,
459 -module_mipi_dsi_driver(st7701_dsi_driver);
461 +/* SPI display probe */
462 +static const struct of_device_id st7701_spi_of_match[] = {
463 + { .compatible = "txw,txw210001b0",
464 + .data = &txw210001b0_desc,
466 + .compatible = "pimoroni,hyperpixel2round",
467 + .data = &hyperpixel2r_desc,
472 +MODULE_DEVICE_TABLE(of, st7701_spi_of_match);
474 +static int st7701_spi_probe(struct spi_device *spi)
476 + struct st7701 *st7701;
479 + spi->mode = SPI_MODE_3;
480 + spi->bits_per_word = 9;
481 + ret = spi_setup(spi);
483 + dev_err(&spi->dev, "failed to setup SPI: %d\n", ret);
487 + ret = st7701_probe(&spi->dev, &st7701);
492 + spi_set_drvdata(spi, st7701);
498 +static void st7701_spi_remove(struct spi_device *spi)
500 + struct st7701 *ctx = spi_get_drvdata(spi);
502 + drm_panel_remove(&ctx->panel);
505 +static const struct spi_device_id st7701_spi_ids[] = {
506 + { "txw210001b0", 0 },
507 + { "hyperpixel2round", 0 },
510 +MODULE_DEVICE_TABLE(spi, st7701_spi_ids);
512 +static struct spi_driver st7701_spi_driver = {
513 + .probe = st7701_spi_probe,
514 + .remove = st7701_spi_remove,
517 + .of_match_table = st7701_spi_of_match,
519 + .id_table = st7701_spi_ids,
522 +static int __init panel_st7701_init(void)
526 + err = spi_register_driver(&st7701_spi_driver);
530 + if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
531 + err = mipi_dsi_driver_register(&st7701_dsi_driver);
533 + goto err_did_spi_register;
538 +err_did_spi_register:
539 + spi_unregister_driver(&st7701_spi_driver);
543 +module_init(panel_st7701_init);
545 +static void __exit panel_st7701_exit(void)
547 + if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
548 + mipi_dsi_driver_unregister(&st7701_dsi_driver);
550 + spi_unregister_driver(&st7701_spi_driver);
552 +module_exit(panel_st7701_exit);
554 MODULE_AUTHOR("Jagan Teki <jagan@amarulasolutions.com>");
555 MODULE_DESCRIPTION("Sitronix ST7701 LCD Panel Driver");