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