2 * Copyright (C) 2018 Renesas Electronics
4 * Copyright (C) 2016 Atmel
5 * Bo Shen <voice.shen@atmel.com>
7 * Authors: Bo Shen <voice.shen@atmel.com>
8 * Boris Brezillon <boris.brezillon@free-electrons.com>
9 * Wu, Songjun <Songjun.Wu@atmel.com>
12 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include <linux/gpio/consumer.h>
26 #include <linux/i2c-mux.h>
27 #include <linux/i2c.h>
28 #include <linux/module.h>
29 #include <linux/regmap.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_edid.h>
34 #include <drm/drm_probe_helper.h>
36 #define SII902X_TPI_VIDEO_DATA 0x0
38 #define SII902X_TPI_PIXEL_REPETITION 0x8
39 #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5)
40 #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4)
41 #define SII902X_TPI_AVI_PIXEL_REP_4X 3
42 #define SII902X_TPI_AVI_PIXEL_REP_2X 1
43 #define SII902X_TPI_AVI_PIXEL_REP_NONE 0
44 #define SII902X_TPI_CLK_RATIO_HALF (0 << 6)
45 #define SII902X_TPI_CLK_RATIO_1X (1 << 6)
46 #define SII902X_TPI_CLK_RATIO_2X (2 << 6)
47 #define SII902X_TPI_CLK_RATIO_4X (3 << 6)
49 #define SII902X_TPI_AVI_IN_FORMAT 0x9
50 #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7)
51 #define SII902X_TPI_AVI_INPUT_DITHER BIT(6)
52 #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2)
53 #define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2)
54 #define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2)
55 #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0)
56 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0)
57 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0)
58 #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0)
60 #define SII902X_TPI_AVI_INFOFRAME 0x0c
62 #define SII902X_SYS_CTRL_DATA 0x1a
63 #define SII902X_SYS_CTRL_PWR_DWN BIT(4)
64 #define SII902X_SYS_CTRL_AV_MUTE BIT(3)
65 #define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2)
66 #define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1)
67 #define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0)
68 #define SII902X_SYS_CTRL_OUTPUT_HDMI 1
69 #define SII902X_SYS_CTRL_OUTPUT_DVI 0
71 #define SII902X_REG_CHIPID(n) (0x1b + (n))
73 #define SII902X_PWR_STATE_CTRL 0x1e
74 #define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0)
75 #define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK)
77 #define SII902X_INT_ENABLE 0x3c
78 #define SII902X_INT_STATUS 0x3d
79 #define SII902X_HOTPLUG_EVENT BIT(0)
80 #define SII902X_PLUGGED_STATUS BIT(2)
82 #define SII902X_REG_TPI_RQB 0xc7
84 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500
87 struct i2c_client
*i2c
;
88 struct regmap
*regmap
;
89 struct drm_bridge bridge
;
90 struct drm_connector connector
;
91 struct gpio_desc
*reset_gpio
;
92 struct i2c_mux_core
*i2cmux
;
95 static int sii902x_read_unlocked(struct i2c_client
*i2c
, u8 reg
, u8
*val
)
97 union i2c_smbus_data data
;
100 ret
= __i2c_smbus_xfer(i2c
->adapter
, i2c
->addr
, i2c
->flags
,
101 I2C_SMBUS_READ
, reg
, I2C_SMBUS_BYTE_DATA
, &data
);
110 static int sii902x_write_unlocked(struct i2c_client
*i2c
, u8 reg
, u8 val
)
112 union i2c_smbus_data data
;
116 return __i2c_smbus_xfer(i2c
->adapter
, i2c
->addr
, i2c
->flags
,
117 I2C_SMBUS_WRITE
, reg
, I2C_SMBUS_BYTE_DATA
,
121 static int sii902x_update_bits_unlocked(struct i2c_client
*i2c
, u8 reg
, u8 mask
,
127 ret
= sii902x_read_unlocked(i2c
, reg
, &status
);
131 status
|= val
& mask
;
132 return sii902x_write_unlocked(i2c
, reg
, status
);
135 static inline struct sii902x
*bridge_to_sii902x(struct drm_bridge
*bridge
)
137 return container_of(bridge
, struct sii902x
, bridge
);
140 static inline struct sii902x
*connector_to_sii902x(struct drm_connector
*con
)
142 return container_of(con
, struct sii902x
, connector
);
145 static void sii902x_reset(struct sii902x
*sii902x
)
147 if (!sii902x
->reset_gpio
)
150 gpiod_set_value(sii902x
->reset_gpio
, 1);
152 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
153 usleep_range(150, 200);
155 gpiod_set_value(sii902x
->reset_gpio
, 0);
158 static enum drm_connector_status
159 sii902x_connector_detect(struct drm_connector
*connector
, bool force
)
161 struct sii902x
*sii902x
= connector_to_sii902x(connector
);
164 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
166 return (status
& SII902X_PLUGGED_STATUS
) ?
167 connector_status_connected
: connector_status_disconnected
;
170 static const struct drm_connector_funcs sii902x_connector_funcs
= {
171 .detect
= sii902x_connector_detect
,
172 .fill_modes
= drm_helper_probe_single_connector_modes
,
173 .destroy
= drm_connector_cleanup
,
174 .reset
= drm_atomic_helper_connector_reset
,
175 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
176 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
179 static int sii902x_get_modes(struct drm_connector
*connector
)
181 struct sii902x
*sii902x
= connector_to_sii902x(connector
);
182 u32 bus_format
= MEDIA_BUS_FMT_RGB888_1X24
;
186 edid
= drm_get_edid(connector
, sii902x
->i2cmux
->adapter
[0]);
187 drm_connector_update_edid_property(connector
, edid
);
189 num
= drm_add_edid_modes(connector
, edid
);
193 ret
= drm_display_info_set_bus_formats(&connector
->display_info
,
201 static enum drm_mode_status
sii902x_mode_valid(struct drm_connector
*connector
,
202 struct drm_display_mode
*mode
)
204 /* TODO: check mode */
209 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs
= {
210 .get_modes
= sii902x_get_modes
,
211 .mode_valid
= sii902x_mode_valid
,
214 static void sii902x_bridge_disable(struct drm_bridge
*bridge
)
216 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
218 regmap_update_bits(sii902x
->regmap
, SII902X_SYS_CTRL_DATA
,
219 SII902X_SYS_CTRL_PWR_DWN
,
220 SII902X_SYS_CTRL_PWR_DWN
);
223 static void sii902x_bridge_enable(struct drm_bridge
*bridge
)
225 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
227 regmap_update_bits(sii902x
->regmap
, SII902X_PWR_STATE_CTRL
,
228 SII902X_AVI_POWER_STATE_MSK
,
229 SII902X_AVI_POWER_STATE_D(0));
230 regmap_update_bits(sii902x
->regmap
, SII902X_SYS_CTRL_DATA
,
231 SII902X_SYS_CTRL_PWR_DWN
, 0);
234 static void sii902x_bridge_mode_set(struct drm_bridge
*bridge
,
235 const struct drm_display_mode
*mode
,
236 const struct drm_display_mode
*adj
)
238 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
239 struct regmap
*regmap
= sii902x
->regmap
;
240 u8 buf
[HDMI_INFOFRAME_SIZE(AVI
)];
241 struct hdmi_avi_infoframe frame
;
245 buf
[1] = adj
->clock
>> 8;
246 buf
[2] = adj
->vrefresh
;
248 buf
[4] = adj
->hdisplay
;
249 buf
[5] = adj
->hdisplay
>> 8;
250 buf
[6] = adj
->vdisplay
;
251 buf
[7] = adj
->vdisplay
>> 8;
252 buf
[8] = SII902X_TPI_CLK_RATIO_1X
| SII902X_TPI_AVI_PIXEL_REP_NONE
|
253 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT
;
254 buf
[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO
|
255 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB
;
257 ret
= regmap_bulk_write(regmap
, SII902X_TPI_VIDEO_DATA
, buf
, 10);
261 ret
= drm_hdmi_avi_infoframe_from_display_mode(&frame
,
262 &sii902x
->connector
, adj
);
264 DRM_ERROR("couldn't fill AVI infoframe\n");
268 ret
= hdmi_avi_infoframe_pack(&frame
, buf
, sizeof(buf
));
270 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret
);
274 /* Do not send the infoframe header, but keep the CRC field. */
275 regmap_bulk_write(regmap
, SII902X_TPI_AVI_INFOFRAME
,
276 buf
+ HDMI_INFOFRAME_HEADER_SIZE
- 1,
277 HDMI_AVI_INFOFRAME_SIZE
+ 1);
280 static int sii902x_bridge_attach(struct drm_bridge
*bridge
)
282 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
283 struct drm_device
*drm
= bridge
->dev
;
286 drm_connector_helper_add(&sii902x
->connector
,
287 &sii902x_connector_helper_funcs
);
289 if (!drm_core_check_feature(drm
, DRIVER_ATOMIC
)) {
290 dev_err(&sii902x
->i2c
->dev
,
291 "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
295 ret
= drm_connector_init(drm
, &sii902x
->connector
,
296 &sii902x_connector_funcs
,
297 DRM_MODE_CONNECTOR_HDMIA
);
301 if (sii902x
->i2c
->irq
> 0)
302 sii902x
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
304 sii902x
->connector
.polled
= DRM_CONNECTOR_POLL_CONNECT
;
306 drm_connector_attach_encoder(&sii902x
->connector
, bridge
->encoder
);
311 static const struct drm_bridge_funcs sii902x_bridge_funcs
= {
312 .attach
= sii902x_bridge_attach
,
313 .mode_set
= sii902x_bridge_mode_set
,
314 .disable
= sii902x_bridge_disable
,
315 .enable
= sii902x_bridge_enable
,
318 static const struct regmap_range sii902x_volatile_ranges
[] = {
319 { .range_min
= 0, .range_max
= 0xff },
322 static const struct regmap_access_table sii902x_volatile_table
= {
323 .yes_ranges
= sii902x_volatile_ranges
,
324 .n_yes_ranges
= ARRAY_SIZE(sii902x_volatile_ranges
),
327 static const struct regmap_config sii902x_regmap_config
= {
330 .volatile_table
= &sii902x_volatile_table
,
331 .cache_type
= REGCACHE_NONE
,
334 static irqreturn_t
sii902x_interrupt(int irq
, void *data
)
336 struct sii902x
*sii902x
= data
;
337 unsigned int status
= 0;
339 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
340 regmap_write(sii902x
->regmap
, SII902X_INT_STATUS
, status
);
342 if ((status
& SII902X_HOTPLUG_EVENT
) && sii902x
->bridge
.dev
)
343 drm_helper_hpd_irq_event(sii902x
->bridge
.dev
);
349 * The purpose of sii902x_i2c_bypass_select is to enable the pass through
350 * mode of the HDMI transmitter. Do not use regmap from within this function,
351 * only use sii902x_*_unlocked functions to read/modify/write registers.
352 * We are holding the parent adapter lock here, keep this in mind before
353 * adding more i2c transactions.
355 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
356 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
357 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
358 * we leave the remaining bits as we have found them.
360 static int sii902x_i2c_bypass_select(struct i2c_mux_core
*mux
, u32 chan_id
)
362 struct sii902x
*sii902x
= i2c_mux_priv(mux
);
363 struct device
*dev
= &sii902x
->i2c
->dev
;
364 unsigned long timeout
;
368 ret
= sii902x_update_bits_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
369 SII902X_SYS_CTRL_DDC_BUS_REQ
,
370 SII902X_SYS_CTRL_DDC_BUS_REQ
);
375 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS
);
377 ret
= sii902x_read_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
381 } while (!(status
& SII902X_SYS_CTRL_DDC_BUS_GRTD
) &&
382 time_before(jiffies
, timeout
));
384 if (!(status
& SII902X_SYS_CTRL_DDC_BUS_GRTD
)) {
385 dev_err(dev
, "Failed to acquire the i2c bus\n");
389 return sii902x_write_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
394 * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through
395 * mode of the HDMI transmitter. Do not use regmap from within this function,
396 * only use sii902x_*_unlocked functions to read/modify/write registers.
397 * We are holding the parent adapter lock here, keep this in mind before
398 * adding more i2c transactions.
400 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere
401 * in this driver, we need to make sure that we only touch 0x1A[2:1] from
402 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that
403 * we leave the remaining bits as we have found them.
405 static int sii902x_i2c_bypass_deselect(struct i2c_mux_core
*mux
, u32 chan_id
)
407 struct sii902x
*sii902x
= i2c_mux_priv(mux
);
408 struct device
*dev
= &sii902x
->i2c
->dev
;
409 unsigned long timeout
;
410 unsigned int retries
;
415 * When the HDMI transmitter is in pass through mode, we need an
416 * (undocumented) additional delay between STOP and START conditions
417 * to guarantee the bus won't get stuck.
422 * Sometimes the I2C bus can stall after failure to use the
423 * EDID channel. Retry a few times to see if things clear
424 * up, else continue anyway.
428 ret
= sii902x_read_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
431 } while (ret
&& retries
);
433 dev_err(dev
, "failed to read status (%d)\n", ret
);
437 ret
= sii902x_update_bits_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
438 SII902X_SYS_CTRL_DDC_BUS_REQ
|
439 SII902X_SYS_CTRL_DDC_BUS_GRTD
, 0);
444 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS
);
446 ret
= sii902x_read_unlocked(sii902x
->i2c
, SII902X_SYS_CTRL_DATA
,
450 } while (status
& (SII902X_SYS_CTRL_DDC_BUS_REQ
|
451 SII902X_SYS_CTRL_DDC_BUS_GRTD
) &&
452 time_before(jiffies
, timeout
));
454 if (status
& (SII902X_SYS_CTRL_DDC_BUS_REQ
|
455 SII902X_SYS_CTRL_DDC_BUS_GRTD
)) {
456 dev_err(dev
, "failed to release the i2c bus\n");
463 static int sii902x_probe(struct i2c_client
*client
,
464 const struct i2c_device_id
*id
)
466 struct device
*dev
= &client
->dev
;
467 unsigned int status
= 0;
468 struct sii902x
*sii902x
;
472 ret
= i2c_check_functionality(client
->adapter
,
473 I2C_FUNC_SMBUS_BYTE_DATA
);
475 dev_err(dev
, "I2C adapter not suitable\n");
479 sii902x
= devm_kzalloc(dev
, sizeof(*sii902x
), GFP_KERNEL
);
483 sii902x
->i2c
= client
;
484 sii902x
->regmap
= devm_regmap_init_i2c(client
, &sii902x_regmap_config
);
485 if (IS_ERR(sii902x
->regmap
))
486 return PTR_ERR(sii902x
->regmap
);
488 sii902x
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
490 if (IS_ERR(sii902x
->reset_gpio
)) {
491 dev_err(dev
, "Failed to retrieve/request reset gpio: %ld\n",
492 PTR_ERR(sii902x
->reset_gpio
));
493 return PTR_ERR(sii902x
->reset_gpio
);
496 sii902x_reset(sii902x
);
498 ret
= regmap_write(sii902x
->regmap
, SII902X_REG_TPI_RQB
, 0x0);
502 ret
= regmap_bulk_read(sii902x
->regmap
, SII902X_REG_CHIPID(0),
505 dev_err(dev
, "regmap_read failed %d\n", ret
);
509 if (chipid
[0] != 0xb0) {
510 dev_err(dev
, "Invalid chipid: %02x (expecting 0xb0)\n",
515 /* Clear all pending interrupts */
516 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
517 regmap_write(sii902x
->regmap
, SII902X_INT_STATUS
, status
);
519 if (client
->irq
> 0) {
520 regmap_write(sii902x
->regmap
, SII902X_INT_ENABLE
,
521 SII902X_HOTPLUG_EVENT
);
523 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
525 IRQF_ONESHOT
, dev_name(dev
),
531 sii902x
->bridge
.funcs
= &sii902x_bridge_funcs
;
532 sii902x
->bridge
.of_node
= dev
->of_node
;
533 drm_bridge_add(&sii902x
->bridge
);
535 i2c_set_clientdata(client
, sii902x
);
537 sii902x
->i2cmux
= i2c_mux_alloc(client
->adapter
, dev
,
539 sii902x_i2c_bypass_select
,
540 sii902x_i2c_bypass_deselect
);
541 if (!sii902x
->i2cmux
)
544 sii902x
->i2cmux
->priv
= sii902x
;
545 return i2c_mux_add_adapter(sii902x
->i2cmux
, 0, 0, 0);
548 static int sii902x_remove(struct i2c_client
*client
)
551 struct sii902x
*sii902x
= i2c_get_clientdata(client
);
553 i2c_mux_del_adapters(sii902x
->i2cmux
);
554 drm_bridge_remove(&sii902x
->bridge
);
559 static const struct of_device_id sii902x_dt_ids
[] = {
560 { .compatible
= "sil,sii9022", },
563 MODULE_DEVICE_TABLE(of
, sii902x_dt_ids
);
565 static const struct i2c_device_id sii902x_i2c_ids
[] = {
569 MODULE_DEVICE_TABLE(i2c
, sii902x_i2c_ids
);
571 static struct i2c_driver sii902x_driver
= {
572 .probe
= sii902x_probe
,
573 .remove
= sii902x_remove
,
576 .of_match_table
= sii902x_dt_ids
,
578 .id_table
= sii902x_i2c_ids
,
580 module_i2c_driver(sii902x_driver
);
582 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
583 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
584 MODULE_LICENSE("GPL");