1 /*
2  * Copyright (C) 2014 STMicroelectronics
3  *
4  * Power off Restart driver, used in STMicroelectronics devices.
5  *
6  * Author: Christophe Kerello <christophe.kerello@st.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2, as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/reboot.h>
19 #include <linux/regmap.h>
20 
21 struct reset_syscfg {
22 	struct regmap *regmap;
23 	/* syscfg used for reset */
24 	unsigned int offset_rst;
25 	unsigned int mask_rst;
26 	/* syscfg used for unmask the reset */
27 	unsigned int offset_rst_msk;
28 	unsigned int mask_rst_msk;
29 };
30 
31 /* STiH415 */
32 #define STIH415_SYSCFG_11	0x2c
33 #define STIH415_SYSCFG_15	0x3c
34 
35 static struct reset_syscfg stih415_reset = {
36 	.offset_rst = STIH415_SYSCFG_11,
37 	.mask_rst = BIT(0),
38 	.offset_rst_msk = STIH415_SYSCFG_15,
39 	.mask_rst_msk = BIT(0)
40 };
41 
42 /* STiH416 */
43 #define STIH416_SYSCFG_500	0x7d0
44 #define STIH416_SYSCFG_504	0x7e0
45 
46 static struct reset_syscfg stih416_reset = {
47 	.offset_rst = STIH416_SYSCFG_500,
48 	.mask_rst = BIT(0),
49 	.offset_rst_msk = STIH416_SYSCFG_504,
50 	.mask_rst_msk = BIT(0)
51 };
52 
53 /* STiH407 */
54 #define STIH407_SYSCFG_4000	0x0
55 #define STIH407_SYSCFG_4008	0x20
56 
57 static struct reset_syscfg stih407_reset = {
58 	.offset_rst = STIH407_SYSCFG_4000,
59 	.mask_rst = BIT(0),
60 	.offset_rst_msk = STIH407_SYSCFG_4008,
61 	.mask_rst_msk = BIT(0)
62 };
63 
64 /* STiD127 */
65 #define STID127_SYSCFG_700	0x0
66 #define STID127_SYSCFG_773	0x124
67 
68 static struct reset_syscfg stid127_reset = {
69 	.offset_rst = STID127_SYSCFG_773,
70 	.mask_rst = BIT(0),
71 	.offset_rst_msk = STID127_SYSCFG_700,
72 	.mask_rst_msk = BIT(8)
73 };
74 
75 static struct reset_syscfg *st_restart_syscfg;
76 
77 static int st_restart(struct notifier_block *this, unsigned long mode,
78 		      void *cmd)
79 {
80 	/* reset syscfg updated */
81 	regmap_update_bits(st_restart_syscfg->regmap,
82 			   st_restart_syscfg->offset_rst,
83 			   st_restart_syscfg->mask_rst,
84 			   0);
85 
86 	/* unmask the reset */
87 	regmap_update_bits(st_restart_syscfg->regmap,
88 			   st_restart_syscfg->offset_rst_msk,
89 			   st_restart_syscfg->mask_rst_msk,
90 			   0);
91 
92 	return NOTIFY_DONE;
93 }
94 
95 static struct notifier_block st_restart_nb = {
96 	.notifier_call = st_restart,
97 	.priority = 192,
98 };
99 
100 static const struct of_device_id st_reset_of_match[] = {
101 	{
102 		.compatible = "st,stih415-restart",
103 		.data = (void *)&stih415_reset,
104 	}, {
105 		.compatible = "st,stih416-restart",
106 		.data = (void *)&stih416_reset,
107 	}, {
108 		.compatible = "st,stih407-restart",
109 		.data = (void *)&stih407_reset,
110 	}, {
111 		.compatible = "st,stid127-restart",
112 		.data = (void *)&stid127_reset,
113 	},
114 	{}
115 };
116 
117 static int st_reset_probe(struct platform_device *pdev)
118 {
119 	struct device_node *np = pdev->dev.of_node;
120 	const struct of_device_id *match;
121 	struct device *dev = &pdev->dev;
122 
123 	match = of_match_device(st_reset_of_match, dev);
124 	if (!match)
125 		return -ENODEV;
126 
127 	st_restart_syscfg = (struct reset_syscfg *)match->data;
128 
129 	st_restart_syscfg->regmap =
130 		syscon_regmap_lookup_by_phandle(np, "st,syscfg");
131 	if (IS_ERR(st_restart_syscfg->regmap)) {
132 		dev_err(dev, "No syscfg phandle specified\n");
133 		return PTR_ERR(st_restart_syscfg->regmap);
134 	}
135 
136 	return register_restart_handler(&st_restart_nb);
137 }
138 
139 static struct platform_driver st_reset_driver = {
140 	.probe = st_reset_probe,
141 	.driver = {
142 		.name = "st_reset",
143 		.of_match_table = st_reset_of_match,
144 	},
145 };
146 
147 static int __init st_reset_init(void)
148 {
149 	return platform_driver_register(&st_reset_driver);
150 }
151 
152 device_initcall(st_reset_init);
153 
154 MODULE_AUTHOR("Christophe Kerello <christophe.kerello@st.com>");
155 MODULE_DESCRIPTION("STMicroelectronics Power off Restart driver");
156 MODULE_LICENSE("GPL v2");
157