root/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. panel_to_ts
  2. rpi_touchscreen_i2c_read
  3. rpi_touchscreen_i2c_write
  4. rpi_touchscreen_write
  5. rpi_touchscreen_disable
  6. rpi_touchscreen_noop
  7. rpi_touchscreen_enable
  8. rpi_touchscreen_get_modes
  9. rpi_touchscreen_probe
  10. rpi_touchscreen_remove
  11. rpi_touchscreen_dsi_probe
  12. rpi_touchscreen_init
  13. rpi_touchscreen_exit

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

/* [<][>][^][v][top][bottom][index][help] */