Lines Matching +full:invert +full:- +full:interrupt
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches
11 #include <linux/interrupt.h>
18 #include <linux/extcon-provider.h>
20 #include "extcon-sm5502.h"
33 bool invert; member
78 .invert = true,
82 .invert = false,
88 .invert = true,
97 .invert = true,
106 .invert = true,
111 .invert = false,
117 .invert = true,
124 .invert = true,
176 /* |---------|--ADC| */
180 /* |Dev Type1|--ADC| */
187 /* List of supported interrupt for SM5502 */
189 { SM5502_IRQ_INT1_ATTACH, "muic-attach" },
190 { SM5502_IRQ_INT1_DETACH, "muic-detach" },
191 { SM5502_IRQ_INT1_KP, "muic-kp" },
192 { SM5502_IRQ_INT1_LKP, "muic-lkp" },
193 { SM5502_IRQ_INT1_LKR, "muic-lkr" },
194 { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
195 { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" },
196 { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" },
197 { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" },
198 { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" },
199 { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" },
200 { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" },
201 { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" },
202 { SM5502_IRQ_INT2_MHL, "muic-mhl" },
205 /* Define interrupt list of SM5502 to register regmap_irq */
236 /* List of supported interrupt for SM5504 */
238 { SM5504_IRQ_INT1_ATTACH, "muic-attach" },
239 { SM5504_IRQ_INT1_DETACH, "muic-detach" },
240 { SM5504_IRQ_INT1_CHG_DET, "muic-chg-det" },
241 { SM5504_IRQ_INT1_DCD_OUT, "muic-dcd-out" },
242 { SM5504_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
243 { SM5504_IRQ_INT1_CONNECT, "muic-connect" },
244 { SM5504_IRQ_INT1_ADC_CHG, "muic-adc-chg" },
245 { SM5504_IRQ_INT2_RID_CHG, "muic-rid-chg" },
246 { SM5504_IRQ_INT2_UVLO, "muic-uvlo" },
247 { SM5504_IRQ_INT2_POR, "muic-por" },
248 { SM5504_IRQ_INT2_OVP_FET, "muic-ovp-fet" },
249 { SM5504_IRQ_INT2_OCP_LATCH, "muic-ocp-latch" },
250 { SM5504_IRQ_INT2_OCP_EVENT, "muic-ocp-event" },
251 { SM5504_IRQ_INT2_OVP_OCP_EVENT, "muic-ovp-ocp-event" },
254 /* Define interrupt list of SM5504 to register regmap_irq */
322 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, in sm5502_muic_set_path()
327 dev_err(info->dev, in sm5502_muic_set_path()
333 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n", in sm5502_muic_set_path()
335 return -EINVAL; in sm5502_muic_set_path()
343 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, in sm5502_muic_set_path()
347 dev_err(info->dev, in sm5502_muic_set_path()
353 dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw); in sm5502_muic_set_path()
354 return -EINVAL; in sm5502_muic_set_path()
367 ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); in sm5502_muic_get_cable_type()
369 dev_err(info->dev, "failed to read ADC register\n"); in sm5502_muic_get_cable_type()
381 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, in sm5502_muic_get_cable_type()
384 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); in sm5502_muic_get_cable_type()
388 if (dev_type1 == info->type->otg_dev_type1) { in sm5502_muic_get_cable_type()
391 dev_dbg(info->dev, in sm5502_muic_get_cable_type()
394 return -EINVAL; in sm5502_muic_get_cable_type()
436 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, in sm5502_muic_get_cable_type()
439 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); in sm5502_muic_get_cable_type()
443 if (dev_type1 == info->type->otg_dev_type1) { in sm5502_muic_get_cable_type()
456 dev_dbg(info->dev, in sm5502_muic_get_cable_type()
459 return -EINVAL; in sm5502_muic_get_cable_type()
463 dev_err(info->dev, in sm5502_muic_get_cable_type()
465 return -EINVAL; in sm5502_muic_get_cable_type()
506 dev_dbg(info->dev, in sm5502_muic_cable_handler()
517 extcon_set_state_sync(info->edev, id, attached); in sm5502_muic_cable_handler()
519 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP, in sm5502_muic_cable_handler()
531 if (!info->edev) in sm5502_muic_irq_work()
534 mutex_lock(&info->mutex); in sm5502_muic_irq_work()
537 if (info->irq_attach) { in sm5502_muic_irq_work()
539 info->irq_attach = false; in sm5502_muic_irq_work()
541 if (info->irq_detach) { in sm5502_muic_irq_work()
543 info->irq_detach = false; in sm5502_muic_irq_work()
547 dev_err(info->dev, "failed to handle MUIC interrupt\n"); in sm5502_muic_irq_work()
549 mutex_unlock(&info->mutex); in sm5502_muic_irq_work()
554 * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
560 info->irq_attach = true; in sm5502_parse_irq()
563 info->irq_detach = true; in sm5502_parse_irq()
588 info->irq_attach = true; in sm5504_parse_irq()
591 info->irq_detach = true; in sm5504_parse_irq()
615 int i, irq_type = -1, ret; in sm5502_muic_irq_handler()
617 for (i = 0; i < info->type->num_muic_irqs; i++) in sm5502_muic_irq_handler()
618 if (irq == info->type->muic_irqs[i].virq) in sm5502_muic_irq_handler()
619 irq_type = info->type->muic_irqs[i].irq; in sm5502_muic_irq_handler()
621 ret = info->type->parse_irq(info, irq_type); in sm5502_muic_irq_handler()
623 dev_warn(info->dev, "cannot handle is interrupt:%d\n", in sm5502_muic_irq_handler()
627 schedule_work(&info->irq_work); in sm5502_muic_irq_handler()
641 dev_warn(info->dev, "failed to detect cable state\n"); in sm5502_muic_detect_cable_wq()
650 ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); in sm5502_init_dev_type()
652 dev_err(info->dev, in sm5502_init_dev_type()
662 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", in sm5502_init_dev_type()
665 /* Initiazle the register of SM5502 device to bring-up */ in sm5502_init_dev_type()
666 for (i = 0; i < info->type->num_reg_data; i++) { in sm5502_init_dev_type()
669 if (!info->type->reg_data[i].invert) in sm5502_init_dev_type()
670 val |= ~info->type->reg_data[i].val; in sm5502_init_dev_type()
672 val = info->type->reg_data[i].val; in sm5502_init_dev_type()
673 regmap_write(info->regmap, info->type->reg_data[i].reg, val); in sm5502_init_dev_type()
679 struct device_node *np = i2c->dev.of_node; in sm5022_muic_i2c_probe()
684 return -EINVAL; in sm5022_muic_i2c_probe()
686 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); in sm5022_muic_i2c_probe()
688 return -ENOMEM; in sm5022_muic_i2c_probe()
691 info->dev = &i2c->dev; in sm5022_muic_i2c_probe()
692 info->i2c = i2c; in sm5022_muic_i2c_probe()
693 info->irq = i2c->irq; in sm5022_muic_i2c_probe()
694 info->type = device_get_match_data(info->dev); in sm5022_muic_i2c_probe()
695 if (!info->type) in sm5022_muic_i2c_probe()
696 return -EINVAL; in sm5022_muic_i2c_probe()
697 if (!info->type->parse_irq) { in sm5022_muic_i2c_probe()
698 dev_err(info->dev, "parse_irq missing in struct sm5502_type\n"); in sm5022_muic_i2c_probe()
699 return -EINVAL; in sm5022_muic_i2c_probe()
702 mutex_init(&info->mutex); in sm5022_muic_i2c_probe()
704 INIT_WORK(&info->irq_work, sm5502_muic_irq_work); in sm5022_muic_i2c_probe()
706 info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); in sm5022_muic_i2c_probe()
707 if (IS_ERR(info->regmap)) { in sm5022_muic_i2c_probe()
708 ret = PTR_ERR(info->regmap); in sm5022_muic_i2c_probe()
709 dev_err(info->dev, "failed to allocate register map: %d\n", in sm5022_muic_i2c_probe()
716 ret = devm_regmap_add_irq_chip(info->dev, info->regmap, info->irq, in sm5022_muic_i2c_probe()
717 irq_flags, 0, info->type->irq_chip, in sm5022_muic_i2c_probe()
718 &info->irq_data); in sm5022_muic_i2c_probe()
720 dev_err(info->dev, "failed to request IRQ %d: %d\n", in sm5022_muic_i2c_probe()
721 info->irq, ret); in sm5022_muic_i2c_probe()
725 for (i = 0; i < info->type->num_muic_irqs; i++) { in sm5022_muic_i2c_probe()
726 struct muic_irq *muic_irq = &info->type->muic_irqs[i]; in sm5022_muic_i2c_probe()
729 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); in sm5022_muic_i2c_probe()
731 return -EINVAL; in sm5022_muic_i2c_probe()
732 muic_irq->virq = virq; in sm5022_muic_i2c_probe()
734 ret = devm_request_threaded_irq(info->dev, virq, NULL, in sm5022_muic_i2c_probe()
737 muic_irq->name, info); in sm5022_muic_i2c_probe()
739 dev_err(info->dev, in sm5022_muic_i2c_probe()
741 muic_irq->irq, ret); in sm5022_muic_i2c_probe()
747 info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); in sm5022_muic_i2c_probe()
748 if (IS_ERR(info->edev)) { in sm5022_muic_i2c_probe()
749 dev_err(info->dev, "failed to allocate memory for extcon\n"); in sm5022_muic_i2c_probe()
750 return -ENOMEM; in sm5022_muic_i2c_probe()
754 ret = devm_extcon_dev_register(info->dev, info->edev); in sm5022_muic_i2c_probe()
756 dev_err(info->dev, "failed to register extcon device\n"); in sm5022_muic_i2c_probe()
763 * - Use delayed workqueue to detect cable state and then in sm5022_muic_i2c_probe()
768 INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq); in sm5022_muic_i2c_probe()
769 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, in sm5022_muic_i2c_probe()
799 { .compatible = "siliconmitus,sm5502-muic", .data = &sm5502_data },
800 { .compatible = "siliconmitus,sm5504-muic", .data = &sm5504_data },
811 enable_irq_wake(info->irq); in sm5502_muic_suspend()
821 disable_irq_wake(info->irq); in sm5502_muic_resume()