diff options
author | Chanwoo Choi <cw00.choi@samsung.com> | 2014-05-22 01:06:33 -0400 |
---|---|---|
committer | Chanwoo Choi <cw00.choi@samsung.com> | 2014-07-22 21:22:30 -0400 |
commit | 914b881f9452fd615cc597b434fd8c0e12a7dae2 (patch) | |
tree | 00ca6bc5c90cca061921ef72b60847db3c38e5a9 /drivers/extcon/extcon-sm5502.c | |
parent | 17271f608bf818a13b2c235d45258311308d5b03 (diff) |
extcon: sm5502: Add support new SM5502 extcon device driver
This patch add new SM5502 MUIC(Micro-USB Interface Controller) device by using
EXTCON subsystem. The extcon-sm5502 driver is capable of identifying the type
of the external power source and attached accessory. An external power sources,
such as Deticated Charger or a standard USB port, are able to charge the battery
in the smart phone via the connector.
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
Diffstat (limited to 'drivers/extcon/extcon-sm5502.c')
-rw-r--r-- | drivers/extcon/extcon-sm5502.c | 620 |
1 files changed, 620 insertions, 0 deletions
diff --git a/drivers/extcon/extcon-sm5502.c b/drivers/extcon/extcon-sm5502.c new file mode 100644 index 000000000000..9f318c222b89 --- /dev/null +++ b/drivers/extcon/extcon-sm5502.c | |||
@@ -0,0 +1,620 @@ | |||
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 | |||
31 | struct muic_irq { | ||
32 | unsigned int irq; | ||
33 | const char *name; | ||
34 | unsigned int virq; | ||
35 | }; | ||
36 | |||
37 | struct reg_data { | ||
38 | u8 reg; | ||
39 | unsigned int val; | ||
40 | bool invert; | ||
41 | }; | ||
42 | |||
43 | struct sm5502_muic_info { | ||
44 | struct device *dev; | ||
45 | struct extcon_dev *edev; | ||
46 | |||
47 | struct i2c_client *i2c; | ||
48 | struct regmap *regmap; | ||
49 | |||
50 | struct regmap_irq_chip_data *irq_data; | ||
51 | struct muic_irq *muic_irqs; | ||
52 | unsigned int num_muic_irqs; | ||
53 | int irq; | ||
54 | bool irq_attach; | ||
55 | bool irq_detach; | ||
56 | struct work_struct irq_work; | ||
57 | |||
58 | struct reg_data *reg_data; | ||
59 | unsigned int num_reg_data; | ||
60 | |||
61 | struct mutex mutex; | ||
62 | }; | ||
63 | |||
64 | /* Default value of SM5502 register to bring up MUIC device. */ | ||
65 | static struct reg_data sm5502_reg_data[] = { | ||
66 | { | ||
67 | .reg = SM5502_REG_CONTROL, | ||
68 | .val = SM5502_REG_CONTROL_MASK_INT_MASK, | ||
69 | .invert = false, | ||
70 | }, { | ||
71 | .reg = SM5502_REG_INTMASK1, | ||
72 | .val = SM5502_REG_INTM1_KP_MASK | ||
73 | | SM5502_REG_INTM1_LKP_MASK | ||
74 | | SM5502_REG_INTM1_LKR_MASK, | ||
75 | .invert = true, | ||
76 | }, { | ||
77 | .reg = SM5502_REG_INTMASK2, | ||
78 | .val = SM5502_REG_INTM2_VBUS_DET_MASK | ||
79 | | SM5502_REG_INTM2_REV_ACCE_MASK | ||
80 | | SM5502_REG_INTM2_ADC_CHG_MASK | ||
81 | | SM5502_REG_INTM2_STUCK_KEY_MASK | ||
82 | | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK | ||
83 | | SM5502_REG_INTM2_MHL_MASK, | ||
84 | .invert = true, | ||
85 | }, | ||
86 | { } | ||
87 | }; | ||
88 | |||
89 | /* List of detectable cables */ | ||
90 | enum { | ||
91 | EXTCON_CABLE_USB = 0, | ||
92 | EXTCON_CABLE_USB_HOST, | ||
93 | EXTCON_CABLE_TA, | ||
94 | |||
95 | EXTCON_CABLE_END, | ||
96 | }; | ||
97 | |||
98 | static const char *sm5502_extcon_cable[] = { | ||
99 | [EXTCON_CABLE_USB] = "USB", | ||
100 | [EXTCON_CABLE_USB_HOST] = "USB-Host", | ||
101 | [EXTCON_CABLE_TA] = "TA", | ||
102 | NULL, | ||
103 | }; | ||
104 | |||
105 | /* Define supported accessory type */ | ||
106 | enum sm5502_muic_acc_type { | ||
107 | SM5502_MUIC_ADC_GROUND = 0x0, | ||
108 | SM5502_MUIC_ADC_SEND_END_BUTTON, | ||
109 | SM5502_MUIC_ADC_REMOTE_S1_BUTTON, | ||
110 | SM5502_MUIC_ADC_REMOTE_S2_BUTTON, | ||
111 | SM5502_MUIC_ADC_REMOTE_S3_BUTTON, | ||
112 | SM5502_MUIC_ADC_REMOTE_S4_BUTTON, | ||
113 | SM5502_MUIC_ADC_REMOTE_S5_BUTTON, | ||
114 | SM5502_MUIC_ADC_REMOTE_S6_BUTTON, | ||
115 | SM5502_MUIC_ADC_REMOTE_S7_BUTTON, | ||
116 | SM5502_MUIC_ADC_REMOTE_S8_BUTTON, | ||
117 | SM5502_MUIC_ADC_REMOTE_S9_BUTTON, | ||
118 | SM5502_MUIC_ADC_REMOTE_S10_BUTTON, | ||
119 | SM5502_MUIC_ADC_REMOTE_S11_BUTTON, | ||
120 | SM5502_MUIC_ADC_REMOTE_S12_BUTTON, | ||
121 | SM5502_MUIC_ADC_RESERVED_ACC_1, | ||
122 | SM5502_MUIC_ADC_RESERVED_ACC_2, | ||
123 | SM5502_MUIC_ADC_RESERVED_ACC_3, | ||
124 | SM5502_MUIC_ADC_RESERVED_ACC_4, | ||
125 | SM5502_MUIC_ADC_RESERVED_ACC_5, | ||
126 | SM5502_MUIC_ADC_AUDIO_TYPE2, | ||
127 | SM5502_MUIC_ADC_PHONE_POWERED_DEV, | ||
128 | SM5502_MUIC_ADC_TTY_CONVERTER, | ||
129 | SM5502_MUIC_ADC_UART_CABLE, | ||
130 | SM5502_MUIC_ADC_TYPE1_CHARGER, | ||
131 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB, | ||
132 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB, | ||
133 | SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE, | ||
134 | SM5502_MUIC_ADC_TYPE2_CHARGER, | ||
135 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART, | ||
136 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART, | ||
137 | SM5502_MUIC_ADC_AUDIO_TYPE1, | ||
138 | SM5502_MUIC_ADC_OPEN = 0x1f, | ||
139 | |||
140 | /* The below accessories have same ADC value (0x1f or 0x1e). | ||
141 | So, Device type1 is used to separate specific accessory. */ | ||
142 | /* |---------|--ADC| */ | ||
143 | /* | [7:5]|[4:0]| */ | ||
144 | SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */ | ||
145 | SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */ | ||
146 | /* |Dev Type1|--ADC| */ | ||
147 | SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */ | ||
148 | SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */ | ||
149 | SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */ | ||
150 | }; | ||
151 | |||
152 | /* List of supported interrupt for SM5502 */ | ||
153 | static struct muic_irq sm5502_muic_irqs[] = { | ||
154 | { SM5502_IRQ_INT1_ATTACH, "muic-attach" }, | ||
155 | { SM5502_IRQ_INT1_DETACH, "muic-detach" }, | ||
156 | { SM5502_IRQ_INT1_KP, "muic-kp" }, | ||
157 | { SM5502_IRQ_INT1_LKP, "muic-lkp" }, | ||
158 | { SM5502_IRQ_INT1_LKR, "muic-lkr" }, | ||
159 | { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" }, | ||
160 | { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" }, | ||
161 | { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" }, | ||
162 | { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" }, | ||
163 | { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" }, | ||
164 | { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" }, | ||
165 | { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" }, | ||
166 | { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" }, | ||
167 | { SM5502_IRQ_INT2_MHL, "muic-mhl" }, | ||
168 | }; | ||
169 | |||
170 | /* Define interrupt list of SM5502 to register regmap_irq */ | ||
171 | static const struct regmap_irq sm5502_irqs[] = { | ||
172 | /* INT1 interrupts */ | ||
173 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, }, | ||
174 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, }, | ||
175 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, }, | ||
176 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, }, | ||
177 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, }, | ||
178 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, }, | ||
179 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, }, | ||
180 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, }, | ||
181 | |||
182 | /* INT2 interrupts */ | ||
183 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,}, | ||
184 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, }, | ||
185 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, }, | ||
186 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, }, | ||
187 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, }, | ||
188 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, }, | ||
189 | }; | ||
190 | |||
191 | static const struct regmap_irq_chip sm5502_muic_irq_chip = { | ||
192 | .name = "sm5502", | ||
193 | .status_base = SM5502_REG_INT1, | ||
194 | .mask_base = SM5502_REG_INTMASK1, | ||
195 | .mask_invert = false, | ||
196 | .num_regs = 2, | ||
197 | .irqs = sm5502_irqs, | ||
198 | .num_irqs = ARRAY_SIZE(sm5502_irqs), | ||
199 | }; | ||
200 | |||
201 | /* Define regmap configuration of SM5502 for I2C communication */ | ||
202 | static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg) | ||
203 | { | ||
204 | switch (reg) { | ||
205 | case SM5502_REG_INTMASK1: | ||
206 | case SM5502_REG_INTMASK2: | ||
207 | return true; | ||
208 | default: | ||
209 | break; | ||
210 | } | ||
211 | return false; | ||
212 | } | ||
213 | |||
214 | static const struct regmap_config sm5502_muic_regmap_config = { | ||
215 | .reg_bits = 8, | ||
216 | .val_bits = 8, | ||
217 | .volatile_reg = sm5502_muic_volatile_reg, | ||
218 | .max_register = SM5502_REG_END, | ||
219 | }; | ||
220 | |||
221 | /* Return cable type of attached or detached accessories */ | ||
222 | static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info) | ||
223 | { | ||
224 | unsigned int cable_type = -1, adc, dev_type1; | ||
225 | int ret; | ||
226 | |||
227 | /* Read ADC value according to external cable or button */ | ||
228 | ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); | ||
229 | if (ret) { | ||
230 | dev_err(info->dev, "failed to read ADC register\n"); | ||
231 | return ret; | ||
232 | } | ||
233 | |||
234 | /* | ||
235 | * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't | ||
236 | * connected with to MUIC device. | ||
237 | */ | ||
238 | cable_type &= SM5502_REG_ADC_MASK; | ||
239 | if (cable_type == SM5502_MUIC_ADC_GROUND) | ||
240 | return SM5502_MUIC_ADC_GROUND; | ||
241 | |||
242 | switch (cable_type) { | ||
243 | case SM5502_MUIC_ADC_GROUND: | ||
244 | case SM5502_MUIC_ADC_SEND_END_BUTTON: | ||
245 | case SM5502_MUIC_ADC_REMOTE_S1_BUTTON: | ||
246 | case SM5502_MUIC_ADC_REMOTE_S2_BUTTON: | ||
247 | case SM5502_MUIC_ADC_REMOTE_S3_BUTTON: | ||
248 | case SM5502_MUIC_ADC_REMOTE_S4_BUTTON: | ||
249 | case SM5502_MUIC_ADC_REMOTE_S5_BUTTON: | ||
250 | case SM5502_MUIC_ADC_REMOTE_S6_BUTTON: | ||
251 | case SM5502_MUIC_ADC_REMOTE_S7_BUTTON: | ||
252 | case SM5502_MUIC_ADC_REMOTE_S8_BUTTON: | ||
253 | case SM5502_MUIC_ADC_REMOTE_S9_BUTTON: | ||
254 | case SM5502_MUIC_ADC_REMOTE_S10_BUTTON: | ||
255 | case SM5502_MUIC_ADC_REMOTE_S11_BUTTON: | ||
256 | case SM5502_MUIC_ADC_REMOTE_S12_BUTTON: | ||
257 | case SM5502_MUIC_ADC_RESERVED_ACC_1: | ||
258 | case SM5502_MUIC_ADC_RESERVED_ACC_2: | ||
259 | case SM5502_MUIC_ADC_RESERVED_ACC_3: | ||
260 | case SM5502_MUIC_ADC_RESERVED_ACC_4: | ||
261 | case SM5502_MUIC_ADC_RESERVED_ACC_5: | ||
262 | case SM5502_MUIC_ADC_AUDIO_TYPE2: | ||
263 | case SM5502_MUIC_ADC_PHONE_POWERED_DEV: | ||
264 | case SM5502_MUIC_ADC_TTY_CONVERTER: | ||
265 | case SM5502_MUIC_ADC_UART_CABLE: | ||
266 | case SM5502_MUIC_ADC_TYPE1_CHARGER: | ||
267 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: | ||
268 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: | ||
269 | case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE: | ||
270 | case SM5502_MUIC_ADC_TYPE2_CHARGER: | ||
271 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: | ||
272 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: | ||
273 | break; | ||
274 | case SM5502_MUIC_ADC_AUDIO_TYPE1: | ||
275 | /* | ||
276 | * Check whether cable type is | ||
277 | * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE | ||
278 | * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END | ||
279 | * by using Button event. | ||
280 | */ | ||
281 | break; | ||
282 | case SM5502_MUIC_ADC_OPEN: | ||
283 | ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, | ||
284 | &dev_type1); | ||
285 | if (ret) { | ||
286 | dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); | ||
287 | return ret; | ||
288 | } | ||
289 | |||
290 | switch (dev_type1) { | ||
291 | case SM5502_REG_DEV_TYPE1_USB_SDP_MASK: | ||
292 | cable_type = SM5502_MUIC_ADC_OPEN_USB; | ||
293 | break; | ||
294 | case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK: | ||
295 | cable_type = SM5502_MUIC_ADC_OPEN_TA; | ||
296 | break; | ||
297 | case SM5502_REG_DEV_TYPE1_USB_OTG_MASK: | ||
298 | cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG; | ||
299 | break; | ||
300 | default: | ||
301 | dev_dbg(info->dev, | ||
302 | "cannot identify the cable type: adc(0x%x) " | ||
303 | "dev_type1(0x%x)\n", adc, dev_type1); | ||
304 | return -EINVAL; | ||
305 | }; | ||
306 | break; | ||
307 | default: | ||
308 | dev_err(info->dev, | ||
309 | "failed to identify the cable type: adc(0x%x)\n", adc); | ||
310 | return -EINVAL; | ||
311 | }; | ||
312 | |||
313 | return cable_type; | ||
314 | } | ||
315 | |||
316 | static int sm5502_muic_cable_handler(struct sm5502_muic_info *info, | ||
317 | bool attached) | ||
318 | { | ||
319 | static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND; | ||
320 | const char **cable_names = info->edev->supported_cable; | ||
321 | unsigned int cable_type = SM5502_MUIC_ADC_GROUND; | ||
322 | unsigned int idx = 0; | ||
323 | |||
324 | if (!cable_names) | ||
325 | return 0; | ||
326 | |||
327 | /* Get the type of attached or detached cable */ | ||
328 | if (attached) | ||
329 | cable_type = sm5502_muic_get_cable_type(info); | ||
330 | else if (!attached) | ||
331 | cable_type = prev_cable_type; | ||
332 | prev_cable_type = cable_type; | ||
333 | |||
334 | switch (cable_type) { | ||
335 | case SM5502_MUIC_ADC_OPEN_USB: | ||
336 | idx = EXTCON_CABLE_USB; | ||
337 | break; | ||
338 | case SM5502_MUIC_ADC_OPEN_TA: | ||
339 | idx = EXTCON_CABLE_TA; | ||
340 | break; | ||
341 | case SM5502_MUIC_ADC_OPEN_USB_OTG: | ||
342 | idx = EXTCON_CABLE_USB_HOST; | ||
343 | break; | ||
344 | default: | ||
345 | dev_dbg(info->dev, | ||
346 | "cannot handle this cable_type (0x%x)\n", cable_type); | ||
347 | return 0; | ||
348 | }; | ||
349 | |||
350 | extcon_set_cable_state(info->edev, cable_names[idx], attached); | ||
351 | |||
352 | return 0; | ||
353 | } | ||
354 | |||
355 | static void sm5502_muic_irq_work(struct work_struct *work) | ||
356 | { | ||
357 | struct sm5502_muic_info *info = container_of(work, | ||
358 | struct sm5502_muic_info, irq_work); | ||
359 | int ret = 0; | ||
360 | |||
361 | if (!info->edev) | ||
362 | return; | ||
363 | |||
364 | mutex_lock(&info->mutex); | ||
365 | |||
366 | /* Detect attached or detached cables */ | ||
367 | if (info->irq_attach) { | ||
368 | ret = sm5502_muic_cable_handler(info, true); | ||
369 | info->irq_attach = false; | ||
370 | } | ||
371 | if (info->irq_detach) { | ||
372 | ret = sm5502_muic_cable_handler(info, false); | ||
373 | info->irq_detach = false; | ||
374 | } | ||
375 | |||
376 | if (ret < 0) | ||
377 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); | ||
378 | |||
379 | mutex_unlock(&info->mutex); | ||
380 | |||
381 | return; | ||
382 | } | ||
383 | |||
384 | /* | ||
385 | * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0. | ||
386 | * Returns -ESRCH if irq_type does not match registered IRQ for this dev type. | ||
387 | */ | ||
388 | static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type) | ||
389 | { | ||
390 | switch (irq_type) { | ||
391 | case SM5502_IRQ_INT1_ATTACH: | ||
392 | info->irq_attach = true; | ||
393 | break; | ||
394 | case SM5502_IRQ_INT1_DETACH: | ||
395 | info->irq_detach = true; | ||
396 | break; | ||
397 | case SM5502_IRQ_INT1_KP: | ||
398 | case SM5502_IRQ_INT1_LKP: | ||
399 | case SM5502_IRQ_INT1_LKR: | ||
400 | case SM5502_IRQ_INT1_OVP_EVENT: | ||
401 | case SM5502_IRQ_INT1_OCP_EVENT: | ||
402 | case SM5502_IRQ_INT1_OVP_OCP_DIS: | ||
403 | case SM5502_IRQ_INT2_VBUS_DET: | ||
404 | case SM5502_IRQ_INT2_REV_ACCE: | ||
405 | case SM5502_IRQ_INT2_ADC_CHG: | ||
406 | case SM5502_IRQ_INT2_STUCK_KEY: | ||
407 | case SM5502_IRQ_INT2_STUCK_KEY_RCV: | ||
408 | case SM5502_IRQ_INT2_MHL: | ||
409 | default: | ||
410 | break; | ||
411 | } | ||
412 | |||
413 | return 0; | ||
414 | } | ||
415 | |||
416 | static irqreturn_t sm5502_muic_irq_handler(int irq, void *data) | ||
417 | { | ||
418 | struct sm5502_muic_info *info = data; | ||
419 | int i, irq_type = -1, ret; | ||
420 | |||
421 | for (i = 0; i < info->num_muic_irqs; i++) | ||
422 | if (irq == info->muic_irqs[i].virq) | ||
423 | irq_type = info->muic_irqs[i].irq; | ||
424 | |||
425 | ret = sm5502_parse_irq(info, irq_type); | ||
426 | if (ret < 0) { | ||
427 | dev_warn(info->dev, "cannot handle is interrupt:%d\n", | ||
428 | irq_type); | ||
429 | return IRQ_HANDLED; | ||
430 | } | ||
431 | schedule_work(&info->irq_work); | ||
432 | |||
433 | return IRQ_HANDLED; | ||
434 | } | ||
435 | |||
436 | static void sm5502_init_dev_type(struct sm5502_muic_info *info) | ||
437 | { | ||
438 | unsigned int reg_data, vendor_id, version_id; | ||
439 | int i, ret; | ||
440 | |||
441 | /* To test I2C, Print version_id and vendor_id of SM5502 */ | ||
442 | ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); | ||
443 | if (ret) { | ||
444 | dev_err(info->dev, | ||
445 | "failed to read DEVICE_ID register: %d\n", ret); | ||
446 | return; | ||
447 | } | ||
448 | |||
449 | vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >> | ||
450 | SM5502_REG_DEVICE_ID_VENDOR_SHIFT); | ||
451 | version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >> | ||
452 | SM5502_REG_DEVICE_ID_VERSION_SHIFT); | ||
453 | |||
454 | dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", | ||
455 | version_id, vendor_id); | ||
456 | |||
457 | /* Initiazle the register of SM5502 device to bring-up */ | ||
458 | for (i = 0; i < info->num_reg_data; i++) { | ||
459 | unsigned int val = 0; | ||
460 | |||
461 | if (!info->reg_data[i].invert) | ||
462 | val |= ~info->reg_data[i].val; | ||
463 | else | ||
464 | val = info->reg_data[i].val; | ||
465 | regmap_write(info->regmap, info->reg_data[i].reg, val); | ||
466 | } | ||
467 | } | ||
468 | |||
469 | static int sm5022_muic_i2c_probe(struct i2c_client *i2c, | ||
470 | const struct i2c_device_id *id) | ||
471 | { | ||
472 | struct device_node *np = i2c->dev.of_node; | ||
473 | struct sm5502_muic_info *info; | ||
474 | int i, ret, irq_flags; | ||
475 | |||
476 | if (!np) | ||
477 | return -EINVAL; | ||
478 | |||
479 | info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); | ||
480 | if (!info) | ||
481 | return -ENOMEM; | ||
482 | i2c_set_clientdata(i2c, info); | ||
483 | |||
484 | info->dev = &i2c->dev; | ||
485 | info->i2c = i2c; | ||
486 | info->irq = i2c->irq; | ||
487 | info->muic_irqs = sm5502_muic_irqs; | ||
488 | info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs); | ||
489 | info->reg_data = sm5502_reg_data; | ||
490 | info->num_reg_data = ARRAY_SIZE(sm5502_reg_data); | ||
491 | |||
492 | mutex_init(&info->mutex); | ||
493 | |||
494 | INIT_WORK(&info->irq_work, sm5502_muic_irq_work); | ||
495 | |||
496 | info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); | ||
497 | if (IS_ERR(info->regmap)) { | ||
498 | ret = PTR_ERR(info->regmap); | ||
499 | dev_err(info->dev, "failed to allocate register map: %d\n", | ||
500 | ret); | ||
501 | return ret; | ||
502 | } | ||
503 | |||
504 | /* Support irq domain for SM5502 MUIC device */ | ||
505 | irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; | ||
506 | ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0, | ||
507 | &sm5502_muic_irq_chip, &info->irq_data); | ||
508 | if (ret != 0) { | ||
509 | dev_err(info->dev, "failed to request IRQ %d: %d\n", | ||
510 | info->irq, ret); | ||
511 | return ret; | ||
512 | } | ||
513 | |||
514 | for (i = 0; i < info->num_muic_irqs; i++) { | ||
515 | struct muic_irq *muic_irq = &info->muic_irqs[i]; | ||
516 | unsigned int virq = 0; | ||
517 | |||
518 | virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); | ||
519 | if (virq <= 0) | ||
520 | return -EINVAL; | ||
521 | muic_irq->virq = virq; | ||
522 | |||
523 | ret = devm_request_threaded_irq(info->dev, virq, NULL, | ||
524 | sm5502_muic_irq_handler, | ||
525 | IRQF_NO_SUSPEND, | ||
526 | muic_irq->name, info); | ||
527 | if (ret) { | ||
528 | dev_err(info->dev, "failed: irq request (IRQ: %d," | ||
529 | " error :%d)\n", muic_irq->irq, ret); | ||
530 | return ret; | ||
531 | } | ||
532 | } | ||
533 | |||
534 | /* Allocate extcon device */ | ||
535 | info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); | ||
536 | if (IS_ERR(info->edev)) { | ||
537 | dev_err(info->dev, "failed to allocate memory for extcon\n"); | ||
538 | return -ENOMEM; | ||
539 | } | ||
540 | info->edev->name = np->name; | ||
541 | |||
542 | /* Register extcon device */ | ||
543 | ret = devm_extcon_dev_register(info->dev, info->edev); | ||
544 | if (ret) { | ||
545 | dev_err(info->dev, "failed to register extcon device\n"); | ||
546 | return ret; | ||
547 | } | ||
548 | |||
549 | /* Initialize SM5502 device and print vendor id and version id */ | ||
550 | sm5502_init_dev_type(info); | ||
551 | |||
552 | return 0; | ||
553 | } | ||
554 | |||
555 | static int sm5502_muic_i2c_remove(struct i2c_client *i2c) | ||
556 | { | ||
557 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | ||
558 | |||
559 | regmap_del_irq_chip(info->irq, info->irq_data); | ||
560 | |||
561 | return 0; | ||
562 | } | ||
563 | |||
564 | static struct of_device_id sm5502_dt_match[] = { | ||
565 | { .compatible = "siliconmitus,sm5502-muic" }, | ||
566 | { }, | ||
567 | }; | ||
568 | |||
569 | #ifdef CONFIG_PM_SLEEP | ||
570 | static int sm5502_muic_suspend(struct device *dev) | ||
571 | { | ||
572 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); | ||
573 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | ||
574 | |||
575 | enable_irq_wake(info->irq); | ||
576 | |||
577 | return 0; | ||
578 | } | ||
579 | |||
580 | static int sm5502_muic_resume(struct device *dev) | ||
581 | { | ||
582 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); | ||
583 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); | ||
584 | |||
585 | disable_irq_wake(info->irq); | ||
586 | |||
587 | return 0; | ||
588 | } | ||
589 | #endif | ||
590 | |||
591 | static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops, | ||
592 | sm5502_muic_suspend, sm5502_muic_resume); | ||
593 | |||
594 | static const struct i2c_device_id sm5502_i2c_id[] = { | ||
595 | { "sm5502", TYPE_SM5502 }, | ||
596 | { } | ||
597 | }; | ||
598 | MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id); | ||
599 | |||
600 | static struct i2c_driver sm5502_muic_i2c_driver = { | ||
601 | .driver = { | ||
602 | .name = "sm5502", | ||
603 | .owner = THIS_MODULE, | ||
604 | .pm = &sm5502_muic_pm_ops, | ||
605 | .of_match_table = sm5502_dt_match, | ||
606 | }, | ||
607 | .probe = sm5022_muic_i2c_probe, | ||
608 | .remove = sm5502_muic_i2c_remove, | ||
609 | .id_table = sm5502_i2c_id, | ||
610 | }; | ||
611 | |||
612 | static int __init sm5502_muic_i2c_init(void) | ||
613 | { | ||
614 | return i2c_add_driver(&sm5502_muic_i2c_driver); | ||
615 | } | ||
616 | subsys_initcall(sm5502_muic_i2c_init); | ||
617 | |||
618 | MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver"); | ||
619 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); | ||
620 | MODULE_LICENSE("GPL"); | ||