1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c), Vaisala Oyj
8 #include <dm/device_compat.h>
11 #include <reboot-mode/reboot-mode.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 int dm_reboot_mode_update(struct udevice *dev)
17 struct reboot_mode_ops *ops = reboot_mode_get_ops(dev);
26 ret = ops->get(dev, &rebootmode);
28 dev_err(dev, "Failed to retrieve the reboot mode value\n");
32 const struct reboot_mode_uclass_platdata *plat_data =
33 dev_get_uclass_plat(dev);
35 for (i = 0; i < plat_data->count; i++) {
36 if (plat_data->modes[i].mode_id == rebootmode) {
37 ret = env_set(plat_data->env_variable,
38 plat_data->modes[i].mode_name);
40 dev_err(dev, "Failed to set env: %s\n",
41 plat_data->env_variable);
50 ret = ops->set(dev, rebootmode);
52 dev_err(dev, "Failed to clear the reboot mode\n");
60 int dm_reboot_mode_pre_probe(struct udevice *dev)
62 struct reboot_mode_uclass_platdata *plat_data;
64 plat_data = dev_get_uclass_plat(dev);
68 #if CONFIG_IS_ENABLED(OF_CONTROL)
69 const int node = dev_of_offset(dev);
70 const char *mode_prefix = "mode-";
71 const int mode_prefix_len = strlen(mode_prefix);
76 plat_data->env_variable = fdt_getprop(gd->fdt_blob,
78 "u-boot,env-variable",
80 if (!plat_data->env_variable)
81 plat_data->env_variable = "reboot-mode";
85 fdt_for_each_property_offset(property, gd->fdt_blob, node) {
86 propvalue = fdt_getprop_by_offset(gd->fdt_blob,
87 property, &propname, NULL);
89 dev_err(dev, "Could not get the value for property %s\n",
94 if (!strncmp(propname, mode_prefix, mode_prefix_len))
98 plat_data->modes = devm_kcalloc(dev, plat_data->count,
99 sizeof(struct reboot_mode_mode), 0);
101 struct reboot_mode_mode *next = plat_data->modes;
103 fdt_for_each_property_offset(property, gd->fdt_blob, node) {
104 propvalue = fdt_getprop_by_offset(gd->fdt_blob,
105 property, &propname, NULL);
107 dev_err(dev, "Could not get the value for property %s\n",
112 if (!strncmp(propname, mode_prefix, mode_prefix_len)) {
113 next->mode_name = &propname[mode_prefix_len];
114 next->mode_id = fdt32_to_cpu(*propvalue);
120 if (!plat_data->env_variable)
121 plat_data->env_variable = "reboot-mode";
128 UCLASS_DRIVER(reboot_mode) = {
129 .name = "reboot-mode",
130 .id = UCLASS_REBOOT_MODE,
131 .pre_probe = dm_reboot_mode_pre_probe,
132 .per_device_plat_auto =
133 sizeof(struct reboot_mode_uclass_platdata),