drivers/leds/leds-pca9532.c: add gpio capability
authorJoachim Eastwood <manabian@gmail.com>
Wed, 25 May 2011 00:13:23 +0000 (17:13 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 25 May 2011 15:39:50 +0000 (08:39 -0700)
Allow unused leds on pca9532 to be used as gpio.  The board I am working
on now has no less than 6 pca9532 chips.  One chips is used for only leds,
one has 14 leds and 2 gpio and the rest of the chips are gpio only.

There is also one board in mainline which could use this capabilty;
arch/arm/mach-iop32x/n2100.c
 232         {       .type = PCA9532_TYPE_NONE }, /* power OFF gpio */
 233         {       .type = PCA9532_TYPE_NONE }, /* reset gpio */

This patch defines a new pin type, PCA9532_TYPE_GPIO, and registers a
gpiochip if any pin has this type set.  The gpio will registers all chip
pins but will filter on gpio_request.

[randy.dunlap@oracle.com: fix build when GPIOLIB is not enabled]
Signed-off-by: Joachim Eastwood <joachim.eastwood@jotron.com>
Reviewed-by: Wolfram Sang <w.sang@pengutronix.de>
Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: Richard Purdie <rpurdie@rpsys.net>
Cc: Grant Likely <grant.likely@secretlab.ca>
Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com>
Cc: Jan Weitzel <j.weitzel@phytec.de>
Cc: Juergen Kilb <j.kilb@phytec.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/leds/Kconfig
drivers/leds/leds-pca9532.c
include/linux/leds-pca9532.h

index 9bec8699b8a327e107ddeb4fcfadb01e588c54e7..09de8dac8a730a255b91d917b76960e805185f87 100644 (file)
@@ -162,6 +162,16 @@ config LEDS_PCA9532
          LED controller. It is generally only useful
          as a platform driver
 
+config LEDS_PCA9532_GPIO
+       bool "Enable GPIO support for PCA9532"
+       depends on LEDS_PCA9532
+       depends on GPIOLIB
+       help
+         Allow unused pins on PCA9532 to be used as gpio.
+
+         To use a pin as gpio pca9532_type in pca9532_platform data needs to
+         set to PCA9532_TYPE_GPIO.
+
 config LEDS_GPIO
        tristate "LED Support for GPIO connected LEDs"
        depends on LEDS_CLASS
index 5bf63af09ddf04adb63dac74bfa97b8882e0c754..ebea85603f4347d3ac50e5ac037c74bbeb0eadf2 100644 (file)
@@ -19,7 +19,9 @@
 #include <linux/mutex.h>
 #include <linux/workqueue.h>
 #include <linux/leds-pca9532.h>
+#include <linux/gpio.h>
 
+#define PCA9532_REG_INPUT(i) ((i)/8)
 #define PCA9532_REG_PSC(i) (0x2+(i)*2)
 #define PCA9532_REG_PWM(i) (0x3+(i)*2)
 #define PCA9532_REG_LS0  0x6
@@ -34,6 +36,9 @@ struct pca9532_data {
        struct mutex update_lock;
        struct input_dev *idev;
        struct work_struct work;
+#ifdef CONFIG_LEDS_PCA9532_GPIO
+       struct gpio_chip gpio;
+#endif
        u8 pwm[2];
        u8 psc[2];
 };
@@ -200,16 +205,68 @@ static void pca9532_led_work(struct work_struct *work)
        pca9532_setled(led);
 }
 
-static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
+#ifdef CONFIG_LEDS_PCA9532_GPIO
+static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
+{
+       struct pca9532_data *data = container_of(gc, struct pca9532_data, gpio);
+       struct pca9532_led *led = &data->leds[offset];
+
+       if (led->type == PCA9532_TYPE_GPIO)
+               return 0;
+
+       return -EBUSY;
+}
+
+static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
+{
+       struct pca9532_data *data = container_of(gc, struct pca9532_data, gpio);
+       struct pca9532_led *led = &data->leds[offset];
+
+       if (val)
+               led->state = PCA9532_ON;
+       else
+               led->state = PCA9532_OFF;
+
+       pca9532_setled(led);
+}
+
+static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
+{
+       struct pca9532_data *data = container_of(gc, struct pca9532_data, gpio);
+       unsigned char reg;
+
+       reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
+
+       return !!(reg & (1 << (offset % 8)));
+}
+
+static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
+{
+       /* To use as input ensure pin is not driven */
+       pca9532_gpio_set_value(gc, offset, 0);
+
+       return 0;
+}
+
+static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
+{
+       pca9532_gpio_set_value(gc, offset, val);
+
+       return 0;
+}
+#endif /* CONFIG_LEDS_PCA9532_GPIO */
+
+static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
 {
        int i = n_devs;
 
        if (!data)
-               return;
+               return -EINVAL;
 
        while (--i >= 0) {
                switch (data->leds[i].type) {
                case PCA9532_TYPE_NONE:
+               case PCA9532_TYPE_GPIO:
                        break;
                case PCA9532_TYPE_LED:
                        led_classdev_unregister(&data->leds[i].ldev);
@@ -224,12 +281,26 @@ static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
                        break;
                }
        }
+
+#ifdef CONFIG_LEDS_PCA9532_GPIO
+       if (data->gpio.dev) {
+               int err = gpiochip_remove(&data->gpio);
+               if (err) {
+                       dev_err(&data->client->dev, "%s failed, %d\n",
+                                               "gpiochip_remove()", err);
+                       return err;
+               }
+       }
+#endif
+
+       return 0;
 }
 
 static int pca9532_configure(struct i2c_client *client,
        struct pca9532_data *data, struct pca9532_platform_data *pdata)
 {
        int i, err = 0;
+       int gpios = 0;
 
        for (i = 0; i < 2; i++) {
                data->pwm[i] = pdata->pwm[i];
@@ -249,6 +320,9 @@ static int pca9532_configure(struct i2c_client *client,
                switch (led->type) {
                case PCA9532_TYPE_NONE:
                        break;
+               case PCA9532_TYPE_GPIO:
+                       gpios++;
+                       break;
                case PCA9532_TYPE_LED:
                        led->state = pled->state;
                        led->name = pled->name;
@@ -297,6 +371,34 @@ static int pca9532_configure(struct i2c_client *client,
                        break;
                }
        }
+
+#ifdef CONFIG_LEDS_PCA9532_GPIO
+       if (gpios) {
+               data->gpio.label = "gpio-pca9532";
+               data->gpio.direction_input = pca9532_gpio_direction_input;
+               data->gpio.direction_output = pca9532_gpio_direction_output;
+               data->gpio.set = pca9532_gpio_set_value;
+               data->gpio.get = pca9532_gpio_get_value;
+               data->gpio.request = pca9532_gpio_request_pin;
+               data->gpio.can_sleep = 1;
+               data->gpio.base = pdata->gpio_base;
+               data->gpio.ngpio = 16;
+               data->gpio.dev = &client->dev;
+               data->gpio.owner = THIS_MODULE;
+
+               err = gpiochip_add(&data->gpio);
+               if (err) {
+                       /* Use data->gpio.dev as a flag for freeing gpiochip */
+                       data->gpio.dev = NULL;
+                       dev_warn(&client->dev, "could not add gpiochip\n");
+               } else {
+                       dev_info(&client->dev, "gpios %i...%i\n",
+                               data->gpio.base, data->gpio.base +
+                               data->gpio.ngpio - 1);
+               }
+       }
+#endif
+
        return 0;
 
 exit:
@@ -337,7 +439,12 @@ static int pca9532_probe(struct i2c_client *client,
 static int pca9532_remove(struct i2c_client *client)
 {
        struct pca9532_data *data = i2c_get_clientdata(client);
-       pca9532_destroy_devices(data, 16);
+       int err;
+
+       err = pca9532_destroy_devices(data, 16);
+       if (err)
+               return err;
+
        kfree(data);
        return 0;
 }
index f158eb1149aa8b6cd0029ca92ac2645a672a3928..b8d6fffed4d801c938e69bf7aa99fe3ad98085cb 100644 (file)
@@ -25,7 +25,7 @@ enum pca9532_state {
 };
 
 enum pca9532_type { PCA9532_TYPE_NONE, PCA9532_TYPE_LED,
-       PCA9532_TYPE_N2100_BEEP };
+       PCA9532_TYPE_N2100_BEEP, PCA9532_TYPE_GPIO };
 
 struct pca9532_led {
        u8 id;
@@ -41,6 +41,7 @@ struct pca9532_platform_data {
        struct pca9532_led leds[16];
        u8 pwm[2];
        u8 psc[2];
+       int gpio_base;
 };
 
 #endif /* __LINUX_PCA9532_H */