summaryrefslogtreecommitdiff
path: root/board/keyborg/touch_scan.c
blob: 83e58f219c928daa17afd62636a91fdc92298683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Touch scanning module */

#include "common.h"
#include "console.h"
#include "debug.h"
#include "dma.h"
#include "encode.h"
#include "gpio.h"
#include "hooks.h"
#include "master_slave.h"
#include "registers.h"
#include "spi_comm.h"
#include "timer.h"
#include "touch_scan.h"
#include "util.h"

#define TS_PIN_TO_CR(p) ((((p).port_id + 1) << 16) | (1 << (p).pin))
#define TS_GPIO_TO_BASE(p) (0x40010800 + (p) * 0x400)

static uint8_t buf[2][ROW_COUNT * 2];

#ifdef CONFIG_KEYBORG_FAST_SCAN
#define SCAN_BUF_SIZE (DIV_ROUND_UP(COL_COUNT * 2, 32) + 2)
#define GET_SCAN_NEEDED(x) (scan_needed[(x) / 32 + 1] & (1 << ((x) & 31)))
static uint32_t scan_needed[SCAN_BUF_SIZE];
#else
#define GET_SCAN_NEEDED(x) 1
#endif

#define SPAN_LENGTH (2 * COL_SPAN + 1)
#define SPAN_MASK ((1 << SPAN_LENGTH) - 1)

static uint32_t mccr_list[COL_COUNT];
static uint32_t mrcr_list[ROW_COUNT];

static void set_gpio(const struct ts_pin pin, enum pin_type type)
{
	uint32_t addr, mode, mask;
	uint32_t port = TS_GPIO_TO_BASE(pin.port_id);
	uint32_t pmask = 1 << pin.pin;

	if (pmask & 0xff) {
		addr = port;
		mode = pmask;
	} else {
		addr = port + 0x04;
		mode = pmask >> 8;
	}
	mode = mode * mode * mode * mode * 0xf;

	mask = REG32(addr) & ~mode;

	if (type == PIN_COL) {
		/* Alternate output open-drain */
		mask |= 0xdddddddd & mode;
	} else if (type == PIN_PD) {
		mask |= 0x88888888 & mode;
		STM32_GPIO_BSRR(port) = pmask << 16;
	} else if (type == PIN_ROW) {
		/* Nothing for PIN_ROW. Already analog input. */
	}

	REG32(addr) = mask;
}

void touch_scan_init(void)
{
	int i;

	for (i = 0; i < ROW_COUNT; ++i) {
		set_gpio(col_pins[i], PIN_ROW);
		STM32_PMSE_PxPMR(row_pins[i].port_id) |= 1 << row_pins[i].pin;
	}
	for (i = 0; i < COL_COUNT; ++i)
		set_gpio(col_pins[i], PIN_PD);

	for (i = 0; i < ROW_COUNT; ++i)
		mrcr_list[i] = TS_PIN_TO_CR(row_pins[i]);
	for (i = 0; i < COL_COUNT; ++i)
		mccr_list[i] = TS_PIN_TO_CR(col_pins[i]);
}

static void discharge(void)
{
	int i;

	/*
	 * The value 20 is deducted from experiments.
	 * Somehow this needs to be in reverse order
	 */
	for (i = 20; i >= 0; --i)
		STM32_PMSE_MRCR = mrcr_list[i];
}

static void start_adc_sample(int id, int wait_cycle)
{
	/* Clear EOC and STRT bit */
	STM32_ADC_SR(id) &= ~((1 << 1) | (1 << 4));

	/* Start conversion */
	STM32_ADC_CR2(id) |= (1 << 0);

	/* Wait for conversion start */
	while (!(STM32_ADC_SR(id) & (1 << 4)))
		;

	/* Each iteration takes 3 CPU cycles */
	asm("1: subs %0, #1\n"
	    "   bne 1b\n" :: "r"(wait_cycle / 3));
}

static inline uint8_t flush_adc(int id)
{
	uint16_t v;

#if ADC_SMPL_CYCLE_2 < ADC_QUNTZ_CYCLE_2
	while (!(STM32_ADC_SR(id) & (1 << 1)))
		;
#endif

	v = STM32_ADC_DR(id) >> ADC_WINDOW_POS;
	if (v > 255)
		v = 255;
	return v;
}

static void enable_col(int idx, int enabled)
{
	if (enabled) {
		set_gpio(col_pins[idx], PIN_COL);
		STM32_PMSE_PxPMR(col_pins[idx].port_id) |=
			1 << col_pins[idx].pin;
	} else {
		set_gpio(col_pins[idx], PIN_PD);
		STM32_PMSE_PxPMR(col_pins[idx].port_id) &=
			~(1 << col_pins[idx].pin);
	}
}

#ifdef CONFIG_KEYBORG_FAST_SCAN
static inline void set_scan_needed(int col)
{
	uint8_t word = (col - COL_SPAN + 32) / 32;
	uint8_t bit = (col - COL_SPAN + 32) & 31;

	scan_needed[word] |= SPAN_MASK << bit;
	if (bit + SPAN_LENGTH > 32)
		scan_needed[word + 1] |= SPAN_MASK >> (32 - bit);
}

int fast_scan(uint32_t *data)
{
	int col;
	int chip_col = 0;

	memset(data, 0, SCAN_BUF_SIZE * 4);

	STM32_PMSE_MRCR = 1 << 31;
	for (col = 0; col < COL_COUNT * 2; ++col) {
		if (master_slave_is_master())
			chip_col = (col >= COL_COUNT) ? col - COL_COUNT : -1;
		else
			chip_col = (col < COL_COUNT) ? col : -1;
		if (chip_col >= 0) {
			enable_col(chip_col, 1);
			STM32_PMSE_MCCR = mccr_list[chip_col];
		}

		if (master_slave_sync(5) != EC_SUCCESS)
			goto fast_scan_err;

		start_adc_sample(0, ADC_SMPL_CPU_CYCLE);
		while (!(STM32_ADC_SR(0) & (1 << 1)))
			;
		if (flush_adc(0) >= COL_THRESHOLD)
			set_scan_needed(col);

		if (master_slave_sync(5) != EC_SUCCESS)
			goto fast_scan_err;
		if (chip_col >= 0) {
			enable_col(chip_col, 0);
			STM32_PMSE_MCCR = 0;
		}
	}
	STM32_PMSE_MRCR = 0;

	/* Discharge the panel */
	discharge();

	return EC_SUCCESS;
fast_scan_err:
	enable_col(chip_col, 0);
	STM32_PMSE_MCCR = 0;
	STM32_PMSE_MRCR = 0;
	return EC_ERROR_UNKNOWN;
}
#else
#define fast_scan(x) EC_SUCCESS
#endif

void scan_column(uint8_t *data)
{
	int i;

	STM32_PMSE_MRCR = mrcr_list[0];
	start_adc_sample(0, ADC_SMPL_CPU_CYCLE);
	STM32_PMSE_MRCR = mrcr_list[1];
	start_adc_sample(1, ADC_SMPL_CPU_CYCLE);

	for (i = 2; i < ROW_COUNT; ++i) {
		data[i - 2] = flush_adc(i & 1);
		STM32_PMSE_MRCR = mrcr_list[i];
		start_adc_sample(i & 1, ADC_SMPL_CPU_CYCLE);
	}

	while (!(STM32_ADC_SR(ROW_COUNT & 1) & (1 << 1)))
		;
	data[ROW_COUNT - 2] = flush_adc(ROW_COUNT & 1);
	while (!(STM32_ADC_SR((ROW_COUNT & 1) ^ 1) & (1 << 1)))
		;
	data[ROW_COUNT - 1] = flush_adc((ROW_COUNT & 1) ^ 1);
}

void touch_scan_slave_start(void)
{
	int col = 0, i, v;
	struct spi_comm_packet *resp = (struct spi_comm_packet *)buf;

	if (fast_scan(scan_needed) != EC_SUCCESS)
		goto slave_err;

	for (col = 0; col < COL_COUNT * 2; ++col) {
		if (col < COL_COUNT) {
			enable_col(col, 1);
			STM32_PMSE_MCCR = mccr_list[col];
		}

		if (master_slave_sync(20) != EC_SUCCESS)
			goto slave_err;

		if (GET_SCAN_NEEDED(col)) {
			scan_column(resp->data);

			/* Reverse the scanned data */
			for (i = 0; ROW_COUNT - 1 - i > i; ++i) {
				v = resp->data[i];
				resp->data[i] = resp->data[ROW_COUNT - 1 - i];
				resp->data[ROW_COUNT - 1 - i] = v;
			}
			resp->size = ROW_COUNT;
		} else {
			resp->size = 0;
		}

		resp->cmd_sts = EC_SUCCESS;

		/* Flush the last response */
		if (col != 0)
			if (spi_slave_send_response_flush() != EC_SUCCESS)
				goto slave_err;

		/* Start sending the response for the current column */
		if (spi_slave_send_response_async(resp) != EC_SUCCESS)
			goto slave_err;

		/* Disable the current column and discharge */
		if (col < COL_COUNT) {
			enable_col(col, 0);
			STM32_PMSE_MCCR = 0;
		}
		discharge();
	}
	spi_slave_send_response_flush();
	master_slave_sync(20);
	return;
slave_err:
	if (col < COL_COUNT)
		enable_col(col, 0);
	STM32_PMSE_MCCR = 0;
	spi_slave_send_response_flush();
}

int touch_scan_full_matrix(void)
{
	struct spi_comm_packet cmd;
	const struct spi_comm_packet *resp;
	int col = 0;
	timestamp_t st = get_time();
	uint8_t *dptr = NULL, *last_dptr = NULL;

	cmd.cmd_sts = TS_CMD_FULL_SCAN;
	cmd.size = 0;

	if (spi_master_send_command(&cmd))
		goto master_err;

	encode_reset();

	if (fast_scan(scan_needed) != EC_SUCCESS)
		goto master_err;

	for (col = 0; col < COL_COUNT * 2; ++col) {
		if (col >= COL_COUNT) {
			enable_col(col - COL_COUNT, 1);
			STM32_PMSE_MCCR = mccr_list[col - COL_COUNT];
		}

		if (master_slave_sync(20) != EC_SUCCESS)
			goto master_err;

		last_dptr = dptr;
		dptr = buf[col & 1];

		if (GET_SCAN_NEEDED(col))
			scan_column(dptr + ROW_COUNT);
		else
			memset(dptr + ROW_COUNT, 0, ROW_COUNT);

		if (col > 0) {
			/* Flush the data from the slave for the last column */
			resp = spi_master_wait_response_done();
			if (resp == NULL)
				goto master_err;
			if (resp->size)
				memcpy(last_dptr, resp->data, ROW_COUNT);
			else
				memset(last_dptr, 0, ROW_COUNT);
			encode_add_column(last_dptr);
		}

		/* Start receiving data for the current column */
		if (spi_master_wait_response_async() != EC_SUCCESS)
			goto master_err;

		/* Disable the current column and discharge */
		if (col >= COL_COUNT) {
			enable_col(col - COL_COUNT, 0);
			STM32_PMSE_MCCR = 0;
		}
		discharge();
	}

	resp = spi_master_wait_response_done();
	if (resp == NULL)
		goto master_err;
	if (resp->size)
		memcpy(last_dptr, resp->data, ROW_COUNT);
	else
		memset(last_dptr, 0, ROW_COUNT);
	encode_add_column(last_dptr);

	master_slave_sync(20);

	debug_printf("Sampling took %d us\n", get_time().val - st.val);
	encode_dump_matrix();

	return EC_SUCCESS;
master_err:
	spi_master_wait_response_done();
	if (col >= COL_COUNT)
		enable_col(col - COL_COUNT, 0);
	STM32_PMSE_MCCR = 0;
	return EC_ERROR_UNKNOWN;
}