1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Sanechips Technology Co., Ltd.
4  * Copyright 2017 Linaro Ltd.
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 
18 #include "zx_drm_drv.h"
19 #include "zx_vga_regs.h"
20 #include "zx_vou.h"
21 
22 struct zx_vga_pwrctrl {
23 	struct regmap *regmap;
24 	u32 reg;
25 	u32 mask;
26 };
27 
28 struct zx_vga_i2c {
29 	struct i2c_adapter adap;
30 	struct mutex lock;
31 };
32 
33 struct zx_vga {
34 	struct drm_connector connector;
35 	struct drm_encoder encoder;
36 	struct zx_vga_i2c *ddc;
37 	struct device *dev;
38 	void __iomem *mmio;
39 	struct clk *i2c_wclk;
40 	struct zx_vga_pwrctrl pwrctrl;
41 	struct completion complete;
42 	bool connected;
43 };
44 
45 #define to_zx_vga(x) container_of(x, struct zx_vga, x)
46 
zx_vga_encoder_enable(struct drm_encoder * encoder)47 static void zx_vga_encoder_enable(struct drm_encoder *encoder)
48 {
49 	struct zx_vga *vga = to_zx_vga(encoder);
50 	struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
51 
52 	/* Set bit to power up VGA DACs */
53 	regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
54 			   pwrctrl->mask);
55 
56 	vou_inf_enable(VOU_VGA, encoder->crtc);
57 }
58 
zx_vga_encoder_disable(struct drm_encoder * encoder)59 static void zx_vga_encoder_disable(struct drm_encoder *encoder)
60 {
61 	struct zx_vga *vga = to_zx_vga(encoder);
62 	struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
63 
64 	vou_inf_disable(VOU_VGA, encoder->crtc);
65 
66 	/* Clear bit to power down VGA DACs */
67 	regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
68 }
69 
70 static const struct drm_encoder_helper_funcs zx_vga_encoder_helper_funcs = {
71 	.enable	= zx_vga_encoder_enable,
72 	.disable = zx_vga_encoder_disable,
73 };
74 
75 static const struct drm_encoder_funcs zx_vga_encoder_funcs = {
76 	.destroy = drm_encoder_cleanup,
77 };
78 
zx_vga_connector_get_modes(struct drm_connector * connector)79 static int zx_vga_connector_get_modes(struct drm_connector *connector)
80 {
81 	struct zx_vga *vga = to_zx_vga(connector);
82 	struct edid *edid;
83 	int ret;
84 
85 	/*
86 	 * Clear both detection bits to switch I2C bus from device
87 	 * detecting to EDID reading.
88 	 */
89 	zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, 0);
90 
91 	edid = drm_get_edid(connector, &vga->ddc->adap);
92 	if (!edid) {
93 		/*
94 		 * If EDID reading fails, we set the device state into
95 		 * disconnected.  Locking is not required here, since the
96 		 * VGA_AUTO_DETECT_SEL register write in irq handler cannot
97 		 * be triggered when both detection bits are cleared as above.
98 		 */
99 		zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL,
100 			  VGA_DETECT_SEL_NO_DEVICE);
101 		vga->connected = false;
102 		return 0;
103 	}
104 
105 	/*
106 	 * As edid reading succeeds, device must be connected, so we set
107 	 * up detection bit for unplug interrupt here.
108 	 */
109 	zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_HAS_DEVICE);
110 
111 	drm_connector_update_edid_property(connector, edid);
112 	ret = drm_add_edid_modes(connector, edid);
113 	kfree(edid);
114 
115 	return ret;
116 }
117 
118 static enum drm_mode_status
zx_vga_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)119 zx_vga_connector_mode_valid(struct drm_connector *connector,
120 			    struct drm_display_mode *mode)
121 {
122 	return MODE_OK;
123 }
124 
125 static struct drm_connector_helper_funcs zx_vga_connector_helper_funcs = {
126 	.get_modes = zx_vga_connector_get_modes,
127 	.mode_valid = zx_vga_connector_mode_valid,
128 };
129 
130 static enum drm_connector_status
zx_vga_connector_detect(struct drm_connector * connector,bool force)131 zx_vga_connector_detect(struct drm_connector *connector, bool force)
132 {
133 	struct zx_vga *vga = to_zx_vga(connector);
134 
135 	return vga->connected ? connector_status_connected :
136 				connector_status_disconnected;
137 }
138 
139 static const struct drm_connector_funcs zx_vga_connector_funcs = {
140 	.fill_modes = drm_helper_probe_single_connector_modes,
141 	.detect = zx_vga_connector_detect,
142 	.destroy = drm_connector_cleanup,
143 	.reset = drm_atomic_helper_connector_reset,
144 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
145 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
146 };
147 
zx_vga_register(struct drm_device * drm,struct zx_vga * vga)148 static int zx_vga_register(struct drm_device *drm, struct zx_vga *vga)
149 {
150 	struct drm_encoder *encoder = &vga->encoder;
151 	struct drm_connector *connector = &vga->connector;
152 	struct device *dev = vga->dev;
153 	int ret;
154 
155 	encoder->possible_crtcs = VOU_CRTC_MASK;
156 
157 	ret = drm_encoder_init(drm, encoder, &zx_vga_encoder_funcs,
158 			       DRM_MODE_ENCODER_DAC, NULL);
159 	if (ret) {
160 		DRM_DEV_ERROR(dev, "failed to init encoder: %d\n", ret);
161 		return ret;
162 	};
163 
164 	drm_encoder_helper_add(encoder, &zx_vga_encoder_helper_funcs);
165 
166 	vga->connector.polled = DRM_CONNECTOR_POLL_HPD;
167 
168 	ret = drm_connector_init(drm, connector, &zx_vga_connector_funcs,
169 				 DRM_MODE_CONNECTOR_VGA);
170 	if (ret) {
171 		DRM_DEV_ERROR(dev, "failed to init connector: %d\n", ret);
172 		goto clean_encoder;
173 	};
174 
175 	drm_connector_helper_add(connector, &zx_vga_connector_helper_funcs);
176 
177 	ret = drm_connector_attach_encoder(connector, encoder);
178 	if (ret) {
179 		DRM_DEV_ERROR(dev, "failed to attach encoder: %d\n", ret);
180 		goto clean_connector;
181 	};
182 
183 	return 0;
184 
185 clean_connector:
186 	drm_connector_cleanup(connector);
187 clean_encoder:
188 	drm_encoder_cleanup(encoder);
189 	return ret;
190 }
191 
zx_vga_pwrctrl_init(struct zx_vga * vga)192 static int zx_vga_pwrctrl_init(struct zx_vga *vga)
193 {
194 	struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
195 	struct device *dev = vga->dev;
196 	struct of_phandle_args out_args;
197 	struct regmap *regmap;
198 	int ret;
199 
200 	ret = of_parse_phandle_with_fixed_args(dev->of_node,
201 				"zte,vga-power-control", 2, 0, &out_args);
202 	if (ret)
203 		return ret;
204 
205 	regmap = syscon_node_to_regmap(out_args.np);
206 	if (IS_ERR(regmap)) {
207 		ret = PTR_ERR(regmap);
208 		goto out;
209 	}
210 
211 	pwrctrl->regmap = regmap;
212 	pwrctrl->reg = out_args.args[0];
213 	pwrctrl->mask = out_args.args[1];
214 
215 out:
216 	of_node_put(out_args.np);
217 	return ret;
218 }
219 
zx_vga_i2c_read(struct zx_vga * vga,struct i2c_msg * msg)220 static int zx_vga_i2c_read(struct zx_vga *vga, struct i2c_msg *msg)
221 {
222 	int len = msg->len;
223 	u8 *buf = msg->buf;
224 	u32 offset = 0;
225 	int i;
226 
227 	reinit_completion(&vga->complete);
228 
229 	/* Select combo write */
230 	zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_COMBO, VGA_CMD_COMBO);
231 	zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_RW, 0);
232 
233 	while (len > 0) {
234 		u32 cnt;
235 
236 		/* Clear RX FIFO */
237 		zx_writel_mask(vga->mmio + VGA_RXF_CTRL, VGA_RX_FIFO_CLEAR,
238 			       VGA_RX_FIFO_CLEAR);
239 
240 		/* Data offset to read from */
241 		zx_writel(vga->mmio + VGA_SUB_ADDR, offset);
242 
243 		/* Kick off the transfer */
244 		zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS,
245 			       VGA_CMD_TRANS);
246 
247 		if (!wait_for_completion_timeout(&vga->complete,
248 						 msecs_to_jiffies(1000))) {
249 			DRM_DEV_ERROR(vga->dev, "transfer timeout\n");
250 			return -ETIMEDOUT;
251 		}
252 
253 		cnt = zx_readl(vga->mmio + VGA_RXF_STATUS);
254 		cnt = (cnt & VGA_RXF_COUNT_MASK) >> VGA_RXF_COUNT_SHIFT;
255 		/* FIFO status may report more data than we need to read */
256 		cnt = min_t(u32, len, cnt);
257 
258 		for (i = 0; i < cnt; i++)
259 			*buf++ = zx_readl(vga->mmio + VGA_DATA);
260 
261 		len -= cnt;
262 		offset += cnt;
263 	}
264 
265 	return 0;
266 }
267 
zx_vga_i2c_write(struct zx_vga * vga,struct i2c_msg * msg)268 static int zx_vga_i2c_write(struct zx_vga *vga, struct i2c_msg *msg)
269 {
270 	/*
271 	 * The DDC I2C adapter is only for reading EDID data, so we assume
272 	 * that the write to this adapter must be the EDID data offset.
273 	 */
274 	if ((msg->len != 1) || ((msg->addr != DDC_ADDR)))
275 		return -EINVAL;
276 
277 	/* Hardware will take care of the slave address shifting */
278 	zx_writel(vga->mmio + VGA_DEVICE_ADDR, msg->addr);
279 
280 	return 0;
281 }
282 
zx_vga_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)283 static int zx_vga_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
284 			   int num)
285 {
286 	struct zx_vga *vga = i2c_get_adapdata(adap);
287 	struct zx_vga_i2c *ddc = vga->ddc;
288 	int ret = 0;
289 	int i;
290 
291 	mutex_lock(&ddc->lock);
292 
293 	for (i = 0; i < num; i++) {
294 		if (msgs[i].flags & I2C_M_RD)
295 			ret = zx_vga_i2c_read(vga, &msgs[i]);
296 		else
297 			ret = zx_vga_i2c_write(vga, &msgs[i]);
298 
299 		if (ret < 0)
300 			break;
301 	}
302 
303 	if (!ret)
304 		ret = num;
305 
306 	mutex_unlock(&ddc->lock);
307 
308 	return ret;
309 }
310 
zx_vga_i2c_func(struct i2c_adapter * adapter)311 static u32 zx_vga_i2c_func(struct i2c_adapter *adapter)
312 {
313 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
314 }
315 
316 static const struct i2c_algorithm zx_vga_algorithm = {
317 	.master_xfer	= zx_vga_i2c_xfer,
318 	.functionality	= zx_vga_i2c_func,
319 };
320 
zx_vga_ddc_register(struct zx_vga * vga)321 static int zx_vga_ddc_register(struct zx_vga *vga)
322 {
323 	struct device *dev = vga->dev;
324 	struct i2c_adapter *adap;
325 	struct zx_vga_i2c *ddc;
326 	int ret;
327 
328 	ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL);
329 	if (!ddc)
330 		return -ENOMEM;
331 
332 	vga->ddc = ddc;
333 	mutex_init(&ddc->lock);
334 
335 	adap = &ddc->adap;
336 	adap->owner = THIS_MODULE;
337 	adap->class = I2C_CLASS_DDC;
338 	adap->dev.parent = dev;
339 	adap->algo = &zx_vga_algorithm;
340 	snprintf(adap->name, sizeof(adap->name), "zx vga i2c");
341 
342 	ret = i2c_add_adapter(adap);
343 	if (ret) {
344 		DRM_DEV_ERROR(dev, "failed to add I2C adapter: %d\n", ret);
345 		return ret;
346 	}
347 
348 	i2c_set_adapdata(adap, vga);
349 
350 	return 0;
351 }
352 
zx_vga_irq_thread(int irq,void * dev_id)353 static irqreturn_t zx_vga_irq_thread(int irq, void *dev_id)
354 {
355 	struct zx_vga *vga = dev_id;
356 
357 	drm_helper_hpd_irq_event(vga->connector.dev);
358 
359 	return IRQ_HANDLED;
360 }
361 
zx_vga_irq_handler(int irq,void * dev_id)362 static irqreturn_t zx_vga_irq_handler(int irq, void *dev_id)
363 {
364 	struct zx_vga *vga = dev_id;
365 	u32 status;
366 
367 	status = zx_readl(vga->mmio + VGA_I2C_STATUS);
368 
369 	/* Clear interrupt status */
370 	zx_writel_mask(vga->mmio + VGA_I2C_STATUS, VGA_CLEAR_IRQ,
371 		       VGA_CLEAR_IRQ);
372 
373 	if (status & VGA_DEVICE_CONNECTED) {
374 		/*
375 		 * Since VGA_DETECT_SEL bits need to be reset for switching DDC
376 		 * bus from device detection to EDID read, rather than setting
377 		 * up HAS_DEVICE bit here, we need to do that in .get_modes
378 		 * hook for unplug detecting after EDID read succeeds.
379 		 */
380 		vga->connected = true;
381 		return IRQ_WAKE_THREAD;
382 	}
383 
384 	if (status & VGA_DEVICE_DISCONNECTED) {
385 		zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL,
386 			  VGA_DETECT_SEL_NO_DEVICE);
387 		vga->connected = false;
388 		return IRQ_WAKE_THREAD;
389 	}
390 
391 	if (status & VGA_TRANS_DONE) {
392 		complete(&vga->complete);
393 		return IRQ_HANDLED;
394 	}
395 
396 	return IRQ_NONE;
397 }
398 
zx_vga_hw_init(struct zx_vga * vga)399 static void zx_vga_hw_init(struct zx_vga *vga)
400 {
401 	unsigned long ref = clk_get_rate(vga->i2c_wclk);
402 	int div;
403 
404 	/*
405 	 * Set up I2C fast speed divider per formula below to get 400kHz.
406 	 *   scl = ref / ((div + 1) * 4)
407 	 */
408 	div = DIV_ROUND_UP(ref / 1000, 400 * 4) - 1;
409 	zx_writel(vga->mmio + VGA_CLK_DIV_FS, div);
410 
411 	/* Set up device detection */
412 	zx_writel(vga->mmio + VGA_AUTO_DETECT_PARA, 0x80);
413 	zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_NO_DEVICE);
414 
415 	/*
416 	 * We need to poke monitor via DDC bus to get connection irq
417 	 * start working.
418 	 */
419 	zx_writel(vga->mmio + VGA_DEVICE_ADDR, DDC_ADDR);
420 	zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS, VGA_CMD_TRANS);
421 }
422 
zx_vga_bind(struct device * dev,struct device * master,void * data)423 static int zx_vga_bind(struct device *dev, struct device *master, void *data)
424 {
425 	struct platform_device *pdev = to_platform_device(dev);
426 	struct drm_device *drm = data;
427 	struct resource *res;
428 	struct zx_vga *vga;
429 	int irq;
430 	int ret;
431 
432 	vga = devm_kzalloc(dev, sizeof(*vga), GFP_KERNEL);
433 	if (!vga)
434 		return -ENOMEM;
435 
436 	vga->dev = dev;
437 	dev_set_drvdata(dev, vga);
438 
439 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
440 	vga->mmio = devm_ioremap_resource(dev, res);
441 	if (IS_ERR(vga->mmio))
442 		return PTR_ERR(vga->mmio);
443 
444 	irq = platform_get_irq(pdev, 0);
445 	if (irq < 0)
446 		return irq;
447 
448 	vga->i2c_wclk = devm_clk_get(dev, "i2c_wclk");
449 	if (IS_ERR(vga->i2c_wclk)) {
450 		ret = PTR_ERR(vga->i2c_wclk);
451 		DRM_DEV_ERROR(dev, "failed to get i2c_wclk: %d\n", ret);
452 		return ret;
453 	}
454 
455 	ret = zx_vga_pwrctrl_init(vga);
456 	if (ret) {
457 		DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
458 		return ret;
459 	}
460 
461 	ret = zx_vga_ddc_register(vga);
462 	if (ret) {
463 		DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
464 		return ret;
465 	}
466 
467 	ret = zx_vga_register(drm, vga);
468 	if (ret) {
469 		DRM_DEV_ERROR(dev, "failed to register vga: %d\n", ret);
470 		return ret;
471 	}
472 
473 	init_completion(&vga->complete);
474 
475 	ret = devm_request_threaded_irq(dev, irq, zx_vga_irq_handler,
476 					zx_vga_irq_thread, IRQF_SHARED,
477 					dev_name(dev), vga);
478 	if (ret) {
479 		DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
480 		return ret;
481 	}
482 
483 	ret = clk_prepare_enable(vga->i2c_wclk);
484 	if (ret)
485 		return ret;
486 
487 	zx_vga_hw_init(vga);
488 
489 	return 0;
490 }
491 
zx_vga_unbind(struct device * dev,struct device * master,void * data)492 static void zx_vga_unbind(struct device *dev, struct device *master,
493 			  void *data)
494 {
495 	struct zx_vga *vga = dev_get_drvdata(dev);
496 
497 	clk_disable_unprepare(vga->i2c_wclk);
498 }
499 
500 static const struct component_ops zx_vga_component_ops = {
501 	.bind = zx_vga_bind,
502 	.unbind = zx_vga_unbind,
503 };
504 
zx_vga_probe(struct platform_device * pdev)505 static int zx_vga_probe(struct platform_device *pdev)
506 {
507 	return component_add(&pdev->dev, &zx_vga_component_ops);
508 }
509 
zx_vga_remove(struct platform_device * pdev)510 static int zx_vga_remove(struct platform_device *pdev)
511 {
512 	component_del(&pdev->dev, &zx_vga_component_ops);
513 	return 0;
514 }
515 
516 static const struct of_device_id zx_vga_of_match[] = {
517 	{ .compatible = "zte,zx296718-vga", },
518 	{ /* end */ },
519 };
520 MODULE_DEVICE_TABLE(of, zx_vga_of_match);
521 
522 struct platform_driver zx_vga_driver = {
523 	.probe = zx_vga_probe,
524 	.remove = zx_vga_remove,
525 	.driver	= {
526 		.name = "zx-vga",
527 		.of_match_table	= zx_vga_of_match,
528 	},
529 };
530