summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/ft6x06.c
blob: 538ca1048085f257fd1dc45543a1d68f4773f53c (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 Aidan MacDonald
 *
 * 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 "ft6x06.h"
#include "kernel.h"
#include "i2c-async.h"
#include <string.h>

struct ft6x06_driver {
    /* i2c bus data */
    int i2c_cookie;
    i2c_descriptor i2c_desc;

    /* callback for touch events */
    ft6x06_event_cb event_cb;

    /* buffer for I2C transfers */
    uint8_t raw_data[6];
};

static struct ft6x06_driver ft_drv;
struct ft6x06_state ft6x06_state;

static void ft6x06_i2c_callback(int status, i2c_descriptor* desc)
{
    (void)desc;
    if(status != I2C_STATUS_OK)
        return;

    int evt = ft_drv.raw_data[1] >> 6;
    int tx = ft_drv.raw_data[2] | ((ft_drv.raw_data[1] & 0xf) << 8);
    int ty = ft_drv.raw_data[4] | ((ft_drv.raw_data[3] & 0xf) << 8);

    ft6x06_state.event = evt;
#ifdef FT6x06_SWAP_AXES
    ft6x06_state.pos_x = ty;
    ft6x06_state.pos_y = tx;
#else
    ft6x06_state.pos_x = tx;
    ft6x06_state.pos_y = ty;
#endif

    ft_drv.event_cb(evt, ft6x06_state.pos_x, ft6x06_state.pos_y);
}

static void ft6x06_dummy_event_cb(int evt, int tx, int ty)
{
    (void)evt;
    (void)tx;
    (void)ty;
}

void ft6x06_init(void)
{
    /* Initialize stuff */
    memset(&ft_drv, 0, sizeof(ft_drv));
    ft_drv.event_cb = ft6x06_dummy_event_cb;

    ft6x06_state.event = FT6x06_EVT_NONE;
    ft6x06_state.pos_x = 0;
    ft6x06_state.pos_y = 0;

    /* Reserve bus management cookie */
    ft_drv.i2c_cookie = i2c_async_reserve_cookies(FT6x06_BUS, 1);

    /* Prep an I2C descriptor to read touch data */
    ft_drv.i2c_desc.slave_addr = FT6x06_ADDR;
    ft_drv.i2c_desc.bus_cond   = I2C_START | I2C_STOP;
    ft_drv.i2c_desc.tran_mode  = I2C_READ;
    ft_drv.i2c_desc.buffer[0]  = &ft_drv.raw_data[5];
    ft_drv.i2c_desc.count[0]   = 1;
    ft_drv.i2c_desc.buffer[1]  = &ft_drv.raw_data[0];
    ft_drv.i2c_desc.count[1]   = 5;
    ft_drv.i2c_desc.callback   = ft6x06_i2c_callback;
    ft_drv.i2c_desc.arg        = 0;
    ft_drv.i2c_desc.next       = NULL;

    /* Set I2C register address */
    ft_drv.raw_data[5] = 0x02;
}

void ft6x06_set_event_cb(ft6x06_event_cb cb)
{
    ft_drv.event_cb = cb ? cb : ft6x06_dummy_event_cb;
}

void ft6x06_enable(bool en)
{
    i2c_reg_write1(FT6x06_BUS, FT6x06_ADDR, 0xa5, en ? 0 : 3);
}

void ft6x06_irq_handler(void)
{
    /* We don't care if this fails, there's not much we can do about it */
    i2c_async_queue(FT6x06_BUS, TIMEOUT_NOBLOCK, I2C_Q_ONCE,
                    ft_drv.i2c_cookie, &ft_drv.i2c_desc);
}