summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_x1000/pwm-x1000.c
blob: 8530a38af566bb0aaabc659df51883aa4e75b4e6 (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
/***************************************************************************
 *             __________               __   ___.
 *   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 "pwm-x1000.h"
#include "clk-x1000.h"
#include "gpio-x1000.h"
#include "system.h"
#include "kernel.h"
#include "x1000/tcu.h"

struct pwm_gpio_data {
    int port;
    unsigned pin;
    int func;
};

struct pwm_state {
    int period_ns;
    int duty_ns;
    int full_ticks;
    int half_ticks;
    int prescaler;
};

static const struct pwm_gpio_data pwm_gpios[] = {
    {GPIO_C, 1 << 25, GPIO_DEVICE(0)},
    {GPIO_C, 1 << 26, GPIO_DEVICE(1)},
    {GPIO_C, 1 << 27, GPIO_DEVICE(1)},
    {GPIO_B, 1 <<  6, GPIO_DEVICE(2)},
    {GPIO_C, 1 << 24, GPIO_DEVICE(0)},
};

static struct pwm_state pwm_state[] = {
    {-1, -1, -1, -1, -1},
    {-1, -1, -1, -1, -1},
    {-1, -1, -1, -1, -1},
    {-1, -1, -1, -1, -1},
    {-1, -1, -1, -1, -1},
};

void pwm_init(int chn)
{
    /* clear cached state */
    struct pwm_state* st = &pwm_state[chn];
    st->period_ns = -1;
    st->duty_ns = -1;
    st->full_ticks = -1;
    st->prescaler = -1;
    st->prescaler = -1;

    /* clear GPIO and disable timer */
    const struct pwm_gpio_data* pg = &pwm_gpios[chn];
    gpio_config(pg->port, pg->pin, GPIO_OUTPUT(0));
    jz_clr(TCU_STOP, 1 << chn);
    jz_clr(TCU_ENABLE, 1 << chn);
    jz_set(TCU_STOP, 1 << chn);
}

void pwm_set_period(int chn, int period_ns, int duty_ns)
{
    struct pwm_state* st = &pwm_state[chn];
    unsigned long long tmp;
    int full_ticks = st->full_ticks;
    int half_ticks = st->half_ticks;
    int prescaler = st->prescaler;

    if(period_ns != st->period_ns) {
        /* calculate full tick period and prescaler */
        tmp = clk_get(X1000_CLK_PCLK) / 1000000;
        tmp *= period_ns;
        tmp /= 1000;

        prescaler = 0;
        while(tmp > 0xffff && prescaler < 5) {
            tmp /= 4;
            prescaler += 1;
        }

        full_ticks = (tmp > 0xffff) ? 0xffff : tmp;
        st->period_ns = period_ns;
    }

    if(duty_ns != st->duty_ns) {
        /* calculate half tick value */
        tmp = full_ticks;
        tmp *= duty_ns;
        tmp /= period_ns;

        half_ticks = (tmp > 0xffff) ? 0xffff : tmp;
        if(half_ticks >= full_ticks)
            half_ticks = full_ticks - 1;
        st->duty_ns = duty_ns;
    }

    /* need to clear STOP bit to access timer unit registers */
    int was_stopped = !!(jz_read(TCU_STOP) & (1 << chn));
    if(was_stopped)
        jz_clr(TCU_STOP, 1 << chn);

    /* check if timer is currently running */
    int was_enabled = !!(jz_read(TCU_ENABLE) & (1 << chn));
    int enabled = was_enabled;

    if(prescaler != st->prescaler) {
        /* must disable timer to change these settings */
        if(was_enabled) {
            jz_clr(TCU_ENABLE, 1 << chn);
            enabled = 0;
        }

        jz_overwritef(TCU_CTRL(chn), SHUTDOWN_V(GRACEFUL), INIT_LVL(0),
                      PWM_EN(1), PRESCALE(prescaler), SOURCE_V(PCLK));
        REG_TCU_COUNT(chn) = 0;
        st->prescaler = prescaler;
    }

    if(full_ticks != st->full_ticks || half_ticks != st->half_ticks) {
        if(enabled) {
            /* avoid changing PWM settings in the middle of a cycle,
             * because for some reason, that is supposed to be "bad" */
            jz_clr(TCU_FLAG, 1 << chn);
            while((REG_TCU_FLAG & (1 << chn)) == 0);
        }

        /* these can be changed while the timer is running */
        REG_TCU_CMP_FULL(chn) = full_ticks;
        REG_TCU_CMP_HALF(chn) = full_ticks - half_ticks;
        st->full_ticks = full_ticks;
        st->half_ticks = half_ticks;
    }

    /* restore the enable/stop state */
    if(was_enabled && !enabled)
        jz_set(TCU_ENABLE, 1 << chn);
    if(was_stopped)
        jz_set(TCU_STOP, 1 << chn);
}

void pwm_enable(int chn)
{
    /* Start timer */
    jz_clr(TCU_STOP, 1 << chn);
    jz_set(TCU_ENABLE, 1 << chn);

    /* Configure GPIO function */
    const struct pwm_gpio_data* pg = &pwm_gpios[chn];
    gpio_config(pg->port, pg->pin, pg->func);
}

void pwm_disable(int chn)
{
    /* Set GPIO to output 0 */
    const struct pwm_gpio_data* pg = &pwm_gpios[chn];
    gpio_config(pg->port, pg->pin, GPIO_OUTPUT(0));

    /* Stop timer */
    jz_clr(TCU_ENABLE, 1 << chn);
    jz_set(TCU_STOP, 1 << chn);
}