1
2
3
4
5
6
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>
13
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>
18
19#include "zx_drm_drv.h"
20#include "zx_vga_regs.h"
21#include "zx_vou.h"
22
23struct zx_vga_pwrctrl {
24 struct regmap *regmap;
25 u32 reg;
26 u32 mask;
27};
28
29struct zx_vga_i2c {
30 struct i2c_adapter adap;
31 struct mutex lock;
32};
33
34struct 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;
44};
45
46#define to_zx_vga(x) container_of(x, struct zx_vga, x)
47
48static void zx_vga_encoder_enable(struct drm_encoder *encoder)
49{
50 struct zx_vga *vga = to_zx_vga(encoder);
51 struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
52
53
54 regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
55 pwrctrl->mask);
56
57 vou_inf_enable(VOU_VGA, encoder->crtc);
58}
59
60static void zx_vga_encoder_disable(struct drm_encoder *encoder)
61{
62 struct zx_vga *vga = to_zx_vga(encoder);
63 struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
64
65 vou_inf_disable(VOU_VGA, encoder->crtc);
66
67
68 regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
69}
70
71static const struct drm_encoder_helper_funcs zx_vga_encoder_helper_funcs = {
72 .enable = zx_vga_encoder_enable,
73 .disable = zx_vga_encoder_disable,
74};
75
76static int zx_vga_connector_get_modes(struct drm_connector *connector)
77{
78 struct zx_vga *vga = to_zx_vga(connector);
79 struct edid *edid;
80 int ret;
81
82
83
84
85
86 zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, 0);
87
88 edid = drm_get_edid(connector, &vga->ddc->adap);
89 if (!edid) {
90
91
92
93
94
95
96 zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL,
97 VGA_DETECT_SEL_NO_DEVICE);
98 vga->connected = false;
99 return 0;
100 }
101
102
103
104
105
106 zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_HAS_DEVICE);
107
108 drm_connector_update_edid_property(connector, edid);
109 ret = drm_add_edid_modes(connector, edid);
110 kfree(edid);
111
112 return ret;
113}
114
115static enum drm_mode_status
116zx_vga_connector_mode_valid(struct drm_connector *connector,
117 struct drm_display_mode *mode)
118{
119 return MODE_OK;
120}
121
122static 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,
125};
126
127static enum drm_connector_status
128zx_vga_connector_detect(struct drm_connector *connector, bool force)
129{
130 struct zx_vga *vga = to_zx_vga(connector);
131
132 return vga->connected ? connector_status_connected :
133 connector_status_disconnected;
134}
135
136static 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,
143};
144
145static int zx_vga_register(struct drm_device *drm, struct zx_vga *vga)
146{
147 struct drm_encoder *encoder = &vga->encoder;
148 struct drm_connector *connector = &vga->connector;
149 struct device *dev = vga->dev;
150 int ret;
151
152 encoder->possible_crtcs = VOU_CRTC_MASK;
153
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;
158 }
159
160 drm_encoder_helper_add(encoder, &zx_vga_encoder_helper_funcs);
161
162 vga->connector.polled = DRM_CONNECTOR_POLL_HPD;
163
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;
171 }
172
173 drm_connector_helper_add(connector, &zx_vga_connector_helper_funcs);
174
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;
179 }
180
181 return 0;
182
183clean_connector:
184 drm_connector_cleanup(connector);
185clean_encoder:
186 drm_encoder_cleanup(encoder);
187 return ret;
188}
189
190static int zx_vga_pwrctrl_init(struct zx_vga *vga)
191{
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;
197
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;
202
203 regmap = syscon_node_to_regmap(out_args.np);
204 if (IS_ERR(regmap)) {
205 ret = PTR_ERR(regmap);
206 goto out;
207 }
208
209 pwrctrl->regmap = regmap;
210 pwrctrl->reg = out_args.args[0];
211 pwrctrl->mask = out_args.args[1];
212
213out:
214 of_node_put(out_args.np);
215 return ret;
216}
217
218static int zx_vga_i2c_read(struct zx_vga *vga, struct i2c_msg *msg)
219{
220 int len = msg->len;
221 u8 *buf = msg->buf;
222 u32 offset = 0;
223 int i;
224
225 reinit_completion(&vga->complete);
226
227
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);
230
231 while (len > 0) {
232 u32 cnt;
233
234
235 zx_writel_mask(vga->mmio + VGA_RXF_CTRL, VGA_RX_FIFO_CLEAR,
236 VGA_RX_FIFO_CLEAR);
237
238
239 zx_writel(vga->mmio + VGA_SUB_ADDR, offset);
240
241
242 zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS,
243 VGA_CMD_TRANS);
244
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;
249 }
250
251 cnt = zx_readl(vga->mmio + VGA_RXF_STATUS);
252 cnt = (cnt & VGA_RXF_COUNT_MASK) >> VGA_RXF_COUNT_SHIFT;
253
254 cnt = min_t(u32, len, cnt);
255
256 for (i = 0; i < cnt; i++)
257 *buf++ = zx_readl(vga->mmio + VGA_DATA);
258
259 len -= cnt;
260 offset += cnt;
261 }
262
263 return 0;
264}
265
266static int zx_vga_i2c_write(struct zx_vga *vga, struct i2c_msg *msg)
267{
268
269
270
271
272 if ((msg->len != 1) || ((msg->addr != DDC_ADDR)))
273 return -EINVAL;
274
275
276 zx_writel(vga->mmio + VGA_DEVICE_ADDR, msg->addr);
277
278 return 0;
279}
280
281static int zx_vga_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
282 int num)
283{
284 struct zx_vga *vga = i2c_get_adapdata(adap);
285 struct zx_vga_i2c *ddc = vga->ddc;
286 int ret = 0;
287 int i;
288
289 mutex_lock(&ddc->lock);
290
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]);
296
297 if (ret < 0)
298 break;
299 }
300
301 if (!ret)
302 ret = num;
303
304 mutex_unlock(&ddc->lock);
305
306 return ret;
307}
308
309static u32 zx_vga_i2c_func(struct i2c_adapter *adapter)
310{
311 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
312}
313
314static const struct i2c_algorithm zx_vga_algorithm = {
315 .master_xfer = zx_vga_i2c_xfer,
316 .functionality = zx_vga_i2c_func,
317};
318
319static int zx_vga_ddc_register(struct zx_vga *vga)
320{
321 struct device *dev = vga->dev;
322 struct i2c_adapter *adap;
323 struct zx_vga_i2c *ddc;
324 int ret;
325
326 ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL);
327 if (!ddc)
328 return -ENOMEM;
329
330 vga->ddc = ddc;
331 mutex_init(&ddc->lock);
332
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");
339
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;
344 }
345
346 i2c_set_adapdata(adap, vga);
347
348 return 0;
349}
350
351static irqreturn_t zx_vga_irq_thread(int irq, void *dev_id)
352{
353 struct zx_vga *vga = dev_id;
354
355 drm_helper_hpd_irq_event(vga->connector.dev);
356
357 return IRQ_HANDLED;
358}
359
360static irqreturn_t zx_vga_irq_handler(int irq, void *dev_id)
361{
362 struct zx_vga *vga = dev_id;
363 u32 status;
364
365 status = zx_readl(vga->mmio + VGA_I2C_STATUS);
366
367
368 zx_writel_mask(vga->mmio + VGA_I2C_STATUS, VGA_CLEAR_IRQ,
369 VGA_CLEAR_IRQ);
370
371 if (status & VGA_DEVICE_CONNECTED) {
372
373
374
375
376
377
378 vga->connected = true;
379 return IRQ_WAKE_THREAD;
380 }
381
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;
387 }
388
389 if (status & VGA_TRANS_DONE) {
390 complete(&vga->complete);
391 return IRQ_HANDLED;
392 }
393
394 return IRQ_NONE;
395}
396
397static void zx_vga_hw_init(struct zx_vga *vga)
398{
399 unsigned long ref = clk_get_rate(vga->i2c_wclk);
400 int div;
401
402
403
404
405
406 div = DIV_ROUND_UP(ref / 1000, 400 * 4) - 1;
407 zx_writel(vga->mmio + VGA_CLK_DIV_FS, div);
408
409
410 zx_writel(vga->mmio + VGA_AUTO_DETECT_PARA, 0x80);
411 zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_NO_DEVICE);
412
413
414
415
416
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);
419}
420
421static int zx_vga_bind(struct device *dev, struct device *master, void *data)
422{
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;
429
430 vga = devm_kzalloc(dev, sizeof(*vga), GFP_KERNEL);
431 if (!vga)
432 return -ENOMEM;
433
434 vga->dev = dev;
435 dev_set_drvdata(dev, vga);
436
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);
441
442 irq = platform_get_irq(pdev, 0);
443 if (irq < 0)
444 return irq;
445
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;
451 }
452
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;
457 }
458
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;
463 }
464
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;
469 }
470
471 init_completion(&vga->complete);
472
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;
479 }
480
481 ret = clk_prepare_enable(vga->i2c_wclk);
482 if (ret)
483 return ret;
484
485 zx_vga_hw_init(vga);
486
487 return 0;
488}
489
490static void zx_vga_unbind(struct device *dev, struct device *master,
491 void *data)
492{
493 struct zx_vga *vga = dev_get_drvdata(dev);
494
495 clk_disable_unprepare(vga->i2c_wclk);
496}
497
498static const struct component_ops zx_vga_component_ops = {
499 .bind = zx_vga_bind,
500 .unbind = zx_vga_unbind,
501};
502
503static int zx_vga_probe(struct platform_device *pdev)
504{
505 return component_add(&pdev->dev, &zx_vga_component_ops);
506}
507
508static int zx_vga_remove(struct platform_device *pdev)
509{
510 component_del(&pdev->dev, &zx_vga_component_ops);
511 return 0;
512}
513
514static const struct of_device_id zx_vga_of_match[] = {
515 { .compatible = "zte,zx296718-vga", },
516 { },
517};
518MODULE_DEVICE_TABLE(of, zx_vga_of_match);
519
520struct 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,
526 },
527};
528