Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2016 Texas Instruments
0004  * Author: Jyri Sarha <jsarha@ti.com>
0005  */
0006 
0007 #include <linux/gpio/consumer.h>
0008 #include <linux/i2c.h>
0009 #include <linux/media-bus-format.h>
0010 #include <linux/module.h>
0011 #include <linux/of_graph.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/workqueue.h>
0014 
0015 #include <drm/drm_atomic_helper.h>
0016 #include <drm/drm_bridge.h>
0017 #include <drm/drm_crtc.h>
0018 #include <drm/drm_edid.h>
0019 #include <drm/drm_print.h>
0020 #include <drm/drm_probe_helper.h>
0021 
0022 #define HOTPLUG_DEBOUNCE_MS     1100
0023 
0024 struct tfp410 {
0025     struct drm_bridge   bridge;
0026     struct drm_connector    connector;
0027 
0028     u32         bus_format;
0029     struct delayed_work hpd_work;
0030     struct gpio_desc    *powerdown;
0031 
0032     struct drm_bridge_timings timings;
0033     struct drm_bridge   *next_bridge;
0034 
0035     struct device *dev;
0036 };
0037 
0038 static inline struct tfp410 *
0039 drm_bridge_to_tfp410(struct drm_bridge *bridge)
0040 {
0041     return container_of(bridge, struct tfp410, bridge);
0042 }
0043 
0044 static inline struct tfp410 *
0045 drm_connector_to_tfp410(struct drm_connector *connector)
0046 {
0047     return container_of(connector, struct tfp410, connector);
0048 }
0049 
0050 static int tfp410_get_modes(struct drm_connector *connector)
0051 {
0052     struct tfp410 *dvi = drm_connector_to_tfp410(connector);
0053     struct edid *edid;
0054     int ret;
0055 
0056     if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
0057         edid = drm_bridge_get_edid(dvi->next_bridge, connector);
0058         if (!edid)
0059             DRM_INFO("EDID read failed. Fallback to standard modes\n");
0060     } else {
0061         edid = NULL;
0062     }
0063 
0064     if (!edid) {
0065         /*
0066          * No EDID, fallback on the XGA standard modes and prefer a mode
0067          * pretty much anything can handle.
0068          */
0069         ret = drm_add_modes_noedid(connector, 1920, 1200);
0070         drm_set_preferred_mode(connector, 1024, 768);
0071         return ret;
0072     }
0073 
0074     drm_connector_update_edid_property(connector, edid);
0075 
0076     ret = drm_add_edid_modes(connector, edid);
0077 
0078     kfree(edid);
0079 
0080     return ret;
0081 }
0082 
0083 static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
0084     .get_modes  = tfp410_get_modes,
0085 };
0086 
0087 static enum drm_connector_status
0088 tfp410_connector_detect(struct drm_connector *connector, bool force)
0089 {
0090     struct tfp410 *dvi = drm_connector_to_tfp410(connector);
0091 
0092     return drm_bridge_detect(dvi->next_bridge);
0093 }
0094 
0095 static const struct drm_connector_funcs tfp410_con_funcs = {
0096     .detect         = tfp410_connector_detect,
0097     .fill_modes     = drm_helper_probe_single_connector_modes,
0098     .destroy        = drm_connector_cleanup,
0099     .reset          = drm_atomic_helper_connector_reset,
0100     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
0101     .atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
0102 };
0103 
0104 static void tfp410_hpd_work_func(struct work_struct *work)
0105 {
0106     struct tfp410 *dvi;
0107 
0108     dvi = container_of(work, struct tfp410, hpd_work.work);
0109 
0110     if (dvi->bridge.dev)
0111         drm_helper_hpd_irq_event(dvi->bridge.dev);
0112 }
0113 
0114 static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
0115 {
0116     struct tfp410 *dvi = arg;
0117 
0118     mod_delayed_work(system_wq, &dvi->hpd_work,
0119              msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
0120 }
0121 
0122 static int tfp410_attach(struct drm_bridge *bridge,
0123              enum drm_bridge_attach_flags flags)
0124 {
0125     struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
0126     int ret;
0127 
0128     ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge,
0129                 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
0130     if (ret < 0)
0131         return ret;
0132 
0133     if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
0134         return 0;
0135 
0136     if (!bridge->encoder) {
0137         dev_err(dvi->dev, "Missing encoder\n");
0138         return -ENODEV;
0139     }
0140 
0141     if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
0142         dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
0143     else
0144         dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
0145 
0146     if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
0147         INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
0148         drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
0149                       dvi);
0150     }
0151 
0152     drm_connector_helper_add(&dvi->connector,
0153                  &tfp410_con_helper_funcs);
0154     ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
0155                       &tfp410_con_funcs,
0156                       dvi->next_bridge->type,
0157                       dvi->next_bridge->ddc);
0158     if (ret) {
0159         dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
0160             ret);
0161         return ret;
0162     }
0163 
0164     drm_display_info_set_bus_formats(&dvi->connector.display_info,
0165                      &dvi->bus_format, 1);
0166 
0167     drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
0168 
0169     return 0;
0170 }
0171 
0172 static void tfp410_detach(struct drm_bridge *bridge)
0173 {
0174     struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
0175 
0176     if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
0177         drm_bridge_hpd_disable(dvi->next_bridge);
0178         cancel_delayed_work_sync(&dvi->hpd_work);
0179     }
0180 }
0181 
0182 static void tfp410_enable(struct drm_bridge *bridge)
0183 {
0184     struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
0185 
0186     gpiod_set_value_cansleep(dvi->powerdown, 0);
0187 }
0188 
0189 static void tfp410_disable(struct drm_bridge *bridge)
0190 {
0191     struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
0192 
0193     gpiod_set_value_cansleep(dvi->powerdown, 1);
0194 }
0195 
0196 static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
0197                           const struct drm_display_info *info,
0198                           const struct drm_display_mode *mode)
0199 {
0200     if (mode->clock < 25000)
0201         return MODE_CLOCK_LOW;
0202 
0203     if (mode->clock > 165000)
0204         return MODE_CLOCK_HIGH;
0205 
0206     return MODE_OK;
0207 }
0208 
0209 static const struct drm_bridge_funcs tfp410_bridge_funcs = {
0210     .attach     = tfp410_attach,
0211     .detach     = tfp410_detach,
0212     .enable     = tfp410_enable,
0213     .disable    = tfp410_disable,
0214     .mode_valid = tfp410_mode_valid,
0215 };
0216 
0217 static const struct drm_bridge_timings tfp410_default_timings = {
0218     .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
0219              | DRM_BUS_FLAG_DE_HIGH,
0220     .setup_time_ps = 1200,
0221     .hold_time_ps = 1300,
0222 };
0223 
0224 static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
0225 {
0226     struct drm_bridge_timings *timings = &dvi->timings;
0227     struct device_node *ep;
0228     u32 pclk_sample = 0;
0229     u32 bus_width = 24;
0230     u32 deskew = 0;
0231 
0232     /* Start with defaults. */
0233     *timings = tfp410_default_timings;
0234 
0235     if (i2c)
0236         /*
0237          * In I2C mode timings are configured through the I2C interface.
0238          * As the driver doesn't support I2C configuration yet, we just
0239          * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
0240          */
0241         return 0;
0242 
0243     /*
0244      * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
0245      * and EDGE pins. They are specified in DT through endpoint properties
0246      * and vendor-specific properties.
0247      */
0248     ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
0249     if (!ep)
0250         return -EINVAL;
0251 
0252     /* Get the sampling edge from the endpoint. */
0253     of_property_read_u32(ep, "pclk-sample", &pclk_sample);
0254     of_property_read_u32(ep, "bus-width", &bus_width);
0255     of_node_put(ep);
0256 
0257     timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
0258 
0259     switch (pclk_sample) {
0260     case 0:
0261         timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
0262                      |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
0263         break;
0264     case 1:
0265         timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
0266                      |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
0267         break;
0268     default:
0269         return -EINVAL;
0270     }
0271 
0272     switch (bus_width) {
0273     case 12:
0274         dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
0275         break;
0276     case 24:
0277         dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
0278         break;
0279     default:
0280         return -EINVAL;
0281     }
0282 
0283     /* Get the setup and hold time from vendor-specific properties. */
0284     of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
0285     if (deskew > 7)
0286         return -EINVAL;
0287 
0288     timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
0289     timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
0290 
0291     return 0;
0292 }
0293 
0294 static int tfp410_init(struct device *dev, bool i2c)
0295 {
0296     struct device_node *node;
0297     struct tfp410 *dvi;
0298     int ret;
0299 
0300     if (!dev->of_node) {
0301         dev_err(dev, "device-tree data is missing\n");
0302         return -ENXIO;
0303     }
0304 
0305     dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
0306     if (!dvi)
0307         return -ENOMEM;
0308 
0309     dvi->dev = dev;
0310     dev_set_drvdata(dev, dvi);
0311 
0312     dvi->bridge.funcs = &tfp410_bridge_funcs;
0313     dvi->bridge.of_node = dev->of_node;
0314     dvi->bridge.timings = &dvi->timings;
0315     dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
0316 
0317     ret = tfp410_parse_timings(dvi, i2c);
0318     if (ret)
0319         return ret;
0320 
0321     /* Get the next bridge, connected to port@1. */
0322     node = of_graph_get_remote_node(dev->of_node, 1, -1);
0323     if (!node)
0324         return -ENODEV;
0325 
0326     dvi->next_bridge = of_drm_find_bridge(node);
0327     of_node_put(node);
0328 
0329     if (!dvi->next_bridge)
0330         return -EPROBE_DEFER;
0331 
0332     /* Get the powerdown GPIO. */
0333     dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
0334                          GPIOD_OUT_HIGH);
0335     if (IS_ERR(dvi->powerdown)) {
0336         dev_err(dev, "failed to parse powerdown gpio\n");
0337         return PTR_ERR(dvi->powerdown);
0338     }
0339 
0340     /*  Register the DRM bridge. */
0341     drm_bridge_add(&dvi->bridge);
0342 
0343     return 0;
0344 }
0345 
0346 static void tfp410_fini(struct device *dev)
0347 {
0348     struct tfp410 *dvi = dev_get_drvdata(dev);
0349 
0350     drm_bridge_remove(&dvi->bridge);
0351 }
0352 
0353 static int tfp410_probe(struct platform_device *pdev)
0354 {
0355     return tfp410_init(&pdev->dev, false);
0356 }
0357 
0358 static int tfp410_remove(struct platform_device *pdev)
0359 {
0360     tfp410_fini(&pdev->dev);
0361 
0362     return 0;
0363 }
0364 
0365 static const struct of_device_id tfp410_match[] = {
0366     { .compatible = "ti,tfp410" },
0367     {},
0368 };
0369 MODULE_DEVICE_TABLE(of, tfp410_match);
0370 
0371 static struct platform_driver tfp410_platform_driver = {
0372     .probe  = tfp410_probe,
0373     .remove = tfp410_remove,
0374     .driver = {
0375         .name       = "tfp410-bridge",
0376         .of_match_table = tfp410_match,
0377     },
0378 };
0379 
0380 #if IS_ENABLED(CONFIG_I2C)
0381 /* There is currently no i2c functionality. */
0382 static int tfp410_i2c_probe(struct i2c_client *client,
0383                 const struct i2c_device_id *id)
0384 {
0385     int reg;
0386 
0387     if (!client->dev.of_node ||
0388         of_property_read_u32(client->dev.of_node, "reg", &reg)) {
0389         dev_err(&client->dev,
0390             "Can't get i2c reg property from device-tree\n");
0391         return -ENXIO;
0392     }
0393 
0394     return tfp410_init(&client->dev, true);
0395 }
0396 
0397 static int tfp410_i2c_remove(struct i2c_client *client)
0398 {
0399     tfp410_fini(&client->dev);
0400 
0401     return 0;
0402 }
0403 
0404 static const struct i2c_device_id tfp410_i2c_ids[] = {
0405     { "tfp410", 0 },
0406     { }
0407 };
0408 MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
0409 
0410 static struct i2c_driver tfp410_i2c_driver = {
0411     .driver = {
0412         .name   = "tfp410",
0413         .of_match_table = of_match_ptr(tfp410_match),
0414     },
0415     .id_table   = tfp410_i2c_ids,
0416     .probe      = tfp410_i2c_probe,
0417     .remove     = tfp410_i2c_remove,
0418 };
0419 #endif /* IS_ENABLED(CONFIG_I2C) */
0420 
0421 static struct {
0422     uint i2c:1;
0423     uint platform:1;
0424 }  tfp410_registered_driver;
0425 
0426 static int __init tfp410_module_init(void)
0427 {
0428     int ret;
0429 
0430 #if IS_ENABLED(CONFIG_I2C)
0431     ret = i2c_add_driver(&tfp410_i2c_driver);
0432     if (ret)
0433         pr_err("%s: registering i2c driver failed: %d",
0434                __func__, ret);
0435     else
0436         tfp410_registered_driver.i2c = 1;
0437 #endif
0438 
0439     ret = platform_driver_register(&tfp410_platform_driver);
0440     if (ret)
0441         pr_err("%s: registering platform driver failed: %d",
0442                __func__, ret);
0443     else
0444         tfp410_registered_driver.platform = 1;
0445 
0446     if (tfp410_registered_driver.i2c ||
0447         tfp410_registered_driver.platform)
0448         return 0;
0449 
0450     return ret;
0451 }
0452 module_init(tfp410_module_init);
0453 
0454 static void __exit tfp410_module_exit(void)
0455 {
0456 #if IS_ENABLED(CONFIG_I2C)
0457     if (tfp410_registered_driver.i2c)
0458         i2c_del_driver(&tfp410_i2c_driver);
0459 #endif
0460     if (tfp410_registered_driver.platform)
0461         platform_driver_unregister(&tfp410_platform_driver);
0462 }
0463 module_exit(tfp410_module_exit);
0464 
0465 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
0466 MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
0467 MODULE_LICENSE("GPL");