xref: /OK3568_Linux_fs/kernel/drivers/input/remotectl/rockchip_pwm_remotectl.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #include <linux/clk.h>
4 #include <linux/io.h>
5 #include <linux/irq.h>
6 #include <linux/irqdesc.h>
7 #include <linux/irqdomain.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/platform_device.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <linux/input.h>
16 #include <linux/workqueue.h>
17 #include <linux/wakelock.h>
18 #include <linux/slab.h>
19 #include <linux/rockchip/rockchip_sip.h>
20 #include "rockchip_pwm_remotectl.h"
21 
22 
23 
24 /*
25  * sys/module/rk_pwm_remotectl/parameters,
26  * modify code_print to change the value
27  */
28 
29 static int rk_remote_print_code;
30 module_param_named(code_print, rk_remote_print_code, int, 0644);
31 #define DBG_CODE(args...) \
32 	do { \
33 		if (rk_remote_print_code) { \
34 			pr_info(args); \
35 		} \
36 	} while (0)
37 
38 static int rk_remote_pwm_dbg_level;
39 module_param_named(dbg_level, rk_remote_pwm_dbg_level, int, 0644);
40 #define DBG(args...) \
41 	do { \
42 		if (rk_remote_pwm_dbg_level) { \
43 			pr_info(args); \
44 		} \
45 	} while (0)
46 
47 
48 struct rkxx_remote_key_table {
49 	int scancode;
50 	int keycode;
51 };
52 
53 struct rkxx_remotectl_button {
54 	int usercode;
55 	int nbuttons;
56 	struct rkxx_remote_key_table key_table[MAX_NUM_KEYS];
57 };
58 
59 struct rkxx_remotectl_drvdata {
60 	void __iomem *base;
61 	int state;
62 	int nbuttons;
63 	int scandata;
64 	int count;
65 	int keynum;
66 	int maxkeybdnum;
67 	int keycode;
68 	int press;
69 	int pre_press;
70 	int irq;
71 	int remote_pwm_id;
72 	int handle_cpu_id;
73 	int wakeup;
74 	int support_psci;
75 	int pwm_pwrkey_capture;
76 	unsigned long period;
77 	unsigned long temp_period;
78 	int pwm_freq_nstime;
79 	int pwrkey_wakeup;
80 	struct input_dev *input;
81 	struct timer_list timer;
82 	struct tasklet_struct remote_tasklet;
83 	struct wake_lock remotectl_wake_lock;
84 };
85 
86 static struct rkxx_remotectl_button *remotectl_button;
87 
remotectl_keybd_num_lookup(struct rkxx_remotectl_drvdata * ddata)88 static int remotectl_keybd_num_lookup(struct rkxx_remotectl_drvdata *ddata)
89 {
90 	int i;
91 	int num;
92 
93 	num = ddata->maxkeybdnum;
94 	for (i = 0; i < num; i++) {
95 		if (remotectl_button[i].usercode == (ddata->scandata&0xFFFF)) {
96 			ddata->keynum = i;
97 			return 1;
98 		}
99 	}
100 	return 0;
101 }
102 
103 
remotectl_keycode_lookup(struct rkxx_remotectl_drvdata * ddata)104 static int remotectl_keycode_lookup(struct rkxx_remotectl_drvdata *ddata)
105 {
106 	int i;
107 	unsigned char keydata = (unsigned char)((ddata->scandata >> 8) & 0xff);
108 
109 	for (i = 0; i < remotectl_button[ddata->keynum].nbuttons; i++) {
110 		if (remotectl_button[ddata->keynum].key_table[i].scancode ==
111 		    keydata) {
112 			ddata->keycode =
113 			remotectl_button[ddata->keynum].key_table[i].keycode;
114 			return 1;
115 		}
116 	}
117 	return 0;
118 }
119 
rk_remotectl_get_irkeybd_count(struct platform_device * pdev)120 static int rk_remotectl_get_irkeybd_count(struct platform_device *pdev)
121 {
122 	struct device_node *node = pdev->dev.of_node;
123 	struct device_node *child_node;
124 	int boardnum;
125 	int temp_usercode;
126 
127 	boardnum = 0;
128 	for_each_child_of_node(node, child_node) {
129 		if (of_property_read_u32(child_node, "rockchip,usercode",
130 			&temp_usercode)) {
131 			DBG("get keybd num error.\n");
132 		} else {
133 			boardnum++;
134 		}
135 	}
136 	DBG("get keybd num = 0x%x.\n", boardnum);
137 	return boardnum;
138 }
139 
140 
rk_remotectl_parse_ir_keys(struct platform_device * pdev)141 static int rk_remotectl_parse_ir_keys(struct platform_device *pdev)
142 {
143 	struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
144 	struct device_node *node = pdev->dev.of_node;
145 	struct device_node *child_node;
146 	int loop;
147 	int ret;
148 	int len;
149 	int boardnum;
150 
151 	boardnum = 0;
152 	for_each_child_of_node(node, child_node) {
153 		if (of_property_read_u32(child_node, "rockchip,usercode",
154 			 &remotectl_button[boardnum].usercode)) {
155 			dev_err(&pdev->dev, "Missing usercode in the DTS.\n");
156 			ret = -1;
157 			return ret;
158 		}
159 		DBG("remotectl_button[0].usercode=0x%x\n",
160 				remotectl_button[boardnum].usercode);
161 		of_get_property(child_node, "rockchip,key_table", &len);
162 		len /= sizeof(u32);
163 		DBG("len=0x%x\n", len);
164 		remotectl_button[boardnum].nbuttons = len/2;
165 		if (of_property_read_u32_array(child_node, "rockchip,key_table",
166 			 (u32 *)remotectl_button[boardnum].key_table, len)) {
167 			dev_err(&pdev->dev, "Missing key_table in the DTS.\n");
168 			ret = -1;
169 			return ret;
170 		}
171 		for (loop = 0; loop < (len/2); loop++) {
172 			DBG("board[%d].scanCode[%d]=0x%x\n", boardnum, loop,
173 			     remotectl_button[boardnum].key_table[loop].scancode);
174 			DBG("board[%d].keyCode[%d]=%d\n", boardnum, loop,
175 			     remotectl_button[boardnum].key_table[loop].keycode);
176 		}
177 		boardnum++;
178 		if (boardnum > ddata->maxkeybdnum)
179 			break;
180 	}
181 	DBG("keybdNum=0x%x\n", boardnum);
182 	return 0;
183 }
184 
185 
186 
rk_pwm_remotectl_do_something(unsigned long data)187 static void rk_pwm_remotectl_do_something(unsigned long  data)
188 {
189 	struct rkxx_remotectl_drvdata *ddata;
190 
191 	ddata = (struct rkxx_remotectl_drvdata *)data;
192 	switch (ddata->state) {
193 	case RMC_IDLE: {
194 		;
195 		break;
196 	}
197 	case RMC_PRELOAD: {
198 		mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(140));
199 		if ((ddata->period > RK_PWM_TIME_PRE_MIN) &&
200 		    (ddata->period < RK_PWM_TIME_PRE_MAX)) {
201 			ddata->scandata = 0;
202 			ddata->count = 0;
203 			ddata->state = RMC_USERCODE;
204 		} else {
205 			ddata->state = RMC_PRELOAD;
206 		}
207 		break;
208 	}
209 	case RMC_USERCODE: {
210 		if ((ddata->period > RK_PWM_TIME_BIT1_MIN) &&
211 		    (ddata->period < RK_PWM_TIME_BIT1_MAX))
212 			ddata->scandata |= (0x01 << ddata->count);
213 		ddata->count++;
214 		if (ddata->count == 0x10) {
215 			DBG_CODE("USERCODE=0x%x\n", ddata->scandata);
216 			if (remotectl_keybd_num_lookup(ddata)) {
217 				ddata->state = RMC_GETDATA;
218 				ddata->scandata = 0;
219 				ddata->count = 0;
220 			} else {
221 				if (rk_remote_print_code) {
222 					ddata->state = RMC_GETDATA;
223 					ddata->scandata = 0;
224 					ddata->count = 0;
225 				} else
226 					ddata->state = RMC_PRELOAD;
227 			}
228 		}
229 	}
230 	break;
231 	case RMC_GETDATA: {
232 		if ((ddata->period > RK_PWM_TIME_BIT1_MIN) &&
233 		    (ddata->period < RK_PWM_TIME_BIT1_MAX))
234 			ddata->scandata |= (0x01<<ddata->count);
235 		ddata->count++;
236 		if (ddata->count < 0x10)
237 			return;
238 		DBG_CODE("RMC_GETDATA=%x\n", (ddata->scandata>>8));
239 		if ((ddata->scandata&0x0ff) ==
240 		    ((~ddata->scandata >> 8) & 0x0ff)) {
241 			if (remotectl_keycode_lookup(ddata)) {
242 				ddata->press = 1;
243 				input_event(ddata->input, EV_KEY,
244 					    ddata->keycode, 1);
245 				input_sync(ddata->input);
246 				ddata->state = RMC_SEQUENCE;
247 			} else {
248 				ddata->state = RMC_PRELOAD;
249 			}
250 		} else {
251 			ddata->state = RMC_PRELOAD;
252 		}
253 	}
254 	break;
255 	case RMC_SEQUENCE:{
256 		DBG("S=%ld\n", ddata->period);
257 		if ((ddata->period > RK_PWM_TIME_RPT_MIN) &&
258 		    (ddata->period < RK_PWM_TIME_RPT_MAX)) {
259 			DBG("S1\n");
260 			mod_timer(&ddata->timer, jiffies
261 				  + msecs_to_jiffies(130));
262 		} else if ((ddata->period > RK_PWM_TIME_SEQ1_MIN) &&
263 			   (ddata->period < RK_PWM_TIME_SEQ1_MAX)) {
264 			DBG("S2\n");
265 			mod_timer(&ddata->timer, jiffies
266 				  + msecs_to_jiffies(130));
267 		} else if ((ddata->period > RK_PWM_TIME_SEQ2_MIN) &&
268 			   (ddata->period < RK_PWM_TIME_SEQ2_MAX)) {
269 			DBG("S3\n");
270 			mod_timer(&ddata->timer, jiffies
271 				  + msecs_to_jiffies(130));
272 		} else {
273 			DBG("S4\n");
274 			input_event(ddata->input, EV_KEY,
275 				    ddata->keycode, 0);
276 			input_sync(ddata->input);
277 			ddata->state = RMC_PRELOAD;
278 			ddata->press = 0;
279 		}
280 	}
281 	break;
282 	default:
283 	break;
284 	}
285 }
286 
rk_pwm_remotectl_timer(struct timer_list * t)287 static void rk_pwm_remotectl_timer(struct timer_list *t)
288 {
289 	struct rkxx_remotectl_drvdata *ddata = from_timer(ddata, t, timer);
290 
291 	if (ddata->press != ddata->pre_press) {
292 		ddata->pre_press = 0;
293 		ddata->press = 0;
294 		input_event(ddata->input, EV_KEY, ddata->keycode, 0);
295 		input_sync(ddata->input);
296 	}
297 	ddata->state = RMC_PRELOAD;
298 }
299 
rockchip_pwm_pwrirq(int irq,void * dev_id)300 static irqreturn_t rockchip_pwm_pwrirq(int irq, void *dev_id)
301 {
302 	struct rkxx_remotectl_drvdata *ddata = dev_id;
303 	int val;
304 	unsigned int id = ddata->remote_pwm_id;
305 
306 	if (id > 3)
307 		return IRQ_NONE;
308 
309 	val = readl_relaxed(ddata->base + PWM_REG_INTSTS(id));
310 
311 	if (val & PWM_PWR_KEY_INT) {
312 		DBG("pwr=0x%x\n", readl_relaxed(ddata->base + PWM_PWRCAPTURE_VALUE(id)));
313 		writel_relaxed(PWM_PWR_KEY_INT, ddata->base + PWM_REG_INTSTS(id));
314 		ddata->pwrkey_wakeup = 1;
315 		return IRQ_HANDLED;
316 	}
317 
318 	return IRQ_NONE;
319 }
320 
rockchip_pwm_irq(int irq,void * dev_id)321 static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id)
322 {
323 	struct rkxx_remotectl_drvdata *ddata = dev_id;
324 	int val;
325 	int temp_hpr;
326 	int temp_lpr;
327 	int temp_period;
328 	unsigned int id = ddata->remote_pwm_id;
329 
330 	if (id > 3)
331 		return IRQ_NONE;
332 	val = readl_relaxed(ddata->base + PWM_REG_INTSTS(id));
333 
334 	if ((val & PWM_CH_INT(id)) == 0)
335 		return IRQ_NONE;
336 	if ((val & PWM_CH_POL(id)) == 0) {
337 		temp_hpr = readl_relaxed(ddata->base + PWM_REG_HPR);
338 		writel_relaxed(0, ddata->base + PWM_REG_HPR);
339 		temp_lpr = readl_relaxed(ddata->base + PWM_REG_LPR);
340 		writel_relaxed(0, ddata->base + PWM_REG_LPR);
341 		DBG("hpr=%d\n", temp_hpr);
342 		DBG("lpr=%d\n", temp_lpr);
343 
344 		temp_period = ddata->pwm_freq_nstime * temp_lpr / 1000;
345 		if (temp_period > RK_PWM_TIME_BIT0_MIN) {
346 			ddata->period = ddata->temp_period
347 			    + ddata->pwm_freq_nstime * temp_hpr / 1000;
348 			tasklet_hi_schedule(&ddata->remote_tasklet);
349 			ddata->temp_period = 0;
350 			DBG("period+ =%ld\n", ddata->period);
351 		} else {
352 			ddata->temp_period += ddata->pwm_freq_nstime
353 			    * (temp_hpr + temp_lpr) / 1000;
354 		}
355 	}
356 	writel_relaxed(PWM_CH_INT(id), ddata->base + PWM_REG_INTSTS(id));
357 	if (ddata->state == RMC_PRELOAD)
358 		wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
359 	return IRQ_HANDLED;
360 }
361 
rk_pwm_pwrkey_wakeup_init(struct platform_device * pdev)362 static int rk_pwm_pwrkey_wakeup_init(struct platform_device *pdev)
363 {
364 	struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
365 	int val, min_temp, max_temp;
366 	unsigned int pwm_id = ddata->remote_pwm_id;
367 	int version;
368 	int i, j;
369 	int num = 0;
370 	int ret = -1;
371 	int pwr_irq;
372 
373 	ddata->pwm_pwrkey_capture = 0;
374 	version = readl_relaxed(ddata->base + RK_PWM_VERSION_ID(pwm_id));
375 	dev_info(&pdev->dev, "pwm version is 0x%x\n", version & 0xffff0000);
376 	if (((version >> 24) & 0xFF) < 2) {
377 		dev_info(&pdev->dev, "pwm version is less v2.0\n");
378 		goto end;
379 	}
380 	pwr_irq = platform_get_irq(pdev, 1);
381 	if (pwr_irq < 0) {
382 		dev_err(&pdev->dev, "cannot find PWR IRQ\n");
383 		goto end;
384 	}
385 	ret = devm_request_irq(&pdev->dev, pwr_irq, rockchip_pwm_pwrirq,
386 			       IRQF_NO_SUSPEND, "rk_pwm_pwr_irq", ddata);
387 	if (ret) {
388 		dev_err(&pdev->dev, "cannot claim PWR_IRQ!!!\n");
389 		goto end;
390 	}
391 	val = readl_relaxed(ddata->base + PWM_REG_CTRL);
392 	val = (val & 0xFFFFFFFE) | PWM_DISABLE;
393 	writel_relaxed(val, ddata->base + PWM_REG_CTRL);
394 
395 	//preloader low min:8000us, max:10000us
396 	min_temp = RK_PWM_TIME_PRE_MIN_LOW * 1000 / ddata->pwm_freq_nstime;
397 	max_temp = RK_PWM_TIME_PRE_MAX_LOW * 1000 / ddata->pwm_freq_nstime;
398 	val = (max_temp & 0xffff) << 16 | (min_temp & 0xffff);
399 	writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_LPRE(pwm_id));
400 
401 	//preloader higt min:4000us, max:5000us
402 	min_temp = RK_PWM_TIME_PRE_MIN * 1000 / ddata->pwm_freq_nstime;
403 	max_temp = RK_PWM_TIME_PRE_MAX * 1000 / ddata->pwm_freq_nstime;
404 	val = (max_temp & 0xffff) << 16 | (min_temp & 0xffff);
405 	writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_HPRE(pwm_id));
406 
407 	//logic 0/1 low min:480us, max 700us
408 	min_temp = RK_PWM_TIME_BIT_MIN_LOW * 1000 / ddata->pwm_freq_nstime;
409 	max_temp = RK_PWM_TIME_BIT_MAX_LOW * 1000 / ddata->pwm_freq_nstime;
410 	val = (max_temp & 0xffff) << 16 | (min_temp & 0xffff);
411 	writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_LD(pwm_id));
412 
413 	//logic 0 higt min:480us, max 700us
414 	min_temp = RK_PWM_TIME_BIT0_MIN * 1000 / ddata->pwm_freq_nstime;
415 	max_temp = RK_PWM_TIME_BIT0_MAX * 1000 / ddata->pwm_freq_nstime;
416 	val = (max_temp & 0xffff) << 16 | (min_temp & 0xffff);
417 	writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_HD_ZERO(pwm_id));
418 
419 	//logic 1 higt min:1300us, max 2000us
420 	min_temp = RK_PWM_TIME_BIT1_MIN * 1000 / ddata->pwm_freq_nstime;
421 	max_temp = RK_PWM_TIME_BIT1_MAX * 1000 / ddata->pwm_freq_nstime;
422 	val = (max_temp & 0xffff) << 16 | (min_temp & 0xffff);
423 	writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_HD_ONE(pwm_id));
424 
425 	for (j = 0; j < ddata->maxkeybdnum; j++) {
426 		for (i = 0; i < remotectl_button[j].nbuttons; i++) {
427 			int scancode, usercode, pwrkey;
428 
429 			if (remotectl_button[j].key_table[i].keycode != KEY_POWER)
430 				continue;
431 			usercode = remotectl_button[j].usercode & 0xffff;
432 			scancode = remotectl_button[j].key_table[i].scancode & 0xff;
433 			DBG("usercode=%x, key=%x\n", usercode, scancode);
434 			pwrkey  = usercode;
435 			pwrkey |= (scancode << 24) | ((~scancode & 0xff) << 16);
436 			DBG("pwrkey = %x\n", pwrkey);
437 			writel_relaxed(pwrkey, ddata->base
438 					+ PWM_PWRMATCH_VALUE(pwm_id) + num * 4);
439 			num++;
440 			if (num >= PWM_PWR_KEY_CAPURURE_MAX)
441 				break;
442 		}
443 	}
444 
445 	val = readl_relaxed(ddata->base + PWM_REG_INT_EN(pwm_id));
446 	val = (val & 0xFFFFFF7F) | PWM_PWR_INT_ENABLE;
447 	writel_relaxed(val, ddata->base + PWM_REG_INT_EN(pwm_id));
448 
449 	val = CH3_PWRKEY_ENABLE;
450 	writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_CTRL(pwm_id));
451 
452 	val = readl_relaxed(ddata->base + PWM_REG_CTRL);
453 	val = (val & 0xFFFFFFFE) | PWM_ENABLE;
454 	writel_relaxed(val, ddata->base + PWM_REG_CTRL);
455 	ddata->pwm_pwrkey_capture = 1;
456 end:
457 	return ret;
458 }
459 
rk_pwm_int_ctrl(void __iomem * pwm_base,uint pwm_id,int ctrl)460 static void rk_pwm_int_ctrl(void __iomem *pwm_base, uint pwm_id, int ctrl)
461 {
462 	int val;
463 
464 	if (pwm_id > 3)
465 		return;
466 	val = readl_relaxed(pwm_base + PWM_REG_INT_EN(pwm_id));
467 	if (ctrl) {
468 		val |= PWM_CH_INT_ENABLE(pwm_id);
469 		DBG("pwm int enabled, value is 0x%x\n", val);
470 		writel_relaxed(val, pwm_base + PWM_REG_INT_EN(pwm_id));
471 	} else {
472 		val &= ~PWM_CH_INT_ENABLE(pwm_id);
473 		DBG("pwm int disabled, value is 0x%x\n", val);
474 	}
475 	writel_relaxed(val, pwm_base + PWM_REG_INT_EN(pwm_id));
476 }
477 
rk_pwm_remotectl_hw_init(void __iomem * pwm_base,uint pwm_id)478 static int rk_pwm_remotectl_hw_init(void __iomem *pwm_base, uint pwm_id)
479 {
480 	int val;
481 
482 	if (pwm_id > 3)
483 		return -1;
484 	//1. disabled pwm
485 	val = readl_relaxed(pwm_base + PWM_REG_CTRL);
486 	val = (val & 0xFFFFFFFE) | PWM_DISABLE;
487 	writel_relaxed(val, pwm_base + PWM_REG_CTRL);
488 	//2. capture mode
489 	val = readl_relaxed(pwm_base + PWM_REG_CTRL);
490 	val = (val & 0xFFFFFFF9) | PWM_MODE_CAPTURE;
491 	writel_relaxed(val, pwm_base + PWM_REG_CTRL);
492 	//set clk div, clk div to 64
493 	val = readl_relaxed(pwm_base + PWM_REG_CTRL);
494 	val = (val & 0xFF0001FF) | PWM_DIV64;
495 	writel_relaxed(val, pwm_base + PWM_REG_CTRL);
496 	//4. enabled pwm int
497 	rk_pwm_int_ctrl(pwm_base, pwm_id, PWM_INT_ENABLE);
498 	//5. enabled pwm
499 	val = readl_relaxed(pwm_base + PWM_REG_CTRL);
500 	val = (val & 0xFFFFFFFE) | PWM_ENABLE;
501 	writel_relaxed(val, pwm_base + PWM_REG_CTRL);
502 	return 0;
503 }
504 
rk_pwm_sip_wakeup_init(struct platform_device * pdev)505 static int rk_pwm_sip_wakeup_init(struct platform_device *pdev)
506 {
507 	struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
508 	struct device_node *np = pdev->dev.of_node;
509 	struct irq_desc *desc;
510 	int support_psci = 0;
511 	int irq;
512 	int hwirq;
513 	int i, j;
514 	int num;
515 	int pwm_id;
516 	int ret = -1;
517 
518 	if (of_property_read_u32(np, "remote_support_psci", &support_psci)) {
519 		dev_info(&pdev->dev, "PWM Missing psci property in the DT.\n");
520 		goto end;
521 	}
522 	DBG("support_psci=0x%x\n", support_psci);
523 	if (!support_psci)
524 		goto end;
525 	irq = ddata->irq;
526 	desc = irq_to_desc(irq);
527 	if (!desc || !desc->irq_data.domain)
528 		goto end;
529 	hwirq = desc->irq_data.hwirq;
530 	ret = sip_smc_remotectl_config(REMOTECTL_SET_IRQ, hwirq);
531 	if (ret) {
532 		dev_err(&pdev->dev, "set irq err, set support_psci to 0 !!\n");
533 		/*
534 		 * if atf doesn't support, return probe success to abandon atf
535 		 * function and still use kernel pwm parse function
536 		 */
537 		goto end;
538 	}
539 	pwm_id = ddata->remote_pwm_id;
540 	num = ddata->maxkeybdnum;
541 	sip_smc_remotectl_config(REMOTECTL_SET_PWM_CH, pwm_id);
542 	for (j = 0; j < num; j++) {
543 		for (i = 0; i < remotectl_button[j].nbuttons; i++) {
544 			int scancode, pwrkey;
545 
546 			if (remotectl_button[j].key_table[i].keycode
547 			    != KEY_POWER)
548 				continue;
549 			scancode = remotectl_button[j].key_table[i].scancode;
550 			DBG("usercode=%x, key=%x\n",
551 			    remotectl_button[j].usercode, scancode);
552 			pwrkey = (remotectl_button[j].usercode & 0xffff) << 16;
553 			pwrkey |= (scancode & 0xff) << 8;
554 			DBG("deliver: key=%x\n", pwrkey);
555 			sip_smc_remotectl_config(REMOTECTL_SET_PWRKEY,
556 							pwrkey);
557 		}
558 	}
559 	sip_smc_remotectl_config(REMOTECTL_ENABLE, 1);
560 	ddata->support_psci = support_psci;
561 	DBG("rk pwm sip init end!\n");
562 	return 0;
563 end:
564 	dev_info(&pdev->dev, "pwm sip wakeup config error!!\n");
565 	ddata->support_psci = 0;
566 	return ret;
567 }
568 
rk_pwm_wakeup(struct input_dev * input)569 static inline void rk_pwm_wakeup(struct input_dev *input)
570 {
571 	input_event(input, EV_KEY, KEY_POWER, 1);
572 	input_event(input, EV_KEY, KEY_POWER, 0);
573 	input_sync(input);
574 }
575 
rk_pwm_probe(struct platform_device * pdev)576 static int rk_pwm_probe(struct platform_device *pdev)
577 {
578 	struct rkxx_remotectl_drvdata *ddata;
579 	struct device_node *np = pdev->dev.of_node;
580 	struct resource *r;
581 	struct input_dev *input;
582 	struct clk *clk;
583 	struct clk *p_clk;
584 	struct cpumask cpumask;
585 	int num;
586 	int irq;
587 	int ret;
588 	int i, j;
589 	int cpu_id;
590 	int pwm_id;
591 	int pwm_freq;
592 	int count;
593 
594 	pr_err(".. rk pwm remotectl v2.0 init\n");
595 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
596 	if (!r) {
597 		dev_err(&pdev->dev, "no memory resources defined\n");
598 		return -ENODEV;
599 	}
600 	ddata = devm_kzalloc(&pdev->dev, sizeof(struct rkxx_remotectl_drvdata),
601 			     GFP_KERNEL);
602 	if (!ddata) {
603 		dev_err(&pdev->dev, "failed to allocate memory\n");
604 		return -ENOMEM;
605 	}
606 	ddata->state = RMC_PRELOAD;
607 	ddata->temp_period = 0;
608 	ddata->base = devm_ioremap_resource(&pdev->dev, r);
609 	if (IS_ERR(ddata->base))
610 		return PTR_ERR(ddata->base);
611 	count = of_property_count_strings(np, "clock-names");
612 	if (count == 2) {
613 		clk = devm_clk_get(&pdev->dev, "pwm");
614 		p_clk = devm_clk_get(&pdev->dev, "pclk");
615 	} else {
616 		clk = devm_clk_get(&pdev->dev, NULL);
617 		p_clk = clk;
618 	}
619 	if (IS_ERR(clk)) {
620 		ret = PTR_ERR(clk);
621 		if (ret != -EPROBE_DEFER)
622 			dev_err(&pdev->dev, "Can't get bus clk: %d\n", ret);
623 		return ret;
624 	}
625 	if (IS_ERR(p_clk)) {
626 		ret = PTR_ERR(p_clk);
627 		if (ret != -EPROBE_DEFER)
628 			dev_err(&pdev->dev, "Can't get periph clk: %d\n", ret);
629 		return ret;
630 	}
631 	ret = clk_prepare_enable(clk);
632 	if (ret) {
633 		dev_err(&pdev->dev, "Can't enable bus clk: %d\n", ret);
634 		return ret;
635 	}
636 	ret = clk_prepare_enable(p_clk);
637 	if (ret) {
638 		dev_err(&pdev->dev, "Can't enable bus periph clk: %d\n", ret);
639 		goto error_clk;
640 	}
641 	platform_set_drvdata(pdev, ddata);
642 	num = rk_remotectl_get_irkeybd_count(pdev);
643 	if (num == 0) {
644 		pr_err("remotectl: no ir keyboard add in dts!!\n");
645 		ret = -EINVAL;
646 		goto error_pclk;
647 	}
648 	ddata->maxkeybdnum = num;
649 	remotectl_button = devm_kzalloc(&pdev->dev,
650 					num * sizeof(struct rkxx_remotectl_button),
651 					GFP_KERNEL);
652 	if (!remotectl_button) {
653 		pr_err("failed to malloc remote button memory\n");
654 		ret = -ENOMEM;
655 		goto error_pclk;
656 	}
657 	input = devm_input_allocate_device(&pdev->dev);
658 	if (!input) {
659 		pr_err("failed to allocate input device\n");
660 		ret = -ENOMEM;
661 		goto error_pclk;
662 	}
663 	input->name = pdev->name;
664 	input->phys = "gpio-keys/remotectl";
665 	input->dev.parent = &pdev->dev;
666 	input->id.bustype = BUS_HOST;
667 	input->id.vendor = 0x524b;
668 	input->id.product = 0x0006;
669 	input->id.version = 0x0100;
670 	ddata->input = input;
671 	irq = platform_get_irq(pdev, 0);
672 	if (irq < 0) {
673 		dev_err(&pdev->dev, "cannot find IRQ\n");
674 		goto error_pclk;
675 	}
676 	ddata->irq = irq;
677 	ddata->wakeup = 1;
678 	of_property_read_u32(np, "remote_pwm_id", &pwm_id);
679 	pwm_id %= 4;
680 	ddata->remote_pwm_id = pwm_id;
681 	if (pwm_id > 3) {
682 		dev_err(&pdev->dev, "pwm id error\n");
683 		goto error_pclk;
684 	}
685 	DBG("remotectl: remote pwm id=0x%x\n", pwm_id);
686 	of_property_read_u32(np, "handle_cpu_id", &cpu_id);
687 	ddata->handle_cpu_id = cpu_id;
688 	DBG("remotectl: handle cpu id=0x%x\n", cpu_id);
689 	rk_remotectl_parse_ir_keys(pdev);
690 	tasklet_init(&ddata->remote_tasklet, rk_pwm_remotectl_do_something,
691 		     (unsigned long)ddata);
692 	for (j = 0; j < num; j++) {
693 		DBG("remotectl probe j = 0x%x\n", j);
694 		for (i = 0; i < remotectl_button[j].nbuttons; i++) {
695 			int keycode;
696 
697 			keycode = remotectl_button[j].key_table[i].keycode;
698 			input_set_capability(input, EV_KEY, keycode);
699 		}
700 	}
701 	ret = input_register_device(input);
702 	if (ret)
703 		dev_err(&pdev->dev, "register input device err, ret=%d\n", ret);
704 	input_set_capability(input, EV_KEY, KEY_WAKEUP);
705 	device_init_wakeup(&pdev->dev, 1);
706 	enable_irq_wake(irq);
707 	timer_setup(&ddata->timer, rk_pwm_remotectl_timer, 0);
708 	wake_lock_init(&ddata->remotectl_wake_lock,
709 		       WAKE_LOCK_SUSPEND, "rockchip_pwm_remote");
710 	cpumask_clear(&cpumask);
711 	cpumask_set_cpu(cpu_id, &cpumask);
712 	irq_set_affinity_hint(irq, &cpumask);
713 	ret = devm_request_irq(&pdev->dev, irq, rockchip_pwm_irq,
714 			       IRQF_NO_SUSPEND, "rk_pwm_irq", ddata);
715 	if (ret) {
716 		dev_err(&pdev->dev, "cannot claim IRQ %d\n", irq);
717 		goto error_irq;
718 	}
719 
720 	pwm_freq = clk_get_rate(clk) / 64;
721 	ddata->pwm_freq_nstime = 1000000000 / pwm_freq;
722 	rk_pwm_remotectl_hw_init(ddata->base, pwm_id);
723 
724 	ret = rk_pwm_pwrkey_wakeup_init(pdev);
725 	if (!ret) {
726 		dev_info(&pdev->dev, "Controller support pwrkey capture\n");
727 		goto end;
728 	}
729 
730 	ret = rk_pwm_sip_wakeup_init(pdev);
731 	if (ret)
732 		dev_info(&pdev->dev, "Donot support ATF Wakeup\n");
733 	else
734 		dev_info(&pdev->dev, "Support ATF Wakeup\n");
735 
736 	DBG("rk pwm remotectl init end!\n");
737 end:
738 	return 0;
739 error_irq:
740 	wake_lock_destroy(&ddata->remotectl_wake_lock);
741 error_pclk:
742 	clk_unprepare(p_clk);
743 error_clk:
744 	clk_unprepare(clk);
745 	return ret;
746 }
747 
rk_pwm_remove(struct platform_device * pdev)748 static int rk_pwm_remove(struct platform_device *pdev)
749 {
750 	return 0;
751 }
752 
753 #ifdef CONFIG_PM
remotectl_suspend(struct device * dev)754 static int remotectl_suspend(struct device *dev)
755 {
756 	int cpu = 0;
757 	int pwm_id;
758 	struct cpumask cpumask;
759 	struct platform_device *pdev = to_platform_device(dev);
760 	struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
761 
762 	if (ddata->pwm_pwrkey_capture) {
763 		pwm_id = ddata->remote_pwm_id;
764 		ddata->pwrkey_wakeup = 0;
765 		rk_pwm_int_ctrl(ddata->base, pwm_id, PWM_INT_DISABLE);
766 	}
767 	cpumask_clear(&cpumask);
768 	cpumask_set_cpu(cpu, &cpumask);
769 	irq_set_affinity_hint(ddata->irq, &cpumask);
770 	return 0;
771 }
772 
773 
remotectl_resume(struct device * dev)774 static int remotectl_resume(struct device *dev)
775 {
776 	struct cpumask cpumask;
777 	int pwm_id;
778 	struct platform_device *pdev = to_platform_device(dev);
779 	struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
780 	int state;
781 
782 	cpumask_clear(&cpumask);
783 	cpumask_set_cpu(ddata->handle_cpu_id, &cpumask);
784 	irq_set_affinity_hint(ddata->irq, &cpumask);
785 	if (ddata->support_psci) {
786 		/*
787 		 * loop wakeup state
788 		 */
789 		state = sip_smc_remotectl_config(
790 					REMOTECTL_GET_WAKEUP_STATE, 0);
791 		if (state == REMOTECTL_PWRKEY_WAKEUP)
792 			rk_pwm_wakeup(ddata->input);
793 	}  else if (ddata->pwm_pwrkey_capture) {
794 		pwm_id = ddata->remote_pwm_id;
795 		rk_pwm_int_ctrl(ddata->base, pwm_id, PWM_INT_ENABLE);
796 		if (ddata->pwrkey_wakeup == 0)
797 			return 0;
798 		ddata->pwrkey_wakeup = 0;
799 		rk_pwm_wakeup(ddata->input);
800 	}
801 
802 	return 0;
803 }
804 
805 static const struct dev_pm_ops remotectl_pm_ops = {
806 	.suspend_late = remotectl_suspend,
807 	.resume_early = remotectl_resume,
808 };
809 #endif
810 
811 static const struct of_device_id rk_pwm_of_match[] = {
812 	{ .compatible =  "rockchip,remotectl-pwm"},
813 	{ }
814 };
815 
816 MODULE_DEVICE_TABLE(of, rk_pwm_of_match);
817 
818 static struct platform_driver rk_pwm_driver = {
819 	.driver = {
820 		.name = "remotectl-pwm",
821 		.of_match_table = rk_pwm_of_match,
822 #ifdef CONFIG_PM
823 		.pm = &remotectl_pm_ops,
824 #endif
825 	},
826 	.remove = rk_pwm_remove,
827 };
828 
829 module_platform_driver_probe(rk_pwm_driver, rk_pwm_probe);
830 
831 MODULE_LICENSE("GPL");
832