Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/device.h>
7#include <linux/delay.h>
8#include <linux/gpio/consumer.h>
9#include <linux/i2c.h>
10#include <linux/media-bus-format.h>
11#include <linux/regmap.h>
12
13#include <drm/drm_probe_helper.h>
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_edid.h>
16#include <drm/drm_mipi_dsi.h>
17#include <drm/drm_of.h>
18
19#include <video/videomode.h>
20
21#define I2C_MAIN 0
22#define I2C_ADDR_MAIN 0x48
23
24#define I2C_CEC_DSI 1
25#define I2C_ADDR_CEC_DSI 0x49
26
27#define I2C_MAX_IDX 2
28
29struct lt8912 {
30 struct device *dev;
31 struct drm_bridge bridge;
32 struct drm_connector connector;
33
34 struct i2c_client *i2c_client[I2C_MAX_IDX];
35 struct regmap *regmap[I2C_MAX_IDX];
36
37 struct device_node *host_node;
38 struct drm_bridge *hdmi_port;
39
40 struct mipi_dsi_device *dsi;
41
42 struct gpio_desc *gp_reset;
43
44 struct videomode mode;
45
46 struct regulator_bulk_data supplies[7];
47
48 u8 data_lanes;
49 bool is_power_on;
50};
51
52static int lt8912_write_init_config(struct lt8912 *lt)
53{
54 const struct reg_sequence seq[] = {
55 /* Digital clock en*/
56 {0x08, 0xff},
57 {0x09, 0xff},
58 {0x0a, 0xff},
59 {0x0b, 0x7c},
60 {0x0c, 0xff},
61 {0x42, 0x04},
62
63 /*Tx Analog*/
64 {0x31, 0xb1},
65 {0x32, 0xb1},
66 {0x33, 0x0e},
67 {0x37, 0x00},
68 {0x38, 0x22},
69 {0x60, 0x82},
70
71 /*Cbus Analog*/
72 {0x39, 0x45},
73 {0x3a, 0x00},
74 {0x3b, 0x00},
75
76 /*HDMI Pll Analog*/
77 {0x44, 0x31},
78 {0x55, 0x44},
79 {0x57, 0x01},
80 {0x5a, 0x02},
81
82 /*MIPI Analog*/
83 {0x3e, 0xd6},
84 {0x3f, 0xd4},
85 {0x41, 0x3c},
86 {0xB2, 0x00},
87 };
88
89 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
90}
91
92static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
93{
94 const struct reg_sequence seq[] = {
95 {0x12, 0x04},
96 {0x14, 0x00},
97 {0x15, 0x00},
98 {0x1a, 0x03},
99 {0x1b, 0x03},
100 };
101
102 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
103};
104
105static int lt8912_write_dds_config(struct lt8912 *lt)
106{
107 const struct reg_sequence seq[] = {
108 {0x4e, 0xff},
109 {0x4f, 0x56},
110 {0x50, 0x69},
111 {0x51, 0x80},
112 {0x1f, 0x5e},
113 {0x20, 0x01},
114 {0x21, 0x2c},
115 {0x22, 0x01},
116 {0x23, 0xfa},
117 {0x24, 0x00},
118 {0x25, 0xc8},
119 {0x26, 0x00},
120 {0x27, 0x5e},
121 {0x28, 0x01},
122 {0x29, 0x2c},
123 {0x2a, 0x01},
124 {0x2b, 0xfa},
125 {0x2c, 0x00},
126 {0x2d, 0xc8},
127 {0x2e, 0x00},
128 {0x42, 0x64},
129 {0x43, 0x00},
130 {0x44, 0x04},
131 {0x45, 0x00},
132 {0x46, 0x59},
133 {0x47, 0x00},
134 {0x48, 0xf2},
135 {0x49, 0x06},
136 {0x4a, 0x00},
137 {0x4b, 0x72},
138 {0x4c, 0x45},
139 {0x4d, 0x00},
140 {0x52, 0x08},
141 {0x53, 0x00},
142 {0x54, 0xb2},
143 {0x55, 0x00},
144 {0x56, 0xe4},
145 {0x57, 0x0d},
146 {0x58, 0x00},
147 {0x59, 0xe4},
148 {0x5a, 0x8a},
149 {0x5b, 0x00},
150 {0x5c, 0x34},
151 {0x1e, 0x4f},
152 {0x51, 0x00},
153 };
154
155 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
156}
157
158static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
159{
160 int ret;
161
162 ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
163 usleep_range(10000, 20000);
164 ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
165
166 return ret;
167};
168
169/* enable LVDS output with some hardcoded configuration, not required for the HDMI output */
170static int lt8912_write_lvds_config(struct lt8912 *lt)
171{
172 const struct reg_sequence seq[] = {
173 // lvds power up
174 {0x44, 0x30},
175 {0x51, 0x05},
176
177 // core pll bypass
178 {0x50, 0x24}, // cp=50uA
179 {0x51, 0x2d}, // Pix_clk as reference, second order passive LPF PLL
180 {0x52, 0x04}, // loopdiv=0, use second-order PLL
181 {0x69, 0x0e}, // CP_PRESET_DIV_RATIO
182 {0x69, 0x8e},
183 {0x6a, 0x00},
184 {0x6c, 0xb8}, // RGD_CP_SOFT_K_EN,RGD_CP_SOFT_K[13:8]
185 {0x6b, 0x51},
186
187 {0x04, 0xfb}, // core pll reset
188 {0x04, 0xff},
189
190 // scaler bypass
191 {0x7f, 0x00}, // disable scaler
192 {0xa8, 0x13}, // 0x13: JEIDA, 0x33: VESA
193
194 {0x02, 0xf7}, // lvds pll reset
195 {0x02, 0xff},
196 {0x03, 0xcf},
197 {0x03, 0xff},
198 };
199
200 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
201};
202
203static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
204{
205 return container_of(b, struct lt8912, bridge);
206}
207
208static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
209{
210 return container_of(c, struct lt8912, connector);
211}
212
213static const struct regmap_config lt8912_regmap_config = {
214 .reg_bits = 8,
215 .val_bits = 8,
216 .max_register = 0xff,
217};
218
219static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
220{
221 unsigned int i;
222 /*
223 * At this time we only initialize 2 chips, but the lt8912 provides
224 * a third interface for the audio over HDMI configuration.
225 */
226 struct i2c_board_info info[] = {
227 { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
228 { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
229 };
230
231 if (!lt)
232 return -ENODEV;
233
234 for (i = 0; i < ARRAY_SIZE(info); i++) {
235 if (i > 0) {
236 lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
237 info[i].addr);
238 if (IS_ERR(lt->i2c_client[i]))
239 return PTR_ERR(lt->i2c_client[i]);
240 }
241
242 lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
243 <8912_regmap_config);
244 if (IS_ERR(lt->regmap[i]))
245 return PTR_ERR(lt->regmap[i]);
246 }
247 return 0;
248}
249
250static int lt8912_free_i2c(struct lt8912 *lt)
251{
252 unsigned int i;
253
254 for (i = 1; i < I2C_MAX_IDX; i++)
255 i2c_unregister_device(lt->i2c_client[i]);
256
257 return 0;
258}
259
260static int lt8912_hard_power_on(struct lt8912 *lt)
261{
262 int ret;
263
264 ret = regulator_bulk_enable(ARRAY_SIZE(lt->supplies), lt->supplies);
265 if (ret)
266 return ret;
267
268 gpiod_set_value_cansleep(lt->gp_reset, 0);
269 msleep(20);
270
271 return 0;
272}
273
274static void lt8912_hard_power_off(struct lt8912 *lt)
275{
276 gpiod_set_value_cansleep(lt->gp_reset, 1);
277 msleep(20);
278
279 regulator_bulk_disable(ARRAY_SIZE(lt->supplies), lt->supplies);
280
281 lt->is_power_on = false;
282}
283
284static int lt8912_video_setup(struct lt8912 *lt)
285{
286 u32 hactive, h_total, hpw, hfp, hbp;
287 u32 vactive, v_total, vpw, vfp, vbp;
288 u8 settle = 0x08;
289 int ret, hsync_activehigh, vsync_activehigh;
290
291 if (!lt)
292 return -EINVAL;
293
294 hactive = lt->mode.hactive;
295 hfp = lt->mode.hfront_porch;
296 hpw = lt->mode.hsync_len;
297 hbp = lt->mode.hback_porch;
298 h_total = hactive + hfp + hpw + hbp;
299 hsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH;
300
301 vactive = lt->mode.vactive;
302 vfp = lt->mode.vfront_porch;
303 vpw = lt->mode.vsync_len;
304 vbp = lt->mode.vback_porch;
305 v_total = vactive + vfp + vpw + vbp;
306 vsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH;
307
308 if (vactive <= 600)
309 settle = 0x04;
310 else if (vactive == 1080)
311 settle = 0x0a;
312
313 ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
314 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
315 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
316 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
317 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
318 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
319
320 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
321
322 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
323 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
324
325 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
326 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
327
328 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
329 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
330
331 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
332 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
333
334 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
335 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
336
337 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
338 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
339
340 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(0),
341 vsync_activehigh ? BIT(0) : 0);
342 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(1),
343 hsync_activehigh ? BIT(1) : 0);
344 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xb2, BIT(0),
345 lt->connector.display_info.is_hdmi ? BIT(0) : 0);
346
347 return ret;
348}
349
350static int lt8912_soft_power_on(struct lt8912 *lt)
351{
352 if (!lt->is_power_on) {
353 u32 lanes = lt->data_lanes;
354
355 lt8912_write_init_config(lt);
356 regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
357
358 lt8912_write_mipi_basic_config(lt);
359
360 lt->is_power_on = true;
361 }
362
363 return 0;
364}
365
366static int lt8912_video_on(struct lt8912 *lt)
367{
368 int ret;
369
370 ret = lt8912_video_setup(lt);
371 if (ret < 0)
372 goto end;
373
374 ret = lt8912_write_dds_config(lt);
375 if (ret < 0)
376 goto end;
377
378 ret = lt8912_write_rxlogicres_config(lt);
379 if (ret < 0)
380 goto end;
381
382 ret = lt8912_write_lvds_config(lt);
383 if (ret < 0)
384 goto end;
385
386end:
387 return ret;
388}
389
390static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
391{
392 int ret;
393 unsigned int reg_val;
394
395 ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, ®_val);
396 if (ret)
397 return connector_status_unknown;
398
399 if (reg_val & BIT(7))
400 return connector_status_connected;
401
402 return connector_status_disconnected;
403}
404
405static enum drm_connector_status
406lt8912_connector_detect(struct drm_connector *connector, bool force)
407{
408 struct lt8912 *lt = connector_to_lt8912(connector);
409
410 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
411 return drm_bridge_detect(lt->hdmi_port);
412
413 return lt8912_check_cable_status(lt);
414}
415
416static const struct drm_connector_funcs lt8912_connector_funcs = {
417 .detect = lt8912_connector_detect,
418 .fill_modes = drm_helper_probe_single_connector_modes,
419 .destroy = drm_connector_cleanup,
420 .reset = drm_atomic_helper_connector_reset,
421 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
422 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
423};
424
425static int lt8912_connector_get_modes(struct drm_connector *connector)
426{
427 const struct drm_edid *drm_edid;
428 struct lt8912 *lt = connector_to_lt8912(connector);
429 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
430 int ret, num;
431
432 drm_edid = drm_bridge_edid_read(lt->hdmi_port, connector);
433 drm_edid_connector_update(connector, drm_edid);
434 if (!drm_edid)
435 return 0;
436
437 num = drm_edid_connector_add_modes(connector);
438
439 ret = drm_display_info_set_bus_formats(&connector->display_info,
440 &bus_format, 1);
441 if (ret < 0)
442 num = 0;
443
444 drm_edid_free(drm_edid);
445 return num;
446}
447
448static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
449 .get_modes = lt8912_connector_get_modes,
450};
451
452static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
453 const struct drm_display_mode *mode,
454 const struct drm_display_mode *adj)
455{
456 struct lt8912 *lt = bridge_to_lt8912(bridge);
457
458 drm_display_mode_to_videomode(adj, <->mode);
459}
460
461static void lt8912_bridge_enable(struct drm_bridge *bridge)
462{
463 struct lt8912 *lt = bridge_to_lt8912(bridge);
464
465 lt8912_video_on(lt);
466}
467
468static int lt8912_attach_dsi(struct lt8912 *lt)
469{
470 struct device *dev = lt->dev;
471 struct mipi_dsi_host *host;
472 struct mipi_dsi_device *dsi;
473 int ret = -1;
474 const struct mipi_dsi_device_info info = { .type = "lt8912",
475 .channel = 0,
476 .node = NULL,
477 };
478
479 host = of_find_mipi_dsi_host_by_node(lt->host_node);
480 if (!host)
481 return dev_err_probe(dev, -EPROBE_DEFER, "failed to find dsi host\n");
482
483 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
484 if (IS_ERR(dsi)) {
485 ret = PTR_ERR(dsi);
486 dev_err(dev, "failed to create dsi device (%d)\n", ret);
487 return ret;
488 }
489
490 lt->dsi = dsi;
491
492 dsi->lanes = lt->data_lanes;
493 dsi->format = MIPI_DSI_FMT_RGB888;
494
495 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
496 MIPI_DSI_MODE_LPM |
497 MIPI_DSI_MODE_NO_EOT_PACKET;
498
499 ret = devm_mipi_dsi_attach(dev, dsi);
500 if (ret < 0) {
501 dev_err(dev, "failed to attach dsi to host\n");
502 return ret;
503 }
504
505 return 0;
506}
507
508static void lt8912_bridge_hpd_cb(void *data, enum drm_connector_status status)
509{
510 struct lt8912 *lt = data;
511
512 if (lt->bridge.dev)
513 drm_helper_hpd_irq_event(lt->bridge.dev);
514}
515
516static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
517{
518 int ret;
519 struct lt8912 *lt = bridge_to_lt8912(bridge);
520 struct drm_connector *connector = <->connector;
521
522 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD) {
523 drm_bridge_hpd_enable(lt->hdmi_port, lt8912_bridge_hpd_cb, lt);
524 connector->polled = DRM_CONNECTOR_POLL_HPD;
525 } else {
526 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
527 DRM_CONNECTOR_POLL_DISCONNECT;
528 }
529
530 ret = drm_connector_init(bridge->dev, connector,
531 <8912_connector_funcs,
532 lt->hdmi_port->type);
533 if (ret)
534 goto exit;
535
536 drm_connector_helper_add(connector, <8912_connector_helper_funcs);
537
538 connector->dpms = DRM_MODE_DPMS_OFF;
539 drm_connector_attach_encoder(connector, bridge->encoder);
540
541exit:
542 return ret;
543}
544
545static int lt8912_bridge_attach(struct drm_bridge *bridge,
546 enum drm_bridge_attach_flags flags)
547{
548 struct lt8912 *lt = bridge_to_lt8912(bridge);
549 int ret;
550
551 ret = drm_bridge_attach(bridge->encoder, lt->hdmi_port, bridge,
552 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
553 if (ret < 0) {
554 dev_err(lt->dev, "Failed to attach next bridge (%d)\n", ret);
555 return ret;
556 }
557
558 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
559 ret = lt8912_bridge_connector_init(bridge);
560 if (ret) {
561 dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
562 return ret;
563 }
564 }
565
566 ret = lt8912_hard_power_on(lt);
567 if (ret)
568 return ret;
569
570 ret = lt8912_soft_power_on(lt);
571 if (ret)
572 goto error;
573
574 return 0;
575
576error:
577 lt8912_hard_power_off(lt);
578 return ret;
579}
580
581static void lt8912_bridge_detach(struct drm_bridge *bridge)
582{
583 struct lt8912 *lt = bridge_to_lt8912(bridge);
584
585 lt8912_hard_power_off(lt);
586
587 if (lt->connector.dev && lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD)
588 drm_bridge_hpd_disable(lt->hdmi_port);
589}
590
591static enum drm_mode_status
592lt8912_bridge_mode_valid(struct drm_bridge *bridge,
593 const struct drm_display_info *info,
594 const struct drm_display_mode *mode)
595{
596 if (mode->clock > 150000)
597 return MODE_CLOCK_HIGH;
598
599 if (mode->hdisplay > 1920)
600 return MODE_BAD_HVALUE;
601
602 if (mode->vdisplay > 1080)
603 return MODE_BAD_VVALUE;
604
605 return MODE_OK;
606}
607
608static enum drm_connector_status
609lt8912_bridge_detect(struct drm_bridge *bridge)
610{
611 struct lt8912 *lt = bridge_to_lt8912(bridge);
612
613 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
614 return drm_bridge_detect(lt->hdmi_port);
615
616 return lt8912_check_cable_status(lt);
617}
618
619static const struct drm_edid *lt8912_bridge_edid_read(struct drm_bridge *bridge,
620 struct drm_connector *connector)
621{
622 struct lt8912 *lt = bridge_to_lt8912(bridge);
623
624 /*
625 * edid must be read through the ddc bus but it must be
626 * given to the hdmi connector node.
627 */
628 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
629 return drm_bridge_edid_read(lt->hdmi_port, connector);
630
631 dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
632 return NULL;
633}
634
635static const struct drm_bridge_funcs lt8912_bridge_funcs = {
636 .attach = lt8912_bridge_attach,
637 .detach = lt8912_bridge_detach,
638 .mode_valid = lt8912_bridge_mode_valid,
639 .mode_set = lt8912_bridge_mode_set,
640 .enable = lt8912_bridge_enable,
641 .detect = lt8912_bridge_detect,
642 .edid_read = lt8912_bridge_edid_read,
643};
644
645static int lt8912_bridge_resume(struct device *dev)
646{
647 struct lt8912 *lt = dev_get_drvdata(dev);
648 int ret;
649
650 ret = lt8912_hard_power_on(lt);
651 if (ret)
652 return ret;
653
654 ret = lt8912_soft_power_on(lt);
655 if (ret)
656 return ret;
657
658 return lt8912_video_on(lt);
659}
660
661static int lt8912_bridge_suspend(struct device *dev)
662{
663 struct lt8912 *lt = dev_get_drvdata(dev);
664
665 lt8912_hard_power_off(lt);
666
667 return 0;
668}
669
670static DEFINE_SIMPLE_DEV_PM_OPS(lt8912_bridge_pm_ops, lt8912_bridge_suspend, lt8912_bridge_resume);
671
672static int lt8912_get_regulators(struct lt8912 *lt)
673{
674 unsigned int i;
675 const char * const supply_names[] = {
676 "vdd", "vccmipirx", "vccsysclk", "vcclvdstx",
677 "vcchdmitx", "vcclvdspll", "vcchdmipll"
678 };
679
680 for (i = 0; i < ARRAY_SIZE(lt->supplies); i++)
681 lt->supplies[i].supply = supply_names[i];
682
683 return devm_regulator_bulk_get(lt->dev, ARRAY_SIZE(lt->supplies),
684 lt->supplies);
685}
686
687static int lt8912_parse_dt(struct lt8912 *lt)
688{
689 struct gpio_desc *gp_reset;
690 struct device *dev = lt->dev;
691 int ret;
692 int data_lanes;
693 struct device_node *port_node;
694
695 gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
696 if (IS_ERR(gp_reset)) {
697 ret = PTR_ERR(gp_reset);
698 if (ret != -EPROBE_DEFER)
699 dev_err(dev, "Failed to get reset gpio: %d\n", ret);
700 return ret;
701 }
702 lt->gp_reset = gp_reset;
703
704 data_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, -1, 1, 4);
705 if (data_lanes < 0) {
706 dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
707 return data_lanes;
708 }
709
710 lt->data_lanes = data_lanes;
711
712 lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
713 if (!lt->host_node) {
714 dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
715 return -ENODEV;
716 }
717
718 port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
719 if (!port_node) {
720 dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
721 ret = -ENODEV;
722 goto err_free_host_node;
723 }
724
725 lt->hdmi_port = of_drm_find_bridge(port_node);
726 if (!lt->hdmi_port) {
727 ret = -EPROBE_DEFER;
728 dev_err_probe(lt->dev, ret, "%s: Failed to get hdmi port\n", __func__);
729 goto err_free_host_node;
730 }
731
732 if (!of_device_is_compatible(port_node, "hdmi-connector")) {
733 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
734 ret = -EINVAL;
735 goto err_free_host_node;
736 }
737
738 ret = lt8912_get_regulators(lt);
739 if (ret)
740 goto err_free_host_node;
741
742 of_node_put(port_node);
743 return 0;
744
745err_free_host_node:
746 of_node_put(port_node);
747 of_node_put(lt->host_node);
748 return ret;
749}
750
751static int lt8912_put_dt(struct lt8912 *lt)
752{
753 of_node_put(lt->host_node);
754 return 0;
755}
756
757static int lt8912_probe(struct i2c_client *client)
758{
759 static struct lt8912 *lt;
760 int ret = 0;
761 struct device *dev = &client->dev;
762
763 lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
764 if (!lt)
765 return -ENOMEM;
766
767 lt->dev = dev;
768 lt->i2c_client[0] = client;
769
770 ret = lt8912_parse_dt(lt);
771 if (ret)
772 goto err_dt_parse;
773
774 ret = lt8912_init_i2c(lt, client);
775 if (ret)
776 goto err_i2c;
777
778 i2c_set_clientdata(client, lt);
779
780 lt->bridge.funcs = <8912_bridge_funcs;
781 lt->bridge.of_node = dev->of_node;
782 lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
783 DRM_BRIDGE_OP_DETECT);
784
785 drm_bridge_add(<->bridge);
786
787 ret = lt8912_attach_dsi(lt);
788 if (ret)
789 goto err_attach;
790
791 return 0;
792
793err_attach:
794 drm_bridge_remove(<->bridge);
795 lt8912_free_i2c(lt);
796err_i2c:
797 lt8912_put_dt(lt);
798err_dt_parse:
799 return ret;
800}
801
802static void lt8912_remove(struct i2c_client *client)
803{
804 struct lt8912 *lt = i2c_get_clientdata(client);
805
806 drm_bridge_remove(<->bridge);
807 lt8912_free_i2c(lt);
808 lt8912_put_dt(lt);
809}
810
811static const struct of_device_id lt8912_dt_match[] = {
812 {.compatible = "lontium,lt8912b"},
813 {}
814};
815MODULE_DEVICE_TABLE(of, lt8912_dt_match);
816
817static const struct i2c_device_id lt8912_id[] = {
818 {"lt8912", 0},
819 {},
820};
821MODULE_DEVICE_TABLE(i2c, lt8912_id);
822
823static struct i2c_driver lt8912_i2c_driver = {
824 .driver = {
825 .name = "lt8912",
826 .of_match_table = lt8912_dt_match,
827 .pm = pm_sleep_ptr(<8912_bridge_pm_ops),
828 },
829 .probe = lt8912_probe,
830 .remove = lt8912_remove,
831 .id_table = lt8912_id,
832};
833module_i2c_driver(lt8912_i2c_driver);
834
835MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
836MODULE_DESCRIPTION("lt8912 drm driver");
837MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/device.h>
7#include <linux/delay.h>
8#include <linux/gpio/consumer.h>
9#include <linux/i2c.h>
10#include <linux/regmap.h>
11
12#include <drm/drm_probe_helper.h>
13#include <drm/drm_atomic_helper.h>
14#include <drm/drm_mipi_dsi.h>
15#include <drm/drm_of.h>
16
17#include <video/videomode.h>
18
19#define I2C_MAIN 0
20#define I2C_ADDR_MAIN 0x48
21
22#define I2C_CEC_DSI 1
23#define I2C_ADDR_CEC_DSI 0x49
24
25#define I2C_MAX_IDX 2
26
27struct lt8912 {
28 struct device *dev;
29 struct drm_bridge bridge;
30 struct drm_connector connector;
31
32 struct i2c_client *i2c_client[I2C_MAX_IDX];
33 struct regmap *regmap[I2C_MAX_IDX];
34
35 struct device_node *host_node;
36 struct drm_bridge *hdmi_port;
37
38 struct mipi_dsi_device *dsi;
39
40 struct gpio_desc *gp_reset;
41
42 struct videomode mode;
43
44 u8 data_lanes;
45 bool is_power_on;
46 bool is_attached;
47};
48
49static int lt8912_write_init_config(struct lt8912 *lt)
50{
51 const struct reg_sequence seq[] = {
52 /* Digital clock en*/
53 {0x08, 0xff},
54 {0x09, 0xff},
55 {0x0a, 0xff},
56 {0x0b, 0x7c},
57 {0x0c, 0xff},
58 {0x42, 0x04},
59
60 /*Tx Analog*/
61 {0x31, 0xb1},
62 {0x32, 0xb1},
63 {0x33, 0x0e},
64 {0x37, 0x00},
65 {0x38, 0x22},
66 {0x60, 0x82},
67
68 /*Cbus Analog*/
69 {0x39, 0x45},
70 {0x3a, 0x00},
71 {0x3b, 0x00},
72
73 /*HDMI Pll Analog*/
74 {0x44, 0x31},
75 {0x55, 0x44},
76 {0x57, 0x01},
77 {0x5a, 0x02},
78
79 /*MIPI Analog*/
80 {0x3e, 0xd6},
81 {0x3f, 0xd4},
82 {0x41, 0x3c},
83 {0xB2, 0x00},
84 };
85
86 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
87}
88
89static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
90{
91 const struct reg_sequence seq[] = {
92 {0x12, 0x04},
93 {0x14, 0x00},
94 {0x15, 0x00},
95 {0x1a, 0x03},
96 {0x1b, 0x03},
97 };
98
99 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
100};
101
102static int lt8912_write_dds_config(struct lt8912 *lt)
103{
104 const struct reg_sequence seq[] = {
105 {0x4e, 0xff},
106 {0x4f, 0x56},
107 {0x50, 0x69},
108 {0x51, 0x80},
109 {0x1f, 0x5e},
110 {0x20, 0x01},
111 {0x21, 0x2c},
112 {0x22, 0x01},
113 {0x23, 0xfa},
114 {0x24, 0x00},
115 {0x25, 0xc8},
116 {0x26, 0x00},
117 {0x27, 0x5e},
118 {0x28, 0x01},
119 {0x29, 0x2c},
120 {0x2a, 0x01},
121 {0x2b, 0xfa},
122 {0x2c, 0x00},
123 {0x2d, 0xc8},
124 {0x2e, 0x00},
125 {0x42, 0x64},
126 {0x43, 0x00},
127 {0x44, 0x04},
128 {0x45, 0x00},
129 {0x46, 0x59},
130 {0x47, 0x00},
131 {0x48, 0xf2},
132 {0x49, 0x06},
133 {0x4a, 0x00},
134 {0x4b, 0x72},
135 {0x4c, 0x45},
136 {0x4d, 0x00},
137 {0x52, 0x08},
138 {0x53, 0x00},
139 {0x54, 0xb2},
140 {0x55, 0x00},
141 {0x56, 0xe4},
142 {0x57, 0x0d},
143 {0x58, 0x00},
144 {0x59, 0xe4},
145 {0x5a, 0x8a},
146 {0x5b, 0x00},
147 {0x5c, 0x34},
148 {0x1e, 0x4f},
149 {0x51, 0x00},
150 };
151
152 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
153}
154
155static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
156{
157 int ret;
158
159 ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
160 usleep_range(10000, 20000);
161 ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
162
163 return ret;
164};
165
166static int lt8912_write_lvds_config(struct lt8912 *lt)
167{
168 const struct reg_sequence seq[] = {
169 {0x44, 0x30},
170 {0x51, 0x05},
171 {0x50, 0x24},
172 {0x51, 0x2d},
173 {0x52, 0x04},
174 {0x69, 0x0e},
175 {0x69, 0x8e},
176 {0x6a, 0x00},
177 {0x6c, 0xb8},
178 {0x6b, 0x51},
179 {0x04, 0xfb},
180 {0x04, 0xff},
181 {0x7f, 0x00},
182 {0xa8, 0x13},
183 {0x02, 0xf7},
184 {0x02, 0xff},
185 {0x03, 0xcf},
186 {0x03, 0xff},
187 };
188
189 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
190};
191
192static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
193{
194 return container_of(b, struct lt8912, bridge);
195}
196
197static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
198{
199 return container_of(c, struct lt8912, connector);
200}
201
202static const struct regmap_config lt8912_regmap_config = {
203 .reg_bits = 8,
204 .val_bits = 8,
205 .max_register = 0xff,
206};
207
208static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
209{
210 unsigned int i;
211 /*
212 * At this time we only initialize 2 chips, but the lt8912 provides
213 * a third interface for the audio over HDMI configuration.
214 */
215 struct i2c_board_info info[] = {
216 { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
217 { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
218 };
219
220 if (!lt)
221 return -ENODEV;
222
223 for (i = 0; i < ARRAY_SIZE(info); i++) {
224 if (i > 0) {
225 lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
226 info[i].addr);
227 if (IS_ERR(lt->i2c_client[i]))
228 return PTR_ERR(lt->i2c_client[i]);
229 }
230
231 lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
232 <8912_regmap_config);
233 if (IS_ERR(lt->regmap[i]))
234 return PTR_ERR(lt->regmap[i]);
235 }
236 return 0;
237}
238
239static int lt8912_free_i2c(struct lt8912 *lt)
240{
241 unsigned int i;
242
243 for (i = 1; i < I2C_MAX_IDX; i++)
244 i2c_unregister_device(lt->i2c_client[i]);
245
246 return 0;
247}
248
249static int lt8912_hard_power_on(struct lt8912 *lt)
250{
251 gpiod_set_value_cansleep(lt->gp_reset, 0);
252 msleep(20);
253
254 return 0;
255}
256
257static void lt8912_hard_power_off(struct lt8912 *lt)
258{
259 gpiod_set_value_cansleep(lt->gp_reset, 1);
260 msleep(20);
261 lt->is_power_on = false;
262}
263
264static int lt8912_video_setup(struct lt8912 *lt)
265{
266 u32 hactive, h_total, hpw, hfp, hbp;
267 u32 vactive, v_total, vpw, vfp, vbp;
268 u8 settle = 0x08;
269 int ret;
270
271 if (!lt)
272 return -EINVAL;
273
274 hactive = lt->mode.hactive;
275 hfp = lt->mode.hfront_porch;
276 hpw = lt->mode.hsync_len;
277 hbp = lt->mode.hback_porch;
278 h_total = hactive + hfp + hpw + hbp;
279
280 vactive = lt->mode.vactive;
281 vfp = lt->mode.vfront_porch;
282 vpw = lt->mode.vsync_len;
283 vbp = lt->mode.vback_porch;
284 v_total = vactive + vfp + vpw + vbp;
285
286 if (vactive <= 600)
287 settle = 0x04;
288 else if (vactive == 1080)
289 settle = 0x0a;
290
291 ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
292 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
293 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
294 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
295 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
296 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
297
298 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
299
300 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
301 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
302
303 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
304 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
305
306 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
307 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
308
309 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
310 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
311
312 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
313 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
314
315 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
316 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
317
318 return ret;
319}
320
321static int lt8912_soft_power_on(struct lt8912 *lt)
322{
323 if (!lt->is_power_on) {
324 u32 lanes = lt->data_lanes;
325
326 lt8912_write_init_config(lt);
327 regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
328
329 lt8912_write_mipi_basic_config(lt);
330
331 lt->is_power_on = true;
332 }
333
334 return 0;
335}
336
337static int lt8912_video_on(struct lt8912 *lt)
338{
339 int ret;
340
341 ret = lt8912_video_setup(lt);
342 if (ret < 0)
343 goto end;
344
345 ret = lt8912_write_dds_config(lt);
346 if (ret < 0)
347 goto end;
348
349 ret = lt8912_write_rxlogicres_config(lt);
350 if (ret < 0)
351 goto end;
352
353 ret = lt8912_write_lvds_config(lt);
354 if (ret < 0)
355 goto end;
356
357end:
358 return ret;
359}
360
361static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
362{
363 int ret;
364 unsigned int reg_val;
365
366 ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, ®_val);
367 if (ret)
368 return connector_status_unknown;
369
370 if (reg_val & BIT(7))
371 return connector_status_connected;
372
373 return connector_status_disconnected;
374}
375
376static enum drm_connector_status
377lt8912_connector_detect(struct drm_connector *connector, bool force)
378{
379 struct lt8912 *lt = connector_to_lt8912(connector);
380
381 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
382 return drm_bridge_detect(lt->hdmi_port);
383
384 return lt8912_check_cable_status(lt);
385}
386
387static const struct drm_connector_funcs lt8912_connector_funcs = {
388 .detect = lt8912_connector_detect,
389 .fill_modes = drm_helper_probe_single_connector_modes,
390 .destroy = drm_connector_cleanup,
391 .reset = drm_atomic_helper_connector_reset,
392 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
393 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
394};
395
396static enum drm_mode_status
397lt8912_connector_mode_valid(struct drm_connector *connector,
398 struct drm_display_mode *mode)
399{
400 if (mode->clock > 150000)
401 return MODE_CLOCK_HIGH;
402
403 if (mode->hdisplay > 1920)
404 return MODE_BAD_HVALUE;
405
406 if (mode->vdisplay > 1080)
407 return MODE_BAD_VVALUE;
408
409 return MODE_OK;
410}
411
412static int lt8912_connector_get_modes(struct drm_connector *connector)
413{
414 struct edid *edid;
415 int ret = -1;
416 int num = 0;
417 struct lt8912 *lt = connector_to_lt8912(connector);
418 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
419
420 edid = drm_bridge_get_edid(lt->hdmi_port, connector);
421 if (edid) {
422 drm_connector_update_edid_property(connector, edid);
423 num = drm_add_edid_modes(connector, edid);
424 } else {
425 return ret;
426 }
427
428 ret = drm_display_info_set_bus_formats(&connector->display_info,
429 &bus_format, 1);
430 if (ret)
431 num = ret;
432
433 kfree(edid);
434 return num;
435}
436
437static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
438 .get_modes = lt8912_connector_get_modes,
439 .mode_valid = lt8912_connector_mode_valid,
440};
441
442static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
443 const struct drm_display_mode *mode,
444 const struct drm_display_mode *adj)
445{
446 struct lt8912 *lt = bridge_to_lt8912(bridge);
447
448 drm_display_mode_to_videomode(adj, <->mode);
449}
450
451static void lt8912_bridge_enable(struct drm_bridge *bridge)
452{
453 struct lt8912 *lt = bridge_to_lt8912(bridge);
454
455 lt8912_video_on(lt);
456}
457
458static int lt8912_attach_dsi(struct lt8912 *lt)
459{
460 struct device *dev = lt->dev;
461 struct mipi_dsi_host *host;
462 struct mipi_dsi_device *dsi;
463 int ret = -1;
464 const struct mipi_dsi_device_info info = { .type = "lt8912",
465 .channel = 0,
466 .node = NULL,
467 };
468
469 host = of_find_mipi_dsi_host_by_node(lt->host_node);
470 if (!host) {
471 dev_err(dev, "failed to find dsi host\n");
472 return -EPROBE_DEFER;
473 }
474
475 dsi = mipi_dsi_device_register_full(host, &info);
476 if (IS_ERR(dsi)) {
477 ret = PTR_ERR(dsi);
478 dev_err(dev, "failed to create dsi device (%d)\n", ret);
479 goto err_dsi_device;
480 }
481
482 lt->dsi = dsi;
483
484 dsi->lanes = lt->data_lanes;
485 dsi->format = MIPI_DSI_FMT_RGB888;
486
487 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
488 MIPI_DSI_MODE_VIDEO_BURST |
489 MIPI_DSI_MODE_LPM |
490 MIPI_DSI_MODE_EOT_PACKET;
491
492 ret = mipi_dsi_attach(dsi);
493 if (ret < 0) {
494 dev_err(dev, "failed to attach dsi to host\n");
495 goto err_dsi_attach;
496 }
497
498 return 0;
499
500err_dsi_attach:
501 mipi_dsi_device_unregister(dsi);
502err_dsi_device:
503 return ret;
504}
505
506static void lt8912_detach_dsi(struct lt8912 *lt)
507{
508 mipi_dsi_detach(lt->dsi);
509 mipi_dsi_device_unregister(lt->dsi);
510}
511
512static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
513{
514 int ret;
515 struct lt8912 *lt = bridge_to_lt8912(bridge);
516 struct drm_connector *connector = <->connector;
517
518 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
519 DRM_CONNECTOR_POLL_DISCONNECT;
520
521 ret = drm_connector_init(bridge->dev, connector,
522 <8912_connector_funcs,
523 lt->hdmi_port->type);
524 if (ret)
525 goto exit;
526
527 drm_connector_helper_add(connector, <8912_connector_helper_funcs);
528
529 connector->dpms = DRM_MODE_DPMS_OFF;
530 drm_connector_attach_encoder(connector, bridge->encoder);
531
532exit:
533 return ret;
534}
535
536static int lt8912_bridge_attach(struct drm_bridge *bridge,
537 enum drm_bridge_attach_flags flags)
538{
539 struct lt8912 *lt = bridge_to_lt8912(bridge);
540 int ret;
541
542 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
543 ret = lt8912_bridge_connector_init(bridge);
544 if (ret) {
545 dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
546 return ret;
547 }
548 }
549
550 ret = lt8912_hard_power_on(lt);
551 if (ret)
552 return ret;
553
554 ret = lt8912_soft_power_on(lt);
555 if (ret)
556 goto error;
557
558 ret = lt8912_attach_dsi(lt);
559 if (ret)
560 goto error;
561
562 lt->is_attached = true;
563
564 return 0;
565
566error:
567 lt8912_hard_power_off(lt);
568 return ret;
569}
570
571static void lt8912_bridge_detach(struct drm_bridge *bridge)
572{
573 struct lt8912 *lt = bridge_to_lt8912(bridge);
574
575 if (lt->is_attached) {
576 lt8912_detach_dsi(lt);
577 lt8912_hard_power_off(lt);
578 drm_connector_unregister(<->connector);
579 drm_connector_cleanup(<->connector);
580 }
581}
582
583static enum drm_connector_status
584lt8912_bridge_detect(struct drm_bridge *bridge)
585{
586 struct lt8912 *lt = bridge_to_lt8912(bridge);
587
588 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
589 return drm_bridge_detect(lt->hdmi_port);
590
591 return lt8912_check_cable_status(lt);
592}
593
594static struct edid *lt8912_bridge_get_edid(struct drm_bridge *bridge,
595 struct drm_connector *connector)
596{
597 struct lt8912 *lt = bridge_to_lt8912(bridge);
598
599 /*
600 * edid must be read through the ddc bus but it must be
601 * given to the hdmi connector node.
602 */
603 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
604 return drm_bridge_get_edid(lt->hdmi_port, connector);
605
606 dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
607 return NULL;
608}
609
610static const struct drm_bridge_funcs lt8912_bridge_funcs = {
611 .attach = lt8912_bridge_attach,
612 .detach = lt8912_bridge_detach,
613 .mode_set = lt8912_bridge_mode_set,
614 .enable = lt8912_bridge_enable,
615 .detect = lt8912_bridge_detect,
616 .get_edid = lt8912_bridge_get_edid,
617};
618
619static int lt8912_parse_dt(struct lt8912 *lt)
620{
621 struct gpio_desc *gp_reset;
622 struct device *dev = lt->dev;
623 int ret;
624 int data_lanes;
625 struct device_node *port_node;
626 struct device_node *endpoint;
627
628 gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
629 if (IS_ERR(gp_reset)) {
630 ret = PTR_ERR(gp_reset);
631 if (ret != -EPROBE_DEFER)
632 dev_err(dev, "Failed to get reset gpio: %d\n", ret);
633 return ret;
634 }
635 lt->gp_reset = gp_reset;
636
637 endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
638 if (!endpoint)
639 return -ENODEV;
640
641 data_lanes = of_property_count_u32_elems(endpoint, "data-lanes");
642 of_node_put(endpoint);
643 if (data_lanes < 0) {
644 dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
645 return data_lanes;
646 }
647 lt->data_lanes = data_lanes;
648
649 lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
650 if (!lt->host_node) {
651 dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
652 return -ENODEV;
653 }
654
655 port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
656 if (!port_node) {
657 dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
658 ret = -ENODEV;
659 goto err_free_host_node;
660 }
661
662 lt->hdmi_port = of_drm_find_bridge(port_node);
663 if (!lt->hdmi_port) {
664 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
665 ret = -ENODEV;
666 goto err_free_host_node;
667 }
668
669 if (!of_device_is_compatible(port_node, "hdmi-connector")) {
670 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
671 ret = -EINVAL;
672 goto err_free_host_node;
673 }
674
675 of_node_put(port_node);
676 return 0;
677
678err_free_host_node:
679 of_node_put(port_node);
680 of_node_put(lt->host_node);
681 return ret;
682}
683
684static int lt8912_put_dt(struct lt8912 *lt)
685{
686 of_node_put(lt->host_node);
687 return 0;
688}
689
690static int lt8912_probe(struct i2c_client *client,
691 const struct i2c_device_id *id)
692{
693 static struct lt8912 *lt;
694 int ret = 0;
695 struct device *dev = &client->dev;
696
697 lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
698 if (!lt)
699 return -ENOMEM;
700
701 lt->dev = dev;
702 lt->i2c_client[0] = client;
703
704 ret = lt8912_parse_dt(lt);
705 if (ret)
706 goto err_dt_parse;
707
708 ret = lt8912_init_i2c(lt, client);
709 if (ret)
710 goto err_i2c;
711
712 i2c_set_clientdata(client, lt);
713
714 lt->bridge.funcs = <8912_bridge_funcs;
715 lt->bridge.of_node = dev->of_node;
716 lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
717 DRM_BRIDGE_OP_DETECT);
718
719 drm_bridge_add(<->bridge);
720
721 return 0;
722
723err_i2c:
724 lt8912_put_dt(lt);
725err_dt_parse:
726 return ret;
727}
728
729static int lt8912_remove(struct i2c_client *client)
730{
731 struct lt8912 *lt = i2c_get_clientdata(client);
732
733 lt8912_bridge_detach(<->bridge);
734 drm_bridge_remove(<->bridge);
735 lt8912_free_i2c(lt);
736 lt8912_put_dt(lt);
737 return 0;
738}
739
740static const struct of_device_id lt8912_dt_match[] = {
741 {.compatible = "lontium,lt8912b"},
742 {}
743};
744MODULE_DEVICE_TABLE(of, lt8912_dt_match);
745
746static const struct i2c_device_id lt8912_id[] = {
747 {"lt8912", 0},
748 {},
749};
750MODULE_DEVICE_TABLE(i2c, lt8912_id);
751
752static struct i2c_driver lt8912_i2c_driver = {
753 .driver = {
754 .name = "lt8912",
755 .of_match_table = lt8912_dt_match,
756 },
757 .probe = lt8912_probe,
758 .remove = lt8912_remove,
759 .id_table = lt8912_id,
760};
761module_i2c_driver(lt8912_i2c_driver);
762
763MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
764MODULE_DESCRIPTION("lt8912 drm driver");
765MODULE_LICENSE("GPL v2");