Linux Audio

Check our new training course

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