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