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