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 | |
25 | struct muic_irq { |
26 | unsigned int irq; |
27 | const char *name; |
28 | unsigned int virq; |
29 | }; |
30 | |
31 | struct reg_data { |
32 | u8 reg; |
33 | u8 mask; |
34 | u8 val; |
35 | bool invert; |
36 | }; |
37 | |
38 | struct 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. */ |
71 | static 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 */ |
89 | static 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) */ |
99 | enum 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 */ |
107 | enum 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 */ |
151 | static 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 */ |
170 | static 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 | |
191 | static 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 | .num_regs = 2, |
196 | .irqs = rt8973a_irqs, |
197 | .num_irqs = ARRAY_SIZE(rt8973a_irqs), |
198 | }; |
199 | |
200 | /* Define regmap configuration of RT8973A for I2C communication */ |
201 | static bool rt8973a_muic_volatile_reg(struct device *dev, unsigned int reg) |
202 | { |
203 | switch (reg) { |
204 | case RT8973A_REG_INTM1: |
205 | case RT8973A_REG_INTM2: |
206 | return true; |
207 | default: |
208 | break; |
209 | } |
210 | return false; |
211 | } |
212 | |
213 | static const struct regmap_config rt8973a_muic_regmap_config = { |
214 | .reg_bits = 8, |
215 | .val_bits = 8, |
216 | .volatile_reg = rt8973a_muic_volatile_reg, |
217 | .max_register = RT8973A_REG_END, |
218 | }; |
219 | |
220 | /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */ |
221 | static int rt8973a_muic_set_path(struct rt8973a_muic_info *info, |
222 | unsigned int con_sw, bool attached) |
223 | { |
224 | int ret; |
225 | |
226 | /* |
227 | * Don't need to set h/w path according to cable type |
228 | * if Auto-configuration mode of CONTROL1 register is true. |
229 | */ |
230 | if (info->auto_config) |
231 | return 0; |
232 | |
233 | if (!attached) |
234 | con_sw = DM_DP_SWITCH_UART; |
235 | |
236 | switch (con_sw) { |
237 | case DM_DP_SWITCH_OPEN: |
238 | case DM_DP_SWITCH_USB: |
239 | case DM_DP_SWITCH_UART: |
240 | ret = regmap_update_bits(map: info->regmap, reg: RT8973A_REG_MANUAL_SW1, |
241 | RT8973A_REG_MANUAL_SW1_DP_MASK | |
242 | RT8973A_REG_MANUAL_SW1_DM_MASK, |
243 | val: con_sw); |
244 | if (ret < 0) { |
245 | dev_err(info->dev, |
246 | "cannot update DM_CON/DP_CON switch\n" ); |
247 | return ret; |
248 | } |
249 | break; |
250 | default: |
251 | dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n" , |
252 | con_sw); |
253 | return -EINVAL; |
254 | } |
255 | |
256 | return 0; |
257 | } |
258 | |
259 | static int rt8973a_muic_get_cable_type(struct rt8973a_muic_info *info) |
260 | { |
261 | unsigned int adc, dev1; |
262 | int ret, cable_type; |
263 | |
264 | /* Read ADC value according to external cable or button */ |
265 | ret = regmap_read(map: info->regmap, reg: RT8973A_REG_ADC, val: &adc); |
266 | if (ret) { |
267 | dev_err(info->dev, "failed to read ADC register\n" ); |
268 | return ret; |
269 | } |
270 | cable_type = adc & RT8973A_REG_ADC_MASK; |
271 | |
272 | /* Read Device 1 reigster to identify correct cable type */ |
273 | ret = regmap_read(map: info->regmap, reg: RT8973A_REG_DEV1, val: &dev1); |
274 | if (ret) { |
275 | dev_err(info->dev, "failed to read DEV1 register\n" ); |
276 | return ret; |
277 | } |
278 | |
279 | switch (adc) { |
280 | case RT8973A_MUIC_ADC_OPEN: |
281 | if (dev1 & RT8973A_REG_DEV1_USB_MASK) |
282 | cable_type = RT8973A_MUIC_ADC_USB; |
283 | else if (dev1 & RT8973A_REG_DEV1_DCPORT_MASK) |
284 | cable_type = RT8973A_MUIC_ADC_TA; |
285 | else |
286 | cable_type = RT8973A_MUIC_ADC_OPEN; |
287 | break; |
288 | default: |
289 | break; |
290 | } |
291 | |
292 | return cable_type; |
293 | } |
294 | |
295 | static int rt8973a_muic_cable_handler(struct rt8973a_muic_info *info, |
296 | enum rt8973a_event_type event) |
297 | { |
298 | static unsigned int prev_cable_type; |
299 | unsigned int con_sw = DM_DP_SWITCH_UART; |
300 | int ret, cable_type; |
301 | unsigned int id; |
302 | bool attached = false; |
303 | |
304 | switch (event) { |
305 | case RT8973A_EVENT_ATTACH: |
306 | cable_type = rt8973a_muic_get_cable_type(info); |
307 | attached = true; |
308 | break; |
309 | case RT8973A_EVENT_DETACH: |
310 | cable_type = prev_cable_type; |
311 | attached = false; |
312 | break; |
313 | case RT8973A_EVENT_OVP: |
314 | case RT8973A_EVENT_OTP: |
315 | dev_warn(info->dev, |
316 | "happen Over %s issue. Need to disconnect all cables\n" , |
317 | event == RT8973A_EVENT_OVP ? "Voltage" : "Temperature" ); |
318 | cable_type = prev_cable_type; |
319 | attached = false; |
320 | break; |
321 | default: |
322 | dev_err(info->dev, |
323 | "Cannot handle this event (event:%d)\n" , event); |
324 | return -EINVAL; |
325 | } |
326 | prev_cable_type = cable_type; |
327 | |
328 | switch (cable_type) { |
329 | case RT8973A_MUIC_ADC_OTG: |
330 | id = EXTCON_USB_HOST; |
331 | con_sw = DM_DP_SWITCH_USB; |
332 | break; |
333 | case RT8973A_MUIC_ADC_TA: |
334 | id = EXTCON_CHG_USB_DCP; |
335 | con_sw = DM_DP_SWITCH_OPEN; |
336 | break; |
337 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: |
338 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: |
339 | id = EXTCON_JIG; |
340 | con_sw = DM_DP_SWITCH_USB; |
341 | break; |
342 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: |
343 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: |
344 | id = EXTCON_JIG; |
345 | con_sw = DM_DP_SWITCH_UART; |
346 | break; |
347 | case RT8973A_MUIC_ADC_USB: |
348 | id = EXTCON_USB; |
349 | con_sw = DM_DP_SWITCH_USB; |
350 | break; |
351 | case RT8973A_MUIC_ADC_OPEN: |
352 | return 0; |
353 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_1: |
354 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_2: |
355 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_3: |
356 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_4: |
357 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_5: |
358 | dev_warn(info->dev, |
359 | "Unknown accessory type (adc:0x%x)\n" , cable_type); |
360 | return 0; |
361 | case RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON: |
362 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON: |
363 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON: |
364 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON: |
365 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON: |
366 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON: |
367 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON: |
368 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON: |
369 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON: |
370 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON: |
371 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON: |
372 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON: |
373 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON: |
374 | case RT8973A_MUIC_ADC_AUDIO_TYPE2: |
375 | dev_warn(info->dev, |
376 | "Audio device/button type (adc:0x%x)\n" , cable_type); |
377 | return 0; |
378 | case RT8973A_MUIC_ADC_RESERVED_ACC_1: |
379 | case RT8973A_MUIC_ADC_RESERVED_ACC_2: |
380 | case RT8973A_MUIC_ADC_RESERVED_ACC_3: |
381 | case RT8973A_MUIC_ADC_RESERVED_ACC_4: |
382 | case RT8973A_MUIC_ADC_RESERVED_ACC_5: |
383 | case RT8973A_MUIC_ADC_PHONE_POWERED_DEV: |
384 | return 0; |
385 | default: |
386 | dev_err(info->dev, |
387 | "Cannot handle this cable_type (adc:0x%x)\n" , |
388 | cable_type); |
389 | return -EINVAL; |
390 | } |
391 | |
392 | /* Change internal hardware path(DM_CON/DP_CON) */ |
393 | ret = rt8973a_muic_set_path(info, con_sw, attached); |
394 | if (ret < 0) |
395 | return ret; |
396 | |
397 | /* Change the state of external accessory */ |
398 | extcon_set_state_sync(edev: info->edev, id, state: attached); |
399 | if (id == EXTCON_USB) |
400 | extcon_set_state_sync(edev: info->edev, EXTCON_CHG_USB_SDP, |
401 | state: attached); |
402 | |
403 | return 0; |
404 | } |
405 | |
406 | static void rt8973a_muic_irq_work(struct work_struct *work) |
407 | { |
408 | struct rt8973a_muic_info *info = container_of(work, |
409 | struct rt8973a_muic_info, irq_work); |
410 | int ret = 0; |
411 | |
412 | if (!info->edev) |
413 | return; |
414 | |
415 | mutex_lock(&info->mutex); |
416 | |
417 | /* Detect attached or detached cables */ |
418 | if (info->irq_attach) { |
419 | ret = rt8973a_muic_cable_handler(info, event: RT8973A_EVENT_ATTACH); |
420 | info->irq_attach = false; |
421 | } |
422 | |
423 | if (info->irq_detach) { |
424 | ret = rt8973a_muic_cable_handler(info, event: RT8973A_EVENT_DETACH); |
425 | info->irq_detach = false; |
426 | } |
427 | |
428 | if (info->irq_ovp) { |
429 | ret = rt8973a_muic_cable_handler(info, event: RT8973A_EVENT_OVP); |
430 | info->irq_ovp = false; |
431 | } |
432 | |
433 | if (info->irq_otp) { |
434 | ret = rt8973a_muic_cable_handler(info, event: RT8973A_EVENT_OTP); |
435 | info->irq_otp = false; |
436 | } |
437 | |
438 | if (ret < 0) |
439 | dev_err(info->dev, "failed to handle MUIC interrupt\n" ); |
440 | |
441 | mutex_unlock(lock: &info->mutex); |
442 | } |
443 | |
444 | static irqreturn_t rt8973a_muic_irq_handler(int irq, void *data) |
445 | { |
446 | struct rt8973a_muic_info *info = data; |
447 | int i, irq_type = -1; |
448 | |
449 | for (i = 0; i < info->num_muic_irqs; i++) |
450 | if (irq == info->muic_irqs[i].virq) |
451 | irq_type = info->muic_irqs[i].irq; |
452 | |
453 | switch (irq_type) { |
454 | case RT8973A_INT1_ATTACH: |
455 | info->irq_attach = true; |
456 | break; |
457 | case RT8973A_INT1_DETACH: |
458 | info->irq_detach = true; |
459 | break; |
460 | case RT8973A_INT1_OVP: |
461 | info->irq_ovp = true; |
462 | break; |
463 | case RT8973A_INT1_OTP: |
464 | info->irq_otp = true; |
465 | break; |
466 | case RT8973A_INT1_CHGDET: |
467 | case RT8973A_INT1_DCD_T: |
468 | case RT8973A_INT1_CONNECT: |
469 | case RT8973A_INT1_ADC_CHG: |
470 | case RT8973A_INT2_UVLO: |
471 | case RT8973A_INT2_POR: |
472 | case RT8973A_INT2_OTP_FET: |
473 | case RT8973A_INT2_OVP_FET: |
474 | case RT8973A_INT2_OCP_LATCH: |
475 | case RT8973A_INT2_OCP: |
476 | case RT8973A_INT2_OVP_OCP: |
477 | default: |
478 | dev_dbg(info->dev, |
479 | "Cannot handle this interrupt (%d)\n" , irq_type); |
480 | break; |
481 | } |
482 | |
483 | schedule_work(work: &info->irq_work); |
484 | |
485 | return IRQ_HANDLED; |
486 | } |
487 | |
488 | static void rt8973a_muic_detect_cable_wq(struct work_struct *work) |
489 | { |
490 | struct rt8973a_muic_info *info = container_of(to_delayed_work(work), |
491 | struct rt8973a_muic_info, wq_detcable); |
492 | int ret; |
493 | |
494 | /* Notify the state of connector cable or not */ |
495 | ret = rt8973a_muic_cable_handler(info, event: RT8973A_EVENT_ATTACH); |
496 | if (ret < 0) |
497 | dev_warn(info->dev, "failed to detect cable state\n" ); |
498 | } |
499 | |
500 | static void rt8973a_init_dev_type(struct rt8973a_muic_info *info) |
501 | { |
502 | unsigned int data, vendor_id, version_id; |
503 | int i, ret; |
504 | |
505 | /* To test I2C, Print version_id and vendor_id of RT8973A */ |
506 | ret = regmap_read(map: info->regmap, reg: RT8973A_REG_DEVICE_ID, val: &data); |
507 | if (ret) { |
508 | dev_err(info->dev, |
509 | "failed to read DEVICE_ID register: %d\n" , ret); |
510 | return; |
511 | } |
512 | |
513 | vendor_id = ((data & RT8973A_REG_DEVICE_ID_VENDOR_MASK) >> |
514 | RT8973A_REG_DEVICE_ID_VENDOR_SHIFT); |
515 | version_id = ((data & RT8973A_REG_DEVICE_ID_VERSION_MASK) >> |
516 | RT8973A_REG_DEVICE_ID_VERSION_SHIFT); |
517 | |
518 | dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n" , |
519 | version_id, vendor_id); |
520 | |
521 | /* Initiazle the register of RT8973A device to bring-up */ |
522 | for (i = 0; i < info->num_reg_data; i++) { |
523 | u8 reg = info->reg_data[i].reg; |
524 | u8 mask = info->reg_data[i].mask; |
525 | u8 val = 0; |
526 | |
527 | if (info->reg_data[i].invert) |
528 | val = ~info->reg_data[i].val; |
529 | else |
530 | val = info->reg_data[i].val; |
531 | |
532 | regmap_update_bits(map: info->regmap, reg, mask, val); |
533 | } |
534 | |
535 | /* Check whether RT8973A is auto switching mode or not */ |
536 | ret = regmap_read(map: info->regmap, reg: RT8973A_REG_CONTROL1, val: &data); |
537 | if (ret) { |
538 | dev_err(info->dev, |
539 | "failed to read CONTROL1 register: %d\n" , ret); |
540 | return; |
541 | } |
542 | |
543 | data &= RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK; |
544 | if (data) { |
545 | info->auto_config = true; |
546 | dev_info(info->dev, |
547 | "Enable Auto-configuration for internal path\n" ); |
548 | } |
549 | } |
550 | |
551 | static int rt8973a_muic_i2c_probe(struct i2c_client *i2c) |
552 | { |
553 | struct device_node *np = i2c->dev.of_node; |
554 | struct rt8973a_muic_info *info; |
555 | int i, ret, irq_flags; |
556 | |
557 | if (!np) |
558 | return -EINVAL; |
559 | |
560 | info = devm_kzalloc(dev: &i2c->dev, size: sizeof(*info), GFP_KERNEL); |
561 | if (!info) |
562 | return -ENOMEM; |
563 | i2c_set_clientdata(client: i2c, data: info); |
564 | |
565 | info->dev = &i2c->dev; |
566 | info->i2c = i2c; |
567 | info->irq = i2c->irq; |
568 | info->muic_irqs = rt8973a_muic_irqs; |
569 | info->num_muic_irqs = ARRAY_SIZE(rt8973a_muic_irqs); |
570 | info->reg_data = rt8973a_reg_data; |
571 | info->num_reg_data = ARRAY_SIZE(rt8973a_reg_data); |
572 | |
573 | mutex_init(&info->mutex); |
574 | |
575 | INIT_WORK(&info->irq_work, rt8973a_muic_irq_work); |
576 | |
577 | info->regmap = devm_regmap_init_i2c(i2c, &rt8973a_muic_regmap_config); |
578 | if (IS_ERR(ptr: info->regmap)) { |
579 | ret = PTR_ERR(ptr: info->regmap); |
580 | dev_err(info->dev, "failed to allocate register map: %d\n" , |
581 | ret); |
582 | return ret; |
583 | } |
584 | |
585 | /* Support irq domain for RT8973A MUIC device */ |
586 | irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; |
587 | ret = regmap_add_irq_chip(map: info->regmap, irq: info->irq, irq_flags, irq_base: 0, |
588 | chip: &rt8973a_muic_irq_chip, data: &info->irq_data); |
589 | if (ret != 0) { |
590 | dev_err(info->dev, "failed to add irq_chip (irq:%d, err:%d)\n" , |
591 | info->irq, ret); |
592 | return ret; |
593 | } |
594 | |
595 | for (i = 0; i < info->num_muic_irqs; i++) { |
596 | struct muic_irq *muic_irq = &info->muic_irqs[i]; |
597 | int virq = 0; |
598 | |
599 | virq = regmap_irq_get_virq(data: info->irq_data, irq: muic_irq->irq); |
600 | if (virq <= 0) |
601 | return -EINVAL; |
602 | muic_irq->virq = virq; |
603 | |
604 | ret = devm_request_threaded_irq(dev: info->dev, irq: virq, NULL, |
605 | thread_fn: rt8973a_muic_irq_handler, |
606 | IRQF_NO_SUSPEND | IRQF_ONESHOT, |
607 | devname: muic_irq->name, dev_id: info); |
608 | if (ret) { |
609 | dev_err(info->dev, |
610 | "failed: irq request (IRQ: %d, error :%d)\n" , |
611 | muic_irq->irq, ret); |
612 | return ret; |
613 | } |
614 | } |
615 | |
616 | /* Allocate extcon device */ |
617 | info->edev = devm_extcon_dev_allocate(dev: info->dev, cable: rt8973a_extcon_cable); |
618 | if (IS_ERR(ptr: info->edev)) { |
619 | dev_err(info->dev, "failed to allocate memory for extcon\n" ); |
620 | return -ENOMEM; |
621 | } |
622 | |
623 | /* Register extcon device */ |
624 | ret = devm_extcon_dev_register(dev: info->dev, edev: info->edev); |
625 | if (ret) { |
626 | dev_err(info->dev, "failed to register extcon device\n" ); |
627 | return ret; |
628 | } |
629 | |
630 | /* |
631 | * Detect accessory after completing the initialization of platform |
632 | * |
633 | * - Use delayed workqueue to detect cable state and then |
634 | * notify cable state to notifiee/platform through uevent. |
635 | * After completing the booting of platform, the extcon provider |
636 | * driver should notify cable state to upper layer. |
637 | */ |
638 | INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq); |
639 | queue_delayed_work(wq: system_power_efficient_wq, dwork: &info->wq_detcable, |
640 | delay: msecs_to_jiffies(DELAY_MS_DEFAULT)); |
641 | |
642 | /* Initialize RT8973A device and print vendor id and version id */ |
643 | rt8973a_init_dev_type(info); |
644 | |
645 | return 0; |
646 | } |
647 | |
648 | static void rt8973a_muic_i2c_remove(struct i2c_client *i2c) |
649 | { |
650 | struct rt8973a_muic_info *info = i2c_get_clientdata(client: i2c); |
651 | |
652 | regmap_del_irq_chip(irq: info->irq, data: info->irq_data); |
653 | } |
654 | |
655 | static const struct of_device_id rt8973a_dt_match[] = { |
656 | { .compatible = "richtek,rt8973a-muic" }, |
657 | { }, |
658 | }; |
659 | MODULE_DEVICE_TABLE(of, rt8973a_dt_match); |
660 | |
661 | #ifdef CONFIG_PM_SLEEP |
662 | static int rt8973a_muic_suspend(struct device *dev) |
663 | { |
664 | struct i2c_client *i2c = to_i2c_client(dev); |
665 | struct rt8973a_muic_info *info = i2c_get_clientdata(client: i2c); |
666 | |
667 | enable_irq_wake(irq: info->irq); |
668 | |
669 | return 0; |
670 | } |
671 | |
672 | static int rt8973a_muic_resume(struct device *dev) |
673 | { |
674 | struct i2c_client *i2c = to_i2c_client(dev); |
675 | struct rt8973a_muic_info *info = i2c_get_clientdata(client: i2c); |
676 | |
677 | disable_irq_wake(irq: info->irq); |
678 | |
679 | return 0; |
680 | } |
681 | #endif |
682 | |
683 | static SIMPLE_DEV_PM_OPS(rt8973a_muic_pm_ops, |
684 | rt8973a_muic_suspend, rt8973a_muic_resume); |
685 | |
686 | static const struct i2c_device_id rt8973a_i2c_id[] = { |
687 | { "rt8973a" , TYPE_RT8973A }, |
688 | { } |
689 | }; |
690 | MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id); |
691 | |
692 | static struct i2c_driver rt8973a_muic_i2c_driver = { |
693 | .driver = { |
694 | .name = "rt8973a" , |
695 | .pm = &rt8973a_muic_pm_ops, |
696 | .of_match_table = rt8973a_dt_match, |
697 | }, |
698 | .probe = rt8973a_muic_i2c_probe, |
699 | .remove = rt8973a_muic_i2c_remove, |
700 | .id_table = rt8973a_i2c_id, |
701 | }; |
702 | |
703 | static int __init rt8973a_muic_i2c_init(void) |
704 | { |
705 | return i2c_add_driver(&rt8973a_muic_i2c_driver); |
706 | } |
707 | subsys_initcall(rt8973a_muic_i2c_init); |
708 | |
709 | MODULE_DESCRIPTION("Richtek RT8973A Extcon driver" ); |
710 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>" ); |
711 | MODULE_LICENSE("GPL" ); |
712 | |