xref: /openbmc/linux/drivers/gpu/drm/bridge/sii902x.c (revision fadbafc1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2018 Renesas Electronics
4  *
5  * Copyright (C) 2016 Atmel
6  *		      Bo Shen <voice.shen@atmel.com>
7  *
8  * Authors:	      Bo Shen <voice.shen@atmel.com>
9  *		      Boris Brezillon <boris.brezillon@free-electrons.com>
10  *		      Wu, Songjun <Songjun.Wu@atmel.com>
11  *
12  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
13  */
14 
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c-mux.h>
17 #include <linux/i2c.h>
18 #include <linux/media-bus-format.h>
19 #include <linux/module.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/clk.h>
23 
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_bridge.h>
26 #include <drm/drm_drv.h>
27 #include <drm/drm_edid.h>
28 #include <drm/drm_print.h>
29 #include <drm/drm_probe_helper.h>
30 
31 #include <sound/hdmi-codec.h>
32 
33 #define SII902X_TPI_VIDEO_DATA			0x0
34 
35 #define SII902X_TPI_PIXEL_REPETITION		0x8
36 #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT     BIT(5)
37 #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE   BIT(4)
38 #define SII902X_TPI_AVI_PIXEL_REP_4X		3
39 #define SII902X_TPI_AVI_PIXEL_REP_2X		1
40 #define SII902X_TPI_AVI_PIXEL_REP_NONE		0
41 #define SII902X_TPI_CLK_RATIO_HALF		(0 << 6)
42 #define SII902X_TPI_CLK_RATIO_1X		(1 << 6)
43 #define SII902X_TPI_CLK_RATIO_2X		(2 << 6)
44 #define SII902X_TPI_CLK_RATIO_4X		(3 << 6)
45 
46 #define SII902X_TPI_AVI_IN_FORMAT		0x9
47 #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT	BIT(7)
48 #define SII902X_TPI_AVI_INPUT_DITHER		BIT(6)
49 #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED	(2 << 2)
50 #define SII902X_TPI_AVI_INPUT_RANGE_FULL	(1 << 2)
51 #define SII902X_TPI_AVI_INPUT_RANGE_AUTO	(0 << 2)
52 #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK	(3 << 0)
53 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422	(2 << 0)
54 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444	(1 << 0)
55 #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB	(0 << 0)
56 
57 #define SII902X_TPI_AVI_INFOFRAME		0x0c
58 
59 #define SII902X_SYS_CTRL_DATA			0x1a
60 #define SII902X_SYS_CTRL_PWR_DWN		BIT(4)
61 #define SII902X_SYS_CTRL_AV_MUTE		BIT(3)
62 #define SII902X_SYS_CTRL_DDC_BUS_REQ		BIT(2)
63 #define SII902X_SYS_CTRL_DDC_BUS_GRTD		BIT(1)
64 #define SII902X_SYS_CTRL_OUTPUT_MODE		BIT(0)
65 #define SII902X_SYS_CTRL_OUTPUT_HDMI		1
66 #define SII902X_SYS_CTRL_OUTPUT_DVI		0
67 
68 #define SII902X_REG_CHIPID(n)			(0x1b + (n))
69 
70 #define SII902X_PWR_STATE_CTRL			0x1e
71 #define SII902X_AVI_POWER_STATE_MSK		GENMASK(1, 0)
72 #define SII902X_AVI_POWER_STATE_D(l)		((l) & SII902X_AVI_POWER_STATE_MSK)
73 
74 /* Audio  */
75 #define SII902X_TPI_I2S_ENABLE_MAPPING_REG	0x1f
76 #define SII902X_TPI_I2S_CONFIG_FIFO0			(0 << 0)
77 #define SII902X_TPI_I2S_CONFIG_FIFO1			(1 << 0)
78 #define SII902X_TPI_I2S_CONFIG_FIFO2			(2 << 0)
79 #define SII902X_TPI_I2S_CONFIG_FIFO3			(3 << 0)
80 #define SII902X_TPI_I2S_LEFT_RIGHT_SWAP			(1 << 2)
81 #define SII902X_TPI_I2S_AUTO_DOWNSAMPLE			(1 << 3)
82 #define SII902X_TPI_I2S_SELECT_SD0			(0 << 4)
83 #define SII902X_TPI_I2S_SELECT_SD1			(1 << 4)
84 #define SII902X_TPI_I2S_SELECT_SD2			(2 << 4)
85 #define SII902X_TPI_I2S_SELECT_SD3			(3 << 4)
86 #define SII902X_TPI_I2S_FIFO_ENABLE			(1 << 7)
87 
88 #define SII902X_TPI_I2S_INPUT_CONFIG_REG	0x20
89 #define SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES		(0 << 0)
90 #define SII902X_TPI_I2S_FIRST_BIT_SHIFT_NO		(1 << 0)
91 #define SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST		(0 << 1)
92 #define SII902X_TPI_I2S_SD_DIRECTION_LSB_FIRST		(1 << 1)
93 #define SII902X_TPI_I2S_SD_JUSTIFY_LEFT			(0 << 2)
94 #define SII902X_TPI_I2S_SD_JUSTIFY_RIGHT		(1 << 2)
95 #define SII902X_TPI_I2S_WS_POLARITY_LOW			(0 << 3)
96 #define SII902X_TPI_I2S_WS_POLARITY_HIGH		(1 << 3)
97 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_128		(0 << 4)
98 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_256		(1 << 4)
99 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_384		(2 << 4)
100 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_512		(3 << 4)
101 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_768		(4 << 4)
102 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_1024		(5 << 4)
103 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_1152		(6 << 4)
104 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_192		(7 << 4)
105 #define SII902X_TPI_I2S_SCK_EDGE_FALLING		(0 << 7)
106 #define SII902X_TPI_I2S_SCK_EDGE_RISING			(1 << 7)
107 
108 #define SII902X_TPI_I2S_STRM_HDR_BASE	0x21
109 #define SII902X_TPI_I2S_STRM_HDR_SIZE	5
110 
111 #define SII902X_TPI_AUDIO_CONFIG_BYTE2_REG	0x26
112 #define SII902X_TPI_AUDIO_CODING_STREAM_HEADER		(0 << 0)
113 #define SII902X_TPI_AUDIO_CODING_PCM			(1 << 0)
114 #define SII902X_TPI_AUDIO_CODING_AC3			(2 << 0)
115 #define SII902X_TPI_AUDIO_CODING_MPEG1			(3 << 0)
116 #define SII902X_TPI_AUDIO_CODING_MP3			(4 << 0)
117 #define SII902X_TPI_AUDIO_CODING_MPEG2			(5 << 0)
118 #define SII902X_TPI_AUDIO_CODING_AAC			(6 << 0)
119 #define SII902X_TPI_AUDIO_CODING_DTS			(7 << 0)
120 #define SII902X_TPI_AUDIO_CODING_ATRAC			(8 << 0)
121 #define SII902X_TPI_AUDIO_MUTE_DISABLE			(0 << 4)
122 #define SII902X_TPI_AUDIO_MUTE_ENABLE			(1 << 4)
123 #define SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS		(0 << 5)
124 #define SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS		(1 << 5)
125 #define SII902X_TPI_AUDIO_INTERFACE_DISABLE		(0 << 6)
126 #define SII902X_TPI_AUDIO_INTERFACE_SPDIF		(1 << 6)
127 #define SII902X_TPI_AUDIO_INTERFACE_I2S			(2 << 6)
128 
129 #define SII902X_TPI_AUDIO_CONFIG_BYTE3_REG	0x27
130 #define SII902X_TPI_AUDIO_FREQ_STREAM			(0 << 3)
131 #define SII902X_TPI_AUDIO_FREQ_32KHZ			(1 << 3)
132 #define SII902X_TPI_AUDIO_FREQ_44KHZ			(2 << 3)
133 #define SII902X_TPI_AUDIO_FREQ_48KHZ			(3 << 3)
134 #define SII902X_TPI_AUDIO_FREQ_88KHZ			(4 << 3)
135 #define SII902X_TPI_AUDIO_FREQ_96KHZ			(5 << 3)
136 #define SII902X_TPI_AUDIO_FREQ_176KHZ			(6 << 3)
137 #define SII902X_TPI_AUDIO_FREQ_192KHZ			(7 << 3)
138 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_STREAM		(0 << 6)
139 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_16		(1 << 6)
140 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_20		(2 << 6)
141 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_24		(3 << 6)
142 
143 #define SII902X_TPI_AUDIO_CONFIG_BYTE4_REG	0x28
144 
145 #define SII902X_INT_ENABLE			0x3c
146 #define SII902X_INT_STATUS			0x3d
147 #define SII902X_HOTPLUG_EVENT			BIT(0)
148 #define SII902X_PLUGGED_STATUS			BIT(2)
149 
150 #define SII902X_REG_TPI_RQB			0xc7
151 
152 /* Indirect internal register access */
153 #define SII902X_IND_SET_PAGE			0xbc
154 #define SII902X_IND_OFFSET			0xbd
155 #define SII902X_IND_VALUE			0xbe
156 
157 #define SII902X_TPI_MISC_INFOFRAME_BASE		0xbf
158 #define SII902X_TPI_MISC_INFOFRAME_END		0xde
159 #define SII902X_TPI_MISC_INFOFRAME_SIZE	\
160 	(SII902X_TPI_MISC_INFOFRAME_END - SII902X_TPI_MISC_INFOFRAME_BASE)
161 
162 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS	500
163 
164 #define SII902X_AUDIO_PORT_INDEX		3
165 
166 struct sii902x {
167 	struct i2c_client *i2c;
168 	struct regmap *regmap;
169 	struct drm_bridge bridge;
170 	struct drm_bridge *next_bridge;
171 	struct drm_connector connector;
172 	struct gpio_desc *reset_gpio;
173 	struct i2c_mux_core *i2cmux;
174 	struct regulator_bulk_data supplies[2];
175 	bool sink_is_hdmi;
176 	/*
177 	 * Mutex protects audio and video functions from interfering
178 	 * each other, by keeping their i2c command sequences atomic.
179 	 */
180 	struct mutex mutex;
181 	struct sii902x_audio {
182 		struct platform_device *pdev;
183 		struct clk *mclk;
184 		u32 i2s_fifo_sequence[4];
185 	} audio;
186 };
187 
188 static int sii902x_read_unlocked(struct i2c_client *i2c, u8 reg, u8 *val)
189 {
190 	union i2c_smbus_data data;
191 	int ret;
192 
193 	ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
194 			       I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data);
195 
196 	if (ret < 0)
197 		return ret;
198 
199 	*val = data.byte;
200 	return 0;
201 }
202 
203 static int sii902x_write_unlocked(struct i2c_client *i2c, u8 reg, u8 val)
204 {
205 	union i2c_smbus_data data;
206 
207 	data.byte = val;
208 
209 	return __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
210 				I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA,
211 				&data);
212 }
213 
214 static int sii902x_update_bits_unlocked(struct i2c_client *i2c, u8 reg, u8 mask,
215 					u8 val)
216 {
217 	int ret;
218 	u8 status;
219 
220 	ret = sii902x_read_unlocked(i2c, reg, &status);
221 	if (ret)
222 		return ret;
223 	status &= ~mask;
224 	status |= val & mask;
225 	return sii902x_write_unlocked(i2c, reg, status);
226 }
227 
228 static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge)
229 {
230 	return container_of(bridge, struct sii902x, bridge);
231 }
232 
233 static inline struct sii902x *connector_to_sii902x(struct drm_connector *con)
234 {
235 	return container_of(con, struct sii902x, connector);
236 }
237 
238 static void sii902x_reset(struct sii902x *sii902x)
239 {
240 	if (!sii902x->reset_gpio)
241 		return;
242 
243 	gpiod_set_value(sii902x->reset_gpio, 1);
244 
245 	/* The datasheet says treset-min = 100us. Make it 150us to be sure. */
246 	usleep_range(150, 200);
247 
248 	gpiod_set_value(sii902x->reset_gpio, 0);
249 }
250 
251 static enum drm_connector_status sii902x_detect(struct sii902x *sii902x)
252 {
253 	unsigned int status;
254 
255 	mutex_lock(&sii902x->mutex);
256 
257 	regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
258 
259 	mutex_unlock(&sii902x->mutex);
260 
261 	return (status & SII902X_PLUGGED_STATUS) ?
262 	       connector_status_connected : connector_status_disconnected;
263 }
264 
265 static enum drm_connector_status
266 sii902x_connector_detect(struct drm_connector *connector, bool force)
267 {
268 	struct sii902x *sii902x = connector_to_sii902x(connector);
269 
270 	return sii902x_detect(sii902x);
271 }
272 
273 static const struct drm_connector_funcs sii902x_connector_funcs = {
274 	.detect = sii902x_connector_detect,
275 	.fill_modes = drm_helper_probe_single_connector_modes,
276 	.destroy = drm_connector_cleanup,
277 	.reset = drm_atomic_helper_connector_reset,
278 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
279 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
280 };
281 
282 static struct edid *sii902x_get_edid(struct sii902x *sii902x,
283 				     struct drm_connector *connector)
284 {
285 	struct edid *edid;
286 
287 	mutex_lock(&sii902x->mutex);
288 
289 	edid = drm_get_edid(connector, sii902x->i2cmux->adapter[0]);
290 	if (edid) {
291 		if (drm_detect_hdmi_monitor(edid))
292 			sii902x->sink_is_hdmi = true;
293 		else
294 			sii902x->sink_is_hdmi = false;
295 	}
296 
297 	mutex_unlock(&sii902x->mutex);
298 
299 	return edid;
300 }
301 
302 static int sii902x_get_modes(struct drm_connector *connector)
303 {
304 	struct sii902x *sii902x = connector_to_sii902x(connector);
305 	struct edid *edid;
306 	int num = 0;
307 
308 	edid = sii902x_get_edid(sii902x, connector);
309 	drm_connector_update_edid_property(connector, edid);
310 	if (edid) {
311 		num = drm_add_edid_modes(connector, edid);
312 		kfree(edid);
313 	}
314 
315 	return num;
316 }
317 
318 static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector,
319 					       struct drm_display_mode *mode)
320 {
321 	/* TODO: check mode */
322 
323 	return MODE_OK;
324 }
325 
326 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = {
327 	.get_modes = sii902x_get_modes,
328 	.mode_valid = sii902x_mode_valid,
329 };
330 
331 static void sii902x_bridge_disable(struct drm_bridge *bridge)
332 {
333 	struct sii902x *sii902x = bridge_to_sii902x(bridge);
334 
335 	mutex_lock(&sii902x->mutex);
336 
337 	regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
338 			   SII902X_SYS_CTRL_PWR_DWN,
339 			   SII902X_SYS_CTRL_PWR_DWN);
340 
341 	mutex_unlock(&sii902x->mutex);
342 }
343 
344 static void sii902x_bridge_enable(struct drm_bridge *bridge)
345 {
346 	struct sii902x *sii902x = bridge_to_sii902x(bridge);
347 
348 	mutex_lock(&sii902x->mutex);
349 
350 	regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL,
351 			   SII902X_AVI_POWER_STATE_MSK,
352 			   SII902X_AVI_POWER_STATE_D(0));
353 	regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
354 			   SII902X_SYS_CTRL_PWR_DWN, 0);
355 
356 	mutex_unlock(&sii902x->mutex);
357 }
358 
359 static void sii902x_bridge_mode_set(struct drm_bridge *bridge,
360 				    const struct drm_display_mode *mode,
361 				    const struct drm_display_mode *adj)
362 {
363 	struct sii902x *sii902x = bridge_to_sii902x(bridge);
364 	u8 output_mode = SII902X_SYS_CTRL_OUTPUT_DVI;
365 	struct regmap *regmap = sii902x->regmap;
366 	u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
367 	struct hdmi_avi_infoframe frame;
368 	u16 pixel_clock_10kHz = adj->clock / 10;
369 	int ret;
370 
371 	if (sii902x->sink_is_hdmi)
372 		output_mode = SII902X_SYS_CTRL_OUTPUT_HDMI;
373 
374 	buf[0] = pixel_clock_10kHz & 0xff;
375 	buf[1] = pixel_clock_10kHz >> 8;
376 	buf[2] = drm_mode_vrefresh(adj);
377 	buf[3] = 0x00;
378 	buf[4] = adj->hdisplay;
379 	buf[5] = adj->hdisplay >> 8;
380 	buf[6] = adj->vdisplay;
381 	buf[7] = adj->vdisplay >> 8;
382 	buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE |
383 		 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT;
384 	buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO |
385 		 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB;
386 
387 	mutex_lock(&sii902x->mutex);
388 
389 	ret = regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA,
390 				 SII902X_SYS_CTRL_OUTPUT_MODE, output_mode);
391 	if (ret)
392 		goto out;
393 
394 	ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10);
395 	if (ret)
396 		goto out;
397 
398 	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame,
399 						       &sii902x->connector, adj);
400 	if (ret < 0) {
401 		DRM_ERROR("couldn't fill AVI infoframe\n");
402 		goto out;
403 	}
404 
405 	ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf));
406 	if (ret < 0) {
407 		DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
408 		goto out;
409 	}
410 
411 	/* Do not send the infoframe header, but keep the CRC field. */
412 	regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME,
413 			  buf + HDMI_INFOFRAME_HEADER_SIZE - 1,
414 			  HDMI_AVI_INFOFRAME_SIZE + 1);
415 
416 out:
417 	mutex_unlock(&sii902x->mutex);
418 }
419 
420 static int sii902x_bridge_attach(struct drm_bridge *bridge,
421 				 enum drm_bridge_attach_flags flags)
422 {
423 	struct sii902x *sii902x = bridge_to_sii902x(bridge);
424 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
425 	struct drm_device *drm = bridge->dev;
426 	int ret;
427 
428 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
429 		return drm_bridge_attach(bridge->encoder, sii902x->next_bridge,
430 					 bridge, flags);
431 
432 	drm_connector_helper_add(&sii902x->connector,
433 				 &sii902x_connector_helper_funcs);
434 
435 	if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) {
436 		dev_err(&sii902x->i2c->dev,
437 			"sii902x driver is only compatible with DRM devices supporting atomic updates\n");
438 		return -ENOTSUPP;
439 	}
440 
441 	ret = drm_connector_init(drm, &sii902x->connector,
442 				 &sii902x_connector_funcs,
443 				 DRM_MODE_CONNECTOR_HDMIA);
444 	if (ret)
445 		return ret;
446 
447 	if (sii902x->i2c->irq > 0)
448 		sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD;
449 	else
450 		sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
451 
452 	ret = drm_display_info_set_bus_formats(&sii902x->connector.display_info,
453 					       &bus_format, 1);
454 	if (ret)
455 		return ret;
456 
457 	drm_connector_attach_encoder(&sii902x->connector, bridge->encoder);
458 
459 	return 0;
460 }
461 
462 static enum drm_connector_status sii902x_bridge_detect(struct drm_bridge *bridge)
463 {
464 	struct sii902x *sii902x = bridge_to_sii902x(bridge);
465 
466 	return sii902x_detect(sii902x);
467 }
468 
469 static struct edid *sii902x_bridge_get_edid(struct drm_bridge *bridge,
470 					    struct drm_connector *connector)
471 {
472 	struct sii902x *sii902x = bridge_to_sii902x(bridge);
473 
474 	return sii902x_get_edid(sii902x, connector);
475 }
476 
477 static const struct drm_bridge_funcs sii902x_bridge_funcs = {
478 	.attach = sii902x_bridge_attach,
479 	.mode_set = sii902x_bridge_mode_set,
480 	.disable = sii902x_bridge_disable,
481 	.enable = sii902x_bridge_enable,
482 	.detect = sii902x_bridge_detect,
483 	.get_edid = sii902x_bridge_get_edid,
484 };
485 
486 static int sii902x_mute(struct sii902x *sii902x, bool mute)
487 {
488 	struct device *dev = &sii902x->i2c->dev;
489 	unsigned int val = mute ? SII902X_TPI_AUDIO_MUTE_ENABLE :
490 		SII902X_TPI_AUDIO_MUTE_DISABLE;
491 
492 	dev_dbg(dev, "%s: %s\n", __func__, mute ? "Muted" : "Unmuted");
493 
494 	return regmap_update_bits(sii902x->regmap,
495 				  SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
496 				  SII902X_TPI_AUDIO_MUTE_ENABLE, val);
497 }
498 
499 static const int sii902x_mclk_div_table[] = {
500 	128, 256, 384, 512, 768, 1024, 1152, 192 };
501 
502 static int sii902x_select_mclk_div(u8 *i2s_config_reg, unsigned int rate,
503 				   unsigned int mclk)
504 {
505 	int div = mclk / rate;
506 	int distance = 100000;
507 	u8 i, nearest = 0;
508 
509 	for (i = 0; i < ARRAY_SIZE(sii902x_mclk_div_table); i++) {
510 		unsigned int d = abs(div - sii902x_mclk_div_table[i]);
511 
512 		if (d >= distance)
513 			continue;
514 
515 		nearest = i;
516 		distance = d;
517 		if (d == 0)
518 			break;
519 	}
520 
521 	*i2s_config_reg |= nearest << 4;
522 
523 	return sii902x_mclk_div_table[nearest];
524 }
525 
526 static const struct sii902x_sample_freq {
527 	u32 freq;
528 	u8 val;
529 } sii902x_sample_freq[] = {
530 	{ .freq = 32000,	.val = SII902X_TPI_AUDIO_FREQ_32KHZ },
531 	{ .freq = 44000,	.val = SII902X_TPI_AUDIO_FREQ_44KHZ },
532 	{ .freq = 48000,	.val = SII902X_TPI_AUDIO_FREQ_48KHZ },
533 	{ .freq = 88000,	.val = SII902X_TPI_AUDIO_FREQ_88KHZ },
534 	{ .freq = 96000,	.val = SII902X_TPI_AUDIO_FREQ_96KHZ },
535 	{ .freq = 176000,	.val = SII902X_TPI_AUDIO_FREQ_176KHZ },
536 	{ .freq = 192000,	.val = SII902X_TPI_AUDIO_FREQ_192KHZ },
537 };
538 
539 static int sii902x_audio_hw_params(struct device *dev, void *data,
540 				   struct hdmi_codec_daifmt *daifmt,
541 				   struct hdmi_codec_params *params)
542 {
543 	struct sii902x *sii902x = dev_get_drvdata(dev);
544 	u8 i2s_config_reg = SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST;
545 	u8 config_byte2_reg = (SII902X_TPI_AUDIO_INTERFACE_I2S |
546 			       SII902X_TPI_AUDIO_MUTE_ENABLE |
547 			       SII902X_TPI_AUDIO_CODING_PCM);
548 	u8 config_byte3_reg = 0;
549 	u8 infoframe_buf[HDMI_INFOFRAME_SIZE(AUDIO)];
550 	unsigned long mclk_rate;
551 	int i, ret;
552 
553 	if (daifmt->bit_clk_provider || daifmt->frame_clk_provider) {
554 		dev_dbg(dev, "%s: I2S clock provider mode not supported\n",
555 			__func__);
556 		return -EINVAL;
557 	}
558 
559 	switch (daifmt->fmt) {
560 	case HDMI_I2S:
561 		i2s_config_reg |= SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES |
562 			SII902X_TPI_I2S_SD_JUSTIFY_LEFT;
563 		break;
564 	case HDMI_RIGHT_J:
565 		i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_RIGHT;
566 		break;
567 	case HDMI_LEFT_J:
568 		i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_LEFT;
569 		break;
570 	default:
571 		dev_dbg(dev, "%s: Unsupported i2s format %u\n", __func__,
572 			daifmt->fmt);
573 		return -EINVAL;
574 	}
575 
576 	if (daifmt->bit_clk_inv)
577 		i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_FALLING;
578 	else
579 		i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_RISING;
580 
581 	if (daifmt->frame_clk_inv)
582 		i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_LOW;
583 	else
584 		i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_HIGH;
585 
586 	if (params->channels > 2)
587 		config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS;
588 	else
589 		config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS;
590 
591 	switch (params->sample_width) {
592 	case 16:
593 		config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_16;
594 		break;
595 	case 20:
596 		config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_20;
597 		break;
598 	case 24:
599 	case 32:
600 		config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_24;
601 		break;
602 	default:
603 		dev_err(dev, "%s: Unsupported sample width %u\n", __func__,
604 			params->sample_width);
605 		return -EINVAL;
606 	}
607 
608 	for (i = 0; i < ARRAY_SIZE(sii902x_sample_freq); i++) {
609 		if (params->sample_rate == sii902x_sample_freq[i].freq) {
610 			config_byte3_reg |= sii902x_sample_freq[i].val;
611 			break;
612 		}
613 	}
614 
615 	ret = clk_prepare_enable(sii902x->audio.mclk);
616 	if (ret) {
617 		dev_err(dev, "Enabling mclk failed: %d\n", ret);
618 		return ret;
619 	}
620 
621 	if (sii902x->audio.mclk) {
622 		mclk_rate = clk_get_rate(sii902x->audio.mclk);
623 		ret = sii902x_select_mclk_div(&i2s_config_reg,
624 					      params->sample_rate, mclk_rate);
625 		if (mclk_rate != ret * params->sample_rate)
626 			dev_dbg(dev, "Inaccurate reference clock (%ld/%d != %u)\n",
627 				mclk_rate, ret, params->sample_rate);
628 	}
629 
630 	mutex_lock(&sii902x->mutex);
631 
632 	ret = regmap_write(sii902x->regmap,
633 			   SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
634 			   config_byte2_reg);
635 	if (ret < 0)
636 		goto out;
637 
638 	ret = regmap_write(sii902x->regmap, SII902X_TPI_I2S_INPUT_CONFIG_REG,
639 			   i2s_config_reg);
640 	if (ret)
641 		goto out;
642 
643 	for (i = 0; i < ARRAY_SIZE(sii902x->audio.i2s_fifo_sequence) &&
644 		    sii902x->audio.i2s_fifo_sequence[i]; i++)
645 		regmap_write(sii902x->regmap,
646 			     SII902X_TPI_I2S_ENABLE_MAPPING_REG,
647 			     sii902x->audio.i2s_fifo_sequence[i]);
648 
649 	ret = regmap_write(sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE3_REG,
650 			   config_byte3_reg);
651 	if (ret)
652 		goto out;
653 
654 	ret = regmap_bulk_write(sii902x->regmap, SII902X_TPI_I2S_STRM_HDR_BASE,
655 				params->iec.status,
656 				min((size_t) SII902X_TPI_I2S_STRM_HDR_SIZE,
657 				    sizeof(params->iec.status)));
658 	if (ret)
659 		goto out;
660 
661 	ret = hdmi_audio_infoframe_pack(&params->cea, infoframe_buf,
662 					sizeof(infoframe_buf));
663 	if (ret < 0) {
664 		dev_err(dev, "%s: Failed to pack audio infoframe: %d\n",
665 			__func__, ret);
666 		goto out;
667 	}
668 
669 	ret = regmap_bulk_write(sii902x->regmap,
670 				SII902X_TPI_MISC_INFOFRAME_BASE,
671 				infoframe_buf,
672 				min(ret, SII902X_TPI_MISC_INFOFRAME_SIZE));
673 	if (ret)
674 		goto out;
675 
676 	/* Decode Level 0 Packets */
677 	ret = regmap_write(sii902x->regmap, SII902X_IND_SET_PAGE, 0x02);
678 	if (ret)
679 		goto out;
680 
681 	ret = regmap_write(sii902x->regmap, SII902X_IND_OFFSET, 0x24);
682 	if (ret)
683 		goto out;
684 
685 	ret = regmap_write(sii902x->regmap, SII902X_IND_VALUE, 0x02);
686 	if (ret)
687 		goto out;
688 
689 	dev_dbg(dev, "%s: hdmi audio enabled\n", __func__);
690 out:
691 	mutex_unlock(&sii902x->mutex);
692 
693 	if (ret) {
694 		clk_disable_unprepare(sii902x->audio.mclk);
695 		dev_err(dev, "%s: hdmi audio enable failed: %d\n", __func__,
696 			ret);
697 	}
698 
699 	return ret;
700 }
701 
702 static void sii902x_audio_shutdown(struct device *dev, void *data)
703 {
704 	struct sii902x *sii902x = dev_get_drvdata(dev);
705 
706 	mutex_lock(&sii902x->mutex);
707 
708 	regmap_write(sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
709 		     SII902X_TPI_AUDIO_INTERFACE_DISABLE);
710 
711 	mutex_unlock(&sii902x->mutex);
712 
713 	clk_disable_unprepare(sii902x->audio.mclk);
714 }
715 
716 static int sii902x_audio_mute(struct device *dev, void *data,
717 			      bool enable, int direction)
718 {
719 	struct sii902x *sii902x = dev_get_drvdata(dev);
720 
721 	mutex_lock(&sii902x->mutex);
722 
723 	sii902x_mute(sii902x, enable);
724 
725 	mutex_unlock(&sii902x->mutex);
726 
727 	return 0;
728 }
729 
730 static int sii902x_audio_get_eld(struct device *dev, void *data,
731 				 uint8_t *buf, size_t len)
732 {
733 	struct sii902x *sii902x = dev_get_drvdata(dev);
734 
735 	mutex_lock(&sii902x->mutex);
736 
737 	memcpy(buf, sii902x->connector.eld,
738 	       min(sizeof(sii902x->connector.eld), len));
739 
740 	mutex_unlock(&sii902x->mutex);
741 
742 	return 0;
743 }
744 
745 static int sii902x_audio_get_dai_id(struct snd_soc_component *component,
746 				    struct device_node *endpoint)
747 {
748 	struct of_endpoint of_ep;
749 	int ret;
750 
751 	ret = of_graph_parse_endpoint(endpoint, &of_ep);
752 	if (ret < 0)
753 		return ret;
754 
755 	/*
756 	 * HDMI sound should be located at reg = <3>
757 	 * Return expected DAI index 0.
758 	 */
759 	if (of_ep.port == SII902X_AUDIO_PORT_INDEX)
760 		return 0;
761 
762 	return -EINVAL;
763 }
764 
765 static const struct hdmi_codec_ops sii902x_audio_codec_ops = {
766 	.hw_params = sii902x_audio_hw_params,
767 	.audio_shutdown = sii902x_audio_shutdown,
768 	.mute_stream = sii902x_audio_mute,
769 	.get_eld = sii902x_audio_get_eld,
770 	.get_dai_id = sii902x_audio_get_dai_id,
771 	.no_capture_mute = 1,
772 };
773 
774 static int sii902x_audio_codec_init(struct sii902x *sii902x,
775 				    struct device *dev)
776 {
777 	static const u8 audio_fifo_id[] = {
778 		SII902X_TPI_I2S_CONFIG_FIFO0,
779 		SII902X_TPI_I2S_CONFIG_FIFO1,
780 		SII902X_TPI_I2S_CONFIG_FIFO2,
781 		SII902X_TPI_I2S_CONFIG_FIFO3,
782 	};
783 	static const u8 i2s_lane_id[] = {
784 		SII902X_TPI_I2S_SELECT_SD0,
785 		SII902X_TPI_I2S_SELECT_SD1,
786 		SII902X_TPI_I2S_SELECT_SD2,
787 		SII902X_TPI_I2S_SELECT_SD3,
788 	};
789 	struct hdmi_codec_pdata codec_data = {
790 		.ops = &sii902x_audio_codec_ops,
791 		.i2s = 1, /* Only i2s support for now. */
792 		.spdif = 0,
793 		.max_i2s_channels = 0,
794 	};
795 	u8 lanes[4];
796 	int num_lanes, i;
797 
798 	if (!of_property_read_bool(dev->of_node, "#sound-dai-cells")) {
799 		dev_dbg(dev, "%s: No \"#sound-dai-cells\", no audio\n",
800 			__func__);
801 		return 0;
802 	}
803 
804 	num_lanes = of_property_read_variable_u8_array(dev->of_node,
805 						       "sil,i2s-data-lanes",
806 						       lanes, 1,
807 						       ARRAY_SIZE(lanes));
808 
809 	if (num_lanes == -EINVAL) {
810 		dev_dbg(dev,
811 			"%s: No \"sil,i2s-data-lanes\", use default <0>\n",
812 			__func__);
813 		num_lanes = 1;
814 		lanes[0] = 0;
815 	} else if (num_lanes < 0) {
816 		dev_err(dev,
817 			"%s: Error gettin \"sil,i2s-data-lanes\": %d\n",
818 			__func__, num_lanes);
819 		return num_lanes;
820 	}
821 	codec_data.max_i2s_channels = 2 * num_lanes;
822 
823 	for (i = 0; i < num_lanes; i++)
824 		sii902x->audio.i2s_fifo_sequence[i] |= audio_fifo_id[i] |
825 			i2s_lane_id[lanes[i]] |	SII902X_TPI_I2S_FIFO_ENABLE;
826 
827 	sii902x->audio.mclk = devm_clk_get_optional(dev, "mclk");
828 	if (IS_ERR(sii902x->audio.mclk)) {
829 		dev_err(dev, "%s: No clock (audio mclk) found: %ld\n",
830 			__func__, PTR_ERR(sii902x->audio.mclk));
831 		return PTR_ERR(sii902x->audio.mclk);
832 	}
833 
834 	sii902x->audio.pdev = platform_device_register_data(
835 		dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
836 		&codec_data, sizeof(codec_data));
837 
838 	return PTR_ERR_OR_ZERO(sii902x->audio.pdev);
839 }
840 
841 static const struct regmap_range sii902x_volatile_ranges[] = {
842 	{ .range_min = 0, .range_max = 0xff },
843 };
844 
845 static const struct regmap_access_table sii902x_volatile_table = {
846 	.yes_ranges = sii902x_volatile_ranges,
847 	.n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges),
848 };
849 
850 static const struct regmap_config sii902x_regmap_config = {
851 	.reg_bits = 8,
852 	.val_bits = 8,
853 	.disable_locking = true, /* struct sii902x mutex should be enough */
854 	.max_register = SII902X_TPI_MISC_INFOFRAME_END,
855 	.volatile_table = &sii902x_volatile_table,
856 	.cache_type = REGCACHE_NONE,
857 };
858 
859 static irqreturn_t sii902x_interrupt(int irq, void *data)
860 {
861 	struct sii902x *sii902x = data;
862 	unsigned int status = 0;
863 
864 	mutex_lock(&sii902x->mutex);
865 
866 	regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
867 	regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
868 
869 	mutex_unlock(&sii902x->mutex);
870 
871 	if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev) {
872 		drm_helper_hpd_irq_event(sii902x->bridge.dev);
873 		drm_bridge_hpd_notify(&sii902x->bridge, (status & SII902X_PLUGGED_STATUS)
874 								? connector_status_connected
875 								: connector_status_disconnected);
876 	}
877 
878 	return IRQ_HANDLED;
879 }
880 
881 /*
882  * The purpose of sii902x_i2c_bypass_select is to enable the pass through
883  * mode of the HDMI transmitter. Do not use regmap from within this function,
884  * only use sii902x_*_unlocked functions to read/modify/write registers.
885  * We are holding the parent adapter lock here, keep this in mind before
886  * adding more i2c transactions.
887  *
888  * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
889  * in this driver, we need to make sure that we only touch 0x1A[2:1] from
890  * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
891  * we leave the remaining bits as we have found them.
892  */
893 static int sii902x_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id)
894 {
895 	struct sii902x *sii902x = i2c_mux_priv(mux);
896 	struct device *dev = &sii902x->i2c->dev;
897 	unsigned long timeout;
898 	u8 status;
899 	int ret;
900 
901 	ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
902 					   SII902X_SYS_CTRL_DDC_BUS_REQ,
903 					   SII902X_SYS_CTRL_DDC_BUS_REQ);
904 	if (ret)
905 		return ret;
906 
907 	timeout = jiffies +
908 		  msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
909 	do {
910 		ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
911 					    &status);
912 		if (ret)
913 			return ret;
914 	} while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
915 		 time_before(jiffies, timeout));
916 
917 	if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
918 		dev_err(dev, "Failed to acquire the i2c bus\n");
919 		return -ETIMEDOUT;
920 	}
921 
922 	return sii902x_write_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
923 				      status);
924 }
925 
926 /*
927  * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through
928  * mode of the HDMI transmitter. Do not use regmap from within this function,
929  * only use sii902x_*_unlocked functions to read/modify/write registers.
930  * We are holding the parent adapter lock here, keep this in mind before
931  * adding more i2c transactions.
932  *
933  * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
934  * in this driver, we need to make sure that we only touch 0x1A[2:1] from
935  * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
936  * we leave the remaining bits as we have found them.
937  */
938 static int sii902x_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id)
939 {
940 	struct sii902x *sii902x = i2c_mux_priv(mux);
941 	struct device *dev = &sii902x->i2c->dev;
942 	unsigned long timeout;
943 	unsigned int retries;
944 	u8 status;
945 	int ret;
946 
947 	/*
948 	 * When the HDMI transmitter is in pass through mode, we need an
949 	 * (undocumented) additional delay between STOP and START conditions
950 	 * to guarantee the bus won't get stuck.
951 	 */
952 	udelay(30);
953 
954 	/*
955 	 * Sometimes the I2C bus can stall after failure to use the
956 	 * EDID channel. Retry a few times to see if things clear
957 	 * up, else continue anyway.
958 	 */
959 	retries = 5;
960 	do {
961 		ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
962 					    &status);
963 		retries--;
964 	} while (ret && retries);
965 	if (ret) {
966 		dev_err(dev, "failed to read status (%d)\n", ret);
967 		return ret;
968 	}
969 
970 	ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
971 					   SII902X_SYS_CTRL_DDC_BUS_REQ |
972 					   SII902X_SYS_CTRL_DDC_BUS_GRTD, 0);
973 	if (ret)
974 		return ret;
975 
976 	timeout = jiffies +
977 		  msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
978 	do {
979 		ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA,
980 					    &status);
981 		if (ret)
982 			return ret;
983 	} while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
984 			   SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
985 		 time_before(jiffies, timeout));
986 
987 	if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
988 		      SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
989 		dev_err(dev, "failed to release the i2c bus\n");
990 		return -ETIMEDOUT;
991 	}
992 
993 	return 0;
994 }
995 
996 static const struct drm_bridge_timings default_sii902x_timings = {
997 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
998 		 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
999 		 | DRM_BUS_FLAG_DE_HIGH,
1000 };
1001 
1002 static int sii902x_init(struct sii902x *sii902x)
1003 {
1004 	struct device *dev = &sii902x->i2c->dev;
1005 	unsigned int status = 0;
1006 	u8 chipid[4];
1007 	int ret;
1008 
1009 	sii902x_reset(sii902x);
1010 
1011 	ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0);
1012 	if (ret)
1013 		return ret;
1014 
1015 	ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0),
1016 			       &chipid, 4);
1017 	if (ret) {
1018 		dev_err(dev, "regmap_read failed %d\n", ret);
1019 		return ret;
1020 	}
1021 
1022 	if (chipid[0] != 0xb0) {
1023 		dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n",
1024 			chipid[0]);
1025 		return -EINVAL;
1026 	}
1027 
1028 	/* Clear all pending interrupts */
1029 	regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status);
1030 	regmap_write(sii902x->regmap, SII902X_INT_STATUS, status);
1031 
1032 	if (sii902x->i2c->irq > 0) {
1033 		regmap_write(sii902x->regmap, SII902X_INT_ENABLE,
1034 			     SII902X_HOTPLUG_EVENT);
1035 
1036 		ret = devm_request_threaded_irq(dev, sii902x->i2c->irq, NULL,
1037 						sii902x_interrupt,
1038 						IRQF_ONESHOT, dev_name(dev),
1039 						sii902x);
1040 		if (ret)
1041 			return ret;
1042 	}
1043 
1044 	sii902x->bridge.funcs = &sii902x_bridge_funcs;
1045 	sii902x->bridge.of_node = dev->of_node;
1046 	sii902x->bridge.timings = &default_sii902x_timings;
1047 	sii902x->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
1048 
1049 	if (sii902x->i2c->irq > 0)
1050 		sii902x->bridge.ops |= DRM_BRIDGE_OP_HPD;
1051 
1052 	drm_bridge_add(&sii902x->bridge);
1053 
1054 	sii902x_audio_codec_init(sii902x, dev);
1055 
1056 	i2c_set_clientdata(sii902x->i2c, sii902x);
1057 
1058 	sii902x->i2cmux = i2c_mux_alloc(sii902x->i2c->adapter, dev,
1059 					1, 0, I2C_MUX_GATE,
1060 					sii902x_i2c_bypass_select,
1061 					sii902x_i2c_bypass_deselect);
1062 	if (!sii902x->i2cmux)
1063 		return -ENOMEM;
1064 
1065 	sii902x->i2cmux->priv = sii902x;
1066 	return i2c_mux_add_adapter(sii902x->i2cmux, 0, 0, 0);
1067 }
1068 
1069 static int sii902x_probe(struct i2c_client *client,
1070 			 const struct i2c_device_id *id)
1071 {
1072 	struct device *dev = &client->dev;
1073 	struct device_node *endpoint;
1074 	struct sii902x *sii902x;
1075 	int ret;
1076 
1077 	ret = i2c_check_functionality(client->adapter,
1078 				      I2C_FUNC_SMBUS_BYTE_DATA);
1079 	if (!ret) {
1080 		dev_err(dev, "I2C adapter not suitable\n");
1081 		return -EIO;
1082 	}
1083 
1084 	sii902x = devm_kzalloc(dev, sizeof(*sii902x), GFP_KERNEL);
1085 	if (!sii902x)
1086 		return -ENOMEM;
1087 
1088 	sii902x->i2c = client;
1089 	sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config);
1090 	if (IS_ERR(sii902x->regmap))
1091 		return PTR_ERR(sii902x->regmap);
1092 
1093 	sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1094 						      GPIOD_OUT_LOW);
1095 	if (IS_ERR(sii902x->reset_gpio)) {
1096 		dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n",
1097 			PTR_ERR(sii902x->reset_gpio));
1098 		return PTR_ERR(sii902x->reset_gpio);
1099 	}
1100 
1101 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 1, -1);
1102 	if (endpoint) {
1103 		struct device_node *remote = of_graph_get_remote_port_parent(endpoint);
1104 
1105 		of_node_put(endpoint);
1106 		if (!remote) {
1107 			dev_err(dev, "Endpoint in port@1 unconnected\n");
1108 			return -ENODEV;
1109 		}
1110 
1111 		if (!of_device_is_available(remote)) {
1112 			dev_err(dev, "port@1 remote device is disabled\n");
1113 			of_node_put(remote);
1114 			return -ENODEV;
1115 		}
1116 
1117 		sii902x->next_bridge = of_drm_find_bridge(remote);
1118 		of_node_put(remote);
1119 		if (!sii902x->next_bridge)
1120 			return -EPROBE_DEFER;
1121 	}
1122 
1123 	mutex_init(&sii902x->mutex);
1124 
1125 	sii902x->supplies[0].supply = "iovcc";
1126 	sii902x->supplies[1].supply = "cvcc12";
1127 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sii902x->supplies),
1128 				      sii902x->supplies);
1129 	if (ret < 0)
1130 		return ret;
1131 
1132 	ret = regulator_bulk_enable(ARRAY_SIZE(sii902x->supplies),
1133 				    sii902x->supplies);
1134 	if (ret < 0) {
1135 		dev_err_probe(dev, ret, "Failed to enable supplies");
1136 		return ret;
1137 	}
1138 
1139 	ret = sii902x_init(sii902x);
1140 	if (ret < 0) {
1141 		regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
1142 				       sii902x->supplies);
1143 	}
1144 
1145 	return ret;
1146 }
1147 
1148 static void sii902x_remove(struct i2c_client *client)
1149 
1150 {
1151 	struct sii902x *sii902x = i2c_get_clientdata(client);
1152 
1153 	i2c_mux_del_adapters(sii902x->i2cmux);
1154 	drm_bridge_remove(&sii902x->bridge);
1155 	regulator_bulk_disable(ARRAY_SIZE(sii902x->supplies),
1156 			       sii902x->supplies);
1157 }
1158 
1159 static const struct of_device_id sii902x_dt_ids[] = {
1160 	{ .compatible = "sil,sii9022", },
1161 	{ }
1162 };
1163 MODULE_DEVICE_TABLE(of, sii902x_dt_ids);
1164 
1165 static const struct i2c_device_id sii902x_i2c_ids[] = {
1166 	{ "sii9022", 0 },
1167 	{ },
1168 };
1169 MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids);
1170 
1171 static struct i2c_driver sii902x_driver = {
1172 	.probe = sii902x_probe,
1173 	.remove = sii902x_remove,
1174 	.driver = {
1175 		.name = "sii902x",
1176 		.of_match_table = sii902x_dt_ids,
1177 	},
1178 	.id_table = sii902x_i2c_ids,
1179 };
1180 module_i2c_driver(sii902x_driver);
1181 
1182 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
1183 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
1184 MODULE_LICENSE("GPL");
1185