1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include <linux/backlight.h>
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/module.h>
7 #include <linux/property.h>
8 #include <linux/spi/spi.h>
9
10 #include <drm/clients/drm_client_setup.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_fbdev_dma.h>
14 #include <drm/drm_gem_atomic_helper.h>
15 #include <drm/drm_gem_dma_helper.h>
16 #include <drm/drm_mipi_dbi.h>
17 #include <drm/drm_modeset_helper.h>
18
19 #include <video/mipi_display.h>
20
21 #define ILI9163_FRMCTR1 0xb1
22
23 #define ILI9163_PWCTRL1 0xc0
24 #define ILI9163_PWCTRL2 0xc1
25 #define ILI9163_VMCTRL1 0xc5
26 #define ILI9163_VMCTRL2 0xc7
27 #define ILI9163_PWCTRLA 0xcb
28 #define ILI9163_PWCTRLB 0xcf
29
30 #define ILI9163_EN3GAM 0xf2
31
32 #define ILI9163_MADCTL_BGR BIT(3)
33 #define ILI9163_MADCTL_MV BIT(5)
34 #define ILI9163_MADCTL_MX BIT(6)
35 #define ILI9163_MADCTL_MY BIT(7)
36
yx240qv29_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)37 static void yx240qv29_enable(struct drm_simple_display_pipe *pipe,
38 struct drm_crtc_state *crtc_state,
39 struct drm_plane_state *plane_state)
40 {
41 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
42 struct mipi_dbi *dbi = &dbidev->dbi;
43 u8 addr_mode;
44 int ret, idx;
45
46 if (!drm_dev_enter(pipe->crtc.dev, &idx))
47 return;
48
49 DRM_DEBUG_KMS("\n");
50
51 ret = mipi_dbi_poweron_conditional_reset(dbidev);
52 if (ret < 0)
53 goto out_exit;
54 if (ret == 1)
55 goto out_enable;
56
57 /* Gamma */
58 mipi_dbi_command(dbi, MIPI_DCS_SET_GAMMA_CURVE, 0x04);
59 mipi_dbi_command(dbi, ILI9163_EN3GAM, 0x00);
60
61 /* Frame Rate */
62 mipi_dbi_command(dbi, ILI9163_FRMCTR1, 0x0a, 0x14);
63
64 /* Power Control */
65 mipi_dbi_command(dbi, ILI9163_PWCTRL1, 0x0a, 0x00);
66 mipi_dbi_command(dbi, ILI9163_PWCTRL2, 0x02);
67
68 /* VCOM */
69 mipi_dbi_command(dbi, ILI9163_VMCTRL1, 0x2f, 0x3e);
70 mipi_dbi_command(dbi, ILI9163_VMCTRL2, 0x40);
71
72 /* Memory Access Control */
73 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
74
75 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
76 msleep(100);
77
78 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
79 msleep(100);
80
81 out_enable:
82 switch (dbidev->rotation) {
83 default:
84 addr_mode = ILI9163_MADCTL_MX | ILI9163_MADCTL_MY;
85 break;
86 case 90:
87 addr_mode = ILI9163_MADCTL_MX | ILI9163_MADCTL_MV;
88 break;
89 case 180:
90 addr_mode = 0;
91 break;
92 case 270:
93 addr_mode = ILI9163_MADCTL_MY | ILI9163_MADCTL_MV;
94 break;
95 }
96 addr_mode |= ILI9163_MADCTL_BGR;
97 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
98 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
99 out_exit:
100 drm_dev_exit(idx);
101 }
102
103 static const struct drm_simple_display_pipe_funcs ili9163_pipe_funcs = {
104 DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(yx240qv29_enable),
105 };
106
107 static const struct drm_display_mode yx240qv29_mode = {
108 DRM_SIMPLE_MODE(128, 160, 28, 35),
109 };
110
111 DEFINE_DRM_GEM_DMA_FOPS(ili9163_fops);
112
113 static struct drm_driver ili9163_driver = {
114 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
115 .fops = &ili9163_fops,
116 DRM_GEM_DMA_DRIVER_OPS_VMAP,
117 DRM_FBDEV_DMA_DRIVER_OPS,
118 .debugfs_init = mipi_dbi_debugfs_init,
119 .name = "ili9163",
120 .desc = "Ilitek ILI9163",
121 .major = 1,
122 .minor = 0,
123 };
124
125 static const struct of_device_id ili9163_of_match[] = {
126 { .compatible = "newhaven,1.8-128160EF" },
127 { }
128 };
129 MODULE_DEVICE_TABLE(of, ili9163_of_match);
130
131 static const struct spi_device_id ili9163_id[] = {
132 { "nhd-1.8-128160EF", 0 },
133 { }
134 };
135 MODULE_DEVICE_TABLE(spi, ili9163_id);
136
ili9163_probe(struct spi_device * spi)137 static int ili9163_probe(struct spi_device *spi)
138 {
139 struct device *dev = &spi->dev;
140 struct mipi_dbi_dev *dbidev;
141 struct drm_device *drm;
142 struct mipi_dbi *dbi;
143 struct gpio_desc *dc;
144 u32 rotation = 0;
145 int ret;
146
147 dbidev = devm_drm_dev_alloc(dev, &ili9163_driver,
148 struct mipi_dbi_dev, drm);
149 if (IS_ERR(dbidev))
150 return PTR_ERR(dbidev);
151
152 dbi = &dbidev->dbi;
153 drm = &dbidev->drm;
154
155 spi_set_drvdata(spi, drm);
156
157 dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
158 if (IS_ERR(dbi->reset)) {
159 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
160 return PTR_ERR(dbi->reset);
161 }
162
163 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
164 if (IS_ERR(dc)) {
165 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
166 return PTR_ERR(dc);
167 }
168
169 dbidev->backlight = devm_of_find_backlight(dev);
170 if (IS_ERR(dbidev->backlight))
171 return PTR_ERR(dbidev->backlight);
172
173 device_property_read_u32(dev, "rotation", &rotation);
174
175 ret = mipi_dbi_spi_init(spi, dbi, dc);
176 if (ret)
177 return ret;
178
179 ret = mipi_dbi_dev_init(dbidev, &ili9163_pipe_funcs, &yx240qv29_mode, rotation);
180 if (ret)
181 return ret;
182
183 drm_mode_config_reset(drm);
184
185 ret = drm_dev_register(drm, 0);
186 if (ret)
187 return ret;
188
189 drm_client_setup(drm, NULL);
190
191 return 0;
192 }
193
ili9163_remove(struct spi_device * spi)194 static void ili9163_remove(struct spi_device *spi)
195 {
196 struct drm_device *drm = spi_get_drvdata(spi);
197
198 drm_dev_unplug(drm);
199 drm_atomic_helper_shutdown(drm);
200 }
201
ili9163_shutdown(struct spi_device * spi)202 static void ili9163_shutdown(struct spi_device *spi)
203 {
204 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
205 }
206
207 static struct spi_driver ili9163_spi_driver = {
208 .driver = {
209 .name = "ili9163",
210 .of_match_table = ili9163_of_match,
211 },
212 .id_table = ili9163_id,
213 .probe = ili9163_probe,
214 .remove = ili9163_remove,
215 .shutdown = ili9163_shutdown,
216 };
217 module_spi_driver(ili9163_spi_driver);
218
219 MODULE_DESCRIPTION("Ilitek ILI9163 DRM driver");
220 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
221 MODULE_LICENSE("GPL");
222