xref: /openbmc/linux/drivers/regulator/raa215300.c (revision c4c3c32d)
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 const char *clkin_name = "clkin";
42 const char *xin_name = "xin";
43 static struct clk *clk;
44 
45 static const struct regmap_config raa215300_regmap_config = {
46 	.reg_bits = 8,
47 	.val_bits = 8,
48 	.max_register = 0xff,
49 };
50 
51 static void raa215300_rtc_unregister_device(void *data)
52 {
53 	i2c_unregister_device(data);
54 	if (!clk) {
55 		clk_unregister_fixed_rate(clk);
56 		clk = NULL;
57 	}
58 }
59 
60 static int raa215300_clk_present(struct i2c_client *client, const char *name)
61 {
62 	struct clk *clk;
63 
64 	clk = devm_clk_get_optional(&client->dev, name);
65 	if (IS_ERR(clk))
66 		return PTR_ERR(clk);
67 
68 	return !!clk;
69 }
70 
71 static int raa215300_i2c_probe(struct i2c_client *client)
72 {
73 	struct device *dev = &client->dev;
74 	const char *clk_name = xin_name;
75 	unsigned int pmic_version, val;
76 	struct regmap *regmap;
77 	int ret;
78 
79 	regmap = devm_regmap_init_i2c(client, &raa215300_regmap_config);
80 	if (IS_ERR(regmap))
81 		return dev_err_probe(dev, PTR_ERR(regmap),
82 				     "regmap i2c init failed\n");
83 
84 	ret = regmap_read(regmap, RAA215300_HW_REV, &pmic_version);
85 	if (ret < 0)
86 		return dev_err_probe(dev, ret, "HW rev read failed\n");
87 
88 	dev_dbg(dev, "RAA215300 PMIC version 0x%04x\n", pmic_version);
89 
90 	/* Clear all blocks except RTC, if enabled */
91 	regmap_read(regmap, RAA215300_REG_BLOCK_EN, &val);
92 	val &= RAA215300_REG_BLOCK_EN_RTC_EN;
93 	regmap_write(regmap, RAA215300_REG_BLOCK_EN, val);
94 
95 	/*Clear the latched registers */
96 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_1, &val);
97 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_1, val);
98 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_2, &val);
99 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_2, val);
100 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_3, &val);
101 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_3, val);
102 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_4, &val);
103 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_4, val);
104 	regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_6, &val);
105 	regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_6, val);
106 
107 	/* Mask all the PMIC interrupts */
108 	regmap_write(regmap, RAA215300_INT_MASK_1, RAA215300_INT_MASK_1_ALL);
109 	regmap_write(regmap, RAA215300_INT_MASK_2, RAA215300_INT_MASK_2_ALL);
110 	regmap_write(regmap, RAA215300_INT_MASK_3, RAA215300_INT_MASK_3_ALL);
111 	regmap_write(regmap, RAA215300_INT_MASK_4, RAA215300_INT_MASK_4_ALL);
112 	regmap_write(regmap, RAA215300_INT_MASK_6, RAA215300_INT_MASK_6_ALL);
113 
114 	ret = raa215300_clk_present(client, xin_name);
115 	if (ret < 0) {
116 		return ret;
117 	} else if (!ret) {
118 		ret = raa215300_clk_present(client, clkin_name);
119 		if (ret < 0)
120 			return ret;
121 
122 		clk_name = clkin_name;
123 	}
124 
125 	if (ret) {
126 		char *name = pmic_version >= 0x12 ? "isl1208" : "raa215300_a0";
127 		struct device_node *np = client->dev.of_node;
128 		u32 addr = RAA215300_RTC_DEFAULT_ADDR;
129 		struct i2c_board_info info = {};
130 		struct i2c_client *rtc_client;
131 		ssize_t size;
132 
133 		clk = clk_register_fixed_rate(NULL, clk_name, NULL, 0, 32000);
134 		clk_register_clkdev(clk, clk_name, NULL);
135 
136 		if (np) {
137 			int i;
138 
139 			i = of_property_match_string(np, "reg-names", "rtc");
140 			if (i >= 0)
141 				of_property_read_u32_index(np, "reg", i, &addr);
142 		}
143 
144 		info.addr = addr;
145 		if (client->irq > 0)
146 			info.irq = client->irq;
147 
148 		size = strscpy(info.type, name, sizeof(info.type));
149 		if (size < 0)
150 			return dev_err_probe(dev, size,
151 					     "Invalid device name: %s\n", name);
152 
153 		/* Enable RTC block */
154 		regmap_update_bits(regmap, RAA215300_REG_BLOCK_EN,
155 				   RAA215300_REG_BLOCK_EN_RTC_EN,
156 				   RAA215300_REG_BLOCK_EN_RTC_EN);
157 
158 		rtc_client = i2c_new_client_device(client->adapter, &info);
159 		if (IS_ERR(rtc_client))
160 			return PTR_ERR(rtc_client);
161 
162 		ret = devm_add_action_or_reset(dev,
163 					       raa215300_rtc_unregister_device,
164 					       rtc_client);
165 		if (ret < 0)
166 			return ret;
167 	}
168 
169 	return 0;
170 }
171 
172 static const struct of_device_id raa215300_dt_match[] = {
173 	{ .compatible = "renesas,raa215300" },
174 	{ /* sentinel */ }
175 };
176 MODULE_DEVICE_TABLE(of, raa215300_dt_match);
177 
178 static struct i2c_driver raa215300_i2c_driver = {
179 	.driver = {
180 		.name = "raa215300",
181 		.of_match_table = raa215300_dt_match,
182 	},
183 	.probe_new = raa215300_i2c_probe,
184 };
185 module_i2c_driver(raa215300_i2c_driver);
186 
187 MODULE_DESCRIPTION("Renesas RAA215300 PMIC driver");
188 MODULE_AUTHOR("Fabrizio Castro <fabrizio.castro.jz@renesas.com>");
189 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
190 MODULE_LICENSE("GPL");
191