Initial commit
[kernel/linux-3.0.git] / drivers / misc / max8997-muic.c
1 /*
2  * max8997-muic.c - MUIC driver for the Maxim 8997
3  *
4  *  Copyright (C) 2010 Samsung Electronics
5  *  <ms925.kim@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/i2c.h>
25 #include <linux/gpio.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/err.h>
29 #include <linux/platform_device.h>
30 #include <linux/workqueue.h>
31 #include <linux/input.h>
32 #include <linux/mfd/max8997.h>
33 #include <linux/mfd/max8997-private.h>
34 #include <plat/udc-hs.h>
35 #ifdef CONFIG_USBHUB_USB3803
36 #include <linux/usb3803.h>
37 #endif
38 #ifdef CONFIG_EXTCON
39 #include <linux/extcon.h>
40 #define DEV_NAME        "max8997-muic"
41 #endif
42
43 /* MAX8997 STATUS1 register */
44 #define STATUS1_ADC_SHIFT               0
45 #define STATUS1_ADCLOW_SHIFT            5
46 #define STATUS1_ADCERR_SHIFT            6
47 #define STATUS1_ADC_MASK                (0x1f << STATUS1_ADC_SHIFT)
48 #define STATUS1_ADCLOW_MASK             (0x1 << STATUS1_ADCLOW_SHIFT)
49 #define STATUS1_ADCERR_MASK             (0x1 << STATUS1_ADCERR_SHIFT)
50
51 /* MAX8997 STATUS2 register */
52 #define STATUS2_CHGTYP_SHIFT            0
53 #define STATUS2_CHGDETRUN_SHIFT         3
54 #define STATUS2_VBVOLT_SHIFT            6
55 #define STATUS2_CHGTYP_MASK             (0x7 << STATUS2_CHGTYP_SHIFT)
56 #define STATUS2_CHGDETRUN_MASK          (0x1 << STATUS2_CHGDETRUN_SHIFT)
57 #define STATUS2_VBVOLT_MASK             (0x1 << STATUS2_VBVOLT_SHIFT)
58
59 /* MAX8997 CDETCTRL register */
60 #define CHGDETEN_SHIFT                  0
61 #define CHGTYPM_SHIFT                   1
62 #define CHGDETEN_MASK                   (0x1 << CHGDETEN_SHIFT)
63 #define CHGTYPM_MASK                    (0x1 << CHGTYPM_SHIFT)
64
65 /* MAX8997 CONTROL1 register */
66 #define COMN1SW_SHIFT                   0
67 #define COMP2SW_SHIFT                   3
68 #define MICEN_SHIFT                     6
69 #define COMN1SW_MASK                    (0x7 << COMN1SW_SHIFT)
70 #define COMP2SW_MASK                    (0x7 << COMP2SW_SHIFT)
71 #define MICEN_MASK                      (0x1 << MICEN_SHIFT)
72
73 /* MAX8997 CONTROL2 register */
74 #define CTRL2_ACCDET_SHIFT              5
75 #define CTRL2_ACCDET_MASK               (0x1 << CTRL2_ACCDET_SHIFT)
76
77 /* MAX8997 CONTROL3 register */
78 #define CTRL3_JIGSET_SHIFT              0
79 #define CTRL3_BOOTSET_SHIFT             2
80 #define CTRL3_ADCDBSET_SHIFT            4
81 #define CTRL3_JIGSET_MASK               (0x3 << CTRL3_JIGSET_SHIFT)
82 #define CTRL3_BOOTSET_MASK              (0x3 << CTRL3_BOOTSET_SHIFT)
83 #define CTRL3_ADCDBSET_MASK             (0x3 << CTRL3_ADCDBSET_SHIFT)
84
85 /* Interrupt 1 */
86 #define INT_DETACH              (0x1 << 1)
87 #define INT_ATTACH              (0x1 << 0)
88
89 enum {
90         CTRL1_TO_OPEN           = 0x0,
91         CTRL1_TO_USB            = 0x1,
92         CTRL1_TO_AUDIO          = 0x2,
93         CTRL1_TO_UART           = 0x3,
94 };
95
96 /* MAX8997 MUIC CHG_TYP setting values */
97 enum {
98         /* No Valid voltage at VB (Vvb < Vvbdet) */
99         CHGTYP_NO_VOLTAGE       = 0x00,
100         /* Unknown (D+/D- does not present a valid USB charger signature) */
101         CHGTYP_USB              = 0x01,
102         /* Charging Downstream Port */
103         CHGTYP_DOWNSTREAM_PORT  = 0x02,
104         /* Dedicated Charger (D+/D- shorted) */
105         CHGTYP_DEDICATED_CHGR   = 0x03,
106         /* Special 500mA charger, max current 500mA */
107         CHGTYP_500MA            = 0x04,
108         /* Special 1A charger, max current 1A */
109         CHGTYP_1A               = 0x05,
110         /* Reserved for Future Use */
111         CHGTYP_RFU              = 0x06,
112         /* Dead Battery Charging, max current 100mA */
113         CHGTYP_DB_100MA         = 0x07,
114         CHGTYP_MAX,
115
116         CHGTYP_INIT,
117         CHGTYP_MIN = CHGTYP_NO_VOLTAGE
118 };
119
120 enum {
121         ADC_GND                 = 0x00,
122         ADC_MHL                 = 0x01,
123         ADC_DOCK_PREV_KEY       = 0x04,
124         ADC_DOCK_NEXT_KEY       = 0x07,
125         ADC_DOCK_VOL_DN         = 0x0a, /* 0x01010 14.46K ohm */
126         ADC_DOCK_VOL_UP         = 0x0b, /* 0x01011 17.26K ohm */
127         ADC_DOCK_PLAY_PAUSE_KEY = 0x0d,
128         ADC_CEA936ATYPE1_CHG    = 0x17, /* 0x10111 200K ohm */
129         ADC_JIG_USB_OFF         = 0x18, /* 0x11000 255K ohm */
130         ADC_JIG_USB_ON          = 0x19, /* 0x11001 301K ohm */
131         ADC_DESKDOCK            = 0x1a, /* 0x11010 365K ohm */
132         ADC_CEA936ATYPE2_CHG    = 0x1b, /* 0x11011 442K ohm */
133         ADC_JIG_UART_OFF        = 0x1c, /* 0x11100 523K ohm */
134         ADC_JIG_UART_ON         = 0x1d, /* 0x11101 619K ohm */
135         ADC_CARDOCK             = 0x1d, /* 0x11101 619K ohm */
136         ADC_OPEN                = 0x1f
137 };
138
139 enum {
140         DOCK_KEY_NONE                   = 0,
141         DOCK_KEY_VOL_UP_PRESSED,
142         DOCK_KEY_VOL_UP_RELEASED,
143         DOCK_KEY_VOL_DOWN_PRESSED,
144         DOCK_KEY_VOL_DOWN_RELEASED,
145         DOCK_KEY_PREV_PRESSED,
146         DOCK_KEY_PREV_RELEASED,
147         DOCK_KEY_PLAY_PAUSE_PRESSED,
148         DOCK_KEY_PLAY_PAUSE_RELEASED,
149         DOCK_KEY_NEXT_PRESSED,
150         DOCK_KEY_NEXT_RELEASED,
151 };
152
153 struct max8997_muic_info {
154         struct device           *dev;
155         struct max8997_dev      *max8997;
156         struct i2c_client       *muic;
157         struct max8997_muic_data *muic_data;
158         int                     irq_adc;
159         int                     irq_chgtype;
160         int                     irq_vbvolt;
161         int                     irq_adcerr;
162         int                     mansw;
163
164         enum muic_acc_type      acc_type;
165         enum muic_chg_type      chg_type;
166         struct delayed_work     init_work;
167         struct delayed_work     usb_work;
168         struct delayed_work     mhl_work;
169         struct mutex            mutex;
170 #if defined(CONFIG_MUIC_MAX8997_OVPUI)
171         int                     irq_chgins;
172         int                     irq_chgrm;
173         bool                    is_ovp_state;
174 #endif /* CONFIG_MUIC_MAX8997_OVPUI */
175
176         bool                    is_usb_ready;
177         bool                    is_mhl_ready;
178
179         struct input_dev        *input;
180         int                     previous_key;
181 #ifdef CONFIG_EXTCON
182         struct extcon_dev       *edev;
183 #endif
184 };
185
186 #if 0
187 static void max8997_muic_dump_regs(struct max8997_muic_info *info)
188 {
189         int i, ret;
190         u8 val;
191
192         for (i = 0; i < MAX8997_MUIC_REG_END; i++) {
193                 ret = max8997_read_reg(info->muic, i, &val);
194                 if (ret) {
195                         dev_err(info->dev, "%s: fail to read reg(0x%x)\n",
196                                         __func__, i);
197                         continue;
198                 }
199                 dev_info(info->dev, "%s: ADDR : 0x%02x, DATA : 0x%02x\n",
200                                 __func__, i, val);
201         }
202 }
203 #endif
204
205 static ssize_t max8997_muic_show_usb_state(struct device *dev,
206                                    struct device_attribute *attr,
207                                    char *buf)
208 {
209         struct max8997_muic_info *info = dev_get_drvdata(dev);
210
211         switch (info->acc_type) {
212         case MUIC_ACC_TYPE_USB:
213         case MUIC_ACC_TYPE_JIG_USB_OFF:
214         case MUIC_ACC_TYPE_JIG_USB_ON:
215                 if (info->chg_type == MUIC_CHG_TYPE_USB)
216                         return sprintf(buf, "USB_STATE_CONFIGURED\n");
217         default:
218                 break;
219         }
220
221         return sprintf(buf, "USB_STATE_NOTCONFIGURED\n");
222 }
223
224 static ssize_t max8997_muic_show_device(struct device *dev,
225                                    struct device_attribute *attr,
226                                    char *buf)
227 {
228         struct max8997_muic_info *info = dev_get_drvdata(dev);
229
230         switch (info->acc_type) {
231         case MUIC_ACC_TYPE_NONE:
232                 return sprintf(buf, "No cable\n");
233         case MUIC_ACC_TYPE_USB:
234                 return sprintf(buf, "USB\n");
235         case MUIC_ACC_TYPE_OTG:
236                 return sprintf(buf, "OTG\n");
237         case MUIC_ACC_TYPE_TA:
238                 return sprintf(buf, "TA\n");
239         case MUIC_ACC_TYPE_DESKDOCK:
240                 return sprintf(buf, "Desk Dock\n");
241         case MUIC_ACC_TYPE_CARDOCK:
242                 return sprintf(buf, "Car Dock\n");
243         case MUIC_ACC_TYPE_JIG_UART_OFF:
244                 if (info->chg_type == MUIC_CHG_TYPE_TA)
245                         return sprintf(buf, "JIG UART OFF VB\n");
246                 else
247                         return sprintf(buf, "JIG UART OFF\n");
248         case MUIC_ACC_TYPE_JIG_UART_ON:
249                 return sprintf(buf, "JIG UART ON\n");
250         case MUIC_ACC_TYPE_JIG_USB_OFF:
251                 return sprintf(buf, "JIG USB OFF\n");
252         case MUIC_ACC_TYPE_JIG_USB_ON:
253                 return sprintf(buf, "JIG USB ON\n");
254         case MUIC_ACC_TYPE_MHL:
255                 if (info->chg_type == MUIC_CHG_TYPE_MHL_VB)
256                         return sprintf(buf, "mHL charging\n");
257                 else
258                         return sprintf(buf, "mHL\n");
259         default:
260                 break;
261         }
262
263         return sprintf(buf, "UNKNOWN\n");
264 }
265
266 static ssize_t max8997_muic_show_manualsw(struct device *dev,
267                 struct device_attribute *attr, char *buf)
268 {
269         struct max8997_muic_info *info = dev_get_drvdata(dev);
270
271         switch (info->muic_data->usb_path) {
272         case AP_USB_MODE:
273                 return sprintf(buf, "PDA\n");
274         case CP_USB_MODE:
275                 return sprintf(buf, "MODEM\n");
276         case AUDIO_MODE:
277                 return sprintf(buf, "Audio\n");
278         default:
279                 break;
280         }
281
282         return sprintf(buf, "UNKNOWN\n");
283 }
284
285 static ssize_t max8997_muic_set_manualsw(struct device *dev,
286                                     struct device_attribute *attr,
287                                     const char *buf, size_t count)
288 {
289         struct max8997_muic_info *info = dev_get_drvdata(dev);
290
291         if (!strncasecmp(buf, "PDA", 3)) {
292                 info->muic_data->usb_path = AP_USB_MODE;
293                 dev_info(info->dev, "%s: AP_USB_MODE\n", __func__);
294         } else if (!strncasecmp(buf, "MODEM", 5)) {
295                 info->muic_data->usb_path = CP_USB_MODE;
296                 dev_info(info->dev, "%s: CP_USB_MODE\n", __func__);
297         } else
298                 dev_warn(info->dev, "%s: Wrong command\n", __func__);
299
300         return count;
301 }
302
303 static ssize_t max8997_muic_show_adc(struct device *dev,
304                                    struct device_attribute *attr,
305                                    char *buf)
306 {
307         struct max8997_muic_info *info = dev_get_drvdata(dev);
308         int ret;
309         u8 val;
310
311         ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_STATUS1, &val);
312         if (ret) {
313                 dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
314                 return sprintf(buf, "UNKNOWN\n");
315         }
316
317         return sprintf(buf, "%x\n", (val & STATUS1_ADC_MASK));
318 }
319
320 static ssize_t max8997_muic_show_audio_path(struct device *dev,
321                 struct device_attribute *attr, char *buf)
322 {
323         struct max8997_muic_info *info = dev_get_drvdata(dev);
324         int ret;
325         u8 val;
326
327         ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_CTRL1, &val);
328         if (ret) {
329                 dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
330                 return sprintf(buf, "UNKNOWN\n");
331         }
332
333         return sprintf(buf, "%x\n", val);
334 }
335
336 static ssize_t max8997_muic_set_audio_path(struct device *dev,
337                                     struct device_attribute *attr,
338                                     const char *buf, size_t count)
339 {
340         struct max8997_muic_info *info = dev_get_drvdata(dev);
341         struct i2c_client *client = info->muic;
342         u8 cntl1_val, cntl1_msk;
343         u8 val;
344
345         if (!strncmp(buf, "0", 1))
346                 val = 0;
347         else if (!strncmp(buf, "1", 1))
348                 val = 2;
349         else {
350                 dev_warn(info->dev, "%s: Wrong command\n", __func__);
351                 return count;
352         }
353
354         cntl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT) |
355                             (0 << MICEN_SHIFT);
356         cntl1_msk = COMN1SW_MASK | COMP2SW_MASK | MICEN_MASK;
357
358         max8997_update_reg(client, MAX8997_MUIC_REG_CTRL1, cntl1_val,
359                                    cntl1_msk);
360
361         cntl1_val = 0;
362         max8997_read_reg(client, MAX8997_MUIC_REG_CTRL1, &cntl1_val);
363         dev_info(info->dev, "%s: CNTL1(0x%02x)\n", __func__, cntl1_val);
364
365         return count;
366 }
367
368 static ssize_t max8997_muic_show_otg_test(struct device *dev,
369                 struct device_attribute *attr, char *buf)
370 {
371         struct max8997_muic_info *info = dev_get_drvdata(dev);
372         int ret;
373         u8 val;
374
375         ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_CDETCTRL, &val);
376         if (ret) {
377                 dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
378                 return sprintf(buf, "UNKNOWN\n");
379         }
380         val &= CHGDETEN_MASK;
381
382         return sprintf(buf, "%x\n", val);
383 }
384
385 static ssize_t max8997_muic_set_otg_test(struct device *dev,
386                                     struct device_attribute *attr,
387                                     const char *buf, size_t count)
388 {
389         struct max8997_muic_info *info = dev_get_drvdata(dev);
390         struct i2c_client *client = info->muic;
391         u8 val;
392
393         if (!strncmp(buf, "0", 1))
394                 val = 0;
395         else if (!strncmp(buf, "1", 1))
396                 val = 1;
397         else {
398                 dev_warn(info->dev, "%s: Wrong command\n", __func__);
399                 return count;
400         }
401
402         max8997_update_reg(client, MAX8997_MUIC_REG_CDETCTRL,
403                         val << CHGDETEN_SHIFT, CHGDETEN_MASK);
404
405         val = 0;
406         max8997_read_reg(client, MAX8997_MUIC_REG_CDETCTRL, &val);
407         dev_info(info->dev, "%s: CDETCTRL(0x%02x)\n", __func__, val);
408
409         return count;
410 }
411
412 static void max8997_muic_set_adcdbset(struct max8997_muic_info *info,
413                                         int value)
414 {
415         int ret;
416         u8 val;
417
418         if (value > 3) {
419                 dev_err(info->dev, "%s: invalid value(%d)\n", __func__, value);
420                 return;
421         }
422
423         if (!info->muic) {
424                 dev_err(info->dev, "%s: no muic i2c client\n", __func__);
425                 return;
426         }
427
428         val = value << CTRL3_ADCDBSET_SHIFT;
429         dev_info(info->dev, "%s: ADCDBSET(0x%02x)\n", __func__, val);
430
431         ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CTRL3, val,
432                         CTRL3_ADCDBSET_MASK);
433         if (ret < 0)
434                 dev_err(info->dev, "%s: fail to update reg\n", __func__);
435 }
436
437 static ssize_t max8997_muic_show_status(struct device *dev,
438                                         struct device_attribute *attr,
439                                         char *buf)
440 {
441         struct max8997_muic_info *info = dev_get_drvdata(dev);
442         u8 devid, int_value[3], status[3], intmask[3], ctrl[3], cdetctrl;
443
444         max8997_read_reg(info->muic, MAX8997_MUIC_REG_ID, &devid);
445         max8997_bulk_read(info->muic, MAX8997_MUIC_REG_INT1, 3, int_value);
446         max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1, 3, status);
447         max8997_bulk_read(info->muic, MAX8997_MUIC_REG_INTMASK1, 3, intmask);
448         max8997_bulk_read(info->muic, MAX8997_MUIC_REG_CTRL1, 3, ctrl);
449         max8997_read_reg(info->muic, MAX8997_MUIC_REG_CDETCTRL, &cdetctrl);
450
451         return sprintf(buf,
452                        "Device ID(0x%02x)\n"
453                            "INT1(0x%02x), INT2(0x%02x), INT3(0x%02x)\n"
454                        "STATUS1(0x%02x), STATUS2(0x%02x), STATUS3(0x%02x)\n"
455                        "INTMASK1(0x%02x), INTMASK2(0x%02x), INTMASK3(0x%02x)\n"
456                        "CTRL1(0x%02x), CTRL2(0x%02x), CTRL3(0x%02x)\n"
457                        "CDETCTRL(0x%02x)\n",
458                        devid, int_value[0], int_value[1], int_value[2],
459                        status[0], status[1], status[2],
460                        intmask[0], intmask[1], intmask[2],
461                        ctrl[0], ctrl[1], ctrl[2],
462                        cdetctrl);
463 }
464
465 static ssize_t max8997_muic_set_status(struct device *dev,
466                                         struct device_attribute *attr,
467                                         const char *buf, size_t count)
468 {
469         struct max8997_muic_info *info = dev_get_drvdata(dev);
470         u8  reg_value[3];
471         char reg_name[5];
472
473         if (sscanf(buf, "%4s:%02x,%02x,%02x" , reg_name, (u32 *)&reg_value[0],
474                 (u32 *)&reg_value[1], (u32 *)&reg_value[2])) {
475                 if (!strncmp(reg_name, "INTM", 4)) {
476                         dev_info(dev, "Manual Set INTMASK to 0x%02x, 0x%02x, 0x%02x\n",
477                                 reg_value[0], reg_value[1], reg_value[2]);
478                         max8997_bulk_write(info->muic,
479                                 MAX8997_MUIC_REG_INTMASK1, 3, reg_value);
480                 } else if (!strncmp(reg_name, "CONT", 4)) {
481                         dev_info(dev, "Manual Set CONTROL to 0x%02x, 0x%02x, 0x%02x\n",
482                                 reg_value[0], reg_value[1], reg_value[2]);
483                         max8997_bulk_write(info->muic,
484                                 MAX8997_MUIC_REG_CTRL1, 3, reg_value);
485                 } else if (!strncmp(reg_name, "CDET", 4)) {
486                         dev_info(dev, "Manual Set CDETCTRL to 0x%02x\n",
487                                 reg_value[0]);
488                         max8997_write_reg(info->muic,
489                                 MAX8997_MUIC_REG_CDETCTRL, reg_value[0]);
490                 } else {
491                         dev_info(dev, "Unsupported CMD format\n");
492                 }
493         } else {
494                 dev_info(dev, "Read CMD fail\n");
495         }
496
497         return count;
498 }
499
500 static DEVICE_ATTR(usb_state, S_IRUGO, max8997_muic_show_usb_state, NULL);
501 static DEVICE_ATTR(device, S_IRUGO, max8997_muic_show_device, NULL);
502 static DEVICE_ATTR(usb_sel, 0664,
503                 max8997_muic_show_manualsw, max8997_muic_set_manualsw);
504 static DEVICE_ATTR(adc, S_IRUGO, max8997_muic_show_adc, NULL);
505 static DEVICE_ATTR(audio_path, 0664,
506                 max8997_muic_show_audio_path, max8997_muic_set_audio_path);
507 static DEVICE_ATTR(otg_test, 0664,
508                 max8997_muic_show_otg_test, max8997_muic_set_otg_test);
509 static DEVICE_ATTR(status, 0664,
510                         max8997_muic_show_status, max8997_muic_set_status);
511
512 static struct attribute *max8997_muic_attributes[] = {
513         &dev_attr_usb_state.attr,
514         &dev_attr_device.attr,
515         &dev_attr_usb_sel.attr,
516         &dev_attr_adc.attr,
517         &dev_attr_audio_path.attr,
518         &dev_attr_otg_test.attr,
519         &dev_attr_status.attr,
520         NULL
521 };
522
523 static const struct attribute_group max8997_muic_group = {
524         .attrs = max8997_muic_attributes,
525 };
526
527 static int set_usb_sel(struct max8997_muic_info *info, int path)
528 {
529         int gpio_usb_sel = info->muic_data->gpio_usb_sel;
530         int ret = 0;
531         int val = (path == USB_SEL_CP) ? GPIO_LEVEL_HIGH : GPIO_LEVEL_LOW;
532         int temp;
533
534 #if !defined(CONFIG_TARGET_LOCALE_NA)
535         if (gpio_is_valid(gpio_usb_sel) && gpio_usb_sel) {
536                 dev_info(info->dev, "%s(%d)\n", __func__, val);
537                 gpio_direction_output(gpio_usb_sel, val);
538
539                 temp = gpio_get_value(gpio_usb_sel);
540                 if (val != temp) {
541                         dev_err(info->dev, "%s(%d)=>%d\n", __func__, val, temp);
542                         ret = -EAGAIN;
543                 }
544         }
545 #endif /* !CONFIG_TARGET_LOCALE_NA */
546
547         return ret;
548 }
549
550 static int set_uart_sel(struct max8997_muic_info *info, int path)
551 {
552         int gpio_uart_sel = info->muic_data->gpio_uart_sel;
553         int ret = 0;
554         int val = (path == UART_PATH_AP) ? GPIO_LEVEL_HIGH : GPIO_LEVEL_LOW;
555         int temp;
556
557 #if !defined(CONFIG_TARGET_LOCALE_NA)
558         if (gpio_is_valid(gpio_uart_sel) && gpio_uart_sel) {
559                 dev_info(info->dev, "%s(%d)\n", __func__, val);
560                 gpio_direction_output(gpio_uart_sel, val);
561
562                 temp = gpio_get_value(gpio_uart_sel);
563                 if (val != temp) {
564                         dev_err(info->dev, "%s(%d)=>%d\n", __func__, val, temp);
565                         ret = -EAGAIN;
566                 }
567         }
568 #endif /* !CONFIG_TARGET_LOCALE_NA */
569
570         return ret;
571 }
572
573 static int set_mic(struct max8997_muic_info *info, bool micen)
574 {
575         struct i2c_client *client = info->muic;
576         int ret = 0;
577         u8 cntl1_val = micen << MICEN_SHIFT;
578         u8 temp = 0;
579
580         /* MIC */
581         dev_info(info->dev, "%s(%s) cntl1(0x%02x)\n", __func__,
582                         (micen ? "en" : "dis"), cntl1_val);
583         ret = max8997_update_reg(client, MAX8997_MUIC_REG_CTRL1, cntl1_val,
584                         MICEN_MASK);
585         if (ret < 0) {
586                 dev_err(info->dev, "%s: update reg err\n", __func__);
587                 return ret;
588         }
589
590         ret = max8997_read_reg(client, MAX8997_MUIC_REG_CTRL1, &temp);
591         if (ret < 0) {
592                 dev_err(info->dev, "%s: read reg err\n", __func__);
593                 return ret;
594         }
595
596         if ((temp & MICEN_MASK) ^ cntl1_val) {
597                 dev_err(info->dev, "%s: err CNTL1(0x%02x)\n", __func__, temp);
598                 ret = -EAGAIN;
599         }
600
601         return ret;
602 }
603
604 static int set_accdet(struct max8997_muic_info *info, u8 accdet)
605 {
606         struct i2c_client *client = info->muic;
607         int ret = 0;
608         u8 cntl2_val = accdet << CTRL2_ACCDET_SHIFT;
609         u8 temp = 0;
610
611         /* accdet */
612         dev_info(info->dev, "%s(%s) cntl2(0x%02x)\n", __func__,
613                         (accdet ? "en" : "dis"), cntl2_val);
614         ret = max8997_update_reg(client, MAX8997_MUIC_REG_CTRL2, cntl2_val,
615                         CTRL2_ACCDET_MASK);
616         if (ret < 0) {
617                 dev_err(info->dev, "%s: update reg err\n", __func__);
618                 return ret;
619         }
620
621         ret = max8997_read_reg(client, MAX8997_MUIC_REG_CTRL2, &temp);
622         if (ret < 0) {
623                 dev_err(info->dev, "%s: read reg err\n", __func__);
624                 return ret;
625         }
626
627         if ((temp & CTRL2_ACCDET_MASK) ^ cntl2_val) {
628                 dev_err(info->dev, "%s: err CNTL2(0x%02x)\n", __func__, temp);
629                 ret = -EAGAIN;
630         }
631
632         return ret;
633 }
634
635 static int set_com_sw(struct max8997_muic_info *info, u8 cntl1_val)
636 {
637         struct i2c_client *client = info->muic;
638         int ret = 0;
639         u8 cntl1_msk = COMN1SW_MASK | COMP2SW_MASK;
640         u8 temp = 0;
641
642         /* com sw */
643         dev_info(info->dev, "%s(0x%02x)\n", __func__, cntl1_val);
644         ret = max8997_update_reg(client, MAX8997_MUIC_REG_CTRL1, cntl1_val,
645                         cntl1_msk);
646         if (ret < 0) {
647                 dev_err(info->dev, "%s: update reg err\n", __func__);
648                 return ret;
649         }
650
651         ret = max8997_read_reg(client, MAX8997_MUIC_REG_CTRL1, &temp);
652         if (ret < 0) {
653                 dev_err(info->dev, "%s: read reg err\n", __func__);
654                 return ret;
655         }
656
657         if ((temp & (COMN1SW_MASK | COMP2SW_MASK)) ^ cntl1_val) {
658                 dev_err(info->dev, "%s: err CNTL1(0x%02x)\n", __func__, temp);
659                 ret = -EAGAIN;
660         }
661
662         return ret;
663 }
664
665 static int com_to_open(struct max8997_muic_info *info)
666 {
667         int ret = 0;
668         u8 cntl1_val;
669         u8 temp = 0;
670
671         /* Disable Factory Accessory Detection State Machine for manual open*/
672         ret = set_accdet(info, 0x00);
673         if (ret) {
674                 dev_err(info->dev, "%s: set_accdet err\n", __func__);
675                 return ret;
676         }
677
678         /* com to open */
679         cntl1_val = (CTRL1_TO_OPEN << COMN1SW_SHIFT) |
680                         (CTRL1_TO_OPEN << COMP2SW_SHIFT);
681         ret = set_com_sw(info, cntl1_val);
682         while (ret) {
683                 ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_CTRL1,
684                                 &temp);
685                 if (ret < 0) {
686                         dev_err(info->dev, "%s: read reg err\n", __func__);
687                         return ret;
688                 }
689
690                 if ((temp & (COMN1SW_MASK | COMP2SW_MASK)) ^ cntl1_val) {
691                         dev_err(info->dev, "%s: err CNTL1(0x%02x)\n", __func__,
692                                         temp);
693                         ret = -EAGAIN;
694                 }
695 #if 0
696                 if (ret) {
697                         dev_err(info->dev, "%s: set_com_sw err\n", __func__);
698                         return ret;
699                 }
700 #endif
701         }
702
703         return ret;
704 }
705
706 static int com_to_usb(struct max8997_muic_info *info)
707 {
708         int ret = 0;
709         u8 accdet = 0x01;
710         u8 cntl1_val;
711
712         if (info->acc_type == MUIC_ACC_TYPE_OTG) {
713                 accdet = 0x00;
714                 /* com to usb */
715                 cntl1_val = (CTRL1_TO_USB << COMN1SW_SHIFT) |
716                                 (CTRL1_TO_USB << COMP2SW_SHIFT);
717         }
718
719         /* Enable/Disable Factory Accessory Detection State Machine */
720         ret = set_accdet(info, accdet);
721         if (ret) {
722                 dev_err(info->dev, "%s: set_accdet err\n", __func__);
723                 return ret;
724         }
725
726         if (!accdet) {
727                 ret = set_com_sw(info, cntl1_val);
728                 if (ret) {
729                         dev_err(info->dev, "%s: set_com_sw err\n", __func__);
730                         return ret;
731                 }
732         }
733
734         return ret;
735 }
736
737 static int com_to_uart(struct max8997_muic_info *info)
738 {
739         int ret = 0;
740         u8 cntl1_val;
741
742         /* Disable Factory Accessory Detection State Machine for manual open*/
743         ret = set_accdet(info, 0x00);
744         if (ret) {
745                 dev_err(info->dev, "%s: set_accdet err\n", __func__);
746                 return ret;
747         }
748
749         /* com to uart */
750         cntl1_val = (CTRL1_TO_UART << COMN1SW_SHIFT) |
751                         (CTRL1_TO_UART << COMP2SW_SHIFT);
752         ret = set_com_sw(info, cntl1_val);
753         if (ret) {
754                 dev_err(info->dev, "%s: set_com_sw err\n", __func__);
755                 return ret;
756         }
757
758         return ret;
759 }
760
761 static int com_to_audio(struct max8997_muic_info *info)
762 {
763         int ret = 0;
764         u8 cntl1_val;
765
766         /* Disable Factory Accessory Detection State Machine for manual open*/
767         ret = set_accdet(info, 0x00);
768         if (ret) {
769                 dev_err(info->dev, "%s: set_accdet err\n", __func__);
770                 return ret;
771         }
772
773         /* com to audio */
774         cntl1_val = (CTRL1_TO_AUDIO << COMN1SW_SHIFT) |
775                         (CTRL1_TO_AUDIO << COMP2SW_SHIFT);
776         ret = set_com_sw(info, cntl1_val);
777         if (ret) {
778                 dev_err(info->dev, "%s: set_com_sw err\n", __func__);
779                 return ret;
780         }
781
782         return ret;
783 }
784
785 static int switch_to_ap_usb(struct max8997_muic_info *info)
786 {
787         struct max8997_muic_data *mdata = info->muic_data;
788         int ret = 0;
789
790         if (mdata->set_safeout) {
791                 ret = mdata->set_safeout(AP_USB_MODE);
792                 if (ret) {
793                         dev_err(info->dev, "%s: safout set err\n", __func__);
794                         return ret;
795                 }
796         }
797
798         dev_info(info->dev, "%s\n", __func__);
799
800         ret = com_to_usb(info);
801         if (ret) {
802                 dev_err(info->dev, "%s: com->usb set err\n", __func__);
803                 return ret;
804         }
805
806         return ret;
807 }
808
809 static int switch_to_ap_uart(struct max8997_muic_info *info)
810 {
811         int ret = 0;
812
813         dev_info(info->dev, "%s\n", __func__);
814
815         ret = set_uart_sel(info, UART_PATH_AP);
816         if (ret) {
817                 dev_err(info->dev, "%s: uart sel set err\n", __func__);
818                 return ret;
819         }
820
821         ret = set_usb_sel(info, USB_SEL_IF);
822         if (ret) {
823                 dev_err(info->dev, "%s: usb sel set err\n", __func__);
824                 return ret;
825         }
826
827         ret = com_to_uart(info);
828         if (ret) {
829                 dev_err(info->dev, "%s: com->uart set err\n", __func__);
830                 return ret;
831         }
832
833         return ret;
834 }
835
836 static int switch_to_cp_usb(struct max8997_muic_info *info)
837 {
838         struct max8997_muic_data *mdata = info->muic_data;
839         int ret = 0;
840
841         if (mdata->set_safeout) {
842                 ret = mdata->set_safeout(CP_USB_MODE);
843                 if (ret) {
844                         dev_err(info->dev, "%s: safout set err\n", __func__);
845                         return ret;
846                 }
847         }
848
849         dev_info(info->dev, "%s\n", __func__);
850
851         ret = set_usb_sel(info, USB_SEL_CP);
852         if (ret) {
853                 dev_err(info->dev, "%s: usb sel set err\n", __func__);
854                 return ret;
855         }
856
857         ret = com_to_uart(info);
858         if (ret) {
859                 dev_err(info->dev, "%s: com->uart set err\n", __func__);
860                 return ret;
861         }
862
863         return ret;
864 }
865
866 static int switch_to_cp_uart(struct max8997_muic_info *info)
867 {
868         int ret = 0;
869
870         dev_info(info->dev, "%s\n", __func__);
871
872         ret = set_uart_sel(info, UART_PATH_CP);
873         if (ret) {
874                 dev_err(info->dev, "%s: uart sel set err\n", __func__);
875                 return ret;
876         }
877
878         ret = set_usb_sel(info, USB_SEL_IF);
879         if (ret) {
880                 dev_err(info->dev, "%s: usb sel set err\n", __func__);
881                 return ret;
882         }
883
884         ret = com_to_uart(info);
885         if (ret) {
886                 dev_err(info->dev, "%s: com->uart set err\n", __func__);
887                 return ret;
888         }
889
890         return ret;
891 }
892
893 static int switch_to_audio(struct max8997_muic_info *info)
894 {
895         int ret = 0;
896
897         dev_info(info->dev, "%s\n", __func__);
898
899         ret = set_mic(info, false);
900         if (ret) {
901                 dev_err(info->dev, "%s: mic en set err\n", __func__);
902                 return ret;
903         }
904
905         ret = com_to_audio(info);
906         if (ret) {
907                 dev_err(info->dev, "%s: com->audio set err\n", __func__);
908                 return ret;
909         }
910
911         return ret;
912 }
913
914 static enum cable_type chg_type_convertor(enum muic_chg_type chg_type)
915 {
916         enum cable_type charger_type;
917         switch (chg_type) {
918         case MUIC_CHG_TYPE_USB:
919                 charger_type = CABLE_TYPE_USB;
920                 break;
921         case MUIC_CHG_TYPE_TA:
922                 charger_type = CABLE_TYPE_TA;
923                 break;
924         case MUIC_CHG_TYPE_MHL_VB:
925                 charger_type = CABLE_TYPE_MHL_VB;
926                 break;
927         case MUIC_CHG_TYPE_NONE:
928         case MUIC_CHG_TYPE_UNKNOWN:
929         default:
930                 charger_type = CABLE_TYPE_NONE;
931                 break;
932         }
933
934         return charger_type;
935 }
936
937 static int attach_charger(struct max8997_muic_info *info,
938                 enum muic_chg_type chg_type)
939 {
940         struct max8997_muic_data *mdata = info->muic_data;
941         int ret = 0;
942
943         dev_info(info->dev, "%s: chg_type(0x%x)\n", __func__, chg_type);
944
945         if (!mdata->charger_cb) {
946                 dev_err(info->dev, "%s: charger_cb is NULL\n", __func__);
947                 return -ENXIO;
948         }
949
950         if (info->chg_type == chg_type) {
951                 dev_info(info->dev, "%s: already, ignore.\n", __func__);
952                 return ret;
953         }
954
955         ret = mdata->charger_cb(chg_type_convertor(chg_type));
956         if (ret) {
957                 info->chg_type = MUIC_CHG_TYPE_NONE;
958                 dev_err(info->dev, "%s: charger_cb(%d) err\n", __func__, ret);
959                 return ret;
960         }
961
962         info->chg_type = chg_type;
963
964         return ret;
965 }
966
967 static int attach_usb_util(struct max8997_muic_info *info)
968 {
969         int ret = 0;
970
971         ret = attach_charger(info, MUIC_CHG_TYPE_USB);
972         if (ret)
973                 return ret;
974
975         if (info->muic_data->usb_path == CP_USB_MODE) {
976                 ret = switch_to_cp_usb(info);
977                 return ret;
978         }
979
980         ret = switch_to_ap_usb(info);
981         return ret;
982 }
983
984 static int detach_charger(struct max8997_muic_info *info)
985 {
986         struct max8997_muic_data *mdata = info->muic_data;
987         int ret = 0;
988
989         dev_info(info->dev, "%s\n", __func__);
990
991         if (!mdata->charger_cb) {
992                 dev_err(info->dev, "%s: charger_cb is NULL\n", __func__);
993                 return -ENXIO;
994         }
995
996         ret = mdata->charger_cb(chg_type_convertor(MUIC_CHG_TYPE_NONE));
997         if (ret) {
998                 dev_err(info->dev, "%s: charger_cb(%d) err\n", __func__, ret);
999                 return ret;
1000         }
1001
1002         info->chg_type = MUIC_CHG_TYPE_NONE;
1003
1004         return ret;
1005 }
1006
1007 static int attach_jig_usb_boot_off(struct max8997_muic_info *info, u8 vbvolt)
1008 {
1009         int ret = 0;
1010
1011         if (!(vbvolt & STATUS2_VBVOLT_MASK))
1012                 return ret;
1013
1014         if (info->acc_type == MUIC_ACC_TYPE_JIG_USB_OFF) {
1015                 dev_info(info->dev, "%s: duplicated(JIG USB OFF)\n", __func__);
1016                 return ret;
1017         }
1018
1019         dev_info(info->dev, "%s\n", __func__);
1020         info->acc_type = MUIC_ACC_TYPE_JIG_USB_OFF;
1021
1022         ret = attach_usb_util(info);
1023         return ret;
1024 }
1025
1026 static int attach_jig_usb_boot_on(struct max8997_muic_info *info, u8 vbvolt)
1027 {
1028         int ret = 0;
1029
1030         if (!(vbvolt & STATUS2_VBVOLT_MASK))
1031                 return ret;
1032
1033         if (info->acc_type == MUIC_ACC_TYPE_JIG_USB_ON) {
1034                 dev_info(info->dev, "%s: duplicated(JIG USB ON)\n", __func__);
1035                 return ret;
1036         }
1037
1038         dev_info(info->dev, "%s\n", __func__);
1039         info->acc_type = MUIC_ACC_TYPE_JIG_USB_ON;
1040
1041         ret = attach_usb_util(info);
1042         return ret;
1043 }
1044
1045 static int attach_usb(struct max8997_muic_info *info)
1046 {
1047         struct max8997_muic_data *mdata = info->muic_data;
1048         int ret = 0;
1049
1050         if (info->acc_type == MUIC_ACC_TYPE_USB) {
1051                 dev_info(info->dev, "%s: duplicated(USB)\n", __func__);
1052                 return ret;
1053         }
1054
1055         dev_info(info->dev, "%s\n", __func__);
1056         info->acc_type = MUIC_ACC_TYPE_USB;
1057
1058         ret = attach_usb_util(info);
1059         if (ret)
1060                 return ret;
1061
1062         if (mdata->usb_cb && info->is_usb_ready)
1063                 mdata->usb_cb(USB_CABLE_ATTACHED);
1064
1065         return ret;
1066 }
1067
1068 static int detach_usb(struct max8997_muic_info *info)
1069 {
1070         struct max8997_muic_data *mdata = info->muic_data;
1071         int ret = 0;
1072
1073         dev_info(info->dev, "%s: acc_type(0x%x)\n", __func__, info->acc_type);
1074
1075         if (info->acc_type != MUIC_ACC_TYPE_MHL) {
1076                 ret = detach_charger(info);
1077                 if (ret)
1078                         return ret;
1079
1080                 ret = com_to_open(info);
1081                 if (ret)
1082                         return ret;
1083
1084                 info->acc_type = MUIC_ACC_TYPE_NONE;
1085         }
1086
1087         if (mdata->usb_path == CP_USB_MODE)
1088                 return ret;
1089
1090         if (mdata->usb_cb && info->is_usb_ready)
1091                 mdata->usb_cb(USB_CABLE_DETACHED);
1092
1093         return ret;
1094 }
1095
1096 static int attach_mhl(struct max8997_muic_info *info, u8 chgtyp)
1097 {
1098         struct max8997_muic_data *mdata = info->muic_data;
1099         int ret = 0;
1100
1101         if (mdata->is_mhl_attached && mdata->is_mhl_attached()) {
1102                 dev_info(info->dev, "%s: mhl_sel HIGH, do nothing\n", __func__);
1103                 return ret;
1104         }
1105
1106         dev_info(info->dev, "%s\n", __func__);
1107
1108         info->acc_type = MUIC_ACC_TYPE_MHL;
1109
1110         if (info->chg_type == MUIC_CHG_TYPE_USB) {
1111                 dev_info(info->dev, "%s: chg_type USB, detach usb\n", __func__);
1112                 ret = detach_usb(info);
1113         }
1114
1115 #ifdef CONFIG_EXTCON
1116         if (info->edev && info->is_mhl_ready)
1117                 extcon_set_cable_state(info->edev, "MHL", true);
1118 #else
1119         if (mdata->mhl_cb && info->is_mhl_ready)
1120                 mdata->mhl_cb(MAX8997_MUIC_ATTACHED);
1121 #endif /* CONFIG_EXTCON */
1122
1123         if (chgtyp == CHGTYP_USB) {
1124                 dev_info(info->dev, "%s: chgtyp USB, attach TA\n", __func__);
1125                 ret = attach_charger(info, MUIC_CHG_TYPE_MHL_VB);
1126         }
1127
1128         return ret;
1129 }
1130
1131 static int detach_mhl(struct max8997_muic_info *info)
1132 {
1133         struct max8997_muic_data *mdata = info->muic_data;
1134         int ret = 0;
1135
1136         dev_info(info->dev, "%s%s\n", __func__,
1137                         info->chg_type == MUIC_CHG_TYPE_MHL_VB ? " VBUS" : "");
1138
1139         if (info->chg_type != MUIC_CHG_TYPE_MHL_VB) {
1140                 if (mdata->is_mhl_attached && mdata->is_mhl_attached())
1141                         dev_info(info->dev, "%s: MHL attached, Do Nothing\n",
1142                                         __func__);
1143                 else
1144                         info->acc_type = MUIC_ACC_TYPE_NONE;
1145
1146                 return ret;
1147         }
1148
1149         ret = detach_charger(info);
1150         if (ret)
1151                 dev_err(info->dev, "%s: detach_charger err\n", __func__);
1152
1153         if (mdata->is_mhl_attached && mdata->is_mhl_attached() &&
1154                         mdata->mhl_cb && info->is_mhl_ready)
1155                 mdata->mhl_cb(MAX8997_MUIC_DETACHED);
1156
1157         info->acc_type = MUIC_ACC_TYPE_NONE;
1158
1159         return ret;
1160 }
1161
1162 static int attach_otg(struct max8997_muic_info *info, u8 chgtyp)
1163 {
1164         struct max8997_muic_data *mdata = info->muic_data;
1165         int ret = 0;
1166
1167         switch (chgtyp) {
1168         case CHGTYP_NO_VOLTAGE:
1169                 if (info->acc_type == MUIC_ACC_TYPE_OTG) {
1170                         dev_info(info->dev, "%s: duplicated(OTG)\n", __func__);
1171                         return ret;
1172                 }
1173
1174                 info->acc_type = MUIC_ACC_TYPE_OTG;
1175                 ret = switch_to_ap_usb(info);
1176                 if (mdata->usb_cb && info->is_usb_ready)
1177                         mdata->usb_cb(USB_OTGHOST_ATTACHED);
1178                 break;
1179         case CHGTYP_USB:
1180         case CHGTYP_DOWNSTREAM_PORT:
1181         case CHGTYP_DEDICATED_CHGR:
1182         case CHGTYP_500MA:
1183         case CHGTYP_1A:
1184                 dev_info(info->dev, "%s: OTG charging pump\n", __func__);
1185                 ret = attach_charger(info, MUIC_CHG_TYPE_NONE);
1186                 break;
1187         default:
1188                 break;
1189         }
1190
1191         return ret;
1192 }
1193
1194 static int detach_otg(struct max8997_muic_info *info)
1195 {
1196         struct max8997_muic_data *mdata = info->muic_data;
1197         int ret = 0;
1198
1199         dev_info(info->dev, "%s\n", __func__);
1200         info->acc_type = MUIC_ACC_TYPE_NONE;
1201
1202         if (mdata->usb_cb && info->is_usb_ready)
1203                 mdata->usb_cb(USB_OTGHOST_DETACHED);
1204
1205         return ret;
1206 }
1207
1208 /* TODO : should be removed */
1209 #define NOTIFY_TEST_MODE        3
1210
1211 static int attach_jig_uart_boot_off(struct max8997_muic_info *info, u8 vbvolt)
1212 {
1213         struct max8997_muic_data *mdata = info->muic_data;
1214         bool is_otgtest = false;
1215         int ret = 0;
1216
1217         dev_info(info->dev, "%s: JIG UART/BOOTOFF(0x%x)\n", __func__, vbvolt);
1218
1219 #if defined(CONFIG_SEC_MODEM_M0_TD)
1220         gpio_set_value(GPIO_AP_CP_INT1, 1);
1221 #endif
1222
1223         if (mdata->uart_path == UART_PATH_AP)
1224                 ret = switch_to_ap_uart(info);
1225         else
1226                 ret = switch_to_cp_uart(info);
1227
1228         info->acc_type = MUIC_ACC_TYPE_JIG_UART_OFF;
1229
1230         if (vbvolt & STATUS2_VBVOLT_MASK) {
1231                 if (mdata->host_notify_cb) {
1232                         if (mdata->host_notify_cb(1) == NOTIFY_TEST_MODE) {
1233                                 is_otgtest = true;
1234                                 dev_info(info->dev, "%s: OTG TEST\n", __func__);
1235                         }
1236                 }
1237
1238                 if (is_otgtest)
1239                         ret = detach_charger(info);
1240                 else
1241                         ret = attach_charger(info, MUIC_CHG_TYPE_TA);
1242         } else {
1243                 if (info->chg_type == MUIC_CHG_TYPE_TA) {
1244                         ret = detach_charger(info);
1245
1246                         if (mdata->host_notify_cb)
1247                                 mdata->host_notify_cb(0);
1248                 }
1249         }
1250
1251         return ret;
1252 }
1253
1254 static int detach_jig_uart_boot_off(struct max8997_muic_info *info)
1255 {
1256         int ret = 0;
1257
1258         dev_info(info->dev, "%s\n", __func__);
1259         info->acc_type = MUIC_ACC_TYPE_NONE;
1260
1261         if (info->chg_type == MUIC_CHG_TYPE_TA)
1262                 ret = detach_charger(info);
1263
1264         return ret;
1265 }
1266
1267 static int detach_jig_uart_boot_on(struct max8997_muic_info *info)
1268 {
1269         int ret = 0;
1270
1271         dev_info(info->dev, "%s\n", __func__);
1272         info->acc_type = MUIC_ACC_TYPE_NONE;
1273
1274         if (info->chg_type == MUIC_CHG_TYPE_TA)
1275                 ret = detach_charger(info);
1276
1277         return ret;
1278 }
1279
1280 static int attach_desk_dock(struct max8997_muic_info *info, u8 status2)
1281 {
1282         struct max8997_muic_data *mdata = info->muic_data;
1283         u8 chgtyp, chgdetrun;
1284         int ret = 0;
1285
1286         chgtyp = status2 & STATUS2_CHGTYP_MASK;
1287         chgdetrun = status2 & STATUS2_CHGDETRUN_MASK;
1288
1289         if (info->acc_type == MUIC_ACC_TYPE_DESKDOCK)
1290                 dev_info(info->dev, "%s: duplicated(DeskDock)\n", __func__);
1291         else {
1292                 dev_info(info->dev, "%s\n", __func__);
1293                 info->acc_type = MUIC_ACC_TYPE_DESKDOCK;
1294
1295                 ret = switch_to_audio(info);
1296
1297                 if (mdata->deskdock_cb)
1298                         mdata->deskdock_cb(MAX8997_MUIC_ATTACHED);
1299         }
1300
1301         switch (chgtyp) {
1302         case CHGTYP_USB:
1303         case CHGTYP_DOWNSTREAM_PORT:
1304         case CHGTYP_DEDICATED_CHGR:
1305         case CHGTYP_500MA:
1306         case CHGTYP_1A:
1307                 ret = attach_charger(info, MUIC_CHG_TYPE_TA);
1308                 break;
1309         case CHGTYP_NO_VOLTAGE:
1310                 if ((info->chg_type == MUIC_CHG_TYPE_TA) && (!chgdetrun))
1311                         ret = detach_charger(info);
1312                 break;
1313         default:
1314                 break;
1315         }
1316
1317         return ret;
1318 }
1319
1320 static int detach_desk_dock(struct max8997_muic_info *info)
1321 {
1322         struct max8997_muic_data *mdata = info->muic_data;
1323         int ret = 0;
1324
1325         dev_info(info->dev, "%s\n", __func__);
1326
1327         if (info->chg_type == MUIC_CHG_TYPE_TA) {
1328                 ret = detach_charger(info);
1329                 if (ret)
1330                         return ret;
1331         }
1332
1333         if (mdata->deskdock_cb)
1334                 mdata->deskdock_cb(MAX8997_MUIC_DETACHED);
1335
1336         info->acc_type = MUIC_ACC_TYPE_NONE;
1337
1338         return ret;
1339 }
1340
1341 static int attach_car_dock(struct max8997_muic_info *info, u8 status2)
1342 {
1343         struct max8997_muic_data *mdata = info->muic_data;
1344         u8 chgtyp, chgdetrun;
1345         int ret = 0;
1346
1347         chgtyp = status2 & STATUS2_CHGTYP_MASK;
1348         chgdetrun = status2 & STATUS2_CHGDETRUN_MASK;
1349
1350         if (info->acc_type == MUIC_ACC_TYPE_CARDOCK)
1351                 dev_info(info->dev, "%s: duplicated(CarDock)\n", __func__);
1352         else {
1353                 dev_info(info->dev, "%s\n", __func__);
1354                 info->acc_type = MUIC_ACC_TYPE_CARDOCK;
1355
1356                 ret = switch_to_audio(info);
1357
1358                 if (mdata->cardock_cb)
1359                         mdata->cardock_cb(MAX8997_MUIC_ATTACHED);
1360         }
1361
1362         switch (chgtyp) {
1363         case CHGTYP_USB:
1364         case CHGTYP_DOWNSTREAM_PORT:
1365         case CHGTYP_DEDICATED_CHGR:
1366         case CHGTYP_500MA:
1367         case CHGTYP_1A:
1368                 ret = attach_charger(info, MUIC_CHG_TYPE_TA);
1369                 break;
1370         case CHGTYP_NO_VOLTAGE:
1371                 if ((info->chg_type == MUIC_CHG_TYPE_TA) && (!chgdetrun))
1372                         ret = detach_charger(info);
1373                 break;
1374         default:
1375                 break;
1376         }
1377
1378         return ret;
1379 }
1380
1381 static int detach_car_dock(struct max8997_muic_info *info)
1382 {
1383         struct max8997_muic_data *mdata = info->muic_data;
1384         int ret = 0;
1385
1386         dev_info(info->dev, "%s\n", __func__);
1387
1388         if (info->chg_type == MUIC_CHG_TYPE_TA) {
1389                 ret = detach_charger(info);
1390                 if (ret)
1391                         return ret;
1392         }
1393
1394         if (mdata->cardock_cb)
1395                 mdata->cardock_cb(MAX8997_MUIC_DETACHED);
1396
1397         info->acc_type = MUIC_ACC_TYPE_NONE;
1398
1399         return ret;
1400 }
1401
1402 static int handle_dock_vol_key(struct max8997_muic_info *info,
1403                                         u8 status1)
1404 {
1405         struct input_dev *input = info->input;
1406         int pre_key = info->previous_key;
1407         unsigned int code;
1408         int state;
1409         u8 adc;
1410
1411         adc = status1 & STATUS1_ADC_MASK;
1412
1413         if (info->acc_type != MUIC_ACC_TYPE_DESKDOCK)
1414                 return 0;
1415
1416         if (adc == ADC_OPEN) {
1417                 switch (pre_key) {
1418                 case DOCK_KEY_VOL_UP_PRESSED:
1419                         code = KEY_VOLUMEUP;
1420                         state = 0;
1421                         info->previous_key = DOCK_KEY_VOL_UP_RELEASED;
1422                         break;
1423                 case DOCK_KEY_VOL_DOWN_PRESSED:
1424                         code = KEY_VOLUMEDOWN;
1425                         state = 0;
1426                         info->previous_key = DOCK_KEY_VOL_DOWN_RELEASED;
1427                         break;
1428                 case DOCK_KEY_PREV_PRESSED:
1429                         code = KEY_PREVIOUSSONG;
1430                         state = 0;
1431                         info->previous_key = DOCK_KEY_PREV_RELEASED;
1432                         break;
1433                 case DOCK_KEY_PLAY_PAUSE_PRESSED:
1434                         code = KEY_PLAYPAUSE;
1435                         state = 0;
1436                         info->previous_key = DOCK_KEY_PLAY_PAUSE_RELEASED;
1437                         break;
1438                 case DOCK_KEY_NEXT_PRESSED:
1439                         code = KEY_NEXTSONG;
1440                         state = 0;
1441                         info->previous_key = DOCK_KEY_NEXT_RELEASED;
1442                         break;
1443                 default:
1444                         return 0;
1445                 }
1446                 input_event(input, EV_KEY, code, state);
1447                 input_sync(input);
1448                 return 0;
1449         }
1450
1451         if (pre_key == DOCK_KEY_NONE) {
1452                 /*
1453                 if (adc != ADC_DOCK_VOL_UP && adc != ADC_DOCK_VOL_DN && \
1454                 adc != ADC_DOCK_PREV_KEY && adc != ADC_DOCK_PLAY_PAUSE_KEY \
1455                 && adc != ADC_DOCK_NEXT_KEY)
1456                 */
1457                 if ((adc < 0x03) || (adc > 0x0d))
1458                         return 0;
1459         }
1460
1461         dev_info(info->dev, "%s: dock vol key(%d)\n", __func__, pre_key);
1462
1463         switch (adc) {
1464         case ADC_DOCK_VOL_UP:
1465                 code = KEY_VOLUMEUP;
1466                 state = 1;
1467                 info->previous_key = DOCK_KEY_VOL_UP_PRESSED;
1468                 break;
1469         case ADC_DOCK_VOL_DN:
1470                 code = KEY_VOLUMEDOWN;
1471                 state = 1;
1472                 info->previous_key = DOCK_KEY_VOL_DOWN_PRESSED;
1473                 break;
1474         case ADC_DOCK_PREV_KEY-1 ... ADC_DOCK_PREV_KEY+1:
1475                 code = KEY_PREVIOUSSONG;
1476                 state = 1;
1477                 info->previous_key = DOCK_KEY_PREV_PRESSED;
1478                 break;
1479         case ADC_DOCK_PLAY_PAUSE_KEY-1 ... ADC_DOCK_PLAY_PAUSE_KEY+1:
1480                 code = KEY_PLAYPAUSE;
1481                 state = 1;
1482                 info->previous_key = DOCK_KEY_PLAY_PAUSE_PRESSED;
1483                 break;
1484         case ADC_DOCK_NEXT_KEY-1 ... ADC_DOCK_NEXT_KEY+1:
1485                 code = KEY_NEXTSONG;
1486                 state = 1;
1487                 info->previous_key = DOCK_KEY_NEXT_PRESSED;
1488                 break;
1489         case ADC_DESKDOCK: /* key release routine */
1490                 if (pre_key == DOCK_KEY_VOL_UP_PRESSED) {
1491                         code = KEY_VOLUMEUP;
1492                         state = 0;
1493                         info->previous_key = DOCK_KEY_VOL_UP_RELEASED;
1494                 } else if (pre_key == DOCK_KEY_VOL_DOWN_PRESSED) {
1495                         code = KEY_VOLUMEDOWN;
1496                         state = 0;
1497                         info->previous_key = DOCK_KEY_VOL_DOWN_RELEASED;
1498                 } else if (pre_key == DOCK_KEY_PREV_PRESSED) {
1499                         code = KEY_PREVIOUSSONG;
1500                         state = 0;
1501                         info->previous_key = DOCK_KEY_PREV_RELEASED;
1502                 } else if (pre_key == DOCK_KEY_PLAY_PAUSE_PRESSED) {
1503                         code = KEY_PLAYPAUSE;
1504                         state = 0;
1505                         info->previous_key = DOCK_KEY_PLAY_PAUSE_RELEASED;
1506                 } else if (pre_key == DOCK_KEY_NEXT_PRESSED) {
1507                         code = KEY_NEXTSONG;
1508                         state = 0;
1509                         info->previous_key = DOCK_KEY_NEXT_RELEASED;
1510                 } else {
1511                         dev_warn(info->dev, "%s:%d should not reach here\n",
1512                                         __func__, __LINE__);
1513                         return 0;
1514                 }
1515                 break;
1516         default:
1517                 dev_warn(info->dev, "%s: unsupported ADC(0x%02x)\n", __func__,
1518                                 adc);
1519                 return 0;
1520         }
1521
1522         input_event(input, EV_KEY, code, state);
1523         input_sync(input);
1524
1525         return 1;
1526 }
1527
1528 static int handle_attach(struct max8997_muic_info *info,
1529                 u8 status1, u8 status2)
1530 {
1531         struct max8997_muic_data *mdata = info->muic_data;
1532         u8 adc, adclow, adcerr, chgtyp, vbvolt, chgdetrun;
1533         int ret = 0;
1534 #ifdef CONFIG_USBHUB_USB3803
1535         /* setting usb hub in Diagnostic(hub) mode */
1536         usb3803_set_mode(USB_3803_MODE_HUB);
1537 #endif /* CONFIG_USBHUB_USB3803 */
1538
1539         adc = status1 & STATUS1_ADC_MASK;
1540         adclow = status1 & STATUS1_ADCLOW_MASK;
1541         adcerr = status1 & STATUS1_ADCERR_MASK;
1542         chgtyp = status2 & STATUS2_CHGTYP_MASK;
1543         vbvolt = status2 & STATUS2_VBVOLT_MASK;
1544         chgdetrun = status2 & STATUS2_CHGDETRUN_MASK;
1545
1546         switch (info->acc_type) {
1547         case MUIC_ACC_TYPE_JIG_UART_OFF:
1548                 /* Workaround for Factory mode.
1549                  * Abandon adc interrupt of approximately +-100K range
1550                  * if previous cable status was JIG UART BOOT OFF.
1551                  */
1552                 if (adc == (ADC_JIG_UART_OFF + 1) ||
1553                                 adc == (ADC_JIG_UART_OFF - 1)) {
1554                         dev_warn(info->dev, "%s: abandon ADC\n", __func__);
1555                         return ret;
1556                 }
1557
1558                 if (adcerr) {
1559                         dev_warn(info->dev, "%s: current jig_uart_off, ignore\n",
1560                                         __func__);
1561                         return ret;
1562                 }
1563
1564                 if (adc != ADC_JIG_UART_OFF) {
1565                         dev_info(info->dev, "%s: adc != JIG_UART_OFF,"
1566                                         " remove JIG UART/BOOTOFF\n", __func__);
1567                         ret = detach_jig_uart_boot_off(info);
1568                 }
1569                 break;
1570
1571         case MUIC_ACC_TYPE_DESKDOCK:
1572                 if (adcerr || (adc != ADC_DESKDOCK)) {
1573                         if (adcerr)
1574                                 dev_err(info->dev, "%s: ADC err occured(DESKDOCK)\n",
1575                                                 __func__);
1576                         else
1577                                 dev_warn(info->dev, "%s: ADC != DESKDOCK, remove DESKDOCK\n",
1578                                                 __func__);
1579
1580                         ret = detach_desk_dock(info);
1581
1582                         if (adcerr)
1583                                 return ret;
1584                 }
1585                 break;
1586
1587         case MUIC_ACC_TYPE_CARDOCK:
1588                 if (adcerr || (adc != ADC_CARDOCK)) {
1589                         if (adcerr)
1590                                 dev_err(info->dev, "%s: ADC err occured(CARDOCK)\n",
1591                                                 __func__);
1592                         else
1593                                 dev_warn(info->dev, "%s: ADC != CARDOCK, remove CARDOCK\n",
1594                                                 __func__);
1595
1596                         ret = detach_car_dock(info);
1597
1598                         if (adcerr)
1599                                 return ret;
1600                 }
1601                 break;
1602
1603         default:
1604                 break;
1605         }
1606
1607         /* 1Kohm ID regiter detection (mHL)
1608          * Old MUIC : ADC value:0x00 or 0x01, ADCLow:1
1609          * New MUIC : ADC value is not set(Open), ADCLow:1, ADCError:1
1610          */
1611         if (adclow && adcerr) {
1612                 ret = attach_mhl(info, chgtyp);
1613                 return ret;
1614         }
1615
1616         switch (adc) {
1617         case ADC_GND:
1618 #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
1619                 /* This is for support old MUIC */
1620                 if (adclow) {
1621                         ret = attach_mhl(info, chgtyp);
1622                         break;
1623                 }
1624 #endif /* CONFIG_MACH_U1 || CONFIG_MACH_TRATS */
1625                 ret = attach_otg(info, chgtyp);
1626                 break;
1627         case ADC_MHL:
1628 #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
1629                 /* This is for support old MUIC */
1630                 ret = attach_mhl(info, chgtyp);
1631 #endif /* CONFIG_MACH_U1 || CONFIG_MACH_TRATS */
1632                 break;
1633         case ADC_JIG_UART_OFF:
1634                 ret = attach_jig_uart_boot_off(info, vbvolt);
1635                 break;
1636         case ADC_JIG_USB_OFF:
1637                 ret = attach_jig_usb_boot_off(info, vbvolt);
1638                 break;
1639         case ADC_JIG_USB_ON:
1640                 ret = attach_jig_usb_boot_on(info, vbvolt);
1641                 break;
1642         case ADC_DESKDOCK:
1643                 ret = attach_desk_dock(info, status2);
1644                 break;
1645         case ADC_CARDOCK:
1646                 ret = attach_car_dock(info, status2);
1647                 break;
1648         case ADC_CEA936ATYPE1_CHG:
1649         case ADC_CEA936ATYPE2_CHG:
1650         case ADC_OPEN:
1651                 switch (chgtyp) {
1652                 case CHGTYP_USB:
1653                         if (adc == ADC_CEA936ATYPE1_CHG ||
1654                                         adc == ADC_CEA936ATYPE2_CHG)
1655                                 break;
1656
1657                         if (mdata->is_mhl_attached &&
1658                                         mdata->is_mhl_attached() &&
1659                                         info->acc_type == MUIC_ACC_TYPE_MHL) {
1660                                 if (info->chg_type == MUIC_CHG_TYPE_MHL_VB) {
1661                                         dev_info(info->dev,
1662                                                 "%s: MHL charging already, do nothing\n",
1663                                                 __func__);
1664                                         return ret;
1665                                 }
1666
1667                                 dev_info(info->dev, "%s: MHL(charging)\n",
1668                                                 __func__);
1669                                 ret = attach_charger(info,
1670                                                 MUIC_CHG_TYPE_MHL_VB);
1671                                 return ret;
1672                         }
1673                         ret = attach_usb(info);
1674                         break;
1675                 case CHGTYP_DOWNSTREAM_PORT:
1676                 case CHGTYP_DEDICATED_CHGR:
1677                 case CHGTYP_500MA:
1678                 case CHGTYP_1A:
1679                         if (info->acc_type == MUIC_ACC_TYPE_TA &&
1680                                         info->chg_type == MUIC_CHG_TYPE_TA) {
1681                                 dev_info(info->dev, "%s: duplicated(TA)\n",
1682                                                 __func__);
1683                                 return ret;
1684                         }
1685                         dev_info(info->dev, "%s:TA\n", __func__);
1686 #ifdef CONFIG_USBHUB_USB3803
1687                         /* setting usb hub in default mode (standby) */
1688                         usb3803_set_mode(USB_3803_MODE_STANDBY);
1689 #endif /* CONFIG_USBHUB_USB3803 */
1690                         ret = attach_charger(info, MUIC_CHG_TYPE_TA);
1691                         if (ret == 0)
1692                                 info->acc_type = MUIC_ACC_TYPE_TA;
1693                         break;
1694                 default:
1695                         break;
1696                 }
1697                 break;
1698         default:
1699                 dev_warn(info->dev, "%s: unsupported adc=0x%x\n",
1700                                 __func__, adc);
1701                 break;
1702         }
1703         return ret;
1704 }
1705
1706 static int handle_detach(struct max8997_muic_info *info)
1707 {
1708         int ret = 0;
1709
1710 #if defined(CONFIG_SEC_MODEM_M0_TD)
1711         gpio_set_value(GPIO_AP_CP_INT1, 0);
1712 #endif
1713
1714         /*
1715          * MAX8996/8997-MUIC bug:
1716          *
1717          * Auto-switching COMN/P is not restored automatically when detached and
1718          * remains undetermined state. UART(UT1, UR2) will be short (because TA
1719          * D+/D- is short) if charger(TA) insertion is followed right after the
1720          * JIG off. Reset CONTROL1 is needed when detaching cable.
1721          */
1722         ret = max8997_write_reg(info->muic, MAX8997_MUIC_REG_CTRL1, 0x00);
1723         if (ret)
1724                 dev_err(info->dev, "%s: reset CONTROL1 err\n", __func__);
1725
1726         if (info->acc_type == MUIC_ACC_TYPE_MHL) {
1727                 /* Enable Factory Accessory Detection State Machine */
1728                 set_accdet(info, 0x01);
1729         }
1730
1731 #ifdef CONFIG_USBHUB_USB3803
1732         /* setting usb hub in default mode (standby) */
1733         usb3803_set_mode(USB_3803_MODE_STANDBY);
1734 #endif  /* CONFIG_USBHUB_USB3803 */
1735         info->previous_key = DOCK_KEY_NONE;
1736
1737         switch (info->acc_type) {
1738         case MUIC_ACC_TYPE_OTG:
1739                 ret = detach_otg(info);
1740                 break;
1741         case MUIC_ACC_TYPE_USB:
1742         case MUIC_ACC_TYPE_JIG_USB_OFF:
1743         case MUIC_ACC_TYPE_JIG_USB_ON:
1744                 ret = detach_usb(info);
1745                 break;
1746         case MUIC_ACC_TYPE_DESKDOCK:
1747                 ret = detach_desk_dock(info);
1748                 break;
1749         case MUIC_ACC_TYPE_CARDOCK:
1750                 ret = detach_car_dock(info);
1751                 break;
1752         case MUIC_ACC_TYPE_TA:
1753                 ret = detach_charger(info);
1754                 info->acc_type = MUIC_ACC_TYPE_NONE;
1755                 break;
1756         case MUIC_ACC_TYPE_JIG_UART_ON:
1757                 ret = detach_jig_uart_boot_on(info);
1758                 break;
1759         case MUIC_ACC_TYPE_JIG_UART_OFF:
1760                 ret = detach_jig_uart_boot_off(info);
1761                 break;
1762         case MUIC_ACC_TYPE_MHL:
1763                 ret = detach_mhl(info);
1764 #ifdef CONFIG_EXTCON
1765                 if (info->edev && info->is_mhl_ready)
1766                         extcon_set_cable_state(info->edev, "MHL", false);
1767 #endif
1768                 break;
1769         case MUIC_ACC_TYPE_NONE:
1770                 dev_info(info->dev, "%s: duplicated(NONE)\n", __func__);
1771                 break;
1772         case MUIC_ACC_TYPE_UNKNOWN:
1773                 dev_info(info->dev, "%s: UNKNOWN\n", __func__);
1774                 ret = detach_charger(info);
1775                 info->acc_type = MUIC_ACC_TYPE_NONE;
1776                 break;
1777         default:
1778                 dev_info(info->dev, "%s:invalid cable type %d\n",
1779                                 __func__, info->acc_type);
1780                 break;
1781         }
1782
1783         return ret;
1784 }
1785
1786 static int detect_dev(struct max8997_muic_info *info, int irq)
1787 {
1788         struct i2c_client *client = info->muic;
1789         u8 status[2];
1790         u8 adc, chgtyp, adcerr, chgdetrun;
1791         int intr = INT_ATTACH;
1792         int ret = 0;
1793
1794         ret = max8997_bulk_read(client, MAX8997_MUIC_REG_STATUS1, 2, status);
1795         if (ret) {
1796                 dev_err(info->dev, "%s: read muic reg(%d) err\n", __func__,
1797                                 ret);
1798                 return ret;
1799         }
1800
1801         dev_info(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
1802                         status[0], status[1]);
1803
1804         if (irq == info->irq_adc) {
1805                 ret = handle_dock_vol_key(info, status[0]);
1806                 if (ret)
1807                         return ret;
1808         }
1809
1810         adc = status[0] & STATUS1_ADC_MASK;
1811         adcerr = status[0] & STATUS1_ADCERR_MASK;
1812         chgtyp = status[1] & STATUS2_CHGTYP_MASK;
1813         chgdetrun = status[1] & STATUS2_CHGDETRUN_MASK;
1814
1815         if (chgdetrun) {
1816                 dev_warn(info->dev, "%s: charger det run, ignore\n", __func__);
1817                 return ret;
1818         }
1819
1820         switch (adc) {
1821         case ADC_MHL:
1822 #if defined(CONFIG_MACH_U1) || defined(CONFIG_MACH_TRATS)
1823                 break;
1824 #endif /* CONFIG_MACH_U1 || CONFIG_MACH_TRATS */
1825         case (ADC_MHL + 1):
1826         case (ADC_DOCK_VOL_DN - 1):
1827         case (ADC_DOCK_PLAY_PAUSE_KEY + 2) ... (ADC_CEA936ATYPE1_CHG - 1):
1828         case (ADC_CARDOCK + 1):
1829                 dev_warn(info->dev, "%s: unsupported ADC(0x%02x)\n",
1830                                 __func__, adc);
1831                 intr = INT_DETACH;
1832                 break;
1833         case ADC_OPEN:
1834                 if (!adcerr) {
1835                         if (chgtyp == CHGTYP_NO_VOLTAGE)
1836                                 intr = INT_DETACH;
1837                         else if (chgtyp == CHGTYP_USB ||
1838                                         chgtyp == CHGTYP_DOWNSTREAM_PORT ||
1839                                         chgtyp == CHGTYP_DEDICATED_CHGR ||
1840                                         chgtyp == CHGTYP_500MA  ||
1841                                         chgtyp == CHGTYP_1A) {
1842                                 switch (info->acc_type) {
1843                                 case MUIC_ACC_TYPE_OTG:
1844                                 case MUIC_ACC_TYPE_STATION:
1845                                 case MUIC_ACC_TYPE_JIG_USB_OFF:
1846                                 case MUIC_ACC_TYPE_JIG_USB_ON:
1847                                 case MUIC_ACC_TYPE_DESKDOCK:
1848                                 case MUIC_ACC_TYPE_JIG_UART_OFF:
1849                                 case MUIC_ACC_TYPE_JIG_UART_ON:
1850                                 case MUIC_ACC_TYPE_CARDOCK:
1851                                         dev_info(info->dev, "%s: chgtyp accur but detach\n",
1852                                                         __func__);
1853                                         intr = INT_DETACH;
1854                                         break;
1855                                 default:
1856                                         break;
1857                                 }
1858                         }
1859                 }
1860                 break;
1861         default:
1862                 break;
1863         }
1864
1865 #if defined(CONFIG_MUIC_MAX8997_OVPUI)
1866         if (intr == INT_ATTACH) {
1867                 if (irq == info->irq_chgins) {
1868                         if (info->is_ovp_state) {
1869                                 ret = handle_attach(info, status[0],
1870                                         status[1]);
1871                                 info->is_ovp_state = false;
1872                                 dev_info(info->dev, "OVP recovered\n");
1873                                 return ret;
1874                         } else {
1875                                 dev_info(info->dev, "Just inserted TA/USB\n");
1876                                 return ret;
1877                         }
1878                 } else if (irq == info->irq_chgrm) {
1879                         ret = handle_detach(info);
1880                         info->is_ovp_state = true;
1881                         dev_info(info->dev, "OVP occured\n");
1882                         return ret;
1883                 }
1884
1885         } else  {
1886                 info->is_ovp_state = false;
1887
1888                 if (irq == info->irq_chgrm) {
1889                         dev_info(info->dev, "Just removed TA/USB\n");
1890                         return ret;
1891                 }
1892         }
1893 #endif /* CONFIG_MUIC_MAX8997_OVPUI */
1894
1895         if (intr == INT_ATTACH) {
1896                 dev_info(info->dev, "%s: ATTACHED\n", __func__);
1897                 ret = handle_attach(info, status[0], status[1]);
1898         } else {
1899                 dev_info(info->dev, "%s: DETACHED\n", __func__);
1900                 ret = handle_detach(info);
1901         }
1902         return ret;
1903 }
1904
1905 static irqreturn_t max8997_muic_irq(int irq, void *data)
1906 {
1907         struct max8997_muic_info *info = data;
1908         int ret = 0;
1909
1910         dev_info(info->dev, "%s: irq:%d\n", __func__, irq);
1911
1912         mutex_lock(&info->mutex);
1913         ret = detect_dev(info, irq);
1914         if (ret)
1915                 dev_err(info->dev, "%s: error returned.\n", __func__);
1916         mutex_unlock(&info->mutex);
1917
1918         return IRQ_HANDLED;
1919 }
1920
1921 #define REQUEST_IRQ(_irq, _name)                                        \
1922 do {                                                                    \
1923         ret = request_threaded_irq(_irq, NULL, max8997_muic_irq,        \
1924                                     0, _name, info);                    \
1925         if (ret < 0)                                                    \
1926                 dev_err(info->dev, "Failed to request IRQ #%d: %d\n",   \
1927                         _irq, ret);                                     \
1928 } while (0)
1929
1930 static int max8997_muic_irq_init(struct max8997_muic_info *info)
1931 {
1932         int ret;
1933
1934         REQUEST_IRQ(info->irq_adc, "muic-adc");
1935         REQUEST_IRQ(info->irq_chgtype, "muic-chgtype");
1936         REQUEST_IRQ(info->irq_vbvolt, "muic-vbvolt");
1937         REQUEST_IRQ(info->irq_adcerr, "muic-adcerr");
1938 #if defined(CONFIG_MUIC_MAX8997_OVPUI)
1939         REQUEST_IRQ(info->irq_chgins, "chg-insert");
1940         REQUEST_IRQ(info->irq_chgrm, "chg-remove");
1941 #endif /* CONFIG_MUIC_MAX8997_OVPUI */
1942         return 0;
1943 }
1944
1945 #define CHECK_GPIO(_gpio, _name)                                        \
1946 do {                                                                    \
1947         if (!_gpio) {                                                   \
1948                 dev_err(&pdev->dev, _name " GPIO defined as 0 !\n");    \
1949                 WARN_ON(!_gpio);                                        \
1950                 ret = -EIO;                                             \
1951                 goto err_kfree;                                         \
1952         }                                                               \
1953 } while (0)
1954
1955 static void max8997_muic_init_detect(struct work_struct *work)
1956 {
1957         struct max8997_muic_info *info = container_of(work,
1958                         struct max8997_muic_info, init_work.work);
1959
1960         dev_info(info->dev, "%s\n", __func__);
1961         if (!info->muic_data)
1962                 return;
1963
1964         mutex_lock(&info->mutex);
1965         detect_dev(info, -1);
1966         mutex_unlock(&info->mutex);
1967 }
1968
1969 static void max8997_muic_usb_detect(struct work_struct *work)
1970 {
1971         struct max8997_muic_info *info = container_of(work,
1972                         struct max8997_muic_info, usb_work.work);
1973         struct max8997_muic_data *mdata = info->muic_data;
1974
1975         dev_info(info->dev, "%s\n", __func__);
1976         if (!mdata)
1977                 return;
1978
1979         mutex_lock(&info->mutex);
1980         info->is_usb_ready = true;
1981
1982         if (info->muic_data->usb_path != CP_USB_MODE) {
1983                 if (mdata->usb_cb) {
1984                         switch (info->acc_type) {
1985                         case MUIC_ACC_TYPE_USB:
1986                         case MUIC_ACC_TYPE_JIG_USB_OFF:
1987                         case MUIC_ACC_TYPE_JIG_USB_ON:
1988                                 dev_info(info->dev, "%s: usb attach\n",
1989                                                 __func__);
1990                                 mdata->usb_cb(USB_CABLE_ATTACHED);
1991                                 break;
1992                         case MUIC_ACC_TYPE_OTG:
1993                                 dev_info(info->dev, "%s: otg attach\n",
1994                                                 __func__);
1995                                 mdata->usb_cb(USB_OTGHOST_ATTACHED);
1996                                 break;
1997                         default:
1998                                 break;
1999                         }
2000                 }
2001         }
2002         mutex_unlock(&info->mutex);
2003 }
2004
2005 static void max8997_muic_mhl_detect(struct work_struct *work)
2006 {
2007         struct max8997_muic_info *info = container_of(work,
2008                         struct max8997_muic_info, mhl_work.work);
2009         struct max8997_muic_data *mdata = info->muic_data;
2010
2011         dev_info(info->dev, "%s\n", __func__);
2012         if (!mdata)
2013                 return;
2014
2015         mutex_lock(&info->mutex);
2016         info->is_mhl_ready = true;
2017 #if !defined(CONFIG_MACH_U1) && !defined(CONFIG_MACH_TRATS)
2018         if (mdata->is_mhl_attached) {
2019                 if (!mdata->is_mhl_attached())
2020                         goto out;
2021         }
2022 #endif /* !CONFIG_MACH_U1 */
2023         if (info->acc_type == MUIC_ACC_TYPE_MHL) {
2024                 dev_info(info->dev, "%s: mhl attach\n", __func__);
2025 #ifdef CONFIG_EXTCON
2026                 if (info->edev)
2027                         extcon_set_cable_state(info->edev, "MHL", true);
2028 #else
2029                 if (mdata->mhl_cb)
2030                         mdata->mhl_cb(MAX8997_MUIC_ATTACHED);
2031 #endif
2032         }
2033 out:
2034         mutex_unlock(&info->mutex);
2035 }
2036
2037 extern struct device *switch_dev;
2038
2039 static int __devinit max8997_muic_probe(struct platform_device *pdev)
2040 {
2041         struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
2042         struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
2043         struct max8997_muic_info *info;
2044         struct input_dev *input;
2045         int ret;
2046
2047         info = kzalloc(sizeof(struct max8997_muic_info), GFP_KERNEL);
2048         input = input_allocate_device();
2049         if (!info || !input) {
2050                 dev_err(&pdev->dev, "%s: failed to allocate state\n", __func__);
2051                 ret = -ENOMEM;
2052                 goto err_kfree;
2053         }
2054
2055         info->dev = &pdev->dev;
2056         info->max8997 = max8997;
2057         info->muic = max8997->muic;
2058         info->input = input;
2059         info->irq_adc = max8997->irq_base + MAX8997_IRQ_ADC;
2060         info->irq_chgtype = max8997->irq_base + MAX8997_IRQ_CHGTYP;
2061         info->irq_vbvolt = max8997->irq_base + MAX8997_IRQ_VBVOLT;
2062         info->irq_adcerr = max8997->irq_base + MAX8997_IRQ_ADCERR;
2063 #if defined(CONFIG_MUIC_MAX8997_OVPUI)
2064         info->irq_chgins = max8997->irq_base + MAX8997_IRQ_CHGINS;
2065         info->irq_chgrm = max8997->irq_base + MAX8997_IRQ_CHGRM;
2066 #endif /* CONFIG_MUIC_MAX8997_OVPUI */
2067         if (pdata->muic) {
2068                 info->muic_data = pdata->muic;
2069                 info->muic_data->usb_path = AP_USB_MODE;
2070         }
2071         info->acc_type = MUIC_ACC_TYPE_UNKNOWN;
2072         info->chg_type = MUIC_CHG_TYPE_UNKNOWN;
2073
2074         platform_set_drvdata(pdev, info);
2075
2076         input->name = pdev->name;
2077         input->phys = "deskdock-key/input0";
2078         input->dev.parent = &pdev->dev;
2079
2080         input->id.bustype = BUS_HOST;
2081         input->id.vendor = 0x0001;
2082         input->id.product = 0x0001;
2083         input->id.version = 0x0001;
2084
2085         /* Enable auto repeat feature of Linux input subsystem */
2086         __set_bit(EV_REP, input->evbit);
2087
2088         input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
2089         input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
2090         input_set_capability(input, EV_KEY, KEY_PLAYPAUSE);
2091         input_set_capability(input, EV_KEY, KEY_PREVIOUSSONG);
2092         input_set_capability(input, EV_KEY, KEY_NEXTSONG);
2093
2094         ret = input_register_device(input);
2095         if (ret) {
2096                 dev_err(info->dev, "%s: Unable to register input device, "
2097                                 "error: %d\n", __func__, ret);
2098                 goto err_input;
2099         }
2100 #if !defined(CONFIG_MACH_U1_NA_USCC)
2101
2102         if (info->muic_data && gpio_is_valid(info->muic_data->gpio_usb_sel)) {
2103                 CHECK_GPIO(info->muic_data->gpio_usb_sel, "USB_SEL");
2104
2105                 if (info->muic_data->cfg_uart_gpio)
2106                         info->muic_data->uart_path =
2107                                 info->muic_data->cfg_uart_gpio();
2108
2109 #if !defined(CONFIG_TARGET_LOCALE_NA)
2110                 ret = gpio_request(info->muic_data->gpio_usb_sel, "USB_SEL");
2111                 if (ret) {
2112                         dev_info(info->dev, "%s: fail to request gpio(%d)\n",
2113                                         __func__, ret);
2114                         goto err_kfree;
2115                 }
2116                 if (gpio_get_value(info->muic_data->gpio_usb_sel)) {
2117                         dev_info(info->dev, "%s: CP USB\n", __func__);
2118                         info->muic_data->usb_path = CP_USB_MODE;
2119                 }
2120 #endif /* !CONFIG_TARGET_LOCALE_NA */
2121         }
2122 #endif
2123         /* create sysfs group*/
2124         ret = sysfs_create_group(&switch_dev->kobj, &max8997_muic_group);
2125         dev_set_drvdata(switch_dev, info);
2126
2127         if (ret) {
2128                 dev_err(&pdev->dev,
2129                                 "failed to create max8997 muic attribute group\n");
2130                 goto fail;
2131         }
2132
2133 #ifdef CONFIG_EXTCON
2134         /* External connector */
2135         info->edev = kzalloc(sizeof(struct extcon_dev), GFP_KERNEL);
2136         if (!info->edev) {
2137                 pr_err("Failed to allocate memory for extcon device\n");
2138                 ret = -ENOMEM;
2139                 goto fail;
2140         }
2141         info->edev->name = DEV_NAME;
2142         info->edev->supported_cable = extcon_cable_name;
2143         ret = extcon_dev_register(info->edev, NULL);
2144         if (ret) {
2145                 pr_err("Failed to register extcon device\n");
2146                 kfree(info->edev);
2147                 goto fail;
2148         }
2149 #endif
2150
2151         if (info->muic_data && info->muic_data->init_cb)
2152                 info->muic_data->init_cb();
2153
2154         mutex_init(&info->mutex);
2155
2156         /* Set ADC debounce time: 25ms */
2157         max8997_muic_set_adcdbset(info, 2);
2158
2159         mutex_lock(&info->mutex);
2160
2161         ret = max8997_muic_irq_init(info);
2162         if (ret < 0) {
2163                 dev_err(&pdev->dev, "Failed to initialize MUIC irq:%d\n", ret);
2164                 mutex_unlock(&info->mutex);
2165                 goto fail;
2166         }
2167
2168         /* initial cable detection */
2169         INIT_DELAYED_WORK(&info->init_work, max8997_muic_init_detect);
2170         schedule_delayed_work(&info->init_work, msecs_to_jiffies(3000));
2171
2172         INIT_DELAYED_WORK(&info->usb_work, max8997_muic_usb_detect);
2173         schedule_delayed_work(&info->usb_work, msecs_to_jiffies(17000));
2174
2175         INIT_DELAYED_WORK(&info->mhl_work, max8997_muic_mhl_detect);
2176         schedule_delayed_work(&info->mhl_work, msecs_to_jiffies(25000));
2177
2178         mutex_unlock(&info->mutex);
2179
2180         return 0;
2181
2182 fail:
2183         if (info->irq_adc)
2184                 free_irq(info->irq_adc, NULL);
2185         if (info->irq_chgtype)
2186                 free_irq(info->irq_chgtype, NULL);
2187         if (info->irq_vbvolt)
2188                 free_irq(info->irq_vbvolt, NULL);
2189         if (info->irq_adcerr)
2190                 free_irq(info->irq_adcerr, NULL);
2191         mutex_destroy(&info->mutex);
2192 err_input:
2193         platform_set_drvdata(pdev, NULL);
2194 err_kfree:
2195         input_free_device(input);
2196         kfree(info);
2197         return ret;
2198 }
2199
2200 static int __devexit max8997_muic_remove(struct platform_device *pdev)
2201 {
2202         struct max8997_muic_info *info = platform_get_drvdata(pdev);
2203
2204         sysfs_remove_group(&switch_dev->kobj, &max8997_muic_group);
2205
2206         if (info) {
2207                 input_unregister_device(info->input);
2208                 cancel_delayed_work(&info->init_work);
2209                 cancel_delayed_work(&info->usb_work);
2210                 cancel_delayed_work(&info->mhl_work);
2211                 free_irq(info->irq_adc, info);
2212                 free_irq(info->irq_chgtype, info);
2213                 free_irq(info->irq_vbvolt, info);
2214                 free_irq(info->irq_adcerr, info);
2215 #if !defined(CONFIG_TARGET_LOCALE_NA)
2216                 gpio_free(info->muic_data->gpio_usb_sel);
2217 #endif /* !CONFIG_TARGET_LOCALE_NA */
2218                 mutex_destroy(&info->mutex);
2219                 kfree(info);
2220         }
2221
2222         return 0;
2223 }
2224
2225 static u8 max8997_dumpaddr_muic[] = {
2226         MAX8997_MUIC_REG_INTMASK1,
2227         MAX8997_MUIC_REG_CDETCTRL,
2228         MAX8997_MUIC_REG_CTRL2,
2229 };
2230
2231 #if defined(CONFIG_PM)
2232 static int max8997_muic_freeze(struct device *dev)
2233 {
2234         struct platform_device *pdev = to_platform_device(dev);
2235         struct max8997_muic_info *info;
2236         int i;
2237         info = platform_get_drvdata(pdev);
2238
2239         for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
2240                 max8997_read_reg(info->max8997->muic, max8997_dumpaddr_muic[i],
2241                         &info->max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
2242
2243         cancel_delayed_work(&info->init_work);
2244         cancel_delayed_work(&info->usb_work);
2245         cancel_delayed_work(&info->mhl_work);
2246
2247         /* hibernation state, disconnect usb state*/
2248         dev_info(info->dev, "%s: DETACHED\n", __func__);
2249         mutex_lock(&info->mutex);
2250         handle_detach(info);
2251         mutex_unlock(&info->mutex);
2252
2253         return 0;
2254 }
2255
2256 static int max8997_muic_restore(struct device *dev)
2257 {
2258         struct platform_device *pdev = to_platform_device(dev);
2259         struct max8997_muic_info *info;
2260         int i;
2261         info = platform_get_drvdata(pdev);
2262
2263         for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
2264                 max8997_write_reg(info->max8997->muic, max8997_dumpaddr_muic[i],
2265                         info->max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
2266
2267         mutex_lock(&info->mutex);
2268         detect_dev(info, -1);
2269         mutex_unlock(&info->mutex);
2270
2271         return 0;
2272 }
2273
2274 static const struct dev_pm_ops max8997_dev_pm_ops = {
2275         .freeze         = max8997_muic_freeze,
2276         .restore        = max8997_muic_restore,
2277 };
2278
2279 #define MAX8997_DEV_PM_OPS      (&max8997_dev_pm_ops)
2280 #else
2281 #define MAX8997_DEV_PM_OPS      NULL
2282 #endif /* CONFIG_PM */
2283
2284 void max8997_muic_shutdown(struct device *dev)
2285 {
2286         struct max8997_muic_info *info = dev_get_drvdata(dev);
2287         int ret;
2288         u8 val;
2289
2290         if (!info->muic) {
2291                 dev_err(info->dev, "%s: no muic i2c client\n", __func__);
2292                 return;
2293         }
2294
2295         dev_info(info->dev, "%s: JIGSet: auto detection\n", __func__);
2296         val = (0 << CTRL3_JIGSET_SHIFT) | (0 << CTRL3_BOOTSET_SHIFT);
2297
2298         ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CTRL3, val,
2299                         CTRL3_JIGSET_MASK | CTRL3_BOOTSET_MASK);
2300         if (ret < 0) {
2301                 dev_err(info->dev, "%s: fail to update reg\n", __func__);
2302                 return;
2303         }
2304 }
2305
2306 static struct platform_driver max8997_muic_driver = {
2307         .driver         = {
2308                 .name   = "max8997-muic",
2309                 .owner  = THIS_MODULE,
2310                 .pm     = MAX8997_DEV_PM_OPS,
2311                 .shutdown = max8997_muic_shutdown,
2312         },
2313         .probe          = max8997_muic_probe,
2314         .remove         = __devexit_p(max8997_muic_remove),
2315 };
2316
2317 static int __init max8997_muic_init(void)
2318 {
2319         return platform_driver_register(&max8997_muic_driver);
2320 }
2321 module_init(max8997_muic_init);
2322
2323 static void __exit max8997_muic_exit(void)
2324 {
2325         platform_driver_unregister(&max8997_muic_driver);
2326 }
2327 module_exit(max8997_muic_exit);
2328
2329
2330 MODULE_DESCRIPTION("Maxim MAX8997 MUIC driver");
2331 MODULE_AUTHOR("<ms925.kim@samsung.com>");
2332 MODULE_LICENSE("GPL");