e016df22f087e105d871e1e7f901d3f377f89147
[tas256xsw-android/tas2563-android-driver.git] / tas2563-regmap.c
1 /*
2 ** =============================================================================
3 ** Copyright (c) 2016  Texas Instruments Inc.
4 **
5 ** This program is free software; you can redistribute it and/or modify it under
6 ** the terms of the GNU General Public License as published by the Free Software
7 ** Foundation; version 2.
8 **
9 ** This program is distributed in the hope that it will be useful, but WITHOUT
10 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 **
13 ** File:
14 **     tas2563-regmap.c
15 **
16 ** Description:
17 **     I2C driver with regmap for Texas Instruments TAS2563 High Performance 4W Smart Amplifier
18 **
19 ** =============================================================================
20 */
22 #ifdef CONFIG_TAS2563_REGMAP
24 #define DEBUG
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/i2c.h>
31 #include <linux/gpio.h>
32 #include <linux/interrupt.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/firmware.h>
35 #include <linux/regmap.h>
36 #include <linux/of.h>
37 #include <linux/of_gpio.h>
38 #include <linux/slab.h>
39 #include <linux/syscalls.h>
40 #include <linux/fcntl.h>
41 #include <linux/uaccess.h>
42 #include "tas2563.h"
44 #ifdef CONFIG_TAS2563_CODEC
45 #include "tas2563-codec.h"
46 #endif
48 #ifdef CONFIG_TAS2563_MISC
49 #include "tas2563-misc.h"
50 #endif
52 #define ENABLE_TILOAD
53 #ifdef ENABLE_TILOAD
54 #include "tiload.h"
55 #endif
57 #define LOW_TEMPERATURE_GAIN 6
58 #define LOW_TEMPERATURE_COUNTER 12
59 static char pICN[] = {0x00, 0x00, 0x2f, 0x2c};
60 static char pICNDelay[] = {0x00, 0x00, 0x70, 0x80};
62 static int tas2563_change_book_page(struct tas2563_priv *pTAS2563,
63         int book, int page)
64 {
65         int nResult = 0;
66         dev_dbg(pTAS2563->dev, "%s, %d", __func__, __LINE__);
68         if ((pTAS2563->mnCurrentBook == book)
69                 && (pTAS2563->mnCurrentPage == page))
70                 goto end;
72         if (pTAS2563->mnCurrentBook != book) {
73         nResult = regmap_write(pTAS2563->regmap, TAS2563_BOOKCTL_PAGE, 0);
74                 if (nResult < 0) {
75                         dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
76                                 __func__, __LINE__, nResult);
77                         goto end;
78                 }
79         pTAS2563->mnCurrentPage = 0;
80         nResult = regmap_write(pTAS2563->regmap, TAS2563_BOOKCTL_REG, book);
81                 if (nResult < 0) {
82                         dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
83                                 __func__, __LINE__, nResult);
84                         goto end;
85                 }
86                 pTAS2563->mnCurrentBook = book;
87         }
89         if (pTAS2563->mnCurrentPage != page) {
90         nResult = regmap_write(pTAS2563->regmap, TAS2563_BOOKCTL_PAGE, page);
91                 if (nResult < 0) {
92                         dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
93                                 __func__, __LINE__, nResult);
94                         goto end;
95                 }
96                 pTAS2563->mnCurrentPage = page;
97         }
99 end:
100         return nResult;
103 static int tas2563_dev_read(struct tas2563_priv *pTAS2563,
104         unsigned int reg, unsigned int *pValue)
106         int nResult = 0;
108         mutex_lock(&pTAS2563->dev_lock);
110         nResult = tas2563_change_book_page(pTAS2563,
111                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
112         if (nResult < 0)
113                 goto end;
115         nResult = regmap_read(pTAS2563->regmap, TAS2563_PAGE_REG(reg), pValue);
116         if (nResult < 0)
117                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
118                         __func__, __LINE__, nResult);
119         else
120                 dev_dbg(pTAS2563->dev,
121                         "%s: BOOK:PAGE:REG 0x%02x:0x%02x:0x%02x,0x%02x\n", 
122                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
123                         TAS2563_PAGE_REG(reg), *pValue);
125 end:
126         mutex_unlock(&pTAS2563->dev_lock);
127         return nResult;
130 static int tas2563_dev_write(struct tas2563_priv *pTAS2563,
131         unsigned int reg, unsigned int value)
133         int nResult = 0;
135         mutex_lock(&pTAS2563->dev_lock);
137         nResult = tas2563_change_book_page(pTAS2563,
138                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
139         if (nResult < 0)
140                 goto end;
142         nResult = regmap_write(pTAS2563->regmap, TAS2563_PAGE_REG(reg),
143                         value);
144         if (nResult < 0)
145                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
146                         __func__, __LINE__, nResult);
147         else
148                 dev_dbg(pTAS2563->dev,
149                         "%s: BOOK:PAGE:REG 0x%02x:0x%02x:0x%02x, VAL: 0x%02x\n",
150                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
151                         TAS2563_PAGE_REG(reg), value);
153 end:
154         mutex_unlock(&pTAS2563->dev_lock);
155         return nResult;
158 static int tas2563_dev_bulk_write(struct tas2563_priv *pTAS2563,
159         unsigned int reg, u8 *pData, unsigned int nLength)
161         int nResult = 0;
163         mutex_lock(&pTAS2563->dev_lock);
165         nResult = tas2563_change_book_page(pTAS2563,
166                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
167         if (nResult < 0)
168                 goto end;
170         nResult = regmap_bulk_write(pTAS2563->regmap,
171                 TAS2563_PAGE_REG(reg), pData, nLength);
172         if (nResult < 0)
173                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
174                         __func__, __LINE__, nResult);
175         else
176                 dev_dbg(pTAS2563->dev,
177                         "%s: BOOK:PAGE:REG 0x%02x:0x%02x:0x%02x, len: 0x%02x\n",
178                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
179                         TAS2563_PAGE_REG(reg), nLength);
181 end:
182         mutex_unlock(&pTAS2563->dev_lock);
183         return nResult;
186 static int tas2563_dev_bulk_read(struct tas2563_priv *pTAS2563,
187         unsigned int reg, u8 *pData, unsigned int nLength)
189         int nResult = 0;
190         int i = 0;
192         mutex_lock(&pTAS2563->dev_lock);
194         nResult = tas2563_change_book_page(pTAS2563,
195                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
196         if (nResult < 0)
197                 goto end;
199         dev_dbg(pTAS2563->dev,
200                 "reg = %u, pData = %s, Lenth = %d", reg, pData, nLength);
202         #define STRIDE 4
203         /* Read chunk bytes defined by STRIDE */
204         for (i = 0; i < (nLength / STRIDE); i++) {
205                         nResult = regmap_bulk_read(pTAS2563->regmap,
206                                                         TAS2563_PAGE_REG((reg + i*STRIDE)),
207                                                         &pData[i*STRIDE], STRIDE);
208                         if (nResult < 0) {
209                                         dev_err(pTAS2563->dev,
210                                                 "%s, %d, I2C error %d\n",
211                                                         __func__, __LINE__, nResult);
212                                         pTAS2563->mnErrCode |= ERROR_DEVA_I2C_COMM;
213                         } else
214                                         pTAS2563->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
215         }
217         /* Read remaining bytes */
218         if ((nLength % STRIDE) != 0) {
219                         nResult = regmap_bulk_read(pTAS2563->regmap,
220                                                         TAS2563_PAGE_REG(reg + i*STRIDE),
221                                                         &pData[i*STRIDE], (nLength % STRIDE));
222                         if (nResult < 0) {
223                                         dev_err(pTAS2563->dev,
224                                                 "%s, %d, I2C error %d\n",
225                                                         __func__, __LINE__, nResult);
226                                         pTAS2563->mnErrCode |= ERROR_DEVA_I2C_COMM;
227                         } else
228                                         pTAS2563->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
229         }
232 end:
233         mutex_unlock(&pTAS2563->dev_lock);
234         return nResult;
237 static int tas2563_dev_update_bits(struct tas2563_priv *pTAS2563,
238         unsigned int reg, unsigned int mask, unsigned int value)
240         int nResult = 0;
242         mutex_lock(&pTAS2563->dev_lock);
243         nResult = tas2563_change_book_page(pTAS2563,
244                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
245         if (nResult < 0)
246                 goto end;
248         nResult = regmap_update_bits(pTAS2563->regmap,
249         TAS2563_PAGE_REG(reg), mask, value);
250         if (nResult < 0)
251                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
252                         __func__, __LINE__, nResult);
253         else
254                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG 0x%02x:0x%02x:0x%02x, mask: 0x%02x, val=0x%02x\n",
255                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
256                         TAS2563_PAGE_REG(reg), mask, value);
257 end:
258         mutex_unlock(&pTAS2563->dev_lock);
259         return nResult;
262 static const struct reg_default tas2563_reg_defaults[] = {
263         { TAS2563_Page, 0x00 },
264         { TAS2563_SoftwareReset, 0x00 },
265         { TAS2563_PowerControl, 0x0e },
266         { TAS2563_PlaybackConfigurationReg0, 0x10 },
267         { TAS2563_MiscConfigurationReg0, 0x07 },
268         { TAS2563_TDMConfigurationReg1, 0x02 },
269         { TAS2563_TDMConfigurationReg2, 0x0a },
270         { TAS2563_TDMConfigurationReg3, 0x10 },
271         { TAS2563_InterruptMaskReg0, 0xfc },
272         { TAS2563_InterruptMaskReg1, 0xb1 },
273         { TAS2563_InterruptConfiguration, 0x1d },
274         { TAS2563_MiscIRQ, 0x81 },
275         { TAS2563_ClockConfiguration, 0x0c },
277 };
279 static bool tas2563_volatile(struct device *dev, unsigned int reg)
281         return true;
284 static bool tas2563_writeable(struct device *dev, unsigned int reg)
286         return true;
288 static const struct regmap_config tas2563_i2c_regmap = {
289         .reg_bits = 8,
290         .val_bits = 8,
291         .writeable_reg = tas2563_writeable,
292         .volatile_reg = tas2563_volatile,
293 //      .reg_defaults = tas2563_reg_defaults,
294 //      .num_reg_defaults = ARRAY_SIZE(tas2563_reg_defaults),
295         .cache_type = REGCACHE_NONE,
296         .max_register = 1 * 128,
297 };
300 static void tas2563_hw_reset(struct tas2563_priv *pTAS2563)
302         if (gpio_is_valid(pTAS2563->mnResetGPIO)) {
303                 gpio_direction_output(pTAS2563->mnResetGPIO, 0);
304                 msleep(5);
305                 gpio_direction_output(pTAS2563->mnResetGPIO, 1);
306                 msleep(2);
307         }
309         pTAS2563->mnCurrentBook = -1;
310         pTAS2563->mnCurrentPage = -1;
313 void tas2563_enableIRQ(struct tas2563_priv *pTAS2563, bool enable)
315         if (enable) {
316                 if (pTAS2563->mbIRQEnable)
317                         return;
318 #ifdef CONFIG_TAS2563_ENABLE_IRQ
319                 if (gpio_is_valid(pTAS2563->mnIRQGPIO))
320                         enable_irq(pTAS2563->mnIRQ);
321 #endif
322                 schedule_delayed_work(&pTAS2563->irq_work, msecs_to_jiffies(10));
323                 pTAS2563->mbIRQEnable = true;
324         } else {
325 #ifdef CONFIG_TAS2563_ENABLE_IRQ
326                 if (gpio_is_valid(pTAS2563->mnIRQGPIO))
327                         disable_irq_nosync(pTAS2563->mnIRQ);
328 #endif
329                 pTAS2563->mbIRQEnable = false;
330         }
334 static void irq_work_routine(struct work_struct *work)
336         struct tas2563_priv *pTAS2563 =
337                 container_of(work, struct tas2563_priv, irq_work.work);
338         unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
339         int nCounter = 2;
340         int nResult = 0;
341         int irqreg;
343         dev_info(pTAS2563->dev, "%s\n", __func__);
344 #ifdef CONFIG_TAS2563_CODEC
345         mutex_lock(&pTAS2563->codec_lock);
346 #endif
348         if (pTAS2563->mbRuntimeSuspend) {
349                 dev_info(pTAS2563->dev, "%s, Runtime Suspended\n", __func__);
350                 goto end;
351         }
353         if (pTAS2563->mnPowerState == TAS2563_POWER_SHUTDOWN) {
354                 dev_info(pTAS2563->dev, "%s, device not powered\n", __func__);
355                 goto end;
356         }
358         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg0,
359                                 TAS2563_InterruptMaskReg0_Disable);
360         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg1,
361                                 TAS2563_InterruptMaskReg1_Disable);
363         if (nResult < 0)
364                 goto reload;
366         nResult = tas2563_dev_read(pTAS2563, TAS2563_LatchedInterruptReg0, &nDevInt1Status);
367         if (nResult >= 0)
368                 nResult = tas2563_dev_read(pTAS2563, TAS2563_LatchedInterruptReg1, &nDevInt2Status);
369         else
370                 goto reload;
372         dev_info(pTAS2563->dev, "IRQ status : 0x%x, 0x%x\n",
373                         nDevInt1Status, nDevInt2Status);
375         if (((nDevInt1Status & 0x7) != 0) || ((nDevInt2Status & 0x0f) != 0)) {
376                 /* in case of INT_OC, INT_OT, INT_OVLT, INT_UVLT, INT_BO */
378                 if (nDevInt1Status & TAS2563_LatchedInterruptReg0_OCEFlagSticky_Interrupt) {
379                         pTAS2563->mnErrCode |= ERROR_OVER_CURRENT;
380                         dev_err(pTAS2563->dev, "SPK over current!\n");
381                 } else
382                         pTAS2563->mnErrCode &= ~ERROR_OVER_CURRENT;
384                 if (nDevInt1Status & TAS2563_LatchedInterruptReg0_OTEFlagSticky_Interrupt) {
385                         pTAS2563->mnErrCode |= ERROR_DIE_OVERTEMP;
386                         dev_err(pTAS2563->dev, "die over temperature!\n");
387                 } else
388                         pTAS2563->mnErrCode &= ~ERROR_DIE_OVERTEMP;
390                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_VBATOVLOSticky_Interrupt) {
391                         pTAS2563->mnErrCode |= ERROR_OVER_VOLTAGE;
392                         dev_err(pTAS2563->dev, "SPK over voltage!\n");
393                 } else
394                         pTAS2563->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
396                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_VBATUVLOSticky_Interrupt) {
397                         pTAS2563->mnErrCode |= ERROR_UNDER_VOLTAGE;
398                         dev_err(pTAS2563->dev, "SPK under voltage!\n");
399                 } else
400                         pTAS2563->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
402                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_BrownOutFlagSticky_Interrupt) {
403                         pTAS2563->mnErrCode |= ERROR_BROWNOUT;
404                         dev_err(pTAS2563->dev, "brownout!\n");
405                 } else
406                         pTAS2563->mnErrCode &= ~ERROR_BROWNOUT;
408                 goto reload;
409         } else {
410                 nCounter = 2;
412                 while (nCounter > 0) {
413                         nResult = tas2563_dev_read(pTAS2563, TAS2563_PowerControl, &nDevInt1Status);
414                         if (nResult < 0)
415                                 goto reload;
417                         pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
418                         dev_info(pTAS2563->dev, "IRQ reg is: %s, %d, %d\n", __func__, irqreg, __LINE__);
419 #ifdef CONFIG_TAS2563_STANDALONE_RECORD
420                         if (((nDevInt1Status & TAS2563_PowerControl_PDM_I2S_MODE_Mask)
421                                 != TAS2563_PowerControl_PDM_I2S_MODE_Disable)&&(pTAS2563->cstream == 1 && pTAS2563->pstream ==0)) {
422                                 dev_info(pTAS2563->dev, "%s %u\n", __func__, __LINE__);
423                                 break;          
424                         }
426                         if ((((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
427                                 != TAS2563_PowerControl_OperationalMode10_Shutdown)
428                                 ||((nDevInt1Status & TAS2563_PowerControl_PDM_I2S_MODE_Mask)
429                                 != TAS2563_PowerControl_PDM_I2S_MODE_Disable))&&(pTAS2563->pstream ==1)) {
430                                 dev_info(pTAS2563->dev, "%s %u\n", __func__, __LINE__);
431                                 break;          
432                         }
435                                 if (pTAS2563->pstream == 1) {
436                                         dev_info(pTAS2563->dev, "%s %u\n", __func__, __LINE__);
437                                         nResult = pTAS2563->update_bits(pTAS2563, TAS2563_PowerControl,
438                                                 TAS2563_PowerControl_PDM_I2S_MODE_Mask |
439                                                 TAS2563_PowerControl_OperationalMode10_Mask |
440                                                 TAS2563_PowerControl_ISNSPower_Mask |
441                                                 TAS2563_PowerControl_VSNSPower_Mask,
442                                                 TAS2563_PowerControl_PDM_I2S_MODE_Disable |
443                                                 TAS2563_PowerControl_OperationalMode10_Active |
444                                                 TAS2563_PowerControl_VSNSPower_Active |
445                                                 TAS2563_PowerControl_ISNSPower_Active);
446                                         if (nResult < 0)
447                                                 goto reload;
448                                 } else if (pTAS2563->cstream == 1 && pTAS2563->pstream ==0) {
449                                 dev_info(pTAS2563->dev, "%s %u\n", __func__, __LINE__);
450                                         nResult = pTAS2563->update_bits(pTAS2563, TAS2563_PowerControl,
451                                                 TAS2563_PowerControl_PDM_I2S_MODE_Mask | 
452                                                 TAS2563_PowerControl_OperationalMode10_Mask |
453                                                 TAS2563_PowerControl_ISNSPower_Mask |
454                                                 TAS2563_PowerControl_VSNSPower_Mask,
455                                                 TAS2563_PowerControl_PDM_I2S_MODE_Enable |
456                                                 TAS2563_PowerControl_OperationalMode10_Active |
457                                                 TAS2563_PowerControl_VSNSPower_PoweredDown |
458                                                 TAS2563_PowerControl_ISNSPower_PoweredDown);
459                                         if (nResult < 0)
460                                                 goto reload;
461                                 }
462 #else
463            if ((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
464                                 != TAS2563_PowerControl_OperationalMode10_Shutdown) {
465                                 break;
466                         }
467                         nResult = pTAS2563->update_bits(pTAS2563, TAS2563_PowerControl,
468                                 TAS2563_PowerControl_OperationalMode10_Mask |
469                                 TAS2563_PowerControl_ISNSPower_Mask |
470                                 TAS2563_PowerControl_VSNSPower_Mask,
471                                 TAS2563_PowerControl_OperationalMode10_Active |
472                                 TAS2563_PowerControl_VSNSPower_Active |
473                                 TAS2563_PowerControl_ISNSPower_Active);
474                         if (nResult < 0)
475                                 goto reload;
476 #endif
477                                 pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
478                                 dev_info(pTAS2563->dev, "IRQ reg is: %s, %d, %d\n", __func__, irqreg, __LINE__);
480                                 dev_info(pTAS2563->dev, "set ICN to -90dB\n");
481                                 nResult = pTAS2563->bulk_write(pTAS2563, TAS2563_ICN_REG, pICN, 4);
482                                 if(nResult < 0)
483                                         goto reload;
485                                 pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
486                                 dev_info(pTAS2563->dev, "IRQ reg is: %d, %d\n", irqreg, __LINE__);
488                                 dev_info(pTAS2563->dev, "set ICN delay\n");
489                                 nResult = pTAS2563->bulk_write(pTAS2563, TAS2563_ICN_DELAY, pICNDelay, 4);
491                                 pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
492                                 dev_info(pTAS2563->dev, "IRQ reg is: %d, %d\n", irqreg, __LINE__);
494                                 nCounter--;
495                                 if (nCounter > 0) {
496                                         /* in case check power status just after power on TAS2563 */
497                                         dev_dbg(pTAS2563->dev, "PowSts B: 0x%x, check again after 10ms\n",
498                                                 nDevInt1Status);
499                                         msleep(20);
500                         }
501                 }
503                 if ((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
504                         == TAS2563_PowerControl_OperationalMode10_Shutdown) {
505                         dev_err(pTAS2563->dev, "%s, Critical ERROR REG[0x%x] = 0x%x\n",
506                                 __func__,
507                                 TAS2563_PowerControl,
508                                 nDevInt1Status);
509                         pTAS2563->mnErrCode |= ERROR_CLASSD_PWR;
510                         goto reload;
511                 }
512                 pTAS2563->mnErrCode &= ~ERROR_CLASSD_PWR;
513         }
515         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg0, 0xf8);
516         if (nResult < 0)
517                 goto reload;
519         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg1, 0xb1);
520         if (nResult < 0)
521                 goto reload;
523         goto end;
525 reload:
526         /* hardware reset and reload */
527         nResult = -1;
528         //tas2563_LoadConfig(pTAS2563);
529         tas2563_set_program(pTAS2563, pTAS2563->mnCurrentProgram, pTAS2563->mnCurrentConfiguration);
531 end:
532         if (nResult >= 0) {
533                 tas2563_enableIRQ(pTAS2563, true);
534         }
536 #ifdef CONFIG_TAS2563_CODEC
537         mutex_unlock(&pTAS2563->codec_lock);
538 #endif
541 static enum hrtimer_restart timer_func(struct hrtimer *timer)
543         struct tas2563_priv *pTAS2563 = container_of(timer,
544                 struct tas2563_priv, mtimerwork);
546         if (pTAS2563->mnPowerState != TAS2563_POWER_SHUTDOWN) {
547                 if (!delayed_work_pending(&pTAS2563->irq_work))
548                         schedule_delayed_work(&pTAS2563->irq_work,
549                                 msecs_to_jiffies(20));
550         }
552         return HRTIMER_NORESTART;
555 static irqreturn_t tas2563_irq_handler(int irq, void *dev_id)
557         struct tas2563_priv *pTAS2563 = (struct tas2563_priv *)dev_id;
559         tas2563_enableIRQ(pTAS2563, false);
560         /* get IRQ status after 100 ms */
561         schedule_delayed_work(&pTAS2563->irq_work, msecs_to_jiffies(100));
562         return IRQ_HANDLED;
566 static int tas2563_parse_dt(struct device *dev, struct tas2563_priv *pTAS2563)
568         struct device_node *np = dev->of_node;
569         int rc = 0, ret = 0;
571         rc = of_property_read_u32(np, "ti,asi-format", &pTAS2563->mnASIFormat);
572         if (rc) {
573                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
574                         "ti,asi-format", np->full_name, rc);
575         } else {
576                 dev_dbg(pTAS2563->dev, "ti,asi-format=%d",
577                         pTAS2563->mnASIFormat);
578         }
580         pTAS2563->mnResetGPIO = of_get_named_gpio(np, "ti,reset-gpio", 0);
581         if (!gpio_is_valid(pTAS2563->mnResetGPIO)) {
582                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
583                         "ti,reset-gpio", np->full_name, pTAS2563->mnResetGPIO);
584         } else {
585                 dev_dbg(pTAS2563->dev, "ti,reset-gpio=%d",
586                         pTAS2563->mnResetGPIO);
587         }
589         pTAS2563->mnIRQGPIO = of_get_named_gpio(np, "ti,irq-gpio", 0);
590         if (!gpio_is_valid(pTAS2563->mnIRQGPIO)) {
591                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
592                         "ti,irq-gpio", np->full_name, pTAS2563->mnIRQGPIO);
593         } else {
594                 dev_dbg(pTAS2563->dev, "ti,irq-gpio=%d", pTAS2563->mnIRQGPIO);
595         }
597         of_property_read_u32(np, "ti,left-slot", &pTAS2563->mnLeftSlot);
598         if (rc) {
599                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
600                         "ti,left-slot", np->full_name, rc);
601         } else {
602                 dev_dbg(pTAS2563->dev, "ti,left-slot=%d",
603                         pTAS2563->mnLeftSlot);
604         }
606         of_property_read_u32(np, "ti,right-slot", &pTAS2563->mnRightSlot);
607         if (rc) {
608                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
609                         "ti,right-slot", np->full_name, rc);
610         } else {
611                 dev_dbg(pTAS2563->dev, "ti,right-slot=%d",
612                         pTAS2563->mnRightSlot);
613         }
615         of_property_read_u32(np, "ti,imon-slot-no", &pTAS2563->mnImon_slot_no);
616         if (rc) {
617                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
618                         "ti,imon-slot-no", np->full_name, rc);
619         } else {
620                 dev_dbg(pTAS2563->dev, "ti,imon-slot-no=%d",
621                         pTAS2563->mnImon_slot_no);
622         }
624         of_property_read_u32(np, "ti,vmon-slot-no", &pTAS2563->mnVmon_slot_no);
625         if (rc) {
626                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
627                         "ti,vmon-slot-no", np->full_name, rc);
628         } else {
629                 dev_dbg(pTAS2563->dev, "ti,vmon-slot-no=%d",
630                         pTAS2563->mnVmon_slot_no);
631         }
633         return ret;
637 static int tas2563_runtime_suspend(struct tas2563_priv *pTAS2563)
639         dev_dbg(pTAS2563->dev, "%s\n", __func__);
641         pTAS2563->mbRuntimeSuspend = true;
642 #ifdef CONFIG_TAS2563_ENABLE_IRQ
643         if (gpio_is_valid(pTAS2563->mnIRQGPIO)) {
644                 if (delayed_work_pending(&pTAS2563->irq_work)) {
645                         dev_dbg(pTAS2563->dev, "cancel IRQ work\n");
646                         cancel_delayed_work_sync(&pTAS2563->irq_work);
647                 }
648         }
649 #endif
650         return 0;
653 static int tas2563_runtime_resume(struct tas2563_priv *pTAS2563)
655         struct TProgram *pProgram;
657         dev_dbg(pTAS2563->dev, "%s\n", __func__);
658         if (!pTAS2563->mpFirmware->mpPrograms) {
659                 dev_dbg(pTAS2563->dev, "%s, firmware not loaded\n", __func__);
660                 goto end;
661         }
663         if (pTAS2563->mnCurrentProgram >= pTAS2563->mpFirmware->mnPrograms) {
664                 dev_err(pTAS2563->dev, "%s, firmware corrupted\n", __func__);
665                 goto end;
666         }
668         pProgram = &(pTAS2563->mpFirmware->mpPrograms[pTAS2563->mnCurrentProgram]);
670         pTAS2563->mbRuntimeSuspend = false;
671 end:
673         return 0;
677 /* tas2563_i2c_probe :
678 * platform dependent
679 * should implement hardware reset functionality
680 */
681 static int tas2563_i2c_probe(struct i2c_client *pClient,
682         const struct i2c_device_id *pID)
684         struct tas2563_priv *pTAS2563;
685         int nResult = 0;
686         unsigned int nValue = 0;
687         const char *pFWName;
689         dev_err(&pClient->dev, "Driver ID: %s\n", TAS2563_DRIVER_ID);
690         dev_info(&pClient->dev, "%s enter\n", __func__);
692         pTAS2563 = devm_kzalloc(&pClient->dev, sizeof(struct tas2563_priv), GFP_KERNEL);
693         if (!pTAS2563) {
694                 nResult = -ENOMEM;
695                 goto err;
696         }
698         pTAS2563->dev = &pClient->dev;
699         i2c_set_clientdata(pClient, pTAS2563);
700         dev_set_drvdata(&pClient->dev, pTAS2563);
702         pTAS2563->regmap = devm_regmap_init_i2c(pClient, &tas2563_i2c_regmap);
703         if (IS_ERR(pTAS2563->regmap)) {
704                 nResult = PTR_ERR(pTAS2563->regmap);
705                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
706                         nResult);
707                 goto err;
708         }
710         if (pClient->dev.of_node)
711                 tas2563_parse_dt(&pClient->dev, pTAS2563);
713         if (gpio_is_valid(pTAS2563->mnResetGPIO)) {
714                 nResult = gpio_request(pTAS2563->mnResetGPIO, "TAS2563-RESET");
715                 if (nResult < 0) {
716                         dev_err(pTAS2563->dev, "%s: GPIO %d request error\n",
717                                 __func__, pTAS2563->mnResetGPIO);
718                         goto err;
719                 }
720                 tas2563_hw_reset(pTAS2563);
721         }
723         pTAS2563->read = tas2563_dev_read;
724         pTAS2563->write = tas2563_dev_write;
725         pTAS2563->bulk_read = tas2563_dev_bulk_read;
726         pTAS2563->bulk_write = tas2563_dev_bulk_write;
727         pTAS2563->update_bits = tas2563_dev_update_bits;
728         pTAS2563->enableIRQ = tas2563_enableIRQ;
729 //      pTAS2563->clearIRQ = tas2563_clearIRQ;
730         pTAS2563->hw_reset = tas2563_hw_reset;
731         pTAS2563->runtime_suspend = tas2563_runtime_suspend;
732         pTAS2563->runtime_resume = tas2563_runtime_resume;
733         pTAS2563->mnRestart = 0;
734         pTAS2563->mnPowerState = TAS2563_POWER_SHUTDOWN;
736         mutex_init(&pTAS2563->dev_lock);
738         /* Reset the chip */
739         nResult = tas2563_dev_write(pTAS2563, TAS2563_SoftwareReset, 0x01);
740         if (nResult < 0) {
741                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
742 //              goto err;
743         }
745         msleep(1);
746         nResult = tas2563_dev_read(pTAS2563, TAS2563_RevisionandPGID, &nValue);
747         pTAS2563->mnPGID = nValue;
748         dev_info(pTAS2563->dev, "PGID: %d\n", pTAS2563->mnPGID);
749         pFWName = TAS2563_FW_NAME;
751 #ifdef CONFIG_TAS2563_ENABLE_IRQ
752         if (gpio_is_valid(pTAS2563->mnIRQGPIO)) {
753                         nResult = gpio_request(pTAS2563->mnIRQGPIO, "TAS2563-IRQ");
754                         if (nResult < 0) {
755                                 dev_err(pTAS2563->dev,
756                                         "%s: GPIO %d request INT error\n",
757                                         __func__, pTAS2563->mnIRQGPIO);
758                                 goto err;
759                         }
761                         gpio_direction_input(pTAS2563->mnIRQGPIO);
762                         pTAS2563->mnIRQ = gpio_to_irq(pTAS2563->mnIRQGPIO);
763                         dev_dbg(pTAS2563->dev, "irq = %d\n", pTAS2563->mnIRQ);
764                         INIT_DELAYED_WORK(&pTAS2563->irq_work, irq_work_routine);
765                         nResult = request_threaded_irq(pTAS2563->mnIRQ, tas2563_irq_handler,
766                                                 NULL, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
767                                                 pClient->name, pTAS2563);
768                         if (nResult < 0) {
769                                 dev_err(pTAS2563->dev,
770                                         "request_irq failed, %d\n", nResult);
771                                 goto err;
772                         }
773                         disable_irq_nosync(pTAS2563->mnIRQ);
774         }
775 #else
776         INIT_DELAYED_WORK(&pTAS2563->irq_work, irq_work_routine);
777 #endif
778         pTAS2563->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
779         if (!pTAS2563->mpFirmware) {
780                 nResult = -ENOMEM;
781                 goto err;
782         }
784         pTAS2563->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
785         if (!pTAS2563->mpCalFirmware) {
786                 nResult = -ENOMEM;
787                 goto err;
788         }
790 #ifdef CONFIG_TAS2563_CODEC
791         mutex_init(&pTAS2563->codec_lock);
792         nResult = tas2563_register_codec(pTAS2563);
793         if (nResult < 0) {
794                 dev_err(pTAS2563->dev,
795                         "register codec failed, %d\n", nResult);
796                 goto err;
797         }
798 #endif
800 #ifdef CONFIG_TAS2563_MISC
801         mutex_init(&pTAS2563->file_lock);
802         nResult = tas2563_register_misc(pTAS2563);
803         if (nResult < 0) {
804                 dev_err(pTAS2563->dev,
805                         "register codec failed, %d\n", nResult);
806                 goto err;
807         }
808 #endif
810 #ifdef ENABLE_TILOAD
811         tiload_driver_init(pTAS2563);
812 #endif
814         hrtimer_init(&pTAS2563->mtimerwork, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
815         pTAS2563->mtimerwork.function = timer_func;
816         //INIT_WORK(&pTAS2563->mtimerwork, timer_func);
818         request_firmware_nowait(THIS_MODULE, 1, pFWName,
819                 pTAS2563->dev, GFP_KERNEL, pTAS2563, tas2563_fw_ready);
821 err:
823         return nResult;
826 static int tas2563_i2c_remove(struct i2c_client *pClient)
828         struct tas2563_priv *pTAS2563 = i2c_get_clientdata(pClient);
830         dev_info(pTAS2563->dev, "%s\n", __func__);
832 #ifdef CONFIG_TAS2563_CODEC
833         tas2563_deregister_codec(pTAS2563);
834         mutex_destroy(&pTAS2563->codec_lock);
835 #endif
837 #ifdef CONFIG_TAS2563_MISC
838         tas2563_deregister_misc(pTAS2563);
839         mutex_destroy(&pTAS2563->file_lock);
840 #endif
842         mutex_destroy(&pTAS2563->dev_lock);
843         return 0;
846 static const struct i2c_device_id tas2563_i2c_id[] = {
847         {"tas2563", 0},
848         {}
849 };
851 MODULE_DEVICE_TABLE(i2c, tas2563_i2c_id);
853 #if defined(CONFIG_OF)
854 static const struct of_device_id tas2563_of_match[] = {
855         {.compatible = "ti,tas2563"},
856         {},
857 };
859 MODULE_DEVICE_TABLE(of, tas2563_of_match);
860 #endif
862 static struct i2c_driver tas2563_i2c_driver = {
863         .driver = {
864                         .name = "tas2563",
865                         .owner = THIS_MODULE,
866 #if defined(CONFIG_OF)
867                         .of_match_table = of_match_ptr(tas2563_of_match),
868 #endif
869                 },
870         .probe = tas2563_i2c_probe,
871         .remove = tas2563_i2c_remove,
872         .id_table = tas2563_i2c_id,
873 };
875 module_i2c_driver(tas2563_i2c_driver);
877 MODULE_AUTHOR("Texas Instruments Inc.");
878 MODULE_DESCRIPTION("TAS2563 I2C Smart Amplifier driver");
879 MODULE_LICENSE("GPL v2");
881 #endif