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