Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2014 Traphandler
0004  * Copyright (C) 2014 Free Electrons
0005  * Copyright (C) 2014 Atmel
0006  *
0007  * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
0008  * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
0009  */
0010 
0011 #include <linux/media-bus-format.h>
0012 #include <linux/of.h>
0013 #include <linux/of_graph.h>
0014 
0015 #include <drm/drm_bridge.h>
0016 #include <drm/drm_encoder.h>
0017 #include <drm/drm_of.h>
0018 #include <drm/drm_simple_kms_helper.h>
0019 
0020 #include "atmel_hlcdc_dc.h"
0021 
0022 struct atmel_hlcdc_rgb_output {
0023     struct drm_encoder encoder;
0024     int bus_fmt;
0025 };
0026 
0027 static struct atmel_hlcdc_rgb_output *
0028 atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
0029 {
0030     return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
0031 }
0032 
0033 int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
0034 {
0035     struct atmel_hlcdc_rgb_output *output;
0036 
0037     output = atmel_hlcdc_encoder_to_rgb_output(encoder);
0038 
0039     return output->bus_fmt;
0040 }
0041 
0042 static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
0043 {
0044     u32 bus_width;
0045     int ret;
0046 
0047     ret = of_property_read_u32(ep, "bus-width", &bus_width);
0048     if (ret == -EINVAL)
0049         return 0;
0050     if (ret)
0051         return ret;
0052 
0053     switch (bus_width) {
0054     case 12:
0055         return MEDIA_BUS_FMT_RGB444_1X12;
0056     case 16:
0057         return MEDIA_BUS_FMT_RGB565_1X16;
0058     case 18:
0059         return MEDIA_BUS_FMT_RGB666_1X18;
0060     case 24:
0061         return MEDIA_BUS_FMT_RGB888_1X24;
0062     default:
0063         return -EINVAL;
0064     }
0065 }
0066 
0067 static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
0068 {
0069     struct atmel_hlcdc_rgb_output *output;
0070     struct device_node *ep;
0071     struct drm_panel *panel;
0072     struct drm_bridge *bridge;
0073     int ret;
0074 
0075     ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
0076     if (!ep)
0077         return -ENODEV;
0078 
0079     ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
0080                       &panel, &bridge);
0081     if (ret) {
0082         of_node_put(ep);
0083         return ret;
0084     }
0085 
0086     output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
0087     if (!output) {
0088         of_node_put(ep);
0089         return -ENOMEM;
0090     }
0091 
0092     output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
0093     of_node_put(ep);
0094     if (output->bus_fmt < 0) {
0095         dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
0096         return -EINVAL;
0097     }
0098 
0099     ret = drm_simple_encoder_init(dev, &output->encoder,
0100                       DRM_MODE_ENCODER_NONE);
0101     if (ret)
0102         return ret;
0103 
0104     output->encoder.possible_crtcs = 0x1;
0105 
0106     if (panel) {
0107         bridge = drm_panel_bridge_add_typed(panel,
0108                             DRM_MODE_CONNECTOR_Unknown);
0109         if (IS_ERR(bridge))
0110             return PTR_ERR(bridge);
0111     }
0112 
0113     if (bridge) {
0114         ret = drm_bridge_attach(&output->encoder, bridge, NULL, 0);
0115         if (!ret)
0116             return 0;
0117 
0118         if (panel)
0119             drm_panel_bridge_remove(bridge);
0120     }
0121 
0122     drm_encoder_cleanup(&output->encoder);
0123 
0124     return ret;
0125 }
0126 
0127 int atmel_hlcdc_create_outputs(struct drm_device *dev)
0128 {
0129     int endpoint, ret = 0;
0130     int attached = 0;
0131 
0132     /*
0133      * Always scan the first few endpoints even if we get -ENODEV,
0134      * but keep going after that as long as we keep getting hits.
0135      */
0136     for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
0137         ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
0138         if (ret == -ENODEV)
0139             continue;
0140         if (ret)
0141             break;
0142         attached++;
0143     }
0144 
0145     /* At least one device was successfully attached.*/
0146     if (ret == -ENODEV && attached)
0147         return 0;
0148 
0149     return ret;
0150 }