extcon-max8997.c 13.2 KB
Newer Older
1
/*
C
Chanwoo Choi 已提交
2
 * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
3
 *
C
Chanwoo Choi 已提交
4
 *  Copyright (C) 2012 Samsung Electrnoics
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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

/* MAX8997-MUIC STATUS1 register */
#define STATUS1_ADC_SHIFT		0
#define STATUS1_ADCLOW_SHIFT		5
#define STATUS1_ADCERR_SHIFT		6
#define STATUS1_ADC_MASK		(0x1f << STATUS1_ADC_SHIFT)
#define STATUS1_ADCLOW_MASK		(0x1 << STATUS1_ADCLOW_SHIFT)
#define STATUS1_ADCERR_MASK		(0x1 << STATUS1_ADCERR_SHIFT)

/* MAX8997-MUIC STATUS2 register */
#define STATUS2_CHGTYP_SHIFT		0
#define STATUS2_CHGDETRUN_SHIFT		3
#define STATUS2_DCDTMR_SHIFT		4
#define STATUS2_DBCHG_SHIFT		5
#define STATUS2_VBVOLT_SHIFT		6
#define STATUS2_CHGTYP_MASK		(0x7 << STATUS2_CHGTYP_SHIFT)
#define STATUS2_CHGDETRUN_MASK		(0x1 << STATUS2_CHGDETRUN_SHIFT)
#define STATUS2_DCDTMR_MASK		(0x1 << STATUS2_DCDTMR_SHIFT)
#define STATUS2_DBCHG_MASK		(0x1 << STATUS2_DBCHG_SHIFT)
#define STATUS2_VBVOLT_MASK		(0x1 << STATUS2_VBVOLT_SHIFT)

/* MAX8997-MUIC STATUS3 register */
#define STATUS3_OVP_SHIFT		2
#define STATUS3_OVP_MASK		(0x1 << STATUS3_OVP_SHIFT)

/* MAX8997-MUIC CONTROL1 register */
#define COMN1SW_SHIFT			0
#define COMP2SW_SHIFT			3
#define COMN1SW_MASK			(0x7 << COMN1SW_SHIFT)
#define COMP2SW_MASK			(0x7 << COMP2SW_SHIFT)
#define SW_MASK				(COMP2SW_MASK | COMN1SW_MASK)

#define MAX8997_SW_USB		((1 << COMP2SW_SHIFT) | (1 << COMN1SW_SHIFT))
#define MAX8997_SW_AUDIO	((2 << COMP2SW_SHIFT) | (2 << COMN1SW_SHIFT))
#define MAX8997_SW_UART		((3 << COMP2SW_SHIFT) | (3 << COMN1SW_SHIFT))
#define MAX8997_SW_OPEN		((0 << COMP2SW_SHIFT) | (0 << COMN1SW_SHIFT))

#define	MAX8997_ADC_GROUND		0x00
#define	MAX8997_ADC_MHL			0x01
#define	MAX8997_ADC_JIG_USB_1		0x18
#define	MAX8997_ADC_JIG_USB_2		0x19
#define	MAX8997_ADC_DESKDOCK		0x1a
#define	MAX8997_ADC_JIG_UART		0x1c
#define	MAX8997_ADC_CARDOCK		0x1d
#define	MAX8997_ADC_OPEN		0x1f

struct max8997_muic_irq {
	unsigned int irq;
	const char *name;
81
	unsigned int virq;
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
};

static struct max8997_muic_irq muic_irqs[] = {
	{ MAX8997_MUICIRQ_ADCError, "muic-ADC_error" },
	{ MAX8997_MUICIRQ_ADCLow, "muic-ADC_low" },
	{ MAX8997_MUICIRQ_ADC, "muic-ADC" },
	{ MAX8997_MUICIRQ_VBVolt, "muic-VB_voltage" },
	{ MAX8997_MUICIRQ_DBChg, "muic-DB_charger" },
	{ MAX8997_MUICIRQ_DCDTmr, "muic-DCD_timer" },
	{ MAX8997_MUICIRQ_ChgDetRun, "muic-CDR_status" },
	{ MAX8997_MUICIRQ_ChgTyp, "muic-charger_type" },
	{ MAX8997_MUICIRQ_OVP, "muic-over_voltage" },
};

struct max8997_muic_info {
	struct device *dev;
	struct i2c_client *muic;
	struct max8997_muic_platform_data *muic_pdata;

	int irq;
	struct work_struct irq_work;

	enum max8997_muic_charger_type pre_charger_type;
	int pre_adc;

	struct mutex mutex;
C
Chanwoo Choi 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120

	struct extcon_dev	*edev;
};

const char *max8997_extcon_cable[] = {
	[0] = "USB",
	[1] = "USB-Host",
	[2] = "TA",
	[3] = "Fast-charger",
	[4] = "Slow-charger",
	[5] = "Charge-downstream",
	[6] = "MHL",
	[7] = "Dock-desk",
121 122
	[8] = "Dock-card",
	[9] = "JIG",
C
Chanwoo Choi 已提交
123 124

	NULL,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
};

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) {
		/* switch to USB */
		ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
				attached ? MAX8997_SW_USB : MAX8997_SW_OPEN,
				SW_MASK);
		if (ret) {
			dev_err(info->dev, "failed to update muic register\n");
			goto out;
		}
	}

C
Chanwoo Choi 已提交
143 144 145 146 147 148 149 150 151 152 153 154
	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:
		ret = -EINVAL;
		break;
	}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
out:
	return ret;
}

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

	/* switch to AUDIO */
	ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
				attached ? MAX8997_SW_AUDIO : MAX8997_SW_OPEN,
				SW_MASK);
	if (ret) {
		dev_err(info->dev, "failed to update muic register\n");
		goto out;
	}

	switch (adc) {
	case MAX8997_ADC_DESKDOCK:
C
Chanwoo Choi 已提交
175
		extcon_set_cable_state(info->edev, "Dock-desk", attached);
176 177
		break;
	case MAX8997_ADC_CARDOCK:
C
Chanwoo Choi 已提交
178
		extcon_set_cable_state(info->edev, "Dock-card", attached);
179 180
		break;
	default:
C
Chanwoo Choi 已提交
181
		ret = -EINVAL;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
		break;
	}
out:
	return ret;
}

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

	/* switch to UART */
	ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
				attached ? MAX8997_SW_UART : MAX8997_SW_OPEN,
				SW_MASK);
	if (ret) {
		dev_err(info->dev, "failed to update muic register\n");
		goto out;
	}

C
Chanwoo Choi 已提交
202
	extcon_set_cable_state(info->edev, "JIG", attached);
203 204 205 206 207 208 209 210 211 212 213 214 215
out:
	return ret;
}

static int max8997_muic_handle_adc_detach(struct max8997_muic_info *info)
{
	int ret = 0;

	switch (info->pre_adc) {
	case MAX8997_ADC_GROUND:
		ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, false);
		break;
	case MAX8997_ADC_MHL:
C
Chanwoo Choi 已提交
216
		extcon_set_cable_state(info->edev, "MHL", false);
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
		break;
	case MAX8997_ADC_JIG_USB_1:
	case MAX8997_ADC_JIG_USB_2:
		ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, false);
		break;
	case MAX8997_ADC_DESKDOCK:
	case MAX8997_ADC_CARDOCK:
		ret = max8997_muic_handle_dock(info, info->pre_adc, false);
		break;
	case MAX8997_ADC_JIG_UART:
		ret = max8997_muic_handle_jig_uart(info, false);
		break;
	default:
		break;
	}

	return ret;
}

static int max8997_muic_handle_adc(struct max8997_muic_info *info, int adc)
{
	int ret = 0;

	switch (adc) {
	case MAX8997_ADC_GROUND:
		ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, true);
		break;
	case MAX8997_ADC_MHL:
C
Chanwoo Choi 已提交
245
		extcon_set_cable_state(info->edev, "MHL", true);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		break;
	case MAX8997_ADC_JIG_USB_1:
	case MAX8997_ADC_JIG_USB_2:
		ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, true);
		break;
	case MAX8997_ADC_DESKDOCK:
	case MAX8997_ADC_CARDOCK:
		ret = max8997_muic_handle_dock(info, adc, true);
		break;
	case MAX8997_ADC_JIG_UART:
		ret = max8997_muic_handle_jig_uart(info, true);
		break;
	case MAX8997_ADC_OPEN:
		ret = max8997_muic_handle_adc_detach(info);
		break;
	default:
C
Chanwoo Choi 已提交
262 263
		ret = -EINVAL;
		goto out;
264 265 266
	}

	info->pre_adc = adc;
C
Chanwoo Choi 已提交
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
out:
	return ret;
}

static int max8997_muic_handle_charger_type_detach(
				struct max8997_muic_info *info)
{
	int ret = 0;

	switch (info->pre_charger_type) {
	case MAX8997_CHARGER_TYPE_USB:
		extcon_set_cable_state(info->edev, "USB", false);
		break;
	case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
		extcon_set_cable_state(info->edev, "Charge-downstream", false);
		break;
	case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
		extcon_set_cable_state(info->edev, "TA", false);
		break;
	case MAX8997_CHARGER_TYPE_500MA:
		extcon_set_cable_state(info->edev, "Slow-charger", false);
		break;
	case MAX8997_CHARGER_TYPE_1A:
		extcon_set_cable_state(info->edev, "Fast-charger", false);
		break;
	default:
		ret = -EINVAL;
		break;
	}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

	return ret;
}

static int max8997_muic_handle_charger_type(struct max8997_muic_info *info,
				enum max8997_muic_charger_type charger_type)
{
	u8 adc;
	int ret;

	ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_STATUS1, &adc);
	if (ret) {
		dev_err(info->dev, "failed to read muic register\n");
		goto out;
	}

	switch (charger_type) {
	case MAX8997_CHARGER_TYPE_NONE:
C
Chanwoo Choi 已提交
314
		ret = max8997_muic_handle_charger_type_detach(info);
315 316 317 318 319 320 321 322
		break;
	case MAX8997_CHARGER_TYPE_USB:
		if ((adc & STATUS1_ADC_MASK) == MAX8997_ADC_OPEN) {
			max8997_muic_handle_usb(info,
					MAX8997_USB_DEVICE, true);
		}
		break;
	case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
C
Chanwoo Choi 已提交
323 324
		extcon_set_cable_state(info->edev, "Charge-downstream", true);
		break;
325
	case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
C
Chanwoo Choi 已提交
326 327
		extcon_set_cable_state(info->edev, "TA", true);
		break;
328
	case MAX8997_CHARGER_TYPE_500MA:
C
Chanwoo Choi 已提交
329 330
		extcon_set_cable_state(info->edev, "Slow-charger", true);
		break;
331
	case MAX8997_CHARGER_TYPE_1A:
C
Chanwoo Choi 已提交
332
		extcon_set_cable_state(info->edev, "Fast-charger", true);
333 334
		break;
	default:
C
Chanwoo Choi 已提交
335 336
		ret = -EINVAL;
		goto out;
337 338 339 340 341 342 343 344 345 346 347
	}

	info->pre_charger_type = charger_type;
out:
	return ret;
}

static void max8997_muic_irq_work(struct work_struct *work)
{
	struct max8997_muic_info *info = container_of(work,
			struct max8997_muic_info, irq_work);
C
Chanwoo Choi 已提交
348
	u8 status[2];
349
	u8 adc, chg_type;
350 351
	int irq_type = 0;
	int i, ret;
352 353 354 355

	mutex_lock(&info->mutex);

	ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
C
Chanwoo Choi 已提交
356
				2, status);
357 358 359 360 361 362 363 364 365
	if (ret) {
		dev_err(info->dev, "failed to read muic register\n");
		mutex_unlock(&info->mutex);
		return;
	}

	dev_dbg(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
			status[0], status[1]);

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

370 371 372 373 374 375 376 377 378 379 380 381 382 383
	switch (irq_type) {
	case MAX8997_MUICIRQ_ADC:
		adc = status[0] & STATUS1_ADC_MASK;
		adc >>= STATUS1_ADC_SHIFT;

		max8997_muic_handle_adc(info, adc);
		break;
	case MAX8997_MUICIRQ_ChgTyp:
		chg_type = status[1] & STATUS2_CHGTYP_MASK;
		chg_type >>= STATUS2_CHGTYP_SHIFT;

		max8997_muic_handle_charger_type(info, chg_type);
		break;
	default:
C
Chanwoo Choi 已提交
384 385
		dev_info(info->dev, "misc interrupt: irq %d occurred\n",
				irq_type);
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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		break;
	}

	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;
}

static void max8997_muic_detect_dev(struct max8997_muic_info *info)
{
	int ret;
	u8 status[2], adc, chg_type;

	ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
				2, status);
	if (ret) {
		dev_err(info->dev, "failed to read muic register\n");
		return;
	}

	dev_info(info->dev, "STATUS1:0x%x, STATUS2:0x%x\n",
			status[0], status[1]);

	adc = status[0] & STATUS1_ADC_MASK;
	adc >>= STATUS1_ADC_SHIFT;

	chg_type = status[1] & STATUS2_CHGTYP_MASK;
	chg_type >>= STATUS2_CHGTYP_SHIFT;

	max8997_muic_handle_adc(info, adc);
	max8997_muic_handle_charger_type(info, chg_type);
}

static int __devinit max8997_muic_probe(struct platform_device *pdev)
{
C
Chanwoo Choi 已提交
433 434
	struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
	struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
435 436 437 438 439 440 441 442 443 444 445
	struct max8997_muic_info *info;
	int ret, i;

	info = kzalloc(sizeof(struct max8997_muic_info), GFP_KERNEL);
	if (!info) {
		dev_err(&pdev->dev, "failed to allocate memory\n");
		ret = -ENOMEM;
		goto err_kfree;
	}

	info->dev = &pdev->dev;
C
Chanwoo Choi 已提交
446
	info->muic = max8997->muic;
447 448 449 450 451 452 453 454

	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];
455 456 457 458 459 460
		int virq = 0;

		virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
		if (!virq)
			goto err_irq;
		muic_irq->virq = virq;
461

462 463
		ret = request_threaded_irq(virq, NULL,max8997_muic_irq_handler,
				0, muic_irq->name, info);
464 465 466 467 468 469 470 471 472
		if (ret) {
			dev_err(&pdev->dev,
				"failed: irq request (IRQ: %d,"
				" error :%d)\n",
				muic_irq->irq, ret);
			goto err_irq;
		}
	}

C
Chanwoo Choi 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	/* External connector */
	info->edev = kzalloc(sizeof(struct extcon_dev), GFP_KERNEL);
	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");
		goto err_extcon;
	}

488
	/* Initialize registers according to platform data */
C
Chanwoo Choi 已提交
489 490 491 492 493 494 495 496
	if (pdata->muic_pdata) {
		struct max8997_muic_platform_data *mdata = info->muic_pdata;

		for (i = 0; i < mdata->num_init_data; i++) {
			max8997_write_reg(info->muic, mdata->init_data[i].addr,
					mdata->init_data[i].data);
		}
	}
497 498 499 500 501 502

	/* Initial device detection */
	max8997_muic_detect_dev(info);

	return ret;

C
Chanwoo Choi 已提交
503 504
err_extcon:
	kfree(info->edev);
505
err_irq:
506
	while (--i >= 0)
507
		free_irq(muic_irqs[i].virq, info);
508 509 510 511 512 513 514 515 516 517 518
	kfree(info);
err_kfree:
	return ret;
}

static int __devexit max8997_muic_remove(struct platform_device *pdev)
{
	struct max8997_muic_info *info = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
519
		free_irq(muic_irqs[i].virq, info);
520
	cancel_work_sync(&info->irq_work);
521

C
Chanwoo Choi 已提交
522 523
	extcon_dev_unregister(info->edev);

524
	kfree(info->edev);
525 526 527 528 529 530 531
	kfree(info);

	return 0;
}

static struct platform_driver max8997_muic_driver = {
	.driver		= {
C
Chanwoo Choi 已提交
532
		.name	= DEV_NAME,
533 534 535 536 537 538
		.owner	= THIS_MODULE,
	},
	.probe		= max8997_muic_probe,
	.remove		= __devexit_p(max8997_muic_remove),
};

539
module_platform_driver(max8997_muic_driver);
540

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