blob: 601dbd9964874a7b5cdef3d0588b60b768487342 [file] [log] [blame]
Chanwoo Choi962e56b2013-11-22 16:51:06 +01001/*
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +02002 * extcon-max14577.c - MAX14577/77836 extcon driver to support MUIC
Chanwoo Choi962e56b2013-11-22 16:51:06 +01003 *
Krzysztof Kozlowski6ab0b112014-11-12 16:28:09 +01004 * Copyright (C) 2013,2014 Samsung Electronics
Chanwoo Choi962e56b2013-11-22 16:51:06 +01005 * Chanwoo Choi <cw00.choi@samsung.com>
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +02006 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
Chanwoo Choi962e56b2013-11-22 16:51:06 +01007 *
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 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/i2c.h>
22#include <linux/interrupt.h>
23#include <linux/platform_device.h>
24#include <linux/mfd/max14577.h>
25#include <linux/mfd/max14577-private.h>
26#include <linux/extcon.h>
27
Chanwoo Choi962e56b2013-11-22 16:51:06 +010028#define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
29
30enum max14577_muic_adc_debounce_time {
31 ADC_DEBOUNCE_TIME_5MS = 0,
32 ADC_DEBOUNCE_TIME_10MS,
33 ADC_DEBOUNCE_TIME_25MS,
34 ADC_DEBOUNCE_TIME_38_62MS,
35};
36
37enum max14577_muic_status {
38 MAX14577_MUIC_STATUS1 = 0,
39 MAX14577_MUIC_STATUS2 = 1,
40 MAX14577_MUIC_STATUS_END,
41};
42
Krzysztof Kozlowski0ca852b2014-04-14 11:17:16 +020043/**
44 * struct max14577_muic_irq
45 * @irq: the index of irq list of MUIC device.
46 * @name: the name of irq.
47 * @virq: the virtual irq to use irq domain
48 */
49struct max14577_muic_irq {
50 unsigned int irq;
51 const char *name;
52 unsigned int virq;
53};
54
55static struct max14577_muic_irq max14577_muic_irqs[] = {
56 { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
57 { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
58 { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
59 { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
60 { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
61 { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
62 { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
63 { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
64};
65
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +020066static struct max14577_muic_irq max77836_muic_irqs[] = {
67 { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
68 { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
69 { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
70 { MAX77836_IRQ_INT1_ADC1K, "muic-ADC1K" },
71 { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
72 { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
73 { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
74 { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
75 { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
76 { MAX77836_IRQ_INT2_VIDRM, "muic-VIDRM" },
77};
78
Chanwoo Choi962e56b2013-11-22 16:51:06 +010079struct max14577_muic_info {
80 struct device *dev;
81 struct max14577 *max14577;
82 struct extcon_dev *edev;
83 int prev_cable_type;
84 int prev_chg_type;
85 u8 status[MAX14577_MUIC_STATUS_END];
86
Krzysztof Kozlowski0ca852b2014-04-14 11:17:16 +020087 struct max14577_muic_irq *muic_irqs;
88 unsigned int muic_irqs_num;
Chanwoo Choi962e56b2013-11-22 16:51:06 +010089 bool irq_adc;
90 bool irq_chg;
91 struct work_struct irq_work;
92 struct mutex mutex;
93
94 /*
95 * Use delayed workqueue to detect cable state and then
96 * notify cable state to notifiee/platform through uevent.
97 * After completing the booting of platform, the extcon provider
98 * driver should notify cable state to upper layer.
99 */
100 struct delayed_work wq_detcable;
101
102 /*
103 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
104 * h/w path of COMP2/COMN1 on CONTROL1 register.
105 */
106 int path_usb;
107 int path_uart;
108};
109
110enum max14577_muic_cable_group {
111 MAX14577_CABLE_GROUP_ADC = 0,
112 MAX14577_CABLE_GROUP_CHG,
113};
114
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100115/* Define supported accessory type */
116enum max14577_muic_acc_type {
117 MAX14577_MUIC_ADC_GROUND = 0x0,
118 MAX14577_MUIC_ADC_SEND_END_BUTTON,
119 MAX14577_MUIC_ADC_REMOTE_S1_BUTTON,
120 MAX14577_MUIC_ADC_REMOTE_S2_BUTTON,
121 MAX14577_MUIC_ADC_REMOTE_S3_BUTTON,
122 MAX14577_MUIC_ADC_REMOTE_S4_BUTTON,
123 MAX14577_MUIC_ADC_REMOTE_S5_BUTTON,
124 MAX14577_MUIC_ADC_REMOTE_S6_BUTTON,
125 MAX14577_MUIC_ADC_REMOTE_S7_BUTTON,
126 MAX14577_MUIC_ADC_REMOTE_S8_BUTTON,
127 MAX14577_MUIC_ADC_REMOTE_S9_BUTTON,
128 MAX14577_MUIC_ADC_REMOTE_S10_BUTTON,
129 MAX14577_MUIC_ADC_REMOTE_S11_BUTTON,
130 MAX14577_MUIC_ADC_REMOTE_S12_BUTTON,
131 MAX14577_MUIC_ADC_RESERVED_ACC_1,
132 MAX14577_MUIC_ADC_RESERVED_ACC_2,
133 MAX14577_MUIC_ADC_RESERVED_ACC_3,
134 MAX14577_MUIC_ADC_RESERVED_ACC_4,
135 MAX14577_MUIC_ADC_RESERVED_ACC_5,
136 MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2,
137 MAX14577_MUIC_ADC_PHONE_POWERED_DEV,
138 MAX14577_MUIC_ADC_TTY_CONVERTER,
139 MAX14577_MUIC_ADC_UART_CABLE,
140 MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG,
141 MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF,
142 MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON,
143 MAX14577_MUIC_ADC_AV_CABLE_NOLOAD,
144 MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG,
145 MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF,
146 MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON,
147 MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1, /* with Remote and Simple Ctrl */
148 MAX14577_MUIC_ADC_OPEN,
149};
150
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900151static const unsigned int max14577_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900152 EXTCON_USB,
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900153 EXTCON_CHG_USB_DCP,
154 EXTCON_CHG_USB_FAST,
155 EXTCON_CHG_USB_SLOW,
156 EXTCON_CHG_USB_CDP,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900157 EXTCON_JIG,
158 EXTCON_NONE,
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100159};
160
161/*
162 * max14577_muic_set_debounce_time - Set the debounce time of ADC
163 * @info: the instance including private data of max14577 MUIC
164 * @time: the debounce time of ADC
165 */
166static int max14577_muic_set_debounce_time(struct max14577_muic_info *info,
167 enum max14577_muic_adc_debounce_time time)
168{
169 u8 ret;
170
171 switch (time) {
172 case ADC_DEBOUNCE_TIME_5MS:
173 case ADC_DEBOUNCE_TIME_10MS:
174 case ADC_DEBOUNCE_TIME_25MS:
175 case ADC_DEBOUNCE_TIME_38_62MS:
176 ret = max14577_update_reg(info->max14577->regmap,
177 MAX14577_MUIC_REG_CONTROL3,
178 CTRL3_ADCDBSET_MASK,
179 time << CTRL3_ADCDBSET_SHIFT);
180 if (ret) {
181 dev_err(info->dev, "failed to set ADC debounce time\n");
182 return ret;
183 }
184 break;
185 default:
186 dev_err(info->dev, "invalid ADC debounce time\n");
187 return -EINVAL;
188 }
189
190 return 0;
191};
192
193/*
194 * max14577_muic_set_path - Set hardware line according to attached cable
195 * @info: the instance including private data of max14577 MUIC
196 * @value: the path according to attached cable
197 * @attached: the state of cable (true:attached, false:detached)
198 *
199 * The max14577 MUIC device share outside H/W line among a varity of cables
200 * so, this function set internal path of H/W line according to the type of
201 * attached cable.
202 */
203static int max14577_muic_set_path(struct max14577_muic_info *info,
204 u8 val, bool attached)
205{
206 int ret = 0;
207 u8 ctrl1, ctrl2 = 0;
208
209 /* Set open state to path before changing hw path */
210 ret = max14577_update_reg(info->max14577->regmap,
211 MAX14577_MUIC_REG_CONTROL1,
212 CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN);
213 if (ret < 0) {
214 dev_err(info->dev, "failed to update MUIC register\n");
215 return ret;
216 }
217
218 if (attached)
219 ctrl1 = val;
220 else
221 ctrl1 = CTRL1_SW_OPEN;
222
223 ret = max14577_update_reg(info->max14577->regmap,
224 MAX14577_MUIC_REG_CONTROL1,
225 CLEAR_IDBEN_MICEN_MASK, ctrl1);
226 if (ret < 0) {
227 dev_err(info->dev, "failed to update MUIC register\n");
228 return ret;
229 }
230
231 if (attached)
232 ctrl2 |= CTRL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
233 else
234 ctrl2 |= CTRL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
235
236 ret = max14577_update_reg(info->max14577->regmap,
237 MAX14577_REG_CONTROL2,
238 CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2);
239 if (ret < 0) {
240 dev_err(info->dev, "failed to update MUIC register\n");
241 return ret;
242 }
243
244 dev_dbg(info->dev,
245 "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
246 ctrl1, ctrl2, attached ? "attached" : "detached");
247
248 return 0;
249}
250
251/*
252 * max14577_muic_get_cable_type - Return cable type and check cable state
253 * @info: the instance including private data of max14577 MUIC
254 * @group: the path according to attached cable
255 * @attached: store cable state and return
256 *
257 * This function check the cable state either attached or detached,
258 * and then divide precise type of cable according to cable group.
259 * - max14577_CABLE_GROUP_ADC
260 * - max14577_CABLE_GROUP_CHG
261 */
262static int max14577_muic_get_cable_type(struct max14577_muic_info *info,
263 enum max14577_muic_cable_group group, bool *attached)
264{
265 int cable_type = 0;
266 int adc;
267 int chg_type;
268
269 switch (group) {
270 case MAX14577_CABLE_GROUP_ADC:
271 /*
272 * Read ADC value to check cable type and decide cable state
273 * according to cable type
274 */
275 adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK;
276 adc >>= STATUS1_ADC_SHIFT;
277
278 /*
279 * Check current cable state/cable type and store cable type
280 * (info->prev_cable_type) for handling cable when cable is
281 * detached.
282 */
283 if (adc == MAX14577_MUIC_ADC_OPEN) {
284 *attached = false;
285
286 cable_type = info->prev_cable_type;
287 info->prev_cable_type = MAX14577_MUIC_ADC_OPEN;
288 } else {
289 *attached = true;
290
291 cable_type = info->prev_cable_type = adc;
292 }
293 break;
294 case MAX14577_CABLE_GROUP_CHG:
295 /*
296 * Read charger type to check cable type and decide cable state
297 * according to type of charger cable.
298 */
299 chg_type = info->status[MAX14577_MUIC_STATUS2] &
300 STATUS2_CHGTYP_MASK;
301 chg_type >>= STATUS2_CHGTYP_SHIFT;
302
303 if (chg_type == MAX14577_CHARGER_TYPE_NONE) {
304 *attached = false;
305
306 cable_type = info->prev_chg_type;
307 info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE;
308 } else {
309 *attached = true;
310
311 /*
312 * Check current cable state/cable type and store cable
313 * type(info->prev_chg_type) for handling cable when
314 * charger cable is detached.
315 */
316 cable_type = info->prev_chg_type = chg_type;
317 }
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 max14577_muic_jig_handler(struct max14577_muic_info *info,
330 int cable_type, bool attached)
331{
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100332 int ret = 0;
333 u8 path = CTRL1_SW_OPEN;
334
335 dev_dbg(info->dev,
336 "external connector is %s (adc:0x%02x)\n",
337 attached ? "attached" : "detached", cable_type);
338
339 switch (cable_type) {
340 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100341 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
342 /* PATH:AP_USB */
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100343 path = CTRL1_SW_USB;
344 break;
345 case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
346 /* PATH:AP_UART */
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100347 path = CTRL1_SW_UART;
348 break;
349 default:
350 dev_err(info->dev, "failed to detect %s jig cable\n",
351 attached ? "attached" : "detached");
352 return -EINVAL;
353 }
354
355 ret = max14577_muic_set_path(info, path, attached);
356 if (ret < 0)
357 return ret;
358
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900359 extcon_set_cable_state_(info->edev, EXTCON_JIG, attached);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100360
361 return 0;
362}
363
364static int max14577_muic_adc_handler(struct max14577_muic_info *info)
365{
366 int cable_type;
367 bool attached;
368 int ret = 0;
369
370 /* Check accessory state which is either detached or attached */
371 cable_type = max14577_muic_get_cable_type(info,
372 MAX14577_CABLE_GROUP_ADC, &attached);
373
374 dev_dbg(info->dev,
375 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
376 attached ? "attached" : "detached", cable_type,
377 info->prev_cable_type);
378
379 switch (cable_type) {
380 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:
381 case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:
382 case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:
383 /* JIG */
384 ret = max14577_muic_jig_handler(info, cable_type, attached);
385 if (ret < 0)
386 return ret;
387 break;
388 case MAX14577_MUIC_ADC_GROUND:
389 case MAX14577_MUIC_ADC_SEND_END_BUTTON:
390 case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON:
391 case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON:
392 case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON:
393 case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON:
394 case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON:
395 case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON:
396 case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON:
397 case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON:
398 case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON:
399 case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON:
400 case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON:
401 case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON:
402 case MAX14577_MUIC_ADC_RESERVED_ACC_1:
403 case MAX14577_MUIC_ADC_RESERVED_ACC_2:
404 case MAX14577_MUIC_ADC_RESERVED_ACC_3:
405 case MAX14577_MUIC_ADC_RESERVED_ACC_4:
406 case MAX14577_MUIC_ADC_RESERVED_ACC_5:
407 case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2:
408 case MAX14577_MUIC_ADC_PHONE_POWERED_DEV:
409 case MAX14577_MUIC_ADC_TTY_CONVERTER:
410 case MAX14577_MUIC_ADC_UART_CABLE:
411 case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG:
412 case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD:
413 case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG:
414 case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON:
415 case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1:
416 /*
417 * This accessory isn't used in general case if it is specially
418 * needed to detect additional accessory, should implement
419 * proper operation when this accessory is attached/detached.
420 */
421 dev_info(info->dev,
422 "accessory is %s but it isn't used (adc:0x%x)\n",
423 attached ? "attached" : "detached", cable_type);
424 return -EAGAIN;
425 default:
426 dev_err(info->dev,
427 "failed to detect %s accessory (adc:0x%x)\n",
428 attached ? "attached" : "detached", cable_type);
429 return -EINVAL;
430 }
431
432 return 0;
433}
434
435static int max14577_muic_chg_handler(struct max14577_muic_info *info)
436{
437 int chg_type;
438 bool attached;
439 int ret = 0;
440
441 chg_type = max14577_muic_get_cable_type(info,
442 MAX14577_CABLE_GROUP_CHG, &attached);
443
444 dev_dbg(info->dev,
445 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
446 attached ? "attached" : "detached",
447 chg_type, info->prev_chg_type);
448
449 switch (chg_type) {
450 case MAX14577_CHARGER_TYPE_USB:
451 /* PATH:AP_USB */
452 ret = max14577_muic_set_path(info, info->path_usb, attached);
453 if (ret < 0)
454 return ret;
455
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900456 extcon_set_cable_state_(info->edev, EXTCON_USB, attached);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100457 break;
458 case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900459 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_DCP,
460 attached);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100461 break;
462 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900463 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_CDP,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900464 attached);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100465 break;
466 case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900467 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_SLOW,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900468 attached);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100469 break;
470 case MAX14577_CHARGER_TYPE_SPECIAL_1A:
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900471 extcon_set_cable_state_(info->edev, EXTCON_CHG_USB_FAST,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900472 attached);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100473 break;
474 case MAX14577_CHARGER_TYPE_NONE:
475 case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
476 break;
477 default:
478 dev_err(info->dev,
479 "failed to detect %s accessory (chg_type:0x%x)\n",
480 attached ? "attached" : "detached", chg_type);
481 return -EINVAL;
482 }
483
484 return 0;
485}
486
487static void max14577_muic_irq_work(struct work_struct *work)
488{
489 struct max14577_muic_info *info = container_of(work,
490 struct max14577_muic_info, irq_work);
491 int ret = 0;
492
493 if (!info->edev)
494 return;
495
496 mutex_lock(&info->mutex);
497
498 ret = max14577_bulk_read(info->max14577->regmap,
499 MAX14577_MUIC_REG_STATUS1, info->status, 2);
500 if (ret) {
501 dev_err(info->dev, "failed to read MUIC register\n");
502 mutex_unlock(&info->mutex);
503 return;
504 }
505
506 if (info->irq_adc) {
507 ret = max14577_muic_adc_handler(info);
508 info->irq_adc = false;
509 }
510 if (info->irq_chg) {
511 ret = max14577_muic_chg_handler(info);
512 info->irq_chg = false;
513 }
514
515 if (ret < 0)
516 dev_err(info->dev, "failed to handle MUIC interrupt\n");
517
518 mutex_unlock(&info->mutex);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100519}
520
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200521/*
522 * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
523 * Returns 0 if irq_type does not match registered IRQ for this device type.
524 */
525static int max14577_parse_irq(struct max14577_muic_info *info, int irq_type)
526{
527 switch (irq_type) {
528 case MAX14577_IRQ_INT1_ADC:
529 case MAX14577_IRQ_INT1_ADCLOW:
530 case MAX14577_IRQ_INT1_ADCERR:
531 /* Handle all of accessory except for
532 type of charger accessory */
533 info->irq_adc = true;
534 return 1;
535 case MAX14577_IRQ_INT2_CHGTYP:
536 case MAX14577_IRQ_INT2_CHGDETRUN:
537 case MAX14577_IRQ_INT2_DCDTMR:
538 case MAX14577_IRQ_INT2_DBCHG:
539 case MAX14577_IRQ_INT2_VBVOLT:
540 /* Handle charger accessory */
541 info->irq_chg = true;
542 return 1;
543 default:
544 return 0;
545 }
546}
547
548/*
549 * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
550 * Returns 0 if irq_type does not match registered IRQ for this device type.
551 */
552static int max77836_parse_irq(struct max14577_muic_info *info, int irq_type)
553{
554 /* First check common max14577 interrupts */
555 if (max14577_parse_irq(info, irq_type))
556 return 1;
557
558 switch (irq_type) {
559 case MAX77836_IRQ_INT1_ADC1K:
560 info->irq_adc = true;
561 return 1;
562 case MAX77836_IRQ_INT2_VIDRM:
563 /* Handle charger accessory */
564 info->irq_chg = true;
565 return 1;
566 default:
567 return 0;
568 }
569}
570
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100571static irqreturn_t max14577_muic_irq_handler(int irq, void *data)
572{
573 struct max14577_muic_info *info = data;
574 int i, irq_type = -1;
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200575 bool irq_parsed;
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100576
577 /*
578 * We may be called multiple times for different nested IRQ-s.
579 * Including changes in INT1_ADC and INT2_CGHTYP at once.
580 * However we only need to know whether it was ADC, charger
581 * or both interrupts so decode IRQ and turn on proper flags.
582 */
Krzysztof Kozlowski0ca852b2014-04-14 11:17:16 +0200583 for (i = 0; i < info->muic_irqs_num; i++)
584 if (irq == info->muic_irqs[i].virq)
585 irq_type = info->muic_irqs[i].irq;
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100586
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200587 switch (info->max14577->dev_type) {
588 case MAXIM_DEVICE_TYPE_MAX77836:
589 irq_parsed = max77836_parse_irq(info, irq_type);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100590 break;
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200591 case MAXIM_DEVICE_TYPE_MAX14577:
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100592 default:
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200593 irq_parsed = max14577_parse_irq(info, irq_type);
594 break;
595 }
596
597 if (!irq_parsed) {
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100598 dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n",
599 irq_type);
600 return IRQ_HANDLED;
601 }
602 schedule_work(&info->irq_work);
603
604 return IRQ_HANDLED;
605}
606
607static int max14577_muic_detect_accessory(struct max14577_muic_info *info)
608{
609 int ret = 0;
610 int adc;
611 int chg_type;
612 bool attached;
613
614 mutex_lock(&info->mutex);
615
616 /* Read STATUSx register to detect accessory */
617 ret = max14577_bulk_read(info->max14577->regmap,
618 MAX14577_MUIC_REG_STATUS1, info->status, 2);
619 if (ret) {
620 dev_err(info->dev, "failed to read MUIC register\n");
621 mutex_unlock(&info->mutex);
622 return ret;
623 }
624
625 adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
626 &attached);
627 if (attached && adc != MAX14577_MUIC_ADC_OPEN) {
628 ret = max14577_muic_adc_handler(info);
629 if (ret < 0) {
630 dev_err(info->dev, "Cannot detect accessory\n");
631 mutex_unlock(&info->mutex);
632 return ret;
633 }
634 }
635
636 chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG,
637 &attached);
638 if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) {
639 ret = max14577_muic_chg_handler(info);
640 if (ret < 0) {
641 dev_err(info->dev, "Cannot detect charger accessory\n");
642 mutex_unlock(&info->mutex);
643 return ret;
644 }
645 }
646
647 mutex_unlock(&info->mutex);
648
649 return 0;
650}
651
652static void max14577_muic_detect_cable_wq(struct work_struct *work)
653{
654 struct max14577_muic_info *info = container_of(to_delayed_work(work),
655 struct max14577_muic_info, wq_detcable);
656
657 max14577_muic_detect_accessory(info);
658}
659
660static int max14577_muic_probe(struct platform_device *pdev)
661{
662 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
663 struct max14577_muic_info *info;
664 int delay_jiffies;
665 int ret;
666 int i;
667 u8 id;
668
669 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Jingoo Han0a16ee62014-07-23 10:07:09 +0900670 if (!info)
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100671 return -ENOMEM;
Jingoo Han0a16ee62014-07-23 10:07:09 +0900672
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100673 info->dev = &pdev->dev;
674 info->max14577 = max14577;
675
676 platform_set_drvdata(pdev, info);
677 mutex_init(&info->mutex);
678
679 INIT_WORK(&info->irq_work, max14577_muic_irq_work);
680
Krzysztof Kozlowski0ca852b2014-04-14 11:17:16 +0200681 switch (max14577->dev_type) {
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200682 case MAXIM_DEVICE_TYPE_MAX77836:
683 info->muic_irqs = max77836_muic_irqs;
684 info->muic_irqs_num = ARRAY_SIZE(max77836_muic_irqs);
685 break;
Krzysztof Kozlowski0ca852b2014-04-14 11:17:16 +0200686 case MAXIM_DEVICE_TYPE_MAX14577:
687 default:
688 info->muic_irqs = max14577_muic_irqs;
689 info->muic_irqs_num = ARRAY_SIZE(max14577_muic_irqs);
690 }
691
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100692 /* Support irq domain for max14577 MUIC device */
Krzysztof Kozlowski0ca852b2014-04-14 11:17:16 +0200693 for (i = 0; i < info->muic_irqs_num; i++) {
694 struct max14577_muic_irq *muic_irq = &info->muic_irqs[i];
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100695 unsigned int virq = 0;
696
697 virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq);
Krzysztof Kozlowski369afd42014-04-18 16:47:30 +0200698 if (virq <= 0)
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100699 return -EINVAL;
700 muic_irq->virq = virq;
701
702 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
703 max14577_muic_irq_handler,
704 IRQF_NO_SUSPEND,
705 muic_irq->name, info);
706 if (ret) {
707 dev_err(&pdev->dev,
Chanwoo Choi34825e52015-03-07 01:41:36 +0900708 "failed: irq request (IRQ: %d, error :%d)\n",
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100709 muic_irq->irq, ret);
710 return ret;
711 }
712 }
713
714 /* Initialize extcon device */
Chanwoo Choi06040022014-04-24 20:15:45 +0900715 info->edev = devm_extcon_dev_allocate(&pdev->dev,
716 max14577_extcon_cable);
717 if (IS_ERR(info->edev)) {
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100718 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
719 return -ENOMEM;
720 }
Chanwoo Choi4005da52014-04-14 11:17:12 +0200721
Sangjung Woo14dbd542014-04-21 19:10:11 +0900722 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100723 if (ret) {
724 dev_err(&pdev->dev, "failed to register extcon device\n");
725 return ret;
726 }
727
728 /* Default h/w line path */
729 info->path_usb = CTRL1_SW_USB;
730 info->path_uart = CTRL1_SW_UART;
731 delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
732
733 /* Set initial path for UART */
734 max14577_muic_set_path(info, info->path_uart, true);
735
736 /* Check revision number of MUIC device*/
737 ret = max14577_read_reg(info->max14577->regmap,
738 MAX14577_REG_DEVICEID, &id);
739 if (ret < 0) {
740 dev_err(&pdev->dev, "failed to read revision number\n");
Sangjung Woo14dbd542014-04-21 19:10:11 +0900741 return ret;
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100742 }
743 dev_info(info->dev, "device ID : 0x%x\n", id);
744
745 /* Set ADC debounce time */
746 max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
747
748 /*
749 * Detect accessory after completing the initialization of platform
750 *
751 * - Use delayed workqueue to detect cable state and then
752 * notify cable state to notifiee/platform through uevent.
753 * After completing the booting of platform, the extcon provider
754 * driver should notify cable state to upper layer.
755 */
756 INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
Krzysztof Kozlowski12adef52014-04-09 11:56:09 +0200757 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100758 delay_jiffies);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100759
760 return ret;
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100761}
762
763static int max14577_muic_remove(struct platform_device *pdev)
764{
765 struct max14577_muic_info *info = platform_get_drvdata(pdev);
766
767 cancel_work_sync(&info->irq_work);
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100768
769 return 0;
770}
771
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200772static const struct platform_device_id max14577_muic_id[] = {
773 { "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, },
774 { "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, },
775 { }
776};
777MODULE_DEVICE_TABLE(platform, max14577_muic_id);
778
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100779static struct platform_driver max14577_muic_driver = {
780 .driver = {
Chanwoo Choi4005da52014-04-14 11:17:12 +0200781 .name = "max14577-muic",
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100782 },
783 .probe = max14577_muic_probe,
784 .remove = max14577_muic_remove,
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200785 .id_table = max14577_muic_id,
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100786};
787
788module_platform_driver(max14577_muic_driver);
789
Krzysztof Kozlowski4706a522014-04-14 11:17:19 +0200790MODULE_DESCRIPTION("Maxim 14577/77836 Extcon driver");
791MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
Chanwoo Choi962e56b2013-11-22 16:51:06 +0100792MODULE_LICENSE("GPL");
793MODULE_ALIAS("platform:extcon-max14577");