1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Renesas Electronics Corporation
7 #include <linux/gpio/consumer.h>
8 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/consumer.h>
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>
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;
28 unsigned int bus_format;
31 static inline struct lvds_codec *to_lvds_codec(struct drm_bridge *bridge)
33 return container_of(bridge, struct lvds_codec, bridge);
36 static int lvds_codec_attach(struct drm_bridge *bridge,
37 enum drm_bridge_attach_flags flags)
39 struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
41 return drm_bridge_attach(bridge->encoder, lvds_codec->panel_bridge,
45 static void lvds_codec_enable(struct drm_bridge *bridge)
47 struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
50 ret = regulator_enable(lvds_codec->vcc);
52 dev_err(lvds_codec->dev,
53 "Failed to enable regulator \"vcc\": %d\n", ret);
57 if (lvds_codec->powerdown_gpio)
58 gpiod_set_value_cansleep(lvds_codec->powerdown_gpio, 0);
61 static void lvds_codec_disable(struct drm_bridge *bridge)
63 struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
66 if (lvds_codec->powerdown_gpio)
67 gpiod_set_value_cansleep(lvds_codec->powerdown_gpio, 1);
69 ret = regulator_disable(lvds_codec->vcc);
71 dev_err(lvds_codec->dev,
72 "Failed to disable regulator \"vcc\": %d\n", ret);
75 static const struct drm_bridge_funcs funcs = {
76 .attach = lvds_codec_attach,
77 .enable = lvds_codec_enable,
78 .disable = lvds_codec_disable,
81 #define MAX_INPUT_SEL_FORMATS 1
83 lvds_codec_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
84 struct drm_bridge_state *bridge_state,
85 struct drm_crtc_state *crtc_state,
86 struct drm_connector_state *conn_state,
88 unsigned int *num_input_fmts)
90 struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
95 input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
100 input_fmts[0] = lvds_codec->bus_format;
101 *num_input_fmts = MAX_INPUT_SEL_FORMATS;
106 static const struct drm_bridge_funcs funcs_decoder = {
107 .attach = lvds_codec_attach,
108 .enable = lvds_codec_enable,
109 .disable = lvds_codec_disable,
110 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
111 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
112 .atomic_reset = drm_atomic_helper_bridge_reset,
113 .atomic_get_input_bus_fmts = lvds_codec_atomic_get_input_bus_fmts,
116 static int lvds_codec_probe(struct platform_device *pdev)
118 struct device *dev = &pdev->dev;
119 struct device_node *panel_node;
120 struct device_node *bus_node;
121 struct drm_panel *panel;
122 struct lvds_codec *lvds_codec;
126 lvds_codec = devm_kzalloc(dev, sizeof(*lvds_codec), GFP_KERNEL);
130 lvds_codec->dev = &pdev->dev;
131 lvds_codec->connector_type = (uintptr_t)of_device_get_match_data(dev);
133 lvds_codec->vcc = devm_regulator_get(lvds_codec->dev, "power");
134 if (IS_ERR(lvds_codec->vcc))
135 return dev_err_probe(dev, PTR_ERR(lvds_codec->vcc),
136 "Unable to get \"vcc\" supply\n");
138 lvds_codec->powerdown_gpio = devm_gpiod_get_optional(dev, "powerdown",
140 if (IS_ERR(lvds_codec->powerdown_gpio))
141 return dev_err_probe(dev, PTR_ERR(lvds_codec->powerdown_gpio),
142 "powerdown GPIO failure\n");
144 /* Locate the panel DT node. */
145 panel_node = of_graph_get_remote_node(dev->of_node, 1, 0);
147 dev_dbg(dev, "panel DT node not found\n");
151 panel = of_drm_find_panel(panel_node);
152 of_node_put(panel_node);
154 dev_dbg(dev, "panel not found, deferring probe\n");
155 return PTR_ERR(panel);
158 lvds_codec->panel_bridge =
159 devm_drm_panel_bridge_add_typed(dev, panel,
160 lvds_codec->connector_type);
161 if (IS_ERR(lvds_codec->panel_bridge))
162 return PTR_ERR(lvds_codec->panel_bridge);
164 lvds_codec->bridge.funcs = &funcs;
167 * Decoder input LVDS format is a property of the decoder chip or even
168 * its strapping. Handle data-mapping the same way lvds-panel does. In
169 * case data-mapping is not present, do nothing, since there are still
170 * legacy bindings which do not specify this property.
172 if (lvds_codec->connector_type != DRM_MODE_CONNECTOR_LVDS) {
173 bus_node = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
175 dev_dbg(dev, "bus DT node not found\n");
179 ret = drm_of_lvds_get_data_mapping(bus_node);
180 of_node_put(bus_node);
181 if (ret == -ENODEV) {
182 dev_warn(dev, "missing 'data-mapping' DT property\n");
184 dev_err(dev, "invalid 'data-mapping' DT property\n");
187 lvds_codec->bus_format = ret;
188 lvds_codec->bridge.funcs = &funcs_decoder;
193 * Encoder might sample data on different clock edge than the display,
194 * for example OnSemi FIN3385 has a dedicated strapping pin to select
197 if (lvds_codec->connector_type == DRM_MODE_CONNECTOR_LVDS &&
198 !of_property_read_u32(dev->of_node, "pclk-sample", &val)) {
199 lvds_codec->timings.input_bus_flags = val ?
200 DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE :
201 DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
205 * The panel_bridge bridge is attached to the panel's of_node,
206 * but we need a bridge attached to our of_node for our user
209 lvds_codec->bridge.of_node = dev->of_node;
210 lvds_codec->bridge.timings = &lvds_codec->timings;
211 drm_bridge_add(&lvds_codec->bridge);
213 platform_set_drvdata(pdev, lvds_codec);
218 static int lvds_codec_remove(struct platform_device *pdev)
220 struct lvds_codec *lvds_codec = platform_get_drvdata(pdev);
222 drm_bridge_remove(&lvds_codec->bridge);
227 static const struct of_device_id lvds_codec_match[] = {
229 .compatible = "lvds-decoder",
230 .data = (void *)DRM_MODE_CONNECTOR_DPI,
233 .compatible = "lvds-encoder",
234 .data = (void *)DRM_MODE_CONNECTOR_LVDS,
237 .compatible = "thine,thc63lvdm83d",
238 .data = (void *)DRM_MODE_CONNECTOR_LVDS,
242 MODULE_DEVICE_TABLE(of, lvds_codec_match);
244 static struct platform_driver lvds_codec_driver = {
245 .probe = lvds_codec_probe,
246 .remove = lvds_codec_remove,
248 .name = "lvds-codec",
249 .of_match_table = lvds_codec_match,
252 module_platform_driver(lvds_codec_driver);
255 MODULE_DESCRIPTION("LVDS encoders and decoders");
256 MODULE_LICENSE("GPL");