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