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> 197e507119SGrigoryev 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 287e507119SGrigoryev Denis static struct regmap_config tps6105x_regmap_config = { 297e507119SGrigoryev Denis .reg_bits = 8, 307e507119SGrigoryev Denis .val_bits = 8, 317e507119SGrigoryev Denis .max_register = TPS6105X_REG_3, 327e507119SGrigoryev Denis }; 33798a8eeeSLinus Walleij 34f791be49SBill Pemberton static int tps6105x_startup(struct tps6105x *tps6105x) 35798a8eeeSLinus Walleij { 36798a8eeeSLinus Walleij int ret; 377e507119SGrigoryev Denis unsigned int regval; 38798a8eeeSLinus Walleij 397e507119SGrigoryev Denis ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, ®val); 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 /* 67*ea50e9d3SGrigoryev Denis * MFD cells - we always have a GPIO cell and we have one cell 68*ea50e9d3SGrigoryev Denis * which is selected operation mode. 69798a8eeeSLinus Walleij */ 70*ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_gpio_cell = { 71798a8eeeSLinus Walleij .name = "tps6105x-gpio", 72798a8eeeSLinus Walleij }; 73798a8eeeSLinus Walleij 74*ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_leds_cell = { 75*ea50e9d3SGrigoryev Denis .name = "tps6105x-leds", 76*ea50e9d3SGrigoryev Denis }; 77*ea50e9d3SGrigoryev Denis 78*ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_flash_cell = { 79*ea50e9d3SGrigoryev Denis .name = "tps6105x-flash", 80*ea50e9d3SGrigoryev Denis }; 81*ea50e9d3SGrigoryev Denis 82*ea50e9d3SGrigoryev Denis static struct mfd_cell tps6105x_regulator_cell = { 83*ea50e9d3SGrigoryev Denis .name = "tps6105x-regulator", 84*ea50e9d3SGrigoryev Denis }; 85*ea50e9d3SGrigoryev Denis 86*ea50e9d3SGrigoryev Denis static int tps6105x_add_device(struct tps6105x *tps6105x, 87*ea50e9d3SGrigoryev Denis struct mfd_cell *cell) 88*ea50e9d3SGrigoryev Denis { 89*ea50e9d3SGrigoryev Denis cell->platform_data = tps6105x; 90*ea50e9d3SGrigoryev Denis cell->pdata_size = sizeof(*tps6105x); 91*ea50e9d3SGrigoryev Denis 92*ea50e9d3SGrigoryev Denis return mfd_add_devices(&tps6105x->client->dev, 93*ea50e9d3SGrigoryev Denis PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL); 94*ea50e9d3SGrigoryev Denis } 95*ea50e9d3SGrigoryev Denis 96f791be49SBill Pemberton static int tps6105x_probe(struct i2c_client *client, 97798a8eeeSLinus Walleij const struct i2c_device_id *id) 98798a8eeeSLinus Walleij { 99798a8eeeSLinus Walleij struct tps6105x *tps6105x; 100798a8eeeSLinus Walleij struct tps6105x_platform_data *pdata; 101798a8eeeSLinus Walleij int ret; 102*ea50e9d3SGrigoryev Denis 103*ea50e9d3SGrigoryev Denis pdata = dev_get_platdata(&client->dev); 104*ea50e9d3SGrigoryev Denis if (!pdata) { 105*ea50e9d3SGrigoryev Denis dev_err(&client->dev, "missing platform data\n"); 106*ea50e9d3SGrigoryev Denis return -ENODEV; 107*ea50e9d3SGrigoryev Denis } 108798a8eeeSLinus Walleij 109ad83533aSHimangi Saraogi tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL); 110798a8eeeSLinus Walleij if (!tps6105x) 111798a8eeeSLinus Walleij return -ENOMEM; 112798a8eeeSLinus Walleij 1137e507119SGrigoryev Denis tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config); 1147e507119SGrigoryev Denis if (IS_ERR(tps6105x->regmap)) 1157e507119SGrigoryev Denis return PTR_ERR(tps6105x->regmap); 1167e507119SGrigoryev Denis 117798a8eeeSLinus Walleij i2c_set_clientdata(client, tps6105x); 118798a8eeeSLinus Walleij tps6105x->client = client; 119798a8eeeSLinus Walleij tps6105x->pdata = pdata; 120798a8eeeSLinus Walleij 121798a8eeeSLinus Walleij ret = tps6105x_startup(tps6105x); 122798a8eeeSLinus Walleij if (ret) { 123798a8eeeSLinus Walleij dev_err(&client->dev, "chip initialization failed\n"); 124ad83533aSHimangi Saraogi return ret; 125798a8eeeSLinus Walleij } 126798a8eeeSLinus Walleij 127*ea50e9d3SGrigoryev Denis ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell); 128*ea50e9d3SGrigoryev Denis if (ret) 129*ea50e9d3SGrigoryev Denis return ret; 130*ea50e9d3SGrigoryev Denis 131798a8eeeSLinus Walleij switch (pdata->mode) { 132798a8eeeSLinus Walleij case TPS6105X_MODE_SHUTDOWN: 133798a8eeeSLinus Walleij dev_info(&client->dev, 134798a8eeeSLinus Walleij "present, not used for anything, only GPIO\n"); 135798a8eeeSLinus Walleij break; 136798a8eeeSLinus Walleij case TPS6105X_MODE_TORCH: 137*ea50e9d3SGrigoryev Denis ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell); 138798a8eeeSLinus Walleij break; 139798a8eeeSLinus Walleij case TPS6105X_MODE_TORCH_FLASH: 140*ea50e9d3SGrigoryev Denis ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell); 141798a8eeeSLinus Walleij break; 142798a8eeeSLinus Walleij case TPS6105X_MODE_VOLTAGE: 143*ea50e9d3SGrigoryev Denis ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell); 144798a8eeeSLinus Walleij break; 145798a8eeeSLinus Walleij default: 146*ea50e9d3SGrigoryev Denis dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode); 147798a8eeeSLinus Walleij break; 148798a8eeeSLinus Walleij } 149798a8eeeSLinus Walleij 150*ea50e9d3SGrigoryev Denis if (ret) 151*ea50e9d3SGrigoryev Denis mfd_remove_devices(&client->dev); 152798a8eeeSLinus Walleij 153*ea50e9d3SGrigoryev Denis return ret; 154798a8eeeSLinus Walleij } 155798a8eeeSLinus Walleij 1564740f73fSBill Pemberton static int tps6105x_remove(struct i2c_client *client) 157798a8eeeSLinus Walleij { 158798a8eeeSLinus Walleij struct tps6105x *tps6105x = i2c_get_clientdata(client); 159798a8eeeSLinus Walleij 160798a8eeeSLinus Walleij mfd_remove_devices(&client->dev); 161798a8eeeSLinus Walleij 162798a8eeeSLinus Walleij /* Put chip in shutdown mode */ 1637e507119SGrigoryev Denis regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0, 164798a8eeeSLinus Walleij TPS6105X_REG0_MODE_MASK, 165798a8eeeSLinus Walleij TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT); 166798a8eeeSLinus Walleij 167798a8eeeSLinus Walleij return 0; 168798a8eeeSLinus Walleij } 169798a8eeeSLinus Walleij 170798a8eeeSLinus Walleij static const struct i2c_device_id tps6105x_id[] = { 171798a8eeeSLinus Walleij { "tps61050", 0 }, 172798a8eeeSLinus Walleij { "tps61052", 0 }, 173798a8eeeSLinus Walleij { } 174798a8eeeSLinus Walleij }; 175798a8eeeSLinus Walleij MODULE_DEVICE_TABLE(i2c, tps6105x_id); 176798a8eeeSLinus Walleij 177798a8eeeSLinus Walleij static struct i2c_driver tps6105x_driver = { 178798a8eeeSLinus Walleij .driver = { 179798a8eeeSLinus Walleij .name = "tps6105x", 180798a8eeeSLinus Walleij }, 181798a8eeeSLinus Walleij .probe = tps6105x_probe, 18284449216SBill Pemberton .remove = tps6105x_remove, 183798a8eeeSLinus Walleij .id_table = tps6105x_id, 184798a8eeeSLinus Walleij }; 185798a8eeeSLinus Walleij 186798a8eeeSLinus Walleij static int __init tps6105x_init(void) 187798a8eeeSLinus Walleij { 188798a8eeeSLinus Walleij return i2c_add_driver(&tps6105x_driver); 189798a8eeeSLinus Walleij } 190798a8eeeSLinus Walleij subsys_initcall(tps6105x_init); 191798a8eeeSLinus Walleij 192798a8eeeSLinus Walleij static void __exit tps6105x_exit(void) 193798a8eeeSLinus Walleij { 194798a8eeeSLinus Walleij i2c_del_driver(&tps6105x_driver); 195798a8eeeSLinus Walleij } 196798a8eeeSLinus Walleij module_exit(tps6105x_exit); 197798a8eeeSLinus Walleij 198798a8eeeSLinus Walleij MODULE_AUTHOR("Linus Walleij"); 199798a8eeeSLinus Walleij MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver"); 200798a8eeeSLinus Walleij MODULE_LICENSE("GPL v2"); 201