Path: blob/master/drivers/gpu/drm/bridge/thc63lvd1024.c
51176 views
// SPDX-License-Identifier: GPL-2.01/*2* THC63LVD1024 LVDS to parallel data DRM bridge driver.3*4* Copyright (C) 2018 Jacopo Mondi <[email protected]>5*/67#include <linux/gpio/consumer.h>8#include <linux/module.h>9#include <linux/of.h>10#include <linux/of_graph.h>11#include <linux/platform_device.h>12#include <linux/regulator/consumer.h>13#include <linux/slab.h>1415#include <drm/drm_bridge.h>16#include <drm/drm_panel.h>1718enum thc63_ports {19THC63_LVDS_IN0,20THC63_LVDS_IN1,21THC63_RGB_OUT0,22THC63_RGB_OUT1,23};2425struct thc63_dev {26struct device *dev;2728struct regulator *vcc;2930struct gpio_desc *pdwn;31struct gpio_desc *oe;3233struct drm_bridge bridge;3435struct drm_bridge_timings timings;36};3738static inline struct thc63_dev *to_thc63(struct drm_bridge *bridge)39{40return container_of(bridge, struct thc63_dev, bridge);41}4243static int thc63_attach(struct drm_bridge *bridge,44struct drm_encoder *encoder,45enum drm_bridge_attach_flags flags)46{47struct thc63_dev *thc63 = to_thc63(bridge);4849return drm_bridge_attach(encoder, thc63->bridge.next_bridge, bridge, flags);50}5152static enum drm_mode_status thc63_mode_valid(struct drm_bridge *bridge,53const struct drm_display_info *info,54const struct drm_display_mode *mode)55{56struct thc63_dev *thc63 = to_thc63(bridge);57unsigned int min_freq;58unsigned int max_freq;5960/*61* The THC63LVD1024 pixel rate range is 8 to 135 MHz in all modes but62* dual-in, single-out where it is 40 to 150 MHz. As dual-in, dual-out63* isn't supported by the driver yet, simply derive the limits from the64* input mode.65*/66if (thc63->timings.dual_link) {67min_freq = 40000;68max_freq = 150000;69} else {70min_freq = 8000;71max_freq = 135000;72}7374if (mode->clock < min_freq)75return MODE_CLOCK_LOW;7677if (mode->clock > max_freq)78return MODE_CLOCK_HIGH;7980return MODE_OK;81}8283static void thc63_enable(struct drm_bridge *bridge)84{85struct thc63_dev *thc63 = to_thc63(bridge);86int ret;8788ret = regulator_enable(thc63->vcc);89if (ret) {90dev_err(thc63->dev,91"Failed to enable regulator \"vcc\": %d\n", ret);92return;93}9495gpiod_set_value(thc63->pdwn, 0);96gpiod_set_value(thc63->oe, 1);97}9899static void thc63_disable(struct drm_bridge *bridge)100{101struct thc63_dev *thc63 = to_thc63(bridge);102int ret;103104gpiod_set_value(thc63->oe, 0);105gpiod_set_value(thc63->pdwn, 1);106107ret = regulator_disable(thc63->vcc);108if (ret)109dev_err(thc63->dev,110"Failed to disable regulator \"vcc\": %d\n", ret);111}112113static const struct drm_bridge_funcs thc63_bridge_func = {114.attach = thc63_attach,115.mode_valid = thc63_mode_valid,116.enable = thc63_enable,117.disable = thc63_disable,118};119120static int thc63_parse_dt(struct thc63_dev *thc63)121{122struct device_node *endpoint;123struct device_node *remote;124125remote = of_graph_get_remote_node(thc63->dev->of_node,126THC63_RGB_OUT0, -1);127if (!remote) {128dev_err(thc63->dev, "No remote endpoint for port@%u\n",129THC63_RGB_OUT0);130return -ENODEV;131}132133thc63->bridge.next_bridge = of_drm_find_and_get_bridge(remote);134of_node_put(remote);135if (!thc63->bridge.next_bridge)136return -EPROBE_DEFER;137138endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,139THC63_LVDS_IN1, -1);140if (endpoint) {141remote = of_graph_get_remote_port_parent(endpoint);142of_node_put(endpoint);143144if (remote) {145if (of_device_is_available(remote))146thc63->timings.dual_link = true;147of_node_put(remote);148}149}150151dev_dbg(thc63->dev, "operating in %s-link mode\n",152thc63->timings.dual_link ? "dual" : "single");153154return 0;155}156157static int thc63_gpio_init(struct thc63_dev *thc63)158{159thc63->oe = devm_gpiod_get_optional(thc63->dev, "oe", GPIOD_OUT_LOW);160if (IS_ERR(thc63->oe)) {161dev_err(thc63->dev, "Unable to get \"oe-gpios\": %ld\n",162PTR_ERR(thc63->oe));163return PTR_ERR(thc63->oe);164}165166thc63->pdwn = devm_gpiod_get_optional(thc63->dev, "powerdown",167GPIOD_OUT_HIGH);168if (IS_ERR(thc63->pdwn)) {169dev_err(thc63->dev, "Unable to get \"powerdown-gpios\": %ld\n",170PTR_ERR(thc63->pdwn));171return PTR_ERR(thc63->pdwn);172}173174return 0;175}176177static int thc63_probe(struct platform_device *pdev)178{179struct thc63_dev *thc63;180int ret;181182thc63 = devm_drm_bridge_alloc(&pdev->dev, struct thc63_dev, bridge,183&thc63_bridge_func);184if (IS_ERR(thc63))185return PTR_ERR(thc63);186187thc63->dev = &pdev->dev;188platform_set_drvdata(pdev, thc63);189190thc63->vcc = devm_regulator_get(thc63->dev, "vcc");191if (IS_ERR(thc63->vcc)) {192if (PTR_ERR(thc63->vcc) == -EPROBE_DEFER)193return -EPROBE_DEFER;194195dev_err(thc63->dev, "Unable to get \"vcc\" supply: %ld\n",196PTR_ERR(thc63->vcc));197return PTR_ERR(thc63->vcc);198}199200ret = thc63_gpio_init(thc63);201if (ret)202return ret;203204ret = thc63_parse_dt(thc63);205if (ret)206return ret;207208thc63->bridge.driver_private = thc63;209thc63->bridge.of_node = pdev->dev.of_node;210thc63->bridge.timings = &thc63->timings;211212drm_bridge_add(&thc63->bridge);213214return 0;215}216217static void thc63_remove(struct platform_device *pdev)218{219struct thc63_dev *thc63 = platform_get_drvdata(pdev);220221drm_bridge_remove(&thc63->bridge);222}223224static const struct of_device_id thc63_match[] = {225{ .compatible = "thine,thc63lvd1024", },226{ },227};228MODULE_DEVICE_TABLE(of, thc63_match);229230static struct platform_driver thc63_driver = {231.probe = thc63_probe,232.remove = thc63_remove,233.driver = {234.name = "thc63lvd1024",235.of_match_table = thc63_match,236},237};238module_platform_driver(thc63_driver);239240MODULE_AUTHOR("Jacopo Mondi <[email protected]>");241MODULE_DESCRIPTION("Thine THC63LVD1024 LVDS decoder DRM bridge driver");242MODULE_LICENSE("GPL v2");243244245