Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 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> |
Grigoryev Denis | 7e50711 | 2015-10-02 16:14:41 +0000 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 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 | |
Grigoryev Denis | 7e50711 | 2015-10-02 16:14:41 +0000 | [diff] [blame] | 28 | static struct regmap_config tps6105x_regmap_config = { |
| 29 | .reg_bits = 8, |
| 30 | .val_bits = 8, |
| 31 | .max_register = TPS6105X_REG_3, |
| 32 | }; |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 33 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 34 | static int tps6105x_startup(struct tps6105x *tps6105x) |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 35 | { |
| 36 | int ret; |
Grigoryev Denis | 7e50711 | 2015-10-02 16:14:41 +0000 | [diff] [blame] | 37 | unsigned int regval; |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 38 | |
Grigoryev Denis | 7e50711 | 2015-10-02 16:14:41 +0000 | [diff] [blame] | 39 | ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, ®val); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 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 | /* |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 67 | * MFD cells - we always have a GPIO cell and we have one cell |
| 68 | * which is selected operation mode. |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 69 | */ |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 70 | static struct mfd_cell tps6105x_gpio_cell = { |
| 71 | .name = "tps6105x-gpio", |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 74 | static struct mfd_cell tps6105x_leds_cell = { |
| 75 | .name = "tps6105x-leds", |
| 76 | }; |
| 77 | |
| 78 | static struct mfd_cell tps6105x_flash_cell = { |
| 79 | .name = "tps6105x-flash", |
| 80 | }; |
| 81 | |
| 82 | static struct mfd_cell tps6105x_regulator_cell = { |
| 83 | .name = "tps6105x-regulator", |
| 84 | }; |
| 85 | |
| 86 | static 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 Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 96 | static int tps6105x_probe(struct i2c_client *client, |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 97 | const struct i2c_device_id *id) |
| 98 | { |
| 99 | struct tps6105x *tps6105x; |
| 100 | struct tps6105x_platform_data *pdata; |
| 101 | int ret; |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 102 | |
| 103 | pdata = dev_get_platdata(&client->dev); |
| 104 | if (!pdata) { |
| 105 | dev_err(&client->dev, "missing platform data\n"); |
| 106 | return -ENODEV; |
| 107 | } |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 108 | |
Himangi Saraogi | ad83533 | 2014-07-19 14:00:10 +0530 | [diff] [blame] | 109 | tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 110 | if (!tps6105x) |
| 111 | return -ENOMEM; |
| 112 | |
Grigoryev Denis | 7e50711 | 2015-10-02 16:14:41 +0000 | [diff] [blame] | 113 | tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config); |
| 114 | if (IS_ERR(tps6105x->regmap)) |
| 115 | return PTR_ERR(tps6105x->regmap); |
| 116 | |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 117 | i2c_set_clientdata(client, tps6105x); |
| 118 | tps6105x->client = client; |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 119 | tps6105x->pdata = pdata; |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 120 | |
| 121 | ret = tps6105x_startup(tps6105x); |
| 122 | if (ret) { |
| 123 | dev_err(&client->dev, "chip initialization failed\n"); |
Himangi Saraogi | ad83533 | 2014-07-19 14:00:10 +0530 | [diff] [blame] | 124 | return ret; |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 127 | ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 131 | 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 Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 137 | ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 138 | break; |
| 139 | case TPS6105X_MODE_TORCH_FLASH: |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 140 | ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 141 | break; |
| 142 | case TPS6105X_MODE_VOLTAGE: |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 143 | ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 144 | break; |
| 145 | default: |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 146 | dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 150 | if (ret) |
| 151 | mfd_remove_devices(&client->dev); |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 152 | |
Grigoryev Denis | ea50e9d | 2015-09-25 16:57:09 +0000 | [diff] [blame] | 153 | return ret; |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 156 | static int tps6105x_remove(struct i2c_client *client) |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 157 | { |
| 158 | struct tps6105x *tps6105x = i2c_get_clientdata(client); |
| 159 | |
| 160 | mfd_remove_devices(&client->dev); |
| 161 | |
| 162 | /* Put chip in shutdown mode */ |
Grigoryev Denis | 7e50711 | 2015-10-02 16:14:41 +0000 | [diff] [blame] | 163 | regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0, |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 164 | TPS6105X_REG0_MODE_MASK, |
| 165 | TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT); |
| 166 | |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static const struct i2c_device_id tps6105x_id[] = { |
| 171 | { "tps61050", 0 }, |
| 172 | { "tps61052", 0 }, |
| 173 | { } |
| 174 | }; |
| 175 | MODULE_DEVICE_TABLE(i2c, tps6105x_id); |
| 176 | |
| 177 | static struct i2c_driver tps6105x_driver = { |
| 178 | .driver = { |
| 179 | .name = "tps6105x", |
| 180 | }, |
| 181 | .probe = tps6105x_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 182 | .remove = tps6105x_remove, |
Linus Walleij | 798a8ee | 2011-03-09 13:02:38 +0100 | [diff] [blame] | 183 | .id_table = tps6105x_id, |
| 184 | }; |
| 185 | |
| 186 | static int __init tps6105x_init(void) |
| 187 | { |
| 188 | return i2c_add_driver(&tps6105x_driver); |
| 189 | } |
| 190 | subsys_initcall(tps6105x_init); |
| 191 | |
| 192 | static void __exit tps6105x_exit(void) |
| 193 | { |
| 194 | i2c_del_driver(&tps6105x_driver); |
| 195 | } |
| 196 | module_exit(tps6105x_exit); |
| 197 | |
| 198 | MODULE_AUTHOR("Linus Walleij"); |
| 199 | MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver"); |
| 200 | MODULE_LICENSE("GPL v2"); |