1204 lines · 34.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * AD714X CapTouch Programmable Controller driver supporting AD7142/3/7/8/7A * * Copyright 2009-2011 Analog Devices Inc. */ #include <linux/device.h>#include <linux/export.h>#include <linux/input.h>#include <linux/interrupt.h>#include <linux/slab.h>#include <linux/input/ad714x.h>#include <linux/module.h>#include "ad714x.h" #define AD714X_PWR_CTRL           0x0#define AD714X_STG_CAL_EN_REG     0x1#define AD714X_AMB_COMP_CTRL0_REG 0x2#define AD714X_PARTID_REG         0x17#define AD7142_PARTID             0xE620#define AD7143_PARTID             0xE630#define AD7147_PARTID             0x1470#define AD7148_PARTID             0x1480#define AD714X_STAGECFG_REG       0x80#define AD714X_SYSCFG_REG         0x0 #define STG_LOW_INT_EN_REG     0x5#define STG_HIGH_INT_EN_REG    0x6#define STG_COM_INT_EN_REG     0x7#define STG_LOW_INT_STA_REG    0x8#define STG_HIGH_INT_STA_REG   0x9#define STG_COM_INT_STA_REG    0xA #define CDC_RESULT_S0          0xB#define CDC_RESULT_S1          0xC#define CDC_RESULT_S2          0xD#define CDC_RESULT_S3          0xE#define CDC_RESULT_S4          0xF#define CDC_RESULT_S5          0x10#define CDC_RESULT_S6          0x11#define CDC_RESULT_S7          0x12#define CDC_RESULT_S8          0x13#define CDC_RESULT_S9          0x14#define CDC_RESULT_S10         0x15#define CDC_RESULT_S11         0x16 #define STAGE0_AMBIENT		0xF1#define STAGE1_AMBIENT		0x115#define STAGE2_AMBIENT		0x139#define STAGE3_AMBIENT		0x15D#define STAGE4_AMBIENT		0x181#define STAGE5_AMBIENT		0x1A5#define STAGE6_AMBIENT		0x1C9#define STAGE7_AMBIENT		0x1ED#define STAGE8_AMBIENT		0x211#define STAGE9_AMBIENT		0x234#define STAGE10_AMBIENT		0x259#define STAGE11_AMBIENT		0x27D #define PER_STAGE_REG_NUM      36#define STAGE_CFGREG_NUM       8#define SYS_CFGREG_NUM         8 /* * driver information which will be used to maintain the software flow */enum ad714x_device_state { IDLE, JITTER, ACTIVE, SPACE }; struct ad714x_slider_drv {	int highest_stage;	int abs_pos;	int flt_pos;	enum ad714x_device_state state;	struct input_dev *input;}; struct ad714x_wheel_drv {	int abs_pos;	int flt_pos;	int pre_highest_stage;	int highest_stage;	enum ad714x_device_state state;	struct input_dev *input;}; struct ad714x_touchpad_drv {	int x_highest_stage;	int x_flt_pos;	int x_abs_pos;	int y_highest_stage;	int y_flt_pos;	int y_abs_pos;	int left_ep;	int left_ep_val;	int right_ep;	int right_ep_val;	int top_ep;	int top_ep_val;	int bottom_ep;	int bottom_ep_val;	enum ad714x_device_state state;	struct input_dev *input;}; struct ad714x_button_drv {	enum ad714x_device_state state;	/*	 * Unlike slider/wheel/touchpad, all buttons point to	 * same input_dev instance	 */	struct input_dev *input;}; struct ad714x_driver_data {	struct ad714x_slider_drv *slider;	struct ad714x_wheel_drv *wheel;	struct ad714x_touchpad_drv *touchpad;	struct ad714x_button_drv *button;}; /* * information to integrate all things which will be private data * of spi/i2c device */ static void ad714x_use_com_int(struct ad714x_chip *ad714x,				int start_stage, int end_stage){	unsigned short data;	unsigned short mask; 	mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1); 	ad714x->read(ad714x, STG_COM_INT_EN_REG, &data, 1);	data |= 1 << end_stage;	ad714x->write(ad714x, STG_COM_INT_EN_REG, data); 	ad714x->read(ad714x, STG_HIGH_INT_EN_REG, &data, 1);	data &= ~mask;	ad714x->write(ad714x, STG_HIGH_INT_EN_REG, data);} static void ad714x_use_thr_int(struct ad714x_chip *ad714x,				int start_stage, int end_stage){	unsigned short data;	unsigned short mask; 	mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1); 	ad714x->read(ad714x, STG_COM_INT_EN_REG, &data, 1);	data &= ~(1 << end_stage);	ad714x->write(ad714x, STG_COM_INT_EN_REG, data); 	ad714x->read(ad714x, STG_HIGH_INT_EN_REG, &data, 1);	data |= mask;	ad714x->write(ad714x, STG_HIGH_INT_EN_REG, data);} static int ad714x_cal_highest_stage(struct ad714x_chip *ad714x,					int start_stage, int end_stage){	int max_res = 0;	int max_idx = 0;	int i; 	for (i = start_stage; i <= end_stage; i++) {		if (ad714x->sensor_val[i] > max_res) {			max_res = ad714x->sensor_val[i];			max_idx = i;		}	} 	return max_idx;} static int ad714x_cal_abs_pos(struct ad714x_chip *ad714x,				int start_stage, int end_stage,				int highest_stage, int max_coord){	int a_param, b_param; 	if (highest_stage == start_stage) {		a_param = ad714x->sensor_val[start_stage + 1];		b_param = ad714x->sensor_val[start_stage] +			ad714x->sensor_val[start_stage + 1];	} else if (highest_stage == end_stage) {		a_param = ad714x->sensor_val[end_stage] *			(end_stage - start_stage) +			ad714x->sensor_val[end_stage - 1] *			(end_stage - start_stage - 1);		b_param = ad714x->sensor_val[end_stage] +			ad714x->sensor_val[end_stage - 1];	} else {		a_param = ad714x->sensor_val[highest_stage] *			(highest_stage - start_stage) +			ad714x->sensor_val[highest_stage - 1] *			(highest_stage - start_stage - 1) +			ad714x->sensor_val[highest_stage + 1] *			(highest_stage - start_stage + 1);		b_param = ad714x->sensor_val[highest_stage] +			ad714x->sensor_val[highest_stage - 1] +			ad714x->sensor_val[highest_stage + 1];	} 	return (max_coord / (end_stage - start_stage)) * a_param / b_param;} /* * One button can connect to multi positive and negative of CDCs * Multi-buttons can connect to same positive/negative of one CDC */static void ad714x_button_state_machine(struct ad714x_chip *ad714x, int idx){	struct ad714x_button_plat *hw = &ad714x->hw->button[idx];	struct ad714x_button_drv *sw = &ad714x->sw->button[idx]; 	switch (sw->state) {	case IDLE:		if (((ad714x->h_state & hw->h_mask) == hw->h_mask) &&		    ((ad714x->l_state & hw->l_mask) == hw->l_mask)) {			dev_dbg(ad714x->dev, "button %d touched\n", idx);			input_report_key(sw->input, hw->keycode, 1);			input_sync(sw->input);			sw->state = ACTIVE;		}		break; 	case ACTIVE:		if (((ad714x->h_state & hw->h_mask) != hw->h_mask) ||		    ((ad714x->l_state & hw->l_mask) != hw->l_mask)) {			dev_dbg(ad714x->dev, "button %d released\n", idx);			input_report_key(sw->input, hw->keycode, 0);			input_sync(sw->input);			sw->state = IDLE;		}		break; 	default:		break;	}} /* * The response of a sensor is defined by the absolute number of codes * between the current CDC value and the ambient value. */static void ad714x_slider_cal_sensor_val(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];	int i; 	ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,			&ad714x->adc_reg[hw->start_stage],			hw->end_stage - hw->start_stage + 1); 	for (i = hw->start_stage; i <= hw->end_stage; i++) {		ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,				&ad714x->amb_reg[i], 1); 		ad714x->sensor_val[i] =			abs(ad714x->adc_reg[i] - ad714x->amb_reg[i]);	}} static void ad714x_slider_cal_highest_stage(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];	struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx]; 	sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,			hw->end_stage); 	dev_dbg(ad714x->dev, "slider %d highest_stage:%d\n", idx,		sw->highest_stage);} /* * The formulae are very straight forward. It uses the sensor with the * highest response and the 2 adjacent ones. * When Sensor 0 has the highest response, only sensor 0 and sensor 1 * are used in the calculations. Similarly when the last sensor has the * highest response, only the last sensor and the second last sensors * are used in the calculations. * * For i= idx_of_peak_Sensor-1 to i= idx_of_peak_Sensor+1 *         v += Sensor response(i)*i *         w += Sensor response(i) * POS=(Number_of_Positions_Wanted/(Number_of_Sensors_Used-1)) *(v/w) */static void ad714x_slider_cal_abs_pos(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];	struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx]; 	sw->abs_pos = ad714x_cal_abs_pos(ad714x, hw->start_stage, hw->end_stage,		sw->highest_stage, hw->max_coord); 	dev_dbg(ad714x->dev, "slider %d absolute position:%d\n", idx,		sw->abs_pos);} /* * To minimise the Impact of the noise on the algorithm, ADI developed a * routine that filters the CDC results after they have been read by the * host processor. * The filter used is an Infinite Input Response(IIR) filter implemented * in firmware and attenuates the noise on the CDC results after they've * been read by the host processor. * Filtered_CDC_result = (Filtered_CDC_result * (10 - Coefficient) + *				Latest_CDC_result * Coefficient)/10 */static void ad714x_slider_cal_flt_pos(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx]; 	sw->flt_pos = (sw->flt_pos * (10 - 4) +			sw->abs_pos * 4)/10; 	dev_dbg(ad714x->dev, "slider %d filter position:%d\n", idx,		sw->flt_pos);} static void ad714x_slider_use_com_int(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx]; 	ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);} static void ad714x_slider_use_thr_int(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx]; 	ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);} static void ad714x_slider_state_machine(struct ad714x_chip *ad714x, int idx){	struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];	struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];	unsigned short h_state, c_state;	unsigned short mask; 	mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1); 	h_state = ad714x->h_state & mask;	c_state = ad714x->c_state & mask; 	switch (sw->state) {	case IDLE:		if (h_state) {			sw->state = JITTER;			/* In End of Conversion interrupt mode, the AD714X			 * continuously generates hardware interrupts.			 */			ad714x_slider_use_com_int(ad714x, idx);			dev_dbg(ad714x->dev, "slider %d touched\n", idx);		}		break; 	case JITTER:		if (c_state == mask) {			ad714x_slider_cal_sensor_val(ad714x, idx);			ad714x_slider_cal_highest_stage(ad714x, idx);			ad714x_slider_cal_abs_pos(ad714x, idx);			sw->flt_pos = sw->abs_pos;			sw->state = ACTIVE;		}		break; 	case ACTIVE:		if (c_state == mask) {			if (h_state) {				ad714x_slider_cal_sensor_val(ad714x, idx);				ad714x_slider_cal_highest_stage(ad714x, idx);				ad714x_slider_cal_abs_pos(ad714x, idx);				ad714x_slider_cal_flt_pos(ad714x, idx);				input_report_abs(sw->input, ABS_X, sw->flt_pos);				input_report_key(sw->input, BTN_TOUCH, 1);			} else {				/* When the user lifts off the sensor, configure				 * the AD714X back to threshold interrupt mode.				 */				ad714x_slider_use_thr_int(ad714x, idx);				sw->state = IDLE;				input_report_key(sw->input, BTN_TOUCH, 0);				dev_dbg(ad714x->dev, "slider %d released\n",					idx);			}			input_sync(sw->input);		}		break; 	default:		break;	}} /* * When the scroll wheel is activated, we compute the absolute position based * on the sensor values. To calculate the position, we first determine the * sensor that has the greatest response among the 8 sensors that constitutes * the scrollwheel. Then we determined the 2 sensors on either sides of the * sensor with the highest response and we apply weights to these sensors. */static void ad714x_wheel_cal_highest_stage(struct ad714x_chip *ad714x, int idx){	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];	struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx]; 	sw->pre_highest_stage = sw->highest_stage;	sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,			hw->end_stage); 	dev_dbg(ad714x->dev, "wheel %d highest_stage:%d\n", idx,		sw->highest_stage);} static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx){	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];	int i; 	ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,			&ad714x->adc_reg[hw->start_stage],			hw->end_stage - hw->start_stage + 1); 	for (i = hw->start_stage; i <= hw->end_stage; i++) {		ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,				&ad714x->amb_reg[i], 1);		if (ad714x->adc_reg[i] > ad714x->amb_reg[i])			ad714x->sensor_val[i] =				ad714x->adc_reg[i] - ad714x->amb_reg[i];		else			ad714x->sensor_val[i] = 0;	}} /* * When the scroll wheel is activated, we compute the absolute position based * on the sensor values. To calculate the position, we first determine the * sensor that has the greatest response among the sensors that constitutes * the scrollwheel. Then we determined the sensors on either sides of the * sensor with the highest response and we apply weights to these sensors. The * result of this computation gives us the mean value. */ static void ad714x_wheel_cal_abs_pos(struct ad714x_chip *ad714x, int idx){	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];	struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];	int stage_num = hw->end_stage - hw->start_stage + 1;	int first_before, highest, first_after;	int a_param, b_param; 	first_before = (sw->highest_stage + stage_num - 1) % stage_num;	highest = sw->highest_stage;	first_after = (sw->highest_stage + stage_num + 1) % stage_num; 	a_param = ad714x->sensor_val[highest] *		(highest - hw->start_stage) +		ad714x->sensor_val[first_before] *		(highest - hw->start_stage - 1) +		ad714x->sensor_val[first_after] *		(highest - hw->start_stage + 1);	b_param = ad714x->sensor_val[highest] +		ad714x->sensor_val[first_before] +		ad714x->sensor_val[first_after]; 	sw->abs_pos = ((hw->max_coord / (hw->end_stage - hw->start_stage)) *			a_param) / b_param; 	if (sw->abs_pos > hw->max_coord)		sw->abs_pos = hw->max_coord;	else if (sw->abs_pos < 0)		sw->abs_pos = 0;} static void ad714x_wheel_cal_flt_pos(struct ad714x_chip *ad714x, int idx){	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];	struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];	if (((sw->pre_highest_stage == hw->end_stage) &&			(sw->highest_stage == hw->start_stage)) ||	    ((sw->pre_highest_stage == hw->start_stage) &&			(sw->highest_stage == hw->end_stage)))		sw->flt_pos = sw->abs_pos;	else		sw->flt_pos = ((sw->flt_pos * 30) + (sw->abs_pos * 71)) / 100; 	if (sw->flt_pos > hw->max_coord)		sw->flt_pos = hw->max_coord;} static void ad714x_wheel_use_com_int(struct ad714x_chip *ad714x, int idx){	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx]; 

Lines 1–500 of 1204.