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 		return dev_err_probe(dev, PTR_ERR(lvds_codec->powerdown_gpio),
76 				     "powerdown GPIO failure\n");
77 
78 	/* Locate the panel DT node. */
79 	panel_node = of_graph_get_remote_node(dev->of_node, 1, 0);
80 	if (!panel_node) {
81 		dev_dbg(dev, "panel DT node not found\n");
82 		return -ENXIO;
83 	}
84 
85 	panel = of_drm_find_panel(panel_node);
86 	of_node_put(panel_node);
87 	if (IS_ERR(panel)) {
88 		dev_dbg(dev, "panel not found, deferring probe\n");
89 		return PTR_ERR(panel);
90 	}
91 
92 	lvds_codec->panel_bridge =
93 		devm_drm_panel_bridge_add_typed(dev, panel,
94 						lvds_codec->connector_type);
95 	if (IS_ERR(lvds_codec->panel_bridge))
96 		return PTR_ERR(lvds_codec->panel_bridge);
97 
98 	/*
99 	 * The panel_bridge bridge is attached to the panel's of_node,
100 	 * but we need a bridge attached to our of_node for our user
101 	 * to look up.
102 	 */
103 	lvds_codec->bridge.of_node = dev->of_node;
104 	lvds_codec->bridge.funcs = &funcs;
105 	drm_bridge_add(&lvds_codec->bridge);
106 
107 	platform_set_drvdata(pdev, lvds_codec);
108 
109 	return 0;
110 }
111 
112 static int lvds_codec_remove(struct platform_device *pdev)
113 {
114 	struct lvds_codec *lvds_codec = platform_get_drvdata(pdev);
115 
116 	drm_bridge_remove(&lvds_codec->bridge);
117 
118 	return 0;
119 }
120 
121 static const struct of_device_id lvds_codec_match[] = {
122 	{
123 		.compatible = "lvds-decoder",
124 		.data = (void *)DRM_MODE_CONNECTOR_DPI,
125 	},
126 	{
127 		.compatible = "lvds-encoder",
128 		.data = (void *)DRM_MODE_CONNECTOR_LVDS,
129 	},
130 	{
131 		.compatible = "thine,thc63lvdm83d",
132 		.data = (void *)DRM_MODE_CONNECTOR_LVDS,
133 	},
134 	{},
135 };
136 MODULE_DEVICE_TABLE(of, lvds_codec_match);
137 
138 static struct platform_driver lvds_codec_driver = {
139 	.probe	= lvds_codec_probe,
140 	.remove	= lvds_codec_remove,
141 	.driver		= {
142 		.name		= "lvds-codec",
143 		.of_match_table	= lvds_codec_match,
144 	},
145 };
146 module_platform_driver(lvds_codec_driver);
147 
148 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
149 MODULE_DESCRIPTION("LVDS encoders and decoders");
150 MODULE_LICENSE("GPL");
151