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_device.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
13 
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_panel.h>
16 
17 struct lvds_codec {
18 	struct drm_bridge bridge;
19 	struct drm_bridge *panel_bridge;
20 	struct gpio_desc *powerdown_gpio;
21 	u32 connector_type;
22 };
23 
24 static inline struct lvds_codec *to_lvds_codec(struct drm_bridge *bridge)
25 {
26 	return container_of(bridge, struct lvds_codec, bridge);
27 }
28 
29 static int lvds_codec_attach(struct drm_bridge *bridge,
30 			     enum drm_bridge_attach_flags flags)
31 {
32 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
33 
34 	return drm_bridge_attach(bridge->encoder, lvds_codec->panel_bridge,
35 				 bridge, flags);
36 }
37 
38 static void lvds_codec_enable(struct drm_bridge *bridge)
39 {
40 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
41 
42 	if (lvds_codec->powerdown_gpio)
43 		gpiod_set_value_cansleep(lvds_codec->powerdown_gpio, 0);
44 }
45 
46 static void lvds_codec_disable(struct drm_bridge *bridge)
47 {
48 	struct lvds_codec *lvds_codec = to_lvds_codec(bridge);
49 
50 	if (lvds_codec->powerdown_gpio)
51 		gpiod_set_value_cansleep(lvds_codec->powerdown_gpio, 1);
52 }
53 
54 static const struct drm_bridge_funcs funcs = {
55 	.attach = lvds_codec_attach,
56 	.enable = lvds_codec_enable,
57 	.disable = lvds_codec_disable,
58 };
59 
60 static int lvds_codec_probe(struct platform_device *pdev)
61 {
62 	struct device *dev = &pdev->dev;
63 	struct device_node *panel_node;
64 	struct drm_panel *panel;
65 	struct lvds_codec *lvds_codec;
66 
67 	lvds_codec = devm_kzalloc(dev, sizeof(*lvds_codec), GFP_KERNEL);
68 	if (!lvds_codec)
69 		return -ENOMEM;
70 
71 	lvds_codec->connector_type = (uintptr_t)of_device_get_match_data(dev);
72 	lvds_codec->powerdown_gpio = devm_gpiod_get_optional(dev, "powerdown",
73 							     GPIOD_OUT_HIGH);
74 	if (IS_ERR(lvds_codec->powerdown_gpio)) {
75 		int err = PTR_ERR(lvds_codec->powerdown_gpio);
76 
77 		if (err != -EPROBE_DEFER)
78 			dev_err(dev, "powerdown GPIO failure: %d\n", err);
79 		return err;
80 	}
81 
82 	/* Locate the panel DT node. */
83 	panel_node = of_graph_get_remote_node(dev->of_node, 1, 0);
84 	if (!panel_node) {
85 		dev_dbg(dev, "panel DT node not found\n");
86 		return -ENXIO;
87 	}
88 
89 	panel = of_drm_find_panel(panel_node);
90 	of_node_put(panel_node);
91 	if (IS_ERR(panel)) {
92 		dev_dbg(dev, "panel not found, deferring probe\n");
93 		return PTR_ERR(panel);
94 	}
95 
96 	lvds_codec->panel_bridge =
97 		devm_drm_panel_bridge_add_typed(dev, panel,
98 						lvds_codec->connector_type);
99 	if (IS_ERR(lvds_codec->panel_bridge))
100 		return PTR_ERR(lvds_codec->panel_bridge);
101 
102 	/*
103 	 * The panel_bridge bridge is attached to the panel's of_node,
104 	 * but we need a bridge attached to our of_node for our user
105 	 * to look up.
106 	 */
107 	lvds_codec->bridge.of_node = dev->of_node;
108 	lvds_codec->bridge.funcs = &funcs;
109 	drm_bridge_add(&lvds_codec->bridge);
110 
111 	platform_set_drvdata(pdev, lvds_codec);
112 
113 	return 0;
114 }
115 
116 static int lvds_codec_remove(struct platform_device *pdev)
117 {
118 	struct lvds_codec *lvds_codec = platform_get_drvdata(pdev);
119 
120 	drm_bridge_remove(&lvds_codec->bridge);
121 
122 	return 0;
123 }
124 
125 static const struct of_device_id lvds_codec_match[] = {
126 	{
127 		.compatible = "lvds-decoder",
128 		.data = (void *)DRM_MODE_CONNECTOR_DPI,
129 	},
130 	{
131 		.compatible = "lvds-encoder",
132 		.data = (void *)DRM_MODE_CONNECTOR_LVDS,
133 	},
134 	{
135 		.compatible = "thine,thc63lvdm83d",
136 		.data = (void *)DRM_MODE_CONNECTOR_LVDS,
137 	},
138 	{},
139 };
140 MODULE_DEVICE_TABLE(of, lvds_codec_match);
141 
142 static struct platform_driver lvds_codec_driver = {
143 	.probe	= lvds_codec_probe,
144 	.remove	= lvds_codec_remove,
145 	.driver		= {
146 		.name		= "lvds-codec",
147 		.of_match_table	= lvds_codec_match,
148 	},
149 };
150 module_platform_driver(lvds_codec_driver);
151 
152 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
153 MODULE_DESCRIPTION("LVDS encoders and decoders");
154 MODULE_LICENSE("GPL");
155