extcon-max8997.c 20.9 KB
Newer Older
1
/*
C
Chanwoo Choi 已提交
2
 * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
3
 *
S
Sachin Kamat 已提交
4
 *  Copyright (C) 2012 Samsung Electronics
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *  Donggeun Kim <dg77.kim@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/mfd/max8997.h>
#include <linux/mfd/max8997-private.h>
C
Chanwoo Choi 已提交
28
#include <linux/extcon.h>
29
#include <linux/irqdomain.h>
C
Chanwoo Choi 已提交
30 31

#define	DEV_NAME			"max8997-muic"
32
#define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */
33

34 35 36 37 38 39 40
enum max8997_muic_adc_debounce_time {
	ADC_DEBOUNCE_TIME_0_5MS = 0,	/* 0.5ms */
	ADC_DEBOUNCE_TIME_10MS,		/* 10ms */
	ADC_DEBOUNCE_TIME_25MS,		/* 25ms */
	ADC_DEBOUNCE_TIME_38_62MS,	/* 38.62ms */
};

41 42 43
struct max8997_muic_irq {
	unsigned int irq;
	const char *name;
44
	unsigned int virq;
45 46 47
};

static struct max8997_muic_irq muic_irqs[] = {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	{ MAX8997_MUICIRQ_ADCError,	"muic-ADCERROR" },
	{ MAX8997_MUICIRQ_ADCLow,	"muic-ADCLOW" },
	{ MAX8997_MUICIRQ_ADC,		"muic-ADC" },
	{ MAX8997_MUICIRQ_VBVolt,	"muic-VBVOLT" },
	{ MAX8997_MUICIRQ_DBChg,	"muic-DBCHG" },
	{ MAX8997_MUICIRQ_DCDTmr,	"muic-DCDTMR" },
	{ MAX8997_MUICIRQ_ChgDetRun,	"muic-CHGDETRUN" },
	{ MAX8997_MUICIRQ_ChgTyp,	"muic-CHGTYP" },
	{ MAX8997_MUICIRQ_OVP,		"muic-OVP" },
};

/* Define supported cable type */
enum max8997_muic_acc_type {
	MAX8997_MUIC_ADC_GROUND = 0x0,
	MAX8997_MUIC_ADC_MHL,			/* MHL*/
	MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
	MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
	MAX8997_MUIC_ADC_RESERVED_ACC_1,
	MAX8997_MUIC_ADC_RESERVED_ACC_2,
	MAX8997_MUIC_ADC_RESERVED_ACC_3,
	MAX8997_MUIC_ADC_RESERVED_ACC_4,
	MAX8997_MUIC_ADC_RESERVED_ACC_5,
	MAX8997_MUIC_ADC_CEA936_AUDIO,
	MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
	MAX8997_MUIC_ADC_TTY_CONVERTER,
	MAX8997_MUIC_ADC_UART_CABLE,
	MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
	MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF,	/* JIG-USB-OFF */
	MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON,	/* JIG-USB-ON */
	MAX8997_MUIC_ADC_AV_CABLE_NOLOAD,	/* DESKDOCK */
	MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
	MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF,	/* JIG-UART */
	MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON,	/* CARDOCK */
	MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
	MAX8997_MUIC_ADC_OPEN,			/* OPEN */
};

enum max8997_muic_cable_group {
	MAX8997_CABLE_GROUP_ADC = 0,
	MAX8997_CABLE_GROUP_ADC_GND,
	MAX8997_CABLE_GROUP_CHG,
	MAX8997_CABLE_GROUP_VBVOLT,
};

enum max8997_muic_usb_type {
	MAX8997_USB_HOST,
	MAX8997_USB_DEVICE,
};

enum max8997_muic_charger_type {
	MAX8997_CHARGER_TYPE_NONE = 0,
	MAX8997_CHARGER_TYPE_USB,
	MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
	MAX8997_CHARGER_TYPE_DEDICATED_CHG,
	MAX8997_CHARGER_TYPE_500MA,
	MAX8997_CHARGER_TYPE_1A,
	MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
115 116 117 118 119
};

struct max8997_muic_info {
	struct device *dev;
	struct i2c_client *muic;
120 121 122 123
	struct extcon_dev *edev;
	int prev_cable_type;
	int prev_chg_type;
	u8 status[2];
124 125 126 127

	int irq;
	struct work_struct irq_work;
	struct mutex mutex;
C
Chanwoo Choi 已提交
128

129 130
	struct max8997_muic_platform_data *muic_pdata;
	enum max8997_muic_charger_type pre_charger_type;
131

132 133 134 135 136 137 138 139
	/*
	 * Use delayed workqueue to detect cable state and then
	 * notify cable state to notifiee/platform through uevent.
	 * After completing the booting of platform, the extcon provider
	 * driver should notify cable state to upper layer.
	 */
	struct delayed_work wq_detcable;

140 141 142 143 144 145
	/*
	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
	 * h/w path of COMP2/COMN1 on CONTROL1 register.
	 */
	int path_usb;
	int path_uart;
C
Chanwoo Choi 已提交
146 147
};

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
enum {
	EXTCON_CABLE_USB = 0,
	EXTCON_CABLE_USB_HOST,
	EXTCON_CABLE_TA,
	EXTCON_CABLE_FAST_CHARGER,
	EXTCON_CABLE_SLOW_CHARGER,
	EXTCON_CABLE_CHARGE_DOWNSTREAM,
	EXTCON_CABLE_MHL,
	EXTCON_CABLE_DOCK_DESK,
	EXTCON_CABLE_DOCK_CARD,
	EXTCON_CABLE_JIG,

	_EXTCON_CABLE_NUM,
};

163
static const char *max8997_extcon_cable[] = {
164 165 166 167 168 169 170 171 172 173
	[EXTCON_CABLE_USB]			= "USB",
	[EXTCON_CABLE_USB_HOST]			= "USB-Host",
	[EXTCON_CABLE_TA]			= "TA",
	[EXTCON_CABLE_FAST_CHARGER]		= "Fast-charger",
	[EXTCON_CABLE_SLOW_CHARGER]		= "Slow-charger",
	[EXTCON_CABLE_CHARGE_DOWNSTREAM]	= "Charge-downstream",
	[EXTCON_CABLE_MHL]			= "MHL",
	[EXTCON_CABLE_DOCK_DESK]		= "Dock-Desk",
	[EXTCON_CABLE_DOCK_CARD]		= "Dock-Card",
	[EXTCON_CABLE_JIG]			= "JIG",
C
Chanwoo Choi 已提交
174 175

	NULL,
176 177
};

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/*
 * max8997_muic_set_debounce_time - Set the debounce time of ADC
 * @info: the instance including private data of max8997 MUIC
 * @time: the debounce time of ADC
 */
static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
		enum max8997_muic_adc_debounce_time time)
{
	int ret;

	switch (time) {
	case ADC_DEBOUNCE_TIME_0_5MS:
	case ADC_DEBOUNCE_TIME_10MS:
	case ADC_DEBOUNCE_TIME_25MS:
	case ADC_DEBOUNCE_TIME_38_62MS:
		ret = max8997_update_reg(info->muic,
					  MAX8997_MUIC_REG_CONTROL3,
					  time << CONTROL3_ADCDBSET_SHIFT,
					  CONTROL3_ADCDBSET_MASK);
		if (ret) {
			dev_err(info->dev, "failed to set ADC debounce time\n");
			return -EAGAIN;
		}
		break;
	default:
		dev_err(info->dev, "invalid ADC debounce time\n");
		return -EINVAL;
	}

	return 0;
};

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/*
 * max8997_muic_set_path - Set hardware line according to attached cable
 * @info: the instance including private data of max8997 MUIC
 * @value: the path according to attached cable
 * @attached: the state of cable (true:attached, false:detached)
 *
 * The max8997 MUIC device share outside H/W line among a varity of cables,
 * so this function set internal path of H/W line according to the type of
 * attached cable.
 */
static int max8997_muic_set_path(struct max8997_muic_info *info,
		u8 val, bool attached)
{
	int ret = 0;
	u8 ctrl1, ctrl2 = 0;

	if (attached)
		ctrl1 = val;
	else
		ctrl1 = CONTROL1_SW_OPEN;

	ret = max8997_update_reg(info->muic,
			MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
	if (ret < 0) {
		dev_err(info->dev, "failed to update MUIC register\n");
		return -EAGAIN;
	}

	if (attached)
		ctrl2 |= CONTROL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */
	else
		ctrl2 |= CONTROL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */

	ret = max8997_update_reg(info->muic,
			MAX8997_MUIC_REG_CONTROL2, ctrl2,
			CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
	if (ret < 0) {
		dev_err(info->dev, "failed to update MUIC register\n");
		return -EAGAIN;
	}

	dev_info(info->dev,
		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
		ctrl1, ctrl2, attached ? "attached" : "detached");

	return 0;
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/*
 * max8997_muic_get_cable_type - Return cable type and check cable state
 * @info: the instance including private data of max8997 MUIC
 * @group: the path according to attached cable
 * @attached: store cable state and return
 *
 * This function check the cable state either attached or detached,
 * and then divide precise type of cable according to cable group.
 *	- MAX8997_CABLE_GROUP_ADC
 *	- MAX8997_CABLE_GROUP_CHG
 */
static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
		enum max8997_muic_cable_group group, bool *attached)
{
	int cable_type = 0;
	int adc;
	int chg_type;

	switch (group) {
	case MAX8997_CABLE_GROUP_ADC:
		/*
		 * Read ADC value to check cable type and decide cable state
		 * according to cable type
		 */
		adc = info->status[0] & STATUS1_ADC_MASK;
		adc >>= STATUS1_ADC_SHIFT;

		/*
		 * Check current cable state/cable type and store cable type
		 * (info->prev_cable_type) for handling cable when cable is
		 * detached.
		 */
		if (adc == MAX8997_MUIC_ADC_OPEN) {
			*attached = false;

			cable_type = info->prev_cable_type;
			info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
		} else {
			*attached = true;

			cable_type = info->prev_cable_type = adc;
		}
		break;
	case MAX8997_CABLE_GROUP_CHG:
		/*
		 * Read charger type to check cable type and decide cable state
		 * according to type of charger cable.
		 */
		chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
		chg_type >>= STATUS2_CHGTYP_SHIFT;

		if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
			*attached = false;

			cable_type = info->prev_chg_type;
			info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
		} else {
			*attached = true;

			/*
			 * Check current cable state/cable type and store cable
			 * type(info->prev_chg_type) for handling cable when
			 * charger cable is detached.
			 */
			cable_type = info->prev_chg_type = chg_type;
		}

		break;
	default:
		dev_err(info->dev, "Unknown cable group (%d)\n", group);
		cable_type = -EINVAL;
		break;
	}

	return cable_type;
}

335 336 337 338 339 340
static int max8997_muic_handle_usb(struct max8997_muic_info *info,
			enum max8997_muic_usb_type usb_type, bool attached)
{
	int ret = 0;

	if (usb_type == MAX8997_USB_HOST) {
341
		ret = max8997_muic_set_path(info, info->path_usb, attached);
342
		if (ret < 0) {
343
			dev_err(info->dev, "failed to update muic register\n");
344
			return ret;
345 346 347
		}
	}

C
Chanwoo Choi 已提交
348 349 350 351 352 353 354 355
	switch (usb_type) {
	case MAX8997_USB_HOST:
		extcon_set_cable_state(info->edev, "USB-Host", attached);
		break;
	case MAX8997_USB_DEVICE:
		extcon_set_cable_state(info->edev, "USB", attached);
		break;
	default:
356 357 358
		dev_err(info->dev, "failed to detect %s usb cable\n",
			attached ? "attached" : "detached");
		return -EINVAL;
C
Chanwoo Choi 已提交
359 360
	}

361
	return 0;
362 363 364
}

static int max8997_muic_handle_dock(struct max8997_muic_info *info,
365
			int cable_type, bool attached)
366 367 368
{
	int ret = 0;

369
	ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
370 371
	if (ret) {
		dev_err(info->dev, "failed to update muic register\n");
372
		return ret;
373 374
	}

375 376
	switch (cable_type) {
	case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
C
Chanwoo Choi 已提交
377
		extcon_set_cable_state(info->edev, "Dock-desk", attached);
378
		break;
379
	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
C
Chanwoo Choi 已提交
380
		extcon_set_cable_state(info->edev, "Dock-card", attached);
381 382
		break;
	default:
383 384 385
		dev_err(info->dev, "failed to detect %s dock device\n",
			attached ? "attached" : "detached");
		return -EINVAL;
386
	}
387 388

	return 0;
389 390 391 392 393 394 395 396
}

static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
			bool attached)
{
	int ret = 0;

	/* switch to UART */
397
	ret = max8997_muic_set_path(info, info->path_uart, attached);
398 399
	if (ret) {
		dev_err(info->dev, "failed to update muic register\n");
400
		return -EINVAL;
401 402
	}

C
Chanwoo Choi 已提交
403
	extcon_set_cable_state(info->edev, "JIG", attached);
404

405
	return 0;
406 407
}

408
static int max8997_muic_adc_handler(struct max8997_muic_info *info)
409
{
410 411
	int cable_type;
	bool attached;
412 413
	int ret = 0;

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	/* Check cable state which is either detached or attached */
	cable_type = max8997_muic_get_cable_type(info,
				MAX8997_CABLE_GROUP_ADC, &attached);

	switch (cable_type) {
	case MAX8997_MUIC_ADC_GROUND:
		ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
		if (ret < 0)
			return ret;
		break;
	case MAX8997_MUIC_ADC_MHL:
		extcon_set_cable_state(info->edev, "MHL", attached);
		break;
	case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
	case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
		ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, attached);
		if (ret < 0)
			return ret;
		break;
	case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
		ret = max8997_muic_handle_dock(info, cable_type, attached);
		if (ret < 0)
			return ret;
		break;
	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
		ret = max8997_muic_handle_jig_uart(info, attached);
		break;
	case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
	case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
	case MAX8997_MUIC_ADC_RESERVED_ACC_1:
	case MAX8997_MUIC_ADC_RESERVED_ACC_2:
	case MAX8997_MUIC_ADC_RESERVED_ACC_3:
	case MAX8997_MUIC_ADC_RESERVED_ACC_4:
	case MAX8997_MUIC_ADC_RESERVED_ACC_5:
	case MAX8997_MUIC_ADC_CEA936_AUDIO:
	case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
	case MAX8997_MUIC_ADC_TTY_CONVERTER:
	case MAX8997_MUIC_ADC_UART_CABLE:
	case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
	case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
	case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
		/*
		 * This cable isn't used in general case if it is specially
		 * needed to detect additional cable, should implement
		 * proper operation when this cable is attached/detached.
		 */
		dev_info(info->dev,
			"cable is %s but it isn't used (type:0x%x)\n",
			attached ? "attached" : "detached", cable_type);
		return -EAGAIN;
C
Chanwoo Choi 已提交
475
	default:
476 477 478
		dev_err(info->dev,
			"failed to detect %s unknown cable (type:0x%x)\n",
			attached ? "attached" : "detached", cable_type);
479
		return -EINVAL;
C
Chanwoo Choi 已提交
480
	}
481

482
	return 0;
483 484
}

485
static int max8997_muic_chg_handler(struct max8997_muic_info *info)
486
{
487 488 489
	int chg_type;
	bool attached;
	int adc;
490

491 492
	chg_type = max8997_muic_get_cable_type(info,
				MAX8997_CABLE_GROUP_CHG, &attached);
493

494
	switch (chg_type) {
495 496 497
	case MAX8997_CHARGER_TYPE_NONE:
		break;
	case MAX8997_CHARGER_TYPE_USB:
498 499 500 501
		adc = info->status[0] & STATUS1_ADC_MASK;
		adc >>= STATUS1_ADC_SHIFT;

		if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
502
			max8997_muic_handle_usb(info,
503
					MAX8997_USB_DEVICE, attached);
504 505 506
		}
		break;
	case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
507
		extcon_set_cable_state(info->edev, "Charge-downstream", attached);
C
Chanwoo Choi 已提交
508
		break;
509
	case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
510
		extcon_set_cable_state(info->edev, "TA", attached);
C
Chanwoo Choi 已提交
511
		break;
512
	case MAX8997_CHARGER_TYPE_500MA:
513
		extcon_set_cable_state(info->edev, "Slow-charger", attached);
C
Chanwoo Choi 已提交
514
		break;
515
	case MAX8997_CHARGER_TYPE_1A:
516
		extcon_set_cable_state(info->edev, "Fast-charger", attached);
517 518
		break;
	default:
519 520 521 522
		dev_err(info->dev,
			"failed to detect %s unknown chg cable (type:0x%x)\n",
			attached ? "attached" : "detached", chg_type);
		return -EINVAL;
523 524
	}

525
	return 0;
526 527 528 529 530 531
}

static void max8997_muic_irq_work(struct work_struct *work)
{
	struct max8997_muic_info *info = container_of(work,
			struct max8997_muic_info, irq_work);
532 533
	int irq_type = 0;
	int i, ret;
534

535 536 537
	if (!info->edev)
		return;

538 539
	mutex_lock(&info->mutex);

540 541 542 543
	for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
		if (info->irq == muic_irqs[i].virq)
			irq_type = muic_irqs[i].irq;

544
	ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
545
				2, info->status);
546 547 548 549 550 551 552
	if (ret) {
		dev_err(info->dev, "failed to read muic register\n");
		mutex_unlock(&info->mutex);
		return;
	}

	switch (irq_type) {
553 554
	case MAX8997_MUICIRQ_ADCError:
	case MAX8997_MUICIRQ_ADCLow:
555
	case MAX8997_MUICIRQ_ADC:
556 557
		/* Handle all of cable except for charger cable */
		ret = max8997_muic_adc_handler(info);
558
		break;
559 560 561 562
	case MAX8997_MUICIRQ_VBVolt:
	case MAX8997_MUICIRQ_DBChg:
	case MAX8997_MUICIRQ_DCDTmr:
	case MAX8997_MUICIRQ_ChgDetRun:
563
	case MAX8997_MUICIRQ_ChgTyp:
564 565 566 567
		/* Handle charger cable */
		ret = max8997_muic_chg_handler(info);
		break;
	case MAX8997_MUICIRQ_OVP:
568 569
		break;
	default:
C
Chanwoo Choi 已提交
570 571
		dev_info(info->dev, "misc interrupt: irq %d occurred\n",
				irq_type);
572 573
		mutex_unlock(&info->mutex);
		return;
574 575
	}

576 577 578
	if (ret < 0)
		dev_err(info->dev, "failed to handle MUIC interrupt\n");

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	mutex_unlock(&info->mutex);

	return;
}

static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
{
	struct max8997_muic_info *info = data;

	dev_dbg(info->dev, "irq:%d\n", irq);
	info->irq = irq;

	schedule_work(&info->irq_work);

	return IRQ_HANDLED;
}

596
static int max8997_muic_detect_dev(struct max8997_muic_info *info)
597
{
598 599 600 601
	int ret = 0;
	int adc;
	int chg_type;
	bool attached;
602

603 604 605 606 607
	mutex_lock(&info->mutex);

	/* Read STATUSx register to detect accessory */
	ret = max8997_bulk_read(info->muic,
			MAX8997_MUIC_REG_STATUS1, 2, info->status);
608
	if (ret) {
609 610 611
		dev_err(info->dev, "failed to read MUIC register\n");
		mutex_unlock(&info->mutex);
		return -EINVAL;
612 613
	}

614 615 616 617 618 619 620 621 622 623
	adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
					&attached);
	if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
		ret = max8997_muic_adc_handler(info);
		if (ret < 0) {
			dev_err(info->dev, "Cannot detect ADC cable\n");
			mutex_unlock(&info->mutex);
			return ret;
		}
	}
624

625 626 627 628 629 630 631 632 633 634
	chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
					&attached);
	if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
		ret = max8997_muic_chg_handler(info);
		if (ret < 0) {
			dev_err(info->dev, "Cannot detect charger cable\n");
			mutex_unlock(&info->mutex);
			return ret;
		}
	}
635

636
	mutex_unlock(&info->mutex);
637

638
	return 0;
639 640
}

641 642 643 644 645 646 647 648 649 650 651
static void max8997_muic_detect_cable_wq(struct work_struct *work)
{
	struct max8997_muic_info *info = container_of(to_delayed_work(work),
				struct max8997_muic_info, wq_detcable);
	int ret;

	ret = max8997_muic_detect_dev(info);
	if (ret < 0)
		pr_err("failed to detect cable type\n");
}

B
Bill Pemberton 已提交
652
static int max8997_muic_probe(struct platform_device *pdev)
653
{
C
Chanwoo Choi 已提交
654 655
	struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
	struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
656
	struct max8997_muic_info *info;
657
	int delay_jiffies;
658 659
	int ret, i;

S
Sachin Kamat 已提交
660 661
	info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
			    GFP_KERNEL);
662 663
	if (!info) {
		dev_err(&pdev->dev, "failed to allocate memory\n");
S
Sachin Kamat 已提交
664
		return -ENOMEM;
665 666 667
	}

	info->dev = &pdev->dev;
C
Chanwoo Choi 已提交
668
	info->muic = max8997->muic;
669 670 671 672 673 674 675 676

	platform_set_drvdata(pdev, info);
	mutex_init(&info->mutex);

	INIT_WORK(&info->irq_work, max8997_muic_irq_work);

	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
		struct max8997_muic_irq *muic_irq = &muic_irqs[i];
677
		unsigned int virq = 0;
678 679

		virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
680 681
		if (!virq) {
			ret = -EINVAL;
682
			goto err_irq;
683
		}
684
		muic_irq->virq = virq;
685

686 687 688 689
		ret = request_threaded_irq(virq, NULL,
				max8997_muic_irq_handler,
				IRQF_NO_SUSPEND,
				muic_irq->name, info);
690 691 692 693 694 695 696 697 698
		if (ret) {
			dev_err(&pdev->dev,
				"failed: irq request (IRQ: %d,"
				" error :%d)\n",
				muic_irq->irq, ret);
			goto err_irq;
		}
	}

C
Chanwoo Choi 已提交
699
	/* External connector */
S
Sachin Kamat 已提交
700 701
	info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
				  GFP_KERNEL);
C
Chanwoo Choi 已提交
702 703 704 705 706 707 708 709 710 711
	if (!info->edev) {
		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
		ret = -ENOMEM;
		goto err_irq;
	}
	info->edev->name = DEV_NAME;
	info->edev->supported_cable = max8997_extcon_cable;
	ret = extcon_dev_register(info->edev, NULL);
	if (ret) {
		dev_err(&pdev->dev, "failed to register extcon device\n");
S
Sachin Kamat 已提交
712
		goto err_irq;
C
Chanwoo Choi 已提交
713 714 715
	}

	if (pdata->muic_pdata) {
716 717 718 719 720 721 722 723
		struct max8997_muic_platform_data *muic_pdata
			= pdata->muic_pdata;

		/* Initialize registers according to platform data */
		for (i = 0; i < muic_pdata->num_init_data; i++) {
			max8997_write_reg(info->muic,
					muic_pdata->init_data[i].addr,
					muic_pdata->init_data[i].data);
C
Chanwoo Choi 已提交
724
		}
725

726 727 728 729 730 731 732 733
		/*
		 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
		 * h/w path of COMP2/COMN1 on CONTROL1 register.
		 */
		if (muic_pdata->path_uart)
			info->path_uart = muic_pdata->path_uart;
		else
			info->path_uart = CONTROL1_SW_UART;
734

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
		if (muic_pdata->path_usb)
			info->path_usb = muic_pdata->path_usb;
		else
			info->path_usb = CONTROL1_SW_USB;

		/*
		 * Default delay time for detecting cable state
		 * after certain time.
		 */
		if (muic_pdata->detcable_delay_ms)
			delay_jiffies =
				msecs_to_jiffies(muic_pdata->detcable_delay_ms);
		else
			delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
	} else {
		info->path_uart = CONTROL1_SW_UART;
751
		info->path_usb = CONTROL1_SW_USB;
752 753
		delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
	}
754 755 756 757

	/* Set initial path for UART */
	 max8997_muic_set_path(info, info->path_uart, true);

758 759 760
	/* Set ADC debounce time */
	max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);

761 762 763 764 765 766 767 768 769 770
	/*
	 * Detect accessory after completing the initialization of platform
	 *
	 * - Use delayed workqueue to detect cable state and then
	 * notify cable state to notifiee/platform through uevent.
	 * After completing the booting of platform, the extcon provider
	 * driver should notify cable state to upper layer.
	 */
	INIT_DELAYED_WORK(&info->wq_detcable, max8997_muic_detect_cable_wq);
	schedule_delayed_work(&info->wq_detcable, delay_jiffies);
771

772
	return 0;
773 774

err_irq:
775
	while (--i >= 0)
776
		free_irq(muic_irqs[i].virq, info);
777 778 779
	return ret;
}

B
Bill Pemberton 已提交
780
static int max8997_muic_remove(struct platform_device *pdev)
781 782 783 784 785
{
	struct max8997_muic_info *info = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
786
		free_irq(muic_irqs[i].virq, info);
787
	cancel_work_sync(&info->irq_work);
788

C
Chanwoo Choi 已提交
789 790
	extcon_dev_unregister(info->edev);

791 792 793 794 795
	return 0;
}

static struct platform_driver max8997_muic_driver = {
	.driver		= {
C
Chanwoo Choi 已提交
796
		.name	= DEV_NAME,
797 798 799
		.owner	= THIS_MODULE,
	},
	.probe		= max8997_muic_probe,
B
Bill Pemberton 已提交
800
	.remove		= max8997_muic_remove,
801 802
};

803
module_platform_driver(max8997_muic_driver);
804

C
Chanwoo Choi 已提交
805
MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
806 807
MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
MODULE_LICENSE("GPL");