2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-07-21 15:45:10 +08:00
|
|
|
/*
|
|
|
|
* Samsung keypad driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Samsung Electronics Co.Ltd
|
|
|
|
* Author: Joonyoung Shim <jy0922.shim@samsung.com>
|
|
|
|
* Author: Donghwa Lee <dh09.lee@samsung.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
2011-12-30 01:58:16 +08:00
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
2010-07-21 15:45:10 +08:00
|
|
|
#include <linux/slab.h>
|
2011-11-02 18:37:22 +08:00
|
|
|
#include <linux/of.h>
|
2010-07-21 15:45:10 +08:00
|
|
|
#include <linux/sched.h>
|
2011-11-08 15:59:41 +08:00
|
|
|
#include <linux/input/samsung-keypad.h>
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
#define SAMSUNG_KEYIFCON 0x00
|
|
|
|
#define SAMSUNG_KEYIFSTSCLR 0x04
|
|
|
|
#define SAMSUNG_KEYIFCOL 0x08
|
|
|
|
#define SAMSUNG_KEYIFROW 0x0c
|
|
|
|
#define SAMSUNG_KEYIFFC 0x10
|
|
|
|
|
|
|
|
/* SAMSUNG_KEYIFCON */
|
|
|
|
#define SAMSUNG_KEYIFCON_INT_F_EN (1 << 0)
|
|
|
|
#define SAMSUNG_KEYIFCON_INT_R_EN (1 << 1)
|
|
|
|
#define SAMSUNG_KEYIFCON_DF_EN (1 << 2)
|
|
|
|
#define SAMSUNG_KEYIFCON_FC_EN (1 << 3)
|
|
|
|
#define SAMSUNG_KEYIFCON_WAKEUPEN (1 << 4)
|
|
|
|
|
|
|
|
/* SAMSUNG_KEYIFSTSCLR */
|
|
|
|
#define SAMSUNG_KEYIFSTSCLR_P_INT_MASK (0xff << 0)
|
|
|
|
#define SAMSUNG_KEYIFSTSCLR_R_INT_MASK (0xff << 8)
|
|
|
|
#define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET 8
|
|
|
|
#define S5PV210_KEYIFSTSCLR_P_INT_MASK (0x3fff << 0)
|
|
|
|
#define S5PV210_KEYIFSTSCLR_R_INT_MASK (0x3fff << 16)
|
|
|
|
#define S5PV210_KEYIFSTSCLR_R_INT_OFFSET 16
|
|
|
|
|
|
|
|
/* SAMSUNG_KEYIFCOL */
|
|
|
|
#define SAMSUNG_KEYIFCOL_MASK (0xff << 0)
|
|
|
|
#define S5PV210_KEYIFCOLEN_MASK (0xff << 8)
|
|
|
|
|
|
|
|
/* SAMSUNG_KEYIFROW */
|
|
|
|
#define SAMSUNG_KEYIFROW_MASK (0xff << 0)
|
|
|
|
#define S5PV210_KEYIFROW_MASK (0x3fff << 0)
|
|
|
|
|
|
|
|
/* SAMSUNG_KEYIFFC */
|
|
|
|
#define SAMSUNG_KEYIFFC_MASK (0x3ff << 0)
|
|
|
|
|
|
|
|
enum samsung_keypad_type {
|
|
|
|
KEYPAD_TYPE_SAMSUNG,
|
|
|
|
KEYPAD_TYPE_S5PV210,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct samsung_keypad {
|
|
|
|
struct input_dev *input_dev;
|
2011-12-30 01:58:16 +08:00
|
|
|
struct platform_device *pdev;
|
2010-07-21 15:45:10 +08:00
|
|
|
struct clk *clk;
|
|
|
|
void __iomem *base;
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
bool stopped;
|
2011-12-30 01:58:16 +08:00
|
|
|
bool wake_enabled;
|
2010-07-21 15:45:10 +08:00
|
|
|
int irq;
|
2011-11-02 18:37:22 +08:00
|
|
|
enum samsung_keypad_type type;
|
2010-07-21 15:45:10 +08:00
|
|
|
unsigned int row_shift;
|
|
|
|
unsigned int rows;
|
|
|
|
unsigned int cols;
|
|
|
|
unsigned int row_state[SAMSUNG_MAX_COLS];
|
|
|
|
unsigned short keycodes[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void samsung_keypad_scan(struct samsung_keypad *keypad,
|
|
|
|
unsigned int *row_state)
|
|
|
|
{
|
|
|
|
unsigned int col;
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
for (col = 0; col < keypad->cols; col++) {
|
2011-11-02 18:37:22 +08:00
|
|
|
if (keypad->type == KEYPAD_TYPE_S5PV210) {
|
2010-07-21 15:45:10 +08:00
|
|
|
val = S5PV210_KEYIFCOLEN_MASK;
|
|
|
|
val &= ~(1 << col) << 8;
|
|
|
|
} else {
|
|
|
|
val = SAMSUNG_KEYIFCOL_MASK;
|
|
|
|
val &= ~(1 << col);
|
|
|
|
}
|
|
|
|
|
|
|
|
writel(val, keypad->base + SAMSUNG_KEYIFCOL);
|
|
|
|
mdelay(1);
|
|
|
|
|
|
|
|
val = readl(keypad->base + SAMSUNG_KEYIFROW);
|
|
|
|
row_state[col] = ~val & ((1 << keypad->rows) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* KEYIFCOL reg clear */
|
|
|
|
writel(0, keypad->base + SAMSUNG_KEYIFCOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool samsung_keypad_report(struct samsung_keypad *keypad,
|
|
|
|
unsigned int *row_state)
|
|
|
|
{
|
|
|
|
struct input_dev *input_dev = keypad->input_dev;
|
|
|
|
unsigned int changed;
|
|
|
|
unsigned int pressed;
|
|
|
|
unsigned int key_down = 0;
|
|
|
|
unsigned int val;
|
|
|
|
unsigned int col, row;
|
|
|
|
|
|
|
|
for (col = 0; col < keypad->cols; col++) {
|
|
|
|
changed = row_state[col] ^ keypad->row_state[col];
|
|
|
|
key_down |= row_state[col];
|
|
|
|
if (!changed)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (row = 0; row < keypad->rows; row++) {
|
|
|
|
if (!(changed & (1 << row)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pressed = row_state[col] & (1 << row);
|
|
|
|
|
|
|
|
dev_dbg(&keypad->input_dev->dev,
|
|
|
|
"key %s, row: %d, col: %d\n",
|
|
|
|
pressed ? "pressed" : "released", row, col);
|
|
|
|
|
|
|
|
val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
|
|
|
|
|
|
|
|
input_event(input_dev, EV_MSC, MSC_SCAN, val);
|
|
|
|
input_report_key(input_dev,
|
|
|
|
keypad->keycodes[val], pressed);
|
|
|
|
}
|
|
|
|
input_sync(keypad->input_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
|
|
|
|
|
|
|
|
return key_down;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct samsung_keypad *keypad = dev_id;
|
|
|
|
unsigned int row_state[SAMSUNG_MAX_COLS];
|
|
|
|
bool key_down;
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
pm_runtime_get_sync(&keypad->pdev->dev);
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
do {
|
2020-11-13 15:27:03 +08:00
|
|
|
readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
|
2010-07-21 15:45:10 +08:00
|
|
|
/* Clear interrupt. */
|
|
|
|
writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
|
|
|
|
|
|
|
|
samsung_keypad_scan(keypad, row_state);
|
|
|
|
|
|
|
|
key_down = samsung_keypad_report(keypad, row_state);
|
|
|
|
if (key_down)
|
|
|
|
wait_event_timeout(keypad->wait, keypad->stopped,
|
|
|
|
msecs_to_jiffies(50));
|
|
|
|
|
|
|
|
} while (key_down && !keypad->stopped);
|
|
|
|
|
2012-01-23 15:27:54 +08:00
|
|
|
pm_runtime_put(&keypad->pdev->dev);
|
2011-12-30 01:58:16 +08:00
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void samsung_keypad_start(struct samsung_keypad *keypad)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
pm_runtime_get_sync(&keypad->pdev->dev);
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
/* Tell IRQ thread that it may poll the device. */
|
|
|
|
keypad->stopped = false;
|
|
|
|
|
|
|
|
clk_enable(keypad->clk);
|
|
|
|
|
|
|
|
/* Enable interrupt bits. */
|
|
|
|
val = readl(keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN;
|
|
|
|
writel(val, keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
|
|
|
|
/* KEYIFCOL reg clear. */
|
|
|
|
writel(0, keypad->base + SAMSUNG_KEYIFCOL);
|
2011-12-30 01:58:16 +08:00
|
|
|
|
2012-01-23 15:27:54 +08:00
|
|
|
pm_runtime_put(&keypad->pdev->dev);
|
2010-07-21 15:45:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void samsung_keypad_stop(struct samsung_keypad *keypad)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
pm_runtime_get_sync(&keypad->pdev->dev);
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
/* Signal IRQ thread to stop polling and disable the handler. */
|
|
|
|
keypad->stopped = true;
|
|
|
|
wake_up(&keypad->wait);
|
|
|
|
disable_irq(keypad->irq);
|
|
|
|
|
|
|
|
/* Clear interrupt. */
|
|
|
|
writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
|
|
|
|
|
|
|
|
/* Disable interrupt bits. */
|
|
|
|
val = readl(keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
|
|
|
|
writel(val, keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
|
|
|
|
clk_disable(keypad->clk);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that chip should not generate interrupts we can safely
|
|
|
|
* re-enable the handler.
|
|
|
|
*/
|
|
|
|
enable_irq(keypad->irq);
|
2011-12-30 01:58:16 +08:00
|
|
|
|
2012-01-23 15:27:54 +08:00
|
|
|
pm_runtime_put(&keypad->pdev->dev);
|
2010-07-21 15:45:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int samsung_keypad_open(struct input_dev *input_dev)
|
|
|
|
{
|
|
|
|
struct samsung_keypad *keypad = input_get_drvdata(input_dev);
|
|
|
|
|
|
|
|
samsung_keypad_start(keypad);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void samsung_keypad_close(struct input_dev *input_dev)
|
|
|
|
{
|
|
|
|
struct samsung_keypad *keypad = input_get_drvdata(input_dev);
|
|
|
|
|
|
|
|
samsung_keypad_stop(keypad);
|
|
|
|
}
|
|
|
|
|
2011-11-02 18:37:22 +08:00
|
|
|
#ifdef CONFIG_OF
|
2012-11-04 03:16:09 +08:00
|
|
|
static struct samsung_keypad_platdata *
|
|
|
|
samsung_keypad_parse_dt(struct device *dev)
|
2011-11-02 18:37:22 +08:00
|
|
|
{
|
|
|
|
struct samsung_keypad_platdata *pdata;
|
|
|
|
struct matrix_keymap_data *keymap_data;
|
|
|
|
uint32_t *keymap, num_rows = 0, num_cols = 0;
|
|
|
|
struct device_node *np = dev->of_node, *key_np;
|
2012-08-22 13:04:46 +08:00
|
|
|
unsigned int key_count;
|
2011-11-02 18:37:22 +08:00
|
|
|
|
2012-11-04 03:16:09 +08:00
|
|
|
if (!np) {
|
|
|
|
dev_err(dev, "missing device tree data\n");
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2011-11-02 18:37:22 +08:00
|
|
|
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
|
|
|
|
if (!pdata) {
|
|
|
|
dev_err(dev, "could not allocate memory for platform data\n");
|
2012-11-04 03:16:09 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2011-11-02 18:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
of_property_read_u32(np, "samsung,keypad-num-rows", &num_rows);
|
|
|
|
of_property_read_u32(np, "samsung,keypad-num-columns", &num_cols);
|
|
|
|
if (!num_rows || !num_cols) {
|
|
|
|
dev_err(dev, "number of keypad rows/columns not specified\n");
|
2012-11-04 03:16:09 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
2011-11-02 18:37:22 +08:00
|
|
|
}
|
|
|
|
pdata->rows = num_rows;
|
|
|
|
pdata->cols = num_cols;
|
|
|
|
|
|
|
|
keymap_data = devm_kzalloc(dev, sizeof(*keymap_data), GFP_KERNEL);
|
|
|
|
if (!keymap_data) {
|
|
|
|
dev_err(dev, "could not allocate memory for keymap data\n");
|
2012-11-04 03:16:09 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2011-11-02 18:37:22 +08:00
|
|
|
}
|
|
|
|
pdata->keymap_data = keymap_data;
|
|
|
|
|
2012-08-22 13:04:46 +08:00
|
|
|
key_count = of_get_child_count(np);
|
2011-11-02 18:37:22 +08:00
|
|
|
keymap_data->keymap_size = key_count;
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:07:58 +08:00
|
|
|
keymap = devm_kcalloc(dev, key_count, sizeof(uint32_t), GFP_KERNEL);
|
2011-11-02 18:37:22 +08:00
|
|
|
if (!keymap) {
|
|
|
|
dev_err(dev, "could not allocate memory for keymap\n");
|
2012-11-04 03:16:09 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2011-11-02 18:37:22 +08:00
|
|
|
}
|
|
|
|
keymap_data->keymap = keymap;
|
|
|
|
|
|
|
|
for_each_child_of_node(np, key_np) {
|
|
|
|
u32 row, col, key_code;
|
|
|
|
of_property_read_u32(key_np, "keypad,row", &row);
|
|
|
|
of_property_read_u32(key_np, "keypad,column", &col);
|
|
|
|
of_property_read_u32(key_np, "linux,code", &key_code);
|
|
|
|
*keymap++ = KEY(row, col, key_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (of_get_property(np, "linux,input-no-autorepeat", NULL))
|
|
|
|
pdata->no_autorepeat = true;
|
2012-11-04 03:16:09 +08:00
|
|
|
|
2015-07-18 08:04:10 +08:00
|
|
|
pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
|
|
|
|
/* legacy name */
|
|
|
|
of_property_read_bool(np, "linux,input-wakeup");
|
|
|
|
|
2011-11-02 18:37:22 +08:00
|
|
|
|
|
|
|
return pdata;
|
|
|
|
}
|
|
|
|
#else
|
2012-11-04 03:16:09 +08:00
|
|
|
static struct samsung_keypad_platdata *
|
|
|
|
samsung_keypad_parse_dt(struct device *dev)
|
2011-11-02 18:37:22 +08:00
|
|
|
{
|
2012-11-04 03:16:09 +08:00
|
|
|
dev_err(dev, "no platform data defined\n");
|
|
|
|
|
|
|
|
return ERR_PTR(-EINVAL);
|
2011-11-02 18:37:22 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-24 13:38:25 +08:00
|
|
|
static int samsung_keypad_probe(struct platform_device *pdev)
|
2010-07-21 15:45:10 +08:00
|
|
|
{
|
|
|
|
const struct samsung_keypad_platdata *pdata;
|
|
|
|
const struct matrix_keymap_data *keymap_data;
|
|
|
|
struct samsung_keypad *keypad;
|
|
|
|
struct resource *res;
|
|
|
|
struct input_dev *input_dev;
|
|
|
|
unsigned int row_shift;
|
|
|
|
unsigned int keymap_size;
|
|
|
|
int error;
|
|
|
|
|
2012-11-04 03:16:09 +08:00
|
|
|
pdata = dev_get_platdata(&pdev->dev);
|
2010-07-21 15:45:10 +08:00
|
|
|
if (!pdata) {
|
2012-11-04 03:16:09 +08:00
|
|
|
pdata = samsung_keypad_parse_dt(&pdev->dev);
|
|
|
|
if (IS_ERR(pdata))
|
|
|
|
return PTR_ERR(pdata);
|
2010-07-21 15:45:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
keymap_data = pdata->keymap_data;
|
|
|
|
if (!keymap_data) {
|
|
|
|
dev_err(&pdev->dev, "no keymap data defined\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* initialize the gpio */
|
|
|
|
if (pdata->cfg_gpio)
|
|
|
|
pdata->cfg_gpio(pdata->rows, pdata->cols);
|
|
|
|
|
|
|
|
row_shift = get_count_order(pdata->cols);
|
|
|
|
keymap_size = (pdata->rows << row_shift) * sizeof(keypad->keycodes[0]);
|
|
|
|
|
2012-11-28 14:34:38 +08:00
|
|
|
keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad) + keymap_size,
|
|
|
|
GFP_KERNEL);
|
|
|
|
input_dev = devm_input_allocate_device(&pdev->dev);
|
|
|
|
if (!keypad || !input_dev)
|
|
|
|
return -ENOMEM;
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2012-11-28 14:34:38 +08:00
|
|
|
if (!res)
|
|
|
|
return -ENODEV;
|
2010-07-21 15:45:10 +08:00
|
|
|
|
2012-11-28 14:34:38 +08:00
|
|
|
keypad->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
|
|
|
|
if (!keypad->base)
|
|
|
|
return -EBUSY;
|
2010-07-21 15:45:10 +08:00
|
|
|
|
2012-11-28 14:34:38 +08:00
|
|
|
keypad->clk = devm_clk_get(&pdev->dev, "keypad");
|
2010-07-21 15:45:10 +08:00
|
|
|
if (IS_ERR(keypad->clk)) {
|
|
|
|
dev_err(&pdev->dev, "failed to get keypad clk\n");
|
2012-11-28 14:34:38 +08:00
|
|
|
return PTR_ERR(keypad->clk);
|
2010-07-21 15:45:10 +08:00
|
|
|
}
|
|
|
|
|
2012-10-04 15:03:49 +08:00
|
|
|
error = clk_prepare(keypad->clk);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&pdev->dev, "keypad clock prepare failed\n");
|
2012-11-28 14:34:38 +08:00
|
|
|
return error;
|
2012-10-04 15:03:49 +08:00
|
|
|
}
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
keypad->input_dev = input_dev;
|
2011-12-30 01:58:16 +08:00
|
|
|
keypad->pdev = pdev;
|
2010-07-21 15:45:10 +08:00
|
|
|
keypad->row_shift = row_shift;
|
|
|
|
keypad->rows = pdata->rows;
|
|
|
|
keypad->cols = pdata->cols;
|
2011-12-30 01:58:16 +08:00
|
|
|
keypad->stopped = true;
|
2010-07-21 15:45:10 +08:00
|
|
|
init_waitqueue_head(&keypad->wait);
|
|
|
|
|
2013-06-26 00:20:08 +08:00
|
|
|
if (pdev->dev.of_node)
|
2011-11-02 18:37:22 +08:00
|
|
|
keypad->type = of_device_is_compatible(pdev->dev.of_node,
|
|
|
|
"samsung,s5pv210-keypad");
|
2013-06-26 00:20:08 +08:00
|
|
|
else
|
2011-11-02 18:37:22 +08:00
|
|
|
keypad->type = platform_get_device_id(pdev)->driver_data;
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
input_dev->name = pdev->name;
|
|
|
|
input_dev->id.bustype = BUS_HOST;
|
|
|
|
input_dev->dev.parent = &pdev->dev;
|
|
|
|
|
|
|
|
input_dev->open = samsung_keypad_open;
|
|
|
|
input_dev->close = samsung_keypad_close;
|
|
|
|
|
2012-05-11 13:37:08 +08:00
|
|
|
error = matrix_keypad_build_keymap(keymap_data, NULL,
|
|
|
|
pdata->rows, pdata->cols,
|
|
|
|
keypad->keycodes, input_dev);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&pdev->dev, "failed to build keymap\n");
|
2012-10-04 15:03:49 +08:00
|
|
|
goto err_unprepare_clk;
|
2012-05-11 13:37:08 +08:00
|
|
|
}
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
input_set_capability(input_dev, EV_MSC, MSC_SCAN);
|
2012-05-11 13:37:08 +08:00
|
|
|
if (!pdata->no_autorepeat)
|
|
|
|
__set_bit(EV_REP, input_dev->evbit);
|
2010-07-21 15:45:10 +08:00
|
|
|
|
2012-05-11 13:37:08 +08:00
|
|
|
input_set_drvdata(input_dev, keypad);
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
keypad->irq = platform_get_irq(pdev, 0);
|
|
|
|
if (keypad->irq < 0) {
|
|
|
|
error = keypad->irq;
|
2012-11-28 14:34:38 +08:00
|
|
|
goto err_unprepare_clk;
|
2010-07-21 15:45:10 +08:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:34:38 +08:00
|
|
|
error = devm_request_threaded_irq(&pdev->dev, keypad->irq, NULL,
|
|
|
|
samsung_keypad_irq, IRQF_ONESHOT,
|
|
|
|
dev_name(&pdev->dev), keypad);
|
2010-07-21 15:45:10 +08:00
|
|
|
if (error) {
|
|
|
|
dev_err(&pdev->dev, "failed to register keypad interrupt\n");
|
2012-11-28 14:34:38 +08:00
|
|
|
goto err_unprepare_clk;
|
2010-07-21 15:45:10 +08:00
|
|
|
}
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
device_init_wakeup(&pdev->dev, pdata->wakeup);
|
|
|
|
platform_set_drvdata(pdev, keypad);
|
|
|
|
pm_runtime_enable(&pdev->dev);
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
error = input_register_device(keypad->input_dev);
|
|
|
|
if (error)
|
2012-11-28 14:34:38 +08:00
|
|
|
goto err_disable_runtime_pm;
|
2010-07-21 15:45:10 +08:00
|
|
|
|
2011-11-02 18:37:22 +08:00
|
|
|
if (pdev->dev.of_node) {
|
|
|
|
devm_kfree(&pdev->dev, (void *)pdata->keymap_data->keymap);
|
|
|
|
devm_kfree(&pdev->dev, (void *)pdata->keymap_data);
|
|
|
|
devm_kfree(&pdev->dev, (void *)pdata);
|
|
|
|
}
|
2010-07-21 15:45:10 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-11-28 14:34:38 +08:00
|
|
|
err_disable_runtime_pm:
|
2011-12-30 01:58:16 +08:00
|
|
|
pm_runtime_disable(&pdev->dev);
|
2012-10-04 15:03:49 +08:00
|
|
|
err_unprepare_clk:
|
|
|
|
clk_unprepare(keypad->clk);
|
2010-07-21 15:45:10 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-24 13:50:47 +08:00
|
|
|
static int samsung_keypad_remove(struct platform_device *pdev)
|
2010-07-21 15:45:10 +08:00
|
|
|
{
|
|
|
|
struct samsung_keypad *keypad = platform_get_drvdata(pdev);
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
pm_runtime_disable(&pdev->dev);
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
input_unregister_device(keypad->input_dev);
|
|
|
|
|
2012-10-04 15:03:49 +08:00
|
|
|
clk_unprepare(keypad->clk);
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
static int samsung_keypad_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct samsung_keypad *keypad = platform_get_drvdata(pdev);
|
|
|
|
unsigned int val;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (keypad->stopped)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* This may fail on some SoCs due to lack of controller support */
|
|
|
|
error = enable_irq_wake(keypad->irq);
|
|
|
|
if (!error)
|
|
|
|
keypad->wake_enabled = true;
|
|
|
|
|
|
|
|
val = readl(keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
val |= SAMSUNG_KEYIFCON_WAKEUPEN;
|
|
|
|
writel(val, keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
|
|
|
|
clk_disable(keypad->clk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int samsung_keypad_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct samsung_keypad *keypad = platform_get_drvdata(pdev);
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
if (keypad->stopped)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
clk_enable(keypad->clk);
|
|
|
|
|
|
|
|
val = readl(keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
|
|
|
|
writel(val, keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
|
|
|
|
if (keypad->wake_enabled)
|
|
|
|
disable_irq_wake(keypad->irq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-21 15:45:10 +08:00
|
|
|
static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
|
|
|
|
bool enable)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
clk_enable(keypad->clk);
|
|
|
|
|
|
|
|
val = readl(keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
if (enable) {
|
|
|
|
val |= SAMSUNG_KEYIFCON_WAKEUPEN;
|
2011-12-30 01:58:16 +08:00
|
|
|
if (device_may_wakeup(&keypad->pdev->dev))
|
2010-07-21 15:45:10 +08:00
|
|
|
enable_irq_wake(keypad->irq);
|
|
|
|
} else {
|
|
|
|
val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
|
2011-12-30 01:58:16 +08:00
|
|
|
if (device_may_wakeup(&keypad->pdev->dev))
|
2010-07-21 15:45:10 +08:00
|
|
|
disable_irq_wake(keypad->irq);
|
|
|
|
}
|
|
|
|
writel(val, keypad->base + SAMSUNG_KEYIFCON);
|
|
|
|
|
|
|
|
clk_disable(keypad->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int samsung_keypad_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct samsung_keypad *keypad = platform_get_drvdata(pdev);
|
|
|
|
struct input_dev *input_dev = keypad->input_dev;
|
|
|
|
|
|
|
|
mutex_lock(&input_dev->mutex);
|
|
|
|
|
2020-10-05 12:16:07 +08:00
|
|
|
if (input_device_enabled(input_dev))
|
2010-07-21 15:45:10 +08:00
|
|
|
samsung_keypad_stop(keypad);
|
|
|
|
|
|
|
|
samsung_keypad_toggle_wakeup(keypad, true);
|
|
|
|
|
|
|
|
mutex_unlock(&input_dev->mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int samsung_keypad_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct samsung_keypad *keypad = platform_get_drvdata(pdev);
|
|
|
|
struct input_dev *input_dev = keypad->input_dev;
|
|
|
|
|
|
|
|
mutex_lock(&input_dev->mutex);
|
|
|
|
|
|
|
|
samsung_keypad_toggle_wakeup(keypad, false);
|
|
|
|
|
2020-10-05 12:16:07 +08:00
|
|
|
if (input_device_enabled(input_dev))
|
2010-07-21 15:45:10 +08:00
|
|
|
samsung_keypad_start(keypad);
|
|
|
|
|
|
|
|
mutex_unlock(&input_dev->mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-30 01:58:16 +08:00
|
|
|
static const struct dev_pm_ops samsung_keypad_pm_ops = {
|
2023-01-15 01:16:07 +08:00
|
|
|
SYSTEM_SLEEP_PM_OPS(samsung_keypad_suspend, samsung_keypad_resume)
|
|
|
|
RUNTIME_PM_OPS(samsung_keypad_runtime_suspend,
|
|
|
|
samsung_keypad_runtime_resume, NULL)
|
2011-12-30 01:58:16 +08:00
|
|
|
};
|
2011-11-08 15:59:35 +08:00
|
|
|
|
2011-11-02 18:37:22 +08:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static const struct of_device_id samsung_keypad_dt_match[] = {
|
|
|
|
{ .compatible = "samsung,s3c6410-keypad" },
|
|
|
|
{ .compatible = "samsung,s5pv210-keypad" },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, samsung_keypad_dt_match);
|
|
|
|
#endif
|
|
|
|
|
2015-05-07 07:54:56 +08:00
|
|
|
static const struct platform_device_id samsung_keypad_driver_ids[] = {
|
2010-07-21 15:45:10 +08:00
|
|
|
{
|
|
|
|
.name = "samsung-keypad",
|
|
|
|
.driver_data = KEYPAD_TYPE_SAMSUNG,
|
|
|
|
}, {
|
|
|
|
.name = "s5pv210-keypad",
|
|
|
|
.driver_data = KEYPAD_TYPE_S5PV210,
|
|
|
|
},
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
|
|
|
|
|
|
|
|
static struct platform_driver samsung_keypad_driver = {
|
|
|
|
.probe = samsung_keypad_probe,
|
2012-11-24 13:27:39 +08:00
|
|
|
.remove = samsung_keypad_remove,
|
2010-07-21 15:45:10 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "samsung-keypad",
|
2012-08-22 13:04:32 +08:00
|
|
|
.of_match_table = of_match_ptr(samsung_keypad_dt_match),
|
2023-01-15 01:16:07 +08:00
|
|
|
.pm = pm_ptr(&samsung_keypad_pm_ops),
|
2010-07-21 15:45:10 +08:00
|
|
|
},
|
|
|
|
.id_table = samsung_keypad_driver_ids,
|
|
|
|
};
|
2011-11-30 03:08:39 +08:00
|
|
|
module_platform_driver(samsung_keypad_driver);
|
2010-07-21 15:45:10 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Samsung keypad driver");
|
|
|
|
MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
|
|
|
|
MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|