blob: 04a601f6aebe500d5491acfbbb4c68d7b56b9a73 [file] [log] [blame]
Flora Fu6df8dd52015-02-22 13:15:29 +01001/*
2 * Copyright (c) 2014 MediaTek Inc.
3 * Author: Flora Fu, MediaTek
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/of_device.h>
18#include <linux/of_irq.h>
19#include <linux/regmap.h>
20#include <linux/mfd/core.h>
21#include <linux/mfd/mt6397/core.h>
John Crispin44760cf2016-01-27 12:47:38 +010022#include <linux/mfd/mt6323/core.h>
Flora Fu6df8dd52015-02-22 13:15:29 +010023#include <linux/mfd/mt6397/registers.h>
John Crispin44760cf2016-01-27 12:47:38 +010024#include <linux/mfd/mt6323/registers.h>
Flora Fu6df8dd52015-02-22 13:15:29 +010025
Eddie Huanga5d7ea02015-05-06 15:23:40 +080026#define MT6397_RTC_BASE 0xe000
27#define MT6397_RTC_SIZE 0x3e
28
John Crispin44760cf2016-01-27 12:47:38 +010029#define MT6323_CID_CODE 0x23
John Crispin1d2c25e2016-01-27 12:47:37 +010030#define MT6391_CID_CODE 0x91
31#define MT6397_CID_CODE 0x97
32
Eddie Huanga5d7ea02015-05-06 15:23:40 +080033static const struct resource mt6397_rtc_resources[] = {
34 {
35 .start = MT6397_RTC_BASE,
36 .end = MT6397_RTC_BASE + MT6397_RTC_SIZE,
37 .flags = IORESOURCE_MEM,
38 },
39 {
40 .start = MT6397_IRQ_RTC,
41 .end = MT6397_IRQ_RTC,
42 .flags = IORESOURCE_IRQ,
43 },
44};
45
John Crispin44760cf2016-01-27 12:47:38 +010046static const struct mfd_cell mt6323_devs[] = {
47 {
48 .name = "mt6323-regulator",
49 .of_compatible = "mediatek,mt6323-regulator"
Sean Wang040fc9b2017-03-20 14:47:27 +080050 }, {
Sean Wang1cb8af82017-01-23 11:54:45 +080051 .name = "mt6323-led",
52 .of_compatible = "mediatek,mt6323-led"
53 },
John Crispin44760cf2016-01-27 12:47:38 +010054};
55
Flora Fu6df8dd52015-02-22 13:15:29 +010056static const struct mfd_cell mt6397_devs[] = {
57 {
58 .name = "mt6397-rtc",
Eddie Huanga5d7ea02015-05-06 15:23:40 +080059 .num_resources = ARRAY_SIZE(mt6397_rtc_resources),
60 .resources = mt6397_rtc_resources,
Flora Fu6df8dd52015-02-22 13:15:29 +010061 .of_compatible = "mediatek,mt6397-rtc",
62 }, {
63 .name = "mt6397-regulator",
64 .of_compatible = "mediatek,mt6397-regulator",
65 }, {
66 .name = "mt6397-codec",
67 .of_compatible = "mediatek,mt6397-codec",
68 }, {
69 .name = "mt6397-clk",
70 .of_compatible = "mediatek,mt6397-clk",
Hongzhou Yangcf550782015-05-27 02:10:35 -070071 }, {
72 .name = "mt6397-pinctrl",
73 .of_compatible = "mediatek,mt6397-pinctrl",
Flora Fu6df8dd52015-02-22 13:15:29 +010074 },
75};
76
77static void mt6397_irq_lock(struct irq_data *data)
78{
Jiang Liu1e84aa42015-07-13 20:44:56 +000079 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010080
81 mutex_lock(&mt6397->irqlock);
82}
83
84static void mt6397_irq_sync_unlock(struct irq_data *data)
85{
Jiang Liu1e84aa42015-07-13 20:44:56 +000086 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010087
John Crispinfeec4792016-01-27 12:47:36 +010088 regmap_write(mt6397->regmap, mt6397->int_con[0],
89 mt6397->irq_masks_cur[0]);
90 regmap_write(mt6397->regmap, mt6397->int_con[1],
91 mt6397->irq_masks_cur[1]);
Flora Fu6df8dd52015-02-22 13:15:29 +010092
93 mutex_unlock(&mt6397->irqlock);
94}
95
96static void mt6397_irq_disable(struct irq_data *data)
97{
Jiang Liu1e84aa42015-07-13 20:44:56 +000098 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010099 int shift = data->hwirq & 0xf;
100 int reg = data->hwirq >> 4;
101
102 mt6397->irq_masks_cur[reg] &= ~BIT(shift);
103}
104
105static void mt6397_irq_enable(struct irq_data *data)
106{
Jiang Liu1e84aa42015-07-13 20:44:56 +0000107 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +0100108 int shift = data->hwirq & 0xf;
109 int reg = data->hwirq >> 4;
110
111 mt6397->irq_masks_cur[reg] |= BIT(shift);
112}
113
Henry Chenf3151ab2015-08-10 21:10:45 +0800114#ifdef CONFIG_PM_SLEEP
115static int mt6397_irq_set_wake(struct irq_data *irq_data, unsigned int on)
116{
117 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(irq_data);
118 int shift = irq_data->hwirq & 0xf;
119 int reg = irq_data->hwirq >> 4;
120
121 if (on)
122 mt6397->wake_mask[reg] |= BIT(shift);
123 else
124 mt6397->wake_mask[reg] &= ~BIT(shift);
125
126 return 0;
127}
128#else
129#define mt6397_irq_set_wake NULL
130#endif
131
Flora Fu6df8dd52015-02-22 13:15:29 +0100132static struct irq_chip mt6397_irq_chip = {
133 .name = "mt6397-irq",
134 .irq_bus_lock = mt6397_irq_lock,
135 .irq_bus_sync_unlock = mt6397_irq_sync_unlock,
136 .irq_enable = mt6397_irq_enable,
137 .irq_disable = mt6397_irq_disable,
Henry Chenf3151ab2015-08-10 21:10:45 +0800138 .irq_set_wake = mt6397_irq_set_wake,
Flora Fu6df8dd52015-02-22 13:15:29 +0100139};
140
141static void mt6397_irq_handle_reg(struct mt6397_chip *mt6397, int reg,
142 int irqbase)
143{
144 unsigned int status;
145 int i, irq, ret;
146
147 ret = regmap_read(mt6397->regmap, reg, &status);
148 if (ret) {
149 dev_err(mt6397->dev, "Failed to read irq status: %d\n", ret);
150 return;
151 }
152
153 for (i = 0; i < 16; i++) {
154 if (status & BIT(i)) {
155 irq = irq_find_mapping(mt6397->irq_domain, irqbase + i);
156 if (irq)
157 handle_nested_irq(irq);
158 }
159 }
160
161 regmap_write(mt6397->regmap, reg, status);
162}
163
164static irqreturn_t mt6397_irq_thread(int irq, void *data)
165{
166 struct mt6397_chip *mt6397 = data;
167
John Crispinfeec4792016-01-27 12:47:36 +0100168 mt6397_irq_handle_reg(mt6397, mt6397->int_status[0], 0);
169 mt6397_irq_handle_reg(mt6397, mt6397->int_status[1], 16);
Flora Fu6df8dd52015-02-22 13:15:29 +0100170
171 return IRQ_HANDLED;
172}
173
174static int mt6397_irq_domain_map(struct irq_domain *d, unsigned int irq,
175 irq_hw_number_t hw)
176{
177 struct mt6397_chip *mt6397 = d->host_data;
178
179 irq_set_chip_data(irq, mt6397);
180 irq_set_chip_and_handler(irq, &mt6397_irq_chip, handle_level_irq);
181 irq_set_nested_thread(irq, 1);
Flora Fu6df8dd52015-02-22 13:15:29 +0100182 irq_set_noprobe(irq);
Flora Fu6df8dd52015-02-22 13:15:29 +0100183
184 return 0;
185}
186
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900187static const struct irq_domain_ops mt6397_irq_domain_ops = {
Flora Fu6df8dd52015-02-22 13:15:29 +0100188 .map = mt6397_irq_domain_map,
189};
190
191static int mt6397_irq_init(struct mt6397_chip *mt6397)
192{
193 int ret;
194
195 mutex_init(&mt6397->irqlock);
196
197 /* Mask all interrupt sources */
John Crispinfeec4792016-01-27 12:47:36 +0100198 regmap_write(mt6397->regmap, mt6397->int_con[0], 0x0);
199 regmap_write(mt6397->regmap, mt6397->int_con[1], 0x0);
Flora Fu6df8dd52015-02-22 13:15:29 +0100200
201 mt6397->irq_domain = irq_domain_add_linear(mt6397->dev->of_node,
202 MT6397_IRQ_NR, &mt6397_irq_domain_ops, mt6397);
203 if (!mt6397->irq_domain) {
204 dev_err(mt6397->dev, "could not create irq domain\n");
205 return -ENOMEM;
206 }
207
208 ret = devm_request_threaded_irq(mt6397->dev, mt6397->irq, NULL,
209 mt6397_irq_thread, IRQF_ONESHOT, "mt6397-pmic", mt6397);
210 if (ret) {
211 dev_err(mt6397->dev, "failed to register irq=%d; err: %d\n",
212 mt6397->irq, ret);
213 return ret;
214 }
215
216 return 0;
217}
218
Henry Chenf3151ab2015-08-10 21:10:45 +0800219#ifdef CONFIG_PM_SLEEP
220static int mt6397_irq_suspend(struct device *dev)
221{
222 struct mt6397_chip *chip = dev_get_drvdata(dev);
223
John Crispinfeec4792016-01-27 12:47:36 +0100224 regmap_write(chip->regmap, chip->int_con[0], chip->wake_mask[0]);
225 regmap_write(chip->regmap, chip->int_con[1], chip->wake_mask[1]);
Henry Chenf3151ab2015-08-10 21:10:45 +0800226
227 enable_irq_wake(chip->irq);
228
229 return 0;
230}
231
232static int mt6397_irq_resume(struct device *dev)
233{
234 struct mt6397_chip *chip = dev_get_drvdata(dev);
235
John Crispinfeec4792016-01-27 12:47:36 +0100236 regmap_write(chip->regmap, chip->int_con[0], chip->irq_masks_cur[0]);
237 regmap_write(chip->regmap, chip->int_con[1], chip->irq_masks_cur[1]);
Henry Chenf3151ab2015-08-10 21:10:45 +0800238
239 disable_irq_wake(chip->irq);
240
241 return 0;
242}
243#endif
244
245static SIMPLE_DEV_PM_OPS(mt6397_pm_ops, mt6397_irq_suspend,
246 mt6397_irq_resume);
247
Flora Fu6df8dd52015-02-22 13:15:29 +0100248static int mt6397_probe(struct platform_device *pdev)
249{
250 int ret;
John Crispin1d2c25e2016-01-27 12:47:37 +0100251 unsigned int id;
252 struct mt6397_chip *pmic;
Flora Fu6df8dd52015-02-22 13:15:29 +0100253
John Crispin1d2c25e2016-01-27 12:47:37 +0100254 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
255 if (!pmic)
Flora Fu6df8dd52015-02-22 13:15:29 +0100256 return -ENOMEM;
257
John Crispin1d2c25e2016-01-27 12:47:37 +0100258 pmic->dev = &pdev->dev;
John Crispinfeec4792016-01-27 12:47:36 +0100259
Flora Fu6df8dd52015-02-22 13:15:29 +0100260 /*
261 * mt6397 MFD is child device of soc pmic wrapper.
262 * Regmap is set from its parent.
263 */
John Crispin1d2c25e2016-01-27 12:47:37 +0100264 pmic->regmap = dev_get_regmap(pdev->dev.parent, NULL);
265 if (!pmic->regmap)
Flora Fu6df8dd52015-02-22 13:15:29 +0100266 return -ENODEV;
267
John Crispin1d2c25e2016-01-27 12:47:37 +0100268 platform_set_drvdata(pdev, pmic);
Flora Fu6df8dd52015-02-22 13:15:29 +0100269
John Crispin1d2c25e2016-01-27 12:47:37 +0100270 ret = regmap_read(pmic->regmap, MT6397_CID, &id);
271 if (ret) {
272 dev_err(pmic->dev, "Failed to read chip id: %d\n", ret);
Henry Chen1387ff52016-04-15 16:30:29 +0800273 return ret;
John Crispin1d2c25e2016-01-27 12:47:37 +0100274 }
275
Henry Chen1387ff52016-04-15 16:30:29 +0800276 pmic->irq = platform_get_irq(pdev, 0);
277 if (pmic->irq <= 0)
278 return pmic->irq;
279
John Crispin1d2c25e2016-01-27 12:47:37 +0100280 switch (id & 0xff) {
John Crispin44760cf2016-01-27 12:47:38 +0100281 case MT6323_CID_CODE:
282 pmic->int_con[0] = MT6323_INT_CON0;
283 pmic->int_con[1] = MT6323_INT_CON1;
284 pmic->int_status[0] = MT6323_INT_STATUS0;
285 pmic->int_status[1] = MT6323_INT_STATUS1;
Henry Chen1387ff52016-04-15 16:30:29 +0800286 ret = mt6397_irq_init(pmic);
287 if (ret)
288 return ret;
289
Laxman Dewangan08e380a2016-04-08 00:13:04 +0530290 ret = devm_mfd_add_devices(&pdev->dev, -1, mt6323_devs,
291 ARRAY_SIZE(mt6323_devs), NULL,
292 0, NULL);
John Crispin44760cf2016-01-27 12:47:38 +0100293 break;
294
John Crispin1d2c25e2016-01-27 12:47:37 +0100295 case MT6397_CID_CODE:
296 case MT6391_CID_CODE:
297 pmic->int_con[0] = MT6397_INT_CON0;
298 pmic->int_con[1] = MT6397_INT_CON1;
299 pmic->int_status[0] = MT6397_INT_STATUS0;
300 pmic->int_status[1] = MT6397_INT_STATUS1;
Henry Chen1387ff52016-04-15 16:30:29 +0800301 ret = mt6397_irq_init(pmic);
302 if (ret)
303 return ret;
304
Laxman Dewangan08e380a2016-04-08 00:13:04 +0530305 ret = devm_mfd_add_devices(&pdev->dev, -1, mt6397_devs,
306 ARRAY_SIZE(mt6397_devs), NULL,
307 0, NULL);
John Crispin1d2c25e2016-01-27 12:47:37 +0100308 break;
309
310 default:
311 dev_err(&pdev->dev, "unsupported chip: %d\n", id);
312 ret = -ENODEV;
313 break;
314 }
315
John Crispin1d2c25e2016-01-27 12:47:37 +0100316 if (ret) {
317 irq_domain_remove(pmic->irq_domain);
Flora Fu6df8dd52015-02-22 13:15:29 +0100318 dev_err(&pdev->dev, "failed to add child devices: %d\n", ret);
John Crispin1d2c25e2016-01-27 12:47:37 +0100319 }
Flora Fu6df8dd52015-02-22 13:15:29 +0100320
321 return ret;
322}
323
Flora Fu6df8dd52015-02-22 13:15:29 +0100324static const struct of_device_id mt6397_of_match[] = {
325 { .compatible = "mediatek,mt6397" },
John Crispin44760cf2016-01-27 12:47:38 +0100326 { .compatible = "mediatek,mt6323" },
Flora Fu6df8dd52015-02-22 13:15:29 +0100327 { }
328};
329MODULE_DEVICE_TABLE(of, mt6397_of_match);
330
Javier Martinez Canillase1d9a102016-02-10 13:50:18 -0300331static const struct platform_device_id mt6397_id[] = {
332 { "mt6397", 0 },
333 { },
334};
335MODULE_DEVICE_TABLE(platform, mt6397_id);
336
Flora Fu6df8dd52015-02-22 13:15:29 +0100337static struct platform_driver mt6397_driver = {
338 .probe = mt6397_probe,
Flora Fu6df8dd52015-02-22 13:15:29 +0100339 .driver = {
340 .name = "mt6397",
341 .of_match_table = of_match_ptr(mt6397_of_match),
Henry Chenf3151ab2015-08-10 21:10:45 +0800342 .pm = &mt6397_pm_ops,
Flora Fu6df8dd52015-02-22 13:15:29 +0100343 },
Javier Martinez Canillase1d9a102016-02-10 13:50:18 -0300344 .id_table = mt6397_id,
Flora Fu6df8dd52015-02-22 13:15:29 +0100345};
346
347module_platform_driver(mt6397_driver);
348
349MODULE_AUTHOR("Flora Fu, MediaTek");
350MODULE_DESCRIPTION("Driver for MediaTek MT6397 PMIC");
351MODULE_LICENSE("GPL");