blob: 0532803e6cbc40225f8151d1716279d899d166a5 [file] [log] [blame]
Josef Friedld28c74c2019-09-10 09:04:44 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Power off through MediaTek PMIC
4 *
5 * Copyright (C) 2018 MediaTek Inc.
6 *
7 * Author: Sean Wang <sean.wang@mediatek.com>
8 *
9 */
10
11#include <linux/err.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/mfd/mt6397/core.h>
16#include <linux/mfd/mt6397/rtc.h>
17
18struct mt6323_pwrc {
19 struct device *dev;
20 struct regmap *regmap;
21 u32 base;
22};
23
24static struct mt6323_pwrc *mt_pwrc;
25
26static void mt6323_do_pwroff(void)
27{
28 struct mt6323_pwrc *pwrc = mt_pwrc;
29 unsigned int val;
30 int ret;
31
32 regmap_write(pwrc->regmap, pwrc->base + RTC_BBPU, RTC_BBPU_KEY);
Ran Bi29ee4002020-04-21 11:00:11 +080033 regmap_write(pwrc->regmap, pwrc->base + RTC_WRTGR_MT6323, 1);
Josef Friedld28c74c2019-09-10 09:04:44 +020034
35 ret = regmap_read_poll_timeout(pwrc->regmap,
36 pwrc->base + RTC_BBPU, val,
37 !(val & RTC_BBPU_CBUSY),
38 MTK_RTC_POLL_DELAY_US,
39 MTK_RTC_POLL_TIMEOUT);
40 if (ret)
41 dev_err(pwrc->dev, "failed to write BBPU: %d\n", ret);
42
43 /* Wait some time until system down, otherwise, notice with a warn */
44 mdelay(1000);
45
46 WARN_ONCE(1, "Unable to power off system\n");
47}
48
49static int mt6323_pwrc_probe(struct platform_device *pdev)
50{
51 struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent);
52 struct mt6323_pwrc *pwrc;
53 struct resource *res;
54
55 pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
56 if (!pwrc)
57 return -ENOMEM;
58
59 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
60 pwrc->base = res->start;
61 pwrc->regmap = mt6397_chip->regmap;
62 pwrc->dev = &pdev->dev;
63 mt_pwrc = pwrc;
64
65 pm_power_off = &mt6323_do_pwroff;
66
67 return 0;
68}
69
70static int mt6323_pwrc_remove(struct platform_device *pdev)
71{
72 if (pm_power_off == &mt6323_do_pwroff)
73 pm_power_off = NULL;
74
75 return 0;
76}
77
78static const struct of_device_id mt6323_pwrc_dt_match[] = {
79 { .compatible = "mediatek,mt6323-pwrc" },
80 {},
81};
82MODULE_DEVICE_TABLE(of, mt6323_pwrc_dt_match);
83
84static struct platform_driver mt6323_pwrc_driver = {
85 .probe = mt6323_pwrc_probe,
86 .remove = mt6323_pwrc_remove,
87 .driver = {
88 .name = "mt6323-pwrc",
89 .of_match_table = mt6323_pwrc_dt_match,
90 },
91};
92
93module_platform_driver(mt6323_pwrc_driver);
94
95MODULE_DESCRIPTION("Poweroff driver for MT6323 PMIC");
96MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
97MODULE_LICENSE("GPL v2");