xref: /linux/drivers/media/rc/ir-sharp-decoder.c (revision d35afc5fe0971bd1d40c824bd3c7587aeda823b2)
11d184b0bSJames Hogan /* ir-sharp-decoder.c - handle Sharp IR Pulse/Space protocol
21d184b0bSJames Hogan  *
31d184b0bSJames Hogan  * Copyright (C) 2013-2014 Imagination Technologies Ltd.
41d184b0bSJames Hogan  *
51d184b0bSJames Hogan  * Based on NEC decoder:
637e59f87SMauro Carvalho Chehab  * Copyright (C) 2010 by Mauro Carvalho Chehab
71d184b0bSJames Hogan  *
81d184b0bSJames Hogan  * This program is free software; you can redistribute it and/or modify
91d184b0bSJames Hogan  *  it under the terms of the GNU General Public License as published by
101d184b0bSJames Hogan  *  the Free Software Foundation version 2 of the License.
111d184b0bSJames Hogan  *
121d184b0bSJames Hogan  *  This program is distributed in the hope that it will be useful,
131d184b0bSJames Hogan  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
141d184b0bSJames Hogan  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151d184b0bSJames Hogan  *  GNU General Public License for more details.
161d184b0bSJames Hogan  */
171d184b0bSJames Hogan 
181d184b0bSJames Hogan #include <linux/bitrev.h>
191d184b0bSJames Hogan #include <linux/module.h>
201d184b0bSJames Hogan #include "rc-core-priv.h"
211d184b0bSJames Hogan 
221d184b0bSJames Hogan #define SHARP_NBITS		15
231d184b0bSJames Hogan #define SHARP_UNIT		40000  /* ns */
241d184b0bSJames Hogan #define SHARP_BIT_PULSE		(8    * SHARP_UNIT) /* 320us */
251d184b0bSJames Hogan #define SHARP_BIT_0_PERIOD	(25   * SHARP_UNIT) /* 1ms (680us space) */
261d184b0bSJames Hogan #define SHARP_BIT_1_PERIOD	(50   * SHARP_UNIT) /* 2ms (1680ms space) */
271d184b0bSJames Hogan #define SHARP_ECHO_SPACE	(1000 * SHARP_UNIT) /* 40 ms */
281d184b0bSJames Hogan #define SHARP_TRAILER_SPACE	(125  * SHARP_UNIT) /* 5 ms (even longer) */
291d184b0bSJames Hogan 
301d184b0bSJames Hogan enum sharp_state {
311d184b0bSJames Hogan 	STATE_INACTIVE,
321d184b0bSJames Hogan 	STATE_BIT_PULSE,
331d184b0bSJames Hogan 	STATE_BIT_SPACE,
341d184b0bSJames Hogan 	STATE_TRAILER_PULSE,
351d184b0bSJames Hogan 	STATE_ECHO_SPACE,
361d184b0bSJames Hogan 	STATE_TRAILER_SPACE,
371d184b0bSJames Hogan };
381d184b0bSJames Hogan 
391d184b0bSJames Hogan /**
401d184b0bSJames Hogan  * ir_sharp_decode() - Decode one Sharp pulse or space
411d184b0bSJames Hogan  * @dev:	the struct rc_dev descriptor of the device
421d184b0bSJames Hogan  * @duration:	the struct ir_raw_event descriptor of the pulse/space
431d184b0bSJames Hogan  *
441d184b0bSJames Hogan  * This function returns -EINVAL if the pulse violates the state machine
451d184b0bSJames Hogan  */
461d184b0bSJames Hogan static int ir_sharp_decode(struct rc_dev *dev, struct ir_raw_event ev)
471d184b0bSJames Hogan {
481d184b0bSJames Hogan 	struct sharp_dec *data = &dev->raw->sharp;
491d184b0bSJames Hogan 	u32 msg, echo, address, command, scancode;
501d184b0bSJames Hogan 
511d184b0bSJames Hogan 	if (!is_timing_event(ev)) {
521d184b0bSJames Hogan 		if (ev.reset)
531d184b0bSJames Hogan 			data->state = STATE_INACTIVE;
541d184b0bSJames Hogan 		return 0;
551d184b0bSJames Hogan 	}
561d184b0bSJames Hogan 
571d184b0bSJames Hogan 	IR_dprintk(2, "Sharp decode started at state %d (%uus %s)\n",
581d184b0bSJames Hogan 		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
591d184b0bSJames Hogan 
601d184b0bSJames Hogan 	switch (data->state) {
611d184b0bSJames Hogan 
621d184b0bSJames Hogan 	case STATE_INACTIVE:
631d184b0bSJames Hogan 		if (!ev.pulse)
641d184b0bSJames Hogan 			break;
651d184b0bSJames Hogan 
661d184b0bSJames Hogan 		if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
671d184b0bSJames Hogan 			       SHARP_BIT_PULSE / 2))
681d184b0bSJames Hogan 			break;
691d184b0bSJames Hogan 
701d184b0bSJames Hogan 		data->count = 0;
711d184b0bSJames Hogan 		data->pulse_len = ev.duration;
721d184b0bSJames Hogan 		data->state = STATE_BIT_SPACE;
731d184b0bSJames Hogan 		return 0;
741d184b0bSJames Hogan 
751d184b0bSJames Hogan 	case STATE_BIT_PULSE:
761d184b0bSJames Hogan 		if (!ev.pulse)
771d184b0bSJames Hogan 			break;
781d184b0bSJames Hogan 
791d184b0bSJames Hogan 		if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
801d184b0bSJames Hogan 			       SHARP_BIT_PULSE / 2))
811d184b0bSJames Hogan 			break;
821d184b0bSJames Hogan 
831d184b0bSJames Hogan 		data->pulse_len = ev.duration;
841d184b0bSJames Hogan 		data->state = STATE_BIT_SPACE;
851d184b0bSJames Hogan 		return 0;
861d184b0bSJames Hogan 
871d184b0bSJames Hogan 	case STATE_BIT_SPACE:
881d184b0bSJames Hogan 		if (ev.pulse)
891d184b0bSJames Hogan 			break;
901d184b0bSJames Hogan 
911d184b0bSJames Hogan 		data->bits <<= 1;
921d184b0bSJames Hogan 		if (eq_margin(data->pulse_len + ev.duration, SHARP_BIT_1_PERIOD,
931d184b0bSJames Hogan 			      SHARP_BIT_PULSE * 2))
941d184b0bSJames Hogan 			data->bits |= 1;
951d184b0bSJames Hogan 		else if (!eq_margin(data->pulse_len + ev.duration,
961d184b0bSJames Hogan 				    SHARP_BIT_0_PERIOD, SHARP_BIT_PULSE * 2))
971d184b0bSJames Hogan 			break;
981d184b0bSJames Hogan 		data->count++;
991d184b0bSJames Hogan 
1001d184b0bSJames Hogan 		if (data->count == SHARP_NBITS ||
1011d184b0bSJames Hogan 		    data->count == SHARP_NBITS * 2)
1021d184b0bSJames Hogan 			data->state = STATE_TRAILER_PULSE;
1031d184b0bSJames Hogan 		else
1041d184b0bSJames Hogan 			data->state = STATE_BIT_PULSE;
1051d184b0bSJames Hogan 
1061d184b0bSJames Hogan 		return 0;
1071d184b0bSJames Hogan 
1081d184b0bSJames Hogan 	case STATE_TRAILER_PULSE:
1091d184b0bSJames Hogan 		if (!ev.pulse)
1101d184b0bSJames Hogan 			break;
1111d184b0bSJames Hogan 
1121d184b0bSJames Hogan 		if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
1131d184b0bSJames Hogan 			       SHARP_BIT_PULSE / 2))
1141d184b0bSJames Hogan 			break;
1151d184b0bSJames Hogan 
1161d184b0bSJames Hogan 		if (data->count == SHARP_NBITS) {
1171d184b0bSJames Hogan 			/* exp,chk bits should be 1,0 */
1181b0621afSHeiner Kallweit 			if ((data->bits & 0x3) != 0x2 &&
1191b0621afSHeiner Kallweit 			/* DENON variant, both chk bits 0 */
1201b0621afSHeiner Kallweit 			    (data->bits & 0x3) != 0x0)
1211d184b0bSJames Hogan 				break;
1221d184b0bSJames Hogan 			data->state = STATE_ECHO_SPACE;
1231d184b0bSJames Hogan 		} else {
1241d184b0bSJames Hogan 			data->state = STATE_TRAILER_SPACE;
1251d184b0bSJames Hogan 		}
1261d184b0bSJames Hogan 		return 0;
1271d184b0bSJames Hogan 
1281d184b0bSJames Hogan 	case STATE_ECHO_SPACE:
1291d184b0bSJames Hogan 		if (ev.pulse)
1301d184b0bSJames Hogan 			break;
1311d184b0bSJames Hogan 
1321d184b0bSJames Hogan 		if (!eq_margin(ev.duration, SHARP_ECHO_SPACE,
1331d184b0bSJames Hogan 			       SHARP_ECHO_SPACE / 4))
1341d184b0bSJames Hogan 			break;
1351d184b0bSJames Hogan 
1361d184b0bSJames Hogan 		data->state = STATE_BIT_PULSE;
1371d184b0bSJames Hogan 
1381d184b0bSJames Hogan 		return 0;
1391d184b0bSJames Hogan 
1401d184b0bSJames Hogan 	case STATE_TRAILER_SPACE:
1411d184b0bSJames Hogan 		if (ev.pulse)
1421d184b0bSJames Hogan 			break;
1431d184b0bSJames Hogan 
1441d184b0bSJames Hogan 		if (!geq_margin(ev.duration, SHARP_TRAILER_SPACE,
1451d184b0bSJames Hogan 				SHARP_BIT_PULSE / 2))
1461d184b0bSJames Hogan 			break;
1471d184b0bSJames Hogan 
1481d184b0bSJames Hogan 		/* Validate - command, ext, chk should be inverted in 2nd */
1491d184b0bSJames Hogan 		msg = (data->bits >> 15) & 0x7fff;
1501d184b0bSJames Hogan 		echo = data->bits & 0x7fff;
1511d184b0bSJames Hogan 		if ((msg ^ echo) != 0x3ff) {
1521d184b0bSJames Hogan 			IR_dprintk(1,
1531d184b0bSJames Hogan 				   "Sharp checksum error: received 0x%04x, 0x%04x\n",
1541d184b0bSJames Hogan 				   msg, echo);
1551d184b0bSJames Hogan 			break;
1561d184b0bSJames Hogan 		}
1571d184b0bSJames Hogan 
1581d184b0bSJames Hogan 		address = bitrev8((msg >> 7) & 0xf8);
1591d184b0bSJames Hogan 		command = bitrev8((msg >> 2) & 0xff);
1601d184b0bSJames Hogan 
1611d184b0bSJames Hogan 		scancode = address << 8 | command;
1621d184b0bSJames Hogan 		IR_dprintk(1, "Sharp scancode 0x%04x\n", scancode);
1631d184b0bSJames Hogan 
164120703f9SDavid Härdeman 		rc_keydown(dev, RC_TYPE_SHARP, scancode, 0);
1651d184b0bSJames Hogan 		data->state = STATE_INACTIVE;
1661d184b0bSJames Hogan 		return 0;
1671d184b0bSJames Hogan 	}
1681d184b0bSJames Hogan 
1691d184b0bSJames Hogan 	IR_dprintk(1, "Sharp decode failed at count %d state %d (%uus %s)\n",
1701d184b0bSJames Hogan 		   data->count, data->state, TO_US(ev.duration),
1711d184b0bSJames Hogan 		   TO_STR(ev.pulse));
1721d184b0bSJames Hogan 	data->state = STATE_INACTIVE;
1731d184b0bSJames Hogan 	return -EINVAL;
1741d184b0bSJames Hogan }
1751d184b0bSJames Hogan 
176*d35afc5fSSean Young static const struct ir_raw_timings_pd ir_sharp_timings = {
177*d35afc5fSSean Young 	.header_pulse  = 0,
178*d35afc5fSSean Young 	.header_space  = 0,
179*d35afc5fSSean Young 	.bit_pulse     = SHARP_BIT_PULSE,
180*d35afc5fSSean Young 	.bit_space[0]  = SHARP_BIT_0_PERIOD,
181*d35afc5fSSean Young 	.bit_space[1]  = SHARP_BIT_1_PERIOD,
182*d35afc5fSSean Young 	.trailer_pulse = SHARP_BIT_PULSE,
183*d35afc5fSSean Young 	.trailer_space = SHARP_ECHO_SPACE,
184*d35afc5fSSean Young 	.msb_first     = 1,
185*d35afc5fSSean Young };
186*d35afc5fSSean Young 
187*d35afc5fSSean Young /**
188*d35afc5fSSean Young  * ir_sharp_encode() - Encode a scancode as a stream of raw events
189*d35afc5fSSean Young  *
190*d35afc5fSSean Young  * @protocol:	protocol to encode
191*d35afc5fSSean Young  * @scancode:	scancode to encode
192*d35afc5fSSean Young  * @events:	array of raw ir events to write into
193*d35afc5fSSean Young  * @max:	maximum size of @events
194*d35afc5fSSean Young  *
195*d35afc5fSSean Young  * Returns:	The number of events written.
196*d35afc5fSSean Young  *		-ENOBUFS if there isn't enough space in the array to fit the
197*d35afc5fSSean Young  *		encoding. In this case all @max events will have been written.
198*d35afc5fSSean Young  */
199*d35afc5fSSean Young static int ir_sharp_encode(enum rc_type protocol, u32 scancode,
200*d35afc5fSSean Young 			   struct ir_raw_event *events, unsigned int max)
201*d35afc5fSSean Young {
202*d35afc5fSSean Young 	struct ir_raw_event *e = events;
203*d35afc5fSSean Young 	int ret;
204*d35afc5fSSean Young 	u32 raw;
205*d35afc5fSSean Young 
206*d35afc5fSSean Young 	raw = (((bitrev8(scancode >> 8) >> 3) << 8) & 0x1f00) |
207*d35afc5fSSean Young 		bitrev8(scancode);
208*d35afc5fSSean Young 	ret = ir_raw_gen_pd(&e, max, &ir_sharp_timings, SHARP_NBITS,
209*d35afc5fSSean Young 			    (raw << 2) | 2);
210*d35afc5fSSean Young 	if (ret < 0)
211*d35afc5fSSean Young 		return ret;
212*d35afc5fSSean Young 
213*d35afc5fSSean Young 	max -= ret;
214*d35afc5fSSean Young 
215*d35afc5fSSean Young 	raw = (((bitrev8(scancode >> 8) >> 3) << 8) & 0x1f00) |
216*d35afc5fSSean Young 		bitrev8(~scancode);
217*d35afc5fSSean Young 	ret = ir_raw_gen_pd(&e, max, &ir_sharp_timings, SHARP_NBITS,
218*d35afc5fSSean Young 			    (raw << 2) | 1);
219*d35afc5fSSean Young 	if (ret < 0)
220*d35afc5fSSean Young 		return ret;
221*d35afc5fSSean Young 
222*d35afc5fSSean Young 	return e - events;
223*d35afc5fSSean Young }
224*d35afc5fSSean Young 
2251d184b0bSJames Hogan static struct ir_raw_handler sharp_handler = {
2261d184b0bSJames Hogan 	.protocols	= RC_BIT_SHARP,
2271d184b0bSJames Hogan 	.decode		= ir_sharp_decode,
228*d35afc5fSSean Young 	.encode		= ir_sharp_encode,
2291d184b0bSJames Hogan };
2301d184b0bSJames Hogan 
2311d184b0bSJames Hogan static int __init ir_sharp_decode_init(void)
2321d184b0bSJames Hogan {
2331d184b0bSJames Hogan 	ir_raw_handler_register(&sharp_handler);
2341d184b0bSJames Hogan 
2351d184b0bSJames Hogan 	pr_info("IR Sharp protocol handler initialized\n");
2361d184b0bSJames Hogan 	return 0;
2371d184b0bSJames Hogan }
2381d184b0bSJames Hogan 
2391d184b0bSJames Hogan static void __exit ir_sharp_decode_exit(void)
2401d184b0bSJames Hogan {
2411d184b0bSJames Hogan 	ir_raw_handler_unregister(&sharp_handler);
2421d184b0bSJames Hogan }
2431d184b0bSJames Hogan 
2441d184b0bSJames Hogan module_init(ir_sharp_decode_init);
2451d184b0bSJames Hogan module_exit(ir_sharp_decode_exit);
2461d184b0bSJames Hogan 
2471d184b0bSJames Hogan MODULE_LICENSE("GPL");
2481d184b0bSJames Hogan MODULE_AUTHOR("James Hogan <james.hogan@imgtec.com>");
2491d184b0bSJames Hogan MODULE_DESCRIPTION("Sharp IR protocol decoder");
250