TAS2562 driver original release
[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
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 //Added/Modified 060356-PP
41 #include "tas2562-misc.h"
43 static int tas2562_change_book_page(struct tas2562_priv *pTAS2562,
44         int book, int page)
45 {
46         int nResult = 0;
48         if ((pTAS2562->mnCurrentBook == book)
49                 && (pTAS2562->mnCurrentPage == page))
50                 goto end;
52         if (pTAS2562->mnCurrentBook != book) {
53         nResult = regmap_write(pTAS2562->regmap, TAS2562_BOOKCTL_PAGE, 0);
54                 if (nResult < 0) {
55                         dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
56                                 __func__, __LINE__, nResult);
57                         goto end;
58                 }
59         pTAS2562->mnCurrentPage = 0;
60         nResult = regmap_write(pTAS2562->regmap, TAS2562_BOOKCTL_REG, book);
61                 if (nResult < 0) {
62                         dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
63                                 __func__, __LINE__, nResult);
64                         goto end;
65                 }
66                 pTAS2562->mnCurrentBook = book;
67         }
69         if (pTAS2562->mnCurrentPage != page) {
70         nResult = regmap_write(pTAS2562->regmap, TAS2562_BOOKCTL_PAGE, page);
71                 if (nResult < 0) {
72                         dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
73                                 __func__, __LINE__, nResult);
74                         goto end;
75                 }
76                 pTAS2562->mnCurrentPage = page;
77         }
79 end:
80         return nResult;
81 }
83 static int tas2562_dev_read(struct tas2562_priv *pTAS2562,
84         unsigned int reg, unsigned int *pValue)
85 {
86         int nResult = 0;
88         mutex_lock(&pTAS2562->dev_lock);
90         nResult = tas2562_change_book_page(pTAS2562,
91                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
92         if (nResult < 0)
93                 goto end;
95         nResult = regmap_read(pTAS2562->regmap, TAS2562_PAGE_REG(reg), pValue);
96         if (nResult < 0)
97                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
98                         __func__, __LINE__, nResult);
99         else
100                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u\n", __func__,
101                         TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
102                         TAS2562_PAGE_REG(reg));
104 end:
105         mutex_unlock(&pTAS2562->dev_lock);
106         return nResult;
109 static int tas2562_dev_write(struct tas2562_priv *pTAS2562,
110         unsigned int reg, unsigned int value)
112         int nResult = 0;
114         mutex_lock(&pTAS2562->dev_lock);
116         nResult = tas2562_change_book_page(pTAS2562,
117                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
118         if (nResult < 0)
119                 goto end;
121         nResult = regmap_write(pTAS2562->regmap, TAS2562_PAGE_REG(reg),
122                         value);
123         if (nResult < 0)
124                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
125                         __func__, __LINE__, nResult);
126         else
127                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, VAL: 0x%02x\n",
128                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
129                         TAS2562_PAGE_REG(reg), value);
131 end:
132         mutex_unlock(&pTAS2562->dev_lock);
133         return nResult;
136 static int tas2562_dev_bulk_write(struct tas2562_priv *pTAS2562,
137         unsigned int reg, unsigned char *pData, unsigned int nLength)
139         int nResult = 0;
141         mutex_lock(&pTAS2562->dev_lock);
143         nResult = tas2562_change_book_page(pTAS2562,
144                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
145         if (nResult < 0)
146                 goto end;
148         nResult = regmap_bulk_write(pTAS2562->regmap,
149                 TAS2562_PAGE_REG(reg), pData, nLength);
150         if (nResult < 0)
151                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
152                         __func__, __LINE__, nResult);
153         else
154                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, len: 0x%02x\n",
155                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
156                         TAS2562_PAGE_REG(reg), nLength);
158 end:
159         mutex_unlock(&pTAS2562->dev_lock);
160         return nResult;
163 static int tas2562_dev_bulk_read(struct tas2562_priv *pTAS2562,
164         unsigned int reg, unsigned char *pData, unsigned int nLength)
166         int nResult = 0;
168         mutex_lock(&pTAS2562->dev_lock);
170         nResult = tas2562_change_book_page(pTAS2562,
171                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
172         if (nResult < 0)
173                 goto end;
175         nResult = regmap_bulk_read(pTAS2562->regmap,
176         TAS2562_PAGE_REG(reg), pData, nLength);
177         if (nResult < 0)
178                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
179                         __func__, __LINE__, nResult);
180         else
181                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, len: 0x%02x\n",
182                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
183                         TAS2562_PAGE_REG(reg), nLength);
184 end:
185         mutex_unlock(&pTAS2562->dev_lock);
186         return nResult;
189 static int tas2562_dev_update_bits(struct tas2562_priv *pTAS2562,
190         unsigned int reg, unsigned int mask, unsigned int value)
192         int nResult = 0;
194         mutex_lock(&pTAS2562->dev_lock);
195         nResult = tas2562_change_book_page(pTAS2562,
196                 TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg));
197         if (nResult < 0)
198                 goto end;
200         nResult = regmap_update_bits(pTAS2562->regmap,
201         TAS2562_PAGE_REG(reg), mask, value);
202         if (nResult < 0)
203                 dev_err(pTAS2562->dev, "%s, ERROR, L=%d, E=%d\n",
204                         __func__, __LINE__, nResult);
205         else
206                 dev_dbg(pTAS2562->dev, "%s: BOOK:PAGE:REG %u:%u:%u, mask: 0x%x, val=0x%x\n",
207                         __func__, TAS2562_BOOK_ID(reg), TAS2562_PAGE_ID(reg),
208                         TAS2562_PAGE_REG(reg), mask, value);
209 end:
210         mutex_unlock(&pTAS2562->dev_lock);
211         return nResult;
214 static const struct reg_default tas2562_reg_defaults[] = {
215         { TAS2562_Page, 0x00 },
216         { TAS2562_SoftwareReset, 0x00 },
217         { TAS2562_PowerControl, 0x0e },
218         { TAS2562_PlaybackConfigurationReg0, 0x10 },
219         { TAS2562_PlaybackConfigurationReg1, 0x01 },
220         { TAS2562_PlaybackConfigurationReg2, 0x00 },
221         { TAS2562_MiscConfigurationReg0, 0x07 },
222         { TAS2562_TDMConfigurationReg1, 0x02 },
223         { TAS2562_TDMConfigurationReg2, 0x0a },
224         { TAS2562_TDMConfigurationReg3, 0x10 },
225         { TAS2562_InterruptMaskReg0, 0xfc },
226         { TAS2562_InterruptMaskReg1, 0xb1 },
227         { TAS2562_InterruptConfiguration, 0x05 },
228         { TAS2562_MiscIRQ, 0x81 },
229         { TAS2562_ClockConfiguration, 0x0c },
231 };
233 static bool tas2562_volatile(struct device *dev, unsigned int reg)
235         switch (reg) {
236         case TAS2562_Page: /* regmap implementation requires this */
237         case TAS2562_SoftwareReset: /* always clears after write */
238         case TAS2562_BrownOutPreventionReg0:/* has a self clearing bit */
239         case TAS2562_LiveInterruptReg0:
240         case TAS2562_LiveInterruptReg1:
241         case TAS2562_LatchedInterruptReg0:/* Sticky interrupt flags */
242         case TAS2562_LatchedInterruptReg1:/* Sticky interrupt flags */
243         case TAS2562_VBATMSB:
244         case TAS2562_VBATLSB:
245         case TAS2562_TEMPMSB:
246         case TAS2562_TEMPLSB:
247                 return true;
248         }
249         return false;
252 static bool tas2562_writeable(struct device *dev, unsigned int reg)
254         switch (reg) {
255         case TAS2562_LiveInterruptReg0:
256         case TAS2562_LiveInterruptReg1:
257         case TAS2562_LatchedInterruptReg0:
258         case TAS2562_LatchedInterruptReg1:
259         case TAS2562_VBATMSB:
260         case TAS2562_VBATLSB:
261         case TAS2562_TEMPMSB:
262         case TAS2562_TEMPLSB:
263         case TAS2562_TDMClockdetectionmonitor:
264         case TAS2562_RevisionandPGID:
265                 return false;
266         }
267         return true;
269 static const struct regmap_config tas2562_i2c_regmap = {
270         .reg_bits = 8,
271         .val_bits = 8,
272         .writeable_reg = tas2562_writeable,
273         .volatile_reg = tas2562_volatile,
274         .reg_defaults = tas2562_reg_defaults,
275         .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
276         .cache_type = REGCACHE_RBTREE,
277         .max_register = 1 * 128,
278 };
281 static void tas2562_hw_reset(struct tas2562_priv *pTAS2562)
283         if (gpio_is_valid(pTAS2562->mnResetGPIO)) {
284                 gpio_direction_output(pTAS2562->mnResetGPIO, 0);
285                 msleep(5);
286                 gpio_direction_output(pTAS2562->mnResetGPIO, 1);
287                 msleep(2);
288         }
290         pTAS2562->mnCurrentBook = -1;
291         pTAS2562->mnCurrentPage = -1;
294 void tas2562_enableIRQ(struct tas2562_priv *pTAS2562, bool enable)
296         if (enable) {
297                 if (pTAS2562->mbIRQEnable)
298                         return;
300                 if (gpio_is_valid(pTAS2562->mnIRQGPIO))
301                         enable_irq(pTAS2562->mnIRQ);
303                 schedule_delayed_work(&pTAS2562->irq_work, msecs_to_jiffies(10));
304                 pTAS2562->mbIRQEnable = true;
305         } else {
306                 if (!pTAS2562->mbIRQEnable)
307                         return;
309                 if (gpio_is_valid(pTAS2562->mnIRQGPIO))
310                         disable_irq_nosync(pTAS2562->mnIRQ);
311                 pTAS2562->mbIRQEnable = false;
312         }
315 static void irq_work_routine(struct work_struct *work)
317         struct tas2562_priv *pTAS2562 =
318                 container_of(work, struct tas2562_priv, irq_work.work);
319          unsigned int nDevInt1Status = 0, nDevInt2Status = 0; 
320 // int nCounter = 2; 
321  int nResult = 0; 
323 #ifdef CONFIG_TAS2562_CODEC
324         mutex_lock(&pTAS2562->codec_lock);
325 #endif
327 #ifdef CONFIG_TAS2562_MISC
328         mutex_lock(&pTAS2562->file_lock);
329 #endif
330         if (pTAS2562->mbRuntimeSuspend) {
331                 dev_info(pTAS2562->dev, "%s, Runtime Suspended\n", __func__);
332                 goto end;
333         }
334 /*
335         if (!pTAS2562->mbPowerUp) {
336                 dev_info(pTAS2562->dev, "%s, device not powered\n", __func__);
337                 goto end;
338         }
339 */
340 #if 1
341         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg0, 0x00);
342         if (nResult < 0)
343                 goto reload;
345         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg1, 0x00);
346         if (nResult < 0)
347                 goto reload;
348         nResult = tas2562_dev_read(pTAS2562, TAS2562_LiveInterruptReg0, &nDevInt1Status);
349         if (nResult >= 0)
350         nResult = tas2562_dev_read(pTAS2562, TAS2562_LiveInterruptReg1, &nDevInt2Status);
351         else
352                 goto reload;
354         if (((nDevInt1Status & 0xff) != 0) || ((nDevInt2Status & 0x8f) != 0)) {
355  /*in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2*/
356         dev_dbg(pTAS2562->dev, "IRQ critical Error : 0x%x, 0x%x\n",
357                         nDevInt1Status, nDevInt2Status);
359                 if (nDevInt1Status & 0x02) {
360                         pTAS2562->mnErrCode |= ERROR_OVER_CURRENT;
361                         dev_err(pTAS2562->dev, "SPK over current!\n");
362                 } else
363                         pTAS2562->mnErrCode &= ~ERROR_OVER_CURRENT;
365                 if (nDevInt1Status & 0x10) {
366                         pTAS2562->mnErrCode |= ERROR_UNDER_VOLTAGE;
367                         dev_err(pTAS2562->dev, "VBAT below limiter inflection point!\n");
368                 } else
369                         pTAS2562->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
371                 if (nDevInt1Status & 0x04) {
372                         pTAS2562->mnErrCode |= ERROR_CLK_HALT;
373                         dev_err(pTAS2562->dev, "TDM clock error!\n");
374                 } else
375                         pTAS2562->mnErrCode &= ~ERROR_CLK_HALT;
377                 if (nDevInt1Status & 0x01) {
378                         pTAS2562->mnErrCode |= ERROR_DIE_OVERTEMP;
379                         dev_err(pTAS2562->dev, "die over temperature!\n");
380                 } else
381                         pTAS2562->mnErrCode &= ~ERROR_DIE_OVERTEMP;
383                 if (nDevInt1Status & 0x80) {
384                         pTAS2562->mnErrCode |= ERROR_BROWNOUT;
385                         dev_err(pTAS2562->dev, "limiter mute!\n");
386                 } else
387                         pTAS2562->mnErrCode &= ~ERROR_BROWNOUT;
389                 if (nDevInt1Status & 0x40) {
390                         pTAS2562->mnErrCode |= ERROR_BROWNOUT;
391                         dev_err(pTAS2562->dev, "limiter infinite hold!\n");
392                 } else
393                         pTAS2562->mnErrCode &= ~ERROR_BROWNOUT;
394                 if (nDevInt1Status & 0x20) {
395                         pTAS2562->mnErrCode |= ERROR_BROWNOUT;
396                         dev_err(pTAS2562->dev, "limiter max attenuation!\n");
397                 } else
398                         pTAS2562->mnErrCode &= ~ERROR_BROWNOUT;
399                 if (nDevInt1Status & 0x04) {
400                         pTAS2562->mnErrCode |= ERROR_BROWNOUT;
401                         dev_err(pTAS2562->dev, "limiter active!\n");
402                 } else
403                         pTAS2562->mnErrCode &= ~ERROR_BROWNOUT;
404                 if (nDevInt2Status & 0x80) {
405                         pTAS2562->mnErrCode |= ERROR_CLK_DET1;
406                         dev_err(pTAS2562->dev, "PDM audio data invalid!\n");
407                 } else
408                         pTAS2562->mnErrCode &= ~ERROR_CLK_DET1;
410                 if (nDevInt2Status & 0x08) {
411                         pTAS2562->mnErrCode |= ERROR_CLK_DET2;
412                         dev_err(pTAS2562->dev, "VBAT OVLO flag!\n");
413                 } else
414                         pTAS2562->mnErrCode &= ~ERROR_CLK_DET2;
416                 if (nDevInt2Status & 0x04) {
417                         pTAS2562->mnErrCode |= ERROR_CLK_DET2;
418                         dev_err(pTAS2562->dev, "VBAT UVLO flag!\n");
419                 } else
420                         pTAS2562->mnErrCode &= ~ERROR_CLK_DET2;
422                 if (nDevInt2Status & 0x02) {
423                         pTAS2562->mnErrCode |= ERROR_CLK_DET2;
424                         dev_err(pTAS2562->dev, "VBAT brown out flag!\n");
425                 } else
426                         pTAS2562->mnErrCode &= ~ERROR_CLK_DET2;
428                 if (nDevInt2Status & 0x01) {
429                         pTAS2562->mnErrCode |= ERROR_CLK_DET2;
430                         dev_err(pTAS2562->dev, "PDM clock error!\n");
431                 } else
432                         pTAS2562->mnErrCode &= ~ERROR_CLK_DET2;
433                 goto reload;
434         } else {
435 #if 0
436                 dev_dbg(pTAS2562->dev, "IRQ status : 0x%x, 0x%x\n",
437                                 nDevInt1Status, nDevInt2Status);
438                 nCounter = 2;
440                 while (nCounter > 0) {
441                 nResult = tas2562_dev_read(pTAS2562,
442                         TAS2562_POWER_UP_FLAG_REG, &nDevInt1Status);
443                         if (nResult < 0)
444                                 goto reload;
446                         if ((nDevInt1Status & 0xc0) == 0xc0)
447                                 break;
449                         nCounter--;
450                         if (nCounter > 0) {
451         /* in case check pow status just after power on TAS2562 */
452                                 dev_dbg(pTAS2562->dev, "PowSts B: 0x%x, check again after 10ms\n",
453                                         nDevInt1Status);
454                                 msleep(20);
455                         }
456                 }
458                 if ((nDevInt1Status & 0xc0) != 0xc0) {
459                         dev_err(pTAS2562->dev, "%s, Critical ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
460                                 __func__,
461                                 TAS2562_BOOK_ID(TAS2562_POWER_UP_FLAG_REG),
462                                 TAS2562_PAGE_ID(TAS2562_POWER_UP_FLAG_REG),
463                                 TAS2562_PAGE_REG(TAS2562_POWER_UP_FLAG_REG),
464                                 nDevInt1Status);
465                         pTAS2562->mnErrCode |= ERROR_CLASSD_PWR;
466                         goto reload;
467                 }
468                 pTAS2562->mnErrCode &= ~ERROR_CLASSD_PWR;
469 #endif
470         }
472         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg0, 0xff);
473         if (nResult < 0)
474                 goto reload;
476         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptMaskReg1, 0xff);
477         if (nResult < 0)
478                 goto reload;
480         goto end;
482 reload:
483         /* hardware reset and reload */
484         //tas2562_LoadConfig(pTAS2562, true);
485 #endif
487 end:
488 /*
489         if (!hrtimer_active(&pTAS2562->mtimer)) {
490                 dev_dbg(pTAS2562->dev, "%s, start timer\n", __func__);
491                 hrtimer_start(&pTAS2562->mtimer,
492                         ns_to_ktime((u64)CHECK_PERIOD * NSEC_PER_MSEC),
493                         HRTIMER_MODE_REL);
494         }
495 */
497 #ifdef CONFIG_TAS2562_MISC
498         mutex_unlock(&pTAS2562->file_lock);
499 #endif
501 #ifdef CONFIG_TAS2562_CODEC
502         mutex_unlock(&pTAS2562->codec_lock);
503 #endif
506 static enum hrtimer_restart timer_func(struct hrtimer *timer)
508         struct tas2562_priv *pTAS2562 = container_of(timer,
509                 struct tas2562_priv, mtimer);
511         if (pTAS2562->mbPowerUp) {
512                 if (!delayed_work_pending(&pTAS2562->irq_work))
513                         schedule_delayed_work(&pTAS2562->irq_work,
514                                 msecs_to_jiffies(20));
515         }
517         return HRTIMER_NORESTART;
519 static inline void irqd_set(struct irq_data *d, unsigned int mask)
521         //Debug Addded/Modified 060356-PP
522         //__irqd_to_state(d) |= mask;
525 static void irq_state_set_masked(struct irq_desc *desc)
527         irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
530 static void irq_state_set_disabled(struct irq_desc *desc)
532         irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
535 static void irq_shutdown(struct irq_desc *desc)
537         irq_state_set_disabled(desc);
538         desc->depth = 1;
539         if (desc->irq_data.chip->irq_shutdown)
540                 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
541         else if (desc->irq_data.chip->irq_disable)
542                 desc->irq_data.chip->irq_disable(&desc->irq_data);
543         else
544                 desc->irq_data.chip->irq_mask(&desc->irq_data);
545         irq_domain_deactivate_irq(&desc->irq_data);
546         irq_state_set_masked(desc);
549 static irqreturn_t tas2562_irq_handler(int irq, void *dev_id)
551         struct tas2562_priv *pTAS2562 = (struct tas2562_priv *)dev_id;
552         struct irq_desc *desc = irq_to_desc(irq);
554 #if 1
555         /* get IRQ status after 100 ms */
556         if (!delayed_work_pending(&pTAS2562->irq_work))
557                 schedule_delayed_work(&pTAS2562->irq_work,
558                         msecs_to_jiffies(100));
559 #endif
560         /* avoid interrupt storm, mask corresponding gic interrupt controller bit*/
561         irq_shutdown(desc);
562         return IRQ_HANDLED;
565 static int tas2562_runtime_suspend(struct tas2562_priv *pTAS2562)
567         dev_dbg(pTAS2562->dev, "%s\n", __func__);
569         pTAS2562->mbRuntimeSuspend = true;
571         if (hrtimer_active(&pTAS2562->mtimer)) {
572                 dev_dbg(pTAS2562->dev, "cancel die temp timer\n");
573                 hrtimer_cancel(&pTAS2562->mtimer);
574         }
576         if (delayed_work_pending(&pTAS2562->irq_work)) {
577                 dev_dbg(pTAS2562->dev, "cancel IRQ work\n");
578                 cancel_delayed_work_sync(&pTAS2562->irq_work);
579         }
581         return 0;
584 static int tas2562_runtime_resume(struct tas2562_priv *pTAS2562)
586         dev_dbg(pTAS2562->dev, "%s\n", __func__);
588         if (pTAS2562->mbPowerUp) {
589 /*              if (!hrtimer_active(&pTAS2562->mtimer)) {
590                 dev_dbg(pTAS2562->dev, "%s, start check timer\n", __func__);
591                         hrtimer_start(&pTAS2562->mtimer,
592                                 ns_to_ktime((u64)CHECK_PERIOD * NSEC_PER_MSEC),
593                                 HRTIMER_MODE_REL);
594                 }
595 */
596         }
598         pTAS2562->mbRuntimeSuspend = false;
600         return 0;
603 static int tas2562_parse_dt(struct device *dev, struct tas2562_priv *pTAS2562)
605         struct device_node *np = dev->of_node;
606         int rc = 0, ret = 0;
608         u32 debounceInfo[2] = { 0, 0 };
609         rc = of_property_read_u32(np, "ti,asi-format", &pTAS2562->mnASIFormat);
610         if (rc) {
611                 dev_err(pTAS2562->dev, "Looking up %s property in node %s failed %d\n",
612                         "ti,asi-format", np->full_name, rc);
613         } else {
614                 dev_dbg(pTAS2562->dev, "ti,asi-format=%d",
615                         pTAS2562->mnASIFormat);
616         }
618         pTAS2562->mnResetGPIO = of_get_named_gpio(np, "ti,reset-gpio", 0);
619         if (!gpio_is_valid(pTAS2562->mnResetGPIO)) {
620                 dev_err(pTAS2562->dev, "Looking up %s property in node %s failed %d\n",
621                         "ti,reset-gpio", np->full_name, pTAS2562->mnResetGPIO);
622         } else {
623                 dev_dbg(pTAS2562->dev, "ti,reset-gpio=%d",
624                         pTAS2562->mnResetGPIO);
625         }
627         of_property_read_u32_array(np, "debounce",
628                             debounceInfo, ARRAY_SIZE(debounceInfo));
629         gpio_set_debounce(37, debounceInfo[1]);
630         pTAS2562->mnIRQGPIO = of_get_named_gpio(np, "ti,irq-gpio", 0);
631         if (!gpio_is_valid(pTAS2562->mnIRQGPIO)) {
632                 dev_err(pTAS2562->dev, "Looking up %s property in node %s failed %d\n",
633                         "ti,irq-gpio", np->full_name, pTAS2562->mnIRQGPIO);
634         } else {
635                 dev_dbg(pTAS2562->dev, "ti,irq-gpio=%d", pTAS2562->mnIRQGPIO);
636         }
638         return ret;
641 static int tas2562_i2c_probe(struct i2c_client *client,
642                         const struct i2c_device_id *id)
644         struct tas2562_priv *pTAS2562;
645         int nResult;
647         //struct pinctrl *pinctrl;
648         //struct pinctrl_state *pin_state;
649         dev_info(&client->dev, "%s enter\n", __func__);
651         pTAS2562 = devm_kzalloc(&client->dev,
652                 sizeof(struct tas2562_priv), GFP_KERNEL);
653         if (pTAS2562 == NULL) {
654                 nResult = -ENOMEM;
655                 goto end;
656         }
658         pTAS2562->dev = &client->dev;
659         i2c_set_clientdata(client, pTAS2562);
660         dev_set_drvdata(&client->dev, pTAS2562);
662         pTAS2562->regmap = devm_regmap_init_i2c(client, &tas2562_i2c_regmap);
663         if (IS_ERR(pTAS2562->regmap)) {
664                 nResult = PTR_ERR(pTAS2562->regmap);
665                 dev_err(&client->dev, "Failed to allocate register map: %d\n",
666                                         nResult);
667                 goto end;
668         }
669 #if 0
670 pinctrl = devm_pinctrl_get(&client->dev);
671 if (IS_ERR(pinctrl)) {
672             dev_err(&client->dev,
673                                        "Unable to acquire pinctrl: Error = %lu",
674                                                    (unsigned long)pinctrl);
675 } else {
676                                      /* Configure GPIO interrupt pin */
677                                      pin_state = pinctrl_lookup_state(pinctrl, "ti_int");
678                                          pinctrl_select_state(pinctrl, pin_state);
680 #endif
681 /* TODO */
682         if (client->dev.of_node)
683                 tas2562_parse_dt(&client->dev, pTAS2562);
685         if (gpio_is_valid(pTAS2562->mnResetGPIO)) {
686                 nResult = gpio_request(pTAS2562->mnResetGPIO, "TAS2562_RESET");
687                 if (nResult) {
688                         dev_err(pTAS2562->dev, "%s: Failed to request gpio %d\n",
689                                 __func__, pTAS2562->mnResetGPIO);
690                         nResult = -EINVAL;
691                         goto free_gpio;
692                 }
693         }
694 #if 0
695         if (gpio_is_valid(pTAS2562->mnIRQGPIO)) {
696                 nResult = gpio_request(pTAS2562->mnIRQGPIO, "TAS2562-IRQ");
697                 if (nResult < 0) {
698                         dev_err(pTAS2562->dev, "%s: GPIO %d request error\n",
699                                 __func__, pTAS2562->mnIRQGPIO);
700                         goto free_gpio;
701                 }
702                 gpio_direction_input(pTAS2562->mnIRQGPIO);
703 #endif
704 if (gpio_is_valid(pTAS2562->mnIRQGPIO)) {
705         nResult = devm_gpio_request_one(&client->dev, pTAS2562->mnIRQGPIO,
706                 GPIOF_DIR_IN, "TAS2562_INT");
708                 if (nResult < 0) {
709                         dev_err(pTAS2562->dev, "%s: GPIO %d request error\n",
710                                 __func__, pTAS2562->mnIRQGPIO);
711                         goto free_gpio;
712                 }
714                 pTAS2562->mnIRQ = gpio_to_irq(pTAS2562->mnIRQGPIO);
715                 dev_dbg(pTAS2562->dev, "irq = %d\n", pTAS2562->mnIRQ);
716                 nResult =devm_request_threaded_irq(&client->dev, pTAS2562->mnIRQ,
717             NULL, tas2562_irq_handler, IRQF_TRIGGER_FALLING|IRQF_ONESHOT,
718                                         client->name, pTAS2562);
719                 if (nResult < 0) {
720                         dev_err(pTAS2562->dev,
721                                 "request_irq failed, %d\n", nResult);
722                         goto free_gpio;
723                 }
724                 //disable_irq_nosync(pTAS2562->mnIRQ);
725                 enable_irq(pTAS2562->mnIRQ);
726                 INIT_DELAYED_WORK(&pTAS2562->irq_work, irq_work_routine);
727         }
729         pTAS2562->read = tas2562_dev_read;
730         pTAS2562->write = tas2562_dev_write;
731         pTAS2562->bulk_read = tas2562_dev_bulk_read;
732         pTAS2562->bulk_write = tas2562_dev_bulk_write;
733         pTAS2562->update_bits = tas2562_dev_update_bits;
734         pTAS2562->hw_reset = tas2562_hw_reset;
735         pTAS2562->enableIRQ = tas2562_enableIRQ;
736         //pTAS2562->clearIRQ = tas2562_clearIRQ;
737         pTAS2562->runtime_suspend = tas2562_runtime_suspend;
738         pTAS2562->runtime_resume = tas2562_runtime_resume;
739         mutex_init(&pTAS2562->dev_lock);
740         if (nResult < 0)
741                 goto destroy_mutex;
743         nResult = tas2562_dev_write(pTAS2562, TAS2562_InterruptConfiguration, 0x00);
744         if (nResult < 0)
745                 pr_err("Write TAS2562_InterruptConfiguration failed\n");
746 #ifdef CONFIG_TAS2562_CODEC
747         mutex_init(&pTAS2562->codec_lock);
748         tas2562_register_codec(pTAS2562);
749 #endif
751 #ifdef CONFIG_TAS2562_MISC
752         mutex_init(&pTAS2562->file_lock);
753         tas2562_register_misc(pTAS2562);
754 #endif
756         hrtimer_init(&pTAS2562->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
757         pTAS2562->mtimer.function = timer_func;
759 destroy_mutex:
760         if (nResult < 0)
761                 mutex_destroy(&pTAS2562->dev_lock);
763 free_gpio:
764         if (nResult < 0) {
765                 if (gpio_is_valid(pTAS2562->mnResetGPIO))
766                         gpio_free(pTAS2562->mnResetGPIO);
767                 if (gpio_is_valid(pTAS2562->mnIRQGPIO))
768                         gpio_free(pTAS2562->mnIRQGPIO);
769         }
771 end:
772         return nResult;
775 static int tas2562_i2c_remove(struct i2c_client *client)
777         struct tas2562_priv *pTAS2562 = i2c_get_clientdata(client);
779         dev_info(pTAS2562->dev, "%s\n", __func__);
781 #ifdef CONFIG_TAS2562_CODEC
782         tas2562_deregister_codec(pTAS2562);
783         mutex_destroy(&pTAS2562->codec_lock);
784 #endif
786 #ifdef CONFIG_TAS2562_MISC
787         tas2562_deregister_misc(pTAS2562);
788         mutex_destroy(&pTAS2562->file_lock);
789 #endif
791         if (gpio_is_valid(pTAS2562->mnResetGPIO))
792                 gpio_free(pTAS2562->mnResetGPIO);
793         if (gpio_is_valid(pTAS2562->mnIRQGPIO))
794                 gpio_free(pTAS2562->mnIRQGPIO);
796         return 0;
800 static const struct i2c_device_id tas2562_i2c_id[] = {
801         { "tas2562", 0},
802         { }
803 };
804 MODULE_DEVICE_TABLE(i2c, tas2562_i2c_id);
806 #if defined(CONFIG_OF)
807 static const struct of_device_id tas2562_of_match[] = {
808         { .compatible = "ti,tas2562" },
809         {},
810 };
811 MODULE_DEVICE_TABLE(of, tas2562_of_match);
812 #endif
815 static struct i2c_driver tas2562_i2c_driver = {
816         .driver = {
817                 .name   = "tas2562",
818                 .owner  = THIS_MODULE,
819 #if defined(CONFIG_OF)
820                 .of_match_table = of_match_ptr(tas2562_of_match),
821 #endif
822         },
823         .probe      = tas2562_i2c_probe,
824         .remove     = tas2562_i2c_remove,
825         .id_table   = tas2562_i2c_id,
826 };
828 module_i2c_driver(tas2562_i2c_driver);
830 MODULE_AUTHOR("Texas Instruments Inc.");
831 MODULE_DESCRIPTION("TAS2562 I2C Smart Amplifier driver");
832 MODULE_LICENSE("GPL v2");
833 #endif