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