1 /* Moorestown PMIC GPIO (access through IPC) driver
2 * Copyright (c) 2008 - 2009, Intel Corporation.
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.
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.
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.
19 * Moorestown platform PMIC chip
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>
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>
37 #define DRIVER_NAME "pmic_gpio"
39 /* register offset that IPC driver should use
40 * 8 GPIO + 8 GPOSW (6 controllable) + 8GPO
42 enum pmic_gpio_register {
51 /* bits definition for GPIO & GPOSW */
56 #define GPIO_INTCTL 0x30
59 #define GPOSW_DRV 0x01
60 #define GPOSW_DOU 0x08
61 #define GPOSW_RDRV 0x30
66 struct pmic_gpio_irq {
68 u32 trigger[NUM_GPIO];
70 struct work_struct work;
75 struct gpio_chip chip;
76 struct pmic_gpio_irq irqtypes;
82 static void pmic_program_irqtype(int gpio, int type)
84 if (type & IRQ_TYPE_EDGE_RISING)
85 intel_scu_ipc_update_register(GPIO0 + gpio, 0x20, 0x20);
87 intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x20);
89 if (type & IRQ_TYPE_EDGE_FALLING)
90 intel_scu_ipc_update_register(GPIO0 + gpio, 0x10, 0x10);
92 intel_scu_ipc_update_register(GPIO0 + gpio, 0x00, 0x10);
95 static void pmic_irqtype_work(struct work_struct *work)
97 struct pmic_gpio_irq *t =
98 container_of(work, struct pmic_gpio_irq, work);
103 spin_lock_irqsave(&t->lock, flags);
104 /* As we drop the lock, we may need multiple scans if we race the
105 pmic_irq_type function */
108 * For each pin that has the dirty bit set send an IPC
109 * message to configure the hardware via the PMIC
111 for (i = 0; i < NUM_GPIO; i++) {
112 if (!(t->dirty & (1 << i)))
114 t->dirty &= ~(1 << i);
115 /* We can't trust the array entry or dirty
116 once the lock is dropped */
117 type = t->trigger[i];
118 spin_unlock_irqrestore(&t->lock, flags);
119 pmic_program_irqtype(i, type);
120 spin_lock_irqsave(&t->lock, flags);
123 spin_unlock_irqrestore(&t->lock, flags);
126 static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
130 "%s: only pin 0-7 support input\n", __func__);
131 return -1;/* we only have 8 GPIO can use as input */
133 return intel_scu_ipc_update_register(GPIO0 + offset,
137 static int pmic_gpio_direction_output(struct gpio_chip *chip,
138 unsigned offset, int value)
142 if (offset < 8)/* it is GPIO */
143 rc = intel_scu_ipc_update_register(GPIO0 + offset,
144 GPIO_DRV | (value ? GPIO_DOU : 0),
145 GPIO_DRV | GPIO_DOU | GPIO_DIR);
146 else if (offset < 16)/* it is GPOSW */
147 rc = intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
148 GPOSW_DRV | (value ? GPOSW_DOU : 0),
149 GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
150 else if (offset > 15 && offset < 24)/* it is GPO */
151 rc = intel_scu_ipc_update_register(GPO,
152 value ? 1 << (offset - 16) : 0,
156 "%s: invalid PMIC GPIO pin %d!\n", __func__, offset);
163 static int pmic_gpio_get(struct gpio_chip *chip, unsigned offset)
168 /* we only have 8 GPIO pins we can use as input */
171 ret = intel_scu_ipc_ioread8(GPIO0 + offset, &r);
177 static void pmic_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
179 if (offset < 8)/* it is GPIO */
180 intel_scu_ipc_update_register(GPIO0 + offset,
181 GPIO_DRV | (value ? GPIO_DOU : 0),
182 GPIO_DRV | GPIO_DOU);
183 else if (offset < 16)/* it is GPOSW */
184 intel_scu_ipc_update_register(GPOSWCTL0 + offset - 8,
185 GPOSW_DRV | (value ? GPOSW_DOU : 0),
186 GPOSW_DRV | GPOSW_DOU | GPOSW_RDRV);
187 else if (offset > 15 && offset < 24) /* it is GPO */
188 intel_scu_ipc_update_register(GPO,
189 value ? 1 << (offset - 16) : 0,
193 static int pmic_irq_type(unsigned irq, unsigned type)
195 struct pmic_gpio *pg = get_irq_chip_data(irq);
196 u32 gpio = irq - pg->irq_base;
199 if (gpio >= pg->chip.ngpio)
202 spin_lock_irqsave(&pg->irqtypes.lock, flags);
203 pg->irqtypes.trigger[gpio] = type;
204 pg->irqtypes.dirty |= (1 << gpio);
205 spin_unlock_irqrestore(&pg->irqtypes.lock, flags);
206 schedule_work(&pg->irqtypes.work);
212 static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
214 struct pmic_gpio *pg = container_of(chip, struct pmic_gpio, chip);
216 return pg->irq_base + offset;
219 /* the gpiointr register is read-clear, so just do nothing. */
220 static void pmic_irq_unmask(unsigned irq)
224 static void pmic_irq_mask(unsigned irq)
228 static struct irq_chip pmic_irqchip = {
230 .mask = pmic_irq_mask,
231 .unmask = pmic_irq_unmask,
232 .set_type = pmic_irq_type,
235 static void pmic_irq_handler(unsigned irq, struct irq_desc *desc)
237 struct pmic_gpio *pg = (struct pmic_gpio *)get_irq_data(irq);
238 u8 intsts = *((u8 *)pg->gpiointr + 4);
241 for (gpio = 0; gpio < 8; gpio++) {
242 if (intsts & (1 << gpio)) {
243 pr_debug("pmic pin %d triggered\n", gpio);
244 generic_handle_irq(pg->irq_base + gpio);
248 if (desc->chip->irq_eoi)
249 desc->chip->irq_eoi(irq_get_irq_data(irq));
251 dev_warn(pg->chip.dev, "missing EOI handler for irq %d\n", irq);
254 static int __devinit platform_pmic_gpio_probe(struct platform_device *pdev)
256 struct device *dev = &pdev->dev;
257 int irq = platform_get_irq(pdev, 0);
258 struct intel_pmic_gpio_platform_data *pdata = dev->platform_data;
260 struct pmic_gpio *pg;
265 dev_dbg(dev, "no IRQ line\n");
269 if (!pdata || !pdata->gpio_base || !pdata->irq_base) {
270 dev_dbg(dev, "incorrect or missing platform data\n");
274 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
278 dev_set_drvdata(dev, pg);
281 /* setting up SRAM mapping for GPIOINT register */
282 pg->gpiointr = ioremap_nocache(pdata->gpiointr, 8);
284 printk(KERN_ERR "%s: Can not map GPIOINT.\n", __func__);
288 pg->irq_base = pdata->irq_base;
289 pg->chip.label = "intel_pmic";
290 pg->chip.direction_input = pmic_gpio_direction_input;
291 pg->chip.direction_output = pmic_gpio_direction_output;
292 pg->chip.get = pmic_gpio_get;
293 pg->chip.set = pmic_gpio_set;
294 pg->chip.to_irq = pmic_gpio_to_irq;
295 pg->chip.base = pdata->gpio_base;
296 pg->chip.ngpio = NUM_GPIO;
297 pg->chip.can_sleep = 1;
300 INIT_WORK(&pg->irqtypes.work, pmic_irqtype_work);
301 spin_lock_init(&pg->irqtypes.lock);
304 retval = gpiochip_add(&pg->chip);
306 printk(KERN_ERR "%s: Can not add pmic gpio chip.\n", __func__);
309 set_irq_data(pg->irq, pg);
310 set_irq_chained_handler(pg->irq, pmic_irq_handler);
311 for (i = 0; i < 8; i++) {
312 set_irq_chip_and_handler_name(i + pg->irq_base, &pmic_irqchip,
313 handle_simple_irq, "demux");
314 set_irq_chip_data(i + pg->irq_base, pg);
318 iounmap(pg->gpiointr);
324 /* at the same time, register a platform driver
325 * this supports the sfi 0.81 fw */
326 static struct platform_driver platform_pmic_gpio_driver = {
329 .owner = THIS_MODULE,
331 .probe = platform_pmic_gpio_probe,
334 static int __init platform_pmic_gpio_init(void)
336 return platform_driver_register(&platform_pmic_gpio_driver);
339 subsys_initcall(platform_pmic_gpio_init);
341 MODULE_AUTHOR("Alek Du <alek.du@intel.com>");
342 MODULE_DESCRIPTION("Intel Moorestown PMIC GPIO driver");
343 MODULE_LICENSE("GPL v2");