ALSA: hda - Add support for 92HD65 / 92HD66 family of codecs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / 74x164.c
blob84e070219839266e054f22fab48ce991ed83ccaa
1 /*
2 * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/mutex.h>
14 #include <linux/spi/spi.h>
15 #include <linux/spi/74x164.h>
16 #include <linux/gpio.h>
17 #include <linux/slab.h>
19 #define GEN_74X164_GPIO_COUNT 8
22 struct gen_74x164_chip {
23 struct spi_device *spi;
24 struct gpio_chip gpio_chip;
25 struct mutex lock;
26 u8 port_config;
29 static void gen_74x164_set_value(struct gpio_chip *, unsigned, int);
31 static struct gen_74x164_chip *gpio_to_chip(struct gpio_chip *gc)
33 return container_of(gc, struct gen_74x164_chip, gpio_chip);
36 static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
38 return spi_write(chip->spi,
39 &chip->port_config, sizeof(chip->port_config));
42 static int gen_74x164_direction_output(struct gpio_chip *gc,
43 unsigned offset, int val)
45 gen_74x164_set_value(gc, offset, val);
46 return 0;
49 static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
51 struct gen_74x164_chip *chip = gpio_to_chip(gc);
52 int ret;
54 mutex_lock(&chip->lock);
55 ret = (chip->port_config >> offset) & 0x1;
56 mutex_unlock(&chip->lock);
58 return ret;
61 static void gen_74x164_set_value(struct gpio_chip *gc,
62 unsigned offset, int val)
64 struct gen_74x164_chip *chip = gpio_to_chip(gc);
66 mutex_lock(&chip->lock);
67 if (val)
68 chip->port_config |= (1 << offset);
69 else
70 chip->port_config &= ~(1 << offset);
72 __gen_74x164_write_config(chip);
73 mutex_unlock(&chip->lock);
76 static int __devinit gen_74x164_probe(struct spi_device *spi)
78 struct gen_74x164_chip *chip;
79 struct gen_74x164_chip_platform_data *pdata;
80 int ret;
82 pdata = spi->dev.platform_data;
83 if (!pdata || !pdata->base) {
84 dev_dbg(&spi->dev, "incorrect or missing platform data\n");
85 return -EINVAL;
89 * bits_per_word cannot be configured in platform data
91 spi->bits_per_word = 8;
93 ret = spi_setup(spi);
94 if (ret < 0)
95 return ret;
97 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
98 if (!chip)
99 return -ENOMEM;
101 mutex_init(&chip->lock);
103 dev_set_drvdata(&spi->dev, chip);
105 chip->spi = spi;
107 chip->gpio_chip.label = GEN_74X164_DRIVER_NAME,
108 chip->gpio_chip.direction_output = gen_74x164_direction_output;
109 chip->gpio_chip.get = gen_74x164_get_value;
110 chip->gpio_chip.set = gen_74x164_set_value;
111 chip->gpio_chip.base = pdata->base;
112 chip->gpio_chip.ngpio = GEN_74X164_GPIO_COUNT;
113 chip->gpio_chip.can_sleep = 1;
114 chip->gpio_chip.dev = &spi->dev;
115 chip->gpio_chip.owner = THIS_MODULE;
117 ret = __gen_74x164_write_config(chip);
118 if (ret) {
119 dev_err(&spi->dev, "Failed writing: %d\n", ret);
120 goto exit_destroy;
123 ret = gpiochip_add(&chip->gpio_chip);
124 if (ret)
125 goto exit_destroy;
127 return ret;
129 exit_destroy:
130 dev_set_drvdata(&spi->dev, NULL);
131 mutex_destroy(&chip->lock);
132 kfree(chip);
133 return ret;
136 static int __devexit gen_74x164_remove(struct spi_device *spi)
138 struct gen_74x164_chip *chip;
139 int ret;
141 chip = dev_get_drvdata(&spi->dev);
142 if (chip == NULL)
143 return -ENODEV;
145 dev_set_drvdata(&spi->dev, NULL);
147 ret = gpiochip_remove(&chip->gpio_chip);
148 if (!ret) {
149 mutex_destroy(&chip->lock);
150 kfree(chip);
151 } else
152 dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
153 ret);
155 return ret;
158 static struct spi_driver gen_74x164_driver = {
159 .driver = {
160 .name = GEN_74X164_DRIVER_NAME,
161 .owner = THIS_MODULE,
163 .probe = gen_74x164_probe,
164 .remove = __devexit_p(gen_74x164_remove),
167 static int __init gen_74x164_init(void)
169 return spi_register_driver(&gen_74x164_driver);
171 subsys_initcall(gen_74x164_init);
173 static void __exit gen_74x164_exit(void)
175 spi_unregister_driver(&gen_74x164_driver);
177 module_exit(gen_74x164_exit);
179 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
180 MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
181 MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
182 MODULE_LICENSE("GPL v2");