Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright (C) 2017 Free Electrons
  4 * Maxime Ripard <maxime.ripard@free-electrons.com>
  5 */
  6
  7#include <linux/clk.h>
  8
  9#include <drm/drm_atomic_helper.h>
 
 10#include <drm/drm_of.h>
 11#include <drm/drm_panel.h>
 12#include <drm/drm_print.h>
 13#include <drm/drm_probe_helper.h>
 
 14
 15#include "sun4i_crtc.h"
 16#include "sun4i_tcon.h"
 17#include "sun4i_lvds.h"
 18
 19struct sun4i_lvds {
 20	struct drm_connector	connector;
 21	struct drm_encoder	encoder;
 22
 23	struct drm_panel	*panel;
 24};
 25
 26static inline struct sun4i_lvds *
 27drm_connector_to_sun4i_lvds(struct drm_connector *connector)
 28{
 29	return container_of(connector, struct sun4i_lvds,
 30			    connector);
 31}
 32
 33static inline struct sun4i_lvds *
 34drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
 35{
 36	return container_of(encoder, struct sun4i_lvds,
 37			    encoder);
 38}
 39
 40static int sun4i_lvds_get_modes(struct drm_connector *connector)
 41{
 42	struct sun4i_lvds *lvds =
 43		drm_connector_to_sun4i_lvds(connector);
 44
 45	return drm_panel_get_modes(lvds->panel);
 46}
 47
 48static struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
 49	.get_modes	= sun4i_lvds_get_modes,
 50};
 51
 52static void
 53sun4i_lvds_connector_destroy(struct drm_connector *connector)
 54{
 55	struct sun4i_lvds *lvds = drm_connector_to_sun4i_lvds(connector);
 56
 57	drm_panel_detach(lvds->panel);
 58	drm_connector_cleanup(connector);
 59}
 60
 61static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
 62	.fill_modes		= drm_helper_probe_single_connector_modes,
 63	.destroy		= sun4i_lvds_connector_destroy,
 64	.reset			= drm_atomic_helper_connector_reset,
 65	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
 66	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
 67};
 68
 69static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
 70{
 71	struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
 72
 73	DRM_DEBUG_DRIVER("Enabling LVDS output\n");
 74
 75	if (lvds->panel) {
 76		drm_panel_prepare(lvds->panel);
 77		drm_panel_enable(lvds->panel);
 78	}
 79}
 80
 81static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
 82{
 83	struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
 84
 85	DRM_DEBUG_DRIVER("Disabling LVDS output\n");
 86
 87	if (lvds->panel) {
 88		drm_panel_disable(lvds->panel);
 89		drm_panel_unprepare(lvds->panel);
 90	}
 91}
 92
 93static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
 94	.disable	= sun4i_lvds_encoder_disable,
 95	.enable		= sun4i_lvds_encoder_enable,
 96};
 97
 98static const struct drm_encoder_funcs sun4i_lvds_enc_funcs = {
 99	.destroy	= drm_encoder_cleanup,
100};
101
102int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
103{
104	struct drm_encoder *encoder;
105	struct drm_bridge *bridge;
106	struct sun4i_lvds *lvds;
107	int ret;
108
109	lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
110	if (!lvds)
111		return -ENOMEM;
112	encoder = &lvds->encoder;
113
114	ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
115					  &lvds->panel, &bridge);
116	if (ret) {
117		dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
118		return 0;
119	}
120
121	drm_encoder_helper_add(&lvds->encoder,
122			       &sun4i_lvds_enc_helper_funcs);
123	ret = drm_encoder_init(drm,
124			       &lvds->encoder,
125			       &sun4i_lvds_enc_funcs,
126			       DRM_MODE_ENCODER_LVDS,
127			       NULL);
128	if (ret) {
129		dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
130		goto err_out;
131	}
132
133	/* The LVDS encoder can only work with the TCON channel 0 */
134	lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
135
136	if (lvds->panel) {
137		drm_connector_helper_add(&lvds->connector,
138					 &sun4i_lvds_con_helper_funcs);
139		ret = drm_connector_init(drm, &lvds->connector,
140					 &sun4i_lvds_con_funcs,
141					 DRM_MODE_CONNECTOR_LVDS);
142		if (ret) {
143			dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
144			goto err_cleanup_connector;
145		}
146
147		drm_connector_attach_encoder(&lvds->connector,
148						  &lvds->encoder);
149
150		ret = drm_panel_attach(lvds->panel, &lvds->connector);
151		if (ret) {
152			dev_err(drm->dev, "Couldn't attach our panel\n");
153			goto err_cleanup_connector;
154		}
155	}
156
157	if (bridge) {
158		ret = drm_bridge_attach(encoder, bridge, NULL);
159		if (ret) {
160			dev_err(drm->dev, "Couldn't attach our bridge\n");
161			goto err_cleanup_connector;
162		}
163	}
164
165	return 0;
166
167err_cleanup_connector:
168	drm_encoder_cleanup(&lvds->encoder);
169err_out:
170	return ret;
171}
172EXPORT_SYMBOL(sun4i_lvds_init);
v5.9
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright (C) 2017 Free Electrons
  4 * Maxime Ripard <maxime.ripard@free-electrons.com>
  5 */
  6
  7#include <linux/clk.h>
  8
  9#include <drm/drm_atomic_helper.h>
 10#include <drm/drm_bridge.h>
 11#include <drm/drm_of.h>
 12#include <drm/drm_panel.h>
 13#include <drm/drm_print.h>
 14#include <drm/drm_probe_helper.h>
 15#include <drm/drm_simple_kms_helper.h>
 16
 17#include "sun4i_crtc.h"
 18#include "sun4i_tcon.h"
 19#include "sun4i_lvds.h"
 20
 21struct sun4i_lvds {
 22	struct drm_connector	connector;
 23	struct drm_encoder	encoder;
 24
 25	struct drm_panel	*panel;
 26};
 27
 28static inline struct sun4i_lvds *
 29drm_connector_to_sun4i_lvds(struct drm_connector *connector)
 30{
 31	return container_of(connector, struct sun4i_lvds,
 32			    connector);
 33}
 34
 35static inline struct sun4i_lvds *
 36drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
 37{
 38	return container_of(encoder, struct sun4i_lvds,
 39			    encoder);
 40}
 41
 42static int sun4i_lvds_get_modes(struct drm_connector *connector)
 43{
 44	struct sun4i_lvds *lvds =
 45		drm_connector_to_sun4i_lvds(connector);
 46
 47	return drm_panel_get_modes(lvds->panel, connector);
 48}
 49
 50static struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
 51	.get_modes	= sun4i_lvds_get_modes,
 52};
 53
 54static void
 55sun4i_lvds_connector_destroy(struct drm_connector *connector)
 56{
 57	struct sun4i_lvds *lvds = drm_connector_to_sun4i_lvds(connector);
 58
 59	drm_panel_detach(lvds->panel);
 60	drm_connector_cleanup(connector);
 61}
 62
 63static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
 64	.fill_modes		= drm_helper_probe_single_connector_modes,
 65	.destroy		= sun4i_lvds_connector_destroy,
 66	.reset			= drm_atomic_helper_connector_reset,
 67	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
 68	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
 69};
 70
 71static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
 72{
 73	struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
 74
 75	DRM_DEBUG_DRIVER("Enabling LVDS output\n");
 76
 77	if (lvds->panel) {
 78		drm_panel_prepare(lvds->panel);
 79		drm_panel_enable(lvds->panel);
 80	}
 81}
 82
 83static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
 84{
 85	struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
 86
 87	DRM_DEBUG_DRIVER("Disabling LVDS output\n");
 88
 89	if (lvds->panel) {
 90		drm_panel_disable(lvds->panel);
 91		drm_panel_unprepare(lvds->panel);
 92	}
 93}
 94
 95static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
 96	.disable	= sun4i_lvds_encoder_disable,
 97	.enable		= sun4i_lvds_encoder_enable,
 98};
 99
 
 
 
 
100int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
101{
102	struct drm_encoder *encoder;
103	struct drm_bridge *bridge;
104	struct sun4i_lvds *lvds;
105	int ret;
106
107	lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
108	if (!lvds)
109		return -ENOMEM;
110	encoder = &lvds->encoder;
111
112	ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
113					  &lvds->panel, &bridge);
114	if (ret) {
115		dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
116		return 0;
117	}
118
119	drm_encoder_helper_add(&lvds->encoder,
120			       &sun4i_lvds_enc_helper_funcs);
121	ret = drm_simple_encoder_init(drm, &lvds->encoder,
122				      DRM_MODE_ENCODER_LVDS);
 
 
 
123	if (ret) {
124		dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
125		goto err_out;
126	}
127
128	/* The LVDS encoder can only work with the TCON channel 0 */
129	lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
130
131	if (lvds->panel) {
132		drm_connector_helper_add(&lvds->connector,
133					 &sun4i_lvds_con_helper_funcs);
134		ret = drm_connector_init(drm, &lvds->connector,
135					 &sun4i_lvds_con_funcs,
136					 DRM_MODE_CONNECTOR_LVDS);
137		if (ret) {
138			dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
139			goto err_cleanup_connector;
140		}
141
142		drm_connector_attach_encoder(&lvds->connector,
143						  &lvds->encoder);
144
145		ret = drm_panel_attach(lvds->panel, &lvds->connector);
146		if (ret) {
147			dev_err(drm->dev, "Couldn't attach our panel\n");
148			goto err_cleanup_connector;
149		}
150	}
151
152	if (bridge) {
153		ret = drm_bridge_attach(encoder, bridge, NULL, 0);
154		if (ret) {
155			dev_err(drm->dev, "Couldn't attach our bridge\n");
156			goto err_cleanup_connector;
157		}
158	}
159
160	return 0;
161
162err_cleanup_connector:
163	drm_encoder_cleanup(&lvds->encoder);
164err_out:
165	return ret;
166}
167EXPORT_SYMBOL(sun4i_lvds_init);