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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2024 Rockchip Electronics Co., Ltd.
* Author:
* Guochun Huang <hero.huang@rock-chips.com>
* Heiko Stuebner <heiko.stuebner@cherry.de>
*/
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/component.h>
#include <linux/media-bus-format.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pm_runtime.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <linux/mfd/syscon.h>
#include <linux/phy/phy.h>
#include <drm/bridge/dw_mipi_dsi2.h>
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_of.h>
#include <drm/drm_simple_kms_helper.h>
#include <uapi/linux/videodev2.h>
#include "rockchip_drm_drv.h"
#define PSEC_PER_SEC 1000000000000LL
struct dsigrf_reg {
u16 offset;
u16 lsb;
u16 msb;
};
enum grf_reg_fields {
TXREQCLKHS_EN,
GATING_EN,
IPI_SHUTDN,
IPI_COLORM,
IPI_COLOR_DEPTH,
IPI_FORMAT,
MAX_FIELDS,
};
#define IPI_DEPTH_5_6_5_BITS 0x02
#define IPI_DEPTH_6_BITS 0x03
#define IPI_DEPTH_8_BITS 0x05
#define IPI_DEPTH_10_BITS 0x06
struct rockchip_dw_dsi2_chip_data {
u32 reg;
const struct dsigrf_reg *grf_regs;
unsigned long long max_bit_rate_per_lane;
};
struct dw_mipi_dsi2_rockchip {
struct device *dev;
struct rockchip_encoder encoder;
struct regmap *regmap;
unsigned int lane_mbps; /* per lane */
u32 format;
struct regmap *grf_regmap;
struct phy *phy;
union phy_configure_opts phy_opts;
struct dw_mipi_dsi2 *dmd;
struct dw_mipi_dsi2_plat_data pdata;
const struct rockchip_dw_dsi2_chip_data *cdata;
};
static inline struct dw_mipi_dsi2_rockchip *to_dsi2(struct drm_encoder *encoder)
{
struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
return container_of(rkencoder, struct dw_mipi_dsi2_rockchip, encoder);
}
static void grf_field_write(struct dw_mipi_dsi2_rockchip *dsi2, enum grf_reg_fields index,
unsigned int val)
{
const struct dsigrf_reg *field = &dsi2->cdata->grf_regs[index];
if (!field)
return;
regmap_write(dsi2->grf_regmap, field->offset,
(val << field->lsb) | (GENMASK(field->msb, field->lsb) << 16));
}
static int dw_mipi_dsi2_phy_init(void *priv_data)
{
return 0;
}
static void dw_mipi_dsi2_phy_power_on(void *priv_data)
{
struct dw_mipi_dsi2_rockchip *dsi2 = priv_data;
int ret;
ret = phy_set_mode(dsi2->phy, PHY_MODE_MIPI_DPHY);
if (ret) {
dev_err(dsi2->dev, "Failed to set phy mode: %d\n", ret);
return;
}
phy_configure(dsi2->phy, &dsi2->phy_opts);
phy_power_on(dsi2->phy);
}
static void dw_mipi_dsi2_phy_power_off(void *priv_data)
{
struct dw_mipi_dsi2_rockchip *dsi2 = priv_data;
phy_power_off(dsi2->phy);
}
static int
dw_mipi_dsi2_get_lane_mbps(void *priv_data, const struct drm_display_mode *mode,
unsigned long mode_flags, u32 lanes, u32 format,
unsigned int *lane_mbps)
{
struct dw_mipi_dsi2_rockchip *dsi2 = priv_data;
u64 max_lane_rate, target_phyclk;
unsigned int lane_rate_kbps;
int bpp;
max_lane_rate = dsi2->cdata->max_bit_rate_per_lane;
dsi2->format = format;
bpp = mipi_dsi_pixel_format_to_bpp(format);
if (bpp < 0) {
dev_err(dsi2->dev, "failed to get bpp for pixel format %d\n", format);
return bpp;
}
lane_rate_kbps = mode->clock * bpp / lanes;
/*
* Set BW a little larger only in video burst mode in
* consideration of the protocol overhead and HS mode
* switching to BLLP mode, take 1 / 0.9, since Mbps must
* big than bandwidth of RGB
*/
if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
lane_rate_kbps = (lane_rate_kbps * 10) / 9;
if (lane_rate_kbps > max_lane_rate) {
dev_err(dsi2->dev, "DPHY clock frequency is out of range\n");
return -ERANGE;
}
dsi2->lane_mbps = lane_rate_kbps / 1000;
*lane_mbps = dsi2->lane_mbps;
if (dsi2->phy) {
target_phyclk = DIV_ROUND_CLOSEST_ULL(lane_rate_kbps * lanes * 1000, bpp);
phy_mipi_dphy_get_default_config(target_phyclk, bpp, lanes,
&dsi2->phy_opts.mipi_dphy);
}
return 0;
}
static void dw_mipi_dsi2_phy_get_iface(void *priv_data, struct dw_mipi_dsi2_phy_iface *iface)
{
/* PPI width is fixed to 16 bits in DCPHY */
iface->ppi_width = 16;
iface->phy_type = DW_MIPI_DSI2_DPHY;
}
static int
dw_mipi_dsi2_phy_get_timing(void *priv_data, unsigned int lane_mbps,
struct dw_mipi_dsi2_phy_timing *timing)
{
struct dw_mipi_dsi2_rockchip *dsi2 = priv_data;
struct phy_configure_opts_mipi_dphy *cfg = &dsi2->phy_opts.mipi_dphy;
unsigned long long tmp, ui;
unsigned long long hstx_clk;
hstx_clk = DIV_ROUND_CLOSEST_ULL(dsi2->lane_mbps * USEC_PER_SEC, 16);
ui = ALIGN(PSEC_PER_SEC, hstx_clk);
do_div(ui, hstx_clk);
/* PHY_LP2HS_TIME = (TLPX + THS-PREPARE + THS-ZERO) / Tphy_hstx_clk */
tmp = cfg->lpx + cfg->hs_prepare + cfg->hs_zero;
tmp = DIV_ROUND_CLOSEST_ULL(tmp << 16, ui);
timing->data_lp2hs = tmp;
/* PHY_HS2LP_TIME = (THS-TRAIL + THS-EXIT) / Tphy_hstx_clk */
tmp = cfg->hs_trail + cfg->hs_exit;
tmp = DIV_ROUND_CLOSEST_ULL(tmp << 16, ui);
timing->data_hs2lp = tmp;
return 0;
}
static const struct dw_mipi_dsi2_phy_ops dw_mipi_dsi2_rockchip_phy_ops = {
.init = dw_mipi_dsi2_phy_init,
.power_on = dw_mipi_dsi2_phy_power_on,
.power_off = dw_mipi_dsi2_phy_power_off,
.get_interface = dw_mipi_dsi2_phy_get_iface,
.get_lane_mbps = dw_mipi_dsi2_get_lane_mbps,
.get_timing = dw_mipi_dsi2_phy_get_timing,
};
static void dw_mipi_dsi2_encoder_atomic_enable(struct drm_encoder *encoder,
struct drm_atomic_state *state)
{
struct dw_mipi_dsi2_rockchip *dsi2 = to_dsi2(encoder);
u32 color_depth;
switch (dsi2->format) {
case MIPI_DSI_FMT_RGB666:
case MIPI_DSI_FMT_RGB666_PACKED:
color_depth = IPI_DEPTH_6_BITS;
break;
case MIPI_DSI_FMT_RGB565:
color_depth = IPI_DEPTH_5_6_5_BITS;
break;
case MIPI_DSI_FMT_RGB888:
color_depth = IPI_DEPTH_8_BITS;
break;
default:
/* Should've been caught by atomic_check */
WARN_ON(1);
return;
}
grf_field_write(dsi2, IPI_COLOR_DEPTH, color_depth);
}
static int
dw_mipi_dsi2_encoder_atomic_check(struct drm_encoder *encoder,
struct drm_crtc_state *crtc_state,
struct drm_connector_state *conn_state)
{
struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
struct dw_mipi_dsi2_rockchip *dsi2 = to_dsi2(encoder);
struct drm_connector *connector = conn_state->connector;
struct drm_display_info *info = &connector->display_info;
switch (dsi2->format) {
case MIPI_DSI_FMT_RGB666:
case MIPI_DSI_FMT_RGB666_PACKED:
s->output_mode = ROCKCHIP_OUT_MODE_P666;
break;
case MIPI_DSI_FMT_RGB565:
s->output_mode = ROCKCHIP_OUT_MODE_P565;
break;
case MIPI_DSI_FMT_RGB888:
s->output_mode = ROCKCHIP_OUT_MODE_P888;
break;
default:
WARN_ON(1);
return -EINVAL;
}
if (info->num_bus_formats)
s->bus_format = info->bus_formats[0];
else
s->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
s->output_type = DRM_MODE_CONNECTOR_DSI;
s->bus_flags = info->bus_flags;
s->color_space = V4L2_COLORSPACE_DEFAULT;
return 0;
}
static const struct drm_encoder_helper_funcs
dw_mipi_dsi2_encoder_helper_funcs = {
.atomic_enable = dw_mipi_dsi2_encoder_atomic_enable,
.atomic_check = dw_mipi_dsi2_encoder_atomic_check,
};
static int rockchip_dsi2_drm_create_encoder(struct dw_mipi_dsi2_rockchip *dsi2,
struct drm_device *drm_dev)
{
struct drm_encoder *encoder = &dsi2->encoder.encoder;
int ret;
encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
dsi2->dev->of_node);
ret = drm_simple_encoder_init(drm_dev, encoder, DRM_MODE_ENCODER_DSI);
if (ret) {
dev_err(dsi2->dev, "Failed to initialize encoder with drm\n");
return ret;
}
drm_encoder_helper_add(encoder, &dw_mipi_dsi2_encoder_helper_funcs);
return 0;
}
static int dw_mipi_dsi2_rockchip_bind(struct device *dev, struct device *master,
void *data)
{
struct dw_mipi_dsi2_rockchip *dsi2 = dev_get_drvdata(dev);
struct drm_device *drm_dev = data;
int ret;
ret = rockchip_dsi2_drm_create_encoder(dsi2, drm_dev);
if (ret)
return dev_err_probe(dev, ret, "Failed to create drm encoder\n");
rockchip_drm_encoder_set_crtc_endpoint_id(&dsi2->encoder,
dev->of_node, 0, 0);
ret = dw_mipi_dsi2_bind(dsi2->dmd, &dsi2->encoder.encoder);
if (ret)
return dev_err_probe(dev, ret, "Failed to bind\n");
return 0;
}
static void dw_mipi_dsi2_rockchip_unbind(struct device *dev, struct device *master,
void *data)
{
struct dw_mipi_dsi2_rockchip *dsi2 = dev_get_drvdata(dev);
dw_mipi_dsi2_unbind(dsi2->dmd);
}
static const struct component_ops dw_mipi_dsi2_rockchip_ops = {
.bind = dw_mipi_dsi2_rockchip_bind,
.unbind = dw_mipi_dsi2_rockchip_unbind,
};
static int dw_mipi_dsi2_rockchip_host_attach(void *priv_data,
struct mipi_dsi_device *device)
{
struct dw_mipi_dsi2_rockchip *dsi2 = priv_data;
int ret;
ret = component_add(dsi2->dev, &dw_mipi_dsi2_rockchip_ops);
if (ret)
return dev_err_probe(dsi2->dev, ret, "Failed to register component\n");
return 0;
}
static int dw_mipi_dsi2_rockchip_host_detach(void *priv_data,
struct mipi_dsi_device *device)
{
struct dw_mipi_dsi2_rockchip *dsi2 = priv_data;
component_del(dsi2->dev, &dw_mipi_dsi2_rockchip_ops);
return 0;
}
static const struct dw_mipi_dsi2_host_ops dw_mipi_dsi2_rockchip_host_ops = {
.attach = dw_mipi_dsi2_rockchip_host_attach,
.detach = dw_mipi_dsi2_rockchip_host_detach,
};
static const struct regmap_config dw_mipi_dsi2_rockchip_regmap_config = {
.name = "dsi2-host",
.reg_bits = 32,
.val_bits = 32,
.reg_stride = 4,
.fast_io = true,
};
static int dw_mipi_dsi2_rockchip_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct device_node *np = dev->of_node;
const struct rockchip_dw_dsi2_chip_data *cdata =
of_device_get_match_data(dev);
struct dw_mipi_dsi2_rockchip *dsi2;
struct resource *res;
void __iomem *base;
int i;
dsi2 = devm_kzalloc(dev, sizeof(*dsi2), GFP_KERNEL);
if (!dsi2)
return -ENOMEM;
base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
if (IS_ERR(base))
return dev_err_probe(dev, PTR_ERR(base), "Unable to get dsi registers\n");
dsi2->regmap = devm_regmap_init_mmio(dev, base, &dw_mipi_dsi2_rockchip_regmap_config);
if (IS_ERR(dsi2->regmap))
return dev_err_probe(dev, PTR_ERR(dsi2->regmap), "failed to init register map\n");
i = 0;
while (cdata[i].reg) {
if (cdata[i].reg == res->start) {
dsi2->cdata = &cdata[i];
break;
}
i++;
}
if (!dsi2->cdata)
return dev_err_probe(dev, -EINVAL, "No dsi-config for %s node\n", np->name);
dsi2->grf_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
if (IS_ERR(dsi2->grf_regmap))
return dev_err_probe(dsi2->dev, PTR_ERR(dsi2->grf_regmap), "Unable to get grf\n");
dsi2->phy = devm_phy_optional_get(dev, "dcphy");
if (IS_ERR(dsi2->phy))
return dev_err_probe(dev, PTR_ERR(dsi2->phy), "failed to get mipi phy\n");
dsi2->dev = dev;
dsi2->pdata.regmap = dsi2->regmap;
dsi2->pdata.max_data_lanes = 4;
dsi2->pdata.phy_ops = &dw_mipi_dsi2_rockchip_phy_ops;
dsi2->pdata.host_ops = &dw_mipi_dsi2_rockchip_host_ops;
dsi2->pdata.priv_data = dsi2;
platform_set_drvdata(pdev, dsi2);
dsi2->dmd = dw_mipi_dsi2_probe(pdev, &dsi2->pdata);
if (IS_ERR(dsi2->dmd))
return dev_err_probe(dev, PTR_ERR(dsi2->dmd), "Failed to probe dw_mipi_dsi2\n");
return 0;
}
static void dw_mipi_dsi2_rockchip_remove(struct platform_device *pdev)
{
struct dw_mipi_dsi2_rockchip *dsi2 = platform_get_drvdata(pdev);
dw_mipi_dsi2_remove(dsi2->dmd);
}
static const struct dsigrf_reg rk3588_dsi0_grf_reg_fields[MAX_FIELDS] = {
[TXREQCLKHS_EN] = { 0x0000, 11, 11 },
[GATING_EN] = { 0x0000, 10, 10 },
[IPI_SHUTDN] = { 0x0000, 9, 9 },
[IPI_COLORM] = { 0x0000, 8, 8 },
[IPI_COLOR_DEPTH] = { 0x0000, 4, 7 },
[IPI_FORMAT] = { 0x0000, 0, 3 },
};
static const struct dsigrf_reg rk3588_dsi1_grf_reg_fields[MAX_FIELDS] = {
[TXREQCLKHS_EN] = { 0x0004, 11, 11 },
[GATING_EN] = { 0x0004, 10, 10 },
[IPI_SHUTDN] = { 0x0004, 9, 9 },
[IPI_COLORM] = { 0x0004, 8, 8 },
[IPI_COLOR_DEPTH] = { 0x0004, 4, 7 },
[IPI_FORMAT] = { 0x0004, 0, 3 },
};
static const struct rockchip_dw_dsi2_chip_data rk3588_chip_data[] = {
{
.reg = 0xfde20000,
.grf_regs = rk3588_dsi0_grf_reg_fields,
.max_bit_rate_per_lane = 4500000ULL,
},
{
.reg = 0xfde30000,
.grf_regs = rk3588_dsi1_grf_reg_fields,
.max_bit_rate_per_lane = 4500000ULL,
}
};
static const struct of_device_id dw_mipi_dsi2_rockchip_dt_ids[] = {
{
.compatible = "rockchip,rk3588-mipi-dsi2",
.data = &rk3588_chip_data,
},
{}
};
MODULE_DEVICE_TABLE(of, dw_mipi_dsi2_rockchip_dt_ids);
struct platform_driver dw_mipi_dsi2_rockchip_driver = {
.probe = dw_mipi_dsi2_rockchip_probe,
.remove = dw_mipi_dsi2_rockchip_remove,
.driver = {
.of_match_table = dw_mipi_dsi2_rockchip_dt_ids,
.name = "dw-mipi-dsi2",
},
};
|