]> Git Repo - linux.git/blob - drivers/gpu/drm/tiny/st7735r.c
Merge tag 'linux-watchdog-6.14-rc1' of git://www.linux-watchdog.org/linux-watchdog
[linux.git] / drivers / gpu / drm / tiny / st7735r.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DRM driver for display panels connected to a Sitronix ST7715R or ST7735R
4  * display controller in SPI mode.
5  *
6  * Copyright 2017 David Lechner <[email protected]>
7  * Copyright (C) 2019 Glider bvba
8  */
9
10 #include <linux/backlight.h>
11 #include <linux/delay.h>
12 #include <linux/dma-buf.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/spi/spi.h>
17 #include <video/mipi_display.h>
18
19 #include <drm/clients/drm_client_setup.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fbdev_dma.h>
23 #include <drm/drm_gem_atomic_helper.h>
24 #include <drm/drm_gem_dma_helper.h>
25 #include <drm/drm_managed.h>
26 #include <drm/drm_mipi_dbi.h>
27
28 #define ST7735R_FRMCTR1         0xb1
29 #define ST7735R_FRMCTR2         0xb2
30 #define ST7735R_FRMCTR3         0xb3
31 #define ST7735R_INVCTR          0xb4
32 #define ST7735R_PWCTR1          0xc0
33 #define ST7735R_PWCTR2          0xc1
34 #define ST7735R_PWCTR3          0xc2
35 #define ST7735R_PWCTR4          0xc3
36 #define ST7735R_PWCTR5          0xc4
37 #define ST7735R_VMCTR1          0xc5
38 #define ST7735R_GAMCTRP1        0xe0
39 #define ST7735R_GAMCTRN1        0xe1
40
41 #define ST7735R_MY      BIT(7)
42 #define ST7735R_MX      BIT(6)
43 #define ST7735R_MV      BIT(5)
44 #define ST7735R_RGB     BIT(3)
45
46 struct st7735r_cfg {
47         const struct drm_display_mode mode;
48         unsigned int left_offset;
49         unsigned int top_offset;
50         unsigned int write_only:1;
51         unsigned int rgb:1;             /* RGB (vs. BGR) */
52 };
53
54 struct st7735r_priv {
55         struct mipi_dbi_dev dbidev;     /* Must be first for .release() */
56         const struct st7735r_cfg *cfg;
57 };
58
59 static void st7735r_pipe_enable(struct drm_simple_display_pipe *pipe,
60                                 struct drm_crtc_state *crtc_state,
61                                 struct drm_plane_state *plane_state)
62 {
63         struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
64         struct st7735r_priv *priv = container_of(dbidev, struct st7735r_priv,
65                                                  dbidev);
66         struct mipi_dbi *dbi = &dbidev->dbi;
67         int ret, idx;
68         u8 addr_mode;
69
70         if (!drm_dev_enter(pipe->crtc.dev, &idx))
71                 return;
72
73         DRM_DEBUG_KMS("\n");
74
75         ret = mipi_dbi_poweron_reset(dbidev);
76         if (ret)
77                 goto out_exit;
78
79         msleep(150);
80
81         mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
82         msleep(500);
83
84         mipi_dbi_command(dbi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d);
85         mipi_dbi_command(dbi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d);
86         mipi_dbi_command(dbi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
87                          0x2d);
88         mipi_dbi_command(dbi, ST7735R_INVCTR, 0x07);
89         mipi_dbi_command(dbi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84);
90         mipi_dbi_command(dbi, ST7735R_PWCTR2, 0xc5);
91         mipi_dbi_command(dbi, ST7735R_PWCTR3, 0x0a, 0x00);
92         mipi_dbi_command(dbi, ST7735R_PWCTR4, 0x8a, 0x2a);
93         mipi_dbi_command(dbi, ST7735R_PWCTR5, 0x8a, 0xee);
94         mipi_dbi_command(dbi, ST7735R_VMCTR1, 0x0e);
95         mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE);
96         switch (dbidev->rotation) {
97         default:
98                 addr_mode = ST7735R_MX | ST7735R_MY;
99                 break;
100         case 90:
101                 addr_mode = ST7735R_MX | ST7735R_MV;
102                 break;
103         case 180:
104                 addr_mode = 0;
105                 break;
106         case 270:
107                 addr_mode = ST7735R_MY | ST7735R_MV;
108                 break;
109         }
110
111         if (priv->cfg->rgb)
112                 addr_mode |= ST7735R_RGB;
113
114         mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
115         mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT,
116                          MIPI_DCS_PIXEL_FMT_16BIT);
117         mipi_dbi_command(dbi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37,
118                          0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
119                          0x03, 0x10);
120         mipi_dbi_command(dbi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e,
121                          0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
122                          0x02, 0x10);
123         mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
124
125         msleep(100);
126
127         mipi_dbi_command(dbi, MIPI_DCS_ENTER_NORMAL_MODE);
128
129         msleep(20);
130
131         mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
132 out_exit:
133         drm_dev_exit(idx);
134 }
135
136 static const struct drm_simple_display_pipe_funcs st7735r_pipe_funcs = {
137         DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(st7735r_pipe_enable),
138 };
139
140 static const struct st7735r_cfg jd_t18003_t01_cfg = {
141         .mode           = { DRM_SIMPLE_MODE(128, 160, 28, 35) },
142         /* Cannot read from Adafruit 1.8" display via SPI */
143         .write_only     = true,
144 };
145
146 static const struct st7735r_cfg rh128128t_cfg = {
147         .mode           = { DRM_SIMPLE_MODE(128, 128, 25, 26) },
148         .left_offset    = 2,
149         .top_offset     = 3,
150         .rgb            = true,
151 };
152
153 DEFINE_DRM_GEM_DMA_FOPS(st7735r_fops);
154
155 static const struct drm_driver st7735r_driver = {
156         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
157         .fops                   = &st7735r_fops,
158         DRM_GEM_DMA_DRIVER_OPS_VMAP,
159         DRM_FBDEV_DMA_DRIVER_OPS,
160         .debugfs_init           = mipi_dbi_debugfs_init,
161         .name                   = "st7735r",
162         .desc                   = "Sitronix ST7735R",
163         .major                  = 1,
164         .minor                  = 0,
165 };
166
167 static const struct of_device_id st7735r_of_match[] = {
168         { .compatible = "jianda,jd-t18003-t01", .data = &jd_t18003_t01_cfg },
169         { .compatible = "okaya,rh128128t", .data = &rh128128t_cfg },
170         { },
171 };
172 MODULE_DEVICE_TABLE(of, st7735r_of_match);
173
174 static const struct spi_device_id st7735r_id[] = {
175         { "jd-t18003-t01", (uintptr_t)&jd_t18003_t01_cfg },
176         { "rh128128t", (uintptr_t)&rh128128t_cfg },
177         { },
178 };
179 MODULE_DEVICE_TABLE(spi, st7735r_id);
180
181 static int st7735r_probe(struct spi_device *spi)
182 {
183         struct device *dev = &spi->dev;
184         const struct st7735r_cfg *cfg;
185         struct mipi_dbi_dev *dbidev;
186         struct st7735r_priv *priv;
187         struct drm_device *drm;
188         struct mipi_dbi *dbi;
189         struct gpio_desc *dc;
190         u32 rotation = 0;
191         int ret;
192
193         cfg = device_get_match_data(&spi->dev);
194         if (!cfg)
195                 cfg = (void *)spi_get_device_id(spi)->driver_data;
196
197         priv = devm_drm_dev_alloc(dev, &st7735r_driver,
198                                   struct st7735r_priv, dbidev.drm);
199         if (IS_ERR(priv))
200                 return PTR_ERR(priv);
201
202         dbidev = &priv->dbidev;
203         priv->cfg = cfg;
204
205         dbi = &dbidev->dbi;
206         drm = &dbidev->drm;
207
208         dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
209         if (IS_ERR(dbi->reset))
210                 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
211
212         dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
213         if (IS_ERR(dc))
214                 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
215
216         dbidev->backlight = devm_of_find_backlight(dev);
217         if (IS_ERR(dbidev->backlight))
218                 return PTR_ERR(dbidev->backlight);
219
220         device_property_read_u32(dev, "rotation", &rotation);
221
222         ret = mipi_dbi_spi_init(spi, dbi, dc);
223         if (ret)
224                 return ret;
225
226         if (cfg->write_only)
227                 dbi->read_commands = NULL;
228
229         dbidev->left_offset = cfg->left_offset;
230         dbidev->top_offset = cfg->top_offset;
231
232         ret = mipi_dbi_dev_init(dbidev, &st7735r_pipe_funcs, &cfg->mode,
233                                 rotation);
234         if (ret)
235                 return ret;
236
237         drm_mode_config_reset(drm);
238
239         ret = drm_dev_register(drm, 0);
240         if (ret)
241                 return ret;
242
243         spi_set_drvdata(spi, drm);
244
245         drm_client_setup(drm, NULL);
246
247         return 0;
248 }
249
250 static void st7735r_remove(struct spi_device *spi)
251 {
252         struct drm_device *drm = spi_get_drvdata(spi);
253
254         drm_dev_unplug(drm);
255         drm_atomic_helper_shutdown(drm);
256 }
257
258 static void st7735r_shutdown(struct spi_device *spi)
259 {
260         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
261 }
262
263 static struct spi_driver st7735r_spi_driver = {
264         .driver = {
265                 .name = "st7735r",
266                 .of_match_table = st7735r_of_match,
267         },
268         .id_table = st7735r_id,
269         .probe = st7735r_probe,
270         .remove = st7735r_remove,
271         .shutdown = st7735r_shutdown,
272 };
273 module_spi_driver(st7735r_spi_driver);
274
275 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
276 MODULE_AUTHOR("David Lechner <[email protected]>");
277 MODULE_LICENSE("GPL");
This page took 0.044369 seconds and 4 git commands to generate.