xref: /linux/drivers/regulator/raa215300.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas RAA215300 PMIC driver
4 //
5 // Copyright (C) 2023 Renesas Electronics Corporation
6 //
7 
8 #include <linux/clk.h>
9 #include <linux/clkdev.h>
10 #include <linux/clk-provider.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/regmap.h>
16 
17 #define RAA215300_FAULT_LATCHED_STATUS_1	0x59
18 #define RAA215300_FAULT_LATCHED_STATUS_2	0x5a
19 #define RAA215300_FAULT_LATCHED_STATUS_3	0x5b
20 #define RAA215300_FAULT_LATCHED_STATUS_4	0x5c
21 #define RAA215300_FAULT_LATCHED_STATUS_6	0x5e
22 
23 #define RAA215300_INT_MASK_1	0x64
24 #define RAA215300_INT_MASK_2	0x65
25 #define RAA215300_INT_MASK_3	0x66
26 #define RAA215300_INT_MASK_4	0x67
27 #define RAA215300_INT_MASK_6	0x68
28 
29 #define RAA215300_REG_BLOCK_EN	0x6c
30 #define RAA215300_HW_REV	0xf8
31 
32 #define RAA215300_INT_MASK_1_ALL	GENMASK(5, 0)
33 #define RAA215300_INT_MASK_2_ALL	GENMASK(3, 0)
34 #define RAA215300_INT_MASK_3_ALL	GENMASK(5, 0)
35 #define RAA215300_INT_MASK_4_ALL	BIT(0)
36 #define RAA215300_INT_MASK_6_ALL	GENMASK(7, 0)
37 
38 #define RAA215300_REG_BLOCK_EN_RTC_EN	BIT(6)
39 #define RAA215300_RTC_DEFAULT_ADDR	0x6f
40 
41 static const struct regmap_config raa215300_regmap_config = {
42 	.reg_bits = 8,
43 	.val_bits = 8,
44 	.max_register = 0xff,
45 };
46 
47 static void raa215300_rtc_unregister_device(void *data)
48 {
49 	i2c_unregister_device(data);
50 }
51 
52 static int raa215300_clk_present(struct i2c_client *client, const char *name)
53 {
54 	struct clk *clk;
55 
56 	clk = devm_clk_get_optional(&client->dev, name);
57 	if (IS_ERR(clk))
58 		return PTR_ERR(clk);
59 
60 	return !!clk;
61 }
62 
63 static int raa215300_i2c_probe(struct i2c_client *client)
64 {
65 	struct device *dev = &client->dev;
66 	const char *clkin_name = "clkin";
67 	unsigned int pmic_version, val;
68 	const char *xin_name = "xin";
69 	const char *clk_name = NULL;
70 	struct regmap *regmap;
71 	int ret;
72 
73 	regmap = devm_regmap_init_i2c(client, &raa215300_regmap_config);
74 	if (IS_ERR(regmap))
75 		return dev_err_probe(dev, PTR_ERR(regmap),
76 				     "regmap i2c init failed\n");
77 
78 	ret = regmap_read(regmap, RAA215300_HW_REV, &pmic_version);
79 	if (ret < 0)
80 		return dev_err_probe(dev, ret, "HW rev read failed\n");
81 
82 	dev_dbg(dev, "RAA215300 PMIC version 0x%04x\n", pmic_version);
83 
84 	/* Clear all blocks except RTC, if enabled */
85 	regmap_read(regmap, RAA215300_REG_BLOCK_EN, &val);
86 	val &= RAA215300_REG_BLOCK_EN_RTC_EN;
87 	regmap_write(regmap, RAA215300_REG_BLOCK_EN, val);
88 
89 	/* Clear the latched registers */
90 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_1, &val);
91 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_1, val);
92 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_2, &val);
93 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_2, val);
94 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_3, &val);
95 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_3, val);
96 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_4, &val);
97 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_4, val);
98 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_6, &val);
99 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_6, val);
100 
101 	/* Mask all the PMIC interrupts */
102 	regmap_write(regmap, RAA215300_INT_MASK_1, RAA215300_INT_MASK_1_ALL);
103 	regmap_write(regmap, RAA215300_INT_MASK_2, RAA215300_INT_MASK_2_ALL);
104 	regmap_write(regmap, RAA215300_INT_MASK_3, RAA215300_INT_MASK_3_ALL);
105 	regmap_write(regmap, RAA215300_INT_MASK_4, RAA215300_INT_MASK_4_ALL);
106 	regmap_write(regmap, RAA215300_INT_MASK_6, RAA215300_INT_MASK_6_ALL);
107 
108 	ret = raa215300_clk_present(client, xin_name);
109 	if (ret < 0) {
110 		return ret;
111 	} else if (ret) {
112 		clk_name = xin_name;
113 	} else {
114 		ret = raa215300_clk_present(client, clkin_name);
115 		if (ret < 0)
116 			return ret;
117 		if (ret)
118 			clk_name = clkin_name;
119 	}
120 
121 	if (clk_name) {
122 		const char *name = pmic_version >= 0x12 ? "isl1208" : "raa215300_a0";
123 		struct device_node *np = client->dev.of_node;
124 		u32 addr = RAA215300_RTC_DEFAULT_ADDR;
125 		struct i2c_board_info info = {};
126 		struct i2c_client *rtc_client;
127 		struct clk_hw *hw;
128 		ssize_t size;
129 
130 		hw = devm_clk_hw_register_fixed_rate(dev, clk_name, NULL, 0, 32768);
131 		if (IS_ERR(hw))
132 			return PTR_ERR(hw);
133 
134 		ret = devm_clk_hw_register_clkdev(dev, hw, clk_name, NULL);
135 		if (ret)
136 			return dev_err_probe(dev, ret, "Failed to initialize clkdev\n");
137 
138 		if (np) {
139 			int i;
140 
141 			i = of_property_match_string(np, "reg-names", "rtc");
142 			if (i >= 0)
143 				of_property_read_u32_index(np, "reg", i, &addr);
144 		}
145 
146 		info.addr = addr;
147 		if (client->irq > 0)
148 			info.irq = client->irq;
149 
150 		size = strscpy(info.type, name, sizeof(info.type));
151 		if (size < 0)
152 			return dev_err_probe(dev, size,
153 					     "Invalid device name: %s\n", name);
154 
155 		/* Enable RTC block */
156 		regmap_update_bits(regmap, RAA215300_REG_BLOCK_EN,
157 				   RAA215300_REG_BLOCK_EN_RTC_EN,
158 				   RAA215300_REG_BLOCK_EN_RTC_EN);
159 
160 		rtc_client = i2c_new_client_device(client->adapter, &info);
161 		if (IS_ERR(rtc_client))
162 			return PTR_ERR(rtc_client);
163 
164 		ret = devm_add_action_or_reset(dev,
165 					       raa215300_rtc_unregister_device,
166 					       rtc_client);
167 		if (ret < 0)
168 			return ret;
169 	}
170 
171 	return 0;
172 }
173 
174 static const struct of_device_id raa215300_dt_match[] = {
175 	{ .compatible = "renesas,raa215300" },
176 	{ /* sentinel */ }
177 };
178 MODULE_DEVICE_TABLE(of, raa215300_dt_match);
179 
180 static struct i2c_driver raa215300_i2c_driver = {
181 	.driver = {
182 		.name = "raa215300",
183 		.of_match_table = raa215300_dt_match,
184 	},
185 	.probe = raa215300_i2c_probe,
186 };
187 module_i2c_driver(raa215300_i2c_driver);
188 
189 MODULE_DESCRIPTION("Renesas RAA215300 PMIC driver");
190 MODULE_AUTHOR("Fabrizio Castro <fabrizio.castro.jz@renesas.com>");
191 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
192 MODULE_LICENSE("GPL");
193