2 * max8997-muic.c - MAX8997 muic driver for the Maxim 8997
4 * Copyright (C) 2011 Samsung Electrnoics
5 * Donggeun Kim <dg77.kim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/err.h>
29 #include <linux/platform_device.h>
30 #include <linux/kobject.h>
31 #include <linux/mfd/max8997.h>
32 #include <linux/mfd/max8997-private.h>
34 /* MAX8997-MUIC STATUS1 register */
35 #define STATUS1_ADC_SHIFT 0
36 #define STATUS1_ADCLOW_SHIFT 5
37 #define STATUS1_ADCERR_SHIFT 6
38 #define STATUS1_ADC_MASK (0x1f << STATUS1_ADC_SHIFT)
39 #define STATUS1_ADCLOW_MASK (0x1 << STATUS1_ADCLOW_SHIFT)
40 #define STATUS1_ADCERR_MASK (0x1 << STATUS1_ADCERR_SHIFT)
42 /* MAX8997-MUIC STATUS2 register */
43 #define STATUS2_CHGTYP_SHIFT 0
44 #define STATUS2_CHGDETRUN_SHIFT 3
45 #define STATUS2_DCDTMR_SHIFT 4
46 #define STATUS2_DBCHG_SHIFT 5
47 #define STATUS2_VBVOLT_SHIFT 6
48 #define STATUS2_CHGTYP_MASK (0x7 << STATUS2_CHGTYP_SHIFT)
49 #define STATUS2_CHGDETRUN_MASK (0x1 << STATUS2_CHGDETRUN_SHIFT)
50 #define STATUS2_DCDTMR_MASK (0x1 << STATUS2_DCDTMR_SHIFT)
51 #define STATUS2_DBCHG_MASK (0x1 << STATUS2_DBCHG_SHIFT)
52 #define STATUS2_VBVOLT_MASK (0x1 << STATUS2_VBVOLT_SHIFT)
54 /* MAX8997-MUIC STATUS3 register */
55 #define STATUS3_OVP_SHIFT 2
56 #define STATUS3_OVP_MASK (0x1 << STATUS3_OVP_SHIFT)
58 /* MAX8997-MUIC CONTROL1 register */
59 #define COMN1SW_SHIFT 0
60 #define COMP2SW_SHIFT 3
61 #define COMN1SW_MASK (0x7 << COMN1SW_SHIFT)
62 #define COMP2SW_MASK (0x7 << COMP2SW_SHIFT)
63 #define SW_MASK (COMP2SW_MASK | COMN1SW_MASK)
65 #define MAX8997_SW_USB ((1 << COMP2SW_SHIFT) | (1 << COMN1SW_SHIFT))
66 #define MAX8997_SW_AUDIO ((2 << COMP2SW_SHIFT) | (2 << COMN1SW_SHIFT))
67 #define MAX8997_SW_UART ((3 << COMP2SW_SHIFT) | (3 << COMN1SW_SHIFT))
68 #define MAX8997_SW_OPEN ((0 << COMP2SW_SHIFT) | (0 << COMN1SW_SHIFT))
70 #define MAX8997_ADC_GROUND 0x00
71 #define MAX8997_ADC_MHL 0x01
72 #define MAX8997_ADC_JIG_USB_1 0x18
73 #define MAX8997_ADC_JIG_USB_2 0x19
74 #define MAX8997_ADC_DESKDOCK 0x1a
75 #define MAX8997_ADC_JIG_UART 0x1c
76 #define MAX8997_ADC_CARDOCK 0x1d
77 #define MAX8997_ADC_OPEN 0x1f
79 struct max8997_muic_irq
{
84 static struct max8997_muic_irq muic_irqs
[] = {
85 { MAX8997_MUICIRQ_ADCError
, "muic-ADC_error" },
86 { MAX8997_MUICIRQ_ADCLow
, "muic-ADC_low" },
87 { MAX8997_MUICIRQ_ADC
, "muic-ADC" },
88 { MAX8997_MUICIRQ_VBVolt
, "muic-VB_voltage" },
89 { MAX8997_MUICIRQ_DBChg
, "muic-DB_charger" },
90 { MAX8997_MUICIRQ_DCDTmr
, "muic-DCD_timer" },
91 { MAX8997_MUICIRQ_ChgDetRun
, "muic-CDR_status" },
92 { MAX8997_MUICIRQ_ChgTyp
, "muic-charger_type" },
93 { MAX8997_MUICIRQ_OVP
, "muic-over_voltage" },
96 struct max8997_muic_info
{
98 struct max8997_dev
*iodev
;
99 struct i2c_client
*muic
;
100 struct max8997_muic_platform_data
*muic_pdata
;
103 struct work_struct irq_work
;
105 enum max8997_muic_charger_type pre_charger_type
;
111 static int max8997_muic_handle_usb(struct max8997_muic_info
*info
,
112 enum max8997_muic_usb_type usb_type
, bool attached
)
114 struct max8997_muic_platform_data
*mdata
= info
->muic_pdata
;
117 if (usb_type
== MAX8997_USB_HOST
) {
119 ret
= max8997_update_reg(info
->muic
, MAX8997_MUIC_REG_CONTROL1
,
120 attached
? MAX8997_SW_USB
: MAX8997_SW_OPEN
,
123 dev_err(info
->dev
, "failed to update muic register\n");
128 if (mdata
->usb_callback
)
129 mdata
->usb_callback(usb_type
, attached
);
134 static void max8997_muic_handle_mhl(struct max8997_muic_info
*info
,
137 struct max8997_muic_platform_data
*mdata
= info
->muic_pdata
;
139 if (mdata
->mhl_callback
)
140 mdata
->mhl_callback(attached
);
143 static int max8997_muic_handle_dock(struct max8997_muic_info
*info
,
144 int adc
, bool attached
)
146 struct max8997_muic_platform_data
*mdata
= info
->muic_pdata
;
149 /* switch to AUDIO */
150 ret
= max8997_update_reg(info
->muic
, MAX8997_MUIC_REG_CONTROL1
,
151 attached
? MAX8997_SW_AUDIO
: MAX8997_SW_OPEN
,
154 dev_err(info
->dev
, "failed to update muic register\n");
159 case MAX8997_ADC_DESKDOCK
:
160 if (mdata
->deskdock_callback
)
161 mdata
->deskdock_callback(attached
);
163 case MAX8997_ADC_CARDOCK
:
164 if (mdata
->cardock_callback
)
165 mdata
->cardock_callback(attached
);
174 static int max8997_muic_handle_jig_uart(struct max8997_muic_info
*info
,
177 struct max8997_muic_platform_data
*mdata
= info
->muic_pdata
;
181 ret
= max8997_update_reg(info
->muic
, MAX8997_MUIC_REG_CONTROL1
,
182 attached
? MAX8997_SW_UART
: MAX8997_SW_OPEN
,
185 dev_err(info
->dev
, "failed to update muic register\n");
189 if (mdata
->uart_callback
)
190 mdata
->uart_callback(attached
);
195 static int max8997_muic_handle_adc_detach(struct max8997_muic_info
*info
)
199 switch (info
->pre_adc
) {
200 case MAX8997_ADC_GROUND
:
201 ret
= max8997_muic_handle_usb(info
, MAX8997_USB_HOST
, false);
203 case MAX8997_ADC_MHL
:
204 max8997_muic_handle_mhl(info
, false);
206 case MAX8997_ADC_JIG_USB_1
:
207 case MAX8997_ADC_JIG_USB_2
:
208 ret
= max8997_muic_handle_usb(info
, MAX8997_USB_DEVICE
, false);
210 case MAX8997_ADC_DESKDOCK
:
211 case MAX8997_ADC_CARDOCK
:
212 ret
= max8997_muic_handle_dock(info
, info
->pre_adc
, false);
214 case MAX8997_ADC_JIG_UART
:
215 ret
= max8997_muic_handle_jig_uart(info
, false);
224 static int max8997_muic_handle_adc(struct max8997_muic_info
*info
, int adc
)
229 case MAX8997_ADC_GROUND
:
230 ret
= max8997_muic_handle_usb(info
, MAX8997_USB_HOST
, true);
232 case MAX8997_ADC_MHL
:
233 max8997_muic_handle_mhl(info
, true);
235 case MAX8997_ADC_JIG_USB_1
:
236 case MAX8997_ADC_JIG_USB_2
:
237 ret
= max8997_muic_handle_usb(info
, MAX8997_USB_DEVICE
, true);
239 case MAX8997_ADC_DESKDOCK
:
240 case MAX8997_ADC_CARDOCK
:
241 ret
= max8997_muic_handle_dock(info
, adc
, true);
243 case MAX8997_ADC_JIG_UART
:
244 ret
= max8997_muic_handle_jig_uart(info
, true);
246 case MAX8997_ADC_OPEN
:
247 ret
= max8997_muic_handle_adc_detach(info
);
258 static int max8997_muic_handle_charger_type(struct max8997_muic_info
*info
,
259 enum max8997_muic_charger_type charger_type
)
261 struct max8997_muic_platform_data
*mdata
= info
->muic_pdata
;
265 ret
= max8997_read_reg(info
->muic
, MAX8997_MUIC_REG_STATUS1
, &adc
);
267 dev_err(info
->dev
, "failed to read muic register\n");
271 switch (charger_type
) {
272 case MAX8997_CHARGER_TYPE_NONE
:
273 if (mdata
->charger_callback
)
274 mdata
->charger_callback(false, charger_type
);
275 if (info
->pre_charger_type
== MAX8997_CHARGER_TYPE_USB
) {
276 max8997_muic_handle_usb(info
,
277 MAX8997_USB_DEVICE
, false);
280 case MAX8997_CHARGER_TYPE_USB
:
281 if ((adc
& STATUS1_ADC_MASK
) == MAX8997_ADC_OPEN
) {
282 max8997_muic_handle_usb(info
,
283 MAX8997_USB_DEVICE
, true);
285 if (mdata
->charger_callback
)
286 mdata
->charger_callback(true, charger_type
);
288 case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT
:
289 case MAX8997_CHARGER_TYPE_DEDICATED_CHG
:
290 case MAX8997_CHARGER_TYPE_500MA
:
291 case MAX8997_CHARGER_TYPE_1A
:
292 if (mdata
->charger_callback
)
293 mdata
->charger_callback(true, charger_type
);
299 info
->pre_charger_type
= charger_type
;
304 static void max8997_muic_irq_work(struct work_struct
*work
)
306 struct max8997_muic_info
*info
= container_of(work
,
307 struct max8997_muic_info
, irq_work
);
308 struct max8997_platform_data
*pdata
=
309 dev_get_platdata(info
->iodev
->dev
);
313 int irq_type
= info
->irq
- pdata
->irq_base
;
316 mutex_lock(&info
->mutex
);
318 ret
= max8997_bulk_read(info
->muic
, MAX8997_MUIC_REG_STATUS1
,
321 dev_err(info
->dev
, "failed to read muic register\n");
322 mutex_unlock(&info
->mutex
);
326 dev_dbg(info
->dev
, "%s: STATUS1:0x%x, 2:0x%x\n", __func__
,
327 status
[0], status
[1]);
330 case MAX8997_MUICIRQ_ADC
:
331 adc
= status
[0] & STATUS1_ADC_MASK
;
332 adc
>>= STATUS1_ADC_SHIFT
;
334 max8997_muic_handle_adc(info
, adc
);
336 case MAX8997_MUICIRQ_ChgTyp
:
337 chg_type
= status
[1] & STATUS2_CHGTYP_MASK
;
338 chg_type
>>= STATUS2_CHGTYP_SHIFT
;
340 max8997_muic_handle_charger_type(info
, chg_type
);
343 dev_info(info
->dev
, "misc interrupt: %s occurred\n",
344 muic_irqs
[irq_type
].name
);
348 mutex_unlock(&info
->mutex
);
353 static irqreturn_t
max8997_muic_irq_handler(int irq
, void *data
)
355 struct max8997_muic_info
*info
= data
;
357 dev_dbg(info
->dev
, "irq:%d\n", irq
);
360 schedule_work(&info
->irq_work
);
365 static void max8997_muic_detect_dev(struct max8997_muic_info
*info
)
368 u8 status
[2], adc
, chg_type
;
370 ret
= max8997_bulk_read(info
->muic
, MAX8997_MUIC_REG_STATUS1
,
373 dev_err(info
->dev
, "failed to read muic register\n");
377 dev_info(info
->dev
, "STATUS1:0x%x, STATUS2:0x%x\n",
378 status
[0], status
[1]);
380 adc
= status
[0] & STATUS1_ADC_MASK
;
381 adc
>>= STATUS1_ADC_SHIFT
;
383 chg_type
= status
[1] & STATUS2_CHGTYP_MASK
;
384 chg_type
>>= STATUS2_CHGTYP_SHIFT
;
386 max8997_muic_handle_adc(info
, adc
);
387 max8997_muic_handle_charger_type(info
, chg_type
);
390 static void max8997_initialize_device(struct max8997_muic_info
*info
)
392 struct max8997_muic_platform_data
*mdata
= info
->muic_pdata
;
395 for (i
= 0; i
< mdata
->num_init_data
; i
++) {
396 max8997_write_reg(info
->muic
, mdata
->init_data
[i
].addr
,
397 mdata
->init_data
[i
].data
);
401 static int __devinit
max8997_muic_probe(struct platform_device
*pdev
)
403 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
404 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
405 struct max8997_muic_info
*info
;
408 info
= kzalloc(sizeof(struct max8997_muic_info
), GFP_KERNEL
);
410 dev_err(&pdev
->dev
, "failed to allocate memory\n");
415 if (!pdata
->muic_pdata
) {
416 dev_err(&pdev
->dev
, "failed to get platform_data\n");
420 info
->muic_pdata
= pdata
->muic_pdata
;
422 info
->dev
= &pdev
->dev
;
424 info
->muic
= iodev
->muic
;
426 platform_set_drvdata(pdev
, info
);
427 mutex_init(&info
->mutex
);
429 INIT_WORK(&info
->irq_work
, max8997_muic_irq_work
);
431 for (i
= 0; i
< ARRAY_SIZE(muic_irqs
); i
++) {
432 struct max8997_muic_irq
*muic_irq
= &muic_irqs
[i
];
434 ret
= request_threaded_irq(pdata
->irq_base
+ muic_irq
->irq
,
435 NULL
, max8997_muic_irq_handler
,
440 "failed: irq request (IRQ: %d,"
444 for (i
= i
- 1; i
>= 0; i
--)
445 free_irq(muic_irq
->irq
, info
);
451 /* Initialize registers according to platform data */
452 max8997_initialize_device(info
);
454 /* Initial device detection */
455 max8997_muic_detect_dev(info
);
466 static int __devexit
max8997_muic_remove(struct platform_device
*pdev
)
468 struct max8997_muic_info
*info
= platform_get_drvdata(pdev
);
469 struct max8997_platform_data
*pdata
=
470 dev_get_platdata(info
->iodev
->dev
);
473 for (i
= 0; i
< ARRAY_SIZE(muic_irqs
); i
++)
474 free_irq(pdata
->irq_base
+ muic_irqs
[i
].irq
, info
);
475 cancel_work_sync(&info
->irq_work
);
482 static struct platform_driver max8997_muic_driver
= {
484 .name
= "max8997-muic",
485 .owner
= THIS_MODULE
,
487 .probe
= max8997_muic_probe
,
488 .remove
= __devexit_p(max8997_muic_remove
),
491 static int __init
max8997_muic_init(void)
493 return platform_driver_register(&max8997_muic_driver
);
495 module_init(max8997_muic_init
);
497 static void __exit
max8997_muic_exit(void)
499 platform_driver_unregister(&max8997_muic_driver
);
501 module_exit(max8997_muic_exit
);
503 MODULE_DESCRIPTION("Maxim MAX8997 MUIC driver");
504 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
505 MODULE_LICENSE("GPL");