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