xref: /openbmc/linux/drivers/mfd/tps6105x.c (revision 7e50711993552800644a4667daa0f569a7665eca)
1798a8eeeSLinus Walleij /*
2798a8eeeSLinus Walleij  * Core driver for TPS61050/61052 boost converters, used for while LED
3798a8eeeSLinus Walleij  * driving, audio power amplification, white LED flash, and generic
4798a8eeeSLinus Walleij  * boost conversion. Additionally it provides a 1-bit GPIO pin (out or in)
5798a8eeeSLinus Walleij  * and a flash synchronization pin to synchronize flash events when used as
6798a8eeeSLinus Walleij  * flashgun.
7798a8eeeSLinus Walleij  *
8798a8eeeSLinus Walleij  * Copyright (C) 2011 ST-Ericsson SA
9798a8eeeSLinus Walleij  * Written on behalf of Linaro for ST-Ericsson
10798a8eeeSLinus Walleij  *
11798a8eeeSLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
12798a8eeeSLinus Walleij  *
13798a8eeeSLinus Walleij  * License terms: GNU General Public License (GPL) version 2
14798a8eeeSLinus Walleij  */
15798a8eeeSLinus Walleij 
16798a8eeeSLinus Walleij #include <linux/module.h>
17798a8eeeSLinus Walleij #include <linux/init.h>
18798a8eeeSLinus Walleij #include <linux/i2c.h>
19*7e507119SGrigoryev Denis #include <linux/regmap.h>
20798a8eeeSLinus Walleij #include <linux/gpio.h>
21798a8eeeSLinus Walleij #include <linux/spinlock.h>
22798a8eeeSLinus Walleij #include <linux/slab.h>
23798a8eeeSLinus Walleij #include <linux/err.h>
24798a8eeeSLinus Walleij #include <linux/regulator/driver.h>
25798a8eeeSLinus Walleij #include <linux/mfd/core.h>
26798a8eeeSLinus Walleij #include <linux/mfd/tps6105x.h>
27798a8eeeSLinus Walleij 
28*7e507119SGrigoryev Denis static struct regmap_config tps6105x_regmap_config = {
29*7e507119SGrigoryev Denis 	.reg_bits = 8,
30*7e507119SGrigoryev Denis 	.val_bits = 8,
31*7e507119SGrigoryev Denis 	.max_register = TPS6105X_REG_3,
32*7e507119SGrigoryev Denis };
33798a8eeeSLinus Walleij 
34f791be49SBill Pemberton static int tps6105x_startup(struct tps6105x *tps6105x)
35798a8eeeSLinus Walleij {
36798a8eeeSLinus Walleij 	int ret;
37*7e507119SGrigoryev Denis 	unsigned int regval;
38798a8eeeSLinus Walleij 
39*7e507119SGrigoryev Denis 	ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
40798a8eeeSLinus Walleij 	if (ret)
41798a8eeeSLinus Walleij 		return ret;
42798a8eeeSLinus Walleij 	switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
43798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_SHUTDOWN:
44798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
45798a8eeeSLinus Walleij 			 "TPS6105x found in SHUTDOWN mode\n");
46798a8eeeSLinus Walleij 		break;
47798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_TORCH:
48798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
49798a8eeeSLinus Walleij 			 "TPS6105x found in TORCH mode\n");
50798a8eeeSLinus Walleij 		break;
51798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_TORCH_FLASH:
52798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
53798a8eeeSLinus Walleij 			 "TPS6105x found in FLASH mode\n");
54798a8eeeSLinus Walleij 		break;
55798a8eeeSLinus Walleij 	case TPS6105X_REG0_MODE_VOLTAGE:
56798a8eeeSLinus Walleij 		dev_info(&tps6105x->client->dev,
57798a8eeeSLinus Walleij 			 "TPS6105x found in VOLTAGE mode\n");
58798a8eeeSLinus Walleij 		break;
59798a8eeeSLinus Walleij 	default:
60798a8eeeSLinus Walleij 		break;
61798a8eeeSLinus Walleij 	}
62798a8eeeSLinus Walleij 
63798a8eeeSLinus Walleij 	return ret;
64798a8eeeSLinus Walleij }
65798a8eeeSLinus Walleij 
66798a8eeeSLinus Walleij /*
67798a8eeeSLinus Walleij  * MFD cells - we have one cell which is selected operation
68798a8eeeSLinus Walleij  * mode, and we always have a GPIO cell.
69798a8eeeSLinus Walleij  */
70798a8eeeSLinus Walleij static struct mfd_cell tps6105x_cells[] = {
71798a8eeeSLinus Walleij 	{
72798a8eeeSLinus Walleij 		/* name will be runtime assigned */
73798a8eeeSLinus Walleij 		.id = -1,
74798a8eeeSLinus Walleij 	},
75798a8eeeSLinus Walleij 	{
76798a8eeeSLinus Walleij 		.name = "tps6105x-gpio",
77798a8eeeSLinus Walleij 		.id = -1,
78798a8eeeSLinus Walleij 	},
79798a8eeeSLinus Walleij };
80798a8eeeSLinus Walleij 
81f791be49SBill Pemberton static int tps6105x_probe(struct i2c_client *client,
82798a8eeeSLinus Walleij 			const struct i2c_device_id *id)
83798a8eeeSLinus Walleij {
84798a8eeeSLinus Walleij 	struct tps6105x			*tps6105x;
85798a8eeeSLinus Walleij 	struct tps6105x_platform_data	*pdata;
86798a8eeeSLinus Walleij 	int ret;
87798a8eeeSLinus Walleij 	int i;
88798a8eeeSLinus Walleij 
89ad83533aSHimangi Saraogi 	tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
90798a8eeeSLinus Walleij 	if (!tps6105x)
91798a8eeeSLinus Walleij 		return -ENOMEM;
92798a8eeeSLinus Walleij 
93*7e507119SGrigoryev Denis 	tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
94*7e507119SGrigoryev Denis 	if (IS_ERR(tps6105x->regmap))
95*7e507119SGrigoryev Denis 		return PTR_ERR(tps6105x->regmap);
96*7e507119SGrigoryev Denis 
97798a8eeeSLinus Walleij 	i2c_set_clientdata(client, tps6105x);
98798a8eeeSLinus Walleij 	tps6105x->client = client;
99334a41ceSJingoo Han 	pdata = dev_get_platdata(&client->dev);
100798a8eeeSLinus Walleij 	tps6105x->pdata = pdata;
101798a8eeeSLinus Walleij 
102798a8eeeSLinus Walleij 	ret = tps6105x_startup(tps6105x);
103798a8eeeSLinus Walleij 	if (ret) {
104798a8eeeSLinus Walleij 		dev_err(&client->dev, "chip initialization failed\n");
105ad83533aSHimangi Saraogi 		return ret;
106798a8eeeSLinus Walleij 	}
107798a8eeeSLinus Walleij 
108798a8eeeSLinus Walleij 	/* Remove warning texts when you implement new cell drivers */
109798a8eeeSLinus Walleij 	switch (pdata->mode) {
110798a8eeeSLinus Walleij 	case TPS6105X_MODE_SHUTDOWN:
111798a8eeeSLinus Walleij 		dev_info(&client->dev,
112798a8eeeSLinus Walleij 			 "present, not used for anything, only GPIO\n");
113798a8eeeSLinus Walleij 		break;
114798a8eeeSLinus Walleij 	case TPS6105X_MODE_TORCH:
115798a8eeeSLinus Walleij 		tps6105x_cells[0].name = "tps6105x-leds";
116798a8eeeSLinus Walleij 		dev_warn(&client->dev,
117798a8eeeSLinus Walleij 			 "torch mode is unsupported\n");
118798a8eeeSLinus Walleij 		break;
119798a8eeeSLinus Walleij 	case TPS6105X_MODE_TORCH_FLASH:
120798a8eeeSLinus Walleij 		tps6105x_cells[0].name = "tps6105x-flash";
121798a8eeeSLinus Walleij 		dev_warn(&client->dev,
122798a8eeeSLinus Walleij 			 "flash mode is unsupported\n");
123798a8eeeSLinus Walleij 		break;
124798a8eeeSLinus Walleij 	case TPS6105X_MODE_VOLTAGE:
125798a8eeeSLinus Walleij 		tps6105x_cells[0].name ="tps6105x-regulator";
126798a8eeeSLinus Walleij 		break;
127798a8eeeSLinus Walleij 	default:
128798a8eeeSLinus Walleij 		break;
129798a8eeeSLinus Walleij 	}
130798a8eeeSLinus Walleij 
131798a8eeeSLinus Walleij 	/* Set up and register the platform devices. */
132798a8eeeSLinus Walleij 	for (i = 0; i < ARRAY_SIZE(tps6105x_cells); i++) {
133798a8eeeSLinus Walleij 		/* One state holder for all drivers, this is simple */
134a7c98ce2SSamuel Ortiz 		tps6105x_cells[i].platform_data = tps6105x;
135a7c98ce2SSamuel Ortiz 		tps6105x_cells[i].pdata_size = sizeof(*tps6105x);
136798a8eeeSLinus Walleij 	}
137798a8eeeSLinus Walleij 
138ad83533aSHimangi Saraogi 	return mfd_add_devices(&client->dev, 0, tps6105x_cells,
1390848c94fSMark Brown 			       ARRAY_SIZE(tps6105x_cells), NULL, 0, NULL);
140798a8eeeSLinus Walleij }
141798a8eeeSLinus Walleij 
1424740f73fSBill Pemberton static int tps6105x_remove(struct i2c_client *client)
143798a8eeeSLinus Walleij {
144798a8eeeSLinus Walleij 	struct tps6105x *tps6105x = i2c_get_clientdata(client);
145798a8eeeSLinus Walleij 
146798a8eeeSLinus Walleij 	mfd_remove_devices(&client->dev);
147798a8eeeSLinus Walleij 
148798a8eeeSLinus Walleij 	/* Put chip in shutdown mode */
149*7e507119SGrigoryev Denis 	regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
150798a8eeeSLinus Walleij 		TPS6105X_REG0_MODE_MASK,
151798a8eeeSLinus Walleij 		TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
152798a8eeeSLinus Walleij 
153798a8eeeSLinus Walleij 	return 0;
154798a8eeeSLinus Walleij }
155798a8eeeSLinus Walleij 
156798a8eeeSLinus Walleij static const struct i2c_device_id tps6105x_id[] = {
157798a8eeeSLinus Walleij 	{ "tps61050", 0 },
158798a8eeeSLinus Walleij 	{ "tps61052", 0 },
159798a8eeeSLinus Walleij 	{ }
160798a8eeeSLinus Walleij };
161798a8eeeSLinus Walleij MODULE_DEVICE_TABLE(i2c, tps6105x_id);
162798a8eeeSLinus Walleij 
163798a8eeeSLinus Walleij static struct i2c_driver tps6105x_driver = {
164798a8eeeSLinus Walleij 	.driver = {
165798a8eeeSLinus Walleij 		.name	= "tps6105x",
166798a8eeeSLinus Walleij 	},
167798a8eeeSLinus Walleij 	.probe		= tps6105x_probe,
16884449216SBill Pemberton 	.remove		= tps6105x_remove,
169798a8eeeSLinus Walleij 	.id_table	= tps6105x_id,
170798a8eeeSLinus Walleij };
171798a8eeeSLinus Walleij 
172798a8eeeSLinus Walleij static int __init tps6105x_init(void)
173798a8eeeSLinus Walleij {
174798a8eeeSLinus Walleij 	return i2c_add_driver(&tps6105x_driver);
175798a8eeeSLinus Walleij }
176798a8eeeSLinus Walleij subsys_initcall(tps6105x_init);
177798a8eeeSLinus Walleij 
178798a8eeeSLinus Walleij static void __exit tps6105x_exit(void)
179798a8eeeSLinus Walleij {
180798a8eeeSLinus Walleij 	i2c_del_driver(&tps6105x_driver);
181798a8eeeSLinus Walleij }
182798a8eeeSLinus Walleij module_exit(tps6105x_exit);
183798a8eeeSLinus Walleij 
184798a8eeeSLinus Walleij MODULE_AUTHOR("Linus Walleij");
185798a8eeeSLinus Walleij MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
186798a8eeeSLinus Walleij MODULE_LICENSE("GPL v2");
187