drm/tiny: fix sparse warning: incorrect type in assignment (different base types)
[linux-2.6-microblaze.git] / drivers / gpu / drm / tiny / ili9486.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DRM driver for Ilitek ILI9486 panels
4  *
5  * Copyright 2020 Kamlesh Gurudasani <kamlesh.gurudasani@gmail.com>
6  */
7
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/spi/spi.h>
14
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/drm_mipi_dbi.h>
23 #include <drm/drm_modeset_helper.h>
24
25 #define ILI9486_ITFCTR1         0xb0
26 #define ILI9486_PWCTRL1         0xc2
27 #define ILI9486_VMCTRL1         0xc5
28 #define ILI9486_PGAMCTRL        0xe0
29 #define ILI9486_NGAMCTRL        0xe1
30 #define ILI9486_DGAMCTRL        0xe2
31 #define ILI9486_MADCTL_BGR      BIT(3)
32 #define ILI9486_MADCTL_MV       BIT(5)
33 #define ILI9486_MADCTL_MX       BIT(6)
34 #define ILI9486_MADCTL_MY       BIT(7)
35
36 /*
37  * The PiScreen/waveshare rpi-lcd-35 has a SPI to 16-bit parallel bus converter
38  * in front of the  display controller. This means that 8-bit values have to be
39  * transferred as 16-bit.
40  */
41 static int waveshare_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
42                              size_t num)
43 {
44         struct spi_device *spi = mipi->spi;
45         void *data = par;
46         u32 speed_hz;
47         int i, ret;
48         __be16 *buf;
49
50         buf = kmalloc(32 * sizeof(u16), GFP_KERNEL);
51         if (!buf)
52                 return -ENOMEM;
53
54         /*
55          * The displays are Raspberry Pi HATs and connected to the 8-bit only
56          * SPI controller, so 16-bit command and parameters need byte swapping
57          * before being transferred as 8-bit on the big endian SPI bus.
58          * Pixel data bytes have already been swapped before this function is
59          * called.
60          */
61         buf[0] = cpu_to_be16(*cmd);
62         gpiod_set_value_cansleep(mipi->dc, 0);
63         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 2);
64         ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, buf, 2);
65         if (ret || !num)
66                 goto free;
67
68         /* 8-bit configuration data, not 16-bit pixel data */
69         if (num <= 32) {
70                 for (i = 0; i < num; i++)
71                         buf[i] = cpu_to_be16(par[i]);
72                 num *= 2;
73                 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
74                 data = buf;
75         }
76
77         gpiod_set_value_cansleep(mipi->dc, 1);
78         ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, data, num);
79  free:
80         kfree(buf);
81
82         return ret;
83 }
84
85 static void waveshare_enable(struct drm_simple_display_pipe *pipe,
86                              struct drm_crtc_state *crtc_state,
87                              struct drm_plane_state *plane_state)
88 {
89         struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
90         struct mipi_dbi *dbi = &dbidev->dbi;
91         u8 addr_mode;
92         int ret, idx;
93
94         if (!drm_dev_enter(pipe->crtc.dev, &idx))
95                 return;
96
97         DRM_DEBUG_KMS("\n");
98
99         ret = mipi_dbi_poweron_conditional_reset(dbidev);
100         if (ret < 0)
101                 goto out_exit;
102         if (ret == 1)
103                 goto out_enable;
104
105         mipi_dbi_command(dbi, ILI9486_ITFCTR1);
106         mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
107         msleep(250);
108
109         mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
110
111         mipi_dbi_command(dbi, ILI9486_PWCTRL1, 0x44);
112
113         mipi_dbi_command(dbi, ILI9486_VMCTRL1, 0x00, 0x00, 0x00, 0x00);
114
115         mipi_dbi_command(dbi, ILI9486_PGAMCTRL,
116                          0x0F, 0x1F, 0x1C, 0x0C, 0x0F, 0x08, 0x48, 0x98,
117                          0x37, 0x0A, 0x13, 0x04, 0x11, 0x0D, 0x0);
118         mipi_dbi_command(dbi, ILI9486_NGAMCTRL,
119                          0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75,
120                          0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00);
121         mipi_dbi_command(dbi, ILI9486_DGAMCTRL,
122                          0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75,
123                          0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00);
124
125         mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
126         msleep(100);
127
128  out_enable:
129         switch (dbidev->rotation) {
130         case 90:
131                 addr_mode = ILI9486_MADCTL_MY;
132                 break;
133         case 180:
134                 addr_mode = ILI9486_MADCTL_MV;
135                 break;
136         case 270:
137                 addr_mode = ILI9486_MADCTL_MX;
138                 break;
139         default:
140                 addr_mode = ILI9486_MADCTL_MV | ILI9486_MADCTL_MY |
141                         ILI9486_MADCTL_MX;
142                 break;
143         }
144         addr_mode |= ILI9486_MADCTL_BGR;
145         mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
146         mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
147  out_exit:
148         drm_dev_exit(idx);
149 }
150
151 static const struct drm_simple_display_pipe_funcs waveshare_pipe_funcs = {
152         .enable = waveshare_enable,
153         .disable = mipi_dbi_pipe_disable,
154         .update = mipi_dbi_pipe_update,
155         .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
156 };
157
158 static const struct drm_display_mode waveshare_mode = {
159         DRM_SIMPLE_MODE(480, 320, 73, 49),
160 };
161
162 DEFINE_DRM_GEM_CMA_FOPS(ili9486_fops);
163
164 static struct drm_driver ili9486_driver = {
165         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
166         .fops                   = &ili9486_fops,
167         .release                = mipi_dbi_release,
168         DRM_GEM_CMA_VMAP_DRIVER_OPS,
169         .debugfs_init           = mipi_dbi_debugfs_init,
170         .name                   = "ili9486",
171         .desc                   = "Ilitek ILI9486",
172         .date                   = "20200118",
173         .major                  = 1,
174         .minor                  = 0,
175 };
176
177 static const struct of_device_id ili9486_of_match[] = {
178         { .compatible = "waveshare,rpi-lcd-35" },
179         { .compatible = "ozzmaker,piscreen" },
180         {},
181 };
182 MODULE_DEVICE_TABLE(of, ili9486_of_match);
183
184 static const struct spi_device_id ili9486_id[] = {
185         { "ili9486", 0 },
186         { }
187 };
188 MODULE_DEVICE_TABLE(spi, ili9486_id);
189
190 static int ili9486_probe(struct spi_device *spi)
191 {
192         struct device *dev = &spi->dev;
193         struct mipi_dbi_dev *dbidev;
194         struct drm_device *drm;
195         struct mipi_dbi *dbi;
196         struct gpio_desc *dc;
197         u32 rotation = 0;
198         int ret;
199
200         dbidev = kzalloc(sizeof(*dbidev), GFP_KERNEL);
201         if (!dbidev)
202                 return -ENOMEM;
203
204         dbi = &dbidev->dbi;
205         drm = &dbidev->drm;
206         ret = devm_drm_dev_init(dev, drm, &ili9486_driver);
207         if (ret) {
208                 kfree(dbidev);
209                 return ret;
210         }
211
212         drm_mode_config_init(drm);
213
214         dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
215         if (IS_ERR(dbi->reset)) {
216                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
217                 return PTR_ERR(dbi->reset);
218         }
219
220         dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
221         if (IS_ERR(dc)) {
222                 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
223                 return PTR_ERR(dc);
224         }
225
226         dbidev->backlight = devm_of_find_backlight(dev);
227         if (IS_ERR(dbidev->backlight))
228                 return PTR_ERR(dbidev->backlight);
229
230         device_property_read_u32(dev, "rotation", &rotation);
231
232         ret = mipi_dbi_spi_init(spi, dbi, dc);
233         if (ret)
234                 return ret;
235
236         dbi->command = waveshare_command;
237         dbi->read_commands = NULL;
238
239         ret = mipi_dbi_dev_init(dbidev, &waveshare_pipe_funcs,
240                                 &waveshare_mode, rotation);
241         if (ret)
242                 return ret;
243
244         drm_mode_config_reset(drm);
245
246         ret = drm_dev_register(drm, 0);
247         if (ret)
248                 return ret;
249
250         spi_set_drvdata(spi, drm);
251
252         drm_fbdev_generic_setup(drm, 0);
253
254         return 0;
255 }
256
257 static int ili9486_remove(struct spi_device *spi)
258 {
259         struct drm_device *drm = spi_get_drvdata(spi);
260
261         drm_dev_unplug(drm);
262         drm_atomic_helper_shutdown(drm);
263
264         return 0;
265 }
266
267 static void ili9486_shutdown(struct spi_device *spi)
268 {
269         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
270 }
271
272 static struct spi_driver ili9486_spi_driver = {
273         .driver = {
274                 .name = "ili9486",
275                 .of_match_table = ili9486_of_match,
276         },
277         .id_table = ili9486_id,
278         .probe = ili9486_probe,
279         .remove = ili9486_remove,
280         .shutdown = ili9486_shutdown,
281 };
282 module_spi_driver(ili9486_spi_driver);
283
284 MODULE_DESCRIPTION("Ilitek ILI9486 DRM driver");
285 MODULE_AUTHOR("Kamlesh Gurudasani <kamlesh.gurudasani@gmail.com>");
286 MODULE_LICENSE("GPL");