blob: 5cefcf43615f87db19c49ac5a5e59363aea160de [file] [log] [blame]
Flemmard9dc34f32014-01-05 00:13:45 -08001/* driver/i2c/chip/tap6185.c
2 *
3 * TI rt5501 Speaker Amp
4 *
5 * Copyright (C) 2010 HTC Corporation
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/interrupt.h>
19#include <linux/i2c.h>
20#include <linux/slab.h>
21#include <linux/irq.h>
22#include <linux/miscdevice.h>
23#include <asm/uaccess.h>
24#include <linux/delay.h>
25#include <linux/input.h>
26#include <linux/workqueue.h>
27#include <linux/freezer.h>
28#include <linux/rt5501.h>
29#include <linux/mutex.h>
30#include <linux/debugfs.h>
31#include <linux/gpio.h>
32#include <linux/module.h>
33#include <linux/mfd/pm8xxx/pm8921.h>
34#include <linux/mfd/pm8xxx/pm8921.h>
35#include <mach/htc_headset_mgr.h>
36#include <linux/wakelock.h>
37#include <mach/htc_acoustic_pmic.h>
38#include <linux/jiffies.h>
39
40#ifdef CONFIG_AMP_RT5501_ON_GPIO
41#define DEBUG (1)
42#else
43#define DEBUG (1)
44#endif
45#define AMP_ON_CMD_LEN 7
46#define RETRY_CNT 5
47
48#define DRIVER_NAME "RT5501"
49
50struct headset_query {
51 struct mutex mlock;
52 struct mutex gpiolock;
53 struct delayed_work hs_imp_detec_work;
54 struct wake_lock hs_wake_lock;
55 struct wake_lock gpio_wake_lock;
56 enum HEADSET_QUERY_STATUS hs_qstatus;
57 enum RT5501_STATUS rt5501_status;
58 enum HEADSET_OM headsetom;
59 enum RT5501_Mode curmode;
60 enum AMP_GPIO_STATUS gpiostatus;
61 enum AMP_S4_STATUS s4status;
62 int action_on;
63 int gpio_off_cancel;
64 struct mutex actionlock;
65 struct delayed_work volume_ramp_work;
66 struct delayed_work gpio_off_work;
67};
68
69static struct i2c_client *this_client;
70static struct rt5501_platform_data *pdata;
71static int rt5501Connect = 0;
72static int MFG_MODE = 0;
73
74struct rt5501_config_data rt5501_config_data;
75static struct mutex hp_amp_lock;
76static int rt5501_opened;
77static int last_spkamp_state;
78struct rt5501_config RT5501_AMP_ON = {6,{{0x1,0x1c},{0x2,0x00},{0x7,0x7f},{0x9,0x1},{0xa,0x0},{0xb,0xc7},}};
79struct rt5501_config RT5501_AMP_INIT = {11,{{0,0xc0},{0x81,0x30},{0x87,0xf6},{0x93,0x8d},{0x95,0x7d},{0xa4,0x52},\
80 {0x96,0xae},{0x97,0x13},{0x99,0x35},{0x9b,0x68},{0x9d,0x68},}};
81
82struct rt5501_config RT5501_AMP_MUTE = {1,{{0x1,0xC7},}};;
83struct rt5501_config RT5501_AMP_OFF = {1,{{0x0,0x1},}};
84
85static int rt5501_write_reg(u8 reg, u8 val);
86static int rt5501_i2c_write_for_read(char *txData, int length);
87static int rt5501_i2c_read(char *rxData, int length);
88static void hs_imp_detec_func(struct work_struct *work);
89static int rt5501_i2c_read_addr(char *rxData, unsigned char addr);
90static int rt5501_i2c_write(struct rt5501_reg_data *txData, int length);
91static void set_amp(int on, struct rt5501_config *i2c_command);
92
93struct headset_query rt5501_query;
94static struct workqueue_struct *hs_wq;
95
96static struct workqueue_struct *ramp_wq;
97static struct workqueue_struct *gpio_wq;
98static int high_imp = 0;
99static u64 last_hp_remove = 0;
100#if 0
101static int query_playback(void *pdata)
102{
103 return 0;
104}
105#endif
106
107static int rt5501_headset_detect(int on)
108{
109
110 if(on) {
111 pr_info("%s: headset in ++\n",__func__);
112 mutex_lock(&rt5501_query.mlock);
113
114 if(rt5501_query.headsetom == HEADSET_OM_UNDER_DETECT || \
115 time_after64(get_jiffies_64(),last_hp_remove + msecs_to_jiffies(500))) {
116 rt5501_query.hs_qstatus = RT5501_QUERY_HEADSET;
117 rt5501_query.headsetom = HEADSET_OM_UNDER_DETECT;
118 } else {
119 rt5501_query.hs_qstatus = RT5501_QUERY_FINISH;
120 }
121
122 mutex_unlock(&rt5501_query.mlock);
123
124 cancel_delayed_work_sync(&rt5501_query.hs_imp_detec_work);
125
126 mutex_lock(&rt5501_query.gpiolock);
127 mutex_lock(&rt5501_query.mlock);
128
129 if(rt5501_query.rt5501_status == RT5501_PLAYBACK) {
130
131 if(high_imp) {
132 rt5501_write_reg(1,0x7);
133 rt5501_write_reg(0xb1,0x81);
134 } else {
135 rt5501_write_reg(1,0xc7);
136
137 }
138 last_spkamp_state = 0;
139 pr_info("%s: OFF\n", __func__);
140 rt5501_query.rt5501_status = RT5501_SUSPEND;
141 }
142 pr_info("%s: headset in --\n",__func__);
143 mutex_unlock(&rt5501_query.mlock);
144 mutex_unlock(&rt5501_query.gpiolock);
145
146 queue_delayed_work(hs_wq,&rt5501_query.hs_imp_detec_work,msecs_to_jiffies(5));
147 pr_info("%s: headset in --2\n",__func__);
148
149 } else {
150
151 pr_info("%s: headset remove ++\n",__func__);
152 flush_work_sync(&rt5501_query.volume_ramp_work.work);
153 mutex_lock(&rt5501_query.mlock);
154 rt5501_query.hs_qstatus = RT5501_QUERY_OFF;
155 mutex_unlock(&rt5501_query.mlock);
156
157 cancel_delayed_work_sync(&rt5501_query.hs_imp_detec_work);
158
159 mutex_lock(&rt5501_query.gpiolock);
160 mutex_lock(&rt5501_query.mlock);
161
162
163 if(rt5501_query.rt5501_status == RT5501_PLAYBACK) {
164
165 if(high_imp) {
166 rt5501_write_reg(1,0x7);
167 rt5501_write_reg(0xb1,0x81);
168 } else {
169 rt5501_write_reg(1,0xc7);
170
171 }
172
173 last_spkamp_state = 0;
174 pr_info("%s: OFF\n", __func__);
175
176 rt5501_query.rt5501_status = RT5501_SUSPEND;
177 }
178
179 rt5501_query.curmode = RT5501_MODE_OFF;
180 pr_info("%s: headset remove --1\n",__func__);
181
182 if(high_imp) {
183 int closegpio = 0;
184 if((rt5501_query.gpiostatus == AMP_GPIO_OFF) && pdata->gpio_rt5501_spk_en) {
185
186 if(rt5501_query.s4status == AMP_S4_AUTO) {
187 pm8921_aud_set_s4_pwm();
188 rt5501_query.s4status = AMP_S4_PWM;
189 msleep(1);
190 }
191
192 pr_info("%s: enable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
193 gpio_direction_output(pdata->gpio_rt5501_spk_en, 1);
194 rt5501_query.gpiostatus = AMP_GPIO_ON;
195 closegpio = 1;
196 msleep(1);
197 }
198 pr_info("%s: reset rt5501\n",__func__);
199 rt5501_write_reg(0x0,0x4);
200 mdelay(1);
201
202 rt5501_write_reg(0x1,0xc7);
203 high_imp = 0;
204
205 if(closegpio && (rt5501_query.gpiostatus == AMP_GPIO_ON) && pdata->gpio_rt5501_spk_en) {
206 pr_info("%s: disable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
207 gpio_direction_output(pdata->gpio_rt5501_spk_en, 0);
208 rt5501_query.gpiostatus = AMP_GPIO_OFF;
209
210 if(rt5501_query.s4status == AMP_S4_PWM) {
211 pm8921_aud_set_s4_auto();
212 rt5501_query.s4status = AMP_S4_AUTO;
213 }
214 }
215 }
216 last_hp_remove = get_jiffies_64();
217 mutex_unlock(&rt5501_query.mlock);
218 mutex_unlock(&rt5501_query.gpiolock);
219
220 pr_info("%s: headset remove --2\n",__func__);
221 }
222
223 return 0;
224}
225
226static int rt5501_write_reg(u8 reg, u8 val)
227{
228 int err;
229 struct i2c_msg msg[1];
230 unsigned char data[2];
231
232 msg->addr = this_client->addr;
233 msg->flags = 0;
234 msg->len = 2;
235 msg->buf = data;
236 data[0] = reg;
237 data[1] = val;
238 pr_info("%s: write reg 0x%x val 0x%x\n",__func__,data[0],data[1]);
239 err = i2c_transfer(this_client->adapter, msg, 1);
240 if (err >= 0)
241 return 0;
242 else {
243
244 pr_info("%s: write error error %d\n",__func__,err);
245 return err;
246 }
247}
248
249static int rt5501_i2c_write(struct rt5501_reg_data *txData, int length)
250{
251 int i, retry, pass = 0;
252 char buf[2];
253 struct i2c_msg msg[] = {
254 {
255 .addr = this_client->addr,
256 .flags = 0,
257 .len = 2,
258 .buf = buf,
259 },
260 };
261 for (i = 0; i < length; i++) {
262
263
264 buf[0] = txData[i].addr;
265 buf[1] = txData[i].val;
266
267#if DEBUG
268 pr_info("%s:i2c_write addr 0x%x val 0x%x\n", __func__,buf[0], buf[1]);
269#endif
270 msg->buf = buf;
271 retry = RETRY_CNT;
272 pass = 0;
273 while (retry--) {
274 if (i2c_transfer(this_client->adapter, msg, 1) < 0) {
275 pr_err("%s: I2C transfer error %d retry %d\n",
276 __func__, i, retry);
277 msleep(20);
278 } else {
279 pass = 1;
280 break;
281 }
282 }
283 if (pass == 0) {
284 pr_err("I2C transfer error, retry fail\n");
285 return -EIO;
286 }
287 }
288 return 0;
289}
290
291static int rt5501_i2c_write_for_read(char *txData, int length)
292{
293 int i, retry, pass = 0;
294 char buf[2];
295 struct i2c_msg msg[] = {
296 {
297 .addr = this_client->addr,
298 .flags = 0,
299 .len = 2,
300 .buf = buf,
301 },
302 };
303 for (i = 0; i < length; i++) {
304
305
306 buf[0] = i;
307 buf[1] = txData[i];
308#if DEBUG
309 pr_info("i2c_write %d=%x\n", i, buf[1]);
310#endif
311 msg->buf = buf;
312 retry = RETRY_CNT;
313 pass = 0;
314 while (retry--) {
315 if (i2c_transfer(this_client->adapter, msg, 1) < 0) {
316 pr_err("%s: I2C transfer error %d retry %d\n",
317 __func__, i, retry);
318 msleep(20);
319 } else {
320 pass = 1;
321 break;
322 }
323 }
324 if (pass == 0) {
325 pr_err("I2C transfer error, retry fail\n");
326 return -EIO;
327 }
328 }
329 return 0;
330}
331
332static int rt5501_i2c_read(char *rxData, int length)
333{
334 int rc;
335 struct i2c_msg msgs[] = {
336 {
337 .addr = this_client->addr,
338 .flags = I2C_M_RD,
339 .len = length,
340 .buf = rxData,
341 },
342 };
343
344 rc = i2c_transfer(this_client->adapter, msgs, 1);
345 if (rc < 0) {
346 pr_err("%s: transfer error %d\n", __func__, rc);
347 return rc;
348 }
349
350 {
351 int i = 0;
352 for (i = 0; i < length; i++)
353 pr_info("i2c_read %s: rx[%d] = 0x%x\n", __func__, i, \
354 rxData[i]);
355 }
356
357 return 0;
358}
359
360static int rt5501_i2c_read_addr(char *rxData, unsigned char addr)
361{
362 int rc;
363 struct i2c_msg msgs[] = {
364 {
365 .addr = this_client->addr,
366 .flags = 0,
367 .len = 1,
368 .buf = rxData,
369 },
370 {
371 .addr = this_client->addr,
372 .flags = I2C_M_RD,
373 .len = 1,
374 .buf = rxData,
375 },
376 };
377
378 if(!rxData)
379 return -1;
380
381 *rxData = addr;
382
383 rc = i2c_transfer(this_client->adapter, &msgs[0], 1);
384 if (rc < 0) {
385 pr_err("%s: transfer error %d\n", __func__, rc);
386 return rc;
387 }
388
389 rc = i2c_transfer(this_client->adapter, &msgs[1], 1);
390 if (rc < 0) {
391 pr_err("%s: transfer error %d\n", __func__, rc);
392 return rc;
393 }
394
395 pr_info("%s:i2c_read addr 0x%x value = 0x%x\n", __func__, addr, *rxData);
396 return 0;
397}
398
399static int rt5501_open(struct inode *inode, struct file *file)
400{
401 int rc = 0;
402
403 mutex_lock(&hp_amp_lock);
404
405 if (rt5501_opened) {
406 pr_err("%s: busy\n", __func__);
407 rc = -EBUSY;
408 goto done;
409 }
410 rt5501_opened = 1;
411done:
412 mutex_unlock(&hp_amp_lock);
413 return rc;
414}
415
416static int rt5501_release(struct inode *inode, struct file *file)
417{
418 mutex_lock(&hp_amp_lock);
419 rt5501_opened = 0;
420 mutex_unlock(&hp_amp_lock);
421
422 return 0;
423}
424#if 0
425static int init_rt5501(void)
426{
427 int ret;
428
429 ret = rt5501_i2c_write(RT5501_AMP_INIT.reg, RT5501_AMP_INIT.reg_len);
430
431 if(ret < 0) {
432 pr_err("init rt5501 error %d\n",ret);
433 return ret;
434 }
435#if 0
436 ret = rt5501_i2c_write(RT5501_AMP_ON.reg, RT5501_AMP_ON.reg_len);
437
438 if(ret < 0) {
439 pr_err("init rt5501 to playback error %d\n",ret);
440 return ret;
441 }
442
443 ret = rt5501_i2c_write(RT5501_AMP_MUTE.reg, RT5501_AMP_MUTE.reg_len);
444
445 if(ret < 0) {
446 pr_err("init rt5501 to mute error %d\n",ret);
447 return ret;
448 }
449
450 ret = rt5501_i2c_write(RT5501_AMP_OFF.reg, RT5501_AMP_OFF.reg_len);
451
452 if(ret < 0) {
453 pr_err("init rt5501 to off error %d\n",ret);
454 return ret;
455 }
456#endif
457 return ret;
458}
459#endif
460static void hs_imp_gpio_off(struct work_struct *work)
461{
462 u64 timeout = get_jiffies_64() + 5*HZ;
463 wake_lock(&rt5501_query.gpio_wake_lock);
464
465 while(1) {
466 if(time_after64(get_jiffies_64(),timeout))
467 break;
468 else if(rt5501_query.gpio_off_cancel) {
469 wake_unlock(&rt5501_query.gpio_wake_lock);
470 return;
471 } else
472 msleep(10);
473 }
474
475 mutex_lock(&rt5501_query.gpiolock);
476 pr_info("%s: disable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
477 gpio_direction_output(pdata->gpio_rt5501_spk_en, 0);
478 rt5501_query.gpiostatus = AMP_GPIO_OFF;
479
480 if(rt5501_query.s4status == AMP_S4_PWM) {
481 pm8921_aud_set_s4_auto();
482 rt5501_query.s4status = AMP_S4_AUTO;
483 }
484
485 mutex_unlock(&rt5501_query.gpiolock);
486 wake_unlock(&rt5501_query.gpio_wake_lock);
487}
488
489static void hs_imp_detec_func(struct work_struct *work)
490{
491 struct headset_query *hs;
492 char temp[8]={0x1,};
493 int ret;
494 int rt5501_status;
495 pr_info("%s: read rt5501 hs imp \n",__func__);
496
497 hs = container_of(work, struct headset_query, hs_imp_detec_work.work);
498 wake_lock(&hs->hs_wake_lock);
499
500 rt5501_query.gpio_off_cancel = 1;
501 cancel_delayed_work_sync(&rt5501_query.gpio_off_work);
502 mutex_lock(&hs->gpiolock);
503 mutex_lock(&hs->mlock);
504
505 if((hs->gpiostatus == AMP_GPIO_OFF) && pdata->gpio_rt5501_spk_en) {
506
507 if(rt5501_query.s4status == AMP_S4_AUTO) {
508 pm8921_aud_set_s4_pwm();
509 rt5501_query.s4status = AMP_S4_PWM;
510 msleep(1);
511 }
512 pr_info("%s: enable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
513 gpio_direction_output(pdata->gpio_rt5501_spk_en, 1);
514 rt5501_query.gpiostatus = AMP_GPIO_ON;
515 }
516
517 msleep(1);
518
519 if(hs->hs_qstatus == RT5501_QUERY_HEADSET) {
520 rt5501_write_reg(0,0x04);
521 rt5501_write_reg(0xa4,0x52);
522
523 rt5501_write_reg(1,0x7);
524 msleep(10);
525 rt5501_write_reg(0x3,0x81);
526
527 msleep(101);
528
529 ret = rt5501_i2c_read_addr(temp,0x4);
530
531 if(ret < 0) {
532 pr_err("%s: read rt5501 status error %d\n",__func__,ret);
533
534 if((hs->gpiostatus == AMP_GPIO_ON) && pdata->gpio_rt5501_spk_en) {
535
536 rt5501_query.gpio_off_cancel = 0;
537 queue_delayed_work(gpio_wq, &rt5501_query.gpio_off_work, msecs_to_jiffies(0));
538 }
539
540 mutex_unlock(&hs->mlock);
541 mutex_unlock(&hs->gpiolock);
542 wake_unlock(&hs->hs_wake_lock);
543 return;
544 }
545
546 temp[1] = 0x4;
547 rt5501_i2c_read_addr(&temp[1],0x6);
548
549 if(temp[0] & RT5501_SENSE_READY) {
550
551 unsigned char om, hsmode;
552 enum HEADSET_OM hsom;
553
554 high_imp = 0;
555 hsmode = (temp[0] & 0x30) >> 4;
556 om = (temp[0] & 0xe) >> 1;
557
558 if((temp[0] == 0xc0 || temp[0] == 0xc1) && (temp[1] == 0)) {
559
560 hsom = HEADSET_MONO;
561 } else {
562
563 switch(om) {
564 case 0:
565 hsom = HEADSET_8OM;
566 break;
567 case 1:
568 hsom = HEADSET_16OM;
569 break;
570 case 2:
571 hsom = HEADSET_32OM;
572 break;
573 case 3:
574 hsom = HEADSET_64OM;
575 break;
576 case 4:
577 hsom = HEADSET_128OM;
578 break;
579 case 5:
580 hsom = HEADSET_256OM;
581 break;
582 case 6:
583 hsom = HEADSET_500OM;
584 break;
585 case 7:
586 hsom = HEADSET_1KOM;
587 break;
588
589 default:
590 hsom = HEADSET_OM_UNDER_DETECT;
591 break;
592 }
593 }
594
595 hs->hs_qstatus = RT5501_QUERY_FINISH;
596 hs->headsetom = hsom;
597
598 if(om >= HEADSET_256OM && om <= HEADSET_1KOM)
599 high_imp = 1;
600
601 pr_info("rt5501 hs imp value 0x%x hsmode %d om 0x%x hsom %d high_imp %d\n", \
602 temp[0] & 0xf,hsmode,om,hsom,high_imp);
603
604 } else {
605
606 if(hs->hs_qstatus == RT5501_QUERY_HEADSET)
607 queue_delayed_work(hs_wq,&rt5501_query.hs_imp_detec_work,QUERY_LATTER);
608 }
609
610 }
611
612 rt5501_write_reg(0x0,0x4);
613 mdelay(1);
614
615 rt5501_write_reg(0x0,0xc0);
616 rt5501_write_reg(0x81,0x30);
617
618 rt5501_write_reg(0x90,0xd0);
619 rt5501_write_reg(0x93,0x9d);
620 rt5501_write_reg(0x95,0x7b);
621 rt5501_write_reg(0xa4,0x01);
622
623 rt5501_write_reg(0x97,0x11);
624 rt5501_write_reg(0x98,0x22);
625 rt5501_write_reg(0x99,0x44);
626 rt5501_write_reg(0x9a,0x55);
627 rt5501_write_reg(0x9b,0x66);
628 rt5501_write_reg(0x9c,0x99);
629 rt5501_write_reg(0x9d,0x66);
630 rt5501_write_reg(0x9e,0x99);
631
632 rt5501_status = hs->rt5501_status;
633
634 if(high_imp) {
635 rt5501_write_reg(0xb1,0x81);
636 rt5501_write_reg(0x80,0x87);
637 rt5501_write_reg(0x83,0xc3);
638 rt5501_write_reg(0x84,0x63);
639 rt5501_write_reg(0x89,0x7);
640 mdelay(9);
641 rt5501_write_reg(0x83,0xcf);
642 rt5501_write_reg(0x89,0x1d);
643 mdelay(1);
644
645 rt5501_write_reg(1,0x7);
646 rt5501_write_reg(0xb1,0x81);
647 } else {
648
649 rt5501_write_reg(1,0xc7);
650 }
651
652
653 if((hs->gpiostatus == AMP_GPIO_ON) && pdata->gpio_rt5501_spk_en) {
654 rt5501_query.gpio_off_cancel = 0;
655 queue_delayed_work(gpio_wq, &rt5501_query.gpio_off_work, msecs_to_jiffies(0));
656 }
657
658 mutex_unlock(&hs->mlock);
659 mutex_unlock(&hs->gpiolock);
660
661 if(rt5501_status == RT5501_SUSPEND)
662 set_rt5501_amp(1);
663
664 wake_unlock(&hs->hs_wake_lock);
665}
666
667static void volume_ramp_func(struct work_struct *work)
668{
669 mutex_lock(&rt5501_query.actionlock);
670 if(rt5501_query.rt5501_status != RT5501_PLAYBACK) {
671 u8 val;
672 pr_info("%s: ramping-------------------------\n",__func__);
673 mdelay(1);
674
675 if(high_imp)
676 rt5501_write_reg(0xb1,0x80);
677
678 rt5501_write_reg(0x2,0x0);
679 mdelay(1);
680 val = 0x7;
681
682 if (MFG_MODE) {
683 pr_info("Skip volume ramp for MFG build");
684 val += 15;
685 rt5501_write_reg(1,val);
686 } else {
687#if 1
688 int i;
689 for(i=0; i<15; i++) {
690 if(!rt5501_query.action_on) {
691 mutex_unlock(&rt5501_query.actionlock);
692 return;
693 }
694 msleep(1);
695 rt5501_write_reg(1,val);
696 val++;
697 }
698#else
699 for(i=0; i<8; i++) {
700 msleep(10);
701 rt5501_write_reg(1,val);
702 val += 2;
703 }
704
705#endif
706 }
707 }
708
709 set_amp(1, &RT5501_AMP_ON);
710 mutex_unlock(&rt5501_query.actionlock);
711}
712
713static void set_amp(int on, struct rt5501_config *i2c_command)
714{
715 pr_info("%s: %d\n", __func__, on);
716 mutex_lock(&rt5501_query.mlock);
717 mutex_lock(&hp_amp_lock);
718
719 if(rt5501_query.hs_qstatus == RT5501_QUERY_HEADSET)
720 rt5501_query.hs_qstatus = RT5501_QUERY_FINISH;
721
722 if (on) {
723 rt5501_query.rt5501_status = RT5501_PLAYBACK;
724 if (rt5501_i2c_write(i2c_command->reg, i2c_command->reg_len) == 0) {
725 last_spkamp_state = 1;
726 pr_info("%s: ON \n",
727 __func__);
728 }
729
730
731 } else {
732
733 if(high_imp) {
734 rt5501_write_reg(1,0x7);
735 rt5501_write_reg(0xb1,0x81);
736 } else {
737 rt5501_write_reg(1,0xc7);
738 }
739
740 if(rt5501_query.rt5501_status == RT5501_PLAYBACK) {
741 last_spkamp_state = 0;
742 pr_info("%s: OFF\n", __func__);
743 }
744 rt5501_query.rt5501_status = RT5501_OFF;
745 rt5501_query.curmode = RT5501_MODE_OFF;
746 }
747 mutex_unlock(&hp_amp_lock);
748 mutex_unlock(&rt5501_query.mlock);
749}
750
751int query_rt5501(void)
752{
753 return rt5501Connect;
754}
755
756void set_rt5501_amp(int on)
757{
758 pr_info("%s: %d\n", __func__, on);
759 rt5501_query.gpio_off_cancel = 1;
760 if(!on)
761 rt5501_query.action_on = 0;
762 cancel_delayed_work_sync(&rt5501_query.gpio_off_work);
763 cancel_delayed_work_sync(&rt5501_query.volume_ramp_work);
764 flush_work_sync(&rt5501_query.volume_ramp_work.work);
765 mutex_lock(&rt5501_query.gpiolock);
766
767 if(on) {
768
769 if((rt5501_query.gpiostatus == AMP_GPIO_OFF) && pdata->gpio_rt5501_spk_en) {
770
771 if(rt5501_query.s4status == AMP_S4_AUTO) {
772 pm8921_aud_set_s4_pwm();
773 rt5501_query.s4status = AMP_S4_PWM;
774 msleep(1);
775 }
776
777#ifdef CONFIG_AMP_RT5501_DELAY
778 msleep(50);
779#endif
780 pr_info("%s: enable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
781 gpio_direction_output(pdata->gpio_rt5501_spk_en, 1);
782 rt5501_query.gpiostatus = AMP_GPIO_ON;
783 msleep(1);
784 }
785 rt5501_query.action_on = 1;
786 queue_delayed_work(ramp_wq, &rt5501_query.volume_ramp_work, msecs_to_jiffies(0));
787
788 } else {
789 set_amp(0, &RT5501_AMP_ON);
790 if((rt5501_query.gpiostatus == AMP_GPIO_ON) && pdata->gpio_rt5501_spk_en) {
791
792 rt5501_query.gpio_off_cancel = 0;
793 queue_delayed_work(gpio_wq, &rt5501_query.gpio_off_work, msecs_to_jiffies(0));
794 }
795
796 }
797
798 mutex_unlock(&rt5501_query.gpiolock);
799}
800
801static int update_amp_parameter(int mode)
802{
803 if (mode >= rt5501_config_data.mode_num)
804 return -EINVAL;
805
806 pr_info("%s: set mode %d\n", __func__, mode);
807
808 if (mode == RT5501_MODE_OFF)
809 memcpy(&RT5501_AMP_OFF, &rt5501_config_data.cmd_data[mode].config,
810 sizeof(struct rt5501_config));
811 else if (mode == RT5501_INIT)
812 memcpy(&RT5501_AMP_INIT, &rt5501_config_data.cmd_data[mode].config,
813 sizeof(struct rt5501_config));
814 else if (mode == RT5501_MUTE)
815 memcpy(&RT5501_AMP_MUTE, &rt5501_config_data.cmd_data[mode].config,
816 sizeof(struct rt5501_config));
817 else {
818 memcpy(&RT5501_AMP_ON, &rt5501_config_data.cmd_data[mode].config,
819 sizeof(struct rt5501_config));
820 }
821 return 0;
822}
823
824
825static long rt5501_ioctl(struct file *file, unsigned int cmd,
826 unsigned long arg)
827{
828 void __user *argp = (void __user *)arg;
829 int rc = 0, modeid = 0;
830 unsigned char tmp[7];
831 unsigned char reg_idx[1] = {0x01};
832 struct rt5501_comm_data spk_cfg;
833 unsigned char reg_value[2];
834 int premode = 0;
835 int rt5501_status = 0;
836
837 switch (cmd) {
838 case RT5501_WRITE_REG:
839 pr_info("%s: RT5501_WRITE_REG\n", __func__);
840 mutex_lock(&hp_amp_lock);
841 if (!last_spkamp_state) {
842
843 mdelay(30);
844 }
845 if (copy_from_user(reg_value, argp, sizeof(reg_value)))
846 goto err1;
847 pr_info("%s: reg_value[0]=%2x, reg_value[1]=%2x\n", __func__, \
848 reg_value[0], reg_value[1]);
849 rc = rt5501_write_reg(reg_value[0], reg_value[1]);
850
851err1:
852 mutex_unlock(&hp_amp_lock);
853 break;
854 case RT5501_SET_CONFIG:
855 if (copy_from_user(&spk_cfg, argp, sizeof(struct rt5501_comm_data)))
856 return -EFAULT;
857
858 memcpy(&RT5501_AMP_ON, &spk_cfg.config,
859 sizeof(struct rt5501_config));
860 break;
861 case RT5501_READ_CONFIG:
862 mutex_lock(&hp_amp_lock);
863 if (!last_spkamp_state) {
864
865 mdelay(30);
866 }
867
868 rc = rt5501_i2c_write_for_read(reg_idx, sizeof(reg_idx));
869 if (rc < 0)
870 goto err2;
871
872 rc = rt5501_i2c_read(tmp, sizeof(tmp));
873 if (rc < 0)
874 goto err2;
875
876 if (copy_to_user(argp, &tmp, sizeof(tmp)))
877 rc = -EFAULT;
878err2:
879 mutex_unlock(&hp_amp_lock);
880 break;
881 case RT5501_SET_MODE:
882 if (copy_from_user(&modeid, argp, sizeof(modeid)))
883 return -EFAULT;
884
885 if (modeid >= rt5501_config_data.mode_num || modeid <= 0) {
886 pr_err("unsupported rt5501 mode %d\n", modeid);
887 return -EINVAL;
888 }
889 mutex_lock(&hp_amp_lock);
890 premode = rt5501_query.curmode;
891 rt5501_query.curmode = modeid;
892 rc = update_amp_parameter(modeid);
893 rt5501_status = rt5501_query.rt5501_status;
894 mutex_unlock(&hp_amp_lock);
895 pr_info("%s:set rt5501 mode to %d curstatus %d\n", __func__,modeid,rt5501_status);
896 if(rt5501_status == RT5501_SUSPEND || (rt5501_status == RT5501_PLAYBACK && premode != rt5501_query.curmode)) {
897 flush_work_sync(&rt5501_query.volume_ramp_work.work);
898 mutex_lock(&rt5501_query.actionlock);
899 rt5501_query.action_on = 1;
900 mutex_unlock(&rt5501_query.actionlock);
901 queue_delayed_work(ramp_wq, &rt5501_query.volume_ramp_work, msecs_to_jiffies(280));
902 }
903 break;
904 case RT5501_SET_PARAM:
905 if (copy_from_user(&rt5501_config_data.mode_num, argp, sizeof(unsigned int))) {
906 pr_err("%s: copy from user failed.\n", __func__);
907 return -EFAULT;
908 }
909
910 if (rt5501_config_data.mode_num <= 0) {
911 pr_err("%s: invalid mode number %d\n",
912 __func__, rt5501_config_data.mode_num);
913 return -EINVAL;
914 }
915 if (rt5501_config_data.cmd_data == NULL)
916 rt5501_config_data.cmd_data = kzalloc(sizeof(struct rt5501_comm_data)*rt5501_config_data.mode_num, GFP_KERNEL);
917
918 if (!rt5501_config_data.cmd_data) {
919 pr_err("%s: out of memory\n", __func__);
920 return -ENOMEM;
921 }
922
923 if (copy_from_user(rt5501_config_data.cmd_data, ((struct rt5501_config_data*)argp)->cmd_data \
924 ,sizeof(struct rt5501_comm_data)*rt5501_config_data.mode_num)) {
925 pr_err("%s: copy data from user failed.\n", __func__);
926 kfree(rt5501_config_data.cmd_data);
927 rt5501_config_data.cmd_data = NULL;
928 return -EFAULT;
929 }
930
931 pr_info("%s: update rt5501 i2c commands #%d success.\n",
932 __func__, rt5501_config_data.mode_num);
933
934 mutex_lock(&hp_amp_lock);
935 update_amp_parameter(RT5501_MODE_OFF);
936 update_amp_parameter(RT5501_MUTE);
937 update_amp_parameter(RT5501_INIT);
938
939 mutex_unlock(&hp_amp_lock);
940 rc = 0;
941 break;
942 case RT5501_QUERY_OM:
943 mutex_lock(&rt5501_query.mlock);
944 rc = rt5501_query.headsetom;
945 mutex_unlock(&rt5501_query.mlock);
946 pr_info("%s: query headset om %d\n", __func__,rc);
947
948 if (copy_to_user(argp, &rc, sizeof(rc)))
949 rc = -EFAULT;
950 else
951 rc = 0;
952 break;
953
954 default:
955 pr_err("%s: Invalid command\n", __func__);
956 rc = -EINVAL;
957 break;
958 }
959 return rc;
960}
961
962static struct file_operations rt5501_fops = {
963 .owner = THIS_MODULE,
964 .open = rt5501_open,
965 .release = rt5501_release,
966 .unlocked_ioctl = rt5501_ioctl,
967};
968
969static struct miscdevice rt5501_device = {
970 .minor = MISC_DYNAMIC_MINOR,
971 .name = "rt5501",
972 .fops = &rt5501_fops,
973};
974
975int rt5501_probe(struct i2c_client *client, const struct i2c_device_id *id)
976{
977 int ret = 0;
978 int err = 0;
979 MFG_MODE = board_mfg_mode();
980 pdata = client->dev.platform_data;
981
982 if (pdata == NULL) {
983 pr_info("%s: platform data null\n", __func__);
984 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
985 if (pdata == NULL) {
986 ret = -ENOMEM;
987 pr_err("%s: platform data is NULL\n", __func__);
988 goto err_alloc_data_failed;
989 }
990 }
991
992 this_client = client;
993
994 if (ret < 0) {
995 pr_err("%s: pmic request aud_spk_en pin failed\n", __func__);
996 goto err_free_gpio_all;
997 }
998
999 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1000 pr_err("%s: i2c check functionality error\n", __func__);
1001 ret = -ENODEV;
1002 goto err_free_gpio_all;
1003 }
1004
1005 if(pdata->gpio_rt5501_spk_en) {
1006 char temp[2];
1007
1008 err = gpio_request(pdata->gpio_rt5501_spk_en, "hp_en");
1009
1010 ret = gpio_direction_output(pdata->gpio_rt5501_spk_en, 1);
1011
1012 if(ret < 0) {
1013 pr_err("%s: gpio %d on error %d\n", __func__,pdata->gpio_rt5501_spk_en,ret);
1014 }
1015
1016 mdelay(1);
1017
1018 ret = rt5501_i2c_read(temp, 2);
1019
1020 if(ret < 0) {
1021 pr_info("rt5501 is not connected\n");
1022 rt5501Connect = 0;
1023 } else {
1024 pr_info("rt5501 is connected\n");
1025 rt5501Connect = 1;
1026 }
1027
1028 rt5501_write_reg(0x0,0x4);
1029 mdelay(1);
1030
1031 rt5501_write_reg(0x0,0xc0);
1032 rt5501_write_reg(0x81,0x30);
1033
1034 rt5501_write_reg(0x90,0xd0);
1035 rt5501_write_reg(0x93,0x9d);
1036 rt5501_write_reg(0x95,0x7b);
1037 rt5501_write_reg(0xa4,0x01);
1038
1039 rt5501_write_reg(0x97,0x11);
1040 rt5501_write_reg(0x98,0x22);
1041 rt5501_write_reg(0x99,0x44);
1042 rt5501_write_reg(0x9a,0x55);
1043 rt5501_write_reg(0x9b,0x66);
1044 rt5501_write_reg(0x9c,0x99);
1045 rt5501_write_reg(0x9d,0x66);
1046 rt5501_write_reg(0x9e,0x99);
1047 rt5501_write_reg(0x1,0xc7);
1048
1049 gpio_direction_output(pdata->gpio_rt5501_spk_en, 0);
1050
1051 if(!err)
1052 gpio_free(pdata->gpio_rt5501_spk_en);
1053
1054 if(ret < 0) {
1055 pr_err("%s: gpio %d off error %d\n", __func__,pdata->gpio_rt5501_spk_en,ret);
1056 }
1057
1058 }
1059
1060 if(rt5501Connect) {
1061 struct headset_notifier notifier;
1062 ret = misc_register(&rt5501_device);
1063 if (ret) {
1064 pr_err("%s: rt5501_device register failed\n", __func__);
1065 goto err_free_gpio_all;
1066 }
1067
1068 hs_wq = create_workqueue("rt5501_hsdetect");
1069 INIT_DELAYED_WORK(&rt5501_query.hs_imp_detec_work,hs_imp_detec_func);
1070 wake_lock_init(&rt5501_query.hs_wake_lock, WAKE_LOCK_SUSPEND, DRIVER_NAME);
1071 wake_lock_init(&rt5501_query.gpio_wake_lock, WAKE_LOCK_SUSPEND, DRIVER_NAME);
1072 ramp_wq = create_workqueue("rt5501_volume_ramp");
1073 INIT_DELAYED_WORK(&rt5501_query.volume_ramp_work, volume_ramp_func);
1074 gpio_wq = create_workqueue("rt5501_gpio_off");
1075 INIT_DELAYED_WORK(&rt5501_query.gpio_off_work, hs_imp_gpio_off);
1076 notifier.id = HEADSET_REG_HS_INSERT;
1077 notifier.func = rt5501_headset_detect;
1078 headset_notifier_register(&notifier);
1079 }
1080 return 0;
1081
1082err_free_gpio_all:
1083 rt5501Connect = 0;
1084 return ret;
1085err_alloc_data_failed:
1086 rt5501Connect = 0;
1087 return ret;
1088}
1089
1090static int rt5501_remove(struct i2c_client *client)
1091{
1092 struct rt5501_platform_data *p6185data = i2c_get_clientdata(client);
1093 kfree(p6185data);
1094
1095 if(rt5501Connect) {
1096 misc_deregister(&rt5501_device);
1097 cancel_delayed_work_sync(&rt5501_query.hs_imp_detec_work);
1098 destroy_workqueue(hs_wq);
1099 }
1100 return 0;
1101}
1102
1103static void rt5501_shutdown(struct i2c_client *client)
1104{
1105 rt5501_query.gpio_off_cancel = 1;
1106 cancel_delayed_work_sync(&rt5501_query.gpio_off_work);
1107 cancel_delayed_work_sync(&rt5501_query.volume_ramp_work);
1108
1109 mutex_lock(&rt5501_query.gpiolock);
1110 mutex_lock(&hp_amp_lock);
1111 mutex_lock(&rt5501_query.mlock);
1112
1113 if((rt5501_query.gpiostatus == AMP_GPIO_OFF) && pdata->gpio_rt5501_spk_en) {
1114
1115 if(rt5501_query.s4status == AMP_S4_AUTO) {
1116 pm8921_aud_set_s4_pwm();
1117 rt5501_query.s4status = AMP_S4_PWM;
1118 msleep(1);
1119 }
1120
1121 pr_info("%s: enable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
1122 gpio_direction_output(pdata->gpio_rt5501_spk_en, 1);
1123 rt5501_query.gpiostatus = AMP_GPIO_ON;
1124 msleep(1);
1125 }
1126
1127 pr_info("%s: reset rt5501\n",__func__);
1128 rt5501_write_reg(0x0,0x4);
1129 mdelay(1);
1130
1131 high_imp = 0;
1132
1133 if((rt5501_query.gpiostatus == AMP_GPIO_ON) && pdata->gpio_rt5501_spk_en) {
1134 pr_info("%s: disable gpio %d\n",__func__,pdata->gpio_rt5501_spk_en);
1135 gpio_direction_output(pdata->gpio_rt5501_spk_en, 0);
1136 rt5501_query.gpiostatus = AMP_GPIO_OFF;
1137
1138 if(rt5501_query.s4status == AMP_S4_PWM) {
1139 pm8921_aud_set_s4_auto();
1140 rt5501_query.s4status = AMP_S4_AUTO;
1141 }
1142 }
1143
1144 mutex_unlock(&rt5501_query.mlock);
1145 mutex_unlock(&hp_amp_lock);
1146 mutex_unlock(&rt5501_query.gpiolock);
1147
1148}
1149
1150static int rt5501_suspend(struct i2c_client *client, pm_message_t mesg)
1151{
1152 return 0;
1153}
1154
1155static int rt5501_resume(struct i2c_client *client)
1156{
1157 return 0;
1158}
1159
1160static const struct i2c_device_id rt5501_id[] = {
1161 { RT5501_I2C_NAME, 0 },
1162 { }
1163};
1164
1165static struct i2c_driver rt5501_driver = {
1166 .probe = rt5501_probe,
1167 .remove = rt5501_remove,
1168 .shutdown = rt5501_shutdown,
1169 .suspend = rt5501_suspend,
1170 .resume = rt5501_resume,
1171 .id_table = rt5501_id,
1172 .driver = {
1173 .name = RT5501_I2C_NAME,
1174 },
1175};
1176
1177static int __init rt5501_init(void)
1178{
1179 pr_info("%s\n", __func__);
1180 mutex_init(&hp_amp_lock);
1181 mutex_init(&rt5501_query.mlock);
1182 mutex_init(&rt5501_query.gpiolock);
1183 mutex_init(&rt5501_query.actionlock);
1184 rt5501_query.rt5501_status = RT5501_OFF;
1185 rt5501_query.hs_qstatus = RT5501_QUERY_OFF;
1186 rt5501_query.headsetom = HEADSET_8OM;
1187 rt5501_query.curmode = RT5501_MODE_OFF;
1188 rt5501_query.gpiostatus = AMP_GPIO_OFF;
1189 rt5501_query.s4status = AMP_S4_AUTO;
1190 return i2c_add_driver(&rt5501_driver);
1191}
1192
1193static void __exit rt5501_exit(void)
1194{
1195 i2c_del_driver(&rt5501_driver);
1196}
1197
1198module_init(rt5501_init);
1199module_exit(rt5501_exit);
1200
1201MODULE_DESCRIPTION("rt5501 Speaker Amp driver");
1202MODULE_LICENSE("GPL");