Path: blob/master/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_output.c
52467 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2014 Traphandler3* Copyright (C) 2014 Free Electrons4* Copyright (C) 2014 Atmel5*6* Author: Jean-Jacques Hiblot <[email protected]>7* Author: Boris BREZILLON <[email protected]>8*/910#include <linux/media-bus-format.h>11#include <linux/of.h>12#include <linux/of_graph.h>1314#include <drm/drm_bridge.h>15#include <drm/drm_encoder.h>16#include <drm/drm_of.h>17#include <drm/drm_print.h>18#include <drm/drm_simple_kms_helper.h>1920#include "atmel_hlcdc_dc.h"2122struct atmel_hlcdc_rgb_output {23struct drm_encoder encoder;24int bus_fmt;25};2627static struct atmel_hlcdc_rgb_output *28atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)29{30return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);31}3233int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)34{35struct atmel_hlcdc_rgb_output *output;3637output = atmel_hlcdc_encoder_to_rgb_output(encoder);3839return output->bus_fmt;40}4142static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)43{44u32 bus_width;45int ret;4647ret = of_property_read_u32(ep, "bus-width", &bus_width);48if (ret == -EINVAL)49return 0;50if (ret)51return ret;5253switch (bus_width) {54case 12:55return MEDIA_BUS_FMT_RGB444_1X12;56case 16:57return MEDIA_BUS_FMT_RGB565_1X16;58case 18:59return MEDIA_BUS_FMT_RGB666_1X18;60case 24:61return MEDIA_BUS_FMT_RGB888_1X24;62default:63return -EINVAL;64}65}6667static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)68{69struct atmel_hlcdc_rgb_output *output;70struct device_node *ep;71struct drm_bridge *bridge;72struct atmel_hlcdc_dc *dc = dev->dev_private;73struct drm_crtc *crtc = dc->crtc;74int ret = 0;7576bridge = devm_drm_of_get_bridge(dev->dev, dev->dev->of_node, 0, endpoint);77if (IS_ERR(bridge))78return PTR_ERR(bridge);7980output = drmm_simple_encoder_alloc(dev, struct atmel_hlcdc_rgb_output,81encoder, DRM_MODE_ENCODER_NONE);82if (IS_ERR(output))83return PTR_ERR(output);8485ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);86if (!ep)87return -ENODEV;8889output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);90of_node_put(ep);91if (output->bus_fmt < 0) {92drm_err(dev, "endpoint %d: invalid bus width\n", endpoint);93return -EINVAL;94}959697output->encoder.possible_crtcs = drm_crtc_mask(crtc);9899if (bridge)100ret = drm_bridge_attach(&output->encoder, bridge, NULL, 0);101102return ret;103}104105int atmel_hlcdc_create_outputs(struct drm_device *dev)106{107int endpoint, ret = 0;108int attached = 0;109110/*111* Always scan the first few endpoints even if we get -ENODEV,112* but keep going after that as long as we keep getting hits.113*/114for (endpoint = 0; !ret || endpoint < 4; endpoint++) {115ret = atmel_hlcdc_attach_endpoint(dev, endpoint);116if (ret == -ENODEV)117continue;118if (ret)119break;120attached++;121}122123/* At least one device was successfully attached.*/124if (ret == -ENODEV && attached)125return 0;126127return ret;128}129130131