Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2021 Google Inc.
0004  *
0005  * Panel driver for the Samsung ATNA33XC20 panel. This panel can't be handled
0006  * by the DRM_PANEL_SIMPLE driver because its power sequencing is non-standard.
0007  */
0008 
0009 #include <linux/backlight.h>
0010 #include <linux/delay.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/iopoll.h>
0013 #include <linux/module.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/regulator/consumer.h>
0016 
0017 #include <drm/display/drm_dp_aux_bus.h>
0018 #include <drm/display/drm_dp_helper.h>
0019 #include <drm/drm_edid.h>
0020 #include <drm/drm_panel.h>
0021 
0022 /* T3 VCC to HPD high is max 200 ms */
0023 #define HPD_MAX_MS  200
0024 #define HPD_MAX_US  (HPD_MAX_MS * 1000)
0025 
0026 struct atana33xc20_panel {
0027     struct drm_panel base;
0028     bool prepared;
0029     bool enabled;
0030     bool el3_was_on;
0031 
0032     bool no_hpd;
0033     struct gpio_desc *hpd_gpio;
0034 
0035     struct regulator *supply;
0036     struct gpio_desc *el_on3_gpio;
0037     struct drm_dp_aux *aux;
0038 
0039     struct edid *edid;
0040 
0041     ktime_t powered_off_time;
0042     ktime_t powered_on_time;
0043     ktime_t el_on3_off_time;
0044 };
0045 
0046 static inline struct atana33xc20_panel *to_atana33xc20(struct drm_panel *panel)
0047 {
0048     return container_of(panel, struct atana33xc20_panel, base);
0049 }
0050 
0051 static void atana33xc20_wait(ktime_t start_ktime, unsigned int min_ms)
0052 {
0053     ktime_t now_ktime, min_ktime;
0054 
0055     min_ktime = ktime_add(start_ktime, ms_to_ktime(min_ms));
0056     now_ktime = ktime_get();
0057 
0058     if (ktime_before(now_ktime, min_ktime))
0059         msleep(ktime_to_ms(ktime_sub(min_ktime, now_ktime)) + 1);
0060 }
0061 
0062 static int atana33xc20_suspend(struct device *dev)
0063 {
0064     struct atana33xc20_panel *p = dev_get_drvdata(dev);
0065     int ret;
0066 
0067     /*
0068      * Note 3 (Example of power off sequence in detail) in spec
0069      * specifies to wait 150 ms after deasserting EL3_ON before
0070      * powering off.
0071      */
0072     if (p->el3_was_on)
0073         atana33xc20_wait(p->el_on3_off_time, 150);
0074 
0075     ret = regulator_disable(p->supply);
0076     if (ret)
0077         return ret;
0078     p->powered_off_time = ktime_get();
0079     p->el3_was_on = false;
0080 
0081     return 0;
0082 }
0083 
0084 static int atana33xc20_resume(struct device *dev)
0085 {
0086     struct atana33xc20_panel *p = dev_get_drvdata(dev);
0087     int hpd_asserted;
0088     int ret;
0089 
0090     /* T12 (Power off time) is min 500 ms */
0091     atana33xc20_wait(p->powered_off_time, 500);
0092 
0093     ret = regulator_enable(p->supply);
0094     if (ret)
0095         return ret;
0096     p->powered_on_time = ktime_get();
0097 
0098     if (p->no_hpd) {
0099         msleep(HPD_MAX_MS);
0100         return 0;
0101     }
0102 
0103     if (p->hpd_gpio) {
0104         ret = readx_poll_timeout(gpiod_get_value_cansleep, p->hpd_gpio,
0105                      hpd_asserted, hpd_asserted,
0106                      1000, HPD_MAX_US);
0107         if (hpd_asserted < 0)
0108             ret = hpd_asserted;
0109 
0110         if (ret)
0111             dev_warn(dev, "Error waiting for HPD GPIO: %d\n", ret);
0112 
0113         return ret;
0114     }
0115 
0116     if (p->aux->wait_hpd_asserted) {
0117         ret = p->aux->wait_hpd_asserted(p->aux, HPD_MAX_US);
0118 
0119         if (ret)
0120             dev_warn(dev, "Controller error waiting for HPD: %d\n", ret);
0121 
0122         return ret;
0123     }
0124 
0125     /*
0126      * Note that it's possible that no_hpd is false, hpd_gpio is
0127      * NULL, and wait_hpd_asserted is NULL. This is because
0128      * wait_hpd_asserted() is optional even if HPD is hooked up to
0129      * a dedicated pin on the eDP controller. In this case we just
0130      * assume that the controller driver will wait for HPD at the
0131      * right times.
0132      */
0133     return 0;
0134 }
0135 
0136 static int atana33xc20_disable(struct drm_panel *panel)
0137 {
0138     struct atana33xc20_panel *p = to_atana33xc20(panel);
0139 
0140     /* Disabling when already disabled is a no-op */
0141     if (!p->enabled)
0142         return 0;
0143 
0144     gpiod_set_value_cansleep(p->el_on3_gpio, 0);
0145     p->el_on3_off_time = ktime_get();
0146     p->enabled = false;
0147 
0148     /*
0149      * Keep track of the fact that EL_ON3 was on but we haven't power
0150      * cycled yet. This lets us know that "el_on3_off_time" is recent (we
0151      * don't need to worry about ktime wraparounds) and also makes it
0152      * obvious if we try to enable again without a power cycle (see the
0153      * warning in atana33xc20_enable()).
0154      */
0155     p->el3_was_on = true;
0156 
0157     /*
0158      * Sleeping 20 ms here (after setting the GPIO) avoids a glitch when
0159      * powering off.
0160      */
0161     msleep(20);
0162 
0163     return 0;
0164 }
0165 
0166 static int atana33xc20_enable(struct drm_panel *panel)
0167 {
0168     struct atana33xc20_panel *p = to_atana33xc20(panel);
0169 
0170     /* Enabling when already enabled is a no-op */
0171     if (p->enabled)
0172         return 0;
0173 
0174     /*
0175      * Once EL_ON3 drops we absolutely need a power cycle before the next
0176      * enable or the backlight will never come on again. The code ensures
0177      * this because disable() is _always_ followed by unprepare() and
0178      * unprepare() forces a suspend with pm_runtime_put_sync_suspend(),
0179      * but let's track just to make sure since the requirement is so
0180      * non-obvious.
0181      */
0182     if (WARN_ON(p->el3_was_on))
0183         return -EIO;
0184 
0185     /*
0186      * Note 2 (Example of power on sequence in detail) in spec specifies
0187      * to wait 400 ms after powering on before asserting EL3_on.
0188      */
0189     atana33xc20_wait(p->powered_on_time, 400);
0190 
0191     gpiod_set_value_cansleep(p->el_on3_gpio, 1);
0192     p->enabled = true;
0193 
0194     return 0;
0195 }
0196 
0197 static int atana33xc20_unprepare(struct drm_panel *panel)
0198 {
0199     struct atana33xc20_panel *p = to_atana33xc20(panel);
0200     int ret;
0201 
0202     /* Unpreparing when already unprepared is a no-op */
0203     if (!p->prepared)
0204         return 0;
0205 
0206     /*
0207      * Purposely do a put_sync, don't use autosuspend. The panel's tcon
0208      * seems to sometimes crash when you stop giving it data and this is
0209      * the best way to ensure it will come back.
0210      *
0211      * NOTE: we still want autosuspend for cases where we only turn on
0212      * to get the EDID or otherwise send DP AUX commands to the panel.
0213      */
0214     ret = pm_runtime_put_sync_suspend(panel->dev);
0215     if (ret < 0)
0216         return ret;
0217     p->prepared = false;
0218 
0219     return 0;
0220 }
0221 
0222 static int atana33xc20_prepare(struct drm_panel *panel)
0223 {
0224     struct atana33xc20_panel *p = to_atana33xc20(panel);
0225     int ret;
0226 
0227     /* Preparing when already prepared is a no-op */
0228     if (p->prepared)
0229         return 0;
0230 
0231     ret = pm_runtime_get_sync(panel->dev);
0232     if (ret < 0) {
0233         pm_runtime_put_autosuspend(panel->dev);
0234         return ret;
0235     }
0236     p->prepared = true;
0237 
0238     return 0;
0239 }
0240 
0241 static int atana33xc20_get_modes(struct drm_panel *panel,
0242                  struct drm_connector *connector)
0243 {
0244     struct atana33xc20_panel *p = to_atana33xc20(panel);
0245     struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(panel->dev);
0246     int num = 0;
0247 
0248     pm_runtime_get_sync(panel->dev);
0249 
0250     if (!p->edid)
0251         p->edid = drm_get_edid(connector, &aux_ep->aux->ddc);
0252     num = drm_add_edid_modes(connector, p->edid);
0253 
0254     pm_runtime_mark_last_busy(panel->dev);
0255     pm_runtime_put_autosuspend(panel->dev);
0256 
0257     return num;
0258 }
0259 
0260 static const struct drm_panel_funcs atana33xc20_funcs = {
0261     .disable = atana33xc20_disable,
0262     .enable = atana33xc20_enable,
0263     .unprepare = atana33xc20_unprepare,
0264     .prepare = atana33xc20_prepare,
0265     .get_modes = atana33xc20_get_modes,
0266 };
0267 
0268 static void atana33xc20_runtime_disable(void *data)
0269 {
0270     pm_runtime_disable(data);
0271 }
0272 
0273 static void atana33xc20_dont_use_autosuspend(void *data)
0274 {
0275     pm_runtime_dont_use_autosuspend(data);
0276 }
0277 
0278 static int atana33xc20_probe(struct dp_aux_ep_device *aux_ep)
0279 {
0280     struct atana33xc20_panel *panel;
0281     struct device *dev = &aux_ep->dev;
0282     int ret;
0283 
0284     panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
0285     if (!panel)
0286         return -ENOMEM;
0287     dev_set_drvdata(dev, panel);
0288 
0289     panel->aux = aux_ep->aux;
0290 
0291     panel->supply = devm_regulator_get(dev, "power");
0292     if (IS_ERR(panel->supply))
0293         return dev_err_probe(dev, PTR_ERR(panel->supply),
0294                      "Failed to get power supply\n");
0295 
0296     panel->el_on3_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
0297     if (IS_ERR(panel->el_on3_gpio))
0298         return dev_err_probe(dev, PTR_ERR(panel->el_on3_gpio),
0299                      "Failed to get enable GPIO\n");
0300 
0301     panel->no_hpd = of_property_read_bool(dev->of_node, "no-hpd");
0302     if (!panel->no_hpd) {
0303         panel->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
0304         if (IS_ERR(panel->hpd_gpio))
0305             return dev_err_probe(dev, PTR_ERR(panel->hpd_gpio),
0306                          "Failed to get HPD GPIO\n");
0307     }
0308 
0309     pm_runtime_enable(dev);
0310     ret = devm_add_action_or_reset(dev,  atana33xc20_runtime_disable, dev);
0311     if (ret)
0312         return ret;
0313     pm_runtime_set_autosuspend_delay(dev, 1000);
0314     pm_runtime_use_autosuspend(dev);
0315     ret = devm_add_action_or_reset(dev,  atana33xc20_dont_use_autosuspend, dev);
0316     if (ret)
0317         return ret;
0318 
0319     drm_panel_init(&panel->base, dev, &atana33xc20_funcs, DRM_MODE_CONNECTOR_eDP);
0320 
0321     pm_runtime_get_sync(dev);
0322     ret = drm_panel_dp_aux_backlight(&panel->base, aux_ep->aux);
0323     pm_runtime_mark_last_busy(dev);
0324     pm_runtime_put_autosuspend(dev);
0325     if (ret)
0326         return dev_err_probe(dev, ret,
0327                      "failed to register dp aux backlight\n");
0328 
0329     drm_panel_add(&panel->base);
0330 
0331     return 0;
0332 }
0333 
0334 static void atana33xc20_remove(struct dp_aux_ep_device *aux_ep)
0335 {
0336     struct device *dev = &aux_ep->dev;
0337     struct atana33xc20_panel *panel = dev_get_drvdata(dev);
0338 
0339     drm_panel_remove(&panel->base);
0340     drm_panel_disable(&panel->base);
0341     drm_panel_unprepare(&panel->base);
0342 
0343     kfree(panel->edid);
0344 }
0345 
0346 static void atana33xc20_shutdown(struct dp_aux_ep_device *aux_ep)
0347 {
0348     struct device *dev = &aux_ep->dev;
0349     struct atana33xc20_panel *panel = dev_get_drvdata(dev);
0350 
0351     drm_panel_disable(&panel->base);
0352     drm_panel_unprepare(&panel->base);
0353 }
0354 
0355 static const struct of_device_id atana33xc20_dt_match[] = {
0356     { .compatible = "samsung,atna33xc20", },
0357     { /* sentinal */ }
0358 };
0359 MODULE_DEVICE_TABLE(of, atana33xc20_dt_match);
0360 
0361 static const struct dev_pm_ops atana33xc20_pm_ops = {
0362     SET_RUNTIME_PM_OPS(atana33xc20_suspend, atana33xc20_resume, NULL)
0363     SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
0364                 pm_runtime_force_resume)
0365 };
0366 
0367 static struct dp_aux_ep_driver atana33xc20_driver = {
0368     .driver = {
0369         .name       = "samsung_atana33xc20",
0370         .of_match_table = atana33xc20_dt_match,
0371         .pm     = &atana33xc20_pm_ops,
0372     },
0373     .probe = atana33xc20_probe,
0374     .remove = atana33xc20_remove,
0375     .shutdown = atana33xc20_shutdown,
0376 };
0377 
0378 static int __init atana33xc20_init(void)
0379 {
0380     return dp_aux_dp_driver_register(&atana33xc20_driver);
0381 }
0382 module_init(atana33xc20_init);
0383 
0384 static void __exit atana33xc20_exit(void)
0385 {
0386     dp_aux_dp_driver_unregister(&atana33xc20_driver);
0387 }
0388 module_exit(atana33xc20_exit);
0389 
0390 MODULE_DESCRIPTION("Samsung ATANA33XC20 Panel Driver");
0391 MODULE_LICENSE("GPL v2");