ASoC:ma120x0p: Corrects the volume level display
[platform/kernel/linux-rpi.git] / sound / soc / codecs / ma120x0p.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ASoC Driver for Infineon Merus(TM) ma120x0p multi-level class-D amplifier
4  *
5  * Authors:     Ariel Muszkat <ariel.muszkat@gmail.com>
6  * Jorgen Kragh Jakobsen <jorgen.kraghjakobsen@infineon.com>
7  *
8  * Copyright (C) 2019 Infineon Technologies AG
9  *
10  */
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/i2c.h>
17 #include <linux/of_device.h>
18 #include <linux/spi/spi.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31 #include <linux/interrupt.h>
32
33 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/fs.h>
36 #include <linux/uaccess.h>
37
38 #ifndef _MA120X0P_
39 #define _MA120X0P_
40 //------------------------------------------------------------------manualPM---
41 // Select Manual PowerMode control
42 #define ma_manualpm__a 0
43 #define ma_manualpm__len 1
44 #define ma_manualpm__mask 0x40
45 #define ma_manualpm__shift 0x06
46 #define ma_manualpm__reset 0x00
47 //--------------------------------------------------------------------pm_man---
48 // manual selected power mode
49 #define ma_pm_man__a 0
50 #define ma_pm_man__len 2
51 #define ma_pm_man__mask 0x30
52 #define ma_pm_man__shift 0x04
53 #define ma_pm_man__reset 0x03
54 //------------------------------------------ ----------------------mthr_1to2---
55 // mod. index threshold value for pm1=>pm2 change.
56 #define ma_mthr_1to2__a 1
57 #define ma_mthr_1to2__len 8
58 #define ma_mthr_1to2__mask 0xff
59 #define ma_mthr_1to2__shift 0x00
60 #define ma_mthr_1to2__reset 0x3c
61 //-----------------------------------------------------------------mthr_2to1---
62 // mod. index threshold value for pm2=>pm1 change.
63 #define ma_mthr_2to1__a 2
64 #define ma_mthr_2to1__len 8
65 #define ma_mthr_2to1__mask 0xff
66 #define ma_mthr_2to1__shift 0x00
67 #define ma_mthr_2to1__reset 0x32
68 //-----------------------------------------------------------------mthr_2to3---
69 // mod. index threshold value for pm2=>pm3 change.
70 #define ma_mthr_2to3__a 3
71 #define ma_mthr_2to3__len 8
72 #define ma_mthr_2to3__mask 0xff
73 #define ma_mthr_2to3__shift 0x00
74 #define ma_mthr_2to3__reset 0x5a
75 //-----------------------------------------------------------------mthr_3to2---
76 // mod. index threshold value for pm3=>pm2 change.
77 #define ma_mthr_3to2__a 4
78 #define ma_mthr_3to2__len 8
79 #define ma_mthr_3to2__mask 0xff
80 #define ma_mthr_3to2__shift 0x00
81 #define ma_mthr_3to2__reset 0x50
82 //-------------------------------------------------------------pwmclkdiv_nom---
83 // pwm default clock divider value
84 #define ma_pwmclkdiv_nom__a 8
85 #define ma_pwmclkdiv_nom__len 8
86 #define ma_pwmclkdiv_nom__mask 0xff
87 #define ma_pwmclkdiv_nom__shift 0x00
88 #define ma_pwmclkdiv_nom__reset 0x26
89 //--------- ----------------------------------------------------ocp_latch_en---
90 // high to use permanently latching level-2 ocp
91 #define ma_ocp_latch_en__a 10
92 #define ma_ocp_latch_en__len 1
93 #define ma_ocp_latch_en__mask 0x02
94 #define ma_ocp_latch_en__shift 0x01
95 #define ma_ocp_latch_en__reset 0x00
96 //---------------------------------------------------------------lf_clamp_en---
97 // high (default) to enable lf int2+3 clamping on clip
98 #define ma_lf_clamp_en__a 10
99 #define ma_lf_clamp_en__len 1
100 #define ma_lf_clamp_en__mask 0x80
101 #define ma_lf_clamp_en__shift 0x07
102 #define ma_lf_clamp_en__reset 0x00
103 //-------------------------------------------------------pmcfg_btl_b.modtype---
104 //
105 #define ma_pmcfg_btl_b__modtype__a 18
106 #define ma_pmcfg_btl_b__modtype__len 2
107 #define ma_pmcfg_btl_b__modtype__mask 0x18
108 #define ma_pmcfg_btl_b__modtype__shift 0x03
109 #define ma_pmcfg_btl_b__modtype__reset 0x02
110 //-------------------------------------------------------pmcfg_btl_b.freqdiv---
111 #define ma_pmcfg_btl_b__freqdiv__a 18
112 #define ma_pmcfg_btl_b__freqdiv__len 2
113 #define ma_pmcfg_btl_b__freqdiv__mask 0x06
114 #define ma_pmcfg_btl_b__freqdiv__shift 0x01
115 #define ma_pmcfg_btl_b__freqdiv__reset 0x01
116 //----------------------------------------------------pmcfg_btl_b.lf_gain_ol---
117 //
118 #define ma_pmcfg_btl_b__lf_gain_ol__a 18
119 #define ma_pmcfg_btl_b__lf_gain_ol__len 1
120 #define ma_pmcfg_btl_b__lf_gain_ol__mask 0x01
121 #define ma_pmcfg_btl_b__lf_gain_ol__shift 0x00
122 #define ma_pmcfg_btl_b__lf_gain_ol__reset 0x01
123 //-------------------------------------------------------pmcfg_btl_c.freqdiv---
124 //
125 #define ma_pmcfg_btl_c__freqdiv__a 19
126 #define ma_pmcfg_btl_c__freqdiv__len 2
127 #define ma_pmcfg_btl_c__freqdiv__mask 0x06
128 #define ma_pmcfg_btl_c__freqdiv__shift 0x01
129 #define ma_pmcfg_btl_c__freqdiv__reset 0x01
130 //-------------------------------------------------------pmcfg_btl_c.modtype---
131 //
132 #define ma_pmcfg_btl_c__modtype__a 19
133 #define ma_pmcfg_btl_c__modtype__len 2
134 #define ma_pmcfg_btl_c__modtype__mask 0x18
135 #define ma_pmcfg_btl_c__modtype__shift 0x03
136 #define ma_pmcfg_btl_c__modtype__reset 0x01
137 //----------------------------------------------------pmcfg_btl_c.lf_gain_ol---
138 //
139 #define ma_pmcfg_btl_c__lf_gain_ol__a 19
140 #define ma_pmcfg_btl_c__lf_gain_ol__len 1
141 #define ma_pmcfg_btl_c__lf_gain_ol__mask 0x01
142 #define ma_pmcfg_btl_c__lf_gain_ol__shift 0x00
143 #define ma_pmcfg_btl_c__lf_gain_ol__reset 0x00
144 //-------------------------------------------------------pmcfg_btl_d.modtype---
145 //
146 #define ma_pmcfg_btl_d__modtype__a 20
147 #define ma_pmcfg_btl_d__modtype__len 2
148 #define ma_pmcfg_btl_d__modtype__mask 0x18
149 #define ma_pmcfg_btl_d__modtype__shift 0x03
150 #define ma_pmcfg_btl_d__modtype__reset 0x02
151 //-------------------------------------------------------pmcfg_btl_d.freqdiv---
152 //
153 #define ma_pmcfg_btl_d__freqdiv__a 20
154 #define ma_pmcfg_btl_d__freqdiv__len 2
155 #define ma_pmcfg_btl_d__freqdiv__mask 0x06
156 #define ma_pmcfg_btl_d__freqdiv__shift 0x01
157 #define ma_pmcfg_btl_d__freqdiv__reset 0x02
158 //----------------------------------------------------pmcfg_btl_d.lf_gain_ol---
159 //
160 #define ma_pmcfg_btl_d__lf_gain_ol__a 20
161 #define ma_pmcfg_btl_d__lf_gain_ol__len 1
162 #define ma_pmcfg_btl_d__lf_gain_ol__mask 0x01
163 #define ma_pmcfg_btl_d__lf_gain_ol__shift 0x00
164 #define ma_pmcfg_btl_d__lf_gain_ol__reset 0x00
165 //------------ -------------------------------------------pmcfg_se_a.modtype---
166 //
167 #define ma_pmcfg_se_a__modtype__a 21
168 #define ma_pmcfg_se_a__modtype__len 2
169 #define ma_pmcfg_se_a__modtype__mask 0x18
170 #define ma_pmcfg_se_a__modtype__shift 0x03
171 #define ma_pmcfg_se_a__modtype__reset 0x01
172 //--------------------------------------------------------pmcfg_se_a.freqdiv---
173 //
174 #define ma_pmcfg_se_a__freqdiv__a 21
175 #define ma_pmcfg_se_a__freqdiv__len 2
176 #define ma_pmcfg_se_a__freqdiv__mask 0x06
177 #define ma_pmcfg_se_a__freqdiv__shift 0x01
178 #define ma_pmcfg_se_a__freqdiv__reset 0x00
179 //-----------------------------------------------------pmcfg_se_a.lf_gain_ol---
180 //
181 #define ma_pmcfg_se_a__lf_gain_ol__a 21
182 #define ma_pmcfg_se_a__lf_gain_ol__len 1
183 #define ma_pmcfg_se_a__lf_gain_ol__mask 0x01
184 #define ma_pmcfg_se_a__lf_gain_ol__shift 0x00
185 #define ma_pmcfg_se_a__lf_gain_ol__reset 0x01
186 //-----------------------------------------------------pmcfg_se_b.lf_gain_ol---
187 //
188 #define ma_pmcfg_se_b__lf_gain_ol__a 22
189 #define ma_pmcfg_se_b__lf_gain_ol__len 1
190 #define ma_pmcfg_se_b__lf_gain_ol__mask 0x01
191 #define ma_pmcfg_se_b__lf_gain_ol__shift 0x00
192 #define ma_pmcfg_se_b__lf_gain_ol__reset 0x00
193 //--------------------------------------------------------pmcfg_se_b.freqdiv---
194 //
195 #define ma_pmcfg_se_b__freqdiv__a 22
196 #define ma_pmcfg_se_b__freqdiv__len 2
197 #define ma_pmcfg_se_b__freqdiv__mask 0x06
198 #define ma_pmcfg_se_b__freqdiv__shift 0x01
199 #define ma_pmcfg_se_b__freqdiv__reset 0x01
200 //--------------------------------------------------------pmcfg_se_b.modtype---
201 //
202 #define ma_pmcfg_se_b__modtype__a 22
203 #define ma_pmcfg_se_b__modtype__len 2
204 #define ma_pmcfg_se_b__modtype__mask 0x18
205 #define ma_pmcfg_se_b__modtype__shift 0x03
206 #define ma_pmcfg_se_b__modtype__reset 0x01
207 //----------------------------------------------------------balwaitcount_pm1---
208 // pm1 balancing period.
209 #define ma_balwaitcount_pm1__a 23
210 #define ma_balwaitcount_pm1__len 8
211 #define ma_balwaitcount_pm1__mask 0xff
212 #define ma_balwaitcount_pm1__shift 0x00
213 #define ma_balwaitcount_pm1__reset 0x14
214 //----------------------------------------------------------balwaitcount_pm2---
215 // pm2 balancing period.
216 #define ma_balwaitcount_pm2__a 24
217 #define ma_balwaitcount_pm2__len 8
218 #define ma_balwaitcount_pm2__mask 0xff
219 #define ma_balwaitcount_pm2__shift 0x00
220 #define ma_balwaitcount_pm2__reset 0x14
221 //----------------------------------------------------------balwaitcount_pm3---
222 // pm3 balancing period.
223 #define ma_balwaitcount_pm3__a 25
224 #define ma_balwaitcount_pm3__len 8
225 #define ma_balwaitcount_pm3__mask 0xff
226 #define ma_balwaitcount_pm3__shift 0x00
227 #define ma_balwaitcount_pm3__reset 0x1a
228 //-------------------------------------------------------------usespread_pm1---
229 // pm1 pwm spread-spectrum mode on/off.
230 #define ma_usespread_pm1__a 26
231 #define ma_usespread_pm1__len 1
232 #define ma_usespread_pm1__mask 0x40
233 #define ma_usespread_pm1__shift 0x06
234 #define ma_usespread_pm1__reset 0x00
235 //---------------------------------------------------------------dtsteps_pm1---
236 // pm1 dead time setting [10ns steps].
237 #define ma_dtsteps_pm1__a 26
238 #define ma_dtsteps_pm1__len 3
239 #define ma_dtsteps_pm1__mask 0x38
240 #define ma_dtsteps_pm1__shift 0x03
241 #define ma_dtsteps_pm1__reset 0x04
242 //---------------------------------------------------------------baltype_pm1---
243 // pm1 balancing sensor scheme.
244 #define ma_baltype_pm1__a 26
245 #define ma_baltype_pm1__len 3
246 #define ma_baltype_pm1__mask 0x07
247 #define ma_baltype_pm1__shift 0x00
248 #define ma_baltype_pm1__reset 0x00
249 //-------------------------------------------------------------usespread_pm2---
250 // pm2 pwm spread-spectrum mode on/off.
251 #define ma_usespread_pm2__a 27
252 #define ma_usespread_pm2__len 1
253 #define ma_usespread_pm2__mask 0x40
254 #define ma_usespread_pm2__shift 0x06
255 #define ma_usespread_pm2__reset 0x00
256 //---------------------------------------------------------------dtsteps_pm2---
257 // pm2 dead time setting [10ns steps].
258 #define ma_dtsteps_pm2__a 27
259 #define ma_dtsteps_pm2__len 3
260 #define ma_dtsteps_pm2__mask 0x38
261 #define ma_dtsteps_pm2__shift 0x03
262 #define ma_dtsteps_pm2__reset 0x03
263 //---------------------------------------------------------------baltype_pm2---
264 // pm2 balancing sensor scheme.
265 #define ma_baltype_pm2__a 27
266 #define ma_baltype_pm2__len 3
267 #define ma_baltype_pm2__mask 0x07
268 #define ma_baltype_pm2__shift 0x00
269 #define ma_baltype_pm2__reset 0x01
270 //-------------------------------------------------------------usespread_pm3---
271 // pm3 pwm spread-spectrum mode on/off.
272 #define ma_usespread_pm3__a 28
273 #define ma_usespread_pm3__len 1
274 #define ma_usespread_pm3__mask 0x40
275 #define ma_usespread_pm3__shift 0x06
276 #define ma_usespread_pm3__reset 0x00
277 //---------------------------------------------------------------dtsteps_pm3---
278 // pm3 dead time setting [10ns steps].
279 #define ma_dtsteps_pm3__a 28
280 #define ma_dtsteps_pm3__len 3
281 #define ma_dtsteps_pm3__mask 0x38
282 #define ma_dtsteps_pm3__shift 0x03
283 #define ma_dtsteps_pm3__reset 0x01
284 //---------------------------------------------------------------baltype_pm3---
285 // pm3 balancing sensor scheme.
286 #define ma_baltype_pm3__a 28
287 #define ma_baltype_pm3__len 3
288 #define ma_baltype_pm3__mask 0x07
289 #define ma_baltype_pm3__shift 0x00
290 #define ma_baltype_pm3__reset 0x03
291 //-----------------------------------------------------------------pmprofile---
292 // pm profile select. valid presets: 0-1-2-3-4. 5=> custom profile.
293 #define ma_pmprofile__a 29
294 #define ma_pmprofile__len 3
295 #define ma_pmprofile__mask 0x07
296 #define ma_pmprofile__shift 0x00
297 #define ma_pmprofile__reset 0x00
298 //-------------------------------------------------------------------pm3_man---
299 // custom profile pm3 contents. 0=>a,  1=>b,  2=>c,  3=>d
300 #define ma_pm3_man__a 30
301 #define ma_pm3_man__len 2
302 #define ma_pm3_man__mask 0x30
303 #define ma_pm3_man__shift 0x04
304 #define ma_pm3_man__reset 0x02
305 //-------------------------------------------------------------------pm2_man---
306 // custom profile pm2 contents. 0=>a,  1=>b,  2=>c,  3=>d
307 #define ma_pm2_man__a 30
308 #define ma_pm2_man__len 2
309 #define ma_pm2_man__mask 0x0c
310 #define ma_pm2_man__shift 0x02
311 #define ma_pm2_man__reset 0x03
312 //-------------------------------------------------------------------pm1_man---
313 // custom profile pm1 contents. 0=>a,  1=>b,  2=>c,  3=>d
314 #define ma_pm1_man__a 30
315 #define ma_pm1_man__len 2
316 #define ma_pm1_man__mask 0x03
317 #define ma_pm1_man__shift 0x00
318 #define ma_pm1_man__reset 0x03
319 //-----------------------------------------------------------ocp_latch_clear---
320 // low-high clears current ocp latched condition.
321 #define ma_ocp_latch_clear__a 32
322 #define ma_ocp_latch_clear__len 1
323 #define ma_ocp_latch_clear__mask 0x80
324 #define ma_ocp_latch_clear__shift 0x07
325 #define ma_ocp_latch_clear__reset 0x00
326 //-------------------------------------------------------------audio_in_mode---
327 // audio input mode; 0-1-2-3-4-5
328 #define ma_audio_in_mode__a 37
329 #define ma_audio_in_mode__len 3
330 #define ma_audio_in_mode__mask 0xe0
331 #define ma_audio_in_mode__shift 0x05
332 #define ma_audio_in_mode__reset 0x00
333 //-----------------------------------------------------------------eh_dcshdn---
334 // high to enable dc protection
335 #define ma_eh_dcshdn__a 38
336 #define ma_eh_dcshdn__len 1
337 #define ma_eh_dcshdn__mask 0x04
338 #define ma_eh_dcshdn__shift 0x02
339 #define ma_eh_dcshdn__reset 0x01
340 //---------------------------------------------------------audio_in_mode_ext---
341 // if set,  audio_in_mode is controlled from audio_in_mode register. if not set
342 //audio_in_mode is set from fuse bank setting
343 #define ma_audio_in_mode_ext__a 39
344 #define ma_audio_in_mode_ext__len 1
345 #define ma_audio_in_mode_ext__mask 0x20
346 #define ma_audio_in_mode_ext__shift 0x05
347 #define ma_audio_in_mode_ext__reset 0x00
348 //------------------------------------------------------------------eh_clear---
349 // flip to clear error registers
350 #define ma_eh_clear__a 45
351 #define ma_eh_clear__len 1
352 #define ma_eh_clear__mask 0x04
353 #define ma_eh_clear__shift 0x02
354 #define ma_eh_clear__reset 0x00
355 //----------------------------------------------------------thermal_compr_en---
356 // enable otw-contr.  input compression?
357 #define ma_thermal_compr_en__a 45
358 #define ma_thermal_compr_en__len 1
359 #define ma_thermal_compr_en__mask 0x20
360 #define ma_thermal_compr_en__shift 0x05
361 #define ma_thermal_compr_en__reset 0x01
362 //---------------------------------------------------------------system_mute---
363 // 1 = mute system,  0 = normal operation
364 #define ma_system_mute__a 45
365 #define ma_system_mute__len 1
366 #define ma_system_mute__mask 0x40
367 #define ma_system_mute__shift 0x06
368 #define ma_system_mute__reset 0x00
369 //------------------------------------------------------thermal_compr_max_db---
370 // audio limiter max thermal reduction
371 #define ma_thermal_compr_max_db__a 46
372 #define ma_thermal_compr_max_db__len 3
373 #define ma_thermal_compr_max_db__mask 0x07
374 #define ma_thermal_compr_max_db__shift 0x00
375 #define ma_thermal_compr_max_db__reset 0x04
376 //---------------------------------------------------------audio_proc_enable---
377 // enable audio proc,  bypass if not enabled
378 #define ma_audio_proc_enable__a 53
379 #define ma_audio_proc_enable__len 1
380 #define ma_audio_proc_enable__mask 0x08
381 #define ma_audio_proc_enable__shift 0x03
382 #define ma_audio_proc_enable__reset 0x00
383 //--------------------------------------------------------audio_proc_release---
384 // 00:slow,  01:normal,  10:fast
385 #define ma_audio_proc_release__a 53
386 #define ma_audio_proc_release__len 2
387 #define ma_audio_proc_release__mask 0x30
388 #define ma_audio_proc_release__shift 0x04
389 #define ma_audio_proc_release__reset 0x00
390 //---------------------------------------------------------audio_proc_attack---
391 // 00:slow,  01:normal,  10:fast
392 #define ma_audio_proc_attack__a 53
393 #define ma_audio_proc_attack__len 2
394 #define ma_audio_proc_attack__mask 0xc0
395 #define ma_audio_proc_attack__shift 0x06
396 #define ma_audio_proc_attack__reset 0x00
397 //----------------------------------------------------------------i2s_format---
398 // i2s basic data format,  000 = std. i2s,  001 = left justified (default)
399 #define ma_i2s_format__a 53
400 #define ma_i2s_format__len 3
401 #define ma_i2s_format__mask 0x07
402 #define ma_i2s_format__shift 0x00
403 #define ma_i2s_format__reset 0x01
404 //--------------------------------------------------audio_proc_limiterenable---
405 // 1: enable limiter,  0: disable limiter
406 #define ma_audio_proc_limiterenable__a 54
407 #define ma_audio_proc_limiterenable__len 1
408 #define ma_audio_proc_limiterenable__mask 0x40
409 #define ma_audio_proc_limiterenable__shift 0x06
410 #define ma_audio_proc_limiterenable__reset 0x00
411 //-----------------------------------------------------------audio_proc_mute---
412 // 1: mute,  0: unmute
413 #define ma_audio_proc_mute__a 54
414 #define ma_audio_proc_mute__len 1
415 #define ma_audio_proc_mute__mask 0x80
416 #define ma_audio_proc_mute__shift 0x07
417 #define ma_audio_proc_mute__reset 0x00
418 //---------------------------------------------------------------i2s_sck_pol---
419 // i2s sck polarity cfg. 0 = rising edge data change
420 #define ma_i2s_sck_pol__a 54
421 #define ma_i2s_sck_pol__len 1
422 #define ma_i2s_sck_pol__mask 0x01
423 #define ma_i2s_sck_pol__shift 0x00
424 #define ma_i2s_sck_pol__reset 0x01
425 //-------------------------------------------------------------i2s_framesize---
426 // i2s word length. 00 = 32bit,  01 = 24bit
427 #define ma_i2s_framesize__a 54
428 #define ma_i2s_framesize__len 2
429 #define ma_i2s_framesize__mask 0x18
430 #define ma_i2s_framesize__shift 0x03
431 #define ma_i2s_framesize__reset 0x00
432 //----------------------------------------------------------------i2s_ws_pol---
433 // i2s ws polarity. 0 = low first
434 #define ma_i2s_ws_pol__a 54
435 #define ma_i2s_ws_pol__len 1
436 #define ma_i2s_ws_pol__mask 0x02
437 #define ma_i2s_ws_pol__shift 0x01
438 #define ma_i2s_ws_pol__reset 0x00
439 //-----------------------------------------------------------------i2s_order---
440 // i2s word bit order. 0 = msb first
441 #define ma_i2s_order__a 54
442 #define ma_i2s_order__len 1
443 #define ma_i2s_order__mask 0x04
444 #define ma_i2s_order__shift 0x02
445 #define ma_i2s_order__reset 0x00
446 //------------------------------------------------------------i2s_rightfirst---
447 // i2s l/r word order; 0 = left first
448 #define ma_i2s_rightfirst__a 54
449 #define ma_i2s_rightfirst__len 1
450 #define ma_i2s_rightfirst__mask 0x20
451 #define ma_i2s_rightfirst__shift 0x05
452 #define ma_i2s_rightfirst__reset 0x00
453 //-------------------------------------------------------------vol_db_master---
454 // master volume db
455 #define ma_vol_db_master__a 64
456 #define ma_vol_db_master__len 8
457 #define ma_vol_db_master__mask 0xff
458 #define ma_vol_db_master__shift 0x00
459 #define ma_vol_db_master__reset 0x18
460 //------------------------------------------------------------vol_lsb_master---
461 // master volume lsb 1/4 steps
462 #define ma_vol_lsb_master__a 65
463 #define ma_vol_lsb_master__len 2
464 #define ma_vol_lsb_master__mask 0x03
465 #define ma_vol_lsb_master__shift 0x00
466 #define ma_vol_lsb_master__reset 0x00
467 //----------------------------------------------------------------vol_db_ch0---
468 // volume channel 0
469 #define ma_vol_db_ch0__a 66
470 #define ma_vol_db_ch0__len 8
471 #define ma_vol_db_ch0__mask 0xff
472 #define ma_vol_db_ch0__shift 0x00
473 #define ma_vol_db_ch0__reset 0x18
474 //----------------------------------------------------------------vol_db_ch1---
475 // volume channel 1
476 #define ma_vol_db_ch1__a 67
477 #define ma_vol_db_ch1__len 8
478 #define ma_vol_db_ch1__mask 0xff
479 #define ma_vol_db_ch1__shift 0x00
480 #define ma_vol_db_ch1__reset 0x18
481 //----------------------------------------------------------------vol_db_ch2---
482 // volume channel 2
483 #define ma_vol_db_ch2__a 68
484 #define ma_vol_db_ch2__len 8
485 #define ma_vol_db_ch2__mask 0xff
486 #define ma_vol_db_ch2__shift 0x00
487 #define ma_vol_db_ch2__reset 0x18
488 //----------------------------------------------------------------vol_db_ch3---
489 // volume channel 3
490 #define ma_vol_db_ch3__a 69
491 #define ma_vol_db_ch3__len 8
492 #define ma_vol_db_ch3__mask 0xff
493 #define ma_vol_db_ch3__shift 0x00
494 #define ma_vol_db_ch3__reset 0x18
495 //---------------------------------------------------------------vol_lsb_ch0---
496 // volume channel 1 - 1/4 steps
497 #define ma_vol_lsb_ch0__a 70
498 #define ma_vol_lsb_ch0__len 2
499 #define ma_vol_lsb_ch0__mask 0x03
500 #define ma_vol_lsb_ch0__shift 0x00
501 #define ma_vol_lsb_ch0__reset 0x00
502 //---------------------------------------------------------------vol_lsb_ch1---
503 // volume channel 3 - 1/4 steps
504 #define ma_vol_lsb_ch1__a 70
505 #define ma_vol_lsb_ch1__len 2
506 #define ma_vol_lsb_ch1__mask 0x0c
507 #define ma_vol_lsb_ch1__shift 0x02
508 #define ma_vol_lsb_ch1__reset 0x00
509 //---------------------------------------------------------------vol_lsb_ch2---
510 // volume channel 2 - 1/4 steps
511 #define ma_vol_lsb_ch2__a 70
512 #define ma_vol_lsb_ch2__len 2
513 #define ma_vol_lsb_ch2__mask 0x30
514 #define ma_vol_lsb_ch2__shift 0x04
515 #define ma_vol_lsb_ch2__reset 0x00
516 //---------------------------------------------------------------vol_lsb_ch3---
517 // volume channel 3 - 1/4 steps
518 #define ma_vol_lsb_ch3__a 70
519 #define ma_vol_lsb_ch3__len 2
520 #define ma_vol_lsb_ch3__mask 0xc0
521 #define ma_vol_lsb_ch3__shift 0x06
522 #define ma_vol_lsb_ch3__reset 0x00
523 //----------------------------------------------------------------thr_db_ch0---
524 // thr_db channel 0
525 #define ma_thr_db_ch0__a 71
526 #define ma_thr_db_ch0__len 8
527 #define ma_thr_db_ch0__mask 0xff
528 #define ma_thr_db_ch0__shift 0x00
529 #define ma_thr_db_ch0__reset 0x18
530 //----------------------------------------------------------------thr_db_ch1---
531 // thr db ch1
532 #define ma_thr_db_ch1__a 72
533 #define ma_thr_db_ch1__len 8
534 #define ma_thr_db_ch1__mask 0xff
535 #define ma_thr_db_ch1__shift 0x00
536 #define ma_thr_db_ch1__reset 0x18
537 //----------------------------------------------------------------thr_db_ch2---
538 // thr db ch2
539 #define ma_thr_db_ch2__a 73
540 #define ma_thr_db_ch2__len 8
541 #define ma_thr_db_ch2__mask 0xff
542 #define ma_thr_db_ch2__shift 0x00
543 #define ma_thr_db_ch2__reset 0x18
544 //----------------------------------------------------------------thr_db_ch3---
545 // threshold db ch3
546 #define ma_thr_db_ch3__a 74
547 #define ma_thr_db_ch3__len 8
548 #define ma_thr_db_ch3__mask 0xff
549 #define ma_thr_db_ch3__shift 0x00
550 #define ma_thr_db_ch3__reset 0x18
551 //---------------------------------------------------------------thr_lsb_ch0---
552 // thr lsb ch0
553 #define ma_thr_lsb_ch0__a 75
554 #define ma_thr_lsb_ch0__len 2
555 #define ma_thr_lsb_ch0__mask 0x03
556 #define ma_thr_lsb_ch0__shift 0x00
557 #define ma_thr_lsb_ch0__reset 0x00
558 //---------------------------------------------------------------thr_lsb_ch1---
559 // thr lsb ch1
560 #define ma_thr_lsb_ch1__a 75
561 #define ma_thr_lsb_ch1__len 2
562 #define ma_thr_lsb_ch1__mask 0x0c
563 #define ma_thr_lsb_ch1__shift 0x02
564 #define ma_thr_lsb_ch1__reset 0x00
565 //---------------------------------------------------------------thr_lsb_ch2---
566 // thr lsb ch2 1/4 db step
567 #define ma_thr_lsb_ch2__a 75
568 #define ma_thr_lsb_ch2__len 2
569 #define ma_thr_lsb_ch2__mask 0x30
570 #define ma_thr_lsb_ch2__shift 0x04
571 #define ma_thr_lsb_ch2__reset 0x00
572 //---------------------------------------------------------------thr_lsb_ch3---
573 // threshold lsb ch3
574 #define ma_thr_lsb_ch3__a 75
575 #define ma_thr_lsb_ch3__len 2
576 #define ma_thr_lsb_ch3__mask 0xc0
577 #define ma_thr_lsb_ch3__shift 0x06
578 #define ma_thr_lsb_ch3__reset 0x00
579 //-----------------------------------------------------------dcu_mon0.pm_mon---
580 // power mode monitor channel 0
581 #define ma_dcu_mon0__pm_mon__a 96
582 #define ma_dcu_mon0__pm_mon__len 2
583 #define ma_dcu_mon0__pm_mon__mask 0x03
584 #define ma_dcu_mon0__pm_mon__shift 0x00
585 #define ma_dcu_mon0__pm_mon__reset 0x00
586 //-----------------------------------------------------dcu_mon0.freqmode_mon---
587 // frequence mode monitor channel 0
588 #define ma_dcu_mon0__freqmode_mon__a 96
589 #define ma_dcu_mon0__freqmode_mon__len 3
590 #define ma_dcu_mon0__freqmode_mon__mask 0x70
591 #define ma_dcu_mon0__freqmode_mon__shift 0x04
592 #define ma_dcu_mon0__freqmode_mon__reset 0x00
593 //-------------------------------------------------------dcu_mon0.pps_passed---
594 // dcu0 pps completion indicator
595 #define ma_dcu_mon0__pps_passed__a 96
596 #define ma_dcu_mon0__pps_passed__len 1
597 #define ma_dcu_mon0__pps_passed__mask 0x80
598 #define ma_dcu_mon0__pps_passed__shift 0x07
599 #define ma_dcu_mon0__pps_passed__reset 0x00
600 //----------------------------------------------------------dcu_mon0.ocp_mon---
601 // ocp monitor channel 0
602 #define ma_dcu_mon0__ocp_mon__a 97
603 #define ma_dcu_mon0__ocp_mon__len 1
604 #define ma_dcu_mon0__ocp_mon__mask 0x01
605 #define ma_dcu_mon0__ocp_mon__shift 0x00
606 #define ma_dcu_mon0__ocp_mon__reset 0x00
607 //--------------------------------------------------------dcu_mon0.vcfly1_ok---
608 // cfly1 protection monitor channel 0.
609 #define ma_dcu_mon0__vcfly1_ok__a 97
610 #define ma_dcu_mon0__vcfly1_ok__len 1
611 #define ma_dcu_mon0__vcfly1_ok__mask 0x02
612 #define ma_dcu_mon0__vcfly1_ok__shift 0x01
613 #define ma_dcu_mon0__vcfly1_ok__reset 0x00
614 //--------------------------------------------------------dcu_mon0.vcfly2_ok---
615 // cfly2 protection monitor channel 0.
616 #define ma_dcu_mon0__vcfly2_ok__a 97
617 #define ma_dcu_mon0__vcfly2_ok__len 1
618 #define ma_dcu_mon0__vcfly2_ok__mask 0x04
619 #define ma_dcu_mon0__vcfly2_ok__shift 0x02
620 #define ma_dcu_mon0__vcfly2_ok__reset 0x00
621 //----------------------------------------------------------dcu_mon0.pvdd_ok---
622 // dcu0 pvdd monitor
623 #define ma_dcu_mon0__pvdd_ok__a 97
624 #define ma_dcu_mon0__pvdd_ok__len 1
625 #define ma_dcu_mon0__pvdd_ok__mask 0x08
626 #define ma_dcu_mon0__pvdd_ok__shift 0x03
627 #define ma_dcu_mon0__pvdd_ok__reset 0x00
628 //-----------------------------------------------------------dcu_mon0.vdd_ok---
629 // dcu0 vdd monitor
630 #define ma_dcu_mon0__vdd_ok__a 97
631 #define ma_dcu_mon0__vdd_ok__len 1
632 #define ma_dcu_mon0__vdd_ok__mask 0x10
633 #define ma_dcu_mon0__vdd_ok__shift 0x04
634 #define ma_dcu_mon0__vdd_ok__reset 0x00
635 //-------------------------------------------------------------dcu_mon0.mute---
636 // dcu0 mute monitor
637 #define ma_dcu_mon0__mute__a 97
638 #define ma_dcu_mon0__mute__len 1
639 #define ma_dcu_mon0__mute__mask 0x20
640 #define ma_dcu_mon0__mute__shift 0x05
641 #define ma_dcu_mon0__mute__reset 0x00
642 //------------------------------------------------------------dcu_mon0.m_mon---
643 // m sense monitor channel 0
644 #define ma_dcu_mon0__m_mon__a 98
645 #define ma_dcu_mon0__m_mon__len 8
646 #define ma_dcu_mon0__m_mon__mask 0xff
647 #define ma_dcu_mon0__m_mon__shift 0x00
648 #define ma_dcu_mon0__m_mon__reset 0x00
649 //-----------------------------------------------------------dcu_mon1.pm_mon---
650 // power mode monitor channel 1
651 #define ma_dcu_mon1__pm_mon__a 100
652 #define ma_dcu_mon1__pm_mon__len 2
653 #define ma_dcu_mon1__pm_mon__mask 0x03
654 #define ma_dcu_mon1__pm_mon__shift 0x00
655 #define ma_dcu_mon1__pm_mon__reset 0x00
656 //-----------------------------------------------------dcu_mon1.freqmode_mon---
657 // frequence mode monitor channel 1
658 #define ma_dcu_mon1__freqmode_mon__a 100
659 #define ma_dcu_mon1__freqmode_mon__len 3
660 #define ma_dcu_mon1__freqmode_mon__mask 0x70
661 #define ma_dcu_mon1__freqmode_mon__shift 0x04
662 #define ma_dcu_mon1__freqmode_mon__reset 0x00
663 //-------------------------------------------------------dcu_mon1.pps_passed---
664 // dcu1 pps completion indicator
665 #define ma_dcu_mon1__pps_passed__a 100
666 #define ma_dcu_mon1__pps_passed__len 1
667 #define ma_dcu_mon1__pps_passed__mask 0x80
668 #define ma_dcu_mon1__pps_passed__shift 0x07
669 #define ma_dcu_mon1__pps_passed__reset 0x00
670 //----------------------------------------------------------dcu_mon1.ocp_mon---
671 // ocp monitor channel 1
672 #define ma_dcu_mon1__ocp_mon__a 101
673 #define ma_dcu_mon1__ocp_mon__len 1
674 #define ma_dcu_mon1__ocp_mon__mask 0x01
675 #define ma_dcu_mon1__ocp_mon__shift 0x00
676 #define ma_dcu_mon1__ocp_mon__reset 0x00
677 //--------------------------------------------------------dcu_mon1.vcfly1_ok---
678 // cfly1 protcetion monitor channel 1
679 #define ma_dcu_mon1__vcfly1_ok__a 101
680 #define ma_dcu_mon1__vcfly1_ok__len 1
681 #define ma_dcu_mon1__vcfly1_ok__mask 0x02
682 #define ma_dcu_mon1__vcfly1_ok__shift 0x01
683 #define ma_dcu_mon1__vcfly1_ok__reset 0x00
684 //--------------------------------------------------------dcu_mon1.vcfly2_ok---
685 // cfly2 protection monitor channel 1
686 #define ma_dcu_mon1__vcfly2_ok__a 101
687 #define ma_dcu_mon1__vcfly2_ok__len 1
688 #define ma_dcu_mon1__vcfly2_ok__mask 0x04
689 #define ma_dcu_mon1__vcfly2_ok__shift 0x02
690 #define ma_dcu_mon1__vcfly2_ok__reset 0x00
691 //----------------------------------------------------------dcu_mon1.pvdd_ok---
692 // dcu1 pvdd monitor
693 #define ma_dcu_mon1__pvdd_ok__a 101
694 #define ma_dcu_mon1__pvdd_ok__len 1
695 #define ma_dcu_mon1__pvdd_ok__mask 0x08
696 #define ma_dcu_mon1__pvdd_ok__shift 0x03
697 #define ma_dcu_mon1__pvdd_ok__reset 0x00
698 //-----------------------------------------------------------dcu_mon1.vdd_ok---
699 // dcu1 vdd monitor
700 #define ma_dcu_mon1__vdd_ok__a 101
701 #define ma_dcu_mon1__vdd_ok__len 1
702 #define ma_dcu_mon1__vdd_ok__mask 0x10
703 #define ma_dcu_mon1__vdd_ok__shift 0x04
704 #define ma_dcu_mon1__vdd_ok__reset 0x00
705 //-------------------------------------------------------------dcu_mon1.mute---
706 // dcu1 mute monitor
707 #define ma_dcu_mon1__mute__a 101
708 #define ma_dcu_mon1__mute__len 1
709 #define ma_dcu_mon1__mute__mask 0x20
710 #define ma_dcu_mon1__mute__shift 0x05
711 #define ma_dcu_mon1__mute__reset 0x00
712 //------------------------------------------------------------dcu_mon1.m_mon---
713 // m sense monitor channel 1
714 #define ma_dcu_mon1__m_mon__a 102
715 #define ma_dcu_mon1__m_mon__len 8
716 #define ma_dcu_mon1__m_mon__mask 0xff
717 #define ma_dcu_mon1__m_mon__shift 0x00
718 #define ma_dcu_mon1__m_mon__reset 0x00
719 //--------------------------------------------------------dcu_mon0.sw_enable---
720 // dcu0 switch enable monitor
721 #define ma_dcu_mon0__sw_enable__a 104
722 #define ma_dcu_mon0__sw_enable__len 1
723 #define ma_dcu_mon0__sw_enable__mask 0x40
724 #define ma_dcu_mon0__sw_enable__shift 0x06
725 #define ma_dcu_mon0__sw_enable__reset 0x00
726 //--------------------------------------------------------dcu_mon1.sw_enable---
727 // dcu1 switch enable monitor
728 #define ma_dcu_mon1__sw_enable__a 104
729 #define ma_dcu_mon1__sw_enable__len 1
730 #define ma_dcu_mon1__sw_enable__mask 0x80
731 #define ma_dcu_mon1__sw_enable__shift 0x07
732 #define ma_dcu_mon1__sw_enable__reset 0x00
733 //------------------------------------------------------------hvboot0_ok_mon---
734 // hvboot0_ok for test/debug
735 #define ma_hvboot0_ok_mon__a 105
736 #define ma_hvboot0_ok_mon__len 1
737 #define ma_hvboot0_ok_mon__mask 0x40
738 #define ma_hvboot0_ok_mon__shift 0x06
739 #define ma_hvboot0_ok_mon__reset 0x00
740 //------------------------------------------------------------hvboot1_ok_mon---
741 // hvboot1_ok for test/debug
742 #define ma_hvboot1_ok_mon__a 105
743 #define ma_hvboot1_ok_mon__len 1
744 #define ma_hvboot1_ok_mon__mask 0x80
745 #define ma_hvboot1_ok_mon__shift 0x07
746 #define ma_hvboot1_ok_mon__reset 0x00
747 //-----------------------------------------------------------------error_acc---
748 // accumulated errors,  at and after triggering
749 #define ma_error_acc__a 109
750 #define ma_error_acc__len 8
751 #define ma_error_acc__mask 0xff
752 #define ma_error_acc__shift 0x00
753 #define ma_error_acc__reset 0x00
754 //-------------------------------------------------------------i2s_data_rate---
755 // detected i2s data rate: 00/01/10 = x1/x2/x4
756 #define ma_i2s_data_rate__a 116
757 #define ma_i2s_data_rate__len 2
758 #define ma_i2s_data_rate__mask 0x03
759 #define ma_i2s_data_rate__shift 0x00
760 #define ma_i2s_data_rate__reset 0x00
761 //---------------------------------------------------------audio_in_mode_mon---
762 // audio input mode monitor
763 #define ma_audio_in_mode_mon__a 116
764 #define ma_audio_in_mode_mon__len 3
765 #define ma_audio_in_mode_mon__mask 0x1c
766 #define ma_audio_in_mode_mon__shift 0x02
767 #define ma_audio_in_mode_mon__reset 0x00
768 //------------------------------------------------------------------msel_mon---
769 // msel[2:0] monitor register
770 #define ma_msel_mon__a 117
771 #define ma_msel_mon__len 3
772 #define ma_msel_mon__mask 0x07
773 #define ma_msel_mon__shift 0x00
774 #define ma_msel_mon__reset 0x00
775 //---------------------------------------------------------------------error---
776 // current error flag monitor reg - for app. ctrl.
777 #define ma_error__a 124
778 #define ma_error__len 8
779 #define ma_error__mask 0xff
780 #define ma_error__shift 0x00
781 #define ma_error__reset 0x00
782 //----------------------------------------------------audio_proc_limiter_mon---
783 // b7-b4: channel 3-0 limiter active
784 #define ma_audio_proc_limiter_mon__a 126
785 #define ma_audio_proc_limiter_mon__len 4
786 #define ma_audio_proc_limiter_mon__mask 0xf0
787 #define ma_audio_proc_limiter_mon__shift 0x04
788 #define ma_audio_proc_limiter_mon__reset 0x00
789 //-------------------------------------------------------audio_proc_clip_mon---
790 // b3-b0: channel 3-0 clipping monitor
791 #define ma_audio_proc_clip_mon__a 126
792 #define ma_audio_proc_clip_mon__len 4
793 #define ma_audio_proc_clip_mon__mask 0x0f
794 #define ma_audio_proc_clip_mon__shift 0x00
795 #define ma_audio_proc_clip_mon__reset 0x00
796 #endif
797
798 #define SOC_ENUM_ERR(xname, xenum)\
799 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
800         .access = SNDRV_CTL_ELEM_ACCESS_READ,\
801         .info = snd_soc_info_enum_double,\
802         .get = snd_soc_get_enum_double, .put = snd_soc_put_enum_double,\
803         .private_value = (unsigned long)&(xenum) }
804
805 static struct i2c_client *i2c;
806
807 struct ma120x0p_priv {
808         struct regmap *regmap;
809         int mclk_div;
810         struct snd_soc_component *component;
811         struct gpio_desc *enable_gpio;
812         struct gpio_desc *mute_gpio;
813         struct gpio_desc *booster_gpio;
814         struct gpio_desc *error_gpio;
815 };
816
817 static struct ma120x0p_priv *priv_data;
818
819 //Used to share the IRQ number within this file
820 static unsigned int irqNumber;
821
822 // Function prototype for the custom IRQ handler function
823 static irqreturn_t ma120x0p_irq_handler(int irq, void *data);
824
825 //Alsa Controls
826 static const char * const limenable_text[] = {"Bypassed", "Enabled"};
827 static const char * const limatack_text[] = {"Slow", "Normal", "Fast"};
828 static const char * const limrelease_text[] = {"Slow", "Normal", "Fast"};
829
830 static const char * const err_flycap_text[] = {"Ok", "Error"};
831 static const char * const err_overcurr_text[] = {"Ok", "Error"};
832 static const char * const err_pllerr_text[] = {"Ok", "Error"};
833 static const char * const err_pvddunder_text[] = {"Ok", "Error"};
834 static const char * const err_overtempw_text[] = {"Ok", "Error"};
835 static const char * const err_overtempe_text[] = {"Ok", "Error"};
836 static const char * const err_pinlowimp_text[] = {"Ok", "Error"};
837 static const char * const err_dcprot_text[] = {"Ok", "Error"};
838
839 static const char * const pwr_mode_prof_text[] = {"PMF0", "PMF1", "PMF2",
840 "PMF3", "PMF4"};
841
842 static const struct soc_enum lim_enable_ctrl =
843         SOC_ENUM_SINGLE(ma_audio_proc_limiterenable__a,
844                 ma_audio_proc_limiterenable__shift,
845                 ma_audio_proc_limiterenable__len + 1,
846                 limenable_text);
847 static const struct soc_enum limatack_ctrl =
848         SOC_ENUM_SINGLE(ma_audio_proc_attack__a,
849                 ma_audio_proc_attack__shift,
850                 ma_audio_proc_attack__len + 1,
851                 limatack_text);
852 static const struct soc_enum limrelease_ctrl =
853         SOC_ENUM_SINGLE(ma_audio_proc_release__a,
854                 ma_audio_proc_release__shift,
855                 ma_audio_proc_release__len + 1,
856                 limrelease_text);
857 static const struct soc_enum err_flycap_ctrl =
858         SOC_ENUM_SINGLE(ma_error__a, 0, 3, err_flycap_text);
859 static const struct soc_enum err_overcurr_ctrl =
860         SOC_ENUM_SINGLE(ma_error__a, 1, 3, err_overcurr_text);
861 static const struct soc_enum err_pllerr_ctrl =
862         SOC_ENUM_SINGLE(ma_error__a, 2, 3, err_pllerr_text);
863 static const struct soc_enum err_pvddunder_ctrl =
864         SOC_ENUM_SINGLE(ma_error__a, 3, 3, err_pvddunder_text);
865 static const struct soc_enum err_overtempw_ctrl =
866         SOC_ENUM_SINGLE(ma_error__a, 4, 3, err_overtempw_text);
867 static const struct soc_enum err_overtempe_ctrl =
868         SOC_ENUM_SINGLE(ma_error__a, 5, 3, err_overtempe_text);
869 static const struct soc_enum err_pinlowimp_ctrl =
870         SOC_ENUM_SINGLE(ma_error__a, 6, 3, err_pinlowimp_text);
871 static const struct soc_enum err_dcprot_ctrl =
872         SOC_ENUM_SINGLE(ma_error__a, 7, 3, err_dcprot_text);
873 static const struct soc_enum pwr_mode_prof_ctrl =
874         SOC_ENUM_SINGLE(ma_pmprofile__a, ma_pmprofile__shift, 5,
875                 pwr_mode_prof_text);
876
877 static const char * const pwr_mode_texts[] = {
878                 "Dynamic power mode",
879                 "Power mode 1",
880                 "Power mode 2",
881                 "Power mode 3",
882         };
883
884 static const int pwr_mode_values[] = {
885                 0x10,
886                 0x50,
887                 0x60,
888                 0x70,
889         };
890
891 static SOC_VALUE_ENUM_SINGLE_DECL(pwr_mode_ctrl,
892         ma_pm_man__a, 0, 0x70,
893         pwr_mode_texts,
894         pwr_mode_values);
895
896 static const DECLARE_TLV_DB_SCALE(ma120x0p_vol_tlv, -14400, 100,  0);
897 static const DECLARE_TLV_DB_SCALE(ma120x0p_lim_tlv, -5000, 100,  0);
898 static const DECLARE_TLV_DB_SCALE(ma120x0p_lr_tlv, -5000, 100,  0);
899
900 static const struct snd_kcontrol_new ma120x0p_snd_controls[] = {
901         //Master Volume
902         SOC_SINGLE_RANGE_TLV("A.Mstr Vol Volume",
903                 ma_vol_db_master__a, 0, 0x18, 0xa8, 1, ma120x0p_vol_tlv),
904
905         //L-R Volume ch0
906         SOC_SINGLE_RANGE_TLV("B.L Vol Volume",
907                 ma_vol_db_ch0__a, 0, 0x18, 0x4a, 1, ma120x0p_lr_tlv),
908         SOC_SINGLE_RANGE_TLV("C.R Vol Volume",
909                 ma_vol_db_ch1__a, 0, 0x18, 0x4a, 1, ma120x0p_lr_tlv),
910
911         //L-R Limiter Threshold ch0-ch1
912         SOC_DOUBLE_R_RANGE_TLV("D.Lim thresh Volume",
913                 ma_thr_db_ch0__a, ma_thr_db_ch1__a, 0, 0x0e, 0x4a, 1,
914                 ma120x0p_lim_tlv),
915
916         //Enum Switches/Selectors
917         //SOC_ENUM("E.AudioProc Mute", audioproc_mute_ctrl),
918         SOC_ENUM("F.Limiter Enable", lim_enable_ctrl),
919         SOC_ENUM("G.Limiter Attck", limatack_ctrl),
920         SOC_ENUM("H.Limiter Rls", limrelease_ctrl),
921
922         //Enum Error Monitor (read-only)
923         SOC_ENUM_ERR("I.Err flycap", err_flycap_ctrl),
924         SOC_ENUM_ERR("J.Err overcurr", err_overcurr_ctrl),
925         SOC_ENUM_ERR("K.Err pllerr", err_pllerr_ctrl),
926         SOC_ENUM_ERR("L.Err pvddunder", err_pvddunder_ctrl),
927         SOC_ENUM_ERR("M.Err overtempw", err_overtempw_ctrl),
928         SOC_ENUM_ERR("N.Err overtempe", err_overtempe_ctrl),
929         SOC_ENUM_ERR("O.Err pinlowimp", err_pinlowimp_ctrl),
930         SOC_ENUM_ERR("P.Err dcprot", err_dcprot_ctrl),
931
932         //Power modes profiles
933         SOC_ENUM("Q.PM Prof", pwr_mode_prof_ctrl),
934
935         // Power mode selection (Dynamic,1,2,3)
936         SOC_ENUM("R.Power Mode", pwr_mode_ctrl),
937 };
938
939 //Machine Driver
940 static int ma120x0p_hw_params(struct snd_pcm_substream *substream,
941         struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
942 {
943         u16 blen = 0x00;
944
945         struct snd_soc_component *component = dai->component;
946
947         priv_data->component = component;
948
949         switch (params_format(params)) {
950         case SNDRV_PCM_FORMAT_S16_LE:
951                 blen = 0x10;
952                 break;
953         case SNDRV_PCM_FORMAT_S24_LE:
954                 blen = 0x00;
955                 break;
956         case SNDRV_PCM_FORMAT_S32_LE:
957                 blen = 0x00;
958                 break;
959         default:
960                 dev_err(dai->dev, "Unsupported word length: %u\n",
961                 params_format(params));
962                 return -EINVAL;
963         }
964
965         // set word length
966         snd_soc_component_update_bits(component, ma_i2s_framesize__a,
967                 ma_i2s_framesize__mask, blen);
968
969         return 0;
970 }
971
972 static int ma120x0p_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
973 {
974         int val = 0;
975
976         struct ma120x0p_priv *ma120x0p;
977
978         struct snd_soc_component *component = dai->component;
979
980         ma120x0p = snd_soc_component_get_drvdata(component);
981
982         if (mute)
983                 val = 0;
984         else
985                 val = 1;
986
987         gpiod_set_value_cansleep(priv_data->mute_gpio, val);
988
989         return 0;
990 }
991
992 static const struct snd_soc_dai_ops ma120x0p_dai_ops = {
993         .hw_params              =       ma120x0p_hw_params,
994         .mute_stream    =       ma120x0p_mute_stream,
995 };
996
997 static struct snd_soc_dai_driver ma120x0p_dai = {
998         .name           = "ma120x0p-amp",
999         .playback       =       {
1000                 .stream_name    = "Playback",
1001                 .channels_min   = 2,
1002                 .channels_max   = 2,
1003                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1004                 .rate_min = 44100,
1005                 .rate_max = 192000,
1006                 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE
1007         },
1008         .ops        = &ma120x0p_dai_ops,
1009 };
1010
1011 //Codec Driver
1012 static int ma120x0p_clear_err(struct snd_soc_component *component)
1013 {
1014         int ret = 0;
1015
1016         struct ma120x0p_priv *ma120x0p;
1017
1018         ma120x0p = snd_soc_component_get_drvdata(component);
1019
1020         ret = snd_soc_component_update_bits(component,
1021                 ma_eh_clear__a, ma_eh_clear__mask, 0x00);
1022         if (ret < 0)
1023                 return ret;
1024
1025         ret = snd_soc_component_update_bits(component,
1026                 ma_eh_clear__a, ma_eh_clear__mask, 0x04);
1027         if (ret < 0)
1028                 return ret;
1029
1030         ret = snd_soc_component_update_bits(component,
1031                 ma_eh_clear__a, ma_eh_clear__mask, 0x00);
1032         if (ret < 0)
1033                 return ret;
1034
1035         return 0;
1036 }
1037
1038 static void ma120x0p_remove(struct snd_soc_component *component)
1039 {
1040         struct ma120x0p_priv *ma120x0p;
1041
1042         ma120x0p = snd_soc_component_get_drvdata(component);
1043 }
1044
1045 static int ma120x0p_probe(struct snd_soc_component *component)
1046 {
1047         struct ma120x0p_priv *ma120x0p;
1048
1049         int ret = 0;
1050
1051         i2c = container_of(component->dev, struct i2c_client, dev);
1052
1053         ma120x0p = snd_soc_component_get_drvdata(component);
1054
1055         //Reset error
1056         ma120x0p_clear_err(component);
1057         if (ret < 0)
1058                 return ret;
1059
1060         // set serial audio format I2S and enable audio processor
1061         ret = snd_soc_component_write(component, ma_i2s_format__a, 0x08);
1062         if (ret < 0)
1063                 return ret;
1064
1065         // Enable audio limiter
1066         ret = snd_soc_component_update_bits(component,
1067                 ma_audio_proc_limiterenable__a,
1068                 ma_audio_proc_limiterenable__mask, 0x40);
1069         if (ret < 0)
1070                 return ret;
1071
1072         // Set lim attack to fast
1073         ret = snd_soc_component_update_bits(component,
1074                 ma_audio_proc_attack__a, ma_audio_proc_attack__mask, 0x80);
1075         if (ret < 0)
1076                 return ret;
1077
1078         // Set lim attack to low
1079         ret = snd_soc_component_update_bits(component,
1080                 ma_audio_proc_release__a, ma_audio_proc_release__mask, 0x00);
1081         if (ret < 0)
1082                 return ret;
1083
1084         // set volume to 0dB
1085         ret = snd_soc_component_write(component, ma_vol_db_master__a, 0x18);
1086         if (ret < 0)
1087                 return ret;
1088
1089         // set ch0 lim thresh to -15dB
1090         ret = snd_soc_component_write(component, ma_thr_db_ch0__a, 0x27);
1091         if (ret < 0)
1092                 return ret;
1093
1094         // set ch1 lim thresh to -15dB
1095         ret = snd_soc_component_write(component, ma_thr_db_ch1__a, 0x27);
1096         if (ret < 0)
1097                 return ret;
1098
1099         //Check for errors
1100         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x00, 0);
1101         if (ret < 0)
1102                 return ret;
1103         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x01, 0);
1104         if (ret < 0)
1105                 return ret;
1106         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x02, 0);
1107         if (ret < 0)
1108                 return ret;
1109         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x08, 0);
1110         if (ret < 0)
1111                 return ret;
1112         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x10, 0);
1113         if (ret < 0)
1114                 return ret;
1115         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x20, 0);
1116         if (ret < 0)
1117                 return ret;
1118         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x40, 0);
1119         if (ret < 0)
1120                 return ret;
1121         ret = snd_soc_component_test_bits(component, ma_error_acc__a, 0x80, 0);
1122         if (ret < 0)
1123                 return ret;
1124
1125         return 0;
1126 }
1127
1128 static int ma120x0p_set_bias_level(struct snd_soc_component *component,
1129         enum snd_soc_bias_level level)
1130 {
1131         int ret = 0;
1132
1133         struct ma120x0p_priv *ma120x0p;
1134
1135         ma120x0p = snd_soc_component_get_drvdata(component);
1136
1137         switch (level) {
1138         case SND_SOC_BIAS_ON:
1139                 break;
1140
1141         case SND_SOC_BIAS_PREPARE:
1142                 break;
1143
1144         case SND_SOC_BIAS_STANDBY:
1145                 ret = gpiod_get_value_cansleep(priv_data->enable_gpio);
1146                 if (ret != 0) {
1147                         dev_err(component->dev, "Device ma120x0p disabled in STANDBY BIAS: %d\n",
1148                         ret);
1149                         return ret;
1150                 }
1151                 break;
1152
1153         case SND_SOC_BIAS_OFF:
1154                 break;
1155         }
1156
1157         return 0;
1158 }
1159
1160 static const struct snd_soc_dapm_widget ma120x0p_dapm_widgets[] = {
1161         SND_SOC_DAPM_OUTPUT("OUT_A"),
1162         SND_SOC_DAPM_OUTPUT("OUT_B"),
1163 };
1164
1165 static const struct snd_soc_dapm_route ma120x0p_dapm_routes[] = {
1166         { "OUT_B",  NULL, "Playback" },
1167         { "OUT_A",  NULL, "Playback" },
1168 };
1169
1170 static const struct snd_soc_component_driver ma120x0p_component_driver = {
1171         .probe = ma120x0p_probe,
1172         .remove = ma120x0p_remove,
1173         .set_bias_level = ma120x0p_set_bias_level,
1174         .dapm_widgets           = ma120x0p_dapm_widgets,
1175         .num_dapm_widgets       = ARRAY_SIZE(ma120x0p_dapm_widgets),
1176         .dapm_routes            = ma120x0p_dapm_routes,
1177         .num_dapm_routes        = ARRAY_SIZE(ma120x0p_dapm_routes),
1178         .controls = ma120x0p_snd_controls,
1179         .num_controls = ARRAY_SIZE(ma120x0p_snd_controls),
1180         .use_pmdown_time        = 1,
1181         .endianness             = 1,
1182         .non_legacy_dai_naming  = 1,
1183 };
1184
1185 //I2C Driver
1186 static const struct reg_default ma120x0p_reg_defaults[] = {
1187         {       0x01,   0x3c    },
1188 };
1189
1190 static bool ma120x0p_reg_volatile(struct device *dev, unsigned int reg)
1191 {
1192         switch (reg) {
1193         case ma_error__a:
1194                         return true;
1195         default:
1196                         return false;
1197         }
1198 }
1199
1200 static const struct of_device_id ma120x0p_of_match[] = {
1201         { .compatible = "ma,ma120x0p", },
1202         { }
1203 };
1204
1205 MODULE_DEVICE_TABLE(of, ma120x0p_of_match);
1206
1207 static struct regmap_config ma120x0p_regmap_config = {
1208         .reg_bits = 8,
1209         .val_bits = 8,
1210
1211         .max_register = 255,
1212         .volatile_reg = ma120x0p_reg_volatile,
1213
1214         .cache_type = REGCACHE_RBTREE,
1215         .reg_defaults = ma120x0p_reg_defaults,
1216         .num_reg_defaults = ARRAY_SIZE(ma120x0p_reg_defaults),
1217 };
1218
1219 static int ma120x0p_i2c_probe(struct i2c_client *i2c,
1220         const struct i2c_device_id *id)
1221 {
1222         int ret;
1223
1224         priv_data = devm_kzalloc(&i2c->dev, sizeof(*priv_data), GFP_KERNEL);
1225         if (!priv_data)
1226                 return -ENOMEM;
1227         i2c_set_clientdata(i2c, priv_data);
1228
1229         priv_data->regmap = devm_regmap_init_i2c(i2c, &ma120x0p_regmap_config);
1230         if (IS_ERR(priv_data->regmap)) {
1231                 ret = PTR_ERR(priv_data->regmap);
1232                 return ret;
1233         }
1234
1235         //Startup sequence
1236
1237         //Make sure the device is muted
1238         priv_data->mute_gpio = devm_gpiod_get_optional(&i2c->dev, "mute_gp",
1239                 GPIOD_OUT_LOW);
1240         if (IS_ERR(priv_data->mute_gpio)) {
1241                 ret = PTR_ERR(priv_data->mute_gpio);
1242                 dev_err(&i2c->dev, "Failed to get mute gpio line: %d\n", ret);
1243                 return ret;
1244         }
1245         msleep(50);
1246
1247 // MA120xx0P devices are usually powered by an integrated boost converter.
1248 // An option GPIO control line is provided to enable the booster properly and
1249 // in sync with the enable and mute GPIO lines.
1250         priv_data->booster_gpio = devm_gpiod_get_optional(&i2c->dev,
1251                 "booster_gp", GPIOD_OUT_LOW);
1252         if (IS_ERR(priv_data->booster_gpio)) {
1253                 ret = PTR_ERR(priv_data->booster_gpio);
1254                 dev_err(&i2c->dev,
1255                 "Failed to get booster enable gpio line: %d\n", ret);
1256                 return ret;
1257         }
1258         msleep(50);
1259
1260         //Enable booster and wait 200ms until stable PVDD
1261         gpiod_set_value_cansleep(priv_data->booster_gpio, 1);
1262         msleep(200);
1263
1264         //Enable ma120x0pp
1265         priv_data->enable_gpio = devm_gpiod_get_optional(&i2c->dev,
1266                 "enable_gp", GPIOD_OUT_LOW);
1267         if (IS_ERR(priv_data->enable_gpio)) {
1268                 ret = PTR_ERR(priv_data->enable_gpio);
1269                 dev_err(&i2c->dev,
1270                 "Failed to get ma120x0p enable gpio line: %d\n", ret);
1271                 return ret;
1272         }
1273         msleep(50);
1274
1275         //Optional use of ma120x0pp error line as an interrupt trigger to
1276         //platform GPIO.
1277         //Get error input gpio ma120x0p
1278         priv_data->error_gpio = devm_gpiod_get_optional(&i2c->dev,
1279                  "error_gp", GPIOD_IN);
1280         if (IS_ERR(priv_data->error_gpio)) {
1281                 ret = PTR_ERR(priv_data->error_gpio);
1282                 dev_err(&i2c->dev,
1283                         "Failed to get ma120x0p error gpio line: %d\n", ret);
1284                 return ret;
1285         }
1286
1287         if (priv_data->error_gpio != NULL) {
1288                 irqNumber = gpiod_to_irq(priv_data->error_gpio);
1289
1290                 ret = devm_request_threaded_irq(&i2c->dev,
1291                          irqNumber, ma120x0p_irq_handler,
1292                          NULL, IRQF_TRIGGER_FALLING,
1293                          "ma120x0p", priv_data);
1294                 if (ret != 0)
1295                         dev_warn(&i2c->dev, "Failed to request IRQ: %d\n",
1296                                 ret);
1297         }
1298
1299         ret = devm_snd_soc_register_component(&i2c->dev,
1300                 &ma120x0p_component_driver, &ma120x0p_dai, 1);
1301
1302         return ret;
1303 }
1304
1305 static irqreturn_t ma120x0p_irq_handler(int irq, void *data)
1306 {
1307         gpiod_set_value_cansleep(priv_data->mute_gpio, 0);
1308         gpiod_set_value_cansleep(priv_data->enable_gpio, 1);
1309         return IRQ_HANDLED;
1310 }
1311
1312 static int ma120x0p_i2c_remove(struct i2c_client *i2c)
1313 {
1314         snd_soc_unregister_component(&i2c->dev);
1315         i2c_set_clientdata(i2c, NULL);
1316
1317         gpiod_set_value_cansleep(priv_data->mute_gpio, 0);
1318         msleep(30);
1319         gpiod_set_value_cansleep(priv_data->enable_gpio, 1);
1320         msleep(200);
1321         gpiod_set_value_cansleep(priv_data->booster_gpio, 0);
1322         msleep(200);
1323
1324         kfree(priv_data);
1325
1326         return 0;
1327 }
1328
1329 static void ma120x0p_i2c_shutdown(struct i2c_client *i2c)
1330 {
1331         snd_soc_unregister_component(&i2c->dev);
1332         i2c_set_clientdata(i2c, NULL);
1333
1334         gpiod_set_value_cansleep(priv_data->mute_gpio, 0);
1335         msleep(30);
1336         gpiod_set_value_cansleep(priv_data->enable_gpio, 1);
1337         msleep(200);
1338         gpiod_set_value_cansleep(priv_data->booster_gpio, 0);
1339         msleep(200);
1340
1341         kfree(priv_data);
1342 }
1343
1344 static const struct i2c_device_id ma120x0p_i2c_id[] = {
1345         { "ma120x0p", 0 },
1346         { }
1347 };
1348
1349 MODULE_DEVICE_TABLE(i2c, ma120x0p_i2c_id);
1350
1351 static struct i2c_driver ma120x0p_i2c_driver = {
1352         .driver = {
1353                 .name = "ma120x0p",
1354                 .owner = THIS_MODULE,
1355                 .of_match_table = ma120x0p_of_match,
1356         },
1357         .probe = ma120x0p_i2c_probe,
1358         .remove = ma120x0p_i2c_remove,
1359         .shutdown = ma120x0p_i2c_shutdown,
1360         .id_table = ma120x0p_i2c_id
1361 };
1362
1363 static int __init ma120x0p_modinit(void)
1364 {
1365         int ret = 0;
1366
1367         ret = i2c_add_driver(&ma120x0p_i2c_driver);
1368         if (ret != 0) {
1369                 pr_err("Failed to register MA120X0P I2C driver: %d\n", ret);
1370                 return ret;
1371         }
1372         return ret;
1373 }
1374 module_init(ma120x0p_modinit);
1375
1376 static void __exit ma120x0p_exit(void)
1377 {
1378         i2c_del_driver(&ma120x0p_i2c_driver);
1379 }
1380 module_exit(ma120x0p_exit);
1381
1382 MODULE_AUTHOR("Ariel Muszkat ariel.muszkat@gmail.com>");
1383 MODULE_DESCRIPTION("ASoC driver for ma120x0p");
1384 MODULE_LICENSE("GPL v2");