1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
4 * Copyright 2017 Linaro Ltd.
8 #include <linux/component.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_simple_kms_helper.h>
19 #include "zx_drm_drv.h"
20 #include "zx_vga_regs.h"
23 struct zx_vga_pwrctrl
{
24 struct regmap
*regmap
;
30 struct i2c_adapter adap
;
35 struct drm_connector connector
;
36 struct drm_encoder encoder
;
37 struct zx_vga_i2c
*ddc
;
41 struct zx_vga_pwrctrl pwrctrl
;
42 struct completion complete
;
46 #define to_zx_vga(x) container_of(x, struct zx_vga, x)
48 static void zx_vga_encoder_enable(struct drm_encoder
*encoder
)
50 struct zx_vga
*vga
= to_zx_vga(encoder
);
51 struct zx_vga_pwrctrl
*pwrctrl
= &vga
->pwrctrl
;
53 /* Set bit to power up VGA DACs */
54 regmap_update_bits(pwrctrl
->regmap
, pwrctrl
->reg
, pwrctrl
->mask
,
57 vou_inf_enable(VOU_VGA
, encoder
->crtc
);
60 static void zx_vga_encoder_disable(struct drm_encoder
*encoder
)
62 struct zx_vga
*vga
= to_zx_vga(encoder
);
63 struct zx_vga_pwrctrl
*pwrctrl
= &vga
->pwrctrl
;
65 vou_inf_disable(VOU_VGA
, encoder
->crtc
);
67 /* Clear bit to power down VGA DACs */
68 regmap_update_bits(pwrctrl
->regmap
, pwrctrl
->reg
, pwrctrl
->mask
, 0);
71 static const struct drm_encoder_helper_funcs zx_vga_encoder_helper_funcs
= {
72 .enable
= zx_vga_encoder_enable
,
73 .disable
= zx_vga_encoder_disable
,
76 static int zx_vga_connector_get_modes(struct drm_connector
*connector
)
78 struct zx_vga
*vga
= to_zx_vga(connector
);
83 * Clear both detection bits to switch I2C bus from device
84 * detecting to EDID reading.
86 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
, 0);
88 edid
= drm_get_edid(connector
, &vga
->ddc
->adap
);
91 * If EDID reading fails, we set the device state into
92 * disconnected. Locking is not required here, since the
93 * VGA_AUTO_DETECT_SEL register write in irq handler cannot
94 * be triggered when both detection bits are cleared as above.
96 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
,
97 VGA_DETECT_SEL_NO_DEVICE
);
98 vga
->connected
= false;
103 * As edid reading succeeds, device must be connected, so we set
104 * up detection bit for unplug interrupt here.
106 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
, VGA_DETECT_SEL_HAS_DEVICE
);
108 drm_connector_update_edid_property(connector
, edid
);
109 ret
= drm_add_edid_modes(connector
, edid
);
115 static enum drm_mode_status
116 zx_vga_connector_mode_valid(struct drm_connector
*connector
,
117 struct drm_display_mode
*mode
)
122 static struct drm_connector_helper_funcs zx_vga_connector_helper_funcs
= {
123 .get_modes
= zx_vga_connector_get_modes
,
124 .mode_valid
= zx_vga_connector_mode_valid
,
127 static enum drm_connector_status
128 zx_vga_connector_detect(struct drm_connector
*connector
, bool force
)
130 struct zx_vga
*vga
= to_zx_vga(connector
);
132 return vga
->connected
? connector_status_connected
:
133 connector_status_disconnected
;
136 static const struct drm_connector_funcs zx_vga_connector_funcs
= {
137 .fill_modes
= drm_helper_probe_single_connector_modes
,
138 .detect
= zx_vga_connector_detect
,
139 .destroy
= drm_connector_cleanup
,
140 .reset
= drm_atomic_helper_connector_reset
,
141 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
142 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
145 static int zx_vga_register(struct drm_device
*drm
, struct zx_vga
*vga
)
147 struct drm_encoder
*encoder
= &vga
->encoder
;
148 struct drm_connector
*connector
= &vga
->connector
;
149 struct device
*dev
= vga
->dev
;
152 encoder
->possible_crtcs
= VOU_CRTC_MASK
;
154 ret
= drm_simple_encoder_init(drm
, encoder
, DRM_MODE_ENCODER_DAC
);
156 DRM_DEV_ERROR(dev
, "failed to init encoder: %d\n", ret
);
160 drm_encoder_helper_add(encoder
, &zx_vga_encoder_helper_funcs
);
162 vga
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
164 ret
= drm_connector_init_with_ddc(drm
, connector
,
165 &zx_vga_connector_funcs
,
166 DRM_MODE_CONNECTOR_VGA
,
169 DRM_DEV_ERROR(dev
, "failed to init connector: %d\n", ret
);
173 drm_connector_helper_add(connector
, &zx_vga_connector_helper_funcs
);
175 ret
= drm_connector_attach_encoder(connector
, encoder
);
177 DRM_DEV_ERROR(dev
, "failed to attach encoder: %d\n", ret
);
178 goto clean_connector
;
184 drm_connector_cleanup(connector
);
186 drm_encoder_cleanup(encoder
);
190 static int zx_vga_pwrctrl_init(struct zx_vga
*vga
)
192 struct zx_vga_pwrctrl
*pwrctrl
= &vga
->pwrctrl
;
193 struct device
*dev
= vga
->dev
;
194 struct of_phandle_args out_args
;
195 struct regmap
*regmap
;
198 ret
= of_parse_phandle_with_fixed_args(dev
->of_node
,
199 "zte,vga-power-control", 2, 0, &out_args
);
203 regmap
= syscon_node_to_regmap(out_args
.np
);
204 if (IS_ERR(regmap
)) {
205 ret
= PTR_ERR(regmap
);
209 pwrctrl
->regmap
= regmap
;
210 pwrctrl
->reg
= out_args
.args
[0];
211 pwrctrl
->mask
= out_args
.args
[1];
214 of_node_put(out_args
.np
);
218 static int zx_vga_i2c_read(struct zx_vga
*vga
, struct i2c_msg
*msg
)
225 reinit_completion(&vga
->complete
);
227 /* Select combo write */
228 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_COMBO
, VGA_CMD_COMBO
);
229 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_RW
, 0);
235 zx_writel_mask(vga
->mmio
+ VGA_RXF_CTRL
, VGA_RX_FIFO_CLEAR
,
238 /* Data offset to read from */
239 zx_writel(vga
->mmio
+ VGA_SUB_ADDR
, offset
);
241 /* Kick off the transfer */
242 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_TRANS
,
245 if (!wait_for_completion_timeout(&vga
->complete
,
246 msecs_to_jiffies(1000))) {
247 DRM_DEV_ERROR(vga
->dev
, "transfer timeout\n");
251 cnt
= zx_readl(vga
->mmio
+ VGA_RXF_STATUS
);
252 cnt
= (cnt
& VGA_RXF_COUNT_MASK
) >> VGA_RXF_COUNT_SHIFT
;
253 /* FIFO status may report more data than we need to read */
254 cnt
= min_t(u32
, len
, cnt
);
256 for (i
= 0; i
< cnt
; i
++)
257 *buf
++ = zx_readl(vga
->mmio
+ VGA_DATA
);
266 static int zx_vga_i2c_write(struct zx_vga
*vga
, struct i2c_msg
*msg
)
269 * The DDC I2C adapter is only for reading EDID data, so we assume
270 * that the write to this adapter must be the EDID data offset.
272 if ((msg
->len
!= 1) || ((msg
->addr
!= DDC_ADDR
)))
275 /* Hardware will take care of the slave address shifting */
276 zx_writel(vga
->mmio
+ VGA_DEVICE_ADDR
, msg
->addr
);
281 static int zx_vga_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
284 struct zx_vga
*vga
= i2c_get_adapdata(adap
);
285 struct zx_vga_i2c
*ddc
= vga
->ddc
;
289 mutex_lock(&ddc
->lock
);
291 for (i
= 0; i
< num
; i
++) {
292 if (msgs
[i
].flags
& I2C_M_RD
)
293 ret
= zx_vga_i2c_read(vga
, &msgs
[i
]);
295 ret
= zx_vga_i2c_write(vga
, &msgs
[i
]);
304 mutex_unlock(&ddc
->lock
);
309 static u32
zx_vga_i2c_func(struct i2c_adapter
*adapter
)
311 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
314 static const struct i2c_algorithm zx_vga_algorithm
= {
315 .master_xfer
= zx_vga_i2c_xfer
,
316 .functionality
= zx_vga_i2c_func
,
319 static int zx_vga_ddc_register(struct zx_vga
*vga
)
321 struct device
*dev
= vga
->dev
;
322 struct i2c_adapter
*adap
;
323 struct zx_vga_i2c
*ddc
;
326 ddc
= devm_kzalloc(dev
, sizeof(*ddc
), GFP_KERNEL
);
331 mutex_init(&ddc
->lock
);
334 adap
->owner
= THIS_MODULE
;
335 adap
->class = I2C_CLASS_DDC
;
336 adap
->dev
.parent
= dev
;
337 adap
->algo
= &zx_vga_algorithm
;
338 snprintf(adap
->name
, sizeof(adap
->name
), "zx vga i2c");
340 ret
= i2c_add_adapter(adap
);
342 DRM_DEV_ERROR(dev
, "failed to add I2C adapter: %d\n", ret
);
346 i2c_set_adapdata(adap
, vga
);
351 static irqreturn_t
zx_vga_irq_thread(int irq
, void *dev_id
)
353 struct zx_vga
*vga
= dev_id
;
355 drm_helper_hpd_irq_event(vga
->connector
.dev
);
360 static irqreturn_t
zx_vga_irq_handler(int irq
, void *dev_id
)
362 struct zx_vga
*vga
= dev_id
;
365 status
= zx_readl(vga
->mmio
+ VGA_I2C_STATUS
);
367 /* Clear interrupt status */
368 zx_writel_mask(vga
->mmio
+ VGA_I2C_STATUS
, VGA_CLEAR_IRQ
,
371 if (status
& VGA_DEVICE_CONNECTED
) {
373 * Since VGA_DETECT_SEL bits need to be reset for switching DDC
374 * bus from device detection to EDID read, rather than setting
375 * up HAS_DEVICE bit here, we need to do that in .get_modes
376 * hook for unplug detecting after EDID read succeeds.
378 vga
->connected
= true;
379 return IRQ_WAKE_THREAD
;
382 if (status
& VGA_DEVICE_DISCONNECTED
) {
383 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
,
384 VGA_DETECT_SEL_NO_DEVICE
);
385 vga
->connected
= false;
386 return IRQ_WAKE_THREAD
;
389 if (status
& VGA_TRANS_DONE
) {
390 complete(&vga
->complete
);
397 static void zx_vga_hw_init(struct zx_vga
*vga
)
399 unsigned long ref
= clk_get_rate(vga
->i2c_wclk
);
403 * Set up I2C fast speed divider per formula below to get 400kHz.
404 * scl = ref / ((div + 1) * 4)
406 div
= DIV_ROUND_UP(ref
/ 1000, 400 * 4) - 1;
407 zx_writel(vga
->mmio
+ VGA_CLK_DIV_FS
, div
);
409 /* Set up device detection */
410 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_PARA
, 0x80);
411 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
, VGA_DETECT_SEL_NO_DEVICE
);
414 * We need to poke monitor via DDC bus to get connection irq
417 zx_writel(vga
->mmio
+ VGA_DEVICE_ADDR
, DDC_ADDR
);
418 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_TRANS
, VGA_CMD_TRANS
);
421 static int zx_vga_bind(struct device
*dev
, struct device
*master
, void *data
)
423 struct platform_device
*pdev
= to_platform_device(dev
);
424 struct drm_device
*drm
= data
;
425 struct resource
*res
;
430 vga
= devm_kzalloc(dev
, sizeof(*vga
), GFP_KERNEL
);
435 dev_set_drvdata(dev
, vga
);
437 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
438 vga
->mmio
= devm_ioremap_resource(dev
, res
);
439 if (IS_ERR(vga
->mmio
))
440 return PTR_ERR(vga
->mmio
);
442 irq
= platform_get_irq(pdev
, 0);
446 vga
->i2c_wclk
= devm_clk_get(dev
, "i2c_wclk");
447 if (IS_ERR(vga
->i2c_wclk
)) {
448 ret
= PTR_ERR(vga
->i2c_wclk
);
449 DRM_DEV_ERROR(dev
, "failed to get i2c_wclk: %d\n", ret
);
453 ret
= zx_vga_pwrctrl_init(vga
);
455 DRM_DEV_ERROR(dev
, "failed to init power control: %d\n", ret
);
459 ret
= zx_vga_ddc_register(vga
);
461 DRM_DEV_ERROR(dev
, "failed to register ddc: %d\n", ret
);
465 ret
= zx_vga_register(drm
, vga
);
467 DRM_DEV_ERROR(dev
, "failed to register vga: %d\n", ret
);
471 init_completion(&vga
->complete
);
473 ret
= devm_request_threaded_irq(dev
, irq
, zx_vga_irq_handler
,
474 zx_vga_irq_thread
, IRQF_SHARED
,
477 DRM_DEV_ERROR(dev
, "failed to request threaded irq: %d\n", ret
);
481 ret
= clk_prepare_enable(vga
->i2c_wclk
);
490 static void zx_vga_unbind(struct device
*dev
, struct device
*master
,
493 struct zx_vga
*vga
= dev_get_drvdata(dev
);
495 clk_disable_unprepare(vga
->i2c_wclk
);
498 static const struct component_ops zx_vga_component_ops
= {
500 .unbind
= zx_vga_unbind
,
503 static int zx_vga_probe(struct platform_device
*pdev
)
505 return component_add(&pdev
->dev
, &zx_vga_component_ops
);
508 static int zx_vga_remove(struct platform_device
*pdev
)
510 component_del(&pdev
->dev
, &zx_vga_component_ops
);
514 static const struct of_device_id zx_vga_of_match
[] = {
515 { .compatible
= "zte,zx296718-vga", },
518 MODULE_DEVICE_TABLE(of
, zx_vga_of_match
);
520 struct platform_driver zx_vga_driver
= {
521 .probe
= zx_vga_probe
,
522 .remove
= zx_vga_remove
,
525 .of_match_table
= zx_vga_of_match
,