Fix IRQ related bugs
[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, "%s: BOOK:PAGE:REG %u:%u:%u,%x\n", __func__,
121                         TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
122                         TAS2563_PAGE_REG(reg), *pValue);
124 end:
125         mutex_unlock(&pTAS2563->dev_lock);
126         return nResult;
129 static int tas2563_dev_write(struct tas2563_priv *pTAS2563,
130         unsigned int reg, unsigned int value)
132         int nResult = 0;
134         mutex_lock(&pTAS2563->dev_lock);
136         nResult = tas2563_change_book_page(pTAS2563,
137                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
138         if (nResult < 0)
139                 goto end;
141         nResult = regmap_write(pTAS2563->regmap, TAS2563_PAGE_REG(reg),
142                         value);
143         if (nResult < 0)
144                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
145                         __func__, __LINE__, nResult);
146         else
147                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u, VAL: 0x%02x\n",
148                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
149                         TAS2563_PAGE_REG(reg), value);
151 end:
152         mutex_unlock(&pTAS2563->dev_lock);
153         return nResult;
156 static int tas2563_dev_bulk_write(struct tas2563_priv *pTAS2563,
157         unsigned int reg, u8 *pData, unsigned int nLength)
159         int nResult = 0;
161         mutex_lock(&pTAS2563->dev_lock);
163         nResult = tas2563_change_book_page(pTAS2563,
164                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
165         if (nResult < 0)
166                 goto end;
168         nResult = regmap_bulk_write(pTAS2563->regmap,
169                 TAS2563_PAGE_REG(reg), pData, nLength);
170         if (nResult < 0)
171                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
172                         __func__, __LINE__, nResult);
173         else
174                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u, len: 0x%02x\n",
175                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
176                         TAS2563_PAGE_REG(reg), nLength);
178 end:
179         mutex_unlock(&pTAS2563->dev_lock);
180         return nResult;
183 static int tas2563_dev_bulk_read(struct tas2563_priv *pTAS2563,
184         unsigned int reg, u8 *pData, unsigned int nLength)
186         int nResult = 0;
187         int i = 0;
189         mutex_lock(&pTAS2563->dev_lock);
191         nResult = tas2563_change_book_page(pTAS2563,
192                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
193         if (nResult < 0)
194                 goto end;
196         dev_dbg(pTAS2563->dev, "reg = %u, pData = %s, Lenth = %d", reg, pData, nLength);
198         #define STRIDE 4
199         /* Read chunk bytes defined by STRIDE */
200         for (i = 0; i < (nLength / STRIDE); i++) {
201                         nResult = regmap_bulk_read(pTAS2563->regmap,
202                                                         TAS2563_PAGE_REG((reg + i*STRIDE)),
203                                                         &pData[i*STRIDE], STRIDE);
204                         if (nResult < 0) {
205                                         dev_err(pTAS2563->dev, "%s, %d, I2C error %d\n",
206                                                         __func__, __LINE__, nResult);
207                                         pTAS2563->mnErrCode |= ERROR_DEVA_I2C_COMM;
208                         } else
209                                         pTAS2563->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
210         }
212         /* Read remaining bytes */
213         if ((nLength % STRIDE) != 0) {
214                         nResult = regmap_bulk_read(pTAS2563->regmap,
215                                                         TAS2563_PAGE_REG(reg + i*STRIDE),
216                                                         &pData[i*STRIDE], (nLength % STRIDE));
217                         if (nResult < 0) {
218                                         dev_err(pTAS2563->dev, "%s, %d, I2C error %d\n",
219                                                         __func__, __LINE__, nResult);
220                                         pTAS2563->mnErrCode |= ERROR_DEVA_I2C_COMM;
221                         } else
222                                         pTAS2563->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
223         }
226 end:
227         mutex_unlock(&pTAS2563->dev_lock);
228         return nResult;
231 static int tas2563_dev_update_bits(struct tas2563_priv *pTAS2563,
232         unsigned int reg, unsigned int mask, unsigned int value)
234         int nResult = 0;
236         mutex_lock(&pTAS2563->dev_lock);
237         nResult = tas2563_change_book_page(pTAS2563,
238                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
239         if (nResult < 0)
240                 goto end;
242         nResult = regmap_update_bits(pTAS2563->regmap,
243         TAS2563_PAGE_REG(reg), mask, value);
244         if (nResult < 0)
245                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
246                         __func__, __LINE__, nResult);
247         else
248                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u, mask: 0x%x, val=0x%x\n",
249                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
250                         TAS2563_PAGE_REG(reg), mask, value);
251 end:
252         mutex_unlock(&pTAS2563->dev_lock);
253         return nResult;
256 static const struct reg_default tas2563_reg_defaults[] = {
257         { TAS2563_Page, 0x00 },
258         { TAS2563_SoftwareReset, 0x00 },
259         { TAS2563_PowerControl, 0x0e },
260         { TAS2563_PlaybackConfigurationReg0, 0x10 },
261         { TAS2563_MiscConfigurationReg0, 0x07 },
262         { TAS2563_TDMConfigurationReg1, 0x02 },
263         { TAS2563_TDMConfigurationReg2, 0x0a },
264         { TAS2563_TDMConfigurationReg3, 0x10 },
265         { TAS2563_InterruptMaskReg0, 0xfc },
266         { TAS2563_InterruptMaskReg1, 0xb1 },
267         { TAS2563_InterruptConfiguration, 0x1d },
268         { TAS2563_MiscIRQ, 0x81 },
269         { TAS2563_ClockConfiguration, 0x0c },
271 };
273 static bool tas2563_volatile(struct device *dev, unsigned int reg)
275         return true;
278 static bool tas2563_writeable(struct device *dev, unsigned int reg)
280         return true;
282 static const struct regmap_config tas2563_i2c_regmap = {
283         .reg_bits = 8,
284         .val_bits = 8,
285         .writeable_reg = tas2563_writeable,
286         .volatile_reg = tas2563_volatile,
287 //      .reg_defaults = tas2563_reg_defaults,
288 //      .num_reg_defaults = ARRAY_SIZE(tas2563_reg_defaults),
289         .cache_type = REGCACHE_NONE,
290         .max_register = 1 * 128,
291 };
294 static void tas2563_hw_reset(struct tas2563_priv *pTAS2563)
296         if (gpio_is_valid(pTAS2563->mnResetGPIO)) {
297                 gpio_direction_output(pTAS2563->mnResetGPIO, 0);
298                 msleep(5);
299                 gpio_direction_output(pTAS2563->mnResetGPIO, 1);
300                 msleep(2);
301         }
303         pTAS2563->mnCurrentBook = -1;
304         pTAS2563->mnCurrentPage = -1;
307 void tas2563_enableIRQ(struct tas2563_priv *pTAS2563, bool enable)
309         if (enable) {
310                 if (pTAS2563->mbIRQEnable)
311                         return;
313                 if (gpio_is_valid(pTAS2563->mnIRQGPIO))
314                         enable_irq(pTAS2563->mnIRQ);
316                 schedule_delayed_work(&pTAS2563->irq_work, msecs_to_jiffies(10));
317                 pTAS2563->mbIRQEnable = true;
318         } else {
319                 if (gpio_is_valid(pTAS2563->mnIRQGPIO))
320                         disable_irq_nosync(pTAS2563->mnIRQ);
321                 pTAS2563->mbIRQEnable = false;
322         }
326 static void irq_work_routine(struct work_struct *work)
328         struct tas2563_priv *pTAS2563 =
329                 container_of(work, struct tas2563_priv, irq_work.work);
330         unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
331         int nCounter = 2;
332         int nResult = 0;
333         int irqreg;
335         dev_info(pTAS2563->dev, "%s\n", __func__);
336 #ifdef CONFIG_TAS2563_CODEC
337         mutex_lock(&pTAS2563->codec_lock);
338 #endif
340         if (pTAS2563->mbRuntimeSuspend) {
341                 dev_info(pTAS2563->dev, "%s, Runtime Suspended\n", __func__);
342                 goto end;
343         }
345         if (pTAS2563->mnPowerState == TAS2563_POWER_SHUTDOWN) {
346                 dev_info(pTAS2563->dev, "%s, device not powered\n", __func__);
347                 goto end;
348         }
350         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg0,
351                                 TAS2563_InterruptMaskReg0_Disable);
352         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg1,
353                                 TAS2563_InterruptMaskReg1_Disable);
355         if (nResult < 0)
356                 goto reload;
358         nResult = tas2563_dev_read(pTAS2563, TAS2563_LatchedInterruptReg0, &nDevInt1Status);
359         if (nResult >= 0)
360                 nResult = tas2563_dev_read(pTAS2563, TAS2563_LatchedInterruptReg1, &nDevInt2Status);
361         else
362                 goto reload;
364         dev_info(pTAS2563->dev, "IRQ status : 0x%x, 0x%x\n",
365                         nDevInt1Status, nDevInt2Status);
367         if (((nDevInt1Status & 0x7) != 0) || ((nDevInt2Status & 0x0f) != 0)) {
368                 /* in case of INT_OC, INT_OT, INT_OVLT, INT_UVLT, INT_BO */
370                 if (nDevInt1Status & TAS2563_LatchedInterruptReg0_OCEFlagSticky_Interrupt) {
371                         pTAS2563->mnErrCode |= ERROR_OVER_CURRENT;
372                         dev_err(pTAS2563->dev, "SPK over current!\n");
373                 } else
374                         pTAS2563->mnErrCode &= ~ERROR_OVER_CURRENT;
376                 if (nDevInt1Status & TAS2563_LatchedInterruptReg0_OTEFlagSticky_Interrupt) {
377                         pTAS2563->mnErrCode |= ERROR_DIE_OVERTEMP;
378                         dev_err(pTAS2563->dev, "die over temperature!\n");
379                 } else
380                         pTAS2563->mnErrCode &= ~ERROR_DIE_OVERTEMP;
382                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_VBATOVLOSticky_Interrupt) {
383                         pTAS2563->mnErrCode |= ERROR_OVER_VOLTAGE;
384                         dev_err(pTAS2563->dev, "SPK over voltage!\n");
385                 } else
386                         pTAS2563->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
388                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_VBATUVLOSticky_Interrupt) {
389                         pTAS2563->mnErrCode |= ERROR_UNDER_VOLTAGE;
390                         dev_err(pTAS2563->dev, "SPK under voltage!\n");
391                 } else
392                         pTAS2563->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
394                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_BrownOutFlagSticky_Interrupt) {
395                         pTAS2563->mnErrCode |= ERROR_BROWNOUT;
396                         dev_err(pTAS2563->dev, "brownout!\n");
397                 } else
398                         pTAS2563->mnErrCode &= ~ERROR_BROWNOUT;
400                 goto reload;
401         } else {
402                 nCounter = 2;
404                 while (nCounter > 0) {
405                         nResult = tas2563_dev_read(pTAS2563, TAS2563_PowerControl, &nDevInt1Status);
406                         if (nResult < 0)
407                                 goto reload;
409                         if ((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
410                                 != TAS2563_PowerControl_OperationalMode10_Shutdown)
411                                 break;
413                         pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
414                         dev_info(pTAS2563->dev, "IRQ reg is: %s %d, %d\n", __func__, irqreg, __LINE__);
416                         nResult = pTAS2563->update_bits(pTAS2563, TAS2563_PowerControl,
417                                 TAS2563_PowerControl_OperationalMode10_Mask |
418                                 TAS2563_PowerControl_ISNSPower_Mask |
419                                 TAS2563_PowerControl_VSNSPower_Mask,
420                                 TAS2563_PowerControl_OperationalMode10_Active |
421                                 TAS2563_PowerControl_VSNSPower_Active |
422                                 TAS2563_PowerControl_ISNSPower_Active);
423                         if (nResult < 0)
424                                 goto reload;
426                         pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
427                         dev_info(pTAS2563->dev, "IRQ reg is: %s, %d, %d\n", __func__, irqreg, __LINE__);
429                         dev_info(pTAS2563->dev, "set ICN to -90dB\n");
430                         nResult = pTAS2563->bulk_write(pTAS2563, TAS2563_ICN_REG, pICN, 4);
431                         if(nResult < 0)
432                                 goto reload;
434                         pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
435                         dev_info(pTAS2563->dev, "IRQ reg is: %d, %d\n", irqreg, __LINE__);
437                         dev_info(pTAS2563->dev, "set ICN delay\n");
438                         nResult = pTAS2563->bulk_write(pTAS2563, TAS2563_ICN_DELAY, pICNDelay, 4);
440                         pTAS2563->read(pTAS2563, TAS2563_LatchedInterruptReg0, &irqreg);
441                         dev_info(pTAS2563->dev, "IRQ reg is: %d, %d\n", irqreg, __LINE__);
443                         nCounter--;
444                         if (nCounter > 0) {
445                                 /* in case check power status just after power on TAS2563 */
446                                 dev_dbg(pTAS2563->dev, "PowSts B: 0x%x, check again after 10ms\n",
447                                         nDevInt1Status);
448                                 msleep(20);
449                         }
450                 }
452                 if ((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
453                         == TAS2563_PowerControl_OperationalMode10_Shutdown) {
454                         dev_err(pTAS2563->dev, "%s, Critical ERROR REG[0x%x] = 0x%x\n",
455                                 __func__,
456                                 TAS2563_PowerControl,
457                                 nDevInt1Status);
458                         pTAS2563->mnErrCode |= ERROR_CLASSD_PWR;
459                         goto reload;
460                 }
461                 pTAS2563->mnErrCode &= ~ERROR_CLASSD_PWR;
462         }
464         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg0, 0xf8);
465         if (nResult < 0)
466                 goto reload;
468         nResult = tas2563_dev_write(pTAS2563, TAS2563_InterruptMaskReg1, 0xb1);
469         if (nResult < 0)
470                 goto reload;
472         goto end;
474 reload:
475         /* hardware reset and reload */
476         nResult = -1;
477         //tas2563_LoadConfig(pTAS2563);
478         tas2563_set_program(pTAS2563, pTAS2563->mnCurrentProgram, pTAS2563->mnCurrentConfiguration);
480 end:
481         if (nResult >= 0) {
482                 tas2563_enableIRQ(pTAS2563, true);
483         }
485 #ifdef CONFIG_TAS2563_CODEC
486         mutex_unlock(&pTAS2563->codec_lock);
487 #endif
490 static enum hrtimer_restart timer_func(struct hrtimer *timer)
492         struct tas2563_priv *pTAS2563 = container_of(timer,
493                 struct tas2563_priv, mtimerwork);
495         if (pTAS2563->mnPowerState != TAS2563_POWER_SHUTDOWN) {
496                 if (!delayed_work_pending(&pTAS2563->irq_work))
497                         schedule_delayed_work(&pTAS2563->irq_work,
498                                 msecs_to_jiffies(20));
499         }
501         return HRTIMER_NORESTART;
504 static irqreturn_t tas2563_irq_handler(int irq, void *dev_id)
506         struct tas2563_priv *pTAS2563 = (struct tas2563_priv *)dev_id;
508         tas2563_enableIRQ(pTAS2563, false);
509         /* get IRQ status after 100 ms */
510         schedule_delayed_work(&pTAS2563->irq_work, msecs_to_jiffies(100));
511         return IRQ_HANDLED;
515 static int tas2563_parse_dt(struct device *dev, struct tas2563_priv *pTAS2563)
517         struct device_node *np = dev->of_node;
518         int rc = 0, ret = 0;
520         rc = of_property_read_u32(np, "ti,asi-format", &pTAS2563->mnASIFormat);
521         if (rc) {
522                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
523                         "ti,asi-format", np->full_name, rc);
524         } else {
525                 dev_dbg(pTAS2563->dev, "ti,asi-format=%d",
526                         pTAS2563->mnASIFormat);
527         }
529         pTAS2563->mnResetGPIO = of_get_named_gpio(np, "ti,reset-gpio", 0);
530         if (!gpio_is_valid(pTAS2563->mnResetGPIO)) {
531                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
532                         "ti,reset-gpio", np->full_name, pTAS2563->mnResetGPIO);
533         } else {
534                 dev_dbg(pTAS2563->dev, "ti,reset-gpio=%d",
535                         pTAS2563->mnResetGPIO);
536         }
538         pTAS2563->mnIRQGPIO = of_get_named_gpio(np, "ti,irq-gpio", 0);
539         if (!gpio_is_valid(pTAS2563->mnIRQGPIO)) {
540                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
541                         "ti,irq-gpio", np->full_name, pTAS2563->mnIRQGPIO);
542         } else {
543                 dev_dbg(pTAS2563->dev, "ti,irq-gpio=%d", pTAS2563->mnIRQGPIO);
544         }
546         of_property_read_u32(np, "ti,left-slot", &pTAS2563->mnLeftSlot);
547         if (rc) {
548                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
549                         "ti,left-slot", np->full_name, rc);
550         } else {
551                 dev_dbg(pTAS2563->dev, "ti,left-slot=%d",
552                         pTAS2563->mnLeftSlot);
553         }
555         of_property_read_u32(np, "ti,right-slot", &pTAS2563->mnRightSlot);
556         if (rc) {
557                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
558                         "ti,right-slot", np->full_name, rc);
559         } else {
560                 dev_dbg(pTAS2563->dev, "ti,right-slot=%d",
561                         pTAS2563->mnRightSlot);
562         }
564         of_property_read_u32(np, "ti,imon-slot-no", &pTAS2563->mnImon_slot_no);
565         if (rc) {
566                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
567                         "ti,imon-slot-no", np->full_name, rc);
568         } else {
569                 dev_dbg(pTAS2563->dev, "ti,imon-slot-no=%d",
570                         pTAS2563->mnImon_slot_no);
571         }
573         of_property_read_u32(np, "ti,vmon-slot-no", &pTAS2563->mnVmon_slot_no);
574         if (rc) {
575                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
576                         "ti,vmon-slot-no", np->full_name, rc);
577         } else {
578                 dev_dbg(pTAS2563->dev, "ti,vmon-slot-no=%d",
579                         pTAS2563->mnVmon_slot_no);
580         }
582         return ret;
586 static int tas2563_runtime_suspend(struct tas2563_priv *pTAS2563)
588         dev_dbg(pTAS2563->dev, "%s\n", __func__);
590         pTAS2563->mbRuntimeSuspend = true;
592         if (gpio_is_valid(pTAS2563->mnIRQGPIO)) {
593                 if (delayed_work_pending(&pTAS2563->irq_work)) {
594                         dev_dbg(pTAS2563->dev, "cancel IRQ work\n");
595                         cancel_delayed_work_sync(&pTAS2563->irq_work);
596                 }
597         }
599         return 0;
602 static int tas2563_runtime_resume(struct tas2563_priv *pTAS2563)
604         struct TProgram *pProgram;
606         dev_dbg(pTAS2563->dev, "%s\n", __func__);
607         if (!pTAS2563->mpFirmware->mpPrograms) {
608                 dev_dbg(pTAS2563->dev, "%s, firmware not loaded\n", __func__);
609                 goto end;
610         }
612         if (pTAS2563->mnCurrentProgram >= pTAS2563->mpFirmware->mnPrograms) {
613                 dev_err(pTAS2563->dev, "%s, firmware corrupted\n", __func__);
614                 goto end;
615         }
617         pProgram = &(pTAS2563->mpFirmware->mpPrograms[pTAS2563->mnCurrentProgram]);
619         pTAS2563->mbRuntimeSuspend = false;
620 end:
622         return 0;
626 /* tas2563_i2c_probe :
627 * platform dependent
628 * should implement hardware reset functionality
629 */
630 static int tas2563_i2c_probe(struct i2c_client *pClient,
631         const struct i2c_device_id *pID)
633         struct tas2563_priv *pTAS2563;
634         int nResult = 0;
635         unsigned int nValue = 0;
636         const char *pFWName;
638         dev_info(&pClient->dev, "%s enter\n", __func__);
640         pTAS2563 = devm_kzalloc(&pClient->dev, sizeof(struct tas2563_priv), GFP_KERNEL);
641         if (!pTAS2563) {
642                 nResult = -ENOMEM;
643                 goto err;
644         }
646         pTAS2563->dev = &pClient->dev;
647         i2c_set_clientdata(pClient, pTAS2563);
648         dev_set_drvdata(&pClient->dev, pTAS2563);
650         pTAS2563->regmap = devm_regmap_init_i2c(pClient, &tas2563_i2c_regmap);
651         if (IS_ERR(pTAS2563->regmap)) {
652                 nResult = PTR_ERR(pTAS2563->regmap);
653                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
654                         nResult);
655                 goto err;
656         }
658         if (pClient->dev.of_node)
659                 tas2563_parse_dt(&pClient->dev, pTAS2563);
661         if (gpio_is_valid(pTAS2563->mnResetGPIO)) {
662                 nResult = gpio_request(pTAS2563->mnResetGPIO, "TAS2563-RESET");
663                 if (nResult < 0) {
664                         dev_err(pTAS2563->dev, "%s: GPIO %d request error\n",
665                                 __func__, pTAS2563->mnResetGPIO);
666                         goto err;
667                 }
668                 tas2563_hw_reset(pTAS2563);
669         }
671         pTAS2563->read = tas2563_dev_read;
672         pTAS2563->write = tas2563_dev_write;
673         pTAS2563->bulk_read = tas2563_dev_bulk_read;
674         pTAS2563->bulk_write = tas2563_dev_bulk_write;
675         pTAS2563->update_bits = tas2563_dev_update_bits;
676         pTAS2563->enableIRQ = tas2563_enableIRQ;
677 //      pTAS2563->clearIRQ = tas2563_clearIRQ;
678         pTAS2563->hw_reset = tas2563_hw_reset;
679         pTAS2563->runtime_suspend = tas2563_runtime_suspend;
680         pTAS2563->runtime_resume = tas2563_runtime_resume;
681         pTAS2563->mnRestart = 0;
682         pTAS2563->mnPowerState = TAS2563_POWER_SHUTDOWN;
684         mutex_init(&pTAS2563->dev_lock);
686         /* Reset the chip */
687         nResult = tas2563_dev_write(pTAS2563, TAS2563_SoftwareReset, 0x01);
688         if (nResult < 0) {
689                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
690 //              goto err;
691         }
693         msleep(1);
694         nResult = tas2563_dev_read(pTAS2563, TAS2563_RevisionandPGID, &nValue);
695         pTAS2563->mnPGID = nValue;
696         dev_info(pTAS2563->dev, "PGID: %d\n", pTAS2563->mnPGID);
697         pFWName = TAS2563_FW_NAME;
699         if (gpio_is_valid(pTAS2563->mnIRQGPIO)) {
700                 nResult = gpio_request(pTAS2563->mnIRQGPIO, "TAS2563-IRQ");
701                 if (nResult < 0) {
702                         dev_err(pTAS2563->dev,
703                                 "%s: GPIO %d request INT error\n",
704                                 __func__, pTAS2563->mnIRQGPIO);
705                         goto err;
706                 }
708                 gpio_direction_input(pTAS2563->mnIRQGPIO);
709                 pTAS2563->mnIRQ = gpio_to_irq(pTAS2563->mnIRQGPIO);
710                 dev_dbg(pTAS2563->dev, "irq = %d\n", pTAS2563->mnIRQ);
711                 INIT_DELAYED_WORK(&pTAS2563->irq_work, irq_work_routine);
712                 nResult = request_threaded_irq(pTAS2563->mnIRQ, tas2563_irq_handler,
713                                         NULL, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
714                                         pClient->name, pTAS2563);
715                 if (nResult < 0) {
716                         dev_err(pTAS2563->dev,
717                                 "request_irq failed, %d\n", nResult);
718                         goto err;
719                 }
720                 disable_irq_nosync(pTAS2563->mnIRQ);
721         }
723         pTAS2563->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
724         if (!pTAS2563->mpFirmware) {
725                 nResult = -ENOMEM;
726                 goto err;
727         }
729         pTAS2563->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
730         if (!pTAS2563->mpCalFirmware) {
731                 nResult = -ENOMEM;
732                 goto err;
733         }
735 #ifdef CONFIG_TAS2563_CODEC
736         mutex_init(&pTAS2563->codec_lock);
737         nResult = tas2563_register_codec(pTAS2563);
738         if (nResult < 0) {
739                 dev_err(pTAS2563->dev,
740                         "register codec failed, %d\n", nResult);
741                 goto err;
742         }
743 #endif
745 #ifdef CONFIG_TAS2563_MISC
746         mutex_init(&pTAS2563->file_lock);
747         nResult = tas2563_register_misc(pTAS2563);
748         if (nResult < 0) {
749                 dev_err(pTAS2563->dev,
750                         "register codec failed, %d\n", nResult);
751                 goto err;
752         }
753 #endif
755 #ifdef ENABLE_TILOAD
756         tiload_driver_init(pTAS2563);
757 #endif
759         hrtimer_init(&pTAS2563->mtimerwork, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
760         pTAS2563->mtimerwork.function = timer_func;
761         //INIT_WORK(&pTAS2563->mtimerwork, timer_func);
763         request_firmware_nowait(THIS_MODULE, 1, pFWName,
764                 pTAS2563->dev, GFP_KERNEL, pTAS2563, tas2563_fw_ready);
766 err:
768         return nResult;
771 static int tas2563_i2c_remove(struct i2c_client *pClient)
773         struct tas2563_priv *pTAS2563 = i2c_get_clientdata(pClient);
775         dev_info(pTAS2563->dev, "%s\n", __func__);
777 #ifdef CONFIG_TAS2563_CODEC
778         tas2563_deregister_codec(pTAS2563);
779         mutex_destroy(&pTAS2563->codec_lock);
780 #endif
782 #ifdef CONFIG_TAS2563_MISC
783         tas2563_deregister_misc(pTAS2563);
784         mutex_destroy(&pTAS2563->file_lock);
785 #endif
787         mutex_destroy(&pTAS2563->dev_lock);
788         return 0;
791 static const struct i2c_device_id tas2563_i2c_id[] = {
792         {"tas2563", 0},
793         {}
794 };
796 MODULE_DEVICE_TABLE(i2c, tas2563_i2c_id);
798 #if defined(CONFIG_OF)
799 static const struct of_device_id tas2563_of_match[] = {
800         {.compatible = "ti,tas2563"},
801         {},
802 };
804 MODULE_DEVICE_TABLE(of, tas2563_of_match);
805 #endif
807 static struct i2c_driver tas2563_i2c_driver = {
808         .driver = {
809                         .name = "tas2563",
810                         .owner = THIS_MODULE,
811 #if defined(CONFIG_OF)
812                         .of_match_table = of_match_ptr(tas2563_of_match),
813 #endif
814                 },
815         .probe = tas2563_i2c_probe,
816         .remove = tas2563_i2c_remove,
817         .id_table = tas2563_i2c_id,
818 };
820 module_i2c_driver(tas2563_i2c_driver);
822 MODULE_AUTHOR("Texas Instruments Inc.");
823 MODULE_DESCRIPTION("TAS2563 I2C Smart Amplifier driver");
824 MODULE_LICENSE("GPL v2");
826 #endif