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>
15 /* Convert NEC data to a scancode */
16 static int img_ir_nec_scancode(int len, u64 raw, u64 enabled_protocols,
17 struct img_ir_scancode_req *request)
19 unsigned int addr, addr_inv, data, data_inv;
20 /* a repeat code has no data */
22 return IMG_IR_REPEATCODE;
25 /* raw encoding: ddDDaaAA */
26 addr = (raw >> 0) & 0xff;
27 addr_inv = (raw >> 8) & 0xff;
28 data = (raw >> 16) & 0xff;
29 data_inv = (raw >> 24) & 0xff;
30 if ((data_inv ^ data) != 0xff) {
31 /* 32-bit NEC (used by Apple and TiVo remotes) */
32 /* scan encoding: as transmitted, MSBit = first received bit */
33 request->scancode = bitrev8(addr) << 24 |
34 bitrev8(addr_inv) << 16 |
37 } else if ((addr_inv ^ addr) != 0xff) {
39 /* scan encoding: AAaaDD */
40 request->scancode = addr << 16 |
45 /* scan encoding: AADD */
46 request->scancode = addr << 8 |
49 request->protocol = RC_TYPE_NEC;
50 return IMG_IR_SCANCODE;
53 /* Convert NEC scancode to NEC data filter */
54 static int img_ir_nec_filter(const struct rc_scancode_filter *in,
55 struct img_ir_filter *out, u64 protocols)
57 unsigned int addr, addr_inv, data, data_inv;
58 unsigned int addr_m, addr_inv_m, data_m, data_inv_m;
60 data = in->data & 0xff;
61 data_m = in->mask & 0xff;
63 if ((in->data | in->mask) & 0xff000000) {
64 /* 32-bit NEC (used by Apple and TiVo remotes) */
65 /* scan encoding: as transmitted, MSBit = first received bit */
66 addr = bitrev8(in->data >> 24);
67 addr_m = bitrev8(in->mask >> 24);
68 addr_inv = bitrev8(in->data >> 16);
69 addr_inv_m = bitrev8(in->mask >> 16);
70 data = bitrev8(in->data >> 8);
71 data_m = bitrev8(in->mask >> 8);
72 data_inv = bitrev8(in->data >> 0);
73 data_inv_m = bitrev8(in->mask >> 0);
74 } else if ((in->data | in->mask) & 0x00ff0000) {
76 /* scan encoding AAaaDD */
77 addr = (in->data >> 16) & 0xff;
78 addr_m = (in->mask >> 16) & 0xff;
79 addr_inv = (in->data >> 8) & 0xff;
80 addr_inv_m = (in->mask >> 8) & 0xff;
81 data_inv = data ^ 0xff;
85 /* scan encoding: AADD */
86 addr = (in->data >> 8) & 0xff;
87 addr_m = (in->mask >> 8) & 0xff;
88 addr_inv = addr ^ 0xff;
90 data_inv = data ^ 0xff;
94 /* raw encoding: ddDDaaAA */
95 out->data = data_inv << 24 |
99 out->mask = data_inv_m << 24 |
108 * See also http://www.sbprojects.com/knowledge/ir/nec.php
109 * http://wiki.altium.com/display/ADOH/NEC+Infrared+Transmission+Protocol
111 struct img_ir_decoder img_ir_nec = {
115 .code_type = IMG_IR_CODETYPE_PULSEDIST,
118 .unit = 562500, /* 562.5 us */
122 .pulse = { 16 /* 9ms */ },
123 .space = { 8 /* 4.5ms */ },
127 .pulse = { 1 /* 562.5 us */ },
128 .space = { 1 /* 562.5 us */ },
132 .pulse = { 1 /* 562.5 us */ },
133 .space = { 3 /* 1687.5 us */ },
139 .ft_min = 10, /* 5.625 ms */
143 .repeat = 108, /* 108 ms */
147 .space = { 4 /* 2.25 ms */ },
151 .minlen = 0, /* repeat code has no data */
156 .scancode = img_ir_nec_scancode,
157 .filter = img_ir_nec_filter,