summaryrefslogtreecommitdiff
path: root/firmware/target/arm/olympus/mrobe-100/lcd-remote-mr100.c
blob: aa3d228952f6b9fc30c8033c01c88bba8a708c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 Mark Arigo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "system.h"
#include "lcd-remote.h"
#include "lcd-remote-target.h"
#include "button.h"
#include "button-target.h"

/* Temporary defines until we sort out why the gui stuff doesn't like this size
   (I believe the status bar isn't doing sanity checks and is writing outside
   the frame buffer size). */
#define RC_WIDTH                79
#define RC_HEIGHT               16

#define RC_CONTRAST_MASK        0x000000ff
#define RC_SCREEN_ON            0x00000100
#define RC_BACKLIGHT_ON         0x00000200

#define RC_DEV_INIT             0x00001000
#define RC_DETECTED             0x00002000
#define RC_AWAKE                0x00004000
#define RC_POWER_OFF            0x00008000

#define RC_UPDATE_LCD           0x00010000
#define RC_UPDATE_CONTROLLER    0x00020000
#define RC_UPDATE_ICONS         0x00040000
#define RC_UPDATE_MASK          (RC_UPDATE_LCD|RC_UPDATE_CONTROLLER|RC_UPDATE_ICONS)

#define RC_TX_ERROR             0x00100000
#define RC_RX_ERROR             0x00200000
#define RC_TIMEOUT_ERROR        0x00400000
#define RC_ERROR_MASK           (RC_TX_ERROR|RC_RX_ERROR|RC_TIMEOUT_ERROR)

#define RC_FORCE_DETECT         0x80000000

#define RX_READY                0x01
#define TX_READY                0x20

#define POLL_TIMEOUT            50000

bool remote_initialized = false;
unsigned int rc_status = 0;
unsigned char rc_buf[5];

/* ================================================== */
/* Remote thread functions                            */
/*   These functions are private to the remote thread */
/* ================================================== */
static struct semaphore rc_thread_wakeup;
static unsigned int remote_thread_id;
static int remote_stack[256/sizeof(int)];
static const char * const remote_thread_name = "remote";

static bool remote_wait_ready(int ready_mask)
{
    unsigned long current;
    unsigned long start = USEC_TIMER;
    unsigned long timeout = start + POLL_TIMEOUT;

    rc_status &= ~RC_TIMEOUT_ERROR;

    if (start <= timeout)
    {
        do
        {
            if (SER1_LSR & ready_mask)
                return true;

            //~ sleep(1);

            current = USEC_TIMER;
        } while (current < timeout);
    }
    else
    {
        do
        {
            if (SER1_LSR & ready_mask)
                return true;

            //~ sleep(1);

            current = USEC_TIMER - POLL_TIMEOUT;
        } while (current < start);
    }

    rc_status |= RC_TIMEOUT_ERROR;
    return false;
}

static bool remote_rx(void)
{
    int i;
    unsigned char chksum[2];

    rc_status &= ~RC_RX_ERROR;

    for (i = 0; i < 5; i++)
    {
        if (!remote_wait_ready(RX_READY))
        {
            rc_status |= RC_RX_ERROR;
            return false;
        }

        rc_buf[i] = SER1_RBR;
    }

    /* check opcode */
    if ((rc_buf[0] & 0xf0) != 0xf0)
    {
        rc_status |= RC_RX_ERROR;
        return false;
    }

    /* verify the checksums */
    chksum[0] = chksum[1] = 0;
    for (i = 0; i < 3; i++)
    {
        chksum[0] ^= rc_buf[i];
        chksum[1] += rc_buf[i];
    }

    if ((chksum[0] != rc_buf[3]) && (chksum[1] != rc_buf[4]))
    {
        rc_status |= RC_RX_ERROR;
        return false;
    }

    /* reception error */
    if ((rc_buf[0] & 0x1) || (rc_buf[0] & 0x2) || (rc_buf[0] & 0x4))
    {
        rc_status |= RC_RX_ERROR;
        return false;
    }

    return true;
}

static bool remote_tx(unsigned char *data, int len)
{
    int i;
    unsigned char chksum[2];

    rc_status &= ~RC_TX_ERROR;
    
    chksum[0] = chksum[1] = 0;

    for (i = 0; i < len; i++)
    {
        if (!remote_wait_ready(TX_READY))
        {
            rc_status |= RC_TX_ERROR;
            return false;
        }

        SER1_THR = data[i];
        chksum[0] ^= data[i];
        chksum[1] += data[i];
    }

    for (i = 0; i < 2; i++)
    {
        if (!remote_wait_ready(TX_READY))
        {
            rc_status |= RC_TX_ERROR;
            return false;
        }

        SER1_THR = chksum[i];
    }

    return remote_rx();
}

static void remote_dev_enable(bool enable)
{
    if (enable)
    {
        outl(inl(0x70000018) | 0xaa000, 0x70000018);
        DEV_INIT2 &= ~0x800;

        GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_VAL, 0x80);
        GPIO_SET_BITWISE(GPIOL_OUTPUT_EN, 0x80);

        DEV_EN |= DEV_SER1;

        SER1_RBR;
        SER1_LCR = 0x80;
        SER1_DLL = 0x50;
        SER1_DLM = 0x00;
        SER1_LCR = 0x03;
        SER1_FCR = 0x07;

        rc_status |= RC_DEV_INIT;
    }
    else
    {
        outl(inl(0x70000018) & ~0xaa000, 0x70000018);
        DEV_INIT2 &= ~0x800;

        GPIO_SET_BITWISE(GPIOL_OUTPUT_VAL, 0x80);
        GPIO_SET_BITWISE(GPIOL_OUTPUT_EN, 0x80);

        DEV_RS |= DEV_SER1;
        nop;
        DEV_RS &= ~DEV_SER1;

        DEV_EN &= ~DEV_SER1;

        rc_status &= ~RC_DEV_INIT;
    }
}

static void remote_update_lcd(void)
{
    int x, y, draw_now;
    unsigned char data[RC_WIDTH + 7];

    /* If the draw_now bit is set, the draw occurs directly on the LCD.
       Otherwise, the data is stored in an off-screen buffer and displayed
       the next time a draw operation is executed with this flag set. */
    draw_now = 0;

    for (y = 0; y < 2; y++)
    {
        data[0] = 0x51;
        data[1] = draw_now << 7;
        data[2] = RC_WIDTH;         /* width */
        data[3] = 0;                /* x1    */
        data[4] = y << 3;           /* y1    */
        data[5] = RC_WIDTH;         /* x2    */
        data[6] = (y + 1) << 3;     /* y2    */

        for (x = 0; x < RC_WIDTH; x++)
            data[x + 7] = lcd_remote_framebuffer[y][x];

        remote_tx(data, RC_WIDTH + 7);

        draw_now = 1;
    }
}

static void remote_update_controller(void)
{
    unsigned char data[3];

    data[0] = 0x31;
    data[1] = 0x00;
    if (rc_status & RC_SCREEN_ON)
        data[1] |= 0x80;
    if (rc_status & RC_BACKLIGHT_ON)
        data[1] |= 0x40;
    data[2] = (unsigned char)(rc_status & RC_CONTRAST_MASK);
    remote_tx(data, 3);
}

#if 0
static void remote_update_icons(unsigned char symbols)
{
    unsigned char data[2];

    if (!(rc_status & RC_AWAKE) && !(rc_status & RC_SCREEN_ON))
        return;

    data[0] = 0x41;
    data[1] = symbols;
    remote_tx(data, 2);
}
#endif

static bool remote_nop(void)
{
    unsigned char val[2];

    val[0] = 0x11;
    val[1] = 0x30;
    return remote_tx(val, 2);
}

static void remote_wake(void)
{
    if (remote_nop())
    {
        rc_status |= RC_AWAKE;
        return;
    }

    rc_status &= ~RC_AWAKE;
    return;
}

static void remote_sleep(void)
{
    unsigned char data[2];

    if (rc_status & RC_AWAKE)
    {
        data[0] = 0x71;
        data[1] = 0x30;
        remote_tx(data, 2);

        udelay(25000);
    }

    rc_status &= ~RC_AWAKE;
}

static void remote_off(void)
{
    if (rc_status & RC_AWAKE)
        remote_sleep();

    if (rc_status & RC_DEV_INIT)
        remote_dev_enable(false);

    rc_status &= ~(RC_DETECTED | RC_ERROR_MASK | RC_UPDATE_MASK);
    remote_initialized = false;
}

static void remote_on(void)
{
    if (!(rc_status & RC_DEV_INIT))
        remote_dev_enable(true);

    remote_wake();

    if (rc_status & RC_AWAKE)
    {
        rc_status |= RC_DETECTED;
        remote_initialized = true;

        rc_status |= (RC_UPDATE_MASK | RC_SCREEN_ON | RC_BACKLIGHT_ON);
        //~ remote_update_icons(0xf0); /* show battery */
    }
    else
    {
        rc_status &= ~RC_DETECTED;
        remote_initialized = false;
    }
}

static void remote_thread(void)
{
    int rc_thread_sleep_count = 10;
    int rc_thread_wait_timeout = TIMEOUT_BLOCK;

    while (1)
    {
        semaphore_wait(&rc_thread_wakeup, rc_thread_wait_timeout);

        /* Error handling (most likely due to remote not present) */
        if (rc_status & RC_ERROR_MASK)
        {
            if (--rc_thread_sleep_count == 0)
                rc_status |= RC_POWER_OFF;
        }

        /* Power-off (thread sleeps) */
        if (rc_status & RC_POWER_OFF)
        {
            remote_off();

            rc_thread_sleep_count = 10;
            rc_thread_wait_timeout = TIMEOUT_BLOCK;

            continue;
        }

        /* Detection */
        if (!(rc_status & RC_DETECTED))
        {
            rc_thread_wait_timeout = HZ;

            if (headphones_inserted())
            {
                remote_on();

                if (rc_status & RC_AWAKE)
                {
                    rc_thread_sleep_count = 10;
                    rc_thread_wait_timeout = HZ/20; /* ~50ms for updates */
                }
            }
            else
            {
                if (--rc_thread_sleep_count == 0)
                    rc_status &= ~RC_POWER_OFF;
            }

            continue;
        }

        /* Update the remote (one per wakeup cycle) */
        if (headphones_inserted() && (rc_status & RC_AWAKE))
        {
            if (rc_status & RC_SCREEN_ON)
            {
                /* In order of importance */
                if (rc_status & RC_UPDATE_CONTROLLER)
                {
                    remote_update_controller();
                    rc_status &= ~RC_UPDATE_CONTROLLER;
                }
                else if (rc_status & RC_UPDATE_LCD)
                {
                    remote_update_lcd();
                    rc_status &= ~RC_UPDATE_LCD;
                }
                else
                {
                    remote_nop();
                }
            }
            else
            {
                remote_nop();
            }
        }
    }
}

/* ============================================= */
/* Public functions                              */
/*   These should only set the update flags that */
/*   will be executed in the remote thread.      */
/* ============================================= */
bool lcd_remote_read_device(unsigned char *data)
{
    if (!(rc_status & RC_AWAKE) || (rc_status & RC_ERROR_MASK))
        return false;

    /* Return the most recent data. While the remote is plugged,
       this is updated ~50ms */
    data[0] = rc_buf[0];
    data[1] = rc_buf[1];
    data[2] = rc_buf[2];
    data[3] = rc_buf[3];
    data[4] = rc_buf[4];

    return true;
}

void lcd_remote_set_invert_display(bool yesno)
{
    /* dummy function...need to introduce HAVE_LCD_REMOTE_INVERT */
    (void)yesno;
}

/* turn the display upside down (call lcd_remote_update() afterwards) */
void lcd_remote_set_flip(bool yesno)
{
    /* dummy function...need to introduce HAVE_LCD_REMOTE_FLIP */
    (void)yesno;
}

int lcd_remote_default_contrast(void)
{
    return DEFAULT_REMOTE_CONTRAST_SETTING;
}

void lcd_remote_set_contrast(int val)
{
    rc_status = (rc_status & ~RC_CONTRAST_MASK) | (val & RC_CONTRAST_MASK);
    rc_status |= RC_UPDATE_CONTROLLER;
}

void lcd_remote_off(void)
{
    /* should only be used to power off at shutdown */
    rc_status |= RC_POWER_OFF;
    semaphore_release(&rc_thread_wakeup);

    /* wait until the things are powered off */
    while (rc_status & RC_DEV_INIT)
        sleep(HZ/10);
}

void lcd_remote_on(void)
{
    /* Only wake the remote thread if it's in the blocked state. */
    struct thread_entry *rc_thread = thread_id_entry(remote_thread_id);
    if (rc_thread->state == STATE_BLOCKED || (rc_status & RC_FORCE_DETECT))
    {
        rc_status &= ~RC_FORCE_DETECT;
        rc_status &= ~RC_POWER_OFF;
        semaphore_release(&rc_thread_wakeup);
    }
}

bool remote_detect(void)
{
    return (rc_status & RC_DETECTED);
}

void lcd_remote_init_device(void)
{
    /* reset */
    remote_dev_enable(false);
    rc_status |= RC_FORCE_DETECT; /* force detection at startup */

    /* unknown */
    GPIO_SET_BITWISE(GPIOL_ENABLE, 0x80);
    GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_VAL, 0x80);
    GPIO_SET_BITWISE(GPIOL_OUTPUT_EN, 0x80);

    /* a thread is required to poll & update the remote */
    semaphore_init(&rc_thread_wakeup, 1, 0);
    remote_thread_id = create_thread(remote_thread, remote_stack,
                            sizeof(remote_stack), 0, remote_thread_name
                            IF_PRIO(, PRIORITY_SYSTEM)
                            IF_COP(, CPU));
}

/* Update the display.
   This must be called after all other LCD functions that change the display. */
void lcd_remote_update(void)
{
    rc_status |= RC_UPDATE_LCD;
}

/* Update a fraction of the display. */
void lcd_remote_update_rect(int x, int y, int width, int height)
{
    (void)x;
    (void)y;
    (void)width;
    (void)height;

    rc_status |= RC_UPDATE_LCD;
}

void _remote_backlight_on(void)
{
    rc_status |= RC_BACKLIGHT_ON;
    rc_status |= RC_UPDATE_CONTROLLER;
}

void _remote_backlight_off(void)
{
    rc_status &= ~RC_BACKLIGHT_ON;
    rc_status |= RC_UPDATE_CONTROLLER;
}