1 /*
2 * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <errno.h>
9
10 #include <common/debug.h>
11 #include <common/fdt_wrappers.h>
12 #include <drivers/st/regulator.h>
13 #include <drivers/st/regulator_fixed.h>
14 #include <libfdt.h>
15
16 #ifndef PLAT_NB_FIXED_REGS
17 #error "Missing PLAT_NB_FIXED_REGS"
18 #endif
19
20 #define FIXED_NAME_LEN 32
21
22 struct fixed_data {
23 char name[FIXED_NAME_LEN];
24 uint16_t volt;
25 struct regul_description desc;
26 };
27
28 static struct fixed_data data[PLAT_NB_FIXED_REGS];
29
fixed_set_state(const struct regul_description * desc,bool state)30 static int fixed_set_state(const struct regul_description *desc, bool state)
31 {
32 return 0;
33 }
34
fixed_get_state(const struct regul_description * desc)35 static int fixed_get_state(const struct regul_description *desc)
36 {
37 return 1;
38 }
39
40 static struct regul_ops fixed_ops = {
41 .set_state = fixed_set_state,
42 .get_state = fixed_get_state,
43 };
44
fixed_regulator_register(void)45 int fixed_regulator_register(void)
46 {
47 uint32_t count = 0;
48 void *fdt;
49 int node;
50
51 VERBOSE("fixed reg init!\n");
52
53 if (fdt_get_address(&fdt) == 0) {
54 return -FDT_ERR_NOTFOUND;
55 }
56
57 fdt_for_each_compatible_node(fdt, node, "regulator-fixed") {
58 int len __unused;
59 int ret;
60 struct fixed_data *d = &data[count];
61 const char *reg_name;
62
63 reg_name = fdt_get_name(fdt, node, NULL);
64
65 VERBOSE("register fixed reg %s!\n", reg_name);
66
67 len = snprintf(d->name, FIXED_NAME_LEN - 1, "%s", reg_name);
68 assert((len > 0) && (len < (FIXED_NAME_LEN - 1)));
69
70 d->desc.node_name = d->name;
71 d->desc.driver_data = d;
72 d->desc.ops = &fixed_ops;
73
74 ret = regulator_register(&d->desc, node);
75 if (ret != 0) {
76 WARN("%s:%d failed to register %s\n", __func__,
77 __LINE__, reg_name);
78 return ret;
79 }
80
81 count++;
82 assert(count <= PLAT_NB_FIXED_REGS);
83
84 }
85
86 return 0;
87 }
88