2 ** =============================================================================
3 ** Copyright (c) 2016 Texas Instruments Inc.
4 ** Copyright (C) 2017 XiaoMi, Inc.
6 ** This program is free software; you can redistribute it and/or modify it under
7 ** the terms of the GNU General Public License as published by the Free Software
8 ** Foundation; version 2.
10 ** This program is distributed in the hope that it will be useful, but WITHOUT
11 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ** I2C driver with regmap for Texas Instruments TAS2559 High Performance 4W Smart Amplifier
20 ** =============================================================================
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
28 #include <linux/i2c.h>
29 #include <linux/gpio.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/firmware.h>
32 #include <linux/regmap.h>
34 #include <linux/of_gpio.h>
35 #include <linux/slab.h>
36 #include <linux/syscalls.h>
37 #include <linux/fcntl.h>
38 #include <linux/uaccess.h>
39 #include <linux/interrupt.h>
40 #include <soc/qcom/socinfo.h>
42 #include "tas2559-core.h"
45 #ifdef CONFIG_TAS2559_CODEC
46 #include "tas2559-codec.h"
49 #ifdef CONFIG_TAS2559_MISC
50 #include "tas2559-misc.h"
59 * tas2559_i2c_write_device : write single byte to device
60 * platform dependent, need platform specific support
62 static int tas2559_i2c_write_device(struct tas2559_priv *pTAS2559,
69 pTAS2559->client->addr = addr;
70 nResult = regmap_write(pTAS2559->mpRegmap, reg, value);
73 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
74 __func__, addr, nResult);
80 * tas2559_i2c_bulkwrite_device : write multiple bytes to device
81 * platform dependent, need platform specific support
83 static int tas2559_i2c_bulkwrite_device(struct tas2559_priv *pTAS2559,
91 pTAS2559->client->addr = addr;
92 nResult = regmap_bulk_write(pTAS2559->mpRegmap, reg, pBuf, len);
95 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
96 __func__, addr, nResult);
102 * tas2559_i2c_read_device : read single byte from device
103 * platform dependent, need platform specific support
105 static int tas2559_i2c_read_device(struct tas2559_priv *pTAS2559,
108 unsigned char *p_value)
111 unsigned int val = 0;
113 pTAS2559->client->addr = addr;
114 nResult = regmap_read(pTAS2559->mpRegmap, reg, &val);
117 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
118 __func__, addr, nResult);
120 *p_value = (unsigned char)val;
126 * tas2559_i2c_bulkread_device : read multiple bytes from device
127 * platform dependent, need platform specific support
129 static int tas2559_i2c_bulkread_device(struct tas2559_priv *pTAS2559,
132 unsigned char *p_value,
137 pTAS2559->client->addr = addr;
138 nResult = regmap_bulk_read(pTAS2559->mpRegmap, reg, p_value, len);
141 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
142 __func__, addr, nResult);
147 static int tas2559_i2c_update_bits(struct tas2559_priv *pTAS2559,
155 pTAS2559->client->addr = addr;
156 nResult = regmap_update_bits(pTAS2559->mpRegmap, reg, mask, value);
159 dev_err(pTAS2559->dev, "%s[0x%x] Error %d\n",
160 __func__, addr, nResult);
166 * tas2559_change_book_page : switch to certain book and page
167 * platform independent, don't change unless necessary
169 static int tas2559_change_book_page(struct tas2559_priv *pTAS2559,
177 if (pTAS2559->mnDevACurrentBook == nBook) {
178 if (pTAS2559->mnDevACurrentPage != nPage) {
179 nResult = tas2559_i2c_write_device(pTAS2559,
180 pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_PAGE, nPage);
183 pTAS2559->mnDevACurrentPage = nPage;
186 nResult = tas2559_i2c_write_device(pTAS2559,
187 pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_PAGE, 0);
190 pTAS2559->mnDevACurrentPage = 0;
191 nResult = tas2559_i2c_write_device(pTAS2559,
192 pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_REG, nBook);
193 pTAS2559->mnDevACurrentBook = nBook;
196 nResult = tas2559_i2c_write_device(pTAS2559,
197 pTAS2559->mnDevAAddr, TAS2559_BOOKCTL_PAGE, nPage);
198 pTAS2559->mnDevACurrentPage = nPage;
205 if (pTAS2559->mnDevBCurrentBook == nBook) {
206 if (pTAS2559->mnDevBCurrentPage != nPage) {
207 nResult = tas2559_i2c_write_device(pTAS2559,
208 pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_PAGE, nPage);
211 pTAS2559->mnDevBCurrentPage = nPage;
214 nResult = tas2559_i2c_write_device(pTAS2559,
215 pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_PAGE, 0);
218 pTAS2559->mnDevBCurrentPage = 0;
219 nResult = tas2559_i2c_write_device(pTAS2559,
220 pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_REG, nBook);
221 pTAS2559->mnDevBCurrentBook = nBook;
224 tas2559_i2c_write_device(pTAS2559,
225 pTAS2559->mnDevBAddr, TAS2559_BOOKCTL_PAGE, nPage);
226 pTAS2559->mnDevBCurrentPage = nPage;
237 * platform independent, don't change unless necessary
239 static int tas2559_dev_read(struct tas2559_priv *pTAS2559,
241 unsigned int nRegister,
242 unsigned int *pValue)
245 unsigned char Value = 0;
247 mutex_lock(&pTAS2559->dev_lock);
249 if (pTAS2559->mbTILoadActive) {
250 if (!(nRegister & 0x80000000)) {
251 /* let only reads from TILoad pass. */
255 nRegister &= ~0x80000000;
257 dev_dbg(pTAS2559->dev, "TiLoad R CH[%d] REG B[%d]P[%d]R[%d]\n",
259 TAS2559_BOOK_ID(nRegister),
260 TAS2559_PAGE_ID(nRegister),
261 TAS2559_PAGE_REG(nRegister));
264 nResult = tas2559_change_book_page(pTAS2559, chn,
265 TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
269 nResult = tas2559_i2c_read_device(pTAS2559,
270 pTAS2559->mnDevAAddr, TAS2559_PAGE_REG(nRegister), &Value);
273 nResult = tas2559_i2c_read_device(pTAS2559,
274 pTAS2559->mnDevBAddr, TAS2559_PAGE_REG(nRegister), &Value);
276 dev_err(pTAS2559->dev, "%s, read chn ERROR %d\n", __func__, chn);
286 mutex_unlock(&pTAS2559->dev_lock);
291 * tas2559_dev_write :
292 * platform independent, don't change unless necessary
294 static int tas2559_dev_write(struct tas2559_priv *pTAS2559,
296 unsigned int nRegister,
301 mutex_lock(&pTAS2559->dev_lock);
303 if ((nRegister == 0xAFFEAFFE) && (nValue == 0xBABEBABE)) {
304 pTAS2559->mbTILoadActive = true;
305 dev_dbg(pTAS2559->dev, "TiLoad Active\n");
309 if ((nRegister == 0xBABEBABE) && (nValue == 0xAFFEAFFE)) {
310 pTAS2559->mbTILoadActive = false;
311 dev_dbg(pTAS2559->dev, "TiLoad DeActive\n");
315 if (pTAS2559->mbTILoadActive) {
316 if (!(nRegister & 0x80000000)) {
317 /* let only writes from TILoad pass. */
321 nRegister &= ~0x80000000;
322 dev_dbg(pTAS2559->dev, "TiLoad W CH[%d] REG B[%d]P[%d]R[%d] =0x%x\n",
323 chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
324 TAS2559_PAGE_REG(nRegister), nValue);
327 nResult = tas2559_change_book_page(pTAS2559,
328 chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
332 nResult = tas2559_i2c_write_device(pTAS2559,
333 pTAS2559->mnDevAAddr, TAS2559_PAGE_REG(nRegister), nValue);
336 nResult = tas2559_i2c_write_device(pTAS2559,
337 pTAS2559->mnDevBAddr, TAS2559_PAGE_REG(nRegister), nValue);
342 mutex_unlock(&pTAS2559->dev_lock);
347 * tas2559_dev_bulk_read :
348 * platform independent, don't change unless necessary
350 static int tas2559_dev_bulk_read(struct tas2559_priv *pTAS2559,
352 unsigned int nRegister,
353 unsigned char *pData,
354 unsigned int nLength)
357 unsigned char reg = 0;
359 mutex_lock(&pTAS2559->dev_lock);
361 if (pTAS2559->mbTILoadActive) {
362 if (!(nRegister & 0x80000000)) {
363 /* let only writes from TILoad pass. */
367 nRegister &= ~0x80000000;
368 dev_dbg(pTAS2559->dev, "TiLoad BR CH[%d] REG B[%d]P[%d]R[%d], count=%d\n",
369 chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
370 TAS2559_PAGE_REG(nRegister), nLength);
373 nResult = tas2559_change_book_page(pTAS2559, chn,
374 TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
377 reg = TAS2559_PAGE_REG(nRegister);
380 nResult = tas2559_i2c_bulkread_device(pTAS2559,
381 pTAS2559->mnDevAAddr, reg, pData, nLength);
384 nResult = tas2559_i2c_bulkread_device(pTAS2559,
385 pTAS2559->mnDevBAddr, reg, pData, nLength);
387 dev_err(pTAS2559->dev, "%s, chn ERROR %d\n", __func__, chn);
394 mutex_unlock(&pTAS2559->dev_lock);
399 * tas2559_dev_bulk_write :
400 * platform independent, don't change unless necessary
402 static int tas2559_dev_bulk_write(struct tas2559_priv *pTAS2559,
404 unsigned int nRegister,
405 unsigned char *pData,
406 unsigned int nLength)
409 unsigned char reg = 0;
411 mutex_lock(&pTAS2559->dev_lock);
413 if (pTAS2559->mbTILoadActive) {
414 if (!(nRegister & 0x80000000)) {
415 /* let only writes from TILoad pass. */
419 nRegister &= ~0x80000000;
420 dev_dbg(pTAS2559->dev, "TiLoad BW CH[%d] REG B[%d]P[%d]R[%d], count=%d\n",
421 chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
422 TAS2559_PAGE_REG(nRegister), nLength);
425 nResult = tas2559_change_book_page(pTAS2559, chn,
426 TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
429 reg = TAS2559_PAGE_REG(nRegister);
432 nResult = tas2559_i2c_bulkwrite_device(pTAS2559,
433 pTAS2559->mnDevAAddr, reg, pData, nLength);
436 nResult = tas2559_i2c_bulkwrite_device(pTAS2559,
437 pTAS2559->mnDevBAddr, reg, pData, nLength);
442 mutex_unlock(&pTAS2559->dev_lock);
447 * tas2559_dev_update_bits :
448 * platform independent, don't change unless necessary
450 static int tas2559_dev_update_bits(
451 struct tas2559_priv *pTAS2559,
453 unsigned int nRegister,
459 mutex_lock(&pTAS2559->dev_lock);
461 if (pTAS2559->mbTILoadActive) {
462 if (!(nRegister & 0x80000000)) {
463 /* let only writes from TILoad pass. */
467 nRegister &= ~0x80000000;
468 dev_dbg(pTAS2559->dev, "TiLoad SB CH[%d] REG B[%d]P[%d]R[%d], mask=0x%x, value=0x%x\n",
469 chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister),
470 TAS2559_PAGE_REG(nRegister), nMask, nValue);
473 nResult = tas2559_change_book_page(pTAS2559,
474 chn, TAS2559_BOOK_ID(nRegister), TAS2559_PAGE_ID(nRegister));
478 nResult = tas2559_i2c_update_bits(pTAS2559,
479 pTAS2559->mnDevAAddr, TAS2559_PAGE_REG(nRegister), nMask, nValue);
482 nResult = tas2559_i2c_update_bits(pTAS2559,
483 pTAS2559->mnDevBAddr, TAS2559_PAGE_REG(nRegister), nMask, nValue);
488 mutex_unlock(&pTAS2559->dev_lock);
492 void tas2559_clearIRQ(struct tas2559_priv *pTAS2559)
497 nResult = pTAS2559->read(pTAS2559, DevA, TAS2559_FLAGS_1, &nValue);
500 pTAS2559->read(pTAS2559, DevA, TAS2559_FLAGS_2, &nValue);
502 nResult = pTAS2559->read(pTAS2559, DevB, TAS2560_FLAGS_1, &nValue);
505 pTAS2559->read(pTAS2559, DevB, TAS2560_FLAGS_2, &nValue);
508 void tas2559_enableIRQ(struct tas2559_priv *pTAS2559, enum channel chl, bool enable)
510 static bool bDevAEnable;
511 static bool bDevBEnable;
514 if (pTAS2559->mbIRQEnable)
518 if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)) {
519 enable_irq(pTAS2559->mnDevAIRQ);
524 if (gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
525 if (pTAS2559->mnDevAGPIOIRQ == pTAS2559->mnDevBGPIOIRQ) {
527 enable_irq(pTAS2559->mnDevBIRQ);
532 enable_irq(pTAS2559->mnDevBIRQ);
538 if (bDevAEnable || bDevBEnable) {
539 /* check after 10 ms */
540 schedule_delayed_work(&pTAS2559->irq_work, msecs_to_jiffies(10));
542 pTAS2559->mbIRQEnable = true;
544 if (pTAS2559->mbIRQEnable) {
545 if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)) {
547 disable_irq_nosync(pTAS2559->mnDevAIRQ);
552 if (gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
554 disable_irq_nosync(pTAS2559->mnDevBIRQ);
559 pTAS2559->mbIRQEnable = false;
564 static void tas2559_hw_reset(struct tas2559_priv *pTAS2559)
566 dev_dbg(pTAS2559->dev, "%s\n", __func__);
568 if (gpio_is_valid(pTAS2559->mnDevAGPIORST)) {
569 gpio_direction_output(pTAS2559->mnDevAGPIORST, 0);
571 gpio_direction_output(pTAS2559->mnDevAGPIORST, 1);
575 if (gpio_is_valid(pTAS2559->mnDevBGPIORST)) {
576 if (pTAS2559->mnDevAGPIORST != pTAS2559->mnDevBGPIORST) {
577 gpio_direction_output(pTAS2559->mnDevBGPIORST, 0);
579 gpio_direction_output(pTAS2559->mnDevBGPIORST, 1);
584 pTAS2559->mnDevACurrentBook = -1;
585 pTAS2559->mnDevACurrentPage = -1;
586 pTAS2559->mnDevBCurrentBook = -1;
587 pTAS2559->mnDevBCurrentPage = -1;
589 if (pTAS2559->mnErrCode)
590 dev_info(pTAS2559->dev, "%s, ErrCode=0x%x\n", __func__, pTAS2559->mnErrCode);
592 pTAS2559->mnErrCode = 0;
595 static void irq_work_routine(struct work_struct *work)
597 struct tas2559_priv *pTAS2559 =
598 container_of(work, struct tas2559_priv, irq_work.work);
599 struct TConfiguration *pConfiguration;
600 unsigned int nDevLInt1Status = 0, nDevLInt2Status = 0;
601 unsigned int nDevRInt1Status = 0, nDevRInt2Status = 0;
605 #ifdef CONFIG_TAS2559_CODEC
606 mutex_lock(&pTAS2559->codec_lock);
609 #ifdef CONFIG_TAS2559_MISC
610 mutex_lock(&pTAS2559->file_lock);
613 if (pTAS2559->mbRuntimeSuspend) {
614 dev_info(pTAS2559->dev, "%s, Runtime Suspended\n", __func__);
618 if (pTAS2559->mnErrCode & ERROR_FAILSAFE)
621 if (!pTAS2559->mbPowerUp) {
622 dev_info(pTAS2559->dev, "%s, device not powered\n", __func__);
626 if ((!pTAS2559->mpFirmware->mnConfigurations)
627 || (!pTAS2559->mpFirmware->mnPrograms)) {
628 dev_info(pTAS2559->dev, "%s, firmware not loaded\n", __func__);
632 pConfiguration = &(pTAS2559->mpFirmware->mpConfigurations[pTAS2559->mnCurrentConfiguration]);
634 if (pConfiguration->mnDevices & DevA) {
635 nResult = tas2559_dev_read(pTAS2559, DevA, TAS2559_FLAGS_1, &nDevLInt1Status);
638 nResult = tas2559_dev_read(pTAS2559, DevA, TAS2559_FLAGS_2, &nDevLInt2Status);
642 if (((nDevLInt1Status & 0xfc) != 0) || ((nDevLInt2Status & 0x0c) != 0)) {
643 /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
644 dev_dbg(pTAS2559->dev, "IRQ critical Error DevA: 0x%x, 0x%x\n",
645 nDevLInt1Status, nDevLInt2Status);
647 if (nDevLInt1Status & 0x80) {
648 pTAS2559->mnErrCode |= ERROR_OVER_CURRENT;
649 dev_err(pTAS2559->dev, "DEVA SPK over current!\n");
651 pTAS2559->mnErrCode &= ~ERROR_OVER_CURRENT;
653 if (nDevLInt1Status & 0x40) {
654 pTAS2559->mnErrCode |= ERROR_UNDER_VOLTAGE;
655 dev_err(pTAS2559->dev, "DEVA SPK under voltage!\n");
657 pTAS2559->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
659 if (nDevLInt1Status & 0x20) {
660 pTAS2559->mnErrCode |= ERROR_CLK_HALT;
661 dev_err(pTAS2559->dev, "DEVA clk halted!\n");
663 pTAS2559->mnErrCode &= ~ERROR_CLK_HALT;
665 if (nDevLInt1Status & 0x10) {
666 pTAS2559->mnErrCode |= ERROR_DIE_OVERTEMP;
667 dev_err(pTAS2559->dev, "DEVA die over temperature!\n");
669 pTAS2559->mnErrCode &= ~ERROR_DIE_OVERTEMP;
671 if (nDevLInt1Status & 0x08) {
672 pTAS2559->mnErrCode |= ERROR_BROWNOUT;
673 dev_err(pTAS2559->dev, "DEVA brownout!\n");
675 pTAS2559->mnErrCode &= ~ERROR_BROWNOUT;
677 if (nDevLInt1Status & 0x04) {
678 pTAS2559->mnErrCode |= ERROR_CLK_LOST;
679 dev_err(pTAS2559->dev, "DEVA clock lost!\n");
681 pTAS2559->mnErrCode &= ~ERROR_CLK_LOST;
683 if (nDevLInt2Status & 0x08) {
684 pTAS2559->mnErrCode |= ERROR_CLK_DET1;
685 dev_err(pTAS2559->dev, "DEVA clk detection 1!\n");
687 pTAS2559->mnErrCode &= ~ERROR_CLK_DET1;
689 if (nDevLInt2Status & 0x04) {
690 pTAS2559->mnErrCode |= ERROR_CLK_DET2;
691 dev_err(pTAS2559->dev, "DEVA clk detection 2!\n");
693 pTAS2559->mnErrCode &= ~ERROR_CLK_DET2;
697 dev_dbg(pTAS2559->dev, "IRQ status DevA: 0x%x, 0x%x\n",
698 nDevLInt1Status, nDevLInt2Status);
701 while (nCounter > 0) {
702 nResult = tas2559_dev_read(pTAS2559, DevA, TAS2559_POWER_UP_FLAG_REG, &nDevLInt1Status);
707 if ((nDevLInt1Status & 0xc0) == 0xc0)
713 /* in case check pow status just after power on TAS2559 */
714 dev_dbg(pTAS2559->dev, "PowSts A: 0x%x, check again after 10ms\n",
720 if ((nDevLInt1Status & 0xc0) != 0xc0) {
721 dev_err(pTAS2559->dev, "%s, Critical DevA ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
723 TAS2559_BOOK_ID(TAS2559_POWER_UP_FLAG_REG),
724 TAS2559_PAGE_ID(TAS2559_POWER_UP_FLAG_REG),
725 TAS2559_PAGE_REG(TAS2559_POWER_UP_FLAG_REG),
727 pTAS2559->mnErrCode |= ERROR_CLASSD_PWR;
731 pTAS2559->mnErrCode &= ~ERROR_CLASSD_PWR;
735 if (pConfiguration->mnDevices & DevB) {
736 nResult = tas2559_dev_read(pTAS2559, DevB, TAS2560_FLAGS_1, &nDevRInt1Status);
739 nResult = tas2559_dev_read(pTAS2559, DevB, TAS2560_FLAGS_2, &nDevRInt2Status);
743 if (((nDevRInt1Status & 0xfc) != 0) || ((nDevRInt2Status & 0xc0) != 0)) {
744 /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
745 dev_dbg(pTAS2559->dev, "IRQ critical Error DevB: 0x%x, 0x%x\n",
746 nDevRInt1Status, nDevRInt2Status);
748 if (nDevRInt1Status & 0x80) {
749 pTAS2559->mnErrCode |= ERROR_OVER_CURRENT;
750 dev_err(pTAS2559->dev, "DEVB SPK over current!\n");
752 pTAS2559->mnErrCode &= ~ERROR_OVER_CURRENT;
754 if (nDevRInt1Status & 0x40) {
755 pTAS2559->mnErrCode |= ERROR_UNDER_VOLTAGE;
756 dev_err(pTAS2559->dev, "DEVB SPK under voltage!\n");
758 pTAS2559->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
760 if (nDevRInt1Status & 0x20) {
761 pTAS2559->mnErrCode |= ERROR_CLK_HALT;
762 dev_err(pTAS2559->dev, "DEVB clk halted!\n");
764 pTAS2559->mnErrCode &= ~ERROR_CLK_HALT;
766 if (nDevRInt1Status & 0x10) {
767 pTAS2559->mnErrCode |= ERROR_DIE_OVERTEMP;
768 dev_err(pTAS2559->dev, "DEVB die over temperature!\n");
770 pTAS2559->mnErrCode &= ~ERROR_DIE_OVERTEMP;
772 if (nDevRInt1Status & 0x08) {
773 pTAS2559->mnErrCode |= ERROR_BROWNOUT;
774 dev_err(pTAS2559->dev, "DEVB brownout!\n");
776 pTAS2559->mnErrCode &= ~ERROR_BROWNOUT;
778 if (nDevRInt1Status & 0x04) {
779 pTAS2559->mnErrCode |= ERROR_CLK_LOST;
780 dev_err(pTAS2559->dev, "DEVB clock lost!\n");
782 pTAS2559->mnErrCode &= ~ERROR_CLK_LOST;
784 if (nDevRInt2Status & 0x80) {
785 pTAS2559->mnErrCode |= ERROR_CLK_DET1;
786 dev_err(pTAS2559->dev, "DEVB clk detection 1!\n");
788 pTAS2559->mnErrCode &= ~ERROR_CLK_DET1;
790 if (nDevRInt2Status & 0x40) {
791 pTAS2559->mnErrCode |= ERROR_CLK_DET2;
792 dev_err(pTAS2559->dev, "DEVB clk detection 2!\n");
794 pTAS2559->mnErrCode &= ~ERROR_CLK_DET2;
798 dev_dbg(pTAS2559->dev, "IRQ status DevB: 0x%x, 0x%x\n",
799 nDevRInt1Status, nDevRInt2Status);
802 while (nCounter > 0) {
803 nResult = tas2559_dev_read(pTAS2559, DevB, TAS2560_POWER_UP_FLAG_REG, &nDevRInt1Status);
808 if ((nDevRInt1Status & 0xc0) == 0xc0)
814 /* in case check pow status just after power on TAS2560 */
815 dev_dbg(pTAS2559->dev, "PowSts B: 0x%x, check again after 10ms\n",
821 if ((nDevRInt1Status & 0xc0) != 0xc0) {
822 dev_err(pTAS2559->dev, "%s, Critical DevB ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
824 TAS2559_BOOK_ID(TAS2560_POWER_UP_FLAG_REG),
825 TAS2559_PAGE_ID(TAS2560_POWER_UP_FLAG_REG),
826 TAS2559_PAGE_REG(TAS2560_POWER_UP_FLAG_REG),
828 pTAS2559->mnErrCode |= ERROR_CLASSD_PWR;
832 pTAS2559->mnErrCode &= ~ERROR_CLASSD_PWR;
839 /* hardware reset and reload */
840 tas2559_set_program(pTAS2559, pTAS2559->mnCurrentProgram, pTAS2559->mnCurrentConfiguration);
844 #ifdef CONFIG_TAS2559_MISC
845 mutex_unlock(&pTAS2559->file_lock);
848 #ifdef CONFIG_TAS2559_CODEC
849 mutex_unlock(&pTAS2559->codec_lock);
853 static irqreturn_t tas2559_irq_handler(int irq, void *dev_id)
855 struct tas2559_priv *pTAS2559 = (struct tas2559_priv *)dev_id;
857 tas2559_enableIRQ(pTAS2559, DevBoth, false);
859 /* get IRQ status after 100 ms */
860 if (!delayed_work_pending(&pTAS2559->irq_work))
861 schedule_delayed_work(&pTAS2559->irq_work, msecs_to_jiffies(100));
866 static void timer_work_routine(struct work_struct *work)
868 struct tas2559_priv *pTAS2559 = container_of(work, struct tas2559_priv, mtimerwork);
869 int nResult, nTemp, nActTemp;
870 struct TProgram *pProgram;
873 #ifdef CONFIG_TAS2559_CODEC
874 mutex_lock(&pTAS2559->codec_lock);
877 #ifdef CONFIG_TAS2559_MISC
878 mutex_lock(&pTAS2559->file_lock);
881 if (pTAS2559->mbRuntimeSuspend) {
882 dev_info(pTAS2559->dev, "%s, Runtime Suspended\n", __func__);
886 if (!pTAS2559->mpFirmware->mnConfigurations) {
887 dev_info(pTAS2559->dev, "%s, firmware not loaded\n", __func__);
891 pProgram = &(pTAS2559->mpFirmware->mpPrograms[pTAS2559->mnCurrentProgram]);
893 if (!pTAS2559->mbPowerUp
894 || (pProgram->mnAppMode != TAS2559_APP_TUNINGMODE)) {
895 dev_info(pTAS2559->dev, "%s, pass, Pow=%d, program=%s\n",
896 __func__, pTAS2559->mbPowerUp, pProgram->mpName);
900 nResult = tas2559_get_die_temperature(pTAS2559, &nTemp);
903 nActTemp = (int)(nTemp >> 23);
904 dev_dbg(pTAS2559->dev, "Die=0x%x, degree=%d\n", nTemp, nActTemp);
906 if (!pTAS2559->mnDieTvReadCounter)
909 pTAS2559->mnDieTvReadCounter++;
912 if (!(pTAS2559->mnDieTvReadCounter % LOW_TEMPERATURE_COUNTER)) {
913 nAvg /= LOW_TEMPERATURE_COUNTER;
914 dev_dbg(pTAS2559->dev, "check : avg=%d\n", nAvg);
916 if ((nAvg & 0x80000000) != 0) {
917 /* if Die temperature is below ZERO */
918 if (pTAS2559->mnDevCurrentGain != LOW_TEMPERATURE_GAIN) {
919 nResult = tas2559_set_DAC_gain(pTAS2559, DevBoth, LOW_TEMPERATURE_GAIN);
924 pTAS2559->mnDevCurrentGain = LOW_TEMPERATURE_GAIN;
925 dev_dbg(pTAS2559->dev, "LOW Temp: set gain to %d\n", LOW_TEMPERATURE_GAIN);
927 } else if (nAvg > 5) {
928 /* if Die temperature is above 5 degree C */
929 if (pTAS2559->mnDevCurrentGain != pTAS2559->mnDevGain) {
930 nResult = tas2559_set_DAC_gain(pTAS2559, DevBoth, pTAS2559->mnDevGain);
935 pTAS2559->mnDevCurrentGain = pTAS2559->mnDevGain;
936 dev_dbg(pTAS2559->dev, "LOW Temp: set gain to original\n");
943 if (pTAS2559->mbPowerUp)
944 hrtimer_start(&pTAS2559->mtimer,
945 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
950 #ifdef CONFIG_TAS2559_MISC
951 mutex_unlock(&pTAS2559->file_lock);
954 #ifdef CONFIG_TAS2559_CODEC
955 mutex_unlock(&pTAS2559->codec_lock);
959 static enum hrtimer_restart temperature_timer_func(struct hrtimer *timer)
961 struct tas2559_priv *pTAS2559 = container_of(timer, struct tas2559_priv, mtimer);
963 if (pTAS2559->mbPowerUp) {
964 schedule_work(&pTAS2559->mtimerwork);
966 if (!delayed_work_pending(&pTAS2559->irq_work))
967 schedule_delayed_work(&pTAS2559->irq_work, msecs_to_jiffies(20));
970 return HRTIMER_NORESTART;
973 static int tas2559_runtime_suspend(struct tas2559_priv *pTAS2559)
975 dev_dbg(pTAS2559->dev, "%s\n", __func__);
977 pTAS2559->mbRuntimeSuspend = true;
979 if (hrtimer_active(&pTAS2559->mtimer)) {
980 dev_dbg(pTAS2559->dev, "cancel die temp timer\n");
981 hrtimer_cancel(&pTAS2559->mtimer);
983 if (work_pending(&pTAS2559->mtimerwork)) {
984 dev_dbg(pTAS2559->dev, "cancel timer work\n");
985 cancel_work_sync(&pTAS2559->mtimerwork);
987 if (delayed_work_pending(&pTAS2559->irq_work)) {
988 dev_dbg(pTAS2559->dev, "cancel IRQ work\n");
989 cancel_delayed_work_sync(&pTAS2559->irq_work);
995 static int tas2559_runtime_resume(struct tas2559_priv *pTAS2559)
997 struct TProgram *pProgram;
999 dev_dbg(pTAS2559->dev, "%s\n", __func__);
1000 if (!pTAS2559->mpFirmware->mpPrograms) {
1001 dev_dbg(pTAS2559->dev, "%s, firmware not loaded\n", __func__);
1005 if (pTAS2559->mnCurrentProgram >= pTAS2559->mpFirmware->mnPrograms) {
1006 dev_err(pTAS2559->dev, "%s, firmware corrupted\n", __func__);
1010 pProgram = &(pTAS2559->mpFirmware->mpPrograms[pTAS2559->mnCurrentProgram]);
1011 if (pTAS2559->mbPowerUp && (pProgram->mnAppMode == TAS2559_APP_TUNINGMODE)) {
1012 if (!hrtimer_active(&pTAS2559->mtimer)) {
1013 dev_dbg(pTAS2559->dev, "%s, start Die Temp check timer\n", __func__);
1014 pTAS2559->mnDieTvReadCounter = 0;
1015 hrtimer_start(&pTAS2559->mtimer,
1016 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
1020 pTAS2559->mbRuntimeSuspend = false;
1026 static bool tas2559_volatile(struct device *pDev, unsigned int nRegister)
1031 static bool tas2559_writeable(struct device *pDev, unsigned int nRegister)
1036 static const struct regmap_config tas2559_i2c_regmap = {
1039 .writeable_reg = tas2559_writeable,
1040 .volatile_reg = tas2559_volatile,
1041 .cache_type = REGCACHE_NONE,
1042 .max_register = 128,
1046 * tas2559_i2c_probe :
1047 * platform dependent
1048 * should implement hardware reset functionality
1050 static int tas2559_i2c_probe(struct i2c_client *pClient,
1051 const struct i2c_device_id *pID)
1053 struct tas2559_priv *pTAS2559;
1055 unsigned int nValue = 0;
1056 const char *fw_name;
1058 dev_info(&pClient->dev, "%s enter\n", __func__);
1060 pTAS2559 = devm_kzalloc(&pClient->dev, sizeof(struct tas2559_priv), GFP_KERNEL);
1063 dev_err(&pClient->dev, " -ENOMEM\n");
1068 pTAS2559->client = pClient;
1069 pTAS2559->dev = &pClient->dev;
1070 i2c_set_clientdata(pClient, pTAS2559);
1071 dev_set_drvdata(&pClient->dev, pTAS2559);
1073 pTAS2559->mpRegmap = devm_regmap_init_i2c(pClient, &tas2559_i2c_regmap);
1075 if (IS_ERR(pTAS2559->mpRegmap)) {
1076 nResult = PTR_ERR(pTAS2559->mpRegmap);
1077 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
1082 if (pClient->dev.of_node)
1083 tas2559_parse_dt(&pClient->dev, pTAS2559);
1085 if (gpio_is_valid(pTAS2559->mnDevAGPIORST)) {
1086 nResult = gpio_request(pTAS2559->mnDevAGPIORST, "TAS2559-RESET");
1089 dev_err(pTAS2559->dev, "%s: GPIO %d request error\n",
1090 __func__, pTAS2559->mnDevAGPIORST);
1095 if (gpio_is_valid(pTAS2559->mnDevBGPIORST)
1096 && (pTAS2559->mnDevAGPIORST != pTAS2559->mnDevBGPIORST)) {
1097 nResult = gpio_request(pTAS2559->mnDevBGPIORST, "TAS2560-RESET");
1100 dev_err(pTAS2559->dev, "%s: GPIO %d request error\n",
1101 __func__, pTAS2559->mnDevBGPIORST);
1106 if (gpio_is_valid(pTAS2559->mnDevAGPIORST)
1107 || gpio_is_valid(pTAS2559->mnDevBGPIORST))
1108 tas2559_hw_reset(pTAS2559);
1110 pTAS2559->read = tas2559_dev_read;
1111 pTAS2559->write = tas2559_dev_write;
1112 pTAS2559->bulk_read = tas2559_dev_bulk_read;
1113 pTAS2559->bulk_write = tas2559_dev_bulk_write;
1114 pTAS2559->update_bits = tas2559_dev_update_bits;
1115 pTAS2559->enableIRQ = tas2559_enableIRQ;
1116 pTAS2559->clearIRQ = tas2559_clearIRQ;
1117 pTAS2559->set_config = tas2559_set_config;
1118 pTAS2559->set_calibration = tas2559_set_calibration;
1119 pTAS2559->hw_reset = tas2559_hw_reset;
1120 pTAS2559->runtime_suspend = tas2559_runtime_suspend;
1121 pTAS2559->runtime_resume = tas2559_runtime_resume;
1122 pTAS2559->mnRestart = 0;
1124 mutex_init(&pTAS2559->dev_lock);
1126 /* Reset the chip */
1127 nResult = tas2559_dev_write(pTAS2559, DevBoth, TAS2559_SW_RESET_REG, 1);
1129 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
1133 nResult = pTAS2559->read(pTAS2559, DevA, TAS2559_REV_PGID_REG, &nValue);
1134 pTAS2559->mnDevAPGID = nValue;
1135 dev_info(&pClient->dev, "TAS2559 PGID=0x%x\n", nValue);
1136 nResult = pTAS2559->read(pTAS2559, DevB, TAS2560_ID_REG, &nValue);
1137 pTAS2559->mnDevBPGID = nValue;
1138 dev_info(pTAS2559->dev, "TAS2560 PGID=0x%02x\n", nValue);
1140 if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)) {
1141 nResult = gpio_request(pTAS2559->mnDevAGPIOIRQ, "TAS2559-IRQ");
1144 dev_err(pTAS2559->dev,
1145 "%s: GPIO %d request INT error\n",
1146 __func__, pTAS2559->mnDevAGPIOIRQ);
1150 gpio_direction_input(pTAS2559->mnDevAGPIOIRQ);
1151 pTAS2559->mnDevAIRQ = gpio_to_irq(pTAS2559->mnDevAGPIOIRQ);
1152 dev_dbg(pTAS2559->dev, "irq = %d\n", pTAS2559->mnDevAIRQ);
1153 nResult = request_threaded_irq(pTAS2559->mnDevAIRQ, tas2559_irq_handler,
1154 NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1155 pClient->name, pTAS2559);
1158 dev_err(pTAS2559->dev,
1159 "request_irq failed, %d\n", nResult);
1163 disable_irq_nosync(pTAS2559->mnDevAIRQ);
1166 if (gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
1167 if (pTAS2559->mnDevAGPIOIRQ != pTAS2559->mnDevBGPIOIRQ) {
1168 nResult = gpio_request(pTAS2559->mnDevBGPIOIRQ, "TAS2560-IRQ");
1171 dev_err(pTAS2559->dev,
1172 "%s: GPIO %d request INT error\n",
1173 __func__, pTAS2559->mnDevBGPIOIRQ);
1177 gpio_direction_input(pTAS2559->mnDevBGPIOIRQ);
1178 pTAS2559->mnDevBIRQ = gpio_to_irq(pTAS2559->mnDevBGPIOIRQ);
1179 dev_dbg(pTAS2559->dev, "irq = %d\n", pTAS2559->mnDevBIRQ);
1180 nResult = request_threaded_irq(pTAS2559->mnDevBIRQ, tas2559_irq_handler,
1181 NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1182 pClient->name, pTAS2559);
1185 dev_err(pTAS2559->dev,
1186 "request_irq failed, %d\n", nResult);
1190 disable_irq_nosync(pTAS2559->mnDevBIRQ);
1192 pTAS2559->mnDevBIRQ = pTAS2559->mnDevAIRQ;
1195 if (gpio_is_valid(pTAS2559->mnDevAGPIOIRQ)
1196 || gpio_is_valid(pTAS2559->mnDevBGPIOIRQ)) {
1197 INIT_DELAYED_WORK(&pTAS2559->irq_work, irq_work_routine);
1200 pTAS2559->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
1202 if (!pTAS2559->mpFirmware) {
1203 dev_err(&pClient->dev, "mpFirmware ENOMEM\n");
1208 pTAS2559->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
1210 if (!pTAS2559->mpCalFirmware) {
1211 dev_err(&pClient->dev, "mpCalFirmware ENOMEM\n");
1216 #ifdef CONFIG_TAS2559_CODEC
1217 mutex_init(&pTAS2559->codec_lock);
1218 tas2559_register_codec(pTAS2559);
1221 #ifdef CONFIG_TAS2559_MISC
1222 mutex_init(&pTAS2559->file_lock);
1223 tas2559_register_misc(pTAS2559);
1226 #ifdef ENABLE_TILOAD
1227 tiload_driver_init(pTAS2559);
1230 hrtimer_init(&pTAS2559->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1231 pTAS2559->mtimer.function = temperature_timer_func;
1232 INIT_WORK(&pTAS2559->mtimerwork, timer_work_routine);
1234 if (get_hw_version_platform() == HARDWARE_PLATFORM_CHIRON_S)
1235 fw_name = TAS2559_S_FW_NAME;
1237 fw_name = TAS2559_FW_NAME;
1239 dev_err(&pClient->dev, "Firmware file name is %s\n", fw_name);
1240 nResult = request_firmware_nowait(THIS_MODULE, 1, fw_name,
1241 pTAS2559->dev, GFP_KERNEL, pTAS2559, tas2559_fw_ready);
1248 static int tas2559_i2c_remove(struct i2c_client *pClient)
1250 struct tas2559_priv *pTAS2559 = i2c_get_clientdata(pClient);
1252 dev_info(pTAS2559->dev, "%s\n", __func__);
1254 #ifdef CONFIG_TAS2559_CODEC
1255 tas2559_deregister_codec(pTAS2559);
1256 mutex_destroy(&pTAS2559->codec_lock);
1259 #ifdef CONFIG_TAS2559_MISC
1260 tas2559_deregister_misc(pTAS2559);
1261 mutex_destroy(&pTAS2559->file_lock);
1264 mutex_destroy(&pTAS2559->dev_lock);
1268 static const struct i2c_device_id tas2559_i2c_id[] = {
1273 MODULE_DEVICE_TABLE(i2c, tas2559_i2c_id);
1275 #if defined(CONFIG_OF)
1276 static const struct of_device_id tas2559_of_match[] = {
1277 {.compatible = "ti,tas2559"},
1281 MODULE_DEVICE_TABLE(of, tas2559_of_match);
1284 static struct i2c_driver tas2559_i2c_driver = {
1287 .owner = THIS_MODULE,
1288 #if defined(CONFIG_OF)
1289 .of_match_table = of_match_ptr(tas2559_of_match),
1292 .probe = tas2559_i2c_probe,
1293 .remove = tas2559_i2c_remove,
1294 .id_table = tas2559_i2c_id,
1297 module_i2c_driver(tas2559_i2c_driver);
1299 MODULE_AUTHOR("Texas Instruments Inc.");
1300 MODULE_DESCRIPTION("TAS2559 Stereo I2C Smart Amplifier driver");
1301 MODULE_LICENSE("GPL v2");