Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * THC63LVD1024 LVDS to parallel data DRM bridge driver.
0004  *
0005  * Copyright (C) 2018 Jacopo Mondi <jacopo+renesas@jmondi.org>
0006  */
0007 
0008 #include <linux/gpio/consumer.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/of_graph.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/slab.h>
0015 
0016 #include <drm/drm_bridge.h>
0017 #include <drm/drm_panel.h>
0018 
0019 enum thc63_ports {
0020     THC63_LVDS_IN0,
0021     THC63_LVDS_IN1,
0022     THC63_RGB_OUT0,
0023     THC63_RGB_OUT1,
0024 };
0025 
0026 struct thc63_dev {
0027     struct device *dev;
0028 
0029     struct regulator *vcc;
0030 
0031     struct gpio_desc *pdwn;
0032     struct gpio_desc *oe;
0033 
0034     struct drm_bridge bridge;
0035     struct drm_bridge *next;
0036 
0037     struct drm_bridge_timings timings;
0038 };
0039 
0040 static inline struct thc63_dev *to_thc63(struct drm_bridge *bridge)
0041 {
0042     return container_of(bridge, struct thc63_dev, bridge);
0043 }
0044 
0045 static int thc63_attach(struct drm_bridge *bridge,
0046             enum drm_bridge_attach_flags flags)
0047 {
0048     struct thc63_dev *thc63 = to_thc63(bridge);
0049 
0050     return drm_bridge_attach(bridge->encoder, thc63->next, bridge, flags);
0051 }
0052 
0053 static enum drm_mode_status thc63_mode_valid(struct drm_bridge *bridge,
0054                     const struct drm_display_info *info,
0055                     const struct drm_display_mode *mode)
0056 {
0057     struct thc63_dev *thc63 = to_thc63(bridge);
0058     unsigned int min_freq;
0059     unsigned int max_freq;
0060 
0061     /*
0062      * The THC63LVD1024 pixel rate range is 8 to 135 MHz in all modes but
0063      * dual-in, single-out where it is 40 to 150 MHz. As dual-in, dual-out
0064      * isn't supported by the driver yet, simply derive the limits from the
0065      * input mode.
0066      */
0067     if (thc63->timings.dual_link) {
0068         min_freq = 40000;
0069         max_freq = 150000;
0070     } else {
0071         min_freq = 8000;
0072         max_freq = 135000;
0073     }
0074 
0075     if (mode->clock < min_freq)
0076         return MODE_CLOCK_LOW;
0077 
0078     if (mode->clock > max_freq)
0079         return MODE_CLOCK_HIGH;
0080 
0081     return MODE_OK;
0082 }
0083 
0084 static void thc63_enable(struct drm_bridge *bridge)
0085 {
0086     struct thc63_dev *thc63 = to_thc63(bridge);
0087     int ret;
0088 
0089     ret = regulator_enable(thc63->vcc);
0090     if (ret) {
0091         dev_err(thc63->dev,
0092             "Failed to enable regulator \"vcc\": %d\n", ret);
0093         return;
0094     }
0095 
0096     gpiod_set_value(thc63->pdwn, 0);
0097     gpiod_set_value(thc63->oe, 1);
0098 }
0099 
0100 static void thc63_disable(struct drm_bridge *bridge)
0101 {
0102     struct thc63_dev *thc63 = to_thc63(bridge);
0103     int ret;
0104 
0105     gpiod_set_value(thc63->oe, 0);
0106     gpiod_set_value(thc63->pdwn, 1);
0107 
0108     ret = regulator_disable(thc63->vcc);
0109     if (ret)
0110         dev_err(thc63->dev,
0111             "Failed to disable regulator \"vcc\": %d\n", ret);
0112 }
0113 
0114 static const struct drm_bridge_funcs thc63_bridge_func = {
0115     .attach = thc63_attach,
0116     .mode_valid = thc63_mode_valid,
0117     .enable = thc63_enable,
0118     .disable = thc63_disable,
0119 };
0120 
0121 static int thc63_parse_dt(struct thc63_dev *thc63)
0122 {
0123     struct device_node *endpoint;
0124     struct device_node *remote;
0125 
0126     endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
0127                          THC63_RGB_OUT0, -1);
0128     if (!endpoint) {
0129         dev_err(thc63->dev, "Missing endpoint in port@%u\n",
0130             THC63_RGB_OUT0);
0131         return -ENODEV;
0132     }
0133 
0134     remote = of_graph_get_remote_port_parent(endpoint);
0135     of_node_put(endpoint);
0136     if (!remote) {
0137         dev_err(thc63->dev, "Endpoint in port@%u unconnected\n",
0138             THC63_RGB_OUT0);
0139         return -ENODEV;
0140     }
0141 
0142     if (!of_device_is_available(remote)) {
0143         dev_err(thc63->dev, "port@%u remote endpoint is disabled\n",
0144             THC63_RGB_OUT0);
0145         of_node_put(remote);
0146         return -ENODEV;
0147     }
0148 
0149     thc63->next = of_drm_find_bridge(remote);
0150     of_node_put(remote);
0151     if (!thc63->next)
0152         return -EPROBE_DEFER;
0153 
0154     endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
0155                          THC63_LVDS_IN1, -1);
0156     if (endpoint) {
0157         remote = of_graph_get_remote_port_parent(endpoint);
0158         of_node_put(endpoint);
0159 
0160         if (remote) {
0161             if (of_device_is_available(remote))
0162                 thc63->timings.dual_link = true;
0163             of_node_put(remote);
0164         }
0165     }
0166 
0167     dev_dbg(thc63->dev, "operating in %s-link mode\n",
0168         thc63->timings.dual_link ? "dual" : "single");
0169 
0170     return 0;
0171 }
0172 
0173 static int thc63_gpio_init(struct thc63_dev *thc63)
0174 {
0175     thc63->oe = devm_gpiod_get_optional(thc63->dev, "oe", GPIOD_OUT_LOW);
0176     if (IS_ERR(thc63->oe)) {
0177         dev_err(thc63->dev, "Unable to get \"oe-gpios\": %ld\n",
0178             PTR_ERR(thc63->oe));
0179         return PTR_ERR(thc63->oe);
0180     }
0181 
0182     thc63->pdwn = devm_gpiod_get_optional(thc63->dev, "powerdown",
0183                           GPIOD_OUT_HIGH);
0184     if (IS_ERR(thc63->pdwn)) {
0185         dev_err(thc63->dev, "Unable to get \"powerdown-gpios\": %ld\n",
0186             PTR_ERR(thc63->pdwn));
0187         return PTR_ERR(thc63->pdwn);
0188     }
0189 
0190     return 0;
0191 }
0192 
0193 static int thc63_probe(struct platform_device *pdev)
0194 {
0195     struct thc63_dev *thc63;
0196     int ret;
0197 
0198     thc63 = devm_kzalloc(&pdev->dev, sizeof(*thc63), GFP_KERNEL);
0199     if (!thc63)
0200         return -ENOMEM;
0201 
0202     thc63->dev = &pdev->dev;
0203     platform_set_drvdata(pdev, thc63);
0204 
0205     thc63->vcc = devm_regulator_get(thc63->dev, "vcc");
0206     if (IS_ERR(thc63->vcc)) {
0207         if (PTR_ERR(thc63->vcc) == -EPROBE_DEFER)
0208             return -EPROBE_DEFER;
0209 
0210         dev_err(thc63->dev, "Unable to get \"vcc\" supply: %ld\n",
0211             PTR_ERR(thc63->vcc));
0212         return PTR_ERR(thc63->vcc);
0213     }
0214 
0215     ret = thc63_gpio_init(thc63);
0216     if (ret)
0217         return ret;
0218 
0219     ret = thc63_parse_dt(thc63);
0220     if (ret)
0221         return ret;
0222 
0223     thc63->bridge.driver_private = thc63;
0224     thc63->bridge.of_node = pdev->dev.of_node;
0225     thc63->bridge.funcs = &thc63_bridge_func;
0226     thc63->bridge.timings = &thc63->timings;
0227 
0228     drm_bridge_add(&thc63->bridge);
0229 
0230     return 0;
0231 }
0232 
0233 static int thc63_remove(struct platform_device *pdev)
0234 {
0235     struct thc63_dev *thc63 = platform_get_drvdata(pdev);
0236 
0237     drm_bridge_remove(&thc63->bridge);
0238 
0239     return 0;
0240 }
0241 
0242 static const struct of_device_id thc63_match[] = {
0243     { .compatible = "thine,thc63lvd1024", },
0244     { },
0245 };
0246 MODULE_DEVICE_TABLE(of, thc63_match);
0247 
0248 static struct platform_driver thc63_driver = {
0249     .probe  = thc63_probe,
0250     .remove = thc63_remove,
0251     .driver = {
0252         .name       = "thc63lvd1024",
0253         .of_match_table = thc63_match,
0254     },
0255 };
0256 module_platform_driver(thc63_driver);
0257 
0258 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
0259 MODULE_DESCRIPTION("Thine THC63LVD1024 LVDS decoder DRM bridge driver");
0260 MODULE_LICENSE("GPL v2");