blob: fdd928542c19dffe06d2ba762b64b4aaf85d7442 [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
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900209 ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900210
211 ret = regmap_update_bits(max77843->regmap_muic,
212 MAX77843_MUIC_REG_CONTROL1,
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900213 MAX77843_MUIC_CONTROL1_COM_SW, ctrl1);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900214 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;
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900247 adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900248
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);
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900313 gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900314
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:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900342 ret = max77843_muic_set_path(info,
343 MAX77843_MUIC_CONTROL1_SW_USB,
344 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900345 if (ret < 0)
346 return ret;
347
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900348 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900349 break;
350 case MAX77843_MUIC_GND_MHL_VB:
351 case MAX77843_MUIC_GND_MHL:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900352 ret = max77843_muic_set_path(info,
353 MAX77843_MUIC_CONTROL1_SW_OPEN,
354 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900355 if (ret < 0)
356 return ret;
357
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900358 extcon_set_cable_state_(info->edev, EXTCON_MHL, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900359 break;
360 default:
361 dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n",
362 attached ? "attached" : "detached", gnd_cable_type);
363 return -EINVAL;
364 }
365
366 return 0;
367}
368
369static int max77843_muic_jig_handler(struct max77843_muic_info *info,
370 int cable_type, bool attached)
371{
372 int ret;
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900373 u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900374
375 dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n",
376 attached ? "attached" : "detached", cable_type);
377
378 switch (cable_type) {
379 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
Jaewon Kim27a28d32015-02-04 13:56:07 +0900380 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900381 path = MAX77843_MUIC_CONTROL1_SW_USB;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900382 break;
383 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900384 path = MAX77843_MUIC_CONTROL1_SW_UART;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900385 break;
386 default:
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900387 return -EINVAL;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900388 }
389
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900390 ret = max77843_muic_set_path(info, path, attached);
391 if (ret < 0)
392 return ret;
393
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900394 extcon_set_cable_state_(info->edev, EXTCON_JIG, attached);
Chanwoo Choi41b3c012015-04-24 19:50:48 +0900395
Jaewon Kim27a28d32015-02-04 13:56:07 +0900396 return 0;
397}
398
399static int max77843_muic_adc_handler(struct max77843_muic_info *info)
400{
401 int ret, cable_type;
402 bool attached;
403
404 cable_type = max77843_muic_get_cable_type(info,
405 MAX77843_CABLE_GROUP_ADC, &attached);
406
407 dev_dbg(info->dev,
408 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
409 attached ? "attached" : "detached", cable_type,
410 info->prev_cable_type);
411
412 switch (cable_type) {
413 case MAX77843_MUIC_ADC_GROUND:
414 ret = max77843_muic_adc_gnd_handler(info);
415 if (ret < 0)
416 return ret;
417 break;
418 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF:
419 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
420 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
421 ret = max77843_muic_jig_handler(info, cable_type, attached);
422 if (ret < 0)
423 return ret;
424 break;
425 case MAX77843_MUIC_ADC_SEND_END_BUTTON:
426 case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON:
427 case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON:
428 case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON:
429 case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON:
430 case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON:
431 case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON:
432 case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON:
433 case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON:
434 case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON:
435 case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON:
436 case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON:
437 case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON:
438 case MAX77843_MUIC_ADC_RESERVED_ACC_1:
439 case MAX77843_MUIC_ADC_RESERVED_ACC_2:
440 case MAX77843_MUIC_ADC_RESERVED_ACC_3:
441 case MAX77843_MUIC_ADC_RESERVED_ACC_4:
442 case MAX77843_MUIC_ADC_RESERVED_ACC_5:
443 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2:
444 case MAX77843_MUIC_ADC_PHONE_POWERED_DEV:
445 case MAX77843_MUIC_ADC_TTY_CONVERTER:
446 case MAX77843_MUIC_ADC_UART_CABLE:
447 case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG:
448 case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD:
449 case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG:
450 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON:
451 case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1:
452 case MAX77843_MUIC_ADC_OPEN:
453 dev_err(info->dev,
454 "accessory is %s but it isn't used (adc:0x%x)\n",
455 attached ? "attached" : "detached", cable_type);
456 return -EAGAIN;
457 default:
458 dev_err(info->dev,
459 "failed to detect %s accessory (adc:0x%x)\n",
460 attached ? "attached" : "detached", cable_type);
461 return -EINVAL;
462 }
463
464 return 0;
465}
466
467static int max77843_muic_chg_handler(struct max77843_muic_info *info)
468{
469 int ret, chg_type, gnd_type;
470 bool attached;
471
472 chg_type = max77843_muic_get_cable_type(info,
473 MAX77843_CABLE_GROUP_CHG, &attached);
474
475 dev_dbg(info->dev,
476 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
477 attached ? "attached" : "detached",
478 chg_type, info->prev_chg_type);
479
480 switch (chg_type) {
481 case MAX77843_MUIC_CHG_USB:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900482 ret = max77843_muic_set_path(info,
483 MAX77843_MUIC_CONTROL1_SW_USB,
484 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900485 if (ret < 0)
486 return ret;
487
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900488 extcon_set_cable_state_(info->edev, EXTCON_USB, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900489 break;
490 case MAX77843_MUIC_CHG_DOWNSTREAM:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900491 ret = max77843_muic_set_path(info,
492 MAX77843_MUIC_CONTROL1_SW_OPEN,
493 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900494 if (ret < 0)
495 return ret;
496
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900497 extcon_set_cable_state_(info->edev, EXTCON_CHARGE_DOWNSTREAM,
498 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900499 break;
500 case MAX77843_MUIC_CHG_DEDICATED:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900501 ret = max77843_muic_set_path(info,
502 MAX77843_MUIC_CONTROL1_SW_OPEN,
503 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900504 if (ret < 0)
505 return ret;
506
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900507 extcon_set_cable_state_(info->edev, EXTCON_TA, attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900508 break;
509 case MAX77843_MUIC_CHG_SPECIAL_500MA:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900510 ret = max77843_muic_set_path(info,
511 MAX77843_MUIC_CONTROL1_SW_OPEN,
512 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900513 if (ret < 0)
514 return ret;
515
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900516 extcon_set_cable_state_(info->edev, EXTCON_SLOW_CHARGER,
517 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900518 break;
519 case MAX77843_MUIC_CHG_SPECIAL_1A:
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900520 ret = max77843_muic_set_path(info,
521 MAX77843_MUIC_CONTROL1_SW_OPEN,
522 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900523 if (ret < 0)
524 return ret;
525
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900526 extcon_set_cable_state_(info->edev, EXTCON_FAST_CHARGER,
527 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900528 break;
529 case MAX77843_MUIC_CHG_GND:
530 gnd_type = max77843_muic_get_cable_type(info,
531 MAX77843_CABLE_GROUP_ADC_GND, &attached);
532
533 /* Charger cable on MHL accessory is attach or detach */
534 if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900535 extcon_set_cable_state_(info->edev, EXTCON_TA, true);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900536 else if (gnd_type == MAX77843_MUIC_GND_MHL)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900537 extcon_set_cable_state_(info->edev, EXTCON_TA, false);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900538 break;
539 case MAX77843_MUIC_CHG_NONE:
540 break;
541 default:
542 dev_err(info->dev,
543 "failed to detect %s accessory (chg_type:0x%x)\n",
544 attached ? "attached" : "detached", chg_type);
545
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900546 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN,
547 attached);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900548 return -EINVAL;
549 }
550
551 return 0;
552}
553
554static void max77843_muic_irq_work(struct work_struct *work)
555{
556 struct max77843_muic_info *info = container_of(work,
557 struct max77843_muic_info, irq_work);
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900558 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900559 int ret = 0;
560
561 mutex_lock(&info->mutex);
562
563 ret = regmap_bulk_read(max77843->regmap_muic,
564 MAX77843_MUIC_REG_STATUS1, info->status,
565 MAX77843_MUIC_STATUS_NUM);
566 if (ret) {
567 dev_err(info->dev, "Cannot read STATUS registers\n");
568 mutex_unlock(&info->mutex);
569 return;
570 }
571
572 if (info->irq_adc) {
573 ret = max77843_muic_adc_handler(info);
574 if (ret)
575 dev_err(info->dev, "Unknown cable type\n");
576 info->irq_adc = false;
577 }
578
579 if (info->irq_chg) {
580 ret = max77843_muic_chg_handler(info);
581 if (ret)
582 dev_err(info->dev, "Unknown charger type\n");
583 info->irq_chg = false;
584 }
585
586 mutex_unlock(&info->mutex);
587}
588
589static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
590{
591 struct max77843_muic_info *info = data;
592 int i, irq_type = -1;
593
594 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
595 if (irq == max77843_muic_irqs[i].virq)
596 irq_type = max77843_muic_irqs[i].irq;
597
598 switch (irq_type) {
599 case MAX77843_MUIC_IRQ_INT1_ADC:
600 case MAX77843_MUIC_IRQ_INT1_ADCERROR:
601 case MAX77843_MUIC_IRQ_INT1_ADC1K:
602 info->irq_adc = true;
603 break;
604 case MAX77843_MUIC_IRQ_INT2_CHGTYP:
605 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
606 case MAX77843_MUIC_IRQ_INT2_DCDTMR:
607 case MAX77843_MUIC_IRQ_INT2_DXOVP:
608 case MAX77843_MUIC_IRQ_INT2_VBVOLT:
609 info->irq_chg = true;
610 break;
611 case MAX77843_MUIC_IRQ_INT3_VBADC:
612 case MAX77843_MUIC_IRQ_INT3_VDNMON:
613 case MAX77843_MUIC_IRQ_INT3_DNRES:
614 case MAX77843_MUIC_IRQ_INT3_MPNACK:
615 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
616 case MAX77843_MUIC_IRQ_INT3_MRXTRF:
617 case MAX77843_MUIC_IRQ_INT3_MRXPERR:
618 case MAX77843_MUIC_IRQ_INT3_MRXRDY:
619 break;
620 default:
621 dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
622 break;
623 }
624
625 schedule_work(&info->irq_work);
626
627 return IRQ_HANDLED;
628}
629
630static void max77843_muic_detect_cable_wq(struct work_struct *work)
631{
632 struct max77843_muic_info *info = container_of(to_delayed_work(work),
633 struct max77843_muic_info, wq_detcable);
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900634 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900635 int chg_type, adc, ret;
636 bool attached;
637
638 mutex_lock(&info->mutex);
639
640 ret = regmap_bulk_read(max77843->regmap_muic,
641 MAX77843_MUIC_REG_STATUS1, info->status,
642 MAX77843_MUIC_STATUS_NUM);
643 if (ret) {
644 dev_err(info->dev, "Cannot read STATUS registers\n");
645 goto err_cable_wq;
646 }
647
648 adc = max77843_muic_get_cable_type(info,
649 MAX77843_CABLE_GROUP_ADC, &attached);
650 if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
651 ret = max77843_muic_adc_handler(info);
652 if (ret < 0) {
653 dev_err(info->dev, "Cannot detect accessory\n");
654 goto err_cable_wq;
655 }
656 }
657
658 chg_type = max77843_muic_get_cable_type(info,
659 MAX77843_CABLE_GROUP_CHG, &attached);
660 if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
661 ret = max77843_muic_chg_handler(info);
662 if (ret < 0) {
663 dev_err(info->dev, "Cannot detect charger accessory\n");
664 goto err_cable_wq;
665 }
666 }
667
668err_cable_wq:
669 mutex_unlock(&info->mutex);
670}
671
672static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
673 enum max77843_muic_adc_debounce_time time)
674{
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900675 struct max77693_dev *max77843 = info->max77843;
Dan Carpenterd927c592015-03-15 13:49:47 +0300676 int ret;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900677
678 switch (time) {
679 case MAX77843_DEBOUNCE_TIME_5MS:
680 case MAX77843_DEBOUNCE_TIME_10MS:
681 case MAX77843_DEBOUNCE_TIME_25MS:
682 case MAX77843_DEBOUNCE_TIME_38_62MS:
683 ret = regmap_update_bits(max77843->regmap_muic,
684 MAX77843_MUIC_REG_CONTROL4,
685 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900686 time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900687 if (ret < 0) {
688 dev_err(info->dev, "Cannot write MUIC regmap\n");
689 return ret;
690 }
691 break;
692 default:
693 dev_err(info->dev, "Invalid ADC debounce time\n");
694 return -EINVAL;
695 }
696
697 return 0;
698}
699
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900700static int max77843_init_muic_regmap(struct max77693_dev *max77843)
Jaewon Kim27a28d32015-02-04 13:56:07 +0900701{
702 int ret;
703
704 max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter,
705 I2C_ADDR_MUIC);
706 if (!max77843->i2c_muic) {
707 dev_err(&max77843->i2c->dev,
708 "Cannot allocate I2C device for MUIC\n");
Dan Carpenterb9b518f2015-03-15 13:56:04 +0300709 return -ENOMEM;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900710 }
711
712 i2c_set_clientdata(max77843->i2c_muic, max77843);
713
714 max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
715 &max77843_muic_regmap_config);
716 if (IS_ERR(max77843->regmap_muic)) {
717 ret = PTR_ERR(max77843->regmap_muic);
718 goto err_muic_i2c;
719 }
720
721 ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
722 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
723 0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
724 if (ret < 0) {
725 dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
726 goto err_muic_i2c;
727 }
728
729 return 0;
730
731err_muic_i2c:
732 i2c_unregister_device(max77843->i2c_muic);
733
734 return ret;
735}
736
737static int max77843_muic_probe(struct platform_device *pdev)
738{
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900739 struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900740 struct max77843_muic_info *info;
741 unsigned int id;
742 int i, ret;
743
744 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
745 if (!info)
746 return -ENOMEM;
747
748 info->dev = &pdev->dev;
749 info->max77843 = max77843;
750
751 platform_set_drvdata(pdev, info);
752 mutex_init(&info->mutex);
753
754 /* Initialize i2c and regmap */
755 ret = max77843_init_muic_regmap(max77843);
756 if (ret) {
757 dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
758 return ret;
759 }
760
761 /* Turn off auto detection configuration */
762 ret = regmap_update_bits(max77843->regmap_muic,
763 MAX77843_MUIC_REG_CONTROL4,
764 MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
765 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
766 CONTROL4_AUTO_DISABLE);
767
768 /* Initialize extcon device */
769 info->edev = devm_extcon_dev_allocate(&pdev->dev,
770 max77843_extcon_cable);
771 if (IS_ERR(info->edev)) {
772 dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
773 ret = -ENODEV;
774 goto err_muic_irq;
775 }
776
777 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
778 if (ret) {
779 dev_err(&pdev->dev, "Failed to register extcon device\n");
780 goto err_muic_irq;
781 }
782
783 /* Set ADC debounce time */
784 max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
785
786 /* Set initial path for UART */
Krzysztof Kozlowski309a3e02015-07-15 21:59:53 +0900787 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, true);
Jaewon Kim27a28d32015-02-04 13:56:07 +0900788
789 /* Check revision number of MUIC device */
790 ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
791 if (ret < 0) {
792 dev_err(&pdev->dev, "Failed to read revision number\n");
793 goto err_muic_irq;
794 }
795 dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
796
797 /* Support virtual irq domain for max77843 MUIC device */
798 INIT_WORK(&info->irq_work, max77843_muic_irq_work);
799
Jaewon Kim135d9f72015-06-05 13:32:27 +0900800 /* Clear IRQ bits before request IRQs */
801 ret = regmap_bulk_read(max77843->regmap_muic,
802 MAX77843_MUIC_REG_INT1, info->status,
803 MAX77843_MUIC_IRQ_NUM);
804 if (ret) {
805 dev_err(&pdev->dev, "Failed to Clear IRQ bits\n");
806 goto err_muic_irq;
807 }
808
Jaewon Kim27a28d32015-02-04 13:56:07 +0900809 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
810 struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
811 unsigned int virq = 0;
812
813 virq = regmap_irq_get_virq(max77843->irq_data_muic,
814 muic_irq->irq);
815 if (virq <= 0) {
816 ret = -EINVAL;
817 goto err_muic_irq;
818 }
819 muic_irq->virq = virq;
820
821 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
822 max77843_muic_irq_handler, IRQF_NO_SUSPEND,
823 muic_irq->name, info);
824 if (ret) {
825 dev_err(&pdev->dev,
826 "Failed to request irq (IRQ: %d, error: %d)\n",
827 muic_irq->irq, ret);
828 goto err_muic_irq;
829 }
830 }
831
832 /* Detect accessory after completing the initialization of platform */
833 INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
834 queue_delayed_work(system_power_efficient_wq,
835 &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
836
837 return 0;
838
839err_muic_irq:
840 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
841 i2c_unregister_device(max77843->i2c_muic);
842
843 return ret;
844}
845
846static int max77843_muic_remove(struct platform_device *pdev)
847{
848 struct max77843_muic_info *info = platform_get_drvdata(pdev);
Krzysztof Kozlowskibc1aadc2015-07-15 21:59:51 +0900849 struct max77693_dev *max77843 = info->max77843;
Jaewon Kim27a28d32015-02-04 13:56:07 +0900850
851 cancel_work_sync(&info->irq_work);
852 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
853 i2c_unregister_device(max77843->i2c_muic);
854
855 return 0;
856}
857
858static const struct platform_device_id max77843_muic_id[] = {
859 { "max77843-muic", },
860 { /* sentinel */ },
861};
862MODULE_DEVICE_TABLE(platform, max77843_muic_id);
863
864static struct platform_driver max77843_muic_driver = {
865 .driver = {
866 .name = "max77843-muic",
867 },
868 .probe = max77843_muic_probe,
869 .remove = max77843_muic_remove,
870 .id_table = max77843_muic_id,
871};
872
873static int __init max77843_muic_init(void)
874{
875 return platform_driver_register(&max77843_muic_driver);
876}
877subsys_initcall(max77843_muic_init);
878
879MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
880MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
881MODULE_LICENSE("GPL");