max14577.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * max14577.c - mfd core driver for the Maxim 14577
 *
 * Copyright (C) 2013 Samsung Electrnoics
 * Chanwoo Choi <cw00.choi@samsung.com>
 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * This driver is based on max8997.c
 */

21
#include <linux/err.h>
22 23
#include <linux/module.h>
#include <linux/interrupt.h>
24
#include <linux/of_device.h>
25 26 27 28 29
#include <linux/mfd/core.h>
#include <linux/mfd/max14577.h>
#include <linux/mfd/max14577-private.h>

static struct mfd_cell max14577_devs[] = {
30 31 32 33
	{
		.name = "max14577-muic",
		.of_compatible = "maxim,max14577-muic",
	},
34 35 36 37
	{
		.name = "max14577-regulator",
		.of_compatible = "maxim,max14577-regulator",
	},
38 39 40
	{ .name = "max14577-charger", },
};

41 42 43 44 45 46 47 48
static struct of_device_id max14577_dt_match[] = {
	{
		.compatible = "maxim,max14577",
		.data = (void *)MAXIM_DEVICE_TYPE_MAX14577,
	},
	{},
};

49
static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg)
50 51 52 53 54 55 56 57 58 59
{
	switch (reg) {
	case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
		return true;
	default:
		break;
	}
	return false;
}

60
static const struct regmap_config max14577_muic_regmap_config = {
61 62
	.reg_bits	= 8,
	.val_bits	= 8,
63
	.volatile_reg	= max14577_muic_volatile_reg,
64 65 66 67 68
	.max_register	= MAX14577_REG_END,
};

static const struct regmap_irq max14577_irqs[] = {
	/* INT1 interrupts */
69 70 71
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
72
	/* INT2 interrupts */
73 74 75 76 77
	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
78
	/* INT3 interrupts */
79 80 81 82
	{ .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
83 84 85 86 87 88 89 90 91 92 93 94
};

static const struct regmap_irq_chip max14577_irq_chip = {
	.name			= "max14577",
	.status_base		= MAX14577_REG_INT1,
	.mask_base		= MAX14577_REG_INTMASK1,
	.mask_invert		= 1,
	.num_regs		= 3,
	.irqs			= max14577_irqs,
	.num_irqs		= ARRAY_SIZE(max14577_irqs),
};

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void max14577_print_dev_type(struct max14577 *max14577)
{
	u8 reg_data, vendor_id, device_id;
	int ret;

	ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
			&reg_data);
	if (ret) {
		dev_err(max14577->dev,
			"Failed to read DEVICEID register: %d\n", ret);
		return;
	}

	vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
				DEVID_VENDORID_SHIFT);
	device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
				DEVID_DEVICEID_SHIFT);

	dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n",
			max14577->dev_type, device_id, vendor_id);
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static int max14577_i2c_probe(struct i2c_client *i2c,
			      const struct i2c_device_id *id)
{
	struct max14577 *max14577;
	struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
	struct device_node *np = i2c->dev.of_node;
	int ret = 0;

	if (np) {
		pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
		if (!pdata)
			return -ENOMEM;
		i2c->dev.platform_data = pdata;
	}

	if (!pdata) {
133
		dev_err(&i2c->dev, "No platform data found.\n");
134 135 136 137 138 139 140 141 142 143 144 145
		return -EINVAL;
	}

	max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
	if (!max14577)
		return -ENOMEM;

	i2c_set_clientdata(i2c, max14577);
	max14577->dev = &i2c->dev;
	max14577->i2c = i2c;
	max14577->irq = i2c->irq;

146 147
	max14577->regmap = devm_regmap_init_i2c(i2c,
			&max14577_muic_regmap_config);
148 149 150 151 152 153 154
	if (IS_ERR(max14577->regmap)) {
		ret = PTR_ERR(max14577->regmap);
		dev_err(max14577->dev, "Failed to allocate register map: %d\n",
				ret);
		return ret;
	}

155 156 157 158 159 160 161 162
	if (np) {
		const struct of_device_id *of_id;

		of_id = of_match_device(max14577_dt_match, &i2c->dev);
		if (of_id)
			max14577->dev_type = (unsigned int)of_id->data;
	} else {
		max14577->dev_type = id->driver_data;
163
	}
164 165

	max14577_print_dev_type(max14577);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
				  IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0,
				  &max14577_irq_chip,
				  &max14577->irq_data);
	if (ret != 0) {
		dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
				max14577->irq, ret);
		return ret;
	}

	ret = mfd_add_devices(max14577->dev, -1, max14577_devs,
			ARRAY_SIZE(max14577_devs), NULL, 0,
			regmap_irq_get_domain(max14577->irq_data));
	if (ret < 0)
		goto err_mfd;

	device_init_wakeup(max14577->dev, 1);

	return 0;

err_mfd:
	regmap_del_irq_chip(max14577->irq, max14577->irq_data);

	return ret;
}

static int max14577_i2c_remove(struct i2c_client *i2c)
{
	struct max14577 *max14577 = i2c_get_clientdata(i2c);

	mfd_remove_devices(max14577->dev);
	regmap_del_irq_chip(max14577->irq, max14577->irq_data);

	return 0;
}

static const struct i2c_device_id max14577_i2c_id[] = {
204
	{ "max14577", MAXIM_DEVICE_TYPE_MAX14577, },
205 206 207 208
	{ }
};
MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);

209
#ifdef CONFIG_PM_SLEEP
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static int max14577_suspend(struct device *dev)
{
	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
	struct max14577 *max14577 = i2c_get_clientdata(i2c);

	if (device_may_wakeup(dev)) {
		enable_irq_wake(max14577->irq);
		/*
		 * MUIC IRQ must be disabled during suspend if this is
		 * a wake up source because it will be handled before
		 * resuming I2C.
		 *
		 * When device is woken up from suspend (e.g. by ADC change),
		 * an interrupt occurs before resuming I2C bus controller.
		 * Interrupt handler tries to read registers but this read
		 * will fail because I2C is still suspended.
		 */
		disable_irq(max14577->irq);
	}

	return 0;
}

static int max14577_resume(struct device *dev)
{
	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
	struct max14577 *max14577 = i2c_get_clientdata(i2c);

	if (device_may_wakeup(dev)) {
		disable_irq_wake(max14577->irq);
		enable_irq(max14577->irq);
	}

	return 0;
}
245
#endif /* CONFIG_PM_SLEEP */
246 247 248 249 250 251 252 253

static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);

static struct i2c_driver max14577_i2c_driver = {
	.driver = {
		.name = "max14577",
		.owner = THIS_MODULE,
		.pm = &max14577_pm,
254
		.of_match_table = max14577_dt_match,
255 256 257 258 259 260 261 262
	},
	.probe = max14577_i2c_probe,
	.remove = max14577_i2c_remove,
	.id_table = max14577_i2c_id,
};

static int __init max14577_i2c_init(void)
{
263 264 265
	BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM);
	BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM);

266 267 268 269 270 271 272 273 274 275 276 277 278
	return i2c_add_driver(&max14577_i2c_driver);
}
subsys_initcall(max14577_i2c_init);

static void __exit max14577_i2c_exit(void)
{
	i2c_del_driver(&max14577_i2c_driver);
}
module_exit(max14577_i2c_exit);

MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
MODULE_DESCRIPTION("MAXIM 14577 multi-function core driver");
MODULE_LICENSE("GPL");