summaryrefstree
path: root/kernel/drivers/media/platform/exynos/fimc-is/fimc-is-spi.c
blob: 0ee9b0eb1b511c062f5f16788982d5b9c97ab8fd (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
/*
 * driver for FIMC-IS SPI
 *
 * Copyright (c) 2011, 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/gpio.h>
#include "fimc-is-core.h"
#include "fimc-is-regs.h"

#define STREAM_TO_U16(var16, p)	{(var16) = ((u16)(*((u8 *)p+1)) + \
				((u8)(*((u8 *)p) << 8))); }

#ifndef __devinit
#define __devinit
#endif

#ifndef __devexit
#define __devexit
#endif

#ifndef __devexit_p
#define __devexit_p(x) x
#endif

static struct spi_device *g_spi;

int fimc_is_spi_reset(void *buf, u32 rx_addr, size_t size)
{
	unsigned char req_rst[1] = { 0x99 };
	int ret;

	struct spi_transfer t_c;
	struct spi_transfer t_r;

	struct spi_message m;

	memset(&t_c, 0x00, sizeof(t_c));
	memset(&t_r, 0x00, sizeof(t_r));

	t_c.tx_buf = req_rst;
	t_c.len = 1;
	t_c.cs_change = 0;

	spi_message_init(&m);
	spi_message_add_tail(&t_c, &m);

	ret = spi_sync(g_spi, &m);
	if (ret) {
		err("spi sync error - can't get device information");
		return -EIO;
	}

	return 0;
}

int fimc_is_spi_read(void *buf, u32 rx_addr, size_t size)
{
	unsigned char req_data[4] = { 0x03,  };
	int ret;

	struct spi_transfer t_c;
	struct spi_transfer t_r;

	struct spi_message m;

	memset(&t_c, 0x00, sizeof(t_c));
	memset(&t_r, 0x00, sizeof(t_r));

	req_data[1] = (rx_addr & 0xFF0000) >> 16;
	req_data[2] = (rx_addr & 0xFF00) >> 8;
	req_data[3] = (rx_addr & 0xFF);

	t_c.tx_buf = req_data;
	t_c.len = 4;
	t_c.cs_change = 1;
	t_c.bits_per_word = 32;

	t_r.rx_buf = buf;
	t_r.len = size;
	t_r.cs_change = 0;
	t_r.bits_per_word = 32;

	spi_message_init(&m);
	spi_message_add_tail(&t_c, &m);
	spi_message_add_tail(&t_r, &m);

	ret = spi_sync(g_spi, &m);
	if (ret) {
		err("spi sync error - can't read data");
		return -EIO;
	} else
		return 0;
}

int fimc_is_spi_reset_by_core(struct spi_device *spi, void *buf, u32 rx_addr, size_t size)
{
	unsigned char req_rst[1] = { 0x99 };
	int ret;

	struct spi_transfer t_c;
	struct spi_transfer t_r;

	struct spi_message m;

	memset(&t_c, 0x00, sizeof(t_c));
	memset(&t_r, 0x00, sizeof(t_r));

	t_c.tx_buf = req_rst;
	t_c.len = 1;
	t_c.cs_change = 0;

	spi_message_init(&m);
	spi_message_add_tail(&t_c, &m);

	ret = spi_sync(spi, &m);
	if (ret) {
		err("spi sync error - can't get device information");
		return -EIO;
	}

	return 0;
}
EXPORT_SYMBOL(fimc_is_spi_reset_by_core);

int fimc_is_spi_read_by_core(struct spi_device *spi, void *buf, u32 rx_addr, size_t size)
{
	unsigned char req_data[4] = { 0x03,  };
	int ret;

	struct spi_transfer t_c;
	struct spi_transfer t_r;

	struct spi_message m;

	memset(&t_c, 0x00, sizeof(t_c));
	memset(&t_r, 0x00, sizeof(t_r));

	req_data[1] = (rx_addr & 0xFF0000) >> 16;
	req_data[2] = (rx_addr & 0xFF00) >> 8;
	req_data[3] = (rx_addr & 0xFF);

	t_c.tx_buf = req_data;
	t_c.len = 4;
	t_c.cs_change = 1;
	t_c.bits_per_word = 32;

	t_r.rx_buf = buf;
	t_r.len = size;
	t_r.cs_change = 0;
	t_r.bits_per_word = 32;

	spi_message_init(&m);
	spi_message_add_tail(&t_c, &m);
	spi_message_add_tail(&t_r, &m);

	spi->max_speed_hz = 48000000;
	ret = spi_sync(spi, &m);
	if (ret) {
		err("spi sync error - can't read data");
		return -EIO;
	} else {
		return 0;
		}
}
EXPORT_SYMBOL(fimc_is_spi_read_by_core);

static int __devinit fimc_is_spi_probe(struct spi_device *spi)
{
	int ret = 0;

	dbg_core("%s\n", __func__);

	/* spi->bits_per_word = 16; */
	if (spi_setup(spi)) {
		pr_err("failed to setup spi for fimc_is_spi\n");
		ret = -EINVAL;
		goto exit;
	}

	g_spi = spi;

exit:
	return ret;
}

static int __devexit fimc_is_spi_remove(struct spi_device *spi)
{
	return 0;
}

static struct spi_driver fimc_is_spi_driver = {
	.driver = {
		.name = "fimc_is_spi",
		.bus = &spi_bus_type,
		.owner = THIS_MODULE,
	},
	.probe = fimc_is_spi_probe,
	.remove = __devexit_p(fimc_is_spi_remove),
};

static int __init fimc_is_spi_init(void)
{
	int ret;

	ret = spi_register_driver(&fimc_is_spi_driver);

	if (ret)
		pr_err("failed to register imc_is_spi- %x\n", ret);

	return ret;
}

static void __exit fimc_is_spi_exit(void)
{
	spi_unregister_driver(&fimc_is_spi_driver);
}

module_init(fimc_is_spi_init);
module_exit(fimc_is_spi_exit);

MODULE_DESCRIPTION("FIMC-IS SPI driver");
MODULE_LICENSE("GPL");