summaryrefslogtreecommitdiff
path: root/components/drivers/graphic/framebuffer/fb-simple.c
blob: 55f80542cf761def3710985cfb1ddc1589058447 (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
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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "fb.simple"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

struct simplefb_format
{
    const char *name;
    rt_uint32_t mode;
    rt_uint32_t bits_per_pixel;
};

struct simplefb_params
{
    rt_uint32_t width;
    rt_uint32_t height;
    rt_uint32_t stride;
    struct simplefb_format *format;
};

struct simplefb
{
    struct rt_graphic_device parent;

    void *screen_base;
    rt_size_t screen_size;
    rt_size_t stride;

#ifdef RT_USING_CLK
    rt_bool_t clk_arr_enabled;
    struct rt_clk_array *clk_arr;
#endif
#ifdef RT_USING_REGULATOR
    rt_bool_t supplys_enabled;
    rt_size_t supplys_nr;
    struct rt_regulator **supplys;
#endif
};

#ifdef RT_USING_CLK
static rt_err_t simplefb_clk_probe(struct simplefb *sfb,
        struct rt_platform_device *pdev)
{
    sfb->clk_arr = rt_clk_get_array(&pdev->parent);

    if (rt_is_err(sfb->clk_arr))
    {
        return rt_ptr_err(sfb->clk_arr);
    }

    return RT_EOK;
}

static void simplefb_clk_enable(struct simplefb *sfb)
{
    rt_clk_array_prepare_enable(sfb->clk_arr);
    sfb->clk_arr_enabled = RT_TRUE;
}

static void simplefb_clk_remove(struct simplefb *sfb)
{
    if (!rt_is_err_or_null(sfb->clk_arr))
    {
        if (sfb->clk_arr_enabled)
        {
            rt_clk_array_disable_unprepare(sfb->clk_arr);
        }

        rt_clk_array_put(sfb->clk_arr);
    }
}
#else
static rt_err_t simplefb_clk_probe(struct simplefb *sfb,
        struct rt_platform_device *pdev) { return RT_EOK; }
static void simplefb_clk_enable(struct simplefb *sfb) { }
static void simplefb_clk_remove(struct simplefb *sfb) { }
#endif /* RT_USING_CLK */

#ifdef RT_USING_REGULATOR
#define SUPPLY_SUFFIX "-supply"

static rt_err_t simplefb_regulator_probe(struct simplefb *sfb,
        struct rt_platform_device *pdev)
{
    int i = 0;
    const char *name;
    struct rt_device *dev = &pdev->parent;
    struct rt_ofw_prop *prop;
    struct rt_ofw_node *np = dev->ofw_node;

    rt_ofw_foreach_prop(np, prop)
    {
        name = rt_strstr(prop->name, SUPPLY_SUFFIX);

        if (name && name != prop->name)
        {
            ++sfb->supplys_nr;
        }
    }

    sfb->supplys = rt_calloc(sfb->supplys_nr, sizeof(sfb->supplys[0]));

    if (!sfb->supplys)
    {
        return -RT_ENOMEM;
    }

    rt_ofw_foreach_prop(np, prop)
    {
        name = rt_strstr(prop->name, SUPPLY_SUFFIX);

        if (name && name != prop->name)
        {
            char name[32];
            int len = name - prop->name;

            rt_strncpy(name, prop->name, len);
            name[len] = '\0';

            sfb->supplys[i] = rt_regulator_get(dev, (const char *)name);

            if (rt_is_err(sfb->supplys[i]))
            {
                return rt_ptr_err(sfb->supplys[i]);
            }

            ++i;
        }
    }

    return RT_EOK;
}

static void simplefb_regulator_enable(struct simplefb *sfb)
{
    if (sfb->supplys)
    {
        for (int i = 0; i < sfb->supplys_nr; ++i)
        {
            rt_regulator_enable(sfb->supplys[i]);
        }

        sfb->supplys_enabled = RT_TRUE;
    }
}

static void simplefb_regulator_remove(struct simplefb *sfb)
{
    if (sfb->supplys && sfb->supplys_enabled)
    {
        for (int i = 0; i < sfb->supplys_nr; ++i)
        {
            struct rt_regulator *supply = sfb->supplys[i];

            if (!rt_is_err(supply))
            {
                rt_regulator_disable(supply);
                rt_regulator_put(supply);
            }
        }

        rt_free(sfb->supplys);
    }
}
#else
static rt_err_t simplefb_regulator_probe(struct simplefb *sfb,
        struct rt_platform_device *pdev)
{
    return RT_EOK;
}

static void simplefb_regulator_enable(struct simplefb *sfb)
{
}

static void simplefb_regulator_remove(struct simplefb *sfb)
{
}
#endif /* RT_USING_REGULATOR */

static struct simplefb_format simplefb_formats[] =
{
    { "r5g6b5",   RTGRAPHIC_PIXEL_FORMAT_RGB565,  16 },
    { "r8g8b8",   RTGRAPHIC_PIXEL_FORMAT_RGB888,  24 },
    { "x8r8g8b8", RTGRAPHIC_PIXEL_FORMAT_ARGB888, 32 },
    { "a8r8g8b8", RTGRAPHIC_PIXEL_FORMAT_ARGB888, 32 },
    { "x8b8g8r8", RTGRAPHIC_PIXEL_FORMAT_ABGR888, 32 },
    { "a8b8g8r8", RTGRAPHIC_PIXEL_FORMAT_ABGR888, 32 },
};

static rt_err_t simplefb_params_parse(struct simplefb_params *params,
        struct rt_platform_device *pdev)
{
    rt_err_t err;
    const char *format;
    struct rt_device *dev = &pdev->parent;

    if ((err = rt_dm_dev_prop_read_u32(dev, "width", &params->width)))
    {
        LOG_E("Can't parse width property");

        return err;
    }

    if ((err = rt_dm_dev_prop_read_u32(dev, "height", &params->height)))
    {
        LOG_E("Can't parse height property");

        return err;
    }

    if ((err = rt_dm_dev_prop_read_u32(dev, "stride", &params->stride)))
    {
        LOG_E("Can't parse stride property");

        return err;
    }

    if ((err = rt_dm_dev_prop_read_string(dev, "format", &format)))
    {
        LOG_E("Can't parse format property");

        return err;
    }

    for (int i = 0; i < RT_ARRAY_SIZE(simplefb_formats); ++i)
    {
        if (rt_strcmp(format, simplefb_formats[i].name))
        {
            continue;
        }

        params->format = &simplefb_formats[i];

        return RT_EOK;
    }

    LOG_E("Unsupport format value");

    return -RT_EINVAL;
}

static rt_err_t simplefb_plane_fb_remap(struct rt_graphic_plane *plane,
        rt_uint32_t mode, struct rt_device_rect_info *rect)
{
    struct simplefb *sfb = rt_container_of(plane->graphic, struct simplefb, parent);

    plane->line_length = sfb->stride;
    plane->bits_per_pixel = rt_graphic_mode_bpp(mode);

    plane->framebuffer = sfb->screen_base;
    plane->screen_len = sfb->screen_size;
    plane->framebuffer_len = sfb->screen_size;

    return RT_EOK;
}

static const struct rt_graphic_plane_ops simplefb_plane_ops =
{
    .fb_remap = simplefb_plane_fb_remap,
};

static rt_err_t simplefb_probe(struct rt_platform_device *pdev)
{
    rt_err_t err;
    rt_uint64_t addr, size;
    struct simplefb_params params = {};
    struct simplefb *sfb = rt_calloc(1, sizeof(*sfb));

    if (!sfb)
    {
        return -RT_ENOMEM;
    }

    if ((err = simplefb_params_parse(&params, pdev)))
    {
        goto _fail;
    }

    sfb->stride = params.stride;

    if ((err = rt_dm_dev_get_address(&pdev->parent, 0, &addr, &size)))
    {
        goto _fail;
    }

    sfb->screen_size = (rt_size_t)size;
    sfb->screen_base = rt_ioremap_wt((void *)addr, sfb->screen_size);

    if (!sfb->screen_base)
    {
        err = -RT_EIO;
        goto _fail;
    }

    if ((err = simplefb_clk_probe(sfb, pdev)))
    {
        LOG_E("Get %s error = %s", "clk", rt_strerror(err));

        goto _fail;
    }

    if ((err = simplefb_regulator_probe(sfb, pdev)))
    {
        LOG_E("Get %s error = %s", "regulator", rt_strerror(err));

        goto _fail;
    }

    simplefb_clk_enable(sfb);
    simplefb_regulator_enable(sfb);

    if ((err = rt_graphic_device_simple_register(&sfb->parent,
            params.width, params.height, 0, &simplefb_plane_ops,
            &params.format->mode, 1)))
    {
        goto _fail;
    }

    pdev->parent.user_data = sfb;

    return RT_EOK;

_fail:
    if (sfb->screen_base)
    {
        rt_iounmap(sfb->screen_base);
    }

    simplefb_clk_remove(sfb);
    simplefb_regulator_remove(sfb);

    rt_free(sfb);

    return err;
}

static rt_err_t simplefb_remove(struct rt_platform_device *pdev)
{
    struct simplefb *sfb = pdev->parent.user_data;

    rt_graphic_device_simple_unregister(&sfb->parent);

    simplefb_clk_remove(sfb);
    simplefb_regulator_remove(sfb);

    rt_iounmap(sfb->screen_base);

    rt_free(sfb);

    return RT_EOK;
}

static const struct rt_ofw_node_id simplefb_ofw_ids[] =
{
    { .compatible = "simple-framebuffer" },
    { /* sentinel */ }
};

static struct rt_platform_driver simplefb_driver =
{
    .name = "simple-framebuffer",
    .ids = simplefb_ofw_ids,

    .probe = simplefb_probe,
    .remove = simplefb_remove,
};
RT_PLATFORM_DRIVER_EXPORT(simplefb_driver);