dell-smbios: rename release_buffer() to dell_smbios_release_buffer()
[linux-2.6-block.git] / drivers / extcon / extcon-max77843.c
CommitLineData
27a28d32
JK
1/*
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>
bc1aadc1 18#include <linux/mfd/max77693-common.h>
27a28d32
JK
19#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;
bc1aadc1 36 struct max77693_dev *max77843;
27a28d32
JK
37 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
73b6ecdb 122static const unsigned int max77843_extcon_cable[] = {
2a9de9c0
CC
123 EXTCON_USB,
124 EXTCON_USB_HOST,
11eecf91
CC
125 EXTCON_CHG_USB_DCP,
126 EXTCON_CHG_USB_CDP,
127 EXTCON_CHG_USB_FAST,
128 EXTCON_CHG_USB_SLOW,
129 EXTCON_DISP_MHL,
2a9de9c0
CC
130 EXTCON_JIG,
131 EXTCON_NONE,
27a28d32
JK
132};
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{
bc1aadc1 202 struct max77693_dev *max77843 = info->max77843;
27a28d32
JK
203 int ret = 0;
204 unsigned int ctrl1, ctrl2;
205
206 if (attached)
207 ctrl1 = val;
208 else
309a3e00 209 ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN;
27a28d32
JK
210
211 ret = regmap_update_bits(max77843->regmap_muic,
212 MAX77843_MUIC_REG_CONTROL1,
309a3e00 213 MAX77843_MUIC_CONTROL1_COM_SW, ctrl1);
27a28d32
JK
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;
309a3e00 247 adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT;
27a28d32
JK
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);
309a3e00 313 gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT;
27a28d32
JK
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:
309a3e00
KK
342 ret = max77843_muic_set_path(info,
343 MAX77843_MUIC_CONTROL1_SW_USB,
344 attached);
27a28d32
JK
345 if (ret < 0)
346 return ret;
347
2a9de9c0 348 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, attached);
27a28d32
JK
349 break;
350 case MAX77843_MUIC_GND_MHL_VB:
351 case MAX77843_MUIC_GND_MHL:
309a3e00
KK
352 ret = max77843_muic_set_path(info,
353 MAX77843_MUIC_CONTROL1_SW_OPEN,
354 attached);
27a28d32
JK
355 if (ret < 0)
356 return ret;
357
11eecf91 358 extcon_set_cable_state_(info->edev, EXTCON_DISP_MHL, attached);
27a28d32
JK
359 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;
309a3e00 373 u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN;
27a28d32
JK
374
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:
27a28d32 380 case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON:
309a3e00 381 path = MAX77843_MUIC_CONTROL1_SW_USB;
27a28d32
JK
382 break;
383 case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF:
309a3e00 384 path = MAX77843_MUIC_CONTROL1_SW_UART;
27a28d32
JK
385 break;
386 default:
41b3c015 387 return -EINVAL;
27a28d32
JK
388 }
389
41b3c015
CC
390 ret = max77843_muic_set_path(info, path, attached);
391 if (ret < 0)
392 return ret;
393
2a9de9c0 394 extcon_set_cable_state_(info->edev, EXTCON_JIG, attached);
41b3c015 395
27a28d32
JK
396 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:
309a3e00
KK
482 ret = max77843_muic_set_path(info,
483 MAX77843_MUIC_CONTROL1_SW_USB,
484 attached);
27a28d32
JK
485 if (ret < 0)
486 return ret;
487
2a9de9c0 488 extcon_set_cable_state_(info->edev, EXTCON_USB, attached);
27a28d32
JK
489 break;
490 case MAX77843_MUIC_CHG_DOWNSTREAM:
309a3e00
KK
491 ret = max77843_muic_set_path(info,
492 MAX77843_MUIC_CONTROL1_SW_OPEN,
493 attached);
27a28d32
JK
494 if (ret < 0)
495 return ret;
496
11eecf91 497 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_CDP,
2a9de9c0 498 attached);
27a28d32
JK
499 break;
500 case MAX77843_MUIC_CHG_DEDICATED:
309a3e00
KK
501 ret = max77843_muic_set_path(info,
502 MAX77843_MUIC_CONTROL1_SW_OPEN,
503 attached);
27a28d32
JK
504 if (ret < 0)
505 return ret;
506
11eecf91
CC
507 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_DCP,
508 attached);
27a28d32
JK
509 break;
510 case MAX77843_MUIC_CHG_SPECIAL_500MA:
309a3e00
KK
511 ret = max77843_muic_set_path(info,
512 MAX77843_MUIC_CONTROL1_SW_OPEN,
513 attached);
27a28d32
JK
514 if (ret < 0)
515 return ret;
516
11eecf91 517 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_SLOW,
2a9de9c0 518 attached);
27a28d32
JK
519 break;
520 case MAX77843_MUIC_CHG_SPECIAL_1A:
309a3e00
KK
521 ret = max77843_muic_set_path(info,
522 MAX77843_MUIC_CONTROL1_SW_OPEN,
523 attached);
27a28d32
JK
524 if (ret < 0)
525 return ret;
526
11eecf91 527 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_FAST,
2a9de9c0 528 attached);
27a28d32
JK
529 break;
530 case MAX77843_MUIC_CHG_GND:
531 gnd_type = max77843_muic_get_cable_type(info,
532 MAX77843_CABLE_GROUP_ADC_GND, &attached);
533
534 /* Charger cable on MHL accessory is attach or detach */
535 if (gnd_type == MAX77843_MUIC_GND_MHL_VB)
11eecf91
CC
536 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_DCP,
537 true);
27a28d32 538 else if (gnd_type == MAX77843_MUIC_GND_MHL)
11eecf91
CC
539 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_DCP,
540 false);
27a28d32
JK
541 break;
542 case MAX77843_MUIC_CHG_NONE:
543 break;
544 default:
545 dev_err(info->dev,
546 "failed to detect %s accessory (chg_type:0x%x)\n",
547 attached ? "attached" : "detached", chg_type);
548
309a3e00
KK
549 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN,
550 attached);
27a28d32
JK
551 return -EINVAL;
552 }
553
554 return 0;
555}
556
557static void max77843_muic_irq_work(struct work_struct *work)
558{
559 struct max77843_muic_info *info = container_of(work,
560 struct max77843_muic_info, irq_work);
bc1aadc1 561 struct max77693_dev *max77843 = info->max77843;
27a28d32
JK
562 int ret = 0;
563
564 mutex_lock(&info->mutex);
565
566 ret = regmap_bulk_read(max77843->regmap_muic,
567 MAX77843_MUIC_REG_STATUS1, info->status,
568 MAX77843_MUIC_STATUS_NUM);
569 if (ret) {
570 dev_err(info->dev, "Cannot read STATUS registers\n");
571 mutex_unlock(&info->mutex);
572 return;
573 }
574
575 if (info->irq_adc) {
576 ret = max77843_muic_adc_handler(info);
577 if (ret)
578 dev_err(info->dev, "Unknown cable type\n");
579 info->irq_adc = false;
580 }
581
582 if (info->irq_chg) {
583 ret = max77843_muic_chg_handler(info);
584 if (ret)
585 dev_err(info->dev, "Unknown charger type\n");
586 info->irq_chg = false;
587 }
588
589 mutex_unlock(&info->mutex);
590}
591
592static irqreturn_t max77843_muic_irq_handler(int irq, void *data)
593{
594 struct max77843_muic_info *info = data;
595 int i, irq_type = -1;
596
597 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++)
598 if (irq == max77843_muic_irqs[i].virq)
599 irq_type = max77843_muic_irqs[i].irq;
600
601 switch (irq_type) {
602 case MAX77843_MUIC_IRQ_INT1_ADC:
603 case MAX77843_MUIC_IRQ_INT1_ADCERROR:
604 case MAX77843_MUIC_IRQ_INT1_ADC1K:
605 info->irq_adc = true;
606 break;
607 case MAX77843_MUIC_IRQ_INT2_CHGTYP:
608 case MAX77843_MUIC_IRQ_INT2_CHGDETRUN:
609 case MAX77843_MUIC_IRQ_INT2_DCDTMR:
610 case MAX77843_MUIC_IRQ_INT2_DXOVP:
611 case MAX77843_MUIC_IRQ_INT2_VBVOLT:
612 info->irq_chg = true;
613 break;
614 case MAX77843_MUIC_IRQ_INT3_VBADC:
615 case MAX77843_MUIC_IRQ_INT3_VDNMON:
616 case MAX77843_MUIC_IRQ_INT3_DNRES:
617 case MAX77843_MUIC_IRQ_INT3_MPNACK:
618 case MAX77843_MUIC_IRQ_INT3_MRXBUFOW:
619 case MAX77843_MUIC_IRQ_INT3_MRXTRF:
620 case MAX77843_MUIC_IRQ_INT3_MRXPERR:
621 case MAX77843_MUIC_IRQ_INT3_MRXRDY:
622 break;
623 default:
624 dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type);
625 break;
626 }
627
628 schedule_work(&info->irq_work);
629
630 return IRQ_HANDLED;
631}
632
633static void max77843_muic_detect_cable_wq(struct work_struct *work)
634{
635 struct max77843_muic_info *info = container_of(to_delayed_work(work),
636 struct max77843_muic_info, wq_detcable);
bc1aadc1 637 struct max77693_dev *max77843 = info->max77843;
27a28d32
JK
638 int chg_type, adc, ret;
639 bool attached;
640
641 mutex_lock(&info->mutex);
642
643 ret = regmap_bulk_read(max77843->regmap_muic,
644 MAX77843_MUIC_REG_STATUS1, info->status,
645 MAX77843_MUIC_STATUS_NUM);
646 if (ret) {
647 dev_err(info->dev, "Cannot read STATUS registers\n");
648 goto err_cable_wq;
649 }
650
651 adc = max77843_muic_get_cable_type(info,
652 MAX77843_CABLE_GROUP_ADC, &attached);
653 if (attached && adc != MAX77843_MUIC_ADC_OPEN) {
654 ret = max77843_muic_adc_handler(info);
655 if (ret < 0) {
656 dev_err(info->dev, "Cannot detect accessory\n");
657 goto err_cable_wq;
658 }
659 }
660
661 chg_type = max77843_muic_get_cable_type(info,
662 MAX77843_CABLE_GROUP_CHG, &attached);
663 if (attached && chg_type != MAX77843_MUIC_CHG_NONE) {
664 ret = max77843_muic_chg_handler(info);
665 if (ret < 0) {
666 dev_err(info->dev, "Cannot detect charger accessory\n");
667 goto err_cable_wq;
668 }
669 }
670
671err_cable_wq:
672 mutex_unlock(&info->mutex);
673}
674
675static int max77843_muic_set_debounce_time(struct max77843_muic_info *info,
676 enum max77843_muic_adc_debounce_time time)
677{
bc1aadc1 678 struct max77693_dev *max77843 = info->max77843;
d927c590 679 int ret;
27a28d32
JK
680
681 switch (time) {
682 case MAX77843_DEBOUNCE_TIME_5MS:
683 case MAX77843_DEBOUNCE_TIME_10MS:
684 case MAX77843_DEBOUNCE_TIME_25MS:
685 case MAX77843_DEBOUNCE_TIME_38_62MS:
686 ret = regmap_update_bits(max77843->regmap_muic,
687 MAX77843_MUIC_REG_CONTROL4,
688 MAX77843_MUIC_CONTROL4_ADCDBSET_MASK,
309a3e00 689 time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT);
27a28d32
JK
690 if (ret < 0) {
691 dev_err(info->dev, "Cannot write MUIC regmap\n");
692 return ret;
693 }
694 break;
695 default:
696 dev_err(info->dev, "Invalid ADC debounce time\n");
697 return -EINVAL;
698 }
699
700 return 0;
701}
702
bc1aadc1 703static int max77843_init_muic_regmap(struct max77693_dev *max77843)
27a28d32
JK
704{
705 int ret;
706
707 max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter,
708 I2C_ADDR_MUIC);
709 if (!max77843->i2c_muic) {
710 dev_err(&max77843->i2c->dev,
711 "Cannot allocate I2C device for MUIC\n");
b9b518f5 712 return -ENOMEM;
27a28d32
JK
713 }
714
715 i2c_set_clientdata(max77843->i2c_muic, max77843);
716
717 max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic,
718 &max77843_muic_regmap_config);
719 if (IS_ERR(max77843->regmap_muic)) {
720 ret = PTR_ERR(max77843->regmap_muic);
721 goto err_muic_i2c;
722 }
723
724 ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq,
725 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
726 0, &max77843_muic_irq_chip, &max77843->irq_data_muic);
727 if (ret < 0) {
728 dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n");
729 goto err_muic_i2c;
730 }
731
732 return 0;
733
734err_muic_i2c:
735 i2c_unregister_device(max77843->i2c_muic);
736
737 return ret;
738}
739
740static int max77843_muic_probe(struct platform_device *pdev)
741{
bc1aadc1 742 struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent);
27a28d32
JK
743 struct max77843_muic_info *info;
744 unsigned int id;
745 int i, ret;
746
747 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
748 if (!info)
749 return -ENOMEM;
750
751 info->dev = &pdev->dev;
752 info->max77843 = max77843;
753
754 platform_set_drvdata(pdev, info);
755 mutex_init(&info->mutex);
756
757 /* Initialize i2c and regmap */
758 ret = max77843_init_muic_regmap(max77843);
759 if (ret) {
760 dev_err(&pdev->dev, "Failed to init MUIC regmap\n");
761 return ret;
762 }
763
764 /* Turn off auto detection configuration */
765 ret = regmap_update_bits(max77843->regmap_muic,
766 MAX77843_MUIC_REG_CONTROL4,
767 MAX77843_MUIC_CONTROL4_USBAUTO_MASK |
768 MAX77843_MUIC_CONTROL4_FCTAUTO_MASK,
769 CONTROL4_AUTO_DISABLE);
770
771 /* Initialize extcon device */
772 info->edev = devm_extcon_dev_allocate(&pdev->dev,
773 max77843_extcon_cable);
774 if (IS_ERR(info->edev)) {
775 dev_err(&pdev->dev, "Failed to allocate memory for extcon\n");
776 ret = -ENODEV;
777 goto err_muic_irq;
778 }
779
780 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
781 if (ret) {
782 dev_err(&pdev->dev, "Failed to register extcon device\n");
783 goto err_muic_irq;
784 }
785
786 /* Set ADC debounce time */
787 max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS);
788
789 /* Set initial path for UART */
309a3e00 790 max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, true);
27a28d32
JK
791
792 /* Check revision number of MUIC device */
793 ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id);
794 if (ret < 0) {
795 dev_err(&pdev->dev, "Failed to read revision number\n");
796 goto err_muic_irq;
797 }
798 dev_info(info->dev, "MUIC device ID : 0x%x\n", id);
799
800 /* Support virtual irq domain for max77843 MUIC device */
801 INIT_WORK(&info->irq_work, max77843_muic_irq_work);
802
135d9f7d
JK
803 /* Clear IRQ bits before request IRQs */
804 ret = regmap_bulk_read(max77843->regmap_muic,
805 MAX77843_MUIC_REG_INT1, info->status,
806 MAX77843_MUIC_IRQ_NUM);
807 if (ret) {
808 dev_err(&pdev->dev, "Failed to Clear IRQ bits\n");
809 goto err_muic_irq;
810 }
811
27a28d32
JK
812 for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) {
813 struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i];
c05c0d54 814 int virq = 0;
27a28d32
JK
815
816 virq = regmap_irq_get_virq(max77843->irq_data_muic,
817 muic_irq->irq);
818 if (virq <= 0) {
819 ret = -EINVAL;
820 goto err_muic_irq;
821 }
822 muic_irq->virq = virq;
823
824 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
825 max77843_muic_irq_handler, IRQF_NO_SUSPEND,
826 muic_irq->name, info);
827 if (ret) {
828 dev_err(&pdev->dev,
829 "Failed to request irq (IRQ: %d, error: %d)\n",
830 muic_irq->irq, ret);
831 goto err_muic_irq;
832 }
833 }
834
835 /* Detect accessory after completing the initialization of platform */
836 INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq);
837 queue_delayed_work(system_power_efficient_wq,
838 &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT));
839
840 return 0;
841
842err_muic_irq:
843 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
844 i2c_unregister_device(max77843->i2c_muic);
845
846 return ret;
847}
848
849static int max77843_muic_remove(struct platform_device *pdev)
850{
851 struct max77843_muic_info *info = platform_get_drvdata(pdev);
bc1aadc1 852 struct max77693_dev *max77843 = info->max77843;
27a28d32
JK
853
854 cancel_work_sync(&info->irq_work);
855 regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic);
856 i2c_unregister_device(max77843->i2c_muic);
857
858 return 0;
859}
860
861static const struct platform_device_id max77843_muic_id[] = {
862 { "max77843-muic", },
863 { /* sentinel */ },
864};
865MODULE_DEVICE_TABLE(platform, max77843_muic_id);
866
867static struct platform_driver max77843_muic_driver = {
868 .driver = {
869 .name = "max77843-muic",
870 },
871 .probe = max77843_muic_probe,
872 .remove = max77843_muic_remove,
873 .id_table = max77843_muic_id,
874};
875
876static int __init max77843_muic_init(void)
877{
878 return platform_driver_register(&max77843_muic_driver);
879}
880subsys_initcall(max77843_muic_init);
881
882MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver");
883MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
884MODULE_LICENSE("GPL");