1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ImgTec IR Decoder setup for Sharp protocol.
4  *
5  * Copyright 2012-2014 Imagination Technologies Ltd.
6  */
7 
8 #include "img-ir-hw.h"
9 
10 /* Convert Sharp data to a scancode */
11 static int img_ir_sharp_scancode(int len, u64 raw, u64 enabled_protocols,
12 				 struct img_ir_scancode_req *request)
13 {
14 	unsigned int addr, cmd, exp, chk;
15 
16 	if (len != 15)
17 		return -EINVAL;
18 
19 	addr = (raw >>   0) & 0x1f;
20 	cmd  = (raw >>   5) & 0xff;
21 	exp  = (raw >>  13) &  0x1;
22 	chk  = (raw >>  14) &  0x1;
23 
24 	/* validate data */
25 	if (!exp)
26 		return -EINVAL;
27 	if (chk)
28 		/* probably the second half of the message */
29 		return -EINVAL;
30 
31 	request->protocol = RC_PROTO_SHARP;
32 	request->scancode = addr << 8 | cmd;
33 	return IMG_IR_SCANCODE;
34 }
35 
36 /* Convert Sharp scancode to Sharp data filter */
37 static int img_ir_sharp_filter(const struct rc_scancode_filter *in,
38 			       struct img_ir_filter *out, u64 protocols)
39 {
40 	unsigned int addr, cmd, exp = 0, chk = 0;
41 	unsigned int addr_m, cmd_m, exp_m = 0, chk_m = 0;
42 
43 	addr   = (in->data >> 8) & 0x1f;
44 	addr_m = (in->mask >> 8) & 0x1f;
45 	cmd    = (in->data >> 0) & 0xff;
46 	cmd_m  = (in->mask >> 0) & 0xff;
47 	if (cmd_m) {
48 		/* if filtering commands, we can only match the first part */
49 		exp   = 1;
50 		exp_m = 1;
51 		chk   = 0;
52 		chk_m = 1;
53 	}
54 
55 	out->data = addr        |
56 		    cmd   <<  5 |
57 		    exp   << 13 |
58 		    chk   << 14;
59 	out->mask = addr_m      |
60 		    cmd_m <<  5 |
61 		    exp_m << 13 |
62 		    chk_m << 14;
63 
64 	return 0;
65 }
66 
67 /*
68  * Sharp decoder
69  * See also http://www.sbprojects.com/knowledge/ir/sharp.php
70  */
71 struct img_ir_decoder img_ir_sharp = {
72 	.type = RC_PROTO_BIT_SHARP,
73 	.control = {
74 		.decoden = 0,
75 		.decodend2 = 1,
76 		.code_type = IMG_IR_CODETYPE_PULSEDIST,
77 		.d1validsel = 1,
78 	},
79 	/* main timings */
80 	.tolerance = 20,	/* 20% */
81 	.timings = {
82 		/* 0 symbol */
83 		.s10 = {
84 			.pulse = { 320	/* 320 us */ },
85 			.space = { 680	/* 1 ms period */ },
86 		},
87 		/* 1 symbol */
88 		.s11 = {
89 			.pulse = { 320	/* 320 us */ },
90 			.space = { 1680	/* 2 ms period */ },
91 		},
92 		/* free time */
93 		.ft = {
94 			.minlen = 15,
95 			.maxlen = 15,
96 			.ft_min = 5000,	/* 5 ms */
97 		},
98 	},
99 	/* scancode logic */
100 	.scancode = img_ir_sharp_scancode,
101 	.filter = img_ir_sharp_filter,
102 };
103