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 #define NO_UPDATE_PENDING -1
30 struct sx150x_device_data
{
47 struct gpio_chip gpio_chip
;
48 struct i2c_client
*client
;
49 const struct sx150x_device_data
*dev_cfg
;
57 struct irq_chip irq_chip
;
61 static const struct sx150x_device_data sx150x_devices
[] = {
94 static const struct i2c_device_id sx150x_id
[] = {
99 MODULE_DEVICE_TABLE(i2c
, sx150x_id
);
101 static s32
sx150x_i2c_write(struct i2c_client
*client
, u8 reg
, u8 val
)
103 s32 err
= i2c_smbus_write_byte_data(client
, reg
, val
);
106 dev_warn(&client
->dev
,
107 "i2c write fail: can't write %02x to %02x: %d\n",
112 static s32
sx150x_i2c_read(struct i2c_client
*client
, u8 reg
, u8
*val
)
114 s32 err
= i2c_smbus_read_byte_data(client
, reg
);
119 dev_warn(&client
->dev
,
120 "i2c read fail: can't read from %02x: %d\n",
125 static inline bool offset_is_oscio(struct sx150x_chip
*chip
, unsigned offset
)
127 return (chip
->dev_cfg
->ngpios
== offset
);
131 * These utility functions solve the common problem of locating and setting
132 * configuration bits. Configuration bits are grouped into registers
133 * whose indexes increase downwards. For example, with eight-bit registers,
134 * sixteen gpios would have their config bits grouped in the following order:
135 * REGISTER N-1 [ f e d c b a 9 8 ]
136 * N [ 7 6 5 4 3 2 1 0 ]
138 * For multi-bit configurations, the pattern gets wider:
139 * REGISTER N-3 [ f f e e d d c c ]
140 * N-2 [ b b a a 9 9 8 8 ]
141 * N-1 [ 7 7 6 6 5 5 4 4 ]
142 * N [ 3 3 2 2 1 1 0 0 ]
144 * Given the address of the starting register 'N', the index of the gpio
145 * whose configuration we seek to change, and the width in bits of that
146 * configuration, these functions allow us to locate the correct
147 * register and mask the correct bits.
149 static inline void sx150x_find_cfg(u8 offset
, u8 width
,
150 u8
*reg
, u8
*mask
, u8
*shift
)
152 *reg
-= offset
* width
/ 8;
153 *mask
= (1 << width
) - 1;
154 *shift
= (offset
* width
) % 8;
158 static s32
sx150x_write_cfg(struct sx150x_chip
*chip
,
159 u8 offset
, u8 width
, u8 reg
, u8 val
)
166 sx150x_find_cfg(offset
, width
, ®
, &mask
, &shift
);
167 err
= sx150x_i2c_read(chip
->client
, reg
, &data
);
172 data
|= (val
<< shift
) & mask
;
173 return sx150x_i2c_write(chip
->client
, reg
, data
);
176 static int sx150x_get_io(struct sx150x_chip
*chip
, unsigned offset
)
178 u8 reg
= chip
->dev_cfg
->reg_data
;
184 sx150x_find_cfg(offset
, 1, ®
, &mask
, &shift
);
185 err
= sx150x_i2c_read(chip
->client
, reg
, &data
);
187 err
= (data
& mask
) != 0 ? 1 : 0;
192 static void sx150x_set_oscio(struct sx150x_chip
*chip
, int val
)
194 sx150x_i2c_write(chip
->client
,
195 chip
->dev_cfg
->reg_clock
,
196 (val
? 0x1f : 0x10));
199 static void sx150x_set_io(struct sx150x_chip
*chip
, unsigned offset
, int val
)
201 sx150x_write_cfg(chip
,
204 chip
->dev_cfg
->reg_data
,
208 static int sx150x_io_input(struct sx150x_chip
*chip
, unsigned offset
)
210 return sx150x_write_cfg(chip
,
213 chip
->dev_cfg
->reg_dir
,
217 static int sx150x_io_output(struct sx150x_chip
*chip
, unsigned offset
, int val
)
221 err
= sx150x_write_cfg(chip
,
224 chip
->dev_cfg
->reg_data
,
227 err
= sx150x_write_cfg(chip
,
230 chip
->dev_cfg
->reg_dir
,
235 static int sx150x_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
237 struct sx150x_chip
*chip
;
238 int status
= -EINVAL
;
240 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
242 if (!offset_is_oscio(chip
, offset
)) {
243 mutex_lock(&chip
->lock
);
244 status
= sx150x_get_io(chip
, offset
);
245 mutex_unlock(&chip
->lock
);
251 static void sx150x_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int val
)
253 struct sx150x_chip
*chip
;
255 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
257 mutex_lock(&chip
->lock
);
258 if (offset_is_oscio(chip
, offset
))
259 sx150x_set_oscio(chip
, val
);
261 sx150x_set_io(chip
, offset
, val
);
262 mutex_unlock(&chip
->lock
);
265 static int sx150x_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
267 struct sx150x_chip
*chip
;
268 int status
= -EINVAL
;
270 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
272 if (!offset_is_oscio(chip
, offset
)) {
273 mutex_lock(&chip
->lock
);
274 status
= sx150x_io_input(chip
, offset
);
275 mutex_unlock(&chip
->lock
);
280 static int sx150x_gpio_direction_output(struct gpio_chip
*gc
,
284 struct sx150x_chip
*chip
;
287 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
289 if (!offset_is_oscio(chip
, offset
)) {
290 mutex_lock(&chip
->lock
);
291 status
= sx150x_io_output(chip
, offset
, val
);
292 mutex_unlock(&chip
->lock
);
297 static int sx150x_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
299 struct sx150x_chip
*chip
;
301 chip
= container_of(gc
, struct sx150x_chip
, gpio_chip
);
303 if (offset
>= chip
->dev_cfg
->ngpios
)
306 if (chip
->irq_base
< 0)
309 return chip
->irq_base
+ offset
;
312 static void sx150x_irq_mask(struct irq_data
*d
)
314 struct sx150x_chip
*chip
= irq_data_get_irq_chip_data(d
);
317 n
= d
->irq
- chip
->irq_base
;
318 chip
->irq_masked
|= (1 << n
);
319 chip
->irq_update
= n
;
322 static void sx150x_irq_unmask(struct irq_data
*d
)
324 struct sx150x_chip
*chip
= irq_data_get_irq_chip_data(d
);
327 n
= d
->irq
- chip
->irq_base
;
328 chip
->irq_masked
&= ~(1 << n
);
329 chip
->irq_update
= n
;
332 static int sx150x_irq_set_type(struct irq_data
*d
, unsigned int flow_type
)
334 struct sx150x_chip
*chip
= irq_data_get_irq_chip_data(d
);
337 if (flow_type
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
))
340 n
= d
->irq
- chip
->irq_base
;
342 if (flow_type
& IRQ_TYPE_EDGE_RISING
)
344 if (flow_type
& IRQ_TYPE_EDGE_FALLING
)
347 chip
->irq_sense
&= ~(3UL << (n
* 2));
348 chip
->irq_sense
|= val
<< (n
* 2);
349 chip
->irq_update
= n
;
353 static irqreturn_t
sx150x_irq_thread_fn(int irq
, void *dev_id
)
355 struct sx150x_chip
*chip
= (struct sx150x_chip
*)dev_id
;
356 unsigned nhandled
= 0;
363 for (i
= (chip
->dev_cfg
->ngpios
/ 8) - 1; i
>= 0; --i
) {
364 err
= sx150x_i2c_read(chip
->client
,
365 chip
->dev_cfg
->reg_irq_src
- i
,
370 sx150x_i2c_write(chip
->client
,
371 chip
->dev_cfg
->reg_irq_src
- i
,
373 for (n
= 0; n
< 8; ++n
) {
374 if (val
& (1 << n
)) {
375 sub_irq
= chip
->irq_base
+ (i
* 8) + n
;
376 handle_nested_irq(sub_irq
);
382 return (nhandled
> 0 ? IRQ_HANDLED
: IRQ_NONE
);
385 static void sx150x_irq_bus_lock(struct irq_data
*d
)
387 struct sx150x_chip
*chip
= irq_data_get_irq_chip_data(d
);
389 mutex_lock(&chip
->lock
);
392 static void sx150x_irq_bus_sync_unlock(struct irq_data
*d
)
394 struct sx150x_chip
*chip
= irq_data_get_irq_chip_data(d
);
397 if (chip
->irq_update
== NO_UPDATE_PENDING
)
400 n
= chip
->irq_update
;
401 chip
->irq_update
= NO_UPDATE_PENDING
;
403 /* Avoid updates if nothing changed */
404 if (chip
->dev_sense
== chip
->irq_sense
&&
405 chip
->dev_sense
== chip
->irq_masked
)
408 chip
->dev_sense
= chip
->irq_sense
;
409 chip
->dev_masked
= chip
->irq_masked
;
411 if (chip
->irq_masked
& (1 << n
)) {
412 sx150x_write_cfg(chip
, n
, 1, chip
->dev_cfg
->reg_irq_mask
, 1);
413 sx150x_write_cfg(chip
, n
, 2, chip
->dev_cfg
->reg_sense
, 0);
415 sx150x_write_cfg(chip
, n
, 1, chip
->dev_cfg
->reg_irq_mask
, 0);
416 sx150x_write_cfg(chip
, n
, 2, chip
->dev_cfg
->reg_sense
,
417 chip
->irq_sense
>> (n
* 2));
420 mutex_unlock(&chip
->lock
);
423 static void sx150x_init_chip(struct sx150x_chip
*chip
,
424 struct i2c_client
*client
,
425 kernel_ulong_t driver_data
,
426 struct sx150x_platform_data
*pdata
)
428 mutex_init(&chip
->lock
);
430 chip
->client
= client
;
431 chip
->dev_cfg
= &sx150x_devices
[driver_data
];
432 chip
->gpio_chip
.label
= client
->name
;
433 chip
->gpio_chip
.direction_input
= sx150x_gpio_direction_input
;
434 chip
->gpio_chip
.direction_output
= sx150x_gpio_direction_output
;
435 chip
->gpio_chip
.get
= sx150x_gpio_get
;
436 chip
->gpio_chip
.set
= sx150x_gpio_set
;
437 chip
->gpio_chip
.to_irq
= sx150x_gpio_to_irq
;
438 chip
->gpio_chip
.base
= pdata
->gpio_base
;
439 chip
->gpio_chip
.can_sleep
= 1;
440 chip
->gpio_chip
.ngpio
= chip
->dev_cfg
->ngpios
;
441 if (pdata
->oscio_is_gpo
)
442 ++chip
->gpio_chip
.ngpio
;
444 chip
->irq_chip
.name
= client
->name
;
445 chip
->irq_chip
.irq_mask
= sx150x_irq_mask
;
446 chip
->irq_chip
.irq_unmask
= sx150x_irq_unmask
;
447 chip
->irq_chip
.irq_set_type
= sx150x_irq_set_type
;
448 chip
->irq_chip
.irq_bus_lock
= sx150x_irq_bus_lock
;
449 chip
->irq_chip
.irq_bus_sync_unlock
= sx150x_irq_bus_sync_unlock
;
450 chip
->irq_summary
= -1;
452 chip
->irq_masked
= ~0;
454 chip
->dev_masked
= ~0;
456 chip
->irq_update
= NO_UPDATE_PENDING
;
459 static int sx150x_init_io(struct sx150x_chip
*chip
, u8 base
, u16 cfg
)
464 for (n
= 0; err
>= 0 && n
< (chip
->dev_cfg
->ngpios
/ 8); ++n
)
465 err
= sx150x_i2c_write(chip
->client
, base
- n
, cfg
>> (n
* 8));
469 static int sx150x_reset(struct sx150x_chip
*chip
)
473 err
= i2c_smbus_write_byte_data(chip
->client
,
474 chip
->dev_cfg
->reg_reset
,
479 err
= i2c_smbus_write_byte_data(chip
->client
,
480 chip
->dev_cfg
->reg_reset
,
485 static int sx150x_init_hw(struct sx150x_chip
*chip
,
486 struct sx150x_platform_data
*pdata
)
490 if (pdata
->reset_during_probe
) {
491 err
= sx150x_reset(chip
);
496 err
= sx150x_i2c_write(chip
->client
,
497 chip
->dev_cfg
->reg_misc
,
502 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_pullup
,
503 pdata
->io_pullup_ena
);
507 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_pulldn
,
508 pdata
->io_pulldn_ena
);
512 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_drain
,
513 pdata
->io_open_drain_ena
);
517 err
= sx150x_init_io(chip
, chip
->dev_cfg
->reg_polarity
,
522 if (pdata
->oscio_is_gpo
)
523 sx150x_set_oscio(chip
, 0);
528 static int sx150x_install_irq_chip(struct sx150x_chip
*chip
,
536 chip
->irq_summary
= irq_summary
;
537 chip
->irq_base
= irq_base
;
539 for (n
= 0; n
< chip
->dev_cfg
->ngpios
; ++n
) {
541 irq_set_chip_data(irq
, chip
);
542 irq_set_chip_and_handler(irq
, &chip
->irq_chip
, handle_edge_irq
);
543 irq_set_nested_thread(irq
, 1);
545 set_irq_flags(irq
, IRQF_VALID
);
547 irq_set_noprobe(irq
);
551 err
= devm_request_threaded_irq(&chip
->client
->dev
,
554 sx150x_irq_thread_fn
,
555 IRQF_SHARED
| IRQF_TRIGGER_FALLING
,
559 chip
->irq_summary
= -1;
566 static void sx150x_remove_irq_chip(struct sx150x_chip
*chip
)
571 for (n
= 0; n
< chip
->dev_cfg
->ngpios
; ++n
) {
572 irq
= chip
->irq_base
+ n
;
573 irq_set_chip_and_handler(irq
, NULL
, NULL
);
577 static int sx150x_probe(struct i2c_client
*client
,
578 const struct i2c_device_id
*id
)
580 static const u32 i2c_funcs
= I2C_FUNC_SMBUS_BYTE_DATA
|
581 I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
582 struct sx150x_platform_data
*pdata
;
583 struct sx150x_chip
*chip
;
586 pdata
= dev_get_platdata(&client
->dev
);
590 if (!i2c_check_functionality(client
->adapter
, i2c_funcs
))
593 chip
= devm_kzalloc(&client
->dev
,
594 sizeof(struct sx150x_chip
), GFP_KERNEL
);
598 sx150x_init_chip(chip
, client
, id
->driver_data
, pdata
);
599 rc
= sx150x_init_hw(chip
, pdata
);
603 rc
= gpiochip_add(&chip
->gpio_chip
);
607 if (pdata
->irq_summary
>= 0) {
608 rc
= sx150x_install_irq_chip(chip
,
612 goto probe_fail_post_gpiochip_add
;
615 i2c_set_clientdata(client
, chip
);
618 probe_fail_post_gpiochip_add
:
619 WARN_ON(gpiochip_remove(&chip
->gpio_chip
) < 0);
623 static int sx150x_remove(struct i2c_client
*client
)
625 struct sx150x_chip
*chip
;
628 chip
= i2c_get_clientdata(client
);
629 rc
= gpiochip_remove(&chip
->gpio_chip
);
633 if (chip
->irq_summary
>= 0)
634 sx150x_remove_irq_chip(chip
);
639 static struct i2c_driver sx150x_driver
= {
644 .probe
= sx150x_probe
,
645 .remove
= sx150x_remove
,
646 .id_table
= sx150x_id
,
649 static int __init
sx150x_init(void)
651 return i2c_add_driver(&sx150x_driver
);
653 subsys_initcall(sx150x_init
);
655 static void __exit
sx150x_exit(void)
657 return i2c_del_driver(&sx150x_driver
);
659 module_exit(sx150x_exit
);
661 MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
662 MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders");
663 MODULE_LICENSE("GPL v2");
664 MODULE_ALIAS("i2c:sx150x");