2 * Parade PS8622 eDP/LVDS bridge driver
4 * Copyright (C) 2014 Google, Inc.
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.
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.
16 #include <linux/backlight.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/gpio.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/i2c.h>
22 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/of_graph.h>
27 #include <linux/regulator/consumer.h>
29 #include <drm/drm_panel.h>
33 #include "drm_crtc_helper.h"
34 #include "drm_atomic_helper.h"
36 /* Brightness scale on the Parade chip */
37 #define PS8622_MAX_BRIGHTNESS 0xff
39 /* Timings taken from the version 1.7 datasheet for the PS8622/PS8625 */
40 #define PS8622_POWER_RISE_T1_MIN_US 10
41 #define PS8622_POWER_RISE_T1_MAX_US 10000
42 #define PS8622_RST_HIGH_T2_MIN_US 3000
43 #define PS8622_RST_HIGH_T2_MAX_US 30000
44 #define PS8622_PWMO_END_T12_MS 200
45 #define PS8622_POWER_FALL_T16_MAX_US 10000
46 #define PS8622_POWER_OFF_T17_MS 500
48 #if ((PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US) > \
49 (PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US))
50 #error "T2.min + T1.max must be less than T2.max + T1.min"
53 struct ps8622_bridge
{
54 struct drm_connector connector
;
55 struct i2c_client
*client
;
56 struct drm_bridge bridge
;
57 struct drm_panel
*panel
;
58 struct regulator
*v12
;
59 struct backlight_device
*bl
;
61 struct gpio_desc
*gpio_slp
;
62 struct gpio_desc
*gpio_rst
;
70 static inline struct ps8622_bridge
*
71 bridge_to_ps8622(struct drm_bridge
*bridge
)
73 return container_of(bridge
, struct ps8622_bridge
, bridge
);
76 static inline struct ps8622_bridge
*
77 connector_to_ps8622(struct drm_connector
*connector
)
79 return container_of(connector
, struct ps8622_bridge
, connector
);
82 static int ps8622_set(struct i2c_client
*client
, u8 page
, u8 reg
, u8 val
)
85 struct i2c_adapter
*adap
= client
->adapter
;
87 u8 data
[] = {reg
, val
};
89 msg
.addr
= client
->addr
+ page
;
91 msg
.len
= sizeof(data
);
94 ret
= i2c_transfer(adap
, &msg
, 1);
96 pr_warn("PS8622 I2C write (0x%02x,0x%02x,0x%02x) failed: %d\n",
97 client
->addr
+ page
, reg
, val
, ret
);
101 static int ps8622_send_config(struct ps8622_bridge
*ps8622
)
103 struct i2c_client
*cl
= ps8622
->client
;
107 err
= ps8622_set(cl
, 0x02, 0xa1, 0x01);
111 /* SW setting: [1:0] SW output 1.2V voltage is lower to 96% */
112 err
= ps8622_set(cl
, 0x04, 0x14, 0x01);
116 /* RCO SS setting: [5:4] = b01 0.5%, b10 1%, b11 1.5% */
117 err
= ps8622_set(cl
, 0x04, 0xe3, 0x20);
121 /* [7] RCO SS enable */
122 err
= ps8622_set(cl
, 0x04, 0xe2, 0x80);
127 * [3:2] CDR tune wait cycle before measure for fine tune
128 * b00: 1us b01: 0.5us b10:2us, b11: 4us
130 err
= ps8622_set(cl
, 0x04, 0x8a, 0x0c);
134 /* [3] RFD always on */
135 err
= ps8622_set(cl
, 0x04, 0x89, 0x08);
139 /* CTN lock in/out: 20000ppm/80000ppm. Lock out 2 times. */
140 err
= ps8622_set(cl
, 0x04, 0x71, 0x2d);
144 /* 2.7G CDR settings: NOF=40LSB for HBR CDR setting */
145 err
= ps8622_set(cl
, 0x04, 0x7d, 0x07);
149 /* [1:0] Fmin=+4bands */
150 err
= ps8622_set(cl
, 0x04, 0x7b, 0x00);
154 /* [7:5] DCO_FTRNG=+-40% */
155 err
= ps8622_set(cl
, 0x04, 0x7a, 0xfd);
159 /* 1.62G CDR settings: [5:2]NOF=64LSB [1:0]DCO scale is 2/5 */
160 err
= ps8622_set(cl
, 0x04, 0xc0, 0x12);
165 err
= ps8622_set(cl
, 0x04, 0xc1, 0x92);
170 err
= ps8622_set(cl
, 0x04, 0xc2, 0x1c);
174 /* [7] LOS signal disable */
175 err
= ps8622_set(cl
, 0x04, 0x32, 0x80);
179 /* RPIO Setting: [7:4] LVDS driver bias current : 75% (250mV swing) */
180 err
= ps8622_set(cl
, 0x04, 0x00, 0xb0);
184 /* [7:6] Right-bar GPIO output strength is 8mA */
185 err
= ps8622_set(cl
, 0x04, 0x15, 0x40);
189 /* EQ Training State Machine Setting, RCO calibration start */
190 err
= ps8622_set(cl
, 0x04, 0x54, 0x10);
194 /* Logic, needs more than 10 I2C command */
195 /* [4:0] MAX_LANE_COUNT set to max supported lanes */
196 err
= ps8622_set(cl
, 0x01, 0x02, 0x80 | ps8622
->max_lane_count
);
200 /* [4:0] LANE_COUNT_SET set to chosen lane count */
201 err
= ps8622_set(cl
, 0x01, 0x21, 0x80 | ps8622
->lane_count
);
205 err
= ps8622_set(cl
, 0x00, 0x52, 0x20);
209 /* HPD CP toggle enable */
210 err
= ps8622_set(cl
, 0x00, 0xf1, 0x03);
214 err
= ps8622_set(cl
, 0x00, 0x62, 0x41);
218 /* Counter number, add 1ms counter delay */
219 err
= ps8622_set(cl
, 0x00, 0xf6, 0x01);
223 /* [6]PWM function control by DPCD0040f[7], default is PWM block */
224 err
= ps8622_set(cl
, 0x00, 0x77, 0x06);
228 /* 04h Adjust VTotal toleranceto fix the 30Hz no display issue */
229 err
= ps8622_set(cl
, 0x00, 0x4c, 0x04);
233 /* DPCD00400='h00, Parade OUI ='h001cf8 */
234 err
= ps8622_set(cl
, 0x01, 0xc0, 0x00);
239 err
= ps8622_set(cl
, 0x01, 0xc1, 0x1c);
244 err
= ps8622_set(cl
, 0x01, 0xc2, 0xf8);
248 /* DPCD403~408 = ASCII code, D2SLV5='h4432534c5635 */
249 err
= ps8622_set(cl
, 0x01, 0xc3, 0x44);
254 err
= ps8622_set(cl
, 0x01, 0xc4, 0x32);
259 err
= ps8622_set(cl
, 0x01, 0xc5, 0x53);
264 err
= ps8622_set(cl
, 0x01, 0xc6, 0x4c);
269 err
= ps8622_set(cl
, 0x01, 0xc7, 0x56);
274 err
= ps8622_set(cl
, 0x01, 0xc8, 0x35);
278 /* DPCD40A, Initial Code major revision '01' */
279 err
= ps8622_set(cl
, 0x01, 0xca, 0x01);
283 /* DPCD40B, Initial Code minor revision '05' */
284 err
= ps8622_set(cl
, 0x01, 0xcb, 0x05);
290 /* DPCD720, internal PWM */
291 err
= ps8622_set(cl
, 0x01, 0xa5, 0xa0);
295 /* FFh for 100% brightness, 0h for 0% brightness */
296 err
= ps8622_set(cl
, 0x01, 0xa7,
297 ps8622
->bl
->props
.brightness
);
301 /* DPCD720, external PWM */
302 err
= ps8622_set(cl
, 0x01, 0xa5, 0x80);
307 /* Set LVDS output as 6bit-VESA mapping, single LVDS channel */
308 err
= ps8622_set(cl
, 0x01, 0xcc, 0x13);
312 /* Enable SSC set by register */
313 err
= ps8622_set(cl
, 0x02, 0xb1, 0x20);
317 /* Set SSC enabled and +/-1% central spreading */
318 err
= ps8622_set(cl
, 0x04, 0x10, 0x16);
323 /* MPU Clock source: LC => RCO */
324 err
= ps8622_set(cl
, 0x04, 0x59, 0x60);
329 err
= ps8622_set(cl
, 0x04, 0x54, 0x14);
334 err
= ps8622_set(cl
, 0x02, 0xa1, 0x91);
337 return err
? -EIO
: 0;
340 static int ps8622_backlight_update(struct backlight_device
*bl
)
342 struct ps8622_bridge
*ps8622
= dev_get_drvdata(&bl
->dev
);
343 int ret
, brightness
= bl
->props
.brightness
;
345 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
346 bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
349 if (!ps8622
->enabled
)
352 ret
= ps8622_set(ps8622
->client
, 0x01, 0xa7, brightness
);
357 static const struct backlight_ops ps8622_backlight_ops
= {
358 .update_status
= ps8622_backlight_update
,
361 static void ps8622_pre_enable(struct drm_bridge
*bridge
)
363 struct ps8622_bridge
*ps8622
= bridge_to_ps8622(bridge
);
369 gpiod_set_value(ps8622
->gpio_rst
, 0);
372 ret
= regulator_enable(ps8622
->v12
);
374 DRM_ERROR("fails to enable ps8622->v12");
377 if (drm_panel_prepare(ps8622
->panel
)) {
378 DRM_ERROR("failed to prepare panel\n");
382 gpiod_set_value(ps8622
->gpio_slp
, 1);
385 * T1 is the range of time that it takes for the power to rise after we
386 * enable the lcd/ps8622 fet. T2 is the range of time in which the
387 * data sheet specifies we should deassert the reset pin.
389 * If it takes T1.max for the power to rise, we need to wait atleast
390 * T2.min before deasserting the reset pin. If it takes T1.min for the
391 * power to rise, we need to wait at most T2.max before deasserting the
394 usleep_range(PS8622_RST_HIGH_T2_MIN_US
+ PS8622_POWER_RISE_T1_MAX_US
,
395 PS8622_RST_HIGH_T2_MAX_US
+ PS8622_POWER_RISE_T1_MIN_US
);
397 gpiod_set_value(ps8622
->gpio_rst
, 1);
399 /* wait 20ms after RST high */
400 usleep_range(20000, 30000);
402 ret
= ps8622_send_config(ps8622
);
404 DRM_ERROR("Failed to send config to bridge (%d)\n", ret
);
408 ps8622
->enabled
= true;
411 static void ps8622_enable(struct drm_bridge
*bridge
)
413 struct ps8622_bridge
*ps8622
= bridge_to_ps8622(bridge
);
415 if (drm_panel_enable(ps8622
->panel
)) {
416 DRM_ERROR("failed to enable panel\n");
421 static void ps8622_disable(struct drm_bridge
*bridge
)
423 struct ps8622_bridge
*ps8622
= bridge_to_ps8622(bridge
);
425 if (drm_panel_disable(ps8622
->panel
)) {
426 DRM_ERROR("failed to disable panel\n");
429 msleep(PS8622_PWMO_END_T12_MS
);
432 static void ps8622_post_disable(struct drm_bridge
*bridge
)
434 struct ps8622_bridge
*ps8622
= bridge_to_ps8622(bridge
);
436 if (!ps8622
->enabled
)
439 ps8622
->enabled
= false;
442 * This doesn't matter if the regulators are turned off, but something
443 * else might keep them on. In that case, we want to assert the slp gpio
446 gpiod_set_value(ps8622
->gpio_slp
, 0);
448 if (drm_panel_unprepare(ps8622
->panel
)) {
449 DRM_ERROR("failed to unprepare panel\n");
454 regulator_disable(ps8622
->v12
);
457 * Sleep for at least the amount of time that it takes the power rail to
458 * fall to prevent asserting the rst gpio from doing anything.
460 usleep_range(PS8622_POWER_FALL_T16_MAX_US
,
461 2 * PS8622_POWER_FALL_T16_MAX_US
);
462 gpiod_set_value(ps8622
->gpio_rst
, 0);
464 msleep(PS8622_POWER_OFF_T17_MS
);
467 static int ps8622_get_modes(struct drm_connector
*connector
)
469 struct ps8622_bridge
*ps8622
;
471 ps8622
= connector_to_ps8622(connector
);
473 return drm_panel_get_modes(ps8622
->panel
);
476 static const struct drm_connector_helper_funcs ps8622_connector_helper_funcs
= {
477 .get_modes
= ps8622_get_modes
,
480 static enum drm_connector_status
ps8622_detect(struct drm_connector
*connector
,
483 return connector_status_connected
;
486 static const struct drm_connector_funcs ps8622_connector_funcs
= {
487 .dpms
= drm_atomic_helper_connector_dpms
,
488 .fill_modes
= drm_helper_probe_single_connector_modes
,
489 .detect
= ps8622_detect
,
490 .destroy
= drm_connector_cleanup
,
491 .reset
= drm_atomic_helper_connector_reset
,
492 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
493 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
496 static int ps8622_attach(struct drm_bridge
*bridge
)
498 struct ps8622_bridge
*ps8622
= bridge_to_ps8622(bridge
);
501 if (!bridge
->encoder
) {
502 DRM_ERROR("Parent encoder object not found");
506 ps8622
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
507 ret
= drm_connector_init(bridge
->dev
, &ps8622
->connector
,
508 &ps8622_connector_funcs
, DRM_MODE_CONNECTOR_LVDS
);
510 DRM_ERROR("Failed to initialize connector with drm\n");
513 drm_connector_helper_add(&ps8622
->connector
,
514 &ps8622_connector_helper_funcs
);
515 drm_connector_register(&ps8622
->connector
);
516 drm_mode_connector_attach_encoder(&ps8622
->connector
,
520 drm_panel_attach(ps8622
->panel
, &ps8622
->connector
);
522 drm_helper_hpd_irq_event(ps8622
->connector
.dev
);
527 static const struct drm_bridge_funcs ps8622_bridge_funcs
= {
528 .pre_enable
= ps8622_pre_enable
,
529 .enable
= ps8622_enable
,
530 .disable
= ps8622_disable
,
531 .post_disable
= ps8622_post_disable
,
532 .attach
= ps8622_attach
,
535 static const struct of_device_id ps8622_devices
[] = {
536 {.compatible
= "parade,ps8622",},
537 {.compatible
= "parade,ps8625",},
540 MODULE_DEVICE_TABLE(of
, ps8622_devices
);
542 static int ps8622_probe(struct i2c_client
*client
,
543 const struct i2c_device_id
*id
)
545 struct device
*dev
= &client
->dev
;
546 struct device_node
*endpoint
, *panel_node
;
547 struct ps8622_bridge
*ps8622
;
550 ps8622
= devm_kzalloc(dev
, sizeof(*ps8622
), GFP_KERNEL
);
554 endpoint
= of_graph_get_next_endpoint(dev
->of_node
, NULL
);
556 panel_node
= of_graph_get_remote_port_parent(endpoint
);
558 ps8622
->panel
= of_drm_find_panel(panel_node
);
559 of_node_put(panel_node
);
561 return -EPROBE_DEFER
;
565 ps8622
->client
= client
;
567 ps8622
->v12
= devm_regulator_get(dev
, "vdd12");
568 if (IS_ERR(ps8622
->v12
)) {
569 dev_info(dev
, "no 1.2v regulator found for PS8622\n");
573 ps8622
->gpio_slp
= devm_gpiod_get(dev
, "sleep", GPIOD_OUT_HIGH
);
574 if (IS_ERR(ps8622
->gpio_slp
)) {
575 ret
= PTR_ERR(ps8622
->gpio_slp
);
576 dev_err(dev
, "cannot get gpio_slp %d\n", ret
);
581 * Assert the reset pin high to avoid the bridge being
582 * initialized prematurely
584 ps8622
->gpio_rst
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
585 if (IS_ERR(ps8622
->gpio_rst
)) {
586 ret
= PTR_ERR(ps8622
->gpio_rst
);
587 dev_err(dev
, "cannot get gpio_rst %d\n", ret
);
591 ps8622
->max_lane_count
= id
->driver_data
;
593 if (of_property_read_u32(dev
->of_node
, "lane-count",
594 &ps8622
->lane_count
)) {
595 ps8622
->lane_count
= ps8622
->max_lane_count
;
596 } else if (ps8622
->lane_count
> ps8622
->max_lane_count
) {
597 dev_info(dev
, "lane-count property is too high,"
598 "using max_lane_count\n");
599 ps8622
->lane_count
= ps8622
->max_lane_count
;
602 if (!of_find_property(dev
->of_node
, "use-external-pwm", NULL
)) {
603 ps8622
->bl
= backlight_device_register("ps8622-backlight",
604 dev
, ps8622
, &ps8622_backlight_ops
,
606 if (IS_ERR(ps8622
->bl
)) {
607 DRM_ERROR("failed to register backlight\n");
608 ret
= PTR_ERR(ps8622
->bl
);
612 ps8622
->bl
->props
.max_brightness
= PS8622_MAX_BRIGHTNESS
;
613 ps8622
->bl
->props
.brightness
= PS8622_MAX_BRIGHTNESS
;
616 ps8622
->bridge
.funcs
= &ps8622_bridge_funcs
;
617 ps8622
->bridge
.of_node
= dev
->of_node
;
618 ret
= drm_bridge_add(&ps8622
->bridge
);
620 DRM_ERROR("Failed to add bridge\n");
624 i2c_set_clientdata(client
, ps8622
);
629 static int ps8622_remove(struct i2c_client
*client
)
631 struct ps8622_bridge
*ps8622
= i2c_get_clientdata(client
);
633 backlight_device_unregister(ps8622
->bl
);
634 drm_bridge_remove(&ps8622
->bridge
);
639 static const struct i2c_device_id ps8622_i2c_table
[] = {
640 /* Device type, max_lane_count */
645 MODULE_DEVICE_TABLE(i2c
, ps8622_i2c_table
);
647 static struct i2c_driver ps8622_driver
= {
648 .id_table
= ps8622_i2c_table
,
649 .probe
= ps8622_probe
,
650 .remove
= ps8622_remove
,
653 .of_match_table
= ps8622_devices
,
656 module_i2c_driver(ps8622_driver
);
658 MODULE_AUTHOR("Vincent Palatin <vpalatin@chromium.org>");
659 MODULE_DESCRIPTION("Parade ps8622/ps8625 eDP-LVDS converter driver");
660 MODULE_LICENSE("GPL v2");