linux/drivers/gpio/gpio-twl4030.c
<<
>>
Prefs
   1/*
   2 * Access to GPIOs on TWL4030/TPS659x0 chips
   3 *
   4 * Copyright (C) 2006-2007 Texas Instruments, Inc.
   5 * Copyright (C) 2006 MontaVista Software, Inc.
   6 *
   7 * Code re-arranged and cleaned up by:
   8 *      Syed Mohammed Khasim <x0khasim@ti.com>
   9 *
  10 * Initial Code:
  11 *      Andy Lowe / Nishanth Menon
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2 of the License, or
  16 * (at your option) any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  26 */
  27
  28#include <linux/module.h>
  29#include <linux/init.h>
  30#include <linux/interrupt.h>
  31#include <linux/kthread.h>
  32#include <linux/irq.h>
  33#include <linux/gpio.h>
  34#include <linux/platform_device.h>
  35#include <linux/of.h>
  36#include <linux/irqdomain.h>
  37
  38#include <linux/i2c/twl.h>
  39
  40
  41/*
  42 * The GPIO "subchip" supports 18 GPIOs which can be configured as
  43 * inputs or outputs, with pullups or pulldowns on each pin.  Each
  44 * GPIO can trigger interrupts on either or both edges.
  45 *
  46 * GPIO interrupts can be fed to either of two IRQ lines; this is
  47 * intended to support multiple hosts.
  48 *
  49 * There are also two LED pins used sometimes as output-only GPIOs.
  50 */
  51
  52
  53static struct gpio_chip twl_gpiochip;
  54static int twl4030_gpio_base;
  55static int twl4030_gpio_irq_base;
  56
  57/* genirq interfaces are not available to modules */
  58#ifdef MODULE
  59#define is_module()     true
  60#else
  61#define is_module()     false
  62#endif
  63
  64/* GPIO_CTRL Fields */
  65#define MASK_GPIO_CTRL_GPIO0CD1         BIT(0)
  66#define MASK_GPIO_CTRL_GPIO1CD2         BIT(1)
  67#define MASK_GPIO_CTRL_GPIO_ON          BIT(2)
  68
  69/* Mask for GPIO registers when aggregated into a 32-bit integer */
  70#define GPIO_32_MASK                    0x0003ffff
  71
  72/* Data structures */
  73static DEFINE_MUTEX(gpio_lock);
  74
  75/* store usage of each GPIO. - each bit represents one GPIO */
  76static unsigned int gpio_usage_count;
  77
  78/*----------------------------------------------------------------------*/
  79
  80/*
  81 * To configure TWL4030 GPIO module registers
  82 */
  83static inline int gpio_twl4030_write(u8 address, u8 data)
  84{
  85        return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
  86}
  87
  88/*----------------------------------------------------------------------*/
  89
  90/*
  91 * LED register offsets (use TWL4030_MODULE_{LED,PWMA,PWMB}))
  92 * PWMs A and B are dedicated to LEDs A and B, respectively.
  93 */
  94
  95#define TWL4030_LED_LEDEN       0x0
  96
  97/* LEDEN bits */
  98#define LEDEN_LEDAON            BIT(0)
  99#define LEDEN_LEDBON            BIT(1)
 100#define LEDEN_LEDAEXT           BIT(2)
 101#define LEDEN_LEDBEXT           BIT(3)
 102#define LEDEN_LEDAPWM           BIT(4)
 103#define LEDEN_LEDBPWM           BIT(5)
 104#define LEDEN_PWM_LENGTHA       BIT(6)
 105#define LEDEN_PWM_LENGTHB       BIT(7)
 106
 107#define TWL4030_PWMx_PWMxON     0x0
 108#define TWL4030_PWMx_PWMxOFF    0x1
 109
 110#define PWMxON_LENGTH           BIT(7)
 111
 112/*----------------------------------------------------------------------*/
 113
 114/*
 115 * To read a TWL4030 GPIO module register
 116 */
 117static inline int gpio_twl4030_read(u8 address)
 118{
 119        u8 data;
 120        int ret = 0;
 121
 122        ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
 123        return (ret < 0) ? ret : data;
 124}
 125
 126/*----------------------------------------------------------------------*/
 127
 128static u8 cached_leden;         /* protected by gpio_lock */
 129
 130/* The LED lines are open drain outputs ... a FET pulls to GND, so an
 131 * external pullup is needed.  We could also expose the integrated PWM
 132 * as a LED brightness control; we initialize it as "always on".
 133 */
 134static void twl4030_led_set_value(int led, int value)
 135{
 136        u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
 137        int status;
 138
 139        if (led)
 140                mask <<= 1;
 141
 142        mutex_lock(&gpio_lock);
 143        if (value)
 144                cached_leden &= ~mask;
 145        else
 146                cached_leden |= mask;
 147        status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
 148                        TWL4030_LED_LEDEN);
 149        mutex_unlock(&gpio_lock);
 150}
 151
 152static int twl4030_set_gpio_direction(int gpio, int is_input)
 153{
 154        u8 d_bnk = gpio >> 3;
 155        u8 d_msk = BIT(gpio & 0x7);
 156        u8 reg = 0;
 157        u8 base = REG_GPIODATADIR1 + d_bnk;
 158        int ret = 0;
 159
 160        mutex_lock(&gpio_lock);
 161        ret = gpio_twl4030_read(base);
 162        if (ret >= 0) {
 163                if (is_input)
 164                        reg = ret & ~d_msk;
 165                else
 166                        reg = ret | d_msk;
 167
 168                ret = gpio_twl4030_write(base, reg);
 169        }
 170        mutex_unlock(&gpio_lock);
 171        return ret;
 172}
 173
 174static int twl4030_set_gpio_dataout(int gpio, int enable)
 175{
 176        u8 d_bnk = gpio >> 3;
 177        u8 d_msk = BIT(gpio & 0x7);
 178        u8 base = 0;
 179
 180        if (enable)
 181                base = REG_SETGPIODATAOUT1 + d_bnk;
 182        else
 183                base = REG_CLEARGPIODATAOUT1 + d_bnk;
 184
 185        return gpio_twl4030_write(base, d_msk);
 186}
 187
 188static int twl4030_get_gpio_datain(int gpio)
 189{
 190        u8 d_bnk = gpio >> 3;
 191        u8 d_off = gpio & 0x7;
 192        u8 base = 0;
 193        int ret = 0;
 194
 195        if (unlikely((gpio >= TWL4030_GPIO_MAX)
 196                || !(gpio_usage_count & BIT(gpio))))
 197                return -EPERM;
 198
 199        base = REG_GPIODATAIN1 + d_bnk;
 200        ret = gpio_twl4030_read(base);
 201        if (ret > 0)
 202                ret = (ret >> d_off) & 0x1;
 203
 204        return ret;
 205}
 206
 207/*----------------------------------------------------------------------*/
 208
 209static int twl_request(struct gpio_chip *chip, unsigned offset)
 210{
 211        int status = 0;
 212
 213        mutex_lock(&gpio_lock);
 214
 215        /* Support the two LED outputs as output-only GPIOs. */
 216        if (offset >= TWL4030_GPIO_MAX) {
 217                u8      ledclr_mask = LEDEN_LEDAON | LEDEN_LEDAEXT
 218                                | LEDEN_LEDAPWM | LEDEN_PWM_LENGTHA;
 219                u8      module = TWL4030_MODULE_PWMA;
 220
 221                offset -= TWL4030_GPIO_MAX;
 222                if (offset) {
 223                        ledclr_mask <<= 1;
 224                        module = TWL4030_MODULE_PWMB;
 225                }
 226
 227                /* initialize PWM to always-drive */
 228                status = twl_i2c_write_u8(module, 0x7f,
 229                                TWL4030_PWMx_PWMxOFF);
 230                if (status < 0)
 231                        goto done;
 232                status = twl_i2c_write_u8(module, 0x7f,
 233                                TWL4030_PWMx_PWMxON);
 234                if (status < 0)
 235                        goto done;
 236
 237                /* init LED to not-driven (high) */
 238                module = TWL4030_MODULE_LED;
 239                status = twl_i2c_read_u8(module, &cached_leden,
 240                                TWL4030_LED_LEDEN);
 241                if (status < 0)
 242                        goto done;
 243                cached_leden &= ~ledclr_mask;
 244                status = twl_i2c_write_u8(module, cached_leden,
 245                                TWL4030_LED_LEDEN);
 246                if (status < 0)
 247                        goto done;
 248
 249                status = 0;
 250                goto done;
 251        }
 252
 253        /* on first use, turn GPIO module "on" */
 254        if (!gpio_usage_count) {
 255                struct twl4030_gpio_platform_data *pdata;
 256                u8 value = MASK_GPIO_CTRL_GPIO_ON;
 257
 258                /* optionally have the first two GPIOs switch vMMC1
 259                 * and vMMC2 power supplies based on card presence.
 260                 */
 261                pdata = chip->dev->platform_data;
 262                if (pdata)
 263                        value |= pdata->mmc_cd & 0x03;
 264
 265                status = gpio_twl4030_write(REG_GPIO_CTRL, value);
 266        }
 267
 268        if (!status)
 269                gpio_usage_count |= (0x1 << offset);
 270
 271done:
 272        mutex_unlock(&gpio_lock);
 273        return status;
 274}
 275
 276static void twl_free(struct gpio_chip *chip, unsigned offset)
 277{
 278        if (offset >= TWL4030_GPIO_MAX) {
 279                twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
 280                return;
 281        }
 282
 283        mutex_lock(&gpio_lock);
 284
 285        gpio_usage_count &= ~BIT(offset);
 286
 287        /* on last use, switch off GPIO module */
 288        if (!gpio_usage_count)
 289                gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
 290
 291        mutex_unlock(&gpio_lock);
 292}
 293
 294static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
 295{
 296        return (offset < TWL4030_GPIO_MAX)
 297                ? twl4030_set_gpio_direction(offset, 1)
 298                : -EINVAL;
 299}
 300
 301static int twl_get(struct gpio_chip *chip, unsigned offset)
 302{
 303        int status = 0;
 304
 305        if (offset < TWL4030_GPIO_MAX)
 306                status = twl4030_get_gpio_datain(offset);
 307        else if (offset == TWL4030_GPIO_MAX)
 308                status = cached_leden & LEDEN_LEDAON;
 309        else
 310                status = cached_leden & LEDEN_LEDBON;
 311        return (status < 0) ? 0 : status;
 312}
 313
 314static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
 315{
 316        if (offset < TWL4030_GPIO_MAX) {
 317                twl4030_set_gpio_dataout(offset, value);
 318                return twl4030_set_gpio_direction(offset, 0);
 319        } else {
 320                twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
 321                return 0;
 322        }
 323}
 324
 325static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
 326{
 327        if (offset < TWL4030_GPIO_MAX)
 328                twl4030_set_gpio_dataout(offset, value);
 329        else
 330                twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
 331}
 332
 333static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
 334{
 335        return (twl4030_gpio_irq_base && (offset < TWL4030_GPIO_MAX))
 336                ? (twl4030_gpio_irq_base + offset)
 337                : -EINVAL;
 338}
 339
 340static struct gpio_chip twl_gpiochip = {
 341        .label                  = "twl4030",
 342        .owner                  = THIS_MODULE,
 343        .request                = twl_request,
 344        .free                   = twl_free,
 345        .direction_input        = twl_direction_in,
 346        .get                    = twl_get,
 347        .direction_output       = twl_direction_out,
 348        .set                    = twl_set,
 349        .to_irq                 = twl_to_irq,
 350        .can_sleep              = 1,
 351};
 352
 353/*----------------------------------------------------------------------*/
 354
 355static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
 356{
 357        u8              message[6];
 358        unsigned        i, gpio_bit;
 359
 360        /* For most pins, a pulldown was enabled by default.
 361         * We should have data that's specific to this board.
 362         */
 363        for (gpio_bit = 1, i = 1; i < 6; i++) {
 364                u8              bit_mask;
 365                unsigned        j;
 366
 367                for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
 368                        if (ups & gpio_bit)
 369                                bit_mask |= 1 << (j + 1);
 370                        else if (downs & gpio_bit)
 371                                bit_mask |= 1 << (j + 0);
 372                }
 373                message[i] = bit_mask;
 374        }
 375
 376        return twl_i2c_write(TWL4030_MODULE_GPIO, message,
 377                                REG_GPIOPUPDCTR1, 5);
 378}
 379
 380static int __devinit gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
 381{
 382        u8              message[4];
 383
 384        /* 30 msec of debouncing is always used for MMC card detect,
 385         * and is optional for everything else.
 386         */
 387        message[1] = (debounce & 0xff) | (mmc_cd & 0x03);
 388        debounce >>= 8;
 389        message[2] = (debounce & 0xff);
 390        debounce >>= 8;
 391        message[3] = (debounce & 0x03);
 392
 393        return twl_i2c_write(TWL4030_MODULE_GPIO, message,
 394                                REG_GPIO_DEBEN1, 3);
 395}
 396
 397static int gpio_twl4030_remove(struct platform_device *pdev);
 398
 399static struct twl4030_gpio_platform_data *of_gpio_twl4030(struct device *dev)
 400{
 401        struct twl4030_gpio_platform_data *omap_twl_info;
 402
 403        omap_twl_info = devm_kzalloc(dev, sizeof(*omap_twl_info), GFP_KERNEL);
 404        if (!omap_twl_info)
 405                return NULL;
 406
 407        omap_twl_info->use_leds = of_property_read_bool(dev->of_node,
 408                        "ti,use-leds");
 409
 410        of_property_read_u32(dev->of_node, "ti,debounce",
 411                             &omap_twl_info->debounce);
 412        of_property_read_u32(dev->of_node, "ti,mmc-cd",
 413                             (u32 *)&omap_twl_info->mmc_cd);
 414        of_property_read_u32(dev->of_node, "ti,pullups",
 415                             &omap_twl_info->pullups);
 416        of_property_read_u32(dev->of_node, "ti,pulldowns",
 417                             &omap_twl_info->pulldowns);
 418
 419        return omap_twl_info;
 420}
 421
 422static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
 423{
 424        struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
 425        struct device_node *node = pdev->dev.of_node;
 426        int ret, irq_base;
 427
 428        /* maybe setup IRQs */
 429        if (is_module()) {
 430                dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
 431                goto no_irqs;
 432        }
 433
 434        irq_base = irq_alloc_descs(-1, 0, TWL4030_GPIO_MAX, 0);
 435        if (irq_base < 0) {
 436                dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
 437                return irq_base;
 438        }
 439
 440        irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
 441                              &irq_domain_simple_ops, NULL);
 442
 443        ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
 444        if (ret < 0)
 445                return ret;
 446
 447        twl4030_gpio_irq_base = irq_base;
 448
 449no_irqs:
 450        twl_gpiochip.base = -1;
 451        twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
 452        twl_gpiochip.dev = &pdev->dev;
 453
 454        if (node)
 455                pdata = of_gpio_twl4030(&pdev->dev);
 456
 457        if (pdata == NULL) {
 458                dev_err(&pdev->dev, "Platform data is missing\n");
 459                return -ENXIO;
 460        }
 461
 462        /*
 463         * NOTE:  boards may waste power if they don't set pullups
 464         * and pulldowns correctly ... default for non-ULPI pins is
 465         * pulldown, and some other pins may have external pullups
 466         * or pulldowns.  Careful!
 467         */
 468        ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
 469        if (ret)
 470                dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
 471                        pdata->pullups, pdata->pulldowns, ret);
 472
 473        ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
 474        if (ret)
 475                dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
 476                        pdata->debounce, pdata->mmc_cd, ret);
 477
 478        /*
 479         * NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
 480         * is (still) clear if use_leds is set.
 481         */
 482        if (pdata->use_leds)
 483                twl_gpiochip.ngpio += 2;
 484
 485        ret = gpiochip_add(&twl_gpiochip);
 486        if (ret < 0) {
 487                dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
 488                twl_gpiochip.ngpio = 0;
 489                gpio_twl4030_remove(pdev);
 490                goto out;
 491        }
 492
 493        twl4030_gpio_base = twl_gpiochip.base;
 494
 495        if (pdata && pdata->setup) {
 496                int status;
 497
 498                status = pdata->setup(&pdev->dev,
 499                                twl4030_gpio_base, TWL4030_GPIO_MAX);
 500                if (status)
 501                        dev_dbg(&pdev->dev, "setup --> %d\n", status);
 502        }
 503
 504out:
 505        return ret;
 506}
 507
 508/* Cannot use __devexit as gpio_twl4030_probe() calls us */
 509static int gpio_twl4030_remove(struct platform_device *pdev)
 510{
 511        struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
 512        int status;
 513
 514        if (pdata && pdata->teardown) {
 515                status = pdata->teardown(&pdev->dev,
 516                                twl4030_gpio_base, TWL4030_GPIO_MAX);
 517                if (status) {
 518                        dev_dbg(&pdev->dev, "teardown --> %d\n", status);
 519                        return status;
 520                }
 521        }
 522
 523        status = gpiochip_remove(&twl_gpiochip);
 524        if (status < 0)
 525                return status;
 526
 527        if (is_module())
 528                return 0;
 529
 530        /* REVISIT no support yet for deregistering all the IRQs */
 531        WARN_ON(1);
 532        return -EIO;
 533}
 534
 535static const struct of_device_id twl_gpio_match[] = {
 536        { .compatible = "ti,twl4030-gpio", },
 537        { },
 538};
 539MODULE_DEVICE_TABLE(of, twl_gpio_match);
 540
 541/* Note:  this hardware lives inside an I2C-based multi-function device. */
 542MODULE_ALIAS("platform:twl4030_gpio");
 543
 544static struct platform_driver gpio_twl4030_driver = {
 545        .driver = {
 546                .name   = "twl4030_gpio",
 547                .owner  = THIS_MODULE,
 548                .of_match_table = of_match_ptr(twl_gpio_match),
 549        },
 550        .probe          = gpio_twl4030_probe,
 551        .remove         = gpio_twl4030_remove,
 552};
 553
 554static int __init gpio_twl4030_init(void)
 555{
 556        return platform_driver_register(&gpio_twl4030_driver);
 557}
 558subsys_initcall(gpio_twl4030_init);
 559
 560static void __exit gpio_twl4030_exit(void)
 561{
 562        platform_driver_unregister(&gpio_twl4030_driver);
 563}
 564module_exit(gpio_twl4030_exit);
 565
 566MODULE_AUTHOR("Texas Instruments, Inc.");
 567MODULE_DESCRIPTION("GPIO interface for TWL4030");
 568MODULE_LICENSE("GPL");
 569
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.