1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 Renesas Electronics
5 * Copyright (C) 2016 Atmel
6 * Bo Shen <voice.shen@atmel.com>
8 * Authors: Bo Shen <voice.shen@atmel.com>
9 * Boris Brezillon <boris.brezillon@free-electrons.com>
10 * Wu, Songjun <Songjun.Wu@atmel.com>
12 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
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>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_edid.h>
24 #include <drm/drm_probe_helper.h>
26 #define SII902X_TPI_VIDEO_DATA 0x0
28 #define SII902X_TPI_PIXEL_REPETITION 0x8
29 #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5)
30 #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4)
31 #define SII902X_TPI_AVI_PIXEL_REP_4X 3
32 #define SII902X_TPI_AVI_PIXEL_REP_2X 1
33 #define SII902X_TPI_AVI_PIXEL_REP_NONE 0
34 #define SII902X_TPI_CLK_RATIO_HALF (0 << 6)
35 #define SII902X_TPI_CLK_RATIO_1X (1 << 6)
36 #define SII902X_TPI_CLK_RATIO_2X (2 << 6)
37 #define SII902X_TPI_CLK_RATIO_4X (3 << 6)
39 #define SII902X_TPI_AVI_IN_FORMAT 0x9
40 #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7)
41 #define SII902X_TPI_AVI_INPUT_DITHER BIT(6)
42 #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2)
43 #define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2)
44 #define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2)
45 #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0)
46 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0)
47 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0)
48 #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0)
50 #define SII902X_TPI_AVI_INFOFRAME 0x0c
52 #define SII902X_SYS_CTRL_DATA 0x1a
53 #define SII902X_SYS_CTRL_PWR_DWN BIT(4)
54 #define SII902X_SYS_CTRL_AV_MUTE BIT(3)
55 #define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2)
56 #define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1)
57 #define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0)
58 #define SII902X_SYS_CTRL_OUTPUT_HDMI 1
59 #define SII902X_SYS_CTRL_OUTPUT_DVI 0
61 #define SII902X_REG_CHIPID(n) (0x1b + (n))
63 #define SII902X_PWR_STATE_CTRL 0x1e
64 #define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0)
65 #define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK)
67 #define SII902X_INT_ENABLE 0x3c
68 #define SII902X_INT_STATUS 0x3d
69 #define SII902X_HOTPLUG_EVENT BIT(0)
70 #define SII902X_PLUGGED_STATUS BIT(2)
72 #define SII902X_REG_TPI_RQB 0xc7
74 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500
77 struct i2c_client
*i2c
;
78 struct regmap
*regmap
;
79 struct drm_bridge bridge
;
80 struct drm_connector connector
;
81 struct gpio_desc
*reset_gpio
;
82 struct i2c_mux_core
*i2cmux
;
85 static int sii902x_read_unlocked(struct i2c_client
*i2c
, u8 reg
, u8
*val
)
87 union i2c_smbus_data data
;
90 ret
= __i2c_smbus_xfer(i2c
->adapter
, i2c
->addr
, i2c
->flags
,
91 I2C_SMBUS_READ
, reg
, I2C_SMBUS_BYTE_DATA
, &data
);
100 static int sii902x_write_unlocked(struct i2c_client
*i2c
, u8 reg
, u8 val
)
102 union i2c_smbus_data data
;
106 return __i2c_smbus_xfer(i2c
->adapter
, i2c
->addr
, i2c
->flags
,
107 I2C_SMBUS_WRITE
, reg
, I2C_SMBUS_BYTE_DATA
,
111 static int sii902x_update_bits_unlocked(struct i2c_client
*i2c
, u8 reg
, u8 mask
,
117 ret
= sii902x_read_unlocked(i2c
, reg
, &status
);
121 status
|= val
& mask
;
122 return sii902x_write_unlocked(i2c
, reg
, status
);
125 static inline struct sii902x
*bridge_to_sii902x(struct drm_bridge
*bridge
)
127 return container_of(bridge
, struct sii902x
, bridge
);
130 static inline struct sii902x
*connector_to_sii902x(struct drm_connector
*con
)
132 return container_of(con
, struct sii902x
, connector
);
135 static void sii902x_reset(struct sii902x
*sii902x
)
137 if (!sii902x
->reset_gpio
)
140 gpiod_set_value(sii902x
->reset_gpio
, 1);
142 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
143 usleep_range(150, 200);
145 gpiod_set_value(sii902x
->reset_gpio
, 0);
148 static enum drm_connector_status
149 sii902x_connector_detect(struct drm_connector
*connector
, bool force
)
151 struct sii902x
*sii902x
= connector_to_sii902x(connector
);
154 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
156 return (status
& SII902X_PLUGGED_STATUS
) ?
157 connector_status_connected
: connector_status_disconnected
;
160 static const struct drm_connector_funcs sii902x_connector_funcs
= {
161 .detect
= sii902x_connector_detect
,
162 .fill_modes
= drm_helper_probe_single_connector_modes
,
163 .destroy
= drm_connector_cleanup
,
164 .reset
= drm_atomic_helper_connector_reset
,
165 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
166 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
169 static int sii902x_get_modes(struct drm_connector
*connector
)
171 struct sii902x
*sii902x
= connector_to_sii902x(connector
);
172 u32 bus_format
= MEDIA_BUS_FMT_RGB888_1X24
;
176 edid
= drm_get_edid(connector
, sii902x
->i2cmux
->adapter
[0]);
177 drm_connector_update_edid_property(connector
, edid
);
179 num
= drm_add_edid_modes(connector
, edid
);
183 ret
= drm_display_info_set_bus_formats(&connector
->display_info
,
191 static enum drm_mode_status
sii902x_mode_valid(struct drm_connector
*connector
,
192 struct drm_display_mode
*mode
)
194 /* TODO: check mode */
199 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs
= {
200 .get_modes
= sii902x_get_modes
,
201 .mode_valid
= sii902x_mode_valid
,
204 static void sii902x_bridge_disable(struct drm_bridge
*bridge
)
206 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
208 regmap_update_bits(sii902x
->regmap
, SII902X_SYS_CTRL_DATA
,
209 SII902X_SYS_CTRL_PWR_DWN
,
210 SII902X_SYS_CTRL_PWR_DWN
);
213 static void sii902x_bridge_enable(struct drm_bridge
*bridge
)
215 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
217 regmap_update_bits(sii902x
->regmap
, SII902X_PWR_STATE_CTRL
,
218 SII902X_AVI_POWER_STATE_MSK
,
219 SII902X_AVI_POWER_STATE_D(0));
220 regmap_update_bits(sii902x
->regmap
, SII902X_SYS_CTRL_DATA
,
221 SII902X_SYS_CTRL_PWR_DWN
, 0);
224 static void sii902x_bridge_mode_set(struct drm_bridge
*bridge
,
225 const struct drm_display_mode
*mode
,
226 const struct drm_display_mode
*adj
)
228 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
229 struct regmap
*regmap
= sii902x
->regmap
;
230 u8 buf
[HDMI_INFOFRAME_SIZE(AVI
)];
231 struct hdmi_avi_infoframe frame
;
235 buf
[1] = adj
->clock
>> 8;
236 buf
[2] = adj
->vrefresh
;
238 buf
[4] = adj
->hdisplay
;
239 buf
[5] = adj
->hdisplay
>> 8;
240 buf
[6] = adj
->vdisplay
;
241 buf
[7] = adj
->vdisplay
>> 8;
242 buf
[8] = SII902X_TPI_CLK_RATIO_1X
| SII902X_TPI_AVI_PIXEL_REP_NONE
|
243 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT
;
244 buf
[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO
|
245 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB
;
247 ret
= regmap_bulk_write(regmap
, SII902X_TPI_VIDEO_DATA
, buf
, 10);
251 ret
= drm_hdmi_avi_infoframe_from_display_mode(&frame
,
252 &sii902x
->connector
, adj
);
254 DRM_ERROR("couldn't fill AVI infoframe\n");
258 ret
= hdmi_avi_infoframe_pack(&frame
, buf
, sizeof(buf
));
260 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret
);
264 /* Do not send the infoframe header, but keep the CRC field. */
265 regmap_bulk_write(regmap
, SII902X_TPI_AVI_INFOFRAME
,
266 buf
+ HDMI_INFOFRAME_HEADER_SIZE
- 1,
267 HDMI_AVI_INFOFRAME_SIZE
+ 1);
270 static int sii902x_bridge_attach(struct drm_bridge
*bridge
)
272 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
273 struct drm_device
*drm
= bridge
->dev
;
276 drm_connector_helper_add(&sii902x
->connector
,
277 &sii902x_connector_helper_funcs
);
279 if (!drm_core_check_feature(drm
, DRIVER_ATOMIC
)) {
280 dev_err(&sii902x
->i2c
->dev
,
281 "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
285 ret
= drm_connector_init(drm
, &sii902x
->connector
,
286 &sii902x_connector_funcs
,
287 DRM_MODE_CONNECTOR_HDMIA
);
291 if (sii902x
->i2c
->irq
> 0)
292 sii902x
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
294 sii902x
->connector
.polled
= DRM_CONNECTOR_POLL_CONNECT
;
296 drm_connector_attach_encoder(&sii902x
->connector
, bridge
->encoder
);
301 static const struct drm_bridge_funcs sii902x_bridge_funcs
= {
302 .attach
= sii902x_bridge_attach
,
303 .mode_set
= sii902x_bridge_mode_set
,
304 .disable
= sii902x_bridge_disable
,
305 .enable
= sii902x_bridge_enable
,
308 static const struct regmap_range sii902x_volatile_ranges
[] = {
309 { .range_min
= 0, .range_max
= 0xff },
312 static const struct regmap_access_table sii902x_volatile_table
= {
313 .yes_ranges
= sii902x_volatile_ranges
,
314 .n_yes_ranges
= ARRAY_SIZE(sii902x_volatile_ranges
),
317 static const struct regmap_config sii902x_regmap_config
= {
320 .volatile_table
= &sii902x_volatile_table
,
321 .cache_type
= REGCACHE_NONE
,
324 static irqreturn_t
sii902x_interrupt(int irq
, void *data
)
326 struct sii902x
*sii902x
= data
;
327 unsigned int status
= 0;
329 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
330 regmap_write(sii902x
->regmap
, SII902X_INT_STATUS
, status
);
332 if ((status
& SII902X_HOTPLUG_EVENT
) && sii902x
->bridge
.dev
)
333 drm_helper_hpd_irq_event(sii902x
->bridge
.dev
);
339 * The purpose of sii902x_i2c_bypass_select is to enable the pass through
340 * mode of the HDMI transmitter. Do not use regmap from within this function,
341 * only use sii902x_*_unlocked functions to read/modify/write registers.
342 * We are holding the parent adapter lock here, keep this in mind before
343 * adding more i2c transactions.
345 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
346 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
347 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
348 * we leave the remaining bits as we have found them.
350 static int sii902x_i2c_bypass_select(struct i2c_mux_core
*mux
, u32 chan_id
)
352 struct sii902x
*sii902x
= i2c_mux_priv(mux
);
353 struct device
*dev
= &sii902x
->i2c
->dev
;
354 unsigned long timeout
;
358 ret
= sii902x_update_bits_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
359 SII902X_SYS_CTRL_DDC_BUS_REQ
,
360 SII902X_SYS_CTRL_DDC_BUS_REQ
);
365 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS
);
367 ret
= sii902x_read_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
371 } while (!(status
& SII902X_SYS_CTRL_DDC_BUS_GRTD
) &&
372 time_before(jiffies
, timeout
));
374 if (!(status
& SII902X_SYS_CTRL_DDC_BUS_GRTD
)) {
375 dev_err(dev
, "Failed to acquire the i2c bus\n");
379 return sii902x_write_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
384 * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through
385 * mode of the HDMI transmitter. Do not use regmap from within this function,
386 * only use sii902x_*_unlocked functions to read/modify/write registers.
387 * We are holding the parent adapter lock here, keep this in mind before
388 * adding more i2c transactions.
390 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
391 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
392 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
393 * we leave the remaining bits as we have found them.
395 static int sii902x_i2c_bypass_deselect(struct i2c_mux_core
*mux
, u32 chan_id
)
397 struct sii902x
*sii902x
= i2c_mux_priv(mux
);
398 struct device
*dev
= &sii902x
->i2c
->dev
;
399 unsigned long timeout
;
400 unsigned int retries
;
405 * When the HDMI transmitter is in pass through mode, we need an
406 * (undocumented) additional delay between STOP and START conditions
407 * to guarantee the bus won't get stuck.
412 * Sometimes the I2C bus can stall after failure to use the
413 * EDID channel. Retry a few times to see if things clear
414 * up, else continue anyway.
418 ret
= sii902x_read_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
421 } while (ret
&& retries
);
423 dev_err(dev
, "failed to read status (%d)\n", ret
);
427 ret
= sii902x_update_bits_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
428 SII902X_SYS_CTRL_DDC_BUS_REQ
|
429 SII902X_SYS_CTRL_DDC_BUS_GRTD
, 0);
434 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS
);
436 ret
= sii902x_read_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
440 } while (status
& (SII902X_SYS_CTRL_DDC_BUS_REQ
|
441 SII902X_SYS_CTRL_DDC_BUS_GRTD
) &&
442 time_before(jiffies
, timeout
));
444 if (status
& (SII902X_SYS_CTRL_DDC_BUS_REQ
|
445 SII902X_SYS_CTRL_DDC_BUS_GRTD
)) {
446 dev_err(dev
, "failed to release the i2c bus\n");
453 static int sii902x_probe(struct i2c_client
*client
,
454 const struct i2c_device_id
*id
)
456 struct device
*dev
= &client
->dev
;
457 unsigned int status
= 0;
458 struct sii902x
*sii902x
;
462 ret
= i2c_check_functionality(client
->adapter
,
463 I2C_FUNC_SMBUS_BYTE_DATA
);
465 dev_err(dev
, "I2C adapter not suitable\n");
469 sii902x
= devm_kzalloc(dev
, sizeof(*sii902x
), GFP_KERNEL
);
473 sii902x
->i2c
= client
;
474 sii902x
->regmap
= devm_regmap_init_i2c(client
, &sii902x_regmap_config
);
475 if (IS_ERR(sii902x
->regmap
))
476 return PTR_ERR(sii902x
->regmap
);
478 sii902x
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
480 if (IS_ERR(sii902x
->reset_gpio
)) {
481 dev_err(dev
, "Failed to retrieve/request reset gpio: %ld\n",
482 PTR_ERR(sii902x
->reset_gpio
));
483 return PTR_ERR(sii902x
->reset_gpio
);
486 sii902x_reset(sii902x
);
488 ret
= regmap_write(sii902x
->regmap
, SII902X_REG_TPI_RQB
, 0x0);
492 ret
= regmap_bulk_read(sii902x
->regmap
, SII902X_REG_CHIPID(0),
495 dev_err(dev
, "regmap_read failed %d\n", ret
);
499 if (chipid
[0] != 0xb0) {
500 dev_err(dev
, "Invalid chipid: %02x (expecting 0xb0)\n",
505 /* Clear all pending interrupts */
506 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
507 regmap_write(sii902x
->regmap
, SII902X_INT_STATUS
, status
);
509 if (client
->irq
> 0) {
510 regmap_write(sii902x
->regmap
, SII902X_INT_ENABLE
,
511 SII902X_HOTPLUG_EVENT
);
513 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
515 IRQF_ONESHOT
, dev_name(dev
),
521 sii902x
->bridge
.funcs
= &sii902x_bridge_funcs
;
522 sii902x
->bridge
.of_node
= dev
->of_node
;
523 drm_bridge_add(&sii902x
->bridge
);
525 i2c_set_clientdata(client
, sii902x
);
527 sii902x
->i2cmux
= i2c_mux_alloc(client
->adapter
, dev
,
529 sii902x_i2c_bypass_select
,
530 sii902x_i2c_bypass_deselect
);
531 if (!sii902x
->i2cmux
)
534 sii902x
->i2cmux
->priv
= sii902x
;
535 return i2c_mux_add_adapter(sii902x
->i2cmux
, 0, 0, 0);
538 static int sii902x_remove(struct i2c_client
*client
)
541 struct sii902x
*sii902x
= i2c_get_clientdata(client
);
543 i2c_mux_del_adapters(sii902x
->i2cmux
);
544 drm_bridge_remove(&sii902x
->bridge
);
549 static const struct of_device_id sii902x_dt_ids
[] = {
550 { .compatible
= "sil,sii9022", },
553 MODULE_DEVICE_TABLE(of
, sii902x_dt_ids
);
555 static const struct i2c_device_id sii902x_i2c_ids
[] = {
559 MODULE_DEVICE_TABLE(i2c
, sii902x_i2c_ids
);
561 static struct i2c_driver sii902x_driver
= {
562 .probe
= sii902x_probe
,
563 .remove
= sii902x_remove
,
566 .of_match_table
= sii902x_dt_ids
,
568 .id_table
= sii902x_i2c_ids
,
570 module_i2c_driver(sii902x_driver
);
572 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
573 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
574 MODULE_LICENSE("GPL");