1 /***************************************************************************
2 * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
4 * Based on Logitech G13 driver (v0.4) *
5 * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, version 2 of the License. *
11 * This driver is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this software. If not see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
20 #include <linux/hid.h>
21 #include "usbhid/usbhid.h"
22 #include <linux/usb.h>
25 #include <linux/module.h>
27 #include "hid-picolcd.h"
31 * The PicoLCD use a Topway LCD module of 256x64 pixel
32 * This display area is tiled over 4 controllers with 8 tiles
33 * each. Each tile has 8x64 pixel, each data byte representing
34 * a 1-bit wide vertical line of the tile.
36 * The display can be updated at a tile granularity.
38 * Chip 1 Chip 2 Chip 3 Chip 4
39 * +----------------+----------------+----------------+----------------+
40 * | Tile 1 | Tile 1 | Tile 1 | Tile 1 |
41 * +----------------+----------------+----------------+----------------+
42 * | Tile 2 | Tile 2 | Tile 2 | Tile 2 |
43 * +----------------+----------------+----------------+----------------+
45 * +----------------+----------------+----------------+----------------+
46 * | Tile 8 | Tile 8 | Tile 8 | Tile 8 |
47 * +----------------+----------------+----------------+----------------+
49 #define PICOLCDFB_NAME "picolcdfb"
50 #define PICOLCDFB_WIDTH (256)
51 #define PICOLCDFB_HEIGHT (64)
52 #define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
54 #define PICOLCDFB_UPDATE_RATE_LIMIT 10
55 #define PICOLCDFB_UPDATE_RATE_DEFAULT 2
57 /* Framebuffer visual structures */
58 static const struct fb_fix_screeninfo picolcdfb_fix = {
60 .type = FB_TYPE_PACKED_PIXELS,
61 .visual = FB_VISUAL_MONO01,
65 .line_length = PICOLCDFB_WIDTH / 8,
66 .accel = FB_ACCEL_NONE,
69 static const struct fb_var_screeninfo picolcdfb_var = {
70 .xres = PICOLCDFB_WIDTH,
71 .yres = PICOLCDFB_HEIGHT,
72 .xres_virtual = PICOLCDFB_WIDTH,
73 .yres_virtual = PICOLCDFB_HEIGHT,
100 /* Send a given tile to PicoLCD */
101 static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
103 struct picolcd_data *data = hid_get_drvdata(hdev);
104 struct hid_report *report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, hdev);
105 struct hid_report *report2 = picolcd_out_report(REPORT_LCD_DATA, hdev);
110 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
113 spin_lock_irqsave(&data->lock, flags);
114 hid_set_field(report1->field[0], 0, chip << 2);
115 hid_set_field(report1->field[0], 1, 0x02);
116 hid_set_field(report1->field[0], 2, 0x00);
117 hid_set_field(report1->field[0], 3, 0x00);
118 hid_set_field(report1->field[0], 4, 0xb8 | tile);
119 hid_set_field(report1->field[0], 5, 0x00);
120 hid_set_field(report1->field[0], 6, 0x00);
121 hid_set_field(report1->field[0], 7, 0x40);
122 hid_set_field(report1->field[0], 8, 0x00);
123 hid_set_field(report1->field[0], 9, 0x00);
124 hid_set_field(report1->field[0], 10, 32);
126 hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
127 hid_set_field(report2->field[0], 1, 0x00);
128 hid_set_field(report2->field[0], 2, 0x00);
129 hid_set_field(report2->field[0], 3, 32);
131 tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
132 for (i = 0; i < 64; i++)
134 hid_set_field(report1->field[0], 11 + i, tdata[i]);
136 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
138 usbhid_submit_report(data->hdev, report1, USB_DIR_OUT);
139 usbhid_submit_report(data->hdev, report2, USB_DIR_OUT);
140 spin_unlock_irqrestore(&data->lock, flags);
144 /* Translate a single tile*/
145 static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
148 int i, b, changed = 0;
150 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
153 for (b = 7; b >= 0; b--) {
154 const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
155 for (i = 0; i < 64; i++) {
157 tdata[i] |= (bdata[i/8] >> (i % 8)) & 0x01;
160 } else if (bpp == 8) {
161 for (b = 7; b >= 0; b--) {
162 const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
163 for (i = 0; i < 64; i++) {
165 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
169 /* Oops, we should never get here! */
174 for (i = 0; i < 64; i++)
175 if (tdata[i] != vdata[i]) {
182 void picolcd_fb_refresh(struct picolcd_data *data)
185 schedule_delayed_work(&data->fb_info->deferred_work, 0);
188 /* Reconfigure LCD display */
189 int picolcd_fb_reset(struct picolcd_data *data, int clear)
191 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
194 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
196 if (!report || report->maxfield != 1)
199 spin_lock_irqsave(&data->lock, flags);
200 for (i = 0; i < 4; i++) {
201 for (j = 0; j < report->field[0]->maxusage; j++)
203 hid_set_field(report->field[0], j, i << 2);
204 else if (j < sizeof(mapcmd))
205 hid_set_field(report->field[0], j, mapcmd[j]);
207 hid_set_field(report->field[0], j, 0);
208 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
211 data->status |= PICOLCD_READY_FB;
212 spin_unlock_irqrestore(&data->lock, flags);
214 if (data->fb_bitmap) {
216 memset(data->fb_vbitmap, 0, PICOLCDFB_SIZE);
217 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
222 /* schedule first output of framebuffer */
223 picolcd_fb_refresh(data);
228 /* Update fb_vbitmap from the screen_base and send changed tiles to device */
229 static void picolcd_fb_update(struct fb_info *info)
233 struct picolcd_data *data;
235 mutex_lock(&info->lock);
240 spin_lock_irqsave(&data->lock, flags);
241 if (!(data->status & PICOLCD_READY_FB)) {
242 spin_unlock_irqrestore(&data->lock, flags);
243 picolcd_fb_reset(data, 0);
245 spin_unlock_irqrestore(&data->lock, flags);
249 * Translate the framebuffer into the format needed by the PicoLCD.
250 * See display layout above.
251 * Do this one tile after the other and push those tiles that changed.
253 * Wait for our IO to complete as otherwise we might flood the queue!
256 for (chip = 0; chip < 4; chip++)
257 for (tile = 0; tile < 8; tile++)
258 if (picolcd_fb_update_tile(data->fb_vbitmap,
259 data->fb_bitmap, data->fb_bpp, chip, tile) ||
262 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
263 mutex_unlock(&info->lock);
264 usbhid_wait_io(data->hdev);
265 mutex_lock(&info->lock);
269 spin_lock_irqsave(&data->lock, flags);
270 if (data->status & PICOLCD_FAILED) {
271 spin_unlock_irqrestore(&data->lock, flags);
274 spin_unlock_irqrestore(&data->lock, flags);
277 picolcd_fb_send_tile(data->hdev, chip, tile);
279 data->fb_force = false;
281 mutex_unlock(&info->lock);
282 usbhid_wait_io(data->hdev);
286 mutex_unlock(&info->lock);
289 /* Stub to call the system default and update the image on the picoLCD */
290 static void picolcd_fb_fillrect(struct fb_info *info,
291 const struct fb_fillrect *rect)
295 sys_fillrect(info, rect);
297 schedule_delayed_work(&info->deferred_work, 0);
300 /* Stub to call the system default and update the image on the picoLCD */
301 static void picolcd_fb_copyarea(struct fb_info *info,
302 const struct fb_copyarea *area)
306 sys_copyarea(info, area);
308 schedule_delayed_work(&info->deferred_work, 0);
311 /* Stub to call the system default and update the image on the picoLCD */
312 static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
316 sys_imageblit(info, image);
318 schedule_delayed_work(&info->deferred_work, 0);
322 * this is the slow path from userspace. they can seek and write to
323 * the fb. it's inefficient to do anything less than a full screen draw
325 static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
326 size_t count, loff_t *ppos)
331 ret = fb_sys_write(info, buf, count, ppos);
333 schedule_delayed_work(&info->deferred_work, 0);
337 static int picolcd_fb_blank(int blank, struct fb_info *info)
341 /* We let fb notification do this for us via lcd/backlight device */
345 static void picolcd_fb_destroy(struct fb_info *info)
347 /* make sure no work is deferred */
348 fb_deferred_io_cleanup(info);
350 vfree((u8 *)info->fix.smem_start);
351 framebuffer_release(info);
352 printk(KERN_DEBUG "picolcd_fb_destroy(%p)\n", info);
355 static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
357 __u32 bpp = var->bits_per_pixel;
358 __u32 activate = var->activate;
360 /* only allow 1/8 bit depth (8-bit is grayscale) */
361 *var = picolcdfb_var;
362 var->activate = activate;
364 var->bits_per_pixel = 8;
366 var->green.length = 8;
367 var->blue.length = 8;
369 var->bits_per_pixel = 1;
371 var->green.length = 1;
372 var->blue.length = 1;
377 static int picolcd_set_par(struct fb_info *info)
379 struct picolcd_data *data = info->par;
383 if (info->var.bits_per_pixel == data->fb_bpp)
385 /* switch between 1/8 bit depths */
386 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
389 o_fb = data->fb_bitmap;
390 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
394 /* translate FB content to new bits-per-pixel */
395 if (info->var.bits_per_pixel == 1) {
397 for (i = 0; i < PICOLCDFB_SIZE; i++) {
399 for (b = 0; b < 8; b++) {
401 p |= o_fb[i*8+b] ? 0x01 : 0x00;
405 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
406 info->fix.visual = FB_VISUAL_MONO01;
407 info->fix.line_length = PICOLCDFB_WIDTH / 8;
410 memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
411 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
412 o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
413 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
414 info->fix.line_length = PICOLCDFB_WIDTH;
418 data->fb_bpp = info->var.bits_per_pixel;
422 /* Note this can't be const because of struct fb_info definition */
423 static struct fb_ops picolcdfb_ops = {
424 .owner = THIS_MODULE,
425 .fb_destroy = picolcd_fb_destroy,
426 .fb_read = fb_sys_read,
427 .fb_write = picolcd_fb_write,
428 .fb_blank = picolcd_fb_blank,
429 .fb_fillrect = picolcd_fb_fillrect,
430 .fb_copyarea = picolcd_fb_copyarea,
431 .fb_imageblit = picolcd_fb_imageblit,
432 .fb_check_var = picolcd_fb_check_var,
433 .fb_set_par = picolcd_set_par,
437 /* Callback from deferred IO workqueue */
438 static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
440 picolcd_fb_update(info);
443 static const struct fb_deferred_io picolcd_fb_defio = {
444 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
445 .deferred_io = picolcd_fb_deferred_io,
450 * The "fb_update_rate" sysfs attribute
452 static ssize_t picolcd_fb_update_rate_show(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 struct picolcd_data *data = dev_get_drvdata(dev);
456 unsigned i, fb_update_rate = data->fb_update_rate;
459 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
460 if (ret >= PAGE_SIZE)
462 else if (i == fb_update_rate)
463 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
465 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
467 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
471 static ssize_t picolcd_fb_update_rate_store(struct device *dev,
472 struct device_attribute *attr, const char *buf, size_t count)
474 struct picolcd_data *data = dev_get_drvdata(dev);
478 if (count < 1 || count > 10)
481 i = sscanf(buf, "%u", &u);
485 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
488 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
490 data->fb_update_rate = u;
491 data->fb_info->fbdefio->delay = HZ / data->fb_update_rate;
495 static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
496 picolcd_fb_update_rate_store);
498 /* initialize Framebuffer device */
499 int picolcd_init_framebuffer(struct picolcd_data *data)
501 struct device *dev = &data->hdev->dev;
502 struct fb_info *info = NULL;
503 int i, error = -ENOMEM;
504 u8 *fb_vbitmap = NULL;
505 u8 *fb_bitmap = NULL;
508 fb_bitmap = vmalloc(PICOLCDFB_SIZE*8);
509 if (fb_bitmap == NULL) {
510 dev_err(dev, "can't get a free page for framebuffer\n");
514 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
515 if (fb_vbitmap == NULL) {
516 dev_err(dev, "can't alloc vbitmap image buffer\n");
520 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
521 /* The extra memory is:
522 * - 256*u32 for pseudo_palette
523 * - struct fb_deferred_io
525 info = framebuffer_alloc(256 * sizeof(u32) +
526 sizeof(struct fb_deferred_io), dev);
528 dev_err(dev, "failed to allocate a framebuffer\n");
532 info->fbdefio = info->par;
533 *info->fbdefio = picolcd_fb_defio;
534 palette = info->par + sizeof(struct fb_deferred_io);
535 for (i = 0; i < 256; i++)
536 palette[i] = i > 0 && i < 16 ? 0xff : 0;
537 info->pseudo_palette = palette;
538 info->screen_base = (char __force __iomem *)fb_bitmap;
539 info->fbops = &picolcdfb_ops;
540 info->var = picolcdfb_var;
541 info->fix = picolcdfb_fix;
542 info->fix.smem_len = PICOLCDFB_SIZE*8;
543 info->fix.smem_start = (unsigned long)fb_bitmap;
545 info->flags = FBINFO_FLAG_DEFAULT;
547 data->fb_vbitmap = fb_vbitmap;
548 data->fb_bitmap = fb_bitmap;
549 data->fb_bpp = picolcdfb_var.bits_per_pixel;
550 error = picolcd_fb_reset(data, 1);
552 dev_err(dev, "failed to configure display\n");
555 error = device_create_file(dev, &dev_attr_fb_update_rate);
557 dev_err(dev, "failed to create sysfs attributes\n");
560 fb_deferred_io_init(info);
561 data->fb_info = info;
562 error = register_framebuffer(info);
564 dev_err(dev, "failed to register framebuffer\n");
567 /* schedule first output of framebuffer */
569 schedule_delayed_work(&info->deferred_work, 0);
573 fb_deferred_io_cleanup(info);
574 device_remove_file(dev, &dev_attr_fb_update_rate);
576 data->fb_vbitmap = NULL;
577 data->fb_bitmap = NULL;
579 data->fb_info = NULL;
582 framebuffer_release(info);
588 void picolcd_exit_framebuffer(struct picolcd_data *data)
590 struct fb_info *info = data->fb_info;
591 u8 *fb_vbitmap = data->fb_vbitmap;
596 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
598 unregister_framebuffer(info);
599 data->fb_vbitmap = NULL;
600 data->fb_bitmap = NULL;
602 data->fb_info = NULL;