1 // SPDX-License-Identifier: GPL-2.0-only
3 * Chrontel CH7033 Video Encoder Driver
5 * Copyright (C) 2019,2020 Lubomir Rintel
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_bridge.h>
14 #include <drm/drm_edid.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_print.h>
17 #include <drm/drm_probe_helper.h>
19 /* Page 0, Register 0x07 */
25 /* Page 0, Register 0x08 */
27 DRI_PDDRI
= GENMASK(7, 4),
28 PDDAC
= GENMASK(3, 1),
32 /* Page 0, Register 0x09 */
44 /* Page 0, Register 0x0a */
56 /* Page 0, Register 0x18 */
72 /* Page 0, Register 0x19 */
78 GCLKFREQ
= GENMASK(2, 0),
81 /* Page 0, Register 0x2e */
91 /* Page 0, Register 0x2b */
93 SWAPS
= GENMASK(7, 4),
97 /* Page 0, Register 0x54 */
101 HWO_HDMI_HI
= GENMASK(5, 3),
102 HOO_HDMI_HI
= GENMASK(2, 0),
105 /* Page 0, Register 0x57 */
108 VWO_HDMI_HI
= GENMASK(5, 3),
109 VOO_HDMI_HI
= GENMASK(2, 0),
112 /* Page 0, Register 0x7e */
114 HDMI_LVDS_SEL
= BIT(7),
116 PWM_INDEX_HI
= BIT(5),
118 R_INT
= GENMASK(3, 0),
121 /* Page 1, Register 0x07 */
124 DRI_CMFB_EN
= BIT(6),
132 /* Page 1, Register 0x08 */
138 DRI_IT_LVDS
= GENMASK(2, 1),
142 /* Page 1, Register 0x0c */
144 DRI_PLL_CP
= GENMASK(7, 6),
145 DRI_PLL_DIVSEL
= BIT(5),
146 DRI_PLL_N1_1
= BIT(4),
147 DRI_PLL_N1_0
= BIT(3),
148 DRI_PLL_N3_1
= BIT(2),
149 DRI_PLL_N3_0
= BIT(1),
150 DRI_PLL_CKTSTEN
= BIT(0),
153 /* Page 1, Register 0x6b */
155 VCO3CS
= GENMASK(7, 6),
156 ICPGBK2_0
= GENMASK(5, 3),
157 DRI_VCO357SC
= BIT(2),
162 /* Page 1, Register 0x6c */
164 PLL2N11
= GENMASK(7, 4),
171 /* Page 3, Register 0x28 */
173 DIFF_EN
= GENMASK(7, 6),
174 CORREC_EN
= GENMASK(5, 4),
180 /* Page 3, Register 0x2a */
187 THRWL
= GENMASK(2, 0),
190 /* Page 4, Register 0x52 */
200 struct regmap
*regmap
;
201 struct drm_bridge
*next_bridge
;
202 struct drm_bridge bridge
;
203 struct drm_connector connector
;
206 #define conn_to_ch7033_priv(x) \
207 container_of(x, struct ch7033_priv, connector)
208 #define bridge_to_ch7033_priv(x) \
209 container_of(x, struct ch7033_priv, bridge)
212 static enum drm_connector_status
ch7033_connector_detect(
213 struct drm_connector
*connector
, bool force
)
215 struct ch7033_priv
*priv
= conn_to_ch7033_priv(connector
);
217 return drm_bridge_detect(priv
->next_bridge
);
220 static const struct drm_connector_funcs ch7033_connector_funcs
= {
221 .reset
= drm_atomic_helper_connector_reset
,
222 .fill_modes
= drm_helper_probe_single_connector_modes
,
223 .detect
= ch7033_connector_detect
,
224 .destroy
= drm_connector_cleanup
,
225 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
226 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
229 static int ch7033_connector_get_modes(struct drm_connector
*connector
)
231 struct ch7033_priv
*priv
= conn_to_ch7033_priv(connector
);
235 edid
= drm_bridge_get_edid(priv
->next_bridge
, connector
);
236 drm_connector_update_edid_property(connector
, edid
);
238 ret
= drm_add_edid_modes(connector
, edid
);
241 ret
= drm_add_modes_noedid(connector
, 1920, 1080);
242 drm_set_preferred_mode(connector
, 1024, 768);
248 static struct drm_encoder
*ch7033_connector_best_encoder(
249 struct drm_connector
*connector
)
251 struct ch7033_priv
*priv
= conn_to_ch7033_priv(connector
);
253 return priv
->bridge
.encoder
;
256 static const struct drm_connector_helper_funcs ch7033_connector_helper_funcs
= {
257 .get_modes
= ch7033_connector_get_modes
,
258 .best_encoder
= ch7033_connector_best_encoder
,
261 static void ch7033_hpd_event(void *arg
, enum drm_connector_status status
)
263 struct ch7033_priv
*priv
= arg
;
265 if (priv
->bridge
.dev
)
266 drm_helper_hpd_irq_event(priv
->connector
.dev
);
269 static int ch7033_bridge_attach(struct drm_bridge
*bridge
,
270 enum drm_bridge_attach_flags flags
)
272 struct ch7033_priv
*priv
= bridge_to_ch7033_priv(bridge
);
273 struct drm_connector
*connector
= &priv
->connector
;
276 ret
= drm_bridge_attach(bridge
->encoder
, priv
->next_bridge
, bridge
,
277 DRM_BRIDGE_ATTACH_NO_CONNECTOR
);
281 if (flags
& DRM_BRIDGE_ATTACH_NO_CONNECTOR
)
284 if (priv
->next_bridge
->ops
& DRM_BRIDGE_OP_DETECT
) {
285 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
287 connector
->polled
= DRM_CONNECTOR_POLL_CONNECT
|
288 DRM_CONNECTOR_POLL_DISCONNECT
;
291 if (priv
->next_bridge
->ops
& DRM_BRIDGE_OP_HPD
) {
292 drm_bridge_hpd_enable(priv
->next_bridge
, ch7033_hpd_event
,
296 drm_connector_helper_add(connector
,
297 &ch7033_connector_helper_funcs
);
298 ret
= drm_connector_init_with_ddc(bridge
->dev
, &priv
->connector
,
299 &ch7033_connector_funcs
,
300 priv
->next_bridge
->type
,
301 priv
->next_bridge
->ddc
);
303 DRM_ERROR("Failed to initialize connector\n");
307 return drm_connector_attach_encoder(&priv
->connector
, bridge
->encoder
);
310 static void ch7033_bridge_detach(struct drm_bridge
*bridge
)
312 struct ch7033_priv
*priv
= bridge_to_ch7033_priv(bridge
);
314 if (priv
->next_bridge
->ops
& DRM_BRIDGE_OP_HPD
)
315 drm_bridge_hpd_disable(priv
->next_bridge
);
316 drm_connector_cleanup(&priv
->connector
);
319 static enum drm_mode_status
ch7033_bridge_mode_valid(struct drm_bridge
*bridge
,
320 const struct drm_display_info
*info
,
321 const struct drm_display_mode
*mode
)
323 if (mode
->clock
> 165000)
324 return MODE_CLOCK_HIGH
;
325 if (mode
->hdisplay
>= 1920)
326 return MODE_BAD_HVALUE
;
327 if (mode
->vdisplay
>= 1080)
328 return MODE_BAD_VVALUE
;
332 static void ch7033_bridge_disable(struct drm_bridge
*bridge
)
334 struct ch7033_priv
*priv
= bridge_to_ch7033_priv(bridge
);
336 regmap_write(priv
->regmap
, 0x03, 0x04);
337 regmap_update_bits(priv
->regmap
, 0x52, RESETDB
, 0x00);
340 static void ch7033_bridge_enable(struct drm_bridge
*bridge
)
342 struct ch7033_priv
*priv
= bridge_to_ch7033_priv(bridge
);
344 regmap_write(priv
->regmap
, 0x03, 0x04);
345 regmap_update_bits(priv
->regmap
, 0x52, RESETDB
, RESETDB
);
348 static void ch7033_bridge_mode_set(struct drm_bridge
*bridge
,
349 const struct drm_display_mode
*mode
,
350 const struct drm_display_mode
*adjusted_mode
)
352 struct ch7033_priv
*priv
= bridge_to_ch7033_priv(bridge
);
353 int hbporch
= mode
->hsync_start
- mode
->hdisplay
;
354 int hsynclen
= mode
->hsync_end
- mode
->hsync_start
;
355 int vbporch
= mode
->vsync_start
- mode
->vdisplay
;
356 int vsynclen
= mode
->vsync_end
- mode
->vsync_start
;
361 regmap_write(priv
->regmap
, 0x03, 0x04);
363 /* Turn everything off to set all the registers to their defaults. */
364 regmap_write(priv
->regmap
, 0x52, 0x00);
365 /* Bring I/O block up. */
366 regmap_write(priv
->regmap
, 0x52, RESETIB
);
371 regmap_write(priv
->regmap
, 0x03, 0x00);
373 /* Bring up parts we need from the power down. */
374 regmap_update_bits(priv
->regmap
, 0x07, DRI_PD
| IO_PD
, 0);
375 regmap_update_bits(priv
->regmap
, 0x08, DRI_PDDRI
| PDDAC
| PANEN
, 0);
376 regmap_update_bits(priv
->regmap
, 0x09, DPD
| GCKOFF
|
377 HDMI_PD
| VGA_PD
, 0);
378 regmap_update_bits(priv
->regmap
, 0x0a, HD_DVIB
, 0);
380 /* Horizontal input timing. */
381 regmap_write(priv
->regmap
, 0x0b, (mode
->htotal
>> 8) << 3 |
382 (mode
->hdisplay
>> 8));
383 regmap_write(priv
->regmap
, 0x0c, mode
->hdisplay
);
384 regmap_write(priv
->regmap
, 0x0d, mode
->htotal
);
385 regmap_write(priv
->regmap
, 0x0e, (hsynclen
>> 8) << 3 |
387 regmap_write(priv
->regmap
, 0x0f, hbporch
);
388 regmap_write(priv
->regmap
, 0x10, hsynclen
);
390 /* Vertical input timing. */
391 regmap_write(priv
->regmap
, 0x11, (mode
->vtotal
>> 8) << 3 |
392 (mode
->vdisplay
>> 8));
393 regmap_write(priv
->regmap
, 0x12, mode
->vdisplay
);
394 regmap_write(priv
->regmap
, 0x13, mode
->vtotal
);
395 regmap_write(priv
->regmap
, 0x14, ((vsynclen
>> 8) << 3) |
397 regmap_write(priv
->regmap
, 0x15, vbporch
);
398 regmap_write(priv
->regmap
, 0x16, vsynclen
);
400 /* Input color swap. */
401 regmap_update_bits(priv
->regmap
, 0x18, SWAP
, BYTE_SWAP_BGR
);
403 /* Input clock and sync polarity. */
404 regmap_update_bits(priv
->regmap
, 0x19, 0x1, mode
->clock
>> 16);
405 regmap_update_bits(priv
->regmap
, 0x19, HPO_I
| VPO_I
| GCLKFREQ
,
406 (mode
->flags
& DRM_MODE_FLAG_PHSYNC
) ? HPO_I
: 0 |
407 (mode
->flags
& DRM_MODE_FLAG_PVSYNC
) ? VPO_I
: 0 |
409 regmap_write(priv
->regmap
, 0x1a, mode
->clock
>> 8);
410 regmap_write(priv
->regmap
, 0x1b, mode
->clock
);
412 /* Horizontal output timing. */
413 regmap_write(priv
->regmap
, 0x1f, (mode
->htotal
>> 8) << 3 |
414 (mode
->hdisplay
>> 8));
415 regmap_write(priv
->regmap
, 0x20, mode
->hdisplay
);
416 regmap_write(priv
->regmap
, 0x21, mode
->htotal
);
418 /* Vertical output timing. */
419 regmap_write(priv
->regmap
, 0x25, (mode
->vtotal
>> 8) << 3 |
420 (mode
->vdisplay
>> 8));
421 regmap_write(priv
->regmap
, 0x26, mode
->vdisplay
);
422 regmap_write(priv
->regmap
, 0x27, mode
->vtotal
);
424 /* VGA channel bypass */
425 regmap_update_bits(priv
->regmap
, 0x2b, VFMT
, 9);
427 /* Output sync polarity. */
428 regmap_update_bits(priv
->regmap
, 0x2e, HPO_O
| VPO_O
,
429 (mode
->flags
& DRM_MODE_FLAG_PHSYNC
) ? HPO_O
: 0 |
430 (mode
->flags
& DRM_MODE_FLAG_PVSYNC
) ? VPO_O
: 0);
432 /* HDMI horizontal output timing. */
433 regmap_update_bits(priv
->regmap
, 0x54, HWO_HDMI_HI
| HOO_HDMI_HI
,
434 (hsynclen
>> 8) << 3 |
436 regmap_write(priv
->regmap
, 0x55, hbporch
);
437 regmap_write(priv
->regmap
, 0x56, hsynclen
);
439 /* HDMI vertical output timing. */
440 regmap_update_bits(priv
->regmap
, 0x57, VWO_HDMI_HI
| VOO_HDMI_HI
,
441 (vsynclen
>> 8) << 3 |
443 regmap_write(priv
->regmap
, 0x58, vbporch
);
444 regmap_write(priv
->regmap
, 0x59, vsynclen
);
446 /* Pick HDMI, not LVDS. */
447 regmap_update_bits(priv
->regmap
, 0x7e, HDMI_LVDS_SEL
, HDMI_LVDS_SEL
);
452 regmap_write(priv
->regmap
, 0x03, 0x01);
454 /* No idea what these do, but VGA is wobbly and blinky without them. */
455 regmap_update_bits(priv
->regmap
, 0x07, CKINV
, CKINV
);
456 regmap_update_bits(priv
->regmap
, 0x08, DISPON
, DISPON
);
459 regmap_update_bits(priv
->regmap
, 0x0c, DRI_PLL_DIVSEL
, DRI_PLL_DIVSEL
);
460 if (mode
->clock
<= 40000) {
461 regmap_update_bits(priv
->regmap
, 0x0c, DRI_PLL_N1_1
|
466 } else if (mode
->clock
< 80000) {
467 regmap_update_bits(priv
->regmap
, 0x0c, DRI_PLL_N1_1
|
474 regmap_update_bits(priv
->regmap
, 0x0c, DRI_PLL_N1_1
|
482 /* This seems to be color calibration for VGA. */
483 regmap_write(priv
->regmap
, 0x64, 0x29); /* LSB Blue */
484 regmap_write(priv
->regmap
, 0x65, 0x29); /* LSB Green */
485 regmap_write(priv
->regmap
, 0x66, 0x29); /* LSB Red */
486 regmap_write(priv
->regmap
, 0x67, 0x00); /* MSB Blue */
487 regmap_write(priv
->regmap
, 0x68, 0x00); /* MSB Green */
488 regmap_write(priv
->regmap
, 0x69, 0x00); /* MSB Red */
490 regmap_update_bits(priv
->regmap
, 0x6b, DRI_PD_SER
, 0x00);
491 regmap_update_bits(priv
->regmap
, 0x6c, DRI_PLL_PD
, 0x00);
496 regmap_write(priv
->regmap
, 0x03, 0x03);
498 /* More bypasses and apparently another HDMI/LVDS selector. */
499 regmap_update_bits(priv
->regmap
, 0x28, VGACLK_BP
| HM_LV_SEL
,
500 VGACLK_BP
| HM_LV_SEL
);
501 regmap_update_bits(priv
->regmap
, 0x2a, HDMICLK_BP
| HDMI_BP
,
502 HDMICLK_BP
| HDMI_BP
);
507 regmap_write(priv
->regmap
, 0x03, 0x04);
510 regmap_write(priv
->regmap
, 0x10, mode
->clock
>> 16);
511 regmap_write(priv
->regmap
, 0x11, mode
->clock
>> 8);
512 regmap_write(priv
->regmap
, 0x12, mode
->clock
);
515 static const struct drm_bridge_funcs ch7033_bridge_funcs
= {
516 .attach
= ch7033_bridge_attach
,
517 .detach
= ch7033_bridge_detach
,
518 .mode_valid
= ch7033_bridge_mode_valid
,
519 .disable
= ch7033_bridge_disable
,
520 .enable
= ch7033_bridge_enable
,
521 .mode_set
= ch7033_bridge_mode_set
,
524 static const struct regmap_config ch7033_regmap_config
= {
527 .max_register
= 0x7f,
530 static int ch7033_probe(struct i2c_client
*client
,
531 const struct i2c_device_id
*id
)
533 struct device
*dev
= &client
->dev
;
534 struct ch7033_priv
*priv
;
538 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
542 dev_set_drvdata(dev
, priv
);
544 ret
= drm_of_find_panel_or_bridge(dev
->of_node
, 1, -1, NULL
,
549 priv
->regmap
= devm_regmap_init_i2c(client
, &ch7033_regmap_config
);
550 if (IS_ERR(priv
->regmap
)) {
551 dev_err(&client
->dev
, "regmap init failed\n");
552 return PTR_ERR(priv
->regmap
);
555 ret
= regmap_read(priv
->regmap
, 0x00, &val
);
557 dev_err(&client
->dev
, "error reading the model id: %d\n", ret
);
560 if ((val
& 0xf7) != 0x56) {
561 dev_err(&client
->dev
, "the device is not a ch7033\n");
565 regmap_write(priv
->regmap
, 0x03, 0x04);
566 ret
= regmap_read(priv
->regmap
, 0x51, &val
);
568 dev_err(&client
->dev
, "error reading the model id: %d\n", ret
);
571 if ((val
& 0x0f) != 3) {
572 dev_err(&client
->dev
, "unknown revision %u\n", val
);
576 INIT_LIST_HEAD(&priv
->bridge
.list
);
577 priv
->bridge
.funcs
= &ch7033_bridge_funcs
;
578 priv
->bridge
.of_node
= dev
->of_node
;
579 drm_bridge_add(&priv
->bridge
);
581 dev_info(dev
, "Chrontel CH7033 Video Encoder\n");
585 static int ch7033_remove(struct i2c_client
*client
)
587 struct device
*dev
= &client
->dev
;
588 struct ch7033_priv
*priv
= dev_get_drvdata(dev
);
590 drm_bridge_remove(&priv
->bridge
);
595 static const struct of_device_id ch7033_dt_ids
[] = {
596 { .compatible
= "chrontel,ch7033", },
599 MODULE_DEVICE_TABLE(of
, ch7033_dt_ids
);
601 static const struct i2c_device_id ch7033_ids
[] = {
605 MODULE_DEVICE_TABLE(i2c
, ch7033_ids
);
607 static struct i2c_driver ch7033_driver
= {
608 .probe
= ch7033_probe
,
609 .remove
= ch7033_remove
,
612 .of_match_table
= of_match_ptr(ch7033_dt_ids
),
614 .id_table
= ch7033_ids
,
617 module_i2c_driver(ch7033_driver
);
619 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
620 MODULE_DESCRIPTION("Chrontel CH7033 Video Encoder Driver");
621 MODULE_LICENSE("GPL v2");