Pine64 PinePhone keyboard support
Subsystems (5)
Samuel Holland
maintainerv4 cover Pine64 PinePhone keyboard support
This series adds support for the official keyboard case for the Pine64 PinePhone and PinePhone Pro. This accessory contains a keyboard MCU and an IP5209 power bank IC. The keyboard MCU firmware[0] is free software. It exposes the keyboard scan matrix over I2C, and also provides commands for SMBus access to the IP5209. In order to keep the IP5209 driver (CONFIG_IP5XXX_POWER) generic, this is modeled as a child I2C bus.
[0]: https://megous.com/git/pinephone-keyboard/about/
Changes in v4:
- Rebase to resolve MAINTAINERS merge conflict
- Add missing newlines in error messages
Changes in v3:
- Replace unevaluatedProperties with additionalProperties
- Rename i2c-bus to i2c
- Rename i2c-bus to i2c
Changes in v2:
- Drop keymap DT properties
- Add vbat-supply property
- Fix missing key release events when FN state changes
- Add VBAT consumer to ensure enough power is available for the MCU
- Use a single fixed-size, fixed-contents keymap for both layers
Samuel Holland (4):
dt-bindings: input: Add the PinePhone keyboard binding
Input: pinephone-keyboard - Add PinePhone keyboard driver
Input: pinephone-keyboard - Support the proxied I2C bus [DO NOT MERGE] arm64: dts: allwinner: pinephone: Add keyboard
.../input/pine64,pinephone-keyboard.yaml | 66 +++ MAINTAINERS | 6 + .../dts/allwinner/sun50i-a64-pinephone.dtsi | 18 + drivers/input/keyboard/Kconfig | 10 + drivers/input/keyboard/Makefile | 1 + drivers/input/keyboard/pinephone-keyboard.c | 438 ++++++++++++++++++ 6 files changed, 539 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/pine64,pinephone-keyboard.yaml create mode 100644 drivers/input/keyboard/pinephone-keyboard.c
Signature · 1 line
2.35.1
Samuel Holland
maintainerPATCH v4 1/4 dt-bindings: input: Add the PinePhone keyboard binding
Add devicetree support for the PinePhone keyboard case, which provides a matrix keyboard interface and a proxied I2C bus.
---
(no changes since v3)
Changes in v3:
- Replace unevaluatedProperties with additionalProperties
- Rename i2c-bus to i2c
Changes in v2:
- Drop keymap DT properties
- Add vbat-supply property
.../input/pine64,pinephone-keyboard.yaml | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/pine64,pinephone-keyboard.yaml
Documentation/devicetree/bindings/input/pine64,pinephone-keyboard.yaml new file +66 -0
@@ -0,0 +1,66 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/input/pine64,pinephone-keyboard.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Pine64 PinePhone keyboard device tree bindings + +maintainers: + - Samuel Holland <[email protected]> + +description: + A keyboard accessory is available for the Pine64 PinePhone and PinePhone Pro. + It connects via I2C, providing a raw scan matrix, a flashing interface, and a + subordinate I2C bus for communication with a battery charger IC. + +properties: + compatible: + const: pine64,pinephone-keyboard + + reg: + const: 0x15 + + interrupts: + maxItems: 1 + + vbat-supply: + description: Supply for the keyboard MCU + + wakeup-source: true + + i2c: + $ref: /schemas/i2c/i2c-controller.yaml# + +required: + - compatible + - reg + - interrupts + +additionalProperties: false + +examples: + - | + #include <dt-bindings/input/input.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + keyboard@15 { + compatible = "pine64,pinephone-keyboard"; + reg = <0x15>; + interrupt-parent = <&r_pio>; + interrupts = <0 12 IRQ_TYPE_EDGE_FALLING>; /* PL12 */ + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + charger@75 { + reg = <0x75>; + }; + }; + }; + };
Signature · 1 line
2.35.1
Samuel Holland
maintainerPATCH v4 2/4 Input: pinephone-keyboard - Add PinePhone keyboard driver
In reply to Samuel HollandThe official Pine64 PinePhone keyboard case contains a matrix keypad and a MCU which runs a libre firmware. Add support for its I2C interface.
---
Changes in v4:
- Rebase to resolve MAINTAINERS merge conflict
- Add missing newlines in error messages
Changes in v2:
- Fix missing key release events when FN state changes
- Add VBAT consumer to ensure enough power is available for the MCU
- Use a single fixed-size, fixed-contents keymap for both layers
MAINTAINERS | 6 + drivers/input/keyboard/Kconfig | 10 + drivers/input/keyboard/Makefile | 1 + drivers/input/keyboard/pinephone-keyboard.c | 365 ++++++++++++++++++++ 4 files changed, 382 insertions(+) create mode 100644 drivers/input/keyboard/pinephone-keyboard.c
MAINTAINERS +6 -0
@@ -15820,6 +15820,12 @@ F: Documentation/devicetree/bindings/pinctrl/sunplus,* F: drivers/pinctrl/sunplus/ F: include/dt-bindings/pinctrl/sppctl*.h +PINE64 PINEPHONE KEYBOARD DRIVER +M: Samuel Holland <[email protected]> +S: Supported +F: Documentation/devicetree/bindings/input/pine64,pinephone-keyboard.yaml +F: drivers/input/keyboard/pinephone-keyboard.c + PKTCDVD DRIVER M: [email protected] S: Orphan
drivers/input/keyboard/Kconfig +10 -0
@@ -524,6 +524,16 @@ config KEYBOARD_OPENCORES To compile this driver as a module, choose M here; the module will be called opencores-kbd. +config KEYBOARD_PINEPHONE + tristate "Pine64 PinePhone Keyboard" + depends on I2C && REGULATOR + select CRC8 + select INPUT_MATRIXKMAP + help + Say Y here to enable support for the keyboard in the Pine64 PinePhone + keyboard case. This driver supports the FLOSS firmware available at + https://megous.com/git/pinephone-keyboard/ + config KEYBOARD_PXA27x tristate "PXA27x/PXA3xx keypad support" depends on PXA27x || PXA3xx || ARCH_MMP
drivers/input/keyboard/Makefile +1 -0
@@ -52,6 +52,7 @@ obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o obj-$(CONFIG_KEYBOARD_OMAP4) += omap4-keypad.o obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o +obj-$(CONFIG_KEYBOARD_PINEPHONE) += pinephone-keyboard.o obj-$(CONFIG_KEYBOARD_PMIC8XXX) += pmic8xxx-keypad.o obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
drivers/input/keyboard/pinephone-keyboard.c new file +365 -0
365 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH v4 2/4] Input: pinephone-keyboard - Add PinePhone keyboard driver
Quoted 3 lines · Samuel Holland
The official Pine64 PinePhone keyboard case contains a matrix keypad and a MCU which runs a libre firmware. Add support for its I2C interface.
...
drivers/input/keyboard/pinephone-keyboard.c line 10
Quoted 6 lines
drivers/input/keyboard/pinephone-keyboard.c new file +6
-0+#include <linux/crc8.h> +#include <linux/i2c.h> +#include <linux/input/matrix_keypad.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/regulator/consumer.h>
Missedtypes.h
...
Quoted 2 lines
drivers/input/keyboard/pinephone-keyboard.c new file +2
-0+#define PPKB_ROWS 6 +#define PPKB_COLS 12
...
Quoted 14 lines
drivers/input/keyboard/pinephone-keyboard.c new file +14
-0+for (col =0 ; col <PPKB_COLS ; ++col) { +u8 old = old_buf[1 + col]; +u8 new = new_buf[1 + col]; +u8 changed = old ^ new; + +if (!changed) +continue ; + +for (row =0 ; row <PPKB_ROWS ; ++row) { +u8 mask =BIT (row); +u8 value = new & mask; +unsigned short code; +bool fn_state; +
drivers/input/keyboard/pinephone-keyboard.c line 191
Quoted 10 lines
drivers/input/keyboard/pinephone-keyboard.c new file +10
-0+if (!(changed & mask)) +continue ; + +/* +* Save off the FN key state when the key was pressed, +* and use that to determine the code during a release. +*/ + fn_state = value ? ppkb->fn_pressed : ppkb->fn_state[col] & mask; +if (fn_state) + ppkb->fn_state[col] ^= mask;
Can't it be converted to use bitmap APIs?
Quoted 2 lines
(quoted diff) +2
-0+ } + }
...
Quoted 13 lines
drivers/input/keyboard/pinephone-keyboard.c new file +13
-0+static int ppkb_set_scan (struct i2c_client *client,bool enable) +{ +struct device *dev = &client->dev; +int ret, val; + + ret =i2c_smbus_read_byte_data (client,PPKB_SYS_CONFIG ); +if (ret <0 ) { +dev_err (dev,"Failed to read config: %d\n" , ret); +return ret; + } + +if (enable) + val = ret & ~PPKB_SYS_CONFIG_DISABLE_SCAN ;
drivers/input/keyboard/pinephone-keyboard.c line 235
Quoted 10 lines
drivers/input/keyboard/pinephone-keyboard.c new file +9
-0+else + val = ret |PPKB_SYS_CONFIG_DISABLE_SCAN ; + ret =i2c_smbus_write_byte_data (client,PPKB_SYS_CONFIG , val); +if (ret) { +dev_err (dev,"Failed to write config: %d\n" , ret); +return ret; + } + +return 0 ;
return ret;
Quoted 1 line
+}
...
drivers/input/keyboard/pinephone-keyboard.c line 247
Quoted 11 lines
drivers/input/keyboard/pinephone-keyboard.c new file +10
-0+static int ppkb_open (struct input_dev *input) +{ +struct i2c_client *client =input_get_drvdata (input); +int ret; + + ret =ppkb_set_scan (client, true); +if (ret) +return ret; + +return 0 ;
return ppkb_set_scan(...);
Quoted 1 line
+}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Samuel Holland
maintainerRe: [PATCH v4 2/4] Input: pinephone-keyboard - Add PinePhone keyboard driver
Quoted 42 lines · Andy Shevchenko
Quoted 3 lines · Samuel Holland
The official Pine64 PinePhone keyboard case contains a matrix keypad and a MCU which runs a libre firmware. Add support for its I2C interface.
...
drivers/input/keyboard/pinephone-keyboard.c line 10
Quoted 6 lines
drivers/input/keyboard/pinephone-keyboard.c new file +6
-0+#include <linux/crc8.h> +#include <linux/i2c.h> +#include <linux/input/matrix_keypad.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/regulator/consumer.h> Missed
types.h...
Quoted 2 lines
drivers/input/keyboard/pinephone-keyboard.c new file +2
-0+#define PPKB_ROWS 6 +#define PPKB_COLS 12...
Quoted 18 lines
drivers/input/keyboard/pinephone-keyboard.c new file +18
-0+for (col =0 ; col <PPKB_COLS ; ++col) { +u8 old = old_buf[1 + col]; +u8 new = new_buf[1 + col]; +u8 changed = old ^ new; + +if (!changed) +continue ; + +for (row =0 ; row <PPKB_ROWS ; ++row) { +u8 mask =BIT (row); +u8 value = new & mask; +unsigned short code; +bool fn_state; + +if (!(changed & mask)) +continue ; + +/*
Quoted 8 lines
drivers/input/keyboard/pinephone-keyboard.c line 191
Quoted 6 lines
drivers/input/keyboard/pinephone-keyboard.c new file +6
-0+* Save off the FN key state when the key was pressed, +* and use that to determine the code during a release. +*/ + fn_state = value ? ppkb->fn_pressed : ppkb->fn_state[col] & mask; +if (fn_state) + ppkb->fn_state[col] ^= mask;Can't it be converted to use bitmap APIs?
This is a 2D matrix, with one byte per column, and one bit per row. There are only 6 rows, so two bits per byte are unused. Converting this to the bitmap API would unnecessarily complicate things.
Quoted 24 lines
Quoted 2 lines
(quoted diff) +2
-0+ } + }...
Quoted 18 lines
drivers/input/keyboard/pinephone-keyboard.c new file +18
-0+static int ppkb_set_scan (struct i2c_client *client,bool enable) +{ +struct device *dev = &client->dev; +int ret, val; + + ret =i2c_smbus_read_byte_data (client,PPKB_SYS_CONFIG ); +if (ret <0 ) { +dev_err (dev,"Failed to read config: %d\n" , ret); +return ret; + } + +if (enable) + val = ret & ~PPKB_SYS_CONFIG_DISABLE_SCAN ; +else + val = ret |PPKB_SYS_CONFIG_DISABLE_SCAN ; + ret =i2c_smbus_write_byte_data (client,PPKB_SYS_CONFIG , val); +if (ret) { +dev_err (dev,"Failed to write config: %d\n" , ret);
Quoted 6 lines
drivers/input/keyboard/pinephone-keyboard.c line 235
Quoted 4 lines
drivers/input/keyboard/pinephone-keyboard.c new file +4
-0+return ret; + } + +return 0 ;return ret;
The "return 0" pattern is idiomatic, and more diff-friendly when adding error handling or more operations. But I don't have that strong of an opinion on it.
Regards,
Samuel
Dmitry Torokhov
maintainerRe: [PATCH v4 2/4] Input: pinephone-keyboard - Add PinePhone keyboard driver
Quoted 52 lines · Samuel Holland
Quoted 42 lines · Andy Shevchenko
Quoted 3 lines · Samuel Holland
The official Pine64 PinePhone keyboard case contains a matrix keypad and a MCU which runs a libre firmware. Add support for its I2C interface.
...
drivers/input/keyboard/pinephone-keyboard.c line 10
Quoted 6 lines
drivers/input/keyboard/pinephone-keyboard.c new file +6
-0+#include <linux/crc8.h> +#include <linux/i2c.h> +#include <linux/input/matrix_keypad.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/regulator/consumer.h> Missed
types.h...
Quoted 2 lines
drivers/input/keyboard/pinephone-keyboard.c new file +2
-0+#define PPKB_ROWS 6 +#define PPKB_COLS 12...
Quoted 18 lines
drivers/input/keyboard/pinephone-keyboard.c new file +18
-0+for (col =0 ; col <PPKB_COLS ; ++col) { +u8 old = old_buf[1 + col]; +u8 new = new_buf[1 + col]; +u8 changed = old ^ new; + +if (!changed) +continue ; + +for (row =0 ; row <PPKB_ROWS ; ++row) { +u8 mask =BIT (row); +u8 value = new & mask; +unsigned short code; +bool fn_state; + +if (!(changed & mask)) +continue ; + +/* Quoted 8 lines
drivers/input/keyboard/pinephone-keyboard.c line 191
Quoted 6 lines
drivers/input/keyboard/pinephone-keyboard.c new file +6
-0+* Save off the FN key state when the key was pressed, +* and use that to determine the code during a release. +*/ + fn_state = value ? ppkb->fn_pressed : ppkb->fn_state[col] & mask; +if (fn_state) + ppkb->fn_state[col] ^= mask;Can't it be converted to use bitmap APIs?
Quoted 3 lines
This is a 2D matrix, with one byte per column, and one bit per row. There are only 6 rows, so two bits per byte are unused. Converting this to the bitmap API would unnecessarily complicate things.
We could have a constant bitmap masking the unused bits so they are always skipped.
Anyway, I made a few edits and applied, we can continue improving the driver in an iterative manner.
Thanks.
Signature · 1 line
Dmitry
Samuel Holland
maintainerPATCH v4 3/4 Input: pinephone-keyboard - Support the proxied I2C bus
In reply to Samuel HollandThe PinePhone keyboard case contains a battery managed by an integrated power bank IC. The power bank IC communicates over I2C, and the keyboard MCU firmware provides an interface to read and write its registers. Let's use this interface to implement a SMBus adapter, so we can reuse the driver for the power bank IC.
---
(no changes since v3)
Changes in v3:
- Rename i2c-bus to i2c
drivers/input/keyboard/pinephone-keyboard.c | 73 +++++++++++++++++++++ 1 file changed, 73 insertions(+)
drivers/input/keyboard/pinephone-keyboard.c +73 -0
@@ -3,6 +3,7 @@// Copyright (C) 2021-2022 Samuel Holland <[email protected]> #include <linux/crc8.h> +#include <linux/delay.h> #include <linux/i2c.h> #include <linux/input/matrix_keypad.h> #include <linux/interrupt.h>
@@ -24,6 +25,11 @@#define PPKB_SCAN_DATA 0x08#define PPKB_SYS_CONFIG 0x20#define PPKB_SYS_CONFIG_DISABLE_SCAN BIT(0) +#define PPKB_SYS_SMBUS_COMMAND 0x21 +#define PPKB_SYS_SMBUS_DATA 0x22 +#define PPKB_SYS_COMMAND 0x23 +#define PPKB_SYS_COMMAND_SMBUS_READ 0x91 +#define PPKB_SYS_COMMAND_SMBUS_WRITE 0xa1#define PPKB_ROWS 6#define PPKB_COLS 12
@@ -132,6 +138,7 @@ static const struct matrix_keymap_data ppkb_keymap_data = { };struct pinephone_keyboard { +struct i2c_adapter adapter;struct input_dev *input;u8 buf[2 ][PPKB_BUF_LEN ];u8 crc_table[CRC8_TABLE_SIZE ];
@@ -140,6 +147,57 @@ struct pinephone_keyboard {bool fn_pressed; }; +static int ppkb_adap_smbus_xfer (struct i2c_adapter *adap,u16 addr, +unsigned short flags,char read_write, +u8 command,int size, +union i2c_smbus_data *data) +{ +struct i2c_client *client = adap->algo_data; +u8 buf[3 ]; +int ret; + + buf[0 ] = command; + buf[1 ] = data->byte; + buf[2 ] = read_write ==I2C_SMBUS_READ ?PPKB_SYS_COMMAND_SMBUS_READ + :PPKB_SYS_COMMAND_SMBUS_WRITE ; + + ret =i2c_smbus_write_i2c_block_data (client,PPKB_SYS_SMBUS_COMMAND , +sizeof (buf), buf); +if (ret) +return ret; + +/* Read back the command status until it passes or fails. */ +do { +usleep_range (300 ,500 ); + ret =i2c_smbus_read_byte_data (client,PPKB_SYS_COMMAND ); + }while (ret == buf[2 ]); +if (ret <0 ) +return ret; +/* Commands return 0x00 on success and 0xff on failure. */ +if (ret) +return -EIO ; + +if (read_write ==I2C_SMBUS_READ ) { + ret =i2c_smbus_read_byte_data (client,PPKB_SYS_SMBUS_DATA ); +if (ret <0 ) +return ret; + + data->byte = ret; + } + +return 0 ; +} + +static u32 ppkg_adap_functionality (struct i2c_adapter *adap) +{ +return I2C_FUNC_SMBUS_BYTE_DATA ; +} + +static const struct i2c_algorithm ppkb_adap_algo = { + .smbus_xfer = ppkb_adap_smbus_xfer, + .functionality = ppkg_adap_functionality, +}; +static void ppkb_update (struct i2c_client *client) {struct pinephone_keyboard *ppkb =i2c_get_clientdata (client);
@@ -266,6 +324,7 @@ static int ppkb_probe(struct i2c_client *client)struct pinephone_keyboard *ppkb;struct regulator *vbat_supply;u8 info[PPKB_MATRIX_SIZE +1 ]; +struct device_node *i2c_bus;int ret; vbat_supply =devm_regulator_get (dev,"vbat" );
@@ -311,6 +370,20 @@ static int ppkb_probe(struct i2c_client *client)i2c_set_clientdata (client, ppkb); + i2c_bus =of_get_child_by_name (dev->of_node,"i2c" ); +if (i2c_bus) { + ppkb->adapter.owner =THIS_MODULE ; + ppkb->adapter.algo = &ppkb_adap_algo; + ppkb->adapter.algo_data = client; + ppkb->adapter.dev.parent = dev; + ppkb->adapter.dev.of_node = i2c_bus; +strscpy (ppkb->adapter.name,DRV_NAME ,sizeof (ppkb->adapter.name)); + + ret =devm_i2c_add_adapter (dev, &ppkb->adapter); +if (ret) +return dev_err_probe (dev, ret,"Failed to add I2C adapter\n" ); + } +crc8_populate_msb (ppkb->crc_table,PPKB_CRC8_POLYNOMIAL ); ppkb->input =devm_input_allocate_device (dev);
Signature · 1 line
2.35.1
Re: [PATCH v4 3/4] Input: pinephone-keyboard - Support the proxied I2C bus
Quoted 6 lines · Samuel Holland
The PinePhone keyboard case contains a battery managed by an integrated power bank IC. The power bank IC communicates over I2C, and the keyboard MCU firmware provides an interface to read and write its registers. Let's use this interface to implement a SMBus adapter, so we can reuse the driver for the power bank IC.
...
drivers/input/keyboard/pinephone-keyboard.c struct pinephone_keyboard { · line 178
Quoted 10 lines
drivers/input/keyboard/pinephone-keyboard.c +10
-0+/* Read back the command status until it passes or fails. */ +do { +usleep_range (300 ,500 ); + ret =i2c_smbus_read_byte_data (client,PPKB_SYS_COMMAND ); + }while (ret == buf[2 ]); +if (ret <0 ) +return ret; +/* Commands return 0x00 on success and 0xff on failure. */ +if (ret) +return -EIO ;
Something to use from iopoll.h ?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Samuel Holland
maintainerPATCH v4 4/4 arm64: dts: allwinner: pinephone: Add keyboard
In reply to Samuel HollandThe official PinePhone keyboard accessory connects to the phone's POGO pins for I2C and interrupts. It has an Injoinic IP5209 power bank IC connected to the keyboard's internal I2C bus.
---
(no changes since v3)
Changes in v3:
- Rename i2c-bus to i2c
.../dts/allwinner/sun50i-a64-pinephone.dtsi | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/arm64/boot/dts/allwinner/sun50i-a64-pinephone.dtsi +18 -0
@@ -208,6 +208,24 @@ accelerometer@68 { /* Connected to pogo pins (external spring based pinheader for user addons) */ &i2c2 { status = "okay"; + + keyboard@15 { + compatible = "pine64,pinephone-keyboard"; + reg = <0x15>; + interrupt-parent = <&r_pio>; + interrupts = <0 12 IRQ_TYPE_EDGE_FALLING>; /* PL12 */ + wakeup-source; + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + charger@75 { + compatible = "injoinic,ip5209"; + reg = <0x75>; + }; + }; + }; }; &lradc {
Signature · 1 line
2.35.1