bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / gpu / drm / tinydrm / mi0283qt.c
blob674d407640bee55305775222cf52a3d319955af8
1 /*
2 * DRM driver for Multi-Inno MI0283QT panels
4 * Copyright 2016 Noralf Trønnes
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.
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_modeset_helper.h>
14 #include <drm/tinydrm/ili9341.h>
15 #include <drm/tinydrm/mipi-dbi.h>
16 #include <drm/tinydrm/tinydrm-helpers.h>
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
20 #include <linux/property.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/spi/spi.h>
23 #include <video/mipi_display.h>
25 static int mi0283qt_init(struct mipi_dbi *mipi)
27 struct tinydrm_device *tdev = &mipi->tinydrm;
28 struct device *dev = tdev->drm->dev;
29 u8 addr_mode;
30 int ret;
32 DRM_DEBUG_KMS("\n");
34 ret = regulator_enable(mipi->regulator);
35 if (ret) {
36 DRM_DEV_ERROR(dev, "Failed to enable regulator %d\n", ret);
37 return ret;
40 /* Avoid flicker by skipping setup if the bootloader has done it */
41 if (mipi_dbi_display_is_on(mipi))
42 return 0;
44 mipi_dbi_hw_reset(mipi);
45 ret = mipi_dbi_command(mipi, MIPI_DCS_SOFT_RESET);
46 if (ret) {
47 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
48 regulator_disable(mipi->regulator);
49 return ret;
52 msleep(20);
54 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
56 mipi_dbi_command(mipi, ILI9341_PWCTRLB, 0x00, 0x83, 0x30);
57 mipi_dbi_command(mipi, ILI9341_PWRSEQ, 0x64, 0x03, 0x12, 0x81);
58 mipi_dbi_command(mipi, ILI9341_DTCTRLA, 0x85, 0x01, 0x79);
59 mipi_dbi_command(mipi, ILI9341_PWCTRLA, 0x39, 0x2c, 0x00, 0x34, 0x02);
60 mipi_dbi_command(mipi, ILI9341_PUMPCTRL, 0x20);
61 mipi_dbi_command(mipi, ILI9341_DTCTRLB, 0x00, 0x00);
63 /* Power Control */
64 mipi_dbi_command(mipi, ILI9341_PWCTRL1, 0x26);
65 mipi_dbi_command(mipi, ILI9341_PWCTRL2, 0x11);
66 /* VCOM */
67 mipi_dbi_command(mipi, ILI9341_VMCTRL1, 0x35, 0x3e);
68 mipi_dbi_command(mipi, ILI9341_VMCTRL2, 0xbe);
70 /* Memory Access Control */
71 mipi_dbi_command(mipi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
73 switch (mipi->rotation) {
74 default:
75 addr_mode = ILI9341_MADCTL_MV | ILI9341_MADCTL_MY |
76 ILI9341_MADCTL_MX;
77 break;
78 case 90:
79 addr_mode = ILI9341_MADCTL_MY;
80 break;
81 case 180:
82 addr_mode = ILI9341_MADCTL_MV;
83 break;
84 case 270:
85 addr_mode = ILI9341_MADCTL_MX;
86 break;
88 addr_mode |= ILI9341_MADCTL_BGR;
89 mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
91 /* Frame Rate */
92 mipi_dbi_command(mipi, ILI9341_FRMCTR1, 0x00, 0x1b);
94 /* Gamma */
95 mipi_dbi_command(mipi, ILI9341_EN3GAM, 0x08);
96 mipi_dbi_command(mipi, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
97 mipi_dbi_command(mipi, ILI9341_PGAMCTRL,
98 0x1f, 0x1a, 0x18, 0x0a, 0x0f, 0x06, 0x45, 0x87,
99 0x32, 0x0a, 0x07, 0x02, 0x07, 0x05, 0x00);
100 mipi_dbi_command(mipi, ILI9341_NGAMCTRL,
101 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3a, 0x78,
102 0x4d, 0x05, 0x18, 0x0d, 0x38, 0x3a, 0x1f);
104 /* DDRAM */
105 mipi_dbi_command(mipi, ILI9341_ETMOD, 0x07);
107 /* Display */
108 mipi_dbi_command(mipi, ILI9341_DISCTRL, 0x0a, 0x82, 0x27, 0x00);
109 mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
110 msleep(100);
112 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
113 msleep(100);
115 return 0;
118 static void mi0283qt_fini(void *data)
120 struct mipi_dbi *mipi = data;
122 DRM_DEBUG_KMS("\n");
123 regulator_disable(mipi->regulator);
126 static const struct drm_simple_display_pipe_funcs mi0283qt_pipe_funcs = {
127 .enable = mipi_dbi_pipe_enable,
128 .disable = mipi_dbi_pipe_disable,
129 .update = tinydrm_display_pipe_update,
130 .prepare_fb = tinydrm_display_pipe_prepare_fb,
133 static const struct drm_display_mode mi0283qt_mode = {
134 TINYDRM_MODE(320, 240, 58, 43),
137 DEFINE_DRM_GEM_CMA_FOPS(mi0283qt_fops);
139 static struct drm_driver mi0283qt_driver = {
140 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
141 DRIVER_ATOMIC,
142 .fops = &mi0283qt_fops,
143 TINYDRM_GEM_DRIVER_OPS,
144 .lastclose = drm_fb_helper_lastclose,
145 .debugfs_init = mipi_dbi_debugfs_init,
146 .name = "mi0283qt",
147 .desc = "Multi-Inno MI0283QT",
148 .date = "20160614",
149 .major = 1,
150 .minor = 0,
153 static const struct of_device_id mi0283qt_of_match[] = {
154 { .compatible = "multi-inno,mi0283qt" },
157 MODULE_DEVICE_TABLE(of, mi0283qt_of_match);
159 static const struct spi_device_id mi0283qt_id[] = {
160 { "mi0283qt", 0 },
161 { },
163 MODULE_DEVICE_TABLE(spi, mi0283qt_id);
165 static int mi0283qt_probe(struct spi_device *spi)
167 struct device *dev = &spi->dev;
168 struct mipi_dbi *mipi;
169 struct gpio_desc *dc;
170 u32 rotation = 0;
171 int ret;
173 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
174 if (!mipi)
175 return -ENOMEM;
177 mipi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
178 if (IS_ERR(mipi->reset)) {
179 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
180 return PTR_ERR(mipi->reset);
183 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
184 if (IS_ERR(dc)) {
185 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
186 return PTR_ERR(dc);
189 mipi->regulator = devm_regulator_get(dev, "power");
190 if (IS_ERR(mipi->regulator))
191 return PTR_ERR(mipi->regulator);
193 mipi->backlight = tinydrm_of_find_backlight(dev);
194 if (IS_ERR(mipi->backlight))
195 return PTR_ERR(mipi->backlight);
197 device_property_read_u32(dev, "rotation", &rotation);
199 ret = mipi_dbi_spi_init(spi, mipi, dc);
200 if (ret)
201 return ret;
203 ret = mipi_dbi_init(&spi->dev, mipi, &mi0283qt_pipe_funcs,
204 &mi0283qt_driver, &mi0283qt_mode, rotation);
205 if (ret)
206 return ret;
208 ret = mi0283qt_init(mipi);
209 if (ret)
210 return ret;
212 /* use devres to fini after drm unregister (drv->remove is before) */
213 ret = devm_add_action(dev, mi0283qt_fini, mipi);
214 if (ret) {
215 mi0283qt_fini(mipi);
216 return ret;
219 spi_set_drvdata(spi, mipi);
221 return devm_tinydrm_register(&mipi->tinydrm);
224 static void mi0283qt_shutdown(struct spi_device *spi)
226 struct mipi_dbi *mipi = spi_get_drvdata(spi);
228 tinydrm_shutdown(&mipi->tinydrm);
231 static int __maybe_unused mi0283qt_pm_suspend(struct device *dev)
233 struct mipi_dbi *mipi = dev_get_drvdata(dev);
234 int ret;
236 ret = drm_mode_config_helper_suspend(mipi->tinydrm.drm);
237 if (ret)
238 return ret;
240 mi0283qt_fini(mipi);
242 return 0;
245 static int __maybe_unused mi0283qt_pm_resume(struct device *dev)
247 struct mipi_dbi *mipi = dev_get_drvdata(dev);
248 int ret;
250 ret = mi0283qt_init(mipi);
251 if (ret)
252 return ret;
254 drm_mode_config_helper_resume(mipi->tinydrm.drm);
256 return 0;
259 static const struct dev_pm_ops mi0283qt_pm_ops = {
260 SET_SYSTEM_SLEEP_PM_OPS(mi0283qt_pm_suspend, mi0283qt_pm_resume)
263 static struct spi_driver mi0283qt_spi_driver = {
264 .driver = {
265 .name = "mi0283qt",
266 .owner = THIS_MODULE,
267 .of_match_table = mi0283qt_of_match,
268 .pm = &mi0283qt_pm_ops,
270 .id_table = mi0283qt_id,
271 .probe = mi0283qt_probe,
272 .shutdown = mi0283qt_shutdown,
274 module_spi_driver(mi0283qt_spi_driver);
276 MODULE_DESCRIPTION("Multi-Inno MI0283QT DRM driver");
277 MODULE_AUTHOR("Noralf Trønnes");
278 MODULE_LICENSE("GPL");