diff --git a/drivers/Makefile b/drivers/Makefile index 8afe40a..83ae25e 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -1,3 +1,4 @@ # the drivers registration framework lib-y += framework.o lib-y += samsung/exynos-speedy.o +lib-y += samsung/exynos-acpm.o diff --git a/drivers/samsung/exynos-acpm.c b/drivers/samsung/exynos-acpm.c new file mode 100644 index 0000000..c169465 --- /dev/null +++ b/drivers/samsung/exynos-acpm.c @@ -0,0 +1,128 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2025, Ivaylo Ivanov + */ + +#include +#include +#include +#include + +void acpm_ipc_send_data_async(void *reg_base, unsigned int ipc_ch, unsigned int cmd0, unsigned int cmd1) +{ + unsigned int acpm_ipc = 0; + + acpm_ipc = (1 << ipc_ch) << INTR_FLAG_OFFSET; + + writel(cmd0, reg_base + SR0); + writel(cmd1, reg_base + SR1); + __asm__ volatile ("dsb sy"); + writel(acpm_ipc, reg_base + INTGR0); +} + +void acpm_ipc_send_data_wait(void *reg_base, unsigned int ipc_ch, unsigned int *cmd0, unsigned int *cmd1) +{ + unsigned int reg = 0; + + do { + reg = readl(reg_base + INTSR1) & (0x1 << ipc_ch); + } while (!(reg)); + + writel(0x1 << ipc_ch, reg_base + INTCR1); + + *cmd0 = readl(reg_base + SR2); + *cmd1 = readl(reg_base + SR3); +} + +void spmi_read(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, unsigned char *dest) +{ + unsigned int command[4] = {0,}; + int ret = 0; + + command[0] = set_protocol(type, TYPE) | set_protocol(reg, REG); + command[1] = set_protocol(FUNC_READ, FUNC) | set_protocol(sid, SID); + + acpm_ipc_send_data_async(reg_base, IPC_AP_SPMI, command[0], command[1]); + + acpm_ipc_send_data_wait(reg_base, IPC_AP_SPMI, &command[2], &command[3]); + + *dest = read_protocol(command[3], DEST); + ret = read_protocol(command[3], RETURN); + if (ret) { + ; + } +} + +void spmi_bulk_read(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, int count, unsigned char *buf) +{ + unsigned int command[6] = {0,}; + int i, ret; + + command[0] = set_protocol(type, TYPE) | set_protocol(reg, REG); + command[1] = set_protocol(FUNC_BULK_READ, FUNC) | set_protocol(count, CNT) | set_protocol(sid, SID); + + acpm_ipc_send_data_async(reg_base, IPC_AP_SPMI, command[0], command[1]); + + acpm_ipc_send_data_wait(reg_base, IPC_AP_SPMI, &command[2], &command[3]); + + ret = read_protocol(command[3], RETURN); + if (ret) + return; + + command[4] = readl(reg_base + SR0); + command[5] = readl(reg_base + SR1); + + for (i = 0; i < count; i++) { + if (i < 4) + buf[i] = read_bulk_protocol(command[4], BULK_VAL, i); + else + buf[i] = read_bulk_protocol(command[5], BULK_VAL, i - 4); + } +} + +void spmi_write(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, unsigned char value) +{ + unsigned int command[4] = {0,}; + int ret = 0; + + command[0] = set_protocol(type, TYPE) | set_protocol(reg, REG); + command[1] = set_protocol(FUNC_WRITE, FUNC) | set_protocol(value, WRITE_VAL) | set_protocol(sid, SID); + + + acpm_ipc_send_data_async(reg_base, IPC_AP_SPMI, command[0], command[1]); + + acpm_ipc_send_data_wait(reg_base, IPC_AP_SPMI, &command[2], &command[3]); + + ret = read_protocol(command[3], RETURN); + if (ret) { + ; + } +} + +void spmi_bulk_write(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, int count, unsigned char *buf) +{ + unsigned int command[4] = {0,}; + int ret = 0, i; + + command[0] = set_protocol(type, TYPE) | set_protocol(reg, REG); + command[1] = set_protocol(FUNC_BULK_WRITE, FUNC) | set_protocol(count, CNT) | set_protocol(sid, SID); + + for (i = 0; i < count; i++) { + if (i < 4) + command[2] |= set_bulk_protocol(buf[i], BULK_VAL, i); + else + command[3] |= set_bulk_protocol(buf[i], BULK_VAL, i - 4); + } + + writel(command[2], reg_base + SR2); + writel(command[3], reg_base + SR3); + + acpm_ipc_send_data_async(reg_base, IPC_AP_SPMI, command[0], command[1]); + + acpm_ipc_send_data_wait(reg_base, IPC_AP_SPMI, &command[2], &command[3]); + + ret = read_protocol(command[3], RETURN); + if (ret) { + ; + } +} diff --git a/include/drivers/samsung/exynos-acpm.h b/include/drivers/samsung/exynos-acpm.h new file mode 100644 index 0000000..bf2fc08 --- /dev/null +++ b/include/drivers/samsung/exynos-acpm.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ +/* + * Copyright (c) 2025 Ivaylo Ivanov + * + * Bindings for exynos-acpm + */ + +#ifndef EXYNOS_ACPM_H_ +#define EXYNOS_ACPM_H_ + +#define INTGR0 (0x0008) +#define INTCR1 (0x0020) +#define INTSR1 (0x0028) +#define SR0 (0x100) +#define SR1 (0x104) +#define SR2 (0x108) +#define SR3 (0x10C) +#define SR4 (0x110) +#define SR5 (0x114) + +#define IPC_AP_SPMI (8) +#define INTR_FLAG_OFFSET (16) + +void spmi_read(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, unsigned char *dest); +void spmi_write(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, unsigned char value); +void spmi_bulk_read(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, int count, unsigned char *buf); +void spmi_bulk_write(void *reg_base, unsigned char sid, unsigned char type, unsigned char reg, int count, unsigned char *buf); + +/* Define shift */ +#define SHIFT_BYTE (8) +#define SHIFT_REG (0) +#define SHIFT_TYPE (8) +#define SHIFT_FUNC (0) +#define SHIFT_DEST (8) +#define SHIFT_CNT (8) +#define SHIFT_WRITE_VAL (8) +#define SHIFT_UPDATE_VAL (8) +#define SHIFT_UPDATE_MASK (16) +#define SHIFT_RETURN (24) +#define SHIFT_BULK_VAL SHIFT_BYTE +#define SHIFT_SID (24) + +/* Define mask */ +#define MASK_BYTE (0xFF) +#define MASK_REG MASK_BYTE +#define MASK_TYPE MASK_BYTE +#define MASK_FUNC MASK_BYTE +#define MASK_DEST MASK_BYTE +#define MASK_CNT MASK_BYTE +#define MASK_WRITE_VAL MASK_BYTE +#define MASK_UPDATE_VAL MASK_BYTE +#define MASK_UPDATE_MASK MASK_BYTE +#define MASK_RETURN MASK_BYTE +#define MASK_BULK_VAL MASK_BYTE +#define MASK_SID (0xF) + +/* Command */ +#define set_protocol(data, protocol) ((data & MASK_##protocol) << SHIFT_##protocol) +#define set_bulk_protocol(data, protocol, n) ((data & MASK_##protocol) << (SHIFT_##protocol * (n))) +#define read_protocol(data, protocol) ((data >> SHIFT_##protocol) & MASK_##protocol) +#define read_bulk_protocol(data, protocol, n) ((data >> (SHIFT_##protocol * (n))) & MASK_##protocol) + +#ifndef pr_fmt +#define pr_fmt(fmt) fmt +#endif + +enum mfd_func { + FUNC_READ, + FUNC_WRITE, + FUNC_UPDATE, + FUNC_BULK_READ, + FUNC_BULK_WRITE, +}; + +#endif /* EXYNOS_ACPM_H_ */