]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/extcon/extcon-max77843.c
KVM: SVM: Move spec control call after restore of GS
[mirror_ubuntu-artful-kernel.git] / drivers / extcon / extcon-max77843.c
1 /*
2 * extcon-max77843.c - Maxim MAX77843 extcon driver to support
3 * MUIC(Micro USB Interface Controller)
4 *
5 * Copyright (C) 2015 Samsung Electronics
6 * Author: Jaewon Kim <jaewon02.kim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14 #include <linux/extcon.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/max77693-common.h>
19 #include <linux/mfd/max77843-private.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/workqueue.h>
23
24 #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */
25
26 enum max77843_muic_status {
27 MAX77843_MUIC_STATUS1 = 0,
28 MAX77843_MUIC_STATUS2,
29 MAX77843_MUIC_STATUS3,
30
31 MAX77843_MUIC_STATUS_NUM,
32 };
33
34 struct max77843_muic_info {
35 struct device *dev;
36 struct max77693_dev *max77843;
37 struct extcon_dev *edev;
38
39 struct mutex mutex;
40 struct work_struct irq_work;
41 struct delayed_work wq_detcable;
42
43 u8 status[MAX77843_MUIC_STATUS_NUM];
44 int prev_cable_type;
45 int prev_chg_type;
46 int prev_gnd_type;
47
48 bool irq_adc;
49 bool irq_chg;
50 };
51
52 enum max77843_muic_cable_group {
53 MAX77843_CABLE_GROUP_ADC = 0,
54 MAX77843_CABLE_GROUP_ADC_GND,
55 MAX77843_CABLE_GROUP_CHG,
56 };
57
58 enum max77843_muic_adc_debounce_time {
59 MAX77843_DEBOUNCE_TIME_5MS = 0,
60 MAX77843_DEBOUNCE_TIME_10MS,
61 MAX77843_DEBOUNCE_TIME_25MS,
62 MAX77843_DEBOUNCE_TIME_38_62MS,
63 };
64
65 /* Define accessory cable type */
66 enum max77843_muic_accessory_type {
67 MAX77843_MUIC_ADC_GROUND = 0,
68 MAX77843_MUIC_ADC_SEND_END_BUTTON,
69 MAX77843_MUIC_ADC_REMOTE_S1_BUTTON,
70 MAX77843_MUIC_ADC_REMOTE_S2_BUTTON,
71 MAX77843_MUIC_ADC_REMOTE_S3_BUTTON,
72 MAX77843_MUIC_ADC_REMOTE_S4_BUTTON,
73 MAX77843_MUIC_ADC_REMOTE_S5_BUTTON,
74 MAX77843_MUIC_ADC_REMOTE_S6_BUTTON,
75 MAX77843_MUIC_ADC_REMOTE_S7_BUTTON,
76 MAX77843_MUIC_ADC_REMOTE_S8_BUTTON,
77 MAX77843_MUIC_ADC_REMOTE_S9_BUTTON,
78 MAX77843_MUIC_ADC_REMOTE_S10_BUTTON,
79 MAX77843_MUIC_ADC_REMOTE_S11_BUTTON,
80 MAX77843_MUIC_ADC_REMOTE_S12_BUTTON,
81 MAX77843_MUIC_ADC_RESERVED_ACC_1,
82 MAX77843_MUIC_ADC_RESERVED_ACC_2,
83 MAX77843_MUIC_ADC_RESERVED_ACC_3,
84 MAX77843_MUIC_ADC_RESERVED_ACC_4,
85 MAX77843_MUIC_ADC_RESERVED_ACC_5,
86 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2,
87 MAX77843_MUIC_ADC_PHONE_POWERED_DEV,
88 MAX77843_MUIC_ADC_TTY_CONVERTER,
89 MAX77843_MUIC_ADC_UART_CABLE,
90 MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG,
91 MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF,
92 MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON,
93 MAX77843_MUIC_ADC_AV_CABLE_NOLOAD,
94 MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG,
95 MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF,
96 MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON,
97 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1,
98 MAX77843_MUIC_ADC_OPEN,
99
100 /*
101 * The below accessories should check
102 * not only ADC value but also ADC1K and VBVolt value.
103 */
104 /* Offset|ADC1K|VBVolt| */
105 MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */
106 MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */
107 MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */
108 MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */
109 };
110
111 /* Define charger cable type */
112 enum max77843_muic_charger_type {
113 MAX77843_MUIC_CHG_NONE = 0,
114 MAX77843_MUIC_CHG_USB,
115 MAX77843_MUIC_CHG_DOWNSTREAM,
116 MAX77843_MUIC_CHG_DEDICATED,
117 MAX77843_MUIC_CHG_SPECIAL_500MA,
118 MAX77843_MUIC_CHG_SPECIAL_1A,
119 MAX77843_MUIC_CHG_SPECIAL_BIAS,
120 MAX77843_MUIC_CHG_RESERVED,
121 MAX77843_MUIC_CHG_GND,
122 };
123
124 static const unsigned int max77843_extcon_cable[] = {
125 EXTCON_USB,
126 EXTCON_USB_HOST,
127 EXTCON_CHG_USB_SDP,
128 EXTCON_CHG_USB_DCP,
129 EXTCON_CHG_USB_CDP,
130 EXTCON_CHG_USB_FAST,
131 EXTCON_CHG_USB_SLOW,
132 EXTCON_DISP_MHL,
133 EXTCON_JIG,
134 EXTCON_NONE,
135 };
136
137 struct max77843_muic_irq {
138 unsigned int irq;
139 const char *name;
140 unsigned int virq;
141 };
142
143 static struct max77843_muic_irq max77843_muic_irqs[] = {
144 { MAX77843_MUIC_IRQ_INT1_ADC, "MUIC-ADC" },
145 { MAX77843_MUIC_IRQ_INT1_ADCERROR, "MUIC-ADC_ERROR" },
146 { MAX77843_MUIC_IRQ_INT1_ADC1K, "MUIC-ADC1K" },
147 { MAX77843_MUIC_IRQ_INT2_CHGTYP, "MUIC-CHGTYP" },
148 { MAX77843_MUIC_IRQ_INT2_CHGDETRUN, "MUIC-CHGDETRUN" },
149 { MAX77843_MUIC_IRQ_INT2_DCDTMR, "MUIC-DCDTMR" },
150 { MAX77843_MUIC_IRQ_INT2_DXOVP, "MUIC-DXOVP" },
151 { MAX77843_MUIC_IRQ_INT2_VBVOLT, "MUIC-VBVOLT" },
152 { MAX77843_MUIC_IRQ_INT3_VBADC, "MUIC-VBADC" },
153 { MAX77843_MUIC_IRQ_INT3_VDNMON, "MUIC-VDNMON" },
154 { MAX77843_MUIC_IRQ_INT3_DNRES, "MUIC-DNRES" },
155 { MAX77843_MUIC_IRQ_INT3_MPNACK, "MUIC-MPNACK"},
156 { MAX77843_MUIC_IRQ_INT3_MRXBUFOW, "MUIC-MRXBUFOW"},
157 { MAX77843_MUIC_IRQ_INT3_MRXTRF, "MUIC-MRXTRF"},
158 { MAX77843_MUIC_IRQ_INT3_MRXPERR, "MUIC-MRXPERR"},
159 { MAX77843_MUIC_IRQ_INT3_MRXRDY, "MUIC-MRXRDY"},
160 };
161
162 static const struct regmap_config max77843_muic_regmap_config = {
163 .reg_bits = 8,
164 .val_bits = 8,
165 .max_register = MAX77843_MUIC_REG_END,
166 };
167
168 static const struct regmap_irq max77843_muic_irq[] = {
169 /* INT1 interrupt */
170 { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, },
171 { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, },
172 { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, },
173
174 /* INT2 interrupt */
175 { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, },
176 { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, },
177 { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, },
178 { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, },
179 { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, },
180
181 /* INT3 interrupt */
182 { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, },
183 { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, },
184 { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, },
185 { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, },
186 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, },
187 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, },
188 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, },
189 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, },
190 };
191
192 static const struct regmap_irq_chip max77843_muic_irq_chip = {
193 .name = "max77843-muic",
194 .status_base = MAX77843_MUIC_REG_INT1,
195 .mask_base = MAX77843_MUIC_REG_INTMASK1,
196 .mask_invert = true,
197 .num_regs = 3,
198 .irqs = max77843_muic_irq,
199 .num_irqs = ARRAY_SIZE(max77843_muic_irq),
200 };
201
202 static int max77843_muic_set_path(struct max77843_muic_info *info,
203 u8 val, bool attached)
204 {
205 struct max77693_dev *max77843 = info->max77843;
206 int ret = 0;
207 unsigned int ctrl1, ctrl2;
208
209 if (attached)
210 ctrl1 = val;
211 else
212 ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN;
213
214 ret = regmap_update_bits(max77843->regmap_muic,
215 MAX77843_MUIC_REG_CONTROL1,
216 MAX77843_MUIC_CONTROL1_COM_SW, ctrl1);
217 if (ret < 0) {
218 dev_err(info->dev, "Cannot switch MUIC port\n");
219 return ret;
220 }
221
222 if (attached)
223 ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK;
224 else
225 ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK;
226
227 ret = regmap_update_bits(max77843->regmap_muic,
228 MAX77843_MUIC_REG_CONTROL2,
229 MAX77843_MUIC_CONTROL2_LOWPWR_MASK |
230 MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2);
231 if (ret < 0) {
232 dev_err(info->dev, "Cannot update lowpower mode\n");
233 return ret;
234 }
235
236 dev_dbg(info->dev,
237 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
238 ctrl1, ctrl2, attached ? "attached" : "detached");
239
240 return 0;
241 }
242
243 static int max77843_muic_get_cable_type(struct max77843_muic_info *info,
244 enum max77843_muic_cable_group group, bool *attached)
245 {
246 int adc, chg_type, cable_type, gnd_type;
247
248 adc = info->status[MAX77843_MUIC_STATUS1] &
249 MAX77843_MUIC_STATUS1_ADC_MASK;
250 adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT;
251
252 switch (group) {
253 case MAX77843_CABLE_GROUP_ADC:
254 if (adc == MAX77843_MUIC_ADC_OPEN) {
255 *attached = false;
256 cable_type = info->prev_cable_type;
257 info->prev_cable_type = MAX77843_MUIC_ADC_OPEN;
258 } else {
259 *attached = true;
260 cable_type = info->prev_cable_type = adc;
261 }
262 break;
263 case MAX77843_CABLE_GROUP_CHG:
264 chg_type = info->status[MAX77843_MUIC_STATUS2] &
265 MAX77843_MUIC_STATUS2_CHGTYP_MASK;
266
267 /* Check GROUND accessory with charger cable */
268 if (adc == MAX77843_MUIC_ADC_GROUND) {
269 if (chg_type == MAX77843_MUIC_CHG_NONE) {
270 /*
271 * The following state when charger cable is
272 * disconnected but the GROUND accessory still
273 * connected.
274 */
275 *attached = false;
276 cable_type = info->prev_chg_type;
277 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
278 } else {
279
280 /*
281 * The following state when charger cable is
282 * connected on the GROUND accessory.
283 */
284 *attached = true;
285 cable_type = MAX77843_MUIC_CHG_GND;
286 info->prev_chg_type = MAX77843_MUIC_CHG_GND;
287 }
288 break;
289 }
290
291 if (chg_type == MAX77843_MUIC_CHG_NONE) {
292 *attached = false;
293 cable_type = info->prev_chg_type;
294 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
295 } else {
296 *attached = true;
297 cable_type = info->prev_chg_type = chg_type;
298 }
299 break;
300 case MAX77843_CABLE_GROUP_ADC_GND:
301 if (adc == MAX77843_MUIC_ADC_OPEN) {
302 *attached = false;
303 cable_type = info->prev_gnd_type;
304 info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN;
305 } else {
306 *attached = true;
307
308 /*
309 * Offset|ADC1K|VBVolt|
310 * 0x1| 0| 0| USB-HOST
311 * 0x1| 0| 1| USB-HOST with VB
312 * 0x1| 1| 0| MHL
313 * 0x1| 1| 1| MHL with VB
314 */
315 /* Get ADC1K register bit */
316 gnd_type = (info->status[MAX77843_MUIC_STATUS1] &
317 MAX77843_MUIC_STATUS1_ADC1K_MASK);
318
319 /* Get VBVolt register bit */
320 gnd_type |= (info->status[MAX77843_MUIC_STATUS2] &
321 MAX77843_MUIC_STATUS2_VBVOLT_MASK);
322 gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT;
323
324 /* Offset of GND cable */
325 gnd_type |= MAX77843_MUIC_GND_USB_HOST;
326 cable_type = info->prev_gnd_type = gnd_type;
327 }
328 break;
329 default:
330 dev_err(info->dev, "Unknown cable group (%d)\n", group);
331 cable_type = -EINVAL;
332 break;
333 }
334
335 return cable_type;
336 }
337
338 static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info)
339 {
340 int ret, gnd_cable_type;
341 bool attached;
342
343 gnd_cable_type = max77843_muic_get_cable_type(info,
344 MAX77843_CABLE_GROUP_ADC_GND, &attached);
345 dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n",
346 attached ? "attached" : "detached", gnd_cable_type);
347
348 switch (gnd_cable_type) {
349 case MAX77843_MUIC_GND_USB_HOST:
350 case MAX77843_MUIC_GND_USB_HOST_VB:
351 ret = max77843_muic_set_path(info,
352 MAX77843_MUIC_CONTROL1_SW_USB,
353 attached);
354 if (ret < 0)
355 return ret;
356
357 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
358 break;
359 case MAX77843_MUIC_GND_MHL_VB:
360 case MAX77843_MUIC_GND_MHL:
361 ret = max77843_muic_set_path(info,
362 MAX77843_MUIC_CONTROL1_SW_OPEN,
363 attached);
364 if (ret < 0)
365 return ret;
366
367 extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
368 break;
369 default:
370 dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
371 attached ? "attached" : "detached", gnd_cable_type);
372 return -EINVAL;
373 }
374
375 return 0;
376 }
377
378 static int max77843_muic_jig_handler(struct max77843_muic_info *info,
379 int cable_type, bool attached)
380 {
381 int ret;
382 u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN;
383
384 dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
385 attached ? "attached" : "detached", cable_type);
386
387 switch (cable_type) {
388 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
389 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
390 path = MAX77843_MUIC_CONTROL1_SW_USB;
391 break;
392 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
393 path = MAX77843_MUIC_CONTROL1_SW_UART;
394 break;
395 default:
396 return -EINVAL;
397 }
398
399 ret = max77843_muic_set_path(info, path, attached);
400 if (ret < 0)
401 return ret;
402
403 extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
404
405 return 0;
406 }
407
408 static int max77843_muic_adc_handler(struct max77843_muic_info *info)
409 {
410 int ret, cable_type;
411 bool attached;
412
413 cable_type = max77843_muic_get_cable_type(info,
414 MAX77843_CABLE_GROUP_ADC, &attached);
415
416 dev_dbg(info->dev,
417 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
418 attached ? "attached" : "detached", cable_type,
419 info->prev_cable_type);
420
421 switch (cable_type) {
422 case MAX77843_MUIC_ADC_GROUND:
423 ret = max77843_muic_adc_gnd_handler(info);
424 if (ret < 0)
425 return ret;
426 break;
427 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
428 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
429 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
430 ret = max77843_muic_jig_handler(info, cable_type, attached);
431 if (ret < 0)
432 return ret;
433 break;
434 case MAX77843_MUIC_ADC_SEND_END_BUTTON:
435 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
436 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
437 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
438 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
439 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
440 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
441 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
442 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
443 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
444 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
445 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
446 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
447 case MAX77843_MUIC_ADC_RESERVED_ACC_1:
448 case MAX77843_MUIC_ADC_RESERVED_ACC_2:
449 case MAX77843_MUIC_ADC_RESERVED_ACC_3:
450 case MAX77843_MUIC_ADC_RESERVED_ACC_4:
451 case MAX77843_MUIC_ADC_RESERVED_ACC_5:
452 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
453 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
454 case MAX77843_MUIC_ADC_TTY_CONVERTER:
455 case MAX77843_MUIC_ADC_UART_CABLE:
456 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
457 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
458 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
459 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
460 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
461 case MAX77843_MUIC_ADC_OPEN:
462 dev_err(info->dev,
463 "accessory is %s but it isn't used (adc:0x%x)\n",
464 attached ? "attached" : "detached", cable_type);
465 return -EAGAIN;
466 default:
467 dev_err(info->dev,
468 "failed to detect %s accessory (adc:0x%x)\n",
469 attached ? "attached" : "detached", cable_type);
470 return -EINVAL;
471 }
472
473 return 0;
474 }
475
476 static int max77843_muic_chg_handler(struct max77843_muic_info *info)
477 {
478 int ret, chg_type, gnd_type;
479 bool attached;
480
481 chg_type = max77843_muic_get_cable_type(info,
482 MAX77843_CABLE_GROUP_CHG, &attached);
483
484 dev_dbg(info->dev,
485 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
486 attached ? "attached" : "detached",
487 chg_type, info->prev_chg_type);
488
489 switch (chg_type) {
490 case MAX77843_MUIC_CHG_USB:
491 ret = max77843_muic_set_path(info,
492 MAX77843_MUIC_CONTROL1_SW_USB,
493 attached);
494 if (ret < 0)
495 return ret;
496
497 extcon_set_state_sync(info->edev, EXTCON_USB, attached);
498 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
499 attached);
500 break;
501 case MAX77843_MUIC_CHG_DOWNSTREAM:
502 ret = max77843_muic_set_path(info,
503 MAX77843_MUIC_CONTROL1_SW_OPEN,
504 attached);
505 if (ret < 0)
506 return ret;
507
508 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
509 attached);
510 break;
511 case MAX77843_MUIC_CHG_DEDICATED:
512 ret = max77843_muic_set_path(info,
513 MAX77843_MUIC_CONTROL1_SW_OPEN,
514 attached);
515 if (ret < 0)
516 return ret;
517
518 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
519 attached);
520 break;
521 case MAX77843_MUIC_CHG_SPECIAL_500MA:
522 ret = max77843_muic_set_path(info,
523 MAX77843_MUIC_CONTROL1_SW_OPEN,
524 attached);
525 if (ret < 0)
526 return ret;
527
528 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
529 attached);
530 break;
531 case MAX77843_MUIC_CHG_SPECIAL_1A:
532 ret = max77843_muic_set_path(info,
533 MAX77843_MUIC_CONTROL1_SW_OPEN,
534 attached);
535 if (ret < 0)
536 return ret;
537
538 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
539 attached);
540 break;
541 case MAX77843_MUIC_CHG_GND:
542 gnd_type = max77843_muic_get_cable_type(info,
543 MAX77843_CABLE_GROUP_ADC_GND, &attached);
544
545 /* Charger cable on MHL accessory is attach or detach */
546 if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
547 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
548 true);
549 else if (gnd_type == MAX77843_MUIC_GND_MHL)
550 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
551 false);
552 break;
553 case MAX77843_MUIC_CHG_NONE:
554 break;
555 default:
556 dev_err(info->dev,
557 "failed to detect %s accessory (chg_type:0x%x)\n",
558 attached ? "attached" : "detached", chg_type);
559
560 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN,
561 attached);
562 return -EINVAL;
563 }
564
565 return 0;
566 }
567
568 static void max77843_muic_irq_work(struct work_struct *work)
569 {
570 struct max77843_muic_info *info = container_of(work,
571 struct max77843_muic_info, irq_work);
572 struct max77693_dev *max77843 = info->max77843;
573 int ret = 0;
574
575 mutex_lock(&info->mutex);
576
577 ret = regmap_bulk_read(max77843->regmap_muic,
578 MAX77843_MUIC_REG_STATUS1, info->status,
579 MAX77843_MUIC_STATUS_NUM);
580 if (ret) {
581 dev_err(info->dev, "Cannot read STATUS registers\n");
582 mutex_unlock(&info->mutex);
583 return;
584 }
585
586 if (info->irq_adc) {
587 ret = max77843_muic_adc_handler(info);
588 if (ret)
589 dev_err(info->dev, "Unknown cable type\n");
590 info->irq_adc = false;
591 }
592
593 if (info->irq_chg) {
594 ret = max77843_muic_chg_handler(info);
595 if (ret)
596 dev_err(info->dev, "Unknown charger type\n");
597 info->irq_chg = false;
598 }
599
600 mutex_unlock(&info->mutex);
601 }
602
603 static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
604 {
605 struct max77843_muic_info *info = data;
606 int i, irq_type = -1;
607
608 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
609 if (irq == max77843_muic_irqs[i].virq)
610 irq_type = max77843_muic_irqs[i].irq;
611
612 switch (irq_type) {
613 case MAX77843_MUIC_IRQ_INT1_ADC:
614 case MAX77843_MUIC_IRQ_INT1_ADCERROR:
615 case MAX77843_MUIC_IRQ_INT1_ADC1K:
616 info->irq_adc = true;
617 break;
618 case MAX77843_MUIC_IRQ_INT2_CHGTYP:
619 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
620 case MAX77843_MUIC_IRQ_INT2_DCDTMR:
621 case MAX77843_MUIC_IRQ_INT2_DXOVP:
622 case MAX77843_MUIC_IRQ_INT2_VBVOLT:
623 info->irq_chg = true;
624 break;
625 case MAX77843_MUIC_IRQ_INT3_VBADC:
626 case MAX77843_MUIC_IRQ_INT3_VDNMON:
627 case MAX77843_MUIC_IRQ_INT3_DNRES:
628 case MAX77843_MUIC_IRQ_INT3_MPNACK:
629 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
630 case MAX77843_MUIC_IRQ_INT3_MRXTRF:
631 case MAX77843_MUIC_IRQ_INT3_MRXPERR:
632 case MAX77843_MUIC_IRQ_INT3_MRXRDY:
633 break;
634 default:
635 dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
636 break;
637 }
638
639 schedule_work(&info->irq_work);
640
641 return IRQ_HANDLED;
642 }
643
644 static void max77843_muic_detect_cable_wq(struct work_struct *work)
645 {
646 struct max77843_muic_info *info = container_of(to_delayed_work(work),
647 struct max77843_muic_info, wq_detcable);
648 struct max77693_dev *max77843 = info->max77843;
649 int chg_type, adc, ret;
650 bool attached;
651
652 mutex_lock(&info->mutex);
653
654 ret = regmap_bulk_read(max77843->regmap_muic,
655 MAX77843_MUIC_REG_STATUS1, info->status,
656 MAX77843_MUIC_STATUS_NUM);
657 if (ret) {
658 dev_err(info->dev, "Cannot read STATUS registers\n");
659 goto err_cable_wq;
660 }
661
662 adc = max77843_muic_get_cable_type(info,
663 MAX77843_CABLE_GROUP_ADC, &attached);
664 if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
665 ret = max77843_muic_adc_handler(info);
666 if (ret < 0) {
667 dev_err(info->dev, "Cannot detect accessory\n");
668 goto err_cable_wq;
669 }
670 }
671
672 chg_type = max77843_muic_get_cable_type(info,
673 MAX77843_CABLE_GROUP_CHG, &attached);
674 if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
675 ret = max77843_muic_chg_handler(info);
676 if (ret < 0) {
677 dev_err(info->dev, "Cannot detect charger accessory\n");
678 goto err_cable_wq;
679 }
680 }
681
682 err_cable_wq:
683 mutex_unlock(&info->mutex);
684 }
685
686 static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
687 enum max77843_muic_adc_debounce_time time)
688 {
689 struct max77693_dev *max77843 = info->max77843;
690 int ret;
691
692 switch (time) {
693 case MAX77843_DEBOUNCE_TIME_5MS:
694 case MAX77843_DEBOUNCE_TIME_10MS:
695 case MAX77843_DEBOUNCE_TIME_25MS:
696 case MAX77843_DEBOUNCE_TIME_38_62MS:
697 ret = regmap_update_bits(max77843->regmap_muic,
698 MAX77843_MUIC_REG_CONTROL4,
699 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
700 time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT);
701 if (ret < 0) {
702 dev_err(info->dev, "Cannot write MUIC regmap\n");
703 return ret;
704 }
705 break;
706 default:
707 dev_err(info->dev, "Invalid ADC debounce time\n");
708 return -EINVAL;
709 }
710
711 return 0;
712 }
713
714 static int max77843_init_muic_regmap(struct max77693_dev *max77843)
715 {
716 int ret;
717
718 max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter,
719 I2C_ADDR_MUIC);
720 if (!max77843->i2c_muic) {
721 dev_err(&max77843->i2c->dev,
722 "Cannot allocate I2C device for MUIC\n");
723 return -ENOMEM;
724 }
725
726 i2c_set_clientdata(max77843->i2c_muic, max77843);
727
728 max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
729 &max77843_muic_regmap_config);
730 if (IS_ERR(max77843->regmap_muic)) {
731 ret = PTR_ERR(max77843->regmap_muic);
732 goto err_muic_i2c;
733 }
734
735 ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
736 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
737 0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
738 if (ret < 0) {
739 dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
740 goto err_muic_i2c;
741 }
742
743 return 0;
744
745 err_muic_i2c:
746 i2c_unregister_device(max77843->i2c_muic);
747
748 return ret;
749 }
750
751 static int max77843_muic_probe(struct platform_device *pdev)
752 {
753 struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent);
754 struct max77843_muic_info *info;
755 unsigned int id;
756 int i, ret;
757
758 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
759 if (!info)
760 return -ENOMEM;
761
762 info->dev = &pdev->dev;
763 info->max77843 = max77843;
764
765 platform_set_drvdata(pdev, info);
766 mutex_init(&info->mutex);
767
768 /* Initialize i2c and regmap */
769 ret = max77843_init_muic_regmap(max77843);
770 if (ret) {
771 dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
772 return ret;
773 }
774
775 /* Turn off auto detection configuration */
776 ret = regmap_update_bits(max77843->regmap_muic,
777 MAX77843_MUIC_REG_CONTROL4,
778 MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
779 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
780 CONTROL4_AUTO_DISABLE);
781
782 /* Initialize extcon device */
783 info->edev = devm_extcon_dev_allocate(&pdev->dev,
784 max77843_extcon_cable);
785 if (IS_ERR(info->edev)) {
786 dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
787 ret = -ENODEV;
788 goto err_muic_irq;
789 }
790
791 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
792 if (ret) {
793 dev_err(&pdev->dev, "Failed to register extcon device\n");
794 goto err_muic_irq;
795 }
796
797 /* Set ADC debounce time */
798 max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
799
800 /* Set initial path for UART */
801 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, true);
802
803 /* Check revision number of MUIC device */
804 ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
805 if (ret < 0) {
806 dev_err(&pdev->dev, "Failed to read revision number\n");
807 goto err_muic_irq;
808 }
809 dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
810
811 /* Support virtual irq domain for max77843 MUIC device */
812 INIT_WORK(&info->irq_work, max77843_muic_irq_work);
813
814 /* Clear IRQ bits before request IRQs */
815 ret = regmap_bulk_read(max77843->regmap_muic,
816 MAX77843_MUIC_REG_INT1, info->status,
817 MAX77843_MUIC_STATUS_NUM);
818 if (ret) {
819 dev_err(&pdev->dev, "Failed to Clear IRQ bits\n");
820 goto err_muic_irq;
821 }
822
823 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
824 struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
825 int virq = 0;
826
827 virq = regmap_irq_get_virq(max77843->irq_data_muic,
828 muic_irq->irq);
829 if (virq <= 0) {
830 ret = -EINVAL;
831 goto err_muic_irq;
832 }
833 muic_irq->virq = virq;
834
835 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
836 max77843_muic_irq_handler, IRQF_NO_SUSPEND,
837 muic_irq->name, info);
838 if (ret) {
839 dev_err(&pdev->dev,
840 "Failed to request irq (IRQ: %d, error: %d)\n",
841 muic_irq->irq, ret);
842 goto err_muic_irq;
843 }
844 }
845
846 /* Detect accessory after completing the initialization of platform */
847 INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
848 queue_delayed_work(system_power_efficient_wq,
849 &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
850
851 return 0;
852
853 err_muic_irq:
854 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
855 i2c_unregister_device(max77843->i2c_muic);
856
857 return ret;
858 }
859
860 static int max77843_muic_remove(struct platform_device *pdev)
861 {
862 struct max77843_muic_info *info = platform_get_drvdata(pdev);
863 struct max77693_dev *max77843 = info->max77843;
864
865 cancel_work_sync(&info->irq_work);
866 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
867 i2c_unregister_device(max77843->i2c_muic);
868
869 return 0;
870 }
871
872 static const struct platform_device_id max77843_muic_id[] = {
873 { "max77843-muic", },
874 { /* sentinel */ },
875 };
876 MODULE_DEVICE_TABLE(platform, max77843_muic_id);
877
878 static struct platform_driver max77843_muic_driver = {
879 .driver = {
880 .name = "max77843-muic",
881 },
882 .probe = max77843_muic_probe,
883 .remove = max77843_muic_remove,
884 .id_table = max77843_muic_id,
885 };
886
887 static int __init max77843_muic_init(void)
888 {
889 return platform_driver_register(&max77843_muic_driver);
890 }
891 subsys_initcall(max77843_muic_init);
892
893 MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
894 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
895 MODULE_LICENSE("GPL");