xref: /rk3399_rockchip-uboot/drivers/input/rk8xx_pwrkey.c (revision ed73b76733ce7cd243da2ebec965b0e1fca8924d)
1 /*
2  * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <key.h>
10 #include <linux/input.h>
11 #include <power/pmic.h>
12 #include <power/rk8xx_pmic.h>
13 #include <irq-generic.h>
14 #include <asm/arch/periph.h>
15 #include <dm/pinctrl.h>
16 
17 #define	RK817_INT_STS_REG0	0xf8
18 #define	RK817_INT_MSK_REG0	0xf9
19 #define	RK817_INT_STS_REG1	0xfa
20 #define	RK817_INT_MSK_REG1	0xfb
21 #define	RK817_INT_STS_REG2	0xfc
22 #define	RK817_INT_MSK_REG2	0xfd
23 #define RK817_PWRON_RISE_INT	(1 << 1)
24 #define RK817_PWRON_FALL_INT	(1 << 0)
25 #define RK817_PLUG_OUT_INT	(1 << 1)
26 
27 #define	RK816_INT_STS_REG1	0x49
28 #define	RK816_INT_MSK_REG1	0x4a
29 #define	RK816_INT_STS_REG2	0x4c
30 #define	RK816_INT_MSK_REG2	0x4d
31 #define	RK816_INT_STS_REG3	0x4e
32 #define	RK816_INT_MSK_REG3	0x4f
33 #define RK816_PWRON_RISE_INT	(1 << 6)
34 #define RK816_PWRON_FALL_INT	(1 << 5)
35 #define RK816_PLUG_OUT_INT	(1 << 1)
36 
37 #define	RK805_INT_STS_REG	0x4c
38 #define	RK805_INT_MSK_REG	0x4d
39 #define RK805_PWRON_RISE_INT	(1 << 0)
40 #define RK805_PWRON_FALL_INT	(1 << 7)
41 
42 struct rk8xx_key_priv {
43 	u8 key_int_sts_reg;
44 	u8 key_int_msk_reg;
45 	u8 plug_int_sts_reg;
46 	u8 plug_int_msk_reg;
47 	u8 pwron_rise_int;
48 	u8 pwron_fall_int;
49 	u8 plug_out_int;
50 	struct reg_data *init_reg;
51 	u32 init_reg_num;
52 	struct reg_data *irq_reg;
53 	u32 irq_reg_num;
54 };
55 
56 static struct reg_data rk817_init_reg[] = {
57 	/* only enable rise/fall interrupt, plugout */
58 	{ RK817_INT_MSK_REG0, 0xfc },
59 	{ RK817_INT_MSK_REG1, 0xfd },
60 	{ RK817_INT_MSK_REG2, 0xff },
61 	/* clear all interrupt states */
62 	{ RK817_INT_STS_REG0, 0xff },
63 	{ RK817_INT_STS_REG1, 0xff },
64 	{ RK817_INT_STS_REG2, 0xff },
65 };
66 
67 static struct reg_data rk817_irq_reg[] = {
68 	/* clear all interrupt states */
69 	{ RK817_INT_STS_REG0, 0xff },
70 	{ RK817_INT_STS_REG1, 0xff },
71 	{ RK817_INT_STS_REG2, 0xff },
72 };
73 
74 static struct reg_data rk816_init_reg[] = {
75 	/* only enable rise/fall interrupt, plugout */
76 	{ RK816_INT_MSK_REG1, 0x9f },
77 	{ RK816_INT_MSK_REG2, 0xff },
78 	{ RK816_INT_MSK_REG3, 0xfd },
79 	/* clear all interrupt states */
80 	{ RK816_INT_STS_REG1, 0xff },
81 	{ RK816_INT_STS_REG2, 0xff },
82 	{ RK816_INT_STS_REG3, 0xff },
83 };
84 
85 static struct reg_data rk816_irq_reg[] = {
86 	/* clear all interrupt states */
87 	{ RK816_INT_STS_REG1, 0xff },
88 	{ RK816_INT_STS_REG2, 0xff },
89 	{ RK816_INT_STS_REG3, 0xff },
90 };
91 
92 static struct reg_data rk805_irq_reg[] = {
93 	/* clear all interrupt states */
94 	{ RK805_INT_STS_REG, 0xff },
95 };
96 
97 static struct reg_data rk805_init_reg[] = {
98 	/* only enable rise/fall interrupt */
99 	{ RK805_INT_MSK_REG, 0x7e },
100 	/* clear all interrupt states */
101 	{ RK805_INT_STS_REG, 0xff },
102 };
103 
104 static void pwrkey_irq_handler(int irq, void *data)
105 {
106 	struct udevice *dev = data;
107 	struct rk8xx_key_priv *priv = dev_get_priv(dev);
108 	struct dm_key_uclass_platdata *uc_key = dev_get_uclass_platdata(dev);
109 	int ret, val, i;
110 
111 	debug("%s: irq = %d\n", __func__, irq);
112 
113 	/*
114 	 * This plug out interrupt only used to wakeup cpu while U-Boot
115 	 * charging and system suspend. Because we need to detect charger
116 	 * plug out event and then shutdown system.
117 	 */
118 	if (priv->plug_int_sts_reg) {
119 		val = pmic_reg_read(dev->parent, priv->plug_int_sts_reg);
120 		if (val < 0) {
121 			printf("%s: i2c read failed, ret=%d\n", __func__, val);
122 			return;
123 		}
124 
125 		if (val & priv->plug_out_int)
126 			printf("Plug out interrupt\n");
127 	}
128 
129 	/* read key status */
130 	val = pmic_reg_read(dev->parent, priv->key_int_sts_reg);
131 	if (val < 0) {
132 		printf("%s: i2c read failed, ret=%d\n", __func__, val);
133 		return;
134 	}
135 
136 	/* fall event */
137 	if (val & priv->pwron_fall_int) {
138 		uc_key->fall_ms = key_timer(0);
139 		debug("%s: key down: %llu ms\n", __func__, uc_key->fall_ms);
140 	}
141 
142 	/* rise event */
143 	if (val & priv->pwron_rise_int) {
144 		uc_key->rise_ms = key_timer(0);
145 		debug("%s: key up: %llu ms\n", __func__, uc_key->rise_ms);
146 	}
147 
148 	/* clear intertup */
149 	for (i = 0; i < priv->irq_reg_num; i++) {
150 		ret = pmic_reg_write(dev->parent,
151 				     priv->irq_reg[i].reg,
152 				     priv->irq_reg[i].val);
153 		if (ret < 0) {
154 			printf("%s: i2c write reg 0x%x failed, ret=%d\n",
155 			       __func__, priv->irq_reg[i].reg, ret);
156 		}
157 
158 		debug("%s: reg[0x%x] = 0x%x\n", __func__, priv->irq_reg[i].reg,
159 		      pmic_reg_read(dev->parent, priv->irq_reg[i].reg));
160 	}
161 }
162 
163 static int pwrkey_interrupt_init(struct udevice *dev)
164 {
165 	struct dm_key_uclass_platdata *uc_key = dev_get_uclass_platdata(dev);
166 	u32 interrupt[2], phandle;
167 	int irq, ret;
168 
169 	phandle = dev_read_u32_default(dev->parent, "interrupt-parent", -1);
170 	if (phandle < 0) {
171 		printf("read 'interrupt-parent' failed, ret=%d\n", phandle);
172 		return phandle;
173 	}
174 
175 	ret = dev_read_u32_array(dev->parent, "interrupts", interrupt, 2);
176 	if (ret) {
177 		printf("read 'interrupt' failed, ret=%d\n", ret);
178 		return ret;
179 	}
180 
181 	uc_key->name = "rk8xx_pwr";
182 	uc_key->type = GPIO_KEY;
183 	uc_key->code = KEY_POWER;
184 	irq = phandle_gpio_to_irq(phandle, interrupt[0]);
185 	if (irq < 0) {
186 		printf("%s: request irq failed, ret=%d\n", uc_key->name, irq);
187 		return irq;
188 	}
189 	uc_key->irq = irq;
190 	irq_install_handler(irq, pwrkey_irq_handler, dev);
191 	irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
192 	irq_handler_enable(irq);
193 
194 	return 0;
195 }
196 
197 static int rk8xx_pwrkey_probe(struct udevice *dev)
198 {
199 	struct rk8xx_priv *rk8xx = dev_get_priv(dev->parent);
200 	struct rk8xx_key_priv *priv = dev_get_priv(dev);
201 	int ret, i;
202 
203 	switch (rk8xx->variant) {
204 	case RK805_ID:
205 		priv->key_int_sts_reg = RK805_INT_STS_REG;
206 		priv->key_int_msk_reg = RK805_INT_MSK_REG;
207 		priv->pwron_rise_int = RK805_PWRON_RISE_INT;
208 		priv->pwron_fall_int = RK805_PWRON_FALL_INT;
209 		priv->init_reg = rk805_init_reg;
210 		priv->init_reg_num = ARRAY_SIZE(rk805_init_reg);
211 		priv->irq_reg = rk805_irq_reg;
212 		priv->irq_reg_num = ARRAY_SIZE(rk805_irq_reg);
213 		break;
214 
215 	case RK816_ID:
216 		priv->key_int_sts_reg = RK816_INT_STS_REG1;
217 		priv->key_int_msk_reg = RK816_INT_MSK_REG1;
218 		priv->plug_int_sts_reg = RK816_INT_STS_REG3;
219 		priv->plug_int_msk_reg = RK816_INT_MSK_REG3;
220 		priv->pwron_rise_int = RK816_PWRON_RISE_INT;
221 		priv->pwron_fall_int = RK816_PWRON_FALL_INT;
222 		priv->plug_out_int = RK816_PLUG_OUT_INT;
223 		priv->init_reg = rk816_init_reg;
224 		priv->init_reg_num = ARRAY_SIZE(rk816_init_reg);
225 		priv->irq_reg = rk816_irq_reg;
226 		priv->irq_reg_num = ARRAY_SIZE(rk816_irq_reg);
227 		break;
228 	case RK809_ID:
229 	case RK817_ID:
230 		priv->key_int_sts_reg = RK817_INT_STS_REG0;
231 		priv->key_int_msk_reg = RK817_INT_MSK_REG0;
232 		priv->plug_int_sts_reg = RK817_INT_STS_REG1;
233 		priv->plug_int_msk_reg = RK817_INT_MSK_REG1;
234 		priv->pwron_rise_int = RK817_PWRON_RISE_INT;
235 		priv->pwron_fall_int = RK817_PWRON_FALL_INT;
236 		priv->plug_out_int = RK817_PLUG_OUT_INT;
237 		priv->init_reg = rk817_init_reg;
238 		priv->init_reg_num = ARRAY_SIZE(rk817_init_reg);
239 		priv->irq_reg = rk817_irq_reg;
240 		priv->irq_reg_num = ARRAY_SIZE(rk817_irq_reg);
241 		break;
242 	default:
243 		return -EINVAL;
244 	}
245 
246 	/* mask and clear interrupt */
247 	for (i = 0; i < priv->init_reg_num; i++) {
248 		ret = pmic_reg_write(dev->parent,
249 				     priv->init_reg[i].reg,
250 				     priv->init_reg[i].val);
251 		if (ret < 0) {
252 			printf("%s: i2c write reg 0x%x failed, ret=%d\n",
253 			       __func__, priv->init_reg[i].reg, ret);
254 			return ret;
255 		}
256 
257 		debug("%s: reg[%x] = 0x%x\n", __func__, priv->init_reg[i].reg,
258 		      pmic_reg_read(dev->parent, priv->init_reg[i].reg));
259 	}
260 
261 	return pwrkey_interrupt_init(dev);
262 }
263 
264 U_BOOT_DRIVER(rk8xx_pwrkey) = {
265 	.name   = "rk8xx_pwrkey",
266 	.id     = UCLASS_KEY,
267 	.probe  = rk8xx_pwrkey_probe,
268 	.priv_auto_alloc_size = sizeof(struct rk8xx_key_priv),
269 };
270