Loading...
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#define GEN1_REASON_SHIFT 2
18#define GEN2_REASON_SHIFT 1
19
20#define NO_REASON_SHIFT 0
21
22struct pm8916_pon {
23 struct device *dev;
24 struct regmap *regmap;
25 u32 baseaddr;
26 struct reboot_mode_driver reboot_mode;
27 long reason_shift;
28};
29
30static int pm8916_reboot_mode_write(struct reboot_mode_driver *reboot,
31 unsigned int magic)
32{
33 struct pm8916_pon *pon = container_of
34 (reboot, struct pm8916_pon, reboot_mode);
35 int ret;
36
37 ret = regmap_update_bits(pon->regmap,
38 pon->baseaddr + PON_SOFT_RB_SPARE,
39 GENMASK(7, pon->reason_shift),
40 magic << pon->reason_shift);
41 if (ret < 0)
42 dev_err(pon->dev, "update reboot mode bits failed\n");
43
44 return ret;
45}
46
47static int pm8916_pon_probe(struct platform_device *pdev)
48{
49 struct pm8916_pon *pon;
50 long reason_shift;
51 int error;
52
53 pon = devm_kzalloc(&pdev->dev, sizeof(*pon), GFP_KERNEL);
54 if (!pon)
55 return -ENOMEM;
56
57 pon->dev = &pdev->dev;
58
59 pon->regmap = dev_get_regmap(pdev->dev.parent, NULL);
60 if (!pon->regmap) {
61 dev_err(&pdev->dev, "failed to locate regmap\n");
62 return -ENODEV;
63 }
64
65 error = of_property_read_u32(pdev->dev.of_node, "reg",
66 &pon->baseaddr);
67 if (error)
68 return error;
69
70 reason_shift = (long)of_device_get_match_data(&pdev->dev);
71
72 if (reason_shift != NO_REASON_SHIFT) {
73 pon->reboot_mode.dev = &pdev->dev;
74 pon->reason_shift = reason_shift;
75 pon->reboot_mode.write = pm8916_reboot_mode_write;
76 error = devm_reboot_mode_register(&pdev->dev, &pon->reboot_mode);
77 if (error) {
78 dev_err(&pdev->dev, "can't register reboot mode\n");
79 return error;
80 }
81 }
82
83 platform_set_drvdata(pdev, pon);
84
85 return devm_of_platform_populate(&pdev->dev);
86}
87
88static const struct of_device_id pm8916_pon_id_table[] = {
89 { .compatible = "qcom,pm8916-pon", .data = (void *)GEN1_REASON_SHIFT },
90 { .compatible = "qcom,pm8941-pon", .data = (void *)NO_REASON_SHIFT },
91 { .compatible = "qcom,pms405-pon", .data = (void *)GEN1_REASON_SHIFT },
92 { .compatible = "qcom,pm8998-pon", .data = (void *)GEN2_REASON_SHIFT },
93 { .compatible = "qcom,pmk8350-pon", .data = (void *)GEN2_REASON_SHIFT },
94 { }
95};
96MODULE_DEVICE_TABLE(of, pm8916_pon_id_table);
97
98static struct platform_driver pm8916_pon_driver = {
99 .probe = pm8916_pon_probe,
100 .driver = {
101 .name = "pm8916-pon",
102 .of_match_table = pm8916_pon_id_table,
103 },
104};
105module_platform_driver(pm8916_pon_driver);
106
107MODULE_DESCRIPTION("pm8916 Power On driver");
108MODULE_LICENSE("GPL v2");
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#define GEN1_REASON_SHIFT 2
18#define GEN2_REASON_SHIFT 1
19
20struct pm8916_pon {
21 struct device *dev;
22 struct regmap *regmap;
23 u32 baseaddr;
24 struct reboot_mode_driver reboot_mode;
25 long reason_shift;
26};
27
28static int pm8916_reboot_mode_write(struct reboot_mode_driver *reboot,
29 unsigned int magic)
30{
31 struct pm8916_pon *pon = container_of
32 (reboot, struct pm8916_pon, reboot_mode);
33 int ret;
34
35 ret = regmap_update_bits(pon->regmap,
36 pon->baseaddr + PON_SOFT_RB_SPARE,
37 0xfc, magic << pon->reason_shift);
38 if (ret < 0)
39 dev_err(pon->dev, "update reboot mode bits failed\n");
40
41 return ret;
42}
43
44static int pm8916_pon_probe(struct platform_device *pdev)
45{
46 struct pm8916_pon *pon;
47 int error;
48
49 pon = devm_kzalloc(&pdev->dev, sizeof(*pon), GFP_KERNEL);
50 if (!pon)
51 return -ENOMEM;
52
53 pon->dev = &pdev->dev;
54
55 pon->regmap = dev_get_regmap(pdev->dev.parent, NULL);
56 if (!pon->regmap) {
57 dev_err(&pdev->dev, "failed to locate regmap\n");
58 return -ENODEV;
59 }
60
61 error = of_property_read_u32(pdev->dev.of_node, "reg",
62 &pon->baseaddr);
63 if (error)
64 return error;
65
66 pon->reboot_mode.dev = &pdev->dev;
67 pon->reason_shift = (long)of_device_get_match_data(&pdev->dev);
68 pon->reboot_mode.write = pm8916_reboot_mode_write;
69 error = devm_reboot_mode_register(&pdev->dev, &pon->reboot_mode);
70 if (error) {
71 dev_err(&pdev->dev, "can't register reboot mode\n");
72 return error;
73 }
74
75 platform_set_drvdata(pdev, pon);
76
77 return devm_of_platform_populate(&pdev->dev);
78}
79
80static const struct of_device_id pm8916_pon_id_table[] = {
81 { .compatible = "qcom,pm8916-pon", .data = (void *)GEN1_REASON_SHIFT },
82 { .compatible = "qcom,pms405-pon", .data = (void *)GEN1_REASON_SHIFT },
83 { .compatible = "qcom,pm8998-pon", .data = (void *)GEN2_REASON_SHIFT },
84 { }
85};
86MODULE_DEVICE_TABLE(of, pm8916_pon_id_table);
87
88static struct platform_driver pm8916_pon_driver = {
89 .probe = pm8916_pon_probe,
90 .driver = {
91 .name = "pm8916-pon",
92 .of_match_table = of_match_ptr(pm8916_pon_id_table),
93 },
94};
95module_platform_driver(pm8916_pon_driver);
96
97MODULE_DESCRIPTION("pm8916 Power On driver");
98MODULE_LICENSE("GPL v2");