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