Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * Touchkey driver for Freescale MPR121 Controllor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2011 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on mcs_touchkey.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Register definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define ELE_TOUCH_STATUS_0_ADDR	0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define ELE_TOUCH_STATUS_1_ADDR	0X1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define MHD_RISING_ADDR		0x2b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define NHD_RISING_ADDR		0x2c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define NCL_RISING_ADDR		0x2d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define FDL_RISING_ADDR		0x2e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define MHD_FALLING_ADDR	0x2f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define NHD_FALLING_ADDR	0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define NCL_FALLING_ADDR	0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define FDL_FALLING_ADDR	0x32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define ELE0_TOUCH_THRESHOLD_ADDR	0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define ELE0_RELEASE_THRESHOLD_ADDR	0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define AFE_CONF_ADDR			0x5c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define FILTER_CONF_ADDR		0x5d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * ELECTRODE_CONF_ADDR: This register configures the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * enabled capacitance sensing inputs and its run/suspend mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define ELECTRODE_CONF_ADDR		0x5e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define ELECTRODE_CONF_QUICK_CHARGE	0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define AUTO_CONFIG_CTRL_ADDR		0x7b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define AUTO_CONFIG_USL_ADDR		0x7d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define AUTO_CONFIG_LSL_ADDR		0x7e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define AUTO_CONFIG_TL_ADDR		0x7f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /* Threshold of touch/release trigger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define TOUCH_THRESHOLD			0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define RELEASE_THRESHOLD		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /* Masks for touch and release triggers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define TOUCH_STATUS_MASK		0xfff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) /* MPR121 has 12 keys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define MPR121_MAX_KEY_COUNT		12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define MPR121_MIN_POLL_INTERVAL	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define MPR121_MAX_POLL_INTERVAL	200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) struct mpr121_touchkey {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct input_dev	*input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	unsigned int		statusbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned int		keycount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	u32			keycodes[MPR121_MAX_KEY_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) struct mpr121_init_register {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static const struct mpr121_init_register init_reg_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	{ MHD_RISING_ADDR,	0x1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	{ NHD_RISING_ADDR,	0x1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	{ MHD_FALLING_ADDR,	0x1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	{ NHD_FALLING_ADDR,	0x1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	{ NCL_FALLING_ADDR,	0xff },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	{ FDL_FALLING_ADDR,	0x02 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	{ FILTER_CONF_ADDR,	0x04 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	{ AFE_CONF_ADDR,	0x0b },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	{ AUTO_CONFIG_CTRL_ADDR, 0x0b },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static void mpr121_vdd_supply_disable(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct regulator *vdd_supply = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	regulator_disable(vdd_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static struct regulator *mpr121_vdd_supply_init(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct regulator *vdd_supply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	vdd_supply = devm_regulator_get(dev, "vdd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (IS_ERR(vdd_supply)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		dev_err(dev, "failed to get vdd regulator: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			PTR_ERR(vdd_supply));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return vdd_supply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	err = regulator_enable(vdd_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		dev_err(dev, "failed to enable vdd regulator: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	err = devm_add_action(dev, mpr121_vdd_supply_disable, vdd_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		regulator_disable(vdd_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		dev_err(dev, "failed to add disable regulator action: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return vdd_supply;
^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) static void mpr_touchkey_report(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct mpr121_touchkey *mpr121 = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct input_dev *input = mpr121->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct i2c_client *client = mpr121->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	unsigned long bit_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	unsigned int key_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (reg < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		dev_err(&client->dev, "i2c read error [%d]\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	reg <<= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (reg < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		dev_err(&client->dev, "i2c read error [%d]\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	reg &= TOUCH_STATUS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	/* use old press bit to figure out which bit changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	bit_changed = reg ^ mpr121->statusbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	mpr121->statusbits = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	for_each_set_bit(key_num, &bit_changed, mpr121->keycount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		unsigned int key_val, pressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		pressed = reg & BIT(key_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		key_val = mpr121->keycodes[key_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		input_event(input, EV_MSC, MSC_SCAN, key_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		input_report_key(input, key_val, pressed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			pressed ? "pressed" : "released");
^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) 	input_sync(input);
^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 irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct mpr121_touchkey *mpr121 = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	mpr_touchkey_report(mpr121->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int mpr121_phys_init(struct mpr121_touchkey *mpr121,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			    struct i2c_client *client, int vdd_uv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	const struct mpr121_init_register *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	unsigned char usl, lsl, tl, eleconf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int i, t, vdd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	/* Set up touch/release threshold for ele0-ele11 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			goto err_i2c_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		ret = i2c_smbus_write_byte_data(client, t + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 						RELEASE_THRESHOLD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			goto err_i2c_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	/* Set up init register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		reg = &init_reg_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			goto err_i2c_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 * Capacitance on sensing input varies and needs to be compensated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * The internal MPR121-auto-configuration can do this if it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * registers are set properly (based on vdd_uv).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	vdd = vdd_uv / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	usl = ((vdd - 700) * 256) / vdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	lsl = (usl * 65) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	tl = (usl * 90) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 * Quick charge bit will let the capacitive charge to ready
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 * state quickly, or the buttons may not function after system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 * boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 					 eleconf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		goto err_i2c_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) err_i2c_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	dev_err(&client->dev, "i2c write error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int mpr_touchkey_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			      const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct regulator *vdd_supply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int vdd_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct mpr121_touchkey *mpr121;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	u32 poll_interval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	vdd_supply = mpr121_vdd_supply_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (IS_ERR(vdd_supply))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return PTR_ERR(vdd_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	vdd_uv = regulator_get_voltage(vdd_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (!mpr121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	input_dev = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	mpr121->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	mpr121->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	mpr121->keycount = device_property_count_u32(dev, "linux,keycodes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (mpr121->keycount > MPR121_MAX_KEY_COUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		dev_err(dev, "too many keys defined (%d)\n", mpr121->keycount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	error = device_property_read_u32_array(dev, "linux,keycodes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 					       mpr121->keycodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 					       mpr121->keycount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			"failed to read linux,keycode property: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	input_dev->name = "Freescale MPR121 Touchkey";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	input_dev->id.bustype = BUS_I2C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	input_dev->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (device_property_read_bool(dev, "autorepeat"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		__set_bit(EV_REP, input_dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	input_set_drvdata(input_dev, mpr121);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	input_dev->keycode = mpr121->keycodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	input_dev->keycodemax = mpr121->keycount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	for (i = 0; i < mpr121->keycount; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	error = mpr121_phys_init(mpr121, client, vdd_uv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		dev_err(dev, "Failed to init register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	device_property_read_u32(dev, "poll-interval", &poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (client->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		error = devm_request_threaded_irq(dev, client->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 						  mpr_touchkey_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 						  IRQF_TRIGGER_FALLING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 						  IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 						  dev->driver->name, mpr121);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			dev_err(dev, "Failed to register interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	} else if (poll_interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		if (poll_interval < MPR121_MIN_POLL_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (poll_interval > MPR121_MAX_POLL_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		error = input_setup_polling(input_dev, mpr_touchkey_report);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			dev_err(dev, "Failed to setup polling\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			return error;
^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) 		input_set_poll_interval(input_dev, poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		input_set_min_poll_interval(input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 					    MPR121_MIN_POLL_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		input_set_max_poll_interval(input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 					    MPR121_MAX_POLL_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			"invalid IRQ number and polling not configured\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	error = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	i2c_set_clientdata(client, mpr121);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	device_init_wakeup(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			device_property_read_bool(dev, "wakeup-source"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int __maybe_unused mpr_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (device_may_wakeup(&client->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		enable_irq_wake(client->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int __maybe_unused mpr_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (device_may_wakeup(&client->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		disable_irq_wake(client->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 				  mpr121->keycount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static const struct i2c_device_id mpr121_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	{ "mpr121_touchkey", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_DEVICE_TABLE(i2c, mpr121_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static const struct of_device_id mpr121_touchkey_dt_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	{ .compatible = "fsl,mpr121-touchkey" },
^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) MODULE_DEVICE_TABLE(of, mpr121_touchkey_dt_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static struct i2c_driver mpr_touchkey_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		.name	= "mpr121",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		.pm	= &mpr121_touchkey_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		.of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	.id_table	= mpr121_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	.probe		= mpr_touchkey_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) module_i2c_driver(mpr_touchkey_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");