blob: 4dceb0449c4ec36438b32d9656031e26f249851f [file] [log] [blame]
Meng Wang43bbb872018-12-10 12:32:05 +08001// SPDX-License-Identifier: GPL-2.0-only
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302/*
Ramprasad Katkame38aed42018-03-07 16:26:49 +05303 * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304 */
5
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/platform_device.h>
10#include <linux/device.h>
11#include <linux/printk.h>
12#include <linux/bitops.h>
13#include <linux/regulator/consumer.h>
14#include <linux/pm_runtime.h>
15#include <linux/delay.h>
16#include <linux/kernel.h>
17#include <linux/gpio.h>
18#include <linux/of_gpio.h>
19#include <linux/regmap.h>
20#include <linux/debugfs.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053021#include <soc/soundwire.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053022#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26#include <sound/tlv.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053027#include "msm-cdc-pinctrl.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053028#include "wsa881x.h"
29#include "wsa881x-temp-sensor.h"
30
Meng Wang15c825d2018-09-06 10:49:18 +080031#define DRV_NAME "wsa-codec"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053032#define WSA881X_NUM_RETRY 5
33
34enum {
35 G_18DB = 0,
36 G_16P5DB,
37 G_15DB,
38 G_13P5DB,
39 G_12DB,
40 G_10P5DB,
41 G_9DB,
42 G_7P5DB,
43 G_6DB,
44 G_4P5DB,
45 G_3DB,
46 G_1P5DB,
47 G_0DB,
48};
49
50enum {
51 DISABLE = 0,
52 ENABLE,
53};
54
55enum {
56 SWR_DAC_PORT,
57 SWR_COMP_PORT,
58 SWR_BOOST_PORT,
59 SWR_VISENSE_PORT,
60};
61
62struct swr_port {
63 u8 port_id;
64 u8 ch_mask;
65 u32 ch_rate;
66 u8 num_ch;
Ramprasad Katkame38aed42018-03-07 16:26:49 +053067 u8 port_type;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053068};
69
70enum {
71 WSA881X_DEV_DOWN,
72 WSA881X_DEV_UP,
Laxminath Kasamc0684fc2018-07-31 19:26:56 +053073 WSA881X_DEV_READY,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053074};
75
76/*
77 * Private data Structure for wsa881x. All parameters related to
78 * WSA881X codec needs to be defined here.
79 */
80struct wsa881x_priv {
81 struct regmap *regmap;
82 struct device *dev;
83 struct swr_device *swr_slave;
Meng Wang15c825d2018-09-06 10:49:18 +080084 struct snd_soc_component *component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053085 bool comp_enable;
86 bool boost_enable;
87 bool visense_enable;
88 u8 pa_gain;
89 struct swr_port port[WSA881X_MAX_SWR_PORTS];
90 int pd_gpio;
91 struct wsa881x_tz_priv tz_pdata;
92 int bg_cnt;
93 int clk_cnt;
94 int version;
95 struct mutex bg_lock;
96 struct mutex res_lock;
Laxminath Kasamc0684fc2018-07-31 19:26:56 +053097 struct mutex temp_lock;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053098 struct snd_info_entry *entry;
99 struct snd_info_entry *version_entry;
100 int state;
101 struct delayed_work ocp_ctl_work;
102 struct device_node *wsa_rst_np;
103 int pa_mute;
104};
105
106#define SWR_SLV_MAX_REG_ADDR 0x390
107#define SWR_SLV_START_REG_ADDR 0x40
108#define SWR_SLV_MAX_BUF_LEN 20
109#define BYTES_PER_LINE 12
110#define SWR_SLV_RD_BUF_LEN 8
111#define SWR_SLV_WR_BUF_LEN 32
112#define SWR_SLV_MAX_DEVICES 2
113
114#define WSA881X_VERSION_ENTRY_SIZE 27
115#define WSA881X_OCP_CTL_TIMER_SEC 2
116#define WSA881X_OCP_CTL_TEMP_CELSIUS 25
117#define WSA881X_OCP_CTL_POLL_TIMER_SEC 60
118
119static int wsa881x_ocp_poll_timer_sec = WSA881X_OCP_CTL_POLL_TIMER_SEC;
120module_param(wsa881x_ocp_poll_timer_sec, int, 0664);
121MODULE_PARM_DESC(wsa881x_ocp_poll_timer_sec, "timer for ocp ctl polling");
122
123static struct wsa881x_priv *dbgwsa881x;
124static struct dentry *debugfs_wsa881x_dent;
125static struct dentry *debugfs_peek;
126static struct dentry *debugfs_poke;
127static struct dentry *debugfs_reg_dump;
128static unsigned int read_data;
129static unsigned int devnum;
130
Meng Wang15c825d2018-09-06 10:49:18 +0800131static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530132 bool enable);
133
134static const char * const wsa_pa_gain_text[] = {
135 "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB", "G_12_DB", "G_10P5_DB",
136 "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
137 "G_0_DB"
138};
139
140static const struct soc_enum wsa_pa_gain_enum =
141 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
142
143static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
144 struct snd_ctl_elem_value *ucontrol)
145{
Meng Wang15c825d2018-09-06 10:49:18 +0800146 struct snd_soc_component *component =
147 snd_soc_kcontrol_component(kcontrol);
148 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530149
150 ucontrol->value.integer.value[0] = wsa881x->pa_gain;
151
Meng Wang15c825d2018-09-06 10:49:18 +0800152 dev_dbg(component->dev, "%s: PA gain = 0x%x\n", __func__,
153 wsa881x->pa_gain);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530154
155 return 0;
156}
157
158static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
160{
Meng Wang15c825d2018-09-06 10:49:18 +0800161 struct snd_soc_component *component =
162 snd_soc_kcontrol_component(kcontrol);
163 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530164
Meng Wang15c825d2018-09-06 10:49:18 +0800165 dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530166 __func__, ucontrol->value.integer.value[0]);
167
168 wsa881x->pa_gain = ucontrol->value.integer.value[0];
169
170 return 0;
171}
172
173static int wsa881x_get_mute(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176
Meng Wang15c825d2018-09-06 10:49:18 +0800177 struct snd_soc_component *component =
178 snd_soc_kcontrol_component(kcontrol);
179 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530180
181 ucontrol->value.integer.value[0] = wsa881x->pa_mute;
182
183 return 0;
184}
185
186static int wsa881x_set_mute(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
Meng Wang15c825d2018-09-06 10:49:18 +0800189 struct snd_soc_component *component =
190 snd_soc_kcontrol_component(kcontrol);
191 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530192 int value = ucontrol->value.integer.value[0];
193
Meng Wang15c825d2018-09-06 10:49:18 +0800194 dev_dbg(component->dev, "%s: mute current %d, new %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530195 __func__, wsa881x->pa_mute, value);
196
197 if (value)
Meng Wang15c825d2018-09-06 10:49:18 +0800198 snd_soc_component_update_bits(component, WSA881X_SPKR_DRV_EN,
199 0x80, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530200 wsa881x->pa_mute = value;
201
202 return 0;
203}
204
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530205static int wsa881x_get_t0_init(struct snd_kcontrol *kcontrol,
206 struct snd_ctl_elem_value *ucontrol)
207{
208
Meng Wang15c825d2018-09-06 10:49:18 +0800209 struct snd_soc_component *component =
210 snd_soc_kcontrol_component(kcontrol);
211 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530212 struct wsa881x_tz_priv *pdata = &wsa881x->tz_pdata;
213
214 ucontrol->value.integer.value[0] = pdata->t0_init;
Meng Wang15c825d2018-09-06 10:49:18 +0800215 dev_dbg(component->dev, "%s: t0 init %d\n", __func__, pdata->t0_init);
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530216
217 return 0;
218}
219
220static int wsa881x_set_t0_init(struct snd_kcontrol *kcontrol,
221 struct snd_ctl_elem_value *ucontrol)
222{
Meng Wang15c825d2018-09-06 10:49:18 +0800223 struct snd_soc_component *component =
224 snd_soc_kcontrol_component(kcontrol);
225 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530226 struct wsa881x_tz_priv *pdata = &wsa881x->tz_pdata;
227
228 pdata->t0_init = ucontrol->value.integer.value[0];
Meng Wang15c825d2018-09-06 10:49:18 +0800229 dev_dbg(component->dev, "%s: t0 init %d\n", __func__, pdata->t0_init);
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530230
231 return 0;
232}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530233
234static const struct snd_kcontrol_new wsa_snd_controls[] = {
235 SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
236 wsa_pa_gain_get, wsa_pa_gain_put),
237 SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
238 wsa881x_get_mute, wsa881x_set_mute),
Sudheer Papothic9dd3be2018-04-06 00:51:48 +0530239 SOC_SINGLE_EXT("WSA T0 Init", SND_SOC_NOPM, 0, 1, 0,
240 wsa881x_get_t0_init, wsa881x_set_t0_init),
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530241};
242
243static int codec_debug_open(struct inode *inode, struct file *file)
244{
245 file->private_data = inode->i_private;
246 return 0;
247}
248
249static int get_parameters(char *buf, u32 *param1, int num_of_par)
250{
251 char *token;
252 int base, cnt;
253
254 token = strsep(&buf, " ");
255 for (cnt = 0; cnt < num_of_par; cnt++) {
256 if (token) {
257 if ((token[1] == 'x') || (token[1] == 'X'))
258 base = 16;
259 else
260 base = 10;
261
262 if (kstrtou32(token, base, &param1[cnt]) != 0)
263 return -EINVAL;
264
265 token = strsep(&buf, " ");
266 } else
267 return -EINVAL;
268 }
269 return 0;
270}
271
272static ssize_t wsa881x_codec_version_read(struct snd_info_entry *entry,
273 void *file_private_data, struct file *file,
274 char __user *buf, size_t count, loff_t pos)
275{
276 struct wsa881x_priv *wsa881x;
277 char buffer[WSA881X_VERSION_ENTRY_SIZE];
278 int len;
279
280 wsa881x = (struct wsa881x_priv *) entry->private_data;
281 if (!wsa881x) {
282 pr_err("%s: wsa881x priv is null\n", __func__);
283 return -EINVAL;
284 }
285
286 len = snprintf(buffer, sizeof(buffer), "WSA881X-SOUNDWIRE_2_0\n");
287
288 return simple_read_from_buffer(buf, count, &pos, buffer, len);
289}
290
291static struct snd_info_entry_ops wsa881x_codec_info_ops = {
292 .read = wsa881x_codec_version_read,
293};
294
295/*
296 * wsa881x_codec_info_create_codec_entry - creates wsa881x module
297 * @codec_root: The parent directory
Meng Wang15c825d2018-09-06 10:49:18 +0800298 * @component: Codec instance
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530299 *
300 * Creates wsa881x module and version entry under the given
301 * parent directory.
302 *
303 * Return: 0 on success or negative error code on failure.
304 */
305int wsa881x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
Meng Wang15c825d2018-09-06 10:49:18 +0800306 struct snd_soc_component *component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530307{
308 struct snd_info_entry *version_entry;
309 struct wsa881x_priv *wsa881x;
310 struct snd_soc_card *card;
311 char name[80];
312
Meng Wang15c825d2018-09-06 10:49:18 +0800313 if (!codec_root || !component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530314 return -EINVAL;
315
Meng Wang15c825d2018-09-06 10:49:18 +0800316 wsa881x = snd_soc_component_get_drvdata(component);
317 card = component->card;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530318 snprintf(name, sizeof(name), "%s.%x", "wsa881x",
319 (u32)wsa881x->swr_slave->addr);
320
321 wsa881x->entry = snd_info_create_subdir(codec_root->module,
322 (const char *)name,
323 codec_root);
324 if (!wsa881x->entry) {
Meng Wang15c825d2018-09-06 10:49:18 +0800325 dev_dbg(component->dev, "%s: failed to create wsa881x entry\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530326 __func__);
327 return -ENOMEM;
328 }
329
330 version_entry = snd_info_create_card_entry(card->snd_card,
331 "version",
332 wsa881x->entry);
333 if (!version_entry) {
Meng Wang15c825d2018-09-06 10:49:18 +0800334 dev_dbg(component->dev, "%s: failed to create wsa881x version entry\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530335 __func__);
336 return -ENOMEM;
337 }
338
339 version_entry->private_data = wsa881x;
340 version_entry->size = WSA881X_VERSION_ENTRY_SIZE;
341 version_entry->content = SNDRV_INFO_CONTENT_DATA;
342 version_entry->c.ops = &wsa881x_codec_info_ops;
343
344 if (snd_info_register(version_entry) < 0) {
345 snd_info_free_entry(version_entry);
346 return -ENOMEM;
347 }
348 wsa881x->version_entry = version_entry;
349
350 return 0;
351}
352EXPORT_SYMBOL(wsa881x_codec_info_create_codec_entry);
353
354static bool is_swr_slv_reg_readable(int reg)
355{
356 bool ret = true;
357
358 if (((reg > 0x46) && (reg < 0x4A)) ||
359 ((reg > 0x4A) && (reg < 0x50)) ||
360 ((reg > 0x55) && (reg < 0xE0)) ||
361 ((reg > 0xE0) && (reg < 0xF0)) ||
362 ((reg > 0xF0) && (reg < 0x100)) ||
363 ((reg > 0x105) && (reg < 0x120)) ||
364 ((reg > 0x128) && (reg < 0x130)) ||
365 ((reg > 0x138) && (reg < 0x200)) ||
366 ((reg > 0x205) && (reg < 0x220)) ||
367 ((reg > 0x228) && (reg < 0x230)) ||
368 ((reg > 0x238) && (reg < 0x300)) ||
369 ((reg > 0x305) && (reg < 0x320)) ||
370 ((reg > 0x328) && (reg < 0x330)) ||
371 ((reg > 0x338) && (reg < 0x400)) ||
372 ((reg > 0x405) && (reg < 0x420)))
373 ret = false;
374
375 return ret;
376}
377
378static ssize_t wsa881x_swrslave_reg_show(char __user *ubuf, size_t count,
379 loff_t *ppos)
380{
381 int i, reg_val, len;
382 ssize_t total = 0;
383 char tmp_buf[SWR_SLV_MAX_BUF_LEN];
384
385 if (!ubuf || !ppos || (devnum == 0))
386 return 0;
387
388 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
389 i <= SWR_SLV_MAX_REG_ADDR; i++) {
390 if (!is_swr_slv_reg_readable(i))
391 continue;
392 swr_read(dbgwsa881x->swr_slave, devnum,
393 i, &reg_val, 1);
394 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i,
395 (reg_val & 0xFF));
396 if ((total + len) >= count - 1)
397 break;
398 if (copy_to_user((ubuf + total), tmp_buf, len)) {
399 pr_err("%s: fail to copy reg dump\n", __func__);
400 total = -EFAULT;
401 goto copy_err;
402 }
403 *ppos += len;
404 total += len;
405 }
406
407copy_err:
408 return total;
409}
410
411static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
412 size_t count, loff_t *ppos)
413{
414 char lbuf[SWR_SLV_RD_BUF_LEN];
415 char *access_str;
416 ssize_t ret_cnt;
417
418 if (!count || !file || !ppos || !ubuf)
419 return -EINVAL;
420
421 access_str = file->private_data;
422 if (*ppos < 0)
423 return -EINVAL;
424
425 if (!strcmp(access_str, "swrslave_peek")) {
426 snprintf(lbuf, sizeof(lbuf), "0x%x\n", (read_data & 0xFF));
427 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
428 strnlen(lbuf, 7));
429 } else if (!strcmp(access_str, "swrslave_reg_dump")) {
430 ret_cnt = wsa881x_swrslave_reg_show(ubuf, count, ppos);
431 } else {
432 pr_err("%s: %s not permitted to read\n", __func__, access_str);
433 ret_cnt = -EPERM;
434 }
435 return ret_cnt;
436}
437
438static ssize_t codec_debug_write(struct file *filp,
439 const char __user *ubuf, size_t cnt, loff_t *ppos)
440{
441 char lbuf[SWR_SLV_WR_BUF_LEN];
442 int rc;
443 u32 param[5];
444 char *access_str;
445
446 if (!filp || !ppos || !ubuf)
447 return -EINVAL;
448
449 access_str = filp->private_data;
450 if (cnt > sizeof(lbuf) - 1)
451 return -EINVAL;
452
453 rc = copy_from_user(lbuf, ubuf, cnt);
454 if (rc)
455 return -EFAULT;
456
457 lbuf[cnt] = '\0';
458 if (!strcmp(access_str, "swrslave_poke")) {
459 /* write */
460 rc = get_parameters(lbuf, param, 3);
461 if ((param[0] <= SWR_SLV_MAX_REG_ADDR) && (param[1] <= 0xFF) &&
462 (rc == 0))
463 swr_write(dbgwsa881x->swr_slave, param[2],
464 param[0], &param[1]);
465 else
466 rc = -EINVAL;
467 } else if (!strcmp(access_str, "swrslave_peek")) {
468 /* read */
469 rc = get_parameters(lbuf, param, 2);
470 if ((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0))
471 swr_read(dbgwsa881x->swr_slave, param[1],
472 param[0], &read_data, 1);
473 else
474 rc = -EINVAL;
475 } else if (!strcmp(access_str, "swrslave_reg_dump")) {
476 /* reg dump */
477 rc = get_parameters(lbuf, param, 1);
478 if ((rc == 0) && (param[0] > 0) &&
479 (param[0] <= SWR_SLV_MAX_DEVICES))
480 devnum = param[0];
481 else
482 rc = -EINVAL;
483 }
484 if (rc == 0)
485 rc = cnt;
486 else
487 pr_err("%s: rc = %d\n", __func__, rc);
488
489 return rc;
490}
491
492static const struct file_operations codec_debug_ops = {
493 .open = codec_debug_open,
494 .write = codec_debug_write,
495 .read = codec_debug_read,
496};
497
Laxminath Kasamc0684fc2018-07-31 19:26:56 +0530498static void wsa881x_regcache_sync(struct wsa881x_priv *wsa881x)
499{
500 mutex_lock(&wsa881x->res_lock);
501 if (wsa881x->state != WSA881X_DEV_READY) {
502 regcache_mark_dirty(wsa881x->regmap);
503 regcache_sync(wsa881x->regmap);
504 wsa881x->state = WSA881X_DEV_READY;
505 }
506 mutex_unlock(&wsa881x->res_lock);
507}
508
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530509static const struct reg_sequence wsa881x_pre_pmu_pa[] = {
510 {WSA881X_SPKR_DRV_GAIN, 0x41, 0},
511 {WSA881X_SPKR_MISC_CTL1, 0x01, 0},
512 {WSA881X_ADC_EN_DET_TEST_I, 0x01, 0},
513 {WSA881X_ADC_EN_MODU_V, 0x02, 0},
514 {WSA881X_ADC_EN_DET_TEST_V, 0x10, 0},
515 {WSA881X_SPKR_PWRSTG_DBG, 0xA0, 0},
516};
517
518static const struct reg_sequence wsa881x_pre_pmu_pa_2_0[] = {
519 {WSA881X_SPKR_DRV_GAIN, 0x41, 0},
520 {WSA881X_SPKR_MISC_CTL1, 0x87, 0},
521};
522
523static const struct reg_sequence wsa881x_post_pmu_pa[] = {
524 {WSA881X_SPKR_PWRSTG_DBG, 0x00, 0},
525 {WSA881X_ADC_EN_DET_TEST_V, 0x00, 0},
526 {WSA881X_ADC_EN_MODU_V, 0x00, 0},
527 {WSA881X_ADC_EN_DET_TEST_I, 0x00, 0},
528};
529
530static const struct reg_sequence wsa881x_vi_txfe_en[] = {
531 {WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x85, 0},
532 {WSA881X_SPKR_PROT_ATEST2, 0x0A, 0},
533 {WSA881X_SPKR_PROT_FE_GAIN, 0xCF, 0},
534};
535
536static const struct reg_sequence wsa881x_vi_txfe_en_2_0[] = {
537 {WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x85, 0},
538 {WSA881X_SPKR_PROT_ATEST2, 0x0A, 0},
539 {WSA881X_SPKR_PROT_FE_GAIN, 0x47, 0},
540};
541
Meng Wang15c825d2018-09-06 10:49:18 +0800542static int wsa881x_boost_ctrl(struct snd_soc_component *component, bool enable)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530543{
Meng Wang15c825d2018-09-06 10:49:18 +0800544 dev_dbg(component->dev, "%s: enable:%d\n", __func__, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530545 if (enable)
Meng Wang15c825d2018-09-06 10:49:18 +0800546 snd_soc_component_update_bits(component, WSA881X_BOOST_EN_CTL,
547 0x80, 0x80);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530548 else
Meng Wang15c825d2018-09-06 10:49:18 +0800549 snd_soc_component_update_bits(component, WSA881X_BOOST_EN_CTL,
550 0x80, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530551 /*
552 * 1.5ms sleep is needed after boost enable/disable as per
553 * HW requirement
554 */
555 usleep_range(1500, 1510);
556 return 0;
557}
558
Meng Wang15c825d2018-09-06 10:49:18 +0800559static int wsa881x_visense_txfe_ctrl(struct snd_soc_component *component,
560 bool enable, u8 isense1_gain,
561 u8 isense2_gain, u8 vsense_gain)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530562{
Meng Wang15c825d2018-09-06 10:49:18 +0800563 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530564
Meng Wang15c825d2018-09-06 10:49:18 +0800565 dev_dbg(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530566 "%s: enable:%d, isense1 gain: %d, isense2 gain: %d, vsense_gain %d\n",
567 __func__, enable, isense1_gain, isense2_gain, vsense_gain);
568
569 if (enable) {
570 regmap_multi_reg_write(wsa881x->regmap,
571 wsa881x_vi_txfe_en_2_0,
572 ARRAY_SIZE(wsa881x_vi_txfe_en_2_0));
573 } else {
Meng Wang15c825d2018-09-06 10:49:18 +0800574 snd_soc_component_update_bits(component,
575 WSA881X_SPKR_PROT_FE_VSENSE_VCM,
576 0x08, 0x08);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530577 /*
578 * 200us sleep is needed after visense txfe disable as per
579 * HW requirement.
580 */
581 usleep_range(200, 210);
Meng Wang15c825d2018-09-06 10:49:18 +0800582 snd_soc_component_update_bits(component,
583 WSA881X_SPKR_PROT_FE_GAIN,
584 0x01, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530585 }
586 return 0;
587}
588
Meng Wang15c825d2018-09-06 10:49:18 +0800589static int wsa881x_visense_adc_ctrl(struct snd_soc_component *component,
590 bool enable)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530591{
592
Meng Wang15c825d2018-09-06 10:49:18 +0800593 dev_dbg(component->dev, "%s: enable:%d\n", __func__, enable);
594 snd_soc_component_update_bits(component, WSA881X_ADC_EN_MODU_V,
595 (0x01 << 7), (enable << 7));
596 snd_soc_component_update_bits(component, WSA881X_ADC_EN_MODU_I,
597 (0x01 << 7), (enable << 7));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530598 return 0;
599}
600
Meng Wang15c825d2018-09-06 10:49:18 +0800601static void wsa881x_bandgap_ctrl(struct snd_soc_component *component,
602 bool enable)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530603{
Meng Wang15c825d2018-09-06 10:49:18 +0800604 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530605
Meng Wang15c825d2018-09-06 10:49:18 +0800606 dev_dbg(component->dev, "%s: enable:%d, bg_count:%d\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530607 enable, wsa881x->bg_cnt);
608 mutex_lock(&wsa881x->bg_lock);
609 if (enable) {
610 ++wsa881x->bg_cnt;
611 if (wsa881x->bg_cnt == 1) {
Meng Wang15c825d2018-09-06 10:49:18 +0800612 snd_soc_component_update_bits(component,
613 WSA881X_TEMP_OP,
614 0x08, 0x08);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530615 /* 400usec sleep is needed as per HW requirement */
616 usleep_range(400, 410);
Meng Wang15c825d2018-09-06 10:49:18 +0800617 snd_soc_component_update_bits(component,
618 WSA881X_TEMP_OP,
619 0x04, 0x04);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530620 }
621 } else {
622 --wsa881x->bg_cnt;
623 if (wsa881x->bg_cnt <= 0) {
624 WARN_ON(wsa881x->bg_cnt < 0);
625 wsa881x->bg_cnt = 0;
Meng Wang15c825d2018-09-06 10:49:18 +0800626 snd_soc_component_update_bits(component,
627 WSA881X_TEMP_OP, 0x04, 0x00);
628 snd_soc_component_update_bits(component,
629 WSA881X_TEMP_OP, 0x08, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530630 }
631 }
632 mutex_unlock(&wsa881x->bg_lock);
633}
634
Meng Wang15c825d2018-09-06 10:49:18 +0800635static void wsa881x_clk_ctrl(struct snd_soc_component *component, bool enable)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530636{
Meng Wang15c825d2018-09-06 10:49:18 +0800637 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530638
Meng Wang15c825d2018-09-06 10:49:18 +0800639 dev_dbg(component->dev, "%s: enable:%d, clk_count:%d\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530640 enable, wsa881x->clk_cnt);
641 mutex_lock(&wsa881x->res_lock);
642 if (enable) {
643 ++wsa881x->clk_cnt;
644 if (wsa881x->clk_cnt == 1) {
Meng Wang15c825d2018-09-06 10:49:18 +0800645 snd_soc_component_write(component,
646 WSA881X_CDC_DIG_CLK_CTL, 0x01);
647 snd_soc_component_write(component,
648 WSA881X_CDC_ANA_CLK_CTL, 0x01);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530649 }
650 } else {
651 --wsa881x->clk_cnt;
652 if (wsa881x->clk_cnt <= 0) {
653 WARN_ON(wsa881x->clk_cnt < 0);
654 wsa881x->clk_cnt = 0;
Meng Wang15c825d2018-09-06 10:49:18 +0800655 snd_soc_component_write(component,
656 WSA881X_CDC_DIG_CLK_CTL, 0x00);
657 snd_soc_component_write(component,
658 WSA881X_CDC_ANA_CLK_CTL, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530659 }
660 }
661 mutex_unlock(&wsa881x->res_lock);
662}
663
664static int wsa881x_get_compander(struct snd_kcontrol *kcontrol,
665 struct snd_ctl_elem_value *ucontrol)
666{
667
Meng Wang15c825d2018-09-06 10:49:18 +0800668 struct snd_soc_component *component =
669 snd_soc_kcontrol_component(kcontrol);
670 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530671
672 ucontrol->value.integer.value[0] = wsa881x->comp_enable;
673 return 0;
674}
675
676static int wsa881x_set_compander(struct snd_kcontrol *kcontrol,
677 struct snd_ctl_elem_value *ucontrol)
678{
Meng Wang15c825d2018-09-06 10:49:18 +0800679 struct snd_soc_component *component =
680 snd_soc_kcontrol_component(kcontrol);
681 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530682 int value = ucontrol->value.integer.value[0];
683
Meng Wang15c825d2018-09-06 10:49:18 +0800684 dev_dbg(component->dev, "%s: Compander enable current %d, new %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530685 __func__, wsa881x->comp_enable, value);
686 wsa881x->comp_enable = value;
687 return 0;
688}
689
690static int wsa881x_get_boost(struct snd_kcontrol *kcontrol,
691 struct snd_ctl_elem_value *ucontrol)
692{
693
Meng Wang15c825d2018-09-06 10:49:18 +0800694 struct snd_soc_component *component =
695 snd_soc_kcontrol_component(kcontrol);
696 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530697
698 ucontrol->value.integer.value[0] = wsa881x->boost_enable;
699 return 0;
700}
701
702static int wsa881x_set_boost(struct snd_kcontrol *kcontrol,
703 struct snd_ctl_elem_value *ucontrol)
704{
Meng Wang15c825d2018-09-06 10:49:18 +0800705 struct snd_soc_component *component =
706 snd_soc_kcontrol_component(kcontrol);
707 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530708 int value = ucontrol->value.integer.value[0];
709
Meng Wang15c825d2018-09-06 10:49:18 +0800710 dev_dbg(component->dev, "%s: Boost enable current %d, new %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530711 __func__, wsa881x->boost_enable, value);
712 wsa881x->boost_enable = value;
713 return 0;
714}
715
716static int wsa881x_get_visense(struct snd_kcontrol *kcontrol,
717 struct snd_ctl_elem_value *ucontrol)
718{
719
Meng Wang15c825d2018-09-06 10:49:18 +0800720 struct snd_soc_component *component =
721 snd_soc_kcontrol_component(kcontrol);
722 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530723
724 ucontrol->value.integer.value[0] = wsa881x->visense_enable;
725 return 0;
726}
727
728static int wsa881x_set_visense(struct snd_kcontrol *kcontrol,
729 struct snd_ctl_elem_value *ucontrol)
730{
Meng Wang15c825d2018-09-06 10:49:18 +0800731 struct snd_soc_component *component =
732 snd_soc_kcontrol_component(kcontrol);
733 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530734 int value = ucontrol->value.integer.value[0];
735
Meng Wang15c825d2018-09-06 10:49:18 +0800736 dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530737 __func__, wsa881x->visense_enable, value);
738 wsa881x->visense_enable = value;
739 return 0;
740}
741
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800742static int wsa881x_set_boost_level(struct snd_kcontrol *kcontrol,
743 struct snd_ctl_elem_value *ucontrol)
744{
Meng Wang15c825d2018-09-06 10:49:18 +0800745 struct snd_soc_component *component =
746 snd_soc_kcontrol_component(kcontrol);
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800747 u8 wsa_boost_level = 0;
748
Meng Wang15c825d2018-09-06 10:49:18 +0800749 dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800750 __func__, ucontrol->value.integer.value[0]);
751
752 wsa_boost_level = ucontrol->value.integer.value[0];
Meng Wang15c825d2018-09-06 10:49:18 +0800753 snd_soc_component_update_bits(component, WSA881X_BOOST_PRESET_OUT1,
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800754 0xff, wsa_boost_level);
755
756 return 0;
757}
758
759static int wsa881x_get_boost_level(struct snd_kcontrol *kcontrol,
760 struct snd_ctl_elem_value *ucontrol)
761{
Meng Wang15c825d2018-09-06 10:49:18 +0800762 struct snd_soc_component *component =
763 snd_soc_kcontrol_component(kcontrol);
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800764 u8 wsa_boost_level = 0;
765
Meng Wang15c825d2018-09-06 10:49:18 +0800766 wsa_boost_level = snd_soc_component_read32(component,
767 WSA881X_BOOST_PRESET_OUT1);
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800768 ucontrol->value.integer.value[0] = wsa_boost_level;
Meng Wang15c825d2018-09-06 10:49:18 +0800769 dev_dbg(component->dev, "%s: boost level = 0x%x\n", __func__,
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800770 wsa_boost_level);
771
772 return 0;
773}
774
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530775static const struct snd_kcontrol_new wsa881x_snd_controls[] = {
776 SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
777 wsa881x_get_compander, wsa881x_set_compander),
778
779 SOC_SINGLE_EXT("BOOST Switch", SND_SOC_NOPM, 0, 1, 0,
780 wsa881x_get_boost, wsa881x_set_boost),
781
782 SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
783 wsa881x_get_visense, wsa881x_set_visense),
Xiaojun Sangfa21d8c2017-09-22 17:05:02 +0800784
785 SOC_SINGLE_EXT("Boost Level", SND_SOC_NOPM, 0, 0xff, 0,
786 wsa881x_get_boost_level, wsa881x_set_boost_level),
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530787};
788
789static const struct snd_kcontrol_new swr_dac_port[] = {
790 SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
791};
792
Meng Wang15c825d2018-09-06 10:49:18 +0800793static int wsa881x_set_port(struct snd_soc_component *component, int port_idx,
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530794 u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate,
795 u8 *port_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530796{
Meng Wang15c825d2018-09-06 10:49:18 +0800797 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530798
799 *port_id = wsa881x->port[port_idx].port_id;
800 *num_ch = wsa881x->port[port_idx].num_ch;
801 *ch_mask = wsa881x->port[port_idx].ch_mask;
802 *ch_rate = wsa881x->port[port_idx].ch_rate;
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530803 *port_type = wsa881x->port[port_idx].port_type;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530804 return 0;
805}
806
807static int wsa881x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
808 struct snd_kcontrol *kcontrol, int event)
809{
Meng Wang15c825d2018-09-06 10:49:18 +0800810 struct snd_soc_component *component =
811 snd_soc_dapm_to_component(w->dapm);
812 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530813 u8 port_id[WSA881X_MAX_SWR_PORTS];
814 u8 num_ch[WSA881X_MAX_SWR_PORTS];
815 u8 ch_mask[WSA881X_MAX_SWR_PORTS];
816 u32 ch_rate[WSA881X_MAX_SWR_PORTS];
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530817 u8 port_type[WSA881X_MAX_SWR_PORTS];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530818 u8 num_port = 0;
819
Meng Wang15c825d2018-09-06 10:49:18 +0800820 dev_dbg(component->dev, "%s: event %d name %s\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530821 event, w->name);
822 if (wsa881x == NULL)
823 return -EINVAL;
824
825 switch (event) {
826 case SND_SOC_DAPM_PRE_PMU:
Meng Wang15c825d2018-09-06 10:49:18 +0800827 wsa881x_set_port(component, SWR_DAC_PORT,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530828 &port_id[num_port], &num_ch[num_port],
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530829 &ch_mask[num_port], &ch_rate[num_port],
830 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530831 ++num_port;
832
833 if (wsa881x->comp_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +0800834 wsa881x_set_port(component, SWR_COMP_PORT,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530835 &port_id[num_port], &num_ch[num_port],
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530836 &ch_mask[num_port], &ch_rate[num_port],
837 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530838 ++num_port;
839 }
840 if (wsa881x->boost_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +0800841 wsa881x_set_port(component, SWR_BOOST_PORT,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530842 &port_id[num_port], &num_ch[num_port],
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530843 &ch_mask[num_port], &ch_rate[num_port],
844 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530845 ++num_port;
846 }
847 if (wsa881x->visense_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +0800848 wsa881x_set_port(component, SWR_VISENSE_PORT,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530849 &port_id[num_port], &num_ch[num_port],
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530850 &ch_mask[num_port], &ch_rate[num_port],
851 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530852 ++num_port;
853 }
854 swr_connect_port(wsa881x->swr_slave, &port_id[0], num_port,
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530855 &ch_mask[0], &ch_rate[0], &num_ch[0],
856 &port_type[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530857 break;
858 case SND_SOC_DAPM_POST_PMU:
859 break;
860 case SND_SOC_DAPM_PRE_PMD:
861 break;
862 case SND_SOC_DAPM_POST_PMD:
Meng Wang15c825d2018-09-06 10:49:18 +0800863 wsa881x_set_port(component, SWR_DAC_PORT,
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530864 &port_id[num_port], &num_ch[num_port],
865 &ch_mask[num_port], &ch_rate[num_port],
866 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530867 ++num_port;
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530868
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530869 if (wsa881x->comp_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +0800870 wsa881x_set_port(component, SWR_COMP_PORT,
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530871 &port_id[num_port], &num_ch[num_port],
872 &ch_mask[num_port], &ch_rate[num_port],
873 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530874 ++num_port;
875 }
876 if (wsa881x->boost_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +0800877 wsa881x_set_port(component, SWR_BOOST_PORT,
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530878 &port_id[num_port], &num_ch[num_port],
879 &ch_mask[num_port], &ch_rate[num_port],
880 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530881 ++num_port;
882 }
883 if (wsa881x->visense_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +0800884 wsa881x_set_port(component, SWR_VISENSE_PORT,
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530885 &port_id[num_port], &num_ch[num_port],
886 &ch_mask[num_port], &ch_rate[num_port],
887 &port_type[num_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530888 ++num_port;
889 }
Ramprasad Katkame38aed42018-03-07 16:26:49 +0530890 swr_disconnect_port(wsa881x->swr_slave, &port_id[0], num_port,
891 &ch_mask[0], &port_type[0]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530892 break;
893 default:
894 break;
895 }
896 return 0;
897}
898
899static int wsa881x_rdac_event(struct snd_soc_dapm_widget *w,
900 struct snd_kcontrol *kcontrol, int event)
901{
Meng Wang15c825d2018-09-06 10:49:18 +0800902 struct snd_soc_component *component =
903 snd_soc_dapm_to_component(w->dapm);
904 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530905
Meng Wang15c825d2018-09-06 10:49:18 +0800906 dev_dbg(component->dev, "%s: %s %d boost %d visense %d\n", __func__,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530907 w->name, event, wsa881x->boost_enable,
908 wsa881x->visense_enable);
909
910 switch (event) {
911 case SND_SOC_DAPM_PRE_PMU:
Laxminath Kasamc0684fc2018-07-31 19:26:56 +0530912 mutex_lock(&wsa881x->temp_lock);
Meng Wang15c825d2018-09-06 10:49:18 +0800913 wsa881x_resource_acquire(component, ENABLE);
Laxminath Kasamc0684fc2018-07-31 19:26:56 +0530914 mutex_unlock(&wsa881x->temp_lock);
Meng Wang15c825d2018-09-06 10:49:18 +0800915 wsa881x_boost_ctrl(component, ENABLE);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530916 break;
917 case SND_SOC_DAPM_POST_PMD:
918 swr_slvdev_datapath_control(wsa881x->swr_slave,
919 wsa881x->swr_slave->dev_num,
920 false);
Meng Wang15c825d2018-09-06 10:49:18 +0800921 wsa881x_boost_ctrl(component, DISABLE);
Laxminath Kasamc0684fc2018-07-31 19:26:56 +0530922 mutex_lock(&wsa881x->temp_lock);
Meng Wang15c825d2018-09-06 10:49:18 +0800923 wsa881x_resource_acquire(component, DISABLE);
Laxminath Kasamc0684fc2018-07-31 19:26:56 +0530924 mutex_unlock(&wsa881x->temp_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530925 break;
926 }
927 return 0;
928}
929
Meng Wang15c825d2018-09-06 10:49:18 +0800930static int wsa881x_ramp_pa_gain(struct snd_soc_component *component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530931 int min_gain, int max_gain, int udelay)
932{
933 int val;
934
935 for (val = min_gain; max_gain <= val; val--) {
Meng Wang15c825d2018-09-06 10:49:18 +0800936 snd_soc_component_update_bits(component, WSA881X_SPKR_DRV_GAIN,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530937 0xF0, val << 4);
938 /*
939 * 1ms delay is needed for every step change in gain as per
940 * HW requirement.
941 */
942 usleep_range(udelay, udelay+10);
943 }
944 return 0;
945}
946
947static void wsa881x_ocp_ctl_work(struct work_struct *work)
948{
949 struct wsa881x_priv *wsa881x;
950 struct delayed_work *dwork;
Meng Wang15c825d2018-09-06 10:49:18 +0800951 struct snd_soc_component *component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530952 int temp_val;
953
954 dwork = to_delayed_work(work);
955 wsa881x = container_of(dwork, struct wsa881x_priv, ocp_ctl_work);
956
Meng Wang15c825d2018-09-06 10:49:18 +0800957 component = wsa881x->component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530958 wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
Meng Wang15c825d2018-09-06 10:49:18 +0800959 dev_dbg(component->dev, " temp = %d\n", temp_val);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530960
961 if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
Meng Wang15c825d2018-09-06 10:49:18 +0800962 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
963 0xC0, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530964 else
Meng Wang15c825d2018-09-06 10:49:18 +0800965 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
966 0xC0, 0xC0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530967
968 schedule_delayed_work(&wsa881x->ocp_ctl_work,
969 msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
970}
971
972static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
973 struct snd_kcontrol *kcontrol, int event)
974{
Meng Wang15c825d2018-09-06 10:49:18 +0800975 struct snd_soc_component *component =
976 snd_soc_dapm_to_component(w->dapm);
977 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530978 int min_gain, max_gain;
979
Meng Wang15c825d2018-09-06 10:49:18 +0800980 dev_dbg(component->dev, "%s: %s %d\n", __func__, w->name, event);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530981 switch (event) {
982 case SND_SOC_DAPM_PRE_PMU:
Meng Wang15c825d2018-09-06 10:49:18 +0800983 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
984 0xC0, 0x80);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530985 regmap_multi_reg_write(wsa881x->regmap,
986 wsa881x_pre_pmu_pa_2_0,
987 ARRAY_SIZE(wsa881x_pre_pmu_pa_2_0));
988 swr_slvdev_datapath_control(wsa881x->swr_slave,
989 wsa881x->swr_slave->dev_num,
990 true);
991 /* Set register mode if compander is not enabled */
992 if (!wsa881x->comp_enable)
Meng Wang15c825d2018-09-06 10:49:18 +0800993 snd_soc_component_update_bits(component,
994 WSA881X_SPKR_DRV_GAIN,
995 0x08, 0x08);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530996 else
Meng Wang15c825d2018-09-06 10:49:18 +0800997 snd_soc_component_update_bits(component,
998 WSA881X_SPKR_DRV_GAIN,
999 0x08, 0x00);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301000
1001 break;
1002 case SND_SOC_DAPM_POST_PMU:
1003 if (!wsa881x->comp_enable) {
1004 max_gain = wsa881x->pa_gain;
1005 /*
1006 * Gain has to set incrementally in 4 steps
1007 * as per HW sequence
1008 */
1009 if (max_gain > G_4P5DB)
1010 min_gain = G_0DB;
1011 else
1012 min_gain = max_gain + 3;
1013 /*
1014 * 1ms delay is needed before change in gain
1015 * as per HW requirement.
1016 */
1017 usleep_range(1000, 1010);
Meng Wang15c825d2018-09-06 10:49:18 +08001018 wsa881x_ramp_pa_gain(component, min_gain, max_gain,
1019 1000);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301020 }
1021 if (wsa881x->visense_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +08001022 wsa881x_visense_txfe_ctrl(component, ENABLE,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301023 0x00, 0x03, 0x01);
Meng Wang15c825d2018-09-06 10:49:18 +08001024 snd_soc_component_update_bits(component,
1025 WSA881X_ADC_EN_SEL_IBAIS,
1026 0x07, 0x01);
1027 wsa881x_visense_adc_ctrl(component, ENABLE);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301028 }
1029 schedule_delayed_work(&wsa881x->ocp_ctl_work,
1030 msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
1031 /* Force remove group */
1032 swr_remove_from_group(wsa881x->swr_slave,
1033 wsa881x->swr_slave->dev_num);
1034 break;
1035 case SND_SOC_DAPM_POST_PMD:
1036 if (wsa881x->visense_enable) {
Meng Wang15c825d2018-09-06 10:49:18 +08001037 wsa881x_visense_adc_ctrl(component, DISABLE);
1038 wsa881x_visense_txfe_ctrl(component, DISABLE,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301039 0x00, 0x01, 0x01);
1040 }
1041 cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
Meng Wang15c825d2018-09-06 10:49:18 +08001042 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
1043 0xC0, 0xC0);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301044 break;
1045 }
1046 return 0;
1047}
1048
1049static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
1050 SND_SOC_DAPM_INPUT("IN"),
1051
1052 SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
1053 ARRAY_SIZE(swr_dac_port), wsa881x_enable_swr_dac_port,
1054 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1055 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
1056
1057 SND_SOC_DAPM_DAC_E("RDAC", NULL, WSA881X_SPKR_DAC_CTL, 7, 0,
1058 wsa881x_rdac_event,
1059 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1060
1061 SND_SOC_DAPM_PGA_E("SPKR PGA", WSA881X_SPKR_DRV_EN, 7, 0, NULL, 0,
1062 wsa881x_spkr_pa_event, SND_SOC_DAPM_PRE_PMU |
1063 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1064
1065 SND_SOC_DAPM_OUTPUT("SPKR"),
1066};
1067
1068static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
1069 {"SWR DAC_Port", "Switch", "IN"},
1070 {"RDAC", NULL, "SWR DAC_Port"},
1071 {"SPKR PGA", NULL, "RDAC"},
1072 {"SPKR", NULL, "SPKR PGA"},
1073};
1074
Meng Wang15c825d2018-09-06 10:49:18 +08001075int wsa881x_set_channel_map(struct snd_soc_component *component, u8 *port,
1076 u8 num_port, unsigned int *ch_mask,
1077 unsigned int *ch_rate, u8 *port_type)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301078{
Meng Wang15c825d2018-09-06 10:49:18 +08001079 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301080 int i;
1081
1082 if (!port || !ch_mask || !ch_rate ||
1083 (num_port > WSA881X_MAX_SWR_PORTS)) {
Meng Wang15c825d2018-09-06 10:49:18 +08001084 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301085 "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
1086 __func__, port, ch_mask, ch_rate);
1087 return -EINVAL;
1088 }
1089 for (i = 0; i < num_port; i++) {
1090 wsa881x->port[i].port_id = port[i];
1091 wsa881x->port[i].ch_mask = ch_mask[i];
1092 wsa881x->port[i].ch_rate = ch_rate[i];
1093 wsa881x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
Ramprasad Katkame38aed42018-03-07 16:26:49 +05301094 if (port_type)
1095 wsa881x->port[i].port_type = port_type[i];
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301096 }
1097 return 0;
1098}
1099EXPORT_SYMBOL(wsa881x_set_channel_map);
1100
Meng Wang15c825d2018-09-06 10:49:18 +08001101static void wsa881x_init(struct snd_soc_component *component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301102{
Meng Wang15c825d2018-09-06 10:49:18 +08001103 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301104
Meng Wang15c825d2018-09-06 10:49:18 +08001105 wsa881x->version =
1106 snd_soc_component_read32(component, WSA881X_CHIP_ID1);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301107 wsa881x_regmap_defaults(wsa881x->regmap, wsa881x->version);
Vatsal Bucha83716b92017-09-14 12:13:13 +05301108 /* Enable software reset output from soundwire slave */
Meng Wang15c825d2018-09-06 10:49:18 +08001109 snd_soc_component_update_bits(component, WSA881X_SWR_RESET_EN,
1110 0x07, 0x07);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301111 /* Bring out of analog reset */
Meng Wang15c825d2018-09-06 10:49:18 +08001112 snd_soc_component_update_bits(component, WSA881X_CDC_RST_CTL,
1113 0x02, 0x02);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301114 /* Bring out of digital reset */
Meng Wang15c825d2018-09-06 10:49:18 +08001115 snd_soc_component_update_bits(component, WSA881X_CDC_RST_CTL,
1116 0x01, 0x01);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301117
Meng Wang15c825d2018-09-06 10:49:18 +08001118 snd_soc_component_update_bits(component, WSA881X_CLOCK_CONFIG,
1119 0x10, 0x10);
1120 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
1121 0x02, 0x02);
1122 snd_soc_component_update_bits(component, WSA881X_SPKR_MISC_CTL1,
1123 0xC0, 0x80);
1124 snd_soc_component_update_bits(component, WSA881X_SPKR_MISC_CTL1,
1125 0x06, 0x06);
1126 snd_soc_component_update_bits(component, WSA881X_SPKR_BIAS_INT,
1127 0xFF, 0x00);
1128 snd_soc_component_update_bits(component, WSA881X_SPKR_PA_INT,
1129 0xF0, 0x40);
1130 snd_soc_component_update_bits(component, WSA881X_SPKR_PA_INT,
1131 0x0E, 0x0E);
1132 snd_soc_component_update_bits(component, WSA881X_BOOST_LOOP_STABILITY,
1133 0x03, 0x03);
1134 snd_soc_component_update_bits(component, WSA881X_BOOST_MISC2_CTL,
1135 0xFF, 0x14);
1136 snd_soc_component_update_bits(component, WSA881X_BOOST_START_CTL,
1137 0x80, 0x80);
1138 snd_soc_component_update_bits(component, WSA881X_BOOST_START_CTL,
1139 0x03, 0x00);
1140 snd_soc_component_update_bits(component,
1141 WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
1142 0x0C, 0x04);
1143 snd_soc_component_update_bits(component,
1144 WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
1145 0x03, 0x00);
1146 if (snd_soc_component_read32(component, WSA881X_OTP_REG_0))
1147 snd_soc_component_update_bits(component,
1148 WSA881X_BOOST_PRESET_OUT1,
1149 0xF0, 0x70);
1150 snd_soc_component_update_bits(component, WSA881X_BOOST_PRESET_OUT2,
1151 0xF0, 0x30);
1152 snd_soc_component_update_bits(component, WSA881X_SPKR_DRV_EN,
1153 0x08, 0x08);
1154 snd_soc_component_update_bits(component, WSA881X_BOOST_CURRENT_LIMIT,
1155 0x0F, 0x08);
1156 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
1157 0x30, 0x30);
1158 snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
1159 0x0C, 0x00);
1160 snd_soc_component_update_bits(component, WSA881X_OTP_REG_28,
1161 0x3F, 0x3A);
1162 snd_soc_component_update_bits(component, WSA881X_BONGO_RESRV_REG1,
1163 0xFF, 0xB2);
1164 snd_soc_component_update_bits(component, WSA881X_BONGO_RESRV_REG2,
1165 0xFF, 0x05);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301166}
1167
Meng Wang15c825d2018-09-06 10:49:18 +08001168static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301169 bool enable)
1170{
Meng Wang15c825d2018-09-06 10:49:18 +08001171 wsa881x_clk_ctrl(component, enable);
1172 wsa881x_bandgap_ctrl(component, enable);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301173 return 0;
1174}
1175
Meng Wang15c825d2018-09-06 10:49:18 +08001176static int32_t wsa881x_temp_reg_read(struct snd_soc_component *component,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301177 struct wsa_temp_register *wsa_temp_reg)
1178{
Meng Wang15c825d2018-09-06 10:49:18 +08001179 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301180 struct swr_device *dev;
1181 u8 retry = WSA881X_NUM_RETRY;
1182 u8 devnum = 0;
1183
1184 if (!wsa881x) {
Meng Wang15c825d2018-09-06 10:49:18 +08001185 dev_err(component->dev, "%s: wsa881x is NULL\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301186 return -EINVAL;
1187 }
1188 dev = wsa881x->swr_slave;
1189 if (dev && (wsa881x->state == WSA881X_DEV_DOWN)) {
1190 while (swr_get_logical_dev_num(dev, dev->addr, &devnum) &&
1191 retry--) {
1192 /* Retry after 1 msec delay */
1193 usleep_range(1000, 1100);
1194 }
1195 if (retry == 0) {
Meng Wang15c825d2018-09-06 10:49:18 +08001196 dev_err(component->dev,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301197 "%s get devnum %d for dev addr %lx failed\n",
1198 __func__, devnum, dev->addr);
1199 return -EINVAL;
1200 }
1201 }
Laxminath Kasamc0684fc2018-07-31 19:26:56 +05301202 wsa881x_regcache_sync(wsa881x);
1203 mutex_lock(&wsa881x->temp_lock);
Meng Wang15c825d2018-09-06 10:49:18 +08001204 wsa881x_resource_acquire(component, ENABLE);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301205
Meng Wang15c825d2018-09-06 10:49:18 +08001206 snd_soc_component_update_bits(component, WSA881X_TADC_VALUE_CTL,
1207 0x01, 0x00);
1208 wsa_temp_reg->dmeas_msb = snd_soc_component_read32(
1209 component, WSA881X_TEMP_MSB);
1210 wsa_temp_reg->dmeas_lsb = snd_soc_component_read32(
1211 component, WSA881X_TEMP_LSB);
1212 snd_soc_component_update_bits(component, WSA881X_TADC_VALUE_CTL,
1213 0x01, 0x01);
1214 wsa_temp_reg->d1_msb = snd_soc_component_read32(
1215 component, WSA881X_OTP_REG_1);
1216 wsa_temp_reg->d1_lsb = snd_soc_component_read32(
1217 component, WSA881X_OTP_REG_2);
1218 wsa_temp_reg->d2_msb = snd_soc_component_read32(
1219 component, WSA881X_OTP_REG_3);
1220 wsa_temp_reg->d2_lsb = snd_soc_component_read32(
1221 component, WSA881X_OTP_REG_4);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301222
Meng Wang15c825d2018-09-06 10:49:18 +08001223 wsa881x_resource_acquire(component, DISABLE);
Laxminath Kasamc0684fc2018-07-31 19:26:56 +05301224 mutex_unlock(&wsa881x->temp_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301225
1226 return 0;
1227}
1228
Meng Wang15c825d2018-09-06 10:49:18 +08001229static int wsa881x_probe(struct snd_soc_component *component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301230{
Meng Wang15c825d2018-09-06 10:49:18 +08001231 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301232 struct swr_device *dev;
1233
1234 if (!wsa881x)
1235 return -EINVAL;
Meng Wang15c825d2018-09-06 10:49:18 +08001236 snd_soc_component_init_regmap(component, wsa881x->regmap);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301237
1238 dev = wsa881x->swr_slave;
Meng Wang15c825d2018-09-06 10:49:18 +08001239 wsa881x->component = component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301240 mutex_init(&wsa881x->bg_lock);
Meng Wang15c825d2018-09-06 10:49:18 +08001241 wsa881x_init(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301242 snprintf(wsa881x->tz_pdata.name, sizeof(wsa881x->tz_pdata.name),
1243 "%s.%x", "wsatz", (u8)dev->addr);
1244 wsa881x->bg_cnt = 0;
1245 wsa881x->clk_cnt = 0;
Meng Wang15c825d2018-09-06 10:49:18 +08001246 wsa881x->tz_pdata.component = component;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301247 wsa881x->tz_pdata.wsa_temp_reg_read = wsa881x_temp_reg_read;
1248 wsa881x_init_thermal(&wsa881x->tz_pdata);
Meng Wang15c825d2018-09-06 10:49:18 +08001249 snd_soc_add_component_controls(component, wsa_snd_controls,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301250 ARRAY_SIZE(wsa_snd_controls));
1251 INIT_DELAYED_WORK(&wsa881x->ocp_ctl_work, wsa881x_ocp_ctl_work);
1252 return 0;
1253}
1254
Meng Wang15c825d2018-09-06 10:49:18 +08001255static void wsa881x_remove(struct snd_soc_component *component)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301256{
Meng Wang15c825d2018-09-06 10:49:18 +08001257 struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301258
1259 if (wsa881x->tz_pdata.tz_dev)
1260 wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
1261 mutex_destroy(&wsa881x->bg_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301262
Meng Wang15c825d2018-09-06 10:49:18 +08001263 return;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301264}
1265
Meng Wang15c825d2018-09-06 10:49:18 +08001266static const struct snd_soc_component_driver soc_codec_dev_wsa881x = {
1267 .name = DRV_NAME,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301268 .probe = wsa881x_probe,
1269 .remove = wsa881x_remove,
Meng Wang15c825d2018-09-06 10:49:18 +08001270 .controls = wsa881x_snd_controls,
1271 .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
1272 .dapm_widgets = wsa881x_dapm_widgets,
1273 .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
1274 .dapm_routes = wsa881x_audio_map,
1275 .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301276};
1277
1278static int wsa881x_gpio_ctrl(struct wsa881x_priv *wsa881x, bool enable)
1279{
1280 int ret = 0;
1281
1282 if (wsa881x->pd_gpio < 0) {
1283 dev_err(wsa881x->dev, "%s: gpio is not valid %d\n",
1284 __func__, wsa881x->pd_gpio);
1285 return -EINVAL;
1286 }
1287
1288 if (wsa881x->wsa_rst_np) {
1289 if (enable)
1290 ret = msm_cdc_pinctrl_select_active_state(
1291 wsa881x->wsa_rst_np);
1292 else
1293 ret = msm_cdc_pinctrl_select_sleep_state(
1294 wsa881x->wsa_rst_np);
1295 if (ret != 0)
1296 dev_err(wsa881x->dev,
1297 "%s: Failed to turn state %d; ret=%d\n",
1298 __func__, enable, ret);
1299 } else {
1300 if (gpio_is_valid(wsa881x->pd_gpio))
1301 gpio_direction_output(wsa881x->pd_gpio, enable);
1302 }
1303
1304 return ret;
1305}
1306
1307static int wsa881x_gpio_init(struct swr_device *pdev)
1308{
1309 int ret = 0;
1310 struct wsa881x_priv *wsa881x;
1311
1312 wsa881x = swr_get_dev_data(pdev);
1313 if (!wsa881x) {
1314 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1315 return -EINVAL;
1316 }
1317 dev_dbg(&pdev->dev, "%s: gpio %d request with name %s\n",
1318 __func__, wsa881x->pd_gpio, dev_name(&pdev->dev));
1319 ret = gpio_request(wsa881x->pd_gpio, dev_name(&pdev->dev));
1320 if (ret) {
1321 if (ret == -EBUSY) {
1322 /* GPIO was already requested */
1323 dev_dbg(&pdev->dev,
1324 "%s: gpio %d is already set to high\n",
1325 __func__, wsa881x->pd_gpio);
1326 ret = 0;
1327 } else {
1328 dev_err(&pdev->dev, "%s: Failed to request gpio %d, err: %d\n",
1329 __func__, wsa881x->pd_gpio, ret);
1330 }
1331 }
1332 return ret;
1333}
1334
1335static int wsa881x_swr_probe(struct swr_device *pdev)
1336{
1337 int ret = 0;
1338 struct wsa881x_priv *wsa881x;
1339 u8 devnum = 0;
1340 bool pin_state_current = false;
1341
1342 wsa881x = devm_kzalloc(&pdev->dev, sizeof(struct wsa881x_priv),
1343 GFP_KERNEL);
1344 if (!wsa881x)
1345 return -ENOMEM;
1346 wsa881x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
1347 "qcom,spkr-sd-n-node", 0);
1348 if (!wsa881x->wsa_rst_np) {
1349 dev_dbg(&pdev->dev, "%s: Not using pinctrl, fallback to gpio\n",
1350 __func__);
1351 wsa881x->pd_gpio = of_get_named_gpio(pdev->dev.of_node,
1352 "qcom,spkr-sd-n-gpio", 0);
1353 if (wsa881x->pd_gpio < 0) {
1354 dev_err(&pdev->dev, "%s: %s property is not found %d\n",
1355 __func__, "qcom,spkr-sd-n-gpio",
1356 wsa881x->pd_gpio);
1357 goto err;
1358 }
1359 dev_dbg(&pdev->dev, "%s: reset gpio %d\n", __func__,
1360 wsa881x->pd_gpio);
1361 }
1362 swr_set_dev_data(pdev, wsa881x);
1363
1364 wsa881x->swr_slave = pdev;
1365
1366 if (!wsa881x->wsa_rst_np) {
1367 ret = wsa881x_gpio_init(pdev);
1368 if (ret)
1369 goto err;
1370 }
1371 if (wsa881x->wsa_rst_np)
1372 pin_state_current = msm_cdc_pinctrl_get_state(
1373 wsa881x->wsa_rst_np);
1374 wsa881x_gpio_ctrl(wsa881x, true);
1375 wsa881x->state = WSA881X_DEV_UP;
1376
1377 if (!debugfs_wsa881x_dent) {
1378 dbgwsa881x = wsa881x;
1379 debugfs_wsa881x_dent = debugfs_create_dir(
1380 "wsa881x_swr_slave", 0);
1381 if (!IS_ERR(debugfs_wsa881x_dent)) {
1382 debugfs_peek = debugfs_create_file("swrslave_peek",
1383 S_IFREG | 0444, debugfs_wsa881x_dent,
1384 (void *) "swrslave_peek",
1385 &codec_debug_ops);
1386
1387 debugfs_poke = debugfs_create_file("swrslave_poke",
1388 S_IFREG | 0444, debugfs_wsa881x_dent,
1389 (void *) "swrslave_poke",
1390 &codec_debug_ops);
1391
1392 debugfs_reg_dump = debugfs_create_file(
1393 "swrslave_reg_dump",
1394 S_IFREG | 0444,
1395 debugfs_wsa881x_dent,
1396 (void *) "swrslave_reg_dump",
1397 &codec_debug_ops);
1398 }
1399 }
1400
1401 /*
1402 * Add 5msec delay to provide sufficient time for
1403 * soundwire auto enumeration of slave devices as
1404 * as per HW requirement.
1405 */
1406 usleep_range(5000, 5010);
1407 ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
1408 if (ret) {
1409 dev_dbg(&pdev->dev,
1410 "%s get devnum %d for dev addr %lx failed\n",
1411 __func__, devnum, pdev->addr);
1412 goto dev_err;
1413 }
1414 pdev->dev_num = devnum;
1415
1416 wsa881x->regmap = devm_regmap_init_swr(pdev,
1417 &wsa881x_regmap_config);
1418 if (IS_ERR(wsa881x->regmap)) {
1419 ret = PTR_ERR(wsa881x->regmap);
1420 dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
1421 __func__, ret);
1422 goto dev_err;
1423 }
1424
Meng Wang15c825d2018-09-06 10:49:18 +08001425 ret = snd_soc_register_component(&pdev->dev, &soc_codec_dev_wsa881x,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301426 NULL, 0);
1427 if (ret) {
1428 dev_err(&pdev->dev, "%s: Codec registration failed\n",
1429 __func__);
1430 goto dev_err;
1431 }
Laxminath Kasamc0684fc2018-07-31 19:26:56 +05301432 mutex_init(&wsa881x->res_lock);
1433 mutex_init(&wsa881x->temp_lock);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301434
1435 return 0;
1436
1437dev_err:
1438 if (pin_state_current == false)
1439 wsa881x_gpio_ctrl(wsa881x, false);
1440 swr_remove_device(pdev);
1441err:
1442 return ret;
1443}
1444
1445static int wsa881x_swr_remove(struct swr_device *pdev)
1446{
1447 struct wsa881x_priv *wsa881x;
1448
1449 wsa881x = swr_get_dev_data(pdev);
1450 if (!wsa881x) {
1451 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1452 return -EINVAL;
1453 }
1454 debugfs_remove_recursive(debugfs_wsa881x_dent);
1455 debugfs_wsa881x_dent = NULL;
Laxminath Kasamc0684fc2018-07-31 19:26:56 +05301456 mutex_destroy(&wsa881x->res_lock);
1457 mutex_destroy(&wsa881x->temp_lock);
Meng Wang15c825d2018-09-06 10:49:18 +08001458 snd_soc_unregister_component(&pdev->dev);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301459 if (wsa881x->pd_gpio)
1460 gpio_free(wsa881x->pd_gpio);
1461 swr_set_dev_data(pdev, NULL);
1462 return 0;
1463}
1464
1465static int wsa881x_swr_up(struct swr_device *pdev)
1466{
1467 int ret;
1468 struct wsa881x_priv *wsa881x;
1469
1470 wsa881x = swr_get_dev_data(pdev);
1471 if (!wsa881x) {
1472 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1473 return -EINVAL;
1474 }
1475 ret = wsa881x_gpio_ctrl(wsa881x, true);
1476 if (ret)
1477 dev_err(&pdev->dev, "%s: Failed to enable gpio\n", __func__);
1478 else
1479 wsa881x->state = WSA881X_DEV_UP;
1480
1481 return ret;
1482}
1483
1484static int wsa881x_swr_down(struct swr_device *pdev)
1485{
1486 struct wsa881x_priv *wsa881x;
1487 int ret;
1488
1489 wsa881x = swr_get_dev_data(pdev);
1490 if (!wsa881x) {
1491 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1492 return -EINVAL;
1493 }
1494 if (delayed_work_pending(&wsa881x->ocp_ctl_work))
1495 cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
1496 ret = wsa881x_gpio_ctrl(wsa881x, false);
1497 if (ret)
1498 dev_err(&pdev->dev, "%s: Failed to disable gpio\n", __func__);
1499 else
1500 wsa881x->state = WSA881X_DEV_DOWN;
1501
1502 return ret;
1503}
1504
1505static int wsa881x_swr_reset(struct swr_device *pdev)
1506{
1507 struct wsa881x_priv *wsa881x;
1508 u8 retry = WSA881X_NUM_RETRY;
1509 u8 devnum = 0;
1510
1511 wsa881x = swr_get_dev_data(pdev);
1512 if (!wsa881x) {
1513 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1514 return -EINVAL;
1515 }
Laxminath Kasamc0684fc2018-07-31 19:26:56 +05301516 if (wsa881x->state == WSA881X_DEV_READY) {
1517 dev_dbg(&pdev->dev, "%s: device already active\n", __func__);
1518 return 0;
1519 }
1520
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301521 wsa881x->bg_cnt = 0;
1522 wsa881x->clk_cnt = 0;
1523 while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
1524 /* Retry after 1 msec delay */
1525 usleep_range(1000, 1100);
1526 }
1527 pdev->dev_num = devnum;
Laxminath Kasamc0684fc2018-07-31 19:26:56 +05301528 wsa881x_regcache_sync(wsa881x);
1529
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301530 return 0;
1531}
1532
1533#ifdef CONFIG_PM_SLEEP
1534static int wsa881x_swr_suspend(struct device *dev)
1535{
1536 dev_dbg(dev, "%s: system suspend\n", __func__);
1537 return 0;
1538}
1539
1540static int wsa881x_swr_resume(struct device *dev)
1541{
1542 struct wsa881x_priv *wsa881x = swr_get_dev_data(to_swr_device(dev));
1543
1544 if (!wsa881x) {
1545 dev_err(dev, "%s: wsa881x private data is NULL\n", __func__);
1546 return -EINVAL;
1547 }
1548 dev_dbg(dev, "%s: system resume\n", __func__);
1549 return 0;
1550}
1551#endif /* CONFIG_PM_SLEEP */
1552
1553static const struct dev_pm_ops wsa881x_swr_pm_ops = {
1554 SET_SYSTEM_SLEEP_PM_OPS(wsa881x_swr_suspend, wsa881x_swr_resume)
1555};
1556
1557static const struct swr_device_id wsa881x_swr_id[] = {
1558 {"wsa881x", 0},
1559 {}
1560};
1561
1562static const struct of_device_id wsa881x_swr_dt_match[] = {
1563 {
1564 .compatible = "qcom,wsa881x",
1565 },
1566 {}
1567};
1568
1569static struct swr_driver wsa881x_codec_driver = {
1570 .driver = {
1571 .name = "wsa881x",
1572 .owner = THIS_MODULE,
1573 .pm = &wsa881x_swr_pm_ops,
1574 .of_match_table = wsa881x_swr_dt_match,
1575 },
1576 .probe = wsa881x_swr_probe,
1577 .remove = wsa881x_swr_remove,
1578 .id_table = wsa881x_swr_id,
1579 .device_up = wsa881x_swr_up,
1580 .device_down = wsa881x_swr_down,
1581 .reset_device = wsa881x_swr_reset,
1582};
1583
1584static int __init wsa881x_codec_init(void)
1585{
1586 return swr_driver_register(&wsa881x_codec_driver);
1587}
1588
1589static void __exit wsa881x_codec_exit(void)
1590{
1591 swr_driver_unregister(&wsa881x_codec_driver);
1592}
1593
1594module_init(wsa881x_codec_init);
1595module_exit(wsa881x_codec_exit);
1596
1597MODULE_DESCRIPTION("WSA881x Codec driver");
1598MODULE_LICENSE("GPL v2");