blob: 349f65f6a4a451f1b272cb2dce2ae9925159435d [file] [log] [blame]
Donggeun Kim99f09be2011-11-24 18:12:18 +09001/*
Chanwoo Choib76668b2012-05-09 12:31:58 +09002 * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
Donggeun Kim99f09be2011-11-24 18:12:18 +09003 *
Sachin Kamat2ca36f42012-11-20 15:46:33 +09004 * Copyright (C) 2012 Samsung Electronics
Donggeun Kim99f09be2011-11-24 18:12:18 +09005 * Donggeun Kim <dg77.kim@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.
Donggeun Kim99f09be2011-11-24 18:12:18 +090016 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
23#include <linux/err.h>
24#include <linux/platform_device.h>
25#include <linux/kobject.h>
26#include <linux/mfd/max8997.h>
27#include <linux/mfd/max8997-private.h>
Chanwoo Choib76668b2012-05-09 12:31:58 +090028#include <linux/extcon.h>
Chanwoo Choidca1a712012-07-02 09:03:00 +090029#include <linux/irqdomain.h>
Chanwoo Choib76668b2012-05-09 12:31:58 +090030
31#define DEV_NAME "max8997-muic"
Donggeun Kim99f09be2011-11-24 18:12:18 +090032
Chanwoo Choi027fcd52013-02-13 08:50:00 +090033enum max8997_muic_adc_debounce_time {
34 ADC_DEBOUNCE_TIME_0_5MS = 0, /* 0.5ms */
35 ADC_DEBOUNCE_TIME_10MS, /* 10ms */
36 ADC_DEBOUNCE_TIME_25MS, /* 25ms */
37 ADC_DEBOUNCE_TIME_38_62MS, /* 38.62ms */
38};
39
Donggeun Kim99f09be2011-11-24 18:12:18 +090040struct max8997_muic_irq {
41 unsigned int irq;
42 const char *name;
Chanwoo Choidca1a712012-07-02 09:03:00 +090043 unsigned int virq;
Donggeun Kim99f09be2011-11-24 18:12:18 +090044};
45
46static struct max8997_muic_irq muic_irqs[] = {
Chanwoo Choif73f6232013-02-13 12:05:42 +090047 { MAX8997_MUICIRQ_ADCError, "muic-ADCERROR" },
48 { MAX8997_MUICIRQ_ADCLow, "muic-ADCLOW" },
49 { MAX8997_MUICIRQ_ADC, "muic-ADC" },
50 { MAX8997_MUICIRQ_VBVolt, "muic-VBVOLT" },
51 { MAX8997_MUICIRQ_DBChg, "muic-DBCHG" },
52 { MAX8997_MUICIRQ_DCDTmr, "muic-DCDTMR" },
53 { MAX8997_MUICIRQ_ChgDetRun, "muic-CHGDETRUN" },
54 { MAX8997_MUICIRQ_ChgTyp, "muic-CHGTYP" },
55 { MAX8997_MUICIRQ_OVP, "muic-OVP" },
56};
57
58/* Define supported cable type */
59enum max8997_muic_acc_type {
60 MAX8997_MUIC_ADC_GROUND = 0x0,
61 MAX8997_MUIC_ADC_MHL, /* MHL*/
62 MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
63 MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
64 MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
65 MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
66 MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
67 MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
68 MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
69 MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
70 MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
71 MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
72 MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
73 MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
74 MAX8997_MUIC_ADC_RESERVED_ACC_1,
75 MAX8997_MUIC_ADC_RESERVED_ACC_2,
76 MAX8997_MUIC_ADC_RESERVED_ACC_3,
77 MAX8997_MUIC_ADC_RESERVED_ACC_4,
78 MAX8997_MUIC_ADC_RESERVED_ACC_5,
79 MAX8997_MUIC_ADC_CEA936_AUDIO,
80 MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
81 MAX8997_MUIC_ADC_TTY_CONVERTER,
82 MAX8997_MUIC_ADC_UART_CABLE,
83 MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
84 MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF, /* JIG-USB-OFF */
85 MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON, /* JIG-USB-ON */
86 MAX8997_MUIC_ADC_AV_CABLE_NOLOAD, /* DESKDOCK */
87 MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
88 MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF, /* JIG-UART */
89 MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON, /* CARDOCK */
90 MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
91 MAX8997_MUIC_ADC_OPEN, /* OPEN */
92};
93
94enum max8997_muic_cable_group {
95 MAX8997_CABLE_GROUP_ADC = 0,
96 MAX8997_CABLE_GROUP_ADC_GND,
97 MAX8997_CABLE_GROUP_CHG,
98 MAX8997_CABLE_GROUP_VBVOLT,
99};
100
101enum max8997_muic_usb_type {
102 MAX8997_USB_HOST,
103 MAX8997_USB_DEVICE,
104};
105
106enum max8997_muic_charger_type {
107 MAX8997_CHARGER_TYPE_NONE = 0,
108 MAX8997_CHARGER_TYPE_USB,
109 MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
110 MAX8997_CHARGER_TYPE_DEDICATED_CHG,
111 MAX8997_CHARGER_TYPE_500MA,
112 MAX8997_CHARGER_TYPE_1A,
113 MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
Donggeun Kim99f09be2011-11-24 18:12:18 +0900114};
115
116struct max8997_muic_info {
117 struct device *dev;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900118 struct i2c_client *muic;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900119 struct extcon_dev *edev;
120 int prev_cable_type;
121 int prev_chg_type;
122 u8 status[2];
Donggeun Kim99f09be2011-11-24 18:12:18 +0900123
124 int irq;
125 struct work_struct irq_work;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900126 struct mutex mutex;
Chanwoo Choib76668b2012-05-09 12:31:58 +0900127
Chanwoo Choif73f6232013-02-13 12:05:42 +0900128 struct max8997_muic_platform_data *muic_pdata;
129 enum max8997_muic_charger_type pre_charger_type;
Chanwoo Choi685dc9a2013-02-13 15:04:15 +0900130
131 /*
132 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
133 * h/w path of COMP2/COMN1 on CONTROL1 register.
134 */
135 int path_usb;
136 int path_uart;
Chanwoo Choib76668b2012-05-09 12:31:58 +0900137};
138
Chanwoo Choie3e5bc02013-02-12 20:44:19 +0900139enum {
140 EXTCON_CABLE_USB = 0,
141 EXTCON_CABLE_USB_HOST,
142 EXTCON_CABLE_TA,
143 EXTCON_CABLE_FAST_CHARGER,
144 EXTCON_CABLE_SLOW_CHARGER,
145 EXTCON_CABLE_CHARGE_DOWNSTREAM,
146 EXTCON_CABLE_MHL,
147 EXTCON_CABLE_DOCK_DESK,
148 EXTCON_CABLE_DOCK_CARD,
149 EXTCON_CABLE_JIG,
150
151 _EXTCON_CABLE_NUM,
152};
153
Sachin Kamatcae93db2013-01-31 09:29:29 +0900154static const char *max8997_extcon_cable[] = {
Chanwoo Choie3e5bc02013-02-12 20:44:19 +0900155 [EXTCON_CABLE_USB] = "USB",
156 [EXTCON_CABLE_USB_HOST] = "USB-Host",
157 [EXTCON_CABLE_TA] = "TA",
158 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
159 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
160 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
161 [EXTCON_CABLE_MHL] = "MHL",
162 [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
163 [EXTCON_CABLE_DOCK_CARD] = "Dock-Card",
164 [EXTCON_CABLE_JIG] = "JIG",
Chanwoo Choib76668b2012-05-09 12:31:58 +0900165
166 NULL,
Donggeun Kim99f09be2011-11-24 18:12:18 +0900167};
168
Chanwoo Choi07c70502013-02-13 08:42:37 +0900169/*
Chanwoo Choi027fcd52013-02-13 08:50:00 +0900170 * max8997_muic_set_debounce_time - Set the debounce time of ADC
171 * @info: the instance including private data of max8997 MUIC
172 * @time: the debounce time of ADC
173 */
174static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
175 enum max8997_muic_adc_debounce_time time)
176{
177 int ret;
178
179 switch (time) {
180 case ADC_DEBOUNCE_TIME_0_5MS:
181 case ADC_DEBOUNCE_TIME_10MS:
182 case ADC_DEBOUNCE_TIME_25MS:
183 case ADC_DEBOUNCE_TIME_38_62MS:
184 ret = max8997_update_reg(info->muic,
185 MAX8997_MUIC_REG_CONTROL3,
186 time << CONTROL3_ADCDBSET_SHIFT,
187 CONTROL3_ADCDBSET_MASK);
188 if (ret) {
189 dev_err(info->dev, "failed to set ADC debounce time\n");
190 return -EAGAIN;
191 }
192 break;
193 default:
194 dev_err(info->dev, "invalid ADC debounce time\n");
195 return -EINVAL;
196 }
197
198 return 0;
199};
200
201/*
Chanwoo Choi07c70502013-02-13 08:42:37 +0900202 * max8997_muic_set_path - Set hardware line according to attached cable
203 * @info: the instance including private data of max8997 MUIC
204 * @value: the path according to attached cable
205 * @attached: the state of cable (true:attached, false:detached)
206 *
207 * The max8997 MUIC device share outside H/W line among a varity of cables,
208 * so this function set internal path of H/W line according to the type of
209 * attached cable.
210 */
211static int max8997_muic_set_path(struct max8997_muic_info *info,
212 u8 val, bool attached)
213{
214 int ret = 0;
215 u8 ctrl1, ctrl2 = 0;
216
217 if (attached)
218 ctrl1 = val;
219 else
220 ctrl1 = CONTROL1_SW_OPEN;
221
222 ret = max8997_update_reg(info->muic,
223 MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
224 if (ret < 0) {
225 dev_err(info->dev, "failed to update MUIC register\n");
226 return -EAGAIN;
227 }
228
229 if (attached)
230 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
231 else
232 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
233
234 ret = max8997_update_reg(info->muic,
235 MAX8997_MUIC_REG_CONTROL2, ctrl2,
236 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
237 if (ret < 0) {
238 dev_err(info->dev, "failed to update MUIC register\n");
239 return -EAGAIN;
240 }
241
242 dev_info(info->dev,
243 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
244 ctrl1, ctrl2, attached ? "attached" : "detached");
245
246 return 0;
247}
248
Chanwoo Choif73f6232013-02-13 12:05:42 +0900249/*
250 * max8997_muic_get_cable_type - Return cable type and check cable state
251 * @info: the instance including private data of max8997 MUIC
252 * @group: the path according to attached cable
253 * @attached: store cable state and return
254 *
255 * This function check the cable state either attached or detached,
256 * and then divide precise type of cable according to cable group.
257 * - MAX8997_CABLE_GROUP_ADC
258 * - MAX8997_CABLE_GROUP_CHG
259 */
260static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
261 enum max8997_muic_cable_group group, bool *attached)
262{
263 int cable_type = 0;
264 int adc;
265 int chg_type;
266
267 switch (group) {
268 case MAX8997_CABLE_GROUP_ADC:
269 /*
270 * Read ADC value to check cable type and decide cable state
271 * according to cable type
272 */
273 adc = info->status[0] & STATUS1_ADC_MASK;
274 adc >>= STATUS1_ADC_SHIFT;
275
276 /*
277 * Check current cable state/cable type and store cable type
278 * (info->prev_cable_type) for handling cable when cable is
279 * detached.
280 */
281 if (adc == MAX8997_MUIC_ADC_OPEN) {
282 *attached = false;
283
284 cable_type = info->prev_cable_type;
285 info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
286 } else {
287 *attached = true;
288
289 cable_type = info->prev_cable_type = adc;
290 }
291 break;
292 case MAX8997_CABLE_GROUP_CHG:
293 /*
294 * Read charger type to check cable type and decide cable state
295 * according to type of charger cable.
296 */
297 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
298 chg_type >>= STATUS2_CHGTYP_SHIFT;
299
300 if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
301 *attached = false;
302
303 cable_type = info->prev_chg_type;
304 info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
305 } else {
306 *attached = true;
307
308 /*
309 * Check current cable state/cable type and store cable
310 * type(info->prev_chg_type) for handling cable when
311 * charger cable is detached.
312 */
313 cable_type = info->prev_chg_type = chg_type;
314 }
315
316 break;
317 default:
318 dev_err(info->dev, "Unknown cable group (%d)\n", group);
319 cable_type = -EINVAL;
320 break;
321 }
322
323 return cable_type;
324}
325
Donggeun Kim99f09be2011-11-24 18:12:18 +0900326static int max8997_muic_handle_usb(struct max8997_muic_info *info,
327 enum max8997_muic_usb_type usb_type, bool attached)
328{
Donggeun Kim99f09be2011-11-24 18:12:18 +0900329 int ret = 0;
330
331 if (usb_type == MAX8997_USB_HOST) {
Chanwoo Choi685dc9a2013-02-13 15:04:15 +0900332 ret = max8997_muic_set_path(info, info->path_usb, attached);
Chanwoo Choif73f6232013-02-13 12:05:42 +0900333 if (ret < 0) {
Donggeun Kim99f09be2011-11-24 18:12:18 +0900334 dev_err(info->dev, "failed to update muic register\n");
Chanwoo Choif73f6232013-02-13 12:05:42 +0900335 return ret;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900336 }
337 }
338
Chanwoo Choib76668b2012-05-09 12:31:58 +0900339 switch (usb_type) {
340 case MAX8997_USB_HOST:
341 extcon_set_cable_state(info->edev, "USB-Host", attached);
342 break;
343 case MAX8997_USB_DEVICE:
344 extcon_set_cable_state(info->edev, "USB", attached);
345 break;
346 default:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900347 dev_err(info->dev, "failed to detect %s usb cable\n",
348 attached ? "attached" : "detached");
349 return -EINVAL;
Chanwoo Choib76668b2012-05-09 12:31:58 +0900350 }
351
Chanwoo Choif73f6232013-02-13 12:05:42 +0900352 return 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900353}
354
Donggeun Kim99f09be2011-11-24 18:12:18 +0900355static int max8997_muic_handle_dock(struct max8997_muic_info *info,
Chanwoo Choif73f6232013-02-13 12:05:42 +0900356 int cable_type, bool attached)
Donggeun Kim99f09be2011-11-24 18:12:18 +0900357{
Donggeun Kim99f09be2011-11-24 18:12:18 +0900358 int ret = 0;
359
Chanwoo Choi07c70502013-02-13 08:42:37 +0900360 ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900361 if (ret) {
362 dev_err(info->dev, "failed to update muic register\n");
Chanwoo Choif73f6232013-02-13 12:05:42 +0900363 return ret;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900364 }
365
Chanwoo Choif73f6232013-02-13 12:05:42 +0900366 switch (cable_type) {
367 case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
Chanwoo Choib76668b2012-05-09 12:31:58 +0900368 extcon_set_cable_state(info->edev, "Dock-desk", attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900369 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900370 case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
Chanwoo Choib76668b2012-05-09 12:31:58 +0900371 extcon_set_cable_state(info->edev, "Dock-card", attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900372 break;
373 default:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900374 dev_err(info->dev, "failed to detect %s dock device\n",
375 attached ? "attached" : "detached");
376 return -EINVAL;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900377 }
Chanwoo Choif73f6232013-02-13 12:05:42 +0900378
379 return 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900380}
381
382static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
383 bool attached)
384{
Donggeun Kim99f09be2011-11-24 18:12:18 +0900385 int ret = 0;
386
387 /* switch to UART */
Chanwoo Choi685dc9a2013-02-13 15:04:15 +0900388 ret = max8997_muic_set_path(info, info->path_uart, attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900389 if (ret) {
390 dev_err(info->dev, "failed to update muic register\n");
Chanwoo Choif73f6232013-02-13 12:05:42 +0900391 return -EINVAL;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900392 }
393
Chanwoo Choib76668b2012-05-09 12:31:58 +0900394 extcon_set_cable_state(info->edev, "JIG", attached);
Chanwoo Choif73f6232013-02-13 12:05:42 +0900395
396 return 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900397}
398
Chanwoo Choif73f6232013-02-13 12:05:42 +0900399static int max8997_muic_adc_handler(struct max8997_muic_info *info)
Donggeun Kim99f09be2011-11-24 18:12:18 +0900400{
Chanwoo Choif73f6232013-02-13 12:05:42 +0900401 int cable_type;
402 bool attached;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900403 int ret = 0;
404
Chanwoo Choif73f6232013-02-13 12:05:42 +0900405 /* Check cable state which is either detached or attached */
406 cable_type = max8997_muic_get_cable_type(info,
407 MAX8997_CABLE_GROUP_ADC, &attached);
408
409 switch (cable_type) {
410 case MAX8997_MUIC_ADC_GROUND:
411 ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
412 if (ret < 0)
413 return ret;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900414 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900415 case MAX8997_MUIC_ADC_MHL:
416 extcon_set_cable_state(info->edev, "MHL", attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900417 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900418 case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
419 case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
420 ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, attached);
421 if (ret < 0)
422 return ret;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900423 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900424 case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
425 case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
426 ret = max8997_muic_handle_dock(info, cable_type, attached);
427 if (ret < 0)
428 return ret;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900429 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900430 case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
431 ret = max8997_muic_handle_jig_uart(info, attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900432 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900433 case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
434 case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
435 case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
436 case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
437 case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
438 case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
439 case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
440 case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
441 case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
442 case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
443 case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
444 case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
445 case MAX8997_MUIC_ADC_RESERVED_ACC_1:
446 case MAX8997_MUIC_ADC_RESERVED_ACC_2:
447 case MAX8997_MUIC_ADC_RESERVED_ACC_3:
448 case MAX8997_MUIC_ADC_RESERVED_ACC_4:
449 case MAX8997_MUIC_ADC_RESERVED_ACC_5:
450 case MAX8997_MUIC_ADC_CEA936_AUDIO:
451 case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
452 case MAX8997_MUIC_ADC_TTY_CONVERTER:
453 case MAX8997_MUIC_ADC_UART_CABLE:
454 case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
455 case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
456 case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
457 /*
458 * This cable isn't used in general case if it is specially
459 * needed to detect additional cable, should implement
460 * proper operation when this cable is attached/detached.
461 */
462 dev_info(info->dev,
463 "cable is %s but it isn't used (type:0x%x)\n",
464 attached ? "attached" : "detached", cable_type);
465 return -EAGAIN;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900466 default:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900467 dev_err(info->dev,
468 "failed to detect %s unknown cable (type:0x%x)\n",
469 attached ? "attached" : "detached", cable_type);
Devendra Naga3cafbd42012-08-31 14:00:19 +0530470 return -EINVAL;
Chanwoo Choib76668b2012-05-09 12:31:58 +0900471 }
Donggeun Kim99f09be2011-11-24 18:12:18 +0900472
Devendra Naga3cafbd42012-08-31 14:00:19 +0530473 return 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900474}
475
Chanwoo Choif73f6232013-02-13 12:05:42 +0900476static int max8997_muic_chg_handler(struct max8997_muic_info *info)
Donggeun Kim99f09be2011-11-24 18:12:18 +0900477{
Chanwoo Choif73f6232013-02-13 12:05:42 +0900478 int chg_type;
479 bool attached;
480 int adc;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900481
Chanwoo Choif73f6232013-02-13 12:05:42 +0900482 chg_type = max8997_muic_get_cable_type(info,
483 MAX8997_CABLE_GROUP_CHG, &attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900484
Chanwoo Choif73f6232013-02-13 12:05:42 +0900485 switch (chg_type) {
Donggeun Kim99f09be2011-11-24 18:12:18 +0900486 case MAX8997_CHARGER_TYPE_NONE:
Donggeun Kim99f09be2011-11-24 18:12:18 +0900487 break;
488 case MAX8997_CHARGER_TYPE_USB:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900489 adc = info->status[0] & STATUS1_ADC_MASK;
490 adc >>= STATUS1_ADC_SHIFT;
491
492 if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
Donggeun Kim99f09be2011-11-24 18:12:18 +0900493 max8997_muic_handle_usb(info,
Chanwoo Choif73f6232013-02-13 12:05:42 +0900494 MAX8997_USB_DEVICE, attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900495 }
Donggeun Kim99f09be2011-11-24 18:12:18 +0900496 break;
497 case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900498 extcon_set_cable_state(info->edev, "Charge-downstream", attached);
Chanwoo Choib76668b2012-05-09 12:31:58 +0900499 break;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900500 case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900501 extcon_set_cable_state(info->edev, "TA", attached);
Chanwoo Choib76668b2012-05-09 12:31:58 +0900502 break;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900503 case MAX8997_CHARGER_TYPE_500MA:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900504 extcon_set_cable_state(info->edev, "Slow-charger", attached);
Chanwoo Choib76668b2012-05-09 12:31:58 +0900505 break;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900506 case MAX8997_CHARGER_TYPE_1A:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900507 extcon_set_cable_state(info->edev, "Fast-charger", attached);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900508 break;
509 default:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900510 dev_err(info->dev,
511 "failed to detect %s unknown chg cable (type:0x%x)\n",
512 attached ? "attached" : "detached", chg_type);
513 return -EINVAL;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900514 }
515
Chanwoo Choif73f6232013-02-13 12:05:42 +0900516 return 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900517}
518
519static void max8997_muic_irq_work(struct work_struct *work)
520{
521 struct max8997_muic_info *info = container_of(work,
522 struct max8997_muic_info, irq_work);
Chanwoo Choidca1a712012-07-02 09:03:00 +0900523 int irq_type = 0;
524 int i, ret;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900525
Chanwoo Choif73f6232013-02-13 12:05:42 +0900526 if (!info->edev)
527 return;
528
Donggeun Kim99f09be2011-11-24 18:12:18 +0900529 mutex_lock(&info->mutex);
530
Chanwoo Choif73f6232013-02-13 12:05:42 +0900531 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
532 if (info->irq == muic_irqs[i].virq)
533 irq_type = muic_irqs[i].irq;
534
Donggeun Kim99f09be2011-11-24 18:12:18 +0900535 ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
Chanwoo Choif73f6232013-02-13 12:05:42 +0900536 2, info->status);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900537 if (ret) {
538 dev_err(info->dev, "failed to read muic register\n");
539 mutex_unlock(&info->mutex);
540 return;
541 }
542
Donggeun Kim99f09be2011-11-24 18:12:18 +0900543 switch (irq_type) {
Chanwoo Choif73f6232013-02-13 12:05:42 +0900544 case MAX8997_MUICIRQ_ADCError:
545 case MAX8997_MUICIRQ_ADCLow:
Donggeun Kim99f09be2011-11-24 18:12:18 +0900546 case MAX8997_MUICIRQ_ADC:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900547 /* Handle all of cable except for charger cable */
548 ret = max8997_muic_adc_handler(info);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900549 break;
Chanwoo Choif73f6232013-02-13 12:05:42 +0900550 case MAX8997_MUICIRQ_VBVolt:
551 case MAX8997_MUICIRQ_DBChg:
552 case MAX8997_MUICIRQ_DCDTmr:
553 case MAX8997_MUICIRQ_ChgDetRun:
Donggeun Kim99f09be2011-11-24 18:12:18 +0900554 case MAX8997_MUICIRQ_ChgTyp:
Chanwoo Choif73f6232013-02-13 12:05:42 +0900555 /* Handle charger cable */
556 ret = max8997_muic_chg_handler(info);
557 break;
558 case MAX8997_MUICIRQ_OVP:
Donggeun Kim99f09be2011-11-24 18:12:18 +0900559 break;
560 default:
Chanwoo Choib76668b2012-05-09 12:31:58 +0900561 dev_info(info->dev, "misc interrupt: irq %d occurred\n",
562 irq_type);
Chanwoo Choif73f6232013-02-13 12:05:42 +0900563 mutex_unlock(&info->mutex);
564 return;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900565 }
566
Chanwoo Choif73f6232013-02-13 12:05:42 +0900567 if (ret < 0)
568 dev_err(info->dev, "failed to handle MUIC interrupt\n");
569
Donggeun Kim99f09be2011-11-24 18:12:18 +0900570 mutex_unlock(&info->mutex);
571
572 return;
573}
574
575static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
576{
577 struct max8997_muic_info *info = data;
578
579 dev_dbg(info->dev, "irq:%d\n", irq);
580 info->irq = irq;
581
582 schedule_work(&info->irq_work);
583
584 return IRQ_HANDLED;
585}
586
Chanwoo Choif73f6232013-02-13 12:05:42 +0900587static int max8997_muic_detect_dev(struct max8997_muic_info *info)
Donggeun Kim99f09be2011-11-24 18:12:18 +0900588{
Chanwoo Choif73f6232013-02-13 12:05:42 +0900589 int ret = 0;
590 int adc;
591 int chg_type;
592 bool attached;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900593
Chanwoo Choif73f6232013-02-13 12:05:42 +0900594 mutex_lock(&info->mutex);
595
596 /* Read STATUSx register to detect accessory */
597 ret = max8997_bulk_read(info->muic,
598 MAX8997_MUIC_REG_STATUS1, 2, info->status);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900599 if (ret) {
Chanwoo Choif73f6232013-02-13 12:05:42 +0900600 dev_err(info->dev, "failed to read MUIC register\n");
601 mutex_unlock(&info->mutex);
602 return -EINVAL;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900603 }
604
Chanwoo Choif73f6232013-02-13 12:05:42 +0900605 adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
606 &attached);
607 if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
608 ret = max8997_muic_adc_handler(info);
609 if (ret < 0) {
610 dev_err(info->dev, "Cannot detect ADC cable\n");
611 mutex_unlock(&info->mutex);
612 return ret;
613 }
614 }
Donggeun Kim99f09be2011-11-24 18:12:18 +0900615
Chanwoo Choif73f6232013-02-13 12:05:42 +0900616 chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
617 &attached);
618 if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
619 ret = max8997_muic_chg_handler(info);
620 if (ret < 0) {
621 dev_err(info->dev, "Cannot detect charger cable\n");
622 mutex_unlock(&info->mutex);
623 return ret;
624 }
625 }
Donggeun Kim99f09be2011-11-24 18:12:18 +0900626
Chanwoo Choif73f6232013-02-13 12:05:42 +0900627 mutex_unlock(&info->mutex);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900628
Chanwoo Choif73f6232013-02-13 12:05:42 +0900629 return 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900630}
631
Bill Pemberton44f34fd2012-11-19 13:23:21 -0500632static int max8997_muic_probe(struct platform_device *pdev)
Donggeun Kim99f09be2011-11-24 18:12:18 +0900633{
Chanwoo Choib76668b2012-05-09 12:31:58 +0900634 struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
635 struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900636 struct max8997_muic_info *info;
637 int ret, i;
638
Sachin Kamat0b672e92012-11-20 15:46:47 +0900639 info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
640 GFP_KERNEL);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900641 if (!info) {
642 dev_err(&pdev->dev, "failed to allocate memory\n");
Sachin Kamat0b672e92012-11-20 15:46:47 +0900643 return -ENOMEM;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900644 }
645
Donggeun Kim99f09be2011-11-24 18:12:18 +0900646 info->dev = &pdev->dev;
Chanwoo Choib76668b2012-05-09 12:31:58 +0900647 info->muic = max8997->muic;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900648
649 platform_set_drvdata(pdev, info);
650 mutex_init(&info->mutex);
651
Donggeun Kim99f09be2011-11-24 18:12:18 +0900652 INIT_WORK(&info->irq_work, max8997_muic_irq_work);
653
654 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
655 struct max8997_muic_irq *muic_irq = &muic_irqs[i];
Sachin Kamat68c92742012-11-20 15:46:41 +0900656 unsigned int virq = 0;
Donggeun Kim99f09be2011-11-24 18:12:18 +0900657
Chanwoo Choidca1a712012-07-02 09:03:00 +0900658 virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
Sachin Kamat68c92742012-11-20 15:46:41 +0900659 if (!virq) {
660 ret = -EINVAL;
Chanwoo Choidca1a712012-07-02 09:03:00 +0900661 goto err_irq;
Sachin Kamat68c92742012-11-20 15:46:41 +0900662 }
Chanwoo Choidca1a712012-07-02 09:03:00 +0900663 muic_irq->virq = virq;
664
Chanwoo Choiae3b3212012-11-28 12:39:01 +0900665 ret = request_threaded_irq(virq, NULL,
666 max8997_muic_irq_handler,
667 IRQF_NO_SUSPEND,
668 muic_irq->name, info);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900669 if (ret) {
670 dev_err(&pdev->dev,
671 "failed: irq request (IRQ: %d,"
672 " error :%d)\n",
673 muic_irq->irq, ret);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900674 goto err_irq;
675 }
676 }
677
Chanwoo Choib76668b2012-05-09 12:31:58 +0900678 /* External connector */
Sachin Kamat0b672e92012-11-20 15:46:47 +0900679 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
680 GFP_KERNEL);
Chanwoo Choib76668b2012-05-09 12:31:58 +0900681 if (!info->edev) {
682 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
683 ret = -ENOMEM;
684 goto err_irq;
685 }
686 info->edev->name = DEV_NAME;
687 info->edev->supported_cable = max8997_extcon_cable;
688 ret = extcon_dev_register(info->edev, NULL);
689 if (ret) {
690 dev_err(&pdev->dev, "failed to register extcon device\n");
Sachin Kamat0b672e92012-11-20 15:46:47 +0900691 goto err_irq;
Chanwoo Choib76668b2012-05-09 12:31:58 +0900692 }
693
Donggeun Kim99f09be2011-11-24 18:12:18 +0900694 /* Initialize registers according to platform data */
Chanwoo Choib76668b2012-05-09 12:31:58 +0900695 if (pdata->muic_pdata) {
696 struct max8997_muic_platform_data *mdata = info->muic_pdata;
697
698 for (i = 0; i < mdata->num_init_data; i++) {
699 max8997_write_reg(info->muic, mdata->init_data[i].addr,
700 mdata->init_data[i].data);
701 }
702 }
Donggeun Kim99f09be2011-11-24 18:12:18 +0900703
Chanwoo Choi685dc9a2013-02-13 15:04:15 +0900704 /*
705 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
706 * h/w path of COMP2/COMN1 on CONTROL1 register.
707 */
708 if (pdata->muic_pdata->path_uart)
709 info->path_uart = pdata->muic_pdata->path_uart;
710 else
711 info->path_uart = CONTROL1_SW_UART;
712
713 if (pdata->muic_pdata->path_usb)
714 info->path_usb = pdata->muic_pdata->path_usb;
715 else
716 info->path_usb = CONTROL1_SW_USB;
717
718 /* Set initial path for UART */
719 max8997_muic_set_path(info, info->path_uart, true);
720
Chanwoo Choi027fcd52013-02-13 08:50:00 +0900721 /* Set ADC debounce time */
722 max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
723
Donggeun Kim99f09be2011-11-24 18:12:18 +0900724 /* Initial device detection */
Chanwoo Choif73f6232013-02-13 12:05:42 +0900725 ret = max8997_muic_detect_dev(info);
726 if (ret < 0) {
727 dev_err(&pdev->dev, "failed to detect cable type\n");
728 goto err_extcon;
729 }
Donggeun Kim99f09be2011-11-24 18:12:18 +0900730
731 return ret;
732
Chanwoo Choif73f6232013-02-13 12:05:42 +0900733err_extcon:
734 extcon_dev_unregister(info->edev);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900735err_irq:
Axel Lin3241d562012-03-26 09:57:08 +0800736 while (--i >= 0)
Chanwoo Choidca1a712012-07-02 09:03:00 +0900737 free_irq(muic_irqs[i].virq, info);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900738 return ret;
739}
740
Bill Pemberton93ed0322012-11-19 13:25:49 -0500741static int max8997_muic_remove(struct platform_device *pdev)
Donggeun Kim99f09be2011-11-24 18:12:18 +0900742{
743 struct max8997_muic_info *info = platform_get_drvdata(pdev);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900744 int i;
745
Donggeun Kim99f09be2011-11-24 18:12:18 +0900746 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
Chanwoo Choidca1a712012-07-02 09:03:00 +0900747 free_irq(muic_irqs[i].virq, info);
Donggeun Kim71e58782011-12-15 18:20:47 +0900748 cancel_work_sync(&info->irq_work);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900749
Chanwoo Choib76668b2012-05-09 12:31:58 +0900750 extcon_dev_unregister(info->edev);
751
Donggeun Kim99f09be2011-11-24 18:12:18 +0900752 return 0;
753}
754
755static struct platform_driver max8997_muic_driver = {
756 .driver = {
Chanwoo Choib76668b2012-05-09 12:31:58 +0900757 .name = DEV_NAME,
Donggeun Kim99f09be2011-11-24 18:12:18 +0900758 .owner = THIS_MODULE,
759 },
760 .probe = max8997_muic_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500761 .remove = max8997_muic_remove,
Donggeun Kim99f09be2011-11-24 18:12:18 +0900762};
763
Axel Linb00e1262012-01-22 15:33:49 +0800764module_platform_driver(max8997_muic_driver);
Donggeun Kim99f09be2011-11-24 18:12:18 +0900765
Chanwoo Choib76668b2012-05-09 12:31:58 +0900766MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
Donggeun Kim99f09be2011-11-24 18:12:18 +0900767MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
768MODULE_LICENSE("GPL");