tzh
2024-08-15 d4a1bd480003f3e1a0590bc46fbcb24f05652ca7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * 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/axp2101.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/sunxi-gpio.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
 
#define AXP858_GPIO1_FUNCTIONS        (0x3 << 5)
#define AXP858_GPIO1_FUNCTION_OUT_LOW    (1 << 5)
#define AXP858_GPIO1_FUNCTION_OUT_HIGH    (2 << 5)
#define AXP858_GPIO1_FUNCTION_INPUT        (3 << 5)
#define AXP858_GPIO1_VOL_MASK            0x1f
#define AXP858_GPIO2_FUNCTIONS        0x7
#define AXP858_GPIO2_FUNCTION_OUT_LOW    1
#define AXP858_GPIO2_FUNCTION_OUT_HIGH    2
#define AXP858_GPIO2_FUNCTION_INPUT        3
#define AXP858_GPIO2_VOL_MASK            0x3f
 
#define AXP858_GPIO1_IRQ_EN        (1 << 2)
#define AXP858_GPIO2_IRQ_EN        (1 << 5)
 
#define AXP_GPIO_BASE            510
 
struct axp20x_gpio {
   struct gpio_chip    chip;
   struct regmap        *regmap;
};
 
static int axp20x_gpio_get_reg(unsigned offset)
{
   switch (offset) {
   case 0:
       return AXP858_CLDO3_GPIO1_CTL;
   case 1:
       return AXP858_CLDO4_GPIO2_CTL;
   }
 
   return -EINVAL;
}
 
static int axp20x_gpio_input(struct gpio_chip *chip, unsigned offset)
{
   struct axp20x_gpio *gpio = gpiochip_get_data(chip);
   int reg;
   unsigned int val;
 
   reg = axp20x_gpio_get_reg(offset);
   if (reg < 0)
       return reg;
 
   regmap_read(gpio->regmap, reg, &val);
   val &= ~(offset ? AXP858_GPIO2_FUNCTIONS : AXP858_GPIO1_FUNCTIONS);
   val |= (offset ? AXP858_GPIO2_FUNCTION_INPUT : AXP858_GPIO1_FUNCTION_INPUT);
   regmap_write(gpio->regmap, reg, val);
   return 0;
//    return regmap_update_bits(gpio->regmap, reg,
//                  offset ? AXP858_GPIO2_FUNCTIONS : AXP858_GPIO1_FUNCTIONS,
//                  offset ?
//                  AXP858_GPIO2_FUNCTION_INPUT : AXP858_GPIO1_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_get_direction(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;
 
   /*
    * This shouldn't really happen if the pin is in use already,
    * or if it's not in use yet, it doesn't matter since we're
    * going to change the value soon anyway. Default to output.
    */
   if (offset)
       val &= AXP858_GPIO2_FUNCTIONS;
   else
       val = (val & AXP858_GPIO1_FUNCTIONS) >> 5;
 
 
   /*
    * The GPIO directions are the three lowest values.
    * 3 is input, 1 and 2 are output
    */
   return (val > 2 ? 1 : 0);
}
 
static int axp20x_gpio_output(struct gpio_chip *chip, unsigned offset,
                 int value)
{
   struct axp20x_gpio *gpio = gpiochip_get_data(chip);
   int reg;
   unsigned int val;
 
   reg = axp20x_gpio_get_reg(offset);
   if (reg < 0)
       return reg;
 
   regmap_read(gpio->regmap, reg, &val);
   val &= ~(offset ? AXP858_GPIO2_FUNCTIONS : AXP858_GPIO1_FUNCTIONS);
   val |= (value ? AXP858_GPIO1_FUNCTION_OUT_HIGH : AXP858_GPIO1_FUNCTION_OUT_LOW);
   regmap_write(gpio->regmap, reg, val);
   return 0;
}
 
static void axp20x_gpio_set(struct gpio_chip *chip, unsigned offset,
               int value)
{
   axp20x_gpio_output(chip, offset, value);
}
 
static int axp_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
       const struct of_phandle_args *gpiospec,
       u32 *flags)
{
   struct gpio_config *config = NULL;
   int pin;
 
   pr_debug("%s enter... gpiospec->args[0] = %d,\"\
           + \" gpiospec->args[1] = %d\n",
           __func__, gpiospec->args[0], gpiospec->args[1]);
 
   pin = gpiospec->args[1];
 
   if (pin > gc->ngpio)
       return -EINVAL;
 
   if (flags) {
       config = (struct gpio_config *)flags;
       config->gpio = gc->base + gpiospec->args[1];
       config->mul_sel = gpiospec->args[2];
       config->drv_level = gpiospec->args[3];
       config->pull = gpiospec->args[4];
       config->data = gpiospec->args[5];
   }
 
   return pin;
}
 
 
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            = AXP_GPIO_BASE;
   gpio->chip.can_sleep        = 0;
   gpio->chip.parent        = &pdev->dev;
   gpio->chip.label        = dev_name(&pdev->dev);
   gpio->chip.owner        = THIS_MODULE;
/*    gpio->chip.request        = axp_gpio_request;*/
/*    gpio->chip.free            = axp_gpio_free;*/
   gpio->chip.get            = axp20x_gpio_get;
   gpio->chip.get_direction    = axp20x_gpio_get_direction;
   gpio->chip.set            = axp20x_gpio_set;
   gpio->chip.direction_input    = axp20x_gpio_input;
   gpio->chip.direction_output    = axp20x_gpio_output;
   gpio->chip.of_xlate            = axp_pinctrl_gpio_of_xlate;
   gpio->chip.of_gpio_n_cells    = 6;
   gpio->chip.ngpio        = 2;
 
   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, "AXP858 GPIO driver loaded\n");
 
   return 0;
}
 
static const struct of_device_id axp20x_gpio_match[] = {
   { .compatible = "x-powers,axp858-gpio" },
   { }
};
MODULE_DEVICE_TABLE(of, axp20x_gpio_match);
 
static struct platform_driver axp20x_gpio_driver = {
   .probe        = axp20x_gpio_probe,
   .driver = {
       .name        = "axp858-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");