blob: 59f084fee4f3f82f69d44bcc073a9b5002183f18 [file] [log] [blame]
Chanwoo Choi962e56b2013-11-22 16:51:06 +01001/*
2 * extcon-max14577.c - MAX14577 extcon driver to support MAX14577 MUIC
3 *
4 * Copyright (C) 2013 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@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
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
23#include <linux/mfd/max14577.h>
24#include <linux/mfd/max14577-private.h>
25#include <linux/extcon.h>
26
Chanwoo Choi962e56b2013-11-22 16:51:06 +010027#define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
28
29enum max14577_muic_adc_debounce_time {
30 ADC_DEBOUNCE_TIME_5MS = 0,
31 ADC_DEBOUNCE_TIME_10MS,
32 ADC_DEBOUNCE_TIME_25MS,
33 ADC_DEBOUNCE_TIME_38_62MS,
34};
35
36enum max14577_muic_status {
37 MAX14577_MUIC_STATUS1 = 0,
38 MAX14577_MUIC_STATUS2 = 1,
39 MAX14577_MUIC_STATUS_END,
40};
41
42struct max14577_muic_info {
43 struct device *dev;
44 struct max14577 *max14577;
45 struct extcon_dev *edev;
46 int prev_cable_type;
47 int prev_chg_type;
48 u8 status[MAX14577_MUIC_STATUS_END];
49
50 bool irq_adc;
51 bool irq_chg;
52 struct work_struct irq_work;
53 struct mutex mutex;
54
55 /*
56 * Use delayed workqueue to detect cable state and then
57 * notify cable state to notifiee/platform through uevent.
58 * After completing the booting of platform, the extcon provider
59 * driver should notify cable state to upper layer.
60 */
61 struct delayed_work wq_detcable;
62
63 /*
64 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
65 * h/w path of COMP2/COMN1 on CONTROL1 register.
66 */
67 int path_usb;
68 int path_uart;
69};
70
71enum max14577_muic_cable_group {
72 MAX14577_CABLE_GROUP_ADC = 0,
73 MAX14577_CABLE_GROUP_CHG,
74};
75
76/**
77 * struct max14577_muic_irq
78 * @irq: the index of irq list of MUIC device.
79 * @name: the name of irq.
80 * @virq: the virtual irq to use irq domain
81 */
82struct max14577_muic_irq {
83 unsigned int irq;
84 const char *name;
85 unsigned int virq;
86};
87
88static struct max14577_muic_irq muic_irqs[] = {
89 { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
90 { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
91 { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
92 { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
93 { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
94 { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
95 { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
96 { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
97};
98
99/* Define supported accessory type */
100enum max14577_muic_acc_type {
101 MAX14577_MUIC_ADC_GROUND = 0x0,
102 MAX14577_MUIC_ADC_SEND_END_BUTTON,
103 MAX14577_MUIC_ADC_REMOTE_S1_BUTTON,
104 MAX14577_MUIC_ADC_REMOTE_S2_BUTTON,
105 MAX14577_MUIC_ADC_REMOTE_S3_BUTTON,
106 MAX14577_MUIC_ADC_REMOTE_S4_BUTTON,
107 MAX14577_MUIC_ADC_REMOTE_S5_BUTTON,
108 MAX14577_MUIC_ADC_REMOTE_S6_BUTTON,
109 MAX14577_MUIC_ADC_REMOTE_S7_BUTTON,
110 MAX14577_MUIC_ADC_REMOTE_S8_BUTTON,
111 MAX14577_MUIC_ADC_REMOTE_S9_BUTTON,
112 MAX14577_MUIC_ADC_REMOTE_S10_BUTTON,
113 MAX14577_MUIC_ADC_REMOTE_S11_BUTTON,
114 MAX14577_MUIC_ADC_REMOTE_S12_BUTTON,
115 MAX14577_MUIC_ADC_RESERVED_ACC_1,
116 MAX14577_MUIC_ADC_RESERVED_ACC_2,
117 MAX14577_MUIC_ADC_RESERVED_ACC_3,
118 MAX14577_MUIC_ADC_RESERVED_ACC_4,
119 MAX14577_MUIC_ADC_RESERVED_ACC_5,
120 MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2,
121 MAX14577_MUIC_ADC_PHONE_POWERED_DEV,
122 MAX14577_MUIC_ADC_TTY_CONVERTER,
123 MAX14577_MUIC_ADC_UART_CABLE,
124 MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG,
125 MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF,
126 MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON,
127 MAX14577_MUIC_ADC_AV_CABLE_NOLOAD,
128 MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG,
129 MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF,
130 MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON,
131 MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1, /* with Remote and Simple Ctrl */
132 MAX14577_MUIC_ADC_OPEN,
133};
134
135/* max14577 MUIC device support below list of accessories(external connector) */
136enum {
137 EXTCON_CABLE_USB = 0,
138 EXTCON_CABLE_TA,
139 EXTCON_CABLE_FAST_CHARGER,
140 EXTCON_CABLE_SLOW_CHARGER,
141 EXTCON_CABLE_CHARGE_DOWNSTREAM,
142 EXTCON_CABLE_JIG_USB_ON,
143 EXTCON_CABLE_JIG_USB_OFF,
144 EXTCON_CABLE_JIG_UART_OFF,
145 EXTCON_CABLE_JIG_UART_ON,
146
147 _EXTCON_CABLE_NUM,
148};
149
150static const char *max14577_extcon_cable[] = {
151 [EXTCON_CABLE_USB] = "USB",
152 [EXTCON_CABLE_TA] = "TA",
153 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
154 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
155 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
156 [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON",
157 [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF",
158 [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF",
159 [EXTCON_CABLE_JIG_UART_ON] = "JIG-UART-ON",
160
161 NULL,
162};
163
164/*
165 * max14577_muic_set_debounce_time - Set the debounce time of ADC
166 * @info: the instance including private data of max14577 MUIC
167 * @time: the debounce time of ADC
168 */
169static int max14577_muic_set_debounce_time(struct max14577_muic_info *info,
170 enum max14577_muic_adc_debounce_time time)
171{
172 u8 ret;
173
174 switch (time) {
175 case ADC_DEBOUNCE_TIME_5MS:
176 case ADC_DEBOUNCE_TIME_10MS:
177 case ADC_DEBOUNCE_TIME_25MS:
178 case ADC_DEBOUNCE_TIME_38_62MS:
179 ret = max14577_update_reg(info->max14577->regmap,
180 MAX14577_MUIC_REG_CONTROL3,
181 CTRL3_ADCDBSET_MASK,
182 time << CTRL3_ADCDBSET_SHIFT);
183 if (ret) {
184 dev_err(info->dev, "failed to set ADC debounce time\n");
185 return ret;
186 }
187 break;
188 default:
189 dev_err(info->dev, "invalid ADC debounce time\n");
190 return -EINVAL;
191 }
192
193 return 0;
194};
195
196/*
197 * max14577_muic_set_path - Set hardware line according to attached cable
198 * @info: the instance including private data of max14577 MUIC
199 * @value: the path according to attached cable
200 * @attached: the state of cable (true:attached, false:detached)
201 *
202 * The max14577 MUIC device share outside H/W line among a varity of cables
203 * so, this function set internal path of H/W line according to the type of
204 * attached cable.
205 */
206static int max14577_muic_set_path(struct max14577_muic_info *info,
207 u8 val, bool attached)
208{
209 int ret = 0;
210 u8 ctrl1, ctrl2 = 0;
211
212 /* Set open state to path before changing hw path */
213 ret = max14577_update_reg(info->max14577->regmap,
214 MAX14577_MUIC_REG_CONTROL1,
215 CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN);
216 if (ret < 0) {
217 dev_err(info->dev, "failed to update MUIC register\n");
218 return ret;
219 }
220
221 if (attached)
222 ctrl1 = val;
223 else
224 ctrl1 = CTRL1_SW_OPEN;
225
226 ret = max14577_update_reg(info->max14577->regmap,
227 MAX14577_MUIC_REG_CONTROL1,
228 CLEAR_IDBEN_MICEN_MASK, ctrl1);
229 if (ret < 0) {
230 dev_err(info->dev, "failed to update MUIC register\n");
231 return ret;
232 }
233
234 if (attached)
235 ctrl2 |= CTRL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
236 else
237 ctrl2 |= CTRL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
238
239 ret = max14577_update_reg(info->max14577->regmap,
240 MAX14577_REG_CONTROL2,
241 CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2);
242 if (ret < 0) {
243 dev_err(info->dev, "failed to update MUIC register\n");
244 return ret;
245 }
246
247 dev_dbg(info->dev,
248 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
249 ctrl1, ctrl2, attached ? "attached" : "detached");
250
251 return 0;
252}
253
254/*
255 * max14577_muic_get_cable_type - Return cable type and check cable state
256 * @info: the instance including private data of max14577 MUIC
257 * @group: the path according to attached cable
258 * @attached: store cable state and return
259 *
260 * This function check the cable state either attached or detached,
261 * and then divide precise type of cable according to cable group.
262 * - max14577_CABLE_GROUP_ADC
263 * - max14577_CABLE_GROUP_CHG
264 */
265static int max14577_muic_get_cable_type(struct max14577_muic_info *info,
266 enum max14577_muic_cable_group group, bool *attached)
267{
268 int cable_type = 0;
269 int adc;
270 int chg_type;
271
272 switch (group) {
273 case MAX14577_CABLE_GROUP_ADC:
274 /*
275 * Read ADC value to check cable type and decide cable state
276 * according to cable type
277 */
278 adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK;
279 adc >>= STATUS1_ADC_SHIFT;
280
281 /*
282 * Check current cable state/cable type and store cable type
283 * (info->prev_cable_type) for handling cable when cable is
284 * detached.
285 */
286 if (adc == MAX14577_MUIC_ADC_OPEN) {
287 *attached = false;
288
289 cable_type = info->prev_cable_type;
290 info->prev_cable_type = MAX14577_MUIC_ADC_OPEN;
291 } else {
292 *attached = true;
293
294 cable_type = info->prev_cable_type = adc;
295 }
296 break;
297 case MAX14577_CABLE_GROUP_CHG:
298 /*
299 * Read charger type to check cable type and decide cable state
300 * according to type of charger cable.
301 */
302 chg_type = info->status[MAX14577_MUIC_STATUS2] &
303 STATUS2_CHGTYP_MASK;
304 chg_type >>= STATUS2_CHGTYP_SHIFT;
305
306 if (chg_type == MAX14577_CHARGER_TYPE_NONE) {
307 *attached = false;
308
309 cable_type = info->prev_chg_type;
310 info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE;
311 } else {
312 *attached = true;
313
314 /*
315 * Check current cable state/cable type and store cable
316 * type(info->prev_chg_type) for handling cable when
317 * charger cable is detached.
318 */
319 cable_type = info->prev_chg_type = chg_type;
320 }
321
322 break;
323 default:
324 dev_err(info->dev, "Unknown cable group (%d)\n", group);
325 cable_type = -EINVAL;
326 break;
327 }
328
329 return cable_type;
330}
331
332static int max14577_muic_jig_handler(struct max14577_muic_info *info,
333 int cable_type, bool attached)
334{
335 char cable_name[32];
336 int ret = 0;
337 u8 path = CTRL1_SW_OPEN;
338
339 dev_dbg(info->dev,
340 "external connector is %s (adc:0x%02x)\n",
341 attached ? "attached" : "detached", cable_type);
342
343 switch (cable_type) {
344 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
345 /* PATH:AP_USB */
346 strcpy(cable_name, "JIG-USB-OFF");
347 path = CTRL1_SW_USB;
348 break;
349 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
350 /* PATH:AP_USB */
351 strcpy(cable_name, "JIG-USB-ON");
352 path = CTRL1_SW_USB;
353 break;
354 case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
355 /* PATH:AP_UART */
356 strcpy(cable_name, "JIG-UART-OFF");
357 path = CTRL1_SW_UART;
358 break;
359 default:
360 dev_err(info->dev, "failed to detect %s jig cable\n",
361 attached ? "attached" : "detached");
362 return -EINVAL;
363 }
364
365 ret = max14577_muic_set_path(info, path, attached);
366 if (ret < 0)
367 return ret;
368
369 extcon_set_cable_state(info->edev, cable_name, attached);
370
371 return 0;
372}
373
374static int max14577_muic_adc_handler(struct max14577_muic_info *info)
375{
376 int cable_type;
377 bool attached;
378 int ret = 0;
379
380 /* Check accessory state which is either detached or attached */
381 cable_type = max14577_muic_get_cable_type(info,
382 MAX14577_CABLE_GROUP_ADC, &attached);
383
384 dev_dbg(info->dev,
385 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
386 attached ? "attached" : "detached", cable_type,
387 info->prev_cable_type);
388
389 switch (cable_type) {
390 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:
391 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:
392 case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:
393 /* JIG */
394 ret = max14577_muic_jig_handler(info, cable_type, attached);
395 if (ret < 0)
396 return ret;
397 break;
398 case MAX14577_MUIC_ADC_GROUND:
399 case MAX14577_MUIC_ADC_SEND_END_BUTTON:
400 case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON:
401 case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON:
402 case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON:
403 case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON:
404 case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON:
405 case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON:
406 case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON:
407 case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON:
408 case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON:
409 case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON:
410 case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON:
411 case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON:
412 case MAX14577_MUIC_ADC_RESERVED_ACC_1:
413 case MAX14577_MUIC_ADC_RESERVED_ACC_2:
414 case MAX14577_MUIC_ADC_RESERVED_ACC_3:
415 case MAX14577_MUIC_ADC_RESERVED_ACC_4:
416 case MAX14577_MUIC_ADC_RESERVED_ACC_5:
417 case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2:
418 case MAX14577_MUIC_ADC_PHONE_POWERED_DEV:
419 case MAX14577_MUIC_ADC_TTY_CONVERTER:
420 case MAX14577_MUIC_ADC_UART_CABLE:
421 case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG:
422 case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD:
423 case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG:
424 case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON:
425 case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1:
426 /*
427 * This accessory isn't used in general case if it is specially
428 * needed to detect additional accessory, should implement
429 * proper operation when this accessory is attached/detached.
430 */
431 dev_info(info->dev,
432 "accessory is %s but it isn't used (adc:0x%x)\n",
433 attached ? "attached" : "detached", cable_type);
434 return -EAGAIN;
435 default:
436 dev_err(info->dev,
437 "failed to detect %s accessory (adc:0x%x)\n",
438 attached ? "attached" : "detached", cable_type);
439 return -EINVAL;
440 }
441
442 return 0;
443}
444
445static int max14577_muic_chg_handler(struct max14577_muic_info *info)
446{
447 int chg_type;
448 bool attached;
449 int ret = 0;
450
451 chg_type = max14577_muic_get_cable_type(info,
452 MAX14577_CABLE_GROUP_CHG, &attached);
453
454 dev_dbg(info->dev,
455 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
456 attached ? "attached" : "detached",
457 chg_type, info->prev_chg_type);
458
459 switch (chg_type) {
460 case MAX14577_CHARGER_TYPE_USB:
461 /* PATH:AP_USB */
462 ret = max14577_muic_set_path(info, info->path_usb, attached);
463 if (ret < 0)
464 return ret;
465
466 extcon_set_cable_state(info->edev, "USB", attached);
467 break;
468 case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
469 extcon_set_cable_state(info->edev, "TA", attached);
470 break;
471 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
472 extcon_set_cable_state(info->edev,
473 "Charge-downstream", attached);
474 break;
475 case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
476 extcon_set_cable_state(info->edev, "Slow-charger", attached);
477 break;
478 case MAX14577_CHARGER_TYPE_SPECIAL_1A:
479 extcon_set_cable_state(info->edev, "Fast-charger", attached);
480 break;
481 case MAX14577_CHARGER_TYPE_NONE:
482 case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
483 break;
484 default:
485 dev_err(info->dev,
486 "failed to detect %s accessory (chg_type:0x%x)\n",
487 attached ? "attached" : "detached", chg_type);
488 return -EINVAL;
489 }
490
491 return 0;
492}
493
494static void max14577_muic_irq_work(struct work_struct *work)
495{
496 struct max14577_muic_info *info = container_of(work,
497 struct max14577_muic_info, irq_work);
498 int ret = 0;
499
500 if (!info->edev)
501 return;
502
503 mutex_lock(&info->mutex);
504
505 ret = max14577_bulk_read(info->max14577->regmap,
506 MAX14577_MUIC_REG_STATUS1, info->status, 2);
507 if (ret) {
508 dev_err(info->dev, "failed to read MUIC register\n");
509 mutex_unlock(&info->mutex);
510 return;
511 }
512
513 if (info->irq_adc) {
514 ret = max14577_muic_adc_handler(info);
515 info->irq_adc = false;
516 }
517 if (info->irq_chg) {
518 ret = max14577_muic_chg_handler(info);
519 info->irq_chg = false;
520 }
521
522 if (ret < 0)
523 dev_err(info->dev, "failed to handle MUIC interrupt\n");
524
525 mutex_unlock(&info->mutex);
526
527 return;
528}
529
530static irqreturn_t max14577_muic_irq_handler(int irq, void *data)
531{
532 struct max14577_muic_info *info = data;
533 int i, irq_type = -1;
534
535 /*
536 * We may be called multiple times for different nested IRQ-s.
537 * Including changes in INT1_ADC and INT2_CGHTYP at once.
538 * However we only need to know whether it was ADC, charger
539 * or both interrupts so decode IRQ and turn on proper flags.
540 */
541 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
542 if (irq == muic_irqs[i].virq)
543 irq_type = muic_irqs[i].irq;
544
545 switch (irq_type) {
546 case MAX14577_IRQ_INT1_ADC:
547 case MAX14577_IRQ_INT1_ADCLOW:
548 case MAX14577_IRQ_INT1_ADCERR:
549 /* Handle all of accessory except for
550 type of charger accessory */
551 info->irq_adc = true;
552 break;
553 case MAX14577_IRQ_INT2_CHGTYP:
554 case MAX14577_IRQ_INT2_CHGDETRUN:
555 case MAX14577_IRQ_INT2_DCDTMR:
556 case MAX14577_IRQ_INT2_DBCHG:
557 case MAX14577_IRQ_INT2_VBVOLT:
558 /* Handle charger accessory */
559 info->irq_chg = true;
560 break;
561 default:
562 dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n",
563 irq_type);
564 return IRQ_HANDLED;
565 }
566 schedule_work(&info->irq_work);
567
568 return IRQ_HANDLED;
569}
570
571static int max14577_muic_detect_accessory(struct max14577_muic_info *info)
572{
573 int ret = 0;
574 int adc;
575 int chg_type;
576 bool attached;
577
578 mutex_lock(&info->mutex);
579
580 /* Read STATUSx register to detect accessory */
581 ret = max14577_bulk_read(info->max14577->regmap,
582 MAX14577_MUIC_REG_STATUS1, info->status, 2);
583 if (ret) {
584 dev_err(info->dev, "failed to read MUIC register\n");
585 mutex_unlock(&info->mutex);
586 return ret;
587 }
588
589 adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
590 &attached);
591 if (attached && adc != MAX14577_MUIC_ADC_OPEN) {
592 ret = max14577_muic_adc_handler(info);
593 if (ret < 0) {
594 dev_err(info->dev, "Cannot detect accessory\n");
595 mutex_unlock(&info->mutex);
596 return ret;
597 }
598 }
599
600 chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG,
601 &attached);
602 if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) {
603 ret = max14577_muic_chg_handler(info);
604 if (ret < 0) {
605 dev_err(info->dev, "Cannot detect charger accessory\n");
606 mutex_unlock(&info->mutex);
607 return ret;
608 }
609 }
610
611 mutex_unlock(&info->mutex);
612
613 return 0;
614}
615
616static void max14577_muic_detect_cable_wq(struct work_struct *work)
617{
618 struct max14577_muic_info *info = container_of(to_delayed_work(work),
619 struct max14577_muic_info, wq_detcable);
620
621 max14577_muic_detect_accessory(info);
622}
623
624static int max14577_muic_probe(struct platform_device *pdev)
625{
626 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
627 struct max14577_muic_info *info;
628 int delay_jiffies;
629 int ret;
630 int i;
631 u8 id;
632
633 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
634 if (!info) {
635 dev_err(&pdev->dev, "failed to allocate memory\n");
636 return -ENOMEM;
637 }
638 info->dev = &pdev->dev;
639 info->max14577 = max14577;
640
641 platform_set_drvdata(pdev, info);
642 mutex_init(&info->mutex);
643
644 INIT_WORK(&info->irq_work, max14577_muic_irq_work);
645
646 /* Support irq domain for max14577 MUIC device */
647 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
648 struct max14577_muic_irq *muic_irq = &muic_irqs[i];
649 unsigned int virq = 0;
650
651 virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq);
652 if (!virq)
653 return -EINVAL;
654 muic_irq->virq = virq;
655
656 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
657 max14577_muic_irq_handler,
658 IRQF_NO_SUSPEND,
659 muic_irq->name, info);
660 if (ret) {
661 dev_err(&pdev->dev,
662 "failed: irq request (IRQ: %d,"
663 " error :%d)\n",
664 muic_irq->irq, ret);
665 return ret;
666 }
667 }
668
669 /* Initialize extcon device */
670 info->edev = devm_kzalloc(&pdev->dev, sizeof(*info->edev), GFP_KERNEL);
671 if (!info->edev) {
672 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
673 return -ENOMEM;
674 }
Chanwoo Choi4005da52014-04-14 11:17:12 +0200675
676 info->edev->name = dev_name(&pdev->dev);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100677 info->edev->supported_cable = max14577_extcon_cable;
678 ret = extcon_dev_register(info->edev);
679 if (ret) {
680 dev_err(&pdev->dev, "failed to register extcon device\n");
681 return ret;
682 }
683
684 /* Default h/w line path */
685 info->path_usb = CTRL1_SW_USB;
686 info->path_uart = CTRL1_SW_UART;
687 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
688
689 /* Set initial path for UART */
690 max14577_muic_set_path(info, info->path_uart, true);
691
692 /* Check revision number of MUIC device*/
693 ret = max14577_read_reg(info->max14577->regmap,
694 MAX14577_REG_DEVICEID, &id);
695 if (ret < 0) {
696 dev_err(&pdev->dev, "failed to read revision number\n");
697 goto err_extcon;
698 }
699 dev_info(info->dev, "device ID : 0x%x\n", id);
700
701 /* Set ADC debounce time */
702 max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
703
704 /*
705 * Detect accessory after completing the initialization of platform
706 *
707 * - Use delayed workqueue to detect cable state and then
708 * notify cable state to notifiee/platform through uevent.
709 * After completing the booting of platform, the extcon provider
710 * driver should notify cable state to upper layer.
711 */
712 INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
713 ret = queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
714 delay_jiffies);
715 if (ret < 0) {
716 dev_err(&pdev->dev,
717 "failed to schedule delayed work for cable detect\n");
718 goto err_extcon;
719 }
720
721 return ret;
722
723err_extcon:
724 extcon_dev_unregister(info->edev);
725 return ret;
726}
727
728static int max14577_muic_remove(struct platform_device *pdev)
729{
730 struct max14577_muic_info *info = platform_get_drvdata(pdev);
731
732 cancel_work_sync(&info->irq_work);
733 extcon_dev_unregister(info->edev);
734
735 return 0;
736}
737
738static struct platform_driver max14577_muic_driver = {
739 .driver = {
Chanwoo Choi4005da52014-04-14 11:17:12 +0200740 .name = "max14577-muic",
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100741 .owner = THIS_MODULE,
742 },
743 .probe = max14577_muic_probe,
744 .remove = max14577_muic_remove,
745};
746
747module_platform_driver(max14577_muic_driver);
748
749MODULE_DESCRIPTION("MAXIM 14577 Extcon driver");
750MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
751MODULE_LICENSE("GPL");
752MODULE_ALIAS("platform:extcon-max14577");