xref: /openbmc/linux/drivers/mfd/tps6105x.c (revision 9816d859)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2798a8eeeSLinus Walleij /*
3798a8eeeSLinus Walleij  * Core driver for TPS61050/61052 boost converters, used for while LED
4798a8eeeSLinus Walleij  * driving, audio power amplification, white LED flash, and generic
5798a8eeeSLinus Walleij  * boost conversion. Additionally it provides a 1-bit GPIO pin (out or in)
6798a8eeeSLinus Walleij  * and a flash synchronization pin to synchronize flash events when used as
7798a8eeeSLinus Walleij  * flashgun.
8798a8eeeSLinus Walleij  *
9798a8eeeSLinus Walleij  * Copyright (C) 2011 ST-Ericsson SA
10798a8eeeSLinus Walleij  * Written on behalf of Linaro for ST-Ericsson
11798a8eeeSLinus Walleij  *
12798a8eeeSLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
13798a8eeeSLinus Walleij  */
14798a8eeeSLinus Walleij 
15798a8eeeSLinus Walleij #include <linux/module.h>
16798a8eeeSLinus Walleij #include <linux/init.h>
17798a8eeeSLinus Walleij #include <linux/i2c.h>
187e507119SGrigoryev Denis #include <linux/regmap.h>
19798a8eeeSLinus Walleij #include <linux/gpio.h>
20798a8eeeSLinus Walleij #include <linux/spinlock.h>
21798a8eeeSLinus Walleij #include <linux/slab.h>
22798a8eeeSLinus Walleij #include <linux/err.h>
23798a8eeeSLinus Walleij #include <linux/mfd/core.h>
24798a8eeeSLinus Walleij #include <linux/mfd/tps6105x.h>
25798a8eeeSLinus Walleij 
267e507119SGrigoryev Denis static struct regmap_config tps6105x_regmap_config = {
277e507119SGrigoryev Denis 	.reg_bits = 8,
287e507119SGrigoryev Denis 	.val_bits = 8,
297e507119SGrigoryev Denis 	.max_register = TPS6105X_REG_3,
307e507119SGrigoryev Denis };
31798a8eeeSLinus Walleij 
tps6105x_startup(struct tps6105x * tps6105x)32f791be49SBill Pemberton static int tps6105x_startup(struct tps6105x *tps6105x)
33798a8eeeSLinus Walleij {
34798a8eeeSLinus Walleij 	int ret;
357e507119SGrigoryev Denis 	unsigned int regval;
36798a8eeeSLinus Walleij 
377e507119SGrigoryev Denis 	ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
38798a8eeeSLinus Walleij 	if (ret)
39798a8eeeSLinus Walleij 		return ret;
40798a8eeeSLinus Walleij 	switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
41798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_SHUTDOWN:
42798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
43798a8eeeSLinus Walleij 			 "TPS6105x found in SHUTDOWN mode\n");
44798a8eeeSLinus Walleij 		break;
45798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_TORCH:
46798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
47798a8eeeSLinus Walleij 			 "TPS6105x found in TORCH mode\n");
48798a8eeeSLinus Walleij 		break;
49798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_TORCH_FLASH:
50798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
51798a8eeeSLinus Walleij 			 "TPS6105x found in FLASH mode\n");
52798a8eeeSLinus Walleij 		break;
53798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_VOLTAGE:
54798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
55798a8eeeSLinus Walleij 			 "TPS6105x found in VOLTAGE mode\n");
56798a8eeeSLinus Walleij 		break;
57798a8eeeSLinus Walleij 	default:
58798a8eeeSLinus Walleij 		break;
59798a8eeeSLinus Walleij 	}
60798a8eeeSLinus Walleij 
61798a8eeeSLinus Walleij 	return ret;
62798a8eeeSLinus Walleij }
63798a8eeeSLinus Walleij 
64798a8eeeSLinus Walleij /*
65ea50e9d3SGrigoryev Denis  * MFD cells - we always have a GPIO cell and we have one cell
66ea50e9d3SGrigoryev Denis  * which is selected operation mode.
67798a8eeeSLinus Walleij  */
68ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_gpio_cell = {
69798a8eeeSLinus Walleij 	.name = "tps6105x-gpio",
70798a8eeeSLinus Walleij };
71798a8eeeSLinus Walleij 
72ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_leds_cell = {
73ea50e9d3SGrigoryev Denis 	.name = "tps6105x-leds",
74ea50e9d3SGrigoryev Denis };
75ea50e9d3SGrigoryev Denis 
76ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_flash_cell = {
77ea50e9d3SGrigoryev Denis 	.name = "tps6105x-flash",
78ea50e9d3SGrigoryev Denis };
79ea50e9d3SGrigoryev Denis 
80ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_regulator_cell = {
81ea50e9d3SGrigoryev Denis 	.name = "tps6105x-regulator",
82ea50e9d3SGrigoryev Denis };
83ea50e9d3SGrigoryev Denis 
tps6105x_add_device(struct tps6105x * tps6105x,struct mfd_cell * cell)84ea50e9d3SGrigoryev Denis static int tps6105x_add_device(struct tps6105x *tps6105x,
85ea50e9d3SGrigoryev Denis 			       struct mfd_cell *cell)
86ea50e9d3SGrigoryev Denis {
87ea50e9d3SGrigoryev Denis 	cell->platform_data = tps6105x;
88ea50e9d3SGrigoryev Denis 	cell->pdata_size = sizeof(*tps6105x);
89ea50e9d3SGrigoryev Denis 
90ea50e9d3SGrigoryev Denis 	return mfd_add_devices(&tps6105x->client->dev,
91ea50e9d3SGrigoryev Denis 			       PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL);
92ea50e9d3SGrigoryev Denis }
93ea50e9d3SGrigoryev Denis 
tps6105x_parse_dt(struct device * dev)9462f7f3ecSSven Van Asbroeck static struct tps6105x_platform_data *tps6105x_parse_dt(struct device *dev)
9562f7f3ecSSven Van Asbroeck {
9662f7f3ecSSven Van Asbroeck 	struct device_node *np = dev->of_node;
9762f7f3ecSSven Van Asbroeck 	struct tps6105x_platform_data *pdata;
9862f7f3ecSSven Van Asbroeck 	struct device_node *child;
9962f7f3ecSSven Van Asbroeck 
10062f7f3ecSSven Van Asbroeck 	if (!np)
10162f7f3ecSSven Van Asbroeck 		return ERR_PTR(-EINVAL);
10262f7f3ecSSven Van Asbroeck 	if (of_get_available_child_count(np) > 1) {
10362f7f3ecSSven Van Asbroeck 		dev_err(dev, "cannot support multiple operational modes");
10462f7f3ecSSven Van Asbroeck 		return ERR_PTR(-EINVAL);
10562f7f3ecSSven Van Asbroeck 	}
10662f7f3ecSSven Van Asbroeck 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
10762f7f3ecSSven Van Asbroeck 	if (!pdata)
10862f7f3ecSSven Van Asbroeck 		return ERR_PTR(-ENOMEM);
10962f7f3ecSSven Van Asbroeck 	pdata->mode = TPS6105X_MODE_SHUTDOWN;
11062f7f3ecSSven Van Asbroeck 	for_each_available_child_of_node(np, child) {
11162f7f3ecSSven Van Asbroeck 		if (child->name && !of_node_cmp(child->name, "regulator"))
11262f7f3ecSSven Van Asbroeck 			pdata->mode = TPS6105X_MODE_VOLTAGE;
11362f7f3ecSSven Van Asbroeck 		else if (child->name && !of_node_cmp(child->name, "led"))
11462f7f3ecSSven Van Asbroeck 			pdata->mode = TPS6105X_MODE_TORCH;
11562f7f3ecSSven Van Asbroeck 	}
11662f7f3ecSSven Van Asbroeck 
11762f7f3ecSSven Van Asbroeck 	return pdata;
11862f7f3ecSSven Van Asbroeck }
11962f7f3ecSSven Van Asbroeck 
tps6105x_probe(struct i2c_client * client)120bb5b1c64SUwe Kleine-König static int tps6105x_probe(struct i2c_client *client)
121798a8eeeSLinus Walleij {
122798a8eeeSLinus Walleij 	struct tps6105x			*tps6105x;
123798a8eeeSLinus Walleij 	struct tps6105x_platform_data	*pdata;
124798a8eeeSLinus Walleij 	int ret;
125ea50e9d3SGrigoryev Denis 
126ea50e9d3SGrigoryev Denis 	pdata = dev_get_platdata(&client->dev);
12762f7f3ecSSven Van Asbroeck 	if (!pdata)
12862f7f3ecSSven Van Asbroeck 		pdata = tps6105x_parse_dt(&client->dev);
12962f7f3ecSSven Van Asbroeck 	if (IS_ERR(pdata)) {
13062f7f3ecSSven Van Asbroeck 		dev_err(&client->dev, "No platform data or DT found");
13162f7f3ecSSven Van Asbroeck 		return PTR_ERR(pdata);
132ea50e9d3SGrigoryev Denis 	}
133798a8eeeSLinus Walleij 
134ad83533aSHimangi Saraogi 	tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
135798a8eeeSLinus Walleij 	if (!tps6105x)
136798a8eeeSLinus Walleij 		return -ENOMEM;
137798a8eeeSLinus Walleij 
1387e507119SGrigoryev Denis 	tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
1397e507119SGrigoryev Denis 	if (IS_ERR(tps6105x->regmap))
1407e507119SGrigoryev Denis 		return PTR_ERR(tps6105x->regmap);
1417e507119SGrigoryev Denis 
142798a8eeeSLinus Walleij 	i2c_set_clientdata(client, tps6105x);
143798a8eeeSLinus Walleij 	tps6105x->client = client;
144798a8eeeSLinus Walleij 	tps6105x->pdata = pdata;
145798a8eeeSLinus Walleij 
146798a8eeeSLinus Walleij 	ret = tps6105x_startup(tps6105x);
147798a8eeeSLinus Walleij 	if (ret) {
148798a8eeeSLinus Walleij 		dev_err(&client->dev, "chip initialization failed\n");
149ad83533aSHimangi Saraogi 		return ret;
150798a8eeeSLinus Walleij 	}
151798a8eeeSLinus Walleij 
152ea50e9d3SGrigoryev Denis 	ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
153ea50e9d3SGrigoryev Denis 	if (ret)
154ea50e9d3SGrigoryev Denis 		return ret;
155ea50e9d3SGrigoryev Denis 
156798a8eeeSLinus Walleij 	switch (pdata->mode) {
157798a8eeeSLinus Walleij 	case TPS6105X_MODE_SHUTDOWN:
158798a8eeeSLinus Walleij 		dev_info(&client->dev,
159798a8eeeSLinus Walleij 			 "present, not used for anything, only GPIO\n");
160798a8eeeSLinus Walleij 		break;
161798a8eeeSLinus Walleij 	case TPS6105X_MODE_TORCH:
162ea50e9d3SGrigoryev Denis 		ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
163798a8eeeSLinus Walleij 		break;
164798a8eeeSLinus Walleij 	case TPS6105X_MODE_TORCH_FLASH:
165ea50e9d3SGrigoryev Denis 		ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
166798a8eeeSLinus Walleij 		break;
167798a8eeeSLinus Walleij 	case TPS6105X_MODE_VOLTAGE:
168ea50e9d3SGrigoryev Denis 		ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
169798a8eeeSLinus Walleij 		break;
170798a8eeeSLinus Walleij 	default:
171ea50e9d3SGrigoryev Denis 		dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
172798a8eeeSLinus Walleij 		break;
173798a8eeeSLinus Walleij 	}
174798a8eeeSLinus Walleij 
175ea50e9d3SGrigoryev Denis 	if (ret)
176ea50e9d3SGrigoryev Denis 		mfd_remove_devices(&client->dev);
177798a8eeeSLinus Walleij 
178ea50e9d3SGrigoryev Denis 	return ret;
179798a8eeeSLinus Walleij }
180798a8eeeSLinus Walleij 
tps6105x_remove(struct i2c_client * client)181ed5c2f5fSUwe Kleine-König static void tps6105x_remove(struct i2c_client *client)
182798a8eeeSLinus Walleij {
183798a8eeeSLinus Walleij 	struct tps6105x *tps6105x = i2c_get_clientdata(client);
184798a8eeeSLinus Walleij 
185798a8eeeSLinus Walleij 	mfd_remove_devices(&client->dev);
186798a8eeeSLinus Walleij 
187798a8eeeSLinus Walleij 	/* Put chip in shutdown mode */
1887e507119SGrigoryev Denis 	regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
189798a8eeeSLinus Walleij 		TPS6105X_REG0_MODE_MASK,
190798a8eeeSLinus Walleij 		TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
191798a8eeeSLinus Walleij }
192798a8eeeSLinus Walleij 
193798a8eeeSLinus Walleij static const struct i2c_device_id tps6105x_id[] = {
194798a8eeeSLinus Walleij 	{ "tps61050", 0 },
195798a8eeeSLinus Walleij 	{ "tps61052", 0 },
196798a8eeeSLinus Walleij 	{ }
197798a8eeeSLinus Walleij };
198798a8eeeSLinus Walleij MODULE_DEVICE_TABLE(i2c, tps6105x_id);
199798a8eeeSLinus Walleij 
200125b249eSJavier Martinez Canillas static const struct of_device_id tps6105x_of_match[] = {
201125b249eSJavier Martinez Canillas 	{ .compatible = "ti,tps61050" },
202125b249eSJavier Martinez Canillas 	{ .compatible = "ti,tps61052" },
203125b249eSJavier Martinez Canillas 	{ },
204125b249eSJavier Martinez Canillas };
205125b249eSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, tps6105x_of_match);
206125b249eSJavier Martinez Canillas 
207798a8eeeSLinus Walleij static struct i2c_driver tps6105x_driver = {
208798a8eeeSLinus Walleij 	.driver = {
209798a8eeeSLinus Walleij 		.name	= "tps6105x",
210125b249eSJavier Martinez Canillas 		.of_match_table = tps6105x_of_match,
211798a8eeeSLinus Walleij 	},
212*9816d859SUwe Kleine-König 	.probe		= tps6105x_probe,
21384449216SBill Pemberton 	.remove		= tps6105x_remove,
214798a8eeeSLinus Walleij 	.id_table	= tps6105x_id,
215798a8eeeSLinus Walleij };
216798a8eeeSLinus Walleij 
tps6105x_init(void)217798a8eeeSLinus Walleij static int __init tps6105x_init(void)
218798a8eeeSLinus Walleij {
219798a8eeeSLinus Walleij 	return i2c_add_driver(&tps6105x_driver);
220798a8eeeSLinus Walleij }
221798a8eeeSLinus Walleij subsys_initcall(tps6105x_init);
222798a8eeeSLinus Walleij 
tps6105x_exit(void)223798a8eeeSLinus Walleij static void __exit tps6105x_exit(void)
224798a8eeeSLinus Walleij {
225798a8eeeSLinus Walleij 	i2c_del_driver(&tps6105x_driver);
226798a8eeeSLinus Walleij }
227798a8eeeSLinus Walleij module_exit(tps6105x_exit);
228798a8eeeSLinus Walleij 
229798a8eeeSLinus Walleij MODULE_AUTHOR("Linus Walleij");
230798a8eeeSLinus Walleij MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
231798a8eeeSLinus Walleij MODULE_LICENSE("GPL v2");
232