Blame view

drivers/extcon/extcon-max77693.c 36.5 KB
db1b90374   Chanwoo Choi   extcon: MAX77693:...
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>
39bf369e4   Chanwoo Choi   extcon: max77693:...
22
  #include <linux/input.h>
db1b90374   Chanwoo Choi   extcon: MAX77693:...
23
24
25
26
  #include <linux/interrupt.h>
  #include <linux/err.h>
  #include <linux/platform_device.h>
  #include <linux/mfd/max77693.h>
61b305cd2   Krzysztof Kozlowski   drivers: max77693...
27
  #include <linux/mfd/max77693-common.h>
db1b90374   Chanwoo Choi   extcon: MAX77693:...
28
29
30
31
32
33
  #include <linux/mfd/max77693-private.h>
  #include <linux/extcon.h>
  #include <linux/regmap.h>
  #include <linux/irqdomain.h>
  
  #define	DEV_NAME			"max77693-muic"
297620fd1   Chanwoo Choi   extcon: max77693:...
34
  #define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
35

0ec83bd24   Chanwoo Choi   extcon: max77693:...
36
37
38
39
40
41
  /*
   * Default value of MAX77693 register to bring up MUIC device.
   * If user don't set some initial value for MUIC device through platform data,
   * extcon-max77693 driver use 'default_init_data' to bring up base operation
   * of MAX77693 MUIC device.
   */
813b45164   Sachin Kamat   extcon: max77693:...
42
  static struct max77693_reg_data default_init_data[] = {
0ec83bd24   Chanwoo Choi   extcon: max77693:...
43
44
45
  	{
  		/* STATUS2 - [3]ChgDetRun */
  		.addr = MAX77693_MUIC_REG_STATUS2,
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
46
  		.data = MAX77693_STATUS2_CHGDETRUN_MASK,
0ec83bd24   Chanwoo Choi   extcon: max77693:...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	}, {
  		/* INTMASK1 - Unmask [3]ADC1KM,[0]ADCM */
  		.addr = MAX77693_MUIC_REG_INTMASK1,
  		.data = INTMASK1_ADC1K_MASK
  			| INTMASK1_ADC_MASK,
  	}, {
  		/* INTMASK2 - Unmask [0]ChgTypM */
  		.addr = MAX77693_MUIC_REG_INTMASK2,
  		.data = INTMASK2_CHGTYP_MASK,
  	}, {
  		/* INTMASK3 - Mask all of interrupts */
  		.addr = MAX77693_MUIC_REG_INTMASK3,
  		.data = 0x0,
  	}, {
  		/* CDETCTRL2 */
  		.addr = MAX77693_MUIC_REG_CDETCTRL2,
  		.data = CDETCTRL2_VIDRMEN_MASK
  			| CDETCTRL2_DXOVPEN_MASK,
  	},
  };
db1b90374   Chanwoo Choi   extcon: MAX77693:...
67
68
69
70
71
72
73
74
75
76
77
  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;
154f757fd   Chanwoo Choi   extcon: max77693:...
78
79
  	int prev_cable_type;
  	int prev_cable_type_gnd;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
80
  	int prev_chg_type;
39bf369e4   Chanwoo Choi   extcon: max77693:...
81
  	int prev_button_type;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
82
83
84
85
86
  	u8 status[2];
  
  	int irq;
  	struct work_struct irq_work;
  	struct mutex mutex;
39bf369e4   Chanwoo Choi   extcon: max77693:...
87

297620fd1   Chanwoo Choi   extcon: max77693:...
88
89
90
91
92
93
94
  	/*
  	 * 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;
39bf369e4   Chanwoo Choi   extcon: max77693:...
95
96
  	/* Button of dock device */
  	struct input_dev *dock;
2b75799f5   Chanwoo Choi   extcon: max77693:...
97
98
99
100
101
102
103
  
  	/*
  	 * 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;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
104
  };
154f757fd   Chanwoo Choi   extcon: max77693:...
105
106
107
108
109
110
  enum max77693_muic_cable_group {
  	MAX77693_CABLE_GROUP_ADC = 0,
  	MAX77693_CABLE_GROUP_ADC_GND,
  	MAX77693_CABLE_GROUP_CHG,
  	MAX77693_CABLE_GROUP_VBVOLT,
  };
db1b90374   Chanwoo Choi   extcon: MAX77693:...
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  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,
af57fa4de   Srikant Ritolia   extcon: Restructu...
186
187
188
189
  	/*
  	 * The below accessories have same ADC value so ADCLow and
  	 * ADC1K bit is used to separate specific accessory.
  	 */
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
190
  						/* ADC|VBVolot|ADCLow|ADC1K| */
4c883abee   Jaewon Kim   extcon: max77693:...
191
192
  	MAX77693_MUIC_GND_USB_HOST = 0x100,	/* 0x0|      0|     0|    0| */
  	MAX77693_MUIC_GND_USB_HOST_VB = 0x104,	/* 0x0|      1|     0|    0| */
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
193
194
195
  	MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* 0x0|      0|     1|    0| */
  	MAX77693_MUIC_GND_MHL = 0x103,		/* 0x0|      0|     1|    1| */
  	MAX77693_MUIC_GND_MHL_VB = 0x107,	/* 0x0|      1|     1|    1| */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
196
  };
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
197
198
199
  /*
   * MAX77693 MUIC device support below list of accessories(external connector)
   */
73b6ecdb9   Chanwoo Choi   extcon: Redefine ...
200
  static const unsigned int max77693_extcon_cable[] = {
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
201
202
  	EXTCON_USB,
  	EXTCON_USB_HOST,
8b45b6a07   Chanwoo Choi   extcon: Add the E...
203
  	EXTCON_CHG_USB_SDP,
11eecf910   Chanwoo Choi   extcon: Modify th...
204
205
206
207
208
  	EXTCON_CHG_USB_DCP,
  	EXTCON_CHG_USB_FAST,
  	EXTCON_CHG_USB_SLOW,
  	EXTCON_CHG_USB_CDP,
  	EXTCON_DISP_MHL,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
209
210
211
  	EXTCON_JIG,
  	EXTCON_DOCK,
  	EXTCON_NONE,
db1b90374   Chanwoo Choi   extcon: MAX77693:...
212
  };
154f757fd   Chanwoo Choi   extcon: max77693:...
213
214
215
216
217
  /*
   * 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
   */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
218
219
220
  static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
  		enum max77693_muic_adc_debounce_time time)
  {
bf2627d66   Axel Lin   extcon: max77693:...
221
  	int ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
222
223
224
225
226
227
  
  	switch (time) {
  	case ADC_DEBOUNCE_TIME_5MS:
  	case ADC_DEBOUNCE_TIME_10MS:
  	case ADC_DEBOUNCE_TIME_25MS:
  	case ADC_DEBOUNCE_TIME_38_62MS:
dc6048d72   Jonghwa Lee   extcon: max77693:...
228
229
230
231
232
233
234
  		/*
  		 * Don't touch BTLDset, JIGset when you want to change adc
  		 * debounce time. If it writes other than 0 to BTLDset, JIGset
  		 * muic device will be reset and loose current state.
  		 */
  		ret = regmap_write(info->max77693->regmap_muic,
  				  MAX77693_MUIC_REG_CTRL3,
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
235
  				  time << MAX77693_CONTROL3_ADCDBSET_SHIFT);
19d3243e7   Chanwoo Choi   extcon: max77693:...
236
  		if (ret) {
db1b90374   Chanwoo Choi   extcon: MAX77693:...
237
238
  			dev_err(info->dev, "failed to set ADC debounce time
  ");
c2536543d   Sachin Kamat   extcon: max77693:...
239
  			return ret;
19d3243e7   Chanwoo Choi   extcon: max77693:...
240
  		}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
241
242
243
244
  		break;
  	default:
  		dev_err(info->dev, "invalid ADC debounce time
  ");
19d3243e7   Chanwoo Choi   extcon: max77693:...
245
  		return -EINVAL;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
246
  	}
19d3243e7   Chanwoo Choi   extcon: max77693:...
247
  	return 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
248
  };
154f757fd   Chanwoo Choi   extcon: max77693:...
249
250
251
252
253
254
255
256
257
258
  /*
   * 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.
   */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
259
260
261
262
  static int max77693_muic_set_path(struct max77693_muic_info *info,
  		u8 val, bool attached)
  {
  	int ret = 0;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
263
  	unsigned int ctrl1, ctrl2 = 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
264
265
266
267
  
  	if (attached)
  		ctrl1 = val;
  	else
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
268
  		ctrl1 = MAX77693_CONTROL1_SW_OPEN;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
269

d0540f91c   Robert Baldyga   mfd: max77693: Re...
270
271
  	ret = regmap_update_bits(info->max77693->regmap_muic,
  			MAX77693_MUIC_REG_CTRL1, COMP_SW_MASK, ctrl1);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
272
273
274
  	if (ret < 0) {
  		dev_err(info->dev, "failed to update MUIC register
  ");
c2536543d   Sachin Kamat   extcon: max77693:...
275
  		return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
276
277
278
  	}
  
  	if (attached)
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
279
  		ctrl2 |= MAX77693_CONTROL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
280
  	else
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
281
  		ctrl2 |= MAX77693_CONTROL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
282

d0540f91c   Robert Baldyga   mfd: max77693: Re...
283
284
  	ret = regmap_update_bits(info->max77693->regmap_muic,
  			MAX77693_MUIC_REG_CTRL2,
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
285
286
  			MAX77693_CONTROL2_LOWPWR_MASK | MAX77693_CONTROL2_CPEN_MASK,
  			ctrl2);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
287
288
289
  	if (ret < 0) {
  		dev_err(info->dev, "failed to update MUIC register
  ");
c2536543d   Sachin Kamat   extcon: max77693:...
290
  		return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
291
292
293
294
295
296
  	}
  
  	dev_info(info->dev,
  		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s
  ",
  		ctrl1, ctrl2, attached ? "attached" : "detached");
19d3243e7   Chanwoo Choi   extcon: max77693:...
297
298
  
  	return 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
299
  }
154f757fd   Chanwoo Choi   extcon: max77693:...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  /*
   * 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)
db1b90374   Chanwoo Choi   extcon: MAX77693:...
315
  {
154f757fd   Chanwoo Choi   extcon: max77693:...
316
317
318
319
320
321
322
323
324
325
326
327
328
  	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
  		 */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
329
330
  		adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;
  		adc >>= MAX77693_STATUS1_ADC_SHIFT;
154f757fd   Chanwoo Choi   extcon: max77693:...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  
  		/*
  		 * 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
  		 */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
353
354
  		adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;
  		adc >>= MAX77693_STATUS1_ADC_SHIFT;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
355

154f757fd   Chanwoo Choi   extcon: max77693:...
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;
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
368
369
370
371
  			adclow = info->status[0] & MAX77693_STATUS1_ADCLOW_MASK;
  			adclow >>= MAX77693_STATUS1_ADCLOW_SHIFT;
  			adc1k = info->status[0] & MAX77693_STATUS1_ADC1K_MASK;
  			adc1k >>= MAX77693_STATUS1_ADC1K_SHIFT;
154f757fd   Chanwoo Choi   extcon: max77693:...
372

cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
373
374
  			vbvolt = info->status[1] & MAX77693_STATUS2_VBVOLT_MASK;
  			vbvolt >>= MAX77693_STATUS2_VBVOLT_SHIFT;
154f757fd   Chanwoo Choi   extcon: max77693:...
375
376
  
  			/**
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
377
  			 * [0x1|VBVolt|ADCLow|ADC1K]
4c883abee   Jaewon Kim   extcon: max77693:...
378
379
  			 * [0x1|     0|     0|    0] USB_HOST
  			 * [0x1|     1|     0|    0] USB_HSOT_VB
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
380
381
382
  			 * [0x1|     0|     1|    0] Audio Video cable with load
  			 * [0x1|     0|     1|    1] MHL without charging cable
  			 * [0x1|     1|     1|    1] MHL with charging cable
154f757fd   Chanwoo Choi   extcon: max77693:...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  			 */
  			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.
  		 */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
399
400
  		chg_type = info->status[1] & MAX77693_STATUS2_CHGTYP_MASK;
  		chg_type >>= MAX77693_STATUS2_CHGTYP_SHIFT;
154f757fd   Chanwoo Choi   extcon: max77693:...
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
  
  		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
  		 */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
424
425
426
427
  		adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;
  		adc >>= MAX77693_STATUS1_ADC_SHIFT;
  		chg_type = info->status[1] & MAX77693_STATUS2_CHGTYP_MASK;
  		chg_type >>= MAX77693_STATUS2_CHGTYP_SHIFT;
154f757fd   Chanwoo Choi   extcon: max77693:...
428
429
430
431
432
433
434
435
436
437
  
  		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.
db1b90374   Chanwoo Choi   extcon: MAX77693:...
438
  		 */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
439
440
  		vbvolt = info->status[1] & MAX77693_STATUS2_VBVOLT_MASK;
  		vbvolt >>= MAX77693_STATUS2_VBVOLT_SHIFT;
154f757fd   Chanwoo Choi   extcon: max77693:...
441
442
443
444
445
446
447
448
449
450
451
452
  
  		cable_type = vbvolt;
  		break;
  	default:
  		dev_err(info->dev, "Unknown cable group (%d)
  ", group);
  		cable_type = -EINVAL;
  		break;
  	}
  
  	return cable_type;
  }
39bf369e4   Chanwoo Choi   extcon: max77693:...
453
454
455
456
  static int max77693_muic_dock_handler(struct max77693_muic_info *info,
  		int cable_type, bool attached)
  {
  	int ret = 0;
a16262985   Chanwoo Choi   extcon: max77693:...
457
458
  	int vbvolt;
  	bool cable_attached;
73b6ecdb9   Chanwoo Choi   extcon: Redefine ...
459
  	unsigned int dock_id;
39bf369e4   Chanwoo Choi   extcon: max77693:...
460
461
462
463
464
465
466
467
  
  	dev_info(info->dev,
  		"external connector is %s (adc:0x%02x)
  ",
  		attached ? "attached" : "detached", cable_type);
  
  	switch (cable_type) {
  	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
a16262985   Chanwoo Choi   extcon: max77693:...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  		/*
  		 * 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
  ");
  			return 0;
  		}
  
  		/*
d519c423f   Chanwoo Choi   extcon: Unify the...
484
485
  		 * Notify Dock/MHL state.
  		 * - Dock device include three type of cable which
a16262985   Chanwoo Choi   extcon: max77693:...
486
  		 * are HDMI, USB for mouse/keyboard and micro-usb port
d519c423f   Chanwoo Choi   extcon: Unify the...
487
488
489
  		 * for USB/TA cable. Dock device need always exteranl
  		 * power supply(USB/TA cable through micro-usb cable). Dock
  		 * device support screen output of target to separate
a16262985   Chanwoo Choi   extcon: max77693:...
490
491
  		 * monitor and mouse/keyboard for desktop mode.
  		 *
d519c423f   Chanwoo Choi   extcon: Unify the...
492
  		 * Features of 'USB/TA cable with Dock device'
a16262985   Chanwoo Choi   extcon: max77693:...
493
494
495
496
497
498
499
  		 * - 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.
4c883abee   Jaewon Kim   extcon: max77693:...
500
  		 * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)
a16262985   Chanwoo Choi   extcon: max77693:...
501
502
  		 */
  		ret = max77693_muic_set_path(info, info->path_usb, attached);
39bf369e4   Chanwoo Choi   extcon: max77693:...
503
  		if (ret < 0)
a16262985   Chanwoo Choi   extcon: max77693:...
504
  			return ret;
39bf369e4   Chanwoo Choi   extcon: max77693:...
505

8670b4598   Chanwoo Choi   extcon: Use the e...
506
507
  		extcon_set_state_sync(info->edev, EXTCON_DOCK, attached);
  		extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
39bf369e4   Chanwoo Choi   extcon: max77693:...
508
  		goto out;
39bf369e4   Chanwoo Choi   extcon: max77693:...
509
  	case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:	/* Dock-Desk */
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
510
  		dock_id = EXTCON_DOCK;
39bf369e4   Chanwoo Choi   extcon: max77693:...
511
512
  		break;
  	case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
513
  		dock_id = EXTCON_DOCK;
8b45b6a07   Chanwoo Choi   extcon: Add the E...
514
  		if (!attached) {
8670b4598   Chanwoo Choi   extcon: Use the e...
515
516
  			extcon_set_state_sync(info->edev, EXTCON_USB, false);
  			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
8b45b6a07   Chanwoo Choi   extcon: Add the E...
517
518
  						false);
  		}
39bf369e4   Chanwoo Choi   extcon: max77693:...
519
  		break;
19d3243e7   Chanwoo Choi   extcon: max77693:...
520
521
522
523
524
  	default:
  		dev_err(info->dev, "failed to detect %s dock device
  ",
  			attached ? "attached" : "detached");
  		return -EINVAL;
39bf369e4   Chanwoo Choi   extcon: max77693:...
525
526
527
  	}
  
  	/* Dock-Car/Desk/Audio, PATH:AUDIO */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
528
529
  	ret = max77693_muic_set_path(info, MAX77693_CONTROL1_SW_AUDIO,
  					attached);
39bf369e4   Chanwoo Choi   extcon: max77693:...
530
  	if (ret < 0)
a16262985   Chanwoo Choi   extcon: max77693:...
531
  		return ret;
8670b4598   Chanwoo Choi   extcon: Use the e...
532
  	extcon_set_state_sync(info->edev, dock_id, attached);
39bf369e4   Chanwoo Choi   extcon: max77693:...
533
534
  
  out:
a16262985   Chanwoo Choi   extcon: max77693:...
535
  	return 0;
39bf369e4   Chanwoo Choi   extcon: max77693:...
536
537
538
539
540
541
542
  }
  
  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;
39bf369e4   Chanwoo Choi   extcon: max77693:...
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
  
  	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)
  ",
  			attached ? "pressed" : "released", button_type);
19d3243e7   Chanwoo Choi   extcon: max77693:...
573
  		return -EINVAL;
39bf369e4   Chanwoo Choi   extcon: max77693:...
574
575
576
577
  	}
  
  	input_event(dock, EV_KEY, code, attached);
  	input_sync(dock);
39bf369e4   Chanwoo Choi   extcon: max77693:...
578
579
  	return 0;
  }
154f757fd   Chanwoo Choi   extcon: max77693:...
580
581
582
583
584
  static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
  {
  	int cable_type_gnd;
  	int ret = 0;
  	bool attached;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
585

154f757fd   Chanwoo Choi   extcon: max77693:...
586
587
  	cable_type_gnd = max77693_muic_get_cable_type(info,
  				MAX77693_CABLE_GROUP_ADC_GND, &attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
588

154f757fd   Chanwoo Choi   extcon: max77693:...
589
  	switch (cable_type_gnd) {
4c883abee   Jaewon Kim   extcon: max77693:...
590
591
592
  	case MAX77693_MUIC_GND_USB_HOST:
  	case MAX77693_MUIC_GND_USB_HOST_VB:
  		/* USB_HOST, PATH: AP_USB */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
593
594
  		ret = max77693_muic_set_path(info, MAX77693_CONTROL1_SW_USB,
  						attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
595
  		if (ret < 0)
19d3243e7   Chanwoo Choi   extcon: max77693:...
596
  			return ret;
8670b4598   Chanwoo Choi   extcon: Use the e...
597
  		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
598
599
  		break;
  	case MAX77693_MUIC_GND_AV_CABLE_LOAD:
06bed0afa   Chanwoo Choi   extcon: max77693:...
600
  		/* Audio Video Cable with load, PATH:AUDIO */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
601
602
  		ret = max77693_muic_set_path(info, MAX77693_CONTROL1_SW_AUDIO,
  						attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
603
  		if (ret < 0)
19d3243e7   Chanwoo Choi   extcon: max77693:...
604
  			return ret;
8670b4598   Chanwoo Choi   extcon: Use the e...
605
606
  		extcon_set_state_sync(info->edev, EXTCON_USB, attached);
  		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
8b45b6a07   Chanwoo Choi   extcon: Add the E...
607
  					attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
608
  		break;
06bed0afa   Chanwoo Choi   extcon: max77693:...
609
610
611
  	case MAX77693_MUIC_GND_MHL:
  	case MAX77693_MUIC_GND_MHL_VB:
  		/* MHL or MHL with USB/TA cable */
8670b4598   Chanwoo Choi   extcon: Use the e...
612
  		extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
613
614
  		break;
  	default:
19d3243e7   Chanwoo Choi   extcon: max77693:...
615
616
  		dev_err(info->dev, "failed to detect %s cable of gnd type
  ",
db1b90374   Chanwoo Choi   extcon: MAX77693:...
617
  			attached ? "attached" : "detached");
19d3243e7   Chanwoo Choi   extcon: max77693:...
618
  		return -EINVAL;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
619
  	}
19d3243e7   Chanwoo Choi   extcon: max77693:...
620
  	return 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
621
  }
d0587eb79   Chanwoo Choi   extcon: max77693:...
622
623
624
  static int max77693_muic_jig_handler(struct max77693_muic_info *info,
  		int cable_type, bool attached)
  {
d0587eb79   Chanwoo Choi   extcon: max77693:...
625
  	int ret = 0;
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
626
  	u8 path = MAX77693_CONTROL1_SW_OPEN;
d0587eb79   Chanwoo Choi   extcon: max77693:...
627
628
629
630
631
632
633
634
  
  	dev_info(info->dev,
  		"external connector is %s (adc:0x%02x)
  ",
  		attached ? "attached" : "detached", cable_type);
  
  	switch (cable_type) {
  	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:	/* ADC_JIG_USB_OFF */
d0587eb79   Chanwoo Choi   extcon: max77693:...
635
636
  	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:	/* ADC_JIG_USB_ON */
  		/* PATH:AP_USB */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
637
  		path = MAX77693_CONTROL1_SW_USB;
d0587eb79   Chanwoo Choi   extcon: max77693:...
638
639
  		break;
  	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:	/* ADC_JIG_UART_OFF */
c22159a2d   Krzysztof Kozlowski   extcon: max77693:...
640
641
  	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:	/* ADC_JIG_UART_ON */
  		/* PATH:AP_UART */
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
642
  		path = MAX77693_CONTROL1_SW_UART;
c22159a2d   Krzysztof Kozlowski   extcon: max77693:...
643
  		break;
19d3243e7   Chanwoo Choi   extcon: max77693:...
644
645
646
647
648
  	default:
  		dev_err(info->dev, "failed to detect %s jig cable
  ",
  			attached ? "attached" : "detached");
  		return -EINVAL;
d0587eb79   Chanwoo Choi   extcon: max77693:...
649
650
651
652
  	}
  
  	ret = max77693_muic_set_path(info, path, attached);
  	if (ret < 0)
19d3243e7   Chanwoo Choi   extcon: max77693:...
653
  		return ret;
d0587eb79   Chanwoo Choi   extcon: max77693:...
654

8670b4598   Chanwoo Choi   extcon: Use the e...
655
  	extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
19d3243e7   Chanwoo Choi   extcon: max77693:...
656
657
  
  	return 0;
d0587eb79   Chanwoo Choi   extcon: max77693:...
658
  }
154f757fd   Chanwoo Choi   extcon: max77693:...
659
  static int max77693_muic_adc_handler(struct max77693_muic_info *info)
db1b90374   Chanwoo Choi   extcon: MAX77693:...
660
  {
154f757fd   Chanwoo Choi   extcon: max77693:...
661
  	int cable_type;
39bf369e4   Chanwoo Choi   extcon: max77693:...
662
  	int button_type;
154f757fd   Chanwoo Choi   extcon: max77693:...
663
  	bool attached;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
664
  	int ret = 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
665

154f757fd   Chanwoo Choi   extcon: max77693:...
666
667
668
  	/* Check accessory state which is either detached or attached */
  	cable_type = max77693_muic_get_cable_type(info,
  				MAX77693_CABLE_GROUP_ADC, &attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
669
670
671
672
  
  	dev_info(info->dev,
  		"external connector is %s (adc:0x%02x, prev_adc:0x%x)
  ",
154f757fd   Chanwoo Choi   extcon: max77693:...
673
674
  		attached ? "attached" : "detached", cable_type,
  		info->prev_cable_type);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
675

154f757fd   Chanwoo Choi   extcon: max77693:...
676
  	switch (cable_type) {
db1b90374   Chanwoo Choi   extcon: MAX77693:...
677
  	case MAX77693_MUIC_ADC_GROUND:
4c883abee   Jaewon Kim   extcon: max77693:...
678
  		/* USB_HOST/MHL/Audio */
154f757fd   Chanwoo Choi   extcon: max77693:...
679
  		max77693_muic_adc_ground_handler(info);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
680
681
682
  		break;
  	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
  	case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
683
  	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
c22159a2d   Krzysztof Kozlowski   extcon: max77693:...
684
  	case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
685
  		/* JIG */
d0587eb79   Chanwoo Choi   extcon: max77693:...
686
  		ret = max77693_muic_jig_handler(info, cable_type, attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
687
  		if (ret < 0)
19d3243e7   Chanwoo Choi   extcon: max77693:...
688
  			return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
689
  		break;
39bf369e4   Chanwoo Choi   extcon: max77693:...
690
  	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */
39bf369e4   Chanwoo Choi   extcon: max77693:...
691
692
693
694
695
696
697
698
699
700
701
702
703
  	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)
19d3243e7   Chanwoo Choi   extcon: max77693:...
704
  			return ret;
39bf369e4   Chanwoo Choi   extcon: max77693:...
705
  		break;
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
706
707
708
709
710
  	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 */
39bf369e4   Chanwoo Choi   extcon: max77693:...
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
  		/*
  		 * 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);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
728
  		if (ret < 0)
19d3243e7   Chanwoo Choi   extcon: max77693:...
729
  			return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
730
731
732
733
  		break;
  	case MAX77693_MUIC_ADC_SEND_END_BUTTON:
  	case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
  	case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
734
735
736
  	case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
  	case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
  	case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
737
  	case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
738
  	case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
739
740
  	case MAX77693_MUIC_ADC_RESERVED_ACC_1:
  	case MAX77693_MUIC_ADC_RESERVED_ACC_2:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
741
742
743
744
745
746
747
  	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:
db1b90374   Chanwoo Choi   extcon: MAX77693:...
748
  	case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
39bf369e4   Chanwoo Choi   extcon: max77693:...
749
750
751
752
753
  		/*
  		 * 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.
  		 */
db1b90374   Chanwoo Choi   extcon: MAX77693:...
754
755
756
  		dev_info(info->dev,
  			"accessory is %s but it isn't used (adc:0x%x)
  ",
154f757fd   Chanwoo Choi   extcon: max77693:...
757
  			attached ? "attached" : "detached", cable_type);
19d3243e7   Chanwoo Choi   extcon: max77693:...
758
  		return -EAGAIN;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
759
760
761
762
  	default:
  		dev_err(info->dev,
  			"failed to detect %s accessory (adc:0x%x)
  ",
154f757fd   Chanwoo Choi   extcon: max77693:...
763
  			attached ? "attached" : "detached", cable_type);
19d3243e7   Chanwoo Choi   extcon: max77693:...
764
  		return -EINVAL;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
765
  	}
19d3243e7   Chanwoo Choi   extcon: max77693:...
766
  	return 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
767
  }
154f757fd   Chanwoo Choi   extcon: max77693:...
768
  static int max77693_muic_chg_handler(struct max77693_muic_info *info)
db1b90374   Chanwoo Choi   extcon: MAX77693:...
769
  {
db1b90374   Chanwoo Choi   extcon: MAX77693:...
770
  	int chg_type;
06bed0afa   Chanwoo Choi   extcon: max77693:...
771
  	int cable_type_gnd;
39bf369e4   Chanwoo Choi   extcon: max77693:...
772
  	int cable_type;
154f757fd   Chanwoo Choi   extcon: max77693:...
773
  	bool attached;
06bed0afa   Chanwoo Choi   extcon: max77693:...
774
  	bool cable_attached;
154f757fd   Chanwoo Choi   extcon: max77693:...
775
  	int ret = 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
776

154f757fd   Chanwoo Choi   extcon: max77693:...
777
778
  	chg_type = max77693_muic_get_cable_type(info,
  				MAX77693_CABLE_GROUP_CHG, &attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
779
780
781
782
783
  
  	dev_info(info->dev,
  		"external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)
  ",
  			attached ? "attached" : "detached",
154f757fd   Chanwoo Choi   extcon: max77693:...
784
  			chg_type, info->prev_chg_type);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
785
786
787
  
  	switch (chg_type) {
  	case MAX77693_CHARGER_TYPE_USB:
a16262985   Chanwoo Choi   extcon: max77693:...
788
  	case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
0e2738f59   Chanwoo Choi   extcon: max77693:...
789
  	case MAX77693_CHARGER_TYPE_NONE:
a16262985   Chanwoo Choi   extcon: max77693:...
790
  		/* Check MAX77693_CABLE_GROUP_ADC_GND type */
06bed0afa   Chanwoo Choi   extcon: max77693:...
791
792
793
  		cable_type_gnd = max77693_muic_get_cable_type(info,
  					MAX77693_CABLE_GROUP_ADC_GND,
  					&cable_attached);
a16262985   Chanwoo Choi   extcon: max77693:...
794
795
796
797
  		switch (cable_type_gnd) {
  		case MAX77693_MUIC_GND_MHL:
  		case MAX77693_MUIC_GND_MHL_VB:
  			/*
942e0239d   Chanwoo Choi   extcon: Alter MHL...
798
  			 * MHL cable with USB/TA cable
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
799
800
  			 * - MHL cable include two port(HDMI line and separate
  			 * micro-usb port. When the target connect MHL cable,
942e0239d   Chanwoo Choi   extcon: Alter MHL...
801
802
  			 * extcon driver check whether USB/TA cable is
  			 * connected. If USB/TA cable is connected, extcon
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
803
  			 * driver notify state to notifiee for charging battery.
a16262985   Chanwoo Choi   extcon: max77693:...
804
  			 *
942e0239d   Chanwoo Choi   extcon: Alter MHL...
805
  			 * Features of 'USB/TA with MHL cable'
a16262985   Chanwoo Choi   extcon: max77693:...
806
  			 * - Support MHL
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
807
808
  			 * - Support charging through micro-usb port without
  			 *   data connection
a16262985   Chanwoo Choi   extcon: max77693:...
809
  			 */
8670b4598   Chanwoo Choi   extcon: Use the e...
810
  			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
11eecf910   Chanwoo Choi   extcon: Modify th...
811
  						attached);
4243c408f   Maciej Purski   extcon: max77693:...
812
813
  			extcon_set_state_sync(info->edev, EXTCON_DISP_MHL,
  						cable_attached);
a16262985   Chanwoo Choi   extcon: max77693:...
814
  			break;
39bf369e4   Chanwoo Choi   extcon: max77693:...
815
  		}
a16262985   Chanwoo Choi   extcon: max77693:...
816
  		/* Check MAX77693_CABLE_GROUP_ADC type */
39bf369e4   Chanwoo Choi   extcon: max77693:...
817
818
819
  		cable_type = max77693_muic_get_cable_type(info,
  					MAX77693_CABLE_GROUP_ADC,
  					&cable_attached);
a16262985   Chanwoo Choi   extcon: max77693:...
820
821
822
823
  		switch (cable_type) {
  		case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:		/* Dock-Audio */
  			/*
  			 * Dock-Audio device with USB/TA cable
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
824
825
826
827
828
  			 * - 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
  			 * or not. If USB/TA cable is connected, extcon driver
  			 * notify state to notifiee for charging battery.
a16262985   Chanwoo Choi   extcon: max77693:...
829
830
831
  			 *
  			 * Features of 'USB/TA cable with Dock-Audio device'
  			 * - Support external output feature of audio.
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
832
833
  			 * - Support charging through micro-usb port without
  			 *   data connection.
a16262985   Chanwoo Choi   extcon: max77693:...
834
  			 */
8670b4598   Chanwoo Choi   extcon: Use the e...
835
  			extcon_set_state_sync(info->edev, EXTCON_USB,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
836
  						attached);
8670b4598   Chanwoo Choi   extcon: Use the e...
837
  			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
8b45b6a07   Chanwoo Choi   extcon: Add the E...
838
  						attached);
39bf369e4   Chanwoo Choi   extcon: max77693:...
839
840
  
  			if (!cable_attached)
8670b4598   Chanwoo Choi   extcon: Use the e...
841
  				extcon_set_state_sync(info->edev, EXTCON_DOCK,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
842
  							cable_attached);
a16262985   Chanwoo Choi   extcon: max77693:...
843
844
845
846
847
848
  			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
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
849
850
851
852
853
  			 * 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.
a16262985   Chanwoo Choi   extcon: max77693:...
854
855
856
857
  			 *
  			 * Features of 'USB/TA cable with Dock-Smart device'
  			 * - Support MHL
  			 * - Support external output feature of audio
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
858
859
860
861
862
  			 * - 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
4c883abee   Jaewon Kim   extcon: max77693:...
863
  			 * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)
a16262985   Chanwoo Choi   extcon: max77693:...
864
  			 */
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
865
866
  			ret = max77693_muic_set_path(info, info->path_usb,
  						    attached);
a16262985   Chanwoo Choi   extcon: max77693:...
867
868
  			if (ret < 0)
  				return ret;
8670b4598   Chanwoo Choi   extcon: Use the e...
869
  			extcon_set_state_sync(info->edev, EXTCON_DOCK,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
870
  						attached);
8670b4598   Chanwoo Choi   extcon: Use the e...
871
  			extcon_set_state_sync(info->edev, EXTCON_DISP_MHL,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
872
  						attached);
a16262985   Chanwoo Choi   extcon: max77693:...
873
  			break;
06bed0afa   Chanwoo Choi   extcon: max77693:...
874
  		}
39bf369e4   Chanwoo Choi   extcon: max77693:...
875

a16262985   Chanwoo Choi   extcon: max77693:...
876
877
878
879
  		/* Check MAX77693_CABLE_GROUP_CHG type */
  		switch (chg_type) {
  		case MAX77693_CHARGER_TYPE_NONE:
  			/*
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
880
881
882
883
884
885
886
887
  			 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA
  			 * cable is attached, muic device happen below two irq.
  			 * - '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
  			 * irq than MAX77693_MUIC_IRQ_INT2_CHGTYP irq.
a16262985   Chanwoo Choi   extcon: max77693:...
888
  			 *
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
889
890
891
892
893
894
895
  			 * If user attach MHL (with USB/TA cable and immediately
  			 * detach MHL with USB/TA cable before MAX77693_MUIC_IRQ
  			 * _INT2_CHGTYP irq 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.
a16262985   Chanwoo Choi   extcon: max77693:...
896
  			 */
0e2738f59   Chanwoo Choi   extcon: max77693:...
897
  			break;
a16262985   Chanwoo Choi   extcon: max77693:...
898
899
  		case MAX77693_CHARGER_TYPE_USB:
  			/* Only USB cable, PATH:AP_USB */
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
900
901
  			ret = max77693_muic_set_path(info, info->path_usb,
  						    attached);
a16262985   Chanwoo Choi   extcon: max77693:...
902
903
  			if (ret < 0)
  				return ret;
0e2738f59   Chanwoo Choi   extcon: max77693:...
904

8670b4598   Chanwoo Choi   extcon: Use the e...
905
  			extcon_set_state_sync(info->edev, EXTCON_USB,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
906
  						attached);
8670b4598   Chanwoo Choi   extcon: Use the e...
907
  			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
8b45b6a07   Chanwoo Choi   extcon: Add the E...
908
  						attached);
a16262985   Chanwoo Choi   extcon: max77693:...
909
910
911
  			break;
  		case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
  			/* Only TA cable */
8670b4598   Chanwoo Choi   extcon: Use the e...
912
  			extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
11eecf910   Chanwoo Choi   extcon: Modify th...
913
  						attached);
a16262985   Chanwoo Choi   extcon: max77693:...
914
915
  			break;
  		}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
916
917
  		break;
  	case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
8670b4598   Chanwoo Choi   extcon: Use the e...
918
  		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
919
  					attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
920
  		break;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
921
  	case MAX77693_CHARGER_TYPE_APPLE_500MA:
8670b4598   Chanwoo Choi   extcon: Use the e...
922
  		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
923
  					attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
924
925
  		break;
  	case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
8670b4598   Chanwoo Choi   extcon: Use the e...
926
  		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
2a9de9c0f   Chanwoo Choi   extcon: Use the u...
927
  					attached);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
928
929
930
931
932
933
934
935
  		break;
  	case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
  		break;
  	default:
  		dev_err(info->dev,
  			"failed to detect %s accessory (chg_type:0x%x)
  ",
  			attached ? "attached" : "detached", chg_type);
a16262985   Chanwoo Choi   extcon: max77693:...
936
  		return -EINVAL;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
937
  	}
a16262985   Chanwoo Choi   extcon: max77693:...
938
  	return 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
939
940
941
942
943
944
  }
  
  static void max77693_muic_irq_work(struct work_struct *work)
  {
  	struct max77693_muic_info *info = container_of(work,
  			struct max77693_muic_info, irq_work);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
945
946
  	int irq_type = -1;
  	int i, ret = 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
947
948
949
950
951
  
  	if (!info->edev)
  		return;
  
  	mutex_lock(&info->mutex);
a33411b26   Sachin Kamat   extcon: max77693:...
952
  	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
db1b90374   Chanwoo Choi   extcon: MAX77693:...
953
954
  		if (info->irq == muic_irqs[i].virq)
  			irq_type = muic_irqs[i].irq;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
955
956
  	ret = regmap_bulk_read(info->max77693->regmap_muic,
  			MAX77693_MUIC_REG_STATUS1, info->status, 2);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
957
958
959
960
961
962
963
964
965
966
967
968
  	if (ret) {
  		dev_err(info->dev, "failed to read MUIC register
  ");
  		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:
af57fa4de   Srikant Ritolia   extcon: Restructu...
969
970
971
972
  		/*
  		 * Handle all of accessory except for
  		 * type of charger accessory.
  		 */
154f757fd   Chanwoo Choi   extcon: max77693:...
973
  		ret = max77693_muic_adc_handler(info);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
974
975
976
977
978
979
980
981
  		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 */
154f757fd   Chanwoo Choi   extcon: max77693:...
982
  		ret = max77693_muic_chg_handler(info);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
983
984
985
986
987
988
989
990
991
992
993
994
  		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
  ",
  				irq_type);
19d3243e7   Chanwoo Choi   extcon: max77693:...
995
996
  		mutex_unlock(&info->mutex);
  		return;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
997
998
999
1000
1001
1002
1003
  	}
  
  	if (ret < 0)
  		dev_err(info->dev, "failed to handle MUIC interrupt
  ");
  
  	mutex_unlock(&info->mutex);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  }
  
  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;
  }
659489002   Krzysztof Kozlowski   extcon: max77693:...
1015
  static const struct regmap_config max77693_muic_regmap_config = {
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1016
1017
1018
1019
1020
1021
1022
  	.reg_bits = 8,
  	.val_bits = 8,
  };
  
  static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
  {
  	int ret = 0;
154f757fd   Chanwoo Choi   extcon: max77693:...
1023
1024
1025
  	int adc;
  	int chg_type;
  	bool attached;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1026
1027
1028
1029
  
  	mutex_lock(&info->mutex);
  
  	/* Read STATUSx register to detect accessory */
d0540f91c   Robert Baldyga   mfd: max77693: Re...
1030
1031
  	ret = regmap_bulk_read(info->max77693->regmap_muic,
  			MAX77693_MUIC_REG_STATUS1, info->status, 2);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1032
1033
1034
1035
  	if (ret) {
  		dev_err(info->dev, "failed to read MUIC register
  ");
  		mutex_unlock(&info->mutex);
c2536543d   Sachin Kamat   extcon: max77693:...
1036
  		return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1037
  	}
154f757fd   Chanwoo Choi   extcon: max77693:...
1038
1039
1040
1041
  	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);
19d3243e7   Chanwoo Choi   extcon: max77693:...
1042
  		if (ret < 0) {
154f757fd   Chanwoo Choi   extcon: max77693:...
1043
1044
  			dev_err(info->dev, "Cannot detect accessory
  ");
19d3243e7   Chanwoo Choi   extcon: max77693:...
1045
1046
1047
  			mutex_unlock(&info->mutex);
  			return ret;
  		}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1048
  	}
154f757fd   Chanwoo Choi   extcon: max77693:...
1049
1050
1051
1052
  	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);
19d3243e7   Chanwoo Choi   extcon: max77693:...
1053
  		if (ret < 0) {
154f757fd   Chanwoo Choi   extcon: max77693:...
1054
1055
  			dev_err(info->dev, "Cannot detect charger accessory
  ");
19d3243e7   Chanwoo Choi   extcon: max77693:...
1056
1057
1058
  			mutex_unlock(&info->mutex);
  			return ret;
  		}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1059
  	}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1060
  	mutex_unlock(&info->mutex);
154f757fd   Chanwoo Choi   extcon: max77693:...
1061

19d3243e7   Chanwoo Choi   extcon: max77693:...
1062
  	return 0;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1063
  }
297620fd1   Chanwoo Choi   extcon: max77693:...
1064
1065
1066
1067
1068
1069
1070
  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);
  }
44f34fd4a   Bill Pemberton   extcon: remove us...
1071
  static int max77693_muic_probe(struct platform_device *pdev)
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1072
1073
  {
  	struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
f8457d574   Chanwoo Choi   extcon: MAX77693:...
1074
  	struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1075
  	struct max77693_muic_info *info;
0ec83bd24   Chanwoo Choi   extcon: max77693:...
1076
1077
  	struct max77693_reg_data *init_data;
  	int num_init_data;
297620fd1   Chanwoo Choi   extcon: max77693:...
1078
1079
1080
  	int delay_jiffies;
  	int ret;
  	int i;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
1081
  	unsigned int id;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1082

f4bb5cb54   Sachin Kamat   extcon: max77693:...
1083
1084
  	info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
  				   GFP_KERNEL);
0a16ee633   Jingoo Han   extcon: Remove un...
1085
  	if (!info)
f4bb5cb54   Sachin Kamat   extcon: max77693:...
1086
  		return -ENOMEM;
0a16ee633   Jingoo Han   extcon: Remove un...
1087

db1b90374   Chanwoo Choi   extcon: MAX77693:...
1088
1089
  	info->dev = &pdev->dev;
  	info->max77693 = max77693;
1967fa08d   Sachin Kamat   extcon: max77693:...
1090
  	if (info->max77693->regmap_muic) {
b186b1248   Chanwoo Choi   mfd: MAX77693: Fi...
1091
1092
  		dev_dbg(&pdev->dev, "allocate register map
  ");
1967fa08d   Sachin Kamat   extcon: max77693:...
1093
  	} else {
b186b1248   Chanwoo Choi   mfd: MAX77693: Fi...
1094
  		info->max77693->regmap_muic = devm_regmap_init_i2c(
61b305cd2   Krzysztof Kozlowski   drivers: max77693...
1095
  						info->max77693->i2c_muic,
b186b1248   Chanwoo Choi   mfd: MAX77693: Fi...
1096
1097
1098
1099
1100
1101
  						&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
  ", ret);
3bf742ffd   Sachin Kamat   extcon: max77693:...
1102
  			return ret;
b186b1248   Chanwoo Choi   mfd: MAX77693: Fi...
1103
  		}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1104
  	}
39bf369e4   Chanwoo Choi   extcon: max77693:...
1105
1106
  
  	/* Register input device for button of dock device */
eff7d74f5   Chanwoo Choi   extcon: max77693:...
1107
  	info->dock = devm_input_allocate_device(&pdev->dev);
39bf369e4   Chanwoo Choi   extcon: max77693:...
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
  	if (!info->dock) {
  		dev_err(&pdev->dev, "%s: failed to allocate input
  ", __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)
  ",
  				ret);
  		return ret;
  	}
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1132
1133
1134
1135
1136
1137
1138
1139
  	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];
cbc46603d   Andrzej Hajda   extcon: max77693:...
1140
  		int virq;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1141

342d669c1   Robert Baldyga   mfd: max77693: Ha...
1142
1143
  		virq = regmap_irq_get_virq(max77693->irq_data_muic,
  					muic_irq->irq);
cbc46603d   Andrzej Hajda   extcon: max77693:...
1144
  		if (virq <= 0)
d71552317   Krzysztof Kozlowski   extcon: max77693:...
1145
  			return -EINVAL;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1146
  		muic_irq->virq = virq;
d71552317   Krzysztof Kozlowski   extcon: max77693:...
1147
  		ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1148
  				max77693_muic_irq_handler,
ae3b3215f   Chanwoo Choi   extcon: max8997/m...
1149
1150
  				IRQF_NO_SUSPEND,
  				muic_irq->name, info);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1151
1152
  		if (ret) {
  			dev_err(&pdev->dev,
34825e511   Chanwoo Choi   extcon: Fix the c...
1153
1154
  				"failed: irq request (IRQ: %d, error :%d)
  ",
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1155
  				muic_irq->irq, ret);
d71552317   Krzysztof Kozlowski   extcon: max77693:...
1156
  			return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1157
1158
1159
1160
  		}
  	}
  
  	/* Initialize extcon device */
577bef110   Chanwoo Choi   extcon: max77693:...
1161
1162
1163
  	info->edev = devm_extcon_dev_allocate(&pdev->dev,
  					      max77693_extcon_cable);
  	if (IS_ERR(info->edev)) {
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1164
1165
  		dev_err(&pdev->dev, "failed to allocate memory for extcon
  ");
d71552317   Krzysztof Kozlowski   extcon: max77693:...
1166
  		return -ENOMEM;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1167
  	}
577bef110   Chanwoo Choi   extcon: max77693:...
1168

10fae1184   Sangjung Woo   extcon: max77693:...
1169
  	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1170
1171
1172
  	if (ret) {
  		dev_err(&pdev->dev, "failed to register extcon device
  ");
d71552317   Krzysztof Kozlowski   extcon: max77693:...
1173
  		return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1174
  	}
0ec83bd24   Chanwoo Choi   extcon: max77693:...
1175
  	/* Initialize MUIC register by using platform data or default data */
d5653f2b7   Krzysztof Kozlowski   extcon: max77693:...
1176
  	if (pdata && pdata->muic_data) {
0ec83bd24   Chanwoo Choi   extcon: max77693:...
1177
1178
1179
1180
1181
1182
  		init_data = pdata->muic_data->init_data;
  		num_init_data = pdata->muic_data->num_init_data;
  	} else {
  		init_data = default_init_data;
  		num_init_data = ARRAY_SIZE(default_init_data);
  	}
a33411b26   Sachin Kamat   extcon: max77693:...
1183
  	for (i = 0; i < num_init_data; i++) {
d0540f91c   Robert Baldyga   mfd: max77693: Re...
1184
  		regmap_write(info->max77693->regmap_muic,
0ec83bd24   Chanwoo Choi   extcon: max77693:...
1185
1186
  				init_data[i].addr,
  				init_data[i].data);
0ec83bd24   Chanwoo Choi   extcon: max77693:...
1187
  	}
d5653f2b7   Krzysztof Kozlowski   extcon: max77693:...
1188
  	if (pdata && pdata->muic_data) {
c2275d2fa   Chanwoo Choi   extcon: Fix up 80...
1189
1190
  		struct max77693_muic_platform_data *muic_pdata
  						   = pdata->muic_data;
0ec83bd24   Chanwoo Choi   extcon: max77693:...
1191

190d7cfc8   Chanwoo Choi   extcon: max77693:...
1192
1193
1194
1195
1196
1197
1198
  		/*
  		 * 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
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
1199
  			info->path_uart = MAX77693_CONTROL1_SW_UART;
f8457d574   Chanwoo Choi   extcon: MAX77693:...
1200

190d7cfc8   Chanwoo Choi   extcon: max77693:...
1201
1202
1203
  		if (muic_pdata->path_usb)
  			info->path_usb = muic_pdata->path_usb;
  		else
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
1204
  			info->path_usb = MAX77693_CONTROL1_SW_USB;
2b75799f5   Chanwoo Choi   extcon: max77693:...
1205

190d7cfc8   Chanwoo Choi   extcon: max77693:...
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
  		/*
  		 * 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 {
cceb433a1   Krzysztof Kozlowski   mfd/extcon: max77...
1216
1217
  		info->path_usb = MAX77693_CONTROL1_SW_USB;
  		info->path_uart = MAX77693_CONTROL1_SW_UART;
190d7cfc8   Chanwoo Choi   extcon: max77693:...
1218
1219
  		delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
  	}
2b75799f5   Chanwoo Choi   extcon: max77693:...
1220
1221
1222
  
  	/* Set initial path for UART */
  	 max77693_muic_set_path(info, info->path_uart, true);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1223
  	/* Check revision number of MUIC device*/
d0540f91c   Robert Baldyga   mfd: max77693: Re...
1224
  	ret = regmap_read(info->max77693->regmap_muic,
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1225
1226
1227
1228
  			MAX77693_MUIC_REG_ID, &id);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "failed to read revision number
  ");
d71552317   Krzysztof Kozlowski   extcon: max77693:...
1229
  		return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1230
1231
1232
1233
1234
1235
  	}
  	dev_info(info->dev, "device ID : 0x%x
  ", id);
  
  	/* Set ADC debounce time */
  	max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
297620fd1   Chanwoo Choi   extcon: max77693:...
1236
1237
1238
1239
1240
1241
1242
1243
1244
  	/*
  	 * 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);
b8629411b   Krzysztof Kozlowski   extcon: max77693:...
1245
1246
  	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  			delay_jiffies);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1247
1248
  
  	return ret;
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1249
  }
93ed03278   Bill Pemberton   extcon: remove us...
1250
  static int max77693_muic_remove(struct platform_device *pdev)
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1251
1252
  {
  	struct max77693_muic_info *info = platform_get_drvdata(pdev);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1253

db1b90374   Chanwoo Choi   extcon: MAX77693:...
1254
  	cancel_work_sync(&info->irq_work);
39bf369e4   Chanwoo Choi   extcon: max77693:...
1255
  	input_unregister_device(info->dock);
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1256
1257
1258
1259
1260
1261
1262
  
  	return 0;
  }
  
  static struct platform_driver max77693_muic_driver = {
  	.driver		= {
  		.name	= DEV_NAME,
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1263
1264
  	},
  	.probe		= max77693_muic_probe,
5f7e22283   Bill Pemberton   extcon: remove us...
1265
  	.remove		= max77693_muic_remove,
db1b90374   Chanwoo Choi   extcon: MAX77693:...
1266
1267
1268
1269
1270
1271
1272
1273
  };
  
  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");