tps65090.c 7.2 KB
Newer Older
V
Venu Byravarasu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Core driver for TI TPS65090 PMIC family
 *
 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.

 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.

 * This program is distributed in the hope 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.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/mfd/core.h>
#include <linux/mfd/tps65090.h>
28 29
#include <linux/of.h>
#include <linux/of_device.h>
V
Venu Byravarasu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
#include <linux/err.h>

#define NUM_INT_REG 2
#define TOTAL_NUM_REG 0x18

/* interrupt status registers */
#define TPS65090_INT_STS	0x0
#define TPS65090_INT_STS2	0x1

/* interrupt mask registers */
#define TPS65090_INT_MSK	0x2
#define TPS65090_INT_MSK2	0x3

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#define TPS65090_INT1_MASK_VAC_STATUS_CHANGE		1
#define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE		2
#define TPS65090_INT1_MASK_BAT_STATUS_CHANGE		3
#define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE	4
#define TPS65090_INT1_MASK_CHARGING_COMPLETE		5
#define TPS65090_INT1_MASK_OVERLOAD_DCDC1		6
#define TPS65090_INT1_MASK_OVERLOAD_DCDC2		7
#define TPS65090_INT2_MASK_OVERLOAD_DCDC3		0
#define TPS65090_INT2_MASK_OVERLOAD_FET1		1
#define TPS65090_INT2_MASK_OVERLOAD_FET2		2
#define TPS65090_INT2_MASK_OVERLOAD_FET3		3
#define TPS65090_INT2_MASK_OVERLOAD_FET4		4
#define TPS65090_INT2_MASK_OVERLOAD_FET5		5
#define TPS65090_INT2_MASK_OVERLOAD_FET6		6
#define TPS65090_INT2_MASK_OVERLOAD_FET7		7
V
Venu Byravarasu 已提交
58 59 60

static struct mfd_cell tps65090s[] = {
	{
61
		.name = "tps65090-pmic",
V
Venu Byravarasu 已提交
62 63
	},
	{
64
		.name = "tps65090-charger",
V
Venu Byravarasu 已提交
65 66 67
	},
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static const struct regmap_irq tps65090_irqs[] = {
	/* INT1 IRQs*/
	[TPS65090_IRQ_VAC_STATUS_CHANGE] = {
			.mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
	},
	[TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
			.mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
	},
	[TPS65090_IRQ_BAT_STATUS_CHANGE] = {
			.mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
	},
	[TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
			.mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
	},
	[TPS65090_IRQ_CHARGING_COMPLETE] = {
			.mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
	},
	[TPS65090_IRQ_OVERLOAD_DCDC1] = {
			.mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
	},
	[TPS65090_IRQ_OVERLOAD_DCDC2] = {
			.mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
	},
	/* INT2 IRQs*/
	[TPS65090_IRQ_OVERLOAD_DCDC3] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
	},
	[TPS65090_IRQ_OVERLOAD_FET1] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
	},
	[TPS65090_IRQ_OVERLOAD_FET2] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
	},
	[TPS65090_IRQ_OVERLOAD_FET3] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
	},
	[TPS65090_IRQ_OVERLOAD_FET4] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
	},
	[TPS65090_IRQ_OVERLOAD_FET5] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
	},
	[TPS65090_IRQ_OVERLOAD_FET6] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
	},
	[TPS65090_IRQ_OVERLOAD_FET7] = {
			.reg_offset = 1,
			.mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
	},
};
V
Venu Byravarasu 已提交
125

126 127 128 129 130 131 132 133 134
static struct regmap_irq_chip tps65090_irq_chip = {
	.name = "tps65090",
	.irqs = tps65090_irqs,
	.num_irqs = ARRAY_SIZE(tps65090_irqs),
	.num_regs = NUM_INT_REG,
	.status_base = TPS65090_INT_STS,
	.mask_base = TPS65090_INT_MSK,
	.mask_invert = true,
};
V
Venu Byravarasu 已提交
135 136 137

static bool is_volatile_reg(struct device *dev, unsigned int reg)
{
138
	if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2))
V
Venu Byravarasu 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152
		return true;
	else
		return false;
}

static const struct regmap_config tps65090_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = TOTAL_NUM_REG,
	.num_reg_defaults_raw = TOTAL_NUM_REG,
	.cache_type = REGCACHE_RBTREE,
	.volatile_reg = is_volatile_reg,
};

153 154 155 156 157 158 159 160
#ifdef CONFIG_OF
static const struct of_device_id tps65090_of_match[] = {
	{ .compatible = "ti,tps65090",},
	{},
};
MODULE_DEVICE_TABLE(of, tps65090_of_match);
#endif

B
Bill Pemberton 已提交
161
static int tps65090_i2c_probe(struct i2c_client *client,
V
Venu Byravarasu 已提交
162 163 164
					const struct i2c_device_id *id)
{
	struct tps65090_platform_data *pdata = client->dev.platform_data;
165
	int irq_base = 0;
V
Venu Byravarasu 已提交
166 167 168
	struct tps65090 *tps65090;
	int ret;

169 170 171
	if (!pdata && !client->dev.of_node) {
		dev_err(&client->dev,
			"tps65090 requires platform data or of_node\n");
V
Venu Byravarasu 已提交
172 173 174
		return -EINVAL;
	}

175 176 177
	if (pdata)
		irq_base = pdata->irq_base;

178 179 180
	tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
	if (!tps65090) {
		dev_err(&client->dev, "mem alloc for tps65090 failed\n");
V
Venu Byravarasu 已提交
181
		return -ENOMEM;
182
	}
V
Venu Byravarasu 已提交
183 184 185 186

	tps65090->dev = &client->dev;
	i2c_set_clientdata(client, tps65090);

187
	tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
V
Venu Byravarasu 已提交
188
	if (IS_ERR(tps65090->rmap)) {
189 190
		ret = PTR_ERR(tps65090->rmap);
		dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
191 192 193 194 195
		return ret;
	}

	if (client->irq) {
		ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
196
			IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
197 198 199 200 201 202
			&tps65090_irq_chip, &tps65090->irq_data);
			if (ret) {
				dev_err(&client->dev,
					"IRQ init failed with err: %d\n", ret);
			return ret;
		}
203
	}
V
Venu Byravarasu 已提交
204 205

	ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
206 207
		ARRAY_SIZE(tps65090s), NULL,
		regmap_irq_chip_get_base(tps65090->irq_data), NULL);
V
Venu Byravarasu 已提交
208 209 210
	if (ret) {
		dev_err(&client->dev, "add mfd devices failed with err: %d\n",
			ret);
211
		goto err_irq_exit;
V
Venu Byravarasu 已提交
212 213 214 215 216 217
	}

	return 0;

err_irq_exit:
	if (client->irq)
218
		regmap_del_irq_chip(client->irq, tps65090->irq_data);
V
Venu Byravarasu 已提交
219 220 221
	return ret;
}

B
Bill Pemberton 已提交
222
static int tps65090_i2c_remove(struct i2c_client *client)
V
Venu Byravarasu 已提交
223 224 225 226 227
{
	struct tps65090 *tps65090 = i2c_get_clientdata(client);

	mfd_remove_devices(tps65090->dev);
	if (client->irq)
228
		regmap_del_irq_chip(client->irq, tps65090->irq_data);
V
Venu Byravarasu 已提交
229 230 231 232

	return 0;
}

233
#ifdef CONFIG_PM_SLEEP
234
static int tps65090_suspend(struct device *dev)
V
Venu Byravarasu 已提交
235
{
236
	struct i2c_client *client = to_i2c_client(dev);
V
Venu Byravarasu 已提交
237 238 239 240 241
	if (client->irq)
		disable_irq(client->irq);
	return 0;
}

242
static int tps65090_resume(struct device *dev)
V
Venu Byravarasu 已提交
243
{
244
	struct i2c_client *client = to_i2c_client(dev);
V
Venu Byravarasu 已提交
245 246 247 248 249 250
	if (client->irq)
		enable_irq(client->irq);
	return 0;
}
#endif

251 252 253 254
static const struct dev_pm_ops tps65090_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
};

V
Venu Byravarasu 已提交
255 256 257 258 259 260 261 262 263 264
static const struct i2c_device_id tps65090_id_table[] = {
	{ "tps65090", 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, tps65090_id_table);

static struct i2c_driver tps65090_driver = {
	.driver	= {
		.name	= "tps65090",
		.owner	= THIS_MODULE,
265
		.of_match_table = of_match_ptr(tps65090_of_match),
266
		.pm	= &tps65090_pm_ops,
V
Venu Byravarasu 已提交
267 268
	},
	.probe		= tps65090_i2c_probe,
B
Bill Pemberton 已提交
269
	.remove		= tps65090_i2c_remove,
V
Venu Byravarasu 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	.id_table	= tps65090_id_table,
};

static int __init tps65090_init(void)
{
	return i2c_add_driver(&tps65090_driver);
}
subsys_initcall(tps65090_init);

static void __exit tps65090_exit(void)
{
	i2c_del_driver(&tps65090_driver);
}
module_exit(tps65090_exit);

MODULE_DESCRIPTION("TPS65090 core driver");
MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
MODULE_LICENSE("GPL v2");