From patchwork Mon May 31 19:28:59 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_R=C3=83=C2=A9tornaz?= X-Patchwork-Id: 103321 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o4VJhdZH017014 for ; Mon, 31 May 2010 19:43:40 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756958Ab0EaTnj (ORCPT ); Mon, 31 May 2010 15:43:39 -0400 Received: from smtp3.epfl.ch ([128.178.224.226]:47204 "HELO smtp3.epfl.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1756962Ab0EaTni (ORCPT ); Mon, 31 May 2010 15:43:38 -0400 Received: (qmail 15079 invoked by uid 107); 31 May 2010 19:36:55 -0000 X-Virus-Scanned: ClamAV Received: from lsro1pc40.epfl.ch (128.178.145.86) (authenticated) by smtp3.epfl.ch (AngelmatoPhylax SMTP proxy); Mon, 31 May 2010 21:36:55 +0200 From: =?utf-8?q?Philippe=20R=C3=A9tornaz?= To: s.hauer@pengutronix.de Cc: linux-arm-kernel@lists.infradead.org, dmitry.torokhov@gmail.com, sameo@linux.intel.com, linux-input@vger.kernel.org, broonie@opensource.wolfsonmicro.com, u.kleine-koenig@pengutronix.de, ext-jari.vanhala@nokia.com, valentin.longchamp@epfl.ch, =?utf-8?q?Philippe=20R=C3=A9tornaz?= Subject: [PATCH 1/2] mc13783: add power button support Date: Mon, 31 May 2010 21:28:59 +0200 Message-Id: <1275334140-29339-2-git-send-email-philippe.retornaz@epfl.ch> X-Mailer: git-send-email 1.6.3.3 In-Reply-To: <1275334140-29339-1-git-send-email-philippe.retornaz@epfl.ch> References: <1275334140-29339-1-git-send-email-philippe.retornaz@epfl.ch> MIME-Version: 1.0 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, 31 May 2010 19:43:40 +0000 (UTC) diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig index 23140a3..2d25b16 100644 --- a/drivers/input/misc/Kconfig +++ b/drivers/input/misc/Kconfig @@ -340,4 +340,11 @@ config INPUT_PCAP To compile this driver as a module, choose M here: the module will be called pcap_keys. +config INPUT_MC13783_ON + tristate "MC13783 ON buttons" + depends on MFD_MC13783 + help + Support the ON buttons of MC13783 PMIC as an input device + reporting power button status. + endif diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile index 7e95a5d..c96311d 100644 --- a/drivers/input/misc/Makefile +++ b/drivers/input/misc/Makefile @@ -32,4 +32,5 @@ obj-$(CONFIG_INPUT_WINBOND_CIR) += winbond-cir.o obj-$(CONFIG_INPUT_WISTRON_BTNS) += wistron_btns.o obj-$(CONFIG_INPUT_WM831X_ON) += wm831x-on.o obj-$(CONFIG_INPUT_YEALINK) += yealink.o +obj-$(CONFIG_INPUT_MC13783_ON) += mc13783-pwrbutton.o diff --git a/drivers/input/misc/mc13783-pwrbutton.c b/drivers/input/misc/mc13783-pwrbutton.c new file mode 100644 index 0000000..61cfcce --- /dev/null +++ b/drivers/input/misc/mc13783-pwrbutton.c @@ -0,0 +1,301 @@ +/** + * mc13783-pwrbutton.c - MC13783 Power Button Input Driver + * + * Copyright (C) 2010 Philippe Rétornaz + * + * Based on twl4030-pwrbutton driver by: + * Peter De Schrijver + * Felipe Balbi + * + * This file is subject to the terms and conditions of the GNU General + * Public License. See the file "COPYING" in the main directory of this + * archive for more details. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct mc13783_pwrb { + struct input_dev *pwr; + struct mc13783 *mc13783; +#define MC13783_PWRB_B1_EMULATE_CAD (1 << 0) +#define MC13783_PWRB_B2_EMULATE_CAD (1 << 1) +#define MC13783_PWRB_B3_EMULATE_CAD (1 << 2) +#define MC13783_PWRB_B1_POL_INVERT (1 << 3) +#define MC13783_PWRB_B2_POL_INVERT (1 << 4) +#define MC13783_PWRB_B3_POL_INVERT (1 << 5) + int flags; + unsigned int b1on_key; + unsigned int b2on_key; + unsigned int b3on_key; +}; + +#define MC13783_REG_INTERRUPT_SENSE_1 5 +#define MC13783_IRQSENSE1_ONOFD1S (1 << 3) +#define MC13783_IRQSENSE1_ONOFD2S (1 << 4) +#define MC13783_IRQSENSE1_ONOFD3S (1 << 5) + +#define MC13783_REG_POWER_CONTROL_2 15 +#define MC13783_POWER_CONTROL_2_ON1BDBNC 4 +#define MC13783_POWER_CONTROL_2_ON2BDBNC 6 +#define MC13783_POWER_CONTROL_2_ON3BDBNC 8 +#define MC13783_POWER_CONTROL_2_ON1BRSTEN (1 << 1) +#define MC13783_POWER_CONTROL_2_ON2BRSTEN (1 << 2) +#define MC13783_POWER_CONTROL_2_ON3BRSTEN (1 << 3) + +static irqreturn_t b1on_irq(int irq, void *_priv) +{ + struct mc13783_pwrb *priv = _priv; + int val; + + mc13783_irq_ack(priv->mc13783, irq); + mc13783_reg_read(priv->mc13783, MC13783_REG_INTERRUPT_SENSE_1, &val); + + val = val & MC13783_IRQSENSE1_ONOFD1S ? 1 : 0; + + if (priv->flags & MC13783_PWRB_B1_POL_INVERT) + val ^= 1; + + if (val && priv->flags & MC13783_PWRB_B1_EMULATE_CAD) + kill_cad_pid(SIGINT, 1); + + input_report_key(priv->pwr, priv->b1on_key, val); + input_sync(priv->pwr); + return IRQ_HANDLED; +} + +static irqreturn_t b2on_irq(int irq, void *_priv) +{ + struct mc13783_pwrb *priv = _priv; + int val; + + mc13783_irq_ack(priv->mc13783, irq); + mc13783_reg_read(priv->mc13783, MC13783_REG_INTERRUPT_SENSE_1, &val); + + val = val & MC13783_IRQSENSE1_ONOFD2S ? 1 : 0; + + if (priv->flags & MC13783_PWRB_B2_POL_INVERT) + val ^= 1; + + if (val && priv->flags & MC13783_PWRB_B2_EMULATE_CAD) + kill_cad_pid(SIGINT, 1); + + input_report_key(priv->pwr, priv->b2on_key, val); + input_sync(priv->pwr); + return IRQ_HANDLED; +} + +static irqreturn_t b3on_irq(int irq, void *_priv) +{ + struct mc13783_pwrb *priv = _priv; + int val; + + mc13783_irq_ack(priv->mc13783, irq); + mc13783_reg_read(priv->mc13783, MC13783_REG_INTERRUPT_SENSE_1, &val); + + val = val & MC13783_IRQSENSE1_ONOFD3S ? 1 : 0; + + if (priv->flags & MC13783_PWRB_B3_POL_INVERT) + val ^= 1; + + if (val && priv->flags & MC13783_PWRB_B3_EMULATE_CAD) + kill_cad_pid(SIGINT, 1); + + input_report_key(priv->pwr, priv->b3on_key, val); + input_sync(priv->pwr); + return IRQ_HANDLED; +} + +static int __devinit mc13783_pwrbutton_probe(struct platform_device *pdev) +{ + struct mc13783_buttons_platform_data *pdata; + struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent); + struct input_dev *pwr; + struct mc13783_pwrb *priv; + int err = 0; + int reg = 0; + + pdata = dev_get_platdata(&pdev->dev); + if (pdata == NULL) { + dev_err(&pdev->dev, "missing platform data\n"); + return -ENODEV; + } + + pwr = input_allocate_device(); + if (!pwr) { + dev_dbg(&pdev->dev, "Can't allocate power button\n"); + return -ENOMEM; + } + + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + + if (!priv) { + dev_dbg(&pdev->dev, "Can't allocate power button\n"); + goto free_input_dev; + } + + pwr->evbit[0] = BIT_MASK(EV_KEY); + + reg |= (pdata->b1on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON1BDBNC; + reg |= (pdata->b2on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON2BDBNC; + reg |= (pdata->b3on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON3BDBNC; + + priv->pwr = pwr; + priv->mc13783 = mc13783; + + mc13783_lock(mc13783); + + if (pdata->b1on_flags & MC13783_BUTTON_ENABLE) { + priv->b1on_key = pdata->b1on_key; + if (pdata->b1on_key != KEY_RESERVED) + pwr->keybit[BIT_WORD(pdata->b1on_key)] |= + BIT_MASK(pdata->b1on_key); + + if (pdata->b1on_flags & MC13783_BUTTON_POL_INVERT) + priv->flags |= MC13783_PWRB_B1_POL_INVERT; + if (pdata->b1on_flags & MC13783_BUTTON_EMUL_CAD) + priv->flags |= MC13783_PWRB_B1_EMULATE_CAD; + + if (pdata->b1on_flags & MC13783_BUTTON_RESET_EN) + reg |= MC13783_POWER_CONTROL_2_ON1BRSTEN; + + mc13783_irq_request(mc13783, MC13783_IRQ_ONOFD1, b1on_irq, + "b1on", priv); + } + + if (pdata->b2on_flags & MC13783_BUTTON_ENABLE) { + priv->b2on_key = pdata->b2on_key; + if (pdata->b2on_key != KEY_RESERVED) + pwr->keybit[BIT_WORD(pdata->b2on_key)] |= + BIT_MASK(pdata->b2on_key); + + if (pdata->b2on_flags & MC13783_BUTTON_POL_INVERT) + priv->flags |= MC13783_PWRB_B2_POL_INVERT; + if (pdata->b2on_flags & MC13783_BUTTON_EMUL_CAD) + priv->flags |= MC13783_PWRB_B2_EMULATE_CAD; + + if (pdata->b2on_flags & MC13783_BUTTON_RESET_EN) + reg |= MC13783_POWER_CONTROL_2_ON2BRSTEN; + + mc13783_irq_request(mc13783, MC13783_IRQ_ONOFD2, b2on_irq, + "b2on", priv); + } + + if (pdata->b3on_flags & MC13783_BUTTON_ENABLE) { + priv->b3on_key = pdata->b3on_key; + if (pdata->b3on_key != KEY_RESERVED) + pwr->keybit[BIT_WORD(pdata->b3on_key)] |= + BIT_MASK(pdata->b3on_key); + + if (pdata->b3on_flags & MC13783_BUTTON_POL_INVERT) + priv->flags |= MC13783_PWRB_B3_POL_INVERT; + if (pdata->b3on_flags & MC13783_BUTTON_EMUL_CAD) + priv->flags |= MC13783_PWRB_B3_EMULATE_CAD; + + if (pdata->b3on_flags & MC13783_BUTTON_RESET_EN) + reg |= MC13783_POWER_CONTROL_2_ON3BRSTEN; + + mc13783_irq_request(mc13783, MC13783_IRQ_ONOFD3, b3on_irq, + "b3on", priv); + } + + mc13783_reg_rmw(mc13783, MC13783_REG_POWER_CONTROL_2, 0x3FE, reg); + + mc13783_unlock(mc13783); + + pwr->name = "mc13783_pwrbutton"; + pwr->phys = "mc13783_pwrbutton/input0"; + pwr->dev.parent = &pdev->dev; + + err = input_register_device(pwr); + if (err) { + dev_dbg(&pdev->dev, "Can't register power button: %d\n", err); + goto free_irq; + } + + platform_set_drvdata(pdev, priv); + + return 0; + +free_irq: + mc13783_lock(mc13783); + if (pdata->b3on_flags & MC13783_BUTTON_ENABLE) + mc13783_irq_free(mc13783, MC13783_IRQ_ONOFD3, priv); + if (pdata->b2on_flags & MC13783_BUTTON_ENABLE) + mc13783_irq_free(mc13783, MC13783_IRQ_ONOFD2, priv); + if (pdata->b1on_flags & MC13783_BUTTON_ENABLE) + mc13783_irq_free(mc13783, MC13783_IRQ_ONOFD1, priv); + mc13783_unlock(mc13783); + + kfree(priv); + +free_input_dev: + input_free_device(pwr); + return err; +} + +static int __devexit mc13783_pwrbutton_remove(struct platform_device *pdev) +{ + struct mc13783_pwrb *priv = platform_get_drvdata(pdev); + struct mc13783_buttons_platform_data *pdata; + pdata = dev_get_platdata(&pdev->dev); + + mc13783_lock(priv->mc13783); + if (pdata->b3on_flags & MC13783_BUTTON_ENABLE) + mc13783_irq_free(priv->mc13783, MC13783_IRQ_ONOFD3, priv); + if (pdata->b2on_flags & MC13783_BUTTON_ENABLE) + mc13783_irq_free(priv->mc13783, MC13783_IRQ_ONOFD2, priv); + if (pdata->b1on_flags & MC13783_BUTTON_ENABLE) + mc13783_irq_free(priv->mc13783, MC13783_IRQ_ONOFD1, priv); + mc13783_unlock(priv->mc13783); + + input_unregister_device(priv->pwr); + + kfree(priv); + + return 0; +} + +struct platform_driver mc13783_pwrbutton_driver = { + .probe = mc13783_pwrbutton_probe, + .remove = __devexit_p(mc13783_pwrbutton_remove), + .driver = { + .name = "mc13783-pwrbutton", + .owner = THIS_MODULE, + }, +}; + +static int __init mc13783_pwrbutton_init(void) +{ + return platform_driver_register(&mc13783_pwrbutton_driver); +} +module_init(mc13783_pwrbutton_init); + +static void __exit mc13783_pwrbutton_exit(void) +{ + platform_driver_unregister(&mc13783_pwrbutton_driver); +} +module_exit(mc13783_pwrbutton_exit); + +MODULE_ALIAS("platform:mc13783-pwrbutton"); +MODULE_DESCRIPTION("MC13783 Power Button"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Philippe Retornaz"); + diff --git a/drivers/mfd/mc13783-core.c b/drivers/mfd/mc13783-core.c index fecf38a..fb3e98b 100644 --- a/drivers/mfd/mc13783-core.c +++ b/drivers/mfd/mc13783-core.c @@ -683,6 +683,10 @@ err_revision: mc13783_add_subdevice_pdata(mc13783, "mc13783-led", pdata->leds, sizeof(*pdata->leds)); + if (pdata->flags & MC13783_USE_BUTTON) + mc13783_add_subdevice_pdata(mc13783, "mc13783-pwrbutton", + pdata->buttons, sizeof(*pdata->buttons)); + return 0; } diff --git a/include/linux/mfd/mc13783.h b/include/linux/mfd/mc13783.h index 4a894f6..66981a5 100644 --- a/include/linux/mfd/mc13783.h +++ b/include/linux/mfd/mc13783.h @@ -128,6 +128,23 @@ struct mc13783_leds_platform_data { char tc3_period; }; +struct mc13783_buttons_platform_data { +#define MC13783_BUTTON_DBNC_0MS 0 +#define MC13783_BUTTON_DBNC_30MS 1 +#define MC13783_BUTTON_DBNC_150MS 2 +#define MC13783_BUTTON_DBNC_750MS 3 +#define MC13783_BUTTON_ENABLE (1 << 2) +#define MC13783_BUTTON_POL_INVERT (1 << 3) +#define MC13783_BUTTON_RESET_EN (1 << 4) +#define MC13783_BUTTON_EMUL_CAD (1 << 5) + int b1on_flags; + unsigned int b1on_key; + int b2on_flags; + unsigned int b2on_key; + int b3on_flags; + unsigned int b3on_key; +}; + /* to be cleaned up */ struct regulator_init_data; @@ -145,6 +162,7 @@ struct mc13783_platform_data { int num_regulators; struct mc13783_regulator_init_data *regulators; struct mc13783_leds_platform_data *leds; + struct mc13783_buttons_platform_data *buttons; #define MC13783_USE_TOUCHSCREEN (1 << 0) #define MC13783_USE_CODEC (1 << 1) @@ -152,6 +170,7 @@ struct mc13783_platform_data { #define MC13783_USE_RTC (1 << 3) #define MC13783_USE_REGULATOR (1 << 4) #define MC13783_USE_LED (1 << 5) +#define MC13783_USE_BUTTON (1 << 6) unsigned int flags; };