xref: /openbmc/u-boot/drivers/input/twl4030.c (revision 63e22517)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * TWL4030 input
4  *
5  * Copyright (C) 2015 Paul Kocialkowski <contact@paulk.fr>
6  */
7 
8 #include <twl4030.h>
9 
10 int twl4030_input_power_button(void)
11 {
12 	u8 data;
13 
14 	twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
15 			    TWL4030_PM_MASTER_STS_HW_CONDITIONS, &data);
16 
17 	if (data & TWL4030_PM_MASTER_STS_HW_CONDITIONS_PWON)
18 		return 1;
19 
20 	return 0;
21 }
22 
23 int twl4030_input_charger(void)
24 {
25 	u8 data;
26 
27 	twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
28 			    TWL4030_PM_MASTER_STS_HW_CONDITIONS, &data);
29 
30 	if (data & TWL4030_PM_MASTER_STS_HW_CONDITIONS_CHG)
31 		return 1;
32 
33 	return 0;
34 }
35 
36 int twl4030_input_usb(void)
37 {
38 	u8 data;
39 
40 	twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
41 			    TWL4030_PM_MASTER_STS_HW_CONDITIONS, &data);
42 
43 	if (data & TWL4030_PM_MASTER_STS_HW_CONDITIONS_USB ||
44 	    data & TWL4030_PM_MASTER_STS_HW_CONDITIONS_VBUS)
45 		return 1;
46 
47 	return 0;
48 }
49 
50 int twl4030_keypad_scan(unsigned char *matrix)
51 {
52 	u8 data;
53 	u8 c, r;
54 
55 	twl4030_i2c_read_u8(TWL4030_CHIP_KEYPAD,
56 			    TWL4030_KEYPAD_KEYP_CTRL_REG, &data);
57 
58 	data |= TWL4030_KEYPAD_CTRL_SOFT_NRST | TWL4030_KEYPAD_CTRL_KBD_ON;
59 	data &= ~TWL4030_KEYPAD_CTRL_SOFTMODEN;
60 
61 	twl4030_i2c_write_u8(TWL4030_CHIP_KEYPAD,
62 			     TWL4030_KEYPAD_KEYP_CTRL_REG, data);
63 
64 	for (c = 0; c < 8; c++) {
65 		data = 0xff & ~(1 << c);
66 		twl4030_i2c_write_u8(TWL4030_CHIP_KEYPAD,
67 				     TWL4030_KEYPAD_KBC_REG, data);
68 
69 		data = 0xff;
70 		twl4030_i2c_read_u8(TWL4030_CHIP_KEYPAD,
71 				    TWL4030_KEYPAD_KBR_REG, &data);
72 
73 		for (r = 0; r < 8; r++)
74 			matrix[c * 8 + r] = !(data & (1 << r));
75 	}
76 
77 	data = 0xff & ~(TWL4030_KEYPAD_CTRL_SOFT_NRST);
78 	twl4030_i2c_write_u8(TWL4030_CHIP_KEYPAD,
79 			     TWL4030_KEYPAD_KEYP_CTRL_REG, data);
80 
81 	return 0;
82 }
83 
84 int twl4030_keypad_key(unsigned char *matrix, u8 c, u8 r)
85 {
86 	return matrix[c * 8 + r];
87 }
88