blob: 4dfe0a6337d844c0d643aba18d7e14d30484e25a [file] [log] [blame]
Jaewon Kim27a28d32015-02-04 13:56:07 +09001/*
2 * extcon-max77843.c - Maxim MAX77843 extcon driver to support
3 * MUIC(Micro USB Interface Controller)
4 *
5 * Copyright (C) 2015 Samsung Electronics
6 * Author: Jaewon Kim <jaewon02.kim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/extcon.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17#include <linux/kernel.h>
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +090018#include <linux/mfd/max77693-common.h>
Jaewon Kim27a28d32015-02-04 13:56:07 +090019#include <linux/mfd/max77843-private.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/workqueue.h>
23
24#define DELAY_MS_DEFAULT 15000 /* unit: millisecond */
25
26enum max77843_muic_status {
27 MAX77843_MUIC_STATUS1 = 0,
28 MAX77843_MUIC_STATUS2,
29 MAX77843_MUIC_STATUS3,
30
31 MAX77843_MUIC_STATUS_NUM,
32};
33
34struct max77843_muic_info {
35 struct device *dev;
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +090036 struct max77693_dev *max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +090037 struct extcon_dev *edev;
38
39 struct mutex mutex;
40 struct work_struct irq_work;
41 struct delayed_work wq_detcable;
42
43 u8 status[MAX77843_MUIC_STATUS_NUM];
44 int prev_cable_type;
45 int prev_chg_type;
46 int prev_gnd_type;
47
48 bool irq_adc;
49 bool irq_chg;
50};
51
52enum max77843_muic_cable_group {
53 MAX77843_CABLE_GROUP_ADC = 0,
54 MAX77843_CABLE_GROUP_ADC_GND,
55 MAX77843_CABLE_GROUP_CHG,
56};
57
58enum max77843_muic_adc_debounce_time {
59 MAX77843_DEBOUNCE_TIME_5MS = 0,
60 MAX77843_DEBOUNCE_TIME_10MS,
61 MAX77843_DEBOUNCE_TIME_25MS,
62 MAX77843_DEBOUNCE_TIME_38_62MS,
63};
64
65/* Define accessory cable type */
66enum max77843_muic_accessory_type {
67 MAX77843_MUIC_ADC_GROUND = 0,
68 MAX77843_MUIC_ADC_SEND_END_BUTTON,
69 MAX77843_MUIC_ADC_REMOTE_S1_BUTTON,
70 MAX77843_MUIC_ADC_REMOTE_S2_BUTTON,
71 MAX77843_MUIC_ADC_REMOTE_S3_BUTTON,
72 MAX77843_MUIC_ADC_REMOTE_S4_BUTTON,
73 MAX77843_MUIC_ADC_REMOTE_S5_BUTTON,
74 MAX77843_MUIC_ADC_REMOTE_S6_BUTTON,
75 MAX77843_MUIC_ADC_REMOTE_S7_BUTTON,
76 MAX77843_MUIC_ADC_REMOTE_S8_BUTTON,
77 MAX77843_MUIC_ADC_REMOTE_S9_BUTTON,
78 MAX77843_MUIC_ADC_REMOTE_S10_BUTTON,
79 MAX77843_MUIC_ADC_REMOTE_S11_BUTTON,
80 MAX77843_MUIC_ADC_REMOTE_S12_BUTTON,
81 MAX77843_MUIC_ADC_RESERVED_ACC_1,
82 MAX77843_MUIC_ADC_RESERVED_ACC_2,
83 MAX77843_MUIC_ADC_RESERVED_ACC_3,
84 MAX77843_MUIC_ADC_RESERVED_ACC_4,
85 MAX77843_MUIC_ADC_RESERVED_ACC_5,
86 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2,
87 MAX77843_MUIC_ADC_PHONE_POWERED_DEV,
88 MAX77843_MUIC_ADC_TTY_CONVERTER,
89 MAX77843_MUIC_ADC_UART_CABLE,
90 MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG,
91 MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF,
92 MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON,
93 MAX77843_MUIC_ADC_AV_CABLE_NOLOAD,
94 MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG,
95 MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF,
96 MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON,
97 MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1,
98 MAX77843_MUIC_ADC_OPEN,
99
100 /* The blow accessories should check
101 not only ADC value but also ADC1K and VBVolt value. */
102 /* Offset|ADC1K|VBVolt| */
103 MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */
104 MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */
105 MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */
106 MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */
107};
108
109/* Define charger cable type */
110enum max77843_muic_charger_type {
111 MAX77843_MUIC_CHG_NONE = 0,
112 MAX77843_MUIC_CHG_USB,
113 MAX77843_MUIC_CHG_DOWNSTREAM,
114 MAX77843_MUIC_CHG_DEDICATED,
115 MAX77843_MUIC_CHG_SPECIAL_500MA,
116 MAX77843_MUIC_CHG_SPECIAL_1A,
117 MAX77843_MUIC_CHG_SPECIAL_BIAS,
118 MAX77843_MUIC_CHG_RESERVED,
119 MAX77843_MUIC_CHG_GND,
120};
121
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900122static const unsigned int max77843_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900123 EXTCON_USB,
124 EXTCON_USB_HOST,
125 EXTCON_TA,
126 EXTCON_CHARGE_DOWNSTREAM,
127 EXTCON_FAST_CHARGER,
128 EXTCON_SLOW_CHARGER,
129 EXTCON_MHL,
130 EXTCON_JIG,
131 EXTCON_NONE,
Jaewon Kim27a28d32015-02-04 13:56:07 +0900132};
133
134struct max77843_muic_irq {
135 unsigned int irq;
136 const char *name;
137 unsigned int virq;
138};
139
140static struct max77843_muic_irq max77843_muic_irqs[] = {
141 { MAX77843_MUIC_IRQ_INT1_ADC, "MUIC-ADC" },
142 { MAX77843_MUIC_IRQ_INT1_ADCERROR, "MUIC-ADC_ERROR" },
143 { MAX77843_MUIC_IRQ_INT1_ADC1K, "MUIC-ADC1K" },
144 { MAX77843_MUIC_IRQ_INT2_CHGTYP, "MUIC-CHGTYP" },
145 { MAX77843_MUIC_IRQ_INT2_CHGDETRUN, "MUIC-CHGDETRUN" },
146 { MAX77843_MUIC_IRQ_INT2_DCDTMR, "MUIC-DCDTMR" },
147 { MAX77843_MUIC_IRQ_INT2_DXOVP, "MUIC-DXOVP" },
148 { MAX77843_MUIC_IRQ_INT2_VBVOLT, "MUIC-VBVOLT" },
149 { MAX77843_MUIC_IRQ_INT3_VBADC, "MUIC-VBADC" },
150 { MAX77843_MUIC_IRQ_INT3_VDNMON, "MUIC-VDNMON" },
151 { MAX77843_MUIC_IRQ_INT3_DNRES, "MUIC-DNRES" },
152 { MAX77843_MUIC_IRQ_INT3_MPNACK, "MUIC-MPNACK"},
153 { MAX77843_MUIC_IRQ_INT3_MRXBUFOW, "MUIC-MRXBUFOW"},
154 { MAX77843_MUIC_IRQ_INT3_MRXTRF, "MUIC-MRXTRF"},
155 { MAX77843_MUIC_IRQ_INT3_MRXPERR, "MUIC-MRXPERR"},
156 { MAX77843_MUIC_IRQ_INT3_MRXRDY, "MUIC-MRXRDY"},
157};
158
159static const struct regmap_config max77843_muic_regmap_config = {
160 .reg_bits = 8,
161 .val_bits = 8,
162 .max_register = MAX77843_MUIC_REG_END,
163};
164
165static const struct regmap_irq max77843_muic_irq[] = {
166 /* INT1 interrupt */
167 { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, },
168 { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, },
169 { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, },
170
171 /* INT2 interrupt */
172 { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, },
173 { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, },
174 { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, },
175 { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, },
176 { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, },
177
178 /* INT3 interrupt */
179 { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, },
180 { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, },
181 { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, },
182 { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, },
183 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, },
184 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, },
185 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, },
186 { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, },
187};
188
189static const struct regmap_irq_chip max77843_muic_irq_chip = {
190 .name = "max77843-muic",
191 .status_base = MAX77843_MUIC_REG_INT1,
192 .mask_base = MAX77843_MUIC_REG_INTMASK1,
193 .mask_invert = true,
194 .num_regs = 3,
195 .irqs = max77843_muic_irq,
196 .num_irqs = ARRAY_SIZE(max77843_muic_irq),
197};
198
199static int max77843_muic_set_path(struct max77843_muic_info *info,
200 u8 val, bool attached)
201{
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900202 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900203 int ret = 0;
204 unsigned int ctrl1, ctrl2;
205
206 if (attached)
207 ctrl1 = val;
208 else
209 ctrl1 = CONTROL1_SW_OPEN;
210
211 ret = regmap_update_bits(max77843->regmap_muic,
212 MAX77843_MUIC_REG_CONTROL1,
213 CONTROL1_COM_SW, ctrl1);
214 if (ret < 0) {
215 dev_err(info->dev, "Cannot switch MUIC port\n");
216 return ret;
217 }
218
219 if (attached)
220 ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK;
221 else
222 ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK;
223
224 ret = regmap_update_bits(max77843->regmap_muic,
225 MAX77843_MUIC_REG_CONTROL2,
226 MAX77843_MUIC_CONTROL2_LOWPWR_MASK |
227 MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2);
228 if (ret < 0) {
229 dev_err(info->dev, "Cannot update lowpower mode\n");
230 return ret;
231 }
232
233 dev_dbg(info->dev,
234 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
235 ctrl1, ctrl2, attached ? "attached" : "detached");
236
237 return 0;
238}
239
240static int max77843_muic_get_cable_type(struct max77843_muic_info *info,
241 enum max77843_muic_cable_group group, bool *attached)
242{
243 int adc, chg_type, cable_type, gnd_type;
244
245 adc = info->status[MAX77843_MUIC_STATUS1] &
246 MAX77843_MUIC_STATUS1_ADC_MASK;
247 adc >>= STATUS1_ADC_SHIFT;
248
249 switch (group) {
250 case MAX77843_CABLE_GROUP_ADC:
251 if (adc == MAX77843_MUIC_ADC_OPEN) {
252 *attached = false;
253 cable_type = info->prev_cable_type;
254 info->prev_cable_type = MAX77843_MUIC_ADC_OPEN;
255 } else {
256 *attached = true;
257 cable_type = info->prev_cable_type = adc;
258 }
259 break;
260 case MAX77843_CABLE_GROUP_CHG:
261 chg_type = info->status[MAX77843_MUIC_STATUS2] &
262 MAX77843_MUIC_STATUS2_CHGTYP_MASK;
263
264 /* Check GROUND accessory with charger cable */
265 if (adc == MAX77843_MUIC_ADC_GROUND) {
266 if (chg_type == MAX77843_MUIC_CHG_NONE) {
267 /* The following state when charger cable is
268 * disconnected but the GROUND accessory still
269 * connected */
270 *attached = false;
271 cable_type = info->prev_chg_type;
272 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
273 } else {
274
275 /* The following state when charger cable is
276 * connected on the GROUND accessory */
277 *attached = true;
278 cable_type = MAX77843_MUIC_CHG_GND;
279 info->prev_chg_type = MAX77843_MUIC_CHG_GND;
280 }
281 break;
282 }
283
284 if (chg_type == MAX77843_MUIC_CHG_NONE) {
285 *attached = false;
286 cable_type = info->prev_chg_type;
287 info->prev_chg_type = MAX77843_MUIC_CHG_NONE;
288 } else {
289 *attached = true;
290 cable_type = info->prev_chg_type = chg_type;
291 }
292 break;
293 case MAX77843_CABLE_GROUP_ADC_GND:
294 if (adc == MAX77843_MUIC_ADC_OPEN) {
295 *attached = false;
296 cable_type = info->prev_gnd_type;
297 info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN;
298 } else {
299 *attached = true;
300
301 /* Offset|ADC1K|VBVolt|
302 * 0x1| 0| 0| USB-HOST
303 * 0x1| 0| 1| USB-HOST with VB
304 * 0x1| 1| 0| MHL
305 * 0x1| 1| 1| MHL with VB */
306 /* Get ADC1K register bit */
307 gnd_type = (info->status[MAX77843_MUIC_STATUS1] &
308 MAX77843_MUIC_STATUS1_ADC1K_MASK);
309
310 /* Get VBVolt register bit */
311 gnd_type |= (info->status[MAX77843_MUIC_STATUS2] &
312 MAX77843_MUIC_STATUS2_VBVOLT_MASK);
313 gnd_type >>= STATUS2_VBVOLT_SHIFT;
314
315 /* Offset of GND cable */
316 gnd_type |= MAX77843_MUIC_GND_USB_HOST;
317 cable_type = info->prev_gnd_type = gnd_type;
318 }
319 break;
320 default:
321 dev_err(info->dev, "Unknown cable group (%d)\n", group);
322 cable_type = -EINVAL;
323 break;
324 }
325
326 return cable_type;
327}
328
329static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info)
330{
331 int ret, gnd_cable_type;
332 bool attached;
333
334 gnd_cable_type = max77843_muic_get_cable_type(info,
335 MAX77843_CABLE_GROUP_ADC_GND, &attached);
336 dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n",
337 attached ? "attached" : "detached", gnd_cable_type);
338
339 switch (gnd_cable_type) {
340 case MAX77843_MUIC_GND_USB_HOST:
341 case MAX77843_MUIC_GND_USB_HOST_VB:
342 ret = max77843_muic_set_path(info, CONTROL1_SW_USB, attached);
343 if (ret < 0)
344 return ret;
345
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900346 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900347 break;
348 case MAX77843_MUIC_GND_MHL_VB:
349 case MAX77843_MUIC_GND_MHL:
350 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
351 if (ret < 0)
352 return ret;
353
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900354 extcon_set_cable_state_(info->edev, EXTCON_MHL, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900355 break;
356 default:
357 dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
358 attached ? "attached" : "detached", gnd_cable_type);
359 return -EINVAL;
360 }
361
362 return 0;
363}
364
365static int max77843_muic_jig_handler(struct max77843_muic_info *info,
366 int cable_type, bool attached)
367{
368 int ret;
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900369 u8 path = CONTROL1_SW_OPEN;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900370
371 dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
372 attached ? "attached" : "detached", cable_type);
373
374 switch (cable_type) {
375 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
Jaewon Kim27a28d32015-02-04 13:56:07 +0900376 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900377 path = CONTROL1_SW_USB;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900378 break;
379 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900380 path = CONTROL1_SW_UART;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900381 break;
382 default:
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900383 return -EINVAL;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900384 }
385
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900386 ret = max77843_muic_set_path(info, path, attached);
387 if (ret < 0)
388 return ret;
389
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900390 extcon_set_cable_state_(info->edev, EXTCON_JIG, attached);
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900391
Jaewon Kim27a28d32015-02-04 13:56:07 +0900392 return 0;
393}
394
395static int max77843_muic_adc_handler(struct max77843_muic_info *info)
396{
397 int ret, cable_type;
398 bool attached;
399
400 cable_type = max77843_muic_get_cable_type(info,
401 MAX77843_CABLE_GROUP_ADC, &attached);
402
403 dev_dbg(info->dev,
404 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
405 attached ? "attached" : "detached", cable_type,
406 info->prev_cable_type);
407
408 switch (cable_type) {
409 case MAX77843_MUIC_ADC_GROUND:
410 ret = max77843_muic_adc_gnd_handler(info);
411 if (ret < 0)
412 return ret;
413 break;
414 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
415 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
416 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
417 ret = max77843_muic_jig_handler(info, cable_type, attached);
418 if (ret < 0)
419 return ret;
420 break;
421 case MAX77843_MUIC_ADC_SEND_END_BUTTON:
422 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
423 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
424 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
425 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
426 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
427 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
428 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
429 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
430 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
431 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
432 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
433 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
434 case MAX77843_MUIC_ADC_RESERVED_ACC_1:
435 case MAX77843_MUIC_ADC_RESERVED_ACC_2:
436 case MAX77843_MUIC_ADC_RESERVED_ACC_3:
437 case MAX77843_MUIC_ADC_RESERVED_ACC_4:
438 case MAX77843_MUIC_ADC_RESERVED_ACC_5:
439 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
440 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
441 case MAX77843_MUIC_ADC_TTY_CONVERTER:
442 case MAX77843_MUIC_ADC_UART_CABLE:
443 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
444 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
445 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
446 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
447 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
448 case MAX77843_MUIC_ADC_OPEN:
449 dev_err(info->dev,
450 "accessory is %s but it isn't used (adc:0x%x)\n",
451 attached ? "attached" : "detached", cable_type);
452 return -EAGAIN;
453 default:
454 dev_err(info->dev,
455 "failed to detect %s accessory (adc:0x%x)\n",
456 attached ? "attached" : "detached", cable_type);
457 return -EINVAL;
458 }
459
460 return 0;
461}
462
463static int max77843_muic_chg_handler(struct max77843_muic_info *info)
464{
465 int ret, chg_type, gnd_type;
466 bool attached;
467
468 chg_type = max77843_muic_get_cable_type(info,
469 MAX77843_CABLE_GROUP_CHG, &attached);
470
471 dev_dbg(info->dev,
472 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
473 attached ? "attached" : "detached",
474 chg_type, info->prev_chg_type);
475
476 switch (chg_type) {
477 case MAX77843_MUIC_CHG_USB:
478 ret = max77843_muic_set_path(info, CONTROL1_SW_USB, attached);
479 if (ret < 0)
480 return ret;
481
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900482 extcon_set_cable_state_(info->edev, EXTCON_USB, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900483 break;
484 case MAX77843_MUIC_CHG_DOWNSTREAM:
485 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
486 if (ret < 0)
487 return ret;
488
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900489 extcon_set_cable_state_(info->edev, EXTCON_CHARGE_DOWNSTREAM,
490 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900491 break;
492 case MAX77843_MUIC_CHG_DEDICATED:
493 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
494 if (ret < 0)
495 return ret;
496
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900497 extcon_set_cable_state_(info->edev, EXTCON_TA, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900498 break;
499 case MAX77843_MUIC_CHG_SPECIAL_500MA:
500 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
501 if (ret < 0)
502 return ret;
503
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900504 extcon_set_cable_state_(info->edev, EXTCON_SLOW_CHARGER,
505 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900506 break;
507 case MAX77843_MUIC_CHG_SPECIAL_1A:
508 ret = max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
509 if (ret < 0)
510 return ret;
511
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900512 extcon_set_cable_state_(info->edev, EXTCON_FAST_CHARGER,
513 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900514 break;
515 case MAX77843_MUIC_CHG_GND:
516 gnd_type = max77843_muic_get_cable_type(info,
517 MAX77843_CABLE_GROUP_ADC_GND, &attached);
518
519 /* Charger cable on MHL accessory is attach or detach */
520 if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900521 extcon_set_cable_state_(info->edev, EXTCON_TA, true);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900522 else if (gnd_type == MAX77843_MUIC_GND_MHL)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900523 extcon_set_cable_state_(info->edev, EXTCON_TA, false);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900524 break;
525 case MAX77843_MUIC_CHG_NONE:
526 break;
527 default:
528 dev_err(info->dev,
529 "failed to detect %s accessory (chg_type:0x%x)\n",
530 attached ? "attached" : "detached", chg_type);
531
532 max77843_muic_set_path(info, CONTROL1_SW_OPEN, attached);
533 return -EINVAL;
534 }
535
536 return 0;
537}
538
539static void max77843_muic_irq_work(struct work_struct *work)
540{
541 struct max77843_muic_info *info = container_of(work,
542 struct max77843_muic_info, irq_work);
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900543 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900544 int ret = 0;
545
546 mutex_lock(&info->mutex);
547
548 ret = regmap_bulk_read(max77843->regmap_muic,
549 MAX77843_MUIC_REG_STATUS1, info->status,
550 MAX77843_MUIC_STATUS_NUM);
551 if (ret) {
552 dev_err(info->dev, "Cannot read STATUS registers\n");
553 mutex_unlock(&info->mutex);
554 return;
555 }
556
557 if (info->irq_adc) {
558 ret = max77843_muic_adc_handler(info);
559 if (ret)
560 dev_err(info->dev, "Unknown cable type\n");
561 info->irq_adc = false;
562 }
563
564 if (info->irq_chg) {
565 ret = max77843_muic_chg_handler(info);
566 if (ret)
567 dev_err(info->dev, "Unknown charger type\n");
568 info->irq_chg = false;
569 }
570
571 mutex_unlock(&info->mutex);
572}
573
574static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
575{
576 struct max77843_muic_info *info = data;
577 int i, irq_type = -1;
578
579 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
580 if (irq == max77843_muic_irqs[i].virq)
581 irq_type = max77843_muic_irqs[i].irq;
582
583 switch (irq_type) {
584 case MAX77843_MUIC_IRQ_INT1_ADC:
585 case MAX77843_MUIC_IRQ_INT1_ADCERROR:
586 case MAX77843_MUIC_IRQ_INT1_ADC1K:
587 info->irq_adc = true;
588 break;
589 case MAX77843_MUIC_IRQ_INT2_CHGTYP:
590 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
591 case MAX77843_MUIC_IRQ_INT2_DCDTMR:
592 case MAX77843_MUIC_IRQ_INT2_DXOVP:
593 case MAX77843_MUIC_IRQ_INT2_VBVOLT:
594 info->irq_chg = true;
595 break;
596 case MAX77843_MUIC_IRQ_INT3_VBADC:
597 case MAX77843_MUIC_IRQ_INT3_VDNMON:
598 case MAX77843_MUIC_IRQ_INT3_DNRES:
599 case MAX77843_MUIC_IRQ_INT3_MPNACK:
600 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
601 case MAX77843_MUIC_IRQ_INT3_MRXTRF:
602 case MAX77843_MUIC_IRQ_INT3_MRXPERR:
603 case MAX77843_MUIC_IRQ_INT3_MRXRDY:
604 break;
605 default:
606 dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
607 break;
608 }
609
610 schedule_work(&info->irq_work);
611
612 return IRQ_HANDLED;
613}
614
615static void max77843_muic_detect_cable_wq(struct work_struct *work)
616{
617 struct max77843_muic_info *info = container_of(to_delayed_work(work),
618 struct max77843_muic_info, wq_detcable);
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900619 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900620 int chg_type, adc, ret;
621 bool attached;
622
623 mutex_lock(&info->mutex);
624
625 ret = regmap_bulk_read(max77843->regmap_muic,
626 MAX77843_MUIC_REG_STATUS1, info->status,
627 MAX77843_MUIC_STATUS_NUM);
628 if (ret) {
629 dev_err(info->dev, "Cannot read STATUS registers\n");
630 goto err_cable_wq;
631 }
632
633 adc = max77843_muic_get_cable_type(info,
634 MAX77843_CABLE_GROUP_ADC, &attached);
635 if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
636 ret = max77843_muic_adc_handler(info);
637 if (ret < 0) {
638 dev_err(info->dev, "Cannot detect accessory\n");
639 goto err_cable_wq;
640 }
641 }
642
643 chg_type = max77843_muic_get_cable_type(info,
644 MAX77843_CABLE_GROUP_CHG, &attached);
645 if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
646 ret = max77843_muic_chg_handler(info);
647 if (ret < 0) {
648 dev_err(info->dev, "Cannot detect charger accessory\n");
649 goto err_cable_wq;
650 }
651 }
652
653err_cable_wq:
654 mutex_unlock(&info->mutex);
655}
656
657static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
658 enum max77843_muic_adc_debounce_time time)
659{
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900660 struct max77693_dev *max77843 = info->max77843;
Dan Carpenterd927c592015-03-15 13:49:47 +0300661 int ret;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900662
663 switch (time) {
664 case MAX77843_DEBOUNCE_TIME_5MS:
665 case MAX77843_DEBOUNCE_TIME_10MS:
666 case MAX77843_DEBOUNCE_TIME_25MS:
667 case MAX77843_DEBOUNCE_TIME_38_62MS:
668 ret = regmap_update_bits(max77843->regmap_muic,
669 MAX77843_MUIC_REG_CONTROL4,
670 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
671 time << CONTROL4_ADCDBSET_SHIFT);
672 if (ret < 0) {
673 dev_err(info->dev, "Cannot write MUIC regmap\n");
674 return ret;
675 }
676 break;
677 default:
678 dev_err(info->dev, "Invalid ADC debounce time\n");
679 return -EINVAL;
680 }
681
682 return 0;
683}
684
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900685static int max77843_init_muic_regmap(struct max77693_dev *max77843)
Jaewon Kim27a28d32015-02-04 13:56:07 +0900686{
687 int ret;
688
689 max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter,
690 I2C_ADDR_MUIC);
691 if (!max77843->i2c_muic) {
692 dev_err(&max77843->i2c->dev,
693 "Cannot allocate I2C device for MUIC\n");
Dan Carpenterb9b518f2015-03-15 13:56:04 +0300694 return -ENOMEM;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900695 }
696
697 i2c_set_clientdata(max77843->i2c_muic, max77843);
698
699 max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
700 &max77843_muic_regmap_config);
701 if (IS_ERR(max77843->regmap_muic)) {
702 ret = PTR_ERR(max77843->regmap_muic);
703 goto err_muic_i2c;
704 }
705
706 ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
707 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
708 0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
709 if (ret < 0) {
710 dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
711 goto err_muic_i2c;
712 }
713
714 return 0;
715
716err_muic_i2c:
717 i2c_unregister_device(max77843->i2c_muic);
718
719 return ret;
720}
721
722static int max77843_muic_probe(struct platform_device *pdev)
723{
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900724 struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900725 struct max77843_muic_info *info;
726 unsigned int id;
727 int i, ret;
728
729 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
730 if (!info)
731 return -ENOMEM;
732
733 info->dev = &pdev->dev;
734 info->max77843 = max77843;
735
736 platform_set_drvdata(pdev, info);
737 mutex_init(&info->mutex);
738
739 /* Initialize i2c and regmap */
740 ret = max77843_init_muic_regmap(max77843);
741 if (ret) {
742 dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
743 return ret;
744 }
745
746 /* Turn off auto detection configuration */
747 ret = regmap_update_bits(max77843->regmap_muic,
748 MAX77843_MUIC_REG_CONTROL4,
749 MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
750 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
751 CONTROL4_AUTO_DISABLE);
752
753 /* Initialize extcon device */
754 info->edev = devm_extcon_dev_allocate(&pdev->dev,
755 max77843_extcon_cable);
756 if (IS_ERR(info->edev)) {
757 dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
758 ret = -ENODEV;
759 goto err_muic_irq;
760 }
761
762 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
763 if (ret) {
764 dev_err(&pdev->dev, "Failed to register extcon device\n");
765 goto err_muic_irq;
766 }
767
768 /* Set ADC debounce time */
769 max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
770
771 /* Set initial path for UART */
772 max77843_muic_set_path(info, CONTROL1_SW_UART, true);
773
774 /* Check revision number of MUIC device */
775 ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
776 if (ret < 0) {
777 dev_err(&pdev->dev, "Failed to read revision number\n");
778 goto err_muic_irq;
779 }
780 dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
781
782 /* Support virtual irq domain for max77843 MUIC device */
783 INIT_WORK(&info->irq_work, max77843_muic_irq_work);
784
785 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
786 struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
787 unsigned int virq = 0;
788
789 virq = regmap_irq_get_virq(max77843->irq_data_muic,
790 muic_irq->irq);
791 if (virq <= 0) {
792 ret = -EINVAL;
793 goto err_muic_irq;
794 }
795 muic_irq->virq = virq;
796
797 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
798 max77843_muic_irq_handler, IRQF_NO_SUSPEND,
799 muic_irq->name, info);
800 if (ret) {
801 dev_err(&pdev->dev,
802 "Failed to request irq (IRQ: %d, error: %d)\n",
803 muic_irq->irq, ret);
804 goto err_muic_irq;
805 }
806 }
807
808 /* Detect accessory after completing the initialization of platform */
809 INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
810 queue_delayed_work(system_power_efficient_wq,
811 &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
812
813 return 0;
814
815err_muic_irq:
816 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
817 i2c_unregister_device(max77843->i2c_muic);
818
819 return ret;
820}
821
822static int max77843_muic_remove(struct platform_device *pdev)
823{
824 struct max77843_muic_info *info = platform_get_drvdata(pdev);
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900825 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900826
827 cancel_work_sync(&info->irq_work);
828 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
829 i2c_unregister_device(max77843->i2c_muic);
830
831 return 0;
832}
833
834static const struct platform_device_id max77843_muic_id[] = {
835 { "max77843-muic", },
836 { /* sentinel */ },
837};
838MODULE_DEVICE_TABLE(platform, max77843_muic_id);
839
840static struct platform_driver max77843_muic_driver = {
841 .driver = {
842 .name = "max77843-muic",
843 },
844 .probe = max77843_muic_probe,
845 .remove = max77843_muic_remove,
846 .id_table = max77843_muic_id,
847};
848
849static int __init max77843_muic_init(void)
850{
851 return platform_driver_register(&max77843_muic_driver);
852}
853subsys_initcall(max77843_muic_init);
854
855MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
856MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
857MODULE_LICENSE("GPL");