1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <delay.h>
4 #include <device/i2c_simple.h>
5 #include <edid.h>
6 #include <console/console.h>
7 #include <timer.h>
8 #include <dp_aux.h>
9 #include "ps8640.h"
10
ps8640_get_edid(uint8_t bus,uint8_t chip,struct edid * out)11 int ps8640_get_edid(uint8_t bus, uint8_t chip, struct edid *out)
12 {
13 int ret;
14 u8 edid[EDID_LENGTH * 2];
15 int edid_size = EDID_LENGTH;
16
17 i2c_writeb(bus, chip + 2, PAGE2_I2C_BYPASS,
18 EDID_I2C_ADDR | I2C_BYPASS_EN);
19 ret = i2c_read_bytes(bus, EDID_I2C_ADDR, 0, edid, EDID_LENGTH);
20
21 if (ret != 0) {
22 printk(BIOS_INFO, "Failed to read EDID.\n");
23 return -1;
24 }
25
26 /* check if edid have extension flag, and read additional EDID data */
27 if (edid[EDID_EXTENSION_FLAG]) {
28 edid_size += EDID_LENGTH;
29 ret = i2c_read_bytes(bus, EDID_I2C_ADDR, EDID_LENGTH,
30 &edid[EDID_LENGTH], EDID_LENGTH);
31 if (ret != 0) {
32 printk(BIOS_INFO, "Failed to read EDID ext block.\n");
33 return -1;
34 }
35 }
36
37 if (decode_edid(edid, edid_size, out) != EDID_CONFORMANT) {
38 printk(BIOS_INFO, "Failed to decode EDID.\n");
39 return -1;
40 }
41
42 return 0;
43 }
44
ps8640_init(uint8_t bus,uint8_t chip)45 int ps8640_init(uint8_t bus, uint8_t chip)
46 {
47 u8 set_vdo_done;
48 struct stopwatch sw;
49
50 mdelay(200);
51 stopwatch_init_msecs_expire(&sw, 200);
52
53 while (true) {
54 i2c_readb(bus, chip + 2, PAGE2_GPIO_H, &set_vdo_done);
55 if ((set_vdo_done & PS_GPIO9) == PS_GPIO9)
56 break;
57 if (stopwatch_expired(&sw)) {
58 printk(BIOS_INFO, "Failed to init ps8640.\n");
59 return -1;
60 }
61
62 mdelay(20);
63 }
64
65 mdelay(50);
66
67 /*
68 * The Manufacturer Command Set (MCS) is a device dependent interface
69 * intended for factory programming of the display module default
70 * parameters. Once the display module is configured, the MCS shall be
71 * disabled by the manufacturer. Once disabled, all MCS commands are
72 * ignored by the display interface.
73 */
74 i2c_write_field(bus, chip + 2, PAGE2_MCS_EN, 0x0, MCS_EN_MASK,
75 MCS_EN_SHIFT);
76 i2c_writeb(bus, chip + 3, PAGE3_SET_ADD, VDO_CTL_ADD);
77 i2c_writeb(bus, chip + 3, PAGE3_SET_VAL, VDO_DIS);
78 i2c_writeb(bus, chip + 3, PAGE3_SET_ADD, VDO_CTL_ADD);
79 i2c_writeb(bus, chip + 3, PAGE3_SET_VAL, VDO_EN);
80
81 return 0;
82 }
83
ps8640_bridge_aux_request(uint8_t bus,uint8_t chip,unsigned int target_reg,unsigned int total_size,enum aux_request request,uint8_t * data)84 static enum cb_err ps8640_bridge_aux_request(uint8_t bus,
85 uint8_t chip,
86 unsigned int target_reg,
87 unsigned int total_size,
88 enum aux_request request,
89 uint8_t *data)
90 {
91 int i;
92 uint32_t length;
93 uint8_t buf;
94 uint8_t reg;
95 int ret;
96
97 if (target_reg & ~SWAUX_ADDR_MASK)
98 return CB_ERR;
99
100 while (total_size) {
101 length = MIN(total_size, DP_AUX_MAX_PAYLOAD_BYTES);
102 total_size -= length;
103
104 ret = i2c_writeb(bus, chip, PAGE0_AUXCH_CFG3, AUXCH_CFG3_RESET);
105 if (ret)
106 return CB_ERR;
107
108 enum i2c_over_aux cmd = dp_get_aux_cmd(request, total_size);
109 if (i2c_writeb(bus, chip, PAGE0_SWAUX_ADDR_23_16,
110 (target_reg >> 16) | (cmd << 4)) ||
111 i2c_writeb(bus, chip, PAGE0_SWAUX_ADDR_15_8, target_reg >> 8) ||
112 i2c_writeb(bus, chip, PAGE0_SWAUX_ADDR_7_0, target_reg)) {
113 return CB_ERR;
114 }
115
116 if (dp_aux_request_is_write(request)) {
117 reg = PAGE0_SWAUX_WDATA;
118 for (i = 0; i < length; i++) {
119 ret = i2c_writeb(bus, chip, reg++, *data++);
120 if (ret)
121 return CB_ERR;
122 }
123 } else {
124 if (length == 0)
125 i2c_writeb(bus, chip, PAGE0_SWAUX_LENGTH, SWAUX_NO_PAYLOAD);
126 else
127 i2c_writeb(bus, chip, PAGE0_SWAUX_LENGTH, length - 1);
128 }
129
130 ret = i2c_writeb(bus, chip, PAGE0_SWAUX_CTRL, SWAUX_SEND);
131 if (ret)
132 return CB_ERR;
133
134 if (!wait_ms(100, !i2c_readb(bus, chip, PAGE0_SWAUX_CTRL, &buf) &&
135 !(buf & SWAUX_SEND)))
136 return CB_ERR;
137
138 if (i2c_readb(bus, chip, PAGE0_SWAUX_STATUS, &buf))
139 return CB_ERR;
140
141 switch (buf & SWAUX_STATUS_MASK) {
142 case SWAUX_STATUS_NACK:
143 case SWAUX_STATUS_I2C_NACK:
144 case SWAUX_STATUS_INVALID:
145 case SWAUX_STATUS_TIMEOUT:
146 return CB_ERR;
147 case SWAUX_STATUS_ACKM:
148 length = buf & SWAUX_M_MASK;
149 break;
150 }
151
152 if (length && !dp_aux_request_is_write(request)) {
153 reg = PAGE0_SWAUX_RDATA;
154 for (i = 0; i < length; i++) {
155 if (i2c_readb(bus, chip, reg++, &buf))
156 return CB_ERR;
157 *data++ = buf;
158 }
159 }
160 }
161
162 return CB_SUCCESS;
163 }
164
ps8640_backlight_enable(uint8_t bus,uint8_t chip)165 void ps8640_backlight_enable(uint8_t bus, uint8_t chip)
166 {
167 uint8_t val;
168
169 val = DP_BACKLIGHT_CONTROL_MODE_DPCD;
170 ps8640_bridge_aux_request(bus, chip, DP_BACKLIGHT_MODE_SET, 1,
171 DPCD_WRITE, &val);
172
173 val = 0xff;
174 ps8640_bridge_aux_request(bus, chip, DP_BACKLIGHT_BRIGHTNESS_MSB, 1,
175 DPCD_WRITE, &val);
176
177 val = DP_BACKLIGHT_ENABLE;
178 ps8640_bridge_aux_request(bus, chip, DP_DISPLAY_CONTROL_REGISTER, 1,
179 DPCD_WRITE, &val);
180 }
181