]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/tinydrm/mi0283qt.c
drm/tinydrm: Use drm_dev_enter/exit()
[linux.git] / drivers / gpu / drm / tinydrm / mi0283qt.c
1 /*
2  * DRM driver for Multi-Inno MI0283QT panels
3  *
4  * Copyright 2016 Noralf Trønnes
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/backlight.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
19
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/drm_modeset_helper.h>
26 #include <drm/tinydrm/mipi-dbi.h>
27 #include <drm/tinydrm/tinydrm-helpers.h>
28 #include <video/mipi_display.h>
29
30 #define ILI9341_FRMCTR1         0xb1
31 #define ILI9341_DISCTRL         0xb6
32 #define ILI9341_ETMOD           0xb7
33
34 #define ILI9341_PWCTRL1         0xc0
35 #define ILI9341_PWCTRL2         0xc1
36 #define ILI9341_VMCTRL1         0xc5
37 #define ILI9341_VMCTRL2         0xc7
38 #define ILI9341_PWCTRLA         0xcb
39 #define ILI9341_PWCTRLB         0xcf
40
41 #define ILI9341_PGAMCTRL        0xe0
42 #define ILI9341_NGAMCTRL        0xe1
43 #define ILI9341_DTCTRLA         0xe8
44 #define ILI9341_DTCTRLB         0xea
45 #define ILI9341_PWRSEQ          0xed
46
47 #define ILI9341_EN3GAM          0xf2
48 #define ILI9341_PUMPCTRL        0xf7
49
50 #define ILI9341_MADCTL_BGR      BIT(3)
51 #define ILI9341_MADCTL_MV       BIT(5)
52 #define ILI9341_MADCTL_MX       BIT(6)
53 #define ILI9341_MADCTL_MY       BIT(7)
54
55 static void mi0283qt_enable(struct drm_simple_display_pipe *pipe,
56                             struct drm_crtc_state *crtc_state,
57                             struct drm_plane_state *plane_state)
58 {
59         struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
60         u8 addr_mode;
61         int ret, idx;
62
63         if (!drm_dev_enter(pipe->crtc.dev, &idx))
64                 return;
65
66         DRM_DEBUG_KMS("\n");
67
68         ret = mipi_dbi_poweron_conditional_reset(mipi);
69         if (ret < 0)
70                 goto out_exit;
71         if (ret == 1)
72                 goto out_enable;
73
74         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
75
76         mipi_dbi_command(mipi, ILI9341_PWCTRLB, 0x00, 0x83, 0x30);
77         mipi_dbi_command(mipi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
78         mipi_dbi_command(mipi, ILI9341_DTCTRLA, 0x85, 0x01, 0x79);
79         mipi_dbi_command(mipi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
80         mipi_dbi_command(mipi, ILI9341_PUMPCTRL, 0x20);
81         mipi_dbi_command(mipi, ILI9341_DTCTRLB, 0x00, 0x00);
82
83         /* Power Control */
84         mipi_dbi_command(mipi, ILI9341_PWCTRL1, 0x26);
85         mipi_dbi_command(mipi, ILI9341_PWCTRL2, 0x11);
86         /* VCOM */
87         mipi_dbi_command(mipi, ILI9341_VMCTRL1, 0x35, 0x3e);
88         mipi_dbi_command(mipi, ILI9341_VMCTRL2, 0xbe);
89
90         /* Memory Access Control */
91         mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
92
93         /* Frame Rate */
94         mipi_dbi_command(mipi, ILI9341_FRMCTR1, 0x00, 0x1b);
95
96         /* Gamma */
97         mipi_dbi_command(mipi, ILI9341_EN3GAM, 0x08);
98         mipi_dbi_command(mipi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
99         mipi_dbi_command(mipi, ILI9341_PGAMCTRL,
100                        0x1f, 0x1a, 0x18, 0x0a, 0x0f, 0x06, 0x45, 0x87,
101                        0x32, 0x0a, 0x07, 0x02, 0x07, 0x05, 0x00);
102         mipi_dbi_command(mipi, ILI9341_NGAMCTRL,
103                        0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3a, 0x78,
104                        0x4d, 0x05, 0x18, 0x0d, 0x38, 0x3a, 0x1f);
105
106         /* DDRAM */
107         mipi_dbi_command(mipi, ILI9341_ETMOD, 0x07);
108
109         /* Display */
110         mipi_dbi_command(mipi, ILI9341_DISCTRL, 0x0a, 0x82, 0x27, 0x00);
111         mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
112         msleep(100);
113
114         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
115         msleep(100);
116
117 out_enable:
118         /* The PiTFT (ili9340) has a hardware reset circuit that
119          * resets only on power-on and not on each reboot through
120          * a gpio like the rpi-display does.
121          * As a result, we need to always apply the rotation value
122          * regardless of the display "on/off" state.
123          */
124         switch (mipi->rotation) {
125         default:
126                 addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
127                             ILI9341_MADCTL_MX;
128                 break;
129         case 90:
130                 addr_mode = ILI9341_MADCTL_MY;
131                 break;
132         case 180:
133                 addr_mode = ILI9341_MADCTL_MV;
134                 break;
135         case 270:
136                 addr_mode = ILI9341_MADCTL_MX;
137                 break;
138         }
139         addr_mode |= ILI9341_MADCTL_BGR;
140         mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
141         mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
142 out_exit:
143         drm_dev_exit(idx);
144 }
145
146 static const struct drm_simple_display_pipe_funcs mi0283qt_pipe_funcs = {
147         .enable = mi0283qt_enable,
148         .disable = mipi_dbi_pipe_disable,
149         .update = mipi_dbi_pipe_update,
150         .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
151 };
152
153 static const struct drm_display_mode mi0283qt_mode = {
154         DRM_SIMPLE_MODE(320, 240, 58, 43),
155 };
156
157 DEFINE_DRM_GEM_CMA_FOPS(mi0283qt_fops);
158
159 static struct drm_driver mi0283qt_driver = {
160         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
161                                   DRIVER_ATOMIC,
162         .fops                   = &mi0283qt_fops,
163         .release                = mipi_dbi_release,
164         DRM_GEM_CMA_VMAP_DRIVER_OPS,
165         .debugfs_init           = mipi_dbi_debugfs_init,
166         .name                   = "mi0283qt",
167         .desc                   = "Multi-Inno MI0283QT",
168         .date                   = "20160614",
169         .major                  = 1,
170         .minor                  = 0,
171 };
172
173 static const struct of_device_id mi0283qt_of_match[] = {
174         { .compatible = "multi-inno,mi0283qt" },
175         {},
176 };
177 MODULE_DEVICE_TABLE(of, mi0283qt_of_match);
178
179 static const struct spi_device_id mi0283qt_id[] = {
180         { "mi0283qt", 0 },
181         { },
182 };
183 MODULE_DEVICE_TABLE(spi, mi0283qt_id);
184
185 static int mi0283qt_probe(struct spi_device *spi)
186 {
187         struct device *dev = &spi->dev;
188         struct drm_device *drm;
189         struct mipi_dbi *mipi;
190         struct gpio_desc *dc;
191         u32 rotation = 0;
192         int ret;
193
194         mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
195         if (!mipi)
196                 return -ENOMEM;
197
198         drm = &mipi->drm;
199         ret = devm_drm_dev_init(dev, drm, &mi0283qt_driver);
200         if (ret) {
201                 kfree(mipi);
202                 return ret;
203         }
204
205         drm_mode_config_init(drm);
206
207         mipi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
208         if (IS_ERR(mipi->reset)) {
209                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
210                 return PTR_ERR(mipi->reset);
211         }
212
213         dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
214         if (IS_ERR(dc)) {
215                 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
216                 return PTR_ERR(dc);
217         }
218
219         mipi->regulator = devm_regulator_get(dev, "power");
220         if (IS_ERR(mipi->regulator))
221                 return PTR_ERR(mipi->regulator);
222
223         mipi->backlight = devm_of_find_backlight(dev);
224         if (IS_ERR(mipi->backlight))
225                 return PTR_ERR(mipi->backlight);
226
227         device_property_read_u32(dev, "rotation", &rotation);
228
229         ret = mipi_dbi_spi_init(spi, mipi, dc);
230         if (ret)
231                 return ret;
232
233         ret = mipi_dbi_init(mipi, &mi0283qt_pipe_funcs, &mi0283qt_mode, 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_fbdev_generic_setup(drm, 32);
246
247         return 0;
248 }
249
250 static int mi0283qt_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         return 0;
258 }
259
260 static void mi0283qt_shutdown(struct spi_device *spi)
261 {
262         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
263 }
264
265 static int __maybe_unused mi0283qt_pm_suspend(struct device *dev)
266 {
267         return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
268 }
269
270 static int __maybe_unused mi0283qt_pm_resume(struct device *dev)
271 {
272         drm_mode_config_helper_resume(dev_get_drvdata(dev));
273
274         return 0;
275 }
276
277 static const struct dev_pm_ops mi0283qt_pm_ops = {
278         SET_SYSTEM_SLEEP_PM_OPS(mi0283qt_pm_suspend, mi0283qt_pm_resume)
279 };
280
281 static struct spi_driver mi0283qt_spi_driver = {
282         .driver = {
283                 .name = "mi0283qt",
284                 .owner = THIS_MODULE,
285                 .of_match_table = mi0283qt_of_match,
286                 .pm = &mi0283qt_pm_ops,
287         },
288         .id_table = mi0283qt_id,
289         .probe = mi0283qt_probe,
290         .remove = mi0283qt_remove,
291         .shutdown = mi0283qt_shutdown,
292 };
293 module_spi_driver(mi0283qt_spi_driver);
294
295 MODULE_DESCRIPTION("Multi-Inno MI0283QT DRM driver");
296 MODULE_AUTHOR("Noralf Trønnes");
297 MODULE_LICENSE("GPL");