blob: 51c54951c2206b98cd74daf638e3ddee7f978649 [file] [log] [blame]
Linus Walleij798a8ee2011-03-09 13:02:38 +01001/*
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>
Grigoryev Denis7e507112015-10-02 16:14:41 +000019#include <linux/regmap.h>
Linus Walleij798a8ee2011-03-09 13:02:38 +010020#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
Grigoryev Denis7e507112015-10-02 16:14:41 +000028static struct regmap_config tps6105x_regmap_config = {
29 .reg_bits = 8,
30 .val_bits = 8,
31 .max_register = TPS6105X_REG_3,
32};
Linus Walleij798a8ee2011-03-09 13:02:38 +010033
Bill Pembertonf791be42012-11-19 13:23:04 -050034static int tps6105x_startup(struct tps6105x *tps6105x)
Linus Walleij798a8ee2011-03-09 13:02:38 +010035{
36 int ret;
Grigoryev Denis7e507112015-10-02 16:14:41 +000037 unsigned int regval;
Linus Walleij798a8ee2011-03-09 13:02:38 +010038
Grigoryev Denis7e507112015-10-02 16:14:41 +000039 ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
Linus Walleij798a8ee2011-03-09 13:02:38 +010040 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/*
Grigoryev Denisea50e9d2015-09-25 16:57:09 +000067 * MFD cells - we always have a GPIO cell and we have one cell
68 * which is selected operation mode.
Linus Walleij798a8ee2011-03-09 13:02:38 +010069 */
Grigoryev Denisea50e9d2015-09-25 16:57:09 +000070static struct mfd_cell tps6105x_gpio_cell = {
71 .name = "tps6105x-gpio",
Linus Walleij798a8ee2011-03-09 13:02:38 +010072};
73
Grigoryev Denisea50e9d2015-09-25 16:57:09 +000074static struct mfd_cell tps6105x_leds_cell = {
75 .name = "tps6105x-leds",
76};
77
78static struct mfd_cell tps6105x_flash_cell = {
79 .name = "tps6105x-flash",
80};
81
82static struct mfd_cell tps6105x_regulator_cell = {
83 .name = "tps6105x-regulator",
84};
85
86static int tps6105x_add_device(struct tps6105x *tps6105x,
87 struct mfd_cell *cell)
88{
89 cell->platform_data = tps6105x;
90 cell->pdata_size = sizeof(*tps6105x);
91
92 return mfd_add_devices(&tps6105x->client->dev,
93 PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL);
94}
95
Bill Pembertonf791be42012-11-19 13:23:04 -050096static int tps6105x_probe(struct i2c_client *client,
Linus Walleij798a8ee2011-03-09 13:02:38 +010097 const struct i2c_device_id *id)
98{
99 struct tps6105x *tps6105x;
100 struct tps6105x_platform_data *pdata;
101 int ret;
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000102
103 pdata = dev_get_platdata(&client->dev);
104 if (!pdata) {
105 dev_err(&client->dev, "missing platform data\n");
106 return -ENODEV;
107 }
Linus Walleij798a8ee2011-03-09 13:02:38 +0100108
Himangi Saraogiad835332014-07-19 14:00:10 +0530109 tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
Linus Walleij798a8ee2011-03-09 13:02:38 +0100110 if (!tps6105x)
111 return -ENOMEM;
112
Grigoryev Denis7e507112015-10-02 16:14:41 +0000113 tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
114 if (IS_ERR(tps6105x->regmap))
115 return PTR_ERR(tps6105x->regmap);
116
Linus Walleij798a8ee2011-03-09 13:02:38 +0100117 i2c_set_clientdata(client, tps6105x);
118 tps6105x->client = client;
Linus Walleij798a8ee2011-03-09 13:02:38 +0100119 tps6105x->pdata = pdata;
Linus Walleij798a8ee2011-03-09 13:02:38 +0100120
121 ret = tps6105x_startup(tps6105x);
122 if (ret) {
123 dev_err(&client->dev, "chip initialization failed\n");
Himangi Saraogiad835332014-07-19 14:00:10 +0530124 return ret;
Linus Walleij798a8ee2011-03-09 13:02:38 +0100125 }
126
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000127 ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
128 if (ret)
129 return ret;
130
Linus Walleij798a8ee2011-03-09 13:02:38 +0100131 switch (pdata->mode) {
132 case TPS6105X_MODE_SHUTDOWN:
133 dev_info(&client->dev,
134 "present, not used for anything, only GPIO\n");
135 break;
136 case TPS6105X_MODE_TORCH:
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000137 ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
Linus Walleij798a8ee2011-03-09 13:02:38 +0100138 break;
139 case TPS6105X_MODE_TORCH_FLASH:
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000140 ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
Linus Walleij798a8ee2011-03-09 13:02:38 +0100141 break;
142 case TPS6105X_MODE_VOLTAGE:
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000143 ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
Linus Walleij798a8ee2011-03-09 13:02:38 +0100144 break;
145 default:
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000146 dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
Linus Walleij798a8ee2011-03-09 13:02:38 +0100147 break;
148 }
149
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000150 if (ret)
151 mfd_remove_devices(&client->dev);
Linus Walleij798a8ee2011-03-09 13:02:38 +0100152
Grigoryev Denisea50e9d2015-09-25 16:57:09 +0000153 return ret;
Linus Walleij798a8ee2011-03-09 13:02:38 +0100154}
155
Bill Pemberton4740f732012-11-19 13:26:01 -0500156static int tps6105x_remove(struct i2c_client *client)
Linus Walleij798a8ee2011-03-09 13:02:38 +0100157{
158 struct tps6105x *tps6105x = i2c_get_clientdata(client);
159
160 mfd_remove_devices(&client->dev);
161
162 /* Put chip in shutdown mode */
Grigoryev Denis7e507112015-10-02 16:14:41 +0000163 regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
Linus Walleij798a8ee2011-03-09 13:02:38 +0100164 TPS6105X_REG0_MODE_MASK,
165 TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
166
Linus Walleij798a8ee2011-03-09 13:02:38 +0100167 return 0;
168}
169
170static const struct i2c_device_id tps6105x_id[] = {
171 { "tps61050", 0 },
172 { "tps61052", 0 },
173 { }
174};
175MODULE_DEVICE_TABLE(i2c, tps6105x_id);
176
177static struct i2c_driver tps6105x_driver = {
178 .driver = {
179 .name = "tps6105x",
180 },
181 .probe = tps6105x_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500182 .remove = tps6105x_remove,
Linus Walleij798a8ee2011-03-09 13:02:38 +0100183 .id_table = tps6105x_id,
184};
185
186static int __init tps6105x_init(void)
187{
188 return i2c_add_driver(&tps6105x_driver);
189}
190subsys_initcall(tps6105x_init);
191
192static void __exit tps6105x_exit(void)
193{
194 i2c_del_driver(&tps6105x_driver);
195}
196module_exit(tps6105x_exit);
197
198MODULE_AUTHOR("Linus Walleij");
199MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
200MODULE_LICENSE("GPL v2");