blob: 0b4bffbe67c812ff71fcc385efdbfc8acde82af4 [file] [log] [blame]
Jaya Kumaraad09e52007-05-08 00:37:43 -07001/*
Jaya Kumar0e27aa32008-04-28 02:15:40 -07002 * linux/drivers/video/hecubafb.c -- FB driver for Hecuba/Apollo controller
Jaya Kumaraad09e52007-05-08 00:37:43 -07003 *
4 * Copyright (C) 2006, Jaya Kumar
5 * This work was sponsored by CIS(M) Sdn Bhd
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
12 * This work was possible because of apollo display code from E-Ink's website
13 * http://support.eink.com/community
14 * All information used to write this code is from public material made
15 * available by E-Ink on its support site. Some commands such as 0xA4
16 * were found by looping through cmd=0x00 thru 0xFF and supplying random
17 * values. There are other commands that the display is capable of,
18 * beyond the 5 used here but they are more complex.
19 *
Jaya Kumar0e27aa32008-04-28 02:15:40 -070020 * This driver is written to be used with the Hecuba display architecture.
21 * The actual display chip is called Apollo and the interface electronics
22 * it needs is called Hecuba.
Jaya Kumaraad09e52007-05-08 00:37:43 -070023 *
Jaya Kumar0e27aa32008-04-28 02:15:40 -070024 * It is intended to be architecture independent. A board specific driver
25 * must be used to perform all the physical IO interactions. An example
26 * is provided as n411.c
Jaya Kumaraad09e52007-05-08 00:37:43 -070027 *
28 */
29
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/errno.h>
33#include <linux/string.h>
34#include <linux/mm.h>
35#include <linux/slab.h>
36#include <linux/vmalloc.h>
37#include <linux/delay.h>
38#include <linux/interrupt.h>
39#include <linux/fb.h>
40#include <linux/init.h>
41#include <linux/platform_device.h>
42#include <linux/list.h>
Krzysztof Helt84902b72007-10-16 01:29:04 -070043#include <linux/uaccess.h>
Jaya Kumaraad09e52007-05-08 00:37:43 -070044
Jaya Kumar0e27aa32008-04-28 02:15:40 -070045#include <video/hecubafb.h>
Jaya Kumaraad09e52007-05-08 00:37:43 -070046
47/* Display specific information */
48#define DPY_W 600
49#define DPY_H 800
50
Jaya Kumaraad09e52007-05-08 00:37:43 -070051static struct fb_fix_screeninfo hecubafb_fix __devinitdata = {
52 .id = "hecubafb",
53 .type = FB_TYPE_PACKED_PIXELS,
54 .visual = FB_VISUAL_MONO01,
55 .xpanstep = 0,
56 .ypanstep = 0,
57 .ywrapstep = 0,
Jaya Kumar0e27aa32008-04-28 02:15:40 -070058 .line_length = DPY_W,
Jaya Kumaraad09e52007-05-08 00:37:43 -070059 .accel = FB_ACCEL_NONE,
60};
61
62static struct fb_var_screeninfo hecubafb_var __devinitdata = {
63 .xres = DPY_W,
64 .yres = DPY_H,
65 .xres_virtual = DPY_W,
66 .yres_virtual = DPY_H,
67 .bits_per_pixel = 1,
68 .nonstd = 1,
69};
70
Jaya Kumar0e27aa32008-04-28 02:15:40 -070071/* main hecubafb functions */
Jaya Kumaraad09e52007-05-08 00:37:43 -070072
Adrian Bunk9a268a62007-05-08 00:37:45 -070073static void apollo_send_data(struct hecubafb_par *par, unsigned char data)
Jaya Kumaraad09e52007-05-08 00:37:43 -070074{
75 /* set data */
Jaya Kumar0e27aa32008-04-28 02:15:40 -070076 par->board->set_data(par, data);
Jaya Kumaraad09e52007-05-08 00:37:43 -070077
78 /* set DS low */
Jaya Kumar0e27aa32008-04-28 02:15:40 -070079 par->board->set_ctl(par, HCB_DS_BIT, 0);
Jaya Kumaraad09e52007-05-08 00:37:43 -070080
Jaya Kumar0e27aa32008-04-28 02:15:40 -070081 /* wait for ack */
82 par->board->wait_for_ack(par, 0);
Jaya Kumaraad09e52007-05-08 00:37:43 -070083
84 /* set DS hi */
Jaya Kumar0e27aa32008-04-28 02:15:40 -070085 par->board->set_ctl(par, HCB_DS_BIT, 1);
Jaya Kumaraad09e52007-05-08 00:37:43 -070086
Jaya Kumar0e27aa32008-04-28 02:15:40 -070087 /* wait for ack to clear */
88 par->board->wait_for_ack(par, 1);
Jaya Kumaraad09e52007-05-08 00:37:43 -070089}
90
Adrian Bunk9a268a62007-05-08 00:37:45 -070091static void apollo_send_command(struct hecubafb_par *par, unsigned char data)
Jaya Kumaraad09e52007-05-08 00:37:43 -070092{
93 /* command so set CD to high */
Jaya Kumar0e27aa32008-04-28 02:15:40 -070094 par->board->set_ctl(par, HCB_CD_BIT, 1);
Jaya Kumaraad09e52007-05-08 00:37:43 -070095
96 /* actually strobe with command */
97 apollo_send_data(par, data);
98
99 /* clear CD back to low */
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700100 par->board->set_ctl(par, HCB_CD_BIT, 0);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700101}
102
Jaya Kumaraad09e52007-05-08 00:37:43 -0700103static void hecubafb_dpy_update(struct hecubafb_par *par)
104{
105 int i;
Antonino A. Daplas28cdf762007-05-08 00:38:50 -0700106 unsigned char *buf = (unsigned char __force *)par->info->screen_base;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700107
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700108 apollo_send_command(par, APOLLO_START_NEW_IMG);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700109
110 for (i=0; i < (DPY_W*DPY_H/8); i++) {
111 apollo_send_data(par, *(buf++));
112 }
113
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700114 apollo_send_command(par, APOLLO_STOP_IMG_DATA);
115 apollo_send_command(par, APOLLO_DISPLAY_IMG);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700116}
117
118/* this is called back from the deferred io workqueue */
119static void hecubafb_dpy_deferred_io(struct fb_info *info,
120 struct list_head *pagelist)
121{
122 hecubafb_dpy_update(info->par);
123}
124
125static void hecubafb_fillrect(struct fb_info *info,
126 const struct fb_fillrect *rect)
127{
128 struct hecubafb_par *par = info->par;
129
Antonino A. Daplasd6774932007-05-08 00:39:00 -0700130 sys_fillrect(info, rect);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700131
132 hecubafb_dpy_update(par);
133}
134
135static void hecubafb_copyarea(struct fb_info *info,
136 const struct fb_copyarea *area)
137{
138 struct hecubafb_par *par = info->par;
139
Antonino A. Daplasd6774932007-05-08 00:39:00 -0700140 sys_copyarea(info, area);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700141
142 hecubafb_dpy_update(par);
143}
144
145static void hecubafb_imageblit(struct fb_info *info,
146 const struct fb_image *image)
147{
148 struct hecubafb_par *par = info->par;
149
Antonino A. Daplasd6774932007-05-08 00:39:00 -0700150 sys_imageblit(info, image);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700151
152 hecubafb_dpy_update(par);
153}
154
155/*
156 * this is the slow path from userspace. they can seek and write to
157 * the fb. it's inefficient to do anything less than a full screen draw
158 */
Antonino A. Daplas3f9b0882007-05-08 00:39:02 -0700159static ssize_t hecubafb_write(struct fb_info *info, const char __user *buf,
Jaya Kumaraad09e52007-05-08 00:37:43 -0700160 size_t count, loff_t *ppos)
161{
Jaya Kumar963654a2008-04-28 02:15:37 -0700162 struct hecubafb_par *par = info->par;
163 unsigned long p = *ppos;
164 void *dst;
165 int err = 0;
166 unsigned long total_size;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700167
Jaya Kumar963654a2008-04-28 02:15:37 -0700168 if (info->state != FBINFO_STATE_RUNNING)
169 return -EPERM;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700170
Jaya Kumar963654a2008-04-28 02:15:37 -0700171 total_size = info->fix.smem_len;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700172
Jaya Kumar963654a2008-04-28 02:15:37 -0700173 if (p > total_size)
174 return -EFBIG;
175
176 if (count > total_size) {
177 err = -EFBIG;
178 count = total_size;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700179 }
180
Jaya Kumar963654a2008-04-28 02:15:37 -0700181 if (count + p > total_size) {
182 if (!err)
183 err = -ENOSPC;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700184
Jaya Kumar963654a2008-04-28 02:15:37 -0700185 count = total_size - p;
186 }
187
188 dst = (void __force *) (info->screen_base + p);
189
190 if (copy_from_user(dst, buf, count))
Jaya Kumaraad09e52007-05-08 00:37:43 -0700191 err = -EFAULT;
Jaya Kumar963654a2008-04-28 02:15:37 -0700192
193 if (!err)
194 *ppos += count;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700195
196 hecubafb_dpy_update(par);
197
Jaya Kumar963654a2008-04-28 02:15:37 -0700198 return (err) ? err : count;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700199}
200
201static struct fb_ops hecubafb_ops = {
202 .owner = THIS_MODULE,
Antonino A. Daplas28d45642007-05-08 00:39:06 -0700203 .fb_read = fb_sys_read,
Jaya Kumaraad09e52007-05-08 00:37:43 -0700204 .fb_write = hecubafb_write,
205 .fb_fillrect = hecubafb_fillrect,
206 .fb_copyarea = hecubafb_copyarea,
207 .fb_imageblit = hecubafb_imageblit,
208};
209
210static struct fb_deferred_io hecubafb_defio = {
211 .delay = HZ,
212 .deferred_io = hecubafb_dpy_deferred_io,
213};
214
215static int __devinit hecubafb_probe(struct platform_device *dev)
216{
217 struct fb_info *info;
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700218 struct hecuba_board *board;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700219 int retval = -ENOMEM;
220 int videomemorysize;
221 unsigned char *videomemory;
222 struct hecubafb_par *par;
223
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700224 /* pick up board specific routines */
225 board = dev->dev.platform_data;
226 if (!board)
227 return -EINVAL;
228
229 /* try to count device specific driver, if can't, platform recalls */
230 if (!try_module_get(board->owner))
231 return -ENODEV;
232
Jaya Kumaraad09e52007-05-08 00:37:43 -0700233 videomemorysize = (DPY_W*DPY_H)/8;
234
235 if (!(videomemory = vmalloc(videomemorysize)))
236 return retval;
237
238 memset(videomemory, 0, videomemorysize);
239
240 info = framebuffer_alloc(sizeof(struct hecubafb_par), &dev->dev);
241 if (!info)
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700242 goto err_fballoc;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700243
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700244 info->screen_base = (char __force __iomem *)videomemory;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700245 info->fbops = &hecubafb_ops;
246
247 info->var = hecubafb_var;
248 info->fix = hecubafb_fix;
249 info->fix.smem_len = videomemorysize;
250 par = info->par;
251 par->info = info;
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700252 par->board = board;
253 par->send_command = apollo_send_command;
254 par->send_data = apollo_send_data;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700255
Jaya Kumaraad09e52007-05-08 00:37:43 -0700256 info->flags = FBINFO_FLAG_DEFAULT;
257
258 info->fbdefio = &hecubafb_defio;
259 fb_deferred_io_init(info);
260
261 retval = register_framebuffer(info);
262 if (retval < 0)
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700263 goto err_fbreg;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700264 platform_set_drvdata(dev, info);
265
266 printk(KERN_INFO
267 "fb%d: Hecuba frame buffer device, using %dK of video memory\n",
268 info->node, videomemorysize >> 10);
269
270 /* this inits the dpy */
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700271 retval = par->board->init(par);
272 if (retval < 0)
273 goto err_fbreg;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700274
275 return 0;
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700276err_fbreg:
Jaya Kumaraad09e52007-05-08 00:37:43 -0700277 framebuffer_release(info);
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700278err_fballoc:
Jaya Kumaraad09e52007-05-08 00:37:43 -0700279 vfree(videomemory);
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700280 module_put(board->owner);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700281 return retval;
282}
283
284static int __devexit hecubafb_remove(struct platform_device *dev)
285{
286 struct fb_info *info = platform_get_drvdata(dev);
287
288 if (info) {
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700289 struct hecubafb_par *par = info->par;
Jaya Kumaraad09e52007-05-08 00:37:43 -0700290 fb_deferred_io_cleanup(info);
291 unregister_framebuffer(info);
Antonino A. Daplas28cdf762007-05-08 00:38:50 -0700292 vfree((void __force *)info->screen_base);
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700293 if (par->board->remove)
294 par->board->remove(par);
295 module_put(par->board->owner);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700296 framebuffer_release(info);
297 }
298 return 0;
299}
300
301static struct platform_driver hecubafb_driver = {
302 .probe = hecubafb_probe,
303 .remove = hecubafb_remove,
304 .driver = {
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700305 .owner = THIS_MODULE,
Jaya Kumaraad09e52007-05-08 00:37:43 -0700306 .name = "hecubafb",
307 },
308};
309
Jaya Kumaraad09e52007-05-08 00:37:43 -0700310static int __init hecubafb_init(void)
311{
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700312 return platform_driver_register(&hecubafb_driver);
Jaya Kumaraad09e52007-05-08 00:37:43 -0700313}
314
315static void __exit hecubafb_exit(void)
316{
Jaya Kumaraad09e52007-05-08 00:37:43 -0700317 platform_driver_unregister(&hecubafb_driver);
318}
319
Jaya Kumaraad09e52007-05-08 00:37:43 -0700320module_init(hecubafb_init);
321module_exit(hecubafb_exit);
322
Jaya Kumar0e27aa32008-04-28 02:15:40 -0700323MODULE_DESCRIPTION("fbdev driver for Hecuba/Apollo controller");
Jaya Kumaraad09e52007-05-08 00:37:43 -0700324MODULE_AUTHOR("Jaya Kumar");
325MODULE_LICENSE("GPL");