2 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
3 * Copyright 2017 Linaro Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/component.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/regmap.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_probe_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 const struct drm_encoder_funcs zx_vga_encoder_funcs
= {
77 .destroy
= drm_encoder_cleanup
,
80 static int zx_vga_connector_get_modes(struct drm_connector
*connector
)
82 struct zx_vga
*vga
= to_zx_vga(connector
);
87 * Clear both detection bits to switch I2C bus from device
88 * detecting to EDID reading.
90 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
, 0);
92 edid
= drm_get_edid(connector
, &vga
->ddc
->adap
);
95 * If EDID reading fails, we set the device state into
96 * disconnected. Locking is not required here, since the
97 * VGA_AUTO_DETECT_SEL register write in irq handler cannot
98 * be triggered when both detection bits are cleared as above.
100 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
,
101 VGA_DETECT_SEL_NO_DEVICE
);
102 vga
->connected
= false;
107 * As edid reading succeeds, device must be connected, so we set
108 * up detection bit for unplug interrupt here.
110 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
, VGA_DETECT_SEL_HAS_DEVICE
);
112 drm_connector_update_edid_property(connector
, edid
);
113 ret
= drm_add_edid_modes(connector
, edid
);
119 static enum drm_mode_status
120 zx_vga_connector_mode_valid(struct drm_connector
*connector
,
121 struct drm_display_mode
*mode
)
126 static struct drm_connector_helper_funcs zx_vga_connector_helper_funcs
= {
127 .get_modes
= zx_vga_connector_get_modes
,
128 .mode_valid
= zx_vga_connector_mode_valid
,
131 static enum drm_connector_status
132 zx_vga_connector_detect(struct drm_connector
*connector
, bool force
)
134 struct zx_vga
*vga
= to_zx_vga(connector
);
136 return vga
->connected
? connector_status_connected
:
137 connector_status_disconnected
;
140 static const struct drm_connector_funcs zx_vga_connector_funcs
= {
141 .fill_modes
= drm_helper_probe_single_connector_modes
,
142 .detect
= zx_vga_connector_detect
,
143 .destroy
= drm_connector_cleanup
,
144 .reset
= drm_atomic_helper_connector_reset
,
145 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
146 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
149 static int zx_vga_register(struct drm_device
*drm
, struct zx_vga
*vga
)
151 struct drm_encoder
*encoder
= &vga
->encoder
;
152 struct drm_connector
*connector
= &vga
->connector
;
153 struct device
*dev
= vga
->dev
;
156 encoder
->possible_crtcs
= VOU_CRTC_MASK
;
158 ret
= drm_encoder_init(drm
, encoder
, &zx_vga_encoder_funcs
,
159 DRM_MODE_ENCODER_DAC
, NULL
);
161 DRM_DEV_ERROR(dev
, "failed to init encoder: %d\n", ret
);
165 drm_encoder_helper_add(encoder
, &zx_vga_encoder_helper_funcs
);
167 vga
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
169 ret
= drm_connector_init(drm
, connector
, &zx_vga_connector_funcs
,
170 DRM_MODE_CONNECTOR_VGA
);
172 DRM_DEV_ERROR(dev
, "failed to init connector: %d\n", ret
);
176 drm_connector_helper_add(connector
, &zx_vga_connector_helper_funcs
);
178 ret
= drm_connector_attach_encoder(connector
, encoder
);
180 DRM_DEV_ERROR(dev
, "failed to attach encoder: %d\n", ret
);
181 goto clean_connector
;
187 drm_connector_cleanup(connector
);
189 drm_encoder_cleanup(encoder
);
193 static int zx_vga_pwrctrl_init(struct zx_vga
*vga
)
195 struct zx_vga_pwrctrl
*pwrctrl
= &vga
->pwrctrl
;
196 struct device
*dev
= vga
->dev
;
197 struct of_phandle_args out_args
;
198 struct regmap
*regmap
;
201 ret
= of_parse_phandle_with_fixed_args(dev
->of_node
,
202 "zte,vga-power-control", 2, 0, &out_args
);
206 regmap
= syscon_node_to_regmap(out_args
.np
);
207 if (IS_ERR(regmap
)) {
208 ret
= PTR_ERR(regmap
);
212 pwrctrl
->regmap
= regmap
;
213 pwrctrl
->reg
= out_args
.args
[0];
214 pwrctrl
->mask
= out_args
.args
[1];
217 of_node_put(out_args
.np
);
221 static int zx_vga_i2c_read(struct zx_vga
*vga
, struct i2c_msg
*msg
)
228 reinit_completion(&vga
->complete
);
230 /* Select combo write */
231 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_COMBO
, VGA_CMD_COMBO
);
232 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_RW
, 0);
238 zx_writel_mask(vga
->mmio
+ VGA_RXF_CTRL
, VGA_RX_FIFO_CLEAR
,
241 /* Data offset to read from */
242 zx_writel(vga
->mmio
+ VGA_SUB_ADDR
, offset
);
244 /* Kick off the transfer */
245 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_TRANS
,
248 if (!wait_for_completion_timeout(&vga
->complete
,
249 msecs_to_jiffies(1000))) {
250 DRM_DEV_ERROR(vga
->dev
, "transfer timeout\n");
254 cnt
= zx_readl(vga
->mmio
+ VGA_RXF_STATUS
);
255 cnt
= (cnt
& VGA_RXF_COUNT_MASK
) >> VGA_RXF_COUNT_SHIFT
;
256 /* FIFO status may report more data than we need to read */
257 cnt
= min_t(u32
, len
, cnt
);
259 for (i
= 0; i
< cnt
; i
++)
260 *buf
++ = zx_readl(vga
->mmio
+ VGA_DATA
);
269 static int zx_vga_i2c_write(struct zx_vga
*vga
, struct i2c_msg
*msg
)
272 * The DDC I2C adapter is only for reading EDID data, so we assume
273 * that the write to this adapter must be the EDID data offset.
275 if ((msg
->len
!= 1) || ((msg
->addr
!= DDC_ADDR
)))
278 /* Hardware will take care of the slave address shifting */
279 zx_writel(vga
->mmio
+ VGA_DEVICE_ADDR
, msg
->addr
);
284 static int zx_vga_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
287 struct zx_vga
*vga
= i2c_get_adapdata(adap
);
288 struct zx_vga_i2c
*ddc
= vga
->ddc
;
292 mutex_lock(&ddc
->lock
);
294 for (i
= 0; i
< num
; i
++) {
295 if (msgs
[i
].flags
& I2C_M_RD
)
296 ret
= zx_vga_i2c_read(vga
, &msgs
[i
]);
298 ret
= zx_vga_i2c_write(vga
, &msgs
[i
]);
307 mutex_unlock(&ddc
->lock
);
312 static u32
zx_vga_i2c_func(struct i2c_adapter
*adapter
)
314 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
317 static const struct i2c_algorithm zx_vga_algorithm
= {
318 .master_xfer
= zx_vga_i2c_xfer
,
319 .functionality
= zx_vga_i2c_func
,
322 static int zx_vga_ddc_register(struct zx_vga
*vga
)
324 struct device
*dev
= vga
->dev
;
325 struct i2c_adapter
*adap
;
326 struct zx_vga_i2c
*ddc
;
329 ddc
= devm_kzalloc(dev
, sizeof(*ddc
), GFP_KERNEL
);
334 mutex_init(&ddc
->lock
);
337 adap
->owner
= THIS_MODULE
;
338 adap
->class = I2C_CLASS_DDC
;
339 adap
->dev
.parent
= dev
;
340 adap
->algo
= &zx_vga_algorithm
;
341 snprintf(adap
->name
, sizeof(adap
->name
), "zx vga i2c");
343 ret
= i2c_add_adapter(adap
);
345 DRM_DEV_ERROR(dev
, "failed to add I2C adapter: %d\n", ret
);
349 i2c_set_adapdata(adap
, vga
);
354 static irqreturn_t
zx_vga_irq_thread(int irq
, void *dev_id
)
356 struct zx_vga
*vga
= dev_id
;
358 drm_helper_hpd_irq_event(vga
->connector
.dev
);
363 static irqreturn_t
zx_vga_irq_handler(int irq
, void *dev_id
)
365 struct zx_vga
*vga
= dev_id
;
368 status
= zx_readl(vga
->mmio
+ VGA_I2C_STATUS
);
370 /* Clear interrupt status */
371 zx_writel_mask(vga
->mmio
+ VGA_I2C_STATUS
, VGA_CLEAR_IRQ
,
374 if (status
& VGA_DEVICE_CONNECTED
) {
376 * Since VGA_DETECT_SEL bits need to be reset for switching DDC
377 * bus from device detection to EDID read, rather than setting
378 * up HAS_DEVICE bit here, we need to do that in .get_modes
379 * hook for unplug detecting after EDID read succeeds.
381 vga
->connected
= true;
382 return IRQ_WAKE_THREAD
;
385 if (status
& VGA_DEVICE_DISCONNECTED
) {
386 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
,
387 VGA_DETECT_SEL_NO_DEVICE
);
388 vga
->connected
= false;
389 return IRQ_WAKE_THREAD
;
392 if (status
& VGA_TRANS_DONE
) {
393 complete(&vga
->complete
);
400 static void zx_vga_hw_init(struct zx_vga
*vga
)
402 unsigned long ref
= clk_get_rate(vga
->i2c_wclk
);
406 * Set up I2C fast speed divider per formula below to get 400kHz.
407 * scl = ref / ((div + 1) * 4)
409 div
= DIV_ROUND_UP(ref
/ 1000, 400 * 4) - 1;
410 zx_writel(vga
->mmio
+ VGA_CLK_DIV_FS
, div
);
412 /* Set up device detection */
413 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_PARA
, 0x80);
414 zx_writel(vga
->mmio
+ VGA_AUTO_DETECT_SEL
, VGA_DETECT_SEL_NO_DEVICE
);
417 * We need to poke monitor via DDC bus to get connection irq
420 zx_writel(vga
->mmio
+ VGA_DEVICE_ADDR
, DDC_ADDR
);
421 zx_writel_mask(vga
->mmio
+ VGA_CMD_CFG
, VGA_CMD_TRANS
, VGA_CMD_TRANS
);
424 static int zx_vga_bind(struct device
*dev
, struct device
*master
, void *data
)
426 struct platform_device
*pdev
= to_platform_device(dev
);
427 struct drm_device
*drm
= data
;
428 struct resource
*res
;
433 vga
= devm_kzalloc(dev
, sizeof(*vga
), GFP_KERNEL
);
438 dev_set_drvdata(dev
, vga
);
440 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
441 vga
->mmio
= devm_ioremap_resource(dev
, res
);
442 if (IS_ERR(vga
->mmio
))
443 return PTR_ERR(vga
->mmio
);
445 irq
= platform_get_irq(pdev
, 0);
449 vga
->i2c_wclk
= devm_clk_get(dev
, "i2c_wclk");
450 if (IS_ERR(vga
->i2c_wclk
)) {
451 ret
= PTR_ERR(vga
->i2c_wclk
);
452 DRM_DEV_ERROR(dev
, "failed to get i2c_wclk: %d\n", ret
);
456 ret
= zx_vga_pwrctrl_init(vga
);
458 DRM_DEV_ERROR(dev
, "failed to init power control: %d\n", ret
);
462 ret
= zx_vga_ddc_register(vga
);
464 DRM_DEV_ERROR(dev
, "failed to register ddc: %d\n", ret
);
468 ret
= zx_vga_register(drm
, vga
);
470 DRM_DEV_ERROR(dev
, "failed to register vga: %d\n", ret
);
474 init_completion(&vga
->complete
);
476 ret
= devm_request_threaded_irq(dev
, irq
, zx_vga_irq_handler
,
477 zx_vga_irq_thread
, IRQF_SHARED
,
480 DRM_DEV_ERROR(dev
, "failed to request threaded irq: %d\n", ret
);
484 ret
= clk_prepare_enable(vga
->i2c_wclk
);
493 static void zx_vga_unbind(struct device
*dev
, struct device
*master
,
496 struct zx_vga
*vga
= dev_get_drvdata(dev
);
498 clk_disable_unprepare(vga
->i2c_wclk
);
501 static const struct component_ops zx_vga_component_ops
= {
503 .unbind
= zx_vga_unbind
,
506 static int zx_vga_probe(struct platform_device
*pdev
)
508 return component_add(&pdev
->dev
, &zx_vga_component_ops
);
511 static int zx_vga_remove(struct platform_device
*pdev
)
513 component_del(&pdev
->dev
, &zx_vga_component_ops
);
517 static const struct of_device_id zx_vga_of_match
[] = {
518 { .compatible
= "zte,zx296718-vga", },
521 MODULE_DEVICE_TABLE(of
, zx_vga_of_match
);
523 struct platform_driver zx_vga_driver
= {
524 .probe
= zx_vga_probe
,
525 .remove
= zx_vga_remove
,
528 .of_match_table
= zx_vga_of_match
,