hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) Rockchip Electronics Co.Ltd
 * Author:
 *      Guochun Huang <hero.huang@rock-chips.com>
 */
 
#include <asm/unaligned.h>
#include <drm/drm_bridge.h>
#include <drm/drm_atomic_state_helper.h>
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_modeset_helper_vtables.h>
#include <drm/drm_of.h>
#include <drm/drm_print.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_panel.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/gpio/consumer.h>
#include <linux/regulator/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/regmap.h>
 
struct serdes_init_seq {
   struct reg_sequence *reg_sequence;
   unsigned int reg_seq_cnt;
};
 
struct bu18tl82 {
   struct drm_bridge base;
   struct drm_bridge *bridge;
   struct device *dev;
   struct regmap *regmap;
   struct mipi_dsi_device *dsi;
   struct device_node *dsi_node;
   struct serdes_init_seq *serdes_init_seq;
   bool sel_mipi;
   struct gpio_desc *reset_gpio;
   struct gpio_desc *enable_gpio;
};
 
static const struct regmap_config bu18tl82_regmap_config = {
   .name = "bu18tl82",
   .reg_bits = 16,
   .val_bits = 8,
   .max_register = 0x0700,
};
 
static struct bu18tl82 *bridge_to_bu18tl82(struct drm_bridge *bridge)
{
   return container_of(bridge, struct bu18tl82, base);
}
 
static int bu18tl82_parse_init_seq(struct device *dev, const u16 *data,
                  int length, struct serdes_init_seq *seq)
{
   struct reg_sequence *reg_sequence;
   u16 *buf, *d;
   unsigned int i, cnt;
 
   if (!seq)
       return -EINVAL;
 
   buf = devm_kmemdup(dev, data, length, GFP_KERNEL);
   if (!buf)
       return -ENOMEM;
 
   d = buf;
   cnt = length / 4;
   seq->reg_seq_cnt = cnt;
 
   seq->reg_sequence = devm_kcalloc(dev, cnt, sizeof(struct reg_sequence), GFP_KERNEL);
   if (!seq->reg_sequence)
       return -ENOMEM;
 
 
   for (i = 0; i < cnt; i++) {
       reg_sequence = &seq->reg_sequence[i];
       reg_sequence->reg = get_unaligned_be16(&d[0]);
       reg_sequence->def = get_unaligned_be16(&d[1]);
       d += 2;
   }
 
   return 0;
}
 
static int bu18tl82_get_init_seq(struct bu18tl82 *bu18tl82)
{
   struct device *dev = bu18tl82->dev;
   struct device_node *np = dev->of_node;
   const void *data;
   int len, err;
 
   data = of_get_property(np, "serdes-init-sequence", &len);
   if (!data) {
       dev_err(dev, "failed to get serdes-init-sequence\n");
       return -EINVAL;
   }
 
   bu18tl82->serdes_init_seq = devm_kzalloc(dev, sizeof(*bu18tl82->serdes_init_seq),
                        GFP_KERNEL);
   if (!bu18tl82->serdes_init_seq)
       return -ENOMEM;
 
   err = bu18tl82_parse_init_seq(dev, data, len, bu18tl82->serdes_init_seq);
   if (err) {
       dev_err(dev, "failed to parse serdes-init-sequence\n");
       return err;
   }
 
   return 0;
}
 
static int bu18tl82_bridge_get_modes(struct drm_bridge *bridge,
                    struct drm_connector *connector)
{
   struct bu18tl82 *bu18tl82 = bridge_to_bu18tl82(bridge);
 
   return drm_bridge_get_modes(bu18tl82->bridge, connector);
}
 
static struct mipi_dsi_device *bu18tl82_attach_dsi(struct bu18tl82 *bu18tl82,
                        struct device_node *dsi_node)
{
   const struct mipi_dsi_device_info info = { "bu18tl82", 0, NULL };
   struct mipi_dsi_device *dsi;
   struct mipi_dsi_host *host;
   int ret;
 
   host = of_find_mipi_dsi_host_by_node(dsi_node);
   if (!host) {
       dev_err(bu18tl82->dev, "failed to find dsi host\n");
       return ERR_PTR(-EPROBE_DEFER);
   }
 
   dsi = mipi_dsi_device_register_full(host, &info);
   if (IS_ERR(dsi)) {
       dev_err(bu18tl82->dev, "failed to create dsi device\n");
       return dsi;
   }
 
   dsi->lanes = 4;
   dsi->format = MIPI_DSI_FMT_RGB888;
   dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
             MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_EOT_PACKET;
 
   ret = mipi_dsi_attach(dsi);
   if (ret < 0) {
       dev_err(bu18tl82->dev, "failed to attach dsi to host\n");
       mipi_dsi_device_unregister(dsi);
       return ERR_PTR(ret);
   }
 
   return dsi;
}
 
static int bu18tl82_bridge_attach(struct drm_bridge *bridge,
                 enum drm_bridge_attach_flags flags)
{
   struct bu18tl82 *bu18tl82 = bridge_to_bu18tl82(bridge);
   int ret;
 
   ret = drm_of_find_panel_or_bridge(bu18tl82->dev->of_node, 1, -1,
                     NULL, &bu18tl82->bridge);
   if (ret)
       return ret;
 
   if (bu18tl82->sel_mipi) {
       /* Attach primary DSI */
       bu18tl82->dsi = bu18tl82_attach_dsi(bu18tl82, bu18tl82->dsi_node);
       if (IS_ERR(bu18tl82->dsi))
           return PTR_ERR(bu18tl82->dsi);
   }
 
   ret = drm_bridge_attach(bridge->encoder, bu18tl82->bridge,
               bridge, flags);
   if (ret) {
       if (bu18tl82->sel_mipi)
           mipi_dsi_device_unregister(bu18tl82->dsi);
 
       dev_err(bu18tl82->dev, "failed to attach bridge\n");
       return ret;
   }
 
   return 0;
}
 
static void bu18tl82_bridge_detach(struct drm_bridge *bridge)
{
   struct bu18tl82 *bu18tl82 = bridge_to_bu18tl82(bridge);
 
   if (bu18tl82->sel_mipi) {
       mipi_dsi_detach(bu18tl82->dsi);
       mipi_dsi_device_unregister(bu18tl82->dsi);
   }
}
 
static void bu18tl82_bridge_enable(struct drm_bridge *bridge)
{
}
 
static void bu18tl82_bridge_disable(struct drm_bridge *bridge)
{
}
 
static void bu18tl82_bridge_pre_enable(struct drm_bridge *bridge)
{
   struct bu18tl82 *bu18tl82 = bridge_to_bu18tl82(bridge);
   struct serdes_init_seq *init_seq = bu18tl82->serdes_init_seq;
   int ret;
 
   if (bu18tl82->enable_gpio) {
       gpiod_direction_output(bu18tl82->enable_gpio, 1);
       msleep(20);
   }
 
   if (bu18tl82->reset_gpio) {
       gpiod_direction_output(bu18tl82->reset_gpio, 0);
       msleep(30);
   }
 
   ret = regmap_multi_reg_write(bu18tl82->regmap, init_seq->reg_sequence,
                    init_seq->reg_seq_cnt);
   if (ret) {
       dev_err(bu18tl82->dev, "failed to set register setting: %d\n", ret);
       return;
   }
   msleep(160);
}
 
static void bu18tl82_bridge_post_disable(struct drm_bridge *bridge)
{
   struct bu18tl82 *bu18tl82 = bridge_to_bu18tl82(bridge);
 
   if (bu18tl82->reset_gpio)
       gpiod_direction_output(bu18tl82->reset_gpio, 1);
 
   if (bu18tl82->enable_gpio)
       gpiod_direction_output(bu18tl82->enable_gpio, 0);
}
 
static const struct drm_bridge_funcs bu18tl82_bridge_funcs = {
   .attach = bu18tl82_bridge_attach,
   .detach = bu18tl82_bridge_detach,
   .enable = bu18tl82_bridge_enable,
   .disable = bu18tl82_bridge_disable,
   .pre_enable = bu18tl82_bridge_pre_enable,
   .post_disable = bu18tl82_bridge_post_disable,
   .get_modes = bu18tl82_bridge_get_modes,
};
 
static int bu18tl82_i2c_probe(struct i2c_client *client,
                 const struct i2c_device_id *id)
{
   struct device *dev = &client->dev;
   struct bu18tl82 *bu18tl82;
   int ret;
 
   bu18tl82 = devm_kzalloc(dev, sizeof(*bu18tl82), GFP_KERNEL);
   if (!bu18tl82)
       return -ENOMEM;
 
   bu18tl82->dev = dev;
   i2c_set_clientdata(client, bu18tl82);
 
   bu18tl82->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
   if (IS_ERR(bu18tl82->reset_gpio))
       return dev_err_probe(dev, PTR_ERR(bu18tl82->reset_gpio),
                    "failed to acquire reset gpio\n");
 
   bu18tl82->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_ASIS);
   if (IS_ERR(bu18tl82->enable_gpio))
       return dev_err_probe(dev, PTR_ERR(bu18tl82->enable_gpio),
                    "failed to acquire enable gpio\n");
 
   bu18tl82->regmap = devm_regmap_init_i2c(client, &bu18tl82_regmap_config);
   if (IS_ERR(bu18tl82->regmap))
       return dev_err_probe(dev, PTR_ERR(bu18tl82->regmap),
                    "failed to initialize regmap\n");
 
   bu18tl82->sel_mipi = of_property_read_bool(dev->of_node, "sel-mipi");
 
   if (bu18tl82->sel_mipi) {
       bu18tl82->dsi_node = of_graph_get_remote_node(dev->of_node, 0, -1);
       if (!bu18tl82->dsi_node)
           return dev_err_probe(dev, -ENODEV,
                        "failed to get remote node for dsi\n");
   }
 
   ret = bu18tl82_get_init_seq(bu18tl82);
   if (ret)
       return ret;
 
   bu18tl82->base.funcs = &bu18tl82_bridge_funcs;
   bu18tl82->base.of_node = dev->of_node;
   bu18tl82->base.ops = DRM_BRIDGE_OP_MODES;
 
   drm_bridge_add(&bu18tl82->base);
 
   return 0;
}
 
static int bu18tl82_i2c_remove(struct i2c_client *client)
{
   struct bu18tl82 *bu18tl82 = i2c_get_clientdata(client);
 
   drm_bridge_remove(&bu18tl82->base);
 
   return 0;
}
 
static const struct i2c_device_id bu18tl82_i2c_table[] = {
   { "bu18tl82", 0 },
   {}
};
MODULE_DEVICE_TABLE(i2c, bu18tl82_i2c_table);
 
static const struct of_device_id bu18tl82_of_match[] = {
   { .compatible = "rohm,bu18tl82" },
   {}
};
MODULE_DEVICE_TABLE(of, bu18tl82_of_match);
 
static struct i2c_driver bu18tl82_i2c_driver = {
   .driver = {
       .name = "bu18tl82",
       .of_match_table = bu18tl82_of_match,
   },
   .probe = bu18tl82_i2c_probe,
   .remove = bu18tl82_i2c_remove,
   .id_table = bu18tl82_i2c_table,
};
module_i2c_driver(bu18tl82_i2c_driver);
 
MODULE_AUTHOR("Guochun Huang <hero.huang@rock-chips.com>");
MODULE_DESCRIPTION("Rohm BU18TL82 Clockless Link-BD Serializer with MIPI and LVDS Interface");
MODULE_LICENSE("GPL");