xref: /openbmc/linux/drivers/mfd/tps65218.c (revision 18bb399f)
144b4dc61SKeerthy /*
244b4dc61SKeerthy  * Driver for TPS65218 Integrated power management chipsets
344b4dc61SKeerthy  *
444b4dc61SKeerthy  * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
544b4dc61SKeerthy  *
644b4dc61SKeerthy  * This program is free software; you can redistribute it and/or
744b4dc61SKeerthy  * modify it under the terms of the GNU General Public License version 2 as
844b4dc61SKeerthy  * published by the Free Software Foundation.
944b4dc61SKeerthy  *
1044b4dc61SKeerthy  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
1144b4dc61SKeerthy  * kind, whether expressed or implied; without even the implied warranty
1244b4dc61SKeerthy  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1344b4dc61SKeerthy  * GNU General Public License version 2 for more details.
1444b4dc61SKeerthy  */
1544b4dc61SKeerthy 
1644b4dc61SKeerthy #include <linux/kernel.h>
1744b4dc61SKeerthy #include <linux/device.h>
1844b4dc61SKeerthy #include <linux/module.h>
1944b4dc61SKeerthy #include <linux/platform_device.h>
2044b4dc61SKeerthy #include <linux/init.h>
2144b4dc61SKeerthy #include <linux/i2c.h>
2244b4dc61SKeerthy #include <linux/slab.h>
2344b4dc61SKeerthy #include <linux/regmap.h>
2444b4dc61SKeerthy #include <linux/err.h>
2544b4dc61SKeerthy #include <linux/of.h>
2644b4dc61SKeerthy #include <linux/of_device.h>
2744b4dc61SKeerthy #include <linux/irq.h>
2844b4dc61SKeerthy #include <linux/interrupt.h>
2944b4dc61SKeerthy #include <linux/mutex.h>
3044b4dc61SKeerthy 
3144b4dc61SKeerthy #include <linux/mfd/core.h>
3244b4dc61SKeerthy #include <linux/mfd/tps65218.h>
3344b4dc61SKeerthy 
3444b4dc61SKeerthy #define TPS65218_PASSWORD_REGS_UNLOCK   0x7D
3544b4dc61SKeerthy 
3644b4dc61SKeerthy /**
3744b4dc61SKeerthy  * tps65218_reg_read: Read a single tps65218 register.
3844b4dc61SKeerthy  *
3944b4dc61SKeerthy  * @tps: Device to read from.
4044b4dc61SKeerthy  * @reg: Register to read.
4144b4dc61SKeerthy  * @val: Contians the value
4244b4dc61SKeerthy  */
4344b4dc61SKeerthy int tps65218_reg_read(struct tps65218 *tps, unsigned int reg,
4444b4dc61SKeerthy 			unsigned int *val)
4544b4dc61SKeerthy {
4644b4dc61SKeerthy 	return regmap_read(tps->regmap, reg, val);
4744b4dc61SKeerthy }
4844b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_reg_read);
4944b4dc61SKeerthy 
5044b4dc61SKeerthy /**
5144b4dc61SKeerthy  * tps65218_reg_write: Write a single tps65218 register.
5244b4dc61SKeerthy  *
5344b4dc61SKeerthy  * @tps65218: Device to write to.
5444b4dc61SKeerthy  * @reg: Register to write to.
5544b4dc61SKeerthy  * @val: Value to write.
5644b4dc61SKeerthy  * @level: Password protected level
5744b4dc61SKeerthy  */
5844b4dc61SKeerthy int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
5944b4dc61SKeerthy 			unsigned int val, unsigned int level)
6044b4dc61SKeerthy {
6144b4dc61SKeerthy 	int ret;
6244b4dc61SKeerthy 	unsigned int xor_reg_val;
6344b4dc61SKeerthy 
6444b4dc61SKeerthy 	switch (level) {
6544b4dc61SKeerthy 	case TPS65218_PROTECT_NONE:
6644b4dc61SKeerthy 		return regmap_write(tps->regmap, reg, val);
6744b4dc61SKeerthy 	case TPS65218_PROTECT_L1:
6844b4dc61SKeerthy 		xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
6944b4dc61SKeerthy 		ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
7044b4dc61SKeerthy 							xor_reg_val);
7144b4dc61SKeerthy 		if (ret < 0)
7244b4dc61SKeerthy 			return ret;
7344b4dc61SKeerthy 
7444b4dc61SKeerthy 		return regmap_write(tps->regmap, reg, val);
7544b4dc61SKeerthy 	default:
7644b4dc61SKeerthy 		return -EINVAL;
7744b4dc61SKeerthy 	}
7844b4dc61SKeerthy }
7944b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_reg_write);
8044b4dc61SKeerthy 
8144b4dc61SKeerthy /**
8244b4dc61SKeerthy  * tps65218_update_bits: Modify bits w.r.t mask, val and level.
8344b4dc61SKeerthy  *
8444b4dc61SKeerthy  * @tps65218: Device to write to.
8544b4dc61SKeerthy  * @reg: Register to read-write to.
8644b4dc61SKeerthy  * @mask: Mask.
8744b4dc61SKeerthy  * @val: Value to write.
8844b4dc61SKeerthy  * @level: Password protected level
8944b4dc61SKeerthy  */
9044b4dc61SKeerthy static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
9144b4dc61SKeerthy 		unsigned int mask, unsigned int val, unsigned int level)
9244b4dc61SKeerthy {
9344b4dc61SKeerthy 	int ret;
9444b4dc61SKeerthy 	unsigned int data;
9544b4dc61SKeerthy 
9644b4dc61SKeerthy 	ret = tps65218_reg_read(tps, reg, &data);
9744b4dc61SKeerthy 	if (ret) {
9844b4dc61SKeerthy 		dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
9944b4dc61SKeerthy 		return ret;
10044b4dc61SKeerthy 	}
10144b4dc61SKeerthy 
10244b4dc61SKeerthy 	data &= ~mask;
10344b4dc61SKeerthy 	data |= val & mask;
10444b4dc61SKeerthy 
10544b4dc61SKeerthy 	mutex_lock(&tps->tps_lock);
10644b4dc61SKeerthy 	ret = tps65218_reg_write(tps, reg, data, level);
10744b4dc61SKeerthy 	if (ret)
10844b4dc61SKeerthy 		dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
10944b4dc61SKeerthy 	mutex_unlock(&tps->tps_lock);
11044b4dc61SKeerthy 
11144b4dc61SKeerthy 	return ret;
11244b4dc61SKeerthy }
11344b4dc61SKeerthy 
11444b4dc61SKeerthy int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
11544b4dc61SKeerthy 		unsigned int mask, unsigned int val, unsigned int level)
11644b4dc61SKeerthy {
11744b4dc61SKeerthy 	return tps65218_update_bits(tps, reg, mask, val, level);
11844b4dc61SKeerthy }
11944b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_set_bits);
12044b4dc61SKeerthy 
12144b4dc61SKeerthy int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
12244b4dc61SKeerthy 		unsigned int mask, unsigned int level)
12344b4dc61SKeerthy {
12444b4dc61SKeerthy 	return tps65218_update_bits(tps, reg, mask, 0, level);
12544b4dc61SKeerthy }
12644b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_clear_bits);
12744b4dc61SKeerthy 
128773328daSFelipe Balbi static const struct regmap_range tps65218_yes_ranges[] = {
129773328daSFelipe Balbi 	regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
130773328daSFelipe Balbi 	regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
131773328daSFelipe Balbi };
132773328daSFelipe Balbi 
133773328daSFelipe Balbi static const struct regmap_access_table tps65218_volatile_table = {
134773328daSFelipe Balbi 	.yes_ranges = tps65218_yes_ranges,
135773328daSFelipe Balbi 	.n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
136773328daSFelipe Balbi };
137773328daSFelipe Balbi 
13818bb399fSKrzysztof Kozlowski static const struct regmap_config tps65218_regmap_config = {
13944b4dc61SKeerthy 	.reg_bits = 8,
14044b4dc61SKeerthy 	.val_bits = 8,
14144b4dc61SKeerthy 	.cache_type = REGCACHE_RBTREE,
142773328daSFelipe Balbi 	.volatile_table = &tps65218_volatile_table,
14344b4dc61SKeerthy };
14444b4dc61SKeerthy 
14544b4dc61SKeerthy static const struct regmap_irq tps65218_irqs[] = {
14644b4dc61SKeerthy 	/* INT1 IRQs */
14744b4dc61SKeerthy 	[TPS65218_PRGC_IRQ] = {
14844b4dc61SKeerthy 		.mask = TPS65218_INT1_PRGC,
14944b4dc61SKeerthy 	},
15044b4dc61SKeerthy 	[TPS65218_CC_AQC_IRQ] = {
15144b4dc61SKeerthy 		.mask = TPS65218_INT1_CC_AQC,
15244b4dc61SKeerthy 	},
15344b4dc61SKeerthy 	[TPS65218_HOT_IRQ] = {
15444b4dc61SKeerthy 		.mask = TPS65218_INT1_HOT,
15544b4dc61SKeerthy 	},
15644b4dc61SKeerthy 	[TPS65218_PB_IRQ] = {
15744b4dc61SKeerthy 		.mask = TPS65218_INT1_PB,
15844b4dc61SKeerthy 	},
15944b4dc61SKeerthy 	[TPS65218_AC_IRQ] = {
16044b4dc61SKeerthy 		.mask = TPS65218_INT1_AC,
16144b4dc61SKeerthy 	},
16244b4dc61SKeerthy 	[TPS65218_VPRG_IRQ] = {
16344b4dc61SKeerthy 		.mask = TPS65218_INT1_VPRG,
16444b4dc61SKeerthy 	},
16544b4dc61SKeerthy 	[TPS65218_INVALID1_IRQ] = {
16644b4dc61SKeerthy 	},
16744b4dc61SKeerthy 	[TPS65218_INVALID2_IRQ] = {
16844b4dc61SKeerthy 	},
16944b4dc61SKeerthy 	/* INT2 IRQs*/
17044b4dc61SKeerthy 	[TPS65218_LS1_I_IRQ] = {
17144b4dc61SKeerthy 		.mask = TPS65218_INT2_LS1_I,
17244b4dc61SKeerthy 		.reg_offset = 1,
17344b4dc61SKeerthy 	},
17444b4dc61SKeerthy 	[TPS65218_LS2_I_IRQ] = {
17544b4dc61SKeerthy 		.mask = TPS65218_INT2_LS2_I,
17644b4dc61SKeerthy 		.reg_offset = 1,
17744b4dc61SKeerthy 	},
17844b4dc61SKeerthy 	[TPS65218_LS3_I_IRQ] = {
17944b4dc61SKeerthy 		.mask = TPS65218_INT2_LS3_I,
18044b4dc61SKeerthy 		.reg_offset = 1,
18144b4dc61SKeerthy 	},
18244b4dc61SKeerthy 	[TPS65218_LS1_F_IRQ] = {
18344b4dc61SKeerthy 		.mask = TPS65218_INT2_LS1_F,
18444b4dc61SKeerthy 		.reg_offset = 1,
18544b4dc61SKeerthy 	},
18644b4dc61SKeerthy 	[TPS65218_LS2_F_IRQ] = {
18744b4dc61SKeerthy 		.mask = TPS65218_INT2_LS2_F,
18844b4dc61SKeerthy 		.reg_offset = 1,
18944b4dc61SKeerthy 	},
19044b4dc61SKeerthy 	[TPS65218_LS3_F_IRQ] = {
19144b4dc61SKeerthy 		.mask = TPS65218_INT2_LS3_F,
19244b4dc61SKeerthy 		.reg_offset = 1,
19344b4dc61SKeerthy 	},
19444b4dc61SKeerthy 	[TPS65218_INVALID3_IRQ] = {
19544b4dc61SKeerthy 	},
19644b4dc61SKeerthy 	[TPS65218_INVALID4_IRQ] = {
19744b4dc61SKeerthy 	},
19844b4dc61SKeerthy };
19944b4dc61SKeerthy 
20044b4dc61SKeerthy static struct regmap_irq_chip tps65218_irq_chip = {
20144b4dc61SKeerthy 	.name = "tps65218",
20244b4dc61SKeerthy 	.irqs = tps65218_irqs,
20344b4dc61SKeerthy 	.num_irqs = ARRAY_SIZE(tps65218_irqs),
20444b4dc61SKeerthy 
20544b4dc61SKeerthy 	.num_regs = 2,
20644b4dc61SKeerthy 	.mask_base = TPS65218_REG_INT_MASK1,
207f29ae369SFelipe Balbi 	.status_base = TPS65218_REG_INT1,
20844b4dc61SKeerthy };
20944b4dc61SKeerthy 
21044b4dc61SKeerthy static const struct of_device_id of_tps65218_match_table[] = {
21144b4dc61SKeerthy 	{ .compatible = "ti,tps65218", },
2128320513eSStephen Boyd 	{}
21344b4dc61SKeerthy };
21444b4dc61SKeerthy 
21544b4dc61SKeerthy static int tps65218_probe(struct i2c_client *client,
21644b4dc61SKeerthy 				const struct i2c_device_id *ids)
21744b4dc61SKeerthy {
21844b4dc61SKeerthy 	struct tps65218 *tps;
21944b4dc61SKeerthy 	const struct of_device_id *match;
22044b4dc61SKeerthy 	int ret;
22144b4dc61SKeerthy 
22244b4dc61SKeerthy 	match = of_match_device(of_tps65218_match_table, &client->dev);
22344b4dc61SKeerthy 	if (!match) {
22444b4dc61SKeerthy 		dev_err(&client->dev,
22544b4dc61SKeerthy 			"Failed to find matching dt id\n");
22644b4dc61SKeerthy 		return -EINVAL;
22744b4dc61SKeerthy 	}
22844b4dc61SKeerthy 
22944b4dc61SKeerthy 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
23044b4dc61SKeerthy 	if (!tps)
23144b4dc61SKeerthy 		return -ENOMEM;
23244b4dc61SKeerthy 
23344b4dc61SKeerthy 	i2c_set_clientdata(client, tps);
23444b4dc61SKeerthy 	tps->dev = &client->dev;
23544b4dc61SKeerthy 	tps->irq = client->irq;
23644b4dc61SKeerthy 	tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
23744b4dc61SKeerthy 	if (IS_ERR(tps->regmap)) {
23844b4dc61SKeerthy 		ret = PTR_ERR(tps->regmap);
23944b4dc61SKeerthy 		dev_err(tps->dev, "Failed to allocate register map: %d\n",
24044b4dc61SKeerthy 			ret);
24144b4dc61SKeerthy 		return ret;
24244b4dc61SKeerthy 	}
24344b4dc61SKeerthy 
24444b4dc61SKeerthy 	mutex_init(&tps->tps_lock);
24544b4dc61SKeerthy 
24644b4dc61SKeerthy 	ret = regmap_add_irq_chip(tps->regmap, tps->irq,
24744b4dc61SKeerthy 			IRQF_ONESHOT, 0, &tps65218_irq_chip,
24844b4dc61SKeerthy 			&tps->irq_data);
24944b4dc61SKeerthy 	if (ret < 0)
25044b4dc61SKeerthy 		return ret;
25144b4dc61SKeerthy 
25244b4dc61SKeerthy 	ret = of_platform_populate(client->dev.of_node, NULL, NULL,
25344b4dc61SKeerthy 				   &client->dev);
25444b4dc61SKeerthy 	if (ret < 0)
25544b4dc61SKeerthy 		goto err_irq;
25644b4dc61SKeerthy 
25744b4dc61SKeerthy 	return 0;
25844b4dc61SKeerthy 
25944b4dc61SKeerthy err_irq:
26044b4dc61SKeerthy 	regmap_del_irq_chip(tps->irq, tps->irq_data);
26144b4dc61SKeerthy 
26244b4dc61SKeerthy 	return ret;
26344b4dc61SKeerthy }
26444b4dc61SKeerthy 
26544b4dc61SKeerthy static int tps65218_remove(struct i2c_client *client)
26644b4dc61SKeerthy {
26744b4dc61SKeerthy 	struct tps65218 *tps = i2c_get_clientdata(client);
26844b4dc61SKeerthy 
26944b4dc61SKeerthy 	regmap_del_irq_chip(tps->irq, tps->irq_data);
27044b4dc61SKeerthy 
27144b4dc61SKeerthy 	return 0;
27244b4dc61SKeerthy }
27344b4dc61SKeerthy 
27444b4dc61SKeerthy static const struct i2c_device_id tps65218_id_table[] = {
27544b4dc61SKeerthy 	{ "tps65218", TPS65218 },
27644b4dc61SKeerthy 	{ },
27744b4dc61SKeerthy };
27844b4dc61SKeerthy MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
27944b4dc61SKeerthy 
28044b4dc61SKeerthy static struct i2c_driver tps65218_driver = {
28144b4dc61SKeerthy 	.driver		= {
28244b4dc61SKeerthy 		.name	= "tps65218",
28344b4dc61SKeerthy 		.owner	= THIS_MODULE,
28444b4dc61SKeerthy 		.of_match_table = of_tps65218_match_table,
28544b4dc61SKeerthy 	},
28644b4dc61SKeerthy 	.probe		= tps65218_probe,
28744b4dc61SKeerthy 	.remove		= tps65218_remove,
28844b4dc61SKeerthy 	.id_table       = tps65218_id_table,
28944b4dc61SKeerthy };
29044b4dc61SKeerthy 
29144b4dc61SKeerthy module_i2c_driver(tps65218_driver);
29244b4dc61SKeerthy 
29344b4dc61SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
29444b4dc61SKeerthy MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
29544b4dc61SKeerthy MODULE_LICENSE("GPL v2");
296