xref: /linux/drivers/power/reset/nvmem-reboot-mode.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) Vaisala Oyj. All rights reserved.
4  */
5 
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/of.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/platform_device.h>
12 #include <linux/reboot-mode.h>
13 
14 struct nvmem_reboot_mode {
15 	struct reboot_mode_driver reboot;
16 	struct nvmem_cell *cell;
17 };
18 
19 static int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
20 				    unsigned int magic)
21 {
22 	int ret;
23 	struct nvmem_reboot_mode *nvmem_rbm;
24 
25 	nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
26 
27 	ret = nvmem_cell_write(nvmem_rbm->cell, &magic, sizeof(magic));
28 	if (ret < 0)
29 		dev_err(reboot->dev, "update reboot mode bits failed\n");
30 
31 	return ret;
32 }
33 
34 static int nvmem_reboot_mode_probe(struct platform_device *pdev)
35 {
36 	int ret;
37 	struct nvmem_reboot_mode *nvmem_rbm;
38 
39 	nvmem_rbm = devm_kzalloc(&pdev->dev, sizeof(*nvmem_rbm), GFP_KERNEL);
40 	if (!nvmem_rbm)
41 		return -ENOMEM;
42 
43 	nvmem_rbm->reboot.dev = &pdev->dev;
44 	nvmem_rbm->reboot.write = nvmem_reboot_mode_write;
45 
46 	nvmem_rbm->cell = devm_nvmem_cell_get(&pdev->dev, "reboot-mode");
47 	if (IS_ERR(nvmem_rbm->cell)) {
48 		dev_err(&pdev->dev, "failed to get the nvmem cell reboot-mode\n");
49 		return PTR_ERR(nvmem_rbm->cell);
50 	}
51 
52 	ret = devm_reboot_mode_register(&pdev->dev, &nvmem_rbm->reboot);
53 	if (ret)
54 		dev_err(&pdev->dev, "can't register reboot mode\n");
55 
56 	return ret;
57 }
58 
59 static const struct of_device_id nvmem_reboot_mode_of_match[] = {
60 	{ .compatible = "nvmem-reboot-mode" },
61 	{}
62 };
63 MODULE_DEVICE_TABLE(of, nvmem_reboot_mode_of_match);
64 
65 static struct platform_driver nvmem_reboot_mode_driver = {
66 	.probe = nvmem_reboot_mode_probe,
67 	.driver = {
68 		.name = "nvmem-reboot-mode",
69 		.of_match_table = nvmem_reboot_mode_of_match,
70 	},
71 };
72 module_platform_driver(nvmem_reboot_mode_driver);
73 
74 MODULE_AUTHOR("Nandor Han <nandor.han@vaisala.com>");
75 MODULE_DESCRIPTION("NVMEM reboot mode driver");
76 MODULE_LICENSE("GPL");
77