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