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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* MAXIM MAX77660 GPIO driver
*
* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
* Author: Laxman dewangan <ldewangan@nvidia.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/debugfs.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mfd/max77660/max77660-core.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/seq_file.h>
#include <linux/regmap.h>
#define GPIO_REG_ADDR(offset) (MAX77660_REG_CNFG_GPIO0 + offset)
struct max77660_gpio {
struct gpio_chip gpio_chip;
struct device *parent;
struct device *dev;
int gpio_irq;
int irq_base;
int gpio_base;
};
static const struct regmap_irq max77660_gpio_irqs[] = {
[MAX77660_IRQ_GPIO0 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE0,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 0,
},
[MAX77660_IRQ_GPIO1 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE1,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 1,
},
[MAX77660_IRQ_GPIO2 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE2,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 2,
},
[MAX77660_IRQ_GPIO3 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE3,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 3,
},
[MAX77660_IRQ_GPIO4 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE4,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 4,
},
[MAX77660_IRQ_GPIO5 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE5,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 5,
},
[MAX77660_IRQ_GPIO6 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE6,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 6,
},
[MAX77660_IRQ_GPIO7 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ1_LVL2_GPIO_EDGE7,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 0,
.type_reg_offset = 7,
},
[MAX77660_IRQ_GPIO8 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ2_LVL2_GPIO_EDGE8,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 1,
.type_reg_offset = 8,
},
[MAX77660_IRQ_GPIO9 - MAX77660_IRQ_GPIO0] = {
.mask = MAX77660_IRQ2_LVL2_GPIO_EDGE9,
.type_rising_mask = MAX77660_CNFG_GPIO_INT_RISING,
.type_falling_mask = MAX77660_CNFG_GPIO_INT_FALLING,
.reg_offset = 1,
.type_reg_offset = 9,
},
};
static struct regmap_irq_chip max77660_gpio_irq_chip = {
.name = "max77660-gpio",
.irqs = max77660_gpio_irqs,
.num_irqs = ARRAY_SIZE(max77660_gpio_irqs),
.num_regs = 2,
.num_type_reg = 10,
.irq_reg_stride = 1,
.type_reg_stride = 1,
.status_base = MAX77660_REG_IRQ1_LVL2_GPIO,
.type_base = MAX77660_REG_CNFG_GPIO0,
};
static inline struct max77660_gpio *to_max77660_gpio(struct gpio_chip *gpio)
{
return container_of(gpio, struct max77660_gpio, gpio_chip);
}
static int max77660_gpio_dir_input(struct gpio_chip *gpio, unsigned offset)
{
struct max77660_gpio *max77660_gpio = to_max77660_gpio(gpio);
struct device *dev = max77660_gpio->dev;
struct device *parent = max77660_gpio->parent;
int ret;
ret = max77660_reg_update(parent, MAX77660_PWR_SLAVE,
GPIO_REG_ADDR(offset), MAX77660_CNFG_GPIO_DIR_INPUT,
MAX77660_CNFG_GPIO_DIR_MASK);
if (ret < 0)
dev_err(dev, "CNFG_GPIOx dir update failed: %d\n", ret);
return ret;
}
static int max77660_gpio_get(struct gpio_chip *gpio, unsigned offset)
{
struct max77660_gpio *max77660_gpio = to_max77660_gpio(gpio);
struct device *dev = max77660_gpio->dev;
struct device *parent = max77660_gpio->parent;
u8 val;
int ret;
ret = max77660_reg_read(parent, MAX77660_PWR_SLAVE,
GPIO_REG_ADDR(offset), &val);
if (ret < 0) {
dev_err(dev, "CNFG_GPIOx read failed: %d\n", ret);
return ret;
}
return !!(val & MAX77660_CNFG_GPIO_INPUT_VAL_MASK);
}
static int max77660_gpio_dir_output(struct gpio_chip *gpio, unsigned offset,
int value)
{
struct max77660_gpio *max77660_gpio = to_max77660_gpio(gpio);
struct device *dev = max77660_gpio->dev;
struct device *parent = max77660_gpio->parent;
u8 val;
int ret;
if (value)
val = MAX77660_CNFG_GPIO_OUTPUT_VAL_HIGH;
else
val = MAX77660_CNFG_GPIO_OUTPUT_VAL_LOW;
ret = max77660_reg_update(parent, MAX77660_PWR_SLAVE,
GPIO_REG_ADDR(offset),
val, MAX77660_CNFG_GPIO_OUTPUT_VAL_MASK);
if (ret < 0) {
dev_err(dev, "CNFG_GPIOx val update failed: %d\n", ret);
return ret;
}
ret = max77660_reg_update(parent, MAX77660_PWR_SLAVE,
GPIO_REG_ADDR(offset), MAX77660_CNFG_GPIO_DIR_OUTPUT,
MAX77660_CNFG_GPIO_DIR_MASK);
if (ret < 0)
dev_err(dev, "CNFG_GPIOx dir update failed: %d\n", ret);
return ret;
}
static int max77660_gpio_set_debounce(struct gpio_chip *gpio,
unsigned offset, unsigned debounce)
{
struct max77660_gpio *max77660_gpio = to_max77660_gpio(gpio);
struct device *dev = max77660_gpio->dev;
struct device *parent = max77660_gpio->parent;
u8 val;
int ret;
/* ES 1.0 errata: GPIO1 debaunce does not worked in ES1.0 */
if (max77660_is_es_1_0(parent) && (offset == 1)) {
dev_err(dev, "ES1.0: GPIO1 Debaunce is not supported in HW\n");
return -EINVAL;
}
if (debounce == 0)
val = MAX77660_CNFG_GPIO_DBNC_None;
else if ((0 < debounce) && (debounce <= 8))
val = MAX77660_CNFG_GPIO_DBNC_8ms;
else if ((8 < debounce) && (debounce <= 16))
val = MAX77660_CNFG_GPIO_DBNC_16ms;
else if ((16 < debounce) && (debounce <= 32))
val = MAX77660_CNFG_GPIO_DBNC_32ms;
else {
dev_err(dev, "%s(): illegal value %u\n", __func__, debounce);
return -EINVAL;
}
ret = max77660_reg_update(parent, MAX77660_PWR_SLAVE,
GPIO_REG_ADDR(offset), val, MAX77660_CNFG_GPIO_DBNC_MASK);
if (ret < 0)
dev_err(dev, "CNFG_GPIOx debounce update failed: %d\n", ret);
return ret;
}
static void max77660_gpio_set(struct gpio_chip *gpio, unsigned offset,
int value)
{
struct max77660_gpio *max77660_gpio = to_max77660_gpio(gpio);
struct device *dev = max77660_gpio->dev;
struct device *parent = max77660_gpio->parent;
u8 val;
int ret;
if (value)
val = MAX77660_CNFG_GPIO_OUTPUT_VAL_HIGH;
else
val = MAX77660_CNFG_GPIO_OUTPUT_VAL_LOW;
ret = max77660_reg_update(parent, MAX77660_PWR_SLAVE,
GPIO_REG_ADDR(offset), val,
MAX77660_CNFG_GPIO_OUTPUT_VAL_MASK);
if (ret < 0)
dev_err(dev, "CNFG_GPIOx val update failed: %d\n", ret);
}
static int max77660_gpio_to_irq(struct gpio_chip *gpio, unsigned offset)
{
struct max77660_gpio *max77660_gpio = to_max77660_gpio(gpio);
return max77660_gpio->irq_base + offset;
}
static int max77660_gpio_irq_init(struct max77660_gpio *max77660_gpio,
struct max77660_platform_data *pdata)
{
struct device *parent = max77660_gpio->parent;
struct max77660_chip *chip = dev_get_drvdata(parent);
int ret;
max77660_gpio->irq_base = pdata->irq_base + MAX77660_IRQ_GPIO0;
ret = regmap_add_irq_chip(chip->rmap[MAX77660_PWR_SLAVE],
max77660_gpio->gpio_irq, IRQF_ONESHOT,
max77660_gpio->irq_base,
&max77660_gpio_irq_chip, &chip->gpio_irq_data);
if (ret < 0) {
dev_err(max77660_gpio->dev,
"Failed to add gpio irq_chip %d\n", ret);
return ret;
}
return 0;
}
static void max77660_gpio_irq_remove(struct max77660_gpio *max77660_gpio)
{
struct max77660_chip *chip;
chip = dev_get_drvdata(max77660_gpio->dev->parent);
regmap_del_irq_chip(max77660_gpio->gpio_irq,
chip->gpio_irq_data);
chip->gpio_irq_data = NULL;
}
static int __devinit max77660_gpio_probe(struct platform_device *pdev)
{
struct max77660_platform_data *pdata;
struct max77660_gpio *max77660_gpio;
int ret;
int gpio_irq;
pdata = dev_get_platdata(pdev->dev.parent);
if (!pdata) {
dev_err(&pdev->dev, "Platform data not found\n");
return -ENODEV;
}
gpio_irq = platform_get_irq(pdev, 0);
if (gpio_irq <= 0) {
dev_err(&pdev->dev, "Gpio interrupt is not available\n");
return -ENODEV;
}
max77660_gpio = devm_kzalloc(&pdev->dev,
sizeof(*max77660_gpio), GFP_KERNEL);
if (!max77660_gpio) {
dev_err(&pdev->dev, "Could not allocate max77660_gpio\n");
return -ENOMEM;
}
max77660_gpio->parent = pdev->dev.parent;
max77660_gpio->dev = &pdev->dev;
max77660_gpio->gpio_irq = gpio_irq;
max77660_gpio->gpio_chip.owner = THIS_MODULE;
max77660_gpio->gpio_chip.label = pdev->name;
max77660_gpio->gpio_chip.dev = &pdev->dev;
max77660_gpio->gpio_chip.direction_input = max77660_gpio_dir_input;
max77660_gpio->gpio_chip.get = max77660_gpio_get;
max77660_gpio->gpio_chip.direction_output = max77660_gpio_dir_output;
max77660_gpio->gpio_chip.set_debounce = max77660_gpio_set_debounce;
max77660_gpio->gpio_chip.set = max77660_gpio_set;
max77660_gpio->gpio_chip.to_irq = max77660_gpio_to_irq;
max77660_gpio->gpio_chip.ngpio = MAX77660_GPIO_NR;
max77660_gpio->gpio_chip.can_sleep = 1;
if (pdata->gpio_base)
max77660_gpio->gpio_chip.base = pdata->gpio_base;
else
max77660_gpio->gpio_chip.base = -1;
platform_set_drvdata(pdev, max77660_gpio);
ret = gpiochip_add(&max77660_gpio->gpio_chip);
if (ret < 0) {
dev_err(&pdev->dev, "gpio_init: Failed to add max77660_gpio\n");
return ret;
}
max77660_gpio->gpio_base = max77660_gpio->gpio_chip.base;
ret = max77660_gpio_irq_init(max77660_gpio, pdata);
if (ret < 0) {
dev_err(&pdev->dev, "gpio irq init failed: %d\n", ret);
goto fail;
}
return 0;
fail:
ret = gpiochip_remove(&max77660_gpio->gpio_chip);
return ret;
}
static int __devexit max77660_gpio_remove(struct platform_device *pdev)
{
struct max77660_gpio *max77660_gpio = platform_get_drvdata(pdev);
int ret;
max77660_gpio_irq_remove(max77660_gpio);
ret = gpiochip_remove(&max77660_gpio->gpio_chip);
if (ret < 0)
dev_err(max77660_gpio->dev,
"gpiochip_remove failed: %d\n", ret);
return ret;
}
static struct platform_driver max77660_gpio_driver = {
.driver.name = "max77660-gpio",
.driver.owner = THIS_MODULE,
.probe = max77660_gpio_probe,
.remove = __devexit_p(max77660_gpio_remove),
};
static int __init max77660_gpio_init(void)
{
return platform_driver_register(&max77660_gpio_driver);
}
subsys_initcall(max77660_gpio_init);
static void __exit max77660_gpio_exit(void)
{
platform_driver_unregister(&max77660_gpio_driver);
}
module_exit(max77660_gpio_exit);
MODULE_DESCRIPTION("GPIO interface for MAX77660 PMIC");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Maxim Integrated");
MODULE_ALIAS("platform:max77660-gpio");
|