blob: 2a8e3e64bdb4d9e03e01a8d3c405eb4f3a7ca987 [file] [log] [blame]
/*
* sm5713-irq.c - Interrupt controller support for sm5713
*
* Copyright (C) 2017 Samsung Electronics Co.Ltd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/err.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/mfd/sm5713.h>
#include <linux/mfd/sm5713-private.h>
static const u8 sm5713_mask_reg[] = {
[MUIC_INT1] = SM5713_MUIC_REG_INTMASK1,
[MUIC_INT2] = SM5713_MUIC_REG_INTMASK2,
[CHG_INT1] = SM5713_CHG_REG_INTMSK1,
[CHG_INT2] = SM5713_CHG_REG_INTMSK2,
[CHG_INT3] = SM5713_CHG_REG_INTMSK3,
[CHG_INT4] = SM5713_CHG_REG_INTMSK4,
[CHG_INT5] = SM5713_CHG_REG_INTMSK5,
[CHG_INT6] = SM5713_CHG_REG_INTMSK6,
[FG_INT] = SM5713_FG_REG_INTFG_MASK,
};
struct sm5713_irq_data {
int mask;
int group;
};
#define DECLARE_IRQ(idx, _group, _mask) \
[(idx)] = { .group = (_group), .mask = (_mask) }
static const struct sm5713_irq_data sm5713_irqs[] = {
/* MUIC-irqs */
DECLARE_IRQ(SM5713_MUIC_IRQ_INT1_DPDM_OVP, MUIC_INT1, 1 << 5),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT1_VBUS_RID_DETACH, MUIC_INT1, 1 << 4),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT1_AUTOVBUSCHECK, MUIC_INT1, 1 << 3),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT1_RID_DETECT, MUIC_INT1, 1 << 2),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT1_CHGTYPE, MUIC_INT1, 1 << 1),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT1_DCDTIMEOUT, MUIC_INT1, 1 << 0),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT2_AFC_ERROR, MUIC_INT2, 1 << 5),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT2_AFC_STA_CHG, MUIC_INT2, 1 << 4),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT2_MULTI_BYTE, MUIC_INT2, 1 << 3),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT2_VBUS_UPDATE, MUIC_INT2, 1 << 2),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT2_AFC_ACCEPTED, MUIC_INT2, 1 << 1),
DECLARE_IRQ(SM5713_MUIC_IRQ_INT2_AFC_TA_ATTACHED, MUIC_INT2, 1 << 0),
/* Charger-irqs */
DECLARE_IRQ(SM5713_CHG_IRQ_INT1_VBUSLIMIT, CHG_INT1, 1 << 3),
DECLARE_IRQ(SM5713_CHG_IRQ_INT1_VBUSOVP, CHG_INT1, 1 << 2),
DECLARE_IRQ(SM5713_CHG_IRQ_INT1_VBUSUVLO, CHG_INT1, 1 << 1),
DECLARE_IRQ(SM5713_CHG_IRQ_INT1_VBUSPOK, CHG_INT1, 1 << 0),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_WDTMROFF, CHG_INT2, 1 << 7),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_DONE, CHG_INT2, 1 << 6),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_TOPOFF, CHG_INT2, 1 << 5),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_Q4FULLON, CHG_INT2, 1 << 4),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_CHGON, CHG_INT2, 1 << 3),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_NOBAT, CHG_INT2, 1 << 2),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_BATOVP, CHG_INT2, 1 << 1),
DECLARE_IRQ(SM5713_CHG_IRQ_INT2_AICL, CHG_INT2, 1 << 0),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_VSYSOVP, CHG_INT3, 1 << 7),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_nENQ4, CHG_INT3, 1 << 6),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_FASTTMROFF, CHG_INT3, 1 << 5),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_PRETMROFF, CHG_INT3, 1 << 4),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_DISLIMIT, CHG_INT3, 1 << 3),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_OTGFAIL, CHG_INT3, 1 << 2),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_THEMSHDN, CHG_INT3, 1 << 1),
DECLARE_IRQ(SM5713_CHG_IRQ_INT3_THEMREG, CHG_INT3, 1 << 0),
DECLARE_IRQ(SM5713_CHG_IRQ_INT4_CVMODE, CHG_INT4, 1 << 7),
DECLARE_IRQ(SM5713_CHG_IRQ_INT4_VBUS_UPDATE, CHG_INT4, 1 << 6),
DECLARE_IRQ(SM5713_CHG_IRQ_INT4_MRSTB, CHG_INT4, 1 << 4),
DECLARE_IRQ(SM5713_CHG_IRQ_INT4_nVBUSOK, CHG_INT4, 1 << 2),
DECLARE_IRQ(SM5713_CHG_IRQ_INT4_BOOSTPOK, CHG_INT4, 1 << 1),
DECLARE_IRQ(SM5713_CHG_IRQ_INT4_BOOSTPOK_NG, CHG_INT4, 1 << 0),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_ABSTMR2OFF, CHG_INT5, 1 << 7),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_ABSTMR1OFF, CHG_INT5, 1 << 6),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_FLED3OPEN, CHG_INT5, 1 << 5),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_FLED3SHORT, CHG_INT5, 1 << 4),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_FLED2OPEN, CHG_INT5, 1 << 3),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_FLED2SHORT, CHG_INT5, 1 << 2),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_FLED1OPEN, CHG_INT5, 1 << 1),
DECLARE_IRQ(SM5713_CHG_IRQ_INT5_FLED1SHORT, CHG_INT5, 1 << 0),
DECLARE_IRQ(SM5713_CHG_IRQ_INT6_VBUSSHORT, CHG_INT6, 1 << 0),
/* FuelGauge-irqs */
DECLARE_IRQ(SM5713_FG_IRQ_INT_LOW_SOC, FG_INT, 1 << 4),
DECLARE_IRQ(SM5713_FG_IRQ_INT_HIGH_TEMP, FG_INT, 1 << 3),
DECLARE_IRQ(SM5713_FG_IRQ_INT_LOW_TEMP, FG_INT, 1 << 2),
DECLARE_IRQ(SM5713_FG_IRQ_INT_HIGH_VOLTAGE, FG_INT, 1 << 1),
DECLARE_IRQ(SM5713_FG_IRQ_INT_LOW_VOLTAGE, FG_INT, 1 << 0),
};
static struct i2c_client *get_i2c(struct sm5713_dev *sm5713, int src)
{
switch (src) {
case MUIC_INT1:
case MUIC_INT2:
return sm5713->muic;
case CHG_INT1:
case CHG_INT2:
case CHG_INT3:
case CHG_INT4:
case CHG_INT5:
case CHG_INT6:
return sm5713->charger;
case FG_INT:
return sm5713->fuelgauge;
}
return ERR_PTR(-EINVAL);
}
static void sm5713_irq_lock(struct irq_data *data)
{
struct sm5713_dev *sm5713 = irq_get_chip_data(data->irq);
mutex_lock(&sm5713->irqlock);
}
static void sm5713_irq_sync_unlock(struct irq_data *data)
{
struct sm5713_dev *sm5713 = irq_get_chip_data(data->irq);
int i;
for (i = 0; i < SM5713_IRQ_GROUP_NR; i++) {
struct i2c_client *i2c = get_i2c(sm5713, i);
if (IS_ERR_OR_NULL(i2c))
continue;
sm5713->irq_masks_cache[i] = sm5713->irq_masks_cur[i];
if (i == FG_INT) {
sm5713_write_word(i2c, sm5713_mask_reg[i], sm5713->irq_masks_cur[i]);
} else {
sm5713_write_reg(i2c, sm5713_mask_reg[i], sm5713->irq_masks_cur[i]);
}
}
mutex_unlock(&sm5713->irqlock);
}
static const inline struct sm5713_irq_data *
irq_to_sm5713_irq(struct sm5713_dev *sm5713, int irq)
{
return &sm5713_irqs[irq - sm5713->irq_base];
}
static void sm5713_irq_mask(struct irq_data *data)
{
struct sm5713_dev *sm5713 = irq_get_chip_data(data->irq);
const struct sm5713_irq_data *irq_data = irq_to_sm5713_irq(sm5713, data->irq);
if (irq_data->group >= SM5713_IRQ_GROUP_NR)
return;
sm5713->irq_masks_cur[irq_data->group] |= irq_data->mask;
}
static void sm5713_irq_unmask(struct irq_data *data)
{
struct sm5713_dev *sm5713 = irq_get_chip_data(data->irq);
const struct sm5713_irq_data *irq_data = irq_to_sm5713_irq(sm5713, data->irq);
if (irq_data->group >= SM5713_IRQ_GROUP_NR)
return;
sm5713->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
}
static struct irq_chip sm5713_irq_chip = {
.name = MFD_DEV_NAME,
.irq_bus_lock = sm5713_irq_lock,
.irq_bus_sync_unlock = sm5713_irq_sync_unlock,
.irq_mask = sm5713_irq_mask,
.irq_unmask = sm5713_irq_unmask,
};
static irqreturn_t sm5713_irq_thread(int irq, void *data)
{
struct sm5713_dev *sm5713 = data;
u8 irq_reg[SM5713_IRQ_GROUP_NR] = {0};
u8 irq_src;
int i, ret;
pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__, gpio_get_value(sm5713->irq_gpio));
ret = sm5713_read_reg(sm5713->charger, SM5713_CHG_REG_INT_SOURCE, &irq_src);
pr_info("%s\n irq_src = %x", __func__, irq_src);
if (ret) {
pr_err("%s:%s fail to read interrupt source: %d\n", MFD_DEV_NAME, __func__, ret);
return IRQ_NONE;
}
pr_debug("%s: INT_SOURCE=0x%02x)\n", __func__, irq_src);
/* Irregular case: check IC status */
if (irq_src == 0) {
if (sm5713->check_muic_reset)
sm5713->check_muic_reset(sm5713->muic, sm5713->muic_data);
if (sm5713->check_chg_reset)
sm5713->check_chg_reset(sm5713->charger, sm5713->chg_data);
if (sm5713->check_fg_reset)
sm5713->check_fg_reset(sm5713->fuelgauge, sm5713->fg_data);
}
/* Charger INT 1 ~ 6 */
if (irq_src & SM5713_IRQSRC_CHG) {
ret = sm5713_bulk_read(sm5713->charger, SM5713_CHG_REG_INT1, SM5713_NUM_IRQ_CHG_REGS, &irq_reg[CHG_INT1]);
if (ret) {
pr_err("%s:%s fail to read CHG_INT source: %d\n", MFD_DEV_NAME, __func__, ret);
return IRQ_NONE;
}
for (i = CHG_INT1; i <= CHG_INT6; i++) {
pr_debug("%s:%s CHG_INT%d = 0x%x\n", MFD_DEV_NAME, __func__, (i - 1), irq_reg[i]);
}
}
/* MUIC INT 1 ~ 2 */
if (irq_src & SM5713_IRQSRC_MUIC) {
ret = sm5713_bulk_read(sm5713->muic, SM5713_MUIC_REG_INT1, SM5713_NUM_IRQ_MUIC_REGS, &irq_reg[MUIC_INT1]);
pr_info ("%s:%s MUIC_INT1 = 0x%x, MUIC_INT2 = 0x%x\n", MFD_DEV_NAME, __func__, irq_reg[MUIC_INT1], irq_reg[MUIC_INT2]);
if (ret) {
pr_err("%s:%s fail to read MUIC_INT source: %d\n", MFD_DEV_NAME, __func__, ret);
return IRQ_NONE;
}
for (i = MUIC_INT1; i <= MUIC_INT2; i++) {
pr_debug("%s:%s MUIC_INT%d = 0x%x\n", MFD_DEV_NAME, __func__, (i + 1), irq_reg[i]);
}
}
/* Fuel Gauge INT */
if (irq_src & SM5713_IRQSRC_FG) {
/* FG_INT Lock */
sm5713->irq_masks_cur[FG_INT] |= (1 << 7);
sm5713_write_word(sm5713->fuelgauge, sm5713_mask_reg[FG_INT], sm5713->irq_masks_cur[FG_INT]);
irq_reg[FG_INT] = (u8)(sm5713_read_word(sm5713->fuelgauge, SM5713_FG_REG_INTFG) & 0x00FF);
/* FG_INT Un-Lock */
sm5713->irq_masks_cur[FG_INT] &= ~(1 << 7);
sm5713_write_word(sm5713->fuelgauge, sm5713_mask_reg[FG_INT], sm5713->irq_masks_cur[FG_INT]);
pr_info("%s:%s FG_INT = 0x%x\n", MFD_DEV_NAME, __func__, irq_reg[FG_INT]);
}
/* Apply masking */
for (i = 0; i < SM5713_IRQ_GROUP_NR; i++) {
irq_reg[i] &= ~sm5713->irq_masks_cur[i];
}
/* Report */
for (i = 0; i < SM5713_IRQ_NR; i++) {
if (irq_reg[sm5713_irqs[i].group] & sm5713_irqs[i].mask) {
handle_nested_irq(sm5713->irq_base + i);
}
}
return IRQ_HANDLED;
}
int sm5713_irq_init(struct sm5713_dev *sm5713)
{
struct i2c_client *i2c;
int i;
int ret;
if (!sm5713->irq_gpio) {
dev_warn(sm5713->dev, "No interrupt specified.\n");
sm5713->irq_base = 0;
return 0;
}
if (!sm5713->irq_base) {
dev_err(sm5713->dev, "No interrupt base specified.\n");
return 0;
}
mutex_init(&sm5713->irqlock);
sm5713->irq = gpio_to_irq(sm5713->irq_gpio);
pr_err("%s:%s irq=%d, irq->gpio=%d\n", MFD_DEV_NAME, __func__,
sm5713->irq, sm5713->irq_gpio);
ret = gpio_request(sm5713->irq_gpio, "if_pmic_irq");
if (ret) {
dev_err(sm5713->dev, "%s: failed requesting gpio %d\n",
__func__, sm5713->irq_gpio);
return ret;
}
gpio_direction_input(sm5713->irq_gpio);
gpio_free(sm5713->irq_gpio);
/* Mask individual interrupt sources */
for (i = 0; i < SM5713_IRQ_GROUP_NR; i++) {
i2c = get_i2c(sm5713, i);
if (IS_ERR_OR_NULL(i2c))
continue;
if (i == FG_INT) {
sm5713->irq_masks_cur[i] = 0x000f;
sm5713->irq_masks_cache[i] = 0x000f;
sm5713_write_word(i2c, sm5713_mask_reg[i], sm5713->irq_masks_cur[i]);
} else {
sm5713->irq_masks_cur[i] = 0xff;
sm5713->irq_masks_cache[i] = 0xff;
sm5713_write_reg(i2c, sm5713_mask_reg[i], sm5713->irq_masks_cur[i]);
}
}
/* Register with genirq */
for (i = 0; i < SM5713_IRQ_NR; i++) {
int cur_irq;
cur_irq = i + sm5713->irq_base;
irq_set_chip_data(cur_irq, sm5713);
irq_set_chip_and_handler(cur_irq, &sm5713_irq_chip, handle_level_irq);
irq_set_nested_thread(cur_irq, 1);
#ifdef CONFIG_ARM
set_irq_flags(cur_irq, IRQF_VALID);
#else
irq_set_noprobe(cur_irq);
#endif
}
ret = request_threaded_irq(sm5713->irq, NULL, sm5713_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
"sm5713-irq", sm5713);
if (ret) {
dev_err(sm5713->dev, "Fail to request IRQ %d: %d\n", sm5713->irq, ret);
return ret;
}
return 0;
}
void sm5713_irq_exit(struct sm5713_dev *sm5713)
{
if (sm5713->irq)
free_irq(sm5713->irq, sm5713);
}