Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electrnoics |
| 5 | * Chanwoo Choi <cw00.choi@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/slab.h> |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 22 | #include <linux/input.h> |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/mfd/max77693.h> |
| 27 | #include <linux/mfd/max77693-private.h> |
| 28 | #include <linux/extcon.h> |
| 29 | #include <linux/regmap.h> |
| 30 | #include <linux/irqdomain.h> |
| 31 | |
| 32 | #define DEV_NAME "max77693-muic" |
Chanwoo Choi | 297620f | 2012-12-26 13:10:11 +0900 | [diff] [blame] | 33 | #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 34 | |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 35 | enum max77693_muic_adc_debounce_time { |
| 36 | ADC_DEBOUNCE_TIME_5MS = 0, |
| 37 | ADC_DEBOUNCE_TIME_10MS, |
| 38 | ADC_DEBOUNCE_TIME_25MS, |
| 39 | ADC_DEBOUNCE_TIME_38_62MS, |
| 40 | }; |
| 41 | |
| 42 | struct max77693_muic_info { |
| 43 | struct device *dev; |
| 44 | struct max77693_dev *max77693; |
| 45 | struct extcon_dev *edev; |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 46 | int prev_cable_type; |
| 47 | int prev_cable_type_gnd; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 48 | int prev_chg_type; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 49 | int prev_button_type; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 50 | u8 status[2]; |
| 51 | |
| 52 | int irq; |
| 53 | struct work_struct irq_work; |
| 54 | struct mutex mutex; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 55 | |
Chanwoo Choi | 297620f | 2012-12-26 13:10:11 +0900 | [diff] [blame] | 56 | /* |
| 57 | * Use delayed workqueue to detect cable state and then |
| 58 | * notify cable state to notifiee/platform through uevent. |
| 59 | * After completing the booting of platform, the extcon provider |
| 60 | * driver should notify cable state to upper layer. |
| 61 | */ |
| 62 | struct delayed_work wq_detcable; |
| 63 | |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 64 | /* Button of dock device */ |
| 65 | struct input_dev *dock; |
Chanwoo Choi | 2b75799 | 2012-12-06 21:27:56 +0900 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB |
| 69 | * h/w path of COMP2/COMN1 on CONTROL1 register. |
| 70 | */ |
| 71 | int path_usb; |
| 72 | int path_uart; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 73 | }; |
| 74 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 75 | enum max77693_muic_cable_group { |
| 76 | MAX77693_CABLE_GROUP_ADC = 0, |
| 77 | MAX77693_CABLE_GROUP_ADC_GND, |
| 78 | MAX77693_CABLE_GROUP_CHG, |
| 79 | MAX77693_CABLE_GROUP_VBVOLT, |
| 80 | }; |
| 81 | |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 82 | enum max77693_muic_charger_type { |
| 83 | MAX77693_CHARGER_TYPE_NONE = 0, |
| 84 | MAX77693_CHARGER_TYPE_USB, |
| 85 | MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT, |
| 86 | MAX77693_CHARGER_TYPE_DEDICATED_CHG, |
| 87 | MAX77693_CHARGER_TYPE_APPLE_500MA, |
| 88 | MAX77693_CHARGER_TYPE_APPLE_1A_2A, |
| 89 | MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7, |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * struct max77693_muic_irq |
| 94 | * @irq: the index of irq list of MUIC device. |
| 95 | * @name: the name of irq. |
| 96 | * @virq: the virtual irq to use irq domain |
| 97 | */ |
| 98 | struct max77693_muic_irq { |
| 99 | unsigned int irq; |
| 100 | const char *name; |
| 101 | unsigned int virq; |
| 102 | }; |
| 103 | |
| 104 | static struct max77693_muic_irq muic_irqs[] = { |
| 105 | { MAX77693_MUIC_IRQ_INT1_ADC, "muic-ADC" }, |
| 106 | { MAX77693_MUIC_IRQ_INT1_ADC_LOW, "muic-ADCLOW" }, |
| 107 | { MAX77693_MUIC_IRQ_INT1_ADC_ERR, "muic-ADCError" }, |
| 108 | { MAX77693_MUIC_IRQ_INT1_ADC1K, "muic-ADC1K" }, |
| 109 | { MAX77693_MUIC_IRQ_INT2_CHGTYP, "muic-CHGTYP" }, |
| 110 | { MAX77693_MUIC_IRQ_INT2_CHGDETREUN, "muic-CHGDETREUN" }, |
| 111 | { MAX77693_MUIC_IRQ_INT2_DCDTMR, "muic-DCDTMR" }, |
| 112 | { MAX77693_MUIC_IRQ_INT2_DXOVP, "muic-DXOVP" }, |
| 113 | { MAX77693_MUIC_IRQ_INT2_VBVOLT, "muic-VBVOLT" }, |
| 114 | { MAX77693_MUIC_IRQ_INT2_VIDRM, "muic-VIDRM" }, |
| 115 | { MAX77693_MUIC_IRQ_INT3_EOC, "muic-EOC" }, |
| 116 | { MAX77693_MUIC_IRQ_INT3_CGMBC, "muic-CGMBC" }, |
| 117 | { MAX77693_MUIC_IRQ_INT3_OVP, "muic-OVP" }, |
| 118 | { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, "muic-MBCCHG_ERR" }, |
| 119 | { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, "muic-CHG_ENABLED" }, |
| 120 | { MAX77693_MUIC_IRQ_INT3_BAT_DET, "muic-BAT_DET" }, |
| 121 | }; |
| 122 | |
| 123 | /* Define supported accessory type */ |
| 124 | enum max77693_muic_acc_type { |
| 125 | MAX77693_MUIC_ADC_GROUND = 0x0, |
| 126 | MAX77693_MUIC_ADC_SEND_END_BUTTON, |
| 127 | MAX77693_MUIC_ADC_REMOTE_S1_BUTTON, |
| 128 | MAX77693_MUIC_ADC_REMOTE_S2_BUTTON, |
| 129 | MAX77693_MUIC_ADC_REMOTE_S3_BUTTON, |
| 130 | MAX77693_MUIC_ADC_REMOTE_S4_BUTTON, |
| 131 | MAX77693_MUIC_ADC_REMOTE_S5_BUTTON, |
| 132 | MAX77693_MUIC_ADC_REMOTE_S6_BUTTON, |
| 133 | MAX77693_MUIC_ADC_REMOTE_S7_BUTTON, |
| 134 | MAX77693_MUIC_ADC_REMOTE_S8_BUTTON, |
| 135 | MAX77693_MUIC_ADC_REMOTE_S9_BUTTON, |
| 136 | MAX77693_MUIC_ADC_REMOTE_S10_BUTTON, |
| 137 | MAX77693_MUIC_ADC_REMOTE_S11_BUTTON, |
| 138 | MAX77693_MUIC_ADC_REMOTE_S12_BUTTON, |
| 139 | MAX77693_MUIC_ADC_RESERVED_ACC_1, |
| 140 | MAX77693_MUIC_ADC_RESERVED_ACC_2, |
| 141 | MAX77693_MUIC_ADC_RESERVED_ACC_3, |
| 142 | MAX77693_MUIC_ADC_RESERVED_ACC_4, |
| 143 | MAX77693_MUIC_ADC_RESERVED_ACC_5, |
| 144 | MAX77693_MUIC_ADC_CEA936_AUDIO, |
| 145 | MAX77693_MUIC_ADC_PHONE_POWERED_DEV, |
| 146 | MAX77693_MUIC_ADC_TTY_CONVERTER, |
| 147 | MAX77693_MUIC_ADC_UART_CABLE, |
| 148 | MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG, |
| 149 | MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF, |
| 150 | MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON, |
| 151 | MAX77693_MUIC_ADC_AV_CABLE_NOLOAD, |
| 152 | MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG, |
| 153 | MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF, |
| 154 | MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON, |
| 155 | MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE, |
| 156 | MAX77693_MUIC_ADC_OPEN, |
| 157 | |
| 158 | /* The below accessories have same ADC value so ADCLow and |
| 159 | ADC1K bit is used to separate specific accessory */ |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 160 | MAX77693_MUIC_GND_USB_OTG = 0x100, /* ADC:0x0, VBVolot:0, ADCLow:0, ADC1K:0 */ |
| 161 | MAX77693_MUIC_GND_USB_OTG_VB = 0x104, /* ADC:0x0, VBVolot:1, ADCLow:0, ADC1K:0 */ |
| 162 | MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:0 */ |
| 163 | MAX77693_MUIC_GND_MHL = 0x103, /* ADC:0x0, VBVolot:0, ADCLow:1, ADC1K:1 */ |
| 164 | MAX77693_MUIC_GND_MHL_VB = 0x107, /* ADC:0x0, VBVolot:1, ADCLow:1, ADC1K:1 */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | /* MAX77693 MUIC device support below list of accessories(external connector) */ |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 168 | enum { |
| 169 | EXTCON_CABLE_USB = 0, |
| 170 | EXTCON_CABLE_USB_HOST, |
| 171 | EXTCON_CABLE_TA, |
| 172 | EXTCON_CABLE_FAST_CHARGER, |
| 173 | EXTCON_CABLE_SLOW_CHARGER, |
| 174 | EXTCON_CABLE_CHARGE_DOWNSTREAM, |
| 175 | EXTCON_CABLE_MHL, |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 176 | EXTCON_CABLE_MHL_TA, |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 177 | EXTCON_CABLE_JIG_USB_ON, |
| 178 | EXTCON_CABLE_JIG_USB_OFF, |
| 179 | EXTCON_CABLE_JIG_UART_OFF, |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 180 | EXTCON_CABLE_JIG_UART_ON, |
| 181 | EXTCON_CABLE_DOCK_SMART, |
| 182 | EXTCON_CABLE_DOCK_DESK, |
| 183 | EXTCON_CABLE_DOCK_AUDIO, |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 184 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 185 | _EXTCON_CABLE_NUM, |
| 186 | }; |
| 187 | |
Sachin Kamat | 45d4a4e | 2013-01-31 09:31:47 +0900 | [diff] [blame] | 188 | static const char *max77693_extcon_cable[] = { |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 189 | [EXTCON_CABLE_USB] = "USB", |
| 190 | [EXTCON_CABLE_USB_HOST] = "USB-Host", |
| 191 | [EXTCON_CABLE_TA] = "TA", |
| 192 | [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger", |
| 193 | [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger", |
| 194 | [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream", |
| 195 | [EXTCON_CABLE_MHL] = "MHL", |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 196 | [EXTCON_CABLE_MHL_TA] = "MHL_TA", |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 197 | [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON", |
| 198 | [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF", |
| 199 | [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF", |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 200 | [EXTCON_CABLE_JIG_UART_ON] = "Dock-Car", |
| 201 | [EXTCON_CABLE_DOCK_SMART] = "Dock-Smart", |
| 202 | [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk", |
| 203 | [EXTCON_CABLE_DOCK_AUDIO] = "Dock-Audio", |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 204 | |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 205 | NULL, |
| 206 | }; |
| 207 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 208 | /* |
| 209 | * max77693_muic_set_debounce_time - Set the debounce time of ADC |
| 210 | * @info: the instance including private data of max77693 MUIC |
| 211 | * @time: the debounce time of ADC |
| 212 | */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 213 | static int max77693_muic_set_debounce_time(struct max77693_muic_info *info, |
| 214 | enum max77693_muic_adc_debounce_time time) |
| 215 | { |
Axel Lin | bf2627d | 2012-10-04 09:55:23 +0900 | [diff] [blame] | 216 | int ret; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 217 | |
| 218 | switch (time) { |
| 219 | case ADC_DEBOUNCE_TIME_5MS: |
| 220 | case ADC_DEBOUNCE_TIME_10MS: |
| 221 | case ADC_DEBOUNCE_TIME_25MS: |
| 222 | case ADC_DEBOUNCE_TIME_38_62MS: |
Axel Lin | bf2627d | 2012-10-04 09:55:23 +0900 | [diff] [blame] | 223 | ret = max77693_update_reg(info->max77693->regmap_muic, |
| 224 | MAX77693_MUIC_REG_CTRL3, |
| 225 | time << CONTROL3_ADCDBSET_SHIFT, |
| 226 | CONTROL3_ADCDBSET_MASK); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 227 | if (ret) { |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 228 | dev_err(info->dev, "failed to set ADC debounce time\n"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 229 | return -EAGAIN; |
| 230 | } |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 231 | break; |
| 232 | default: |
| 233 | dev_err(info->dev, "invalid ADC debounce time\n"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 234 | return -EINVAL; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 237 | return 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 238 | }; |
| 239 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 240 | /* |
| 241 | * max77693_muic_set_path - Set hardware line according to attached cable |
| 242 | * @info: the instance including private data of max77693 MUIC |
| 243 | * @value: the path according to attached cable |
| 244 | * @attached: the state of cable (true:attached, false:detached) |
| 245 | * |
| 246 | * The max77693 MUIC device share outside H/W line among a varity of cables |
| 247 | * so, this function set internal path of H/W line according to the type of |
| 248 | * attached cable. |
| 249 | */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 250 | static int max77693_muic_set_path(struct max77693_muic_info *info, |
| 251 | u8 val, bool attached) |
| 252 | { |
| 253 | int ret = 0; |
| 254 | u8 ctrl1, ctrl2 = 0; |
| 255 | |
| 256 | if (attached) |
| 257 | ctrl1 = val; |
| 258 | else |
| 259 | ctrl1 = CONTROL1_SW_OPEN; |
| 260 | |
| 261 | ret = max77693_update_reg(info->max77693->regmap_muic, |
| 262 | MAX77693_MUIC_REG_CTRL1, ctrl1, COMP_SW_MASK); |
| 263 | if (ret < 0) { |
| 264 | dev_err(info->dev, "failed to update MUIC register\n"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 265 | return -EAGAIN; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (attached) |
| 269 | ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */ |
| 270 | else |
| 271 | ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */ |
| 272 | |
| 273 | ret = max77693_update_reg(info->max77693->regmap_muic, |
| 274 | MAX77693_MUIC_REG_CTRL2, ctrl2, |
| 275 | CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK); |
| 276 | if (ret < 0) { |
| 277 | dev_err(info->dev, "failed to update MUIC register\n"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 278 | return -EAGAIN; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | dev_info(info->dev, |
| 282 | "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", |
| 283 | ctrl1, ctrl2, attached ? "attached" : "detached"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 284 | |
| 285 | return 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 286 | } |
| 287 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 288 | /* |
| 289 | * max77693_muic_get_cable_type - Return cable type and check cable state |
| 290 | * @info: the instance including private data of max77693 MUIC |
| 291 | * @group: the path according to attached cable |
| 292 | * @attached: store cable state and return |
| 293 | * |
| 294 | * This function check the cable state either attached or detached, |
| 295 | * and then divide precise type of cable according to cable group. |
| 296 | * - MAX77693_CABLE_GROUP_ADC |
| 297 | * - MAX77693_CABLE_GROUP_ADC_GND |
| 298 | * - MAX77693_CABLE_GROUP_CHG |
| 299 | * - MAX77693_CABLE_GROUP_VBVOLT |
| 300 | */ |
| 301 | static int max77693_muic_get_cable_type(struct max77693_muic_info *info, |
| 302 | enum max77693_muic_cable_group group, bool *attached) |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 303 | { |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 304 | int cable_type = 0; |
| 305 | int adc; |
| 306 | int adc1k; |
| 307 | int adclow; |
| 308 | int vbvolt; |
| 309 | int chg_type; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 310 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 311 | switch (group) { |
| 312 | case MAX77693_CABLE_GROUP_ADC: |
| 313 | /* |
| 314 | * Read ADC value to check cable type and decide cable state |
| 315 | * according to cable type |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 316 | */ |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 317 | adc = info->status[0] & STATUS1_ADC_MASK; |
| 318 | adc >>= STATUS1_ADC_SHIFT; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 319 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 320 | /* |
| 321 | * Check current cable state/cable type and store cable type |
| 322 | * (info->prev_cable_type) for handling cable when cable is |
| 323 | * detached. |
| 324 | */ |
| 325 | if (adc == MAX77693_MUIC_ADC_OPEN) { |
| 326 | *attached = false; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 327 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 328 | cable_type = info->prev_cable_type; |
| 329 | info->prev_cable_type = MAX77693_MUIC_ADC_OPEN; |
| 330 | } else { |
| 331 | *attached = true; |
| 332 | |
| 333 | cable_type = info->prev_cable_type = adc; |
| 334 | } |
| 335 | break; |
| 336 | case MAX77693_CABLE_GROUP_ADC_GND: |
| 337 | /* |
| 338 | * Read ADC value to check cable type and decide cable state |
| 339 | * according to cable type |
| 340 | */ |
| 341 | adc = info->status[0] & STATUS1_ADC_MASK; |
| 342 | adc >>= STATUS1_ADC_SHIFT; |
| 343 | |
| 344 | /* |
| 345 | * Check current cable state/cable type and store cable type |
| 346 | * (info->prev_cable_type/_gnd) for handling cable when cable |
| 347 | * is detached. |
| 348 | */ |
| 349 | if (adc == MAX77693_MUIC_ADC_OPEN) { |
| 350 | *attached = false; |
| 351 | |
| 352 | cable_type = info->prev_cable_type_gnd; |
| 353 | info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN; |
| 354 | } else { |
| 355 | *attached = true; |
| 356 | |
| 357 | adclow = info->status[0] & STATUS1_ADCLOW_MASK; |
| 358 | adclow >>= STATUS1_ADCLOW_SHIFT; |
| 359 | adc1k = info->status[0] & STATUS1_ADC1K_MASK; |
| 360 | adc1k >>= STATUS1_ADC1K_SHIFT; |
| 361 | |
| 362 | vbvolt = info->status[1] & STATUS2_VBVOLT_MASK; |
| 363 | vbvolt >>= STATUS2_VBVOLT_SHIFT; |
| 364 | |
| 365 | /** |
| 366 | * [0x1][VBVolt][ADCLow][ADC1K] |
| 367 | * [0x1 0 0 0 ] : USB_OTG |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 368 | * [0x1 1 0 0 ] : USB_OTG_VB |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 369 | * [0x1 0 1 0 ] : Audio Video Cable with load |
| 370 | * [0x1 0 1 1 ] : MHL without charging connector |
| 371 | * [0x1 1 1 1 ] : MHL with charging connector |
| 372 | */ |
| 373 | cable_type = ((0x1 << 8) |
| 374 | | (vbvolt << 2) |
| 375 | | (adclow << 1) |
| 376 | | adc1k); |
| 377 | |
| 378 | info->prev_cable_type = adc; |
| 379 | info->prev_cable_type_gnd = cable_type; |
| 380 | } |
| 381 | |
| 382 | break; |
| 383 | case MAX77693_CABLE_GROUP_CHG: |
| 384 | /* |
| 385 | * Read charger type to check cable type and decide cable state |
| 386 | * according to type of charger cable. |
| 387 | */ |
| 388 | chg_type = info->status[1] & STATUS2_CHGTYP_MASK; |
| 389 | chg_type >>= STATUS2_CHGTYP_SHIFT; |
| 390 | |
| 391 | if (chg_type == MAX77693_CHARGER_TYPE_NONE) { |
| 392 | *attached = false; |
| 393 | |
| 394 | cable_type = info->prev_chg_type; |
| 395 | info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE; |
| 396 | } else { |
| 397 | *attached = true; |
| 398 | |
| 399 | /* |
| 400 | * Check current cable state/cable type and store cable |
| 401 | * type(info->prev_chg_type) for handling cable when |
| 402 | * charger cable is detached. |
| 403 | */ |
| 404 | cable_type = info->prev_chg_type = chg_type; |
| 405 | } |
| 406 | |
| 407 | break; |
| 408 | case MAX77693_CABLE_GROUP_VBVOLT: |
| 409 | /* |
| 410 | * Read ADC value to check cable type and decide cable state |
| 411 | * according to cable type |
| 412 | */ |
| 413 | adc = info->status[0] & STATUS1_ADC_MASK; |
| 414 | adc >>= STATUS1_ADC_SHIFT; |
| 415 | chg_type = info->status[1] & STATUS2_CHGTYP_MASK; |
| 416 | chg_type >>= STATUS2_CHGTYP_SHIFT; |
| 417 | |
| 418 | if (adc == MAX77693_MUIC_ADC_OPEN |
| 419 | && chg_type == MAX77693_CHARGER_TYPE_NONE) |
| 420 | *attached = false; |
| 421 | else |
| 422 | *attached = true; |
| 423 | |
| 424 | /* |
| 425 | * Read vbvolt field, if vbvolt is 1, |
| 426 | * this cable is used for charging. |
| 427 | */ |
| 428 | vbvolt = info->status[1] & STATUS2_VBVOLT_MASK; |
| 429 | vbvolt >>= STATUS2_VBVOLT_SHIFT; |
| 430 | |
| 431 | cable_type = vbvolt; |
| 432 | break; |
| 433 | default: |
| 434 | dev_err(info->dev, "Unknown cable group (%d)\n", group); |
| 435 | cable_type = -EINVAL; |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | return cable_type; |
| 440 | } |
| 441 | |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 442 | static int max77693_muic_dock_handler(struct max77693_muic_info *info, |
| 443 | int cable_type, bool attached) |
| 444 | { |
| 445 | int ret = 0; |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 446 | int vbvolt; |
| 447 | bool cable_attached; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 448 | char dock_name[CABLE_NAME_MAX]; |
| 449 | |
| 450 | dev_info(info->dev, |
| 451 | "external connector is %s (adc:0x%02x)\n", |
| 452 | attached ? "attached" : "detached", cable_type); |
| 453 | |
| 454 | switch (cable_type) { |
| 455 | case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */ |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 456 | /* |
| 457 | * Check power cable whether attached or detached state. |
| 458 | * The Dock-Smart device need surely external power supply. |
| 459 | * If power cable(USB/TA) isn't connected to Dock device, |
| 460 | * user can't use Dock-Smart for desktop mode. |
| 461 | */ |
| 462 | vbvolt = max77693_muic_get_cable_type(info, |
| 463 | MAX77693_CABLE_GROUP_VBVOLT, &cable_attached); |
| 464 | if (attached && !vbvolt) { |
| 465 | dev_warn(info->dev, |
| 466 | "Cannot detect external power supply\n"); |
| 467 | return 0; |
| 468 | } |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 469 | |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 470 | /* |
| 471 | * Notify Dock-Smart/MHL state. |
| 472 | * - Dock-Smart device include three type of cable which |
| 473 | * are HDMI, USB for mouse/keyboard and micro-usb port |
| 474 | * for USB/TA cable. Dock-Smart device need always exteranl |
| 475 | * power supply(USB/TA cable through micro-usb cable). Dock- |
| 476 | * Smart device support screen output of target to separate |
| 477 | * monitor and mouse/keyboard for desktop mode. |
| 478 | * |
| 479 | * Features of 'USB/TA cable with Dock-Smart device' |
| 480 | * - Support MHL |
| 481 | * - Support external output feature of audio |
| 482 | * - Support charging through micro-usb port without data |
| 483 | * connection if TA cable is connected to target. |
| 484 | * - Support charging and data connection through micro-usb port |
| 485 | * if USB cable is connected between target and host |
| 486 | * device. |
| 487 | * - Support OTG device (Mouse/Keyboard) |
| 488 | */ |
| 489 | ret = max77693_muic_set_path(info, info->path_usb, attached); |
| 490 | if (ret < 0) |
| 491 | return ret; |
| 492 | |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 493 | extcon_set_cable_state(info->edev, "Dock-Smart", attached); |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 494 | extcon_set_cable_state(info->edev, "MHL", attached); |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 495 | goto out; |
| 496 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */ |
| 497 | strcpy(dock_name, "Dock-Car"); |
| 498 | break; |
| 499 | case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */ |
| 500 | strcpy(dock_name, "Dock-Desk"); |
| 501 | break; |
| 502 | case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */ |
| 503 | strcpy(dock_name, "Dock-Audio"); |
| 504 | if (!attached) |
| 505 | extcon_set_cable_state(info->edev, "USB", false); |
| 506 | break; |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 507 | default: |
| 508 | dev_err(info->dev, "failed to detect %s dock device\n", |
| 509 | attached ? "attached" : "detached"); |
| 510 | return -EINVAL; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* Dock-Car/Desk/Audio, PATH:AUDIO */ |
| 514 | ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached); |
| 515 | if (ret < 0) |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 516 | return ret; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 517 | extcon_set_cable_state(info->edev, dock_name, attached); |
| 518 | |
| 519 | out: |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 520 | return 0; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static int max77693_muic_dock_button_handler(struct max77693_muic_info *info, |
| 524 | int button_type, bool attached) |
| 525 | { |
| 526 | struct input_dev *dock = info->dock; |
| 527 | unsigned int code; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 528 | |
| 529 | switch (button_type) { |
| 530 | case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1 |
| 531 | ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1: |
| 532 | /* DOCK_KEY_PREV */ |
| 533 | code = KEY_PREVIOUSSONG; |
| 534 | break; |
| 535 | case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1 |
| 536 | ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1: |
| 537 | /* DOCK_KEY_NEXT */ |
| 538 | code = KEY_NEXTSONG; |
| 539 | break; |
| 540 | case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: |
| 541 | /* DOCK_VOL_DOWN */ |
| 542 | code = KEY_VOLUMEDOWN; |
| 543 | break; |
| 544 | case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: |
| 545 | /* DOCK_VOL_UP */ |
| 546 | code = KEY_VOLUMEUP; |
| 547 | break; |
| 548 | case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1 |
| 549 | ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1: |
| 550 | /* DOCK_KEY_PLAY_PAUSE */ |
| 551 | code = KEY_PLAYPAUSE; |
| 552 | break; |
| 553 | default: |
| 554 | dev_err(info->dev, |
| 555 | "failed to detect %s key (adc:0x%x)\n", |
| 556 | attached ? "pressed" : "released", button_type); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 557 | return -EINVAL; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | input_event(dock, EV_KEY, code, attached); |
| 561 | input_sync(dock); |
| 562 | |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 566 | static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info) |
| 567 | { |
| 568 | int cable_type_gnd; |
| 569 | int ret = 0; |
| 570 | bool attached; |
| 571 | |
| 572 | cable_type_gnd = max77693_muic_get_cable_type(info, |
| 573 | MAX77693_CABLE_GROUP_ADC_GND, &attached); |
| 574 | |
| 575 | switch (cable_type_gnd) { |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 576 | case MAX77693_MUIC_GND_USB_OTG: |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 577 | case MAX77693_MUIC_GND_USB_OTG_VB: |
| 578 | /* USB_OTG, PATH: AP_USB */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 579 | ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached); |
| 580 | if (ret < 0) |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 581 | return ret; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 582 | extcon_set_cable_state(info->edev, "USB-Host", attached); |
| 583 | break; |
| 584 | case MAX77693_MUIC_GND_AV_CABLE_LOAD: |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 585 | /* Audio Video Cable with load, PATH:AUDIO */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 586 | ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached); |
| 587 | if (ret < 0) |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 588 | return ret; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 589 | extcon_set_cable_state(info->edev, |
| 590 | "Audio-video-load", attached); |
| 591 | break; |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 592 | case MAX77693_MUIC_GND_MHL: |
| 593 | case MAX77693_MUIC_GND_MHL_VB: |
| 594 | /* MHL or MHL with USB/TA cable */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 595 | extcon_set_cable_state(info->edev, "MHL", attached); |
| 596 | break; |
| 597 | default: |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 598 | dev_err(info->dev, "failed to detect %s cable of gnd type\n", |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 599 | attached ? "attached" : "detached"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 600 | return -EINVAL; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 601 | } |
| 602 | |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 603 | return 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 604 | } |
| 605 | |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 606 | static int max77693_muic_jig_handler(struct max77693_muic_info *info, |
| 607 | int cable_type, bool attached) |
| 608 | { |
| 609 | char cable_name[32]; |
| 610 | int ret = 0; |
| 611 | u8 path = CONTROL1_SW_OPEN; |
| 612 | |
| 613 | dev_info(info->dev, |
| 614 | "external connector is %s (adc:0x%02x)\n", |
| 615 | attached ? "attached" : "detached", cable_type); |
| 616 | |
| 617 | switch (cable_type) { |
| 618 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */ |
| 619 | /* PATH:AP_USB */ |
| 620 | strcpy(cable_name, "JIG-USB-OFF"); |
| 621 | path = CONTROL1_SW_USB; |
| 622 | break; |
| 623 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */ |
| 624 | /* PATH:AP_USB */ |
| 625 | strcpy(cable_name, "JIG-USB-ON"); |
| 626 | path = CONTROL1_SW_USB; |
| 627 | break; |
| 628 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */ |
| 629 | /* PATH:AP_UART */ |
| 630 | strcpy(cable_name, "JIG-UART-OFF"); |
| 631 | path = CONTROL1_SW_UART; |
| 632 | break; |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 633 | default: |
| 634 | dev_err(info->dev, "failed to detect %s jig cable\n", |
| 635 | attached ? "attached" : "detached"); |
| 636 | return -EINVAL; |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | ret = max77693_muic_set_path(info, path, attached); |
| 640 | if (ret < 0) |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 641 | return ret; |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 642 | |
| 643 | extcon_set_cable_state(info->edev, cable_name, attached); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 644 | |
| 645 | return 0; |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 646 | } |
| 647 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 648 | static int max77693_muic_adc_handler(struct max77693_muic_info *info) |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 649 | { |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 650 | int cable_type; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 651 | int button_type; |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 652 | bool attached; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 653 | int ret = 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 654 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 655 | /* Check accessory state which is either detached or attached */ |
| 656 | cable_type = max77693_muic_get_cable_type(info, |
| 657 | MAX77693_CABLE_GROUP_ADC, &attached); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 658 | |
| 659 | dev_info(info->dev, |
| 660 | "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 661 | attached ? "attached" : "detached", cable_type, |
| 662 | info->prev_cable_type); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 663 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 664 | switch (cable_type) { |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 665 | case MAX77693_MUIC_ADC_GROUND: |
| 666 | /* USB_OTG/MHL/Audio */ |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 667 | max77693_muic_adc_ground_handler(info); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 668 | break; |
| 669 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: |
| 670 | case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 671 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 672 | /* JIG */ |
Chanwoo Choi | d0587eb | 2012-11-27 12:06:49 +0900 | [diff] [blame] | 673 | ret = max77693_muic_jig_handler(info, cable_type, attached); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 674 | if (ret < 0) |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 675 | return ret; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 676 | break; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 677 | case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */ |
| 678 | case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON: /* Dock-Car */ |
| 679 | case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: /* Dock-Desk */ |
| 680 | case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */ |
| 681 | /* |
| 682 | * DOCK device |
| 683 | * |
| 684 | * The MAX77693 MUIC device can detect total 34 cable type |
| 685 | * except of charger cable and MUIC device didn't define |
| 686 | * specfic role of cable in the range of from 0x01 to 0x12 |
| 687 | * of ADC value. So, can use/define cable with no role according |
| 688 | * to schema of hardware board. |
| 689 | */ |
| 690 | ret = max77693_muic_dock_handler(info, cable_type, attached); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 691 | if (ret < 0) |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 692 | return ret; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 693 | break; |
| 694 | case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON: /* DOCK_KEY_PREV */ |
| 695 | case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON: /* DOCK_KEY_NEXT */ |
| 696 | case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON: /* DOCK_VOL_DOWN */ |
| 697 | case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON: /* DOCK_VOL_UP */ |
| 698 | case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON: /* DOCK_KEY_PLAY_PAUSE */ |
| 699 | /* |
| 700 | * Button of DOCK device |
| 701 | * - the Prev/Next/Volume Up/Volume Down/Play-Pause button |
| 702 | * |
| 703 | * The MAX77693 MUIC device can detect total 34 cable type |
| 704 | * except of charger cable and MUIC device didn't define |
| 705 | * specfic role of cable in the range of from 0x01 to 0x12 |
| 706 | * of ADC value. So, can use/define cable with no role according |
| 707 | * to schema of hardware board. |
| 708 | */ |
| 709 | if (attached) |
| 710 | button_type = info->prev_button_type = cable_type; |
| 711 | else |
| 712 | button_type = info->prev_button_type; |
| 713 | |
| 714 | ret = max77693_muic_dock_button_handler(info, button_type, |
| 715 | attached); |
| 716 | if (ret < 0) |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 717 | return ret; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 718 | break; |
| 719 | case MAX77693_MUIC_ADC_SEND_END_BUTTON: |
| 720 | case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON: |
| 721 | case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 722 | case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON: |
| 723 | case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON: |
| 724 | case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 725 | case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 726 | case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 727 | case MAX77693_MUIC_ADC_RESERVED_ACC_1: |
| 728 | case MAX77693_MUIC_ADC_RESERVED_ACC_2: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 729 | case MAX77693_MUIC_ADC_RESERVED_ACC_4: |
| 730 | case MAX77693_MUIC_ADC_RESERVED_ACC_5: |
| 731 | case MAX77693_MUIC_ADC_CEA936_AUDIO: |
| 732 | case MAX77693_MUIC_ADC_PHONE_POWERED_DEV: |
| 733 | case MAX77693_MUIC_ADC_TTY_CONVERTER: |
| 734 | case MAX77693_MUIC_ADC_UART_CABLE: |
| 735 | case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG: |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 736 | case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG: |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 737 | /* |
| 738 | * This accessory isn't used in general case if it is specially |
| 739 | * needed to detect additional accessory, should implement |
| 740 | * proper operation when this accessory is attached/detached. |
| 741 | */ |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 742 | dev_info(info->dev, |
| 743 | "accessory is %s but it isn't used (adc:0x%x)\n", |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 744 | attached ? "attached" : "detached", cable_type); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 745 | return -EAGAIN; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 746 | default: |
| 747 | dev_err(info->dev, |
| 748 | "failed to detect %s accessory (adc:0x%x)\n", |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 749 | attached ? "attached" : "detached", cable_type); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 750 | return -EINVAL; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 751 | } |
| 752 | |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 753 | return 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 754 | } |
| 755 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 756 | static int max77693_muic_chg_handler(struct max77693_muic_info *info) |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 757 | { |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 758 | int chg_type; |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 759 | int cable_type_gnd; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 760 | int cable_type; |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 761 | bool attached; |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 762 | bool cable_attached; |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 763 | int ret = 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 764 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 765 | chg_type = max77693_muic_get_cable_type(info, |
| 766 | MAX77693_CABLE_GROUP_CHG, &attached); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 767 | |
| 768 | dev_info(info->dev, |
| 769 | "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", |
| 770 | attached ? "attached" : "detached", |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 771 | chg_type, info->prev_chg_type); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 772 | |
| 773 | switch (chg_type) { |
| 774 | case MAX77693_CHARGER_TYPE_USB: |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 775 | case MAX77693_CHARGER_TYPE_DEDICATED_CHG: |
Chanwoo Choi | 0e2738f | 2012-12-06 21:36:18 +0900 | [diff] [blame] | 776 | case MAX77693_CHARGER_TYPE_NONE: |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 777 | /* Check MAX77693_CABLE_GROUP_ADC_GND type */ |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 778 | cable_type_gnd = max77693_muic_get_cable_type(info, |
| 779 | MAX77693_CABLE_GROUP_ADC_GND, |
| 780 | &cable_attached); |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 781 | switch (cable_type_gnd) { |
| 782 | case MAX77693_MUIC_GND_MHL: |
| 783 | case MAX77693_MUIC_GND_MHL_VB: |
| 784 | /* |
| 785 | * MHL cable with MHL_TA(USB/TA) cable |
| 786 | * - MHL cable include two port(HDMI line and separate micro- |
| 787 | * usb port. When the target connect MHL cable, extcon driver |
| 788 | * check whether MHL_TA(USB/TA) cable is connected. If MHL_TA |
| 789 | * cable is connected, extcon driver notify state to notifiee |
| 790 | * for charging battery. |
| 791 | * |
| 792 | * Features of 'MHL_TA(USB/TA) with MHL cable' |
| 793 | * - Support MHL |
| 794 | * - Support charging through micro-usb port without data connection |
| 795 | */ |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 796 | extcon_set_cable_state(info->edev, "MHL_TA", attached); |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 797 | if (!cable_attached) |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 798 | extcon_set_cable_state(info->edev, "MHL", cable_attached); |
| 799 | break; |
Chanwoo Choi | 06bed0a | 2012-11-27 11:30:35 +0900 | [diff] [blame] | 800 | } |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 801 | |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 802 | /* Check MAX77693_CABLE_GROUP_ADC type */ |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 803 | cable_type = max77693_muic_get_cable_type(info, |
| 804 | MAX77693_CABLE_GROUP_ADC, |
| 805 | &cable_attached); |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 806 | switch (cable_type) { |
| 807 | case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD: /* Dock-Audio */ |
| 808 | /* |
| 809 | * Dock-Audio device with USB/TA cable |
| 810 | * - Dock device include two port(Dock-Audio and micro-usb |
| 811 | * port). When the target connect Dock-Audio device, extcon |
| 812 | * driver check whether USB/TA cable is connected. If USB/TA |
| 813 | * cable is connected, extcon driver notify state to notifiee |
| 814 | * for charging battery. |
| 815 | * |
| 816 | * Features of 'USB/TA cable with Dock-Audio device' |
| 817 | * - Support external output feature of audio. |
| 818 | * - Support charging through micro-usb port without data |
| 819 | * connection. |
| 820 | */ |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 821 | extcon_set_cable_state(info->edev, "USB", attached); |
| 822 | |
| 823 | if (!cable_attached) |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 824 | extcon_set_cable_state(info->edev, "Dock-Audio", cable_attached); |
| 825 | break; |
| 826 | case MAX77693_MUIC_ADC_RESERVED_ACC_3: /* Dock-Smart */ |
| 827 | /* |
| 828 | * Dock-Smart device with USB/TA cable |
| 829 | * - Dock-Desk device include three type of cable which |
| 830 | * are HDMI, USB for mouse/keyboard and micro-usb port |
| 831 | * for USB/TA cable. Dock-Smart device need always exteranl |
| 832 | * power supply(USB/TA cable through micro-usb cable). Dock- |
| 833 | * Smart device support screen output of target to separate |
| 834 | * monitor and mouse/keyboard for desktop mode. |
| 835 | * |
| 836 | * Features of 'USB/TA cable with Dock-Smart device' |
| 837 | * - Support MHL |
| 838 | * - Support external output feature of audio |
| 839 | * - Support charging through micro-usb port without data |
| 840 | * connection if TA cable is connected to target. |
| 841 | * - Support charging and data connection through micro-usb port |
| 842 | * if USB cable is connected between target and host |
| 843 | * device. |
| 844 | * - Support OTG device (Mouse/Keyboard) |
| 845 | */ |
| 846 | ret = max77693_muic_set_path(info, info->path_usb, attached); |
| 847 | if (ret < 0) |
| 848 | return ret; |
| 849 | |
| 850 | extcon_set_cable_state(info->edev, "Dock-Smart", attached); |
| 851 | extcon_set_cable_state(info->edev, "MHL", attached); |
| 852 | |
| 853 | break; |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 854 | } |
| 855 | |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 856 | /* Check MAX77693_CABLE_GROUP_CHG type */ |
| 857 | switch (chg_type) { |
| 858 | case MAX77693_CHARGER_TYPE_NONE: |
| 859 | /* |
| 860 | * When MHL(with USB/TA cable) or Dock-Audio with USB/TA cable |
| 861 | * is attached, muic device happen below two interrupt. |
| 862 | * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting MHL/Dock-Audio. |
| 863 | * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting USB/TA cable |
| 864 | * connected to MHL or Dock-Audio. |
| 865 | * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC interrupt |
| 866 | * than MAX77693_MUIC_IRQ_INT2_CHGTYP interrupt. |
| 867 | * |
| 868 | * If user attach MHL (with USB/TA cable and immediately detach |
| 869 | * MHL with USB/TA cable before MAX77693_MUIC_IRQ_INT2_CHGTYP |
| 870 | * interrupt is happened, USB/TA cable remain connected state to |
| 871 | * target. But USB/TA cable isn't connected to target. The user |
| 872 | * be face with unusual action. So, driver should check this |
| 873 | * situation in spite of, that previous charger type is N/A. |
| 874 | */ |
Chanwoo Choi | 0e2738f | 2012-12-06 21:36:18 +0900 | [diff] [blame] | 875 | break; |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 876 | case MAX77693_CHARGER_TYPE_USB: |
| 877 | /* Only USB cable, PATH:AP_USB */ |
| 878 | ret = max77693_muic_set_path(info, info->path_usb, attached); |
| 879 | if (ret < 0) |
| 880 | return ret; |
Chanwoo Choi | 0e2738f | 2012-12-06 21:36:18 +0900 | [diff] [blame] | 881 | |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 882 | extcon_set_cable_state(info->edev, "USB", attached); |
| 883 | break; |
| 884 | case MAX77693_CHARGER_TYPE_DEDICATED_CHG: |
| 885 | /* Only TA cable */ |
| 886 | extcon_set_cable_state(info->edev, "TA", attached); |
| 887 | break; |
| 888 | } |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 889 | break; |
| 890 | case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT: |
| 891 | extcon_set_cable_state(info->edev, |
| 892 | "Charge-downstream", attached); |
| 893 | break; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 894 | case MAX77693_CHARGER_TYPE_APPLE_500MA: |
| 895 | extcon_set_cable_state(info->edev, "Slow-charger", attached); |
| 896 | break; |
| 897 | case MAX77693_CHARGER_TYPE_APPLE_1A_2A: |
| 898 | extcon_set_cable_state(info->edev, "Fast-charger", attached); |
| 899 | break; |
| 900 | case MAX77693_CHARGER_TYPE_DEAD_BATTERY: |
| 901 | break; |
| 902 | default: |
| 903 | dev_err(info->dev, |
| 904 | "failed to detect %s accessory (chg_type:0x%x)\n", |
| 905 | attached ? "attached" : "detached", chg_type); |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 906 | return -EINVAL; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 907 | } |
| 908 | |
Chanwoo Choi | a162629 | 2012-12-10 19:07:53 +0900 | [diff] [blame] | 909 | return 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static void max77693_muic_irq_work(struct work_struct *work) |
| 913 | { |
| 914 | struct max77693_muic_info *info = container_of(work, |
| 915 | struct max77693_muic_info, irq_work); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 916 | int irq_type = -1; |
| 917 | int i, ret = 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 918 | |
| 919 | if (!info->edev) |
| 920 | return; |
| 921 | |
| 922 | mutex_lock(&info->mutex); |
| 923 | |
| 924 | for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++) |
| 925 | if (info->irq == muic_irqs[i].virq) |
| 926 | irq_type = muic_irqs[i].irq; |
| 927 | |
| 928 | ret = max77693_bulk_read(info->max77693->regmap_muic, |
| 929 | MAX77693_MUIC_REG_STATUS1, 2, info->status); |
| 930 | if (ret) { |
| 931 | dev_err(info->dev, "failed to read MUIC register\n"); |
| 932 | mutex_unlock(&info->mutex); |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | switch (irq_type) { |
| 937 | case MAX77693_MUIC_IRQ_INT1_ADC: |
| 938 | case MAX77693_MUIC_IRQ_INT1_ADC_LOW: |
| 939 | case MAX77693_MUIC_IRQ_INT1_ADC_ERR: |
| 940 | case MAX77693_MUIC_IRQ_INT1_ADC1K: |
| 941 | /* Handle all of accessory except for |
| 942 | type of charger accessory */ |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 943 | ret = max77693_muic_adc_handler(info); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 944 | break; |
| 945 | case MAX77693_MUIC_IRQ_INT2_CHGTYP: |
| 946 | case MAX77693_MUIC_IRQ_INT2_CHGDETREUN: |
| 947 | case MAX77693_MUIC_IRQ_INT2_DCDTMR: |
| 948 | case MAX77693_MUIC_IRQ_INT2_DXOVP: |
| 949 | case MAX77693_MUIC_IRQ_INT2_VBVOLT: |
| 950 | case MAX77693_MUIC_IRQ_INT2_VIDRM: |
| 951 | /* Handle charger accessory */ |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 952 | ret = max77693_muic_chg_handler(info); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 953 | break; |
| 954 | case MAX77693_MUIC_IRQ_INT3_EOC: |
| 955 | case MAX77693_MUIC_IRQ_INT3_CGMBC: |
| 956 | case MAX77693_MUIC_IRQ_INT3_OVP: |
| 957 | case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR: |
| 958 | case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED: |
| 959 | case MAX77693_MUIC_IRQ_INT3_BAT_DET: |
| 960 | break; |
| 961 | default: |
| 962 | dev_err(info->dev, "muic interrupt: irq %d occurred\n", |
| 963 | irq_type); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 964 | mutex_unlock(&info->mutex); |
| 965 | return; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (ret < 0) |
| 969 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); |
| 970 | |
| 971 | mutex_unlock(&info->mutex); |
| 972 | |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | static irqreturn_t max77693_muic_irq_handler(int irq, void *data) |
| 977 | { |
| 978 | struct max77693_muic_info *info = data; |
| 979 | |
| 980 | info->irq = irq; |
| 981 | schedule_work(&info->irq_work); |
| 982 | |
| 983 | return IRQ_HANDLED; |
| 984 | } |
| 985 | |
| 986 | static struct regmap_config max77693_muic_regmap_config = { |
| 987 | .reg_bits = 8, |
| 988 | .val_bits = 8, |
| 989 | }; |
| 990 | |
| 991 | static int max77693_muic_detect_accessory(struct max77693_muic_info *info) |
| 992 | { |
| 993 | int ret = 0; |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 994 | int adc; |
| 995 | int chg_type; |
| 996 | bool attached; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 997 | |
| 998 | mutex_lock(&info->mutex); |
| 999 | |
| 1000 | /* Read STATUSx register to detect accessory */ |
| 1001 | ret = max77693_bulk_read(info->max77693->regmap_muic, |
| 1002 | MAX77693_MUIC_REG_STATUS1, 2, info->status); |
| 1003 | if (ret) { |
| 1004 | dev_err(info->dev, "failed to read MUIC register\n"); |
| 1005 | mutex_unlock(&info->mutex); |
| 1006 | return -EINVAL; |
| 1007 | } |
| 1008 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 1009 | adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC, |
| 1010 | &attached); |
| 1011 | if (attached && adc != MAX77693_MUIC_ADC_OPEN) { |
| 1012 | ret = max77693_muic_adc_handler(info); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 1013 | if (ret < 0) { |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 1014 | dev_err(info->dev, "Cannot detect accessory\n"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 1015 | mutex_unlock(&info->mutex); |
| 1016 | return ret; |
| 1017 | } |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1018 | } |
| 1019 | |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 1020 | chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG, |
| 1021 | &attached); |
| 1022 | if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) { |
| 1023 | ret = max77693_muic_chg_handler(info); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 1024 | if (ret < 0) { |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 1025 | dev_err(info->dev, "Cannot detect charger accessory\n"); |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 1026 | mutex_unlock(&info->mutex); |
| 1027 | return ret; |
| 1028 | } |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1029 | } |
| 1030 | |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1031 | mutex_unlock(&info->mutex); |
Chanwoo Choi | 154f757f | 2012-11-27 09:40:32 +0900 | [diff] [blame] | 1032 | |
Chanwoo Choi | 19d3243 | 2013-02-13 18:35:08 +0900 | [diff] [blame] | 1033 | return 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1034 | } |
| 1035 | |
Chanwoo Choi | 297620f | 2012-12-26 13:10:11 +0900 | [diff] [blame] | 1036 | static void max77693_muic_detect_cable_wq(struct work_struct *work) |
| 1037 | { |
| 1038 | struct max77693_muic_info *info = container_of(to_delayed_work(work), |
| 1039 | struct max77693_muic_info, wq_detcable); |
| 1040 | |
| 1041 | max77693_muic_detect_accessory(info); |
| 1042 | } |
| 1043 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 1044 | static int max77693_muic_probe(struct platform_device *pdev) |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1045 | { |
| 1046 | struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); |
Chanwoo Choi | f8457d57 | 2012-10-08 14:41:49 +0900 | [diff] [blame] | 1047 | struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1048 | struct max77693_muic_info *info; |
Chanwoo Choi | 297620f | 2012-12-26 13:10:11 +0900 | [diff] [blame] | 1049 | int delay_jiffies; |
| 1050 | int ret; |
| 1051 | int i; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1052 | u8 id; |
| 1053 | |
Sachin Kamat | f4bb5cb | 2012-11-20 15:46:50 +0900 | [diff] [blame] | 1054 | info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info), |
| 1055 | GFP_KERNEL); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1056 | if (!info) { |
| 1057 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
Sachin Kamat | f4bb5cb | 2012-11-20 15:46:50 +0900 | [diff] [blame] | 1058 | return -ENOMEM; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1059 | } |
| 1060 | info->dev = &pdev->dev; |
| 1061 | info->max77693 = max77693; |
Sachin Kamat | 1967fa0 | 2012-11-21 10:04:58 +0530 | [diff] [blame] | 1062 | if (info->max77693->regmap_muic) { |
Chanwoo Choi | b186b12 | 2012-08-21 15:16:23 +0900 | [diff] [blame] | 1063 | dev_dbg(&pdev->dev, "allocate register map\n"); |
Sachin Kamat | 1967fa0 | 2012-11-21 10:04:58 +0530 | [diff] [blame] | 1064 | } else { |
Chanwoo Choi | b186b12 | 2012-08-21 15:16:23 +0900 | [diff] [blame] | 1065 | info->max77693->regmap_muic = devm_regmap_init_i2c( |
| 1066 | info->max77693->muic, |
| 1067 | &max77693_muic_regmap_config); |
| 1068 | if (IS_ERR(info->max77693->regmap_muic)) { |
| 1069 | ret = PTR_ERR(info->max77693->regmap_muic); |
| 1070 | dev_err(max77693->dev, |
| 1071 | "failed to allocate register map: %d\n", ret); |
Sachin Kamat | 3bf742f | 2012-11-21 10:04:57 +0530 | [diff] [blame] | 1072 | return ret; |
Chanwoo Choi | b186b12 | 2012-08-21 15:16:23 +0900 | [diff] [blame] | 1073 | } |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1074 | } |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 1075 | |
| 1076 | /* Register input device for button of dock device */ |
Chanwoo Choi | eff7d74 | 2013-02-13 18:35:05 +0900 | [diff] [blame] | 1077 | info->dock = devm_input_allocate_device(&pdev->dev); |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 1078 | if (!info->dock) { |
| 1079 | dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__); |
| 1080 | return -ENOMEM; |
| 1081 | } |
| 1082 | info->dock->name = "max77693-muic/dock"; |
| 1083 | info->dock->phys = "max77693-muic/extcon"; |
| 1084 | info->dock->dev.parent = &pdev->dev; |
| 1085 | |
| 1086 | __set_bit(EV_REP, info->dock->evbit); |
| 1087 | |
| 1088 | input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP); |
| 1089 | input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN); |
| 1090 | input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE); |
| 1091 | input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG); |
| 1092 | input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG); |
| 1093 | |
| 1094 | ret = input_register_device(info->dock); |
| 1095 | if (ret < 0) { |
| 1096 | dev_err(&pdev->dev, "Cannot register input device error(%d)\n", |
| 1097 | ret); |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1101 | platform_set_drvdata(pdev, info); |
| 1102 | mutex_init(&info->mutex); |
| 1103 | |
| 1104 | INIT_WORK(&info->irq_work, max77693_muic_irq_work); |
| 1105 | |
| 1106 | /* Support irq domain for MAX77693 MUIC device */ |
| 1107 | for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) { |
| 1108 | struct max77693_muic_irq *muic_irq = &muic_irqs[i]; |
Sachin Kamat | 00af4b1 | 2012-11-20 15:46:44 +0900 | [diff] [blame] | 1109 | unsigned int virq = 0; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1110 | |
| 1111 | virq = irq_create_mapping(max77693->irq_domain, muic_irq->irq); |
Sachin Kamat | 00af4b1 | 2012-11-20 15:46:44 +0900 | [diff] [blame] | 1112 | if (!virq) { |
| 1113 | ret = -EINVAL; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1114 | goto err_irq; |
Sachin Kamat | 00af4b1 | 2012-11-20 15:46:44 +0900 | [diff] [blame] | 1115 | } |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1116 | muic_irq->virq = virq; |
| 1117 | |
| 1118 | ret = request_threaded_irq(virq, NULL, |
| 1119 | max77693_muic_irq_handler, |
Chanwoo Choi | ae3b321 | 2012-11-28 12:39:01 +0900 | [diff] [blame] | 1120 | IRQF_NO_SUSPEND, |
| 1121 | muic_irq->name, info); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1122 | if (ret) { |
| 1123 | dev_err(&pdev->dev, |
| 1124 | "failed: irq request (IRQ: %d," |
| 1125 | " error :%d)\n", |
| 1126 | muic_irq->irq, ret); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1127 | goto err_irq; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /* Initialize extcon device */ |
Sachin Kamat | f4bb5cb | 2012-11-20 15:46:50 +0900 | [diff] [blame] | 1132 | info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev), |
| 1133 | GFP_KERNEL); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1134 | if (!info->edev) { |
| 1135 | dev_err(&pdev->dev, "failed to allocate memory for extcon\n"); |
| 1136 | ret = -ENOMEM; |
| 1137 | goto err_irq; |
| 1138 | } |
| 1139 | info->edev->name = DEV_NAME; |
| 1140 | info->edev->supported_cable = max77693_extcon_cable; |
| 1141 | ret = extcon_dev_register(info->edev, NULL); |
| 1142 | if (ret) { |
| 1143 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
Sachin Kamat | f4bb5cb | 2012-11-20 15:46:50 +0900 | [diff] [blame] | 1144 | goto err_irq; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1145 | } |
| 1146 | |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1147 | if (pdata->muic_data) { |
| 1148 | struct max77693_muic_platform_data *muic_pdata = pdata->muic_data; |
Chanwoo Choi | f8457d57 | 2012-10-08 14:41:49 +0900 | [diff] [blame] | 1149 | |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1150 | /* Initialize MUIC register by using platform data */ |
| 1151 | for (i = 0 ; i < muic_pdata->num_init_data ; i++) { |
| 1152 | enum max77693_irq_source irq_src |
| 1153 | = MAX77693_IRQ_GROUP_NR; |
Chanwoo Choi | f8457d57 | 2012-10-08 14:41:49 +0900 | [diff] [blame] | 1154 | |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1155 | max77693_write_reg(info->max77693->regmap_muic, |
| 1156 | muic_pdata->init_data[i].addr, |
| 1157 | muic_pdata->init_data[i].data); |
| 1158 | |
| 1159 | switch (muic_pdata->init_data[i].addr) { |
| 1160 | case MAX77693_MUIC_REG_INTMASK1: |
| 1161 | irq_src = MUIC_INT1; |
| 1162 | break; |
| 1163 | case MAX77693_MUIC_REG_INTMASK2: |
| 1164 | irq_src = MUIC_INT2; |
| 1165 | break; |
| 1166 | case MAX77693_MUIC_REG_INTMASK3: |
| 1167 | irq_src = MUIC_INT3; |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | if (irq_src < MAX77693_IRQ_GROUP_NR) |
| 1172 | info->max77693->irq_masks_cur[irq_src] |
| 1173 | = muic_pdata->init_data[i].data; |
Chanwoo Choi | f8457d57 | 2012-10-08 14:41:49 +0900 | [diff] [blame] | 1174 | } |
| 1175 | |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1176 | /* |
| 1177 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB |
| 1178 | * h/w path of COMP2/COMN1 on CONTROL1 register. |
| 1179 | */ |
| 1180 | if (muic_pdata->path_uart) |
| 1181 | info->path_uart = muic_pdata->path_uart; |
| 1182 | else |
| 1183 | info->path_uart = CONTROL1_SW_UART; |
Chanwoo Choi | f8457d57 | 2012-10-08 14:41:49 +0900 | [diff] [blame] | 1184 | |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1185 | if (muic_pdata->path_usb) |
| 1186 | info->path_usb = muic_pdata->path_usb; |
| 1187 | else |
| 1188 | info->path_usb = CONTROL1_SW_USB; |
Chanwoo Choi | 2b75799 | 2012-12-06 21:27:56 +0900 | [diff] [blame] | 1189 | |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1190 | /* |
| 1191 | * Default delay time for detecting cable state |
| 1192 | * after certain time. |
| 1193 | */ |
| 1194 | if (muic_pdata->detcable_delay_ms) |
| 1195 | delay_jiffies = |
| 1196 | msecs_to_jiffies(muic_pdata->detcable_delay_ms); |
| 1197 | else |
| 1198 | delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT); |
| 1199 | } else { |
Chanwoo Choi | 2b75799 | 2012-12-06 21:27:56 +0900 | [diff] [blame] | 1200 | info->path_usb = CONTROL1_SW_USB; |
Chanwoo Choi | 190d7cf | 2013-02-18 10:03:32 +0900 | [diff] [blame^] | 1201 | info->path_uart = CONTROL1_SW_UART; |
| 1202 | delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT); |
| 1203 | } |
Chanwoo Choi | 2b75799 | 2012-12-06 21:27:56 +0900 | [diff] [blame] | 1204 | |
| 1205 | /* Set initial path for UART */ |
| 1206 | max77693_muic_set_path(info, info->path_uart, true); |
| 1207 | |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1208 | /* Check revision number of MUIC device*/ |
| 1209 | ret = max77693_read_reg(info->max77693->regmap_muic, |
| 1210 | MAX77693_MUIC_REG_ID, &id); |
| 1211 | if (ret < 0) { |
| 1212 | dev_err(&pdev->dev, "failed to read revision number\n"); |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 1213 | goto err_extcon; |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1214 | } |
| 1215 | dev_info(info->dev, "device ID : 0x%x\n", id); |
| 1216 | |
| 1217 | /* Set ADC debounce time */ |
| 1218 | max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS); |
| 1219 | |
Chanwoo Choi | 297620f | 2012-12-26 13:10:11 +0900 | [diff] [blame] | 1220 | /* |
| 1221 | * Detect accessory after completing the initialization of platform |
| 1222 | * |
| 1223 | * - Use delayed workqueue to detect cable state and then |
| 1224 | * notify cable state to notifiee/platform through uevent. |
| 1225 | * After completing the booting of platform, the extcon provider |
| 1226 | * driver should notify cable state to upper layer. |
| 1227 | */ |
| 1228 | INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq); |
Chanwoo Choi | 297620f | 2012-12-26 13:10:11 +0900 | [diff] [blame] | 1229 | schedule_delayed_work(&info->wq_detcable, delay_jiffies); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1230 | |
| 1231 | return ret; |
| 1232 | |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 1233 | err_extcon: |
| 1234 | extcon_dev_unregister(info->edev); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1235 | err_irq: |
Sachin Kamat | 00af4b1 | 2012-11-20 15:46:44 +0900 | [diff] [blame] | 1236 | while (--i >= 0) |
| 1237 | free_irq(muic_irqs[i].virq, info); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1238 | return ret; |
| 1239 | } |
| 1240 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 1241 | static int max77693_muic_remove(struct platform_device *pdev) |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1242 | { |
| 1243 | struct max77693_muic_info *info = platform_get_drvdata(pdev); |
| 1244 | int i; |
| 1245 | |
| 1246 | for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) |
| 1247 | free_irq(muic_irqs[i].virq, info); |
| 1248 | cancel_work_sync(&info->irq_work); |
Chanwoo Choi | 39bf369 | 2012-12-03 13:09:41 +0900 | [diff] [blame] | 1249 | input_unregister_device(info->dock); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1250 | extcon_dev_unregister(info->edev); |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static struct platform_driver max77693_muic_driver = { |
| 1256 | .driver = { |
| 1257 | .name = DEV_NAME, |
| 1258 | .owner = THIS_MODULE, |
| 1259 | }, |
| 1260 | .probe = max77693_muic_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 1261 | .remove = max77693_muic_remove, |
Chanwoo Choi | db1b903 | 2012-07-17 13:28:28 +0900 | [diff] [blame] | 1262 | }; |
| 1263 | |
| 1264 | module_platform_driver(max77693_muic_driver); |
| 1265 | |
| 1266 | MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver"); |
| 1267 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
| 1268 | MODULE_LICENSE("GPL"); |
| 1269 | MODULE_ALIAS("platform:extcon-max77693"); |