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#include <linux/kernel.h>
29#include <linux/init.h>
30#include <linux/module.h>
31#include <linux/interrupt.h>
32#include <linux/platform_device.h>
33#include <linux/i2c.h>
34#include <linux/irq.h>
35#include <linux/spinlock.h>
36#include <linux/htcpld.h>
37#include <linux/gpio.h>
38#include <linux/slab.h>
39
40struct htcpld_chip {
41 spinlock_t lock;
42
43
44 u8 reset;
45 u8 addr;
46 struct device *dev;
47 struct i2c_client *client;
48
49
50 u8 cache_out;
51 struct gpio_chip chip_out;
52
53
54 u8 cache_in;
55 struct gpio_chip chip_in;
56
57 u16 irqs_enabled;
58 uint irq_start;
59 int nirqs;
60
61
62
63
64
65 struct work_struct set_val_work;
66};
67
68struct htcpld_data {
69
70 u16 irqs_enabled;
71 uint irq_start;
72 int nirqs;
73 uint chained_irq;
74 unsigned int int_reset_gpio_hi;
75 unsigned int int_reset_gpio_lo;
76
77
78 struct htcpld_chip *chip;
79 unsigned int nchips;
80};
81
82
83
84
85static void htcpld_mask(struct irq_data *data)
86{
87 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
88 chip->irqs_enabled &= ~(1 << (data->irq - chip->irq_start));
89 pr_debug("HTCPLD mask %d %04x\n", data->irq, chip->irqs_enabled);
90}
91static void htcpld_unmask(struct irq_data *data)
92{
93 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
94 chip->irqs_enabled |= 1 << (data->irq - chip->irq_start);
95 pr_debug("HTCPLD unmask %d %04x\n", data->irq, chip->irqs_enabled);
96}
97
98static int htcpld_set_type(struct irq_data *data, unsigned int flags)
99{
100 struct irq_desc *d = irq_to_desc(data->irq);
101
102 if (!d) {
103 pr_err("HTCPLD invalid IRQ: %d\n", data->irq);
104 return -EINVAL;
105 }
106
107 if (flags & ~IRQ_TYPE_SENSE_MASK)
108 return -EINVAL;
109
110
111 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))
112 return -EINVAL;
113
114 d->status &= ~IRQ_TYPE_SENSE_MASK;
115 d->status |= flags;
116
117 return 0;
118}
119
120static struct irq_chip htcpld_muxed_chip = {
121 .name = "htcpld",
122 .irq_mask = htcpld_mask,
123 .irq_unmask = htcpld_unmask,
124 .irq_set_type = htcpld_set_type,
125};
126
127
128
129
130
131
132static irqreturn_t htcpld_handler(int irq, void *dev)
133{
134 struct htcpld_data *htcpld = dev;
135 unsigned int i;
136 unsigned long flags;
137 int irqpin;
138 struct irq_desc *desc;
139
140 if (!htcpld) {
141 pr_debug("htcpld is null in ISR\n");
142 return IRQ_HANDLED;
143 }
144
145
146
147
148
149
150
151
152 for (i = 0; i < htcpld->nchips; i++) {
153 struct htcpld_chip *chip = &htcpld->chip[i];
154 struct i2c_client *client;
155 int val;
156 unsigned long uval, old_val;
157
158 if (!chip) {
159 pr_debug("chip %d is null in ISR\n", i);
160 continue;
161 }
162
163 if (chip->nirqs == 0)
164 continue;
165
166 client = chip->client;
167 if (!client) {
168 pr_debug("client %d is null in ISR\n", i);
169 continue;
170 }
171
172
173 val = i2c_smbus_read_byte_data(client, chip->cache_out);
174 if (val < 0) {
175
176 dev_warn(chip->dev, "Unable to read from chip: %d\n",
177 val);
178 continue;
179 }
180
181 uval = (unsigned long)val;
182
183 spin_lock_irqsave(&chip->lock, flags);
184
185
186 old_val = chip->cache_in;
187
188
189 chip->cache_in = uval;
190
191 spin_unlock_irqrestore(&chip->lock, flags);
192
193
194
195
196
197 for (irqpin = 0; irqpin < chip->nirqs; irqpin++) {
198 unsigned oldb, newb;
199 int flags;
200
201 irq = chip->irq_start + irqpin;
202 desc = irq_to_desc(irq);
203 flags = desc->status;
204
205
206
207 oldb = (old_val >> irqpin) & 1;
208 newb = (uval >> irqpin) & 1;
209
210 if ((!oldb && newb && (flags & IRQ_TYPE_EDGE_RISING)) ||
211 (oldb && !newb &&
212 (flags & IRQ_TYPE_EDGE_FALLING))) {
213 pr_debug("fire IRQ %d\n", irqpin);
214 desc->handle_irq(irq, desc);
215 }
216 }
217 }
218
219
220
221
222
223 if (htcpld->int_reset_gpio_hi)
224 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
225 if (htcpld->int_reset_gpio_lo)
226 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
227
228 return IRQ_HANDLED;
229}
230
231
232
233
234
235
236
237
238static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val)
239{
240 struct i2c_client *client;
241 struct htcpld_chip *chip_data;
242 unsigned long flags;
243
244 chip_data = container_of(chip, struct htcpld_chip, chip_out);
245 if (!chip_data)
246 return;
247
248 client = chip_data->client;
249 if (client == NULL)
250 return;
251
252 spin_lock_irqsave(&chip_data->lock, flags);
253 if (val)
254 chip_data->cache_out |= (1 << offset);
255 else
256 chip_data->cache_out &= ~(1 << offset);
257 spin_unlock_irqrestore(&chip_data->lock, flags);
258
259 schedule_work(&(chip_data->set_val_work));
260}
261
262static void htcpld_chip_set_ni(struct work_struct *work)
263{
264 struct htcpld_chip *chip_data;
265 struct i2c_client *client;
266
267 chip_data = container_of(work, struct htcpld_chip, set_val_work);
268 client = chip_data->client;
269 i2c_smbus_read_byte_data(client, chip_data->cache_out);
270}
271
272static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset)
273{
274 struct htcpld_chip *chip_data;
275 int val = 0;
276 int is_input = 0;
277
278
279 chip_data = container_of(chip, struct htcpld_chip, chip_out);
280 if (!chip_data) {
281
282 is_input = 1;
283 chip_data = container_of(chip, struct htcpld_chip, chip_in);
284 if (!chip_data)
285 return -EINVAL;
286 }
287
288
289 if (!is_input)
290
291 val = (chip_data->cache_out >> offset) & 1;
292 else
293
294 val = (chip_data->cache_in >> offset) & 1;
295
296 if (val)
297 return 1;
298 else
299 return 0;
300}
301
302static int htcpld_direction_output(struct gpio_chip *chip,
303 unsigned offset, int value)
304{
305 htcpld_chip_set(chip, offset, value);
306 return 0;
307}
308
309static int htcpld_direction_input(struct gpio_chip *chip,
310 unsigned offset)
311{
312
313
314
315
316 return (offset < chip->ngpio) ? 0 : -EINVAL;
317}
318
319static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset)
320{
321 struct htcpld_chip *chip_data;
322
323 chip_data = container_of(chip, struct htcpld_chip, chip_in);
324
325 if (offset < chip_data->nirqs)
326 return chip_data->irq_start + offset;
327 else
328 return -EINVAL;
329}
330
331static void htcpld_chip_reset(struct i2c_client *client)
332{
333 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
334 if (!chip_data)
335 return;
336
337 i2c_smbus_read_byte_data(
338 client, (chip_data->cache_out = chip_data->reset));
339}
340
341static int __devinit htcpld_setup_chip_irq(
342 struct platform_device *pdev,
343 int chip_index)
344{
345 struct htcpld_data *htcpld;
346 struct device *dev = &pdev->dev;
347 struct htcpld_core_platform_data *pdata;
348 struct htcpld_chip *chip;
349 struct htcpld_chip_platform_data *plat_chip_data;
350 unsigned int irq, irq_end;
351 int ret = 0;
352
353
354 pdata = dev->platform_data;
355 htcpld = platform_get_drvdata(pdev);
356 chip = &htcpld->chip[chip_index];
357 plat_chip_data = &pdata->chip[chip_index];
358
359
360 irq_end = chip->irq_start + chip->nirqs;
361 for (irq = chip->irq_start; irq < irq_end; irq++) {
362 set_irq_chip(irq, &htcpld_muxed_chip);
363 set_irq_chip_data(irq, chip);
364 set_irq_handler(irq, handle_simple_irq);
365#ifdef CONFIG_ARM
366 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
367#else
368 set_irq_probe(irq);
369#endif
370 }
371
372 return ret;
373}
374
375static int __devinit htcpld_register_chip_i2c(
376 struct platform_device *pdev,
377 int chip_index)
378{
379 struct htcpld_data *htcpld;
380 struct device *dev = &pdev->dev;
381 struct htcpld_core_platform_data *pdata;
382 struct htcpld_chip *chip;
383 struct htcpld_chip_platform_data *plat_chip_data;
384 struct i2c_adapter *adapter;
385 struct i2c_client *client;
386 struct i2c_board_info info;
387
388
389 pdata = dev->platform_data;
390 htcpld = platform_get_drvdata(pdev);
391 chip = &htcpld->chip[chip_index];
392 plat_chip_data = &pdata->chip[chip_index];
393
394 adapter = i2c_get_adapter(pdata->i2c_adapter_id);
395 if (adapter == NULL) {
396
397 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
398 plat_chip_data->addr, pdata->i2c_adapter_id);
399 return -ENODEV;
400 }
401
402 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
403 dev_warn(dev, "i2c adapter %d non-functional\n",
404 pdata->i2c_adapter_id);
405 return -EINVAL;
406 }
407
408 memset(&info, 0, sizeof(struct i2c_board_info));
409 info.addr = plat_chip_data->addr;
410 strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE);
411 info.platform_data = chip;
412
413
414 client = i2c_new_device(adapter, &info);
415 if (!client) {
416
417 dev_warn(dev, "Unable to add I2C device for 0x%x\n",
418 plat_chip_data->addr);
419 return -ENODEV;
420 }
421
422 i2c_set_clientdata(client, chip);
423 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%d", client->addr);
424 chip->client = client;
425
426
427 htcpld_chip_reset(client);
428 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
429
430 return 0;
431}
432
433static void __devinit htcpld_unregister_chip_i2c(
434 struct platform_device *pdev,
435 int chip_index)
436{
437 struct htcpld_data *htcpld;
438 struct htcpld_chip *chip;
439
440
441 htcpld = platform_get_drvdata(pdev);
442 chip = &htcpld->chip[chip_index];
443
444 if (chip->client)
445 i2c_unregister_device(chip->client);
446}
447
448static int __devinit htcpld_register_chip_gpio(
449 struct platform_device *pdev,
450 int chip_index)
451{
452 struct htcpld_data *htcpld;
453 struct device *dev = &pdev->dev;
454 struct htcpld_core_platform_data *pdata;
455 struct htcpld_chip *chip;
456 struct htcpld_chip_platform_data *plat_chip_data;
457 struct gpio_chip *gpio_chip;
458 int ret = 0;
459
460
461 pdata = dev->platform_data;
462 htcpld = platform_get_drvdata(pdev);
463 chip = &htcpld->chip[chip_index];
464 plat_chip_data = &pdata->chip[chip_index];
465
466
467 gpio_chip = &(chip->chip_out);
468 gpio_chip->label = "htcpld-out";
469 gpio_chip->dev = dev;
470 gpio_chip->owner = THIS_MODULE;
471 gpio_chip->get = htcpld_chip_get;
472 gpio_chip->set = htcpld_chip_set;
473 gpio_chip->direction_input = NULL;
474 gpio_chip->direction_output = htcpld_direction_output;
475 gpio_chip->base = plat_chip_data->gpio_out_base;
476 gpio_chip->ngpio = plat_chip_data->num_gpios;
477
478 gpio_chip = &(chip->chip_in);
479 gpio_chip->label = "htcpld-in";
480 gpio_chip->dev = dev;
481 gpio_chip->owner = THIS_MODULE;
482 gpio_chip->get = htcpld_chip_get;
483 gpio_chip->set = NULL;
484 gpio_chip->direction_input = htcpld_direction_input;
485 gpio_chip->direction_output = NULL;
486 gpio_chip->to_irq = htcpld_chip_to_irq;
487 gpio_chip->base = plat_chip_data->gpio_in_base;
488 gpio_chip->ngpio = plat_chip_data->num_gpios;
489
490
491 ret = gpiochip_add(&(chip->chip_out));
492 if (ret) {
493 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
494 plat_chip_data->addr, ret);
495 return ret;
496 }
497
498 ret = gpiochip_add(&(chip->chip_in));
499 if (ret) {
500 int error;
501
502 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n",
503 plat_chip_data->addr, ret);
504
505 error = gpiochip_remove(&(chip->chip_out));
506 if (error)
507 dev_warn(dev, "Error while trying to unregister gpio chip: %d\n", error);
508
509 return ret;
510 }
511
512 return 0;
513}
514
515static int __devinit htcpld_setup_chips(struct platform_device *pdev)
516{
517 struct htcpld_data *htcpld;
518 struct device *dev = &pdev->dev;
519 struct htcpld_core_platform_data *pdata;
520 int i;
521
522
523 pdata = dev->platform_data;
524 htcpld = platform_get_drvdata(pdev);
525
526
527 htcpld->nchips = pdata->num_chip;
528 htcpld->chip = kzalloc(sizeof(struct htcpld_chip) * htcpld->nchips,
529 GFP_KERNEL);
530 if (!htcpld->chip) {
531 dev_warn(dev, "Unable to allocate memory for chips\n");
532 return -ENOMEM;
533 }
534
535
536 for (i = 0; i < htcpld->nchips; i++) {
537 int ret;
538
539
540 htcpld->chip[i].reset = pdata->chip[i].reset;
541 htcpld->chip[i].cache_out = pdata->chip[i].reset;
542 htcpld->chip[i].cache_in = 0;
543 htcpld->chip[i].dev = dev;
544 htcpld->chip[i].irq_start = pdata->chip[i].irq_base;
545 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs;
546
547 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni);
548 spin_lock_init(&(htcpld->chip[i].lock));
549
550
551 if (htcpld->chained_irq) {
552 ret = htcpld_setup_chip_irq(pdev, i);
553 if (ret)
554 continue;
555 }
556
557
558 ret = htcpld_register_chip_i2c(pdev, i);
559 if (ret)
560 continue;
561
562
563
564 ret = htcpld_register_chip_gpio(pdev, i);
565 if (ret) {
566
567 htcpld_unregister_chip_i2c(pdev, i);
568 continue;
569 }
570
571 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
572 }
573
574 return 0;
575}
576
577static int __devinit htcpld_core_probe(struct platform_device *pdev)
578{
579 struct htcpld_data *htcpld;
580 struct device *dev = &pdev->dev;
581 struct htcpld_core_platform_data *pdata;
582 struct resource *res;
583 int ret = 0;
584
585 if (!dev)
586 return -ENODEV;
587
588 pdata = dev->platform_data;
589 if (!pdata) {
590 dev_warn(dev, "Platform data not found for htcpld core!\n");
591 return -ENXIO;
592 }
593
594 htcpld = kzalloc(sizeof(struct htcpld_data), GFP_KERNEL);
595 if (!htcpld)
596 return -ENOMEM;
597
598
599 ret = -EINVAL;
600 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
601 if (res) {
602 int flags;
603 htcpld->chained_irq = res->start;
604
605
606 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
607 ret = request_threaded_irq(htcpld->chained_irq,
608 NULL, htcpld_handler,
609 flags, pdev->name, htcpld);
610 if (ret) {
611 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
612 goto fail;
613 } else
614 device_init_wakeup(dev, 0);
615 }
616
617
618 platform_set_drvdata(pdev, htcpld);
619
620
621 ret = htcpld_setup_chips(pdev);
622 if (ret)
623 goto fail;
624
625
626 if (pdata->int_reset_gpio_hi) {
627 ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core");
628 if (ret) {
629
630
631
632
633 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
634 htcpld->int_reset_gpio_hi = 0;
635 } else {
636 htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi;
637 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
638 }
639 }
640
641 if (pdata->int_reset_gpio_lo) {
642 ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core");
643 if (ret) {
644
645
646
647
648 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
649 htcpld->int_reset_gpio_lo = 0;
650 } else {
651 htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo;
652 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
653 }
654 }
655
656 dev_info(dev, "Initialized successfully\n");
657 return 0;
658
659fail:
660 kfree(htcpld);
661 return ret;
662}
663
664
665static const struct i2c_device_id htcpld_chip_id[] = {
666 { "htcpld-chip", 0 },
667 { }
668};
669MODULE_DEVICE_TABLE(i2c, htcpld_chip_id);
670
671
672static struct i2c_driver htcpld_chip_driver = {
673 .driver = {
674 .name = "htcpld-chip",
675 },
676 .id_table = htcpld_chip_id,
677};
678
679
680static struct platform_driver htcpld_core_driver = {
681 .driver = {
682 .name = "i2c-htcpld",
683 },
684};
685
686static int __init htcpld_core_init(void)
687{
688 int ret;
689
690
691 ret = i2c_add_driver(&htcpld_chip_driver);
692 if (ret)
693 return ret;
694
695
696 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe);
697}
698
699static void __exit htcpld_core_exit(void)
700{
701 i2c_del_driver(&htcpld_chip_driver);
702 platform_driver_unregister(&htcpld_core_driver);
703}
704
705module_init(htcpld_core_init);
706module_exit(htcpld_core_exit);
707
708MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>");
709MODULE_DESCRIPTION("I2C HTC PLD Driver");
710MODULE_LICENSE("GPL");
711
712