17c32750b657d8c2b7ac3a93aaf51a7009b4d44c
[AGL/meta-agl.git] / meta-agl-bsp / meta-raspberrypi / recipes-kernel / linux / linux-raspberrypi / dsi / 0004-drm-panel-Backport-4.15-support-for-the-Raspberry-Pi.patch
1 From 7f745c7e13b50911424b9bbb4181d4dc72ef155f Mon Sep 17 00:00:00 2001
2 From: Kevin Quigley <kevin@kquigley.co.uk>
3 Date: Mon, 24 Sep 2018 12:14:41 +0000
4 Subject: [PATCH 4/4] drm/panel: Backport 4.15 support for the Raspberry Pi 7"
5  Touchscreen
6
7 ---
8  .../drm/panel/panel-raspberrypi-touchscreen.c | 242 ++++++++----------
9  1 file changed, 111 insertions(+), 131 deletions(-)
10
11 diff --git a/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
12 index 128523d8d518..2c9c9722734f 100644
13 --- a/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
14 +++ b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
15 @@ -1,5 +1,5 @@
16  /*
17 - * Copyright © 2016 Broadcom
18 + * Copyright © 2016-2017 Broadcom
19   *
20   * This program is free software; you can redistribute it and/or modify
21   * it under the terms of the GNU General Public License version 2 as
22 @@ -30,14 +30,15 @@
23   */
24  
25  /**
26 - * DOC: Raspberry Pi 7" touchscreen panel driver.
27 + * Raspberry Pi 7" touchscreen panel driver.
28   *
29   * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
30   * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
31 - * controlling power management, the LCD PWM, and the touchscreen.
32 + * controlling power management, the LCD PWM, and initial register
33 + * setup of the Tohsiba.
34   *
35 - * This driver presents this device as a MIPI DSI panel to the DRM
36 - * driver, and should expose the touchscreen as a HID device.
37 + * This driver controls the TC358762 and ATTINY88, presenting a DSI
38 + * device with a drm_panel.
39   */
40  
41  #include <linux/delay.h>
42 @@ -58,10 +59,12 @@
43  #include <drm/drm_mipi_dsi.h>
44  #include <drm/drm_panel.h>
45  
46 +#define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
47 +
48  /* I2C registers of the Atmel microcontroller. */
49  enum REG_ADDR {
50         REG_ID = 0x80,
51 -       REG_PORTA, // BIT(2) for horizontal flip, BIT(3) for vertical flip
52 +       REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
53         REG_PORTB,
54         REG_PORTC,
55         REG_PORTD,
56 @@ -81,9 +84,6 @@ enum REG_ADDR {
57         REG_ID2,
58  };
59  
60 -/* We only turn the PWM on or off, without varying values. */
61 -#define RPI_TOUCHSCREEN_MAX_BRIGHTNESS 1
62 -
63  /* DSI D-PHY Layer Registers */
64  #define D0W_DPHYCONTTX         0x0004
65  #define CLW_DPHYCONTRX         0x0020
66 @@ -100,8 +100,6 @@ enum REG_ADDR {
67  #define PPI_BUSYPPI            0x0108
68  #define PPI_LINEINITCNT                0x0110
69  #define PPI_LPTXTIMECNT                0x0114
70 -//#define PPI_LANEENABLE               0x0134
71 -//#define PPI_TX_RX_TA         0x013C
72  #define PPI_CLS_ATMR           0x0140
73  #define PPI_D0S_ATMR           0x0144
74  #define PPI_D1S_ATMR           0x0148
75 @@ -197,43 +195,23 @@ enum REG_ADDR {
76  struct rpi_touchscreen {
77         struct drm_panel base;
78         struct mipi_dsi_device *dsi;
79 -       struct i2c_client *bridge_i2c;
80 -
81 -       /* Version of the firmware on the bridge chip */
82 -       int atmel_ver;
83 +       struct i2c_client *i2c;
84  };
85  
86  static const struct drm_display_mode rpi_touchscreen_modes[] = {
87         {
88 -               /* The DSI PLL can only integer divide from the 2Ghz
89 -                * PLLD, giving us few choices.  We pick a divide by 3
90 -                * as our DSI HS clock, giving us a pixel clock of
91 -                * that divided by 24 bits.  Pad out HFP to get our
92 -                * panel to refresh at 60Hz, even if that doesn't
93 -                * match the datasheet.
94 -                */
95 -#define PIXEL_CLOCK ((2000000000 / 3) / 24)
96 -#define VREFRESH    60
97 -#define VTOTAL      (480 + 7 + 2 + 21)
98 -#define HACT        800
99 -#define HSW         2
100 -#define HBP         46
101 -#define HFP         ((PIXEL_CLOCK / (VTOTAL * VREFRESH)) - (HACT + HSW + HBP))
102 -
103 -               /* Round up the pixel clock a bit (10khz), so that the
104 -                * "don't run things faster than the requested clock
105 -                * rate" rule of the clk driver doesn't reject the
106 -                * divide-by-3 mode due to rounding error.
107 +               /* Modeline comes from the Raspberry Pi firmware, with HFP=1
108 +                * plugged in and clock re-computed from that.
109                  */
110 -               .clock = PIXEL_CLOCK / 1000 + 10,
111 -               .hdisplay = HACT,
112 -               .hsync_start = HACT + HFP,
113 -               .hsync_end = HACT + HFP + HSW,
114 -               .htotal = HACT + HFP + HSW + HBP,
115 +               .clock = 25979400 / 1000,
116 +               .hdisplay = 800,
117 +               .hsync_start = 800 + 1,
118 +               .hsync_end = 800 + 1 + 2,
119 +               .htotal = 800 + 1 + 2 + 46,
120                 .vdisplay = 480,
121                 .vsync_start = 480 + 7,
122                 .vsync_end = 480 + 7 + 2,
123 -               .vtotal = VTOTAL,
124 +               .vtotal = 480 + 7 + 2 + 21,
125                 .vrefresh = 60,
126         },
127  };
128 @@ -245,7 +223,7 @@ static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
129  
130  static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
131  {
132 -       return i2c_smbus_read_byte_data(ts->bridge_i2c, reg);
133 +       return i2c_smbus_read_byte_data(ts->i2c, reg);
134  }
135  
136  static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
137 @@ -253,19 +231,13 @@ static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
138  {
139         int ret;
140  
141 -       ret = i2c_smbus_write_byte_data(ts->bridge_i2c, reg, val);
142 +       ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
143         if (ret)
144                 dev_err(&ts->dsi->dev, "I2C write failed: %d\n", ret);
145  }
146  
147  static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
148  {
149 -#if 0
150 -       /* The firmware uses LP DSI transactions like this to bring up
151 -        * the hardware, which should be faster than using I2C to then
152 -        * pass to the Toshiba.  However, I was unable to get it to
153 -        * work.
154 -        */
155         u8 msg[] = {
156                 reg,
157                 reg >> 8,
158 @@ -275,13 +247,7 @@ static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
159                 val >> 24,
160         };
161  
162 -       mipi_dsi_dcs_write_buffer(ts->dsi, msg, sizeof(msg));
163 -#else
164 -       rpi_touchscreen_i2c_write(ts, REG_WR_ADDRH, reg >> 8);
165 -       rpi_touchscreen_i2c_write(ts, REG_WR_ADDRL, reg);
166 -       rpi_touchscreen_i2c_write(ts, REG_WRITEH, val >> 8);
167 -       rpi_touchscreen_i2c_write(ts, REG_WRITEL, val);
168 -#endif
169 +       mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
170  
171         return 0;
172  }
173 @@ -317,8 +283,7 @@ static int rpi_touchscreen_enable(struct drm_panel *panel)
174  
175         rpi_touchscreen_write(ts, DSI_LANEENABLE,
176                               DSI_LANEENABLE_CLOCK |
177 -                             DSI_LANEENABLE_D0 |
178 -                             (ts->dsi->lanes > 1 ? DSI_LANEENABLE_D1 : 0));
179 +                             DSI_LANEENABLE_D0);
180         rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
181         rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
182         rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
183 @@ -352,6 +317,7 @@ static int rpi_touchscreen_get_modes(struct drm_panel *panel)
184         struct drm_connector *connector = panel->connector;
185         struct drm_device *drm = panel->drm;
186         unsigned int i, num = 0;
187 +       static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
188  
189         for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
190                 const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
191 @@ -378,6 +344,8 @@ static int rpi_touchscreen_get_modes(struct drm_panel *panel)
192         connector->display_info.bpc = 8;
193         connector->display_info.width_mm = 154;
194         connector->display_info.height_mm = 86;
195 +       drm_display_info_set_bus_formats(&connector->display_info,
196 +                                        &bus_format, 1);
197  
198         return num;
199  }
200 @@ -390,51 +358,27 @@ static const struct drm_panel_funcs rpi_touchscreen_funcs = {
201         .get_modes = rpi_touchscreen_get_modes,
202  };
203  
204 -static struct i2c_client *rpi_touchscreen_get_i2c(struct device *dev,
205 -                                                 const char *name)
206 -{
207 -       struct device_node *node;
208 -       struct i2c_client *client;
209 -
210 -       node = of_parse_phandle(dev->of_node, name, 0);
211 -       if (!node)
212 -               return ERR_PTR(-ENODEV);
213 -
214 -       client = of_find_i2c_device_by_node(node);
215 -
216 -       of_node_put(node);
217 -
218 -       if (!client)
219 -               return ERR_PTR(-EPROBE_DEFER);
220 -
221 -       return client;
222 -}
223 -
224 -static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
225 +static int rpi_touchscreen_probe(struct i2c_client *i2c,
226 +                                const struct i2c_device_id *id)
227  {
228 -       struct device *dev = &dsi->dev;
229 +       struct device *dev = &i2c->dev;
230         struct rpi_touchscreen *ts;
231 +       struct device_node *endpoint, *dsi_host_node;
232 +       struct mipi_dsi_host *host;
233         int ret, ver;
234 +       struct mipi_dsi_device_info info = {
235 +               .type = RPI_DSI_DRIVER_NAME,
236 +               .channel = 0,
237 +               .node = NULL,
238 +       };
239  
240         ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
241         if (!ts)
242                 return -ENOMEM;
243  
244 -       dev_set_drvdata(dev, ts);
245 +       i2c_set_clientdata(i2c, ts);
246  
247 -       ts->dsi = dsi;
248 -       dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
249 -                          MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
250 -                          MIPI_DSI_MODE_LPM);
251 -       dsi->format = MIPI_DSI_FMT_RGB888;
252 -       dsi->lanes = 1;
253 -
254 -       ts->bridge_i2c =
255 -               rpi_touchscreen_get_i2c(dev, "raspberrypi,touchscreen-bridge");
256 -       if (IS_ERR(ts->bridge_i2c)) {
257 -               ret = -EPROBE_DEFER;
258 -               return ret;
259 -       }
260 +       ts->i2c = i2c;
261  
262         ver = rpi_touchscreen_i2c_read(ts, REG_ID);
263         if (ver < 0) {
264 @@ -443,11 +387,8 @@ static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
265         }
266  
267         switch (ver) {
268 -       case 0xde:
269 -               ts->atmel_ver = 1;
270 -               break;
271 -       case 0xc3:
272 -               ts->atmel_ver = 2;
273 +       case 0xde: /* ver 1 */
274 +       case 0xc3: /* ver 2 */
275                 break;
276         default:
277                 dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
278 @@ -457,65 +398,104 @@ static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
279         /* Turn off at boot, so we can cleanly sequence powering on. */
280         rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
281  
282 -       drm_panel_init(&ts->base);
283 +       /* Look up the DSI host.  It needs to probe before we do. */
284 +       endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
285 +       dsi_host_node = of_graph_get_remote_port_parent(endpoint);
286 +       host = of_find_mipi_dsi_host_by_node(dsi_host_node);
287 +       of_node_put(dsi_host_node);
288 +       if (!host) {
289 +               of_node_put(endpoint);
290 +               return -EPROBE_DEFER;
291 +       }
292 +
293 +       info.node = of_graph_get_remote_port(endpoint);
294 +       of_node_put(endpoint);
295 +
296 +       ts->dsi = mipi_dsi_device_register_full(host, &info);
297 +       if (IS_ERR(ts->dsi)) {
298 +               dev_err(dev, "DSI device registration failed: %ld\n",
299 +                       PTR_ERR(ts->dsi));
300 +               return PTR_ERR(ts->dsi);
301 +       }
302 +
303         ts->base.dev = dev;
304         ts->base.funcs = &rpi_touchscreen_funcs;
305  
306 +       /* This appears last, as it's what will unblock the DSI host
307 +        * driver's component bind function.
308 +        */
309         ret = drm_panel_add(&ts->base);
310 -       if (ret < 0)
311 -               goto err_release_bridge;
312 -
313 -       return mipi_dsi_attach(dsi);
314 +       if (ret)
315 +               return ret;
316  
317 -err_release_bridge:
318 -       put_device(&ts->bridge_i2c->dev);
319 -       return ret;
320 +       return 0;
321  }
322  
323 -static int rpi_touchscreen_dsi_remove(struct mipi_dsi_device *dsi)
324 +static int rpi_touchscreen_remove(struct i2c_client *i2c)
325  {
326 -       struct device *dev = &dsi->dev;
327 -       struct rpi_touchscreen *ts = dev_get_drvdata(dev);
328 -       int ret;
329 +       struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
330  
331 -       ret = mipi_dsi_detach(dsi);
332 -       if (ret < 0) {
333 -               dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
334 -               return ret;
335 -       }
336 +       mipi_dsi_detach(ts->dsi);
337  
338 -       drm_panel_detach(&ts->base);
339         drm_panel_remove(&ts->base);
340  
341 -       put_device(&ts->bridge_i2c->dev);
342 +       mipi_dsi_device_unregister(ts->dsi);
343 +       kfree(ts->dsi);
344  
345         return 0;
346  }
347  
348 -static void rpi_touchscreen_dsi_shutdown(struct mipi_dsi_device *dsi)
349 +static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
350  {
351 -       struct device *dev = &dsi->dev;
352 -       struct rpi_touchscreen *ts = dev_get_drvdata(dev);
353 +       int ret;
354  
355 -       rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
356 +       dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
357 +                          MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
358 +                          MIPI_DSI_MODE_LPM);
359 +       dsi->format = MIPI_DSI_FMT_RGB888;
360 +       dsi->lanes = 1;
361 +
362 +       ret = mipi_dsi_attach(dsi);
363 +
364 +       if (ret)
365 +               dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
366 +
367 +       return ret;
368  }
369  
370 -static const struct of_device_id rpi_touchscreen_of_match[] = {
371 -       { .compatible = "raspberrypi,touchscreen" },
372 +static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
373 +       .driver.name = RPI_DSI_DRIVER_NAME,
374 +       .probe = rpi_touchscreen_dsi_probe,
375 +};
376 +
377 +static const struct of_device_id rpi_touchscreen_of_ids[] = {
378 +       { .compatible = "raspberrypi,7inch-touchscreen-panel" },
379         { } /* sentinel */
380  };
381 -MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_match);
382 +MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
383  
384 -static struct mipi_dsi_driver rpi_touchscreen_driver = {
385 +static struct i2c_driver rpi_touchscreen_driver = {
386         .driver = {
387 -               .name = "raspberrypi-touchscreen",
388 -               .of_match_table = rpi_touchscreen_of_match,
389 +               .name = "rpi_touchscreen",
390 +               .of_match_table = rpi_touchscreen_of_ids,
391         },
392 -       .probe = rpi_touchscreen_dsi_probe,
393 -       .remove = rpi_touchscreen_dsi_remove,
394 -       .shutdown = rpi_touchscreen_dsi_shutdown,
395 +       .probe = rpi_touchscreen_probe,
396 +       .remove = rpi_touchscreen_remove,
397  };
398 -module_mipi_dsi_driver(rpi_touchscreen_driver);
399 +
400 +static int __init rpi_touchscreen_init(void)
401 +{
402 +       mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
403 +       return i2c_add_driver(&rpi_touchscreen_driver);
404 +}
405 +module_init(rpi_touchscreen_init);
406 +
407 +static void __exit rpi_touchscreen_exit(void)
408 +{
409 +       i2c_del_driver(&rpi_touchscreen_driver);
410 +       mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
411 +}
412 +module_exit(rpi_touchscreen_exit);
413  
414  MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
415  MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
416 -- 
417 2.21.0
418