|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (C) 2023 Microchip Technology Inc. and its subsidiaries |
| 4 | + * |
| 5 | + * Author: Manikandan Muralidharan <manikandan.m@microchip.com> |
| 6 | + * Author: Dharma Balasubiramani <dharma.b@microchip.com> |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <linux/clk.h> |
| 11 | +#include <linux/component.h> |
| 12 | +#include <linux/delay.h> |
| 13 | +#include <linux/jiffies.h> |
| 14 | +#include <linux/mfd/syscon.h> |
| 15 | +#include <linux/of_graph.h> |
| 16 | +#include <linux/pinctrl/devinfo.h> |
| 17 | +#include <linux/phy/phy.h> |
| 18 | +#include <linux/platform_device.h> |
| 19 | +#include <linux/pm_runtime.h> |
| 20 | +#include <linux/regmap.h> |
| 21 | +#include <linux/reset.h> |
| 22 | + |
| 23 | +#include <drm/drm_atomic_helper.h> |
| 24 | +#include <drm/drm_bridge.h> |
| 25 | +#include <drm/drm_of.h> |
| 26 | +#include <drm/drm_panel.h> |
| 27 | +#include <drm/drm_print.h> |
| 28 | +#include <drm/drm_probe_helper.h> |
| 29 | +#include <drm/drm_simple_kms_helper.h> |
| 30 | + |
| 31 | +#define LVDS_POLL_TIMEOUT_MS 1000 |
| 32 | + |
| 33 | +/* LVDSC register offsets */ |
| 34 | +#define LVDSC_CR 0x00 |
| 35 | +#define LVDSC_CFGR 0x04 |
| 36 | +#define LVDSC_SR 0x0C |
| 37 | +#define LVDSC_WPMR 0xE4 |
| 38 | + |
| 39 | +/* Bitfields in LVDSC_CR (Control Register) */ |
| 40 | +#define LVDSC_CR_SER_EN BIT(0) |
| 41 | + |
| 42 | +/* Bitfields in LVDSC_CFGR (Configuration Register) */ |
| 43 | +#define LVDSC_CFGR_PIXSIZE_24BITS 0 |
| 44 | +#define LVDSC_CFGR_DEN_POL_HIGH 0 |
| 45 | +#define LVDSC_CFGR_DC_UNBALANCED 0 |
| 46 | +#define LVDSC_CFGR_MAPPING_JEIDA BIT(6) |
| 47 | + |
| 48 | +/*Bitfields in LVDSC_SR */ |
| 49 | +#define LVDSC_SR_CS BIT(0) |
| 50 | + |
| 51 | +/* Bitfields in LVDSC_WPMR (Write Protection Mode Register) */ |
| 52 | +#define LVDSC_WPMR_WPKEY_MASK GENMASK(31, 8) |
| 53 | +#define LVDSC_WPMR_WPKEY_PSSWD 0x4C5644 |
| 54 | + |
| 55 | +struct mchp_lvds { |
| 56 | + struct device *dev; |
| 57 | + void __iomem *regs; |
| 58 | + struct clk *pclk; |
| 59 | + int format; /* vesa or jeida format */ |
| 60 | + struct drm_panel *panel; |
| 61 | + struct drm_bridge bridge; |
| 62 | + struct drm_bridge *panel_bridge; |
| 63 | +}; |
| 64 | + |
| 65 | +static inline struct mchp_lvds *bridge_to_lvds(struct drm_bridge *bridge) |
| 66 | +{ |
| 67 | + return container_of(bridge, struct mchp_lvds, bridge); |
| 68 | +} |
| 69 | + |
| 70 | +static inline u32 lvds_readl(struct mchp_lvds *lvds, u32 offset) |
| 71 | +{ |
| 72 | + return readl_relaxed(lvds->regs + offset); |
| 73 | +} |
| 74 | + |
| 75 | +static inline void lvds_writel(struct mchp_lvds *lvds, u32 offset, u32 val) |
| 76 | +{ |
| 77 | + writel_relaxed(val, lvds->regs + offset); |
| 78 | +} |
| 79 | + |
| 80 | +static void lvds_serialiser_on(struct mchp_lvds *lvds) |
| 81 | +{ |
| 82 | + unsigned long timeout = jiffies + msecs_to_jiffies(LVDS_POLL_TIMEOUT_MS); |
| 83 | + |
| 84 | + /* The LVDSC registers can only be written if WPEN is cleared */ |
| 85 | + lvds_writel(lvds, LVDSC_WPMR, (LVDSC_WPMR_WPKEY_PSSWD & |
| 86 | + LVDSC_WPMR_WPKEY_MASK)); |
| 87 | + |
| 88 | + /* Wait for the status of configuration registers to be changed */ |
| 89 | + while (lvds_readl(lvds, LVDSC_SR) & LVDSC_SR_CS) { |
| 90 | + if (time_after(jiffies, timeout)) { |
| 91 | + dev_err(lvds->dev, "%s: timeout error\n", __func__); |
| 92 | + return; |
| 93 | + } |
| 94 | + usleep_range(1000, 2000); |
| 95 | + } |
| 96 | + |
| 97 | + /* Configure the LVDSC */ |
| 98 | + lvds_writel(lvds, LVDSC_CFGR, (LVDSC_CFGR_MAPPING_JEIDA | |
| 99 | + LVDSC_CFGR_DC_UNBALANCED | |
| 100 | + LVDSC_CFGR_DEN_POL_HIGH | |
| 101 | + LVDSC_CFGR_PIXSIZE_24BITS)); |
| 102 | + |
| 103 | + /* Enable the LVDS serializer */ |
| 104 | + lvds_writel(lvds, LVDSC_CR, LVDSC_CR_SER_EN); |
| 105 | +} |
| 106 | + |
| 107 | +static int mchp_lvds_attach(struct drm_bridge *bridge, |
| 108 | + enum drm_bridge_attach_flags flags) |
| 109 | +{ |
| 110 | + struct mchp_lvds *lvds = bridge_to_lvds(bridge); |
| 111 | + |
| 112 | + bridge->encoder->encoder_type = DRM_MODE_ENCODER_LVDS; |
| 113 | + |
| 114 | + return drm_bridge_attach(bridge->encoder, lvds->panel_bridge, |
| 115 | + bridge, flags); |
| 116 | +} |
| 117 | + |
| 118 | +static void mchp_lvds_enable(struct drm_bridge *bridge) |
| 119 | +{ |
| 120 | + struct mchp_lvds *lvds = bridge_to_lvds(bridge); |
| 121 | + int ret; |
| 122 | + |
| 123 | + ret = clk_enable(lvds->pclk); |
| 124 | + if (ret < 0) { |
| 125 | + DRM_DEV_ERROR(lvds->dev, "failed to enable lvds pclk %d\n", ret); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + ret = pm_runtime_get_sync(lvds->dev); |
| 130 | + if (ret < 0) { |
| 131 | + DRM_DEV_ERROR(lvds->dev, "failed to get pm runtime: %d\n", ret); |
| 132 | + clk_disable(lvds->pclk); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + lvds_serialiser_on(lvds); |
| 137 | +} |
| 138 | + |
| 139 | +static void mchp_lvds_disable(struct drm_bridge *bridge) |
| 140 | +{ |
| 141 | + struct mchp_lvds *lvds = bridge_to_lvds(bridge); |
| 142 | + |
| 143 | + pm_runtime_put(lvds->dev); |
| 144 | + clk_disable(lvds->pclk); |
| 145 | +} |
| 146 | + |
| 147 | +static const struct drm_bridge_funcs mchp_lvds_bridge_funcs = { |
| 148 | + .attach = mchp_lvds_attach, |
| 149 | + .enable = mchp_lvds_enable, |
| 150 | + .disable = mchp_lvds_disable, |
| 151 | +}; |
| 152 | + |
| 153 | +static int mchp_lvds_probe(struct platform_device *pdev) |
| 154 | +{ |
| 155 | + struct device *dev = &pdev->dev; |
| 156 | + struct mchp_lvds *lvds; |
| 157 | + struct resource *res; |
| 158 | + struct device_node *port; |
| 159 | + int ret; |
| 160 | + |
| 161 | + if (!dev->of_node) |
| 162 | + return -ENODEV; |
| 163 | + |
| 164 | + lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL); |
| 165 | + if (!lvds) |
| 166 | + return -ENOMEM; |
| 167 | + |
| 168 | + lvds->dev = dev; |
| 169 | + |
| 170 | + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 171 | + lvds->regs = devm_ioremap_resource(lvds->dev, res); |
| 172 | + if (IS_ERR(lvds->regs)) |
| 173 | + return PTR_ERR(lvds->regs); |
| 174 | + |
| 175 | + lvds->pclk = devm_clk_get(lvds->dev, "pclk"); |
| 176 | + if (IS_ERR(lvds->pclk)) { |
| 177 | + DRM_DEV_ERROR(lvds->dev, "could not get pclk_lvds\n"); |
| 178 | + return PTR_ERR(lvds->pclk); |
| 179 | + } |
| 180 | + |
| 181 | + ret = clk_prepare(lvds->pclk); |
| 182 | + if (ret < 0) { |
| 183 | + DRM_DEV_ERROR(lvds->dev, "failed to prepare pclk_lvds\n"); |
| 184 | + return ret; |
| 185 | + } |
| 186 | + |
| 187 | + port = of_graph_get_remote_node(dev->of_node, 1, 0); |
| 188 | + if (!port) { |
| 189 | + DRM_DEV_ERROR(dev, |
| 190 | + "can't find port point, please init lvds panel port!\n"); |
| 191 | + return -EINVAL; |
| 192 | + } |
| 193 | + |
| 194 | + lvds->panel = of_drm_find_panel(port); |
| 195 | + of_node_put(port); |
| 196 | + |
| 197 | + if (IS_ERR(lvds->panel)) { |
| 198 | + DRM_DEV_ERROR(dev, "failed to find panel node\n"); |
| 199 | + return -EPROBE_DEFER; |
| 200 | + } |
| 201 | + |
| 202 | + lvds->panel_bridge = devm_drm_panel_bridge_add(dev, lvds->panel); |
| 203 | + |
| 204 | + if (IS_ERR(lvds->panel_bridge)) |
| 205 | + return PTR_ERR(lvds->panel_bridge); |
| 206 | + |
| 207 | + lvds->bridge.of_node = dev->of_node; |
| 208 | + lvds->bridge.type = DRM_MODE_CONNECTOR_LVDS; |
| 209 | + lvds->bridge.funcs = &mchp_lvds_bridge_funcs; |
| 210 | + |
| 211 | + dev_set_drvdata(dev, lvds); |
| 212 | + pm_runtime_enable(dev); |
| 213 | + |
| 214 | + drm_bridge_add(&lvds->bridge); |
| 215 | + |
| 216 | + return 0; |
| 217 | +} |
| 218 | + |
| 219 | +static int mchp_lvds_remove(struct platform_device *pdev) |
| 220 | +{ |
| 221 | + struct mchp_lvds *lvds = platform_get_drvdata(pdev); |
| 222 | + |
| 223 | + pm_runtime_disable(&pdev->dev); |
| 224 | + clk_unprepare(lvds->pclk); |
| 225 | + |
| 226 | + return 0; |
| 227 | +} |
| 228 | + |
| 229 | +static const struct of_device_id mchp_lvds_dt_ids[] = { |
| 230 | + { |
| 231 | + .compatible = "microchip,sam9x7-lvds", |
| 232 | + }, |
| 233 | + {}, |
| 234 | +}; |
| 235 | + |
| 236 | +struct platform_driver mchp_lvds_driver = { |
| 237 | + .probe = mchp_lvds_probe, |
| 238 | + .remove = mchp_lvds_remove, |
| 239 | + .driver = { |
| 240 | + .name = "microchip-lvds", |
| 241 | + .of_match_table = mchp_lvds_dt_ids, |
| 242 | + }, |
| 243 | +}; |
| 244 | +module_platform_driver(mchp_lvds_driver); |
| 245 | + |
| 246 | +MODULE_AUTHOR("Manikandan Muralidharan <manikandan.m@microchip.com>"); |
| 247 | +MODULE_AUTHOR("Dharma Balasubiramani <dharma.b@microchip.com>"); |
| 248 | +MODULE_DESCRIPTION("Low Voltage Differential Signaling Controller Driver"); |
| 249 | +MODULE_LICENSE("GPL"); |
| 250 | +MODULE_ALIAS("platform:microchip-lvds"); |
0 commit comments