Remove some unused code
[tas256xsw-android/tas2563-android-driver.git] / tas2563-regmap.c
1 /*
2 ** =============================================================================
3 ** Copyright (c) 2016  Texas Instruments Inc.
4 **
5 ** This program is free software; you can redistribute it and/or modify it under
6 ** the terms of the GNU General Public License as published by the Free Software
7 ** Foundation; version 2.
8 **
9 ** This program is distributed in the hope that it will be useful, but WITHOUT
10 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 ** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 **
13 ** File:
14 **     tas2563-regmap.c
15 **
16 ** Description:
17 **     I2C driver with regmap for Texas Instruments TAS2563 High Performance 4W Smart Amplifier
18 **
19 ** =============================================================================
20 */
22 #ifdef CONFIG_TAS2563_REGMAP
24 #define DEBUG
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/i2c.h>
31 #include <linux/gpio.h>
32 #include <linux/interrupt.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/firmware.h>
35 #include <linux/regmap.h>
36 #include <linux/of.h>
37 #include <linux/of_gpio.h>
38 #include <linux/slab.h>
39 #include <linux/syscalls.h>
40 #include <linux/fcntl.h>
41 #include <linux/uaccess.h>
42 #include "tas2563.h"
44 #ifdef CONFIG_TAS2563_CODEC
45 #include "tas2563-codec.h"
46 #endif
48 #ifdef CONFIG_TAS2563_MISC
49 #include "tas2563-misc.h"
50 #endif
52 #define ENABLE_TILOAD
53 #ifdef ENABLE_TILOAD
54 #include "tiload.h"
55 #endif
57 #define LOW_TEMPERATURE_GAIN 6
58 #define LOW_TEMPERATURE_COUNTER 12
60 static int tas2563_change_book_page(struct tas2563_priv *pTAS2563,
61         int book, int page)
62 {
63         int nResult = 0;
64         dev_dbg(pTAS2563->dev, "%s, %d", __func__, __LINE__);
66         if ((pTAS2563->mnCurrentBook == book)
67                 && (pTAS2563->mnCurrentPage == page))
68                 goto end;
70         if (pTAS2563->mnCurrentBook != book) {
71         nResult = regmap_write(pTAS2563->regmap, TAS2563_BOOKCTL_PAGE, 0);
72                 if (nResult < 0) {
73                         dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
74                                 __func__, __LINE__, nResult);
75                         goto end;
76                 }
77         pTAS2563->mnCurrentPage = 0;
78         nResult = regmap_write(pTAS2563->regmap, TAS2563_BOOKCTL_REG, book);
79                 if (nResult < 0) {
80                         dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
81                                 __func__, __LINE__, nResult);
82                         goto end;
83                 }
84                 pTAS2563->mnCurrentBook = book;
85         }
87         if (pTAS2563->mnCurrentPage != page) {
88         nResult = regmap_write(pTAS2563->regmap, TAS2563_BOOKCTL_PAGE, page);
89                 if (nResult < 0) {
90                         dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
91                                 __func__, __LINE__, nResult);
92                         goto end;
93                 }
94                 pTAS2563->mnCurrentPage = page;
95         }
97 end:
98         return nResult;
99 }
101 static int tas2563_dev_read(struct tas2563_priv *pTAS2563,
102         unsigned int reg, unsigned int *pValue)
104         int nResult = 0;
106         mutex_lock(&pTAS2563->dev_lock);
108         nResult = tas2563_change_book_page(pTAS2563,
109                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
110         if (nResult < 0)
111                 goto end;
113         nResult = regmap_read(pTAS2563->regmap, TAS2563_PAGE_REG(reg), pValue);
114         if (nResult < 0)
115                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
116                         __func__, __LINE__, nResult);
117         else
118                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u\n", __func__,
119                         TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
120                         TAS2563_PAGE_REG(reg));
122 end:
123         mutex_unlock(&pTAS2563->dev_lock);
124         return nResult;
127 static int tas2563_dev_write(struct tas2563_priv *pTAS2563,
128         unsigned int reg, unsigned int value)
130         int nResult = 0;
132         mutex_lock(&pTAS2563->dev_lock);
134         nResult = tas2563_change_book_page(pTAS2563,
135                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
136         if (nResult < 0)
137                 goto end;
139         nResult = regmap_write(pTAS2563->regmap, TAS2563_PAGE_REG(reg),
140                         value);
141         if (nResult < 0)
142                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
143                         __func__, __LINE__, nResult);
144         else
145                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u, VAL: 0x%02x\n",
146                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
147                         TAS2563_PAGE_REG(reg), value);
149 end:
150         mutex_unlock(&pTAS2563->dev_lock);
151         return nResult;
154 static int tas2563_dev_bulk_write(struct tas2563_priv *pTAS2563,
155         unsigned int reg, u8 *pData, unsigned int nLength)
157         int nResult = 0;
159         mutex_lock(&pTAS2563->dev_lock);
161         nResult = tas2563_change_book_page(pTAS2563,
162                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
163         if (nResult < 0)
164                 goto end;
166         nResult = regmap_bulk_write(pTAS2563->regmap,
167                 TAS2563_PAGE_REG(reg), pData, nLength);
168         if (nResult < 0)
169                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
170                         __func__, __LINE__, nResult);
171         else
172                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u, len: 0x%02x\n",
173                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
174                         TAS2563_PAGE_REG(reg), nLength);
176 end:
177         mutex_unlock(&pTAS2563->dev_lock);
178         return nResult;
181 static int tas2563_dev_bulk_read(struct tas2563_priv *pTAS2563,
182         unsigned int reg, u8 *pData, unsigned int nLength)
184         int nResult = 0;
185         int i = 0;
187         mutex_lock(&pTAS2563->dev_lock);
189         nResult = tas2563_change_book_page(pTAS2563,
190                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
191         if (nResult < 0)
192                 goto end;
194         dev_dbg(pTAS2563->dev, "reg = %u, pData = %s, Lenth = %d", reg, pData, nLength);
196         #define STRIDE 4
197         /* Read chunk bytes defined by STRIDE */
198         for (i = 0; i < (nLength / STRIDE); i++) {
199                         nResult = regmap_bulk_read(pTAS2563->regmap,
200                                                         TAS2563_PAGE_REG((reg + i*STRIDE)),
201                                                         &pData[i*STRIDE], STRIDE);
202                         if (nResult < 0) {
203                                         dev_err(pTAS2563->dev, "%s, %d, I2C error %d\n",
204                                                         __func__, __LINE__, nResult);
205                                         pTAS2563->mnErrCode |= ERROR_DEVA_I2C_COMM;
206                         } else
207                                         pTAS2563->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
208         }
210         /* Read remaining bytes */
211         if ((nLength % STRIDE) != 0) {
212                         nResult = regmap_bulk_read(pTAS2563->regmap,
213                                                         TAS2563_PAGE_REG(reg + i*STRIDE),
214                                                         &pData[i*STRIDE], (nLength % STRIDE));
215                         if (nResult < 0) {
216                                         dev_err(pTAS2563->dev, "%s, %d, I2C error %d\n",
217                                                         __func__, __LINE__, nResult);
218                                         pTAS2563->mnErrCode |= ERROR_DEVA_I2C_COMM;
219                         } else
220                                         pTAS2563->mnErrCode &= ~ERROR_DEVA_I2C_COMM;
221         }
224 end:
225         mutex_unlock(&pTAS2563->dev_lock);
226         return nResult;
229 static int tas2563_dev_update_bits(struct tas2563_priv *pTAS2563,
230         unsigned int reg, unsigned int mask, unsigned int value)
232         int nResult = 0;
234         mutex_lock(&pTAS2563->dev_lock);
235         nResult = tas2563_change_book_page(pTAS2563,
236                 TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg));
237         if (nResult < 0)
238                 goto end;
240         nResult = regmap_update_bits(pTAS2563->regmap,
241         TAS2563_PAGE_REG(reg), mask, value);
242         if (nResult < 0)
243                 dev_err(pTAS2563->dev, "%s, ERROR, L=%d, E=%d\n",
244                         __func__, __LINE__, nResult);
245         else
246                 dev_dbg(pTAS2563->dev, "%s: BOOK:PAGE:REG %u:%u:%u, mask: 0x%x, val=0x%x\n",
247                         __func__, TAS2563_BOOK_ID(reg), TAS2563_PAGE_ID(reg),
248                         TAS2563_PAGE_REG(reg), mask, value);
249 end:
250         mutex_unlock(&pTAS2563->dev_lock);
251         return nResult;
254 static const struct reg_default tas2563_reg_defaults[] = {
255         { TAS2563_Page, 0x00 },
256         { TAS2563_SoftwareReset, 0x00 },
257         { TAS2563_PowerControl, 0x0e },
258         { TAS2563_PlaybackConfigurationReg0, 0x10 },
259         { TAS2563_MiscConfigurationReg0, 0x07 },
260         { TAS2563_TDMConfigurationReg1, 0x02 },
261         { TAS2563_TDMConfigurationReg2, 0x0a },
262         { TAS2563_TDMConfigurationReg3, 0x10 },
263         { TAS2563_InterruptMaskReg0, 0xfc },
264         { TAS2563_InterruptMaskReg1, 0xb1 },
265         { TAS2563_InterruptConfiguration, 0x05 },
266         { TAS2563_MiscIRQ, 0x81 },
267         { TAS2563_ClockConfiguration, 0x0c },
269 };
271 static bool tas2563_volatile(struct device *dev, unsigned int reg)
273         return true;
276 static bool tas2563_writeable(struct device *dev, unsigned int reg)
278         return true;
280 static const struct regmap_config tas2563_i2c_regmap = {
281         .reg_bits = 8,
282         .val_bits = 8,
283         .writeable_reg = tas2563_writeable,
284         .volatile_reg = tas2563_volatile,
285 //      .reg_defaults = tas2563_reg_defaults,
286 //      .num_reg_defaults = ARRAY_SIZE(tas2563_reg_defaults),
287         .cache_type = REGCACHE_NONE,
288         .max_register = 1 * 128,
289 };
292 static void tas2563_hw_reset(struct tas2563_priv *pTAS2563)
294         if (gpio_is_valid(pTAS2563->mnResetGPIO)) {
295                 gpio_direction_output(pTAS2563->mnResetGPIO, 0);
296                 msleep(5);
297                 gpio_direction_output(pTAS2563->mnResetGPIO, 1);
298                 msleep(2);
299         }
301         pTAS2563->mnCurrentBook = -1;
302         pTAS2563->mnCurrentPage = -1;
305 void tas2563_enableIRQ(struct tas2563_priv *pTAS2563, bool enable)
307         if (enable) {
308                 if (pTAS2563->mbIRQEnable)
309                         return;
311                 if (gpio_is_valid(pTAS2563->mnIRQGPIO))
312                         enable_irq(pTAS2563->mnIRQ);
314                 schedule_delayed_work(&pTAS2563->irq_work, msecs_to_jiffies(10));
315                 pTAS2563->mbIRQEnable = true;
316         } else {
317                 if (gpio_is_valid(pTAS2563->mnIRQGPIO))
318                         disable_irq_nosync(pTAS2563->mnIRQ);
319                 pTAS2563->mbIRQEnable = false;
320         }
324 static void irq_work_routine(struct work_struct *work)
326         struct tas2563_priv *pTAS2563 =
327                 container_of(work, struct tas2563_priv, irq_work.work);
328         unsigned int nDevInt1Status = 0, nDevInt2Status = 0;
329         int nCounter = 2;
330         int nResult = 0;
332         dev_info(pTAS2563->dev, "%s\n", __func__);
333 #ifdef CONFIG_TAS2563_CODEC
334         mutex_lock(&pTAS2563->codec_lock);
335 #endif
337         if (pTAS2563->mbRuntimeSuspend) {
338                 dev_info(pTAS2563->dev, "%s, Runtime Suspended\n", __func__);
339                 goto end;
340         }
342         if (pTAS2563->mnPowerState == TAS2563_POWER_SHUTDOWN) {
343                 dev_info(pTAS2563->dev, "%s, device not powered\n", __func__);
344                 goto end;
345         }
347         nResult = regmap_write(pTAS2563->regmap, TAS2563_InterruptMaskReg0,
348                                 TAS2563_InterruptMaskReg0_Disable);
349         nResult = regmap_write(pTAS2563->regmap, TAS2563_InterruptMaskReg1,
350                                 TAS2563_InterruptMaskReg1_Disable);
352         if (nResult < 0)
353                 goto reload;
355         nResult = regmap_read(pTAS2563->regmap, TAS2563_LatchedInterruptReg0, &nDevInt1Status);
356         if (nResult >= 0)
357                 nResult = regmap_read(pTAS2563->regmap, TAS2563_LatchedInterruptReg1, &nDevInt2Status);
358         else
359                 goto reload;
361         dev_dbg(pTAS2563->dev, "IRQ status : 0x%x, 0x%x\n",
362                         nDevInt1Status, nDevInt2Status);
364         if (((nDevInt1Status & 0x3) != 0) || ((nDevInt2Status & 0x0f) != 0)) {
365                 /* in case of INT_OC, INT_OT, INT_OVLT, INT_UVLT, INT_BO */
367                 if (nDevInt1Status & TAS2563_LatchedInterruptReg0_OCEFlagSticky_Interrupt) {
368                         pTAS2563->mnErrCode |= ERROR_OVER_CURRENT;
369                         dev_err(pTAS2563->dev, "SPK over current!\n");
370                 } else
371                         pTAS2563->mnErrCode &= ~ERROR_OVER_CURRENT;
373                 if (nDevInt1Status & TAS2563_LatchedInterruptReg0_OTEFlagSticky_Interrupt) {
374                         pTAS2563->mnErrCode |= ERROR_DIE_OVERTEMP;
375                         dev_err(pTAS2563->dev, "die over temperature!\n");
376                 } else
377                         pTAS2563->mnErrCode &= ~ERROR_DIE_OVERTEMP;
379                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_VBATOVLOSticky_Interrupt) {
380                         pTAS2563->mnErrCode |= ERROR_OVER_VOLTAGE;
381                         dev_err(pTAS2563->dev, "SPK over voltage!\n");
382                 } else
383                         pTAS2563->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
385                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_VBATUVLOSticky_Interrupt) {
386                         pTAS2563->mnErrCode |= ERROR_UNDER_VOLTAGE;
387                         dev_err(pTAS2563->dev, "SPK under voltage!\n");
388                 } else
389                         pTAS2563->mnErrCode &= ~ERROR_UNDER_VOLTAGE;
391                 if (nDevInt2Status & TAS2563_LatchedInterruptReg1_BrownOutFlagSticky_Interrupt) {
392                         pTAS2563->mnErrCode |= ERROR_BROWNOUT;
393                         dev_err(pTAS2563->dev, "brownout!\n");
394                 } else
395                         pTAS2563->mnErrCode &= ~ERROR_BROWNOUT;
397                 goto reload;
398         } else {
399                 nCounter = 2;
401                 while (nCounter > 0) {
402                         nResult = regmap_read(pTAS2563->regmap, TAS2563_PowerControl, &nDevInt1Status);
403                         if (nResult < 0)
404                                 goto reload;
406                         if ((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
407                                 != TAS2563_PowerControl_OperationalMode10_Shutdown)
408                                 break;
410                         nCounter--;
411                         if (nCounter > 0) {
412                                 /* in case check pow status just after power on TAS2563 */
413                                 dev_dbg(pTAS2563->dev, "PowSts B: 0x%x, check again after 10ms\n",
414                                         nDevInt1Status);
415                                 msleep(10);
416                         }
417                 }
419                 if ((nDevInt1Status & TAS2563_PowerControl_OperationalMode10_Mask)
420                         == TAS2563_PowerControl_OperationalMode10_Shutdown) {
421                         dev_err(pTAS2563->dev, "%s, Critical ERROR REG[0x%x] = 0x%x\n",
422                                 __func__,
423                                 TAS2563_PowerControl,
424                                 nDevInt1Status);
425                         pTAS2563->mnErrCode |= ERROR_CLASSD_PWR;
426                         goto reload;
427                 }
428                 pTAS2563->mnErrCode &= ~ERROR_CLASSD_PWR;
429         }
431         nResult = regmap_write(pTAS2563->regmap, TAS2563_InterruptMaskReg0, 0xfc);
432         if (nResult < 0)
433                 goto reload;
435         nResult = regmap_write(pTAS2563->regmap, TAS2563_InterruptMaskReg1, 0xb1);
436         if (nResult < 0)
437                 goto reload;
439         goto end;
441 reload:
442         /* hardware reset and reload */
443         nResult = -1;
444         //tas2563_LoadConfig(pTAS2563);
445         tas2563_set_program(pTAS2563, pTAS2563->mnCurrentProgram, pTAS2563->mnCurrentConfiguration);
447         if (nResult >= 0) {
448                 tas2563_enableIRQ(pTAS2563, true);
449         }
450         
451 end:
452 #ifdef CONFIG_TAS2563_CODEC
453         mutex_unlock(&pTAS2563->codec_lock);
454 #endif
457 static enum hrtimer_restart timer_func(struct hrtimer *timer)
459         struct tas2563_priv *pTAS2563 = container_of(timer,
460                 struct tas2563_priv, mtimerwork);
462         if (pTAS2563->mnPowerState != TAS2563_POWER_SHUTDOWN) {
463                 if (!delayed_work_pending(&pTAS2563->irq_work))
464                         schedule_delayed_work(&pTAS2563->irq_work,
465                                 msecs_to_jiffies(20));
466         }
468         return HRTIMER_NORESTART;
471 static irqreturn_t tas2563_irq_handler(int irq, void *dev_id)
473         struct tas2563_priv *pTAS2563 = (struct tas2563_priv *)dev_id;
475         tas2563_enableIRQ(pTAS2563, false);
476         /* get IRQ status after 100 ms */
477         schedule_delayed_work(&pTAS2563->irq_work, msecs_to_jiffies(100));
478         return IRQ_HANDLED;
482 static int tas2563_parse_dt(struct device *dev, struct tas2563_priv *pTAS2563)
484         struct device_node *np = dev->of_node;
485         int rc = 0, ret = 0;
487         rc = of_property_read_u32(np, "ti,asi-format", &pTAS2563->mnASIFormat);
488         if (rc) {
489                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
490                         "ti,asi-format", np->full_name, rc);
491         } else {
492                 dev_dbg(pTAS2563->dev, "ti,asi-format=%d",
493                         pTAS2563->mnASIFormat);
494         }
496         pTAS2563->mnResetGPIO = of_get_named_gpio(np, "ti,reset-gpio", 0);
497         if (!gpio_is_valid(pTAS2563->mnResetGPIO)) {
498                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
499                         "ti,reset-gpio", np->full_name, pTAS2563->mnResetGPIO);
500         } else {
501                 dev_dbg(pTAS2563->dev, "ti,reset-gpio=%d",
502                         pTAS2563->mnResetGPIO);
503         }
505         pTAS2563->mnIRQGPIO = of_get_named_gpio(np, "ti,irq-gpio", 0);
506         if (!gpio_is_valid(pTAS2563->mnIRQGPIO)) {
507                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
508                         "ti,irq-gpio", np->full_name, pTAS2563->mnIRQGPIO);
509         } else {
510                 dev_dbg(pTAS2563->dev, "ti,irq-gpio=%d", pTAS2563->mnIRQGPIO);
511         }
513         of_property_read_u32(np, "ti,left-slot", &pTAS2563->mnLeftSlot);
514         if (rc) {
515                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
516                         "ti,left-slot", np->full_name, rc);
517         } else {
518                 dev_dbg(pTAS2563->dev, "ti,left-slot=%d",
519                         pTAS2563->mnLeftSlot);
520         }
522         of_property_read_u32(np, "ti,right-slot", &pTAS2563->mnRightSlot);
523         if (rc) {
524                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
525                         "ti,right-slot", np->full_name, rc);
526         } else {
527                 dev_dbg(pTAS2563->dev, "ti,right-slot=%d",
528                         pTAS2563->mnRightSlot);
529         }
531         of_property_read_u32(np, "ti,imon-slot-no", &pTAS2563->mnImon_slot_no);
532         if (rc) {
533                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
534                         "ti,imon-slot-no", np->full_name, rc);
535         } else {
536                 dev_dbg(pTAS2563->dev, "ti,imon-slot-no=%d",
537                         pTAS2563->mnImon_slot_no);
538         }
540         of_property_read_u32(np, "ti,vmon-slot-no", &pTAS2563->mnVmon_slot_no);
541         if (rc) {
542                 dev_err(pTAS2563->dev, "Looking up %s property in node %s failed %d\n",
543                         "ti,vmon-slot-no", np->full_name, rc);
544         } else {
545                 dev_dbg(pTAS2563->dev, "ti,vmon-slot-no=%d",
546                         pTAS2563->mnVmon_slot_no);
547         }
549         return ret;
553 static int tas2563_runtime_suspend(struct tas2563_priv *pTAS2563)
555         dev_dbg(pTAS2563->dev, "%s\n", __func__);
557         pTAS2563->mbRuntimeSuspend = true;
559         if (gpio_is_valid(pTAS2563->mnIRQGPIO)) {
560                 if (delayed_work_pending(&pTAS2563->irq_work)) {
561                         dev_dbg(pTAS2563->dev, "cancel IRQ work\n");
562                         cancel_delayed_work_sync(&pTAS2563->irq_work);
563                 }
564         }
566         return 0;
569 static int tas2563_runtime_resume(struct tas2563_priv *pTAS2563)
571         struct TProgram *pProgram;
573         dev_dbg(pTAS2563->dev, "%s\n", __func__);
574         if (!pTAS2563->mpFirmware->mpPrograms) {
575                 dev_dbg(pTAS2563->dev, "%s, firmware not loaded\n", __func__);
576                 goto end;
577         }
579         if (pTAS2563->mnCurrentProgram >= pTAS2563->mpFirmware->mnPrograms) {
580                 dev_err(pTAS2563->dev, "%s, firmware corrupted\n", __func__);
581                 goto end;
582         }
584         pProgram = &(pTAS2563->mpFirmware->mpPrograms[pTAS2563->mnCurrentProgram]);
586         pTAS2563->mbRuntimeSuspend = false;
587 end:
589         return 0;
593 /* tas2563_i2c_probe :
594 * platform dependent
595 * should implement hardware reset functionality
596 */
597 static int tas2563_i2c_probe(struct i2c_client *pClient,
598         const struct i2c_device_id *pID)
600         struct tas2563_priv *pTAS2563;
601         int nResult = 0;
602         unsigned int nValue = 0;
603         const char *pFWName;
605         dev_info(&pClient->dev, "%s enter\n", __func__);
607         pTAS2563 = devm_kzalloc(&pClient->dev, sizeof(struct tas2563_priv), GFP_KERNEL);
608         if (!pTAS2563) {
609                 nResult = -ENOMEM;
610                 goto err;
611         }
613         pTAS2563->dev = &pClient->dev;
614         i2c_set_clientdata(pClient, pTAS2563);
615         dev_set_drvdata(&pClient->dev, pTAS2563);
617         pTAS2563->regmap = devm_regmap_init_i2c(pClient, &tas2563_i2c_regmap);
618         if (IS_ERR(pTAS2563->regmap)) {
619                 nResult = PTR_ERR(pTAS2563->regmap);
620                 dev_err(&pClient->dev, "Failed to allocate register map: %d\n",
621                         nResult);
622                 goto err;
623         }
625         if (pClient->dev.of_node)
626                 tas2563_parse_dt(&pClient->dev, pTAS2563);
628         if (gpio_is_valid(pTAS2563->mnResetGPIO)) {
629                 nResult = gpio_request(pTAS2563->mnResetGPIO, "TAS2563-RESET");
630                 if (nResult < 0) {
631                         dev_err(pTAS2563->dev, "%s: GPIO %d request error\n",
632                                 __func__, pTAS2563->mnResetGPIO);
633                         goto err;
634                 }
635                 tas2563_hw_reset(pTAS2563);
636         }
638         pTAS2563->read = tas2563_dev_read;
639         pTAS2563->write = tas2563_dev_write;
640         pTAS2563->bulk_read = tas2563_dev_bulk_read;
641         pTAS2563->bulk_write = tas2563_dev_bulk_write;
642         pTAS2563->update_bits = tas2563_dev_update_bits;
643         pTAS2563->enableIRQ = tas2563_enableIRQ;
644 //      pTAS2563->clearIRQ = tas2563_clearIRQ;
645         pTAS2563->hw_reset = tas2563_hw_reset;
646         pTAS2563->runtime_suspend = tas2563_runtime_suspend;
647         pTAS2563->runtime_resume = tas2563_runtime_resume;
648         pTAS2563->mnRestart = 0;
649         pTAS2563->mnPowerState = TAS2563_POWER_SHUTDOWN;
651         mutex_init(&pTAS2563->dev_lock);
653         /* Reset the chip */
654         nResult = tas2563_dev_write(pTAS2563, TAS2563_SoftwareReset, 0x01);
655         if (nResult < 0) {
656                 dev_err(&pClient->dev, "I2c fail, %d\n", nResult);
657 //              goto err;
658         }
660         msleep(1);
661         nResult = tas2563_dev_read(pTAS2563, TAS2563_RevisionandPGID, &nValue);
662         pTAS2563->mnPGID = nValue;
663         dev_info(pTAS2563->dev, "PGID: %d\n", pTAS2563->mnPGID);
664         pFWName = TAS2563_FW_NAME;
666         if (gpio_is_valid(pTAS2563->mnIRQGPIO)) {
667                 nResult = gpio_request(pTAS2563->mnIRQGPIO, "TAS2563-IRQ");
668                 if (nResult < 0) {
669                         dev_err(pTAS2563->dev,
670                                 "%s: GPIO %d request INT error\n",
671                                 __func__, pTAS2563->mnIRQGPIO);
672                         goto err;
673                 }
675                 gpio_direction_input(pTAS2563->mnIRQGPIO);
676                 pTAS2563->mnIRQ = gpio_to_irq(pTAS2563->mnIRQGPIO);
677                 dev_dbg(pTAS2563->dev, "irq = %d\n", pTAS2563->mnIRQ);
678                 INIT_DELAYED_WORK(&pTAS2563->irq_work, irq_work_routine);
679                 nResult = request_threaded_irq(pTAS2563->mnIRQ, tas2563_irq_handler,
680                                         NULL, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
681                                         pClient->name, pTAS2563);
682                 if (nResult < 0) {
683                         dev_err(pTAS2563->dev,
684                                 "request_irq failed, %d\n", nResult);
685                         goto err;
686                 }
687                 disable_irq_nosync(pTAS2563->mnIRQ);
688         }
690         pTAS2563->mpFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
691         if (!pTAS2563->mpFirmware) {
692                 nResult = -ENOMEM;
693                 goto err;
694         }
696         pTAS2563->mpCalFirmware = devm_kzalloc(&pClient->dev, sizeof(struct TFirmware), GFP_KERNEL);
697         if (!pTAS2563->mpCalFirmware) {
698                 nResult = -ENOMEM;
699                 goto err;
700         }
702 #ifdef CONFIG_TAS2563_CODEC
703         mutex_init(&pTAS2563->codec_lock);
704         nResult = tas2563_register_codec(pTAS2563);
705         if (nResult < 0) {
706                 dev_err(pTAS2563->dev,
707                         "register codec failed, %d\n", nResult);
708                 goto err;
709         }
710 #endif
712 #ifdef CONFIG_TAS2563_MISC
713         mutex_init(&pTAS2563->file_lock);
714         nResult = tas2563_register_misc(pTAS2563);
715         if (nResult < 0) {
716                 dev_err(pTAS2563->dev,
717                         "register codec failed, %d\n", nResult);
718                 goto err;
719         }
720 #endif
722 #ifdef ENABLE_TILOAD
723         tiload_driver_init(pTAS2563);
724 #endif
726         hrtimer_init(&pTAS2563->mtimerwork, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
727         pTAS2563->mtimerwork.function = timer_func;
728         //INIT_WORK(&pTAS2563->mtimerwork, timer_func);
730         request_firmware_nowait(THIS_MODULE, 1, pFWName,
731                 pTAS2563->dev, GFP_KERNEL, pTAS2563, tas2563_fw_ready);
733 err:
735         return nResult;
738 static int tas2563_i2c_remove(struct i2c_client *pClient)
740         struct tas2563_priv *pTAS2563 = i2c_get_clientdata(pClient);
742         dev_info(pTAS2563->dev, "%s\n", __func__);
744 #ifdef CONFIG_TAS2563_CODEC
745         tas2563_deregister_codec(pTAS2563);
746         mutex_destroy(&pTAS2563->codec_lock);
747 #endif
749 #ifdef CONFIG_TAS2563_MISC
750         tas2563_deregister_misc(pTAS2563);
751         mutex_destroy(&pTAS2563->file_lock);
752 #endif
754         mutex_destroy(&pTAS2563->dev_lock);
755         return 0;
758 static const struct i2c_device_id tas2563_i2c_id[] = {
759         {"tas2563", 0},
760         {}
761 };
763 MODULE_DEVICE_TABLE(i2c, tas2563_i2c_id);
765 #if defined(CONFIG_OF)
766 static const struct of_device_id tas2563_of_match[] = {
767         {.compatible = "ti,tas2563"},
768         {},
769 };
771 MODULE_DEVICE_TABLE(of, tas2563_of_match);
772 #endif
774 static struct i2c_driver tas2563_i2c_driver = {
775         .driver = {
776                         .name = "tas2563",
777                         .owner = THIS_MODULE,
778 #if defined(CONFIG_OF)
779                         .of_match_table = of_match_ptr(tas2563_of_match),
780 #endif
781                 },
782         .probe = tas2563_i2c_probe,
783         .remove = tas2563_i2c_remove,
784         .id_table = tas2563_i2c_id,
785 };
787 module_i2c_driver(tas2563_i2c_driver);
789 MODULE_AUTHOR("Texas Instruments Inc.");
790 MODULE_DESCRIPTION("TAS2563 I2C Smart Amplifier driver");
791 MODULE_LICENSE("GPL v2");
793 #endif