1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2021 Google Inc.
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.
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>
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>
22 struct atana33xc20_panel {
23 struct drm_panel base;
29 struct gpio_desc *hpd_gpio;
31 struct regulator *supply;
32 struct gpio_desc *el_on3_gpio;
36 ktime_t powered_off_time;
37 ktime_t powered_on_time;
38 ktime_t el_on3_off_time;
41 static inline struct atana33xc20_panel *to_atana33xc20(struct drm_panel *panel)
43 return container_of(panel, struct atana33xc20_panel, base);
46 static void atana33xc20_wait(ktime_t start_ktime, unsigned int min_ms)
48 ktime_t now_ktime, min_ktime;
50 min_ktime = ktime_add(start_ktime, ms_to_ktime(min_ms));
51 now_ktime = ktime_get();
53 if (ktime_before(now_ktime, min_ktime))
54 msleep(ktime_to_ms(ktime_sub(min_ktime, now_ktime)) + 1);
57 static int atana33xc20_suspend(struct device *dev)
59 struct atana33xc20_panel *p = dev_get_drvdata(dev);
63 * Note 3 (Example of power off sequence in detail) in spec
64 * specifies to wait 150 ms after deasserting EL3_ON before
68 atana33xc20_wait(p->el_on3_off_time, 150);
70 ret = regulator_disable(p->supply);
73 p->powered_off_time = ktime_get();
74 p->el3_was_on = false;
79 static int atana33xc20_resume(struct device *dev)
81 struct atana33xc20_panel *p = dev_get_drvdata(dev);
82 bool hpd_asserted = false;
85 /* T12 (Power off time) is min 500 ms */
86 atana33xc20_wait(p->powered_off_time, 500);
88 ret = regulator_enable(p->supply);
91 p->powered_on_time = ktime_get();
94 * Handle HPD. Note: if HPD is hooked up to a dedicated pin on the
95 * eDP controller then "no_hpd" will be false _and_ "hpd_gpio" will be
96 * NULL. It's up to the controller driver to wait for HPD after
97 * preparing the panel in that case.
100 /* T3 VCC to HPD high is max 200 ms */
102 } else if (p->hpd_gpio) {
103 ret = readx_poll_timeout(gpiod_get_value_cansleep, p->hpd_gpio,
104 hpd_asserted, hpd_asserted,
107 dev_warn(dev, "Timeout waiting for HPD\n");
113 static int atana33xc20_disable(struct drm_panel *panel)
115 struct atana33xc20_panel *p = to_atana33xc20(panel);
117 /* Disabling when already disabled is a no-op */
121 gpiod_set_value_cansleep(p->el_on3_gpio, 0);
122 p->el_on3_off_time = ktime_get();
126 * Keep track of the fact that EL_ON3 was on but we haven't power
127 * cycled yet. This lets us know that "el_on3_off_time" is recent (we
128 * don't need to worry about ktime wraparounds) and also makes it
129 * obvious if we try to enable again without a power cycle (see the
130 * warning in atana33xc20_enable()).
132 p->el3_was_on = true;
135 * Sleeping 20 ms here (after setting the GPIO) avoids a glitch when
143 static int atana33xc20_enable(struct drm_panel *panel)
145 struct atana33xc20_panel *p = to_atana33xc20(panel);
147 /* Enabling when already enabled is a no-op */
152 * Once EL_ON3 drops we absolutely need a power cycle before the next
153 * enable or the backlight will never come on again. The code ensures
154 * this because disable() is _always_ followed by unprepare() and
155 * unprepare() forces a suspend with pm_runtime_put_sync_suspend(),
156 * but let's track just to make sure since the requirement is so
159 if (WARN_ON(p->el3_was_on))
163 * Note 2 (Example of power on sequence in detail) in spec specifies
164 * to wait 400 ms after powering on before asserting EL3_on.
166 atana33xc20_wait(p->powered_on_time, 400);
168 gpiod_set_value_cansleep(p->el_on3_gpio, 1);
174 static int atana33xc20_unprepare(struct drm_panel *panel)
176 struct atana33xc20_panel *p = to_atana33xc20(panel);
179 /* Unpreparing when already unprepared is a no-op */
184 * Purposely do a put_sync, don't use autosuspend. The panel's tcon
185 * seems to sometimes crash when you stop giving it data and this is
186 * the best way to ensure it will come back.
188 * NOTE: we still want autosuspend for cases where we only turn on
189 * to get the EDID or otherwise send DP AUX commands to the panel.
191 ret = pm_runtime_put_sync_suspend(panel->dev);
199 static int atana33xc20_prepare(struct drm_panel *panel)
201 struct atana33xc20_panel *p = to_atana33xc20(panel);
204 /* Preparing when already prepared is a no-op */
208 ret = pm_runtime_get_sync(panel->dev);
210 pm_runtime_put_autosuspend(panel->dev);
218 static int atana33xc20_get_modes(struct drm_panel *panel,
219 struct drm_connector *connector)
221 struct atana33xc20_panel *p = to_atana33xc20(panel);
222 struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(panel->dev);
225 pm_runtime_get_sync(panel->dev);
228 p->edid = drm_get_edid(connector, &aux_ep->aux->ddc);
229 num = drm_add_edid_modes(connector, p->edid);
231 pm_runtime_mark_last_busy(panel->dev);
232 pm_runtime_put_autosuspend(panel->dev);
237 static const struct drm_panel_funcs atana33xc20_funcs = {
238 .disable = atana33xc20_disable,
239 .enable = atana33xc20_enable,
240 .unprepare = atana33xc20_unprepare,
241 .prepare = atana33xc20_prepare,
242 .get_modes = atana33xc20_get_modes,
245 static void atana33xc20_runtime_disable(void *data)
247 pm_runtime_disable(data);
250 static void atana33xc20_dont_use_autosuspend(void *data)
252 pm_runtime_dont_use_autosuspend(data);
255 static int atana33xc20_probe(struct dp_aux_ep_device *aux_ep)
257 struct atana33xc20_panel *panel;
258 struct device *dev = &aux_ep->dev;
261 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
264 dev_set_drvdata(dev, panel);
266 panel->supply = devm_regulator_get(dev, "power");
267 if (IS_ERR(panel->supply))
268 return dev_err_probe(dev, PTR_ERR(panel->supply),
269 "Failed to get power supply\n");
271 panel->el_on3_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
272 if (IS_ERR(panel->el_on3_gpio))
273 return dev_err_probe(dev, PTR_ERR(panel->el_on3_gpio),
274 "Failed to get enable GPIO\n");
276 panel->no_hpd = of_property_read_bool(dev->of_node, "no-hpd");
277 if (!panel->no_hpd) {
278 panel->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
279 if (IS_ERR(panel->hpd_gpio))
280 return dev_err_probe(dev, PTR_ERR(panel->hpd_gpio),
281 "Failed to get HPD GPIO\n");
284 pm_runtime_enable(dev);
285 ret = devm_add_action_or_reset(dev, atana33xc20_runtime_disable, dev);
288 pm_runtime_set_autosuspend_delay(dev, 1000);
289 pm_runtime_use_autosuspend(dev);
290 ret = devm_add_action_or_reset(dev, atana33xc20_dont_use_autosuspend, dev);
294 drm_panel_init(&panel->base, dev, &atana33xc20_funcs, DRM_MODE_CONNECTOR_eDP);
296 pm_runtime_get_sync(dev);
297 ret = drm_panel_dp_aux_backlight(&panel->base, aux_ep->aux);
298 pm_runtime_mark_last_busy(dev);
299 pm_runtime_put_autosuspend(dev);
301 return dev_err_probe(dev, ret,
302 "failed to register dp aux backlight\n");
304 drm_panel_add(&panel->base);
309 static void atana33xc20_remove(struct dp_aux_ep_device *aux_ep)
311 struct device *dev = &aux_ep->dev;
312 struct atana33xc20_panel *panel = dev_get_drvdata(dev);
314 drm_panel_remove(&panel->base);
315 drm_panel_disable(&panel->base);
316 drm_panel_unprepare(&panel->base);
321 static void atana33xc20_shutdown(struct dp_aux_ep_device *aux_ep)
323 struct device *dev = &aux_ep->dev;
324 struct atana33xc20_panel *panel = dev_get_drvdata(dev);
326 drm_panel_disable(&panel->base);
327 drm_panel_unprepare(&panel->base);
330 static const struct of_device_id atana33xc20_dt_match[] = {
331 { .compatible = "samsung,atna33xc20", },
334 MODULE_DEVICE_TABLE(of, atana33xc20_dt_match);
336 static const struct dev_pm_ops atana33xc20_pm_ops = {
337 SET_RUNTIME_PM_OPS(atana33xc20_suspend, atana33xc20_resume, NULL)
338 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
339 pm_runtime_force_resume)
342 static struct dp_aux_ep_driver atana33xc20_driver = {
344 .name = "samsung_atana33xc20",
345 .of_match_table = atana33xc20_dt_match,
346 .pm = &atana33xc20_pm_ops,
348 .probe = atana33xc20_probe,
349 .remove = atana33xc20_remove,
350 .shutdown = atana33xc20_shutdown,
353 static int __init atana33xc20_init(void)
355 return dp_aux_dp_driver_register(&atana33xc20_driver);
357 module_init(atana33xc20_init);
359 static void __exit atana33xc20_exit(void)
361 dp_aux_dp_driver_unregister(&atana33xc20_driver);
363 module_exit(atana33xc20_exit);
365 MODULE_DESCRIPTION("Samsung ATANA33XC20 Panel Driver");
366 MODULE_LICENSE("GPL v2");