Fix TAS2558 compile issue
[tas256xsw-android/tas2562-android-driver.git] / tas2562-regmap.c
1 /*
2  * ALSA SoC Texas Instruments TAS2562 High Performance 4W Smart Amplifier
3  *
4  * Copyright (C) 2016 Texas Instruments, Inc.
5  *
6  * Author: saiprasad
7  *
8  * This package is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  */
17 #ifdef CONFIG_TAS2562_REGMAP
19 #define DEBUG 5
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/pm.h>
26 #include <linux/i2c.h>
27 #include <linux/gpio.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/firmware.h>
30 #include <linux/regmap.h>
31 #include <linux/of.h>
32 #include <linux/of_gpio.h>
33 #include <linux/slab.h>
34 #include <sound/soc.h>
35 #include <linux/interrupt.h>
36 #include <linux/irq.h>
38 #include "tas2562.h"
39 #include "tas2562-codec.h"
40 #include "tas2562-misc.h"
42 static char pICN[] = {0x00, 0x03, 0x46, 0xdc};
44 static int tas2562_change_book_page(struct tas2562_priv *pTAS2562,
45         int book, int page)
46 {
47         int nResult = 0;
49         if ((pTAS2562->mnCurrentBook == book)
50                 && (pTAS2562->mnCurrentPage == page))
51                 goto end;
53         if (pTAS2562->mnCurrentBook != book) {
54                 nResult = regmap_write(pTAS2562->regmap, TAS2562_BOOKCTL_PAGE, 0);
55                 if (nResult < 0) {
56                         dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
57                                 __func__, __LINE__, nResult);
58                         goto end;
59                 }
60                 pTAS2562->mnCurrentPage = 0;
61                 nResult = regmap_write(pTAS2562->regmap, TAS2562_BOOKCTL_REG, book);
62                 if (nResult < 0) {
63                         dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
64                                 __func__, __LINE__, nResult);
65                         goto end;
66                 }
67                 pTAS2562->mnCurrentBook = book;
68         }
70         if (pTAS2562->mnCurrentPage != page) {
71                 nResult = regmap_write(pTAS2562->regmap, TAS2562_BOOKCTL_PAGE, page);
72                 if (nResult < 0) {
73                         dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
74                                 __func__, __LINE__, nResult);
75                         goto end;
76                 }
77                 pTAS2562->mnCurrentPage = page;
78         }
80 end:
81         return nResult;
82 }
84 static int tas2562_dev_read(struct tas2562_priv *pTAS2562,
85         unsigned int reg, unsigned int *pValue)
86 {
87         int nResult = 0;
89         mutex_lock(&pTAS2562->dev_lock);
91         nResult = tas2562_change_book_page(pTAS2562,
92                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
93         if (nResult < 0)
94                 goto end;
96         nResult = regmap_read(pTAS2562->regmap, TAS2562_PAGE_REG(reg), pValue);
97         if (nResult < 0)
98                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
99                         __func__, __LINE__, nResult);
100         else
101                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u\n", __func__,
102                         TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
103                         TAS2562_PAGE_REG(reg));
105 end:
106         mutex_unlock(&pTAS2562->dev_lock);
107         return nResult;
110 static int tas2562_dev_write(struct tas2562_priv *pTAS2562,
111         unsigned int reg, unsigned int value)
113         int nResult = 0;
115         mutex_lock(&pTAS2562->dev_lock);
117         nResult = tas2562_change_book_page(pTAS2562,
118                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
119         if (nResult < 0)
120                 goto end;
122         nResult = regmap_write(pTAS2562->regmap, TAS2562_PAGE_REG(reg),
123                         value);
124         if (nResult < 0)
125                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
126                         __func__, __LINE__, nResult);
127         else
128                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, VAL: 0x%02x\n",
129                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
130                         TAS2562_PAGE_REG(reg), value);
132 end:
133         mutex_unlock(&pTAS2562->dev_lock);
134         return nResult;
137 static int tas2562_dev_bulk_write(struct tas2562_priv *pTAS2562,
138         unsigned int reg, unsigned char *pData, unsigned int nLength)
140         int nResult = 0;
142         mutex_lock(&pTAS2562->dev_lock);
144         nResult = tas2562_change_book_page(pTAS2562,
145                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
146         if (nResult < 0)
147                 goto end;
149         nResult = regmap_bulk_write(pTAS2562->regmap,
150                 TAS2562_PAGE_REG(reg), pData, nLength);
151         if (nResult < 0)
152                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
153                         __func__, __LINE__, nResult);
154         else
155                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, len: 0x%02x\n",
156                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
157                         TAS2562_PAGE_REG(reg), nLength);
159 end:
160         mutex_unlock(&pTAS2562->dev_lock);
161         return nResult;
164 static int tas2562_dev_bulk_read(struct tas2562_priv *pTAS2562,
165         unsigned int reg, unsigned char *pData, unsigned int nLength)
167         int nResult = 0;
169         mutex_lock(&pTAS2562->dev_lock);
171         nResult = tas2562_change_book_page(pTAS2562,
172                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
173         if (nResult < 0)
174                 goto end;
176         nResult = regmap_bulk_read(pTAS2562->regmap,
177         TAS2562_PAGE_REG(reg), pData, nLength);
178         if (nResult < 0)
179                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
180                         __func__, __LINE__, nResult);
181         else
182                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, len: 0x%02x\n",
183                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
184                         TAS2562_PAGE_REG(reg), nLength);
185 end:
186         mutex_unlock(&pTAS2562->dev_lock);
187         return nResult;
190 static int tas2562_dev_update_bits(struct tas2562_priv *pTAS2562,
191         unsigned int reg, unsigned int mask, unsigned int value)
193         int nResult = 0;
195         mutex_lock(&pTAS2562->dev_lock);
196         nResult = tas2562_change_book_page(pTAS2562,
197                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
198         if (nResult < 0)
199                 goto end;
201         nResult = regmap_update_bits(pTAS2562->regmap,
202         TAS2562_PAGE_REG(reg), mask, value);
203         if (nResult < 0)
204                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
205                         __func__, __LINE__, nResult);
206         else
207                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, mask: 0x%x, val=0x%x\n",
208                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
209                         TAS2562_PAGE_REG(reg), mask, value);
210 end:
211         mutex_unlock(&pTAS2562->dev_lock);
212         return nResult;
215 static bool tas2562_volatile(struct device *dev, unsigned int reg)
217         return true;
220 static bool tas2562_writeable(struct device *dev, unsigned int reg)
222         return true;
224 static const struct regmap_config tas2562_i2c_regmap = {
225         .reg_bits = 8,
226         .val_bits = 8,
227         .writeable_reg = tas2562_writeable,
228         .volatile_reg = tas2562_volatile,
229         .cache_type = REGCACHE_NONE,
230         .max_register = 1 * 128,
231 };
234 static void tas2562_hw_reset(struct tas2562_priv *pTAS2562)
236         if (gpio_is_valid(pTAS2562->mnResetGPIO)) {
237                 gpio_direction_output(pTAS2562->mnResetGPIO, 0);
238                 msleep(5);
239                 gpio_direction_output(pTAS2562->mnResetGPIO, 1);
240                 msleep(2);
241         }
242         dev_err(pTAS2562->dev, "gpio up !!\n");
244         pTAS2562->mnCurrentBook = -1;
245         pTAS2562->mnCurrentPage = -1;
248 void tas2562_enableIRQ(struct tas2562_priv *pTAS2562, bool enable)
250         if (enable) {
251                 if (pTAS2562->mbIRQEnable)
252                         return;
254                 if (gpio_is_valid(pTAS2562->mnIRQGPIO))
255                         enable_irq(pTAS2562->mnIRQ);
257                 schedule_delayed_work(&pTAS2562->irq_work, msecs_to_jiffies(10));
259                 pTAS2562->mbIRQEnable = true;
260         } else {
261                 if (gpio_is_valid(pTAS2562->mnIRQGPIO))
262                         disable_irq_nosync(pTAS2562->mnIRQ);
263                 pTAS2562->mbIRQEnable = false;
264         }
267 static void irq_work_routine(struct work_struct *work)
269         struct tas2562_priv *pTAS2562 =
270                 container_of(work, struct tas2562_priv, irq_work.work);
271         unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
272         int nCounter = 2;
273         int nResult = 0;
274         int irqreg;
276         dev_info(pTAS2562->dev, "%s\n", __func__);
277 #ifdef CONFIG_TAS2562_CODEC
278         mutex_lock(&pTAS2562->codec_lock);
279 #endif
281         if (pTAS2562->mbRuntimeSuspend) {
282                 dev_info(pTAS2562->dev, "%s, Runtime Suspended\n", __func__);
283                 goto end;
284         }
286         if (pTAS2562->mnPowerState == TAS2562_POWER_SHUTDOWN) {
287                 dev_info(pTAS2562->dev, "%s, device not powered\n", __func__);
288                 goto end;
289         }
291         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg0,
292                                 TAS2562_InterruptMaskReg0_Disable);
293         if (nResult < 0)
294                 goto reload;
295         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg1,
296                                 TAS2562_InterruptMaskReg1_Disable);
297         if (nResult < 0)
298                 goto reload;
300         nResult = tas2562_dev_read(pTAS2562, TAS2562_LatchedInterruptReg0, &nDevInt1Status);
301         if (nResult >= 0)
302                 nResult = tas2562_dev_read(pTAS2562, TAS2562_LatchedInterruptReg1, &nDevInt2Status);
303         else
304                 goto reload;
306         dev_dbg(pTAS2562->dev, "IRQ status : 0x%x, 0x%x\n",
307                         nDevInt1Status, nDevInt2Status);
309         if (((nDevInt1Status & 0x7) != 0) || ((nDevInt2Status & 0x0f) != 0)) {
310                 /* in case of INT_OC, INT_OT, INT_OVLT, INT_UVLT, INT_BO */
312                 if (nDevInt1Status & TAS2562_LatchedInterruptReg0_OCEFlagSticky_Interrupt) {
313                         pTAS2562->mnErrCode |= ERROR_OVER_CURRENT;
314                         dev_err(pTAS2562->dev, "SPK over current!\n");
315                 } else
316                         pTAS2562->mnErrCode &= ~ERROR_OVER_CURRENT;
318                 if (nDevInt1Status & TAS2562_LatchedInterruptReg0_OTEFlagSticky_Interrupt) {
319                         pTAS2562->mnErrCode |= ERROR_DIE_OVERTEMP;
320                         dev_err(pTAS2562->dev, "die over temperature!\n");
321                 } else
322                         pTAS2562->mnErrCode &= ~ERROR_DIE_OVERTEMP;
324                 if (nDevInt2Status & TAS2562_LatchedInterruptReg1_VBATOVLOSticky_Interrupt) {
325                         pTAS2562->mnErrCode |= ERROR_OVER_VOLTAGE;
326                         dev_err(pTAS2562->dev, "SPK over voltage!\n");
327                 } else
328                         pTAS2562->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
330                 if (nDevInt2Status & TAS2562_LatchedInterruptReg1_VBATUVLOSticky_Interrupt) {
331                         pTAS2562->mnErrCode |= ERROR_UNDER_VOLTAGE;
332                         dev_err(pTAS2562->dev, "SPK under voltage!\n");
333                 } else
334                         pTAS2562->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
336                 if (nDevInt2Status & TAS2562_LatchedInterruptReg1_BrownOutFlagSticky_Interrupt) {
337                         pTAS2562->mnErrCode |= ERROR_BROWNOUT;
338                         dev_err(pTAS2562->dev, "brownout!\n");
339                 } else
340                         pTAS2562->mnErrCode &= ~ERROR_BROWNOUT;
342                 dev_err(pTAS2562->dev, "before goto reload\n");
343                 goto reload;
344         } else {
345                 nCounter = 2;
347                 while (nCounter > 0) {
348                         nResult = tas2562_dev_read(pTAS2562, TAS2562_PowerControl, &nDevInt1Status);
349                         if (nResult < 0)
350                                 goto reload;
352                         if ((nDevInt1Status & TAS2562_PowerControl_OperationalMode10_Mask)
353                                 != TAS2562_PowerControl_OperationalMode10_Shutdown)
354                                 break;
356                         pTAS2562->read(pTAS2562, TAS2562_LatchedInterruptReg0, &irqreg);
357                         dev_info(pTAS2562->dev, "IRQ reg is: %s %d, %d\n", __func__, irqreg, __LINE__);
359                         nResult = pTAS2562->update_bits(pTAS2562, TAS2562_PowerControl,
360                                 TAS2562_PowerControl_OperationalMode10_Mask,
361                                 TAS2562_PowerControl_OperationalMode10_Active);
362                         if (nResult < 0)
363                                 goto reload;
365                         dev_info(pTAS2562->dev, "set ICN to -80dB\n");
366                         nResult = pTAS2562->bulk_write(pTAS2562, TAS2562_ICN_REG, pICN, 4);
368                         pTAS2562->read(pTAS2562, TAS2562_LatchedInterruptReg0, &irqreg);
369                         dev_info(pTAS2562->dev, "IRQ reg is: %s, %d, %d\n", __func__, irqreg, __LINE__);
371                         nCounter--;
372                         if (nCounter > 0) {
373                                 /* in case check pow status just after power on TAS2562 */
374                                 dev_dbg(pTAS2562->dev, "PowSts B: 0x%x, check again after 10ms\n",
375                                         nDevInt1Status);
376                                 msleep(10);
377                         }
378                 }
380                 if ((nDevInt1Status & TAS2562_PowerControl_OperationalMode10_Mask)
381                         == TAS2562_PowerControl_OperationalMode10_Shutdown) {
382                         dev_err(pTAS2562->dev, "%s, Critical ERROR REG[0x%x] = 0x%x\n",
383                                 __func__,
384                                 TAS2562_PowerControl,
385                                 nDevInt1Status);
386                         pTAS2562->mnErrCode |= ERROR_CLASSD_PWR;
387                         goto reload;
388                 }
389                 pTAS2562->mnErrCode &= ~ERROR_CLASSD_PWR;
390         }
392         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg0, 0xf8);
393         if (nResult < 0)
394                 goto reload;
396         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg1, 0xb1);
397         if (nResult < 0)
398                 goto reload;
400         goto end;
402 reload:
403         /* hardware reset and reload */
404         nResult = -1;
405         tas2562_LoadConfig(pTAS2562);
407 end:
408         if (nResult >= 0) {
409                 tas2562_enableIRQ(pTAS2562, true);
410             dev_info(pTAS2562->dev, "enable IRQ\n");
411         }
413         nResult = gpio_get_value(pTAS2562->mnIRQGPIO);
414         dev_info(pTAS2562->dev, "%s, irq GPIO state: %d\n", __func__, nResult);
416         if (!hrtimer_active(&pTAS2562->mtimer)) {
417                 dev_dbg(pTAS2562->dev, "%s, start timer\n", __func__);
418                 hrtimer_start(&pTAS2562->mtimer,
419                         ns_to_ktime((u64)CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
420         } else
421                 dev_info(pTAS2562->dev, "timer not active\n");
423 #ifdef CONFIG_TAS2562_CODEC
424         mutex_unlock(&pTAS2562->codec_lock);
425 #endif
428 static enum hrtimer_restart timer_func(struct hrtimer *timer)
430         struct tas2562_priv *pTAS2562 = container_of(timer,
431                 struct tas2562_priv, mtimer);
433         if (pTAS2562->mbPowerUp) {
434                 if (!delayed_work_pending(&pTAS2562->irq_work))
435                         schedule_delayed_work(&pTAS2562->irq_work,
436                                 msecs_to_jiffies(20));
437         }
439         return HRTIMER_NORESTART;
442 static irqreturn_t tas2562_irq_handler(int irq, void *dev_id)
444         struct tas2562_priv *pTAS2562 = (struct tas2562_priv *)dev_id;
446         tas2562_enableIRQ(pTAS2562, false);
447         /* get IRQ status after 100 ms */
448         schedule_delayed_work(&pTAS2562->irq_work, msecs_to_jiffies(100));
449         return IRQ_HANDLED;
452 static int tas2562_runtime_suspend(struct tas2562_priv *pTAS2562)
454         dev_dbg(pTAS2562->dev, "%s\n", __func__);
456         pTAS2562->mbRuntimeSuspend = true;
458         if (hrtimer_active(&pTAS2562->mtimer)) {
459                 dev_dbg(pTAS2562->dev, "cancel timer\n");
460                 hrtimer_cancel(&pTAS2562->mtimer);
461         }
463         if (delayed_work_pending(&pTAS2562->irq_work)) {
464                 dev_dbg(pTAS2562->dev, "cancel IRQ work\n");
465                 cancel_delayed_work_sync(&pTAS2562->irq_work);
466         }
468         return 0;
471 static int tas2562_runtime_resume(struct tas2562_priv *pTAS2562)
473         dev_dbg(pTAS2562->dev, "%s\n", __func__);
475         if (pTAS2562->mbPowerUp) {
476                 if (!hrtimer_active(&pTAS2562->mtimer)) {
477                 dev_dbg(pTAS2562->dev, "%s, start check timer\n", __func__);
478                         hrtimer_start(&pTAS2562->mtimer,
479                                 ns_to_ktime((u64)CHECK_PERIOD * NSEC_PER_MSEC),
480                                 HRTIMER_MODE_REL);
481                 }
482         }
484         pTAS2562->mbRuntimeSuspend = false;
486         return 0;
489 static int tas2562_parse_dt(struct device *dev, struct tas2562_priv *pTAS2562)
491         struct device_node *np = dev->of_node;
492         int rc = 0, ret = 0;
494 //      u32 debounceInfo[2] = { 0, 0 };
495         rc = of_property_read_u32(np, "ti,asi-format", &pTAS2562->mnASIFormat);
496         if (rc) {
497                 dev_err(pTAS2562->dev, "Looking up %s property in node %s failed %d\n",
498                         "ti,asi-format", np->full_name, rc);
499         } else {
500                 dev_dbg(pTAS2562->dev, "ti,asi-format=%d",
501                         pTAS2562->mnASIFormat);
502         }
504         pTAS2562->mnResetGPIO = of_get_named_gpio(np, "ti,reset-gpio", 0);
505         if (!gpio_is_valid(pTAS2562->mnResetGPIO)) {
506                 dev_err(pTAS2562->dev, "Looking up %s property in node %s failed %d\n",
507                         "ti,reset-gpio", np->full_name, pTAS2562->mnResetGPIO);
508         } else {
509                 dev_dbg(pTAS2562->dev, "ti,reset-gpio=%d",
510                         pTAS2562->mnResetGPIO);
511         }
513         pTAS2562->mnIRQGPIO = of_get_named_gpio(np, "ti,irq-gpio", 0);
514         if (!gpio_is_valid(pTAS2562->mnIRQGPIO)) {
515                 dev_err(pTAS2562->dev, "Looking up %s property in node %s failed %d\n",
516                         "ti,irq-gpio", np->full_name, pTAS2562->mnIRQGPIO);
517         } else {
518                 dev_dbg(pTAS2562->dev, "ti,irq-gpio=%d", pTAS2562->mnIRQGPIO);
519         }
521         return ret;
524 static int tas2562_i2c_probe(struct i2c_client *pClient,
525                         const struct i2c_device_id *id)
527         struct tas2562_priv *pTAS2562;
528         int nResult;
530         dev_err(&pClient->dev, "Driver ID: %s\n", TAS2562_DRIVER_ID);
531         dev_info(&pClient->dev, "%s enter\n", __func__);
533         pTAS2562 = devm_kzalloc(&pClient->dev,
534                 sizeof(struct tas2562_priv), GFP_KERNEL);
535         if (pTAS2562 == NULL) {
536                 dev_err(&pClient->dev, "failed to get i2c device\n");
537                 nResult = -ENOMEM;
538                 goto err;
539         }
541         pTAS2562->dev = &pClient->dev;
542         i2c_set_clientdata(pClient, pTAS2562);
543         dev_set_drvdata(&pClient->dev, pTAS2562);
545         pTAS2562->regmap = devm_regmap_init_i2c(pClient, &tas2562_i2c_regmap);
546         if (IS_ERR(pTAS2562->regmap)) {
547                 nResult = PTR_ERR(pTAS2562->regmap);
548                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
549                                         nResult);
550                 goto err;
551         }
553         if (pClient->dev.of_node)
554                 tas2562_parse_dt(&pClient->dev, pTAS2562);
556         if (gpio_is_valid(pTAS2562->mnResetGPIO)) {
557                 nResult = gpio_request(pTAS2562->mnResetGPIO, "TAS2562_RESET");
558                 if (nResult) {
559                         dev_err(pTAS2562->dev, "%s: Failed to request gpio %d\n",
560                                 __func__, pTAS2562->mnResetGPIO);
561                         nResult = -EINVAL;
562                         goto err;
563                 }
564                 tas2562_hw_reset(pTAS2562);
565         }
567         pTAS2562->read = tas2562_dev_read;
568         pTAS2562->write = tas2562_dev_write;
569         pTAS2562->bulk_read = tas2562_dev_bulk_read;
570         pTAS2562->bulk_write = tas2562_dev_bulk_write;
571         pTAS2562->update_bits = tas2562_dev_update_bits;
572         pTAS2562->hw_reset = tas2562_hw_reset;
573         pTAS2562->enableIRQ = tas2562_enableIRQ;
574         //pTAS2562->clearIRQ = tas2562_clearIRQ;
575         pTAS2562->runtime_suspend = tas2562_runtime_suspend;
576         pTAS2562->runtime_resume = tas2562_runtime_resume;
577         pTAS2562->mnPowerState = TAS2562_POWER_SHUTDOWN;
579         mutex_init(&pTAS2562->dev_lock);
581         /* Reset the chip */
582         nResult = tas2562_dev_write(pTAS2562, TAS2562_SoftwareReset, 0x01);
583         if (nResult < 0) {
584                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
585                 goto err;
586         }
588         if (gpio_is_valid(pTAS2562->mnIRQGPIO)) {
589                 nResult = gpio_request(pTAS2562->mnIRQGPIO, "TAS2562-IRQ");
590                 if (nResult < 0) {
591                         dev_err(pTAS2562->dev, "%s: GPIO %d request error\n",
592                                 __func__, pTAS2562->mnIRQGPIO);
593                         goto err;
594                 }
595                 gpio_direction_input(pTAS2562->mnIRQGPIO);
596                 nResult = gpio_get_value(pTAS2562->mnIRQGPIO);
597                 dev_info(pTAS2562->dev, "irq GPIO state: %d\n", nResult);
599                 pTAS2562->mnIRQ = gpio_to_irq(pTAS2562->mnIRQGPIO);
600                 dev_dbg(pTAS2562->dev, "irq = %d\n", pTAS2562->mnIRQ);
601                 INIT_DELAYED_WORK(&pTAS2562->irq_work, irq_work_routine);
602                 nResult = request_threaded_irq(pTAS2562->mnIRQ, tas2562_irq_handler,
603                                 NULL, IRQF_TRIGGER_LOW|IRQF_ONESHOT,
604                                 pClient->name, pTAS2562);
605                 if (nResult < 0) {
606                         dev_err(pTAS2562->dev,
607                                 "request_irq failed, %d\n", nResult);
608                         goto err;
609                 }
610                 disable_irq_nosync(pTAS2562->mnIRQ);
611         }
613 #ifdef CONFIG_TAS2562_CODEC
614         mutex_init(&pTAS2562->codec_lock);
615         nResult = tas2562_register_codec(pTAS2562);
616         if (nResult < 0) {
617                 dev_err(pTAS2562->dev,
618                         "register codec failed, %d\n", nResult);
619                 goto err;
620         }
621 #endif
623 #ifdef CONFIG_TAS2562_MISC
624         mutex_init(&pTAS2562->file_lock);
625         nResult = tas2562_register_misc(pTAS2562);
626         if (nResult < 0) {
627                 dev_err(pTAS2562->dev,
628                         "register codec failed, %d\n", nResult);
629                 goto err;
630         }
631 #endif
633         hrtimer_init(&pTAS2562->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
634         pTAS2562->mtimer.function = timer_func;
636 err:
637         return nResult;
640 static int tas2562_i2c_remove(struct i2c_client *pClient)
642         struct tas2562_priv *pTAS2562 = i2c_get_clientdata(pClient);
644         dev_info(pTAS2562->dev, "%s\n", __func__);
646 #ifdef CONFIG_TAS2562_CODEC
647         tas2562_deregister_codec(pTAS2562);
648         mutex_destroy(&pTAS2562->codec_lock);
649 #endif
651 #ifdef CONFIG_TAS2562_MISC
652         tas2562_deregister_misc(pTAS2562);
653         mutex_destroy(&pTAS2562->file_lock);
654 #endif
656         if (gpio_is_valid(pTAS2562->mnResetGPIO))
657                 gpio_free(pTAS2562->mnResetGPIO);
658         if (gpio_is_valid(pTAS2562->mnIRQGPIO))
659                 gpio_free(pTAS2562->mnIRQGPIO);
661         return 0;
665 static const struct i2c_device_id tas2562_i2c_id[] = {
666         { "tas2562", 0},
667         { }
668 };
669 MODULE_DEVICE_TABLE(i2c, tas2562_i2c_id);
671 #if defined(CONFIG_OF)
672 static const struct of_device_id tas2562_of_match[] = {
673         { .compatible = "ti,tas2562" },
674         {},
675 };
676 MODULE_DEVICE_TABLE(of, tas2562_of_match);
677 #endif
680 static struct i2c_driver tas2562_i2c_driver = {
681         .driver = {
682                 .name   = "tas2562",
683                 .owner  = THIS_MODULE,
684 #if defined(CONFIG_OF)
685                 .of_match_table = of_match_ptr(tas2562_of_match),
686 #endif
687         },
688         .probe      = tas2562_i2c_probe,
689         .remove     = tas2562_i2c_remove,
690         .id_table   = tas2562_i2c_id,
691 };
693 module_i2c_driver(tas2562_i2c_driver);
695 MODULE_AUTHOR("Texas Instruments Inc.");
696 MODULE_DESCRIPTION("TAS2562 I2C Smart Amplifier driver");
697 MODULE_LICENSE("GPL v2");
698 #endif