OSDN Git Service

2b9f2bae246e6c3ac2151b3edbc91cc4a3645bd6
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / sound / soc / codecs / tas2559 / tas2559-regmap.c
1 /*
2 ** =============================================================================
3 ** Copyright (c) 2016  Texas Instruments Inc.
4 ** Copyright (C) 2017 XiaoMi, Inc.
5 **
6 ** This program is free software; you can redistribute it and/or modify it under
7 ** the terms of the GNU General Public License as published by the Free Software
8 ** Foundation; version 2.
9 **
10 ** This program is distributed in the hope that it will be useful, but WITHOUT
11 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 **
14 ** File:
15 **     tas2559-regmap.c
16 **
17 ** Description:
18 **     I2C driver with regmap for Texas Instruments TAS2559 High Performance 4W Smart Amplifier
19 **
20 ** =============================================================================
21 */
22
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/pm.h>
28 #include <linux/i2c.h>
29 #include <linux/gpio.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/firmware.h>
32 #include <linux/regmap.h>
33 #include <linux/of.h>
34 #include <linux/of_gpio.h>
35 #include <linux/slab.h>
36 #include <linux/syscalls.h>
37 #include <linux/fcntl.h>
38 #include <linux/uaccess.h>
39 #include <linux/interrupt.h>
40 #include <soc/qcom/socinfo.h>
41 #include "tas2559.h"
42 #include "tas2559-core.h"
43 #include "tas2560.h"
44
45 #ifdef CONFIG_TAS2559_CODEC
46 #include "tas2559-codec.h"
47 #endif
48
49 #ifdef CONFIG_TAS2559_MISC
50 #include "tas2559-misc.h"
51 #endif
52
53 #define ENABLE_TILOAD
54 #ifdef ENABLE_TILOAD
55 #include "tiload.h"
56 #endif
57
58 /*
59 * tas2559_i2c_write_device : write single byte to device
60 * platform dependent, need platform specific support
61 */
62 static int tas2559_i2c_write_device(struct tas2559_priv *pTAS2559,
63                                     unsigned char addr,
64                                     unsigned char reg,
65                                     unsigned char value)
66 {
67         int nResult = 0;
68
69         pTAS2559->client->addr = addr;
70         nResult = regmap_write(pTAS2559->mpRegmap, reg, value);
71
72         if (nResult < 0)
73                 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
74                         __func__, addr, nResult);
75
76         return nResult;
77 }
78
79 /*
80 * tas2559_i2c_bulkwrite_device : write multiple bytes to device
81 * platform dependent, need platform specific support
82 */
83 static int tas2559_i2c_bulkwrite_device(struct tas2559_priv *pTAS2559,
84                                         unsigned char addr,
85                                         unsigned char reg,
86                                         unsigned char *pBuf,
87                                         unsigned int len)
88 {
89         int nResult = 0;
90
91         pTAS2559->client->addr = addr;
92         nResult = regmap_bulk_write(pTAS2559->mpRegmap, reg, pBuf, len);
93
94         if (nResult < 0)
95                 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
96                         __func__, addr, nResult);
97
98         return nResult;
99 }
100
101 /*
102 * tas2559_i2c_read_device : read single byte from device
103 * platform dependent, need platform specific support
104 */
105 static int tas2559_i2c_read_device(struct tas2559_priv *pTAS2559,
106                                    unsigned char addr,
107                                    unsigned char reg,
108                                    unsigned char *p_value)
109 {
110         int nResult = 0;
111         unsigned int val = 0;
112
113         pTAS2559->client->addr = addr;
114         nResult = regmap_read(pTAS2559->mpRegmap, reg, &val);
115
116         if (nResult < 0)
117                 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
118                         __func__, addr, nResult);
119         else
120                 *p_value = (unsigned char)val;
121
122         return nResult;
123 }
124
125 /*
126 * tas2559_i2c_bulkread_device : read multiple bytes from device
127 * platform dependent, need platform specific support
128 */
129 static int tas2559_i2c_bulkread_device(struct tas2559_priv *pTAS2559,
130                                        unsigned char addr,
131                                        unsigned char reg,
132                                        unsigned char *p_value,
133                                        unsigned int len)
134 {
135         int nResult = 0;
136
137         pTAS2559->client->addr = addr;
138         nResult = regmap_bulk_read(pTAS2559->mpRegmap, reg, p_value, len);
139
140         if (nResult < 0)
141                 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
142                         __func__, addr, nResult);
143
144         return nResult;
145 }
146
147 static int tas2559_i2c_update_bits(struct tas2559_priv *pTAS2559,
148                                    unsigned char addr,
149                                    unsigned char reg,
150                                    unsigned char mask,
151                                    unsigned char value)
152 {
153         int nResult = 0;
154
155         pTAS2559->client->addr = addr;
156         nResult = regmap_update_bits(pTAS2559->mpRegmap, reg, mask, value);
157
158         if (nResult < 0)
159                 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
160                         __func__, addr, nResult);
161
162         return nResult;
163 }
164
165 /*
166 * tas2559_change_book_page : switch to certain book and page
167 * platform independent, don't change unless necessary
168 */
169 static int tas2559_change_book_page(struct tas2559_priv *pTAS2559,
170                                     enum channel chn,
171                                     unsigned char nBook,
172                                     unsigned char nPage)
173 {
174         int nResult = 0;
175
176         if (chn & DevA) {
177                 if (pTAS2559->mnDevACurrentBook == nBook) {
178                         if (pTAS2559->mnDevACurrentPage != nPage) {
179                                 nResult = tas2559_i2c_write_device(pTAS2559,
180                                                                    pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_PAGE, nPage);
181
182                                 if (nResult >= 0)
183                                         pTAS2559->mnDevACurrentPage = nPage;
184                         }
185                 } else {
186                         nResult = tas2559_i2c_write_device(pTAS2559,
187                                                            pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_PAGE, 0);
188
189                         if (nResult >= 0) {
190                                 pTAS2559->mnDevACurrentPage = 0;
191                                 nResult = tas2559_i2c_write_device(pTAS2559,
192                                                                    pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_REG, nBook);
193                                 pTAS2559->mnDevACurrentBook = nBook;
194
195                                 if (nPage != 0) {
196                                         nResult = tas2559_i2c_write_device(pTAS2559,
197                                                                            pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_PAGE, nPage);
198                                         pTAS2559->mnDevACurrentPage = nPage;
199                                 }
200                         }
201                 }
202         }
203
204         if (chn & DevB) {
205                 if (pTAS2559->mnDevBCurrentBook == nBook) {
206                         if (pTAS2559->mnDevBCurrentPage != nPage) {
207                                 nResult = tas2559_i2c_write_device(pTAS2559,
208                                                                    pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_PAGE, nPage);
209
210                                 if (nResult >= 0)
211                                         pTAS2559->mnDevBCurrentPage = nPage;
212                         }
213                 } else {
214                         nResult = tas2559_i2c_write_device(pTAS2559,
215                                                            pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_PAGE, 0);
216
217                         if (nResult >= 0) {
218                                 pTAS2559->mnDevBCurrentPage = 0;
219                                 nResult = tas2559_i2c_write_device(pTAS2559,
220                                                                    pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_REG, nBook);
221                                 pTAS2559->mnDevBCurrentBook = nBook;
222
223                                 if (nPage != 0) {
224                                         tas2559_i2c_write_device(pTAS2559,
225                                                                  pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_PAGE, nPage);
226                                         pTAS2559->mnDevBCurrentPage = nPage;
227                                 }
228                         }
229                 }
230         }
231
232         return nResult;
233 }
234
235 /*
236 * tas2559_dev_read :
237 * platform independent, don't change unless necessary
238 */
239 static int tas2559_dev_read(struct tas2559_priv *pTAS2559,
240                             enum channel chn,
241                             unsigned int nRegister,
242                             unsigned int *pValue)
243 {
244         int nResult = 0;
245         unsigned char Value = 0;
246
247         mutex_lock(&pTAS2559->dev_lock);
248
249         if (pTAS2559->mbTILoadActive) {
250                 if (!(nRegister & 0x80000000)) {
251                         /* let only reads from TILoad pass. */
252                         goto end;
253                 }
254
255                 nRegister &= ~0x80000000;
256
257                 dev_dbg(pTAS2559->dev, "TiLoad R CH[%d] REG B[%d]P[%d]R[%d]\n",
258                         chn,
259                         TAS2559_BOOK_ID(nRegister),
260                         TAS2559_PAGE_ID(nRegister),
261                         TAS2559_PAGE_REG(nRegister));
262         }
263
264         nResult = tas2559_change_book_page(pTAS2559, chn,
265                                            TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
266
267         if (nResult >= 0) {
268                 if (chn == DevA)
269                         nResult = tas2559_i2c_read_device(pTAS2559,
270                                                           pTAS2559->mnDevAAddr, TAS2559_PAGE_REG(nRegister), &Value);
271                 else
272                         if (chn == DevB)
273                                 nResult = tas2559_i2c_read_device(pTAS2559,
274                                                                   pTAS2559->mnDevBAddr, TAS2559_PAGE_REG(nRegister), &Value);
275                         else {
276                                 dev_err(pTAS2559->dev, "%s, read chn ERROR %d\n", __func__, chn);
277                                 nResult = -EINVAL;
278                         }
279
280                 if (nResult >= 0)
281                         *pValue = Value;
282         }
283
284 end:
285
286         mutex_unlock(&pTAS2559->dev_lock);
287         return nResult;
288 }
289
290 /*
291 * tas2559_dev_write :
292 * platform independent, don't change unless necessary
293 */
294 static int tas2559_dev_write(struct tas2559_priv *pTAS2559,
295                              enum channel chn,
296                              unsigned int nRegister,
297                              unsigned int nValue)
298 {
299         int nResult = 0;
300
301         mutex_lock(&pTAS2559->dev_lock);
302
303         if ((nRegister == 0xAFFEAFFE) && (nValue == 0xBABEBABE)) {
304                 pTAS2559->mbTILoadActive = true;
305                 dev_dbg(pTAS2559->dev, "TiLoad Active\n");
306                 goto end;
307         }
308
309         if ((nRegister == 0xBABEBABE) && (nValue == 0xAFFEAFFE)) {
310                 pTAS2559->mbTILoadActive = false;
311                 dev_dbg(pTAS2559->dev, "TiLoad DeActive\n");
312                 goto end;
313         }
314
315         if (pTAS2559->mbTILoadActive) {
316                 if (!(nRegister & 0x80000000)) {
317                         /* let only writes from TILoad pass. */
318                         goto end;
319                 }
320
321                 nRegister &= ~0x80000000;
322                 dev_dbg(pTAS2559->dev, "TiLoad W CH[%d] REG B[%d]P[%d]R[%d] =0x%x\n",
323                         chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
324                         TAS2559_PAGE_REG(nRegister), nValue);
325         }
326
327         nResult = tas2559_change_book_page(pTAS2559,
328                                            chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
329
330         if (nResult >= 0) {
331                 if (chn & DevA)
332                         nResult = tas2559_i2c_write_device(pTAS2559,
333                                                            pTAS2559->mnDevAAddr, TAS2559_PAGE_REG(nRegister), nValue);
334
335                 if (chn & DevB)
336                         nResult = tas2559_i2c_write_device(pTAS2559,
337                                                            pTAS2559->mnDevBAddr, TAS2559_PAGE_REG(nRegister), nValue);
338         }
339
340 end:
341
342         mutex_unlock(&pTAS2559->dev_lock);
343         return nResult;
344 }
345
346 /*
347 * tas2559_dev_bulk_read :
348 * platform independent, don't change unless necessary
349 */
350 static int tas2559_dev_bulk_read(struct tas2559_priv *pTAS2559,
351                                  enum channel chn,
352                                  unsigned int nRegister,
353                                  unsigned char *pData,
354                                  unsigned int nLength)
355 {
356         int nResult = 0;
357         unsigned char reg = 0;
358
359         mutex_lock(&pTAS2559->dev_lock);
360
361         if (pTAS2559->mbTILoadActive) {
362                 if (!(nRegister & 0x80000000)) {
363                         /* let only writes from TILoad pass. */
364                         goto end;
365                 }
366
367                 nRegister &= ~0x80000000;
368                 dev_dbg(pTAS2559->dev, "TiLoad BR CH[%d] REG B[%d]P[%d]R[%d], count=%d\n",
369                         chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
370                         TAS2559_PAGE_REG(nRegister), nLength);
371         }
372
373         nResult = tas2559_change_book_page(pTAS2559, chn,
374                                            TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
375
376         if (nResult >= 0) {
377                 reg = TAS2559_PAGE_REG(nRegister);
378
379                 if (chn == DevA)
380                         nResult = tas2559_i2c_bulkread_device(pTAS2559,
381                                                               pTAS2559->mnDevAAddr, reg, pData, nLength);
382                 else
383                         if (chn == DevB)
384                                 nResult = tas2559_i2c_bulkread_device(pTAS2559,
385                                                                       pTAS2559->mnDevBAddr, reg, pData, nLength);
386                         else {
387                                 dev_err(pTAS2559->dev, "%s, chn ERROR %d\n", __func__, chn);
388                                 nResult = -EINVAL;
389                         }
390         }
391
392 end:
393
394         mutex_unlock(&pTAS2559->dev_lock);
395         return nResult;
396 }
397
398 /*
399 * tas2559_dev_bulk_write :
400 * platform independent, don't change unless necessary
401 */
402 static int tas2559_dev_bulk_write(struct tas2559_priv *pTAS2559,
403                                   enum channel chn,
404                                   unsigned int nRegister,
405                                   unsigned char *pData,
406                                   unsigned int nLength)
407 {
408         int nResult = 0;
409         unsigned char reg = 0;
410
411         mutex_lock(&pTAS2559->dev_lock);
412
413         if (pTAS2559->mbTILoadActive) {
414                 if (!(nRegister & 0x80000000)) {
415                         /* let only writes from TILoad pass. */
416                         goto end;
417                 }
418
419                 nRegister &= ~0x80000000;
420                 dev_dbg(pTAS2559->dev, "TiLoad BW CH[%d] REG B[%d]P[%d]R[%d], count=%d\n",
421                         chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
422                         TAS2559_PAGE_REG(nRegister), nLength);
423         }
424
425         nResult = tas2559_change_book_page(pTAS2559, chn,
426                                            TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
427
428         if (nResult >= 0) {
429                 reg = TAS2559_PAGE_REG(nRegister);
430
431                 if (chn & DevA)
432                         nResult = tas2559_i2c_bulkwrite_device(pTAS2559,
433                                                                pTAS2559->mnDevAAddr, reg, pData, nLength);
434
435                 if (chn & DevB)
436                         nResult = tas2559_i2c_bulkwrite_device(pTAS2559,
437                                                                pTAS2559->mnDevBAddr, reg, pData, nLength);
438         }
439
440 end:
441
442         mutex_unlock(&pTAS2559->dev_lock);
443         return nResult;
444 }
445
446 /*
447 * tas2559_dev_update_bits :
448 * platform independent, don't change unless necessary
449 */
450 static int tas2559_dev_update_bits(
451         struct tas2559_priv *pTAS2559,
452         enum channel chn,
453         unsigned int nRegister,
454         unsigned int nMask,
455         unsigned int nValue)
456 {
457         int nResult = 0;
458
459         mutex_lock(&pTAS2559->dev_lock);
460
461         if (pTAS2559->mbTILoadActive) {
462                 if (!(nRegister & 0x80000000)) {
463                         /* let only writes from TILoad pass. */
464                         goto end;
465                 }
466
467                 nRegister &= ~0x80000000;
468                 dev_dbg(pTAS2559->dev, "TiLoad SB CH[%d] REG B[%d]P[%d]R[%d], mask=0x%x, value=0x%x\n",
469                         chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
470                         TAS2559_PAGE_REG(nRegister), nMask, nValue);
471         }
472
473         nResult = tas2559_change_book_page(pTAS2559,
474                                            chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
475
476         if (nResult >= 0) {
477                 if (chn & DevA)
478                         nResult = tas2559_i2c_update_bits(pTAS2559,
479                                                           pTAS2559->mnDevAAddr, TAS2559_PAGE_REG(nRegister), nMask, nValue);
480
481                 if (chn & DevB)
482                         nResult = tas2559_i2c_update_bits(pTAS2559,
483                                                           pTAS2559->mnDevBAddr, TAS2559_PAGE_REG(nRegister), nMask, nValue);
484         }
485
486 end:
487
488         mutex_unlock(&pTAS2559->dev_lock);
489         return nResult;
490 }
491
492 void tas2559_clearIRQ(struct tas2559_priv *pTAS2559)
493 {
494         unsigned int nValue;
495         int nResult = 0;
496
497         nResult = pTAS2559->read(pTAS2559, DevA, TAS2559_FLAGS_1, &nValue);
498
499         if (nResult >= 0)
500                 pTAS2559->read(pTAS2559, DevA, TAS2559_FLAGS_2, &nValue);
501
502         nResult = pTAS2559->read(pTAS2559, DevB, TAS2560_FLAGS_1, &nValue);
503
504         if (nResult >= 0)
505                 pTAS2559->read(pTAS2559, DevB, TAS2560_FLAGS_2, &nValue);
506 }
507
508 void tas2559_enableIRQ(struct tas2559_priv *pTAS2559, enum channel chl, bool enable)
509 {
510         static bool bDevAEnable;
511         static bool bDevBEnable;
512
513         if (enable) {
514                 if (pTAS2559->mbIRQEnable)
515                         return;
516
517                 if (chl & DevA) {
518                         if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)) {
519                                 enable_irq(pTAS2559->mnDevAIRQ);
520                                 bDevAEnable = true;
521                         }
522                 }
523                 if (chl & DevB) {
524                         if (gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
525                                 if (pTAS2559->mnDevAGPIOIRQ == pTAS2559->mnDevBGPIOIRQ) {
526                                         if (!bDevAEnable) {
527                                                 enable_irq(pTAS2559->mnDevBIRQ);
528                                                 bDevBEnable = true;
529                                         } else
530                                                 bDevBEnable = false;
531                                 } else {
532                                         enable_irq(pTAS2559->mnDevBIRQ);
533                                         bDevBEnable = true;
534                                 }
535                         }
536                 }
537
538                 if (bDevAEnable || bDevBEnable) {
539                         /* check after 10 ms */
540                         schedule_delayed_work(&pTAS2559->irq_work, msecs_to_jiffies(10));
541                 }
542                 pTAS2559->mbIRQEnable = true;
543         } else {
544                 if (pTAS2559->mbIRQEnable) {
545                         if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)) {
546                                 if (bDevAEnable) {
547                                         disable_irq_nosync(pTAS2559->mnDevAIRQ);
548                                         bDevAEnable = false;
549                                 }
550                         }
551
552                         if (gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
553                                 if (bDevBEnable) {
554                                         disable_irq_nosync(pTAS2559->mnDevBIRQ);
555                                         bDevBEnable = false;
556                                 }
557                         }
558
559                         pTAS2559->mbIRQEnable = false;
560                 }
561         }
562 }
563
564 static void tas2559_hw_reset(struct tas2559_priv *pTAS2559)
565 {
566         dev_dbg(pTAS2559->dev, "%s\n", __func__);
567
568         if (gpio_is_valid(pTAS2559->mnDevAGPIORST)) {
569                 gpio_direction_output(pTAS2559->mnDevAGPIORST, 0);
570                 msleep(5);
571                 gpio_direction_output(pTAS2559->mnDevAGPIORST, 1);
572                 msleep(2);
573         }
574
575         if (gpio_is_valid(pTAS2559->mnDevBGPIORST)) {
576                 if (pTAS2559->mnDevAGPIORST != pTAS2559->mnDevBGPIORST) {
577                         gpio_direction_output(pTAS2559->mnDevBGPIORST, 0);
578                         msleep(5);
579                         gpio_direction_output(pTAS2559->mnDevBGPIORST, 1);
580                         msleep(2);
581                 }
582         }
583
584         pTAS2559->mnDevACurrentBook = -1;
585         pTAS2559->mnDevACurrentPage = -1;
586         pTAS2559->mnDevBCurrentBook = -1;
587         pTAS2559->mnDevBCurrentPage = -1;
588
589         if (pTAS2559->mnErrCode)
590                 dev_info(pTAS2559->dev, "%s, ErrCode=0x%x\n", __func__, pTAS2559->mnErrCode);
591
592         pTAS2559->mnErrCode = 0;
593 }
594
595 static void irq_work_routine(struct work_struct *work)
596 {
597         struct tas2559_priv *pTAS2559 =
598                 container_of(work, struct tas2559_priv, irq_work.work);
599         struct TConfiguration *pConfiguration;
600         unsigned int nDevLInt1Status = 0, nDevLInt2Status = 0;
601         unsigned int nDevRInt1Status = 0, nDevRInt2Status = 0;
602         int nCounter = 2;
603         int nResult = 0;
604
605 #ifdef CONFIG_TAS2559_CODEC
606         mutex_lock(&pTAS2559->codec_lock);
607 #endif
608
609 #ifdef CONFIG_TAS2559_MISC
610         mutex_lock(&pTAS2559->file_lock);
611 #endif
612
613         if (pTAS2559->mbRuntimeSuspend) {
614                 dev_info(pTAS2559->dev, "%s, Runtime Suspended\n", __func__);
615                 goto end;
616         }
617
618         if (pTAS2559->mnErrCode & ERROR_FAILSAFE)
619                 goto program;
620
621         if (!pTAS2559->mbPowerUp) {
622                 dev_info(pTAS2559->dev, "%s, device not powered\n", __func__);
623                 goto end;
624         }
625
626         if ((!pTAS2559->mpFirmware->mnConfigurations)
627             || (!pTAS2559->mpFirmware->mnPrograms)) {
628                 dev_info(pTAS2559->dev, "%s, firmware not loaded\n", __func__);
629                 goto end;
630         }
631
632         pConfiguration = &(pTAS2559->mpFirmware->mpConfigurations[pTAS2559->mnCurrentConfiguration]);
633
634         if (pConfiguration->mnDevices & DevA) {
635                 nResult = tas2559_dev_read(pTAS2559, DevA, TAS2559_FLAGS_1, &nDevLInt1Status);
636
637                 if (nResult >= 0)
638                         nResult = tas2559_dev_read(pTAS2559, DevA, TAS2559_FLAGS_2, &nDevLInt2Status);
639                 else
640                         goto program;
641
642                 if (((nDevLInt1Status & 0xfc) != 0) || ((nDevLInt2Status & 0x0c) != 0)) {
643                         /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
644                         dev_dbg(pTAS2559->dev, "IRQ critical Error DevA: 0x%x, 0x%x\n",
645                                 nDevLInt1Status, nDevLInt2Status);
646
647                         if (nDevLInt1Status & 0x80) {
648                                 pTAS2559->mnErrCode |= ERROR_OVER_CURRENT;
649                                 dev_err(pTAS2559->dev, "DEVA SPK over current!\n");
650                         } else
651                                 pTAS2559->mnErrCode &= ~ERROR_OVER_CURRENT;
652
653                         if (nDevLInt1Status & 0x40) {
654                                 pTAS2559->mnErrCode |= ERROR_UNDER_VOLTAGE;
655                                 dev_err(pTAS2559->dev, "DEVA SPK under voltage!\n");
656                         } else
657                                 pTAS2559->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
658
659                         if (nDevLInt1Status & 0x20) {
660                                 pTAS2559->mnErrCode |= ERROR_CLK_HALT;
661                                 dev_err(pTAS2559->dev, "DEVA clk halted!\n");
662                         } else
663                                 pTAS2559->mnErrCode &= ~ERROR_CLK_HALT;
664
665                         if (nDevLInt1Status & 0x10) {
666                                 pTAS2559->mnErrCode |= ERROR_DIE_OVERTEMP;
667                                 dev_err(pTAS2559->dev, "DEVA die over temperature!\n");
668                         } else
669                                 pTAS2559->mnErrCode &= ~ERROR_DIE_OVERTEMP;
670
671                         if (nDevLInt1Status & 0x08) {
672                                 pTAS2559->mnErrCode |= ERROR_BROWNOUT;
673                                 dev_err(pTAS2559->dev, "DEVA brownout!\n");
674                         } else
675                                 pTAS2559->mnErrCode &= ~ERROR_BROWNOUT;
676
677                         if (nDevLInt1Status & 0x04) {
678                                 pTAS2559->mnErrCode |= ERROR_CLK_LOST;
679                                 dev_err(pTAS2559->dev, "DEVA clock lost!\n");
680                         } else
681                                 pTAS2559->mnErrCode &= ~ERROR_CLK_LOST;
682
683                         if (nDevLInt2Status & 0x08) {
684                                 pTAS2559->mnErrCode |= ERROR_CLK_DET1;
685                                 dev_err(pTAS2559->dev, "DEVA clk detection 1!\n");
686                         } else
687                                 pTAS2559->mnErrCode &= ~ERROR_CLK_DET1;
688
689                         if (nDevLInt2Status & 0x04) {
690                                 pTAS2559->mnErrCode |= ERROR_CLK_DET2;
691                                 dev_err(pTAS2559->dev, "DEVA clk detection 2!\n");
692                         } else
693                                 pTAS2559->mnErrCode &= ~ERROR_CLK_DET2;
694
695                         goto program;
696                 } else {
697                         dev_dbg(pTAS2559->dev, "IRQ status DevA: 0x%x, 0x%x\n",
698                                 nDevLInt1Status, nDevLInt2Status);
699                         nCounter = 2;
700
701                         while (nCounter > 0) {
702                                 nResult = tas2559_dev_read(pTAS2559, DevA, TAS2559_POWER_UP_FLAG_REG, &nDevLInt1Status);
703
704                                 if (nResult < 0)
705                                         goto program;
706
707                                 if ((nDevLInt1Status & 0xc0) == 0xc0)
708                                         break;
709
710                                 nCounter--;
711
712                                 if (nCounter > 0) {
713                                         /* in case check pow status just after power on TAS2559 */
714                                         dev_dbg(pTAS2559->dev, "PowSts A: 0x%x, check again after 10ms\n",
715                                                 nDevLInt1Status);
716                                         msleep(10);
717                                 }
718                         }
719
720                         if ((nDevLInt1Status & 0xc0) != 0xc0) {
721                                 dev_err(pTAS2559->dev, "%s, Critical DevA ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
722                                         __func__,
723                                         TAS2559_BOOK_ID(TAS2559_POWER_UP_FLAG_REG),
724                                         TAS2559_PAGE_ID(TAS2559_POWER_UP_FLAG_REG),
725                                         TAS2559_PAGE_REG(TAS2559_POWER_UP_FLAG_REG),
726                                         nDevLInt1Status);
727                                 pTAS2559->mnErrCode |= ERROR_CLASSD_PWR;
728                                 goto program;
729                         }
730
731                         pTAS2559->mnErrCode &= ~ERROR_CLASSD_PWR;
732                 }
733         }
734
735         if (pConfiguration->mnDevices & DevB) {
736                 nResult = tas2559_dev_read(pTAS2559, DevB, TAS2560_FLAGS_1, &nDevRInt1Status);
737
738                 if (nResult >= 0)
739                         nResult = tas2559_dev_read(pTAS2559, DevB, TAS2560_FLAGS_2, &nDevRInt2Status);
740                 else
741                         goto program;
742
743                 if (((nDevRInt1Status & 0xfc) != 0) || ((nDevRInt2Status & 0xc0) != 0)) {
744                         /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
745                         dev_dbg(pTAS2559->dev, "IRQ critical Error DevB: 0x%x, 0x%x\n",
746                                 nDevRInt1Status, nDevRInt2Status);
747
748                         if (nDevRInt1Status & 0x80) {
749                                 pTAS2559->mnErrCode |= ERROR_OVER_CURRENT;
750                                 dev_err(pTAS2559->dev, "DEVB SPK over current!\n");
751                         } else
752                                 pTAS2559->mnErrCode &= ~ERROR_OVER_CURRENT;
753
754                         if (nDevRInt1Status & 0x40) {
755                                 pTAS2559->mnErrCode |= ERROR_UNDER_VOLTAGE;
756                                 dev_err(pTAS2559->dev, "DEVB SPK under voltage!\n");
757                         } else
758                                 pTAS2559->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
759
760                         if (nDevRInt1Status & 0x20) {
761                                 pTAS2559->mnErrCode |= ERROR_CLK_HALT;
762                                 dev_err(pTAS2559->dev, "DEVB clk halted!\n");
763                         } else
764                                 pTAS2559->mnErrCode &= ~ERROR_CLK_HALT;
765
766                         if (nDevRInt1Status & 0x10) {
767                                 pTAS2559->mnErrCode |= ERROR_DIE_OVERTEMP;
768                                 dev_err(pTAS2559->dev, "DEVB die over temperature!\n");
769                         } else
770                                 pTAS2559->mnErrCode &= ~ERROR_DIE_OVERTEMP;
771
772                         if (nDevRInt1Status & 0x08) {
773                                 pTAS2559->mnErrCode |= ERROR_BROWNOUT;
774                                 dev_err(pTAS2559->dev, "DEVB brownout!\n");
775                         } else
776                                 pTAS2559->mnErrCode &= ~ERROR_BROWNOUT;
777
778                         if (nDevRInt1Status & 0x04) {
779                                 pTAS2559->mnErrCode |= ERROR_CLK_LOST;
780                                 dev_err(pTAS2559->dev, "DEVB clock lost!\n");
781                         } else
782                                 pTAS2559->mnErrCode &= ~ERROR_CLK_LOST;
783
784                         if (nDevRInt2Status & 0x80) {
785                                 pTAS2559->mnErrCode |= ERROR_CLK_DET1;
786                                 dev_err(pTAS2559->dev, "DEVB clk detection 1!\n");
787                         } else
788                                 pTAS2559->mnErrCode &= ~ERROR_CLK_DET1;
789
790                         if (nDevRInt2Status & 0x40) {
791                                 pTAS2559->mnErrCode |= ERROR_CLK_DET2;
792                                 dev_err(pTAS2559->dev, "DEVB clk detection 2!\n");
793                         } else
794                                 pTAS2559->mnErrCode &= ~ERROR_CLK_DET2;
795
796                         goto program;
797                 } else {
798                         dev_dbg(pTAS2559->dev, "IRQ status DevB: 0x%x, 0x%x\n",
799                                 nDevRInt1Status, nDevRInt2Status);
800                         nCounter = 2;
801
802                         while (nCounter > 0) {
803                                 nResult = tas2559_dev_read(pTAS2559, DevB, TAS2560_POWER_UP_FLAG_REG, &nDevRInt1Status);
804
805                                 if (nResult < 0)
806                                         goto program;
807
808                                 if ((nDevRInt1Status & 0xc0) == 0xc0)
809                                         break;
810
811                                 nCounter--;
812
813                                 if (nCounter > 0) {
814                                         /* in case check pow status just after power on TAS2560 */
815                                         dev_dbg(pTAS2559->dev, "PowSts B: 0x%x, check again after 10ms\n",
816                                                 nDevRInt1Status);
817                                         msleep(10);
818                                 }
819                         }
820
821                         if ((nDevRInt1Status & 0xc0) != 0xc0) {
822                                 dev_err(pTAS2559->dev, "%s, Critical DevB ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
823                                         __func__,
824                                         TAS2559_BOOK_ID(TAS2560_POWER_UP_FLAG_REG),
825                                         TAS2559_PAGE_ID(TAS2560_POWER_UP_FLAG_REG),
826                                         TAS2559_PAGE_REG(TAS2560_POWER_UP_FLAG_REG),
827                                         nDevRInt1Status);
828                                 pTAS2559->mnErrCode |= ERROR_CLASSD_PWR;
829                                 goto program;
830                         }
831
832                         pTAS2559->mnErrCode &= ~ERROR_CLASSD_PWR;
833                 }
834         }
835
836         goto end;
837
838 program:
839         /* hardware reset and reload */
840         tas2559_set_program(pTAS2559, pTAS2559->mnCurrentProgram, pTAS2559->mnCurrentConfiguration);
841
842 end:
843
844 #ifdef CONFIG_TAS2559_MISC
845         mutex_unlock(&pTAS2559->file_lock);
846 #endif
847
848 #ifdef CONFIG_TAS2559_CODEC
849         mutex_unlock(&pTAS2559->codec_lock);
850 #endif
851 }
852
853 static irqreturn_t tas2559_irq_handler(int irq, void *dev_id)
854 {
855         struct tas2559_priv *pTAS2559 = (struct tas2559_priv *)dev_id;
856
857         tas2559_enableIRQ(pTAS2559, DevBoth, false);
858
859         /* get IRQ status after 100 ms */
860         if (!delayed_work_pending(&pTAS2559->irq_work))
861                 schedule_delayed_work(&pTAS2559->irq_work, msecs_to_jiffies(100));
862
863         return IRQ_HANDLED;
864 }
865
866 static void timer_work_routine(struct work_struct *work)
867 {
868         struct tas2559_priv *pTAS2559 = container_of(work, struct tas2559_priv, mtimerwork);
869         int nResult, nTemp, nActTemp;
870         struct TProgram *pProgram;
871         static int nAvg;
872
873 #ifdef CONFIG_TAS2559_CODEC
874         mutex_lock(&pTAS2559->codec_lock);
875 #endif
876
877 #ifdef CONFIG_TAS2559_MISC
878         mutex_lock(&pTAS2559->file_lock);
879 #endif
880
881         if (pTAS2559->mbRuntimeSuspend) {
882                 dev_info(pTAS2559->dev, "%s, Runtime Suspended\n", __func__);
883                 goto end;
884         }
885
886         if (!pTAS2559->mpFirmware->mnConfigurations) {
887                 dev_info(pTAS2559->dev, "%s, firmware not loaded\n", __func__);
888                 goto end;
889         }
890
891         pProgram = &(pTAS2559->mpFirmware->mpPrograms[pTAS2559->mnCurrentProgram]);
892
893         if (!pTAS2559->mbPowerUp
894             || (pProgram->mnAppMode != TAS2559_APP_TUNINGMODE)) {
895                 dev_info(pTAS2559->dev, "%s, pass, Pow=%d, program=%s\n",
896                          __func__, pTAS2559->mbPowerUp, pProgram->mpName);
897                 goto end;
898         }
899
900         nResult = tas2559_get_die_temperature(pTAS2559, &nTemp);
901
902         if (nResult >= 0) {
903                 nActTemp = (int)(nTemp >> 23);
904                 dev_dbg(pTAS2559->dev, "Die=0x%x, degree=%d\n", nTemp, nActTemp);
905
906                 if (!pTAS2559->mnDieTvReadCounter)
907                         nAvg = 0;
908
909                 pTAS2559->mnDieTvReadCounter++;
910                 nAvg += nActTemp;
911
912                 if (!(pTAS2559->mnDieTvReadCounter % LOW_TEMPERATURE_COUNTER)) {
913                         nAvg /= LOW_TEMPERATURE_COUNTER;
914                         dev_dbg(pTAS2559->dev, "check : avg=%d\n", nAvg);
915
916                         if ((nAvg & 0x80000000) != 0) {
917                                 /* if Die temperature is below ZERO */
918                                 if (pTAS2559->mnDevCurrentGain != LOW_TEMPERATURE_GAIN) {
919                                         nResult = tas2559_set_DAC_gain(pTAS2559, DevBoth, LOW_TEMPERATURE_GAIN);
920
921                                         if (nResult < 0)
922                                                 goto end;
923
924                                         pTAS2559->mnDevCurrentGain = LOW_TEMPERATURE_GAIN;
925                                         dev_dbg(pTAS2559->dev, "LOW Temp: set gain to %d\n", LOW_TEMPERATURE_GAIN);
926                                 }
927                         } else if (nAvg > 5) {
928                                 /* if Die temperature is above 5 degree C */
929                                 if (pTAS2559->mnDevCurrentGain != pTAS2559->mnDevGain) {
930                                         nResult = tas2559_set_DAC_gain(pTAS2559, DevBoth, pTAS2559->mnDevGain);
931
932                                 if (nResult < 0)
933                                         goto end;
934
935                                 pTAS2559->mnDevCurrentGain = pTAS2559->mnDevGain;
936                                 dev_dbg(pTAS2559->dev, "LOW Temp: set gain to original\n");
937                                 }
938                         }
939
940                         nAvg = 0;
941                 }
942
943                 if (pTAS2559->mbPowerUp)
944                         hrtimer_start(&pTAS2559->mtimer,
945                                       ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
946         }
947
948 end:
949
950 #ifdef CONFIG_TAS2559_MISC
951         mutex_unlock(&pTAS2559->file_lock);
952 #endif
953
954 #ifdef CONFIG_TAS2559_CODEC
955         mutex_unlock(&pTAS2559->codec_lock);
956 #endif
957 }
958
959 static enum hrtimer_restart temperature_timer_func(struct hrtimer *timer)
960 {
961         struct tas2559_priv *pTAS2559 = container_of(timer, struct tas2559_priv, mtimer);
962
963         if (pTAS2559->mbPowerUp) {
964                 schedule_work(&pTAS2559->mtimerwork);
965
966                 if (!delayed_work_pending(&pTAS2559->irq_work))
967                         schedule_delayed_work(&pTAS2559->irq_work, msecs_to_jiffies(20));
968         }
969
970         return HRTIMER_NORESTART;
971 }
972
973 static int tas2559_runtime_suspend(struct tas2559_priv *pTAS2559)
974 {
975         dev_dbg(pTAS2559->dev, "%s\n", __func__);
976
977         pTAS2559->mbRuntimeSuspend = true;
978
979         if (hrtimer_active(&pTAS2559->mtimer)) {
980                 dev_dbg(pTAS2559->dev, "cancel die temp timer\n");
981                 hrtimer_cancel(&pTAS2559->mtimer);
982         }
983         if (work_pending(&pTAS2559->mtimerwork)) {
984                 dev_dbg(pTAS2559->dev, "cancel timer work\n");
985                 cancel_work_sync(&pTAS2559->mtimerwork);
986         }
987         if (delayed_work_pending(&pTAS2559->irq_work)) {
988                 dev_dbg(pTAS2559->dev, "cancel IRQ work\n");
989                 cancel_delayed_work_sync(&pTAS2559->irq_work);
990         }
991
992         return 0;
993 }
994
995 static int tas2559_runtime_resume(struct tas2559_priv *pTAS2559)
996 {
997         struct TProgram *pProgram;
998
999         dev_dbg(pTAS2559->dev, "%s\n", __func__);
1000         if (!pTAS2559->mpFirmware->mpPrograms) {
1001                 dev_dbg(pTAS2559->dev, "%s, firmware not loaded\n", __func__);
1002                 goto end;
1003         }
1004
1005         if (pTAS2559->mnCurrentProgram >= pTAS2559->mpFirmware->mnPrograms) {
1006                 dev_err(pTAS2559->dev, "%s, firmware corrupted\n", __func__);
1007                 goto end;
1008         }
1009
1010         pProgram = &(pTAS2559->mpFirmware->mpPrograms[pTAS2559->mnCurrentProgram]);
1011         if (pTAS2559->mbPowerUp && (pProgram->mnAppMode == TAS2559_APP_TUNINGMODE)) {
1012                 if (!hrtimer_active(&pTAS2559->mtimer)) {
1013                         dev_dbg(pTAS2559->dev, "%s, start Die Temp check timer\n", __func__);
1014                         pTAS2559->mnDieTvReadCounter = 0;
1015                         hrtimer_start(&pTAS2559->mtimer,
1016                                 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
1017                 }
1018         }
1019
1020         pTAS2559->mbRuntimeSuspend = false;
1021 end:
1022
1023         return 0;
1024 }
1025
1026 static bool tas2559_volatile(struct device *pDev, unsigned int nRegister)
1027 {
1028         return true;
1029 }
1030
1031 static bool tas2559_writeable(struct device *pDev, unsigned int nRegister)
1032 {
1033         return true;
1034 }
1035
1036 static const struct regmap_config tas2559_i2c_regmap = {
1037         .reg_bits = 8,
1038         .val_bits = 8,
1039         .writeable_reg = tas2559_writeable,
1040         .volatile_reg = tas2559_volatile,
1041         .cache_type = REGCACHE_NONE,
1042         .max_register = 128,
1043 };
1044
1045 /*
1046 * tas2559_i2c_probe :
1047 * platform dependent
1048 * should implement hardware reset functionality
1049 */
1050 static int tas2559_i2c_probe(struct i2c_client *pClient,
1051                              const struct i2c_device_id *pID)
1052 {
1053         struct tas2559_priv *pTAS2559;
1054         int nResult;
1055         unsigned int nValue = 0;
1056         const char *fw_name;
1057
1058         dev_info(&pClient->dev, "%s enter\n", __func__);
1059
1060         pTAS2559 = devm_kzalloc(&pClient->dev, sizeof(struct tas2559_priv), GFP_KERNEL);
1061
1062         if (!pTAS2559) {
1063                 dev_err(&pClient->dev, " -ENOMEM\n");
1064                 nResult = -ENOMEM;
1065                 goto err;
1066         }
1067
1068         pTAS2559->client = pClient;
1069         pTAS2559->dev = &pClient->dev;
1070         i2c_set_clientdata(pClient, pTAS2559);
1071         dev_set_drvdata(&pClient->dev, pTAS2559);
1072
1073         pTAS2559->mpRegmap = devm_regmap_init_i2c(pClient, &tas2559_i2c_regmap);
1074
1075         if (IS_ERR(pTAS2559->mpRegmap)) {
1076                 nResult = PTR_ERR(pTAS2559->mpRegmap);
1077                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
1078                         nResult);
1079                 goto err;
1080         }
1081
1082         if (pClient->dev.of_node)
1083                 tas2559_parse_dt(&pClient->dev, pTAS2559);
1084
1085         if (gpio_is_valid(pTAS2559->mnDevAGPIORST)) {
1086                 nResult = gpio_request(pTAS2559->mnDevAGPIORST, "TAS2559-RESET");
1087
1088                 if (nResult < 0) {
1089                         dev_err(pTAS2559->dev, "%s: GPIO %d request error\n",
1090                                 __func__, pTAS2559->mnDevAGPIORST);
1091                         goto err;
1092                 }
1093         }
1094
1095         if (gpio_is_valid(pTAS2559->mnDevBGPIORST)
1096             && (pTAS2559->mnDevAGPIORST != pTAS2559->mnDevBGPIORST)) {
1097                 nResult = gpio_request(pTAS2559->mnDevBGPIORST, "TAS2560-RESET");
1098
1099                 if (nResult < 0) {
1100                         dev_err(pTAS2559->dev, "%s: GPIO %d request error\n",
1101                                 __func__, pTAS2559->mnDevBGPIORST);
1102                         goto err;
1103                 }
1104         }
1105
1106         if (gpio_is_valid(pTAS2559->mnDevAGPIORST)
1107             || gpio_is_valid(pTAS2559->mnDevBGPIORST))
1108                 tas2559_hw_reset(pTAS2559);
1109
1110         pTAS2559->read = tas2559_dev_read;
1111         pTAS2559->write = tas2559_dev_write;
1112         pTAS2559->bulk_read = tas2559_dev_bulk_read;
1113         pTAS2559->bulk_write = tas2559_dev_bulk_write;
1114         pTAS2559->update_bits = tas2559_dev_update_bits;
1115         pTAS2559->enableIRQ = tas2559_enableIRQ;
1116         pTAS2559->clearIRQ = tas2559_clearIRQ;
1117         pTAS2559->set_config = tas2559_set_config;
1118         pTAS2559->set_calibration = tas2559_set_calibration;
1119         pTAS2559->hw_reset = tas2559_hw_reset;
1120         pTAS2559->runtime_suspend = tas2559_runtime_suspend;
1121         pTAS2559->runtime_resume = tas2559_runtime_resume;
1122         pTAS2559->mnRestart = 0;
1123
1124         mutex_init(&pTAS2559->dev_lock);
1125
1126         /* Reset the chip */
1127         nResult = tas2559_dev_write(pTAS2559, DevBoth, TAS2559_SW_RESET_REG, 1);
1128         if (nResult < 0) {
1129                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
1130                 goto err;
1131         }
1132         msleep(1);
1133         nResult = pTAS2559->read(pTAS2559, DevA, TAS2559_REV_PGID_REG, &nValue);
1134         pTAS2559->mnDevAPGID = nValue;
1135         dev_info(&pClient->dev, "TAS2559 PGID=0x%x\n", nValue);
1136         nResult = pTAS2559->read(pTAS2559, DevB, TAS2560_ID_REG, &nValue);
1137         pTAS2559->mnDevBPGID = nValue;
1138         dev_info(pTAS2559->dev, "TAS2560 PGID=0x%02x\n", nValue);
1139
1140         if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)) {
1141                 nResult = gpio_request(pTAS2559->mnDevAGPIOIRQ, "TAS2559-IRQ");
1142
1143                 if (nResult < 0) {
1144                         dev_err(pTAS2559->dev,
1145                                 "%s: GPIO %d request INT error\n",
1146                                 __func__, pTAS2559->mnDevAGPIOIRQ);
1147                         goto err;
1148                 }
1149
1150                 gpio_direction_input(pTAS2559->mnDevAGPIOIRQ);
1151                 pTAS2559->mnDevAIRQ = gpio_to_irq(pTAS2559->mnDevAGPIOIRQ);
1152                 dev_dbg(pTAS2559->dev, "irq = %d\n", pTAS2559->mnDevAIRQ);
1153                 nResult = request_threaded_irq(pTAS2559->mnDevAIRQ, tas2559_irq_handler,
1154                                                NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1155                                                pClient->name, pTAS2559);
1156
1157                 if (nResult < 0) {
1158                         dev_err(pTAS2559->dev,
1159                                 "request_irq failed, %d\n", nResult);
1160                         goto err;
1161                 }
1162
1163                 disable_irq_nosync(pTAS2559->mnDevAIRQ);
1164         }
1165
1166         if (gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
1167                 if (pTAS2559->mnDevAGPIOIRQ != pTAS2559->mnDevBGPIOIRQ) {
1168                         nResult = gpio_request(pTAS2559->mnDevBGPIOIRQ, "TAS2560-IRQ");
1169
1170                         if (nResult < 0) {
1171                                 dev_err(pTAS2559->dev,
1172                                         "%s: GPIO %d request INT error\n",
1173                                         __func__, pTAS2559->mnDevBGPIOIRQ);
1174                                 goto err;
1175                         }
1176
1177                         gpio_direction_input(pTAS2559->mnDevBGPIOIRQ);
1178                         pTAS2559->mnDevBIRQ = gpio_to_irq(pTAS2559->mnDevBGPIOIRQ);
1179                         dev_dbg(pTAS2559->dev, "irq = %d\n", pTAS2559->mnDevBIRQ);
1180                         nResult = request_threaded_irq(pTAS2559->mnDevBIRQ, tas2559_irq_handler,
1181                                                        NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1182                                                        pClient->name, pTAS2559);
1183
1184                         if (nResult < 0) {
1185                                 dev_err(pTAS2559->dev,
1186                                         "request_irq failed, %d\n", nResult);
1187                                 goto err;
1188                         }
1189
1190                         disable_irq_nosync(pTAS2559->mnDevBIRQ);
1191                 } else
1192                         pTAS2559->mnDevBIRQ = pTAS2559->mnDevAIRQ;
1193         }
1194
1195         if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)
1196             || gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
1197                 INIT_DELAYED_WORK(&pTAS2559->irq_work, irq_work_routine);
1198         }
1199
1200         pTAS2559->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
1201
1202         if (!pTAS2559->mpFirmware) {
1203                 dev_err(&pClient->dev, "mpFirmware ENOMEM\n");
1204                 nResult = -ENOMEM;
1205                 goto err;
1206         }
1207
1208         pTAS2559->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
1209
1210         if (!pTAS2559->mpCalFirmware) {
1211                 dev_err(&pClient->dev, "mpCalFirmware ENOMEM\n");
1212                 nResult = -ENOMEM;
1213                 goto err;
1214         }
1215
1216 #ifdef CONFIG_TAS2559_CODEC
1217         mutex_init(&pTAS2559->codec_lock);
1218         tas2559_register_codec(pTAS2559);
1219 #endif
1220
1221 #ifdef CONFIG_TAS2559_MISC
1222         mutex_init(&pTAS2559->file_lock);
1223         tas2559_register_misc(pTAS2559);
1224 #endif
1225
1226 #ifdef ENABLE_TILOAD
1227         tiload_driver_init(pTAS2559);
1228 #endif
1229
1230         hrtimer_init(&pTAS2559->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1231         pTAS2559->mtimer.function = temperature_timer_func;
1232         INIT_WORK(&pTAS2559->mtimerwork, timer_work_routine);
1233
1234         if (get_hw_version_platform() == HARDWARE_PLATFORM_CHIRON_S)
1235                 fw_name = TAS2559_S_FW_NAME;
1236         else
1237                 fw_name = TAS2559_FW_NAME;
1238
1239         dev_err(&pClient->dev, "Firmware file name is %s\n", fw_name);
1240         nResult = request_firmware_nowait(THIS_MODULE, 1, fw_name,
1241                                           pTAS2559->dev, GFP_KERNEL, pTAS2559, tas2559_fw_ready);
1242
1243 err:
1244
1245         return nResult;
1246 }
1247
1248 static int tas2559_i2c_remove(struct i2c_client *pClient)
1249 {
1250         struct tas2559_priv *pTAS2559 = i2c_get_clientdata(pClient);
1251
1252         dev_info(pTAS2559->dev, "%s\n", __func__);
1253
1254 #ifdef CONFIG_TAS2559_CODEC
1255         tas2559_deregister_codec(pTAS2559);
1256         mutex_destroy(&pTAS2559->codec_lock);
1257 #endif
1258
1259 #ifdef CONFIG_TAS2559_MISC
1260         tas2559_deregister_misc(pTAS2559);
1261         mutex_destroy(&pTAS2559->file_lock);
1262 #endif
1263
1264         mutex_destroy(&pTAS2559->dev_lock);
1265         return 0;
1266 }
1267
1268 static const struct i2c_device_id tas2559_i2c_id[] = {
1269         {"tas2559", 0},
1270         {}
1271 };
1272
1273 MODULE_DEVICE_TABLE(i2c, tas2559_i2c_id);
1274
1275 #if defined(CONFIG_OF)
1276 static const struct of_device_id tas2559_of_match[] = {
1277         {.compatible = "ti,tas2559"},
1278         {},
1279 };
1280
1281 MODULE_DEVICE_TABLE(of, tas2559_of_match);
1282 #endif
1283
1284 static struct i2c_driver tas2559_i2c_driver = {
1285         .driver = {
1286                 .name = "tas2559",
1287                 .owner = THIS_MODULE,
1288 #if defined(CONFIG_OF)
1289                 .of_match_table = of_match_ptr(tas2559_of_match),
1290 #endif
1291         },
1292         .probe = tas2559_i2c_probe,
1293         .remove = tas2559_i2c_remove,
1294         .id_table = tas2559_i2c_id,
1295 };
1296
1297 module_i2c_driver(tas2559_i2c_driver);
1298
1299 MODULE_AUTHOR("Texas Instruments Inc.");
1300 MODULE_DESCRIPTION("TAS2559 Stereo I2C Smart Amplifier driver");
1301 MODULE_LICENSE("GPL v2");