1 /*
2  *  intelmid_v2_control.c - Intel Sound card driver for MID
3  *
4  *  Copyright (C) 2008-10 Intel Corp
5  *  Authors:	Vinod Koul <vinod.koul@intel.com>
6  *		Harsha Priya <priya.harsha@intel.com>
7  *		KP Jeeja <jeeja.kp@intel.com>
8  *		Dharageswari R <dharageswari.r@intel.com>
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  *  This file contains the control operations of vendor 3
27  */
28 
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/file.h>
34 #include "intel_sst.h"
35 #include "intelmid_snd_control.h"
36 
37 enum reg_v3 {
38 	VAUDIOCNT = 0x51,
39 	VOICEPORT1 = 0x100,
40 	VOICEPORT2 = 0x101,
41 	AUDIOPORT1 = 0x102,
42 	AUDIOPORT2 = 0x103,
43 	ADCSAMPLERATE = 0x104,
44 	DMICCTRL1 = 0x105,
45 	DMICCTRL2 = 0x106,
46 	MICCTRL = 0x107,
47 	MICSELVOL = 0x108,
48 	LILSEL = 0x109,
49 	LIRSEL = 0x10a,
50 	VOICEVOL = 0x10b,
51 	AUDIOLVOL = 0x10c,
52 	AUDIORVOL = 0x10d,
53 	LMUTE = 0x10e,
54 	RMUTE = 0x10f,
55 	POWERCTRL1 = 0x110,
56 	POWERCTRL2 = 0x111,
57 	DRVPOWERCTRL = 0x112,
58 	VREFPLL = 0x113,
59 	PCMBUFCTRL = 0x114,
60 	SOFTMUTE = 0x115,
61 	DTMFPATH = 0x116,
62 	DTMFVOL = 0x117,
63 	DTMFFREQ = 0x118,
64 	DTMFHFREQ = 0x119,
65 	DTMFLFREQ = 0x11a,
66 	DTMFCTRL = 0x11b,
67 	DTMFASON = 0x11c,
68 	DTMFASOFF = 0x11d,
69 	DTMFASINUM = 0x11e,
70 	CLASSDVOL = 0x11f,
71 	VOICEDACAVOL = 0x120,
72 	AUDDACAVOL = 0x121,
73 	LOMUTEVOL = 0x122,
74 	HPLVOL = 0x123,
75 	HPRVOL = 0x124,
76 	MONOVOL = 0x125,
77 	LINEOUTMIXVOL = 0x126,
78 	EPMIXVOL = 0x127,
79 	LINEOUTLSEL = 0x128,
80 	LINEOUTRSEL = 0x129,
81 	EPMIXOUTSEL = 0x12a,
82 	HPLMIXSEL = 0x12b,
83 	HPRMIXSEL = 0x12c,
84 	LOANTIPOP = 0x12d,
85 };
86 
87 /****
88  * nc_init_card - initialize the sound card
89  *
90  * This initializes the audio paths to know values in case of this sound card
91  */
nc_init_card(void)92 static int nc_init_card(void)
93 {
94 	struct sc_reg_access sc_access[] = {
95 		{VAUDIOCNT, 0x25, 0},
96 		{VOICEPORT1, 0x00, 0},
97 		{VOICEPORT2, 0x00, 0},
98 		{AUDIOPORT1, 0x98, 0},
99 		{AUDIOPORT2, 0x09, 0},
100 		{AUDIOLVOL, 0x00, 0},
101 		{AUDIORVOL, 0x00, 0},
102 		{LMUTE, 0x03, 0},
103 		{RMUTE, 0x03, 0},
104 		{POWERCTRL1, 0x00, 0},
105 		{POWERCTRL2, 0x00, 0},
106 		{DRVPOWERCTRL, 0x00, 0},
107 		{VREFPLL, 0x10, 0},
108 		{HPLMIXSEL, 0xee, 0},
109 		{HPRMIXSEL, 0xf6, 0},
110 		{PCMBUFCTRL, 0x0, 0},
111 		{VOICEVOL, 0x0e, 0},
112 		{HPLVOL, 0x06, 0},
113 		{HPRVOL, 0x06, 0},
114 		{MICCTRL, 0x41, 0x00},
115 		{ADCSAMPLERATE, 0x8B, 0x00},
116 		{MICSELVOL, 0x5B, 0x00},
117 		{LILSEL, 0x06, 0},
118 		{LIRSEL, 0x46, 0},
119 		{LOANTIPOP, 0x00, 0},
120 		{DMICCTRL1, 0x40, 0},
121 	};
122 	snd_pmic_ops_nc.card_status = SND_CARD_INIT_DONE;
123 	snd_pmic_ops_nc.master_mute = UNMUTE;
124 	snd_pmic_ops_nc.mute_status = UNMUTE;
125 	sst_sc_reg_access(sc_access, PMIC_WRITE, 26);
126 	pr_debug("init complete!!\n");
127 	return 0;
128 }
129 
nc_enable_audiodac(int value)130 static int nc_enable_audiodac(int value)
131 {
132 	struct sc_reg_access sc_access[3];
133 	int mute_val = 0;
134 
135 	if (snd_pmic_ops_nc.mute_status == MUTE)
136 		return 0;
137 
138 	if (((snd_pmic_ops_nc.output_dev_id == MONO_EARPIECE) ||
139 		(snd_pmic_ops_nc.output_dev_id == INTERNAL_SPKR)) &&
140 		(value == UNMUTE))
141 		return 0;
142 	if (value == UNMUTE) {
143 			/* unmute the system, set the 7th bit to zero */
144 			mute_val = 0x00;
145 		} else {
146 			/* MUTE:Set the seventh bit */
147 			mute_val = 0x04;
148 
149 		}
150 		sc_access[0].reg_addr = LMUTE;
151 		sc_access[1].reg_addr = RMUTE;
152 		sc_access[0].mask = sc_access[1].mask = MASK2;
153 		sc_access[0].value = sc_access[1].value = mute_val;
154 
155 		if (snd_pmic_ops_nc.num_channel == 1)
156 			sc_access[1].value = 0x04;
157 		return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 2);
158 
159 }
160 
nc_power_up_pb(unsigned int port)161 static int nc_power_up_pb(unsigned int port)
162 {
163 	struct sc_reg_access sc_access[7];
164 	int retval = 0;
165 
166 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
167 		retval = nc_init_card();
168 	if (retval)
169 		return retval;
170 	if (port == 0xFF)
171 		return 0;
172 	nc_enable_audiodac(MUTE);
173 	msleep(30);
174 
175 	pr_debug("powering up pb....\n");
176 
177 	sc_access[0].reg_addr = VAUDIOCNT;
178 	sc_access[0].value = 0x27;
179 	sc_access[0].mask = 0x27;
180 	sc_access[1].reg_addr = VREFPLL;
181 	if (port == 0) {
182 		sc_access[1].value = 0x3A;
183 		sc_access[1].mask = 0x3A;
184 	} else if (port == 1) {
185 		sc_access[1].value = 0x35;
186 		sc_access[1].mask = 0x35;
187 	}
188 	retval =  sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 2);
189 
190 
191 
192 	sc_access[0].reg_addr = POWERCTRL1;
193 	if (port == 0) {
194 		sc_access[0].value = 0x40;
195 		sc_access[0].mask = 0x40;
196 	} else if (port == 1) {
197 		sc_access[0].value = 0x01;
198 		sc_access[0].mask = 0x01;
199 	}
200 	sc_access[1].reg_addr = POWERCTRL2;
201 	sc_access[1].value = 0x0C;
202 	sc_access[1].mask = 0x0C;
203 
204 	sc_access[2].reg_addr = DRVPOWERCTRL;
205 	sc_access[2].value = 0x86;
206 	sc_access[2].mask = 0x86;
207 
208 	sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 3);
209 
210 	msleep(30);
211 
212 	return nc_enable_audiodac(UNMUTE);
213 
214 }
215 
nc_power_up_cp(unsigned int port)216 static int nc_power_up_cp(unsigned int port)
217 {
218 	struct sc_reg_access sc_access[5];
219 	int retval = 0;
220 
221 
222 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
223 		retval = nc_init_card();
224 	if (retval)
225 		return retval;
226 
227 
228 	pr_debug("powering up cp....\n");
229 
230 	if (port == 0xFF)
231 		return 0;
232 	sc_access[0].reg_addr = VAUDIOCNT;
233 	sc_access[0].value = 0x27;
234 	sc_access[0].mask = 0x27;
235 	sc_access[1].reg_addr = VREFPLL;
236 	if (port == 0) {
237 		sc_access[1].value = 0x3E;
238 		sc_access[1].mask = 0x3E;
239 	} else if (port == 1) {
240 		sc_access[1].value = 0x35;
241 		sc_access[1].mask = 0x35;
242 	}
243 
244 	retval =  sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 2);
245 
246 
247 	sc_access[0].reg_addr = POWERCTRL1;
248 	if (port == 0) {
249 		sc_access[0].value = 0xB4;
250 		sc_access[0].mask = 0xB4;
251 	} else if (port == 1) {
252 		sc_access[0].value = 0xBF;
253 		sc_access[0].mask = 0xBF;
254 	}
255 	sc_access[1].reg_addr = POWERCTRL2;
256 	if (port == 0) {
257 		sc_access[1].value = 0x0C;
258 		sc_access[1].mask = 0x0C;
259 	} else if (port == 1) {
260 		sc_access[1].value = 0x02;
261 		sc_access[1].mask = 0x02;
262 	}
263 
264 	return  sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 2);
265 
266 }
267 
nc_power_down(void)268 static int nc_power_down(void)
269 {
270 	int retval = 0;
271 	struct sc_reg_access sc_access[5];
272 
273 
274 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
275 		retval = nc_init_card();
276 	if (retval)
277 		return retval;
278 	nc_enable_audiodac(MUTE);
279 
280 
281 	pr_debug("powering dn nc_power_down ....\n");
282 
283 	msleep(30);
284 
285 	sc_access[0].reg_addr = DRVPOWERCTRL;
286 	sc_access[0].value = 0x00;
287 	sc_access[0].mask = 0x00;
288 
289 	sst_sc_reg_access(sc_access, PMIC_WRITE, 1);
290 
291 	sc_access[0].reg_addr = POWERCTRL1;
292 	sc_access[0].value = 0x00;
293 	sc_access[0].mask = 0x00;
294 
295 	sc_access[1].reg_addr = POWERCTRL2;
296 	sc_access[1].value = 0x00;
297 	sc_access[1].mask = 0x00;
298 
299 
300 
301 	sst_sc_reg_access(sc_access, PMIC_WRITE, 2);
302 
303 	msleep(30);
304 	sc_access[0].reg_addr = VREFPLL;
305 	sc_access[0].value = 0x10;
306 	sc_access[0].mask = 0x10;
307 
308 	sc_access[1].reg_addr = VAUDIOCNT;
309 	sc_access[1].value = 0x25;
310 	sc_access[1].mask = 0x25;
311 
312 
313 	retval =  sst_sc_reg_access(sc_access, PMIC_WRITE, 2);
314 
315 	msleep(30);
316 	return nc_enable_audiodac(UNMUTE);
317 }
318 
nc_power_down_pb(void)319 static int nc_power_down_pb(void)
320 {
321 
322 	int retval = 0;
323 	struct sc_reg_access sc_access[5];
324 
325 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
326 		retval = nc_init_card();
327 	if (retval)
328 		return retval;
329 
330 	pr_debug("powering dn pb....\n");
331 
332 	nc_enable_audiodac(MUTE);
333 
334 
335 	msleep(30);
336 
337 
338 	sc_access[0].reg_addr = DRVPOWERCTRL;
339 	sc_access[0].value = 0x00;
340 	sc_access[0].mask = 0x00;
341 
342 	sst_sc_reg_access(sc_access, PMIC_WRITE, 1);
343 
344 	msleep(30);
345 
346 	sc_access[0].reg_addr = POWERCTRL1;
347 	sc_access[0].value = 0x00;
348 	sc_access[0].mask = 0x41;
349 
350 	sc_access[1].reg_addr = POWERCTRL2;
351 	sc_access[1].value = 0x00;
352 	sc_access[1].mask = 0x0C;
353 
354 	sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 2);
355 
356 	msleep(30);
357 
358 	return nc_enable_audiodac(UNMUTE);
359 
360 
361 }
362 
nc_power_down_cp(void)363 static int nc_power_down_cp(void)
364 {
365 	struct sc_reg_access sc_access[] = {
366 		{POWERCTRL1, 0x00, 0xBE},
367 		{POWERCTRL2, 0x00, 0x02},
368 	};
369 	int retval = 0;
370 
371 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
372 		retval = nc_init_card();
373 	if (retval)
374 		return retval;
375 
376 	pr_debug("powering dn cp....\n");
377 	return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
378 }
379 
nc_set_pcm_voice_params(void)380 static int nc_set_pcm_voice_params(void)
381 {
382 	struct sc_reg_access sc_access[] = {
383 			{0x100, 0xD5, 0},
384 			{0x101, 0x08, 0},
385 			{0x104, 0x03, 0},
386 			{0x107, 0x10, 0},
387 			{0x10B, 0x0E, 0},
388 			{0x10E, 0x03, 0},
389 			{0x10F, 0x03, 0},
390 			{0x114, 0x13, 0},
391 			{0x115, 0x00, 0},
392 			{0x128, 0xFE, 0},
393 			{0x129, 0xFE, 0},
394 			{0x12A, 0xFE, 0},
395 			{0x12B, 0xDE, 0},
396 			{0x12C, 0xDE, 0},
397 	};
398 	int retval = 0;
399 
400 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
401 		retval = nc_init_card();
402 	if (retval)
403 		return retval;
404 
405 	sst_sc_reg_access(sc_access, PMIC_WRITE, 14);
406 	pr_debug("Voice parameters set successfully!!\n");
407 	return 0;
408 }
409 
410 
nc_set_pcm_audio_params(int sfreq,int word_size,int num_channel)411 static int nc_set_pcm_audio_params(int sfreq, int word_size, int num_channel)
412 {
413 	int config2 = 0;
414 	struct sc_reg_access sc_access;
415 	int retval = 0;
416 
417 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
418 		retval = nc_init_card();
419 	if (retval)
420 		return retval;
421 
422 	switch (sfreq) {
423 	case 8000:
424 		config2 = 0x00;
425 		break;
426 	case 11025:
427 		config2 = 0x01;
428 		break;
429 	case 12000:
430 		config2 = 0x02;
431 		break;
432 	case 16000:
433 		config2 = 0x03;
434 		break;
435 	case 22050:
436 		config2 = 0x04;
437 		break;
438 	case 24000:
439 		config2 = 0x05;
440 		break;
441 	case 32000:
442 		config2 = 0x07;
443 		break;
444 	case 44100:
445 		config2 = 0x08;
446 		break;
447 	case 48000:
448 		config2 = 0x09;
449 		break;
450 	}
451 
452 	snd_pmic_ops_nc.num_channel = num_channel;
453 	if (snd_pmic_ops_nc.num_channel == 1)	{
454 
455 		sc_access.value = 0x07;
456 		sc_access.reg_addr = RMUTE;
457 		pr_debug("RIGHT_HP_MUTE value%d\n", sc_access.value);
458 		sc_access.mask = MASK2;
459 		sst_sc_reg_access(&sc_access, PMIC_READ_MODIFY, 1);
460 	} else {
461 		sc_access.value = 0x00;
462 		sc_access.reg_addr = RMUTE;
463 		pr_debug("RIGHT_HP_MUTE value %d\n", sc_access.value);
464 		sc_access.mask = MASK2;
465 		sst_sc_reg_access(&sc_access, PMIC_READ_MODIFY, 1);
466 
467 
468 	}
469 
470 	pr_debug("word_size = %d\n", word_size);
471 
472 	if (word_size == 24) {
473 		sc_access.reg_addr = AUDIOPORT2;
474 		sc_access.value = config2 | 0x10;
475 		sc_access.mask = 0x1F;
476 	} else {
477 		sc_access.value = config2;
478 		sc_access.mask = 0x1F;
479 		sc_access.reg_addr = AUDIOPORT2;
480 	}
481 	sst_sc_reg_access(&sc_access, PMIC_READ_MODIFY, 1);
482 
483 	pr_debug("word_size = %d\n", word_size);
484 	sc_access.reg_addr = AUDIOPORT1;
485 	sc_access.mask = MASK5|MASK4|MASK1|MASK0;
486 	if (word_size == 16)
487 		sc_access.value = 0x98;
488 	else if (word_size == 24)
489 		sc_access.value = 0xAB;
490 
491 	return sst_sc_reg_access(&sc_access, PMIC_READ_MODIFY, 1);
492 
493 
494 
495 }
496 
nc_set_selected_output_dev(u8 value)497 static int nc_set_selected_output_dev(u8 value)
498 {
499 	struct sc_reg_access sc_access_HP[] = {
500 		{LMUTE, 0x02, 0x06},
501 		{RMUTE, 0x02, 0x06}
502 	};
503 	struct sc_reg_access sc_access_IS[] = {
504 		{LMUTE, 0x04, 0x06},
505 		{RMUTE, 0x04, 0x06}
506 	};
507 	int retval = 0;
508 
509 	snd_pmic_ops_nc.output_dev_id = value;
510 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
511 		retval = nc_init_card();
512 	if (retval)
513 		return retval;
514 	pr_debug("nc set selected output:%d\n", value);
515 	switch (value) {
516 	case STEREO_HEADPHONE:
517 		retval = sst_sc_reg_access(sc_access_HP, PMIC_WRITE, 2);
518 		break;
519 	case INTERNAL_SPKR:
520 		retval = sst_sc_reg_access(sc_access_IS, PMIC_WRITE, 2);
521 		break;
522 	default:
523 		pr_err("rcvd illegal request: %d\n", value);
524 		return -EINVAL;
525 	}
526 	return retval;
527 }
528 
nc_audio_init(void)529 static int nc_audio_init(void)
530 {
531 	struct sc_reg_access sc_acces, sc_access[] = {
532 			{0x100, 0x00, 0},
533 			{0x101, 0x00, 0},
534 			{0x104, 0x8B, 0},
535 			{0x107, 0x11, 0},
536 			{0x10B, 0x0E, 0},
537 			{0x114, 0x00, 0},
538 			{0x115, 0x00, 0},
539 			{0x128, 0x00, 0},
540 			{0x129, 0x00, 0},
541 			{0x12A, 0x00, 0},
542 			{0x12B, 0xee, 0},
543 			{0x12C, 0xf6, 0},
544 	};
545 
546 	sst_sc_reg_access(sc_access, PMIC_WRITE, 12);
547 	pr_debug("Audio Init successfully!!\n");
548 
549 	/*set output device */
550 	nc_set_selected_output_dev(snd_pmic_ops_nc.output_dev_id);
551 
552 	if (snd_pmic_ops_nc.num_channel == 1) {
553 		sc_acces.value = 0x07;
554 		sc_acces.reg_addr = RMUTE;
555 		pr_debug("RIGHT_HP_MUTE value%d\n", sc_acces.value);
556 		sc_acces.mask = MASK2;
557 		sst_sc_reg_access(&sc_acces, PMIC_READ_MODIFY, 1);
558 	} else {
559 		sc_acces.value = 0x00;
560 		sc_acces.reg_addr = RMUTE;
561 		pr_debug("RIGHT_HP_MUTE value%d\n", sc_acces.value);
562 		sc_acces.mask = MASK2;
563 		sst_sc_reg_access(&sc_acces, PMIC_READ_MODIFY, 1);
564 	}
565 
566 	return 0;
567 }
568 
nc_set_audio_port(int status)569 static int nc_set_audio_port(int status)
570 {
571 	struct sc_reg_access sc_access[2] = {{0,},};
572 	int retval = 0;
573 
574 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
575 		retval = nc_init_card();
576 	if (retval)
577 		return retval;
578 
579 	if (status == DEACTIVATE) {
580 		/* Deactivate audio port-tristate and power */
581 		sc_access[0].value = 0x00;
582 		sc_access[0].mask = MASK4|MASK5;
583 		sc_access[0].reg_addr = AUDIOPORT1;
584 		return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
585 	} else if (status == ACTIVATE) {
586 		/* activate audio port */
587 		nc_audio_init();
588 		sc_access[0].value = 0x10;
589 		sc_access[0].mask =  MASK4|MASK5 ;
590 		sc_access[0].reg_addr = AUDIOPORT1;
591 		return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
592 	} else
593 		return -EINVAL;
594 
595 }
596 
nc_set_voice_port(int status)597 static int nc_set_voice_port(int status)
598 {
599 	struct sc_reg_access sc_access[2] = {{0,},};
600 	int retval = 0;
601 
602 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
603 		retval = nc_init_card();
604 	if (retval)
605 		return retval;
606 
607 	if (status == DEACTIVATE) {
608 		/* Activate Voice port */
609 		sc_access[0].value = 0x00;
610 		sc_access[0].mask = MASK4;
611 		sc_access[0].reg_addr = VOICEPORT1;
612 		return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
613 	} else if (status == ACTIVATE) {
614 		/* Deactivate voice port */
615 		nc_set_pcm_voice_params();
616 		sc_access[0].value = 0x10;
617 		sc_access[0].mask = MASK4;
618 		sc_access[0].reg_addr = VOICEPORT1;
619 		return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
620 	} else
621 		return -EINVAL;
622 }
623 
nc_set_mute(int dev_id,u8 value)624 static int nc_set_mute(int dev_id, u8 value)
625 {
626 	struct sc_reg_access sc_access[3];
627 	u8 mute_val, cap_mute;
628 	int retval = 0;
629 
630 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
631 		retval = nc_init_card();
632 	if (retval)
633 		return retval;
634 
635 	pr_debug("set device id::%d, value %d\n", dev_id, value);
636 
637 	switch (dev_id) {
638 	case PMIC_SND_MUTE_ALL:
639 		pr_debug("PMIC_SND_MUTE_ALL value %d\n", value);
640 		snd_pmic_ops_nc.mute_status = value;
641 		snd_pmic_ops_nc.master_mute = value;
642 		if (value == UNMUTE) {
643 			/* unmute the system, set the 7th bit to zero */
644 			mute_val = cap_mute = 0x00;
645 		} else {
646 			/* MUTE:Set the seventh bit */
647 			mute_val = 0x80;
648 			cap_mute = 0x40;
649 		}
650 		sc_access[0].reg_addr = AUDIOLVOL;
651 		sc_access[1].reg_addr = AUDIORVOL;
652 		sc_access[0].mask = sc_access[1].mask = MASK7;
653 		sc_access[0].value = sc_access[1].value = mute_val;
654 		if (snd_pmic_ops_nc.num_channel == 1)
655 				sc_access[1].value = 0x80;
656 		if (!sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 2)) {
657 			sc_access[0].reg_addr = 0x109;
658 			sc_access[1].reg_addr = 0x10a;
659 			sc_access[2].reg_addr = 0x105;
660 			sc_access[0].mask = sc_access[1].mask =
661 				sc_access[2].mask = MASK6;
662 			sc_access[0].value = sc_access[1].value =
663 				sc_access[2].value = cap_mute;
664 
665 			if ((snd_pmic_ops_nc.input_dev_id == AMIC) ||
666 				(snd_pmic_ops_nc.input_dev_id == DMIC))
667 					sc_access[1].value = 0x40;
668 			if (snd_pmic_ops_nc.input_dev_id == HS_MIC)
669 					sc_access[0].value = 0x40;
670 			retval = sst_sc_reg_access(sc_access,
671 					PMIC_READ_MODIFY, 3);
672 		}
673 		break;
674 	case PMIC_SND_HP_MIC_MUTE:
675 		pr_debug("PMIC_SND_HPMIC_MUTE value %d\n", value);
676 		if (value == UNMUTE) {
677 			/* unmute the system, set the 6th bit to one */
678 			sc_access[0].value = 0x00;
679 		} else {
680 			/* mute the system, reset the 6th bit to zero */
681 			sc_access[0].value = 0x40;
682 		}
683 		sc_access[0].reg_addr = LIRSEL;
684 		sc_access[0].mask = MASK6;
685 		retval = sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
686 		break;
687 	case PMIC_SND_AMIC_MUTE:
688 		pr_debug("PMIC_SND_AMIC_MUTE value %d\n", value);
689 		if (value == UNMUTE) {
690 			/* unmute the system, set the 6th bit to one */
691 			sc_access[0].value = 0x00;
692 		} else {
693 			/* mute the system, reset the 6th bit to zero */
694 			sc_access[0].value = 0x40;
695 		}
696 		sc_access[0].reg_addr = LILSEL;
697 		sc_access[0].mask = MASK6;
698 		retval = sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
699 		break;
700 
701 	case PMIC_SND_DMIC_MUTE:
702 		pr_debug("INPUT_MUTE_DMIC value%d\n", value);
703 		if (value == UNMUTE) {
704 			/* unmute the system, set the 6th bit to one */
705 			sc_access[1].value = 0x00;
706 			sc_access[0].value = 0x00;
707 		} else {
708 			/* mute the system, reset the 6th bit to zero */
709 			sc_access[1].value = 0x40;
710 			sc_access[0].value = 0x40;
711 		}
712 		sc_access[0].reg_addr = DMICCTRL1;
713 		sc_access[0].mask = MASK6;
714 		sc_access[1].reg_addr = LILSEL;
715 		sc_access[1].mask = MASK6;
716 		retval = sst_sc_reg_access(sc_access,
717 					PMIC_READ_MODIFY, 2);
718 		break;
719 
720 	case PMIC_SND_LEFT_HP_MUTE:
721 	case PMIC_SND_RIGHT_HP_MUTE:
722 		snd_pmic_ops_nc.mute_status = value;
723 		if (value == UNMUTE)
724 			sc_access[0].value = 0x0;
725 		else
726 			sc_access[0].value = 0x04;
727 
728 		if (dev_id == PMIC_SND_LEFT_HP_MUTE) {
729 			sc_access[0].reg_addr = LMUTE;
730 			pr_debug("LEFT_HP_MUTE value %d\n",
731 					sc_access[0].value);
732 		} else {
733 			if (snd_pmic_ops_nc.num_channel == 1)
734 				sc_access[0].value = 0x04;
735 			sc_access[0].reg_addr = RMUTE;
736 			pr_debug("RIGHT_HP_MUTE value %d\n",
737 					sc_access[0].value);
738 		}
739 		sc_access[0].mask = MASK2;
740 		retval = sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
741 		break;
742 	case PMIC_SND_LEFT_SPEAKER_MUTE:
743 	case PMIC_SND_RIGHT_SPEAKER_MUTE:
744 		if (value == UNMUTE)
745 			sc_access[0].value = 0x00;
746 		else
747 			sc_access[0].value = 0x03;
748 		sc_access[0].reg_addr = LMUTE;
749 		pr_debug("SPEAKER_MUTE %d\n", sc_access[0].value);
750 		sc_access[0].mask = MASK1;
751 		retval = sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, 1);
752 		break;
753 	default:
754 		return -EINVAL;
755 	}
756 	return retval ;
757 
758 }
759 
nc_set_vol(int dev_id,int value)760 static int nc_set_vol(int dev_id, int value)
761 {
762 	struct sc_reg_access sc_access[3];
763 	int retval = 0, entries = 0;
764 
765 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
766 		retval = nc_init_card();
767 	if (retval)
768 		return retval;
769 
770 	pr_debug("set volume:%d\n", dev_id);
771 	switch (dev_id) {
772 	case PMIC_SND_CAPTURE_VOL:
773 		pr_debug("PMIC_SND_CAPTURE_VOL:value::%d\n", value);
774 		sc_access[0].value = sc_access[1].value =
775 					sc_access[2].value = -value;
776 		sc_access[0].mask = sc_access[1].mask = sc_access[2].mask =
777 					(MASK0|MASK1|MASK2|MASK3|MASK4|MASK5);
778 		sc_access[0].reg_addr = 0x10a;
779 		sc_access[1].reg_addr = 0x109;
780 		sc_access[2].reg_addr = 0x105;
781 		entries = 3;
782 		break;
783 
784 	case PMIC_SND_LEFT_PB_VOL:
785 		pr_debug("PMIC_SND_LEFT_HP_VOL %d\n", value);
786 		sc_access[0].value = -value;
787 		sc_access[0].reg_addr  = AUDIOLVOL;
788 		sc_access[0].mask =
789 			(MASK0|MASK1|MASK2|MASK3|MASK4|MASK5|MASK6);
790 		entries = 1;
791 		break;
792 
793 	case PMIC_SND_RIGHT_PB_VOL:
794 		pr_debug("PMIC_SND_RIGHT_HP_VOL value %d\n", value);
795 		if (snd_pmic_ops_nc.num_channel == 1) {
796 			sc_access[0].value = 0x04;
797 		    sc_access[0].reg_addr = RMUTE;
798 			sc_access[0].mask = MASK2;
799 		} else {
800 		sc_access[0].value = -value;
801 		sc_access[0].reg_addr  = AUDIORVOL;
802 		sc_access[0].mask =
803 				(MASK0|MASK1|MASK2|MASK3|MASK4|MASK5|MASK6);
804 		entries = 1;
805 		}
806 		break;
807 
808 	default:
809 		return -EINVAL;
810 
811 	}
812 	return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, entries);
813 }
814 
nc_set_selected_input_dev(u8 value)815 static int nc_set_selected_input_dev(u8 value)
816 {
817 	struct sc_reg_access sc_access[6];
818 	u8 num_val;
819 	int retval = 0;
820 
821 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
822 		retval = nc_init_card();
823 	if (retval)
824 		return retval;
825 	snd_pmic_ops_nc.input_dev_id = value;
826 
827 	pr_debug("nc set selected input:%d\n", value);
828 
829 	switch (value) {
830 	case AMIC:
831 		pr_debug("Selecting AMIC\n");
832 		sc_access[0].reg_addr = 0x107;
833 		sc_access[0].value = 0x40;
834 		sc_access[0].mask =  MASK6|MASK4|MASK3|MASK1|MASK0;
835 		sc_access[1].reg_addr = 0x10a;
836 		sc_access[1].value = 0x40;
837 		sc_access[1].mask = MASK6;
838 		sc_access[2].reg_addr = 0x109;
839 		sc_access[2].value = 0x00;
840 		sc_access[2].mask = MASK6;
841 		sc_access[3].reg_addr = 0x105;
842 		sc_access[3].value = 0x40;
843 		sc_access[3].mask = MASK6;
844 		num_val = 4;
845 		break;
846 
847 	case HS_MIC:
848 		pr_debug("Selecting HS_MIC\n");
849 		sc_access[0].reg_addr = 0x107;
850 		sc_access[0].mask =  MASK6|MASK4|MASK3|MASK1|MASK0;
851 		sc_access[0].value = 0x10;
852 		sc_access[1].reg_addr = 0x109;
853 		sc_access[1].mask = MASK6;
854 		sc_access[1].value = 0x40;
855 		sc_access[2].reg_addr = 0x10a;
856 		sc_access[2].mask = MASK6;
857 		sc_access[2].value = 0x00;
858 		sc_access[3].reg_addr = 0x105;
859 		sc_access[3].value = 0x40;
860 		sc_access[3].mask = MASK6;
861 		num_val = 4;
862 		break;
863 
864 	case DMIC:
865 		pr_debug("DMIC\n");
866 		sc_access[0].reg_addr = 0x107;
867 		sc_access[0].mask = MASK6|MASK4|MASK3|MASK1|MASK0;
868 		sc_access[0].value = 0x0B;
869 		sc_access[1].reg_addr = 0x105;
870 		sc_access[1].value = 0x80;
871 		sc_access[1].mask = MASK7|MASK6;
872 		sc_access[2].reg_addr = 0x10a;
873 		sc_access[2].value = 0x40;
874 		sc_access[2].mask = MASK6;
875 		sc_access[3].reg_addr = 0x109;
876 		sc_access[3].mask = MASK6;
877 		sc_access[3].value = 0x00;
878 		sc_access[4].reg_addr = 0x104;
879 		sc_access[4].value = 0x3C;
880 		sc_access[4].mask = 0xff;
881 		num_val = 5;
882 		break;
883 	default:
884 		return -EINVAL;
885 	}
886 	return sst_sc_reg_access(sc_access, PMIC_READ_MODIFY, num_val);
887 }
888 
nc_get_mute(int dev_id,u8 * value)889 static int nc_get_mute(int dev_id, u8 *value)
890 {
891 	int retval = 0, mask = 0;
892 	struct sc_reg_access sc_access = {0,};
893 
894 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
895 		retval = nc_init_card();
896 	if (retval)
897 		return retval;
898 
899 	pr_debug("get mute::%d\n", dev_id);
900 
901 	switch (dev_id) {
902 	case PMIC_SND_AMIC_MUTE:
903 		pr_debug("PMIC_SND_INPUT_MUTE_MIC1\n");
904 		sc_access.reg_addr = LILSEL;
905 		mask = MASK6;
906 		break;
907 	case PMIC_SND_HP_MIC_MUTE:
908 		pr_debug("PMIC_SND_INPUT_MUTE_MIC2\n");
909 		sc_access.reg_addr = LIRSEL;
910 		mask = MASK6;
911 		break;
912 	case PMIC_SND_LEFT_HP_MUTE:
913 	case PMIC_SND_RIGHT_HP_MUTE:
914 		mask = MASK2;
915 		pr_debug("PMIC_SN_LEFT/RIGHT_HP_MUTE\n");
916 		if (dev_id == PMIC_SND_RIGHT_HP_MUTE)
917 			sc_access.reg_addr = RMUTE;
918 		else
919 			sc_access.reg_addr = LMUTE;
920 		break;
921 
922 	case PMIC_SND_LEFT_SPEAKER_MUTE:
923 		pr_debug("PMIC_MONO_EARPIECE_MUTE\n");
924 		sc_access.reg_addr = RMUTE;
925 		mask = MASK1;
926 		break;
927 	case PMIC_SND_DMIC_MUTE:
928 		pr_debug("PMIC_SND_INPUT_MUTE_DMIC\n");
929 		sc_access.reg_addr = 0x105;
930 		mask = MASK6;
931 		break;
932 	default:
933 		return -EINVAL;
934 
935 	}
936 	retval = sst_sc_reg_access(&sc_access, PMIC_READ, 1);
937 	pr_debug("reg value = %d\n", sc_access.value);
938 	if (retval)
939 		return retval;
940 	*value = (sc_access.value) & mask;
941 	pr_debug("masked value = %d\n", *value);
942 	if (*value)
943 		*value = 0;
944 	else
945 		*value = 1;
946 	pr_debug("value returned = 0x%x\n", *value);
947 	return retval;
948 }
949 
nc_get_vol(int dev_id,int * value)950 static int nc_get_vol(int dev_id, int *value)
951 {
952 	int retval = 0, mask = 0;
953 	struct sc_reg_access sc_access = {0,};
954 
955 	if (snd_pmic_ops_nc.card_status == SND_CARD_UN_INIT)
956 		retval = nc_init_card();
957 	if (retval)
958 		return retval;
959 
960 	switch (dev_id) {
961 	case PMIC_SND_CAPTURE_VOL:
962 		pr_debug("PMIC_SND_INPUT_CAPTURE_VOL\n");
963 		sc_access.reg_addr =  LILSEL;
964 		mask = (MASK0|MASK1|MASK2|MASK3|MASK4|MASK5);
965 		break;
966 
967 	case PMIC_SND_RIGHT_PB_VOL:
968 		pr_debug("GET_VOLUME_PMIC_LEFT_HP_VOL\n");
969 		sc_access.reg_addr = AUDIOLVOL;
970 		mask = (MASK0|MASK1|MASK2|MASK3|MASK4|MASK5|MASK6);
971 		break;
972 
973 	case PMIC_SND_LEFT_PB_VOL:
974 		pr_debug("GET_VOLUME_PMIC_RIGHT_HP_VOL\n");
975 		sc_access.reg_addr = AUDIORVOL;
976 		mask = (MASK0|MASK1|MASK2|MASK3|MASK4|MASK5|MASK6);
977 		break;
978 
979 	default:
980 		return -EINVAL;
981 
982 	}
983 	retval = sst_sc_reg_access(&sc_access, PMIC_READ, 1);
984 	pr_debug("value read = 0x%x\n", sc_access.value);
985 	*value = -((sc_access.value) & mask);
986 	pr_debug("get vol value returned = %d\n", *value);
987 	return retval;
988 }
989 
990 struct snd_pmic_ops snd_pmic_ops_nc = {
991 	.set_input_dev	=	nc_set_selected_input_dev,
992 	.set_output_dev =	nc_set_selected_output_dev,
993 	.set_mute	=	nc_set_mute,
994 	.get_mute	=	nc_get_mute,
995 	.set_vol	=	nc_set_vol,
996 	.get_vol	=	nc_get_vol,
997 	.init_card	=	nc_init_card,
998 	.set_pcm_audio_params	= nc_set_pcm_audio_params,
999 	.set_pcm_voice_params	= nc_set_pcm_voice_params,
1000 	.set_voice_port = nc_set_voice_port,
1001 	.set_audio_port = nc_set_audio_port,
1002 	.power_up_pmic_pb =	nc_power_up_pb,
1003 	.power_up_pmic_cp =	nc_power_up_cp,
1004 	.power_down_pmic_pb =	nc_power_down_pb,
1005 	.power_down_pmic_cp =	nc_power_down_cp,
1006 	.power_down_pmic =	nc_power_down,
1007 };
1008