1 /* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 #include <linux/gpio.h>
18 #include <linux/i2c.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/i2c/sx150x.h>
28 struct sx150x_device_data
{
45 struct gpio_chip gpio_chip
;
46 struct i2c_client
*client
;
47 const struct sx150x_device_data
*dev_cfg
;
51 unsigned long irq_set_type_pending
;
52 struct irq_chip irq_chip
;
56 static const struct sx150x_device_data sx150x_devices
[] = {
89 static const struct i2c_device_id sx150x_id
[] = {
94 MODULE_DEVICE_TABLE(i2c
, sx150x_id
);
96 static s32
sx150x_i2c_write(struct i2c_client
*client
, u8 reg
, u8 val
)
98 s32 err
= i2c_smbus_write_byte_data(client
, reg
, val
);
101 dev_warn(&client
->dev
,
102 "i2c write fail: can't write %02x to %02x: %d\n",
107 static s32
sx150x_i2c_read(struct i2c_client
*client
, u8 reg
, u8
*val
)
109 s32 err
= i2c_smbus_read_byte_data(client
, reg
);
114 dev_warn(&client
->dev
,
115 "i2c read fail: can't read from %02x: %d\n",
120 static inline bool offset_is_oscio(struct sx150x_chip
*chip
, unsigned offset
)
122 return (chip
->dev_cfg
->ngpios
== offset
);
126 * These utility functions solve the common problem of locating and setting
127 * configuration bits. Configuration bits are grouped into registers
128 * whose indexes increase downwards. For example, with eight-bit registers,
129 * sixteen gpios would have their config bits grouped in the following order:
130 * REGISTER N-1 [ f e d c b a 9 8 ]
131 * N [ 7 6 5 4 3 2 1 0 ]
133 * For multi-bit configurations, the pattern gets wider:
134 * REGISTER N-3 [ f f e e d d c c ]
135 * N-2 [ b b a a 9 9 8 8 ]
136 * N-1 [ 7 7 6 6 5 5 4 4 ]
137 * N [ 3 3 2 2 1 1 0 0 ]
139 * Given the address of the starting register 'N', the index of the gpio
140 * whose configuration we seek to change, and the width in bits of that
141 * configuration, these functions allow us to locate the correct
142 * register and mask the correct bits.
144 static inline void sx150x_find_cfg(u8 offset
, u8 width
,
145 u8
*reg
, u8
*mask
, u8
*shift
)
147 *reg
-= offset
* width
/ 8;
148 *mask
= (1 << width
) - 1;
149 *shift
= (offset
* width
) % 8;
153 static s32
sx150x_write_cfg(struct sx150x_chip
*chip
,
154 u8 offset
, u8 width
, u8 reg
, u8 val
)
161 sx150x_find_cfg(offset
, width
, ®
, &mask
, &shift
);
162 err
= sx150x_i2c_read(chip
->client
, reg
, &data
);
167 data
|= (val
<< shift
) & mask
;
168 return sx150x_i2c_write(chip
->client
, reg
, data
);
171 static int sx150x_get_io(struct sx150x_chip
*chip
, unsigned offset
)
173 u8 reg
= chip
->dev_cfg
->reg_data
;
179 sx150x_find_cfg(offset
, 1, ®
, &mask
, &shift
);
180 err
= sx150x_i2c_read(chip
->client
, reg
, &data
);
182 err
= (data
& mask
) != 0 ? 1 : 0;
187 static void sx150x_set_oscio(struct sx150x_chip
*chip
, int val
)
189 sx150x_i2c_write(chip
->client
,
190 chip
->dev_cfg
->reg_clock
,
191 (val
? 0x1f : 0x10));
194 static void sx150x_set_io(struct sx150x_chip
*chip
, unsigned offset
, int val
)
196 sx150x_write_cfg(chip
,
199 chip
->dev_cfg
->reg_data
,
203 static int sx150x_io_input(struct sx150x_chip
*chip
, unsigned offset
)
205 return sx150x_write_cfg(chip
,
208 chip
->dev_cfg
->reg_dir
,
212 static int sx150x_io_output(struct sx150x_chip
*chip
, unsigned offset
, int val
)
216 err
= sx150x_write_cfg(chip
,
219 chip
->dev_cfg
->reg_data
,
222 err
= sx150x_write_cfg(chip
,
225 chip
->dev_cfg
->reg_dir
,
230 static int sx150x_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
232 struct sx150x_chip
*chip
;
233 int status
= -EINVAL
;
235 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
237 if (!offset_is_oscio(chip
, offset
)) {
238 mutex_lock(&chip
->lock
);
239 status
= sx150x_get_io(chip
, offset
);
240 mutex_unlock(&chip
->lock
);
246 static void sx150x_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int val
)
248 struct sx150x_chip
*chip
;
250 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
252 mutex_lock(&chip
->lock
);
253 if (offset_is_oscio(chip
, offset
))
254 sx150x_set_oscio(chip
, val
);
256 sx150x_set_io(chip
, offset
, val
);
257 mutex_unlock(&chip
->lock
);
260 static int sx150x_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
262 struct sx150x_chip
*chip
;
263 int status
= -EINVAL
;
265 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
267 if (!offset_is_oscio(chip
, offset
)) {
268 mutex_lock(&chip
->lock
);
269 status
= sx150x_io_input(chip
, offset
);
270 mutex_unlock(&chip
->lock
);
275 static int sx150x_gpio_direction_output(struct gpio_chip
*gc
,
279 struct sx150x_chip
*chip
;
282 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
284 if (!offset_is_oscio(chip
, offset
)) {
285 mutex_lock(&chip
->lock
);
286 status
= sx150x_io_output(chip
, offset
, val
);
287 mutex_unlock(&chip
->lock
);
292 static int sx150x_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
294 struct sx150x_chip
*chip
;
296 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
298 if (offset
>= chip
->dev_cfg
->ngpios
)
301 if (chip
->irq_base
< 0)
304 return chip
->irq_base
+ offset
;
307 static void sx150x_irq_mask(struct irq_data
*d
)
309 struct irq_chip
*ic
= irq_data_get_irq_chip(d
);
310 struct sx150x_chip
*chip
;
313 chip
= container_of(ic
, struct sx150x_chip
, irq_chip
);
314 n
= d
->irq
- chip
->irq_base
;
316 sx150x_write_cfg(chip
, n
, 1, chip
->dev_cfg
->reg_irq_mask
, 1);
317 sx150x_write_cfg(chip
, n
, 2, chip
->dev_cfg
->reg_sense
, 0);
320 static void sx150x_irq_unmask(struct irq_data
*d
)
322 struct irq_chip
*ic
= irq_data_get_irq_chip(d
);
323 struct sx150x_chip
*chip
;
326 chip
= container_of(ic
, struct sx150x_chip
, irq_chip
);
327 n
= d
->irq
- chip
->irq_base
;
329 sx150x_write_cfg(chip
, n
, 1, chip
->dev_cfg
->reg_irq_mask
, 0);
330 sx150x_write_cfg(chip
, n
, 2, chip
->dev_cfg
->reg_sense
,
331 chip
->irq_sense
>> (n
* 2));
334 static int sx150x_irq_set_type(struct irq_data
*d
, unsigned int flow_type
)
336 struct irq_chip
*ic
= irq_data_get_irq_chip(d
);
337 struct sx150x_chip
*chip
;
340 if (flow_type
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
))
343 chip
= container_of(ic
, struct sx150x_chip
, irq_chip
);
344 n
= d
->irq
- chip
->irq_base
;
346 if (flow_type
& IRQ_TYPE_EDGE_RISING
)
348 if (flow_type
& IRQ_TYPE_EDGE_FALLING
)
351 chip
->irq_sense
&= ~(3UL << (n
* 2));
352 chip
->irq_sense
|= val
<< (n
* 2);
353 chip
->irq_set_type_pending
|= BIT(n
);
357 static irqreturn_t
sx150x_irq_thread_fn(int irq
, void *dev_id
)
359 struct sx150x_chip
*chip
= (struct sx150x_chip
*)dev_id
;
360 unsigned nhandled
= 0;
367 for (i
= (chip
->dev_cfg
->ngpios
/ 8) - 1; i
>= 0; --i
) {
368 err
= sx150x_i2c_read(chip
->client
,
369 chip
->dev_cfg
->reg_irq_src
- i
,
374 sx150x_i2c_write(chip
->client
,
375 chip
->dev_cfg
->reg_irq_src
- i
,
377 for (n
= 0; n
< 8; ++n
) {
378 if (val
& (1 << n
)) {
379 sub_irq
= chip
->irq_base
+ (i
* 8) + n
;
380 handle_nested_irq(sub_irq
);
386 return (nhandled
> 0 ? IRQ_HANDLED
: IRQ_NONE
);
389 static void sx150x_irq_bus_lock(struct irq_data
*d
)
391 struct irq_chip
*ic
= irq_data_get_irq_chip(d
);
392 struct sx150x_chip
*chip
;
394 chip
= container_of(ic
, struct sx150x_chip
, irq_chip
);
396 mutex_lock(&chip
->lock
);
399 static void sx150x_irq_bus_sync_unlock(struct irq_data
*d
)
401 struct irq_chip
*ic
= irq_data_get_irq_chip(d
);
402 struct sx150x_chip
*chip
;
405 chip
= container_of(ic
, struct sx150x_chip
, irq_chip
);
407 while (chip
->irq_set_type_pending
) {
408 n
= __ffs(chip
->irq_set_type_pending
);
409 chip
->irq_set_type_pending
&= ~BIT(n
);
410 if (!(irq_to_desc(n
+ chip
->irq_base
)->status
& IRQ_MASKED
))
411 sx150x_write_cfg(chip
, n
, 2,
412 chip
->dev_cfg
->reg_sense
,
413 chip
->irq_sense
>> (n
* 2));
416 mutex_unlock(&chip
->lock
);
419 static void sx150x_init_chip(struct sx150x_chip
*chip
,
420 struct i2c_client
*client
,
421 kernel_ulong_t driver_data
,
422 struct sx150x_platform_data
*pdata
)
424 mutex_init(&chip
->lock
);
426 chip
->client
= client
;
427 chip
->dev_cfg
= &sx150x_devices
[driver_data
];
428 chip
->gpio_chip
.label
= client
->name
;
429 chip
->gpio_chip
.direction_input
= sx150x_gpio_direction_input
;
430 chip
->gpio_chip
.direction_output
= sx150x_gpio_direction_output
;
431 chip
->gpio_chip
.get
= sx150x_gpio_get
;
432 chip
->gpio_chip
.set
= sx150x_gpio_set
;
433 chip
->gpio_chip
.to_irq
= sx150x_gpio_to_irq
;
434 chip
->gpio_chip
.base
= pdata
->gpio_base
;
435 chip
->gpio_chip
.can_sleep
= 1;
436 chip
->gpio_chip
.ngpio
= chip
->dev_cfg
->ngpios
;
437 if (pdata
->oscio_is_gpo
)
438 ++chip
->gpio_chip
.ngpio
;
440 chip
->irq_chip
.name
= client
->name
;
441 chip
->irq_chip
.irq_mask
= sx150x_irq_mask
;
442 chip
->irq_chip
.irq_unmask
= sx150x_irq_unmask
;
443 chip
->irq_chip
.irq_set_type
= sx150x_irq_set_type
;
444 chip
->irq_chip
.irq_bus_lock
= sx150x_irq_bus_lock
;
445 chip
->irq_chip
.irq_bus_sync_unlock
= sx150x_irq_bus_sync_unlock
;
446 chip
->irq_summary
= -1;
449 chip
->irq_set_type_pending
= 0;
452 static int sx150x_init_io(struct sx150x_chip
*chip
, u8 base
, u16 cfg
)
457 for (n
= 0; err
>= 0 && n
< (chip
->dev_cfg
->ngpios
/ 8); ++n
)
458 err
= sx150x_i2c_write(chip
->client
, base
- n
, cfg
>> (n
* 8));
462 static int sx150x_reset(struct sx150x_chip
*chip
)
466 err
= i2c_smbus_write_byte_data(chip
->client
,
467 chip
->dev_cfg
->reg_reset
,
472 err
= i2c_smbus_write_byte_data(chip
->client
,
473 chip
->dev_cfg
->reg_reset
,
478 static int sx150x_init_hw(struct sx150x_chip
*chip
,
479 struct sx150x_platform_data
*pdata
)
483 if (pdata
->reset_during_probe
) {
484 err
= sx150x_reset(chip
);
489 err
= sx150x_i2c_write(chip
->client
,
490 chip
->dev_cfg
->reg_misc
,
495 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_pullup
,
496 pdata
->io_pullup_ena
);
500 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_pulldn
,
501 pdata
->io_pulldn_ena
);
505 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_drain
,
506 pdata
->io_open_drain_ena
);
510 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_polarity
,
515 if (pdata
->oscio_is_gpo
)
516 sx150x_set_oscio(chip
, 0);
521 static int sx150x_install_irq_chip(struct sx150x_chip
*chip
,
529 chip
->irq_summary
= irq_summary
;
530 chip
->irq_base
= irq_base
;
532 for (n
= 0; n
< chip
->dev_cfg
->ngpios
; ++n
) {
534 set_irq_chip_and_handler(irq
, &chip
->irq_chip
, handle_edge_irq
);
535 set_irq_nested_thread(irq
, 1);
537 set_irq_flags(irq
, IRQF_VALID
);
539 set_irq_noprobe(irq
);
543 err
= request_threaded_irq(irq_summary
,
545 sx150x_irq_thread_fn
,
546 IRQF_SHARED
| IRQF_TRIGGER_FALLING
,
550 chip
->irq_summary
= -1;
557 static void sx150x_remove_irq_chip(struct sx150x_chip
*chip
)
562 free_irq(chip
->irq_summary
, chip
);
564 for (n
= 0; n
< chip
->dev_cfg
->ngpios
; ++n
) {
565 irq
= chip
->irq_base
+ n
;
566 set_irq_handler(irq
, NULL
);
567 set_irq_chip(irq
, NULL
);
571 static int __devinit
sx150x_probe(struct i2c_client
*client
,
572 const struct i2c_device_id
*id
)
574 static const u32 i2c_funcs
= I2C_FUNC_SMBUS_BYTE_DATA
|
575 I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
576 struct sx150x_platform_data
*pdata
;
577 struct sx150x_chip
*chip
;
580 pdata
= client
->dev
.platform_data
;
584 if (!i2c_check_functionality(client
->adapter
, i2c_funcs
))
587 chip
= kzalloc(sizeof(struct sx150x_chip
), GFP_KERNEL
);
591 sx150x_init_chip(chip
, client
, id
->driver_data
, pdata
);
592 rc
= sx150x_init_hw(chip
, pdata
);
594 goto probe_fail_pre_gpiochip_add
;
596 rc
= gpiochip_add(&chip
->gpio_chip
);
598 goto probe_fail_pre_gpiochip_add
;
600 if (pdata
->irq_summary
>= 0) {
601 rc
= sx150x_install_irq_chip(chip
,
605 goto probe_fail_post_gpiochip_add
;
608 i2c_set_clientdata(client
, chip
);
611 probe_fail_post_gpiochip_add
:
612 WARN_ON(gpiochip_remove(&chip
->gpio_chip
) < 0);
613 probe_fail_pre_gpiochip_add
:
618 static int __devexit
sx150x_remove(struct i2c_client
*client
)
620 struct sx150x_chip
*chip
;
623 chip
= i2c_get_clientdata(client
);
624 rc
= gpiochip_remove(&chip
->gpio_chip
);
628 if (chip
->irq_summary
>= 0)
629 sx150x_remove_irq_chip(chip
);
636 static struct i2c_driver sx150x_driver
= {
641 .probe
= sx150x_probe
,
642 .remove
= __devexit_p(sx150x_remove
),
643 .id_table
= sx150x_id
,
646 static int __init
sx150x_init(void)
648 return i2c_add_driver(&sx150x_driver
);
650 subsys_initcall(sx150x_init
);
652 static void __exit
sx150x_exit(void)
654 return i2c_del_driver(&sx150x_driver
);
656 module_exit(sx150x_exit
);
658 MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
659 MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders");
660 MODULE_LICENSE("GPL v2");
661 MODULE_ALIAS("i2c:sx150x");