1330b48bdSThierry Reding /*
2330b48bdSThierry Reding  * Parade PS8622 eDP/LVDS bridge driver
3330b48bdSThierry Reding  *
4330b48bdSThierry Reding  * Copyright (C) 2014 Google, Inc.
5330b48bdSThierry Reding  *
6330b48bdSThierry Reding  * This software is licensed under the terms of the GNU General Public
7330b48bdSThierry Reding  * License version 2, as published by the Free Software Foundation, and
8330b48bdSThierry Reding  * may be copied, distributed, and modified under those terms.
9330b48bdSThierry Reding  *
10330b48bdSThierry Reding  * This program is distributed in the hope that it will be useful,
11330b48bdSThierry Reding  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12330b48bdSThierry Reding  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13330b48bdSThierry Reding  * GNU General Public License for more details.
14330b48bdSThierry Reding  */
15330b48bdSThierry Reding 
16330b48bdSThierry Reding #include <linux/backlight.h>
17330b48bdSThierry Reding #include <linux/delay.h>
18330b48bdSThierry Reding #include <linux/err.h>
19330b48bdSThierry Reding #include <linux/fb.h>
20330b48bdSThierry Reding #include <linux/gpio.h>
21330b48bdSThierry Reding #include <linux/gpio/consumer.h>
22330b48bdSThierry Reding #include <linux/i2c.h>
23330b48bdSThierry Reding #include <linux/module.h>
24330b48bdSThierry Reding #include <linux/of.h>
25330b48bdSThierry Reding #include <linux/of_device.h>
26330b48bdSThierry Reding #include <linux/of_graph.h>
27330b48bdSThierry Reding #include <linux/pm.h>
28330b48bdSThierry Reding #include <linux/regulator/consumer.h>
29330b48bdSThierry Reding 
30330b48bdSThierry Reding #include <drm/drm_panel.h>
31330b48bdSThierry Reding 
32330b48bdSThierry Reding #include "drmP.h"
33330b48bdSThierry Reding #include "drm_crtc.h"
34330b48bdSThierry Reding #include "drm_crtc_helper.h"
35330b48bdSThierry Reding #include "drm_atomic_helper.h"
36330b48bdSThierry Reding 
37330b48bdSThierry Reding /* Brightness scale on the Parade chip */
38330b48bdSThierry Reding #define PS8622_MAX_BRIGHTNESS 0xff
39330b48bdSThierry Reding 
40330b48bdSThierry Reding /* Timings taken from the version 1.7 datasheet for the PS8622/PS8625 */
41330b48bdSThierry Reding #define PS8622_POWER_RISE_T1_MIN_US 10
42330b48bdSThierry Reding #define PS8622_POWER_RISE_T1_MAX_US 10000
43330b48bdSThierry Reding #define PS8622_RST_HIGH_T2_MIN_US 3000
44330b48bdSThierry Reding #define PS8622_RST_HIGH_T2_MAX_US 30000
45330b48bdSThierry Reding #define PS8622_PWMO_END_T12_MS 200
46330b48bdSThierry Reding #define PS8622_POWER_FALL_T16_MAX_US 10000
47330b48bdSThierry Reding #define PS8622_POWER_OFF_T17_MS 500
48330b48bdSThierry Reding 
49330b48bdSThierry Reding #if ((PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US) > \
50330b48bdSThierry Reding 	(PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US))
51330b48bdSThierry Reding #error "T2.min + T1.max must be less than T2.max + T1.min"
52330b48bdSThierry Reding #endif
53330b48bdSThierry Reding 
54330b48bdSThierry Reding struct ps8622_bridge {
55330b48bdSThierry Reding 	struct drm_connector connector;
56330b48bdSThierry Reding 	struct i2c_client *client;
57330b48bdSThierry Reding 	struct drm_bridge bridge;
58330b48bdSThierry Reding 	struct drm_panel *panel;
59330b48bdSThierry Reding 	struct regulator *v12;
60330b48bdSThierry Reding 	struct backlight_device *bl;
61330b48bdSThierry Reding 
62330b48bdSThierry Reding 	struct gpio_desc *gpio_slp;
63330b48bdSThierry Reding 	struct gpio_desc *gpio_rst;
64330b48bdSThierry Reding 
65330b48bdSThierry Reding 	u32 max_lane_count;
66330b48bdSThierry Reding 	u32 lane_count;
67330b48bdSThierry Reding 
68330b48bdSThierry Reding 	bool enabled;
69330b48bdSThierry Reding };
70330b48bdSThierry Reding 
71330b48bdSThierry Reding static inline struct ps8622_bridge *
72330b48bdSThierry Reding 		bridge_to_ps8622(struct drm_bridge *bridge)
73330b48bdSThierry Reding {
74330b48bdSThierry Reding 	return container_of(bridge, struct ps8622_bridge, bridge);
75330b48bdSThierry Reding }
76330b48bdSThierry Reding 
77330b48bdSThierry Reding static inline struct ps8622_bridge *
78330b48bdSThierry Reding 		connector_to_ps8622(struct drm_connector *connector)
79330b48bdSThierry Reding {
80330b48bdSThierry Reding 	return container_of(connector, struct ps8622_bridge, connector);
81330b48bdSThierry Reding }
82330b48bdSThierry Reding 
83330b48bdSThierry Reding static int ps8622_set(struct i2c_client *client, u8 page, u8 reg, u8 val)
84330b48bdSThierry Reding {
85330b48bdSThierry Reding 	int ret;
86330b48bdSThierry Reding 	struct i2c_adapter *adap = client->adapter;
87330b48bdSThierry Reding 	struct i2c_msg msg;
88330b48bdSThierry Reding 	u8 data[] = {reg, val};
89330b48bdSThierry Reding 
90330b48bdSThierry Reding 	msg.addr = client->addr + page;
91330b48bdSThierry Reding 	msg.flags = 0;
92330b48bdSThierry Reding 	msg.len = sizeof(data);
93330b48bdSThierry Reding 	msg.buf = data;
94330b48bdSThierry Reding 
95330b48bdSThierry Reding 	ret = i2c_transfer(adap, &msg, 1);
96330b48bdSThierry Reding 	if (ret != 1)
97330b48bdSThierry Reding 		pr_warn("PS8622 I2C write (0x%02x,0x%02x,0x%02x) failed: %d\n",
98330b48bdSThierry Reding 			client->addr + page, reg, val, ret);
99330b48bdSThierry Reding 	return !(ret == 1);
100330b48bdSThierry Reding }
101330b48bdSThierry Reding 
102330b48bdSThierry Reding static int ps8622_send_config(struct ps8622_bridge *ps8622)
103330b48bdSThierry Reding {
104330b48bdSThierry Reding 	struct i2c_client *cl = ps8622->client;
105330b48bdSThierry Reding 	int err = 0;
106330b48bdSThierry Reding 
107330b48bdSThierry Reding 	/* HPD low */
108330b48bdSThierry Reding 	err = ps8622_set(cl, 0x02, 0xa1, 0x01);
109330b48bdSThierry Reding 	if (err)
110330b48bdSThierry Reding 		goto error;
111330b48bdSThierry Reding 
112330b48bdSThierry Reding 	/* SW setting: [1:0] SW output 1.2V voltage is lower to 96% */
113330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x14, 0x01);
114330b48bdSThierry Reding 	if (err)
115330b48bdSThierry Reding 		goto error;
116330b48bdSThierry Reding 
117330b48bdSThierry Reding 	/* RCO SS setting: [5:4] = b01 0.5%, b10 1%, b11 1.5% */
118330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0xe3, 0x20);
119330b48bdSThierry Reding 	if (err)
120330b48bdSThierry Reding 		goto error;
121330b48bdSThierry Reding 
122330b48bdSThierry Reding 	/* [7] RCO SS enable */
123330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0xe2, 0x80);
124330b48bdSThierry Reding 	if (err)
125330b48bdSThierry Reding 		goto error;
126330b48bdSThierry Reding 
127330b48bdSThierry Reding 	/* RPHY Setting
128330b48bdSThierry Reding 	 * [3:2] CDR tune wait cycle before measure for fine tune
129330b48bdSThierry Reding 	 * b00: 1us b01: 0.5us b10:2us, b11: 4us
130330b48bdSThierry Reding 	 */
131330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x8a, 0x0c);
132330b48bdSThierry Reding 	if (err)
133330b48bdSThierry Reding 		goto error;
134330b48bdSThierry Reding 
135330b48bdSThierry Reding 	/* [3] RFD always on */
136330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x89, 0x08);
137330b48bdSThierry Reding 	if (err)
138330b48bdSThierry Reding 		goto error;
139330b48bdSThierry Reding 
140330b48bdSThierry Reding 	/* CTN lock in/out: 20000ppm/80000ppm. Lock out 2 times. */
141330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x71, 0x2d);
142330b48bdSThierry Reding 	if (err)
143330b48bdSThierry Reding 		goto error;
144330b48bdSThierry Reding 
145330b48bdSThierry Reding 	/* 2.7G CDR settings: NOF=40LSB for HBR CDR  setting */
146330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x7d, 0x07);
147330b48bdSThierry Reding 	if (err)
148330b48bdSThierry Reding 		goto error;
149330b48bdSThierry Reding 
150330b48bdSThierry Reding 	/* [1:0] Fmin=+4bands */
151330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x7b, 0x00);
152330b48bdSThierry Reding 	if (err)
153330b48bdSThierry Reding 		goto error;
154330b48bdSThierry Reding 
155330b48bdSThierry Reding 	/* [7:5] DCO_FTRNG=+-40% */
156330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x7a, 0xfd);
157330b48bdSThierry Reding 	if (err)
158330b48bdSThierry Reding 		goto error;
159330b48bdSThierry Reding 
160330b48bdSThierry Reding 	/* 1.62G CDR settings: [5:2]NOF=64LSB [1:0]DCO scale is 2/5 */
161330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0xc0, 0x12);
162330b48bdSThierry Reding 	if (err)
163330b48bdSThierry Reding 		goto error;
164330b48bdSThierry Reding 
165330b48bdSThierry Reding 	/* Gitune=-37% */
166330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0xc1, 0x92);
167330b48bdSThierry Reding 	if (err)
168330b48bdSThierry Reding 		goto error;
169330b48bdSThierry Reding 
170330b48bdSThierry Reding 	/* Fbstep=100% */
171330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0xc2, 0x1c);
172330b48bdSThierry Reding 	if (err)
173330b48bdSThierry Reding 		goto error;
174330b48bdSThierry Reding 
175330b48bdSThierry Reding 	/* [7] LOS signal disable */
176330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x32, 0x80);
177330b48bdSThierry Reding 	if (err)
178330b48bdSThierry Reding 		goto error;
179330b48bdSThierry Reding 
180330b48bdSThierry Reding 	/* RPIO Setting: [7:4] LVDS driver bias current : 75% (250mV swing) */
181330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x00, 0xb0);
182330b48bdSThierry Reding 	if (err)
183330b48bdSThierry Reding 		goto error;
184330b48bdSThierry Reding 
185330b48bdSThierry Reding 	/* [7:6] Right-bar GPIO output strength is 8mA */
186330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x15, 0x40);
187330b48bdSThierry Reding 	if (err)
188330b48bdSThierry Reding 		goto error;
189330b48bdSThierry Reding 
190330b48bdSThierry Reding 	/* EQ Training State Machine Setting, RCO calibration start */
191330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x54, 0x10);
192330b48bdSThierry Reding 	if (err)
193330b48bdSThierry Reding 		goto error;
194330b48bdSThierry Reding 
195330b48bdSThierry Reding 	/* Logic, needs more than 10 I2C command */
196330b48bdSThierry Reding 	/* [4:0] MAX_LANE_COUNT set to max supported lanes */
197330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0x02, 0x80 | ps8622->max_lane_count);
198330b48bdSThierry Reding 	if (err)
199330b48bdSThierry Reding 		goto error;
200330b48bdSThierry Reding 
201330b48bdSThierry Reding 	/* [4:0] LANE_COUNT_SET set to chosen lane count */
202330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0x21, 0x80 | ps8622->lane_count);
203330b48bdSThierry Reding 	if (err)
204330b48bdSThierry Reding 		goto error;
205330b48bdSThierry Reding 
206330b48bdSThierry Reding 	err = ps8622_set(cl, 0x00, 0x52, 0x20);
207330b48bdSThierry Reding 	if (err)
208330b48bdSThierry Reding 		goto error;
209330b48bdSThierry Reding 
210330b48bdSThierry Reding 	/* HPD CP toggle enable */
211330b48bdSThierry Reding 	err = ps8622_set(cl, 0x00, 0xf1, 0x03);
212330b48bdSThierry Reding 	if (err)
213330b48bdSThierry Reding 		goto error;
214330b48bdSThierry Reding 
215330b48bdSThierry Reding 	err = ps8622_set(cl, 0x00, 0x62, 0x41);
216330b48bdSThierry Reding 	if (err)
217330b48bdSThierry Reding 		goto error;
218330b48bdSThierry Reding 
219330b48bdSThierry Reding 	/* Counter number, add 1ms counter delay */
220330b48bdSThierry Reding 	err = ps8622_set(cl, 0x00, 0xf6, 0x01);
221330b48bdSThierry Reding 	if (err)
222330b48bdSThierry Reding 		goto error;
223330b48bdSThierry Reding 
224330b48bdSThierry Reding 	/* [6]PWM function control by DPCD0040f[7], default is PWM block */
225330b48bdSThierry Reding 	err = ps8622_set(cl, 0x00, 0x77, 0x06);
226330b48bdSThierry Reding 	if (err)
227330b48bdSThierry Reding 		goto error;
228330b48bdSThierry Reding 
229330b48bdSThierry Reding 	/* 04h Adjust VTotal toleranceto fix the 30Hz no display issue */
230330b48bdSThierry Reding 	err = ps8622_set(cl, 0x00, 0x4c, 0x04);
231330b48bdSThierry Reding 	if (err)
232330b48bdSThierry Reding 		goto error;
233330b48bdSThierry Reding 
234330b48bdSThierry Reding 	/* DPCD00400='h00, Parade OUI ='h001cf8 */
235330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc0, 0x00);
236330b48bdSThierry Reding 	if (err)
237330b48bdSThierry Reding 		goto error;
238330b48bdSThierry Reding 
239330b48bdSThierry Reding 	/* DPCD00401='h1c */
240330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc1, 0x1c);
241330b48bdSThierry Reding 	if (err)
242330b48bdSThierry Reding 		goto error;
243330b48bdSThierry Reding 
244330b48bdSThierry Reding 	/* DPCD00402='hf8 */
245330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc2, 0xf8);
246330b48bdSThierry Reding 	if (err)
247330b48bdSThierry Reding 		goto error;
248330b48bdSThierry Reding 
249330b48bdSThierry Reding 	/* DPCD403~408 = ASCII code, D2SLV5='h4432534c5635 */
250330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc3, 0x44);
251330b48bdSThierry Reding 	if (err)
252330b48bdSThierry Reding 		goto error;
253330b48bdSThierry Reding 
254330b48bdSThierry Reding 	/* DPCD404 */
255330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc4, 0x32);
256330b48bdSThierry Reding 	if (err)
257330b48bdSThierry Reding 		goto error;
258330b48bdSThierry Reding 
259330b48bdSThierry Reding 	/* DPCD405 */
260330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc5, 0x53);
261330b48bdSThierry Reding 	if (err)
262330b48bdSThierry Reding 		goto error;
263330b48bdSThierry Reding 
264330b48bdSThierry Reding 	/* DPCD406 */
265330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc6, 0x4c);
266330b48bdSThierry Reding 	if (err)
267330b48bdSThierry Reding 		goto error;
268330b48bdSThierry Reding 
269330b48bdSThierry Reding 	/* DPCD407 */
270330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc7, 0x56);
271330b48bdSThierry Reding 	if (err)
272330b48bdSThierry Reding 		goto error;
273330b48bdSThierry Reding 
274330b48bdSThierry Reding 	/* DPCD408 */
275330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xc8, 0x35);
276330b48bdSThierry Reding 	if (err)
277330b48bdSThierry Reding 		goto error;
278330b48bdSThierry Reding 
279330b48bdSThierry Reding 	/* DPCD40A, Initial Code major revision '01' */
280330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xca, 0x01);
281330b48bdSThierry Reding 	if (err)
282330b48bdSThierry Reding 		goto error;
283330b48bdSThierry Reding 
284330b48bdSThierry Reding 	/* DPCD40B, Initial Code minor revision '05' */
285330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xcb, 0x05);
286330b48bdSThierry Reding 	if (err)
287330b48bdSThierry Reding 		goto error;
288330b48bdSThierry Reding 
289330b48bdSThierry Reding 
290330b48bdSThierry Reding 	if (ps8622->bl) {
291330b48bdSThierry Reding 		/* DPCD720, internal PWM */
292330b48bdSThierry Reding 		err = ps8622_set(cl, 0x01, 0xa5, 0xa0);
293330b48bdSThierry Reding 		if (err)
294330b48bdSThierry Reding 			goto error;
295330b48bdSThierry Reding 
296330b48bdSThierry Reding 		/* FFh for 100% brightness, 0h for 0% brightness */
297330b48bdSThierry Reding 		err = ps8622_set(cl, 0x01, 0xa7,
298330b48bdSThierry Reding 				ps8622->bl->props.brightness);
299330b48bdSThierry Reding 		if (err)
300330b48bdSThierry Reding 			goto error;
301330b48bdSThierry Reding 	} else {
302330b48bdSThierry Reding 		/* DPCD720, external PWM */
303330b48bdSThierry Reding 		err = ps8622_set(cl, 0x01, 0xa5, 0x80);
304330b48bdSThierry Reding 		if (err)
305330b48bdSThierry Reding 			goto error;
306330b48bdSThierry Reding 	}
307330b48bdSThierry Reding 
308330b48bdSThierry Reding 	/* Set LVDS output as 6bit-VESA mapping, single LVDS channel */
309330b48bdSThierry Reding 	err = ps8622_set(cl, 0x01, 0xcc, 0x13);
310330b48bdSThierry Reding 	if (err)
311330b48bdSThierry Reding 		goto error;
312330b48bdSThierry Reding 
313330b48bdSThierry Reding 	/* Enable SSC set by register */
314330b48bdSThierry Reding 	err = ps8622_set(cl, 0x02, 0xb1, 0x20);
315330b48bdSThierry Reding 	if (err)
316330b48bdSThierry Reding 		goto error;
317330b48bdSThierry Reding 
318330b48bdSThierry Reding 	/* Set SSC enabled and +/-1% central spreading */
319330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x10, 0x16);
320330b48bdSThierry Reding 	if (err)
321330b48bdSThierry Reding 		goto error;
322330b48bdSThierry Reding 
323330b48bdSThierry Reding 	/* Logic end */
324330b48bdSThierry Reding 	/* MPU Clock source: LC => RCO */
325330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x59, 0x60);
326330b48bdSThierry Reding 	if (err)
327330b48bdSThierry Reding 		goto error;
328330b48bdSThierry Reding 
329330b48bdSThierry Reding 	/* LC -> RCO */
330330b48bdSThierry Reding 	err = ps8622_set(cl, 0x04, 0x54, 0x14);
331330b48bdSThierry Reding 	if (err)
332330b48bdSThierry Reding 		goto error;
333330b48bdSThierry Reding 
334330b48bdSThierry Reding 	/* HPD high */
335330b48bdSThierry Reding 	err = ps8622_set(cl, 0x02, 0xa1, 0x91);
336330b48bdSThierry Reding 
337330b48bdSThierry Reding error:
338330b48bdSThierry Reding 	return err ? -EIO : 0;
339330b48bdSThierry Reding }
340330b48bdSThierry Reding 
341330b48bdSThierry Reding static int ps8622_backlight_update(struct backlight_device *bl)
342330b48bdSThierry Reding {
343330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = dev_get_drvdata(&bl->dev);
344330b48bdSThierry Reding 	int ret, brightness = bl->props.brightness;
345330b48bdSThierry Reding 
346330b48bdSThierry Reding 	if (bl->props.power != FB_BLANK_UNBLANK ||
347330b48bdSThierry Reding 	    bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
348330b48bdSThierry Reding 		brightness = 0;
349330b48bdSThierry Reding 
350330b48bdSThierry Reding 	if (!ps8622->enabled)
351330b48bdSThierry Reding 		return -EINVAL;
352330b48bdSThierry Reding 
353330b48bdSThierry Reding 	ret = ps8622_set(ps8622->client, 0x01, 0xa7, brightness);
354330b48bdSThierry Reding 
355330b48bdSThierry Reding 	return ret;
356330b48bdSThierry Reding }
357330b48bdSThierry Reding 
358330b48bdSThierry Reding static const struct backlight_ops ps8622_backlight_ops = {
359330b48bdSThierry Reding 	.update_status	= ps8622_backlight_update,
360330b48bdSThierry Reding };
361330b48bdSThierry Reding 
362330b48bdSThierry Reding static void ps8622_pre_enable(struct drm_bridge *bridge)
363330b48bdSThierry Reding {
364330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
365330b48bdSThierry Reding 	int ret;
366330b48bdSThierry Reding 
367330b48bdSThierry Reding 	if (ps8622->enabled)
368330b48bdSThierry Reding 		return;
369330b48bdSThierry Reding 
370330b48bdSThierry Reding 	gpiod_set_value(ps8622->gpio_rst, 0);
371330b48bdSThierry Reding 
372330b48bdSThierry Reding 	if (ps8622->v12) {
373330b48bdSThierry Reding 		ret = regulator_enable(ps8622->v12);
374330b48bdSThierry Reding 		if (ret)
375330b48bdSThierry Reding 			DRM_ERROR("fails to enable ps8622->v12");
376330b48bdSThierry Reding 	}
377330b48bdSThierry Reding 
378330b48bdSThierry Reding 	if (drm_panel_prepare(ps8622->panel)) {
379330b48bdSThierry Reding 		DRM_ERROR("failed to prepare panel\n");
380330b48bdSThierry Reding 		return;
381330b48bdSThierry Reding 	}
382330b48bdSThierry Reding 
383330b48bdSThierry Reding 	gpiod_set_value(ps8622->gpio_slp, 1);
384330b48bdSThierry Reding 
385330b48bdSThierry Reding 	/*
386330b48bdSThierry Reding 	 * T1 is the range of time that it takes for the power to rise after we
387330b48bdSThierry Reding 	 * enable the lcd/ps8622 fet. T2 is the range of time in which the
388330b48bdSThierry Reding 	 * data sheet specifies we should deassert the reset pin.
389330b48bdSThierry Reding 	 *
390330b48bdSThierry Reding 	 * If it takes T1.max for the power to rise, we need to wait atleast
391330b48bdSThierry Reding 	 * T2.min before deasserting the reset pin. If it takes T1.min for the
392330b48bdSThierry Reding 	 * power to rise, we need to wait at most T2.max before deasserting the
393330b48bdSThierry Reding 	 * reset pin.
394330b48bdSThierry Reding 	 */
395330b48bdSThierry Reding 	usleep_range(PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US,
396330b48bdSThierry Reding 		     PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US);
397330b48bdSThierry Reding 
398330b48bdSThierry Reding 	gpiod_set_value(ps8622->gpio_rst, 1);
399330b48bdSThierry Reding 
400330b48bdSThierry Reding 	/* wait 20ms after RST high */
401330b48bdSThierry Reding 	usleep_range(20000, 30000);
402330b48bdSThierry Reding 
403330b48bdSThierry Reding 	ret = ps8622_send_config(ps8622);
404330b48bdSThierry Reding 	if (ret) {
405330b48bdSThierry Reding 		DRM_ERROR("Failed to send config to bridge (%d)\n", ret);
406330b48bdSThierry Reding 		return;
407330b48bdSThierry Reding 	}
408330b48bdSThierry Reding 
409330b48bdSThierry Reding 	ps8622->enabled = true;
410330b48bdSThierry Reding }
411330b48bdSThierry Reding 
412330b48bdSThierry Reding static void ps8622_enable(struct drm_bridge *bridge)
413330b48bdSThierry Reding {
414330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
415330b48bdSThierry Reding 
416330b48bdSThierry Reding 	if (drm_panel_enable(ps8622->panel)) {
417330b48bdSThierry Reding 		DRM_ERROR("failed to enable panel\n");
418330b48bdSThierry Reding 		return;
419330b48bdSThierry Reding 	}
420330b48bdSThierry Reding }
421330b48bdSThierry Reding 
422330b48bdSThierry Reding static void ps8622_disable(struct drm_bridge *bridge)
423330b48bdSThierry Reding {
424330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
425330b48bdSThierry Reding 
426330b48bdSThierry Reding 	if (drm_panel_disable(ps8622->panel)) {
427330b48bdSThierry Reding 		DRM_ERROR("failed to disable panel\n");
428330b48bdSThierry Reding 		return;
429330b48bdSThierry Reding 	}
430330b48bdSThierry Reding 	msleep(PS8622_PWMO_END_T12_MS);
431330b48bdSThierry Reding }
432330b48bdSThierry Reding 
433330b48bdSThierry Reding static void ps8622_post_disable(struct drm_bridge *bridge)
434330b48bdSThierry Reding {
435330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
436330b48bdSThierry Reding 
437330b48bdSThierry Reding 	if (!ps8622->enabled)
438330b48bdSThierry Reding 		return;
439330b48bdSThierry Reding 
440330b48bdSThierry Reding 	ps8622->enabled = false;
441330b48bdSThierry Reding 
442330b48bdSThierry Reding 	/*
443330b48bdSThierry Reding 	 * This doesn't matter if the regulators are turned off, but something
444330b48bdSThierry Reding 	 * else might keep them on. In that case, we want to assert the slp gpio
445330b48bdSThierry Reding 	 * to lower power.
446330b48bdSThierry Reding 	 */
447330b48bdSThierry Reding 	gpiod_set_value(ps8622->gpio_slp, 0);
448330b48bdSThierry Reding 
449330b48bdSThierry Reding 	if (drm_panel_unprepare(ps8622->panel)) {
450330b48bdSThierry Reding 		DRM_ERROR("failed to unprepare panel\n");
451330b48bdSThierry Reding 		return;
452330b48bdSThierry Reding 	}
453330b48bdSThierry Reding 
454330b48bdSThierry Reding 	if (ps8622->v12)
455330b48bdSThierry Reding 		regulator_disable(ps8622->v12);
456330b48bdSThierry Reding 
457330b48bdSThierry Reding 	/*
458330b48bdSThierry Reding 	 * Sleep for at least the amount of time that it takes the power rail to
459330b48bdSThierry Reding 	 * fall to prevent asserting the rst gpio from doing anything.
460330b48bdSThierry Reding 	 */
461330b48bdSThierry Reding 	usleep_range(PS8622_POWER_FALL_T16_MAX_US,
462330b48bdSThierry Reding 		     2 * PS8622_POWER_FALL_T16_MAX_US);
463330b48bdSThierry Reding 	gpiod_set_value(ps8622->gpio_rst, 0);
464330b48bdSThierry Reding 
465330b48bdSThierry Reding 	msleep(PS8622_POWER_OFF_T17_MS);
466330b48bdSThierry Reding }
467330b48bdSThierry Reding 
468330b48bdSThierry Reding static int ps8622_get_modes(struct drm_connector *connector)
469330b48bdSThierry Reding {
470330b48bdSThierry Reding 	struct ps8622_bridge *ps8622;
471330b48bdSThierry Reding 
472330b48bdSThierry Reding 	ps8622 = connector_to_ps8622(connector);
473330b48bdSThierry Reding 
474330b48bdSThierry Reding 	return drm_panel_get_modes(ps8622->panel);
475330b48bdSThierry Reding }
476330b48bdSThierry Reding 
477330b48bdSThierry Reding static struct drm_encoder *ps8622_best_encoder(struct drm_connector *connector)
478330b48bdSThierry Reding {
479330b48bdSThierry Reding 	struct ps8622_bridge *ps8622;
480330b48bdSThierry Reding 
481330b48bdSThierry Reding 	ps8622 = connector_to_ps8622(connector);
482330b48bdSThierry Reding 
483330b48bdSThierry Reding 	return ps8622->bridge.encoder;
484330b48bdSThierry Reding }
485330b48bdSThierry Reding 
486330b48bdSThierry Reding static const struct drm_connector_helper_funcs ps8622_connector_helper_funcs = {
487330b48bdSThierry Reding 	.get_modes = ps8622_get_modes,
488330b48bdSThierry Reding 	.best_encoder = ps8622_best_encoder,
489330b48bdSThierry Reding };
490330b48bdSThierry Reding 
491330b48bdSThierry Reding static enum drm_connector_status ps8622_detect(struct drm_connector *connector,
492330b48bdSThierry Reding 								bool force)
493330b48bdSThierry Reding {
494330b48bdSThierry Reding 	return connector_status_connected;
495330b48bdSThierry Reding }
496330b48bdSThierry Reding 
497330b48bdSThierry Reding static void ps8622_connector_destroy(struct drm_connector *connector)
498330b48bdSThierry Reding {
499330b48bdSThierry Reding 	drm_connector_cleanup(connector);
500330b48bdSThierry Reding }
501330b48bdSThierry Reding 
502330b48bdSThierry Reding static const struct drm_connector_funcs ps8622_connector_funcs = {
503330b48bdSThierry Reding 	.dpms = drm_atomic_helper_connector_dpms,
504330b48bdSThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
505330b48bdSThierry Reding 	.detect = ps8622_detect,
506330b48bdSThierry Reding 	.destroy = ps8622_connector_destroy,
507330b48bdSThierry Reding 	.reset = drm_atomic_helper_connector_reset,
508330b48bdSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
509330b48bdSThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
510330b48bdSThierry Reding };
511330b48bdSThierry Reding 
512330b48bdSThierry Reding static int ps8622_attach(struct drm_bridge *bridge)
513330b48bdSThierry Reding {
514330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
515330b48bdSThierry Reding 	int ret;
516330b48bdSThierry Reding 
517330b48bdSThierry Reding 	if (!bridge->encoder) {
518330b48bdSThierry Reding 		DRM_ERROR("Parent encoder object not found");
519330b48bdSThierry Reding 		return -ENODEV;
520330b48bdSThierry Reding 	}
521330b48bdSThierry Reding 
522330b48bdSThierry Reding 	ps8622->connector.polled = DRM_CONNECTOR_POLL_HPD;
523330b48bdSThierry Reding 	ret = drm_connector_init(bridge->dev, &ps8622->connector,
524330b48bdSThierry Reding 			&ps8622_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
525330b48bdSThierry Reding 	if (ret) {
526330b48bdSThierry Reding 		DRM_ERROR("Failed to initialize connector with drm\n");
527330b48bdSThierry Reding 		return ret;
528330b48bdSThierry Reding 	}
529330b48bdSThierry Reding 	drm_connector_helper_add(&ps8622->connector,
530330b48bdSThierry Reding 					&ps8622_connector_helper_funcs);
531330b48bdSThierry Reding 	drm_connector_register(&ps8622->connector);
532330b48bdSThierry Reding 	drm_mode_connector_attach_encoder(&ps8622->connector,
533330b48bdSThierry Reding 							bridge->encoder);
534330b48bdSThierry Reding 
535330b48bdSThierry Reding 	if (ps8622->panel)
536330b48bdSThierry Reding 		drm_panel_attach(ps8622->panel, &ps8622->connector);
537330b48bdSThierry Reding 
538330b48bdSThierry Reding 	drm_helper_hpd_irq_event(ps8622->connector.dev);
539330b48bdSThierry Reding 
540330b48bdSThierry Reding 	return ret;
541330b48bdSThierry Reding }
542330b48bdSThierry Reding 
543330b48bdSThierry Reding static const struct drm_bridge_funcs ps8622_bridge_funcs = {
544330b48bdSThierry Reding 	.pre_enable = ps8622_pre_enable,
545330b48bdSThierry Reding 	.enable = ps8622_enable,
546330b48bdSThierry Reding 	.disable = ps8622_disable,
547330b48bdSThierry Reding 	.post_disable = ps8622_post_disable,
548330b48bdSThierry Reding 	.attach = ps8622_attach,
549330b48bdSThierry Reding };
550330b48bdSThierry Reding 
551330b48bdSThierry Reding static const struct of_device_id ps8622_devices[] = {
552330b48bdSThierry Reding 	{.compatible = "parade,ps8622",},
553330b48bdSThierry Reding 	{.compatible = "parade,ps8625",},
554330b48bdSThierry Reding 	{}
555330b48bdSThierry Reding };
556330b48bdSThierry Reding MODULE_DEVICE_TABLE(of, ps8622_devices);
557330b48bdSThierry Reding 
558330b48bdSThierry Reding static int ps8622_probe(struct i2c_client *client,
559330b48bdSThierry Reding 					const struct i2c_device_id *id)
560330b48bdSThierry Reding {
561330b48bdSThierry Reding 	struct device *dev = &client->dev;
562330b48bdSThierry Reding 	struct device_node *endpoint, *panel_node;
563330b48bdSThierry Reding 	struct ps8622_bridge *ps8622;
564330b48bdSThierry Reding 	int ret;
565330b48bdSThierry Reding 
566330b48bdSThierry Reding 	ps8622 = devm_kzalloc(dev, sizeof(*ps8622), GFP_KERNEL);
567330b48bdSThierry Reding 	if (!ps8622)
568330b48bdSThierry Reding 		return -ENOMEM;
569330b48bdSThierry Reding 
570330b48bdSThierry Reding 	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
571330b48bdSThierry Reding 	if (endpoint) {
572330b48bdSThierry Reding 		panel_node = of_graph_get_remote_port_parent(endpoint);
573330b48bdSThierry Reding 		if (panel_node) {
574330b48bdSThierry Reding 			ps8622->panel = of_drm_find_panel(panel_node);
575330b48bdSThierry Reding 			of_node_put(panel_node);
576330b48bdSThierry Reding 			if (!ps8622->panel)
577330b48bdSThierry Reding 				return -EPROBE_DEFER;
578330b48bdSThierry Reding 		}
579330b48bdSThierry Reding 	}
580330b48bdSThierry Reding 
581330b48bdSThierry Reding 	ps8622->client = client;
582330b48bdSThierry Reding 
583330b48bdSThierry Reding 	ps8622->v12 = devm_regulator_get(dev, "vdd12");
584330b48bdSThierry Reding 	if (IS_ERR(ps8622->v12)) {
585330b48bdSThierry Reding 		dev_info(dev, "no 1.2v regulator found for PS8622\n");
586330b48bdSThierry Reding 		ps8622->v12 = NULL;
587330b48bdSThierry Reding 	}
588330b48bdSThierry Reding 
589330b48bdSThierry Reding 	ps8622->gpio_slp = devm_gpiod_get(dev, "sleep", GPIOD_OUT_HIGH);
590330b48bdSThierry Reding 	if (IS_ERR(ps8622->gpio_slp)) {
591330b48bdSThierry Reding 		ret = PTR_ERR(ps8622->gpio_slp);
592330b48bdSThierry Reding 		dev_err(dev, "cannot get gpio_slp %d\n", ret);
593330b48bdSThierry Reding 		return ret;
594330b48bdSThierry Reding 	}
595330b48bdSThierry Reding 
596330b48bdSThierry Reding 	/*
597330b48bdSThierry Reding 	 * Assert the reset pin high to avoid the bridge being
598330b48bdSThierry Reding 	 * initialized prematurely
599330b48bdSThierry Reding 	 */
600330b48bdSThierry Reding 	ps8622->gpio_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
601330b48bdSThierry Reding 	if (IS_ERR(ps8622->gpio_rst)) {
602330b48bdSThierry Reding 		ret = PTR_ERR(ps8622->gpio_rst);
603330b48bdSThierry Reding 		dev_err(dev, "cannot get gpio_rst %d\n", ret);
604330b48bdSThierry Reding 		return ret;
605330b48bdSThierry Reding 	}
606330b48bdSThierry Reding 
607330b48bdSThierry Reding 	ps8622->max_lane_count = id->driver_data;
608330b48bdSThierry Reding 
609330b48bdSThierry Reding 	if (of_property_read_u32(dev->of_node, "lane-count",
610330b48bdSThierry Reding 						&ps8622->lane_count)) {
611330b48bdSThierry Reding 		ps8622->lane_count = ps8622->max_lane_count;
612330b48bdSThierry Reding 	} else if (ps8622->lane_count > ps8622->max_lane_count) {
613330b48bdSThierry Reding 		dev_info(dev, "lane-count property is too high,"
614330b48bdSThierry Reding 						"using max_lane_count\n");
615330b48bdSThierry Reding 		ps8622->lane_count = ps8622->max_lane_count;
616330b48bdSThierry Reding 	}
617330b48bdSThierry Reding 
618330b48bdSThierry Reding 	if (!of_find_property(dev->of_node, "use-external-pwm", NULL)) {
619330b48bdSThierry Reding 		ps8622->bl = backlight_device_register("ps8622-backlight",
620330b48bdSThierry Reding 				dev, ps8622, &ps8622_backlight_ops,
621330b48bdSThierry Reding 				NULL);
622330b48bdSThierry Reding 		if (IS_ERR(ps8622->bl)) {
623330b48bdSThierry Reding 			DRM_ERROR("failed to register backlight\n");
624330b48bdSThierry Reding 			ret = PTR_ERR(ps8622->bl);
625330b48bdSThierry Reding 			ps8622->bl = NULL;
626330b48bdSThierry Reding 			return ret;
627330b48bdSThierry Reding 		}
628330b48bdSThierry Reding 		ps8622->bl->props.max_brightness = PS8622_MAX_BRIGHTNESS;
629330b48bdSThierry Reding 		ps8622->bl->props.brightness = PS8622_MAX_BRIGHTNESS;
630330b48bdSThierry Reding 	}
631330b48bdSThierry Reding 
632330b48bdSThierry Reding 	ps8622->bridge.funcs = &ps8622_bridge_funcs;
633330b48bdSThierry Reding 	ps8622->bridge.of_node = dev->of_node;
634330b48bdSThierry Reding 	ret = drm_bridge_add(&ps8622->bridge);
635330b48bdSThierry Reding 	if (ret) {
636330b48bdSThierry Reding 		DRM_ERROR("Failed to add bridge\n");
637330b48bdSThierry Reding 		return ret;
638330b48bdSThierry Reding 	}
639330b48bdSThierry Reding 
640330b48bdSThierry Reding 	i2c_set_clientdata(client, ps8622);
641330b48bdSThierry Reding 
642330b48bdSThierry Reding 	return 0;
643330b48bdSThierry Reding }
644330b48bdSThierry Reding 
645330b48bdSThierry Reding static int ps8622_remove(struct i2c_client *client)
646330b48bdSThierry Reding {
647330b48bdSThierry Reding 	struct ps8622_bridge *ps8622 = i2c_get_clientdata(client);
648330b48bdSThierry Reding 
649330b48bdSThierry Reding 	if (ps8622->bl)
650330b48bdSThierry Reding 		backlight_device_unregister(ps8622->bl);
651330b48bdSThierry Reding 
652330b48bdSThierry Reding 	drm_bridge_remove(&ps8622->bridge);
653330b48bdSThierry Reding 
654330b48bdSThierry Reding 	return 0;
655330b48bdSThierry Reding }
656330b48bdSThierry Reding 
657330b48bdSThierry Reding static const struct i2c_device_id ps8622_i2c_table[] = {
658330b48bdSThierry Reding 	/* Device type, max_lane_count */
659330b48bdSThierry Reding 	{"ps8622", 1},
660330b48bdSThierry Reding 	{"ps8625", 2},
661330b48bdSThierry Reding 	{},
662330b48bdSThierry Reding };
663330b48bdSThierry Reding MODULE_DEVICE_TABLE(i2c, ps8622_i2c_table);
664330b48bdSThierry Reding 
665330b48bdSThierry Reding static struct i2c_driver ps8622_driver = {
666330b48bdSThierry Reding 	.id_table	= ps8622_i2c_table,
667330b48bdSThierry Reding 	.probe		= ps8622_probe,
668330b48bdSThierry Reding 	.remove		= ps8622_remove,
669330b48bdSThierry Reding 	.driver		= {
670330b48bdSThierry Reding 		.name	= "ps8622",
671330b48bdSThierry Reding 		.owner	= THIS_MODULE,
672330b48bdSThierry Reding 		.of_match_table = ps8622_devices,
673330b48bdSThierry Reding 	},
674330b48bdSThierry Reding };
675330b48bdSThierry Reding module_i2c_driver(ps8622_driver);
676330b48bdSThierry Reding 
677330b48bdSThierry Reding MODULE_AUTHOR("Vincent Palatin <vpalatin@chromium.org>");
678330b48bdSThierry Reding MODULE_DESCRIPTION("Parade ps8622/ps8625 eDP-LVDS converter driver");
679330b48bdSThierry Reding MODULE_LICENSE("GPL v2");
680