• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 struct axp20x_gpio {
31 	struct gpio_chip	chip;
32 	struct regmap		*regmap;
33 };
34 
axp20x_gpio_get_reg(unsigned offset)35 static 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 
axp20x_gpio_input(struct gpio_chip * chip,unsigned offset)49 static 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 
axp20x_gpio_get(struct gpio_chip * chip,unsigned offset)63 static 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 
axp20x_gpio_get_direction(struct gpio_chip * chip,unsigned offset)80 static int axp20x_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
81 {
82 	struct axp20x_gpio *gpio = gpiochip_get_data(chip);
83 	unsigned int val;
84 	int reg, ret;
85 
86 	reg = axp20x_gpio_get_reg(offset);
87 	if (reg < 0)
88 		return reg;
89 
90 	ret = regmap_read(gpio->regmap, reg, &val);
91 	if (ret)
92 		return ret;
93 
94 	/*
95 	 * This shouldn't really happen if the pin is in use already,
96 	 * or if it's not in use yet, it doesn't matter since we're
97 	 * going to change the value soon anyway. Default to output.
98 	 */
99 	if ((val & AXP20X_GPIO_FUNCTIONS) > 2)
100 		return 0;
101 
102 	/*
103 	 * The GPIO directions are the three lowest values.
104 	 * 2 is input, 0 and 1 are output
105 	 */
106 	return val & 2;
107 }
108 
axp20x_gpio_output(struct gpio_chip * chip,unsigned offset,int value)109 static int axp20x_gpio_output(struct gpio_chip *chip, unsigned offset,
110 			      int value)
111 {
112 	struct axp20x_gpio *gpio = gpiochip_get_data(chip);
113 	int reg;
114 
115 	reg = axp20x_gpio_get_reg(offset);
116 	if (reg < 0)
117 		return reg;
118 
119 	return regmap_update_bits(gpio->regmap, reg,
120 				  AXP20X_GPIO_FUNCTIONS,
121 				  value ? AXP20X_GPIO_FUNCTION_OUT_HIGH
122 				  : AXP20X_GPIO_FUNCTION_OUT_LOW);
123 }
124 
axp20x_gpio_set(struct gpio_chip * chip,unsigned offset,int value)125 static void axp20x_gpio_set(struct gpio_chip *chip, unsigned offset,
126 			    int value)
127 {
128 	axp20x_gpio_output(chip, offset, value);
129 }
130 
axp20x_gpio_probe(struct platform_device * pdev)131 static int axp20x_gpio_probe(struct platform_device *pdev)
132 {
133 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
134 	struct axp20x_gpio *gpio;
135 	int ret;
136 
137 	if (!of_device_is_available(pdev->dev.of_node))
138 		return -ENODEV;
139 
140 	if (!axp20x) {
141 		dev_err(&pdev->dev, "Parent drvdata not set\n");
142 		return -EINVAL;
143 	}
144 
145 	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
146 	if (!gpio)
147 		return -ENOMEM;
148 
149 	gpio->chip.base			= -1;
150 	gpio->chip.can_sleep		= true;
151 	gpio->chip.parent		= &pdev->dev;
152 	gpio->chip.label		= dev_name(&pdev->dev);
153 	gpio->chip.owner		= THIS_MODULE;
154 	gpio->chip.get			= axp20x_gpio_get;
155 	gpio->chip.get_direction	= axp20x_gpio_get_direction;
156 	gpio->chip.set			= axp20x_gpio_set;
157 	gpio->chip.direction_input	= axp20x_gpio_input;
158 	gpio->chip.direction_output	= axp20x_gpio_output;
159 	gpio->chip.ngpio		= 3;
160 
161 	gpio->regmap = axp20x->regmap;
162 
163 	ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
164 	if (ret) {
165 		dev_err(&pdev->dev, "Failed to register GPIO chip\n");
166 		return ret;
167 	}
168 
169 	dev_info(&pdev->dev, "AXP209 GPIO driver loaded\n");
170 
171 	return 0;
172 }
173 
174 static const struct of_device_id axp20x_gpio_match[] = {
175 	{ .compatible = "x-powers,axp209-gpio" },
176 	{ }
177 };
178 MODULE_DEVICE_TABLE(of, axp20x_gpio_match);
179 
180 static struct platform_driver axp20x_gpio_driver = {
181 	.probe		= axp20x_gpio_probe,
182 	.driver = {
183 		.name		= "axp20x-gpio",
184 		.of_match_table	= axp20x_gpio_match,
185 	},
186 };
187 
188 module_platform_driver(axp20x_gpio_driver);
189 
190 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
191 MODULE_DESCRIPTION("AXP20x PMIC GPIO driver");
192 MODULE_LICENSE("GPL");
193