rcutorture: Eliminate unused ts_rem local from rcu_trace_clock_local()
[linux/fpc-iii.git] / drivers / gpu / drm / zte / zx_vga.c
blob1e0811f775cb33bff8c2de1734c3fd5f6be38e25
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_crtc_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_mode_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 .dpms = drm_atomic_helper_connector_dpms,
142 .fill_modes = drm_helper_probe_single_connector_modes,
143 .detect = zx_vga_connector_detect,
144 .destroy = drm_connector_cleanup,
145 .reset = drm_atomic_helper_connector_reset,
146 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
147 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
150 static int zx_vga_register(struct drm_device *drm, struct zx_vga *vga)
152 struct drm_encoder *encoder = &vga->encoder;
153 struct drm_connector *connector = &vga->connector;
154 struct device *dev = vga->dev;
155 int ret;
157 encoder->possible_crtcs = VOU_CRTC_MASK;
159 ret = drm_encoder_init(drm, encoder, &zx_vga_encoder_funcs,
160 DRM_MODE_ENCODER_DAC, NULL);
161 if (ret) {
162 DRM_DEV_ERROR(dev, "failed to init encoder: %d\n", ret);
163 return ret;
166 drm_encoder_helper_add(encoder, &zx_vga_encoder_helper_funcs);
168 vga->connector.polled = DRM_CONNECTOR_POLL_HPD;
170 ret = drm_connector_init(drm, connector, &zx_vga_connector_funcs,
171 DRM_MODE_CONNECTOR_VGA);
172 if (ret) {
173 DRM_DEV_ERROR(dev, "failed to init connector: %d\n", ret);
174 goto clean_encoder;
177 drm_connector_helper_add(connector, &zx_vga_connector_helper_funcs);
179 ret = drm_mode_connector_attach_encoder(connector, encoder);
180 if (ret) {
181 DRM_DEV_ERROR(dev, "failed to attach encoder: %d\n", ret);
182 goto clean_connector;
185 return 0;
187 clean_connector:
188 drm_connector_cleanup(connector);
189 clean_encoder:
190 drm_encoder_cleanup(encoder);
191 return ret;
194 static int zx_vga_pwrctrl_init(struct zx_vga *vga)
196 struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
197 struct device *dev = vga->dev;
198 struct of_phandle_args out_args;
199 struct regmap *regmap;
200 int ret;
202 ret = of_parse_phandle_with_fixed_args(dev->of_node,
203 "zte,vga-power-control", 2, 0, &out_args);
204 if (ret)
205 return ret;
207 regmap = syscon_node_to_regmap(out_args.np);
208 if (IS_ERR(regmap)) {
209 ret = PTR_ERR(regmap);
210 goto out;
213 pwrctrl->regmap = regmap;
214 pwrctrl->reg = out_args.args[0];
215 pwrctrl->mask = out_args.args[1];
217 out:
218 of_node_put(out_args.np);
219 return ret;
222 static int zx_vga_i2c_read(struct zx_vga *vga, struct i2c_msg *msg)
224 int len = msg->len;
225 u8 *buf = msg->buf;
226 u32 offset = 0;
227 int i;
229 reinit_completion(&vga->complete);
231 /* Select combo write */
232 zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_COMBO, VGA_CMD_COMBO);
233 zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_RW, 0);
235 while (len > 0) {
236 u32 cnt;
238 /* Clear RX FIFO */
239 zx_writel_mask(vga->mmio + VGA_RXF_CTRL, VGA_RX_FIFO_CLEAR,
240 VGA_RX_FIFO_CLEAR);
242 /* Data offset to read from */
243 zx_writel(vga->mmio + VGA_SUB_ADDR, offset);
245 /* Kick off the transfer */
246 zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS,
247 VGA_CMD_TRANS);
249 if (!wait_for_completion_timeout(&vga->complete,
250 msecs_to_jiffies(1000))) {
251 DRM_DEV_ERROR(vga->dev, "transfer timeout\n");
252 return -ETIMEDOUT;
255 cnt = zx_readl(vga->mmio + VGA_RXF_STATUS);
256 cnt = (cnt & VGA_RXF_COUNT_MASK) >> VGA_RXF_COUNT_SHIFT;
257 /* FIFO status may report more data than we need to read */
258 cnt = min_t(u32, len, cnt);
260 for (i = 0; i < cnt; i++)
261 *buf++ = zx_readl(vga->mmio + VGA_DATA);
263 len -= cnt;
264 offset += cnt;
267 return 0;
270 static int zx_vga_i2c_write(struct zx_vga *vga, struct i2c_msg *msg)
273 * The DDC I2C adapter is only for reading EDID data, so we assume
274 * that the write to this adapter must be the EDID data offset.
276 if ((msg->len != 1) || ((msg->addr != DDC_ADDR)))
277 return -EINVAL;
279 /* Hardware will take care of the slave address shifting */
280 zx_writel(vga->mmio + VGA_DEVICE_ADDR, msg->addr);
282 return 0;
285 static int zx_vga_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
286 int num)
288 struct zx_vga *vga = i2c_get_adapdata(adap);
289 struct zx_vga_i2c *ddc = vga->ddc;
290 int ret = 0;
291 int i;
293 mutex_lock(&ddc->lock);
295 for (i = 0; i < num; i++) {
296 if (msgs[i].flags & I2C_M_RD)
297 ret = zx_vga_i2c_read(vga, &msgs[i]);
298 else
299 ret = zx_vga_i2c_write(vga, &msgs[i]);
301 if (ret < 0)
302 break;
305 if (!ret)
306 ret = num;
308 mutex_unlock(&ddc->lock);
310 return ret;
313 static u32 zx_vga_i2c_func(struct i2c_adapter *adapter)
315 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
318 static const struct i2c_algorithm zx_vga_algorithm = {
319 .master_xfer = zx_vga_i2c_xfer,
320 .functionality = zx_vga_i2c_func,
323 static int zx_vga_ddc_register(struct zx_vga *vga)
325 struct device *dev = vga->dev;
326 struct i2c_adapter *adap;
327 struct zx_vga_i2c *ddc;
328 int ret;
330 ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL);
331 if (!ddc)
332 return -ENOMEM;
334 vga->ddc = ddc;
335 mutex_init(&ddc->lock);
337 adap = &ddc->adap;
338 adap->owner = THIS_MODULE;
339 adap->class = I2C_CLASS_DDC;
340 adap->dev.parent = dev;
341 adap->algo = &zx_vga_algorithm;
342 snprintf(adap->name, sizeof(adap->name), "zx vga i2c");
344 ret = i2c_add_adapter(adap);
345 if (ret) {
346 DRM_DEV_ERROR(dev, "failed to add I2C adapter: %d\n", ret);
347 return ret;
350 i2c_set_adapdata(adap, vga);
352 return 0;
355 static irqreturn_t zx_vga_irq_thread(int irq, void *dev_id)
357 struct zx_vga *vga = dev_id;
359 drm_helper_hpd_irq_event(vga->connector.dev);
361 return IRQ_HANDLED;
364 static irqreturn_t zx_vga_irq_handler(int irq, void *dev_id)
366 struct zx_vga *vga = dev_id;
367 u32 status;
369 status = zx_readl(vga->mmio + VGA_I2C_STATUS);
371 /* Clear interrupt status */
372 zx_writel_mask(vga->mmio + VGA_I2C_STATUS, VGA_CLEAR_IRQ,
373 VGA_CLEAR_IRQ);
375 if (status & VGA_DEVICE_CONNECTED) {
377 * Since VGA_DETECT_SEL bits need to be reset for switching DDC
378 * bus from device detection to EDID read, rather than setting
379 * up HAS_DEVICE bit here, we need to do that in .get_modes
380 * hook for unplug detecting after EDID read succeeds.
382 vga->connected = true;
383 return IRQ_WAKE_THREAD;
386 if (status & VGA_DEVICE_DISCONNECTED) {
387 zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL,
388 VGA_DETECT_SEL_NO_DEVICE);
389 vga->connected = false;
390 return IRQ_WAKE_THREAD;
393 if (status & VGA_TRANS_DONE) {
394 complete(&vga->complete);
395 return IRQ_HANDLED;
398 return IRQ_NONE;
401 static void zx_vga_hw_init(struct zx_vga *vga)
403 unsigned long ref = clk_get_rate(vga->i2c_wclk);
404 int div;
407 * Set up I2C fast speed divider per formula below to get 400kHz.
408 * scl = ref / ((div + 1) * 4)
410 div = DIV_ROUND_UP(ref / 1000, 400 * 4) - 1;
411 zx_writel(vga->mmio + VGA_CLK_DIV_FS, div);
413 /* Set up device detection */
414 zx_writel(vga->mmio + VGA_AUTO_DETECT_PARA, 0x80);
415 zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_NO_DEVICE);
418 * We need to poke monitor via DDC bus to get connection irq
419 * start working.
421 zx_writel(vga->mmio + VGA_DEVICE_ADDR, DDC_ADDR);
422 zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS, VGA_CMD_TRANS);
425 static int zx_vga_bind(struct device *dev, struct device *master, void *data)
427 struct platform_device *pdev = to_platform_device(dev);
428 struct drm_device *drm = data;
429 struct resource *res;
430 struct zx_vga *vga;
431 int irq;
432 int ret;
434 vga = devm_kzalloc(dev, sizeof(*vga), GFP_KERNEL);
435 if (!vga)
436 return -ENOMEM;
438 vga->dev = dev;
439 dev_set_drvdata(dev, vga);
441 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
442 vga->mmio = devm_ioremap_resource(dev, res);
443 if (IS_ERR(vga->mmio))
444 return PTR_ERR(vga->mmio);
446 irq = platform_get_irq(pdev, 0);
447 if (irq < 0)
448 return irq;
450 vga->i2c_wclk = devm_clk_get(dev, "i2c_wclk");
451 if (IS_ERR(vga->i2c_wclk)) {
452 ret = PTR_ERR(vga->i2c_wclk);
453 DRM_DEV_ERROR(dev, "failed to get i2c_wclk: %d\n", ret);
454 return ret;
457 ret = zx_vga_pwrctrl_init(vga);
458 if (ret) {
459 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
460 return ret;
463 ret = zx_vga_ddc_register(vga);
464 if (ret) {
465 DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
466 return ret;
469 ret = zx_vga_register(drm, vga);
470 if (ret) {
471 DRM_DEV_ERROR(dev, "failed to register vga: %d\n", ret);
472 return ret;
475 init_completion(&vga->complete);
477 ret = devm_request_threaded_irq(dev, irq, zx_vga_irq_handler,
478 zx_vga_irq_thread, IRQF_SHARED,
479 dev_name(dev), vga);
480 if (ret) {
481 DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
482 return ret;
485 ret = clk_prepare_enable(vga->i2c_wclk);
486 if (ret)
487 return ret;
489 zx_vga_hw_init(vga);
491 return 0;
494 static void zx_vga_unbind(struct device *dev, struct device *master,
495 void *data)
497 struct zx_vga *vga = dev_get_drvdata(dev);
499 clk_disable_unprepare(vga->i2c_wclk);
502 static const struct component_ops zx_vga_component_ops = {
503 .bind = zx_vga_bind,
504 .unbind = zx_vga_unbind,
507 static int zx_vga_probe(struct platform_device *pdev)
509 return component_add(&pdev->dev, &zx_vga_component_ops);
512 static int zx_vga_remove(struct platform_device *pdev)
514 component_del(&pdev->dev, &zx_vga_component_ops);
515 return 0;
518 static const struct of_device_id zx_vga_of_match[] = {
519 { .compatible = "zte,zx296718-vga", },
520 { /* end */ },
522 MODULE_DEVICE_TABLE(of, zx_vga_of_match);
524 struct platform_driver zx_vga_driver = {
525 .probe = zx_vga_probe,
526 .remove = zx_vga_remove,
527 .driver = {
528 .name = "zx-vga",
529 .of_match_table = zx_vga_of_match,