1 /*
2 ** =============================================================================
3 ** Copyright (c) 2016 Texas Instruments Inc.
4 **
5 ** This program is free software; you can redistribute it and/or modify it under
6 ** the terms of the GNU General Public License as published by the Free Software
7 ** Foundation; version 2.
8 **
9 ** This program is distributed in the hope that it will be useful, but WITHOUT
10 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 **
13 ** File:
14 ** tas2557-regmap.c
15 **
16 ** Description:
17 ** I2C driver with regmap for Texas Instruments TAS2557 High Performance 4W Smart Amplifier
18 **
19 ** =============================================================================
20 */
22 #ifdef CONFIG_TAS2557_REGMAP
24 #define DEBUG
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/i2c.h>
31 #include <linux/gpio.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/firmware.h>
34 #include <linux/regmap.h>
35 #include <linux/of.h>
36 #include <linux/of_gpio.h>
37 #include <linux/slab.h>
38 #include <linux/syscalls.h>
39 #include <linux/fcntl.h>
40 #include <linux/uaccess.h>
41 #include "tas2557.h"
42 #include "tas2557-core.h"
44 #ifdef CONFIG_TAS2557_CODEC
45 #include "tas2557-codec.h"
46 #endif
48 #ifdef CONFIG_TAS2557_MISC
49 #include "tas2557-misc.h"
50 #endif
52 #define ENABLE_TILOAD
53 #ifdef ENABLE_TILOAD
54 #include "tiload.h"
55 #endif
57 #define LOW_TEMPERATURE_GAIN 6
58 #define LOW_TEMPERATURE_COUNTER 12
60 static int tas2557_change_book_page(
61 struct tas2557_priv *pTAS2557,
62 unsigned char nBook,
63 unsigned char nPage)
64 {
65 int nResult = 0;
67 if ((pTAS2557->mnCurrentBook == nBook)
68 && pTAS2557->mnCurrentPage == nPage)
69 goto end;
71 if (pTAS2557->mnCurrentBook != nBook) {
72 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_PAGE, 0);
73 if (nResult < 0) {
74 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
75 __func__, __LINE__, nResult);
76 goto end;
77 }
78 pTAS2557->mnCurrentPage = 0;
79 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_REG, nBook);
80 if (nResult < 0) {
81 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
82 __func__, __LINE__, nResult);
83 goto end;
84 }
85 pTAS2557->mnCurrentBook = nBook;
86 if (nPage != 0) {
87 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_PAGE, nPage);
88 if (nResult < 0) {
89 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
90 __func__, __LINE__, nResult);
91 goto end;
92 }
93 pTAS2557->mnCurrentPage = nPage;
94 }
95 } else if (pTAS2557->mnCurrentPage != nPage) {
96 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_BOOKCTL_PAGE, nPage);
97 if (nResult < 0) {
98 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
99 __func__, __LINE__, nResult);
100 goto end;
101 }
102 pTAS2557->mnCurrentPage = nPage;
103 }
105 end:
106 if (nResult < 0)
107 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
108 else
109 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
111 return nResult;
112 }
114 static int tas2557_dev_read(
115 struct tas2557_priv *pTAS2557,
116 unsigned int nRegister,
117 unsigned int *pValue)
118 {
119 int nResult = 0;
120 unsigned int Value = 0;
122 mutex_lock(&pTAS2557->dev_lock);
124 if (pTAS2557->mbTILoadActive) {
125 if (!(nRegister & 0x80000000))
126 goto end; /* let only reads from TILoad pass. */
127 nRegister &= ~0x80000000;
129 dev_dbg(pTAS2557->dev, "TiLoad R REG B[%d]P[%d]R[%d]\n",
130 TAS2557_BOOK_ID(nRegister),
131 TAS2557_PAGE_ID(nRegister),
132 TAS2557_PAGE_REG(nRegister));
133 }
135 nResult = tas2557_change_book_page(pTAS2557,
136 TAS2557_BOOK_ID(nRegister),
137 TAS2557_PAGE_ID(nRegister));
138 if (nResult >= 0) {
139 nResult = regmap_read(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), &Value);
140 if (nResult < 0) {
141 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
142 __func__, __LINE__, nResult);
143 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
144 goto end;
145 } else
146 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
147 *pValue = Value;
148 }
150 end:
152 mutex_unlock(&pTAS2557->dev_lock);
153 return nResult;
154 }
156 static int tas2557_dev_write(
157 struct tas2557_priv *pTAS2557,
158 unsigned int nRegister,
159 unsigned int nValue)
160 {
161 int nResult = 0;
163 mutex_lock(&pTAS2557->dev_lock);
164 if ((nRegister == 0xAFFEAFFE) && (nValue == 0xBABEBABE)) {
165 pTAS2557->mbTILoadActive = true;
166 goto end;
167 }
169 if ((nRegister == 0xBABEBABE) && (nValue == 0xAFFEAFFE)) {
170 pTAS2557->mbTILoadActive = false;
171 goto end;
172 }
174 if (pTAS2557->mbTILoadActive) {
175 if (!(nRegister & 0x80000000))
176 goto end;/* let only writes from TILoad pass. */
177 nRegister &= ~0x80000000;
179 dev_dbg(pTAS2557->dev, "TiLoad W REG B[%d]P[%d]R[%d] =0x%x\n",
180 TAS2557_BOOK_ID(nRegister),
181 TAS2557_PAGE_ID(nRegister),
182 TAS2557_PAGE_REG(nRegister),
183 nValue);
184 }
186 nResult = tas2557_change_book_page(pTAS2557,
187 TAS2557_BOOK_ID(nRegister),
188 TAS2557_PAGE_ID(nRegister));
189 if (nResult >= 0) {
190 nResult = regmap_write(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), nValue);
191 if (nResult < 0) {
192 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
193 __func__, __LINE__, nResult);
194 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
195 } else
196 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
197 }
199 end:
201 mutex_unlock(&pTAS2557->dev_lock);
203 return nResult;
204 }
206 static int tas2557_dev_bulk_read(
207 struct tas2557_priv *pTAS2557,
208 unsigned int nRegister,
209 u8 *pData,
210 unsigned int nLength)
211 {
212 int nResult = 0;
214 mutex_lock(&pTAS2557->dev_lock);
215 if (pTAS2557->mbTILoadActive) {
216 if (!(nRegister & 0x80000000))
217 goto end; /* let only writes from TILoad pass. */
219 nRegister &= ~0x80000000;
220 dev_dbg(pTAS2557->dev, "TiLoad BR REG B[%d]P[%d]R[%d], count=%d\n",
221 TAS2557_BOOK_ID(nRegister),
222 TAS2557_PAGE_ID(nRegister),
223 TAS2557_PAGE_REG(nRegister),
224 nLength);
225 }
227 nResult = tas2557_change_book_page(pTAS2557,
228 TAS2557_BOOK_ID(nRegister),
229 TAS2557_PAGE_ID(nRegister));
230 if (nResult >= 0) {
231 nResult = regmap_bulk_read(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), pData, nLength);
232 if (nResult < 0) {
233 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
234 __func__, __LINE__, nResult);
235 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
236 } else
237 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
238 }
240 end:
242 mutex_unlock(&pTAS2557->dev_lock);
243 return nResult;
244 }
246 static int tas2557_dev_bulk_write(
247 struct tas2557_priv *pTAS2557,
248 unsigned int nRegister,
249 u8 *pData,
250 unsigned int nLength)
251 {
252 int nResult = 0;
254 mutex_lock(&pTAS2557->dev_lock);
255 if (pTAS2557->mbTILoadActive) {
256 if (!(nRegister & 0x80000000))
257 goto end; /* let only writes from TILoad pass. */
259 nRegister &= ~0x80000000;
261 dev_dbg(pTAS2557->dev, "TiLoad BW REG B[%d]P[%d]R[%d], count=%d\n",
262 TAS2557_BOOK_ID(nRegister),
263 TAS2557_PAGE_ID(nRegister),
264 TAS2557_PAGE_REG(nRegister),
265 nLength);
266 }
268 nResult = tas2557_change_book_page( pTAS2557,
269 TAS2557_BOOK_ID(nRegister),
270 TAS2557_PAGE_ID(nRegister));
271 if (nResult >= 0) {
272 nResult = regmap_bulk_write(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), pData, nLength);
273 if (nResult < 0) {
274 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
275 __func__, __LINE__, nResult);
276 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
277 } else
278 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
279 }
281 end:
283 mutex_unlock(&pTAS2557->dev_lock);
284 return nResult;
285 }
287 static int tas2557_dev_update_bits(
288 struct tas2557_priv *pTAS2557,
289 unsigned int nRegister,
290 unsigned int nMask,
291 unsigned int nValue)
292 {
293 int nResult = 0;
295 mutex_lock(&pTAS2557->dev_lock);
297 if (pTAS2557->mbTILoadActive) {
298 if (!(nRegister & 0x80000000))
299 goto end; /* let only writes from TILoad pass. */
301 nRegister &= ~0x80000000;
302 dev_dbg(pTAS2557->dev, "TiLoad SB REG B[%d]P[%d]R[%d], mask=0x%x, value=0x%x\n",
303 TAS2557_BOOK_ID(nRegister),
304 TAS2557_PAGE_ID(nRegister),
305 TAS2557_PAGE_REG(nRegister),
306 nMask, nValue);
307 }
309 nResult = tas2557_change_book_page( pTAS2557,
310 TAS2557_BOOK_ID(nRegister),
311 TAS2557_PAGE_ID(nRegister));
312 if (nResult >= 0) {
313 nResult = regmap_update_bits(pTAS2557->mpRegmap, TAS2557_PAGE_REG(nRegister), nMask, nValue);
314 if (nResult < 0) {
315 dev_err(pTAS2557->dev, "%s, %d, I2C error %d\n",
316 __func__, __LINE__, nResult);
317 pTAS2557->mnErrCode |= ERROR_DEVA_I2C_COMM;
318 } else
319 pTAS2557->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
320 }
322 end:
323 mutex_unlock(&pTAS2557->dev_lock);
324 return nResult;
325 }
327 void tas2557_clearIRQ(struct tas2557_priv *pTAS2557)
328 {
329 unsigned int nValue;
330 int nResult = 0;
332 nResult = pTAS2557->read(pTAS2557, TAS2557_FLAGS_1, &nValue);
333 if (nResult >= 0)
334 pTAS2557->read(pTAS2557, TAS2557_FLAGS_2, &nValue);
336 }
339 void tas2557_enableIRQ(struct tas2557_priv *pTAS2557, bool enable)
340 {
341 if (enable) {
342 if (!pTAS2557->mbIRQEnable) {
343 if (gpio_is_valid(pTAS2557->mnGpioINT)) {
344 enable_irq(pTAS2557->mnIRQ);
345 /* check after 10 ms */
346 schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(10));
347 pTAS2557->mbIRQEnable = true;
348 }
349 }
350 } else {
351 if (pTAS2557->mbIRQEnable) {
352 if (gpio_is_valid(pTAS2557->mnGpioINT))
353 disable_irq_nosync(pTAS2557->mnIRQ);
354 pTAS2557->mbIRQEnable = false;
355 }
356 }
357 }
359 static void tas2557_hw_reset(struct tas2557_priv *pTAS2557)
360 {
361 if (gpio_is_valid(pTAS2557->mnResetGPIO)) {
362 gpio_direction_output(pTAS2557->mnResetGPIO, 0);
363 msleep(5);
364 gpio_direction_output(pTAS2557->mnResetGPIO, 1);
365 msleep(2);
366 }
368 pTAS2557->mnCurrentBook = -1;
369 pTAS2557->mnCurrentPage = -1;
370 if (pTAS2557->mnErrCode)
371 dev_info(pTAS2557->dev, "before reset, ErrCode=0x%x\n", pTAS2557->mnErrCode);
372 pTAS2557->mnErrCode = 0;
373 }
375 static void irq_work_routine(struct work_struct *work)
376 {
377 int nResult = 0;
378 unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
379 unsigned int nDevPowerUpFlag = 0;
380 int nCounter = 2;
381 struct tas2557_priv *pTAS2557 =
382 container_of(work, struct tas2557_priv, irq_work.work);
384 #ifdef CONFIG_TAS2557_CODEC
385 mutex_lock(&pTAS2557->codec_lock);
386 #endif
388 #ifdef CONFIG_TAS2557_MISC
389 mutex_lock(&pTAS2557->file_lock);
390 #endif
392 if (!pTAS2557->mbPowerUp)
393 goto end;
395 if ((!pTAS2557->mpFirmware->mnConfigurations)
396 || (!pTAS2557->mpFirmware->mnPrograms)) {
397 dev_info(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
398 goto end;
399 }
401 nResult = tas2557_dev_read(pTAS2557, TAS2557_FLAGS_1, &nDevInt1Status);
402 if (nResult >= 0)
403 nResult = tas2557_dev_read(pTAS2557, TAS2557_FLAGS_2, &nDevInt2Status);
405 if (nResult < 0)
406 goto program;
408 if (((nDevInt1Status & 0xfc) != 0) || ((nDevInt2Status & 0x0c) != 0)) {
409 /* in case of INT_OC, INT_UV, INT_OT, INT_BO, INT_CL, INT_CLK1, INT_CLK2 */
410 dev_err(pTAS2557->dev, "critical error: 0x%x, 0x%x\n", nDevInt1Status, nDevInt2Status);
411 if (nDevInt1Status & 0x80) {
412 pTAS2557->mnErrCode |= ERROR_OVER_CURRENT;
413 dev_err(pTAS2557->dev, "DEVA SPK over current!\n");
414 } else
415 pTAS2557->mnErrCode &= ~ERROR_OVER_CURRENT;
417 if (nDevInt1Status & 0x40) {
418 pTAS2557->mnErrCode |= ERROR_UNDER_VOLTAGE;
419 dev_err(pTAS2557->dev, "DEVA SPK under voltage!\n");
420 } else
421 pTAS2557->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
423 if (nDevInt1Status & 0x20) {
424 pTAS2557->mnErrCode |= ERROR_CLK_HALT;
425 dev_err(pTAS2557->dev, "DEVA clk halted!\n");
426 } else
427 pTAS2557->mnErrCode &= ~ERROR_CLK_HALT;
429 if (nDevInt1Status & 0x10) {
430 pTAS2557->mnErrCode |= ERROR_DIE_OVERTEMP;
431 dev_err(pTAS2557->dev, "DEVA die over temperature!\n");
432 } else
433 pTAS2557->mnErrCode &= ~ERROR_DIE_OVERTEMP;
435 if (nDevInt1Status & 0x08) {
436 pTAS2557->mnErrCode |= ERROR_BROWNOUT;
437 dev_err(pTAS2557->dev, "DEVA brownout!\n");
438 } else
439 pTAS2557->mnErrCode &= ~ERROR_BROWNOUT;
441 if (nDevInt1Status & 0x04) {
442 pTAS2557->mnErrCode |= ERROR_CLK_LOST;
443 dev_err(pTAS2557->dev, "DEVA clock lost!\n");
444 } else
445 pTAS2557->mnErrCode &= ~ERROR_CLK_LOST;
447 if (nDevInt2Status & 0x08) {
448 pTAS2557->mnErrCode |= ERROR_CLK_DET1;
449 dev_err(pTAS2557->dev, "DEVA clk detection 1!\n");
450 } else
451 pTAS2557->mnErrCode &= ~ERROR_CLK_DET1;
453 if (nDevInt2Status & 0x04) {
454 pTAS2557->mnErrCode |= ERROR_CLK_DET2;
455 dev_err(pTAS2557->dev, "DEVA clk detection 2!\n");
456 } else
457 pTAS2557->mnErrCode &= ~ERROR_CLK_DET2;
459 goto program;
460 } else {
461 dev_dbg(pTAS2557->dev, "IRQ Status: 0x%x, 0x%x\n", nDevInt1Status, nDevInt2Status);
462 nCounter = 2;
463 while (nCounter > 0) {
464 nResult = tas2557_dev_read(pTAS2557, TAS2557_POWER_UP_FLAG_REG, &nDevPowerUpFlag);
465 if (nResult < 0)
466 goto program;
467 if ((nDevPowerUpFlag & 0xc0) == 0xc0)
468 break;
469 nCounter--;
470 if (nCounter > 0) {
471 /* in case check pow status just after power on TAS2557 */
472 dev_dbg(pTAS2557->dev, "PowSts: 0x%x, check again after 10ms\n",
473 nDevPowerUpFlag);
474 msleep(10);
475 }
476 }
477 if ((nDevPowerUpFlag & 0xc0) != 0xc0) {
478 dev_err(pTAS2557->dev, "%s, Critical ERROR B[%d]_P[%d]_R[%d]= 0x%x\n",
479 __func__,
480 TAS2557_BOOK_ID(TAS2557_POWER_UP_FLAG_REG),
481 TAS2557_PAGE_ID(TAS2557_POWER_UP_FLAG_REG),
482 TAS2557_PAGE_REG(TAS2557_POWER_UP_FLAG_REG),
483 nDevPowerUpFlag);
484 pTAS2557->mnErrCode |= ERROR_CLASSD_PWR;
485 goto program;
486 }
487 pTAS2557->mnErrCode &= ~ERROR_CLASSD_PWR;
489 dev_dbg(pTAS2557->dev, "%s: INT1=0x%x, INT2=0x%x; PowerUpFlag=0x%x\n",
490 __func__, nDevInt1Status, nDevInt2Status, nDevPowerUpFlag);
491 goto end;
492 }
494 program:
495 /* hardware reset and reload */
496 tas2557_set_program(pTAS2557, pTAS2557->mnCurrentProgram, pTAS2557->mnCurrentConfiguration);
498 end:
500 #ifdef CONFIG_TAS2557_MISC
501 mutex_unlock(&pTAS2557->file_lock);
502 #endif
504 #ifdef CONFIG_TAS2557_CODEC
505 mutex_unlock(&pTAS2557->codec_lock);
506 #endif
507 }
509 static irqreturn_t tas2557_irq_handler(int irq, void *dev_id)
510 {
511 struct tas2557_priv *pTAS2557 = (struct tas2557_priv *)dev_id;
513 tas2557_enableIRQ(pTAS2557, false);
514 /* get IRQ status after 100 ms */
515 schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(100));
516 return IRQ_HANDLED;
517 }
519 static enum hrtimer_restart temperature_timer_func(struct hrtimer *timer)
520 {
521 struct tas2557_priv *pTAS2557 = container_of(timer, struct tas2557_priv, mtimer);
523 if (pTAS2557->mbPowerUp) {
524 schedule_work(&pTAS2557->mtimerwork);
525 schedule_delayed_work(&pTAS2557->irq_work, msecs_to_jiffies(1));
526 }
527 return HRTIMER_NORESTART;
528 }
530 static void timer_work_routine(struct work_struct *work)
531 {
532 struct tas2557_priv *pTAS2557 = container_of(work, struct tas2557_priv, mtimerwork);
533 int nResult, nTemp, nActTemp;
534 struct TProgram *pProgram;
535 static int nAvg;
537 #ifdef CONFIG_TAS2557_CODEC
538 mutex_lock(&pTAS2557->codec_lock);
539 #endif
541 #ifdef CONFIG_TAS2557_MISC
542 mutex_lock(&pTAS2557->file_lock);
543 #endif
545 if (!pTAS2557->mpFirmware->mnConfigurations) {
546 dev_info(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
547 goto end;
548 }
550 pProgram = &(pTAS2557->mpFirmware->mpPrograms[pTAS2557->mnCurrentProgram]);
551 if (!pTAS2557->mbPowerUp
552 || (pProgram->mnAppMode != TAS2557_APP_TUNINGMODE)) {
553 dev_info(pTAS2557->dev, "%s, pass, Pow=%d, program=%s\n",
554 __func__, pTAS2557->mbPowerUp, pProgram->mpName);
555 goto end;
556 }
558 nResult = tas2557_get_die_temperature(pTAS2557, &nTemp);
559 if (nResult >= 0) {
560 nActTemp = (int)(nTemp >> 23);
561 dev_dbg(pTAS2557->dev, "Die=0x%x, degree=%d\n", nTemp, nActTemp);
562 if (!pTAS2557->mnDieTvReadCounter)
563 nAvg = 0;
564 pTAS2557->mnDieTvReadCounter++;
565 nAvg += nActTemp;
566 if (!(pTAS2557->mnDieTvReadCounter % LOW_TEMPERATURE_COUNTER)) {
567 nAvg /= LOW_TEMPERATURE_COUNTER;
568 dev_dbg(pTAS2557->dev, "check : avg=%d\n", nAvg);
569 if ((nAvg & 0x80000000) != 0) {
570 /* if Die temperature is below ZERO */
571 if (pTAS2557->mnDevCurrentGain != LOW_TEMPERATURE_GAIN) {
572 nResult = tas2557_set_DAC_gain(pTAS2557, LOW_TEMPERATURE_GAIN);
573 if (nResult < 0)
574 goto end;
575 pTAS2557->mnDevCurrentGain = LOW_TEMPERATURE_GAIN;
576 dev_dbg(pTAS2557->dev, "LOW Temp: set gain to %d\n", LOW_TEMPERATURE_GAIN);
577 }
578 } else if (nAvg > 5) {
579 /* if Die temperature is above 5 degree C */
580 if (pTAS2557->mnDevCurrentGain != pTAS2557->mnDevGain) {
581 nResult = tas2557_set_DAC_gain(pTAS2557, pTAS2557->mnDevGain);
582 if (nResult < 0)
583 goto end;
584 pTAS2557->mnDevCurrentGain = pTAS2557->mnDevGain;
585 dev_dbg(pTAS2557->dev, "LOW Temp: set gain to original\n");
586 }
587 }
588 nAvg = 0;
589 }
591 if (pTAS2557->mbPowerUp)
592 hrtimer_start(&pTAS2557->mtimer,
593 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
594 }
596 end:
598 #ifdef CONFIG_TAS2557_MISC
599 mutex_unlock(&pTAS2557->file_lock);
600 #endif
602 #ifdef CONFIG_TAS2557_CODEC
603 mutex_unlock(&pTAS2557->codec_lock);
604 #endif
605 }
607 #ifdef CONFIG_PM_SLEEP
608 static int tas2557_suspend(struct device *dev)
609 {
610 struct tas2557_priv *pTAS2557 = dev_get_drvdata(dev);
612 dev_dbg(pTAS2557->dev, "%s\n", __func__);
613 if (hrtimer_active(&pTAS2557->mtimer)) {
614 dev_dbg(pTAS2557->dev, "cancel die temp timer\n");
615 hrtimer_cancel(&pTAS2557->mtimer);
616 }
618 return 0;
619 }
621 static int tas2557_resume(struct device *dev)
622 {
623 struct tas2557_priv *pTAS2557 = dev_get_drvdata(dev);
624 struct TProgram *pProgram;
626 dev_dbg(pTAS2557->dev, "%s\n", __func__);
627 if (!pTAS2557->mpFirmware->mpPrograms) {
628 dev_dbg(pTAS2557->dev, "%s, firmware not loaded\n", __func__);
629 goto end;
630 }
632 if (pTAS2557->mnCurrentProgram >= pTAS2557->mpCalFirmware->mnPrograms) {
633 dev_err(pTAS2557->dev, "%s, firmware corrupted\n", __func__);
634 goto end;
635 }
637 pProgram = &(pTAS2557->mpFirmware->mpPrograms[pTAS2557->mnCurrentProgram]);
638 if (pTAS2557->mbPowerUp && (pProgram->mnAppMode == TAS2557_APP_TUNINGMODE)) {
639 if (!hrtimer_active(&pTAS2557->mtimer)) {
640 dev_dbg(pTAS2557->dev, "%s, start Die Temp check timer\n", __func__);
641 pTAS2557->mnDieTvReadCounter = 0;
642 hrtimer_start(&pTAS2557->mtimer,
643 ns_to_ktime((u64)LOW_TEMPERATURE_CHECK_PERIOD * NSEC_PER_MSEC), HRTIMER_MODE_REL);
644 }
645 }
647 end:
649 return 0;
650 }
651 #endif
653 static bool tas2557_volatile(struct device *pDev, unsigned int nRegister)
654 {
655 return true;
656 }
658 static bool tas2557_writeable(struct device *pDev, unsigned int nRegister)
659 {
660 return true;
661 }
663 static const struct regmap_config tas2557_i2c_regmap = {
664 .reg_bits = 8,
665 .val_bits = 8,
666 .writeable_reg = tas2557_writeable,
667 .volatile_reg = tas2557_volatile,
668 .cache_type = REGCACHE_NONE,
669 .max_register = 128,
670 };
672 /* tas2557_i2c_probe :
673 * platform dependent
674 * should implement hardware reset functionality
675 */
676 static int tas2557_i2c_probe(struct i2c_client *pClient,
677 const struct i2c_device_id *pID)
678 {
679 struct tas2557_priv *pTAS2557;
680 int nResult = 0;
681 unsigned int nValue = 0;
682 const char *pFWName;
684 dev_info(&pClient->dev, "%s enter\n", __func__);
686 pTAS2557 = devm_kzalloc(&pClient->dev, sizeof(struct tas2557_priv), GFP_KERNEL);
687 if (!pTAS2557) {
688 nResult = -ENOMEM;
689 goto err;
690 }
692 pTAS2557->dev = &pClient->dev;
693 i2c_set_clientdata(pClient, pTAS2557);
694 dev_set_drvdata(&pClient->dev, pTAS2557);
696 pTAS2557->mpRegmap = devm_regmap_init_i2c(pClient, &tas2557_i2c_regmap);
697 if (IS_ERR(pTAS2557->mpRegmap)) {
698 nResult = PTR_ERR(pTAS2557->mpRegmap);
699 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
700 nResult);
701 goto err;
702 }
704 if (pClient->dev.of_node)
705 tas2557_parse_dt(&pClient->dev, pTAS2557);
707 if (gpio_is_valid(pTAS2557->mnResetGPIO)) {
708 nResult = gpio_request(pTAS2557->mnResetGPIO, "TAS2557-RESET");
709 if (nResult < 0) {
710 dev_err(pTAS2557->dev, "%s: GPIO %d request error\n",
711 __func__, pTAS2557->mnResetGPIO);
712 goto err;
713 }
714 tas2557_hw_reset(pTAS2557);
715 }
717 pTAS2557->read = tas2557_dev_read;
718 pTAS2557->write = tas2557_dev_write;
719 pTAS2557->bulk_read = tas2557_dev_bulk_read;
720 pTAS2557->bulk_write = tas2557_dev_bulk_write;
721 pTAS2557->update_bits = tas2557_dev_update_bits;
722 pTAS2557->enableIRQ = tas2557_enableIRQ;
723 pTAS2557->clearIRQ = tas2557_clearIRQ;
724 pTAS2557->set_config = tas2557_set_config;
725 pTAS2557->set_calibration = tas2557_set_calibration;
726 pTAS2557->hw_reset = tas2557_hw_reset;
728 mutex_init(&pTAS2557->dev_lock);
730 /* Reset the chip */
731 nResult = tas2557_dev_write(pTAS2557, TAS2557_SW_RESET_REG, 0x01);
732 if (nResult < 0) {
733 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
734 goto err;
735 }
737 msleep(1);
738 tas2557_dev_read(pTAS2557, TAS2557_REV_PGID_REG, &nValue);
739 pTAS2557->mnPGID = nValue;
740 if (pTAS2557->mnPGID == TAS2557_PG_VERSION_2P1) {
741 dev_info(pTAS2557->dev, "PG2.1 Silicon found\n");
742 pFWName = TAS2557_FW_NAME;
743 } else if (pTAS2557->mnPGID == TAS2557_PG_VERSION_1P0) {
744 dev_info(pTAS2557->dev, "PG1.0 Silicon found\n");
745 pFWName = TAS2557_PG1P0_FW_NAME;
746 } else {
747 nResult = -ENOTSUPP;
748 dev_info(pTAS2557->dev, "unsupport Silicon 0x%x\n", pTAS2557->mnPGID);
749 goto err;
750 }
752 if (gpio_is_valid(pTAS2557->mnGpioINT)) {
753 nResult = gpio_request(pTAS2557->mnGpioINT, "TAS2557-IRQ");
754 if (nResult < 0) {
755 dev_err(pTAS2557->dev,
756 "%s: GPIO %d request INT error\n",
757 __func__, pTAS2557->mnGpioINT);
758 goto err;
759 }
761 gpio_direction_input(pTAS2557->mnGpioINT);
762 pTAS2557->mnIRQ = gpio_to_irq(pTAS2557->mnGpioINT);
763 dev_dbg(pTAS2557->dev, "irq = %d\n", pTAS2557->mnIRQ);
764 INIT_DELAYED_WORK(&pTAS2557->irq_work, irq_work_routine);
765 nResult = request_threaded_irq(pTAS2557->mnIRQ, tas2557_irq_handler,
766 NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
767 pClient->name, pTAS2557);
768 if (nResult < 0) {
769 dev_err(pTAS2557->dev,
770 "request_irq failed, %d\n", nResult);
771 goto err;
772 }
773 disable_irq_nosync(pTAS2557->mnIRQ);
774 }
776 pTAS2557->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
777 if (!pTAS2557->mpFirmware) {
778 nResult = -ENOMEM;
779 goto err;
780 }
782 pTAS2557->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
783 if (!pTAS2557->mpCalFirmware) {
784 nResult = -ENOMEM;
785 goto err;
786 }
788 #ifdef CONFIG_TAS2557_CODEC
789 mutex_init(&pTAS2557->codec_lock);
790 tas2557_register_codec(pTAS2557);
791 #endif
793 #ifdef CONFIG_TAS2557_MISC
794 mutex_init(&pTAS2557->file_lock);
795 tas2557_register_misc(pTAS2557);
796 #endif
798 #ifdef ENABLE_TILOAD
799 tiload_driver_init(pTAS2557);
800 #endif
802 hrtimer_init(&pTAS2557->mtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
803 pTAS2557->mtimer.function = temperature_timer_func;
804 INIT_WORK(&pTAS2557->mtimerwork, timer_work_routine);
806 nResult = request_firmware_nowait(THIS_MODULE, 1, pFWName,
807 pTAS2557->dev, GFP_KERNEL, pTAS2557, tas2557_fw_ready);
809 err:
811 return nResult;
812 }
814 static int tas2557_i2c_remove(struct i2c_client *pClient)
815 {
816 struct tas2557_priv *pTAS2557 = i2c_get_clientdata(pClient);
818 dev_info(pTAS2557->dev, "%s\n", __func__);
820 #ifdef CONFIG_TAS2557_CODEC
821 tas2557_deregister_codec(pTAS2557);
822 mutex_destroy(&pTAS2557->codec_lock);
823 #endif
825 #ifdef CONFIG_TAS2557_MISC
826 tas2557_deregister_misc(pTAS2557);
827 mutex_destroy(&pTAS2557->file_lock);
828 #endif
830 mutex_destroy(&pTAS2557->dev_lock);
831 return 0;
832 }
834 static const struct i2c_device_id tas2557_i2c_id[] = {
835 {"tas2557", 0},
836 {}
837 };
839 MODULE_DEVICE_TABLE(i2c, tas2557_i2c_id);
841 #if defined(CONFIG_OF)
842 static const struct of_device_id tas2557_of_match[] = {
843 {.compatible = "ti,tas2557"},
844 {},
845 };
847 MODULE_DEVICE_TABLE(of, tas2557_of_match);
848 #endif
850 #ifdef CONFIG_PM_SLEEP
851 static const struct dev_pm_ops tas2557_pm_ops = {
852 SET_SYSTEM_SLEEP_PM_OPS(tas2557_suspend, tas2557_resume)
853 };
854 #endif
856 static struct i2c_driver tas2557_i2c_driver = {
857 .driver = {
858 .name = "tas2557",
859 .owner = THIS_MODULE,
860 #ifdef CONFIG_PM_SLEEP
861 .pm = &tas2557_pm_ops,
862 #endif
863 #if defined(CONFIG_OF)
864 .of_match_table = of_match_ptr(tas2557_of_match),
865 #endif
866 },
867 .probe = tas2557_i2c_probe,
868 .remove = tas2557_i2c_remove,
869 .id_table = tas2557_i2c_id,
870 };
872 module_i2c_driver(tas2557_i2c_driver);
874 MODULE_AUTHOR("Texas Instruments Inc.");
875 MODULE_DESCRIPTION("TAS2557 I2C Smart Amplifier driver");
876 MODULE_LICENSE("GPL v2");
878 #endif