19fb0493fSWyon Bi // SPDX-License-Identifier: GPL-2.0+
29fb0493fSWyon Bi /*
39fb0493fSWyon Bi * (C) Copyright 2008-2018 Fuzhou Rockchip Electronics Co., Ltd
49fb0493fSWyon Bi *
59fb0493fSWyon Bi * Author: Wyon Bi <bivvy.bi@rock-chips.com>
69fb0493fSWyon Bi */
79fb0493fSWyon Bi
89fb0493fSWyon Bi #include <config.h>
99fb0493fSWyon Bi #include <common.h>
109fb0493fSWyon Bi #include <errno.h>
119fb0493fSWyon Bi #include <dm.h>
129fb0493fSWyon Bi #include <asm/io.h>
139fb0493fSWyon Bi #include <linux/iopoll.h>
149fb0493fSWyon Bi
159fb0493fSWyon Bi #include "rockchip_phy.h"
169fb0493fSWyon Bi
179fb0493fSWyon Bi /* Register: 0x0030 */
189fb0493fSWyon Bi #define DISABLE_PLL BIT(3)
199fb0493fSWyon Bi /* Register: 0x003c */
209fb0493fSWyon Bi #define PLL_LOCK BIT(1)
219fb0493fSWyon Bi /* Register: 0x0084 */
229fb0493fSWyon Bi #define ENABLE_TX BIT(7)
239fb0493fSWyon Bi
249fb0493fSWyon Bi struct inno_video_phy {
259fb0493fSWyon Bi void __iomem *base;
269fb0493fSWyon Bi enum phy_mode mode;
279fb0493fSWyon Bi bool dual_channel;
289fb0493fSWyon Bi };
299fb0493fSWyon Bi
309fb0493fSWyon Bi struct reg_sequence {
319fb0493fSWyon Bi unsigned int reg;
329fb0493fSWyon Bi unsigned int def;
339fb0493fSWyon Bi unsigned int delay_us;
349fb0493fSWyon Bi };
359fb0493fSWyon Bi
369fb0493fSWyon Bi static const struct reg_sequence ttl_mode[] = {
379fb0493fSWyon Bi { 0x0000, 0x7f },
389fb0493fSWyon Bi { 0x0004, 0x3f },
399fb0493fSWyon Bi { 0x0008, 0x80 },
409fb0493fSWyon Bi { 0x0010, 0x3f },
419fb0493fSWyon Bi { 0x0014, 0x3f },
429fb0493fSWyon Bi { 0x0080, 0x44 },
439fb0493fSWyon Bi
449fb0493fSWyon Bi { 0x0100, 0x7f },
459fb0493fSWyon Bi { 0x0104, 0x3f },
469fb0493fSWyon Bi { 0x0108, 0x80 },
479fb0493fSWyon Bi { 0x0110, 0x3f },
489fb0493fSWyon Bi { 0x0114, 0x3f },
499fb0493fSWyon Bi { 0x0180, 0x44 },
509fb0493fSWyon Bi };
519fb0493fSWyon Bi
529fb0493fSWyon Bi static const struct reg_sequence lvds_mode_single_channel[] = {
539fb0493fSWyon Bi { 0x0000, 0xbf },
549fb0493fSWyon Bi { 0x0004, 0x3f },
559fb0493fSWyon Bi { 0x0008, 0xfe },
569fb0493fSWyon Bi { 0x0010, 0x00 },
579fb0493fSWyon Bi { 0x0014, 0x00 },
589fb0493fSWyon Bi { 0x0080, 0x44 },
599fb0493fSWyon Bi
609fb0493fSWyon Bi { 0x0100, 0x00 },
619fb0493fSWyon Bi { 0x0104, 0x00 },
629fb0493fSWyon Bi { 0x0108, 0x00 },
639fb0493fSWyon Bi { 0x0110, 0x00 },
649fb0493fSWyon Bi { 0x0114, 0x00 },
659fb0493fSWyon Bi { 0x0180, 0x44 },
669fb0493fSWyon Bi };
679fb0493fSWyon Bi
689fb0493fSWyon Bi static const struct reg_sequence lvds_mode_dual_channel[] = {
699fb0493fSWyon Bi { 0x0000, 0xbf },
709fb0493fSWyon Bi { 0x0004, 0x3f },
719fb0493fSWyon Bi { 0x0008, 0xfe },
729fb0493fSWyon Bi { 0x0010, 0x00 },
739fb0493fSWyon Bi { 0x0014, 0x00 },
749fb0493fSWyon Bi { 0x0080, 0x44 },
759fb0493fSWyon Bi
769fb0493fSWyon Bi { 0x0100, 0xbf },
779fb0493fSWyon Bi { 0x0104, 0x3f },
789fb0493fSWyon Bi { 0x0108, 0xfe },
799fb0493fSWyon Bi { 0x0110, 0x00 },
809fb0493fSWyon Bi { 0x0114, 0x00 },
819fb0493fSWyon Bi { 0x0180, 0x44 },
829fb0493fSWyon Bi };
839fb0493fSWyon Bi
phy_write(struct inno_video_phy * inno,u32 reg,u32 val)849fb0493fSWyon Bi static inline void phy_write(struct inno_video_phy *inno, u32 reg, u32 val)
859fb0493fSWyon Bi {
869fb0493fSWyon Bi writel(val, inno->base + reg);
879fb0493fSWyon Bi }
889fb0493fSWyon Bi
phy_read(struct inno_video_phy * inno,u32 reg)899fb0493fSWyon Bi static inline u32 phy_read(struct inno_video_phy *inno, u32 reg)
909fb0493fSWyon Bi {
919fb0493fSWyon Bi return readl(inno->base + reg);
929fb0493fSWyon Bi }
939fb0493fSWyon Bi
phy_update_bits(struct inno_video_phy * inno,u32 reg,u32 mask,u32 val)949fb0493fSWyon Bi static inline void phy_update_bits(struct inno_video_phy *inno,
959fb0493fSWyon Bi u32 reg, u32 mask, u32 val)
969fb0493fSWyon Bi {
979fb0493fSWyon Bi u32 tmp, orig;
989fb0493fSWyon Bi
999fb0493fSWyon Bi orig = phy_read(inno, reg);
1009fb0493fSWyon Bi tmp = orig & ~mask;
1019fb0493fSWyon Bi tmp |= val & mask;
1029fb0493fSWyon Bi phy_write(inno, reg, tmp);
1039fb0493fSWyon Bi }
1049fb0493fSWyon Bi
phy_multi_write(struct inno_video_phy * inno,const struct reg_sequence * regs,int num_regs)1059fb0493fSWyon Bi static void phy_multi_write(struct inno_video_phy *inno,
1069fb0493fSWyon Bi const struct reg_sequence *regs, int num_regs)
1079fb0493fSWyon Bi {
1089fb0493fSWyon Bi int i;
1099fb0493fSWyon Bi
1109fb0493fSWyon Bi for (i = 0; i < num_regs; i++) {
1119fb0493fSWyon Bi phy_write(inno, regs[i].reg, regs[i].def);
1129fb0493fSWyon Bi
1139fb0493fSWyon Bi if (regs[i].delay_us)
1149fb0493fSWyon Bi udelay(regs[i].delay_us);
1159fb0493fSWyon Bi }
1169fb0493fSWyon Bi }
1179fb0493fSWyon Bi
inno_video_phy_power_on(struct rockchip_phy * phy)1189fb0493fSWyon Bi static int inno_video_phy_power_on(struct rockchip_phy *phy)
1199fb0493fSWyon Bi {
1209fb0493fSWyon Bi struct inno_video_phy *inno = dev_get_priv(phy->dev);
1219fb0493fSWyon Bi const struct reg_sequence *wseq;
1229fb0493fSWyon Bi int nregs;
1239fb0493fSWyon Bi u32 status;
1249fb0493fSWyon Bi int ret;
1259fb0493fSWyon Bi
1269fb0493fSWyon Bi switch (inno->mode) {
127*9e3ffb10SGuochun Huang case PHY_MODE_VIDEO_LVDS:
1289fb0493fSWyon Bi if (inno->dual_channel) {
1299fb0493fSWyon Bi wseq = lvds_mode_dual_channel;
1309fb0493fSWyon Bi nregs = ARRAY_SIZE(lvds_mode_dual_channel);
1319fb0493fSWyon Bi } else {
1329fb0493fSWyon Bi wseq = lvds_mode_single_channel;
1339fb0493fSWyon Bi nregs = ARRAY_SIZE(lvds_mode_single_channel);
1349fb0493fSWyon Bi }
1359fb0493fSWyon Bi break;
136*9e3ffb10SGuochun Huang case PHY_MODE_VIDEO_TTL:
1379fb0493fSWyon Bi wseq = ttl_mode;
1389fb0493fSWyon Bi nregs = ARRAY_SIZE(ttl_mode);
139*9e3ffb10SGuochun Huang break;
140*9e3ffb10SGuochun Huang default:
141*9e3ffb10SGuochun Huang return -EINVAL;
1429fb0493fSWyon Bi }
1439fb0493fSWyon Bi
1449fb0493fSWyon Bi phy_multi_write(inno, wseq, nregs);
1459fb0493fSWyon Bi
1469fb0493fSWyon Bi phy_update_bits(inno, 0x0030, DISABLE_PLL, 0);
1479fb0493fSWyon Bi ret = readl_poll_timeout(inno->base + 0x003c, status,
148f0f5bdc2SWyon Bi status & PLL_LOCK, 100000);
1499fb0493fSWyon Bi if (ret) {
1509fb0493fSWyon Bi dev_err(phy->dev, "PLL is not lock\n");
1519fb0493fSWyon Bi return ret;
1529fb0493fSWyon Bi }
1539fb0493fSWyon Bi
1549fb0493fSWyon Bi phy_update_bits(inno, 0x0084, ENABLE_TX, ENABLE_TX);
1559fb0493fSWyon Bi
1569fb0493fSWyon Bi return 0;
1579fb0493fSWyon Bi }
1589fb0493fSWyon Bi
inno_video_phy_power_off(struct rockchip_phy * phy)1599fb0493fSWyon Bi static int inno_video_phy_power_off(struct rockchip_phy *phy)
1609fb0493fSWyon Bi {
1619fb0493fSWyon Bi struct inno_video_phy *inno = dev_get_priv(phy->dev);
1629fb0493fSWyon Bi
1639fb0493fSWyon Bi phy_update_bits(inno, 0x0084, ENABLE_TX, 0);
1649fb0493fSWyon Bi phy_update_bits(inno, 0x0030, DISABLE_PLL, DISABLE_PLL);
1659fb0493fSWyon Bi
1669fb0493fSWyon Bi return 0;
1679fb0493fSWyon Bi }
1689fb0493fSWyon Bi
inno_video_phy_set_mode(struct rockchip_phy * phy,enum phy_mode mode)1699fb0493fSWyon Bi static int inno_video_phy_set_mode(struct rockchip_phy *phy,
1709fb0493fSWyon Bi enum phy_mode mode)
1719fb0493fSWyon Bi {
1729fb0493fSWyon Bi struct inno_video_phy *inno = dev_get_priv(phy->dev);
1739fb0493fSWyon Bi
174*9e3ffb10SGuochun Huang switch (mode) {
175*9e3ffb10SGuochun Huang case PHY_MODE_VIDEO_LVDS:
176*9e3ffb10SGuochun Huang case PHY_MODE_VIDEO_TTL:
1779fb0493fSWyon Bi inno->mode = mode;
178*9e3ffb10SGuochun Huang break;
179*9e3ffb10SGuochun Huang default:
180*9e3ffb10SGuochun Huang return -EINVAL;
181*9e3ffb10SGuochun Huang }
1829fb0493fSWyon Bi
1839fb0493fSWyon Bi return 0;
1849fb0493fSWyon Bi }
1859fb0493fSWyon Bi
1869fb0493fSWyon Bi static int
inno_video_phy_set_bus_width(struct rockchip_phy * phy,u32 bus_width)1879fb0493fSWyon Bi inno_video_phy_set_bus_width(struct rockchip_phy *phy, u32 bus_width)
1889fb0493fSWyon Bi {
1899fb0493fSWyon Bi struct inno_video_phy *inno = dev_get_priv(phy->dev);
1909fb0493fSWyon Bi
1919fb0493fSWyon Bi inno->dual_channel = (bus_width == 2) ? true : false;
1929fb0493fSWyon Bi
1939fb0493fSWyon Bi return 0;
1949fb0493fSWyon Bi }
1959fb0493fSWyon Bi
1969fb0493fSWyon Bi static const struct rockchip_phy_funcs inno_video_phy_funcs = {
1979fb0493fSWyon Bi .power_on = inno_video_phy_power_on,
1989fb0493fSWyon Bi .power_off = inno_video_phy_power_off,
1999fb0493fSWyon Bi .set_mode = inno_video_phy_set_mode,
2009fb0493fSWyon Bi .set_bus_width = inno_video_phy_set_bus_width,
2019fb0493fSWyon Bi };
2029fb0493fSWyon Bi
inno_video_phy_probe(struct udevice * dev)2039fb0493fSWyon Bi static int inno_video_phy_probe(struct udevice *dev)
2049fb0493fSWyon Bi {
2059fb0493fSWyon Bi struct inno_video_phy *inno = dev_get_priv(dev);
2069fb0493fSWyon Bi struct rockchip_phy *phy =
2079fb0493fSWyon Bi (struct rockchip_phy *)dev_get_driver_data(dev);
2089fb0493fSWyon Bi
2099fb0493fSWyon Bi inno->base = dev_read_addr_ptr(dev);
2109fb0493fSWyon Bi phy->dev = dev;
2119fb0493fSWyon Bi
2129fb0493fSWyon Bi return 0;
2139fb0493fSWyon Bi }
2149fb0493fSWyon Bi
2159fb0493fSWyon Bi static struct rockchip_phy inno_video_phy_driver_data = {
2169fb0493fSWyon Bi .funcs = &inno_video_phy_funcs,
2179fb0493fSWyon Bi };
2189fb0493fSWyon Bi
2199fb0493fSWyon Bi static const struct udevice_id inno_video_phy_ids[] = {
2209fb0493fSWyon Bi {
2219fb0493fSWyon Bi .compatible = "rockchip,rk3288-video-phy",
2229fb0493fSWyon Bi .data = (ulong)&inno_video_phy_driver_data,
2239fb0493fSWyon Bi },
2249fb0493fSWyon Bi {}
2259fb0493fSWyon Bi };
2269fb0493fSWyon Bi
2279fb0493fSWyon Bi U_BOOT_DRIVER(inno_video_phy) = {
2289fb0493fSWyon Bi .name = "inno_video_phy",
2299fb0493fSWyon Bi .id = UCLASS_PHY,
2309fb0493fSWyon Bi .of_match = inno_video_phy_ids,
2319fb0493fSWyon Bi .probe = inno_video_phy_probe,
2329fb0493fSWyon Bi .priv_auto_alloc_size = sizeof(struct inno_video_phy),
2339fb0493fSWyon Bi };
234