]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mfd/mt6397-core.c
mfd: provide RTC resource in MT6397 MFD
[karo-tx-linux.git] / drivers / mfd / mt6397-core.c
1 /*
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
24 #define MT6397_RTC_BASE         0xe000
25 #define MT6397_RTC_SIZE         0x3e
26
27 static 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
40 static const struct mfd_cell mt6397_devs[] = {
41         {
42                 .name = "mt6397-rtc",
43                 .num_resources = ARRAY_SIZE(mt6397_rtc_resources),
44                 .resources = mt6397_rtc_resources,
45                 .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",
55         },
56 };
57
58 static void mt6397_irq_lock(struct irq_data *data)
59 {
60         struct mt6397_chip *mt6397 = irq_get_chip_data(data->irq);
61
62         mutex_lock(&mt6397->irqlock);
63 }
64
65 static void mt6397_irq_sync_unlock(struct irq_data *data)
66 {
67         struct mt6397_chip *mt6397 = irq_get_chip_data(data->irq);
68
69         regmap_write(mt6397->regmap, MT6397_INT_CON0, mt6397->irq_masks_cur[0]);
70         regmap_write(mt6397->regmap, MT6397_INT_CON1, mt6397->irq_masks_cur[1]);
71
72         mutex_unlock(&mt6397->irqlock);
73 }
74
75 static void mt6397_irq_disable(struct irq_data *data)
76 {
77         struct mt6397_chip *mt6397 = irq_get_chip_data(data->irq);
78         int shift = data->hwirq & 0xf;
79         int reg = data->hwirq >> 4;
80
81         mt6397->irq_masks_cur[reg] &= ~BIT(shift);
82 }
83
84 static void mt6397_irq_enable(struct irq_data *data)
85 {
86         struct mt6397_chip *mt6397 = irq_get_chip_data(data->irq);
87         int shift = data->hwirq & 0xf;
88         int reg = data->hwirq >> 4;
89
90         mt6397->irq_masks_cur[reg] |= BIT(shift);
91 }
92
93 static struct irq_chip mt6397_irq_chip = {
94         .name = "mt6397-irq",
95         .irq_bus_lock = mt6397_irq_lock,
96         .irq_bus_sync_unlock = mt6397_irq_sync_unlock,
97         .irq_enable = mt6397_irq_enable,
98         .irq_disable = mt6397_irq_disable,
99 };
100
101 static void mt6397_irq_handle_reg(struct mt6397_chip *mt6397, int reg,
102                 int irqbase)
103 {
104         unsigned int status;
105         int i, irq, ret;
106
107         ret = regmap_read(mt6397->regmap, reg, &status);
108         if (ret) {
109                 dev_err(mt6397->dev, "Failed to read irq status: %d\n", ret);
110                 return;
111         }
112
113         for (i = 0; i < 16; i++) {
114                 if (status & BIT(i)) {
115                         irq = irq_find_mapping(mt6397->irq_domain, irqbase + i);
116                         if (irq)
117                                 handle_nested_irq(irq);
118                 }
119         }
120
121         regmap_write(mt6397->regmap, reg, status);
122 }
123
124 static irqreturn_t mt6397_irq_thread(int irq, void *data)
125 {
126         struct mt6397_chip *mt6397 = data;
127
128         mt6397_irq_handle_reg(mt6397, MT6397_INT_STATUS0, 0);
129         mt6397_irq_handle_reg(mt6397, MT6397_INT_STATUS1, 16);
130
131         return IRQ_HANDLED;
132 }
133
134 static int mt6397_irq_domain_map(struct irq_domain *d, unsigned int irq,
135                                         irq_hw_number_t hw)
136 {
137         struct mt6397_chip *mt6397 = d->host_data;
138
139         irq_set_chip_data(irq, mt6397);
140         irq_set_chip_and_handler(irq, &mt6397_irq_chip, handle_level_irq);
141         irq_set_nested_thread(irq, 1);
142 #ifdef CONFIG_ARM
143         set_irq_flags(irq, IRQF_VALID);
144 #else
145         irq_set_noprobe(irq);
146 #endif
147
148         return 0;
149 }
150
151 static struct irq_domain_ops mt6397_irq_domain_ops = {
152         .map = mt6397_irq_domain_map,
153 };
154
155 static int mt6397_irq_init(struct mt6397_chip *mt6397)
156 {
157         int ret;
158
159         mutex_init(&mt6397->irqlock);
160
161         /* Mask all interrupt sources */
162         regmap_write(mt6397->regmap, MT6397_INT_CON0, 0x0);
163         regmap_write(mt6397->regmap, MT6397_INT_CON1, 0x0);
164
165         mt6397->irq_domain = irq_domain_add_linear(mt6397->dev->of_node,
166                 MT6397_IRQ_NR, &mt6397_irq_domain_ops, mt6397);
167         if (!mt6397->irq_domain) {
168                 dev_err(mt6397->dev, "could not create irq domain\n");
169                 return -ENOMEM;
170         }
171
172         ret = devm_request_threaded_irq(mt6397->dev, mt6397->irq, NULL,
173                 mt6397_irq_thread, IRQF_ONESHOT, "mt6397-pmic", mt6397);
174         if (ret) {
175                 dev_err(mt6397->dev, "failed to register irq=%d; err: %d\n",
176                         mt6397->irq, ret);
177                 return ret;
178         }
179
180         return 0;
181 }
182
183 static int mt6397_probe(struct platform_device *pdev)
184 {
185         int ret;
186         struct mt6397_chip *mt6397;
187
188         mt6397 = devm_kzalloc(&pdev->dev, sizeof(*mt6397), GFP_KERNEL);
189         if (!mt6397)
190                 return -ENOMEM;
191
192         mt6397->dev = &pdev->dev;
193         /*
194          * mt6397 MFD is child device of soc pmic wrapper.
195          * Regmap is set from its parent.
196          */
197         mt6397->regmap = dev_get_regmap(pdev->dev.parent, NULL);
198         if (!mt6397->regmap)
199                 return -ENODEV;
200
201         platform_set_drvdata(pdev, mt6397);
202
203         mt6397->irq = platform_get_irq(pdev, 0);
204         if (mt6397->irq > 0) {
205                 ret = mt6397_irq_init(mt6397);
206                 if (ret)
207                         return ret;
208         }
209
210         ret = mfd_add_devices(&pdev->dev, -1, mt6397_devs,
211                         ARRAY_SIZE(mt6397_devs), NULL, 0, NULL);
212         if (ret)
213                 dev_err(&pdev->dev, "failed to add child devices: %d\n", ret);
214
215         return ret;
216 }
217
218 static int mt6397_remove(struct platform_device *pdev)
219 {
220         mfd_remove_devices(&pdev->dev);
221
222         return 0;
223 }
224
225 static const struct of_device_id mt6397_of_match[] = {
226         { .compatible = "mediatek,mt6397" },
227         { }
228 };
229 MODULE_DEVICE_TABLE(of, mt6397_of_match);
230
231 static struct platform_driver mt6397_driver = {
232         .probe = mt6397_probe,
233         .remove = mt6397_remove,
234         .driver = {
235                 .name = "mt6397",
236                 .of_match_table = of_match_ptr(mt6397_of_match),
237         },
238 };
239
240 module_platform_driver(mt6397_driver);
241
242 MODULE_AUTHOR("Flora Fu, MediaTek");
243 MODULE_DESCRIPTION("Driver for MediaTek MT6397 PMIC");
244 MODULE_LICENSE("GPL");
245 MODULE_ALIAS("platform:mt6397");