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