audio: TFA9887 driver
[linux-2.6.git] / drivers / misc / tfa9887.c
1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/err.h>
4 #include <linux/module.h>
5 #include <linux/interrupt.h>
6 #include <linux/i2c.h>
7 #include <linux/regmap.h>
8 #include <linux/tfa9887.h>
9 #include <sound/initval.h>
10 #include <linux/sysfs.h>
11 #include <linux/miscdevice.h>
12
13 static ssize_t tfa9887_cal_show(struct kobject *kobj,
14                 struct kobj_attribute *attr, char *buf);
15
16 static ssize_t tfa9887_cal_store(struct kobject *kobj,
17                 struct kobj_attribute *attr, const char *buf, size_t count);
18
19 static ssize_t tfa9887_config_show(struct kobject *kobj,
20                 struct kobj_attribute *attr, char *buf);
21
22 static ssize_t tfa9887_config_store(struct kobject *kobj,
23                 struct kobj_attribute *attr, const char *buf, size_t count);
24
25 static ssize_t tfa9887_vol_show(struct kobject *kobj,
26                 struct kobj_attribute *attr, char *buf);
27
28 static ssize_t tfa9887_vol_store(struct kobject *kobj,
29                 struct kobj_attribute *attr, const char *buf, size_t count);
30
31
32 static struct kobj_attribute tfa9887_config =
33                 __ATTR(config, 0640, tfa9887_config_show, tfa9887_config_store);
34
35 static struct kobj_attribute tfa9887_cal =
36                 __ATTR(cal, 0640, tfa9887_cal_show, tfa9887_cal_store);
37
38 static struct kobj_attribute tfa9887_vol =
39                 __ATTR(vol, 0640, tfa9887_vol_show, tfa9887_vol_store);
40
41 static struct kobject *tfa9887_kobj;
42
43 static struct tfa9887_priv *tfa9887R, *tfa9887L;
44
45 /* begin binary data: */
46 unsigned char coldpatch_data[] = {/* 10 */
47 0x08,0x00,0x70,0x00,0x07,0x81,0x00,0x00,0x00,0x01
48 };
49 /* end binary data. size = 10 bytes */
50
51 /* begin binary data: */
52 unsigned char n1d2_data[] = {/* 2224 */
53  0x03,0x00,0x70,0x00,0x01,0xFB,0x00,0x71,0x40,0x00,0x6A,0x7E,0xFD,0x04,0x3A
54 ,0x91,0x00,0xC7,0x6A,0x3F,0x42,0x90,0x3A,0x92,0xFF,0xE9,0x71,0x24,0x58,0x7F
55 ,0x30,0x60,0x00,0x80,0x3B,0x80,0x00,0xB6,0xB8,0xA2,0xD5,0x7C,0xB5,0x00,0x54
56 ,0xBE,0x38,0x20,0x0A,0x6F,0xB5,0x00,0x76,0x08,0x30,0x12,0x00,0x00,0xB5,0x00
57 ,0x40,0x00,0xA0,0x00,0xD5,0x3C,0xB5,0x00,0x40,0x40,0x3A,0x9A,0xFF,0x50,0xB5
58 ,0x00,0x44,0x18,0x38,0x08,0x22,0x13,0xB5,0x00,0x42,0x90,0x3A,0x80,0x00,0x3E
59 ,0xD0,0x24,0x42,0x80,0xB5,0x00,0x7E,0x9C,0x82,0x14,0x7E,0x9C,0x3B,0x47,0x40
60 ,0x1F,0x3A,0x83,0x00,0x07,0x9B,0xC5,0x61,0x40,0x9B,0xC6,0x55,0x3D,0x3A,0x92
61 ,0x00,0x09,0xB5,0x00,0x42,0x10,0xD0,0x24,0x7E,0x9C,0x70,0xA0,0x7E,0x9C,0xB5
62 ,0x00,0x42,0x00,0x3A,0x80,0x00,0x9B,0xD0,0x24,0x54,0x7C,0x30,0x70,0x00,0x80
63 ,0xB5,0x00,0x61,0x40,0x3B,0x80,0x00,0xA9,0x90,0xD9,0x62,0x09,0x7F,0x4E,0x54
64 ,0xBC,0x6A,0x1F,0x4C,0x08,0x3B,0x80,0x01,0x0E,0x30,0x50,0x00,0x80,0xB5,0x00
65 ,0x58,0x3F,0x3C,0xD8,0x03,0x00,0x8B,0x80,0x7D,0x7C,0xB5,0x00,0x7E,0x9C,0x98
66 ,0x99,0xFD,0x06,0x9B,0x0C,0x52,0x7B,0xA6,0x18,0x58,0x7A,0x39,0x02,0x22,0x1C
67 ,0x30,0x90,0x00,0xD8,0x30,0xB0,0x20,0x94,0x30,0xA0,0x00,0xE4,0x20,0x03,0x40
68 ,0x40,0xA8,0x00,0xD4,0xFC,0xB5,0x00,0x5A,0x7D,0x62,0x08,0x66,0x0B,0x3A,0x31
69 ,0xFF,0xFE,0x3B,0x80,0x02,0x08,0x9B,0xC0,0xD5,0x7F,0x80,0xB0,0x54,0xBC,0x3A
70 ,0x89,0x00,0x04,0xFB,0x00,0x71,0x40,0x3E,0x6A,0x9F,0xD5,0xBE,0x6A,0x7E,0x44
71 ,0x51,0x3A,0x9B,0x00,0x05,0xB5,0x00,0x52,0xFC,0x30,0x90,0x00,0xD8,0x30,0xA0
72 ,0x00,0xE4,0x30,0xB0,0x20,0x94,0x20,0x03,0x40,0x50,0x6A,0x7E,0xD2,0x3D,0x80
73 ,0xB0,0x66,0x0B,0xB5,0x00,0x44,0x10,0x3A,0x31,0xFF,0xFE,0x3B,0x80,0x02,0x08
74 ,0x9B,0xC0,0xD5,0x7F,0x6A,0xDF,0x54,0xBD,0x3A,0x89,0x00,0x04,0x62,0x28,0xD5
75 ,0x3F,0x3A,0x9B,0x00,0x05,0xB5,0x00,0x54,0xFD,0x9B,0xAC,0x52,0x3C,0xA6,0x12
76 ,0xD2,0x3B,0x39,0x02,0x22,0x1C,0xA6,0x52,0xD8,0x3A,0xA8,0x00,0xFE,0x9C,0x3C
77 ,0xD8,0x03,0x00,0xA9,0x01,0x7D,0x7A,0xB5,0x00,0x61,0x34,0x71,0x44,0x7D,0x03
78 ,0x6A,0x7E,0xC4,0x08,0xB5,0x00,0x58,0x7F,0x30,0x50,0x00,0x80,0x3B,0x80,0x01
79 ,0x25,0xB5,0x00,0x55,0x7E,0x6A,0xDE,0xD5,0x3E,0x62,0x4C,0xC4,0x11,0x38,0x20
80 ,0x0A,0x6F,0x6A,0xFE,0xE1,0x50,0xB5,0x00,0x55,0x7E,0x30,0x40,0x00,0x80,0x3B
81 ,0x80,0x01,0x00,0xB0,0xB2,0xF6,0x00,0x6A,0x1F,0x54,0xBD,0x3B,0x80,0x01,0x00
82 ,0x9B,0xA6,0xFE,0x9C,0xB5,0x00,0x58,0x3F,0x3C,0xD8,0x03,0x00,0x8B,0x80,0x7D
83 ,0x7D,0xB5,0x00,0x7E,0x9C,0x38,0x08,0x0A,0x6F,0x9B,0xA6,0x7D,0x04,0xB0,0xB6
84 ,0x62,0x88,0x69,0x7E,0xC4,0x08,0x6A,0xBF,0x54,0xFC,0xB5,0x00,0x58,0x7F,0x3B
85 ,0x80,0x01,0x25,0x30,0x50,0x00,0x80,0x69,0x5E,0xD5,0x3C,0x72,0x02,0xC4,0x11
86 ,0x6A,0x1F,0x76,0x40,0x30,0x40,0x00,0x80,0x3B,0x80,0x01,0x00,0xB0,0xB2,0xD5
87 ,0x7C,0xFB,0x00,0x71,0x40,0x7C,0x9B,0xA6,0xD4,0x3E,0xB5,0x00,0x54,0xBC,0x3B
88 ,0x80,0x01,0x00,0x3A,0x80,0x00,0x80,0xB5,0x00,0x58,0x3F,0x3C,0xD8,0x03,0x00
89 ,0x8B,0x80,0x7D,0x7C,0xB5,0x00,0x7E,0x9C,0x9B,0x86,0x7D,0x04,0x38,0x0A,0x0A
90 ,0x6F,0xB0,0xB2,0xD4,0x7E,0x6A,0x7E,0x52,0xFD,0x71,0x04,0xD8,0x7F,0x3B,0x80
91 ,0x01,0x25,0x30,0x50,0x00,0x80,0x69,0x5E,0xD5,0x3E,0x72,0x02,0xC4,0x11,0x6A
92 ,0xBE,0xD4,0xBC,0x30,0x40,0x00,0x80,0x3B,0x80,0x01,0x00,0xB0,0xB2,0xF6,0x00
93 ,0xB5,0x00,0x54,0x3C,0x3A,0x88,0x00,0x80,0xB5,0x00,0x54,0x3D,0x3B,0x80,0x01
94 ,0x00,0x9B,0xA6,0xFE,0x9C,0xB5,0x00,0x58,0x3F,0x3C,0xD8,0x03,0x00,0x8B,0x80
95 ,0x7D,0x7C,0xB5,0x00,0x7E,0x9C,0x3A,0x88,0x00,0x0A,0x9B,0x8C,0x42,0x88,0xA6
96 ,0x56,0x7D,0x05,0x39,0x02,0x22,0x20,0xAA,0x08,0xC4,0x00,0x3A,0x89,0x00,0xA6
97 ,0xBA,0x41,0x58,0x7B,0x6E,0x3F,0x54,0x7C,0x3B,0x80,0x13,0x20,0xBA,0x20,0xDA
98 ,0x7D,0x6D,0x5E,0xD0,0xBE,0x3A,0x81,0x00,0x18,0x82,0x04,0x7E,0x40,0x3A,0x90
99 ,0xFF,0xFA,0xBF,0x10,0x4C,0x90,0xBB,0x00,0x55,0x7D,0x31,0x80,0x20,0xAF,0x3B
100 ,0x80,0x01,0x07,0x9B,0xA2,0xCA,0x48,0x7F,0x4E,0x54,0xBD,0xB5,0x00,0x4C,0x88
101 ,0x3A,0x91,0xFF,0x43,0xB5,0x00,0x42,0x90,0x96,0xF4,0xD5,0xBC,0x3A,0x81,0xFF
102 ,0x41,0x3A,0x93,0x00,0x29,0x61,0x48,0xFD,0xA9,0x3A,0x88,0x00,0xAF,0x61,0x8A
103 ,0x72,0xE8,0x6A,0x7E,0xC4,0x00,0x30,0x40,0x00,0x80,0x30,0x70,0x02,0xBC,0xFB
104 ,0x00,0x71,0x40,0xBA,0x3B,0x80,0x1C,0x5A,0xB5,0x00,0x55,0x7C,0xB5,0x00,0x54
105 ,0x3D,0x3A,0x88,0x00,0x0F,0x62,0x24,0x44,0x0A,0x3A,0x91,0xFF,0x4F,0x30,0x50
106 ,0x00,0x80,0x3B,0x80,0x01,0x25,0xB5,0x00,0x55,0x7D,0x6A,0xDE,0x54,0xBD,0x3A
107 ,0x81,0xFF,0xF3,0x72,0x22,0xC2,0x84,0x30,0xA0,0x0A,0x70,0x61,0x04,0x42,0x98
108 ,0x7F,0x2C,0x76,0x91,0x38,0x00,0x22,0x08,0x61,0x88,0x40,0x48,0x69,0x3F,0x54
109 ,0x7D,0xB5,0x00,0x52,0xFF,0x3B,0x80,0x1B,0x5B,0x38,0x0D,0x0A,0x6F,0x30,0x00
110 ,0x00,0x20,0xB9,0x00,0x54,0x3D,0x9B,0x2B,0x55,0x3C,0xB0,0x04,0x52,0xBE,0x38
111 ,0x08,0x22,0x01,0xA6,0xD0,0x52,0x3F,0x3A,0x80,0x00,0x0F,0x8B,0x80,0x42,0x50
112 ,0x9B,0xA2,0x42,0xC0,0x3A,0x88,0x00,0xB4,0xB5,0x00,0x53,0x7C,0x31,0x1F,0xFF
113 ,0xE9,0x69,0x3F,0x44,0x0D,0x3B,0x80,0x01,0x25,0xB5,0x00,0x54,0xFD,0x7F,0x4E
114 ,0x54,0x3D,0xB5,0x00,0x44,0x00,0x3B,0x80,0x01,0x25,0x9B,0xA7,0x7E,0x9C,0x7D
115 ,0x8C,0x54,0x3D,0x7C,0x24,0xC4,0x85,0x6A,0x3E,0xD2,0xBC,0x61,0x64,0x44,0x00
116 ,0x3B,0x80,0x02,0xE4,0x30,0x50,0x00,0x80,0x9B,0xA2,0x52,0x3E,0x90,0x90,0xD4
117 ,0xBD,0xA2,0x12,0xFB,0x24,0xB5,0x00,0x4C,0x8D,0x3B,0x44,0x40,0xDA,0x31,0x10
118 ,0x00,0x1C,0xB5,0x00,0x4C,0xCD,0xB5,0x00,0x58,0x3B,0x3C,0xD8,0x03,0x00,0x8B
119 ,0x80,0x7D,0x7B,0xB5,0x00,0x7E,0x9C,0x6C,0x3F,0xFD,0x02,0x3B,0x80,0x10,0xA5
120 ,0xB5,0x00,0x54,0x7E,0xB5,0x00,0x54,0x3E,0x3A,0x98,0x00,0x5C,0xFB,0x00,0x71
121 ,0x40,0xF8,0x30,0x40,0x00,0x82,0x7F,0x2C,0xC2,0x59,0x3A,0xAB,0xFF,0xDE,0x7F
122 ,0x2C,0x78,0x6B,0x39,0x00,0x22,0x20,0x64,0x24,0x4A,0x49,0x3A,0x91,0xFF,0xFA
123 ,0x7F,0x30,0x79,0x14,0x3A,0x84,0x00,0x1A,0x3A,0xB0,0xFF,0xC4,0x30,0x45,0x00
124 ,0x00,0x61,0x20,0x78,0xF5,0x30,0x11,0x00,0x00,0x38,0x04,0x22,0x01,0x38,0x10
125 ,0x26,0x91,0x60,0xA8,0x40,0xE8,0x7F,0x2C,0x42,0x70,0x3B,0x80,0x40,0x59,0x3A
126 ,0x8B,0xFF,0xF2,0xB5,0x00,0x58,0x3F,0x3C,0xD8,0x03,0x00,0x8B,0x80,0x7D,0x7E
127 ,0xB5,0x00,0x7E,0x9C,0x3C,0xD0,0x00,0x8E,0x3C,0xCB,0xFF,0xB4,0x82,0x14,0x52
128 ,0xF6,0x6C,0x3C,0xD4,0x78,0xB5,0x00,0x55,0xFA,0x3B,0x46,0x41,0x1C,0x95,0xD4
129 ,0xF9,0x1A,0xB5,0x00,0x53,0x77,0xB5,0x00,0x52,0xB6,0x3C,0xC5,0x41,0x1B,0x6A
130 ,0x5C,0x62,0x0C,0x61,0x84,0xFE,0x9C,0x7F,0x20,0xC3,0x41,0x3C,0xC9,0xFF,0x72
131 ,0x30,0xA0,0x02,0x17,0x9B,0xA0,0xE2,0x0B,0x9B,0xC0,0xD6,0x7B,0x9B,0x00,0xD4
132 ,0xFC,0x8B,0x80,0x55,0x7D,0x30,0xB0,0x21,0x2C,0x73,0x05,0xD3,0xB7,0xB5,0x00
133 ,0x52,0x7F,0x3B,0x80,0x01,0xDA,0x3A,0x31,0xFF,0xFE,0x6A,0x1E,0x54,0xBA,0x7C
134 ,0x01,0x78,0x4A,0x6A,0x7E,0x52,0xB7,0x3B,0x80,0x01,0x00,0xB5,0x00,0x54,0x7A
135 ,0x9B,0xC0,0xD2,0xBF,0x90,0x94,0xD5,0x3D,0x3A,0x92,0x00,0x04,0x92,0x11,0xD5
136 ,0xBE,0x6A,0x1E,0x54,0xBA,0x3A,0x9B,0x00,0x05,0x7C,0x27,0x78,0x06,0xB5,0x00
137 ,0x55,0x7D,0x3B,0x44,0x41,0x23,0x80,0x18,0x54,0x7A,0xFB,0x00,0x71,0x41,0x36
138 ,0x80,0xB8,0x54,0xFC,0xB5,0x00,0x52,0xB6,0x82,0x14,0x7E,0x9C,0x3B,0x46,0x41
139 ,0x42,0x6A,0x5D,0xD4,0x38,0x3C,0xC5,0x41,0x3F,0xB5,0x00,0x43,0x0B,0x94,0x18
140 ,0xFE,0x9C,0xB5,0x00,0x43,0x0B,0x94,0x18,0xC0,0x41,0x3B,0x00,0x41,0x43,0xB5
141 ,0x00,0x58,0x39,0xB5,0x00,0x58,0x39,0x3C,0xD8,0x03,0x00,0x8B,0x80,0x7E,0x9C
142 ,0x3C,0xD0,0xFF,0x72,0x71,0x65,0x7D,0x2B,0x6A,0x7B,0xC2,0x91,0x6A,0xBC,0x62
143 ,0x4B,0x6A,0x3D,0xD2,0xF9,0xB5,0x00,0x58,0x7C,0x3B,0x80,0x16,0x94,0xB5,0x00
144 ,0x54,0xFA,0x7C,0x44,0xD4,0xB9,0x6A,0x1C,0x42,0x91,0x61,0xC0,0x7E,0x9C,0x6A
145 ,0xBE,0xD4,0xB7,0x3B,0x80,0x00,0xA9,0x9B,0xC3,0x62,0x09,0x6A,0xDC,0x54,0x3D
146 ,0x3A,0x90,0x00,0x10,0x61,0xCC,0x42,0x80,0x6A,0xBE,0xD4,0xBB,0x3B,0x80,0x00
147 ,0xA9,0x9B,0xC3,0x62,0x09,0x9B,0xA0,0xD4,0x38,0x3A,0x88,0x00,0x96,0x66,0x04
148 ,0x45,0x0A,0x79,0x40,0xC3,0x80,0x3A,0x89,0xFF,0x6B,0x80,0x18,0x54,0x3B,0xB5
149 ,0x00,0x54,0xFE,0x3B,0x80,0x02,0x8F,0x80,0xD4,0x74,0x57,0x7F,0x4E,0x54,0xBE
150 ,0xB5,0x00,0x42,0x88,0x3A,0x81,0x00,0x5B,0x6A,0x3F,0xD4,0xBB,0x3B,0x80,0x01
151 ,0x00,0x3C,0xC8,0xFF,0xD7,0x7F,0x4E,0x54,0xBF,0xB5,0x00,0x42,0x88,0x82,0x14
152 ,0x54,0x3A,0x3A,0x80,0x00,0x5C,0x3B,0x43,0x41,0x7C,0x6A,0x3C,0x55,0x38,0x3A
153 ,0x8A,0x00,0x95,0x31,0x1F,0xFF,0x6A,0x9B,0xA0,0xCC,0x0D,0x6A,0x7F,0xC3,0x90
154 ,0x6A,0x1B,0xF2,0x81,0xB5,0x00,0x74,0x57,0xFB,0x00,0x71,0x41,0x74,0x30,0xA0
155 ,0x0E,0xBA,0x3B,0x80,0x02,0x8F,0x80,0x18,0x61,0x85,0x6A,0x5D,0xD5,0x3F,0x3B
156 ,0x80,0x1A,0x0A,0x3C,0xC8,0xFF,0xD7,0x3B,0x00,0x41,0x7D,0xB5,0x00,0x54,0xBD
157 ,0xB5,0x00,0x54,0xBD,0x61,0x44,0x7E,0x9C,0x98,0xB5,0x54,0xBE,0xB5,0x00,0x7E
158 ,0x9C,0x82,0x14,0x43,0x08,0x3B,0x63,0x41,0x86,0xB5,0x00,0x54,0x37,0x3B,0x80
159 ,0x01,0x00,0x80,0xB8,0x74,0x57,0x3B,0x20,0x41,0x8F,0x6A,0x1D,0x54,0xB9,0x3A
160 ,0x89,0x00,0x22,0x3A,0x80,0x00,0x14,0x61,0xC4,0x42,0x80,0xD0,0x6C,0x74,0x57
161 ,0x30,0xA0,0x07,0xA1,0x94,0x03,0xD4,0x37,0x3B,0x80,0x00,0x14,0xB5,0x00,0x61
162 ,0x40,0x9B,0xA0,0xD4,0xBE,0x3A,0x81,0x00,0x97,0x61,0xC4,0x45,0x04,0x31,0x0F
163 ,0xFF,0x6A,0x6A,0x3C,0xCC,0x05,0x6A,0x5B,0xF2,0x81,0xB5,0x00,0x74,0x17,0x3B
164 ,0x80,0x02,0x8F,0x80,0x18,0x61,0x85,0x6A,0x5D,0xD5,0x39,0xB5,0x00,0x54,0x37
165 ,0x3B,0x80,0x17,0x99,0xB5,0x00,0x7E,0x9C,0x92,0x13,0xD4,0xB8,0x3B,0x62,0x41
166 ,0xB4,0x8B,0x80,0x42,0x88,0x90,0xB7,0xF8,0x0B,0xB2,0xD2,0xC2,0x80,0x92,0x17
167 ,0xC3,0x48,0x3B,0x42,0x41,0xB2,0xB5,0x00,0x54,0x3A,0x3A,0x90,0x00,0x5D,0xB5
168 ,0x00,0x43,0x10,0x82,0x18,0x7E,0x9C,0x3B,0x66,0x41,0xB2,0x3A,0x88,0x00,0x5E
169 ,0xB5,0x00,0x43,0x08,0x90,0xD8,0xFE,0x9C,0xA2,0x1A,0xC3,0x48,0x3B,0x66,0x41
170 ,0xB0,0x6C,0x1E,0x7E,0x4A,0x3B,0x00,0x41,0xB5,0xB5,0x00,0x7E,0x48,0x3B,0x00
171 ,0x41,0xB5,0xB5,0x00,0x58,0x3C,0x4B,0x00,0x71,0x41,0xB2,0x3B,0x00,0x41,0xB5
172 ,0xB5,0x00,0x58,0x3C,0x9B,0x9F,0xD8,0x3C,0x3C,0xD8,0x03,0x00,0x7F,0x4E,0x7D
173 ,0x55,0x39,0x84,0x80,0x02,0x39,0x86,0x80,0x02,0x30,0x40,0x01,0x00,0xA8,0x1E
174 ,0x7E,0x9C,0xA2,0x02,0x7E,0x9C,0x3B,0x43,0x41,0xC1,0x30,0x10,0x01,0xFF,0xA8
175 ,0x38,0xFE,0x9C,0x30,0x4F,0xFE,0x00,0xA9,0x26,0x7E,0x9C,0x3C,0xD8,0x03,0x00
176 ,0x9B,0x99,0x7E,0x9C,0xA0,0x86,0x7E,0x9C,0x93,0x00,0x71,0x41,0xDB,0x72,0x28
177 ,0x7B,0x01,0x32,0x00,0x40,0xA0,0x32,0x40,0x40,0xC0,0x32,0x80,0x41,0x00,0x7D
178 ,0xC1,0xFC,0x04,0x3B,0x00,0x41,0xF8,0x8B,0x20,0x68,0x60,0x38,0x14,0x00,0xD2
179 ,0x38,0x12,0x00,0xD5,0x38,0x10,0x00,0xD4,0x82,0x10,0x7E,0x9C,0x3B,0x45,0x41
180 ,0xF0,0x38,0x30,0x00,0xCF,0xB5,0x00,0x62,0xD8,0x60,0x09,0xE3,0x0A,0x60,0x24
181 ,0x78,0xDB,0x71,0xA5,0x40,0x1A,0x60,0x00,0x40,0x52,0x60,0x0C,0x40,0x50,0x3B
182 ,0x00,0x41,0xF2,0xB5,0x00,0x40,0x68,0x7F,0x29,0xE3,0x0A,0x7F,0x28,0x7E,0x48
183 ,0x90,0x24,0x81,0xA3,0x60,0x07,0xCC,0xA7,0x38,0x11,0x00,0xD4,0x38,0x31,0x00
184 ,0xCF,0x38,0x19,0x00,0xD2,0x38,0x13,0x00,0xD5,0x38,0x00,0x00,0xEF,0xA2,0x04
185 ,0x7E,0x9C,0x3B,0x64,0x41,0xE2,0x38,0x10,0x00,0xD4,0x38,0x20,0x00,0xA2,0x3C
186 ,0xD8,0x03,0x00,0xB5,0x00,0x42,0x07,0x03,0x00,0x70,0x00,0x03,0x21,0x00,0x71
187 ,0x0A,0x6F,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00
188 ,0x00,0x02,0x00,0x00,0x02,0x00,0x00,0x02,0x00,0x00,0x06,0x00,0x00,0x06,0x00
189 ,0x00,0x06,0x03,0x00,0x70,0x00,0x05,0x03,0x00,0x70,0x00,0x07,0x7B,0x00,0x71
190 ,0x03,0x00,0x00,0x00,0x01,0x00,0x1B,0xA7,0x00,0x40,0x00,0x00,0x3B,0x80,0x00
191 ,0x00,0x01,0x00,0x03,0xE7,0x00,0x40,0x2E,0x00,0x3B,0x80,0x00,0x00,0x01,0x00
192 ,0x0B,0x0D,0x00,0x40,0x59,0x00,0x3B,0x80,0x00,0x00,0x01,0x00,0x0B,0x23,0x00
193 ,0x40,0x6E,0x00,0x3B,0x80,0x00,0x00,0x01,0x00,0x0B,0xAB,0x00,0x40,0x84,0x00
194 ,0x3B,0x80,0x00,0x00,0x01,0x00,0x0D,0xF5,0x00,0x40,0x9A,0x00,0x3B,0x80,0x00
195 ,0x00,0x01,0x00,0x0D,0x24,0x00,0x41,0x46,0x00,0x3B,0x80,0x00,0x00,0x01,0x00
196 ,0x0A,0x6B,0x00,0x40,0xF3,0x00,0x3B,0x80,0x00,0x00,0x01,0x00,0x05,0xC6,0x00
197 ,0x41,0x0F,0x00,0x3B,0x80,0x00,0x00,0x01,0x00,0x05,0x7B,0x00,0x41,0xB7,0x00
198 ,0x3B,0x80,0x03,0x00,0x70,0x00,0x00,0x23,0x00,0x70,0x00,0x02,0x00,0x01,0x00
199 ,0x80,0x03,0x00,0x00,0x19,0x00,0x00,0x17,0x00,0x00,0x16,0x00,0x00,0x1D,0x00
200 ,0x00,0x1B,0x00,0x00,0x1B,0x00,0x00,0x1E,0x00,0x00,0x1C,0x00,0x00,0x1C,0x03
201 ,0x00,0x70,0x01,0x12
202 };
203 /* end binary data. size = 2224 bytes */
204
205 /* begin binary data: */
206 unsigned char speaker_data[] = { /* 423 */
207  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
208 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
209 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
210 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
211 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
212 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
213 ,0xFF,0xFC,0x03,0xFF,0xF8,0x41,0xFF,0xFD,0x44,0xFF,0xFD,0x9A,0x00,0x04,0xCA
214 ,0xFF,0xFB,0xD7,0x00,0x02,0x1A,0x00,0x01,0x28,0xFF,0xFF,0xBE,0xFF,0xF7,0x7D
215 ,0xFF,0xFD,0x0C,0xFF,0xF9,0x11,0x00,0x02,0x4A,0x00,0x00,0xF4,0xFF,0xF6,0x91
216 ,0x00,0x03,0x83,0x00,0x05,0xA7,0xFF,0xFA,0x1E,0xFF,0xFE,0x25,0xFF,0xF9,0xC4
217 ,0xFF,0xF8,0x65,0xFF,0xFE,0x43,0xFF,0xFC,0x41,0xFF,0xFD,0xDD,0xFF,0xFD,0xCD
218 ,0x00,0x03,0x15,0x00,0x01,0x9E,0xFF,0xF7,0x79,0xFF,0xF7,0x8B,0xFF,0xFF,0x1F
219 ,0xFF,0xFA,0x1A,0xFF,0xFB,0x89,0xFF,0xF8,0x78,0x00,0x02,0x91,0xFF,0xFF,0xBB
220 ,0xFF,0xFB,0x13,0x00,0x03,0x01,0x00,0x01,0x84,0xFF,0xF8,0x5D,0xFF,0xF1,0x38
221 ,0xFF,0xFE,0xEA,0xFF,0xF7,0x12,0x00,0x02,0xED,0xFF,0xFB,0x8D,0x00,0x00,0xF5
222 ,0x00,0x02,0x62,0xFF,0xF7,0xE0,0xFF,0xFA,0x0D,0xFF,0xFE,0x76,0xFF,0xFD,0xFC
223 ,0xFF,0xF8,0x46,0xFF,0xF5,0x6C,0xFF,0xFB,0xCA,0x00,0x00,0x8E,0xFF,0xF7,0xF0
224 ,0xFF,0xFB,0xD4,0xFF,0xFC,0x6C,0x00,0x00,0x0D,0x00,0x06,0xE1,0x00,0x02,0x5D
225 ,0xFF,0xF6,0x23,0xFF,0xF9,0x7E,0xFF,0xF7,0x51,0xFF,0xF1,0xE1,0xFF,0xF9,0xD8
226 ,0xFF,0xEE,0xC5,0xFF,0xFB,0x11,0x00,0x0A,0xC3,0x00,0x18,0x1C,0x00,0x24,0x81
227 ,0x00,0x1B,0x9C,0x00,0x01,0xF7,0xFF,0xF1,0xF7,0xFF,0xCF,0x95,0xFF,0xBD,0x0D
228 ,0xFF,0xB3,0x57,0xFF,0xDE,0xD5,0xFF,0xFD,0xCB,0x00,0x35,0x80,0x00,0x67,0x11
229 ,0x00,0x7C,0x80,0x00,0x63,0x57,0x00,0x32,0x02,0xFF,0xBF,0x6F,0xFF,0xA6,0xD4
230 ,0x07,0x23,0x71,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
231 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
232 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4F,0x70,0x1D
233 ,0x66,0x66,0x66,0x26,0x66,0x66,0x26,0x66,0x66,0x24,0xCC,0xCD,0x1E,0x66,0x67
234 ,0x00,0x02,0xE7,0x00,0x02,0xE7,0x04,0x00,0x00,0x00,0x45,0x1F,0x19,0x00,0x00
235 ,0x03,0x7B,0x4A
236 };
237 /* end binary data. size = 423 bytes */
238
239 /* begin binary data: */
240 unsigned char preset_data[] = { /* 87 */
241  0x00,0x00,0x00,0x00,0x00,0xFA,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x1F,0x40
242 ,0x00,0x00,0x00,0x00,0x01,0x2C,0x01,0x47,0xAE,0x00,0x2B,0xB0,0x00,0x00,0x00
243 ,0x00,0x20,0xC5,0x00,0x80,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x08,0x00,0x00
244 ,0x08,0x00,0x00,0x05,0x00,0x00,0x00,0x80,0x00,0x00,0x0C,0xCD,0x00,0x80,0x00
245 ,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21
246 ,0x08,0x00,0x00,0x08,0x00,0x00,0x00,0x0C,0xCD,0x00,0x00,0x03
247 };
248 /* end binary data. size = 87 bytes */
249
250 /* begin binary data: */
251 unsigned char config_data[] = { /* 165 */
252  0x09,0xF3,0x33,0x01,0x3E,0x66,0x00,0x51,0xEC,0x00,0x00,0x14,0x00,0x00,0x02
253 ,0x1A,0xDB,0xA7,0x1B,0x36,0x15,0x1C,0x4E,0xAD,0x00,0x00,0x00,0x00,0x00,0x01
254 ,0x00,0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x21,0x00,0x00,0x21
255 ,0x00,0x00,0x00,0x00,0x00,0xFA,0x00,0x00,0x01,0x00,0x00,0x02,0x00,0x00,0x01
256 ,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x80,0x00,0x01,0x40,0x00,0x00,0x03,0x47
257 ,0x01,0x47,0xAE,0x00,0x19,0x9A,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x80,0x00
258 ,0x00,0x00,0x01,0x05,0x00,0x00,0x00,0x80,0x00,0x00,0x0F,0xFF,0x07,0xC2,0x8F
259 ,0x00,0x10,0x00,0x08,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x80,0x00,0x00,0x01
260 ,0x00,0x00,0x01,0x01,0x47,0xAE,0x00,0x00,0x40,0x00,0x00,0x00,0x19,0x99,0x9A
261 ,0x00,0x80,0x00,0x00,0x00,0x02,0x00,0x00,0x10,0x00,0x00,0x02,0x00,0x00,0x30
262 ,0xEC,0x00,0x00,0x00,0x03,0xD7,0x01,0x00,0x00,0x08,0x00,0x00,0x01,0x00,0x00
263 };
264 /* end binary data. size = 165 bytes */
265
266
267 void convertBytes2Data(int num_bytes, const unsigned char bytes[], int data[])
268 {
269         int i; /* index for data */
270         int k; /* index for bytes */
271         int d;
272         int num_data = num_bytes/3;
273
274         for (i = 0, k = 0; i < num_data; ++i, k += 3) {
275                 d = (bytes[k] << 16) | (bytes[k+1] << 8) | (bytes[k+2]);
276                 if (bytes[k] & 0x80) {/* sign bit was set*/
277                         d = - ((1<<24)-d);
278                 }
279                 data[i] = d;
280         }
281 }
282
283 int ProcessPatchFile(struct tfa9887_priv *tfa9887, int length, const unsigned char* bytes)
284 {
285         unsigned int size;
286         int index = 0;
287         unsigned char buffer[MAX_I2C_LENGTH];
288         int error;
289         int value = 0;
290         unsigned int status;
291
292         error = Tfa9887_ReadRegister(tfa9887, TFA9887_STATUS, &status);
293         if (error == Tfa9887_Error_Ok) {
294                 if ( (status & 0x0043) != 0x0043) {
295                         /* one of Vddd, PLL and clocks not ok */
296                         error = -1;
297                 }
298         }
299         pr_info("tfa status %u\n",status);
300         error = DspReadMem(tfa9887, 0x2210, 1, &value);
301         pr_info("tfa Version  %x\n",value);
302         while (index < length) {
303                 /* extract little endian length */
304                 size = bytes[index] + bytes[index+1] * 256;
305                 index += 2;
306                 if ( (index + size) > length) {
307                         /* too big, outside the buffer, error in the input data */
308                         return -1;
309                 }
310                 memcpy(buffer, bytes + index, size);
311                 error = regmap_raw_write(tfa9887->regmap, buffer[0], &buffer[1] ,(size -1) * 2);
312                 if (error != Tfa9887_Error_Ok) {
313                         break;
314                 }
315                 index += size;
316         }
317         return error;
318 }
319
320 int DspReadMem(struct tfa9887_priv *tfa9887, unsigned short start_offset, int num_words, int *pValues)
321 {
322         unsigned int cf_ctrl; /* the value to sent to the CF_CONTROLS register */
323         unsigned char bytes[MAX_I2C_LENGTH];
324         int burst_size; /* number of words per burst size */
325         int bytes_per_word=3;
326         int num_bytes;
327         int* p;
328         int error;
329         /* first set DMEM and AIF, leaving other bits intact */
330         error = Tfa9887_ReadRegister(tfa9887, TFA9887_CF_CONTROLS, &cf_ctrl);
331         if (error != Tfa9887_Error_Ok) {
332                 return error;
333         }
334         cf_ctrl &= ~0x000E; /* clear AIF & DMEM */
335         cf_ctrl |= (Tfa9887_DMEM_XMEM<<1); /* set DMEM, leave AIF cleared for autoincrement */
336         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_CONTROLS, cf_ctrl);
337         if (error != Tfa9887_Error_Ok) {
338                 return error;
339         }
340         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_MAD, start_offset);
341         if (error != Tfa9887_Error_Ok) {
342                 return error;
343         }
344         
345         num_bytes = num_words*bytes_per_word;
346         p=pValues;
347         for (; num_bytes > 0; ) {
348                 burst_size = ROUND_DOWN(MAX_I2C_LENGTH, bytes_per_word);
349                 if (num_bytes < burst_size) {
350                         burst_size = num_bytes;
351                 }
352
353                 error = regmap_raw_read(tfa9887->regmap, TFA9887_CF_MEM, bytes,burst_size * 2);
354                 if (error != Tfa9887_Error_Ok) {
355                         return error;
356                 }
357                 convertBytes2Data(burst_size, bytes, p);
358                 num_bytes -= burst_size;
359                 p += burst_size/bytes_per_word;
360         }
361         return Tfa9887_Error_Ok;
362 }
363
364 int DspWriteMem(struct tfa9887_priv *tfa9887, unsigned int address, int value)
365 {
366         unsigned int cf_ctrl; /* the value to sent to the CF_CONTROLS register */
367         unsigned int bytes[3];
368         int error;
369         /* first set DMEM and AIF, leaving other bits intact */
370         error = Tfa9887_ReadRegister(tfa9887, TFA9887_CF_CONTROLS, &cf_ctrl);
371         if (error != Tfa9887_Error_Ok) {
372                 return error;
373         }
374         cf_ctrl &= ~0x000E; /* clear AIF & DMEM */
375         cf_ctrl |= (Tfa9887_DMEM_XMEM<<1); /* set DMEM, leave AIF cleared for autoincrement */
376         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_CONTROLS, cf_ctrl);
377         if (error != Tfa9887_Error_Ok) {
378                 return error;
379         }
380         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_MAD, address);
381         if (error != Tfa9887_Error_Ok) {
382                 return error;
383         }
384         //convertData2Bytes(1, &value, bytes);
385         error = regmap_raw_write(tfa9887->regmap, TFA9887_CF_MEM,bytes, 3 * 2);
386         if (error != Tfa9887_Error_Ok) {
387                 return error;
388         }
389         return Tfa9887_Error_Ok;
390
391 }
392
393 int DspSetParam(struct tfa9887_priv *tfa9887,unsigned char module_id, unsigned char param_id, int num_bytes, const unsigned char *data)
394 {
395
396         int error;
397         unsigned int cf_ctrl = 0x0002; /* the value to be sent to the CF_CONTROLS register: cf_req=00000000, cf_int=0, cf_aif=0, cf_dmem=XMEM=01, cf_rst_dsp=0 */
398         unsigned int cf_mad = 0x0001; /* memory address to be accessed (0 : Status, 1 : ID, 2 : parameters) */
399         unsigned int cf_status; /* the contents of the CF_STATUS register */
400         unsigned int id[3];
401         int tries = 0;
402         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_CONTROLS, cf_ctrl);
403         if (error == Tfa9887_Error_Ok) {
404                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_MAD, cf_mad);
405         }
406         if (error == Tfa9887_Error_Ok) {
407                 id[0] = 0;
408                 id[1] = module_id+128;
409                 id[2] = param_id;
410                 error = regmap_raw_write(tfa9887->regmap, TFA9887_CF_MEM,id, 3*2);
411         }
412         if (error == Tfa9887_Error_Ok) {
413                 error = regmap_raw_write(tfa9887->regmap, TFA9887_CF_MEM, data, num_bytes*2);
414         }
415
416         cf_ctrl |= (1<<8) | (1<<4); /* set the cf_req1 and cf_int bit */
417         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_CONTROLS, cf_ctrl);
418         pr_info("Writing TFA9887_CF_MEM %d \n",error);
419
420         do {
421                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_CF_STATUS, &cf_status);
422                 tries++;
423         } while ( (error==Tfa9887_Error_Ok) && ((cf_status & 0x0100) == 0) && (tries < 10) ); /* don't wait forever, DSP is pretty quick to respond (< 1ms) */
424
425         if (tries >= 10) {
426                 /* something wrong with communication with DSP */
427                 pr_info("Setparam something wrong\n");
428                 error = -1;
429         }
430         return error;
431 }
432
433 int DspGetParam(struct tfa9887_priv *tfa9887, unsigned char module_id, unsigned char param_id, int num_bytes, const unsigned char *data)
434 {
435
436         unsigned int cf_ctrl = 0x0002; /* the value to be sent to the CF_CONTROLS register: cf_req=00000000, cf_int=0, cf_aif=0, cf_dmem=XMEM=01, cf_rst_dsp=0 */
437         unsigned int cf_mad = 0x0001; /* memory address to be accessed (0 : Status, 1 : ID, 2 : parameters) */
438         unsigned int cf_status; /* the contents of the CF_STATUS register */
439         int error;
440         int tries = 0;
441         unsigned int id[3];
442
443         error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_CONTROLS, cf_ctrl);
444
445         if (error == Tfa9887_Error_Ok) {
446                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_MAD, cf_mad);
447         }
448
449         if (error == Tfa9887_Error_Ok) {
450                 id[0] = 0;
451                 id[1] = module_id+128;
452                 id[2] = param_id;
453                 /* only try MEM once, if error, need to resend mad as well */
454                 error = regmap_raw_write(tfa9887->regmap, TFA9887_CF_MEM, id, 3*2);
455         }
456         /* 2) wake up the DSP and let it process the data */
457         if (error == Tfa9887_Error_Ok) {
458                 cf_ctrl |= (1<<8) | (1<<4); /* set the cf_req1 and cf_int bit */
459                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_CONTROLS, cf_ctrl);
460         }
461
462         /* 3) wait for the ack */
463         if (error == Tfa9887_Error_Ok) {
464                 do {
465                         error = Tfa9887_ReadRegister(tfa9887, TFA9887_CF_STATUS, &cf_status);
466                         tries++;
467                 } while ( (error==Tfa9887_Error_Ok) && ((cf_status & 0x0100) == 0) && (tries < 10) ); /* don't wait forever, DSP is pretty quick to respond (< 1ms) */
468
469                 if (tries >= 10) {
470                         /* something wrong with communication with DSP */
471                         pr_info("Something wrong with DSP\n");
472                         return -1;
473                 }
474         }
475         /* 5) read the resulting data */
476         if (error == Tfa9887_Error_Ok) {
477                 cf_mad = 0x0002; /* memory address to be accessed (0 : Status, 1 : ID, 2 : parameters) */
478                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_CF_MAD, cf_mad);
479         }
480
481         if (error == Tfa9887_Error_Ok) {
482                 error = regmap_raw_read(tfa9887->regmap, TFA9887_CF_MEM,data,num_bytes*2);
483         }
484         return error;
485 }
486
487 int Tfa9887_WriteRegister(struct tfa9887_priv *tfa9887, unsigned int subaddress, unsigned int value)
488 {
489          int error = regmap_write(tfa9887->regmap,subaddress,value);
490          return error;
491 }
492
493 int Tfa9887_ReadRegister(struct tfa9887_priv *tfa9887, unsigned int subaddress, unsigned int* pValue)
494 {
495          int error = regmap_read(tfa9887->regmap,subaddress,pValue);
496          return error;
497 }
498
499 int Tfa9887_Init(void)
500 {
501         int error = 0;
502         if(tfa9887R->deviceInit)
503                 error = Init(tfa9887R);
504         if(tfa9887L->deviceInit)
505                 error = Init(tfa9887L);
506         if (error != 0)
507                 pr_info("Failed to Init tfa\n");
508         return error;
509 }
510
511 int coldStarup(struct tfa9887_priv *tfa9887)
512 {
513         int error,volume_value;
514         unsigned int value;
515
516         error = Tfa9887_WriteRegister(tfa9887, 0x09, 0x0002);
517         if (Tfa9887_Error_Ok == error) {
518                 error = Tfa9887_ReadRegister(tfa9887, 0x09, &value);
519         }
520         if (Tfa9887_Error_Ok == error) {
521                 /* DSP must be in control of the amplifier to avoid plops */
522                 value |= TFA9887_SYSCTRL_SEL_ENBL_AMP;
523                 error = Tfa9887_WriteRegister(tfa9887, 0x09, value);
524         }
525
526         /* some other registers must be set for optimal amplifier behaviour */
527         if (Tfa9887_Error_Ok == error) {
528                 error = Tfa9887_WriteRegister(tfa9887, 0x40, 0x5A6B);
529         }
530         if (Tfa9887_Error_Ok == error) {
531                 error = Tfa9887_WriteRegister(tfa9887, 0x05, 0x13AB);
532         }
533         if (Tfa9887_Error_Ok == error) {
534                 error = Tfa9887_WriteRegister(tfa9887, 0x06, 0x001F);
535         }
536         if (Tfa9887_Error_Ok == error) {
537                 error = Tfa9887_WriteRegister(tfa9887,
538                 TFA9887_SPKR_CALIBRATION, 0x0C4F); /*  adjusted to mandatory defaults */
539         }
540         if (Tfa9887_Error_Ok == error) {
541                 error = Tfa9887_WriteRegister(tfa9887, 0x09, 0x025D);
542         }
543         if (Tfa9887_Error_Ok == error) {
544                 error = Tfa9887_WriteRegister(tfa9887, 0x0A, 0x3EC3);
545         }
546         if (Tfa9887_Error_Ok == error) {
547                 error = Tfa9887_WriteRegister(tfa9887, 0x41, 0x0308);
548         }
549         if (Tfa9887_Error_Ok == error) {
550                 error = Tfa9887_WriteRegister(tfa9887, 0x48, 0x0180);
551         }
552         if (Tfa9887_Error_Ok == error) {
553                 error = Tfa9887_WriteRegister(tfa9887, 0x49, 0x0E82);
554         }
555         if (Tfa9887_Error_Ok == error) {
556                 error = Tfa9887_WriteRegister(tfa9887, 0x52, 0x0000);
557         }
558         if (Tfa9887_Error_Ok == error) {
559                 error = Tfa9887_WriteRegister(tfa9887, 0x40, 0x0000);
560         }
561         //Set Sampling Frequency
562         error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_CONTROL, &value);
563         if (error == Tfa9887_Error_Ok) {
564                 // clear the 4 bits first
565                 value &= (~(0xF<<TFA9887_I2SCTRL_RATE_SHIFT));
566
567                 value |= TFA9887_I2SCTRL_RATE_48000;
568                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_CONTROL, value);
569         }
570         //SelectAmplifierInput
571         error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_CONTROL, &value);
572         if (error == Tfa9887_Error_Ok) {
573                 // clear the 2 bits first
574                 value &= ~(0x3<<TFA9887_I2SCTRL_INPUT_SEL_SHIFT);
575
576                 value |=(0x2<<TFA9887_I2SCTRL_INPUT_SEL_SHIFT);
577                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_CONTROL, value);
578         }
579         //Set Max Volume
580         error = Tfa9887_ReadRegister(tfa9887, TFA9887_AUDIO_CONTROL, &value);
581         if(error == Tfa9887_Error_Ok) {
582                 volume_value = 2;
583                 value = (value&0x00FF) | (unsigned int)(volume_value<<8);
584                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_AUDIO_CONTROL, value);
585         }
586
587         //PowerUp
588         error = Tfa9887_ReadRegister(tfa9887, TFA9887_SYSTEM_CONTROL, &value);
589         if(error == Tfa9887_Error_Ok) {
590                 value &= ~(TFA9887_SYSCTRL_POWERDOWN);
591                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_SYSTEM_CONTROL, value);
592         }
593
594         //Firmware
595         error = ProcessPatchFile(tfa9887, 10, coldpatch_data);
596         error = ProcessPatchFile(tfa9887, 2224, n1d2_data);
597
598         return error;
599 }
600
601 int Init(struct tfa9887_priv *tfa9887)
602 {
603         int error;
604         unsigned int value;
605
606         error = coldStarup(tfa9887);
607         if(error != Tfa9887_Error_Ok) {
608                 pr_info("ColdStartup Failed\n");
609         }
610
611         //error = loadSettings(tfa9887);
612         if(error != Tfa9887_Error_Ok) {
613                 pr_info("Loading Settings Failed\n");
614         }
615
616         error = stereoRouting(tfa9887);
617
618         if(error != Tfa9887_Error_Ok) {
619                 pr_info("Stereo routing Failed\n");
620         }
621
622          //SetConfigured
623         error = Tfa9887_ReadRegister(tfa9887, TFA9887_SYSTEM_CONTROL, &value);
624         if(error == Tfa9887_Error_Ok)
625         {
626                 value |= TFA9887_SYSCTRL_CONFIGURED;
627                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_SYSTEM_CONTROL, value);
628         }
629          //PowerDown
630         if(error == Tfa9887_Error_Ok)
631         {
632                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_SYSTEM_CONTROL, &value);
633                 value |= TFA9887_SYSCTRL_POWERDOWN;
634                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_SYSTEM_CONTROL, value);
635         }
636         return error;
637 }
638
639
640 int loadSettings(struct tfa9887_priv *tfa9887)
641 {
642         int error;
643         //Load settings
644         error = DspSetParam(tfa9887,MODULE_SPEAKERBOOST, PARAM_SET_LSMODEL, 423, speaker_data);
645         error = DspSetParam(tfa9887, MODULE_SPEAKERBOOST, PARAM_SET_CONFIG, 165, config_data);
646         error = DspSetParam(tfa9887, MODULE_SPEAKERBOOST, PARAM_SET_PRESET, 87, preset_data);
647         return error;
648 }
649
650 int stereoRouting(struct tfa9887_priv *tfa9887)
651 {
652         int error;
653         unsigned int value;
654         if(tfa9887 == tfa9887L) {
655                 //select channel
656                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_CONTROL, &value);
657                 // clear the 2 bits first
658                 value &= ~(0x3<<TFA9887_I2SCTRL_CHANSEL_SHIFT);
659
660                 value |=(1<<TFA9887_I2SCTRL_CHANSEL_SHIFT);
661                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_CONTROL, value);
662
663                 //select ouput left for gain
664                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_SEL, &value);
665                 value &= ~(0x7<<TFA9887_I2SSEL_I2SOUT_LEFT_SHIFT);
666                 value |=(1<<TFA9887_I2SSEL_I2SOUT_LEFT_SHIFT);
667                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_SEL, value);
668                 //Select stereo gain
669                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_CONTROL, &value);
670                 value &= ~(0x1<<TFA9887_I2SCTRL_DATAI2_SHIFT);
671                 value |=(1<<TFA9887_I2SCTRL_DATAI2_SHIFT);
672                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_CONTROL, value);
673
674                 //select output right for current sense
675                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_SEL, &value);
676                 value &= ~(0x7<<TFA9887_I2SSEL_I2SOUT_RIGHT_SHIFT);
677                 value |=(0<<TFA9887_I2SSEL_I2SOUT_RIGHT_SHIFT);
678                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_SEL, value);
679                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_SEL, value);
680                 pr_info("Tfa inside left\n");
681
682          }
683          else if (tfa9887 == tfa9887R) {
684                 // clear the 2 bits first
685                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_CONTROL, &value);
686                 value &= ~(0x3<<TFA9887_I2SCTRL_CHANSEL_SHIFT);
687
688                 value |=(2<<TFA9887_I2SCTRL_CHANSEL_SHIFT);
689                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_CONTROL, value);
690
691                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_SEL, &value);
692                 value &= ~(0x7<<TFA9887_I2SSEL_I2SOUT_RIGHT_SHIFT);
693                 value |=(1<<TFA9887_I2SSEL_I2SOUT_RIGHT_SHIFT);
694                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_SEL, value);
695
696                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_CONTROL, &value);
697                 value &= ~(0x1<<TFA9887_I2SCTRL_DATAI2_SHIFT);
698                 value |=(0<<TFA9887_I2SCTRL_DATAI2_SHIFT);
699                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_CONTROL, value);
700
701                 error = Tfa9887_ReadRegister(tfa9887, TFA9887_I2S_SEL, &value);
702                 value &= ~(0x7<<TFA9887_I2SSEL_I2SOUT_LEFT_SHIFT);
703                 value |=(0<<TFA9887_I2SSEL_I2SOUT_LEFT_SHIFT);
704                 error = Tfa9887_WriteRegister(tfa9887, TFA9887_I2S_SEL, value);
705                 pr_info("tfa inside right\n");
706
707         }
708         else
709         {
710                 error = -1;
711         }
712         return error;
713 }
714
715 int Tfa9887_Powerdown(int powerdown)
716 {
717         int error = 0;
718         if(tfa9887R->deviceInit)
719                 error = Powerdown(tfa9887R, powerdown);
720         if(tfa9887L->deviceInit)
721                 error = Powerdown(tfa9887L, powerdown); 
722         return error;
723 }
724
725 EXPORT_SYMBOL(Tfa9887_Powerdown);
726
727 int Powerdown(struct tfa9887_priv *tfa9887, int powerdown)
728 {
729         int error;
730         unsigned int value;
731
732         /* read the SystemControl register, modify the bit and write again */
733         error = Tfa9887_ReadRegister(tfa9887, TFA9887_SYSTEM_CONTROL, &value);
734         if (error != Tfa9887_Error_Ok) {
735                 return error;
736         }
737
738         switch(powerdown) {
739                 case 1:
740                         value |= TFA9887_SYSCTRL_POWERDOWN;
741                         break;
742                 case 0:
743                         value &= ~(TFA9887_SYSCTRL_POWERDOWN);
744                         break;
745                 default:
746                 return -1;
747         }
748         error = Tfa9887_WriteRegister(tfa9887, TFA9887_SYSTEM_CONTROL, value);
749         return error;
750 }
751
752 bool tfa9887_readable_register(struct device *dev, unsigned int reg)
753 {
754          return true;
755 }
756
757 bool tfa9887_volatile_register(struct device *dev, unsigned int reg)
758 {
759          return true;
760 }
761
762 static const struct regmap_config tfa9887_regmap = {
763         .reg_bits = 8,
764         .val_bits = 16,
765         .volatile_reg = tfa9887_volatile_register,
766         .readable_reg = tfa9887_readable_register,
767         .cache_type = REGCACHE_RBTREE,
768 };
769
770 static ssize_t tfa9887_cal_show(struct kobject *kobj,
771                 struct kobj_attribute *attr, char *buf)
772 {
773         printk("!tfa9887_cal_show\n");
774         return 0;
775 }
776
777 static ssize_t tfa9887_cal_store(struct kobject *kobj,
778         struct kobj_attribute *attr, const char *buf, size_t count)
779 {
780         ssize_t ret = count;
781
782         printk("+tfa9887_cal_store: %p, %d\n", buf, count);
783
784         if (!buf || !count) {
785                 ret = -EINVAL;
786                 goto fail;
787         }
788
789 fail:
790         printk("-tfa9887_cal_store: %d\n", count);
791         return ret;
792 }
793
794
795 static ssize_t tfa9887_config_show(struct kobject *kobj,
796                 struct kobj_attribute *attr, char *buf)
797 {
798         printk("!tfa9887_config_show\n");
799         return 0;
800 }
801
802 static ssize_t tfa9887_config_store(struct kobject *kobj,
803         struct kobj_attribute *attr, const char *buf, size_t count)
804 {
805         ssize_t ret = count;
806
807         printk("+tfa9887_config_store: %p, %d\n", buf, count);
808
809         if (!buf || !count) {
810                 ret = -EINVAL;
811                 goto fail;
812         }
813
814 fail:
815         printk("-tfa9887_config_store: %d\n", count);
816         return ret;
817 }
818
819 static ssize_t tfa9887_vol_show(struct kobject *kobj,
820                 struct kobj_attribute *attr, char *buf)
821 {
822         printk("!tfa9887_vol_show\n");
823         return 0;
824 }
825
826 static ssize_t tfa9887_vol_store(struct kobject *kobj,
827         struct kobj_attribute *attr, const char *buf, size_t count)
828 {
829         ssize_t ret = count;
830
831         printk("+tfa9887_vol_store: %p, %d\n", buf, count);
832
833         if (!buf || !count) {
834                 ret = -EINVAL;
835                 goto fail;
836         }
837
838 fail:
839         printk("-tfa9887_vol_store: %d\n", count);
840         return ret;
841 }
842
843 static __devinit int tfa9887R_i2c_probe(struct i2c_client *i2c,
844                                       const struct i2c_device_id *id)
845 {
846         unsigned int val;
847         int ret;
848
849         pr_info("tfa9887R_i2c_probe\n");
850         tfa9887R = devm_kzalloc(&i2c->dev,  sizeof(struct tfa9887_priv),
851                               GFP_KERNEL);
852         if (tfa9887R == NULL)
853                 return -ENOMEM;
854         tfa9887R->regmap = regmap_init_i2c(i2c, &tfa9887_regmap);
855         if (IS_ERR(tfa9887R->regmap)) {
856                 ret = PTR_ERR(tfa9887R->regmap);
857                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
858                         ret);
859                 return ret;
860         }
861
862         i2c_set_clientdata(i2c, tfa9887R);
863         tfa9887R->irq = i2c->irq;
864         ret = regmap_read(tfa9887R->regmap, TFA9887_REVISIONNUMBER, &val);
865         if (ret != 0) {
866                 dev_err(&i2c->dev, "Failed to read chip revision: %d\n", ret);
867                 goto err;
868         }
869         dev_info(&i2c->dev, "TFA9887 revision %d\n",val);
870         tfa9887_kobj = kobject_create_and_add("tfa9887", kernel_kobj);
871
872         ret = sysfs_create_file(tfa9887_kobj, &tfa9887_config);
873         printk("tfa9887_add_sysfs ret=%d",ret);
874         if (ret != 0) {
875                 dev_err(&i2c->dev, "Failed to add sysfs: %d\n", ret);
876                 goto err;
877         }
878         ret = sysfs_create_file(tfa9887_kobj, &tfa9887_cal);
879         printk("tfa9887_add_sysfs ret=%d",ret);
880         if (ret != 0) {
881                 dev_err(&i2c->dev, "Failed to add sysfs: %d\n", ret);
882                 goto err;
883         }
884         ret = sysfs_create_file(tfa9887_kobj, &tfa9887_vol);
885         printk("tfa9887_add_sysfs ret=%d",ret);
886         if (ret != 0) {
887                 dev_err(&i2c->dev, "Failed to add sysfs: %d\n", ret);
888                 goto err;
889         }
890         tfa9887R->deviceInit = true;
891         return 0;
892 err:
893         regmap_exit(tfa9887R->regmap);
894         return ret;
895 }
896
897 static __devexit int tfa9887R_i2c_remove(struct i2c_client *client)
898 {
899         struct tfa9887_priv *tfa9887R = i2c_get_clientdata(client);
900         regmap_exit(tfa9887R->regmap);
901         sysfs_remove_file(tfa9887_kobj, &tfa9887_config);
902         sysfs_remove_file(tfa9887_kobj, &tfa9887_cal);
903         sysfs_remove_file(tfa9887_kobj, &tfa9887_vol);
904         kobject_del(tfa9887_kobj);
905         return 0;
906 }
907
908 static const struct of_device_id tfa9887R_of_match[] = {
909         { .compatible = "nxp,tfa9887R", },
910         {},
911 };
912 MODULE_DEVICE_TABLE(of, tfa9887R_of_match);
913
914 static const struct i2c_device_id tfa9887R_i2c_id[] = {
915         { "tfa9887R", 0 },
916         { }
917 };
918 MODULE_DEVICE_TABLE(i2c, tfa9887R_i2c_id);
919
920 static struct i2c_driver tfa9887R_i2c_driver = {
921          .driver = {
922          .name = "tfa9887R",
923          .owner = THIS_MODULE,
924          .of_match_table = tfa9887R_of_match,
925          },
926          .probe =    tfa9887R_i2c_probe,
927          .remove =   __devexit_p(tfa9887R_i2c_remove),
928          .id_table = tfa9887R_i2c_id,
929 };
930
931 static __devinit int tfa9887L_i2c_probe(struct i2c_client *i2c,
932                                       const struct i2c_device_id *id)
933 {
934         unsigned int val;
935         int ret;
936
937         pr_info("tfa9887L_i2c_probe\n");
938         tfa9887L = devm_kzalloc(&i2c->dev,  sizeof(struct tfa9887_priv),
939                               GFP_KERNEL);
940         if (tfa9887L == NULL)
941                 return -ENOMEM;
942         tfa9887L->regmap = regmap_init_i2c(i2c, &tfa9887_regmap);
943         if (IS_ERR(tfa9887L->regmap)) {
944                 ret = PTR_ERR(tfa9887L->regmap);
945                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
946                         ret);
947                 return ret;
948         }
949
950         i2c_set_clientdata(i2c, tfa9887L);
951         tfa9887L->irq = i2c->irq;
952         ret = regmap_read(tfa9887L->regmap, TFA9887_REVISIONNUMBER, &val);
953         if (ret != 0) {
954                 dev_err(&i2c->dev, "Failed to read chip revision: %d\n", ret);
955                 goto err;
956         }
957         dev_info(&i2c->dev, "TFA9887 revision %d\n",val);
958         tfa9887L->deviceInit = true;    
959         return 0;
960 err:
961         regmap_exit(tfa9887L->regmap);
962         return ret;
963 }
964
965 static __devexit int tfa9887L_i2c_remove(struct i2c_client *client)
966 {
967         struct tfa9887_priv *tfa9887L = i2c_get_clientdata(client);
968         regmap_exit(tfa9887L->regmap);
969         return 0;
970 }
971
972 static const struct of_device_id tfa9887L_of_match[] = {
973         { .compatible = "nxp,tfa9887L", },
974         {},
975 };
976 MODULE_DEVICE_TABLE(of, tfa9887L_of_match);
977
978 static const struct i2c_device_id tfa9887L_i2c_id[] = {
979         { "tfa9887L", 0 },
980         { }
981 };
982 MODULE_DEVICE_TABLE(i2c, tfa9887L_i2c_id);
983
984 static struct i2c_driver tfa9887L_i2c_driver = {
985          .driver = {
986          .name = "tfa9887L",
987          .owner = THIS_MODULE,
988          .of_match_table = tfa9887L_of_match,
989          },
990          .probe =    tfa9887L_i2c_probe,
991          .remove =   __devexit_p(tfa9887L_i2c_remove),
992          .id_table = tfa9887L_i2c_id,
993 };
994
995 static int __init tfa9887_modinit(void)
996 {
997          int ret = 0;
998          ret = i2c_add_driver(&tfa9887R_i2c_driver);
999          if (ret != 0) {
1000          printk(KERN_ERR "Failed to register tfa9887 I2C driver: %d\n",
1001             ret);
1002          }
1003          ret = i2c_add_driver(&tfa9887L_i2c_driver);
1004          if (ret != 0) {
1005          printk(KERN_ERR "Failed to register tfa9887 I2C driver: %d\n",
1006             ret);
1007          }
1008          return ret;
1009 }
1010 module_init(tfa9887_modinit);
1011
1012 static void __exit tfa9887_exit(void)
1013 {
1014          i2c_del_driver(&tfa9887R_i2c_driver);
1015          i2c_del_driver(&tfa9887L_i2c_driver);
1016 }
1017 module_exit(tfa9887_exit);
1018