blob: bff0ad5397920d18290a3b7d540ecda0ff5fa812 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/*
2 * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/platform_device.h>
18#include <linux/device.h>
19#include <linux/printk.h>
20#include <linux/bitops.h>
21#include <linux/regulator/consumer.h>
22#include <linux/pm_runtime.h>
23#include <linux/delay.h>
24#include <linux/kernel.h>
25#include <linux/gpio.h>
26#include <linux/of_gpio.h>
27#include <linux/regmap.h>
28#include <linux/debugfs.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053029#include <soc/soundwire.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053030#include <sound/pcm.h>
31#include <sound/pcm_params.h>
32#include <sound/soc.h>
33#include <sound/soc-dapm.h>
34#include <sound/tlv.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053035#include "msm-cdc-pinctrl.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053036#include "wsa881x.h"
37#include "wsa881x-temp-sensor.h"
38
39#define WSA881X_NUM_RETRY 5
40
41enum {
42 G_18DB = 0,
43 G_16P5DB,
44 G_15DB,
45 G_13P5DB,
46 G_12DB,
47 G_10P5DB,
48 G_9DB,
49 G_7P5DB,
50 G_6DB,
51 G_4P5DB,
52 G_3DB,
53 G_1P5DB,
54 G_0DB,
55};
56
57enum {
58 DISABLE = 0,
59 ENABLE,
60};
61
62enum {
63 SWR_DAC_PORT,
64 SWR_COMP_PORT,
65 SWR_BOOST_PORT,
66 SWR_VISENSE_PORT,
67};
68
69struct swr_port {
70 u8 port_id;
71 u8 ch_mask;
72 u32 ch_rate;
73 u8 num_ch;
74};
75
76enum {
77 WSA881X_DEV_DOWN,
78 WSA881X_DEV_UP,
79};
80
81/*
82 * Private data Structure for wsa881x. All parameters related to
83 * WSA881X codec needs to be defined here.
84 */
85struct wsa881x_priv {
86 struct regmap *regmap;
87 struct device *dev;
88 struct swr_device *swr_slave;
89 struct snd_soc_codec *codec;
90 bool comp_enable;
91 bool boost_enable;
92 bool visense_enable;
93 u8 pa_gain;
94 struct swr_port port[WSA881X_MAX_SWR_PORTS];
95 int pd_gpio;
96 struct wsa881x_tz_priv tz_pdata;
97 int bg_cnt;
98 int clk_cnt;
99 int version;
100 struct mutex bg_lock;
101 struct mutex res_lock;
102 struct snd_info_entry *entry;
103 struct snd_info_entry *version_entry;
104 int state;
105 struct delayed_work ocp_ctl_work;
106 struct device_node *wsa_rst_np;
107 int pa_mute;
108};
109
110#define SWR_SLV_MAX_REG_ADDR 0x390
111#define SWR_SLV_START_REG_ADDR 0x40
112#define SWR_SLV_MAX_BUF_LEN 20
113#define BYTES_PER_LINE 12
114#define SWR_SLV_RD_BUF_LEN 8
115#define SWR_SLV_WR_BUF_LEN 32
116#define SWR_SLV_MAX_DEVICES 2
117
118#define WSA881X_VERSION_ENTRY_SIZE 27
119#define WSA881X_OCP_CTL_TIMER_SEC 2
120#define WSA881X_OCP_CTL_TEMP_CELSIUS 25
121#define WSA881X_OCP_CTL_POLL_TIMER_SEC 60
122
123static int wsa881x_ocp_poll_timer_sec = WSA881X_OCP_CTL_POLL_TIMER_SEC;
124module_param(wsa881x_ocp_poll_timer_sec, int, 0664);
125MODULE_PARM_DESC(wsa881x_ocp_poll_timer_sec, "timer for ocp ctl polling");
126
127static struct wsa881x_priv *dbgwsa881x;
128static struct dentry *debugfs_wsa881x_dent;
129static struct dentry *debugfs_peek;
130static struct dentry *debugfs_poke;
131static struct dentry *debugfs_reg_dump;
132static unsigned int read_data;
133static unsigned int devnum;
134
135static int32_t wsa881x_resource_acquire(struct snd_soc_codec *codec,
136 bool enable);
137
138static const char * const wsa_pa_gain_text[] = {
139 "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB", "G_12_DB", "G_10P5_DB",
140 "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
141 "G_0_DB"
142};
143
144static const struct soc_enum wsa_pa_gain_enum =
145 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
146
147static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
148 struct snd_ctl_elem_value *ucontrol)
149{
150 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
151 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
152
153 ucontrol->value.integer.value[0] = wsa881x->pa_gain;
154
155 dev_dbg(codec->dev, "%s: PA gain = 0x%x\n", __func__, wsa881x->pa_gain);
156
157 return 0;
158}
159
160static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
161 struct snd_ctl_elem_value *ucontrol)
162{
163 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
164 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
165
166 dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
167 __func__, ucontrol->value.integer.value[0]);
168
169 wsa881x->pa_gain = ucontrol->value.integer.value[0];
170
171 return 0;
172}
173
174static int wsa881x_get_mute(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
176{
177
178 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
179 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
180
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{
189 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
190 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
191 int value = ucontrol->value.integer.value[0];
192
193 dev_dbg(codec->dev, "%s: mute current %d, new %d\n",
194 __func__, wsa881x->pa_mute, value);
195
196 if (value)
197 snd_soc_update_bits(codec, WSA881X_SPKR_DRV_EN, 0x80, 0x00);
198 wsa881x->pa_mute = value;
199
200 return 0;
201}
202
203
204static const struct snd_kcontrol_new wsa_snd_controls[] = {
205 SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
206 wsa_pa_gain_get, wsa_pa_gain_put),
207 SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
208 wsa881x_get_mute, wsa881x_set_mute),
209};
210
211static int codec_debug_open(struct inode *inode, struct file *file)
212{
213 file->private_data = inode->i_private;
214 return 0;
215}
216
217static int get_parameters(char *buf, u32 *param1, int num_of_par)
218{
219 char *token;
220 int base, cnt;
221
222 token = strsep(&buf, " ");
223 for (cnt = 0; cnt < num_of_par; cnt++) {
224 if (token) {
225 if ((token[1] == 'x') || (token[1] == 'X'))
226 base = 16;
227 else
228 base = 10;
229
230 if (kstrtou32(token, base, &param1[cnt]) != 0)
231 return -EINVAL;
232
233 token = strsep(&buf, " ");
234 } else
235 return -EINVAL;
236 }
237 return 0;
238}
239
240static ssize_t wsa881x_codec_version_read(struct snd_info_entry *entry,
241 void *file_private_data, struct file *file,
242 char __user *buf, size_t count, loff_t pos)
243{
244 struct wsa881x_priv *wsa881x;
245 char buffer[WSA881X_VERSION_ENTRY_SIZE];
246 int len;
247
248 wsa881x = (struct wsa881x_priv *) entry->private_data;
249 if (!wsa881x) {
250 pr_err("%s: wsa881x priv is null\n", __func__);
251 return -EINVAL;
252 }
253
254 len = snprintf(buffer, sizeof(buffer), "WSA881X-SOUNDWIRE_2_0\n");
255
256 return simple_read_from_buffer(buf, count, &pos, buffer, len);
257}
258
259static struct snd_info_entry_ops wsa881x_codec_info_ops = {
260 .read = wsa881x_codec_version_read,
261};
262
263/*
264 * wsa881x_codec_info_create_codec_entry - creates wsa881x module
265 * @codec_root: The parent directory
266 * @codec: Codec instance
267 *
268 * Creates wsa881x module and version entry under the given
269 * parent directory.
270 *
271 * Return: 0 on success or negative error code on failure.
272 */
273int wsa881x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
274 struct snd_soc_codec *codec)
275{
276 struct snd_info_entry *version_entry;
277 struct wsa881x_priv *wsa881x;
278 struct snd_soc_card *card;
279 char name[80];
280
281 if (!codec_root || !codec)
282 return -EINVAL;
283
284 wsa881x = snd_soc_codec_get_drvdata(codec);
285 card = codec->component.card;
286 snprintf(name, sizeof(name), "%s.%x", "wsa881x",
287 (u32)wsa881x->swr_slave->addr);
288
289 wsa881x->entry = snd_info_create_subdir(codec_root->module,
290 (const char *)name,
291 codec_root);
292 if (!wsa881x->entry) {
293 dev_dbg(codec->dev, "%s: failed to create wsa881x entry\n",
294 __func__);
295 return -ENOMEM;
296 }
297
298 version_entry = snd_info_create_card_entry(card->snd_card,
299 "version",
300 wsa881x->entry);
301 if (!version_entry) {
302 dev_dbg(codec->dev, "%s: failed to create wsa881x version entry\n",
303 __func__);
304 return -ENOMEM;
305 }
306
307 version_entry->private_data = wsa881x;
308 version_entry->size = WSA881X_VERSION_ENTRY_SIZE;
309 version_entry->content = SNDRV_INFO_CONTENT_DATA;
310 version_entry->c.ops = &wsa881x_codec_info_ops;
311
312 if (snd_info_register(version_entry) < 0) {
313 snd_info_free_entry(version_entry);
314 return -ENOMEM;
315 }
316 wsa881x->version_entry = version_entry;
317
318 return 0;
319}
320EXPORT_SYMBOL(wsa881x_codec_info_create_codec_entry);
321
322static bool is_swr_slv_reg_readable(int reg)
323{
324 bool ret = true;
325
326 if (((reg > 0x46) && (reg < 0x4A)) ||
327 ((reg > 0x4A) && (reg < 0x50)) ||
328 ((reg > 0x55) && (reg < 0xE0)) ||
329 ((reg > 0xE0) && (reg < 0xF0)) ||
330 ((reg > 0xF0) && (reg < 0x100)) ||
331 ((reg > 0x105) && (reg < 0x120)) ||
332 ((reg > 0x128) && (reg < 0x130)) ||
333 ((reg > 0x138) && (reg < 0x200)) ||
334 ((reg > 0x205) && (reg < 0x220)) ||
335 ((reg > 0x228) && (reg < 0x230)) ||
336 ((reg > 0x238) && (reg < 0x300)) ||
337 ((reg > 0x305) && (reg < 0x320)) ||
338 ((reg > 0x328) && (reg < 0x330)) ||
339 ((reg > 0x338) && (reg < 0x400)) ||
340 ((reg > 0x405) && (reg < 0x420)))
341 ret = false;
342
343 return ret;
344}
345
346static ssize_t wsa881x_swrslave_reg_show(char __user *ubuf, size_t count,
347 loff_t *ppos)
348{
349 int i, reg_val, len;
350 ssize_t total = 0;
351 char tmp_buf[SWR_SLV_MAX_BUF_LEN];
352
353 if (!ubuf || !ppos || (devnum == 0))
354 return 0;
355
356 for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
357 i <= SWR_SLV_MAX_REG_ADDR; i++) {
358 if (!is_swr_slv_reg_readable(i))
359 continue;
360 swr_read(dbgwsa881x->swr_slave, devnum,
361 i, &reg_val, 1);
362 len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i,
363 (reg_val & 0xFF));
364 if ((total + len) >= count - 1)
365 break;
366 if (copy_to_user((ubuf + total), tmp_buf, len)) {
367 pr_err("%s: fail to copy reg dump\n", __func__);
368 total = -EFAULT;
369 goto copy_err;
370 }
371 *ppos += len;
372 total += len;
373 }
374
375copy_err:
376 return total;
377}
378
379static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
380 size_t count, loff_t *ppos)
381{
382 char lbuf[SWR_SLV_RD_BUF_LEN];
383 char *access_str;
384 ssize_t ret_cnt;
385
386 if (!count || !file || !ppos || !ubuf)
387 return -EINVAL;
388
389 access_str = file->private_data;
390 if (*ppos < 0)
391 return -EINVAL;
392
393 if (!strcmp(access_str, "swrslave_peek")) {
394 snprintf(lbuf, sizeof(lbuf), "0x%x\n", (read_data & 0xFF));
395 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
396 strnlen(lbuf, 7));
397 } else if (!strcmp(access_str, "swrslave_reg_dump")) {
398 ret_cnt = wsa881x_swrslave_reg_show(ubuf, count, ppos);
399 } else {
400 pr_err("%s: %s not permitted to read\n", __func__, access_str);
401 ret_cnt = -EPERM;
402 }
403 return ret_cnt;
404}
405
406static ssize_t codec_debug_write(struct file *filp,
407 const char __user *ubuf, size_t cnt, loff_t *ppos)
408{
409 char lbuf[SWR_SLV_WR_BUF_LEN];
410 int rc;
411 u32 param[5];
412 char *access_str;
413
414 if (!filp || !ppos || !ubuf)
415 return -EINVAL;
416
417 access_str = filp->private_data;
418 if (cnt > sizeof(lbuf) - 1)
419 return -EINVAL;
420
421 rc = copy_from_user(lbuf, ubuf, cnt);
422 if (rc)
423 return -EFAULT;
424
425 lbuf[cnt] = '\0';
426 if (!strcmp(access_str, "swrslave_poke")) {
427 /* write */
428 rc = get_parameters(lbuf, param, 3);
429 if ((param[0] <= SWR_SLV_MAX_REG_ADDR) && (param[1] <= 0xFF) &&
430 (rc == 0))
431 swr_write(dbgwsa881x->swr_slave, param[2],
432 param[0], &param[1]);
433 else
434 rc = -EINVAL;
435 } else if (!strcmp(access_str, "swrslave_peek")) {
436 /* read */
437 rc = get_parameters(lbuf, param, 2);
438 if ((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0))
439 swr_read(dbgwsa881x->swr_slave, param[1],
440 param[0], &read_data, 1);
441 else
442 rc = -EINVAL;
443 } else if (!strcmp(access_str, "swrslave_reg_dump")) {
444 /* reg dump */
445 rc = get_parameters(lbuf, param, 1);
446 if ((rc == 0) && (param[0] > 0) &&
447 (param[0] <= SWR_SLV_MAX_DEVICES))
448 devnum = param[0];
449 else
450 rc = -EINVAL;
451 }
452 if (rc == 0)
453 rc = cnt;
454 else
455 pr_err("%s: rc = %d\n", __func__, rc);
456
457 return rc;
458}
459
460static const struct file_operations codec_debug_ops = {
461 .open = codec_debug_open,
462 .write = codec_debug_write,
463 .read = codec_debug_read,
464};
465
466static const struct reg_sequence wsa881x_pre_pmu_pa[] = {
467 {WSA881X_SPKR_DRV_GAIN, 0x41, 0},
468 {WSA881X_SPKR_MISC_CTL1, 0x01, 0},
469 {WSA881X_ADC_EN_DET_TEST_I, 0x01, 0},
470 {WSA881X_ADC_EN_MODU_V, 0x02, 0},
471 {WSA881X_ADC_EN_DET_TEST_V, 0x10, 0},
472 {WSA881X_SPKR_PWRSTG_DBG, 0xA0, 0},
473};
474
475static const struct reg_sequence wsa881x_pre_pmu_pa_2_0[] = {
476 {WSA881X_SPKR_DRV_GAIN, 0x41, 0},
477 {WSA881X_SPKR_MISC_CTL1, 0x87, 0},
478};
479
480static const struct reg_sequence wsa881x_post_pmu_pa[] = {
481 {WSA881X_SPKR_PWRSTG_DBG, 0x00, 0},
482 {WSA881X_ADC_EN_DET_TEST_V, 0x00, 0},
483 {WSA881X_ADC_EN_MODU_V, 0x00, 0},
484 {WSA881X_ADC_EN_DET_TEST_I, 0x00, 0},
485};
486
487static const struct reg_sequence wsa881x_vi_txfe_en[] = {
488 {WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x85, 0},
489 {WSA881X_SPKR_PROT_ATEST2, 0x0A, 0},
490 {WSA881X_SPKR_PROT_FE_GAIN, 0xCF, 0},
491};
492
493static const struct reg_sequence wsa881x_vi_txfe_en_2_0[] = {
494 {WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x85, 0},
495 {WSA881X_SPKR_PROT_ATEST2, 0x0A, 0},
496 {WSA881X_SPKR_PROT_FE_GAIN, 0x47, 0},
497};
498
499static int wsa881x_boost_ctrl(struct snd_soc_codec *codec, bool enable)
500{
501 dev_dbg(codec->dev, "%s: enable:%d\n", __func__, enable);
502 if (enable)
503 snd_soc_update_bits(codec, WSA881X_BOOST_EN_CTL, 0x80, 0x80);
504 else
505 snd_soc_update_bits(codec, WSA881X_BOOST_EN_CTL, 0x80, 0x00);
506 /*
507 * 1.5ms sleep is needed after boost enable/disable as per
508 * HW requirement
509 */
510 usleep_range(1500, 1510);
511 return 0;
512}
513
514static int wsa881x_visense_txfe_ctrl(struct snd_soc_codec *codec, bool enable,
515 u8 isense1_gain, u8 isense2_gain,
516 u8 vsense_gain)
517{
518 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
519
520 dev_dbg(codec->dev,
521 "%s: enable:%d, isense1 gain: %d, isense2 gain: %d, vsense_gain %d\n",
522 __func__, enable, isense1_gain, isense2_gain, vsense_gain);
523
524 if (enable) {
525 regmap_multi_reg_write(wsa881x->regmap,
526 wsa881x_vi_txfe_en_2_0,
527 ARRAY_SIZE(wsa881x_vi_txfe_en_2_0));
528 } else {
529 snd_soc_update_bits(codec, WSA881X_SPKR_PROT_FE_VSENSE_VCM,
530 0x08, 0x08);
531 /*
532 * 200us sleep is needed after visense txfe disable as per
533 * HW requirement.
534 */
535 usleep_range(200, 210);
536 snd_soc_update_bits(codec, WSA881X_SPKR_PROT_FE_GAIN,
537 0x01, 0x00);
538 }
539 return 0;
540}
541
542static int wsa881x_visense_adc_ctrl(struct snd_soc_codec *codec, bool enable)
543{
544
545 dev_dbg(codec->dev, "%s: enable:%d\n", __func__, enable);
546 snd_soc_update_bits(codec, WSA881X_ADC_EN_MODU_V, (0x01 << 7),
547 (enable << 7));
548 snd_soc_update_bits(codec, WSA881X_ADC_EN_MODU_I, (0x01 << 7),
549 (enable << 7));
550 return 0;
551}
552
553static void wsa881x_bandgap_ctrl(struct snd_soc_codec *codec, bool enable)
554{
555 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
556
557 dev_dbg(codec->dev, "%s: enable:%d, bg_count:%d\n", __func__,
558 enable, wsa881x->bg_cnt);
559 mutex_lock(&wsa881x->bg_lock);
560 if (enable) {
561 ++wsa881x->bg_cnt;
562 if (wsa881x->bg_cnt == 1) {
563 snd_soc_update_bits(codec, WSA881X_TEMP_OP,
564 0x08, 0x08);
565 /* 400usec sleep is needed as per HW requirement */
566 usleep_range(400, 410);
567 snd_soc_update_bits(codec, WSA881X_TEMP_OP,
568 0x04, 0x04);
569 }
570 } else {
571 --wsa881x->bg_cnt;
572 if (wsa881x->bg_cnt <= 0) {
573 WARN_ON(wsa881x->bg_cnt < 0);
574 wsa881x->bg_cnt = 0;
575 snd_soc_update_bits(codec, WSA881X_TEMP_OP, 0x04, 0x00);
576 snd_soc_update_bits(codec, WSA881X_TEMP_OP, 0x08, 0x00);
577 }
578 }
579 mutex_unlock(&wsa881x->bg_lock);
580}
581
582static void wsa881x_clk_ctrl(struct snd_soc_codec *codec, bool enable)
583{
584 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
585
586 dev_dbg(codec->dev, "%s: enable:%d, clk_count:%d\n", __func__,
587 enable, wsa881x->clk_cnt);
588 mutex_lock(&wsa881x->res_lock);
589 if (enable) {
590 ++wsa881x->clk_cnt;
591 if (wsa881x->clk_cnt == 1) {
592 snd_soc_write(codec, WSA881X_CDC_DIG_CLK_CTL, 0x01);
593 snd_soc_write(codec, WSA881X_CDC_ANA_CLK_CTL, 0x01);
594 }
595 } else {
596 --wsa881x->clk_cnt;
597 if (wsa881x->clk_cnt <= 0) {
598 WARN_ON(wsa881x->clk_cnt < 0);
599 wsa881x->clk_cnt = 0;
600 snd_soc_write(codec, WSA881X_CDC_DIG_CLK_CTL, 0x00);
601 snd_soc_write(codec, WSA881X_CDC_ANA_CLK_CTL, 0x00);
602 }
603 }
604 mutex_unlock(&wsa881x->res_lock);
605}
606
607static int wsa881x_get_compander(struct snd_kcontrol *kcontrol,
608 struct snd_ctl_elem_value *ucontrol)
609{
610
611 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
612 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
613
614 ucontrol->value.integer.value[0] = wsa881x->comp_enable;
615 return 0;
616}
617
618static int wsa881x_set_compander(struct snd_kcontrol *kcontrol,
619 struct snd_ctl_elem_value *ucontrol)
620{
621 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
622 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
623 int value = ucontrol->value.integer.value[0];
624
625 dev_dbg(codec->dev, "%s: Compander enable current %d, new %d\n",
626 __func__, wsa881x->comp_enable, value);
627 wsa881x->comp_enable = value;
628 return 0;
629}
630
631static int wsa881x_get_boost(struct snd_kcontrol *kcontrol,
632 struct snd_ctl_elem_value *ucontrol)
633{
634
635 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
636 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
637
638 ucontrol->value.integer.value[0] = wsa881x->boost_enable;
639 return 0;
640}
641
642static int wsa881x_set_boost(struct snd_kcontrol *kcontrol,
643 struct snd_ctl_elem_value *ucontrol)
644{
645 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
646 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
647 int value = ucontrol->value.integer.value[0];
648
649 dev_dbg(codec->dev, "%s: Boost enable current %d, new %d\n",
650 __func__, wsa881x->boost_enable, value);
651 wsa881x->boost_enable = value;
652 return 0;
653}
654
655static int wsa881x_get_visense(struct snd_kcontrol *kcontrol,
656 struct snd_ctl_elem_value *ucontrol)
657{
658
659 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
660 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
661
662 ucontrol->value.integer.value[0] = wsa881x->visense_enable;
663 return 0;
664}
665
666static int wsa881x_set_visense(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_value *ucontrol)
668{
669 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
670 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
671 int value = ucontrol->value.integer.value[0];
672
673 dev_dbg(codec->dev, "%s: VIsense enable current %d, new %d\n",
674 __func__, wsa881x->visense_enable, value);
675 wsa881x->visense_enable = value;
676 return 0;
677}
678
679static const struct snd_kcontrol_new wsa881x_snd_controls[] = {
680 SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
681 wsa881x_get_compander, wsa881x_set_compander),
682
683 SOC_SINGLE_EXT("BOOST Switch", SND_SOC_NOPM, 0, 1, 0,
684 wsa881x_get_boost, wsa881x_set_boost),
685
686 SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
687 wsa881x_get_visense, wsa881x_set_visense),
688};
689
690static const struct snd_kcontrol_new swr_dac_port[] = {
691 SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
692};
693
694static int wsa881x_set_port(struct snd_soc_codec *codec, int port_idx,
695 u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate)
696{
697 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
698
699 *port_id = wsa881x->port[port_idx].port_id;
700 *num_ch = wsa881x->port[port_idx].num_ch;
701 *ch_mask = wsa881x->port[port_idx].ch_mask;
702 *ch_rate = wsa881x->port[port_idx].ch_rate;
703 return 0;
704}
705
706static int wsa881x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
707 struct snd_kcontrol *kcontrol, int event)
708{
709 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
710 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
711 u8 port_id[WSA881X_MAX_SWR_PORTS];
712 u8 num_ch[WSA881X_MAX_SWR_PORTS];
713 u8 ch_mask[WSA881X_MAX_SWR_PORTS];
714 u32 ch_rate[WSA881X_MAX_SWR_PORTS];
715 u8 num_port = 0;
716
717 dev_dbg(codec->dev, "%s: event %d name %s\n", __func__,
718 event, w->name);
719 if (wsa881x == NULL)
720 return -EINVAL;
721
722 switch (event) {
723 case SND_SOC_DAPM_PRE_PMU:
724 wsa881x_set_port(codec, SWR_DAC_PORT,
725 &port_id[num_port], &num_ch[num_port],
726 &ch_mask[num_port], &ch_rate[num_port]);
727 ++num_port;
728
729 if (wsa881x->comp_enable) {
730 wsa881x_set_port(codec, SWR_COMP_PORT,
731 &port_id[num_port], &num_ch[num_port],
732 &ch_mask[num_port], &ch_rate[num_port]);
733 ++num_port;
734 }
735 if (wsa881x->boost_enable) {
736 wsa881x_set_port(codec, SWR_BOOST_PORT,
737 &port_id[num_port], &num_ch[num_port],
738 &ch_mask[num_port], &ch_rate[num_port]);
739 ++num_port;
740 }
741 if (wsa881x->visense_enable) {
742 wsa881x_set_port(codec, SWR_VISENSE_PORT,
743 &port_id[num_port], &num_ch[num_port],
744 &ch_mask[num_port], &ch_rate[num_port]);
745 ++num_port;
746 }
747 swr_connect_port(wsa881x->swr_slave, &port_id[0], num_port,
748 &ch_mask[0], &ch_rate[0], &num_ch[0]);
749 break;
750 case SND_SOC_DAPM_POST_PMU:
751 break;
752 case SND_SOC_DAPM_PRE_PMD:
753 break;
754 case SND_SOC_DAPM_POST_PMD:
755 port_id[num_port] = wsa881x->port[SWR_DAC_PORT].port_id;
756 ++num_port;
757 if (wsa881x->comp_enable) {
758 port_id[num_port] =
759 wsa881x->port[SWR_COMP_PORT].port_id;
760 ++num_port;
761 }
762 if (wsa881x->boost_enable) {
763 port_id[num_port] =
764 wsa881x->port[SWR_BOOST_PORT].port_id;
765 ++num_port;
766 }
767 if (wsa881x->visense_enable) {
768 port_id[num_port] =
769 wsa881x->port[SWR_VISENSE_PORT].port_id;
770 ++num_port;
771 }
772 swr_disconnect_port(wsa881x->swr_slave, &port_id[0], num_port);
773 break;
774 default:
775 break;
776 }
777 return 0;
778}
779
780static int wsa881x_rdac_event(struct snd_soc_dapm_widget *w,
781 struct snd_kcontrol *kcontrol, int event)
782{
783 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
784 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
785
786 dev_dbg(codec->dev, "%s: %s %d boost %d visense %d\n", __func__,
787 w->name, event, wsa881x->boost_enable,
788 wsa881x->visense_enable);
789
790 switch (event) {
791 case SND_SOC_DAPM_PRE_PMU:
792 wsa881x_resource_acquire(codec, ENABLE);
793 wsa881x_boost_ctrl(codec, ENABLE);
794 break;
795 case SND_SOC_DAPM_POST_PMD:
796 swr_slvdev_datapath_control(wsa881x->swr_slave,
797 wsa881x->swr_slave->dev_num,
798 false);
799 wsa881x_boost_ctrl(codec, DISABLE);
800 wsa881x_resource_acquire(codec, DISABLE);
801 break;
802 }
803 return 0;
804}
805
806static int wsa881x_ramp_pa_gain(struct snd_soc_codec *codec,
807 int min_gain, int max_gain, int udelay)
808{
809 int val;
810
811 for (val = min_gain; max_gain <= val; val--) {
812 snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
813 0xF0, val << 4);
814 /*
815 * 1ms delay is needed for every step change in gain as per
816 * HW requirement.
817 */
818 usleep_range(udelay, udelay+10);
819 }
820 return 0;
821}
822
823static void wsa881x_ocp_ctl_work(struct work_struct *work)
824{
825 struct wsa881x_priv *wsa881x;
826 struct delayed_work *dwork;
827 struct snd_soc_codec *codec;
828 int temp_val;
829
830 dwork = to_delayed_work(work);
831 wsa881x = container_of(dwork, struct wsa881x_priv, ocp_ctl_work);
832
833 codec = wsa881x->codec;
834 wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
835 dev_dbg(codec->dev, " temp = %d\n", temp_val);
836
837 if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
838 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0x00);
839 else
840 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
841
842 schedule_delayed_work(&wsa881x->ocp_ctl_work,
843 msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
844}
845
846static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
847 struct snd_kcontrol *kcontrol, int event)
848{
849 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
850 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
851 int min_gain, max_gain;
852
853 dev_dbg(codec->dev, "%s: %s %d\n", __func__, w->name, event);
854 switch (event) {
855 case SND_SOC_DAPM_PRE_PMU:
856 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0x80);
857 regmap_multi_reg_write(wsa881x->regmap,
858 wsa881x_pre_pmu_pa_2_0,
859 ARRAY_SIZE(wsa881x_pre_pmu_pa_2_0));
860 swr_slvdev_datapath_control(wsa881x->swr_slave,
861 wsa881x->swr_slave->dev_num,
862 true);
863 /* Set register mode if compander is not enabled */
864 if (!wsa881x->comp_enable)
865 snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
866 0x08, 0x08);
867 else
868 snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
869 0x08, 0x00);
870
871 break;
872 case SND_SOC_DAPM_POST_PMU:
873 if (!wsa881x->comp_enable) {
874 max_gain = wsa881x->pa_gain;
875 /*
876 * Gain has to set incrementally in 4 steps
877 * as per HW sequence
878 */
879 if (max_gain > G_4P5DB)
880 min_gain = G_0DB;
881 else
882 min_gain = max_gain + 3;
883 /*
884 * 1ms delay is needed before change in gain
885 * as per HW requirement.
886 */
887 usleep_range(1000, 1010);
888 wsa881x_ramp_pa_gain(codec, min_gain, max_gain, 1000);
889 }
890 if (wsa881x->visense_enable) {
891 wsa881x_visense_txfe_ctrl(codec, ENABLE,
892 0x00, 0x03, 0x01);
893 snd_soc_update_bits(codec, WSA881X_ADC_EN_SEL_IBAIS,
894 0x07, 0x01);
895 wsa881x_visense_adc_ctrl(codec, ENABLE);
896 }
897 schedule_delayed_work(&wsa881x->ocp_ctl_work,
898 msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
899 /* Force remove group */
900 swr_remove_from_group(wsa881x->swr_slave,
901 wsa881x->swr_slave->dev_num);
902 break;
903 case SND_SOC_DAPM_POST_PMD:
904 if (wsa881x->visense_enable) {
905 wsa881x_visense_adc_ctrl(codec, DISABLE);
906 wsa881x_visense_txfe_ctrl(codec, DISABLE,
907 0x00, 0x01, 0x01);
908 }
909 cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
910 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
911 break;
912 }
913 return 0;
914}
915
916static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
917 SND_SOC_DAPM_INPUT("IN"),
918
919 SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
920 ARRAY_SIZE(swr_dac_port), wsa881x_enable_swr_dac_port,
921 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
922 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
923
924 SND_SOC_DAPM_DAC_E("RDAC", NULL, WSA881X_SPKR_DAC_CTL, 7, 0,
925 wsa881x_rdac_event,
926 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
927
928 SND_SOC_DAPM_PGA_E("SPKR PGA", WSA881X_SPKR_DRV_EN, 7, 0, NULL, 0,
929 wsa881x_spkr_pa_event, SND_SOC_DAPM_PRE_PMU |
930 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
931
932 SND_SOC_DAPM_OUTPUT("SPKR"),
933};
934
935static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
936 {"SWR DAC_Port", "Switch", "IN"},
937 {"RDAC", NULL, "SWR DAC_Port"},
938 {"SPKR PGA", NULL, "RDAC"},
939 {"SPKR", NULL, "SPKR PGA"},
940};
941
942int wsa881x_set_channel_map(struct snd_soc_codec *codec, u8 *port, u8 num_port,
943 unsigned int *ch_mask, unsigned int *ch_rate)
944{
945 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
946 int i;
947
948 if (!port || !ch_mask || !ch_rate ||
949 (num_port > WSA881X_MAX_SWR_PORTS)) {
950 dev_err(codec->dev,
951 "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
952 __func__, port, ch_mask, ch_rate);
953 return -EINVAL;
954 }
955 for (i = 0; i < num_port; i++) {
956 wsa881x->port[i].port_id = port[i];
957 wsa881x->port[i].ch_mask = ch_mask[i];
958 wsa881x->port[i].ch_rate = ch_rate[i];
959 wsa881x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
960 }
961 return 0;
962}
963EXPORT_SYMBOL(wsa881x_set_channel_map);
964
965static void wsa881x_init(struct snd_soc_codec *codec)
966{
967 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
968
969 wsa881x->version = snd_soc_read(codec, WSA881X_CHIP_ID1);
970 wsa881x_regmap_defaults(wsa881x->regmap, wsa881x->version);
Vatsal Bucha83716b92017-09-14 12:13:13 +0530971 /* Enable software reset output from soundwire slave */
972 snd_soc_update_bits(codec, WSA881X_SWR_RESET_EN, 0x07, 0x07);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530973 /* Bring out of analog reset */
974 snd_soc_update_bits(codec, WSA881X_CDC_RST_CTL, 0x02, 0x02);
975 /* Bring out of digital reset */
976 snd_soc_update_bits(codec, WSA881X_CDC_RST_CTL, 0x01, 0x01);
977
978 snd_soc_update_bits(codec, WSA881X_CLOCK_CONFIG, 0x10, 0x10);
979 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x02, 0x02);
980 snd_soc_update_bits(codec, WSA881X_SPKR_MISC_CTL1, 0xC0, 0x80);
981 snd_soc_update_bits(codec, WSA881X_SPKR_MISC_CTL1, 0x06, 0x06);
982 snd_soc_update_bits(codec, WSA881X_SPKR_BIAS_INT, 0xFF, 0x00);
983 snd_soc_update_bits(codec, WSA881X_SPKR_PA_INT, 0xF0, 0x40);
984 snd_soc_update_bits(codec, WSA881X_SPKR_PA_INT, 0x0E, 0x0E);
985 snd_soc_update_bits(codec, WSA881X_BOOST_LOOP_STABILITY,
986 0x03, 0x03);
987 snd_soc_update_bits(codec, WSA881X_BOOST_MISC2_CTL, 0xFF, 0x14);
988 snd_soc_update_bits(codec, WSA881X_BOOST_START_CTL, 0x80, 0x80);
989 snd_soc_update_bits(codec, WSA881X_BOOST_START_CTL, 0x03, 0x00);
990 snd_soc_update_bits(codec, WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
991 0x0C, 0x04);
992 snd_soc_update_bits(codec, WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
993 0x03, 0x00);
994 if (snd_soc_read(codec, WSA881X_OTP_REG_0))
995 snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT1,
996 0xF0, 0x70);
997 snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT2,
998 0xF0, 0x30);
999 snd_soc_update_bits(codec, WSA881X_SPKR_DRV_EN, 0x08, 0x08);
1000 snd_soc_update_bits(codec, WSA881X_BOOST_CURRENT_LIMIT,
1001 0x0F, 0x08);
1002 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x30, 0x30);
1003 snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x0C, 0x00);
1004 snd_soc_update_bits(codec, WSA881X_OTP_REG_28, 0x3F, 0x3A);
1005 snd_soc_update_bits(codec, WSA881X_BONGO_RESRV_REG1,
1006 0xFF, 0xB2);
1007 snd_soc_update_bits(codec, WSA881X_BONGO_RESRV_REG2,
1008 0xFF, 0x05);
1009}
1010
1011static int32_t wsa881x_resource_acquire(struct snd_soc_codec *codec,
1012 bool enable)
1013{
1014 wsa881x_clk_ctrl(codec, enable);
1015 wsa881x_bandgap_ctrl(codec, enable);
1016 return 0;
1017}
1018
1019static int32_t wsa881x_temp_reg_read(struct snd_soc_codec *codec,
1020 struct wsa_temp_register *wsa_temp_reg)
1021{
1022 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
1023 struct swr_device *dev;
1024 u8 retry = WSA881X_NUM_RETRY;
1025 u8 devnum = 0;
1026
1027 if (!wsa881x) {
1028 dev_err(codec->dev, "%s: wsa881x is NULL\n", __func__);
1029 return -EINVAL;
1030 }
1031 dev = wsa881x->swr_slave;
1032 if (dev && (wsa881x->state == WSA881X_DEV_DOWN)) {
1033 while (swr_get_logical_dev_num(dev, dev->addr, &devnum) &&
1034 retry--) {
1035 /* Retry after 1 msec delay */
1036 usleep_range(1000, 1100);
1037 }
1038 if (retry == 0) {
1039 dev_err(codec->dev,
1040 "%s get devnum %d for dev addr %lx failed\n",
1041 __func__, devnum, dev->addr);
1042 return -EINVAL;
1043 }
1044 }
1045 mutex_lock(&wsa881x->res_lock);
1046 if (!wsa881x->clk_cnt) {
1047 regcache_mark_dirty(wsa881x->regmap);
1048 regcache_sync(wsa881x->regmap);
1049 }
1050 mutex_unlock(&wsa881x->res_lock);
1051
1052 wsa881x_resource_acquire(codec, ENABLE);
1053
1054 snd_soc_update_bits(codec, WSA881X_TADC_VALUE_CTL, 0x01, 0x00);
1055 wsa_temp_reg->dmeas_msb = snd_soc_read(codec, WSA881X_TEMP_MSB);
1056 wsa_temp_reg->dmeas_lsb = snd_soc_read(codec, WSA881X_TEMP_LSB);
1057 snd_soc_update_bits(codec, WSA881X_TADC_VALUE_CTL, 0x01, 0x01);
1058 wsa_temp_reg->d1_msb = snd_soc_read(codec, WSA881X_OTP_REG_1);
1059 wsa_temp_reg->d1_lsb = snd_soc_read(codec, WSA881X_OTP_REG_2);
1060 wsa_temp_reg->d2_msb = snd_soc_read(codec, WSA881X_OTP_REG_3);
1061 wsa_temp_reg->d2_lsb = snd_soc_read(codec, WSA881X_OTP_REG_4);
1062
1063 wsa881x_resource_acquire(codec, DISABLE);
1064
1065 return 0;
1066}
1067
1068static int wsa881x_probe(struct snd_soc_codec *codec)
1069{
1070 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
1071 struct swr_device *dev;
1072
1073 if (!wsa881x)
1074 return -EINVAL;
1075
1076 dev = wsa881x->swr_slave;
1077 wsa881x->codec = codec;
1078 mutex_init(&wsa881x->bg_lock);
1079 mutex_init(&wsa881x->res_lock);
1080 wsa881x_init(codec);
1081 snprintf(wsa881x->tz_pdata.name, sizeof(wsa881x->tz_pdata.name),
1082 "%s.%x", "wsatz", (u8)dev->addr);
1083 wsa881x->bg_cnt = 0;
1084 wsa881x->clk_cnt = 0;
1085 wsa881x->tz_pdata.codec = codec;
1086 wsa881x->tz_pdata.wsa_temp_reg_read = wsa881x_temp_reg_read;
1087 wsa881x_init_thermal(&wsa881x->tz_pdata);
1088 snd_soc_add_codec_controls(codec, wsa_snd_controls,
1089 ARRAY_SIZE(wsa_snd_controls));
1090 INIT_DELAYED_WORK(&wsa881x->ocp_ctl_work, wsa881x_ocp_ctl_work);
1091 return 0;
1092}
1093
1094static int wsa881x_remove(struct snd_soc_codec *codec)
1095{
1096 struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
1097
1098 if (wsa881x->tz_pdata.tz_dev)
1099 wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
1100 mutex_destroy(&wsa881x->bg_lock);
1101 mutex_destroy(&wsa881x->res_lock);
1102
1103 return 0;
1104}
1105
1106static struct regmap *wsa881x_get_regmap(struct device *dev)
1107{
1108 struct wsa881x_priv *control = swr_get_dev_data(to_swr_device(dev));
1109
1110 if (!control)
1111 return NULL;
1112
1113 return control->regmap;
1114}
1115
1116static struct snd_soc_codec_driver soc_codec_dev_wsa881x = {
1117 .probe = wsa881x_probe,
1118 .remove = wsa881x_remove,
1119 .get_regmap = wsa881x_get_regmap,
1120 .component_driver = {
1121 .controls = wsa881x_snd_controls,
1122 .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
1123 .dapm_widgets = wsa881x_dapm_widgets,
1124 .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
1125 .dapm_routes = wsa881x_audio_map,
1126 .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
1127 },
1128};
1129
1130static int wsa881x_gpio_ctrl(struct wsa881x_priv *wsa881x, bool enable)
1131{
1132 int ret = 0;
1133
1134 if (wsa881x->pd_gpio < 0) {
1135 dev_err(wsa881x->dev, "%s: gpio is not valid %d\n",
1136 __func__, wsa881x->pd_gpio);
1137 return -EINVAL;
1138 }
1139
1140 if (wsa881x->wsa_rst_np) {
1141 if (enable)
1142 ret = msm_cdc_pinctrl_select_active_state(
1143 wsa881x->wsa_rst_np);
1144 else
1145 ret = msm_cdc_pinctrl_select_sleep_state(
1146 wsa881x->wsa_rst_np);
1147 if (ret != 0)
1148 dev_err(wsa881x->dev,
1149 "%s: Failed to turn state %d; ret=%d\n",
1150 __func__, enable, ret);
1151 } else {
1152 if (gpio_is_valid(wsa881x->pd_gpio))
1153 gpio_direction_output(wsa881x->pd_gpio, enable);
1154 }
1155
1156 return ret;
1157}
1158
1159static int wsa881x_gpio_init(struct swr_device *pdev)
1160{
1161 int ret = 0;
1162 struct wsa881x_priv *wsa881x;
1163
1164 wsa881x = swr_get_dev_data(pdev);
1165 if (!wsa881x) {
1166 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1167 return -EINVAL;
1168 }
1169 dev_dbg(&pdev->dev, "%s: gpio %d request with name %s\n",
1170 __func__, wsa881x->pd_gpio, dev_name(&pdev->dev));
1171 ret = gpio_request(wsa881x->pd_gpio, dev_name(&pdev->dev));
1172 if (ret) {
1173 if (ret == -EBUSY) {
1174 /* GPIO was already requested */
1175 dev_dbg(&pdev->dev,
1176 "%s: gpio %d is already set to high\n",
1177 __func__, wsa881x->pd_gpio);
1178 ret = 0;
1179 } else {
1180 dev_err(&pdev->dev, "%s: Failed to request gpio %d, err: %d\n",
1181 __func__, wsa881x->pd_gpio, ret);
1182 }
1183 }
1184 return ret;
1185}
1186
1187static int wsa881x_swr_probe(struct swr_device *pdev)
1188{
1189 int ret = 0;
1190 struct wsa881x_priv *wsa881x;
1191 u8 devnum = 0;
1192 bool pin_state_current = false;
1193
1194 wsa881x = devm_kzalloc(&pdev->dev, sizeof(struct wsa881x_priv),
1195 GFP_KERNEL);
1196 if (!wsa881x)
1197 return -ENOMEM;
1198 wsa881x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
1199 "qcom,spkr-sd-n-node", 0);
1200 if (!wsa881x->wsa_rst_np) {
1201 dev_dbg(&pdev->dev, "%s: Not using pinctrl, fallback to gpio\n",
1202 __func__);
1203 wsa881x->pd_gpio = of_get_named_gpio(pdev->dev.of_node,
1204 "qcom,spkr-sd-n-gpio", 0);
1205 if (wsa881x->pd_gpio < 0) {
1206 dev_err(&pdev->dev, "%s: %s property is not found %d\n",
1207 __func__, "qcom,spkr-sd-n-gpio",
1208 wsa881x->pd_gpio);
1209 goto err;
1210 }
1211 dev_dbg(&pdev->dev, "%s: reset gpio %d\n", __func__,
1212 wsa881x->pd_gpio);
1213 }
1214 swr_set_dev_data(pdev, wsa881x);
1215
1216 wsa881x->swr_slave = pdev;
1217
1218 if (!wsa881x->wsa_rst_np) {
1219 ret = wsa881x_gpio_init(pdev);
1220 if (ret)
1221 goto err;
1222 }
1223 if (wsa881x->wsa_rst_np)
1224 pin_state_current = msm_cdc_pinctrl_get_state(
1225 wsa881x->wsa_rst_np);
1226 wsa881x_gpio_ctrl(wsa881x, true);
1227 wsa881x->state = WSA881X_DEV_UP;
1228
1229 if (!debugfs_wsa881x_dent) {
1230 dbgwsa881x = wsa881x;
1231 debugfs_wsa881x_dent = debugfs_create_dir(
1232 "wsa881x_swr_slave", 0);
1233 if (!IS_ERR(debugfs_wsa881x_dent)) {
1234 debugfs_peek = debugfs_create_file("swrslave_peek",
1235 S_IFREG | 0444, debugfs_wsa881x_dent,
1236 (void *) "swrslave_peek",
1237 &codec_debug_ops);
1238
1239 debugfs_poke = debugfs_create_file("swrslave_poke",
1240 S_IFREG | 0444, debugfs_wsa881x_dent,
1241 (void *) "swrslave_poke",
1242 &codec_debug_ops);
1243
1244 debugfs_reg_dump = debugfs_create_file(
1245 "swrslave_reg_dump",
1246 S_IFREG | 0444,
1247 debugfs_wsa881x_dent,
1248 (void *) "swrslave_reg_dump",
1249 &codec_debug_ops);
1250 }
1251 }
1252
1253 /*
1254 * Add 5msec delay to provide sufficient time for
1255 * soundwire auto enumeration of slave devices as
1256 * as per HW requirement.
1257 */
1258 usleep_range(5000, 5010);
1259 ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
1260 if (ret) {
1261 dev_dbg(&pdev->dev,
1262 "%s get devnum %d for dev addr %lx failed\n",
1263 __func__, devnum, pdev->addr);
1264 goto dev_err;
1265 }
1266 pdev->dev_num = devnum;
1267
1268 wsa881x->regmap = devm_regmap_init_swr(pdev,
1269 &wsa881x_regmap_config);
1270 if (IS_ERR(wsa881x->regmap)) {
1271 ret = PTR_ERR(wsa881x->regmap);
1272 dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
1273 __func__, ret);
1274 goto dev_err;
1275 }
1276
1277 ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wsa881x,
1278 NULL, 0);
1279 if (ret) {
1280 dev_err(&pdev->dev, "%s: Codec registration failed\n",
1281 __func__);
1282 goto dev_err;
1283 }
1284
1285 return 0;
1286
1287dev_err:
1288 if (pin_state_current == false)
1289 wsa881x_gpio_ctrl(wsa881x, false);
1290 swr_remove_device(pdev);
1291err:
1292 return ret;
1293}
1294
1295static int wsa881x_swr_remove(struct swr_device *pdev)
1296{
1297 struct wsa881x_priv *wsa881x;
1298
1299 wsa881x = swr_get_dev_data(pdev);
1300 if (!wsa881x) {
1301 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1302 return -EINVAL;
1303 }
1304 debugfs_remove_recursive(debugfs_wsa881x_dent);
1305 debugfs_wsa881x_dent = NULL;
1306 snd_soc_unregister_codec(&pdev->dev);
1307 if (wsa881x->pd_gpio)
1308 gpio_free(wsa881x->pd_gpio);
1309 swr_set_dev_data(pdev, NULL);
1310 return 0;
1311}
1312
1313static int wsa881x_swr_up(struct swr_device *pdev)
1314{
1315 int ret;
1316 struct wsa881x_priv *wsa881x;
1317
1318 wsa881x = swr_get_dev_data(pdev);
1319 if (!wsa881x) {
1320 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1321 return -EINVAL;
1322 }
1323 ret = wsa881x_gpio_ctrl(wsa881x, true);
1324 if (ret)
1325 dev_err(&pdev->dev, "%s: Failed to enable gpio\n", __func__);
1326 else
1327 wsa881x->state = WSA881X_DEV_UP;
1328
1329 return ret;
1330}
1331
1332static int wsa881x_swr_down(struct swr_device *pdev)
1333{
1334 struct wsa881x_priv *wsa881x;
1335 int ret;
1336
1337 wsa881x = swr_get_dev_data(pdev);
1338 if (!wsa881x) {
1339 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1340 return -EINVAL;
1341 }
1342 if (delayed_work_pending(&wsa881x->ocp_ctl_work))
1343 cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
1344 ret = wsa881x_gpio_ctrl(wsa881x, false);
1345 if (ret)
1346 dev_err(&pdev->dev, "%s: Failed to disable gpio\n", __func__);
1347 else
1348 wsa881x->state = WSA881X_DEV_DOWN;
1349
1350 return ret;
1351}
1352
1353static int wsa881x_swr_reset(struct swr_device *pdev)
1354{
1355 struct wsa881x_priv *wsa881x;
1356 u8 retry = WSA881X_NUM_RETRY;
1357 u8 devnum = 0;
1358
1359 wsa881x = swr_get_dev_data(pdev);
1360 if (!wsa881x) {
1361 dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
1362 return -EINVAL;
1363 }
1364 wsa881x->bg_cnt = 0;
1365 wsa881x->clk_cnt = 0;
1366 while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
1367 /* Retry after 1 msec delay */
1368 usleep_range(1000, 1100);
1369 }
1370 pdev->dev_num = devnum;
1371 regcache_mark_dirty(wsa881x->regmap);
1372 regcache_sync(wsa881x->regmap);
1373 return 0;
1374}
1375
1376#ifdef CONFIG_PM_SLEEP
1377static int wsa881x_swr_suspend(struct device *dev)
1378{
1379 dev_dbg(dev, "%s: system suspend\n", __func__);
1380 return 0;
1381}
1382
1383static int wsa881x_swr_resume(struct device *dev)
1384{
1385 struct wsa881x_priv *wsa881x = swr_get_dev_data(to_swr_device(dev));
1386
1387 if (!wsa881x) {
1388 dev_err(dev, "%s: wsa881x private data is NULL\n", __func__);
1389 return -EINVAL;
1390 }
1391 dev_dbg(dev, "%s: system resume\n", __func__);
1392 return 0;
1393}
1394#endif /* CONFIG_PM_SLEEP */
1395
1396static const struct dev_pm_ops wsa881x_swr_pm_ops = {
1397 SET_SYSTEM_SLEEP_PM_OPS(wsa881x_swr_suspend, wsa881x_swr_resume)
1398};
1399
1400static const struct swr_device_id wsa881x_swr_id[] = {
1401 {"wsa881x", 0},
1402 {}
1403};
1404
1405static const struct of_device_id wsa881x_swr_dt_match[] = {
1406 {
1407 .compatible = "qcom,wsa881x",
1408 },
1409 {}
1410};
1411
1412static struct swr_driver wsa881x_codec_driver = {
1413 .driver = {
1414 .name = "wsa881x",
1415 .owner = THIS_MODULE,
1416 .pm = &wsa881x_swr_pm_ops,
1417 .of_match_table = wsa881x_swr_dt_match,
1418 },
1419 .probe = wsa881x_swr_probe,
1420 .remove = wsa881x_swr_remove,
1421 .id_table = wsa881x_swr_id,
1422 .device_up = wsa881x_swr_up,
1423 .device_down = wsa881x_swr_down,
1424 .reset_device = wsa881x_swr_reset,
1425};
1426
1427static int __init wsa881x_codec_init(void)
1428{
1429 return swr_driver_register(&wsa881x_codec_driver);
1430}
1431
1432static void __exit wsa881x_codec_exit(void)
1433{
1434 swr_driver_unregister(&wsa881x_codec_driver);
1435}
1436
1437module_init(wsa881x_codec_init);
1438module_exit(wsa881x_codec_exit);
1439
1440MODULE_DESCRIPTION("WSA881x Codec driver");
1441MODULE_LICENSE("GPL v2");