Skip to contentSkip to conversation

input: keyboard: Add keys driver for the LPC32xx SoC

PATCH Superseded Roland Stigge 2 participants 3 messages Jun 24, 2012 – Jun 25, 2012

Subsystems (4)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 input: keyboard: Add keys driver for the LPC32xx SoC

This patch adds a driver for the key scan interface of the LPC32xx SoC

---
Applies to v3.5-rc3

Changes since v5:
* Handle return code from matrix_keypad_build_keymap()
* Remove platform data structure and integrate into device struct

Changes since v4:
* Remove irq from struct lpc32xx_kscan_drv

Changes since v3:
* Make driver DT-only since LPC32xx is converted to DT anyway now.
* Use MATRIX_SCAN_CODE()
* Use matrix_keypad_build_keymap()
* Emit MSC_SCAN also
* Use PTR_ERR() on error handling
* Rename return value to error
* Removed unnecessary '&' on initializations with function pointers
* Use CONFIG_PM_SLEEP, dev_pm_ops, SIMPLE_DEV_PM_OPS instead of CONFIG_PM
* Removed devm_*() management
* Re-enabled clock for period of probe()

Changes since v2:
* Fixed case of lpc32XX_* -> lpc32xx_*
* Removed unnecessary casts
* Removed unnecessary unlikely()
* Use unsigned for key bits
* Included Russell's lpc32xx_mod_states() algorithm change suggestion
* Replaced (res == NULL) with (!res)
* clk_enable() -> clk_prepare_enable()
* Moved clk_prepare_enable() and clk_disable_unprepare() to input's open()/close() callbacks to save power
* Handle clk_prepare_enable() return value appropriately
* DT: Use "keypad,num-rows", "keypad,num-columns" and "linux,keymap" properties

Changes since v1:
* Removed devm_kfree(), lpc32XX_free_dt(), iounmap(), release_mem_region()
* Changed __raw_writel()+__raw_readl() to writel()+readl()

Thanks to Axel Lin, Russell King, Rob Herring, Shubhrajyoti and Dmitry Torokhov for reviewing!

 Documentation/devicetree/bindings/input/lpc32xx-key.txt |   28 +
 drivers/input/keyboard/Kconfig                          |   10 
 drivers/input/keyboard/Makefile                         |    1 
 drivers/input/keyboard/lpc32xx-keys.c                   |  362 ++++++++++++++++
 4 files changed, 401 insertions(+)
Documentation/devicetree/bindings/input/lpc32xx-key.txt new file +28 -0
@@ -0,0 +1,28 @@
+NXP LPC32xx Key Scan Interface
+
+Required Properties:
+- compatible: Should be "nxp,lpc3220-key"
+- reg: Physical base address of the controller and length of memory mapped
+  region.
+- interrupts: The interrupt number to the cpu.
+- keypad,num-rows: Number of rows and columns, e.g. 1: 1x1, 6: 6x6
+- keypad,num-columns: Must be equal to keypad,num-rows since LPC32xx only
+  supports square matrices
+- nxp,debounce-delay-ms: Debounce delay in ms
+- nxp,scan-delay-ms: Repeated scan period in ms
+- linux,keymap: the key-code to be reported when the key is pressed
+  and released, see also
+  Documentation/devicetree/bindings/input/matrix-keymap.txt
+
+Example:
+
+	key at 40050000 {
+		compatible = "nxp,lpc3220-key";
+		reg = <0x40050000 0x1000>;
+		interrupts = <54 0>;
+		keypad,num-rows = <1>;
+		keypad,num-columns = <1>;
+		nxp,debounce-delay-ms = <3>;
+		nxp,scan-delay-ms = <34>;
+		linux,keymap = <0x00000002>;
+	};
drivers/input/keyboard/Kconfig +10 -0
@@ -332,6 +332,16 @@ config KEYBOARD_LOCOMO
 	  To compile this driver as a module, choose M here: the
 	  module will be called locomokbd.
 
+config KEYBOARD_LPC32XX
+	tristate "LPC32XX matrix key scanner support"
+	depends on ARCH_LPC32XX && OF
+	help
+	  Say Y here if you want to use NXP LPC32XX SoC key scanner interface,
+	  connected to a key matrix.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called lpc32xx-keys.
+
 config KEYBOARD_MAPLE
 	tristate "Maple bus keyboard"
 	depends on SH_DREAMCAST && MAPLE
drivers/input/keyboard/Makefile +1 -0
@@ -26,6 +26,7 @@ obj-$(CONFIG_KEYBOARD_LKKBD)		+= lkkbd.o
 obj-$(CONFIG_KEYBOARD_LM8323)		+= lm8323.o
 obj-$(CONFIG_KEYBOARD_LM8333)		+= lm8333.o
 obj-$(CONFIG_KEYBOARD_LOCOMO)		+= locomokbd.o
+obj-$(CONFIG_KEYBOARD_LPC32XX)		+= lpc32xx-keys.o
 obj-$(CONFIG_KEYBOARD_MAPLE)		+= maple_keyb.o
 obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
 obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
drivers/input/keyboard/lpc32xx-keys.c new file +362 -0

362 lines not on this page · raw message

maintainer
#1 lore raw

HI Roland,

Quoted 4 lines · Roland Stigge

This patch adds a driver for the key scan interface of the LPC32xx SoC

This looks _very_ good, I just have a couple more comments:

drivers/input/keyboard/lpc32xx-keys.c line 125

Quoted 6 lines
drivers/input/keyboard/lpc32xx-keys.c new file +6 -0
+
+static int lpc32xx_kscan_open(struct input_dev *dev)
+{
+	struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
+
+	return clk_prepare_enable(kscandat->clk);

Don't you need to clear IRQ here, the same way you do it in resume?

drivers/input/keyboard/lpc32xx-keys.c line 132

Quoted 7 lines
drivers/input/keyboard/lpc32xx-keys.c new file +7 -0
+}
+
+static void lpc32xx_kscan_close(struct input_dev *dev)
+{
+	struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
+
+	clk_disable_unprepare(kscandat->clk);

And same here.

drivers/input/keyboard/lpc32xx-keys.c line 135

Quoted 3 lines
drivers/input/keyboard/lpc32xx-keys.c new file +3 -0
+}
+
+int lpc32xx_parse_dt(struct platform_device *pdev)

Static? Also why don't you pass kscandat instead of platform device?

Quoted 19 lines
drivers/input/keyboard/lpc32xx-keys.c new file +19 -0
+{
+	struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
+	struct device_node *np = pdev->dev.of_node;
+	u32 rows, columns;
+
+	of_property_read_u32(np, "keypad,num-rows", &rows);
+	of_property_read_u32(np, "keypad,num-columns", &columns);
+	if (!rows || rows != columns) {
+		dev_err(&pdev->dev,
+			"rows and columns must be specified and be equal!\n");
+		return -EINVAL;
+	}
+
+	kscandat->matrix_sz = rows;
+	kscandat->row_shift = get_count_order(columns);
+
+	of_property_read_u32(np, "nxp,debounce-delay-ms", &kscandat->deb_clks);
+	of_property_read_u32(np, "nxp,scan-delay-ms", &kscandat->scan_delay);
+	if (!kscandat->deb_clks || !kscandat->scan_delay) {

drivers/input/keyboard/lpc32xx-keys.c line 164

Quoted 10 lines
drivers/input/keyboard/lpc32xx-keys.c new file +10 -0
+		dev_err(&pdev->dev, "debounce or scan delay not specified\n");
+		return -ENXIO;
+	}
+
+	kscandat->keymap = kzalloc(sizeof(kscandat->keymap[0]) *
+				(rows << kscandat->row_shift), GFP_KERNEL);
+	if (!kscandat->keymap) {
+		dev_err(&pdev->dev, "could not allocate memory for keymap\n");
+		return -ENOMEM;
+	}

I think this allocation belongs to the probe().

Quoted 38 lines
drivers/input/keyboard/lpc32xx-keys.c new file +38 -0
+
+	return 0;
+}
+
+static int __devinit lpc32xx_kscan_probe(struct platform_device *pdev)
+{
+	struct lpc32xx_kscan_drv *kscandat;
+	struct resource *res;
+	int error;
+	int irq;
+
+	kscandat = kzalloc(sizeof(struct lpc32xx_kscan_drv), GFP_KERNEL);
+	if (!kscandat) {
+		dev_err(&pdev->dev, "failed to allocate memory\n");
+		return -ENOMEM;
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res) {
+		dev_err(&pdev->dev, "failed to get platform I/O memory\n");
+		error = -EBUSY;
+		goto out1;
+	}
+
+	res = request_mem_region(res->start, resource_size(res), pdev->name);
+	if (!res) {
+		dev_err(&pdev->dev, "failed to request I/O memory\n");
+		error = -EBUSY;
+		goto out1;
+	}
+	kscandat->io_p_start = res->start;
+	kscandat->io_p_size = resource_size(res);
+
+	kscandat->kscan_base = ioremap(res->start, resource_size(res));
+	if (!kscandat->kscan_base) {
+		dev_err(&pdev->dev, "failed to remap I/O memory\n");
+		error = -EBUSY;
+		goto out2;

drivers/input/keyboard/lpc32xx-keys.c line 212

Quoted 10 lines
drivers/input/keyboard/lpc32xx-keys.c new file +10 -0
+	}
+
+	/* Get the key scanner clock */
+	kscandat->clk = clk_get(&pdev->dev, NULL);
+	if (IS_ERR(kscandat->clk)) {
+		dev_err(&pdev->dev, "failed to get clock\n");
+		error = PTR_ERR(kscandat->clk);
+		goto out3;
+	}
+	clk_enable(kscandat->clk);

Why not clk_prepare_enable()? Also I think you need to move it down, to where you actually write to the device's registers.

Quoted 9 lines
drivers/input/keyboard/lpc32xx-keys.c new file +9 -0
+
+	irq = platform_get_irq(pdev, 0);
+	if ((irq < 0) || (irq >= NR_IRQS)) {
+		dev_err(&pdev->dev, "failed to get platform irq\n");
+		error = -EINVAL;
+		goto out4;
+	}
+	error = request_irq(irq, lpc32xx_kscan_irq, 0, pdev->name, kscandat);
+	if (error) {

drivers/input/keyboard/lpc32xx-keys.c line 231

Quoted 10 lines
drivers/input/keyboard/lpc32xx-keys.c new file +10 -0
+		dev_err(&pdev->dev, "failed to request irq\n");
+		goto out4;
+	}
+
+	kscandat->input = input_allocate_device();
+	if (kscandat->input == NULL) {
+		dev_err(&pdev->dev, "failed to allocate device\n");
+		error = -ENOMEM;
+		goto out5;
+	}

This might be too late - IRQ is alreday registered and clock is enabled
- what is to stop interrupts to be delivered? I'd allocate the input device together with kscandat structure.

Quoted 27 lines
drivers/input/keyboard/lpc32xx-keys.c new file +27 -0
+
+	platform_set_drvdata(pdev, kscandat);
+
+	error = lpc32xx_parse_dt(pdev);
+	if (error) {
+		dev_err(&pdev->dev, "failed to parse device tree\n");
+		goto out6;
+	}
+
+	/* Setup key input */
+	kscandat->input->evbit[0]	= BIT_MASK(EV_KEY);
+	kscandat->input->name		= pdev->name;
+	kscandat->input->phys		= "matrix-keys/input0";
+	kscandat->input->id.vendor	= 0x0001;
+	kscandat->input->id.product	= 0x0001;
+	kscandat->input->id.version	= 0x0100;
+	kscandat->input->open		= lpc32xx_kscan_open;
+	kscandat->input->close		= lpc32xx_kscan_close;
+	kscandat->input->dev.parent	= &pdev->dev;
+
+	error = matrix_keypad_build_keymap(NULL, NULL, kscandat->matrix_sz,
+					   kscandat->matrix_sz,
+					   kscandat->keymap, kscandat->input);
+	if (error) {
+		dev_err(&pdev->dev, "failed to build keymap\n");
+		goto out6;
+	}

drivers/input/keyboard/lpc32xx-keys.c line 268

Quoted 10 lines
drivers/input/keyboard/lpc32xx-keys.c new file +10 -0
+
+	input_set_drvdata(kscandat->input, kscandat);
+	input_set_capability(kscandat->input, EV_MSC, MSC_SCAN);
+
+	error = input_register_device(kscandat->input);
+	if (error) {
+		dev_err(&pdev->dev, "failed to register input device\n");
+		goto out6;
+	}
+

I think this needs to be done before you register input device as as soon as it is registered it may be used.

drivers/input/keyboard/lpc32xx-keys.c line 277

Quoted 9 lines
drivers/input/keyboard/lpc32xx-keys.c new file +9 -0
+	/* Configure the key scanner */
+	writel(kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base));
+	writel(kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base));
+	writel(LPC32XX_KSCAN_FTST_USE32K_CLK,
+	       LPC32XX_KS_FAST_TST(kscandat->kscan_base));
+	writel(kscandat->matrix_sz,
+	       LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base));
+	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
+	clk_disable(kscandat->clk);

clk_disable_unprepare()?

Quoted 14 lines
drivers/input/keyboard/lpc32xx-keys.c new file +14 -0
+	return 0;
+
+out6:
+	input_free_device(kscandat->input);
+out5:
+	free_irq(irq, pdev);
+out4:
+	clk_disable(kscandat->clk);
+	clk_put(kscandat->clk);
+out3:
+	iounmap(kscandat->kscan_base);
+out2:
+	release_mem_region(kscandat->io_p_start, kscandat->io_p_size);
+out1:

drivers/input/keyboard/lpc32xx-keys.c line 301

Quoted 10 lines
drivers/input/keyboard/lpc32xx-keys.c new file +10 -0
+	kfree(kscandat);
+
+	return error;
+}
+
+static int __devexit lpc32xx_kscan_remove(struct platform_device *pdev)
+{
+	struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
+
+	input_unregister_device(kscandat->input);

Are we certain it is safe to free input device before freeing IRQ? I see we have close(), still I'd feel safer if you swapper unregister and free_irq().

Quoted 9 lines
drivers/input/keyboard/lpc32xx-keys.c new file +9 -0
+	free_irq(platform_get_irq(pdev, 0), pdev);
+	clk_put(kscandat->clk);
+	iounmap(kscandat->kscan_base);
+	release_mem_region(kscandat->io_p_start, kscandat->io_p_size);
+	kfree(kscandat);
+
+	return 0;
+}
+

drivers/input/keyboard/lpc32xx-keys.c line 320

Quoted 10 lines
drivers/input/keyboard/lpc32xx-keys.c new file +10 -0
+#ifdef CONFIG_PM_SLEEP
+static int lpc32xx_kscan_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
+
+	/* Clear IRQ and disable clock */
+	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
+	clk_disable_unprepare(kscandat->clk);
+

This may race with open()/close(). You need to take input_dev->mutex and avoid touching the device if it sis not opened (users == 0).

drivers/input/keyboard/lpc32xx-keys.c line 332

Quoted 12 lines
drivers/input/keyboard/lpc32xx-keys.c new file +12 -0
+	return 0;
+}
+
+static int lpc32xx_kscan_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
+
+	/* Enable clock and clear IRQ */
+	clk_prepare_enable(kscandat->clk);
+	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
+

Same here.

Thanks!

Signature · 1 line
Dmitry
#2 lore raw

Hi Dmitry!

Quoted 7 lines · Dmitry Torokhov
Quoted 4 lines · Roland Stigge

This patch adds a driver for the key scan interface of the LPC32xx SoC

This looks _very_ good, I just have a couple more comments:

Thanks for reviewing!

I think I'm addressing all of those points in my v7 patch.

Roland