treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / analogix / analogix-anx6345.c
blob56f55c53abfd8bfccdc6cf88e94ea211cf001f1f
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright(c) 2016, Analogix Semiconductor.
4 * Copyright(c) 2017, Icenowy Zheng <icenowy@aosc.io>
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>
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>
32 #include "analogix-i2c-dptx.h"
33 #include "analogix-i2c-txcommon.h"
35 #define POLL_DELAY 50000 /* us */
36 #define POLL_TIMEOUT 5000000 /* us */
38 #define I2C_IDX_DPTX 0
39 #define I2C_IDX_TXCOM 1
41 static const u8 anx6345_i2c_addresses[] = {
42 [I2C_IDX_DPTX] = 0x70,
43 [I2C_IDX_TXCOM] = 0x72,
45 #define I2C_NUM_ADDRESSES ARRAY_SIZE(anx6345_i2c_addresses)
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 */
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];
63 u16 chipid;
64 u8 dpcd[DP_RECEIVER_CAP_SIZE];
66 bool powered;
69 static inline struct anx6345 *connector_to_anx6345(struct drm_connector *c)
71 return container_of(c, struct anx6345, connector);
74 static inline struct anx6345 *bridge_to_anx6345(struct drm_bridge *bridge)
76 return container_of(bridge, struct anx6345, bridge);
79 static int anx6345_set_bits(struct regmap *map, u8 reg, u8 mask)
81 return regmap_update_bits(map, reg, mask, mask);
84 static int anx6345_clear_bits(struct regmap *map, u8 reg, u8 mask)
86 return regmap_update_bits(map, reg, mask, 0);
89 static ssize_t anx6345_aux_transfer(struct drm_dp_aux *aux,
90 struct drm_dp_aux_msg *msg)
92 struct anx6345 *anx6345 = container_of(aux, struct anx6345, aux);
94 return anx_dp_aux_transfer(anx6345->map[I2C_IDX_DPTX], msg);
97 static int anx6345_dp_link_training(struct anx6345 *anx6345)
99 unsigned int value;
100 u8 dp_bw, dpcd[2];
101 int err;
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;
109 err = drm_dp_dpcd_readb(&anx6345->aux, DP_MAX_LINK_RATE, &dp_bw);
110 if (err < 0)
111 return err;
113 switch (dp_bw) {
114 case DP_LINK_BW_1_62:
115 case DP_LINK_BW_2_7:
116 break;
118 default:
119 DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);
120 return -EINVAL;
123 err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
124 SP_VIDEO_MUTE);
125 if (err)
126 return err;
128 err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
129 SP_VID_CTRL1_REG, SP_VIDEO_EN);
130 if (err)
131 return err;
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;
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;
148 * Power up the sink (DP_SET_POWER register is only available on DPCD
149 * v1.1 and later).
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;
159 dpcd[0] &= ~DP_SET_POWER_MASK;
160 dpcd[0] |= DP_SET_POWER_D0;
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;
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).
174 usleep_range(1000, 2000);
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;
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;
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;
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;
213 dpcd[0] = drm_dp_max_link_rate(anx6345->dpcd);
214 dpcd[0] = drm_dp_link_rate_to_bw_code(dpcd[0]);
215 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
216 SP_DP_MAIN_LINK_BW_SET_REG, dpcd[0]);
217 if (err)
218 return err;
220 dpcd[1] = drm_dp_max_lane_count(anx6345->dpcd);
222 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
223 SP_DP_LANE_COUNT_SET_REG, dpcd[1]);
224 if (err)
225 return err;
227 if (drm_dp_enhanced_frame_cap(anx6345->dpcd))
228 dpcd[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
230 err = drm_dp_dpcd_write(&anx6345->aux, DP_LINK_BW_SET, dpcd,
231 sizeof(dpcd));
233 if (err < 0) {
234 DRM_ERROR("Failed to configure link: %d\n", err);
235 return err;
238 /* Start training on the source */
239 err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_LT_CTRL_REG,
240 SP_LT_EN);
241 if (err)
242 return err;
244 return regmap_read_poll_timeout(anx6345->map[I2C_IDX_DPTX],
245 SP_DP_LT_CTRL_REG,
246 value, !(value & SP_DP_LT_INPROGRESS),
247 POLL_DELAY, POLL_TIMEOUT);
250 static int anx6345_tx_initialization(struct anx6345 *anx6345)
252 int err, i;
254 /* FIXME: colordepth is hardcoded for now */
255 err = regmap_write(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL2_REG,
256 SP_IN_BPC_6BIT << SP_IN_BPC_SHIFT);
257 if (err)
258 return err;
260 err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_PLL_CTRL_REG, 0);
261 if (err)
262 return err;
264 err = regmap_write(anx6345->map[I2C_IDX_TXCOM],
265 SP_ANALOG_DEBUG1_REG, 0);
266 if (err)
267 return err;
269 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
270 SP_DP_LINK_DEBUG_CTRL_REG,
271 SP_NEW_PRBS7 | SP_M_VID_DEBUG);
272 if (err)
273 return err;
275 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
276 SP_DP_ANALOG_POWER_DOWN_REG, 0);
277 if (err)
278 return err;
280 /* Force HPD */
281 err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
282 SP_DP_SYSTEM_CTRL_BASE + 3,
283 SP_HPD_FORCE | SP_HPD_CTRL);
284 if (err)
285 return err;
287 for (i = 0; i < 4; i++) {
288 /* 4 lanes */
289 err = regmap_write(anx6345->map[I2C_IDX_DPTX],
290 SP_DP_LANE0_LT_CTRL_REG + i, 0);
291 if (err)
292 return err;
295 /* Reset AUX */
296 err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM],
297 SP_RESET_CTRL2_REG, SP_AUX_RST);
298 if (err)
299 return err;
301 return anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
302 SP_RESET_CTRL2_REG, SP_AUX_RST);
305 static void anx6345_poweron(struct anx6345 *anx6345)
307 int err;
309 /* Ensure reset is asserted before starting power on sequence */
310 gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
311 usleep_range(1000, 2000);
313 err = regulator_enable(anx6345->dvdd12);
314 if (err) {
315 DRM_ERROR("Failed to enable dvdd12 regulator: %d\n",
316 err);
317 return;
320 /* T1 - delay between VDD12 and VDD25 should be 0-2ms */
321 usleep_range(1000, 2000);
323 err = regulator_enable(anx6345->dvdd25);
324 if (err) {
325 DRM_ERROR("Failed to enable dvdd25 regulator: %d\n",
326 err);
327 return;
330 /* T2 - delay between RESETN and all power rail stable,
331 * should be 2-5ms
333 usleep_range(2000, 5000);
335 gpiod_set_value_cansleep(anx6345->gpiod_reset, 0);
337 /* Power on registers module */
338 anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
339 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
340 anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
341 SP_REGISTER_PD | SP_TOTAL_PD);
343 if (anx6345->panel)
344 drm_panel_prepare(anx6345->panel);
346 anx6345->powered = true;
349 static void anx6345_poweroff(struct anx6345 *anx6345)
351 int err;
353 gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
354 usleep_range(1000, 2000);
356 if (anx6345->panel)
357 drm_panel_unprepare(anx6345->panel);
359 err = regulator_disable(anx6345->dvdd25);
360 if (err) {
361 DRM_ERROR("Failed to disable dvdd25 regulator: %d\n",
362 err);
363 return;
366 usleep_range(5000, 10000);
368 err = regulator_disable(anx6345->dvdd12);
369 if (err) {
370 DRM_ERROR("Failed to disable dvdd12 regulator: %d\n",
371 err);
372 return;
375 usleep_range(1000, 2000);
377 anx6345->powered = false;
380 static int anx6345_start(struct anx6345 *anx6345)
382 int err;
384 if (!anx6345->powered)
385 anx6345_poweron(anx6345);
387 /* Power on needed modules */
388 err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
389 SP_POWERDOWN_CTRL_REG,
390 SP_VIDEO_PD | SP_LINK_PD);
392 err = anx6345_tx_initialization(anx6345);
393 if (err) {
394 DRM_ERROR("Failed eDP transmitter initialization: %d\n", err);
395 anx6345_poweroff(anx6345);
396 return err;
399 err = anx6345_dp_link_training(anx6345);
400 if (err) {
401 DRM_ERROR("Failed link training: %d\n", err);
402 anx6345_poweroff(anx6345);
403 return err;
407 * This delay seems to help keep the hardware in a good state. Without
408 * it, there are times where it fails silently.
410 usleep_range(10000, 15000);
412 return 0;
415 static int anx6345_config_dp_output(struct anx6345 *anx6345)
417 int err;
419 err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
420 SP_VIDEO_MUTE);
421 if (err)
422 return err;
424 /* Enable DP output */
425 err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
426 SP_VIDEO_EN);
427 if (err)
428 return err;
430 /* Force stream valid */
431 return anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
432 SP_DP_SYSTEM_CTRL_BASE + 3,
433 SP_STRM_FORCE | SP_STRM_CTRL);
436 static int anx6345_get_downstream_info(struct anx6345 *anx6345)
438 u8 value;
439 int err;
441 err = drm_dp_dpcd_readb(&anx6345->aux, DP_SINK_COUNT, &value);
442 if (err < 0) {
443 DRM_ERROR("Get sink count failed %d\n", err);
444 return err;
447 if (!DP_GET_SINK_COUNT(value)) {
448 DRM_ERROR("Downstream disconnected\n");
449 return -EIO;
452 return 0;
455 static int anx6345_get_modes(struct drm_connector *connector)
457 struct anx6345 *anx6345 = connector_to_anx6345(connector);
458 int err, num_modes = 0;
459 bool power_off = false;
461 mutex_lock(&anx6345->lock);
463 if (!anx6345->edid) {
464 if (!anx6345->powered) {
465 anx6345_poweron(anx6345);
466 power_off = true;
469 err = anx6345_get_downstream_info(anx6345);
470 if (err) {
471 DRM_ERROR("Failed to get downstream info: %d\n", err);
472 goto unlock;
475 anx6345->edid = drm_get_edid(connector, &anx6345->aux.ddc);
476 if (!anx6345->edid)
477 DRM_ERROR("Failed to read EDID from panel\n");
479 err = drm_connector_update_edid_property(connector,
480 anx6345->edid);
481 if (err) {
482 DRM_ERROR("Failed to update EDID property: %d\n", err);
483 goto unlock;
487 num_modes += drm_add_edid_modes(connector, anx6345->edid);
489 unlock:
490 if (power_off)
491 anx6345_poweroff(anx6345);
493 mutex_unlock(&anx6345->lock);
495 if (!num_modes && anx6345->panel)
496 num_modes += drm_panel_get_modes(anx6345->panel, connector);
498 return num_modes;
501 static const struct drm_connector_helper_funcs anx6345_connector_helper_funcs = {
502 .get_modes = anx6345_get_modes,
505 static void
506 anx6345_connector_destroy(struct drm_connector *connector)
508 struct anx6345 *anx6345 = connector_to_anx6345(connector);
510 if (anx6345->panel)
511 drm_panel_detach(anx6345->panel);
512 drm_connector_cleanup(connector);
515 static const struct drm_connector_funcs anx6345_connector_funcs = {
516 .fill_modes = drm_helper_probe_single_connector_modes,
517 .destroy = anx6345_connector_destroy,
518 .reset = drm_atomic_helper_connector_reset,
519 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
520 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
523 static int anx6345_bridge_attach(struct drm_bridge *bridge)
525 struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
526 int err;
528 if (!bridge->encoder) {
529 DRM_ERROR("Parent encoder object not found");
530 return -ENODEV;
533 /* Register aux channel */
534 anx6345->aux.name = "DP-AUX";
535 anx6345->aux.dev = &anx6345->client->dev;
536 anx6345->aux.transfer = anx6345_aux_transfer;
538 err = drm_dp_aux_register(&anx6345->aux);
539 if (err < 0) {
540 DRM_ERROR("Failed to register aux channel: %d\n", err);
541 return err;
544 err = drm_connector_init(bridge->dev, &anx6345->connector,
545 &anx6345_connector_funcs,
546 DRM_MODE_CONNECTOR_eDP);
547 if (err) {
548 DRM_ERROR("Failed to initialize connector: %d\n", err);
549 return err;
552 drm_connector_helper_add(&anx6345->connector,
553 &anx6345_connector_helper_funcs);
555 err = drm_connector_register(&anx6345->connector);
556 if (err) {
557 DRM_ERROR("Failed to register connector: %d\n", err);
558 return err;
561 anx6345->connector.polled = DRM_CONNECTOR_POLL_HPD;
563 err = drm_connector_attach_encoder(&anx6345->connector,
564 bridge->encoder);
565 if (err) {
566 DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
567 return err;
570 if (anx6345->panel) {
571 err = drm_panel_attach(anx6345->panel, &anx6345->connector);
572 if (err) {
573 DRM_ERROR("Failed to attach panel: %d\n", err);
574 return err;
578 return 0;
581 static enum drm_mode_status
582 anx6345_bridge_mode_valid(struct drm_bridge *bridge,
583 const struct drm_display_mode *mode)
585 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
586 return MODE_NO_INTERLACE;
588 /* Max 1200p at 5.4 Ghz, one lane */
589 if (mode->clock > 154000)
590 return MODE_CLOCK_HIGH;
592 return MODE_OK;
595 static void anx6345_bridge_disable(struct drm_bridge *bridge)
597 struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
599 /* Power off all modules except configuration registers access */
600 anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
601 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
602 if (anx6345->panel)
603 drm_panel_disable(anx6345->panel);
605 if (anx6345->powered)
606 anx6345_poweroff(anx6345);
609 static void anx6345_bridge_enable(struct drm_bridge *bridge)
611 struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
612 int err;
614 if (anx6345->panel)
615 drm_panel_enable(anx6345->panel);
617 err = anx6345_start(anx6345);
618 if (err) {
619 DRM_ERROR("Failed to initialize: %d\n", err);
620 return;
623 err = anx6345_config_dp_output(anx6345);
624 if (err)
625 DRM_ERROR("Failed to enable DP output: %d\n", err);
628 static const struct drm_bridge_funcs anx6345_bridge_funcs = {
629 .attach = anx6345_bridge_attach,
630 .mode_valid = anx6345_bridge_mode_valid,
631 .disable = anx6345_bridge_disable,
632 .enable = anx6345_bridge_enable,
635 static void unregister_i2c_dummy_clients(struct anx6345 *anx6345)
637 unsigned int i;
639 for (i = 1; i < ARRAY_SIZE(anx6345->i2c_clients); i++)
640 if (anx6345->i2c_clients[i] &&
641 anx6345->i2c_clients[i]->addr != anx6345->client->addr)
642 i2c_unregister_device(anx6345->i2c_clients[i]);
645 static const struct regmap_config anx6345_regmap_config = {
646 .reg_bits = 8,
647 .val_bits = 8,
648 .max_register = 0xff,
649 .cache_type = REGCACHE_NONE,
652 static const u16 anx6345_chipid_list[] = {
653 0x6345,
656 static bool anx6345_get_chip_id(struct anx6345 *anx6345)
658 unsigned int i, idl, idh, version;
660 if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDL_REG, &idl))
661 return false;
663 if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDH_REG, &idh))
664 return false;
666 anx6345->chipid = (u8)idl | ((u8)idh << 8);
668 if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_VERSION_REG,
669 &version))
670 return false;
672 for (i = 0; i < ARRAY_SIZE(anx6345_chipid_list); i++) {
673 if (anx6345->chipid == anx6345_chipid_list[i]) {
674 DRM_INFO("Found ANX%x (ver. %d) eDP Transmitter\n",
675 anx6345->chipid, version);
676 return true;
680 DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
681 anx6345->chipid, version);
683 return false;
686 static int anx6345_i2c_probe(struct i2c_client *client,
687 const struct i2c_device_id *id)
689 struct anx6345 *anx6345;
690 struct device *dev;
691 int i, err;
693 anx6345 = devm_kzalloc(&client->dev, sizeof(*anx6345), GFP_KERNEL);
694 if (!anx6345)
695 return -ENOMEM;
697 mutex_init(&anx6345->lock);
699 anx6345->bridge.of_node = client->dev.of_node;
701 anx6345->client = client;
702 i2c_set_clientdata(client, anx6345);
704 dev = &anx6345->client->dev;
706 err = drm_of_find_panel_or_bridge(client->dev.of_node, 1, 0,
707 &anx6345->panel, NULL);
708 if (err == -EPROBE_DEFER)
709 return err;
711 if (err)
712 DRM_DEBUG("No panel found\n");
714 /* 1.2V digital core power regulator */
715 anx6345->dvdd12 = devm_regulator_get(dev, "dvdd12-supply");
716 if (IS_ERR(anx6345->dvdd12)) {
717 DRM_ERROR("dvdd12-supply not found\n");
718 return PTR_ERR(anx6345->dvdd12);
721 /* 2.5V digital core power regulator */
722 anx6345->dvdd25 = devm_regulator_get(dev, "dvdd25-supply");
723 if (IS_ERR(anx6345->dvdd25)) {
724 DRM_ERROR("dvdd25-supply not found\n");
725 return PTR_ERR(anx6345->dvdd25);
728 /* GPIO for chip reset */
729 anx6345->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
730 if (IS_ERR(anx6345->gpiod_reset)) {
731 DRM_ERROR("Reset gpio not found\n");
732 return PTR_ERR(anx6345->gpiod_reset);
735 /* Map slave addresses of ANX6345 */
736 for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
737 if (anx6345_i2c_addresses[i] >> 1 != client->addr)
738 anx6345->i2c_clients[i] = i2c_new_dummy_device(client->adapter,
739 anx6345_i2c_addresses[i] >> 1);
740 else
741 anx6345->i2c_clients[i] = client;
743 if (IS_ERR(anx6345->i2c_clients[i])) {
744 err = PTR_ERR(anx6345->i2c_clients[i]);
745 DRM_ERROR("Failed to reserve I2C bus %02x\n",
746 anx6345_i2c_addresses[i]);
747 goto err_unregister_i2c;
750 anx6345->map[i] = devm_regmap_init_i2c(anx6345->i2c_clients[i],
751 &anx6345_regmap_config);
752 if (IS_ERR(anx6345->map[i])) {
753 err = PTR_ERR(anx6345->map[i]);
754 DRM_ERROR("Failed regmap initialization %02x\n",
755 anx6345_i2c_addresses[i]);
756 goto err_unregister_i2c;
760 /* Look for supported chip ID */
761 anx6345_poweron(anx6345);
762 if (anx6345_get_chip_id(anx6345)) {
763 anx6345->bridge.funcs = &anx6345_bridge_funcs;
764 drm_bridge_add(&anx6345->bridge);
766 return 0;
767 } else {
768 anx6345_poweroff(anx6345);
769 err = -ENODEV;
772 err_unregister_i2c:
773 unregister_i2c_dummy_clients(anx6345);
774 return err;
777 static int anx6345_i2c_remove(struct i2c_client *client)
779 struct anx6345 *anx6345 = i2c_get_clientdata(client);
781 drm_bridge_remove(&anx6345->bridge);
783 unregister_i2c_dummy_clients(anx6345);
785 kfree(anx6345->edid);
787 mutex_destroy(&anx6345->lock);
789 return 0;
792 static const struct i2c_device_id anx6345_id[] = {
793 { "anx6345", 0 },
794 { /* sentinel */ }
796 MODULE_DEVICE_TABLE(i2c, anx6345_id);
798 static const struct of_device_id anx6345_match_table[] = {
799 { .compatible = "analogix,anx6345", },
800 { /* sentinel */ },
802 MODULE_DEVICE_TABLE(of, anx6345_match_table);
804 static struct i2c_driver anx6345_driver = {
805 .driver = {
806 .name = "anx6345",
807 .of_match_table = of_match_ptr(anx6345_match_table),
809 .probe = anx6345_i2c_probe,
810 .remove = anx6345_i2c_remove,
811 .id_table = anx6345_id,
813 module_i2c_driver(anx6345_driver);
815 MODULE_DESCRIPTION("ANX6345 eDP Transmitter driver");
816 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
817 MODULE_LICENSE("GPL v2");