linux/drivers/gpio/gpio-em.c
<<
>>
Prefs
   1/*
   2 * Emma Mobile GPIO Support - GIO
   3 *
   4 *  Copyright (C) 2012 Magnus Damm
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 */
  19
  20#include <linux/init.h>
  21#include <linux/platform_device.h>
  22#include <linux/spinlock.h>
  23#include <linux/interrupt.h>
  24#include <linux/ioport.h>
  25#include <linux/io.h>
  26#include <linux/irq.h>
  27#include <linux/irqdomain.h>
  28#include <linux/bitops.h>
  29#include <linux/err.h>
  30#include <linux/gpio.h>
  31#include <linux/slab.h>
  32#include <linux/module.h>
  33#include <linux/platform_data/gpio-em.h>
  34
  35struct em_gio_priv {
  36        void __iomem *base0;
  37        void __iomem *base1;
  38        unsigned int irq_base;
  39        spinlock_t sense_lock;
  40        struct platform_device *pdev;
  41        struct gpio_chip gpio_chip;
  42        struct irq_chip irq_chip;
  43        struct irq_domain *irq_domain;
  44};
  45
  46#define GIO_E1 0x00
  47#define GIO_E0 0x04
  48#define GIO_EM 0x04
  49#define GIO_OL 0x08
  50#define GIO_OH 0x0c
  51#define GIO_I 0x10
  52#define GIO_IIA 0x14
  53#define GIO_IEN 0x18
  54#define GIO_IDS 0x1c
  55#define GIO_IIM 0x1c
  56#define GIO_RAW 0x20
  57#define GIO_MST 0x24
  58#define GIO_IIR 0x28
  59
  60#define GIO_IDT0 0x40
  61#define GIO_IDT1 0x44
  62#define GIO_IDT2 0x48
  63#define GIO_IDT3 0x4c
  64#define GIO_RAWBL 0x50
  65#define GIO_RAWBH 0x54
  66#define GIO_IRBL 0x58
  67#define GIO_IRBH 0x5c
  68
  69#define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))
  70
  71static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
  72{
  73        if (offs < GIO_IDT0)
  74                return ioread32(p->base0 + offs);
  75        else
  76                return ioread32(p->base1 + (offs - GIO_IDT0));
  77}
  78
  79static inline void em_gio_write(struct em_gio_priv *p, int offs,
  80                                unsigned long value)
  81{
  82        if (offs < GIO_IDT0)
  83                iowrite32(value, p->base0 + offs);
  84        else
  85                iowrite32(value, p->base1 + (offs - GIO_IDT0));
  86}
  87
  88static void em_gio_irq_disable(struct irq_data *d)
  89{
  90        struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  91
  92        em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
  93}
  94
  95static void em_gio_irq_enable(struct irq_data *d)
  96{
  97        struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  98
  99        em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
 100}
 101
 102#define GIO_ASYNC(x) (x + 8)
 103
 104static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
 105        [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
 106        [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
 107        [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
 108        [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
 109        [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
 110};
 111
 112static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
 113{
 114        unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
 115        struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
 116        unsigned int reg, offset, shift;
 117        unsigned long flags;
 118        unsigned long tmp;
 119
 120        if (!value)
 121                return -EINVAL;
 122
 123        offset = irqd_to_hwirq(d);
 124
 125        pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
 126
 127        /* 8 x 4 bit fields in 4 IDT registers */
 128        reg = GIO_IDT(offset >> 3);
 129        shift = (offset & 0x07) << 4;
 130
 131        spin_lock_irqsave(&p->sense_lock, flags);
 132
 133        /* disable the interrupt in IIA */
 134        tmp = em_gio_read(p, GIO_IIA);
 135        tmp &= ~BIT(offset);
 136        em_gio_write(p, GIO_IIA, tmp);
 137
 138        /* change the sense setting in IDT */
 139        tmp = em_gio_read(p, reg);
 140        tmp &= ~(0xf << shift);
 141        tmp |= value << shift;
 142        em_gio_write(p, reg, tmp);
 143
 144        /* clear pending interrupts */
 145        em_gio_write(p, GIO_IIR, BIT(offset));
 146
 147        /* enable the interrupt in IIA */
 148        tmp = em_gio_read(p, GIO_IIA);
 149        tmp |= BIT(offset);
 150        em_gio_write(p, GIO_IIA, tmp);
 151
 152        spin_unlock_irqrestore(&p->sense_lock, flags);
 153
 154        return 0;
 155}
 156
 157static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
 158{
 159        struct em_gio_priv *p = dev_id;
 160        unsigned long pending;
 161        unsigned int offset, irqs_handled = 0;
 162
 163        while ((pending = em_gio_read(p, GIO_MST))) {
 164                offset = __ffs(pending);
 165                em_gio_write(p, GIO_IIR, BIT(offset));
 166                generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
 167                irqs_handled++;
 168        }
 169
 170        return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
 171}
 172
 173static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
 174{
 175        return container_of(chip, struct em_gio_priv, gpio_chip);
 176}
 177
 178static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
 179{
 180        em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
 181        return 0;
 182}
 183
 184static int em_gio_get(struct gpio_chip *chip, unsigned offset)
 185{
 186        return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
 187}
 188
 189static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
 190                         unsigned shift, int value)
 191{
 192        /* upper 16 bits contains mask and lower 16 actual value */
 193        em_gio_write(gpio_to_priv(chip), reg,
 194                     (1 << (shift + 16)) | (value << shift));
 195}
 196
 197static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
 198{
 199        /* output is split into two registers */
 200        if (offset < 16)
 201                __em_gio_set(chip, GIO_OL, offset, value);
 202        else
 203                __em_gio_set(chip, GIO_OH, offset - 16, value);
 204}
 205
 206static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
 207                                   int value)
 208{
 209        /* write GPIO value to output before selecting output mode of pin */
 210        em_gio_set(chip, offset, value);
 211        em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
 212        return 0;
 213}
 214
 215static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
 216{
 217        return irq_find_mapping(gpio_to_priv(chip)->irq_domain, offset);
 218}
 219
 220static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int virq,
 221                                 irq_hw_number_t hw)
 222{
 223        struct em_gio_priv *p = h->host_data;
 224
 225        pr_debug("gio: map hw irq = %d, virq = %d\n", (int)hw, virq);
 226
 227        irq_set_chip_data(virq, h->host_data);
 228        irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
 229        set_irq_flags(virq, IRQF_VALID); /* kill me now */
 230        return 0;
 231}
 232
 233static struct irq_domain_ops em_gio_irq_domain_ops = {
 234        .map    = em_gio_irq_domain_map,
 235};
 236
 237static int __devinit em_gio_irq_domain_init(struct em_gio_priv *p)
 238{
 239        struct platform_device *pdev = p->pdev;
 240        struct gpio_em_config *pdata = pdev->dev.platform_data;
 241
 242        p->irq_base = irq_alloc_descs(pdata->irq_base, 0,
 243                                      pdata->number_of_pins, numa_node_id());
 244        if (p->irq_base < 0) {
 245                dev_err(&pdev->dev, "cannot get irq_desc\n");
 246                return p->irq_base;
 247        }
 248        pr_debug("gio: hw base = %d, nr = %d, sw base = %d\n",
 249                 pdata->gpio_base, pdata->number_of_pins, p->irq_base);
 250
 251        p->irq_domain = irq_domain_add_legacy(pdev->dev.of_node,
 252                                              pdata->number_of_pins,
 253                                              p->irq_base, 0,
 254                                              &em_gio_irq_domain_ops, p);
 255        if (!p->irq_domain) {
 256                irq_free_descs(p->irq_base, pdata->number_of_pins);
 257                return -ENXIO;
 258        }
 259
 260        return 0;
 261}
 262
 263static void em_gio_irq_domain_cleanup(struct em_gio_priv *p)
 264{
 265        struct gpio_em_config *pdata = p->pdev->dev.platform_data;
 266
 267        irq_free_descs(p->irq_base, pdata->number_of_pins);
 268        /* FIXME: irq domain wants to be freed! */
 269}
 270
 271static int __devinit em_gio_probe(struct platform_device *pdev)
 272{
 273        struct gpio_em_config *pdata = pdev->dev.platform_data;
 274        struct em_gio_priv *p;
 275        struct resource *io[2], *irq[2];
 276        struct gpio_chip *gpio_chip;
 277        struct irq_chip *irq_chip;
 278        const char *name = dev_name(&pdev->dev);
 279        int ret;
 280
 281        p = kzalloc(sizeof(*p), GFP_KERNEL);
 282        if (!p) {
 283                dev_err(&pdev->dev, "failed to allocate driver data\n");
 284                ret = -ENOMEM;
 285                goto err0;
 286        }
 287
 288        p->pdev = pdev;
 289        platform_set_drvdata(pdev, p);
 290        spin_lock_init(&p->sense_lock);
 291
 292        io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 293        io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 294        irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 295        irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
 296
 297        if (!io[0] || !io[1] || !irq[0] || !irq[1] || !pdata) {
 298                dev_err(&pdev->dev, "missing IRQ, IOMEM or configuration\n");
 299                ret = -EINVAL;
 300                goto err1;
 301        }
 302
 303        p->base0 = ioremap_nocache(io[0]->start, resource_size(io[0]));
 304        if (!p->base0) {
 305                dev_err(&pdev->dev, "failed to remap low I/O memory\n");
 306                ret = -ENXIO;
 307                goto err1;
 308        }
 309
 310        p->base1 = ioremap_nocache(io[1]->start, resource_size(io[1]));
 311        if (!p->base1) {
 312                dev_err(&pdev->dev, "failed to remap high I/O memory\n");
 313                ret = -ENXIO;
 314                goto err2;
 315        }
 316
 317        gpio_chip = &p->gpio_chip;
 318        gpio_chip->direction_input = em_gio_direction_input;
 319        gpio_chip->get = em_gio_get;
 320        gpio_chip->direction_output = em_gio_direction_output;
 321        gpio_chip->set = em_gio_set;
 322        gpio_chip->to_irq = em_gio_to_irq;
 323        gpio_chip->label = name;
 324        gpio_chip->owner = THIS_MODULE;
 325        gpio_chip->base = pdata->gpio_base;
 326        gpio_chip->ngpio = pdata->number_of_pins;
 327
 328        irq_chip = &p->irq_chip;
 329        irq_chip->name = name;
 330        irq_chip->irq_mask = em_gio_irq_disable;
 331        irq_chip->irq_unmask = em_gio_irq_enable;
 332        irq_chip->irq_enable = em_gio_irq_enable;
 333        irq_chip->irq_disable = em_gio_irq_disable;
 334        irq_chip->irq_set_type = em_gio_irq_set_type;
 335        irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
 336
 337        ret = em_gio_irq_domain_init(p);
 338        if (ret) {
 339                dev_err(&pdev->dev, "cannot initialize irq domain\n");
 340                goto err3;
 341        }
 342
 343        if (request_irq(irq[0]->start, em_gio_irq_handler, 0, name, p)) {
 344                dev_err(&pdev->dev, "failed to request low IRQ\n");
 345                ret = -ENOENT;
 346                goto err4;
 347        }
 348
 349        if (request_irq(irq[1]->start, em_gio_irq_handler, 0, name, p)) {
 350                dev_err(&pdev->dev, "failed to request high IRQ\n");
 351                ret = -ENOENT;
 352                goto err5;
 353        }
 354
 355        ret = gpiochip_add(gpio_chip);
 356        if (ret) {
 357                dev_err(&pdev->dev, "failed to add GPIO controller\n");
 358                goto err6;
 359        }
 360        return 0;
 361
 362err6:
 363        free_irq(irq[1]->start, pdev);
 364err5:
 365        free_irq(irq[0]->start, pdev);
 366err4:
 367        em_gio_irq_domain_cleanup(p);
 368err3:
 369        iounmap(p->base1);
 370err2:
 371        iounmap(p->base0);
 372err1:
 373        kfree(p);
 374err0:
 375        return ret;
 376}
 377
 378static int __devexit em_gio_remove(struct platform_device *pdev)
 379{
 380        struct em_gio_priv *p = platform_get_drvdata(pdev);
 381        struct resource *irq[2];
 382        int ret;
 383
 384        ret = gpiochip_remove(&p->gpio_chip);
 385        if (ret)
 386                return ret;
 387
 388        irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 389        irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
 390
 391        free_irq(irq[1]->start, pdev);
 392        free_irq(irq[0]->start, pdev);
 393        em_gio_irq_domain_cleanup(p);
 394        iounmap(p->base1);
 395        iounmap(p->base0);
 396        kfree(p);
 397        return 0;
 398}
 399
 400static struct platform_driver em_gio_device_driver = {
 401        .probe          = em_gio_probe,
 402        .remove         = __devexit_p(em_gio_remove),
 403        .driver         = {
 404                .name   = "em_gio",
 405        }
 406};
 407
 408module_platform_driver(em_gio_device_driver);
 409
 410MODULE_AUTHOR("Magnus Damm");
 411MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
 412MODULE_LICENSE("GPL v2");
 413
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.