Commit | Line | Data |
---|---|---|
914b881f CC |
1 | /* |
2 | * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches | |
3 | * | |
4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd | |
5 | * Author: Chanwoo Choi <cw00.choi@samsung.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2 of the License, or (at your | |
10 | * 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/err.h> | |
19 | #include <linux/i2c.h> | |
20 | #include <linux/input.h> | |
21 | #include <linux/interrupt.h> | |
22 | #include <linux/irqdomain.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/platform_device.h> | |
26 | #include <linux/regmap.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/extcon.h> | |
29 | #include <linux/extcon/sm5502.h> | |
30 | ||
e1954452 CC |
31 | #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */ |
32 | ||
914b881f CC |
33 | struct muic_irq { |
34 | unsigned int irq; | |
35 | const char *name; | |
36 | unsigned int virq; | |
37 | }; | |
38 | ||
39 | struct reg_data { | |
40 | u8 reg; | |
41 | unsigned int val; | |
42 | bool invert; | |
43 | }; | |
44 | ||
45 | struct sm5502_muic_info { | |
46 | struct device *dev; | |
47 | struct extcon_dev *edev; | |
48 | ||
49 | struct i2c_client *i2c; | |
50 | struct regmap *regmap; | |
51 | ||
52 | struct regmap_irq_chip_data *irq_data; | |
53 | struct muic_irq *muic_irqs; | |
54 | unsigned int num_muic_irqs; | |
55 | int irq; | |
56 | bool irq_attach; | |
57 | bool irq_detach; | |
58 | struct work_struct irq_work; | |
59 | ||
60 | struct reg_data *reg_data; | |
61 | unsigned int num_reg_data; | |
62 | ||
63 | struct mutex mutex; | |
e1954452 CC |
64 | |
65 | /* | |
66 | * Use delayed workqueue to detect cable state and then | |
67 | * notify cable state to notifiee/platform through uevent. | |
68 | * After completing the booting of platform, the extcon provider | |
69 | * driver should notify cable state to upper layer. | |
70 | */ | |
71 | struct delayed_work wq_detcable; | |
914b881f CC |
72 | }; |
73 | ||
74 | /* Default value of SM5502 register to bring up MUIC device. */ | |
75 | static struct reg_data sm5502_reg_data[] = { | |
76 | { | |
77 | .reg = SM5502_REG_CONTROL, | |
78 | .val = SM5502_REG_CONTROL_MASK_INT_MASK, | |
79 | .invert = false, | |
80 | }, { | |
81 | .reg = SM5502_REG_INTMASK1, | |
82 | .val = SM5502_REG_INTM1_KP_MASK | |
83 | | SM5502_REG_INTM1_LKP_MASK | |
84 | | SM5502_REG_INTM1_LKR_MASK, | |
85 | .invert = true, | |
86 | }, { | |
87 | .reg = SM5502_REG_INTMASK2, | |
88 | .val = SM5502_REG_INTM2_VBUS_DET_MASK | |
89 | | SM5502_REG_INTM2_REV_ACCE_MASK | |
90 | | SM5502_REG_INTM2_ADC_CHG_MASK | |
91 | | SM5502_REG_INTM2_STUCK_KEY_MASK | |
92 | | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK | |
93 | | SM5502_REG_INTM2_MHL_MASK, | |
94 | .invert = true, | |
95 | }, | |
96 | { } | |
97 | }; | |
98 | ||
99 | /* List of detectable cables */ | |
100 | enum { | |
101 | EXTCON_CABLE_USB = 0, | |
102 | EXTCON_CABLE_USB_HOST, | |
103 | EXTCON_CABLE_TA, | |
104 | ||
105 | EXTCON_CABLE_END, | |
106 | }; | |
107 | ||
108 | static const char *sm5502_extcon_cable[] = { | |
109 | [EXTCON_CABLE_USB] = "USB", | |
110 | [EXTCON_CABLE_USB_HOST] = "USB-Host", | |
111 | [EXTCON_CABLE_TA] = "TA", | |
112 | NULL, | |
113 | }; | |
114 | ||
115 | /* Define supported accessory type */ | |
116 | enum sm5502_muic_acc_type { | |
117 | SM5502_MUIC_ADC_GROUND = 0x0, | |
118 | SM5502_MUIC_ADC_SEND_END_BUTTON, | |
119 | SM5502_MUIC_ADC_REMOTE_S1_BUTTON, | |
120 | SM5502_MUIC_ADC_REMOTE_S2_BUTTON, | |
121 | SM5502_MUIC_ADC_REMOTE_S3_BUTTON, | |
122 | SM5502_MUIC_ADC_REMOTE_S4_BUTTON, | |
123 | SM5502_MUIC_ADC_REMOTE_S5_BUTTON, | |
124 | SM5502_MUIC_ADC_REMOTE_S6_BUTTON, | |
125 | SM5502_MUIC_ADC_REMOTE_S7_BUTTON, | |
126 | SM5502_MUIC_ADC_REMOTE_S8_BUTTON, | |
127 | SM5502_MUIC_ADC_REMOTE_S9_BUTTON, | |
128 | SM5502_MUIC_ADC_REMOTE_S10_BUTTON, | |
129 | SM5502_MUIC_ADC_REMOTE_S11_BUTTON, | |
130 | SM5502_MUIC_ADC_REMOTE_S12_BUTTON, | |
131 | SM5502_MUIC_ADC_RESERVED_ACC_1, | |
132 | SM5502_MUIC_ADC_RESERVED_ACC_2, | |
133 | SM5502_MUIC_ADC_RESERVED_ACC_3, | |
134 | SM5502_MUIC_ADC_RESERVED_ACC_4, | |
135 | SM5502_MUIC_ADC_RESERVED_ACC_5, | |
136 | SM5502_MUIC_ADC_AUDIO_TYPE2, | |
137 | SM5502_MUIC_ADC_PHONE_POWERED_DEV, | |
138 | SM5502_MUIC_ADC_TTY_CONVERTER, | |
139 | SM5502_MUIC_ADC_UART_CABLE, | |
140 | SM5502_MUIC_ADC_TYPE1_CHARGER, | |
141 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB, | |
142 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB, | |
143 | SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE, | |
144 | SM5502_MUIC_ADC_TYPE2_CHARGER, | |
145 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART, | |
146 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART, | |
147 | SM5502_MUIC_ADC_AUDIO_TYPE1, | |
148 | SM5502_MUIC_ADC_OPEN = 0x1f, | |
149 | ||
150 | /* The below accessories have same ADC value (0x1f or 0x1e). | |
151 | So, Device type1 is used to separate specific accessory. */ | |
152 | /* |---------|--ADC| */ | |
153 | /* | [7:5]|[4:0]| */ | |
154 | SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */ | |
155 | SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */ | |
156 | /* |Dev Type1|--ADC| */ | |
157 | SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */ | |
158 | SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */ | |
159 | SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */ | |
160 | }; | |
161 | ||
162 | /* List of supported interrupt for SM5502 */ | |
163 | static struct muic_irq sm5502_muic_irqs[] = { | |
164 | { SM5502_IRQ_INT1_ATTACH, "muic-attach" }, | |
165 | { SM5502_IRQ_INT1_DETACH, "muic-detach" }, | |
166 | { SM5502_IRQ_INT1_KP, "muic-kp" }, | |
167 | { SM5502_IRQ_INT1_LKP, "muic-lkp" }, | |
168 | { SM5502_IRQ_INT1_LKR, "muic-lkr" }, | |
169 | { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" }, | |
170 | { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" }, | |
171 | { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" }, | |
172 | { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" }, | |
173 | { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" }, | |
174 | { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" }, | |
175 | { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" }, | |
176 | { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" }, | |
177 | { SM5502_IRQ_INT2_MHL, "muic-mhl" }, | |
178 | }; | |
179 | ||
180 | /* Define interrupt list of SM5502 to register regmap_irq */ | |
181 | static const struct regmap_irq sm5502_irqs[] = { | |
182 | /* INT1 interrupts */ | |
183 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, }, | |
184 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, }, | |
185 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, }, | |
186 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, }, | |
187 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, }, | |
188 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, }, | |
189 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, }, | |
190 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, }, | |
191 | ||
192 | /* INT2 interrupts */ | |
193 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,}, | |
194 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, }, | |
195 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, }, | |
196 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, }, | |
197 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, }, | |
198 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, }, | |
199 | }; | |
200 | ||
201 | static const struct regmap_irq_chip sm5502_muic_irq_chip = { | |
202 | .name = "sm5502", | |
203 | .status_base = SM5502_REG_INT1, | |
204 | .mask_base = SM5502_REG_INTMASK1, | |
205 | .mask_invert = false, | |
206 | .num_regs = 2, | |
207 | .irqs = sm5502_irqs, | |
208 | .num_irqs = ARRAY_SIZE(sm5502_irqs), | |
209 | }; | |
210 | ||
211 | /* Define regmap configuration of SM5502 for I2C communication */ | |
212 | static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg) | |
213 | { | |
214 | switch (reg) { | |
215 | case SM5502_REG_INTMASK1: | |
216 | case SM5502_REG_INTMASK2: | |
217 | return true; | |
218 | default: | |
219 | break; | |
220 | } | |
221 | return false; | |
222 | } | |
223 | ||
224 | static const struct regmap_config sm5502_muic_regmap_config = { | |
225 | .reg_bits = 8, | |
226 | .val_bits = 8, | |
227 | .volatile_reg = sm5502_muic_volatile_reg, | |
228 | .max_register = SM5502_REG_END, | |
229 | }; | |
230 | ||
a75fed2e CC |
231 | /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */ |
232 | static int sm5502_muic_set_path(struct sm5502_muic_info *info, | |
233 | unsigned int con_sw, unsigned int vbus_sw, | |
234 | bool attached) | |
235 | { | |
236 | int ret; | |
237 | ||
238 | if (!attached) { | |
239 | con_sw = DM_DP_SWITCH_OPEN; | |
240 | vbus_sw = VBUSIN_SWITCH_OPEN; | |
241 | } | |
242 | ||
243 | switch (con_sw) { | |
244 | case DM_DP_SWITCH_OPEN: | |
245 | case DM_DP_SWITCH_USB: | |
246 | case DM_DP_SWITCH_AUDIO: | |
247 | case DM_DP_SWITCH_UART: | |
248 | ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, | |
249 | SM5502_REG_MANUAL_SW1_DP_MASK | | |
250 | SM5502_REG_MANUAL_SW1_DM_MASK, | |
251 | con_sw); | |
252 | if (ret < 0) { | |
253 | dev_err(info->dev, | |
254 | "cannot update DM_CON/DP_CON switch\n"); | |
255 | return ret; | |
256 | } | |
257 | break; | |
258 | default: | |
259 | dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n", | |
260 | con_sw); | |
261 | return -EINVAL; | |
262 | }; | |
263 | ||
264 | switch (vbus_sw) { | |
265 | case VBUSIN_SWITCH_OPEN: | |
266 | case VBUSIN_SWITCH_VBUSOUT: | |
267 | case VBUSIN_SWITCH_MIC: | |
268 | case VBUSIN_SWITCH_VBUSOUT_WITH_USB: | |
269 | ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, | |
270 | SM5502_REG_MANUAL_SW1_VBUSIN_MASK, | |
271 | vbus_sw); | |
272 | if (ret < 0) { | |
273 | dev_err(info->dev, | |
274 | "cannot update VBUSIN switch\n"); | |
275 | return ret; | |
276 | } | |
277 | break; | |
278 | default: | |
279 | dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw); | |
280 | return -EINVAL; | |
281 | }; | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
914b881f CC |
286 | /* Return cable type of attached or detached accessories */ |
287 | static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info) | |
288 | { | |
289 | unsigned int cable_type = -1, adc, dev_type1; | |
290 | int ret; | |
291 | ||
292 | /* Read ADC value according to external cable or button */ | |
293 | ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); | |
294 | if (ret) { | |
295 | dev_err(info->dev, "failed to read ADC register\n"); | |
296 | return ret; | |
297 | } | |
298 | ||
299 | /* | |
300 | * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't | |
301 | * connected with to MUIC device. | |
302 | */ | |
303 | cable_type &= SM5502_REG_ADC_MASK; | |
304 | if (cable_type == SM5502_MUIC_ADC_GROUND) | |
305 | return SM5502_MUIC_ADC_GROUND; | |
306 | ||
307 | switch (cable_type) { | |
308 | case SM5502_MUIC_ADC_GROUND: | |
309 | case SM5502_MUIC_ADC_SEND_END_BUTTON: | |
310 | case SM5502_MUIC_ADC_REMOTE_S1_BUTTON: | |
311 | case SM5502_MUIC_ADC_REMOTE_S2_BUTTON: | |
312 | case SM5502_MUIC_ADC_REMOTE_S3_BUTTON: | |
313 | case SM5502_MUIC_ADC_REMOTE_S4_BUTTON: | |
314 | case SM5502_MUIC_ADC_REMOTE_S5_BUTTON: | |
315 | case SM5502_MUIC_ADC_REMOTE_S6_BUTTON: | |
316 | case SM5502_MUIC_ADC_REMOTE_S7_BUTTON: | |
317 | case SM5502_MUIC_ADC_REMOTE_S8_BUTTON: | |
318 | case SM5502_MUIC_ADC_REMOTE_S9_BUTTON: | |
319 | case SM5502_MUIC_ADC_REMOTE_S10_BUTTON: | |
320 | case SM5502_MUIC_ADC_REMOTE_S11_BUTTON: | |
321 | case SM5502_MUIC_ADC_REMOTE_S12_BUTTON: | |
322 | case SM5502_MUIC_ADC_RESERVED_ACC_1: | |
323 | case SM5502_MUIC_ADC_RESERVED_ACC_2: | |
324 | case SM5502_MUIC_ADC_RESERVED_ACC_3: | |
325 | case SM5502_MUIC_ADC_RESERVED_ACC_4: | |
326 | case SM5502_MUIC_ADC_RESERVED_ACC_5: | |
327 | case SM5502_MUIC_ADC_AUDIO_TYPE2: | |
328 | case SM5502_MUIC_ADC_PHONE_POWERED_DEV: | |
329 | case SM5502_MUIC_ADC_TTY_CONVERTER: | |
330 | case SM5502_MUIC_ADC_UART_CABLE: | |
331 | case SM5502_MUIC_ADC_TYPE1_CHARGER: | |
332 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: | |
333 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: | |
334 | case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE: | |
335 | case SM5502_MUIC_ADC_TYPE2_CHARGER: | |
336 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: | |
337 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: | |
338 | break; | |
339 | case SM5502_MUIC_ADC_AUDIO_TYPE1: | |
340 | /* | |
341 | * Check whether cable type is | |
342 | * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE | |
343 | * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END | |
344 | * by using Button event. | |
345 | */ | |
346 | break; | |
347 | case SM5502_MUIC_ADC_OPEN: | |
348 | ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, | |
349 | &dev_type1); | |
350 | if (ret) { | |
351 | dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); | |
352 | return ret; | |
353 | } | |
354 | ||
355 | switch (dev_type1) { | |
356 | case SM5502_REG_DEV_TYPE1_USB_SDP_MASK: | |
357 | cable_type = SM5502_MUIC_ADC_OPEN_USB; | |
358 | break; | |
359 | case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK: | |
360 | cable_type = SM5502_MUIC_ADC_OPEN_TA; | |
361 | break; | |
362 | case SM5502_REG_DEV_TYPE1_USB_OTG_MASK: | |
363 | cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG; | |
364 | break; | |
365 | default: | |
366 | dev_dbg(info->dev, | |
367 | "cannot identify the cable type: adc(0x%x) " | |
368 | "dev_type1(0x%x)\n", adc, dev_type1); | |
369 | return -EINVAL; | |
370 | }; | |
371 | break; | |
372 | default: | |
373 | dev_err(info->dev, | |
374 | "failed to identify the cable type: adc(0x%x)\n", adc); | |
375 | return -EINVAL; | |
376 | }; | |
377 | ||
378 | return cable_type; | |
379 | } | |
380 | ||
381 | static int sm5502_muic_cable_handler(struct sm5502_muic_info *info, | |
382 | bool attached) | |
383 | { | |
384 | static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND; | |
385 | const char **cable_names = info->edev->supported_cable; | |
386 | unsigned int cable_type = SM5502_MUIC_ADC_GROUND; | |
a75fed2e CC |
387 | unsigned int con_sw = DM_DP_SWITCH_OPEN; |
388 | unsigned int vbus_sw = VBUSIN_SWITCH_OPEN; | |
914b881f | 389 | unsigned int idx = 0; |
a75fed2e | 390 | int ret; |
914b881f CC |
391 | |
392 | if (!cable_names) | |
393 | return 0; | |
394 | ||
395 | /* Get the type of attached or detached cable */ | |
396 | if (attached) | |
397 | cable_type = sm5502_muic_get_cable_type(info); | |
398 | else if (!attached) | |
399 | cable_type = prev_cable_type; | |
400 | prev_cable_type = cable_type; | |
401 | ||
402 | switch (cable_type) { | |
403 | case SM5502_MUIC_ADC_OPEN_USB: | |
a75fed2e CC |
404 | idx = EXTCON_CABLE_USB; |
405 | con_sw = DM_DP_SWITCH_USB; | |
406 | vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB; | |
914b881f CC |
407 | break; |
408 | case SM5502_MUIC_ADC_OPEN_TA: | |
a75fed2e CC |
409 | idx = EXTCON_CABLE_TA; |
410 | con_sw = DM_DP_SWITCH_OPEN; | |
411 | vbus_sw = VBUSIN_SWITCH_VBUSOUT; | |
914b881f CC |
412 | break; |
413 | case SM5502_MUIC_ADC_OPEN_USB_OTG: | |
a75fed2e CC |
414 | idx = EXTCON_CABLE_USB_HOST; |
415 | con_sw = DM_DP_SWITCH_USB; | |
416 | vbus_sw = VBUSIN_SWITCH_OPEN; | |
e1954452 | 417 | break; |
914b881f CC |
418 | default: |
419 | dev_dbg(info->dev, | |
420 | "cannot handle this cable_type (0x%x)\n", cable_type); | |
421 | return 0; | |
422 | }; | |
423 | ||
a75fed2e CC |
424 | /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */ |
425 | ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached); | |
426 | if (ret < 0) | |
427 | return ret; | |
428 | ||
429 | /* Change the state of external accessory */ | |
914b881f CC |
430 | extcon_set_cable_state(info->edev, cable_names[idx], attached); |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
435 | static void sm5502_muic_irq_work(struct work_struct *work) | |
436 | { | |
437 | struct sm5502_muic_info *info = container_of(work, | |
438 | struct sm5502_muic_info, irq_work); | |
439 | int ret = 0; | |
440 | ||
441 | if (!info->edev) | |
442 | return; | |
443 | ||
444 | mutex_lock(&info->mutex); | |
445 | ||
446 | /* Detect attached or detached cables */ | |
447 | if (info->irq_attach) { | |
448 | ret = sm5502_muic_cable_handler(info, true); | |
449 | info->irq_attach = false; | |
450 | } | |
451 | if (info->irq_detach) { | |
452 | ret = sm5502_muic_cable_handler(info, false); | |
453 | info->irq_detach = false; | |
454 | } | |
455 | ||
456 | if (ret < 0) | |
457 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); | |
458 | ||
459 | mutex_unlock(&info->mutex); | |
460 | ||
461 | return; | |
462 | } | |
463 | ||
464 | /* | |
465 | * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0. | |
466 | * Returns -ESRCH if irq_type does not match registered IRQ for this dev type. | |
467 | */ | |
468 | static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type) | |
469 | { | |
470 | switch (irq_type) { | |
471 | case SM5502_IRQ_INT1_ATTACH: | |
472 | info->irq_attach = true; | |
473 | break; | |
474 | case SM5502_IRQ_INT1_DETACH: | |
475 | info->irq_detach = true; | |
476 | break; | |
477 | case SM5502_IRQ_INT1_KP: | |
478 | case SM5502_IRQ_INT1_LKP: | |
479 | case SM5502_IRQ_INT1_LKR: | |
480 | case SM5502_IRQ_INT1_OVP_EVENT: | |
481 | case SM5502_IRQ_INT1_OCP_EVENT: | |
482 | case SM5502_IRQ_INT1_OVP_OCP_DIS: | |
483 | case SM5502_IRQ_INT2_VBUS_DET: | |
484 | case SM5502_IRQ_INT2_REV_ACCE: | |
485 | case SM5502_IRQ_INT2_ADC_CHG: | |
486 | case SM5502_IRQ_INT2_STUCK_KEY: | |
487 | case SM5502_IRQ_INT2_STUCK_KEY_RCV: | |
488 | case SM5502_IRQ_INT2_MHL: | |
489 | default: | |
490 | break; | |
491 | } | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
496 | static irqreturn_t sm5502_muic_irq_handler(int irq, void *data) | |
497 | { | |
498 | struct sm5502_muic_info *info = data; | |
499 | int i, irq_type = -1, ret; | |
500 | ||
501 | for (i = 0; i < info->num_muic_irqs; i++) | |
502 | if (irq == info->muic_irqs[i].virq) | |
503 | irq_type = info->muic_irqs[i].irq; | |
504 | ||
505 | ret = sm5502_parse_irq(info, irq_type); | |
506 | if (ret < 0) { | |
507 | dev_warn(info->dev, "cannot handle is interrupt:%d\n", | |
508 | irq_type); | |
509 | return IRQ_HANDLED; | |
510 | } | |
511 | schedule_work(&info->irq_work); | |
512 | ||
513 | return IRQ_HANDLED; | |
514 | } | |
515 | ||
e1954452 CC |
516 | static void sm5502_muic_detect_cable_wq(struct work_struct *work) |
517 | { | |
518 | struct sm5502_muic_info *info = container_of(to_delayed_work(work), | |
519 | struct sm5502_muic_info, wq_detcable); | |
520 | int ret; | |
521 | ||
522 | /* Notify the state of connector cable or not */ | |
523 | ret = sm5502_muic_cable_handler(info, true); | |
524 | if (ret < 0) | |
525 | dev_warn(info->dev, "failed to detect cable state\n"); | |
526 | } | |
527 | ||
914b881f CC |
528 | static void sm5502_init_dev_type(struct sm5502_muic_info *info) |
529 | { | |
530 | unsigned int reg_data, vendor_id, version_id; | |
531 | int i, ret; | |
532 | ||
533 | /* To test I2C, Print version_id and vendor_id of SM5502 */ | |
534 | ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); | |
535 | if (ret) { | |
536 | dev_err(info->dev, | |
537 | "failed to read DEVICE_ID register: %d\n", ret); | |
538 | return; | |
539 | } | |
540 | ||
541 | vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >> | |
542 | SM5502_REG_DEVICE_ID_VENDOR_SHIFT); | |
543 | version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >> | |
544 | SM5502_REG_DEVICE_ID_VERSION_SHIFT); | |
545 | ||
546 | dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", | |
547 | version_id, vendor_id); | |
548 | ||
549 | /* Initiazle the register of SM5502 device to bring-up */ | |
550 | for (i = 0; i < info->num_reg_data; i++) { | |
551 | unsigned int val = 0; | |
552 | ||
553 | if (!info->reg_data[i].invert) | |
554 | val |= ~info->reg_data[i].val; | |
555 | else | |
556 | val = info->reg_data[i].val; | |
557 | regmap_write(info->regmap, info->reg_data[i].reg, val); | |
558 | } | |
559 | } | |
560 | ||
561 | static int sm5022_muic_i2c_probe(struct i2c_client *i2c, | |
562 | const struct i2c_device_id *id) | |
563 | { | |
564 | struct device_node *np = i2c->dev.of_node; | |
565 | struct sm5502_muic_info *info; | |
566 | int i, ret, irq_flags; | |
567 | ||
568 | if (!np) | |
569 | return -EINVAL; | |
570 | ||
571 | info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); | |
572 | if (!info) | |
573 | return -ENOMEM; | |
574 | i2c_set_clientdata(i2c, info); | |
575 | ||
576 | info->dev = &i2c->dev; | |
577 | info->i2c = i2c; | |
578 | info->irq = i2c->irq; | |
579 | info->muic_irqs = sm5502_muic_irqs; | |
580 | info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs); | |
581 | info->reg_data = sm5502_reg_data; | |
582 | info->num_reg_data = ARRAY_SIZE(sm5502_reg_data); | |
583 | ||
584 | mutex_init(&info->mutex); | |
585 | ||
586 | INIT_WORK(&info->irq_work, sm5502_muic_irq_work); | |
587 | ||
588 | info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); | |
589 | if (IS_ERR(info->regmap)) { | |
590 | ret = PTR_ERR(info->regmap); | |
591 | dev_err(info->dev, "failed to allocate register map: %d\n", | |
592 | ret); | |
593 | return ret; | |
594 | } | |
595 | ||
596 | /* Support irq domain for SM5502 MUIC device */ | |
597 | irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; | |
598 | ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0, | |
599 | &sm5502_muic_irq_chip, &info->irq_data); | |
600 | if (ret != 0) { | |
601 | dev_err(info->dev, "failed to request IRQ %d: %d\n", | |
602 | info->irq, ret); | |
603 | return ret; | |
604 | } | |
605 | ||
606 | for (i = 0; i < info->num_muic_irqs; i++) { | |
607 | struct muic_irq *muic_irq = &info->muic_irqs[i]; | |
608 | unsigned int virq = 0; | |
609 | ||
610 | virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); | |
611 | if (virq <= 0) | |
612 | return -EINVAL; | |
613 | muic_irq->virq = virq; | |
614 | ||
615 | ret = devm_request_threaded_irq(info->dev, virq, NULL, | |
616 | sm5502_muic_irq_handler, | |
617 | IRQF_NO_SUSPEND, | |
618 | muic_irq->name, info); | |
619 | if (ret) { | |
620 | dev_err(info->dev, "failed: irq request (IRQ: %d," | |
621 | " error :%d)\n", muic_irq->irq, ret); | |
622 | return ret; | |
623 | } | |
624 | } | |
625 | ||
626 | /* Allocate extcon device */ | |
627 | info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); | |
628 | if (IS_ERR(info->edev)) { | |
629 | dev_err(info->dev, "failed to allocate memory for extcon\n"); | |
630 | return -ENOMEM; | |
631 | } | |
632 | info->edev->name = np->name; | |
633 | ||
634 | /* Register extcon device */ | |
635 | ret = devm_extcon_dev_register(info->dev, info->edev); | |
636 | if (ret) { | |
637 | dev_err(info->dev, "failed to register extcon device\n"); | |
638 | return ret; | |
639 | } | |
640 | ||
e1954452 CC |
641 | /* |
642 | * Detect accessory after completing the initialization of platform | |
643 | * | |
644 | * - Use delayed workqueue to detect cable state and then | |
645 | * notify cable state to notifiee/platform through uevent. | |
646 | * After completing the booting of platform, the extcon provider | |
647 | * driver should notify cable state to upper layer. | |
648 | */ | |
649 | INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq); | |
650 | queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, | |
651 | msecs_to_jiffies(DELAY_MS_DEFAULT)); | |
652 | ||
914b881f CC |
653 | /* Initialize SM5502 device and print vendor id and version id */ |
654 | sm5502_init_dev_type(info); | |
655 | ||
656 | return 0; | |
657 | } | |
658 | ||
659 | static int sm5502_muic_i2c_remove(struct i2c_client *i2c) | |
660 | { | |
661 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | |
662 | ||
663 | regmap_del_irq_chip(info->irq, info->irq_data); | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | static struct of_device_id sm5502_dt_match[] = { | |
669 | { .compatible = "siliconmitus,sm5502-muic" }, | |
670 | { }, | |
671 | }; | |
672 | ||
673 | #ifdef CONFIG_PM_SLEEP | |
674 | static int sm5502_muic_suspend(struct device *dev) | |
675 | { | |
676 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); | |
677 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | |
678 | ||
679 | enable_irq_wake(info->irq); | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
684 | static int sm5502_muic_resume(struct device *dev) | |
685 | { | |
686 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); | |
687 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | |
688 | ||
689 | disable_irq_wake(info->irq); | |
690 | ||
691 | return 0; | |
692 | } | |
693 | #endif | |
694 | ||
695 | static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops, | |
696 | sm5502_muic_suspend, sm5502_muic_resume); | |
697 | ||
698 | static const struct i2c_device_id sm5502_i2c_id[] = { | |
699 | { "sm5502", TYPE_SM5502 }, | |
700 | { } | |
701 | }; | |
702 | MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id); | |
703 | ||
704 | static struct i2c_driver sm5502_muic_i2c_driver = { | |
705 | .driver = { | |
706 | .name = "sm5502", | |
707 | .owner = THIS_MODULE, | |
708 | .pm = &sm5502_muic_pm_ops, | |
709 | .of_match_table = sm5502_dt_match, | |
710 | }, | |
711 | .probe = sm5022_muic_i2c_probe, | |
712 | .remove = sm5502_muic_i2c_remove, | |
713 | .id_table = sm5502_i2c_id, | |
714 | }; | |
715 | ||
716 | static int __init sm5502_muic_i2c_init(void) | |
717 | { | |
718 | return i2c_add_driver(&sm5502_muic_i2c_driver); | |
719 | } | |
720 | subsys_initcall(sm5502_muic_i2c_init); | |
721 | ||
722 | MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver"); | |
723 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); | |
724 | MODULE_LICENSE("GPL"); |