2 * ImgTec IR Decoder setup for NEC protocol.
4 * Copyright 2010-2014 Imagination Technologies Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include "img-ir-hw.h"
13 #include <linux/bitrev.h>
14 #include <linux/log2.h>
16 /* Convert NEC data to a scancode */
17 static int img_ir_nec_scancode(int len
, u64 raw
, u64 enabled_protocols
,
18 struct img_ir_scancode_req
*request
)
20 unsigned int addr
, addr_inv
, data
, data_inv
;
21 /* a repeat code has no data */
23 return IMG_IR_REPEATCODE
;
26 /* raw encoding: ddDDaaAA */
27 addr
= (raw
>> 0) & 0xff;
28 addr_inv
= (raw
>> 8) & 0xff;
29 data
= (raw
>> 16) & 0xff;
30 data_inv
= (raw
>> 24) & 0xff;
31 if ((data_inv
^ data
) != 0xff) {
32 /* 32-bit NEC (used by Apple and TiVo remotes) */
33 /* scan encoding: as transmitted, MSBit = first received bit */
34 request
->scancode
= bitrev8(addr
) << 24 |
35 bitrev8(addr_inv
) << 16 |
38 request
->protocol
= RC_PROTO_NEC32
;
39 } else if ((addr_inv
^ addr
) != 0xff) {
41 /* scan encoding: AAaaDD */
42 request
->scancode
= addr
<< 16 |
45 request
->protocol
= RC_PROTO_NECX
;
48 /* scan encoding: AADD */
49 request
->scancode
= addr
<< 8 |
51 request
->protocol
= RC_PROTO_NEC
;
53 return IMG_IR_SCANCODE
;
56 /* Convert NEC scancode to NEC data filter */
57 static int img_ir_nec_filter(const struct rc_scancode_filter
*in
,
58 struct img_ir_filter
*out
, u64 protocols
)
60 unsigned int addr
, addr_inv
, data
, data_inv
;
61 unsigned int addr_m
, addr_inv_m
, data_m
, data_inv_m
;
63 data
= in
->data
& 0xff;
64 data_m
= in
->mask
& 0xff;
66 protocols
&= RC_PROTO_BIT_NEC
| RC_PROTO_BIT_NECX
| RC_PROTO_BIT_NEC32
;
69 * If only one bit is set, we were requested to do an exact
70 * protocol. This should be the case for wakeup filters; for
71 * normal filters, guess the protocol from the scancode.
73 if (!is_power_of_2(protocols
)) {
74 if ((in
->data
| in
->mask
) & 0xff000000)
75 protocols
= RC_PROTO_BIT_NEC32
;
76 else if ((in
->data
| in
->mask
) & 0x00ff0000)
77 protocols
= RC_PROTO_BIT_NECX
;
79 protocols
= RC_PROTO_BIT_NEC
;
82 if (protocols
== RC_PROTO_BIT_NEC32
) {
83 /* 32-bit NEC (used by Apple and TiVo remotes) */
84 /* scan encoding: as transmitted, MSBit = first received bit */
85 addr
= bitrev8(in
->data
>> 24);
86 addr_m
= bitrev8(in
->mask
>> 24);
87 addr_inv
= bitrev8(in
->data
>> 16);
88 addr_inv_m
= bitrev8(in
->mask
>> 16);
89 data
= bitrev8(in
->data
>> 8);
90 data_m
= bitrev8(in
->mask
>> 8);
91 data_inv
= bitrev8(in
->data
>> 0);
92 data_inv_m
= bitrev8(in
->mask
>> 0);
93 } else if (protocols
== RC_PROTO_BIT_NECX
) {
95 /* scan encoding AAaaDD */
96 addr
= (in
->data
>> 16) & 0xff;
97 addr_m
= (in
->mask
>> 16) & 0xff;
98 addr_inv
= (in
->data
>> 8) & 0xff;
99 addr_inv_m
= (in
->mask
>> 8) & 0xff;
100 data_inv
= data
^ 0xff;
104 /* scan encoding: AADD */
105 addr
= (in
->data
>> 8) & 0xff;
106 addr_m
= (in
->mask
>> 8) & 0xff;
107 addr_inv
= addr
^ 0xff;
109 data_inv
= data
^ 0xff;
113 /* raw encoding: ddDDaaAA */
114 out
->data
= data_inv
<< 24 |
118 out
->mask
= data_inv_m
<< 24 |
127 * See also http://www.sbprojects.com/knowledge/ir/nec.php
128 * http://wiki.altium.com/display/ADOH/NEC+Infrared+Transmission+Protocol
130 struct img_ir_decoder img_ir_nec
= {
131 .type
= RC_PROTO_BIT_NEC
| RC_PROTO_BIT_NECX
| RC_PROTO_BIT_NEC32
,
134 .code_type
= IMG_IR_CODETYPE_PULSEDIST
,
137 .unit
= 562500, /* 562.5 us */
141 .pulse
= { 16 /* 9ms */ },
142 .space
= { 8 /* 4.5ms */ },
146 .pulse
= { 1 /* 562.5 us */ },
147 .space
= { 1 /* 562.5 us */ },
151 .pulse
= { 1 /* 562.5 us */ },
152 .space
= { 3 /* 1687.5 us */ },
158 .ft_min
= 10, /* 5.625 ms */
162 .repeat
= 108, /* 108 ms */
166 .space
= { 4 /* 2.25 ms */ },
170 .minlen
= 0, /* repeat code has no data */
175 .scancode
= img_ir_nec_scancode
,
176 .filter
= img_ir_nec_filter
,