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