diff options
author | Chris Metcalf <cmetcalf@tilera.com> | 2010-08-13 19:59:15 -0400 |
---|---|---|
committer | Chris Metcalf <cmetcalf@tilera.com> | 2010-08-13 19:59:15 -0400 |
commit | 7d72e6fa56c4100b9669efe0044f77ed9eb785a1 (patch) | |
tree | 5e90bf4969809a1ab20b97432b85be20ccfaa1f4 /drivers/i2c | |
parent | ba00376b0b13f234d839541a7b36a5bf5c2a4036 (diff) | |
parent | 2be1f3a73dd02e38e181cf5abacb3d45a6a2d6b8 (diff) |
Merge branch 'master' into for-linus
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/Kconfig | 13 | ||||
-rw-r--r-- | drivers/i2c/Makefile | 3 | ||||
-rw-r--r-- | drivers/i2c/busses/Kconfig | 13 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-cpm.c | 8 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-ibm_iic.c | 6 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-mpc.c | 4 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-sh_mobile.c | 121 | ||||
-rw-r--r-- | drivers/i2c/i2c-core.c | 158 | ||||
-rw-r--r-- | drivers/i2c/i2c-dev.c | 66 | ||||
-rw-r--r-- | drivers/i2c/i2c-mux.c | 165 | ||||
-rw-r--r-- | drivers/i2c/muxes/Kconfig | 18 | ||||
-rw-r--r-- | drivers/i2c/muxes/Makefile | 8 | ||||
-rw-r--r-- | drivers/i2c/muxes/pca954x.c | 301 |
13 files changed, 774 insertions, 110 deletions
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index d06083fdffbb..30f06e956bfb 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig | |||
@@ -47,6 +47,19 @@ config I2C_CHARDEV | |||
47 | This support is also available as a module. If so, the module | 47 | This support is also available as a module. If so, the module |
48 | will be called i2c-dev. | 48 | will be called i2c-dev. |
49 | 49 | ||
50 | config I2C_MUX | ||
51 | tristate "I2C bus multiplexing support" | ||
52 | depends on EXPERIMENTAL | ||
53 | help | ||
54 | Say Y here if you want the I2C core to support the ability to | ||
55 | handle multiplexed I2C bus topologies, by presenting each | ||
56 | multiplexed segment as a I2C adapter. | ||
57 | |||
58 | This support is also available as a module. If so, the module | ||
59 | will be called i2c-mux. | ||
60 | |||
61 | source drivers/i2c/muxes/Kconfig | ||
62 | |||
50 | config I2C_HELPER_AUTO | 63 | config I2C_HELPER_AUTO |
51 | bool "Autoselect pertinent helper modules" | 64 | bool "Autoselect pertinent helper modules" |
52 | default y | 65 | default y |
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index a7d9b4be9bb3..c00fd66388f5 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile | |||
@@ -6,7 +6,8 @@ obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o | |||
6 | obj-$(CONFIG_I2C) += i2c-core.o | 6 | obj-$(CONFIG_I2C) += i2c-core.o |
7 | obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o | 7 | obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o |
8 | obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o | 8 | obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o |
9 | obj-y += algos/ busses/ | 9 | obj-$(CONFIG_I2C_MUX) += i2c-mux.o |
10 | obj-y += algos/ busses/ muxes/ | ||
10 | 11 | ||
11 | ifeq ($(CONFIG_I2C_DEBUG_CORE),y) | 12 | ifeq ($(CONFIG_I2C_DEBUG_CORE),y) |
12 | EXTRA_CFLAGS += -DDEBUG | 13 | EXTRA_CFLAGS += -DDEBUG |
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index bceafbfa7268..15a9702e2941 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig | |||
@@ -521,12 +521,19 @@ config I2C_PXA_SLAVE | |||
521 | is necessary for systems where the PXA may be a target on the | 521 | is necessary for systems where the PXA may be a target on the |
522 | I2C bus. | 522 | I2C bus. |
523 | 523 | ||
524 | config HAVE_S3C2410_I2C | ||
525 | bool | ||
526 | help | ||
527 | This will include I2C support for Samsung SoCs. If you want to | ||
528 | include I2C support for any machine, kindly select this in the | ||
529 | respective Kconfig file. | ||
530 | |||
524 | config I2C_S3C2410 | 531 | config I2C_S3C2410 |
525 | tristate "S3C2410 I2C Driver" | 532 | tristate "S3C2410 I2C Driver" |
526 | depends on ARCH_S3C2410 || ARCH_S3C64XX | 533 | depends on HAVE_S3C2410_I2C |
527 | help | 534 | help |
528 | Say Y here to include support for I2C controller in the | 535 | Say Y here to include support for I2C controller in the |
529 | Samsung S3C2410 based System-on-Chip devices. | 536 | Samsung SoCs. |
530 | 537 | ||
531 | config I2C_S6000 | 538 | config I2C_S6000 |
532 | tristate "S6000 I2C support" | 539 | tristate "S6000 I2C support" |
@@ -549,7 +556,7 @@ config I2C_SH7760 | |||
549 | 556 | ||
550 | config I2C_SH_MOBILE | 557 | config I2C_SH_MOBILE |
551 | tristate "SuperH Mobile I2C Controller" | 558 | tristate "SuperH Mobile I2C Controller" |
552 | depends on SUPERH | 559 | depends on SUPERH || ARCH_SHMOBILE |
553 | help | 560 | help |
554 | If you say yes to this option, support will be included for the | 561 | If you say yes to this option, support will be included for the |
555 | built-in I2C interface on the Renesas SH-Mobile processor. | 562 | built-in I2C interface on the Renesas SH-Mobile processor. |
diff --git a/drivers/i2c/busses/i2c-cpm.c b/drivers/i2c/busses/i2c-cpm.c index e591de1bc704..f7bd2613cecc 100644 --- a/drivers/i2c/busses/i2c-cpm.c +++ b/drivers/i2c/busses/i2c-cpm.c | |||
@@ -105,7 +105,7 @@ struct i2c_reg { | |||
105 | 105 | ||
106 | struct cpm_i2c { | 106 | struct cpm_i2c { |
107 | char *base; | 107 | char *base; |
108 | struct of_device *ofdev; | 108 | struct platform_device *ofdev; |
109 | struct i2c_adapter adap; | 109 | struct i2c_adapter adap; |
110 | uint dp_addr; | 110 | uint dp_addr; |
111 | int version; /* CPM1=1, CPM2=2 */ | 111 | int version; /* CPM1=1, CPM2=2 */ |
@@ -428,7 +428,7 @@ static const struct i2c_adapter cpm_ops = { | |||
428 | 428 | ||
429 | static int __devinit cpm_i2c_setup(struct cpm_i2c *cpm) | 429 | static int __devinit cpm_i2c_setup(struct cpm_i2c *cpm) |
430 | { | 430 | { |
431 | struct of_device *ofdev = cpm->ofdev; | 431 | struct platform_device *ofdev = cpm->ofdev; |
432 | const u32 *data; | 432 | const u32 *data; |
433 | int len, ret, i; | 433 | int len, ret, i; |
434 | void __iomem *i2c_base; | 434 | void __iomem *i2c_base; |
@@ -634,7 +634,7 @@ static void cpm_i2c_shutdown(struct cpm_i2c *cpm) | |||
634 | cpm_muram_free(cpm->i2c_addr); | 634 | cpm_muram_free(cpm->i2c_addr); |
635 | } | 635 | } |
636 | 636 | ||
637 | static int __devinit cpm_i2c_probe(struct of_device *ofdev, | 637 | static int __devinit cpm_i2c_probe(struct platform_device *ofdev, |
638 | const struct of_device_id *match) | 638 | const struct of_device_id *match) |
639 | { | 639 | { |
640 | int result, len; | 640 | int result, len; |
@@ -687,7 +687,7 @@ out_free: | |||
687 | return result; | 687 | return result; |
688 | } | 688 | } |
689 | 689 | ||
690 | static int __devexit cpm_i2c_remove(struct of_device *ofdev) | 690 | static int __devexit cpm_i2c_remove(struct platform_device *ofdev) |
691 | { | 691 | { |
692 | struct cpm_i2c *cpm = dev_get_drvdata(&ofdev->dev); | 692 | struct cpm_i2c *cpm = dev_get_drvdata(&ofdev->dev); |
693 | 693 | ||
diff --git a/drivers/i2c/busses/i2c-ibm_iic.c b/drivers/i2c/busses/i2c-ibm_iic.c index 1168d61418c9..43ca32fddde2 100644 --- a/drivers/i2c/busses/i2c-ibm_iic.c +++ b/drivers/i2c/busses/i2c-ibm_iic.c | |||
@@ -661,7 +661,7 @@ static inline u8 iic_clckdiv(unsigned int opb) | |||
661 | return (u8)((opb + 9) / 10 - 1); | 661 | return (u8)((opb + 9) / 10 - 1); |
662 | } | 662 | } |
663 | 663 | ||
664 | static int __devinit iic_request_irq(struct of_device *ofdev, | 664 | static int __devinit iic_request_irq(struct platform_device *ofdev, |
665 | struct ibm_iic_private *dev) | 665 | struct ibm_iic_private *dev) |
666 | { | 666 | { |
667 | struct device_node *np = ofdev->dev.of_node; | 667 | struct device_node *np = ofdev->dev.of_node; |
@@ -692,7 +692,7 @@ static int __devinit iic_request_irq(struct of_device *ofdev, | |||
692 | /* | 692 | /* |
693 | * Register single IIC interface | 693 | * Register single IIC interface |
694 | */ | 694 | */ |
695 | static int __devinit iic_probe(struct of_device *ofdev, | 695 | static int __devinit iic_probe(struct platform_device *ofdev, |
696 | const struct of_device_id *match) | 696 | const struct of_device_id *match) |
697 | { | 697 | { |
698 | struct device_node *np = ofdev->dev.of_node; | 698 | struct device_node *np = ofdev->dev.of_node; |
@@ -780,7 +780,7 @@ error_cleanup: | |||
780 | /* | 780 | /* |
781 | * Cleanup initialized IIC interface | 781 | * Cleanup initialized IIC interface |
782 | */ | 782 | */ |
783 | static int __devexit iic_remove(struct of_device *ofdev) | 783 | static int __devexit iic_remove(struct platform_device *ofdev) |
784 | { | 784 | { |
785 | struct ibm_iic_private *dev = dev_get_drvdata(&ofdev->dev); | 785 | struct ibm_iic_private *dev = dev_get_drvdata(&ofdev->dev); |
786 | 786 | ||
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c index 6545d1c99b61..a1c419a716af 100644 --- a/drivers/i2c/busses/i2c-mpc.c +++ b/drivers/i2c/busses/i2c-mpc.c | |||
@@ -560,7 +560,7 @@ static struct i2c_adapter mpc_ops = { | |||
560 | .timeout = HZ, | 560 | .timeout = HZ, |
561 | }; | 561 | }; |
562 | 562 | ||
563 | static int __devinit fsl_i2c_probe(struct of_device *op, | 563 | static int __devinit fsl_i2c_probe(struct platform_device *op, |
564 | const struct of_device_id *match) | 564 | const struct of_device_id *match) |
565 | { | 565 | { |
566 | struct mpc_i2c *i2c; | 566 | struct mpc_i2c *i2c; |
@@ -646,7 +646,7 @@ static int __devinit fsl_i2c_probe(struct of_device *op, | |||
646 | return result; | 646 | return result; |
647 | }; | 647 | }; |
648 | 648 | ||
649 | static int __devexit fsl_i2c_remove(struct of_device *op) | 649 | static int __devexit fsl_i2c_remove(struct platform_device *op) |
650 | { | 650 | { |
651 | struct mpc_i2c *i2c = dev_get_drvdata(&op->dev); | 651 | struct mpc_i2c *i2c = dev_get_drvdata(&op->dev); |
652 | 652 | ||
diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c index ffb405d7c6f2..598c49acaeb5 100644 --- a/drivers/i2c/busses/i2c-sh_mobile.c +++ b/drivers/i2c/busses/i2c-sh_mobile.c | |||
@@ -119,8 +119,10 @@ struct sh_mobile_i2c_data { | |||
119 | struct i2c_adapter adap; | 119 | struct i2c_adapter adap; |
120 | 120 | ||
121 | struct clk *clk; | 121 | struct clk *clk; |
122 | u_int8_t icic; | ||
122 | u_int8_t iccl; | 123 | u_int8_t iccl; |
123 | u_int8_t icch; | 124 | u_int8_t icch; |
125 | u_int8_t flags; | ||
124 | 126 | ||
125 | spinlock_t lock; | 127 | spinlock_t lock; |
126 | wait_queue_head_t wait; | 128 | wait_queue_head_t wait; |
@@ -129,15 +131,17 @@ struct sh_mobile_i2c_data { | |||
129 | int sr; | 131 | int sr; |
130 | }; | 132 | }; |
131 | 133 | ||
134 | #define IIC_FLAG_HAS_ICIC67 (1 << 0) | ||
135 | |||
132 | #define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */ | 136 | #define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */ |
133 | 137 | ||
134 | /* Register offsets */ | 138 | /* Register offsets */ |
135 | #define ICDR(pd) (pd->reg + 0x00) | 139 | #define ICDR 0x00 |
136 | #define ICCR(pd) (pd->reg + 0x04) | 140 | #define ICCR 0x04 |
137 | #define ICSR(pd) (pd->reg + 0x08) | 141 | #define ICSR 0x08 |
138 | #define ICIC(pd) (pd->reg + 0x0c) | 142 | #define ICIC 0x0c |
139 | #define ICCL(pd) (pd->reg + 0x10) | 143 | #define ICCL 0x10 |
140 | #define ICCH(pd) (pd->reg + 0x14) | 144 | #define ICCH 0x14 |
141 | 145 | ||
142 | /* Register bits */ | 146 | /* Register bits */ |
143 | #define ICCR_ICE 0x80 | 147 | #define ICCR_ICE 0x80 |
@@ -155,11 +159,32 @@ struct sh_mobile_i2c_data { | |||
155 | #define ICSR_WAIT 0x02 | 159 | #define ICSR_WAIT 0x02 |
156 | #define ICSR_DTE 0x01 | 160 | #define ICSR_DTE 0x01 |
157 | 161 | ||
162 | #define ICIC_ICCLB8 0x80 | ||
163 | #define ICIC_ICCHB8 0x40 | ||
158 | #define ICIC_ALE 0x08 | 164 | #define ICIC_ALE 0x08 |
159 | #define ICIC_TACKE 0x04 | 165 | #define ICIC_TACKE 0x04 |
160 | #define ICIC_WAITE 0x02 | 166 | #define ICIC_WAITE 0x02 |
161 | #define ICIC_DTEE 0x01 | 167 | #define ICIC_DTEE 0x01 |
162 | 168 | ||
169 | static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data) | ||
170 | { | ||
171 | if (offs == ICIC) | ||
172 | data |= pd->icic; | ||
173 | |||
174 | iowrite8(data, pd->reg + offs); | ||
175 | } | ||
176 | |||
177 | static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs) | ||
178 | { | ||
179 | return ioread8(pd->reg + offs); | ||
180 | } | ||
181 | |||
182 | static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs, | ||
183 | unsigned char set, unsigned char clr) | ||
184 | { | ||
185 | iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr); | ||
186 | } | ||
187 | |||
163 | static void activate_ch(struct sh_mobile_i2c_data *pd) | 188 | static void activate_ch(struct sh_mobile_i2c_data *pd) |
164 | { | 189 | { |
165 | unsigned long i2c_clk; | 190 | unsigned long i2c_clk; |
@@ -187,6 +212,14 @@ static void activate_ch(struct sh_mobile_i2c_data *pd) | |||
187 | else | 212 | else |
188 | pd->iccl = (u_int8_t)(num/denom); | 213 | pd->iccl = (u_int8_t)(num/denom); |
189 | 214 | ||
215 | /* one more bit of ICCL in ICIC */ | ||
216 | if (pd->flags & IIC_FLAG_HAS_ICIC67) { | ||
217 | if ((num/denom) > 0xff) | ||
218 | pd->icic |= ICIC_ICCLB8; | ||
219 | else | ||
220 | pd->icic &= ~ICIC_ICCLB8; | ||
221 | } | ||
222 | |||
190 | /* Calculate the value for icch. From the data sheet: | 223 | /* Calculate the value for icch. From the data sheet: |
191 | icch = (p clock / transfer rate) * (H / (L + H)) */ | 224 | icch = (p clock / transfer rate) * (H / (L + H)) */ |
192 | num = i2c_clk * 4; | 225 | num = i2c_clk * 4; |
@@ -196,25 +229,33 @@ static void activate_ch(struct sh_mobile_i2c_data *pd) | |||
196 | else | 229 | else |
197 | pd->icch = (u_int8_t)(num/denom); | 230 | pd->icch = (u_int8_t)(num/denom); |
198 | 231 | ||
232 | /* one more bit of ICCH in ICIC */ | ||
233 | if (pd->flags & IIC_FLAG_HAS_ICIC67) { | ||
234 | if ((num/denom) > 0xff) | ||
235 | pd->icic |= ICIC_ICCHB8; | ||
236 | else | ||
237 | pd->icic &= ~ICIC_ICCHB8; | ||
238 | } | ||
239 | |||
199 | /* Enable channel and configure rx ack */ | 240 | /* Enable channel and configure rx ack */ |
200 | iowrite8(ioread8(ICCR(pd)) | ICCR_ICE, ICCR(pd)); | 241 | iic_set_clr(pd, ICCR, ICCR_ICE, 0); |
201 | 242 | ||
202 | /* Mask all interrupts */ | 243 | /* Mask all interrupts */ |
203 | iowrite8(0, ICIC(pd)); | 244 | iic_wr(pd, ICIC, 0); |
204 | 245 | ||
205 | /* Set the clock */ | 246 | /* Set the clock */ |
206 | iowrite8(pd->iccl, ICCL(pd)); | 247 | iic_wr(pd, ICCL, pd->iccl); |
207 | iowrite8(pd->icch, ICCH(pd)); | 248 | iic_wr(pd, ICCH, pd->icch); |
208 | } | 249 | } |
209 | 250 | ||
210 | static void deactivate_ch(struct sh_mobile_i2c_data *pd) | 251 | static void deactivate_ch(struct sh_mobile_i2c_data *pd) |
211 | { | 252 | { |
212 | /* Clear/disable interrupts */ | 253 | /* Clear/disable interrupts */ |
213 | iowrite8(0, ICSR(pd)); | 254 | iic_wr(pd, ICSR, 0); |
214 | iowrite8(0, ICIC(pd)); | 255 | iic_wr(pd, ICIC, 0); |
215 | 256 | ||
216 | /* Disable channel */ | 257 | /* Disable channel */ |
217 | iowrite8(ioread8(ICCR(pd)) & ~ICCR_ICE, ICCR(pd)); | 258 | iic_set_clr(pd, ICCR, 0, ICCR_ICE); |
218 | 259 | ||
219 | /* Disable clock and mark device as idle */ | 260 | /* Disable clock and mark device as idle */ |
220 | clk_disable(pd->clk); | 261 | clk_disable(pd->clk); |
@@ -233,35 +274,35 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, | |||
233 | 274 | ||
234 | switch (op) { | 275 | switch (op) { |
235 | case OP_START: /* issue start and trigger DTE interrupt */ | 276 | case OP_START: /* issue start and trigger DTE interrupt */ |
236 | iowrite8(0x94, ICCR(pd)); | 277 | iic_wr(pd, ICCR, 0x94); |
237 | break; | 278 | break; |
238 | case OP_TX_FIRST: /* disable DTE interrupt and write data */ | 279 | case OP_TX_FIRST: /* disable DTE interrupt and write data */ |
239 | iowrite8(ICIC_WAITE | ICIC_ALE | ICIC_TACKE, ICIC(pd)); | 280 | iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
240 | iowrite8(data, ICDR(pd)); | 281 | iic_wr(pd, ICDR, data); |
241 | break; | 282 | break; |
242 | case OP_TX: /* write data */ | 283 | case OP_TX: /* write data */ |
243 | iowrite8(data, ICDR(pd)); | 284 | iic_wr(pd, ICDR, data); |
244 | break; | 285 | break; |
245 | case OP_TX_STOP: /* write data and issue a stop afterwards */ | 286 | case OP_TX_STOP: /* write data and issue a stop afterwards */ |
246 | iowrite8(data, ICDR(pd)); | 287 | iic_wr(pd, ICDR, data); |
247 | iowrite8(0x90, ICCR(pd)); | 288 | iic_wr(pd, ICCR, 0x90); |
248 | break; | 289 | break; |
249 | case OP_TX_TO_RX: /* select read mode */ | 290 | case OP_TX_TO_RX: /* select read mode */ |
250 | iowrite8(0x81, ICCR(pd)); | 291 | iic_wr(pd, ICCR, 0x81); |
251 | break; | 292 | break; |
252 | case OP_RX: /* just read data */ | 293 | case OP_RX: /* just read data */ |
253 | ret = ioread8(ICDR(pd)); | 294 | ret = iic_rd(pd, ICDR); |
254 | break; | 295 | break; |
255 | case OP_RX_STOP: /* enable DTE interrupt, issue stop */ | 296 | case OP_RX_STOP: /* enable DTE interrupt, issue stop */ |
256 | iowrite8(ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE, | 297 | iic_wr(pd, ICIC, |
257 | ICIC(pd)); | 298 | ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
258 | iowrite8(0xc0, ICCR(pd)); | 299 | iic_wr(pd, ICCR, 0xc0); |
259 | break; | 300 | break; |
260 | case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ | 301 | case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ |
261 | iowrite8(ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE, | 302 | iic_wr(pd, ICIC, |
262 | ICIC(pd)); | 303 | ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
263 | ret = ioread8(ICDR(pd)); | 304 | ret = iic_rd(pd, ICDR); |
264 | iowrite8(0xc0, ICCR(pd)); | 305 | iic_wr(pd, ICCR, 0xc0); |
265 | break; | 306 | break; |
266 | } | 307 | } |
267 | 308 | ||
@@ -367,7 +408,7 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) | |||
367 | unsigned char sr; | 408 | unsigned char sr; |
368 | int wakeup; | 409 | int wakeup; |
369 | 410 | ||
370 | sr = ioread8(ICSR(pd)); | 411 | sr = iic_rd(pd, ICSR); |
371 | pd->sr |= sr; /* remember state */ | 412 | pd->sr |= sr; /* remember state */ |
372 | 413 | ||
373 | dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, | 414 | dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, |
@@ -376,7 +417,7 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) | |||
376 | 417 | ||
377 | if (sr & (ICSR_AL | ICSR_TACK)) { | 418 | if (sr & (ICSR_AL | ICSR_TACK)) { |
378 | /* don't interrupt transaction - continue to issue stop */ | 419 | /* don't interrupt transaction - continue to issue stop */ |
379 | iowrite8(sr & ~(ICSR_AL | ICSR_TACK), ICSR(pd)); | 420 | iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK)); |
380 | wakeup = 0; | 421 | wakeup = 0; |
381 | } else if (pd->msg->flags & I2C_M_RD) | 422 | } else if (pd->msg->flags & I2C_M_RD) |
382 | wakeup = sh_mobile_i2c_isr_rx(pd); | 423 | wakeup = sh_mobile_i2c_isr_rx(pd); |
@@ -384,7 +425,7 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) | |||
384 | wakeup = sh_mobile_i2c_isr_tx(pd); | 425 | wakeup = sh_mobile_i2c_isr_tx(pd); |
385 | 426 | ||
386 | if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */ | 427 | if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */ |
387 | iowrite8(sr & ~ICSR_WAIT, ICSR(pd)); | 428 | iic_wr(pd, ICSR, sr & ~ICSR_WAIT); |
388 | 429 | ||
389 | if (wakeup) { | 430 | if (wakeup) { |
390 | pd->sr |= SW_DONE; | 431 | pd->sr |= SW_DONE; |
@@ -402,21 +443,21 @@ static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg) | |||
402 | } | 443 | } |
403 | 444 | ||
404 | /* Initialize channel registers */ | 445 | /* Initialize channel registers */ |
405 | iowrite8(ioread8(ICCR(pd)) & ~ICCR_ICE, ICCR(pd)); | 446 | iic_set_clr(pd, ICCR, 0, ICCR_ICE); |
406 | 447 | ||
407 | /* Enable channel and configure rx ack */ | 448 | /* Enable channel and configure rx ack */ |
408 | iowrite8(ioread8(ICCR(pd)) | ICCR_ICE, ICCR(pd)); | 449 | iic_set_clr(pd, ICCR, ICCR_ICE, 0); |
409 | 450 | ||
410 | /* Set the clock */ | 451 | /* Set the clock */ |
411 | iowrite8(pd->iccl, ICCL(pd)); | 452 | iic_wr(pd, ICCL, pd->iccl); |
412 | iowrite8(pd->icch, ICCH(pd)); | 453 | iic_wr(pd, ICCH, pd->icch); |
413 | 454 | ||
414 | pd->msg = usr_msg; | 455 | pd->msg = usr_msg; |
415 | pd->pos = -1; | 456 | pd->pos = -1; |
416 | pd->sr = 0; | 457 | pd->sr = 0; |
417 | 458 | ||
418 | /* Enable all interrupts to begin with */ | 459 | /* Enable all interrupts to begin with */ |
419 | iowrite8(ICIC_WAITE | ICIC_ALE | ICIC_TACKE | ICIC_DTEE, ICIC(pd)); | 460 | iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
420 | return 0; | 461 | return 0; |
421 | } | 462 | } |
422 | 463 | ||
@@ -451,7 +492,7 @@ static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, | |||
451 | 492 | ||
452 | retry_count = 1000; | 493 | retry_count = 1000; |
453 | again: | 494 | again: |
454 | val = ioread8(ICSR(pd)); | 495 | val = iic_rd(pd, ICSR); |
455 | 496 | ||
456 | dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); | 497 | dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); |
457 | 498 | ||
@@ -576,6 +617,12 @@ static int sh_mobile_i2c_probe(struct platform_device *dev) | |||
576 | goto err_irq; | 617 | goto err_irq; |
577 | } | 618 | } |
578 | 619 | ||
620 | /* The IIC blocks on SH-Mobile ARM processors | ||
621 | * come with two new bits in ICIC. | ||
622 | */ | ||
623 | if (size > 0x17) | ||
624 | pd->flags |= IIC_FLAG_HAS_ICIC67; | ||
625 | |||
579 | /* Enable Runtime PM for this device. | 626 | /* Enable Runtime PM for this device. |
580 | * | 627 | * |
581 | * Also tell the Runtime PM core to ignore children | 628 | * Also tell the Runtime PM core to ignore children |
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index df937df845eb..6649176de940 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -20,7 +20,9 @@ | |||
20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. | 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> | 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and | 22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
23 | Jean Delvare <khali@linux-fr.org> */ | 23 | Jean Delvare <khali@linux-fr.org> |
24 | Mux support by Rodolfo Giometti <giometti@enneenne.com> and | ||
25 | Michael Lawnick <michael.lawnick.ext@nsn.com> */ | ||
24 | 26 | ||
25 | #include <linux/module.h> | 27 | #include <linux/module.h> |
26 | #include <linux/kernel.h> | 28 | #include <linux/kernel.h> |
@@ -423,11 +425,87 @@ static int __i2c_check_addr_busy(struct device *dev, void *addrp) | |||
423 | return 0; | 425 | return 0; |
424 | } | 426 | } |
425 | 427 | ||
428 | /* walk up mux tree */ | ||
429 | static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) | ||
430 | { | ||
431 | int result; | ||
432 | |||
433 | result = device_for_each_child(&adapter->dev, &addr, | ||
434 | __i2c_check_addr_busy); | ||
435 | |||
436 | if (!result && i2c_parent_is_i2c_adapter(adapter)) | ||
437 | result = i2c_check_mux_parents( | ||
438 | to_i2c_adapter(adapter->dev.parent), addr); | ||
439 | |||
440 | return result; | ||
441 | } | ||
442 | |||
443 | /* recurse down mux tree */ | ||
444 | static int i2c_check_mux_children(struct device *dev, void *addrp) | ||
445 | { | ||
446 | int result; | ||
447 | |||
448 | if (dev->type == &i2c_adapter_type) | ||
449 | result = device_for_each_child(dev, addrp, | ||
450 | i2c_check_mux_children); | ||
451 | else | ||
452 | result = __i2c_check_addr_busy(dev, addrp); | ||
453 | |||
454 | return result; | ||
455 | } | ||
456 | |||
426 | static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) | 457 | static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) |
427 | { | 458 | { |
428 | return device_for_each_child(&adapter->dev, &addr, | 459 | int result = 0; |
429 | __i2c_check_addr_busy); | 460 | |
461 | if (i2c_parent_is_i2c_adapter(adapter)) | ||
462 | result = i2c_check_mux_parents( | ||
463 | to_i2c_adapter(adapter->dev.parent), addr); | ||
464 | |||
465 | if (!result) | ||
466 | result = device_for_each_child(&adapter->dev, &addr, | ||
467 | i2c_check_mux_children); | ||
468 | |||
469 | return result; | ||
470 | } | ||
471 | |||
472 | /** | ||
473 | * i2c_lock_adapter - Get exclusive access to an I2C bus segment | ||
474 | * @adapter: Target I2C bus segment | ||
475 | */ | ||
476 | void i2c_lock_adapter(struct i2c_adapter *adapter) | ||
477 | { | ||
478 | if (i2c_parent_is_i2c_adapter(adapter)) | ||
479 | i2c_lock_adapter(to_i2c_adapter(adapter->dev.parent)); | ||
480 | else | ||
481 | rt_mutex_lock(&adapter->bus_lock); | ||
482 | } | ||
483 | EXPORT_SYMBOL_GPL(i2c_lock_adapter); | ||
484 | |||
485 | /** | ||
486 | * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment | ||
487 | * @adapter: Target I2C bus segment | ||
488 | */ | ||
489 | static int i2c_trylock_adapter(struct i2c_adapter *adapter) | ||
490 | { | ||
491 | if (i2c_parent_is_i2c_adapter(adapter)) | ||
492 | return i2c_trylock_adapter(to_i2c_adapter(adapter->dev.parent)); | ||
493 | else | ||
494 | return rt_mutex_trylock(&adapter->bus_lock); | ||
495 | } | ||
496 | |||
497 | /** | ||
498 | * i2c_unlock_adapter - Release exclusive access to an I2C bus segment | ||
499 | * @adapter: Target I2C bus segment | ||
500 | */ | ||
501 | void i2c_unlock_adapter(struct i2c_adapter *adapter) | ||
502 | { | ||
503 | if (i2c_parent_is_i2c_adapter(adapter)) | ||
504 | i2c_unlock_adapter(to_i2c_adapter(adapter->dev.parent)); | ||
505 | else | ||
506 | rt_mutex_unlock(&adapter->bus_lock); | ||
430 | } | 507 | } |
508 | EXPORT_SYMBOL_GPL(i2c_unlock_adapter); | ||
431 | 509 | ||
432 | /** | 510 | /** |
433 | * i2c_new_device - instantiate an i2c device | 511 | * i2c_new_device - instantiate an i2c device |
@@ -633,9 +711,9 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, | |||
633 | return -EINVAL; | 711 | return -EINVAL; |
634 | 712 | ||
635 | /* Keep track of the added device */ | 713 | /* Keep track of the added device */ |
636 | i2c_lock_adapter(adap); | 714 | mutex_lock(&adap->userspace_clients_lock); |
637 | list_add_tail(&client->detected, &adap->userspace_clients); | 715 | list_add_tail(&client->detected, &adap->userspace_clients); |
638 | i2c_unlock_adapter(adap); | 716 | mutex_unlock(&adap->userspace_clients_lock); |
639 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", | 717 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", |
640 | info.type, info.addr); | 718 | info.type, info.addr); |
641 | 719 | ||
@@ -674,7 +752,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | |||
674 | 752 | ||
675 | /* Make sure the device was added through sysfs */ | 753 | /* Make sure the device was added through sysfs */ |
676 | res = -ENOENT; | 754 | res = -ENOENT; |
677 | i2c_lock_adapter(adap); | 755 | mutex_lock(&adap->userspace_clients_lock); |
678 | list_for_each_entry_safe(client, next, &adap->userspace_clients, | 756 | list_for_each_entry_safe(client, next, &adap->userspace_clients, |
679 | detected) { | 757 | detected) { |
680 | if (client->addr == addr) { | 758 | if (client->addr == addr) { |
@@ -687,7 +765,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | |||
687 | break; | 765 | break; |
688 | } | 766 | } |
689 | } | 767 | } |
690 | i2c_unlock_adapter(adap); | 768 | mutex_unlock(&adap->userspace_clients_lock); |
691 | 769 | ||
692 | if (res < 0) | 770 | if (res < 0) |
693 | dev_err(dev, "%s: Can't find device in list\n", | 771 | dev_err(dev, "%s: Can't find device in list\n", |
@@ -714,10 +792,11 @@ static const struct attribute_group *i2c_adapter_attr_groups[] = { | |||
714 | NULL | 792 | NULL |
715 | }; | 793 | }; |
716 | 794 | ||
717 | static struct device_type i2c_adapter_type = { | 795 | struct device_type i2c_adapter_type = { |
718 | .groups = i2c_adapter_attr_groups, | 796 | .groups = i2c_adapter_attr_groups, |
719 | .release = i2c_adapter_dev_release, | 797 | .release = i2c_adapter_dev_release, |
720 | }; | 798 | }; |
799 | EXPORT_SYMBOL_GPL(i2c_adapter_type); | ||
721 | 800 | ||
722 | #ifdef CONFIG_I2C_COMPAT | 801 | #ifdef CONFIG_I2C_COMPAT |
723 | static struct class_compat *i2c_adapter_compat_class; | 802 | static struct class_compat *i2c_adapter_compat_class; |
@@ -760,7 +839,7 @@ static int __process_new_adapter(struct device_driver *d, void *data) | |||
760 | 839 | ||
761 | static int i2c_register_adapter(struct i2c_adapter *adap) | 840 | static int i2c_register_adapter(struct i2c_adapter *adap) |
762 | { | 841 | { |
763 | int res = 0, dummy; | 842 | int res = 0; |
764 | 843 | ||
765 | /* Can't register until after driver model init */ | 844 | /* Can't register until after driver model init */ |
766 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { | 845 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
@@ -769,6 +848,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap) | |||
769 | } | 848 | } |
770 | 849 | ||
771 | rt_mutex_init(&adap->bus_lock); | 850 | rt_mutex_init(&adap->bus_lock); |
851 | mutex_init(&adap->userspace_clients_lock); | ||
772 | INIT_LIST_HEAD(&adap->userspace_clients); | 852 | INIT_LIST_HEAD(&adap->userspace_clients); |
773 | 853 | ||
774 | /* Set default timeout to 1 second if not already set */ | 854 | /* Set default timeout to 1 second if not already set */ |
@@ -801,8 +881,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap) | |||
801 | 881 | ||
802 | /* Notify drivers */ | 882 | /* Notify drivers */ |
803 | mutex_lock(&core_lock); | 883 | mutex_lock(&core_lock); |
804 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, | 884 | bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); |
805 | __process_new_adapter); | ||
806 | mutex_unlock(&core_lock); | 885 | mutex_unlock(&core_lock); |
807 | 886 | ||
808 | return 0; | 887 | return 0; |
@@ -975,7 +1054,7 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
975 | return res; | 1054 | return res; |
976 | 1055 | ||
977 | /* Remove devices instantiated from sysfs */ | 1056 | /* Remove devices instantiated from sysfs */ |
978 | i2c_lock_adapter(adap); | 1057 | mutex_lock(&adap->userspace_clients_lock); |
979 | list_for_each_entry_safe(client, next, &adap->userspace_clients, | 1058 | list_for_each_entry_safe(client, next, &adap->userspace_clients, |
980 | detected) { | 1059 | detected) { |
981 | dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, | 1060 | dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, |
@@ -983,7 +1062,7 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
983 | list_del(&client->detected); | 1062 | list_del(&client->detected); |
984 | i2c_unregister_device(client); | 1063 | i2c_unregister_device(client); |
985 | } | 1064 | } |
986 | i2c_unlock_adapter(adap); | 1065 | mutex_unlock(&adap->userspace_clients_lock); |
987 | 1066 | ||
988 | /* Detach any active clients. This can't fail, thus we do not | 1067 | /* Detach any active clients. This can't fail, thus we do not |
989 | checking the returned value. */ | 1068 | checking the returned value. */ |
@@ -1238,12 +1317,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) | |||
1238 | #endif | 1317 | #endif |
1239 | 1318 | ||
1240 | if (in_atomic() || irqs_disabled()) { | 1319 | if (in_atomic() || irqs_disabled()) { |
1241 | ret = rt_mutex_trylock(&adap->bus_lock); | 1320 | ret = i2c_trylock_adapter(adap); |
1242 | if (!ret) | 1321 | if (!ret) |
1243 | /* I2C activity is ongoing. */ | 1322 | /* I2C activity is ongoing. */ |
1244 | return -EAGAIN; | 1323 | return -EAGAIN; |
1245 | } else { | 1324 | } else { |
1246 | rt_mutex_lock(&adap->bus_lock); | 1325 | i2c_lock_adapter(adap); |
1247 | } | 1326 | } |
1248 | 1327 | ||
1249 | /* Retry automatically on arbitration loss */ | 1328 | /* Retry automatically on arbitration loss */ |
@@ -1255,7 +1334,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) | |||
1255 | if (time_after(jiffies, orig_jiffies + adap->timeout)) | 1334 | if (time_after(jiffies, orig_jiffies + adap->timeout)) |
1256 | break; | 1335 | break; |
1257 | } | 1336 | } |
1258 | rt_mutex_unlock(&adap->bus_lock); | 1337 | i2c_unlock_adapter(adap); |
1259 | 1338 | ||
1260 | return ret; | 1339 | return ret; |
1261 | } else { | 1340 | } else { |
@@ -1350,13 +1429,17 @@ static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) | |||
1350 | I2C_SMBUS_BYTE_DATA, &dummy); | 1429 | I2C_SMBUS_BYTE_DATA, &dummy); |
1351 | else | 1430 | else |
1352 | #endif | 1431 | #endif |
1353 | if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50 | 1432 | if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) |
1354 | || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) | 1433 | && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) |
1355 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | ||
1356 | I2C_SMBUS_BYTE, &dummy); | ||
1357 | else | ||
1358 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, | 1434 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, |
1359 | I2C_SMBUS_QUICK, NULL); | 1435 | I2C_SMBUS_QUICK, NULL); |
1436 | else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) | ||
1437 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | ||
1438 | I2C_SMBUS_BYTE, &dummy); | ||
1439 | else { | ||
1440 | dev_warn(&adap->dev, "No suitable probing method supported\n"); | ||
1441 | err = -EOPNOTSUPP; | ||
1442 | } | ||
1360 | 1443 | ||
1361 | return err >= 0; | 1444 | return err >= 0; |
1362 | } | 1445 | } |
@@ -1437,16 +1520,6 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) | |||
1437 | if (!(adapter->class & driver->class)) | 1520 | if (!(adapter->class & driver->class)) |
1438 | goto exit_free; | 1521 | goto exit_free; |
1439 | 1522 | ||
1440 | /* Stop here if the bus doesn't support probing */ | ||
1441 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) { | ||
1442 | if (address_list[0] == I2C_CLIENT_END) | ||
1443 | goto exit_free; | ||
1444 | |||
1445 | dev_warn(&adapter->dev, "Probing not supported\n"); | ||
1446 | err = -EOPNOTSUPP; | ||
1447 | goto exit_free; | ||
1448 | } | ||
1449 | |||
1450 | for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { | 1523 | for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { |
1451 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " | 1524 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
1452 | "addr 0x%02x\n", adap_id, address_list[i]); | 1525 | "addr 0x%02x\n", adap_id, address_list[i]); |
@@ -1461,18 +1534,23 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) | |||
1461 | return err; | 1534 | return err; |
1462 | } | 1535 | } |
1463 | 1536 | ||
1537 | int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) | ||
1538 | { | ||
1539 | return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, | ||
1540 | I2C_SMBUS_QUICK, NULL) >= 0; | ||
1541 | } | ||
1542 | EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); | ||
1543 | |||
1464 | struct i2c_client * | 1544 | struct i2c_client * |
1465 | i2c_new_probed_device(struct i2c_adapter *adap, | 1545 | i2c_new_probed_device(struct i2c_adapter *adap, |
1466 | struct i2c_board_info *info, | 1546 | struct i2c_board_info *info, |
1467 | unsigned short const *addr_list) | 1547 | unsigned short const *addr_list, |
1548 | int (*probe)(struct i2c_adapter *, unsigned short addr)) | ||
1468 | { | 1549 | { |
1469 | int i; | 1550 | int i; |
1470 | 1551 | ||
1471 | /* Stop here if the bus doesn't support probing */ | 1552 | if (!probe) |
1472 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { | 1553 | probe = i2c_default_probe; |
1473 | dev_err(&adap->dev, "Probing not supported\n"); | ||
1474 | return NULL; | ||
1475 | } | ||
1476 | 1554 | ||
1477 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { | 1555 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
1478 | /* Check address validity */ | 1556 | /* Check address validity */ |
@@ -1490,7 +1568,7 @@ i2c_new_probed_device(struct i2c_adapter *adap, | |||
1490 | } | 1568 | } |
1491 | 1569 | ||
1492 | /* Test address responsiveness */ | 1570 | /* Test address responsiveness */ |
1493 | if (i2c_default_probe(adap, addr_list[i])) | 1571 | if (probe(adap, addr_list[i])) |
1494 | break; | 1572 | break; |
1495 | } | 1573 | } |
1496 | 1574 | ||
@@ -2002,7 +2080,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, | |||
2002 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; | 2080 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
2003 | 2081 | ||
2004 | if (adapter->algo->smbus_xfer) { | 2082 | if (adapter->algo->smbus_xfer) { |
2005 | rt_mutex_lock(&adapter->bus_lock); | 2083 | i2c_lock_adapter(adapter); |
2006 | 2084 | ||
2007 | /* Retry automatically on arbitration loss */ | 2085 | /* Retry automatically on arbitration loss */ |
2008 | orig_jiffies = jiffies; | 2086 | orig_jiffies = jiffies; |
@@ -2016,7 +2094,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, | |||
2016 | orig_jiffies + adapter->timeout)) | 2094 | orig_jiffies + adapter->timeout)) |
2017 | break; | 2095 | break; |
2018 | } | 2096 | } |
2019 | rt_mutex_unlock(&adapter->bus_lock); | 2097 | i2c_unlock_adapter(adapter); |
2020 | } else | 2098 | } else |
2021 | res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, | 2099 | res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, |
2022 | command, protocol, data); | 2100 | command, protocol, data); |
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index e0694e4d86c7..5f3a52d517c3 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c | |||
@@ -167,13 +167,9 @@ static ssize_t i2cdev_write(struct file *file, const char __user *buf, | |||
167 | if (count > 8192) | 167 | if (count > 8192) |
168 | count = 8192; | 168 | count = 8192; |
169 | 169 | ||
170 | tmp = kmalloc(count, GFP_KERNEL); | 170 | tmp = memdup_user(buf, count); |
171 | if (tmp == NULL) | 171 | if (IS_ERR(tmp)) |
172 | return -ENOMEM; | 172 | return PTR_ERR(tmp); |
173 | if (copy_from_user(tmp, buf, count)) { | ||
174 | kfree(tmp); | ||
175 | return -EFAULT; | ||
176 | } | ||
177 | 173 | ||
178 | pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n", | 174 | pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n", |
179 | iminor(file->f_path.dentry->d_inode), count); | 175 | iminor(file->f_path.dentry->d_inode), count); |
@@ -193,12 +189,50 @@ static int i2cdev_check(struct device *dev, void *addrp) | |||
193 | return dev->driver ? -EBUSY : 0; | 189 | return dev->driver ? -EBUSY : 0; |
194 | } | 190 | } |
195 | 191 | ||
192 | /* walk up mux tree */ | ||
193 | static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr) | ||
194 | { | ||
195 | int result; | ||
196 | |||
197 | result = device_for_each_child(&adapter->dev, &addr, i2cdev_check); | ||
198 | |||
199 | if (!result && i2c_parent_is_i2c_adapter(adapter)) | ||
200 | result = i2cdev_check_mux_parents( | ||
201 | to_i2c_adapter(adapter->dev.parent), addr); | ||
202 | |||
203 | return result; | ||
204 | } | ||
205 | |||
206 | /* recurse down mux tree */ | ||
207 | static int i2cdev_check_mux_children(struct device *dev, void *addrp) | ||
208 | { | ||
209 | int result; | ||
210 | |||
211 | if (dev->type == &i2c_adapter_type) | ||
212 | result = device_for_each_child(dev, addrp, | ||
213 | i2cdev_check_mux_children); | ||
214 | else | ||
215 | result = i2cdev_check(dev, addrp); | ||
216 | |||
217 | return result; | ||
218 | } | ||
219 | |||
196 | /* This address checking function differs from the one in i2c-core | 220 | /* This address checking function differs from the one in i2c-core |
197 | in that it considers an address with a registered device, but no | 221 | in that it considers an address with a registered device, but no |
198 | driver bound to it, as NOT busy. */ | 222 | driver bound to it, as NOT busy. */ |
199 | static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr) | 223 | static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr) |
200 | { | 224 | { |
201 | return device_for_each_child(&adapter->dev, &addr, i2cdev_check); | 225 | int result = 0; |
226 | |||
227 | if (i2c_parent_is_i2c_adapter(adapter)) | ||
228 | result = i2cdev_check_mux_parents( | ||
229 | to_i2c_adapter(adapter->dev.parent), addr); | ||
230 | |||
231 | if (!result) | ||
232 | result = device_for_each_child(&adapter->dev, &addr, | ||
233 | i2cdev_check_mux_children); | ||
234 | |||
235 | return result; | ||
202 | } | 236 | } |
203 | 237 | ||
204 | static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client, | 238 | static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client, |
@@ -219,9 +253,7 @@ static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client, | |||
219 | if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS) | 253 | if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS) |
220 | return -EINVAL; | 254 | return -EINVAL; |
221 | 255 | ||
222 | rdwr_pa = (struct i2c_msg *) | 256 | rdwr_pa = kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), GFP_KERNEL); |
223 | kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), | ||
224 | GFP_KERNEL); | ||
225 | if (!rdwr_pa) | 257 | if (!rdwr_pa) |
226 | return -ENOMEM; | 258 | return -ENOMEM; |
227 | 259 | ||
@@ -247,15 +279,9 @@ static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client, | |||
247 | break; | 279 | break; |
248 | } | 280 | } |
249 | data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf; | 281 | data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf; |
250 | rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL); | 282 | rdwr_pa[i].buf = memdup_user(data_ptrs[i], rdwr_pa[i].len); |
251 | if (rdwr_pa[i].buf == NULL) { | 283 | if (IS_ERR(rdwr_pa[i].buf)) { |
252 | res = -ENOMEM; | 284 | res = PTR_ERR(rdwr_pa[i].buf); |
253 | break; | ||
254 | } | ||
255 | if (copy_from_user(rdwr_pa[i].buf, data_ptrs[i], | ||
256 | rdwr_pa[i].len)) { | ||
257 | ++i; /* Needs to be kfreed too */ | ||
258 | res = -EFAULT; | ||
259 | break; | 285 | break; |
260 | } | 286 | } |
261 | } | 287 | } |
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c new file mode 100644 index 000000000000..d32a4843fc3a --- /dev/null +++ b/drivers/i2c/i2c-mux.c | |||
@@ -0,0 +1,165 @@ | |||
1 | /* | ||
2 | * Multiplexed I2C bus driver. | ||
3 | * | ||
4 | * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it> | ||
5 | * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it> | ||
6 | * Copyright (c) 2009-2010 NSN GmbH & Co KG <michael.lawnick.ext@nsn.com> | ||
7 | * | ||
8 | * Simplifies access to complex multiplexed I2C bus topologies, by presenting | ||
9 | * each multiplexed bus segment as an additional I2C adapter. | ||
10 | * Supports multi-level mux'ing (mux behind a mux). | ||
11 | * | ||
12 | * Based on: | ||
13 | * i2c-virt.c from Kumar Gala <galak@kernel.crashing.org> | ||
14 | * i2c-virtual.c from Ken Harrenstien, Copyright (c) 2004 Google, Inc. | ||
15 | * i2c-virtual.c from Brian Kuschak <bkuschak@yahoo.com> | ||
16 | * | ||
17 | * This file is licensed under the terms of the GNU General Public | ||
18 | * License version 2. This program is licensed "as is" without any | ||
19 | * warranty of any kind, whether express or implied. | ||
20 | */ | ||
21 | |||
22 | #include <linux/kernel.h> | ||
23 | #include <linux/module.h> | ||
24 | #include <linux/slab.h> | ||
25 | #include <linux/i2c.h> | ||
26 | #include <linux/i2c-mux.h> | ||
27 | |||
28 | /* multiplexer per channel data */ | ||
29 | struct i2c_mux_priv { | ||
30 | struct i2c_adapter adap; | ||
31 | struct i2c_algorithm algo; | ||
32 | |||
33 | struct i2c_adapter *parent; | ||
34 | void *mux_dev; /* the mux chip/device */ | ||
35 | u32 chan_id; /* the channel id */ | ||
36 | |||
37 | int (*select)(struct i2c_adapter *, void *mux_dev, u32 chan_id); | ||
38 | int (*deselect)(struct i2c_adapter *, void *mux_dev, u32 chan_id); | ||
39 | }; | ||
40 | |||
41 | static int i2c_mux_master_xfer(struct i2c_adapter *adap, | ||
42 | struct i2c_msg msgs[], int num) | ||
43 | { | ||
44 | struct i2c_mux_priv *priv = adap->algo_data; | ||
45 | struct i2c_adapter *parent = priv->parent; | ||
46 | int ret; | ||
47 | |||
48 | /* Switch to the right mux port and perform the transfer. */ | ||
49 | |||
50 | ret = priv->select(parent, priv->mux_dev, priv->chan_id); | ||
51 | if (ret >= 0) | ||
52 | ret = parent->algo->master_xfer(parent, msgs, num); | ||
53 | if (priv->deselect) | ||
54 | priv->deselect(parent, priv->mux_dev, priv->chan_id); | ||
55 | |||
56 | return ret; | ||
57 | } | ||
58 | |||
59 | static int i2c_mux_smbus_xfer(struct i2c_adapter *adap, | ||
60 | u16 addr, unsigned short flags, | ||
61 | char read_write, u8 command, | ||
62 | int size, union i2c_smbus_data *data) | ||
63 | { | ||
64 | struct i2c_mux_priv *priv = adap->algo_data; | ||
65 | struct i2c_adapter *parent = priv->parent; | ||
66 | int ret; | ||
67 | |||
68 | /* Select the right mux port and perform the transfer. */ | ||
69 | |||
70 | ret = priv->select(parent, priv->mux_dev, priv->chan_id); | ||
71 | if (ret >= 0) | ||
72 | ret = parent->algo->smbus_xfer(parent, addr, flags, | ||
73 | read_write, command, size, data); | ||
74 | if (priv->deselect) | ||
75 | priv->deselect(parent, priv->mux_dev, priv->chan_id); | ||
76 | |||
77 | return ret; | ||
78 | } | ||
79 | |||
80 | /* Return the parent's functionality */ | ||
81 | static u32 i2c_mux_functionality(struct i2c_adapter *adap) | ||
82 | { | ||
83 | struct i2c_mux_priv *priv = adap->algo_data; | ||
84 | struct i2c_adapter *parent = priv->parent; | ||
85 | |||
86 | return parent->algo->functionality(parent); | ||
87 | } | ||
88 | |||
89 | struct i2c_adapter *i2c_add_mux_adapter(struct i2c_adapter *parent, | ||
90 | void *mux_dev, u32 force_nr, u32 chan_id, | ||
91 | int (*select) (struct i2c_adapter *, | ||
92 | void *, u32), | ||
93 | int (*deselect) (struct i2c_adapter *, | ||
94 | void *, u32)) | ||
95 | { | ||
96 | struct i2c_mux_priv *priv; | ||
97 | int ret; | ||
98 | |||
99 | priv = kzalloc(sizeof(struct i2c_mux_priv), GFP_KERNEL); | ||
100 | if (!priv) | ||
101 | return NULL; | ||
102 | |||
103 | /* Set up private adapter data */ | ||
104 | priv->parent = parent; | ||
105 | priv->mux_dev = mux_dev; | ||
106 | priv->chan_id = chan_id; | ||
107 | priv->select = select; | ||
108 | priv->deselect = deselect; | ||
109 | |||
110 | /* Need to do algo dynamically because we don't know ahead | ||
111 | * of time what sort of physical adapter we'll be dealing with. | ||
112 | */ | ||
113 | if (parent->algo->master_xfer) | ||
114 | priv->algo.master_xfer = i2c_mux_master_xfer; | ||
115 | if (parent->algo->smbus_xfer) | ||
116 | priv->algo.smbus_xfer = i2c_mux_smbus_xfer; | ||
117 | priv->algo.functionality = i2c_mux_functionality; | ||
118 | |||
119 | /* Now fill out new adapter structure */ | ||
120 | snprintf(priv->adap.name, sizeof(priv->adap.name), | ||
121 | "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id); | ||
122 | priv->adap.owner = THIS_MODULE; | ||
123 | priv->adap.id = parent->id; | ||
124 | priv->adap.algo = &priv->algo; | ||
125 | priv->adap.algo_data = priv; | ||
126 | priv->adap.dev.parent = &parent->dev; | ||
127 | |||
128 | if (force_nr) { | ||
129 | priv->adap.nr = force_nr; | ||
130 | ret = i2c_add_numbered_adapter(&priv->adap); | ||
131 | } else { | ||
132 | ret = i2c_add_adapter(&priv->adap); | ||
133 | } | ||
134 | if (ret < 0) { | ||
135 | dev_err(&parent->dev, | ||
136 | "failed to add mux-adapter (error=%d)\n", | ||
137 | ret); | ||
138 | kfree(priv); | ||
139 | return NULL; | ||
140 | } | ||
141 | |||
142 | dev_info(&parent->dev, "Added multiplexed i2c bus %d\n", | ||
143 | i2c_adapter_id(&priv->adap)); | ||
144 | |||
145 | return &priv->adap; | ||
146 | } | ||
147 | EXPORT_SYMBOL_GPL(i2c_add_mux_adapter); | ||
148 | |||
149 | int i2c_del_mux_adapter(struct i2c_adapter *adap) | ||
150 | { | ||
151 | struct i2c_mux_priv *priv = adap->algo_data; | ||
152 | int ret; | ||
153 | |||
154 | ret = i2c_del_adapter(adap); | ||
155 | if (ret < 0) | ||
156 | return ret; | ||
157 | kfree(priv); | ||
158 | |||
159 | return 0; | ||
160 | } | ||
161 | EXPORT_SYMBOL_GPL(i2c_del_mux_adapter); | ||
162 | |||
163 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | ||
164 | MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses"); | ||
165 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig new file mode 100644 index 000000000000..4c9a99c4fcb0 --- /dev/null +++ b/drivers/i2c/muxes/Kconfig | |||
@@ -0,0 +1,18 @@ | |||
1 | # | ||
2 | # Multiplexer I2C chip drivers configuration | ||
3 | # | ||
4 | |||
5 | menu "Multiplexer I2C Chip support" | ||
6 | depends on I2C_MUX | ||
7 | |||
8 | config I2C_MUX_PCA954x | ||
9 | tristate "Philips PCA954x I2C Mux/switches" | ||
10 | depends on EXPERIMENTAL | ||
11 | help | ||
12 | If you say yes here you get support for the Philips PCA954x | ||
13 | I2C mux/switch devices. | ||
14 | |||
15 | This driver can also be built as a module. If so, the module | ||
16 | will be called pca954x. | ||
17 | |||
18 | endmenu | ||
diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile new file mode 100644 index 000000000000..bd83b5274815 --- /dev/null +++ b/drivers/i2c/muxes/Makefile | |||
@@ -0,0 +1,8 @@ | |||
1 | # | ||
2 | # Makefile for multiplexer I2C chip drivers. | ||
3 | |||
4 | obj-$(CONFIG_I2C_MUX_PCA954x) += pca954x.o | ||
5 | |||
6 | ifeq ($(CONFIG_I2C_DEBUG_BUS),y) | ||
7 | EXTRA_CFLAGS += -DDEBUG | ||
8 | endif | ||
diff --git a/drivers/i2c/muxes/pca954x.c b/drivers/i2c/muxes/pca954x.c new file mode 100644 index 000000000000..6f9accf3189d --- /dev/null +++ b/drivers/i2c/muxes/pca954x.c | |||
@@ -0,0 +1,301 @@ | |||
1 | /* | ||
2 | * I2C multiplexer | ||
3 | * | ||
4 | * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it> | ||
5 | * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it> | ||
6 | * | ||
7 | * This module supports the PCA954x series of I2C multiplexer/switch chips | ||
8 | * made by Philips Semiconductors. | ||
9 | * This includes the: | ||
10 | * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547 | ||
11 | * and PCA9548. | ||
12 | * | ||
13 | * These chips are all controlled via the I2C bus itself, and all have a | ||
14 | * single 8-bit register. The upstream "parent" bus fans out to two, | ||
15 | * four, or eight downstream busses or channels; which of these | ||
16 | * are selected is determined by the chip type and register contents. A | ||
17 | * mux can select only one sub-bus at a time; a switch can select any | ||
18 | * combination simultaneously. | ||
19 | * | ||
20 | * Based on: | ||
21 | * pca954x.c from Kumar Gala <galak@kernel.crashing.org> | ||
22 | * Copyright (C) 2006 | ||
23 | * | ||
24 | * Based on: | ||
25 | * pca954x.c from Ken Harrenstien | ||
26 | * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) | ||
27 | * | ||
28 | * Based on: | ||
29 | * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com> | ||
30 | * and | ||
31 | * pca9540.c from Jean Delvare <khali@linux-fr.org>. | ||
32 | * | ||
33 | * This file is licensed under the terms of the GNU General Public | ||
34 | * License version 2. This program is licensed "as is" without any | ||
35 | * warranty of any kind, whether express or implied. | ||
36 | */ | ||
37 | |||
38 | #include <linux/module.h> | ||
39 | #include <linux/init.h> | ||
40 | #include <linux/slab.h> | ||
41 | #include <linux/device.h> | ||
42 | #include <linux/i2c.h> | ||
43 | #include <linux/i2c-mux.h> | ||
44 | |||
45 | #include <linux/i2c/pca954x.h> | ||
46 | |||
47 | #define PCA954X_MAX_NCHANS 8 | ||
48 | |||
49 | enum pca_type { | ||
50 | pca_9540, | ||
51 | pca_9542, | ||
52 | pca_9543, | ||
53 | pca_9544, | ||
54 | pca_9545, | ||
55 | pca_9546, | ||
56 | pca_9547, | ||
57 | pca_9548, | ||
58 | }; | ||
59 | |||
60 | struct pca954x { | ||
61 | enum pca_type type; | ||
62 | struct i2c_adapter *virt_adaps[PCA954X_MAX_NCHANS]; | ||
63 | |||
64 | u8 last_chan; /* last register value */ | ||
65 | }; | ||
66 | |||
67 | struct chip_desc { | ||
68 | u8 nchans; | ||
69 | u8 enable; /* used for muxes only */ | ||
70 | enum muxtype { | ||
71 | pca954x_ismux = 0, | ||
72 | pca954x_isswi | ||
73 | } muxtype; | ||
74 | }; | ||
75 | |||
76 | /* Provide specs for the PCA954x types we know about */ | ||
77 | static const struct chip_desc chips[] = { | ||
78 | [pca_9540] = { | ||
79 | .nchans = 2, | ||
80 | .enable = 0x4, | ||
81 | .muxtype = pca954x_ismux, | ||
82 | }, | ||
83 | [pca_9543] = { | ||
84 | .nchans = 2, | ||
85 | .muxtype = pca954x_isswi, | ||
86 | }, | ||
87 | [pca_9544] = { | ||
88 | .nchans = 4, | ||
89 | .enable = 0x4, | ||
90 | .muxtype = pca954x_ismux, | ||
91 | }, | ||
92 | [pca_9545] = { | ||
93 | .nchans = 4, | ||
94 | .muxtype = pca954x_isswi, | ||
95 | }, | ||
96 | [pca_9547] = { | ||
97 | .nchans = 8, | ||
98 | .enable = 0x8, | ||
99 | .muxtype = pca954x_ismux, | ||
100 | }, | ||
101 | [pca_9548] = { | ||
102 | .nchans = 8, | ||
103 | .muxtype = pca954x_isswi, | ||
104 | }, | ||
105 | }; | ||
106 | |||
107 | static const struct i2c_device_id pca954x_id[] = { | ||
108 | { "pca9540", pca_9540 }, | ||
109 | { "pca9542", pca_9540 }, | ||
110 | { "pca9543", pca_9543 }, | ||
111 | { "pca9544", pca_9544 }, | ||
112 | { "pca9545", pca_9545 }, | ||
113 | { "pca9546", pca_9545 }, | ||
114 | { "pca9547", pca_9547 }, | ||
115 | { "pca9548", pca_9548 }, | ||
116 | { } | ||
117 | }; | ||
118 | MODULE_DEVICE_TABLE(i2c, pca954x_id); | ||
119 | |||
120 | /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer() | ||
121 | for this as they will try to lock adapter a second time */ | ||
122 | static int pca954x_reg_write(struct i2c_adapter *adap, | ||
123 | struct i2c_client *client, u8 val) | ||
124 | { | ||
125 | int ret = -ENODEV; | ||
126 | |||
127 | if (adap->algo->master_xfer) { | ||
128 | struct i2c_msg msg; | ||
129 | char buf[1]; | ||
130 | |||
131 | msg.addr = client->addr; | ||
132 | msg.flags = 0; | ||
133 | msg.len = 1; | ||
134 | buf[0] = val; | ||
135 | msg.buf = buf; | ||
136 | ret = adap->algo->master_xfer(adap, &msg, 1); | ||
137 | } else { | ||
138 | union i2c_smbus_data data; | ||
139 | ret = adap->algo->smbus_xfer(adap, client->addr, | ||
140 | client->flags, | ||
141 | I2C_SMBUS_WRITE, | ||
142 | val, I2C_SMBUS_BYTE, &data); | ||
143 | } | ||
144 | |||
145 | return ret; | ||
146 | } | ||
147 | |||
148 | static int pca954x_select_chan(struct i2c_adapter *adap, | ||
149 | void *client, u32 chan) | ||
150 | { | ||
151 | struct pca954x *data = i2c_get_clientdata(client); | ||
152 | const struct chip_desc *chip = &chips[data->type]; | ||
153 | u8 regval; | ||
154 | int ret = 0; | ||
155 | |||
156 | /* we make switches look like muxes, not sure how to be smarter */ | ||
157 | if (chip->muxtype == pca954x_ismux) | ||
158 | regval = chan | chip->enable; | ||
159 | else | ||
160 | regval = 1 << chan; | ||
161 | |||
162 | /* Only select the channel if its different from the last channel */ | ||
163 | if (data->last_chan != regval) { | ||
164 | ret = pca954x_reg_write(adap, client, regval); | ||
165 | data->last_chan = regval; | ||
166 | } | ||
167 | |||
168 | return ret; | ||
169 | } | ||
170 | |||
171 | static int pca954x_deselect_mux(struct i2c_adapter *adap, | ||
172 | void *client, u32 chan) | ||
173 | { | ||
174 | struct pca954x *data = i2c_get_clientdata(client); | ||
175 | |||
176 | /* Deselect active channel */ | ||
177 | data->last_chan = 0; | ||
178 | return pca954x_reg_write(adap, client, data->last_chan); | ||
179 | } | ||
180 | |||
181 | /* | ||
182 | * I2C init/probing/exit functions | ||
183 | */ | ||
184 | static int __devinit pca954x_probe(struct i2c_client *client, | ||
185 | const struct i2c_device_id *id) | ||
186 | { | ||
187 | struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); | ||
188 | struct pca954x_platform_data *pdata = client->dev.platform_data; | ||
189 | int num, force; | ||
190 | struct pca954x *data; | ||
191 | int ret = -ENODEV; | ||
192 | |||
193 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) | ||
194 | goto err; | ||
195 | |||
196 | data = kzalloc(sizeof(struct pca954x), GFP_KERNEL); | ||
197 | if (!data) { | ||
198 | ret = -ENOMEM; | ||
199 | goto err; | ||
200 | } | ||
201 | |||
202 | i2c_set_clientdata(client, data); | ||
203 | |||
204 | /* Read the mux register at addr to verify | ||
205 | * that the mux is in fact present. | ||
206 | */ | ||
207 | if (i2c_smbus_read_byte(client) < 0) { | ||
208 | dev_warn(&client->dev, "probe failed\n"); | ||
209 | goto exit_free; | ||
210 | } | ||
211 | |||
212 | data->type = id->driver_data; | ||
213 | data->last_chan = 0; /* force the first selection */ | ||
214 | |||
215 | /* Now create an adapter for each channel */ | ||
216 | for (num = 0; num < chips[data->type].nchans; num++) { | ||
217 | force = 0; /* dynamic adap number */ | ||
218 | if (pdata) { | ||
219 | if (num < pdata->num_modes) | ||
220 | /* force static number */ | ||
221 | force = pdata->modes[num].adap_id; | ||
222 | else | ||
223 | /* discard unconfigured channels */ | ||
224 | break; | ||
225 | } | ||
226 | |||
227 | data->virt_adaps[num] = | ||
228 | i2c_add_mux_adapter(adap, client, | ||
229 | force, num, pca954x_select_chan, | ||
230 | (pdata && pdata->modes[num].deselect_on_exit) | ||
231 | ? pca954x_deselect_mux : NULL); | ||
232 | |||
233 | if (data->virt_adaps[num] == NULL) { | ||
234 | ret = -ENODEV; | ||
235 | dev_err(&client->dev, | ||
236 | "failed to register multiplexed adapter" | ||
237 | " %d as bus %d\n", num, force); | ||
238 | goto virt_reg_failed; | ||
239 | } | ||
240 | } | ||
241 | |||
242 | dev_info(&client->dev, | ||
243 | "registered %d multiplexed busses for I2C %s %s\n", | ||
244 | num, chips[data->type].muxtype == pca954x_ismux | ||
245 | ? "mux" : "switch", client->name); | ||
246 | |||
247 | return 0; | ||
248 | |||
249 | virt_reg_failed: | ||
250 | for (num--; num >= 0; num--) | ||
251 | i2c_del_mux_adapter(data->virt_adaps[num]); | ||
252 | exit_free: | ||
253 | kfree(data); | ||
254 | err: | ||
255 | return ret; | ||
256 | } | ||
257 | |||
258 | static int __devexit pca954x_remove(struct i2c_client *client) | ||
259 | { | ||
260 | struct pca954x *data = i2c_get_clientdata(client); | ||
261 | const struct chip_desc *chip = &chips[data->type]; | ||
262 | int i, err; | ||
263 | |||
264 | for (i = 0; i < chip->nchans; ++i) | ||
265 | if (data->virt_adaps[i]) { | ||
266 | err = i2c_del_mux_adapter(data->virt_adaps[i]); | ||
267 | if (err) | ||
268 | return err; | ||
269 | data->virt_adaps[i] = NULL; | ||
270 | } | ||
271 | |||
272 | kfree(data); | ||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | static struct i2c_driver pca954x_driver = { | ||
277 | .driver = { | ||
278 | .name = "pca954x", | ||
279 | .owner = THIS_MODULE, | ||
280 | }, | ||
281 | .probe = pca954x_probe, | ||
282 | .remove = __devexit_p(pca954x_remove), | ||
283 | .id_table = pca954x_id, | ||
284 | }; | ||
285 | |||
286 | static int __init pca954x_init(void) | ||
287 | { | ||
288 | return i2c_add_driver(&pca954x_driver); | ||
289 | } | ||
290 | |||
291 | static void __exit pca954x_exit(void) | ||
292 | { | ||
293 | i2c_del_driver(&pca954x_driver); | ||
294 | } | ||
295 | |||
296 | module_init(pca954x_init); | ||
297 | module_exit(pca954x_exit); | ||
298 | |||
299 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | ||
300 | MODULE_DESCRIPTION("PCA954x I2C mux/switch driver"); | ||
301 | MODULE_LICENSE("GPL v2"); | ||