dm: video: Add support for the Parade PS8622/625 bridge

This chip provides an eDP to LVDS bridge which is useful for SoCs that don't
support LVDS displays (or it would waste scarce pins). The setup is included
in the device tree.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/doc/device-tree-bindings/video/bridge/ps8622.txt b/doc/device-tree-bindings/video/bridge/ps8622.txt
new file mode 100644
index 0000000..66d5d07
--- /dev/null
+++ b/doc/device-tree-bindings/video/bridge/ps8622.txt
@@ -0,0 +1,33 @@
+ps8622-bridge bindings
+
+Required properties:
+	- compatible: "parade,ps8622" or "parade,ps8625"
+	- reg: first i2c address of the bridge
+	- sleep-gpios: OF device-tree gpio specification for PD_ pin.
+	- reset-gpios: OF device-tree gpio specification for RST_ pin.
+	- parade,regs: List of 3-byte registers tuples to write:
+		<I2C chip address offset> <register> <value>
+
+Optional properties:
+	- lane-count: number of DP lanes to use
+	- use-external-pwm: backlight will be controlled by an external PWM
+	- video interfaces: Device node can contain video interface port
+			    nodes for panel according to [1].
+
+[1]: Documentation/devicetree/bindings/media/video-interfaces.txt
+
+Example:
+	lvds-bridge@48 {
+		compatible = "parade,ps8622";
+		reg = <0x48>;
+		sleep-gpios = <&gpc3 6 1 0 0>;
+		reset-gpios = <&gpc3 1 1 0 0>;
+		lane-count = <1>;
+		ports {
+			port@0 {
+				bridge_out: endpoint {
+					remote-endpoint = <&panel_in>;
+				};
+			};
+		};
+	};
diff --git a/drivers/video/bridge/Kconfig b/drivers/video/bridge/Kconfig
index 7ba6b17..589795d 100644
--- a/drivers/video/bridge/Kconfig
+++ b/drivers/video/bridge/Kconfig
@@ -6,3 +6,13 @@
 	  another. For example, where the SoC only supports eDP and the LCD
 	  requires LVDS, an eDP->LVDS bridge chip can be used to provide the
 	  necessary conversion. This option enables support for these devices.
+
+config VIDEO_BRIDGE_PARADE_PS862X
+	bool "Support Parade PS862X DP->LVDS bridge"
+	depends on VIDEO_BRIDGE
+	help
+	  The Parade PS8622 and PS8625 are DisplayPort-to-LVDS (Low voltage
+	  differential signalling) converters. They enable an LVDS LCD panel
+	  to be connected to an eDP output device such as an SoC that lacks
+	  LVDS capability, or where LVDS requires too many signals to route
+	  on the PCB. Setup parameters are provided in the device tree.
diff --git a/drivers/video/bridge/Makefile b/drivers/video/bridge/Makefile
index f4bf087..c7cc563 100644
--- a/drivers/video/bridge/Makefile
+++ b/drivers/video/bridge/Makefile
@@ -5,3 +5,4 @@
 #  SPDX-License-Identifier:	GPL-2.0+
 
 obj-$(CONFIG_VIDEO_BRIDGE) += video-bridge-uclass.o
+obj-$(CONFIG_VIDEO_BRIDGE_PARADE_PS862X) += ps862x.o
diff --git a/drivers/video/bridge/ps862x.c b/drivers/video/bridge/ps862x.c
new file mode 100644
index 0000000..80f63e3
--- /dev/null
+++ b/drivers/video/bridge/ps862x.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2015 Google, Inc
+ * Written by Simon Glass <sjg@chromium.org>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <i2c.h>
+#include <video_bridge.h>
+#include <power/regulator.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/*
+ * Initialisation of the chip is a process of writing certain values into
+ * certain registers over i2c bus. The chip in fact responds to a range of
+ * addresses on the i2c bus, so for each written value three parameters are
+ * required: i2c address, register address and the actual value.
+ *
+ * The base address is derived from the device tree, but oddly the chip
+ * responds on several addresses with different register sets for each.
+ */
+
+/**
+ * ps8622_write() Write a PS8622 eDP bridge i2c register
+ *
+ * @param dev		I2C device
+ * @param addr_off	offset from the i2c base address for ps8622
+ * @param reg_addr	register address to write
+ * @param value		value to be written
+ * @return 0 on success, non-0 on failure
+ */
+static int ps8622_write(struct udevice *dev, unsigned addr_off,
+			unsigned char reg_addr, unsigned char value)
+{
+	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
+	uint8_t buf[2];
+	struct i2c_msg msg;
+	int ret;
+
+	msg.addr = chip->chip_addr + addr_off;
+	msg.flags = 0;
+	buf[0] = reg_addr;
+	buf[1] = value;
+	msg.buf = buf;
+	msg.len = 2;
+	ret = dm_i2c_xfer(dev, &msg, 1);
+	if (ret) {
+		debug("%s: write failed, reg=%#x, value=%#x, ret=%d\n",
+		      __func__, reg_addr, value, ret);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int ps8622_set_backlight(struct udevice *dev, int percent)
+{
+	int level = percent * 255 / 100;
+
+	debug("%s: level=%d\n", __func__, level);
+	return ps8622_write(dev, 0x01, 0xa7, level);
+}
+
+static int ps8622_attach(struct udevice *dev)
+{
+	const uint8_t *params;
+	struct udevice *reg;
+	int ret, i, len;
+
+	debug("%s: %s\n", __func__, dev->name);
+	/* set the LDO providing the 1.2V rail to the Parade bridge */
+	ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
+					   "power-supply", &reg);
+	if (!ret) {
+		ret = regulator_autoset(reg);
+	} else if (ret != -ENOENT) {
+		debug("%s: Failed to enable power: ret=%d\n", __func__, ret);
+		return ret;
+	}
+
+	ret = video_bridge_set_active(dev, true);
+	if (ret)
+		return ret;
+
+	params = fdt_getprop(gd->fdt_blob, dev->of_offset, "parade,regs", &len);
+	if (!params || len % 3) {
+		debug("%s: missing/invalid params=%p, len=%x\n", __func__,
+		      params, len);
+		return -EINVAL;
+	}
+
+	/* need to wait 20ms after power on before doing I2C writes */
+	mdelay(20);
+	for (i = 0; i < len; i += 3) {
+		ret = ps8622_write(dev, params[i + 0], params[i + 1],
+				   params[i + 2]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int ps8622_probe(struct udevice *dev)
+{
+	debug("%s\n", __func__);
+	if (device_get_uclass_id(dev->parent) != UCLASS_I2C)
+		return -EPROTONOSUPPORT;
+
+	return 0;
+}
+
+struct video_bridge_ops ps8622_ops = {
+	.attach = ps8622_attach,
+	.set_backlight = ps8622_set_backlight,
+};
+
+static const struct udevice_id ps8622_ids[] = {
+	{ .compatible = "parade,ps8622", },
+	{ .compatible = "parade,ps8625", },
+	{ }
+};
+
+U_BOOT_DRIVER(parade_ps8622) = {
+	.name	= "parade_ps8622",
+	.id	= UCLASS_VIDEO_BRIDGE,
+	.of_match = ps8622_ids,
+	.probe	= ps8622_probe,
+	.ops	= &ps8622_ops,
+};