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/init.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/gpio.h>
12 #include <linux/interrupt.h>
14 #include <linux/mfd/stmpe.h>
15 #include <linux/seq_file.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 /* No variant has more than 24 GPIOs */
25 #define CACHE_NR_BANKS (24 / 8)
28 struct gpio_chip chip
;
31 struct mutex irq_lock
;
33 /* Caches of interrupt control registers for bus_lock */
34 u8 regs
[CACHE_NR_REGS
][CACHE_NR_BANKS
];
35 u8 oldregs
[CACHE_NR_REGS
][CACHE_NR_BANKS
];
38 static int stmpe_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
40 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(chip
);
41 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
42 u8 reg
= stmpe
->regs
[STMPE_IDX_GPMR_LSB
] - (offset
/ 8);
43 u8 mask
= 1 << (offset
% 8);
46 ret
= stmpe_reg_read(stmpe
, reg
);
50 return !!(ret
& mask
);
53 static void stmpe_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
55 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(chip
);
56 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
57 int which
= val
? STMPE_IDX_GPSR_LSB
: STMPE_IDX_GPCR_LSB
;
58 u8 reg
= stmpe
->regs
[which
] - (offset
/ 8);
59 u8 mask
= 1 << (offset
% 8);
62 * Some variants have single register for gpio set/clear functionality.
63 * For them we need to write 0 to clear and 1 to set.
65 if (stmpe
->regs
[STMPE_IDX_GPSR_LSB
] == stmpe
->regs
[STMPE_IDX_GPCR_LSB
])
66 stmpe_set_bits(stmpe
, reg
, mask
, val
? mask
: 0);
68 stmpe_reg_write(stmpe
, reg
, mask
);
71 static int stmpe_gpio_direction_output(struct gpio_chip
*chip
,
72 unsigned offset
, int val
)
74 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(chip
);
75 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
76 u8 reg
= stmpe
->regs
[STMPE_IDX_GPDR_LSB
] - (offset
/ 8);
77 u8 mask
= 1 << (offset
% 8);
79 stmpe_gpio_set(chip
, offset
, val
);
81 return stmpe_set_bits(stmpe
, reg
, mask
, mask
);
84 static int stmpe_gpio_direction_input(struct gpio_chip
*chip
,
87 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(chip
);
88 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
89 u8 reg
= stmpe
->regs
[STMPE_IDX_GPDR_LSB
] - (offset
/ 8);
90 u8 mask
= 1 << (offset
% 8);
92 return stmpe_set_bits(stmpe
, reg
, mask
, 0);
95 static int stmpe_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
97 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(chip
);
98 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
100 if (stmpe_gpio
->norequest_mask
& (1 << offset
))
103 return stmpe_set_altfunc(stmpe
, 1 << offset
, STMPE_BLOCK_GPIO
);
106 static struct gpio_chip template_chip
= {
108 .owner
= THIS_MODULE
,
109 .direction_input
= stmpe_gpio_direction_input
,
110 .get
= stmpe_gpio_get
,
111 .direction_output
= stmpe_gpio_direction_output
,
112 .set
= stmpe_gpio_set
,
113 .request
= stmpe_gpio_request
,
117 static int stmpe_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
119 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
120 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(gc
);
121 int offset
= d
->hwirq
;
122 int regoffset
= offset
/ 8;
123 int mask
= 1 << (offset
% 8);
125 if (type
& IRQ_TYPE_LEVEL_LOW
|| type
& IRQ_TYPE_LEVEL_HIGH
)
128 /* STMPE801 doesn't have RE and FE registers */
129 if (stmpe_gpio
->stmpe
->partnum
== STMPE801
)
132 if (type
& IRQ_TYPE_EDGE_RISING
)
133 stmpe_gpio
->regs
[REG_RE
][regoffset
] |= mask
;
135 stmpe_gpio
->regs
[REG_RE
][regoffset
] &= ~mask
;
137 if (type
& IRQ_TYPE_EDGE_FALLING
)
138 stmpe_gpio
->regs
[REG_FE
][regoffset
] |= mask
;
140 stmpe_gpio
->regs
[REG_FE
][regoffset
] &= ~mask
;
145 static void stmpe_gpio_irq_lock(struct irq_data
*d
)
147 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
148 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(gc
);
150 mutex_lock(&stmpe_gpio
->irq_lock
);
153 static void stmpe_gpio_irq_sync_unlock(struct irq_data
*d
)
155 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
156 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(gc
);
157 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
158 int num_banks
= DIV_ROUND_UP(stmpe
->num_gpios
, 8);
159 static const u8 regmap
[] = {
160 [REG_RE
] = STMPE_IDX_GPRER_LSB
,
161 [REG_FE
] = STMPE_IDX_GPFER_LSB
,
162 [REG_IE
] = STMPE_IDX_IEGPIOR_LSB
,
166 for (i
= 0; i
< CACHE_NR_REGS
; i
++) {
167 /* STMPE801 doesn't have RE and FE registers */
168 if ((stmpe
->partnum
== STMPE801
) &&
172 for (j
= 0; j
< num_banks
; j
++) {
173 u8 old
= stmpe_gpio
->oldregs
[i
][j
];
174 u8
new = stmpe_gpio
->regs
[i
][j
];
179 stmpe_gpio
->oldregs
[i
][j
] = new;
180 stmpe_reg_write(stmpe
, stmpe
->regs
[regmap
[i
]] - j
, new);
184 mutex_unlock(&stmpe_gpio
->irq_lock
);
187 static void stmpe_gpio_irq_mask(struct irq_data
*d
)
189 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
190 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(gc
);
191 int offset
= d
->hwirq
;
192 int regoffset
= offset
/ 8;
193 int mask
= 1 << (offset
% 8);
195 stmpe_gpio
->regs
[REG_IE
][regoffset
] &= ~mask
;
198 static void stmpe_gpio_irq_unmask(struct irq_data
*d
)
200 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
201 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(gc
);
202 int offset
= d
->hwirq
;
203 int regoffset
= offset
/ 8;
204 int mask
= 1 << (offset
% 8);
206 stmpe_gpio
->regs
[REG_IE
][regoffset
] |= mask
;
209 static void stmpe_dbg_show_one(struct seq_file
*s
,
210 struct gpio_chip
*gc
,
211 unsigned offset
, unsigned gpio
)
213 struct stmpe_gpio
*stmpe_gpio
= gpiochip_get_data(gc
);
214 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
215 const char *label
= gpiochip_is_requested(gc
, offset
);
216 int num_banks
= DIV_ROUND_UP(stmpe
->num_gpios
, 8);
217 bool val
= !!stmpe_gpio_get(gc
, offset
);
218 u8 dir_reg
= stmpe
->regs
[STMPE_IDX_GPDR_LSB
] - (offset
/ 8);
219 u8 mask
= 1 << (offset
% 8);
223 ret
= stmpe_reg_read(stmpe
, dir_reg
);
226 dir
= !!(ret
& mask
);
229 seq_printf(s
, " gpio-%-3d (%-20.20s) out %s",
230 gpio
, label
?: "(none)",
233 u8 edge_det_reg
= stmpe
->regs
[STMPE_IDX_GPEDR_MSB
] + num_banks
- 1 - (offset
/ 8);
234 u8 rise_reg
= stmpe
->regs
[STMPE_IDX_GPRER_LSB
] - (offset
/ 8);
235 u8 fall_reg
= stmpe
->regs
[STMPE_IDX_GPFER_LSB
] - (offset
/ 8);
236 u8 irqen_reg
= stmpe
->regs
[STMPE_IDX_IEGPIOR_LSB
] - (offset
/ 8);
242 ret
= stmpe_reg_read(stmpe
, edge_det_reg
);
245 edge_det
= !!(ret
& mask
);
246 ret
= stmpe_reg_read(stmpe
, rise_reg
);
249 rise
= !!(ret
& mask
);
250 ret
= stmpe_reg_read(stmpe
, fall_reg
);
253 fall
= !!(ret
& mask
);
254 ret
= stmpe_reg_read(stmpe
, irqen_reg
);
257 irqen
= !!(ret
& mask
);
259 seq_printf(s
, " gpio-%-3d (%-20.20s) in %s %s %s%s%s",
260 gpio
, label
?: "(none)",
262 edge_det
? "edge-asserted" : "edge-inactive",
263 irqen
? "IRQ-enabled" : "",
264 rise
? " rising-edge-detection" : "",
265 fall
? " falling-edge-detection" : "");
269 static void stmpe_dbg_show(struct seq_file
*s
, struct gpio_chip
*gc
)
272 unsigned gpio
= gc
->base
;
274 for (i
= 0; i
< gc
->ngpio
; i
++, gpio
++) {
275 stmpe_dbg_show_one(s
, gc
, i
, gpio
);
280 static struct irq_chip stmpe_gpio_irq_chip
= {
281 .name
= "stmpe-gpio",
282 .irq_bus_lock
= stmpe_gpio_irq_lock
,
283 .irq_bus_sync_unlock
= stmpe_gpio_irq_sync_unlock
,
284 .irq_mask
= stmpe_gpio_irq_mask
,
285 .irq_unmask
= stmpe_gpio_irq_unmask
,
286 .irq_set_type
= stmpe_gpio_irq_set_type
,
289 static irqreturn_t
stmpe_gpio_irq(int irq
, void *dev
)
291 struct stmpe_gpio
*stmpe_gpio
= dev
;
292 struct stmpe
*stmpe
= stmpe_gpio
->stmpe
;
293 u8 statmsbreg
= stmpe
->regs
[STMPE_IDX_ISGPIOR_MSB
];
294 int num_banks
= DIV_ROUND_UP(stmpe
->num_gpios
, 8);
295 u8 status
[num_banks
];
299 ret
= stmpe_block_read(stmpe
, statmsbreg
, num_banks
, status
);
303 for (i
= 0; i
< num_banks
; i
++) {
304 int bank
= num_banks
- i
- 1;
305 unsigned int enabled
= stmpe_gpio
->regs
[REG_IE
][bank
];
306 unsigned int stat
= status
[i
];
313 int bit
= __ffs(stat
);
314 int line
= bank
* 8 + bit
;
315 int child_irq
= irq_find_mapping(stmpe_gpio
->chip
.irqdomain
,
318 handle_nested_irq(child_irq
);
322 stmpe_reg_write(stmpe
, statmsbreg
+ i
, status
[i
]);
324 /* Edge detect register is not present on 801 */
325 if (stmpe
->partnum
!= STMPE801
)
326 stmpe_reg_write(stmpe
, stmpe
->regs
[STMPE_IDX_GPEDR_MSB
]
333 static int stmpe_gpio_probe(struct platform_device
*pdev
)
335 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
336 struct device_node
*np
= pdev
->dev
.of_node
;
337 struct stmpe_gpio
*stmpe_gpio
;
341 irq
= platform_get_irq(pdev
, 0);
343 stmpe_gpio
= kzalloc(sizeof(struct stmpe_gpio
), GFP_KERNEL
);
347 mutex_init(&stmpe_gpio
->irq_lock
);
349 stmpe_gpio
->dev
= &pdev
->dev
;
350 stmpe_gpio
->stmpe
= stmpe
;
351 stmpe_gpio
->chip
= template_chip
;
352 stmpe_gpio
->chip
.ngpio
= stmpe
->num_gpios
;
353 stmpe_gpio
->chip
.parent
= &pdev
->dev
;
354 stmpe_gpio
->chip
.of_node
= np
;
355 stmpe_gpio
->chip
.base
= -1;
357 if (IS_ENABLED(CONFIG_DEBUG_FS
))
358 stmpe_gpio
->chip
.dbg_show
= stmpe_dbg_show
;
360 of_property_read_u32(np
, "st,norequest-mask",
361 &stmpe_gpio
->norequest_mask
);
365 "device configured in no-irq mode: "
366 "irqs are not available\n");
368 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_GPIO
);
372 ret
= gpiochip_add_data(&stmpe_gpio
->chip
, stmpe_gpio
);
374 dev_err(&pdev
->dev
, "unable to add gpiochip: %d\n", ret
);
379 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
380 stmpe_gpio_irq
, IRQF_ONESHOT
,
381 "stmpe-gpio", stmpe_gpio
);
383 dev_err(&pdev
->dev
, "unable to get irq: %d\n", ret
);
386 ret
= gpiochip_irqchip_add(&stmpe_gpio
->chip
,
387 &stmpe_gpio_irq_chip
,
393 "could not connect irqchip to gpiochip\n");
397 gpiochip_set_chained_irqchip(&stmpe_gpio
->chip
,
398 &stmpe_gpio_irq_chip
,
403 platform_set_drvdata(pdev
, stmpe_gpio
);
408 stmpe_disable(stmpe
, STMPE_BLOCK_GPIO
);
409 gpiochip_remove(&stmpe_gpio
->chip
);
415 static struct platform_driver stmpe_gpio_driver
= {
417 .suppress_bind_attrs
= true,
418 .name
= "stmpe-gpio",
419 .owner
= THIS_MODULE
,
421 .probe
= stmpe_gpio_probe
,
424 static int __init
stmpe_gpio_init(void)
426 return platform_driver_register(&stmpe_gpio_driver
);
428 subsys_initcall(stmpe_gpio_init
);