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