1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Filename: cfag12864bfb.c
4  *     Version: 0.1.0
5  * Description: cfag12864b LCD framebuffer driver
6  *     Depends: cfag12864b
7  *
8  *      Author: Copyright (C) Miguel Ojeda Sandonis
9  *        Date: 2006-10-31
10  */
11 
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/fb.h>
18 #include <linux/mm.h>
19 #include <linux/platform_device.h>
20 #include <linux/string.h>
21 #include <linux/uaccess.h>
22 #include <linux/cfag12864b.h>
23 
24 #define CFAG12864BFB_NAME "cfag12864bfb"
25 
26 static const struct fb_fix_screeninfo cfag12864bfb_fix = {
27 	.id = "cfag12864b",
28 	.type = FB_TYPE_PACKED_PIXELS,
29 	.visual = FB_VISUAL_MONO10,
30 	.xpanstep = 0,
31 	.ypanstep = 0,
32 	.ywrapstep = 0,
33 	.line_length = CFAG12864B_WIDTH / 8,
34 	.accel = FB_ACCEL_NONE,
35 };
36 
37 static const struct fb_var_screeninfo cfag12864bfb_var = {
38 	.xres = CFAG12864B_WIDTH,
39 	.yres = CFAG12864B_HEIGHT,
40 	.xres_virtual = CFAG12864B_WIDTH,
41 	.yres_virtual = CFAG12864B_HEIGHT,
42 	.bits_per_pixel = 1,
43 	.red = { 0, 1, 0 },
44       	.green = { 0, 1, 0 },
45       	.blue = { 0, 1, 0 },
46 	.left_margin = 0,
47 	.right_margin = 0,
48 	.upper_margin = 0,
49 	.lower_margin = 0,
50 	.vmode = FB_VMODE_NONINTERLACED,
51 };
52 
53 static int cfag12864bfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
54 {
55 	return vm_insert_page(vma, vma->vm_start,
56 		virt_to_page(cfag12864b_buffer));
57 }
58 
59 static struct fb_ops cfag12864bfb_ops = {
60 	.owner = THIS_MODULE,
61 	.fb_read = fb_sys_read,
62 	.fb_write = fb_sys_write,
63 	.fb_fillrect = sys_fillrect,
64 	.fb_copyarea = sys_copyarea,
65 	.fb_imageblit = sys_imageblit,
66 	.fb_mmap = cfag12864bfb_mmap,
67 };
68 
69 static int cfag12864bfb_probe(struct platform_device *device)
70 {
71 	int ret = -EINVAL;
72  	struct fb_info *info = framebuffer_alloc(0, &device->dev);
73 
74 	if (!info)
75 		goto none;
76 
77 	info->screen_base = (char __iomem *) cfag12864b_buffer;
78 	info->screen_size = CFAG12864B_SIZE;
79 	info->fbops = &cfag12864bfb_ops;
80 	info->fix = cfag12864bfb_fix;
81 	info->var = cfag12864bfb_var;
82 	info->pseudo_palette = NULL;
83 	info->par = NULL;
84 	info->flags = FBINFO_FLAG_DEFAULT;
85 
86 	if (register_framebuffer(info) < 0)
87 		goto fballoced;
88 
89 	platform_set_drvdata(device, info);
90 
91 	fb_info(info, "%s frame buffer device\n", info->fix.id);
92 
93 	return 0;
94 
95 fballoced:
96 	framebuffer_release(info);
97 
98 none:
99 	return ret;
100 }
101 
102 static int cfag12864bfb_remove(struct platform_device *device)
103 {
104 	struct fb_info *info = platform_get_drvdata(device);
105 
106 	if (info) {
107 		unregister_framebuffer(info);
108 		framebuffer_release(info);
109 	}
110 
111 	return 0;
112 }
113 
114 static struct platform_driver cfag12864bfb_driver = {
115 	.probe	= cfag12864bfb_probe,
116 	.remove = cfag12864bfb_remove,
117 	.driver = {
118 		.name	= CFAG12864BFB_NAME,
119 	},
120 };
121 
122 static struct platform_device *cfag12864bfb_device;
123 
124 static int __init cfag12864bfb_init(void)
125 {
126 	int ret = -EINVAL;
127 
128 	/* cfag12864b_init() must be called first */
129 	if (!cfag12864b_isinited()) {
130 		printk(KERN_ERR CFAG12864BFB_NAME ": ERROR: "
131 			"cfag12864b is not initialized\n");
132 		goto none;
133 	}
134 
135 	if (cfag12864b_enable()) {
136 		printk(KERN_ERR CFAG12864BFB_NAME ": ERROR: "
137 			"can't enable cfag12864b refreshing (being used)\n");
138 		return -ENODEV;
139 	}
140 
141 	ret = platform_driver_register(&cfag12864bfb_driver);
142 
143 	if (!ret) {
144 		cfag12864bfb_device =
145 			platform_device_alloc(CFAG12864BFB_NAME, 0);
146 
147 		if (cfag12864bfb_device)
148 			ret = platform_device_add(cfag12864bfb_device);
149 		else
150 			ret = -ENOMEM;
151 
152 		if (ret) {
153 			platform_device_put(cfag12864bfb_device);
154 			platform_driver_unregister(&cfag12864bfb_driver);
155 		}
156 	}
157 
158 none:
159 	return ret;
160 }
161 
162 static void __exit cfag12864bfb_exit(void)
163 {
164 	platform_device_unregister(cfag12864bfb_device);
165 	platform_driver_unregister(&cfag12864bfb_driver);
166 	cfag12864b_disable();
167 }
168 
169 module_init(cfag12864bfb_init);
170 module_exit(cfag12864bfb_exit);
171 
172 MODULE_LICENSE("GPL v2");
173 MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>");
174 MODULE_DESCRIPTION("cfag12864b LCD framebuffer driver");
175