^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Rockchip AMP support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Tony Xie <tony.xie@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pm_domain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/rockchip/rockchip_sip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define RK_CPU_STATUS_OFF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define RK_CPU_STATUS_ON 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define RK_CPU_STATUS_BUSY -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) enum amp_cpu_ctrl_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) AMP_CPU_STATUS_AMP_DIS = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) AMP_CPU_STATUS_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) AMP_CPU_STATUS_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) AMP_CPU_STATUS_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define AMP_FLAG_CPU_ARM64 BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define AMP_FLAG_CPU_EL2_HYP BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define AMP_FLAG_CPU_ARM32_T BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct rkamp_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct clk_bulk_data *clks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int num_clks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct device **pd_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int num_pds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u32 en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u32 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u64 entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u64 cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) } cpu_boot_info[CONFIG_NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int get_cpu_boot_info_idx(unsigned long cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < CONFIG_NR_CPUS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (cpu_boot_info[i].cpu_id == cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static ssize_t boot_cpu_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) str += sprintf(str, "cpu on/off:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) str += sprintf(str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) " echo on/off [cpu id] > /sys/rk_amp/boot_cpu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) str += sprintf(str, "get cpu on/off status:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) str += sprintf(str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) " echo status [cpu id] > /sys/rk_amp/boot_cpu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (str != buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *(str - 1) = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return (str - buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void cpu_status_print(unsigned long cpu_id, struct arm_smccc_res *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (res->a0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) pr_info("get cpu-0x%lx status(%lx) error!\n", cpu_id, res->a0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (res->a1 == AMP_CPU_STATUS_AMP_DIS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pr_info("cpu-0x%lx amp is disable (%ld)\n", cpu_id, res->a1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) else if (res->a1 == AMP_CPU_STATUS_EN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pr_info("cpu-0x%lx amp is enable (%ld)\n", cpu_id, res->a1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) else if (res->a1 == AMP_CPU_STATUS_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_info("cpu-0x%lx amp: cpu is on (%ld)\n", cpu_id, res->a1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) else if (res->a1 == AMP_CPU_STATUS_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pr_info("cpu-0x%lx amp: cpu is off(%ld)\n", cpu_id, res->a1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pr_info("cpu-0x%lx status(%ld) is error\n", cpu_id, res->a1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (res->a2 == RK_CPU_STATUS_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pr_info("cpu-0x%lx status(%ld) is off\n", cpu_id, res->a2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) else if (res->a2 == RK_CPU_STATUS_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pr_info("cpu-0x%lx status(%ld) is on\n", cpu_id, res->a2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) else if (res->a2 == RK_CPU_STATUS_BUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pr_info("cpu-0x%lx status(%ld) is busy\n", cpu_id, res->a2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pr_info("cpu-0x%lx status(%ld) is error\n", cpu_id, res->a2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static ssize_t boot_cpu_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) char cmd[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct arm_smccc_res res = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int ret, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ret = sscanf(buf, "%s", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pr_info("Use on/off [cpu id] or status [cpu id]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!strncmp(cmd, "status", strlen("status"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ret = sscanf(buf, "%s %lx", cmd, &cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ret != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) res = sip_smc_get_amp_info(RK_AMP_SUB_FUNC_GET_CPU_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) cpu_status_print(cpu_id, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else if (!strncmp(cmd, "off", strlen("off"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ret = sscanf(buf, "%s %lx", cmd, &cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (ret != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) idx = get_cpu_boot_info_idx(cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (idx >= 0 && cpu_boot_info[idx].en) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret = sip_smc_amp_config(RK_AMP_SUB_FUNC_REQ_CPU_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) cpu_id, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pr_info("requesting a cpu off is error(%d)!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) } else if (!strncmp(cmd, "on", strlen("on"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ret = sscanf(buf, "%s %lx", cmd, &cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (ret != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) idx = get_cpu_boot_info_idx(cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (idx >= 0 && cpu_boot_info[idx].en) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ret = sip_smc_amp_config(RK_AMP_SUB_FUNC_CPU_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) cpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) cpu_boot_info[idx].entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) pr_info("booting up a cpu is error(%d)!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pr_info("unsupported cmd(%s)\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static struct kobject *rk_amp_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static struct device_attribute rk_amp_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) __ATTR(boot_cpu, 0664, boot_cpu_show, boot_cpu_store),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int rockchip_amp_boot_cpus(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct device_node *cpu_node, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) u64 cpu_entry, cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) u32 cpu_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (idx >= CONFIG_NR_CPUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (of_property_read_u64_array(cpu_node, "entry", &cpu_entry, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_warn(dev, "can not get the entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!cpu_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) dev_warn(dev, "cpu-entry is 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (of_property_read_u64_array(cpu_node, "id", &cpu_id, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dev_warn(dev, "can not get the cpu id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (of_property_read_u32_array(cpu_node, "mode", &cpu_mode, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) dev_warn(dev, "can not get the cpu mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) cpu_boot_info[idx].entry = cpu_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) cpu_boot_info[idx].mode = cpu_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) cpu_boot_info[idx].cpu_id = cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ret = sip_smc_amp_config(RK_AMP_SUB_FUNC_CFG_MODE, cpu_id, cpu_mode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev_warn(dev, "setting cpu mode is error(%d)!\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = sip_smc_amp_config(RK_AMP_SUB_FUNC_CPU_ON, cpu_id, cpu_entry, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) dev_warn(dev, "booting up a cpu is error(%d)!\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) cpu_boot_info[idx].en = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int rockchip_amp_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct rkamp_device *rkamp_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int ret, i, idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct device_node *cpus_node, *cpu_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) rkamp_dev = devm_kzalloc(&pdev->dev, sizeof(*rkamp_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!rkamp_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) rkamp_dev->num_clks = devm_clk_bulk_get_all(&pdev->dev, &rkamp_dev->clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (rkamp_dev->num_clks < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ret = clk_bulk_prepare_enable(rkamp_dev->num_clks, rkamp_dev->clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return dev_err_probe(&pdev->dev, ret, "failed to prepare enable clks: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) pm_runtime_enable(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) rkamp_dev->num_pds = of_count_phandle_with_args(pdev->dev.of_node, "power-domains",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) "#power-domain-cells");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (rkamp_dev->num_pds > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) rkamp_dev->pd_dev = devm_kmalloc_array(&pdev->dev, rkamp_dev->num_pds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) sizeof(*rkamp_dev->pd_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (!rkamp_dev->pd_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (rkamp_dev->num_pds == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = pm_runtime_resume_and_get(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return dev_err_probe(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) "failed to get power-domain\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) for (i = 0; i < rkamp_dev->num_pds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) rkamp_dev->pd_dev[i] = dev_pm_domain_attach_by_id(&pdev->dev, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ret = pm_runtime_resume_and_get(rkamp_dev->pd_dev[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return dev_err_probe(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) "failed to get pd_dev[%d]\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) cpus_node = of_get_child_by_name(pdev->dev.of_node, "amp-cpus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (cpus_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for_each_available_child_of_node(cpus_node, cpu_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!rockchip_amp_boot_cpus(&pdev->dev, cpu_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) rk_amp_kobj = kobject_create_and_add("rk_amp", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!rk_amp_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) for (i = 0; i < ARRAY_SIZE(rk_amp_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = sysfs_create_file(rk_amp_kobj, &rk_amp_attrs[i].attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return dev_err_probe(&pdev->dev, ret, "create file index %d error\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int rockchip_amp_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct rkamp_device *rkamp_dev = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) clk_bulk_disable_unprepare(rkamp_dev->num_clks, rkamp_dev->clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (rkamp_dev->num_pds == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) pm_runtime_put_sync(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) } else if (rkamp_dev->num_pds > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) for (i = 0; i < rkamp_dev->num_pds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) pm_runtime_put_sync(rkamp_dev->pd_dev[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) dev_pm_domain_detach(rkamp_dev->pd_dev[i], true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) rkamp_dev->pd_dev[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) pm_runtime_disable(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) for (i = 0; i < ARRAY_SIZE(rk_amp_attrs); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) sysfs_remove_file(rk_amp_kobj, &rk_amp_attrs[i].attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) kobject_put(rk_amp_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static const struct of_device_id rockchip_amp_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .compatible = "rockchip,amp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) { /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) MODULE_DEVICE_TABLE(of, rockchip_amp_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static struct platform_driver rockchip_amp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .probe = rockchip_amp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .remove = rockchip_amp_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) .name = "rockchip-amp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .of_match_table = rockchip_amp_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) module_platform_driver(rockchip_amp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) MODULE_DESCRIPTION("Rockchip AMP driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_AUTHOR("Tony xie<tony.xie@rock-chips.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) MODULE_LICENSE("GPL");