Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / zte / zx_vga.c
blob0f9bbb7e3b8dd4007e8ab2cf37d8da2490feb774
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
4 * Copyright 2017 Linaro Ltd.
5 */
7 #include <linux/clk.h>
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"
21 #include "zx_vou.h"
23 struct zx_vga_pwrctrl {
24 struct regmap *regmap;
25 u32 reg;
26 u32 mask;
29 struct zx_vga_i2c {
30 struct i2c_adapter adap;
31 struct mutex lock;
34 struct zx_vga {
35 struct drm_connector connector;
36 struct drm_encoder encoder;
37 struct zx_vga_i2c *ddc;
38 struct device *dev;
39 void __iomem *mmio;
40 struct clk *i2c_wclk;
41 struct zx_vga_pwrctrl pwrctrl;
42 struct completion complete;
43 bool connected;
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,
55 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);
79 struct edid *edid;
80 int ret;
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);
89 if (!edid) {
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;
99 return 0;
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);
110 kfree(edid);
112 return ret;
115 static enum drm_mode_status
116 zx_vga_connector_mode_valid(struct drm_connector *connector,
117 struct drm_display_mode *mode)
119 return MODE_OK;
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;
150 int ret;
152 encoder->possible_crtcs = VOU_CRTC_MASK;
154 ret = drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_DAC);
155 if (ret) {
156 DRM_DEV_ERROR(dev, "failed to init encoder: %d\n", ret);
157 return 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,
167 &vga->ddc->adap);
168 if (ret) {
169 DRM_DEV_ERROR(dev, "failed to init connector: %d\n", ret);
170 goto clean_encoder;
173 drm_connector_helper_add(connector, &zx_vga_connector_helper_funcs);
175 ret = drm_connector_attach_encoder(connector, encoder);
176 if (ret) {
177 DRM_DEV_ERROR(dev, "failed to attach encoder: %d\n", ret);
178 goto clean_connector;
181 return 0;
183 clean_connector:
184 drm_connector_cleanup(connector);
185 clean_encoder:
186 drm_encoder_cleanup(encoder);
187 return ret;
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;
196 int ret;
198 ret = of_parse_phandle_with_fixed_args(dev->of_node,
199 "zte,vga-power-control", 2, 0, &out_args);
200 if (ret)
201 return ret;
203 regmap = syscon_node_to_regmap(out_args.np);
204 if (IS_ERR(regmap)) {
205 ret = PTR_ERR(regmap);
206 goto out;
209 pwrctrl->regmap = regmap;
210 pwrctrl->reg = out_args.args[0];
211 pwrctrl->mask = out_args.args[1];
213 out:
214 of_node_put(out_args.np);
215 return ret;
218 static int zx_vga_i2c_read(struct zx_vga *vga, struct i2c_msg *msg)
220 int len = msg->len;
221 u8 *buf = msg->buf;
222 u32 offset = 0;
223 int i;
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);
231 while (len > 0) {
232 u32 cnt;
234 /* Clear RX FIFO */
235 zx_writel_mask(vga->mmio + VGA_RXF_CTRL, VGA_RX_FIFO_CLEAR,
236 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,
243 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");
248 return -ETIMEDOUT;
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);
259 len -= cnt;
260 offset += cnt;
263 return 0;
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)))
273 return -EINVAL;
275 /* Hardware will take care of the slave address shifting */
276 zx_writel(vga->mmio + VGA_DEVICE_ADDR, msg->addr);
278 return 0;
281 static int zx_vga_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
282 int num)
284 struct zx_vga *vga = i2c_get_adapdata(adap);
285 struct zx_vga_i2c *ddc = vga->ddc;
286 int ret = 0;
287 int i;
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]);
294 else
295 ret = zx_vga_i2c_write(vga, &msgs[i]);
297 if (ret < 0)
298 break;
301 if (!ret)
302 ret = num;
304 mutex_unlock(&ddc->lock);
306 return ret;
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;
324 int ret;
326 ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL);
327 if (!ddc)
328 return -ENOMEM;
330 vga->ddc = ddc;
331 mutex_init(&ddc->lock);
333 adap = &ddc->adap;
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);
341 if (ret) {
342 DRM_DEV_ERROR(dev, "failed to add I2C adapter: %d\n", ret);
343 return ret;
346 i2c_set_adapdata(adap, vga);
348 return 0;
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);
357 return IRQ_HANDLED;
360 static irqreturn_t zx_vga_irq_handler(int irq, void *dev_id)
362 struct zx_vga *vga = dev_id;
363 u32 status;
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,
369 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);
391 return IRQ_HANDLED;
394 return IRQ_NONE;
397 static void zx_vga_hw_init(struct zx_vga *vga)
399 unsigned long ref = clk_get_rate(vga->i2c_wclk);
400 int div;
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
415 * start working.
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;
426 struct zx_vga *vga;
427 int irq;
428 int ret;
430 vga = devm_kzalloc(dev, sizeof(*vga), GFP_KERNEL);
431 if (!vga)
432 return -ENOMEM;
434 vga->dev = dev;
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);
443 if (irq < 0)
444 return irq;
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);
450 return ret;
453 ret = zx_vga_pwrctrl_init(vga);
454 if (ret) {
455 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
456 return ret;
459 ret = zx_vga_ddc_register(vga);
460 if (ret) {
461 DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
462 return ret;
465 ret = zx_vga_register(drm, vga);
466 if (ret) {
467 DRM_DEV_ERROR(dev, "failed to register vga: %d\n", ret);
468 return 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,
475 dev_name(dev), vga);
476 if (ret) {
477 DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
478 return ret;
481 ret = clk_prepare_enable(vga->i2c_wclk);
482 if (ret)
483 return ret;
485 zx_vga_hw_init(vga);
487 return 0;
490 static void zx_vga_unbind(struct device *dev, struct device *master,
491 void *data)
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 = {
499 .bind = zx_vga_bind,
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);
511 return 0;
514 static const struct of_device_id zx_vga_of_match[] = {
515 { .compatible = "zte,zx296718-vga", },
516 { /* end */ },
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,
523 .driver = {
524 .name = "zx-vga",
525 .of_match_table = zx_vga_of_match,