1 From 4da2b00d471f7fca0d770551afafa95e85ec9b89 Mon Sep 17 00:00:00 2001
2 From: Eric Anholt <eric@anholt.net>
3 Date: Tue, 26 Apr 2016 13:46:13 -0700
4 Subject: [PATCH] drm/panel: Add support for the Raspberry Pi 7" Touchscreen.
6 Signed-off-by: Eric Anholt <eric@anholt.net>
8 drivers/gpu/drm/panel/Kconfig | 9 +
9 drivers/gpu/drm/panel/Makefile | 1 +
10 .../gpu/drm/panel/panel-raspberrypi-touchscreen.c | 347 +++++++++++++++++++++
11 3 files changed, 357 insertions(+)
12 create mode 100644 drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
14 --- a/drivers/gpu/drm/panel/Kconfig
15 +++ b/drivers/gpu/drm/panel/Kconfig
16 @@ -31,6 +31,15 @@ config DRM_PANEL_LG_LG4573
17 Say Y here if you want to enable support for LG4573 RGB panel.
18 To compile this driver as a module, choose M here.
20 +config DRM_PANEL_RASPBERRYPI_TOUCHSCREEN
21 + tristate "Raspberry Pi 7-inch touchscreen panel"
22 + depends on DRM_MIPI_DSI
23 + depends on BACKLIGHT_CLASS_DEVICE
25 + Say Y here if you want to enable support for the Raspberry
26 + Pi 7" Touchscreen. To compile this driver as a module,
29 config DRM_PANEL_SAMSUNG_S6E8AA0
30 tristate "Samsung S6E8AA0 DSI video mode panel"
32 --- a/drivers/gpu/drm/panel/Makefile
33 +++ b/drivers/gpu/drm/panel/Makefile
35 obj-$(CONFIG_DRM_PANEL_SIMPLE) += panel-simple.o
36 obj-$(CONFIG_DRM_PANEL_LG_LG4573) += panel-lg-lg4573.o
37 +obj-$(CONFIG_DRM_PANEL_RASPBERRYPI_TOUCHSCREEN) += panel-raspberrypi-touchscreen.o
38 obj-$(CONFIG_DRM_PANEL_SAMSUNG_LD9040) += panel-samsung-ld9040.o
39 obj-$(CONFIG_DRM_PANEL_SAMSUNG_S6E8AA0) += panel-samsung-s6e8aa0.o
40 obj-$(CONFIG_DRM_PANEL_SHARP_LQ101R1SX01) += panel-sharp-lq101r1sx01.o
42 +++ b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
45 + * Copyright © 2016 Broadcom Limited
47 + * This program is free software; you can redistribute it and/or modify
48 + * it under the terms of the GNU General Public License version 2 as
49 + * published by the Free Software Foundation.
51 + * Portions of this file (derived from panel-simple.c) are:
53 + * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
55 + * Permission is hereby granted, free of charge, to any person obtaining a
56 + * copy of this software and associated documentation files (the "Software"),
57 + * to deal in the Software without restriction, including without limitation
58 + * the rights to use, copy, modify, merge, publish, distribute, sub license,
59 + * and/or sell copies of the Software, and to permit persons to whom the
60 + * Software is furnished to do so, subject to the following conditions:
62 + * The above copyright notice and this permission notice (including the
63 + * next paragraph) shall be included in all copies or substantial portions
66 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68 + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
69 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
71 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
72 + * DEALINGS IN THE SOFTWARE.
76 + * DOC: Raspberry Pi 7" touchscreen panel driver.
78 + * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
79 + * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
80 + * controlling power management, the LCD PWM, and the touchscreen.
82 + * This driver presents this device as a MIPI DSI panel to the DRM
83 + * driver, and should expose the touchscreen as a HID device.
86 +#include <linux/backlight.h>
87 +#include <linux/delay.h>
88 +#include <linux/err.h>
89 +#include <linux/fb.h>
90 +#include <linux/gpio.h>
91 +#include <linux/gpio/consumer.h>
92 +#include <linux/i2c.h>
93 +#include <linux/module.h>
94 +#include <linux/of.h>
95 +#include <linux/of_device.h>
96 +#include <linux/of_graph.h>
97 +#include <linux/pm.h>
99 +#include <drm/drm_panel.h>
100 +#include <drm/drmP.h>
101 +#include <drm/drm_crtc.h>
102 +#include <drm/drm_mipi_dsi.h>
103 +#include <drm/drm_panel.h>
105 +struct rpi_touchscreen {
106 + struct drm_panel base;
107 + struct mipi_dsi_device *dsi;
108 + struct i2c_client *bridge_i2c;
109 + struct backlight_device *backlight;
114 + /* Version of the firmware on the bridge chip */
118 +static const struct drm_display_mode rpi_touchscreen_modes[] = {
120 + /* This is assuming that we'll be running the DSI PLL
121 + * at 2Ghz / 3 (since we only get integer dividers),
122 + * so a pixel clock of 2Ghz / 3 / 8.
126 + .hsync_start = 800 + 61,
127 + .hsync_end = 800 + 61 + 2,
128 + .htotal = 800 + 61 + 2 + 44,
130 + .vsync_start = 480 + 7,
131 + .vsync_end = 480 + 7 + 2,
132 + .vtotal = 480 + 7 + 2 + 21,
137 +static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
139 + return container_of(panel, struct rpi_touchscreen, base);
147 +#define REGDUMP(reg) { #reg, reg }
149 +static int rpi_touchscreen_disable(struct drm_panel *panel)
151 + struct rpi_touchscreen *ts = panel_to_ts(panel);
152 + pr_err("disable\n");
157 + if (ts->backlight) {
158 + ts->backlight->props.power = FB_BLANK_POWERDOWN;
159 + backlight_update_status(ts->backlight);
162 + ts->enabled = false;
167 +static int rpi_touchscreen_unprepare(struct drm_panel *panel)
169 + struct rpi_touchscreen *ts = panel_to_ts(panel);
174 + ts->prepared = false;
179 +static int rpi_touchscreen_prepare(struct drm_panel *panel)
181 + struct rpi_touchscreen *ts = panel_to_ts(panel);
186 + ts->prepared = true;
192 + * Powers on the panel once the DSI link is up.
194 + * The TC358762 is run in PLLOFF mode, where it usees the MIPI DSI
195 + * byte clock instead of an external reference clock. This means that
196 + * we need the DSI host to be on and transmitting before we start
199 +static int rpi_touchscreen_enable(struct drm_panel *panel)
201 + struct rpi_touchscreen *ts = panel_to_ts(panel);
206 + if (ts->backlight) {
207 + ts->backlight->props.power = FB_BLANK_UNBLANK;
208 + backlight_update_status(ts->backlight);
211 + ts->enabled = true;
216 +static int rpi_touchscreen_get_modes(struct drm_panel *panel)
218 + struct drm_connector *connector = panel->connector;
219 + struct drm_device *drm = panel->drm;
220 + unsigned int i, num = 0;
222 + for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
223 + const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
224 + struct drm_display_mode *mode;
226 + mode = drm_mode_duplicate(drm, m);
228 + dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
229 + m->hdisplay, m->vdisplay, m->vrefresh);
233 + mode->type |= DRM_MODE_TYPE_DRIVER;
236 + mode->type |= DRM_MODE_TYPE_PREFERRED;
238 + drm_mode_set_name(mode);
240 + drm_mode_probed_add(connector, mode);
244 + connector->display_info.bpc = 8;
245 + connector->display_info.width_mm = 217; /* XXX */
246 + connector->display_info.height_mm = 136; /* XXX */
251 +static int rpi_touchscreen_backlight_update(struct backlight_device *bl)
253 + int brightness = bl->props.brightness;
255 + if (bl->props.power != FB_BLANK_UNBLANK ||
256 + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
262 +static const struct backlight_ops rpi_touchscreen_backlight_ops = {
263 + .update_status = rpi_touchscreen_backlight_update,
266 +static const struct drm_panel_funcs rpi_touchscreen_funcs = {
267 + .disable = rpi_touchscreen_disable,
268 + .unprepare = rpi_touchscreen_unprepare,
269 + .prepare = rpi_touchscreen_prepare,
270 + .enable = rpi_touchscreen_enable,
271 + .get_modes = rpi_touchscreen_get_modes,
274 +static struct i2c_client *rpi_touchscreen_get_i2c(struct device *dev,
277 + struct device_node *node;
278 + struct i2c_client *client;
280 + node = of_parse_phandle(dev->of_node, name, 0);
282 + return ERR_PTR(-ENODEV);
284 + client = of_find_i2c_device_by_node(node);
291 +static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
293 + struct device *dev = &dsi->dev;
294 + struct rpi_touchscreen *ts;
297 + ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
301 + dev_set_drvdata(dev, ts);
304 + dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
305 + MIPI_DSI_MODE_VIDEO_SYNC_PULSE);
306 + dsi->format = MIPI_DSI_FMT_RGB888;
310 + rpi_touchscreen_get_i2c(dev, "raspberrypi,touchscreen-bridge");
311 + if (!ts->bridge_i2c) {
312 + ret = -EPROBE_DEFER;
318 + devm_backlight_device_register(dev,
319 + "raspberrypi-touchscreen-backlight",
321 + &rpi_touchscreen_backlight_ops,
323 + if (IS_ERR(ts->backlight)) {
324 + DRM_ERROR("failed to register backlight\n");
325 + return PTR_ERR(ts->backlight);
327 + ts->backlight->props.max_brightness = RPI_TOUCHSCREEN_MAX_BRIGHTNESS;
328 + ts->backlight->props.brightness = RPI_TOUCHSCREEN_MAX_BRIGHTNESS;
331 + drm_panel_init(&ts->base);
332 + ts->base.dev = dev;
333 + ts->base.funcs = &rpi_touchscreen_funcs;
335 + ret = drm_panel_add(&ts->base);
337 + goto err_release_bridge;
339 + return mipi_dsi_attach(dsi);
342 + put_device(&ts->bridge_i2c->dev);
346 +static int rpi_touchscreen_dsi_remove(struct mipi_dsi_device *dsi)
348 + struct device *dev = &dsi->dev;
349 + struct rpi_touchscreen *ts = dev_get_drvdata(dev);
352 + ret = mipi_dsi_detach(dsi);
354 + dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
358 + drm_panel_detach(&ts->base);
359 + drm_panel_remove(&ts->base);
361 + put_device(&ts->bridge_i2c->dev);
366 +static void rpi_touchscreen_dsi_shutdown(struct mipi_dsi_device *dsi)
368 + /* XXX: poweroff */
371 +static const struct of_device_id rpi_touchscreen_of_match[] = {
372 + { .compatible = "raspberrypi,touchscreen" },
375 +MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_match);
377 +static struct mipi_dsi_driver rpi_touchscreen_driver = {
379 + .name = "raspberrypi-touchscreen",
380 + .of_match_table = rpi_touchscreen_of_match,
382 + .probe = rpi_touchscreen_dsi_probe,
383 + .remove = rpi_touchscreen_dsi_remove,
384 + .shutdown = rpi_touchscreen_dsi_shutdown,
386 +module_mipi_dsi_driver(rpi_touchscreen_driver);
388 +MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
389 +MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
390 +MODULE_LICENSE("GPL v2");