blob: fea10624f3e5580f53735f25e11b9278056a4a63 [file] [log] [blame]
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001/*
2 * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
3 *
4 * Copyright (C) 2012 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/slab.h>
Chanwoo Choi39bf3692012-12-03 13:09:41 +090022#include <linux/input.h>
Chanwoo Choidb1b9032012-07-17 13:28:28 +090023#include <linux/interrupt.h>
24#include <linux/err.h>
25#include <linux/platform_device.h>
26#include <linux/mfd/max77693.h>
27#include <linux/mfd/max77693-private.h>
28#include <linux/extcon.h>
29#include <linux/regmap.h>
30#include <linux/irqdomain.h>
31
32#define DEV_NAME "max77693-muic"
Chanwoo Choi297620f2012-12-26 13:10:11 +090033#define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
Chanwoo Choidb1b9032012-07-17 13:28:28 +090034
Chanwoo Choidb1b9032012-07-17 13:28:28 +090035enum max77693_muic_adc_debounce_time {
36 ADC_DEBOUNCE_TIME_5MS = 0,
37 ADC_DEBOUNCE_TIME_10MS,
38 ADC_DEBOUNCE_TIME_25MS,
39 ADC_DEBOUNCE_TIME_38_62MS,
40};
41
42struct max77693_muic_info {
43 struct device *dev;
44 struct max77693_dev *max77693;
45 struct extcon_dev *edev;
Chanwoo Choi154f757f2012-11-27 09:40:32 +090046 int prev_cable_type;
47 int prev_cable_type_gnd;
Chanwoo Choidb1b9032012-07-17 13:28:28 +090048 int prev_chg_type;
Chanwoo Choi39bf3692012-12-03 13:09:41 +090049 int prev_button_type;
Chanwoo Choidb1b9032012-07-17 13:28:28 +090050 u8 status[2];
51
52 int irq;
53 struct work_struct irq_work;
54 struct mutex mutex;
Chanwoo Choi39bf3692012-12-03 13:09:41 +090055
Chanwoo Choi297620f2012-12-26 13:10:11 +090056 /*
57 * Use delayed workqueue to detect cable state and then
58 * notify cable state to notifiee/platform through uevent.
59 * After completing the booting of platform, the extcon provider
60 * driver should notify cable state to upper layer.
61 */
62 struct delayed_work wq_detcable;
63
Chanwoo Choi39bf3692012-12-03 13:09:41 +090064 /* Button of dock device */
65 struct input_dev *dock;
Chanwoo Choi2b757992012-12-06 21:27:56 +090066
67 /*
68 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
69 * h/w path of COMP2/COMN1 on CONTROL1 register.
70 */
71 int path_usb;
72 int path_uart;
Chanwoo Choidb1b9032012-07-17 13:28:28 +090073};
74
Chanwoo Choi154f757f2012-11-27 09:40:32 +090075enum max77693_muic_cable_group {
76 MAX77693_CABLE_GROUP_ADC = 0,
77 MAX77693_CABLE_GROUP_ADC_GND,
78 MAX77693_CABLE_GROUP_CHG,
79 MAX77693_CABLE_GROUP_VBVOLT,
80};
81
Chanwoo Choidb1b9032012-07-17 13:28:28 +090082enum max77693_muic_charger_type {
83 MAX77693_CHARGER_TYPE_NONE = 0,
84 MAX77693_CHARGER_TYPE_USB,
85 MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
86 MAX77693_CHARGER_TYPE_DEDICATED_CHG,
87 MAX77693_CHARGER_TYPE_APPLE_500MA,
88 MAX77693_CHARGER_TYPE_APPLE_1A_2A,
89 MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
90};
91
92/**
93 * struct max77693_muic_irq
94 * @irq: the index of irq list of MUIC device.
95 * @name: the name of irq.
96 * @virq: the virtual irq to use irq domain
97 */
98struct max77693_muic_irq {
99 unsigned int irq;
100 const char *name;
101 unsigned int virq;
102};
103
104static struct max77693_muic_irq muic_irqs[] = {
105 { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" },
106 { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" },
107 { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" },
108 { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" },
109 { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
110 { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" },
111 { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
112 { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" },
113 { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
114 { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" },
115 { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" },
116 { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" },
117 { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" },
118 { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" },
119 { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" },
120 { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" },
121};
122
123/* Define supported accessory type */
124enum max77693_muic_acc_type {
125 MAX77693_MUIC_ADC_GROUND = 0x0,
126 MAX77693_MUIC_ADC_SEND_END_BUTTON,
127 MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
128 MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
129 MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
130 MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
131 MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
132 MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
133 MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
134 MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
135 MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
136 MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
137 MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
138 MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
139 MAX77693_MUIC_ADC_RESERVED_ACC_1,
140 MAX77693_MUIC_ADC_RESERVED_ACC_2,
141 MAX77693_MUIC_ADC_RESERVED_ACC_3,
142 MAX77693_MUIC_ADC_RESERVED_ACC_4,
143 MAX77693_MUIC_ADC_RESERVED_ACC_5,
144 MAX77693_MUIC_ADC_CEA936_AUDIO,
145 MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
146 MAX77693_MUIC_ADC_TTY_CONVERTER,
147 MAX77693_MUIC_ADC_UART_CABLE,
148 MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
149 MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
150 MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
151 MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
152 MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
153 MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
154 MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
155 MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
156 MAX77693_MUIC_ADC_OPEN,
157
158 /* The below accessories have same ADC value so ADCLow and
159 ADC1K bit is used to separate specific accessory */
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900160 MAX77693_MUIC_GND_USB_OTG = 0x100, /* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */
161 MAX77693_MUIC_GND_USB_OTG_VB = 0x104, /* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */
162 MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */
163 MAX77693_MUIC_GND_MHL = 0x103, /* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */
164 MAX77693_MUIC_GND_MHL_VB = 0x107, /* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900165};
166
167/* MAX77693 MUIC device support below list of accessories(external connector) */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900168enum {
169 EXTCON_CABLE_USB = 0,
170 EXTCON_CABLE_USB_HOST,
171 EXTCON_CABLE_TA,
172 EXTCON_CABLE_FAST_CHARGER,
173 EXTCON_CABLE_SLOW_CHARGER,
174 EXTCON_CABLE_CHARGE_DOWNSTREAM,
175 EXTCON_CABLE_MHL,
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900176 EXTCON_CABLE_MHL_TA,
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900177 EXTCON_CABLE_JIG_USB_ON,
178 EXTCON_CABLE_JIG_USB_OFF,
179 EXTCON_CABLE_JIG_UART_OFF,
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900180 EXTCON_CABLE_JIG_UART_ON,
181 EXTCON_CABLE_DOCK_SMART,
182 EXTCON_CABLE_DOCK_DESK,
183 EXTCON_CABLE_DOCK_AUDIO,
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900184
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900185 _EXTCON_CABLE_NUM,
186};
187
Sachin Kamat45d4a4e2013-01-31 09:31:47 +0900188static const char *max77693_extcon_cable[] = {
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900189 [EXTCON_CABLE_USB] = "USB",
190 [EXTCON_CABLE_USB_HOST] = "USB-Host",
191 [EXTCON_CABLE_TA] = "TA",
192 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
193 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
194 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
195 [EXTCON_CABLE_MHL] = "MHL",
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900196 [EXTCON_CABLE_MHL_TA] = "MHL_TA",
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900197 [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON",
198 [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF",
199 [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF",
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900200 [EXTCON_CABLE_JIG_UART_ON] = "Dock-Car",
201 [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart",
202 [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
203 [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio",
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900204
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900205 NULL,
206};
207
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900208/*
209 * max77693_muic_set_debounce_time - Set the debounce time of ADC
210 * @info: the instance including private data of max77693 MUIC
211 * @time: the debounce time of ADC
212 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900213static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
214 enum max77693_muic_adc_debounce_time time)
215{
Axel Linbf2627d2012-10-04 09:55:23 +0900216 int ret;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900217
218 switch (time) {
219 case ADC_DEBOUNCE_TIME_5MS:
220 case ADC_DEBOUNCE_TIME_10MS:
221 case ADC_DEBOUNCE_TIME_25MS:
222 case ADC_DEBOUNCE_TIME_38_62MS:
Axel Linbf2627d2012-10-04 09:55:23 +0900223 ret = max77693_update_reg(info->max77693->regmap_muic,
224 MAX77693_MUIC_REG_CTRL3,
225 time << CONTROL3_ADCDBSET_SHIFT,
226 CONTROL3_ADCDBSET_MASK);
Chanwoo Choi19d32432013-02-13 18:35:08 +0900227 if (ret) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900228 dev_err(info->dev, "failed to set ADC debounce time\n");
Chanwoo Choi19d32432013-02-13 18:35:08 +0900229 return -EAGAIN;
230 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900231 break;
232 default:
233 dev_err(info->dev, "invalid ADC debounce time\n");
Chanwoo Choi19d32432013-02-13 18:35:08 +0900234 return -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900235 }
236
Chanwoo Choi19d32432013-02-13 18:35:08 +0900237 return 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900238};
239
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900240/*
241 * max77693_muic_set_path - Set hardware line according to attached cable
242 * @info: the instance including private data of max77693 MUIC
243 * @value: the path according to attached cable
244 * @attached: the state of cable (true:attached, false:detached)
245 *
246 * The max77693 MUIC device share outside H/W line among a varity of cables
247 * so, this function set internal path of H/W line according to the type of
248 * attached cable.
249 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900250static int max77693_muic_set_path(struct max77693_muic_info *info,
251 u8 val, bool attached)
252{
253 int ret = 0;
254 u8 ctrl1, ctrl2 = 0;
255
256 if (attached)
257 ctrl1 = val;
258 else
259 ctrl1 = CONTROL1_SW_OPEN;
260
261 ret = max77693_update_reg(info->max77693->regmap_muic,
262 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
263 if (ret < 0) {
264 dev_err(info->dev, "failed to update MUIC register\n");
Chanwoo Choi19d32432013-02-13 18:35:08 +0900265 return -EAGAIN;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900266 }
267
268 if (attached)
269 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
270 else
271 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
272
273 ret = max77693_update_reg(info->max77693->regmap_muic,
274 MAX77693_MUIC_REG_CTRL2, ctrl2,
275 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
276 if (ret < 0) {
277 dev_err(info->dev, "failed to update MUIC register\n");
Chanwoo Choi19d32432013-02-13 18:35:08 +0900278 return -EAGAIN;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900279 }
280
281 dev_info(info->dev,
282 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
283 ctrl1, ctrl2, attached ? "attached" : "detached");
Chanwoo Choi19d32432013-02-13 18:35:08 +0900284
285 return 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900286}
287
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900288/*
289 * max77693_muic_get_cable_type - Return cable type and check cable state
290 * @info: the instance including private data of max77693 MUIC
291 * @group: the path according to attached cable
292 * @attached: store cable state and return
293 *
294 * This function check the cable state either attached or detached,
295 * and then divide precise type of cable according to cable group.
296 * - MAX77693_CABLE_GROUP_ADC
297 * - MAX77693_CABLE_GROUP_ADC_GND
298 * - MAX77693_CABLE_GROUP_CHG
299 * - MAX77693_CABLE_GROUP_VBVOLT
300 */
301static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
302 enum max77693_muic_cable_group group, bool *attached)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900303{
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900304 int cable_type = 0;
305 int adc;
306 int adc1k;
307 int adclow;
308 int vbvolt;
309 int chg_type;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900310
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900311 switch (group) {
312 case MAX77693_CABLE_GROUP_ADC:
313 /*
314 * Read ADC value to check cable type and decide cable state
315 * according to cable type
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900316 */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900317 adc = info->status[0] & STATUS1_ADC_MASK;
318 adc >>= STATUS1_ADC_SHIFT;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900319
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900320 /*
321 * Check current cable state/cable type and store cable type
322 * (info->prev_cable_type) for handling cable when cable is
323 * detached.
324 */
325 if (adc == MAX77693_MUIC_ADC_OPEN) {
326 *attached = false;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900327
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900328 cable_type = info->prev_cable_type;
329 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
330 } else {
331 *attached = true;
332
333 cable_type = info->prev_cable_type = adc;
334 }
335 break;
336 case MAX77693_CABLE_GROUP_ADC_GND:
337 /*
338 * Read ADC value to check cable type and decide cable state
339 * according to cable type
340 */
341 adc = info->status[0] & STATUS1_ADC_MASK;
342 adc >>= STATUS1_ADC_SHIFT;
343
344 /*
345 * Check current cable state/cable type and store cable type
346 * (info->prev_cable_type/_gnd) for handling cable when cable
347 * is detached.
348 */
349 if (adc == MAX77693_MUIC_ADC_OPEN) {
350 *attached = false;
351
352 cable_type = info->prev_cable_type_gnd;
353 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
354 } else {
355 *attached = true;
356
357 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
358 adclow >>= STATUS1_ADCLOW_SHIFT;
359 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
360 adc1k >>= STATUS1_ADC1K_SHIFT;
361
362 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
363 vbvolt >>= STATUS2_VBVOLT_SHIFT;
364
365 /**
366 * [0x1][VBVolt][ADCLow][ADC1K]
367 * [0x1 0 0 0 ] : USB_OTG
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900368 * [0x1 1 0 0 ] : USB_OTG_VB
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900369 * [0x1 0 1 0 ] : Audio Video Cable with load
370 * [0x1 0 1 1 ] : MHL without charging connector
371 * [0x1 1 1 1 ] : MHL with charging connector
372 */
373 cable_type = ((0x1 << 8)
374 | (vbvolt << 2)
375 | (adclow << 1)
376 | adc1k);
377
378 info->prev_cable_type = adc;
379 info->prev_cable_type_gnd = cable_type;
380 }
381
382 break;
383 case MAX77693_CABLE_GROUP_CHG:
384 /*
385 * Read charger type to check cable type and decide cable state
386 * according to type of charger cable.
387 */
388 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
389 chg_type >>= STATUS2_CHGTYP_SHIFT;
390
391 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
392 *attached = false;
393
394 cable_type = info->prev_chg_type;
395 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
396 } else {
397 *attached = true;
398
399 /*
400 * Check current cable state/cable type and store cable
401 * type(info->prev_chg_type) for handling cable when
402 * charger cable is detached.
403 */
404 cable_type = info->prev_chg_type = chg_type;
405 }
406
407 break;
408 case MAX77693_CABLE_GROUP_VBVOLT:
409 /*
410 * Read ADC value to check cable type and decide cable state
411 * according to cable type
412 */
413 adc = info->status[0] & STATUS1_ADC_MASK;
414 adc >>= STATUS1_ADC_SHIFT;
415 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
416 chg_type >>= STATUS2_CHGTYP_SHIFT;
417
418 if (adc == MAX77693_MUIC_ADC_OPEN
419 && chg_type == MAX77693_CHARGER_TYPE_NONE)
420 *attached = false;
421 else
422 *attached = true;
423
424 /*
425 * Read vbvolt field, if vbvolt is 1,
426 * this cable is used for charging.
427 */
428 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
429 vbvolt >>= STATUS2_VBVOLT_SHIFT;
430
431 cable_type = vbvolt;
432 break;
433 default:
434 dev_err(info->dev, "Unknown cable group (%d)\n", group);
435 cable_type = -EINVAL;
436 break;
437 }
438
439 return cable_type;
440}
441
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900442static int max77693_muic_dock_handler(struct max77693_muic_info *info,
443 int cable_type, bool attached)
444{
445 int ret = 0;
Chanwoo Choia1626292012-12-10 19:07:53 +0900446 int vbvolt;
447 bool cable_attached;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900448 char dock_name[CABLE_NAME_MAX];
449
450 dev_info(info->dev,
451 "external connector is %s (adc:0x%02x)\n",
452 attached ? "attached" : "detached", cable_type);
453
454 switch (cable_type) {
455 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
Chanwoo Choia1626292012-12-10 19:07:53 +0900456 /*
457 * Check power cable whether attached or detached state.
458 * The Dock-Smart device need surely external power supply.
459 * If power cable(USB/TA) isn't connected to Dock device,
460 * user can't use Dock-Smart for desktop mode.
461 */
462 vbvolt = max77693_muic_get_cable_type(info,
463 MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
464 if (attached && !vbvolt) {
465 dev_warn(info->dev,
466 "Cannot detect external power supply\n");
467 return 0;
468 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900469
Chanwoo Choia1626292012-12-10 19:07:53 +0900470 /*
471 * Notify Dock-Smart/MHL state.
472 * - Dock-Smart device include three type of cable which
473 * are HDMI, USB for mouse/keyboard and micro-usb port
474 * for USB/TA cable. Dock-Smart device need always exteranl
475 * power supply(USB/TA cable through micro-usb cable). Dock-
476 * Smart device support screen output of target to separate
477 * monitor and mouse/keyboard for desktop mode.
478 *
479 * Features of 'USB/TA cable with Dock-Smart device'
480 * - Support MHL
481 * - Support external output feature of audio
482 * - Support charging through micro-usb port without data
483 * connection if TA cable is connected to target.
484 * - Support charging and data connection through micro-usb port
485 * if USB cable is connected between target and host
486 * device.
487 * - Support OTG device (Mouse/Keyboard)
488 */
489 ret = max77693_muic_set_path(info, info->path_usb, attached);
490 if (ret < 0)
491 return ret;
492
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900493 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
Chanwoo Choia1626292012-12-10 19:07:53 +0900494 extcon_set_cable_state(info->edev, "MHL", attached);
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900495 goto out;
496 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
497 strcpy(dock_name, "Dock-Car");
498 break;
499 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
500 strcpy(dock_name, "Dock-Desk");
501 break;
502 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
503 strcpy(dock_name, "Dock-Audio");
504 if (!attached)
505 extcon_set_cable_state(info->edev, "USB", false);
506 break;
Chanwoo Choi19d32432013-02-13 18:35:08 +0900507 default:
508 dev_err(info->dev, "failed to detect %s dock device\n",
509 attached ? "attached" : "detached");
510 return -EINVAL;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900511 }
512
513 /* Dock-Car/Desk/Audio, PATH:AUDIO */
514 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
515 if (ret < 0)
Chanwoo Choia1626292012-12-10 19:07:53 +0900516 return ret;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900517 extcon_set_cable_state(info->edev, dock_name, attached);
518
519out:
Chanwoo Choia1626292012-12-10 19:07:53 +0900520 return 0;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900521}
522
523static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
524 int button_type, bool attached)
525{
526 struct input_dev *dock = info->dock;
527 unsigned int code;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900528
529 switch (button_type) {
530 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
531 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
532 /* DOCK_KEY_PREV */
533 code = KEY_PREVIOUSSONG;
534 break;
535 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
536 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
537 /* DOCK_KEY_NEXT */
538 code = KEY_NEXTSONG;
539 break;
540 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
541 /* DOCK_VOL_DOWN */
542 code = KEY_VOLUMEDOWN;
543 break;
544 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
545 /* DOCK_VOL_UP */
546 code = KEY_VOLUMEUP;
547 break;
548 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
549 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
550 /* DOCK_KEY_PLAY_PAUSE */
551 code = KEY_PLAYPAUSE;
552 break;
553 default:
554 dev_err(info->dev,
555 "failed to detect %s key (adc:0x%x)\n",
556 attached ? "pressed" : "released", button_type);
Chanwoo Choi19d32432013-02-13 18:35:08 +0900557 return -EINVAL;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900558 }
559
560 input_event(dock, EV_KEY, code, attached);
561 input_sync(dock);
562
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900563 return 0;
564}
565
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900566static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
567{
568 int cable_type_gnd;
569 int ret = 0;
570 bool attached;
571
572 cable_type_gnd = max77693_muic_get_cable_type(info,
573 MAX77693_CABLE_GROUP_ADC_GND, &attached);
574
575 switch (cable_type_gnd) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900576 case MAX77693_MUIC_GND_USB_OTG:
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900577 case MAX77693_MUIC_GND_USB_OTG_VB:
578 /* USB_OTG, PATH: AP_USB */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900579 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
580 if (ret < 0)
Chanwoo Choi19d32432013-02-13 18:35:08 +0900581 return ret;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900582 extcon_set_cable_state(info->edev, "USB-Host", attached);
583 break;
584 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900585 /* Audio Video Cable with load, PATH:AUDIO */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900586 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
587 if (ret < 0)
Chanwoo Choi19d32432013-02-13 18:35:08 +0900588 return ret;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900589 extcon_set_cable_state(info->edev,
590 "Audio-video-load", attached);
591 break;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900592 case MAX77693_MUIC_GND_MHL:
593 case MAX77693_MUIC_GND_MHL_VB:
594 /* MHL or MHL with USB/TA cable */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900595 extcon_set_cable_state(info->edev, "MHL", attached);
596 break;
597 default:
Chanwoo Choi19d32432013-02-13 18:35:08 +0900598 dev_err(info->dev, "failed to detect %s cable of gnd type\n",
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900599 attached ? "attached" : "detached");
Chanwoo Choi19d32432013-02-13 18:35:08 +0900600 return -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900601 }
602
Chanwoo Choi19d32432013-02-13 18:35:08 +0900603 return 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900604}
605
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900606static int max77693_muic_jig_handler(struct max77693_muic_info *info,
607 int cable_type, bool attached)
608{
609 char cable_name[32];
610 int ret = 0;
611 u8 path = CONTROL1_SW_OPEN;
612
613 dev_info(info->dev,
614 "external connector is %s (adc:0x%02x)\n",
615 attached ? "attached" : "detached", cable_type);
616
617 switch (cable_type) {
618 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
619 /* PATH:AP_USB */
620 strcpy(cable_name, "JIG-USB-OFF");
621 path = CONTROL1_SW_USB;
622 break;
623 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
624 /* PATH:AP_USB */
625 strcpy(cable_name, "JIG-USB-ON");
626 path = CONTROL1_SW_USB;
627 break;
628 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
629 /* PATH:AP_UART */
630 strcpy(cable_name, "JIG-UART-OFF");
631 path = CONTROL1_SW_UART;
632 break;
Chanwoo Choi19d32432013-02-13 18:35:08 +0900633 default:
634 dev_err(info->dev, "failed to detect %s jig cable\n",
635 attached ? "attached" : "detached");
636 return -EINVAL;
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900637 }
638
639 ret = max77693_muic_set_path(info, path, attached);
640 if (ret < 0)
Chanwoo Choi19d32432013-02-13 18:35:08 +0900641 return ret;
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900642
643 extcon_set_cable_state(info->edev, cable_name, attached);
Chanwoo Choi19d32432013-02-13 18:35:08 +0900644
645 return 0;
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900646}
647
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900648static int max77693_muic_adc_handler(struct max77693_muic_info *info)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900649{
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900650 int cable_type;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900651 int button_type;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900652 bool attached;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900653 int ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900654
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900655 /* Check accessory state which is either detached or attached */
656 cable_type = max77693_muic_get_cable_type(info,
657 MAX77693_CABLE_GROUP_ADC, &attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900658
659 dev_info(info->dev,
660 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900661 attached ? "attached" : "detached", cable_type,
662 info->prev_cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900663
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900664 switch (cable_type) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900665 case MAX77693_MUIC_ADC_GROUND:
666 /* USB_OTG/MHL/Audio */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900667 max77693_muic_adc_ground_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900668 break;
669 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
670 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900671 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900672 /* JIG */
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900673 ret = max77693_muic_jig_handler(info, cable_type, attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900674 if (ret < 0)
Chanwoo Choi19d32432013-02-13 18:35:08 +0900675 return ret;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900676 break;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900677 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
678 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
679 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
680 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
681 /*
682 * DOCK device
683 *
684 * The MAX77693 MUIC device can detect total 34 cable type
685 * except of charger cable and MUIC device didn't define
686 * specfic role of cable in the range of from 0x01 to 0x12
687 * of ADC value. So, can use/define cable with no role according
688 * to schema of hardware board.
689 */
690 ret = max77693_muic_dock_handler(info, cable_type, attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900691 if (ret < 0)
Chanwoo Choi19d32432013-02-13 18:35:08 +0900692 return ret;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900693 break;
694 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
695 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
696 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
697 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
698 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
699 /*
700 * Button of DOCK device
701 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
702 *
703 * The MAX77693 MUIC device can detect total 34 cable type
704 * except of charger cable and MUIC device didn't define
705 * specfic role of cable in the range of from 0x01 to 0x12
706 * of ADC value. So, can use/define cable with no role according
707 * to schema of hardware board.
708 */
709 if (attached)
710 button_type = info->prev_button_type = cable_type;
711 else
712 button_type = info->prev_button_type;
713
714 ret = max77693_muic_dock_button_handler(info, button_type,
715 attached);
716 if (ret < 0)
Chanwoo Choi19d32432013-02-13 18:35:08 +0900717 return ret;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900718 break;
719 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
720 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
721 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900722 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
723 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
724 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900725 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900726 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900727 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
728 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900729 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
730 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
731 case MAX77693_MUIC_ADC_CEA936_AUDIO:
732 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
733 case MAX77693_MUIC_ADC_TTY_CONVERTER:
734 case MAX77693_MUIC_ADC_UART_CABLE:
735 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900736 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900737 /*
738 * This accessory isn't used in general case if it is specially
739 * needed to detect additional accessory, should implement
740 * proper operation when this accessory is attached/detached.
741 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900742 dev_info(info->dev,
743 "accessory is %s but it isn't used (adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900744 attached ? "attached" : "detached", cable_type);
Chanwoo Choi19d32432013-02-13 18:35:08 +0900745 return -EAGAIN;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900746 default:
747 dev_err(info->dev,
748 "failed to detect %s accessory (adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900749 attached ? "attached" : "detached", cable_type);
Chanwoo Choi19d32432013-02-13 18:35:08 +0900750 return -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900751 }
752
Chanwoo Choi19d32432013-02-13 18:35:08 +0900753 return 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900754}
755
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900756static int max77693_muic_chg_handler(struct max77693_muic_info *info)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900757{
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900758 int chg_type;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900759 int cable_type_gnd;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900760 int cable_type;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900761 bool attached;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900762 bool cable_attached;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900763 int ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900764
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900765 chg_type = max77693_muic_get_cable_type(info,
766 MAX77693_CABLE_GROUP_CHG, &attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900767
768 dev_info(info->dev,
769 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
770 attached ? "attached" : "detached",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900771 chg_type, info->prev_chg_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900772
773 switch (chg_type) {
774 case MAX77693_CHARGER_TYPE_USB:
Chanwoo Choia1626292012-12-10 19:07:53 +0900775 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
Chanwoo Choi0e2738f2012-12-06 21:36:18 +0900776 case MAX77693_CHARGER_TYPE_NONE:
Chanwoo Choia1626292012-12-10 19:07:53 +0900777 /* Check MAX77693_CABLE_GROUP_ADC_GND type */
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900778 cable_type_gnd = max77693_muic_get_cable_type(info,
779 MAX77693_CABLE_GROUP_ADC_GND,
780 &cable_attached);
Chanwoo Choia1626292012-12-10 19:07:53 +0900781 switch (cable_type_gnd) {
782 case MAX77693_MUIC_GND_MHL:
783 case MAX77693_MUIC_GND_MHL_VB:
784 /*
785 * MHL cable with MHL_TA(USB/TA) cable
786 * - MHL cable include two port(HDMI line and separate micro-
787 * usb port. When the target connect MHL cable, extcon driver
788 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
789 * cable is connected, extcon driver notify state to notifiee
790 * for charging battery.
791 *
792 * Features of 'MHL_TA(USB/TA) with MHL cable'
793 * - Support MHL
794 * - Support charging through micro-usb port without data connection
795 */
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900796 extcon_set_cable_state(info->edev, "MHL_TA", attached);
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900797 if (!cable_attached)
Chanwoo Choia1626292012-12-10 19:07:53 +0900798 extcon_set_cable_state(info->edev, "MHL", cable_attached);
799 break;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900800 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900801
Chanwoo Choia1626292012-12-10 19:07:53 +0900802 /* Check MAX77693_CABLE_GROUP_ADC type */
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900803 cable_type = max77693_muic_get_cable_type(info,
804 MAX77693_CABLE_GROUP_ADC,
805 &cable_attached);
Chanwoo Choia1626292012-12-10 19:07:53 +0900806 switch (cable_type) {
807 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
808 /*
809 * Dock-Audio device with USB/TA cable
810 * - Dock device include two port(Dock-Audio and micro-usb
811 * port). When the target connect Dock-Audio device, extcon
812 * driver check whether USB/TA cable is connected. If USB/TA
813 * cable is connected, extcon driver notify state to notifiee
814 * for charging battery.
815 *
816 * Features of 'USB/TA cable with Dock-Audio device'
817 * - Support external output feature of audio.
818 * - Support charging through micro-usb port without data
819 * connection.
820 */
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900821 extcon_set_cable_state(info->edev, "USB", attached);
822
823 if (!cable_attached)
Chanwoo Choia1626292012-12-10 19:07:53 +0900824 extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached);
825 break;
826 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
827 /*
828 * Dock-Smart device with USB/TA cable
829 * - Dock-Desk device include three type of cable which
830 * are HDMI, USB for mouse/keyboard and micro-usb port
831 * for USB/TA cable. Dock-Smart device need always exteranl
832 * power supply(USB/TA cable through micro-usb cable). Dock-
833 * Smart device support screen output of target to separate
834 * monitor and mouse/keyboard for desktop mode.
835 *
836 * Features of 'USB/TA cable with Dock-Smart device'
837 * - Support MHL
838 * - Support external output feature of audio
839 * - Support charging through micro-usb port without data
840 * connection if TA cable is connected to target.
841 * - Support charging and data connection through micro-usb port
842 * if USB cable is connected between target and host
843 * device.
844 * - Support OTG device (Mouse/Keyboard)
845 */
846 ret = max77693_muic_set_path(info, info->path_usb, attached);
847 if (ret < 0)
848 return ret;
849
850 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
851 extcon_set_cable_state(info->edev, "MHL", attached);
852
853 break;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900854 }
855
Chanwoo Choia1626292012-12-10 19:07:53 +0900856 /* Check MAX77693_CABLE_GROUP_CHG type */
857 switch (chg_type) {
858 case MAX77693_CHARGER_TYPE_NONE:
859 /*
860 * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable
861 * is attached, muic device happen below two interrupt.
862 * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio.
863 * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable
864 * connected to MHL or Dock-Audio.
865 * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt
866 * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt.
867 *
868 * If user attach MHL (with USB/TA cable and immediately detach
869 * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP
870 * interrupt is happened, USB/TA cable remain connected state to
871 * target. But USB/TA cable isn't connected to target. The user
872 * be face with unusual action. So, driver should check this
873 * situation in spite of, that previous charger type is N/A.
874 */
Chanwoo Choi0e2738f2012-12-06 21:36:18 +0900875 break;
Chanwoo Choia1626292012-12-10 19:07:53 +0900876 case MAX77693_CHARGER_TYPE_USB:
877 /* Only USB cable, PATH:AP_USB */
878 ret = max77693_muic_set_path(info, info->path_usb, attached);
879 if (ret < 0)
880 return ret;
Chanwoo Choi0e2738f2012-12-06 21:36:18 +0900881
Chanwoo Choia1626292012-12-10 19:07:53 +0900882 extcon_set_cable_state(info->edev, "USB", attached);
883 break;
884 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
885 /* Only TA cable */
886 extcon_set_cable_state(info->edev, "TA", attached);
887 break;
888 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900889 break;
890 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
891 extcon_set_cable_state(info->edev,
892 "Charge-downstream", attached);
893 break;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900894 case MAX77693_CHARGER_TYPE_APPLE_500MA:
895 extcon_set_cable_state(info->edev, "Slow-charger", attached);
896 break;
897 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
898 extcon_set_cable_state(info->edev, "Fast-charger", attached);
899 break;
900 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
901 break;
902 default:
903 dev_err(info->dev,
904 "failed to detect %s accessory (chg_type:0x%x)\n",
905 attached ? "attached" : "detached", chg_type);
Chanwoo Choia1626292012-12-10 19:07:53 +0900906 return -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900907 }
908
Chanwoo Choia1626292012-12-10 19:07:53 +0900909 return 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900910}
911
912static void max77693_muic_irq_work(struct work_struct *work)
913{
914 struct max77693_muic_info *info = container_of(work,
915 struct max77693_muic_info, irq_work);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900916 int irq_type = -1;
917 int i, ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900918
919 if (!info->edev)
920 return;
921
922 mutex_lock(&info->mutex);
923
924 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
925 if (info->irq == muic_irqs[i].virq)
926 irq_type = muic_irqs[i].irq;
927
928 ret = max77693_bulk_read(info->max77693->regmap_muic,
929 MAX77693_MUIC_REG_STATUS1, 2, info->status);
930 if (ret) {
931 dev_err(info->dev, "failed to read MUIC register\n");
932 mutex_unlock(&info->mutex);
933 return;
934 }
935
936 switch (irq_type) {
937 case MAX77693_MUIC_IRQ_INT1_ADC:
938 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
939 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
940 case MAX77693_MUIC_IRQ_INT1_ADC1K:
941 /* Handle all of accessory except for
942 type of charger accessory */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900943 ret = max77693_muic_adc_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900944 break;
945 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
946 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
947 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
948 case MAX77693_MUIC_IRQ_INT2_DXOVP:
949 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
950 case MAX77693_MUIC_IRQ_INT2_VIDRM:
951 /* Handle charger accessory */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900952 ret = max77693_muic_chg_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900953 break;
954 case MAX77693_MUIC_IRQ_INT3_EOC:
955 case MAX77693_MUIC_IRQ_INT3_CGMBC:
956 case MAX77693_MUIC_IRQ_INT3_OVP:
957 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
958 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
959 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
960 break;
961 default:
962 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
963 irq_type);
Chanwoo Choi19d32432013-02-13 18:35:08 +0900964 mutex_unlock(&info->mutex);
965 return;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900966 }
967
968 if (ret < 0)
969 dev_err(info->dev, "failed to handle MUIC interrupt\n");
970
971 mutex_unlock(&info->mutex);
972
973 return;
974}
975
976static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
977{
978 struct max77693_muic_info *info = data;
979
980 info->irq = irq;
981 schedule_work(&info->irq_work);
982
983 return IRQ_HANDLED;
984}
985
986static struct regmap_config max77693_muic_regmap_config = {
987 .reg_bits = 8,
988 .val_bits = 8,
989};
990
991static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
992{
993 int ret = 0;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900994 int adc;
995 int chg_type;
996 bool attached;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900997
998 mutex_lock(&info->mutex);
999
1000 /* Read STATUSx register to detect accessory */
1001 ret = max77693_bulk_read(info->max77693->regmap_muic,
1002 MAX77693_MUIC_REG_STATUS1, 2, info->status);
1003 if (ret) {
1004 dev_err(info->dev, "failed to read MUIC register\n");
1005 mutex_unlock(&info->mutex);
1006 return -EINVAL;
1007 }
1008
Chanwoo Choi154f757f2012-11-27 09:40:32 +09001009 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1010 &attached);
1011 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1012 ret = max77693_muic_adc_handler(info);
Chanwoo Choi19d32432013-02-13 18:35:08 +09001013 if (ret < 0) {
Chanwoo Choi154f757f2012-11-27 09:40:32 +09001014 dev_err(info->dev, "Cannot detect accessory\n");
Chanwoo Choi19d32432013-02-13 18:35:08 +09001015 mutex_unlock(&info->mutex);
1016 return ret;
1017 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001018 }
1019
Chanwoo Choi154f757f2012-11-27 09:40:32 +09001020 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1021 &attached);
1022 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1023 ret = max77693_muic_chg_handler(info);
Chanwoo Choi19d32432013-02-13 18:35:08 +09001024 if (ret < 0) {
Chanwoo Choi154f757f2012-11-27 09:40:32 +09001025 dev_err(info->dev, "Cannot detect charger accessory\n");
Chanwoo Choi19d32432013-02-13 18:35:08 +09001026 mutex_unlock(&info->mutex);
1027 return ret;
1028 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001029 }
1030
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001031 mutex_unlock(&info->mutex);
Chanwoo Choi154f757f2012-11-27 09:40:32 +09001032
Chanwoo Choi19d32432013-02-13 18:35:08 +09001033 return 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001034}
1035
Chanwoo Choi297620f2012-12-26 13:10:11 +09001036static void max77693_muic_detect_cable_wq(struct work_struct *work)
1037{
1038 struct max77693_muic_info *info = container_of(to_delayed_work(work),
1039 struct max77693_muic_info, wq_detcable);
1040
1041 max77693_muic_detect_accessory(info);
1042}
1043
Bill Pemberton44f34fd2012-11-19 13:23:21 -05001044static int max77693_muic_probe(struct platform_device *pdev)
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001045{
1046 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
Chanwoo Choif8457d572012-10-08 14:41:49 +09001047 struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001048 struct max77693_muic_info *info;
Chanwoo Choi297620f2012-12-26 13:10:11 +09001049 int delay_jiffies;
1050 int ret;
1051 int i;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001052 u8 id;
1053
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001054 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1055 GFP_KERNEL);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001056 if (!info) {
1057 dev_err(&pdev->dev, "failed to allocate memory\n");
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001058 return -ENOMEM;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001059 }
1060 info->dev = &pdev->dev;
1061 info->max77693 = max77693;
Sachin Kamat1967fa02012-11-21 10:04:58 +05301062 if (info->max77693->regmap_muic) {
Chanwoo Choib186b122012-08-21 15:16:23 +09001063 dev_dbg(&pdev->dev, "allocate register map\n");
Sachin Kamat1967fa02012-11-21 10:04:58 +05301064 } else {
Chanwoo Choib186b122012-08-21 15:16:23 +09001065 info->max77693->regmap_muic = devm_regmap_init_i2c(
1066 info->max77693->muic,
1067 &max77693_muic_regmap_config);
1068 if (IS_ERR(info->max77693->regmap_muic)) {
1069 ret = PTR_ERR(info->max77693->regmap_muic);
1070 dev_err(max77693->dev,
1071 "failed to allocate register map: %d\n", ret);
Sachin Kamat3bf742f2012-11-21 10:04:57 +05301072 return ret;
Chanwoo Choib186b122012-08-21 15:16:23 +09001073 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001074 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001075
1076 /* Register input device for button of dock device */
Chanwoo Choieff7d742013-02-13 18:35:05 +09001077 info->dock = devm_input_allocate_device(&pdev->dev);
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001078 if (!info->dock) {
1079 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1080 return -ENOMEM;
1081 }
1082 info->dock->name = "max77693-muic/dock";
1083 info->dock->phys = "max77693-muic/extcon";
1084 info->dock->dev.parent = &pdev->dev;
1085
1086 __set_bit(EV_REP, info->dock->evbit);
1087
1088 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1089 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1090 input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1091 input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1092 input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1093
1094 ret = input_register_device(info->dock);
1095 if (ret < 0) {
1096 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1097 ret);
1098 return ret;
1099 }
1100
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001101 platform_set_drvdata(pdev, info);
1102 mutex_init(&info->mutex);
1103
1104 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1105
1106 /* Support irq domain for MAX77693 MUIC device */
1107 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1108 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
Sachin Kamat00af4b12012-11-20 15:46:44 +09001109 unsigned int virq = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001110
1111 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
Sachin Kamat00af4b12012-11-20 15:46:44 +09001112 if (!virq) {
1113 ret = -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001114 goto err_irq;
Sachin Kamat00af4b12012-11-20 15:46:44 +09001115 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001116 muic_irq->virq = virq;
1117
1118 ret = request_threaded_irq(virq, NULL,
1119 max77693_muic_irq_handler,
Chanwoo Choiae3b3212012-11-28 12:39:01 +09001120 IRQF_NO_SUSPEND,
1121 muic_irq->name, info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001122 if (ret) {
1123 dev_err(&pdev->dev,
1124 "failed: irq request (IRQ: %d,"
1125 " error :%d)\n",
1126 muic_irq->irq, ret);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001127 goto err_irq;
1128 }
1129 }
1130
1131 /* Initialize extcon device */
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001132 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1133 GFP_KERNEL);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001134 if (!info->edev) {
1135 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1136 ret = -ENOMEM;
1137 goto err_irq;
1138 }
1139 info->edev->name = DEV_NAME;
1140 info->edev->supported_cable = max77693_extcon_cable;
1141 ret = extcon_dev_register(info->edev, NULL);
1142 if (ret) {
1143 dev_err(&pdev->dev, "failed to register extcon device\n");
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001144 goto err_irq;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001145 }
1146
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001147 if (pdata->muic_data) {
1148 struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
Chanwoo Choif8457d572012-10-08 14:41:49 +09001149
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001150 /* Initialize MUIC register by using platform data */
1151 for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1152 enum max77693_irq_source irq_src
1153 = MAX77693_IRQ_GROUP_NR;
Chanwoo Choif8457d572012-10-08 14:41:49 +09001154
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001155 max77693_write_reg(info->max77693->regmap_muic,
1156 muic_pdata->init_data[i].addr,
1157 muic_pdata->init_data[i].data);
1158
1159 switch (muic_pdata->init_data[i].addr) {
1160 case MAX77693_MUIC_REG_INTMASK1:
1161 irq_src = MUIC_INT1;
1162 break;
1163 case MAX77693_MUIC_REG_INTMASK2:
1164 irq_src = MUIC_INT2;
1165 break;
1166 case MAX77693_MUIC_REG_INTMASK3:
1167 irq_src = MUIC_INT3;
1168 break;
1169 }
1170
1171 if (irq_src < MAX77693_IRQ_GROUP_NR)
1172 info->max77693->irq_masks_cur[irq_src]
1173 = muic_pdata->init_data[i].data;
Chanwoo Choif8457d572012-10-08 14:41:49 +09001174 }
1175
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001176 /*
1177 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1178 * h/w path of COMP2/COMN1 on CONTROL1 register.
1179 */
1180 if (muic_pdata->path_uart)
1181 info->path_uart = muic_pdata->path_uart;
1182 else
1183 info->path_uart = CONTROL1_SW_UART;
Chanwoo Choif8457d572012-10-08 14:41:49 +09001184
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001185 if (muic_pdata->path_usb)
1186 info->path_usb = muic_pdata->path_usb;
1187 else
1188 info->path_usb = CONTROL1_SW_USB;
Chanwoo Choi2b757992012-12-06 21:27:56 +09001189
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001190 /*
1191 * Default delay time for detecting cable state
1192 * after certain time.
1193 */
1194 if (muic_pdata->detcable_delay_ms)
1195 delay_jiffies =
1196 msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1197 else
1198 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1199 } else {
Chanwoo Choi2b757992012-12-06 21:27:56 +09001200 info->path_usb = CONTROL1_SW_USB;
Chanwoo Choi190d7cf2013-02-18 10:03:32 +09001201 info->path_uart = CONTROL1_SW_UART;
1202 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1203 }
Chanwoo Choi2b757992012-12-06 21:27:56 +09001204
1205 /* Set initial path for UART */
1206 max77693_muic_set_path(info, info->path_uart, true);
1207
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001208 /* Check revision number of MUIC device*/
1209 ret = max77693_read_reg(info->max77693->regmap_muic,
1210 MAX77693_MUIC_REG_ID, &id);
1211 if (ret < 0) {
1212 dev_err(&pdev->dev, "failed to read revision number\n");
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001213 goto err_extcon;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001214 }
1215 dev_info(info->dev, "device ID : 0x%x\n", id);
1216
1217 /* Set ADC debounce time */
1218 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1219
Chanwoo Choi297620f2012-12-26 13:10:11 +09001220 /*
1221 * Detect accessory after completing the initialization of platform
1222 *
1223 * - Use delayed workqueue to detect cable state and then
1224 * notify cable state to notifiee/platform through uevent.
1225 * After completing the booting of platform, the extcon provider
1226 * driver should notify cable state to upper layer.
1227 */
1228 INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
Chanwoo Choi297620f2012-12-26 13:10:11 +09001229 schedule_delayed_work(&info->wq_detcable, delay_jiffies);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001230
1231 return ret;
1232
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001233err_extcon:
1234 extcon_dev_unregister(info->edev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001235err_irq:
Sachin Kamat00af4b12012-11-20 15:46:44 +09001236 while (--i >= 0)
1237 free_irq(muic_irqs[i].virq, info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001238 return ret;
1239}
1240
Bill Pemberton93ed0322012-11-19 13:25:49 -05001241static int max77693_muic_remove(struct platform_device *pdev)
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001242{
1243 struct max77693_muic_info *info = platform_get_drvdata(pdev);
1244 int i;
1245
1246 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1247 free_irq(muic_irqs[i].virq, info);
1248 cancel_work_sync(&info->irq_work);
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001249 input_unregister_device(info->dock);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001250 extcon_dev_unregister(info->edev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001251
1252 return 0;
1253}
1254
1255static struct platform_driver max77693_muic_driver = {
1256 .driver = {
1257 .name = DEV_NAME,
1258 .owner = THIS_MODULE,
1259 },
1260 .probe = max77693_muic_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -05001261 .remove = max77693_muic_remove,
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001262};
1263
1264module_platform_driver(max77693_muic_driver);
1265
1266MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1267MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1268MODULE_LICENSE("GPL");
1269MODULE_ALIAS("platform:extcon-max77693");