Linux Audio

Check our new training course

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