blob: 98f61de9a5e070a87d2d9fd5234f30fa9392ef4f [file] [log] [blame]
Bruno Prémontfabdbf22012-07-30 21:38:28 +02001/***************************************************************************
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>
Stephen Rothwelld1c60a02012-08-16 15:20:10 +100021#include <linux/vmalloc.h>
Bruno Prémontfabdbf22012-07-30 21:38:28 +020022#include "usbhid/usbhid.h"
Bruno Prémontfabdbf22012-07-30 21:38:28 +020023
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 */
Bruno Prémont16048702012-08-19 19:32:04 +0200101static int picolcd_fb_send_tile(struct picolcd_data *data, u8 *vbitmap,
102 int chip, int tile)
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200103{
Bruno Prémont16048702012-08-19 19:32:04 +0200104 struct hid_report *report1, *report2;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200105 unsigned long flags;
106 u8 *tdata;
107 int i;
108
Bruno Prémont16048702012-08-19 19:32:04 +0200109 report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, data->hdev);
110 if (!report1 || report1->maxfield != 1)
111 return -ENODEV;
112 report2 = picolcd_out_report(REPORT_LCD_DATA, data->hdev);
113 if (!report2 || report2->maxfield != 1)
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200114 return -ENODEV;
115
116 spin_lock_irqsave(&data->lock, flags);
Bruno Prémont16048702012-08-19 19:32:04 +0200117 if ((data->status & PICOLCD_FAILED)) {
118 spin_unlock_irqrestore(&data->lock, flags);
119 return -ENODEV;
120 }
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200121 hid_set_field(report1->field[0], 0, chip << 2);
122 hid_set_field(report1->field[0], 1, 0x02);
123 hid_set_field(report1->field[0], 2, 0x00);
124 hid_set_field(report1->field[0], 3, 0x00);
125 hid_set_field(report1->field[0], 4, 0xb8 | tile);
126 hid_set_field(report1->field[0], 5, 0x00);
127 hid_set_field(report1->field[0], 6, 0x00);
128 hid_set_field(report1->field[0], 7, 0x40);
129 hid_set_field(report1->field[0], 8, 0x00);
130 hid_set_field(report1->field[0], 9, 0x00);
131 hid_set_field(report1->field[0], 10, 32);
132
133 hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
134 hid_set_field(report2->field[0], 1, 0x00);
135 hid_set_field(report2->field[0], 2, 0x00);
136 hid_set_field(report2->field[0], 3, 32);
137
Bruno Prémont16048702012-08-19 19:32:04 +0200138 tdata = vbitmap + (tile * 4 + chip) * 64;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200139 for (i = 0; i < 64; i++)
140 if (i < 32)
141 hid_set_field(report1->field[0], 11 + i, tdata[i]);
142 else
143 hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
144
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100145 hid_hw_request(data->hdev, report1, HID_REQ_SET_REPORT);
146 hid_hw_request(data->hdev, report2, HID_REQ_SET_REPORT);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200147 spin_unlock_irqrestore(&data->lock, flags);
148 return 0;
149}
150
151/* Translate a single tile*/
152static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
153 int chip, int tile)
154{
155 int i, b, changed = 0;
156 u8 tdata[64];
157 u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
158
159 if (bpp == 1) {
160 for (b = 7; b >= 0; b--) {
161 const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
162 for (i = 0; i < 64; i++) {
163 tdata[i] <<= 1;
164 tdata[i] |= (bdata[i/8] >> (i % 8)) & 0x01;
165 }
166 }
167 } else if (bpp == 8) {
168 for (b = 7; b >= 0; b--) {
169 const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
170 for (i = 0; i < 64; i++) {
171 tdata[i] <<= 1;
172 tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
173 }
174 }
175 } else {
176 /* Oops, we should never get here! */
177 WARN_ON(1);
178 return 0;
179 }
180
181 for (i = 0; i < 64; i++)
182 if (tdata[i] != vdata[i]) {
183 changed = 1;
184 vdata[i] = tdata[i];
185 }
186 return changed;
187}
188
189void picolcd_fb_refresh(struct picolcd_data *data)
190{
191 if (data->fb_info)
192 schedule_delayed_work(&data->fb_info->deferred_work, 0);
193}
194
195/* Reconfigure LCD display */
196int picolcd_fb_reset(struct picolcd_data *data, int clear)
197{
198 struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
Bruno Prémont16048702012-08-19 19:32:04 +0200199 struct picolcd_fb_data *fbdata = data->fb_info->par;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200200 int i, j;
201 unsigned long flags;
202 static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
203
204 if (!report || report->maxfield != 1)
205 return -ENODEV;
206
207 spin_lock_irqsave(&data->lock, flags);
208 for (i = 0; i < 4; i++) {
209 for (j = 0; j < report->field[0]->maxusage; j++)
210 if (j == 0)
211 hid_set_field(report->field[0], j, i << 2);
212 else if (j < sizeof(mapcmd))
213 hid_set_field(report->field[0], j, mapcmd[j]);
214 else
215 hid_set_field(report->field[0], j, 0);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100216 hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200217 }
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200218 spin_unlock_irqrestore(&data->lock, flags);
219
Bruno Prémont16048702012-08-19 19:32:04 +0200220 if (clear) {
221 memset(fbdata->vbitmap, 0, PICOLCDFB_SIZE);
222 memset(fbdata->bitmap, 0, PICOLCDFB_SIZE*fbdata->bpp);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200223 }
Bruno Prémont16048702012-08-19 19:32:04 +0200224 fbdata->force = 1;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200225
226 /* schedule first output of framebuffer */
Bruno Prémont16048702012-08-19 19:32:04 +0200227 if (fbdata->ready)
228 schedule_delayed_work(&data->fb_info->deferred_work, 0);
229 else
230 fbdata->ready = 1;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200231
232 return 0;
233}
234
235/* Update fb_vbitmap from the screen_base and send changed tiles to device */
Bruno Prémonta93ab842012-07-30 21:38:57 +0200236static void picolcd_fb_update(struct fb_info *info)
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200237{
238 int chip, tile, n;
239 unsigned long flags;
Bruno Prémont16048702012-08-19 19:32:04 +0200240 struct picolcd_fb_data *fbdata = info->par;
Bruno Prémonta93ab842012-07-30 21:38:57 +0200241 struct picolcd_data *data;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200242
Bruno Prémonta93ab842012-07-30 21:38:57 +0200243 mutex_lock(&info->lock);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200244
Bruno Prémont16048702012-08-19 19:32:04 +0200245 spin_lock_irqsave(&fbdata->lock, flags);
246 if (!fbdata->ready && fbdata->picolcd)
247 picolcd_fb_reset(fbdata->picolcd, 0);
248 spin_unlock_irqrestore(&fbdata->lock, flags);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200249
250 /*
251 * Translate the framebuffer into the format needed by the PicoLCD.
252 * See display layout above.
253 * Do this one tile after the other and push those tiles that changed.
254 *
255 * Wait for our IO to complete as otherwise we might flood the queue!
256 */
257 n = 0;
258 for (chip = 0; chip < 4; chip++)
Bruno Prémont16048702012-08-19 19:32:04 +0200259 for (tile = 0; tile < 8; tile++) {
260 if (!fbdata->force && !picolcd_fb_update_tile(
261 fbdata->vbitmap, fbdata->bitmap,
262 fbdata->bpp, chip, tile))
263 continue;
264 n += 2;
265 if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
266 spin_lock_irqsave(&fbdata->lock, flags);
267 data = fbdata->picolcd;
268 spin_unlock_irqrestore(&fbdata->lock, flags);
269 mutex_unlock(&info->lock);
270 if (!data)
271 return;
272 usbhid_wait_io(data->hdev);
273 mutex_lock(&info->lock);
274 n = 0;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200275 }
Bruno Prémont16048702012-08-19 19:32:04 +0200276 spin_lock_irqsave(&fbdata->lock, flags);
277 data = fbdata->picolcd;
278 spin_unlock_irqrestore(&fbdata->lock, flags);
279 if (!data || picolcd_fb_send_tile(data,
280 fbdata->vbitmap, chip, tile))
281 goto out;
282 }
283 fbdata->force = false;
Bruno Prémonta93ab842012-07-30 21:38:57 +0200284 if (n) {
Bruno Prémont16048702012-08-19 19:32:04 +0200285 spin_lock_irqsave(&fbdata->lock, flags);
286 data = fbdata->picolcd;
287 spin_unlock_irqrestore(&fbdata->lock, flags);
Bruno Prémonta93ab842012-07-30 21:38:57 +0200288 mutex_unlock(&info->lock);
Bruno Prémont16048702012-08-19 19:32:04 +0200289 if (data)
290 usbhid_wait_io(data->hdev);
Bruno Prémonta93ab842012-07-30 21:38:57 +0200291 return;
292 }
293out:
294 mutex_unlock(&info->lock);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200295}
296
297/* Stub to call the system default and update the image on the picoLCD */
298static void picolcd_fb_fillrect(struct fb_info *info,
299 const struct fb_fillrect *rect)
300{
301 if (!info->par)
302 return;
303 sys_fillrect(info, rect);
304
305 schedule_delayed_work(&info->deferred_work, 0);
306}
307
308/* Stub to call the system default and update the image on the picoLCD */
309static void picolcd_fb_copyarea(struct fb_info *info,
310 const struct fb_copyarea *area)
311{
312 if (!info->par)
313 return;
314 sys_copyarea(info, area);
315
316 schedule_delayed_work(&info->deferred_work, 0);
317}
318
319/* Stub to call the system default and update the image on the picoLCD */
320static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
321{
322 if (!info->par)
323 return;
324 sys_imageblit(info, image);
325
326 schedule_delayed_work(&info->deferred_work, 0);
327}
328
329/*
330 * this is the slow path from userspace. they can seek and write to
331 * the fb. it's inefficient to do anything less than a full screen draw
332 */
333static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
334 size_t count, loff_t *ppos)
335{
336 ssize_t ret;
337 if (!info->par)
338 return -ENODEV;
339 ret = fb_sys_write(info, buf, count, ppos);
340 if (ret >= 0)
341 schedule_delayed_work(&info->deferred_work, 0);
342 return ret;
343}
344
345static int picolcd_fb_blank(int blank, struct fb_info *info)
346{
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200347 /* We let fb notification do this for us via lcd/backlight device */
348 return 0;
349}
350
351static void picolcd_fb_destroy(struct fb_info *info)
352{
Bruno Prémont16048702012-08-19 19:32:04 +0200353 struct picolcd_fb_data *fbdata = info->par;
354
Bruno Prémont9966c372012-07-30 21:38:46 +0200355 /* make sure no work is deferred */
Bruno Prémonta93ab842012-07-30 21:38:57 +0200356 fb_deferred_io_cleanup(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200357
Bruno Prémont16048702012-08-19 19:32:04 +0200358 /* No thridparty should ever unregister our framebuffer! */
359 WARN_ON(fbdata->picolcd != NULL);
360
Bruno Prémont9966c372012-07-30 21:38:46 +0200361 vfree((u8 *)info->fix.smem_start);
362 framebuffer_release(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200363}
364
365static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
366{
367 __u32 bpp = var->bits_per_pixel;
368 __u32 activate = var->activate;
369
370 /* only allow 1/8 bit depth (8-bit is grayscale) */
371 *var = picolcdfb_var;
372 var->activate = activate;
373 if (bpp >= 8) {
374 var->bits_per_pixel = 8;
375 var->red.length = 8;
376 var->green.length = 8;
377 var->blue.length = 8;
378 } else {
379 var->bits_per_pixel = 1;
380 var->red.length = 1;
381 var->green.length = 1;
382 var->blue.length = 1;
383 }
384 return 0;
385}
386
387static int picolcd_set_par(struct fb_info *info)
388{
Bruno Prémont16048702012-08-19 19:32:04 +0200389 struct picolcd_fb_data *fbdata = info->par;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200390 u8 *tmp_fb, *o_fb;
Bruno Prémont16048702012-08-19 19:32:04 +0200391 if (info->var.bits_per_pixel == fbdata->bpp)
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200392 return 0;
393 /* switch between 1/8 bit depths */
394 if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
395 return -EINVAL;
396
Bruno Prémont16048702012-08-19 19:32:04 +0200397 o_fb = fbdata->bitmap;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200398 tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
399 if (!tmp_fb)
400 return -ENOMEM;
401
402 /* translate FB content to new bits-per-pixel */
403 if (info->var.bits_per_pixel == 1) {
404 int i, b;
405 for (i = 0; i < PICOLCDFB_SIZE; i++) {
406 u8 p = 0;
407 for (b = 0; b < 8; b++) {
408 p <<= 1;
409 p |= o_fb[i*8+b] ? 0x01 : 0x00;
410 }
411 tmp_fb[i] = p;
412 }
413 memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
414 info->fix.visual = FB_VISUAL_MONO01;
415 info->fix.line_length = PICOLCDFB_WIDTH / 8;
416 } else {
417 int i;
418 memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
419 for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
420 o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
421 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
422 info->fix.line_length = PICOLCDFB_WIDTH;
423 }
424
425 kfree(tmp_fb);
Bruno Prémont16048702012-08-19 19:32:04 +0200426 fbdata->bpp = info->var.bits_per_pixel;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200427 return 0;
428}
429
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200430/* Note this can't be const because of struct fb_info definition */
431static struct fb_ops picolcdfb_ops = {
432 .owner = THIS_MODULE,
433 .fb_destroy = picolcd_fb_destroy,
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200434 .fb_read = fb_sys_read,
435 .fb_write = picolcd_fb_write,
436 .fb_blank = picolcd_fb_blank,
437 .fb_fillrect = picolcd_fb_fillrect,
438 .fb_copyarea = picolcd_fb_copyarea,
439 .fb_imageblit = picolcd_fb_imageblit,
440 .fb_check_var = picolcd_fb_check_var,
441 .fb_set_par = picolcd_set_par,
442};
443
444
445/* Callback from deferred IO workqueue */
446static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
447{
Bruno Prémonta93ab842012-07-30 21:38:57 +0200448 picolcd_fb_update(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200449}
450
451static const struct fb_deferred_io picolcd_fb_defio = {
452 .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
453 .deferred_io = picolcd_fb_deferred_io,
454};
455
456
457/*
458 * The "fb_update_rate" sysfs attribute
459 */
460static ssize_t picolcd_fb_update_rate_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
463 struct picolcd_data *data = dev_get_drvdata(dev);
Bruno Prémont16048702012-08-19 19:32:04 +0200464 struct picolcd_fb_data *fbdata = data->fb_info->par;
465 unsigned i, fb_update_rate = fbdata->update_rate;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200466 size_t ret = 0;
467
468 for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
469 if (ret >= PAGE_SIZE)
470 break;
471 else if (i == fb_update_rate)
472 ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
473 else
474 ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
475 if (ret > 0)
476 buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
477 return ret;
478}
479
480static ssize_t picolcd_fb_update_rate_store(struct device *dev,
481 struct device_attribute *attr, const char *buf, size_t count)
482{
483 struct picolcd_data *data = dev_get_drvdata(dev);
Bruno Prémont16048702012-08-19 19:32:04 +0200484 struct picolcd_fb_data *fbdata = data->fb_info->par;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200485 int i;
486 unsigned u;
487
488 if (count < 1 || count > 10)
489 return -EINVAL;
490
491 i = sscanf(buf, "%u", &u);
492 if (i != 1)
493 return -EINVAL;
494
495 if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
496 return -ERANGE;
497 else if (u == 0)
498 u = PICOLCDFB_UPDATE_RATE_DEFAULT;
499
Bruno Prémont16048702012-08-19 19:32:04 +0200500 fbdata->update_rate = u;
501 data->fb_info->fbdefio->delay = HZ / fbdata->update_rate;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200502 return count;
503}
504
505static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
506 picolcd_fb_update_rate_store);
507
508/* initialize Framebuffer device */
509int picolcd_init_framebuffer(struct picolcd_data *data)
510{
511 struct device *dev = &data->hdev->dev;
512 struct fb_info *info = NULL;
Bruno Prémont16048702012-08-19 19:32:04 +0200513 struct picolcd_fb_data *fbdata = NULL;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200514 int i, error = -ENOMEM;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200515 u32 *palette;
516
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200517 /* The extra memory is:
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200518 * - 256*u32 for pseudo_palette
Bruno Prémont9966c372012-07-30 21:38:46 +0200519 * - struct fb_deferred_io
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200520 */
Bruno Prémont9966c372012-07-30 21:38:46 +0200521 info = framebuffer_alloc(256 * sizeof(u32) +
Bruno Prémont16048702012-08-19 19:32:04 +0200522 sizeof(struct fb_deferred_io) +
523 sizeof(struct picolcd_fb_data) +
524 PICOLCDFB_SIZE, dev);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200525 if (info == NULL) {
526 dev_err(dev, "failed to allocate a framebuffer\n");
527 goto err_nomem;
528 }
529
Bruno Prémont9966c372012-07-30 21:38:46 +0200530 info->fbdefio = info->par;
531 *info->fbdefio = picolcd_fb_defio;
Bruno Prémont16048702012-08-19 19:32:04 +0200532 info->par += sizeof(struct fb_deferred_io);
533 palette = info->par;
534 info->par += 256 * sizeof(u32);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200535 for (i = 0; i < 256; i++)
536 palette[i] = i > 0 && i < 16 ? 0xff : 0;
537 info->pseudo_palette = palette;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200538 info->fbops = &picolcdfb_ops;
539 info->var = picolcdfb_var;
540 info->fix = picolcdfb_fix;
541 info->fix.smem_len = PICOLCDFB_SIZE*8;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200542 info->flags = FBINFO_FLAG_DEFAULT;
543
Bruno Prémont16048702012-08-19 19:32:04 +0200544 fbdata = info->par;
545 spin_lock_init(&fbdata->lock);
546 fbdata->picolcd = data;
547 fbdata->update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
548 fbdata->bpp = picolcdfb_var.bits_per_pixel;
549 fbdata->force = 1;
550 fbdata->vbitmap = info->par + sizeof(struct picolcd_fb_data);
551 fbdata->bitmap = vmalloc(PICOLCDFB_SIZE*8);
552 if (fbdata->bitmap == NULL) {
553 dev_err(dev, "can't get a free page for framebuffer\n");
554 goto err_nomem;
555 }
556 info->screen_base = (char __force __iomem *)fbdata->bitmap;
557 info->fix.smem_start = (unsigned long)fbdata->bitmap;
558 memset(fbdata->vbitmap, 0xff, PICOLCDFB_SIZE);
559 data->fb_info = info;
560
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200561 error = picolcd_fb_reset(data, 1);
562 if (error) {
563 dev_err(dev, "failed to configure display\n");
564 goto err_cleanup;
565 }
Bruno Prémont16048702012-08-19 19:32:04 +0200566
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200567 error = device_create_file(dev, &dev_attr_fb_update_rate);
568 if (error) {
569 dev_err(dev, "failed to create sysfs attributes\n");
570 goto err_cleanup;
571 }
Bruno Prémont16048702012-08-19 19:32:04 +0200572
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200573 fb_deferred_io_init(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200574 error = register_framebuffer(info);
575 if (error) {
576 dev_err(dev, "failed to register framebuffer\n");
577 goto err_sysfs;
578 }
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200579 return 0;
580
581err_sysfs:
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200582 device_remove_file(dev, &dev_attr_fb_update_rate);
Bruno Prémont16048702012-08-19 19:32:04 +0200583 fb_deferred_io_cleanup(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200584err_cleanup:
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200585 data->fb_info = NULL;
586
587err_nomem:
Bruno Prémont16048702012-08-19 19:32:04 +0200588 if (fbdata)
589 vfree(fbdata->bitmap);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200590 framebuffer_release(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200591 return error;
592}
593
594void picolcd_exit_framebuffer(struct picolcd_data *data)
595{
596 struct fb_info *info = data->fb_info;
Bruno Prémont16048702012-08-19 19:32:04 +0200597 struct picolcd_fb_data *fbdata = info->par;
598 unsigned long flags;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200599
600 device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
Bruno Prémont16048702012-08-19 19:32:04 +0200601
602 /* disconnect framebuffer from HID dev */
603 spin_lock_irqsave(&fbdata->lock, flags);
604 fbdata->picolcd = NULL;
605 spin_unlock_irqrestore(&fbdata->lock, flags);
606
607 /* make sure there is no running update - thus that fbdata->picolcd
608 * once obtained under lock is guaranteed not to get free() under
609 * the feet of the deferred work */
Linus Torvalds033d9952012-10-02 09:54:49 -0700610 flush_delayed_work(&info->deferred_work);
Bruno Prémont16048702012-08-19 19:32:04 +0200611
612 data->fb_info = NULL;
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200613 unregister_framebuffer(info);
Bruno Prémontfabdbf22012-07-30 21:38:28 +0200614}