xref: /openbmc/linux/drivers/mfd/tps6105x.c (revision 7e507119)
1 /*
2  * Core driver for TPS61050/61052 boost converters, used for while LED
3  * driving, audio power amplification, white LED flash, and generic
4  * boost conversion. Additionally it provides a 1-bit GPIO pin (out or in)
5  * and a flash synchronization pin to synchronize flash events when used as
6  * flashgun.
7  *
8  * Copyright (C) 2011 ST-Ericsson SA
9  * Written on behalf of Linaro for ST-Ericsson
10  *
11  * Author: Linus Walleij <linus.walleij@linaro.org>
12  *
13  * License terms: GNU General Public License (GPL) version 2
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
20 #include <linux/gpio.h>
21 #include <linux/spinlock.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/tps6105x.h>
27 
28 static struct regmap_config tps6105x_regmap_config = {
29 	.reg_bits = 8,
30 	.val_bits = 8,
31 	.max_register = TPS6105X_REG_3,
32 };
33 
34 static int tps6105x_startup(struct tps6105x *tps6105x)
35 {
36 	int ret;
37 	unsigned int regval;
38 
39 	ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
40 	if (ret)
41 		return ret;
42 	switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
43 	case TPS6105X_REG0_MODE_SHUTDOWN:
44 		dev_info(&tps6105x->client->dev,
45 			 "TPS6105x found in SHUTDOWN mode\n");
46 		break;
47 	case TPS6105X_REG0_MODE_TORCH:
48 		dev_info(&tps6105x->client->dev,
49 			 "TPS6105x found in TORCH mode\n");
50 		break;
51 	case TPS6105X_REG0_MODE_TORCH_FLASH:
52 		dev_info(&tps6105x->client->dev,
53 			 "TPS6105x found in FLASH mode\n");
54 		break;
55 	case TPS6105X_REG0_MODE_VOLTAGE:
56 		dev_info(&tps6105x->client->dev,
57 			 "TPS6105x found in VOLTAGE mode\n");
58 		break;
59 	default:
60 		break;
61 	}
62 
63 	return ret;
64 }
65 
66 /*
67  * MFD cells - we have one cell which is selected operation
68  * mode, and we always have a GPIO cell.
69  */
70 static struct mfd_cell tps6105x_cells[] = {
71 	{
72 		/* name will be runtime assigned */
73 		.id = -1,
74 	},
75 	{
76 		.name = "tps6105x-gpio",
77 		.id = -1,
78 	},
79 };
80 
81 static int tps6105x_probe(struct i2c_client *client,
82 			const struct i2c_device_id *id)
83 {
84 	struct tps6105x			*tps6105x;
85 	struct tps6105x_platform_data	*pdata;
86 	int ret;
87 	int i;
88 
89 	tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
90 	if (!tps6105x)
91 		return -ENOMEM;
92 
93 	tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
94 	if (IS_ERR(tps6105x->regmap))
95 		return PTR_ERR(tps6105x->regmap);
96 
97 	i2c_set_clientdata(client, tps6105x);
98 	tps6105x->client = client;
99 	pdata = dev_get_platdata(&client->dev);
100 	tps6105x->pdata = pdata;
101 
102 	ret = tps6105x_startup(tps6105x);
103 	if (ret) {
104 		dev_err(&client->dev, "chip initialization failed\n");
105 		return ret;
106 	}
107 
108 	/* Remove warning texts when you implement new cell drivers */
109 	switch (pdata->mode) {
110 	case TPS6105X_MODE_SHUTDOWN:
111 		dev_info(&client->dev,
112 			 "present, not used for anything, only GPIO\n");
113 		break;
114 	case TPS6105X_MODE_TORCH:
115 		tps6105x_cells[0].name = "tps6105x-leds";
116 		dev_warn(&client->dev,
117 			 "torch mode is unsupported\n");
118 		break;
119 	case TPS6105X_MODE_TORCH_FLASH:
120 		tps6105x_cells[0].name = "tps6105x-flash";
121 		dev_warn(&client->dev,
122 			 "flash mode is unsupported\n");
123 		break;
124 	case TPS6105X_MODE_VOLTAGE:
125 		tps6105x_cells[0].name ="tps6105x-regulator";
126 		break;
127 	default:
128 		break;
129 	}
130 
131 	/* Set up and register the platform devices. */
132 	for (i = 0; i < ARRAY_SIZE(tps6105x_cells); i++) {
133 		/* One state holder for all drivers, this is simple */
134 		tps6105x_cells[i].platform_data = tps6105x;
135 		tps6105x_cells[i].pdata_size = sizeof(*tps6105x);
136 	}
137 
138 	return mfd_add_devices(&client->dev, 0, tps6105x_cells,
139 			       ARRAY_SIZE(tps6105x_cells), NULL, 0, NULL);
140 }
141 
142 static int tps6105x_remove(struct i2c_client *client)
143 {
144 	struct tps6105x *tps6105x = i2c_get_clientdata(client);
145 
146 	mfd_remove_devices(&client->dev);
147 
148 	/* Put chip in shutdown mode */
149 	regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
150 		TPS6105X_REG0_MODE_MASK,
151 		TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
152 
153 	return 0;
154 }
155 
156 static const struct i2c_device_id tps6105x_id[] = {
157 	{ "tps61050", 0 },
158 	{ "tps61052", 0 },
159 	{ }
160 };
161 MODULE_DEVICE_TABLE(i2c, tps6105x_id);
162 
163 static struct i2c_driver tps6105x_driver = {
164 	.driver = {
165 		.name	= "tps6105x",
166 	},
167 	.probe		= tps6105x_probe,
168 	.remove		= tps6105x_remove,
169 	.id_table	= tps6105x_id,
170 };
171 
172 static int __init tps6105x_init(void)
173 {
174 	return i2c_add_driver(&tps6105x_driver);
175 }
176 subsys_initcall(tps6105x_init);
177 
178 static void __exit tps6105x_exit(void)
179 {
180 	i2c_del_driver(&tps6105x_driver);
181 }
182 module_exit(tps6105x_exit);
183 
184 MODULE_AUTHOR("Linus Walleij");
185 MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
186 MODULE_LICENSE("GPL v2");
187