]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tas2557sw-android/tas2557-android-driver.git/blob - tas2557-regmap.c
071e6a9cbc6d5a07fa79ac0245cff62c428873f8
[tas2557sw-android/tas2557-android-driver.git] / tas2557-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 **     tas2557-regmap.c
15 **
16 ** Description:
17 **     I2C driver with regmap for Texas Instruments TAS2557 High Performance 4W Smart Amplifier
18 **
19 ** =============================================================================
20 */
22 #ifdef CONFIG_TAS2557_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/regulator/consumer.h>
33 #include <linux/firmware.h>
34 #include <linux/regmap.h>
35 #include <linux/of.h>
36 #include <linux/of_gpio.h>
37 #include <linux/slab.h>
38 #include <linux/syscalls.h>
39 #include <linux/fcntl.h>
40 #include <linux/uaccess.h>
41 #include "tas2557.h"
42 #include "tas2557-core.h"
44 #ifdef CONFIG_TAS2557_CODEC
45 #include "tas2557-codec.h"
46 #endif
48 #ifdef CONFIG_TAS2557_MISC
49 #include "tas2557-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
60 static int tas2557_change_book_page(
61         struct tas2557_priv *pTAS2557,
62         unsigned char nBook,
63         unsigned char nPage)
64 {
65         int nResult = 0;
67         if ((pTAS2557->mnCurrentBook == nBook) 
68                 && pTAS2557->mnCurrentPage == nPage)
69                 goto end;
71         if (pTAS2557->mnCurrentBook != nBook) {
72                 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_PAGE, 0);
73                 if (nResult < 0) {
74                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
75                                 __func__, __LINE__, nResult);
76                         goto end;
77                 }
78                 pTAS2557->mnCurrentPage = 0;
79                 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_REG, nBook);
80                 if (nResult < 0) {
81                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
82                                 __func__, __LINE__, nResult);
83                         goto end;
84                 }
85                 pTAS2557->mnCurrentBook = nBook;
86                 if (nPage != 0) {
87                         nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_PAGE, nPage);
88                         if (nResult < 0) {
89                                 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
90                                         __func__, __LINE__, nResult);
91                                 goto end;
92                         }
93                         pTAS2557->mnCurrentPage = nPage;
94                 }
95         } else if (pTAS2557->mnCurrentPage != nPage) {
96                 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_PAGE, nPage);
97                 if (nResult < 0) {
98                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
99                                 __func__, __LINE__, nResult);
100                         goto end;
101                 }
102                 pTAS2557->mnCurrentPage = nPage;
103         }
105 end:
106         if (nResult < 0)
107                 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
108         else
109                 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
111         return nResult;
114 static int tas2557_dev_read(
115         struct tas2557_priv *pTAS2557,
116         unsigned int nRegister,
117         unsigned int *pValue)
119         int nResult = 0;
120         unsigned int Value = 0;
122         mutex_lock(&pTAS2557->dev_lock);
124         if (pTAS2557->mbTILoadActive) {
125                 if (!(nRegister & 0x80000000))
126                         goto end; /* let only reads from TILoad pass. */
127                 nRegister &= ~0x80000000;
129                 dev_dbg(pTAS2557->dev, "TiLoad R REG B[%d]P[%d]R[%d]\n",
130                                 TAS2557_BOOK_ID(nRegister),
131                                 TAS2557_PAGE_ID(nRegister),
132                                 TAS2557_PAGE_REG(nRegister));
133         }
135         nResult = tas2557_change_book_page(pTAS2557, 
136                                 TAS2557_BOOK_ID(nRegister),
137                                 TAS2557_PAGE_ID(nRegister));
138         if (nResult >= 0) {
139                 nResult = regmap_read(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), &Value);
140                 if (nResult < 0) {
141                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
142                                 __func__, __LINE__, nResult);
143                         pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
144                         goto end;
145                 } else
146                         pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
147                 *pValue = Value;
148         }
150 end:
152         mutex_unlock(&pTAS2557->dev_lock);
153         return nResult;
156 static int tas2557_dev_write(
157         struct tas2557_priv *pTAS2557,
158         unsigned int nRegister,
159         unsigned int nValue)
161         int nResult = 0;
163         mutex_lock(&pTAS2557->dev_lock);
164         if ((nRegister == 0xAFFEAFFE) && (nValue == 0xBABEBABE)) {
165                 pTAS2557->mbTILoadActive = true;
166                 goto end;
167         }
169         if ((nRegister == 0xBABEBABE) && (nValue == 0xAFFEAFFE)) {
170                 pTAS2557->mbTILoadActive = false;
171                 goto end;
172         }
174         if (pTAS2557->mbTILoadActive) {
175                 if (!(nRegister & 0x80000000))
176                         goto end;/* let only writes from TILoad pass. */
177                 nRegister &= ~0x80000000;
179                 dev_dbg(pTAS2557->dev, "TiLoad W REG B[%d]P[%d]R[%d] =0x%x\n",
180                                                 TAS2557_BOOK_ID(nRegister),
181                                                 TAS2557_PAGE_ID(nRegister),
182                                                 TAS2557_PAGE_REG(nRegister),
183                                                 nValue);
184         }
186         nResult = tas2557_change_book_page(pTAS2557,
187                                 TAS2557_BOOK_ID(nRegister),
188                                 TAS2557_PAGE_ID(nRegister));
189         if (nResult >= 0) {
190                 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), nValue);
191                 if (nResult < 0) {
192                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
193                                 __func__, __LINE__, nResult);
194                         pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
195                 } else
196                         pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
197         }
199 end:
201         mutex_unlock(&pTAS2557->dev_lock);
203         return nResult;
206 static int tas2557_dev_bulk_read(
207         struct tas2557_priv *pTAS2557,
208         unsigned int nRegister,
209         u8 *pData,
210         unsigned int nLength)
212         int nResult = 0;
214         mutex_lock(&pTAS2557->dev_lock);
215         if (pTAS2557->mbTILoadActive) {
216                 if (!(nRegister & 0x80000000))
217                         goto end; /* let only writes from TILoad pass. */
219                 nRegister &= ~0x80000000;
220                 dev_dbg(pTAS2557->dev, "TiLoad BR REG B[%d]P[%d]R[%d], count=%d\n",
221                                 TAS2557_BOOK_ID(nRegister),
222                                 TAS2557_PAGE_ID(nRegister),
223                                 TAS2557_PAGE_REG(nRegister),
224                                 nLength);
225         }
227         nResult = tas2557_change_book_page(pTAS2557,
228                                 TAS2557_BOOK_ID(nRegister),
229                                 TAS2557_PAGE_ID(nRegister));
230         if (nResult >= 0) {
231                 nResult = regmap_bulk_read(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), pData, nLength);
232                 if (nResult < 0) {
233                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
234                                 __func__, __LINE__, nResult);
235                         pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
236                 } else
237                         pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
238         }
240 end:
242         mutex_unlock(&pTAS2557->dev_lock);
243         return nResult;
246 static int tas2557_dev_bulk_write(
247         struct tas2557_priv *pTAS2557,
248         unsigned int nRegister,
249         u8 *pData,
250         unsigned int nLength)
252         int nResult = 0;
254         mutex_lock(&pTAS2557->dev_lock);
255         if (pTAS2557->mbTILoadActive) {
256                 if (!(nRegister & 0x80000000))
257                         goto end; /* let only writes from TILoad pass. */
259                 nRegister &= ~0x80000000;
261                 dev_dbg(pTAS2557->dev, "TiLoad BW REG B[%d]P[%d]R[%d], count=%d\n",
262                                 TAS2557_BOOK_ID(nRegister),
263                                 TAS2557_PAGE_ID(nRegister),
264                                 TAS2557_PAGE_REG(nRegister),
265                                 nLength);
266         }
268         nResult = tas2557_change_book_page( pTAS2557,
269                                 TAS2557_BOOK_ID(nRegister),
270                                 TAS2557_PAGE_ID(nRegister));
271         if (nResult >= 0) {
272                 nResult = regmap_bulk_write(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), pData, nLength);
273                 if (nResult < 0) {
274                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
275                                 __func__, __LINE__, nResult);
276                         pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
277                 } else
278                         pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
279         }
281 end:
283         mutex_unlock(&pTAS2557->dev_lock);
284         return nResult;
287 static int tas2557_dev_update_bits(
288         struct tas2557_priv *pTAS2557,
289         unsigned int nRegister,
290         unsigned int nMask,
291         unsigned int nValue)
293         int nResult = 0;
295         mutex_lock(&pTAS2557->dev_lock);
297         if (pTAS2557->mbTILoadActive) {
298                 if (!(nRegister & 0x80000000))
299                         goto end; /* let only writes from TILoad pass. */
301                 nRegister &= ~0x80000000;
302                 dev_dbg(pTAS2557->dev, "TiLoad SB REG B[%d]P[%d]R[%d], mask=0x%x, value=0x%x\n",
303                                 TAS2557_BOOK_ID(nRegister),
304                                 TAS2557_PAGE_ID(nRegister),
305                                 TAS2557_PAGE_REG(nRegister),
306                                 nMask, nValue);
307         }
309         nResult = tas2557_change_book_page( pTAS2557,
310                                 TAS2557_BOOK_ID(nRegister),
311                                 TAS2557_PAGE_ID(nRegister));
312         if (nResult >= 0) {
313                 nResult = regmap_update_bits(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), nMask, nValue);
314                 if (nResult < 0) {
315                         dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
316                                 __func__, __LINE__, nResult);
317                         pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
318                 } else
319                         pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
320         }
322 end:
323         mutex_unlock(&pTAS2557->dev_lock);
324         return nResult;
327 void tas2557_clearIRQ(struct tas2557_priv *pTAS2557)
329         unsigned int nValue;
330         int nResult = 0;
332         nResult = pTAS2557->read(pTAS2557, TAS2557_FLAGS_1, &nValue);
333         if (nResult >= 0)
334                 pTAS2557->read(pTAS2557, TAS2557_FLAGS_2, &nValue);
339 void tas2557_enableIRQ(struct tas2557_priv *pTAS2557, bool enable)
341         if (enable) {
342                 if (!pTAS2557->mbIRQEnable) {
343                         if (gpio_is_valid(pTAS2557->mnGpioINT)) {
344                                 enable_irq(pTAS2557->mnIRQ);
345                                 /* check after 10 ms */
346                                 schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(10));
347                                 pTAS2557->mbIRQEnable = true;
348                         }
349                 }
350         } else {
351                 if (pTAS2557->mbIRQEnable) {
352                         if (gpio_is_valid(pTAS2557->mnGpioINT))
353                                 disable_irq_nosync(pTAS2557->mnIRQ);
354                         pTAS2557->mbIRQEnable = false;
355                 }
356         }
359 static void tas2557_hw_reset(struct tas2557_priv *pTAS2557)
361         if (gpio_is_valid(pTAS2557->mnResetGPIO)) {
362                 gpio_direction_output(pTAS2557->mnResetGPIO, 0);
363                 msleep(5);
364                 gpio_direction_output(pTAS2557->mnResetGPIO, 1);
365                 msleep(2);
366         }
368         pTAS2557->mnCurrentBook = -1;
369         pTAS2557->mnCurrentPage = -1;
370         if (pTAS2557->mnErrCode)
371                 dev_info(pTAS2557->dev, "before reset, ErrCode=0x%x\n", pTAS2557->mnErrCode);
372         pTAS2557->mnErrCode = 0;
375 static void irq_work_routine(struct work_struct *work)
377         int nResult = 0;
378         unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
379         unsigned int nDevPowerUpFlag = 0;
380         struct tas2557_priv *pTAS2557 =
381                 container_of(work, struct tas2557_priv, irq_work.work);
383 #ifdef CONFIG_TAS2557_CODEC
384         mutex_lock(&pTAS2557->codec_lock);
385 #endif
387 #ifdef CONFIG_TAS2557_MISC
388         mutex_lock(&pTAS2557->file_lock);
389 #endif
391         if (!pTAS2557->mbPowerUp)
392                 goto end;
394         if ((!pTAS2557->mpFirmware->mnConfigurations)
395                 || (!pTAS2557->mpFirmware->mnPrograms)) {
396                 dev_info(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
397                 goto end;
398         }
400         nResult = tas2557_dev_read(pTAS2557, TAS2557_FLAGS_1, &nDevInt1Status);
401         if (nResult >= 0)
402                 nResult = tas2557_dev_read(pTAS2557, TAS2557_FLAGS_2, &nDevInt2Status);
404         if (nResult < 0)
405                 goto program;
407         if (((nDevInt1Status & 0xfc) != 0) || ((nDevInt2Status & 0x0c) != 0)) {
408                 /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
409                 dev_err(pTAS2557->dev, "critical error: 0x%x, 0x%x\n", nDevInt1Status, nDevInt2Status);
410                         if (nDevInt1Status & 0x80) {
411                                 pTAS2557->mnErrCode |= ERROR_OVER_CURRENT;
412                                 dev_err(pTAS2557->dev, "DEVA SPK over current!\n");
413                         } else
414                                 pTAS2557->mnErrCode &= ~ERROR_OVER_CURRENT;
416                         if (nDevInt1Status & 0x40) {
417                                 pTAS2557->mnErrCode |= ERROR_UNDER_VOLTAGE;
418                                 dev_err(pTAS2557->dev, "DEVA SPK under voltage!\n");
419                         } else
420                                 pTAS2557->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
422                         if (nDevInt1Status & 0x20) {
423                                 pTAS2557->mnErrCode |= ERROR_CLK_HALT;
424                                 dev_err(pTAS2557->dev, "DEVA clk halted!\n");
425                         } else
426                                 pTAS2557->mnErrCode &= ~ERROR_CLK_HALT;
428                         if (nDevInt1Status & 0x10) {
429                                 pTAS2557->mnErrCode |= ERROR_DIE_OVERTEMP;
430                                 dev_err(pTAS2557->dev, "DEVA die over temperature!\n");
431                         } else
432                                 pTAS2557->mnErrCode &= ~ERROR_DIE_OVERTEMP;
434                         if (nDevInt1Status & 0x08) {
435                                 pTAS2557->mnErrCode |= ERROR_BROWNOUT;
436                                 dev_err(pTAS2557->dev, "DEVA brownout!\n");
437                         } else
438                                 pTAS2557->mnErrCode &= ~ERROR_BROWNOUT;
440                         if (nDevInt1Status & 0x04) {
441                                 pTAS2557->mnErrCode |= ERROR_CLK_LOST;
442                                 dev_err(pTAS2557->dev, "DEVA clock lost!\n");
443                         } else
444                                 pTAS2557->mnErrCode &= ~ERROR_CLK_LOST;
446                         if (nDevInt2Status & 0x08) {
447                                 pTAS2557->mnErrCode |= ERROR_CLK_DET1;
448                                 dev_err(pTAS2557->dev, "DEVA clk detection 1!\n");
449                         } else
450                                 pTAS2557->mnErrCode &= ~ERROR_CLK_DET1;
452                         if (nDevInt2Status & 0x04) {
453                                 pTAS2557->mnErrCode |= ERROR_CLK_DET2;
454                                 dev_err(pTAS2557->dev, "DEVA clk detection 2!\n");
455                         } else
456                                 pTAS2557->mnErrCode &= ~ERROR_CLK_DET2;
458                 goto program;
459         } else {
460                 nResult = tas2557_dev_read(pTAS2557, TAS2557_POWER_UP_FLAG_REG, &nDevPowerUpFlag);
461                 if (nResult < 0)
462                         goto program;
463                 if ((nDevPowerUpFlag & 0xc0) != 0xc0) {
464                         dev_err(pTAS2557->dev, "%s, Critical ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
465                                 __func__,
466                                 TAS2557_BOOK_ID(TAS2557_POWER_UP_FLAG_REG),
467                                 TAS2557_PAGE_ID(TAS2557_POWER_UP_FLAG_REG),
468                                 TAS2557_PAGE_REG(TAS2557_POWER_UP_FLAG_REG),
469                                 nDevPowerUpFlag);
470                         pTAS2557->mnErrCode |= ERROR_CLASSD_PWR;
471                         goto program;
472                 }
473                 pTAS2557->mnErrCode &= ~ERROR_CLASSD_PWR;
475                 dev_dbg(pTAS2557->dev, "%s: INT1=0x%x, INT2=0x%x; PowerUpFlag=0x%x\n",
476                         __func__, nDevInt1Status, nDevInt2Status, nDevPowerUpFlag);
477                 goto end;
478         }
480 program:
481         /* hardware reset and reload */
482         tas2557_set_program(pTAS2557, pTAS2557->mnCurrentProgram, pTAS2557->mnCurrentConfiguration);
484 end:
486 #ifdef CONFIG_TAS2557_MISC
487         mutex_unlock(&pTAS2557->file_lock);
488 #endif
490 #ifdef CONFIG_TAS2557_CODEC
491         mutex_unlock(&pTAS2557->codec_lock);
492 #endif
495 static irqreturn_t tas2557_irq_handler(int irq, void *dev_id)
497         struct tas2557_priv *pTAS2557 = (struct tas2557_priv *)dev_id;
499         tas2557_enableIRQ(pTAS2557, false);
500         /* get IRQ status after 100 ms */
501         schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(100));
502         return IRQ_HANDLED;
505 static enum hrtimer_restart temperature_timer_func(struct hrtimer *timer)
507         struct tas2557_priv *pTAS2557 = container_of(timer, struct tas2557_priv, mtimer);
509         if (pTAS2557->mbPowerUp) {
510                 schedule_work(&pTAS2557->mtimerwork);
511                 schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(1));
512         }
513         return HRTIMER_NORESTART;
516 static void timer_work_routine(struct work_struct *work)
518         struct tas2557_priv *pTAS2557 = container_of(work, struct tas2557_priv, mtimerwork);
519         int nResult, nTemp, nActTemp;
520         struct TProgram *pProgram;
521         static int nAvg;
523 #ifdef CONFIG_TAS2557_CODEC
524         mutex_lock(&pTAS2557->codec_lock);
525 #endif
527 #ifdef CONFIG_TAS2557_MISC
528         mutex_lock(&pTAS2557->file_lock);
529 #endif
531         if (!pTAS2557->mpFirmware->mnConfigurations) {
532                 dev_info(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
533                 goto end;
534         }
536         pProgram = &(pTAS2557->mpFirmware->mpPrograms[pTAS2557->mnCurrentProgram]);
537         if (!pTAS2557->mbPowerUp
538                 || (pProgram->mnAppMode != TAS2557_APP_TUNINGMODE)) {
539                 dev_info(pTAS2557->dev, "%s, pass, Pow=%d, program=%s\n",
540                         __func__, pTAS2557->mbPowerUp, pProgram->mpName);
541                 goto end;
542         }
544         nResult = tas2557_get_die_temperature(pTAS2557, &nTemp);
545         if (nResult >= 0) {
546                 nActTemp = (int)(nTemp >> 23);
547                 dev_dbg(pTAS2557->dev, "Die=0x%x, degree=%d\n", nTemp, nActTemp);
548                 if (!pTAS2557->mnDieTvReadCounter)
549                         nAvg = 0;
550                 pTAS2557->mnDieTvReadCounter++;
551                 nAvg += nActTemp;
552                 if (!(pTAS2557->mnDieTvReadCounter % LOW_TEMPERATURE_COUNTER)) {
553                         nAvg /= LOW_TEMPERATURE_COUNTER;
554                         dev_dbg(pTAS2557->dev, "check : avg=%d\n", nAvg);
555                         if ((nAvg & 0x80000000) != 0) {
556                                 /* if Die temperature is below ZERO */
557                                 if (pTAS2557->mnDevCurrentGain != LOW_TEMPERATURE_GAIN) {
558                                         nResult = tas2557_set_DAC_gain(pTAS2557, LOW_TEMPERATURE_GAIN);
559                                         if (nResult < 0)
560                                                 goto end;
561                                         pTAS2557->mnDevCurrentGain = LOW_TEMPERATURE_GAIN;
562                                         dev_dbg(pTAS2557->dev, "LOW Temp: set gain to %d\n", LOW_TEMPERATURE_GAIN);
563                                 }
564                         } else if (nAvg > 5) {
565                                 /* if Die temperature is above 5 degree C */
566                                 if (pTAS2557->mnDevCurrentGain != pTAS2557->mnDevGain) {
567                                         nResult = tas2557_set_DAC_gain(pTAS2557, pTAS2557->mnDevGain);
568                                         if (nResult < 0)
569                                                 goto end;
570                                         pTAS2557->mnDevCurrentGain = pTAS2557->mnDevGain;
571                                         dev_dbg(pTAS2557->dev, "LOW Temp: set gain to original\n");
572                                 }
573                         }
574                         nAvg = 0;
575                 }
577                 if (pTAS2557->mbPowerUp)
578                         hrtimer_start(&pTAS2557->mtimer,
579                                 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
580         }
582 end:
584 #ifdef CONFIG_TAS2557_MISC
585         mutex_unlock(&pTAS2557->file_lock);
586 #endif
588 #ifdef CONFIG_TAS2557_CODEC
589         mutex_unlock(&pTAS2557->codec_lock);
590 #endif
593 #ifdef CONFIG_PM_SLEEP
594 static int tas2557_suspend(struct device *dev)
596         struct tas2557_priv *pTAS2557 = dev_get_drvdata(dev);
598         dev_dbg(pTAS2557->dev, "%s\n", __func__);
599         if (hrtimer_active(&pTAS2557->mtimer)) {
600                 dev_dbg(pTAS2557->dev, "cancel die temp timer\n");
601                 hrtimer_cancel(&pTAS2557->mtimer);
602         }
604         return 0;
607 static int tas2557_resume(struct device *dev)
609         struct tas2557_priv *pTAS2557 = dev_get_drvdata(dev);
610         struct TProgram *pProgram;
612         dev_dbg(pTAS2557->dev, "%s\n", __func__);
613         if (!pTAS2557->mpFirmware->mpPrograms) {
614                 dev_dbg(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
615                 goto end;
616         }
618         if (pTAS2557->mnCurrentProgram >= pTAS2557->mpCalFirmware->mnPrograms) {
619                 dev_err(pTAS2557->dev, "%s, firmware corrupted\n", __func__);
620                 goto end;
621         }
623         pProgram = &(pTAS2557->mpFirmware->mpPrograms[pTAS2557->mnCurrentProgram]);
624         if (pTAS2557->mbPowerUp && (pProgram->mnAppMode == TAS2557_APP_TUNINGMODE)) {
625                 if (!hrtimer_active(&pTAS2557->mtimer)) {
626                         dev_dbg(pTAS2557->dev, "%s, start Die Temp check timer\n", __func__);
627                         pTAS2557->mnDieTvReadCounter = 0;
628                         hrtimer_start(&pTAS2557->mtimer,
629                                 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
630                 }
631         }
633 end:
635         return 0;
637 #endif
639 static bool tas2557_volatile(struct device *pDev, unsigned int nRegister)
641         return true;
644 static bool tas2557_writeable(struct device *pDev, unsigned int nRegister)
646         return true;
649 static const struct regmap_config tas2557_i2c_regmap = {
650         .reg_bits = 8,
651         .val_bits = 8,
652         .writeable_reg = tas2557_writeable,
653         .volatile_reg = tas2557_volatile,
654         .cache_type = REGCACHE_NONE,
655         .max_register = 128,
656 };
658 /* tas2557_i2c_probe :
659 * platform dependent
660 * should implement hardware reset functionality
661 */
662 static int tas2557_i2c_probe(struct i2c_client *pClient,
663         const struct i2c_device_id *pID)
665         struct tas2557_priv *pTAS2557;
666         int nResult = 0;
667         unsigned int nValue = 0;
668         const char *pFWName;
670         dev_info(&pClient->dev, "%s enter\n", __func__);
672         pTAS2557 = devm_kzalloc(&pClient->dev, sizeof(struct tas2557_priv), GFP_KERNEL);
673         if (!pTAS2557) {
674                 nResult = -ENOMEM;
675                 goto err;
676         }
678         pTAS2557->dev = &pClient->dev;
679         i2c_set_clientdata(pClient, pTAS2557);
680         dev_set_drvdata(&pClient->dev, pTAS2557);
682         pTAS2557->mpRegmap = devm_regmap_init_i2c(pClient, &tas2557_i2c_regmap);
683         if (IS_ERR(pTAS2557->mpRegmap)) {
684                 nResult = PTR_ERR(pTAS2557->mpRegmap);
685                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
686                         nResult);
687                 goto err;
688         }
690         if (pClient->dev.of_node)
691                 tas2557_parse_dt(&pClient->dev, pTAS2557);
693         if (gpio_is_valid(pTAS2557->mnResetGPIO)) {
694                 nResult = gpio_request(pTAS2557->mnResetGPIO, "TAS2557-RESET");
695                 if (nResult < 0) {
696                         dev_err(pTAS2557->dev, "%s: GPIO %d request error\n",
697                                 __func__, pTAS2557->mnResetGPIO);
698                         goto err;
699                 }
700                 tas2557_hw_reset(pTAS2557);
701         }
703         pTAS2557->read = tas2557_dev_read;
704         pTAS2557->write = tas2557_dev_write;
705         pTAS2557->bulk_read = tas2557_dev_bulk_read;
706         pTAS2557->bulk_write = tas2557_dev_bulk_write;
707         pTAS2557->update_bits = tas2557_dev_update_bits;
708         pTAS2557->enableIRQ = tas2557_enableIRQ;
709         pTAS2557->clearIRQ = tas2557_clearIRQ;
710         pTAS2557->set_config = tas2557_set_config;
711         pTAS2557->set_calibration = tas2557_set_calibration;
712         pTAS2557->hw_reset = tas2557_hw_reset;
714         mutex_init(&pTAS2557->dev_lock);
716         /* Reset the chip */
717         nResult = tas2557_dev_write(pTAS2557, TAS2557_SW_RESET_REG, 0x01);
718         if (nResult < 0) {
719                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
720                 goto err;
721         }
723         msleep(1);
724         tas2557_dev_read(pTAS2557, TAS2557_REV_PGID_REG, &nValue);
725         pTAS2557->mnPGID = nValue;
726         if (pTAS2557->mnPGID == TAS2557_PG_VERSION_2P1) {
727                 dev_info(pTAS2557->dev, "PG2.1 Silicon found\n");
728                 pFWName = TAS2557_FW_NAME;
729         } else if (pTAS2557->mnPGID == TAS2557_PG_VERSION_1P0) {
730                 dev_info(pTAS2557->dev, "PG1.0 Silicon found\n");
731                 pFWName = TAS2557_PG1P0_FW_NAME;
732         } else {
733                 nResult = -ENOTSUPP;
734                 dev_info(pTAS2557->dev, "unsupport Silicon 0x%x\n", pTAS2557->mnPGID);
735                 goto err;
736         }
738         if (gpio_is_valid(pTAS2557->mnGpioINT)) {
739                 nResult = gpio_request(pTAS2557->mnGpioINT, "TAS2557-IRQ");
740                 if (nResult < 0) {
741                         dev_err(pTAS2557->dev,
742                                 "%s: GPIO %d request INT error\n",
743                                 __func__, pTAS2557->mnGpioINT);
744                         goto err;
745                 }
747                 gpio_direction_input(pTAS2557->mnGpioINT);
748                 pTAS2557->mnIRQ = gpio_to_irq(pTAS2557->mnGpioINT);
749                 dev_dbg(pTAS2557->dev, "irq = %d\n", pTAS2557->mnIRQ);
750                 INIT_DELAYED_WORK(&pTAS2557->irq_work, irq_work_routine);
751                 nResult = request_threaded_irq(pTAS2557->mnIRQ, tas2557_irq_handler,
752                                         NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
753                                 pClient->name, pTAS2557);
754                 if (nResult < 0) {
755                         dev_err(pTAS2557->dev,
756                                 "request_irq failed, %d\n", nResult);
757                         goto err;
758                 }
759                 disable_irq_nosync(pTAS2557->mnIRQ);
760         }
762         pTAS2557->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
763         if (!pTAS2557->mpFirmware) {
764                 nResult = -ENOMEM;
765                 goto err;
766         }
768         pTAS2557->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
769         if (!pTAS2557->mpCalFirmware) {
770                 nResult = -ENOMEM;
771                 goto err;
772         }
774 #ifdef CONFIG_TAS2557_CODEC
775         mutex_init(&pTAS2557->codec_lock);
776         tas2557_register_codec(pTAS2557);
777 #endif
779 #ifdef CONFIG_TAS2557_MISC
780         mutex_init(&pTAS2557->file_lock);
781         tas2557_register_misc(pTAS2557);
782 #endif
784 #ifdef ENABLE_TILOAD
785         tiload_driver_init(pTAS2557);
786 #endif
788         hrtimer_init(&pTAS2557->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
789         pTAS2557->mtimer.function = temperature_timer_func;
790         INIT_WORK(&pTAS2557->mtimerwork, timer_work_routine);
792         nResult = request_firmware_nowait(THIS_MODULE, 1, pFWName,
793                 pTAS2557->dev, GFP_KERNEL, pTAS2557, tas2557_fw_ready);
795 err:
797         return nResult;
800 static int tas2557_i2c_remove(struct i2c_client *pClient)
802         struct tas2557_priv *pTAS2557 = i2c_get_clientdata(pClient);
804         dev_info(pTAS2557->dev, "%s\n", __func__);
806 #ifdef CONFIG_TAS2557_CODEC
807         tas2557_deregister_codec(pTAS2557);
808         mutex_destroy(&pTAS2557->codec_lock);
809 #endif
811 #ifdef CONFIG_TAS2557_MISC
812         tas2557_deregister_misc(pTAS2557);
813         mutex_destroy(&pTAS2557->file_lock);
814 #endif
816         mutex_destroy(&pTAS2557->dev_lock);
817         return 0;
820 static const struct i2c_device_id tas2557_i2c_id[] = {
821         {"tas2557", 0},
822         {}
823 };
825 MODULE_DEVICE_TABLE(i2c, tas2557_i2c_id);
827 #if defined(CONFIG_OF)
828 static const struct of_device_id tas2557_of_match[] = {
829         {.compatible = "ti,tas2557"},
830         {},
831 };
833 MODULE_DEVICE_TABLE(of, tas2557_of_match);
834 #endif
836 #ifdef CONFIG_PM_SLEEP
837 static const struct dev_pm_ops tas2557_pm_ops = {
838         SET_SYSTEM_SLEEP_PM_OPS(tas2557_suspend, tas2557_resume)
839 };
840 #endif
842 static struct i2c_driver tas2557_i2c_driver = {
843         .driver = {
844                         .name = "tas2557",
845                         .owner = THIS_MODULE,
846 #ifdef CONFIG_PM_SLEEP
847                         .pm = &tas2557_pm_ops,
848 #endif
849 #if defined(CONFIG_OF)
850                         .of_match_table = of_match_ptr(tas2557_of_match),
851 #endif
852                 },
853         .probe = tas2557_i2c_probe,
854         .remove = tas2557_i2c_remove,
855         .id_table = tas2557_i2c_id,
856 };
858 module_i2c_driver(tas2557_i2c_driver);
860 MODULE_AUTHOR("Texas Instruments Inc.");
861 MODULE_DESCRIPTION("TAS2557 I2C Smart Amplifier driver");
862 MODULE_LICENSE("GPL v2");
864 #endif