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