1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/pinctrl/consumer.h>
10 
11 #include "msm_kms.h"
12 #include "hdmi.h"
13 
14 struct hdmi_connector {
15 	struct drm_connector base;
16 	struct hdmi *hdmi;
17 	struct work_struct hpd_work;
18 };
19 #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
20 
msm_hdmi_phy_reset(struct hdmi * hdmi)21 static void msm_hdmi_phy_reset(struct hdmi *hdmi)
22 {
23 	unsigned int val;
24 
25 	val = hdmi_read(hdmi, REG_HDMI_PHY_CTRL);
26 
27 	if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
28 		/* pull low */
29 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
30 				val & ~HDMI_PHY_CTRL_SW_RESET);
31 	} else {
32 		/* pull high */
33 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
34 				val | HDMI_PHY_CTRL_SW_RESET);
35 	}
36 
37 	if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
38 		/* pull low */
39 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
40 				val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
41 	} else {
42 		/* pull high */
43 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
44 				val | HDMI_PHY_CTRL_SW_RESET_PLL);
45 	}
46 
47 	msleep(100);
48 
49 	if (val & HDMI_PHY_CTRL_SW_RESET_LOW) {
50 		/* pull high */
51 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
52 				val | HDMI_PHY_CTRL_SW_RESET);
53 	} else {
54 		/* pull low */
55 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
56 				val & ~HDMI_PHY_CTRL_SW_RESET);
57 	}
58 
59 	if (val & HDMI_PHY_CTRL_SW_RESET_PLL_LOW) {
60 		/* pull high */
61 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
62 				val | HDMI_PHY_CTRL_SW_RESET_PLL);
63 	} else {
64 		/* pull low */
65 		hdmi_write(hdmi, REG_HDMI_PHY_CTRL,
66 				val & ~HDMI_PHY_CTRL_SW_RESET_PLL);
67 	}
68 }
69 
gpio_config(struct hdmi * hdmi,bool on)70 static int gpio_config(struct hdmi *hdmi, bool on)
71 {
72 	const struct hdmi_platform_config *config = hdmi->config;
73 	int i;
74 
75 	if (on) {
76 		for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
77 			struct hdmi_gpio_data gpio = config->gpios[i];
78 
79 			if (gpio.gpiod) {
80 				if (gpio.output) {
81 					gpiod_direction_output(gpio.gpiod,
82 							       gpio.value);
83 				} else {
84 					gpiod_direction_input(gpio.gpiod);
85 					gpiod_set_value_cansleep(gpio.gpiod,
86 								 gpio.value);
87 				}
88 			}
89 		}
90 
91 		DBG("gpio on");
92 	} else {
93 		for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
94 			struct hdmi_gpio_data gpio = config->gpios[i];
95 
96 			if (!gpio.gpiod)
97 				continue;
98 
99 			if (gpio.output) {
100 				int value = gpio.value ? 0 : 1;
101 
102 				gpiod_set_value_cansleep(gpio.gpiod, value);
103 			}
104 		}
105 
106 		DBG("gpio off");
107 	}
108 
109 	return 0;
110 }
111 
enable_hpd_clocks(struct hdmi * hdmi,bool enable)112 static void enable_hpd_clocks(struct hdmi *hdmi, bool enable)
113 {
114 	const struct hdmi_platform_config *config = hdmi->config;
115 	struct device *dev = &hdmi->pdev->dev;
116 	int i, ret;
117 
118 	if (enable) {
119 		for (i = 0; i < config->hpd_clk_cnt; i++) {
120 			if (config->hpd_freq && config->hpd_freq[i]) {
121 				ret = clk_set_rate(hdmi->hpd_clks[i],
122 						   config->hpd_freq[i]);
123 				if (ret)
124 					dev_warn(dev,
125 						 "failed to set clk %s (%d)\n",
126 						 config->hpd_clk_names[i], ret);
127 			}
128 
129 			ret = clk_prepare_enable(hdmi->hpd_clks[i]);
130 			if (ret) {
131 				DRM_DEV_ERROR(dev,
132 					"failed to enable hpd clk: %s (%d)\n",
133 					config->hpd_clk_names[i], ret);
134 			}
135 		}
136 	} else {
137 		for (i = config->hpd_clk_cnt - 1; i >= 0; i--)
138 			clk_disable_unprepare(hdmi->hpd_clks[i]);
139 	}
140 }
141 
msm_hdmi_hpd_enable(struct drm_connector * connector)142 int msm_hdmi_hpd_enable(struct drm_connector *connector)
143 {
144 	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
145 	struct hdmi *hdmi = hdmi_connector->hdmi;
146 	const struct hdmi_platform_config *config = hdmi->config;
147 	struct device *dev = &hdmi->pdev->dev;
148 	uint32_t hpd_ctrl;
149 	int ret;
150 	unsigned long flags;
151 
152 	ret = regulator_bulk_enable(config->hpd_reg_cnt, hdmi->hpd_regs);
153 	if (ret) {
154 		DRM_DEV_ERROR(dev, "failed to enable hpd regulators: %d\n", ret);
155 		goto fail;
156 	}
157 
158 	ret = pinctrl_pm_select_default_state(dev);
159 	if (ret) {
160 		DRM_DEV_ERROR(dev, "pinctrl state chg failed: %d\n", ret);
161 		goto fail;
162 	}
163 
164 	ret = gpio_config(hdmi, true);
165 	if (ret) {
166 		DRM_DEV_ERROR(dev, "failed to configure GPIOs: %d\n", ret);
167 		goto fail;
168 	}
169 
170 	pm_runtime_get_sync(dev);
171 	enable_hpd_clocks(hdmi, true);
172 
173 	msm_hdmi_set_mode(hdmi, false);
174 	msm_hdmi_phy_reset(hdmi);
175 	msm_hdmi_set_mode(hdmi, true);
176 
177 	hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
178 
179 	/* enable HPD events: */
180 	hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
181 			HDMI_HPD_INT_CTRL_INT_CONNECT |
182 			HDMI_HPD_INT_CTRL_INT_EN);
183 
184 	/* set timeout to 4.1ms (max) for hardware debounce */
185 	spin_lock_irqsave(&hdmi->reg_lock, flags);
186 	hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
187 	hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
188 
189 	/* Toggle HPD circuit to trigger HPD sense */
190 	hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
191 			~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
192 	hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
193 			HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
194 	spin_unlock_irqrestore(&hdmi->reg_lock, flags);
195 
196 	return 0;
197 
198 fail:
199 	return ret;
200 }
201 
hdp_disable(struct hdmi_connector * hdmi_connector)202 static void hdp_disable(struct hdmi_connector *hdmi_connector)
203 {
204 	struct hdmi *hdmi = hdmi_connector->hdmi;
205 	const struct hdmi_platform_config *config = hdmi->config;
206 	struct device *dev = &hdmi->pdev->dev;
207 	int ret;
208 
209 	/* Disable HPD interrupt */
210 	hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
211 
212 	msm_hdmi_set_mode(hdmi, false);
213 
214 	enable_hpd_clocks(hdmi, false);
215 	pm_runtime_put_autosuspend(dev);
216 
217 	ret = gpio_config(hdmi, false);
218 	if (ret)
219 		dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
220 
221 	ret = pinctrl_pm_select_sleep_state(dev);
222 	if (ret)
223 		dev_warn(dev, "pinctrl state chg failed: %d\n", ret);
224 
225 	ret = regulator_bulk_disable(config->hpd_reg_cnt, hdmi->hpd_regs);
226 	if (ret)
227 		dev_warn(dev, "failed to disable hpd regulator: %d\n", ret);
228 }
229 
230 static void
msm_hdmi_hotplug_work(struct work_struct * work)231 msm_hdmi_hotplug_work(struct work_struct *work)
232 {
233 	struct hdmi_connector *hdmi_connector =
234 		container_of(work, struct hdmi_connector, hpd_work);
235 	struct drm_connector *connector = &hdmi_connector->base;
236 	drm_helper_hpd_irq_event(connector->dev);
237 }
238 
msm_hdmi_connector_irq(struct drm_connector * connector)239 void msm_hdmi_connector_irq(struct drm_connector *connector)
240 {
241 	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
242 	struct hdmi *hdmi = hdmi_connector->hdmi;
243 	uint32_t hpd_int_status, hpd_int_ctrl;
244 
245 	/* Process HPD: */
246 	hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
247 	hpd_int_ctrl   = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
248 
249 	if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
250 			(hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
251 		bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
252 
253 		/* ack & disable (temporarily) HPD events: */
254 		hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
255 			HDMI_HPD_INT_CTRL_INT_ACK);
256 
257 		DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
258 
259 		/* detect disconnect if we are connected or visa versa: */
260 		hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
261 		if (!detected)
262 			hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
263 		hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
264 
265 		queue_work(hdmi->workq, &hdmi_connector->hpd_work);
266 	}
267 }
268 
detect_reg(struct hdmi * hdmi)269 static enum drm_connector_status detect_reg(struct hdmi *hdmi)
270 {
271 	uint32_t hpd_int_status;
272 
273 	pm_runtime_get_sync(&hdmi->pdev->dev);
274 	enable_hpd_clocks(hdmi, true);
275 
276 	hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
277 
278 	enable_hpd_clocks(hdmi, false);
279 	pm_runtime_put_autosuspend(&hdmi->pdev->dev);
280 
281 	return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
282 			connector_status_connected : connector_status_disconnected;
283 }
284 
285 #define HPD_GPIO_INDEX	2
detect_gpio(struct hdmi * hdmi)286 static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
287 {
288 	const struct hdmi_platform_config *config = hdmi->config;
289 	struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
290 
291 	return gpiod_get_value(hpd_gpio.gpiod) ?
292 			connector_status_connected :
293 			connector_status_disconnected;
294 }
295 
hdmi_connector_detect(struct drm_connector * connector,bool force)296 static enum drm_connector_status hdmi_connector_detect(
297 		struct drm_connector *connector, bool force)
298 {
299 	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
300 	struct hdmi *hdmi = hdmi_connector->hdmi;
301 	const struct hdmi_platform_config *config = hdmi->config;
302 	struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
303 	enum drm_connector_status stat_gpio, stat_reg;
304 	int retry = 20;
305 
306 	/*
307 	 * some platforms may not have hpd gpio. Rely only on the status
308 	 * provided by REG_HDMI_HPD_INT_STATUS in this case.
309 	 */
310 	if (!hpd_gpio.gpiod)
311 		return detect_reg(hdmi);
312 
313 	do {
314 		stat_gpio = detect_gpio(hdmi);
315 		stat_reg  = detect_reg(hdmi);
316 
317 		if (stat_gpio == stat_reg)
318 			break;
319 
320 		mdelay(10);
321 	} while (--retry);
322 
323 	/* the status we get from reading gpio seems to be more reliable,
324 	 * so trust that one the most if we didn't manage to get hdmi and
325 	 * gpio status to agree:
326 	 */
327 	if (stat_gpio != stat_reg) {
328 		DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
329 		DBG("hpd gpio tells us: %d", stat_gpio);
330 	}
331 
332 	return stat_gpio;
333 }
334 
hdmi_connector_destroy(struct drm_connector * connector)335 static void hdmi_connector_destroy(struct drm_connector *connector)
336 {
337 	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
338 
339 	hdp_disable(hdmi_connector);
340 
341 	drm_connector_cleanup(connector);
342 
343 	kfree(hdmi_connector);
344 }
345 
msm_hdmi_connector_get_modes(struct drm_connector * connector)346 static int msm_hdmi_connector_get_modes(struct drm_connector *connector)
347 {
348 	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
349 	struct hdmi *hdmi = hdmi_connector->hdmi;
350 	struct edid *edid;
351 	uint32_t hdmi_ctrl;
352 	int ret = 0;
353 
354 	hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
355 	hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
356 
357 	edid = drm_get_edid(connector, hdmi->i2c);
358 
359 	hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
360 
361 	hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
362 	drm_connector_update_edid_property(connector, edid);
363 
364 	if (edid) {
365 		ret = drm_add_edid_modes(connector, edid);
366 		kfree(edid);
367 	}
368 
369 	return ret;
370 }
371 
msm_hdmi_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)372 static int msm_hdmi_connector_mode_valid(struct drm_connector *connector,
373 				 struct drm_display_mode *mode)
374 {
375 	struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
376 	struct hdmi *hdmi = hdmi_connector->hdmi;
377 	const struct hdmi_platform_config *config = hdmi->config;
378 	struct msm_drm_private *priv = connector->dev->dev_private;
379 	struct msm_kms *kms = priv->kms;
380 	long actual, requested;
381 
382 	requested = 1000 * mode->clock;
383 	actual = kms->funcs->round_pixclk(kms,
384 			requested, hdmi_connector->hdmi->encoder);
385 
386 	/* for mdp5/apq8074, we manage our own pixel clk (as opposed to
387 	 * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
388 	 * instead):
389 	 */
390 	if (config->pwr_clk_cnt > 0)
391 		actual = clk_round_rate(hdmi->pwr_clks[0], actual);
392 
393 	DBG("requested=%ld, actual=%ld", requested, actual);
394 
395 	if (actual != requested)
396 		return MODE_CLOCK_RANGE;
397 
398 	return 0;
399 }
400 
401 static const struct drm_connector_funcs hdmi_connector_funcs = {
402 	.detect = hdmi_connector_detect,
403 	.fill_modes = drm_helper_probe_single_connector_modes,
404 	.destroy = hdmi_connector_destroy,
405 	.reset = drm_atomic_helper_connector_reset,
406 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
407 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
408 };
409 
410 static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs = {
411 	.get_modes = msm_hdmi_connector_get_modes,
412 	.mode_valid = msm_hdmi_connector_mode_valid,
413 };
414 
415 /* initialize connector */
msm_hdmi_connector_init(struct hdmi * hdmi)416 struct drm_connector *msm_hdmi_connector_init(struct hdmi *hdmi)
417 {
418 	struct drm_connector *connector = NULL;
419 	struct hdmi_connector *hdmi_connector;
420 
421 	hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
422 	if (!hdmi_connector)
423 		return ERR_PTR(-ENOMEM);
424 
425 	hdmi_connector->hdmi = hdmi;
426 	INIT_WORK(&hdmi_connector->hpd_work, msm_hdmi_hotplug_work);
427 
428 	connector = &hdmi_connector->base;
429 
430 	drm_connector_init_with_ddc(hdmi->dev, connector,
431 				    &hdmi_connector_funcs,
432 				    DRM_MODE_CONNECTOR_HDMIA,
433 				    hdmi->i2c);
434 	drm_connector_helper_add(connector, &msm_hdmi_connector_helper_funcs);
435 
436 	connector->polled = DRM_CONNECTOR_POLL_CONNECT |
437 			DRM_CONNECTOR_POLL_DISCONNECT;
438 
439 	connector->interlace_allowed = 0;
440 	connector->doublescan_allowed = 0;
441 
442 	drm_connector_attach_encoder(connector, hdmi->encoder);
443 
444 	return connector;
445 }
446