Merge remote-tracking branch 'gpio/for-next'
[deliverable/linux.git] / drivers / gpio / gpio-axp209.c
CommitLineData
f72f4b44
MR
1/*
2 * AXP20x GPIO driver
3 *
4 * Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
11
12#include <linux/bitops.h>
13#include <linux/device.h>
14#include <linux/gpio/driver.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/kernel.h>
18#include <linux/mfd/axp20x.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/slab.h>
24
25#define AXP20X_GPIO_FUNCTIONS 0x7
26#define AXP20X_GPIO_FUNCTION_OUT_LOW 0
27#define AXP20X_GPIO_FUNCTION_OUT_HIGH 1
28#define AXP20X_GPIO_FUNCTION_INPUT 2
29
30struct axp20x_gpio {
31 struct gpio_chip chip;
32 struct regmap *regmap;
33};
34
35static int axp20x_gpio_get_reg(unsigned offset)
36{
37 switch (offset) {
38 case 0:
39 return AXP20X_GPIO0_CTRL;
40 case 1:
41 return AXP20X_GPIO1_CTRL;
42 case 2:
43 return AXP20X_GPIO2_CTRL;
44 }
45
46 return -EINVAL;
47}
48
49static int axp20x_gpio_input(struct gpio_chip *chip, unsigned offset)
50{
51 struct axp20x_gpio *gpio = gpiochip_get_data(chip);
52 int reg;
53
54 reg = axp20x_gpio_get_reg(offset);
55 if (reg < 0)
56 return reg;
57
58 return regmap_update_bits(gpio->regmap, reg,
59 AXP20X_GPIO_FUNCTIONS,
60 AXP20X_GPIO_FUNCTION_INPUT);
61}
62
63static int axp20x_gpio_get(struct gpio_chip *chip, unsigned offset)
64{
65 struct axp20x_gpio *gpio = gpiochip_get_data(chip);
66 unsigned int val;
67 int reg, ret;
68
69 reg = axp20x_gpio_get_reg(offset);
70 if (reg < 0)
71 return reg;
72
73 ret = regmap_read(gpio->regmap, reg, &val);
74 if (ret)
75 return ret;
76
77 return !!(val & BIT(offset + 4));
78}
79
80static int axp20x_gpio_output(struct gpio_chip *chip, unsigned offset,
81 int value)
82{
83 struct axp20x_gpio *gpio = gpiochip_get_data(chip);
84 int reg;
85
86 reg = axp20x_gpio_get_reg(offset);
87 if (reg < 0)
88 return reg;
89
90 return regmap_update_bits(gpio->regmap, reg,
91 AXP20X_GPIO_FUNCTIONS,
92 value ? AXP20X_GPIO_FUNCTION_OUT_HIGH
93 : AXP20X_GPIO_FUNCTION_OUT_LOW);
94}
95
96static void axp20x_gpio_set(struct gpio_chip *chip, unsigned offset,
97 int value)
98{
99 axp20x_gpio_output(chip, offset, value);
100}
101
102static int axp20x_gpio_probe(struct platform_device *pdev)
103{
104 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
105 struct axp20x_gpio *gpio;
106 int ret;
107
108 if (!of_device_is_available(pdev->dev.of_node))
109 return -ENODEV;
110
111 if (!axp20x) {
112 dev_err(&pdev->dev, "Parent drvdata not set\n");
113 return -EINVAL;
114 }
115
116 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
117 if (!gpio)
118 return -ENOMEM;
119
120 gpio->chip.base = -1;
121 gpio->chip.can_sleep = true;
122 gpio->chip.parent = &pdev->dev;
123 gpio->chip.label = dev_name(&pdev->dev);
124 gpio->chip.owner = THIS_MODULE;
125 gpio->chip.get = axp20x_gpio_get;
126 gpio->chip.set = axp20x_gpio_set;
127 gpio->chip.direction_input = axp20x_gpio_input;
128 gpio->chip.direction_output = axp20x_gpio_output;
129 gpio->chip.ngpio = 3;
130
131 gpio->regmap = axp20x->regmap;
132
133 ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
134 if (ret) {
135 dev_err(&pdev->dev, "Failed to register GPIO chip\n");
136 return ret;
137 }
138
139 dev_info(&pdev->dev, "AXP209 GPIO driver loaded\n");
140
141 return 0;
142}
143
144static const struct of_device_id axp20x_gpio_match[] = {
145 { .compatible = "x-powers,axp209-gpio" },
146 { }
147};
148MODULE_DEVICE_TABLE(of, axp20x_gpio_match);
149
150static struct platform_driver axp20x_gpio_driver = {
151 .probe = axp20x_gpio_probe,
152 .driver = {
153 .name = "axp20x-gpio",
154 .of_match_table = axp20x_gpio_match,
155 },
156};
157
158module_platform_driver(axp20x_gpio_driver);
159
160MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
161MODULE_DESCRIPTION("AXP20x PMIC GPIO driver");
162MODULE_LICENSE("GPL");
This page took 0.031091 seconds and 5 git commands to generate.