xref: /linux/drivers/gpu/drm/panel/panel-samsung-atna33xc20.c (revision daa2be74b1b2302004945b2a5e32424e177cc7da)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2021 Google Inc.
4  *
5  * Panel driver for the Samsung ATNA33XC20 panel. This panel can't be handled
6  * by the DRM_PANEL_SIMPLE driver because its power sequencing is non-standard.
7  */
8 
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/iopoll.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regulator/consumer.h>
16 
17 #include <drm/display/drm_dp_aux_bus.h>
18 #include <drm/display/drm_dp_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_panel.h>
21 
22 /* T3 VCC to HPD high is max 200 ms */
23 #define HPD_MAX_MS	200
24 #define HPD_MAX_US	(HPD_MAX_MS * 1000)
25 
26 struct atana33xc20_panel {
27 	struct drm_panel base;
28 	bool el3_was_on;
29 
30 	bool no_hpd;
31 	struct gpio_desc *hpd_gpio;
32 
33 	struct regulator *supply;
34 	struct gpio_desc *el_on3_gpio;
35 	struct drm_dp_aux *aux;
36 
37 	const struct drm_edid *drm_edid;
38 
39 	ktime_t powered_off_time;
40 	ktime_t powered_on_time;
41 	ktime_t el_on3_off_time;
42 };
43 
44 static inline struct atana33xc20_panel *to_atana33xc20(struct drm_panel *panel)
45 {
46 	return container_of(panel, struct atana33xc20_panel, base);
47 }
48 
49 static void atana33xc20_wait(ktime_t start_ktime, unsigned int min_ms)
50 {
51 	ktime_t now_ktime, min_ktime;
52 
53 	min_ktime = ktime_add(start_ktime, ms_to_ktime(min_ms));
54 	now_ktime = ktime_get_boottime();
55 
56 	if (ktime_before(now_ktime, min_ktime))
57 		msleep(ktime_to_ms(ktime_sub(min_ktime, now_ktime)) + 1);
58 }
59 
60 static int atana33xc20_suspend(struct device *dev)
61 {
62 	struct atana33xc20_panel *p = dev_get_drvdata(dev);
63 	int ret;
64 
65 	/*
66 	 * Note 3 (Example of power off sequence in detail) in spec
67 	 * specifies to wait 150 ms after deasserting EL3_ON before
68 	 * powering off.
69 	 */
70 	if (p->el3_was_on)
71 		atana33xc20_wait(p->el_on3_off_time, 150);
72 
73 	drm_dp_dpcd_set_powered(p->aux, false);
74 	ret = regulator_disable(p->supply);
75 	if (ret)
76 		return ret;
77 	p->powered_off_time = ktime_get_boottime();
78 	p->el3_was_on = false;
79 
80 	return 0;
81 }
82 
83 static int atana33xc20_resume(struct device *dev)
84 {
85 	struct atana33xc20_panel *p = dev_get_drvdata(dev);
86 	int hpd_asserted;
87 	int ret;
88 
89 	/* T12 (Power off time) is min 500 ms */
90 	atana33xc20_wait(p->powered_off_time, 500);
91 
92 	ret = regulator_enable(p->supply);
93 	if (ret)
94 		return ret;
95 	drm_dp_dpcd_set_powered(p->aux, true);
96 	p->powered_on_time = ktime_get_boottime();
97 
98 	if (p->no_hpd) {
99 		msleep(HPD_MAX_MS);
100 		return 0;
101 	}
102 
103 	if (p->hpd_gpio) {
104 		ret = readx_poll_timeout(gpiod_get_value_cansleep, p->hpd_gpio,
105 					 hpd_asserted, hpd_asserted,
106 					 1000, HPD_MAX_US);
107 		if (hpd_asserted < 0)
108 			ret = hpd_asserted;
109 
110 		if (ret) {
111 			dev_warn(dev, "Error waiting for HPD GPIO: %d\n", ret);
112 			goto error;
113 		}
114 	} else if (p->aux->wait_hpd_asserted) {
115 		ret = p->aux->wait_hpd_asserted(p->aux, HPD_MAX_US);
116 
117 		if (ret) {
118 			dev_warn(dev, "Controller error waiting for HPD: %d\n", ret);
119 			goto error;
120 		}
121 	}
122 
123 	/*
124 	 * Note that it's possible that no_hpd is false, hpd_gpio is
125 	 * NULL, and wait_hpd_asserted is NULL. This is because
126 	 * wait_hpd_asserted() is optional even if HPD is hooked up to
127 	 * a dedicated pin on the eDP controller. In this case we just
128 	 * assume that the controller driver will wait for HPD at the
129 	 * right times.
130 	 */
131 	return 0;
132 
133 error:
134 	drm_dp_dpcd_set_powered(p->aux, false);
135 	regulator_disable(p->supply);
136 
137 	return ret;
138 }
139 
140 static int atana33xc20_disable(struct drm_panel *panel)
141 {
142 	struct atana33xc20_panel *p = to_atana33xc20(panel);
143 
144 	gpiod_set_value_cansleep(p->el_on3_gpio, 0);
145 	p->el_on3_off_time = ktime_get_boottime();
146 
147 	/*
148 	 * Keep track of the fact that EL_ON3 was on but we haven't power
149 	 * cycled yet. This lets us know that "el_on3_off_time" is recent (we
150 	 * don't need to worry about ktime wraparounds) and also makes it
151 	 * obvious if we try to enable again without a power cycle (see the
152 	 * warning in atana33xc20_enable()).
153 	 */
154 	p->el3_was_on = true;
155 
156 	/*
157 	 * Sleeping 20 ms here (after setting the GPIO) avoids a glitch when
158 	 * powering off.
159 	 */
160 	msleep(20);
161 
162 	return 0;
163 }
164 
165 static int atana33xc20_enable(struct drm_panel *panel)
166 {
167 	struct atana33xc20_panel *p = to_atana33xc20(panel);
168 
169 	/*
170 	 * Once EL_ON3 drops we absolutely need a power cycle before the next
171 	 * enable or the backlight will never come on again. The code ensures
172 	 * this because disable() is _always_ followed by unprepare() and
173 	 * unprepare() forces a suspend with pm_runtime_put_sync_suspend(),
174 	 * but let's track just to make sure since the requirement is so
175 	 * non-obvious.
176 	 */
177 	if (WARN_ON(p->el3_was_on))
178 		return -EIO;
179 
180 	/*
181 	 * Note 2 (Example of power on sequence in detail) in spec specifies
182 	 * to wait 400 ms after powering on before asserting EL3_on.
183 	 */
184 	atana33xc20_wait(p->powered_on_time, 400);
185 
186 	gpiod_set_value_cansleep(p->el_on3_gpio, 1);
187 
188 	return 0;
189 }
190 
191 static int atana33xc20_unprepare(struct drm_panel *panel)
192 {
193 	int ret;
194 
195 	/*
196 	 * Purposely do a put_sync, don't use autosuspend. The panel's tcon
197 	 * seems to sometimes crash when you stop giving it data and this is
198 	 * the best way to ensure it will come back.
199 	 *
200 	 * NOTE: we still want autosuspend for cases where we only turn on
201 	 * to get the EDID or otherwise send DP AUX commands to the panel.
202 	 */
203 	ret = pm_runtime_put_sync_suspend(panel->dev);
204 	if (ret < 0)
205 		return ret;
206 
207 	return 0;
208 }
209 
210 static int atana33xc20_prepare(struct drm_panel *panel)
211 {
212 	int ret;
213 
214 	ret = pm_runtime_get_sync(panel->dev);
215 	if (ret < 0) {
216 		pm_runtime_put_autosuspend(panel->dev);
217 		return ret;
218 	}
219 
220 	return 0;
221 }
222 
223 static int atana33xc20_get_modes(struct drm_panel *panel,
224 				 struct drm_connector *connector)
225 {
226 	struct atana33xc20_panel *p = to_atana33xc20(panel);
227 	struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(panel->dev);
228 	int num = 0;
229 
230 	pm_runtime_get_sync(panel->dev);
231 
232 	if (!p->drm_edid)
233 		p->drm_edid = drm_edid_read_ddc(connector, &aux_ep->aux->ddc);
234 
235 	drm_edid_connector_update(connector, p->drm_edid);
236 
237 	num = drm_edid_connector_add_modes(connector);
238 
239 	pm_runtime_mark_last_busy(panel->dev);
240 	pm_runtime_put_autosuspend(panel->dev);
241 
242 	return num;
243 }
244 
245 static const struct drm_panel_funcs atana33xc20_funcs = {
246 	.disable = atana33xc20_disable,
247 	.enable = atana33xc20_enable,
248 	.unprepare = atana33xc20_unprepare,
249 	.prepare = atana33xc20_prepare,
250 	.get_modes = atana33xc20_get_modes,
251 };
252 
253 static void atana33xc20_runtime_disable(void *data)
254 {
255 	pm_runtime_disable(data);
256 }
257 
258 static void atana33xc20_dont_use_autosuspend(void *data)
259 {
260 	pm_runtime_dont_use_autosuspend(data);
261 }
262 
263 static int atana33xc20_probe(struct dp_aux_ep_device *aux_ep)
264 {
265 	struct atana33xc20_panel *panel;
266 	struct device *dev = &aux_ep->dev;
267 	int ret;
268 
269 	panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
270 	if (!panel)
271 		return -ENOMEM;
272 	dev_set_drvdata(dev, panel);
273 
274 	panel->aux = aux_ep->aux;
275 
276 	panel->supply = devm_regulator_get(dev, "power");
277 	if (IS_ERR(panel->supply))
278 		return dev_err_probe(dev, PTR_ERR(panel->supply),
279 				     "Failed to get power supply\n");
280 
281 	panel->el_on3_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
282 	if (IS_ERR(panel->el_on3_gpio))
283 		return dev_err_probe(dev, PTR_ERR(panel->el_on3_gpio),
284 				     "Failed to get enable GPIO\n");
285 
286 	panel->no_hpd = of_property_read_bool(dev->of_node, "no-hpd");
287 	if (!panel->no_hpd) {
288 		panel->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
289 		if (IS_ERR(panel->hpd_gpio))
290 			return dev_err_probe(dev, PTR_ERR(panel->hpd_gpio),
291 					     "Failed to get HPD GPIO\n");
292 	}
293 
294 	pm_runtime_enable(dev);
295 	ret = devm_add_action_or_reset(dev,  atana33xc20_runtime_disable, dev);
296 	if (ret)
297 		return ret;
298 	pm_runtime_set_autosuspend_delay(dev, 2000);
299 	pm_runtime_use_autosuspend(dev);
300 	ret = devm_add_action_or_reset(dev,  atana33xc20_dont_use_autosuspend, dev);
301 	if (ret)
302 		return ret;
303 
304 	drm_panel_init(&panel->base, dev, &atana33xc20_funcs, DRM_MODE_CONNECTOR_eDP);
305 
306 	pm_runtime_get_sync(dev);
307 	ret = drm_panel_dp_aux_backlight(&panel->base, aux_ep->aux);
308 	pm_runtime_mark_last_busy(dev);
309 	pm_runtime_put_autosuspend(dev);
310 
311 	/*
312 	 * Warn if we get an error, but don't consider it fatal. Having
313 	 * a panel where we can't control the backlight is better than
314 	 * no panel.
315 	 */
316 	if (ret)
317 		dev_warn(dev, "failed to register dp aux backlight: %d\n", ret);
318 
319 	drm_panel_add(&panel->base);
320 
321 	return 0;
322 }
323 
324 static void atana33xc20_remove(struct dp_aux_ep_device *aux_ep)
325 {
326 	struct device *dev = &aux_ep->dev;
327 	struct atana33xc20_panel *panel = dev_get_drvdata(dev);
328 
329 	drm_panel_remove(&panel->base);
330 
331 	drm_edid_free(panel->drm_edid);
332 }
333 
334 static const struct of_device_id atana33xc20_dt_match[] = {
335 	{ .compatible = "samsung,atna33xc20", },
336 	{ /* sentinal */ }
337 };
338 MODULE_DEVICE_TABLE(of, atana33xc20_dt_match);
339 
340 static const struct dev_pm_ops atana33xc20_pm_ops = {
341 	SET_RUNTIME_PM_OPS(atana33xc20_suspend, atana33xc20_resume, NULL)
342 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
343 				pm_runtime_force_resume)
344 };
345 
346 static struct dp_aux_ep_driver atana33xc20_driver = {
347 	.driver = {
348 		.name		= "samsung_atana33xc20",
349 		.of_match_table = atana33xc20_dt_match,
350 		.pm		= &atana33xc20_pm_ops,
351 	},
352 	.probe = atana33xc20_probe,
353 	.remove = atana33xc20_remove,
354 };
355 
356 static int __init atana33xc20_init(void)
357 {
358 	return dp_aux_dp_driver_register(&atana33xc20_driver);
359 }
360 module_init(atana33xc20_init);
361 
362 static void __exit atana33xc20_exit(void)
363 {
364 	dp_aux_dp_driver_unregister(&atana33xc20_driver);
365 }
366 module_exit(atana33xc20_exit);
367 
368 MODULE_DESCRIPTION("Samsung ATANA33XC20 Panel Driver");
369 MODULE_LICENSE("GPL v2");
370