summaryrefslogtreecommitdiff
path: root/components/drivers/phye/phye-mipi-dphy.c
blob: 63ec91f826263c4591abc8ff4874cbc417f396dc (plain)
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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-23     GuEe-GUI     first version
 */

#include <drivers/phye-mipi-dphy.h>

#define PSEC_PER_SEC 1000000000000ULL

static rt_err_t phye_mipi_dphy_calc_config(rt_uint64_t pixel_clock,
                                           rt_uint32_t bpp, rt_uint32_t lanes, rt_uint64_t hs_clk_rate,
                                           struct rt_phye_configure_opts_mipi_dphy *cfg)
{
    rt_uint64_t ui;

    if (!cfg)
    {
        return -RT_EINVAL;
    }

    if (!hs_clk_rate)
    {
        if (!pixel_clock || !bpp || !lanes)
        {
            return -RT_EINVAL;
        }

        hs_clk_rate = pixel_clock * bpp / lanes;
    }

    ui = (PSEC_PER_SEC + hs_clk_rate - 1) / hs_clk_rate;

    cfg->clk_pre     = 8;
    cfg->clk_prepare = 38000;
    cfg->clk_zero    = 262000;
    cfg->clk_post    = 60000 + 52 * (rt_uint32_t)ui;
    cfg->clk_trail   = 60000;
    cfg->lpx         = 50000;
    cfg->hs_prepare  = 40000 + 4 * (rt_uint32_t)ui;
    cfg->hs_zero     = 105000 + 6 * (rt_uint32_t)ui;
    cfg->hs_trail    = (4 * 8 * ui > 60000 + 4 * 4 * ui) ? 4 * 8 * (rt_uint32_t)ui : 60000 + 4 * 4 * (rt_uint32_t)ui;
    cfg->hs_exit     = 100000;
    cfg->hs_clk_rate = hs_clk_rate;
    cfg->lanes       = lanes;

    return RT_EOK;
}

rt_err_t rt_phye_mipi_dphy_get_default_config(rt_uint64_t pixel_clock,
                                              rt_uint32_t bpp, rt_uint32_t lanes,
                                              struct rt_phye_configure_opts_mipi_dphy *cfg)
{
    return phye_mipi_dphy_calc_config(pixel_clock, bpp, lanes, 0, cfg);
}

rt_err_t rt_phye_mipi_dphy_get_default_config_for_hsclk(rt_uint64_t hs_clk_rate,
                                                        rt_uint32_t lanes, struct rt_phye_configure_opts_mipi_dphy *cfg)
{
    if (!hs_clk_rate)
    {
        return -RT_EINVAL;
    }

    return phye_mipi_dphy_calc_config(0, 0, lanes, hs_clk_rate, cfg);
}