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