sound: soc: codecs: update the TI codec driver for K39
[linux-2.6.git] / sound / soc / codecs / tlv320aic326x_mini-dsp.c
1 /*
2  * linux/sound/soc/codecs/tlv320aic326x_mini-dsp.c
3  *
4  * Copyright (C) 2012 Texas Instruments, Inc.
5  *
6  * This package is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
11  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * The TLV320AIC3262 is a flexible, low-power, low-voltage stereo audio
15  * codec with digital microphone inputs and programmable outputs.
16  *
17  * History:
18  *
19  * Rev 0.1   Added the miniDSP Support     01-03-2011
20  *
21  * Rev 0.2   Updated the code-base for miniDSP switching and
22  *     mux control update.    21-03-2011
23  *
24  * Rev 0.3   Updated the code-base to support Multi-Configuration feature
25  *           of PPS GDE
26  */
27
28 /*
29  *****************************************************************************
30  * INCLUDES
31  *****************************************************************************
32  */
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/fs.h>
38 #include <linux/types.h>
39 #include <linux/kdev_t.h>
40 #include <linux/cdev.h>
41 #include <linux/device.h>
42 #include <linux/io.h>
43 #include <linux/delay.h>
44 #include <linux/i2c.h>
45 #include <linux/platform_device.h>
46 #include <sound/soc.h>
47 #include <sound/core.h>
48 #include <sound/soc-dapm.h>
49 #include <sound/control.h>
50 #include <linux/time.h>         /* For timing computations */
51 #include "tlv320aic326x.h"
52 #include "tlv320aic326x_mini-dsp.h"
53
54 #include "first_rate_pps_driver.h"
55 #include "second_rate_pps_driver.h"
56
57 #ifdef CONFIG_MINI_DSP
58
59 #ifdef REG_DUMP_MINIDSP
60 static void aic3262_dump_page(struct i2c_client *i2c, u8 page);
61 #endif
62
63 /*
64  *****************************************************************************
65  * LOCAL STATIC DECLARATIONS
66  *****************************************************************************
67  */
68 static int m_control_info(struct snd_kcontrol *kcontrol,
69                               struct snd_ctl_elem_info *uinfo);
70 static int m_control_get(struct snd_kcontrol *kcontrol,
71                              struct snd_ctl_elem_value *ucontrol);
72 static int m_control_put(struct snd_kcontrol *kcontrol,
73                              struct snd_ctl_elem_value *ucontrol);
74
75 /*
76  *****************************************************************************
77  * MINIDSP RELATED GLOBALS
78  *****************************************************************************
79  */
80 /* The below variable is used to maintain the I2C Transactions
81  * to be carried out during miniDSP switching.
82  */
83 minidsp_parser_data dsp_parse_data[MINIDSP_PARSER_ARRAY_SIZE*2];
84
85 struct i2c_msg i2c_transaction[MINIDSP_PARSER_ARRAY_SIZE * 2];
86 /* Total count of I2C Messages are stored in the i2c_count */
87 int i2c_count;
88
89 /* The below array is used to store the burst array for I2C Multibyte
90  * Operations
91  */
92 minidsp_i2c_page i2c_page_array[MINIDSP_PARSER_ARRAY_SIZE];
93 int i2c_page_count;
94
95 /* kcontrol structure used to register with ALSA Core layer */
96 static struct snd_kcontrol_new snd_mux_controls[MAX_MUX_CONTROLS];
97
98 /* mode variables */
99 static int amode;
100 static int dmode;
101
102 /* k-control macros used for miniDSP related Kcontrols */
103 #define SOC_SINGLE_VALUE_M(xmax, xinvert) \
104         ((unsigned long)&(struct soc_mixer_control) \
105         {.max = xmax, \
106         .invert = xinvert})
107 #define SOC_SINGLE_M(xname, max, invert) \
108 {\
109         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
110         .info = m_control_info, .get = m_control_get,\
111         .put = m_control_put, \
112         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
113         .private_value = SOC_SINGLE_VALUE_M(max, invert) }
114 #define SOC_SINGLE_AIC3262_M(xname) \
115 {\
116         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
117         .info = m_control_info, .get = m_control_get,\
118         .put = m_control_put, \
119         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
120 }
121
122 /*
123  * aic3262_minidsp_controls
124  *
125  * Contains the list of the Kcontrol macros required for modifying the
126  * miniDSP behavior at run-time.
127  */
128 static const struct snd_kcontrol_new aic3262_minidsp_controls[] = {
129         SOC_SINGLE_AIC3262_M("Minidsp mode") ,
130         SOC_SINGLE_AIC3262_M("ADC Adaptive mode Enable") ,
131         SOC_SINGLE_AIC3262_M("DAC Adaptive mode Enable") ,
132         SOC_SINGLE_AIC3262_M("Dump Regs Book0") ,
133         SOC_SINGLE_AIC3262_M("Verify minidsp program") ,
134 };
135
136 #ifdef REG_DUMP_MINIDSP
137 /*
138  *----------------------------------------------------------------------------
139  * Function : aic3262_dump_page
140  * Purpose  : Read and display one codec register page, for debugging purpose
141  *----------------------------------------------------------------------------
142  */
143 static void aic3262_dump_page(struct i2c_client *i2c, u8 page)
144 {
145         int i;
146         u8 data;
147         u8 test_page_array[256];
148
149         aic3262_change_page(codec, page);
150
151         data = 0x0;
152
153         i2c_master_send(i2c, data, 1);
154         i2c_master_recv(i2c, test_page_array, 128);
155
156         DBG("\n------- MINI_DSP PAGE %d DUMP --------\n", page);
157         for (i = 0; i < 128; i++)
158                 DBG(KERN_INFO " [ %d ] = 0x%x\n", i, test_page_array[i]);
159
160 }
161 #endif
162
163 /*
164  *----------------------------------------------------------------------------
165  * Function : update_kcontrols
166  * Purpose  : Given the miniDSP process flow, this function reads the
167  *            corresponding Page Numbers and then performs I2C Read for those
168  *            Pages.
169  *----------------------------------------------------------------------------
170  */
171 void update_kcontrols(struct snd_soc_codec *codec, int process_flow)
172 {
173         int i, val1, array_size;
174         char **knames;
175         control *cntl;
176
177         if (process_flow == 1) {
178                 knames = Second_Rate_MUX_control_names;
179                 cntl = Second_Rate_MUX_controls;
180                 array_size = ARRAY_SIZE(Second_Rate_MUX_controls);
181         } else {
182                 knames = main44_MUX_control_names;
183                 cntl = main44_MUX_controls;
184                 array_size = ARRAY_SIZE(main44_MUX_controls);
185         }
186
187         DBG(KERN_INFO "%s: ARRAY_SIZE = %d\tmode=%d\n", __func__,
188                         array_size, process_flow);
189         for (i = 0; i < array_size; i++) {
190                 aic3262_change_book(codec, cntl[i].control_book);
191                 aic3262_change_page(codec, cntl[i].control_page);
192                 val1 = i2c_smbus_read_byte_data(codec->control_data,
193                                 cntl[i].control_base);
194                 snd_mux_controls[i].private_value = 0;
195                 aic3262_change_book(codec,0);
196         }
197 }
198
199 /*
200  *----------------------------------------------------------------------------
201  * Function : byte_i2c_array_transfer
202  * Purpose  : Function used only for debugging purpose. This function will
203  *            be used while switching miniDSP Modes register by register.
204  *            This needs to be used only during development.
205  *-----------------------------------------------------------------------------
206  */
207 int byte_i2c_array_transfer(struct snd_soc_codec *codec,
208                                 reg_value *program_ptr,
209                                 int size)
210 {
211         int j;
212         u8 buf[3];
213
214         for (j = 0; j < size; j++) {
215                 /* Check if current Reg offset is zero */
216                 if (program_ptr[j].reg_off == 0) {
217                         /* Check for the Book Change Request */
218                         if ((j < (size - 1)) &&
219                                 (program_ptr[j+1].reg_off == 127)) {
220                                 aic3262_change_book(codec,
221                                         program_ptr[j+1].reg_val);
222                         /* Increment for loop counter across Book Change */
223                                 j++;
224                                 continue;
225                 }
226                 /* Check for the Page Change Request in Current book */
227                 aic3262_change_page(codec, program_ptr[j].reg_val);
228                 continue;
229                 }
230
231                 buf[AIC3262_REG_OFFSET_INDEX] = program_ptr[j].reg_off % 128;
232                 buf[AIC3262_REG_DATA_INDEX] =
233                                 program_ptr[j].reg_val & AIC3262_8BITS_MASK;
234
235                 if (codec->hw_write(codec->control_data, buf, 2) != 2) {
236                         printk(KERN_ERR "Error in i2c write\n");
237                         return -EIO;
238                 }
239         }
240         aic3262_change_book(codec,0);
241         return 0;
242 }
243
244 /*
245  *----------------------------------------------------------------------------
246  * Function : byte_i2c_array_read
247  * Purpose  : This function is used to perform Byte I2C Read. This is used
248  *            only for debugging purposes to read back the Codec Page
249  *            Registers after miniDSP Configuration.
250  *----------------------------------------------------------------------------
251  */
252 int byte_i2c_array_read(struct snd_soc_codec *codec,
253                         reg_value *program_ptr, int size)
254 {
255         int j;
256         u8 val1;
257         u8 cur_page = 0;
258         u8 cur_book = 0;
259         for (j = 0; j < size; j++) {
260                 /* Check if current Reg offset is zero */
261                 if (program_ptr[j].reg_off == 0) {
262                         /* Check for the Book Change Request */
263                         if ((j < (size - 1)) &&
264                                 (program_ptr[j+1].reg_off == 127)) {
265                                 aic3262_change_book(codec,
266                                         program_ptr[j+1].reg_val);
267                                 cur_book = program_ptr[j+1].reg_val;
268                         /* Increment for loop counter across Book Change */
269                                 j++;
270                                 continue;
271                         }
272                         /* Check for the Page Change Request in Current book */
273                         aic3262_change_page(codec, program_ptr[j].reg_val);
274                         cur_page = program_ptr[j].reg_val;
275                         continue;
276                 }
277
278                 val1 = i2c_smbus_read_byte_data(codec->control_data,
279                                 program_ptr[j].reg_off);
280                 if (val1 < 0)
281                         printk(KERN_ERR "Error in smbus read\n");
282
283                 DBG(KERN_INFO "[%d][%d][%d]= %x\n",
284                         cur_book, cur_page, program_ptr[j].reg_off, val1);
285         }
286         return 0;
287 }
288
289 /*
290  *----------------------------------------------------------------------------
291  * Function : minidsp_get_burst
292  * Purpose  : Format one I2C burst for transfer from mini dsp program array.
293  *            This function will parse the program array and get next burst
294  *            data for doing an I2C bulk transfer.
295  *----------------------------------------------------------------------------
296  */
297 static void
298 minidsp_get_burst(reg_value *program_ptr,
299                                 int program_size,
300                                 minidsp_parser_data *parse_data)
301 {
302         int index = parse_data->current_loc;
303         int burst_write_count = 0;
304
305         /*DBG("GET_BURST: start\n");*/
306         /* check if first location is page register, and populate page addr */
307         if (program_ptr[index].reg_off == 0) {
308                 if ((index < (program_size - 1)) &&
309                         (program_ptr[index+1].reg_off == 127)) {
310                         parse_data->book_change = 1;
311                         parse_data->book_no = program_ptr[index+1].reg_val;
312                         index += 2;
313                         goto finish_out;
314
315                 }
316                 parse_data->page_num = program_ptr[index].reg_val;
317                 parse_data->burst_array[burst_write_count++] =
318                         program_ptr[index].reg_off;
319                 parse_data->burst_array[burst_write_count++] =
320                         program_ptr[index].reg_val;
321                 index++;
322                 goto finish_out;
323         }
324
325         parse_data->burst_array[burst_write_count++] =
326                         program_ptr[index].reg_off;
327         parse_data->burst_array[burst_write_count++] =
328                         program_ptr[index].reg_val;
329         index++;
330
331         for (; index < program_size; index++) {
332                 if (program_ptr[index].reg_off !=
333                                 (program_ptr[index - 1].reg_off + 1))
334                         break;
335                 else
336                         parse_data->burst_array[burst_write_count++] =
337                                 program_ptr[index].reg_val;
338
339         }
340 finish_out:
341         parse_data->burst_size = burst_write_count;
342         if (index == program_size)
343                 /* parsing completed */
344                 parse_data->current_loc = MINIDSP_PARSING_END;
345         else
346                 parse_data->current_loc = index;
347         /*DBG("GET_BURST: end\n");*/
348 }
349 /*
350  *----------------------------------------------------------------------------
351  * Function : minidsp_i2c_multibyte_transfer
352  * Purpose  : Function used to perform multi-byte I2C Writes. Used to configure
353  *            the miniDSP Pages.
354  *----------------------------------------------------------------------------
355  */
356 int
357 minidsp_i2c_multibyte_transfer(struct snd_soc_codec *codec,
358                                         reg_value *program_ptr,
359                                         int program_size)
360 {
361         struct i2c_client *client = codec->control_data;
362
363         minidsp_parser_data parse_data;
364         int count = 0;
365
366 #ifdef DEBUG_MINIDSP_LOADING
367         int i = 0, j = 0;
368 #endif
369         /* point the current location to start of program array */
370         parse_data.current_loc = 0;
371         parse_data.page_num = 0;
372         parse_data.book_change = 0;
373         parse_data.book_no = 0;
374
375         DBG(KERN_INFO "size is : %d", program_size);
376         do {
377                 do {
378                         /* Get first burst data */
379                         minidsp_get_burst(program_ptr, program_size,
380                                         &parse_data);
381                         if (parse_data.book_change == 1)
382                                 break;
383                         dsp_parse_data[count] = parse_data;
384
385                         i2c_transaction[count].addr = client->addr;
386                         i2c_transaction[count].flags =
387                                 client->flags & I2C_M_TEN;
388                         i2c_transaction[count].len =
389                                 dsp_parse_data[count].burst_size;
390                         i2c_transaction[count].buf =
391                                 dsp_parse_data[count].burst_array;
392
393 #ifdef DEBUG_MINIDSP_LOADING
394                         DBG(KERN_INFO
395                         "i: %d\taddr: %d\tflags: %d\tlen: %d\tbuf:",
396                         i, client->addr, client->flags & I2C_M_TEN,
397                         dsp_parse_data[count].burst_size);
398
399                         for (j = 0; j <= dsp_parse_data[count].burst_size; j++)
400                                 DBG(KERN_INFO "%x ",
401                                         dsp_parse_data[i].burst_array[j]);
402
403                         DBG(KERN_INFO "\n\n");
404                         i++;
405 #endif
406
407                         count++;
408                         /* Proceed to the next burst reg_addr_incruence */
409                 } while (parse_data.current_loc != MINIDSP_PARSING_END);
410
411                 if (count > 0) {
412                         if (i2c_transfer(client->adapter,
413                                 i2c_transaction, count) != count) {
414                                 printk(KERN_ERR "Write burst i2c data error!\n");
415                         }
416                 }
417                 if (parse_data.book_change == 1) {
418                         aic3262_change_book(codec, parse_data.book_no);
419                         parse_data.book_change = 0;
420                 }
421         } while (parse_data.current_loc != MINIDSP_PARSING_END);
422
423         return 0;
424 }
425
426 /*
427  *----------------------------------------------------------------------------
428  * Function : set_minidsp_mode
429  * Purpose  : Switch to the first minidsp mode.
430  *----------------------------------------------------------------------------
431  */
432 int
433 set_minidsp_mode(struct snd_soc_codec *codec, int new_mode)
434 {
435         struct aic3262_priv *aic326x = snd_soc_codec_get_drvdata(codec);
436
437         DBG("%s: switch  mode start\n", __func__);
438         aic3262_reset_cache(codec);
439         /*reg_def_conf(codec);*/
440
441         if (new_mode == 0) {
442
443                 /*      General Programming     */
444                 DBG(KERN_INFO "$Writing reg_section_init_program\n");
445                 if (ARRAY_SIZE(main44_REG_Section_init_program) > 0) {
446 #ifndef MULTIBYTE_I2C
447                         byte_i2c_array_transfer(codec,
448                                 main44_REG_Section_init_program,
449                                 ARRAY_SIZE(main44_REG_Section_init_program));
450 #else
451                         minidsp_i2c_multibyte_transfer(codec,
452                                 main44_REG_Section_init_program,
453                                 ARRAY_SIZE(main44_REG_Section_init_program));
454 #endif
455
456                 } else {
457                         printk(KERN_ERR
458                         "_CODEC_REGS: Insufficient data for programming\n");
459                 }
460
461                 /*      minidsp A programming   */
462                 DBG(KERN_INFO "#Writing minidsp_A_reg_values\n");
463
464                 if ((main44_miniDSP_A_reg_values_COEFF_SIZE +
465                         main44_miniDSP_A_reg_values_INST_SIZE) > 0) {
466 #ifndef MULTIBYTE_I2C
467                         byte_i2c_array_transfer(codec,
468                                 main44_miniDSP_A_reg_values,
469                                 (main44_miniDSP_A_reg_values_COEFF_SIZE +
470                                 main44_miniDSP_A_reg_values_INST_SIZE));
471 #else
472                         minidsp_i2c_multibyte_transfer(codec,
473                                 main44_miniDSP_A_reg_values,
474                                 (main44_miniDSP_A_reg_values_COEFF_SIZE +
475                                 main44_miniDSP_A_reg_values_INST_SIZE));
476 #endif
477                 } else {
478                         printk(KERN_ERR
479                 "MINI_DSP_A_second: Insufficient data for programming\n");
480                 }
481                 /*      minidsp D programming   */
482                 DBG(KERN_INFO "#Writing minidsp_D_reg_values\n");
483                 if ((main44_miniDSP_D_reg_values_COEFF_SIZE +
484                         main44_miniDSP_D_reg_values_INST_SIZE) > 0) {
485
486 #ifdef MULTIBYTE_CONFIG_SUPPORT
487                         /*Multibyte for DAC */
488 #endif
489
490 #ifndef MULTIBYTE_I2C
491                         byte_i2c_array_transfer(codec,
492                                 main44_miniDSP_D_reg_values,
493                                 (main44_miniDSP_D_reg_values_COEFF_SIZE +
494                                 main44_miniDSP_D_reg_values_INST_SIZE));
495 #else
496                         minidsp_i2c_multibyte_transfer(codec,
497                                 main44_miniDSP_D_reg_values,
498                                 (main44_miniDSP_D_reg_values_COEFF_SIZE +
499                                 main44_miniDSP_D_reg_values_INST_SIZE));
500 #endif
501
502                 } else {
503                         printk(KERN_ERR
504                 "MINI_DSP_D_second: Insufficient data for programming\n");
505                 }
506                 DBG(KERN_INFO "#Writing reg_section_post_program\n");
507                 if (ARRAY_SIZE(main44_REG_Section_post_program) > 0) {
508                         #ifndef MULTIBYTE_I2C
509                         byte_i2c_array_transfer(codec,
510                                 main44_REG_Section_post_program,
511                                 ARRAY_SIZE(REG_Section_post_program));
512                         #else
513                         minidsp_i2c_multibyte_transfer(codec,
514                                 main44_REG_Section_post_program,
515                                 ARRAY_SIZE(main44_REG_Section_post_program));
516                         #endif
517                 } else {
518                         printk(KERN_ERR
519                 "second_CODEC_REGS: Insufficient data for programming\n");
520                 }
521         }
522
523         if (new_mode == 1) {
524                 /*      General Programming     */
525                 DBG(KERN_INFO "#Writing reg_section_init_program\n");
526                 if (ARRAY_SIZE(Second_Rate_REG_Section_init_program) > 0) {
527 #ifndef MULTIBYTE_I2C
528                         byte_i2c_array_transfer(codec,
529                         Second_Rate_REG_Section_init_program,
530                         ARRAY_SIZE(Second_Rate_REG_Section_init_program));
531 #else
532                         minidsp_i2c_multibyte_transfer(codec,
533                         Second_Rate_REG_Section_init_program,
534                         ARRAY_SIZE(Second_Rate_REG_Section_init_program));
535 #endif
536
537                 } else {
538                         printk(KERN_ERR
539                          "_CODEC_REGS: Insufficient data for programming\n");
540                 }
541                 /*      minidsp A programming   */
542                 DBG(KERN_INFO "#Writing minidsp_A_reg_values\n");
543
544                 if ((Second_Rate_miniDSP_A_reg_values_COEFF_SIZE +
545                         Second_Rate_miniDSP_A_reg_values_INST_SIZE) > 0) {
546 #ifndef MULTIBYTE_I2C
547                         byte_i2c_array_transfer(codec,
548                                 Second_Rate_miniDSP_A_reg_values,
549                                 (Second_Rate_miniDSP_A_reg_values_COEFF_SIZE +
550                                 Second_Rate_miniDSP_A_reg_values_INST_SIZE));
551 #else
552                         minidsp_i2c_multibyte_transfer(codec,
553                                 Second_Rate_miniDSP_A_reg_values,
554                                 (Second_Rate_miniDSP_A_reg_values_COEFF_SIZE +
555                                 Second_Rate_miniDSP_A_reg_values_INST_SIZE));
556 #endif
557                 } else {
558                         printk(KERN_ERR
559                 "MINI_DSP_A_second: Insufficient data for programming\n");
560                 }
561
562                 /*      minidsp D programming   */
563                 DBG(KERN_INFO "#Writing minidsp_D_reg_values\n");
564
565                 if ((Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
566                         Second_Rate_miniDSP_D_reg_values_INST_SIZE) > 0) {
567 #ifndef MULTIBYTE_I2C
568                         byte_i2c_array_transfer(codec,
569                                 Second_Rate_miniDSP_D_reg_values,
570                                 (Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
571                                 Second_Rate_miniDSP_D_reg_values_INST_SIZE));
572 #else
573                         minidsp_i2c_multibyte_transfer(codec,
574                                 Second_Rate_miniDSP_D_reg_values,
575                                 (Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
576                                 Second_Rate_miniDSP_D_reg_values_INST_SIZE));
577 #endif
578                 } else {
579                         printk(KERN_ERR
580                 "MINI_DSP_D_second: Insufficient data for programming\n");
581                 }
582                 DBG(KERN_INFO "Writing reg_section_post_program\n");
583                 if (ARRAY_SIZE(Second_Rate_REG_Section_post_program) > 0) {
584 #ifndef MULTIBYTE_I2C
585                         byte_i2c_array_transfer(codec,
586                                 Second_Rate_REG_Section_post_program,
587                                 ARRAY_SIZE(REG_Section_post_program));
588 #else
589                         minidsp_i2c_multibyte_transfer(codec,
590                         Second_Rate_REG_Section_post_program,
591                         ARRAY_SIZE(Second_Rate_REG_Section_post_program));
592 #endif
593                 } else {
594                         printk(KERN_ERR
595                  "second_CODEC_REGS: Insufficient data for programming\n");
596                 }
597         }
598
599 #ifdef MULTIBYTE_CONFIG_SUPPORT
600         /*Multibyte for DAC */
601         aic326x->process_flow = new_mode;
602         config_multibyte_for_mode(codec, new_mode);
603 #endif
604         DBG("%s: switch mode finished\n", __func__);
605         return 0;
606 }
607
608 /*
609  * i2c_verify
610  *
611  * Function used to validate the contents written into the miniDSP
612  * pages after miniDSP Configuration.
613 */
614 int i2c_verify(struct snd_soc_codec *codec)
615 {
616
617         DBG(KERN_INFO "#%s: Invoked.. Resetting to page 0\n", __func__);
618
619         aic3262_change_book(codec, 0);
620         DBG(KERN_INFO "#Reading reg_section_init_program\n");
621
622         byte_i2c_array_read(codec, main44_REG_Section_init_program,
623                 ARRAY_SIZE(main44_REG_Section_init_program));
624
625         DBG(KERN_INFO "#Reading minidsp_A_reg_values\n");
626         byte_i2c_array_read(codec, main44_miniDSP_A_reg_values,
627                 (main44_miniDSP_A_reg_values_COEFF_SIZE +
628                  main44_miniDSP_A_reg_values_INST_SIZE));
629
630         DBG(KERN_INFO "#Reading minidsp_D_reg_values\n");
631         byte_i2c_array_read(codec, main44_miniDSP_D_reg_values,
632                 (main44_miniDSP_D_reg_values_COEFF_SIZE +
633                  main44_miniDSP_D_reg_values_INST_SIZE));
634
635         DBG(KERN_INFO "#Reading reg_section_post_program\n");
636         byte_i2c_array_read(codec, main44_REG_Section_post_program,
637                 ARRAY_SIZE(main44_REG_Section_post_program));
638
639         DBG(KERN_INFO "i2c_verify completed\n");
640         return 0;
641 }
642
643 /*
644  *----------------------------------------------------------------------------
645  * Function : set_minidsp_mode1
646  * Purpose  : for laoding the default minidsp mode for the first time .
647  *----------------------------------------------------------------------------
648  */
649 int
650 set_minidsp_mode1(struct snd_soc_codec *codec, int new_mode)
651 {
652         DBG("#%s: switch  mode start\n", __func__);
653         aic3262_reset_cache(codec);
654
655         if (new_mode == 0) {
656                 /*      General Programming     */
657                 DBG(KERN_INFO "#Writing reg_section_init_program\n");
658                 if (ARRAY_SIZE(main44_REG_Section_init_program) > 0) {
659 #ifndef MULTIBYTE_I2C
660                         byte_i2c_array_transfer(codec,
661                                 main44_REG_Section_init_program,
662                                 ARRAY_SIZE(main44_REG_Section_init_program));
663 #else
664                         minidsp_i2c_multibyte_transfer(codec,
665                                 main44_REG_Section_init_program,
666                                 ARRAY_SIZE(main44_REG_Section_init_program));
667 #endif
668
669                 } else {
670                         printk(KERN_ERR
671                         "_CODEC_REGS: Insufficient data for programming\n");
672                 }
673                 /*      minidsp A programming   */
674                 DBG(KERN_INFO "#Writing minidsp_A_reg_values\n");
675                 if ((main44_miniDSP_A_reg_values_COEFF_SIZE +
676                      main44_miniDSP_A_reg_values_INST_SIZE) > 0) {
677 #ifndef MULTIBYTE_I2C
678                         byte_i2c_array_transfer(codec,
679                                 main44_miniDSP_A_reg_values,
680                                 (main44_miniDSP_A_reg_values_COEFF_SIZE +
681                                 main44_miniDSP_A_reg_values_INST_SIZE));
682 #else
683                         minidsp_i2c_multibyte_transfer(codec,
684                                 main44_miniDSP_A_reg_values,
685                                 (main44_miniDSP_A_reg_values_COEFF_SIZE +
686                                 main44_miniDSP_A_reg_values_INST_SIZE));
687 #endif
688                 } else {
689                         printk(KERN_ERR
690                 "MINI_DSP_A_second: Insufficient data for programming\n");
691                 }
692
693                 /*      minidsp D programming   */
694                 DBG(KERN_INFO "#Writing minidsp_D_reg_values\n");
695                 if ((main44_miniDSP_D_reg_values_COEFF_SIZE +
696                      main44_miniDSP_D_reg_values_INST_SIZE) > 0) {
697 #ifndef MULTIBYTE_I2C
698                         byte_i2c_array_transfer(codec,
699                                 main44_miniDSP_D_reg_values,
700                                 (main44_miniDSP_D_reg_values_COEFF_SIZE +
701                                 main44_miniDSP_D_reg_values_INST_SIZE));
702 #else
703                         minidsp_i2c_multibyte_transfer(codec,
704                                 main44_miniDSP_D_reg_values,
705                                 (main44_miniDSP_D_reg_values_COEFF_SIZE +
706                                 main44_miniDSP_D_reg_values_INST_SIZE));
707 #endif
708                 } else {
709                         printk(KERN_ERR
710                 "MINI_DSP_D_second: Insufficient data for programming\n");
711                 }
712
713                 DBG(KERN_INFO "#Writing reg_section_post_program\n");
714                 if (ARRAY_SIZE(main44_REG_Section_post_program) > 0) {
715 #ifndef MULTIBYTE_I2C
716                         byte_i2c_array_transfer(codec,
717                         main44_REG_Section_post_program,
718                         ARRAY_SIZE(REG_Section_post_program));
719 #else
720                         minidsp_i2c_multibyte_transfer(codec,
721                         main44_REG_Section_post_program,
722                         ARRAY_SIZE(main44_REG_Section_post_program));
723 #endif
724                 } else {
725                         printk(KERN_ERR
726                 "second_CODEC_REGS: Insufficient data for programming\n");
727                 }
728         }
729
730         if (new_mode == 1) {
731                 /*      General Programming     */
732                 DBG(KERN_INFO "#Writing reg_section_init_program\n");
733                 if (ARRAY_SIZE(Second_Rate_REG_Section_init_program) > 0) {
734 #ifndef MULTIBYTE_I2C
735                         byte_i2c_array_transfer(codec,
736                         second_Rate_REG_Section_init_program,
737                         ARRAY_SIZE(Second_Rate_REG_Section_init_program));
738 #else
739                         minidsp_i2c_multibyte_transfer(codec,
740                         Second_Rate_REG_Section_init_program,
741                         ARRAY_SIZE(Second_Rate_REG_Section_init_program));
742 #endif
743                 } else {
744                         printk(KERN_ERR
745                         "_CODEC_REGS: Insufficient data for programming\n");
746                 }
747                 /*      minidsp A programming   */
748                 DBG(KERN_INFO "#Writing minidsp_A_reg_values\n");
749                 if ((Second_Rate_miniDSP_A_reg_values_COEFF_SIZE +
750                         Second_Rate_miniDSP_A_reg_values_INST_SIZE) > 0) {
751 #ifndef MULTIBYTE_I2C
752                         byte_i2c_array_transfer(codec,
753                         Second_Rate_miniDSP_A_reg_values,
754                         (Second_Rate_miniDSP_A_reg_values_COEFF_SIZE +
755                         Second_Rate_miniDSP_A_reg_values_INST_SIZE));
756 #else
757                         minidsp_i2c_multibyte_transfer(codec,
758                         Second_Rate_miniDSP_A_reg_values,
759                         (Second_Rate_miniDSP_A_reg_values_COEFF_SIZE +
760                         Second_Rate_miniDSP_A_reg_values_INST_SIZE));
761 #endif
762                 } else {
763                         printk(KERN_ERR\
764                 "MINI_DSP_A_second: Insufficient data for programming\n");
765                 }
766                 /*      minidsp D programming   */
767                 DBG(KERN_INFO "#Writing minidsp_D_reg_values\n");
768                 if ((Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
769                         Second_Rate_miniDSP_D_reg_values_INST_SIZE) > 0) {
770 #ifndef MULTIBYTE_I2C
771                         byte_i2c_array_transfer(codec,
772                         Second_Rate_miniDSP_D_reg_values,
773                         (Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
774                         Second_Rate_miniDSP_D_reg_values_INST_SIZE));
775 #else
776                         minidsp_i2c_multibyte_transfer(codec,
777                         Second_Rate_miniDSP_D_reg_values,
778                         (Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
779                         Second_Rate_miniDSP_D_reg_values_INST_SIZE));
780 #endif
781                 } else
782                         printk(KERN_ERR "MINI_DSP_D_second: Insufficient data for programming\n");
783
784                 DBG(KERN_INFO "#Writing reg_section_post_program\n");
785                 if (ARRAY_SIZE(Second_Rate_REG_Section_post_program) > 0) {
786 #ifndef MULTIBYTE_I2C
787                         byte_i2c_array_transfer(codec,
788                         Second_Rate_REG_Section_post_program,
789                         ARRAY_SIZE(REG_Section_post_program));
790 #else
791                         minidsp_i2c_multibyte_transfer(codec,
792                         Second_Rate_REG_Section_post_program,
793                         ARRAY_SIZE(Second_Rate_REG_Section_post_program));
794 #endif
795                 } else
796                         printk(KERN_ERR\
797                 "second_CODEC_REGS: Insufficient data for programming\n");
798
799         }
800
801         DBG("#%s: switch mode completed\n", __func__);
802         return 0;
803 }
804
805 /*
806  *----------------------------------------------------------------------------
807  * Function : aic3262_minidsp_program
808  * Purpose  : Program mini dsp for AIC3262 codec chip. This routine is
809  *              called from the aic3262 codec driver, if mini dsp programming
810  *              is enabled.
811  *----------------------------------------------------------------------------
812  */
813 int aic3262_minidsp_program(struct snd_soc_codec *codec)
814 {
815         DBG(KERN_INFO "#AIC3262: programming mini dsp\n");
816
817 #if defined(PROGRAM_MINI_DSP_first)
818         #ifdef DEBUG
819         DBG("#Verifying book 0\n");
820         i2c_verify_book0(codec);
821 #endif
822         aic3262_change_book(codec, 0);
823         set_minidsp_mode1(codec, 0);
824         aic3262_change_book(codec, 0);
825 #ifdef DEBUG
826         DBG("#verifying book 0\n");
827         i2c_verify_book0(codec);
828 #endif
829 #endif
830 #if defined(PROGRAM_MINI_DSP_second)
831 #ifdef DEBUG
832         DBG("#Verifying book 0\n");
833         i2c_verify_book0(codec);
834 #endif
835         aic3262_change_book(codec,0);
836         set_minidsp_mode1(codec, 1);
837         aic3262_change_book(codec,0);
838 #ifdef DEBUG
839         DBG("#verifying book 0\n");
840         i2c_verify_book0(codec);
841 #endif
842 #endif
843         return 0;
844 }
845 /*
846  *----------------------------------------------------------------------------
847  * Function : m_control_info
848  * Purpose  : This function is to initialize data for new control required to
849  *            program the AIC3262 registers.
850  *
851  *----------------------------------------------------------------------------
852  */
853 static int m_control_info(struct snd_kcontrol *kcontrol,
854                               struct snd_ctl_elem_info *uinfo)
855 {
856         uinfo->count = 1;
857         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
858         uinfo->value.integer.min = 0;
859         uinfo->value.integer.max = 1;
860         return 0;
861 }
862
863 /*
864  *----------------------------------------------------------------------------
865  * Function : m_control_get
866  * Purpose  : This function is to read data of new control for
867  *            program the AIC3262 registers.
868  *
869  *----------------------------------------------------------------------------
870  */
871 static int m_control_get(struct snd_kcontrol *kcontrol,
872                              struct snd_ctl_elem_value *ucontrol)
873 {
874         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
875         struct aic3262_priv *aic3262 = snd_soc_codec_get_drvdata(codec);
876         u32 val;
877         u8 val1;
878
879         if (!strcmp(kcontrol->id.name, "Minidsp mode")) {
880                 val = aic3262->process_flow;
881                 ucontrol->value.integer.value[0] = val;
882                 DBG(KERN_INFO "control get : mode=%d\n", aic3262->process_flow);
883         }
884         if (!strcmp(kcontrol->id.name, "DAC Adaptive mode Enable")) {
885                 aic3262_change_book(codec, 80);
886                 val1 = i2c_smbus_read_byte_data(codec->control_data, 1);
887                 ucontrol->value.integer.value[0] = ((val1>>1)&0x01);
888                 DBG(KERN_INFO "control get : mode=%d\n", aic3262->process_flow);
889                 aic3262_change_book(codec,0);
890         }
891         if (!strcmp(kcontrol->id.name, "ADC Adaptive mode Enable")) {
892                 aic3262_change_book(codec, 40);
893                 val1 = i2c_smbus_read_byte_data(codec->control_data, 1);
894                 ucontrol->value.integer.value[0] = ((val1>>1)&0x01);
895                 DBG(KERN_INFO "control get : mode=%d\n", dmode);
896                 aic3262_change_book(codec,0);
897         }
898
899         return 0;
900 }
901
902 /*
903  *----------------------------------------------------------------------------
904  * Function : m_new_control_put
905  * Purpose  : new_control_put is called to pass data from user/application to
906  *            the driver.
907  *
908  *----------------------------------------------------------------------------
909  */
910 static int m_control_put(struct snd_kcontrol *kcontrol,
911                              struct snd_ctl_elem_value *ucontrol)
912 {
913         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
914         struct aic3262_priv *aic3262 = snd_soc_codec_get_drvdata(codec);
915
916         u32 val;
917         u8 val1;
918         int mode = aic3262->process_flow;
919
920         DBG("n_control_put\n");
921         val = ucontrol->value.integer.value[0];
922         if (!strcmp(kcontrol->id.name, "Minidsp mode")) {
923                 DBG(KERN_INFO "\nMini dsp put\n mode = %d, val=%d\n",
924                         aic3262->process_flow, val);
925                 if (val != mode) {
926                         if (aic3262->mute_codec == 1) {
927                                 i2c_verify_book0(codec);
928                                 aic3262_change_book(codec, 0);
929                                 set_minidsp_mode(codec, val);
930
931                                 aic3262_change_book(codec, 0);
932                                 i2c_verify_book0(codec);
933         /*                      update_kcontrols(codec, val);*/
934                         } else {
935                                 printk(KERN_ERR
936                         " Cant Switch Processflows, Playback in progress");
937                         }
938                 }
939         }
940
941         if (!strcmp(kcontrol->id.name, "DAC Adaptive mode Enable")) {
942                 DBG(KERN_INFO "\nMini dsp put\n mode = %d, val=%d\n",
943                         aic3262->process_flow, val);
944                 if (val != amode) {
945                         aic3262_change_book(codec, 80);
946                         val1 = i2c_smbus_read_byte_data(codec->control_data, 1);
947                         aic3262_write(codec, 1, (val1&0xfb)|(val<<1));
948                         aic3262_change_book(codec,0);
949                 }
950                 amode = val;
951         }
952
953         if (!strcmp(kcontrol->id.name, "ADC Adaptive mode Enable")) {
954                 DBG(KERN_INFO "\nMini dsp put\n mode = %d, val=%d\n",
955                         aic3262->process_flow, val);
956                 if (val != dmode) {
957                         aic3262_change_book(codec, 40);
958                         val1 = i2c_smbus_read_byte_data(codec->control_data, 1);
959                         aic3262_write(codec, 1, (val1&0xfb)|(val<<1));
960                         aic3262_change_book(codec,0);
961                 }
962                 dmode = val;
963         }
964
965         if (!strcmp(kcontrol->id.name, "Dump Regs Book0"))
966                 i2c_verify_book0(codec);
967
968
969         if (!strcmp(kcontrol->id.name, "Verify minidsp program")) {
970                 if (mode == 0) {
971                         DBG("Current mod=%d\nVerifying minidsp_D_regs", mode);
972                         byte_i2c_array_read(codec,  main44_miniDSP_D_reg_values,
973                                 (main44_miniDSP_D_reg_values_COEFF_SIZE +
974                                 main44_miniDSP_D_reg_values_INST_SIZE));
975                 } else {
976                         byte_i2c_array_read(codec,
977                                 Second_Rate_miniDSP_D_reg_values,
978                                 (Second_Rate_miniDSP_D_reg_values_COEFF_SIZE +
979                                 Second_Rate_miniDSP_D_reg_values_INST_SIZE));
980                 }
981         }
982         DBG("\nmode = %d\n", mode);
983         return mode;
984 }
985
986 /************************** MUX CONTROL section *****************************/
987 /*
988  *----------------------------------------------------------------------------
989  * Function : __new_control_info_minidsp_mux
990  * Purpose  : info routine for mini dsp mux control amixer kcontrols
991  *----------------------------------------------------------------------------
992  */
993 static int __new_control_info_minidsp_mux(struct snd_kcontrol *kcontrol,
994                                           struct snd_ctl_elem_info *uinfo)
995 {
996         int index;
997         int ret_val = -1;
998
999         for (index = 0; index < ARRAY_SIZE(main44_MUX_controls); index++) {
1000                 if (strstr(kcontrol->id.name, main44_MUX_control_names[index]))
1001                         break;
1002         }
1003
1004         if (index < ARRAY_SIZE(main44_MUX_controls)) {
1005                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1006                 uinfo->count = 1;
1007                 uinfo->value.integer.min = MIN_MUX_CTRL;
1008                 uinfo->value.integer.max = MAX_MUX_CTRL;
1009                 ret_val = 0;
1010         }
1011         return ret_val;
1012 }
1013
1014 /*
1015  *----------------------------------------------------------------------------
1016  * Function : __new_control_get_minidsp_mux
1017  *
1018  * Purpose  : get routine for  mux control amixer kcontrols,
1019  *   read current register values to user.
1020  *   Used for for mini dsp 'MUX control' amixer controls.
1021  *----------------------------------------------------------------------------
1022  */
1023 static int __new_control_get_minidsp_mux(struct snd_kcontrol *kcontrol,
1024                                          struct snd_ctl_elem_value *ucontrol)
1025 {
1026         ucontrol->value.integer.value[0] = kcontrol->private_value;
1027         return 0;
1028 }
1029
1030 /*
1031  *----------------------------------------------------------------------------
1032  * Function : __new_control_put_minidsp_mux
1033  *
1034  * Purpose  : put routine for amixer kcontrols, write user values to registers
1035  *            values. Used for for mini dsp 'MUX control' amixer controls.
1036  *----------------------------------------------------------------------------
1037  */
1038 static int __new_control_put_minidsp_mux(struct snd_kcontrol *kcontrol,
1039                                         struct snd_ctl_elem_value *ucontrol)
1040 {
1041         u8 data[MUX_CTRL_REG_SIZE + 1];
1042         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1043         int index = 1;
1044         int user_value = ucontrol->value.integer.value[0];
1045         struct i2c_client *i2c;
1046         u8 value[2], swap_reg_pre, swap_reg_post;
1047         u8 page;
1048         int ret_val = -1, array_size;
1049         control *array;
1050         char **array_names;
1051         char *control_name, *control_name1;
1052         struct aic3262_priv *aic326x = snd_soc_codec_get_drvdata(codec);
1053         i2c = codec->control_data;
1054
1055         if (aic326x->process_flow == 0) {
1056                 DBG("#the current process flow is %d", aic326x->process_flow);
1057                 array = main44_MUX_controls;
1058                 array_size = ARRAY_SIZE(main44_MUX_controls);
1059                 array_names = main44_MUX_control_names;
1060                 control_name = "Stereo_Mux_TwoToOne_1";
1061                 control_name1 = "Mono_Mux_1_1";
1062
1063                 /* Configure only for process flow  1 controls */
1064                 if (strcmp(kcontrol->id.name, control_name) &&
1065                     strcmp(kcontrol->id.name, control_name1))
1066                         return 0;
1067         } else {
1068                 array = Second_Rate_MUX_controls;
1069                 array_size = ARRAY_SIZE(Second_Rate_MUX_controls);
1070                 array_names = Second_Rate_MUX_control_names;
1071                 control_name = "Stereo_Mux_TwoToOne_1_Second";
1072                 control_name1 = "Mono_Mux_1_1_Second";
1073
1074                 /* Configure only for process flow 2 controls */
1075                 if (strcmp(kcontrol->id.name, control_name) &&
1076                     strcmp(kcontrol->id.name, control_name1))
1077                         return 0;
1078         }
1079
1080         page = array[index].control_page;
1081
1082         DBG("#user value = 0x%x\n", user_value);
1083         for (index = 0; index < array_size; index++) {
1084                 if (strstr(kcontrol->id.name, array_names[index]))
1085                         break;
1086         }
1087         if (index < array_size) {
1088                 DBG(KERN_INFO "#Index %d Changing to Page %d\n", index,
1089                         array[index].control_page);
1090
1091                 aic3262_change_book(codec,
1092                                     array[index].control_book);
1093                 aic3262_change_page(codec,
1094                                     array[index].control_page);
1095
1096                 if (!strcmp(array_names[index], control_name)) {
1097                         if (user_value > 0) {
1098                                 data[1] = 0x00;
1099                                 data[2] = 0x00;
1100                                 data[3] = 0x00;
1101                         } else {
1102                                 data[1] = 0xFF;
1103                                 data[2] = 0xFf;
1104                                 data[3] = 0xFF;
1105                         }
1106                 } else {
1107                         if (user_value > 0) {
1108                                 data[1] =
1109                                         (u8) ((user_value >> 16) &
1110                                               AIC3262_8BITS_MASK);
1111                                 data[2] =
1112                                         (u8) ((user_value >> 8) &
1113                                               AIC3262_8BITS_MASK);
1114                                 data[3] =
1115                                         (u8)((user_value) & AIC3262_8BITS_MASK);
1116                         }
1117                 }
1118                 /* start register address */
1119                 data[0] = array[index].control_base;
1120
1121                 DBG(KERN_INFO
1122                 "#Writing %d %d %d \r\n", data[0], data[1], data[2]);
1123
1124                 ret_val = i2c_master_send(i2c, data, MUX_CTRL_REG_SIZE + 1);
1125
1126                 if (ret_val != MUX_CTRL_REG_SIZE + 1)
1127                         printk(KERN_ERR "i2c_master_send transfer failed\n");
1128                 else {
1129                         /* store the current level */
1130                         kcontrol->private_value = user_value;
1131                         ret_val = 0;
1132                         /* Enable adaptive filtering for ADC/DAC */
1133                 }
1134
1135                 /* Perform a BUFFER SWAP Command. Check if we are currently not
1136                  * in Page 8, if so, swap to Page 8 first
1137                  */
1138
1139                 value[0] = 1;
1140
1141                 if (i2c_master_send(i2c, value, 1) != 1)
1142                         printk(KERN_ERR "Can not write register address\n");
1143
1144                 /* Read the Value of the Page 8 Register 1 which controls the
1145                    Adaptive Switching Mode */
1146                 if (i2c_master_recv(i2c, value, 1) != 1)
1147                         printk(KERN_ERR "Can not read codec registers\n");
1148
1149                 swap_reg_pre = value[0];
1150                 /* Write the Register bit updates */
1151                 value[1] = value[0] | 1;
1152                 value[0] = 1;
1153
1154                 if (i2c_master_send(i2c, value, 2) != 2)
1155                         printk(KERN_ERR "Can not write register address\n");
1156
1157                 value[0] = 1;
1158                 /* verify buffer swap */
1159                 if (i2c_master_send(i2c, value, 1) != 1)
1160                         printk(KERN_ERR "Can not write register address\n");
1161
1162                 /* Read the Value of the Page 8 Register 1 which controls the
1163                    Adaptive Switching Mode */
1164                 if (i2c_master_recv(i2c, &swap_reg_post, 1) != 1)
1165                         printk(KERN_ERR "Can not read codec registers\n");
1166
1167                 if ((swap_reg_pre == 4 && swap_reg_post == 6)
1168                     || (swap_reg_pre == 6 && swap_reg_post == 4))
1169                         DBG("Buffer swap success\n");
1170                 else
1171                         printk(KERN_ERR
1172                         "Buffer swap...FAILED\nswap_reg_pre=%x, \
1173                         swap_reg_post=%x\n", swap_reg_pre, swap_reg_post);
1174
1175         }
1176         /* update the new buffer value in the old, just swapped out buffer */
1177         aic3262_change_book(codec, array[index].control_book);
1178         aic3262_change_page(codec, array[index].control_page);
1179         ret_val = i2c_master_send(i2c, data, MUX_CTRL_REG_SIZE + 1);
1180         ret_val = 0;
1181
1182         aic3262_change_book(codec, 0);
1183         return ret_val;
1184 }
1185
1186 /*
1187  *----------------------------------------------------------------------------
1188  * Function : minidsp_mux_ctrl_mixer_controls
1189  *
1190  * Purpose  : Add amixer kcontrols for mini dsp mux controls,
1191  *----------------------------------------------------------------------------
1192  */
1193 static int minidsp_mux_ctrl_mixer_controls(struct snd_soc_codec *codec,
1194                                                 int size, control *cntl,
1195                                                 char **name)
1196 {
1197         int i, err;
1198         int val1;
1199
1200         /* DBG("%d mixer controls for mini dsp MUX\n", no_mux_controls);*/
1201
1202         if (size) {
1203                 for (i = 0; i < size; i++) {
1204
1205                         snd_mux_controls[i].name = name[i];
1206                         snd_mux_controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1207                         snd_mux_controls[i].access =
1208                                 SNDRV_CTL_ELEM_ACCESS_READWRITE;
1209                         snd_mux_controls[i].info =
1210                                 __new_control_info_minidsp_mux;
1211                         snd_mux_controls[i].get = __new_control_get_minidsp_mux;
1212                         snd_mux_controls[i].put = __new_control_put_minidsp_mux;
1213                         /*
1214                          *  TBD: read volume reg and update the index number
1215                          */
1216                         aic3262_change_book(codec, cntl[i].control_book);
1217                         aic3262_change_page(codec, cntl[i].control_page);
1218                         val1 = i2c_smbus_read_byte_data(codec->control_data,
1219                                         cntl[i].control_base);
1220                         DBG(KERN_INFO "Control data %x\n", val1);
1221                         /*
1222                         if( val1 >= 0 )
1223                                 snd_mux_controls[i].private_value = val1;
1224                         else
1225                                 snd_mux_controls[i].private_value = 0;
1226                         */
1227                         DBG(KERN_INFO
1228                                 "the value of amixer control mux=%d", val1);
1229                         if (val1 >= 0 && val1 != 255)
1230                                 snd_mux_controls[i].private_value = val1;
1231                         else
1232                                 snd_mux_controls[i].private_value = 0;
1233
1234                         snd_mux_controls[i].count = 0;
1235
1236                         err = snd_ctl_add(codec->card->snd_card,
1237                                 snd_ctl_new1(&snd_mux_controls[i],
1238                                                codec));
1239                         if (err < 0)
1240                                 printk(KERN_ERR
1241                                         "%s:Invalid control %s\n", __FILE__,
1242                                         snd_mux_controls[i].name);
1243                 }
1244         }
1245                 aic3262_change_book(codec, 0);
1246         return 0;
1247 }
1248
1249 /*-------------------------  Volume Controls  -----------------------*/
1250 static int volume_lite_table[] = {
1251
1252         0x00000D, 0x00000E, 0x00000E, 0x00000F,
1253         0x000010, 0x000011, 0x000012, 0x000013,
1254         0x000015, 0x000016, 0x000017, 0x000018,
1255         0x00001A, 0x00001C, 0x00001D, 0x00001F,
1256         0x000021, 0x000023, 0x000025, 0x000027,
1257         0x000029, 0x00002C, 0x00002F, 0x000031,
1258         0x000034, 0x000037, 0x00003B, 0x00003E,
1259         0x000042, 0x000046, 0x00004A, 0x00004F,
1260         0x000053, 0x000058, 0x00005D, 0x000063,
1261         0x000069, 0x00006F, 0x000076, 0x00007D,
1262         0x000084, 0x00008C, 0x000094, 0x00009D,
1263         0x0000A6, 0x0000B0, 0x0000BB, 0x0000C6,
1264         0x0000D2, 0x0000DE, 0x0000EB, 0x0000F9,
1265         0x000108, 0x000118, 0x000128, 0x00013A,
1266         0x00014D, 0x000160, 0x000175, 0x00018B,
1267         0x0001A3, 0x0001BC, 0x0001D6, 0x0001F2,
1268         0x000210, 0x00022F, 0x000250, 0x000273,
1269         0x000298, 0x0002C0, 0x0002E9, 0x000316,
1270         0x000344, 0x000376, 0x0003AA, 0x0003E2,
1271         0x00041D, 0x00045B, 0x00049E, 0x0004E4,
1272         0x00052E, 0x00057C, 0x0005D0, 0x000628,
1273         0x000685, 0x0006E8, 0x000751, 0x0007C0,
1274         0x000836, 0x0008B2, 0x000936, 0x0009C2,
1275         0x000A56, 0x000AF3, 0x000B99, 0x000C49,
1276         0x000D03, 0x000DC9, 0x000E9A, 0x000F77,
1277         0x001062, 0x00115A, 0x001262, 0x001378,
1278         0x0014A0, 0x0015D9, 0x001724, 0x001883,
1279         0x0019F7, 0x001B81, 0x001D22, 0x001EDC,
1280         0x0020B0, 0x0022A0, 0x0024AD, 0x0026DA,
1281         0x002927, 0x002B97, 0x002E2D, 0x0030E9,
1282         0x0033CF, 0x0036E1, 0x003A21, 0x003D93,
1283         0x004139, 0x004517, 0x00492F, 0x004D85,
1284         0x00521D, 0x0056FA, 0x005C22, 0x006197,
1285         0x006760, 0x006D80, 0x0073FD, 0x007ADC,
1286         0x008224, 0x0089DA, 0x009205, 0x009AAC,
1287         0x00A3D7, 0x00B7D4, 0x00AD8C, 0x00C2B9,
1288         0x00CE43, 0x00DA7B, 0x00E76E, 0x00F524,
1289         0x0103AB, 0x01130E, 0x01235A, 0x01349D,
1290         0x0146E7, 0x015A46, 0x016ECA, 0x018486,
1291         0x019B8C, 0x01B3EE, 0x01CDC3, 0x01E920,
1292         0x02061B, 0x0224CE, 0x024553, 0x0267C5,
1293         0x028C42, 0x02B2E8, 0x02DBD8, 0x030736,
1294         0x033525, 0x0365CD, 0x039957, 0x03CFEE,
1295         0x0409C2, 0x044703, 0x0487E5, 0x04CCA0,
1296         0x05156D, 0x05628A, 0x05B439, 0x060ABF,
1297         0x066666, 0x06C77B, 0x072E50, 0x079B3D,
1298         0x080E9F, 0x0888D7, 0x090A4D, 0x09936E,
1299         0x0A24B0, 0x0ABE8D, 0x0B6188, 0x0C0E2B,
1300         0x0CC509, 0x0D86BD, 0x0E53EB, 0x0F2D42,
1301         0x101379, 0x110754, 0x1209A3, 0x131B40,
1302         0x143D13, 0x157012, 0x16B543, 0x180DB8,
1303         0x197A96, 0x1AFD13, 0x1C9676, 0x1E481C,
1304         0x201373, 0x21FA02, 0x23FD66, 0x261F54,
1305         0x28619A, 0x2AC625, 0x2D4EFB, 0x2FFE44,
1306         0x32D646, 0x35D96B, 0x390A41, 0x3C6B7E,
1307         0x400000, 0x43CAD0, 0x47CF26, 0x4C106B,
1308         0x50923B, 0x55586A, 0x5A6703, 0x5FC253,
1309         0x656EE3, 0x6B7186, 0x71CF54, 0x788DB4,
1310         0x7FB260,
1311 };
1312
1313 static struct snd_kcontrol_new snd_vol_controls[MAX_VOLUME_CONTROLS];
1314 /*
1315  *----------------------------------------------------------------------------
1316  * Function : __new_control_info_main44_minidsp_volume
1317  * Purpose  : info routine for volumeLite amixer kcontrols
1318  *----------------------------------------------------------------------------
1319  */
1320
1321 static int
1322 __new_control_info_minidsp_volume(struct snd_kcontrol *kcontrol,
1323                                   struct snd_ctl_elem_info *uinfo)
1324 {
1325         int index, index8;
1326         int ret_val = -1;
1327
1328         for (index = 0; index < ARRAY_SIZE(main44_VOLUME_controls); index++) {
1329                 if (strstr
1330                     (kcontrol->id.name, main44_VOLUME_control_names[index]))
1331                         break;
1332         }
1333         for (index8 = 0; index8 < ARRAY_SIZE(Second_Rate_VOLUME_controls);
1334                         index8++) {
1335                 if (strstr
1336                     (kcontrol->id.name,
1337                     Second_Rate_VOLUME_control_names[index]))
1338                         break;
1339         }
1340         if ((index < ARRAY_SIZE(main44_VOLUME_controls))
1341             || (index8 < ARRAY_SIZE(Second_Rate_VOLUME_controls))) {
1342                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1343                 uinfo->count = 1;
1344                 uinfo->value.integer.min = MIN_VOLUME;
1345                 uinfo->value.integer.max = MAX_VOLUME;
1346                 ret_val = 0;
1347         }
1348         return ret_val;
1349 }
1350
1351 /*
1352  *----------------------------------------------------------------------------
1353  * Function : __new_control_get_main44_minidsp_vol
1354  * Purpose  : get routine for amixer kcontrols, read current register
1355  *         values. Used for for mini dsp 'VolumeLite' amixer controls.
1356  *----------------------------------------------------------------------------
1357  */
1358 static int
1359 __new_control_get_minidsp_volume(struct snd_kcontrol *kcontrol,
1360                                  struct snd_ctl_elem_value *ucontrol)
1361 {
1362         ucontrol->value.integer.value[0] = kcontrol->private_value;
1363         return 0;
1364 }
1365
1366 /*
1367  *----------------------------------------------------------------------------
1368  * Function : __new_control_put_main44_minidsp_volume
1369  * Purpose  : put routine for amixer kcontrols, write user values to registers
1370  *        values. Used for for mini dsp 'VolumeLite' amixer controls.
1371  *----------------------------------------------------------------------------
1372  */
1373 static int
1374 __new_control_put_minidsp_volume(struct snd_kcontrol *kcontrol,
1375                                  struct snd_ctl_elem_value *ucontrol)
1376 {
1377         u8 data[4];
1378         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1379         int user_value = ucontrol->value.integer.value[0];
1380         struct i2c_client *i2c = codec->control_data;
1381         int ret_val = -1;
1382         int coeff;
1383         u8 value[2], swap_reg_pre, swap_reg_post;
1384         struct aic3262_priv *aic3262 = snd_soc_codec_get_drvdata(codec);
1385
1386         control *volume_controls = NULL;
1387         printk(KERN_INFO "user value = 0x%x\n", user_value);
1388
1389         if (aic3262->process_flow == 0)
1390                 volume_controls = main44_VOLUME_controls;
1391         else
1392                 volume_controls = Second_Rate_VOLUME_controls;
1393
1394         aic3262_change_book(codec, volume_controls->control_book);
1395         aic3262_change_page(codec, volume_controls->control_page);
1396
1397         coeff = volume_lite_table[user_value << 1];
1398
1399         data[1] = (u8) ((coeff >> 16) & AIC3262_8BITS_MASK);
1400         data[2] = (u8) ((coeff >> 8) & AIC3262_8BITS_MASK);
1401         data[3] = (u8) ((coeff) & AIC3262_8BITS_MASK);
1402
1403         /* Start register address */
1404         data[0] = volume_controls->control_base;
1405         ret_val = i2c_master_send(i2c, data, VOLUME_REG_SIZE + 1);
1406         if (ret_val != VOLUME_REG_SIZE + 1)
1407                 printk(KERN_ERR "i2c_master_send transfer failed\n");
1408         else {
1409                 /* store the current level */
1410                 kcontrol->private_value = user_value;
1411                 ret_val = 0;
1412         }
1413         /* Initiate buffer swap */
1414         value[0] = 1;
1415
1416         if (i2c_master_send(i2c, value, 1) != 1)
1417                 printk(KERN_ERR "Can not write register address\n");
1418
1419         /* Read the Value of the Page 8 Register 1 which controls the
1420            Adaptive Switching Mode */
1421         if (i2c_master_recv(i2c, value, 1) != 1)
1422                 printk(KERN_ERR "Can not read codec registers\n");
1423
1424         swap_reg_pre = value[0];
1425         /* Write the Register bit updates */
1426         value[1] = value[0] | 1;
1427         value[0] = 1;
1428         if (i2c_master_send(i2c, value, 2) != 2)
1429                 printk(KERN_ERR "Can not write register address\n");
1430
1431         value[0] = 1;
1432         /* verify buffer swap */
1433         if (i2c_master_send(i2c, value, 1) != 1)
1434                 printk(KERN_ERR "Can not write register address\n");
1435
1436         /* Read the Value of the Page 8 Register 1 which controls the
1437            Adaptive Switching Mode */
1438         if (i2c_master_recv(i2c, &swap_reg_post, 1) != 1)
1439                 printk(KERN_ERR "Can not read codec registers\n");
1440
1441         if ((swap_reg_pre == 4 && swap_reg_post == 6)
1442             || (swap_reg_pre == 6 && swap_reg_post == 4))
1443                 DBG("Buffer swap success\n");
1444         else
1445                 DBG("Buffer swap...FAILED\nswap_reg_pre=%x, swap_reg_post=%x\n",
1446                          swap_reg_pre, swap_reg_post);
1447
1448         /* update the new buffer value in the old, just swapped out buffer */
1449         aic3262_change_book(codec, volume_controls->control_book);
1450         aic3262_change_page(codec, volume_controls->control_page);
1451         i2c_master_send(i2c, data, MUX_CTRL_REG_SIZE + 1);
1452
1453         aic3262_change_book(codec, 0);
1454
1455         return 0;
1456 }
1457
1458 /*
1459  *----------------------------------------------------------------------------
1460  * Function : minidsp_volume_main44_mixer_controls
1461  * Purpose  : Add amixer kcontrols for mini dsp volume Lite controls,
1462  *----------------------------------------------------------------------------
1463  */
1464 static int minidsp_volume_mixer_controls(struct snd_soc_codec *codec)
1465 {
1466         int i, err, no_volume_controls;
1467         static char volume_control_name[MAX_VOLUME_CONTROLS][40];
1468
1469         /*      ADD first process volume controls       */
1470         no_volume_controls = ARRAY_SIZE(main44_VOLUME_controls);
1471
1472         printk(KERN_INFO " %d mixer controls for mini dsp 'volumeLite'\n",
1473                 no_volume_controls);
1474
1475         if (no_volume_controls) {
1476
1477                 for (i = 0; i < no_volume_controls; i++) {
1478                         strcpy(volume_control_name[i],
1479                                main44_VOLUME_control_names[i]);
1480                         strcat(volume_control_name[i], VOLUME_KCONTROL_NAME);
1481
1482                         printk(KERN_ERR "Volume controls: %s\n",
1483                                 volume_control_name[i]);
1484
1485                         snd_vol_controls[i].name = volume_control_name[i];
1486                         snd_vol_controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1487                         snd_vol_controls[i].access =
1488                                 SNDRV_CTL_ELEM_ACCESS_READWRITE;
1489                         snd_vol_controls[i].info =
1490                                 __new_control_info_minidsp_volume;
1491                         snd_vol_controls[i].get =
1492                                 __new_control_get_minidsp_volume;
1493                         snd_vol_controls[i].put =
1494                                 __new_control_put_minidsp_volume;
1495                         /*
1496                          *      TBD: read volume reg and update the index number
1497                          */
1498                         snd_vol_controls[i].private_value = 0;
1499                         snd_vol_controls[i].count = 0;
1500
1501                         err = snd_ctl_add(codec->card->snd_card,
1502                                           snd_ctl_new1(&snd_vol_controls[i],
1503                                                        codec));
1504                         if (err < 0) {
1505                                 printk(KERN_ERR
1506                                         "%s:Invalid control %s\n", __FILE__,
1507                                         snd_vol_controls[i].name);
1508                         }
1509                 }
1510         }
1511         /*      ADD second process volume controls      */
1512         no_volume_controls = ARRAY_SIZE(Second_Rate_VOLUME_controls);
1513
1514         printk(KERN_ERR " %d mixer controls for mini dsp 'volumeLite'\n",
1515                 no_volume_controls);
1516
1517         if (no_volume_controls) {
1518
1519                 for (i = 0; i < no_volume_controls; i++) {
1520                         strcpy(volume_control_name[i],
1521                                Second_Rate_VOLUME_control_names[i]);
1522                         strcat(volume_control_name[i], VOLUME_KCONTROL_NAME);
1523
1524                         printk(KERN_ERR "Volume controls: %s\n",
1525                                 volume_control_name[i]);
1526
1527                         snd_vol_controls[i].name = volume_control_name[i];
1528                         snd_vol_controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1529                         snd_vol_controls[i].access =
1530                                 SNDRV_CTL_ELEM_ACCESS_READWRITE;
1531                         snd_vol_controls[i].info =
1532                                 __new_control_info_minidsp_volume;
1533                         snd_vol_controls[i].get =
1534                                 __new_control_get_minidsp_volume;
1535                         snd_vol_controls[i].put =
1536                                 __new_control_put_minidsp_volume;
1537                         /*
1538                          *      TBD: read volume reg and update the index number
1539                          */
1540                         snd_vol_controls[i].private_value = 0;
1541                         snd_vol_controls[i].count = 0;
1542
1543                         err = snd_ctl_add(codec->card->snd_card,
1544                                           snd_ctl_new1(&snd_vol_controls[i],
1545                                                        codec));
1546                         if (err < 0) {
1547                                 printk(KERN_ERR
1548                                         "%s:Invalid control %s\n", __FILE__,
1549                                        snd_vol_controls[i].name);
1550                         }
1551                 }
1552         }
1553         return 0;
1554 }
1555
1556 /*
1557  *--------------------------------------------------------------------------
1558  * Function : aic3262_add_minidsp_controls
1559  * Purpose :  Configures the AMIXER Control Interfaces that can be exercised by
1560  *            the user at run-time. Utilizes the  the snd_adaptive_controls[]
1561  *            array to specify two run-time controls.
1562  *---------------------------------------------------------------------------
1563  */
1564 int aic3262_add_minidsp_controls(struct snd_soc_codec *codec)
1565 {
1566 #ifdef ADD_MINI_DSP_CONTROLS
1567         int i, err, no_mux_controls;
1568         /* add mode k control */
1569         for (i = 0; i < ARRAY_SIZE(aic3262_minidsp_controls); i++) {
1570                 err = snd_ctl_add(codec->card->snd_card,
1571                 snd_ctl_new1(&aic3262_minidsp_controls[i], codec));
1572                 if (err < 0) {
1573                         printk(KERN_ERR "Invalid control\n");
1574                         return err;
1575                 }
1576         }
1577
1578         /* add mux controls */
1579         no_mux_controls = ARRAY_SIZE(main44_MUX_controls);
1580         minidsp_mux_ctrl_mixer_controls(codec, no_mux_controls,
1581                 main44_MUX_controls, main44_MUX_control_names);
1582
1583         no_mux_controls = ARRAY_SIZE(Second_Rate_MUX_controls);
1584         minidsp_mux_ctrl_mixer_controls(codec, no_mux_controls,
1585                 Second_Rate_MUX_controls, Second_Rate_MUX_control_names);
1586
1587         /* add volume controls*/
1588         minidsp_volume_mixer_controls(codec);
1589 #endif /* ADD_MINI_DSP_CONTROLS */
1590         return 0;
1591 }
1592
1593 MODULE_DESCRIPTION("ASoC TLV320AIC3262 miniDSP driver");
1594 MODULE_AUTHOR("Y Preetam Sashank Reddy <preetam@mistralsolutions.com>");
1595 MODULE_LICENSE("GPL");
1596 #endif /* End of CONFIG_MINI_DSP */