aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/ci-bridge-spi.c
blob: 368bef72de19191dae85533b30577a6568ff7f0d (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Copyright (c) 2012, The Linux Foundation. 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 and
 * only 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.
 */


/* This driver implements a simple SPI read/write interface to access
 * an external device over SPI.
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/spi/spi.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/delay.h>

#include <linux/ci-bridge-spi.h>

#define CI_MAX_BUFFER_SIZE	(64 * 1024)

struct ci_bridge {
	dev_t ci_bridge_dev;
	struct cdev cdev;
	struct class *bridge_class;
	struct device *bridge_dev;
	char *write_buffer;
	char *read_buffer;
	struct mutex lock;
	struct spi_device *spi;
	unsigned int gpio_reset_pin;
	unsigned int gpio_interrupt_pin;
	int num_opened;

};

static struct ci_bridge ci;

static int __devinit ci_bridge_spi_probe(struct spi_device *spi)
{
	int ret;
	struct ci_bridge_platform_data *pdata;

	if (spi->dev.platform_data == NULL) {
		pr_err("%s: platform data is missing\n", __func__);
		return -EINVAL;
	}

	ci.spi = spi;
	ci.num_opened = 0;
	mutex_init(&ci.lock);
	spi_set_drvdata(spi, &ci);
	pdata = spi->dev.platform_data;
	ci.gpio_reset_pin = pdata->reset_pin;
	ci.gpio_interrupt_pin = pdata->interrupt_pin;

	ret = gpio_request(ci.gpio_reset_pin, "ci_bridge_spi");
	if (ret) {
		pr_err("%s: GPIO request for pin number %u failed\n",
			   __func__, ci.gpio_reset_pin);
		return ret;
	}
	ret = gpio_direction_output(ci.gpio_reset_pin, 1);
	if (ret) {
		pr_err("%s: unable to set GPIO direction, err=%d\n",
			  __func__, ret);
		goto err_free_reset_pin;
	}

	ret = gpio_request(ci.gpio_interrupt_pin, "ci_bridge_spi");
	if (ret) {
		pr_err("%s: GPIO request for pin number %u failed\n",
			   __func__, ci.gpio_interrupt_pin);
		goto err_free_reset_pin;
	}
	ret = gpio_direction_input(ci.gpio_interrupt_pin);
	if (ret) {
		pr_err("%s: unable to set GPIO direction, err=%d\n",
			   __func__, ret);
		goto err_free_int_pin;
	}

	return 0;

err_free_int_pin:
	gpio_free(ci.gpio_interrupt_pin);
err_free_reset_pin:
	gpio_free(ci.gpio_reset_pin);

	return ret;
}

static int __devexit ci_bridge_spi_remove(struct spi_device *spi)
{
	struct ci_bridge *bridge = spi_get_drvdata(spi);

	spi_set_drvdata(bridge->spi, NULL);
	bridge->spi = NULL;
	mutex_destroy(&ci.lock);

	gpio_free(ci.gpio_reset_pin);
	gpio_free(ci.gpio_interrupt_pin);

	return 0;
}

static struct spi_driver ci_bridge_driver = {
	.driver = {
		.name = "ci_bridge_spi",
		.owner = THIS_MODULE,
	},
	.probe = ci_bridge_spi_probe,
	.remove = __devexit_p(ci_bridge_spi_remove),
};

static void ci_bridge_spi_completion_cb(void *arg)
{
	complete(arg);
}

static ssize_t ci_bridge_spi_read(struct file *filp,
				char __user *buf,
				size_t count,
				loff_t *f_pos)
{
	int ret = 0;
	unsigned long not_copied = 0;
	struct spi_transfer spi_transfer;
	struct spi_message spi_message;
	DECLARE_COMPLETION_ONSTACK(context);
	struct ci_bridge *bridge = filp->private_data;

	if ((bridge == NULL) || (bridge->spi == NULL))
		return -ENODEV;

	if (count > CI_MAX_BUFFER_SIZE)
		return -EMSGSIZE;

	memset(&spi_transfer, 0, sizeof(struct spi_transfer));
	memset(&spi_message, 0, sizeof(struct spi_message));

	mutex_lock(&bridge->lock);

	spi_transfer.rx_buf = bridge->read_buffer;
	spi_transfer.len =  count;
	spi_message_init(&spi_message);
	spi_message_add_tail(&spi_transfer, &spi_message);
	spi_message.complete = ci_bridge_spi_completion_cb;
	spi_message.context = &context;

	/* must use spi_async in a context that may sleep */
	ret = spi_async(bridge->spi, &spi_message);
	if (ret == 0) {
		wait_for_completion(&context);

		if (spi_message.status == 0) {
			/* spi_message.actual_length should contain the number
			 * of bytes actually read and should update ret to be
			 * the actual length, but since our driver doesn't
			 * support this, assume all count bytes were read.
			 */
			ret = count;
		}

		if (ret > 0) {
			not_copied =
				copy_to_user(buf, bridge->read_buffer, ret);
			if (not_copied == ret)
				ret = -EFAULT;
			else
				ret -= not_copied;
		}
	} else {
		pr_err("%s: Error calling spi_async, ret = %d\n",
			__func__, ret);
	}

	mutex_unlock(&bridge->lock);

	return ret;
}

static ssize_t ci_bridge_spi_write(struct file *filp,
				const char __user *buf,
				size_t count,
				loff_t *f_pos)
{
	int ret = 0;
	unsigned long not_copied = 0;
	struct spi_transfer spi_transfer;
	struct spi_message spi_message;
	DECLARE_COMPLETION_ONSTACK(context);
	struct ci_bridge *bridge = filp->private_data;

	if ((bridge == NULL) || (bridge->spi == NULL))
		return -ENODEV;

	if (count > CI_MAX_BUFFER_SIZE)
		return -EMSGSIZE;

	memset(&spi_transfer, 0, sizeof(struct spi_transfer));
	memset(&spi_message, 0, sizeof(struct spi_message));

	mutex_lock(&bridge->lock);
	/* copy user data to our SPI Tx buffer */
	not_copied = copy_from_user(bridge->write_buffer, buf, count);
	if (not_copied != 0) {
		ret = -EFAULT;
	} else {
		spi_transfer.tx_buf = bridge->write_buffer;
		spi_transfer.len = count;

		spi_message_init(&spi_message);
		spi_message_add_tail(&spi_transfer, &spi_message);
		spi_message.complete = ci_bridge_spi_completion_cb;
		spi_message.context = &context;

		/* must use spi_async in a context that may sleep */
		ret = spi_async(bridge->spi, &spi_message);
		if (ret == 0) {
			wait_for_completion(&context);
			/* update ret to contain
			 * the number of bytes actually written
			 */
			if (spi_message.status == 0)
				ret = spi_transfer.len;
			else
				pr_err("%s: SPI transfer error, spi_message.status = %d\n",
					__func__, spi_message.status);
		} else {
			pr_err("%s: Error calling spi_async, ret = %d\n",
				__func__, ret);
		}
	}
	mutex_unlock(&bridge->lock);

	return ret;
}

static int ci_bridge_spi_open(struct inode *inode, struct file *filp)
{
	/* forbid opening more then one instance at a time,
	   parallel execution can still be problematic */
	if (ci.num_opened != 0)
		return -EBUSY;

	/* allocate write buffer */
	ci.write_buffer =
		kzalloc((CI_MAX_BUFFER_SIZE * sizeof(char)), GFP_KERNEL);
	if (ci.write_buffer == NULL) {
		pr_err("%s: Error allocating memory for write buffer\n",
			__func__);
		return -ENOMEM;
	}
	/* allocate read buffer */
	ci.read_buffer =
		kzalloc((CI_MAX_BUFFER_SIZE * sizeof(char)), GFP_KERNEL);
	if (ci.read_buffer == NULL) {
		pr_err("%s: Error allocating memory for read buffer\n",
			__func__);
		kfree(ci.write_buffer);
		return -ENOMEM;
	}
	/* device is non-seekable */
	nonseekable_open(inode, filp);

	filp->private_data = &ci;
	ci.num_opened = 1;

	return 0;
}

static int ci_bridge_ioctl_get_int(void *arg)
{
	int state;

	if (arg == NULL)
		return -EINVAL;

	state = gpio_get_value_cansleep(ci.gpio_interrupt_pin);
	if (copy_to_user(arg, &state, sizeof(state)))
		return -EFAULT;

	return 0;
}

static int ci_bridge_ioctl_reset(unsigned long arg)
{
	if ((arg != 0) && (arg != 1))
		return -EINVAL;

	gpio_set_value_cansleep(ci.gpio_reset_pin, arg);

	return 0;
}

static long ci_bridge_spi_ioctl(struct file *file, unsigned int cmd,
	unsigned long arg)
{
	int ret;

	switch (cmd) {

	case CI_BRIDGE_IOCTL_RESET:
		ret = ci_bridge_ioctl_reset(arg);
		break;

	case CI_BRIDGE_IOCTL_GET_INT_STATE:
		ret = ci_bridge_ioctl_get_int((void *) arg);
		break;

	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static int ci_bridge_spi_release(struct inode *inode, struct file *filp)
{
	struct ci_bridge *bridge = filp->private_data;

	if ((bridge == NULL) || (bridge->spi == NULL))
		return -ENODEV;

	kfree(bridge->write_buffer);
	kfree(bridge->read_buffer);
	filp->private_data = NULL;
	ci.num_opened = 0;

	return 0;
}

static const struct file_operations ci_bridge_spi_fops = {
	.owner   = THIS_MODULE,
	.read    = ci_bridge_spi_read,
	.write   = ci_bridge_spi_write,
	.open    = ci_bridge_spi_open,
	.unlocked_ioctl = ci_bridge_spi_ioctl,
	.release = ci_bridge_spi_release,
	.llseek  = no_llseek,
};

static int __init ci_bridge_init(void)
{
	int ret = 0;

	ret = alloc_chrdev_region(&ci.ci_bridge_dev, 0, 1, "ci_bridge_spi");
	if (ret != 0)
		return ret;

	ci.bridge_class = class_create(THIS_MODULE, "ci_bridge_spi");
	if (IS_ERR(ci.bridge_class)) {
		ret = PTR_ERR(ci.bridge_class);
		pr_err("Error creating ci.bridge_class: %d\n", ret);
		goto free_region;
	}

	cdev_init(&ci.cdev, &ci_bridge_spi_fops);
	ci.cdev.owner = THIS_MODULE;
	ret = cdev_add(&ci.cdev, ci.ci_bridge_dev, 1);
	if (ret != 0) {
		pr_err("Error calling cdev_add: %d\n", ret);
		goto class_destroy;
	}


	ci.bridge_dev = device_create(ci.bridge_class, NULL, ci.cdev.dev,
				     &ci, "ci_bridge_spi0");
	if (IS_ERR(ci.bridge_dev)) {
		ret = PTR_ERR(ci.bridge_dev);
		pr_err("device_create failed: %d\n", ret);
		goto del_cdev;
	}

	ret = spi_register_driver(&ci_bridge_driver);
	if (ret != 0) {
		pr_err("Error registering spi driver: %d\n", ret);
		goto device_destroy;
	}

	/* successful return */
	return 0;

device_destroy:
	device_destroy(ci.bridge_class, ci.ci_bridge_dev);

del_cdev:
	cdev_del(&ci.cdev);

class_destroy:
	class_destroy(ci.bridge_class);

free_region:
	unregister_chrdev_region(ci.ci_bridge_dev, 1);

	return ret;
}

static void __exit ci_bridge_exit(void)
{
	spi_unregister_driver(&ci_bridge_driver);
	device_destroy(ci.bridge_class, ci.ci_bridge_dev);
	cdev_del(&ci.cdev);
	class_destroy(ci.bridge_class);
	unregister_chrdev_region(ci.ci_bridge_dev, 1);
}

module_init(ci_bridge_init);
module_exit(ci_bridge_exit);

MODULE_DESCRIPTION("CI Bridge SPI Driver");
MODULE_LICENSE("GPL v2");