1 /*
2  * ImgTec IR Decoder setup for Sony (SIRC) protocol.
3  *
4  * Copyright 2012-2014 Imagination Technologies Ltd.
5  *
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.
10  */
11 
12 #include "img-ir-hw.h"
13 
14 /* Convert Sony data to a scancode */
15 static int img_ir_sony_scancode(int len, u64 raw, u64 enabled_protocols,
16 				struct img_ir_scancode_req *request)
17 {
18 	unsigned int dev, subdev, func;
19 
20 	switch (len) {
21 	case 12:
22 		if (!(enabled_protocols & RC_BIT_SONY12))
23 			return -EINVAL;
24 		func   = raw & 0x7f;	/* first 7 bits */
25 		raw    >>= 7;
26 		dev    = raw & 0x1f;	/* next 5 bits */
27 		subdev = 0;
28 		request->protocol = RC_TYPE_SONY12;
29 		break;
30 	case 15:
31 		if (!(enabled_protocols & RC_BIT_SONY15))
32 			return -EINVAL;
33 		func   = raw & 0x7f;	/* first 7 bits */
34 		raw    >>= 7;
35 		dev    = raw & 0xff;	/* next 8 bits */
36 		subdev = 0;
37 		request->protocol = RC_TYPE_SONY15;
38 		break;
39 	case 20:
40 		if (!(enabled_protocols & RC_BIT_SONY20))
41 			return -EINVAL;
42 		func   = raw & 0x7f;	/* first 7 bits */
43 		raw    >>= 7;
44 		dev    = raw & 0x1f;	/* next 5 bits */
45 		raw    >>= 5;
46 		subdev = raw & 0xff;	/* next 8 bits */
47 		request->protocol = RC_TYPE_SONY20;
48 		break;
49 	default:
50 		return -EINVAL;
51 	}
52 	request->scancode = dev << 16 | subdev << 8 | func;
53 	return IMG_IR_SCANCODE;
54 }
55 
56 /* Convert NEC scancode to NEC data filter */
57 static int img_ir_sony_filter(const struct rc_scancode_filter *in,
58 			      struct img_ir_filter *out, u64 protocols)
59 {
60 	unsigned int dev, subdev, func;
61 	unsigned int dev_m, subdev_m, func_m;
62 	unsigned int len = 0;
63 
64 	dev      = (in->data >> 16) & 0xff;
65 	dev_m    = (in->mask >> 16) & 0xff;
66 	subdev   = (in->data >> 8)  & 0xff;
67 	subdev_m = (in->mask >> 8)  & 0xff;
68 	func     = (in->data >> 0)  & 0x7f;
69 	func_m   = (in->mask >> 0)  & 0x7f;
70 
71 	protocols &= RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20;
72 
73 	/*
74 	 * If only one bit is set, we were requested to do an exact
75 	 * protocol. This should be the case for wakeup filters; for
76 	 * normal filters, guess the protocol from the scancode.
77 	 */
78 	if (!is_power_of_2(protocols)) {
79 		if (subdev & subdev_m)
80 			protocols = RC_BIT_SONY20;
81 		else if (dev & dev_m & 0xe0)
82 			protocols = RC_BIT_SONY15;
83 		else
84 			protocols = RC_BIT_SONY12;
85 	}
86 
87 	if (protocols == RC_BIT_SONY20) {
88 		/* can't encode subdev and higher device bits */
89 		if (dev & dev_m & 0xe0)
90 			return -EINVAL;
91 		len = 20;
92 		dev_m &= 0x1f;
93 	} else if (protocols == RC_BIT_SONY15) {
94 		len = 15;
95 		subdev_m = 0;
96 	} else {
97 		/*
98 		 * The hardware mask cannot distinguish high device bits and low
99 		 * extended bits, so logically AND those bits of the masks
100 		 * together.
101 		 */
102 		subdev_m &= (dev_m >> 5) | 0xf8;
103 		dev_m &= 0x1f;
104 	}
105 
106 	/* ensure there aren't any bits straying between fields */
107 	dev &= dev_m;
108 	subdev &= subdev_m;
109 
110 	/* write the hardware filter */
111 	out->data = func          |
112 		    dev      << 7 |
113 		    subdev   << 15;
114 	out->mask = func_m        |
115 		    dev_m    << 7 |
116 		    subdev_m << 15;
117 
118 	if (len) {
119 		out->minlen = len;
120 		out->maxlen = len;
121 	}
122 	return 0;
123 }
124 
125 /*
126  * Sony SIRC decoder
127  * See also http://www.sbprojects.com/knowledge/ir/sirc.php
128  *          http://picprojects.org.uk/projects/sirc/sonysirc.pdf
129  */
130 struct img_ir_decoder img_ir_sony = {
131 	.type = RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20,
132 	.control = {
133 		.decoden = 1,
134 		.code_type = IMG_IR_CODETYPE_PULSELEN,
135 	},
136 	/* main timings */
137 	.unit = 600000, /* 600 us */
138 	.timings = {
139 		/* leader symbol */
140 		.ldr = {
141 			.pulse = { 4	/* 2.4 ms */ },
142 			.space = { 1	/* 600 us */ },
143 		},
144 		/* 0 symbol */
145 		.s00 = {
146 			.pulse = { 1	/* 600 us */ },
147 			.space = { 1	/* 600 us */ },
148 		},
149 		/* 1 symbol */
150 		.s01 = {
151 			.pulse = { 2	/* 1.2 ms */ },
152 			.space = { 1	/* 600 us */ },
153 		},
154 		/* free time */
155 		.ft = {
156 			.minlen = 12,
157 			.maxlen = 20,
158 			.ft_min = 10,	/* 6 ms */
159 		},
160 	},
161 	/* scancode logic */
162 	.scancode = img_ir_sony_scancode,
163 	.filter = img_ir_sony_filter,
164 };
165