Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * extcon-rt8973a.c - Richtek RT8973A extcon driver to support USB switches
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
7 */
8
9#include <linux/err.h>
10#include <linux/i2c.h>
11#include <linux/input.h>
12#include <linux/interrupt.h>
13#include <linux/irqdomain.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/regmap.h>
18#include <linux/slab.h>
19#include <linux/extcon-provider.h>
20
21#include "extcon-rt8973a.h"
22
23#define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
24
25struct muic_irq {
26 unsigned int irq;
27 const char *name;
28 unsigned int virq;
29};
30
31struct reg_data {
32 u8 reg;
33 u8 mask;
34 u8 val;
35 bool invert;
36};
37
38struct rt8973a_muic_info {
39 struct device *dev;
40 struct extcon_dev *edev;
41
42 struct i2c_client *i2c;
43 struct regmap *regmap;
44
45 struct regmap_irq_chip_data *irq_data;
46 struct muic_irq *muic_irqs;
47 unsigned int num_muic_irqs;
48 int irq;
49 bool irq_attach;
50 bool irq_detach;
51 bool irq_ovp;
52 bool irq_otp;
53 struct work_struct irq_work;
54
55 struct reg_data *reg_data;
56 unsigned int num_reg_data;
57 bool auto_config;
58
59 struct mutex mutex;
60
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;
68};
69
70/* Default value of RT8973A register to bring up MUIC device. */
71static struct reg_data rt8973a_reg_data[] = {
72 {
73 .reg = RT8973A_REG_CONTROL1,
74 .mask = RT8973A_REG_CONTROL1_ADC_EN_MASK
75 | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
76 | RT8973A_REG_CONTROL1_CHGTYP_MASK
77 | RT8973A_REG_CONTROL1_SWITCH_OPEN_MASK
78 | RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK
79 | RT8973A_REG_CONTROL1_INTM_MASK,
80 .val = RT8973A_REG_CONTROL1_ADC_EN_MASK
81 | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
82 | RT8973A_REG_CONTROL1_CHGTYP_MASK,
83 .invert = false,
84 },
85 { /* sentinel */ }
86};
87
88/* List of detectable cables */
89static const unsigned int rt8973a_extcon_cable[] = {
90 EXTCON_USB,
91 EXTCON_USB_HOST,
92 EXTCON_CHG_USB_SDP,
93 EXTCON_CHG_USB_DCP,
94 EXTCON_JIG,
95 EXTCON_NONE,
96};
97
98/* Define OVP (Over Voltage Protection), OTP (Over Temperature Protection) */
99enum rt8973a_event_type {
100 RT8973A_EVENT_ATTACH = 1,
101 RT8973A_EVENT_DETACH,
102 RT8973A_EVENT_OVP,
103 RT8973A_EVENT_OTP,
104};
105
106/* Define supported accessory type */
107enum rt8973a_muic_acc_type {
108 RT8973A_MUIC_ADC_OTG = 0x0,
109 RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON,
110 RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON,
111 RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON,
112 RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON,
113 RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON,
114 RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON,
115 RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON,
116 RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON,
117 RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON,
118 RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON,
119 RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON,
120 RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON,
121 RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON,
122 RT8973A_MUIC_ADC_RESERVED_ACC_1,
123 RT8973A_MUIC_ADC_RESERVED_ACC_2,
124 RT8973A_MUIC_ADC_RESERVED_ACC_3,
125 RT8973A_MUIC_ADC_RESERVED_ACC_4,
126 RT8973A_MUIC_ADC_RESERVED_ACC_5,
127 RT8973A_MUIC_ADC_AUDIO_TYPE2,
128 RT8973A_MUIC_ADC_PHONE_POWERED_DEV,
129 RT8973A_MUIC_ADC_UNKNOWN_ACC_1,
130 RT8973A_MUIC_ADC_UNKNOWN_ACC_2,
131 RT8973A_MUIC_ADC_TA,
132 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
133 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
134 RT8973A_MUIC_ADC_UNKNOWN_ACC_3,
135 RT8973A_MUIC_ADC_UNKNOWN_ACC_4,
136 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
137 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
138 RT8973A_MUIC_ADC_UNKNOWN_ACC_5,
139 RT8973A_MUIC_ADC_OPEN = 0x1f,
140
141 /*
142 * The below accessories has same ADC value (0x1f).
143 * So, Device type1 is used to separate specific accessory.
144 */
145 /* |---------|--ADC| */
146 /* | [7:5]|[4:0]| */
147 RT8973A_MUIC_ADC_USB = 0x3f, /* | 001|11111| */
148};
149
150/* List of supported interrupt for RT8973A */
151static struct muic_irq rt8973a_muic_irqs[] = {
152 { RT8973A_INT1_ATTACH, "muic-attach" },
153 { RT8973A_INT1_DETACH, "muic-detach" },
154 { RT8973A_INT1_CHGDET, "muic-chgdet" },
155 { RT8973A_INT1_DCD_T, "muic-dcd-t" },
156 { RT8973A_INT1_OVP, "muic-ovp" },
157 { RT8973A_INT1_CONNECT, "muic-connect" },
158 { RT8973A_INT1_ADC_CHG, "muic-adc-chg" },
159 { RT8973A_INT1_OTP, "muic-otp" },
160 { RT8973A_INT2_UVLO, "muic-uvlo" },
161 { RT8973A_INT2_POR, "muic-por" },
162 { RT8973A_INT2_OTP_FET, "muic-otp-fet" },
163 { RT8973A_INT2_OVP_FET, "muic-ovp-fet" },
164 { RT8973A_INT2_OCP_LATCH, "muic-ocp-latch" },
165 { RT8973A_INT2_OCP, "muic-ocp" },
166 { RT8973A_INT2_OVP_OCP, "muic-ovp-ocp" },
167};
168
169/* Define interrupt list of RT8973A to register regmap_irq */
170static const struct regmap_irq rt8973a_irqs[] = {
171 /* INT1 interrupts */
172 { .reg_offset = 0, .mask = RT8973A_INT1_ATTACH_MASK, },
173 { .reg_offset = 0, .mask = RT8973A_INT1_DETACH_MASK, },
174 { .reg_offset = 0, .mask = RT8973A_INT1_CHGDET_MASK, },
175 { .reg_offset = 0, .mask = RT8973A_INT1_DCD_T_MASK, },
176 { .reg_offset = 0, .mask = RT8973A_INT1_OVP_MASK, },
177 { .reg_offset = 0, .mask = RT8973A_INT1_CONNECT_MASK, },
178 { .reg_offset = 0, .mask = RT8973A_INT1_ADC_CHG_MASK, },
179 { .reg_offset = 0, .mask = RT8973A_INT1_OTP_MASK, },
180
181 /* INT2 interrupts */
182 { .reg_offset = 1, .mask = RT8973A_INT2_UVLOT_MASK,},
183 { .reg_offset = 1, .mask = RT8973A_INT2_POR_MASK, },
184 { .reg_offset = 1, .mask = RT8973A_INT2_OTP_FET_MASK, },
185 { .reg_offset = 1, .mask = RT8973A_INT2_OVP_FET_MASK, },
186 { .reg_offset = 1, .mask = RT8973A_INT2_OCP_LATCH_MASK, },
187 { .reg_offset = 1, .mask = RT8973A_INT2_OCP_MASK, },
188 { .reg_offset = 1, .mask = RT8973A_INT2_OVP_OCP_MASK, },
189};
190
191static const struct regmap_irq_chip rt8973a_muic_irq_chip = {
192 .name = "rt8973a",
193 .status_base = RT8973A_REG_INT1,
194 .mask_base = RT8973A_REG_INTM1,
195 .mask_invert = false,
196 .num_regs = 2,
197 .irqs = rt8973a_irqs,
198 .num_irqs = ARRAY_SIZE(rt8973a_irqs),
199};
200
201/* Define regmap configuration of RT8973A for I2C communication */
202static bool rt8973a_muic_volatile_reg(struct device *dev, unsigned int reg)
203{
204 switch (reg) {
205 case RT8973A_REG_INTM1:
206 case RT8973A_REG_INTM2:
207 return true;
208 default:
209 break;
210 }
211 return false;
212}
213
214static const struct regmap_config rt8973a_muic_regmap_config = {
215 .reg_bits = 8,
216 .val_bits = 8,
217 .volatile_reg = rt8973a_muic_volatile_reg,
218 .max_register = RT8973A_REG_END,
219};
220
221/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
222static int rt8973a_muic_set_path(struct rt8973a_muic_info *info,
223 unsigned int con_sw, bool attached)
224{
225 int ret;
226
227 /*
228 * Don't need to set h/w path according to cable type
229 * if Auto-configuration mode of CONTROL1 register is true.
230 */
231 if (info->auto_config)
232 return 0;
233
234 if (!attached)
235 con_sw = DM_DP_SWITCH_UART;
236
237 switch (con_sw) {
238 case DM_DP_SWITCH_OPEN:
239 case DM_DP_SWITCH_USB:
240 case DM_DP_SWITCH_UART:
241 ret = regmap_update_bits(info->regmap, RT8973A_REG_MANUAL_SW1,
242 RT8973A_REG_MANUAL_SW1_DP_MASK |
243 RT8973A_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 return 0;
258}
259
260static int rt8973a_muic_get_cable_type(struct rt8973a_muic_info *info)
261{
262 unsigned int adc, dev1;
263 int ret, cable_type;
264
265 /* Read ADC value according to external cable or button */
266 ret = regmap_read(info->regmap, RT8973A_REG_ADC, &adc);
267 if (ret) {
268 dev_err(info->dev, "failed to read ADC register\n");
269 return ret;
270 }
271 cable_type = adc & RT8973A_REG_ADC_MASK;
272
273 /* Read Device 1 reigster to identify correct cable type */
274 ret = regmap_read(info->regmap, RT8973A_REG_DEV1, &dev1);
275 if (ret) {
276 dev_err(info->dev, "failed to read DEV1 register\n");
277 return ret;
278 }
279
280 switch (adc) {
281 case RT8973A_MUIC_ADC_OPEN:
282 if (dev1 & RT8973A_REG_DEV1_USB_MASK)
283 cable_type = RT8973A_MUIC_ADC_USB;
284 else if (dev1 & RT8973A_REG_DEV1_DCPORT_MASK)
285 cable_type = RT8973A_MUIC_ADC_TA;
286 else
287 cable_type = RT8973A_MUIC_ADC_OPEN;
288 break;
289 default:
290 break;
291 }
292
293 return cable_type;
294}
295
296static int rt8973a_muic_cable_handler(struct rt8973a_muic_info *info,
297 enum rt8973a_event_type event)
298{
299 static unsigned int prev_cable_type;
300 unsigned int con_sw = DM_DP_SWITCH_UART;
301 int ret, cable_type;
302 unsigned int id;
303 bool attached = false;
304
305 switch (event) {
306 case RT8973A_EVENT_ATTACH:
307 cable_type = rt8973a_muic_get_cable_type(info);
308 attached = true;
309 break;
310 case RT8973A_EVENT_DETACH:
311 cable_type = prev_cable_type;
312 attached = false;
313 break;
314 case RT8973A_EVENT_OVP:
315 case RT8973A_EVENT_OTP:
316 dev_warn(info->dev,
317 "happen Over %s issue. Need to disconnect all cables\n",
318 event == RT8973A_EVENT_OVP ? "Voltage" : "Temperature");
319 cable_type = prev_cable_type;
320 attached = false;
321 break;
322 default:
323 dev_err(info->dev,
324 "Cannot handle this event (event:%d)\n", event);
325 return -EINVAL;
326 }
327 prev_cable_type = cable_type;
328
329 switch (cable_type) {
330 case RT8973A_MUIC_ADC_OTG:
331 id = EXTCON_USB_HOST;
332 con_sw = DM_DP_SWITCH_USB;
333 break;
334 case RT8973A_MUIC_ADC_TA:
335 id = EXTCON_CHG_USB_DCP;
336 con_sw = DM_DP_SWITCH_OPEN;
337 break;
338 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
339 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
340 id = EXTCON_JIG;
341 con_sw = DM_DP_SWITCH_USB;
342 break;
343 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
344 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
345 id = EXTCON_JIG;
346 con_sw = DM_DP_SWITCH_UART;
347 break;
348 case RT8973A_MUIC_ADC_USB:
349 id = EXTCON_USB;
350 con_sw = DM_DP_SWITCH_USB;
351 break;
352 case RT8973A_MUIC_ADC_OPEN:
353 return 0;
354 case RT8973A_MUIC_ADC_UNKNOWN_ACC_1:
355 case RT8973A_MUIC_ADC_UNKNOWN_ACC_2:
356 case RT8973A_MUIC_ADC_UNKNOWN_ACC_3:
357 case RT8973A_MUIC_ADC_UNKNOWN_ACC_4:
358 case RT8973A_MUIC_ADC_UNKNOWN_ACC_5:
359 dev_warn(info->dev,
360 "Unknown accessory type (adc:0x%x)\n", cable_type);
361 return 0;
362 case RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON:
363 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON:
364 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON:
365 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON:
366 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON:
367 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON:
368 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON:
369 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON:
370 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON:
371 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON:
372 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON:
373 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON:
374 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON:
375 case RT8973A_MUIC_ADC_AUDIO_TYPE2:
376 dev_warn(info->dev,
377 "Audio device/button type (adc:0x%x)\n", cable_type);
378 return 0;
379 case RT8973A_MUIC_ADC_RESERVED_ACC_1:
380 case RT8973A_MUIC_ADC_RESERVED_ACC_2:
381 case RT8973A_MUIC_ADC_RESERVED_ACC_3:
382 case RT8973A_MUIC_ADC_RESERVED_ACC_4:
383 case RT8973A_MUIC_ADC_RESERVED_ACC_5:
384 case RT8973A_MUIC_ADC_PHONE_POWERED_DEV:
385 return 0;
386 default:
387 dev_err(info->dev,
388 "Cannot handle this cable_type (adc:0x%x)\n",
389 cable_type);
390 return -EINVAL;
391 }
392
393 /* Change internal hardware path(DM_CON/DP_CON) */
394 ret = rt8973a_muic_set_path(info, con_sw, attached);
395 if (ret < 0)
396 return ret;
397
398 /* Change the state of external accessory */
399 extcon_set_state_sync(info->edev, id, attached);
400 if (id == EXTCON_USB)
401 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
402 attached);
403
404 return 0;
405}
406
407static void rt8973a_muic_irq_work(struct work_struct *work)
408{
409 struct rt8973a_muic_info *info = container_of(work,
410 struct rt8973a_muic_info, irq_work);
411 int ret = 0;
412
413 if (!info->edev)
414 return;
415
416 mutex_lock(&info->mutex);
417
418 /* Detect attached or detached cables */
419 if (info->irq_attach) {
420 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
421 info->irq_attach = false;
422 }
423
424 if (info->irq_detach) {
425 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_DETACH);
426 info->irq_detach = false;
427 }
428
429 if (info->irq_ovp) {
430 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OVP);
431 info->irq_ovp = false;
432 }
433
434 if (info->irq_otp) {
435 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OTP);
436 info->irq_otp = false;
437 }
438
439 if (ret < 0)
440 dev_err(info->dev, "failed to handle MUIC interrupt\n");
441
442 mutex_unlock(&info->mutex);
443}
444
445static irqreturn_t rt8973a_muic_irq_handler(int irq, void *data)
446{
447 struct rt8973a_muic_info *info = data;
448 int i, irq_type = -1;
449
450 for (i = 0; i < info->num_muic_irqs; i++)
451 if (irq == info->muic_irqs[i].virq)
452 irq_type = info->muic_irqs[i].irq;
453
454 switch (irq_type) {
455 case RT8973A_INT1_ATTACH:
456 info->irq_attach = true;
457 break;
458 case RT8973A_INT1_DETACH:
459 info->irq_detach = true;
460 break;
461 case RT8973A_INT1_OVP:
462 info->irq_ovp = true;
463 break;
464 case RT8973A_INT1_OTP:
465 info->irq_otp = true;
466 break;
467 case RT8973A_INT1_CHGDET:
468 case RT8973A_INT1_DCD_T:
469 case RT8973A_INT1_CONNECT:
470 case RT8973A_INT1_ADC_CHG:
471 case RT8973A_INT2_UVLO:
472 case RT8973A_INT2_POR:
473 case RT8973A_INT2_OTP_FET:
474 case RT8973A_INT2_OVP_FET:
475 case RT8973A_INT2_OCP_LATCH:
476 case RT8973A_INT2_OCP:
477 case RT8973A_INT2_OVP_OCP:
478 default:
479 dev_dbg(info->dev,
480 "Cannot handle this interrupt (%d)\n", irq_type);
481 break;
482 }
483
484 schedule_work(&info->irq_work);
485
486 return IRQ_HANDLED;
487}
488
489static void rt8973a_muic_detect_cable_wq(struct work_struct *work)
490{
491 struct rt8973a_muic_info *info = container_of(to_delayed_work(work),
492 struct rt8973a_muic_info, wq_detcable);
493 int ret;
494
495 /* Notify the state of connector cable or not */
496 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
497 if (ret < 0)
498 dev_warn(info->dev, "failed to detect cable state\n");
499}
500
501static void rt8973a_init_dev_type(struct rt8973a_muic_info *info)
502{
503 unsigned int data, vendor_id, version_id;
504 int i, ret;
505
506 /* To test I2C, Print version_id and vendor_id of RT8973A */
507 ret = regmap_read(info->regmap, RT8973A_REG_DEVICE_ID, &data);
508 if (ret) {
509 dev_err(info->dev,
510 "failed to read DEVICE_ID register: %d\n", ret);
511 return;
512 }
513
514 vendor_id = ((data & RT8973A_REG_DEVICE_ID_VENDOR_MASK) >>
515 RT8973A_REG_DEVICE_ID_VENDOR_SHIFT);
516 version_id = ((data & RT8973A_REG_DEVICE_ID_VERSION_MASK) >>
517 RT8973A_REG_DEVICE_ID_VERSION_SHIFT);
518
519 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
520 version_id, vendor_id);
521
522 /* Initiazle the register of RT8973A device to bring-up */
523 for (i = 0; i < info->num_reg_data; i++) {
524 u8 reg = info->reg_data[i].reg;
525 u8 mask = info->reg_data[i].mask;
526 u8 val = 0;
527
528 if (info->reg_data[i].invert)
529 val = ~info->reg_data[i].val;
530 else
531 val = info->reg_data[i].val;
532
533 regmap_update_bits(info->regmap, reg, mask, val);
534 }
535
536 /* Check whether RT8973A is auto switching mode or not */
537 ret = regmap_read(info->regmap, RT8973A_REG_CONTROL1, &data);
538 if (ret) {
539 dev_err(info->dev,
540 "failed to read CONTROL1 register: %d\n", ret);
541 return;
542 }
543
544 data &= RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK;
545 if (data) {
546 info->auto_config = true;
547 dev_info(info->dev,
548 "Enable Auto-configuration for internal path\n");
549 }
550}
551
552static int rt8973a_muic_i2c_probe(struct i2c_client *i2c,
553 const struct i2c_device_id *id)
554{
555 struct device_node *np = i2c->dev.of_node;
556 struct rt8973a_muic_info *info;
557 int i, ret, irq_flags;
558
559 if (!np)
560 return -EINVAL;
561
562 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
563 if (!info)
564 return -ENOMEM;
565 i2c_set_clientdata(i2c, info);
566
567 info->dev = &i2c->dev;
568 info->i2c = i2c;
569 info->irq = i2c->irq;
570 info->muic_irqs = rt8973a_muic_irqs;
571 info->num_muic_irqs = ARRAY_SIZE(rt8973a_muic_irqs);
572 info->reg_data = rt8973a_reg_data;
573 info->num_reg_data = ARRAY_SIZE(rt8973a_reg_data);
574
575 mutex_init(&info->mutex);
576
577 INIT_WORK(&info->irq_work, rt8973a_muic_irq_work);
578
579 info->regmap = devm_regmap_init_i2c(i2c, &rt8973a_muic_regmap_config);
580 if (IS_ERR(info->regmap)) {
581 ret = PTR_ERR(info->regmap);
582 dev_err(info->dev, "failed to allocate register map: %d\n",
583 ret);
584 return ret;
585 }
586
587 /* Support irq domain for RT8973A MUIC device */
588 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
589 ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
590 &rt8973a_muic_irq_chip, &info->irq_data);
591 if (ret != 0) {
592 dev_err(info->dev, "failed to add irq_chip (irq:%d, err:%d)\n",
593 info->irq, ret);
594 return ret;
595 }
596
597 for (i = 0; i < info->num_muic_irqs; i++) {
598 struct muic_irq *muic_irq = &info->muic_irqs[i];
599 int virq = 0;
600
601 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
602 if (virq <= 0)
603 return -EINVAL;
604 muic_irq->virq = virq;
605
606 ret = devm_request_threaded_irq(info->dev, virq, NULL,
607 rt8973a_muic_irq_handler,
608 IRQF_NO_SUSPEND | IRQF_ONESHOT,
609 muic_irq->name, info);
610 if (ret) {
611 dev_err(info->dev,
612 "failed: irq request (IRQ: %d, error :%d)\n",
613 muic_irq->irq, ret);
614 return ret;
615 }
616 }
617
618 /* Allocate extcon device */
619 info->edev = devm_extcon_dev_allocate(info->dev, rt8973a_extcon_cable);
620 if (IS_ERR(info->edev)) {
621 dev_err(info->dev, "failed to allocate memory for extcon\n");
622 return -ENOMEM;
623 }
624
625 /* Register extcon device */
626 ret = devm_extcon_dev_register(info->dev, info->edev);
627 if (ret) {
628 dev_err(info->dev, "failed to register extcon device\n");
629 return ret;
630 }
631
632 /*
633 * Detect accessory after completing the initialization of platform
634 *
635 * - Use delayed workqueue to detect cable state and then
636 * notify cable state to notifiee/platform through uevent.
637 * After completing the booting of platform, the extcon provider
638 * driver should notify cable state to upper layer.
639 */
640 INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq);
641 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
642 msecs_to_jiffies(DELAY_MS_DEFAULT));
643
644 /* Initialize RT8973A device and print vendor id and version id */
645 rt8973a_init_dev_type(info);
646
647 return 0;
648}
649
650static int rt8973a_muic_i2c_remove(struct i2c_client *i2c)
651{
652 struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
653
654 regmap_del_irq_chip(info->irq, info->irq_data);
655
656 return 0;
657}
658
659static const struct of_device_id rt8973a_dt_match[] = {
660 { .compatible = "richtek,rt8973a-muic" },
661 { },
662};
663MODULE_DEVICE_TABLE(of, rt8973a_dt_match);
664
665#ifdef CONFIG_PM_SLEEP
666static int rt8973a_muic_suspend(struct device *dev)
667{
668 struct i2c_client *i2c = to_i2c_client(dev);
669 struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
670
671 enable_irq_wake(info->irq);
672
673 return 0;
674}
675
676static int rt8973a_muic_resume(struct device *dev)
677{
678 struct i2c_client *i2c = to_i2c_client(dev);
679 struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
680
681 disable_irq_wake(info->irq);
682
683 return 0;
684}
685#endif
686
687static SIMPLE_DEV_PM_OPS(rt8973a_muic_pm_ops,
688 rt8973a_muic_suspend, rt8973a_muic_resume);
689
690static const struct i2c_device_id rt8973a_i2c_id[] = {
691 { "rt8973a", TYPE_RT8973A },
692 { }
693};
694MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id);
695
696static struct i2c_driver rt8973a_muic_i2c_driver = {
697 .driver = {
698 .name = "rt8973a",
699 .pm = &rt8973a_muic_pm_ops,
700 .of_match_table = rt8973a_dt_match,
701 },
702 .probe = rt8973a_muic_i2c_probe,
703 .remove = rt8973a_muic_i2c_remove,
704 .id_table = rt8973a_i2c_id,
705};
706
707static int __init rt8973a_muic_i2c_init(void)
708{
709 return i2c_add_driver(&rt8973a_muic_i2c_driver);
710}
711subsys_initcall(rt8973a_muic_i2c_init);
712
713MODULE_DESCRIPTION("Richtek RT8973A Extcon driver");
714MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
715MODULE_LICENSE("GPL");
1/*
2 * extcon-rt8973a.c - Richtek RT8973A extcon driver 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/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-provider.h>
24
25#include "extcon-rt8973a.h"
26
27#define DELAY_MS_DEFAULT 20000 /* unit: millisecond */
28
29struct muic_irq {
30 unsigned int irq;
31 const char *name;
32 unsigned int virq;
33};
34
35struct reg_data {
36 u8 reg;
37 u8 mask;
38 u8 val;
39 bool invert;
40};
41
42struct rt8973a_muic_info {
43 struct device *dev;
44 struct extcon_dev *edev;
45
46 struct i2c_client *i2c;
47 struct regmap *regmap;
48
49 struct regmap_irq_chip_data *irq_data;
50 struct muic_irq *muic_irqs;
51 unsigned int num_muic_irqs;
52 int irq;
53 bool irq_attach;
54 bool irq_detach;
55 bool irq_ovp;
56 bool irq_otp;
57 struct work_struct irq_work;
58
59 struct reg_data *reg_data;
60 unsigned int num_reg_data;
61 bool auto_config;
62
63 struct mutex mutex;
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;
72};
73
74/* Default value of RT8973A register to bring up MUIC device. */
75static struct reg_data rt8973a_reg_data[] = {
76 {
77 .reg = RT8973A_REG_CONTROL1,
78 .mask = RT8973A_REG_CONTROL1_ADC_EN_MASK
79 | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
80 | RT8973A_REG_CONTROL1_CHGTYP_MASK
81 | RT8973A_REG_CONTROL1_SWITCH_OPEN_MASK
82 | RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK
83 | RT8973A_REG_CONTROL1_INTM_MASK,
84 .val = RT8973A_REG_CONTROL1_ADC_EN_MASK
85 | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK
86 | RT8973A_REG_CONTROL1_CHGTYP_MASK,
87 .invert = false,
88 },
89 { /* sentinel */ }
90};
91
92/* List of detectable cables */
93static const unsigned int rt8973a_extcon_cable[] = {
94 EXTCON_USB,
95 EXTCON_USB_HOST,
96 EXTCON_CHG_USB_SDP,
97 EXTCON_CHG_USB_DCP,
98 EXTCON_JIG,
99 EXTCON_NONE,
100};
101
102/* Define OVP (Over Voltage Protection), OTP (Over Temperature Protection) */
103enum rt8973a_event_type {
104 RT8973A_EVENT_ATTACH = 1,
105 RT8973A_EVENT_DETACH,
106 RT8973A_EVENT_OVP,
107 RT8973A_EVENT_OTP,
108};
109
110/* Define supported accessory type */
111enum rt8973a_muic_acc_type {
112 RT8973A_MUIC_ADC_OTG = 0x0,
113 RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON,
114 RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON,
115 RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON,
116 RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON,
117 RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON,
118 RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON,
119 RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON,
120 RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON,
121 RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON,
122 RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON,
123 RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON,
124 RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON,
125 RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON,
126 RT8973A_MUIC_ADC_RESERVED_ACC_1,
127 RT8973A_MUIC_ADC_RESERVED_ACC_2,
128 RT8973A_MUIC_ADC_RESERVED_ACC_3,
129 RT8973A_MUIC_ADC_RESERVED_ACC_4,
130 RT8973A_MUIC_ADC_RESERVED_ACC_5,
131 RT8973A_MUIC_ADC_AUDIO_TYPE2,
132 RT8973A_MUIC_ADC_PHONE_POWERED_DEV,
133 RT8973A_MUIC_ADC_UNKNOWN_ACC_1,
134 RT8973A_MUIC_ADC_UNKNOWN_ACC_2,
135 RT8973A_MUIC_ADC_TA,
136 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB,
137 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB,
138 RT8973A_MUIC_ADC_UNKNOWN_ACC_3,
139 RT8973A_MUIC_ADC_UNKNOWN_ACC_4,
140 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART,
141 RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART,
142 RT8973A_MUIC_ADC_UNKNOWN_ACC_5,
143 RT8973A_MUIC_ADC_OPEN = 0x1f,
144
145 /*
146 * The below accessories has same ADC value (0x1f).
147 * So, Device type1 is used to separate specific accessory.
148 */
149 /* |---------|--ADC| */
150 /* | [7:5]|[4:0]| */
151 RT8973A_MUIC_ADC_USB = 0x3f, /* | 001|11111| */
152};
153
154/* List of supported interrupt for RT8973A */
155static struct muic_irq rt8973a_muic_irqs[] = {
156 { RT8973A_INT1_ATTACH, "muic-attach" },
157 { RT8973A_INT1_DETACH, "muic-detach" },
158 { RT8973A_INT1_CHGDET, "muic-chgdet" },
159 { RT8973A_INT1_DCD_T, "muic-dcd-t" },
160 { RT8973A_INT1_OVP, "muic-ovp" },
161 { RT8973A_INT1_CONNECT, "muic-connect" },
162 { RT8973A_INT1_ADC_CHG, "muic-adc-chg" },
163 { RT8973A_INT1_OTP, "muic-otp" },
164 { RT8973A_INT2_UVLO, "muic-uvlo" },
165 { RT8973A_INT2_POR, "muic-por" },
166 { RT8973A_INT2_OTP_FET, "muic-otp-fet" },
167 { RT8973A_INT2_OVP_FET, "muic-ovp-fet" },
168 { RT8973A_INT2_OCP_LATCH, "muic-ocp-latch" },
169 { RT8973A_INT2_OCP, "muic-ocp" },
170 { RT8973A_INT2_OVP_OCP, "muic-ovp-ocp" },
171};
172
173/* Define interrupt list of RT8973A to register regmap_irq */
174static const struct regmap_irq rt8973a_irqs[] = {
175 /* INT1 interrupts */
176 { .reg_offset = 0, .mask = RT8973A_INT1_ATTACH_MASK, },
177 { .reg_offset = 0, .mask = RT8973A_INT1_DETACH_MASK, },
178 { .reg_offset = 0, .mask = RT8973A_INT1_CHGDET_MASK, },
179 { .reg_offset = 0, .mask = RT8973A_INT1_DCD_T_MASK, },
180 { .reg_offset = 0, .mask = RT8973A_INT1_OVP_MASK, },
181 { .reg_offset = 0, .mask = RT8973A_INT1_CONNECT_MASK, },
182 { .reg_offset = 0, .mask = RT8973A_INT1_ADC_CHG_MASK, },
183 { .reg_offset = 0, .mask = RT8973A_INT1_OTP_MASK, },
184
185 /* INT2 interrupts */
186 { .reg_offset = 1, .mask = RT8973A_INT2_UVLOT_MASK,},
187 { .reg_offset = 1, .mask = RT8973A_INT2_POR_MASK, },
188 { .reg_offset = 1, .mask = RT8973A_INT2_OTP_FET_MASK, },
189 { .reg_offset = 1, .mask = RT8973A_INT2_OVP_FET_MASK, },
190 { .reg_offset = 1, .mask = RT8973A_INT2_OCP_LATCH_MASK, },
191 { .reg_offset = 1, .mask = RT8973A_INT2_OCP_MASK, },
192 { .reg_offset = 1, .mask = RT8973A_INT2_OVP_OCP_MASK, },
193};
194
195static const struct regmap_irq_chip rt8973a_muic_irq_chip = {
196 .name = "rt8973a",
197 .status_base = RT8973A_REG_INT1,
198 .mask_base = RT8973A_REG_INTM1,
199 .mask_invert = false,
200 .num_regs = 2,
201 .irqs = rt8973a_irqs,
202 .num_irqs = ARRAY_SIZE(rt8973a_irqs),
203};
204
205/* Define regmap configuration of RT8973A for I2C communication */
206static bool rt8973a_muic_volatile_reg(struct device *dev, unsigned int reg)
207{
208 switch (reg) {
209 case RT8973A_REG_INTM1:
210 case RT8973A_REG_INTM2:
211 return true;
212 default:
213 break;
214 }
215 return false;
216}
217
218static const struct regmap_config rt8973a_muic_regmap_config = {
219 .reg_bits = 8,
220 .val_bits = 8,
221 .volatile_reg = rt8973a_muic_volatile_reg,
222 .max_register = RT8973A_REG_END,
223};
224
225/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */
226static int rt8973a_muic_set_path(struct rt8973a_muic_info *info,
227 unsigned int con_sw, bool attached)
228{
229 int ret;
230
231 /*
232 * Don't need to set h/w path according to cable type
233 * if Auto-configuration mode of CONTROL1 register is true.
234 */
235 if (info->auto_config)
236 return 0;
237
238 if (!attached)
239 con_sw = DM_DP_SWITCH_UART;
240
241 switch (con_sw) {
242 case DM_DP_SWITCH_OPEN:
243 case DM_DP_SWITCH_USB:
244 case DM_DP_SWITCH_UART:
245 ret = regmap_update_bits(info->regmap, RT8973A_REG_MANUAL_SW1,
246 RT8973A_REG_MANUAL_SW1_DP_MASK |
247 RT8973A_REG_MANUAL_SW1_DM_MASK,
248 con_sw);
249 if (ret < 0) {
250 dev_err(info->dev,
251 "cannot update DM_CON/DP_CON switch\n");
252 return ret;
253 }
254 break;
255 default:
256 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
257 con_sw);
258 return -EINVAL;
259 }
260
261 return 0;
262}
263
264static int rt8973a_muic_get_cable_type(struct rt8973a_muic_info *info)
265{
266 unsigned int adc, dev1;
267 int ret, cable_type;
268
269 /* Read ADC value according to external cable or button */
270 ret = regmap_read(info->regmap, RT8973A_REG_ADC, &adc);
271 if (ret) {
272 dev_err(info->dev, "failed to read ADC register\n");
273 return ret;
274 }
275 cable_type = adc & RT8973A_REG_ADC_MASK;
276
277 /* Read Device 1 reigster to identify correct cable type */
278 ret = regmap_read(info->regmap, RT8973A_REG_DEV1, &dev1);
279 if (ret) {
280 dev_err(info->dev, "failed to read DEV1 register\n");
281 return ret;
282 }
283
284 switch (adc) {
285 case RT8973A_MUIC_ADC_OPEN:
286 if (dev1 & RT8973A_REG_DEV1_USB_MASK)
287 cable_type = RT8973A_MUIC_ADC_USB;
288 else if (dev1 & RT8973A_REG_DEV1_DCPORT_MASK)
289 cable_type = RT8973A_MUIC_ADC_TA;
290 else
291 cable_type = RT8973A_MUIC_ADC_OPEN;
292 break;
293 default:
294 break;
295 }
296
297 return cable_type;
298}
299
300static int rt8973a_muic_cable_handler(struct rt8973a_muic_info *info,
301 enum rt8973a_event_type event)
302{
303 static unsigned int prev_cable_type;
304 unsigned int con_sw = DM_DP_SWITCH_UART;
305 int ret, cable_type;
306 unsigned int id;
307 bool attached = false;
308
309 switch (event) {
310 case RT8973A_EVENT_ATTACH:
311 cable_type = rt8973a_muic_get_cable_type(info);
312 attached = true;
313 break;
314 case RT8973A_EVENT_DETACH:
315 cable_type = prev_cable_type;
316 attached = false;
317 break;
318 case RT8973A_EVENT_OVP:
319 case RT8973A_EVENT_OTP:
320 dev_warn(info->dev,
321 "happen Over %s issue. Need to disconnect all cables\n",
322 event == RT8973A_EVENT_OVP ? "Voltage" : "Temperature");
323 cable_type = prev_cable_type;
324 attached = false;
325 break;
326 default:
327 dev_err(info->dev,
328 "Cannot handle this event (event:%d)\n", event);
329 return -EINVAL;
330 }
331 prev_cable_type = cable_type;
332
333 switch (cable_type) {
334 case RT8973A_MUIC_ADC_OTG:
335 id = EXTCON_USB_HOST;
336 con_sw = DM_DP_SWITCH_USB;
337 break;
338 case RT8973A_MUIC_ADC_TA:
339 id = EXTCON_CHG_USB_DCP;
340 con_sw = DM_DP_SWITCH_OPEN;
341 break;
342 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB:
343 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB:
344 id = EXTCON_JIG;
345 con_sw = DM_DP_SWITCH_USB;
346 break;
347 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART:
348 case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART:
349 id = EXTCON_JIG;
350 con_sw = DM_DP_SWITCH_UART;
351 break;
352 case RT8973A_MUIC_ADC_USB:
353 id = EXTCON_USB;
354 con_sw = DM_DP_SWITCH_USB;
355 break;
356 case RT8973A_MUIC_ADC_OPEN:
357 return 0;
358 case RT8973A_MUIC_ADC_UNKNOWN_ACC_1:
359 case RT8973A_MUIC_ADC_UNKNOWN_ACC_2:
360 case RT8973A_MUIC_ADC_UNKNOWN_ACC_3:
361 case RT8973A_MUIC_ADC_UNKNOWN_ACC_4:
362 case RT8973A_MUIC_ADC_UNKNOWN_ACC_5:
363 dev_warn(info->dev,
364 "Unknown accessory type (adc:0x%x)\n", cable_type);
365 return 0;
366 case RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON:
367 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON:
368 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON:
369 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON:
370 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON:
371 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON:
372 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON:
373 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON:
374 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON:
375 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON:
376 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON:
377 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON:
378 case RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON:
379 case RT8973A_MUIC_ADC_AUDIO_TYPE2:
380 dev_warn(info->dev,
381 "Audio device/button type (adc:0x%x)\n", cable_type);
382 return 0;
383 case RT8973A_MUIC_ADC_RESERVED_ACC_1:
384 case RT8973A_MUIC_ADC_RESERVED_ACC_2:
385 case RT8973A_MUIC_ADC_RESERVED_ACC_3:
386 case RT8973A_MUIC_ADC_RESERVED_ACC_4:
387 case RT8973A_MUIC_ADC_RESERVED_ACC_5:
388 case RT8973A_MUIC_ADC_PHONE_POWERED_DEV:
389 return 0;
390 default:
391 dev_err(info->dev,
392 "Cannot handle this cable_type (adc:0x%x)\n",
393 cable_type);
394 return -EINVAL;
395 }
396
397 /* Change internal hardware path(DM_CON/DP_CON) */
398 ret = rt8973a_muic_set_path(info, con_sw, attached);
399 if (ret < 0)
400 return ret;
401
402 /* Change the state of external accessory */
403 extcon_set_state_sync(info->edev, id, attached);
404 if (id == EXTCON_USB)
405 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
406 attached);
407
408 return 0;
409}
410
411static void rt8973a_muic_irq_work(struct work_struct *work)
412{
413 struct rt8973a_muic_info *info = container_of(work,
414 struct rt8973a_muic_info, irq_work);
415 int ret = 0;
416
417 if (!info->edev)
418 return;
419
420 mutex_lock(&info->mutex);
421
422 /* Detect attached or detached cables */
423 if (info->irq_attach) {
424 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
425 info->irq_attach = false;
426 }
427
428 if (info->irq_detach) {
429 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_DETACH);
430 info->irq_detach = false;
431 }
432
433 if (info->irq_ovp) {
434 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OVP);
435 info->irq_ovp = false;
436 }
437
438 if (info->irq_otp) {
439 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OTP);
440 info->irq_otp = false;
441 }
442
443 if (ret < 0)
444 dev_err(info->dev, "failed to handle MUIC interrupt\n");
445
446 mutex_unlock(&info->mutex);
447}
448
449static irqreturn_t rt8973a_muic_irq_handler(int irq, void *data)
450{
451 struct rt8973a_muic_info *info = data;
452 int i, irq_type = -1;
453
454 for (i = 0; i < info->num_muic_irqs; i++)
455 if (irq == info->muic_irqs[i].virq)
456 irq_type = info->muic_irqs[i].irq;
457
458 switch (irq_type) {
459 case RT8973A_INT1_ATTACH:
460 info->irq_attach = true;
461 break;
462 case RT8973A_INT1_DETACH:
463 info->irq_detach = true;
464 break;
465 case RT8973A_INT1_OVP:
466 info->irq_ovp = true;
467 break;
468 case RT8973A_INT1_OTP:
469 info->irq_otp = true;
470 break;
471 case RT8973A_INT1_CHGDET:
472 case RT8973A_INT1_DCD_T:
473 case RT8973A_INT1_CONNECT:
474 case RT8973A_INT1_ADC_CHG:
475 case RT8973A_INT2_UVLO:
476 case RT8973A_INT2_POR:
477 case RT8973A_INT2_OTP_FET:
478 case RT8973A_INT2_OVP_FET:
479 case RT8973A_INT2_OCP_LATCH:
480 case RT8973A_INT2_OCP:
481 case RT8973A_INT2_OVP_OCP:
482 default:
483 dev_dbg(info->dev,
484 "Cannot handle this interrupt (%d)\n", irq_type);
485 break;
486 }
487
488 schedule_work(&info->irq_work);
489
490 return IRQ_HANDLED;
491}
492
493static void rt8973a_muic_detect_cable_wq(struct work_struct *work)
494{
495 struct rt8973a_muic_info *info = container_of(to_delayed_work(work),
496 struct rt8973a_muic_info, wq_detcable);
497 int ret;
498
499 /* Notify the state of connector cable or not */
500 ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH);
501 if (ret < 0)
502 dev_warn(info->dev, "failed to detect cable state\n");
503}
504
505static void rt8973a_init_dev_type(struct rt8973a_muic_info *info)
506{
507 unsigned int data, vendor_id, version_id;
508 int i, ret;
509
510 /* To test I2C, Print version_id and vendor_id of RT8973A */
511 ret = regmap_read(info->regmap, RT8973A_REG_DEVICE_ID, &data);
512 if (ret) {
513 dev_err(info->dev,
514 "failed to read DEVICE_ID register: %d\n", ret);
515 return;
516 }
517
518 vendor_id = ((data & RT8973A_REG_DEVICE_ID_VENDOR_MASK) >>
519 RT8973A_REG_DEVICE_ID_VENDOR_SHIFT);
520 version_id = ((data & RT8973A_REG_DEVICE_ID_VERSION_MASK) >>
521 RT8973A_REG_DEVICE_ID_VERSION_SHIFT);
522
523 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
524 version_id, vendor_id);
525
526 /* Initiazle the register of RT8973A device to bring-up */
527 for (i = 0; i < info->num_reg_data; i++) {
528 u8 reg = info->reg_data[i].reg;
529 u8 mask = info->reg_data[i].mask;
530 u8 val = 0;
531
532 if (info->reg_data[i].invert)
533 val = ~info->reg_data[i].val;
534 else
535 val = info->reg_data[i].val;
536
537 regmap_update_bits(info->regmap, reg, mask, val);
538 }
539
540 /* Check whether RT8973A is auto switching mode or not */
541 ret = regmap_read(info->regmap, RT8973A_REG_CONTROL1, &data);
542 if (ret) {
543 dev_err(info->dev,
544 "failed to read CONTROL1 register: %d\n", ret);
545 return;
546 }
547
548 data &= RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK;
549 if (data) {
550 info->auto_config = true;
551 dev_info(info->dev,
552 "Enable Auto-configuration for internal path\n");
553 }
554}
555
556static int rt8973a_muic_i2c_probe(struct i2c_client *i2c,
557 const struct i2c_device_id *id)
558{
559 struct device_node *np = i2c->dev.of_node;
560 struct rt8973a_muic_info *info;
561 int i, ret, irq_flags;
562
563 if (!np)
564 return -EINVAL;
565
566 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
567 if (!info)
568 return -ENOMEM;
569 i2c_set_clientdata(i2c, info);
570
571 info->dev = &i2c->dev;
572 info->i2c = i2c;
573 info->irq = i2c->irq;
574 info->muic_irqs = rt8973a_muic_irqs;
575 info->num_muic_irqs = ARRAY_SIZE(rt8973a_muic_irqs);
576 info->reg_data = rt8973a_reg_data;
577 info->num_reg_data = ARRAY_SIZE(rt8973a_reg_data);
578
579 mutex_init(&info->mutex);
580
581 INIT_WORK(&info->irq_work, rt8973a_muic_irq_work);
582
583 info->regmap = devm_regmap_init_i2c(i2c, &rt8973a_muic_regmap_config);
584 if (IS_ERR(info->regmap)) {
585 ret = PTR_ERR(info->regmap);
586 dev_err(info->dev, "failed to allocate register map: %d\n",
587 ret);
588 return ret;
589 }
590
591 /* Support irq domain for RT8973A MUIC device */
592 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
593 ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0,
594 &rt8973a_muic_irq_chip, &info->irq_data);
595 if (ret != 0) {
596 dev_err(info->dev, "failed to add irq_chip (irq:%d, err:%d)\n",
597 info->irq, ret);
598 return ret;
599 }
600
601 for (i = 0; i < info->num_muic_irqs; i++) {
602 struct muic_irq *muic_irq = &info->muic_irqs[i];
603 int virq = 0;
604
605 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq);
606 if (virq <= 0)
607 return -EINVAL;
608 muic_irq->virq = virq;
609
610 ret = devm_request_threaded_irq(info->dev, virq, NULL,
611 rt8973a_muic_irq_handler,
612 IRQF_NO_SUSPEND | IRQF_ONESHOT,
613 muic_irq->name, info);
614 if (ret) {
615 dev_err(info->dev,
616 "failed: irq request (IRQ: %d, error :%d)\n",
617 muic_irq->irq, ret);
618 return ret;
619 }
620 }
621
622 /* Allocate extcon device */
623 info->edev = devm_extcon_dev_allocate(info->dev, rt8973a_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
629 /* Register extcon device */
630 ret = devm_extcon_dev_register(info->dev, info->edev);
631 if (ret) {
632 dev_err(info->dev, "failed to register extcon device\n");
633 return ret;
634 }
635
636 /*
637 * Detect accessory after completing the initialization of platform
638 *
639 * - Use delayed workqueue to detect cable state and then
640 * notify cable state to notifiee/platform through uevent.
641 * After completing the booting of platform, the extcon provider
642 * driver should notify cable state to upper layer.
643 */
644 INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq);
645 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
646 msecs_to_jiffies(DELAY_MS_DEFAULT));
647
648 /* Initialize RT8973A device and print vendor id and version id */
649 rt8973a_init_dev_type(info);
650
651 return 0;
652}
653
654static int rt8973a_muic_i2c_remove(struct i2c_client *i2c)
655{
656 struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
657
658 regmap_del_irq_chip(info->irq, info->irq_data);
659
660 return 0;
661}
662
663static const struct of_device_id rt8973a_dt_match[] = {
664 { .compatible = "richtek,rt8973a-muic" },
665 { },
666};
667MODULE_DEVICE_TABLE(of, rt8973a_dt_match);
668
669#ifdef CONFIG_PM_SLEEP
670static int rt8973a_muic_suspend(struct device *dev)
671{
672 struct i2c_client *i2c = to_i2c_client(dev);
673 struct rt8973a_muic_info *info = i2c_get_clientdata(i2c);
674
675 enable_irq_wake(info->irq);
676
677 return 0;
678}
679
680static int rt8973a_muic_resume(struct device *dev)
681{
682 struct i2c_client *i2c = to_i2c_client(dev);
683 struct rt8973a_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(rt8973a_muic_pm_ops,
692 rt8973a_muic_suspend, rt8973a_muic_resume);
693
694static const struct i2c_device_id rt8973a_i2c_id[] = {
695 { "rt8973a", TYPE_RT8973A },
696 { }
697};
698MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id);
699
700static struct i2c_driver rt8973a_muic_i2c_driver = {
701 .driver = {
702 .name = "rt8973a",
703 .pm = &rt8973a_muic_pm_ops,
704 .of_match_table = rt8973a_dt_match,
705 },
706 .probe = rt8973a_muic_i2c_probe,
707 .remove = rt8973a_muic_i2c_remove,
708 .id_table = rt8973a_i2c_id,
709};
710
711static int __init rt8973a_muic_i2c_init(void)
712{
713 return i2c_add_driver(&rt8973a_muic_i2c_driver);
714}
715subsys_initcall(rt8973a_muic_i2c_init);
716
717MODULE_DESCRIPTION("Richtek RT8973A Extcon driver");
718MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
719MODULE_LICENSE("GPL");