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
166struct 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 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
187static 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(adapter: i2c->adapter, addr: i2c->addr, flags: i2c->flags,
193 I2C_SMBUS_READ, command: reg, I2C_SMBUS_BYTE_DATA, data: &data);
194
195 if (ret < 0)
196 return ret;
197
198 *val = data.byte;
199 return 0;
200}
201
202static 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(adapter: i2c->adapter, addr: i2c->addr, flags: i2c->flags,
209 I2C_SMBUS_WRITE, command: reg, I2C_SMBUS_BYTE_DATA,
210 data: &data);
211}
212
213static 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, val: &status);
220 if (ret)
221 return ret;
222 status &= ~mask;
223 status |= val & mask;
224 return sii902x_write_unlocked(i2c, reg, val: status);
225}
226
227static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge)
228{
229 return container_of(bridge, struct sii902x, bridge);
230}
231
232static inline struct sii902x *connector_to_sii902x(struct drm_connector *con)
233{
234 return container_of(con, struct sii902x, connector);
235}
236
237static void sii902x_reset(struct sii902x *sii902x)
238{
239 if (!sii902x->reset_gpio)
240 return;
241
242 gpiod_set_value_cansleep(desc: sii902x->reset_gpio, value: 1);
243
244 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
245 usleep_range(min: 150, max: 200);
246
247 gpiod_set_value_cansleep(desc: sii902x->reset_gpio, value: 0);
248}
249
250static enum drm_connector_status sii902x_detect(struct sii902x *sii902x)
251{
252 unsigned int status;
253
254 mutex_lock(&sii902x->mutex);
255
256 regmap_read(map: sii902x->regmap, SII902X_INT_STATUS, val: &status);
257
258 mutex_unlock(lock: &sii902x->mutex);
259
260 return (status & SII902X_PLUGGED_STATUS) ?
261 connector_status_connected : connector_status_disconnected;
262}
263
264static enum drm_connector_status
265sii902x_connector_detect(struct drm_connector *connector, bool force)
266{
267 struct sii902x *sii902x = connector_to_sii902x(con: connector);
268
269 return sii902x_detect(sii902x);
270}
271
272static 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
281static const struct drm_edid *sii902x_edid_read(struct sii902x *sii902x,
282 struct drm_connector *connector)
283{
284 const struct drm_edid *drm_edid;
285
286 mutex_lock(&sii902x->mutex);
287
288 drm_edid = drm_edid_read_ddc(connector, adapter: sii902x->i2cmux->adapter[0]);
289
290 mutex_unlock(lock: &sii902x->mutex);
291
292 return drm_edid;
293}
294
295static int sii902x_get_modes(struct drm_connector *connector)
296{
297 struct sii902x *sii902x = connector_to_sii902x(con: connector);
298 const struct drm_edid *drm_edid;
299 int num = 0;
300
301 drm_edid = sii902x_edid_read(sii902x, connector);
302 drm_edid_connector_update(connector, edid: drm_edid);
303 if (drm_edid) {
304 num = drm_edid_connector_add_modes(connector);
305 drm_edid_free(drm_edid);
306 }
307
308 sii902x->sink_is_hdmi = connector->display_info.is_hdmi;
309
310 return num;
311}
312
313static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector,
314 struct drm_display_mode *mode)
315{
316 /* TODO: check mode */
317
318 return MODE_OK;
319}
320
321static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = {
322 .get_modes = sii902x_get_modes,
323 .mode_valid = sii902x_mode_valid,
324};
325
326static void sii902x_bridge_disable(struct drm_bridge *bridge)
327{
328 struct sii902x *sii902x = bridge_to_sii902x(bridge);
329
330 mutex_lock(&sii902x->mutex);
331
332 regmap_update_bits(map: sii902x->regmap, SII902X_SYS_CTRL_DATA,
333 SII902X_SYS_CTRL_PWR_DWN,
334 SII902X_SYS_CTRL_PWR_DWN);
335
336 mutex_unlock(lock: &sii902x->mutex);
337}
338
339static void sii902x_bridge_enable(struct drm_bridge *bridge)
340{
341 struct sii902x *sii902x = bridge_to_sii902x(bridge);
342
343 mutex_lock(&sii902x->mutex);
344
345 regmap_update_bits(map: sii902x->regmap, SII902X_PWR_STATE_CTRL,
346 SII902X_AVI_POWER_STATE_MSK,
347 SII902X_AVI_POWER_STATE_D(0));
348 regmap_update_bits(map: sii902x->regmap, SII902X_SYS_CTRL_DATA,
349 SII902X_SYS_CTRL_PWR_DWN, val: 0);
350
351 mutex_unlock(lock: &sii902x->mutex);
352}
353
354static void sii902x_bridge_mode_set(struct drm_bridge *bridge,
355 const struct drm_display_mode *mode,
356 const struct drm_display_mode *adj)
357{
358 struct sii902x *sii902x = bridge_to_sii902x(bridge);
359 u8 output_mode = SII902X_SYS_CTRL_OUTPUT_DVI;
360 struct regmap *regmap = sii902x->regmap;
361 u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
362 struct hdmi_avi_infoframe frame;
363 u16 pixel_clock_10kHz = adj->clock / 10;
364 int ret;
365
366 if (sii902x->sink_is_hdmi)
367 output_mode = SII902X_SYS_CTRL_OUTPUT_HDMI;
368
369 buf[0] = pixel_clock_10kHz & 0xff;
370 buf[1] = pixel_clock_10kHz >> 8;
371 buf[2] = drm_mode_vrefresh(mode: adj);
372 buf[3] = 0x00;
373 buf[4] = adj->hdisplay;
374 buf[5] = adj->hdisplay >> 8;
375 buf[6] = adj->vdisplay;
376 buf[7] = adj->vdisplay >> 8;
377 buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE |
378 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT;
379 buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO |
380 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB;
381
382 mutex_lock(&sii902x->mutex);
383
384 ret = regmap_update_bits(map: sii902x->regmap, SII902X_SYS_CTRL_DATA,
385 SII902X_SYS_CTRL_OUTPUT_MODE, val: output_mode);
386 if (ret)
387 goto out;
388
389 ret = regmap_bulk_write(map: regmap, SII902X_TPI_VIDEO_DATA, val: buf, val_count: 10);
390 if (ret)
391 goto out;
392
393 ret = drm_hdmi_avi_infoframe_from_display_mode(frame: &frame,
394 connector: &sii902x->connector, mode: adj);
395 if (ret < 0) {
396 DRM_ERROR("couldn't fill AVI infoframe\n");
397 goto out;
398 }
399
400 ret = hdmi_avi_infoframe_pack(frame: &frame, buffer: buf, size: sizeof(buf));
401 if (ret < 0) {
402 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
403 goto out;
404 }
405
406 /* Do not send the infoframe header, but keep the CRC field. */
407 regmap_bulk_write(map: regmap, SII902X_TPI_AVI_INFOFRAME,
408 val: buf + HDMI_INFOFRAME_HEADER_SIZE - 1,
409 HDMI_AVI_INFOFRAME_SIZE + 1);
410
411out:
412 mutex_unlock(lock: &sii902x->mutex);
413}
414
415static int sii902x_bridge_attach(struct drm_bridge *bridge,
416 enum drm_bridge_attach_flags flags)
417{
418 struct sii902x *sii902x = bridge_to_sii902x(bridge);
419 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
420 struct drm_device *drm = bridge->dev;
421 int ret;
422
423 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
424 return drm_bridge_attach(encoder: bridge->encoder, bridge: sii902x->next_bridge,
425 previous: bridge, flags);
426
427 drm_connector_helper_add(connector: &sii902x->connector,
428 funcs: &sii902x_connector_helper_funcs);
429
430 if (!drm_core_check_feature(dev: drm, feature: DRIVER_ATOMIC)) {
431 dev_err(&sii902x->i2c->dev,
432 "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
433 return -ENOTSUPP;
434 }
435
436 ret = drm_connector_init(dev: drm, connector: &sii902x->connector,
437 funcs: &sii902x_connector_funcs,
438 DRM_MODE_CONNECTOR_HDMIA);
439 if (ret)
440 return ret;
441
442 if (sii902x->i2c->irq > 0)
443 sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD;
444 else
445 sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
446
447 ret = drm_display_info_set_bus_formats(info: &sii902x->connector.display_info,
448 formats: &bus_format, num_formats: 1);
449 if (ret)
450 return ret;
451
452 drm_connector_attach_encoder(connector: &sii902x->connector, encoder: bridge->encoder);
453
454 return 0;
455}
456
457static enum drm_connector_status sii902x_bridge_detect(struct drm_bridge *bridge)
458{
459 struct sii902x *sii902x = bridge_to_sii902x(bridge);
460
461 return sii902x_detect(sii902x);
462}
463
464static const struct drm_edid *sii902x_bridge_edid_read(struct drm_bridge *bridge,
465 struct drm_connector *connector)
466{
467 struct sii902x *sii902x = bridge_to_sii902x(bridge);
468
469 return sii902x_edid_read(sii902x, connector);
470}
471
472static u32 *sii902x_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
473 struct drm_bridge_state *bridge_state,
474 struct drm_crtc_state *crtc_state,
475 struct drm_connector_state *conn_state,
476 u32 output_fmt,
477 unsigned int *num_input_fmts)
478{
479 u32 *input_fmts;
480
481 *num_input_fmts = 0;
482
483 input_fmts = kcalloc(n: 1, size: sizeof(*input_fmts), GFP_KERNEL);
484 if (!input_fmts)
485 return NULL;
486
487 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
488 *num_input_fmts = 1;
489
490 return input_fmts;
491}
492
493static int sii902x_bridge_atomic_check(struct drm_bridge *bridge,
494 struct drm_bridge_state *bridge_state,
495 struct drm_crtc_state *crtc_state,
496 struct drm_connector_state *conn_state)
497{
498 /*
499 * There might be flags negotiation supported in future but
500 * set the bus flags in atomic_check statically for now.
501 */
502 bridge_state->input_bus_cfg.flags = bridge->timings->input_bus_flags;
503
504 return 0;
505}
506
507static const struct drm_bridge_funcs sii902x_bridge_funcs = {
508 .attach = sii902x_bridge_attach,
509 .mode_set = sii902x_bridge_mode_set,
510 .disable = sii902x_bridge_disable,
511 .enable = sii902x_bridge_enable,
512 .detect = sii902x_bridge_detect,
513 .edid_read = sii902x_bridge_edid_read,
514 .atomic_reset = drm_atomic_helper_bridge_reset,
515 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
516 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
517 .atomic_get_input_bus_fmts = sii902x_bridge_atomic_get_input_bus_fmts,
518 .atomic_check = sii902x_bridge_atomic_check,
519};
520
521static int sii902x_mute(struct sii902x *sii902x, bool mute)
522{
523 struct device *dev = &sii902x->i2c->dev;
524 unsigned int val = mute ? SII902X_TPI_AUDIO_MUTE_ENABLE :
525 SII902X_TPI_AUDIO_MUTE_DISABLE;
526
527 dev_dbg(dev, "%s: %s\n", __func__, mute ? "Muted" : "Unmuted");
528
529 return regmap_update_bits(map: sii902x->regmap,
530 SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
531 SII902X_TPI_AUDIO_MUTE_ENABLE, val);
532}
533
534static const int sii902x_mclk_div_table[] = {
535 128, 256, 384, 512, 768, 1024, 1152, 192 };
536
537static int sii902x_select_mclk_div(u8 *i2s_config_reg, unsigned int rate,
538 unsigned int mclk)
539{
540 int div = mclk / rate;
541 int distance = 100000;
542 u8 i, nearest = 0;
543
544 for (i = 0; i < ARRAY_SIZE(sii902x_mclk_div_table); i++) {
545 unsigned int d = abs(div - sii902x_mclk_div_table[i]);
546
547 if (d >= distance)
548 continue;
549
550 nearest = i;
551 distance = d;
552 if (d == 0)
553 break;
554 }
555
556 *i2s_config_reg |= nearest << 4;
557
558 return sii902x_mclk_div_table[nearest];
559}
560
561static const struct sii902x_sample_freq {
562 u32 freq;
563 u8 val;
564} sii902x_sample_freq[] = {
565 { .freq = 32000, .val = SII902X_TPI_AUDIO_FREQ_32KHZ },
566 { .freq = 44000, .val = SII902X_TPI_AUDIO_FREQ_44KHZ },
567 { .freq = 48000, .val = SII902X_TPI_AUDIO_FREQ_48KHZ },
568 { .freq = 88000, .val = SII902X_TPI_AUDIO_FREQ_88KHZ },
569 { .freq = 96000, .val = SII902X_TPI_AUDIO_FREQ_96KHZ },
570 { .freq = 176000, .val = SII902X_TPI_AUDIO_FREQ_176KHZ },
571 { .freq = 192000, .val = SII902X_TPI_AUDIO_FREQ_192KHZ },
572};
573
574static int sii902x_audio_hw_params(struct device *dev, void *data,
575 struct hdmi_codec_daifmt *daifmt,
576 struct hdmi_codec_params *params)
577{
578 struct sii902x *sii902x = dev_get_drvdata(dev);
579 u8 i2s_config_reg = SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST;
580 u8 config_byte2_reg = (SII902X_TPI_AUDIO_INTERFACE_I2S |
581 SII902X_TPI_AUDIO_MUTE_ENABLE |
582 SII902X_TPI_AUDIO_CODING_PCM);
583 u8 config_byte3_reg = 0;
584 u8 infoframe_buf[HDMI_INFOFRAME_SIZE(AUDIO)];
585 unsigned long mclk_rate;
586 int i, ret;
587
588 if (daifmt->bit_clk_provider || daifmt->frame_clk_provider) {
589 dev_dbg(dev, "%s: I2S clock provider mode not supported\n",
590 __func__);
591 return -EINVAL;
592 }
593
594 switch (daifmt->fmt) {
595 case HDMI_I2S:
596 i2s_config_reg |= SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES |
597 SII902X_TPI_I2S_SD_JUSTIFY_LEFT;
598 break;
599 case HDMI_RIGHT_J:
600 i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_RIGHT;
601 break;
602 case HDMI_LEFT_J:
603 i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_LEFT;
604 break;
605 default:
606 dev_dbg(dev, "%s: Unsupported i2s format %u\n", __func__,
607 daifmt->fmt);
608 return -EINVAL;
609 }
610
611 if (daifmt->bit_clk_inv)
612 i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_FALLING;
613 else
614 i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_RISING;
615
616 if (daifmt->frame_clk_inv)
617 i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_LOW;
618 else
619 i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_HIGH;
620
621 if (params->channels > 2)
622 config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS;
623 else
624 config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS;
625
626 switch (params->sample_width) {
627 case 16:
628 config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_16;
629 break;
630 case 20:
631 config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_20;
632 break;
633 case 24:
634 case 32:
635 config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_24;
636 break;
637 default:
638 dev_err(dev, "%s: Unsupported sample width %u\n", __func__,
639 params->sample_width);
640 return -EINVAL;
641 }
642
643 for (i = 0; i < ARRAY_SIZE(sii902x_sample_freq); i++) {
644 if (params->sample_rate == sii902x_sample_freq[i].freq) {
645 config_byte3_reg |= sii902x_sample_freq[i].val;
646 break;
647 }
648 }
649
650 ret = clk_prepare_enable(clk: sii902x->audio.mclk);
651 if (ret) {
652 dev_err(dev, "Enabling mclk failed: %d\n", ret);
653 return ret;
654 }
655
656 if (sii902x->audio.mclk) {
657 mclk_rate = clk_get_rate(clk: sii902x->audio.mclk);
658 ret = sii902x_select_mclk_div(i2s_config_reg: &i2s_config_reg,
659 rate: params->sample_rate, mclk: mclk_rate);
660 if (mclk_rate != ret * params->sample_rate)
661 dev_dbg(dev, "Inaccurate reference clock (%ld/%d != %u)\n",
662 mclk_rate, ret, params->sample_rate);
663 }
664
665 mutex_lock(&sii902x->mutex);
666
667 ret = regmap_write(map: sii902x->regmap,
668 SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
669 val: config_byte2_reg);
670 if (ret < 0)
671 goto out;
672
673 ret = regmap_write(map: sii902x->regmap, SII902X_TPI_I2S_INPUT_CONFIG_REG,
674 val: i2s_config_reg);
675 if (ret)
676 goto out;
677
678 for (i = 0; i < ARRAY_SIZE(sii902x->audio.i2s_fifo_sequence) &&
679 sii902x->audio.i2s_fifo_sequence[i]; i++)
680 regmap_write(map: sii902x->regmap,
681 SII902X_TPI_I2S_ENABLE_MAPPING_REG,
682 val: sii902x->audio.i2s_fifo_sequence[i]);
683
684 ret = regmap_write(map: sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE3_REG,
685 val: config_byte3_reg);
686 if (ret)
687 goto out;
688
689 ret = regmap_bulk_write(map: sii902x->regmap, SII902X_TPI_I2S_STRM_HDR_BASE,
690 val: params->iec.status,
691 min((size_t) SII902X_TPI_I2S_STRM_HDR_SIZE,
692 sizeof(params->iec.status)));
693 if (ret)
694 goto out;
695
696 ret = hdmi_audio_infoframe_pack(frame: &params->cea, buffer: infoframe_buf,
697 size: sizeof(infoframe_buf));
698 if (ret < 0) {
699 dev_err(dev, "%s: Failed to pack audio infoframe: %d\n",
700 __func__, ret);
701 goto out;
702 }
703
704 ret = regmap_bulk_write(map: sii902x->regmap,
705 SII902X_TPI_MISC_INFOFRAME_BASE,
706 val: infoframe_buf,
707 min(ret, SII902X_TPI_MISC_INFOFRAME_SIZE));
708 if (ret)
709 goto out;
710
711 /* Decode Level 0 Packets */
712 ret = regmap_write(map: sii902x->regmap, SII902X_IND_SET_PAGE, val: 0x02);
713 if (ret)
714 goto out;
715
716 ret = regmap_write(map: sii902x->regmap, SII902X_IND_OFFSET, val: 0x24);
717 if (ret)
718 goto out;
719
720 ret = regmap_write(map: sii902x->regmap, SII902X_IND_VALUE, val: 0x02);
721 if (ret)
722 goto out;
723
724 dev_dbg(dev, "%s: hdmi audio enabled\n", __func__);
725out:
726 mutex_unlock(lock: &sii902x->mutex);
727
728 if (ret) {
729 clk_disable_unprepare(clk: sii902x->audio.mclk);
730 dev_err(dev, "%s: hdmi audio enable failed: %d\n", __func__,
731 ret);
732 }
733
734 return ret;
735}
736
737static void sii902x_audio_shutdown(struct device *dev, void *data)
738{
739 struct sii902x *sii902x = dev_get_drvdata(dev);
740
741 mutex_lock(&sii902x->mutex);
742
743 regmap_write(map: sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE2_REG,
744 SII902X_TPI_AUDIO_INTERFACE_DISABLE);
745
746 mutex_unlock(lock: &sii902x->mutex);
747
748 clk_disable_unprepare(clk: sii902x->audio.mclk);
749}
750
751static int sii902x_audio_mute(struct device *dev, void *data,
752 bool enable, int direction)
753{
754 struct sii902x *sii902x = dev_get_drvdata(dev);
755
756 mutex_lock(&sii902x->mutex);
757
758 sii902x_mute(sii902x, mute: enable);
759
760 mutex_unlock(lock: &sii902x->mutex);
761
762 return 0;
763}
764
765static int sii902x_audio_get_eld(struct device *dev, void *data,
766 uint8_t *buf, size_t len)
767{
768 struct sii902x *sii902x = dev_get_drvdata(dev);
769
770 mutex_lock(&sii902x->mutex);
771
772 memcpy(buf, sii902x->connector.eld,
773 min(sizeof(sii902x->connector.eld), len));
774
775 mutex_unlock(lock: &sii902x->mutex);
776
777 return 0;
778}
779
780static int sii902x_audio_get_dai_id(struct snd_soc_component *component,
781 struct device_node *endpoint)
782{
783 struct of_endpoint of_ep;
784 int ret;
785
786 ret = of_graph_parse_endpoint(node: endpoint, endpoint: &of_ep);
787 if (ret < 0)
788 return ret;
789
790 /*
791 * HDMI sound should be located at reg = <3>
792 * Return expected DAI index 0.
793 */
794 if (of_ep.port == SII902X_AUDIO_PORT_INDEX)
795 return 0;
796
797 return -EINVAL;
798}
799
800static const struct hdmi_codec_ops sii902x_audio_codec_ops = {
801 .hw_params = sii902x_audio_hw_params,
802 .audio_shutdown = sii902x_audio_shutdown,
803 .mute_stream = sii902x_audio_mute,
804 .get_eld = sii902x_audio_get_eld,
805 .get_dai_id = sii902x_audio_get_dai_id,
806 .no_capture_mute = 1,
807};
808
809static int sii902x_audio_codec_init(struct sii902x *sii902x,
810 struct device *dev)
811{
812 static const u8 audio_fifo_id[] = {
813 SII902X_TPI_I2S_CONFIG_FIFO0,
814 SII902X_TPI_I2S_CONFIG_FIFO1,
815 SII902X_TPI_I2S_CONFIG_FIFO2,
816 SII902X_TPI_I2S_CONFIG_FIFO3,
817 };
818 static const u8 i2s_lane_id[] = {
819 SII902X_TPI_I2S_SELECT_SD0,
820 SII902X_TPI_I2S_SELECT_SD1,
821 SII902X_TPI_I2S_SELECT_SD2,
822 SII902X_TPI_I2S_SELECT_SD3,
823 };
824 struct hdmi_codec_pdata codec_data = {
825 .ops = &sii902x_audio_codec_ops,
826 .i2s = 1, /* Only i2s support for now. */
827 .spdif = 0,
828 .max_i2s_channels = 0,
829 };
830 u8 lanes[4];
831 int num_lanes, i;
832
833 if (!of_property_read_bool(np: dev->of_node, propname: "#sound-dai-cells")) {
834 dev_dbg(dev, "%s: No \"#sound-dai-cells\", no audio\n",
835 __func__);
836 return 0;
837 }
838
839 num_lanes = of_property_read_variable_u8_array(np: dev->of_node,
840 propname: "sil,i2s-data-lanes",
841 out_values: lanes, sz_min: 1,
842 ARRAY_SIZE(lanes));
843
844 if (num_lanes == -EINVAL) {
845 dev_dbg(dev,
846 "%s: No \"sil,i2s-data-lanes\", use default <0>\n",
847 __func__);
848 num_lanes = 1;
849 lanes[0] = 0;
850 } else if (num_lanes < 0) {
851 dev_err(dev,
852 "%s: Error gettin \"sil,i2s-data-lanes\": %d\n",
853 __func__, num_lanes);
854 return num_lanes;
855 }
856 codec_data.max_i2s_channels = 2 * num_lanes;
857
858 for (i = 0; i < num_lanes; i++)
859 sii902x->audio.i2s_fifo_sequence[i] |= audio_fifo_id[i] |
860 i2s_lane_id[lanes[i]] | SII902X_TPI_I2S_FIFO_ENABLE;
861
862 sii902x->audio.mclk = devm_clk_get_optional(dev, id: "mclk");
863 if (IS_ERR(ptr: sii902x->audio.mclk)) {
864 dev_err(dev, "%s: No clock (audio mclk) found: %ld\n",
865 __func__, PTR_ERR(sii902x->audio.mclk));
866 return PTR_ERR(ptr: sii902x->audio.mclk);
867 }
868
869 sii902x->audio.pdev = platform_device_register_data(
870 parent: dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
871 data: &codec_data, size: sizeof(codec_data));
872
873 return PTR_ERR_OR_ZERO(ptr: sii902x->audio.pdev);
874}
875
876static const struct regmap_range sii902x_volatile_ranges[] = {
877 { .range_min = 0, .range_max = 0xff },
878};
879
880static const struct regmap_access_table sii902x_volatile_table = {
881 .yes_ranges = sii902x_volatile_ranges,
882 .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges),
883};
884
885static const struct regmap_config sii902x_regmap_config = {
886 .reg_bits = 8,
887 .val_bits = 8,
888 .disable_locking = true, /* struct sii902x mutex should be enough */
889 .max_register = SII902X_TPI_MISC_INFOFRAME_END,
890 .volatile_table = &sii902x_volatile_table,
891 .cache_type = REGCACHE_NONE,
892};
893
894static irqreturn_t sii902x_interrupt(int irq, void *data)
895{
896 struct sii902x *sii902x = data;
897 unsigned int status = 0;
898
899 mutex_lock(&sii902x->mutex);
900
901 regmap_read(map: sii902x->regmap, SII902X_INT_STATUS, val: &status);
902 regmap_write(map: sii902x->regmap, SII902X_INT_STATUS, val: status);
903
904 mutex_unlock(lock: &sii902x->mutex);
905
906 if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev) {
907 drm_helper_hpd_irq_event(dev: sii902x->bridge.dev);
908 drm_bridge_hpd_notify(bridge: &sii902x->bridge, status: (status & SII902X_PLUGGED_STATUS)
909 ? connector_status_connected
910 : connector_status_disconnected);
911 }
912
913 return IRQ_HANDLED;
914}
915
916/*
917 * The purpose of sii902x_i2c_bypass_select is to enable the pass through
918 * mode of the HDMI transmitter. Do not use regmap from within this function,
919 * only use sii902x_*_unlocked functions to read/modify/write registers.
920 * We are holding the parent adapter lock here, keep this in mind before
921 * adding more i2c transactions.
922 *
923 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
924 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
925 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
926 * we leave the remaining bits as we have found them.
927 */
928static int sii902x_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id)
929{
930 struct sii902x *sii902x = i2c_mux_priv(muxc: mux);
931 struct device *dev = &sii902x->i2c->dev;
932 unsigned long timeout;
933 u8 status;
934 int ret;
935
936 ret = sii902x_update_bits_unlocked(i2c: sii902x->i2c, SII902X_SYS_CTRL_DATA,
937 SII902X_SYS_CTRL_DDC_BUS_REQ,
938 SII902X_SYS_CTRL_DDC_BUS_REQ);
939 if (ret)
940 return ret;
941
942 timeout = jiffies +
943 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
944 do {
945 ret = sii902x_read_unlocked(i2c: sii902x->i2c, SII902X_SYS_CTRL_DATA,
946 val: &status);
947 if (ret)
948 return ret;
949 } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
950 time_before(jiffies, timeout));
951
952 if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
953 dev_err(dev, "Failed to acquire the i2c bus\n");
954 return -ETIMEDOUT;
955 }
956
957 return sii902x_write_unlocked(i2c: sii902x->i2c, SII902X_SYS_CTRL_DATA,
958 val: status);
959}
960
961/*
962 * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through
963 * mode of the HDMI transmitter. Do not use regmap from within this function,
964 * only use sii902x_*_unlocked functions to read/modify/write registers.
965 * We are holding the parent adapter lock here, keep this in mind before
966 * adding more i2c transactions.
967 *
968 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
969 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
970 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
971 * we leave the remaining bits as we have found them.
972 */
973static int sii902x_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id)
974{
975 struct sii902x *sii902x = i2c_mux_priv(muxc: mux);
976 struct device *dev = &sii902x->i2c->dev;
977 unsigned long timeout;
978 unsigned int retries;
979 u8 status;
980 int ret;
981
982 /*
983 * When the HDMI transmitter is in pass through mode, we need an
984 * (undocumented) additional delay between STOP and START conditions
985 * to guarantee the bus won't get stuck.
986 */
987 udelay(30);
988
989 /*
990 * Sometimes the I2C bus can stall after failure to use the
991 * EDID channel. Retry a few times to see if things clear
992 * up, else continue anyway.
993 */
994 retries = 5;
995 do {
996 ret = sii902x_read_unlocked(i2c: sii902x->i2c, SII902X_SYS_CTRL_DATA,
997 val: &status);
998 retries--;
999 } while (ret && retries);
1000 if (ret) {
1001 dev_err(dev, "failed to read status (%d)\n", ret);
1002 return ret;
1003 }
1004
1005 ret = sii902x_update_bits_unlocked(i2c: sii902x->i2c, SII902X_SYS_CTRL_DATA,
1006 SII902X_SYS_CTRL_DDC_BUS_REQ |
1007 SII902X_SYS_CTRL_DDC_BUS_GRTD, val: 0);
1008 if (ret)
1009 return ret;
1010
1011 timeout = jiffies +
1012 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS);
1013 do {
1014 ret = sii902x_read_unlocked(i2c: sii902x->i2c, SII902X_SYS_CTRL_DATA,
1015 val: &status);
1016 if (ret)
1017 return ret;
1018 } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
1019 SII902X_SYS_CTRL_DDC_BUS_GRTD) &&
1020 time_before(jiffies, timeout));
1021
1022 if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ |
1023 SII902X_SYS_CTRL_DDC_BUS_GRTD)) {
1024 dev_err(dev, "failed to release the i2c bus\n");
1025 return -ETIMEDOUT;
1026 }
1027
1028 return 0;
1029}
1030
1031static const struct drm_bridge_timings default_sii902x_timings = {
1032 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
1033 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
1034 | DRM_BUS_FLAG_DE_HIGH,
1035};
1036
1037static int sii902x_init(struct sii902x *sii902x)
1038{
1039 struct device *dev = &sii902x->i2c->dev;
1040 unsigned int status = 0;
1041 u8 chipid[4];
1042 int ret;
1043
1044 sii902x_reset(sii902x);
1045
1046 ret = regmap_write(map: sii902x->regmap, SII902X_REG_TPI_RQB, val: 0x0);
1047 if (ret)
1048 return ret;
1049
1050 ret = regmap_bulk_read(map: sii902x->regmap, SII902X_REG_CHIPID(0),
1051 val: &chipid, val_count: 4);
1052 if (ret) {
1053 dev_err(dev, "regmap_read failed %d\n", ret);
1054 return ret;
1055 }
1056
1057 if (chipid[0] != 0xb0) {
1058 dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n",
1059 chipid[0]);
1060 return -EINVAL;
1061 }
1062
1063 /* Clear all pending interrupts */
1064 regmap_read(map: sii902x->regmap, SII902X_INT_STATUS, val: &status);
1065 regmap_write(map: sii902x->regmap, SII902X_INT_STATUS, val: status);
1066
1067 if (sii902x->i2c->irq > 0) {
1068 regmap_write(map: sii902x->regmap, SII902X_INT_ENABLE,
1069 SII902X_HOTPLUG_EVENT);
1070
1071 ret = devm_request_threaded_irq(dev, irq: sii902x->i2c->irq, NULL,
1072 thread_fn: sii902x_interrupt,
1073 IRQF_ONESHOT, devname: dev_name(dev),
1074 dev_id: sii902x);
1075 if (ret)
1076 return ret;
1077 }
1078
1079 ret = sii902x_audio_codec_init(sii902x, dev);
1080 if (ret)
1081 return ret;
1082
1083 i2c_set_clientdata(client: sii902x->i2c, data: sii902x);
1084
1085 sii902x->i2cmux = i2c_mux_alloc(parent: sii902x->i2c->adapter, dev,
1086 max_adapters: 1, sizeof_priv: 0, I2C_MUX_GATE,
1087 select: sii902x_i2c_bypass_select,
1088 deselect: sii902x_i2c_bypass_deselect);
1089 if (!sii902x->i2cmux) {
1090 ret = -ENOMEM;
1091 goto err_unreg_audio;
1092 }
1093
1094 sii902x->i2cmux->priv = sii902x;
1095 ret = i2c_mux_add_adapter(muxc: sii902x->i2cmux, force_nr: 0, chan_id: 0, class: 0);
1096 if (ret)
1097 goto err_unreg_audio;
1098
1099 sii902x->bridge.funcs = &sii902x_bridge_funcs;
1100 sii902x->bridge.of_node = dev->of_node;
1101 sii902x->bridge.timings = &default_sii902x_timings;
1102 sii902x->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
1103
1104 if (sii902x->i2c->irq > 0)
1105 sii902x->bridge.ops |= DRM_BRIDGE_OP_HPD;
1106
1107 drm_bridge_add(bridge: &sii902x->bridge);
1108
1109 return 0;
1110
1111err_unreg_audio:
1112 if (!PTR_ERR_OR_ZERO(ptr: sii902x->audio.pdev))
1113 platform_device_unregister(sii902x->audio.pdev);
1114
1115 return ret;
1116}
1117
1118static int sii902x_probe(struct i2c_client *client)
1119{
1120 struct device *dev = &client->dev;
1121 struct device_node *endpoint;
1122 struct sii902x *sii902x;
1123 static const char * const supplies[] = {"iovcc", "cvcc12"};
1124 int ret;
1125
1126 ret = i2c_check_functionality(adap: client->adapter,
1127 I2C_FUNC_SMBUS_BYTE_DATA);
1128 if (!ret) {
1129 dev_err(dev, "I2C adapter not suitable\n");
1130 return -EIO;
1131 }
1132
1133 sii902x = devm_kzalloc(dev, size: sizeof(*sii902x), GFP_KERNEL);
1134 if (!sii902x)
1135 return -ENOMEM;
1136
1137 sii902x->i2c = client;
1138 sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config);
1139 if (IS_ERR(ptr: sii902x->regmap))
1140 return PTR_ERR(ptr: sii902x->regmap);
1141
1142 sii902x->reset_gpio = devm_gpiod_get_optional(dev, con_id: "reset",
1143 flags: GPIOD_OUT_LOW);
1144 if (IS_ERR(ptr: sii902x->reset_gpio)) {
1145 dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n",
1146 PTR_ERR(sii902x->reset_gpio));
1147 return PTR_ERR(ptr: sii902x->reset_gpio);
1148 }
1149
1150 endpoint = of_graph_get_endpoint_by_regs(parent: dev->of_node, port_reg: 1, reg: -1);
1151 if (endpoint) {
1152 struct device_node *remote = of_graph_get_remote_port_parent(node: endpoint);
1153
1154 of_node_put(node: endpoint);
1155 if (!remote) {
1156 dev_err(dev, "Endpoint in port@1 unconnected\n");
1157 return -ENODEV;
1158 }
1159
1160 if (!of_device_is_available(device: remote)) {
1161 dev_err(dev, "port@1 remote device is disabled\n");
1162 of_node_put(node: remote);
1163 return -ENODEV;
1164 }
1165
1166 sii902x->next_bridge = of_drm_find_bridge(np: remote);
1167 of_node_put(node: remote);
1168 if (!sii902x->next_bridge)
1169 return dev_err_probe(dev, err: -EPROBE_DEFER,
1170 fmt: "Failed to find remote bridge\n");
1171 }
1172
1173 mutex_init(&sii902x->mutex);
1174
1175 ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(supplies), id: supplies);
1176 if (ret < 0)
1177 return dev_err_probe(dev, err: ret, fmt: "Failed to enable supplies");
1178
1179 return sii902x_init(sii902x);
1180}
1181
1182static void sii902x_remove(struct i2c_client *client)
1183{
1184 struct sii902x *sii902x = i2c_get_clientdata(client);
1185
1186 drm_bridge_remove(bridge: &sii902x->bridge);
1187 i2c_mux_del_adapters(muxc: sii902x->i2cmux);
1188
1189 if (!PTR_ERR_OR_ZERO(ptr: sii902x->audio.pdev))
1190 platform_device_unregister(sii902x->audio.pdev);
1191}
1192
1193static const struct of_device_id sii902x_dt_ids[] = {
1194 { .compatible = "sil,sii9022", },
1195 { }
1196};
1197MODULE_DEVICE_TABLE(of, sii902x_dt_ids);
1198
1199static const struct i2c_device_id sii902x_i2c_ids[] = {
1200 { "sii9022", 0 },
1201 { },
1202};
1203MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids);
1204
1205static struct i2c_driver sii902x_driver = {
1206 .probe = sii902x_probe,
1207 .remove = sii902x_remove,
1208 .driver = {
1209 .name = "sii902x",
1210 .of_match_table = sii902x_dt_ids,
1211 },
1212 .id_table = sii902x_i2c_ids,
1213};
1214module_i2c_driver(sii902x_driver);
1215
1216MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
1217MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
1218MODULE_LICENSE("GPL");
1219

source code of linux/drivers/gpu/drm/bridge/sii902x.c