-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The AXP209 PMIC has a bunch of GPIOs accessible, that are usually used to control LEDs or backlight. Add a driver for them Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com> Acked-by: Rob Herring <robh@kernel.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
- Loading branch information
Maxime Ripard
authored and
Linus Walleij
committed
Aug 11, 2016
1 parent
e23efa3
commit f72f4b4
Showing
4 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
AXP209 GPIO controller | ||
|
||
This driver follows the usual GPIO bindings found in | ||
Documentation/devicetree/bindings/gpio/gpio.txt | ||
|
||
Required properties: | ||
- compatible: Should be "x-powers,axp209-gpio" | ||
- #gpio-cells: Should be two. The first cell is the pin number and the | ||
second is the GPIO flags. | ||
- gpio-controller: Marks the device node as a GPIO controller. | ||
|
||
This node must be a subnode of the axp20x PMIC, documented in | ||
Documentation/devicetree/bindings/mfd/axp20x.txt | ||
|
||
Example: | ||
|
||
axp209: pmic@34 { | ||
compatible = "x-powers,axp209"; | ||
reg = <0x34>; | ||
interrupt-parent = <&nmi_intc>; | ||
interrupts = <0 IRQ_TYPE_LEVEL_LOW>; | ||
interrupt-controller; | ||
#interrupt-cells = <1>; | ||
|
||
axp_gpio: gpio { | ||
compatible = "x-powers,axp209-gpio"; | ||
gpio-controller; | ||
#gpio-cells = <2>; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* AXP20x GPIO driver | ||
* | ||
* Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. | ||
*/ | ||
|
||
#include <linux/bitops.h> | ||
#include <linux/device.h> | ||
#include <linux/gpio/driver.h> | ||
#include <linux/init.h> | ||
#include <linux/interrupt.h> | ||
#include <linux/kernel.h> | ||
#include <linux/mfd/axp20x.h> | ||
#include <linux/module.h> | ||
#include <linux/of.h> | ||
#include <linux/platform_device.h> | ||
#include <linux/regmap.h> | ||
#include <linux/slab.h> | ||
|
||
#define AXP20X_GPIO_FUNCTIONS 0x7 | ||
#define AXP20X_GPIO_FUNCTION_OUT_LOW 0 | ||
#define AXP20X_GPIO_FUNCTION_OUT_HIGH 1 | ||
#define AXP20X_GPIO_FUNCTION_INPUT 2 | ||
|
||
struct axp20x_gpio { | ||
struct gpio_chip chip; | ||
struct regmap *regmap; | ||
}; | ||
|
||
static int axp20x_gpio_get_reg(unsigned offset) | ||
{ | ||
switch (offset) { | ||
case 0: | ||
return AXP20X_GPIO0_CTRL; | ||
case 1: | ||
return AXP20X_GPIO1_CTRL; | ||
case 2: | ||
return AXP20X_GPIO2_CTRL; | ||
} | ||
|
||
return -EINVAL; | ||
} | ||
|
||
static int axp20x_gpio_input(struct gpio_chip *chip, unsigned offset) | ||
{ | ||
struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
int reg; | ||
|
||
reg = axp20x_gpio_get_reg(offset); | ||
if (reg < 0) | ||
return reg; | ||
|
||
return regmap_update_bits(gpio->regmap, reg, | ||
AXP20X_GPIO_FUNCTIONS, | ||
AXP20X_GPIO_FUNCTION_INPUT); | ||
} | ||
|
||
static int axp20x_gpio_get(struct gpio_chip *chip, unsigned offset) | ||
{ | ||
struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
unsigned int val; | ||
int reg, ret; | ||
|
||
reg = axp20x_gpio_get_reg(offset); | ||
if (reg < 0) | ||
return reg; | ||
|
||
ret = regmap_read(gpio->regmap, reg, &val); | ||
if (ret) | ||
return ret; | ||
|
||
return !!(val & BIT(offset + 4)); | ||
} | ||
|
||
static int axp20x_gpio_output(struct gpio_chip *chip, unsigned offset, | ||
int value) | ||
{ | ||
struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
int reg; | ||
|
||
reg = axp20x_gpio_get_reg(offset); | ||
if (reg < 0) | ||
return reg; | ||
|
||
return regmap_update_bits(gpio->regmap, reg, | ||
AXP20X_GPIO_FUNCTIONS, | ||
value ? AXP20X_GPIO_FUNCTION_OUT_HIGH | ||
: AXP20X_GPIO_FUNCTION_OUT_LOW); | ||
} | ||
|
||
static void axp20x_gpio_set(struct gpio_chip *chip, unsigned offset, | ||
int value) | ||
{ | ||
axp20x_gpio_output(chip, offset, value); | ||
} | ||
|
||
static int axp20x_gpio_probe(struct platform_device *pdev) | ||
{ | ||
struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); | ||
struct axp20x_gpio *gpio; | ||
int ret; | ||
|
||
if (!of_device_is_available(pdev->dev.of_node)) | ||
return -ENODEV; | ||
|
||
if (!axp20x) { | ||
dev_err(&pdev->dev, "Parent drvdata not set\n"); | ||
return -EINVAL; | ||
} | ||
|
||
gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); | ||
if (!gpio) | ||
return -ENOMEM; | ||
|
||
gpio->chip.base = -1; | ||
gpio->chip.can_sleep = true; | ||
gpio->chip.parent = &pdev->dev; | ||
gpio->chip.label = dev_name(&pdev->dev); | ||
gpio->chip.owner = THIS_MODULE; | ||
gpio->chip.get = axp20x_gpio_get; | ||
gpio->chip.set = axp20x_gpio_set; | ||
gpio->chip.direction_input = axp20x_gpio_input; | ||
gpio->chip.direction_output = axp20x_gpio_output; | ||
gpio->chip.ngpio = 3; | ||
|
||
gpio->regmap = axp20x->regmap; | ||
|
||
ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); | ||
if (ret) { | ||
dev_err(&pdev->dev, "Failed to register GPIO chip\n"); | ||
return ret; | ||
} | ||
|
||
dev_info(&pdev->dev, "AXP209 GPIO driver loaded\n"); | ||
|
||
return 0; | ||
} | ||
|
||
static const struct of_device_id axp20x_gpio_match[] = { | ||
{ .compatible = "x-powers,axp209-gpio" }, | ||
{ } | ||
}; | ||
MODULE_DEVICE_TABLE(of, axp20x_gpio_match); | ||
|
||
static struct platform_driver axp20x_gpio_driver = { | ||
.probe = axp20x_gpio_probe, | ||
.driver = { | ||
.name = "axp20x-gpio", | ||
.of_match_table = axp20x_gpio_match, | ||
}, | ||
}; | ||
|
||
module_platform_driver(axp20x_gpio_driver); | ||
|
||
MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); | ||
MODULE_DESCRIPTION("AXP20x PMIC GPIO driver"); | ||
MODULE_LICENSE("GPL"); |