summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/imx233/lradc-imx233.c
blob: 4fe05f36f79e69eb80024ead1648333476ccf134 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 by Amaury Pouly
 *
 * 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 "system.h"
#include "system-target.h"
#include "lradc-imx233.h"
#include "kernel-imx233.h"
#include "stdlib.h"

/* channels */
static struct channel_arbiter_t channel_arbiter;
/* delay channels */
static struct channel_arbiter_t delay_arbiter;
/* battery is very special, dedicate a channel and a delay to it */
static int battery_chan;
static int battery_delay_chan;
/* irq callbacks */
static lradc_irq_fn_t irq_cb[LRADC_NUM_CHANNELS];

#define define_cb(x) \
    void INT_LRADC_CH##x(void) \
    { \
        INT_LRADC_CH(x); \
    }

void INT_LRADC_CH(int chan)
{
    if(irq_cb[chan])
        irq_cb[chan](chan);
}

define_cb(0)
define_cb(1)
define_cb(2)
define_cb(3)
define_cb(4)
define_cb(5)
define_cb(6)
define_cb(7)

void imx233_lradc_set_channel_irq_callback(int channel, lradc_irq_fn_t cb)
{
    irq_cb[channel] = cb;
}

void imx233_lradc_setup_channel(int channel, bool div2, bool acc, int nr_samples, int src)
{
    HW_LRADC_CHn_CLR(channel) = BM_OR2(LRADC_CHn, NUM_SAMPLES, ACCUMULATE);
    HW_LRADC_CHn_SET(channel) = BF_OR2(LRADC_CHn, NUM_SAMPLES(nr_samples), ACCUMULATE(acc));
    if(div2)
        BF_SETV(LRADC_CTRL2, DIVIDE_BY_TWO, 1 << channel);
    else
        BF_CLRV(LRADC_CTRL2, DIVIDE_BY_TWO, 1 << channel);
    HW_LRADC_CTRL4_CLR = BM_LRADC_CTRL4_LRADCxSELECT(channel);
    HW_LRADC_CTRL4_SET = src << BP_LRADC_CTRL4_LRADCxSELECT(channel);
}

void imx233_lradc_setup_delay(int dchan, int trigger_lradc, int trigger_delays,
    int loop_count, int delay)
{
    HW_LRADC_DELAYn(dchan) = BF_OR4(LRADC_DELAYn, TRIGGER_LRADCS(trigger_lradc),
        TRIGGER_DELAYS(trigger_delays), LOOP_COUNT(loop_count), DELAY(delay));
}

void imx233_lradc_clear_channel_irq(int channel)
{
    BF_CLR(LRADC_CTRL1, LRADCx_IRQ(channel));
}

bool imx233_lradc_read_channel_irq(int channel)
{
    return BF_RD(LRADC_CTRL1, LRADCx_IRQ(channel));
}

void imx233_lradc_enable_channel_irq(int channel, bool enable)
{
    if(enable)
        BF_SET(LRADC_CTRL1, LRADCx_IRQ_EN(channel));
    else
        BF_CLR(LRADC_CTRL1, LRADCx_IRQ_EN(channel));
    imx233_lradc_clear_channel_irq(channel);
}

void imx233_lradc_kick_channel(int channel)
{
    imx233_lradc_clear_channel_irq(channel);
    BF_SETV(LRADC_CTRL0, SCHEDULE, 1 << channel);
}

void imx233_lradc_kick_delay(int dchan)
{
    BF_SETn(LRADC_DELAYn, dchan, KICK);
}

void imx233_lradc_wait_channel(int channel)
{
    /* wait for completion */
    while(!imx233_lradc_read_channel_irq(channel))
        yield();
}

int imx233_lradc_read_channel(int channel)
{
    return BF_RDn(LRADC_CHn, channel, VALUE);
}

void imx233_lradc_clear_channel(int channel)
{
    BF_CLRn(LRADC_CHn, channel, VALUE);
}

int imx233_lradc_acquire_channel(int timeout)
{
    return arbiter_acquire(&channel_arbiter, timeout);
}

void imx233_lradc_release_channel(int chan)
{
    return arbiter_release(&channel_arbiter, chan);
}

void imx233_lradc_reserve_channel(int channel)
{
    return arbiter_reserve(&channel_arbiter, channel);
}

int imx233_lradc_acquire_delay(int timeout)
{
    return arbiter_acquire(&delay_arbiter, timeout);
}

void imx233_lradc_release_delay(int chan)
{
    return arbiter_release(&delay_arbiter, chan);
}

void imx233_lradc_reserve_delay(int channel)
{
    return arbiter_reserve(&delay_arbiter, channel);
}

int imx233_lradc_sense_die_temperature(int nmos_chan, int pmos_chan)
{
    imx233_lradc_setup_channel(nmos_chan, false, false, 0, LRADC_SRC_NMOS_THIN);
    imx233_lradc_setup_channel(pmos_chan, false, false, 0, LRADC_SRC_PMOS_THIN);
    // mux sensors
    BF_CLR(LRADC_CTRL2, TEMPSENSE_PWD);
    imx233_lradc_clear_channel(nmos_chan);
    imx233_lradc_clear_channel(pmos_chan);
    // schedule both channels
    imx233_lradc_kick_channel(nmos_chan);
    imx233_lradc_kick_channel(pmos_chan);
    // wait completion
    imx233_lradc_wait_channel(nmos_chan);
    imx233_lradc_wait_channel(pmos_chan);
    // mux sensors
    BF_SET(LRADC_CTRL2, TEMPSENSE_PWD);
    // do the computation
    int diff = imx233_lradc_read_channel(nmos_chan) - imx233_lradc_read_channel(pmos_chan);
    // return diff * 1.012 / 4
    return (diff * 1012) / 4000;
}

/* set to 0 to disable current source */
static void imx233_lradc_set_temp_isrc(int sensor, int value)
{
    if(sensor < 0 || sensor > 1)
        panicf("imx233_lradc_set_temp_isrc: invalid sensor");
    unsigned mask = sensor ? BM_LRADC_CTRL2_TEMP_ISRC0 : BM_LRADC_CTRL2_TEMP_ISRC1;
    unsigned bp = sensor ? BP_LRADC_CTRL2_TEMP_ISRC0 : BP_LRADC_CTRL2_TEMP_ISRC1;
    unsigned en = sensor ? BM_LRADC_CTRL2_TEMP_SENSOR_IENABLE0 : BM_LRADC_CTRL2_TEMP_SENSOR_IENABLE1;

    HW_LRADC_CTRL2_CLR = mask;
    HW_LRADC_CTRL2_SET = value << bp;
    if(value != 0)
    {
        HW_LRADC_CTRL2_SET = en;
        udelay(100);
    }
    else
        HW_LRADC_CTRL2_CLR = en;
}

int imx233_lradc_sense_ext_temperature(int chan, int sensor)
{
#define EXT_TEMP_ACC_COUNT  5
    /* setup channel */
    imx233_lradc_setup_channel(chan, false, false, 0, sensor);
    /* set current source to 300µA */
    imx233_lradc_set_temp_isrc(sensor, BV_LRADC_CTRL2_TEMP_ISRC0__300);
    /* read value and accumulate */
    int a = 0;
    for(int i = 0; i < EXT_TEMP_ACC_COUNT; i++)
    {
        imx233_lradc_clear_channel(chan);
        imx233_lradc_kick_channel(chan);
        imx233_lradc_wait_channel(chan);
        a += imx233_lradc_read_channel(chan);
    }
    /* setup channel for small accumulation */
    /* set current source to 20µA */
    imx233_lradc_set_temp_isrc(sensor, BV_LRADC_CTRL2_TEMP_ISRC0__20);
    /* read value */
    int b = 0;
    for(int i = 0; i < EXT_TEMP_ACC_COUNT; i++)
    {
        imx233_lradc_clear_channel(chan);
        imx233_lradc_kick_channel(chan);
        imx233_lradc_wait_channel(chan);
        b += imx233_lradc_read_channel(chan);
    }
    /* disable sensor current */
    imx233_lradc_set_temp_isrc(sensor, BV_LRADC_CTRL2_TEMP_ISRC0__ZERO);
    
    return (abs(b - a) / EXT_TEMP_ACC_COUNT) * 1104 / 1000;
}

void imx233_lradc_setup_battery_conversion(bool automatic, unsigned long scale_factor)
{
    BF_CLR(LRADC_CONVERSION, SCALE_FACTOR);
    BF_SETV(LRADC_CONVERSION, SCALE_FACTOR, scale_factor);
    if(automatic)
        BF_SET(LRADC_CONVERSION, AUTOMATIC);
    else
        BF_CLR(LRADC_CONVERSION, AUTOMATIC);
}

int imx233_lradc_read_battery_voltage(void)
{
    return BF_RD(LRADC_CONVERSION, SCALED_BATT_VOLTAGE);
}

void imx233_lradc_setup_touch(bool xminus_enable, bool yminus_enable,
    bool xplus_enable, bool yplus_enable, bool touch_detect)
{
    HW_LRADC_CTRL0_CLR = BM_OR5(LRADC_CTRL0, XMINUS_ENABLE, YMINUS_ENABLE,
        XPLUS_ENABLE, YPLUS_ENABLE, TOUCH_DETECT_ENABLE);
    HW_LRADC_CTRL0_SET = BF_OR5(LRADC_CTRL0, XMINUS_ENABLE(xminus_enable),
        YMINUS_ENABLE(yminus_enable), XPLUS_ENABLE(xplus_enable),
        YPLUS_ENABLE(yplus_enable), TOUCH_DETECT_ENABLE(touch_detect));
}

void imx233_lradc_enable_touch_detect_irq(bool enable)
{
    if(enable)
        BF_SET(LRADC_CTRL1, TOUCH_DETECT_IRQ_EN);
    else
        BF_CLR(LRADC_CTRL1, TOUCH_DETECT_IRQ_EN);
    imx233_lradc_clear_touch_detect_irq();
}

void imx233_lradc_clear_touch_detect_irq(void)
{
    BF_CLR(LRADC_CTRL1, TOUCH_DETECT_IRQ);
}

bool imx233_lradc_read_touch_detect(void)
{
    return BF_RD(LRADC_STATUS, TOUCH_DETECT_RAW);
}

void imx233_lradc_init(void)
{
    arbiter_init(&channel_arbiter, LRADC_NUM_CHANNELS);
    arbiter_init(&delay_arbiter, LRADC_NUM_DELAYS);
    // enable block
    imx233_reset_block(&HW_LRADC_CTRL0);
    // disable ground ref
    BF_CLR(LRADC_CTRL0, ONCHIP_GROUNDREF);
    // disable temperature sensors
    BF_CLR(LRADC_CTRL2, TEMP_SENSOR_IENABLE0);
    BF_CLR(LRADC_CTRL2, TEMP_SENSOR_IENABLE1);
    BF_SET(LRADC_CTRL2, TEMPSENSE_PWD);
    // set frequency
    BF_CLR(LRADC_CTRL3, CYCLE_TIME);
    BF_SETV(LRADC_CTRL3, CYCLE_TIME_V, 6MHZ);
    // setup battery
    battery_chan = 7;
    imx233_lradc_reserve_channel(battery_chan);
    /* setup them for the simplest use: no accumulation, no division*/
    imx233_lradc_setup_channel(battery_chan, false, false, 0, LRADC_SRC_BATTERY);
    /* setup delay channel for battery for automatic reading and scaling */
    battery_delay_chan = 0;
    imx233_lradc_reserve_delay(battery_delay_chan);
    /* setup delay to trigger battery channel and retrigger itself.
     * The counter runs at 2KHz so a delay of 200 will trigger 10
     * conversions per seconds */
    imx233_lradc_setup_delay(battery_delay_chan, 1 << battery_chan,
        1 << battery_delay_chan, 0, 200);
    imx233_lradc_kick_delay(battery_delay_chan);
    /* enable automatic conversion, use Li-Ion type battery */
    imx233_lradc_setup_battery_conversion(true, BV_LRADC_CONVERSION_SCALE_FACTOR__LI_ION);
}