xref: /openbmc/linux/drivers/mfd/tps6105x.c (revision 62f7f3eca4c30064ab37b42d97cef4292d75fdd0)
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 
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 
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 
94*62f7f3ecSSven Van Asbroeck static struct tps6105x_platform_data *tps6105x_parse_dt(struct device *dev)
95*62f7f3ecSSven Van Asbroeck {
96*62f7f3ecSSven Van Asbroeck 	struct device_node *np = dev->of_node;
97*62f7f3ecSSven Van Asbroeck 	struct tps6105x_platform_data *pdata;
98*62f7f3ecSSven Van Asbroeck 	struct device_node *child;
99*62f7f3ecSSven Van Asbroeck 
100*62f7f3ecSSven Van Asbroeck 	if (!np)
101*62f7f3ecSSven Van Asbroeck 		return ERR_PTR(-EINVAL);
102*62f7f3ecSSven Van Asbroeck 	if (of_get_available_child_count(np) > 1) {
103*62f7f3ecSSven Van Asbroeck 		dev_err(dev, "cannot support multiple operational modes");
104*62f7f3ecSSven Van Asbroeck 		return ERR_PTR(-EINVAL);
105*62f7f3ecSSven Van Asbroeck 	}
106*62f7f3ecSSven Van Asbroeck 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
107*62f7f3ecSSven Van Asbroeck 	if (!pdata)
108*62f7f3ecSSven Van Asbroeck 		return ERR_PTR(-ENOMEM);
109*62f7f3ecSSven Van Asbroeck 	pdata->mode = TPS6105X_MODE_SHUTDOWN;
110*62f7f3ecSSven Van Asbroeck 	for_each_available_child_of_node(np, child) {
111*62f7f3ecSSven Van Asbroeck 		if (child->name && !of_node_cmp(child->name, "regulator"))
112*62f7f3ecSSven Van Asbroeck 			pdata->mode = TPS6105X_MODE_VOLTAGE;
113*62f7f3ecSSven Van Asbroeck 		else if (child->name && !of_node_cmp(child->name, "led"))
114*62f7f3ecSSven Van Asbroeck 			pdata->mode = TPS6105X_MODE_TORCH;
115*62f7f3ecSSven Van Asbroeck 	}
116*62f7f3ecSSven Van Asbroeck 
117*62f7f3ecSSven Van Asbroeck 	return pdata;
118*62f7f3ecSSven Van Asbroeck }
119*62f7f3ecSSven Van Asbroeck 
120f791be49SBill Pemberton static int tps6105x_probe(struct i2c_client *client,
121798a8eeeSLinus Walleij 			const struct i2c_device_id *id)
122798a8eeeSLinus Walleij {
123798a8eeeSLinus Walleij 	struct tps6105x			*tps6105x;
124798a8eeeSLinus Walleij 	struct tps6105x_platform_data	*pdata;
125798a8eeeSLinus Walleij 	int ret;
126ea50e9d3SGrigoryev Denis 
127ea50e9d3SGrigoryev Denis 	pdata = dev_get_platdata(&client->dev);
128*62f7f3ecSSven Van Asbroeck 	if (!pdata)
129*62f7f3ecSSven Van Asbroeck 		pdata = tps6105x_parse_dt(&client->dev);
130*62f7f3ecSSven Van Asbroeck 	if (IS_ERR(pdata)) {
131*62f7f3ecSSven Van Asbroeck 		dev_err(&client->dev, "No platform data or DT found");
132*62f7f3ecSSven Van Asbroeck 		return PTR_ERR(pdata);
133ea50e9d3SGrigoryev Denis 	}
134798a8eeeSLinus Walleij 
135ad83533aSHimangi Saraogi 	tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
136798a8eeeSLinus Walleij 	if (!tps6105x)
137798a8eeeSLinus Walleij 		return -ENOMEM;
138798a8eeeSLinus Walleij 
1397e507119SGrigoryev Denis 	tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
1407e507119SGrigoryev Denis 	if (IS_ERR(tps6105x->regmap))
1417e507119SGrigoryev Denis 		return PTR_ERR(tps6105x->regmap);
1427e507119SGrigoryev Denis 
143798a8eeeSLinus Walleij 	i2c_set_clientdata(client, tps6105x);
144798a8eeeSLinus Walleij 	tps6105x->client = client;
145798a8eeeSLinus Walleij 	tps6105x->pdata = pdata;
146798a8eeeSLinus Walleij 
147798a8eeeSLinus Walleij 	ret = tps6105x_startup(tps6105x);
148798a8eeeSLinus Walleij 	if (ret) {
149798a8eeeSLinus Walleij 		dev_err(&client->dev, "chip initialization failed\n");
150ad83533aSHimangi Saraogi 		return ret;
151798a8eeeSLinus Walleij 	}
152798a8eeeSLinus Walleij 
153ea50e9d3SGrigoryev Denis 	ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
154ea50e9d3SGrigoryev Denis 	if (ret)
155ea50e9d3SGrigoryev Denis 		return ret;
156ea50e9d3SGrigoryev Denis 
157798a8eeeSLinus Walleij 	switch (pdata->mode) {
158798a8eeeSLinus Walleij 	case TPS6105X_MODE_SHUTDOWN:
159798a8eeeSLinus Walleij 		dev_info(&client->dev,
160798a8eeeSLinus Walleij 			 "present, not used for anything, only GPIO\n");
161798a8eeeSLinus Walleij 		break;
162798a8eeeSLinus Walleij 	case TPS6105X_MODE_TORCH:
163ea50e9d3SGrigoryev Denis 		ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
164798a8eeeSLinus Walleij 		break;
165798a8eeeSLinus Walleij 	case TPS6105X_MODE_TORCH_FLASH:
166ea50e9d3SGrigoryev Denis 		ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
167798a8eeeSLinus Walleij 		break;
168798a8eeeSLinus Walleij 	case TPS6105X_MODE_VOLTAGE:
169ea50e9d3SGrigoryev Denis 		ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
170798a8eeeSLinus Walleij 		break;
171798a8eeeSLinus Walleij 	default:
172ea50e9d3SGrigoryev Denis 		dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
173798a8eeeSLinus Walleij 		break;
174798a8eeeSLinus Walleij 	}
175798a8eeeSLinus Walleij 
176ea50e9d3SGrigoryev Denis 	if (ret)
177ea50e9d3SGrigoryev Denis 		mfd_remove_devices(&client->dev);
178798a8eeeSLinus Walleij 
179ea50e9d3SGrigoryev Denis 	return ret;
180798a8eeeSLinus Walleij }
181798a8eeeSLinus Walleij 
1824740f73fSBill Pemberton static int tps6105x_remove(struct i2c_client *client)
183798a8eeeSLinus Walleij {
184798a8eeeSLinus Walleij 	struct tps6105x *tps6105x = i2c_get_clientdata(client);
185798a8eeeSLinus Walleij 
186798a8eeeSLinus Walleij 	mfd_remove_devices(&client->dev);
187798a8eeeSLinus Walleij 
188798a8eeeSLinus Walleij 	/* Put chip in shutdown mode */
1897e507119SGrigoryev Denis 	regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
190798a8eeeSLinus Walleij 		TPS6105X_REG0_MODE_MASK,
191798a8eeeSLinus Walleij 		TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
192798a8eeeSLinus Walleij 
193798a8eeeSLinus Walleij 	return 0;
194798a8eeeSLinus Walleij }
195798a8eeeSLinus Walleij 
196798a8eeeSLinus Walleij static const struct i2c_device_id tps6105x_id[] = {
197798a8eeeSLinus Walleij 	{ "tps61050", 0 },
198798a8eeeSLinus Walleij 	{ "tps61052", 0 },
199798a8eeeSLinus Walleij 	{ }
200798a8eeeSLinus Walleij };
201798a8eeeSLinus Walleij MODULE_DEVICE_TABLE(i2c, tps6105x_id);
202798a8eeeSLinus Walleij 
203125b249eSJavier Martinez Canillas static const struct of_device_id tps6105x_of_match[] = {
204125b249eSJavier Martinez Canillas 	{ .compatible = "ti,tps61050" },
205125b249eSJavier Martinez Canillas 	{ .compatible = "ti,tps61052" },
206125b249eSJavier Martinez Canillas 	{ },
207125b249eSJavier Martinez Canillas };
208125b249eSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, tps6105x_of_match);
209125b249eSJavier Martinez Canillas 
210798a8eeeSLinus Walleij static struct i2c_driver tps6105x_driver = {
211798a8eeeSLinus Walleij 	.driver = {
212798a8eeeSLinus Walleij 		.name	= "tps6105x",
213125b249eSJavier Martinez Canillas 		.of_match_table = tps6105x_of_match,
214798a8eeeSLinus Walleij 	},
215798a8eeeSLinus Walleij 	.probe		= tps6105x_probe,
21684449216SBill Pemberton 	.remove		= tps6105x_remove,
217798a8eeeSLinus Walleij 	.id_table	= tps6105x_id,
218798a8eeeSLinus Walleij };
219798a8eeeSLinus Walleij 
220798a8eeeSLinus Walleij static int __init tps6105x_init(void)
221798a8eeeSLinus Walleij {
222798a8eeeSLinus Walleij 	return i2c_add_driver(&tps6105x_driver);
223798a8eeeSLinus Walleij }
224798a8eeeSLinus Walleij subsys_initcall(tps6105x_init);
225798a8eeeSLinus Walleij 
226798a8eeeSLinus Walleij static void __exit tps6105x_exit(void)
227798a8eeeSLinus Walleij {
228798a8eeeSLinus Walleij 	i2c_del_driver(&tps6105x_driver);
229798a8eeeSLinus Walleij }
230798a8eeeSLinus Walleij module_exit(tps6105x_exit);
231798a8eeeSLinus Walleij 
232798a8eeeSLinus Walleij MODULE_AUTHOR("Linus Walleij");
233798a8eeeSLinus Walleij MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
234798a8eeeSLinus Walleij MODULE_LICENSE("GPL v2");
235