1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2014 STMicroelectronics 4 * 5 * Power off Restart driver, used in STMicroelectronics devices. 6 * 7 * Author: Christophe Kerello <christophe.kerello@st.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/of.h> 12 #include <linux/platform_device.h> 13 #include <linux/mfd/syscon.h> 14 #include <linux/reboot.h> 15 #include <linux/regmap.h> 16 17 struct reset_syscfg { 18 struct regmap *regmap; 19 /* syscfg used for reset */ 20 unsigned int offset_rst; 21 unsigned int mask_rst; 22 /* syscfg used for unmask the reset */ 23 unsigned int offset_rst_msk; 24 unsigned int mask_rst_msk; 25 }; 26 27 /* STiH407 */ 28 #define STIH407_SYSCFG_4000 0x0 29 #define STIH407_SYSCFG_4008 0x20 30 31 static struct reset_syscfg stih407_reset = { 32 .offset_rst = STIH407_SYSCFG_4000, 33 .mask_rst = BIT(0), 34 .offset_rst_msk = STIH407_SYSCFG_4008, 35 .mask_rst_msk = BIT(0) 36 }; 37 38 39 static struct reset_syscfg *st_restart_syscfg; 40 41 static int st_restart(struct notifier_block *this, unsigned long mode, 42 void *cmd) 43 { 44 /* reset syscfg updated */ 45 regmap_update_bits(st_restart_syscfg->regmap, 46 st_restart_syscfg->offset_rst, 47 st_restart_syscfg->mask_rst, 48 0); 49 50 /* unmask the reset */ 51 regmap_update_bits(st_restart_syscfg->regmap, 52 st_restart_syscfg->offset_rst_msk, 53 st_restart_syscfg->mask_rst_msk, 54 0); 55 56 return NOTIFY_DONE; 57 } 58 59 static struct notifier_block st_restart_nb = { 60 .notifier_call = st_restart, 61 .priority = 192, 62 }; 63 64 static const struct of_device_id st_reset_of_match[] = { 65 { 66 .compatible = "st,stih407-restart", 67 .data = (void *)&stih407_reset, 68 }, 69 {} 70 }; 71 72 static int st_reset_probe(struct platform_device *pdev) 73 { 74 struct device_node *np = pdev->dev.of_node; 75 struct device *dev = &pdev->dev; 76 77 st_restart_syscfg = (struct reset_syscfg *)of_device_get_match_data(dev); 78 if (!st_restart_syscfg) 79 return -ENODEV; 80 81 st_restart_syscfg->regmap = 82 syscon_regmap_lookup_by_phandle(np, "st,syscfg"); 83 if (IS_ERR(st_restart_syscfg->regmap)) { 84 dev_err(dev, "No syscfg phandle specified\n"); 85 return PTR_ERR(st_restart_syscfg->regmap); 86 } 87 88 return register_restart_handler(&st_restart_nb); 89 } 90 91 static struct platform_driver st_reset_driver = { 92 .probe = st_reset_probe, 93 .driver = { 94 .name = "st_reset", 95 .of_match_table = st_reset_of_match, 96 }, 97 }; 98 99 builtin_platform_driver(st_reset_driver); 100 101 MODULE_AUTHOR("Christophe Kerello <christophe.kerello@st.com>"); 102 MODULE_DESCRIPTION("STMicroelectronics Power off Restart driver"); 103 MODULE_LICENSE("GPL v2"); 104