extcon-max77693.c 36.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
 *
 * Copyright (C) 2012 Samsung Electrnoics
 * Chanwoo Choi <cw00.choi@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>
22
#include <linux/input.h>
23 24 25 26 27 28 29 30 31 32
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/mfd/max77693.h>
#include <linux/mfd/max77693-private.h>
#include <linux/extcon.h>
#include <linux/regmap.h>
#include <linux/irqdomain.h>

#define	DEV_NAME			"max77693-muic"
33
#define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */
34 35 36 37 38 39 40 41 42 43 44 45

enum max77693_muic_adc_debounce_time {
	ADC_DEBOUNCE_TIME_5MS = 0,
	ADC_DEBOUNCE_TIME_10MS,
	ADC_DEBOUNCE_TIME_25MS,
	ADC_DEBOUNCE_TIME_38_62MS,
};

struct max77693_muic_info {
	struct device *dev;
	struct max77693_dev *max77693;
	struct extcon_dev *edev;
46 47
	int prev_cable_type;
	int prev_cable_type_gnd;
48
	int prev_chg_type;
49
	int prev_button_type;
50 51 52 53 54
	u8 status[2];

	int irq;
	struct work_struct irq_work;
	struct mutex mutex;
55

56 57 58 59 60 61 62 63
	/*
	 * 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;

64 65
	/* Button of dock device */
	struct input_dev *dock;
66 67 68 69 70 71 72

	/*
	 * 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;
73 74
};

75 76 77 78 79 80 81
enum max77693_muic_cable_group {
	MAX77693_CABLE_GROUP_ADC = 0,
	MAX77693_CABLE_GROUP_ADC_GND,
	MAX77693_CABLE_GROUP_CHG,
	MAX77693_CABLE_GROUP_VBVOLT,
};

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
enum max77693_muic_charger_type {
	MAX77693_CHARGER_TYPE_NONE = 0,
	MAX77693_CHARGER_TYPE_USB,
	MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
	MAX77693_CHARGER_TYPE_DEDICATED_CHG,
	MAX77693_CHARGER_TYPE_APPLE_500MA,
	MAX77693_CHARGER_TYPE_APPLE_1A_2A,
	MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
};

/**
 * struct max77693_muic_irq
 * @irq: the index of irq list of MUIC device.
 * @name: the name of irq.
 * @virq: the virtual irq to use irq domain
 */
struct max77693_muic_irq {
	unsigned int irq;
	const char *name;
	unsigned int virq;
};

static struct max77693_muic_irq muic_irqs[] = {
	{ MAX77693_MUIC_IRQ_INT1_ADC,		"muic-ADC" },
	{ MAX77693_MUIC_IRQ_INT1_ADC_LOW,	"muic-ADCLOW" },
	{ MAX77693_MUIC_IRQ_INT1_ADC_ERR,	"muic-ADCError" },
	{ MAX77693_MUIC_IRQ_INT1_ADC1K,		"muic-ADC1K" },
	{ MAX77693_MUIC_IRQ_INT2_CHGTYP,	"muic-CHGTYP" },
	{ MAX77693_MUIC_IRQ_INT2_CHGDETREUN,	"muic-CHGDETREUN" },
	{ MAX77693_MUIC_IRQ_INT2_DCDTMR,	"muic-DCDTMR" },
	{ MAX77693_MUIC_IRQ_INT2_DXOVP,		"muic-DXOVP" },
	{ MAX77693_MUIC_IRQ_INT2_VBVOLT,	"muic-VBVOLT" },
	{ MAX77693_MUIC_IRQ_INT2_VIDRM,		"muic-VIDRM" },
	{ MAX77693_MUIC_IRQ_INT3_EOC,		"muic-EOC" },
	{ MAX77693_MUIC_IRQ_INT3_CGMBC,		"muic-CGMBC" },
	{ MAX77693_MUIC_IRQ_INT3_OVP,		"muic-OVP" },
	{ MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR,	"muic-MBCCHG_ERR" },
	{ MAX77693_MUIC_IRQ_INT3_CHG_ENABLED,	"muic-CHG_ENABLED" },
	{ MAX77693_MUIC_IRQ_INT3_BAT_DET,	"muic-BAT_DET" },
};

/* Define supported accessory type */
enum max77693_muic_acc_type {
	MAX77693_MUIC_ADC_GROUND = 0x0,
	MAX77693_MUIC_ADC_SEND_END_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
	MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
	MAX77693_MUIC_ADC_RESERVED_ACC_1,
	MAX77693_MUIC_ADC_RESERVED_ACC_2,
	MAX77693_MUIC_ADC_RESERVED_ACC_3,
	MAX77693_MUIC_ADC_RESERVED_ACC_4,
	MAX77693_MUIC_ADC_RESERVED_ACC_5,
	MAX77693_MUIC_ADC_CEA936_AUDIO,
	MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
	MAX77693_MUIC_ADC_TTY_CONVERTER,
	MAX77693_MUIC_ADC_UART_CABLE,
	MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
	MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
	MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
	MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
	MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
	MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
	MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
	MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
	MAX77693_MUIC_ADC_OPEN,

	/* The below accessories have same ADC value so ADCLow and
	   ADC1K bit is used to separate specific accessory */
160 161 162 163 164
	MAX77693_MUIC_GND_USB_OTG = 0x100,	/* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */
	MAX77693_MUIC_GND_USB_OTG_VB = 0x104,	/* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */
	MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */
	MAX77693_MUIC_GND_MHL = 0x103,		/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */
	MAX77693_MUIC_GND_MHL_VB = 0x107,	/* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */
165 166 167
};

/* MAX77693 MUIC device support below list of accessories(external connector) */
168 169 170 171 172 173 174 175
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,
176
	EXTCON_CABLE_MHL_TA,
177 178 179
	EXTCON_CABLE_JIG_USB_ON,
	EXTCON_CABLE_JIG_USB_OFF,
	EXTCON_CABLE_JIG_UART_OFF,
180 181 182 183
	EXTCON_CABLE_JIG_UART_ON,
	EXTCON_CABLE_DOCK_SMART,
	EXTCON_CABLE_DOCK_DESK,
	EXTCON_CABLE_DOCK_AUDIO,
184 185 186

	_EXTCON_CABLE_NUM,
};
187

188
static const char *max77693_extcon_cable[] = {
189 190 191 192 193 194 195
	[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",
196
	[EXTCON_CABLE_MHL_TA]			= "MHL_TA",
197 198 199
	[EXTCON_CABLE_JIG_USB_ON]		= "JIG-USB-ON",
	[EXTCON_CABLE_JIG_USB_OFF]		= "JIG-USB-OFF",
	[EXTCON_CABLE_JIG_UART_OFF]		= "JIG-UART-OFF",
200 201 202 203
	[EXTCON_CABLE_JIG_UART_ON]		= "Dock-Car",
	[EXTCON_CABLE_DOCK_SMART]		= "Dock-Smart",
	[EXTCON_CABLE_DOCK_DESK]		= "Dock-Desk",
	[EXTCON_CABLE_DOCK_AUDIO]		= "Dock-Audio",
204

205 206 207
	NULL,
};

208 209 210 211 212
/*
 * max77693_muic_set_debounce_time - Set the debounce time of ADC
 * @info: the instance including private data of max77693 MUIC
 * @time: the debounce time of ADC
 */
213 214 215
static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
		enum max77693_muic_adc_debounce_time time)
{
216
	int ret;
217 218 219 220 221 222

	switch (time) {
	case ADC_DEBOUNCE_TIME_5MS:
	case ADC_DEBOUNCE_TIME_10MS:
	case ADC_DEBOUNCE_TIME_25MS:
	case ADC_DEBOUNCE_TIME_38_62MS:
223 224 225 226
		ret = max77693_update_reg(info->max77693->regmap_muic,
					  MAX77693_MUIC_REG_CTRL3,
					  time << CONTROL3_ADCDBSET_SHIFT,
					  CONTROL3_ADCDBSET_MASK);
227
		if (ret) {
228
			dev_err(info->dev, "failed to set ADC debounce time\n");
229 230
			return -EAGAIN;
		}
231 232 233
		break;
	default:
		dev_err(info->dev, "invalid ADC debounce time\n");
234
		return -EINVAL;
235 236
	}

237
	return 0;
238 239
};

240 241 242 243 244 245 246 247 248 249
/*
 * max77693_muic_set_path - Set hardware line according to attached cable
 * @info: the instance including private data of max77693 MUIC
 * @value: the path according to attached cable
 * @attached: the state of cable (true:attached, false:detached)
 *
 * The max77693 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.
 */
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
static int max77693_muic_set_path(struct max77693_muic_info *info,
		u8 val, bool attached)
{
	int ret = 0;
	u8 ctrl1, ctrl2 = 0;

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

	ret = max77693_update_reg(info->max77693->regmap_muic,
			MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
	if (ret < 0) {
		dev_err(info->dev, "failed to update MUIC register\n");
265
		return -EAGAIN;
266 267 268 269 270 271 272 273 274 275 276 277
	}

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

	ret = max77693_update_reg(info->max77693->regmap_muic,
			MAX77693_MUIC_REG_CTRL2, ctrl2,
			CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
	if (ret < 0) {
		dev_err(info->dev, "failed to update MUIC register\n");
278
		return -EAGAIN;
279 280 281 282 283
	}

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

	return 0;
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/*
 * max77693_muic_get_cable_type - Return cable type and check cable state
 * @info: the instance including private data of max77693 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.
 *	- MAX77693_CABLE_GROUP_ADC
 *	- MAX77693_CABLE_GROUP_ADC_GND
 *	- MAX77693_CABLE_GROUP_CHG
 *	- MAX77693_CABLE_GROUP_VBVOLT
 */
static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
		enum max77693_muic_cable_group group, bool *attached)
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 335 336 337 338 339 340 341 342
	int cable_type = 0;
	int adc;
	int adc1k;
	int adclow;
	int vbvolt;
	int chg_type;

	switch (group) {
	case MAX77693_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 == MAX77693_MUIC_ADC_OPEN) {
			*attached = false;

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

			cable_type = info->prev_cable_type = adc;
		}
		break;
	case MAX77693_CABLE_GROUP_ADC_GND:
		/*
		 * 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;
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
		/*
		 * Check current cable state/cable type and store cable type
		 * (info->prev_cable_type/_gnd) for handling cable when cable
		 * is detached.
		 */
		if (adc == MAX77693_MUIC_ADC_OPEN) {
			*attached = false;

			cable_type = info->prev_cable_type_gnd;
			info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
		} else {
			*attached = true;

			adclow = info->status[0] & STATUS1_ADCLOW_MASK;
			adclow >>= STATUS1_ADCLOW_SHIFT;
			adc1k = info->status[0] & STATUS1_ADC1K_MASK;
			adc1k >>= STATUS1_ADC1K_SHIFT;

			vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
			vbvolt >>= STATUS2_VBVOLT_SHIFT;

			/**
			 * [0x1][VBVolt][ADCLow][ADC1K]
			 * [0x1    0	   0       0  ]	: USB_OTG
368
			 * [0x1    1	   0       0  ]	: USB_OTG_VB
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
			 * [0x1    0       1       0  ] : Audio Video Cable with load
			 * [0x1    0       1       1  ] : MHL without charging connector
			 * [0x1    1       1       1  ] : MHL with charging connector
			 */
			cable_type = ((0x1 << 8)
					| (vbvolt << 2)
					| (adclow << 1)
					| adc1k);

			info->prev_cable_type = adc;
			info->prev_cable_type_gnd = cable_type;
		}

		break;
	case MAX77693_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 == MAX77693_CHARGER_TYPE_NONE) {
			*attached = false;

			cable_type = info->prev_chg_type;
			info->prev_chg_type = MAX77693_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;
	case MAX77693_CABLE_GROUP_VBVOLT:
		/*
		 * Read ADC value to check cable type and decide cable state
		 * according to cable type
		 */
413
		adc = info->status[0] & STATUS1_ADC_MASK;
414 415 416 417 418 419 420 421 422 423 424 425 426
		adc >>= STATUS1_ADC_SHIFT;
		chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
		chg_type >>= STATUS2_CHGTYP_SHIFT;

		if (adc == MAX77693_MUIC_ADC_OPEN
				&& chg_type == MAX77693_CHARGER_TYPE_NONE)
			*attached = false;
		else
			*attached = true;

		/*
		 * Read vbvolt field, if vbvolt is 1,
		 * this cable is used for charging.
427
		 */
428 429 430 431 432 433 434 435 436 437 438 439 440 441
		vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
		vbvolt >>= STATUS2_VBVOLT_SHIFT;

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

	return cable_type;
}

442 443 444 445
static int max77693_muic_dock_handler(struct max77693_muic_info *info,
		int cable_type, bool attached)
{
	int ret = 0;
446 447
	int vbvolt;
	bool cable_attached;
448 449 450 451 452 453 454 455
	char dock_name[CABLE_NAME_MAX];

	dev_info(info->dev,
		"external connector is %s (adc:0x%02x)\n",
		attached ? "attached" : "detached", cable_type);

	switch (cable_type) {
	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
		/*
		 * Check power cable whether attached or detached state.
		 * The Dock-Smart device need surely external power supply.
		 * If power cable(USB/TA) isn't connected to Dock device,
		 * user can't use Dock-Smart for desktop mode.
		 */
		vbvolt = max77693_muic_get_cable_type(info,
				MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
		if (attached && !vbvolt) {
			dev_warn(info->dev,
				"Cannot detect external power supply\n");
			return 0;
		}

		/*
		 * Notify Dock-Smart/MHL state.
		 * - Dock-Smart device include three type of cable which
		 * are HDMI, USB for mouse/keyboard and micro-usb port
		 * for USB/TA cable. Dock-Smart device need always exteranl
		 * power supply(USB/TA cable through micro-usb cable). Dock-
		 * Smart device support screen output of target to separate
		 * monitor and mouse/keyboard for desktop mode.
		 *
		 * Features of 'USB/TA cable with Dock-Smart device'
		 * - Support MHL
		 * - Support external output feature of audio
		 * - Support charging through micro-usb port without data
		 *	     connection if TA cable is connected to target.
		 * - Support charging and data connection through micro-usb port
		 *           if USB cable is connected between target and host
		 *	     device.
		 * - Support OTG device (Mouse/Keyboard)
		 */
		ret = max77693_muic_set_path(info, info->path_usb, attached);
490
		if (ret < 0)
491
			return ret;
492 493

		extcon_set_cable_state(info->edev, "Dock-Smart", attached);
494
		extcon_set_cable_state(info->edev, "MHL", attached);
495 496 497 498 499 500 501 502 503 504 505 506
		goto out;
	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:	/* Dock-Car */
		strcpy(dock_name, "Dock-Car");
		break;
	case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:	/* Dock-Desk */
		strcpy(dock_name, "Dock-Desk");
		break;
	case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
		strcpy(dock_name, "Dock-Audio");
		if (!attached)
			extcon_set_cable_state(info->edev, "USB", false);
		break;
507 508 509 510
	default:
		dev_err(info->dev, "failed to detect %s dock device\n",
			attached ? "attached" : "detached");
		return -EINVAL;
511 512 513 514 515
	}

	/* Dock-Car/Desk/Audio, PATH:AUDIO */
	ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
	if (ret < 0)
516
		return ret;
517 518 519
	extcon_set_cable_state(info->edev, dock_name, attached);

out:
520
	return 0;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}

static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
		int button_type, bool attached)
{
	struct input_dev *dock = info->dock;
	unsigned int code;

	switch (button_type) {
	case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
		... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
		/* DOCK_KEY_PREV */
		code = KEY_PREVIOUSSONG;
		break;
	case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
		... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
		/* DOCK_KEY_NEXT */
		code = KEY_NEXTSONG;
		break;
	case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
		/* DOCK_VOL_DOWN */
		code = KEY_VOLUMEDOWN;
		break;
	case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
		/* DOCK_VOL_UP */
		code = KEY_VOLUMEUP;
		break;
	case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
		... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
		/* DOCK_KEY_PLAY_PAUSE */
		code = KEY_PLAYPAUSE;
		break;
	default:
		dev_err(info->dev,
			"failed to detect %s key (adc:0x%x)\n",
			attached ? "pressed" : "released", button_type);
557
		return -EINVAL;
558 559 560 561 562 563 564 565
	}

	input_event(dock, EV_KEY, code, attached);
	input_sync(dock);

	return 0;
}

566 567 568 569 570
static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
{
	int cable_type_gnd;
	int ret = 0;
	bool attached;
571

572 573
	cable_type_gnd = max77693_muic_get_cable_type(info,
				MAX77693_CABLE_GROUP_ADC_GND, &attached);
574

575
	switch (cable_type_gnd) {
576
	case MAX77693_MUIC_GND_USB_OTG:
577 578
	case MAX77693_MUIC_GND_USB_OTG_VB:
		/* USB_OTG, PATH: AP_USB */
579 580
		ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
		if (ret < 0)
581
			return ret;
582 583 584
		extcon_set_cable_state(info->edev, "USB-Host", attached);
		break;
	case MAX77693_MUIC_GND_AV_CABLE_LOAD:
585
		/* Audio Video Cable with load, PATH:AUDIO */
586 587
		ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
		if (ret < 0)
588
			return ret;
589 590 591
		extcon_set_cable_state(info->edev,
				"Audio-video-load", attached);
		break;
592 593 594
	case MAX77693_MUIC_GND_MHL:
	case MAX77693_MUIC_GND_MHL_VB:
		/* MHL or MHL with USB/TA cable */
595 596 597
		extcon_set_cable_state(info->edev, "MHL", attached);
		break;
	default:
598
		dev_err(info->dev, "failed to detect %s cable of gnd type\n",
599
			attached ? "attached" : "detached");
600
		return -EINVAL;
601 602
	}

603
	return 0;
604 605
}

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
static int max77693_muic_jig_handler(struct max77693_muic_info *info,
		int cable_type, bool attached)
{
	char cable_name[32];
	int ret = 0;
	u8 path = CONTROL1_SW_OPEN;

	dev_info(info->dev,
		"external connector is %s (adc:0x%02x)\n",
		attached ? "attached" : "detached", cable_type);

	switch (cable_type) {
	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:	/* ADC_JIG_USB_OFF */
		/* PATH:AP_USB */
		strcpy(cable_name, "JIG-USB-OFF");
		path = CONTROL1_SW_USB;
		break;
	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:	/* ADC_JIG_USB_ON */
		/* PATH:AP_USB */
		strcpy(cable_name, "JIG-USB-ON");
		path = CONTROL1_SW_USB;
		break;
	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:	/* ADC_JIG_UART_OFF */
		/* PATH:AP_UART */
		strcpy(cable_name, "JIG-UART-OFF");
		path = CONTROL1_SW_UART;
		break;
633 634 635 636
	default:
		dev_err(info->dev, "failed to detect %s jig cable\n",
			attached ? "attached" : "detached");
		return -EINVAL;
637 638 639 640
	}

	ret = max77693_muic_set_path(info, path, attached);
	if (ret < 0)
641
		return ret;
642 643

	extcon_set_cable_state(info->edev, cable_name, attached);
644 645

	return 0;
646 647
}

648
static int max77693_muic_adc_handler(struct max77693_muic_info *info)
649
{
650
	int cable_type;
651
	int button_type;
652
	bool attached;
653 654
	int ret = 0;

655 656 657
	/* Check accessory state which is either detached or attached */
	cable_type = max77693_muic_get_cable_type(info,
				MAX77693_CABLE_GROUP_ADC, &attached);
658 659 660

	dev_info(info->dev,
		"external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
661 662
		attached ? "attached" : "detached", cable_type,
		info->prev_cable_type);
663

664
	switch (cable_type) {
665 666
	case MAX77693_MUIC_ADC_GROUND:
		/* USB_OTG/MHL/Audio */
667
		max77693_muic_adc_ground_handler(info);
668 669 670 671 672
		break;
	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
		/* JIG */
673
		ret = max77693_muic_jig_handler(info, cable_type, attached);
674
		if (ret < 0)
675
			return ret;
676
		break;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:	/* Dock-Car */
	case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:	/* Dock-Desk */
	case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
		/*
		 * DOCK device
		 *
		 * The MAX77693 MUIC device can detect total 34 cable type
		 * except of charger cable and MUIC device didn't define
		 * specfic role of cable in the range of from 0x01 to 0x12
		 * of ADC value. So, can use/define cable with no role according
		 * to schema of hardware board.
		 */
		ret = max77693_muic_dock_handler(info, cable_type, attached);
		if (ret < 0)
692
			return ret;
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
		break;
	case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON:	/* DOCK_KEY_PREV */
	case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON:	/* DOCK_KEY_NEXT */
	case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:	/* DOCK_VOL_DOWN */
	case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:	/* DOCK_VOL_UP */
	case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON:	/* DOCK_KEY_PLAY_PAUSE */
		/*
		 * Button of DOCK device
		 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
		 *
		 * The MAX77693 MUIC device can detect total 34 cable type
		 * except of charger cable and MUIC device didn't define
		 * specfic role of cable in the range of from 0x01 to 0x12
		 * of ADC value. So, can use/define cable with no role according
		 * to schema of hardware board.
		 */
		if (attached)
			button_type = info->prev_button_type = cable_type;
		else
			button_type = info->prev_button_type;

		ret = max77693_muic_dock_button_handler(info, button_type,
							attached);
716
		if (ret < 0)
717
			return ret;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
		break;
	case MAX77693_MUIC_ADC_SEND_END_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
	case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
	case MAX77693_MUIC_ADC_RESERVED_ACC_1:
	case MAX77693_MUIC_ADC_RESERVED_ACC_2:
	case MAX77693_MUIC_ADC_RESERVED_ACC_4:
	case MAX77693_MUIC_ADC_RESERVED_ACC_5:
	case MAX77693_MUIC_ADC_CEA936_AUDIO:
	case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
	case MAX77693_MUIC_ADC_TTY_CONVERTER:
	case MAX77693_MUIC_ADC_UART_CABLE:
	case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
	case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
737 738 739 740 741
		/*
		 * This accessory isn't used in general case if it is specially
		 * needed to detect additional accessory, should implement
		 * proper operation when this accessory is attached/detached.
		 */
742 743
		dev_info(info->dev,
			"accessory is %s but it isn't used (adc:0x%x)\n",
744
			attached ? "attached" : "detached", cable_type);
745
		return -EAGAIN;
746 747 748
	default:
		dev_err(info->dev,
			"failed to detect %s accessory (adc:0x%x)\n",
749
			attached ? "attached" : "detached", cable_type);
750
		return -EINVAL;
751 752
	}

753
	return 0;
754 755
}

756
static int max77693_muic_chg_handler(struct max77693_muic_info *info)
757 758
{
	int chg_type;
759
	int cable_type_gnd;
760
	int cable_type;
761
	bool attached;
762
	bool cable_attached;
763
	int ret = 0;
764

765 766
	chg_type = max77693_muic_get_cable_type(info,
				MAX77693_CABLE_GROUP_CHG, &attached);
767 768 769 770

	dev_info(info->dev,
		"external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
			attached ? "attached" : "detached",
771
			chg_type, info->prev_chg_type);
772 773 774

	switch (chg_type) {
	case MAX77693_CHARGER_TYPE_USB:
775
	case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
776
	case MAX77693_CHARGER_TYPE_NONE:
777
		/* Check MAX77693_CABLE_GROUP_ADC_GND type */
778 779 780
		cable_type_gnd = max77693_muic_get_cable_type(info,
					MAX77693_CABLE_GROUP_ADC_GND,
					&cable_attached);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
		switch (cable_type_gnd) {
		case MAX77693_MUIC_GND_MHL:
		case MAX77693_MUIC_GND_MHL_VB:
			/*
			 * MHL cable with MHL_TA(USB/TA) cable
			 * - MHL cable include two port(HDMI line and separate micro-
			 * usb port. When the target connect MHL cable, extcon driver
			 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
			 * cable is connected, extcon driver notify state to notifiee
			 * for charging battery.
			 *
			 * Features of 'MHL_TA(USB/TA) with MHL cable'
			 * - Support MHL
			 * - Support charging through micro-usb port without data connection
			 */
796 797
			extcon_set_cable_state(info->edev, "MHL_TA", attached);
			if (!cable_attached)
798 799
				extcon_set_cable_state(info->edev, "MHL", cable_attached);
			break;
800 801
		}

802
		/* Check MAX77693_CABLE_GROUP_ADC type */
803 804 805
		cable_type = max77693_muic_get_cable_type(info,
					MAX77693_CABLE_GROUP_ADC,
					&cable_attached);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
		switch (cable_type) {
		case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
			/*
			 * Dock-Audio device with USB/TA cable
			 * - Dock device include two port(Dock-Audio and micro-usb
			 * port). When the target connect Dock-Audio device, extcon
			 * driver check whether USB/TA cable is connected. If USB/TA
			 * cable is connected, extcon driver notify state to notifiee
			 * for charging battery.
			 *
			 * Features of 'USB/TA cable with Dock-Audio device'
			 * - Support external output feature of audio.
			 * - Support charging through micro-usb port without data
			 *           connection.
			 */
821
			extcon_set_cable_state(info->edev, "USB", attached);
822 823

			if (!cable_attached)
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
				extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
			break;
		case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
			/*
			 * Dock-Smart device with USB/TA cable
			 * - Dock-Desk device include three type of cable which
			 * are HDMI, USB for mouse/keyboard and micro-usb port
			 * for USB/TA cable. Dock-Smart device need always exteranl
			 * power supply(USB/TA cable through micro-usb cable). Dock-
			 * Smart device support screen output of target to separate
			 * monitor and mouse/keyboard for desktop mode.
			 *
			 * Features of 'USB/TA cable with Dock-Smart device'
			 * - Support MHL
			 * - Support external output feature of audio
			 * - Support charging through micro-usb port without data
			 *	     connection if TA cable is connected to target.
			 * - Support charging and data connection through micro-usb port
			 *           if USB cable is connected between target and host
			 *	     device.
			 * - Support OTG device (Mouse/Keyboard)
			 */
			ret = max77693_muic_set_path(info, info->path_usb, attached);
			if (ret < 0)
				return ret;

			extcon_set_cable_state(info->edev, "Dock-Smart", attached);
			extcon_set_cable_state(info->edev, "MHL", attached);

			break;
854
		}
855

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
		/* Check MAX77693_CABLE_GROUP_CHG type */
		switch (chg_type) {
		case MAX77693_CHARGER_TYPE_NONE:
			/*
			 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
			 * is attached, muic device happen below two interrupt.
			 * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
			 * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
			 *   connected to MHL or Dock-Audio.
			 * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
			 * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
			 *
			 * If user attach MHL (with USB/TA cable and immediately detach
			 * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
			 * interrupt is happened, USB/TA cable remain connected state to
			 * target. But USB/TA cable isn't connected to target. The user
			 * be face with unusual action. So, driver should check this
			 * situation in spite of, that previous charger type is N/A.
			 */
875
			break;
876 877 878 879 880
		case MAX77693_CHARGER_TYPE_USB:
			/* Only USB cable, PATH:AP_USB */
			ret = max77693_muic_set_path(info, info->path_usb, attached);
			if (ret < 0)
				return ret;
881

882 883 884 885 886 887 888
			extcon_set_cable_state(info->edev, "USB", attached);
			break;
		case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
			/* Only TA cable */
			extcon_set_cable_state(info->edev, "TA", attached);
			break;
		}
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
		break;
	case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
		extcon_set_cable_state(info->edev,
				"Charge-downstream", attached);
		break;
	case MAX77693_CHARGER_TYPE_APPLE_500MA:
		extcon_set_cable_state(info->edev, "Slow-charger", attached);
		break;
	case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
		extcon_set_cable_state(info->edev, "Fast-charger", attached);
		break;
	case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
		break;
	default:
		dev_err(info->dev,
			"failed to detect %s accessory (chg_type:0x%x)\n",
			attached ? "attached" : "detached", chg_type);
906
		return -EINVAL;
907 908
	}

909
	return 0;
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
}

static void max77693_muic_irq_work(struct work_struct *work)
{
	struct max77693_muic_info *info = container_of(work,
			struct max77693_muic_info, irq_work);
	int irq_type = -1;
	int i, ret = 0;

	if (!info->edev)
		return;

	mutex_lock(&info->mutex);

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

	ret = max77693_bulk_read(info->max77693->regmap_muic,
			MAX77693_MUIC_REG_STATUS1, 2, info->status);
	if (ret) {
		dev_err(info->dev, "failed to read MUIC register\n");
		mutex_unlock(&info->mutex);
		return;
	}

	switch (irq_type) {
	case MAX77693_MUIC_IRQ_INT1_ADC:
	case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
	case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
	case MAX77693_MUIC_IRQ_INT1_ADC1K:
		/* Handle all of accessory except for
		   type of charger accessory */
943
		ret = max77693_muic_adc_handler(info);
944 945 946 947 948 949 950 951
		break;
	case MAX77693_MUIC_IRQ_INT2_CHGTYP:
	case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
	case MAX77693_MUIC_IRQ_INT2_DCDTMR:
	case MAX77693_MUIC_IRQ_INT2_DXOVP:
	case MAX77693_MUIC_IRQ_INT2_VBVOLT:
	case MAX77693_MUIC_IRQ_INT2_VIDRM:
		/* Handle charger accessory */
952
		ret = max77693_muic_chg_handler(info);
953 954 955 956 957 958 959 960 961 962 963
		break;
	case MAX77693_MUIC_IRQ_INT3_EOC:
	case MAX77693_MUIC_IRQ_INT3_CGMBC:
	case MAX77693_MUIC_IRQ_INT3_OVP:
	case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
	case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
	case MAX77693_MUIC_IRQ_INT3_BAT_DET:
		break;
	default:
		dev_err(info->dev, "muic interrupt: irq %d occurred\n",
				irq_type);
964 965
		mutex_unlock(&info->mutex);
		return;
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	}

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

	mutex_unlock(&info->mutex);

	return;
}

static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
{
	struct max77693_muic_info *info = data;

	info->irq = irq;
	schedule_work(&info->irq_work);

	return IRQ_HANDLED;
}

static struct regmap_config max77693_muic_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
};

static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
{
	int ret = 0;
994 995 996
	int adc;
	int chg_type;
	bool attached;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

	mutex_lock(&info->mutex);

	/* Read STATUSx register to detect accessory */
	ret = max77693_bulk_read(info->max77693->regmap_muic,
			MAX77693_MUIC_REG_STATUS1, 2, info->status);
	if (ret) {
		dev_err(info->dev, "failed to read MUIC register\n");
		mutex_unlock(&info->mutex);
		return -EINVAL;
	}

1009 1010 1011 1012
	adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
					&attached);
	if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
		ret = max77693_muic_adc_handler(info);
1013
		if (ret < 0) {
1014
			dev_err(info->dev, "Cannot detect accessory\n");
1015 1016 1017
			mutex_unlock(&info->mutex);
			return ret;
		}
1018 1019
	}

1020 1021 1022 1023
	chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
					&attached);
	if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
		ret = max77693_muic_chg_handler(info);
1024
		if (ret < 0) {
1025
			dev_err(info->dev, "Cannot detect charger accessory\n");
1026 1027 1028
			mutex_unlock(&info->mutex);
			return ret;
		}
1029 1030 1031
	}

	mutex_unlock(&info->mutex);
1032

1033
	return 0;
1034 1035
}

1036 1037 1038 1039 1040 1041 1042 1043
static void max77693_muic_detect_cable_wq(struct work_struct *work)
{
	struct max77693_muic_info *info = container_of(to_delayed_work(work),
				struct max77693_muic_info, wq_detcable);

	max77693_muic_detect_accessory(info);
}

B
Bill Pemberton 已提交
1044
static int max77693_muic_probe(struct platform_device *pdev)
1045 1046
{
	struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1047
	struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1048
	struct max77693_muic_info *info;
1049 1050 1051
	int delay_jiffies;
	int ret;
	int i;
1052 1053
	u8 id;

S
Sachin Kamat 已提交
1054 1055
	info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
				   GFP_KERNEL);
1056 1057
	if (!info) {
		dev_err(&pdev->dev, "failed to allocate memory\n");
S
Sachin Kamat 已提交
1058
		return -ENOMEM;
1059 1060 1061
	}
	info->dev = &pdev->dev;
	info->max77693 = max77693;
S
Sachin Kamat 已提交
1062
	if (info->max77693->regmap_muic) {
1063
		dev_dbg(&pdev->dev, "allocate register map\n");
S
Sachin Kamat 已提交
1064
	} else {
1065 1066 1067 1068 1069 1070 1071
		info->max77693->regmap_muic = devm_regmap_init_i2c(
						info->max77693->muic,
						&max77693_muic_regmap_config);
		if (IS_ERR(info->max77693->regmap_muic)) {
			ret = PTR_ERR(info->max77693->regmap_muic);
			dev_err(max77693->dev,
				"failed to allocate register map: %d\n", ret);
1072
			return ret;
1073
		}
1074
	}
1075 1076

	/* Register input device for button of dock device */
1077
	info->dock = devm_input_allocate_device(&pdev->dev);
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	if (!info->dock) {
		dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
		return -ENOMEM;
	}
	info->dock->name = "max77693-muic/dock";
	info->dock->phys = "max77693-muic/extcon";
	info->dock->dev.parent = &pdev->dev;

	__set_bit(EV_REP, info->dock->evbit);

	input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
	input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
	input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
	input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
	input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);

	ret = input_register_device(info->dock);
	if (ret < 0) {
		dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
				ret);
		return ret;
	}

1101 1102 1103 1104 1105 1106 1107 1108
	platform_set_drvdata(pdev, info);
	mutex_init(&info->mutex);

	INIT_WORK(&info->irq_work, max77693_muic_irq_work);

	/* Support irq domain for MAX77693 MUIC device */
	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
		struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1109
		unsigned int virq = 0;
1110 1111

		virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
1112 1113
		if (!virq) {
			ret = -EINVAL;
1114
			goto err_irq;
1115
		}
1116 1117 1118 1119
		muic_irq->virq = virq;

		ret = request_threaded_irq(virq, NULL,
				max77693_muic_irq_handler,
1120 1121
				IRQF_NO_SUSPEND,
				muic_irq->name, info);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
		if (ret) {
			dev_err(&pdev->dev,
				"failed: irq request (IRQ: %d,"
				" error :%d)\n",
				muic_irq->irq, ret);
			goto err_irq;
		}
	}

	/* Initialize extcon device */
S
Sachin Kamat 已提交
1132 1133
	info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
				  GFP_KERNEL);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	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 = max77693_extcon_cable;
	ret = extcon_dev_register(info->edev, NULL);
	if (ret) {
		dev_err(&pdev->dev, "failed to register extcon device\n");
S
Sachin Kamat 已提交
1144
		goto err_irq;
1145 1146
	}

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
	if (pdata->muic_data) {
		struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;

		/* Initialize MUIC register by using platform data */
		for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
			enum max77693_irq_source irq_src
					= MAX77693_IRQ_GROUP_NR;

			max77693_write_reg(info->max77693->regmap_muic,
					muic_pdata->init_data[i].addr,
					muic_pdata->init_data[i].data);

			switch (muic_pdata->init_data[i].addr) {
			case MAX77693_MUIC_REG_INTMASK1:
				irq_src = MUIC_INT1;
				break;
			case MAX77693_MUIC_REG_INTMASK2:
				irq_src = MUIC_INT2;
				break;
			case MAX77693_MUIC_REG_INTMASK3:
				irq_src = MUIC_INT3;
				break;
			}

			if (irq_src < MAX77693_IRQ_GROUP_NR)
				info->max77693->irq_masks_cur[irq_src]
					= muic_pdata->init_data[i].data;
1174 1175
		}

1176 1177 1178 1179 1180 1181 1182 1183
		/*
		 * 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;
1184

1185 1186 1187 1188
		if (muic_pdata->path_usb)
			info->path_usb = muic_pdata->path_usb;
		else
			info->path_usb = CONTROL1_SW_USB;
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
		/*
		 * 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 {
1200
		info->path_usb = CONTROL1_SW_USB;
1201 1202 1203
		info->path_uart = CONTROL1_SW_UART;
		delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
	}
1204 1205 1206 1207

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

1208 1209 1210 1211 1212
	/* Check revision number of MUIC device*/
	ret = max77693_read_reg(info->max77693->regmap_muic,
			MAX77693_MUIC_REG_ID, &id);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to read revision number\n");
1213
		goto err_extcon;
1214 1215 1216 1217 1218 1219
	}
	dev_info(info->dev, "device ID : 0x%x\n", id);

	/* Set ADC debounce time */
	max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
	/*
	 * 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, max77693_muic_detect_cable_wq);
	schedule_delayed_work(&info->wq_detcable, delay_jiffies);
1230 1231 1232

	return ret;

1233 1234
err_extcon:
	extcon_dev_unregister(info->edev);
1235
err_irq:
1236 1237
	while (--i >= 0)
		free_irq(muic_irqs[i].virq, info);
1238 1239 1240
	return ret;
}

B
Bill Pemberton 已提交
1241
static int max77693_muic_remove(struct platform_device *pdev)
1242 1243 1244 1245 1246 1247 1248
{
	struct max77693_muic_info *info = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
		free_irq(muic_irqs[i].virq, info);
	cancel_work_sync(&info->irq_work);
1249
	input_unregister_device(info->dock);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	extcon_dev_unregister(info->edev);

	return 0;
}

static struct platform_driver max77693_muic_driver = {
	.driver		= {
		.name	= DEV_NAME,
		.owner	= THIS_MODULE,
	},
	.probe		= max77693_muic_probe,
B
Bill Pemberton 已提交
1261
	.remove		= max77693_muic_remove,
1262 1263 1264 1265 1266 1267 1268 1269
};

module_platform_driver(max77693_muic_driver);

MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:extcon-max77693");