rendered paste body/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id: button-imx31.c 13941 2007-07-20 15:14:29Z aliask $ * * Copyright (C) 2006 by Linus Nielsen Feltzing * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/#include "config.h"#include "cpu.h"#include "system.h"#include "button.h"#include "kernel.h"#include "backlight.h"#include "adc.h"#include "system.h"#include "backlight-target.h"#include "debug.h"#include "lcd.h"/* Most code in here is taken from the Linux BSP provided by Freescale */void button_init_device(void){ unsigned int reg_val; /* Enable keypad clock */ //mxc_clks_enable(KPP_CLK); /* Enable number of rows in keypad (KPCR[7:0]) * Configure keypad columns as open-drain (KPCR[15:8]) * * Configure the rows/cols in KPP * LSB nibble in KPP is for 8 rows * MSB nibble in KPP is for 8 cols */ reg_val = KPP_KPCR; reg_val |= (1 << 8) - 1; /* LSB */ reg_val |= ((1 << 8) - 1) << 8; /* MSB */ KPP_KPCR = reg_val; /* Write 0's to KPDR[15:8] */ reg_val = KPP_KPDR; reg_val &= 0x00ff; KPP_KPDR = reg_val; /* Configure columns as output, rows as input (KDDR[15:0]) */ reg_val = KPP_KDDR; reg_val |= 0xff00; reg_val &= 0xff00; KPP_KDDR = reg_val; reg_val = KPP_KPSR; reg_val &= ~(0x3); reg_val |= 0xD; KPP_KPSR = reg_val; reg_val |= (1 << 8); reg_val &= ~(1 << 9); KPP_KPSR = reg_val;}inline bool button_hold(void){ return GPIO3_DR & 0x10;}int button_read_device(void){ unsigned short reg_val; int col, row; short scancode = 0; int keycnt = 0; /* How many keys are still pressed */ static unsigned short *cur_rcmap;#if 0 /* save cur keypad matrix to prev */ memcpy(prev_rcmap, cur_rcmap, kpp_dev.kpp_rows * sizeof(prev_rcmap[0]));#endif// memset(cur_rcmap, 0, 5*sizeof(cur_rcmap[0])); cur_rcmap[0] = 0; cur_rcmap[1] = 0; cur_rcmap[2] = 0; cur_rcmap[3] = 0; cur_rcmap[4] = 0; for (col = 0; col < 8; col++) { /* Col */ snprintf(buf,sizeof(buf),"Pre-zerowalking: 0x%X", KPP_KPDR) lcd_puts(0,8,buf); /* 2. Write 1.s to KPDR[15:8] setting column data to 1.s */ reg_val = KPP_KPDR; reg_val |= 0xff00; KPP_KPDR = reg_val; /* * 3. Configure columns as totem pole outputs(for quick * discharging of keypad capacitance) */ reg_val = KPP_KPCR; reg_val &= 0x00ff; KPP_KPCR = reg_val; udelay(2); /* * 4. Configure columns as open-drain */ reg_val = KPP_KPCR; reg_val |= ((1 << 8) - 1) << 8; KPP_KPCR = reg_val; /* * 5. Write a single column to 0, others to 1. * 6. Sample row inputs and save data. Multiple key presses * can be detected on a single column. * 7. Repeat steps 2 - 6 for remaining columns. */ /* Col bit starts at 8th bit in KPDR */ reg_val = KPP_KPDR; reg_val &= ~(1 << (8 + col)); KPP_KPDR = reg_val; /* Delay added to avoid propagating the 0 from column to row * when scanning. */ udelay(2); /* Read row input */ reg_val = KPP_KPDR; for (row = 0; row < 5; row++) { /* sample row */ snprintf(buf,sizeof(buf),"Post zerowalking: 0x%X", reg_val); lcd_puts(0,9,buf); if (!(reg_val & (1 << row))) { cur_rcmap[row] |= (1 << col); DEBUGF("keypress at (%d,%d)",row,col); keycnt++; } } } /* * 8. Return all columns to 0 in preparation for standby mode. * 9. Clear KPKD and KPKR status bit(s) by writing to a .1., * set the KPKR synchronizer chain by writing "1" to KRSS register, * clear the KPKD synchronizer chain by writing "1" to KDSC register */ reg_val = 0x00; KPP_KPDR = reg_val; reg_val = KPP_KPDR; reg_val = KPP_KPSR; reg_val |= 0xF; KPP_KPSR = reg_val; return BUTTON_NONE;}