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