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