]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-picolcd_fb.c
HID: picoLCD: add myself to MAINTAINERS
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-picolcd_fb.c
CommitLineData
fabdbf2f
BP
1/***************************************************************************
2 * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
3 * *
4 * Based on Logitech G13 driver (v0.4) *
5 * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
6 * *
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. *
10 * *
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. *
15 * *
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 ***************************************************************************/
19
20#include <linux/hid.h>
21#include "usbhid/usbhid.h"
22#include <linux/usb.h>
23
24#include <linux/fb.h>
25#include <linux/module.h>
26
27#include "hid-picolcd.h"
28
29/* Framebuffer
30 *
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.
35 *
36 * The display can be updated at a tile granularity.
37 *
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 * +----------------+----------------+----------------+----------------+
44 * ...
45 * +----------------+----------------+----------------+----------------+
46 * | Tile 8 | Tile 8 | Tile 8 | Tile 8 |
47 * +----------------+----------------+----------------+----------------+
48 */
49#define PICOLCDFB_NAME "picolcdfb"
50#define PICOLCDFB_WIDTH (256)
51#define PICOLCDFB_HEIGHT (64)
52#define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
53
54#define PICOLCDFB_UPDATE_RATE_LIMIT 10
55#define PICOLCDFB_UPDATE_RATE_DEFAULT 2
56
57/* Framebuffer visual structures */
58static const struct fb_fix_screeninfo picolcdfb_fix = {
59 .id = PICOLCDFB_NAME,
60 .type = FB_TYPE_PACKED_PIXELS,
61 .visual = FB_VISUAL_MONO01,
62 .xpanstep = 0,
63 .ypanstep = 0,
64 .ywrapstep = 0,
65 .line_length = PICOLCDFB_WIDTH / 8,
66 .accel = FB_ACCEL_NONE,
67};
68
69static 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,
74 .width = 103,
75 .height = 26,
76 .bits_per_pixel = 1,
77 .grayscale = 1,
78 .red = {
79 .offset = 0,
80 .length = 1,
81 .msb_right = 0,
82 },
83 .green = {
84 .offset = 0,
85 .length = 1,
86 .msb_right = 0,
87 },
88 .blue = {
89 .offset = 0,
90 .length = 1,
91 .msb_right = 0,
92 },
93 .transp = {
94 .offset = 0,
95 .length = 0,
96 .msb_right = 0,
97 },
98};
99
100/* Send a given tile to PicoLCD */
101static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
102{
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);
106 unsigned long flags;
107 u8 *tdata;
108 int i;
109
110 if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
111 return -ENODEV;
112
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);
125
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);
130
131 tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
132 for (i = 0; i < 64; i++)
133 if (i < 32)
134 hid_set_field(report1->field[0], 11 + i, tdata[i]);
135 else
136 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
137
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);
141 return 0;
142}
143
144/* Translate a single tile*/
145static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
146 int chip, int tile)
147{
148 int i, b, changed = 0;
149 u8 tdata[64];
150 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
151
152 if (bpp == 1) {
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++) {
156 tdata[i] <<= 1;
157 tdata[i] |= (bdata[i/8] >> (i % 8)) & 0x01;
158 }
159 }
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++) {
164 tdata[i] <<= 1;
165 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
166 }
167 }
168 } else {
169 /* Oops, we should never get here! */
170 WARN_ON(1);
171 return 0;
172 }
173
174 for (i = 0; i < 64; i++)
175 if (tdata[i] != vdata[i]) {
176 changed = 1;
177 vdata[i] = tdata[i];
178 }
179 return changed;
180}
181
182void picolcd_fb_refresh(struct picolcd_data *data)
183{
184 if (data->fb_info)
185 schedule_delayed_work(&data->fb_info->deferred_work, 0);
186}
187
188/* Reconfigure LCD display */
189int picolcd_fb_reset(struct picolcd_data *data, int clear)
190{
191 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
192 int i, j;
193 unsigned long flags;
194 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
195
196 if (!report || report->maxfield != 1)
197 return -ENODEV;
198
199 spin_lock_irqsave(&data->lock, flags);
200 for (i = 0; i < 4; i++) {
201 for (j = 0; j < report->field[0]->maxusage; j++)
202 if (j == 0)
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]);
206 else
207 hid_set_field(report->field[0], j, 0);
208 usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
209 }
210
211 data->status |= PICOLCD_READY_FB;
212 spin_unlock_irqrestore(&data->lock, flags);
213
214 if (data->fb_bitmap) {
215 if (clear) {
216 memset(data->fb_vbitmap, 0, PICOLCDFB_SIZE);
217 memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
218 }
219 data->fb_force = 1;
220 }
221
222 /* schedule first output of framebuffer */
223 picolcd_fb_refresh(data);
224
225 return 0;
226}
227
228/* Update fb_vbitmap from the screen_base and send changed tiles to device */
a93ab849 229static void picolcd_fb_update(struct fb_info *info)
fabdbf2f
BP
230{
231 int chip, tile, n;
232 unsigned long flags;
a93ab849 233 struct picolcd_data *data;
fabdbf2f 234
a93ab849
BP
235 mutex_lock(&info->lock);
236 data = info->par;
fabdbf2f 237 if (!data)
a93ab849 238 goto out;
fabdbf2f
BP
239
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);
244 } else {
245 spin_unlock_irqrestore(&data->lock, flags);
246 }
247
248 /*
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.
252 *
253 * Wait for our IO to complete as otherwise we might flood the queue!
254 */
255 n = 0;
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) ||
260 data->fb_force) {
261 n += 2;
fabdbf2f 262 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
a93ab849 263 mutex_unlock(&info->lock);
fabdbf2f 264 usbhid_wait_io(data->hdev);
a93ab849
BP
265 mutex_lock(&info->lock);
266 data = info->par;
267 if (!data)
268 goto out;
269 spin_lock_irqsave(&data->lock, flags);
270 if (data->status & PICOLCD_FAILED) {
271 spin_unlock_irqrestore(&data->lock, flags);
272 goto out;
273 }
274 spin_unlock_irqrestore(&data->lock, flags);
fabdbf2f
BP
275 n = 0;
276 }
277 picolcd_fb_send_tile(data->hdev, chip, tile);
278 }
279 data->fb_force = false;
a93ab849
BP
280 if (n) {
281 mutex_unlock(&info->lock);
fabdbf2f 282 usbhid_wait_io(data->hdev);
a93ab849
BP
283 return;
284 }
285out:
286 mutex_unlock(&info->lock);
fabdbf2f
BP
287}
288
289/* Stub to call the system default and update the image on the picoLCD */
290static void picolcd_fb_fillrect(struct fb_info *info,
291 const struct fb_fillrect *rect)
292{
293 if (!info->par)
294 return;
295 sys_fillrect(info, rect);
296
297 schedule_delayed_work(&info->deferred_work, 0);
298}
299
300/* Stub to call the system default and update the image on the picoLCD */
301static void picolcd_fb_copyarea(struct fb_info *info,
302 const struct fb_copyarea *area)
303{
304 if (!info->par)
305 return;
306 sys_copyarea(info, area);
307
308 schedule_delayed_work(&info->deferred_work, 0);
309}
310
311/* Stub to call the system default and update the image on the picoLCD */
312static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
313{
314 if (!info->par)
315 return;
316 sys_imageblit(info, image);
317
318 schedule_delayed_work(&info->deferred_work, 0);
319}
320
321/*
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
324 */
325static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
326 size_t count, loff_t *ppos)
327{
328 ssize_t ret;
329 if (!info->par)
330 return -ENODEV;
331 ret = fb_sys_write(info, buf, count, ppos);
332 if (ret >= 0)
333 schedule_delayed_work(&info->deferred_work, 0);
334 return ret;
335}
336
337static int picolcd_fb_blank(int blank, struct fb_info *info)
338{
339 if (!info->par)
340 return -ENODEV;
341 /* We let fb notification do this for us via lcd/backlight device */
342 return 0;
343}
344
345static void picolcd_fb_destroy(struct fb_info *info)
346{
9966c37c 347 /* make sure no work is deferred */
a93ab849 348 fb_deferred_io_cleanup(info);
fabdbf2f 349
9966c37c
BP
350 vfree((u8 *)info->fix.smem_start);
351 framebuffer_release(info);
a93ab849 352 printk(KERN_DEBUG "picolcd_fb_destroy(%p)\n", info);
fabdbf2f
BP
353}
354
355static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
356{
357 __u32 bpp = var->bits_per_pixel;
358 __u32 activate = var->activate;
359
360 /* only allow 1/8 bit depth (8-bit is grayscale) */
361 *var = picolcdfb_var;
362 var->activate = activate;
363 if (bpp >= 8) {
364 var->bits_per_pixel = 8;
365 var->red.length = 8;
366 var->green.length = 8;
367 var->blue.length = 8;
368 } else {
369 var->bits_per_pixel = 1;
370 var->red.length = 1;
371 var->green.length = 1;
372 var->blue.length = 1;
373 }
374 return 0;
375}
376
377static int picolcd_set_par(struct fb_info *info)
378{
379 struct picolcd_data *data = info->par;
380 u8 *tmp_fb, *o_fb;
381 if (!data)
382 return -ENODEV;
383 if (info->var.bits_per_pixel == data->fb_bpp)
384 return 0;
385 /* switch between 1/8 bit depths */
386 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
387 return -EINVAL;
388
389 o_fb = data->fb_bitmap;
390 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
391 if (!tmp_fb)
392 return -ENOMEM;
393
394 /* translate FB content to new bits-per-pixel */
395 if (info->var.bits_per_pixel == 1) {
396 int i, b;
397 for (i = 0; i < PICOLCDFB_SIZE; i++) {
398 u8 p = 0;
399 for (b = 0; b < 8; b++) {
400 p <<= 1;
401 p |= o_fb[i*8+b] ? 0x01 : 0x00;
402 }
403 tmp_fb[i] = p;
404 }
405 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
406 info->fix.visual = FB_VISUAL_MONO01;
407 info->fix.line_length = PICOLCDFB_WIDTH / 8;
408 } else {
409 int i;
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;
415 }
416
417 kfree(tmp_fb);
418 data->fb_bpp = info->var.bits_per_pixel;
419 return 0;
420}
421
fabdbf2f
BP
422/* Note this can't be const because of struct fb_info definition */
423static struct fb_ops picolcdfb_ops = {
424 .owner = THIS_MODULE,
425 .fb_destroy = picolcd_fb_destroy,
fabdbf2f
BP
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,
434};
435
436
437/* Callback from deferred IO workqueue */
438static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
439{
a93ab849 440 picolcd_fb_update(info);
fabdbf2f
BP
441}
442
443static const struct fb_deferred_io picolcd_fb_defio = {
444 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
445 .deferred_io = picolcd_fb_deferred_io,
446};
447
448
449/*
450 * The "fb_update_rate" sysfs attribute
451 */
452static ssize_t picolcd_fb_update_rate_show(struct device *dev,
453 struct device_attribute *attr, char *buf)
454{
455 struct picolcd_data *data = dev_get_drvdata(dev);
456 unsigned i, fb_update_rate = data->fb_update_rate;
457 size_t ret = 0;
458
459 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
460 if (ret >= PAGE_SIZE)
461 break;
462 else if (i == fb_update_rate)
463 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
464 else
465 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
466 if (ret > 0)
467 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
468 return ret;
469}
470
471static ssize_t picolcd_fb_update_rate_store(struct device *dev,
472 struct device_attribute *attr, const char *buf, size_t count)
473{
474 struct picolcd_data *data = dev_get_drvdata(dev);
475 int i;
476 unsigned u;
477
478 if (count < 1 || count > 10)
479 return -EINVAL;
480
481 i = sscanf(buf, "%u", &u);
482 if (i != 1)
483 return -EINVAL;
484
485 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
486 return -ERANGE;
487 else if (u == 0)
488 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
489
490 data->fb_update_rate = u;
9966c37c 491 data->fb_info->fbdefio->delay = HZ / data->fb_update_rate;
fabdbf2f
BP
492 return count;
493}
494
495static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
496 picolcd_fb_update_rate_store);
497
498/* initialize Framebuffer device */
499int picolcd_init_framebuffer(struct picolcd_data *data)
500{
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;
506 u32 *palette;
507
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");
511 goto err_nomem;
512 }
513
514 fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
515 if (fb_vbitmap == NULL) {
516 dev_err(dev, "can't alloc vbitmap image buffer\n");
517 goto err_nomem;
518 }
519
520 data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
fabdbf2f 521 /* The extra memory is:
fabdbf2f 522 * - 256*u32 for pseudo_palette
9966c37c 523 * - struct fb_deferred_io
fabdbf2f 524 */
9966c37c
BP
525 info = framebuffer_alloc(256 * sizeof(u32) +
526 sizeof(struct fb_deferred_io), dev);
fabdbf2f
BP
527 if (info == NULL) {
528 dev_err(dev, "failed to allocate a framebuffer\n");
529 goto err_nomem;
530 }
531
9966c37c
BP
532 info->fbdefio = info->par;
533 *info->fbdefio = picolcd_fb_defio;
534 palette = info->par + sizeof(struct fb_deferred_io);
fabdbf2f
BP
535 for (i = 0; i < 256; i++)
536 palette[i] = i > 0 && i < 16 ? 0xff : 0;
537 info->pseudo_palette = palette;
fabdbf2f
BP
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;
544 info->par = data;
545 info->flags = FBINFO_FLAG_DEFAULT;
546
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);
551 if (error) {
552 dev_err(dev, "failed to configure display\n");
553 goto err_cleanup;
554 }
555 error = device_create_file(dev, &dev_attr_fb_update_rate);
556 if (error) {
557 dev_err(dev, "failed to create sysfs attributes\n");
558 goto err_cleanup;
559 }
560 fb_deferred_io_init(info);
561 data->fb_info = info;
562 error = register_framebuffer(info);
563 if (error) {
564 dev_err(dev, "failed to register framebuffer\n");
565 goto err_sysfs;
566 }
567 /* schedule first output of framebuffer */
568 data->fb_force = 1;
569 schedule_delayed_work(&info->deferred_work, 0);
570 return 0;
571
572err_sysfs:
573 fb_deferred_io_cleanup(info);
574 device_remove_file(dev, &dev_attr_fb_update_rate);
575err_cleanup:
576 data->fb_vbitmap = NULL;
577 data->fb_bitmap = NULL;
578 data->fb_bpp = 0;
579 data->fb_info = NULL;
580
581err_nomem:
582 framebuffer_release(info);
583 vfree(fb_bitmap);
584 kfree(fb_vbitmap);
585 return error;
586}
587
588void picolcd_exit_framebuffer(struct picolcd_data *data)
589{
590 struct fb_info *info = data->fb_info;
591 u8 *fb_vbitmap = data->fb_vbitmap;
592
593 if (!info)
594 return;
595
596 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
9966c37c 597 info->par = NULL;
fabdbf2f
BP
598 unregister_framebuffer(info);
599 data->fb_vbitmap = NULL;
600 data->fb_bitmap = NULL;
601 data->fb_bpp = 0;
602 data->fb_info = NULL;
603 kfree(fb_vbitmap);
604}