151749035a19d9fe0d734ff614f1d5e72bf54200
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / tinydrm / st7735r.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DRM driver for Sitronix ST7735R panels
4  *
5  * Copyright 2017 David Lechner <david@lechnology.com>
6  */
7
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/dma-buf.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/property.h>
14 #include <linux/spi/spi.h>
15 #include <video/mipi_display.h>
16
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/tinydrm/mipi-dbi.h>
23
24 #define ST7735R_FRMCTR1         0xb1
25 #define ST7735R_FRMCTR2         0xb2
26 #define ST7735R_FRMCTR3         0xb3
27 #define ST7735R_INVCTR          0xb4
28 #define ST7735R_PWCTR1          0xc0
29 #define ST7735R_PWCTR2          0xc1
30 #define ST7735R_PWCTR3          0xc2
31 #define ST7735R_PWCTR4          0xc3
32 #define ST7735R_PWCTR5          0xc4
33 #define ST7735R_VMCTR1          0xc5
34 #define ST7735R_GAMCTRP1        0xe0
35 #define ST7735R_GAMCTRN1        0xe1
36
37 #define ST7735R_MY      BIT(7)
38 #define ST7735R_MX      BIT(6)
39 #define ST7735R_MV      BIT(5)
40
41 static void jd_t18003_t01_pipe_enable(struct drm_simple_display_pipe *pipe,
42                                       struct drm_crtc_state *crtc_state,
43                                       struct drm_plane_state *plane_state)
44 {
45         struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
46         int ret, idx;
47         u8 addr_mode;
48
49         if (!drm_dev_enter(pipe->crtc.dev, &idx))
50                 return;
51
52         DRM_DEBUG_KMS("\n");
53
54         ret = mipi_dbi_poweron_reset(mipi);
55         if (ret)
56                 goto out_exit;
57
58         msleep(150);
59
60         mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
61         msleep(500);
62
63         mipi_dbi_command(mipi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d);
64         mipi_dbi_command(mipi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d);
65         mipi_dbi_command(mipi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c,
66                          0x2d);
67         mipi_dbi_command(mipi, ST7735R_INVCTR, 0x07);
68         mipi_dbi_command(mipi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84);
69         mipi_dbi_command(mipi, ST7735R_PWCTR2, 0xc5);
70         mipi_dbi_command(mipi, ST7735R_PWCTR3, 0x0a, 0x00);
71         mipi_dbi_command(mipi, ST7735R_PWCTR4, 0x8a, 0x2a);
72         mipi_dbi_command(mipi, ST7735R_PWCTR5, 0x8a, 0xee);
73         mipi_dbi_command(mipi, ST7735R_VMCTR1, 0x0e);
74         mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
75         switch (mipi->rotation) {
76         default:
77                 addr_mode = ST7735R_MX | ST7735R_MY;
78                 break;
79         case 90:
80                 addr_mode = ST7735R_MX | ST7735R_MV;
81                 break;
82         case 180:
83                 addr_mode = 0;
84                 break;
85         case 270:
86                 addr_mode = ST7735R_MY | ST7735R_MV;
87                 break;
88         }
89         mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
90         mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT,
91                          MIPI_DCS_PIXEL_FMT_16BIT);
92         mipi_dbi_command(mipi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37,
93                          0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01,
94                          0x03, 0x10);
95         mipi_dbi_command(mipi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e,
96                          0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00,
97                          0x02, 0x10);
98         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
99
100         msleep(100);
101
102         mipi_dbi_command(mipi, MIPI_DCS_ENTER_NORMAL_MODE);
103
104         msleep(20);
105
106         mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
107 out_exit:
108         drm_dev_exit(idx);
109 }
110
111 static const struct drm_simple_display_pipe_funcs jd_t18003_t01_pipe_funcs = {
112         .enable         = jd_t18003_t01_pipe_enable,
113         .disable        = mipi_dbi_pipe_disable,
114         .update         = mipi_dbi_pipe_update,
115         .prepare_fb     = drm_gem_fb_simple_display_pipe_prepare_fb,
116 };
117
118 static const struct drm_display_mode jd_t18003_t01_mode = {
119         DRM_SIMPLE_MODE(128, 160, 28, 35),
120 };
121
122 DEFINE_DRM_GEM_CMA_FOPS(st7735r_fops);
123
124 static struct drm_driver st7735r_driver = {
125         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
126         .fops                   = &st7735r_fops,
127         .release                = mipi_dbi_release,
128         DRM_GEM_CMA_VMAP_DRIVER_OPS,
129         .debugfs_init           = mipi_dbi_debugfs_init,
130         .name                   = "st7735r",
131         .desc                   = "Sitronix ST7735R",
132         .date                   = "20171128",
133         .major                  = 1,
134         .minor                  = 0,
135 };
136
137 static const struct of_device_id st7735r_of_match[] = {
138         { .compatible = "jianda,jd-t18003-t01" },
139         { },
140 };
141 MODULE_DEVICE_TABLE(of, st7735r_of_match);
142
143 static const struct spi_device_id st7735r_id[] = {
144         { "jd-t18003-t01", 0 },
145         { },
146 };
147 MODULE_DEVICE_TABLE(spi, st7735r_id);
148
149 static int st7735r_probe(struct spi_device *spi)
150 {
151         struct device *dev = &spi->dev;
152         struct drm_device *drm;
153         struct mipi_dbi *mipi;
154         struct gpio_desc *dc;
155         u32 rotation = 0;
156         int ret;
157
158         mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
159         if (!mipi)
160                 return -ENOMEM;
161
162         drm = &mipi->drm;
163         ret = devm_drm_dev_init(dev, drm, &st7735r_driver);
164         if (ret) {
165                 kfree(mipi);
166                 return ret;
167         }
168
169         drm_mode_config_init(drm);
170
171         mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
172         if (IS_ERR(mipi->reset)) {
173                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
174                 return PTR_ERR(mipi->reset);
175         }
176
177         dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
178         if (IS_ERR(dc)) {
179                 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
180                 return PTR_ERR(dc);
181         }
182
183         mipi->backlight = devm_of_find_backlight(dev);
184         if (IS_ERR(mipi->backlight))
185                 return PTR_ERR(mipi->backlight);
186
187         device_property_read_u32(dev, "rotation", &rotation);
188
189         ret = mipi_dbi_spi_init(spi, mipi, dc);
190         if (ret)
191                 return ret;
192
193         /* Cannot read from Adafruit 1.8" display via SPI */
194         mipi->read_commands = NULL;
195
196         ret = mipi_dbi_init(mipi, &jd_t18003_t01_pipe_funcs, &jd_t18003_t01_mode, rotation);
197         if (ret)
198                 return ret;
199
200         drm_mode_config_reset(drm);
201
202         ret = drm_dev_register(drm, 0);
203         if (ret)
204                 return ret;
205
206         spi_set_drvdata(spi, drm);
207
208         drm_fbdev_generic_setup(drm, 0);
209
210         return 0;
211 }
212
213 static int st7735r_remove(struct spi_device *spi)
214 {
215         struct drm_device *drm = spi_get_drvdata(spi);
216
217         drm_dev_unplug(drm);
218         drm_atomic_helper_shutdown(drm);
219
220         return 0;
221 }
222
223 static void st7735r_shutdown(struct spi_device *spi)
224 {
225         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
226 }
227
228 static struct spi_driver st7735r_spi_driver = {
229         .driver = {
230                 .name = "st7735r",
231                 .owner = THIS_MODULE,
232                 .of_match_table = st7735r_of_match,
233         },
234         .id_table = st7735r_id,
235         .probe = st7735r_probe,
236         .remove = st7735r_remove,
237         .shutdown = st7735r_shutdown,
238 };
239 module_spi_driver(st7735r_spi_driver);
240
241 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver");
242 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
243 MODULE_LICENSE("GPL");