1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Texas Instruments 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include <linux/gpio/consumer.h> 8 #include <linux/pinctrl/consumer.h> 9 #include <linux/platform_device.h> 10 11 #include <video/display_timing.h> 12 #include <video/of_display_timing.h> 13 #include <video/videomode.h> 14 15 #include <drm/drm_atomic_state_helper.h> 16 #include <drm/drm_connector.h> 17 #include <drm/drm_modeset_helper_vtables.h> 18 #include <drm/drm_probe_helper.h> 19 #include <drm/drm_simple_kms_helper.h> 20 21 #include "tilcdc_drv.h" 22 #include "tilcdc_panel.h" 23 24 struct panel_module { 25 struct tilcdc_module base; 26 struct tilcdc_panel_info *info; 27 struct display_timings *timings; 28 struct backlight_device *backlight; 29 struct gpio_desc *enable_gpio; 30 }; 31 #define to_panel_module(x) container_of(x, struct panel_module, base) 32 33 34 /* 35 * Encoder: 36 */ 37 38 struct panel_encoder { 39 struct drm_encoder base; 40 struct panel_module *mod; 41 }; 42 #define to_panel_encoder(x) container_of(x, struct panel_encoder, base) 43 44 static void panel_encoder_dpms(struct drm_encoder *encoder, int mode) 45 { 46 struct panel_encoder *panel_encoder = to_panel_encoder(encoder); 47 struct backlight_device *backlight = panel_encoder->mod->backlight; 48 struct gpio_desc *gpio = panel_encoder->mod->enable_gpio; 49 50 if (backlight) { 51 backlight->props.power = mode == DRM_MODE_DPMS_ON ? 52 FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; 53 backlight_update_status(backlight); 54 } 55 56 if (gpio) 57 gpiod_set_value_cansleep(gpio, 58 mode == DRM_MODE_DPMS_ON ? 1 : 0); 59 } 60 61 static void panel_encoder_prepare(struct drm_encoder *encoder) 62 { 63 panel_encoder_dpms(encoder, DRM_MODE_DPMS_OFF); 64 } 65 66 static void panel_encoder_commit(struct drm_encoder *encoder) 67 { 68 panel_encoder_dpms(encoder, DRM_MODE_DPMS_ON); 69 } 70 71 static void panel_encoder_mode_set(struct drm_encoder *encoder, 72 struct drm_display_mode *mode, 73 struct drm_display_mode *adjusted_mode) 74 { 75 /* nothing needed */ 76 } 77 78 static const struct drm_encoder_helper_funcs panel_encoder_helper_funcs = { 79 .dpms = panel_encoder_dpms, 80 .prepare = panel_encoder_prepare, 81 .commit = panel_encoder_commit, 82 .mode_set = panel_encoder_mode_set, 83 }; 84 85 static struct drm_encoder *panel_encoder_create(struct drm_device *dev, 86 struct panel_module *mod) 87 { 88 struct panel_encoder *panel_encoder; 89 struct drm_encoder *encoder; 90 int ret; 91 92 panel_encoder = devm_kzalloc(dev->dev, sizeof(*panel_encoder), 93 GFP_KERNEL); 94 if (!panel_encoder) 95 return NULL; 96 97 panel_encoder->mod = mod; 98 99 encoder = &panel_encoder->base; 100 encoder->possible_crtcs = 1; 101 102 ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_LVDS); 103 if (ret < 0) 104 goto fail; 105 106 drm_encoder_helper_add(encoder, &panel_encoder_helper_funcs); 107 108 return encoder; 109 110 fail: 111 drm_encoder_cleanup(encoder); 112 return NULL; 113 } 114 115 /* 116 * Connector: 117 */ 118 119 struct panel_connector { 120 struct drm_connector base; 121 122 struct drm_encoder *encoder; /* our connected encoder */ 123 struct panel_module *mod; 124 }; 125 #define to_panel_connector(x) container_of(x, struct panel_connector, base) 126 127 128 static void panel_connector_destroy(struct drm_connector *connector) 129 { 130 drm_connector_unregister(connector); 131 drm_connector_cleanup(connector); 132 } 133 134 static int panel_connector_get_modes(struct drm_connector *connector) 135 { 136 struct drm_device *dev = connector->dev; 137 struct panel_connector *panel_connector = to_panel_connector(connector); 138 struct display_timings *timings = panel_connector->mod->timings; 139 int i; 140 141 for (i = 0; i < timings->num_timings; i++) { 142 struct drm_display_mode *mode = drm_mode_create(dev); 143 struct videomode vm; 144 145 if (videomode_from_timings(timings, &vm, i)) 146 break; 147 148 drm_display_mode_from_videomode(&vm, mode); 149 150 mode->type = DRM_MODE_TYPE_DRIVER; 151 152 if (timings->native_mode == i) 153 mode->type |= DRM_MODE_TYPE_PREFERRED; 154 155 drm_mode_set_name(mode); 156 drm_mode_probed_add(connector, mode); 157 } 158 159 return i; 160 } 161 162 static struct drm_encoder *panel_connector_best_encoder( 163 struct drm_connector *connector) 164 { 165 struct panel_connector *panel_connector = to_panel_connector(connector); 166 return panel_connector->encoder; 167 } 168 169 static const struct drm_connector_funcs panel_connector_funcs = { 170 .destroy = panel_connector_destroy, 171 .fill_modes = drm_helper_probe_single_connector_modes, 172 .reset = drm_atomic_helper_connector_reset, 173 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 174 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 175 }; 176 177 static const struct drm_connector_helper_funcs panel_connector_helper_funcs = { 178 .get_modes = panel_connector_get_modes, 179 .best_encoder = panel_connector_best_encoder, 180 }; 181 182 static struct drm_connector *panel_connector_create(struct drm_device *dev, 183 struct panel_module *mod, struct drm_encoder *encoder) 184 { 185 struct panel_connector *panel_connector; 186 struct drm_connector *connector; 187 int ret; 188 189 panel_connector = devm_kzalloc(dev->dev, sizeof(*panel_connector), 190 GFP_KERNEL); 191 if (!panel_connector) 192 return NULL; 193 194 panel_connector->encoder = encoder; 195 panel_connector->mod = mod; 196 197 connector = &panel_connector->base; 198 199 drm_connector_init(dev, connector, &panel_connector_funcs, 200 DRM_MODE_CONNECTOR_LVDS); 201 drm_connector_helper_add(connector, &panel_connector_helper_funcs); 202 203 connector->interlace_allowed = 0; 204 connector->doublescan_allowed = 0; 205 206 ret = drm_connector_attach_encoder(connector, encoder); 207 if (ret) 208 goto fail; 209 210 return connector; 211 212 fail: 213 panel_connector_destroy(connector); 214 return NULL; 215 } 216 217 /* 218 * Module: 219 */ 220 221 static int panel_modeset_init(struct tilcdc_module *mod, struct drm_device *dev) 222 { 223 struct panel_module *panel_mod = to_panel_module(mod); 224 struct tilcdc_drm_private *priv = dev->dev_private; 225 struct drm_encoder *encoder; 226 struct drm_connector *connector; 227 228 encoder = panel_encoder_create(dev, panel_mod); 229 if (!encoder) 230 return -ENOMEM; 231 232 connector = panel_connector_create(dev, panel_mod, encoder); 233 if (!connector) 234 return -ENOMEM; 235 236 priv->encoders[priv->num_encoders++] = encoder; 237 priv->connectors[priv->num_connectors++] = connector; 238 239 tilcdc_crtc_set_panel_info(priv->crtc, 240 to_panel_encoder(encoder)->mod->info); 241 242 return 0; 243 } 244 245 static const struct tilcdc_module_ops panel_module_ops = { 246 .modeset_init = panel_modeset_init, 247 }; 248 249 /* 250 * Device: 251 */ 252 253 /* maybe move this somewhere common if it is needed by other outputs? */ 254 static struct tilcdc_panel_info *of_get_panel_info(struct device_node *np) 255 { 256 struct device_node *info_np; 257 struct tilcdc_panel_info *info; 258 int ret = 0; 259 260 if (!np) { 261 pr_err("%s: no devicenode given\n", __func__); 262 return NULL; 263 } 264 265 info_np = of_get_child_by_name(np, "panel-info"); 266 if (!info_np) { 267 pr_err("%s: could not find panel-info node\n", __func__); 268 return NULL; 269 } 270 271 info = kzalloc(sizeof(*info), GFP_KERNEL); 272 if (!info) 273 goto put_node; 274 275 ret |= of_property_read_u32(info_np, "ac-bias", &info->ac_bias); 276 ret |= of_property_read_u32(info_np, "ac-bias-intrpt", &info->ac_bias_intrpt); 277 ret |= of_property_read_u32(info_np, "dma-burst-sz", &info->dma_burst_sz); 278 ret |= of_property_read_u32(info_np, "bpp", &info->bpp); 279 ret |= of_property_read_u32(info_np, "fdd", &info->fdd); 280 ret |= of_property_read_u32(info_np, "sync-edge", &info->sync_edge); 281 ret |= of_property_read_u32(info_np, "sync-ctrl", &info->sync_ctrl); 282 ret |= of_property_read_u32(info_np, "raster-order", &info->raster_order); 283 ret |= of_property_read_u32(info_np, "fifo-th", &info->fifo_th); 284 285 /* optional: */ 286 info->tft_alt_mode = of_property_read_bool(info_np, "tft-alt-mode"); 287 info->invert_pxl_clk = of_property_read_bool(info_np, "invert-pxl-clk"); 288 289 if (ret) { 290 pr_err("%s: error reading panel-info properties\n", __func__); 291 kfree(info); 292 info = NULL; 293 } 294 295 put_node: 296 of_node_put(info_np); 297 return info; 298 } 299 300 static int panel_probe(struct platform_device *pdev) 301 { 302 struct device_node *bl_node, *node = pdev->dev.of_node; 303 struct panel_module *panel_mod; 304 struct tilcdc_module *mod; 305 struct pinctrl *pinctrl; 306 int ret; 307 308 /* bail out early if no DT data: */ 309 if (!node) { 310 dev_err(&pdev->dev, "device-tree data is missing\n"); 311 return -ENXIO; 312 } 313 314 panel_mod = devm_kzalloc(&pdev->dev, sizeof(*panel_mod), GFP_KERNEL); 315 if (!panel_mod) 316 return -ENOMEM; 317 318 bl_node = of_parse_phandle(node, "backlight", 0); 319 if (bl_node) { 320 panel_mod->backlight = of_find_backlight_by_node(bl_node); 321 of_node_put(bl_node); 322 323 if (!panel_mod->backlight) 324 return -EPROBE_DEFER; 325 326 dev_info(&pdev->dev, "found backlight\n"); 327 } 328 329 panel_mod->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 330 GPIOD_OUT_LOW); 331 if (IS_ERR(panel_mod->enable_gpio)) { 332 ret = PTR_ERR(panel_mod->enable_gpio); 333 dev_err(&pdev->dev, "failed to request enable GPIO\n"); 334 goto fail_backlight; 335 } 336 337 if (panel_mod->enable_gpio) 338 dev_info(&pdev->dev, "found enable GPIO\n"); 339 340 mod = &panel_mod->base; 341 pdev->dev.platform_data = mod; 342 343 tilcdc_module_init(mod, "panel", &panel_module_ops); 344 345 pinctrl = devm_pinctrl_get_select_default(&pdev->dev); 346 if (IS_ERR(pinctrl)) 347 dev_warn(&pdev->dev, "pins are not configured\n"); 348 349 panel_mod->timings = of_get_display_timings(node); 350 if (!panel_mod->timings) { 351 dev_err(&pdev->dev, "could not get panel timings\n"); 352 ret = -EINVAL; 353 goto fail_free; 354 } 355 356 panel_mod->info = of_get_panel_info(node); 357 if (!panel_mod->info) { 358 dev_err(&pdev->dev, "could not get panel info\n"); 359 ret = -EINVAL; 360 goto fail_timings; 361 } 362 363 return 0; 364 365 fail_timings: 366 display_timings_release(panel_mod->timings); 367 368 fail_free: 369 tilcdc_module_cleanup(mod); 370 371 fail_backlight: 372 if (panel_mod->backlight) 373 put_device(&panel_mod->backlight->dev); 374 return ret; 375 } 376 377 static int panel_remove(struct platform_device *pdev) 378 { 379 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev); 380 struct panel_module *panel_mod = to_panel_module(mod); 381 struct backlight_device *backlight = panel_mod->backlight; 382 383 if (backlight) 384 put_device(&backlight->dev); 385 386 display_timings_release(panel_mod->timings); 387 388 tilcdc_module_cleanup(mod); 389 kfree(panel_mod->info); 390 391 return 0; 392 } 393 394 static const struct of_device_id panel_of_match[] = { 395 { .compatible = "ti,tilcdc,panel", }, 396 { }, 397 }; 398 399 struct platform_driver panel_driver = { 400 .probe = panel_probe, 401 .remove = panel_remove, 402 .driver = { 403 .owner = THIS_MODULE, 404 .name = "tilcdc-panel", 405 .of_match_table = panel_of_match, 406 }, 407 }; 408 409 int __init tilcdc_panel_init(void) 410 { 411 return platform_driver_register(&panel_driver); 412 } 413 414 void __exit tilcdc_panel_fini(void) 415 { 416 platform_driver_unregister(&panel_driver); 417 } 418