xref: /linux/drivers/gpu/drm/bridge/lvds-codec.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2019 Renesas Electronics Corporation
4  * Copyright (C) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5  */
6 
7 #include <linux/gpio/consumer.h>
8 #include <linux/media-bus-format.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/consumer.h>
14 
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_bridge.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_panel.h>
19 
20 struct lvds_codec {
21 	struct device *dev;
22 	struct drm_bridge bridge;
23 	struct drm_bridge *panel_bridge;
24 	struct drm_bridge_timings timings;
25 	struct regulator *vcc;
26 	struct gpio_desc *powerdown_gpio;
27 	u32 connector_type;
28 	unsigned int bus_format;
29 };
30 
to_lvds_codec(struct drm_bridge * bridge)31 static inline struct lvds_codec *to_lvds_codec(struct drm_bridge *bridge)
32 {
33 	return container_of(bridge, struct lvds_codec, bridge);
34 }
35 
lvds_codec_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)36 static int lvds_codec_attach(struct drm_bridge *bridge,
37 			     enum drm_bridge_attach_flags flags)
38 {
39 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
40 
41 	return drm_bridge_attach(bridge->encoder, lvds_codec->panel_bridge,
42 				 bridge, flags);
43 }
44 
lvds_codec_enable(struct drm_bridge * bridge)45 static void lvds_codec_enable(struct drm_bridge *bridge)
46 {
47 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
48 	int ret;
49 
50 	ret = regulator_enable(lvds_codec->vcc);
51 	if (ret) {
52 		dev_err(lvds_codec->dev,
53 			"Failed to enable regulator \"vcc\": %d\n", ret);
54 		return;
55 	}
56 
57 	if (lvds_codec->powerdown_gpio)
58 		gpiod_set_value_cansleep(lvds_codec->powerdown_gpio, 0);
59 }
60 
lvds_codec_disable(struct drm_bridge * bridge)61 static void lvds_codec_disable(struct drm_bridge *bridge)
62 {
63 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
64 	int ret;
65 
66 	if (lvds_codec->powerdown_gpio)
67 		gpiod_set_value_cansleep(lvds_codec->powerdown_gpio, 1);
68 
69 	ret = regulator_disable(lvds_codec->vcc);
70 	if (ret)
71 		dev_err(lvds_codec->dev,
72 			"Failed to disable regulator \"vcc\": %d\n", ret);
73 }
74 
75 #define MAX_INPUT_SEL_FORMATS 1
76 static u32 *
lvds_codec_atomic_get_input_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)77 lvds_codec_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
78 				     struct drm_bridge_state *bridge_state,
79 				     struct drm_crtc_state *crtc_state,
80 				     struct drm_connector_state *conn_state,
81 				     u32 output_fmt,
82 				     unsigned int *num_input_fmts)
83 {
84 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
85 	u32 *input_fmts;
86 
87 	*num_input_fmts = 0;
88 
89 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
90 			     GFP_KERNEL);
91 	if (!input_fmts)
92 		return NULL;
93 
94 	input_fmts[0] = lvds_codec->bus_format;
95 	*num_input_fmts = MAX_INPUT_SEL_FORMATS;
96 
97 	return input_fmts;
98 }
99 
100 static const struct drm_bridge_funcs funcs = {
101 	.attach = lvds_codec_attach,
102 	.enable = lvds_codec_enable,
103 	.disable = lvds_codec_disable,
104 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
105 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
106 	.atomic_reset = drm_atomic_helper_bridge_reset,
107 	.atomic_get_input_bus_fmts = lvds_codec_atomic_get_input_bus_fmts,
108 };
109 
lvds_codec_probe(struct platform_device * pdev)110 static int lvds_codec_probe(struct platform_device *pdev)
111 {
112 	struct device *dev = &pdev->dev;
113 	struct device_node *panel_node;
114 	struct device_node *bus_node;
115 	struct drm_panel *panel;
116 	struct lvds_codec *lvds_codec;
117 	u32 val;
118 	int ret;
119 
120 	lvds_codec = devm_kzalloc(dev, sizeof(*lvds_codec), GFP_KERNEL);
121 	if (!lvds_codec)
122 		return -ENOMEM;
123 
124 	lvds_codec->dev = &pdev->dev;
125 	lvds_codec->connector_type = (uintptr_t)of_device_get_match_data(dev);
126 
127 	lvds_codec->vcc = devm_regulator_get(lvds_codec->dev, "power");
128 	if (IS_ERR(lvds_codec->vcc))
129 		return dev_err_probe(dev, PTR_ERR(lvds_codec->vcc),
130 				     "Unable to get \"vcc\" supply\n");
131 
132 	lvds_codec->powerdown_gpio = devm_gpiod_get_optional(dev, "powerdown",
133 							     GPIOD_OUT_HIGH);
134 	if (IS_ERR(lvds_codec->powerdown_gpio))
135 		return dev_err_probe(dev, PTR_ERR(lvds_codec->powerdown_gpio),
136 				     "powerdown GPIO failure\n");
137 
138 	/* Locate the panel DT node. */
139 	panel_node = of_graph_get_remote_node(dev->of_node, 1, 0);
140 	if (!panel_node) {
141 		dev_dbg(dev, "panel DT node not found\n");
142 		return -ENXIO;
143 	}
144 
145 	panel = of_drm_find_panel(panel_node);
146 	of_node_put(panel_node);
147 	if (IS_ERR(panel)) {
148 		dev_dbg(dev, "panel not found, deferring probe\n");
149 		return PTR_ERR(panel);
150 	}
151 
152 	lvds_codec->panel_bridge =
153 		devm_drm_panel_bridge_add_typed(dev, panel,
154 						lvds_codec->connector_type);
155 	if (IS_ERR(lvds_codec->panel_bridge))
156 		return PTR_ERR(lvds_codec->panel_bridge);
157 
158 	lvds_codec->bridge.funcs = &funcs;
159 
160 	/*
161 	 * Decoder input LVDS format is a property of the decoder chip or even
162 	 * its strapping. Handle data-mapping the same way lvds-panel does. In
163 	 * case data-mapping is not present, do nothing, since there are still
164 	 * legacy bindings which do not specify this property.
165 	 */
166 	if (lvds_codec->connector_type != DRM_MODE_CONNECTOR_LVDS) {
167 		bus_node = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
168 		if (!bus_node) {
169 			dev_dbg(dev, "bus DT node not found\n");
170 			return -ENXIO;
171 		}
172 
173 		ret = drm_of_lvds_get_data_mapping(bus_node);
174 		of_node_put(bus_node);
175 		if (ret == -ENODEV) {
176 			dev_warn(dev, "missing 'data-mapping' DT property\n");
177 		} else if (ret < 0) {
178 			dev_err(dev, "invalid 'data-mapping' DT property\n");
179 			return ret;
180 		} else {
181 			lvds_codec->bus_format = ret;
182 		}
183 	} else {
184 		lvds_codec->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
185 	}
186 
187 	/*
188 	 * Encoder might sample data on different clock edge than the display,
189 	 * for example OnSemi FIN3385 has a dedicated strapping pin to select
190 	 * the sampling edge.
191 	 */
192 	if (lvds_codec->connector_type == DRM_MODE_CONNECTOR_LVDS &&
193 	    !of_property_read_u32(dev->of_node, "pclk-sample", &val)) {
194 		lvds_codec->timings.input_bus_flags = val ?
195 			DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE :
196 			DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
197 	}
198 
199 	/*
200 	 * The panel_bridge bridge is attached to the panel's of_node,
201 	 * but we need a bridge attached to our of_node for our user
202 	 * to look up.
203 	 */
204 	lvds_codec->bridge.of_node = dev->of_node;
205 	lvds_codec->bridge.timings = &lvds_codec->timings;
206 	drm_bridge_add(&lvds_codec->bridge);
207 
208 	platform_set_drvdata(pdev, lvds_codec);
209 
210 	return 0;
211 }
212 
lvds_codec_remove(struct platform_device * pdev)213 static void lvds_codec_remove(struct platform_device *pdev)
214 {
215 	struct lvds_codec *lvds_codec = platform_get_drvdata(pdev);
216 
217 	drm_bridge_remove(&lvds_codec->bridge);
218 }
219 
220 static const struct of_device_id lvds_codec_match[] = {
221 	{
222 		.compatible = "lvds-decoder",
223 		.data = (void *)DRM_MODE_CONNECTOR_DPI,
224 	},
225 	{
226 		.compatible = "lvds-encoder",
227 		.data = (void *)DRM_MODE_CONNECTOR_LVDS,
228 	},
229 	{
230 		.compatible = "thine,thc63lvdm83d",
231 		.data = (void *)DRM_MODE_CONNECTOR_LVDS,
232 	},
233 	{},
234 };
235 MODULE_DEVICE_TABLE(of, lvds_codec_match);
236 
237 static struct platform_driver lvds_codec_driver = {
238 	.probe	= lvds_codec_probe,
239 	.remove_new = lvds_codec_remove,
240 	.driver		= {
241 		.name		= "lvds-codec",
242 		.of_match_table	= lvds_codec_match,
243 	},
244 };
245 module_platform_driver(lvds_codec_driver);
246 
247 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
248 MODULE_DESCRIPTION("LVDS encoders and decoders");
249 MODULE_LICENSE("GPL");
250