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