linux/drivers/gpio/gpio-palmas.c
<<
>>
Prefs
   1/*
   2 * TI Palma series PMIC's GPIO driver.
   3 *
   4 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
   5 *
   6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms and conditions of the GNU General Public License,
  10 * version 2, as published by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope it will be useful, but WITHOUT
  13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15 * more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19 */
  20
  21#include <linux/gpio.h>
  22#include <linux/kernel.h>
  23#include <linux/module.h>
  24#include <linux/mfd/palmas.h>
  25#include <linux/of.h>
  26#include <linux/of_device.h>
  27#include <linux/platform_device.h>
  28
  29struct palmas_gpio {
  30        struct gpio_chip gpio_chip;
  31        struct palmas *palmas;
  32};
  33
  34static inline struct palmas_gpio *to_palmas_gpio(struct gpio_chip *chip)
  35{
  36        return container_of(chip, struct palmas_gpio, gpio_chip);
  37}
  38
  39static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
  40{
  41        struct palmas_gpio *pg = to_palmas_gpio(gc);
  42        struct palmas *palmas = pg->palmas;
  43        unsigned int val;
  44        int ret;
  45
  46        ret = palmas_read(palmas, PALMAS_GPIO_BASE, PALMAS_GPIO_DATA_DIR, &val);
  47        if (ret < 0) {
  48                dev_err(gc->dev, "GPIO_DATA_DIR read failed, err = %d\n", ret);
  49                return ret;
  50        }
  51
  52        if (val & (1 << offset)) {
  53                ret = palmas_read(palmas, PALMAS_GPIO_BASE,
  54                                  PALMAS_GPIO_DATA_OUT, &val);
  55        } else {
  56                ret = palmas_read(palmas, PALMAS_GPIO_BASE,
  57                                  PALMAS_GPIO_DATA_IN, &val);
  58        }
  59        if (ret < 0) {
  60                dev_err(gc->dev, "GPIO_DATA_IN/OUT read failed, err = %d\n",
  61                        ret);
  62                return ret;
  63        }
  64        return !!(val & BIT(offset));
  65}
  66
  67static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
  68                        int value)
  69{
  70        struct palmas_gpio *pg = to_palmas_gpio(gc);
  71        struct palmas *palmas = pg->palmas;
  72        int ret;
  73
  74        if (value)
  75                ret = palmas_write(palmas, PALMAS_GPIO_BASE,
  76                                PALMAS_GPIO_SET_DATA_OUT, BIT(offset));
  77        else
  78                ret = palmas_write(palmas, PALMAS_GPIO_BASE,
  79                                PALMAS_GPIO_CLEAR_DATA_OUT, BIT(offset));
  80        if (ret < 0)
  81                dev_err(gc->dev, "%s write failed, err = %d\n",
  82                        (value) ? "GPIO_SET_DATA_OUT" : "GPIO_CLEAR_DATA_OUT",
  83                        ret);
  84}
  85
  86static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
  87                                int value)
  88{
  89        struct palmas_gpio *pg = to_palmas_gpio(gc);
  90        struct palmas *palmas = pg->palmas;
  91        int ret;
  92
  93        /* Set the initial value */
  94        palmas_gpio_set(gc, offset, value);
  95
  96        ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE,
  97                PALMAS_GPIO_DATA_DIR, BIT(offset), BIT(offset));
  98        if (ret < 0)
  99                dev_err(gc->dev, "GPIO_DATA_DIR write failed, err = %d\n", ret);
 100        return ret;
 101}
 102
 103static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
 104{
 105        struct palmas_gpio *pg = to_palmas_gpio(gc);
 106        struct palmas *palmas = pg->palmas;
 107        int ret;
 108
 109        ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE,
 110                PALMAS_GPIO_DATA_DIR, BIT(offset), 0);
 111        if (ret < 0)
 112                dev_err(gc->dev, "GPIO_DATA_DIR write failed, err = %d\n", ret);
 113        return ret;
 114}
 115
 116static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
 117{
 118        struct palmas_gpio *pg = to_palmas_gpio(gc);
 119        struct palmas *palmas = pg->palmas;
 120
 121        return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
 122}
 123
 124static int palmas_gpio_probe(struct platform_device *pdev)
 125{
 126        struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
 127        struct palmas_platform_data *palmas_pdata;
 128        struct palmas_gpio *palmas_gpio;
 129        int ret;
 130
 131        palmas_gpio = devm_kzalloc(&pdev->dev,
 132                                sizeof(*palmas_gpio), GFP_KERNEL);
 133        if (!palmas_gpio) {
 134                dev_err(&pdev->dev, "Could not allocate palmas_gpio\n");
 135                return -ENOMEM;
 136        }
 137
 138        palmas_gpio->palmas = palmas;
 139        palmas_gpio->gpio_chip.owner = THIS_MODULE;
 140        palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
 141        palmas_gpio->gpio_chip.ngpio = 8;
 142        palmas_gpio->gpio_chip.can_sleep = 1;
 143        palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
 144        palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
 145        palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
 146        palmas_gpio->gpio_chip.set      = palmas_gpio_set;
 147        palmas_gpio->gpio_chip.get      = palmas_gpio_get;
 148        palmas_gpio->gpio_chip.dev = &pdev->dev;
 149#ifdef CONFIG_OF_GPIO
 150        palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
 151#endif
 152        palmas_pdata = dev_get_platdata(palmas->dev);
 153        if (palmas_pdata && palmas_pdata->gpio_base)
 154                palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
 155        else
 156                palmas_gpio->gpio_chip.base = -1;
 157
 158        ret = gpiochip_add(&palmas_gpio->gpio_chip);
 159        if (ret < 0) {
 160                dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
 161                return ret;
 162        }
 163
 164        platform_set_drvdata(pdev, palmas_gpio);
 165        return ret;
 166}
 167
 168static int palmas_gpio_remove(struct platform_device *pdev)
 169{
 170        struct palmas_gpio *palmas_gpio = platform_get_drvdata(pdev);
 171
 172        return gpiochip_remove(&palmas_gpio->gpio_chip);
 173}
 174
 175static struct of_device_id of_palmas_gpio_match[] = {
 176        { .compatible = "ti,palmas-gpio"},
 177        { .compatible = "ti,tps65913-gpio"},
 178        { .compatible = "ti,tps65914-gpio"},
 179        { .compatible = "ti,tps80036-gpio"},
 180        { },
 181};
 182MODULE_DEVICE_TABLE(of, of_palmas_gpio_match);
 183
 184static struct platform_driver palmas_gpio_driver = {
 185        .driver.name    = "palmas-gpio",
 186        .driver.owner   = THIS_MODULE,
 187        .driver.of_match_table = of_palmas_gpio_match,
 188        .probe          = palmas_gpio_probe,
 189        .remove         = palmas_gpio_remove,
 190};
 191
 192static int __init palmas_gpio_init(void)
 193{
 194        return platform_driver_register(&palmas_gpio_driver);
 195}
 196subsys_initcall(palmas_gpio_init);
 197
 198static void __exit palmas_gpio_exit(void)
 199{
 200        platform_driver_unregister(&palmas_gpio_driver);
 201}
 202module_exit(palmas_gpio_exit);
 203
 204MODULE_ALIAS("platform:palmas-gpio");
 205MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
 206MODULE_DESCRIPTION("GPIO driver for TI Palmas series PMICs");
 207MODULE_LICENSE("GPL v2");
 208
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.