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/i2c/sx150x.h>
27 #include <linux/of_address.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_gpio.h>
30 #include <linux/of_device.h>
32 #define NO_UPDATE_PENDING -1
34 /* The chip models of sx150x */
38 struct sx150x_456_pri {
48 struct sx150x_789_pri {
57 struct sx150x_device_data {
68 struct sx150x_456_pri x456;
69 struct sx150x_789_pri x789;
74 struct gpio_chip gpio_chip;
75 struct i2c_client *client;
76 const struct sx150x_device_data *dev_cfg;
84 struct irq_chip irq_chip;
88 static const struct sx150x_device_data sx150x_devices[] = {
100 .reg_polarity = 0x06,
107 [1] = { /* sx1509q */
113 .reg_irq_mask = 0x13,
118 .reg_polarity = 0x0d,
125 [2] = { /* sx1506q */
131 .reg_irq_mask = 0x09,
135 .reg_pld_mode = 0x21,
136 .reg_pld_table0 = 0x23,
137 .reg_pld_table1 = 0x25,
138 .reg_pld_table2 = 0x27,
139 .reg_pld_table3 = 0x29,
140 .reg_pld_table4 = 0x2b,
147 static const struct i2c_device_id sx150x_id[] = {
153 MODULE_DEVICE_TABLE(i2c, sx150x_id);
155 static const struct of_device_id sx150x_of_match[] = {
156 { .compatible = "semtech,sx1508q" },
157 { .compatible = "semtech,sx1509q" },
158 { .compatible = "semtech,sx1506q" },
161 MODULE_DEVICE_TABLE(of, sx150x_of_match);
163 static s32 sx150x_i2c_write(struct i2c_client *client, u8 reg, u8 val)
165 s32 err = i2c_smbus_write_byte_data(client, reg, val);
168 dev_warn(&client->dev,
169 "i2c write fail: can't write %02x to %02x: %d\n",
174 static s32 sx150x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
176 s32 err = i2c_smbus_read_byte_data(client, reg);
181 dev_warn(&client->dev,
182 "i2c read fail: can't read from %02x: %d\n",
187 static inline bool offset_is_oscio(struct sx150x_chip *chip, unsigned offset)
189 return (chip->dev_cfg->ngpios == offset);
193 * These utility functions solve the common problem of locating and setting
194 * configuration bits. Configuration bits are grouped into registers
195 * whose indexes increase downwards. For example, with eight-bit registers,
196 * sixteen gpios would have their config bits grouped in the following order:
197 * REGISTER N-1 [ f e d c b a 9 8 ]
198 * N [ 7 6 5 4 3 2 1 0 ]
200 * For multi-bit configurations, the pattern gets wider:
201 * REGISTER N-3 [ f f e e d d c c ]
202 * N-2 [ b b a a 9 9 8 8 ]
203 * N-1 [ 7 7 6 6 5 5 4 4 ]
204 * N [ 3 3 2 2 1 1 0 0 ]
206 * Given the address of the starting register 'N', the index of the gpio
207 * whose configuration we seek to change, and the width in bits of that
208 * configuration, these functions allow us to locate the correct
209 * register and mask the correct bits.
211 static inline void sx150x_find_cfg(u8 offset, u8 width,
212 u8 *reg, u8 *mask, u8 *shift)
214 *reg -= offset * width / 8;
215 *mask = (1 << width) - 1;
216 *shift = (offset * width) % 8;
220 static s32 sx150x_write_cfg(struct sx150x_chip *chip,
221 u8 offset, u8 width, u8 reg, u8 val)
228 sx150x_find_cfg(offset, width, ®, &mask, &shift);
229 err = sx150x_i2c_read(chip->client, reg, &data);
234 data |= (val << shift) & mask;
235 return sx150x_i2c_write(chip->client, reg, data);
238 static int sx150x_get_io(struct sx150x_chip *chip, unsigned offset)
240 u8 reg = chip->dev_cfg->reg_data;
246 sx150x_find_cfg(offset, 1, ®, &mask, &shift);
247 err = sx150x_i2c_read(chip->client, reg, &data);
249 err = (data & mask) != 0 ? 1 : 0;
254 static void sx150x_set_oscio(struct sx150x_chip *chip, int val)
256 sx150x_i2c_write(chip->client,
257 chip->dev_cfg->pri.x789.reg_clock,
258 (val ? 0x1f : 0x10));
261 static void sx150x_set_io(struct sx150x_chip *chip, unsigned offset, int val)
263 sx150x_write_cfg(chip,
266 chip->dev_cfg->reg_data,
270 static int sx150x_io_input(struct sx150x_chip *chip, unsigned offset)
272 return sx150x_write_cfg(chip,
275 chip->dev_cfg->reg_dir,
279 static int sx150x_io_output(struct sx150x_chip *chip, unsigned offset, int val)
283 err = sx150x_write_cfg(chip,
286 chip->dev_cfg->reg_data,
289 err = sx150x_write_cfg(chip,
292 chip->dev_cfg->reg_dir,
297 static int sx150x_gpio_get(struct gpio_chip *gc, unsigned offset)
299 struct sx150x_chip *chip;
300 int status = -EINVAL;
302 chip = container_of(gc, struct sx150x_chip, gpio_chip);
304 if (!offset_is_oscio(chip, offset)) {
305 mutex_lock(&chip->lock);
306 status = sx150x_get_io(chip, offset);
307 mutex_unlock(&chip->lock);
313 static void sx150x_gpio_set(struct gpio_chip *gc, unsigned offset, int val)
315 struct sx150x_chip *chip;
317 chip = container_of(gc, struct sx150x_chip, gpio_chip);
319 mutex_lock(&chip->lock);
320 if (offset_is_oscio(chip, offset))
321 sx150x_set_oscio(chip, val);
323 sx150x_set_io(chip, offset, val);
324 mutex_unlock(&chip->lock);
327 static int sx150x_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
329 struct sx150x_chip *chip;
330 int status = -EINVAL;
332 chip = container_of(gc, struct sx150x_chip, gpio_chip);
334 if (!offset_is_oscio(chip, offset)) {
335 mutex_lock(&chip->lock);
336 status = sx150x_io_input(chip, offset);
337 mutex_unlock(&chip->lock);
342 static int sx150x_gpio_direction_output(struct gpio_chip *gc,
346 struct sx150x_chip *chip;
349 chip = container_of(gc, struct sx150x_chip, gpio_chip);
351 if (!offset_is_oscio(chip, offset)) {
352 mutex_lock(&chip->lock);
353 status = sx150x_io_output(chip, offset, val);
354 mutex_unlock(&chip->lock);
359 static void sx150x_irq_mask(struct irq_data *d)
361 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
362 unsigned n = d->hwirq;
364 chip->irq_masked |= (1 << n);
365 chip->irq_update = n;
368 static void sx150x_irq_unmask(struct irq_data *d)
370 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
371 unsigned n = d->hwirq;
373 chip->irq_masked &= ~(1 << n);
374 chip->irq_update = n;
377 static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
379 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
382 if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
387 if (flow_type & IRQ_TYPE_EDGE_RISING)
389 if (flow_type & IRQ_TYPE_EDGE_FALLING)
392 chip->irq_sense &= ~(3UL << (n * 2));
393 chip->irq_sense |= val << (n * 2);
394 chip->irq_update = n;
398 static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
400 struct sx150x_chip *chip = (struct sx150x_chip *)dev_id;
401 unsigned nhandled = 0;
408 for (i = (chip->dev_cfg->ngpios / 8) - 1; i >= 0; --i) {
409 err = sx150x_i2c_read(chip->client,
410 chip->dev_cfg->reg_irq_src - i,
415 sx150x_i2c_write(chip->client,
416 chip->dev_cfg->reg_irq_src - i,
418 for (n = 0; n < 8; ++n) {
419 if (val & (1 << n)) {
420 sub_irq = irq_find_mapping(
421 chip->gpio_chip.irqdomain,
423 handle_nested_irq(sub_irq);
429 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
432 static void sx150x_irq_bus_lock(struct irq_data *d)
434 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
436 mutex_lock(&chip->lock);
439 static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
441 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
444 if (chip->irq_update == NO_UPDATE_PENDING)
447 n = chip->irq_update;
448 chip->irq_update = NO_UPDATE_PENDING;
450 /* Avoid updates if nothing changed */
451 if (chip->dev_sense == chip->irq_sense &&
452 chip->dev_masked == chip->irq_masked)
455 chip->dev_sense = chip->irq_sense;
456 chip->dev_masked = chip->irq_masked;
458 if (chip->irq_masked & (1 << n)) {
459 sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 1);
460 sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, 0);
462 sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 0);
463 sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense,
464 chip->irq_sense >> (n * 2));
467 mutex_unlock(&chip->lock);
470 static void sx150x_init_chip(struct sx150x_chip *chip,
471 struct i2c_client *client,
472 kernel_ulong_t driver_data,
473 struct sx150x_platform_data *pdata)
475 mutex_init(&chip->lock);
477 chip->client = client;
478 chip->dev_cfg = &sx150x_devices[driver_data];
479 chip->gpio_chip.dev = &client->dev;
480 chip->gpio_chip.label = client->name;
481 chip->gpio_chip.direction_input = sx150x_gpio_direction_input;
482 chip->gpio_chip.direction_output = sx150x_gpio_direction_output;
483 chip->gpio_chip.get = sx150x_gpio_get;
484 chip->gpio_chip.set = sx150x_gpio_set;
485 chip->gpio_chip.base = pdata->gpio_base;
486 chip->gpio_chip.can_sleep = true;
487 chip->gpio_chip.ngpio = chip->dev_cfg->ngpios;
488 #ifdef CONFIG_OF_GPIO
489 chip->gpio_chip.of_node = client->dev.of_node;
490 chip->gpio_chip.of_gpio_n_cells = 2;
492 if (pdata->oscio_is_gpo)
493 ++chip->gpio_chip.ngpio;
495 chip->irq_chip.name = client->name;
496 chip->irq_chip.irq_mask = sx150x_irq_mask;
497 chip->irq_chip.irq_unmask = sx150x_irq_unmask;
498 chip->irq_chip.irq_set_type = sx150x_irq_set_type;
499 chip->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
500 chip->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
501 chip->irq_summary = -1;
503 chip->irq_masked = ~0;
505 chip->dev_masked = ~0;
507 chip->irq_update = NO_UPDATE_PENDING;
510 static int sx150x_init_io(struct sx150x_chip *chip, u8 base, u16 cfg)
515 for (n = 0; err >= 0 && n < (chip->dev_cfg->ngpios / 8); ++n)
516 err = sx150x_i2c_write(chip->client, base - n, cfg >> (n * 8));
520 static int sx150x_reset(struct sx150x_chip *chip)
524 err = i2c_smbus_write_byte_data(chip->client,
525 chip->dev_cfg->pri.x789.reg_reset,
530 err = i2c_smbus_write_byte_data(chip->client,
531 chip->dev_cfg->pri.x789.reg_reset,
536 static int sx150x_init_hw(struct sx150x_chip *chip,
537 struct sx150x_platform_data *pdata)
541 if (pdata->reset_during_probe) {
542 err = sx150x_reset(chip);
547 if (chip->dev_cfg->model == SX150X_789)
548 err = sx150x_i2c_write(chip->client,
549 chip->dev_cfg->pri.x789.reg_misc,
552 err = sx150x_i2c_write(chip->client,
553 chip->dev_cfg->pri.x456.reg_advance,
558 err = sx150x_init_io(chip, chip->dev_cfg->reg_pullup,
559 pdata->io_pullup_ena);
563 err = sx150x_init_io(chip, chip->dev_cfg->reg_pulldn,
564 pdata->io_pulldn_ena);
568 if (chip->dev_cfg->model == SX150X_789) {
569 err = sx150x_init_io(chip,
570 chip->dev_cfg->pri.x789.reg_drain,
571 pdata->io_open_drain_ena);
575 err = sx150x_init_io(chip,
576 chip->dev_cfg->pri.x789.reg_polarity,
581 /* Set all pins to work in normal mode */
582 err = sx150x_init_io(chip,
583 chip->dev_cfg->pri.x456.reg_pld_mode,
590 if (pdata->oscio_is_gpo)
591 sx150x_set_oscio(chip, 0);
596 static int sx150x_install_irq_chip(struct sx150x_chip *chip,
602 chip->irq_summary = irq_summary;
603 chip->irq_base = irq_base;
605 /* Add gpio chip to irq subsystem */
606 err = gpiochip_irqchip_add(&chip->gpio_chip,
607 &chip->irq_chip, chip->irq_base,
608 handle_edge_irq, IRQ_TYPE_EDGE_BOTH);
610 dev_err(&chip->client->dev,
611 "could not connect irqchip to gpiochip\n");
615 err = devm_request_threaded_irq(&chip->client->dev,
616 irq_summary, NULL, sx150x_irq_thread_fn,
617 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_FALLING,
618 chip->irq_chip.name, chip);
620 chip->irq_summary = -1;
627 static int sx150x_probe(struct i2c_client *client,
628 const struct i2c_device_id *id)
630 static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
631 I2C_FUNC_SMBUS_WRITE_WORD_DATA;
632 struct sx150x_platform_data *pdata;
633 struct sx150x_chip *chip;
636 pdata = dev_get_platdata(&client->dev);
640 if (!i2c_check_functionality(client->adapter, i2c_funcs))
643 chip = devm_kzalloc(&client->dev,
644 sizeof(struct sx150x_chip), GFP_KERNEL);
648 sx150x_init_chip(chip, client, id->driver_data, pdata);
649 rc = sx150x_init_hw(chip, pdata);
653 rc = gpiochip_add(&chip->gpio_chip);
657 if (pdata->irq_summary >= 0) {
658 rc = sx150x_install_irq_chip(chip,
662 goto probe_fail_post_gpiochip_add;
665 i2c_set_clientdata(client, chip);
668 probe_fail_post_gpiochip_add:
669 gpiochip_remove(&chip->gpio_chip);
673 static int sx150x_remove(struct i2c_client *client)
675 struct sx150x_chip *chip;
677 chip = i2c_get_clientdata(client);
678 gpiochip_remove(&chip->gpio_chip);
683 static struct i2c_driver sx150x_driver = {
686 .owner = THIS_MODULE,
687 .of_match_table = of_match_ptr(sx150x_of_match),
689 .probe = sx150x_probe,
690 .remove = sx150x_remove,
691 .id_table = sx150x_id,
694 static int __init sx150x_init(void)
696 return i2c_add_driver(&sx150x_driver);
698 subsys_initcall(sx150x_init);
700 static void __exit sx150x_exit(void)
702 return i2c_del_driver(&sx150x_driver);
704 module_exit(sx150x_exit);
706 MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
707 MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders");
708 MODULE_LICENSE("GPL v2");
709 MODULE_ALIAS("i2c:sx150x");