hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/pinctrl/pinctrl-mcp23s08.c
....@@ -1,33 +1,23 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* MCP23S08 SPI/I2C GPIO driver */
23
4
+#include <linux/bitops.h>
35 #include <linux/kernel.h>
46 #include <linux/device.h>
57 #include <linux/mutex.h>
8
+#include <linux/mod_devicetable.h>
69 #include <linux/module.h>
7
-#include <linux/gpio.h>
8
-#include <linux/i2c.h>
9
-#include <linux/spi/spi.h>
10
-#include <linux/spi/mcp23s08.h>
10
+#include <linux/export.h>
11
+#include <linux/gpio/driver.h>
1112 #include <linux/slab.h>
1213 #include <asm/byteorder.h>
1314 #include <linux/interrupt.h>
14
-#include <linux/of_device.h>
1515 #include <linux/regmap.h>
1616 #include <linux/pinctrl/pinctrl.h>
1717 #include <linux/pinctrl/pinconf.h>
1818 #include <linux/pinctrl/pinconf-generic.h>
1919
20
-/*
21
- * MCP types supported by driver
22
- */
23
-#define MCP_TYPE_S08 0
24
-#define MCP_TYPE_S17 1
25
-#define MCP_TYPE_008 2
26
-#define MCP_TYPE_017 3
27
-#define MCP_TYPE_S18 4
28
-#define MCP_TYPE_018 5
29
-
30
-#define MCP_MAX_DEV_PER_CS 8
20
+#include "pinctrl-mcp23s08.h"
3121
3222 /* Registers are all 8 bits wide.
3323 *
....@@ -51,30 +41,6 @@
5141 #define MCP_INTCAP 0x08
5242 #define MCP_GPIO 0x09
5343 #define MCP_OLAT 0x0a
54
-
55
-struct mcp23s08;
56
-
57
-struct mcp23s08 {
58
- u8 addr;
59
- bool irq_active_high;
60
- bool reg_shift;
61
-
62
- u16 irq_rise;
63
- u16 irq_fall;
64
- int irq;
65
- bool irq_controller;
66
- int cached_gpio;
67
- /* lock protects regmap access with bypass/cache flags */
68
- struct mutex lock;
69
-
70
- struct gpio_chip chip;
71
-
72
- struct regmap *regmap;
73
- struct device *dev;
74
-
75
- struct pinctrl_dev *pctldev;
76
- struct pinctrl_desc pinctrl_desc;
77
-};
7844
7945 static const struct reg_default mcp23x08_defaults[] = {
8046 {.reg = MCP_IODIR, .def = 0xff},
....@@ -107,7 +73,7 @@
10773 .n_yes_ranges = 1,
10874 };
10975
110
-static const struct regmap_config mcp23x08_regmap = {
76
+const struct regmap_config mcp23x08_regmap = {
11177 .reg_bits = 8,
11278 .val_bits = 8,
11379
....@@ -119,6 +85,7 @@
11985 .cache_type = REGCACHE_FLAT,
12086 .max_register = MCP_OLAT,
12187 };
88
+EXPORT_SYMBOL_GPL(mcp23x08_regmap);
12289
12390 static const struct reg_default mcp23x17_defaults[] = {
12491 {.reg = MCP_IODIR << 1, .def = 0xffff},
....@@ -151,7 +118,7 @@
151118 .n_yes_ranges = 1,
152119 };
153120
154
-static const struct regmap_config mcp23x17_regmap = {
121
+const struct regmap_config mcp23x17_regmap = {
155122 .reg_bits = 8,
156123 .val_bits = 16,
157124
....@@ -164,6 +131,7 @@
164131 .cache_type = REGCACHE_FLAT,
165132 .val_format_endian = REGMAP_ENDIAN_LITTLE,
166133 };
134
+EXPORT_SYMBOL_GPL(mcp23x17_regmap);
167135
168136 static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
169137 {
....@@ -265,7 +233,6 @@
265233 status = (data & BIT(pin)) ? 1 : 0;
266234 break;
267235 default:
268
- dev_err(mcp->dev, "Invalid config param %04x\n", param);
269236 return -ENOTSUPP;
270237 }
271238
....@@ -292,7 +259,7 @@
292259 ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg);
293260 break;
294261 default:
295
- dev_err(mcp->dev, "Invalid config param %04x\n", param);
262
+ dev_dbg(mcp->dev, "Invalid config param %04x\n", param);
296263 return -ENOTSUPP;
297264 }
298265 }
....@@ -307,80 +274,6 @@
307274 };
308275
309276 /*----------------------------------------------------------------------*/
310
-
311
-#ifdef CONFIG_SPI_MASTER
312
-
313
-static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
314
-{
315
- struct mcp23s08 *mcp = context;
316
- struct spi_device *spi = to_spi_device(mcp->dev);
317
- struct spi_message m;
318
- struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
319
- { .tx_buf = data, .len = count, }, };
320
-
321
- spi_message_init(&m);
322
- spi_message_add_tail(&t[0], &m);
323
- spi_message_add_tail(&t[1], &m);
324
-
325
- return spi_sync(spi, &m);
326
-}
327
-
328
-static int mcp23sxx_spi_gather_write(void *context,
329
- const void *reg, size_t reg_size,
330
- const void *val, size_t val_size)
331
-{
332
- struct mcp23s08 *mcp = context;
333
- struct spi_device *spi = to_spi_device(mcp->dev);
334
- struct spi_message m;
335
- struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
336
- { .tx_buf = reg, .len = reg_size, },
337
- { .tx_buf = val, .len = val_size, }, };
338
-
339
- spi_message_init(&m);
340
- spi_message_add_tail(&t[0], &m);
341
- spi_message_add_tail(&t[1], &m);
342
- spi_message_add_tail(&t[2], &m);
343
-
344
- return spi_sync(spi, &m);
345
-}
346
-
347
-static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
348
- void *val, size_t val_size)
349
-{
350
- struct mcp23s08 *mcp = context;
351
- struct spi_device *spi = to_spi_device(mcp->dev);
352
- u8 tx[2];
353
-
354
- if (reg_size != 1)
355
- return -EINVAL;
356
-
357
- tx[0] = mcp->addr | 0x01;
358
- tx[1] = *((u8 *) reg);
359
-
360
- return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
361
-}
362
-
363
-static const struct regmap_bus mcp23sxx_spi_regmap = {
364
- .write = mcp23sxx_spi_write,
365
- .gather_write = mcp23sxx_spi_gather_write,
366
- .read = mcp23sxx_spi_read,
367
-};
368
-
369
-#endif /* CONFIG_SPI_MASTER */
370
-
371
-/*----------------------------------------------------------------------*/
372
-
373
-/* A given spi_device can represent up to eight mcp23sxx chips
374
- * sharing the same chipselect but using different addresses
375
- * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
376
- * Driver data holds all the per-chip data.
377
- */
378
-struct mcp23s08_driver_data {
379
- unsigned ngpio;
380
- struct mcp23s08 *mcp[8];
381
- struct mcp23s08 chip[];
382
-};
383
-
384277
385278 static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
386279 {
....@@ -561,7 +454,6 @@
561454 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
562455 struct mcp23s08 *mcp = gpiochip_get_data(gc);
563456 unsigned int pos = data->hwirq;
564
- int status = 0;
565457
566458 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
567459 mcp_set_bit(mcp, MCP_INTCON, pos, false);
....@@ -584,7 +476,7 @@
584476 } else
585477 return -EINVAL;
586478
587
- return status;
479
+ return 0;
588480 }
589481
590482 static void mcp23s08_irq_bus_lock(struct irq_data *data)
....@@ -606,15 +498,6 @@
606498
607499 mutex_unlock(&mcp->lock);
608500 }
609
-
610
-static struct irq_chip mcp23s08_irq_chip = {
611
- .name = "gpio-mcp23xxx",
612
- .irq_mask = mcp23s08_irq_mask,
613
- .irq_unmask = mcp23s08_irq_unmask,
614
- .irq_set_type = mcp23s08_irq_set_type,
615
- .irq_bus_lock = mcp23s08_irq_bus_lock,
616
- .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
617
-};
618501
619502 static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
620503 {
....@@ -639,242 +522,36 @@
639522 return 0;
640523 }
641524
642
-static int mcp23s08_irqchip_setup(struct mcp23s08 *mcp)
643
-{
644
- struct gpio_chip *chip = &mcp->chip;
645
- int err;
646
-
647
- err = gpiochip_irqchip_add_nested(chip,
648
- &mcp23s08_irq_chip,
649
- 0,
650
- handle_simple_irq,
651
- IRQ_TYPE_NONE);
652
- if (err) {
653
- dev_err(chip->parent,
654
- "could not connect irqchip to gpiochip: %d\n", err);
655
- return err;
656
- }
657
-
658
- gpiochip_set_nested_irqchip(chip,
659
- &mcp23s08_irq_chip,
660
- mcp->irq);
661
-
662
- return 0;
663
-}
664
-
665525 /*----------------------------------------------------------------------*/
666526
667
-#ifdef CONFIG_DEBUG_FS
668
-
669
-#include <linux/seq_file.h>
670
-
671
-/*
672
- * This compares the chip's registers with the register
673
- * cache and corrects any incorrectly set register. This
674
- * can be used to fix state for MCP23xxx, that temporary
675
- * lost its power supply.
676
- */
677
-#define MCP23S08_CONFIG_REGS 7
678
-static int __check_mcp23s08_reg_cache(struct mcp23s08 *mcp)
679
-{
680
- int cached[MCP23S08_CONFIG_REGS];
681
- int err = 0, i;
682
-
683
- /* read cached config registers */
684
- for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
685
- err = mcp_read(mcp, i, &cached[i]);
686
- if (err)
687
- goto out;
688
- }
689
-
690
- regcache_cache_bypass(mcp->regmap, true);
691
-
692
- for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
693
- int uncached;
694
- err = mcp_read(mcp, i, &uncached);
695
- if (err)
696
- goto out;
697
-
698
- if (uncached != cached[i]) {
699
- dev_err(mcp->dev, "restoring reg 0x%02x from 0x%04x to 0x%04x (power-loss?)\n",
700
- i, uncached, cached[i]);
701
- mcp_write(mcp, i, cached[i]);
702
- }
703
- }
704
-
705
-out:
706
- if (err)
707
- dev_err(mcp->dev, "read error: reg=%02x, err=%d", i, err);
708
- regcache_cache_bypass(mcp->regmap, false);
709
- return err;
710
-}
711
-
712
-/*
713
- * This shows more info than the generic gpio dump code:
714
- * pullups, deglitching, open drain drive.
715
- */
716
-static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
717
-{
718
- struct mcp23s08 *mcp;
719
- char bank;
720
- int t;
721
- unsigned mask;
722
- int iodir, gpio, gppu;
723
-
724
- mcp = gpiochip_get_data(chip);
725
-
726
- /* NOTE: we only handle one bank for now ... */
727
- bank = '0' + ((mcp->addr >> 1) & 0x7);
728
-
729
- mutex_lock(&mcp->lock);
730
-
731
- t = __check_mcp23s08_reg_cache(mcp);
732
- if (t) {
733
- seq_printf(s, " I/O Error\n");
734
- goto done;
735
- }
736
- t = mcp_read(mcp, MCP_IODIR, &iodir);
737
- if (t) {
738
- seq_printf(s, " I/O Error\n");
739
- goto done;
740
- }
741
- t = mcp_read(mcp, MCP_GPIO, &gpio);
742
- if (t) {
743
- seq_printf(s, " I/O Error\n");
744
- goto done;
745
- }
746
- t = mcp_read(mcp, MCP_GPPU, &gppu);
747
- if (t) {
748
- seq_printf(s, " I/O Error\n");
749
- goto done;
750
- }
751
-
752
- for (t = 0, mask = BIT(0); t < chip->ngpio; t++, mask <<= 1) {
753
- const char *label;
754
-
755
- label = gpiochip_is_requested(chip, t);
756
- if (!label)
757
- continue;
758
-
759
- seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s\n",
760
- chip->base + t, bank, t, label,
761
- (iodir & mask) ? "in " : "out",
762
- (gpio & mask) ? "hi" : "lo",
763
- (gppu & mask) ? "up" : " ");
764
- /* NOTE: ignoring the irq-related registers */
765
- }
766
-done:
767
- mutex_unlock(&mcp->lock);
768
-}
769
-
770
-#else
771
-#define mcp23s08_dbg_show NULL
772
-#endif
773
-
774
-/*----------------------------------------------------------------------*/
775
-
776
-static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
777
- void *data, unsigned addr, unsigned type,
778
- unsigned int base, int cs)
527
+int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
528
+ unsigned int addr, unsigned int type, unsigned int base)
779529 {
780530 int status, ret;
781531 bool mirror = false;
782532 bool open_drain = false;
783
- struct regmap_config *one_regmap_config = NULL;
784
- int raw_chip_address = (addr & ~0x40) >> 1;
785533
786534 mutex_init(&mcp->lock);
787535
788536 mcp->dev = dev;
789537 mcp->addr = addr;
538
+
790539 mcp->irq_active_high = false;
540
+ mcp->irq_chip.name = dev_name(dev);
541
+ mcp->irq_chip.irq_mask = mcp23s08_irq_mask;
542
+ mcp->irq_chip.irq_unmask = mcp23s08_irq_unmask;
543
+ mcp->irq_chip.irq_set_type = mcp23s08_irq_set_type;
544
+ mcp->irq_chip.irq_bus_lock = mcp23s08_irq_bus_lock;
545
+ mcp->irq_chip.irq_bus_sync_unlock = mcp23s08_irq_bus_unlock;
791546
792547 mcp->chip.direction_input = mcp23s08_direction_input;
793548 mcp->chip.get = mcp23s08_get;
794549 mcp->chip.direction_output = mcp23s08_direction_output;
795550 mcp->chip.set = mcp23s08_set;
796
- mcp->chip.dbg_show = mcp23s08_dbg_show;
797551 #ifdef CONFIG_OF_GPIO
798552 mcp->chip.of_gpio_n_cells = 2;
799553 mcp->chip.of_node = dev->of_node;
800554 #endif
801
-
802
- switch (type) {
803
-#ifdef CONFIG_SPI_MASTER
804
- case MCP_TYPE_S08:
805
- case MCP_TYPE_S17:
806
- switch (type) {
807
- case MCP_TYPE_S08:
808
- one_regmap_config =
809
- devm_kmemdup(dev, &mcp23x08_regmap,
810
- sizeof(struct regmap_config), GFP_KERNEL);
811
- mcp->reg_shift = 0;
812
- mcp->chip.ngpio = 8;
813
- mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL,
814
- "mcp23s08.%d", raw_chip_address);
815
- break;
816
- case MCP_TYPE_S17:
817
- one_regmap_config =
818
- devm_kmemdup(dev, &mcp23x17_regmap,
819
- sizeof(struct regmap_config), GFP_KERNEL);
820
- mcp->reg_shift = 1;
821
- mcp->chip.ngpio = 16;
822
- mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL,
823
- "mcp23s17.%d", raw_chip_address);
824
- break;
825
- }
826
- if (!one_regmap_config)
827
- return -ENOMEM;
828
-
829
- one_regmap_config->name = devm_kasprintf(dev, GFP_KERNEL, "%d", raw_chip_address);
830
- mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
831
- one_regmap_config);
832
- break;
833
-
834
- case MCP_TYPE_S18:
835
- one_regmap_config =
836
- devm_kmemdup(dev, &mcp23x17_regmap,
837
- sizeof(struct regmap_config), GFP_KERNEL);
838
- if (!one_regmap_config)
839
- return -ENOMEM;
840
- mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
841
- one_regmap_config);
842
- mcp->reg_shift = 1;
843
- mcp->chip.ngpio = 16;
844
- mcp->chip.label = "mcp23s18";
845
- break;
846
-#endif /* CONFIG_SPI_MASTER */
847
-
848
-#if IS_ENABLED(CONFIG_I2C)
849
- case MCP_TYPE_008:
850
- mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
851
- mcp->reg_shift = 0;
852
- mcp->chip.ngpio = 8;
853
- mcp->chip.label = "mcp23008";
854
- break;
855
-
856
- case MCP_TYPE_017:
857
- mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
858
- mcp->reg_shift = 1;
859
- mcp->chip.ngpio = 16;
860
- mcp->chip.label = "mcp23017";
861
- break;
862
-
863
- case MCP_TYPE_018:
864
- mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
865
- mcp->reg_shift = 1;
866
- mcp->chip.ngpio = 16;
867
- mcp->chip.label = "mcp23018";
868
- break;
869
-#endif /* CONFIG_I2C */
870
-
871
- default:
872
- dev_err(dev, "invalid device type (%d)\n", type);
873
- return -EINVAL;
874
- }
875
-
876
- if (IS_ERR(mcp->regmap))
877
- return PTR_ERR(mcp->regmap);
878555
879556 mcp->chip.base = base;
880557 mcp->chip.can_sleep = true;
....@@ -887,11 +564,7 @@
887564
888565 ret = mcp_read(mcp, MCP_IOCON, &status);
889566 if (ret < 0)
890
- goto fail;
891
-
892
- ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
893
- if (ret < 0)
894
- goto fail;
567
+ return dev_err_probe(dev, ret, "can't identify chip %d\n", addr);
895568
896569 mcp->irq_controller =
897570 device_property_read_bool(dev, "interrupt-controller");
....@@ -925,23 +598,26 @@
925598
926599 ret = mcp_write(mcp, MCP_IOCON, status);
927600 if (ret < 0)
928
- goto fail;
601
+ return dev_err_probe(dev, ret, "can't write IOCON %d\n", addr);
929602 }
930603
931604 if (mcp->irq && mcp->irq_controller) {
932
- ret = mcp23s08_irqchip_setup(mcp);
933
- if (ret)
934
- goto fail;
605
+ struct gpio_irq_chip *girq = &mcp->chip.irq;
606
+
607
+ girq->chip = &mcp->irq_chip;
608
+ /* This will let us handle the parent IRQ in the driver */
609
+ girq->parent_handler = NULL;
610
+ girq->num_parents = 0;
611
+ girq->parents = NULL;
612
+ girq->default_type = IRQ_TYPE_NONE;
613
+ girq->handler = handle_simple_irq;
614
+ girq->threaded = true;
935615 }
936616
937
- if (one_regmap_config) {
938
- mcp->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL,
939
- "mcp23xxx-pinctrl.%d", raw_chip_address);
940
- if (!mcp->pinctrl_desc.name)
941
- return -ENOMEM;
942
- } else {
943
- mcp->pinctrl_desc.name = "mcp23xxx-pinctrl";
944
- }
617
+ ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
618
+ if (ret < 0)
619
+ return dev_err_probe(dev, ret, "can't add GPIO chip\n");
620
+
945621 mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops;
946622 mcp->pinctrl_desc.confops = &mcp_pinconf_ops;
947623 mcp->pinctrl_desc.npins = mcp->chip.ngpio;
....@@ -952,291 +628,17 @@
952628 mcp->pinctrl_desc.owner = THIS_MODULE;
953629
954630 mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp);
955
- if (IS_ERR(mcp->pctldev)) {
956
- ret = PTR_ERR(mcp->pctldev);
957
- goto fail;
958
- }
631
+ if (IS_ERR(mcp->pctldev))
632
+ return dev_err_probe(dev, PTR_ERR(mcp->pctldev), "can't register controller\n");
959633
960
- if (mcp->irq)
634
+ if (mcp->irq) {
961635 ret = mcp23s08_irq_setup(mcp);
962
-
963
-fail:
964
- if (ret < 0)
965
- dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
966
- return ret;
967
-}
968
-
969
-/*----------------------------------------------------------------------*/
970
-
971
-#ifdef CONFIG_OF
972
-#ifdef CONFIG_SPI_MASTER
973
-static const struct of_device_id mcp23s08_spi_of_match[] = {
974
- {
975
- .compatible = "microchip,mcp23s08",
976
- .data = (void *) MCP_TYPE_S08,
977
- },
978
- {
979
- .compatible = "microchip,mcp23s17",
980
- .data = (void *) MCP_TYPE_S17,
981
- },
982
- {
983
- .compatible = "microchip,mcp23s18",
984
- .data = (void *) MCP_TYPE_S18,
985
- },
986
-/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
987
- {
988
- .compatible = "mcp,mcp23s08",
989
- .data = (void *) MCP_TYPE_S08,
990
- },
991
- {
992
- .compatible = "mcp,mcp23s17",
993
- .data = (void *) MCP_TYPE_S17,
994
- },
995
- { },
996
-};
997
-MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
998
-#endif
999
-
1000
-#if IS_ENABLED(CONFIG_I2C)
1001
-static const struct of_device_id mcp23s08_i2c_of_match[] = {
1002
- {
1003
- .compatible = "microchip,mcp23008",
1004
- .data = (void *) MCP_TYPE_008,
1005
- },
1006
- {
1007
- .compatible = "microchip,mcp23017",
1008
- .data = (void *) MCP_TYPE_017,
1009
- },
1010
- {
1011
- .compatible = "microchip,mcp23018",
1012
- .data = (void *) MCP_TYPE_018,
1013
- },
1014
-/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
1015
- {
1016
- .compatible = "mcp,mcp23008",
1017
- .data = (void *) MCP_TYPE_008,
1018
- },
1019
- {
1020
- .compatible = "mcp,mcp23017",
1021
- .data = (void *) MCP_TYPE_017,
1022
- },
1023
- { },
1024
-};
1025
-MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
1026
-#endif
1027
-#endif /* CONFIG_OF */
1028
-
1029
-
1030
-#if IS_ENABLED(CONFIG_I2C)
1031
-
1032
-static int mcp230xx_probe(struct i2c_client *client,
1033
- const struct i2c_device_id *id)
1034
-{
1035
- struct mcp23s08_platform_data *pdata, local_pdata;
1036
- struct mcp23s08 *mcp;
1037
- int status;
1038
-
1039
- pdata = dev_get_platdata(&client->dev);
1040
- if (!pdata) {
1041
- pdata = &local_pdata;
1042
- pdata->base = -1;
636
+ if (ret)
637
+ return dev_err_probe(dev, ret, "can't setup IRQ\n");
1043638 }
1044
-
1045
- mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL);
1046
- if (!mcp)
1047
- return -ENOMEM;
1048
-
1049
- mcp->irq = client->irq;
1050
- status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
1051
- id->driver_data, pdata->base, 0);
1052
- if (status)
1053
- return status;
1054
-
1055
- i2c_set_clientdata(client, mcp);
1056639
1057640 return 0;
1058641 }
1059
-
1060
-static const struct i2c_device_id mcp230xx_id[] = {
1061
- { "mcp23008", MCP_TYPE_008 },
1062
- { "mcp23017", MCP_TYPE_017 },
1063
- { "mcp23018", MCP_TYPE_018 },
1064
- { },
1065
-};
1066
-MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
1067
-
1068
-static struct i2c_driver mcp230xx_driver = {
1069
- .driver = {
1070
- .name = "mcp230xx",
1071
- .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
1072
- },
1073
- .probe = mcp230xx_probe,
1074
- .id_table = mcp230xx_id,
1075
-};
1076
-
1077
-static int __init mcp23s08_i2c_init(void)
1078
-{
1079
- return i2c_add_driver(&mcp230xx_driver);
1080
-}
1081
-
1082
-static void mcp23s08_i2c_exit(void)
1083
-{
1084
- i2c_del_driver(&mcp230xx_driver);
1085
-}
1086
-
1087
-#else
1088
-
1089
-static int __init mcp23s08_i2c_init(void) { return 0; }
1090
-static void mcp23s08_i2c_exit(void) { }
1091
-
1092
-#endif /* CONFIG_I2C */
1093
-
1094
-/*----------------------------------------------------------------------*/
1095
-
1096
-#ifdef CONFIG_SPI_MASTER
1097
-
1098
-static int mcp23s08_probe(struct spi_device *spi)
1099
-{
1100
- struct mcp23s08_platform_data *pdata, local_pdata;
1101
- unsigned addr;
1102
- int chips = 0;
1103
- struct mcp23s08_driver_data *data;
1104
- int status, type;
1105
- unsigned ngpio = 0;
1106
- const struct of_device_id *match;
1107
-
1108
- match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
1109
- if (match)
1110
- type = (int)(uintptr_t)match->data;
1111
- else
1112
- type = spi_get_device_id(spi)->driver_data;
1113
-
1114
- pdata = dev_get_platdata(&spi->dev);
1115
- if (!pdata) {
1116
- pdata = &local_pdata;
1117
- pdata->base = -1;
1118
-
1119
- status = device_property_read_u32(&spi->dev,
1120
- "microchip,spi-present-mask", &pdata->spi_present_mask);
1121
- if (status) {
1122
- status = device_property_read_u32(&spi->dev,
1123
- "mcp,spi-present-mask",
1124
- &pdata->spi_present_mask);
1125
-
1126
- if (status) {
1127
- dev_err(&spi->dev, "missing spi-present-mask");
1128
- return -ENODEV;
1129
- }
1130
- }
1131
- }
1132
-
1133
- if (!pdata->spi_present_mask || pdata->spi_present_mask > 0xff) {
1134
- dev_err(&spi->dev, "invalid spi-present-mask");
1135
- return -ENODEV;
1136
- }
1137
-
1138
- for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
1139
- if (pdata->spi_present_mask & BIT(addr))
1140
- chips++;
1141
- }
1142
-
1143
- if (!chips)
1144
- return -ENODEV;
1145
-
1146
- data = devm_kzalloc(&spi->dev,
1147
- sizeof(*data) + chips * sizeof(struct mcp23s08),
1148
- GFP_KERNEL);
1149
- if (!data)
1150
- return -ENOMEM;
1151
-
1152
- spi_set_drvdata(spi, data);
1153
-
1154
- for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
1155
- if (!(pdata->spi_present_mask & BIT(addr)))
1156
- continue;
1157
- chips--;
1158
- data->mcp[addr] = &data->chip[chips];
1159
- data->mcp[addr]->irq = spi->irq;
1160
- status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
1161
- 0x40 | (addr << 1), type,
1162
- pdata->base, addr);
1163
- if (status < 0)
1164
- return status;
1165
-
1166
- if (pdata->base != -1)
1167
- pdata->base += data->mcp[addr]->chip.ngpio;
1168
- ngpio += data->mcp[addr]->chip.ngpio;
1169
- }
1170
- data->ngpio = ngpio;
1171
-
1172
- return 0;
1173
-}
1174
-
1175
-static const struct spi_device_id mcp23s08_ids[] = {
1176
- { "mcp23s08", MCP_TYPE_S08 },
1177
- { "mcp23s17", MCP_TYPE_S17 },
1178
- { "mcp23s18", MCP_TYPE_S18 },
1179
- { },
1180
-};
1181
-MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
1182
-
1183
-static struct spi_driver mcp23s08_driver = {
1184
- .probe = mcp23s08_probe,
1185
- .id_table = mcp23s08_ids,
1186
- .driver = {
1187
- .name = "mcp23s08",
1188
- .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
1189
- },
1190
-};
1191
-
1192
-static int __init mcp23s08_spi_init(void)
1193
-{
1194
- return spi_register_driver(&mcp23s08_driver);
1195
-}
1196
-
1197
-static void mcp23s08_spi_exit(void)
1198
-{
1199
- spi_unregister_driver(&mcp23s08_driver);
1200
-}
1201
-
1202
-#else
1203
-
1204
-static int __init mcp23s08_spi_init(void) { return 0; }
1205
-static void mcp23s08_spi_exit(void) { }
1206
-
1207
-#endif /* CONFIG_SPI_MASTER */
1208
-
1209
-/*----------------------------------------------------------------------*/
1210
-
1211
-static int __init mcp23s08_init(void)
1212
-{
1213
- int ret;
1214
-
1215
- ret = mcp23s08_spi_init();
1216
- if (ret)
1217
- goto spi_fail;
1218
-
1219
- ret = mcp23s08_i2c_init();
1220
- if (ret)
1221
- goto i2c_fail;
1222
-
1223
- return 0;
1224
-
1225
- i2c_fail:
1226
- mcp23s08_spi_exit();
1227
- spi_fail:
1228
- return ret;
1229
-}
1230
-/* register after spi/i2c postcore initcall and before
1231
- * subsys initcalls that may rely on these GPIOs
1232
- */
1233
-subsys_initcall(mcp23s08_init);
1234
-
1235
-static void __exit mcp23s08_exit(void)
1236
-{
1237
- mcp23s08_spi_exit();
1238
- mcp23s08_i2c_exit();
1239
-}
1240
-module_exit(mcp23s08_exit);
642
+EXPORT_SYMBOL_GPL(mcp23s08_probe_one);
1241643
1242644 MODULE_LICENSE("GPL");