static unsigned int max77843_rgb_get(struct led_classdev *led_cdev)
{
	const struct device *parent = led_cdev->dev->parent;
	struct max77843_rgb *max77843_rgb = dev_get_drvdata(parent);
	struct device *dev;
	int n;
	int ret;
	u8 value;

	pr_info("leds-max77843-rgb: %s\n", __func__);

	ret = max77843_rgb_number(led_cdev, &max77843_rgb);
	if (IS_ERR_VALUE(ret)) {
		dev_err(led_cdev->dev,
			"max77843_rgb_number() returns %d.\n", ret);
		return 0;
	}
	n = ret;

	dev = led_cdev->dev;

	/* Get status */
	ret = max77843_read_reg(max77843_rgb->i2c,
				MAX77843_RGBLED_REG_LEDEN, &value);
	if (IS_ERR_VALUE(ret)) {
		dev_err(dev, "can't read LEDEN : %d\n", ret);
		return 0;
	}
	if (!(value & (1 << n)))
		return LED_OFF;

	/* Get current */
	ret = max77843_read_reg(max77843_rgb->i2c,
				MAX77843_RGBLED_REG_LED0BRT + n, &value);
	if (IS_ERR_VALUE(ret)) {
		dev_err(dev, "can't read LED0BRT : %d\n", ret);
		return 0;
	}

	return value;
}
Beispiel #2
0
static int max77843_freeze(struct device *dev)
{
	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
	struct max77843_dev *max77843 = i2c_get_clientdata(i2c);
	int i;

	for (i = 0; i < ARRAY_SIZE(max77843_dumpaddr_pmic); i++)
		max77843_read_reg(i2c, max77843_dumpaddr_pmic[i],
				&max77843->reg_pmic_dump[i]);

	for (i = 0; i < ARRAY_SIZE(max77843_dumpaddr_muic); i++)
		max77843_read_reg(i2c, max77843_dumpaddr_muic[i],
				&max77843->reg_muic_dump[i]);

	for (i = 0; i < ARRAY_SIZE(max77843_dumpaddr_led); i++)
		max77843_read_reg(i2c, max77843_dumpaddr_led[i],
				&max77843->reg_led_dump[i]);

	disable_irq(max77843->irq);

	return 0;
}
int max77843_irq_init(struct max77843_dev *max77843)
{
	int i;
	int cur_irq;
	int ret;
	u8 i2c_data;

	pr_info("func: %s, irq_gpio: %d, irq_base: %d\n", __func__,
			max77843->irq_gpio, max77843->irq_base);
	if (!max77843->irq_gpio) {
		dev_warn(max77843->dev, "No interrupt specified.\n");
		max77843->irq_base = 0;
		return 0;
	}

	if (!max77843->irq_base) {
		dev_err(max77843->dev, "No interrupt base specified.\n");
		return 0;
	}

	mutex_init(&max77843->irqlock);

	max77843->irq = gpio_to_irq(max77843->irq_gpio);
	ret = gpio_request(max77843->irq_gpio, "if_pmic_irq");
	if (ret) {
		dev_err(max77843->dev, "%s: failed requesting gpio %d\n",
			__func__, max77843->irq_gpio);
		return ret;
	}
	gpio_direction_input(max77843->irq_gpio);
	gpio_free(max77843->irq_gpio);

	/* Mask individual interrupt sources */
	for (i = 0; i < MAX77843_IRQ_GROUP_NR; i++) {
		struct i2c_client *i2c;
		/* MUIC IRQ  0:MASK 1:NOT MASK */
		/* Other IRQ 1:MASK 0:NOT MASK */
		if (i >= MAX77843_MUIC_INT1 && i <= MAX77843_MUIC_INT3) {
			max77843->irq_masks_cur[i] = 0x00;
			max77843->irq_masks_cache[i] = 0x00;
		} else {
			max77843->irq_masks_cur[i] = 0xff;
			max77843->irq_masks_cache[i] = 0xff;
		}
		i2c = get_i2c(max77843, i);

		if (IS_ERR_OR_NULL(i2c))
			continue;
		if (max77843_mask_reg[i] == MAX77843_REG_INVALID)
			continue;
		if (i >= MAX77843_MUIC_INT1 && i <= MAX77843_MUIC_INT3)
			max77843_write_reg(i2c, max77843_mask_reg[i], 0x00);
		else
			max77843_write_reg(i2c, max77843_mask_reg[i], 0xff);
	}

	/* Register with genirq */
	for (i = 0; i < MAX77843_IRQ_NR; i++) {
		cur_irq = i + max77843->irq_base;
		irq_set_chip_data(cur_irq, max77843);
		irq_set_chip_and_handler(cur_irq, &max77843_irq_chip,
					 handle_edge_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
	}

	/* Unmask max77843 interrupt */
	ret = max77843_read_reg(max77843->i2c, MAX77843_PMIC_REG_INTSRC_MASK,
			  &i2c_data);
	if (ret) {
		dev_err(max77843->dev, "%s: fail to read muic reg\n", __func__);
		return ret;
	}

	i2c_data &= ~(MAX77843_IRQSRC_CHG);	/* Unmask charger interrupt */
	i2c_data &= ~(MAX77843_IRQSRC_FG);	/* Unmask fuelgauge interrupt */
	i2c_data &= ~(MAX77843_IRQSRC_MUIC);	/* Unmask muic interrupt */
	max77843_write_reg(max77843->i2c, MAX77843_PMIC_REG_INTSRC_MASK,
			   i2c_data);

	ret = request_threaded_irq(max77843->irq, NULL, max77843_irq_thread,
				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
				   "max77843-irq", max77843);

	if (ret) {
		dev_err(max77843->dev, "Failed to request IRQ %d: %d\n",
			max77843->irq, ret);
		return ret;
	}

	return 0;
}
static irqreturn_t max77843_irq_thread(int irq, void *data)
{
	struct max77843_dev *max77843 = data;
	u8 irq_reg[MAX77843_IRQ_GROUP_NR] = {0};
	u8 irq_src;
	int ret;
	int i;
	pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__,
		gpio_get_value(max77843->irq_gpio));

clear_retry:
	ret = max77843_read_reg(max77843->i2c,
		MAX77843_PMIC_REG_INTSRC, &irq_src);
	if (ret < 0) {
		dev_err(max77843->dev, "Failed to read interrupt source: %d\n",
				ret);
		return IRQ_NONE;
	}
	pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);

	if (irq_src & MAX77843_IRQSRC_TOP) {
		/* TOPSYS_INT */
		ret = max77843_read_reg(max77843->i2c,
				MAX77843_PMIC_REG_TOPSYS_INT,
				&irq_reg[MAX77843_TOPSYS_INT]);
		pr_info("%s: topsys interrupt(0x%02x)\n",
			__func__, irq_reg[MAX77843_TOPSYS_INT]);
	}

	if (irq_src & MAX77843_IRQSRC_CHG) {
		/* CHG_INT */
		ret = max77843_read_reg(max77843->charger, MAX77843_CHG_REG_INT,
				&irq_reg[MAX77843_CHG_INT]);
		pr_info("%s: charger interrupt(0x%02x)\n",
			__func__, irq_reg[MAX77843_CHG_INT]);
		/* mask chgin to prevent chgin infinite interrupt
		 * chgin is unmasked chgin isr
		 */
		if (irq_reg[MAX77843_CHG_INT] & max77843_irqs[MAX77843_CHG_IRQ_CHGIN_I].mask) {
			max77843_update_reg(max77843->i2c,
				MAX77843_CHG_REG_INT_MASK, MAX77843_CHGIN_IM, MAX77843_CHGIN_IM);
		}
	}

	if (irq_src & MAX77843_IRQSRC_FG) {
		pr_info("%s: fuelgauge interrupt, IRQ_BASE(%d), NESTED_IRQ(%d)\n", __func__,
			max77843->irq_base, max77843->irq_base + MAX77843_FG_IRQ_ALERT);
		handle_nested_irq(max77843->irq_base + MAX77843_FG_IRQ_ALERT);
		return IRQ_HANDLED;
	}

	if (irq_src & MAX77843_IRQSRC_MUIC) {
		/* MUIC INT1 ~ INT3 */
		max77843_bulk_read(max77843->muic,
		MAX77843_MUIC_REG_INT1,
		MAX77843_NUM_IRQ_MUIC_REGS,
				&irq_reg[MAX77843_MUIC_INT1]);
		pr_info("%s: muic interrupt(0x%02x, 0x%02x, 0x%02x)\n",
			__func__, irq_reg[MAX77843_MUIC_INT1],
			irq_reg[MAX77843_MUIC_INT2], irq_reg[MAX77843_MUIC_INT3]);
	}

	pr_debug("%s: irq gpio post-state(0x%02x)\n", __func__,
		gpio_get_value(max77843->irq_gpio));

	if (gpio_get_value(max77843->irq_gpio) == 0) {
		pr_warn("%s: irq_gpio is not High!\n", __func__);
		goto clear_retry;
	}
#if 0
	/* Apply masking */
	for (i = 0; i < MAX77843_IRQ_GROUP_NR; i++) {
		if (i >= MAX77843_MUIC_INT1 && i <= MAX77843_MUIC_INT3)
			irq_reg[i] &= max77843->irq_masks_cur[i];
		else
			irq_reg[i] &= ~max77843->irq_masks_cur[i];
	}
#endif
	/* Report */
	for (i = 0; i < MAX77843_IRQ_NR; i++) {
		if (irq_reg[max77843_irqs[i].group] & max77843_irqs[i].mask)
			handle_nested_irq(max77843->irq_base + i);
	}

	return IRQ_HANDLED;
}
static irqreturn_t max77843_irq_thread(int irq, void *data)
{
	struct max77843_dev *max77843 = data;
	u8 irq_reg[MAX77843_IRQ_GROUP_NR] = {0};
	u8 tmp_irq_reg[MAX77843_IRQ_GROUP_NR] = {};
#if defined(CONFIG_MUIC_MAX77843_SUPPORT_AFC_CHARGER)
	u8 tmp_irq_mask_reg[MAX77843_IRQ_GROUP_NR] = {};
#endif /* CONFIG_MUIC_MAX77843_SUPPORT_AFC_CHARGER */
	static int check_num;
	u8 irq_src;
	int i, ret;

	pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__,
				gpio_get_value(max77843->irq_gpio));

	ret = max77843_read_reg(max77843->i2c,
					MAX77843_PMIC_REG_INTSRC, &irq_src);
	if (ret) {
		pr_err("%s:%s Failed to read interrupt source: %d\n",
			MFD_DEV_NAME, __func__, ret);
		return IRQ_NONE;
	}

#ifdef BATTERY_LOG_MESSAGE
	pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);
#endif

	if (irq_src & MAX77843_IRQSRC_CHG) {
		/* CHG_INT */
		ret = max77843_read_reg(max77843->charger, MAX77843_CHG_REG_CHG_INT,
				&irq_reg[CHG_INT]);
		pr_info("%s: charger interrupt(0x%02x)\n",
				__func__, irq_reg[CHG_INT]);
		/* mask chgin to prevent chgin infinite interrupt
		 * chgin is unmasked chgin isr
		 */
		if (irq_reg[CHG_INT] &
				max77843_irqs[MAX77843_CHG_IRQ_CHGIN_I].mask) {
			u8 reg_data;
			max77843_read_reg(max77843->charger,
				MAX77843_CHG_REG_CHG_INT_MASK, &reg_data);
			reg_data |= (1 << 6);
			max77843_write_reg(max77843->charger,
				MAX77843_CHG_REG_CHG_INT_MASK, reg_data);
		}
	}


	if (irq_src & MAX77843_IRQSRC_FG) {
		pr_info("[%s] fuelgauge interrupt\n", __func__);
		pr_info("[%s]IRQ_BASE(%d), NESTED_IRQ(%d)\n",
			__func__, max77843->irq_base, max77843->irq_base + MAX77843_FG_IRQ_ALERT);
		handle_nested_irq(max77843->irq_base + MAX77843_FG_IRQ_ALERT);
		return IRQ_HANDLED;
	}

	if (irq_src & MAX77843_IRQSRC_TOP) {
		/* SYS_INT */
		ret = max77843_read_reg(max77843->i2c, MAX77843_PMIC_REG_SYSTEM_INT,
				&irq_reg[SYS_INT]);
		pr_info("%s: topsys interrupt(0x%02x)\n", __func__, irq_reg[SYS_INT]);
	}

	if (irq_src & MAX77843_IRQSRC_MUIC) {
		/* MUIC INT1 ~ INT3 */
		ret = max77843_bulk_read(max77843->muic, MAX77843_MUIC_REG_INT1,
				MAX77843_NUM_IRQ_MUIC_REGS, &tmp_irq_reg[MUIC_INT1]);
		if (ret) {
			pr_err("%s:%s Failed to read interrupt source: %d\n",
				MFD_DEV_NAME, __func__, ret);
			return IRQ_NONE;
		}

#if defined(CONFIG_MUIC_MAX77843_SUPPORT_AFC_CHARGER)
		max77843_bulk_read(max77843->muic, MAX77843_MUIC_REG_INTMASK1,
				MAX77843_NUM_IRQ_MUIC_REGS, &tmp_irq_mask_reg[MUIC_INT1]);

		for (i = MUIC_INT1; i < MAX77843_IRQ_GROUP_NR; i++)
			tmp_irq_reg[i] &= tmp_irq_mask_reg[i];
#endif /* CONFIG_MUIC_MAX77843_SUPPORT_AFC_CHARGER */

		/* Or temp irq register to irq register for if it retries */
		for (i = MUIC_INT1; i <= MUIC_MAX_INT; i++)
			irq_reg[i] |= tmp_irq_reg[i];

		pr_info("%s: muic interrupt(0x%02x, 0x%02x, 0x%02x)\n", __func__,
			irq_reg[MUIC_INT1], irq_reg[MUIC_INT2], irq_reg[MUIC_INT3]);

		/* for debug */
		if ((irq_reg[MUIC_INT1] == 0) && (irq_reg[MUIC_INT2] == 0)
				&& (irq_reg[MUIC_INT3] == 0)) {
			pr_info("%s: irq gpio post-state(0x%02x)\n", __func__,
				gpio_get_value(max77843->irq_gpio));
			if (check_num >= 15) {
				max77843_muic_read_register(max77843->muic);
				panic("max77843 muic interrupt gpio Err!\n");
			}
			check_num++;
		} else
			check_num = 0;
	}

	/* Apply masking */
	for (i = 0; i < MAX77843_IRQ_GROUP_NR; i++) {
		if (i >= MUIC_INT1 && i <= MUIC_MAX_INT)
			irq_reg[i] &= max77843->irq_masks_cur[i];
		else
			irq_reg[i] &= ~max77843->irq_masks_cur[i];
	}

	/* Report */
	for (i = 0; i < MAX77843_IRQ_NR; i++) {
		if (irq_reg[max77843_irqs[i].group] & max77843_irqs[i].mask)
			handle_nested_irq(max77843->irq_base + i);
	}

	return IRQ_HANDLED;
}
Beispiel #6
0
static int max77843_i2c_probe(struct i2c_client *i2c,
			      const struct i2c_device_id *id)
{
	struct max77843_dev *max77843;
	struct max77843_platform_data *pdata;
	u8 reg_data;
	int ret = 0;
	dev_info(&i2c->dev, "%s\n", __func__);

	max77843 = kzalloc(sizeof(struct max77843_dev), GFP_KERNEL);
	if (max77843 == NULL)
		return -ENOMEM;

	if (i2c->dev.of_node) {
		pdata = devm_kzalloc(&i2c->dev,
				sizeof(struct max77843_platform_data),
				GFP_KERNEL);
		if (!pdata) {
			dev_err(&i2c->dev, "Failed to allocate memory \n");
			ret = -ENOMEM;
			goto err;
		}

		ret = of_max77843_dt(&i2c->dev, pdata);
		if (ret < 0){
			dev_err(&i2c->dev, "Failed to get device of_node \n");
			goto err;
		}
		/*Filling the platform data*/
		pdata->muic_data = &max77843_muic;
#ifdef CONFIG_REGULATOR_MAX77843
		pdata->num_regulators = MAX77843_REG_MAX;
		pdata->regulators = max77843_regulators;
#endif
#ifdef CONFIG_LEDS_MAX77843
		pdata->led_data = &max77843_led_pdata;
#endif
		/*pdata update to other modules*/
		i2c->dev.platform_data = pdata;
	} else
		pdata = i2c->dev.platform_data;

	i2c_set_clientdata(i2c, max77843);
	max77843->dev = &i2c->dev;

	max77843->i2c = i2c;
	max77843->irq = i2c->irq;
	if (pdata) {
		max77843->pdata = pdata;
		max77843->irq_base = irq_alloc_descs(-1, 0, MAX77843_IRQ_NR, -1);
		if (max77843->irq_base < 0) {
			pr_err("%s: irq_alloc_descs Fail ret(%d)\n",
					__func__, max77843->irq_base);
			ret = -EINVAL;
		} else {
			pdata->irq_base = max77843->irq_base;
		}
		max77843->irq_gpio = pdata->irq_gpio;
		max77843->wakeup = pdata->wakeup;
		gpio_tlmm_config(GPIO_CFG(max77843->irq_gpio,  0, GPIO_CFG_INPUT,
                GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
	} else {
		ret = -EINVAL;
		goto err;
	}

	mutex_init(&max77843->iolock);

	max77843->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
	i2c_set_clientdata(max77843->muic, max77843);

	max77843->charger = i2c_new_dummy(i2c->adapter, I2C_ADDR_CHG);
	i2c_set_clientdata(max77843->charger, max77843);

	max77843->fuelgauge = i2c_new_dummy(i2c->adapter, I2C_ADDR_FG);
	i2c_set_clientdata(max77843->fuelgauge, max77843);

	max77843->led = i2c_new_dummy(i2c->adapter, I2C_ADDR_LED);
	i2c_set_clientdata(max77843->led, max77843);

	if (max77843_read_reg(max77843->i2c, MAX77843_PMIC_REG_PMICID, &reg_data) < 0) {
		dev_err(max77843->dev,
			"device not found on this channel (this is not an error)\n");
		ret = -ENODEV;
	} else {
		/* print rev */
		max77843->pmic_rev = (reg_data & 0x7);
		max77843->pmic_ver = ((reg_data & 0xF8) >> 0x3);
		pr_info("%s: device found: rev.0x%x, ver.0x%x\n", __func__,
				max77843->pmic_rev, max77843->pmic_ver);
	}

	ret = max77843_irq_init(max77843);
	if (ret < 0)
		goto err_irq_init;

	ret = mfd_add_devices(max77843->dev, -1, max77843_devs,
			ARRAY_SIZE(max77843_devs), NULL, 0, NULL);
	if (ret < 0)
		goto err_mfd;

	device_init_wakeup(max77843->dev, pdata->wakeup);
#if defined(CONFIG_ADC_ONESHOT)
	/* Set oneshot mode */
	max77843_update_reg(max77843->muic, MAX77843_MUIC_REG_CTRL4,
			MAX77843_ADC_ONESHOT<<MAX77843_CTRL4_ADCMODE_SHIFT,
			MAX77843_CTRL4_ADCMODE_MASK);
#else
	/* Set continuous mode */
	max77843_update_reg(max77843->muic, MAX77843_MUIC_REG_CTRL4,
			MAX77843_ADC_ALWAYS<<MAX77843_CTRL4_ADCMODE_SHIFT,
			MAX77843_CTRL4_ADCMODE_MASK);
#endif
	return ret;

err_mfd:
	mfd_remove_devices(max77843->dev);
	max77843_irq_exit(max77843);
err_irq_init:
	i2c_unregister_device(max77843->muic);
	i2c_unregister_device(max77843->led);
err:
	kfree(max77843);
	return ret;
}