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