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 #define NO_UPDATE_PENDING -1
29 struct sx150x_device_data {
46 struct gpio_chip gpio_chip;
47 struct i2c_client *client;
48 const struct sx150x_device_data *dev_cfg;
56 struct irq_chip irq_chip;
60 static const struct sx150x_device_data sx150x_devices[] = {
93 static const struct i2c_device_id sx150x_id[] = {
98 MODULE_DEVICE_TABLE(i2c, sx150x_id);
100 static s32 sx150x_i2c_write(struct i2c_client *client, u8 reg, u8 val)
102 s32 err = i2c_smbus_write_byte_data(client, reg, val);
105 dev_warn(&client->dev,
106 "i2c write fail: can't write %02x to %02x: %d\n",
111 static s32 sx150x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
113 s32 err = i2c_smbus_read_byte_data(client, reg);
118 dev_warn(&client->dev,
119 "i2c read fail: can't read from %02x: %d\n",
124 static inline bool offset_is_oscio(struct sx150x_chip *chip, unsigned offset)
126 return (chip->dev_cfg->ngpios == offset);
130 * These utility functions solve the common problem of locating and setting
131 * configuration bits. Configuration bits are grouped into registers
132 * whose indexes increase downwards. For example, with eight-bit registers,
133 * sixteen gpios would have their config bits grouped in the following order:
134 * REGISTER N-1 [ f e d c b a 9 8 ]
135 * N [ 7 6 5 4 3 2 1 0 ]
137 * For multi-bit configurations, the pattern gets wider:
138 * REGISTER N-3 [ f f e e d d c c ]
139 * N-2 [ b b a a 9 9 8 8 ]
140 * N-1 [ 7 7 6 6 5 5 4 4 ]
141 * N [ 3 3 2 2 1 1 0 0 ]
143 * Given the address of the starting register 'N', the index of the gpio
144 * whose configuration we seek to change, and the width in bits of that
145 * configuration, these functions allow us to locate the correct
146 * register and mask the correct bits.
148 static inline void sx150x_find_cfg(u8 offset, u8 width,
149 u8 *reg, u8 *mask, u8 *shift)
151 *reg -= offset * width / 8;
152 *mask = (1 << width) - 1;
153 *shift = (offset * width) % 8;
157 static s32 sx150x_write_cfg(struct sx150x_chip *chip,
158 u8 offset, u8 width, u8 reg, u8 val)
165 sx150x_find_cfg(offset, width, ®, &mask, &shift);
166 err = sx150x_i2c_read(chip->client, reg, &data);
171 data |= (val << shift) & mask;
172 return sx150x_i2c_write(chip->client, reg, data);
175 static int sx150x_get_io(struct sx150x_chip *chip, unsigned offset)
177 u8 reg = chip->dev_cfg->reg_data;
183 sx150x_find_cfg(offset, 1, ®, &mask, &shift);
184 err = sx150x_i2c_read(chip->client, reg, &data);
186 err = (data & mask) != 0 ? 1 : 0;
191 static void sx150x_set_oscio(struct sx150x_chip *chip, int val)
193 sx150x_i2c_write(chip->client,
194 chip->dev_cfg->reg_clock,
195 (val ? 0x1f : 0x10));
198 static void sx150x_set_io(struct sx150x_chip *chip, unsigned offset, int val)
200 sx150x_write_cfg(chip,
203 chip->dev_cfg->reg_data,
207 static int sx150x_io_input(struct sx150x_chip *chip, unsigned offset)
209 return sx150x_write_cfg(chip,
212 chip->dev_cfg->reg_dir,
216 static int sx150x_io_output(struct sx150x_chip *chip, unsigned offset, int val)
220 err = sx150x_write_cfg(chip,
223 chip->dev_cfg->reg_data,
226 err = sx150x_write_cfg(chip,
229 chip->dev_cfg->reg_dir,
234 static int sx150x_gpio_get(struct gpio_chip *gc, unsigned offset)
236 struct sx150x_chip *chip;
237 int status = -EINVAL;
239 chip = container_of(gc, struct sx150x_chip, gpio_chip);
241 if (!offset_is_oscio(chip, offset)) {
242 mutex_lock(&chip->lock);
243 status = sx150x_get_io(chip, offset);
244 mutex_unlock(&chip->lock);
250 static void sx150x_gpio_set(struct gpio_chip *gc, unsigned offset, int val)
252 struct sx150x_chip *chip;
254 chip = container_of(gc, struct sx150x_chip, gpio_chip);
256 mutex_lock(&chip->lock);
257 if (offset_is_oscio(chip, offset))
258 sx150x_set_oscio(chip, val);
260 sx150x_set_io(chip, offset, val);
261 mutex_unlock(&chip->lock);
264 static int sx150x_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
266 struct sx150x_chip *chip;
267 int status = -EINVAL;
269 chip = container_of(gc, struct sx150x_chip, gpio_chip);
271 if (!offset_is_oscio(chip, offset)) {
272 mutex_lock(&chip->lock);
273 status = sx150x_io_input(chip, offset);
274 mutex_unlock(&chip->lock);
279 static int sx150x_gpio_direction_output(struct gpio_chip *gc,
283 struct sx150x_chip *chip;
286 chip = container_of(gc, struct sx150x_chip, gpio_chip);
288 if (!offset_is_oscio(chip, offset)) {
289 mutex_lock(&chip->lock);
290 status = sx150x_io_output(chip, offset, val);
291 mutex_unlock(&chip->lock);
296 static int sx150x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
298 struct sx150x_chip *chip;
300 chip = container_of(gc, struct sx150x_chip, gpio_chip);
302 if (offset >= chip->dev_cfg->ngpios)
305 if (chip->irq_base < 0)
308 return chip->irq_base + offset;
311 static void sx150x_irq_mask(struct irq_data *d)
313 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
316 n = d->irq - chip->irq_base;
317 chip->irq_masked |= (1 << n);
318 chip->irq_update = n;
321 static void sx150x_irq_unmask(struct irq_data *d)
323 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
326 n = d->irq - chip->irq_base;
327 chip->irq_masked &= ~(1 << n);
328 chip->irq_update = n;
331 static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
333 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
336 if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
339 n = d->irq - chip->irq_base;
341 if (flow_type & IRQ_TYPE_EDGE_RISING)
343 if (flow_type & IRQ_TYPE_EDGE_FALLING)
346 chip->irq_sense &= ~(3UL << (n * 2));
347 chip->irq_sense |= val << (n * 2);
348 chip->irq_update = n;
352 static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
354 struct sx150x_chip *chip = (struct sx150x_chip *)dev_id;
355 unsigned nhandled = 0;
362 for (i = (chip->dev_cfg->ngpios / 8) - 1; i >= 0; --i) {
363 err = sx150x_i2c_read(chip->client,
364 chip->dev_cfg->reg_irq_src - i,
369 sx150x_i2c_write(chip->client,
370 chip->dev_cfg->reg_irq_src - i,
372 for (n = 0; n < 8; ++n) {
373 if (val & (1 << n)) {
374 sub_irq = chip->irq_base + (i * 8) + n;
375 handle_nested_irq(sub_irq);
381 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
384 static void sx150x_irq_bus_lock(struct irq_data *d)
386 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
388 mutex_lock(&chip->lock);
391 static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
393 struct sx150x_chip *chip = irq_data_get_irq_chip_data(d);
396 if (chip->irq_update == NO_UPDATE_PENDING)
399 n = chip->irq_update;
400 chip->irq_update = NO_UPDATE_PENDING;
402 /* Avoid updates if nothing changed */
403 if (chip->dev_sense == chip->irq_sense &&
404 chip->dev_sense == chip->irq_masked)
407 chip->dev_sense = chip->irq_sense;
408 chip->dev_masked = chip->irq_masked;
410 if (chip->irq_masked & (1 << n)) {
411 sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 1);
412 sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense, 0);
414 sx150x_write_cfg(chip, n, 1, chip->dev_cfg->reg_irq_mask, 0);
415 sx150x_write_cfg(chip, n, 2, chip->dev_cfg->reg_sense,
416 chip->irq_sense >> (n * 2));
419 mutex_unlock(&chip->lock);
422 static void sx150x_init_chip(struct sx150x_chip *chip,
423 struct i2c_client *client,
424 kernel_ulong_t driver_data,
425 struct sx150x_platform_data *pdata)
427 mutex_init(&chip->lock);
429 chip->client = client;
430 chip->dev_cfg = &sx150x_devices[driver_data];
431 chip->gpio_chip.label = client->name;
432 chip->gpio_chip.direction_input = sx150x_gpio_direction_input;
433 chip->gpio_chip.direction_output = sx150x_gpio_direction_output;
434 chip->gpio_chip.get = sx150x_gpio_get;
435 chip->gpio_chip.set = sx150x_gpio_set;
436 chip->gpio_chip.to_irq = sx150x_gpio_to_irq;
437 chip->gpio_chip.base = pdata->gpio_base;
438 chip->gpio_chip.can_sleep = true;
439 chip->gpio_chip.ngpio = chip->dev_cfg->ngpios;
440 if (pdata->oscio_is_gpo)
441 ++chip->gpio_chip.ngpio;
443 chip->irq_chip.name = client->name;
444 chip->irq_chip.irq_mask = sx150x_irq_mask;
445 chip->irq_chip.irq_unmask = sx150x_irq_unmask;
446 chip->irq_chip.irq_set_type = sx150x_irq_set_type;
447 chip->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
448 chip->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
449 chip->irq_summary = -1;
451 chip->irq_masked = ~0;
453 chip->dev_masked = ~0;
455 chip->irq_update = NO_UPDATE_PENDING;
458 static int sx150x_init_io(struct sx150x_chip *chip, u8 base, u16 cfg)
463 for (n = 0; err >= 0 && n < (chip->dev_cfg->ngpios / 8); ++n)
464 err = sx150x_i2c_write(chip->client, base - n, cfg >> (n * 8));
468 static int sx150x_reset(struct sx150x_chip *chip)
472 err = i2c_smbus_write_byte_data(chip->client,
473 chip->dev_cfg->reg_reset,
478 err = i2c_smbus_write_byte_data(chip->client,
479 chip->dev_cfg->reg_reset,
484 static int sx150x_init_hw(struct sx150x_chip *chip,
485 struct sx150x_platform_data *pdata)
489 if (pdata->reset_during_probe) {
490 err = sx150x_reset(chip);
495 err = sx150x_i2c_write(chip->client,
496 chip->dev_cfg->reg_misc,
501 err = sx150x_init_io(chip, chip->dev_cfg->reg_pullup,
502 pdata->io_pullup_ena);
506 err = sx150x_init_io(chip, chip->dev_cfg->reg_pulldn,
507 pdata->io_pulldn_ena);
511 err = sx150x_init_io(chip, chip->dev_cfg->reg_drain,
512 pdata->io_open_drain_ena);
516 err = sx150x_init_io(chip, chip->dev_cfg->reg_polarity,
521 if (pdata->oscio_is_gpo)
522 sx150x_set_oscio(chip, 0);
527 static int sx150x_install_irq_chip(struct sx150x_chip *chip,
535 chip->irq_summary = irq_summary;
536 chip->irq_base = irq_base;
538 for (n = 0; n < chip->dev_cfg->ngpios; ++n) {
540 irq_set_chip_data(irq, chip);
541 irq_set_chip_and_handler(irq, &chip->irq_chip, handle_edge_irq);
542 irq_set_nested_thread(irq, 1);
544 set_irq_flags(irq, IRQF_VALID);
546 irq_set_noprobe(irq);
550 err = devm_request_threaded_irq(&chip->client->dev,
553 sx150x_irq_thread_fn,
554 IRQF_SHARED | IRQF_TRIGGER_FALLING,
558 chip->irq_summary = -1;
565 static void sx150x_remove_irq_chip(struct sx150x_chip *chip)
570 for (n = 0; n < chip->dev_cfg->ngpios; ++n) {
571 irq = chip->irq_base + n;
572 irq_set_chip_and_handler(irq, NULL, NULL);
576 static int sx150x_probe(struct i2c_client *client,
577 const struct i2c_device_id *id)
579 static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
580 I2C_FUNC_SMBUS_WRITE_WORD_DATA;
581 struct sx150x_platform_data *pdata;
582 struct sx150x_chip *chip;
585 pdata = dev_get_platdata(&client->dev);
589 if (!i2c_check_functionality(client->adapter, i2c_funcs))
592 chip = devm_kzalloc(&client->dev,
593 sizeof(struct sx150x_chip), GFP_KERNEL);
597 sx150x_init_chip(chip, client, id->driver_data, pdata);
598 rc = sx150x_init_hw(chip, pdata);
602 rc = gpiochip_add(&chip->gpio_chip);
606 if (pdata->irq_summary >= 0) {
607 rc = sx150x_install_irq_chip(chip,
611 goto probe_fail_post_gpiochip_add;
614 i2c_set_clientdata(client, chip);
617 probe_fail_post_gpiochip_add:
618 gpiochip_remove(&chip->gpio_chip);
622 static int sx150x_remove(struct i2c_client *client)
624 struct sx150x_chip *chip;
626 chip = i2c_get_clientdata(client);
627 gpiochip_remove(&chip->gpio_chip);
629 if (chip->irq_summary >= 0)
630 sx150x_remove_irq_chip(chip);
635 static struct i2c_driver sx150x_driver = {
640 .probe = sx150x_probe,
641 .remove = sx150x_remove,
642 .id_table = sx150x_id,
645 static int __init sx150x_init(void)
647 return i2c_add_driver(&sx150x_driver);
649 subsys_initcall(sx150x_init);
651 static void __exit sx150x_exit(void)
653 return i2c_del_driver(&sx150x_driver);
655 module_exit(sx150x_exit);
657 MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
658 MODULE_DESCRIPTION("Driver for Semtech SX150X I2C GPIO Expanders");
659 MODULE_LICENSE("GPL v2");
660 MODULE_ALIAS("i2c:sx150x");