xref: /dflybsd-src/sys/dev/smbus/atmel_mxt/obp-utils.c (revision 45be1c61ffc4ced70f698da62c2f88fe6792f339)
1*45be1c61SMatthew Dillon /*
2*45be1c61SMatthew Dillon  * Copyright (c) 2014 The DragonFly Project.  All rights reserved.
3*45be1c61SMatthew Dillon  *
4*45be1c61SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5*45be1c61SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6*45be1c61SMatthew Dillon  *
7*45be1c61SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8*45be1c61SMatthew Dillon  * modification, are permitted provided that the following conditions
9*45be1c61SMatthew Dillon  * are met:
10*45be1c61SMatthew Dillon  *
11*45be1c61SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12*45be1c61SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13*45be1c61SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14*45be1c61SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15*45be1c61SMatthew Dillon  *    the documentation and/or other materials provided with the
16*45be1c61SMatthew Dillon  *    distribution.
17*45be1c61SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18*45be1c61SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19*45be1c61SMatthew Dillon  *    from this software without specific, prior written permission.
20*45be1c61SMatthew Dillon  *
21*45be1c61SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*45be1c61SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*45be1c61SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*45be1c61SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25*45be1c61SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*45be1c61SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*45be1c61SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*45be1c61SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*45be1c61SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*45be1c61SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*45be1c61SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*45be1c61SMatthew Dillon  * SUCH DAMAGE.
33*45be1c61SMatthew Dillon  *
34*45be1c61SMatthew Dillon  *------------------------------------------------------------------------------
35*45be1c61SMatthew Dillon  * Copyright 2011 Atmel Corporation. All rights reserved.
36*45be1c61SMatthew Dillon  *
37*45be1c61SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
38*45be1c61SMatthew Dillon  * modification, are permitted provided that the following conditions are met:
39*45be1c61SMatthew Dillon  *
40*45be1c61SMatthew Dillon  *    1. Redistributions of source code must retain the above copyright notice,
41*45be1c61SMatthew Dillon  *    this list of conditions and the following disclaimer.
42*45be1c61SMatthew Dillon  *
43*45be1c61SMatthew Dillon  *    2. Redistributions in binary form must reproduce the above copyright
44*45be1c61SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
45*45be1c61SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
46*45be1c61SMatthew Dillon  *
47*45be1c61SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY ATMEL ''AS IS'' AND ANY EXPRESS OR IMPLIED
48*45be1c61SMatthew Dillon  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49*45be1c61SMatthew Dillon  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
50*45be1c61SMatthew Dillon  * EVENT SHALL ATMEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51*45be1c61SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52*45be1c61SMatthew Dillon  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
53*45be1c61SMatthew Dillon  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54*45be1c61SMatthew Dillon  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55*45be1c61SMatthew Dillon  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
56*45be1c61SMatthew Dillon  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57*45be1c61SMatthew Dillon  *-----------------------------------------------------------------------------
58*45be1c61SMatthew Dillon  */
59*45be1c61SMatthew Dillon /*
60*45be1c61SMatthew Dillon  * This file contains a mix of code taken from the atmel-maxtouch/obp-utils
61*45be1c61SMatthew Dillon  * github codebase and my own for testing the atmel touchscreen chipset in
62*45be1c61SMatthew Dillon  * the Acer C720P chromebook.
63*45be1c61SMatthew Dillon  *
64*45be1c61SMatthew Dillon  * This code is independent and is used by both kernel AND user mode programs.
65*45be1c61SMatthew Dillon  */
66*45be1c61SMatthew Dillon 
67*45be1c61SMatthew Dillon #include <sys/types.h>
68*45be1c61SMatthew Dillon 
69*45be1c61SMatthew Dillon #include "obp-utils.h"
70*45be1c61SMatthew Dillon 
71*45be1c61SMatthew Dillon /*!
72*45be1c61SMatthew Dillon  * @brief Information block checksum return function.
73*45be1c61SMatthew Dillon  * @return 24-bit checksum value in 32-bit integer.
74*45be1c61SMatthew Dillon  */
75*45be1c61SMatthew Dillon uint32_t
obp_convert_crc(struct mxt_raw_crc * crc)76*45be1c61SMatthew Dillon obp_convert_crc(struct mxt_raw_crc *crc)
77*45be1c61SMatthew Dillon {
78*45be1c61SMatthew Dillon 	return ((crc->CRC_hi<<16u) | (crc->CRC));
79*45be1c61SMatthew Dillon }
80*45be1c61SMatthew Dillon 
81*45be1c61SMatthew Dillon /*!
82*45be1c61SMatthew Dillon  * @brief  Information Block Checksum algorithm.
83*45be1c61SMatthew Dillon  * @return Calculated Information Block Checksum.
84*45be1c61SMatthew Dillon  */
85*45be1c61SMatthew Dillon static
86*45be1c61SMatthew Dillon uint32_t
crc24_2byte(uint32_t crc,uint8_t firstbyte,uint8_t secondbyte)87*45be1c61SMatthew Dillon crc24_2byte(uint32_t crc, uint8_t firstbyte, uint8_t secondbyte)
88*45be1c61SMatthew Dillon {
89*45be1c61SMatthew Dillon 	static const uint32_t CRCPOLY = 0x0080001B;
90*45be1c61SMatthew Dillon 	uint32_t result;
91*45be1c61SMatthew Dillon 	uint16_t data_word;
92*45be1c61SMatthew Dillon 
93*45be1c61SMatthew Dillon 	data_word = (uint16_t) ((uint16_t)(secondbyte << 8u) | firstbyte);
94*45be1c61SMatthew Dillon 	result = ((crc << 1u) ^ (uint32_t)data_word);
95*45be1c61SMatthew Dillon 
96*45be1c61SMatthew Dillon 	/* Check if 25th bit is set, and XOR the result to create
97*45be1c61SMatthew Dillon 	 * 24-bit checksum */
98*45be1c61SMatthew Dillon 	if (result & 0x1000000) {
99*45be1c61SMatthew Dillon 		result ^= CRCPOLY;
100*45be1c61SMatthew Dillon 	}
101*45be1c61SMatthew Dillon 	return result;
102*45be1c61SMatthew Dillon }
103*45be1c61SMatthew Dillon 
104*45be1c61SMatthew Dillon /*
105*45be1c61SMatthew Dillon  * Return 24-bit crc.
106*45be1c61SMatthew Dillon  */
107*45be1c61SMatthew Dillon uint32_t
obp_crc24(uint8_t * buf,size_t bytes)108*45be1c61SMatthew Dillon obp_crc24(uint8_t *buf, size_t bytes)
109*45be1c61SMatthew Dillon {
110*45be1c61SMatthew Dillon 	uint32_t crc;
111*45be1c61SMatthew Dillon 	size_t i;
112*45be1c61SMatthew Dillon 
113*45be1c61SMatthew Dillon 	crc = 0;
114*45be1c61SMatthew Dillon 	for (i = 0; i < (bytes & ~1); i += 2) {
115*45be1c61SMatthew Dillon 		crc = crc24_2byte(crc, buf[i], buf[i+1]);
116*45be1c61SMatthew Dillon 	}
117*45be1c61SMatthew Dillon 	if (i < bytes)
118*45be1c61SMatthew Dillon 		crc = crc24_2byte(crc, buf[i], 0);
119*45be1c61SMatthew Dillon 	crc &= 0x00FFFFFF;
120*45be1c61SMatthew Dillon 
121*45be1c61SMatthew Dillon 	return crc;
122*45be1c61SMatthew Dillon }
123