dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / zte / zx_vga.c
blobe14c1d7097409b18c3d8c6aad69d3dc14a372e9b
1 /*
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.
8 */
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>
17 #include <drm/drmP.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 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);
83 struct edid *edid;
84 int ret;
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);
93 if (!edid) {
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;
103 return 0;
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);
114 kfree(edid);
116 return ret;
119 static enum drm_mode_status
120 zx_vga_connector_mode_valid(struct drm_connector *connector,
121 struct drm_display_mode *mode)
123 return MODE_OK;
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;
154 int ret;
156 encoder->possible_crtcs = VOU_CRTC_MASK;
158 ret = drm_encoder_init(drm, encoder, &zx_vga_encoder_funcs,
159 DRM_MODE_ENCODER_DAC, NULL);
160 if (ret) {
161 DRM_DEV_ERROR(dev, "failed to init encoder: %d\n", ret);
162 return 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);
171 if (ret) {
172 DRM_DEV_ERROR(dev, "failed to init connector: %d\n", ret);
173 goto clean_encoder;
176 drm_connector_helper_add(connector, &zx_vga_connector_helper_funcs);
178 ret = drm_connector_attach_encoder(connector, encoder);
179 if (ret) {
180 DRM_DEV_ERROR(dev, "failed to attach encoder: %d\n", ret);
181 goto clean_connector;
184 return 0;
186 clean_connector:
187 drm_connector_cleanup(connector);
188 clean_encoder:
189 drm_encoder_cleanup(encoder);
190 return ret;
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;
199 int ret;
201 ret = of_parse_phandle_with_fixed_args(dev->of_node,
202 "zte,vga-power-control", 2, 0, &out_args);
203 if (ret)
204 return ret;
206 regmap = syscon_node_to_regmap(out_args.np);
207 if (IS_ERR(regmap)) {
208 ret = PTR_ERR(regmap);
209 goto out;
212 pwrctrl->regmap = regmap;
213 pwrctrl->reg = out_args.args[0];
214 pwrctrl->mask = out_args.args[1];
216 out:
217 of_node_put(out_args.np);
218 return ret;
221 static int zx_vga_i2c_read(struct zx_vga *vga, struct i2c_msg *msg)
223 int len = msg->len;
224 u8 *buf = msg->buf;
225 u32 offset = 0;
226 int i;
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);
234 while (len > 0) {
235 u32 cnt;
237 /* Clear RX FIFO */
238 zx_writel_mask(vga->mmio + VGA_RXF_CTRL, VGA_RX_FIFO_CLEAR,
239 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,
246 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");
251 return -ETIMEDOUT;
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);
262 len -= cnt;
263 offset += cnt;
266 return 0;
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)))
276 return -EINVAL;
278 /* Hardware will take care of the slave address shifting */
279 zx_writel(vga->mmio + VGA_DEVICE_ADDR, msg->addr);
281 return 0;
284 static int zx_vga_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
285 int num)
287 struct zx_vga *vga = i2c_get_adapdata(adap);
288 struct zx_vga_i2c *ddc = vga->ddc;
289 int ret = 0;
290 int i;
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]);
297 else
298 ret = zx_vga_i2c_write(vga, &msgs[i]);
300 if (ret < 0)
301 break;
304 if (!ret)
305 ret = num;
307 mutex_unlock(&ddc->lock);
309 return ret;
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;
327 int ret;
329 ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL);
330 if (!ddc)
331 return -ENOMEM;
333 vga->ddc = ddc;
334 mutex_init(&ddc->lock);
336 adap = &ddc->adap;
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);
344 if (ret) {
345 DRM_DEV_ERROR(dev, "failed to add I2C adapter: %d\n", ret);
346 return ret;
349 i2c_set_adapdata(adap, vga);
351 return 0;
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);
360 return IRQ_HANDLED;
363 static irqreturn_t zx_vga_irq_handler(int irq, void *dev_id)
365 struct zx_vga *vga = dev_id;
366 u32 status;
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,
372 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);
394 return IRQ_HANDLED;
397 return IRQ_NONE;
400 static void zx_vga_hw_init(struct zx_vga *vga)
402 unsigned long ref = clk_get_rate(vga->i2c_wclk);
403 int div;
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
418 * start working.
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;
429 struct zx_vga *vga;
430 int irq;
431 int ret;
433 vga = devm_kzalloc(dev, sizeof(*vga), GFP_KERNEL);
434 if (!vga)
435 return -ENOMEM;
437 vga->dev = dev;
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);
446 if (irq < 0)
447 return irq;
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);
453 return ret;
456 ret = zx_vga_pwrctrl_init(vga);
457 if (ret) {
458 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
459 return ret;
462 ret = zx_vga_ddc_register(vga);
463 if (ret) {
464 DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
465 return ret;
468 ret = zx_vga_register(drm, vga);
469 if (ret) {
470 DRM_DEV_ERROR(dev, "failed to register vga: %d\n", ret);
471 return 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,
478 dev_name(dev), vga);
479 if (ret) {
480 DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
481 return ret;
484 ret = clk_prepare_enable(vga->i2c_wclk);
485 if (ret)
486 return ret;
488 zx_vga_hw_init(vga);
490 return 0;
493 static void zx_vga_unbind(struct device *dev, struct device *master,
494 void *data)
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 = {
502 .bind = zx_vga_bind,
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);
514 return 0;
517 static const struct of_device_id zx_vga_of_match[] = {
518 { .compatible = "zte,zx296718-vga", },
519 { /* end */ },
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,
526 .driver = {
527 .name = "zx-vga",
528 .of_match_table = zx_vga_of_match,