blob: 4fab37bf564d5ce19b71225ce50eb6d6298f2620 [file] [log] [blame]
Tomoya MORINAGA49a36792011-01-12 17:00:22 -08001/*
2 * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
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 as published by
6 * the Free Software Foundation; version 2 of the License.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */
17#include <linux/kernel.h>
Andrew Morton545554e2011-05-24 17:13:43 -070018#include <linux/slab.h>
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080019#include <linux/pci.h>
20#include <linux/gpio.h>
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090021#include <linux/interrupt.h>
22#include <linux/irq.h>
23
24#define IOH_EDGE_FALLING 0
25#define IOH_EDGE_RISING BIT(0)
26#define IOH_LEVEL_L BIT(1)
27#define IOH_LEVEL_H (BIT(0) | BIT(1))
28#define IOH_EDGE_BOTH BIT(2)
29#define IOH_IM_MASK (BIT(0) | BIT(1) | BIT(2))
30
31#define IOH_IRQ_BASE 0
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080032
33#define PCI_VENDOR_ID_ROHM 0x10DB
34
35struct ioh_reg_comn {
36 u32 ien;
37 u32 istatus;
38 u32 idisp;
39 u32 iclr;
40 u32 imask;
41 u32 imaskclr;
42 u32 po;
43 u32 pi;
44 u32 pm;
45 u32 im_0;
46 u32 im_1;
47 u32 reserved;
48};
49
50struct ioh_regs {
51 struct ioh_reg_comn regs[8];
52 u32 reserve1[16];
53 u32 ioh_sel_reg[4];
54 u32 reserve2[11];
55 u32 srst;
56};
57
58/**
59 * struct ioh_gpio_reg_data - The register store data.
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090060 * @ien_reg To store contents of interrupt enable register.
61 * @imask_reg: To store contents of interrupt mask regist
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080062 * @po_reg: To store contents of PO register.
63 * @pm_reg: To store contents of PM register.
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090064 * @im0_reg: To store contents of interrupt mode regist0
65 * @im1_reg: To store contents of interrupt mode regist1
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080066 */
67struct ioh_gpio_reg_data {
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090068 u32 ien_reg;
69 u32 imask_reg;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080070 u32 po_reg;
71 u32 pm_reg;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090072 u32 im0_reg;
73 u32 im1_reg;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080074};
75
76/**
77 * struct ioh_gpio - GPIO private data structure.
78 * @base: PCI base address of Memory mapped I/O register.
79 * @reg: Memory mapped IOH GPIO register list.
80 * @dev: Pointer to device structure.
81 * @gpio: Data for GPIO infrastructure.
82 * @ioh_gpio_reg: Memory mapped Register data is saved here
83 * when suspend.
84 * @ch: Indicate GPIO channel
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090085 * @irq_base: Save base of IRQ number for interrupt
86 * @spinlock: Used for register access protection in
87 * interrupt context ioh_irq_type and PM;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080088 */
89struct ioh_gpio {
90 void __iomem *base;
91 struct ioh_regs __iomem *reg;
92 struct device *dev;
93 struct gpio_chip gpio;
94 struct ioh_gpio_reg_data ioh_gpio_reg;
95 struct mutex lock;
96 int ch;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +090097 int irq_base;
98 spinlock_t spinlock;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -080099};
100
101static const int num_ports[] = {6, 12, 16, 16, 15, 16, 16, 12};
102
103static void ioh_gpio_set(struct gpio_chip *gpio, unsigned nr, int val)
104{
105 u32 reg_val;
106 struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
107
108 mutex_lock(&chip->lock);
109 reg_val = ioread32(&chip->reg->regs[chip->ch].po);
110 if (val)
111 reg_val |= (1 << nr);
112 else
113 reg_val &= ~(1 << nr);
114
115 iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
116 mutex_unlock(&chip->lock);
117}
118
119static int ioh_gpio_get(struct gpio_chip *gpio, unsigned nr)
120{
121 struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
122
123 return ioread32(&chip->reg->regs[chip->ch].pi) & (1 << nr);
124}
125
126static int ioh_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
127 int val)
128{
129 struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
130 u32 pm;
131 u32 reg_val;
132
133 mutex_lock(&chip->lock);
134 pm = ioread32(&chip->reg->regs[chip->ch].pm) &
135 ((1 << num_ports[chip->ch]) - 1);
136 pm |= (1 << nr);
137 iowrite32(pm, &chip->reg->regs[chip->ch].pm);
138
139 reg_val = ioread32(&chip->reg->regs[chip->ch].po);
140 if (val)
141 reg_val |= (1 << nr);
142 else
143 reg_val &= ~(1 << nr);
Peter Tyserba438612011-03-24 18:17:14 -0500144 iowrite32(reg_val, &chip->reg->regs[chip->ch].po);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800145
146 mutex_unlock(&chip->lock);
147
148 return 0;
149}
150
151static int ioh_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
152{
153 struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
154 u32 pm;
155
156 mutex_lock(&chip->lock);
157 pm = ioread32(&chip->reg->regs[chip->ch].pm) &
158 ((1 << num_ports[chip->ch]) - 1);
159 pm &= ~(1 << nr);
160 iowrite32(pm, &chip->reg->regs[chip->ch].pm);
161 mutex_unlock(&chip->lock);
162
163 return 0;
164}
165
Andrew Morton545554e2011-05-24 17:13:43 -0700166#ifdef CONFIG_PM
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800167/*
168 * Save register configuration and disable interrupts.
169 */
170static void ioh_gpio_save_reg_conf(struct ioh_gpio *chip)
171{
172 chip->ioh_gpio_reg.po_reg = ioread32(&chip->reg->regs[chip->ch].po);
173 chip->ioh_gpio_reg.pm_reg = ioread32(&chip->reg->regs[chip->ch].pm);
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900174 chip->ioh_gpio_reg.ien_reg = ioread32(&chip->reg->regs[chip->ch].ien);
175 chip->ioh_gpio_reg.imask_reg = ioread32(&chip->reg->regs[chip->ch].imask);
176 chip->ioh_gpio_reg.im0_reg = ioread32(&chip->reg->regs[chip->ch].im_0);
177 chip->ioh_gpio_reg.im1_reg = ioread32(&chip->reg->regs[chip->ch].im_1);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800178}
179
180/*
181 * This function restores the register configuration of the GPIO device.
182 */
183static void ioh_gpio_restore_reg_conf(struct ioh_gpio *chip)
184{
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800185 iowrite32(chip->ioh_gpio_reg.po_reg, &chip->reg->regs[chip->ch].po);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800186 iowrite32(chip->ioh_gpio_reg.pm_reg, &chip->reg->regs[chip->ch].pm);
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900187 iowrite32(chip->ioh_gpio_reg.ien_reg, &chip->reg->regs[chip->ch].ien);
188 iowrite32(chip->ioh_gpio_reg.imask_reg, &chip->reg->regs[chip->ch].imask);
189 iowrite32(chip->ioh_gpio_reg.im0_reg, &chip->reg->regs[chip->ch].im_0);
190 iowrite32(chip->ioh_gpio_reg.im1_reg, &chip->reg->regs[chip->ch].im_1);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800191}
Andrew Morton545554e2011-05-24 17:13:43 -0700192#endif
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800193
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900194static int ioh_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
195{
196 struct ioh_gpio *chip = container_of(gpio, struct ioh_gpio, gpio);
197 return chip->irq_base + offset;
198}
199
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800200static void ioh_gpio_setup(struct ioh_gpio *chip, int num_port)
201{
202 struct gpio_chip *gpio = &chip->gpio;
203
204 gpio->label = dev_name(chip->dev);
205 gpio->owner = THIS_MODULE;
206 gpio->direction_input = ioh_gpio_direction_input;
207 gpio->get = ioh_gpio_get;
208 gpio->direction_output = ioh_gpio_direction_output;
209 gpio->set = ioh_gpio_set;
210 gpio->dbg_show = NULL;
211 gpio->base = -1;
212 gpio->ngpio = num_port;
213 gpio->can_sleep = 0;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900214 gpio->to_irq = ioh_gpio_to_irq;
215}
216
217static int ioh_irq_type(struct irq_data *d, unsigned int type)
218{
219 u32 im;
220 u32 *im_reg;
221 u32 ien;
222 u32 im_pos;
223 int ch;
224 unsigned long flags;
225 u32 val;
226 int irq = d->irq;
227 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
228 struct ioh_gpio *chip = gc->private;
229
230 ch = irq - chip->irq_base;
231 if (irq <= chip->irq_base + 7) {
232 im_reg = &chip->reg->regs[chip->ch].im_0;
233 im_pos = ch;
234 } else {
235 im_reg = &chip->reg->regs[chip->ch].im_1;
236 im_pos = ch - 8;
237 }
238 dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d type=%d\n",
239 __func__, irq, type, ch, im_pos, type);
240
241 spin_lock_irqsave(&chip->spinlock, flags);
242
243 switch (type) {
244 case IRQ_TYPE_EDGE_RISING:
245 val = IOH_EDGE_RISING;
246 break;
247 case IRQ_TYPE_EDGE_FALLING:
248 val = IOH_EDGE_FALLING;
249 break;
250 case IRQ_TYPE_EDGE_BOTH:
251 val = IOH_EDGE_BOTH;
252 break;
253 case IRQ_TYPE_LEVEL_HIGH:
254 val = IOH_LEVEL_H;
255 break;
256 case IRQ_TYPE_LEVEL_LOW:
257 val = IOH_LEVEL_L;
258 break;
259 case IRQ_TYPE_PROBE:
260 goto end;
261 default:
262 dev_warn(chip->dev, "%s: unknown type(%dd)",
263 __func__, type);
264 goto end;
265 }
266
267 /* Set interrupt mode */
268 im = ioread32(im_reg) & ~(IOH_IM_MASK << (im_pos * 4));
269 iowrite32(im | (val << (im_pos * 4)), im_reg);
270
271 /* iclr */
272 iowrite32(BIT(ch), &chip->reg->regs[chip->ch].iclr);
273
274 /* IMASKCLR */
275 iowrite32(BIT(ch), &chip->reg->regs[chip->ch].imaskclr);
276
277 /* Enable interrupt */
278 ien = ioread32(&chip->reg->regs[chip->ch].ien);
279 iowrite32(ien | BIT(ch), &chip->reg->regs[chip->ch].ien);
280end:
281 spin_unlock_irqrestore(&chip->spinlock, flags);
282
283 return 0;
284}
285
286static void ioh_irq_unmask(struct irq_data *d)
287{
288 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
289 struct ioh_gpio *chip = gc->private;
290
291 iowrite32(1 << (d->irq - chip->irq_base),
292 &chip->reg->regs[chip->ch].imaskclr);
293}
294
295static void ioh_irq_mask(struct irq_data *d)
296{
297 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
298 struct ioh_gpio *chip = gc->private;
299
300 iowrite32(1 << (d->irq - chip->irq_base),
301 &chip->reg->regs[chip->ch].imask);
302}
303
304static irqreturn_t ioh_gpio_handler(int irq, void *dev_id)
305{
306 struct ioh_gpio *chip = dev_id;
307 u32 reg_val;
308 int i, j;
309 int ret = IRQ_NONE;
310
311 for (i = 0; i < 8; i++) {
312 reg_val = ioread32(&chip->reg->regs[i].istatus);
313 for (j = 0; j < num_ports[i]; j++) {
314 if (reg_val & BIT(j)) {
315 dev_dbg(chip->dev,
316 "%s:[%d]:irq=%d status=0x%x\n",
317 __func__, j, irq, reg_val);
318 iowrite32(BIT(j),
319 &chip->reg->regs[chip->ch].iclr);
320 generic_handle_irq(chip->irq_base + j);
321 ret = IRQ_HANDLED;
322 }
323 }
324 }
325 return ret;
326}
327
328static __devinit void ioh_gpio_alloc_generic_chip(struct ioh_gpio *chip,
329 unsigned int irq_start, unsigned int num)
330{
331 struct irq_chip_generic *gc;
332 struct irq_chip_type *ct;
333
334 gc = irq_alloc_generic_chip("ioh_gpio", 1, irq_start, chip->base,
335 handle_simple_irq);
336 gc->private = chip;
337 ct = gc->chip_types;
338
339 ct->chip.irq_mask = ioh_irq_mask;
340 ct->chip.irq_unmask = ioh_irq_unmask;
341 ct->chip.irq_set_type = ioh_irq_type;
342
343 irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
344 IRQ_NOREQUEST | IRQ_NOPROBE, 0);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800345}
346
347static int __devinit ioh_gpio_probe(struct pci_dev *pdev,
348 const struct pci_device_id *id)
349{
350 int ret;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900351 int i, j;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800352 struct ioh_gpio *chip;
353 void __iomem *base;
354 void __iomem *chip_save;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900355 int irq_base;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800356
357 ret = pci_enable_device(pdev);
358 if (ret) {
359 dev_err(&pdev->dev, "%s : pci_enable_device failed", __func__);
360 goto err_pci_enable;
361 }
362
363 ret = pci_request_regions(pdev, KBUILD_MODNAME);
364 if (ret) {
365 dev_err(&pdev->dev, "pci_request_regions failed-%d", ret);
366 goto err_request_regions;
367 }
368
369 base = pci_iomap(pdev, 1, 0);
370 if (base == 0) {
371 dev_err(&pdev->dev, "%s : pci_iomap failed", __func__);
372 ret = -ENOMEM;
373 goto err_iomap;
374 }
375
376 chip_save = kzalloc(sizeof(*chip) * 8, GFP_KERNEL);
377 if (chip_save == NULL) {
378 dev_err(&pdev->dev, "%s : kzalloc failed", __func__);
379 ret = -ENOMEM;
380 goto err_kzalloc;
381 }
382
383 chip = chip_save;
384 for (i = 0; i < 8; i++, chip++) {
385 chip->dev = &pdev->dev;
386 chip->base = base;
387 chip->reg = chip->base;
388 chip->ch = i;
389 mutex_init(&chip->lock);
390 ioh_gpio_setup(chip, num_ports[i]);
391 ret = gpiochip_add(&chip->gpio);
392 if (ret) {
393 dev_err(&pdev->dev, "IOH gpio: Failed to register GPIO\n");
394 goto err_gpiochip_add;
395 }
396 }
397
398 chip = chip_save;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900399 for (j = 0; j < 8; j++, chip++) {
400 irq_base = irq_alloc_descs(-1, IOH_IRQ_BASE, num_ports[j],
401 GFP_KERNEL);
402 if (irq_base < 0) {
403 dev_warn(&pdev->dev,
404 "ml_ioh_gpio: Failed to get IRQ base num\n");
405 chip->irq_base = -1;
406 goto err_irq_alloc_descs;
407 }
408 chip->irq_base = irq_base;
409 ioh_gpio_alloc_generic_chip(chip, irq_base, num_ports[j]);
410 }
411
412 chip = chip_save;
413 ret = request_irq(pdev->irq, ioh_gpio_handler,
414 IRQF_SHARED, KBUILD_MODNAME, chip);
415 if (ret != 0) {
416 dev_err(&pdev->dev,
417 "%s request_irq failed\n", __func__);
418 goto err_request_irq;
419 }
420
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800421 pci_set_drvdata(pdev, chip);
422
423 return 0;
424
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900425err_request_irq:
426 chip = chip_save;
427err_irq_alloc_descs:
428 while (--j >= 0) {
429 chip--;
430 irq_free_descs(chip->irq_base, num_ports[j]);
431 }
432
433 chip = chip_save;
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800434err_gpiochip_add:
Axel Lin33300572011-06-14 19:12:57 +0800435 while (--i >= 0) {
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800436 chip--;
437 ret = gpiochip_remove(&chip->gpio);
438 if (ret)
439 dev_err(&pdev->dev, "Failed gpiochip_remove(%d)\n", i);
440 }
441 kfree(chip_save);
442
443err_kzalloc:
444 pci_iounmap(pdev, base);
445
446err_iomap:
447 pci_release_regions(pdev);
448
449err_request_regions:
450 pci_disable_device(pdev);
451
452err_pci_enable:
453
454 dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret);
455 return ret;
456}
457
458static void __devexit ioh_gpio_remove(struct pci_dev *pdev)
459{
460 int err;
461 int i;
462 struct ioh_gpio *chip = pci_get_drvdata(pdev);
463 void __iomem *chip_save;
464
465 chip_save = chip;
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900466
467 free_irq(pdev->irq, chip);
468
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800469 for (i = 0; i < 8; i++, chip++) {
Tomoya MORINAGA54be5662011-08-05 13:04:21 +0900470 irq_free_descs(chip->irq_base, num_ports[i]);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800471 err = gpiochip_remove(&chip->gpio);
472 if (err)
473 dev_err(&pdev->dev, "Failed gpiochip_remove\n");
474 }
475
476 chip = chip_save;
477 pci_iounmap(pdev, chip->base);
478 pci_release_regions(pdev);
479 pci_disable_device(pdev);
480 kfree(chip);
481}
482
483#ifdef CONFIG_PM
484static int ioh_gpio_suspend(struct pci_dev *pdev, pm_message_t state)
485{
486 s32 ret;
487 struct ioh_gpio *chip = pci_get_drvdata(pdev);
488
489 ioh_gpio_save_reg_conf(chip);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800490
491 ret = pci_save_state(pdev);
492 if (ret) {
493 dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret);
494 return ret;
495 }
496 pci_disable_device(pdev);
497 pci_set_power_state(pdev, PCI_D0);
498 ret = pci_enable_wake(pdev, PCI_D0, 1);
499 if (ret)
500 dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret);
501
502 return 0;
503}
504
505static int ioh_gpio_resume(struct pci_dev *pdev)
506{
507 s32 ret;
508 struct ioh_gpio *chip = pci_get_drvdata(pdev);
509
510 ret = pci_enable_wake(pdev, PCI_D0, 0);
511
512 pci_set_power_state(pdev, PCI_D0);
513 ret = pci_enable_device(pdev);
514 if (ret) {
515 dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret);
516 return ret;
517 }
518 pci_restore_state(pdev);
519
520 iowrite32(0x01, &chip->reg->srst);
521 iowrite32(0x00, &chip->reg->srst);
522 ioh_gpio_restore_reg_conf(chip);
523
524 return 0;
525}
526#else
527#define ioh_gpio_suspend NULL
528#define ioh_gpio_resume NULL
529#endif
530
531static DEFINE_PCI_DEVICE_TABLE(ioh_gpio_pcidev_id) = {
532 { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x802E) },
533 { 0, }
534};
Axel Lin19234cd2011-03-11 14:58:30 -0800535MODULE_DEVICE_TABLE(pci, ioh_gpio_pcidev_id);
Tomoya MORINAGA49a36792011-01-12 17:00:22 -0800536
537static struct pci_driver ioh_gpio_driver = {
538 .name = "ml_ioh_gpio",
539 .id_table = ioh_gpio_pcidev_id,
540 .probe = ioh_gpio_probe,
541 .remove = __devexit_p(ioh_gpio_remove),
542 .suspend = ioh_gpio_suspend,
543 .resume = ioh_gpio_resume
544};
545
546static int __init ioh_gpio_pci_init(void)
547{
548 return pci_register_driver(&ioh_gpio_driver);
549}
550module_init(ioh_gpio_pci_init);
551
552static void __exit ioh_gpio_pci_exit(void)
553{
554 pci_unregister_driver(&ioh_gpio_driver);
555}
556module_exit(ioh_gpio_pci_exit);
557
558MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver");
559MODULE_LICENSE("GPL");