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 int dm_reboot_mode_update(struct udevice *dev)
15 struct reboot_mode_ops *ops = reboot_mode_get_ops(dev);
24 ret = ops->get(dev, &rebootmode);
26 dev_err(dev, "Failed to retrieve the reboot mode value\n");
30 const struct reboot_mode_uclass_platdata *plat_data =
31 dev_get_uclass_plat(dev);
33 for (i = 0; i < plat_data->count; i++) {
34 if (plat_data->modes[i].mode_id == rebootmode) {
35 ret = env_set(plat_data->env_variable,
36 plat_data->modes[i].mode_name);
38 dev_err(dev, "Failed to set env: %s\n",
39 plat_data->env_variable);
48 ret = ops->set(dev, rebootmode);
50 dev_err(dev, "Failed to clear the reboot mode\n");
58 int dm_reboot_mode_pre_probe(struct udevice *dev)
60 struct reboot_mode_uclass_platdata *plat_data;
62 plat_data = dev_get_uclass_plat(dev);
66 #if CONFIG_IS_ENABLED(OF_CONTROL)
67 const char *mode_prefix = "mode-";
68 const int mode_prefix_len = strlen(mode_prefix);
69 struct ofprop property;
73 plat_data->env_variable = dev_read_string(dev, "u-boot,env-variable");
74 if (!plat_data->env_variable)
75 plat_data->env_variable = "reboot-mode";
79 dev_for_each_property(property, dev) {
80 propvalue = dev_read_prop_by_prop(&property, &propname, NULL);
82 dev_err(dev, "Could not get the value for property %s\n",
87 if (!strncmp(propname, mode_prefix, mode_prefix_len))
91 plat_data->modes = devm_kcalloc(dev, plat_data->count,
92 sizeof(struct reboot_mode_mode), 0);
94 struct reboot_mode_mode *next = plat_data->modes;
96 dev_for_each_property(property, dev) {
97 propvalue = dev_read_prop_by_prop(&property, &propname, NULL);
99 dev_err(dev, "Could not get the value for property %s\n",
104 if (!strncmp(propname, mode_prefix, mode_prefix_len)) {
105 next->mode_name = &propname[mode_prefix_len];
106 next->mode_id = fdt32_to_cpu(*propvalue);
112 if (!plat_data->env_variable)
113 plat_data->env_variable = "reboot-mode";
120 UCLASS_DRIVER(reboot_mode) = {
121 .name = "reboot-mode",
122 .id = UCLASS_REBOOT_MODE,
123 .pre_probe = dm_reboot_mode_pre_probe,
124 .per_device_plat_auto =
125 sizeof(struct reboot_mode_uclass_platdata),