1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Sascha Hauer, Pengutronix
4 * Copyright 2019,2020,2022 NXP
7 #include <linux/export.h>
8 #include <linux/media-bus-format.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/module.h>
12 #include <linux/regmap.h>
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_print.h>
18 #include "imx-ldb-helper.h"
20 bool ldb_channel_is_single_link(struct ldb_channel *ldb_ch)
22 return ldb_ch->link_type == LDB_CH_SINGLE_LINK;
24 EXPORT_SYMBOL_GPL(ldb_channel_is_single_link);
26 bool ldb_channel_is_split_link(struct ldb_channel *ldb_ch)
28 return ldb_ch->link_type == LDB_CH_DUAL_LINK_EVEN_ODD_PIXELS ||
29 ldb_ch->link_type == LDB_CH_DUAL_LINK_ODD_EVEN_PIXELS;
31 EXPORT_SYMBOL_GPL(ldb_channel_is_split_link);
33 int ldb_bridge_atomic_check_helper(struct drm_bridge *bridge,
34 struct drm_bridge_state *bridge_state,
35 struct drm_crtc_state *crtc_state,
36 struct drm_connector_state *conn_state)
38 struct ldb_channel *ldb_ch = bridge->driver_private;
40 ldb_ch->in_bus_format = bridge_state->input_bus_cfg.format;
41 ldb_ch->out_bus_format = bridge_state->output_bus_cfg.format;
45 EXPORT_SYMBOL_GPL(ldb_bridge_atomic_check_helper);
47 void ldb_bridge_mode_set_helper(struct drm_bridge *bridge,
48 const struct drm_display_mode *mode,
49 const struct drm_display_mode *adjusted_mode)
51 struct ldb_channel *ldb_ch = bridge->driver_private;
52 struct ldb *ldb = ldb_ch->ldb;
53 bool is_split = ldb_channel_is_split_link(ldb_ch);
56 ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
58 switch (ldb_ch->out_bus_format) {
59 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
61 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
62 if (ldb_ch->chno == 0 || is_split)
63 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
64 if (ldb_ch->chno == 1 || is_split)
65 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
67 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
68 if (ldb_ch->chno == 0 || is_split)
69 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
70 LDB_BIT_MAP_CH0_JEIDA;
71 if (ldb_ch->chno == 1 || is_split)
72 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
73 LDB_BIT_MAP_CH1_JEIDA;
77 EXPORT_SYMBOL_GPL(ldb_bridge_mode_set_helper);
79 void ldb_bridge_enable_helper(struct drm_bridge *bridge)
81 struct ldb_channel *ldb_ch = bridge->driver_private;
82 struct ldb *ldb = ldb_ch->ldb;
85 * Platform specific bridge drivers should set ldb_ctrl properly
86 * for the enablement, so just write the ctrl_reg here.
88 regmap_write(ldb->regmap, ldb->ctrl_reg, ldb->ldb_ctrl);
90 EXPORT_SYMBOL_GPL(ldb_bridge_enable_helper);
92 void ldb_bridge_disable_helper(struct drm_bridge *bridge)
94 struct ldb_channel *ldb_ch = bridge->driver_private;
95 struct ldb *ldb = ldb_ch->ldb;
96 bool is_split = ldb_channel_is_split_link(ldb_ch);
98 if (ldb_ch->chno == 0 || is_split)
99 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
100 if (ldb_ch->chno == 1 || is_split)
101 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
103 regmap_write(ldb->regmap, ldb->ctrl_reg, ldb->ldb_ctrl);
105 EXPORT_SYMBOL_GPL(ldb_bridge_disable_helper);
107 int ldb_bridge_attach_helper(struct drm_bridge *bridge,
108 enum drm_bridge_attach_flags flags)
110 struct ldb_channel *ldb_ch = bridge->driver_private;
111 struct ldb *ldb = ldb_ch->ldb;
113 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
114 DRM_DEV_ERROR(ldb->dev,
115 "do not support creating a drm_connector\n");
119 if (!bridge->encoder) {
120 DRM_DEV_ERROR(ldb->dev, "missing encoder\n");
124 return drm_bridge_attach(bridge->encoder,
125 ldb_ch->next_bridge, bridge,
126 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
128 EXPORT_SYMBOL_GPL(ldb_bridge_attach_helper);
130 int ldb_init_helper(struct ldb *ldb)
132 struct device *dev = ldb->dev;
133 struct device_node *np = dev->of_node;
134 struct device_node *child;
138 ldb->regmap = syscon_node_to_regmap(np->parent);
139 if (IS_ERR(ldb->regmap)) {
140 ret = PTR_ERR(ldb->regmap);
141 if (ret != -EPROBE_DEFER)
142 DRM_DEV_ERROR(dev, "failed to get regmap: %d\n", ret);
146 for_each_available_child_of_node(np, child) {
147 struct ldb_channel *ldb_ch;
149 ret = of_property_read_u32(child, "reg", &i);
150 if (ret || i > MAX_LDB_CHAN_NUM - 1) {
153 "invalid channel node address: %u\n", i);
158 ldb_ch = ldb->channel[i];
161 ldb_ch->is_available = true;
164 ldb->available_ch_cnt++;
169 EXPORT_SYMBOL_GPL(ldb_init_helper);
171 int ldb_find_next_bridge_helper(struct ldb *ldb)
173 struct device *dev = ldb->dev;
174 struct ldb_channel *ldb_ch;
177 for (i = 0; i < MAX_LDB_CHAN_NUM; i++) {
178 ldb_ch = ldb->channel[i];
180 if (!ldb_ch->is_available)
183 ldb_ch->next_bridge = devm_drm_of_get_bridge(dev, ldb_ch->np,
185 if (IS_ERR(ldb_ch->next_bridge)) {
186 ret = PTR_ERR(ldb_ch->next_bridge);
187 if (ret != -EPROBE_DEFER)
189 "failed to get next bridge: %d\n",
197 EXPORT_SYMBOL_GPL(ldb_find_next_bridge_helper);
199 void ldb_add_bridge_helper(struct ldb *ldb,
200 const struct drm_bridge_funcs *bridge_funcs)
202 struct ldb_channel *ldb_ch;
205 for (i = 0; i < MAX_LDB_CHAN_NUM; i++) {
206 ldb_ch = ldb->channel[i];
208 if (!ldb_ch->is_available)
211 ldb_ch->bridge.driver_private = ldb_ch;
212 ldb_ch->bridge.funcs = bridge_funcs;
213 ldb_ch->bridge.of_node = ldb_ch->np;
215 drm_bridge_add(&ldb_ch->bridge);
218 EXPORT_SYMBOL_GPL(ldb_add_bridge_helper);
220 void ldb_remove_bridge_helper(struct ldb *ldb)
222 struct ldb_channel *ldb_ch;
225 for (i = 0; i < MAX_LDB_CHAN_NUM; i++) {
226 ldb_ch = ldb->channel[i];
228 if (!ldb_ch->is_available)
231 drm_bridge_remove(&ldb_ch->bridge);
234 EXPORT_SYMBOL_GPL(ldb_remove_bridge_helper);
236 MODULE_DESCRIPTION("i.MX8 LVDS Display Bridge(LDB)/Pixel Mapper bridge helper");
238 MODULE_LICENSE("GPL");