xref: /openbmc/linux/drivers/mfd/wcd934x.c (revision 0ad53fe3)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019, Linaro Limited
3 
4 #include <linux/clk.h>
5 #include <linux/gpio.h>
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/mfd/core.h>
9 #include <linux/mfd/wcd934x/registers.h>
10 #include <linux/mfd/wcd934x/wcd934x.h>
11 #include <linux/module.h>
12 #include <linux/of_gpio.h>
13 #include <linux/of.h>
14 #include <linux/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slimbus.h>
19 
20 #define WCD934X_REGMAP_IRQ_REG(_irq, _off, _mask)		\
21 	[_irq] = {						\
22 		.reg_offset = (_off),				\
23 		.mask = (_mask),				\
24 		.type = {					\
25 			.type_reg_offset = (_off),		\
26 			.types_supported = IRQ_TYPE_EDGE_BOTH,	\
27 			.type_reg_mask  = (_mask),		\
28 			.type_level_low_val = (_mask),		\
29 			.type_level_high_val = (_mask),		\
30 			.type_falling_val = 0,			\
31 			.type_rising_val = 0,			\
32 		},						\
33 	}
34 
35 static const struct mfd_cell wcd934x_devices[] = {
36 	{
37 		.name = "wcd934x-codec",
38 	}, {
39 		.name = "wcd934x-gpio",
40 		.of_compatible = "qcom,wcd9340-gpio",
41 	}, {
42 		.name = "wcd934x-soundwire",
43 		.of_compatible = "qcom,soundwire-v1.3.0",
44 	},
45 };
46 
47 static const struct regmap_irq wcd934x_irqs[] = {
48 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_SLIMBUS, 0, BIT(0)),
49 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_HPH_PA_OCPL_FAULT, 0, BIT(2)),
50 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_HPH_PA_OCPR_FAULT, 0, BIT(3)),
51 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_MBHC_SW_DET, 1, BIT(0)),
52 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_MBHC_ELECT_INS_REM_DET, 1, BIT(1)),
53 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_MBHC_BUTTON_PRESS_DET, 1, BIT(2)),
54 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_MBHC_BUTTON_RELEASE_DET, 1, BIT(3)),
55 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 1, BIT(4)),
56 	WCD934X_REGMAP_IRQ_REG(WCD934X_IRQ_SOUNDWIRE, 2, BIT(4)),
57 };
58 
59 static const struct regmap_irq_chip wcd934x_regmap_irq_chip = {
60 	.name = "wcd934x_irq",
61 	.status_base = WCD934X_INTR_PIN1_STATUS0,
62 	.mask_base = WCD934X_INTR_PIN1_MASK0,
63 	.ack_base = WCD934X_INTR_PIN1_CLEAR0,
64 	.type_base = WCD934X_INTR_LEVEL0,
65 	.num_type_reg = 4,
66 	.type_in_mask = false,
67 	.num_regs = 4,
68 	.irqs = wcd934x_irqs,
69 	.num_irqs = ARRAY_SIZE(wcd934x_irqs),
70 };
71 
72 static bool wcd934x_is_volatile_register(struct device *dev, unsigned int reg)
73 {
74 	switch (reg) {
75 	case WCD934X_INTR_PIN1_STATUS0...WCD934X_INTR_PIN2_CLEAR3:
76 	case WCD934X_SWR_AHB_BRIDGE_RD_DATA_0:
77 	case WCD934X_SWR_AHB_BRIDGE_RD_DATA_1:
78 	case WCD934X_SWR_AHB_BRIDGE_RD_DATA_2:
79 	case WCD934X_SWR_AHB_BRIDGE_RD_DATA_3:
80 	case WCD934X_SWR_AHB_BRIDGE_ACCESS_STATUS:
81 	case WCD934X_ANA_MBHC_RESULT_3:
82 	case WCD934X_ANA_MBHC_RESULT_2:
83 	case WCD934X_ANA_MBHC_RESULT_1:
84 	case WCD934X_ANA_MBHC_MECH:
85 	case WCD934X_ANA_MBHC_ELECT:
86 	case WCD934X_ANA_MBHC_ZDET:
87 	case WCD934X_ANA_MICB2:
88 	case WCD934X_ANA_RCO:
89 	case WCD934X_ANA_BIAS:
90 		return true;
91 	default:
92 		return false;
93 	}
94 };
95 
96 static const struct regmap_range_cfg wcd934x_ranges[] = {
97 	{	.name = "WCD934X",
98 		.range_min =  0x0,
99 		.range_max =  WCD934X_MAX_REGISTER,
100 		.selector_reg = WCD934X_SEL_REGISTER,
101 		.selector_mask = WCD934X_SEL_MASK,
102 		.selector_shift = WCD934X_SEL_SHIFT,
103 		.window_start = WCD934X_WINDOW_START,
104 		.window_len = WCD934X_WINDOW_LENGTH,
105 	},
106 };
107 
108 static struct regmap_config wcd934x_regmap_config = {
109 	.reg_bits = 16,
110 	.val_bits = 8,
111 	.cache_type = REGCACHE_RBTREE,
112 	.max_register = 0xffff,
113 	.can_multi_write = true,
114 	.ranges = wcd934x_ranges,
115 	.num_ranges = ARRAY_SIZE(wcd934x_ranges),
116 	.volatile_reg = wcd934x_is_volatile_register,
117 };
118 
119 static int wcd934x_bring_up(struct wcd934x_ddata *ddata)
120 {
121 	struct regmap *regmap = ddata->regmap;
122 	u16 id_minor, id_major;
123 	int ret;
124 
125 	ret = regmap_bulk_read(regmap, WCD934X_CHIP_TIER_CTRL_CHIP_ID_BYTE0,
126 			       (u8 *)&id_minor, sizeof(u16));
127 	if (ret)
128 		return ret;
129 
130 	ret = regmap_bulk_read(regmap, WCD934X_CHIP_TIER_CTRL_CHIP_ID_BYTE2,
131 			       (u8 *)&id_major, sizeof(u16));
132 	if (ret)
133 		return ret;
134 
135 	dev_info(ddata->dev, "WCD934x chip id major 0x%x, minor 0x%x\n",
136 		 id_major, id_minor);
137 
138 	regmap_write(regmap, WCD934X_CODEC_RPM_RST_CTL, 0x01);
139 	regmap_write(regmap, WCD934X_SIDO_NEW_VOUT_A_STARTUP, 0x19);
140 	regmap_write(regmap, WCD934X_SIDO_NEW_VOUT_D_STARTUP, 0x15);
141 	/* Add 1msec delay for VOUT to settle */
142 	usleep_range(1000, 1100);
143 	regmap_write(regmap, WCD934X_CODEC_RPM_PWR_CDC_DIG_HM_CTL, 0x5);
144 	regmap_write(regmap, WCD934X_CODEC_RPM_PWR_CDC_DIG_HM_CTL, 0x7);
145 	regmap_write(regmap, WCD934X_CODEC_RPM_RST_CTL, 0x3);
146 	regmap_write(regmap, WCD934X_CODEC_RPM_RST_CTL, 0x7);
147 	regmap_write(regmap, WCD934X_CODEC_RPM_PWR_CDC_DIG_HM_CTL, 0x3);
148 
149 	return 0;
150 }
151 
152 static int wcd934x_slim_status_up(struct slim_device *sdev)
153 {
154 	struct device *dev = &sdev->dev;
155 	struct wcd934x_ddata *ddata;
156 	int ret;
157 
158 	ddata = dev_get_drvdata(dev);
159 
160 	ddata->regmap = regmap_init_slimbus(sdev, &wcd934x_regmap_config);
161 	if (IS_ERR(ddata->regmap)) {
162 		dev_err(dev, "Error allocating slim regmap\n");
163 		return PTR_ERR(ddata->regmap);
164 	}
165 
166 	ret = wcd934x_bring_up(ddata);
167 	if (ret) {
168 		dev_err(dev, "Failed to bring up WCD934X: err = %d\n", ret);
169 		return ret;
170 	}
171 
172 	ret = devm_regmap_add_irq_chip(dev, ddata->regmap, ddata->irq,
173 				       IRQF_TRIGGER_HIGH, 0,
174 				       &wcd934x_regmap_irq_chip,
175 				       &ddata->irq_data);
176 	if (ret) {
177 		dev_err(dev, "Failed to add IRQ chip: err = %d\n", ret);
178 		return ret;
179 	}
180 
181 	ret = mfd_add_devices(dev, PLATFORM_DEVID_AUTO, wcd934x_devices,
182 			      ARRAY_SIZE(wcd934x_devices), NULL, 0, NULL);
183 	if (ret) {
184 		dev_err(dev, "Failed to add child devices: err = %d\n",
185 			ret);
186 		return ret;
187 	}
188 
189 	return ret;
190 }
191 
192 static int wcd934x_slim_status(struct slim_device *sdev,
193 			       enum slim_device_status status)
194 {
195 	switch (status) {
196 	case SLIM_DEVICE_STATUS_UP:
197 		return wcd934x_slim_status_up(sdev);
198 	case SLIM_DEVICE_STATUS_DOWN:
199 		mfd_remove_devices(&sdev->dev);
200 		break;
201 	default:
202 		return -EINVAL;
203 	}
204 
205 	return 0;
206 }
207 
208 static int wcd934x_slim_probe(struct slim_device *sdev)
209 {
210 	struct device *dev = &sdev->dev;
211 	struct device_node *np = dev->of_node;
212 	struct wcd934x_ddata *ddata;
213 	int reset_gpio, ret;
214 
215 	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
216 	if (!ddata)
217 		return	-ENOMEM;
218 
219 	ddata->irq = of_irq_get(np, 0);
220 	if (ddata->irq < 0)
221 		return dev_err_probe(ddata->dev, ddata->irq,
222 				     "Failed to get IRQ\n");
223 
224 	reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
225 	if (reset_gpio < 0) {
226 		dev_err(dev, "Failed to get reset gpio: err = %d\n",
227 			reset_gpio);
228 		return reset_gpio;
229 	}
230 
231 	ddata->extclk = devm_clk_get(dev, "extclk");
232 	if (IS_ERR(ddata->extclk)) {
233 		dev_err(dev, "Failed to get extclk");
234 		return PTR_ERR(ddata->extclk);
235 	}
236 
237 	ddata->supplies[0].supply = "vdd-buck";
238 	ddata->supplies[1].supply = "vdd-buck-sido";
239 	ddata->supplies[2].supply = "vdd-tx";
240 	ddata->supplies[3].supply = "vdd-rx";
241 	ddata->supplies[4].supply = "vdd-io";
242 
243 	ret = regulator_bulk_get(dev, WCD934X_MAX_SUPPLY, ddata->supplies);
244 	if (ret) {
245 		dev_err(dev, "Failed to get supplies: err = %d\n", ret);
246 		return ret;
247 	}
248 
249 	ret = regulator_bulk_enable(WCD934X_MAX_SUPPLY, ddata->supplies);
250 	if (ret) {
251 		dev_err(dev, "Failed to enable supplies: err = %d\n", ret);
252 		return ret;
253 	}
254 
255 	/*
256 	 * For WCD934X, it takes about 600us for the Vout_A and
257 	 * Vout_D to be ready after BUCK_SIDO is powered up.
258 	 * SYS_RST_N shouldn't be pulled high during this time
259 	 */
260 	usleep_range(600, 650);
261 	gpio_direction_output(reset_gpio, 0);
262 	msleep(20);
263 	gpio_set_value(reset_gpio, 1);
264 	msleep(20);
265 
266 	ddata->dev = dev;
267 	dev_set_drvdata(dev, ddata);
268 
269 	return 0;
270 }
271 
272 static void wcd934x_slim_remove(struct slim_device *sdev)
273 {
274 	struct wcd934x_ddata *ddata = dev_get_drvdata(&sdev->dev);
275 
276 	regulator_bulk_disable(WCD934X_MAX_SUPPLY, ddata->supplies);
277 	mfd_remove_devices(&sdev->dev);
278 }
279 
280 static const struct slim_device_id wcd934x_slim_id[] = {
281 	{ SLIM_MANF_ID_QCOM, SLIM_PROD_CODE_WCD9340,
282 	  SLIM_DEV_IDX_WCD9340, SLIM_DEV_INSTANCE_ID_WCD9340 },
283 	{}
284 };
285 
286 static struct slim_driver wcd934x_slim_driver = {
287 	.driver = {
288 		.name = "wcd934x-slim",
289 	},
290 	.probe = wcd934x_slim_probe,
291 	.remove = wcd934x_slim_remove,
292 	.device_status = wcd934x_slim_status,
293 	.id_table = wcd934x_slim_id,
294 };
295 
296 module_slim_driver(wcd934x_slim_driver);
297 MODULE_DESCRIPTION("WCD934X slim driver");
298 MODULE_LICENSE("GPL v2");
299 MODULE_ALIAS("slim:217:250:*");
300 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
301