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 const struct drm_edid *drm_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->drm_edid) 257 p->drm_edid = drm_edid_read_ddc(connector, &aux_ep->aux->ddc); 258 259 drm_edid_connector_update(connector, p->drm_edid); 260 261 num = drm_edid_connector_add_modes(connector); 262 263 pm_runtime_mark_last_busy(panel->dev); 264 pm_runtime_put_autosuspend(panel->dev); 265 266 return num; 267 } 268 269 static const struct drm_panel_funcs atana33xc20_funcs = { 270 .disable = atana33xc20_disable, 271 .enable = atana33xc20_enable, 272 .unprepare = atana33xc20_unprepare, 273 .prepare = atana33xc20_prepare, 274 .get_modes = atana33xc20_get_modes, 275 }; 276 277 static void atana33xc20_runtime_disable(void *data) 278 { 279 pm_runtime_disable(data); 280 } 281 282 static void atana33xc20_dont_use_autosuspend(void *data) 283 { 284 pm_runtime_dont_use_autosuspend(data); 285 } 286 287 static int atana33xc20_probe(struct dp_aux_ep_device *aux_ep) 288 { 289 struct atana33xc20_panel *panel; 290 struct device *dev = &aux_ep->dev; 291 int ret; 292 293 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL); 294 if (!panel) 295 return -ENOMEM; 296 dev_set_drvdata(dev, panel); 297 298 panel->aux = aux_ep->aux; 299 300 panel->supply = devm_regulator_get(dev, "power"); 301 if (IS_ERR(panel->supply)) 302 return dev_err_probe(dev, PTR_ERR(panel->supply), 303 "Failed to get power supply\n"); 304 305 panel->el_on3_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); 306 if (IS_ERR(panel->el_on3_gpio)) 307 return dev_err_probe(dev, PTR_ERR(panel->el_on3_gpio), 308 "Failed to get enable GPIO\n"); 309 310 panel->no_hpd = of_property_read_bool(dev->of_node, "no-hpd"); 311 if (!panel->no_hpd) { 312 panel->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN); 313 if (IS_ERR(panel->hpd_gpio)) 314 return dev_err_probe(dev, PTR_ERR(panel->hpd_gpio), 315 "Failed to get HPD GPIO\n"); 316 } 317 318 pm_runtime_enable(dev); 319 ret = devm_add_action_or_reset(dev, atana33xc20_runtime_disable, dev); 320 if (ret) 321 return ret; 322 pm_runtime_set_autosuspend_delay(dev, 2000); 323 pm_runtime_use_autosuspend(dev); 324 ret = devm_add_action_or_reset(dev, atana33xc20_dont_use_autosuspend, dev); 325 if (ret) 326 return ret; 327 328 drm_panel_init(&panel->base, dev, &atana33xc20_funcs, DRM_MODE_CONNECTOR_eDP); 329 330 pm_runtime_get_sync(dev); 331 ret = drm_panel_dp_aux_backlight(&panel->base, aux_ep->aux); 332 pm_runtime_mark_last_busy(dev); 333 pm_runtime_put_autosuspend(dev); 334 335 /* 336 * Warn if we get an error, but don't consider it fatal. Having 337 * a panel where we can't control the backlight is better than 338 * no panel. 339 */ 340 if (ret) 341 dev_warn(dev, "failed to register dp aux backlight: %d\n", ret); 342 343 drm_panel_add(&panel->base); 344 345 return 0; 346 } 347 348 static void atana33xc20_remove(struct dp_aux_ep_device *aux_ep) 349 { 350 struct device *dev = &aux_ep->dev; 351 struct atana33xc20_panel *panel = dev_get_drvdata(dev); 352 353 drm_panel_remove(&panel->base); 354 drm_panel_disable(&panel->base); 355 drm_panel_unprepare(&panel->base); 356 357 drm_edid_free(panel->drm_edid); 358 } 359 360 static void atana33xc20_shutdown(struct dp_aux_ep_device *aux_ep) 361 { 362 struct device *dev = &aux_ep->dev; 363 struct atana33xc20_panel *panel = dev_get_drvdata(dev); 364 365 drm_panel_disable(&panel->base); 366 drm_panel_unprepare(&panel->base); 367 } 368 369 static const struct of_device_id atana33xc20_dt_match[] = { 370 { .compatible = "samsung,atna33xc20", }, 371 { /* sentinal */ } 372 }; 373 MODULE_DEVICE_TABLE(of, atana33xc20_dt_match); 374 375 static const struct dev_pm_ops atana33xc20_pm_ops = { 376 SET_RUNTIME_PM_OPS(atana33xc20_suspend, atana33xc20_resume, NULL) 377 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 378 pm_runtime_force_resume) 379 }; 380 381 static struct dp_aux_ep_driver atana33xc20_driver = { 382 .driver = { 383 .name = "samsung_atana33xc20", 384 .of_match_table = atana33xc20_dt_match, 385 .pm = &atana33xc20_pm_ops, 386 }, 387 .probe = atana33xc20_probe, 388 .remove = atana33xc20_remove, 389 .shutdown = atana33xc20_shutdown, 390 }; 391 392 static int __init atana33xc20_init(void) 393 { 394 return dp_aux_dp_driver_register(&atana33xc20_driver); 395 } 396 module_init(atana33xc20_init); 397 398 static void __exit atana33xc20_exit(void) 399 { 400 dp_aux_dp_driver_unregister(&atana33xc20_driver); 401 } 402 module_exit(atana33xc20_exit); 403 404 MODULE_DESCRIPTION("Samsung ATANA33XC20 Panel Driver"); 405 MODULE_LICENSE("GPL v2"); 406