GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / gpu / drm / panel / panel-raspberrypi-touchscreen.c
1 /*
2  * Copyright © 2016-2017 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Portions of this file (derived from panel-simple.c) are:
9  *
10  * Copyright (C) 2013, NVIDIA Corporation.  All rights reserved.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sub license,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the
20  * next paragraph) shall be included in all copies or substantial portions
21  * of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  */
31
32 /**
33  * Raspberry Pi 7" touchscreen panel driver.
34  *
35  * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
36  * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
37  * controlling power management, the LCD PWM, and initial register
38  * setup of the Tohsiba.
39  *
40  * This driver controls the TC358762 and ATTINY88, presenting a DSI
41  * device with a drm_panel.
42  */
43
44 #include <linux/delay.h>
45 #include <linux/err.h>
46 #include <linux/fb.h>
47 #include <linux/gpio.h>
48 #include <linux/gpio/consumer.h>
49 #include <linux/i2c.h>
50 #include <linux/module.h>
51 #include <linux/of.h>
52 #include <linux/of_device.h>
53 #include <linux/of_graph.h>
54 #include <linux/pm.h>
55
56 #include <drm/drm_panel.h>
57 #include <drm/drmP.h>
58 #include <drm/drm_crtc.h>
59 #include <drm/drm_mipi_dsi.h>
60 #include <drm/drm_panel.h>
61
62 #define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
63
64 /* I2C registers of the Atmel microcontroller. */
65 enum REG_ADDR {
66         REG_ID = 0x80,
67         REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
68         REG_PORTB,
69         REG_PORTC,
70         REG_PORTD,
71         REG_POWERON,
72         REG_PWM,
73         REG_DDRA,
74         REG_DDRB,
75         REG_DDRC,
76         REG_DDRD,
77         REG_TEST,
78         REG_WR_ADDRL,
79         REG_WR_ADDRH,
80         REG_READH,
81         REG_READL,
82         REG_WRITEH,
83         REG_WRITEL,
84         REG_ID2,
85 };
86
87 /* DSI D-PHY Layer Registers */
88 #define D0W_DPHYCONTTX          0x0004
89 #define CLW_DPHYCONTRX          0x0020
90 #define D0W_DPHYCONTRX          0x0024
91 #define D1W_DPHYCONTRX          0x0028
92 #define COM_DPHYCONTRX          0x0038
93 #define CLW_CNTRL               0x0040
94 #define D0W_CNTRL               0x0044
95 #define D1W_CNTRL               0x0048
96 #define DFTMODE_CNTRL           0x0054
97
98 /* DSI PPI Layer Registers */
99 #define PPI_STARTPPI            0x0104
100 #define PPI_BUSYPPI             0x0108
101 #define PPI_LINEINITCNT         0x0110
102 #define PPI_LPTXTIMECNT         0x0114
103 #define PPI_CLS_ATMR            0x0140
104 #define PPI_D0S_ATMR            0x0144
105 #define PPI_D1S_ATMR            0x0148
106 #define PPI_D0S_CLRSIPOCOUNT    0x0164
107 #define PPI_D1S_CLRSIPOCOUNT    0x0168
108 #define CLS_PRE                 0x0180
109 #define D0S_PRE                 0x0184
110 #define D1S_PRE                 0x0188
111 #define CLS_PREP                0x01A0
112 #define D0S_PREP                0x01A4
113 #define D1S_PREP                0x01A8
114 #define CLS_ZERO                0x01C0
115 #define D0S_ZERO                0x01C4
116 #define D1S_ZERO                0x01C8
117 #define PPI_CLRFLG              0x01E0
118 #define PPI_CLRSIPO             0x01E4
119 #define HSTIMEOUT               0x01F0
120 #define HSTIMEOUTENABLE         0x01F4
121
122 /* DSI Protocol Layer Registers */
123 #define DSI_STARTDSI            0x0204
124 #define DSI_BUSYDSI             0x0208
125 #define DSI_LANEENABLE          0x0210
126 # define DSI_LANEENABLE_CLOCK           BIT(0)
127 # define DSI_LANEENABLE_D0              BIT(1)
128 # define DSI_LANEENABLE_D1              BIT(2)
129
130 #define DSI_LANESTATUS0         0x0214
131 #define DSI_LANESTATUS1         0x0218
132 #define DSI_INTSTATUS           0x0220
133 #define DSI_INTMASK             0x0224
134 #define DSI_INTCLR              0x0228
135 #define DSI_LPTXTO              0x0230
136 #define DSI_MODE                0x0260
137 #define DSI_PAYLOAD0            0x0268
138 #define DSI_PAYLOAD1            0x026C
139 #define DSI_SHORTPKTDAT         0x0270
140 #define DSI_SHORTPKTREQ         0x0274
141 #define DSI_BTASTA              0x0278
142 #define DSI_BTACLR              0x027C
143
144 /* DSI General Registers */
145 #define DSIERRCNT               0x0300
146 #define DSISIGMOD               0x0304
147
148 /* DSI Application Layer Registers */
149 #define APLCTRL                 0x0400
150 #define APLSTAT                 0x0404
151 #define APLERR                  0x0408
152 #define PWRMOD                  0x040C
153 #define RDPKTLN                 0x0410
154 #define PXLFMT                  0x0414
155 #define MEMWRCMD                0x0418
156
157 /* LCDC/DPI Host Registers */
158 #define LCDCTRL                 0x0420
159 #define HSR                     0x0424
160 #define HDISPR                  0x0428
161 #define VSR                     0x042C
162 #define VDISPR                  0x0430
163 #define VFUEN                   0x0434
164
165 /* DBI-B Host Registers */
166 #define DBIBCTRL                0x0440
167
168 /* SPI Master Registers */
169 #define SPICMR                  0x0450
170 #define SPITCR                  0x0454
171
172 /* System Controller Registers */
173 #define SYSSTAT                 0x0460
174 #define SYSCTRL                 0x0464
175 #define SYSPLL1                 0x0468
176 #define SYSPLL2                 0x046C
177 #define SYSPLL3                 0x0470
178 #define SYSPMCTRL               0x047C
179
180 /* GPIO Registers */
181 #define GPIOC                   0x0480
182 #define GPIOO                   0x0484
183 #define GPIOI                   0x0488
184
185 /* I2C Registers */
186 #define I2CCLKCTRL              0x0490
187
188 /* Chip/Rev Registers */
189 #define IDREG                   0x04A0
190
191 /* Debug Registers */
192 #define WCMDQUEUE               0x0500
193 #define RCMDQUEUE               0x0504
194
195 struct rpi_touchscreen {
196         struct drm_panel base;
197         struct mipi_dsi_device *dsi;
198         struct i2c_client *i2c;
199 };
200
201 static const struct drm_display_mode rpi_touchscreen_modes[] = {
202         {
203                 /* Modeline comes from the Raspberry Pi firmware, with HFP=1
204                  * plugged in and clock re-computed from that.
205                  */
206                 .clock = 25979400 / 1000,
207                 .hdisplay = 800,
208                 .hsync_start = 800 + 1,
209                 .hsync_end = 800 + 1 + 2,
210                 .htotal = 800 + 1 + 2 + 46,
211                 .vdisplay = 480,
212                 .vsync_start = 480 + 7,
213                 .vsync_end = 480 + 7 + 2,
214                 .vtotal = 480 + 7 + 2 + 21,
215                 .vrefresh = 60,
216         },
217 };
218
219 static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
220 {
221         return container_of(panel, struct rpi_touchscreen, base);
222 }
223
224 static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
225 {
226         return i2c_smbus_read_byte_data(ts->i2c, reg);
227 }
228
229 static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
230                                       u8 reg, u8 val)
231 {
232         int ret;
233
234         ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
235         if (ret)
236                 dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
237 }
238
239 static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
240 {
241         u8 msg[] = {
242                 reg,
243                 reg >> 8,
244                 val,
245                 val >> 8,
246                 val >> 16,
247                 val >> 24,
248         };
249
250         mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
251
252         return 0;
253 }
254
255 static int rpi_touchscreen_disable(struct drm_panel *panel)
256 {
257         struct rpi_touchscreen *ts = panel_to_ts(panel);
258
259         rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
260
261         rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
262         udelay(1);
263
264         return 0;
265 }
266
267 static int rpi_touchscreen_noop(struct drm_panel *panel)
268 {
269         return 0;
270 }
271
272 static int rpi_touchscreen_prepare(struct drm_panel *panel)
273 {
274         struct rpi_touchscreen *ts = panel_to_ts(panel);
275         int i;
276
277         rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
278         /* Wait for nPWRDWN to go low to indicate poweron is done. */
279         for (i = 0; i < 100; i++) {
280                 if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
281                         break;
282         }
283
284         rpi_touchscreen_write(ts, DSI_LANEENABLE,
285                               DSI_LANEENABLE_CLOCK |
286                               DSI_LANEENABLE_D0);
287         rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
288         rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
289         rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
290         rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
291         rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
292
293         rpi_touchscreen_write(ts, SPICMR, 0x00);
294         rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
295         rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
296         msleep(100);
297
298         rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
299         rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
300         msleep(100);
301
302         return 0;
303 }
304
305 static int rpi_touchscreen_enable(struct drm_panel *panel)
306 {
307         struct rpi_touchscreen *ts = panel_to_ts(panel);
308
309         /* Turn on the backlight. */
310         rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
311
312         /* Default to the same orientation as the closed source
313          * firmware used for the panel.  Runtime rotation
314          * configuration will be supported using VC4's plane
315          * orientation bits.
316          */
317         rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
318
319         return 0;
320 }
321
322 static int rpi_touchscreen_get_modes(struct drm_panel *panel)
323 {
324         struct drm_connector *connector = panel->connector;
325         struct drm_device *drm = panel->drm;
326         unsigned int i, num = 0;
327         static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
328
329         for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
330                 const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
331                 struct drm_display_mode *mode;
332
333                 mode = drm_mode_duplicate(drm, m);
334                 if (!mode) {
335                         dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
336                                 m->hdisplay, m->vdisplay, m->vrefresh);
337                         continue;
338                 }
339
340                 mode->type |= DRM_MODE_TYPE_DRIVER;
341
342                 if (i == 0)
343                         mode->type |= DRM_MODE_TYPE_PREFERRED;
344
345                 drm_mode_set_name(mode);
346
347                 drm_mode_probed_add(connector, mode);
348                 num++;
349         }
350
351         connector->display_info.bpc = 8;
352         connector->display_info.width_mm = 154;
353         connector->display_info.height_mm = 86;
354         drm_display_info_set_bus_formats(&connector->display_info,
355                                          &bus_format, 1);
356
357         return num;
358 }
359
360 static const struct drm_panel_funcs rpi_touchscreen_funcs = {
361         .disable = rpi_touchscreen_disable,
362         .unprepare = rpi_touchscreen_noop,
363         .prepare = rpi_touchscreen_prepare,
364         .enable = rpi_touchscreen_enable,
365         .get_modes = rpi_touchscreen_get_modes,
366 };
367
368 static int rpi_touchscreen_probe(struct i2c_client *i2c,
369                                  const struct i2c_device_id *id)
370 {
371         struct device *dev = &i2c->dev;
372         struct rpi_touchscreen *ts;
373         struct device_node *endpoint, *dsi_host_node;
374         struct mipi_dsi_host *host;
375         int ret, ver;
376         struct mipi_dsi_device_info info = {
377                 .type = RPI_DSI_DRIVER_NAME,
378                 .channel = 0,
379                 .node = NULL,
380         };
381
382         ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
383         if (!ts)
384                 return -ENOMEM;
385
386         i2c_set_clientdata(i2c, ts);
387
388         ts->i2c = i2c;
389
390         ver = rpi_touchscreen_i2c_read(ts, REG_ID);
391         if (ver < 0) {
392                 dev_err(dev, "Atmel I2C read failed: %d\n", ver);
393                 return -ENODEV;
394         }
395
396         switch (ver) {
397         case 0xde: /* ver 1 */
398         case 0xc3: /* ver 2 */
399                 break;
400         default:
401                 dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
402                 return -ENODEV;
403         }
404
405         /* Turn off at boot, so we can cleanly sequence powering on. */
406         rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
407
408         /* Look up the DSI host.  It needs to probe before we do. */
409         endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
410         if (!endpoint)
411                 return -ENODEV;
412
413         dsi_host_node = of_graph_get_remote_port_parent(endpoint);
414         if (!dsi_host_node)
415                 goto error;
416
417         host = of_find_mipi_dsi_host_by_node(dsi_host_node);
418         of_node_put(dsi_host_node);
419         if (!host) {
420                 of_node_put(endpoint);
421                 return -EPROBE_DEFER;
422         }
423
424         info.node = of_graph_get_remote_port(endpoint);
425         if (!info.node)
426                 goto error;
427
428         of_node_put(endpoint);
429
430         ts->dsi = mipi_dsi_device_register_full(host, &info);
431         if (IS_ERR(ts->dsi)) {
432                 dev_err(dev, "DSI device registration failed: %ld\n",
433                         PTR_ERR(ts->dsi));
434                 return PTR_ERR(ts->dsi);
435         }
436
437         drm_panel_init(&ts->base);
438         ts->base.dev = dev;
439         ts->base.funcs = &rpi_touchscreen_funcs;
440
441         /* This appears last, as it's what will unblock the DSI host
442          * driver's component bind function.
443          */
444         ret = drm_panel_add(&ts->base);
445         if (ret)
446                 return ret;
447
448         return 0;
449
450 error:
451         of_node_put(endpoint);
452         return -ENODEV;
453 }
454
455 static int rpi_touchscreen_remove(struct i2c_client *i2c)
456 {
457         struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
458
459         mipi_dsi_detach(ts->dsi);
460
461         drm_panel_remove(&ts->base);
462
463         mipi_dsi_device_unregister(ts->dsi);
464
465         return 0;
466 }
467
468 static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
469 {
470         int ret;
471
472         dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
473                            MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
474                            MIPI_DSI_MODE_LPM);
475         dsi->format = MIPI_DSI_FMT_RGB888;
476         dsi->lanes = 1;
477
478         ret = mipi_dsi_attach(dsi);
479
480         if (ret)
481                 dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
482
483         return ret;
484 }
485
486 static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
487         .driver.name = RPI_DSI_DRIVER_NAME,
488         .probe = rpi_touchscreen_dsi_probe,
489 };
490
491 static const struct of_device_id rpi_touchscreen_of_ids[] = {
492         { .compatible = "raspberrypi,7inch-touchscreen-panel" },
493         { } /* sentinel */
494 };
495 MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
496
497 static struct i2c_driver rpi_touchscreen_driver = {
498         .driver = {
499                 .name = "rpi_touchscreen",
500                 .of_match_table = rpi_touchscreen_of_ids,
501         },
502         .probe = rpi_touchscreen_probe,
503         .remove = rpi_touchscreen_remove,
504 };
505
506 static int __init rpi_touchscreen_init(void)
507 {
508         mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
509         return i2c_add_driver(&rpi_touchscreen_driver);
510 }
511 module_init(rpi_touchscreen_init);
512
513 static void __exit rpi_touchscreen_exit(void)
514 {
515         i2c_del_driver(&rpi_touchscreen_driver);
516         mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
517 }
518 module_exit(rpi_touchscreen_exit);
519
520 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
521 MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
522 MODULE_LICENSE("GPL v2");