blob: 038e338e53b6ec4948cbf12398af698dc96e042f [file] [log] [blame]
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05301// SPDX-License-Identifier: GPL-2.0-only
2/*
Laxminath Kasam0bb3a152020-02-03 12:47:48 +05303 * Copyright (c) 2015-2016, 2018-2020, The Linux Foundation. All rights reserved.
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05304 */
5
6#include <linux/clk.h>
Laxminath Kasam0bb3a152020-02-03 12:47:48 +05307#include <linux/clk-provider.h>
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05308#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/of_gpio.h>
12#include <linux/platform_device.h>
13#include <linux/device.h>
14#include <linux/printk.h>
15#include <linux/bitops.h>
16#include <linux/regulator/consumer.h>
17#include <linux/pm_runtime.h>
18#include <soc/soundwire.h>
19#include <sound/pcm.h>
20#include <sound/pcm_params.h>
21#include <sound/soc.h>
22#include <sound/soc-dapm.h>
23#include <sound/tlv.h>
24#include <dsp/q6afe-v2.h>
25#include <linux/delay.h>
26#include <linux/i2c.h>
27#include <linux/kernel.h>
28#include <linux/gpio.h>
29#include <soc/internal.h>
30#include <linux/regmap.h>
31#include <asoc/msm-cdc-pinctrl.h>
32#include "wsa881x-analog.h"
33#include "wsa881x-temp-sensor.h"
34
35#define SPK_GAIN_12DB 4
36#define WIDGET_NAME_MAX_SIZE 80
37
38/*
39 * Private data Structure for wsa881x. All parameters related to
40 * WSA881X codec needs to be defined here.
41 */
42struct wsa881x_pdata {
43 struct regmap *regmap[2];
44 struct i2c_client *client[2];
45 struct snd_soc_component *component;
46
47 /* track wsa881x status during probe */
48 int status;
49 bool boost_enable;
50 bool visense_enable;
51 int spk_pa_gain;
52 struct i2c_msg xfer_msg[2];
53 struct mutex xfer_lock;
54 bool regmap_flag;
55 bool wsa_active;
56 int index;
57 struct wsa881x_tz_priv tz_pdata;
58 struct clk *wsa_mclk;
59 int bg_cnt;
60 int clk_cnt;
61 int enable_cnt;
62 int version;
63 struct mutex bg_lock;
64 struct mutex res_lock;
65 struct delayed_work ocp_ctl_work;
66 struct device_node *wsa_vi_gpio_p;
67 struct device_node *wsa_clk_gpio_p;
68 struct device_node *wsa_reset_gpio_p;
69};
70
71enum {
72 WSA881X_STATUS_PROBING,
73 WSA881X_STATUS_I2C,
74};
75
76#define WSA881X_OCP_CTL_TIMER_SEC 2
77#define WSA881X_OCP_CTL_TEMP_CELSIUS 25
78#define WSA881X_OCP_CTL_POLL_TIMER_SEC 60
79
80static int wsa881x_ocp_poll_timer_sec = WSA881X_OCP_CTL_POLL_TIMER_SEC;
81module_param(wsa881x_ocp_poll_timer_sec, int, 0664);
82MODULE_PARM_DESC(wsa881x_ocp_poll_timer_sec, "timer for ocp ctl polling");
83
84static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
85 bool enable);
86
87const char *wsa_tz_names[] = {"wsa881x.0e", "wsa881x.0f"};
88
89struct wsa881x_pdata wsa_pdata[MAX_WSA881X_DEVICE];
90
91static bool pinctrl_init;
92
93static int wsa881x_populate_dt_pdata(struct device *dev, int wsa881x_index);
94static int wsa881x_reset(struct wsa881x_pdata *pdata, bool enable);
95static int wsa881x_startup(struct wsa881x_pdata *pdata);
96static int wsa881x_shutdown(struct wsa881x_pdata *pdata);
97
98static int delay_array_msec[] = {10, 20, 30, 40, 50};
99
100static int wsa881x_i2c_addr = -1;
101static int wsa881x_probing_count;
102static int wsa881x_presence_count;
103
104static const char * const wsa881x_spk_pa_gain_text[] = {
105"POS_13P5_DB", "POS_12_DB", "POS_10P5_DB", "POS_9_DB", "POS_7P5_DB",
106"POS_6_DB", "POS_4P5_DB", "POS_3_DB", "POS_1P5_DB", "POS_0_DB"};
107
108static const struct soc_enum wsa881x_spk_pa_gain_enum[] = {
109 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa881x_spk_pa_gain_text),
110 wsa881x_spk_pa_gain_text),
111};
112
113static int wsa881x_spk_pa_gain_get(struct snd_kcontrol *kcontrol,
114 struct snd_ctl_elem_value *ucontrol)
115{
116 struct snd_soc_component *component =
117 snd_soc_kcontrol_component(kcontrol);
118 struct wsa881x_pdata *wsa881x =
119 snd_soc_component_get_drvdata(component);
120
121 ucontrol->value.integer.value[0] = wsa881x->spk_pa_gain;
122
123 dev_dbg(component->dev, "%s: spk_pa_gain = %ld\n", __func__,
124 ucontrol->value.integer.value[0]);
125
126 return 0;
127}
128
129static int wsa881x_spk_pa_gain_put(struct snd_kcontrol *kcontrol,
130 struct snd_ctl_elem_value *ucontrol)
131{
132 struct snd_soc_component *component =
133 snd_soc_kcontrol_component(kcontrol);
134 struct wsa881x_pdata *wsa881x =
135 snd_soc_component_get_drvdata(component);
136
137 if (ucontrol->value.integer.value[0] < 0 ||
138 ucontrol->value.integer.value[0] > 0xC) {
139 dev_err(component->dev, "%s: Unsupported gain val %ld\n",
140 __func__, ucontrol->value.integer.value[0]);
141 return -EINVAL;
142 }
143 wsa881x->spk_pa_gain = ucontrol->value.integer.value[0];
144 dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
145 __func__, ucontrol->value.integer.value[0]);
146
147 return 0;
148}
149
150static int get_i2c_wsa881x_device_index(u16 reg)
151{
152 u16 mask = 0x0f00;
153 int value = 0;
154
155 value = ((reg & mask) >> 8) & 0x000f;
156
157 switch (value) {
158 case 0:
159 return 0;
160 case 1:
161 return 1;
162 default:
163 break;
164 }
165 return -EINVAL;
166}
167
168static int wsa881x_i2c_write_device(struct wsa881x_pdata *wsa881x,
169 unsigned int reg, unsigned int val)
170{
171 int i = 0, rc = 0;
172 int wsa881x_index;
173 struct i2c_msg *msg;
174 int ret = 0;
175 int bytes = 1;
176 u8 reg_addr = 0;
177 u8 data[2];
178
179 wsa881x_index = get_i2c_wsa881x_device_index(reg);
180 if (wsa881x_index < 0) {
181 pr_err_ratelimited("%s:invalid register to write\n", __func__);
182 return -EINVAL;
183 }
184 if (wsa881x->regmap_flag) {
185 rc = regmap_write(wsa881x->regmap[wsa881x_index], reg, val);
186 for (i = 0; rc && i < ARRAY_SIZE(delay_array_msec); i++) {
187 pr_err_ratelimited("Failed writing reg=%u-retry(%d)\n",
188 reg, i);
189 /* retry after delay of increasing order */
190 msleep(delay_array_msec[i]);
191 rc = regmap_write(wsa881x->regmap[wsa881x_index],
192 reg, val);
193 }
194 if (rc)
195 pr_err_ratelimited("Failed writing reg=%u rc=%d\n",
196 reg, rc);
197 else
198 pr_debug("write success register = %x val = %x\n",
199 reg, val);
200 } else {
201 reg_addr = (u8)reg;
202 msg = &wsa881x->xfer_msg[0];
203 msg->addr = wsa881x->client[wsa881x_index]->addr;
204 msg->len = bytes + 1;
205 msg->flags = 0;
206 data[0] = reg;
207 data[1] = (u8)val;
208 msg->buf = data;
209 ret = i2c_transfer(wsa881x->client[wsa881x_index]->adapter,
210 wsa881x->xfer_msg, 1);
211 /* Try again if the write fails */
212 if (ret != 1) {
213 ret = i2c_transfer(
214 wsa881x->client[wsa881x_index]->adapter,
215 wsa881x->xfer_msg, 1);
216 if (ret != 1) {
217 pr_err_ratelimited("failed to write the device\n");
218 return ret;
219 }
220 }
221 pr_debug("write success reg = %x val = %x\n", reg, data[1]);
222 }
223 return rc;
224}
225
226static int wsa881x_i2c_read_device(struct wsa881x_pdata *wsa881x,
227 unsigned int reg)
228{
229 int wsa881x_index;
230 int i = 0, rc = 0;
231 unsigned int val;
232 struct i2c_msg *msg;
233 int ret = 0;
234 u8 reg_addr = 0;
235 u8 dest[5] = {0};
236
237 wsa881x_index = get_i2c_wsa881x_device_index(reg);
238 if (wsa881x_index < 0) {
239 pr_err_ratelimited("%s:invalid register to read\n", __func__);
240 return -EINVAL;
241 }
242 if (wsa881x->regmap_flag) {
243 rc = regmap_read(wsa881x->regmap[wsa881x_index], reg, &val);
244 for (i = 0; rc && i < ARRAY_SIZE(delay_array_msec); i++) {
245 pr_err_ratelimited("Failed reading reg=%u - retry(%d)\n",
246 reg, i);
247 /* retry after delay of increasing order */
248 msleep(delay_array_msec[i]);
249 rc = regmap_read(wsa881x->regmap[wsa881x_index],
250 reg, &val);
251 }
252 if (rc) {
253 pr_err_ratelimited("Failed reading reg=%u rc=%d\n",
254 reg, rc);
255 return rc;
256 }
257 pr_debug("read success reg = %x val = %x\n",
258 reg, val);
259 } else {
260 reg_addr = (u8)reg;
261 msg = &wsa881x->xfer_msg[0];
262 msg->addr = wsa881x->client[wsa881x_index]->addr;
263 msg->len = 1;
264 msg->flags = 0;
265 msg->buf = &reg_addr;
266
267 msg = &wsa881x->xfer_msg[1];
268 msg->addr = wsa881x->client[wsa881x_index]->addr;
269 msg->len = 1;
270 msg->flags = I2C_M_RD;
271 msg->buf = dest;
272 ret = i2c_transfer(wsa881x->client[wsa881x_index]->adapter,
273 wsa881x->xfer_msg, 2);
274
275 /* Try again if read fails first time */
276 if (ret != 2) {
277 ret = i2c_transfer(
278 wsa881x->client[wsa881x_index]->adapter,
279 wsa881x->xfer_msg, 2);
280 if (ret != 2) {
281 pr_err_ratelimited("failed to read wsa register:%d\n",
282 reg);
283 return ret;
284 }
285 }
286 val = dest[0];
287 }
288 return val;
289}
290
291static unsigned int wsa881x_i2c_read(struct snd_soc_component *component,
292 unsigned int reg)
293{
294 struct wsa881x_pdata *wsa881x;
295 int wsa881x_index;
296
297 if (component == NULL) {
298 pr_err_ratelimited("%s: invalid component\n", __func__);
299 return -EINVAL;
300 }
301 wsa881x = snd_soc_component_get_drvdata(component);
302 if (!wsa881x->wsa_active)
303 return 0;
304
305 wsa881x_index = get_i2c_wsa881x_device_index(reg);
306 if (wsa881x_index < 0) {
307 pr_err_ratelimited("%s:invalid register to read\n", __func__);
308 return -EINVAL;
309 }
310 return wsa881x_i2c_read_device(wsa881x, reg);
311}
312
313static int wsa881x_i2c_write(struct snd_soc_component *component,
314 unsigned int reg,
315 unsigned int val)
316{
317 struct wsa881x_pdata *wsa881x;
318 int wsa881x_index;
319
320 if (component == NULL) {
321 pr_err_ratelimited("%s: invalid component\n", __func__);
322 return -EINVAL;
323 }
324 wsa881x = snd_soc_component_get_drvdata(component);
325 if (!wsa881x->wsa_active)
326 return 0;
327
328 wsa881x_index = get_i2c_wsa881x_device_index(reg);
329 if (wsa881x_index < 0) {
330 pr_err_ratelimited("%s:invalid register to read\n", __func__);
331 return -EINVAL;
332 }
333 return wsa881x_i2c_write_device(wsa881x, reg, val);
334}
335
336static int wsa881x_i2c_get_client_index(struct i2c_client *client,
337 int *wsa881x_index)
338{
339 int ret = 0;
340
341 switch (client->addr) {
342 case WSA881X_I2C_SPK0_SLAVE0_ADDR:
343 case WSA881X_I2C_SPK0_SLAVE1_ADDR:
344 *wsa881x_index = WSA881X_I2C_SPK0_SLAVE0;
345 break;
346 case WSA881X_I2C_SPK1_SLAVE0_ADDR:
347 case WSA881X_I2C_SPK1_SLAVE1_ADDR:
348 *wsa881x_index = WSA881X_I2C_SPK1_SLAVE0;
349 break;
350 default:
351 ret = -EINVAL;
352 break;
353 }
354 return ret;
355}
356
357static int wsa881x_boost_ctrl(struct snd_soc_component *component, bool enable)
358{
359 struct wsa881x_pdata *wsa881x =
360 snd_soc_component_get_drvdata(component);
361
362
363 pr_debug("%s: enable:%d\n", __func__, enable);
364 if (enable) {
365 if (!WSA881X_IS_2_0(wsa881x->version)) {
366 snd_soc_component_update_bits(component,
367 WSA881X_ANA_CTL, 0x01, 0x01);
368 snd_soc_component_update_bits(component,
369 WSA881X_ANA_CTL, 0x04, 0x04);
370 snd_soc_component_update_bits(component,
371 WSA881X_BOOST_PS_CTL,
372 0x40, 0x00);
373 snd_soc_component_update_bits(component,
374 WSA881X_BOOST_PRESET_OUT1,
375 0xF0, 0xB0);
376 snd_soc_component_update_bits(component,
377 WSA881X_BOOST_ZX_CTL,
378 0x20, 0x00);
379 snd_soc_component_update_bits(component,
380 WSA881X_BOOST_EN_CTL,
381 0x80, 0x80);
382 } else {
383 snd_soc_component_update_bits(component,
384 WSA881X_BOOST_LOOP_STABILITY,
385 0x03, 0x03);
386 snd_soc_component_update_bits(component,
387 WSA881X_BOOST_MISC2_CTL,
388 0xFF, 0x14);
389 snd_soc_component_update_bits(component,
390 WSA881X_BOOST_START_CTL,
391 0x80, 0x80);
392 snd_soc_component_update_bits(component,
393 WSA881X_BOOST_START_CTL,
394 0x03, 0x00);
395 snd_soc_component_update_bits(component,
396 WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
397 0x0C, 0x04);
398 snd_soc_component_update_bits(component,
399 WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
400 0x03, 0x00);
401 snd_soc_component_update_bits(component,
402 WSA881X_BOOST_PRESET_OUT1,
403 0xF0, 0x70);
404 snd_soc_component_update_bits(component,
405 WSA881X_ANA_CTL, 0x03, 0x01);
406 snd_soc_component_update_bits(component,
407 WSA881X_SPKR_DRV_EN,
408 0x08, 0x08);
409 snd_soc_component_update_bits(component,
410 WSA881X_ANA_CTL, 0x04, 0x04);
411 snd_soc_component_update_bits(component,
412 WSA881X_BOOST_CURRENT_LIMIT,
413 0x0F, 0x08);
414 snd_soc_component_update_bits(component,
415 WSA881X_BOOST_EN_CTL,
416 0x80, 0x80);
417 }
418 /* For WSA8810, start-up time is 1500us as per qcrg sequence */
419 usleep_range(1500, 1510);
420 } else {
421 /* ENSURE: Class-D amp is shutdown. CLK is still on */
422 snd_soc_component_update_bits(component,
423 WSA881X_BOOST_EN_CTL, 0x80, 0x00);
424 /* boost settle time is 1500us as per qcrg sequence */
425 usleep_range(1500, 1510);
426 }
427 return 0;
428}
429
430static int wsa881x_visense_txfe_ctrl(struct snd_soc_component *component,
431 bool enable,
432 u8 isense1_gain, u8 isense2_gain,
433 u8 vsense_gain)
434{
435 u8 value = 0;
436 struct wsa881x_pdata *wsa881x =
437 snd_soc_component_get_drvdata(component);
438
439 pr_debug("%s: enable:%d\n", __func__, enable);
440
441 if (enable) {
442 if (WSA881X_IS_2_0(wsa881x->version)) {
443 snd_soc_component_update_bits(component,
444 WSA881X_OTP_REG_28,
445 0x3F, 0x3A);
446 snd_soc_component_update_bits(component,
447 WSA881X_BONGO_RESRV_REG1,
448 0xFF, 0xB2);
449 snd_soc_component_update_bits(component,
450 WSA881X_BONGO_RESRV_REG2,
451 0xFF, 0x05);
452 }
453 snd_soc_component_update_bits(component,
454 WSA881X_SPKR_PROT_FE_VSENSE_VCM,
455 0x08, 0x00);
456 if (WSA881X_IS_2_0(wsa881x->version)) {
457 snd_soc_component_update_bits(component,
458 WSA881X_SPKR_PROT_ATEST2,
459 0x1C, 0x04);
460 } else {
461 snd_soc_component_update_bits(component,
462 WSA881X_SPKR_PROT_ATEST2,
463 0x08, 0x08);
464 snd_soc_component_update_bits(component,
465 WSA881X_SPKR_PROT_ATEST2,
466 0x02, 0x02);
467 }
468 value = ((isense2_gain << 6) | (isense1_gain << 4) |
469 (vsense_gain << 3));
470 snd_soc_component_update_bits(component,
471 WSA881X_SPKR_PROT_FE_GAIN,
472 0xF8, value);
473 snd_soc_component_update_bits(component,
474 WSA881X_SPKR_PROT_FE_GAIN,
475 0x01, 0x01);
476 } else {
477 if (WSA881X_IS_2_0(wsa881x->version))
478 snd_soc_component_update_bits(component,
479 WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x10, 0x10);
480 else
481 snd_soc_component_update_bits(component,
482 WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x08, 0x08);
483 /*
484 * 200us sleep is needed after visense txfe disable as per
485 * HW requirement.
486 */
487 usleep_range(200, 210);
488
489 snd_soc_component_update_bits(component,
490 WSA881X_SPKR_PROT_FE_GAIN,
491 0x01, 0x00);
492 }
493 return 0;
494}
495
496static int wsa881x_visense_adc_ctrl(struct snd_soc_component *component,
497 bool enable)
498{
499 struct wsa881x_pdata *wsa881x =
500 snd_soc_component_get_drvdata(component);
501
502 pr_debug("%s: enable:%d\n", __func__, enable);
503 if (enable) {
504 if (!WSA881X_IS_2_0(wsa881x->version))
505 snd_soc_component_update_bits(component,
506 WSA881X_ADC_SEL_IBIAS,
507 0x70, 0x40);
508 snd_soc_component_update_bits(component,
509 WSA881X_ADC_EN_SEL_IBIAS,
510 0x07, 0x04);
511 snd_soc_component_update_bits(component,
512 WSA881X_ADC_EN_MODU_V, 0x80, 0x80);
513 snd_soc_component_update_bits(component,
514 WSA881X_ADC_EN_MODU_I, 0x80, 0x80);
515 } else {
516 /* Ensure: Speaker Protection has been stopped */
517 snd_soc_component_update_bits(component,
518 WSA881X_ADC_EN_MODU_V, 0x80, 0x00);
519 snd_soc_component_update_bits(component,
520 WSA881X_ADC_EN_MODU_I, 0x80, 0x00);
521 }
522
523 return 0;
524}
525
526static void wsa881x_bandgap_ctrl(struct snd_soc_component *component,
527 bool enable)
528{
529 struct wsa881x_pdata *wsa881x =
530 snd_soc_component_get_drvdata(component);
531
532 dev_dbg(component->dev, "%s: enable:%d, bg_count:%d\n", __func__,
533 enable, wsa881x->bg_cnt);
534 mutex_lock(&wsa881x->bg_lock);
535 if (enable) {
536 ++wsa881x->bg_cnt;
537 if (wsa881x->bg_cnt == 1) {
538 snd_soc_component_update_bits(component,
539 WSA881X_TEMP_OP, 0x08, 0x08);
540 /* 400usec sleep is needed as per HW requirement */
541 usleep_range(400, 410);
542 snd_soc_component_update_bits(component,
543 WSA881X_TEMP_OP, 0x04, 0x04);
544 }
545 } else {
546 --wsa881x->bg_cnt;
547 if (wsa881x->bg_cnt <= 0) {
548 WARN_ON(wsa881x->bg_cnt < 0);
549 wsa881x->bg_cnt = 0;
550 snd_soc_component_update_bits(component,
551 WSA881X_TEMP_OP, 0x04, 0x00);
552 snd_soc_component_update_bits(component,
553 WSA881X_TEMP_OP, 0x08, 0x00);
554 }
555 }
556 mutex_unlock(&wsa881x->bg_lock);
557}
558
559static void wsa881x_clk_ctrl(struct snd_soc_component *component, bool enable)
560{
561 struct wsa881x_pdata *wsa881x =
562 snd_soc_component_get_drvdata(component);
563
564 dev_dbg(component->dev, "%s:ss enable:%d, clk_count:%d\n", __func__,
565 enable, wsa881x->clk_cnt);
566 mutex_lock(&wsa881x->res_lock);
567 if (enable) {
568 ++wsa881x->clk_cnt;
569 if (wsa881x->clk_cnt == 1) {
570 snd_soc_component_write(component,
571 WSA881X_CDC_RST_CTL, 0x02);
572 snd_soc_component_write(component,
573 WSA881X_CDC_RST_CTL, 0x03);
574 snd_soc_component_write(component,
575 WSA881X_CLOCK_CONFIG, 0x01);
576
577 snd_soc_component_write(component,
578 WSA881X_CDC_DIG_CLK_CTL, 0x01);
579 snd_soc_component_write(component,
580 WSA881X_CDC_ANA_CLK_CTL, 0x01);
581
582 }
583 } else {
584 --wsa881x->clk_cnt;
585 if (wsa881x->clk_cnt <= 0) {
586 WARN_ON(wsa881x->clk_cnt < 0);
587 wsa881x->clk_cnt = 0;
588 snd_soc_component_write(component,
589 WSA881X_CDC_ANA_CLK_CTL, 0x00);
590 snd_soc_component_write(component,
591 WSA881X_CDC_DIG_CLK_CTL, 0x00);
592 if (WSA881X_IS_2_0(wsa881x->version))
593 snd_soc_component_update_bits(component,
594 WSA881X_CDC_TOP_CLK_CTL, 0x01, 0x00);
595 }
596 }
597 mutex_unlock(&wsa881x->res_lock);
598}
599
600static int wsa881x_rdac_ctrl(struct snd_soc_component *component, bool enable)
601{
602 struct wsa881x_pdata *wsa881x =
603 snd_soc_component_get_drvdata(component);
604
605 pr_debug("%s: enable:%d\n", __func__, enable);
606 if (enable) {
607 snd_soc_component_update_bits(component,
608 WSA881X_ANA_CTL, 0x08, 0x00);
609 snd_soc_component_update_bits(component,
610 WSA881X_SPKR_DRV_GAIN, 0x08, 0x08);
611 snd_soc_component_update_bits(component,
612 WSA881X_SPKR_DAC_CTL, 0x20, 0x20);
613 snd_soc_component_update_bits(component,
614 WSA881X_SPKR_DAC_CTL, 0x20, 0x00);
615 snd_soc_component_update_bits(component,
616 WSA881X_SPKR_DAC_CTL, 0x40, 0x40);
617 snd_soc_component_update_bits(component,
618 WSA881X_SPKR_DAC_CTL, 0x80, 0x80);
619 if (WSA881X_IS_2_0(wsa881x->version)) {
620 snd_soc_component_update_bits(component,
621 WSA881X_SPKR_BIAS_CAL, 0x01, 0x01);
622 snd_soc_component_update_bits(component,
623 WSA881X_SPKR_OCP_CTL, 0x30, 0x30);
624 snd_soc_component_update_bits(component,
625 WSA881X_SPKR_OCP_CTL, 0x0C, 0x00);
626 }
627 snd_soc_component_update_bits(component,
628 WSA881X_SPKR_DRV_GAIN, 0xF0, 0x40);
629 snd_soc_component_update_bits(component,
630 WSA881X_SPKR_MISC_CTL1, 0x01, 0x01);
631 } else {
632 /* Ensure class-D amp is off */
633 snd_soc_component_update_bits(component,
634 WSA881X_SPKR_DAC_CTL, 0x80, 0x00);
635 }
636 return 0;
637}
638
639static int wsa881x_spkr_pa_ctrl(struct snd_soc_component *component,
640 bool enable)
641{
642 int ret = 0;
643 struct wsa881x_pdata *wsa881x =
644 snd_soc_component_get_drvdata(component);
645
646 pr_debug("%s: enable:%d\n", __func__, enable);
647 if (enable) {
648 /*
649 * Ensure: Boost is enabled and stable, Analog input is up
650 * and outputting silence
651 */
652 if (!WSA881X_IS_2_0(wsa881x->version)) {
653 snd_soc_component_update_bits(component,
654 WSA881X_ADC_EN_DET_TEST_I,
655 0xFF, 0x01);
656 snd_soc_component_update_bits(component,
657 WSA881X_ADC_EN_MODU_V,
658 0x02, 0x02);
659 snd_soc_component_update_bits(component,
660 WSA881X_ADC_EN_DET_TEST_V,
661 0xFF, 0x10);
662 snd_soc_component_update_bits(component,
663 WSA881X_SPKR_PWRSTG_DBG,
664 0xA0, 0xA0);
665 snd_soc_component_update_bits(component,
666 WSA881X_SPKR_DRV_EN,
667 0x80, 0x80);
668 usleep_range(700, 710);
669 snd_soc_component_update_bits(component,
670 WSA881X_SPKR_PWRSTG_DBG,
671 0x00, 0x00);
672 snd_soc_component_update_bits(component,
673 WSA881X_ADC_EN_DET_TEST_V,
674 0xFF, 0x00);
675 snd_soc_component_update_bits(component,
676 WSA881X_ADC_EN_MODU_V,
677 0x02, 0x00);
678 snd_soc_component_update_bits(component,
679 WSA881X_ADC_EN_DET_TEST_I,
680 0xFF, 0x00);
681 } else
682 snd_soc_component_update_bits(component,
683 WSA881X_SPKR_DRV_EN, 0x80, 0x80);
684 /* add 1000us delay as per qcrg */
685 usleep_range(1000, 1010);
686 snd_soc_component_update_bits(component,
687 WSA881X_SPKR_DRV_EN, 0x01, 0x01);
688 if (WSA881X_IS_2_0(wsa881x->version))
689 snd_soc_component_update_bits(component,
690 WSA881X_SPKR_BIAS_CAL,
691 0x01, 0x00);
692 usleep_range(1000, 1010);
693 snd_soc_component_update_bits(component,
694 WSA881X_SPKR_DRV_GAIN,
695 0xF0, (wsa881x->spk_pa_gain << 4));
696 if (wsa881x->visense_enable) {
697 ret = msm_cdc_pinctrl_select_active_state(
698 wsa881x->wsa_vi_gpio_p);
699 if (ret) {
700 pr_err("%s: gpio set cannot be activated %s\n",
701 __func__, "wsa_vi");
702 return ret;
703 }
704 wsa881x_visense_txfe_ctrl(component, true,
705 0x00, 0x01, 0x00);
706 wsa881x_visense_adc_ctrl(component, true);
707 }
708 } else {
709 /*
710 * Ensure: Boost is still on, Stream from Analog input and
711 * Speaker Protection has been stopped and input is at 0V
712 */
713 if (WSA881X_IS_2_0(wsa881x->version)) {
714 snd_soc_component_update_bits(component,
715 WSA881X_SPKR_BIAS_CAL,
716 0x01, 0x01);
717 usleep_range(1000, 1010);
718 snd_soc_component_update_bits(component,
719 WSA881X_SPKR_BIAS_CAL,
720 0x01, 0x00);
721 msleep(20);
722 snd_soc_component_update_bits(component,
723 WSA881X_ANA_CTL, 0x03, 0x00);
724 usleep_range(200, 210);
725 }
726 snd_soc_component_update_bits(component,
727 WSA881X_SPKR_DRV_EN, 0x80, 0x00);
728 }
729 return 0;
730}
731
732static int wsa881x_get_boost(struct snd_kcontrol *kcontrol,
733 struct snd_ctl_elem_value *ucontrol)
734{
735
736 struct snd_soc_component *component =
737 snd_soc_kcontrol_component(kcontrol);
738 struct wsa881x_pdata *wsa881x =
739 snd_soc_component_get_drvdata(component);
740
741 ucontrol->value.integer.value[0] = wsa881x->boost_enable;
742 return 0;
743}
744
745static int wsa881x_set_boost(struct snd_kcontrol *kcontrol,
746 struct snd_ctl_elem_value *ucontrol)
747{
748 struct snd_soc_component *component =
749 snd_soc_kcontrol_component(kcontrol);
750 struct wsa881x_pdata *wsa881x =
751 snd_soc_component_get_drvdata(component);
752 int value = ucontrol->value.integer.value[0];
753
754 dev_dbg(component->dev, "%s: Boost enable current %d, new %d\n",
755 __func__, wsa881x->boost_enable, value);
756 wsa881x->boost_enable = value;
757 return 0;
758}
759
760static int wsa881x_get_visense(struct snd_kcontrol *kcontrol,
761 struct snd_ctl_elem_value *ucontrol)
762{
763
764 struct snd_soc_component *component =
765 snd_soc_kcontrol_component(kcontrol);
766 struct wsa881x_pdata *wsa881x =
767 snd_soc_component_get_drvdata(component);
768
769 ucontrol->value.integer.value[0] = wsa881x->visense_enable;
770 return 0;
771}
772
773static int wsa881x_set_visense(struct snd_kcontrol *kcontrol,
774 struct snd_ctl_elem_value *ucontrol)
775{
776 struct snd_soc_component *component =
777 snd_soc_kcontrol_component(kcontrol);
778 struct wsa881x_pdata *wsa881x =
779 snd_soc_component_get_drvdata(component);
780 int value = ucontrol->value.integer.value[0];
781
782 dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
783 __func__, wsa881x->visense_enable, value);
784 wsa881x->visense_enable = value;
785 return 0;
786}
787
788static const struct snd_kcontrol_new wsa881x_snd_controls[] = {
789 SOC_SINGLE_EXT("BOOST Switch", SND_SOC_NOPM, 0, 1, 0,
790 wsa881x_get_boost, wsa881x_set_boost),
791
792 SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
793 wsa881x_get_visense, wsa881x_set_visense),
794
795 SOC_ENUM_EXT("WSA_SPK PA Gain", wsa881x_spk_pa_gain_enum[0],
796 wsa881x_spk_pa_gain_get, wsa881x_spk_pa_gain_put),
797};
798
799static const char * const rdac_text[] = {
800 "ZERO", "Switch",
801};
802
803static const struct soc_enum rdac_enum =
804 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(rdac_text), rdac_text);
805
806static const struct snd_kcontrol_new rdac_mux[] = {
807 SOC_DAPM_ENUM("RDAC", rdac_enum)
808};
809
810static int wsa881x_rdac_event(struct snd_soc_dapm_widget *w,
811 struct snd_kcontrol *kcontrol, int event)
812{
813 struct snd_soc_component *component =
814 snd_soc_dapm_to_component(w->dapm);
815 struct wsa881x_pdata *wsa881x =
816 snd_soc_component_get_drvdata(component);
817 int ret = 0;
818
819 dev_dbg(component->dev, "%s: %s %d boost %d visense %d\n",
820 __func__, w->name, event,
821 wsa881x->boost_enable, wsa881x->visense_enable);
822
823 switch (event) {
824 case SND_SOC_DAPM_PRE_PMU:
825 ret = wsa881x_startup(wsa881x);
826 if (ret) {
827 pr_err("%s: wsa startup failed ret: %d", __func__, ret);
828 return ret;
829 }
830 wsa881x_clk_ctrl(component, true);
831 snd_soc_component_update_bits(component,
832 WSA881X_SPKR_DAC_CTL, 0x02, 0x02);
833 if (!WSA881X_IS_2_0(wsa881x->version))
834 snd_soc_component_update_bits(component,
835 WSA881X_BIAS_REF_CTRL,
836 0x0F, 0x08);
837 wsa881x_bandgap_ctrl(component, true);
838 if (!WSA881X_IS_2_0(wsa881x->version))
839 snd_soc_component_update_bits(component,
840 WSA881X_SPKR_BBM_CTL,
841 0x02, 0x02);
842 snd_soc_component_update_bits(component,
843 WSA881X_SPKR_MISC_CTL1, 0xC0, 0x80);
844 snd_soc_component_update_bits(component,
845 WSA881X_SPKR_MISC_CTL1, 0x06, 0x06);
846 if (!WSA881X_IS_2_0(wsa881x->version)) {
847 snd_soc_component_update_bits(component,
848 WSA881X_SPKR_MISC_CTL2,
849 0x04, 0x04);
850 snd_soc_component_update_bits(component,
851 WSA881X_SPKR_BIAS_INT,
852 0x09, 0x09);
853 }
854 snd_soc_component_update_bits(component,
855 WSA881X_SPKR_PA_INT, 0xF0, 0x20);
856 if (WSA881X_IS_2_0(wsa881x->version))
857 snd_soc_component_update_bits(component,
858 WSA881X_SPKR_PA_INT,
859 0x0E, 0x0E);
860 if (wsa881x->boost_enable)
861 wsa881x_boost_ctrl(component, true);
862 break;
863 case SND_SOC_DAPM_POST_PMU:
864 wsa881x_rdac_ctrl(component, true);
865 break;
866 case SND_SOC_DAPM_PRE_PMD:
867 wsa881x_rdac_ctrl(component, false);
868 if (wsa881x->visense_enable) {
869 wsa881x_visense_adc_ctrl(component, false);
870 wsa881x_visense_txfe_ctrl(component, false,
871 0x00, 0x01, 0x00);
872 ret = msm_cdc_pinctrl_select_sleep_state(
873 wsa881x->wsa_vi_gpio_p);
874 if (ret) {
875 pr_err("%s: gpio set cannot be suspended %s\n",
876 __func__, "wsa_vi");
877 return ret;
878 }
879 }
880 break;
881 case SND_SOC_DAPM_POST_PMD:
882 if (wsa881x->boost_enable)
883 wsa881x_boost_ctrl(component, false);
884 wsa881x_clk_ctrl(component, false);
885 wsa881x_bandgap_ctrl(component, false);
886 ret = wsa881x_shutdown(wsa881x);
887 if (ret < 0) {
888 pr_err("%s: wsa shutdown failed ret: %d",
889 __func__, ret);
890 return ret;
891 }
892 break;
893 default:
894 pr_err("%s: invalid event:%d\n", __func__, event);
895 return -EINVAL;
896 }
897 return 0;
898}
899
900static void wsa881x_ocp_ctl_work(struct work_struct *work)
901{
902 struct wsa881x_pdata *wsa881x;
903 struct delayed_work *dwork;
904 struct snd_soc_component *component;
905 int temp_val;
906
907 dwork = to_delayed_work(work);
908 wsa881x = container_of(dwork, struct wsa881x_pdata, ocp_ctl_work);
909
910 if (!wsa881x)
911 return;
912
913 component = wsa881x->component;
914 wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
915 dev_dbg(component->dev, " temp = %d\n", temp_val);
916
917 if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
918 snd_soc_component_update_bits(component,
919 WSA881X_SPKR_OCP_CTL, 0xC0, 0x00);
920 else
921 snd_soc_component_update_bits(component,
922 WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
923
924 schedule_delayed_work(&wsa881x->ocp_ctl_work,
925 msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
926}
927
928static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
929 struct snd_kcontrol *kcontrol, int event)
930{
931 struct snd_soc_component *component =
932 snd_soc_dapm_to_component(w->dapm);
933 struct wsa881x_pdata *wsa881x =
934 snd_soc_component_get_drvdata(component);
935
936 pr_debug("%s: %s %d\n", __func__, w->name, event);
937
938 switch (event) {
939 case SND_SOC_DAPM_PRE_PMU:
940 snd_soc_component_update_bits(component,
941 WSA881X_SPKR_OCP_CTL, 0xC0, 0x80);
942 break;
943 case SND_SOC_DAPM_POST_PMU:
944 wsa881x_spkr_pa_ctrl(component, true);
945 schedule_delayed_work(&wsa881x->ocp_ctl_work,
946 msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
947 break;
948 case SND_SOC_DAPM_PRE_PMD:
949 wsa881x_spkr_pa_ctrl(component, false);
950 break;
951 case SND_SOC_DAPM_POST_PMD:
952 cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
953 snd_soc_component_update_bits(component,
954 WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
955 break;
956 default:
957 pr_err("%s: invalid event:%d\n", __func__, event);
958 return -EINVAL;
959 }
960 return 0;
961}
962
963static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
964 SND_SOC_DAPM_INPUT("WSA_IN"),
965
966 SND_SOC_DAPM_DAC_E("RDAC Analog", NULL, SND_SOC_NOPM, 0, 0,
967 wsa881x_rdac_event,
968 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
969 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
970
971 SND_SOC_DAPM_MUX("WSA_RDAC", SND_SOC_NOPM, 0, 0,
972 rdac_mux),
973
974 SND_SOC_DAPM_PGA_S("WSA_SPKR PGA", 1, SND_SOC_NOPM, 0, 0,
975 wsa881x_spkr_pa_event,
976 SND_SOC_DAPM_PRE_PMU |
977 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD |
978 SND_SOC_DAPM_POST_PMD),
979
980 SND_SOC_DAPM_OUTPUT("WSA_SPKR"),
981};
982
983static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
984 {"WSA_RDAC", "Switch", "WSA_IN"},
985 {"RDAC Analog", NULL, "WSA_RDAC"},
986 {"WSA_SPKR PGA", NULL, "RDAC Analog"},
987 {"WSA_SPKR", NULL, "WSA_SPKR PGA"},
988};
989
990
991static int wsa881x_startup(struct wsa881x_pdata *pdata)
992{
993 int ret = 0;
994
995 pr_debug("%s(): wsa startup, enable_cnt:%d\n", __func__,
996 pdata->enable_cnt);
997
998 if (pdata->enable_cnt++ > 0)
999 return 0;
1000 ret = msm_cdc_pinctrl_select_active_state(pdata->wsa_clk_gpio_p);
1001 if (ret) {
1002 pr_err("%s: gpio set cannot be activated %s\n",
1003 __func__, "wsa_clk");
1004 return ret;
1005 }
1006 ret = clk_prepare_enable(pdata->wsa_mclk);
1007 if (ret) {
1008 pr_err("%s: WSA MCLK enable failed\n",
1009 __func__);
1010 return ret;
1011 }
1012 ret = wsa881x_reset(pdata, true);
1013 return ret;
1014}
1015
1016static int wsa881x_shutdown(struct wsa881x_pdata *pdata)
1017{
1018 int ret = 0;
1019
1020 pr_debug("%s(): wsa shutdown, enable_cnt:%d\n", __func__,
1021 pdata->enable_cnt);
1022 if (--pdata->enable_cnt > 0)
1023 return 0;
1024 ret = wsa881x_reset(pdata, false);
1025 if (ret) {
1026 pr_err("%s: wsa reset failed suspend %d\n",
1027 __func__, ret);
1028 return ret;
1029 }
1030
Laxminath Kasam0bb3a152020-02-03 12:47:48 +05301031 if (__clk_is_enabled(pdata->wsa_mclk))
1032 clk_disable_unprepare(pdata->wsa_mclk);
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05301033
1034 ret = msm_cdc_pinctrl_select_sleep_state(pdata->wsa_clk_gpio_p);
1035 if (ret) {
1036 pr_err("%s: gpio set cannot be suspended %s\n",
1037 __func__, "wsa_clk");
1038 return ret;
1039 }
1040
1041 return 0;
1042}
1043
1044static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
1045 bool enable)
1046{
1047 int ret = 0;
1048 struct wsa881x_pdata *wsa881x =
1049 snd_soc_component_get_drvdata(component);
1050
1051 if (enable) {
1052 ret = wsa881x_startup(wsa881x);
1053 if (ret < 0) {
1054 dev_err_ratelimited(component->dev,
1055 "%s: failed to startup\n", __func__);
1056 return ret;
1057 }
1058 }
1059 wsa881x_clk_ctrl(component, enable);
1060 wsa881x_bandgap_ctrl(component, enable);
1061 if (!enable) {
1062 ret = wsa881x_shutdown(wsa881x);
1063 if (ret < 0)
1064 dev_err_ratelimited(component->dev,
1065 "%s: failed to shutdown\n", __func__);
1066 }
1067 return ret;
1068}
1069
1070static int32_t wsa881x_temp_reg_read(struct snd_soc_component *component,
1071 struct wsa_temp_register *wsa_temp_reg)
1072{
1073 struct wsa881x_pdata *wsa881x =
1074 snd_soc_component_get_drvdata(component);
1075 int ret = 0;
1076
1077 if (!wsa881x) {
1078 dev_err(component->dev, "%s: wsa881x is NULL\n", __func__);
1079 return -EINVAL;
1080 }
1081 ret = wsa881x_resource_acquire(component, true);
1082 if (ret) {
1083 dev_err_ratelimited(component->dev,
1084 "%s: resource acquire fail\n", __func__);
1085 return ret;
1086 }
1087
1088 if (WSA881X_IS_2_0(wsa881x->version)) {
1089 snd_soc_component_update_bits(component,
1090 WSA881X_TADC_VALUE_CTL, 0x01, 0x00);
1091 wsa_temp_reg->dmeas_msb =
1092 snd_soc_component_read32(component,
1093 WSA881X_TEMP_MSB);
1094 wsa_temp_reg->dmeas_lsb =
1095 snd_soc_component_read32(component,
1096 WSA881X_TEMP_LSB);
1097 snd_soc_component_update_bits(component,
1098 WSA881X_TADC_VALUE_CTL, 0x01, 0x01);
1099 } else {
1100 wsa_temp_reg->dmeas_msb = snd_soc_component_read32(component,
1101 WSA881X_TEMP_DOUT_MSB);
1102 wsa_temp_reg->dmeas_lsb = snd_soc_component_read32(component,
1103 WSA881X_TEMP_DOUT_LSB);
1104 }
1105 wsa_temp_reg->d1_msb = snd_soc_component_read32(component,
1106 WSA881X_OTP_REG_1);
1107 wsa_temp_reg->d1_lsb = snd_soc_component_read32(component,
1108 WSA881X_OTP_REG_2);
1109 wsa_temp_reg->d2_msb = snd_soc_component_read32(component,
1110 WSA881X_OTP_REG_3);
1111 wsa_temp_reg->d2_lsb = snd_soc_component_read32(component,
1112 WSA881X_OTP_REG_4);
1113
1114 ret = wsa881x_resource_acquire(component, false);
1115 if (ret)
1116 dev_err_ratelimited(component->dev,
1117 "%s: resource release fail\n", __func__);
1118
1119 return ret;
1120}
1121
1122static int wsa881x_probe(struct snd_soc_component *component)
1123{
1124 struct i2c_client *client;
1125 int ret = 0;
1126 int wsa881x_index = 0;
1127 struct snd_soc_dapm_context *dapm =
1128 snd_soc_component_get_dapm(component);
1129 char *widget_name = NULL;
1130 struct snd_soc_card *card = component->card;
1131 struct snd_soc_codec_conf *codec_conf = card->codec_conf;
1132
1133 client = dev_get_drvdata(component->dev);
1134 ret = wsa881x_i2c_get_client_index(client, &wsa881x_index);
1135 if (ret != 0) {
1136 dev_err(&client->dev, "%s: I2C get codec I2C\n"
1137 "client failed\n", __func__);
1138 return ret;
1139 }
1140 mutex_init(&wsa_pdata[wsa881x_index].bg_lock);
1141 mutex_init(&wsa_pdata[wsa881x_index].res_lock);
1142 snprintf(wsa_pdata[wsa881x_index].tz_pdata.name, 100, "%s",
1143 wsa_tz_names[wsa881x_index]);
1144 wsa_pdata[wsa881x_index].component = component;
1145 wsa_pdata[wsa881x_index].spk_pa_gain = SPK_GAIN_12DB;
1146 wsa_pdata[wsa881x_index].component = component;
1147 wsa_pdata[wsa881x_index].tz_pdata.component = component;
1148 wsa_pdata[wsa881x_index].tz_pdata.wsa_temp_reg_read =
1149 wsa881x_temp_reg_read;
1150 snd_soc_component_set_drvdata(component, &wsa_pdata[wsa881x_index]);
1151 wsa881x_init_thermal(&wsa_pdata[wsa881x_index].tz_pdata);
1152 INIT_DELAYED_WORK(&wsa_pdata[wsa881x_index].ocp_ctl_work,
1153 wsa881x_ocp_ctl_work);
1154
1155 if (codec_conf->name_prefix) {
1156 widget_name = kcalloc(WIDGET_NAME_MAX_SIZE, sizeof(char),
1157 GFP_KERNEL);
1158 if (!widget_name)
1159 return -ENOMEM;
1160
1161 snprintf(widget_name, WIDGET_NAME_MAX_SIZE,
1162 "%s WSA_SPKR", codec_conf->name_prefix);
1163 snd_soc_dapm_ignore_suspend(dapm, widget_name);
1164 snprintf(widget_name, WIDGET_NAME_MAX_SIZE,
1165 "%s WSA_IN", codec_conf->name_prefix);
1166 snd_soc_dapm_ignore_suspend(dapm, widget_name);
1167 kfree(widget_name);
1168 } else {
1169 snd_soc_dapm_ignore_suspend(dapm, "WSA_SPKR");
1170 snd_soc_dapm_ignore_suspend(dapm, "WSA_IN");
1171 }
1172
1173 snd_soc_dapm_sync(dapm);
1174
1175 return 0;
1176}
1177
1178static void wsa881x_remove(struct snd_soc_component *component)
1179{
1180 struct wsa881x_pdata *wsa881x =
1181 snd_soc_component_get_drvdata(component);
1182
1183 if (wsa881x->tz_pdata.tz_dev)
1184 wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
1185
1186 mutex_destroy(&wsa881x->bg_lock);
1187 mutex_destroy(&wsa881x->res_lock);
1188}
1189
1190static const struct snd_soc_component_driver soc_component_dev_wsa881x = {
1191 .probe = wsa881x_probe,
1192 .remove = wsa881x_remove,
1193
1194 .read = wsa881x_i2c_read,
1195 .write = wsa881x_i2c_write,
1196
1197 .controls = wsa881x_snd_controls,
1198 .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
1199 .dapm_widgets = wsa881x_dapm_widgets,
1200 .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
1201 .dapm_routes = wsa881x_audio_map,
1202 .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
1203};
1204
1205static int wsa881x_reset(struct wsa881x_pdata *pdata, bool enable)
1206{
1207 int ret = 0;
1208
1209 /*
1210 * shutdown the GPIOs WSA_EN, WSA_MCLK, regulators
1211 * and restore defaults in soc cache when shutdown.
1212 * Enable regulators, GPIOs WSA_MCLK, WSA_EN when powerup.
1213 */
1214 if (enable) {
1215 if (pdata->wsa_active)
1216 return 0;
1217 ret = msm_cdc_pinctrl_select_active_state(
1218 pdata->wsa_reset_gpio_p);
1219 if (ret) {
1220 pr_err("%s: gpio set cannot be activated %s\n",
1221 __func__, "wsa_reset");
1222 return ret;
1223 }
1224 ret = msm_cdc_pinctrl_select_sleep_state(
1225 pdata->wsa_reset_gpio_p);
1226 if (ret) {
1227 pr_err("%s: gpio set cannot be suspended(powerup) %s\n",
1228 __func__, "wsa_reset");
1229 return ret;
1230 }
1231 ret = msm_cdc_pinctrl_select_active_state(
1232 pdata->wsa_reset_gpio_p);
1233 if (ret) {
1234 pr_err("%s: gpio set cannot be activated %s\n",
1235 __func__, "wsa_reset");
1236 return ret;
1237 }
1238 pdata->wsa_active = true;
1239 } else {
1240 if (!pdata->wsa_active)
1241 return 0;
1242 ret = msm_cdc_pinctrl_select_sleep_state(
1243 pdata->wsa_reset_gpio_p);
1244 if (ret) {
1245 pr_err("%s: gpio set cannot be suspended %s\n",
1246 __func__, "wsa_reset");
1247 return ret;
1248 }
1249 pdata->wsa_active = false;
1250 }
1251 return ret;
1252}
1253
1254int wsa881x_get_client_index(void)
1255{
1256 return wsa881x_i2c_addr;
1257}
1258EXPORT_SYMBOL(wsa881x_get_client_index);
1259
1260int wsa881x_get_probing_count(void)
1261{
1262 return wsa881x_probing_count;
1263}
1264EXPORT_SYMBOL(wsa881x_get_probing_count);
1265
1266int wsa881x_get_presence_count(void)
1267{
1268 return wsa881x_presence_count;
1269}
1270EXPORT_SYMBOL(wsa881x_get_presence_count);
1271
1272static int check_wsa881x_presence(struct i2c_client *client)
1273{
1274 int ret = 0;
1275 int wsa881x_index = 0;
1276
1277 ret = wsa881x_i2c_get_client_index(client, &wsa881x_index);
1278 if (ret != 0) {
1279 dev_err(&client->dev, "%s: I2C get codec I2C\n"
1280 "client failed\n", __func__);
1281 return ret;
1282 }
1283 ret = wsa881x_i2c_read_device(&wsa_pdata[wsa881x_index],
1284 WSA881X_CDC_RST_CTL);
1285 if (ret < 0) {
1286 dev_err(&client->dev, "failed to read wsa881x with addr %x\n",
1287 client->addr);
1288 return ret;
1289 }
1290 ret = wsa881x_i2c_write_device(&wsa_pdata[wsa881x_index],
1291 WSA881X_CDC_RST_CTL, 0x01);
1292 if (ret < 0) {
1293 dev_err(&client->dev, "failed write addr %x reg:0x5 val:0x1\n",
1294 client->addr);
1295 return ret;
1296 }
1297 /* allow 20ms before trigger next write to verify wsa881x presence */
1298 msleep(20);
1299 ret = wsa881x_i2c_write_device(&wsa_pdata[wsa881x_index],
1300 WSA881X_CDC_RST_CTL, 0x00);
1301 if (ret < 0) {
1302 dev_err(&client->dev, "failed write addr %x reg:0x5 val:0x0\n",
1303 client->addr);
1304 return ret;
1305 }
1306 return ret;
1307}
1308
1309static int wsa881x_populate_dt_pdata(struct device *dev, int wsa881x_index)
1310{
1311 int ret = 0;
1312 struct wsa881x_pdata *pdata = &wsa_pdata[wsa881x_index];
1313
1314 /* reading the gpio configurations from dtsi file */
1315 pdata->wsa_vi_gpio_p = of_parse_phandle(dev->of_node,
1316 "qcom,wsa-analog-vi-gpio", 0);
1317 pdata->wsa_clk_gpio_p = of_parse_phandle(dev->of_node,
1318 "qcom,wsa-analog-clk-gpio", 0);
1319 pdata->wsa_reset_gpio_p = of_parse_phandle(dev->of_node,
1320 "qcom,wsa-analog-reset-gpio", 0);
1321 pinctrl_init = true;
1322 return ret;
1323}
1324
1325static int wsa881x_i2c_probe(struct i2c_client *client,
1326 const struct i2c_device_id *id)
1327{
1328 int ret = 0;
1329 int wsa881x_index = 0;
1330 struct wsa881x_pdata *pdata = NULL;
1331 struct clk *wsa_mclk = NULL;
1332
1333 ret = wsa881x_i2c_get_client_index(client, &wsa881x_index);
1334 if (ret != 0) {
1335 dev_err(&client->dev, "%s: I2C get codec I2C\n"
1336 "client failed\n", __func__);
1337 return ret;
1338 }
1339
1340 pdata = &wsa_pdata[wsa881x_index];
1341 if ((client->addr == WSA881X_I2C_SPK0_SLAVE1_ADDR ||
1342 client->addr == WSA881X_I2C_SPK1_SLAVE1_ADDR) &&
1343 (pdata->status == WSA881X_STATUS_PROBING)) {
1344 wsa881x_probing_count++;
Laxminath Kasam2b85d8e2019-10-16 11:18:49 +05301345 return -EPROBE_DEFER;
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05301346 }
1347
1348 if (pdata->status == WSA881X_STATUS_I2C) {
1349 dev_dbg(&client->dev, "%s:probe for other slaves\n"
1350 "devices of codec I2C slave Addr = %x\n",
1351 __func__, client->addr);
1352 dev_dbg(&client->dev, "%s:wsa_idx = %d SLAVE = %d\n",
1353 __func__, wsa881x_index, WSA881X_ANALOG_SLAVE);
1354 pdata->regmap[WSA881X_ANALOG_SLAVE] =
1355 devm_regmap_init_i2c(
1356 client,
1357 &wsa881x_ana_regmap_config[WSA881X_ANALOG_SLAVE]);
1358 regcache_cache_bypass(pdata->regmap[WSA881X_ANALOG_SLAVE],
1359 true);
1360 if (IS_ERR(pdata->regmap[WSA881X_ANALOG_SLAVE])) {
1361 ret = PTR_ERR(pdata->regmap[WSA881X_ANALOG_SLAVE]);
1362 dev_err(&client->dev,
1363 "%s: regmap_init failed %d\n",
1364 __func__, ret);
1365 }
1366 client->dev.platform_data = pdata;
1367 i2c_set_clientdata(client, pdata);
1368 pdata->client[WSA881X_ANALOG_SLAVE] = client;
1369 if (pdata->version == WSA881X_2_0)
1370 wsa881x_update_regmap_2_0(
1371 pdata->regmap[WSA881X_ANALOG_SLAVE],
1372 WSA881X_ANALOG_SLAVE);
1373
1374 wsa881x_probing_count++;
1375 return ret;
1376 } else if (pdata->status == WSA881X_STATUS_PROBING) {
1377 pdata->index = wsa881x_index;
1378 if (client->dev.of_node) {
1379 dev_dbg(&client->dev, "%s:Platform data\n"
1380 "from device tree\n", __func__);
1381 ret = wsa881x_populate_dt_pdata(
1382 &client->dev, wsa881x_index);
1383 if (ret < 0) {
1384 dev_err(&client->dev,
1385 "%s: Fail to obtain pdata from device tree\n",
1386 __func__);
1387 ret = -EINVAL;
1388 goto err;
1389 }
1390 client->dev.platform_data = pdata;
1391 } else {
1392 dev_dbg(&client->dev, "%s:Platform data from\n"
1393 "board file\n", __func__);
1394 pdata = client->dev.platform_data;
1395 }
1396 if (!pdata) {
1397 dev_dbg(&client->dev, "no platform data?\n");
1398 ret = -EINVAL;
1399 goto err;
1400 }
Laxminath Kasam2b85d8e2019-10-16 11:18:49 +05301401 wsa_mclk = devm_clk_get(&client->dev, "wsa_mclk");
1402 if (IS_ERR(wsa_mclk)) {
1403 ret = PTR_ERR(wsa_mclk);
1404 dev_dbg(&client->dev, "%s: clk get %s failed %d\n",
1405 __func__, "wsa_mclk", ret);
1406 wsa_mclk = NULL;
1407 goto err;
1408 }
1409 pdata->wsa_mclk = wsa_mclk;
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05301410 dev_set_drvdata(&client->dev, client);
1411
1412 pdata->regmap[WSA881X_DIGITAL_SLAVE] =
1413 devm_regmap_init_i2c(
1414 client,
1415 &wsa881x_ana_regmap_config[WSA881X_DIGITAL_SLAVE]);
1416 regcache_cache_bypass(pdata->regmap[WSA881X_DIGITAL_SLAVE],
1417 true);
1418 if (IS_ERR(pdata->regmap[WSA881X_DIGITAL_SLAVE])) {
1419 ret = PTR_ERR(pdata->regmap[WSA881X_DIGITAL_SLAVE]);
1420 dev_err(&client->dev, "%s: regmap_init failed %d\n",
1421 __func__, ret);
1422 goto err;
1423 }
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05301424
1425 /* bus reset sequence */
1426 ret = wsa881x_reset(pdata, true);
1427 if (ret < 0) {
1428 wsa881x_probing_count++;
1429 dev_err(&client->dev, "%s: WSA enable Failed %d\n",
1430 __func__, ret);
1431 goto err;
1432 }
1433 pdata->client[WSA881X_DIGITAL_SLAVE] = client;
1434 pdata->regmap_flag = true;
1435 ret = check_wsa881x_presence(client);
1436 if (ret < 0) {
1437 dev_err(&client->dev,
1438 "failed to ping wsa with addr:%x, ret = %d\n",
1439 client->addr, ret);
1440 wsa881x_probing_count++;
1441 goto err1;
1442 }
1443 pdata->version = wsa881x_i2c_read_device(pdata,
1444 WSA881X_CHIP_ID1);
1445 pr_debug("%s: wsa881x version: %d\n", __func__, pdata->version);
1446 if (pdata->version == WSA881X_2_0) {
1447 wsa881x_update_reg_defaults_2_0();
1448 wsa881x_update_regmap_2_0(
1449 pdata->regmap[WSA881X_DIGITAL_SLAVE],
1450 WSA881X_DIGITAL_SLAVE);
1451 }
1452 wsa881x_presence_count++;
1453 wsa881x_probing_count++;
1454 ret = snd_soc_register_component(&client->dev,
1455 &soc_component_dev_wsa881x,
1456 NULL, 0);
1457 if (ret < 0)
1458 goto err1;
1459 pdata->status = WSA881X_STATUS_I2C;
1460 }
1461err1:
1462 wsa881x_reset(pdata, false);
1463err:
Laxminath Kasam2b85d8e2019-10-16 11:18:49 +05301464 return ret;
Laxminath Kasam9e78ef82019-07-01 12:31:45 +05301465}
1466
1467static int wsa881x_i2c_remove(struct i2c_client *client)
1468{
1469 struct wsa881x_pdata *wsa881x = client->dev.platform_data;
1470
1471 snd_soc_unregister_component(&client->dev);
1472 i2c_set_clientdata(client, NULL);
1473 kfree(wsa881x);
1474 return 0;
1475}
1476
1477#ifdef CONFIG_PM_SLEEP
1478static int wsa881x_i2c_suspend(struct device *dev)
1479{
1480 pr_debug("%s: system suspend\n", __func__);
1481 return 0;
1482}
1483
1484static int wsa881x_i2c_resume(struct device *dev)
1485{
1486 pr_debug("%s: system resume\n", __func__);
1487 return 0;
1488}
1489
1490static const struct dev_pm_ops wsa881x_i2c_pm_ops = {
1491 SET_SYSTEM_SLEEP_PM_OPS(wsa881x_i2c_suspend, wsa881x_i2c_resume)
1492};
1493#endif /* CONFIG_PM_SLEEP */
1494
1495static const struct i2c_device_id wsa881x_i2c_id[] = {
1496 {"wsa881x-i2c-dev", WSA881X_I2C_SPK0_SLAVE0_ADDR},
1497 {"wsa881x-i2c-dev", WSA881X_I2C_SPK0_SLAVE1_ADDR},
1498 {"wsa881x-i2c-dev", WSA881X_I2C_SPK1_SLAVE0_ADDR},
1499 {"wsa881x-i2c-dev", WSA881X_I2C_SPK1_SLAVE1_ADDR},
1500 {}
1501};
1502
1503MODULE_DEVICE_TABLE(i2c, wsa881x_i2c_id);
1504
1505
1506static const struct of_device_id msm_match_table[] = {
1507 {.compatible = "qcom,wsa881x-i2c-codec"},
1508 {}
1509};
1510MODULE_DEVICE_TABLE(of, msm_match_table);
1511
1512static struct i2c_driver wsa881x_codec_driver = {
1513 .driver = {
1514 .name = "wsa881x-i2c-codec",
1515 .owner = THIS_MODULE,
1516 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1517#ifdef CONFIG_PM_SLEEP
1518 .pm = &wsa881x_i2c_pm_ops,
1519#endif
1520 .of_match_table = msm_match_table,
1521 },
1522 .id_table = wsa881x_i2c_id,
1523 .probe = wsa881x_i2c_probe,
1524 .remove = wsa881x_i2c_remove,
1525};
1526
1527static int __init wsa881x_codec_init(void)
1528{
1529 int i = 0;
1530
1531 for (i = 0; i < MAX_WSA881X_DEVICE; i++)
1532 wsa_pdata[i].status = WSA881X_STATUS_PROBING;
1533 return i2c_add_driver(&wsa881x_codec_driver);
1534}
1535module_init(wsa881x_codec_init);
1536
1537static void __exit wsa881x_codec_exit(void)
1538{
1539 i2c_del_driver(&wsa881x_codec_driver);
1540}
1541
1542module_exit(wsa881x_codec_exit);
1543
1544MODULE_DESCRIPTION("WSA881x Codec driver");
1545MODULE_LICENSE("GPL v2");