]>
Commit | Line | Data |
---|---|---|
db1b9037 CC |
1 | /* |
2 | * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC | |
3 | * | |
4 | * Copyright (C) 2012 Samsung Electrnoics | |
5 | * Chanwoo Choi <cw00.choi@samsung.com> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | */ | |
17 | ||
18 | #include <linux/kernel.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/i2c.h> | |
21 | #include <linux/slab.h> | |
39bf369e | 22 | #include <linux/input.h> |
db1b9037 CC |
23 | #include <linux/interrupt.h> |
24 | #include <linux/err.h> | |
25 | #include <linux/platform_device.h> | |
26 | #include <linux/mfd/max77693.h> | |
27 | #include <linux/mfd/max77693-private.h> | |
28 | #include <linux/extcon.h> | |
29 | #include <linux/regmap.h> | |
30 | #include <linux/irqdomain.h> | |
31 | ||
32 | #define DEV_NAME "max77693-muic" | |
297620fd | 33 | #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */ |
db1b9037 | 34 | |
db1b9037 CC |
35 | enum max77693_muic_adc_debounce_time { |
36 | ADC_DEBOUNCE_TIME_5MS = 0, | |
37 | ADC_DEBOUNCE_TIME_10MS, | |
38 | ADC_DEBOUNCE_TIME_25MS, | |
39 | ADC_DEBOUNCE_TIME_38_62MS, | |
40 | }; | |
41 | ||
42 | struct max77693_muic_info { | |
43 | struct device *dev; | |
44 | struct max77693_dev *max77693; | |
45 | struct extcon_dev *edev; | |
154f757f CC |
46 | int prev_cable_type; |
47 | int prev_cable_type_gnd; | |
db1b9037 | 48 | int prev_chg_type; |
39bf369e | 49 | int prev_button_type; |
db1b9037 CC |
50 | u8 status[2]; |
51 | ||
52 | int irq; | |
53 | struct work_struct irq_work; | |
54 | struct mutex mutex; | |
39bf369e | 55 | |
297620fd CC |
56 | /* |
57 | * Use delayed workqueue to detect cable state and then | |
58 | * notify cable state to notifiee/platform through uevent. | |
59 | * After completing the booting of platform, the extcon provider | |
60 | * driver should notify cable state to upper layer. | |
61 | */ | |
62 | struct delayed_work wq_detcable; | |
63 | ||
39bf369e CC |
64 | /* Button of dock device */ |
65 | struct input_dev *dock; | |
2b75799f CC |
66 | |
67 | /* | |
68 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB | |
69 | * h/w path of COMP2/COMN1 on CONTROL1 register. | |
70 | */ | |
71 | int path_usb; | |
72 | int path_uart; | |
db1b9037 CC |
73 | }; |
74 | ||
154f757f CC |
75 | enum max77693_muic_cable_group { |
76 | MAX77693_CABLE_GROUP_ADC = 0, | |
77 | MAX77693_CABLE_GROUP_ADC_GND, | |
78 | MAX77693_CABLE_GROUP_CHG, | |
79 | MAX77693_CABLE_GROUP_VBVOLT, | |
80 | }; | |
81 | ||
db1b9037 CC |
82 | enum max77693_muic_charger_type { |
83 | MAX77693_CHARGER_TYPE_NONE = 0, | |
84 | MAX77693_CHARGER_TYPE_USB, | |
85 | MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT, | |
86 | MAX77693_CHARGER_TYPE_DEDICATED_CHG, | |
87 | MAX77693_CHARGER_TYPE_APPLE_500MA, | |
88 | MAX77693_CHARGER_TYPE_APPLE_1A_2A, | |
89 | MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7, | |
90 | }; | |
91 | ||
92 | /** | |
93 | * struct max77693_muic_irq | |
94 | * @irq: the index of irq list of MUIC device. | |
95 | * @name: the name of irq. | |
96 | * @virq: the virtual irq to use irq domain | |
97 | */ | |
98 | struct max77693_muic_irq { | |
99 | unsigned int irq; | |
100 | const char *name; | |
101 | unsigned int virq; | |
102 | }; | |
103 | ||
104 | static struct max77693_muic_irq muic_irqs[] = { | |
105 | { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" }, | |
106 | { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" }, | |
107 | { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" }, | |
108 | { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" }, | |
109 | { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" }, | |
110 | { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" }, | |
111 | { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" }, | |
112 | { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" }, | |
113 | { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" }, | |
114 | { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" }, | |
115 | { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" }, | |
116 | { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" }, | |
117 | { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" }, | |
118 | { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" }, | |
119 | { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" }, | |
120 | { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" }, | |
121 | }; | |
122 | ||
123 | /* Define supported accessory type */ | |
124 | enum max77693_muic_acc_type { | |
125 | MAX77693_MUIC_ADC_GROUND = 0x0, | |
126 | MAX77693_MUIC_ADC_SEND_END_BUTTON, | |
127 | MAX77693_MUIC_ADC_REMOTE_S1_BUTTON, | |
128 | MAX77693_MUIC_ADC_REMOTE_S2_BUTTON, | |
129 | MAX77693_MUIC_ADC_REMOTE_S3_BUTTON, | |
130 | MAX77693_MUIC_ADC_REMOTE_S4_BUTTON, | |
131 | MAX77693_MUIC_ADC_REMOTE_S5_BUTTON, | |
132 | MAX77693_MUIC_ADC_REMOTE_S6_BUTTON, | |
133 | MAX77693_MUIC_ADC_REMOTE_S7_BUTTON, | |
134 | MAX77693_MUIC_ADC_REMOTE_S8_BUTTON, | |
135 | MAX77693_MUIC_ADC_REMOTE_S9_BUTTON, | |
136 | MAX77693_MUIC_ADC_REMOTE_S10_BUTTON, | |
137 | MAX77693_MUIC_ADC_REMOTE_S11_BUTTON, | |
138 | MAX77693_MUIC_ADC_REMOTE_S12_BUTTON, | |
139 | MAX77693_MUIC_ADC_RESERVED_ACC_1, | |
140 | MAX77693_MUIC_ADC_RESERVED_ACC_2, | |
141 | MAX77693_MUIC_ADC_RESERVED_ACC_3, | |
142 | MAX77693_MUIC_ADC_RESERVED_ACC_4, | |
143 | MAX77693_MUIC_ADC_RESERVED_ACC_5, | |
144 | MAX77693_MUIC_ADC_CEA936_AUDIO, | |
145 | MAX77693_MUIC_ADC_PHONE_POWERED_DEV, | |
146 | MAX77693_MUIC_ADC_TTY_CONVERTER, | |
147 | MAX77693_MUIC_ADC_UART_CABLE, | |
148 | MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG, | |
149 | MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF, | |
150 | MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON, | |
151 | MAX77693_MUIC_ADC_AV_CABLE_NOLOAD, | |
152 | MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG, | |
153 | MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF, | |
154 | MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON, | |
155 | MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE, | |
156 | MAX77693_MUIC_ADC_OPEN, | |
157 | ||
158 | /* The below accessories have same ADC value so ADCLow and | |
159 | ADC1K bit is used to separate specific accessory */ | |
06bed0af CC |
160 | MAX77693_MUIC_GND_USB_OTG = 0x100, /* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */ |
161 | MAX77693_MUIC_GND_USB_OTG_VB = 0x104, /* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */ | |
162 | MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */ | |
163 | MAX77693_MUIC_GND_MHL = 0x103, /* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */ | |
164 | MAX77693_MUIC_GND_MHL_VB = 0x107, /* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */ | |
db1b9037 CC |
165 | }; |
166 | ||
167 | /* MAX77693 MUIC device support below list of accessories(external connector) */ | |
154f757f CC |
168 | enum { |
169 | EXTCON_CABLE_USB = 0, | |
170 | EXTCON_CABLE_USB_HOST, | |
171 | EXTCON_CABLE_TA, | |
172 | EXTCON_CABLE_FAST_CHARGER, | |
173 | EXTCON_CABLE_SLOW_CHARGER, | |
174 | EXTCON_CABLE_CHARGE_DOWNSTREAM, | |
175 | EXTCON_CABLE_MHL, | |
06bed0af | 176 | EXTCON_CABLE_MHL_TA, |
d0587eb7 CC |
177 | EXTCON_CABLE_JIG_USB_ON, |
178 | EXTCON_CABLE_JIG_USB_OFF, | |
179 | EXTCON_CABLE_JIG_UART_OFF, | |
39bf369e CC |
180 | EXTCON_CABLE_JIG_UART_ON, |
181 | EXTCON_CABLE_DOCK_SMART, | |
182 | EXTCON_CABLE_DOCK_DESK, | |
183 | EXTCON_CABLE_DOCK_AUDIO, | |
154f757f CC |
184 | |
185 | _EXTCON_CABLE_NUM, | |
186 | }; | |
db1b9037 | 187 | |
154f757f CC |
188 | const char *max77693_extcon_cable[] = { |
189 | [EXTCON_CABLE_USB] = "USB", | |
190 | [EXTCON_CABLE_USB_HOST] = "USB-Host", | |
191 | [EXTCON_CABLE_TA] = "TA", | |
192 | [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger", | |
193 | [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger", | |
194 | [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream", | |
195 | [EXTCON_CABLE_MHL] = "MHL", | |
06bed0af | 196 | [EXTCON_CABLE_MHL_TA] = "MHL_TA", |
d0587eb7 CC |
197 | [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON", |
198 | [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF", | |
199 | [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF", | |
39bf369e CC |
200 | [EXTCON_CABLE_JIG_UART_ON] = "Dock-Car", |
201 | [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart", | |
202 | [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk", | |
203 | [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio", | |
d0587eb7 | 204 | |
db1b9037 CC |
205 | NULL, |
206 | }; | |
207 | ||
154f757f CC |
208 | /* |
209 | * max77693_muic_set_debounce_time - Set the debounce time of ADC | |
210 | * @info: the instance including private data of max77693 MUIC | |
211 | * @time: the debounce time of ADC | |
212 | */ | |
db1b9037 CC |
213 | static int max77693_muic_set_debounce_time(struct max77693_muic_info *info, |
214 | enum max77693_muic_adc_debounce_time time) | |
215 | { | |
bf2627d6 | 216 | int ret; |
db1b9037 CC |
217 | |
218 | switch (time) { | |
219 | case ADC_DEBOUNCE_TIME_5MS: | |
220 | case ADC_DEBOUNCE_TIME_10MS: | |
221 | case ADC_DEBOUNCE_TIME_25MS: | |
222 | case ADC_DEBOUNCE_TIME_38_62MS: | |
bf2627d6 AL |
223 | ret = max77693_update_reg(info->max77693->regmap_muic, |
224 | MAX77693_MUIC_REG_CTRL3, | |
225 | time << CONTROL3_ADCDBSET_SHIFT, | |
226 | CONTROL3_ADCDBSET_MASK); | |
227 | if (ret) | |
db1b9037 | 228 | dev_err(info->dev, "failed to set ADC debounce time\n"); |
db1b9037 CC |
229 | break; |
230 | default: | |
231 | dev_err(info->dev, "invalid ADC debounce time\n"); | |
232 | ret = -EINVAL; | |
233 | break; | |
234 | } | |
235 | ||
236 | return ret; | |
237 | }; | |
238 | ||
154f757f CC |
239 | /* |
240 | * max77693_muic_set_path - Set hardware line according to attached cable | |
241 | * @info: the instance including private data of max77693 MUIC | |
242 | * @value: the path according to attached cable | |
243 | * @attached: the state of cable (true:attached, false:detached) | |
244 | * | |
245 | * The max77693 MUIC device share outside H/W line among a varity of cables | |
246 | * so, this function set internal path of H/W line according to the type of | |
247 | * attached cable. | |
248 | */ | |
db1b9037 CC |
249 | static int max77693_muic_set_path(struct max77693_muic_info *info, |
250 | u8 val, bool attached) | |
251 | { | |
252 | int ret = 0; | |
253 | u8 ctrl1, ctrl2 = 0; | |
254 | ||
255 | if (attached) | |
256 | ctrl1 = val; | |
257 | else | |
258 | ctrl1 = CONTROL1_SW_OPEN; | |
259 | ||
260 | ret = max77693_update_reg(info->max77693->regmap_muic, | |
261 | MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK); | |
262 | if (ret < 0) { | |
263 | dev_err(info->dev, "failed to update MUIC register\n"); | |
264 | goto out; | |
265 | } | |
266 | ||
267 | if (attached) | |
268 | ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */ | |
269 | else | |
270 | ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */ | |
271 | ||
272 | ret = max77693_update_reg(info->max77693->regmap_muic, | |
273 | MAX77693_MUIC_REG_CTRL2, ctrl2, | |
274 | CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK); | |
275 | if (ret < 0) { | |
276 | dev_err(info->dev, "failed to update MUIC register\n"); | |
277 | goto out; | |
278 | } | |
279 | ||
280 | dev_info(info->dev, | |
281 | "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", | |
282 | ctrl1, ctrl2, attached ? "attached" : "detached"); | |
283 | out: | |
284 | return ret; | |
285 | } | |
286 | ||
154f757f CC |
287 | /* |
288 | * max77693_muic_get_cable_type - Return cable type and check cable state | |
289 | * @info: the instance including private data of max77693 MUIC | |
290 | * @group: the path according to attached cable | |
291 | * @attached: store cable state and return | |
292 | * | |
293 | * This function check the cable state either attached or detached, | |
294 | * and then divide precise type of cable according to cable group. | |
295 | * - MAX77693_CABLE_GROUP_ADC | |
296 | * - MAX77693_CABLE_GROUP_ADC_GND | |
297 | * - MAX77693_CABLE_GROUP_CHG | |
298 | * - MAX77693_CABLE_GROUP_VBVOLT | |
299 | */ | |
300 | static int max77693_muic_get_cable_type(struct max77693_muic_info *info, | |
301 | enum max77693_muic_cable_group group, bool *attached) | |
db1b9037 | 302 | { |
154f757f CC |
303 | int cable_type = 0; |
304 | int adc; | |
305 | int adc1k; | |
306 | int adclow; | |
307 | int vbvolt; | |
308 | int chg_type; | |
309 | ||
310 | switch (group) { | |
311 | case MAX77693_CABLE_GROUP_ADC: | |
312 | /* | |
313 | * Read ADC value to check cable type and decide cable state | |
314 | * according to cable type | |
315 | */ | |
316 | adc = info->status[0] & STATUS1_ADC_MASK; | |
317 | adc >>= STATUS1_ADC_SHIFT; | |
318 | ||
319 | /* | |
320 | * Check current cable state/cable type and store cable type | |
321 | * (info->prev_cable_type) for handling cable when cable is | |
322 | * detached. | |
323 | */ | |
324 | if (adc == MAX77693_MUIC_ADC_OPEN) { | |
325 | *attached = false; | |
326 | ||
327 | cable_type = info->prev_cable_type; | |
328 | info->prev_cable_type = MAX77693_MUIC_ADC_OPEN; | |
329 | } else { | |
330 | *attached = true; | |
331 | ||
332 | cable_type = info->prev_cable_type = adc; | |
333 | } | |
334 | break; | |
335 | case MAX77693_CABLE_GROUP_ADC_GND: | |
336 | /* | |
337 | * Read ADC value to check cable type and decide cable state | |
338 | * according to cable type | |
339 | */ | |
340 | adc = info->status[0] & STATUS1_ADC_MASK; | |
341 | adc >>= STATUS1_ADC_SHIFT; | |
db1b9037 | 342 | |
154f757f CC |
343 | /* |
344 | * Check current cable state/cable type and store cable type | |
345 | * (info->prev_cable_type/_gnd) for handling cable when cable | |
346 | * is detached. | |
347 | */ | |
348 | if (adc == MAX77693_MUIC_ADC_OPEN) { | |
349 | *attached = false; | |
350 | ||
351 | cable_type = info->prev_cable_type_gnd; | |
352 | info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN; | |
353 | } else { | |
354 | *attached = true; | |
355 | ||
356 | adclow = info->status[0] & STATUS1_ADCLOW_MASK; | |
357 | adclow >>= STATUS1_ADCLOW_SHIFT; | |
358 | adc1k = info->status[0] & STATUS1_ADC1K_MASK; | |
359 | adc1k >>= STATUS1_ADC1K_SHIFT; | |
360 | ||
361 | vbvolt = info->status[1] & STATUS2_VBVOLT_MASK; | |
362 | vbvolt >>= STATUS2_VBVOLT_SHIFT; | |
363 | ||
364 | /** | |
365 | * [0x1][VBVolt][ADCLow][ADC1K] | |
366 | * [0x1 0 0 0 ] : USB_OTG | |
06bed0af | 367 | * [0x1 1 0 0 ] : USB_OTG_VB |
154f757f CC |
368 | * [0x1 0 1 0 ] : Audio Video Cable with load |
369 | * [0x1 0 1 1 ] : MHL without charging connector | |
370 | * [0x1 1 1 1 ] : MHL with charging connector | |
371 | */ | |
372 | cable_type = ((0x1 << 8) | |
373 | | (vbvolt << 2) | |
374 | | (adclow << 1) | |
375 | | adc1k); | |
376 | ||
377 | info->prev_cable_type = adc; | |
378 | info->prev_cable_type_gnd = cable_type; | |
379 | } | |
380 | ||
381 | break; | |
382 | case MAX77693_CABLE_GROUP_CHG: | |
383 | /* | |
384 | * Read charger type to check cable type and decide cable state | |
385 | * according to type of charger cable. | |
386 | */ | |
387 | chg_type = info->status[1] & STATUS2_CHGTYP_MASK; | |
388 | chg_type >>= STATUS2_CHGTYP_SHIFT; | |
389 | ||
390 | if (chg_type == MAX77693_CHARGER_TYPE_NONE) { | |
391 | *attached = false; | |
392 | ||
393 | cable_type = info->prev_chg_type; | |
394 | info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE; | |
395 | } else { | |
396 | *attached = true; | |
397 | ||
398 | /* | |
399 | * Check current cable state/cable type and store cable | |
400 | * type(info->prev_chg_type) for handling cable when | |
401 | * charger cable is detached. | |
402 | */ | |
403 | cable_type = info->prev_chg_type = chg_type; | |
404 | } | |
405 | ||
406 | break; | |
407 | case MAX77693_CABLE_GROUP_VBVOLT: | |
408 | /* | |
409 | * Read ADC value to check cable type and decide cable state | |
410 | * according to cable type | |
411 | */ | |
db1b9037 | 412 | adc = info->status[0] & STATUS1_ADC_MASK; |
154f757f CC |
413 | adc >>= STATUS1_ADC_SHIFT; |
414 | chg_type = info->status[1] & STATUS2_CHGTYP_MASK; | |
415 | chg_type >>= STATUS2_CHGTYP_SHIFT; | |
416 | ||
417 | if (adc == MAX77693_MUIC_ADC_OPEN | |
418 | && chg_type == MAX77693_CHARGER_TYPE_NONE) | |
419 | *attached = false; | |
420 | else | |
421 | *attached = true; | |
422 | ||
423 | /* | |
424 | * Read vbvolt field, if vbvolt is 1, | |
425 | * this cable is used for charging. | |
db1b9037 | 426 | */ |
154f757f CC |
427 | vbvolt = info->status[1] & STATUS2_VBVOLT_MASK; |
428 | vbvolt >>= STATUS2_VBVOLT_SHIFT; | |
429 | ||
430 | cable_type = vbvolt; | |
431 | break; | |
432 | default: | |
433 | dev_err(info->dev, "Unknown cable group (%d)\n", group); | |
434 | cable_type = -EINVAL; | |
435 | break; | |
436 | } | |
437 | ||
438 | return cable_type; | |
439 | } | |
440 | ||
39bf369e CC |
441 | static int max77693_muic_dock_handler(struct max77693_muic_info *info, |
442 | int cable_type, bool attached) | |
443 | { | |
444 | int ret = 0; | |
445 | char dock_name[CABLE_NAME_MAX]; | |
446 | ||
447 | dev_info(info->dev, | |
448 | "external connector is %s (adc:0x%02x)\n", | |
449 | attached ? "attached" : "detached", cable_type); | |
450 | ||
451 | switch (cable_type) { | |
452 | case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */ | |
453 | /* PATH:AP_USB */ | |
454 | ret = max77693_muic_set_path(info, | |
455 | CONTROL1_SW_USB, attached); | |
456 | if (ret < 0) | |
457 | goto out; | |
458 | ||
459 | /* Dock-Smart */ | |
460 | extcon_set_cable_state(info->edev, "Dock-Smart", attached); | |
461 | goto out; | |
462 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */ | |
463 | strcpy(dock_name, "Dock-Car"); | |
464 | break; | |
465 | case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */ | |
466 | strcpy(dock_name, "Dock-Desk"); | |
467 | break; | |
468 | case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */ | |
469 | strcpy(dock_name, "Dock-Audio"); | |
470 | if (!attached) | |
471 | extcon_set_cable_state(info->edev, "USB", false); | |
472 | break; | |
473 | } | |
474 | ||
475 | /* Dock-Car/Desk/Audio, PATH:AUDIO */ | |
476 | ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached); | |
477 | if (ret < 0) | |
478 | goto out; | |
479 | extcon_set_cable_state(info->edev, dock_name, attached); | |
480 | ||
481 | out: | |
482 | return ret; | |
483 | } | |
484 | ||
485 | static int max77693_muic_dock_button_handler(struct max77693_muic_info *info, | |
486 | int button_type, bool attached) | |
487 | { | |
488 | struct input_dev *dock = info->dock; | |
489 | unsigned int code; | |
490 | int ret = 0; | |
491 | ||
492 | switch (button_type) { | |
493 | case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1 | |
494 | ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1: | |
495 | /* DOCK_KEY_PREV */ | |
496 | code = KEY_PREVIOUSSONG; | |
497 | break; | |
498 | case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1 | |
499 | ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1: | |
500 | /* DOCK_KEY_NEXT */ | |
501 | code = KEY_NEXTSONG; | |
502 | break; | |
503 | case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: | |
504 | /* DOCK_VOL_DOWN */ | |
505 | code = KEY_VOLUMEDOWN; | |
506 | break; | |
507 | case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: | |
508 | /* DOCK_VOL_UP */ | |
509 | code = KEY_VOLUMEUP; | |
510 | break; | |
511 | case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1 | |
512 | ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1: | |
513 | /* DOCK_KEY_PLAY_PAUSE */ | |
514 | code = KEY_PLAYPAUSE; | |
515 | break; | |
516 | default: | |
517 | dev_err(info->dev, | |
518 | "failed to detect %s key (adc:0x%x)\n", | |
519 | attached ? "pressed" : "released", button_type); | |
520 | ret = -EINVAL; | |
521 | goto out; | |
522 | } | |
523 | ||
524 | input_event(dock, EV_KEY, code, attached); | |
525 | input_sync(dock); | |
526 | ||
527 | out: | |
528 | return 0; | |
529 | } | |
530 | ||
154f757f CC |
531 | static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info) |
532 | { | |
533 | int cable_type_gnd; | |
534 | int ret = 0; | |
535 | bool attached; | |
db1b9037 | 536 | |
154f757f CC |
537 | cable_type_gnd = max77693_muic_get_cable_type(info, |
538 | MAX77693_CABLE_GROUP_ADC_GND, &attached); | |
db1b9037 | 539 | |
154f757f | 540 | switch (cable_type_gnd) { |
db1b9037 | 541 | case MAX77693_MUIC_GND_USB_OTG: |
06bed0af CC |
542 | case MAX77693_MUIC_GND_USB_OTG_VB: |
543 | /* USB_OTG, PATH: AP_USB */ | |
db1b9037 CC |
544 | ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached); |
545 | if (ret < 0) | |
546 | goto out; | |
547 | extcon_set_cable_state(info->edev, "USB-Host", attached); | |
548 | break; | |
549 | case MAX77693_MUIC_GND_AV_CABLE_LOAD: | |
06bed0af | 550 | /* Audio Video Cable with load, PATH:AUDIO */ |
db1b9037 CC |
551 | ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached); |
552 | if (ret < 0) | |
553 | goto out; | |
554 | extcon_set_cable_state(info->edev, | |
555 | "Audio-video-load", attached); | |
556 | break; | |
06bed0af CC |
557 | case MAX77693_MUIC_GND_MHL: |
558 | case MAX77693_MUIC_GND_MHL_VB: | |
559 | /* MHL or MHL with USB/TA cable */ | |
db1b9037 CC |
560 | extcon_set_cable_state(info->edev, "MHL", attached); |
561 | break; | |
562 | default: | |
afcfaa87 | 563 | dev_err(info->dev, "failed to detect %s accessory\n", |
db1b9037 | 564 | attached ? "attached" : "detached"); |
db1b9037 CC |
565 | ret = -EINVAL; |
566 | break; | |
567 | } | |
568 | ||
569 | out: | |
570 | return ret; | |
571 | } | |
572 | ||
d0587eb7 CC |
573 | static int max77693_muic_jig_handler(struct max77693_muic_info *info, |
574 | int cable_type, bool attached) | |
575 | { | |
576 | char cable_name[32]; | |
577 | int ret = 0; | |
578 | u8 path = CONTROL1_SW_OPEN; | |
579 | ||
580 | dev_info(info->dev, | |
581 | "external connector is %s (adc:0x%02x)\n", | |
582 | attached ? "attached" : "detached", cable_type); | |
583 | ||
584 | switch (cable_type) { | |
585 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */ | |
586 | /* PATH:AP_USB */ | |
587 | strcpy(cable_name, "JIG-USB-OFF"); | |
588 | path = CONTROL1_SW_USB; | |
589 | break; | |
590 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */ | |
591 | /* PATH:AP_USB */ | |
592 | strcpy(cable_name, "JIG-USB-ON"); | |
593 | path = CONTROL1_SW_USB; | |
594 | break; | |
595 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */ | |
596 | /* PATH:AP_UART */ | |
597 | strcpy(cable_name, "JIG-UART-OFF"); | |
598 | path = CONTROL1_SW_UART; | |
599 | break; | |
600 | } | |
601 | ||
602 | ret = max77693_muic_set_path(info, path, attached); | |
603 | if (ret < 0) | |
604 | goto out; | |
605 | ||
606 | extcon_set_cable_state(info->edev, cable_name, attached); | |
607 | out: | |
608 | return ret; | |
609 | } | |
610 | ||
154f757f | 611 | static int max77693_muic_adc_handler(struct max77693_muic_info *info) |
db1b9037 | 612 | { |
154f757f | 613 | int cable_type; |
39bf369e | 614 | int button_type; |
154f757f | 615 | bool attached; |
db1b9037 | 616 | int ret = 0; |
db1b9037 | 617 | |
154f757f CC |
618 | /* Check accessory state which is either detached or attached */ |
619 | cable_type = max77693_muic_get_cable_type(info, | |
620 | MAX77693_CABLE_GROUP_ADC, &attached); | |
db1b9037 CC |
621 | |
622 | dev_info(info->dev, | |
623 | "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", | |
154f757f CC |
624 | attached ? "attached" : "detached", cable_type, |
625 | info->prev_cable_type); | |
db1b9037 | 626 | |
154f757f | 627 | switch (cable_type) { |
db1b9037 CC |
628 | case MAX77693_MUIC_ADC_GROUND: |
629 | /* USB_OTG/MHL/Audio */ | |
154f757f | 630 | max77693_muic_adc_ground_handler(info); |
db1b9037 CC |
631 | break; |
632 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: | |
633 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: | |
db1b9037 | 634 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: |
db1b9037 | 635 | /* JIG */ |
d0587eb7 | 636 | ret = max77693_muic_jig_handler(info, cable_type, attached); |
db1b9037 CC |
637 | if (ret < 0) |
638 | goto out; | |
db1b9037 | 639 | break; |
39bf369e CC |
640 | case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */ |
641 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */ | |
642 | case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */ | |
643 | case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */ | |
644 | /* | |
645 | * DOCK device | |
646 | * | |
647 | * The MAX77693 MUIC device can detect total 34 cable type | |
648 | * except of charger cable and MUIC device didn't define | |
649 | * specfic role of cable in the range of from 0x01 to 0x12 | |
650 | * of ADC value. So, can use/define cable with no role according | |
651 | * to schema of hardware board. | |
652 | */ | |
653 | ret = max77693_muic_dock_handler(info, cable_type, attached); | |
654 | if (ret < 0) | |
655 | goto out; | |
656 | break; | |
657 | case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */ | |
658 | case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */ | |
659 | case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */ | |
660 | case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */ | |
661 | case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */ | |
662 | /* | |
663 | * Button of DOCK device | |
664 | * - the Prev/Next/Volume Up/Volume Down/Play-Pause button | |
665 | * | |
666 | * The MAX77693 MUIC device can detect total 34 cable type | |
667 | * except of charger cable and MUIC device didn't define | |
668 | * specfic role of cable in the range of from 0x01 to 0x12 | |
669 | * of ADC value. So, can use/define cable with no role according | |
670 | * to schema of hardware board. | |
671 | */ | |
672 | if (attached) | |
673 | button_type = info->prev_button_type = cable_type; | |
674 | else | |
675 | button_type = info->prev_button_type; | |
676 | ||
677 | ret = max77693_muic_dock_button_handler(info, button_type, | |
678 | attached); | |
db1b9037 CC |
679 | if (ret < 0) |
680 | goto out; | |
db1b9037 CC |
681 | break; |
682 | case MAX77693_MUIC_ADC_SEND_END_BUTTON: | |
683 | case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON: | |
684 | case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON: | |
db1b9037 CC |
685 | case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON: |
686 | case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON: | |
687 | case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON: | |
db1b9037 | 688 | case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON: |
db1b9037 | 689 | case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON: |
db1b9037 CC |
690 | case MAX77693_MUIC_ADC_RESERVED_ACC_1: |
691 | case MAX77693_MUIC_ADC_RESERVED_ACC_2: | |
db1b9037 CC |
692 | case MAX77693_MUIC_ADC_RESERVED_ACC_4: |
693 | case MAX77693_MUIC_ADC_RESERVED_ACC_5: | |
694 | case MAX77693_MUIC_ADC_CEA936_AUDIO: | |
695 | case MAX77693_MUIC_ADC_PHONE_POWERED_DEV: | |
696 | case MAX77693_MUIC_ADC_TTY_CONVERTER: | |
697 | case MAX77693_MUIC_ADC_UART_CABLE: | |
698 | case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG: | |
db1b9037 | 699 | case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG: |
39bf369e CC |
700 | /* |
701 | * This accessory isn't used in general case if it is specially | |
702 | * needed to detect additional accessory, should implement | |
703 | * proper operation when this accessory is attached/detached. | |
704 | */ | |
db1b9037 CC |
705 | dev_info(info->dev, |
706 | "accessory is %s but it isn't used (adc:0x%x)\n", | |
154f757f | 707 | attached ? "attached" : "detached", cable_type); |
db1b9037 CC |
708 | goto out; |
709 | default: | |
710 | dev_err(info->dev, | |
711 | "failed to detect %s accessory (adc:0x%x)\n", | |
154f757f | 712 | attached ? "attached" : "detached", cable_type); |
db1b9037 CC |
713 | ret = -EINVAL; |
714 | goto out; | |
715 | } | |
716 | ||
717 | out: | |
718 | return ret; | |
719 | } | |
720 | ||
154f757f | 721 | static int max77693_muic_chg_handler(struct max77693_muic_info *info) |
db1b9037 | 722 | { |
db1b9037 | 723 | int chg_type; |
06bed0af | 724 | int cable_type_gnd; |
39bf369e | 725 | int cable_type; |
154f757f | 726 | bool attached; |
06bed0af | 727 | bool cable_attached; |
154f757f | 728 | int ret = 0; |
db1b9037 | 729 | |
154f757f CC |
730 | chg_type = max77693_muic_get_cable_type(info, |
731 | MAX77693_CABLE_GROUP_CHG, &attached); | |
db1b9037 CC |
732 | |
733 | dev_info(info->dev, | |
734 | "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", | |
735 | attached ? "attached" : "detached", | |
154f757f | 736 | chg_type, info->prev_chg_type); |
db1b9037 CC |
737 | |
738 | switch (chg_type) { | |
739 | case MAX77693_CHARGER_TYPE_USB: | |
39bf369e CC |
740 | /* |
741 | * MHL_TA(USB/TA) with MHL cable | |
742 | * - MHL cable include two port(HDMI line and separate micro | |
743 | * -usb port. When the target connect MHL cable, extcon driver | |
744 | * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA | |
745 | * cable is connected, extcon driver notify state to notifiee | |
746 | * for charging battery. | |
747 | */ | |
06bed0af CC |
748 | cable_type_gnd = max77693_muic_get_cable_type(info, |
749 | MAX77693_CABLE_GROUP_ADC_GND, | |
750 | &cable_attached); | |
39bf369e CC |
751 | if (cable_type_gnd == MAX77693_MUIC_GND_MHL |
752 | || cable_type_gnd == MAX77693_MUIC_GND_MHL_VB) { | |
06bed0af CC |
753 | extcon_set_cable_state(info->edev, "MHL_TA", attached); |
754 | ||
755 | if (!cable_attached) | |
756 | extcon_set_cable_state(info->edev, | |
39bf369e CC |
757 | "MHL", false); |
758 | goto out; | |
759 | } | |
760 | ||
761 | /* | |
762 | * USB/TA cable with Dock-Audio device | |
763 | * - Dock device include two port(Dock-Audio and micro-usb | |
764 | * port). When the target connect Dock-Audio device, extcon | |
765 | * driver check whether USB/TA cable is connected. | |
766 | * If USB/TA cable is connected, extcon driver notify state | |
767 | * to notifiee for charging battery. | |
768 | */ | |
769 | cable_type = max77693_muic_get_cable_type(info, | |
770 | MAX77693_CABLE_GROUP_ADC, | |
771 | &cable_attached); | |
772 | if (cable_type == MAX77693_MUIC_ADC_AV_CABLE_NOLOAD) { | |
06bed0af | 773 | extcon_set_cable_state(info->edev, "USB", attached); |
39bf369e CC |
774 | |
775 | if (!cable_attached) | |
776 | extcon_set_cable_state(info->edev, | |
777 | "Dock-Audio", false); | |
778 | goto out; | |
06bed0af | 779 | } |
39bf369e CC |
780 | |
781 | /* Only USB cable, PATH:AP_USB */ | |
782 | ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached); | |
783 | if (ret < 0) | |
784 | goto out; | |
785 | extcon_set_cable_state(info->edev, "USB", attached); | |
db1b9037 CC |
786 | break; |
787 | case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT: | |
788 | extcon_set_cable_state(info->edev, | |
789 | "Charge-downstream", attached); | |
790 | break; | |
791 | case MAX77693_CHARGER_TYPE_DEDICATED_CHG: | |
792 | extcon_set_cable_state(info->edev, "TA", attached); | |
793 | break; | |
794 | case MAX77693_CHARGER_TYPE_APPLE_500MA: | |
795 | extcon_set_cable_state(info->edev, "Slow-charger", attached); | |
796 | break; | |
797 | case MAX77693_CHARGER_TYPE_APPLE_1A_2A: | |
798 | extcon_set_cable_state(info->edev, "Fast-charger", attached); | |
799 | break; | |
800 | case MAX77693_CHARGER_TYPE_DEAD_BATTERY: | |
801 | break; | |
802 | default: | |
803 | dev_err(info->dev, | |
804 | "failed to detect %s accessory (chg_type:0x%x)\n", | |
805 | attached ? "attached" : "detached", chg_type); | |
806 | ret = -EINVAL; | |
807 | goto out; | |
808 | } | |
809 | ||
810 | out: | |
811 | return ret; | |
812 | } | |
813 | ||
814 | static void max77693_muic_irq_work(struct work_struct *work) | |
815 | { | |
816 | struct max77693_muic_info *info = container_of(work, | |
817 | struct max77693_muic_info, irq_work); | |
db1b9037 CC |
818 | int irq_type = -1; |
819 | int i, ret = 0; | |
db1b9037 CC |
820 | |
821 | if (!info->edev) | |
822 | return; | |
823 | ||
824 | mutex_lock(&info->mutex); | |
825 | ||
826 | for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++) | |
827 | if (info->irq == muic_irqs[i].virq) | |
828 | irq_type = muic_irqs[i].irq; | |
829 | ||
830 | ret = max77693_bulk_read(info->max77693->regmap_muic, | |
831 | MAX77693_MUIC_REG_STATUS1, 2, info->status); | |
832 | if (ret) { | |
833 | dev_err(info->dev, "failed to read MUIC register\n"); | |
834 | mutex_unlock(&info->mutex); | |
835 | return; | |
836 | } | |
837 | ||
838 | switch (irq_type) { | |
839 | case MAX77693_MUIC_IRQ_INT1_ADC: | |
840 | case MAX77693_MUIC_IRQ_INT1_ADC_LOW: | |
841 | case MAX77693_MUIC_IRQ_INT1_ADC_ERR: | |
842 | case MAX77693_MUIC_IRQ_INT1_ADC1K: | |
843 | /* Handle all of accessory except for | |
844 | type of charger accessory */ | |
154f757f | 845 | ret = max77693_muic_adc_handler(info); |
db1b9037 CC |
846 | break; |
847 | case MAX77693_MUIC_IRQ_INT2_CHGTYP: | |
848 | case MAX77693_MUIC_IRQ_INT2_CHGDETREUN: | |
849 | case MAX77693_MUIC_IRQ_INT2_DCDTMR: | |
850 | case MAX77693_MUIC_IRQ_INT2_DXOVP: | |
851 | case MAX77693_MUIC_IRQ_INT2_VBVOLT: | |
852 | case MAX77693_MUIC_IRQ_INT2_VIDRM: | |
853 | /* Handle charger accessory */ | |
154f757f | 854 | ret = max77693_muic_chg_handler(info); |
db1b9037 CC |
855 | break; |
856 | case MAX77693_MUIC_IRQ_INT3_EOC: | |
857 | case MAX77693_MUIC_IRQ_INT3_CGMBC: | |
858 | case MAX77693_MUIC_IRQ_INT3_OVP: | |
859 | case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR: | |
860 | case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED: | |
861 | case MAX77693_MUIC_IRQ_INT3_BAT_DET: | |
862 | break; | |
863 | default: | |
864 | dev_err(info->dev, "muic interrupt: irq %d occurred\n", | |
865 | irq_type); | |
866 | break; | |
867 | } | |
868 | ||
869 | if (ret < 0) | |
870 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); | |
871 | ||
872 | mutex_unlock(&info->mutex); | |
873 | ||
874 | return; | |
875 | } | |
876 | ||
877 | static irqreturn_t max77693_muic_irq_handler(int irq, void *data) | |
878 | { | |
879 | struct max77693_muic_info *info = data; | |
880 | ||
881 | info->irq = irq; | |
882 | schedule_work(&info->irq_work); | |
883 | ||
884 | return IRQ_HANDLED; | |
885 | } | |
886 | ||
887 | static struct regmap_config max77693_muic_regmap_config = { | |
888 | .reg_bits = 8, | |
889 | .val_bits = 8, | |
890 | }; | |
891 | ||
892 | static int max77693_muic_detect_accessory(struct max77693_muic_info *info) | |
893 | { | |
894 | int ret = 0; | |
154f757f CC |
895 | int adc; |
896 | int chg_type; | |
897 | bool attached; | |
db1b9037 CC |
898 | |
899 | mutex_lock(&info->mutex); | |
900 | ||
901 | /* Read STATUSx register to detect accessory */ | |
902 | ret = max77693_bulk_read(info->max77693->regmap_muic, | |
903 | MAX77693_MUIC_REG_STATUS1, 2, info->status); | |
904 | if (ret) { | |
905 | dev_err(info->dev, "failed to read MUIC register\n"); | |
906 | mutex_unlock(&info->mutex); | |
907 | return -EINVAL; | |
908 | } | |
909 | ||
154f757f CC |
910 | adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC, |
911 | &attached); | |
912 | if (attached && adc != MAX77693_MUIC_ADC_OPEN) { | |
913 | ret = max77693_muic_adc_handler(info); | |
db1b9037 | 914 | if (ret < 0) |
154f757f | 915 | dev_err(info->dev, "Cannot detect accessory\n"); |
db1b9037 CC |
916 | } |
917 | ||
154f757f CC |
918 | chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG, |
919 | &attached); | |
920 | if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) { | |
921 | ret = max77693_muic_chg_handler(info); | |
db1b9037 | 922 | if (ret < 0) |
154f757f | 923 | dev_err(info->dev, "Cannot detect charger accessory\n"); |
db1b9037 CC |
924 | } |
925 | ||
db1b9037 | 926 | mutex_unlock(&info->mutex); |
154f757f | 927 | |
db1b9037 CC |
928 | return ret; |
929 | } | |
930 | ||
297620fd CC |
931 | static void max77693_muic_detect_cable_wq(struct work_struct *work) |
932 | { | |
933 | struct max77693_muic_info *info = container_of(to_delayed_work(work), | |
934 | struct max77693_muic_info, wq_detcable); | |
935 | ||
936 | max77693_muic_detect_accessory(info); | |
937 | } | |
938 | ||
44f34fd4 | 939 | static int max77693_muic_probe(struct platform_device *pdev) |
db1b9037 CC |
940 | { |
941 | struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); | |
f8457d57 CC |
942 | struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev); |
943 | struct max77693_muic_platform_data *muic_pdata = pdata->muic_data; | |
db1b9037 | 944 | struct max77693_muic_info *info; |
297620fd CC |
945 | int delay_jiffies; |
946 | int ret; | |
947 | int i; | |
db1b9037 CC |
948 | u8 id; |
949 | ||
f4bb5cb5 SK |
950 | info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info), |
951 | GFP_KERNEL); | |
db1b9037 CC |
952 | if (!info) { |
953 | dev_err(&pdev->dev, "failed to allocate memory\n"); | |
f4bb5cb5 | 954 | return -ENOMEM; |
db1b9037 CC |
955 | } |
956 | info->dev = &pdev->dev; | |
957 | info->max77693 = max77693; | |
1967fa08 | 958 | if (info->max77693->regmap_muic) { |
b186b124 | 959 | dev_dbg(&pdev->dev, "allocate register map\n"); |
1967fa08 | 960 | } else { |
b186b124 CC |
961 | info->max77693->regmap_muic = devm_regmap_init_i2c( |
962 | info->max77693->muic, | |
963 | &max77693_muic_regmap_config); | |
964 | if (IS_ERR(info->max77693->regmap_muic)) { | |
965 | ret = PTR_ERR(info->max77693->regmap_muic); | |
966 | dev_err(max77693->dev, | |
967 | "failed to allocate register map: %d\n", ret); | |
3bf742ff | 968 | return ret; |
b186b124 | 969 | } |
db1b9037 | 970 | } |
39bf369e CC |
971 | |
972 | /* Register input device for button of dock device */ | |
973 | info->dock = input_allocate_device(); | |
974 | if (!info->dock) { | |
975 | dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__); | |
976 | return -ENOMEM; | |
977 | } | |
978 | info->dock->name = "max77693-muic/dock"; | |
979 | info->dock->phys = "max77693-muic/extcon"; | |
980 | info->dock->dev.parent = &pdev->dev; | |
981 | ||
982 | __set_bit(EV_REP, info->dock->evbit); | |
983 | ||
984 | input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP); | |
985 | input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN); | |
986 | input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE); | |
987 | input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG); | |
988 | input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG); | |
989 | ||
990 | ret = input_register_device(info->dock); | |
991 | if (ret < 0) { | |
992 | dev_err(&pdev->dev, "Cannot register input device error(%d)\n", | |
993 | ret); | |
994 | return ret; | |
995 | } | |
996 | ||
db1b9037 CC |
997 | platform_set_drvdata(pdev, info); |
998 | mutex_init(&info->mutex); | |
999 | ||
1000 | INIT_WORK(&info->irq_work, max77693_muic_irq_work); | |
1001 | ||
1002 | /* Support irq domain for MAX77693 MUIC device */ | |
1003 | for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) { | |
1004 | struct max77693_muic_irq *muic_irq = &muic_irqs[i]; | |
00af4b16 | 1005 | unsigned int virq = 0; |
db1b9037 CC |
1006 | |
1007 | virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq); | |
00af4b16 SK |
1008 | if (!virq) { |
1009 | ret = -EINVAL; | |
db1b9037 | 1010 | goto err_irq; |
00af4b16 | 1011 | } |
db1b9037 CC |
1012 | muic_irq->virq = virq; |
1013 | ||
1014 | ret = request_threaded_irq(virq, NULL, | |
1015 | max77693_muic_irq_handler, | |
ae3b3215 CC |
1016 | IRQF_NO_SUSPEND, |
1017 | muic_irq->name, info); | |
db1b9037 CC |
1018 | if (ret) { |
1019 | dev_err(&pdev->dev, | |
1020 | "failed: irq request (IRQ: %d," | |
1021 | " error :%d)\n", | |
1022 | muic_irq->irq, ret); | |
db1b9037 CC |
1023 | goto err_irq; |
1024 | } | |
1025 | } | |
1026 | ||
1027 | /* Initialize extcon device */ | |
f4bb5cb5 SK |
1028 | info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev), |
1029 | GFP_KERNEL); | |
db1b9037 CC |
1030 | if (!info->edev) { |
1031 | dev_err(&pdev->dev, "failed to allocate memory for extcon\n"); | |
1032 | ret = -ENOMEM; | |
1033 | goto err_irq; | |
1034 | } | |
1035 | info->edev->name = DEV_NAME; | |
1036 | info->edev->supported_cable = max77693_extcon_cable; | |
1037 | ret = extcon_dev_register(info->edev, NULL); | |
1038 | if (ret) { | |
1039 | dev_err(&pdev->dev, "failed to register extcon device\n"); | |
f4bb5cb5 | 1040 | goto err_irq; |
db1b9037 CC |
1041 | } |
1042 | ||
f8457d57 CC |
1043 | /* Initialize MUIC register by using platform data */ |
1044 | for (i = 0 ; i < muic_pdata->num_init_data ; i++) { | |
1045 | enum max77693_irq_source irq_src = MAX77693_IRQ_GROUP_NR; | |
1046 | ||
1047 | max77693_write_reg(info->max77693->regmap_muic, | |
1048 | muic_pdata->init_data[i].addr, | |
1049 | muic_pdata->init_data[i].data); | |
1050 | ||
1051 | switch (muic_pdata->init_data[i].addr) { | |
1052 | case MAX77693_MUIC_REG_INTMASK1: | |
1053 | irq_src = MUIC_INT1; | |
1054 | break; | |
1055 | case MAX77693_MUIC_REG_INTMASK2: | |
1056 | irq_src = MUIC_INT2; | |
1057 | break; | |
1058 | case MAX77693_MUIC_REG_INTMASK3: | |
1059 | irq_src = MUIC_INT3; | |
1060 | break; | |
1061 | } | |
1062 | ||
1063 | if (irq_src < MAX77693_IRQ_GROUP_NR) | |
1064 | info->max77693->irq_masks_cur[irq_src] | |
1065 | = muic_pdata->init_data[i].data; | |
1066 | } | |
1067 | ||
2b75799f CC |
1068 | /* |
1069 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB | |
1070 | * h/w path of COMP2/COMN1 on CONTROL1 register. | |
1071 | */ | |
1072 | if (muic_pdata->path_uart) | |
1073 | info->path_uart = muic_pdata->path_uart; | |
1074 | else | |
1075 | info->path_uart = CONTROL1_SW_UART; | |
1076 | ||
1077 | if (muic_pdata->path_usb) | |
1078 | info->path_usb = muic_pdata->path_usb; | |
1079 | else | |
1080 | info->path_usb = CONTROL1_SW_USB; | |
1081 | ||
1082 | /* Set initial path for UART */ | |
1083 | max77693_muic_set_path(info, info->path_uart, true); | |
1084 | ||
db1b9037 CC |
1085 | /* Check revision number of MUIC device*/ |
1086 | ret = max77693_read_reg(info->max77693->regmap_muic, | |
1087 | MAX77693_MUIC_REG_ID, &id); | |
1088 | if (ret < 0) { | |
1089 | dev_err(&pdev->dev, "failed to read revision number\n"); | |
39bf369e | 1090 | goto err_extcon; |
db1b9037 CC |
1091 | } |
1092 | dev_info(info->dev, "device ID : 0x%x\n", id); | |
1093 | ||
1094 | /* Set ADC debounce time */ | |
1095 | max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS); | |
1096 | ||
297620fd CC |
1097 | /* |
1098 | * Detect accessory after completing the initialization of platform | |
1099 | * | |
1100 | * - Use delayed workqueue to detect cable state and then | |
1101 | * notify cable state to notifiee/platform through uevent. | |
1102 | * After completing the booting of platform, the extcon provider | |
1103 | * driver should notify cable state to upper layer. | |
1104 | */ | |
1105 | INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq); | |
1106 | if (muic_pdata->detcable_delay_ms) | |
1107 | delay_jiffies = msecs_to_jiffies(muic_pdata->detcable_delay_ms); | |
1108 | else | |
1109 | delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT); | |
1110 | schedule_delayed_work(&info->wq_detcable, delay_jiffies); | |
db1b9037 CC |
1111 | |
1112 | return ret; | |
1113 | ||
39bf369e CC |
1114 | err_extcon: |
1115 | extcon_dev_unregister(info->edev); | |
db1b9037 | 1116 | err_irq: |
00af4b16 SK |
1117 | while (--i >= 0) |
1118 | free_irq(muic_irqs[i].virq, info); | |
db1b9037 CC |
1119 | return ret; |
1120 | } | |
1121 | ||
93ed0327 | 1122 | static int max77693_muic_remove(struct platform_device *pdev) |
db1b9037 CC |
1123 | { |
1124 | struct max77693_muic_info *info = platform_get_drvdata(pdev); | |
1125 | int i; | |
1126 | ||
1127 | for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) | |
1128 | free_irq(muic_irqs[i].virq, info); | |
1129 | cancel_work_sync(&info->irq_work); | |
39bf369e | 1130 | input_unregister_device(info->dock); |
db1b9037 | 1131 | extcon_dev_unregister(info->edev); |
db1b9037 CC |
1132 | |
1133 | return 0; | |
1134 | } | |
1135 | ||
1136 | static struct platform_driver max77693_muic_driver = { | |
1137 | .driver = { | |
1138 | .name = DEV_NAME, | |
1139 | .owner = THIS_MODULE, | |
1140 | }, | |
1141 | .probe = max77693_muic_probe, | |
5f7e2228 | 1142 | .remove = max77693_muic_remove, |
db1b9037 CC |
1143 | }; |
1144 | ||
1145 | module_platform_driver(max77693_muic_driver); | |
1146 | ||
1147 | MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver"); | |
1148 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); | |
1149 | MODULE_LICENSE("GPL"); | |
1150 | MODULE_ALIAS("platform:extcon-max77693"); |