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