From patchwork Mon Jul 19 13:00:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaolong CHEN X-Patchwork-Id: 112658 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o6JD19Bh005475 for ; Mon, 19 Jul 2010 13:01:10 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932230Ab0GSNBG (ORCPT ); Mon, 19 Jul 2010 09:01:06 -0400 Received: from exprod5og114.obsmtp.com ([64.18.0.28]:40737 "EHLO exprod5og114.obsmtp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932190Ab0GSNBF convert rfc822-to-8bit (ORCPT ); Mon, 19 Jul 2010 09:01:05 -0400 Received: from source ([136.182.1.24]) (using TLSv1) by exprod5ob114.postini.com ([64.18.4.12]) with SMTP ID DSNKTERMjpHuuGFzDpW469C9CVf9+4SdNR+Y@postini.com; Mon, 19 Jul 2010 06:01:05 PDT Received: from il27vts01 (il27vts01.cig.mot.com [10.17.196.85]) by mdgate2.corp.mot.com (8.14.3/8.14.3) with SMTP id o6JD0GOt018244 for ; Mon, 19 Jul 2010 07:00:16 -0600 (MDT) Received: from mail-gx0-f182.google.com (mail-gx0-f182.google.com [209.85.161.182]) by mdgate2.corp.mot.com (8.14.3/8.14.3) with ESMTP id o6JD0Gqc018230 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=OK) for ; Mon, 19 Jul 2010 07:00:16 -0600 (MDT) Received: by gxk24 with SMTP id 24so3092025gxk.27 for ; Mon, 19 Jul 2010 06:00:57 -0700 (PDT) MIME-Version: 1.0 Received: by 10.90.54.16 with SMTP id c16mr2918849aga.27.1279544455776; Mon, 19 Jul 2010 06:00:55 -0700 (PDT) Received: by 10.231.174.76 with HTTP; Mon, 19 Jul 2010 06:00:55 -0700 (PDT) In-Reply-To: <20100716164239.GC9232@core.coreip.homeip.net> References: <544AC56F16B56944AEC3BD4E3D5917712E7EA5A357@LIMKCMBX1.ad.analog.com> <20100716162017.GA9232@core.coreip.homeip.net> <20100716164239.GC9232@core.coreip.homeip.net> Date: Mon, 19 Jul 2010 21:00:55 +0800 Message-ID: Subject: Re: [RFC] [PATCH] Input: ADP5588 - Support gpio function on unused pin From: Xiaolong CHEN To: Dmitry Torokhov Cc: Xiaolong CHEN , "Hennerich, Michael" , david-b@pacbell.net, Michael Hennerich , linux-input@vger.kernel.org, TAO HU , "Yuan.Bo YE" X-CFilter-Loop: Reflected Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 19 Jul 2010 13:01:10 +0000 (UTC) diff --git a/drivers/input/keyboard/adp5588-keys.c b/drivers/input/keyboard/adp5588-keys.c index 3c8876a..66d1e4a 100644 --- a/drivers/input/keyboard/adp5588-keys.c +++ b/drivers/input/keyboard/adp5588-keys.c @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -52,6 +53,10 @@ #define KEYP_MAX_EVENT 10 +#define MAXGPIO 18 +#define ADP_BANK(offs) ((offs) >> 3) +#define ADP_BIT(offs) (1u << ((offs) & 0x7)) + /* * Early pre 4.0 Silicon required to delay readout by at least 25ms, * since the Event Counter Register updated 25ms after the interrupt @@ -67,6 +72,12 @@ struct adp5588_kpad { unsigned short keycode[ADP5588_KEYMAPSIZE]; const struct adp5588_gpi_map *gpimap; unsigned short gpimapsize; + unsigned char gpiomap[MAXGPIO]; + bool support_gpio; + struct gpio_chip gpio_chip; + struct mutex gpio_lock; /* Protect cached dir, dat_out */ + uint8_t dat_out[3]; + uint8_t dir[3]; }; static int adp5588_read(struct i2c_client *client, u8 reg) @@ -84,6 +95,85 @@ static int adp5588_write(struct i2c_client *client, u8 reg, u8 val) return i2c_smbus_write_byte_data(client, reg, val); } +static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off) +{ + unsigned bank, bit; + struct adp5588_kpad *kpad = + container_of(chip, struct adp5588_kpad, gpio_chip); + + bank = ADP_BANK(kpad->gpiomap[off]); + bit = ADP_BIT(kpad->gpiomap[off]); + + return !!(adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank) & bit); +} + +static void adp5588_gpio_set_value(struct gpio_chip *chip, + unsigned off, int val) +{ + unsigned bank, bit; + struct adp5588_kpad *kpad = + container_of(chip, struct adp5588_kpad, gpio_chip); + + bank = ADP_BANK(kpad->gpiomap[off]); + bit = ADP_BIT(kpad->gpiomap[off]); + + mutex_lock(&kpad->gpio_lock); + if (val) + kpad->dat_out[bank] |= bit; + else + kpad->dat_out[bank] &= ~bit; + + adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, + kpad->dat_out[bank]); + mutex_unlock(&kpad->gpio_lock); +} + +static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off) +{ + int ret; + unsigned bank, bit; + struct adp5588_kpad *kpad = + container_of(chip, struct adp5588_kpad, gpio_chip); + + bank = ADP_BANK(kpad->gpiomap[off]); + bit = ADP_BIT(kpad->gpiomap[off]); + + mutex_lock(&kpad->gpio_lock); + kpad->dir[bank] &= ~bit; + ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); + mutex_unlock(&kpad->gpio_lock); + + return ret; +} + +static int adp5588_gpio_direction_output(struct gpio_chip *chip, + unsigned off, int val) +{ + int ret; + unsigned bank, bit; + struct adp5588_kpad *kpad = + container_of(chip, struct adp5588_kpad, gpio_chip); + + bank = ADP_BANK(kpad->gpiomap[off]); + bit = ADP_BIT(kpad->gpiomap[off]); + + mutex_lock(&kpad->gpio_lock); + kpad->dir[bank] |= bit; + + if (val) + kpad->dat_out[bank] |= bit; + else + kpad->dat_out[bank] &= ~bit; + + ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, + kpad->dat_out[bank]); + ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank, + kpad->dir[bank]); + mutex_unlock(&kpad->gpio_lock); + + return ret; +} + static void adp5588_work(struct work_struct *work) { struct adp5588_kpad *kpad = container_of(work, @@ -180,6 +270,15 @@ static int __devinit adp5588_setup(struct i2c_client *client) ret |= adp5588_write(client, GPI_EM3, evt_mode3); } + if (pdata->gpio_data) { + for (i = 0; i <= ADP_BANK(MAXGPIO); i++) { + int pull_mask = pdata->gpio_data->pullup_dis_mask; + + ret |= adp5588_write(client, GPIO_PULL1 + i, + (pull_mask >> (8 * i)) & 0xFF); + } + } + ret |= adp5588_write(client, INT_STAT, CMP2_INT | CMP1_INT | OVR_FLOW_INT | K_LCK_INT | GPI_INT | KE_INT); /* Status is W1C */ @@ -204,6 +303,7 @@ static int __devinit adp5588_probe(struct i2c_client *client, int ret, i; int error; int gpi_stat1 = 0, gpi_stat2 = 0, gpi_stat3 = 0; + struct gpio_chip *gc; if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { @@ -273,6 +373,44 @@ static int __devinit adp5588_probe(struct i2c_client *client, kpad->input = input; INIT_DELAYED_WORK(&kpad->work, adp5588_work); + if (pdata->gpio_data) { + int j = 0; + bool pin_used[MAXGPIO]; + + for (i = 0; i < pdata->rows; i++) + pin_used[i] = true; + + for (i = 0; i < pdata->cols; i++) + pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true; + + for (i = 0; i < kpad->gpimapsize; i++) + pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true; + + for (i = 0; i < MAXGPIO; i++) { + if (!pin_used[i]) + kpad->gpiomap[j++] = i; + } + kpad->gpio_chip.ngpio = j; + + if (kpad->gpio_chip.ngpio) + kpad->support_gpio = true; + } + + if (kpad->support_gpio) { + gc = &kpad->gpio_chip; + gc->direction_input = adp5588_gpio_direction_input; + gc->direction_output = adp5588_gpio_direction_output; + gc->get = adp5588_gpio_get_value; + gc->set = adp5588_gpio_set_value; + gc->can_sleep = 1; + + gc->base = pdata->gpio_data->gpio_start; + gc->label = client->name; + gc->owner = THIS_MODULE; + + mutex_init(&kpad->gpio_lock); + } + ret = adp5588_read(client, DEV_ID); if (ret < 0) { error = ret; @@ -340,6 +478,31 @@ static int __devinit adp5588_probe(struct i2c_client *client, device_init_wakeup(&client->dev, 1); i2c_set_clientdata(client, kpad); + if (kpad->support_gpio) { + ret = gpiochip_add(&kpad->gpio_chip); + if (ret) { + dev_err(&client->dev, "gpiochip_add err: %d\n", + ret); + goto err_free_irq; + } + + for (i = 0; i <= ADP_BANK(MAXGPIO); i++) { + kpad->dat_out[i] = adp5588_read(client, + GPIO_DAT_OUT1 + i); + kpad->dir[i] = adp5588_read(client, + GPIO_DIR1 + i); + } + + if (pdata->gpio_data->setup) { + ret = pdata->gpio_data->setup(client, + kpad->gpio_chip.base, kpad->gpio_chip.ngpio, + pdata->gpio_data->context); + if (ret < 0) + dev_warn(&client->dev, + "setup failed, %d\n", ret); + } + } + if (kpad->gpimapsize) { gpi_stat1 = adp5588_read(client, GPIO_DAT_STAT1); gpi_stat2 = adp5588_read(client, GPIO_DAT_STAT2); @@ -389,12 +552,33 @@ static int __devinit adp5588_probe(struct i2c_client *client, static int __devexit adp5588_remove(struct i2c_client *client) { + int ret; + struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; struct adp5588_kpad *kpad = i2c_get_clientdata(client); adp5588_write(client, CFG, 0); free_irq(client->irq, kpad); cancel_delayed_work_sync(&kpad->work); input_unregister_device(kpad->input); + if (kpad->support_gpio) { + if (pdata->gpio_data->teardown) { + ret = pdata->gpio_data->teardown(client, + kpad->gpio_chip.base, kpad->gpio_chip.ngpio, + pdata->gpio_data->context); + if (ret < 0) { + dev_err(&client->dev, + "teardown failed %d\n", ret); + return ret; + } + } + + ret = gpiochip_remove(&kpad->gpio_chip); + if (ret) { + dev_err(&client->dev, + "gpiochip_remove failed %d\n", ret); + return ret; + } + } i2c_set_clientdata(client, NULL); kfree(kpad); diff --git a/include/linux/i2c/adp5588.h b/include/linux/i2c/adp5588.h index b6e8807..ede3cd2 100644 --- a/include/linux/i2c/adp5588.h +++ b/include/linux/i2c/adp5588.h @@ -123,6 +123,7 @@ struct adp5588_kpad_platform_data { unsigned short unlock_key2; /* Unlock Key 2 */ const struct adp5588_gpi_map *gpimap; unsigned short gpimapsize; + struct adp5588_gpio_platform_data *gpio_data; }; struct adp5588_gpio_platform_data {