aboutsummaryrefslogtreecommitdiff
path: root/module/reg_sensor/src/mod_reg_sensor.c
blob: c6c6119d4dee7317567489f45ab6fb5a00a9c36a (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
/*
 * Arm SCP/MCP Software
 * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdint.h>
#include <fwk_assert.h>
#include <fwk_errno.h>
#include <fwk_id.h>
#include <fwk_mm.h>
#include <fwk_module.h>
#include <mod_reg_sensor.h>
#include <mod_sensor.h>

static struct mod_reg_sensor_dev_config **config_table;

/*
 * Module API
 */
static int get_value(fwk_id_t id, uint64_t *value)
{
    int status;
    struct mod_reg_sensor_dev_config *config;

    status = fwk_module_check_call(id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    config = config_table[fwk_id_get_element_idx(id)];

    if (value == NULL) {
        assert(false);
        return FWK_E_PARAM;
    }

    *value = *(uint64_t*)config->reg;

    return FWK_SUCCESS;
}

static int get_info(fwk_id_t id, struct mod_sensor_info *info)
{
    int status;
    struct mod_reg_sensor_dev_config *config;

    status = fwk_module_check_call(id);
    if (status != FWK_SUCCESS)
        return status;

    config = config_table[fwk_id_get_element_idx(id)];
    fwk_assert(config != NULL);

    if (info == NULL)
        return FWK_E_PARAM;

    *info = *(config->info);

    return FWK_SUCCESS;
}

static const struct mod_sensor_driver_api reg_sensor_api = {
    .get_value = get_value,
    .get_info = get_info,
};

/*
 * Framework handlers
 */
static int reg_sensor_init(fwk_id_t module_id,
                           unsigned int element_count,
                           const void *unused)
{
    config_table = fwk_mm_alloc(element_count, sizeof(*config_table));

    if (config_table == NULL)
        return FWK_E_NOMEM;

    return FWK_SUCCESS;
}

static int reg_sensor_element_init(fwk_id_t element_id,
                                   unsigned int sub_element_count,
                                   const void *data)
{
    struct mod_reg_sensor_dev_config *config =
        (struct mod_reg_sensor_dev_config *)data;

    if (config->reg == 0)
        return FWK_E_DATA;

    config_table[fwk_id_get_element_idx(element_id)] = config;

    return FWK_SUCCESS;
}

static int reg_sensor_process_bind_request(fwk_id_t source_id,
                                           fwk_id_t target_id,
                                           fwk_id_t api_type,
                                           const void **api)
{
    *api = &reg_sensor_api;
    return FWK_SUCCESS;
}

const struct fwk_module module_reg_sensor = {
    .name = "Register Sensor",
    .api_count = 1,
    .type = FWK_MODULE_TYPE_DRIVER,
    .init = reg_sensor_init,
    .element_init = reg_sensor_element_init,
    .process_bind_request = reg_sensor_process_bind_request,
};