blob: 10f41f3d5be4155b966d3ee6144a10519c38c715 [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 Choidb1b9032012-07-17 13:28:28 +090066};
67
Chanwoo Choi154f757f2012-11-27 09:40:32 +090068enum max77693_muic_cable_group {
69 MAX77693_CABLE_GROUP_ADC = 0,
70 MAX77693_CABLE_GROUP_ADC_GND,
71 MAX77693_CABLE_GROUP_CHG,
72 MAX77693_CABLE_GROUP_VBVOLT,
73};
74
Chanwoo Choidb1b9032012-07-17 13:28:28 +090075enum max77693_muic_charger_type {
76 MAX77693_CHARGER_TYPE_NONE = 0,
77 MAX77693_CHARGER_TYPE_USB,
78 MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
79 MAX77693_CHARGER_TYPE_DEDICATED_CHG,
80 MAX77693_CHARGER_TYPE_APPLE_500MA,
81 MAX77693_CHARGER_TYPE_APPLE_1A_2A,
82 MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
83};
84
85/**
86 * struct max77693_muic_irq
87 * @irq: the index of irq list of MUIC device.
88 * @name: the name of irq.
89 * @virq: the virtual irq to use irq domain
90 */
91struct max77693_muic_irq {
92 unsigned int irq;
93 const char *name;
94 unsigned int virq;
95};
96
97static struct max77693_muic_irq muic_irqs[] = {
98 { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" },
99 { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" },
100 { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" },
101 { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" },
102 { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
103 { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" },
104 { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
105 { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" },
106 { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
107 { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" },
108 { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" },
109 { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" },
110 { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" },
111 { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" },
112 { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" },
113 { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" },
114};
115
116/* Define supported accessory type */
117enum max77693_muic_acc_type {
118 MAX77693_MUIC_ADC_GROUND = 0x0,
119 MAX77693_MUIC_ADC_SEND_END_BUTTON,
120 MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
121 MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
122 MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
123 MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
124 MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
125 MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
126 MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
127 MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
128 MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
129 MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
130 MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
131 MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
132 MAX77693_MUIC_ADC_RESERVED_ACC_1,
133 MAX77693_MUIC_ADC_RESERVED_ACC_2,
134 MAX77693_MUIC_ADC_RESERVED_ACC_3,
135 MAX77693_MUIC_ADC_RESERVED_ACC_4,
136 MAX77693_MUIC_ADC_RESERVED_ACC_5,
137 MAX77693_MUIC_ADC_CEA936_AUDIO,
138 MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
139 MAX77693_MUIC_ADC_TTY_CONVERTER,
140 MAX77693_MUIC_ADC_UART_CABLE,
141 MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
142 MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
143 MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
144 MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
145 MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
146 MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
147 MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
148 MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
149 MAX77693_MUIC_ADC_OPEN,
150
151 /* The below accessories have same ADC value so ADCLow and
152 ADC1K bit is used to separate specific accessory */
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900153 MAX77693_MUIC_GND_USB_OTG = 0x100, /* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */
154 MAX77693_MUIC_GND_USB_OTG_VB = 0x104, /* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */
155 MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */
156 MAX77693_MUIC_GND_MHL = 0x103, /* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */
157 MAX77693_MUIC_GND_MHL_VB = 0x107, /* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900158};
159
160/* MAX77693 MUIC device support below list of accessories(external connector) */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900161enum {
162 EXTCON_CABLE_USB = 0,
163 EXTCON_CABLE_USB_HOST,
164 EXTCON_CABLE_TA,
165 EXTCON_CABLE_FAST_CHARGER,
166 EXTCON_CABLE_SLOW_CHARGER,
167 EXTCON_CABLE_CHARGE_DOWNSTREAM,
168 EXTCON_CABLE_MHL,
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900169 EXTCON_CABLE_MHL_TA,
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900170 EXTCON_CABLE_JIG_USB_ON,
171 EXTCON_CABLE_JIG_USB_OFF,
172 EXTCON_CABLE_JIG_UART_OFF,
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900173 EXTCON_CABLE_JIG_UART_ON,
174 EXTCON_CABLE_DOCK_SMART,
175 EXTCON_CABLE_DOCK_DESK,
176 EXTCON_CABLE_DOCK_AUDIO,
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900177
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900178 _EXTCON_CABLE_NUM,
179};
180
181const char *max77693_extcon_cable[] = {
182 [EXTCON_CABLE_USB] = "USB",
183 [EXTCON_CABLE_USB_HOST] = "USB-Host",
184 [EXTCON_CABLE_TA] = "TA",
185 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
186 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
187 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
188 [EXTCON_CABLE_MHL] = "MHL",
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900189 [EXTCON_CABLE_MHL_TA] = "MHL_TA",
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900190 [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON",
191 [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF",
192 [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF",
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900193 [EXTCON_CABLE_JIG_UART_ON] = "Dock-Car",
194 [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart",
195 [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
196 [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio",
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900197
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900198 NULL,
199};
200
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900201/*
202 * max77693_muic_set_debounce_time - Set the debounce time of ADC
203 * @info: the instance including private data of max77693 MUIC
204 * @time: the debounce time of ADC
205 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900206static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
207 enum max77693_muic_adc_debounce_time time)
208{
Axel Linbf2627d2012-10-04 09:55:23 +0900209 int ret;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900210
211 switch (time) {
212 case ADC_DEBOUNCE_TIME_5MS:
213 case ADC_DEBOUNCE_TIME_10MS:
214 case ADC_DEBOUNCE_TIME_25MS:
215 case ADC_DEBOUNCE_TIME_38_62MS:
Axel Linbf2627d2012-10-04 09:55:23 +0900216 ret = max77693_update_reg(info->max77693->regmap_muic,
217 MAX77693_MUIC_REG_CTRL3,
218 time << CONTROL3_ADCDBSET_SHIFT,
219 CONTROL3_ADCDBSET_MASK);
220 if (ret)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900221 dev_err(info->dev, "failed to set ADC debounce time\n");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900222 break;
223 default:
224 dev_err(info->dev, "invalid ADC debounce time\n");
225 ret = -EINVAL;
226 break;
227 }
228
229 return ret;
230};
231
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900232/*
233 * max77693_muic_set_path - Set hardware line according to attached cable
234 * @info: the instance including private data of max77693 MUIC
235 * @value: the path according to attached cable
236 * @attached: the state of cable (true:attached, false:detached)
237 *
238 * The max77693 MUIC device share outside H/W line among a varity of cables
239 * so, this function set internal path of H/W line according to the type of
240 * attached cable.
241 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900242static int max77693_muic_set_path(struct max77693_muic_info *info,
243 u8 val, bool attached)
244{
245 int ret = 0;
246 u8 ctrl1, ctrl2 = 0;
247
248 if (attached)
249 ctrl1 = val;
250 else
251 ctrl1 = CONTROL1_SW_OPEN;
252
253 ret = max77693_update_reg(info->max77693->regmap_muic,
254 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
255 if (ret < 0) {
256 dev_err(info->dev, "failed to update MUIC register\n");
257 goto out;
258 }
259
260 if (attached)
261 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
262 else
263 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
264
265 ret = max77693_update_reg(info->max77693->regmap_muic,
266 MAX77693_MUIC_REG_CTRL2, ctrl2,
267 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
268 if (ret < 0) {
269 dev_err(info->dev, "failed to update MUIC register\n");
270 goto out;
271 }
272
273 dev_info(info->dev,
274 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
275 ctrl1, ctrl2, attached ? "attached" : "detached");
276out:
277 return ret;
278}
279
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900280/*
281 * max77693_muic_get_cable_type - Return cable type and check cable state
282 * @info: the instance including private data of max77693 MUIC
283 * @group: the path according to attached cable
284 * @attached: store cable state and return
285 *
286 * This function check the cable state either attached or detached,
287 * and then divide precise type of cable according to cable group.
288 * - MAX77693_CABLE_GROUP_ADC
289 * - MAX77693_CABLE_GROUP_ADC_GND
290 * - MAX77693_CABLE_GROUP_CHG
291 * - MAX77693_CABLE_GROUP_VBVOLT
292 */
293static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
294 enum max77693_muic_cable_group group, bool *attached)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900295{
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900296 int cable_type = 0;
297 int adc;
298 int adc1k;
299 int adclow;
300 int vbvolt;
301 int chg_type;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900302
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900303 switch (group) {
304 case MAX77693_CABLE_GROUP_ADC:
305 /*
306 * Read ADC value to check cable type and decide cable state
307 * according to cable type
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900308 */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900309 adc = info->status[0] & STATUS1_ADC_MASK;
310 adc >>= STATUS1_ADC_SHIFT;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900311
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900312 /*
313 * Check current cable state/cable type and store cable type
314 * (info->prev_cable_type) for handling cable when cable is
315 * detached.
316 */
317 if (adc == MAX77693_MUIC_ADC_OPEN) {
318 *attached = false;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900319
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900320 cable_type = info->prev_cable_type;
321 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
322 } else {
323 *attached = true;
324
325 cable_type = info->prev_cable_type = adc;
326 }
327 break;
328 case MAX77693_CABLE_GROUP_ADC_GND:
329 /*
330 * Read ADC value to check cable type and decide cable state
331 * according to cable type
332 */
333 adc = info->status[0] & STATUS1_ADC_MASK;
334 adc >>= STATUS1_ADC_SHIFT;
335
336 /*
337 * Check current cable state/cable type and store cable type
338 * (info->prev_cable_type/_gnd) for handling cable when cable
339 * is detached.
340 */
341 if (adc == MAX77693_MUIC_ADC_OPEN) {
342 *attached = false;
343
344 cable_type = info->prev_cable_type_gnd;
345 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
346 } else {
347 *attached = true;
348
349 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
350 adclow >>= STATUS1_ADCLOW_SHIFT;
351 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
352 adc1k >>= STATUS1_ADC1K_SHIFT;
353
354 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
355 vbvolt >>= STATUS2_VBVOLT_SHIFT;
356
357 /**
358 * [0x1][VBVolt][ADCLow][ADC1K]
359 * [0x1 0 0 0 ] : USB_OTG
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900360 * [0x1 1 0 0 ] : USB_OTG_VB
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900361 * [0x1 0 1 0 ] : Audio Video Cable with load
362 * [0x1 0 1 1 ] : MHL without charging connector
363 * [0x1 1 1 1 ] : MHL with charging connector
364 */
365 cable_type = ((0x1 << 8)
366 | (vbvolt << 2)
367 | (adclow << 1)
368 | adc1k);
369
370 info->prev_cable_type = adc;
371 info->prev_cable_type_gnd = cable_type;
372 }
373
374 break;
375 case MAX77693_CABLE_GROUP_CHG:
376 /*
377 * Read charger type to check cable type and decide cable state
378 * according to type of charger cable.
379 */
380 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
381 chg_type >>= STATUS2_CHGTYP_SHIFT;
382
383 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
384 *attached = false;
385
386 cable_type = info->prev_chg_type;
387 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
388 } else {
389 *attached = true;
390
391 /*
392 * Check current cable state/cable type and store cable
393 * type(info->prev_chg_type) for handling cable when
394 * charger cable is detached.
395 */
396 cable_type = info->prev_chg_type = chg_type;
397 }
398
399 break;
400 case MAX77693_CABLE_GROUP_VBVOLT:
401 /*
402 * Read ADC value to check cable type and decide cable state
403 * according to cable type
404 */
405 adc = info->status[0] & STATUS1_ADC_MASK;
406 adc >>= STATUS1_ADC_SHIFT;
407 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
408 chg_type >>= STATUS2_CHGTYP_SHIFT;
409
410 if (adc == MAX77693_MUIC_ADC_OPEN
411 && chg_type == MAX77693_CHARGER_TYPE_NONE)
412 *attached = false;
413 else
414 *attached = true;
415
416 /*
417 * Read vbvolt field, if vbvolt is 1,
418 * this cable is used for charging.
419 */
420 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
421 vbvolt >>= STATUS2_VBVOLT_SHIFT;
422
423 cable_type = vbvolt;
424 break;
425 default:
426 dev_err(info->dev, "Unknown cable group (%d)\n", group);
427 cable_type = -EINVAL;
428 break;
429 }
430
431 return cable_type;
432}
433
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900434static int max77693_muic_dock_handler(struct max77693_muic_info *info,
435 int cable_type, bool attached)
436{
437 int ret = 0;
438 char dock_name[CABLE_NAME_MAX];
439
440 dev_info(info->dev,
441 "external connector is %s (adc:0x%02x)\n",
442 attached ? "attached" : "detached", cable_type);
443
444 switch (cable_type) {
445 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
446 /* PATH:AP_USB */
447 ret = max77693_muic_set_path(info,
448 CONTROL1_SW_USB, attached);
449 if (ret < 0)
450 goto out;
451
452 /* Dock-Smart */
453 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
454 goto out;
455 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
456 strcpy(dock_name, "Dock-Car");
457 break;
458 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
459 strcpy(dock_name, "Dock-Desk");
460 break;
461 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
462 strcpy(dock_name, "Dock-Audio");
463 if (!attached)
464 extcon_set_cable_state(info->edev, "USB", false);
465 break;
466 }
467
468 /* Dock-Car/Desk/Audio, PATH:AUDIO */
469 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
470 if (ret < 0)
471 goto out;
472 extcon_set_cable_state(info->edev, dock_name, attached);
473
474out:
475 return ret;
476}
477
478static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
479 int button_type, bool attached)
480{
481 struct input_dev *dock = info->dock;
482 unsigned int code;
483 int ret = 0;
484
485 switch (button_type) {
486 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
487 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
488 /* DOCK_KEY_PREV */
489 code = KEY_PREVIOUSSONG;
490 break;
491 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
492 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
493 /* DOCK_KEY_NEXT */
494 code = KEY_NEXTSONG;
495 break;
496 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
497 /* DOCK_VOL_DOWN */
498 code = KEY_VOLUMEDOWN;
499 break;
500 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
501 /* DOCK_VOL_UP */
502 code = KEY_VOLUMEUP;
503 break;
504 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
505 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
506 /* DOCK_KEY_PLAY_PAUSE */
507 code = KEY_PLAYPAUSE;
508 break;
509 default:
510 dev_err(info->dev,
511 "failed to detect %s key (adc:0x%x)\n",
512 attached ? "pressed" : "released", button_type);
513 ret = -EINVAL;
514 goto out;
515 }
516
517 input_event(dock, EV_KEY, code, attached);
518 input_sync(dock);
519
520out:
521 return 0;
522}
523
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900524static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
525{
526 int cable_type_gnd;
527 int ret = 0;
528 bool attached;
529
530 cable_type_gnd = max77693_muic_get_cable_type(info,
531 MAX77693_CABLE_GROUP_ADC_GND, &attached);
532
533 switch (cable_type_gnd) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900534 case MAX77693_MUIC_GND_USB_OTG:
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900535 case MAX77693_MUIC_GND_USB_OTG_VB:
536 /* USB_OTG, PATH: AP_USB */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900537 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
538 if (ret < 0)
539 goto out;
540 extcon_set_cable_state(info->edev, "USB-Host", attached);
541 break;
542 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900543 /* Audio Video Cable with load, PATH:AUDIO */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900544 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
545 if (ret < 0)
546 goto out;
547 extcon_set_cable_state(info->edev,
548 "Audio-video-load", attached);
549 break;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900550 case MAX77693_MUIC_GND_MHL:
551 case MAX77693_MUIC_GND_MHL_VB:
552 /* MHL or MHL with USB/TA cable */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900553 extcon_set_cable_state(info->edev, "MHL", attached);
554 break;
555 default:
Peter Meerwaldafcfaa82012-08-23 09:11:47 +0900556 dev_err(info->dev, "failed to detect %s accessory\n",
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900557 attached ? "attached" : "detached");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900558 ret = -EINVAL;
559 break;
560 }
561
562out:
563 return ret;
564}
565
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900566static int max77693_muic_jig_handler(struct max77693_muic_info *info,
567 int cable_type, bool attached)
568{
569 char cable_name[32];
570 int ret = 0;
571 u8 path = CONTROL1_SW_OPEN;
572
573 dev_info(info->dev,
574 "external connector is %s (adc:0x%02x)\n",
575 attached ? "attached" : "detached", cable_type);
576
577 switch (cable_type) {
578 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
579 /* PATH:AP_USB */
580 strcpy(cable_name, "JIG-USB-OFF");
581 path = CONTROL1_SW_USB;
582 break;
583 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
584 /* PATH:AP_USB */
585 strcpy(cable_name, "JIG-USB-ON");
586 path = CONTROL1_SW_USB;
587 break;
588 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
589 /* PATH:AP_UART */
590 strcpy(cable_name, "JIG-UART-OFF");
591 path = CONTROL1_SW_UART;
592 break;
593 }
594
595 ret = max77693_muic_set_path(info, path, attached);
596 if (ret < 0)
597 goto out;
598
599 extcon_set_cable_state(info->edev, cable_name, attached);
600out:
601 return ret;
602}
603
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900604static int max77693_muic_adc_handler(struct max77693_muic_info *info)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900605{
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900606 int cable_type;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900607 int button_type;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900608 bool attached;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900609 int ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900610
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900611 /* Check accessory state which is either detached or attached */
612 cable_type = max77693_muic_get_cable_type(info,
613 MAX77693_CABLE_GROUP_ADC, &attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900614
615 dev_info(info->dev,
616 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900617 attached ? "attached" : "detached", cable_type,
618 info->prev_cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900619
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900620 switch (cable_type) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900621 case MAX77693_MUIC_ADC_GROUND:
622 /* USB_OTG/MHL/Audio */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900623 max77693_muic_adc_ground_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900624 break;
625 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
626 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900627 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900628 /* JIG */
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900629 ret = max77693_muic_jig_handler(info, cable_type, attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900630 if (ret < 0)
631 goto out;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900632 break;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900633 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
634 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
635 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
636 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
637 /*
638 * DOCK device
639 *
640 * The MAX77693 MUIC device can detect total 34 cable type
641 * except of charger cable and MUIC device didn't define
642 * specfic role of cable in the range of from 0x01 to 0x12
643 * of ADC value. So, can use/define cable with no role according
644 * to schema of hardware board.
645 */
646 ret = max77693_muic_dock_handler(info, cable_type, attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900647 if (ret < 0)
648 goto out;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900649 break;
650 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
651 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
652 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
653 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
654 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
655 /*
656 * Button of DOCK device
657 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
658 *
659 * The MAX77693 MUIC device can detect total 34 cable type
660 * except of charger cable and MUIC device didn't define
661 * specfic role of cable in the range of from 0x01 to 0x12
662 * of ADC value. So, can use/define cable with no role according
663 * to schema of hardware board.
664 */
665 if (attached)
666 button_type = info->prev_button_type = cable_type;
667 else
668 button_type = info->prev_button_type;
669
670 ret = max77693_muic_dock_button_handler(info, button_type,
671 attached);
672 if (ret < 0)
673 goto out;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900674 break;
675 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
676 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
677 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900678 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
679 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
680 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900681 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900682 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900683 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
684 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900685 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
686 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
687 case MAX77693_MUIC_ADC_CEA936_AUDIO:
688 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
689 case MAX77693_MUIC_ADC_TTY_CONVERTER:
690 case MAX77693_MUIC_ADC_UART_CABLE:
691 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900692 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900693 /*
694 * This accessory isn't used in general case if it is specially
695 * needed to detect additional accessory, should implement
696 * proper operation when this accessory is attached/detached.
697 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900698 dev_info(info->dev,
699 "accessory is %s but it isn't used (adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900700 attached ? "attached" : "detached", cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900701 goto out;
702 default:
703 dev_err(info->dev,
704 "failed to detect %s accessory (adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900705 attached ? "attached" : "detached", cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900706 ret = -EINVAL;
707 goto out;
708 }
709
710out:
711 return ret;
712}
713
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900714static int max77693_muic_chg_handler(struct max77693_muic_info *info)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900715{
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900716 int chg_type;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900717 int cable_type_gnd;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900718 int cable_type;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900719 bool attached;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900720 bool cable_attached;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900721 int ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900722
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900723 chg_type = max77693_muic_get_cable_type(info,
724 MAX77693_CABLE_GROUP_CHG, &attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900725
726 dev_info(info->dev,
727 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
728 attached ? "attached" : "detached",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900729 chg_type, info->prev_chg_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900730
731 switch (chg_type) {
732 case MAX77693_CHARGER_TYPE_USB:
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900733 /*
734 * MHL_TA(USB/TA) with MHL cable
735 * - MHL cable include two port(HDMI line and separate micro
736 * -usb port. When the target connect MHL cable, extcon driver
737 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
738 * cable is connected, extcon driver notify state to notifiee
739 * for charging battery.
740 */
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900741 cable_type_gnd = max77693_muic_get_cable_type(info,
742 MAX77693_CABLE_GROUP_ADC_GND,
743 &cable_attached);
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900744 if (cable_type_gnd == MAX77693_MUIC_GND_MHL
745 || cable_type_gnd == MAX77693_MUIC_GND_MHL_VB) {
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900746 extcon_set_cable_state(info->edev, "MHL_TA", attached);
747
748 if (!cable_attached)
749 extcon_set_cable_state(info->edev,
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900750 "MHL", false);
751 goto out;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900752 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900753
754 /*
755 * USB/TA cable with Dock-Audio device
756 * - Dock device include two port(Dock-Audio and micro-usb
757 * port). When the target connect Dock-Audio device, extcon
758 * driver check whether USB/TA cable is connected.
759 * If USB/TA cable is connected, extcon driver notify state
760 * to notifiee for charging battery.
761 */
762 cable_type = max77693_muic_get_cable_type(info,
763 MAX77693_CABLE_GROUP_ADC,
764 &cable_attached);
765 if (cable_type == MAX77693_MUIC_ADC_AV_CABLE_NOLOAD) {
766 extcon_set_cable_state(info->edev, "USB", attached);
767
768 if (!cable_attached)
769 extcon_set_cable_state(info->edev,
770 "Dock-Audio", false);
771 goto out;
772 }
773
774 /* Only USB cable, PATH:AP_USB */
775 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
776 if (ret < 0)
777 goto out;
778 extcon_set_cable_state(info->edev, "USB", attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900779 break;
780 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
781 extcon_set_cable_state(info->edev,
782 "Charge-downstream", attached);
783 break;
784 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
785 extcon_set_cable_state(info->edev, "TA", attached);
786 break;
787 case MAX77693_CHARGER_TYPE_APPLE_500MA:
788 extcon_set_cable_state(info->edev, "Slow-charger", attached);
789 break;
790 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
791 extcon_set_cable_state(info->edev, "Fast-charger", attached);
792 break;
793 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
794 break;
795 default:
796 dev_err(info->dev,
797 "failed to detect %s accessory (chg_type:0x%x)\n",
798 attached ? "attached" : "detached", chg_type);
799 ret = -EINVAL;
800 goto out;
801 }
802
803out:
804 return ret;
805}
806
807static void max77693_muic_irq_work(struct work_struct *work)
808{
809 struct max77693_muic_info *info = container_of(work,
810 struct max77693_muic_info, irq_work);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900811 int irq_type = -1;
812 int i, ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900813
814 if (!info->edev)
815 return;
816
817 mutex_lock(&info->mutex);
818
819 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
820 if (info->irq == muic_irqs[i].virq)
821 irq_type = muic_irqs[i].irq;
822
823 ret = max77693_bulk_read(info->max77693->regmap_muic,
824 MAX77693_MUIC_REG_STATUS1, 2, info->status);
825 if (ret) {
826 dev_err(info->dev, "failed to read MUIC register\n");
827 mutex_unlock(&info->mutex);
828 return;
829 }
830
831 switch (irq_type) {
832 case MAX77693_MUIC_IRQ_INT1_ADC:
833 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
834 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
835 case MAX77693_MUIC_IRQ_INT1_ADC1K:
836 /* Handle all of accessory except for
837 type of charger accessory */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900838 ret = max77693_muic_adc_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900839 break;
840 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
841 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
842 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
843 case MAX77693_MUIC_IRQ_INT2_DXOVP:
844 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
845 case MAX77693_MUIC_IRQ_INT2_VIDRM:
846 /* Handle charger accessory */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900847 ret = max77693_muic_chg_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900848 break;
849 case MAX77693_MUIC_IRQ_INT3_EOC:
850 case MAX77693_MUIC_IRQ_INT3_CGMBC:
851 case MAX77693_MUIC_IRQ_INT3_OVP:
852 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
853 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
854 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
855 break;
856 default:
857 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
858 irq_type);
859 break;
860 }
861
862 if (ret < 0)
863 dev_err(info->dev, "failed to handle MUIC interrupt\n");
864
865 mutex_unlock(&info->mutex);
866
867 return;
868}
869
870static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
871{
872 struct max77693_muic_info *info = data;
873
874 info->irq = irq;
875 schedule_work(&info->irq_work);
876
877 return IRQ_HANDLED;
878}
879
880static struct regmap_config max77693_muic_regmap_config = {
881 .reg_bits = 8,
882 .val_bits = 8,
883};
884
885static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
886{
887 int ret = 0;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900888 int adc;
889 int chg_type;
890 bool attached;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900891
892 mutex_lock(&info->mutex);
893
894 /* Read STATUSx register to detect accessory */
895 ret = max77693_bulk_read(info->max77693->regmap_muic,
896 MAX77693_MUIC_REG_STATUS1, 2, info->status);
897 if (ret) {
898 dev_err(info->dev, "failed to read MUIC register\n");
899 mutex_unlock(&info->mutex);
900 return -EINVAL;
901 }
902
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900903 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
904 &attached);
905 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
906 ret = max77693_muic_adc_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900907 if (ret < 0)
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900908 dev_err(info->dev, "Cannot detect accessory\n");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900909 }
910
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900911 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
912 &attached);
913 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
914 ret = max77693_muic_chg_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900915 if (ret < 0)
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900916 dev_err(info->dev, "Cannot detect charger accessory\n");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900917 }
918
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900919 mutex_unlock(&info->mutex);
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900920
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900921 return ret;
922}
923
Chanwoo Choi297620f2012-12-26 13:10:11 +0900924static void max77693_muic_detect_cable_wq(struct work_struct *work)
925{
926 struct max77693_muic_info *info = container_of(to_delayed_work(work),
927 struct max77693_muic_info, wq_detcable);
928
929 max77693_muic_detect_accessory(info);
930}
931
Bill Pemberton44f34fd2012-11-19 13:23:21 -0500932static int max77693_muic_probe(struct platform_device *pdev)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900933{
934 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
Chanwoo Choif8457d572012-10-08 14:41:49 +0900935 struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
936 struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900937 struct max77693_muic_info *info;
Chanwoo Choi297620f2012-12-26 13:10:11 +0900938 int delay_jiffies;
939 int ret;
940 int i;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900941 u8 id;
942
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +0900943 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
944 GFP_KERNEL);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900945 if (!info) {
946 dev_err(&pdev->dev, "failed to allocate memory\n");
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +0900947 return -ENOMEM;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900948 }
949 info->dev = &pdev->dev;
950 info->max77693 = max77693;
Sachin Kamat1967fa02012-11-21 10:04:58 +0530951 if (info->max77693->regmap_muic) {
Chanwoo Choib186b122012-08-21 15:16:23 +0900952 dev_dbg(&pdev->dev, "allocate register map\n");
Sachin Kamat1967fa02012-11-21 10:04:58 +0530953 } else {
Chanwoo Choib186b122012-08-21 15:16:23 +0900954 info->max77693->regmap_muic = devm_regmap_init_i2c(
955 info->max77693->muic,
956 &max77693_muic_regmap_config);
957 if (IS_ERR(info->max77693->regmap_muic)) {
958 ret = PTR_ERR(info->max77693->regmap_muic);
959 dev_err(max77693->dev,
960 "failed to allocate register map: %d\n", ret);
Sachin Kamat3bf742f2012-11-21 10:04:57 +0530961 return ret;
Chanwoo Choib186b122012-08-21 15:16:23 +0900962 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900963 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900964
965 /* Register input device for button of dock device */
966 info->dock = input_allocate_device();
967 if (!info->dock) {
968 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
969 return -ENOMEM;
970 }
971 info->dock->name = "max77693-muic/dock";
972 info->dock->phys = "max77693-muic/extcon";
973 info->dock->dev.parent = &pdev->dev;
974
975 __set_bit(EV_REP, info->dock->evbit);
976
977 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
978 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
979 input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
980 input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
981 input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
982
983 ret = input_register_device(info->dock);
984 if (ret < 0) {
985 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
986 ret);
987 return ret;
988 }
989
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900990 platform_set_drvdata(pdev, info);
991 mutex_init(&info->mutex);
992
993 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
994
995 /* Support irq domain for MAX77693 MUIC device */
996 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
997 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
Sachin Kamat00af4b12012-11-20 15:46:44 +0900998 unsigned int virq = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900999
1000 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
Sachin Kamat00af4b12012-11-20 15:46:44 +09001001 if (!virq) {
1002 ret = -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001003 goto err_irq;
Sachin Kamat00af4b12012-11-20 15:46:44 +09001004 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001005 muic_irq->virq = virq;
1006
1007 ret = request_threaded_irq(virq, NULL,
1008 max77693_muic_irq_handler,
Fengguang Wuaa493122012-09-06 11:56:35 +08001009 IRQF_ONESHOT, muic_irq->name, info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001010 if (ret) {
1011 dev_err(&pdev->dev,
1012 "failed: irq request (IRQ: %d,"
1013 " error :%d)\n",
1014 muic_irq->irq, ret);
1015
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001016 goto err_irq;
1017 }
1018 }
1019
1020 /* Initialize extcon device */
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001021 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1022 GFP_KERNEL);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001023 if (!info->edev) {
1024 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1025 ret = -ENOMEM;
1026 goto err_irq;
1027 }
1028 info->edev->name = DEV_NAME;
1029 info->edev->supported_cable = max77693_extcon_cable;
1030 ret = extcon_dev_register(info->edev, NULL);
1031 if (ret) {
1032 dev_err(&pdev->dev, "failed to register extcon device\n");
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001033 goto err_irq;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001034 }
1035
Chanwoo Choif8457d572012-10-08 14:41:49 +09001036 /* Initialize MUIC register by using platform data */
1037 for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1038 enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1039
1040 max77693_write_reg(info->max77693->regmap_muic,
1041 muic_pdata->init_data[i].addr,
1042 muic_pdata->init_data[i].data);
1043
1044 switch (muic_pdata->init_data[i].addr) {
1045 case MAX77693_MUIC_REG_INTMASK1:
1046 irq_src = MUIC_INT1;
1047 break;
1048 case MAX77693_MUIC_REG_INTMASK2:
1049 irq_src = MUIC_INT2;
1050 break;
1051 case MAX77693_MUIC_REG_INTMASK3:
1052 irq_src = MUIC_INT3;
1053 break;
1054 }
1055
1056 if (irq_src < MAX77693_IRQ_GROUP_NR)
1057 info->max77693->irq_masks_cur[irq_src]
1058 = muic_pdata->init_data[i].data;
1059 }
1060
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001061 /* Check revision number of MUIC device*/
1062 ret = max77693_read_reg(info->max77693->regmap_muic,
1063 MAX77693_MUIC_REG_ID, &id);
1064 if (ret < 0) {
1065 dev_err(&pdev->dev, "failed to read revision number\n");
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001066 goto err_extcon;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001067 }
1068 dev_info(info->dev, "device ID : 0x%x\n", id);
1069
1070 /* Set ADC debounce time */
1071 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1072
Chanwoo Choi297620f2012-12-26 13:10:11 +09001073 /*
1074 * Detect accessory after completing the initialization of platform
1075 *
1076 * - Use delayed workqueue to detect cable state and then
1077 * notify cable state to notifiee/platform through uevent.
1078 * After completing the booting of platform, the extcon provider
1079 * driver should notify cable state to upper layer.
1080 */
1081 INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1082 if (muic_pdata->detcable_delay_ms)
1083 delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1084 else
1085 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1086 schedule_delayed_work(&info->wq_detcable, delay_jiffies);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001087
1088 return ret;
1089
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001090err_extcon:
1091 extcon_dev_unregister(info->edev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001092err_irq:
Sachin Kamat00af4b12012-11-20 15:46:44 +09001093 while (--i >= 0)
1094 free_irq(muic_irqs[i].virq, info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001095 return ret;
1096}
1097
Bill Pemberton93ed0322012-11-19 13:25:49 -05001098static int max77693_muic_remove(struct platform_device *pdev)
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001099{
1100 struct max77693_muic_info *info = platform_get_drvdata(pdev);
1101 int i;
1102
1103 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1104 free_irq(muic_irqs[i].virq, info);
1105 cancel_work_sync(&info->irq_work);
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001106 input_unregister_device(info->dock);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001107 extcon_dev_unregister(info->edev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001108
1109 return 0;
1110}
1111
1112static struct platform_driver max77693_muic_driver = {
1113 .driver = {
1114 .name = DEV_NAME,
1115 .owner = THIS_MODULE,
1116 },
1117 .probe = max77693_muic_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -05001118 .remove = max77693_muic_remove,
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001119};
1120
1121module_platform_driver(max77693_muic_driver);
1122
1123MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1124MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1125MODULE_LICENSE("GPL");
1126MODULE_ALIAS("platform:extcon-max77693");