xref: /linux/drivers/reset/reset-tps380x.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
18a4e6154SMarco Felsch // SPDX-License-Identifier: GPL-2.0-or-later
28a4e6154SMarco Felsch /*
38a4e6154SMarco Felsch  * TI TPS380x Supply Voltage Supervisor and Reset Controller Driver
48a4e6154SMarco Felsch  *
58a4e6154SMarco Felsch  * Copyright (C) 2022 Pengutronix, Marco Felsch <kernel@pengutronix.de>
68a4e6154SMarco Felsch  *
78a4e6154SMarco Felsch  * Based on Simple Reset Controller Driver
88a4e6154SMarco Felsch  *
98a4e6154SMarco Felsch  * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
108a4e6154SMarco Felsch  */
118a4e6154SMarco Felsch 
128a4e6154SMarco Felsch #include <linux/delay.h>
138a4e6154SMarco Felsch #include <linux/gpio/consumer.h>
148a4e6154SMarco Felsch #include <linux/module.h>
158a4e6154SMarco Felsch #include <linux/of.h>
168a4e6154SMarco Felsch #include <linux/platform_device.h>
178a4e6154SMarco Felsch #include <linux/property.h>
188a4e6154SMarco Felsch #include <linux/reset-controller.h>
198a4e6154SMarco Felsch 
208a4e6154SMarco Felsch struct tps380x_reset {
218a4e6154SMarco Felsch 	struct reset_controller_dev	rcdev;
228a4e6154SMarco Felsch 	struct gpio_desc		*reset_gpio;
238a4e6154SMarco Felsch 	unsigned int			reset_ms;
248a4e6154SMarco Felsch };
258a4e6154SMarco Felsch 
268a4e6154SMarco Felsch struct tps380x_reset_devdata {
278a4e6154SMarco Felsch 	unsigned int min_reset_ms;
288a4e6154SMarco Felsch 	unsigned int typ_reset_ms;
298a4e6154SMarco Felsch 	unsigned int max_reset_ms;
308a4e6154SMarco Felsch };
318a4e6154SMarco Felsch 
328a4e6154SMarco Felsch static inline
to_tps380x_reset(struct reset_controller_dev * rcdev)338a4e6154SMarco Felsch struct tps380x_reset *to_tps380x_reset(struct reset_controller_dev *rcdev)
348a4e6154SMarco Felsch {
358a4e6154SMarco Felsch 	return container_of(rcdev, struct tps380x_reset, rcdev);
368a4e6154SMarco Felsch }
378a4e6154SMarco Felsch 
388a4e6154SMarco Felsch static int
tps380x_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)398a4e6154SMarco Felsch tps380x_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
408a4e6154SMarco Felsch {
418a4e6154SMarco Felsch 	struct tps380x_reset *tps380x = to_tps380x_reset(rcdev);
428a4e6154SMarco Felsch 
438a4e6154SMarco Felsch 	gpiod_set_value_cansleep(tps380x->reset_gpio, 1);
448a4e6154SMarco Felsch 
458a4e6154SMarco Felsch 	return 0;
468a4e6154SMarco Felsch }
478a4e6154SMarco Felsch 
488a4e6154SMarco Felsch static int
tps380x_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)498a4e6154SMarco Felsch tps380x_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
508a4e6154SMarco Felsch {
518a4e6154SMarco Felsch 	struct tps380x_reset *tps380x = to_tps380x_reset(rcdev);
528a4e6154SMarco Felsch 
538a4e6154SMarco Felsch 	gpiod_set_value_cansleep(tps380x->reset_gpio, 0);
548a4e6154SMarco Felsch 	msleep(tps380x->reset_ms);
558a4e6154SMarco Felsch 
568a4e6154SMarco Felsch 	return 0;
578a4e6154SMarco Felsch }
588a4e6154SMarco Felsch 
598a4e6154SMarco Felsch static const struct reset_control_ops reset_tps380x_ops = {
608a4e6154SMarco Felsch 	.assert		= tps380x_reset_assert,
618a4e6154SMarco Felsch 	.deassert	= tps380x_reset_deassert,
628a4e6154SMarco Felsch };
638a4e6154SMarco Felsch 
tps380x_reset_of_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)648a4e6154SMarco Felsch static int tps380x_reset_of_xlate(struct reset_controller_dev *rcdev,
658a4e6154SMarco Felsch 				  const struct of_phandle_args *reset_spec)
668a4e6154SMarco Felsch {
678a4e6154SMarco Felsch 	/* No special handling needed, we have only one reset line per device */
688a4e6154SMarco Felsch 	return 0;
698a4e6154SMarco Felsch }
708a4e6154SMarco Felsch 
tps380x_reset_probe(struct platform_device * pdev)718a4e6154SMarco Felsch static int tps380x_reset_probe(struct platform_device *pdev)
728a4e6154SMarco Felsch {
738a4e6154SMarco Felsch 	struct device *dev = &pdev->dev;
748a4e6154SMarco Felsch 	const struct tps380x_reset_devdata *devdata;
758a4e6154SMarco Felsch 	struct tps380x_reset *tps380x;
768a4e6154SMarco Felsch 
778a4e6154SMarco Felsch 	devdata = device_get_match_data(dev);
788a4e6154SMarco Felsch 	if (!devdata)
798a4e6154SMarco Felsch 		return -EINVAL;
808a4e6154SMarco Felsch 
818a4e6154SMarco Felsch 	tps380x = devm_kzalloc(dev, sizeof(*tps380x), GFP_KERNEL);
828a4e6154SMarco Felsch 	if (!tps380x)
838a4e6154SMarco Felsch 		return -ENOMEM;
848a4e6154SMarco Felsch 
858a4e6154SMarco Felsch 	tps380x->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
868a4e6154SMarco Felsch 	if (IS_ERR(tps380x->reset_gpio))
878a4e6154SMarco Felsch 		return dev_err_probe(dev, PTR_ERR(tps380x->reset_gpio),
888a4e6154SMarco Felsch 				     "Failed to get GPIO\n");
898a4e6154SMarco Felsch 
908a4e6154SMarco Felsch 	tps380x->reset_ms = devdata->max_reset_ms;
918a4e6154SMarco Felsch 
928a4e6154SMarco Felsch 	tps380x->rcdev.ops = &reset_tps380x_ops;
938a4e6154SMarco Felsch 	tps380x->rcdev.owner = THIS_MODULE;
948a4e6154SMarco Felsch 	tps380x->rcdev.dev = dev;
958a4e6154SMarco Felsch 	tps380x->rcdev.of_node = dev->of_node;
968a4e6154SMarco Felsch 	tps380x->rcdev.of_reset_n_cells = 0;
978a4e6154SMarco Felsch 	tps380x->rcdev.of_xlate = tps380x_reset_of_xlate;
988a4e6154SMarco Felsch 	tps380x->rcdev.nr_resets = 1;
998a4e6154SMarco Felsch 
1008a4e6154SMarco Felsch 	return devm_reset_controller_register(dev, &tps380x->rcdev);
1018a4e6154SMarco Felsch }
1028a4e6154SMarco Felsch 
1038a4e6154SMarco Felsch static const struct tps380x_reset_devdata tps3801_reset_data = {
1048a4e6154SMarco Felsch 	.min_reset_ms = 120,
1058a4e6154SMarco Felsch 	.typ_reset_ms = 200,
1068a4e6154SMarco Felsch 	.max_reset_ms = 280,
1078a4e6154SMarco Felsch };
1088a4e6154SMarco Felsch 
1098a4e6154SMarco Felsch static const struct of_device_id tps380x_reset_dt_ids[] = {
1108a4e6154SMarco Felsch 	{ .compatible = "ti,tps3801", .data = &tps3801_reset_data },
1118a4e6154SMarco Felsch 	{ /* sentinel */ },
1128a4e6154SMarco Felsch };
1138a4e6154SMarco Felsch MODULE_DEVICE_TABLE(of, tps380x_reset_dt_ids);
1148a4e6154SMarco Felsch 
1158a4e6154SMarco Felsch static struct platform_driver tps380x_reset_driver = {
1168a4e6154SMarco Felsch 	.probe	= tps380x_reset_probe,
1178a4e6154SMarco Felsch 	.driver = {
1188a4e6154SMarco Felsch 		.name		= "tps380x-reset",
1198a4e6154SMarco Felsch 		.of_match_table	= tps380x_reset_dt_ids,
1208a4e6154SMarco Felsch 	},
1218a4e6154SMarco Felsch };
1228a4e6154SMarco Felsch module_platform_driver(tps380x_reset_driver);
1238a4e6154SMarco Felsch 
1248a4e6154SMarco Felsch MODULE_AUTHOR("Marco Felsch <kernel@pengutronix.de>");
125*d985db83SColin Ian King MODULE_DESCRIPTION("TI TPS380x Supply Voltage Supervisor and Reset Driver");
1268a4e6154SMarco Felsch MODULE_LICENSE("GPL v2");
127