xref: /openbmc/linux/drivers/mfd/mt6397-core.c (revision b9df3997)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 MediaTek Inc.
4  * Author: Flora Fu, MediaTek
5  */
6 
7 #include <linux/interrupt.h>
8 #include <linux/ioport.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/of_irq.h>
12 #include <linux/regmap.h>
13 #include <linux/mfd/core.h>
14 #include <linux/mfd/mt6323/core.h>
15 #include <linux/mfd/mt6397/core.h>
16 #include <linux/mfd/mt6323/registers.h>
17 #include <linux/mfd/mt6397/registers.h>
18 
19 #define MT6323_RTC_BASE		0x8000
20 #define MT6323_RTC_SIZE		0x40
21 
22 #define MT6397_RTC_BASE		0xe000
23 #define MT6397_RTC_SIZE		0x3e
24 
25 #define MT6323_PWRC_BASE	0x8000
26 #define MT6323_PWRC_SIZE	0x40
27 
28 static const struct resource mt6323_rtc_resources[] = {
29 	DEFINE_RES_MEM(MT6323_RTC_BASE, MT6323_RTC_SIZE),
30 	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_RTC),
31 };
32 
33 static const struct resource mt6397_rtc_resources[] = {
34 	DEFINE_RES_MEM(MT6397_RTC_BASE, MT6397_RTC_SIZE),
35 	DEFINE_RES_IRQ(MT6397_IRQ_RTC),
36 };
37 
38 static const struct resource mt6323_keys_resources[] = {
39 	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_PWRKEY),
40 	DEFINE_RES_IRQ(MT6323_IRQ_STATUS_FCHRKEY),
41 };
42 
43 static const struct resource mt6397_keys_resources[] = {
44 	DEFINE_RES_IRQ(MT6397_IRQ_PWRKEY),
45 	DEFINE_RES_IRQ(MT6397_IRQ_HOMEKEY),
46 };
47 
48 static const struct resource mt6323_pwrc_resources[] = {
49 	DEFINE_RES_MEM(MT6323_PWRC_BASE, MT6323_PWRC_SIZE),
50 };
51 
52 static const struct mfd_cell mt6323_devs[] = {
53 	{
54 		.name = "mt6323-rtc",
55 		.num_resources = ARRAY_SIZE(mt6323_rtc_resources),
56 		.resources = mt6323_rtc_resources,
57 		.of_compatible = "mediatek,mt6323-rtc",
58 	}, {
59 		.name = "mt6323-regulator",
60 		.of_compatible = "mediatek,mt6323-regulator"
61 	}, {
62 		.name = "mt6323-led",
63 		.of_compatible = "mediatek,mt6323-led"
64 	}, {
65 		.name = "mtk-pmic-keys",
66 		.num_resources = ARRAY_SIZE(mt6323_keys_resources),
67 		.resources = mt6323_keys_resources,
68 		.of_compatible = "mediatek,mt6323-keys"
69 	}, {
70 		.name = "mt6323-pwrc",
71 		.num_resources = ARRAY_SIZE(mt6323_pwrc_resources),
72 		.resources = mt6323_pwrc_resources,
73 		.of_compatible = "mediatek,mt6323-pwrc"
74 	},
75 };
76 
77 static const struct mfd_cell mt6397_devs[] = {
78 	{
79 		.name = "mt6397-rtc",
80 		.num_resources = ARRAY_SIZE(mt6397_rtc_resources),
81 		.resources = mt6397_rtc_resources,
82 		.of_compatible = "mediatek,mt6397-rtc",
83 	}, {
84 		.name = "mt6397-regulator",
85 		.of_compatible = "mediatek,mt6397-regulator",
86 	}, {
87 		.name = "mt6397-codec",
88 		.of_compatible = "mediatek,mt6397-codec",
89 	}, {
90 		.name = "mt6397-clk",
91 		.of_compatible = "mediatek,mt6397-clk",
92 	}, {
93 		.name = "mt6397-pinctrl",
94 		.of_compatible = "mediatek,mt6397-pinctrl",
95 	}, {
96 		.name = "mtk-pmic-keys",
97 		.num_resources = ARRAY_SIZE(mt6397_keys_resources),
98 		.resources = mt6397_keys_resources,
99 		.of_compatible = "mediatek,mt6397-keys"
100 	}
101 };
102 
103 #ifdef CONFIG_PM_SLEEP
104 static int mt6397_irq_suspend(struct device *dev)
105 {
106 	struct mt6397_chip *chip = dev_get_drvdata(dev);
107 
108 	regmap_write(chip->regmap, chip->int_con[0], chip->wake_mask[0]);
109 	regmap_write(chip->regmap, chip->int_con[1], chip->wake_mask[1]);
110 
111 	enable_irq_wake(chip->irq);
112 
113 	return 0;
114 }
115 
116 static int mt6397_irq_resume(struct device *dev)
117 {
118 	struct mt6397_chip *chip = dev_get_drvdata(dev);
119 
120 	regmap_write(chip->regmap, chip->int_con[0], chip->irq_masks_cur[0]);
121 	regmap_write(chip->regmap, chip->int_con[1], chip->irq_masks_cur[1]);
122 
123 	disable_irq_wake(chip->irq);
124 
125 	return 0;
126 }
127 #endif
128 
129 static SIMPLE_DEV_PM_OPS(mt6397_pm_ops, mt6397_irq_suspend,
130 			mt6397_irq_resume);
131 
132 static int mt6397_probe(struct platform_device *pdev)
133 {
134 	int ret;
135 	unsigned int id;
136 	struct mt6397_chip *pmic;
137 
138 	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
139 	if (!pmic)
140 		return -ENOMEM;
141 
142 	pmic->dev = &pdev->dev;
143 
144 	/*
145 	 * mt6397 MFD is child device of soc pmic wrapper.
146 	 * Regmap is set from its parent.
147 	 */
148 	pmic->regmap = dev_get_regmap(pdev->dev.parent, NULL);
149 	if (!pmic->regmap)
150 		return -ENODEV;
151 
152 	platform_set_drvdata(pdev, pmic);
153 
154 	ret = regmap_read(pmic->regmap, MT6397_CID, &id);
155 	if (ret) {
156 		dev_err(pmic->dev, "Failed to read chip id: %d\n", ret);
157 		return ret;
158 	}
159 
160 	pmic->irq = platform_get_irq(pdev, 0);
161 	if (pmic->irq <= 0)
162 		return pmic->irq;
163 
164 	switch (id & 0xff) {
165 	case MT6323_CHIP_ID:
166 		pmic->int_con[0] = MT6323_INT_CON0;
167 		pmic->int_con[1] = MT6323_INT_CON1;
168 		pmic->int_status[0] = MT6323_INT_STATUS0;
169 		pmic->int_status[1] = MT6323_INT_STATUS1;
170 		ret = mt6397_irq_init(pmic);
171 		if (ret)
172 			return ret;
173 
174 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6323_devs,
175 					   ARRAY_SIZE(mt6323_devs), NULL,
176 					   0, pmic->irq_domain);
177 		break;
178 
179 	case MT6391_CHIP_ID:
180 	case MT6397_CHIP_ID:
181 		pmic->int_con[0] = MT6397_INT_CON0;
182 		pmic->int_con[1] = MT6397_INT_CON1;
183 		pmic->int_status[0] = MT6397_INT_STATUS0;
184 		pmic->int_status[1] = MT6397_INT_STATUS1;
185 		ret = mt6397_irq_init(pmic);
186 		if (ret)
187 			return ret;
188 
189 		ret = devm_mfd_add_devices(&pdev->dev, -1, mt6397_devs,
190 					   ARRAY_SIZE(mt6397_devs), NULL,
191 					   0, pmic->irq_domain);
192 		break;
193 
194 	default:
195 		dev_err(&pdev->dev, "unsupported chip: %d\n", id);
196 		return -ENODEV;
197 	}
198 
199 	if (ret) {
200 		irq_domain_remove(pmic->irq_domain);
201 		dev_err(&pdev->dev, "failed to add child devices: %d\n", ret);
202 	}
203 
204 	return ret;
205 }
206 
207 static const struct of_device_id mt6397_of_match[] = {
208 	{ .compatible = "mediatek,mt6397" },
209 	{ .compatible = "mediatek,mt6323" },
210 	{ }
211 };
212 MODULE_DEVICE_TABLE(of, mt6397_of_match);
213 
214 static const struct platform_device_id mt6397_id[] = {
215 	{ "mt6397", 0 },
216 	{ },
217 };
218 MODULE_DEVICE_TABLE(platform, mt6397_id);
219 
220 static struct platform_driver mt6397_driver = {
221 	.probe = mt6397_probe,
222 	.driver = {
223 		.name = "mt6397",
224 		.of_match_table = of_match_ptr(mt6397_of_match),
225 		.pm = &mt6397_pm_ops,
226 	},
227 	.id_table = mt6397_id,
228 };
229 
230 module_platform_driver(mt6397_driver);
231 
232 MODULE_AUTHOR("Flora Fu, MediaTek");
233 MODULE_DESCRIPTION("Driver for MediaTek MT6397 PMIC");
234 MODULE_LICENSE("GPL");
235