blob: 6c9d3b17e1db2f8407b009c4e7b3c407bbf0c99f [file] [log] [blame]
Pascal Paillet8b330ce2021-01-20 17:09:10 +01001/*
Pascal Pailletff2b34c2022-03-17 17:20:22 +01002 * Copyright (c) 2021-2023, STMicroelectronics - All Rights Reserved
Pascal Paillet8b330ce2021-01-20 17:09:10 +01003 *
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
Pascal Pailletff2b34c2022-03-17 17:20:22 +010016#ifndef PLAT_NB_FIXED_REGUS
17#error "Missing PLAT_NB_FIXED_REGUS"
Pascal Paillet8b330ce2021-01-20 17:09:10 +010018#endif
19
20#define FIXED_NAME_LEN 32
21
22struct fixed_data {
23 char name[FIXED_NAME_LEN];
24 uint16_t volt;
25 struct regul_description desc;
26};
27
Pascal Pailletff2b34c2022-03-17 17:20:22 +010028static struct fixed_data data[PLAT_NB_FIXED_REGUS];
Pascal Paillet8b330ce2021-01-20 17:09:10 +010029
30static int fixed_set_state(const struct regul_description *desc, bool state)
31{
32 return 0;
33}
34
35static int fixed_get_state(const struct regul_description *desc)
36{
37 return 1;
38}
39
40static struct regul_ops fixed_ops = {
41 .set_state = fixed_set_state,
42 .get_state = fixed_get_state,
43};
44
45int 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++;
Pascal Pailletff2b34c2022-03-17 17:20:22 +010082 assert(count <= PLAT_NB_FIXED_REGUS);
Pascal Paillet8b330ce2021-01-20 17:09:10 +010083
84 }
85
86 return 0;
87}