]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/platform/x86/intel_pmic_gpio.c
platform-drivers: x86: pmic: Use irq_chip buslock mechanism
[mv-sheeva.git] / drivers / platform / x86 / intel_pmic_gpio.c
1 /* Moorestown PMIC GPIO (access through IPC) driver
2  * Copyright (c) 2008 - 2009, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18 /* Supports:
19  * Moorestown platform PMIC chip
20  */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/stddef.h>
27 #include <linux/slab.h>
28 #include <linux/ioport.h>
29 #include <linux/init.h>
30 #include <linux/io.h>
31 #include <linux/gpio.h>
32 #include <asm/intel_scu_ipc.h>
33 #include <linux/device.h>
34 #include <linux/intel_pmic_gpio.h>
35 #include <linux/platform_device.h>
36
37 #define DRIVER_NAME "pmic_gpio"
38
39 /* register offset that IPC driver should use
40  * 8 GPIO + 8 GPOSW (6 controllable) + 8GPO
41  */
42 enum pmic_gpio_register {
43         GPIO0           = 0xE0,
44         GPIO7           = 0xE7,
45         GPIOINT         = 0xE8,
46         GPOSWCTL0       = 0xEC,
47         GPOSWCTL5       = 0xF1,
48         GPO             = 0xF4,
49 };
50
51 /* bits definition for GPIO & GPOSW */
52 #define GPIO_DRV 0x01
53 #define GPIO_DIR 0x02
54 #define GPIO_DIN 0x04
55 #define GPIO_DOU 0x08
56 #define GPIO_INTCTL 0x30
57 #define GPIO_DBC 0xc0
58
59 #define GPOSW_DRV 0x01
60 #define GPOSW_DOU 0x08
61 #define GPOSW_RDRV 0x30
62
63 #define GPIO_UPDATE_TYPE        0x80000000
64
65 #define NUM_GPIO 24
66
67 struct pmic_gpio {
68         struct mutex            buslock;
69         struct gpio_chip        chip;
70         void                    *gpiointr;
71         int                     irq;
72         unsigned                irq_base;
73         unsigned int            update_type;
74         u32                     trigger_type;
75 };
76
77 static void pmic_program_irqtype(int gpio, int type)
78 {
79         if (type & IRQ_TYPE_EDGE_RISING)
80                 intel_scu_ipc_update_register(GPIO0 + gpio, 0x20, 0x20);
81         else
82                 intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x20);
83
84         if (type & IRQ_TYPE_EDGE_FALLING)
85                 intel_scu_ipc_update_register(GPIO0 + gpio, 0x10, 0x10);
86         else
87                 intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x10);
88 };
89
90 static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
91 {
92         if (offset > 8) {
93                 printk(KERN_ERR
94                         "%s: only pin 0-7 support input\n", __func__);
95                 return -1;/* we only have 8 GPIO can use as input */
96         }
97         return intel_scu_ipc_update_register(GPIO0 + offset,
98                                                         GPIO_DIR, GPIO_DIR);
99 }
100
101 static int pmic_gpio_direction_output(struct gpio_chip *chip,
102                         unsigned offset, int value)
103 {
104         int rc = 0;
105
106         if (offset < 8)/* it is GPIO */
107                 rc = intel_scu_ipc_update_register(GPIO0 + offset,
108                                 GPIO_DRV | (value ? GPIO_DOU : 0),
109                                 GPIO_DRV | GPIO_DOU | GPIO_DIR);
110         else if (offset < 16)/* it is GPOSW */
111                 rc = intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
112                                 GPOSW_DRV | (value ? GPOSW_DOU : 0),
113                                 GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
114         else if (offset > 15 && offset < 24)/* it is GPO */
115                 rc = intel_scu_ipc_update_register(GPO,
116                                 value ? 1 << (offset - 16) : 0,
117                                 1 << (offset - 16));
118         else {
119                 printk(KERN_ERR
120                         "%s: invalid PMIC GPIO pin %d!\n", __func__, offset);
121                 WARN_ON(1);
122         }
123
124         return rc;
125 }
126
127 static int pmic_gpio_get(struct gpio_chip *chip, unsigned offset)
128 {
129         u8 r;
130         int ret;
131
132         /* we only have 8 GPIO pins we can use as input */
133         if (offset > 8)
134                 return -EOPNOTSUPP;
135         ret = intel_scu_ipc_ioread8(GPIO0 + offset, &r);
136         if (ret < 0)
137                 return ret;
138         return r & GPIO_DIN;
139 }
140
141 static void pmic_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
142 {
143         if (offset < 8)/* it is GPIO */
144                 intel_scu_ipc_update_register(GPIO0 + offset,
145                         GPIO_DRV | (value ? GPIO_DOU : 0),
146                         GPIO_DRV | GPIO_DOU);
147         else if (offset < 16)/* it is GPOSW */
148                 intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
149                         GPOSW_DRV | (value ? GPOSW_DOU : 0),
150                         GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
151         else if (offset > 15 && offset < 24) /* it is GPO */
152                 intel_scu_ipc_update_register(GPO,
153                         value ? 1 << (offset - 16) : 0,
154                         1 << (offset - 16));
155 }
156
157 /*
158  * This is called from genirq with pg->buslock locked and
159  * irq_desc->lock held. We can not access the scu bus here, so we
160  * store the change and update in the bus_sync_unlock() function below
161  */
162 static int pmic_irq_type(struct irq_data *data, unsigned type)
163 {
164         struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
165         u32 gpio = data->irq - pg->irq_base;
166
167         if (gpio >= pg->chip.ngpio)
168                 return -EINVAL;
169
170         pg->trigger_type = type;
171         pg->update_type = gpio | GPIO_UPDATE_TYPE;
172         return 0;
173 }
174
175 static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
176 {
177         struct pmic_gpio *pg = container_of(chip, struct pmic_gpio, chip);
178
179         return pg->irq_base + offset;
180 }
181
182 static void pmic_bus_lock(struct irq_data *data)
183 {
184         struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
185
186         mutex_lock(&pg->buslock);
187 }
188
189 static void pmic_bus_sync_unlock(struct irq_data *data)
190 {
191         struct pmic_gpio *pg = irq_data_get_irq_chip_data(data);
192
193         if (pg->update_type) {
194                 unsigned int gpio = pg->update_type & ~GPIO_UPDATE_TYPE;
195
196                 pmic_program_irqtype(gpio, pg->trigger_type);
197                 pg->update_type = 0;
198         }
199         mutex_unlock(&pg->buslock);
200 }
201
202 /* the gpiointr register is read-clear, so just do nothing. */
203 static void pmic_irq_unmask(struct irq_data *data) { }
204
205 static void pmic_irq_mask(struct irq_data *data) { }
206
207 static struct irq_chip pmic_irqchip = {
208         .name           = "PMIC-GPIO",
209         .irq_mask       = pmic_irq_mask,
210         .irq_unmask     = pmic_irq_unmask,
211         .irq_set_type   = pmic_irq_type,
212 };
213
214 static void pmic_irq_handler(unsigned irq, struct irq_desc *desc)
215 {
216         struct pmic_gpio *pg = (struct pmic_gpio *)get_irq_data(irq);
217         u8 intsts = *((u8 *)pg->gpiointr + 4);
218         int gpio;
219
220         for (gpio = 0; gpio < 8; gpio++) {
221                 if (intsts & (1 << gpio)) {
222                         pr_debug("pmic pin %d triggered\n", gpio);
223                         generic_handle_irq(pg->irq_base + gpio);
224                 }
225         }
226         desc->chip->irq_eoi(get_irq_desc_chip_data(desc));
227 }
228
229 static int __devinit platform_pmic_gpio_probe(struct platform_device *pdev)
230 {
231         struct device *dev = &pdev->dev;
232         int irq = platform_get_irq(pdev, 0);
233         struct intel_pmic_gpio_platform_data *pdata = dev->platform_data;
234
235         struct pmic_gpio *pg;
236         int retval;
237         int i;
238
239         if (irq < 0) {
240                 dev_dbg(dev, "no IRQ line\n");
241                 return -EINVAL;
242         }
243
244         if (!pdata || !pdata->gpio_base || !pdata->irq_base) {
245                 dev_dbg(dev, "incorrect or missing platform data\n");
246                 return -EINVAL;
247         }
248
249         pg = kzalloc(sizeof(*pg), GFP_KERNEL);
250         if (!pg)
251                 return -ENOMEM;
252
253         dev_set_drvdata(dev, pg);
254
255         pg->irq = irq;
256         /* setting up SRAM mapping for GPIOINT register */
257         pg->gpiointr = ioremap_nocache(pdata->gpiointr, 8);
258         if (!pg->gpiointr) {
259                 printk(KERN_ERR "%s: Can not map GPIOINT.\n", __func__);
260                 retval = -EINVAL;
261                 goto err2;
262         }
263         pg->irq_base = pdata->irq_base;
264         pg->chip.label = "intel_pmic";
265         pg->chip.direction_input = pmic_gpio_direction_input;
266         pg->chip.direction_output = pmic_gpio_direction_output;
267         pg->chip.get = pmic_gpio_get;
268         pg->chip.set = pmic_gpio_set;
269         pg->chip.to_irq = pmic_gpio_to_irq;
270         pg->chip.base = pdata->gpio_base;
271         pg->chip.ngpio = NUM_GPIO;
272         pg->chip.can_sleep = 1;
273         pg->chip.dev = dev;
274
275         mutex_init(&pg->buslock);
276
277         pg->chip.dev = dev;
278         retval = gpiochip_add(&pg->chip);
279         if (retval) {
280                 printk(KERN_ERR "%s: Can not add pmic gpio chip.\n", __func__);
281                 goto err;
282         }
283         set_irq_data(pg->irq, pg);
284         set_irq_chained_handler(pg->irq, pmic_irq_handler);
285         for (i = 0; i < 8; i++) {
286                 set_irq_chip_and_handler_name(i + pg->irq_base, &pmic_irqchip,
287                                         handle_simple_irq, "demux");
288                 set_irq_chip_data(i + pg->irq_base, pg);
289         }
290         return 0;
291 err:
292         iounmap(pg->gpiointr);
293 err2:
294         kfree(pg);
295         return retval;
296 }
297
298 /* at the same time, register a platform driver
299  * this supports the sfi 0.81 fw */
300 static struct platform_driver platform_pmic_gpio_driver = {
301         .driver = {
302                 .name           = DRIVER_NAME,
303                 .owner          = THIS_MODULE,
304         },
305         .probe          = platform_pmic_gpio_probe,
306 };
307
308 static int __init platform_pmic_gpio_init(void)
309 {
310         return platform_driver_register(&platform_pmic_gpio_driver);
311 }
312
313 subsys_initcall(platform_pmic_gpio_init);
314
315 MODULE_AUTHOR("Alek Du <alek.du@intel.com>");
316 MODULE_DESCRIPTION("Intel Moorestown PMIC GPIO driver");
317 MODULE_LICENSE("GPL v2");