blob: 513cfc5c8fb62703529c2e714f70b7bbdf857306 [file] [log] [blame]
Philipp Zabela1635b82008-04-09 19:20:34 +01001/*
2 * Support for the GPIO/IRQ expander chips present on several HTC phones.
3 * These are implemented in CPLD chips present on the board.
4 *
5 * Copyright (c) 2007 Kevin O'Connor <kevin@koconnor.net>
6 * Copyright (c) 2007 Philipp Zabel <philipp.zabel@gmail.com>
7 *
8 * This file may be distributed under the terms of the GNU GPL license.
9 */
10
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/io.h>
16#include <linux/spinlock.h>
17#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Philipp Zabela1635b82008-04-09 19:20:34 +010019#include <linux/module.h>
20#include <linux/mfd/htc-egpio.h>
21
22struct egpio_chip {
23 int reg_start;
24 int cached_values;
25 unsigned long is_out;
26 struct device *dev;
27 struct gpio_chip chip;
28};
29
30struct egpio_info {
31 spinlock_t lock;
32
33 /* iomem info */
34 void __iomem *base_addr;
35 int bus_shift; /* byte shift */
36 int reg_shift; /* bit shift */
37 int reg_mask;
38
39 /* irq info */
40 int ack_register;
41 int ack_write;
42 u16 irqs_enabled;
43 uint irq_start;
44 int nirqs;
45 uint chained_irq;
46
47 /* egpio info */
48 struct egpio_chip *chip;
49 int nchips;
50};
51
52static inline void egpio_writew(u16 value, struct egpio_info *ei, int reg)
53{
54 writew(value, ei->base_addr + (reg << ei->bus_shift));
55}
56
57static inline u16 egpio_readw(struct egpio_info *ei, int reg)
58{
59 return readw(ei->base_addr + (reg << ei->bus_shift));
60}
61
62/*
63 * IRQs
64 */
65
66static inline void ack_irqs(struct egpio_info *ei)
67{
68 egpio_writew(ei->ack_write, ei, ei->ack_register);
69 pr_debug("EGPIO ack - write %x to base+%x\n",
70 ei->ack_write, ei->ack_register << ei->bus_shift);
71}
72
Mark Brown949b9de2010-12-11 16:43:59 +000073static void egpio_ack(struct irq_data *data)
Philipp Zabela1635b82008-04-09 19:20:34 +010074{
75}
76
77/* There does not appear to be a way to proactively mask interrupts
78 * on the egpio chip itself. So, we simply ignore interrupts that
79 * aren't desired. */
Mark Brown949b9de2010-12-11 16:43:59 +000080static void egpio_mask(struct irq_data *data)
Philipp Zabela1635b82008-04-09 19:20:34 +010081{
Mark Brown949b9de2010-12-11 16:43:59 +000082 struct egpio_info *ei = irq_data_get_irq_chip_data(data);
83 ei->irqs_enabled &= ~(1 << (data->irq - ei->irq_start));
84 pr_debug("EGPIO mask %d %04x\n", data->irq, ei->irqs_enabled);
Philipp Zabela1635b82008-04-09 19:20:34 +010085}
Mark Brown949b9de2010-12-11 16:43:59 +000086
87static void egpio_unmask(struct irq_data *data)
Philipp Zabela1635b82008-04-09 19:20:34 +010088{
Mark Brown949b9de2010-12-11 16:43:59 +000089 struct egpio_info *ei = irq_data_get_irq_chip_data(data);
90 ei->irqs_enabled |= 1 << (data->irq - ei->irq_start);
91 pr_debug("EGPIO unmask %d %04x\n", data->irq, ei->irqs_enabled);
Philipp Zabela1635b82008-04-09 19:20:34 +010092}
93
94static struct irq_chip egpio_muxed_chip = {
Mark Brown949b9de2010-12-11 16:43:59 +000095 .name = "htc-egpio",
96 .irq_ack = egpio_ack,
97 .irq_mask = egpio_mask,
98 .irq_unmask = egpio_unmask,
Philipp Zabela1635b82008-04-09 19:20:34 +010099};
100
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200101static void egpio_handler(struct irq_desc *desc)
Philipp Zabela1635b82008-04-09 19:20:34 +0100102{
Thomas Gleixner77eda962011-03-25 11:12:28 +0000103 struct egpio_info *ei = irq_desc_get_handler_data(desc);
Philipp Zabela1635b82008-04-09 19:20:34 +0100104 int irqpin;
105
106 /* Read current pins. */
107 unsigned long readval = egpio_readw(ei, ei->ack_register);
108 pr_debug("IRQ reg: %x\n", (unsigned int)readval);
109 /* Ack/unmask interrupts. */
110 ack_irqs(ei);
111 /* Process all set pins. */
112 readval &= ei->irqs_enabled;
Akinobu Mita984b3f52010-03-05 13:41:37 -0800113 for_each_set_bit(irqpin, &readval, ei->nirqs) {
Philipp Zabela1635b82008-04-09 19:20:34 +0100114 /* Run irq handler */
115 pr_debug("got IRQ %d\n", irqpin);
Thomas Gleixner77eda962011-03-25 11:12:28 +0000116 generic_handle_irq(ei->irq_start + irqpin);
Philipp Zabela1635b82008-04-09 19:20:34 +0100117 }
118}
119
120int htc_egpio_get_wakeup_irq(struct device *dev)
121{
122 struct egpio_info *ei = dev_get_drvdata(dev);
123
124 /* Read current pins. */
125 u16 readval = egpio_readw(ei, ei->ack_register);
126 /* Ack/unmask interrupts. */
127 ack_irqs(ei);
128 /* Return first set pin. */
129 readval &= ei->irqs_enabled;
130 return ei->irq_start + ffs(readval) - 1;
131}
132EXPORT_SYMBOL(htc_egpio_get_wakeup_irq);
133
134static inline int egpio_pos(struct egpio_info *ei, int bit)
135{
136 return bit >> ei->reg_shift;
137}
138
139static inline int egpio_bit(struct egpio_info *ei, int bit)
140{
141 return 1 << (bit & ((1 << ei->reg_shift)-1));
142}
143
144/*
145 * Input pins
146 */
147
148static int egpio_get(struct gpio_chip *chip, unsigned offset)
149{
150 struct egpio_chip *egpio;
151 struct egpio_info *ei;
152 unsigned bit;
153 int reg;
154 int value;
155
156 pr_debug("egpio_get_value(%d)\n", chip->base + offset);
157
Linus Walleij8d5f0952016-03-30 10:48:03 +0200158 egpio = gpiochip_get_data(chip);
Philipp Zabela1635b82008-04-09 19:20:34 +0100159 ei = dev_get_drvdata(egpio->dev);
160 bit = egpio_bit(ei, offset);
161 reg = egpio->reg_start + egpio_pos(ei, offset);
162
163 value = egpio_readw(ei, reg);
164 pr_debug("readw(%p + %x) = %x\n",
165 ei->base_addr, reg << ei->bus_shift, value);
Linus Walleijf7d623662015-12-22 15:47:39 +0100166 return !!(value & bit);
Philipp Zabela1635b82008-04-09 19:20:34 +0100167}
168
169static int egpio_direction_input(struct gpio_chip *chip, unsigned offset)
170{
171 struct egpio_chip *egpio;
172
Linus Walleij8d5f0952016-03-30 10:48:03 +0200173 egpio = gpiochip_get_data(chip);
Philipp Zabela1635b82008-04-09 19:20:34 +0100174 return test_bit(offset, &egpio->is_out) ? -EINVAL : 0;
175}
176
177
178/*
179 * Output pins
180 */
181
182static void egpio_set(struct gpio_chip *chip, unsigned offset, int value)
183{
184 unsigned long flag;
185 struct egpio_chip *egpio;
186 struct egpio_info *ei;
187 unsigned bit;
188 int pos;
189 int reg;
190 int shift;
191
192 pr_debug("egpio_set(%s, %d(%d), %d)\n",
193 chip->label, offset, offset+chip->base, value);
194
Linus Walleij8d5f0952016-03-30 10:48:03 +0200195 egpio = gpiochip_get_data(chip);
Philipp Zabela1635b82008-04-09 19:20:34 +0100196 ei = dev_get_drvdata(egpio->dev);
197 bit = egpio_bit(ei, offset);
198 pos = egpio_pos(ei, offset);
199 reg = egpio->reg_start + pos;
200 shift = pos << ei->reg_shift;
201
202 pr_debug("egpio %s: reg %d = 0x%04x\n", value ? "set" : "clear",
203 reg, (egpio->cached_values >> shift) & ei->reg_mask);
204
205 spin_lock_irqsave(&ei->lock, flag);
206 if (value)
207 egpio->cached_values |= (1 << offset);
208 else
209 egpio->cached_values &= ~(1 << offset);
210 egpio_writew((egpio->cached_values >> shift) & ei->reg_mask, ei, reg);
211 spin_unlock_irqrestore(&ei->lock, flag);
212}
213
214static int egpio_direction_output(struct gpio_chip *chip,
215 unsigned offset, int value)
216{
217 struct egpio_chip *egpio;
218
Linus Walleij8d5f0952016-03-30 10:48:03 +0200219 egpio = gpiochip_get_data(chip);
Philipp Zabela1635b82008-04-09 19:20:34 +0100220 if (test_bit(offset, &egpio->is_out)) {
221 egpio_set(chip, offset, value);
222 return 0;
223 } else {
224 return -EINVAL;
225 }
226}
227
228static void egpio_write_cache(struct egpio_info *ei)
229{
230 int i;
231 struct egpio_chip *egpio;
232 int shift;
233
234 for (i = 0; i < ei->nchips; i++) {
235 egpio = &(ei->chip[i]);
236 if (!egpio->is_out)
237 continue;
238
239 for (shift = 0; shift < egpio->chip.ngpio;
240 shift += (1<<ei->reg_shift)) {
241
242 int reg = egpio->reg_start + egpio_pos(ei, shift);
243
244 if (!((egpio->is_out >> shift) & ei->reg_mask))
245 continue;
246
247 pr_debug("EGPIO: setting %x to %x, was %x\n", reg,
248 (egpio->cached_values >> shift) & ei->reg_mask,
249 egpio_readw(ei, reg));
250
251 egpio_writew((egpio->cached_values >> shift)
252 & ei->reg_mask, ei, reg);
253 }
254 }
255}
256
257
258/*
259 * Setup
260 */
261
262static int __init egpio_probe(struct platform_device *pdev)
263{
Jingoo Han334a41c2013-07-30 17:10:05 +0900264 struct htc_egpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Philipp Zabela1635b82008-04-09 19:20:34 +0100265 struct resource *res;
266 struct egpio_info *ei;
267 struct gpio_chip *chip;
268 unsigned int irq, irq_end;
269 int i;
270 int ret;
271
272 /* Initialize ei data structure. */
Lee Jones58645b32013-05-23 16:25:12 +0100273 ei = devm_kzalloc(&pdev->dev, sizeof(*ei), GFP_KERNEL);
Philipp Zabela1635b82008-04-09 19:20:34 +0100274 if (!ei)
275 return -ENOMEM;
276
277 spin_lock_init(&ei->lock);
278
279 /* Find chained irq */
280 ret = -EINVAL;
281 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
282 if (res)
283 ei->chained_irq = res->start;
284
285 /* Map egpio chip into virtual address space. */
286 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
287 if (!res)
288 goto fail;
Wei Yongjun3f9850f2013-06-19 10:47:06 +0800289 ei->base_addr = devm_ioremap_nocache(&pdev->dev, res->start,
290 resource_size(res));
Philipp Zabela1635b82008-04-09 19:20:34 +0100291 if (!ei->base_addr)
292 goto fail;
Samuel Ortiz504f97f2008-09-25 00:17:00 +0200293 pr_debug("EGPIO phys=%08x virt=%p\n", (u32)res->start, ei->base_addr);
Philipp Zabela1635b82008-04-09 19:20:34 +0100294
295 if ((pdata->bus_width != 16) && (pdata->bus_width != 32))
296 goto fail;
297 ei->bus_shift = fls(pdata->bus_width - 1) - 3;
298 pr_debug("bus_shift = %d\n", ei->bus_shift);
299
300 if ((pdata->reg_width != 8) && (pdata->reg_width != 16))
301 goto fail;
302 ei->reg_shift = fls(pdata->reg_width - 1);
303 pr_debug("reg_shift = %d\n", ei->reg_shift);
304
305 ei->reg_mask = (1 << pdata->reg_width) - 1;
306
307 platform_set_drvdata(pdev, ei);
308
309 ei->nchips = pdata->num_chips;
Lee Jones58645b32013-05-23 16:25:12 +0100310 ei->chip = devm_kzalloc(&pdev->dev,
311 sizeof(struct egpio_chip) * ei->nchips,
312 GFP_KERNEL);
Julia Lawall720fd662009-02-04 20:44:01 +0100313 if (!ei->chip) {
Philipp Zabela1635b82008-04-09 19:20:34 +0100314 ret = -ENOMEM;
315 goto fail;
316 }
317 for (i = 0; i < ei->nchips; i++) {
318 ei->chip[i].reg_start = pdata->chip[i].reg_start;
319 ei->chip[i].cached_values = pdata->chip[i].initial_values;
320 ei->chip[i].is_out = pdata->chip[i].direction;
321 ei->chip[i].dev = &(pdev->dev);
322 chip = &(ei->chip[i].chip);
323 chip->label = "htc-egpio";
Linus Walleij58383c782015-11-04 09:56:26 +0100324 chip->parent = &pdev->dev;
David Brownelld8f388d82008-07-25 01:46:07 -0700325 chip->owner = THIS_MODULE;
Philipp Zabela1635b82008-04-09 19:20:34 +0100326 chip->get = egpio_get;
327 chip->set = egpio_set;
328 chip->direction_input = egpio_direction_input;
329 chip->direction_output = egpio_direction_output;
330 chip->base = pdata->chip[i].gpio_base;
331 chip->ngpio = pdata->chip[i].num_gpios;
332
Linus Walleij8d5f0952016-03-30 10:48:03 +0200333 gpiochip_add_data(chip, &ei->chip[i]);
Philipp Zabela1635b82008-04-09 19:20:34 +0100334 }
335
336 /* Set initial pin values */
337 egpio_write_cache(ei);
338
339 ei->irq_start = pdata->irq_base;
340 ei->nirqs = pdata->num_irqs;
341 ei->ack_register = pdata->ack_register;
342
343 if (ei->chained_irq) {
344 /* Setup irq handlers */
345 ei->ack_write = 0xFFFF;
346 if (pdata->invert_acks)
347 ei->ack_write = 0;
348 irq_end = ei->irq_start + ei->nirqs;
349 for (irq = ei->irq_start; irq < irq_end; irq++) {
Thomas Gleixnerd6f7ce9f2011-03-25 11:12:35 +0000350 irq_set_chip_and_handler(irq, &egpio_muxed_chip,
351 handle_simple_irq);
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000352 irq_set_chip_data(irq, ei);
Rob Herring9bd09f32015-07-27 15:55:20 -0500353 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Philipp Zabela1635b82008-04-09 19:20:34 +0100354 }
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000355 irq_set_irq_type(ei->chained_irq, IRQ_TYPE_EDGE_RISING);
Thomas Gleixner073f7f92015-07-13 20:44:47 +0000356 irq_set_chained_handler_and_data(ei->chained_irq,
357 egpio_handler, ei);
Philipp Zabela1635b82008-04-09 19:20:34 +0100358 ack_irqs(ei);
359
360 device_init_wakeup(&pdev->dev, 1);
361 }
362
363 return 0;
364
365fail:
366 printk(KERN_ERR "EGPIO failed to setup\n");
Philipp Zabela1635b82008-04-09 19:20:34 +0100367 return ret;
368}
369
370static int __exit egpio_remove(struct platform_device *pdev)
371{
372 struct egpio_info *ei = platform_get_drvdata(pdev);
373 unsigned int irq, irq_end;
374
375 if (ei->chained_irq) {
376 irq_end = ei->irq_start + ei->nirqs;
377 for (irq = ei->irq_start; irq < irq_end; irq++) {
Thomas Gleixnerd6f7ce9f2011-03-25 11:12:35 +0000378 irq_set_chip_and_handler(irq, NULL, NULL);
Rob Herring9bd09f32015-07-27 15:55:20 -0500379 irq_set_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Philipp Zabela1635b82008-04-09 19:20:34 +0100380 }
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000381 irq_set_chained_handler(ei->chained_irq, NULL);
Philipp Zabela1635b82008-04-09 19:20:34 +0100382 device_init_wakeup(&pdev->dev, 0);
383 }
Philipp Zabela1635b82008-04-09 19:20:34 +0100384
385 return 0;
386}
387
388#ifdef CONFIG_PM
389static int egpio_suspend(struct platform_device *pdev, pm_message_t state)
390{
391 struct egpio_info *ei = platform_get_drvdata(pdev);
392
393 if (ei->chained_irq && device_may_wakeup(&pdev->dev))
394 enable_irq_wake(ei->chained_irq);
395 return 0;
396}
397
398static int egpio_resume(struct platform_device *pdev)
399{
400 struct egpio_info *ei = platform_get_drvdata(pdev);
401
402 if (ei->chained_irq && device_may_wakeup(&pdev->dev))
403 disable_irq_wake(ei->chained_irq);
404
405 /* Update registers from the cache, in case
406 the CPLD was powered off during suspend */
407 egpio_write_cache(ei);
408 return 0;
409}
410#else
411#define egpio_suspend NULL
412#define egpio_resume NULL
413#endif
414
415
416static struct platform_driver egpio_driver = {
417 .driver = {
418 .name = "htc-egpio",
419 },
420 .remove = __exit_p(egpio_remove),
421 .suspend = egpio_suspend,
422 .resume = egpio_resume,
423};
424
425static int __init egpio_init(void)
426{
427 return platform_driver_probe(&egpio_driver, egpio_probe);
428}
429
430static void __exit egpio_exit(void)
431{
432 platform_driver_unregister(&egpio_driver);
433}
434
435/* start early for dependencies */
436subsys_initcall(egpio_init);
437module_exit(egpio_exit)
438
439MODULE_LICENSE("GPL");
440MODULE_AUTHOR("Kevin O'Connor <kevin@koconnor.net>");