-
Notifications
You must be signed in to change notification settings - Fork 103
/
gpio-da9063.c
270 lines (220 loc) · 6.37 KB
/
gpio-da9063.c
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* GPIO Driver for Dialog DA9063 PMICs.
*
* Copyright(c) 2012 Dialog Semiconductor Ltd.
*
* Author: David Dajun Chen <dchen@diasemi.com>
*/
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/gpio/driver.h>
#include <linux/irqdomain.h>
#include <linux/regulator/consumer.h>
#include <linux/mfd/da9063/core.h>
#include <linux/mfd/da9063/registers.h>
#include <linux/regmap.h>
#define DA9063_VDD_IO 0x1
#define DA9063_GPI 0x1
#define DA9063_OPEN_DRAIN 0x2
#define DA9063_PUSH_PULL 0x3
#define DA9063_ACT_LOW 0x0
#define DA9063_ACT_HIGH 0x1
#define DA9063_PORT_MASK 0x3
#define DA9063_PORT_SHIFT(offset) (4 * (offset % 2))
/*
* Adjust the offset in gpio_get/gpio_set functions using
* REG_GPIO_MODE and REG_STATUS registers.
*/
#define DA9063_GPIO_SHIFT(offset) (offset % 8)
#define DA9063_INPUT DA9063_GPI
#define DA9063_OUTPUT DA9063_PUSH_PULL
struct da9063_gpio {
struct da9063 *da9063;
struct gpio_chip gp;
struct regulator *reg;
};
static inline struct da9063_gpio *to_da9063_gpio(struct gpio_chip *chip)
{
return container_of(chip, struct da9063_gpio, gp);
}
static int da9063_gpio_get(struct gpio_chip *gc, unsigned offset)
{
struct da9063_gpio *gpio = to_da9063_gpio(gc);
int gpio_direction = 0;
int ret;
unsigned int val, reg;
/* Get GPIO direction */
ret = regmap_read(gpio->da9063->regmap, (offset >> 1) +
DA9063_REG_GPIO_0_1, &val);
if (ret < 0)
return ret;
gpio_direction = val & (DA9063_PORT_MASK) << DA9063_PORT_SHIFT(offset);
gpio_direction >>= DA9063_PORT_SHIFT(offset);
switch (gpio_direction) {
case DA9063_INPUT:
reg = (offset >= 8) ? DA9063_REG_STATUS_C : DA9063_REG_STATUS_B;
ret = regmap_read(gpio->da9063->regmap, reg, &val);
if (ret < 0)
return ret;
break;
case DA9063_OUTPUT:
reg = (offset >= 8) ?
DA9063_REG_GPIO_MODE8_15 :
DA9063_REG_GPIO_MODE0_7;
ret = regmap_read(gpio->da9063->regmap, reg, &val);
if (ret < 0)
return ret;
}
return val & (1 << DA9063_GPIO_SHIFT(offset));
}
static void da9063_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
{
struct da9063_gpio *gpio = to_da9063_gpio(gc);
unsigned int reg = (offset >= 8) ?
DA9063_REG_GPIO_MODE8_15 :
DA9063_REG_GPIO_MODE0_7;
regmap_update_bits(gpio->da9063->regmap, reg,
1 << DA9063_GPIO_SHIFT(offset),
value << DA9063_GPIO_SHIFT(offset));
}
static int da9063_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
{
struct da9063_gpio *gpio = to_da9063_gpio(gc);
unsigned char reg_byte;
reg_byte = (DA9063_ACT_LOW | DA9063_GPI)
<< DA9063_PORT_SHIFT(offset);
return regmap_update_bits(gpio->da9063->regmap, (offset >> 1) +
DA9063_REG_GPIO_0_1,
DA9063_PORT_MASK <<
DA9063_PORT_SHIFT(offset),
reg_byte);
}
static int da9063_gpio_direction_output(struct gpio_chip *gc,
unsigned offset, int value)
{
struct da9063_gpio *gpio = to_da9063_gpio(gc);
unsigned char reg_byte;
int ret;
reg_byte = (DA9063_VDD_IO | DA9063_PUSH_PULL)
<< DA9063_PORT_SHIFT(offset);
ret = regmap_update_bits(gpio->da9063->regmap, (offset >> 1) +
DA9063_REG_GPIO_0_1,
DA9063_PORT_MASK <<
DA9063_PORT_SHIFT(offset),
reg_byte);
if (ret < 0)
return ret;
da9063_gpio_set(gc, offset, value);
return 0;
}
static int da9063_gpio_to_irq(struct gpio_chip *gc, u32 offset)
{
struct da9063_gpio *gpio = to_da9063_gpio(gc);
struct da9063 *da9063 = gpio->da9063;
return irq_find_mapping(da9063->irq_domain,
DA9063_IRQ_GPI0 + offset);
}
static const struct gpio_chip reference_gp = {
.label = "da9063-gpio",
.owner = THIS_MODULE,
.get = da9063_gpio_get,
.set = da9063_gpio_set,
.direction_input = da9063_gpio_direction_input,
.direction_output = da9063_gpio_direction_output,
.to_irq = da9063_gpio_to_irq,
.can_sleep = true,
.ngpio = 16,
.base = 240,
};
static const struct of_device_id da9063_gpio_dt_ids[] = {
{ .compatible = "dlg,da9063-gpio", },
{ /* sentinel */ }
};
static int da9063_reg_init(struct da9063_gpio *gpio)
{
int ret = -ENODEV;
gpio->reg = regulator_get(NULL, "gpio-ext-reg");
if (!IS_ERR(gpio->reg))
/* The regulator use count needs to be incremented.
* otherwise we get an unbalanced call if we call
* regulator_disable(). */
ret = regulator_enable(gpio->reg);
return ret;
}
static int da9063_gpio_probe(struct platform_device *pdev)
{
struct da9063_gpio *gpio;
int ret;
gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
if (!gpio)
return -ENOMEM;
gpio->da9063 = dev_get_drvdata(pdev->dev.parent);
if(gpio->da9063 == NULL)
return -EPROBE_DEFER;
gpio->gp = reference_gp;
gpio->gp.of_node = pdev->dev.of_node;
ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
if (ret < 0) {
dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
return ret;
}
platform_set_drvdata(pdev, gpio);
if (da9063_reg_init(gpio))
/* We should -EPROBE_DEFER, but there is machine code like
* the BT & wireless reset lines, that need this driver so
* we can't with the current machine code. */
gpio->reg = NULL;
return 0;
}
static int da9063_gpio_remove(struct platform_device *pdev)
{
struct da9063_gpio *gpio = platform_get_drvdata(pdev);
gpiochip_remove(&gpio->gp);
return 0;
}
static int da9063_gpio_suspend(struct platform_device *pdev, pm_message_t state)
{
struct da9063_gpio *gpio = platform_get_drvdata(pdev);
int ret = 0;
if (gpio->reg == NULL) {
/* Try again in case the probe() was too early */
if (da9063_reg_init(gpio))
gpio->reg = NULL;
}
if (gpio->reg)
ret = regulator_disable(gpio->reg);
return ret;
}
static int da9063_gpio_resume(struct platform_device *pdev)
{
struct da9063_gpio *gpio = platform_get_drvdata(pdev);
int ret = 0;
if (gpio->reg)
ret = regulator_enable(gpio->reg);
return ret;
}
static struct platform_driver da9063_gpio_driver = {
.probe = da9063_gpio_probe,
.remove = da9063_gpio_remove,
.suspend = da9063_gpio_suspend,
.resume = da9063_gpio_resume,
.driver = {
.name = "da9063-gpio",
.owner = THIS_MODULE,
.of_match_table = da9063_gpio_dt_ids,
},
};
static int da9063_gpio_init(void)
{
return platform_driver_register(&da9063_gpio_driver);
}
subsys_initcall(da9063_gpio_init);
static void da9063_gpio_exit(void)
{
platform_driver_unregister(&da9063_gpio_driver);
}
module_exit(da9063_gpio_exit);
MODULE_AUTHOR("Digi International <support@digi.com>");
MODULE_DESCRIPTION("DA9063 GPIO Device Driver");
MODULE_LICENSE("GPL");