aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/s5c73m3_spi.c
blob: 0b537cb9f9f204aaa8893a271c1f677724342b1f (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
/*
 * driver for S5C73M3 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>

static struct spi_device *g_spi;

static inline
int spi_xmit(const u8 *addr, const int len)
{
	int ret;

	struct spi_message msg;

	struct spi_transfer xfer = {
		.len = len,
		.tx_buf = addr,
	};

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

	ret = spi_sync(g_spi, &msg);

	if (ret < 0)
		dev_err(&g_spi->dev, "%s - error %d\n",
				__func__, ret);

	return ret;
}

static inline
int spi_xmit_rx(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(g_spi, &msg);

	if (ret < 0)
		dev_err(&g_spi->dev, "%s - error %d\n",
				__func__, ret);

	return ret;
}

int s5c73m3_spi_read(u8 *buf, size_t len, const int rxSize)
{
	int k;
	int ret = 0;
	int z = 0;

	u8 paddingData[32];
	u32 count = len/rxSize;
	u32 extra = len%rxSize;

	for (k = 0; k < count; k++) {
		ret = spi_xmit_rx(&buf[rxSize*k], rxSize);
		if (ret < 0)
			return -EINVAL;
	}

	if (extra != 0) {
		ret = spi_xmit_rx(&buf[rxSize*k], extra);
		if (ret < 0)
			return -EINVAL;
	}

	return 0;
}

int s5c73m3_spi_write(const u8 *addr, const int len, const int txSize)
{
	int i, j = 0;
	int ret = 0;
	u8 paddingData[32];
	u32 count = len/txSize;
	u32 extra = len%txSize;

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

	for (i = 0 ; i < count ; i++) {
		ret = spi_xmit(&addr[j], txSize);
		j += txSize;
		if (ret < 0)
			goto exit_err;
	}

	if (extra) {
		ret = spi_xmit(&addr[j], extra);
		if (ret < 0)
			goto exit_err;
	}

	ret = spi_xmit(paddingData, 32);
	if (ret < 0)
		goto exit_err;

exit_err:
	return ret;
}

static
int __devinit s5c73m3_spi_probe(struct spi_device *spi)
{
	int ret;

	spi->bits_per_word = 32;
	if (spi_setup(spi)) {
		pr_err("failed to setup spi for s5c73m3_spi\n");
		ret = -EINVAL;
		goto err_setup;
	}

	g_spi = spi;

	pr_err("s5c73m3_spi successfully probed\n");

	return 0;

err_setup:
	return ret;
}

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

static
struct spi_driver s5c73m3_spi_driver = {
	.driver = {
		.name = "s5c73m3_spi",
		.bus = &spi_bus_type,
		.owner = THIS_MODULE,
	},
	.probe = s5c73m3_spi_probe,
	.remove = __devexit_p(s5c73m3_spi_remove),
};

static
int __init s5c73m3_spi_init(void)
{
	int ret;
	pr_err("%s\n", __func__);

	ret = spi_register_driver(&s5c73m3_spi_driver);

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

	return ret;
}

static
void __exit s5c73m3_spi_exit(void)
{
	spi_unregister_driver(&s5c73m3_spi_driver);
}

module_init(s5c73m3_spi_init);
module_exit(s5c73m3_spi_exit);

MODULE_DESCRIPTION("S5C73M3 SPI driver");
MODULE_LICENSE("GPL");