diff options
author | Po Xu <jg_poxu@mediatek.com> | 2018-09-16 13:27:44 +0800 |
---|---|---|
committer | Julius Werner <jwerner@chromium.org> | 2018-11-02 19:57:47 +0000 |
commit | 96631f941dc3e958585cc4798d3c7c74c032c7bd (patch) | |
tree | d80e4b4daa2319578e70916fcfc06442568ac56c /src/soc/mediatek/mt8183/auxadc.c | |
parent | 7a70b664c4ba123dfa51a73fca87f67c168bffd3 (diff) |
mediatek/mt8183: Add AUXADC driver
We plan to get board id and RAM code from AUXADC on Kukui. Add AUXADC
driver to support it.
BUG=b:80501386
BRANCH=none
TEST=Boots correctly on Kukui
Change-Id: I121a6a0240f9c517c0cbc07e0c18b09167849ff1
Signed-off-by: Po Xu <jg_poxu@mediatek.com>
Reviewed-on: https://review.coreboot.org/29061
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'src/soc/mediatek/mt8183/auxadc.c')
-rw-r--r-- | src/soc/mediatek/mt8183/auxadc.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/soc/mediatek/mt8183/auxadc.c b/src/soc/mediatek/mt8183/auxadc.c new file mode 100644 index 0000000000..af88efb3b9 --- /dev/null +++ b/src/soc/mediatek/mt8183/auxadc.c @@ -0,0 +1,68 @@ +/* + * This file is part of the coreboot project. + * + * Copyright 2018 MediaTek Inc. + * + * 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; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include <arch/io.h> +#include <assert.h> +#include <console/console.h> +#include <delay.h> +#include <soc/addressmap.h> +#include <soc/auxadc.h> +#include <soc/infracfg.h> +#include <timer.h> + +static struct mtk_auxadc_regs *const mtk_auxadc = (void *)AUXADC_BASE; + +/* + * Wait until a condition becomes true or times out + * + * cond : a C expression to wait for + * timeout : msecs + */ +#define wait_ms(cond, timeout) \ +({ \ + struct stopwatch sw; \ + int expired = 0; \ + stopwatch_init_msecs_expire(&sw, timeout); \ + while (!(cond) && !(expired = stopwatch_expired(&sw))) \ + ; /* wait */ \ + assert(!expired); \ +}) + +static uint32_t auxadc_get_rawdata(int channel) +{ + setbits_le32(&mt8183_infracfg->module_sw_cg_1_clr, 1 << 10); + wait_ms(!(read32(&mtk_auxadc->con2) & 0x1), 300); + + clrbits_le32(&mtk_auxadc->con1, 1 << channel); + wait_ms(!(read32(&mtk_auxadc->data[channel]) & (1 << 12)), 300); + + setbits_le32(&mtk_auxadc->con1, 1 << channel); + udelay(25); + wait_ms(read32(&mtk_auxadc->data[channel]) & (1 << 12), 300); + + uint32_t value = read32(&mtk_auxadc->data[channel]) & 0x0FFF; + + setbits_le32(&mt8183_infracfg->module_sw_cg_1_set, 1 << 10); + + return value; +} + +int auxadc_get_voltage(unsigned int channel) +{ + assert(channel < 16); + + /* 1.5V in 4096 steps */ + return (int)((int64_t)auxadc_get_rawdata(channel) * 1500000 / 4096); +} |