GNU Linux-libre 4.14.290-gnu1
[releases.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 <drm/tinydrm/ili9341.h>
13 #include <drm/tinydrm/mipi-dbi.h>
14 #include <drm/tinydrm/tinydrm-helpers.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/module.h>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spi/spi.h>
21 #include <video/mipi_display.h>
22
23 static int mi0283qt_init(struct mipi_dbi *mipi)
24 {
25         struct tinydrm_device *tdev = &mipi->tinydrm;
26         struct device *dev = tdev->drm->dev;
27         u8 addr_mode;
28         int ret;
29
30         DRM_DEBUG_KMS("\n");
31
32         ret = regulator_enable(mipi->regulator);
33         if (ret) {
34                 dev_err(dev, "Failed to enable regulator %d\n", ret);
35                 return ret;
36         }
37
38         /* Avoid flicker by skipping setup if the bootloader has done it */
39         if (mipi_dbi_display_is_on(mipi))
40                 return 0;
41
42         mipi_dbi_hw_reset(mipi);
43         ret = mipi_dbi_command(mipi, MIPI_DCS_SOFT_RESET);
44         if (ret) {
45                 dev_err(dev, "Error sending command %d\n", ret);
46                 regulator_disable(mipi->regulator);
47                 return ret;
48         }
49
50         msleep(20);
51
52         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
53
54         mipi_dbi_command(mipi, ILI9341_PWCTRLB, 0x00, 0x83, 0x30);
55         mipi_dbi_command(mipi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
56         mipi_dbi_command(mipi, ILI9341_DTCTRLA, 0x85, 0x01, 0x79);
57         mipi_dbi_command(mipi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
58         mipi_dbi_command(mipi, ILI9341_PUMPCTRL, 0x20);
59         mipi_dbi_command(mipi, ILI9341_DTCTRLB, 0x00, 0x00);
60
61         /* Power Control */
62         mipi_dbi_command(mipi, ILI9341_PWCTRL1, 0x26);
63         mipi_dbi_command(mipi, ILI9341_PWCTRL2, 0x11);
64         /* VCOM */
65         mipi_dbi_command(mipi, ILI9341_VMCTRL1, 0x35, 0x3e);
66         mipi_dbi_command(mipi, ILI9341_VMCTRL2, 0xbe);
67
68         /* Memory Access Control */
69         mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
70
71         switch (mipi->rotation) {
72         default:
73                 addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
74                             ILI9341_MADCTL_MX;
75                 break;
76         case 90:
77                 addr_mode = ILI9341_MADCTL_MY;
78                 break;
79         case 180:
80                 addr_mode = ILI9341_MADCTL_MV;
81                 break;
82         case 270:
83                 addr_mode = ILI9341_MADCTL_MX;
84                 break;
85         }
86         addr_mode |= ILI9341_MADCTL_BGR;
87         mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
88
89         /* Frame Rate */
90         mipi_dbi_command(mipi, ILI9341_FRMCTR1, 0x00, 0x1b);
91
92         /* Gamma */
93         mipi_dbi_command(mipi, ILI9341_EN3GAM, 0x08);
94         mipi_dbi_command(mipi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
95         mipi_dbi_command(mipi, ILI9341_PGAMCTRL,
96                        0x1f, 0x1a, 0x18, 0x0a, 0x0f, 0x06, 0x45, 0x87,
97                        0x32, 0x0a, 0x07, 0x02, 0x07, 0x05, 0x00);
98         mipi_dbi_command(mipi, ILI9341_NGAMCTRL,
99                        0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3a, 0x78,
100                        0x4d, 0x05, 0x18, 0x0d, 0x38, 0x3a, 0x1f);
101
102         /* DDRAM */
103         mipi_dbi_command(mipi, ILI9341_ETMOD, 0x07);
104
105         /* Display */
106         mipi_dbi_command(mipi, ILI9341_DISCTRL, 0x0a, 0x82, 0x27, 0x00);
107         mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
108         msleep(100);
109
110         mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
111         msleep(100);
112
113         return 0;
114 }
115
116 static void mi0283qt_fini(void *data)
117 {
118         struct mipi_dbi *mipi = data;
119
120         DRM_DEBUG_KMS("\n");
121         regulator_disable(mipi->regulator);
122 }
123
124 static const struct drm_simple_display_pipe_funcs mi0283qt_pipe_funcs = {
125         .enable = mipi_dbi_pipe_enable,
126         .disable = mipi_dbi_pipe_disable,
127         .update = tinydrm_display_pipe_update,
128         .prepare_fb = tinydrm_display_pipe_prepare_fb,
129 };
130
131 static const struct drm_display_mode mi0283qt_mode = {
132         TINYDRM_MODE(320, 240, 58, 43),
133 };
134
135 DEFINE_DRM_GEM_CMA_FOPS(mi0283qt_fops);
136
137 static struct drm_driver mi0283qt_driver = {
138         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
139                                   DRIVER_ATOMIC,
140         .fops                   = &mi0283qt_fops,
141         TINYDRM_GEM_DRIVER_OPS,
142         .lastclose              = tinydrm_lastclose,
143         .debugfs_init           = mipi_dbi_debugfs_init,
144         .name                   = "mi0283qt",
145         .desc                   = "Multi-Inno MI0283QT",
146         .date                   = "20160614",
147         .major                  = 1,
148         .minor                  = 0,
149 };
150
151 static const struct of_device_id mi0283qt_of_match[] = {
152         { .compatible = "multi-inno,mi0283qt" },
153         {},
154 };
155 MODULE_DEVICE_TABLE(of, mi0283qt_of_match);
156
157 static const struct spi_device_id mi0283qt_id[] = {
158         { "mi0283qt", 0 },
159         { },
160 };
161 MODULE_DEVICE_TABLE(spi, mi0283qt_id);
162
163 static int mi0283qt_probe(struct spi_device *spi)
164 {
165         struct device *dev = &spi->dev;
166         struct tinydrm_device *tdev;
167         struct mipi_dbi *mipi;
168         struct gpio_desc *dc;
169         u32 rotation = 0;
170         int ret;
171
172         mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
173         if (!mipi)
174                 return -ENOMEM;
175
176         mipi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
177         if (IS_ERR(mipi->reset)) {
178                 dev_err(dev, "Failed to get gpio 'reset'\n");
179                 return PTR_ERR(mipi->reset);
180         }
181
182         dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
183         if (IS_ERR(dc)) {
184                 dev_err(dev, "Failed to get gpio 'dc'\n");
185                 return PTR_ERR(dc);
186         }
187
188         mipi->regulator = devm_regulator_get(dev, "power");
189         if (IS_ERR(mipi->regulator))
190                 return PTR_ERR(mipi->regulator);
191
192         mipi->backlight = tinydrm_of_find_backlight(dev);
193         if (IS_ERR(mipi->backlight))
194                 return PTR_ERR(mipi->backlight);
195
196         device_property_read_u32(dev, "rotation", &rotation);
197
198         ret = mipi_dbi_spi_init(spi, mipi, dc);
199         if (ret)
200                 return ret;
201
202         ret = mipi_dbi_init(&spi->dev, mipi, &mi0283qt_pipe_funcs,
203                             &mi0283qt_driver, &mi0283qt_mode, rotation);
204         if (ret)
205                 return ret;
206
207         ret = mi0283qt_init(mipi);
208         if (ret)
209                 return ret;
210
211         /* use devres to fini after drm unregister (drv->remove is before) */
212         ret = devm_add_action(dev, mi0283qt_fini, mipi);
213         if (ret) {
214                 mi0283qt_fini(mipi);
215                 return ret;
216         }
217
218         tdev = &mipi->tinydrm;
219
220         ret = devm_tinydrm_register(tdev);
221         if (ret)
222                 return ret;
223
224         spi_set_drvdata(spi, mipi);
225
226         DRM_DEBUG_DRIVER("Initialized %s:%s @%uMHz on minor %d\n",
227                          tdev->drm->driver->name, dev_name(dev),
228                          spi->max_speed_hz / 1000000,
229                          tdev->drm->primary->index);
230
231         return 0;
232 }
233
234 static void mi0283qt_shutdown(struct spi_device *spi)
235 {
236         struct mipi_dbi *mipi = spi_get_drvdata(spi);
237
238         tinydrm_shutdown(&mipi->tinydrm);
239 }
240
241 static int __maybe_unused mi0283qt_pm_suspend(struct device *dev)
242 {
243         struct mipi_dbi *mipi = dev_get_drvdata(dev);
244         int ret;
245
246         ret = tinydrm_suspend(&mipi->tinydrm);
247         if (ret)
248                 return ret;
249
250         mi0283qt_fini(mipi);
251
252         return 0;
253 }
254
255 static int __maybe_unused mi0283qt_pm_resume(struct device *dev)
256 {
257         struct mipi_dbi *mipi = dev_get_drvdata(dev);
258         int ret;
259
260         ret = mi0283qt_init(mipi);
261         if (ret)
262                 return ret;
263
264         return tinydrm_resume(&mipi->tinydrm);
265 }
266
267 static const struct dev_pm_ops mi0283qt_pm_ops = {
268         SET_SYSTEM_SLEEP_PM_OPS(mi0283qt_pm_suspend, mi0283qt_pm_resume)
269 };
270
271 static struct spi_driver mi0283qt_spi_driver = {
272         .driver = {
273                 .name = "mi0283qt",
274                 .owner = THIS_MODULE,
275                 .of_match_table = mi0283qt_of_match,
276                 .pm = &mi0283qt_pm_ops,
277         },
278         .id_table = mi0283qt_id,
279         .probe = mi0283qt_probe,
280         .shutdown = mi0283qt_shutdown,
281 };
282 module_spi_driver(mi0283qt_spi_driver);
283
284 MODULE_DESCRIPTION("Multi-Inno MI0283QT DRM driver");
285 MODULE_AUTHOR("Noralf Trønnes");
286 MODULE_LICENSE("GPL");