Path: blob/master/drivers/gpu/drm/bridge/thc63lvd1024.c
26494 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;34struct drm_bridge *next;3536struct drm_bridge_timings timings;37};3839static inline struct thc63_dev *to_thc63(struct drm_bridge *bridge)40{41return container_of(bridge, struct thc63_dev, bridge);42}4344static int thc63_attach(struct drm_bridge *bridge,45struct drm_encoder *encoder,46enum drm_bridge_attach_flags flags)47{48struct thc63_dev *thc63 = to_thc63(bridge);4950return drm_bridge_attach(encoder, thc63->next, bridge, flags);51}5253static enum drm_mode_status thc63_mode_valid(struct drm_bridge *bridge,54const struct drm_display_info *info,55const struct drm_display_mode *mode)56{57struct thc63_dev *thc63 = to_thc63(bridge);58unsigned int min_freq;59unsigned int max_freq;6061/*62* The THC63LVD1024 pixel rate range is 8 to 135 MHz in all modes but63* dual-in, single-out where it is 40 to 150 MHz. As dual-in, dual-out64* isn't supported by the driver yet, simply derive the limits from the65* input mode.66*/67if (thc63->timings.dual_link) {68min_freq = 40000;69max_freq = 150000;70} else {71min_freq = 8000;72max_freq = 135000;73}7475if (mode->clock < min_freq)76return MODE_CLOCK_LOW;7778if (mode->clock > max_freq)79return MODE_CLOCK_HIGH;8081return MODE_OK;82}8384static void thc63_enable(struct drm_bridge *bridge)85{86struct thc63_dev *thc63 = to_thc63(bridge);87int ret;8889ret = regulator_enable(thc63->vcc);90if (ret) {91dev_err(thc63->dev,92"Failed to enable regulator \"vcc\": %d\n", ret);93return;94}9596gpiod_set_value(thc63->pdwn, 0);97gpiod_set_value(thc63->oe, 1);98}99100static void thc63_disable(struct drm_bridge *bridge)101{102struct thc63_dev *thc63 = to_thc63(bridge);103int ret;104105gpiod_set_value(thc63->oe, 0);106gpiod_set_value(thc63->pdwn, 1);107108ret = regulator_disable(thc63->vcc);109if (ret)110dev_err(thc63->dev,111"Failed to disable regulator \"vcc\": %d\n", ret);112}113114static const struct drm_bridge_funcs thc63_bridge_func = {115.attach = thc63_attach,116.mode_valid = thc63_mode_valid,117.enable = thc63_enable,118.disable = thc63_disable,119};120121static int thc63_parse_dt(struct thc63_dev *thc63)122{123struct device_node *endpoint;124struct device_node *remote;125126remote = of_graph_get_remote_node(thc63->dev->of_node,127THC63_RGB_OUT0, -1);128if (!remote) {129dev_err(thc63->dev, "No remote endpoint for port@%u\n",130THC63_RGB_OUT0);131return -ENODEV;132}133134thc63->next = of_drm_find_bridge(remote);135of_node_put(remote);136if (!thc63->next)137return -EPROBE_DEFER;138139endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,140THC63_LVDS_IN1, -1);141if (endpoint) {142remote = of_graph_get_remote_port_parent(endpoint);143of_node_put(endpoint);144145if (remote) {146if (of_device_is_available(remote))147thc63->timings.dual_link = true;148of_node_put(remote);149}150}151152dev_dbg(thc63->dev, "operating in %s-link mode\n",153thc63->timings.dual_link ? "dual" : "single");154155return 0;156}157158static int thc63_gpio_init(struct thc63_dev *thc63)159{160thc63->oe = devm_gpiod_get_optional(thc63->dev, "oe", GPIOD_OUT_LOW);161if (IS_ERR(thc63->oe)) {162dev_err(thc63->dev, "Unable to get \"oe-gpios\": %ld\n",163PTR_ERR(thc63->oe));164return PTR_ERR(thc63->oe);165}166167thc63->pdwn = devm_gpiod_get_optional(thc63->dev, "powerdown",168GPIOD_OUT_HIGH);169if (IS_ERR(thc63->pdwn)) {170dev_err(thc63->dev, "Unable to get \"powerdown-gpios\": %ld\n",171PTR_ERR(thc63->pdwn));172return PTR_ERR(thc63->pdwn);173}174175return 0;176}177178static int thc63_probe(struct platform_device *pdev)179{180struct thc63_dev *thc63;181int ret;182183thc63 = devm_drm_bridge_alloc(&pdev->dev, struct thc63_dev, bridge,184&thc63_bridge_func);185if (IS_ERR(thc63))186return PTR_ERR(thc63);187188thc63->dev = &pdev->dev;189platform_set_drvdata(pdev, thc63);190191thc63->vcc = devm_regulator_get(thc63->dev, "vcc");192if (IS_ERR(thc63->vcc)) {193if (PTR_ERR(thc63->vcc) == -EPROBE_DEFER)194return -EPROBE_DEFER;195196dev_err(thc63->dev, "Unable to get \"vcc\" supply: %ld\n",197PTR_ERR(thc63->vcc));198return PTR_ERR(thc63->vcc);199}200201ret = thc63_gpio_init(thc63);202if (ret)203return ret;204205ret = thc63_parse_dt(thc63);206if (ret)207return ret;208209thc63->bridge.driver_private = thc63;210thc63->bridge.of_node = pdev->dev.of_node;211thc63->bridge.timings = &thc63->timings;212213drm_bridge_add(&thc63->bridge);214215return 0;216}217218static void thc63_remove(struct platform_device *pdev)219{220struct thc63_dev *thc63 = platform_get_drvdata(pdev);221222drm_bridge_remove(&thc63->bridge);223}224225static const struct of_device_id thc63_match[] = {226{ .compatible = "thine,thc63lvd1024", },227{ },228};229MODULE_DEVICE_TABLE(of, thc63_match);230231static struct platform_driver thc63_driver = {232.probe = thc63_probe,233.remove = thc63_remove,234.driver = {235.name = "thc63lvd1024",236.of_match_table = thc63_match,237},238};239module_platform_driver(thc63_driver);240241MODULE_AUTHOR("Jacopo Mondi <[email protected]>");242MODULE_DESCRIPTION("Thine THC63LVD1024 LVDS decoder DRM bridge driver");243MODULE_LICENSE("GPL v2");244245246