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 struct sx150x_chip *to_sx150x(struct gpio_chip *gc)
165 return container_of(gc, struct sx150x_chip, gpio_chip);
168 static s32 sx150x_i2c_write(struct i2c_client *client, u8 reg, u8 val)
170 s32 err = i2c_smbus_write_byte_data(client, reg, val);
173 dev_warn(&client->dev,
174 "i2c write fail: can't write %02x to %02x: %d\n",
179 static s32 sx150x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
181 s32 err = i2c_smbus_read_byte_data(client, reg);
186 dev_warn(&client->dev,
187 "i2c read fail: can't read from %02x: %d\n",
192 static inline bool offset_is_oscio(struct sx150x_chip *chip, unsigned offset)
194 return (chip->dev_cfg->ngpios == offset);
198 * These utility functions solve the common problem of locating and setting
199 * configuration bits. Configuration bits are grouped into registers
200 * whose indexes increase downwards. For example, with eight-bit registers,
201 * sixteen gpios would have their config bits grouped in the following order:
202 * REGISTER N-1 [ f e d c b a 9 8 ]
203 * N [ 7 6 5 4 3 2 1 0 ]
205 * For multi-bit configurations, the pattern gets wider:
206 * REGISTER N-3 [ f f e e d d c c ]
207 * N-2 [ b b a a 9 9 8 8 ]
208 * N-1 [ 7 7 6 6 5 5 4 4 ]
209 * N [ 3 3 2 2 1 1 0 0 ]
211 * Given the address of the starting register 'N', the index of the gpio
212 * whose configuration we seek to change, and the width in bits of that
213 * configuration, these functions allow us to locate the correct
214 * register and mask the correct bits.
216 static inline void sx150x_find_cfg(u8 offset, u8 width,
217 u8 *reg, u8 *mask, u8 *shift)
219 *reg -= offset * width / 8;
220 *mask = (1 << width) - 1;
221 *shift = (offset * width) % 8;
225 static s32 sx150x_write_cfg(struct sx150x_chip *chip,
226 u8 offset, u8 width, u8 reg, u8 val)
233 sx150x_find_cfg(offset, width, ®, &mask, &shift);
234 err = sx150x_i2c_read(chip->client, reg, &data);
239 data |= (val << shift) & mask;
240 return sx150x_i2c_write(chip->client, reg, data);
243 static int sx150x_get_io(struct sx150x_chip *chip, unsigned offset)
245 u8 reg = chip->dev_cfg->reg_data;
251 sx150x_find_cfg(offset, 1, ®, &mask, &shift);
252 err = sx150x_i2c_read(chip->client, reg, &data);
254 err = (data & mask) != 0 ? 1 : 0;
259 static void sx150x_set_oscio(struct sx150x_chip *chip, int val)
261 sx150x_i2c_write(chip->client,
262 chip->dev_cfg->pri.x789.reg_clock,
263 (val ? 0x1f : 0x10));
266 static void sx150x_set_io(struct sx150x_chip *chip, unsigned offset, int val)
268 sx150x_write_cfg(chip,
271 chip->dev_cfg->reg_data,
275 static int sx150x_io_input(struct sx150x_chip *chip, unsigned offset)
277 return sx150x_write_cfg(chip,
280 chip->dev_cfg->reg_dir,
284 static int sx150x_io_output(struct sx150x_chip *chip, unsigned offset, int val)
288 err = sx150x_write_cfg(chip,
291 chip->dev_cfg->reg_data,
294 err = sx150x_write_cfg(chip,
297 chip->dev_cfg->reg_dir,
302 static int sx150x_gpio_get(struct gpio_chip *gc, unsigned offset)
304 struct sx150x_chip *chip = to_sx150x(gc);
305 int status = -EINVAL;
307 if (!offset_is_oscio(chip, offset)) {
308 mutex_lock(&chip->lock);
309 status = sx150x_get_io(chip, offset);
310 mutex_unlock(&chip->lock);
316 static void sx150x_gpio_set(struct gpio_chip *gc, unsigned offset, int val)
318 struct sx150x_chip *chip = to_sx150x(gc);
320 mutex_lock(&chip->lock);
321 if (offset_is_oscio(chip, offset))
322 sx150x_set_oscio(chip, val);
324 sx150x_set_io(chip, offset, val);
325 mutex_unlock(&chip->lock);
328 static int sx150x_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
330 struct sx150x_chip *chip = to_sx150x(gc);
331 int status = -EINVAL;
333 if (!offset_is_oscio(chip, offset)) {
334 mutex_lock(&chip->lock);
335 status = sx150x_io_input(chip, offset);
336 mutex_unlock(&chip->lock);
341 static int sx150x_gpio_direction_output(struct gpio_chip *gc,
345 struct sx150x_chip *chip = to_sx150x(gc);
348 if (!offset_is_oscio(chip, offset)) {
349 mutex_lock(&chip->lock);
350 status = sx150x_io_output(chip, offset, val);
351 mutex_unlock(&chip->lock);
356 static void sx150x_irq_mask(struct irq_data *d)
358 struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d));
359 unsigned n = d->hwirq;
361 chip->irq_masked |= (1 << n);
362 chip->irq_update = n;
365 static void sx150x_irq_unmask(struct irq_data *d)
367 struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d));
368 unsigned n = d->hwirq;
370 chip->irq_masked &= ~(1 << n);
371 chip->irq_update = n;
374 static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
376 struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d));
379 if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
384 if (flow_type & IRQ_TYPE_EDGE_RISING)
386 if (flow_type & IRQ_TYPE_EDGE_FALLING)
389 chip->irq_sense &= ~(3UL << (n * 2));
390 chip->irq_sense |= val << (n * 2);
391 chip->irq_update = n;
395 static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
397 struct sx150x_chip *chip = (struct sx150x_chip *)dev_id;
398 unsigned nhandled = 0;
405 for (i = (chip->dev_cfg->ngpios / 8) - 1; i >= 0; --i) {
406 err = sx150x_i2c_read(chip->client,
407 chip->dev_cfg->reg_irq_src - i,
412 sx150x_i2c_write(chip->client,
413 chip->dev_cfg->reg_irq_src - i,
415 for (n = 0; n < 8; ++n) {
416 if (val & (1 << n)) {
417 sub_irq = irq_find_mapping(
418 chip->gpio_chip.irqdomain,
420 handle_nested_irq(sub_irq);
426 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
429 static void sx150x_irq_bus_lock(struct irq_data *d)
431 struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d));
433 mutex_lock(&chip->lock);
436 static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
438 struct sx150x_chip *chip = to_sx150x(irq_data_get_irq_chip_data(d));
441 if (chip->irq_update == NO_UPDATE_PENDING)
444 n = chip->irq_update;
445 chip->irq_update = NO_UPDATE_PENDING;
447 /* Avoid updates if nothing changed */
448 if (chip->dev_sense == chip->irq_sense &&
449 chip->dev_masked == chip->irq_masked)
452 chip->dev_sense = chip->irq_sense;
453 chip->dev_masked = chip->irq_masked;
455 if (chip->irq_masked & (1 << n)) {
456 sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 1);
457 sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, 0);
459 sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 0);
460 sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense,
461 chip->irq_sense >> (n * 2));
464 mutex_unlock(&chip->lock);
467 static void sx150x_init_chip(struct sx150x_chip *chip,
468 struct i2c_client *client,
469 kernel_ulong_t driver_data,
470 struct sx150x_platform_data *pdata)
472 mutex_init(&chip->lock);
474 chip->client = client;
475 chip->dev_cfg = &sx150x_devices[driver_data];
476 chip->gpio_chip.dev = &client->dev;
477 chip->gpio_chip.label = client->name;
478 chip->gpio_chip.direction_input = sx150x_gpio_direction_input;
479 chip->gpio_chip.direction_output = sx150x_gpio_direction_output;
480 chip->gpio_chip.get = sx150x_gpio_get;
481 chip->gpio_chip.set = sx150x_gpio_set;
482 chip->gpio_chip.base = pdata->gpio_base;
483 chip->gpio_chip.can_sleep = true;
484 chip->gpio_chip.ngpio = chip->dev_cfg->ngpios;
485 #ifdef CONFIG_OF_GPIO
486 chip->gpio_chip.of_node = client->dev.of_node;
487 chip->gpio_chip.of_gpio_n_cells = 2;
489 if (pdata->oscio_is_gpo)
490 ++chip->gpio_chip.ngpio;
492 chip->irq_chip.name = client->name;
493 chip->irq_chip.irq_mask = sx150x_irq_mask;
494 chip->irq_chip.irq_unmask = sx150x_irq_unmask;
495 chip->irq_chip.irq_set_type = sx150x_irq_set_type;
496 chip->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
497 chip->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
498 chip->irq_summary = -1;
500 chip->irq_masked = ~0;
502 chip->dev_masked = ~0;
504 chip->irq_update = NO_UPDATE_PENDING;
507 static int sx150x_init_io(struct sx150x_chip *chip, u8 base, u16 cfg)
512 for (n = 0; err >= 0 && n < (chip->dev_cfg->ngpios / 8); ++n)
513 err = sx150x_i2c_write(chip->client, base - n, cfg >> (n * 8));
517 static int sx150x_reset(struct sx150x_chip *chip)
521 err = i2c_smbus_write_byte_data(chip->client,
522 chip->dev_cfg->pri.x789.reg_reset,
527 err = i2c_smbus_write_byte_data(chip->client,
528 chip->dev_cfg->pri.x789.reg_reset,
533 static int sx150x_init_hw(struct sx150x_chip *chip,
534 struct sx150x_platform_data *pdata)
538 if (pdata->reset_during_probe) {
539 err = sx150x_reset(chip);
544 if (chip->dev_cfg->model == SX150X_789)
545 err = sx150x_i2c_write(chip->client,
546 chip->dev_cfg->pri.x789.reg_misc,
549 err = sx150x_i2c_write(chip->client,
550 chip->dev_cfg->pri.x456.reg_advance,
555 err = sx150x_init_io(chip, chip->dev_cfg->reg_pullup,
556 pdata->io_pullup_ena);
560 err = sx150x_init_io(chip, chip->dev_cfg->reg_pulldn,
561 pdata->io_pulldn_ena);
565 if (chip->dev_cfg->model == SX150X_789) {
566 err = sx150x_init_io(chip,
567 chip->dev_cfg->pri.x789.reg_drain,
568 pdata->io_open_drain_ena);
572 err = sx150x_init_io(chip,
573 chip->dev_cfg->pri.x789.reg_polarity,
578 /* Set all pins to work in normal mode */
579 err = sx150x_init_io(chip,
580 chip->dev_cfg->pri.x456.reg_pld_mode,
587 if (pdata->oscio_is_gpo)
588 sx150x_set_oscio(chip, 0);
593 static int sx150x_install_irq_chip(struct sx150x_chip *chip,
599 chip->irq_summary = irq_summary;
600 chip->irq_base = irq_base;
602 /* Add gpio chip to irq subsystem */
603 err = gpiochip_irqchip_add(&chip->gpio_chip,
604 &chip->irq_chip, chip->irq_base,
605 handle_edge_irq, IRQ_TYPE_EDGE_BOTH);
607 dev_err(&chip->client->dev,
608 "could not connect irqchip to gpiochip\n");
612 err = devm_request_threaded_irq(&chip->client->dev,
613 irq_summary, NULL, sx150x_irq_thread_fn,
614 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_FALLING,
615 chip->irq_chip.name, chip);
617 chip->irq_summary = -1;
624 static int sx150x_probe(struct i2c_client *client,
625 const struct i2c_device_id *id)
627 static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
628 I2C_FUNC_SMBUS_WRITE_WORD_DATA;
629 struct sx150x_platform_data *pdata;
630 struct sx150x_chip *chip;
633 pdata = dev_get_platdata(&client->dev);
637 if (!i2c_check_functionality(client->adapter, i2c_funcs))
640 chip = devm_kzalloc(&client->dev,
641 sizeof(struct sx150x_chip), GFP_KERNEL);
645 sx150x_init_chip(chip, client, id->driver_data, pdata);
646 rc = sx150x_init_hw(chip, pdata);
650 rc = gpiochip_add(&chip->gpio_chip);
654 if (pdata->irq_summary >= 0) {
655 rc = sx150x_install_irq_chip(chip,
659 goto probe_fail_post_gpiochip_add;
662 i2c_set_clientdata(client, chip);
665 probe_fail_post_gpiochip_add:
666 gpiochip_remove(&chip->gpio_chip);
670 static int sx150x_remove(struct i2c_client *client)
672 struct sx150x_chip *chip;
674 chip = i2c_get_clientdata(client);
675 gpiochip_remove(&chip->gpio_chip);
680 static struct i2c_driver sx150x_driver = {
683 .owner = THIS_MODULE,
684 .of_match_table = of_match_ptr(sx150x_of_match),
686 .probe = sx150x_probe,
687 .remove = sx150x_remove,
688 .id_table = sx150x_id,
691 static int __init sx150x_init(void)
693 return i2c_add_driver(&sx150x_driver);
695 subsys_initcall(sx150x_init);
697 static void __exit sx150x_exit(void)
699 return i2c_del_driver(&sx150x_driver);
701 module_exit(sx150x_exit);
703 MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
704 MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders");
705 MODULE_LICENSE("GPL v2");