1/* 2 * Parade PS8622 eDP/LVDS bridge driver 3 * 4 * Copyright (C) 2014 Google, Inc. 5 * 6 * This software is licensed under the terms of the GNU General Public 7 * License version 2, as published by the Free Software Foundation, and 8 * may be copied, distributed, and modified under those terms. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 */ 15 16#include <linux/backlight.h> 17#include <linux/delay.h> 18#include <linux/err.h> 19#include <linux/fb.h> 20#include <linux/gpio.h> 21#include <linux/i2c.h> 22#include <linux/module.h> 23#include <linux/of.h> 24#include <linux/of_device.h> 25#include <linux/of_graph.h> 26#include <linux/pm.h> 27#include <linux/regulator/consumer.h> 28 29#include <drm/drm_panel.h> 30 31#include "drmP.h" 32#include "drm_crtc.h" 33#include "drm_crtc_helper.h" 34 35/* Brightness scale on the Parade chip */ 36#define PS8622_MAX_BRIGHTNESS 0xff 37 38/* Timings taken from the version 1.7 datasheet for the PS8622/PS8625 */ 39#define PS8622_POWER_RISE_T1_MIN_US 10 40#define PS8622_POWER_RISE_T1_MAX_US 10000 41#define PS8622_RST_HIGH_T2_MIN_US 3000 42#define PS8622_RST_HIGH_T2_MAX_US 30000 43#define PS8622_PWMO_END_T12_MS 200 44#define PS8622_POWER_FALL_T16_MAX_US 10000 45#define PS8622_POWER_OFF_T17_MS 500 46 47#if ((PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US) > \ 48 (PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US)) 49#error "T2.min + T1.max must be less than T2.max + T1.min" 50#endif 51 52struct ps8622_bridge { 53 struct drm_connector connector; 54 struct i2c_client *client; 55 struct drm_bridge bridge; 56 struct drm_panel *panel; 57 struct regulator *v12; 58 struct backlight_device *bl; 59 60 struct gpio_desc *gpio_slp; 61 struct gpio_desc *gpio_rst; 62 63 u32 max_lane_count; 64 u32 lane_count; 65 66 bool enabled; 67}; 68 69static inline struct ps8622_bridge * 70 bridge_to_ps8622(struct drm_bridge *bridge) 71{ 72 return container_of(bridge, struct ps8622_bridge, bridge); 73} 74 75static inline struct ps8622_bridge * 76 connector_to_ps8622(struct drm_connector *connector) 77{ 78 return container_of(connector, struct ps8622_bridge, connector); 79} 80 81static int ps8622_set(struct i2c_client *client, u8 page, u8 reg, u8 val) 82{ 83 int ret; 84 struct i2c_adapter *adap = client->adapter; 85 struct i2c_msg msg; 86 u8 data[] = {reg, val}; 87 88 msg.addr = client->addr + page; 89 msg.flags = 0; 90 msg.len = sizeof(data); 91 msg.buf = data; 92 93 ret = i2c_transfer(adap, &msg, 1); 94 if (ret != 1) 95 pr_warn("PS8622 I2C write (0x%02x,0x%02x,0x%02x) failed: %d\n", 96 client->addr + page, reg, val, ret); 97 return !(ret == 1); 98} 99 100static int ps8622_send_config(struct ps8622_bridge *ps8622) 101{ 102 struct i2c_client *cl = ps8622->client; 103 int err = 0; 104 105 /* HPD low */ 106 err = ps8622_set(cl, 0x02, 0xa1, 0x01); 107 if (err) 108 goto error; 109 110 /* SW setting: [1:0] SW output 1.2V voltage is lower to 96% */ 111 err = ps8622_set(cl, 0x04, 0x14, 0x01); 112 if (err) 113 goto error; 114 115 /* RCO SS setting: [5:4] = b01 0.5%, b10 1%, b11 1.5% */ 116 err = ps8622_set(cl, 0x04, 0xe3, 0x20); 117 if (err) 118 goto error; 119 120 /* [7] RCO SS enable */ 121 err = ps8622_set(cl, 0x04, 0xe2, 0x80); 122 if (err) 123 goto error; 124 125 /* RPHY Setting 126 * [3:2] CDR tune wait cycle before measure for fine tune 127 * b00: 1us b01: 0.5us b10:2us, b11: 4us 128 */ 129 err = ps8622_set(cl, 0x04, 0x8a, 0x0c); 130 if (err) 131 goto error; 132 133 /* [3] RFD always on */ 134 err = ps8622_set(cl, 0x04, 0x89, 0x08); 135 if (err) 136 goto error; 137 138 /* CTN lock in/out: 20000ppm/80000ppm. Lock out 2 times. */ 139 err = ps8622_set(cl, 0x04, 0x71, 0x2d); 140 if (err) 141 goto error; 142 143 /* 2.7G CDR settings: NOF=40LSB for HBR CDR setting */ 144 err = ps8622_set(cl, 0x04, 0x7d, 0x07); 145 if (err) 146 goto error; 147 148 /* [1:0] Fmin=+4bands */ 149 err = ps8622_set(cl, 0x04, 0x7b, 0x00); 150 if (err) 151 goto error; 152 153 /* [7:5] DCO_FTRNG=+-40% */ 154 err = ps8622_set(cl, 0x04, 0x7a, 0xfd); 155 if (err) 156 goto error; 157 158 /* 1.62G CDR settings: [5:2]NOF=64LSB [1:0]DCO scale is 2/5 */ 159 err = ps8622_set(cl, 0x04, 0xc0, 0x12); 160 if (err) 161 goto error; 162 163 /* Gitune=-37% */ 164 err = ps8622_set(cl, 0x04, 0xc1, 0x92); 165 if (err) 166 goto error; 167 168 /* Fbstep=100% */ 169 err = ps8622_set(cl, 0x04, 0xc2, 0x1c); 170 if (err) 171 goto error; 172 173 /* [7] LOS signal disable */ 174 err = ps8622_set(cl, 0x04, 0x32, 0x80); 175 if (err) 176 goto error; 177 178 /* RPIO Setting: [7:4] LVDS driver bias current : 75% (250mV swing) */ 179 err = ps8622_set(cl, 0x04, 0x00, 0xb0); 180 if (err) 181 goto error; 182 183 /* [7:6] Right-bar GPIO output strength is 8mA */ 184 err = ps8622_set(cl, 0x04, 0x15, 0x40); 185 if (err) 186 goto error; 187 188 /* EQ Training State Machine Setting, RCO calibration start */ 189 err = ps8622_set(cl, 0x04, 0x54, 0x10); 190 if (err) 191 goto error; 192 193 /* Logic, needs more than 10 I2C command */ 194 /* [4:0] MAX_LANE_COUNT set to max supported lanes */ 195 err = ps8622_set(cl, 0x01, 0x02, 0x80 | ps8622->max_lane_count); 196 if (err) 197 goto error; 198 199 /* [4:0] LANE_COUNT_SET set to chosen lane count */ 200 err = ps8622_set(cl, 0x01, 0x21, 0x80 | ps8622->lane_count); 201 if (err) 202 goto error; 203 204 err = ps8622_set(cl, 0x00, 0x52, 0x20); 205 if (err) 206 goto error; 207 208 /* HPD CP toggle enable */ 209 err = ps8622_set(cl, 0x00, 0xf1, 0x03); 210 if (err) 211 goto error; 212 213 err = ps8622_set(cl, 0x00, 0x62, 0x41); 214 if (err) 215 goto error; 216 217 /* Counter number, add 1ms counter delay */ 218 err = ps8622_set(cl, 0x00, 0xf6, 0x01); 219 if (err) 220 goto error; 221 222 /* [6]PWM function control by DPCD0040f[7], default is PWM block */ 223 err = ps8622_set(cl, 0x00, 0x77, 0x06); 224 if (err) 225 goto error; 226 227 /* 04h Adjust VTotal toleranceto fix the 30Hz no display issue */ 228 err = ps8622_set(cl, 0x00, 0x4c, 0x04); 229 if (err) 230 goto error; 231 232 /* DPCD00400='h00, Parade OUI ='h001cf8 */ 233 err = ps8622_set(cl, 0x01, 0xc0, 0x00); 234 if (err) 235 goto error; 236 237 /* DPCD00401='h1c */ 238 err = ps8622_set(cl, 0x01, 0xc1, 0x1c); 239 if (err) 240 goto error; 241 242 /* DPCD00402='hf8 */ 243 err = ps8622_set(cl, 0x01, 0xc2, 0xf8); 244 if (err) 245 goto error; 246 247 /* DPCD403~408 = ASCII code, D2SLV5='h4432534c5635 */ 248 err = ps8622_set(cl, 0x01, 0xc3, 0x44); 249 if (err) 250 goto error; 251 252 /* DPCD404 */ 253 err = ps8622_set(cl, 0x01, 0xc4, 0x32); 254 if (err) 255 goto error; 256 257 /* DPCD405 */ 258 err = ps8622_set(cl, 0x01, 0xc5, 0x53); 259 if (err) 260 goto error; 261 262 /* DPCD406 */ 263 err = ps8622_set(cl, 0x01, 0xc6, 0x4c); 264 if (err) 265 goto error; 266 267 /* DPCD407 */ 268 err = ps8622_set(cl, 0x01, 0xc7, 0x56); 269 if (err) 270 goto error; 271 272 /* DPCD408 */ 273 err = ps8622_set(cl, 0x01, 0xc8, 0x35); 274 if (err) 275 goto error; 276 277 /* DPCD40A, Initial Code major revision '01' */ 278 err = ps8622_set(cl, 0x01, 0xca, 0x01); 279 if (err) 280 goto error; 281 282 /* DPCD40B, Initial Code minor revision '05' */ 283 err = ps8622_set(cl, 0x01, 0xcb, 0x05); 284 if (err) 285 goto error; 286 287 288 if (ps8622->bl) { 289 /* DPCD720, internal PWM */ 290 err = ps8622_set(cl, 0x01, 0xa5, 0xa0); 291 if (err) 292 goto error; 293 294 /* FFh for 100% brightness, 0h for 0% brightness */ 295 err = ps8622_set(cl, 0x01, 0xa7, 296 ps8622->bl->props.brightness); 297 if (err) 298 goto error; 299 } else { 300 /* DPCD720, external PWM */ 301 err = ps8622_set(cl, 0x01, 0xa5, 0x80); 302 if (err) 303 goto error; 304 } 305 306 /* Set LVDS output as 6bit-VESA mapping, single LVDS channel */ 307 err = ps8622_set(cl, 0x01, 0xcc, 0x13); 308 if (err) 309 goto error; 310 311 /* Enable SSC set by register */ 312 err = ps8622_set(cl, 0x02, 0xb1, 0x20); 313 if (err) 314 goto error; 315 316 /* Set SSC enabled and +/-1% central spreading */ 317 err = ps8622_set(cl, 0x04, 0x10, 0x16); 318 if (err) 319 goto error; 320 321 /* Logic end */ 322 /* MPU Clock source: LC => RCO */ 323 err = ps8622_set(cl, 0x04, 0x59, 0x60); 324 if (err) 325 goto error; 326 327 /* LC -> RCO */ 328 err = ps8622_set(cl, 0x04, 0x54, 0x14); 329 if (err) 330 goto error; 331 332 /* HPD high */ 333 err = ps8622_set(cl, 0x02, 0xa1, 0x91); 334 335error: 336 return err ? -EIO : 0; 337} 338 339static int ps8622_backlight_update(struct backlight_device *bl) 340{ 341 struct ps8622_bridge *ps8622 = dev_get_drvdata(&bl->dev); 342 int ret, brightness = bl->props.brightness; 343 344 if (bl->props.power != FB_BLANK_UNBLANK || 345 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) 346 brightness = 0; 347 348 if (!ps8622->enabled) 349 return -EINVAL; 350 351 ret = ps8622_set(ps8622->client, 0x01, 0xa7, brightness); 352 353 return ret; 354} 355 356static const struct backlight_ops ps8622_backlight_ops = { 357 .update_status = ps8622_backlight_update, 358}; 359 360static void ps8622_pre_enable(struct drm_bridge *bridge) 361{ 362 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge); 363 int ret; 364 365 if (ps8622->enabled) 366 return; 367 368 gpiod_set_value(ps8622->gpio_rst, 0); 369 370 if (ps8622->v12) { 371 ret = regulator_enable(ps8622->v12); 372 if (ret) 373 DRM_ERROR("fails to enable ps8622->v12"); 374 } 375 376 if (drm_panel_prepare(ps8622->panel)) { 377 DRM_ERROR("failed to prepare panel\n"); 378 return; 379 } 380 381 gpiod_set_value(ps8622->gpio_slp, 1); 382 383 /* 384 * T1 is the range of time that it takes for the power to rise after we 385 * enable the lcd/ps8622 fet. T2 is the range of time in which the 386 * data sheet specifies we should deassert the reset pin. 387 * 388 * If it takes T1.max for the power to rise, we need to wait atleast 389 * T2.min before deasserting the reset pin. If it takes T1.min for the 390 * power to rise, we need to wait at most T2.max before deasserting the 391 * reset pin. 392 */ 393 usleep_range(PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US, 394 PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US); 395 396 gpiod_set_value(ps8622->gpio_rst, 1); 397 398 /* wait 20ms after RST high */ 399 usleep_range(20000, 30000); 400 401 ret = ps8622_send_config(ps8622); 402 if (ret) { 403 DRM_ERROR("Failed to send config to bridge (%d)\n", ret); 404 return; 405 } 406 407 ps8622->enabled = true; 408} 409 410static void ps8622_enable(struct drm_bridge *bridge) 411{ 412 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge); 413 414 if (drm_panel_enable(ps8622->panel)) { 415 DRM_ERROR("failed to enable panel\n"); 416 return; 417 } 418} 419 420static void ps8622_disable(struct drm_bridge *bridge) 421{ 422 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge); 423 424 if (drm_panel_disable(ps8622->panel)) { 425 DRM_ERROR("failed to disable panel\n"); 426 return; 427 } 428 msleep(PS8622_PWMO_END_T12_MS); 429} 430 431static void ps8622_post_disable(struct drm_bridge *bridge) 432{ 433 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge); 434 435 if (!ps8622->enabled) 436 return; 437 438 ps8622->enabled = false; 439 440 /* 441 * This doesn't matter if the regulators are turned off, but something 442 * else might keep them on. In that case, we want to assert the slp gpio 443 * to lower power. 444 */ 445 gpiod_set_value(ps8622->gpio_slp, 0); 446 447 if (drm_panel_unprepare(ps8622->panel)) { 448 DRM_ERROR("failed to unprepare panel\n"); 449 return; 450 } 451 452 if (ps8622->v12) 453 regulator_disable(ps8622->v12); 454 455 /* 456 * Sleep for at least the amount of time that it takes the power rail to 457 * fall to prevent asserting the rst gpio from doing anything. 458 */ 459 usleep_range(PS8622_POWER_FALL_T16_MAX_US, 460 2 * PS8622_POWER_FALL_T16_MAX_US); 461 gpiod_set_value(ps8622->gpio_rst, 0); 462 463 msleep(PS8622_POWER_OFF_T17_MS); 464} 465 466static int ps8622_get_modes(struct drm_connector *connector) 467{ 468 struct ps8622_bridge *ps8622; 469 470 ps8622 = connector_to_ps8622(connector); 471 472 return drm_panel_get_modes(ps8622->panel); 473} 474 475static struct drm_encoder *ps8622_best_encoder(struct drm_connector *connector) 476{ 477 struct ps8622_bridge *ps8622; 478 479 ps8622 = connector_to_ps8622(connector); 480 481 return ps8622->bridge.encoder; 482} 483 484static const struct drm_connector_helper_funcs ps8622_connector_helper_funcs = { 485 .get_modes = ps8622_get_modes, 486 .best_encoder = ps8622_best_encoder, 487}; 488 489static enum drm_connector_status ps8622_detect(struct drm_connector *connector, 490 bool force) 491{ 492 return connector_status_connected; 493} 494 495static void ps8622_connector_destroy(struct drm_connector *connector) 496{ 497 drm_connector_cleanup(connector); 498} 499 500static const struct drm_connector_funcs ps8622_connector_funcs = { 501 .dpms = drm_helper_connector_dpms, 502 .fill_modes = drm_helper_probe_single_connector_modes, 503 .detect = ps8622_detect, 504 .destroy = ps8622_connector_destroy, 505}; 506 507static int ps8622_attach(struct drm_bridge *bridge) 508{ 509 struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge); 510 int ret; 511 512 if (!bridge->encoder) { 513 DRM_ERROR("Parent encoder object not found"); 514 return -ENODEV; 515 } 516 517 ps8622->connector.polled = DRM_CONNECTOR_POLL_HPD; 518 ret = drm_connector_init(bridge->dev, &ps8622->connector, 519 &ps8622_connector_funcs, DRM_MODE_CONNECTOR_LVDS); 520 if (ret) { 521 DRM_ERROR("Failed to initialize connector with drm\n"); 522 return ret; 523 } 524 drm_connector_helper_add(&ps8622->connector, 525 &ps8622_connector_helper_funcs); 526 drm_connector_register(&ps8622->connector); 527 drm_mode_connector_attach_encoder(&ps8622->connector, 528 bridge->encoder); 529 530 if (ps8622->panel) 531 drm_panel_attach(ps8622->panel, &ps8622->connector); 532 533 drm_helper_hpd_irq_event(ps8622->connector.dev); 534 535 return ret; 536} 537 538static const struct drm_bridge_funcs ps8622_bridge_funcs = { 539 .pre_enable = ps8622_pre_enable, 540 .enable = ps8622_enable, 541 .disable = ps8622_disable, 542 .post_disable = ps8622_post_disable, 543 .attach = ps8622_attach, 544}; 545 546static const struct of_device_id ps8622_devices[] = { 547 {.compatible = "parade,ps8622",}, 548 {.compatible = "parade,ps8625",}, 549 {} 550}; 551MODULE_DEVICE_TABLE(of, ps8622_devices); 552 553static int ps8622_probe(struct i2c_client *client, 554 const struct i2c_device_id *id) 555{ 556 struct device *dev = &client->dev; 557 struct device_node *endpoint, *panel_node; 558 struct ps8622_bridge *ps8622; 559 int ret; 560 561 ps8622 = devm_kzalloc(dev, sizeof(*ps8622), GFP_KERNEL); 562 if (!ps8622) 563 return -ENOMEM; 564 565 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL); 566 if (endpoint) { 567 panel_node = of_graph_get_remote_port_parent(endpoint); 568 if (panel_node) { 569 ps8622->panel = of_drm_find_panel(panel_node); 570 of_node_put(panel_node); 571 if (!ps8622->panel) 572 return -EPROBE_DEFER; 573 } 574 } 575 576 ps8622->client = client; 577 578 ps8622->v12 = devm_regulator_get(dev, "vdd12"); 579 if (IS_ERR(ps8622->v12)) { 580 dev_info(dev, "no 1.2v regulator found for PS8622\n"); 581 ps8622->v12 = NULL; 582 } 583 584 ps8622->gpio_slp = devm_gpiod_get(dev, "sleep"); 585 if (IS_ERR(ps8622->gpio_slp)) { 586 ret = PTR_ERR(ps8622->gpio_slp); 587 dev_err(dev, "cannot get gpio_slp %d\n", ret); 588 return ret; 589 } 590 ret = gpiod_direction_output(ps8622->gpio_slp, 1); 591 if (ret) { 592 dev_err(dev, "cannot configure gpio_slp\n"); 593 return ret; 594 } 595 596 ps8622->gpio_rst = devm_gpiod_get(dev, "reset"); 597 if (IS_ERR(ps8622->gpio_rst)) { 598 ret = PTR_ERR(ps8622->gpio_rst); 599 dev_err(dev, "cannot get gpio_rst %d\n", ret); 600 return ret; 601 } 602 /* 603 * Assert the reset pin high to avoid the bridge being 604 * initialized prematurely 605 */ 606 ret = gpiod_direction_output(ps8622->gpio_rst, 1); 607 if (ret) { 608 dev_err(dev, "cannot configure gpio_rst\n"); 609 return ret; 610 } 611 612 ps8622->max_lane_count = id->driver_data; 613 614 if (of_property_read_u32(dev->of_node, "lane-count", 615 &ps8622->lane_count)) { 616 ps8622->lane_count = ps8622->max_lane_count; 617 } else if (ps8622->lane_count > ps8622->max_lane_count) { 618 dev_info(dev, "lane-count property is too high," 619 "using max_lane_count\n"); 620 ps8622->lane_count = ps8622->max_lane_count; 621 } 622 623 if (!of_find_property(dev->of_node, "use-external-pwm", NULL)) { 624 ps8622->bl = backlight_device_register("ps8622-backlight", 625 dev, ps8622, &ps8622_backlight_ops, 626 NULL); 627 if (IS_ERR(ps8622->bl)) { 628 DRM_ERROR("failed to register backlight\n"); 629 ret = PTR_ERR(ps8622->bl); 630 ps8622->bl = NULL; 631 return ret; 632 } 633 ps8622->bl->props.max_brightness = PS8622_MAX_BRIGHTNESS; 634 ps8622->bl->props.brightness = PS8622_MAX_BRIGHTNESS; 635 } 636 637 ps8622->bridge.funcs = &ps8622_bridge_funcs; 638 ps8622->bridge.of_node = dev->of_node; 639 ret = drm_bridge_add(&ps8622->bridge); 640 if (ret) { 641 DRM_ERROR("Failed to add bridge\n"); 642 return ret; 643 } 644 645 i2c_set_clientdata(client, ps8622); 646 647 return 0; 648} 649 650static int ps8622_remove(struct i2c_client *client) 651{ 652 struct ps8622_bridge *ps8622 = i2c_get_clientdata(client); 653 654 if (ps8622->bl) 655 backlight_device_unregister(ps8622->bl); 656 657 drm_bridge_remove(&ps8622->bridge); 658 659 return 0; 660} 661 662static const struct i2c_device_id ps8622_i2c_table[] = { 663 /* Device type, max_lane_count */ 664 {"ps8622", 1}, 665 {"ps8625", 2}, 666 {}, 667}; 668MODULE_DEVICE_TABLE(i2c, ps8622_i2c_table); 669 670static struct i2c_driver ps8622_driver = { 671 .id_table = ps8622_i2c_table, 672 .probe = ps8622_probe, 673 .remove = ps8622_remove, 674 .driver = { 675 .name = "ps8622", 676 .owner = THIS_MODULE, 677 .of_match_table = ps8622_devices, 678 }, 679}; 680module_i2c_driver(ps8622_driver); 681 682MODULE_AUTHOR("Vincent Palatin <vpalatin@chromium.org>"); 683MODULE_DESCRIPTION("Parade ps8622/ps8625 eDP-LVDS converter driver"); 684MODULE_LICENSE("GPL v2"); 685