2019-05-27 13:55:01 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-03-28 23:37:14 +07:00
|
|
|
/*
|
|
|
|
* Cirrus Logic CLPS711X Keypad driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of_gpio.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/regmap.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/input/matrix_keypad.h>
|
|
|
|
#include <linux/mfd/syscon.h>
|
|
|
|
#include <linux/mfd/syscon/clps711x.h>
|
|
|
|
|
|
|
|
#define CLPS711X_KEYPAD_COL_COUNT 8
|
|
|
|
|
|
|
|
struct clps711x_gpio_data {
|
|
|
|
struct gpio_desc *desc;
|
|
|
|
DECLARE_BITMAP(last_state, CLPS711X_KEYPAD_COL_COUNT);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct clps711x_keypad_data {
|
|
|
|
struct regmap *syscon;
|
|
|
|
int row_count;
|
|
|
|
unsigned int row_shift;
|
|
|
|
struct clps711x_gpio_data *gpio_data;
|
|
|
|
};
|
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
static void clps711x_keypad_poll(struct input_dev *input)
|
2014-03-28 23:37:14 +07:00
|
|
|
{
|
2019-10-30 06:54:37 +07:00
|
|
|
const unsigned short *keycodes = input->keycode;
|
|
|
|
struct clps711x_keypad_data *priv = input_get_drvdata(input);
|
2014-03-28 23:37:14 +07:00
|
|
|
bool sync = false;
|
|
|
|
int col, row;
|
|
|
|
|
|
|
|
for (col = 0; col < CLPS711X_KEYPAD_COL_COUNT; col++) {
|
|
|
|
/* Assert column */
|
|
|
|
regmap_update_bits(priv->syscon, SYSCON_OFFSET,
|
|
|
|
SYSCON1_KBDSCAN_MASK,
|
|
|
|
SYSCON1_KBDSCAN(8 + col));
|
|
|
|
|
|
|
|
/* Scan rows */
|
|
|
|
for (row = 0; row < priv->row_count; row++) {
|
|
|
|
struct clps711x_gpio_data *data = &priv->gpio_data[row];
|
|
|
|
bool state, state1;
|
|
|
|
|
|
|
|
/* Read twice for protection against fluctuations */
|
|
|
|
do {
|
|
|
|
state = gpiod_get_value_cansleep(data->desc);
|
|
|
|
cond_resched();
|
|
|
|
state1 = gpiod_get_value_cansleep(data->desc);
|
|
|
|
} while (state != state1);
|
|
|
|
|
|
|
|
if (test_bit(col, data->last_state) != state) {
|
|
|
|
int code = MATRIX_SCAN_CODE(row, col,
|
|
|
|
priv->row_shift);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
set_bit(col, data->last_state);
|
2019-10-30 06:54:37 +07:00
|
|
|
input_event(input,
|
|
|
|
EV_MSC, MSC_SCAN, code);
|
2014-03-28 23:37:14 +07:00
|
|
|
} else {
|
|
|
|
clear_bit(col, data->last_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keycodes[code])
|
2019-10-30 06:54:37 +07:00
|
|
|
input_report_key(input,
|
2014-03-28 23:37:14 +07:00
|
|
|
keycodes[code], state);
|
|
|
|
sync = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set all columns to low */
|
|
|
|
regmap_update_bits(priv->syscon, SYSCON_OFFSET,
|
|
|
|
SYSCON1_KBDSCAN_MASK, SYSCON1_KBDSCAN(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sync)
|
2019-10-30 06:54:37 +07:00
|
|
|
input_sync(input);
|
2014-03-28 23:37:14 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int clps711x_keypad_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct clps711x_keypad_data *priv;
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct device_node *np = dev->of_node;
|
2019-10-30 06:54:37 +07:00
|
|
|
struct input_dev *input;
|
2014-03-28 23:37:14 +07:00
|
|
|
u32 poll_interval;
|
|
|
|
int i, err;
|
|
|
|
|
|
|
|
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
|
|
|
|
if (!priv)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
priv->syscon =
|
2016-06-04 14:10:01 +07:00
|
|
|
syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon1");
|
2014-03-28 23:37:14 +07:00
|
|
|
if (IS_ERR(priv->syscon))
|
|
|
|
return PTR_ERR(priv->syscon);
|
|
|
|
|
|
|
|
priv->row_count = of_gpio_named_count(np, "row-gpios");
|
|
|
|
if (priv->row_count < 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
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 04:07:58 +07:00
|
|
|
priv->gpio_data = devm_kcalloc(dev,
|
|
|
|
priv->row_count, sizeof(*priv->gpio_data),
|
2014-03-28 23:37:14 +07:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!priv->gpio_data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
priv->row_shift = get_count_order(CLPS711X_KEYPAD_COL_COUNT);
|
|
|
|
|
|
|
|
for (i = 0; i < priv->row_count; i++) {
|
|
|
|
struct clps711x_gpio_data *data = &priv->gpio_data[i];
|
|
|
|
|
2015-06-17 07:02:13 +07:00
|
|
|
data->desc = devm_gpiod_get_index(dev, "row", i, GPIOD_IN);
|
2014-03-28 23:37:14 +07:00
|
|
|
if (IS_ERR(data->desc))
|
|
|
|
return PTR_ERR(data->desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = of_property_read_u32(np, "poll-interval", &poll_interval);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
input = devm_input_allocate_device(dev);
|
|
|
|
if (!input)
|
2014-03-28 23:37:14 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
input_set_drvdata(input, priv);
|
|
|
|
|
|
|
|
input->name = pdev->name;
|
|
|
|
input->dev.parent = dev;
|
|
|
|
input->id.bustype = BUS_HOST;
|
|
|
|
input->id.vendor = 0x0001;
|
|
|
|
input->id.product = 0x0001;
|
|
|
|
input->id.version = 0x0100;
|
2014-03-28 23:37:14 +07:00
|
|
|
|
|
|
|
err = matrix_keypad_build_keymap(NULL, NULL, priv->row_count,
|
|
|
|
CLPS711X_KEYPAD_COL_COUNT,
|
2019-10-30 06:54:37 +07:00
|
|
|
NULL, input);
|
2014-03-28 23:37:14 +07:00
|
|
|
if (err)
|
2019-10-30 06:54:37 +07:00
|
|
|
return err;
|
2014-03-28 23:37:14 +07:00
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
input_set_capability(input, EV_MSC, MSC_SCAN);
|
2014-03-28 23:37:14 +07:00
|
|
|
if (of_property_read_bool(np, "autorepeat"))
|
2019-10-30 06:54:37 +07:00
|
|
|
__set_bit(EV_REP, input->evbit);
|
2014-03-28 23:37:14 +07:00
|
|
|
|
|
|
|
/* Set all columns to low */
|
|
|
|
regmap_update_bits(priv->syscon, SYSCON_OFFSET, SYSCON1_KBDSCAN_MASK,
|
|
|
|
SYSCON1_KBDSCAN(1));
|
|
|
|
|
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
err = input_setup_polling(input, clps711x_keypad_poll);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2014-03-28 23:37:14 +07:00
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
input_set_poll_interval(input, poll_interval);
|
2014-03-28 23:37:14 +07:00
|
|
|
|
2019-10-30 06:54:37 +07:00
|
|
|
err = input_register_device(input);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2014-03-28 23:37:14 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 02:57:31 +07:00
|
|
|
static const struct of_device_id clps711x_keypad_of_match[] = {
|
2016-06-04 14:10:01 +07:00
|
|
|
{ .compatible = "cirrus,ep7209-keypad", },
|
2014-03-28 23:37:14 +07:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, clps711x_keypad_of_match);
|
|
|
|
|
|
|
|
static struct platform_driver clps711x_keypad_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "clps711x-keypad",
|
|
|
|
.of_match_table = clps711x_keypad_of_match,
|
|
|
|
},
|
|
|
|
.probe = clps711x_keypad_probe,
|
|
|
|
};
|
|
|
|
module_platform_driver(clps711x_keypad_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
|
|
|
|
MODULE_DESCRIPTION("Cirrus Logic CLPS711X Keypad driver");
|
|
|
|
MODULE_LICENSE("GPL");
|