xref: /openbmc/linux/drivers/power/reset/qcom-pon.c (revision e6a578e2)
1e6a578e2SVinod Koul // SPDX-License-Identifier: GPL-2.0
2e6a578e2SVinod Koul // Copyright (c) 2017-18 Linaro Limited
3e6a578e2SVinod Koul 
4e6a578e2SVinod Koul #include <linux/delay.h>
5e6a578e2SVinod Koul #include <linux/errno.h>
6e6a578e2SVinod Koul #include <linux/kernel.h>
7e6a578e2SVinod Koul #include <linux/module.h>
8e6a578e2SVinod Koul #include <linux/of.h>
9e6a578e2SVinod Koul #include <linux/of_platform.h>
10e6a578e2SVinod Koul #include <linux/platform_device.h>
11e6a578e2SVinod Koul #include <linux/reboot.h>
12e6a578e2SVinod Koul #include <linux/reboot-mode.h>
13e6a578e2SVinod Koul #include <linux/regmap.h>
14e6a578e2SVinod Koul 
15e6a578e2SVinod Koul #define PON_SOFT_RB_SPARE		0x8f
16e6a578e2SVinod Koul 
17e6a578e2SVinod Koul struct pm8916_pon {
18e6a578e2SVinod Koul 	struct device *dev;
19e6a578e2SVinod Koul 	struct regmap *regmap;
20e6a578e2SVinod Koul 	u32 baseaddr;
21e6a578e2SVinod Koul 	struct reboot_mode_driver reboot_mode;
22e6a578e2SVinod Koul };
23e6a578e2SVinod Koul 
24e6a578e2SVinod Koul static int pm8916_reboot_mode_write(struct reboot_mode_driver *reboot,
25e6a578e2SVinod Koul 				    unsigned int magic)
26e6a578e2SVinod Koul {
27e6a578e2SVinod Koul 	struct pm8916_pon *pon = container_of
28e6a578e2SVinod Koul 			(reboot, struct pm8916_pon, reboot_mode);
29e6a578e2SVinod Koul 	int ret;
30e6a578e2SVinod Koul 
31e6a578e2SVinod Koul 	ret = regmap_update_bits(pon->regmap,
32e6a578e2SVinod Koul 				 pon->baseaddr + PON_SOFT_RB_SPARE,
33e6a578e2SVinod Koul 				 0xfc, magic << 2);
34e6a578e2SVinod Koul 	if (ret < 0)
35e6a578e2SVinod Koul 		dev_err(pon->dev, "update reboot mode bits failed\n");
36e6a578e2SVinod Koul 
37e6a578e2SVinod Koul 	return ret;
38e6a578e2SVinod Koul }
39e6a578e2SVinod Koul 
40e6a578e2SVinod Koul static int pm8916_pon_probe(struct platform_device *pdev)
41e6a578e2SVinod Koul {
42e6a578e2SVinod Koul 	struct pm8916_pon *pon;
43e6a578e2SVinod Koul 	int error;
44e6a578e2SVinod Koul 
45e6a578e2SVinod Koul 	pon = devm_kzalloc(&pdev->dev, sizeof(*pon), GFP_KERNEL);
46e6a578e2SVinod Koul 	if (!pon)
47e6a578e2SVinod Koul 		return -ENOMEM;
48e6a578e2SVinod Koul 
49e6a578e2SVinod Koul 	pon->dev = &pdev->dev;
50e6a578e2SVinod Koul 
51e6a578e2SVinod Koul 	pon->regmap = dev_get_regmap(pdev->dev.parent, NULL);
52e6a578e2SVinod Koul 	if (!pon->regmap) {
53e6a578e2SVinod Koul 		dev_err(&pdev->dev, "failed to locate regmap\n");
54e6a578e2SVinod Koul 		return -ENODEV;
55e6a578e2SVinod Koul 	}
56e6a578e2SVinod Koul 
57e6a578e2SVinod Koul 	error = of_property_read_u32(pdev->dev.of_node, "reg",
58e6a578e2SVinod Koul 				     &pon->baseaddr);
59e6a578e2SVinod Koul 	if (error)
60e6a578e2SVinod Koul 		return error;
61e6a578e2SVinod Koul 
62e6a578e2SVinod Koul 	pon->reboot_mode.dev = &pdev->dev;
63e6a578e2SVinod Koul 	pon->reboot_mode.write = pm8916_reboot_mode_write;
64e6a578e2SVinod Koul 	error = devm_reboot_mode_register(&pdev->dev, &pon->reboot_mode);
65e6a578e2SVinod Koul 	if (error) {
66e6a578e2SVinod Koul 		dev_err(&pdev->dev, "can't register reboot mode\n");
67e6a578e2SVinod Koul 		return error;
68e6a578e2SVinod Koul 	}
69e6a578e2SVinod Koul 
70e6a578e2SVinod Koul 	platform_set_drvdata(pdev, pon);
71e6a578e2SVinod Koul 
72e6a578e2SVinod Koul 	return devm_of_platform_populate(&pdev->dev);
73e6a578e2SVinod Koul }
74e6a578e2SVinod Koul 
75e6a578e2SVinod Koul static const struct of_device_id pm8916_pon_id_table[] = {
76e6a578e2SVinod Koul 	{ .compatible = "qcom,pm8916-pon" },
77e6a578e2SVinod Koul 	{ }
78e6a578e2SVinod Koul };
79e6a578e2SVinod Koul MODULE_DEVICE_TABLE(of, pm8916_pon_id_table);
80e6a578e2SVinod Koul 
81e6a578e2SVinod Koul static struct platform_driver pm8916_pon_driver = {
82e6a578e2SVinod Koul 	.probe = pm8916_pon_probe,
83e6a578e2SVinod Koul 	.driver = {
84e6a578e2SVinod Koul 		.name = "pm8916-pon",
85e6a578e2SVinod Koul 		.of_match_table = of_match_ptr(pm8916_pon_id_table),
86e6a578e2SVinod Koul 	},
87e6a578e2SVinod Koul };
88e6a578e2SVinod Koul module_platform_driver(pm8916_pon_driver);
89e6a578e2SVinod Koul 
90e6a578e2SVinod Koul MODULE_DESCRIPTION("pm8916 Power On driver");
91e6a578e2SVinod Koul MODULE_LICENSE("GPL v2");
92