/* * Use delayed workqueue to detect cable state and then * notify cable state to notifiee/platform through uevent. * After completing the booting of platform, the extcon provider * driver should notify cable state to upper layer.
*/ struct delayed_work wq_detcable;
};
/* Change DM_CON/DP_CON/VBUSIN switch according to cable type */ staticint sm5502_muic_set_path(struct sm5502_muic_info *info, unsignedint con_sw, unsignedint vbus_sw, bool attached)
{ int ret;
if (!attached) {
con_sw = DM_DP_SWITCH_OPEN;
vbus_sw = VBUSIN_SWITCH_OPEN;
}
switch (con_sw) { case DM_DP_SWITCH_OPEN: case DM_DP_SWITCH_USB: case DM_DP_SWITCH_AUDIO: case DM_DP_SWITCH_UART:
ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
SM5502_REG_MANUAL_SW1_DP_MASK |
SM5502_REG_MANUAL_SW1_DM_MASK,
con_sw); if (ret < 0) {
dev_err(info->dev, "cannot update DM_CON/DP_CON switch\n"); return ret;
} break; default:
dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n",
con_sw); return -EINVAL;
}
switch (vbus_sw) { case VBUSIN_SWITCH_OPEN: case VBUSIN_SWITCH_VBUSOUT: case VBUSIN_SWITCH_MIC: case VBUSIN_SWITCH_VBUSOUT_WITH_USB:
ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1,
SM5502_REG_MANUAL_SW1_VBUSIN_MASK,
vbus_sw); if (ret < 0) {
dev_err(info->dev, "cannot update VBUSIN switch\n"); return ret;
} break; default:
dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw); return -EINVAL;
}
return 0;
}
/* Return cable type of attached or detached accessories */ staticunsignedint sm5502_muic_get_cable_type(struct sm5502_muic_info *info)
{ unsignedint cable_type, adc, dev_type1; int ret;
/* Read ADC value according to external cable or button */
ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); if (ret) {
dev_err(info->dev, "failed to read ADC register\n"); return ret;
}
/* * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't * connected with to MUIC device.
*/
cable_type = adc & SM5502_REG_ADC_MASK;
switch (cable_type) { case SM5502_MUIC_ADC_GROUND:
ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
&dev_type1); if (ret) {
dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); return ret;
}
if (dev_type1 == info->type->otg_dev_type1) {
cable_type = SM5502_MUIC_ADC_GROUND_USB_OTG;
} else {
dev_dbg(info->dev, "cannot identify the cable type: adc(0x%x), dev_type1(0x%x)\n",
adc, dev_type1); return -EINVAL;
} break; case SM5502_MUIC_ADC_SEND_END_BUTTON: case SM5502_MUIC_ADC_REMOTE_S1_BUTTON: case SM5502_MUIC_ADC_REMOTE_S2_BUTTON: case SM5502_MUIC_ADC_REMOTE_S3_BUTTON: case SM5502_MUIC_ADC_REMOTE_S4_BUTTON: case SM5502_MUIC_ADC_REMOTE_S5_BUTTON: case SM5502_MUIC_ADC_REMOTE_S6_BUTTON: case SM5502_MUIC_ADC_REMOTE_S7_BUTTON: case SM5502_MUIC_ADC_REMOTE_S8_BUTTON: case SM5502_MUIC_ADC_REMOTE_S9_BUTTON: case SM5502_MUIC_ADC_REMOTE_S10_BUTTON: case SM5502_MUIC_ADC_REMOTE_S11_BUTTON: case SM5502_MUIC_ADC_REMOTE_S12_BUTTON: case SM5502_MUIC_ADC_RESERVED_ACC_1: case SM5502_MUIC_ADC_RESERVED_ACC_2: case SM5502_MUIC_ADC_RESERVED_ACC_3: case SM5502_MUIC_ADC_RESERVED_ACC_4: case SM5502_MUIC_ADC_RESERVED_ACC_5: case SM5502_MUIC_ADC_AUDIO_TYPE2: case SM5502_MUIC_ADC_PHONE_POWERED_DEV: case SM5502_MUIC_ADC_TTY_CONVERTER: case SM5502_MUIC_ADC_UART_CABLE: case SM5502_MUIC_ADC_TYPE1_CHARGER: case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE: case SM5502_MUIC_ADC_TYPE2_CHARGER: case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: break; case SM5502_MUIC_ADC_AUDIO_TYPE1: /* * Check whether cable type is * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END * by using Button event.
*/ break; case SM5502_MUIC_ADC_OPEN:
ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1,
&dev_type1); if (ret) {
dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); return ret;
}
if (dev_type1 == info->type->otg_dev_type1) {
cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG; break;
}
switch (dev_type1) { case SM5502_REG_DEV_TYPE1_USB_SDP_MASK:
cable_type = SM5502_MUIC_ADC_OPEN_USB; break; case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK:
cable_type = SM5502_MUIC_ADC_OPEN_TA; break; default:
dev_dbg(info->dev, "cannot identify the cable type: adc(0x%x)\n",
adc); return -EINVAL;
} break; default:
dev_err(info->dev, "failed to identify the cable type: adc(0x%x)\n", adc); return -EINVAL;
}
/* Get the type of attached or detached cable */ if (attached)
cable_type = sm5502_muic_get_cable_type(info); else
cable_type = prev_cable_type;
prev_cable_type = cable_type;
switch (cable_type) { case SM5502_MUIC_ADC_OPEN_USB:
id = EXTCON_USB;
con_sw = DM_DP_SWITCH_USB;
vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB; break; case SM5502_MUIC_ADC_OPEN_TA:
id = EXTCON_CHG_USB_DCP;
con_sw = DM_DP_SWITCH_OPEN;
vbus_sw = VBUSIN_SWITCH_VBUSOUT; break; case SM5502_MUIC_ADC_GROUND_USB_OTG: case SM5502_MUIC_ADC_OPEN_USB_OTG:
id = EXTCON_USB_HOST;
con_sw = DM_DP_SWITCH_USB;
vbus_sw = VBUSIN_SWITCH_OPEN; break; default:
dev_dbg(info->dev, "cannot handle this cable_type (0x%x)\n", cable_type); return 0;
}
/* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */
ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached); if (ret < 0) return ret;
/* Change the state of external accessory */
extcon_set_state_sync(info->edev, id, attached); if (id == EXTCON_USB)
extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
attached);
return 0;
}
staticvoid sm5502_muic_irq_work(struct work_struct *work)
{ struct sm5502_muic_info *info = container_of(work, struct sm5502_muic_info, irq_work); int ret = 0;
if (!info->edev) return;
mutex_lock(&info->mutex);
/* Detect attached or detached cables */ if (info->irq_attach) {
ret = sm5502_muic_cable_handler(info, true);
info->irq_attach = false;
} if (info->irq_detach) {
ret = sm5502_muic_cable_handler(info, false);
info->irq_detach = false;
}
if (ret < 0)
dev_err(info->dev, "failed to handle MUIC interrupt\n");
mutex_unlock(&info->mutex);
}
/* * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0. * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
*/ staticint sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type)
{ switch (irq_type) { case SM5502_IRQ_INT1_ATTACH:
info->irq_attach = true; break; case SM5502_IRQ_INT1_DETACH:
info->irq_detach = true; break; case SM5502_IRQ_INT1_KP: case SM5502_IRQ_INT1_LKP: case SM5502_IRQ_INT1_LKR: case SM5502_IRQ_INT1_OVP_EVENT: case SM5502_IRQ_INT1_OCP_EVENT: case SM5502_IRQ_INT1_OVP_OCP_DIS: case SM5502_IRQ_INT2_VBUS_DET: case SM5502_IRQ_INT2_REV_ACCE: case SM5502_IRQ_INT2_ADC_CHG: case SM5502_IRQ_INT2_STUCK_KEY: case SM5502_IRQ_INT2_STUCK_KEY_RCV: case SM5502_IRQ_INT2_MHL: default: break;
}
return 0;
}
staticint sm5504_parse_irq(struct sm5502_muic_info *info, int irq_type)
{ switch (irq_type) { case SM5504_IRQ_INT1_ATTACH:
info->irq_attach = true; break; case SM5504_IRQ_INT1_DETACH:
info->irq_detach = true; break; case SM5504_IRQ_INT1_CHG_DET: case SM5504_IRQ_INT1_DCD_OUT: case SM5504_IRQ_INT1_OVP_EVENT: case SM5504_IRQ_INT1_CONNECT: case SM5504_IRQ_INT1_ADC_CHG: case SM5504_IRQ_INT2_RID_CHG: case SM5504_IRQ_INT2_UVLO: case SM5504_IRQ_INT2_POR: case SM5504_IRQ_INT2_OVP_FET: case SM5504_IRQ_INT2_OCP_LATCH: case SM5504_IRQ_INT2_OCP_EVENT: case SM5504_IRQ_INT2_OVP_OCP_EVENT: default: break;
}
return 0;
}
static irqreturn_t sm5502_muic_irq_handler(int irq, void *data)
{ struct sm5502_muic_info *info = data; int i, irq_type = -1, ret;
for (i = 0; i < info->type->num_muic_irqs; i++) if (irq == info->type->muic_irqs[i].virq)
irq_type = info->type->muic_irqs[i].irq;
ret = info->type->parse_irq(info, irq_type); if (ret < 0) {
dev_warn(info->dev, "cannot handle is interrupt:%d\n",
irq_type); return IRQ_HANDLED;
}
schedule_work(&info->irq_work);
/* Notify the state of connector cable or not */
ret = sm5502_muic_cable_handler(info, true); if (ret < 0)
dev_warn(info->dev, "failed to detect cable state\n");
}
staticvoid sm5502_init_dev_type(struct sm5502_muic_info *info)
{ unsignedint reg_data, vendor_id, version_id; int i, ret;
/* To test I2C, Print version_id and vendor_id of SM5502 */
ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); if (ret) {
dev_err(info->dev, "failed to read DEVICE_ID register: %d\n", ret); return;
}
/* Initiazle the register of SM5502 device to bring-up */ for (i = 0; i < info->type->num_reg_data; i++) { unsignedint val = 0;
if (!info->type->reg_data[i].invert)
val |= ~info->type->reg_data[i].val; else
val = info->type->reg_data[i].val;
regmap_write(info->regmap, info->type->reg_data[i].reg, val);
}
}
staticint sm5022_muic_i2c_probe(struct i2c_client *i2c)
{ struct device_node *np = i2c->dev.of_node; struct sm5502_muic_info *info; int i, ret, irq_flags;
if (!np) return -EINVAL;
info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM;
i2c_set_clientdata(i2c, info);
info->dev = &i2c->dev;
info->i2c = i2c;
info->irq = i2c->irq;
info->type = device_get_match_data(info->dev); if (!info->type) return -EINVAL; if (!info->type->parse_irq) {
dev_err(info->dev, "parse_irq missing in struct sm5502_type\n"); return -EINVAL;
}
mutex_init(&info->mutex);
INIT_WORK(&info->irq_work, sm5502_muic_irq_work);
info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); if (IS_ERR(info->regmap)) {
ret = PTR_ERR(info->regmap);
dev_err(info->dev, "failed to allocate register map: %d\n",
ret); return ret;
}
/* Support irq domain for SM5502 MUIC device */
irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
ret = devm_regmap_add_irq_chip(info->dev, info->regmap, info->irq,
irq_flags, 0, info->type->irq_chip,
&info->irq_data); if (ret != 0) {
dev_err(info->dev, "failed to request IRQ %d: %d\n",
info->irq, ret); return ret;
}
for (i = 0; i < info->type->num_muic_irqs; i++) { struct muic_irq *muic_irq = &info->type->muic_irqs[i]; int virq = 0;
/* Allocate extcon device */
info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); if (IS_ERR(info->edev)) {
dev_err(info->dev, "failed to allocate memory for extcon\n"); return -ENOMEM;
}
/* Register extcon device */
ret = devm_extcon_dev_register(info->dev, info->edev); if (ret) {
dev_err(info->dev, "failed to register extcon device\n"); return ret;
}
/* * Detect accessory after completing the initialization of platform * * - Use delayed workqueue to detect cable state and then * notify cable state to notifiee/platform through uevent. * After completing the booting of platform, the extcon provider * driver should notify cable state to upper layer.
*/
INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
msecs_to_jiffies(DELAY_MS_DEFAULT));
/* Initialize SM5502 device and print vendor id and version id */
sm5502_init_dev_type(info);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.