summaryrefslogtreecommitdiff
path: root/drivers/char/mxs_viim.c
blob: 302d967d12e2156fd7a1691f2e7f26e30a4761cc (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
/*
 * Copyright 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/mm.h>
#include <linux/miscdevice.h>

static unsigned long iim_reg_base0, iim_reg_end0, iim_reg_size0;
static unsigned long iim_reg_base1, iim_reg_end1, iim_reg_size1;
static struct device *iim_dev;

/*!
 * MXS Virtual IIM interface - memory map function
 * This function maps one page size VIIM registers from VIIM base address0
 * if the size of the required virtual memory space is less than or equal to
 * one page size, otherwise this function will also map one page size VIIM
 * registers from VIIM base address1.
 *
 * @param file	     struct file *
 * @param vma	     structure vm_area_struct *
 *
 * @return	     Return 0 on success or negative error code on error
 */
static int mxs_viim_mmap(struct file *file, struct vm_area_struct *vma)
{
	size_t size = vma->vm_end - vma->vm_start;

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

	/* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
	if (remap_pfn_range(vma,
			    vma->vm_start,
			    iim_reg_base0 >> PAGE_SHIFT,
			    iim_reg_size0,
			    vma->vm_page_prot))
		return -EAGAIN;

	if (size > iim_reg_size0) {
		if (remap_pfn_range(vma,
				    vma->vm_start + iim_reg_size0,
				    iim_reg_base1 >> PAGE_SHIFT,
				    iim_reg_size1,
				    vma->vm_page_prot))
			return -EAGAIN;
	}

	return 0;
}

/*!
 * MXS Virtual IIM interface - open function
 *
 * @param inode	     struct inode *
 * @param filp	     struct file *
 *
 * @return	     Return 0 on success or negative error code on error
 */
static int mxs_viim_open(struct inode *inode, struct file *filp)
{
	return 0;
}

/*!
 * MXS Virtual IIM interface - release function
 *
 * @param inode	     struct inode *
 * @param filp	     struct file *
 *
 * @return	     Return 0 on success or negative error code on error
 */
static int mxs_viim_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static const struct file_operations mxs_viim_fops = {
	.mmap = mxs_viim_mmap,
	.open = mxs_viim_open,
	.release = mxs_viim_release,
};

static struct miscdevice mxs_viim_miscdev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "mxs_viim",
	.fops = &mxs_viim_fops,
};

/*!
 * This function is called by the driver framework to get virtual iim base/end
 * address and register iim misc device.
 *
 * @param	dev	The device structure for Virtual IIM passed in by the
 *			driver framework.
 *
 * @return      Returns 0 on success or negative error code on error
 */
static int mxs_viim_probe(struct platform_device *pdev)
{
	struct resource *res;
	int ret;

	iim_dev = &pdev->dev;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (IS_ERR(res)) {
		dev_err(iim_dev, "Unable to get Virtual IIM resource 0\n");
		return -ENODEV;
	}

	iim_reg_base0 = res->start;
	iim_reg_end0 = res->end;
	iim_reg_size0 = iim_reg_end0 - iim_reg_base0 + 1;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (IS_ERR(res)) {
		dev_err(iim_dev, "Unable to get Virtual IIM resource 1\n");
		return -ENODEV;
	}

	iim_reg_base1 = res->start;
	iim_reg_end1 = res->end;
	iim_reg_size1 = iim_reg_end1 - iim_reg_base1 + 1;

	ret = misc_register(&mxs_viim_miscdev);
	if (ret)
		return ret;

	return 0;
}

static int mxs_viim_remove(struct platform_device *pdev)
{
	misc_deregister(&mxs_viim_miscdev);
	return 0;
}

static struct platform_driver mxs_viim_driver = {
	.driver = {
		   .owner = THIS_MODULE,
		   .name = "mxs_viim",
		   },
	.probe = mxs_viim_probe,
	.remove = mxs_viim_remove,
};

static int __init mxs_viim_dev_init(void)
{
	return platform_driver_register(&mxs_viim_driver);
}

static void __exit mxs_viim_dev_cleanup(void)
{
	platform_driver_unregister(&mxs_viim_driver);
}

module_init(mxs_viim_dev_init);
module_exit(mxs_viim_dev_cleanup);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("MXS Virtual IIM driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);