18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014 MediaTek Inc. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 78c2ecf20Sopenharmony_ci#include <linux/module.h> 88c2ecf20Sopenharmony_ci#include <linux/of.h> 98c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 108c2ecf20Sopenharmony_ci#include <linux/regmap.h> 118c2ecf20Sopenharmony_ci#include <linux/reset-controller.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "clk-mtk.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_cistruct mtk_reset { 178c2ecf20Sopenharmony_ci struct regmap *regmap; 188c2ecf20Sopenharmony_ci int regofs; 198c2ecf20Sopenharmony_ci struct reset_controller_dev rcdev; 208c2ecf20Sopenharmony_ci}; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic int mtk_reset_assert_set_clr(struct reset_controller_dev *rcdev, 238c2ecf20Sopenharmony_ci unsigned long id) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci struct mtk_reset *data = container_of(rcdev, struct mtk_reset, rcdev); 268c2ecf20Sopenharmony_ci unsigned int reg = data->regofs + ((id / 32) << 4); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci return regmap_write(data->regmap, reg, BIT(id % 32)); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic int mtk_reset_deassert_set_clr(struct reset_controller_dev *rcdev, 328c2ecf20Sopenharmony_ci unsigned long id) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct mtk_reset *data = container_of(rcdev, struct mtk_reset, rcdev); 358c2ecf20Sopenharmony_ci unsigned int reg = data->regofs + ((id / 32) << 4) + 0x4; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci return regmap_write(data->regmap, reg, BIT(id % 32)); 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int mtk_reset_assert(struct reset_controller_dev *rcdev, 418c2ecf20Sopenharmony_ci unsigned long id) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci struct mtk_reset *data = container_of(rcdev, struct mtk_reset, rcdev); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, data->regofs + ((id / 32) << 2), 468c2ecf20Sopenharmony_ci BIT(id % 32), ~0); 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic int mtk_reset_deassert(struct reset_controller_dev *rcdev, 508c2ecf20Sopenharmony_ci unsigned long id) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct mtk_reset *data = container_of(rcdev, struct mtk_reset, rcdev); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, data->regofs + ((id / 32) << 2), 558c2ecf20Sopenharmony_ci BIT(id % 32), 0); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic int mtk_reset(struct reset_controller_dev *rcdev, 598c2ecf20Sopenharmony_ci unsigned long id) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci int ret; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci ret = mtk_reset_assert(rcdev, id); 648c2ecf20Sopenharmony_ci if (ret) 658c2ecf20Sopenharmony_ci return ret; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci return mtk_reset_deassert(rcdev, id); 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic int mtk_reset_set_clr(struct reset_controller_dev *rcdev, 718c2ecf20Sopenharmony_ci unsigned long id) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci int ret; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci ret = mtk_reset_assert_set_clr(rcdev, id); 768c2ecf20Sopenharmony_ci if (ret) 778c2ecf20Sopenharmony_ci return ret; 788c2ecf20Sopenharmony_ci return mtk_reset_deassert_set_clr(rcdev, id); 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic const struct reset_control_ops mtk_reset_ops = { 828c2ecf20Sopenharmony_ci .assert = mtk_reset_assert, 838c2ecf20Sopenharmony_ci .deassert = mtk_reset_deassert, 848c2ecf20Sopenharmony_ci .reset = mtk_reset, 858c2ecf20Sopenharmony_ci}; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic const struct reset_control_ops mtk_reset_ops_set_clr = { 888c2ecf20Sopenharmony_ci .assert = mtk_reset_assert_set_clr, 898c2ecf20Sopenharmony_ci .deassert = mtk_reset_deassert_set_clr, 908c2ecf20Sopenharmony_ci .reset = mtk_reset_set_clr, 918c2ecf20Sopenharmony_ci}; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic void mtk_register_reset_controller_common(struct device_node *np, 948c2ecf20Sopenharmony_ci unsigned int num_regs, int regofs, 958c2ecf20Sopenharmony_ci const struct reset_control_ops *reset_ops) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci struct mtk_reset *data; 988c2ecf20Sopenharmony_ci int ret; 998c2ecf20Sopenharmony_ci struct regmap *regmap; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci regmap = syscon_node_to_regmap(np); 1028c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) { 1038c2ecf20Sopenharmony_ci pr_err("Cannot find regmap for %pOF: %ld\n", np, 1048c2ecf20Sopenharmony_ci PTR_ERR(regmap)); 1058c2ecf20Sopenharmony_ci return; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci data = kzalloc(sizeof(*data), GFP_KERNEL); 1098c2ecf20Sopenharmony_ci if (!data) 1108c2ecf20Sopenharmony_ci return; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci data->regmap = regmap; 1138c2ecf20Sopenharmony_ci data->regofs = regofs; 1148c2ecf20Sopenharmony_ci data->rcdev.owner = THIS_MODULE; 1158c2ecf20Sopenharmony_ci data->rcdev.nr_resets = num_regs * 32; 1168c2ecf20Sopenharmony_ci data->rcdev.ops = reset_ops; 1178c2ecf20Sopenharmony_ci data->rcdev.of_node = np; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci ret = reset_controller_register(&data->rcdev); 1208c2ecf20Sopenharmony_ci if (ret) { 1218c2ecf20Sopenharmony_ci pr_err("could not register reset controller: %d\n", ret); 1228c2ecf20Sopenharmony_ci kfree(data); 1238c2ecf20Sopenharmony_ci return; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_civoid mtk_register_reset_controller(struct device_node *np, 1288c2ecf20Sopenharmony_ci unsigned int num_regs, int regofs) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci mtk_register_reset_controller_common(np, num_regs, regofs, 1318c2ecf20Sopenharmony_ci &mtk_reset_ops); 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_civoid mtk_register_reset_controller_set_clr(struct device_node *np, 1358c2ecf20Sopenharmony_ci unsigned int num_regs, int regofs) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci mtk_register_reset_controller_common(np, num_regs, regofs, 1388c2ecf20Sopenharmony_ci &mtk_reset_ops_set_clr); 1398c2ecf20Sopenharmony_ci} 140