^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Driver for TCA8418 I2C keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2011 Fuel7, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Kyle Manna <kyle.manna@fuel7.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * modify it under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * License v2 as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * You should have received a copy of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * License along with this program; if not, write to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Boston, MA 021110-1307, USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * If you can't comply with GPLv2, alternative licensing terms may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * arranged. Please contact Fuel7, Inc. (http://fuel7.com/) for proprietary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * alternative licensing inquiries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/input/matrix_keypad.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* TCA8418 hardware limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define TCA8418_MAX_ROWS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define TCA8418_MAX_COLS 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* TCA8418 register offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define REG_CFG 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define REG_INT_STAT 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define REG_KEY_LCK_EC 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define REG_KEY_EVENT_A 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define REG_KEY_EVENT_B 0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define REG_KEY_EVENT_C 0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define REG_KEY_EVENT_D 0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define REG_KEY_EVENT_E 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define REG_KEY_EVENT_F 0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define REG_KEY_EVENT_G 0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define REG_KEY_EVENT_H 0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define REG_KEY_EVENT_I 0x0C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define REG_KEY_EVENT_J 0x0D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define REG_KP_LCK_TIMER 0x0E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define REG_UNLOCK1 0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define REG_UNLOCK2 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define REG_GPIO_INT_STAT1 0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define REG_GPIO_INT_STAT2 0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define REG_GPIO_INT_STAT3 0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define REG_GPIO_DAT_STAT1 0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define REG_GPIO_DAT_STAT2 0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define REG_GPIO_DAT_STAT3 0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define REG_GPIO_DAT_OUT1 0x17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define REG_GPIO_DAT_OUT2 0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define REG_GPIO_DAT_OUT3 0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define REG_GPIO_INT_EN1 0x1A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define REG_GPIO_INT_EN2 0x1B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define REG_GPIO_INT_EN3 0x1C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define REG_KP_GPIO1 0x1D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define REG_KP_GPIO2 0x1E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define REG_KP_GPIO3 0x1F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define REG_GPI_EM1 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define REG_GPI_EM2 0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define REG_GPI_EM3 0x22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define REG_GPIO_DIR1 0x23
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define REG_GPIO_DIR2 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define REG_GPIO_DIR3 0x25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define REG_GPIO_INT_LVL1 0x26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define REG_GPIO_INT_LVL2 0x27
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define REG_GPIO_INT_LVL3 0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define REG_DEBOUNCE_DIS1 0x29
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define REG_DEBOUNCE_DIS2 0x2A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define REG_DEBOUNCE_DIS3 0x2B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define REG_GPIO_PULL1 0x2C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define REG_GPIO_PULL2 0x2D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define REG_GPIO_PULL3 0x2E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* TCA8418 bit definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define CFG_AI BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define CFG_GPI_E_CFG BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define CFG_OVR_FLOW_M BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define CFG_INT_CFG BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define CFG_OVR_FLOW_IEN BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define CFG_K_LCK_IEN BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define CFG_GPI_IEN BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #define CFG_KE_IEN BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define INT_STAT_CAD_INT BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define INT_STAT_OVR_FLOW_INT BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define INT_STAT_K_LCK_INT BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define INT_STAT_GPI_INT BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define INT_STAT_K_INT BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* TCA8418 register masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define KEY_LCK_EC_KEC 0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define KEY_EVENT_CODE 0x7f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define KEY_EVENT_VALUE 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct tca8418_keypad {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int row_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Write a byte to the TCA8418
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int tca8418_write_byte(struct tca8418_keypad *keypad_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) error = i2c_smbus_write_byte_data(keypad_data->client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dev_err(&keypad_data->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) "%s failed, reg: %d, val: %d, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) __func__, reg, val, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Read a byte from the TCA8418
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int tca8418_read_byte(struct tca8418_keypad *keypad_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int reg, u8 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) error = i2c_smbus_read_byte_data(keypad_data->client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) dev_err(&keypad_data->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) "%s failed, reg: %d, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) __func__, reg, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *val = (u8)error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct input_dev *input = keypad_data->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) unsigned short *keymap = input->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int error, col, row;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u8 reg, state, code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_err(&keypad_data->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) "unable to read REG_KEY_EVENT_A\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Assume that key code 0 signifies empty FIFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (reg <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) state = reg & KEY_EVENT_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) code = reg & KEY_EVENT_CODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) row = code / TCA8418_MAX_COLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) col = code % TCA8418_MAX_COLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) row = (col) ? row : row - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) input_event(input, EV_MSC, MSC_SCAN, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) input_report_key(input, keymap[code], state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) } while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^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) * Threaded IRQ handler and this can (and will) sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct tca8418_keypad *keypad_data = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) error = tca8418_read_byte(keypad_data, REG_INT_STAT, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) dev_err(&keypad_data->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) "unable to read REG_INT_STAT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (reg & INT_STAT_OVR_FLOW_INT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dev_warn(&keypad_data->client->dev, "overflow occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (reg & INT_STAT_K_INT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) tca8418_read_keypad(keypad_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) reg = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dev_err(&keypad_data->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) "unable to clear REG_INT_STAT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Configure the TCA8418 for keypad operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int tca8418_configure(struct tca8418_keypad *keypad_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) u32 rows, u32 cols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int reg, error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* Assemble a mask for row and column registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) reg = ~(~0 << rows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) reg += (~(~0 << cols)) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Set registers to keypad mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* Enable column debouncing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) error = tca8418_write_byte(keypad_data, REG_CFG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static int tca8418_keypad_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct tca8418_keypad *keypad_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) u32 rows = 0, cols = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int error, row_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* Check i2c driver capabilities */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) dev_err(dev, "%s adapter not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) dev_driver_string(&client->adapter->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) error = matrix_keypad_parse_properties(dev, &rows, &cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (!rows || rows > TCA8418_MAX_ROWS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev_err(dev, "invalid rows\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!cols || cols > TCA8418_MAX_COLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) dev_err(dev, "invalid columns\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) row_shift = get_count_order(cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /* Allocate memory for keypad_data and input device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!keypad_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) keypad_data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) keypad_data->row_shift = row_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* Read key lock register, if this fails assume device not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) error = tca8418_read_byte(keypad_data, REG_KEY_LCK_EC, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* Configure input device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) input = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) keypad_data->input = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) input->name = client->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) input->id.bustype = BUS_I2C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) input->id.vendor = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) input->id.product = 0x001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) input->id.version = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) error = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL, input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) dev_err(dev, "Failed to build keymap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (device_property_read_bool(dev, "keypad,autorepeat"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) __set_bit(EV_REP, input->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) input_set_capability(input, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) error = devm_request_threaded_irq(dev, client->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) NULL, tca8418_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) IRQF_SHARED | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) client->name, keypad_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) dev_err(dev, "Unable to claim irq %d; error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) client->irq, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return error;
^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) /* Initialize the chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) error = tca8418_configure(keypad_data, rows, cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) dev_err(dev, "Unable to register input device, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^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) static const struct i2c_device_id tca8418_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) { "tca8418", 8418, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_DEVICE_TABLE(i2c, tca8418_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static const struct of_device_id tca8418_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) { .compatible = "ti,tca8418", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static struct i2c_driver tca8418_keypad_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .name = "tca8418_keypad",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) .of_match_table = tca8418_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .probe = tca8418_keypad_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) .id_table = tca8418_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int __init tca8418_keypad_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return i2c_add_driver(&tca8418_keypad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) subsys_initcall(tca8418_keypad_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static void __exit tca8418_keypad_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) i2c_del_driver(&tca8418_keypad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) module_exit(tca8418_keypad_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) MODULE_DESCRIPTION("Keypad driver for TCA8418");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) MODULE_LICENSE("GPL");