Lines Matching +full:chg +full:- +full:int

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches
18 #include <linux/extcon-provider.h>
20 #include "extcon-sm5502.h"
25 unsigned int irq;
27 unsigned int virq;
32 unsigned int val;
45 int irq;
63 unsigned int num_muic_irqs;
67 unsigned int num_reg_data;
69 unsigned int otg_dev_type1;
70 int (*parse_irq)(struct sm5502_muic_info *info, int irq_type);
129 static const unsigned int sm5502_extcon_cable[] = {
176 /* |---------|--ADC| */
180 /* |Dev Type1|--ADC| */
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" },
237 { SM5504_IRQ_INT1_ATTACH, "muic-attach" },
238 { SM5504_IRQ_INT1_DETACH, "muic-detach" },
239 { SM5504_IRQ_INT1_CHG_DET, "muic-chg-det" },
240 { SM5504_IRQ_INT1_DCD_OUT, "muic-dcd-out" },
241 { SM5504_IRQ_INT1_OVP_EVENT, "muic-ovp-event" },
242 { SM5504_IRQ_INT1_CONNECT, "muic-connect" },
243 { SM5504_IRQ_INT1_ADC_CHG, "muic-adc-chg" },
244 { SM5504_IRQ_INT2_RID_CHG, "muic-rid-chg" },
245 { SM5504_IRQ_INT2_UVLO, "muic-uvlo" },
246 { SM5504_IRQ_INT2_POR, "muic-por" },
247 { SM5504_IRQ_INT2_OVP_FET, "muic-ovp-fet" },
248 { SM5504_IRQ_INT2_OCP_LATCH, "muic-ocp-latch" },
249 { SM5504_IRQ_INT2_OCP_EVENT, "muic-ocp-event" },
250 { SM5504_IRQ_INT2_OVP_OCP_EVENT, "muic-ovp-ocp-event" },
284 static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg) in sm5502_muic_volatile_reg()
304 static int sm5502_muic_set_path(struct sm5502_muic_info *info, in sm5502_muic_set_path()
305 unsigned int con_sw, unsigned int vbus_sw, in sm5502_muic_set_path()
308 int ret; in sm5502_muic_set_path()
320 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, in sm5502_muic_set_path()
325 dev_err(info->dev, in sm5502_muic_set_path()
331 dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n", in sm5502_muic_set_path()
333 return -EINVAL; in sm5502_muic_set_path()
341 ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, in sm5502_muic_set_path()
345 dev_err(info->dev, in sm5502_muic_set_path()
351 dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw); in sm5502_muic_set_path()
352 return -EINVAL; in sm5502_muic_set_path()
359 static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info) in sm5502_muic_get_cable_type()
361 unsigned int cable_type, adc, dev_type1; in sm5502_muic_get_cable_type()
362 int ret; in sm5502_muic_get_cable_type()
365 ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); in sm5502_muic_get_cable_type()
367 dev_err(info->dev, "failed to read ADC register\n"); in sm5502_muic_get_cable_type()
379 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, in sm5502_muic_get_cable_type()
382 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); in sm5502_muic_get_cable_type()
386 if (dev_type1 == info->type->otg_dev_type1) { in sm5502_muic_get_cable_type()
389 dev_dbg(info->dev, in sm5502_muic_get_cable_type()
392 return -EINVAL; in sm5502_muic_get_cable_type()
434 ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, in sm5502_muic_get_cable_type()
437 dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); in sm5502_muic_get_cable_type()
441 if (dev_type1 == info->type->otg_dev_type1) { in sm5502_muic_get_cable_type()
454 dev_dbg(info->dev, in sm5502_muic_get_cable_type()
457 return -EINVAL; in sm5502_muic_get_cable_type()
461 dev_err(info->dev, in sm5502_muic_get_cable_type()
463 return -EINVAL; in sm5502_muic_get_cable_type()
469 static int sm5502_muic_cable_handler(struct sm5502_muic_info *info, in sm5502_muic_cable_handler()
472 static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND; in sm5502_muic_cable_handler()
473 unsigned int cable_type = SM5502_MUIC_ADC_GROUND; in sm5502_muic_cable_handler()
474 unsigned int con_sw = DM_DP_SWITCH_OPEN; in sm5502_muic_cable_handler()
475 unsigned int vbus_sw = VBUSIN_SWITCH_OPEN; in sm5502_muic_cable_handler()
476 unsigned int id; in sm5502_muic_cable_handler()
477 int ret; in sm5502_muic_cable_handler()
504 dev_dbg(info->dev, in sm5502_muic_cable_handler()
515 extcon_set_state_sync(info->edev, id, attached); in sm5502_muic_cable_handler()
517 extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP, in sm5502_muic_cable_handler()
527 int ret = 0; in sm5502_muic_irq_work()
529 if (!info->edev) in sm5502_muic_irq_work()
532 mutex_lock(&info->mutex); in sm5502_muic_irq_work()
535 if (info->irq_attach) { in sm5502_muic_irq_work()
537 info->irq_attach = false; in sm5502_muic_irq_work()
539 if (info->irq_detach) { in sm5502_muic_irq_work()
541 info->irq_detach = false; in sm5502_muic_irq_work()
545 dev_err(info->dev, "failed to handle MUIC interrupt\n"); in sm5502_muic_irq_work()
547 mutex_unlock(&info->mutex); in sm5502_muic_irq_work()
552 * Returns -ESRCH if irq_type does not match registered IRQ for this dev type.
554 static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type) in sm5502_parse_irq()
558 info->irq_attach = true; in sm5502_parse_irq()
561 info->irq_detach = true; in sm5502_parse_irq()
582 static int sm5504_parse_irq(struct sm5502_muic_info *info, int irq_type) in sm5504_parse_irq()
586 info->irq_attach = true; in sm5504_parse_irq()
589 info->irq_detach = true; in sm5504_parse_irq()
610 static irqreturn_t sm5502_muic_irq_handler(int irq, void *data) in sm5502_muic_irq_handler()
613 int i, irq_type = -1, ret; in sm5502_muic_irq_handler()
615 for (i = 0; i < info->type->num_muic_irqs; i++) in sm5502_muic_irq_handler()
616 if (irq == info->type->muic_irqs[i].virq) in sm5502_muic_irq_handler()
617 irq_type = info->type->muic_irqs[i].irq; in sm5502_muic_irq_handler()
619 ret = info->type->parse_irq(info, irq_type); in sm5502_muic_irq_handler()
621 dev_warn(info->dev, "cannot handle is interrupt:%d\n", in sm5502_muic_irq_handler()
625 schedule_work(&info->irq_work); in sm5502_muic_irq_handler()
634 int ret; in sm5502_muic_detect_cable_wq()
639 dev_warn(info->dev, "failed to detect cable state\n"); in sm5502_muic_detect_cable_wq()
644 unsigned int reg_data, vendor_id, version_id; in sm5502_init_dev_type()
645 int i, ret; in sm5502_init_dev_type()
648 ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, &reg_data); in sm5502_init_dev_type()
650 dev_err(info->dev, in sm5502_init_dev_type()
660 dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", in sm5502_init_dev_type()
663 /* Initiazle the register of SM5502 device to bring-up */ in sm5502_init_dev_type()
664 for (i = 0; i < info->type->num_reg_data; i++) { in sm5502_init_dev_type()
665 unsigned int val = 0; in sm5502_init_dev_type()
667 if (!info->type->reg_data[i].invert) in sm5502_init_dev_type()
668 val |= ~info->type->reg_data[i].val; in sm5502_init_dev_type()
670 val = info->type->reg_data[i].val; in sm5502_init_dev_type()
671 regmap_write(info->regmap, info->type->reg_data[i].reg, val); in sm5502_init_dev_type()
675 static int sm5022_muic_i2c_probe(struct i2c_client *i2c) in sm5022_muic_i2c_probe()
677 struct device_node *np = i2c->dev.of_node; in sm5022_muic_i2c_probe()
679 int i, ret, irq_flags; in sm5022_muic_i2c_probe()
682 return -EINVAL; in sm5022_muic_i2c_probe()
684 info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); in sm5022_muic_i2c_probe()
686 return -ENOMEM; in sm5022_muic_i2c_probe()
689 info->dev = &i2c->dev; in sm5022_muic_i2c_probe()
690 info->i2c = i2c; in sm5022_muic_i2c_probe()
691 info->irq = i2c->irq; in sm5022_muic_i2c_probe()
692 info->type = device_get_match_data(info->dev); in sm5022_muic_i2c_probe()
693 if (!info->type) in sm5022_muic_i2c_probe()
694 return -EINVAL; in sm5022_muic_i2c_probe()
695 if (!info->type->parse_irq) { in sm5022_muic_i2c_probe()
696 dev_err(info->dev, "parse_irq missing in struct sm5502_type\n"); in sm5022_muic_i2c_probe()
697 return -EINVAL; in sm5022_muic_i2c_probe()
700 mutex_init(&info->mutex); in sm5022_muic_i2c_probe()
702 INIT_WORK(&info->irq_work, sm5502_muic_irq_work); in sm5022_muic_i2c_probe()
704 info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); in sm5022_muic_i2c_probe()
705 if (IS_ERR(info->regmap)) { in sm5022_muic_i2c_probe()
706 ret = PTR_ERR(info->regmap); in sm5022_muic_i2c_probe()
707 dev_err(info->dev, "failed to allocate register map: %d\n", in sm5022_muic_i2c_probe()
714 ret = devm_regmap_add_irq_chip(info->dev, info->regmap, info->irq, in sm5022_muic_i2c_probe()
715 irq_flags, 0, info->type->irq_chip, in sm5022_muic_i2c_probe()
716 &info->irq_data); in sm5022_muic_i2c_probe()
718 dev_err(info->dev, "failed to request IRQ %d: %d\n", in sm5022_muic_i2c_probe()
719 info->irq, ret); in sm5022_muic_i2c_probe()
723 for (i = 0; i < info->type->num_muic_irqs; i++) { in sm5022_muic_i2c_probe()
724 struct muic_irq *muic_irq = &info->type->muic_irqs[i]; in sm5022_muic_i2c_probe()
725 int virq = 0; in sm5022_muic_i2c_probe()
727 virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); in sm5022_muic_i2c_probe()
729 return -EINVAL; in sm5022_muic_i2c_probe()
730 muic_irq->virq = virq; in sm5022_muic_i2c_probe()
732 ret = devm_request_threaded_irq(info->dev, virq, NULL, in sm5022_muic_i2c_probe()
735 muic_irq->name, info); in sm5022_muic_i2c_probe()
737 dev_err(info->dev, in sm5022_muic_i2c_probe()
739 muic_irq->irq, ret); in sm5022_muic_i2c_probe()
745 info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); in sm5022_muic_i2c_probe()
746 if (IS_ERR(info->edev)) { in sm5022_muic_i2c_probe()
747 dev_err(info->dev, "failed to allocate memory for extcon\n"); in sm5022_muic_i2c_probe()
748 return -ENOMEM; in sm5022_muic_i2c_probe()
752 ret = devm_extcon_dev_register(info->dev, info->edev); in sm5022_muic_i2c_probe()
754 dev_err(info->dev, "failed to register extcon device\n"); in sm5022_muic_i2c_probe()
761 * - Use delayed workqueue to detect cable state and then in sm5022_muic_i2c_probe()
766 INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq); in sm5022_muic_i2c_probe()
767 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, in sm5022_muic_i2c_probe()
797 { .compatible = "siliconmitus,sm5502-muic", .data = &sm5502_data },
798 { .compatible = "siliconmitus,sm5504-muic", .data = &sm5504_data },
799 { .compatible = "siliconmitus,sm5703-muic", .data = &sm5502_data },
805 static int sm5502_muic_suspend(struct device *dev) in sm5502_muic_suspend()
810 enable_irq_wake(info->irq); in sm5502_muic_suspend()
815 static int sm5502_muic_resume(struct device *dev) in sm5502_muic_resume()
820 disable_irq_wake(info->irq); in sm5502_muic_resume()
832 { "sm5703-muic", (kernel_ulong_t)&sm5502_data },
847 static int __init sm5502_muic_i2c_init(void) in sm5502_muic_i2c_init()