linux/drivers/staging/imx-drm/parallel-display.c
<<
>>
Prefs
   1/*
   2 * i.MX drm driver - parallel display implementation
   3 *
   4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version 2
   9 * of the License, or (at your option) any later version.
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18 * MA 02110-1301, USA.
  19 */
  20
  21#include <linux/module.h>
  22#include <drm/drmP.h>
  23#include <drm/drm_fb_helper.h>
  24#include <drm/drm_crtc_helper.h>
  25#include <linux/videodev2.h>
  26#include <linux/pinctrl/consumer.h>
  27
  28#include "imx-drm.h"
  29
  30#define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
  31#define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder)
  32
  33struct imx_parallel_display {
  34        struct drm_connector connector;
  35        struct imx_drm_connector *imx_drm_connector;
  36        struct drm_encoder encoder;
  37        struct imx_drm_encoder *imx_drm_encoder;
  38        struct device *dev;
  39        void *edid;
  40        int edid_len;
  41        u32 interface_pix_fmt;
  42        int mode_valid;
  43        struct drm_display_mode mode;
  44};
  45
  46static enum drm_connector_status imx_pd_connector_detect(
  47                struct drm_connector *connector, bool force)
  48{
  49        return connector_status_connected;
  50}
  51
  52static void imx_pd_connector_destroy(struct drm_connector *connector)
  53{
  54        /* do not free here */
  55}
  56
  57static int imx_pd_connector_get_modes(struct drm_connector *connector)
  58{
  59        struct imx_parallel_display *imxpd = con_to_imxpd(connector);
  60        struct device_node *np = imxpd->dev->of_node;
  61        int num_modes = 0;
  62
  63        if (imxpd->edid) {
  64                drm_mode_connector_update_edid_property(connector, imxpd->edid);
  65                num_modes = drm_add_edid_modes(connector, imxpd->edid);
  66        }
  67
  68        if (imxpd->mode_valid) {
  69                struct drm_display_mode *mode = drm_mode_create(connector->dev);
  70                drm_mode_copy(mode, &imxpd->mode);
  71                mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  72                drm_mode_probed_add(connector, mode);
  73                num_modes++;
  74        }
  75
  76        if (np) {
  77                struct drm_display_mode *mode = drm_mode_create(connector->dev);
  78                of_get_drm_display_mode(np, &imxpd->mode, 0);
  79                drm_mode_copy(mode, &imxpd->mode);
  80                mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  81                drm_mode_probed_add(connector, mode);
  82                num_modes++;
  83        }
  84
  85        return num_modes;
  86}
  87
  88static int imx_pd_connector_mode_valid(struct drm_connector *connector,
  89                          struct drm_display_mode *mode)
  90{
  91        return 0;
  92}
  93
  94static struct drm_encoder *imx_pd_connector_best_encoder(
  95                struct drm_connector *connector)
  96{
  97        struct imx_parallel_display *imxpd = con_to_imxpd(connector);
  98
  99        return &imxpd->encoder;
 100}
 101
 102static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
 103{
 104}
 105
 106static bool imx_pd_encoder_mode_fixup(struct drm_encoder *encoder,
 107                           const struct drm_display_mode *mode,
 108                           struct drm_display_mode *adjusted_mode)
 109{
 110        return true;
 111}
 112
 113static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
 114{
 115        struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
 116
 117        imx_drm_crtc_panel_format(encoder->crtc, DRM_MODE_ENCODER_NONE,
 118                        imxpd->interface_pix_fmt);
 119}
 120
 121static void imx_pd_encoder_commit(struct drm_encoder *encoder)
 122{
 123}
 124
 125static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
 126                         struct drm_display_mode *mode,
 127                         struct drm_display_mode *adjusted_mode)
 128{
 129}
 130
 131static void imx_pd_encoder_disable(struct drm_encoder *encoder)
 132{
 133}
 134
 135static void imx_pd_encoder_destroy(struct drm_encoder *encoder)
 136{
 137        /* do not free here */
 138}
 139
 140static struct drm_connector_funcs imx_pd_connector_funcs = {
 141        .dpms = drm_helper_connector_dpms,
 142        .fill_modes = drm_helper_probe_single_connector_modes,
 143        .detect = imx_pd_connector_detect,
 144        .destroy = imx_pd_connector_destroy,
 145};
 146
 147static struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
 148        .get_modes = imx_pd_connector_get_modes,
 149        .best_encoder = imx_pd_connector_best_encoder,
 150        .mode_valid = imx_pd_connector_mode_valid,
 151};
 152
 153static struct drm_encoder_funcs imx_pd_encoder_funcs = {
 154        .destroy = imx_pd_encoder_destroy,
 155};
 156
 157static struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
 158        .dpms = imx_pd_encoder_dpms,
 159        .mode_fixup = imx_pd_encoder_mode_fixup,
 160        .prepare = imx_pd_encoder_prepare,
 161        .commit = imx_pd_encoder_commit,
 162        .mode_set = imx_pd_encoder_mode_set,
 163        .disable = imx_pd_encoder_disable,
 164};
 165
 166static int imx_pd_register(struct imx_parallel_display *imxpd)
 167{
 168        int ret;
 169
 170        drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
 171
 172        imxpd->connector.funcs = &imx_pd_connector_funcs;
 173        imxpd->encoder.funcs = &imx_pd_encoder_funcs;
 174
 175        imxpd->encoder.encoder_type = DRM_MODE_ENCODER_NONE;
 176        imxpd->connector.connector_type = DRM_MODE_CONNECTOR_VGA;
 177
 178        drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
 179        ret = imx_drm_add_encoder(&imxpd->encoder, &imxpd->imx_drm_encoder,
 180                        THIS_MODULE);
 181        if (ret) {
 182                dev_err(imxpd->dev, "adding encoder failed with %d\n", ret);
 183                return ret;
 184        }
 185
 186        drm_connector_helper_add(&imxpd->connector,
 187                        &imx_pd_connector_helper_funcs);
 188
 189        ret = imx_drm_add_connector(&imxpd->connector,
 190                        &imxpd->imx_drm_connector, THIS_MODULE);
 191        if (ret) {
 192                imx_drm_remove_encoder(imxpd->imx_drm_encoder);
 193                dev_err(imxpd->dev, "adding connector failed with %d\n", ret);
 194                return ret;
 195        }
 196
 197        imxpd->connector.encoder = &imxpd->encoder;
 198
 199        return 0;
 200}
 201
 202static int imx_pd_probe(struct platform_device *pdev)
 203{
 204        struct device_node *np = pdev->dev.of_node;
 205        const u8 *edidp;
 206        struct imx_parallel_display *imxpd;
 207        int ret;
 208        const char *fmt;
 209        struct pinctrl *pinctrl;
 210
 211        imxpd = devm_kzalloc(&pdev->dev, sizeof(*imxpd), GFP_KERNEL);
 212        if (!imxpd)
 213                return -ENOMEM;
 214
 215        pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
 216        if (IS_ERR(pinctrl)) {
 217                ret = PTR_ERR(pinctrl);
 218                dev_warn(&pdev->dev, "pinctrl_get_select_default failed with %d",
 219                                ret);
 220                return ret;
 221        }
 222
 223        edidp = of_get_property(np, "edid", &imxpd->edid_len);
 224        if (edidp)
 225                imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
 226
 227        ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
 228        if (!ret) {
 229                if (!strcmp(fmt, "rgb24"))
 230                        imxpd->interface_pix_fmt = V4L2_PIX_FMT_RGB24;
 231                else if (!strcmp(fmt, "rgb565"))
 232                        imxpd->interface_pix_fmt = V4L2_PIX_FMT_RGB565;
 233                else if (!strcmp(fmt, "bgr666"))
 234                        imxpd->interface_pix_fmt = V4L2_PIX_FMT_BGR666;
 235        }
 236
 237        imxpd->dev = &pdev->dev;
 238
 239        ret = imx_pd_register(imxpd);
 240        if (ret)
 241                return ret;
 242
 243        ret = imx_drm_encoder_add_possible_crtcs(imxpd->imx_drm_encoder, np);
 244
 245        platform_set_drvdata(pdev, imxpd);
 246
 247        return 0;
 248}
 249
 250static int imx_pd_remove(struct platform_device *pdev)
 251{
 252        struct imx_parallel_display *imxpd = platform_get_drvdata(pdev);
 253        struct drm_connector *connector = &imxpd->connector;
 254        struct drm_encoder *encoder = &imxpd->encoder;
 255
 256        drm_mode_connector_detach_encoder(connector, encoder);
 257
 258        imx_drm_remove_connector(imxpd->imx_drm_connector);
 259        imx_drm_remove_encoder(imxpd->imx_drm_encoder);
 260
 261        return 0;
 262}
 263
 264static const struct of_device_id imx_pd_dt_ids[] = {
 265        { .compatible = "fsl,imx-parallel-display", },
 266        { /* sentinel */ }
 267};
 268
 269static struct platform_driver imx_pd_driver = {
 270        .probe          = imx_pd_probe,
 271        .remove         = imx_pd_remove,
 272        .driver         = {
 273                .of_match_table = imx_pd_dt_ids,
 274                .name   = "imx-parallel-display",
 275                .owner  = THIS_MODULE,
 276        },
 277};
 278
 279module_platform_driver(imx_pd_driver);
 280
 281MODULE_DESCRIPTION("i.MX parallel display driver");
 282MODULE_AUTHOR("Sascha Hauer, Pengutronix");
 283MODULE_LICENSE("GPL");
 284