aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/sensorhub/stm/ssp_spi.c
blob: 469a95bfa731e7b489b4446ac58d6c66f10cf722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * driver for Android SensorHub SPI
 *
 * Copyright (c) 2013, Samsung Electronics. All rights reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>

#if defined(DEBUG_SSP_SPI)
#define ssp_log(fmt, arg...)	\
	do {					\
		printk(KERN_ERR "[%s:%d] " fmt ,	\
			__func__, __LINE__, ##arg);		\
	}							\
	while (0)
#else
#define ssp_log(fmt, arg...)
#endif


/* If AP can't change the endian to BIG */
/* for s5c73m ISP, this option must is required.*/
/* This option depends on SPI_DMA_MODE */
/* in camera driver file*/
/*#define CHANGE_ENDIAN	*/


int ssp_spi_write_sync(struct spi_device *spi, const u8 *addr, const int len)
{
	int ret;
#if defined(CHANGE_ENDIAN)
	u8 buf[8] = {0};
#endif

	struct spi_message msg;

	struct spi_transfer xfer = {
		.len = len,
#if !defined(CHANGE_ENDIAN)
		.tx_buf = addr,
		/*QCTK ALRAN QUP_CONFIG 0-4 bits BIG ENDIAN*/
		.bits_per_word = 8,
#else
		.tx_buf = buf,
#endif
	};

#if defined(CHANGE_ENDIAN)
	buf[0] = addr[3];
	buf[1] = addr[2];
	buf[2] = addr[1];
	buf[3] = addr[0];
	buf[4] = addr[7];
	buf[5] = addr[6];
	buf[6] = addr[5];
	buf[7] = addr[4];
#endif

	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	ret = spi_sync(spi, &msg);

	if (ret < 0)
		ssp_log("error %d\n", ret);

	return ret;
}


int ssp_spi_read_sync(struct spi_device *spi, u8 *in_buf, size_t len)
{
	int ret;
	u8 read_out_buf[2];

	struct spi_message msg;
	struct spi_transfer xfer = {
		.tx_buf = read_out_buf,
		.rx_buf = in_buf,
		.len	= len,
		.cs_change = 0,
	};

	spi_message_init(&msg);

	spi_message_add_tail(&xfer, &msg);

	ret = spi_sync(spi, &msg);

	if (ret < 0)
		ssp_log("%s - error %d\n",
				__func__, ret);

	return ret;
}


int ssp_spi_sync(struct spi_device *spi, u8 *out_buf,
	size_t out_len, u8 *in_buf)
{
	int ret;

	struct spi_message msg;
	struct spi_transfer xfer = {
		.tx_buf = out_buf,
		.rx_buf = in_buf,
		.len	= out_len,
		.cs_change = 0,
	};

	spi_message_init(&msg);

	spi_message_add_tail(&xfer, &msg);

	ret = spi_sync(spi, &msg);
	ssp_log("%s - received %d\n", __func__, xfer.len);

	if (ret < 0)
		ssp_log("%s - error %d\n",
				__func__, ret);

	return ret;
}

unsigned int g_flag_spirecv;
void ssp_spi_async_complete(void *context)
{
	g_flag_spirecv = 1;
}

int ssp_spi_async(struct spi_device *spi,  u8 *out_buf,
	size_t out_len, u8 *in_buf)
{
	int ret;

	struct spi_message msg;
	struct spi_transfer xfer = {
		.tx_buf = out_buf,
		.rx_buf = in_buf,
		.len	= out_len,
		.cs_change = 0,
	};


	spi_message_init(&msg);

	spi_message_add_tail(&xfer, &msg);
	msg.complete = ssp_spi_async_complete;

	ret = spi_async(spi, &msg);

	if (ret < 0)
		ssp_log("%s - error %d\n",
				__func__, ret);

	return ret;
}




int ssp_spi_read(struct spi_device *spi, u8 *buf, size_t len, const int rxSize)
{
	int k;
	int ret = 0;
	u8 temp_buf[4] = {0};
	u32 count = len/rxSize;
	u32 extra = len%rxSize;

	for (k = 0; k < count; k++) {
		ret = ssp_spi_read_sync(spi, &buf[rxSize*k], rxSize);
		if (ret < 0) {
			ssp_log("%s - error %d\n",
				__func__, ret);
			return -EINVAL;
		}
	}

	if (extra != 0) {
		ret = ssp_spi_read_sync(spi, &buf[rxSize*k], extra);
		if (ret < 0) {
			ssp_log("%s - error %d\n",
				__func__, ret);
			return -EINVAL;
		}
	}

	for (k = 0; k < len-3; k += 4) {
		memcpy(temp_buf, (char *)&buf[k], sizeof(temp_buf));
		buf[k] = temp_buf[3];
		buf[k+1] = temp_buf[2];
		buf[k+2] = temp_buf[1];
		buf[k+3] = temp_buf[0];
	}

	return 0;
}

int ssp_spi_write(struct spi_device *spi, const u8 *addr,
	const int len, const int txSize)
{
	int i, j = 0;
	int ret = 0;
	u8 paddingData[8];
	u32 count = len/txSize;
	u32 extra = len%txSize;
	ssp_log("Entered\n");
	ssp_log("count = %d extra = %d\n", count, extra);

	memset(paddingData, 0, sizeof(paddingData));

	for (i = 0 ; i < count ; i++) {
		ret = ssp_spi_write_sync(spi, &addr[j], txSize);
		j += txSize;
		if (ret < 0) {
			ssp_log("failed to write ssp_spi_write_sync\n");
			goto exit_err;
		}
		ssp_log("Delay!!!\n");
		msleep(50);
	}

	if (extra) {
		ret = ssp_spi_write_sync(spi, &addr[j], extra);
		if (ret < 0) {
			ssp_log("failed to write ssp_spi_write_sync\n");
			goto exit_err;
		}
	}

	for (i = 0; i < 4; i++) {
		memset(paddingData, 0, sizeof(paddingData));
		ret = ssp_spi_write_sync(spi, paddingData, 8);
		if (ret < 0) {
			ssp_log("failed to write ssp_spi_write_sync\n");
			goto exit_err;
		}
	}
	ssp_log("Finish!!\n");
exit_err:
	return ret;
}