2 * Copyright (C) 2016 Atmel
3 * Bo Shen <voice.shen@atmel.com>
5 * Authors: Bo Shen <voice.shen@atmel.com>
6 * Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Wu, Songjun <Songjun.Wu@atmel.com>
10 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 #include <linux/gpio/consumer.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/regmap.h>
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/drm_edid.h>
33 #define SII902X_TPI_VIDEO_DATA 0x0
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)
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)
57 #define SII902X_TPI_AVI_INFOFRAME 0x0c
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
68 #define SII902X_REG_CHIPID(n) (0x1b + (n))
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)
74 #define SII902X_INT_ENABLE 0x3c
75 #define SII902X_INT_STATUS 0x3d
76 #define SII902X_HOTPLUG_EVENT BIT(0)
77 #define SII902X_PLUGGED_STATUS BIT(2)
79 #define SII902X_REG_TPI_RQB 0xc7
81 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500
84 struct i2c_client
*i2c
;
85 struct regmap
*regmap
;
86 struct drm_bridge bridge
;
87 struct drm_connector connector
;
88 struct gpio_desc
*reset_gpio
;
91 static inline struct sii902x
*bridge_to_sii902x(struct drm_bridge
*bridge
)
93 return container_of(bridge
, struct sii902x
, bridge
);
96 static inline struct sii902x
*connector_to_sii902x(struct drm_connector
*con
)
98 return container_of(con
, struct sii902x
, connector
);
101 static void sii902x_reset(struct sii902x
*sii902x
)
103 if (!sii902x
->reset_gpio
)
106 gpiod_set_value(sii902x
->reset_gpio
, 1);
108 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */
109 usleep_range(150, 200);
111 gpiod_set_value(sii902x
->reset_gpio
, 0);
114 static enum drm_connector_status
115 sii902x_connector_detect(struct drm_connector
*connector
, bool force
)
117 struct sii902x
*sii902x
= connector_to_sii902x(connector
);
120 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
122 return (status
& SII902X_PLUGGED_STATUS
) ?
123 connector_status_connected
: connector_status_disconnected
;
126 static const struct drm_connector_funcs sii902x_connector_funcs
= {
127 .dpms
= drm_atomic_helper_connector_dpms
,
128 .detect
= sii902x_connector_detect
,
129 .fill_modes
= drm_helper_probe_single_connector_modes
,
130 .destroy
= drm_connector_cleanup
,
131 .reset
= drm_atomic_helper_connector_reset
,
132 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
133 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
136 static int sii902x_get_modes(struct drm_connector
*connector
)
138 struct sii902x
*sii902x
= connector_to_sii902x(connector
);
139 struct regmap
*regmap
= sii902x
->regmap
;
140 u32 bus_format
= MEDIA_BUS_FMT_RGB888_1X24
;
141 unsigned long timeout
;
147 ret
= regmap_update_bits(regmap
, SII902X_SYS_CTRL_DATA
,
148 SII902X_SYS_CTRL_DDC_BUS_REQ
,
149 SII902X_SYS_CTRL_DDC_BUS_REQ
);
154 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS
);
156 ret
= regmap_read(regmap
, SII902X_SYS_CTRL_DATA
, &status
);
159 } while (!(status
& SII902X_SYS_CTRL_DDC_BUS_GRTD
) &&
160 time_before(jiffies
, timeout
));
162 if (!(status
& SII902X_SYS_CTRL_DDC_BUS_GRTD
)) {
163 dev_err(&sii902x
->i2c
->dev
, "failed to acquire the i2c bus\n");
167 ret
= regmap_write(regmap
, SII902X_SYS_CTRL_DATA
, status
);
171 edid
= drm_get_edid(connector
, sii902x
->i2c
->adapter
);
172 drm_mode_connector_update_edid_property(connector
, edid
);
174 num
= drm_add_edid_modes(connector
, edid
);
178 ret
= drm_display_info_set_bus_formats(&connector
->display_info
,
183 ret
= regmap_read(regmap
, SII902X_SYS_CTRL_DATA
, &status
);
187 ret
= regmap_update_bits(regmap
, SII902X_SYS_CTRL_DATA
,
188 SII902X_SYS_CTRL_DDC_BUS_REQ
|
189 SII902X_SYS_CTRL_DDC_BUS_GRTD
, 0);
194 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS
);
196 ret
= regmap_read(regmap
, SII902X_SYS_CTRL_DATA
, &status
);
199 } while (status
& (SII902X_SYS_CTRL_DDC_BUS_REQ
|
200 SII902X_SYS_CTRL_DDC_BUS_GRTD
) &&
201 time_before(jiffies
, timeout
));
203 if (status
& (SII902X_SYS_CTRL_DDC_BUS_REQ
|
204 SII902X_SYS_CTRL_DDC_BUS_GRTD
)) {
205 dev_err(&sii902x
->i2c
->dev
, "failed to release the i2c bus\n");
212 static enum drm_mode_status
sii902x_mode_valid(struct drm_connector
*connector
,
213 struct drm_display_mode
*mode
)
215 /* TODO: check mode */
220 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs
= {
221 .get_modes
= sii902x_get_modes
,
222 .mode_valid
= sii902x_mode_valid
,
225 static void sii902x_bridge_disable(struct drm_bridge
*bridge
)
227 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
229 regmap_update_bits(sii902x
->regmap
, SII902X_SYS_CTRL_DATA
,
230 SII902X_SYS_CTRL_PWR_DWN
,
231 SII902X_SYS_CTRL_PWR_DWN
);
234 static void sii902x_bridge_enable(struct drm_bridge
*bridge
)
236 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
238 regmap_update_bits(sii902x
->regmap
, SII902X_PWR_STATE_CTRL
,
239 SII902X_AVI_POWER_STATE_MSK
,
240 SII902X_AVI_POWER_STATE_D(0));
241 regmap_update_bits(sii902x
->regmap
, SII902X_SYS_CTRL_DATA
,
242 SII902X_SYS_CTRL_PWR_DWN
, 0);
245 static void sii902x_bridge_mode_set(struct drm_bridge
*bridge
,
246 struct drm_display_mode
*mode
,
247 struct drm_display_mode
*adj
)
249 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
250 struct regmap
*regmap
= sii902x
->regmap
;
251 u8 buf
[HDMI_INFOFRAME_SIZE(AVI
)];
252 struct hdmi_avi_infoframe frame
;
256 buf
[1] = adj
->clock
>> 8;
257 buf
[2] = adj
->vrefresh
;
259 buf
[4] = adj
->hdisplay
;
260 buf
[5] = adj
->hdisplay
>> 8;
261 buf
[6] = adj
->vdisplay
;
262 buf
[7] = adj
->vdisplay
>> 8;
263 buf
[8] = SII902X_TPI_CLK_RATIO_1X
| SII902X_TPI_AVI_PIXEL_REP_NONE
|
264 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT
;
265 buf
[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO
|
266 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB
;
268 ret
= regmap_bulk_write(regmap
, SII902X_TPI_VIDEO_DATA
, buf
, 10);
272 ret
= drm_hdmi_avi_infoframe_from_display_mode(&frame
, adj
);
274 DRM_ERROR("couldn't fill AVI infoframe\n");
278 ret
= hdmi_avi_infoframe_pack(&frame
, buf
, sizeof(buf
));
280 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret
);
284 /* Do not send the infoframe header, but keep the CRC field. */
285 regmap_bulk_write(regmap
, SII902X_TPI_AVI_INFOFRAME
,
286 buf
+ HDMI_INFOFRAME_HEADER_SIZE
- 1,
287 HDMI_AVI_INFOFRAME_SIZE
+ 1);
290 static int sii902x_bridge_attach(struct drm_bridge
*bridge
)
292 struct sii902x
*sii902x
= bridge_to_sii902x(bridge
);
293 struct drm_device
*drm
= bridge
->dev
;
296 drm_connector_helper_add(&sii902x
->connector
,
297 &sii902x_connector_helper_funcs
);
299 if (!drm_core_check_feature(drm
, DRIVER_ATOMIC
)) {
300 dev_err(&sii902x
->i2c
->dev
,
301 "sii902x driver is only compatible with DRM devices supporting atomic updates\n");
305 ret
= drm_connector_init(drm
, &sii902x
->connector
,
306 &sii902x_connector_funcs
,
307 DRM_MODE_CONNECTOR_HDMIA
);
311 if (sii902x
->i2c
->irq
> 0)
312 sii902x
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
314 sii902x
->connector
.polled
= DRM_CONNECTOR_POLL_CONNECT
;
316 drm_mode_connector_attach_encoder(&sii902x
->connector
, bridge
->encoder
);
321 static const struct drm_bridge_funcs sii902x_bridge_funcs
= {
322 .attach
= sii902x_bridge_attach
,
323 .mode_set
= sii902x_bridge_mode_set
,
324 .disable
= sii902x_bridge_disable
,
325 .enable
= sii902x_bridge_enable
,
328 static const struct regmap_range sii902x_volatile_ranges
[] = {
329 { .range_min
= 0, .range_max
= 0xff },
332 static const struct regmap_access_table sii902x_volatile_table
= {
333 .yes_ranges
= sii902x_volatile_ranges
,
334 .n_yes_ranges
= ARRAY_SIZE(sii902x_volatile_ranges
),
337 static const struct regmap_config sii902x_regmap_config
= {
340 .volatile_table
= &sii902x_volatile_table
,
341 .cache_type
= REGCACHE_NONE
,
344 static irqreturn_t
sii902x_interrupt(int irq
, void *data
)
346 struct sii902x
*sii902x
= data
;
347 unsigned int status
= 0;
349 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
350 regmap_write(sii902x
->regmap
, SII902X_INT_STATUS
, status
);
352 if ((status
& SII902X_HOTPLUG_EVENT
) && sii902x
->bridge
.dev
)
353 drm_helper_hpd_irq_event(sii902x
->bridge
.dev
);
358 static int sii902x_probe(struct i2c_client
*client
,
359 const struct i2c_device_id
*id
)
361 struct device
*dev
= &client
->dev
;
362 unsigned int status
= 0;
363 struct sii902x
*sii902x
;
367 sii902x
= devm_kzalloc(dev
, sizeof(*sii902x
), GFP_KERNEL
);
371 sii902x
->i2c
= client
;
372 sii902x
->regmap
= devm_regmap_init_i2c(client
, &sii902x_regmap_config
);
373 if (IS_ERR(sii902x
->regmap
))
374 return PTR_ERR(sii902x
->regmap
);
376 sii902x
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
378 if (IS_ERR(sii902x
->reset_gpio
)) {
379 dev_err(dev
, "Failed to retrieve/request reset gpio: %ld\n",
380 PTR_ERR(sii902x
->reset_gpio
));
381 return PTR_ERR(sii902x
->reset_gpio
);
384 sii902x_reset(sii902x
);
386 ret
= regmap_write(sii902x
->regmap
, SII902X_REG_TPI_RQB
, 0x0);
390 ret
= regmap_bulk_read(sii902x
->regmap
, SII902X_REG_CHIPID(0),
393 dev_err(dev
, "regmap_read failed %d\n", ret
);
397 if (chipid
[0] != 0xb0) {
398 dev_err(dev
, "Invalid chipid: %02x (expecting 0xb0)\n",
403 /* Clear all pending interrupts */
404 regmap_read(sii902x
->regmap
, SII902X_INT_STATUS
, &status
);
405 regmap_write(sii902x
->regmap
, SII902X_INT_STATUS
, status
);
407 if (client
->irq
> 0) {
408 regmap_write(sii902x
->regmap
, SII902X_INT_ENABLE
,
409 SII902X_HOTPLUG_EVENT
);
411 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
413 IRQF_ONESHOT
, dev_name(dev
),
419 sii902x
->bridge
.funcs
= &sii902x_bridge_funcs
;
420 sii902x
->bridge
.of_node
= dev
->of_node
;
421 ret
= drm_bridge_add(&sii902x
->bridge
);
423 dev_err(dev
, "Failed to add drm_bridge\n");
427 i2c_set_clientdata(client
, sii902x
);
432 static int sii902x_remove(struct i2c_client
*client
)
435 struct sii902x
*sii902x
= i2c_get_clientdata(client
);
437 drm_bridge_remove(&sii902x
->bridge
);
442 static const struct of_device_id sii902x_dt_ids
[] = {
443 { .compatible
= "sil,sii9022", },
446 MODULE_DEVICE_TABLE(of
, sii902x_dt_ids
);
448 static const struct i2c_device_id sii902x_i2c_ids
[] = {
452 MODULE_DEVICE_TABLE(i2c
, sii902x_i2c_ids
);
454 static struct i2c_driver sii902x_driver
= {
455 .probe
= sii902x_probe
,
456 .remove
= sii902x_remove
,
459 .of_match_table
= sii902x_dt_ids
,
461 .id_table
= sii902x_i2c_ids
,
463 module_i2c_driver(sii902x_driver
);
465 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
466 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges");
467 MODULE_LICENSE("GPL");