blob: 1a12b0bb9434aabcc90b4df0d77cbe76d47c409b [file] [log] [blame]
Ray Zhangebb763a2018-01-22 10:46:40 +08001/* Copyright (c) 2018, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/platform_device.h>
22#include <linux/fs.h>
23#include <linux/delay.h>
24#include <linux/i2c.h>
25#include <linux/gpio.h>
26#include <linux/interrupt.h>
Ray Zhang3436c0d2018-03-06 15:41:40 +080027#include <linux/component.h>
Ray Zhangebb763a2018-01-22 10:46:40 +080028#include <linux/of_gpio.h>
29#include <linux/of_graph.h>
30#include <linux/of_irq.h>
31#include <linux/regulator/consumer.h>
32#include <linux/hdmi.h>
33#include <drm/drmP.h>
34#include <drm/drm_atomic.h>
35#include <drm/drm_atomic_helper.h>
36#include <drm/drm_edid.h>
37#include <drm/drm_mipi_dsi.h>
38
39
40#define CFG_HPD_INTERRUPTS BIT(0)
41#define CFG_EDID_INTERRUPTS BIT(1)
42#define CFG_CEC_INTERRUPTS BIT(2)
43#define CFG_VID_CHK_INTERRUPTS BIT(3)
44
45#define EDID_SEG_SIZE 256
46
47struct lt9611_reg_cfg {
48 u8 reg;
49 u8 val;
50 int sleep_in_ms;
51};
52
53struct lt9611_vreg {
54 struct regulator *vreg; /* vreg handle */
55 char vreg_name[32];
56 int min_voltage;
57 int max_voltage;
58 int enable_load;
59 int disable_load;
60 int pre_on_sleep;
61 int post_on_sleep;
62 int pre_off_sleep;
63 int post_off_sleep;
64};
65
66struct lt9611_video_cfg {
67 u32 h_active;
68 u32 h_front_porch;
69 u32 h_pulse_width;
70 u32 h_back_porch;
71 bool h_polarity;
72 u32 v_active;
73 u32 v_front_porch;
74 u32 v_pulse_width;
75 u32 v_back_porch;
76 bool v_polarity;
77 u32 pclk_khz;
78 bool interlaced;
79 u32 vic;
80 enum hdmi_picture_aspect ar;
81 u32 num_of_lanes;
82 u32 num_of_intfs;
83 u8 scaninfo;
84};
85
86struct lt9611 {
87 struct device *dev;
88 struct drm_bridge bridge;
89
90 struct device_node *host_node;
91 struct mipi_dsi_device *dsi;
92
93 u8 i2c_addr;
94 int irq;
95 bool ac_mode;
96
97 u32 irq_gpio;
98 u32 reset_gpio;
99 u32 hdmi_ps_gpio;
100 u32 hdmi_en_gpio;
101
102 unsigned int num_vreg;
103 struct lt9611_vreg *vreg_config;
104
105 struct i2c_client *i2c_client;
106
107 enum drm_connector_status status;
108 bool power_on;
109
110 /* get display modes from device tree */
111 bool non_pluggable;
112 u32 num_of_modes;
113 struct list_head mode_list;
114
115 struct drm_display_mode curr_mode;
116 struct lt9611_video_cfg video_cfg;
117
118 u8 edid_buf[EDID_SEG_SIZE];
119 bool hdmi_mode;
120};
121
122static struct lt9611_reg_cfg lt9611_init_setup[] = {
123 /* LT9611_System_Init */
124 {0xFF, 0x81, 0},
125 {0x01, 0x18, 0}, /* sel xtal clock */
126
127 /* timer for frequency meter */
128 {0xff, 0x82, 0},
129 {0x1b, 0x69, 0}, /*timer 2*/
130 {0x1c, 0x78, 0},
131 {0xcb, 0x69, 0}, /*timer 1 */
132 {0xcc, 0x78, 0},
133
134 /* irq init */
135 {0xff, 0x82, 0},
136 {0x51, 0x01, 0},
137 {0x58, 0x0a, 0}, /* hpd irq */
138 {0x59, 0x80, 0}, /* hpd debounce width */
139 {0x9e, 0xf7, 0}, /* video check irq */
140
141 /* power consumption for work */
142 {0xff, 0x80, 0},
143 {0x04, 0xf0, 0},
144 {0x06, 0xf0, 0},
145 {0x0a, 0x80, 0},
146 {0x0b, 0x40, 0},
147 {0x0d, 0xef, 0},
148 {0x11, 0xfa, 0},
149};
150
151struct lt9611_timing_info {
152 u16 xres;
153 u16 yres;
154 u8 bpp;
155 u8 fps;
156 u8 lanes;
157 u8 intfs;
158};
159
160static struct lt9611_timing_info lt9611_supp_timing_cfg[] = {
161 {3840, 2160, 24, 30, 4, 2}, /* 3840x2160 24bit 30Hz 4Lane 2ports */
162 {1920, 1080, 24, 60, 4, 1}, /* 1080P 24bit 60Hz 4lane 1port */
163 {1920, 1080, 24, 30, 3, 1}, /* 1080P 24bit 30Hz 3lane 1port */
164 {1920, 1080, 24, 24, 3, 1},
165 {720, 480, 24, 60, 2, 1},
166 {720, 576, 24, 50, 2, 1},
167 {640, 480, 24, 60, 2, 1},
168 {0xffff, 0xffff, 0xff, 0xff, 0xff},
169};
170
171static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge)
172{
173 return container_of(bridge, struct lt9611, bridge);
174}
175
176static struct lt9611 *connector_to_lt9611(struct drm_connector *connector)
177{
178 WARN_ON(!connector->private);
179
180 return bridge_to_lt9611(connector->private);
181}
182
183static int lt9611_write(struct lt9611 *pdata, u8 reg, u8 val)
184{
185 struct i2c_client *client = pdata->i2c_client;
186 u8 buf[2] = {reg, val};
187 struct i2c_msg msg = {
188 .addr = client->addr,
189 .flags = 0,
190 .len = 2,
191 .buf = buf,
192 };
193
194 if (i2c_transfer(client->adapter, &msg, 1) < 1) {
195 pr_err("i2c write failed\n");
196 return -EIO;
197 }
198
199 return 0;
200}
201
202static int lt9611_read(struct lt9611 *pdata, u8 reg, char *buf, u32 size)
203{
204 struct i2c_client *client = pdata->i2c_client;
205 struct i2c_msg msg[2] = {
206 {
207 .addr = client->addr,
208 .flags = 0,
209 .len = 1,
210 .buf = &reg,
211 },
212 {
213 .addr = client->addr,
214 .flags = I2C_M_RD,
215 .len = size,
216 .buf = buf,
217 }
218 };
219
220 if (i2c_transfer(client->adapter, msg, 2) != 2) {
221 pr_err("i2c read failed\n");
222 return -EIO;
223 }
224
225 return 0;
226}
227
228static int lt9611_write_array(struct lt9611 *pdata,
229 struct lt9611_reg_cfg *cfg, int size)
230{
231 int ret = 0;
232 int i;
233
234 size = size / sizeof(struct lt9611_reg_cfg);
235 for (i = 0; i < size; i++) {
236 ret = lt9611_write(pdata, cfg[i].reg, cfg[i].val);
237
238 if (ret != 0) {
239 pr_err("reg writes failed. Last write %02X to %02X\n",
240 cfg[i].val, cfg[i].reg);
241 goto w_regs_fail;
242 }
243
244 if (cfg[i].sleep_in_ms)
245 msleep(cfg[i].sleep_in_ms);
246 }
247
248w_regs_fail:
249 if (ret != 0)
250 pr_err("exiting with ret = %d after %d writes\n", ret, i);
251
252 return ret;
253}
254
255static int lt9611_parse_dt_modes(struct device_node *np,
256 struct list_head *head,
257 u32 *num_of_modes)
258{
259 int rc = 0;
260 struct drm_display_mode *mode;
261 u32 mode_count = 0;
262 struct device_node *node = NULL;
263 struct device_node *root_node = NULL;
264 u32 h_front_porch, h_pulse_width, h_back_porch;
265 u32 v_front_porch, v_pulse_width, v_back_porch;
266 bool h_active_high, v_active_high;
267 u32 flags = 0;
268
269 root_node = of_get_child_by_name(np, "lt,customize-modes");
270 if (!root_node) {
271 root_node = of_parse_phandle(np, "lt,customize-modes", 0);
272 if (!root_node) {
273 pr_info("No entry present for lt,customize-modes");
274 goto end;
275 }
276 }
277
278 for_each_child_of_node(root_node, node) {
279 rc = 0;
280 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
281 if (!mode) {
282 pr_err("Out of memory\n");
283 rc = -ENOMEM;
284 continue;
285 }
286
287 rc = of_property_read_u32(node, "lt,mode-h-active",
288 &mode->hdisplay);
289 if (rc) {
290 pr_err("failed to read h-active, rc=%d\n", rc);
291 goto fail;
292 }
293
294 rc = of_property_read_u32(node, "lt,mode-h-front-porch",
295 &h_front_porch);
296 if (rc) {
297 pr_err("failed to read h-front-porch, rc=%d\n", rc);
298 goto fail;
299 }
300
301 rc = of_property_read_u32(node, "lt,mode-h-pulse-width",
302 &h_pulse_width);
303 if (rc) {
304 pr_err("failed to read h-pulse-width, rc=%d\n", rc);
305 goto fail;
306 }
307
308 rc = of_property_read_u32(node, "lt,mode-h-back-porch",
309 &h_back_porch);
310 if (rc) {
311 pr_err("failed to read h-back-porch, rc=%d\n", rc);
312 goto fail;
313 }
314
315 h_active_high = of_property_read_bool(node,
316 "lt,mode-h-active-high");
317
318 rc = of_property_read_u32(node, "lt,mode-v-active",
319 &mode->vdisplay);
320 if (rc) {
321 pr_err("failed to read v-active, rc=%d\n", rc);
322 goto fail;
323 }
324
325 rc = of_property_read_u32(node, "lt,mode-v-front-porch",
326 &v_front_porch);
327 if (rc) {
328 pr_err("failed to read v-front-porch, rc=%d\n", rc);
329 goto fail;
330 }
331
332 rc = of_property_read_u32(node, "lt,mode-v-pulse-width",
333 &v_pulse_width);
334 if (rc) {
335 pr_err("failed to read v-pulse-width, rc=%d\n", rc);
336 goto fail;
337 }
338
339 rc = of_property_read_u32(node, "lt,mode-v-back-porch",
340 &v_back_porch);
341 if (rc) {
342 pr_err("failed to read v-back-porch, rc=%d\n", rc);
343 goto fail;
344 }
345
346 v_active_high = of_property_read_bool(node,
347 "lt,mode-v-active-high");
348
349 rc = of_property_read_u32(node, "lt,mode-refresh-rate",
350 &mode->vrefresh);
351 if (rc) {
352 pr_err("failed to read refresh-rate, rc=%d\n", rc);
353 goto fail;
354 }
355
356 rc = of_property_read_u32(node, "lt,mode-clock-in-khz",
357 &mode->clock);
358 if (rc) {
359 pr_err("failed to read clock, rc=%d\n", rc);
360 goto fail;
361 }
362
363 mode->hsync_start = mode->hdisplay + h_front_porch;
364 mode->hsync_end = mode->hsync_start + h_pulse_width;
365 mode->htotal = mode->hsync_end + h_back_porch;
366 mode->vsync_start = mode->vdisplay + v_front_porch;
367 mode->vsync_end = mode->vsync_start + v_pulse_width;
368 mode->vtotal = mode->vsync_end + v_back_porch;
369 if (h_active_high)
370 flags |= DRM_MODE_FLAG_PHSYNC;
371 else
372 flags |= DRM_MODE_FLAG_NHSYNC;
373 if (v_active_high)
374 flags |= DRM_MODE_FLAG_PVSYNC;
375 else
376 flags |= DRM_MODE_FLAG_NVSYNC;
377 mode->flags = flags;
378
379 if (!rc) {
380 mode_count++;
381 list_add_tail(&mode->head, head);
382 }
383
384 drm_mode_set_name(mode);
385
386 pr_debug("mode[%s] h[%d,%d,%d,%d] v[%d,%d,%d,%d] %d %x %dkHZ\n",
387 mode->name, mode->hdisplay, mode->hsync_start,
388 mode->hsync_end, mode->htotal, mode->vdisplay,
389 mode->vsync_start, mode->vsync_end, mode->vtotal,
390 mode->vrefresh, mode->flags, mode->clock);
391fail:
392 if (rc) {
393 kfree(mode);
394 continue;
395 }
396 }
397
398 if (num_of_modes)
399 *num_of_modes = mode_count;
400
401end:
402 return rc;
403}
404
405
406static int lt9611_parse_dt(struct device *dev,
407 struct lt9611 *pdata)
408{
409 struct device_node *np = dev->of_node;
410 struct device_node *end_node;
411 int ret = 0;
412
413 end_node = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
414 if (!end_node) {
415 pr_err("remote endpoint not found\n");
416 return -ENODEV;
417 }
418
419 pdata->host_node = of_graph_get_remote_port_parent(end_node);
420 of_node_put(end_node);
421 if (!pdata->host_node) {
422 pr_err("remote node not found\n");
423 return -ENODEV;
424 }
425 of_node_put(pdata->host_node);
426
427 pdata->irq_gpio =
428 of_get_named_gpio(np, "lt,irq-gpio", 0);
429 if (!gpio_is_valid(pdata->irq_gpio)) {
430 pr_err("irq gpio not specified\n");
431 ret = -EINVAL;
432 }
433 pr_debug("irq_gpio=%d\n", pdata->irq_gpio);
434
435 pdata->reset_gpio =
436 of_get_named_gpio(np, "lt,reset-gpio", 0);
437 if (!gpio_is_valid(pdata->reset_gpio)) {
438 pr_err("reset gpio not specified\n");
439 ret = -EINVAL;
440 }
441 pr_debug("reset_gpio=%d\n", pdata->reset_gpio);
442
443 pdata->hdmi_ps_gpio =
444 of_get_named_gpio(np, "lt,hdmi-ps-gpio", 0);
445 if (!gpio_is_valid(pdata->hdmi_ps_gpio))
446 pr_debug("hdmi ps gpio not specified\n");
447 else
448 pr_debug("hdmi_ps_gpio=%d\n", pdata->hdmi_ps_gpio);
449
450 pdata->hdmi_en_gpio =
451 of_get_named_gpio(np, "lt,hdmi-en-gpio", 0);
452 if (!gpio_is_valid(pdata->hdmi_en_gpio))
453 pr_debug("hdmi en gpio not specified\n");
454 else
455 pr_debug("hdmi_en_gpio=%d\n", pdata->hdmi_en_gpio);
456
457 pdata->ac_mode = of_property_read_bool(np, "lt,ac-mode");
458 pr_debug("ac_mode=%d\n", pdata->ac_mode);
459
460 pdata->non_pluggable = of_property_read_bool(np, "lt,non-pluggable");
461 pr_debug("non_pluggable = %d\n", pdata->non_pluggable);
462 if (pdata->non_pluggable) {
463 INIT_LIST_HEAD(&pdata->mode_list);
464 ret = lt9611_parse_dt_modes(np,
465 &pdata->mode_list, &pdata->num_of_modes);
466 }
467
468 return ret;
469}
470
471static int lt9611_gpio_configure(struct lt9611 *pdata, bool on)
472{
473 int ret = 0;
474
475 if (on) {
476 ret = gpio_request(pdata->reset_gpio,
477 "lt9611-reset-gpio");
478 if (ret) {
479 pr_err("lt9611 reset gpio request failed\n");
480 goto error;
481 }
482
483 ret = gpio_direction_output(pdata->reset_gpio, 0);
484 if (ret) {
485 pr_err("lt9611 reset gpio direction failed\n");
486 goto reset_error;
487 }
488
489 if (gpio_is_valid(pdata->hdmi_en_gpio)) {
490 ret = gpio_request(pdata->hdmi_en_gpio,
491 "lt9611-hdmi-en-gpio");
492 if (ret) {
493 pr_err("lt9611 hdmi en gpio request failed\n");
494 goto reset_error;
495 }
496
497 ret = gpio_direction_output(pdata->hdmi_en_gpio, 1);
498 if (ret) {
499 pr_err("lt9611 hdmi en gpio direction failed\n");
500 goto hdmi_en_error;
501 }
502 }
503
504 if (gpio_is_valid(pdata->hdmi_ps_gpio)) {
505 ret = gpio_request(pdata->hdmi_ps_gpio,
506 "lt9611-hdmi-ps-gpio");
507 if (ret) {
508 pr_err("lt9611 hdmi ps gpio request failed\n");
509 goto hdmi_en_error;
510 }
511
512 ret = gpio_direction_input(pdata->hdmi_ps_gpio);
513 if (ret) {
514 pr_err("lt9611 hdmi ps gpio direction failed\n");
515 goto hdmi_ps_error;
516 }
517 }
518
519 ret = gpio_request(pdata->irq_gpio, "lt9611-irq-gpio");
520 if (ret) {
521 pr_err("lt9611 irq gpio request failed\n");
522 goto hdmi_ps_error;
523 }
524
525 ret = gpio_direction_input(pdata->irq_gpio);
526 if (ret) {
527 pr_err("lt9611 irq gpio direction failed\n");
528 goto irq_error;
529 }
530 } else {
531 gpio_free(pdata->irq_gpio);
532 if (gpio_is_valid(pdata->hdmi_ps_gpio))
533 gpio_free(pdata->hdmi_ps_gpio);
534 if (gpio_is_valid(pdata->hdmi_en_gpio))
535 gpio_free(pdata->hdmi_en_gpio);
536 gpio_free(pdata->reset_gpio);
537 }
538
539 return ret;
540
541
542irq_error:
543 gpio_free(pdata->irq_gpio);
544hdmi_ps_error:
545 if (gpio_is_valid(pdata->hdmi_ps_gpio))
546 gpio_free(pdata->hdmi_ps_gpio);
547hdmi_en_error:
548 if (gpio_is_valid(pdata->hdmi_en_gpio))
549 gpio_free(pdata->hdmi_en_gpio);
550reset_error:
551 gpio_free(pdata->reset_gpio);
552error:
553 return ret;
554}
555
556static int lt9611_read_device_rev(struct lt9611 *pdata)
557{
558 u8 rev = 0;
559 int ret = 0;
560
561 lt9611_write(pdata, 0xff, 0x80);
562 lt9611_write(pdata, 0xee, 0x01);
563
564 ret = lt9611_read(pdata, 0x02, &rev, 1);
565
566 if (ret == 0)
567 pr_info("LT9611 revsion: 0x%x\n", rev);
568
569 return ret;
570}
571
572static int lt9611_mipi_input_analog(struct lt9611 *pdata,
573 struct lt9611_video_cfg *cfg)
574{
575 struct lt9611_reg_cfg reg_cfg[] = {
576 {0xff, 0x81, 0},
577 {0x06, 0x40, 0}, /*port A rx current*/
578 {0x0a, 0xfe, 0}, /*port A ldo voltage set*/
579 {0x0b, 0xbf, 0}, /*enable port A lprx*/
580 {0x11, 0x40, 0}, /*port B rx current*/
581 {0x15, 0xfe, 0}, /*port B ldo voltage set*/
582 {0x16, 0xbf, 0}, /*enable port B lprx*/
583
584 {0x1c, 0x03, 0}, /*PortA clk lane no-LP mode*/
585 {0x20, 0x03, 0}, /*PortB clk lane with-LP mode*/
586 };
587
588 if (!pdata || !cfg) {
589 pr_err("invalid input\n");
590 return -EINVAL;
591 }
592
593 lt9611_write_array(pdata, reg_cfg, sizeof(reg_cfg));
594
595 return 0;
596}
597
598static int lt9611_mipi_input_digital(struct lt9611 *pdata,
599 struct lt9611_video_cfg *cfg)
600{
601 u8 lanes = 0;
602 u8 ports = 0;
603 struct lt9611_reg_cfg reg_cfg[] = {
604 {0xff, 0x82, 0},
605 {0x4f, 0x80, 0},
606 {0x50, 0x10, 0},
607 {0xff, 0x83, 0},
608
609 {0x02, 0x0a, 0},
610 {0x06, 0x0a, 0},
611 };
612
613 if (!pdata || !cfg) {
614 pr_err("invalid input\n");
615 return -EINVAL;
616 }
617
618 lanes = cfg->num_of_lanes;
619 ports = cfg->num_of_intfs;
620
621 lt9611_write(pdata, 0xff, 0x83);
622 if (lanes == 4)
623 lt9611_write(pdata, 0x00, 0x00);
624 else if (lanes < 4)
625 lt9611_write(pdata, 0x00, lanes);
626 else {
627 pr_err("invalid lane count\n");
628 return -EINVAL;
629 }
630
631 if (ports == 1)
632 lt9611_write(pdata, 0x0a, 0x00);
633 else if (ports == 2)
634 lt9611_write(pdata, 0x0a, 0x03);
635 else {
636 pr_err("invalid port count\n");
637 return -EINVAL;
638 }
639
640 lt9611_write_array(pdata, reg_cfg, sizeof(reg_cfg));
641
642 return 0;
643}
644
645static void lt9611_mipi_video_setup(struct lt9611 *pdata,
646 struct lt9611_video_cfg *cfg)
647{
648 u32 h_total, h_act, hpw, hfp, hss;
649 u32 v_total, v_act, vpw, vfp, vss;
650
651 if (!pdata || !cfg) {
652 pr_err("invalid input\n");
653 return;
654 }
655
656 h_total = cfg->h_active + cfg->h_front_porch +
657 cfg->h_pulse_width + cfg->h_back_porch;
658 v_total = cfg->v_active + cfg->v_front_porch +
659 cfg->v_pulse_width + cfg->v_back_porch;
660
661 h_act = cfg->h_active;
662 hpw = cfg->h_pulse_width;
663 hfp = cfg->h_front_porch;
664 hss = cfg->h_pulse_width + cfg->h_back_porch;
665
666 v_act = cfg->v_active;
667 vpw = cfg->v_pulse_width;
668 vfp = cfg->v_front_porch;
669 vss = cfg->v_pulse_width + cfg->v_back_porch;
670
671 pr_debug("h_total=%d, h_active=%d, hfp=%d, hpw=%d, hbp=%d\n",
672 h_total, cfg->h_active, cfg->h_front_porch,
673 cfg->h_pulse_width, cfg->h_back_porch);
674
675 pr_debug("v_total=%d, v_active=%d, vfp=%d, vpw=%d, vbp=%d\n",
676 v_total, cfg->v_active, cfg->v_front_porch,
677 cfg->v_pulse_width, cfg->v_back_porch);
678
679 lt9611_write(pdata, 0xff, 0x83);
680
681 lt9611_write(pdata, 0x0d, (u8)(v_total / 256));
682 lt9611_write(pdata, 0x0e, (u8)(v_total % 256));
683
684 lt9611_write(pdata, 0x0f, (u8)(v_act / 256));
685 lt9611_write(pdata, 0x10, (u8)(v_act % 256));
686
687 lt9611_write(pdata, 0x11, (u8)(h_total / 256));
688 lt9611_write(pdata, 0x12, (u8)(h_total % 256));
689
690 lt9611_write(pdata, 0x13, (u8)(h_act / 256));
691 lt9611_write(pdata, 0x14, (u8)(h_act % 256));
692
693 lt9611_write(pdata, 0x15, (u8)(vpw % 256));
694 lt9611_write(pdata, 0x16, (u8)(hpw % 256));
695
696 lt9611_write(pdata, 0x17, (u8)(vfp % 256));
697
698 lt9611_write(pdata, 0x18, (u8)(vss % 256));
699
700 lt9611_write(pdata, 0x19, (u8)(hfp % 256));
701
702 lt9611_write(pdata, 0x1a, (u8)(hss / 256));
703 lt9611_write(pdata, 0x1b, (u8)(hss % 256));
704}
705
706static int lt9611_pcr_setup(struct lt9611 *pdata,
707 struct lt9611_video_cfg *cfg)
708{
709 u32 h_act = 0;
710 struct lt9611_reg_cfg reg_cfg[] = {
711 {0xff, 0x83, 0},
Ray Zhang56039f52018-03-07 11:02:21 +0800712 {0x0b, 0x01, 0},
713 {0x0c, 0x10, 0},
714 {0x48, 0x00, 0},
715 {0x49, 0x81, 0},
Ray Zhangebb763a2018-01-22 10:46:40 +0800716
Ray Zhang56039f52018-03-07 11:02:21 +0800717 /* stage 1 */
718 {0x21, 0x4a, 0},
719 {0x24, 0x71, 0},
720 {0x25, 0x30, 0},
721 {0x2a, 0x01, 0},
Ray Zhangebb763a2018-01-22 10:46:40 +0800722
Ray Zhang56039f52018-03-07 11:02:21 +0800723 /* stage 2 */
724 {0x4a, 0x40, 0},
725 {0x1d, 0x10, 0},
726
727 /* MK limit */
728 {0x2d, 0x38, 0},
729 {0x31, 0x08, 0},
Ray Zhangebb763a2018-01-22 10:46:40 +0800730 };
731
732 if (!pdata || !cfg) {
733 pr_err("invalid input\n");
734 return -EINVAL;
735 }
736
Ray Zhang56039f52018-03-07 11:02:21 +0800737 lt9611_write_array(pdata, reg_cfg, sizeof(reg_cfg));
738
Ray Zhangebb763a2018-01-22 10:46:40 +0800739 h_act = cfg->h_active;
740
741 if (h_act == 1920) {
Ray Zhangebb763a2018-01-22 10:46:40 +0800742 lt9611_write(pdata, 0x26, 0x37);
Ray Zhangebb763a2018-01-22 10:46:40 +0800743 } else if (h_act == 3840) {
Ray Zhangebb763a2018-01-22 10:46:40 +0800744 lt9611_write(pdata, 0x0b, 0x03);
745 lt9611_write(pdata, 0x0c, 0xd0);
746 lt9611_write(pdata, 0x48, 0x03);
Ray Zhang56039f52018-03-07 11:02:21 +0800747 lt9611_write(pdata, 0x49, 0xe0);
748 lt9611_write(pdata, 0x24, 0x72);
749 lt9611_write(pdata, 0x25, 0x00);
750 lt9611_write(pdata, 0x2a, 0x01);
Ray Zhangebb763a2018-01-22 10:46:40 +0800751 lt9611_write(pdata, 0x4a, 0x10);
752 lt9611_write(pdata, 0x1d, 0x10);
Ray Zhang56039f52018-03-07 11:02:21 +0800753 lt9611_write(pdata, 0x26, 0x37);
754 } else if (h_act == 640) {
755 lt9611_write(pdata, 0x26, 0x14);
Ray Zhangebb763a2018-01-22 10:46:40 +0800756 }
757
Ray Zhang56039f52018-03-07 11:02:21 +0800758 /* pcr rst */
759 lt9611_write(pdata, 0xff, 0x80);
760 lt9611_write(pdata, 0x11, 0x5a);
761 lt9611_write(pdata, 0x11, 0xfa);
Ray Zhangebb763a2018-01-22 10:46:40 +0800762
763 return 0;
764}
765
766static int lt9611_pll_setup(struct lt9611 *pdata,
767 struct lt9611_video_cfg *cfg)
768{
769 u32 pclk = 0;
770 struct lt9611_reg_cfg reg_cfg[] = {
771 /* txpll init */
772 {0xff, 0x81, 0},
773 {0x23, 0x40, 0},
774 {0x24, 0x64, 0},
775 {0x25, 0x80, 0},
776 {0x26, 0x55, 0},
777 {0x2c, 0x37, 0},
778 {0x2f, 0x01, 0},
779 {0x26, 0x55, 0},
780 {0x27, 0x66, 0},
781 {0x28, 0x88, 0},
782 };
783
784 if (!pdata || !cfg) {
785 pr_err("invalid input\n");
786 return -EINVAL;
787 }
788
789 pclk = cfg->pclk_khz;
790
791 lt9611_write_array(pdata, reg_cfg, sizeof(reg_cfg));
792
793 if (pclk > 150000)
794 lt9611_write(pdata, 0x2d, 0x88);
795 else if (pclk > 70000)
796 lt9611_write(pdata, 0x2d, 0x99);
797 else
798 lt9611_write(pdata, 0x2d, 0xaa);
799
800 lt9611_write(pdata, 0xff, 0x82);
801 pclk = pclk / 2;
802 lt9611_write(pdata, 0xe3, pclk/65536); /* pclk[19:16] */
803 pclk = pclk % 65536;
804 lt9611_write(pdata, 0xe4, pclk/256); /* pclk[15:8] */
805 lt9611_write(pdata, 0xe5, pclk%256); /* pclk[7:0] */
806
807 lt9611_write(pdata, 0xde, 0x20);
808 lt9611_write(pdata, 0xde, 0xe0);
809
810 lt9611_write(pdata, 0xff, 0x80);
811 lt9611_write(pdata, 0x16, 0xf1);
812 lt9611_write(pdata, 0x16, 0xf3);
813
814 return 0;
815}
816
817static int lt9611_video_check(struct lt9611 *pdata)
818{
819 int ret = 0;
820 u32 v_total, v_act, h_act_a, h_act_b, h_total_sysclk;
821 u8 temp = 0;
822
823 if (!pdata) {
824 pr_err("invalid input\n");
825 return -EINVAL;
826 }
827
828 /* top module video check */
829 lt9611_write(pdata, 0xff, 0x82);
830
831 /* v_act */
832 ret = lt9611_read(pdata, 0x82, &temp, 1);
833 if (ret)
834 goto end;
835
836 v_act = temp << 8;
837 ret = lt9611_read(pdata, 0x83, &temp, 1);
838 if (ret)
839 goto end;
840 v_act = v_act + temp;
841
842 /* v_total */
843 ret = lt9611_read(pdata, 0x6c, &temp, 1);
844 if (ret)
845 goto end;
846 v_total = temp << 8;
847 ret = lt9611_read(pdata, 0x6d, &temp, 1);
848 if (ret)
849 goto end;
850 v_total = v_total + temp;
851
852 /* h_total_sysclk */
853 ret = lt9611_read(pdata, 0x86, &temp, 1);
854 if (ret)
855 goto end;
856 h_total_sysclk = temp << 8;
857 ret = lt9611_read(pdata, 0x87, &temp, 1);
858 if (ret)
859 goto end;
860 h_total_sysclk = h_total_sysclk + temp;
861
862 /* h_act_a */
863 lt9611_write(pdata, 0xff, 0x83);
864 ret = lt9611_read(pdata, 0x82, &temp, 1);
865 if (ret)
866 goto end;
867 h_act_a = temp << 8;
868 ret = lt9611_read(pdata, 0x83, &temp, 1);
869 if (ret)
870 goto end;
871 h_act_a = (h_act_a + temp)/3;
872
873 /* h_act_b */
874 lt9611_write(pdata, 0xff, 0x83);
875 ret = lt9611_read(pdata, 0x86, &temp, 1);
876 if (ret)
877 goto end;
878 h_act_b = temp << 8;
879 ret = lt9611_read(pdata, 0x87, &temp, 1);
880 if (ret)
881 goto end;
882 h_act_b = (h_act_b + temp)/3;
883
884 pr_info("video check: h_act_a=%d, h_act_b=%d, v_act=%d, v_total=%d, h_total_sysclk=%d\n",
885 h_act_a, h_act_b, v_act, v_total, h_total_sysclk);
886
887 return 0;
888
889end:
890 pr_err("read video check error\n");
891 return ret;
892}
893
894static int lt9611_hdmi_tx_digital(struct lt9611 *pdata,
895 struct lt9611_video_cfg *cfg)
896{
897 int ret = -EINVAL;
898 u32 checksum, vic;
899
900 if (!pdata || !cfg) {
901 pr_err("invalid input\n");
902 return ret;
903 }
904
905 vic = cfg->vic;
906 checksum = 0x46 - vic;
907
908 lt9611_write(pdata, 0xff, 0x84);
909 lt9611_write(pdata, 0x43, checksum);
910 lt9611_write(pdata, 0x44, 0x84);
911 lt9611_write(pdata, 0x47, vic);
912
913 lt9611_write(pdata, 0xff, 0x82);
914 lt9611_write(pdata, 0xd6, 0x8c);
915 lt9611_write(pdata, 0xd7, 0x04);
916
917 return ret;
918}
919
920static int lt9611_hdmi_tx_phy(struct lt9611 *pdata,
921 struct lt9611_video_cfg *cfg)
922{
923 int ret = -EINVAL;
924 struct lt9611_reg_cfg reg_cfg[] = {
925 {0xff, 0x81, 0},
926 {0x30, 0x6a, 0},
927 {0x31, 0x44, 0}, /* HDMI DC mode */
928 {0x32, 0x4a, 0},
929 {0x33, 0x0b, 0},
930 {0x34, 0x00, 0},
931 {0x35, 0x00, 0},
932 {0x36, 0x00, 0},
933 {0x37, 0x44, 0},
934 {0x3f, 0x0f, 0},
935 {0x40, 0xa0, 0},
936 {0x41, 0xa0, 0},
937 {0x42, 0xa0, 0},
938 {0x43, 0xa0, 0},
939 {0x44, 0x0a, 0},
940 };
941
942 if (!pdata || !cfg) {
943 pr_err("invalid input\n");
944 return ret;
945 }
946
947 /* HDMI AC mode */
948 if (pdata->ac_mode)
949 reg_cfg[2].val = 0x73;
950
951 lt9611_write_array(pdata, reg_cfg, sizeof(reg_cfg));
952
953 return ret;
954}
955
956static void lt9611_hdmi_output_enable(struct lt9611 *pdata)
957{
958 lt9611_write(pdata, 0xff, 0x81);
959 lt9611_write(pdata, 0x30, 0xea);
960}
961
962static void lt9611_hdmi_output_disable(struct lt9611 *pdata)
963{
964 lt9611_write(pdata, 0xff, 0x81);
965 lt9611_write(pdata, 0x30, 0x6a);
966}
967
968static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id)
969{
970 struct lt9611 *pdata = dev_id;
971 u8 irq_flag0 = 0;
972 u8 irq_flag3 = 0;
973
974 lt9611_write(pdata, 0xff, 0x82);
975 lt9611_read(pdata, 0x0f, &irq_flag3, 1);
976 lt9611_read(pdata, 0x0c, &irq_flag0, 1);
977
978 /* hpd changed low */
979 if (irq_flag3 & 0x80) {
980 pr_info("hdmi cable disconnected\n");
981
982 lt9611_write(pdata, 0xff, 0x82); /* irq 3 clear flag */
983 lt9611_write(pdata, 0x07, 0xbf);
984 lt9611_write(pdata, 0x07, 0x3f);
985 }
986 /* hpd changed high */
987 if (irq_flag3 & 0x40) {
988 pr_info("hdmi cable connected\n");
989
990 lt9611_write(pdata, 0xff, 0x82); /* irq 3 clear flag */
991 lt9611_write(pdata, 0x07, 0x7f);
992 lt9611_write(pdata, 0x07, 0x3f);
993 }
994
995 /* video input changed */
996 if (irq_flag0 & 0x01) {
997 pr_info("video input changed\n");
998 lt9611_write(pdata, 0xff, 0x82); /* irq 0 clear flag */
999 lt9611_write(pdata, 0x9e, 0xff);
1000 lt9611_write(pdata, 0x9e, 0xf7);
1001 lt9611_write(pdata, 0x04, 0xff);
1002 lt9611_write(pdata, 0x04, 0xfe);
1003 }
1004
1005 return IRQ_HANDLED;
1006}
1007
1008static int lt9611_enable_interrupts(struct lt9611 *pdata, int interrupts)
1009{
1010 int ret = 0;
1011 u8 reg_val = 0;
1012 u8 init_reg_val;
1013
1014 if (!pdata) {
1015 pr_err("invalid input\n");
1016 goto end;
1017 }
1018
1019 if (interrupts & CFG_VID_CHK_INTERRUPTS) {
1020 lt9611_write(pdata, 0xff, 0x82);
1021 lt9611_read(pdata, 0x00, &reg_val, 1);
1022
1023 if (reg_val & 0x01) {
1024 init_reg_val = reg_val & 0xfe;
1025 pr_debug("enabling video check interrupts\n");
1026 lt9611_write(pdata, 0x00, init_reg_val);
1027 }
1028 lt9611_write(pdata, 0x04, 0xff); /* clear */
1029 lt9611_write(pdata, 0x04, 0xfe);
1030 }
1031
1032 if (interrupts & CFG_HPD_INTERRUPTS) {
1033 lt9611_write(pdata, 0xff, 0x82);
1034 lt9611_read(pdata, 0x03, &reg_val, 1);
1035
1036 if (reg_val & 0xc0) { //reg_val | 0xc0???
1037 init_reg_val = reg_val & 0x3f;
1038 pr_debug("enabling hpd interrupts\n");
1039 lt9611_write(pdata, 0x03, init_reg_val);
1040 }
1041
1042 lt9611_write(pdata, 0x07, 0xff); //clear
1043 lt9611_write(pdata, 0x07, 0x3f);
1044 }
1045
1046end:
1047 return ret;
1048}
1049
1050static void lt9611_pcr_mk_debug(struct lt9611 *pdata)
1051{
1052 u8 m = 0, k1 = 0, k2 = 0, k3 = 0;
1053
1054 lt9611_write(pdata, 0xff, 0x83);
1055 lt9611_read(pdata, 0xb4, &m, 1);
1056 lt9611_read(pdata, 0xb5, &k1, 1);
1057 lt9611_read(pdata, 0xb6, &k2, 1);
1058 lt9611_read(pdata, 0xb7, &k3, 1);
1059
1060 pr_info("pcr mk:0x%x 0x%x 0x%x 0x%x\n",
1061 m, k1, k2, k3);
1062}
1063
1064static void lt9611_sleep_setup(struct lt9611 *pdata)
1065{
1066 struct lt9611_reg_cfg sleep_setup[] = {
1067 {0xff, 0x80, 0}, //register I2C addr
1068 {0x24, 0x76, 0},
1069 {0x23, 0x01, 0},
1070 {0xff, 0x81, 0}, //set addr pin as output
1071 {0x57, 0x03, 0},
1072 {0x49, 0x0b, 0},
1073 {0xff, 0x81, 0}, //anlog power down
1074 {0x51, 0x30, 0}, //disable IRQ
1075 {0x02, 0x48, 0}, //MIPI Rx power down
1076 {0x23, 0x80, 0},
1077 {0x30, 0x00, 0},
1078 {0x00, 0x01, 0}, //bandgap power down
1079 {0x01, 0x00, 0}, //system clk power down
1080 };
1081
1082 pr_err("sleep\n");
1083
1084 lt9611_write_array(pdata, sleep_setup, sizeof(sleep_setup));
1085}
1086
1087static int lt9611_power_on(struct lt9611 *pdata, bool on)
1088{
1089 int ret = 0;
1090
1091 pr_debug("power_on: on=%d\n", on);
1092
1093 if (on && !pdata->power_on) {
1094 lt9611_write_array(pdata, lt9611_init_setup,
1095 sizeof(lt9611_init_setup));
1096
1097 ret = lt9611_enable_interrupts(pdata, CFG_HPD_INTERRUPTS);
1098 if (ret) {
1099 pr_err("Failed to enable HPD intr %d\n", ret);
1100 return ret;
1101 }
1102 pdata->power_on = true;
1103 } else if (!on) {
1104 lt9611_write(pdata, 0xff, 0x81);
1105 lt9611_write(pdata, 0x30, 0x6a);
1106
1107 pdata->power_on = false;
1108 }
1109
1110 return ret;
1111}
1112
1113static int lt9611_video_on(struct lt9611 *pdata, bool on)
1114{
1115 int ret = 0;
1116 struct lt9611_video_cfg *cfg = &pdata->video_cfg;
1117
1118 pr_debug("on=%d\n", on);
1119
1120 if (on) {
1121 lt9611_mipi_input_analog(pdata, cfg);
1122 lt9611_mipi_input_digital(pdata, cfg);
1123 lt9611_pll_setup(pdata, cfg);
1124 lt9611_mipi_video_setup(pdata, cfg);
1125 lt9611_pcr_setup(pdata, cfg);
1126 lt9611_hdmi_tx_digital(pdata, cfg);
1127 lt9611_hdmi_tx_phy(pdata, cfg);
1128
1129 msleep(500);
1130
1131 lt9611_video_check(pdata);
1132 lt9611_hdmi_output_enable(pdata);
1133 } else {
1134 lt9611_hdmi_output_disable(pdata);
1135 }
1136
1137 return ret;
1138}
1139
1140static void lt9611_mipi_byte_clk_debug(struct lt9611 *pdata)
1141{
1142 u8 reg_val = 0;
1143 u32 byte_clk;
1144
1145 /* port A byte clk meter */
1146 lt9611_write(pdata, 0xff, 0x82);
1147 lt9611_write(pdata, 0xc7, 0x03); /* port A */
1148 msleep(50);
1149 lt9611_read(pdata, 0xcd, &reg_val, 1);
1150
1151 if ((reg_val & 0x60) == 0x60) {
1152 byte_clk = (reg_val & 0x0f) * 65536;
1153 lt9611_read(pdata, 0xce, &reg_val, 1);
1154 byte_clk = byte_clk + reg_val * 256;
1155 lt9611_read(pdata, 0xcf, &reg_val, 1);
1156 byte_clk = byte_clk + reg_val;
1157
1158 pr_info("port A byte clk = %d khz,\n", byte_clk);
1159 } else
1160 pr_info("port A byte clk unstable\n");
1161
1162 /* port B byte clk meter */
1163 lt9611_write(pdata, 0xff, 0x82);
1164 lt9611_write(pdata, 0xc7, 0x04); /* port B */
1165 msleep(50);
1166 lt9611_read(pdata, 0xcd, &reg_val, 1);
1167
1168 if ((reg_val & 0x60) == 0x60) {
1169 byte_clk = (reg_val & 0x0f) * 65536;
1170 lt9611_read(pdata, 0xce, &reg_val, 1);
1171 byte_clk = byte_clk + reg_val * 256;
1172 lt9611_read(pdata, 0xcf, &reg_val, 1);
1173 byte_clk = byte_clk + reg_val;
1174
1175 pr_info("port B byte clk = %d khz,\n", byte_clk);
1176 } else
1177 pr_info("port B byte clk unstable\n");
1178}
1179
1180static void lt9611_reset(struct lt9611 *pdata)
1181{
1182 gpio_set_value(pdata->reset_gpio, 1);
1183 msleep(20);
1184 gpio_set_value(pdata->reset_gpio, 0);
1185 msleep(20);
1186 gpio_set_value(pdata->reset_gpio, 1);
1187 msleep(20);
1188}
1189
1190static void lt9611_assert_5v(struct lt9611 *pdata)
1191{
1192 if (gpio_is_valid(pdata->hdmi_en_gpio)) {
1193 gpio_set_value(pdata->hdmi_en_gpio, 1);
1194 msleep(20);
1195 }
1196}
1197
1198static int lt9611_config_vreg(struct device *dev,
1199 struct lt9611_vreg *in_vreg, int num_vreg, bool config)
1200{
1201 int i = 0, rc = 0;
1202 struct lt9611_vreg *curr_vreg = NULL;
1203
1204 if (!in_vreg || !num_vreg)
1205 return rc;
1206
1207 if (config) {
1208 for (i = 0; i < num_vreg; i++) {
1209 curr_vreg = &in_vreg[i];
1210 curr_vreg->vreg = regulator_get(dev,
1211 curr_vreg->vreg_name);
1212 rc = PTR_RET(curr_vreg->vreg);
1213 if (rc) {
1214 pr_err("%s get failed. rc=%d\n",
1215 curr_vreg->vreg_name, rc);
1216 curr_vreg->vreg = NULL;
1217 goto vreg_get_fail;
1218 }
1219
1220 rc = regulator_set_voltage(
1221 curr_vreg->vreg,
1222 curr_vreg->min_voltage,
1223 curr_vreg->max_voltage);
1224 if (rc < 0) {
1225 pr_err("%s set vltg fail\n",
1226 curr_vreg->vreg_name);
1227 goto vreg_set_voltage_fail;
1228 }
1229 }
1230 } else {
1231 for (i = num_vreg-1; i >= 0; i--) {
1232 curr_vreg = &in_vreg[i];
1233 if (curr_vreg->vreg) {
1234 regulator_set_voltage(curr_vreg->vreg,
1235 0, curr_vreg->max_voltage);
1236
1237 regulator_put(curr_vreg->vreg);
1238 curr_vreg->vreg = NULL;
1239 }
1240 }
1241 }
1242 return 0;
1243
1244vreg_unconfig:
1245 regulator_set_load(curr_vreg->vreg, 0);
1246
1247vreg_set_voltage_fail:
1248 regulator_put(curr_vreg->vreg);
1249 curr_vreg->vreg = NULL;
1250
1251vreg_get_fail:
1252 for (i--; i >= 0; i--) {
1253 curr_vreg = &in_vreg[i];
1254 goto vreg_unconfig;
1255 }
1256 return rc;
1257}
1258
1259static int lt9611_get_dt_supply(struct device *dev,
1260 struct lt9611 *pdata)
1261{
1262 int i = 0, rc = 0;
1263 u32 tmp = 0;
1264 struct device_node *of_node = NULL, *supply_root_node = NULL;
1265 struct device_node *supply_node = NULL;
1266
1267 if (!dev || !pdata) {
1268 pr_err("invalid input param dev:%pK pdata:%pK\n", dev, pdata);
1269 return -EINVAL;
1270 }
1271
1272 of_node = dev->of_node;
1273
1274 pdata->num_vreg = 0;
1275 supply_root_node = of_get_child_by_name(of_node,
1276 "lt,supply-entries");
1277 if (!supply_root_node) {
1278 pr_info("no supply entry present\n");
1279 return 0;
1280 }
1281
1282 pdata->num_vreg = of_get_available_child_count(supply_root_node);
1283 if (pdata->num_vreg == 0) {
1284 pr_info("no vreg present\n");
1285 return 0;
1286 }
1287
1288 pr_debug("vreg found. count=%d\n", pdata->num_vreg);
1289 pdata->vreg_config = devm_kzalloc(dev, sizeof(struct lt9611_vreg) *
1290 pdata->num_vreg, GFP_KERNEL);
1291 if (!pdata->vreg_config)
1292 return -ENOMEM;
1293
1294 for_each_available_child_of_node(supply_root_node, supply_node) {
1295 const char *st = NULL;
1296
1297 rc = of_property_read_string(supply_node,
1298 "lt,supply-name", &st);
1299 if (rc) {
1300 pr_err("error reading name. rc=%d\n", rc);
1301 goto error;
1302 }
1303
1304 strlcpy(pdata->vreg_config[i].vreg_name, st,
1305 sizeof(pdata->vreg_config[i].vreg_name));
1306
1307 rc = of_property_read_u32(supply_node,
1308 "lt,supply-min-voltage", &tmp);
1309 if (rc) {
1310 pr_err("error reading min volt. rc=%d\n", rc);
1311 goto error;
1312 }
1313 pdata->vreg_config[i].min_voltage = tmp;
1314
1315 rc = of_property_read_u32(supply_node,
1316 "lt,supply-max-voltage", &tmp);
1317 if (rc) {
1318 pr_err("error reading max volt. rc=%d\n", rc);
1319 goto error;
1320 }
1321 pdata->vreg_config[i].max_voltage = tmp;
1322
1323 rc = of_property_read_u32(supply_node,
1324 "lt,supply-enable-load", &tmp);
1325 if (rc)
1326 pr_debug("no supply enable load value. rc=%d\n", rc);
1327
1328 pdata->vreg_config[i].enable_load = (!rc ? tmp : 0);
1329
1330 rc = of_property_read_u32(supply_node,
1331 "lt,supply-disable-load", &tmp);
1332 if (rc)
1333 pr_debug("no supply disable load value. rc=%d\n", rc);
1334
1335 pdata->vreg_config[i].disable_load = (!rc ? tmp : 0);
1336
1337 rc = of_property_read_u32(supply_node,
1338 "lt,supply-pre-on-sleep", &tmp);
1339 if (rc)
1340 pr_debug("no supply pre on sleep value. rc=%d\n", rc);
1341
1342 pdata->vreg_config[i].pre_on_sleep = (!rc ? tmp : 0);
1343
1344 rc = of_property_read_u32(supply_node,
1345 "lt,supply-pre-off-sleep", &tmp);
1346 if (rc)
1347 pr_debug("no supply pre off sleep value. rc=%d\n", rc);
1348
1349 pdata->vreg_config[i].pre_off_sleep = (!rc ? tmp : 0);
1350
1351 rc = of_property_read_u32(supply_node,
1352 "lt,supply-post-on-sleep", &tmp);
1353 if (rc)
1354 pr_debug("no supply post on sleep value. rc=%d\n", rc);
1355
1356 pdata->vreg_config[i].post_on_sleep = (!rc ? tmp : 0);
1357
1358 rc = of_property_read_u32(supply_node,
1359 "lt,supply-post-off-sleep", &tmp);
1360 if (rc)
1361 pr_debug("no supply post off sleep value. rc=%d\n", rc);
1362
1363 pdata->vreg_config[i].post_off_sleep = (!rc ? tmp : 0);
1364
1365 pr_debug("%s min=%d, max=%d, enable=%d, disable=%d, preonsleep=%d, postonsleep=%d, preoffsleep=%d, postoffsleep=%d\n",
1366 pdata->vreg_config[i].vreg_name,
1367 pdata->vreg_config[i].min_voltage,
1368 pdata->vreg_config[i].max_voltage,
1369 pdata->vreg_config[i].enable_load,
1370 pdata->vreg_config[i].disable_load,
1371 pdata->vreg_config[i].pre_on_sleep,
1372 pdata->vreg_config[i].post_on_sleep,
1373 pdata->vreg_config[i].pre_off_sleep,
1374 pdata->vreg_config[i].post_off_sleep);
1375 ++i;
1376
1377 rc = 0;
1378 }
1379
1380 rc = lt9611_config_vreg(dev,
1381 pdata->vreg_config, pdata->num_vreg, true);
1382 if (rc)
1383 goto error;
1384
1385 return rc;
1386
1387error:
1388 if (pdata->vreg_config) {
1389 devm_kfree(dev, pdata->vreg_config);
1390 pdata->vreg_config = NULL;
1391 pdata->num_vreg = 0;
1392 }
1393
1394 return rc;
1395}
1396
1397static void lt9611_put_dt_supply(struct device *dev,
1398 struct lt9611 *pdata)
1399{
1400 if (!dev || !pdata) {
1401 pr_err("invalid input param dev:%pK pdata:%pK\n", dev, pdata);
1402 return;
1403 }
1404
1405 lt9611_config_vreg(dev,
1406 pdata->vreg_config, pdata->num_vreg, false);
1407
1408 if (pdata->vreg_config) {
1409 devm_kfree(dev, pdata->vreg_config);
1410 pdata->vreg_config = NULL;
1411 }
1412 pdata->num_vreg = 0;
1413}
1414
1415static int lt9611_enable_vreg(struct lt9611 *pdata, int enable)
1416{
1417 int i = 0, rc = 0;
1418 bool need_sleep;
1419 struct lt9611_vreg *in_vreg = pdata->vreg_config;
1420 int num_vreg = pdata->num_vreg;
1421
1422 if (enable) {
1423 for (i = 0; i < num_vreg; i++) {
1424 rc = PTR_RET(in_vreg[i].vreg);
1425 if (rc) {
1426 pr_err("%s regulator error. rc=%d\n",
1427 in_vreg[i].vreg_name, rc);
1428 goto vreg_set_opt_mode_fail;
1429 }
1430 need_sleep = !regulator_is_enabled(in_vreg[i].vreg);
1431 if (in_vreg[i].pre_on_sleep && need_sleep)
1432 usleep_range(in_vreg[i].pre_on_sleep * 1000,
1433 in_vreg[i].pre_on_sleep * 1000);
1434 rc = regulator_set_load(in_vreg[i].vreg,
1435 in_vreg[i].enable_load);
1436 if (rc < 0) {
1437 pr_err("%s set opt m fail\n",
1438 in_vreg[i].vreg_name);
1439 goto vreg_set_opt_mode_fail;
1440 }
1441 rc = regulator_enable(in_vreg[i].vreg);
1442 if (in_vreg[i].post_on_sleep && need_sleep)
1443 usleep_range(in_vreg[i].post_on_sleep * 1000,
1444 in_vreg[i].post_on_sleep * 1000);
1445 if (rc < 0) {
1446 pr_err("%s enable failed\n",
1447 in_vreg[i].vreg_name);
1448 goto disable_vreg;
1449 }
1450 }
1451 } else {
1452 for (i = num_vreg-1; i >= 0; i--) {
1453 if (in_vreg[i].pre_off_sleep)
1454 usleep_range(in_vreg[i].pre_off_sleep * 1000,
1455 in_vreg[i].pre_off_sleep * 1000);
1456 regulator_set_load(in_vreg[i].vreg,
1457 in_vreg[i].disable_load);
1458 regulator_disable(in_vreg[i].vreg);
1459 if (in_vreg[i].post_off_sleep)
1460 usleep_range(in_vreg[i].post_off_sleep * 1000,
1461 in_vreg[i].post_off_sleep * 1000);
1462 }
1463 }
1464 return rc;
1465
1466disable_vreg:
1467 regulator_set_load(in_vreg[i].vreg, in_vreg[i].disable_load);
1468
1469vreg_set_opt_mode_fail:
1470 for (i--; i >= 0; i--) {
1471 if (in_vreg[i].pre_off_sleep)
1472 usleep_range(in_vreg[i].pre_off_sleep * 1000,
1473 in_vreg[i].pre_off_sleep * 1000);
1474 regulator_set_load(in_vreg[i].vreg,
1475 in_vreg[i].disable_load);
1476 regulator_disable(in_vreg[i].vreg);
1477 if (in_vreg[i].post_off_sleep)
1478 usleep_range(in_vreg[i].post_off_sleep * 1000,
1479 in_vreg[i].post_off_sleep * 1000);
1480 }
1481
1482 return rc;
1483}
1484
1485static struct lt9611_timing_info *lt9611_get_supported_timing(
1486 struct drm_display_mode *mode) {
1487 int i = 0;
1488
1489 while (lt9611_supp_timing_cfg[i].xres != 0xffff) {
1490 if (lt9611_supp_timing_cfg[i].xres == mode->hdisplay &&
1491 lt9611_supp_timing_cfg[i].yres == mode->vdisplay &&
1492 lt9611_supp_timing_cfg[i].fps ==
1493 drm_mode_vrefresh(mode)) {
1494 return &lt9611_supp_timing_cfg[i];
1495 }
1496 i++;
1497 }
1498
1499 return NULL;
1500}
1501
1502/* TODO: intf/lane number needs info from both DSI host and client */
1503static int lt9611_get_intf_num(struct lt9611 *pdata,
1504 struct drm_display_mode *mode)
1505{
1506 int num_of_intfs = 0;
1507 struct lt9611_timing_info *timing =
1508 lt9611_get_supported_timing(mode);
1509
1510 if (timing)
1511 num_of_intfs = timing->intfs;
1512 else {
1513 pr_err("interface number not defined by bridge chip\n");
1514 num_of_intfs = 0;
1515 }
1516
1517 return num_of_intfs;
1518}
1519
1520static int lt9611_get_lane_num(struct lt9611 *pdata,
1521 struct drm_display_mode *mode)
1522{
1523 int num_of_lanes = 0;
1524 struct lt9611_timing_info *timing =
1525 lt9611_get_supported_timing(mode);
1526
1527 if (timing)
1528 num_of_lanes = timing->lanes;
1529 else {
1530 pr_err("lane number not defined by bridge chip\n");
1531 num_of_lanes = 0;
1532 }
1533
1534 return num_of_lanes;
1535}
1536
1537static void lt9611_get_video_cfg(struct lt9611 *pdata,
1538 struct drm_display_mode *mode,
1539 struct lt9611_video_cfg *video_cfg)
1540{
1541 int rc = 0;
1542 struct hdmi_avi_infoframe avi_frame;
1543
1544 memset(&avi_frame, 0, sizeof(avi_frame));
1545
1546 video_cfg->h_active = mode->hdisplay;
1547 video_cfg->v_active = mode->vdisplay;
1548 video_cfg->h_front_porch = mode->hsync_start - mode->hdisplay;
1549 video_cfg->v_front_porch = mode->vsync_start - mode->vdisplay;
1550 video_cfg->h_back_porch = mode->htotal - mode->hsync_end;
1551 video_cfg->v_back_porch = mode->vtotal - mode->vsync_end;
1552 video_cfg->h_pulse_width = mode->hsync_end - mode->hsync_start;
1553 video_cfg->v_pulse_width = mode->vsync_end - mode->vsync_start;
1554 video_cfg->pclk_khz = mode->clock;
1555
1556 video_cfg->h_polarity = !!(mode->flags & DRM_MODE_FLAG_PHSYNC);
1557 video_cfg->v_polarity = !!(mode->flags & DRM_MODE_FLAG_PVSYNC);
1558
1559 video_cfg->num_of_lanes = lt9611_get_lane_num(pdata, mode);
1560 video_cfg->num_of_intfs = lt9611_get_intf_num(pdata, mode);
1561
1562 pr_debug("video=h[%d,%d,%d,%d] v[%d,%d,%d,%d] pclk=%d lane=%d intf=%d\n",
1563 video_cfg->h_active, video_cfg->h_front_porch,
1564 video_cfg->h_pulse_width, video_cfg->h_back_porch,
1565 video_cfg->v_active, video_cfg->v_front_porch,
1566 video_cfg->v_pulse_width, video_cfg->v_back_porch,
1567 video_cfg->pclk_khz, video_cfg->num_of_lanes,
1568 video_cfg->num_of_intfs);
1569
1570 rc = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, mode);
1571 if (rc) {
1572 pr_err("get avi frame failed ret=%d\n", rc);
1573 } else {
1574 video_cfg->scaninfo = avi_frame.scan_mode;
1575 video_cfg->ar = avi_frame.picture_aspect;
1576 video_cfg->vic = avi_frame.video_code;
1577 pr_debug("scaninfo=%d ar=%d vic=%d\n",
1578 video_cfg->scaninfo, video_cfg->ar, video_cfg->vic);
1579 }
1580}
1581
1582/* connector funcs */
1583static enum drm_connector_status
1584lt9611_connector_detect(struct drm_connector *connector, bool force)
1585{
1586 struct lt9611 *pdata = connector_to_lt9611(connector);
1587
1588 if (!pdata->non_pluggable) {
1589 u8 reg_val = 0;
1590 int connected = 0;
1591
1592 lt9611_write(pdata, 0xff, 0x82);
1593 lt9611_read(pdata, 0x5e, &reg_val, 1);
1594 connected = (reg_val & BIT(2));
1595 pr_debug("connected = %x\n", connected);
1596
1597 pdata->status = connected ? connector_status_connected :
1598 connector_status_disconnected;
1599 } else
1600 pdata->status = connector_status_connected;
1601
1602 return pdata->status;
1603}
1604
1605static int lt9611_read_edid(struct lt9611 *pdata)
1606{
1607 int ret = 0;
1608 u8 i, j;
1609 u8 temp = 0;
1610
1611 if (!pdata) {
1612 pr_err("invalid input\n");
1613 return -EINVAL;
1614 }
1615
1616 memset(pdata->edid_buf, 0, EDID_SEG_SIZE);
1617
1618 lt9611_write(pdata, 0xff, 0x85);
1619 lt9611_write(pdata, 0x03, 0xc9);
1620 lt9611_write(pdata, 0x04, 0xa0); /* 0xA0 is EDID device address */
1621 lt9611_write(pdata, 0x05, 0x00); /* 0x00 is EDID offset address */
1622 lt9611_write(pdata, 0x06, 0x20); /* length for read */
1623 lt9611_write(pdata, 0x14, 0x7f);
1624
1625 for (i = 0 ; i < 8 ; i++) {
1626 lt9611_write(pdata, 0x05, i * 32); /* offset address */
1627 lt9611_write(pdata, 0x07, 0x36);
1628 lt9611_write(pdata, 0x07, 0x31);
1629 lt9611_write(pdata, 0x07, 0x37);
1630 usleep_range(5000, 10000);
1631
1632 lt9611_read(pdata, 0x40, &temp, 1);
1633
1634 if (temp & 0x02) { /*KEY_DDC_ACCS_DONE=1*/
1635 for (j = 0; j < 32; j++) {
1636 lt9611_read(pdata, 0x83,
1637 &(pdata->edid_buf[i*32+j]), 1);
1638 }
1639 } else if (temp & 0x50) { /* DDC No Ack or Abitration lost */
1640 pr_err("read edid failed: no ack\n");
1641 ret = -EIO;
1642 goto end;
1643 } else {
1644 pr_err("read edid failed: access not done\n");
1645 ret = -EIO;
1646 goto end;
1647 }
1648 }
1649
1650 pr_debug("read edid succeeded, checksum = 0x%x\n",
1651 pdata->edid_buf[255]);
1652
1653end:
1654 lt9611_write(pdata, 0x07, 0x1f);
1655 return ret;
1656}
1657
1658/* TODO: add support for more extenstion blocks */
1659static int lt9611_get_edid_block(void *data, u8 *buf, unsigned int block,
1660 size_t len)
1661{
1662 struct lt9611 *pdata = data;
1663 int ret = 0;
1664
1665 pr_debug("get edid block: block=%d, len=%d\n", block, (int)len);
1666
1667 if (len > 128)
1668 return -EINVAL;
1669
1670 /* support up to 1 extension block */
1671 if (block > 1)
1672 return -EINVAL;
1673
1674 if (block == 0) {
1675 /* always read 2 edid blocks once */
1676 ret = lt9611_read_edid(pdata);
1677 if (ret) {
1678 pr_err("edid read failed\n");
1679 return ret;
1680 }
1681 }
1682
1683 if (block % 2 == 0)
1684 memcpy(buf, pdata->edid_buf, len);
1685 else
1686 memcpy(buf, pdata->edid_buf + 128, len);
1687
1688 return 0;
1689}
1690
1691static int lt9611_connector_get_modes(struct drm_connector *connector)
1692{
1693 struct lt9611 *pdata = connector_to_lt9611(connector);
1694 struct drm_display_mode *mode, *m;
1695 unsigned int count = 0;
1696
1697 pr_debug("get modes\n");
1698
1699 if (pdata->non_pluggable) {
1700 list_for_each_entry(mode, &pdata->mode_list, head) {
1701 m = drm_mode_duplicate(connector->dev, mode);
1702 if (!m) {
1703 pr_err("failed to add hdmi mode %dx%d\n",
1704 mode->hdisplay, mode->vdisplay);
1705 break;
1706 }
1707 drm_mode_probed_add(connector, m);
1708 }
1709 count = pdata->num_of_modes;
1710 } else {
1711 struct edid *edid;
1712
1713 if (!pdata->power_on)
1714 lt9611_power_on(pdata, true);
1715 edid = drm_do_get_edid(connector, lt9611_get_edid_block, pdata);
1716
1717 drm_mode_connector_update_edid_property(connector, edid);
1718 count = drm_add_edid_modes(connector, edid);
1719
1720 pdata->hdmi_mode = drm_detect_hdmi_monitor(edid);
1721 pr_debug("hdmi_mode = %d\n", pdata->hdmi_mode);
1722
1723 /* TODO: this should not be hard coded */
1724 drm_set_preferred_mode(connector, 1920, 1080);
1725
1726 kfree(edid);
1727 }
1728
1729 return count;
1730}
1731
1732static enum drm_mode_status lt9611_connector_mode_valid(
1733 struct drm_connector *connector, struct drm_display_mode *mode)
1734{
1735 struct lt9611_timing_info *timing =
1736 lt9611_get_supported_timing(mode);
1737
1738 return timing ? MODE_OK : MODE_BAD;
1739}
1740
1741/* bridge funcs */
1742static void lt9611_bridge_enable(struct drm_bridge *bridge)
1743{
1744 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1745
1746 pr_debug("bridge enable\n");
1747
1748 if (lt9611_power_on(pdata, true)) {
1749 pr_err("power on failed\n");
1750 return;
1751 }
1752
1753 if (lt9611_video_on(pdata, true)) {
1754 pr_err("video on failed\n");
1755 return;
1756 }
1757}
1758
1759static void lt9611_bridge_disable(struct drm_bridge *bridge)
1760{
1761 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1762
1763 pr_debug("bridge disable\n");
1764
1765 if (lt9611_video_on(pdata, false)) {
1766 pr_err("video on failed\n");
1767 return;
1768 }
1769
1770 if (lt9611_power_on(pdata, false)) {
1771 pr_err("power on failed\n");
1772 return;
1773 }
1774}
1775
1776static void lt9611_bridge_mode_set(struct drm_bridge *bridge,
1777 struct drm_display_mode *mode,
1778 struct drm_display_mode *adj_mode)
1779{
1780 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1781 struct lt9611_video_cfg *video_cfg = &pdata->video_cfg;
1782 int ret = 0;
1783
1784 pr_debug("bridge mode_set: hdisplay=%d, vdisplay=%d, vrefresh=%d, clock=%d\n",
1785 adj_mode->hdisplay, adj_mode->vdisplay,
1786 adj_mode->vrefresh, adj_mode->clock);
1787
1788 drm_mode_copy(&pdata->curr_mode, adj_mode);
1789
1790 memset(video_cfg, 0, sizeof(struct lt9611_video_cfg));
1791 lt9611_get_video_cfg(pdata, adj_mode, video_cfg);
1792
1793 /* TODO: update intf number of host */
1794 if (video_cfg->num_of_lanes != pdata->dsi->lanes) {
1795 mipi_dsi_detach(pdata->dsi);
1796 pdata->dsi->lanes = video_cfg->num_of_lanes;
1797 ret = mipi_dsi_attach(pdata->dsi);
1798 if (ret)
1799 pr_err("failed to change host lanes\n");
1800 }
1801}
1802
1803static int lt9611_bridge_attach(struct drm_bridge *bridge)
1804{
1805 struct mipi_dsi_host *host;
1806 struct mipi_dsi_device *dsi;
1807 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1808 int ret;
1809 const struct mipi_dsi_device_info info = { .type = "lt9611",
1810 .channel = 0,
1811 .node = NULL,
1812 };
1813
1814 pr_debug("bridge attach\n");
1815
1816 if (!bridge->encoder) {
1817 DRM_ERROR("Parent encoder object not found");
1818 return -ENODEV;
1819 }
1820
1821 host = of_find_mipi_dsi_host_by_node(pdata->host_node);
1822 if (!host) {
1823 pr_err("failed to find dsi host\n");
1824 return -EPROBE_DEFER;
1825 }
1826
1827 dsi = mipi_dsi_device_register_full(host, &info);
1828 if (IS_ERR(dsi)) {
1829 pr_err("failed to create dsi device\n");
1830 ret = PTR_ERR(dsi);
1831 goto err_dsi_device;
1832 }
1833
1834 dsi->lanes = 4;
1835 dsi->format = MIPI_DSI_FMT_RGB888;
1836 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
1837 MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_VIDEO_BLLP |
1838 MIPI_DSI_MODE_VIDEO_EOF_BLLP;
1839
1840 ret = mipi_dsi_attach(dsi);
1841 if (ret < 0) {
1842 pr_err("failed to attach dsi to host\n");
1843 goto err_dsi_attach;
1844 }
1845
1846 pdata->dsi = dsi;
1847
1848 return 0;
1849
1850err_dsi_attach:
1851 mipi_dsi_device_unregister(dsi);
1852err_dsi_device:
1853 return ret;
1854}
1855
1856static void lt9611_bridge_pre_enable(struct drm_bridge *bridge)
1857{
1858 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1859
1860 pr_debug("bridge pre_enable\n");
1861
1862 lt9611_reset(pdata);
1863
1864 lt9611_write(pdata, 0xff, 0x80);
1865 lt9611_write(pdata, 0xee, 0x01);
1866}
1867
1868static bool lt9611_bridge_mode_fixup(struct drm_bridge *bridge,
1869 const struct drm_display_mode *mode,
1870 struct drm_display_mode *adjusted_mode)
1871{
1872 pr_debug("bridge mode_fixup\n");
1873
1874 return true;
1875}
1876
1877static void lt9611_bridge_post_disable(struct drm_bridge *bridge)
1878{
1879 struct lt9611 *pdata = bridge_to_lt9611(bridge);
1880
1881 pr_debug("bridge post_disable\n");
1882
1883 lt9611_sleep_setup(pdata);
1884}
1885
1886static struct drm_connector_funcs override_funcs;
1887static struct drm_connector_helper_funcs override_helper_private;
1888
1889static int lt9611_bridge_connector_init(struct drm_bridge *bridge,
1890 struct drm_connector *connector)
1891{
1892 pr_debug("bridge connector_init\n");
1893
1894 if (connector->encoder != bridge->encoder) {
1895 pr_err("bridge and connector need attach to the same encoder\n");
1896 return -EINVAL;
1897 }
1898
1899 connector->private = bridge;
1900
1901 /*
1902 * Make a copy of drm_connector_funcs and drm_connector_helper_funcs. To
1903 * make sure other KMS components won't be broken. For example, if other
1904 * connectors share the implementation for ->funs, overwriting this will
1905 * break other connectors.
1906 */
1907 override_funcs = *connector->funcs;
1908 override_funcs.detect = lt9611_connector_detect;
1909 connector->funcs = &override_funcs;
1910
1911 override_helper_private = *connector->helper_private;
1912 override_helper_private.get_modes = lt9611_connector_get_modes;
1913 override_helper_private.mode_valid = lt9611_connector_mode_valid;
1914 connector->helper_private = &override_helper_private;
1915
1916 return 0;
1917}
1918
1919static const struct drm_bridge_funcs lt9611_bridge_funcs = {
1920 .attach = lt9611_bridge_attach,
1921 .mode_fixup = lt9611_bridge_mode_fixup,
1922 .pre_enable = lt9611_bridge_pre_enable,
1923 .enable = lt9611_bridge_enable,
1924 .disable = lt9611_bridge_disable,
1925 .post_disable = lt9611_bridge_post_disable,
1926 .mode_set = lt9611_bridge_mode_set,
1927 .connector_init = lt9611_bridge_connector_init,
1928};
1929
1930/* sysfs */
1931static int lt9611_dump_debug_info(struct lt9611 *pdata)
1932{
1933 if (!pdata->power_on) {
1934 pr_err("device is not power on\n");
1935 return -EINVAL;
1936 }
1937
1938 lt9611_video_check(pdata);
1939
1940 lt9611_pcr_mk_debug(pdata);
1941
1942 lt9611_mipi_byte_clk_debug(pdata);
1943
1944 return 0;
1945}
1946
1947static ssize_t lt9611_dump_info_wta_attr(struct device *dev,
1948 struct device_attribute *attr,
1949 const char *buf,
1950 size_t count)
1951{
1952 struct lt9611 *pdata = dev_get_drvdata(dev);
1953
1954 if (!pdata) {
1955 pr_err("pdata is NULL\n");
1956 return -EINVAL;
1957 }
1958
1959 lt9611_dump_debug_info(pdata);
1960
1961 return count;
1962}
1963
1964static DEVICE_ATTR(dump_info, 0200, NULL, lt9611_dump_info_wta_attr);
1965
1966static struct attribute *lt9611_sysfs_attrs[] = {
1967 &dev_attr_dump_info.attr,
1968 NULL,
1969};
1970
1971static struct attribute_group lt9611_sysfs_attr_grp = {
1972 .attrs = lt9611_sysfs_attrs,
1973};
1974
1975static int lt9611_sysfs_init(struct device *dev)
1976{
1977 int rc = 0;
1978
1979 if (!dev) {
1980 pr_err("%s: Invalid params\n", __func__);
1981 return -EINVAL;
1982 }
1983
1984 rc = sysfs_create_group(&dev->kobj, &lt9611_sysfs_attr_grp);
1985 if (rc)
1986 pr_err("%s: sysfs group creation failed %d\n", __func__, rc);
1987
1988 return rc;
1989}
1990
1991static void lt9611_sysfs_remove(struct device *dev)
1992{
1993 if (!dev) {
1994 pr_err("%s: Invalid params\n", __func__);
1995 return;
1996 }
1997
1998 sysfs_remove_group(&dev->kobj, &lt9611_sysfs_attr_grp);
1999}
2000
Ray Zhang3436c0d2018-03-06 15:41:40 +08002001static int lt9611_bind(struct device *dev, struct device *master, void *data)
2002{
2003 return 0;
2004}
2005
2006static void lt9611_unbind(struct device *dev, struct device *master, void *data)
2007{
2008}
2009
2010static const struct component_ops lt9611_comp_ops = {
2011 .bind = lt9611_bind,
2012 .unbind = lt9611_unbind,
2013};
2014
Ray Zhangebb763a2018-01-22 10:46:40 +08002015static int lt9611_probe(struct i2c_client *client,
2016 const struct i2c_device_id *id)
2017{
2018 struct lt9611 *pdata;
2019 int ret = 0;
2020
2021 if (!client || !client->dev.of_node) {
2022 pr_err("invalid input\n");
2023 return -EINVAL;
2024 }
2025
2026 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
2027 pr_err("device doesn't support I2C\n");
2028 return -ENODEV;
2029 }
2030
2031 pdata = devm_kzalloc(&client->dev,
2032 sizeof(struct lt9611), GFP_KERNEL);
2033 if (!pdata)
2034 return -ENOMEM;
2035
2036 ret = lt9611_parse_dt(&client->dev, pdata);
2037 if (ret) {
2038 pr_err("failed to parse device tree\n");
2039 goto err_dt_parse;
2040 }
2041
2042 ret = lt9611_get_dt_supply(&client->dev, pdata);
2043 if (ret) {
2044 pr_err("failed to get dt supply\n");
2045 goto err_dt_parse;
2046 }
2047
2048 pdata->dev = &client->dev;
2049 pdata->i2c_client = client;
2050 pr_debug("I2C address is %x\n", client->addr);
2051
2052 ret = lt9611_gpio_configure(pdata, true);
2053 if (ret) {
2054 pr_err("failed to configure GPIOs\n");
2055 goto err_dt_supply;
2056 }
2057
2058 lt9611_assert_5v(pdata);
2059
2060 ret = lt9611_enable_vreg(pdata, true);
2061 if (ret) {
2062 pr_err("failed to enable vreg\n");
2063 goto err_dt_supply;
2064 }
2065
2066 lt9611_reset(pdata);
2067
2068 ret = lt9611_read_device_rev(pdata);
2069 if (ret) {
2070 pr_err("failed to read chip rev\n");
2071 goto err_i2c_prog;
2072 }
2073
2074 pdata->irq = gpio_to_irq(pdata->irq_gpio);
2075 ret = request_threaded_irq(pdata->irq, NULL, lt9611_irq_thread_handler,
2076 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lt9611", pdata);
2077 if (ret) {
2078 pr_err("failed to request irq\n");
2079 goto err_i2c_prog;
2080 }
2081
2082 i2c_set_clientdata(client, pdata);
2083 dev_set_drvdata(&client->dev, pdata);
2084
2085 ret = lt9611_sysfs_init(&client->dev);
2086 if (ret) {
2087 pr_err("sysfs init failed\n");
2088 goto err_sysfs_init;
2089 }
2090
2091 pdata->bridge.funcs = &lt9611_bridge_funcs;
2092 pdata->bridge.of_node = client->dev.of_node;
2093
2094 drm_bridge_add(&pdata->bridge);
2095
Ray Zhang3436c0d2018-03-06 15:41:40 +08002096 ret = component_add(&client->dev, &lt9611_comp_ops);
2097 if (ret)
2098 pr_err("component add failed, rc=%d\n", ret);
2099
Ray Zhangebb763a2018-01-22 10:46:40 +08002100 return ret;
2101
2102err_sysfs_init:
2103 disable_irq(pdata->irq);
2104 free_irq(pdata->irq, pdata);
2105err_i2c_prog:
2106 lt9611_gpio_configure(pdata, false);
2107err_dt_supply:
2108 lt9611_put_dt_supply(&client->dev, pdata);
2109err_dt_parse:
2110 devm_kfree(&client->dev, pdata);
2111 return ret;
2112}
2113
2114static int lt9611_remove(struct i2c_client *client)
2115{
2116 int ret = -EINVAL;
2117 struct lt9611 *pdata = i2c_get_clientdata(client);
2118 struct drm_display_mode *mode, *n;
2119
2120 if (!pdata)
2121 goto end;
2122
2123 mipi_dsi_detach(pdata->dsi);
2124 mipi_dsi_device_unregister(pdata->dsi);
2125
2126 drm_bridge_remove(&pdata->bridge);
2127
2128 lt9611_sysfs_remove(&client->dev);
2129
2130 disable_irq(pdata->irq);
2131 free_irq(pdata->irq, pdata);
2132
2133 ret = lt9611_gpio_configure(pdata, false);
2134
2135 lt9611_put_dt_supply(&client->dev, pdata);
2136
2137 if (pdata->non_pluggable) {
2138 list_for_each_entry_safe(mode, n, &pdata->mode_list, head) {
2139 list_del(&mode->head);
2140 kfree(mode);
2141 }
2142 }
2143
2144 devm_kfree(&client->dev, pdata);
2145
2146end:
2147 return ret;
2148}
2149
2150
2151static struct i2c_device_id lt9611_id[] = {
2152 { "lt,lt9611", 0},
2153 {}
2154};
2155
2156static const struct of_device_id lt9611_match_table[] = {
2157 {.compatible = "lt,lt9611"},
2158 {}
2159};
2160MODULE_DEVICE_TABLE(of, lt9611_match_table);
2161
2162static struct i2c_driver lt9611_driver = {
2163 .driver = {
2164 .name = "lt9611",
2165 .owner = THIS_MODULE,
2166 .of_match_table = lt9611_match_table,
2167 },
2168 .probe = lt9611_probe,
2169 .remove = lt9611_remove,
2170 .id_table = lt9611_id,
2171};
2172
2173static int __init lt9611_init(void)
2174{
2175 return i2c_add_driver(&lt9611_driver);
2176}
2177
2178static void __exit lt9611_exit(void)
2179{
2180 i2c_del_driver(&lt9611_driver);
2181}
2182
2183module_init(lt9611_init);
2184module_exit(lt9611_exit);
2185