2f9f771fe59852f689d27f54fc80951594848e2c
[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 (pTAS2557->mnIRQ != 0)
344                                 enable_irq(pTAS2557->mnIRQ);
345                         pTAS2557->mbIRQEnable = true;
346                 }
347         } else {
348                 if (pTAS2557->mbIRQEnable) {
349                         if (pTAS2557->mnIRQ != 0)
350                                 disable_irq_nosync(pTAS2557->mnIRQ);
351                         pTAS2557->mbIRQEnable = false;
352                 }
353         }
356 static void tas2557_hw_reset(struct tas2557_priv *pTAS2557)
358         if (gpio_is_valid(pTAS2557->mnResetGPIO)) {
359                 gpio_direction_output(pTAS2557->mnResetGPIO, 0);
360                 msleep(5);
361                 gpio_direction_output(pTAS2557->mnResetGPIO, 1);
362                 msleep(2);
363         }
365         pTAS2557->mnCurrentBook = -1;
366         pTAS2557->mnCurrentPage = -1;
367         if (pTAS2557->mnErrCode)
368                 dev_info(pTAS2557->dev, "before reset, ErrCode=0x%x\n", pTAS2557->mnErrCode);
369         pTAS2557->mnErrCode = 0;
372 static void irq_work_routine(struct work_struct *work)
374         int nResult = 0;
375         unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
376         unsigned int nDevPowerUpFlag = 0;
377         struct tas2557_priv *pTAS2557 =
378                 container_of(work, struct tas2557_priv, irq_work.work);
380 #ifdef CONFIG_TAS2557_CODEC
381         mutex_lock(&pTAS2557->codec_lock);
382 #endif
384 #ifdef CONFIG_TAS2557_MISC
385         mutex_lock(&pTAS2557->file_lock);
386 #endif
388         if (!pTAS2557->mbPowerUp)
389                 goto end;
391         if ((!pTAS2557->mpFirmware->mnConfigurations)
392                 || (!pTAS2557->mpFirmware->mnPrograms)) {
393                 dev_info(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
394                 goto end;
395         }
397         nResult = tas2557_dev_read(pTAS2557, TAS2557_FLAGS_1, &nDevInt1Status);
398         if (nResult >= 0)
399                 nResult = tas2557_dev_read(pTAS2557, TAS2557_FLAGS_2, &nDevInt2Status);
401         if (nResult < 0)
402                 goto program;
404         if (((nDevInt1Status & 0xfc) != 0) || ((nDevInt2Status & 0x0c) != 0)) {
405                 /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
406                 dev_err(pTAS2557->dev, "critical error: 0x%x, 0x%x\n", nDevInt1Status, nDevInt2Status);
407                         if (nDevInt1Status & 0x80) {
408                                 pTAS2557->mnErrCode |= ERROR_OVER_CURRENT;
409                                 dev_err(pTAS2557->dev, "DEVA SPK over current!\n");
410                         } else
411                                 pTAS2557->mnErrCode &= ~ERROR_OVER_CURRENT;
413                         if (nDevInt1Status & 0x40) {
414                                 pTAS2557->mnErrCode |= ERROR_UNDER_VOLTAGE;
415                                 dev_err(pTAS2557->dev, "DEVA SPK under voltage!\n");
416                         } else
417                                 pTAS2557->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
419                         if (nDevInt1Status & 0x20) {
420                                 pTAS2557->mnErrCode |= ERROR_CLK_HALT;
421                                 dev_err(pTAS2557->dev, "DEVA clk halted!\n");
422                         } else
423                                 pTAS2557->mnErrCode &= ~ERROR_CLK_HALT;
425                         if (nDevInt1Status & 0x10) {
426                                 pTAS2557->mnErrCode |= ERROR_DIE_OVERTEMP;
427                                 dev_err(pTAS2557->dev, "DEVA die over temperature!\n");
428                         } else
429                                 pTAS2557->mnErrCode &= ~ERROR_DIE_OVERTEMP;
431                         if (nDevInt1Status & 0x08) {
432                                 pTAS2557->mnErrCode |= ERROR_BROWNOUT;
433                                 dev_err(pTAS2557->dev, "DEVA brownout!\n");
434                         } else
435                                 pTAS2557->mnErrCode &= ~ERROR_BROWNOUT;
437                         if (nDevInt1Status & 0x04) {
438                                 pTAS2557->mnErrCode |= ERROR_CLK_LOST;
439                                 dev_err(pTAS2557->dev, "DEVA clock lost!\n");
440                         } else
441                                 pTAS2557->mnErrCode &= ~ERROR_CLK_LOST;
443                         if (nDevInt2Status & 0x08) {
444                                 pTAS2557->mnErrCode |= ERROR_CLK_DET1;
445                                 dev_err(pTAS2557->dev, "DEVA clk detection 1!\n");
446                         } else
447                                 pTAS2557->mnErrCode &= ~ERROR_CLK_DET1;
449                         if (nDevInt2Status & 0x04) {
450                                 pTAS2557->mnErrCode |= ERROR_CLK_DET2;
451                                 dev_err(pTAS2557->dev, "DEVA clk detection 2!\n");
452                         } else
453                                 pTAS2557->mnErrCode &= ~ERROR_CLK_DET2;
455                 goto program;
456         } else {
457                 nResult = tas2557_dev_read(pTAS2557, TAS2557_POWER_UP_FLAG_REG, &nDevPowerUpFlag);
458                 if (nResult < 0)
459                         goto program;
460                 if ((nDevPowerUpFlag & 0xc0) != 0xc0) {
461                         dev_err(pTAS2557->dev, "%s, Critical DevA ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
462                                 __func__,
463                                 TAS2557_BOOK_ID(TAS2557_POWER_UP_FLAG_REG),
464                                 TAS2557_PAGE_ID(TAS2557_POWER_UP_FLAG_REG),
465                                 TAS2557_PAGE_REG(TAS2557_POWER_UP_FLAG_REG),
466                                 nDevPowerUpFlag);
467                         pTAS2557->mnErrCode |= ERROR_CLASSD_PWR;
468                         goto program;
469                 }
470                 pTAS2557->mnErrCode &= ~ERROR_CLASSD_PWR;
472                 dev_dbg(pTAS2557->dev, "%s: INT1=0x%x, INT2=0x%x; PowerUpFlag=0x%x\n",
473                         __func__, nDevInt1Status, nDevInt2Status, nDevPowerUpFlag);
474                 goto end;
475         }
477 program:
478         /* hardware reset and reload */
479         tas2557_set_program(pTAS2557, pTAS2557->mnCurrentProgram, pTAS2557->mnCurrentConfiguration);
481 end:
483 #ifdef CONFIG_TAS2557_MISC
484         mutex_unlock(&pTAS2557->file_lock);
485 #endif
487 #ifdef CONFIG_TAS2557_CODEC
488         mutex_unlock(&pTAS2557->codec_lock);
489 #endif
492 static irqreturn_t tas2557_irq_handler(int irq, void *dev_id)
494         struct tas2557_priv *pTAS2557 = (struct tas2557_priv *)dev_id;
496         tas2557_enableIRQ(pTAS2557, false);
497         /* get IRQ status after 100 ms */
498         schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(100));
499         return IRQ_HANDLED;
502 static enum hrtimer_restart temperature_timer_func(struct hrtimer *timer)
504         struct tas2557_priv *pTAS2557 = container_of(timer, struct tas2557_priv, mtimer);
506         if (pTAS2557->mbPowerUp) {
507                 schedule_work(&pTAS2557->mtimerwork);
508                 schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(1));
509         }
510         return HRTIMER_NORESTART;
513 static void timer_work_routine(struct work_struct *work)
515         struct tas2557_priv *pTAS2557 = container_of(work, struct tas2557_priv, mtimerwork);
516         int nResult, nTemp, nActTemp;
517         struct TProgram *pProgram;
518         static int nAvg;
520 #ifdef CONFIG_TAS2557_CODEC
521         mutex_lock(&pTAS2557->codec_lock);
522 #endif
524 #ifdef CONFIG_TAS2557_MISC
525         mutex_lock(&pTAS2557->file_lock);
526 #endif
528         if (!pTAS2557->mpFirmware->mnConfigurations) {
529                 dev_info(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
530                 goto end;
531         }
533         pProgram = &(pTAS2557->mpFirmware->mpPrograms[pTAS2557->mnCurrentProgram]);
534         if (!pTAS2557->mbPowerUp
535                 || (pProgram->mnAppMode != TAS2557_APP_TUNINGMODE)) {
536                 dev_info(pTAS2557->dev, "%s, pass, Pow=%d, program=%s\n",
537                         __func__, pTAS2557->mbPowerUp, pProgram->mpName);
538                 goto end;
539         }
541         nResult = tas2557_get_die_temperature(pTAS2557, &nTemp);
542         if (nResult >= 0) {
543                 nActTemp = (int)(nTemp >> 23);
544                 dev_dbg(pTAS2557->dev, "Die=0x%x, degree=%d\n", nTemp, nActTemp);
545                 if (!pTAS2557->mnDieTvReadCounter)
546                         nAvg = 0;
547                 pTAS2557->mnDieTvReadCounter++;
548                 nAvg += nActTemp;
549                 if (!(pTAS2557->mnDieTvReadCounter % LOW_TEMPERATURE_COUNTER)) {
550                         nAvg /= LOW_TEMPERATURE_COUNTER;
551                         dev_dbg(pTAS2557->dev, "check : avg=%d\n", nAvg);
552                         if ((nAvg & 0x80000000) != 0) {
553                                 /* if Die temperature is below ZERO */
554                                 if (pTAS2557->mnDevCurrentGain != LOW_TEMPERATURE_GAIN) {
555                                         nResult = tas2557_set_DAC_gain(pTAS2557, LOW_TEMPERATURE_GAIN);
556                                         if (nResult < 0)
557                                                 goto end;
558                                         pTAS2557->mnDevCurrentGain = LOW_TEMPERATURE_GAIN;
559                                         dev_dbg(pTAS2557->dev, "LOW Temp: set gain to %d\n", LOW_TEMPERATURE_GAIN);
560                                 }
561                         } else if (nAvg > 5) {
562                                 /* if Die temperature is above 5 degree C */
563                                 if (pTAS2557->mnDevCurrentGain != pTAS2557->mnDevGain) {
564                                         nResult = tas2557_set_DAC_gain(pTAS2557, pTAS2557->mnDevGain);
565                                         if (nResult < 0)
566                                                 goto end;
567                                         pTAS2557->mnDevCurrentGain = pTAS2557->mnDevGain;
568                                         dev_dbg(pTAS2557->dev, "LOW Temp: set gain to original\n");
569                                 }
570                         }
571                         nAvg = 0;
572                 }
574                 if (pTAS2557->mbPowerUp)
575                         hrtimer_start(&pTAS2557->mtimer,
576                                 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
577         }
579 end:
581 #ifdef CONFIG_TAS2557_MISC
582         mutex_unlock(&pTAS2557->file_lock);
583 #endif
585 #ifdef CONFIG_TAS2557_CODEC
586         mutex_unlock(&pTAS2557->codec_lock);
587 #endif
590 #ifdef CONFIG_PM_SLEEP
591 static int tas2557_suspend(struct device *dev)
593         struct tas2557_priv *pTAS2557 = dev_get_drvdata(dev);
595         dev_dbg(pTAS2557->dev, "%s\n", __func__);
596         if (hrtimer_active(&pTAS2557->mtimer)) {
597                 dev_dbg(pTAS2557->dev, "cancel die temp timer\n");
598                 hrtimer_cancel(&pTAS2557->mtimer);
599         }
601         return 0;
604 static int tas2557_resume(struct device *dev)
606         struct tas2557_priv *pTAS2557 = dev_get_drvdata(dev);
607         struct TProgram *pProgram;
609         dev_dbg(pTAS2557->dev, "%s\n", __func__);
610         if (!pTAS2557->mpFirmware->mpPrograms) {
611                 dev_dbg(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
612                 goto end;
613         }
615         if (pTAS2557->mnCurrentProgram >= pTAS2557->mpCalFirmware->mnPrograms) {
616                 dev_err(pTAS2557->dev, "%s, firmware corrupted\n", __func__);
617                 goto end;
618         }
620         pProgram = &(pTAS2557->mpFirmware->mpPrograms[pTAS2557->mnCurrentProgram]);
621         if (pTAS2557->mbPowerUp && (pProgram->mnAppMode == TAS2557_APP_TUNINGMODE)) {
622                 if (!hrtimer_active(&pTAS2557->mtimer)) {
623                         dev_dbg(pTAS2557->dev, "%s, start Die Temp check timer\n", __func__);
624                         pTAS2557->mnDieTvReadCounter = 0;
625                         hrtimer_start(&pTAS2557->mtimer,
626                                 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
627                 }
628         }
630 end:
632         return 0;
634 #endif
636 static bool tas2557_volatile(struct device *pDev, unsigned int nRegister)
638         return true;
641 static bool tas2557_writeable(struct device *pDev, unsigned int nRegister)
643         return true;
646 static const struct regmap_config tas2557_i2c_regmap = {
647         .reg_bits = 8,
648         .val_bits = 8,
649         .writeable_reg = tas2557_writeable,
650         .volatile_reg = tas2557_volatile,
651         .cache_type = REGCACHE_NONE,
652         .max_register = 128,
653 };
655 /* tas2557_i2c_probe :
656 * platform dependent
657 * should implement hardware reset functionality
658 */
659 static int tas2557_i2c_probe(struct i2c_client *pClient,
660         const struct i2c_device_id *pID)
662         struct tas2557_priv *pTAS2557;
663         int nResult = 0;
664         unsigned int nValue = 0;
665         const char *pFWName;
667         dev_info(&pClient->dev, "%s enter\n", __func__);
669         pTAS2557 = devm_kzalloc(&pClient->dev, sizeof(struct tas2557_priv), GFP_KERNEL);
670         if (!pTAS2557) {
671                 nResult = -ENOMEM;
672                 goto err;
673         }
675         pTAS2557->dev = &pClient->dev;
676         i2c_set_clientdata(pClient, pTAS2557);
677         dev_set_drvdata(&pClient->dev, pTAS2557);
679         pTAS2557->mpRegmap = devm_regmap_init_i2c(pClient, &tas2557_i2c_regmap);
680         if (IS_ERR(pTAS2557->mpRegmap)) {
681                 nResult = PTR_ERR(pTAS2557->mpRegmap);
682                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
683                         nResult);
684                 goto err;
685         }
687         if (pClient->dev.of_node)
688                 tas2557_parse_dt(&pClient->dev, pTAS2557);
690         if (gpio_is_valid(pTAS2557->mnResetGPIO)) {
691                 nResult = gpio_request(pTAS2557->mnResetGPIO, "TAS2557-RESET");
692                 if (nResult < 0) {
693                         dev_err(pTAS2557->dev, "%s: GPIO %d request error\n",
694                                 __func__, pTAS2557->mnResetGPIO);
695                         goto err;
696                 }
697                 tas2557_hw_reset(pTAS2557);
698         }
700         pTAS2557->read = tas2557_dev_read;
701         pTAS2557->write = tas2557_dev_write;
702         pTAS2557->bulk_read = tas2557_dev_bulk_read;
703         pTAS2557->bulk_write = tas2557_dev_bulk_write;
704         pTAS2557->update_bits = tas2557_dev_update_bits;
705         pTAS2557->enableIRQ = tas2557_enableIRQ;
706         pTAS2557->clearIRQ = tas2557_clearIRQ;
707         pTAS2557->set_config = tas2557_set_config;
708         pTAS2557->set_calibration = tas2557_set_calibration;
709         pTAS2557->hw_reset = tas2557_hw_reset;
711         mutex_init(&pTAS2557->dev_lock);
713         /* Reset the chip */
714         nResult = tas2557_dev_write(pTAS2557, TAS2557_SW_RESET_REG, 0x01);
715         if (nResult < 0) {
716                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
717                 goto err;
718         }
720         msleep(1);
721         tas2557_dev_read(pTAS2557, TAS2557_REV_PGID_REG, &nValue);
722         pTAS2557->mnPGID = nValue;
723         if (pTAS2557->mnPGID == TAS2557_PG_VERSION_2P1) {
724                 dev_info(pTAS2557->dev, "PG2.1 Silicon found\n");
725                 pFWName = TAS2557_FW_NAME;
726         } else if (pTAS2557->mnPGID == TAS2557_PG_VERSION_1P0) {
727                 dev_info(pTAS2557->dev, "PG1.0 Silicon found\n");
728                 pFWName = TAS2557_PG1P0_FW_NAME;
729         } else {
730                 nResult = -ENOTSUPP;
731                 dev_info(pTAS2557->dev, "unsupport Silicon 0x%x\n", pTAS2557->mnPGID);
732                 goto err;
733         }
735         if (gpio_is_valid(pTAS2557->mnGpioINT)) {
736                 nResult = gpio_request(pTAS2557->mnGpioINT, "TAS2557-IRQ");
737                 if (nResult < 0) {
738                         dev_err(pTAS2557->dev,
739                                 "%s: GPIO %d request INT error\n",
740                                 __func__, pTAS2557->mnGpioINT);
741                         goto err;
742                 }
744                 gpio_direction_input(pTAS2557->mnGpioINT);
745                 pTAS2557->mnIRQ = gpio_to_irq(pTAS2557->mnGpioINT);
746                 dev_dbg(pTAS2557->dev, "irq = %d\n", pTAS2557->mnIRQ);
747                 INIT_DELAYED_WORK(&pTAS2557->irq_work, irq_work_routine);
748                 nResult = request_threaded_irq(pTAS2557->mnIRQ, tas2557_irq_handler,
749                                         NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
750                                 pClient->name, pTAS2557);
751                 if (nResult < 0) {
752                         dev_err(pTAS2557->dev,
753                                 "request_irq failed, %d\n", nResult);
754                         goto err;
755                 }
756                 disable_irq_nosync(pTAS2557->mnIRQ);
757         }
759         pTAS2557->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
760         if (!pTAS2557->mpFirmware) {
761                 nResult = -ENOMEM;
762                 goto err;
763         }
765         pTAS2557->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
766         if (!pTAS2557->mpCalFirmware) {
767                 nResult = -ENOMEM;
768                 goto err;
769         }
771 #ifdef CONFIG_TAS2557_CODEC
772         mutex_init(&pTAS2557->codec_lock);
773         tas2557_register_codec(pTAS2557);
774 #endif
776 #ifdef CONFIG_TAS2557_MISC
777         mutex_init(&pTAS2557->file_lock);
778         tas2557_register_misc(pTAS2557);
779 #endif
781 #ifdef ENABLE_TILOAD
782         tiload_driver_init(pTAS2557);
783 #endif
785         hrtimer_init(&pTAS2557->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
786         pTAS2557->mtimer.function = temperature_timer_func;
787         INIT_WORK(&pTAS2557->mtimerwork, timer_work_routine);
789         nResult = request_firmware_nowait(THIS_MODULE, 1, pFWName,
790                 pTAS2557->dev, GFP_KERNEL, pTAS2557, tas2557_fw_ready);
792 err:
794         return nResult;
797 static int tas2557_i2c_remove(struct i2c_client *pClient)
799         struct tas2557_priv *pTAS2557 = i2c_get_clientdata(pClient);
801         dev_info(pTAS2557->dev, "%s\n", __func__);
803 #ifdef CONFIG_TAS2557_CODEC
804         tas2557_deregister_codec(pTAS2557);
805         mutex_destroy(&pTAS2557->codec_lock);
806 #endif
808 #ifdef CONFIG_TAS2557_MISC
809         tas2557_deregister_misc(pTAS2557);
810         mutex_destroy(&pTAS2557->file_lock);
811 #endif
813         mutex_destroy(&pTAS2557->dev_lock);
814         return 0;
817 static const struct i2c_device_id tas2557_i2c_id[] = {
818         {"tas2557", 0},
819         {}
820 };
822 MODULE_DEVICE_TABLE(i2c, tas2557_i2c_id);
824 #if defined(CONFIG_OF)
825 static const struct of_device_id tas2557_of_match[] = {
826         {.compatible = "ti,tas2557"},
827         {},
828 };
830 MODULE_DEVICE_TABLE(of, tas2557_of_match);
831 #endif
833 #ifdef CONFIG_PM_SLEEP
834 static const struct dev_pm_ops tas2557_pm_ops = {
835         SET_SYSTEM_SLEEP_PM_OPS(tas2557_suspend, tas2557_resume)
836 };
837 #endif
839 static struct i2c_driver tas2557_i2c_driver = {
840         .driver = {
841                         .name = "tas2557",
842                         .owner = THIS_MODULE,
843 #ifdef CONFIG_PM_SLEEP
844                         .pm = &tas2557_pm_ops,
845 #endif
846 #if defined(CONFIG_OF)
847                         .of_match_table = of_match_ptr(tas2557_of_match),
848 #endif
849                 },
850         .probe = tas2557_i2c_probe,
851         .remove = tas2557_i2c_remove,
852         .id_table = tas2557_i2c_id,
853 };
855 module_i2c_driver(tas2557_i2c_driver);
857 MODULE_AUTHOR("Texas Instruments Inc.");
858 MODULE_DESCRIPTION("TAS2557 I2C Smart Amplifier driver");
859 MODULE_LICENSE("GPL v2");
861 #endif