mtd: Support kmsg dumper based on pstore/blk
[linux-2.6-microblaze.git] / drivers / gpu / drm / bridge / analogix / analogix-anx6345.c
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2016, Analogix Semiconductor.
4  * Copyright(c) 2017, Icenowy Zheng <icenowy@aosc.io>
5  *
6  * Based on anx7808 driver obtained from chromeos with copyright:
7  * Copyright(c) 2013, Google Inc.
8  */
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/types.h>
20
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_bridge.h>
23 #include <drm/drm_crtc.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_dp_helper.h>
26 #include <drm/drm_edid.h>
27 #include <drm/drm_of.h>
28 #include <drm/drm_panel.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_probe_helper.h>
31
32 #include "analogix-i2c-dptx.h"
33 #include "analogix-i2c-txcommon.h"
34
35 #define POLL_DELAY              50000 /* us */
36 #define POLL_TIMEOUT            5000000 /* us */
37
38 #define I2C_IDX_DPTX            0
39 #define I2C_IDX_TXCOM           1
40
41 static const u8 anx6345_i2c_addresses[] = {
42         [I2C_IDX_DPTX]  = 0x70,
43         [I2C_IDX_TXCOM] = 0x72,
44 };
45 #define I2C_NUM_ADDRESSES       ARRAY_SIZE(anx6345_i2c_addresses)
46
47 struct anx6345 {
48         struct drm_dp_aux aux;
49         struct drm_bridge bridge;
50         struct i2c_client *client;
51         struct edid *edid;
52         struct drm_connector connector;
53         struct drm_panel *panel;
54         struct regulator *dvdd12;
55         struct regulator *dvdd25;
56         struct gpio_desc *gpiod_reset;
57         struct mutex lock;      /* protect EDID access */
58
59         /* I2C Slave addresses of ANX6345 are mapped as DPTX and SYS */
60         struct i2c_client *i2c_clients[I2C_NUM_ADDRESSES];
61         struct regmap *map[I2C_NUM_ADDRESSES];
62
63         u16 chipid;
64         u8 dpcd[DP_RECEIVER_CAP_SIZE];
65
66         bool powered;
67 };
68
69 static inline struct anx6345 *connector_to_anx6345(struct drm_connector *c)
70 {
71         return container_of(c, struct anx6345, connector);
72 }
73
74 static inline struct anx6345 *bridge_to_anx6345(struct drm_bridge *bridge)
75 {
76         return container_of(bridge, struct anx6345, bridge);
77 }
78
79 static int anx6345_set_bits(struct regmap *map, u8 reg, u8 mask)
80 {
81         return regmap_update_bits(map, reg, mask, mask);
82 }
83
84 static int anx6345_clear_bits(struct regmap *map, u8 reg, u8 mask)
85 {
86         return regmap_update_bits(map, reg, mask, 0);
87 }
88
89 static ssize_t anx6345_aux_transfer(struct drm_dp_aux *aux,
90                                     struct drm_dp_aux_msg *msg)
91 {
92         struct anx6345 *anx6345 = container_of(aux, struct anx6345, aux);
93
94         return anx_dp_aux_transfer(anx6345->map[I2C_IDX_DPTX], msg);
95 }
96
97 static int anx6345_dp_link_training(struct anx6345 *anx6345)
98 {
99         unsigned int value;
100         u8 dp_bw, dpcd[2];
101         int err;
102
103         err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
104                                  SP_POWERDOWN_CTRL_REG,
105                                  SP_TOTAL_PD);
106         if (err)
107                 return err;
108
109         err = drm_dp_dpcd_readb(&anx6345->aux, DP_MAX_LINK_RATE, &dp_bw);
110         if (err < 0)
111                 return err;
112
113         switch (dp_bw) {
114         case DP_LINK_BW_1_62:
115         case DP_LINK_BW_2_7:
116                 break;
117
118         default:
119                 DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);
120                 return -EINVAL;
121         }
122
123         err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
124                                SP_VIDEO_MUTE);
125         if (err)
126                 return err;
127
128         err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
129                                  SP_VID_CTRL1_REG, SP_VIDEO_EN);
130         if (err)
131                 return err;
132
133         /* Get DPCD info */
134         err = drm_dp_dpcd_read(&anx6345->aux, DP_DPCD_REV,
135                                &anx6345->dpcd, DP_RECEIVER_CAP_SIZE);
136         if (err < 0) {
137                 DRM_ERROR("Failed to read DPCD: %d\n", err);
138                 return err;
139         }
140
141         /* Clear channel x SERDES power down */
142         err = anx6345_clear_bits(anx6345->map[I2C_IDX_DPTX],
143                                  SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
144         if (err)
145                 return err;
146
147         /*
148          * Power up the sink (DP_SET_POWER register is only available on DPCD
149          * v1.1 and later).
150          */
151         if (anx6345->dpcd[DP_DPCD_REV] >= 0x11) {
152                 err = drm_dp_dpcd_readb(&anx6345->aux, DP_SET_POWER, &dpcd[0]);
153                 if (err < 0) {
154                         DRM_ERROR("Failed to read DP_SET_POWER register: %d\n",
155                                   err);
156                         return err;
157                 }
158
159                 dpcd[0] &= ~DP_SET_POWER_MASK;
160                 dpcd[0] |= DP_SET_POWER_D0;
161
162                 err = drm_dp_dpcd_writeb(&anx6345->aux, DP_SET_POWER, dpcd[0]);
163                 if (err < 0) {
164                         DRM_ERROR("Failed to power up DisplayPort link: %d\n",
165                                   err);
166                         return err;
167                 }
168
169                 /*
170                  * According to the DP 1.1 specification, a "Sink Device must
171                  * exit the power saving state within 1 ms" (Section 2.5.3.1,
172                  * Table 5-52, "Sink Control Field" (register 0x600).
173                  */
174                 usleep_range(1000, 2000);
175         }
176
177         /* Possibly enable downspread on the sink */
178         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
179                            SP_DP_DOWNSPREAD_CTRL1_REG, 0);
180         if (err)
181                 return err;
182
183         if (anx6345->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5) {
184                 DRM_DEBUG("Enable downspread on the sink\n");
185                 /* 4000PPM */
186                 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
187                                    SP_DP_DOWNSPREAD_CTRL1_REG, 8);
188                 if (err)
189                         return err;
190
191                 err = drm_dp_dpcd_writeb(&anx6345->aux, DP_DOWNSPREAD_CTRL,
192                                          DP_SPREAD_AMP_0_5);
193                 if (err < 0)
194                         return err;
195         } else {
196                 err = drm_dp_dpcd_writeb(&anx6345->aux, DP_DOWNSPREAD_CTRL, 0);
197                 if (err < 0)
198                         return err;
199         }
200
201         /* Set the lane count and the link rate on the sink */
202         if (drm_dp_enhanced_frame_cap(anx6345->dpcd))
203                 err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
204                                        SP_DP_SYSTEM_CTRL_BASE + 4,
205                                        SP_ENHANCED_MODE);
206         else
207                 err = anx6345_clear_bits(anx6345->map[I2C_IDX_DPTX],
208                                          SP_DP_SYSTEM_CTRL_BASE + 4,
209                                          SP_ENHANCED_MODE);
210         if (err)
211                 return err;
212
213         dpcd[0] = dp_bw;
214         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
215                            SP_DP_MAIN_LINK_BW_SET_REG, dpcd[0]);
216         if (err)
217                 return err;
218
219         dpcd[1] = drm_dp_max_lane_count(anx6345->dpcd);
220
221         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
222                            SP_DP_LANE_COUNT_SET_REG, dpcd[1]);
223         if (err)
224                 return err;
225
226         if (drm_dp_enhanced_frame_cap(anx6345->dpcd))
227                 dpcd[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
228
229         err = drm_dp_dpcd_write(&anx6345->aux, DP_LINK_BW_SET, dpcd,
230                                 sizeof(dpcd));
231
232         if (err < 0) {
233                 DRM_ERROR("Failed to configure link: %d\n", err);
234                 return err;
235         }
236
237         /* Start training on the source */
238         err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_LT_CTRL_REG,
239                            SP_LT_EN);
240         if (err)
241                 return err;
242
243         return regmap_read_poll_timeout(anx6345->map[I2C_IDX_DPTX],
244                                        SP_DP_LT_CTRL_REG,
245                                        value, !(value & SP_DP_LT_INPROGRESS),
246                                        POLL_DELAY, POLL_TIMEOUT);
247 }
248
249 static int anx6345_tx_initialization(struct anx6345 *anx6345)
250 {
251         int err, i;
252
253         /* FIXME: colordepth is hardcoded for now */
254         err = regmap_write(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL2_REG,
255                            SP_IN_BPC_6BIT << SP_IN_BPC_SHIFT);
256         if (err)
257                 return err;
258
259         err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_PLL_CTRL_REG, 0);
260         if (err)
261                 return err;
262
263         err = regmap_write(anx6345->map[I2C_IDX_TXCOM],
264                            SP_ANALOG_DEBUG1_REG, 0);
265         if (err)
266                 return err;
267
268         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
269                            SP_DP_LINK_DEBUG_CTRL_REG,
270                            SP_NEW_PRBS7 | SP_M_VID_DEBUG);
271         if (err)
272                 return err;
273
274         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
275                            SP_DP_ANALOG_POWER_DOWN_REG, 0);
276         if (err)
277                 return err;
278
279         /* Force HPD */
280         err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
281                                SP_DP_SYSTEM_CTRL_BASE + 3,
282                                SP_HPD_FORCE | SP_HPD_CTRL);
283         if (err)
284                 return err;
285
286         for (i = 0; i < 4; i++) {
287                 /* 4 lanes */
288                 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
289                                    SP_DP_LANE0_LT_CTRL_REG + i, 0);
290                 if (err)
291                         return err;
292         }
293
294         /* Reset AUX */
295         err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM],
296                                SP_RESET_CTRL2_REG, SP_AUX_RST);
297         if (err)
298                 return err;
299
300         return anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
301                                  SP_RESET_CTRL2_REG, SP_AUX_RST);
302 }
303
304 static void anx6345_poweron(struct anx6345 *anx6345)
305 {
306         int err;
307
308         /* Ensure reset is asserted before starting power on sequence */
309         gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
310         usleep_range(1000, 2000);
311
312         err = regulator_enable(anx6345->dvdd12);
313         if (err) {
314                 DRM_ERROR("Failed to enable dvdd12 regulator: %d\n",
315                           err);
316                 return;
317         }
318
319         /* T1 - delay between VDD12 and VDD25 should be 0-2ms */
320         usleep_range(1000, 2000);
321
322         err = regulator_enable(anx6345->dvdd25);
323         if (err) {
324                 DRM_ERROR("Failed to enable dvdd25 regulator: %d\n",
325                           err);
326                 return;
327         }
328
329         /* T2 - delay between RESETN and all power rail stable,
330          * should be 2-5ms
331          */
332         usleep_range(2000, 5000);
333
334         gpiod_set_value_cansleep(anx6345->gpiod_reset, 0);
335
336         /* Power on registers module */
337         anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
338                          SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
339         anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
340                            SP_REGISTER_PD | SP_TOTAL_PD);
341
342         if (anx6345->panel)
343                 drm_panel_prepare(anx6345->panel);
344
345         anx6345->powered = true;
346 }
347
348 static void anx6345_poweroff(struct anx6345 *anx6345)
349 {
350         int err;
351
352         gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
353         usleep_range(1000, 2000);
354
355         if (anx6345->panel)
356                 drm_panel_unprepare(anx6345->panel);
357
358         err = regulator_disable(anx6345->dvdd25);
359         if (err) {
360                 DRM_ERROR("Failed to disable dvdd25 regulator: %d\n",
361                           err);
362                 return;
363         }
364
365         usleep_range(5000, 10000);
366
367         err = regulator_disable(anx6345->dvdd12);
368         if (err) {
369                 DRM_ERROR("Failed to disable dvdd12 regulator: %d\n",
370                           err);
371                 return;
372         }
373
374         usleep_range(1000, 2000);
375
376         anx6345->powered = false;
377 }
378
379 static int anx6345_start(struct anx6345 *anx6345)
380 {
381         int err;
382
383         if (!anx6345->powered)
384                 anx6345_poweron(anx6345);
385
386         /* Power on needed modules */
387         err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
388                                 SP_POWERDOWN_CTRL_REG,
389                                 SP_VIDEO_PD | SP_LINK_PD);
390
391         err = anx6345_tx_initialization(anx6345);
392         if (err) {
393                 DRM_ERROR("Failed eDP transmitter initialization: %d\n", err);
394                 anx6345_poweroff(anx6345);
395                 return err;
396         }
397
398         err = anx6345_dp_link_training(anx6345);
399         if (err) {
400                 DRM_ERROR("Failed link training: %d\n", err);
401                 anx6345_poweroff(anx6345);
402                 return err;
403         }
404
405         /*
406          * This delay seems to help keep the hardware in a good state. Without
407          * it, there are times where it fails silently.
408          */
409         usleep_range(10000, 15000);
410
411         return 0;
412 }
413
414 static int anx6345_config_dp_output(struct anx6345 *anx6345)
415 {
416         int err;
417
418         err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
419                                  SP_VIDEO_MUTE);
420         if (err)
421                 return err;
422
423         /* Enable DP output */
424         err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
425                                SP_VIDEO_EN);
426         if (err)
427                 return err;
428
429         /* Force stream valid */
430         return anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
431                                SP_DP_SYSTEM_CTRL_BASE + 3,
432                                SP_STRM_FORCE | SP_STRM_CTRL);
433 }
434
435 static int anx6345_get_downstream_info(struct anx6345 *anx6345)
436 {
437         u8 value;
438         int err;
439
440         err = drm_dp_dpcd_readb(&anx6345->aux, DP_SINK_COUNT, &value);
441         if (err < 0) {
442                 DRM_ERROR("Get sink count failed %d\n", err);
443                 return err;
444         }
445
446         if (!DP_GET_SINK_COUNT(value)) {
447                 DRM_ERROR("Downstream disconnected\n");
448                 return -EIO;
449         }
450
451         return 0;
452 }
453
454 static int anx6345_get_modes(struct drm_connector *connector)
455 {
456         struct anx6345 *anx6345 = connector_to_anx6345(connector);
457         int err, num_modes = 0;
458         bool power_off = false;
459
460         mutex_lock(&anx6345->lock);
461
462         if (!anx6345->edid) {
463                 if (!anx6345->powered) {
464                         anx6345_poweron(anx6345);
465                         power_off = true;
466                 }
467
468                 err = anx6345_get_downstream_info(anx6345);
469                 if (err) {
470                         DRM_ERROR("Failed to get downstream info: %d\n", err);
471                         goto unlock;
472                 }
473
474                 anx6345->edid = drm_get_edid(connector, &anx6345->aux.ddc);
475                 if (!anx6345->edid)
476                         DRM_ERROR("Failed to read EDID from panel\n");
477
478                 err = drm_connector_update_edid_property(connector,
479                                                          anx6345->edid);
480                 if (err) {
481                         DRM_ERROR("Failed to update EDID property: %d\n", err);
482                         goto unlock;
483                 }
484         }
485
486         num_modes += drm_add_edid_modes(connector, anx6345->edid);
487
488 unlock:
489         if (power_off)
490                 anx6345_poweroff(anx6345);
491
492         mutex_unlock(&anx6345->lock);
493
494         if (!num_modes && anx6345->panel)
495                 num_modes += drm_panel_get_modes(anx6345->panel, connector);
496
497         return num_modes;
498 }
499
500 static const struct drm_connector_helper_funcs anx6345_connector_helper_funcs = {
501         .get_modes = anx6345_get_modes,
502 };
503
504 static void
505 anx6345_connector_destroy(struct drm_connector *connector)
506 {
507         struct anx6345 *anx6345 = connector_to_anx6345(connector);
508
509         if (anx6345->panel)
510                 drm_panel_detach(anx6345->panel);
511         drm_connector_cleanup(connector);
512 }
513
514 static const struct drm_connector_funcs anx6345_connector_funcs = {
515         .fill_modes = drm_helper_probe_single_connector_modes,
516         .destroy = anx6345_connector_destroy,
517         .reset = drm_atomic_helper_connector_reset,
518         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
519         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
520 };
521
522 static int anx6345_bridge_attach(struct drm_bridge *bridge,
523                                  enum drm_bridge_attach_flags flags)
524 {
525         struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
526         int err;
527
528         if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) {
529                 DRM_ERROR("Fix bridge driver to make connector optional!");
530                 return -EINVAL;
531         }
532
533         if (!bridge->encoder) {
534                 DRM_ERROR("Parent encoder object not found");
535                 return -ENODEV;
536         }
537
538         /* Register aux channel */
539         anx6345->aux.name = "DP-AUX";
540         anx6345->aux.dev = &anx6345->client->dev;
541         anx6345->aux.transfer = anx6345_aux_transfer;
542
543         err = drm_dp_aux_register(&anx6345->aux);
544         if (err < 0) {
545                 DRM_ERROR("Failed to register aux channel: %d\n", err);
546                 return err;
547         }
548
549         err = drm_connector_init(bridge->dev, &anx6345->connector,
550                                  &anx6345_connector_funcs,
551                                  DRM_MODE_CONNECTOR_eDP);
552         if (err) {
553                 DRM_ERROR("Failed to initialize connector: %d\n", err);
554                 return err;
555         }
556
557         drm_connector_helper_add(&anx6345->connector,
558                                  &anx6345_connector_helper_funcs);
559
560         err = drm_connector_register(&anx6345->connector);
561         if (err) {
562                 DRM_ERROR("Failed to register connector: %d\n", err);
563                 return err;
564         }
565
566         anx6345->connector.polled = DRM_CONNECTOR_POLL_HPD;
567
568         err = drm_connector_attach_encoder(&anx6345->connector,
569                                            bridge->encoder);
570         if (err) {
571                 DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
572                 return err;
573         }
574
575         if (anx6345->panel) {
576                 err = drm_panel_attach(anx6345->panel, &anx6345->connector);
577                 if (err) {
578                         DRM_ERROR("Failed to attach panel: %d\n", err);
579                         return err;
580                 }
581         }
582
583         return 0;
584 }
585
586 static enum drm_mode_status
587 anx6345_bridge_mode_valid(struct drm_bridge *bridge,
588                           const struct drm_display_mode *mode)
589 {
590         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
591                 return MODE_NO_INTERLACE;
592
593         /* Max 1200p at 5.4 Ghz, one lane */
594         if (mode->clock > 154000)
595                 return MODE_CLOCK_HIGH;
596
597         return MODE_OK;
598 }
599
600 static void anx6345_bridge_disable(struct drm_bridge *bridge)
601 {
602         struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
603
604         /* Power off all modules except configuration registers access */
605         anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
606                          SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
607         if (anx6345->panel)
608                 drm_panel_disable(anx6345->panel);
609
610         if (anx6345->powered)
611                 anx6345_poweroff(anx6345);
612 }
613
614 static void anx6345_bridge_enable(struct drm_bridge *bridge)
615 {
616         struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
617         int err;
618
619         if (anx6345->panel)
620                 drm_panel_enable(anx6345->panel);
621
622         err = anx6345_start(anx6345);
623         if (err) {
624                 DRM_ERROR("Failed to initialize: %d\n", err);
625                 return;
626         }
627
628         err = anx6345_config_dp_output(anx6345);
629         if (err)
630                 DRM_ERROR("Failed to enable DP output: %d\n", err);
631 }
632
633 static const struct drm_bridge_funcs anx6345_bridge_funcs = {
634         .attach = anx6345_bridge_attach,
635         .mode_valid = anx6345_bridge_mode_valid,
636         .disable = anx6345_bridge_disable,
637         .enable = anx6345_bridge_enable,
638 };
639
640 static void unregister_i2c_dummy_clients(struct anx6345 *anx6345)
641 {
642         unsigned int i;
643
644         for (i = 1; i < ARRAY_SIZE(anx6345->i2c_clients); i++)
645                 if (anx6345->i2c_clients[i] &&
646                     anx6345->i2c_clients[i]->addr != anx6345->client->addr)
647                         i2c_unregister_device(anx6345->i2c_clients[i]);
648 }
649
650 static const struct regmap_config anx6345_regmap_config = {
651         .reg_bits = 8,
652         .val_bits = 8,
653         .max_register = 0xff,
654         .cache_type = REGCACHE_NONE,
655 };
656
657 static const u16 anx6345_chipid_list[] = {
658         0x6345,
659 };
660
661 static bool anx6345_get_chip_id(struct anx6345 *anx6345)
662 {
663         unsigned int i, idl, idh, version;
664
665         if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDL_REG, &idl))
666                 return false;
667
668         if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDH_REG, &idh))
669                 return false;
670
671         anx6345->chipid = (u8)idl | ((u8)idh << 8);
672
673         if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_VERSION_REG,
674                         &version))
675                 return false;
676
677         for (i = 0; i < ARRAY_SIZE(anx6345_chipid_list); i++) {
678                 if (anx6345->chipid == anx6345_chipid_list[i]) {
679                         DRM_INFO("Found ANX%x (ver. %d) eDP Transmitter\n",
680                                  anx6345->chipid, version);
681                         return true;
682                 }
683         }
684
685         DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
686                   anx6345->chipid, version);
687
688         return false;
689 }
690
691 static int anx6345_i2c_probe(struct i2c_client *client,
692                              const struct i2c_device_id *id)
693 {
694         struct anx6345 *anx6345;
695         struct device *dev;
696         int i, err;
697
698         anx6345 = devm_kzalloc(&client->dev, sizeof(*anx6345), GFP_KERNEL);
699         if (!anx6345)
700                 return -ENOMEM;
701
702         mutex_init(&anx6345->lock);
703
704         anx6345->bridge.of_node = client->dev.of_node;
705
706         anx6345->client = client;
707         i2c_set_clientdata(client, anx6345);
708
709         dev = &anx6345->client->dev;
710
711         err = drm_of_find_panel_or_bridge(client->dev.of_node, 1, 0,
712                                           &anx6345->panel, NULL);
713         if (err == -EPROBE_DEFER)
714                 return err;
715
716         if (err)
717                 DRM_DEBUG("No panel found\n");
718
719         /* 1.2V digital core power regulator  */
720         anx6345->dvdd12 = devm_regulator_get(dev, "dvdd12");
721         if (IS_ERR(anx6345->dvdd12)) {
722                 if (PTR_ERR(anx6345->dvdd12) != -EPROBE_DEFER)
723                         DRM_ERROR("Failed to get dvdd12 supply (%ld)\n",
724                                   PTR_ERR(anx6345->dvdd12));
725                 return PTR_ERR(anx6345->dvdd12);
726         }
727
728         /* 2.5V digital core power regulator  */
729         anx6345->dvdd25 = devm_regulator_get(dev, "dvdd25");
730         if (IS_ERR(anx6345->dvdd25)) {
731                 if (PTR_ERR(anx6345->dvdd25) != -EPROBE_DEFER)
732                         DRM_ERROR("Failed to get dvdd25 supply (%ld)\n",
733                                   PTR_ERR(anx6345->dvdd25));
734                 return PTR_ERR(anx6345->dvdd25);
735         }
736
737         /* GPIO for chip reset */
738         anx6345->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
739         if (IS_ERR(anx6345->gpiod_reset)) {
740                 DRM_ERROR("Reset gpio not found\n");
741                 return PTR_ERR(anx6345->gpiod_reset);
742         }
743
744         /* Map slave addresses of ANX6345 */
745         for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
746                 if (anx6345_i2c_addresses[i] >> 1 != client->addr)
747                         anx6345->i2c_clients[i] = i2c_new_dummy_device(client->adapter,
748                                                 anx6345_i2c_addresses[i] >> 1);
749                 else
750                         anx6345->i2c_clients[i] = client;
751
752                 if (IS_ERR(anx6345->i2c_clients[i])) {
753                         err = PTR_ERR(anx6345->i2c_clients[i]);
754                         DRM_ERROR("Failed to reserve I2C bus %02x\n",
755                                   anx6345_i2c_addresses[i]);
756                         goto err_unregister_i2c;
757                 }
758
759                 anx6345->map[i] = devm_regmap_init_i2c(anx6345->i2c_clients[i],
760                                                        &anx6345_regmap_config);
761                 if (IS_ERR(anx6345->map[i])) {
762                         err = PTR_ERR(anx6345->map[i]);
763                         DRM_ERROR("Failed regmap initialization %02x\n",
764                                   anx6345_i2c_addresses[i]);
765                         goto err_unregister_i2c;
766                 }
767         }
768
769         /* Look for supported chip ID */
770         anx6345_poweron(anx6345);
771         if (anx6345_get_chip_id(anx6345)) {
772                 anx6345->bridge.funcs = &anx6345_bridge_funcs;
773                 drm_bridge_add(&anx6345->bridge);
774
775                 return 0;
776         } else {
777                 anx6345_poweroff(anx6345);
778                 err = -ENODEV;
779         }
780
781 err_unregister_i2c:
782         unregister_i2c_dummy_clients(anx6345);
783         return err;
784 }
785
786 static int anx6345_i2c_remove(struct i2c_client *client)
787 {
788         struct anx6345 *anx6345 = i2c_get_clientdata(client);
789
790         drm_bridge_remove(&anx6345->bridge);
791
792         unregister_i2c_dummy_clients(anx6345);
793
794         kfree(anx6345->edid);
795
796         mutex_destroy(&anx6345->lock);
797
798         return 0;
799 }
800
801 static const struct i2c_device_id anx6345_id[] = {
802         { "anx6345", 0 },
803         { /* sentinel */ }
804 };
805 MODULE_DEVICE_TABLE(i2c, anx6345_id);
806
807 static const struct of_device_id anx6345_match_table[] = {
808         { .compatible = "analogix,anx6345", },
809         { /* sentinel */ },
810 };
811 MODULE_DEVICE_TABLE(of, anx6345_match_table);
812
813 static struct i2c_driver anx6345_driver = {
814         .driver = {
815                    .name = "anx6345",
816                    .of_match_table = of_match_ptr(anx6345_match_table),
817                   },
818         .probe = anx6345_i2c_probe,
819         .remove = anx6345_i2c_remove,
820         .id_table = anx6345_id,
821 };
822 module_i2c_driver(anx6345_driver);
823
824 MODULE_DESCRIPTION("ANX6345 eDP Transmitter driver");
825 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
826 MODULE_LICENSE("GPL v2");