blob: 8c27c2631dc9b1eced44282a2c14683ffa1888e8 [file] [log] [blame]
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
GG Houfc792522020-03-16 15:35:49 +08003 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08004 */
5
6#define pr_fmt(fmt) "%s: " fmt, __func__
7
8#include <linux/types.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/platform_device.h>
14#include <linux/fs.h>
15#include <linux/delay.h>
16#include <linux/i2c.h>
17#include <linux/gpio.h>
18#include <linux/interrupt.h>
19#include <linux/component.h>
Yuan Zhao1831e752020-03-23 14:16:28 +080020#include <linux/workqueue.h>
Wenjun Zhang2142e0c2019-10-28 15:41:08 +080021#include <linux/of_gpio.h>
22#include <linux/of_graph.h>
23#include <linux/of_irq.h>
24#include <linux/regulator/consumer.h>
25#include <linux/firmware.h>
26#include <linux/hdmi.h>
27#include <drm/drmP.h>
28#include <drm/drm_atomic.h>
29#include <drm/drm_atomic_helper.h>
30#include <drm/drm_edid.h>
31#include <drm/drm_mipi_dsi.h>
32#include <drm/drm_crtc_helper.h>
33#include <linux/string.h>
34
35#define CFG_HPD_INTERRUPTS BIT(0)
36#define CFG_EDID_INTERRUPTS BIT(1)
37#define CFG_CEC_INTERRUPTS BIT(2)
38#define CFG_VID_CHK_INTERRUPTS BIT(3)
39
40#define EDID_SEG_SIZE 256
41#define READ_BUF_MAX_SIZE 64
42#define WRITE_BUF_MAX_SIZE 64
Wenjun Zhang2142e0c2019-10-28 15:41:08 +080043
44struct lt9611_reg_cfg {
45 u8 reg;
46 u8 val;
47};
48
49enum lt9611_fw_upgrade_status {
50 UPDATE_SUCCESS = 0,
51 UPDATE_RUNNING = 1,
52 UPDATE_FAILED = 2,
53};
54
55struct lt9611_vreg {
56 struct regulator *vreg; /* vreg handle */
57 char vreg_name[32];
58 int min_voltage;
59 int max_voltage;
60 int enable_load;
61 int disable_load;
62 int pre_on_sleep;
63 int post_on_sleep;
64 int pre_off_sleep;
65 int post_off_sleep;
66};
67
68struct lt9611_video_cfg {
69 u32 h_active;
70 u32 h_front_porch;
71 u32 h_pulse_width;
72 u32 h_back_porch;
73 bool h_polarity;
74 u32 v_active;
75 u32 v_front_porch;
76 u32 v_pulse_width;
77 u32 v_back_porch;
78 bool v_polarity;
79 u32 pclk_khz;
80 bool interlaced;
81 u32 vic;
82 enum hdmi_picture_aspect ar;
83 u32 num_of_lanes;
84 u32 num_of_intfs;
85 u8 scaninfo;
86};
87
88struct lt9611 {
89 struct device *dev;
90 struct drm_bridge bridge;
91
92 struct device_node *host_node;
93 struct mipi_dsi_device *dsi;
94 struct drm_connector connector;
95
96 u8 i2c_addr;
97 int irq;
98 bool ac_mode;
99
100 u32 irq_gpio;
101 u32 reset_gpio;
102 u32 hdmi_ps_gpio;
103 u32 hdmi_en_gpio;
104
105 unsigned int num_vreg;
106 struct lt9611_vreg *vreg_config;
107
108 struct i2c_client *i2c_client;
109
110 enum drm_connector_status status;
111 bool power_on;
112
113 /* get display modes from device tree */
114 bool non_pluggable;
115 u32 num_of_modes;
116 struct list_head mode_list;
117
118 struct drm_display_mode curr_mode;
119 struct lt9611_video_cfg video_cfg;
120
Yuan Zhao1831e752020-03-23 14:16:28 +0800121 struct workqueue_struct *wq;
122 struct work_struct work;
123
Wenjun Zhang2142e0c2019-10-28 15:41:08 +0800124 u8 edid_buf[EDID_SEG_SIZE];
125 u8 i2c_wbuf[WRITE_BUF_MAX_SIZE];
126 u8 i2c_rbuf[READ_BUF_MAX_SIZE];
127 bool hdmi_mode;
128 enum lt9611_fw_upgrade_status fw_status;
129};
130
131struct lt9611_timing_info {
132 u16 xres;
133 u16 yres;
134 u8 bpp;
135 u8 fps;
136 u8 lanes;
137 u8 intfs;
138};
139
140static struct lt9611_timing_info lt9611_supp_timing_cfg[] = {
141 {3840, 2160, 24, 30, 4, 2}, /* 3840x2160 24bit 30Hz 4Lane 2ports */
142 {1920, 1080, 24, 60, 4, 1}, /* 1080P 24bit 60Hz 4lane 1port */
143 {1920, 1080, 24, 30, 3, 1}, /* 1080P 24bit 30Hz 3lane 1port */
144 {1920, 1080, 24, 24, 3, 1},
145 {720, 480, 24, 60, 2, 1},
146 {720, 576, 24, 50, 2, 1},
147 {640, 480, 24, 60, 2, 1},
148 {0xffff, 0xffff, 0xff, 0xff, 0xff},
149};
150
Yuan Zhao1831e752020-03-23 14:16:28 +0800151void lt9611_hpd_work(struct work_struct *work)
152{
153 char name[32], status[32];
154 char *envp[5];
155 char *event_string = "HOTPLUG=1";
156 enum drm_connector_status last_status;
157 struct drm_device *dev = NULL;
158 struct lt9611 *pdata = container_of(work, struct lt9611, work);
159
160 if (!pdata || !pdata->connector.funcs ||
161 !pdata->connector.funcs->detect)
162 return;
163
164 dev = pdata->connector.dev;
165 last_status = pdata->connector.status;
166 pdata->connector.status =
167 pdata->connector.funcs->detect(&pdata->connector, true);
168
169 if (last_status == pdata->connector.status)
170 return;
171
172 scnprintf(name, 32, "name=%s",
173 pdata->connector.name);
174 scnprintf(status, 32, "status=%s",
175 drm_get_connector_status_name(pdata->connector.status));
176 pr_debug("[%s]:[%s]\n", name, status);
177 envp[0] = name;
178 envp[1] = status;
179 envp[2] = event_string;
180 envp[3] = NULL;
181 envp[4] = NULL;
182 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
183 envp);
184}
185
Wenjun Zhang2142e0c2019-10-28 15:41:08 +0800186static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge)
187{
188 return container_of(bridge, struct lt9611, bridge);
189}
190
191static struct lt9611 *connector_to_lt9611(struct drm_connector *connector)
192{
193 return container_of(connector, struct lt9611, connector);
194}
195
196/*
197 * Write one reg with more values;
198 * Reg -> value0, value1, value2.
199 */
200
201static int lt9611_write(struct lt9611 *pdata, u8 reg,
202 const u8 *buf, int size)
203{
204 struct i2c_client *client = pdata->i2c_client;
205 struct i2c_msg msg = {
206 .addr = client->addr,
207 .flags = 0,
208 .len = size + 1,
209 .buf = pdata->i2c_wbuf,
210 };
211
212 pdata->i2c_wbuf[0] = reg;
213 if (size > (WRITE_BUF_MAX_SIZE - 1)) {
214 pr_err("invalid write buffer size %d\n", size);
215 return -EINVAL;
216 }
217
218 memcpy(pdata->i2c_wbuf + 1, buf, size);
219
220 if (i2c_transfer(client->adapter, &msg, 1) < 1) {
221 pr_err("i2c write failed\n");
222 return -EIO;
223 }
224
225 return 0;
226}
227
228/*
229 * Write one reg with one value;
230 * Reg -> value
231 */
232static int lt9611_write_byte(struct lt9611 *pdata, const u8 reg, u8 value)
233{
234 struct i2c_client *client = pdata->i2c_client;
235 struct i2c_msg msg = {
236 .addr = client->addr,
237 .flags = 0,
238 .len = 2,
239 .buf = pdata->i2c_wbuf,
240 };
241
242 memset(pdata->i2c_wbuf, 0, WRITE_BUF_MAX_SIZE);
243 pdata->i2c_wbuf[0] = reg;
244 pdata->i2c_wbuf[1] = value;
245
246 if (i2c_transfer(client->adapter, &msg, 1) < 1) {
247 pr_err("i2c write failed\n");
248 return -EIO;
249 }
250
251 return 0;
252}
253
254/*
255 * Write more regs with more values;
256 * Reg1 -> value1
257 * Reg2 -> value2
258 */
259static void lt9611_write_array(struct lt9611 *pdata,
260 struct lt9611_reg_cfg *reg_arry, int size)
261{
262 int i = 0;
263
264 for (i = 0; i < size; i++)
265 lt9611_write_byte(pdata, reg_arry[i].reg, reg_arry[i].val);
266}
267
268static int lt9611_read(struct lt9611 *pdata, u8 reg, char *buf, u32 size)
269{
270 struct i2c_client *client = pdata->i2c_client;
271 struct i2c_msg msg[2] = {
272 {
273 .addr = client->addr,
274 .flags = 0,
275 .len = 1,
276 .buf = pdata->i2c_wbuf,
277 },
278 {
279 .addr = client->addr,
280 .flags = I2C_M_RD,
281 .len = size,
282 .buf = pdata->i2c_rbuf,
283 }
284 };
285
286 memset(pdata->i2c_wbuf, 0x0, WRITE_BUF_MAX_SIZE);
287 memset(pdata->i2c_rbuf, 0x0, READ_BUF_MAX_SIZE);
288 pdata->i2c_wbuf[0] = reg;
289
290 if (i2c_transfer(client->adapter, msg, 2) != 2) {
291 pr_err("i2c read failed\n");
292 return -EIO;
293 }
294
295 memcpy(buf, pdata->i2c_rbuf, size);
296
297 return 0;
298}
299
300void lt9611_config(struct lt9611 *pdata)
301{
302 struct lt9611_reg_cfg reg_cfg[] = {
303 {0xFF, 0x80},
304 {0xEE, 0x01},
305 {0x5E, 0xDF},
306 {0x58, 0x00},
307 {0x59, 0x50},
308 {0x5A, 0x10},
309 {0x5A, 0x00},
310 };
311
312 lt9611_write_array(pdata, reg_cfg, ARRAY_SIZE(reg_cfg));
313}
314
315u8 lt9611_get_version(struct lt9611 *pdata)
316{
317 u8 revison = 0;
318
319 lt9611_write_byte(pdata, 0xFF, 0x80);
320 lt9611_write_byte(pdata, 0xEE, 0x01);
321 lt9611_write_byte(pdata, 0xFF, 0xB0);
322
323 if (!lt9611_read(pdata, 0x21, &revison, 1))
324 pr_info("LT9611 revison: 0x%x\n", revison);
325 else
326 pr_err("LT9611 get revison failed\n");
327
328 lt9611_write_byte(pdata, 0xFF, 0x80);
329 lt9611_write_byte(pdata, 0xEE, 0x00);
Yuan Zhao1831e752020-03-23 14:16:28 +0800330 msleep(50);
Wenjun Zhang2142e0c2019-10-28 15:41:08 +0800331
332 return revison;
333}
334
335void lt9611_flash_write_en(struct lt9611 *pdata)
336{
337 struct lt9611_reg_cfg reg_cfg0[] = {
338 {0xFF, 0x81},
339 {0x08, 0xBF},
340 };
341
342 struct lt9611_reg_cfg reg_cfg1[] = {
343 {0xFF, 0x80},
344 {0x5A, 0x04},
345 {0x5A, 0x00},
346 };
347
348 lt9611_write_array(pdata, reg_cfg0, ARRAY_SIZE(reg_cfg0));
349 msleep(20);
350 lt9611_write_byte(pdata, 0x08, 0xFF);
351 msleep(20);
352 lt9611_write_array(pdata, reg_cfg1, ARRAY_SIZE(reg_cfg1));
353}
354
355void lt9611_block_erase(struct lt9611 *pdata)
356{
357 struct lt9611_reg_cfg reg_cfg[] = {
358 {0xFF, 0x80},
359 {0xEE, 0x01},
360 {0x5A, 0x04},
361 {0x5A, 0x00},
362 {0x5B, 0x00},
363 {0x5C, 0x00},
364 {0x5D, 0x00},
365 {0x5A, 0x01},
366 {0x5A, 0x00},
367 };
368
369 pr_info("LT9611 block erase\n");
370 lt9611_write_array(pdata, reg_cfg, ARRAY_SIZE(reg_cfg));
371 msleep(3000);
372}
373
374void lt9611_flash_read_addr_set(struct lt9611 *pdata, u32 addr)
375{
376 struct lt9611_reg_cfg reg_cfg[] = {
377 {0x5E, 0x5F},
378 {0x5A, 0xA0},
379 {0x5A, 0x80},
380 {0x5B, (addr & 0xFF0000) >> 16},
381 {0x5C, (addr & 0xFF00) >> 8},
382 {0x5D, addr & 0xFF},
383 {0x5A, 0x90},
384 {0x5A, 0x80},
385 {0x58, 0x21},
386 };
387
388 lt9611_write_array(pdata, reg_cfg, ARRAY_SIZE(reg_cfg));
389}
390
391void lt9611_fw_read_back(struct lt9611 *pdata, u8 *buff, int size)
392{
393 u8 page_data[32];
394 int page_number = 0, i = 0, addr = 0;
395
396 struct lt9611_reg_cfg reg_cfg[] = {
397 {0xFF, 0x80},
398 {0xEE, 0x01},
399 {0x5A, 0x84},
400 {0x5A, 0x80},
401 };
402 /*
403 * Read 32 bytes once.
404 */
405 page_number = size / 32;
406 if (size % 32)
407 page_number++;
408
409 lt9611_write_array(pdata, reg_cfg, ARRAY_SIZE(reg_cfg));
410
411 for (i = 0; i < page_number; i++) {
412 memset(page_data, 0x0, 32);
413 lt9611_flash_read_addr_set(pdata, addr);
414 lt9611_read(pdata, 0x5F, page_data, 32);
415 memcpy(buff, page_data, 32);
416 buff += 32;
417 addr += 32;
418 }
419}
420
421void lt9611_flash_write_config(struct lt9611 *pdata)
422{
423 struct lt9611_reg_cfg reg_cfg[] = {
424 {0xFF, 0x80},
425 {0x5E, 0xDF},
426 {0x5A, 0x20},
427 {0x5A, 0x00},
428 {0x58, 0x21},
429 };
430
431 lt9611_flash_write_en(pdata);
432 lt9611_write_array(pdata, reg_cfg, ARRAY_SIZE(reg_cfg));
433}
434
435void lt9611_flash_write_addr_set(struct lt9611 *pdata, u32 addr)
436{
437 struct lt9611_reg_cfg reg_cfg[] = {
438 {0x5B, (addr & 0xFF0000) >> 16},
439 {0x5C, (addr & 0xFF00) >> 8},
440 {0x5D, addr & 0xFF},
441 {0x5A, 0x10},
442 {0x5A, 0x00},
443 };
444
445 lt9611_write_array(pdata, reg_cfg, ARRAY_SIZE(reg_cfg));
446}
447
448void lt9611_firmware_write(struct lt9611 *pdata, const u8 *f_data,
449 int size)
450{
451 u8 last_buf[32];
452 int i = 0, page_size = 32;
453 int start_addr = 0, total_page = 0, rest_data = 0;
454
455 total_page = size / page_size;
456 rest_data = size % page_size;
457
458 for (i = 0; i < total_page; i++) {
459 lt9611_flash_write_config(pdata);
460 lt9611_write(pdata, 0x59, f_data, page_size);
461 lt9611_flash_write_addr_set(pdata, start_addr);
462 start_addr += page_size;
463 f_data += page_size;
464 msleep(20);
465 }
466
467 if (rest_data > 0) {
468 memset(last_buf, 0xFF, 32);
469 memcpy(last_buf, f_data, rest_data);
470 lt9611_flash_write_config(pdata);
471 lt9611_write(pdata, 0x59, last_buf, page_size);
472
473 lt9611_flash_write_addr_set(pdata, start_addr);
474 msleep(20);
475 }
476 msleep(20);
477
478 pr_info("LT9611 FW write over, total size: %d, page: %d, reset: %d\n",
479 size, total_page, rest_data);
480}
481
482void lt9611_firmware_upgrade(struct lt9611 *pdata,
483 const struct firmware *cfg)
484{
485 int i = 0;
486 u8 *fw_read_data = NULL;
487 int data_len = (int)cfg->size;
488
489 pr_info("LT9611 FW total size %d\n", data_len);
490
491 fw_read_data = kzalloc(ALIGN(data_len, 32), GFP_KERNEL);
492 if (!fw_read_data)
493 return;
494
495 pdata->fw_status = UPDATE_RUNNING;
496 lt9611_config(pdata);
497
498 /*
499 * Need erase block 2 timess here.
500 * Sometimes, erase can fail.
501 * This is a workaroud.
502 */
503 for (i = 0; i < 2; i++)
504 lt9611_block_erase(pdata);
505
506 lt9611_firmware_write(pdata, cfg->data, data_len);
507 msleep(20);
508 lt9611_fw_read_back(pdata, fw_read_data, data_len);
509
510 if (!memcmp(cfg->data, fw_read_data, data_len)) {
511 pdata->fw_status = UPDATE_SUCCESS;
512 pr_debug("LT9611 Firmware upgrade success.\n");
513 } else {
514 pdata->fw_status = UPDATE_FAILED;
515 pr_err("LT9611 Firmware upgrade failed\n");
516 }
517
518 kfree(fw_read_data);
519}
520
521static void lt9611_firmware_cb(const struct firmware *cfg, void *data)
522{
523 struct lt9611 *pdata = (struct lt9611 *)data;
524
525 if (!cfg) {
526 pr_err("LT9611 get firmware failed\n");
527 return;
528 }
529
530 lt9611_firmware_upgrade(pdata, cfg);
531 release_firmware(cfg);
532}
533
534static int lt9611_parse_dt_modes(struct device_node *np,
535 struct list_head *head,
536 u32 *num_of_modes)
537{
538 int rc = 0;
539 struct drm_display_mode *mode;
540 u32 mode_count = 0;
541 struct device_node *node = NULL;
542 struct device_node *root_node = NULL;
543 u32 h_front_porch, h_pulse_width, h_back_porch;
544 u32 v_front_porch, v_pulse_width, v_back_porch;
545 bool h_active_high, v_active_high;
546 u32 flags = 0;
547
548 root_node = of_get_child_by_name(np, "lt,customize-modes");
549 if (!root_node) {
550 root_node = of_parse_phandle(np, "lt,customize-modes", 0);
551 if (!root_node) {
552 pr_info("No entry present for lt,customize-modes\n");
553 goto end;
554 }
555 }
556
557 for_each_child_of_node(root_node, node) {
558 rc = 0;
559 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
560 if (!mode) {
561 pr_err("Out of memory\n");
562 rc = -ENOMEM;
563 continue;
564 }
565
566 rc = of_property_read_u32(node, "lt,mode-h-active",
567 &mode->hdisplay);
568 if (rc) {
569 pr_err("failed to read h-active, rc=%d\n", rc);
570 goto fail;
571 }
572
573 rc = of_property_read_u32(node, "lt,mode-h-front-porch",
574 &h_front_porch);
575 if (rc) {
576 pr_err("failed to read h-front-porch, rc=%d\n", rc);
577 goto fail;
578 }
579
580 rc = of_property_read_u32(node, "lt,mode-h-pulse-width",
581 &h_pulse_width);
582 if (rc) {
583 pr_err("failed to read h-pulse-width, rc=%d\n", rc);
584 goto fail;
585 }
586
587 rc = of_property_read_u32(node, "lt,mode-h-back-porch",
588 &h_back_porch);
589 if (rc) {
590 pr_err("failed to read h-back-porch, rc=%d\n", rc);
591 goto fail;
592 }
593
594 h_active_high = of_property_read_bool(node,
595 "lt,mode-h-active-high");
596
597 rc = of_property_read_u32(node, "lt,mode-v-active",
598 &mode->vdisplay);
599 if (rc) {
600 pr_err("failed to read v-active, rc=%d\n", rc);
601 goto fail;
602 }
603
604 rc = of_property_read_u32(node, "lt,mode-v-front-porch",
605 &v_front_porch);
606 if (rc) {
607 pr_err("failed to read v-front-porch, rc=%d\n", rc);
608 goto fail;
609 }
610
611 rc = of_property_read_u32(node, "lt,mode-v-pulse-width",
612 &v_pulse_width);
613 if (rc) {
614 pr_err("failed to read v-pulse-width, rc=%d\n", rc);
615 goto fail;
616 }
617
618 rc = of_property_read_u32(node, "lt,mode-v-back-porch",
619 &v_back_porch);
620 if (rc) {
621 pr_err("failed to read v-back-porch, rc=%d\n", rc);
622 goto fail;
623 }
624
625 v_active_high = of_property_read_bool(node,
626 "lt,mode-v-active-high");
627
628 rc = of_property_read_u32(node, "lt,mode-refresh-rate",
629 &mode->vrefresh);
630 if (rc) {
631 pr_err("failed to read refresh-rate, rc=%d\n", rc);
632 goto fail;
633 }
634
635 rc = of_property_read_u32(node, "lt,mode-clock-in-khz",
636 &mode->clock);
637 if (rc) {
638 pr_err("failed to read clock, rc=%d\n", rc);
639 goto fail;
640 }
641
642 mode->hsync_start = mode->hdisplay + h_front_porch;
643 mode->hsync_end = mode->hsync_start + h_pulse_width;
644 mode->htotal = mode->hsync_end + h_back_porch;
645 mode->vsync_start = mode->vdisplay + v_front_porch;
646 mode->vsync_end = mode->vsync_start + v_pulse_width;
647 mode->vtotal = mode->vsync_end + v_back_porch;
648 if (h_active_high)
649 flags |= DRM_MODE_FLAG_PHSYNC;
650 else
651 flags |= DRM_MODE_FLAG_NHSYNC;
652 if (v_active_high)
653 flags |= DRM_MODE_FLAG_PVSYNC;
654 else
655 flags |= DRM_MODE_FLAG_NVSYNC;
656 mode->flags = flags;
657
658 if (!rc) {
659 mode_count++;
660 list_add_tail(&mode->head, head);
661 }
662
663 drm_mode_set_name(mode);
664
665 pr_debug("mode[%s] h[%d,%d,%d,%d] v[%d,%d,%d,%d] %d %x %dkHZ\n",
666 mode->name, mode->hdisplay, mode->hsync_start,
667 mode->hsync_end, mode->htotal, mode->vdisplay,
668 mode->vsync_start, mode->vsync_end, mode->vtotal,
669 mode->vrefresh, mode->flags, mode->clock);
670fail:
671 if (rc) {
672 kfree(mode);
673 continue;
674 }
675 }
676
677 if (num_of_modes)
678 *num_of_modes = mode_count;
679
680end:
681 return rc;
682}
683
684
685static int lt9611_parse_dt(struct device *dev,
686 struct lt9611 *pdata)
687{
688 struct device_node *np = dev->of_node;
689 struct device_node *end_node;
690 int ret = 0;
691
692 end_node = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
693 if (!end_node) {
694 pr_err("remote endpoint not found\n");
695 return -ENODEV;
696 }
697
698 pdata->host_node = of_graph_get_remote_port_parent(end_node);
699 of_node_put(end_node);
700 if (!pdata->host_node) {
701 pr_err("remote node not found\n");
702 return -ENODEV;
703 }
704 of_node_put(pdata->host_node);
705
706 pdata->irq_gpio =
707 of_get_named_gpio(np, "lt,irq-gpio", 0);
708 if (!gpio_is_valid(pdata->irq_gpio)) {
709 pr_err("irq gpio not specified\n");
710 ret = -EINVAL;
711 }
712 pr_debug("irq_gpio=%d\n", pdata->irq_gpio);
713
714 pdata->reset_gpio =
715 of_get_named_gpio(np, "lt,reset-gpio", 0);
716 if (!gpio_is_valid(pdata->reset_gpio)) {
717 pr_err("reset gpio not specified\n");
718 ret = -EINVAL;
719 }
720 pr_debug("reset_gpio=%d\n", pdata->reset_gpio);
721
722 pdata->hdmi_ps_gpio =
723 of_get_named_gpio(np, "lt,hdmi-ps-gpio", 0);
724 if (!gpio_is_valid(pdata->hdmi_ps_gpio))
725 pr_debug("hdmi ps gpio not specified\n");
726 else
727 pr_debug("hdmi_ps_gpio=%d\n", pdata->hdmi_ps_gpio);
728
729 pdata->hdmi_en_gpio =
730 of_get_named_gpio(np, "lt,hdmi-en-gpio", 0);
731 if (!gpio_is_valid(pdata->hdmi_en_gpio))
732 pr_debug("hdmi en gpio not specified\n");
733 else
734 pr_debug("hdmi_en_gpio=%d\n", pdata->hdmi_en_gpio);
735
736 pdata->ac_mode = of_property_read_bool(np, "lt,ac-mode");
737 pr_debug("ac_mode=%d\n", pdata->ac_mode);
738
739 pdata->non_pluggable = of_property_read_bool(np, "lt,non-pluggable");
740 pr_debug("non_pluggable = %d\n", pdata->non_pluggable);
741 if (pdata->non_pluggable) {
742 INIT_LIST_HEAD(&pdata->mode_list);
743 ret = lt9611_parse_dt_modes(np,
744 &pdata->mode_list, &pdata->num_of_modes);
745 }
746
747 return ret;
748}
749
750static int lt9611_gpio_configure(struct lt9611 *pdata, bool on)
751{
752 int ret = 0;
753
754 if (on) {
755 ret = gpio_request(pdata->reset_gpio,
756 "lt9611-reset-gpio");
757 if (ret) {
758 pr_err("lt9611 reset gpio request failed\n");
759 goto error;
760 }
761
762 ret = gpio_direction_output(pdata->reset_gpio, 0);
763 if (ret) {
764 pr_err("lt9611 reset gpio direction failed\n");
765 goto reset_error;
766 }
767
768 if (gpio_is_valid(pdata->hdmi_en_gpio)) {
769 ret = gpio_request(pdata->hdmi_en_gpio,
770 "lt9611-hdmi-en-gpio");
771 if (ret) {
772 pr_err("lt9611 hdmi en gpio request failed\n");
773 goto reset_error;
774 }
775
776 ret = gpio_direction_output(pdata->hdmi_en_gpio, 1);
777 if (ret) {
778 pr_err("lt9611 hdmi en gpio direction failed\n");
779 goto hdmi_en_error;
780 }
781 }
782
783 if (gpio_is_valid(pdata->hdmi_ps_gpio)) {
784 ret = gpio_request(pdata->hdmi_ps_gpio,
785 "lt9611-hdmi-ps-gpio");
786 if (ret) {
787 pr_err("lt9611 hdmi ps gpio request failed\n");
788 goto hdmi_en_error;
789 }
790
791 ret = gpio_direction_input(pdata->hdmi_ps_gpio);
792 if (ret) {
793 pr_err("lt9611 hdmi ps gpio direction failed\n");
794 goto hdmi_ps_error;
795 }
796 }
797
798 ret = gpio_request(pdata->irq_gpio, "lt9611-irq-gpio");
799 if (ret) {
800 pr_err("lt9611 irq gpio request failed\n");
801 goto hdmi_ps_error;
802 }
803
804 ret = gpio_direction_input(pdata->irq_gpio);
805 if (ret) {
806 pr_err("lt9611 irq gpio direction failed\n");
807 goto irq_error;
808 }
809 } else {
810 gpio_free(pdata->irq_gpio);
811 if (gpio_is_valid(pdata->hdmi_ps_gpio))
812 gpio_free(pdata->hdmi_ps_gpio);
813 if (gpio_is_valid(pdata->hdmi_en_gpio))
814 gpio_free(pdata->hdmi_en_gpio);
815 gpio_free(pdata->reset_gpio);
816 }
817
818 return ret;
819
820
821irq_error:
822 gpio_free(pdata->irq_gpio);
823hdmi_ps_error:
824 if (gpio_is_valid(pdata->hdmi_ps_gpio))
825 gpio_free(pdata->hdmi_ps_gpio);
826hdmi_en_error:
827 if (gpio_is_valid(pdata->hdmi_en_gpio))
828 gpio_free(pdata->hdmi_en_gpio);
829reset_error:
830 gpio_free(pdata->reset_gpio);
831error:
832 return ret;
833}
834
835static int lt9611_read_device_id(struct lt9611 *pdata)
836{
837 u8 rev0 = 0, rev1 = 0;
838 int ret = 0;
839
840 lt9611_write_byte(pdata, 0xFF, 0x80);
841 lt9611_write_byte(pdata, 0xEE, 0x01);
842 lt9611_write_byte(pdata, 0xFF, 0x81);
843
844 if (!lt9611_read(pdata, 0x00, &rev0, 1) &&
845 !lt9611_read(pdata, 0x01, &rev1, 1)) {
846 pr_info("LT9611 id: 0x%x\n", (rev0 << 8) | rev1);
847 } else {
848 pr_err("LT9611 get id failed\n");
849 ret = -1;
850 }
851
852 lt9611_write_byte(pdata, 0xFF, 0x80);
853 lt9611_write_byte(pdata, 0xEE, 0x00);
Yuan Zhao1831e752020-03-23 14:16:28 +0800854 msleep(50);
Wenjun Zhang2142e0c2019-10-28 15:41:08 +0800855
856 return ret;
857}
858
859static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id)
860{
Yuan Zhao1831e752020-03-23 14:16:28 +0800861 u8 irq_status = 0, hpd_status = 0;
862 struct lt9611 *pdata = (struct lt9611 *)dev_id;
863
864 lt9611_write_byte(pdata, 0xFF, 0x80);
865 lt9611_write_byte(pdata, 0xEE, 0x01);
866 lt9611_write_byte(pdata, 0xFF, 0xB0);
867 if (!lt9611_read(pdata, 0x22, &irq_status, 1)) {
868 pr_debug("irq status 0x%x\n", irq_status);
869 if (irq_status) {
870 lt9611_write_byte(pdata, 0x22, 0);
871 lt9611_read(pdata, 0x23, &hpd_status, 1);
872 pr_debug("irq hpd status 0x%x\n", hpd_status);
873 }
874 } else
875 pr_err("get irq status failed\n");
876 lt9611_write_byte(pdata, 0xFF, 0x80);
877 lt9611_write_byte(pdata, 0xEE, 0x00);
878
879 msleep(50);
880 if (irq_status & (BIT(0) | BIT(1)))
881 queue_work(pdata->wq, &pdata->work);
882
Wenjun Zhang2142e0c2019-10-28 15:41:08 +0800883 return IRQ_HANDLED;
884}
885
886static void lt9611_reset(struct lt9611 *pdata, bool on_off)
887{
888 pr_debug("reset: %d\n", on_off);
889 if (on_off) {
890 gpio_set_value(pdata->reset_gpio, 1);
891 msleep(20);
892 gpio_set_value(pdata->reset_gpio, 0);
893 msleep(20);
894 gpio_set_value(pdata->reset_gpio, 1);
Yuan Zhao1831e752020-03-23 14:16:28 +0800895 msleep(300);
Wenjun Zhang2142e0c2019-10-28 15:41:08 +0800896 } else {
897 gpio_set_value(pdata->reset_gpio, 0);
898 }
899}
900
901static void lt9611_assert_5v(struct lt9611 *pdata)
902{
903 if (gpio_is_valid(pdata->hdmi_en_gpio)) {
904 gpio_set_value(pdata->hdmi_en_gpio, 1);
905 msleep(20);
906 }
907}
908
909static int lt9611_config_vreg(struct device *dev,
910 struct lt9611_vreg *in_vreg, int num_vreg, bool config)
911{
912 int i = 0, rc = 0;
913 struct lt9611_vreg *curr_vreg = NULL;
914
915 if (!in_vreg || !num_vreg)
916 return rc;
917
918 if (config) {
919 for (i = 0; i < num_vreg; i++) {
920 curr_vreg = &in_vreg[i];
921 curr_vreg->vreg = regulator_get(dev,
922 curr_vreg->vreg_name);
923 rc = PTR_RET(curr_vreg->vreg);
924 if (rc) {
925 pr_err("%s get failed. rc=%d\n",
926 curr_vreg->vreg_name, rc);
927 curr_vreg->vreg = NULL;
928 goto vreg_get_fail;
929 }
930
931 rc = regulator_set_voltage(
932 curr_vreg->vreg,
933 curr_vreg->min_voltage,
934 curr_vreg->max_voltage);
935 if (rc < 0) {
936 pr_err("%s set vltg fail\n",
937 curr_vreg->vreg_name);
938 goto vreg_set_voltage_fail;
939 }
940 }
941 } else {
942 for (i = num_vreg-1; i >= 0; i--) {
943 curr_vreg = &in_vreg[i];
944 if (curr_vreg->vreg) {
945 regulator_set_voltage(curr_vreg->vreg,
946 0, curr_vreg->max_voltage);
947
948 regulator_put(curr_vreg->vreg);
949 curr_vreg->vreg = NULL;
950 }
951 }
952 }
953 return 0;
954
955vreg_unconfig:
956 regulator_set_load(curr_vreg->vreg, 0);
957
958vreg_set_voltage_fail:
959 regulator_put(curr_vreg->vreg);
960 curr_vreg->vreg = NULL;
961
962vreg_get_fail:
963 for (i--; i >= 0; i--) {
964 curr_vreg = &in_vreg[i];
965 goto vreg_unconfig;
966 }
967 return rc;
968}
969
970static int lt9611_get_dt_supply(struct device *dev,
971 struct lt9611 *pdata)
972{
973 int i = 0, rc = 0;
974 u32 tmp = 0;
975 struct device_node *of_node = NULL, *supply_root_node = NULL;
976 struct device_node *supply_node = NULL;
977
978 if (!dev || !pdata) {
979 pr_err("invalid input param dev:%pK pdata:%pK\n", dev, pdata);
980 return -EINVAL;
981 }
982
983 of_node = dev->of_node;
984
985 pdata->num_vreg = 0;
986 supply_root_node = of_get_child_by_name(of_node,
987 "lt,supply-entries");
988 if (!supply_root_node) {
989 pr_info("no supply entry present\n");
990 return 0;
991 }
992
993 pdata->num_vreg = of_get_available_child_count(supply_root_node);
994 if (pdata->num_vreg == 0) {
995 pr_info("no vreg present\n");
996 return 0;
997 }
998
999 pr_debug("vreg found. count=%d\n", pdata->num_vreg);
1000 pdata->vreg_config = devm_kzalloc(dev, sizeof(struct lt9611_vreg) *
1001 pdata->num_vreg, GFP_KERNEL);
1002 if (!pdata->vreg_config)
1003 return -ENOMEM;
1004
1005 for_each_available_child_of_node(supply_root_node, supply_node) {
1006 const char *st = NULL;
1007
1008 rc = of_property_read_string(supply_node,
1009 "lt,supply-name", &st);
1010 if (rc) {
1011 pr_err("error reading name. rc=%d\n", rc);
1012 goto error;
1013 }
1014
1015 strlcpy(pdata->vreg_config[i].vreg_name, st,
1016 sizeof(pdata->vreg_config[i].vreg_name));
1017
1018 rc = of_property_read_u32(supply_node,
1019 "lt,supply-min-voltage", &tmp);
1020 if (rc) {
1021 pr_err("error reading min volt. rc=%d\n", rc);
1022 goto error;
1023 }
1024 pdata->vreg_config[i].min_voltage = tmp;
1025
1026 rc = of_property_read_u32(supply_node,
1027 "lt,supply-max-voltage", &tmp);
1028 if (rc) {
1029 pr_err("error reading max volt. rc=%d\n", rc);
1030 goto error;
1031 }
1032 pdata->vreg_config[i].max_voltage = tmp;
1033
1034 rc = of_property_read_u32(supply_node,
1035 "lt,supply-enable-load", &tmp);
1036 if (rc)
1037 pr_debug("no supply enable load value. rc=%d\n", rc);
1038
1039 pdata->vreg_config[i].enable_load = (!rc ? tmp : 0);
1040
1041 rc = of_property_read_u32(supply_node,
1042 "lt,supply-disable-load", &tmp);
1043 if (rc)
1044 pr_debug("no supply disable load value. rc=%d\n", rc);
1045
1046 pdata->vreg_config[i].disable_load = (!rc ? tmp : 0);
1047
1048 rc = of_property_read_u32(supply_node,
1049 "lt,supply-pre-on-sleep", &tmp);
1050 if (rc)
1051 pr_debug("no supply pre on sleep value. rc=%d\n", rc);
1052
1053 pdata->vreg_config[i].pre_on_sleep = (!rc ? tmp : 0);
1054
1055 rc = of_property_read_u32(supply_node,
1056 "lt,supply-pre-off-sleep", &tmp);
1057 if (rc)
1058 pr_debug("no supply pre off sleep value. rc=%d\n", rc);
1059
1060 pdata->vreg_config[i].pre_off_sleep = (!rc ? tmp : 0);
1061
1062 rc = of_property_read_u32(supply_node,
1063 "lt,supply-post-on-sleep", &tmp);
1064 if (rc)
1065 pr_debug("no supply post on sleep value. rc=%d\n", rc);
1066
1067 pdata->vreg_config[i].post_on_sleep = (!rc ? tmp : 0);
1068
1069 rc = of_property_read_u32(supply_node,
1070 "lt,supply-post-off-sleep", &tmp);
1071 if (rc)
1072 pr_debug("no supply post off sleep value. rc=%d\n", rc);
1073
1074 pdata->vreg_config[i].post_off_sleep = (!rc ? tmp : 0);
1075
1076 pr_debug("%s min=%d, max=%d, enable=%d, disable=%d\n",
1077 pdata->vreg_config[i].vreg_name,
1078 pdata->vreg_config[i].min_voltage,
1079 pdata->vreg_config[i].max_voltage,
1080 pdata->vreg_config[i].enable_load,
1081 pdata->vreg_config[i].disable_load);
1082 ++i;
1083
1084 rc = 0;
1085 }
1086
1087 rc = lt9611_config_vreg(dev,
1088 pdata->vreg_config, pdata->num_vreg, true);
1089 if (rc)
1090 goto error;
1091
1092 return rc;
1093
1094error:
1095 if (pdata->vreg_config) {
1096 devm_kfree(dev, pdata->vreg_config);
1097 pdata->vreg_config = NULL;
1098 pdata->num_vreg = 0;
1099 }
1100
1101 return rc;
1102}
1103
1104static void lt9611_put_dt_supply(struct device *dev,
1105 struct lt9611 *pdata)
1106{
1107 if (!dev || !pdata) {
1108 pr_err("invalid input param dev:%pK pdata:%pK\n", dev, pdata);
1109 return;
1110 }
1111
1112 lt9611_config_vreg(dev,
1113 pdata->vreg_config, pdata->num_vreg, false);
1114
1115 if (pdata->vreg_config) {
1116 devm_kfree(dev, pdata->vreg_config);
1117 pdata->vreg_config = NULL;
1118 }
1119 pdata->num_vreg = 0;
1120}
1121
1122static int lt9611_enable_vreg(struct lt9611 *pdata, int enable)
1123{
1124 int i = 0, rc = 0;
1125 bool need_sleep;
1126 struct lt9611_vreg *in_vreg = pdata->vreg_config;
1127 int num_vreg = pdata->num_vreg;
1128
1129 if (enable) {
1130 for (i = 0; i < num_vreg; i++) {
1131 rc = PTR_RET(in_vreg[i].vreg);
1132 if (rc) {
1133 pr_err("%s regulator error. rc=%d\n",
1134 in_vreg[i].vreg_name, rc);
1135 goto vreg_set_opt_mode_fail;
1136 }
1137
1138 need_sleep = !regulator_is_enabled(in_vreg[i].vreg);
1139 if (in_vreg[i].pre_on_sleep && need_sleep)
1140 usleep_range(in_vreg[i].pre_on_sleep * 1000,
1141 in_vreg[i].pre_on_sleep * 1000);
1142
1143 rc = regulator_set_load(in_vreg[i].vreg,
1144 in_vreg[i].enable_load);
1145 if (rc < 0) {
1146 pr_err("%s set opt m fail\n",
1147 in_vreg[i].vreg_name);
1148 goto vreg_set_opt_mode_fail;
1149 }
1150
1151 rc = regulator_enable(in_vreg[i].vreg);
1152 if (in_vreg[i].post_on_sleep && need_sleep)
1153 usleep_range(in_vreg[i].post_on_sleep * 1000,
1154 in_vreg[i].post_on_sleep * 1000);
1155 if (rc < 0) {
1156 pr_err("%s enable failed\n",
1157 in_vreg[i].vreg_name);
1158 goto disable_vreg;
1159 }
1160 }
1161 } else {
1162 for (i = num_vreg-1; i >= 0; i--) {
1163 if (in_vreg[i].pre_off_sleep)
1164 usleep_range(in_vreg[i].pre_off_sleep * 1000,
1165 in_vreg[i].pre_off_sleep * 1000);
1166
1167 regulator_set_load(in_vreg[i].vreg,
1168 in_vreg[i].disable_load);
1169 regulator_disable(in_vreg[i].vreg);
1170
1171 if (in_vreg[i].post_off_sleep)
1172 usleep_range(in_vreg[i].post_off_sleep * 1000,
1173 in_vreg[i].post_off_sleep * 1000);
1174 }
1175 }
1176 return rc;
1177
1178disable_vreg:
1179 regulator_set_load(in_vreg[i].vreg, in_vreg[i].disable_load);
1180
1181vreg_set_opt_mode_fail:
1182 for (i--; i >= 0; i--) {
1183 if (in_vreg[i].pre_off_sleep)
1184 usleep_range(in_vreg[i].pre_off_sleep * 1000,
1185 in_vreg[i].pre_off_sleep * 1000);
1186
1187 regulator_set_load(in_vreg[i].vreg,
1188 in_vreg[i].disable_load);
1189 regulator_disable(in_vreg[i].vreg);
1190
1191 if (in_vreg[i].post_off_sleep)
1192 usleep_range(in_vreg[i].post_off_sleep * 1000,
1193 in_vreg[i].post_off_sleep * 1000);
1194 }
1195
1196 return rc;
1197}
1198
1199static struct lt9611_timing_info *lt9611_get_supported_timing(
1200 struct drm_display_mode *mode)
1201{
1202 int i = 0;
1203
1204 while (lt9611_supp_timing_cfg[i].xres != 0xffff) {
1205 if (lt9611_supp_timing_cfg[i].xres == mode->hdisplay &&
1206 lt9611_supp_timing_cfg[i].yres == mode->vdisplay &&
1207 lt9611_supp_timing_cfg[i].fps ==
1208 drm_mode_vrefresh(mode)) {
1209 return &lt9611_supp_timing_cfg[i];
1210 }
1211 i++;
1212 }
1213
1214 return NULL;
1215}
1216
1217/* TODO: intf/lane number needs info from both DSI host and client */
1218static int lt9611_get_intf_num(struct lt9611 *pdata,
1219 struct drm_display_mode *mode)
1220{
1221 int num_of_intfs = 0;
1222 struct lt9611_timing_info *timing =
1223 lt9611_get_supported_timing(mode);
1224
1225 if (timing)
1226 num_of_intfs = timing->intfs;
1227 else {
1228 pr_err("interface number not defined by bridge chip\n");
1229 num_of_intfs = 0;
1230 }
1231
1232 return num_of_intfs;
1233}
1234
1235static int lt9611_get_lane_num(struct lt9611 *pdata,
1236 struct drm_display_mode *mode)
1237{
1238 int num_of_lanes = 0;
1239 struct lt9611_timing_info *timing =
1240 lt9611_get_supported_timing(mode);
1241
1242 if (timing)
1243 num_of_lanes = timing->lanes;
1244 else {
1245 pr_err("lane number not defined by bridge chip\n");
1246 num_of_lanes = 0;
1247 }
1248
1249 return num_of_lanes;
1250}
1251
1252static void lt9611_get_video_cfg(struct lt9611 *pdata,
1253 struct drm_display_mode *mode,
1254 struct lt9611_video_cfg *video_cfg)
1255{
1256 int rc = 0;
1257 struct hdmi_avi_infoframe avi_frame;
1258
1259 memset(&avi_frame, 0, sizeof(avi_frame));
1260
1261 video_cfg->h_active = mode->hdisplay;
1262 video_cfg->v_active = mode->vdisplay;
1263 video_cfg->h_front_porch = mode->hsync_start - mode->hdisplay;
1264 video_cfg->v_front_porch = mode->vsync_start - mode->vdisplay;
1265 video_cfg->h_back_porch = mode->htotal - mode->hsync_end;
1266 video_cfg->v_back_porch = mode->vtotal - mode->vsync_end;
1267 video_cfg->h_pulse_width = mode->hsync_end - mode->hsync_start;
1268 video_cfg->v_pulse_width = mode->vsync_end - mode->vsync_start;
1269 video_cfg->pclk_khz = mode->clock;
1270
1271 video_cfg->h_polarity = !!(mode->flags & DRM_MODE_FLAG_PHSYNC);
1272 video_cfg->v_polarity = !!(mode->flags & DRM_MODE_FLAG_PVSYNC);
1273
1274 video_cfg->num_of_lanes = lt9611_get_lane_num(pdata, mode);
1275 video_cfg->num_of_intfs = lt9611_get_intf_num(pdata, mode);
1276
1277 pr_debug("video=h[%d,%d,%d,%d] v[%d,%d,%d,%d] pclk=%d lane=%d intf=%d\n",
1278 video_cfg->h_active, video_cfg->h_front_porch,
1279 video_cfg->h_pulse_width, video_cfg->h_back_porch,
1280 video_cfg->v_active, video_cfg->v_front_porch,
1281 video_cfg->v_pulse_width, video_cfg->v_back_porch,
1282 video_cfg->pclk_khz, video_cfg->num_of_lanes,
1283 video_cfg->num_of_intfs);
1284
1285 rc = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, mode, false);
1286 if (rc) {
1287 pr_err("get avi frame failed ret=%d\n", rc);
1288 } else {
1289 video_cfg->scaninfo = avi_frame.scan_mode;
1290 video_cfg->ar = avi_frame.picture_aspect;
1291 video_cfg->vic = avi_frame.video_code;
1292 pr_debug("scaninfo=%d ar=%d vic=%d\n",
1293 video_cfg->scaninfo, video_cfg->ar, video_cfg->vic);
1294 }
1295}
1296
1297/* connector funcs */
1298static enum drm_connector_status
1299lt9611_connector_detect(struct drm_connector *connector, bool force)
1300{
Yuan Zhao1831e752020-03-23 14:16:28 +08001301 u8 hpd_status = 0;
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001302 struct lt9611 *pdata = connector_to_lt9611(connector);
Yuan Zhao1831e752020-03-23 14:16:28 +08001303
1304 pdata->status = connector_status_disconnected;
1305 if (force) {
1306 lt9611_write_byte(pdata, 0xFF, 0x80);
1307 lt9611_write_byte(pdata, 0xEE, 0x01);
1308 lt9611_write_byte(pdata, 0xFF, 0xB0);
1309 if (!lt9611_read(pdata, 0x23, &hpd_status, 1)) {
1310 if (hpd_status & BIT(1))
1311 pdata->status = connector_status_connected;
1312 pr_debug("hpd status %x\n", hpd_status);
1313 } else
1314 pr_err("read hpd status failed\n");
1315 lt9611_write_byte(pdata, 0xFF, 0x80);
1316 lt9611_write_byte(pdata, 0xEE, 0x00);
1317 msleep(50);
1318 } else
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001319 pdata->status = connector_status_connected;
1320
1321 return pdata->status;
1322}
1323
1324static int lt9611_read_edid(struct lt9611 *pdata)
1325{
1326 return 0;
1327}
1328
1329/* TODO: add support for more extension blocks */
1330static int lt9611_get_edid_block(void *data, u8 *buf, unsigned int block,
1331 size_t len)
1332{
1333
1334 pr_info("get edid block: block=%d, len=%d\n", block, (int)len);
1335
1336 return 0;
1337}
1338
1339static void lt9611_set_preferred_mode(struct drm_connector *connector)
1340{
1341 struct lt9611 *pdata = connector_to_lt9611(connector);
1342 struct drm_display_mode *mode;
1343 const char *string;
1344
1345 /* use specified mode as preferred */
1346 if (!of_property_read_string(pdata->dev->of_node,
1347 "lt,preferred-mode", &string)) {
1348 list_for_each_entry(mode, &connector->probed_modes, head) {
1349 if (!strcmp(mode->name, string))
1350 mode->type |= DRM_MODE_TYPE_PREFERRED;
1351 }
1352 }
1353}
1354
1355static int lt9611_connector_get_modes(struct drm_connector *connector)
1356{
1357 struct lt9611 *pdata = connector_to_lt9611(connector);
1358 struct drm_display_mode *mode, *m;
1359 unsigned int count = 0;
1360
1361 if (pdata->non_pluggable) {
1362 list_for_each_entry(mode, &pdata->mode_list, head) {
1363 m = drm_mode_duplicate(connector->dev, mode);
1364 if (!m) {
1365 pr_err("failed to add hdmi mode %dx%d\n",
1366 mode->hdisplay, mode->vdisplay);
1367 break;
1368 }
1369 drm_mode_probed_add(connector, m);
1370 }
1371 count = pdata->num_of_modes;
1372 } else {
1373 struct edid *edid;
1374
1375 edid = drm_do_get_edid(connector, lt9611_get_edid_block, pdata);
1376
1377 drm_connector_update_edid_property(connector, edid);
1378 count = drm_add_edid_modes(connector, edid);
1379
1380 pdata->hdmi_mode = drm_detect_hdmi_monitor(edid);
1381 pr_info("hdmi_mode = %d\n", pdata->hdmi_mode);
1382
1383 kfree(edid);
1384 }
1385
1386 lt9611_set_preferred_mode(connector);
1387
1388 return count;
1389}
1390
1391static enum drm_mode_status lt9611_connector_mode_valid(
1392 struct drm_connector *connector, struct drm_display_mode *mode)
1393{
1394
1395 struct lt9611_timing_info *timing =
1396 lt9611_get_supported_timing(mode);
1397
1398 return timing ? MODE_OK : MODE_BAD;
1399}
1400
1401/* bridge funcs */
1402static void lt9611_bridge_enable(struct drm_bridge *bridge)
1403{
1404 pr_debug("bridge enable\n");
1405}
1406
1407static void lt9611_bridge_disable(struct drm_bridge *bridge)
1408{
1409 pr_debug("bridge disable\n");
1410}
1411
1412static void lt9611_bridge_mode_set(struct drm_bridge *bridge,
1413 struct drm_display_mode *mode,
1414 struct drm_display_mode *adj_mode)
1415{
1416 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1417 struct lt9611_video_cfg *video_cfg = &pdata->video_cfg;
1418 int ret = 0;
1419
1420 pr_debug(" hdisplay=%d, vdisplay=%d, vrefresh=%d, clock=%d\n",
1421 adj_mode->hdisplay, adj_mode->vdisplay,
1422 adj_mode->vrefresh, adj_mode->clock);
1423
1424 drm_mode_copy(&pdata->curr_mode, adj_mode);
1425
1426 memset(video_cfg, 0, sizeof(struct lt9611_video_cfg));
1427 lt9611_get_video_cfg(pdata, adj_mode, video_cfg);
1428
1429 /* TODO: update intf number of host */
1430 if (video_cfg->num_of_lanes != pdata->dsi->lanes) {
1431 mipi_dsi_detach(pdata->dsi);
1432 pdata->dsi->lanes = video_cfg->num_of_lanes;
1433 ret = mipi_dsi_attach(pdata->dsi);
1434 if (ret)
1435 pr_err("failed to change host lanes\n");
1436 }
1437}
1438
1439static const struct drm_connector_helper_funcs
1440 lt9611_connector_helper_funcs = {
1441 .get_modes = lt9611_connector_get_modes,
1442 .mode_valid = lt9611_connector_mode_valid,
1443};
1444
1445
1446static const struct drm_connector_funcs lt9611_connector_funcs = {
1447 .fill_modes = drm_helper_probe_single_connector_modes,
1448 .detect = lt9611_connector_detect,
1449 .destroy = drm_connector_cleanup,
1450 .reset = drm_atomic_helper_connector_reset,
1451 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1452 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1453};
1454
1455
1456static int lt9611_bridge_attach(struct drm_bridge *bridge)
1457{
1458 struct mipi_dsi_host *host;
1459 struct mipi_dsi_device *dsi;
1460 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1461 int ret;
1462 const struct mipi_dsi_device_info info = { .type = "lt9611",
1463 .channel = 0,
1464 .node = NULL,
1465 };
1466
1467 if (!bridge->encoder) {
1468 DRM_ERROR("Parent encoder object not found");
1469 return -ENODEV;
1470 }
1471
1472 ret = drm_connector_init(bridge->dev, &pdata->connector,
1473 &lt9611_connector_funcs,
1474 DRM_MODE_CONNECTOR_HDMIA);
1475 if (ret) {
1476 DRM_ERROR("Failed to initialize connector: %d\n", ret);
1477 return ret;
1478 }
1479
1480 drm_connector_helper_add(&pdata->connector,
1481 &lt9611_connector_helper_funcs);
1482
1483 ret = drm_connector_register(&pdata->connector);
1484 if (ret) {
1485 DRM_ERROR("Failed to register connector: %d\n", ret);
1486 return ret;
1487 }
1488
1489 pdata->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
1490
1491 ret = drm_connector_attach_encoder(&pdata->connector,
1492 bridge->encoder);
1493 if (ret) {
1494 DRM_ERROR("Failed to link up connector to encoder: %d\n", ret);
1495 return ret;
1496 }
1497
1498 host = of_find_mipi_dsi_host_by_node(pdata->host_node);
1499 if (!host) {
1500 pr_err("failed to find dsi host\n");
1501 return -EPROBE_DEFER;
1502 }
1503
1504 dsi = mipi_dsi_device_register_full(host, &info);
1505 if (IS_ERR(dsi)) {
1506 pr_err("failed to create dsi device\n");
1507 ret = PTR_ERR(dsi);
1508 goto err_dsi_device;
1509 }
1510
1511 dsi->lanes = 4;
1512 dsi->format = MIPI_DSI_FMT_RGB888;
1513 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
1514 MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_VIDEO_BLLP |
1515 MIPI_DSI_MODE_VIDEO_EOF_BLLP;
1516
1517 ret = mipi_dsi_attach(dsi);
1518 if (ret < 0) {
1519 pr_err("failed to attach dsi to host\n");
1520 goto err_dsi_attach;
1521 }
1522
1523 pdata->dsi = dsi;
1524
1525 return 0;
1526
1527err_dsi_attach:
1528 mipi_dsi_device_unregister(dsi);
1529err_dsi_device:
1530 return ret;
1531}
1532
1533static void lt9611_bridge_pre_enable(struct drm_bridge *bridge)
1534{
1535 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1536
1537 pr_debug("bridge pre_enable\n");
1538 lt9611_reset(pdata, true);
1539}
1540
1541static bool lt9611_bridge_mode_fixup(struct drm_bridge *bridge,
1542 const struct drm_display_mode *mode,
1543 struct drm_display_mode *adjusted_mode)
1544{
1545 return true;
1546}
1547
1548static void lt9611_bridge_post_disable(struct drm_bridge *bridge)
1549{
1550 pr_debug("bridge post_disable\n");
1551
1552}
1553
1554static const struct drm_bridge_funcs lt9611_bridge_funcs = {
1555 .attach = lt9611_bridge_attach,
1556 .mode_fixup = lt9611_bridge_mode_fixup,
1557 .pre_enable = lt9611_bridge_pre_enable,
1558 .enable = lt9611_bridge_enable,
1559 .disable = lt9611_bridge_disable,
1560 .post_disable = lt9611_bridge_post_disable,
1561 .mode_set = lt9611_bridge_mode_set,
1562};
1563
1564/* sysfs */
1565static int lt9611_dump_debug_info(struct lt9611 *pdata)
1566{
1567 if (!pdata->power_on) {
1568 pr_err("device is not power on\n");
1569 return -EINVAL;
1570 }
1571
1572 lt9611_read_edid(pdata);
1573
1574 return 0;
1575}
1576
1577static ssize_t dump_info_store(struct device *dev,
1578 struct device_attribute *attr,
1579 const char *buf,
1580 size_t count)
1581{
1582 struct lt9611 *pdata = dev_get_drvdata(dev);
1583
1584 if (!pdata) {
1585 pr_err("pdata is NULL\n");
1586 return -EINVAL;
1587 }
1588
1589 lt9611_dump_debug_info(pdata);
1590
1591 return count;
1592}
1593
1594static ssize_t firmware_upgrade_store(struct device *dev,
1595 struct device_attribute *attr,
1596 const char *buf,
1597 size_t count)
1598{
1599 struct lt9611 *pdata = dev_get_drvdata(dev);
1600 int ret = 0;
1601
1602 if (!pdata) {
1603 pr_err("pdata is NULL\n");
1604 return -EINVAL;
1605 }
1606
1607 ret = request_firmware_nowait(THIS_MODULE, true,
1608 "lt9611_fw.bin", &pdata->i2c_client->dev, GFP_KERNEL, pdata,
1609 lt9611_firmware_cb);
1610 if (ret)
1611 dev_err(&pdata->i2c_client->dev,
1612 "Failed to invoke firmware loader: %d\n", ret);
1613 else
1614 pr_info("LT9611 starts upgrade, waiting for about 40s...\n");
1615
1616 return count;
1617}
1618
1619static ssize_t firmware_upgrade_show(struct device *dev,
1620 struct device_attribute *attr, char *buf)
1621{
1622 struct lt9611 *pdata = dev_get_drvdata(dev);
1623
1624 return snprintf(buf, 4, "%d\n", pdata->fw_status);
1625}
1626
1627static DEVICE_ATTR_WO(dump_info);
1628static DEVICE_ATTR_RW(firmware_upgrade);
1629
1630static struct attribute *lt9611_sysfs_attrs[] = {
1631 &dev_attr_dump_info.attr,
1632 &dev_attr_firmware_upgrade.attr,
1633 NULL,
1634};
1635
1636static struct attribute_group lt9611_sysfs_attr_grp = {
1637 .attrs = lt9611_sysfs_attrs,
1638};
1639
1640static int lt9611_sysfs_init(struct device *dev)
1641{
1642 int rc = 0;
1643
1644 if (!dev) {
1645 pr_err("%s: Invalid params\n", __func__);
1646 return -EINVAL;
1647 }
1648
1649 rc = sysfs_create_group(&dev->kobj, &lt9611_sysfs_attr_grp);
1650 if (rc)
1651 pr_err("%s: sysfs group creation failed %d\n", __func__, rc);
1652
1653 return rc;
1654}
1655
1656static void lt9611_sysfs_remove(struct device *dev)
1657{
1658 if (!dev) {
1659 pr_err("%s: Invalid params\n", __func__);
1660 return;
1661 }
1662
1663 sysfs_remove_group(&dev->kobj, &lt9611_sysfs_attr_grp);
1664}
1665
1666static int lt9611_probe(struct i2c_client *client,
1667 const struct i2c_device_id *id)
1668{
1669 struct lt9611 *pdata;
1670 int ret = 0;
1671
1672 if (!client || !client->dev.of_node) {
1673 pr_err("invalid input\n");
1674 return -EINVAL;
1675 }
1676
1677 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1678 pr_err("device doesn't support I2C\n");
1679 return -ENODEV;
1680 }
1681
1682 pdata = devm_kzalloc(&client->dev,
1683 sizeof(struct lt9611), GFP_KERNEL);
1684 if (!pdata)
1685 return -ENOMEM;
1686
1687 ret = lt9611_parse_dt(&client->dev, pdata);
1688 if (ret) {
1689 pr_err("failed to parse device tree\n");
1690 goto err_dt_parse;
1691 }
1692
1693 ret = lt9611_get_dt_supply(&client->dev, pdata);
1694 if (ret) {
1695 pr_err("failed to get dt supply\n");
1696 goto err_dt_parse;
1697 }
1698
1699 pdata->dev = &client->dev;
1700 pdata->i2c_client = client;
1701
1702 ret = lt9611_gpio_configure(pdata, true);
1703 if (ret) {
1704 pr_err("failed to configure GPIOs\n");
1705 goto err_dt_supply;
1706 }
1707
1708 lt9611_assert_5v(pdata);
1709
1710 ret = lt9611_enable_vreg(pdata, true);
1711 if (ret) {
1712 pr_err("failed to enable vreg\n");
1713 goto err_i2c_prog;
1714 }
1715
1716 lt9611_reset(pdata, true);
1717
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001718 ret = lt9611_read_device_id(pdata);
1719 if (ret) {
1720 pr_err("failed to read chip rev\n");
Yuan Zhao1831e752020-03-23 14:16:28 +08001721 goto err_i2c_prog;
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001722 }
1723
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001724 i2c_set_clientdata(client, pdata);
1725 dev_set_drvdata(&client->dev, pdata);
1726
GG Houfc792522020-03-16 15:35:49 +08001727 ret = lt9611_sysfs_init(&client->dev);
1728 if (ret) {
1729 pr_err("sysfs init failed\n");
Yuan Zhao1831e752020-03-23 14:16:28 +08001730 goto err_i2c_prog;
GG Houfc792522020-03-16 15:35:49 +08001731 }
1732
1733 if (lt9611_get_version(pdata)) {
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001734 pr_info("LT9611 works, no need to upgrade FW\n");
1735 } else {
1736 ret = request_firmware_nowait(THIS_MODULE, true,
1737 "lt9611_fw.bin", &client->dev, GFP_KERNEL, pdata,
1738 lt9611_firmware_cb);
1739 if (ret) {
1740 dev_err(&client->dev,
1741 "Failed to invoke firmware loader: %d\n", ret);
Yuan Zhao1831e752020-03-23 14:16:28 +08001742 goto err_i2c_prog;
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001743 } else
1744 return 0;
1745 }
1746
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001747#if IS_ENABLED(CONFIG_OF)
1748 pdata->bridge.of_node = client->dev.of_node;
1749#endif
1750
1751 pdata->bridge.funcs = &lt9611_bridge_funcs;
1752 drm_bridge_add(&pdata->bridge);
1753
Yuan Zhao1831e752020-03-23 14:16:28 +08001754 pdata->wq = create_singlethread_workqueue("lt9611_wk");
1755 if (!pdata->wq) {
1756 pr_err("Error creating lt9611 wq\n");
1757 goto err_i2c_prog;
1758 }
1759 INIT_WORK(&pdata->work, lt9611_hpd_work);
1760
1761 pdata->irq = gpio_to_irq(pdata->irq_gpio);
1762 ret = request_threaded_irq(pdata->irq, NULL, lt9611_irq_thread_handler,
1763 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lt9611_irq", pdata);
1764 if (ret) {
1765 pr_err("failed to request irq\n");
1766 goto err_i2c_prog;
1767 }
1768
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001769 return 0;
1770
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001771err_i2c_prog:
1772 lt9611_gpio_configure(pdata, false);
1773err_dt_supply:
1774 lt9611_put_dt_supply(&client->dev, pdata);
1775err_dt_parse:
1776 devm_kfree(&client->dev, pdata);
1777 return ret;
1778}
1779
1780static int lt9611_remove(struct i2c_client *client)
1781{
1782 int ret = -EINVAL;
1783 struct lt9611 *pdata = i2c_get_clientdata(client);
1784 struct drm_display_mode *mode, *n;
1785
1786 if (!pdata)
1787 goto end;
1788
1789 mipi_dsi_detach(pdata->dsi);
1790 mipi_dsi_device_unregister(pdata->dsi);
1791
1792 drm_bridge_remove(&pdata->bridge);
1793
1794 lt9611_sysfs_remove(&client->dev);
1795
1796 disable_irq(pdata->irq);
1797 free_irq(pdata->irq, pdata);
1798
1799 ret = lt9611_gpio_configure(pdata, false);
1800
1801 lt9611_put_dt_supply(&client->dev, pdata);
1802
1803 if (pdata->non_pluggable) {
1804 list_for_each_entry_safe(mode, n, &pdata->mode_list, head) {
1805 list_del(&mode->head);
1806 kfree(mode);
1807 }
1808 }
1809
1810 devm_kfree(&client->dev, pdata);
Yuan Zhao1831e752020-03-23 14:16:28 +08001811 if (pdata->wq)
1812 destroy_workqueue(pdata->wq);
Wenjun Zhang2142e0c2019-10-28 15:41:08 +08001813end:
1814 return ret;
1815}
1816
1817
1818static struct i2c_device_id lt9611_id[] = {
1819 { "lt,lt9611uxc", 0},
1820 {}
1821};
1822
1823static const struct of_device_id lt9611_match_table[] = {
1824 {.compatible = "lt,lt9611uxc"},
1825 {}
1826};
1827MODULE_DEVICE_TABLE(of, lt9611_match_table);
1828
1829static struct i2c_driver lt9611_driver = {
1830 .driver = {
1831 .name = "lt9611",
1832 .of_match_table = lt9611_match_table,
1833 },
1834 .probe = lt9611_probe,
1835 .remove = lt9611_remove,
1836 .id_table = lt9611_id,
1837};
1838
1839static int __init lt9611_init(void)
1840{
1841 return i2c_add_driver(&lt9611_driver);
1842}
1843
1844static void __exit lt9611_exit(void)
1845{
1846 i2c_del_driver(&lt9611_driver);
1847}
1848
1849module_init(lt9611_init);
1850module_exit(lt9611_exit);
1851