18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) Vaisala Oyj. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/init.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/of.h>
108c2ecf20Sopenharmony_ci#include <linux/nvmem-consumer.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/reboot-mode.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistruct nvmem_reboot_mode {
158c2ecf20Sopenharmony_ci	struct reboot_mode_driver reboot;
168c2ecf20Sopenharmony_ci	struct nvmem_cell *cell;
178c2ecf20Sopenharmony_ci};
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
208c2ecf20Sopenharmony_ci				    unsigned int magic)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	int ret;
238c2ecf20Sopenharmony_ci	struct nvmem_reboot_mode *nvmem_rbm;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	ret = nvmem_cell_write(nvmem_rbm->cell, &magic, sizeof(magic));
288c2ecf20Sopenharmony_ci	if (ret < 0)
298c2ecf20Sopenharmony_ci		dev_err(reboot->dev, "update reboot mode bits failed\n");
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	return ret;
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int nvmem_reboot_mode_probe(struct platform_device *pdev)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	int ret;
378c2ecf20Sopenharmony_ci	struct nvmem_reboot_mode *nvmem_rbm;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	nvmem_rbm = devm_kzalloc(&pdev->dev, sizeof(*nvmem_rbm), GFP_KERNEL);
408c2ecf20Sopenharmony_ci	if (!nvmem_rbm)
418c2ecf20Sopenharmony_ci		return -ENOMEM;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	nvmem_rbm->reboot.dev = &pdev->dev;
448c2ecf20Sopenharmony_ci	nvmem_rbm->reboot.write = nvmem_reboot_mode_write;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	nvmem_rbm->cell = devm_nvmem_cell_get(&pdev->dev, "reboot-mode");
478c2ecf20Sopenharmony_ci	if (IS_ERR(nvmem_rbm->cell)) {
488c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get the nvmem cell reboot-mode\n");
498c2ecf20Sopenharmony_ci		return PTR_ERR(nvmem_rbm->cell);
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	ret = devm_reboot_mode_register(&pdev->dev, &nvmem_rbm->reboot);
538c2ecf20Sopenharmony_ci	if (ret)
548c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "can't register reboot mode\n");
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return ret;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic const struct of_device_id nvmem_reboot_mode_of_match[] = {
608c2ecf20Sopenharmony_ci	{ .compatible = "nvmem-reboot-mode" },
618c2ecf20Sopenharmony_ci	{}
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, nvmem_reboot_mode_of_match);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic struct platform_driver nvmem_reboot_mode_driver = {
668c2ecf20Sopenharmony_ci	.probe = nvmem_reboot_mode_probe,
678c2ecf20Sopenharmony_ci	.driver = {
688c2ecf20Sopenharmony_ci		.name = "nvmem-reboot-mode",
698c2ecf20Sopenharmony_ci		.of_match_table = nvmem_reboot_mode_of_match,
708c2ecf20Sopenharmony_ci	},
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_cimodule_platform_driver(nvmem_reboot_mode_driver);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nandor Han <nandor.han@vaisala.com>");
758c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("NVMEM reboot mode driver");
768c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
77