1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019, Linaro Limited
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>
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>
20 #define WCD934X_REGMAP_IRQ_REG(_irq, _off, _mask) \
22 .reg_offset = (_off), \
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, \
35 static const struct mfd_cell wcd934x_devices[] = {
37 .name = "wcd934x-codec",
39 .name = "wcd934x-gpio",
40 .of_compatible = "qcom,wcd9340-gpio",
42 .name = "wcd934x-soundwire",
43 .of_compatible = "qcom,soundwire-v1.3.0",
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_SOUNDWIRE, 2, BIT(4)),
52 static const struct regmap_irq_chip wcd934x_regmap_irq_chip = {
53 .name = "wcd934x_irq",
54 .status_base = WCD934X_INTR_PIN1_STATUS0,
55 .mask_base = WCD934X_INTR_PIN1_MASK0,
56 .ack_base = WCD934X_INTR_PIN1_CLEAR0,
57 .type_base = WCD934X_INTR_LEVEL0,
59 .type_in_mask = false,
62 .num_irqs = ARRAY_SIZE(wcd934x_irqs),
65 static bool wcd934x_is_volatile_register(struct device *dev, unsigned int reg)
68 case WCD934X_INTR_PIN1_STATUS0...WCD934X_INTR_PIN2_CLEAR3:
69 case WCD934X_SWR_AHB_BRIDGE_RD_DATA_0:
70 case WCD934X_SWR_AHB_BRIDGE_RD_DATA_1:
71 case WCD934X_SWR_AHB_BRIDGE_RD_DATA_2:
72 case WCD934X_SWR_AHB_BRIDGE_RD_DATA_3:
73 case WCD934X_SWR_AHB_BRIDGE_ACCESS_STATUS:
74 case WCD934X_ANA_MBHC_RESULT_3:
75 case WCD934X_ANA_MBHC_RESULT_2:
76 case WCD934X_ANA_MBHC_RESULT_1:
77 case WCD934X_ANA_MBHC_MECH:
78 case WCD934X_ANA_MBHC_ELECT:
79 case WCD934X_ANA_MBHC_ZDET:
80 case WCD934X_ANA_MICB2:
82 case WCD934X_ANA_BIAS:
89 static const struct regmap_range_cfg wcd934x_ranges[] = {
92 .range_max = WCD934X_MAX_REGISTER,
93 .selector_reg = WCD934X_SEL_REGISTER,
94 .selector_mask = WCD934X_SEL_MASK,
95 .selector_shift = WCD934X_SEL_SHIFT,
96 .window_start = WCD934X_WINDOW_START,
97 .window_len = WCD934X_WINDOW_LENGTH,
101 static struct regmap_config wcd934x_regmap_config = {
104 .cache_type = REGCACHE_RBTREE,
105 .max_register = 0xffff,
106 .can_multi_write = true,
107 .ranges = wcd934x_ranges,
108 .num_ranges = ARRAY_SIZE(wcd934x_ranges),
109 .volatile_reg = wcd934x_is_volatile_register,
112 static int wcd934x_bring_up(struct wcd934x_ddata *ddata)
114 struct regmap *regmap = ddata->regmap;
115 u16 id_minor, id_major;
118 ret = regmap_bulk_read(regmap, WCD934X_CHIP_TIER_CTRL_CHIP_ID_BYTE0,
119 (u8 *)&id_minor, sizeof(u16));
123 ret = regmap_bulk_read(regmap, WCD934X_CHIP_TIER_CTRL_CHIP_ID_BYTE2,
124 (u8 *)&id_major, sizeof(u16));
128 dev_info(ddata->dev, "WCD934x chip id major 0x%x, minor 0x%x\n",
131 regmap_write(regmap, WCD934X_CODEC_RPM_RST_CTL, 0x01);
132 regmap_write(regmap, WCD934X_SIDO_NEW_VOUT_A_STARTUP, 0x19);
133 regmap_write(regmap, WCD934X_SIDO_NEW_VOUT_D_STARTUP, 0x15);
134 /* Add 1msec delay for VOUT to settle */
135 usleep_range(1000, 1100);
136 regmap_write(regmap, WCD934X_CODEC_RPM_PWR_CDC_DIG_HM_CTL, 0x5);
137 regmap_write(regmap, WCD934X_CODEC_RPM_PWR_CDC_DIG_HM_CTL, 0x7);
138 regmap_write(regmap, WCD934X_CODEC_RPM_RST_CTL, 0x3);
139 regmap_write(regmap, WCD934X_CODEC_RPM_RST_CTL, 0x7);
140 regmap_write(regmap, WCD934X_CODEC_RPM_PWR_CDC_DIG_HM_CTL, 0x3);
145 static int wcd934x_slim_status_up(struct slim_device *sdev)
147 struct device *dev = &sdev->dev;
148 struct wcd934x_ddata *ddata;
151 ddata = dev_get_drvdata(dev);
153 ddata->regmap = regmap_init_slimbus(sdev, &wcd934x_regmap_config);
154 if (IS_ERR(ddata->regmap)) {
155 dev_err(dev, "Error allocating slim regmap\n");
156 return PTR_ERR(ddata->regmap);
159 ret = wcd934x_bring_up(ddata);
161 dev_err(dev, "Failed to bring up WCD934X: err = %d\n", ret);
165 ret = devm_regmap_add_irq_chip(dev, ddata->regmap, ddata->irq,
166 IRQF_TRIGGER_HIGH, 0,
167 &wcd934x_regmap_irq_chip,
170 dev_err(dev, "Failed to add IRQ chip: err = %d\n", ret);
174 ret = mfd_add_devices(dev, PLATFORM_DEVID_AUTO, wcd934x_devices,
175 ARRAY_SIZE(wcd934x_devices), NULL, 0, NULL);
177 dev_err(dev, "Failed to add child devices: err = %d\n",
185 static int wcd934x_slim_status(struct slim_device *sdev,
186 enum slim_device_status status)
189 case SLIM_DEVICE_STATUS_UP:
190 return wcd934x_slim_status_up(sdev);
191 case SLIM_DEVICE_STATUS_DOWN:
192 mfd_remove_devices(&sdev->dev);
201 static int wcd934x_slim_probe(struct slim_device *sdev)
203 struct device *dev = &sdev->dev;
204 struct device_node *np = dev->of_node;
205 struct wcd934x_ddata *ddata;
208 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
212 ddata->irq = of_irq_get(np, 0);
214 return dev_err_probe(ddata->dev, ddata->irq,
215 "Failed to get IRQ\n");
217 reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
218 if (reset_gpio < 0) {
219 dev_err(dev, "Failed to get reset gpio: err = %d\n",
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);
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";
236 ret = regulator_bulk_get(dev, WCD934X_MAX_SUPPLY, ddata->supplies);
238 dev_err(dev, "Failed to get supplies: err = %d\n", ret);
242 ret = regulator_bulk_enable(WCD934X_MAX_SUPPLY, ddata->supplies);
244 dev_err(dev, "Failed to enable supplies: err = %d\n", ret);
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
253 usleep_range(600, 650);
254 gpio_direction_output(reset_gpio, 0);
256 gpio_set_value(reset_gpio, 1);
260 dev_set_drvdata(dev, ddata);
265 static void wcd934x_slim_remove(struct slim_device *sdev)
267 struct wcd934x_ddata *ddata = dev_get_drvdata(&sdev->dev);
269 regulator_bulk_disable(WCD934X_MAX_SUPPLY, ddata->supplies);
270 mfd_remove_devices(&sdev->dev);
273 static const struct slim_device_id wcd934x_slim_id[] = {
274 { SLIM_MANF_ID_QCOM, SLIM_PROD_CODE_WCD9340,
275 SLIM_DEV_IDX_WCD9340, SLIM_DEV_INSTANCE_ID_WCD9340 },
279 static struct slim_driver wcd934x_slim_driver = {
281 .name = "wcd934x-slim",
283 .probe = wcd934x_slim_probe,
284 .remove = wcd934x_slim_remove,
285 .device_status = wcd934x_slim_status,
286 .id_table = wcd934x_slim_id,
289 module_slim_driver(wcd934x_slim_driver);
290 MODULE_DESCRIPTION("WCD934X slim driver");
291 MODULE_LICENSE("GPL v2");
292 MODULE_ALIAS("slim:217:250:*");
293 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");