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