]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/extcon/extcon-max8997.c
extcon: max8997: Set default UART/USB path on probe
[mirror_ubuntu-artful-kernel.git] / drivers / extcon / extcon-max8997.c
CommitLineData
99f09beb 1/*
b76668ba 2 * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
99f09beb 3 *
2ca36f4a 4 * Copyright (C) 2012 Samsung Electronics
99f09beb
DK
5 * Donggeun Kim <dg77.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.
99f09beb
DK
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
23#include <linux/err.h>
24#include <linux/platform_device.h>
25#include <linux/kobject.h>
26#include <linux/mfd/max8997.h>
27#include <linux/mfd/max8997-private.h>
b76668ba 28#include <linux/extcon.h>
dca1a71e 29#include <linux/irqdomain.h>
b76668ba
CC
30
31#define DEV_NAME "max8997-muic"
99f09beb 32
027fcd50
CC
33enum max8997_muic_adc_debounce_time {
34 ADC_DEBOUNCE_TIME_0_5MS = 0, /* 0.5ms */
35 ADC_DEBOUNCE_TIME_10MS, /* 10ms */
36 ADC_DEBOUNCE_TIME_25MS, /* 25ms */
37 ADC_DEBOUNCE_TIME_38_62MS, /* 38.62ms */
38};
39
99f09beb
DK
40struct max8997_muic_irq {
41 unsigned int irq;
42 const char *name;
dca1a71e 43 unsigned int virq;
99f09beb
DK
44};
45
46static struct max8997_muic_irq muic_irqs[] = {
f73f6232
CC
47 { MAX8997_MUICIRQ_ADCError, "muic-ADCERROR" },
48 { MAX8997_MUICIRQ_ADCLow, "muic-ADCLOW" },
49 { MAX8997_MUICIRQ_ADC, "muic-ADC" },
50 { MAX8997_MUICIRQ_VBVolt, "muic-VBVOLT" },
51 { MAX8997_MUICIRQ_DBChg, "muic-DBCHG" },
52 { MAX8997_MUICIRQ_DCDTmr, "muic-DCDTMR" },
53 { MAX8997_MUICIRQ_ChgDetRun, "muic-CHGDETRUN" },
54 { MAX8997_MUICIRQ_ChgTyp, "muic-CHGTYP" },
55 { MAX8997_MUICIRQ_OVP, "muic-OVP" },
56};
57
58/* Define supported cable type */
59enum max8997_muic_acc_type {
60 MAX8997_MUIC_ADC_GROUND = 0x0,
61 MAX8997_MUIC_ADC_MHL, /* MHL*/
62 MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
63 MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
64 MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
65 MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
66 MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
67 MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
68 MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
69 MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
70 MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
71 MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
72 MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
73 MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
74 MAX8997_MUIC_ADC_RESERVED_ACC_1,
75 MAX8997_MUIC_ADC_RESERVED_ACC_2,
76 MAX8997_MUIC_ADC_RESERVED_ACC_3,
77 MAX8997_MUIC_ADC_RESERVED_ACC_4,
78 MAX8997_MUIC_ADC_RESERVED_ACC_5,
79 MAX8997_MUIC_ADC_CEA936_AUDIO,
80 MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
81 MAX8997_MUIC_ADC_TTY_CONVERTER,
82 MAX8997_MUIC_ADC_UART_CABLE,
83 MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
84 MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF, /* JIG-USB-OFF */
85 MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON, /* JIG-USB-ON */
86 MAX8997_MUIC_ADC_AV_CABLE_NOLOAD, /* DESKDOCK */
87 MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
88 MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF, /* JIG-UART */
89 MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON, /* CARDOCK */
90 MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
91 MAX8997_MUIC_ADC_OPEN, /* OPEN */
92};
93
94enum max8997_muic_cable_group {
95 MAX8997_CABLE_GROUP_ADC = 0,
96 MAX8997_CABLE_GROUP_ADC_GND,
97 MAX8997_CABLE_GROUP_CHG,
98 MAX8997_CABLE_GROUP_VBVOLT,
99};
100
101enum max8997_muic_usb_type {
102 MAX8997_USB_HOST,
103 MAX8997_USB_DEVICE,
104};
105
106enum max8997_muic_charger_type {
107 MAX8997_CHARGER_TYPE_NONE = 0,
108 MAX8997_CHARGER_TYPE_USB,
109 MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
110 MAX8997_CHARGER_TYPE_DEDICATED_CHG,
111 MAX8997_CHARGER_TYPE_500MA,
112 MAX8997_CHARGER_TYPE_1A,
113 MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
99f09beb
DK
114};
115
116struct max8997_muic_info {
117 struct device *dev;
99f09beb 118 struct i2c_client *muic;
f73f6232
CC
119 struct extcon_dev *edev;
120 int prev_cable_type;
121 int prev_chg_type;
122 u8 status[2];
99f09beb
DK
123
124 int irq;
125 struct work_struct irq_work;
99f09beb 126 struct mutex mutex;
b76668ba 127
f73f6232
CC
128 struct max8997_muic_platform_data *muic_pdata;
129 enum max8997_muic_charger_type pre_charger_type;
685dc9a7
CC
130
131 /*
132 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
133 * h/w path of COMP2/COMN1 on CONTROL1 register.
134 */
135 int path_usb;
136 int path_uart;
b76668ba
CC
137};
138
e3e5bc02
CC
139enum {
140 EXTCON_CABLE_USB = 0,
141 EXTCON_CABLE_USB_HOST,
142 EXTCON_CABLE_TA,
143 EXTCON_CABLE_FAST_CHARGER,
144 EXTCON_CABLE_SLOW_CHARGER,
145 EXTCON_CABLE_CHARGE_DOWNSTREAM,
146 EXTCON_CABLE_MHL,
147 EXTCON_CABLE_DOCK_DESK,
148 EXTCON_CABLE_DOCK_CARD,
149 EXTCON_CABLE_JIG,
150
151 _EXTCON_CABLE_NUM,
152};
153
cae93db3 154static const char *max8997_extcon_cable[] = {
e3e5bc02
CC
155 [EXTCON_CABLE_USB] = "USB",
156 [EXTCON_CABLE_USB_HOST] = "USB-Host",
157 [EXTCON_CABLE_TA] = "TA",
158 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
159 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
160 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
161 [EXTCON_CABLE_MHL] = "MHL",
162 [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
163 [EXTCON_CABLE_DOCK_CARD] = "Dock-Card",
164 [EXTCON_CABLE_JIG] = "JIG",
b76668ba
CC
165
166 NULL,
99f09beb
DK
167};
168
027fcd50
CC
169/*
170 * max8997_muic_set_debounce_time - Set the debounce time of ADC
171 * @info: the instance including private data of max8997 MUIC
172 * @time: the debounce time of ADC
173 */
174static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
175 enum max8997_muic_adc_debounce_time time)
176{
177 int ret;
178
179 switch (time) {
180 case ADC_DEBOUNCE_TIME_0_5MS:
181 case ADC_DEBOUNCE_TIME_10MS:
182 case ADC_DEBOUNCE_TIME_25MS:
183 case ADC_DEBOUNCE_TIME_38_62MS:
184 ret = max8997_update_reg(info->muic,
185 MAX8997_MUIC_REG_CONTROL3,
186 time << CONTROL3_ADCDBSET_SHIFT,
187 CONTROL3_ADCDBSET_MASK);
188 if (ret) {
189 dev_err(info->dev, "failed to set ADC debounce time\n");
190 return -EAGAIN;
191 }
192 break;
193 default:
194 dev_err(info->dev, "invalid ADC debounce time\n");
195 return -EINVAL;
196 }
197
198 return 0;
199};
200
07c70503
CC
201/*
202 * max8997_muic_set_path - Set hardware line according to attached cable
203 * @info: the instance including private data of max8997 MUIC
204 * @value: the path according to attached cable
205 * @attached: the state of cable (true:attached, false:detached)
206 *
207 * The max8997 MUIC device share outside H/W line among a varity of cables,
208 * so this function set internal path of H/W line according to the type of
209 * attached cable.
210 */
211static int max8997_muic_set_path(struct max8997_muic_info *info,
212 u8 val, bool attached)
213{
214 int ret = 0;
215 u8 ctrl1, ctrl2 = 0;
216
217 if (attached)
218 ctrl1 = val;
219 else
220 ctrl1 = CONTROL1_SW_OPEN;
221
222 ret = max8997_update_reg(info->muic,
223 MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
224 if (ret < 0) {
225 dev_err(info->dev, "failed to update MUIC register\n");
226 return -EAGAIN;
227 }
228
229 if (attached)
230 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
231 else
232 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
233
234 ret = max8997_update_reg(info->muic,
235 MAX8997_MUIC_REG_CONTROL2, ctrl2,
236 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
237 if (ret < 0) {
238 dev_err(info->dev, "failed to update MUIC register\n");
239 return -EAGAIN;
240 }
241
242 dev_info(info->dev,
243 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
244 ctrl1, ctrl2, attached ? "attached" : "detached");
245
246 return 0;
247}
248
f73f6232
CC
249/*
250 * max8997_muic_get_cable_type - Return cable type and check cable state
251 * @info: the instance including private data of max8997 MUIC
252 * @group: the path according to attached cable
253 * @attached: store cable state and return
254 *
255 * This function check the cable state either attached or detached,
256 * and then divide precise type of cable according to cable group.
257 * - MAX8997_CABLE_GROUP_ADC
258 * - MAX8997_CABLE_GROUP_CHG
259 */
260static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
261 enum max8997_muic_cable_group group, bool *attached)
262{
263 int cable_type = 0;
264 int adc;
265 int chg_type;
266
267 switch (group) {
268 case MAX8997_CABLE_GROUP_ADC:
269 /*
270 * Read ADC value to check cable type and decide cable state
271 * according to cable type
272 */
273 adc = info->status[0] & STATUS1_ADC_MASK;
274 adc >>= STATUS1_ADC_SHIFT;
275
276 /*
277 * Check current cable state/cable type and store cable type
278 * (info->prev_cable_type) for handling cable when cable is
279 * detached.
280 */
281 if (adc == MAX8997_MUIC_ADC_OPEN) {
282 *attached = false;
283
284 cable_type = info->prev_cable_type;
285 info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
286 } else {
287 *attached = true;
288
289 cable_type = info->prev_cable_type = adc;
290 }
291 break;
292 case MAX8997_CABLE_GROUP_CHG:
293 /*
294 * Read charger type to check cable type and decide cable state
295 * according to type of charger cable.
296 */
297 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
298 chg_type >>= STATUS2_CHGTYP_SHIFT;
299
300 if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
301 *attached = false;
302
303 cable_type = info->prev_chg_type;
304 info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
305 } else {
306 *attached = true;
307
308 /*
309 * Check current cable state/cable type and store cable
310 * type(info->prev_chg_type) for handling cable when
311 * charger cable is detached.
312 */
313 cable_type = info->prev_chg_type = chg_type;
314 }
315
316 break;
317 default:
318 dev_err(info->dev, "Unknown cable group (%d)\n", group);
319 cable_type = -EINVAL;
320 break;
321 }
322
323 return cable_type;
324}
325
99f09beb
DK
326static int max8997_muic_handle_usb(struct max8997_muic_info *info,
327 enum max8997_muic_usb_type usb_type, bool attached)
328{
99f09beb
DK
329 int ret = 0;
330
331 if (usb_type == MAX8997_USB_HOST) {
685dc9a7 332 ret = max8997_muic_set_path(info, info->path_usb, attached);
f73f6232 333 if (ret < 0) {
99f09beb 334 dev_err(info->dev, "failed to update muic register\n");
f73f6232 335 return ret;
99f09beb
DK
336 }
337 }
338
b76668ba
CC
339 switch (usb_type) {
340 case MAX8997_USB_HOST:
341 extcon_set_cable_state(info->edev, "USB-Host", attached);
342 break;
343 case MAX8997_USB_DEVICE:
344 extcon_set_cable_state(info->edev, "USB", attached);
345 break;
346 default:
f73f6232
CC
347 dev_err(info->dev, "failed to detect %s usb cable\n",
348 attached ? "attached" : "detached");
349 return -EINVAL;
b76668ba
CC
350 }
351
f73f6232 352 return 0;
99f09beb
DK
353}
354
99f09beb 355static int max8997_muic_handle_dock(struct max8997_muic_info *info,
f73f6232 356 int cable_type, bool attached)
99f09beb 357{
99f09beb
DK
358 int ret = 0;
359
07c70503 360 ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
99f09beb
DK
361 if (ret) {
362 dev_err(info->dev, "failed to update muic register\n");
f73f6232 363 return ret;
99f09beb
DK
364 }
365
f73f6232
CC
366 switch (cable_type) {
367 case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
b76668ba 368 extcon_set_cable_state(info->edev, "Dock-desk", attached);
99f09beb 369 break;
f73f6232 370 case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
b76668ba 371 extcon_set_cable_state(info->edev, "Dock-card", attached);
99f09beb
DK
372 break;
373 default:
f73f6232
CC
374 dev_err(info->dev, "failed to detect %s dock device\n",
375 attached ? "attached" : "detached");
376 return -EINVAL;
99f09beb 377 }
f73f6232
CC
378
379 return 0;
99f09beb
DK
380}
381
382static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
383 bool attached)
384{
99f09beb
DK
385 int ret = 0;
386
387 /* switch to UART */
685dc9a7 388 ret = max8997_muic_set_path(info, info->path_uart, attached);
99f09beb
DK
389 if (ret) {
390 dev_err(info->dev, "failed to update muic register\n");
f73f6232 391 return -EINVAL;
99f09beb
DK
392 }
393
b76668ba 394 extcon_set_cable_state(info->edev, "JIG", attached);
99f09beb 395
f73f6232 396 return 0;
99f09beb
DK
397}
398
f73f6232 399static int max8997_muic_adc_handler(struct max8997_muic_info *info)
99f09beb 400{
f73f6232
CC
401 int cable_type;
402 bool attached;
99f09beb
DK
403 int ret = 0;
404
f73f6232
CC
405 /* Check cable state which is either detached or attached */
406 cable_type = max8997_muic_get_cable_type(info,
407 MAX8997_CABLE_GROUP_ADC, &attached);
408
409 switch (cable_type) {
410 case MAX8997_MUIC_ADC_GROUND:
411 ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
412 if (ret < 0)
413 return ret;
414 break;
415 case MAX8997_MUIC_ADC_MHL:
416 extcon_set_cable_state(info->edev, "MHL", attached);
417 break;
418 case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
419 case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
420 ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, attached);
421 if (ret < 0)
422 return ret;
423 break;
424 case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
425 case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
426 ret = max8997_muic_handle_dock(info, cable_type, attached);
427 if (ret < 0)
428 return ret;
429 break;
430 case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
431 ret = max8997_muic_handle_jig_uart(info, attached);
432 break;
433 case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
434 case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
435 case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
436 case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
437 case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
438 case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
439 case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
440 case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
441 case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
442 case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
443 case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
444 case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
445 case MAX8997_MUIC_ADC_RESERVED_ACC_1:
446 case MAX8997_MUIC_ADC_RESERVED_ACC_2:
447 case MAX8997_MUIC_ADC_RESERVED_ACC_3:
448 case MAX8997_MUIC_ADC_RESERVED_ACC_4:
449 case MAX8997_MUIC_ADC_RESERVED_ACC_5:
450 case MAX8997_MUIC_ADC_CEA936_AUDIO:
451 case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
452 case MAX8997_MUIC_ADC_TTY_CONVERTER:
453 case MAX8997_MUIC_ADC_UART_CABLE:
454 case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
455 case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
456 case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
457 /*
458 * This cable isn't used in general case if it is specially
459 * needed to detect additional cable, should implement
460 * proper operation when this cable is attached/detached.
461 */
462 dev_info(info->dev,
463 "cable is %s but it isn't used (type:0x%x)\n",
464 attached ? "attached" : "detached", cable_type);
465 return -EAGAIN;
b76668ba 466 default:
f73f6232
CC
467 dev_err(info->dev,
468 "failed to detect %s unknown cable (type:0x%x)\n",
469 attached ? "attached" : "detached", cable_type);
3cafbd4e 470 return -EINVAL;
b76668ba 471 }
99f09beb 472
3cafbd4e 473 return 0;
99f09beb
DK
474}
475
f73f6232 476static int max8997_muic_chg_handler(struct max8997_muic_info *info)
99f09beb 477{
f73f6232
CC
478 int chg_type;
479 bool attached;
480 int adc;
99f09beb 481
f73f6232
CC
482 chg_type = max8997_muic_get_cable_type(info,
483 MAX8997_CABLE_GROUP_CHG, &attached);
99f09beb 484
f73f6232 485 switch (chg_type) {
99f09beb 486 case MAX8997_CHARGER_TYPE_NONE:
99f09beb
DK
487 break;
488 case MAX8997_CHARGER_TYPE_USB:
f73f6232
CC
489 adc = info->status[0] & STATUS1_ADC_MASK;
490 adc >>= STATUS1_ADC_SHIFT;
491
492 if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
99f09beb 493 max8997_muic_handle_usb(info,
f73f6232 494 MAX8997_USB_DEVICE, attached);
99f09beb 495 }
99f09beb
DK
496 break;
497 case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
f73f6232 498 extcon_set_cable_state(info->edev, "Charge-downstream", attached);
b76668ba 499 break;
99f09beb 500 case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
f73f6232 501 extcon_set_cable_state(info->edev, "TA", attached);
b76668ba 502 break;
99f09beb 503 case MAX8997_CHARGER_TYPE_500MA:
f73f6232 504 extcon_set_cable_state(info->edev, "Slow-charger", attached);
b76668ba 505 break;
99f09beb 506 case MAX8997_CHARGER_TYPE_1A:
f73f6232 507 extcon_set_cable_state(info->edev, "Fast-charger", attached);
99f09beb
DK
508 break;
509 default:
f73f6232
CC
510 dev_err(info->dev,
511 "failed to detect %s unknown chg cable (type:0x%x)\n",
512 attached ? "attached" : "detached", chg_type);
513 return -EINVAL;
99f09beb
DK
514 }
515
f73f6232 516 return 0;
99f09beb
DK
517}
518
519static void max8997_muic_irq_work(struct work_struct *work)
520{
521 struct max8997_muic_info *info = container_of(work,
522 struct max8997_muic_info, irq_work);
dca1a71e
CC
523 int irq_type = 0;
524 int i, ret;
99f09beb 525
f73f6232
CC
526 if (!info->edev)
527 return;
528
99f09beb
DK
529 mutex_lock(&info->mutex);
530
f73f6232
CC
531 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
532 if (info->irq == muic_irqs[i].virq)
533 irq_type = muic_irqs[i].irq;
534
99f09beb 535 ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
f73f6232 536 2, info->status);
99f09beb
DK
537 if (ret) {
538 dev_err(info->dev, "failed to read muic register\n");
539 mutex_unlock(&info->mutex);
540 return;
541 }
542
99f09beb 543 switch (irq_type) {
f73f6232
CC
544 case MAX8997_MUICIRQ_ADCError:
545 case MAX8997_MUICIRQ_ADCLow:
99f09beb 546 case MAX8997_MUICIRQ_ADC:
f73f6232
CC
547 /* Handle all of cable except for charger cable */
548 ret = max8997_muic_adc_handler(info);
99f09beb 549 break;
f73f6232
CC
550 case MAX8997_MUICIRQ_VBVolt:
551 case MAX8997_MUICIRQ_DBChg:
552 case MAX8997_MUICIRQ_DCDTmr:
553 case MAX8997_MUICIRQ_ChgDetRun:
99f09beb 554 case MAX8997_MUICIRQ_ChgTyp:
f73f6232
CC
555 /* Handle charger cable */
556 ret = max8997_muic_chg_handler(info);
557 break;
558 case MAX8997_MUICIRQ_OVP:
99f09beb
DK
559 break;
560 default:
b76668ba
CC
561 dev_info(info->dev, "misc interrupt: irq %d occurred\n",
562 irq_type);
f73f6232
CC
563 mutex_unlock(&info->mutex);
564 return;
99f09beb
DK
565 }
566
f73f6232
CC
567 if (ret < 0)
568 dev_err(info->dev, "failed to handle MUIC interrupt\n");
569
99f09beb
DK
570 mutex_unlock(&info->mutex);
571
572 return;
573}
574
575static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
576{
577 struct max8997_muic_info *info = data;
578
579 dev_dbg(info->dev, "irq:%d\n", irq);
580 info->irq = irq;
581
582 schedule_work(&info->irq_work);
583
584 return IRQ_HANDLED;
585}
586
f73f6232 587static int max8997_muic_detect_dev(struct max8997_muic_info *info)
99f09beb 588{
f73f6232
CC
589 int ret = 0;
590 int adc;
591 int chg_type;
592 bool attached;
99f09beb 593
f73f6232
CC
594 mutex_lock(&info->mutex);
595
596 /* Read STATUSx register to detect accessory */
597 ret = max8997_bulk_read(info->muic,
598 MAX8997_MUIC_REG_STATUS1, 2, info->status);
99f09beb 599 if (ret) {
f73f6232
CC
600 dev_err(info->dev, "failed to read MUIC register\n");
601 mutex_unlock(&info->mutex);
602 return -EINVAL;
99f09beb
DK
603 }
604
f73f6232
CC
605 adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
606 &attached);
607 if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
608 ret = max8997_muic_adc_handler(info);
609 if (ret < 0) {
610 dev_err(info->dev, "Cannot detect ADC cable\n");
611 mutex_unlock(&info->mutex);
612 return ret;
613 }
614 }
99f09beb 615
f73f6232
CC
616 chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
617 &attached);
618 if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
619 ret = max8997_muic_chg_handler(info);
620 if (ret < 0) {
621 dev_err(info->dev, "Cannot detect charger cable\n");
622 mutex_unlock(&info->mutex);
623 return ret;
624 }
625 }
99f09beb 626
f73f6232 627 mutex_unlock(&info->mutex);
99f09beb 628
f73f6232 629 return 0;
99f09beb
DK
630}
631
44f34fd4 632static int max8997_muic_probe(struct platform_device *pdev)
99f09beb 633{
b76668ba
CC
634 struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
635 struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
99f09beb
DK
636 struct max8997_muic_info *info;
637 int ret, i;
638
0b672e9b
SK
639 info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
640 GFP_KERNEL);
99f09beb
DK
641 if (!info) {
642 dev_err(&pdev->dev, "failed to allocate memory\n");
0b672e9b 643 return -ENOMEM;
99f09beb
DK
644 }
645
99f09beb 646 info->dev = &pdev->dev;
b76668ba 647 info->muic = max8997->muic;
99f09beb
DK
648
649 platform_set_drvdata(pdev, info);
650 mutex_init(&info->mutex);
651
99f09beb
DK
652 INIT_WORK(&info->irq_work, max8997_muic_irq_work);
653
654 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
655 struct max8997_muic_irq *muic_irq = &muic_irqs[i];
68c9274d 656 unsigned int virq = 0;
dca1a71e
CC
657
658 virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
68c9274d
SK
659 if (!virq) {
660 ret = -EINVAL;
dca1a71e 661 goto err_irq;
68c9274d 662 }
dca1a71e 663 muic_irq->virq = virq;
99f09beb 664
ae3b3215
CC
665 ret = request_threaded_irq(virq, NULL,
666 max8997_muic_irq_handler,
667 IRQF_NO_SUSPEND,
668 muic_irq->name, info);
99f09beb
DK
669 if (ret) {
670 dev_err(&pdev->dev,
671 "failed: irq request (IRQ: %d,"
672 " error :%d)\n",
673 muic_irq->irq, ret);
99f09beb
DK
674 goto err_irq;
675 }
676 }
677
b76668ba 678 /* External connector */
0b672e9b
SK
679 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
680 GFP_KERNEL);
b76668ba
CC
681 if (!info->edev) {
682 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
683 ret = -ENOMEM;
684 goto err_irq;
685 }
686 info->edev->name = DEV_NAME;
687 info->edev->supported_cable = max8997_extcon_cable;
688 ret = extcon_dev_register(info->edev, NULL);
689 if (ret) {
690 dev_err(&pdev->dev, "failed to register extcon device\n");
0b672e9b 691 goto err_irq;
b76668ba
CC
692 }
693
99f09beb 694 /* Initialize registers according to platform data */
b76668ba
CC
695 if (pdata->muic_pdata) {
696 struct max8997_muic_platform_data *mdata = info->muic_pdata;
697
698 for (i = 0; i < mdata->num_init_data; i++) {
699 max8997_write_reg(info->muic, mdata->init_data[i].addr,
700 mdata->init_data[i].data);
701 }
702 }
99f09beb 703
685dc9a7
CC
704 /*
705 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
706 * h/w path of COMP2/COMN1 on CONTROL1 register.
707 */
708 if (pdata->muic_pdata->path_uart)
709 info->path_uart = pdata->muic_pdata->path_uart;
710 else
711 info->path_uart = CONTROL1_SW_UART;
712
713 if (pdata->muic_pdata->path_usb)
714 info->path_usb = pdata->muic_pdata->path_usb;
715 else
716 info->path_usb = CONTROL1_SW_USB;
717
718 /* Set initial path for UART */
719 max8997_muic_set_path(info, info->path_uart, true);
720
027fcd50
CC
721 /* Set ADC debounce time */
722 max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
723
99f09beb 724 /* Initial device detection */
f73f6232
CC
725 ret = max8997_muic_detect_dev(info);
726 if (ret < 0) {
727 dev_err(&pdev->dev, "failed to detect cable type\n");
728 goto err_extcon;
729 }
99f09beb
DK
730
731 return ret;
732
f73f6232
CC
733err_extcon:
734 extcon_dev_unregister(info->edev);
99f09beb 735err_irq:
3241d56e 736 while (--i >= 0)
dca1a71e 737 free_irq(muic_irqs[i].virq, info);
99f09beb
DK
738 return ret;
739}
740
93ed0327 741static int max8997_muic_remove(struct platform_device *pdev)
99f09beb
DK
742{
743 struct max8997_muic_info *info = platform_get_drvdata(pdev);
99f09beb
DK
744 int i;
745
99f09beb 746 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
dca1a71e 747 free_irq(muic_irqs[i].virq, info);
71e58782 748 cancel_work_sync(&info->irq_work);
99f09beb 749
b76668ba
CC
750 extcon_dev_unregister(info->edev);
751
99f09beb
DK
752 return 0;
753}
754
755static struct platform_driver max8997_muic_driver = {
756 .driver = {
b76668ba 757 .name = DEV_NAME,
99f09beb
DK
758 .owner = THIS_MODULE,
759 },
760 .probe = max8997_muic_probe,
5f7e2228 761 .remove = max8997_muic_remove,
99f09beb
DK
762};
763
b00e126f 764module_platform_driver(max8997_muic_driver);
99f09beb 765
b76668ba 766MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
99f09beb
DK
767MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
768MODULE_LICENSE("GPL");