^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2012-2016 Synaptics Incorporated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/rmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "rmi_driver.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define RMI_F30_QUERY_SIZE 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /* Defs for Query 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define RMI_F30_EXTENDED_PATTERNS 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define RMI_F30_HAS_MAPPABLE_BUTTONS BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define RMI_F30_HAS_LED BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define RMI_F30_HAS_GPIO BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define RMI_F30_HAS_HAPTIC BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define RMI_F30_HAS_GPIO_DRV_CTL BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define RMI_F30_HAS_MECH_MOUSE_BTNS BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Defs for Query 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define RMI_F30_GPIO_LED_COUNT 0x1F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Defs for Control Registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define RMI_F30_CTRL_1_GPIO_DEBOUNCE 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define RMI_F30_CTRL_1_HALT BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define RMI_F30_CTRL_1_HALTED BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define RMI_F30_CTRL_10_NUM_MECH_MOUSE_BTNS 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define RMI_F30_CTRL_MAX_REGS 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define RMI_F30_CTRL_MAX_BYTES DIV_ROUND_UP(RMI_F30_CTRL_MAX_REGS, 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define RMI_F30_CTRL_MAX_REG_BLOCKS 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define RMI_F30_CTRL_REGS_MAX_SIZE (RMI_F30_CTRL_MAX_BYTES \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) + 1 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) + RMI_F30_CTRL_MAX_BYTES \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) + RMI_F30_CTRL_MAX_BYTES \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) + RMI_F30_CTRL_MAX_BYTES \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) + 6 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) + RMI_F30_CTRL_MAX_REGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) + RMI_F30_CTRL_MAX_REGS \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) + RMI_F30_CTRL_MAX_BYTES \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) + 1 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define TRACKSTICK_RANGE_START 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define TRACKSTICK_RANGE_END 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct rmi_f30_ctrl_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u8 *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct f30_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Query Data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) bool has_extended_pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bool has_mappable_buttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) bool has_led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) bool has_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) bool has_haptic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) bool has_gpio_driver_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) bool has_mech_mouse_btns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) u8 gpioled_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 register_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Control Register Data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct rmi_f30_ctrl_data ctrl[RMI_F30_CTRL_MAX_REG_BLOCKS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u8 ctrl_regs[RMI_F30_CTRL_REGS_MAX_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u32 ctrl_regs_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u8 data_regs[RMI_F30_CTRL_MAX_BYTES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u16 *gpioled_key_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct rmi_function *f03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bool trackstick_buttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int rmi_f30_read_control_parameters(struct rmi_function *fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct f30_data *f30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) error = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) f30->ctrl_regs, f30->ctrl_regs_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dev_err(&fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) "%s: Could not read control registers at 0x%x: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __func__, fn->fd.control_base_addr, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void rmi_f30_report_button(struct rmi_function *fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct f30_data *f30, unsigned int button)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned int reg_num = button >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned int bit_num = button & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) u16 key_code = f30->gpioled_key_map[button];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) bool key_down = !(f30->data_regs[reg_num] & BIT(bit_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (f30->trackstick_buttons &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) button >= TRACKSTICK_RANGE_START &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) button <= TRACKSTICK_RANGE_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) rmi_f03_overwrite_button(f30->f03, key_code, key_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) rmi_dbg(RMI_DEBUG_FN, &fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "%s: call input report key (0x%04x) value (0x%02x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) __func__, key_code, key_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) input_report_key(f30->input, key_code, key_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static irqreturn_t rmi_f30_attention(int irq, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct rmi_function *fn = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct f30_data *f30 = dev_get_drvdata(&fn->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct rmi_driver_data *drvdata = dev_get_drvdata(&fn->rmi_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Read the gpi led data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (drvdata->attn_data.data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (drvdata->attn_data.size < f30->register_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) dev_warn(&fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) "F30 interrupted, but data is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) memcpy(f30->data_regs, drvdata->attn_data.data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) f30->register_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) drvdata->attn_data.data += f30->register_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) drvdata->attn_data.size -= f30->register_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) f30->data_regs, f30->register_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev_err(&fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) "%s: Failed to read F30 data registers: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return IRQ_RETVAL(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (f30->has_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) for (i = 0; i < f30->gpioled_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (f30->gpioled_key_map[i] != KEY_RESERVED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) rmi_f30_report_button(fn, f30, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (f30->trackstick_buttons)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rmi_f03_commit_buttons(f30->f03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int rmi_f30_config(struct rmi_function *fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct f30_data *f30 = dev_get_drvdata(&fn->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct rmi_driver *drv = fn->rmi_dev->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) const struct rmi_device_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) rmi_get_platform_data(fn->rmi_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* can happen if gpio_data.disable is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!f30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (pdata->gpio_data.trackstick_buttons) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Try [re-]establish link to F03. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) f30->f03 = rmi_find_function(fn->rmi_dev, 0x03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) f30->trackstick_buttons = f30->f03 != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (pdata->gpio_data.disable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* Write Control Register values back to device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) error = rmi_write_block(fn->rmi_dev, fn->fd.control_base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) f30->ctrl_regs, f30->ctrl_regs_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) dev_err(&fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) "%s: Could not write control registers at 0x%x: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) __func__, fn->fd.control_base_addr, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static void rmi_f30_set_ctrl_data(struct rmi_f30_ctrl_data *ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int *ctrl_addr, int len, u8 **reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ctrl->address = *ctrl_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ctrl->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ctrl->regs = *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *ctrl_addr += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) *reg += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static bool rmi_f30_is_valid_button(int button, struct rmi_f30_ctrl_data *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int byte_position = button >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int bit_position = button & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * ctrl2 -> dir == 0 -> input mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * ctrl3 -> data == 1 -> actual button
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return !(ctrl[2].regs[byte_position] & BIT(bit_position)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) (ctrl[3].regs[byte_position] & BIT(bit_position));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int rmi_f30_map_gpios(struct rmi_function *fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct f30_data *f30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) const struct rmi_device_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) rmi_get_platform_data(fn->rmi_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct input_dev *input = f30->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unsigned int button = BTN_LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) unsigned int trackstick_button = BTN_LEFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) bool button_mapped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int button_count = min_t(u8, f30->gpioled_count, TRACKSTICK_RANGE_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) f30->gpioled_key_map = devm_kcalloc(&fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) button_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) sizeof(f30->gpioled_key_map[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!f30->gpioled_key_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dev_err(&fn->dev, "Failed to allocate gpioled map memory.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) for (i = 0; i < button_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!rmi_f30_is_valid_button(i, f30->ctrl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (pdata->gpio_data.trackstick_buttons &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) i >= TRACKSTICK_RANGE_START && i < TRACKSTICK_RANGE_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) f30->gpioled_key_map[i] = trackstick_button++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) } else if (!pdata->gpio_data.buttonpad || !button_mapped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) f30->gpioled_key_map[i] = button;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) input_set_capability(input, EV_KEY, button++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) button_mapped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) input->keycode = f30->gpioled_key_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) input->keycodesize = sizeof(f30->gpioled_key_map[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) input->keycodemax = f30->gpioled_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * Buttonpad could be also inferred from f30->has_mech_mouse_btns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * but I am not sure, so use only the pdata info and the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * mapped buttons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (pdata->gpio_data.buttonpad || (button - BTN_LEFT == 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int rmi_f30_initialize(struct rmi_function *fn, struct f30_data *f30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) u8 *ctrl_reg = f30->ctrl_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int control_address = fn->fd.control_base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) u8 buf[RMI_F30_QUERY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) buf, RMI_F30_QUERY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) dev_err(&fn->dev, "Failed to read query register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) f30->has_extended_pattern = buf[0] & RMI_F30_EXTENDED_PATTERNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) f30->has_mappable_buttons = buf[0] & RMI_F30_HAS_MAPPABLE_BUTTONS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) f30->has_led = buf[0] & RMI_F30_HAS_LED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) f30->has_gpio = buf[0] & RMI_F30_HAS_GPIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) f30->has_haptic = buf[0] & RMI_F30_HAS_HAPTIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) f30->has_gpio_driver_control = buf[0] & RMI_F30_HAS_GPIO_DRV_CTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) f30->has_mech_mouse_btns = buf[0] & RMI_F30_HAS_MECH_MOUSE_BTNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) f30->gpioled_count = buf[1] & RMI_F30_GPIO_LED_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) f30->register_count = DIV_ROUND_UP(f30->gpioled_count, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (f30->has_gpio && f30->has_led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) rmi_f30_set_ctrl_data(&f30->ctrl[0], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) f30->register_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) rmi_f30_set_ctrl_data(&f30->ctrl[1], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) sizeof(u8), &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (f30->has_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) rmi_f30_set_ctrl_data(&f30->ctrl[2], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) f30->register_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) rmi_f30_set_ctrl_data(&f30->ctrl[3], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) f30->register_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (f30->has_led) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) rmi_f30_set_ctrl_data(&f30->ctrl[4], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) f30->register_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) rmi_f30_set_ctrl_data(&f30->ctrl[5], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) f30->has_extended_pattern ? 6 : 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (f30->has_led || f30->has_gpio_driver_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* control 6 uses a byte per gpio/led */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) rmi_f30_set_ctrl_data(&f30->ctrl[6], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) f30->gpioled_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (f30->has_mappable_buttons) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* control 7 uses a byte per gpio/led */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) rmi_f30_set_ctrl_data(&f30->ctrl[7], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) f30->gpioled_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (f30->has_haptic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) rmi_f30_set_ctrl_data(&f30->ctrl[8], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) f30->register_count, &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) rmi_f30_set_ctrl_data(&f30->ctrl[9], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) sizeof(u8), &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (f30->has_mech_mouse_btns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) rmi_f30_set_ctrl_data(&f30->ctrl[10], &control_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) sizeof(u8), &ctrl_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) f30->ctrl_regs_size = ctrl_reg -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) f30->ctrl_regs ?: RMI_F30_CTRL_REGS_MAX_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) error = rmi_f30_read_control_parameters(fn, f30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) dev_err(&fn->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) "Failed to initialize F30 control params: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (f30->has_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) error = rmi_f30_map_gpios(fn, f30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int rmi_f30_probe(struct rmi_function *fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct rmi_device *rmi_dev = fn->rmi_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) const struct rmi_device_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) rmi_get_platform_data(rmi_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct f30_data *f30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (pdata->gpio_data.disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (!drv_data->input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) dev_info(&fn->dev, "F30: no input device found, ignoring\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) f30 = devm_kzalloc(&fn->dev, sizeof(*f30), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!f30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) f30->input = drv_data->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) error = rmi_f30_initialize(fn, f30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dev_set_drvdata(&fn->dev, f30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct rmi_function_handler rmi_f30_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .name = "rmi4_f30",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .func = 0x30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .probe = rmi_f30_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .config = rmi_f30_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) .attention = rmi_f30_attention,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) };