0001
0002
0003
0004
0005
0006
0007 #include <linux/media-bus-format.h>
0008 #include <linux/mfd/syscon.h>
0009 #include <linux/of.h>
0010 #include <linux/regmap.h>
0011
0012 #include <drm/drm_bridge.h>
0013 #include <drm/drm_of.h>
0014 #include <drm/drm_print.h>
0015
0016 #include "imx-ldb-helper.h"
0017
0018 bool ldb_channel_is_single_link(struct ldb_channel *ldb_ch)
0019 {
0020 return ldb_ch->link_type == LDB_CH_SINGLE_LINK;
0021 }
0022
0023 bool ldb_channel_is_split_link(struct ldb_channel *ldb_ch)
0024 {
0025 return ldb_ch->link_type == LDB_CH_DUAL_LINK_EVEN_ODD_PIXELS ||
0026 ldb_ch->link_type == LDB_CH_DUAL_LINK_ODD_EVEN_PIXELS;
0027 }
0028
0029 int ldb_bridge_atomic_check_helper(struct drm_bridge *bridge,
0030 struct drm_bridge_state *bridge_state,
0031 struct drm_crtc_state *crtc_state,
0032 struct drm_connector_state *conn_state)
0033 {
0034 struct ldb_channel *ldb_ch = bridge->driver_private;
0035
0036 ldb_ch->in_bus_format = bridge_state->input_bus_cfg.format;
0037 ldb_ch->out_bus_format = bridge_state->output_bus_cfg.format;
0038
0039 return 0;
0040 }
0041
0042 void ldb_bridge_mode_set_helper(struct drm_bridge *bridge,
0043 const struct drm_display_mode *mode,
0044 const struct drm_display_mode *adjusted_mode)
0045 {
0046 struct ldb_channel *ldb_ch = bridge->driver_private;
0047 struct ldb *ldb = ldb_ch->ldb;
0048 bool is_split = ldb_channel_is_split_link(ldb_ch);
0049
0050 if (is_split)
0051 ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
0052
0053 switch (ldb_ch->out_bus_format) {
0054 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
0055 break;
0056 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
0057 if (ldb_ch->chno == 0 || is_split)
0058 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
0059 if (ldb_ch->chno == 1 || is_split)
0060 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
0061 break;
0062 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
0063 if (ldb_ch->chno == 0 || is_split)
0064 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
0065 LDB_BIT_MAP_CH0_JEIDA;
0066 if (ldb_ch->chno == 1 || is_split)
0067 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
0068 LDB_BIT_MAP_CH1_JEIDA;
0069 break;
0070 }
0071 }
0072
0073 void ldb_bridge_enable_helper(struct drm_bridge *bridge)
0074 {
0075 struct ldb_channel *ldb_ch = bridge->driver_private;
0076 struct ldb *ldb = ldb_ch->ldb;
0077
0078
0079
0080
0081
0082 regmap_write(ldb->regmap, ldb->ctrl_reg, ldb->ldb_ctrl);
0083 }
0084
0085 void ldb_bridge_disable_helper(struct drm_bridge *bridge)
0086 {
0087 struct ldb_channel *ldb_ch = bridge->driver_private;
0088 struct ldb *ldb = ldb_ch->ldb;
0089 bool is_split = ldb_channel_is_split_link(ldb_ch);
0090
0091 if (ldb_ch->chno == 0 || is_split)
0092 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
0093 if (ldb_ch->chno == 1 || is_split)
0094 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
0095
0096 regmap_write(ldb->regmap, ldb->ctrl_reg, ldb->ldb_ctrl);
0097 }
0098
0099 int ldb_bridge_attach_helper(struct drm_bridge *bridge,
0100 enum drm_bridge_attach_flags flags)
0101 {
0102 struct ldb_channel *ldb_ch = bridge->driver_private;
0103 struct ldb *ldb = ldb_ch->ldb;
0104
0105 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
0106 DRM_DEV_ERROR(ldb->dev,
0107 "do not support creating a drm_connector\n");
0108 return -EINVAL;
0109 }
0110
0111 if (!bridge->encoder) {
0112 DRM_DEV_ERROR(ldb->dev, "missing encoder\n");
0113 return -ENODEV;
0114 }
0115
0116 return drm_bridge_attach(bridge->encoder,
0117 ldb_ch->next_bridge, bridge,
0118 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
0119 }
0120
0121 int ldb_init_helper(struct ldb *ldb)
0122 {
0123 struct device *dev = ldb->dev;
0124 struct device_node *np = dev->of_node;
0125 struct device_node *child;
0126 int ret;
0127 u32 i;
0128
0129 ldb->regmap = syscon_node_to_regmap(np->parent);
0130 if (IS_ERR(ldb->regmap)) {
0131 ret = PTR_ERR(ldb->regmap);
0132 if (ret != -EPROBE_DEFER)
0133 DRM_DEV_ERROR(dev, "failed to get regmap: %d\n", ret);
0134 return ret;
0135 }
0136
0137 for_each_available_child_of_node(np, child) {
0138 struct ldb_channel *ldb_ch;
0139
0140 ret = of_property_read_u32(child, "reg", &i);
0141 if (ret || i > MAX_LDB_CHAN_NUM - 1) {
0142 ret = -EINVAL;
0143 DRM_DEV_ERROR(dev,
0144 "invalid channel node address: %u\n", i);
0145 of_node_put(child);
0146 return ret;
0147 }
0148
0149 ldb_ch = ldb->channel[i];
0150 ldb_ch->ldb = ldb;
0151 ldb_ch->chno = i;
0152 ldb_ch->is_available = true;
0153 ldb_ch->np = child;
0154
0155 ldb->available_ch_cnt++;
0156 }
0157
0158 return 0;
0159 }
0160
0161 int ldb_find_next_bridge_helper(struct ldb *ldb)
0162 {
0163 struct device *dev = ldb->dev;
0164 struct ldb_channel *ldb_ch;
0165 int ret, i;
0166
0167 for (i = 0; i < MAX_LDB_CHAN_NUM; i++) {
0168 ldb_ch = ldb->channel[i];
0169
0170 if (!ldb_ch->is_available)
0171 continue;
0172
0173 ldb_ch->next_bridge = devm_drm_of_get_bridge(dev, ldb_ch->np,
0174 1, 0);
0175 if (IS_ERR(ldb_ch->next_bridge)) {
0176 ret = PTR_ERR(ldb_ch->next_bridge);
0177 if (ret != -EPROBE_DEFER)
0178 DRM_DEV_ERROR(dev,
0179 "failed to get next bridge: %d\n",
0180 ret);
0181 return ret;
0182 }
0183 }
0184
0185 return 0;
0186 }
0187
0188 void ldb_add_bridge_helper(struct ldb *ldb,
0189 const struct drm_bridge_funcs *bridge_funcs)
0190 {
0191 struct ldb_channel *ldb_ch;
0192 int i;
0193
0194 for (i = 0; i < MAX_LDB_CHAN_NUM; i++) {
0195 ldb_ch = ldb->channel[i];
0196
0197 if (!ldb_ch->is_available)
0198 continue;
0199
0200 ldb_ch->bridge.driver_private = ldb_ch;
0201 ldb_ch->bridge.funcs = bridge_funcs;
0202 ldb_ch->bridge.of_node = ldb_ch->np;
0203
0204 drm_bridge_add(&ldb_ch->bridge);
0205 }
0206 }
0207
0208 void ldb_remove_bridge_helper(struct ldb *ldb)
0209 {
0210 struct ldb_channel *ldb_ch;
0211 int i;
0212
0213 for (i = 0; i < MAX_LDB_CHAN_NUM; i++) {
0214 ldb_ch = ldb->channel[i];
0215
0216 if (!ldb_ch->is_available)
0217 continue;
0218
0219 drm_bridge_remove(&ldb_ch->bridge);
0220 }
0221 }