2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License, version 2
5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/gpio.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
15 #include <linux/mfd/stmpe.h>
18 * These registers are modified under the irq bus lock and cached to avoid
19 * unnecessary writes in bus_sync_unlock.
21 enum { REG_RE
, REG_FE
, REG_IE
};
23 #define CACHE_NR_REGS 3
24 #define CACHE_NR_BANKS (STMPE_NR_GPIOS / 8)
27 struct gpio_chip chip
;
30 struct mutex irq_lock
;
34 /* Caches of interrupt control registers for bus_lock */
35 u8 regs
[CACHE_NR_REGS
][CACHE_NR_BANKS
];
36 u8 oldregs
[CACHE_NR_REGS
][CACHE_NR_BANKS
];
39 static inline struct stmpe_gpio
*to_stmpe_gpio(struct gpio_chip
*chip
)
41 return container_of(chip
, struct stmpe_gpio
, chip
);
44 static int stmpe_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
46 struct stmpe_gpio
*stmpe_gpio
= to_stmpe_gpio(chip
);
47 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
48 u8 reg
= stmpe
->regs
[STMPE_IDX_GPMR_LSB
] - (offset
/ 8);
49 u8 mask
= 1 << (offset
% 8);
52 ret
= stmpe_reg_read(stmpe
, reg
);
59 static void stmpe_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
61 struct stmpe_gpio
*stmpe_gpio
= to_stmpe_gpio(chip
);
62 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
63 int which
= val
? STMPE_IDX_GPSR_LSB
: STMPE_IDX_GPCR_LSB
;
64 u8 reg
= stmpe
->regs
[which
] - (offset
/ 8);
65 u8 mask
= 1 << (offset
% 8);
67 stmpe_reg_write(stmpe
, reg
, mask
);
70 static int stmpe_gpio_direction_output(struct gpio_chip
*chip
,
71 unsigned offset
, int val
)
73 struct stmpe_gpio
*stmpe_gpio
= to_stmpe_gpio(chip
);
74 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
75 u8 reg
= stmpe
->regs
[STMPE_IDX_GPDR_LSB
] - (offset
/ 8);
76 u8 mask
= 1 << (offset
% 8);
78 stmpe_gpio_set(chip
, offset
, val
);
80 return stmpe_set_bits(stmpe
, reg
, mask
, mask
);
83 static int stmpe_gpio_direction_input(struct gpio_chip
*chip
,
86 struct stmpe_gpio
*stmpe_gpio
= to_stmpe_gpio(chip
);
87 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
88 u8 reg
= stmpe
->regs
[STMPE_IDX_GPDR_LSB
] - (offset
/ 8);
89 u8 mask
= 1 << (offset
% 8);
91 return stmpe_set_bits(stmpe
, reg
, mask
, 0);
94 static int stmpe_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
96 struct stmpe_gpio
*stmpe_gpio
= to_stmpe_gpio(chip
);
98 return stmpe_gpio
->irq_base
+ offset
;
101 static int stmpe_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
103 struct stmpe_gpio
*stmpe_gpio
= to_stmpe_gpio(chip
);
104 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
106 return stmpe_set_altfunc(stmpe
, 1 << offset
, STMPE_BLOCK_GPIO
);
109 static struct gpio_chip template_chip
= {
111 .owner
= THIS_MODULE
,
112 .direction_input
= stmpe_gpio_direction_input
,
113 .get
= stmpe_gpio_get
,
114 .direction_output
= stmpe_gpio_direction_output
,
115 .set
= stmpe_gpio_set
,
116 .to_irq
= stmpe_gpio_to_irq
,
117 .request
= stmpe_gpio_request
,
121 static int stmpe_gpio_irq_set_type(unsigned int irq
, unsigned int type
)
123 struct stmpe_gpio
*stmpe_gpio
= get_irq_chip_data(irq
);
124 int offset
= irq
- stmpe_gpio
->irq_base
;
125 int regoffset
= offset
/ 8;
126 int mask
= 1 << (offset
% 8);
128 if (type
== IRQ_TYPE_LEVEL_LOW
|| type
== IRQ_TYPE_LEVEL_HIGH
)
131 if (type
== IRQ_TYPE_EDGE_RISING
)
132 stmpe_gpio
->regs
[REG_RE
][regoffset
] |= mask
;
134 stmpe_gpio
->regs
[REG_RE
][regoffset
] &= ~mask
;
136 if (type
== IRQ_TYPE_EDGE_FALLING
)
137 stmpe_gpio
->regs
[REG_FE
][regoffset
] |= mask
;
139 stmpe_gpio
->regs
[REG_FE
][regoffset
] &= ~mask
;
144 static void stmpe_gpio_irq_lock(unsigned int irq
)
146 struct stmpe_gpio
*stmpe_gpio
= get_irq_chip_data(irq
);
148 mutex_lock(&stmpe_gpio
->irq_lock
);
151 static void stmpe_gpio_irq_sync_unlock(unsigned int irq
)
153 struct stmpe_gpio
*stmpe_gpio
= get_irq_chip_data(irq
);
154 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
155 int num_banks
= DIV_ROUND_UP(stmpe
->num_gpios
, 8);
156 static const u8 regmap
[] = {
157 [REG_RE
] = STMPE_IDX_GPRER_LSB
,
158 [REG_FE
] = STMPE_IDX_GPFER_LSB
,
159 [REG_IE
] = STMPE_IDX_IEGPIOR_LSB
,
163 for (i
= 0; i
< CACHE_NR_REGS
; i
++) {
164 for (j
= 0; j
< num_banks
; j
++) {
165 u8 old
= stmpe_gpio
->oldregs
[i
][j
];
166 u8
new = stmpe_gpio
->regs
[i
][j
];
171 stmpe_gpio
->oldregs
[i
][j
] = new;
172 stmpe_reg_write(stmpe
, stmpe
->regs
[regmap
[i
]] - j
, new);
176 mutex_unlock(&stmpe_gpio
->irq_lock
);
179 static void stmpe_gpio_irq_mask(unsigned int irq
)
181 struct stmpe_gpio
*stmpe_gpio
= get_irq_chip_data(irq
);
182 int offset
= irq
- stmpe_gpio
->irq_base
;
183 int regoffset
= offset
/ 8;
184 int mask
= 1 << (offset
% 8);
186 stmpe_gpio
->regs
[REG_IE
][regoffset
] &= ~mask
;
189 static void stmpe_gpio_irq_unmask(unsigned int irq
)
191 struct stmpe_gpio
*stmpe_gpio
= get_irq_chip_data(irq
);
192 int offset
= irq
- stmpe_gpio
->irq_base
;
193 int regoffset
= offset
/ 8;
194 int mask
= 1 << (offset
% 8);
196 stmpe_gpio
->regs
[REG_IE
][regoffset
] |= mask
;
199 static struct irq_chip stmpe_gpio_irq_chip
= {
200 .name
= "stmpe-gpio",
201 .bus_lock
= stmpe_gpio_irq_lock
,
202 .bus_sync_unlock
= stmpe_gpio_irq_sync_unlock
,
203 .mask
= stmpe_gpio_irq_mask
,
204 .unmask
= stmpe_gpio_irq_unmask
,
205 .set_type
= stmpe_gpio_irq_set_type
,
208 static irqreturn_t
stmpe_gpio_irq(int irq
, void *dev
)
210 struct stmpe_gpio
*stmpe_gpio
= dev
;
211 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
212 u8 statmsbreg
= stmpe
->regs
[STMPE_IDX_ISGPIOR_MSB
];
213 int num_banks
= DIV_ROUND_UP(stmpe
->num_gpios
, 8);
214 u8 status
[num_banks
];
218 ret
= stmpe_block_read(stmpe
, statmsbreg
, num_banks
, status
);
222 for (i
= 0; i
< num_banks
; i
++) {
223 int bank
= num_banks
- i
- 1;
224 unsigned int enabled
= stmpe_gpio
->regs
[REG_IE
][bank
];
225 unsigned int stat
= status
[i
];
232 int bit
= __ffs(stat
);
233 int line
= bank
* 8 + bit
;
235 handle_nested_irq(stmpe_gpio
->irq_base
+ line
);
239 stmpe_reg_write(stmpe
, statmsbreg
+ i
, status
[i
]);
240 stmpe_reg_write(stmpe
, stmpe
->regs
[STMPE_IDX_GPEDR_MSB
] + i
,
247 static int __devinit
stmpe_gpio_irq_init(struct stmpe_gpio
*stmpe_gpio
)
249 int base
= stmpe_gpio
->irq_base
;
252 for (irq
= base
; irq
< base
+ stmpe_gpio
->chip
.ngpio
; irq
++) {
253 set_irq_chip_data(irq
, stmpe_gpio
);
254 set_irq_chip_and_handler(irq
, &stmpe_gpio_irq_chip
,
256 set_irq_nested_thread(irq
, 1);
258 set_irq_flags(irq
, IRQF_VALID
);
260 set_irq_noprobe(irq
);
267 static void stmpe_gpio_irq_remove(struct stmpe_gpio
*stmpe_gpio
)
269 int base
= stmpe_gpio
->irq_base
;
272 for (irq
= base
; irq
< base
+ stmpe_gpio
->chip
.ngpio
; irq
++) {
274 set_irq_flags(irq
, 0);
276 set_irq_chip_and_handler(irq
, NULL
, NULL
);
277 set_irq_chip_data(irq
, NULL
);
281 static int __devinit
stmpe_gpio_probe(struct platform_device
*pdev
)
283 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
284 struct stmpe_gpio_platform_data
*pdata
;
285 struct stmpe_gpio
*stmpe_gpio
;
289 pdata
= stmpe
->pdata
->gpio
;
293 irq
= platform_get_irq(pdev
, 0);
297 stmpe_gpio
= kzalloc(sizeof(struct stmpe_gpio
), GFP_KERNEL
);
301 mutex_init(&stmpe_gpio
->irq_lock
);
303 stmpe_gpio
->dev
= &pdev
->dev
;
304 stmpe_gpio
->stmpe
= stmpe
;
306 stmpe_gpio
->chip
= template_chip
;
307 stmpe_gpio
->chip
.ngpio
= stmpe
->num_gpios
;
308 stmpe_gpio
->chip
.dev
= &pdev
->dev
;
309 stmpe_gpio
->chip
.base
= pdata
? pdata
->gpio_base
: -1;
311 stmpe_gpio
->irq_base
= stmpe
->irq_base
+ STMPE_INT_GPIO(0);
313 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_GPIO
);
317 ret
= stmpe_gpio_irq_init(stmpe_gpio
);
321 ret
= request_threaded_irq(irq
, NULL
, stmpe_gpio_irq
, IRQF_ONESHOT
,
322 "stmpe-gpio", stmpe_gpio
);
324 dev_err(&pdev
->dev
, "unable to get irq: %d\n", ret
);
328 ret
= gpiochip_add(&stmpe_gpio
->chip
);
330 dev_err(&pdev
->dev
, "unable to add gpiochip: %d\n", ret
);
334 if (pdata
&& pdata
->setup
)
335 pdata
->setup(stmpe
, stmpe_gpio
->chip
.base
);
337 platform_set_drvdata(pdev
, stmpe_gpio
);
342 free_irq(irq
, stmpe_gpio
);
344 stmpe_gpio_irq_remove(stmpe_gpio
);
350 static int __devexit
stmpe_gpio_remove(struct platform_device
*pdev
)
352 struct stmpe_gpio
*stmpe_gpio
= platform_get_drvdata(pdev
);
353 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
354 struct stmpe_gpio_platform_data
*pdata
= stmpe
->pdata
->gpio
;
355 int irq
= platform_get_irq(pdev
, 0);
358 if (pdata
&& pdata
->remove
)
359 pdata
->remove(stmpe
, stmpe_gpio
->chip
.base
);
361 ret
= gpiochip_remove(&stmpe_gpio
->chip
);
363 dev_err(stmpe_gpio
->dev
,
364 "unable to remove gpiochip: %d\n", ret
);
368 stmpe_disable(stmpe
, STMPE_BLOCK_GPIO
);
370 free_irq(irq
, stmpe_gpio
);
371 stmpe_gpio_irq_remove(stmpe_gpio
);
372 platform_set_drvdata(pdev
, NULL
);
378 static struct platform_driver stmpe_gpio_driver
= {
379 .driver
.name
= "stmpe-gpio",
380 .driver
.owner
= THIS_MODULE
,
381 .probe
= stmpe_gpio_probe
,
382 .remove
= __devexit_p(stmpe_gpio_remove
),
385 static int __init
stmpe_gpio_init(void)
387 return platform_driver_register(&stmpe_gpio_driver
);
389 subsys_initcall(stmpe_gpio_init
);
391 static void __exit
stmpe_gpio_exit(void)
393 platform_driver_unregister(&stmpe_gpio_driver
);
395 module_exit(stmpe_gpio_exit
);
397 MODULE_LICENSE("GPL v2");
398 MODULE_DESCRIPTION("STMPExxxx GPIO driver");
399 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");