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