xref: /OK3568_Linux_fs/kernel/drivers/mfd/tps6105x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Core driver for TPS61050/61052 boost converters, used for while LED
4*4882a593Smuzhiyun  * driving, audio power amplification, white LED flash, and generic
5*4882a593Smuzhiyun  * boost conversion. Additionally it provides a 1-bit GPIO pin (out or in)
6*4882a593Smuzhiyun  * and a flash synchronization pin to synchronize flash events when used as
7*4882a593Smuzhiyun  * flashgun.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (C) 2011 ST-Ericsson SA
10*4882a593Smuzhiyun  * Written on behalf of Linaro for ST-Ericsson
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Author: Linus Walleij <linus.walleij@linaro.org>
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/regmap.h>
19*4882a593Smuzhiyun #include <linux/gpio.h>
20*4882a593Smuzhiyun #include <linux/spinlock.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun #include <linux/err.h>
23*4882a593Smuzhiyun #include <linux/mfd/core.h>
24*4882a593Smuzhiyun #include <linux/mfd/tps6105x.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static struct regmap_config tps6105x_regmap_config = {
27*4882a593Smuzhiyun 	.reg_bits = 8,
28*4882a593Smuzhiyun 	.val_bits = 8,
29*4882a593Smuzhiyun 	.max_register = TPS6105X_REG_3,
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
tps6105x_startup(struct tps6105x * tps6105x)32*4882a593Smuzhiyun static int tps6105x_startup(struct tps6105x *tps6105x)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	int ret;
35*4882a593Smuzhiyun 	unsigned int regval;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
38*4882a593Smuzhiyun 	if (ret)
39*4882a593Smuzhiyun 		return ret;
40*4882a593Smuzhiyun 	switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
41*4882a593Smuzhiyun 	case TPS6105X_REG0_MODE_SHUTDOWN:
42*4882a593Smuzhiyun 		dev_info(&tps6105x->client->dev,
43*4882a593Smuzhiyun 			 "TPS6105x found in SHUTDOWN mode\n");
44*4882a593Smuzhiyun 		break;
45*4882a593Smuzhiyun 	case TPS6105X_REG0_MODE_TORCH:
46*4882a593Smuzhiyun 		dev_info(&tps6105x->client->dev,
47*4882a593Smuzhiyun 			 "TPS6105x found in TORCH mode\n");
48*4882a593Smuzhiyun 		break;
49*4882a593Smuzhiyun 	case TPS6105X_REG0_MODE_TORCH_FLASH:
50*4882a593Smuzhiyun 		dev_info(&tps6105x->client->dev,
51*4882a593Smuzhiyun 			 "TPS6105x found in FLASH mode\n");
52*4882a593Smuzhiyun 		break;
53*4882a593Smuzhiyun 	case TPS6105X_REG0_MODE_VOLTAGE:
54*4882a593Smuzhiyun 		dev_info(&tps6105x->client->dev,
55*4882a593Smuzhiyun 			 "TPS6105x found in VOLTAGE mode\n");
56*4882a593Smuzhiyun 		break;
57*4882a593Smuzhiyun 	default:
58*4882a593Smuzhiyun 		break;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return ret;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * MFD cells - we always have a GPIO cell and we have one cell
66*4882a593Smuzhiyun  * which is selected operation mode.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun static struct mfd_cell tps6105x_gpio_cell = {
69*4882a593Smuzhiyun 	.name = "tps6105x-gpio",
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun static struct mfd_cell tps6105x_leds_cell = {
73*4882a593Smuzhiyun 	.name = "tps6105x-leds",
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun static struct mfd_cell tps6105x_flash_cell = {
77*4882a593Smuzhiyun 	.name = "tps6105x-flash",
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static struct mfd_cell tps6105x_regulator_cell = {
81*4882a593Smuzhiyun 	.name = "tps6105x-regulator",
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
tps6105x_add_device(struct tps6105x * tps6105x,struct mfd_cell * cell)84*4882a593Smuzhiyun static int tps6105x_add_device(struct tps6105x *tps6105x,
85*4882a593Smuzhiyun 			       struct mfd_cell *cell)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	cell->platform_data = tps6105x;
88*4882a593Smuzhiyun 	cell->pdata_size = sizeof(*tps6105x);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	return mfd_add_devices(&tps6105x->client->dev,
91*4882a593Smuzhiyun 			       PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
tps6105x_parse_dt(struct device * dev)94*4882a593Smuzhiyun static struct tps6105x_platform_data *tps6105x_parse_dt(struct device *dev)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
97*4882a593Smuzhiyun 	struct tps6105x_platform_data *pdata;
98*4882a593Smuzhiyun 	struct device_node *child;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (!np)
101*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
102*4882a593Smuzhiyun 	if (of_get_available_child_count(np) > 1) {
103*4882a593Smuzhiyun 		dev_err(dev, "cannot support multiple operational modes");
104*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
107*4882a593Smuzhiyun 	if (!pdata)
108*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
109*4882a593Smuzhiyun 	pdata->mode = TPS6105X_MODE_SHUTDOWN;
110*4882a593Smuzhiyun 	for_each_available_child_of_node(np, child) {
111*4882a593Smuzhiyun 		if (child->name && !of_node_cmp(child->name, "regulator"))
112*4882a593Smuzhiyun 			pdata->mode = TPS6105X_MODE_VOLTAGE;
113*4882a593Smuzhiyun 		else if (child->name && !of_node_cmp(child->name, "led"))
114*4882a593Smuzhiyun 			pdata->mode = TPS6105X_MODE_TORCH;
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	return pdata;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
tps6105x_probe(struct i2c_client * client,const struct i2c_device_id * id)120*4882a593Smuzhiyun static int tps6105x_probe(struct i2c_client *client,
121*4882a593Smuzhiyun 			const struct i2c_device_id *id)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct tps6105x			*tps6105x;
124*4882a593Smuzhiyun 	struct tps6105x_platform_data	*pdata;
125*4882a593Smuzhiyun 	int ret;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	pdata = dev_get_platdata(&client->dev);
128*4882a593Smuzhiyun 	if (!pdata)
129*4882a593Smuzhiyun 		pdata = tps6105x_parse_dt(&client->dev);
130*4882a593Smuzhiyun 	if (IS_ERR(pdata)) {
131*4882a593Smuzhiyun 		dev_err(&client->dev, "No platform data or DT found");
132*4882a593Smuzhiyun 		return PTR_ERR(pdata);
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
136*4882a593Smuzhiyun 	if (!tps6105x)
137*4882a593Smuzhiyun 		return -ENOMEM;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
140*4882a593Smuzhiyun 	if (IS_ERR(tps6105x->regmap))
141*4882a593Smuzhiyun 		return PTR_ERR(tps6105x->regmap);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	i2c_set_clientdata(client, tps6105x);
144*4882a593Smuzhiyun 	tps6105x->client = client;
145*4882a593Smuzhiyun 	tps6105x->pdata = pdata;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	ret = tps6105x_startup(tps6105x);
148*4882a593Smuzhiyun 	if (ret) {
149*4882a593Smuzhiyun 		dev_err(&client->dev, "chip initialization failed\n");
150*4882a593Smuzhiyun 		return ret;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
154*4882a593Smuzhiyun 	if (ret)
155*4882a593Smuzhiyun 		return ret;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	switch (pdata->mode) {
158*4882a593Smuzhiyun 	case TPS6105X_MODE_SHUTDOWN:
159*4882a593Smuzhiyun 		dev_info(&client->dev,
160*4882a593Smuzhiyun 			 "present, not used for anything, only GPIO\n");
161*4882a593Smuzhiyun 		break;
162*4882a593Smuzhiyun 	case TPS6105X_MODE_TORCH:
163*4882a593Smuzhiyun 		ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
164*4882a593Smuzhiyun 		break;
165*4882a593Smuzhiyun 	case TPS6105X_MODE_TORCH_FLASH:
166*4882a593Smuzhiyun 		ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
167*4882a593Smuzhiyun 		break;
168*4882a593Smuzhiyun 	case TPS6105X_MODE_VOLTAGE:
169*4882a593Smuzhiyun 		ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
170*4882a593Smuzhiyun 		break;
171*4882a593Smuzhiyun 	default:
172*4882a593Smuzhiyun 		dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
173*4882a593Smuzhiyun 		break;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	if (ret)
177*4882a593Smuzhiyun 		mfd_remove_devices(&client->dev);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return ret;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
tps6105x_remove(struct i2c_client * client)182*4882a593Smuzhiyun static int tps6105x_remove(struct i2c_client *client)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct tps6105x *tps6105x = i2c_get_clientdata(client);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	mfd_remove_devices(&client->dev);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* Put chip in shutdown mode */
189*4882a593Smuzhiyun 	regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
190*4882a593Smuzhiyun 		TPS6105X_REG0_MODE_MASK,
191*4882a593Smuzhiyun 		TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	return 0;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun static const struct i2c_device_id tps6105x_id[] = {
197*4882a593Smuzhiyun 	{ "tps61050", 0 },
198*4882a593Smuzhiyun 	{ "tps61052", 0 },
199*4882a593Smuzhiyun 	{ }
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, tps6105x_id);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun static const struct of_device_id tps6105x_of_match[] = {
204*4882a593Smuzhiyun 	{ .compatible = "ti,tps61050" },
205*4882a593Smuzhiyun 	{ .compatible = "ti,tps61052" },
206*4882a593Smuzhiyun 	{ },
207*4882a593Smuzhiyun };
208*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tps6105x_of_match);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun static struct i2c_driver tps6105x_driver = {
211*4882a593Smuzhiyun 	.driver = {
212*4882a593Smuzhiyun 		.name	= "tps6105x",
213*4882a593Smuzhiyun 		.of_match_table = tps6105x_of_match,
214*4882a593Smuzhiyun 	},
215*4882a593Smuzhiyun 	.probe		= tps6105x_probe,
216*4882a593Smuzhiyun 	.remove		= tps6105x_remove,
217*4882a593Smuzhiyun 	.id_table	= tps6105x_id,
218*4882a593Smuzhiyun };
219*4882a593Smuzhiyun 
tps6105x_init(void)220*4882a593Smuzhiyun static int __init tps6105x_init(void)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	return i2c_add_driver(&tps6105x_driver);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun subsys_initcall(tps6105x_init);
225*4882a593Smuzhiyun 
tps6105x_exit(void)226*4882a593Smuzhiyun static void __exit tps6105x_exit(void)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	i2c_del_driver(&tps6105x_driver);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun module_exit(tps6105x_exit);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun MODULE_AUTHOR("Linus Walleij");
233*4882a593Smuzhiyun MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
234*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
235