]>
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 | ||
231 | /* Return cable type of attached or detached accessories */ | |
232 | static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info) | |
233 | { | |
234 | unsigned int cable_type = -1, adc, dev_type1; | |
235 | int ret; | |
236 | ||
237 | /* Read ADC value according to external cable or button */ | |
238 | ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); | |
239 | if (ret) { | |
240 | dev_err(info->dev, "failed to read ADC register\n"); | |
241 | return ret; | |
242 | } | |
243 | ||
244 | /* | |
245 | * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't | |
246 | * connected with to MUIC device. | |
247 | */ | |
248 | cable_type &= SM5502_REG_ADC_MASK; | |
249 | if (cable_type == SM5502_MUIC_ADC_GROUND) | |
250 | return SM5502_MUIC_ADC_GROUND; | |
251 | ||
252 | switch (cable_type) { | |
253 | case SM5502_MUIC_ADC_GROUND: | |
254 | case SM5502_MUIC_ADC_SEND_END_BUTTON: | |
255 | case SM5502_MUIC_ADC_REMOTE_S1_BUTTON: | |
256 | case SM5502_MUIC_ADC_REMOTE_S2_BUTTON: | |
257 | case SM5502_MUIC_ADC_REMOTE_S3_BUTTON: | |
258 | case SM5502_MUIC_ADC_REMOTE_S4_BUTTON: | |
259 | case SM5502_MUIC_ADC_REMOTE_S5_BUTTON: | |
260 | case SM5502_MUIC_ADC_REMOTE_S6_BUTTON: | |
261 | case SM5502_MUIC_ADC_REMOTE_S7_BUTTON: | |
262 | case SM5502_MUIC_ADC_REMOTE_S8_BUTTON: | |
263 | case SM5502_MUIC_ADC_REMOTE_S9_BUTTON: | |
264 | case SM5502_MUIC_ADC_REMOTE_S10_BUTTON: | |
265 | case SM5502_MUIC_ADC_REMOTE_S11_BUTTON: | |
266 | case SM5502_MUIC_ADC_REMOTE_S12_BUTTON: | |
267 | case SM5502_MUIC_ADC_RESERVED_ACC_1: | |
268 | case SM5502_MUIC_ADC_RESERVED_ACC_2: | |
269 | case SM5502_MUIC_ADC_RESERVED_ACC_3: | |
270 | case SM5502_MUIC_ADC_RESERVED_ACC_4: | |
271 | case SM5502_MUIC_ADC_RESERVED_ACC_5: | |
272 | case SM5502_MUIC_ADC_AUDIO_TYPE2: | |
273 | case SM5502_MUIC_ADC_PHONE_POWERED_DEV: | |
274 | case SM5502_MUIC_ADC_TTY_CONVERTER: | |
275 | case SM5502_MUIC_ADC_UART_CABLE: | |
276 | case SM5502_MUIC_ADC_TYPE1_CHARGER: | |
277 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: | |
278 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: | |
279 | case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE: | |
280 | case SM5502_MUIC_ADC_TYPE2_CHARGER: | |
281 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: | |
282 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: | |
283 | break; | |
284 | case SM5502_MUIC_ADC_AUDIO_TYPE1: | |
285 | /* | |
286 | * Check whether cable type is | |
287 | * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE | |
288 | * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END | |
289 | * by using Button event. | |
290 | */ | |
291 | break; | |
292 | case SM5502_MUIC_ADC_OPEN: | |
293 | ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, | |
294 | &dev_type1); | |
295 | if (ret) { | |
296 | dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); | |
297 | return ret; | |
298 | } | |
299 | ||
300 | switch (dev_type1) { | |
301 | case SM5502_REG_DEV_TYPE1_USB_SDP_MASK: | |
302 | cable_type = SM5502_MUIC_ADC_OPEN_USB; | |
303 | break; | |
304 | case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK: | |
305 | cable_type = SM5502_MUIC_ADC_OPEN_TA; | |
306 | break; | |
307 | case SM5502_REG_DEV_TYPE1_USB_OTG_MASK: | |
308 | cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG; | |
309 | break; | |
310 | default: | |
311 | dev_dbg(info->dev, | |
312 | "cannot identify the cable type: adc(0x%x) " | |
313 | "dev_type1(0x%x)\n", adc, dev_type1); | |
314 | return -EINVAL; | |
315 | }; | |
316 | break; | |
317 | default: | |
318 | dev_err(info->dev, | |
319 | "failed to identify the cable type: adc(0x%x)\n", adc); | |
320 | return -EINVAL; | |
321 | }; | |
322 | ||
323 | return cable_type; | |
324 | } | |
325 | ||
326 | static int sm5502_muic_cable_handler(struct sm5502_muic_info *info, | |
327 | bool attached) | |
328 | { | |
329 | static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND; | |
330 | const char **cable_names = info->edev->supported_cable; | |
331 | unsigned int cable_type = SM5502_MUIC_ADC_GROUND; | |
332 | unsigned int idx = 0; | |
333 | ||
334 | if (!cable_names) | |
335 | return 0; | |
336 | ||
337 | /* Get the type of attached or detached cable */ | |
338 | if (attached) | |
339 | cable_type = sm5502_muic_get_cable_type(info); | |
340 | else if (!attached) | |
341 | cable_type = prev_cable_type; | |
342 | prev_cable_type = cable_type; | |
343 | ||
344 | switch (cable_type) { | |
345 | case SM5502_MUIC_ADC_OPEN_USB: | |
346 | idx = EXTCON_CABLE_USB; | |
347 | break; | |
348 | case SM5502_MUIC_ADC_OPEN_TA: | |
349 | idx = EXTCON_CABLE_TA; | |
350 | break; | |
351 | case SM5502_MUIC_ADC_OPEN_USB_OTG: | |
352 | idx = EXTCON_CABLE_USB_HOST; | |
353 | break; | |
e1954452 CC |
354 | case SM5502_MUIC_ADC_GROUND: |
355 | break; | |
914b881f CC |
356 | default: |
357 | dev_dbg(info->dev, | |
358 | "cannot handle this cable_type (0x%x)\n", cable_type); | |
359 | return 0; | |
360 | }; | |
361 | ||
362 | extcon_set_cable_state(info->edev, cable_names[idx], attached); | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
367 | static void sm5502_muic_irq_work(struct work_struct *work) | |
368 | { | |
369 | struct sm5502_muic_info *info = container_of(work, | |
370 | struct sm5502_muic_info, irq_work); | |
371 | int ret = 0; | |
372 | ||
373 | if (!info->edev) | |
374 | return; | |
375 | ||
376 | mutex_lock(&info->mutex); | |
377 | ||
378 | /* Detect attached or detached cables */ | |
379 | if (info->irq_attach) { | |
380 | ret = sm5502_muic_cable_handler(info, true); | |
381 | info->irq_attach = false; | |
382 | } | |
383 | if (info->irq_detach) { | |
384 | ret = sm5502_muic_cable_handler(info, false); | |
385 | info->irq_detach = false; | |
386 | } | |
387 | ||
388 | if (ret < 0) | |
389 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); | |
390 | ||
391 | mutex_unlock(&info->mutex); | |
392 | ||
393 | return; | |
394 | } | |
395 | ||
396 | /* | |
397 | * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0. | |
398 | * Returns -ESRCH if irq_type does not match registered IRQ for this dev type. | |
399 | */ | |
400 | static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type) | |
401 | { | |
402 | switch (irq_type) { | |
403 | case SM5502_IRQ_INT1_ATTACH: | |
404 | info->irq_attach = true; | |
405 | break; | |
406 | case SM5502_IRQ_INT1_DETACH: | |
407 | info->irq_detach = true; | |
408 | break; | |
409 | case SM5502_IRQ_INT1_KP: | |
410 | case SM5502_IRQ_INT1_LKP: | |
411 | case SM5502_IRQ_INT1_LKR: | |
412 | case SM5502_IRQ_INT1_OVP_EVENT: | |
413 | case SM5502_IRQ_INT1_OCP_EVENT: | |
414 | case SM5502_IRQ_INT1_OVP_OCP_DIS: | |
415 | case SM5502_IRQ_INT2_VBUS_DET: | |
416 | case SM5502_IRQ_INT2_REV_ACCE: | |
417 | case SM5502_IRQ_INT2_ADC_CHG: | |
418 | case SM5502_IRQ_INT2_STUCK_KEY: | |
419 | case SM5502_IRQ_INT2_STUCK_KEY_RCV: | |
420 | case SM5502_IRQ_INT2_MHL: | |
421 | default: | |
422 | break; | |
423 | } | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | static irqreturn_t sm5502_muic_irq_handler(int irq, void *data) | |
429 | { | |
430 | struct sm5502_muic_info *info = data; | |
431 | int i, irq_type = -1, ret; | |
432 | ||
433 | for (i = 0; i < info->num_muic_irqs; i++) | |
434 | if (irq == info->muic_irqs[i].virq) | |
435 | irq_type = info->muic_irqs[i].irq; | |
436 | ||
437 | ret = sm5502_parse_irq(info, irq_type); | |
438 | if (ret < 0) { | |
439 | dev_warn(info->dev, "cannot handle is interrupt:%d\n", | |
440 | irq_type); | |
441 | return IRQ_HANDLED; | |
442 | } | |
443 | schedule_work(&info->irq_work); | |
444 | ||
445 | return IRQ_HANDLED; | |
446 | } | |
447 | ||
e1954452 CC |
448 | static void sm5502_muic_detect_cable_wq(struct work_struct *work) |
449 | { | |
450 | struct sm5502_muic_info *info = container_of(to_delayed_work(work), | |
451 | struct sm5502_muic_info, wq_detcable); | |
452 | int ret; | |
453 | ||
454 | /* Notify the state of connector cable or not */ | |
455 | ret = sm5502_muic_cable_handler(info, true); | |
456 | if (ret < 0) | |
457 | dev_warn(info->dev, "failed to detect cable state\n"); | |
458 | } | |
459 | ||
914b881f CC |
460 | static void sm5502_init_dev_type(struct sm5502_muic_info *info) |
461 | { | |
462 | unsigned int reg_data, vendor_id, version_id; | |
463 | int i, ret; | |
464 | ||
465 | /* To test I2C, Print version_id and vendor_id of SM5502 */ | |
466 | ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); | |
467 | if (ret) { | |
468 | dev_err(info->dev, | |
469 | "failed to read DEVICE_ID register: %d\n", ret); | |
470 | return; | |
471 | } | |
472 | ||
473 | vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >> | |
474 | SM5502_REG_DEVICE_ID_VENDOR_SHIFT); | |
475 | version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >> | |
476 | SM5502_REG_DEVICE_ID_VERSION_SHIFT); | |
477 | ||
478 | dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", | |
479 | version_id, vendor_id); | |
480 | ||
481 | /* Initiazle the register of SM5502 device to bring-up */ | |
482 | for (i = 0; i < info->num_reg_data; i++) { | |
483 | unsigned int val = 0; | |
484 | ||
485 | if (!info->reg_data[i].invert) | |
486 | val |= ~info->reg_data[i].val; | |
487 | else | |
488 | val = info->reg_data[i].val; | |
489 | regmap_write(info->regmap, info->reg_data[i].reg, val); | |
490 | } | |
491 | } | |
492 | ||
493 | static int sm5022_muic_i2c_probe(struct i2c_client *i2c, | |
494 | const struct i2c_device_id *id) | |
495 | { | |
496 | struct device_node *np = i2c->dev.of_node; | |
497 | struct sm5502_muic_info *info; | |
498 | int i, ret, irq_flags; | |
499 | ||
500 | if (!np) | |
501 | return -EINVAL; | |
502 | ||
503 | info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); | |
504 | if (!info) | |
505 | return -ENOMEM; | |
506 | i2c_set_clientdata(i2c, info); | |
507 | ||
508 | info->dev = &i2c->dev; | |
509 | info->i2c = i2c; | |
510 | info->irq = i2c->irq; | |
511 | info->muic_irqs = sm5502_muic_irqs; | |
512 | info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs); | |
513 | info->reg_data = sm5502_reg_data; | |
514 | info->num_reg_data = ARRAY_SIZE(sm5502_reg_data); | |
515 | ||
516 | mutex_init(&info->mutex); | |
517 | ||
518 | INIT_WORK(&info->irq_work, sm5502_muic_irq_work); | |
519 | ||
520 | info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); | |
521 | if (IS_ERR(info->regmap)) { | |
522 | ret = PTR_ERR(info->regmap); | |
523 | dev_err(info->dev, "failed to allocate register map: %d\n", | |
524 | ret); | |
525 | return ret; | |
526 | } | |
527 | ||
528 | /* Support irq domain for SM5502 MUIC device */ | |
529 | irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; | |
530 | ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0, | |
531 | &sm5502_muic_irq_chip, &info->irq_data); | |
532 | if (ret != 0) { | |
533 | dev_err(info->dev, "failed to request IRQ %d: %d\n", | |
534 | info->irq, ret); | |
535 | return ret; | |
536 | } | |
537 | ||
538 | for (i = 0; i < info->num_muic_irqs; i++) { | |
539 | struct muic_irq *muic_irq = &info->muic_irqs[i]; | |
540 | unsigned int virq = 0; | |
541 | ||
542 | virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); | |
543 | if (virq <= 0) | |
544 | return -EINVAL; | |
545 | muic_irq->virq = virq; | |
546 | ||
547 | ret = devm_request_threaded_irq(info->dev, virq, NULL, | |
548 | sm5502_muic_irq_handler, | |
549 | IRQF_NO_SUSPEND, | |
550 | muic_irq->name, info); | |
551 | if (ret) { | |
552 | dev_err(info->dev, "failed: irq request (IRQ: %d," | |
553 | " error :%d)\n", muic_irq->irq, ret); | |
554 | return ret; | |
555 | } | |
556 | } | |
557 | ||
558 | /* Allocate extcon device */ | |
559 | info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); | |
560 | if (IS_ERR(info->edev)) { | |
561 | dev_err(info->dev, "failed to allocate memory for extcon\n"); | |
562 | return -ENOMEM; | |
563 | } | |
564 | info->edev->name = np->name; | |
565 | ||
566 | /* Register extcon device */ | |
567 | ret = devm_extcon_dev_register(info->dev, info->edev); | |
568 | if (ret) { | |
569 | dev_err(info->dev, "failed to register extcon device\n"); | |
570 | return ret; | |
571 | } | |
572 | ||
e1954452 CC |
573 | /* |
574 | * Detect accessory after completing the initialization of platform | |
575 | * | |
576 | * - Use delayed workqueue to detect cable state and then | |
577 | * notify cable state to notifiee/platform through uevent. | |
578 | * After completing the booting of platform, the extcon provider | |
579 | * driver should notify cable state to upper layer. | |
580 | */ | |
581 | INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq); | |
582 | queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, | |
583 | msecs_to_jiffies(DELAY_MS_DEFAULT)); | |
584 | ||
914b881f CC |
585 | /* Initialize SM5502 device and print vendor id and version id */ |
586 | sm5502_init_dev_type(info); | |
587 | ||
588 | return 0; | |
589 | } | |
590 | ||
591 | static int sm5502_muic_i2c_remove(struct i2c_client *i2c) | |
592 | { | |
593 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | |
594 | ||
595 | regmap_del_irq_chip(info->irq, info->irq_data); | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
600 | static struct of_device_id sm5502_dt_match[] = { | |
601 | { .compatible = "siliconmitus,sm5502-muic" }, | |
602 | { }, | |
603 | }; | |
604 | ||
605 | #ifdef CONFIG_PM_SLEEP | |
606 | static int sm5502_muic_suspend(struct device *dev) | |
607 | { | |
608 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); | |
609 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | |
610 | ||
611 | enable_irq_wake(info->irq); | |
612 | ||
613 | return 0; | |
614 | } | |
615 | ||
616 | static int sm5502_muic_resume(struct device *dev) | |
617 | { | |
618 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); | |
619 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | |
620 | ||
621 | disable_irq_wake(info->irq); | |
622 | ||
623 | return 0; | |
624 | } | |
625 | #endif | |
626 | ||
627 | static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops, | |
628 | sm5502_muic_suspend, sm5502_muic_resume); | |
629 | ||
630 | static const struct i2c_device_id sm5502_i2c_id[] = { | |
631 | { "sm5502", TYPE_SM5502 }, | |
632 | { } | |
633 | }; | |
634 | MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id); | |
635 | ||
636 | static struct i2c_driver sm5502_muic_i2c_driver = { | |
637 | .driver = { | |
638 | .name = "sm5502", | |
639 | .owner = THIS_MODULE, | |
640 | .pm = &sm5502_muic_pm_ops, | |
641 | .of_match_table = sm5502_dt_match, | |
642 | }, | |
643 | .probe = sm5022_muic_i2c_probe, | |
644 | .remove = sm5502_muic_i2c_remove, | |
645 | .id_table = sm5502_i2c_id, | |
646 | }; | |
647 | ||
648 | static int __init sm5502_muic_i2c_init(void) | |
649 | { | |
650 | return i2c_add_driver(&sm5502_muic_i2c_driver); | |
651 | } | |
652 | subsys_initcall(sm5502_muic_i2c_init); | |
653 | ||
654 | MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver"); | |
655 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); | |
656 | MODULE_LICENSE("GPL"); |