2019-05-28 20:10:04 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-01-03 14:46:59 +04:00
|
|
|
/*
|
|
|
|
* TI Palma series PMIC's GPIO driver.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
*
|
|
|
|
* Author: Laxman Dewangan <ldewangan@nvidia.com>
|
|
|
|
*/
|
|
|
|
|
2018-05-24 15:25:13 +03:00
|
|
|
#include <linux/gpio/driver.h>
|
2013-01-03 14:46:59 +04:00
|
|
|
#include <linux/kernel.h>
|
2016-04-01 21:49:36 +03:00
|
|
|
#include <linux/init.h>
|
2013-01-03 14:46:59 +04:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
struct palmas_device_data {
|
|
|
|
int ngpio;
|
|
|
|
};
|
|
|
|
|
2013-01-03 14:46:59 +04:00
|
|
|
static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
|
|
|
|
{
|
2015-12-07 13:18:17 +03:00
|
|
|
struct palmas_gpio *pg = gpiochip_get_data(gc);
|
2013-01-03 14:46:59 +04:00
|
|
|
struct palmas *palmas = pg->palmas;
|
|
|
|
unsigned int val;
|
|
|
|
int ret;
|
2013-09-03 18:28:03 +04:00
|
|
|
unsigned int reg;
|
|
|
|
int gpio16 = (offset/8);
|
|
|
|
|
|
|
|
offset %= 8;
|
|
|
|
reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
|
2013-01-03 14:46:59 +04:00
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
|
2013-01-03 14:46:59 +04:00
|
|
|
if (ret < 0) {
|
2015-11-04 11:56:26 +03:00
|
|
|
dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
|
2013-07-27 00:39:53 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
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);
|
2013-07-27 00:39:53 +04:00
|
|
|
if (ret < 0) {
|
2015-11-04 11:56:26 +03:00
|
|
|
dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
|
2013-01-03 14:46:59 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return !!(val & BIT(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
|
|
|
|
int value)
|
|
|
|
{
|
2015-12-07 13:18:17 +03:00
|
|
|
struct palmas_gpio *pg = gpiochip_get_data(gc);
|
2013-01-03 14:46:59 +04:00
|
|
|
struct palmas *palmas = pg->palmas;
|
|
|
|
int ret;
|
2013-09-03 18:28:03 +04:00
|
|
|
unsigned int reg;
|
|
|
|
int gpio16 = (offset/8);
|
2013-01-03 14:46:59 +04:00
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
offset %= 8;
|
|
|
|
if (gpio16)
|
|
|
|
reg = (value) ?
|
|
|
|
PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
|
2013-01-03 14:46:59 +04:00
|
|
|
else
|
2013-09-03 18:28:03 +04:00
|
|
|
reg = (value) ?
|
|
|
|
PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
|
|
|
|
|
|
|
|
ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
|
2013-01-03 14:46:59 +04:00
|
|
|
if (ret < 0)
|
2015-11-04 11:56:26 +03:00
|
|
|
dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
|
2013-01-03 14:46:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
|
|
|
|
int value)
|
|
|
|
{
|
2015-12-07 13:18:17 +03:00
|
|
|
struct palmas_gpio *pg = gpiochip_get_data(gc);
|
2013-01-03 14:46:59 +04:00
|
|
|
struct palmas *palmas = pg->palmas;
|
|
|
|
int ret;
|
2013-09-03 18:28:03 +04:00
|
|
|
unsigned int reg;
|
|
|
|
int gpio16 = (offset/8);
|
|
|
|
|
|
|
|
offset %= 8;
|
|
|
|
reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
|
2013-01-03 14:46:59 +04:00
|
|
|
|
|
|
|
/* Set the initial value */
|
|
|
|
palmas_gpio_set(gc, offset, value);
|
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
|
|
|
|
BIT(offset), BIT(offset));
|
2013-01-03 14:46:59 +04:00
|
|
|
if (ret < 0)
|
2015-11-04 11:56:26 +03:00
|
|
|
dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
|
|
|
|
ret);
|
2013-01-03 14:46:59 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
|
|
|
|
{
|
2015-12-07 13:18:17 +03:00
|
|
|
struct palmas_gpio *pg = gpiochip_get_data(gc);
|
2013-01-03 14:46:59 +04:00
|
|
|
struct palmas *palmas = pg->palmas;
|
|
|
|
int ret;
|
2013-09-03 18:28:03 +04:00
|
|
|
unsigned int reg;
|
|
|
|
int gpio16 = (offset/8);
|
|
|
|
|
|
|
|
offset %= 8;
|
|
|
|
reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
|
2013-01-03 14:46:59 +04:00
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
|
2013-01-03 14:46:59 +04:00
|
|
|
if (ret < 0)
|
2015-11-04 11:56:26 +03:00
|
|
|
dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
|
|
|
|
ret);
|
2013-01-03 14:46:59 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
|
|
|
|
{
|
2015-12-07 13:18:17 +03:00
|
|
|
struct palmas_gpio *pg = gpiochip_get_data(gc);
|
2013-01-03 14:46:59 +04:00
|
|
|
struct palmas *palmas = pg->palmas;
|
|
|
|
|
|
|
|
return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:28:03 +04:00
|
|
|
static const struct palmas_device_data palmas_dev_data = {
|
|
|
|
.ngpio = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct palmas_device_data tps80036_dev_data = {
|
|
|
|
.ngpio = 16,
|
|
|
|
};
|
|
|
|
|
2014-05-07 13:06:50 +04:00
|
|
|
static const struct of_device_id of_palmas_gpio_match[] = {
|
2013-09-03 18:28:03 +04:00
|
|
|
{ .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,},
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
2013-01-03 14:46:59 +04:00
|
|
|
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;
|
2013-09-03 18:28:03 +04:00
|
|
|
const struct palmas_device_data *dev_data;
|
|
|
|
|
2018-04-30 10:38:13 +03:00
|
|
|
dev_data = of_device_get_match_data(&pdev->dev);
|
2013-09-03 18:28:03 +04:00
|
|
|
if (!dev_data)
|
|
|
|
dev_data = &palmas_dev_data;
|
2013-01-03 14:46:59 +04:00
|
|
|
|
|
|
|
palmas_gpio = devm_kzalloc(&pdev->dev,
|
|
|
|
sizeof(*palmas_gpio), GFP_KERNEL);
|
2014-04-29 12:39:07 +04:00
|
|
|
if (!palmas_gpio)
|
2013-01-03 14:46:59 +04:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
palmas_gpio->palmas = palmas;
|
|
|
|
palmas_gpio->gpio_chip.owner = THIS_MODULE;
|
|
|
|
palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
|
2013-09-03 18:28:03 +04:00
|
|
|
palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
|
2013-12-04 17:42:46 +04:00
|
|
|
palmas_gpio->gpio_chip.can_sleep = true;
|
2013-01-03 14:46:59 +04:00
|
|
|
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;
|
2015-11-04 11:56:26 +03:00
|
|
|
palmas_gpio->gpio_chip.parent = &pdev->dev;
|
2013-01-03 14:46:59 +04:00
|
|
|
#ifdef CONFIG_OF_GPIO
|
2013-07-23 11:36:04 +04:00
|
|
|
palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
|
2013-01-03 14:46:59 +04:00
|
|
|
#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;
|
|
|
|
|
2016-02-22 15:13:28 +03:00
|
|
|
ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip,
|
|
|
|
palmas_gpio);
|
2013-01-03 14:46:59 +04:00
|
|
|
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 struct platform_driver palmas_gpio_driver = {
|
|
|
|
.driver.name = "palmas-gpio",
|
2013-07-23 11:36:04 +04:00
|
|
|
.driver.of_match_table = of_palmas_gpio_match,
|
2013-01-03 14:46:59 +04:00
|
|
|
.probe = palmas_gpio_probe,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init palmas_gpio_init(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&palmas_gpio_driver);
|
|
|
|
}
|
|
|
|
subsys_initcall(palmas_gpio_init);
|