blob: abab068adc358eb820c357f632be4dec96895954 [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
188const char *max77693_extcon_cable[] = {
189 [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);
227 if (ret)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900228 dev_err(info->dev, "failed to set ADC debounce time\n");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900229 break;
230 default:
231 dev_err(info->dev, "invalid ADC debounce time\n");
232 ret = -EINVAL;
233 break;
234 }
235
236 return ret;
237};
238
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900239/*
240 * max77693_muic_set_path - Set hardware line according to attached cable
241 * @info: the instance including private data of max77693 MUIC
242 * @value: the path according to attached cable
243 * @attached: the state of cable (true:attached, false:detached)
244 *
245 * The max77693 MUIC device share outside H/W line among a varity of cables
246 * so, this function set internal path of H/W line according to the type of
247 * attached cable.
248 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900249static int max77693_muic_set_path(struct max77693_muic_info *info,
250 u8 val, bool attached)
251{
252 int ret = 0;
253 u8 ctrl1, ctrl2 = 0;
254
255 if (attached)
256 ctrl1 = val;
257 else
258 ctrl1 = CONTROL1_SW_OPEN;
259
260 ret = max77693_update_reg(info->max77693->regmap_muic,
261 MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK);
262 if (ret < 0) {
263 dev_err(info->dev, "failed to update MUIC register\n");
264 goto out;
265 }
266
267 if (attached)
268 ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
269 else
270 ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
271
272 ret = max77693_update_reg(info->max77693->regmap_muic,
273 MAX77693_MUIC_REG_CTRL2, ctrl2,
274 CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
275 if (ret < 0) {
276 dev_err(info->dev, "failed to update MUIC register\n");
277 goto out;
278 }
279
280 dev_info(info->dev,
281 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
282 ctrl1, ctrl2, attached ? "attached" : "detached");
283out:
284 return ret;
285}
286
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900287/*
288 * max77693_muic_get_cable_type - Return cable type and check cable state
289 * @info: the instance including private data of max77693 MUIC
290 * @group: the path according to attached cable
291 * @attached: store cable state and return
292 *
293 * This function check the cable state either attached or detached,
294 * and then divide precise type of cable according to cable group.
295 * - MAX77693_CABLE_GROUP_ADC
296 * - MAX77693_CABLE_GROUP_ADC_GND
297 * - MAX77693_CABLE_GROUP_CHG
298 * - MAX77693_CABLE_GROUP_VBVOLT
299 */
300static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
301 enum max77693_muic_cable_group group, bool *attached)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900302{
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900303 int cable_type = 0;
304 int adc;
305 int adc1k;
306 int adclow;
307 int vbvolt;
308 int chg_type;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900309
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900310 switch (group) {
311 case MAX77693_CABLE_GROUP_ADC:
312 /*
313 * Read ADC value to check cable type and decide cable state
314 * according to cable type
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900315 */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900316 adc = info->status[0] & STATUS1_ADC_MASK;
317 adc >>= STATUS1_ADC_SHIFT;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900318
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900319 /*
320 * Check current cable state/cable type and store cable type
321 * (info->prev_cable_type) for handling cable when cable is
322 * detached.
323 */
324 if (adc == MAX77693_MUIC_ADC_OPEN) {
325 *attached = false;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900326
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900327 cable_type = info->prev_cable_type;
328 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
329 } else {
330 *attached = true;
331
332 cable_type = info->prev_cable_type = adc;
333 }
334 break;
335 case MAX77693_CABLE_GROUP_ADC_GND:
336 /*
337 * Read ADC value to check cable type and decide cable state
338 * according to cable type
339 */
340 adc = info->status[0] & STATUS1_ADC_MASK;
341 adc >>= STATUS1_ADC_SHIFT;
342
343 /*
344 * Check current cable state/cable type and store cable type
345 * (info->prev_cable_type/_gnd) for handling cable when cable
346 * is detached.
347 */
348 if (adc == MAX77693_MUIC_ADC_OPEN) {
349 *attached = false;
350
351 cable_type = info->prev_cable_type_gnd;
352 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
353 } else {
354 *attached = true;
355
356 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
357 adclow >>= STATUS1_ADCLOW_SHIFT;
358 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
359 adc1k >>= STATUS1_ADC1K_SHIFT;
360
361 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
362 vbvolt >>= STATUS2_VBVOLT_SHIFT;
363
364 /**
365 * [0x1][VBVolt][ADCLow][ADC1K]
366 * [0x1 0 0 0 ] : USB_OTG
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900367 * [0x1 1 0 0 ] : USB_OTG_VB
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900368 * [0x1 0 1 0 ] : Audio Video Cable with load
369 * [0x1 0 1 1 ] : MHL without charging connector
370 * [0x1 1 1 1 ] : MHL with charging connector
371 */
372 cable_type = ((0x1 << 8)
373 | (vbvolt << 2)
374 | (adclow << 1)
375 | adc1k);
376
377 info->prev_cable_type = adc;
378 info->prev_cable_type_gnd = cable_type;
379 }
380
381 break;
382 case MAX77693_CABLE_GROUP_CHG:
383 /*
384 * Read charger type to check cable type and decide cable state
385 * according to type of charger cable.
386 */
387 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
388 chg_type >>= STATUS2_CHGTYP_SHIFT;
389
390 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
391 *attached = false;
392
393 cable_type = info->prev_chg_type;
394 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
395 } else {
396 *attached = true;
397
398 /*
399 * Check current cable state/cable type and store cable
400 * type(info->prev_chg_type) for handling cable when
401 * charger cable is detached.
402 */
403 cable_type = info->prev_chg_type = chg_type;
404 }
405
406 break;
407 case MAX77693_CABLE_GROUP_VBVOLT:
408 /*
409 * Read ADC value to check cable type and decide cable state
410 * according to cable type
411 */
412 adc = info->status[0] & STATUS1_ADC_MASK;
413 adc >>= STATUS1_ADC_SHIFT;
414 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
415 chg_type >>= STATUS2_CHGTYP_SHIFT;
416
417 if (adc == MAX77693_MUIC_ADC_OPEN
418 && chg_type == MAX77693_CHARGER_TYPE_NONE)
419 *attached = false;
420 else
421 *attached = true;
422
423 /*
424 * Read vbvolt field, if vbvolt is 1,
425 * this cable is used for charging.
426 */
427 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
428 vbvolt >>= STATUS2_VBVOLT_SHIFT;
429
430 cable_type = vbvolt;
431 break;
432 default:
433 dev_err(info->dev, "Unknown cable group (%d)\n", group);
434 cable_type = -EINVAL;
435 break;
436 }
437
438 return cable_type;
439}
440
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900441static int max77693_muic_dock_handler(struct max77693_muic_info *info,
442 int cable_type, bool attached)
443{
444 int ret = 0;
445 char dock_name[CABLE_NAME_MAX];
446
447 dev_info(info->dev,
448 "external connector is %s (adc:0x%02x)\n",
449 attached ? "attached" : "detached", cable_type);
450
451 switch (cable_type) {
452 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
453 /* PATH:AP_USB */
454 ret = max77693_muic_set_path(info,
455 CONTROL1_SW_USB, attached);
456 if (ret < 0)
457 goto out;
458
459 /* Dock-Smart */
460 extcon_set_cable_state(info->edev, "Dock-Smart", attached);
461 goto out;
462 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
463 strcpy(dock_name, "Dock-Car");
464 break;
465 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
466 strcpy(dock_name, "Dock-Desk");
467 break;
468 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
469 strcpy(dock_name, "Dock-Audio");
470 if (!attached)
471 extcon_set_cable_state(info->edev, "USB", false);
472 break;
473 }
474
475 /* Dock-Car/Desk/Audio, PATH:AUDIO */
476 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
477 if (ret < 0)
478 goto out;
479 extcon_set_cable_state(info->edev, dock_name, attached);
480
481out:
482 return ret;
483}
484
485static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
486 int button_type, bool attached)
487{
488 struct input_dev *dock = info->dock;
489 unsigned int code;
490 int ret = 0;
491
492 switch (button_type) {
493 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
494 ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
495 /* DOCK_KEY_PREV */
496 code = KEY_PREVIOUSSONG;
497 break;
498 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
499 ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
500 /* DOCK_KEY_NEXT */
501 code = KEY_NEXTSONG;
502 break;
503 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
504 /* DOCK_VOL_DOWN */
505 code = KEY_VOLUMEDOWN;
506 break;
507 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
508 /* DOCK_VOL_UP */
509 code = KEY_VOLUMEUP;
510 break;
511 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
512 ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
513 /* DOCK_KEY_PLAY_PAUSE */
514 code = KEY_PLAYPAUSE;
515 break;
516 default:
517 dev_err(info->dev,
518 "failed to detect %s key (adc:0x%x)\n",
519 attached ? "pressed" : "released", button_type);
520 ret = -EINVAL;
521 goto out;
522 }
523
524 input_event(dock, EV_KEY, code, attached);
525 input_sync(dock);
526
527out:
528 return 0;
529}
530
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900531static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
532{
533 int cable_type_gnd;
534 int ret = 0;
535 bool attached;
536
537 cable_type_gnd = max77693_muic_get_cable_type(info,
538 MAX77693_CABLE_GROUP_ADC_GND, &attached);
539
540 switch (cable_type_gnd) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900541 case MAX77693_MUIC_GND_USB_OTG:
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900542 case MAX77693_MUIC_GND_USB_OTG_VB:
543 /* USB_OTG, PATH: AP_USB */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900544 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
545 if (ret < 0)
546 goto out;
547 extcon_set_cable_state(info->edev, "USB-Host", attached);
548 break;
549 case MAX77693_MUIC_GND_AV_CABLE_LOAD:
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900550 /* Audio Video Cable with load, PATH:AUDIO */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900551 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
552 if (ret < 0)
553 goto out;
554 extcon_set_cable_state(info->edev,
555 "Audio-video-load", attached);
556 break;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900557 case MAX77693_MUIC_GND_MHL:
558 case MAX77693_MUIC_GND_MHL_VB:
559 /* MHL or MHL with USB/TA cable */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900560 extcon_set_cable_state(info->edev, "MHL", attached);
561 break;
562 default:
Peter Meerwaldafcfaa82012-08-23 09:11:47 +0900563 dev_err(info->dev, "failed to detect %s accessory\n",
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900564 attached ? "attached" : "detached");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900565 ret = -EINVAL;
566 break;
567 }
568
569out:
570 return ret;
571}
572
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900573static int max77693_muic_jig_handler(struct max77693_muic_info *info,
574 int cable_type, bool attached)
575{
576 char cable_name[32];
577 int ret = 0;
578 u8 path = CONTROL1_SW_OPEN;
579
580 dev_info(info->dev,
581 "external connector is %s (adc:0x%02x)\n",
582 attached ? "attached" : "detached", cable_type);
583
584 switch (cable_type) {
585 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
586 /* PATH:AP_USB */
587 strcpy(cable_name, "JIG-USB-OFF");
588 path = CONTROL1_SW_USB;
589 break;
590 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
591 /* PATH:AP_USB */
592 strcpy(cable_name, "JIG-USB-ON");
593 path = CONTROL1_SW_USB;
594 break;
595 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
596 /* PATH:AP_UART */
597 strcpy(cable_name, "JIG-UART-OFF");
598 path = CONTROL1_SW_UART;
599 break;
600 }
601
602 ret = max77693_muic_set_path(info, path, attached);
603 if (ret < 0)
604 goto out;
605
606 extcon_set_cable_state(info->edev, cable_name, attached);
607out:
608 return ret;
609}
610
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900611static int max77693_muic_adc_handler(struct max77693_muic_info *info)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900612{
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900613 int cable_type;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900614 int button_type;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900615 bool attached;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900616 int ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900617
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900618 /* Check accessory state which is either detached or attached */
619 cable_type = max77693_muic_get_cable_type(info,
620 MAX77693_CABLE_GROUP_ADC, &attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900621
622 dev_info(info->dev,
623 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900624 attached ? "attached" : "detached", cable_type,
625 info->prev_cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900626
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900627 switch (cable_type) {
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900628 case MAX77693_MUIC_ADC_GROUND:
629 /* USB_OTG/MHL/Audio */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900630 max77693_muic_adc_ground_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900631 break;
632 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
633 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900634 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900635 /* JIG */
Chanwoo Choid0587eb2012-11-27 12:06:49 +0900636 ret = max77693_muic_jig_handler(info, cable_type, attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900637 if (ret < 0)
638 goto out;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900639 break;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900640 case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */
641 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */
642 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */
643 case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */
644 /*
645 * DOCK device
646 *
647 * The MAX77693 MUIC device can detect total 34 cable type
648 * except of charger cable and MUIC device didn't define
649 * specfic role of cable in the range of from 0x01 to 0x12
650 * of ADC value. So, can use/define cable with no role according
651 * to schema of hardware board.
652 */
653 ret = max77693_muic_dock_handler(info, cable_type, attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900654 if (ret < 0)
655 goto out;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900656 break;
657 case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */
658 case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */
659 case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */
660 case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */
661 case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */
662 /*
663 * Button of DOCK device
664 * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
665 *
666 * The MAX77693 MUIC device can detect total 34 cable type
667 * except of charger cable and MUIC device didn't define
668 * specfic role of cable in the range of from 0x01 to 0x12
669 * of ADC value. So, can use/define cable with no role according
670 * to schema of hardware board.
671 */
672 if (attached)
673 button_type = info->prev_button_type = cable_type;
674 else
675 button_type = info->prev_button_type;
676
677 ret = max77693_muic_dock_button_handler(info, button_type,
678 attached);
679 if (ret < 0)
680 goto out;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900681 break;
682 case MAX77693_MUIC_ADC_SEND_END_BUTTON:
683 case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
684 case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900685 case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
686 case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
687 case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900688 case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900689 case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900690 case MAX77693_MUIC_ADC_RESERVED_ACC_1:
691 case MAX77693_MUIC_ADC_RESERVED_ACC_2:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900692 case MAX77693_MUIC_ADC_RESERVED_ACC_4:
693 case MAX77693_MUIC_ADC_RESERVED_ACC_5:
694 case MAX77693_MUIC_ADC_CEA936_AUDIO:
695 case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
696 case MAX77693_MUIC_ADC_TTY_CONVERTER:
697 case MAX77693_MUIC_ADC_UART_CABLE:
698 case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900699 case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900700 /*
701 * This accessory isn't used in general case if it is specially
702 * needed to detect additional accessory, should implement
703 * proper operation when this accessory is attached/detached.
704 */
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900705 dev_info(info->dev,
706 "accessory is %s but it isn't used (adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900707 attached ? "attached" : "detached", cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900708 goto out;
709 default:
710 dev_err(info->dev,
711 "failed to detect %s accessory (adc:0x%x)\n",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900712 attached ? "attached" : "detached", cable_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900713 ret = -EINVAL;
714 goto out;
715 }
716
717out:
718 return ret;
719}
720
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900721static int max77693_muic_chg_handler(struct max77693_muic_info *info)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900722{
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900723 int chg_type;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900724 int cable_type_gnd;
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900725 int cable_type;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900726 bool attached;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900727 bool cable_attached;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900728 int ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900729
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900730 chg_type = max77693_muic_get_cable_type(info,
731 MAX77693_CABLE_GROUP_CHG, &attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900732
733 dev_info(info->dev,
734 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
735 attached ? "attached" : "detached",
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900736 chg_type, info->prev_chg_type);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900737
738 switch (chg_type) {
739 case MAX77693_CHARGER_TYPE_USB:
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900740 /*
741 * MHL_TA(USB/TA) with MHL cable
742 * - MHL cable include two port(HDMI line and separate micro
743 * -usb port. When the target connect MHL cable, extcon driver
744 * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA
745 * cable is connected, extcon driver notify state to notifiee
746 * for charging battery.
747 */
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900748 cable_type_gnd = max77693_muic_get_cable_type(info,
749 MAX77693_CABLE_GROUP_ADC_GND,
750 &cable_attached);
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900751 if (cable_type_gnd == MAX77693_MUIC_GND_MHL
752 || cable_type_gnd == MAX77693_MUIC_GND_MHL_VB) {
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900753 extcon_set_cable_state(info->edev, "MHL_TA", attached);
754
755 if (!cable_attached)
756 extcon_set_cable_state(info->edev,
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900757 "MHL", false);
758 goto out;
Chanwoo Choi06bed0a2012-11-27 11:30:35 +0900759 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900760
761 /*
762 * USB/TA cable with Dock-Audio device
763 * - Dock device include two port(Dock-Audio and micro-usb
764 * port). When the target connect Dock-Audio device, extcon
765 * driver check whether USB/TA cable is connected.
766 * If USB/TA cable is connected, extcon driver notify state
767 * to notifiee for charging battery.
768 */
769 cable_type = max77693_muic_get_cable_type(info,
770 MAX77693_CABLE_GROUP_ADC,
771 &cable_attached);
772 if (cable_type == MAX77693_MUIC_ADC_AV_CABLE_NOLOAD) {
773 extcon_set_cable_state(info->edev, "USB", attached);
774
775 if (!cable_attached)
776 extcon_set_cable_state(info->edev,
777 "Dock-Audio", false);
778 goto out;
779 }
780
781 /* Only USB cable, PATH:AP_USB */
782 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
783 if (ret < 0)
784 goto out;
785 extcon_set_cable_state(info->edev, "USB", attached);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900786 break;
787 case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
788 extcon_set_cable_state(info->edev,
789 "Charge-downstream", attached);
790 break;
791 case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
792 extcon_set_cable_state(info->edev, "TA", attached);
793 break;
794 case MAX77693_CHARGER_TYPE_APPLE_500MA:
795 extcon_set_cable_state(info->edev, "Slow-charger", attached);
796 break;
797 case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
798 extcon_set_cable_state(info->edev, "Fast-charger", attached);
799 break;
800 case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
801 break;
802 default:
803 dev_err(info->dev,
804 "failed to detect %s accessory (chg_type:0x%x)\n",
805 attached ? "attached" : "detached", chg_type);
806 ret = -EINVAL;
807 goto out;
808 }
809
810out:
811 return ret;
812}
813
814static void max77693_muic_irq_work(struct work_struct *work)
815{
816 struct max77693_muic_info *info = container_of(work,
817 struct max77693_muic_info, irq_work);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900818 int irq_type = -1;
819 int i, ret = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900820
821 if (!info->edev)
822 return;
823
824 mutex_lock(&info->mutex);
825
826 for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
827 if (info->irq == muic_irqs[i].virq)
828 irq_type = muic_irqs[i].irq;
829
830 ret = max77693_bulk_read(info->max77693->regmap_muic,
831 MAX77693_MUIC_REG_STATUS1, 2, info->status);
832 if (ret) {
833 dev_err(info->dev, "failed to read MUIC register\n");
834 mutex_unlock(&info->mutex);
835 return;
836 }
837
838 switch (irq_type) {
839 case MAX77693_MUIC_IRQ_INT1_ADC:
840 case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
841 case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
842 case MAX77693_MUIC_IRQ_INT1_ADC1K:
843 /* Handle all of accessory except for
844 type of charger accessory */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900845 ret = max77693_muic_adc_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900846 break;
847 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
848 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
849 case MAX77693_MUIC_IRQ_INT2_DCDTMR:
850 case MAX77693_MUIC_IRQ_INT2_DXOVP:
851 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
852 case MAX77693_MUIC_IRQ_INT2_VIDRM:
853 /* Handle charger accessory */
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900854 ret = max77693_muic_chg_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900855 break;
856 case MAX77693_MUIC_IRQ_INT3_EOC:
857 case MAX77693_MUIC_IRQ_INT3_CGMBC:
858 case MAX77693_MUIC_IRQ_INT3_OVP:
859 case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
860 case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
861 case MAX77693_MUIC_IRQ_INT3_BAT_DET:
862 break;
863 default:
864 dev_err(info->dev, "muic interrupt: irq %d occurred\n",
865 irq_type);
866 break;
867 }
868
869 if (ret < 0)
870 dev_err(info->dev, "failed to handle MUIC interrupt\n");
871
872 mutex_unlock(&info->mutex);
873
874 return;
875}
876
877static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
878{
879 struct max77693_muic_info *info = data;
880
881 info->irq = irq;
882 schedule_work(&info->irq_work);
883
884 return IRQ_HANDLED;
885}
886
887static struct regmap_config max77693_muic_regmap_config = {
888 .reg_bits = 8,
889 .val_bits = 8,
890};
891
892static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
893{
894 int ret = 0;
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900895 int adc;
896 int chg_type;
897 bool attached;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900898
899 mutex_lock(&info->mutex);
900
901 /* Read STATUSx register to detect accessory */
902 ret = max77693_bulk_read(info->max77693->regmap_muic,
903 MAX77693_MUIC_REG_STATUS1, 2, info->status);
904 if (ret) {
905 dev_err(info->dev, "failed to read MUIC register\n");
906 mutex_unlock(&info->mutex);
907 return -EINVAL;
908 }
909
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900910 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
911 &attached);
912 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
913 ret = max77693_muic_adc_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900914 if (ret < 0)
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900915 dev_err(info->dev, "Cannot detect accessory\n");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900916 }
917
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900918 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
919 &attached);
920 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
921 ret = max77693_muic_chg_handler(info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900922 if (ret < 0)
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900923 dev_err(info->dev, "Cannot detect charger accessory\n");
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900924 }
925
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900926 mutex_unlock(&info->mutex);
Chanwoo Choi154f757f2012-11-27 09:40:32 +0900927
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900928 return ret;
929}
930
Chanwoo Choi297620f2012-12-26 13:10:11 +0900931static void max77693_muic_detect_cable_wq(struct work_struct *work)
932{
933 struct max77693_muic_info *info = container_of(to_delayed_work(work),
934 struct max77693_muic_info, wq_detcable);
935
936 max77693_muic_detect_accessory(info);
937}
938
Bill Pemberton44f34fd2012-11-19 13:23:21 -0500939static int max77693_muic_probe(struct platform_device *pdev)
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900940{
941 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
Chanwoo Choif8457d572012-10-08 14:41:49 +0900942 struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
943 struct max77693_muic_platform_data *muic_pdata = pdata->muic_data;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900944 struct max77693_muic_info *info;
Chanwoo Choi297620f2012-12-26 13:10:11 +0900945 int delay_jiffies;
946 int ret;
947 int i;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900948 u8 id;
949
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +0900950 info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
951 GFP_KERNEL);
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900952 if (!info) {
953 dev_err(&pdev->dev, "failed to allocate memory\n");
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +0900954 return -ENOMEM;
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900955 }
956 info->dev = &pdev->dev;
957 info->max77693 = max77693;
Sachin Kamat1967fa02012-11-21 10:04:58 +0530958 if (info->max77693->regmap_muic) {
Chanwoo Choib186b122012-08-21 15:16:23 +0900959 dev_dbg(&pdev->dev, "allocate register map\n");
Sachin Kamat1967fa02012-11-21 10:04:58 +0530960 } else {
Chanwoo Choib186b122012-08-21 15:16:23 +0900961 info->max77693->regmap_muic = devm_regmap_init_i2c(
962 info->max77693->muic,
963 &max77693_muic_regmap_config);
964 if (IS_ERR(info->max77693->regmap_muic)) {
965 ret = PTR_ERR(info->max77693->regmap_muic);
966 dev_err(max77693->dev,
967 "failed to allocate register map: %d\n", ret);
Sachin Kamat3bf742f2012-11-21 10:04:57 +0530968 return ret;
Chanwoo Choib186b122012-08-21 15:16:23 +0900969 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900970 }
Chanwoo Choi39bf3692012-12-03 13:09:41 +0900971
972 /* Register input device for button of dock device */
973 info->dock = input_allocate_device();
974 if (!info->dock) {
975 dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
976 return -ENOMEM;
977 }
978 info->dock->name = "max77693-muic/dock";
979 info->dock->phys = "max77693-muic/extcon";
980 info->dock->dev.parent = &pdev->dev;
981
982 __set_bit(EV_REP, info->dock->evbit);
983
984 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
985 input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
986 input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
987 input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
988 input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
989
990 ret = input_register_device(info->dock);
991 if (ret < 0) {
992 dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
993 ret);
994 return ret;
995 }
996
Chanwoo Choidb1b9032012-07-17 13:28:28 +0900997 platform_set_drvdata(pdev, info);
998 mutex_init(&info->mutex);
999
1000 INIT_WORK(&info->irq_work, max77693_muic_irq_work);
1001
1002 /* Support irq domain for MAX77693 MUIC device */
1003 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1004 struct max77693_muic_irq *muic_irq = &muic_irqs[i];
Sachin Kamat00af4b12012-11-20 15:46:44 +09001005 unsigned int virq = 0;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001006
1007 virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq);
Sachin Kamat00af4b12012-11-20 15:46:44 +09001008 if (!virq) {
1009 ret = -EINVAL;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001010 goto err_irq;
Sachin Kamat00af4b12012-11-20 15:46:44 +09001011 }
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001012 muic_irq->virq = virq;
1013
1014 ret = request_threaded_irq(virq, NULL,
1015 max77693_muic_irq_handler,
Chanwoo Choiae3b3212012-11-28 12:39:01 +09001016 IRQF_NO_SUSPEND,
1017 muic_irq->name, info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001018 if (ret) {
1019 dev_err(&pdev->dev,
1020 "failed: irq request (IRQ: %d,"
1021 " error :%d)\n",
1022 muic_irq->irq, ret);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001023 goto err_irq;
1024 }
1025 }
1026
1027 /* Initialize extcon device */
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001028 info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
1029 GFP_KERNEL);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001030 if (!info->edev) {
1031 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1032 ret = -ENOMEM;
1033 goto err_irq;
1034 }
1035 info->edev->name = DEV_NAME;
1036 info->edev->supported_cable = max77693_extcon_cable;
1037 ret = extcon_dev_register(info->edev, NULL);
1038 if (ret) {
1039 dev_err(&pdev->dev, "failed to register extcon device\n");
Sachin Kamatf4bb5cb2012-11-20 15:46:50 +09001040 goto err_irq;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001041 }
1042
Chanwoo Choif8457d572012-10-08 14:41:49 +09001043 /* Initialize MUIC register by using platform data */
1044 for (i = 0 ; i < muic_pdata->num_init_data ; i++) {
1045 enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR;
1046
1047 max77693_write_reg(info->max77693->regmap_muic,
1048 muic_pdata->init_data[i].addr,
1049 muic_pdata->init_data[i].data);
1050
1051 switch (muic_pdata->init_data[i].addr) {
1052 case MAX77693_MUIC_REG_INTMASK1:
1053 irq_src = MUIC_INT1;
1054 break;
1055 case MAX77693_MUIC_REG_INTMASK2:
1056 irq_src = MUIC_INT2;
1057 break;
1058 case MAX77693_MUIC_REG_INTMASK3:
1059 irq_src = MUIC_INT3;
1060 break;
1061 }
1062
1063 if (irq_src < MAX77693_IRQ_GROUP_NR)
1064 info->max77693->irq_masks_cur[irq_src]
1065 = muic_pdata->init_data[i].data;
1066 }
1067
Chanwoo Choi2b757992012-12-06 21:27:56 +09001068 /*
1069 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1070 * h/w path of COMP2/COMN1 on CONTROL1 register.
1071 */
1072 if (muic_pdata->path_uart)
1073 info->path_uart = muic_pdata->path_uart;
1074 else
1075 info->path_uart = CONTROL1_SW_UART;
1076
1077 if (muic_pdata->path_usb)
1078 info->path_usb = muic_pdata->path_usb;
1079 else
1080 info->path_usb = CONTROL1_SW_USB;
1081
1082 /* Set initial path for UART */
1083 max77693_muic_set_path(info, info->path_uart, true);
1084
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001085 /* Check revision number of MUIC device*/
1086 ret = max77693_read_reg(info->max77693->regmap_muic,
1087 MAX77693_MUIC_REG_ID, &id);
1088 if (ret < 0) {
1089 dev_err(&pdev->dev, "failed to read revision number\n");
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001090 goto err_extcon;
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001091 }
1092 dev_info(info->dev, "device ID : 0x%x\n", id);
1093
1094 /* Set ADC debounce time */
1095 max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1096
Chanwoo Choi297620f2012-12-26 13:10:11 +09001097 /*
1098 * Detect accessory after completing the initialization of platform
1099 *
1100 * - Use delayed workqueue to detect cable state and then
1101 * notify cable state to notifiee/platform through uevent.
1102 * After completing the booting of platform, the extcon provider
1103 * driver should notify cable state to upper layer.
1104 */
1105 INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1106 if (muic_pdata->detcable_delay_ms)
1107 delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1108 else
1109 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1110 schedule_delayed_work(&info->wq_detcable, delay_jiffies);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001111
1112 return ret;
1113
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001114err_extcon:
1115 extcon_dev_unregister(info->edev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001116err_irq:
Sachin Kamat00af4b12012-11-20 15:46:44 +09001117 while (--i >= 0)
1118 free_irq(muic_irqs[i].virq, info);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001119 return ret;
1120}
1121
Bill Pemberton93ed0322012-11-19 13:25:49 -05001122static int max77693_muic_remove(struct platform_device *pdev)
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001123{
1124 struct max77693_muic_info *info = platform_get_drvdata(pdev);
1125 int i;
1126
1127 for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
1128 free_irq(muic_irqs[i].virq, info);
1129 cancel_work_sync(&info->irq_work);
Chanwoo Choi39bf3692012-12-03 13:09:41 +09001130 input_unregister_device(info->dock);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001131 extcon_dev_unregister(info->edev);
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001132
1133 return 0;
1134}
1135
1136static struct platform_driver max77693_muic_driver = {
1137 .driver = {
1138 .name = DEV_NAME,
1139 .owner = THIS_MODULE,
1140 },
1141 .probe = max77693_muic_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -05001142 .remove = max77693_muic_remove,
Chanwoo Choidb1b9032012-07-17 13:28:28 +09001143};
1144
1145module_platform_driver(max77693_muic_driver);
1146
1147MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1148MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1149MODULE_LICENSE("GPL");
1150MODULE_ALIAS("platform:extcon-max77693");