gpio-palmas.c 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * TI Palma series PMIC's GPIO driver.
 *
 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
 *
 * Author: Laxman Dewangan <ldewangan@nvidia.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mfd/palmas.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>

struct palmas_gpio {
	struct gpio_chip gpio_chip;
	struct palmas *palmas;
};

34 35 36 37
struct palmas_device_data {
	int ngpio;
};

38 39
static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
{
40
	struct palmas_gpio *pg = gpiochip_get_data(gc);
41 42 43
	struct palmas *palmas = pg->palmas;
	unsigned int val;
	int ret;
44 45 46 47 48
	unsigned int reg;
	int gpio16 = (offset/8);

	offset %= 8;
	reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
49

50
	ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
51
	if (ret < 0) {
52
		dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
53 54 55
		return ret;
	}

56 57 58 59 60 61
	if (val & BIT(offset))
		reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
	else
		reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;

	ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
62
	if (ret < 0) {
63
		dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
64 65 66 67 68 69 70 71
		return ret;
	}
	return !!(val & BIT(offset));
}

static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
			int value)
{
72
	struct palmas_gpio *pg = gpiochip_get_data(gc);
73 74
	struct palmas *palmas = pg->palmas;
	int ret;
75 76
	unsigned int reg;
	int gpio16 = (offset/8);
77

78 79 80 81
	offset %= 8;
	if (gpio16)
		reg = (value) ?
			PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
82
	else
83 84 85 86
		reg = (value) ?
			PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;

	ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
87
	if (ret < 0)
88
		dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
89 90 91 92 93
}

static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
				int value)
{
94
	struct palmas_gpio *pg = gpiochip_get_data(gc);
95 96
	struct palmas *palmas = pg->palmas;
	int ret;
97 98 99 100 101
	unsigned int reg;
	int gpio16 = (offset/8);

	offset %= 8;
	reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
102 103 104 105

	/* Set the initial value */
	palmas_gpio_set(gc, offset, value);

106 107
	ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
				BIT(offset), BIT(offset));
108
	if (ret < 0)
109 110
		dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
			ret);
111 112 113 114 115
	return ret;
}

static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
{
116
	struct palmas_gpio *pg = gpiochip_get_data(gc);
117 118
	struct palmas *palmas = pg->palmas;
	int ret;
119 120 121 122 123
	unsigned int reg;
	int gpio16 = (offset/8);

	offset %= 8;
	reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
124

125
	ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
126
	if (ret < 0)
127 128
		dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
			ret);
129 130 131 132 133
	return ret;
}

static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
{
134
	struct palmas_gpio *pg = gpiochip_get_data(gc);
135 136 137 138 139
	struct palmas *palmas = pg->palmas;

	return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
}

140 141 142 143 144 145 146 147
static const struct palmas_device_data palmas_dev_data = {
	.ngpio = 8,
};

static const struct palmas_device_data tps80036_dev_data = {
	.ngpio = 16,
};

148
static const struct of_device_id of_palmas_gpio_match[] = {
149 150 151 152 153 154 155 156
	{ .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
	{ .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
	{ .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
	{ .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
	{ },
};
MODULE_DEVICE_TABLE(of, of_palmas_gpio_match);

157 158 159 160 161 162
static int palmas_gpio_probe(struct platform_device *pdev)
{
	struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
	struct palmas_platform_data *palmas_pdata;
	struct palmas_gpio *palmas_gpio;
	int ret;
163 164 165 166
	const struct of_device_id *match;
	const struct palmas_device_data *dev_data;

	match = of_match_device(of_palmas_gpio_match, &pdev->dev);
167 168
	if (!match)
		return -ENODEV;
169 170 171
	dev_data = match->data;
	if (!dev_data)
		dev_data = &palmas_dev_data;
172 173 174

	palmas_gpio = devm_kzalloc(&pdev->dev,
				sizeof(*palmas_gpio), GFP_KERNEL);
175
	if (!palmas_gpio)
176 177 178 179 180
		return -ENOMEM;

	palmas_gpio->palmas = palmas;
	palmas_gpio->gpio_chip.owner = THIS_MODULE;
	palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
181
	palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
182
	palmas_gpio->gpio_chip.can_sleep = true;
183 184 185 186 187
	palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
	palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
	palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
	palmas_gpio->gpio_chip.set	= palmas_gpio_set;
	palmas_gpio->gpio_chip.get	= palmas_gpio_get;
188
	palmas_gpio->gpio_chip.parent = &pdev->dev;
189
#ifdef CONFIG_OF_GPIO
L
Laxman Dewangan 已提交
190
	palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
191 192 193 194 195 196 197
#endif
	palmas_pdata = dev_get_platdata(palmas->dev);
	if (palmas_pdata && palmas_pdata->gpio_base)
		palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
	else
		palmas_gpio->gpio_chip.base = -1;

198
	ret = gpiochip_add_data(&palmas_gpio->gpio_chip, palmas_gpio);
199 200 201 202 203 204 205 206 207 208 209 210 211
	if (ret < 0) {
		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
		return ret;
	}

	platform_set_drvdata(pdev, palmas_gpio);
	return ret;
}

static int palmas_gpio_remove(struct platform_device *pdev)
{
	struct palmas_gpio *palmas_gpio = platform_get_drvdata(pdev);

212 213
	gpiochip_remove(&palmas_gpio->gpio_chip);
	return 0;
214 215 216 217 218
}

static struct platform_driver palmas_gpio_driver = {
	.driver.name	= "palmas-gpio",
	.driver.owner	= THIS_MODULE,
L
Laxman Dewangan 已提交
219
	.driver.of_match_table = of_palmas_gpio_match,
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	.probe		= palmas_gpio_probe,
	.remove		= palmas_gpio_remove,
};

static int __init palmas_gpio_init(void)
{
	return platform_driver_register(&palmas_gpio_driver);
}
subsys_initcall(palmas_gpio_init);

static void __exit palmas_gpio_exit(void)
{
	platform_driver_unregister(&palmas_gpio_driver);
}
module_exit(palmas_gpio_exit);

MODULE_ALIAS("platform:palmas-gpio");
MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
MODULE_DESCRIPTION("GPIO driver for TI Palmas series PMICs");
MODULE_LICENSE("GPL v2");