Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/gpio/gpio-da9052.c
26278 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* GPIO Driver for Dialog DA9052 PMICs.
4
*
5
* Copyright(c) 2011 Dialog Semiconductor Ltd.
6
*
7
* Author: David Dajun Chen <[email protected]>
8
*/
9
#include <linux/fs.h>
10
#include <linux/gpio/driver.h>
11
#include <linux/module.h>
12
#include <linux/platform_device.h>
13
#include <linux/syscalls.h>
14
#include <linux/uaccess.h>
15
16
#include <linux/mfd/da9052/da9052.h>
17
#include <linux/mfd/da9052/pdata.h>
18
#include <linux/mfd/da9052/reg.h>
19
20
#define DA9052_INPUT 1
21
#define DA9052_OUTPUT_OPENDRAIN 2
22
#define DA9052_OUTPUT_PUSHPULL 3
23
24
#define DA9052_SUPPLY_VDD_IO1 0
25
26
#define DA9052_DEBOUNCING_OFF 0
27
#define DA9052_DEBOUNCING_ON 1
28
29
#define DA9052_OUTPUT_LOWLEVEL 0
30
31
#define DA9052_ACTIVE_LOW 0
32
#define DA9052_ACTIVE_HIGH 1
33
34
#define DA9052_GPIO_MAX_PORTS_PER_REGISTER 8
35
#define DA9052_GPIO_SHIFT_COUNT(no) (no%8)
36
#define DA9052_GPIO_MASK_UPPER_NIBBLE 0xF0
37
#define DA9052_GPIO_MASK_LOWER_NIBBLE 0x0F
38
#define DA9052_GPIO_NIBBLE_SHIFT 4
39
#define DA9052_IRQ_GPI0 16
40
#define DA9052_GPIO_ODD_SHIFT 7
41
#define DA9052_GPIO_EVEN_SHIFT 3
42
43
struct da9052_gpio {
44
struct da9052 *da9052;
45
struct gpio_chip gp;
46
};
47
48
static unsigned char da9052_gpio_port_odd(unsigned offset)
49
{
50
return offset % 2;
51
}
52
53
static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset)
54
{
55
struct da9052_gpio *gpio = gpiochip_get_data(gc);
56
int da9052_port_direction = 0;
57
int ret;
58
59
ret = da9052_reg_read(gpio->da9052,
60
DA9052_GPIO_0_1_REG + (offset >> 1));
61
if (ret < 0)
62
return ret;
63
64
if (da9052_gpio_port_odd(offset)) {
65
da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN;
66
da9052_port_direction >>= 4;
67
} else {
68
da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN;
69
}
70
71
switch (da9052_port_direction) {
72
case DA9052_INPUT:
73
if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER)
74
ret = da9052_reg_read(gpio->da9052,
75
DA9052_STATUS_C_REG);
76
else
77
ret = da9052_reg_read(gpio->da9052,
78
DA9052_STATUS_D_REG);
79
if (ret < 0)
80
return ret;
81
return !!(ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)));
82
case DA9052_OUTPUT_PUSHPULL:
83
if (da9052_gpio_port_odd(offset))
84
return !!(ret & DA9052_GPIO_ODD_PORT_MODE);
85
else
86
return !!(ret & DA9052_GPIO_EVEN_PORT_MODE);
87
default:
88
return -EINVAL;
89
}
90
}
91
92
static int da9052_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
93
{
94
struct da9052_gpio *gpio = gpiochip_get_data(gc);
95
96
if (da9052_gpio_port_odd(offset))
97
return da9052_reg_update(gpio->da9052, (offset >> 1) +
98
DA9052_GPIO_0_1_REG,
99
DA9052_GPIO_ODD_PORT_MODE,
100
value << DA9052_GPIO_ODD_SHIFT);
101
102
return da9052_reg_update(gpio->da9052,
103
(offset >> 1) + DA9052_GPIO_0_1_REG,
104
DA9052_GPIO_EVEN_PORT_MODE,
105
value << DA9052_GPIO_EVEN_SHIFT);
106
}
107
108
static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
109
{
110
struct da9052_gpio *gpio = gpiochip_get_data(gc);
111
unsigned char register_value;
112
int ret;
113
114
/* Format: function - 2 bits type - 1 bit mode - 1 bit */
115
register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
116
DA9052_DEBOUNCING_ON << 3;
117
118
if (da9052_gpio_port_odd(offset))
119
ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
120
DA9052_GPIO_0_1_REG,
121
DA9052_GPIO_MASK_UPPER_NIBBLE,
122
(register_value <<
123
DA9052_GPIO_NIBBLE_SHIFT));
124
else
125
ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
126
DA9052_GPIO_0_1_REG,
127
DA9052_GPIO_MASK_LOWER_NIBBLE,
128
register_value);
129
130
return ret;
131
}
132
133
static int da9052_gpio_direction_output(struct gpio_chip *gc,
134
unsigned offset, int value)
135
{
136
struct da9052_gpio *gpio = gpiochip_get_data(gc);
137
unsigned char register_value;
138
int ret;
139
140
/* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
141
register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
142
value << 3;
143
144
if (da9052_gpio_port_odd(offset))
145
ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
146
DA9052_GPIO_0_1_REG,
147
DA9052_GPIO_MASK_UPPER_NIBBLE,
148
(register_value <<
149
DA9052_GPIO_NIBBLE_SHIFT));
150
else
151
ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
152
DA9052_GPIO_0_1_REG,
153
DA9052_GPIO_MASK_LOWER_NIBBLE,
154
register_value);
155
156
return ret;
157
}
158
159
static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
160
{
161
struct da9052_gpio *gpio = gpiochip_get_data(gc);
162
struct da9052 *da9052 = gpio->da9052;
163
164
int irq;
165
166
irq = regmap_irq_get_virq(da9052->irq_data, DA9052_IRQ_GPI0 + offset);
167
168
return irq;
169
}
170
171
static const struct gpio_chip reference_gp = {
172
.label = "da9052-gpio",
173
.owner = THIS_MODULE,
174
.get = da9052_gpio_get,
175
.set = da9052_gpio_set,
176
.direction_input = da9052_gpio_direction_input,
177
.direction_output = da9052_gpio_direction_output,
178
.to_irq = da9052_gpio_to_irq,
179
.can_sleep = true,
180
.ngpio = 16,
181
.base = -1,
182
};
183
184
static int da9052_gpio_probe(struct platform_device *pdev)
185
{
186
struct da9052_gpio *gpio;
187
struct da9052_pdata *pdata;
188
189
gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
190
if (!gpio)
191
return -ENOMEM;
192
193
gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
194
pdata = dev_get_platdata(gpio->da9052->dev);
195
196
gpio->gp = reference_gp;
197
if (pdata && pdata->gpio_base)
198
gpio->gp.base = pdata->gpio_base;
199
200
return devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
201
}
202
203
static struct platform_driver da9052_gpio_driver = {
204
.probe = da9052_gpio_probe,
205
.driver = {
206
.name = "da9052-gpio",
207
},
208
};
209
210
module_platform_driver(da9052_gpio_driver);
211
212
MODULE_AUTHOR("David Dajun Chen <[email protected]>");
213
MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
214
MODULE_LICENSE("GPL");
215
MODULE_ALIAS("platform:da9052-gpio");
216
217