aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/modem_if/modem_boot_device_spi.c
blob: 369e473a54798d10a13782d6a79422e87b1c4209 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2011 Samsung Electronics.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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/interrupt.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/wakelock.h>
#include <linux/delay.h>
#include <linux/spi/spi.h>

#include "modem.h"
#include "modem_prj.h"

#define SPI_XMIT_DELEY	100

static int check_cp_status(unsigned int gpio_cp_status, unsigned int count)
{
	int ret = 0;
	int cnt = 0;

	while (1) {
		if (gpio_get_value(gpio_cp_status) != 0) {
			ret = 0;
			break;
		}

		cnt++;
		if (cnt >= count) {
			mif_err("ERR! gpio_cp_status == 0 (cnt %d)\n", cnt);
			ret = -EFAULT;
			break;
		}

		msleep(20);
	}

	return ret;
}

static inline int spi_send(struct modem_boot_spi *loader, const char val)
{
	char buff[1];
	int ret;
	struct spi_message msg;
	struct spi_transfer xfer = {
		.len = 1,
		.tx_buf = buff,
	};

	buff[0] = val;

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

	ret = spi_sync(loader->spi_dev, &msg);
	if (ret < 0)
		mif_err("ERR! spi_sync fail (err %d)\n", ret);

	return ret;
}

static int spi_boot_write(struct modem_boot_spi *loader, const char *addr,
			const long len)
{
	int i;
	int ret = 0;
	char *buff = NULL;
	mif_err("+++\n");

	buff = kzalloc(len, GFP_KERNEL);
	if (!buff) {
		mif_err("ERR! kzalloc(%ld) fail\n", len);
		ret = -ENOMEM;
		goto exit;
	}

	ret = copy_from_user(buff, (const void __user *)addr, len);
	if (ret) {
		mif_err("ERR! copy_from_user fail (err %d)\n", ret);
		ret = -EFAULT;
		goto exit;
	}

	for (i = 0 ; i < len ; i++) {
		ret = spi_send(loader, buff[i]);
		if (ret < 0) {
			mif_err("ERR! spi_send fail (err %d)\n", ret);
			goto exit;
		}
	}

exit:
	if (buff)
		kfree(buff);

	mif_err("---\n");
	return ret;
}

static int spi_boot_open(struct inode *inode, struct file *filp)
{
	struct modem_boot_spi *loader = to_modem_boot_spi(filp->private_data);
	filp->private_data = loader;
	return 0;
}

static long spi_boot_ioctl(struct file *filp, unsigned int cmd,
			unsigned long arg)
{
	int ret = 0;
	struct modem_firmware img;
	struct modem_boot_spi *loader = filp->private_data;

	mutex_lock(&loader->lock);
	switch (cmd) {
	case IOCTL_MODEM_XMIT_BOOT:
		ret = copy_from_user(&img, (const void __user *)arg,
					sizeof(struct modem_firmware));
		if (ret) {
			mif_err("ERR! copy_from_user fail (err %d)\n", ret);
			ret = -EFAULT;
			goto exit_err;
		}
		mif_info("IOCTL_MODEM_XMIT_BOOT (size %d)\n", img.size);

		ret = spi_boot_write(loader, img.binary, img.size);
		if (ret < 0) {
			mif_err("ERR! spi_boot_write fail (err %d)\n", ret);
			break;
		}

		if (!loader->gpio_cp_status)
			break;

		ret = check_cp_status(loader->gpio_cp_status, 100);
		if (ret < 0)
			mif_err("ERR! check_cp_status fail (err %d)\n", ret);

		break;

	default:
		mif_err("ioctl cmd error\n");
		ret = -ENOIOCTLCMD;

		break;
	}
	mutex_unlock(&loader->lock);

exit_err:
	return ret;
}

static const struct file_operations modem_spi_boot_fops = {
	.owner = THIS_MODULE,
	.open = spi_boot_open,
	.unlocked_ioctl = spi_boot_ioctl,
};

static int __devinit modem_spi_boot_probe(struct spi_device *spi)
{
	int ret;
	struct modem_boot_spi *loader;
	struct modem_boot_spi_platform_data *pdata;
	mif_debug("+++\n");

	loader = kzalloc(sizeof(*loader), GFP_KERNEL);
	if (!loader) {
		mif_err("failed to allocate for modem_boot_spi\n");
		ret = -ENOMEM;
		goto err_alloc;
	}
	mutex_init(&loader->lock);

	spi->bits_per_word = 8;

	if (spi_setup(spi)) {
		mif_err("ERR! spi_setup fail\n");
		ret = -EINVAL;
		goto err_setup;
	}
	loader->spi_dev = spi;

	if (!spi->dev.platform_data) {
		mif_err("ERR! no platform_data\n");
		ret = -EINVAL;
		goto err_setup;
	}
	pdata = (struct modem_boot_spi_platform_data *)spi->dev.platform_data;

	loader->gpio_cp_status = pdata->gpio_cp_status;

	spi_set_drvdata(spi, loader);

	loader->dev.minor = MISC_DYNAMIC_MINOR;
	loader->dev.name = MODEM_BOOT_DEV_SPI;
	loader->dev.fops = &modem_spi_boot_fops;
	ret = misc_register(&loader->dev);
	if (ret) {
		mif_err("ERR! misc_register fail (err %d)\n", ret);
		goto err_setup;
	}

	mif_debug("---\n");
	return 0;

err_setup:
	mutex_destroy(&loader->lock);
	kfree(loader);

err_alloc:
	mif_err("xxx\n");
	return ret;
}

static int __devexit modem_spi_boot_remove(struct spi_device *spi)
{
	struct modem_boot_spi *loader = spi_get_drvdata(spi);

	misc_deregister(&loader->dev);
	mutex_destroy(&loader->lock);
	kfree(loader);

	return 0;
}

static struct spi_driver modem_boot_device_spi_driver = {
	.driver = {
		.name = MODEM_BOOT_DEV_SPI,
		.owner = THIS_MODULE,
	},
	.probe = modem_spi_boot_probe,
	.remove = __devexit_p(modem_spi_boot_remove),
};

static int __init modem_boot_device_spi_init(void)
{
	int err;

	err = spi_register_driver(&modem_boot_device_spi_driver);
	if (err) {
		mif_err("spi_register_driver fail (err %d)\n", err);
		return err;
	}

	return 0;
}

static void __exit modem_boot_device_spi_exit(void)
{
	spi_unregister_driver(&modem_boot_device_spi_driver);
}

module_init(modem_boot_device_spi_init);
module_exit(modem_boot_device_spi_exit);

MODULE_DESCRIPTION("SPI Driver for Downloading Modem Bootloader");
MODULE_LICENSE("GPL");