18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Pistachio SoC Reset Controller driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Imagination Technologies Ltd.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Damien Horsley <Damien.Horsley@imgtec.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/regmap.h>
148c2ecf20Sopenharmony_ci#include <linux/reset-controller.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <dt-bindings/reset/pistachio-resets.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define	PISTACHIO_SOFT_RESET		0
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct pistachio_reset_data {
238c2ecf20Sopenharmony_ci	struct reset_controller_dev	rcdev;
248c2ecf20Sopenharmony_ci	struct regmap			*periph_regs;
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic inline int pistachio_reset_shift(unsigned long id)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	switch (id) {
308c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_I2C0:
318c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_I2C1:
328c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_I2C2:
338c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_I2C3:
348c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_I2S_IN:
358c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_PRL_OUT:
368c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_SPDIF_OUT:
378c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_SPI:
388c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_PWM_PDM:
398c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_UART0:
408c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_UART1:
418c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_QSPI:
428c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_MDC:
438c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_SDHOST:
448c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_ETHERNET:
458c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_IR:
468c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_HASH:
478c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_TIMER:
488c2ecf20Sopenharmony_ci		return id;
498c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_I2S_OUT:
508c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_SPDIF_IN:
518c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_EVT:
528c2ecf20Sopenharmony_ci		return id + 6;
538c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_USB_H:
548c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_USB_PR:
558c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_USB_PHY_PR:
568c2ecf20Sopenharmony_ci	case PISTACHIO_RESET_USB_PHY_PON:
578c2ecf20Sopenharmony_ci		return id + 7;
588c2ecf20Sopenharmony_ci	default:
598c2ecf20Sopenharmony_ci		return -EINVAL;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int pistachio_reset_assert(struct reset_controller_dev *rcdev,
648c2ecf20Sopenharmony_ci				  unsigned long id)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct pistachio_reset_data *rd;
678c2ecf20Sopenharmony_ci	u32 mask;
688c2ecf20Sopenharmony_ci	int shift;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	rd = container_of(rcdev, struct pistachio_reset_data, rcdev);
718c2ecf20Sopenharmony_ci	shift = pistachio_reset_shift(id);
728c2ecf20Sopenharmony_ci	if (shift < 0)
738c2ecf20Sopenharmony_ci		return shift;
748c2ecf20Sopenharmony_ci	mask = BIT(shift);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return regmap_update_bits(rd->periph_regs, PISTACHIO_SOFT_RESET,
778c2ecf20Sopenharmony_ci				  mask, mask);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int pistachio_reset_deassert(struct reset_controller_dev *rcdev,
818c2ecf20Sopenharmony_ci				    unsigned long id)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct pistachio_reset_data *rd;
848c2ecf20Sopenharmony_ci	u32 mask;
858c2ecf20Sopenharmony_ci	int shift;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	rd = container_of(rcdev, struct pistachio_reset_data, rcdev);
888c2ecf20Sopenharmony_ci	shift = pistachio_reset_shift(id);
898c2ecf20Sopenharmony_ci	if (shift < 0)
908c2ecf20Sopenharmony_ci		return shift;
918c2ecf20Sopenharmony_ci	mask = BIT(shift);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return regmap_update_bits(rd->periph_regs, PISTACHIO_SOFT_RESET,
948c2ecf20Sopenharmony_ci				  mask, 0);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic const struct reset_control_ops pistachio_reset_ops = {
988c2ecf20Sopenharmony_ci	.assert		= pistachio_reset_assert,
998c2ecf20Sopenharmony_ci	.deassert	= pistachio_reset_deassert,
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int pistachio_reset_probe(struct platform_device *pdev)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct pistachio_reset_data *rd;
1058c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1068c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	rd = devm_kzalloc(dev, sizeof(*rd), GFP_KERNEL);
1098c2ecf20Sopenharmony_ci	if (!rd)
1108c2ecf20Sopenharmony_ci		return -ENOMEM;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	rd->periph_regs = syscon_node_to_regmap(np->parent);
1138c2ecf20Sopenharmony_ci	if (IS_ERR(rd->periph_regs))
1148c2ecf20Sopenharmony_ci		return PTR_ERR(rd->periph_regs);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	rd->rcdev.owner = THIS_MODULE;
1178c2ecf20Sopenharmony_ci	rd->rcdev.nr_resets = PISTACHIO_RESET_MAX + 1;
1188c2ecf20Sopenharmony_ci	rd->rcdev.ops = &pistachio_reset_ops;
1198c2ecf20Sopenharmony_ci	rd->rcdev.of_node = np;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return devm_reset_controller_register(dev, &rd->rcdev);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic const struct of_device_id pistachio_reset_dt_ids[] = {
1258c2ecf20Sopenharmony_ci	 { .compatible = "img,pistachio-reset", },
1268c2ecf20Sopenharmony_ci	 { /* sentinel */ },
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic struct platform_driver pistachio_reset_driver = {
1308c2ecf20Sopenharmony_ci	.probe	= pistachio_reset_probe,
1318c2ecf20Sopenharmony_ci	.driver = {
1328c2ecf20Sopenharmony_ci		.name		= "pistachio-reset",
1338c2ecf20Sopenharmony_ci		.of_match_table	= pistachio_reset_dt_ids,
1348c2ecf20Sopenharmony_ci	},
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_cibuiltin_platform_driver(pistachio_reset_driver);
137