blob: 9ca290fec003f95f4d1977f44a4c759554f5346b [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>
22#include <linux/mfd/mt6397/registers.h>
23
Eddie Huanga5d7ea02015-05-06 15:23:40 +080024#define MT6397_RTC_BASE 0xe000
25#define MT6397_RTC_SIZE 0x3e
26
27static const struct resource mt6397_rtc_resources[] = {
28 {
29 .start = MT6397_RTC_BASE,
30 .end = MT6397_RTC_BASE + MT6397_RTC_SIZE,
31 .flags = IORESOURCE_MEM,
32 },
33 {
34 .start = MT6397_IRQ_RTC,
35 .end = MT6397_IRQ_RTC,
36 .flags = IORESOURCE_IRQ,
37 },
38};
39
Flora Fu6df8dd52015-02-22 13:15:29 +010040static const struct mfd_cell mt6397_devs[] = {
41 {
42 .name = "mt6397-rtc",
Eddie Huanga5d7ea02015-05-06 15:23:40 +080043 .num_resources = ARRAY_SIZE(mt6397_rtc_resources),
44 .resources = mt6397_rtc_resources,
Flora Fu6df8dd52015-02-22 13:15:29 +010045 .of_compatible = "mediatek,mt6397-rtc",
46 }, {
47 .name = "mt6397-regulator",
48 .of_compatible = "mediatek,mt6397-regulator",
49 }, {
50 .name = "mt6397-codec",
51 .of_compatible = "mediatek,mt6397-codec",
52 }, {
53 .name = "mt6397-clk",
54 .of_compatible = "mediatek,mt6397-clk",
Hongzhou Yangcf550782015-05-27 02:10:35 -070055 }, {
56 .name = "mt6397-pinctrl",
57 .of_compatible = "mediatek,mt6397-pinctrl",
Flora Fu6df8dd52015-02-22 13:15:29 +010058 },
59};
60
61static void mt6397_irq_lock(struct irq_data *data)
62{
Jiang Liu1e84aa42015-07-13 20:44:56 +000063 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010064
65 mutex_lock(&mt6397->irqlock);
66}
67
68static void mt6397_irq_sync_unlock(struct irq_data *data)
69{
Jiang Liu1e84aa42015-07-13 20:44:56 +000070 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010071
72 regmap_write(mt6397->regmap, MT6397_INT_CON0, mt6397->irq_masks_cur[0]);
73 regmap_write(mt6397->regmap, MT6397_INT_CON1, mt6397->irq_masks_cur[1]);
74
75 mutex_unlock(&mt6397->irqlock);
76}
77
78static void mt6397_irq_disable(struct irq_data *data)
79{
Jiang Liu1e84aa42015-07-13 20:44:56 +000080 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010081 int shift = data->hwirq & 0xf;
82 int reg = data->hwirq >> 4;
83
84 mt6397->irq_masks_cur[reg] &= ~BIT(shift);
85}
86
87static void mt6397_irq_enable(struct irq_data *data)
88{
Jiang Liu1e84aa42015-07-13 20:44:56 +000089 struct mt6397_chip *mt6397 = irq_data_get_irq_chip_data(data);
Flora Fu6df8dd52015-02-22 13:15:29 +010090 int shift = data->hwirq & 0xf;
91 int reg = data->hwirq >> 4;
92
93 mt6397->irq_masks_cur[reg] |= BIT(shift);
94}
95
96static struct irq_chip mt6397_irq_chip = {
97 .name = "mt6397-irq",
98 .irq_bus_lock = mt6397_irq_lock,
99 .irq_bus_sync_unlock = mt6397_irq_sync_unlock,
100 .irq_enable = mt6397_irq_enable,
101 .irq_disable = mt6397_irq_disable,
102};
103
104static void mt6397_irq_handle_reg(struct mt6397_chip *mt6397, int reg,
105 int irqbase)
106{
107 unsigned int status;
108 int i, irq, ret;
109
110 ret = regmap_read(mt6397->regmap, reg, &status);
111 if (ret) {
112 dev_err(mt6397->dev, "Failed to read irq status: %d\n", ret);
113 return;
114 }
115
116 for (i = 0; i < 16; i++) {
117 if (status & BIT(i)) {
118 irq = irq_find_mapping(mt6397->irq_domain, irqbase + i);
119 if (irq)
120 handle_nested_irq(irq);
121 }
122 }
123
124 regmap_write(mt6397->regmap, reg, status);
125}
126
127static irqreturn_t mt6397_irq_thread(int irq, void *data)
128{
129 struct mt6397_chip *mt6397 = data;
130
131 mt6397_irq_handle_reg(mt6397, MT6397_INT_STATUS0, 0);
132 mt6397_irq_handle_reg(mt6397, MT6397_INT_STATUS1, 16);
133
134 return IRQ_HANDLED;
135}
136
137static int mt6397_irq_domain_map(struct irq_domain *d, unsigned int irq,
138 irq_hw_number_t hw)
139{
140 struct mt6397_chip *mt6397 = d->host_data;
141
142 irq_set_chip_data(irq, mt6397);
143 irq_set_chip_and_handler(irq, &mt6397_irq_chip, handle_level_irq);
144 irq_set_nested_thread(irq, 1);
Flora Fu6df8dd52015-02-22 13:15:29 +0100145 irq_set_noprobe(irq);
Flora Fu6df8dd52015-02-22 13:15:29 +0100146
147 return 0;
148}
149
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900150static const struct irq_domain_ops mt6397_irq_domain_ops = {
Flora Fu6df8dd52015-02-22 13:15:29 +0100151 .map = mt6397_irq_domain_map,
152};
153
154static int mt6397_irq_init(struct mt6397_chip *mt6397)
155{
156 int ret;
157
158 mutex_init(&mt6397->irqlock);
159
160 /* Mask all interrupt sources */
161 regmap_write(mt6397->regmap, MT6397_INT_CON0, 0x0);
162 regmap_write(mt6397->regmap, MT6397_INT_CON1, 0x0);
163
164 mt6397->irq_domain = irq_domain_add_linear(mt6397->dev->of_node,
165 MT6397_IRQ_NR, &mt6397_irq_domain_ops, mt6397);
166 if (!mt6397->irq_domain) {
167 dev_err(mt6397->dev, "could not create irq domain\n");
168 return -ENOMEM;
169 }
170
171 ret = devm_request_threaded_irq(mt6397->dev, mt6397->irq, NULL,
172 mt6397_irq_thread, IRQF_ONESHOT, "mt6397-pmic", mt6397);
173 if (ret) {
174 dev_err(mt6397->dev, "failed to register irq=%d; err: %d\n",
175 mt6397->irq, ret);
176 return ret;
177 }
178
179 return 0;
180}
181
182static int mt6397_probe(struct platform_device *pdev)
183{
184 int ret;
185 struct mt6397_chip *mt6397;
186
187 mt6397 = devm_kzalloc(&pdev->dev, sizeof(*mt6397), GFP_KERNEL);
188 if (!mt6397)
189 return -ENOMEM;
190
191 mt6397->dev = &pdev->dev;
192 /*
193 * mt6397 MFD is child device of soc pmic wrapper.
194 * Regmap is set from its parent.
195 */
196 mt6397->regmap = dev_get_regmap(pdev->dev.parent, NULL);
197 if (!mt6397->regmap)
198 return -ENODEV;
199
200 platform_set_drvdata(pdev, mt6397);
201
202 mt6397->irq = platform_get_irq(pdev, 0);
203 if (mt6397->irq > 0) {
204 ret = mt6397_irq_init(mt6397);
205 if (ret)
206 return ret;
207 }
208
209 ret = mfd_add_devices(&pdev->dev, -1, mt6397_devs,
210 ARRAY_SIZE(mt6397_devs), NULL, 0, NULL);
211 if (ret)
212 dev_err(&pdev->dev, "failed to add child devices: %d\n", ret);
213
214 return ret;
215}
216
217static int mt6397_remove(struct platform_device *pdev)
218{
219 mfd_remove_devices(&pdev->dev);
220
221 return 0;
222}
223
224static const struct of_device_id mt6397_of_match[] = {
225 { .compatible = "mediatek,mt6397" },
226 { }
227};
228MODULE_DEVICE_TABLE(of, mt6397_of_match);
229
230static struct platform_driver mt6397_driver = {
231 .probe = mt6397_probe,
232 .remove = mt6397_remove,
233 .driver = {
234 .name = "mt6397",
235 .of_match_table = of_match_ptr(mt6397_of_match),
236 },
237};
238
239module_platform_driver(mt6397_driver);
240
241MODULE_AUTHOR("Flora Fu, MediaTek");
242MODULE_DESCRIPTION("Driver for MediaTek MT6397 PMIC");
243MODULE_LICENSE("GPL");
244MODULE_ALIAS("platform:mt6397");