18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Core driver for TPS61050/61052 boost converters, used for while LED
48c2ecf20Sopenharmony_ci * driving, audio power amplification, white LED flash, and generic
58c2ecf20Sopenharmony_ci * boost conversion. Additionally it provides a 1-bit GPIO pin (out or in)
68c2ecf20Sopenharmony_ci * and a flash synchronization pin to synchronize flash events when used as
78c2ecf20Sopenharmony_ci * flashgun.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2011 ST-Ericsson SA
108c2ecf20Sopenharmony_ci * Written on behalf of Linaro for ST-Ericsson
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Author: Linus Walleij <linus.walleij@linaro.org>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/regmap.h>
198c2ecf20Sopenharmony_ci#include <linux/gpio.h>
208c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/err.h>
238c2ecf20Sopenharmony_ci#include <linux/mfd/core.h>
248c2ecf20Sopenharmony_ci#include <linux/mfd/tps6105x.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic struct regmap_config tps6105x_regmap_config = {
278c2ecf20Sopenharmony_ci	.reg_bits = 8,
288c2ecf20Sopenharmony_ci	.val_bits = 8,
298c2ecf20Sopenharmony_ci	.max_register = TPS6105X_REG_3,
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int tps6105x_startup(struct tps6105x *tps6105x)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	int ret;
358c2ecf20Sopenharmony_ci	unsigned int regval;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
388c2ecf20Sopenharmony_ci	if (ret)
398c2ecf20Sopenharmony_ci		return ret;
408c2ecf20Sopenharmony_ci	switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
418c2ecf20Sopenharmony_ci	case TPS6105X_REG0_MODE_SHUTDOWN:
428c2ecf20Sopenharmony_ci		dev_info(&tps6105x->client->dev,
438c2ecf20Sopenharmony_ci			 "TPS6105x found in SHUTDOWN mode\n");
448c2ecf20Sopenharmony_ci		break;
458c2ecf20Sopenharmony_ci	case TPS6105X_REG0_MODE_TORCH:
468c2ecf20Sopenharmony_ci		dev_info(&tps6105x->client->dev,
478c2ecf20Sopenharmony_ci			 "TPS6105x found in TORCH mode\n");
488c2ecf20Sopenharmony_ci		break;
498c2ecf20Sopenharmony_ci	case TPS6105X_REG0_MODE_TORCH_FLASH:
508c2ecf20Sopenharmony_ci		dev_info(&tps6105x->client->dev,
518c2ecf20Sopenharmony_ci			 "TPS6105x found in FLASH mode\n");
528c2ecf20Sopenharmony_ci		break;
538c2ecf20Sopenharmony_ci	case TPS6105X_REG0_MODE_VOLTAGE:
548c2ecf20Sopenharmony_ci		dev_info(&tps6105x->client->dev,
558c2ecf20Sopenharmony_ci			 "TPS6105x found in VOLTAGE mode\n");
568c2ecf20Sopenharmony_ci		break;
578c2ecf20Sopenharmony_ci	default:
588c2ecf20Sopenharmony_ci		break;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return ret;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/*
658c2ecf20Sopenharmony_ci * MFD cells - we always have a GPIO cell and we have one cell
668c2ecf20Sopenharmony_ci * which is selected operation mode.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic struct mfd_cell tps6105x_gpio_cell = {
698c2ecf20Sopenharmony_ci	.name = "tps6105x-gpio",
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic struct mfd_cell tps6105x_leds_cell = {
738c2ecf20Sopenharmony_ci	.name = "tps6105x-leds",
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic struct mfd_cell tps6105x_flash_cell = {
778c2ecf20Sopenharmony_ci	.name = "tps6105x-flash",
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic struct mfd_cell tps6105x_regulator_cell = {
818c2ecf20Sopenharmony_ci	.name = "tps6105x-regulator",
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int tps6105x_add_device(struct tps6105x *tps6105x,
858c2ecf20Sopenharmony_ci			       struct mfd_cell *cell)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	cell->platform_data = tps6105x;
888c2ecf20Sopenharmony_ci	cell->pdata_size = sizeof(*tps6105x);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return mfd_add_devices(&tps6105x->client->dev,
918c2ecf20Sopenharmony_ci			       PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic struct tps6105x_platform_data *tps6105x_parse_dt(struct device *dev)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
978c2ecf20Sopenharmony_ci	struct tps6105x_platform_data *pdata;
988c2ecf20Sopenharmony_ci	struct device_node *child;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (!np)
1018c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1028c2ecf20Sopenharmony_ci	if (of_get_available_child_count(np) > 1) {
1038c2ecf20Sopenharmony_ci		dev_err(dev, "cannot support multiple operational modes");
1048c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1078c2ecf20Sopenharmony_ci	if (!pdata)
1088c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1098c2ecf20Sopenharmony_ci	pdata->mode = TPS6105X_MODE_SHUTDOWN;
1108c2ecf20Sopenharmony_ci	for_each_available_child_of_node(np, child) {
1118c2ecf20Sopenharmony_ci		if (child->name && !of_node_cmp(child->name, "regulator"))
1128c2ecf20Sopenharmony_ci			pdata->mode = TPS6105X_MODE_VOLTAGE;
1138c2ecf20Sopenharmony_ci		else if (child->name && !of_node_cmp(child->name, "led"))
1148c2ecf20Sopenharmony_ci			pdata->mode = TPS6105X_MODE_TORCH;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	return pdata;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int tps6105x_probe(struct i2c_client *client,
1218c2ecf20Sopenharmony_ci			const struct i2c_device_id *id)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct tps6105x			*tps6105x;
1248c2ecf20Sopenharmony_ci	struct tps6105x_platform_data	*pdata;
1258c2ecf20Sopenharmony_ci	int ret;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	pdata = dev_get_platdata(&client->dev);
1288c2ecf20Sopenharmony_ci	if (!pdata)
1298c2ecf20Sopenharmony_ci		pdata = tps6105x_parse_dt(&client->dev);
1308c2ecf20Sopenharmony_ci	if (IS_ERR(pdata)) {
1318c2ecf20Sopenharmony_ci		dev_err(&client->dev, "No platform data or DT found");
1328c2ecf20Sopenharmony_ci		return PTR_ERR(pdata);
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
1368c2ecf20Sopenharmony_ci	if (!tps6105x)
1378c2ecf20Sopenharmony_ci		return -ENOMEM;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
1408c2ecf20Sopenharmony_ci	if (IS_ERR(tps6105x->regmap))
1418c2ecf20Sopenharmony_ci		return PTR_ERR(tps6105x->regmap);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, tps6105x);
1448c2ecf20Sopenharmony_ci	tps6105x->client = client;
1458c2ecf20Sopenharmony_ci	tps6105x->pdata = pdata;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	ret = tps6105x_startup(tps6105x);
1488c2ecf20Sopenharmony_ci	if (ret) {
1498c2ecf20Sopenharmony_ci		dev_err(&client->dev, "chip initialization failed\n");
1508c2ecf20Sopenharmony_ci		return ret;
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
1548c2ecf20Sopenharmony_ci	if (ret)
1558c2ecf20Sopenharmony_ci		return ret;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	switch (pdata->mode) {
1588c2ecf20Sopenharmony_ci	case TPS6105X_MODE_SHUTDOWN:
1598c2ecf20Sopenharmony_ci		dev_info(&client->dev,
1608c2ecf20Sopenharmony_ci			 "present, not used for anything, only GPIO\n");
1618c2ecf20Sopenharmony_ci		break;
1628c2ecf20Sopenharmony_ci	case TPS6105X_MODE_TORCH:
1638c2ecf20Sopenharmony_ci		ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
1648c2ecf20Sopenharmony_ci		break;
1658c2ecf20Sopenharmony_ci	case TPS6105X_MODE_TORCH_FLASH:
1668c2ecf20Sopenharmony_ci		ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
1678c2ecf20Sopenharmony_ci		break;
1688c2ecf20Sopenharmony_ci	case TPS6105X_MODE_VOLTAGE:
1698c2ecf20Sopenharmony_ci		ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
1708c2ecf20Sopenharmony_ci		break;
1718c2ecf20Sopenharmony_ci	default:
1728c2ecf20Sopenharmony_ci		dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
1738c2ecf20Sopenharmony_ci		break;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (ret)
1778c2ecf20Sopenharmony_ci		mfd_remove_devices(&client->dev);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return ret;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic int tps6105x_remove(struct i2c_client *client)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	struct tps6105x *tps6105x = i2c_get_clientdata(client);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	mfd_remove_devices(&client->dev);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* Put chip in shutdown mode */
1898c2ecf20Sopenharmony_ci	regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
1908c2ecf20Sopenharmony_ci		TPS6105X_REG0_MODE_MASK,
1918c2ecf20Sopenharmony_ci		TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic const struct i2c_device_id tps6105x_id[] = {
1978c2ecf20Sopenharmony_ci	{ "tps61050", 0 },
1988c2ecf20Sopenharmony_ci	{ "tps61052", 0 },
1998c2ecf20Sopenharmony_ci	{ }
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tps6105x_id);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic const struct of_device_id tps6105x_of_match[] = {
2048c2ecf20Sopenharmony_ci	{ .compatible = "ti,tps61050" },
2058c2ecf20Sopenharmony_ci	{ .compatible = "ti,tps61052" },
2068c2ecf20Sopenharmony_ci	{ },
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tps6105x_of_match);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic struct i2c_driver tps6105x_driver = {
2118c2ecf20Sopenharmony_ci	.driver = {
2128c2ecf20Sopenharmony_ci		.name	= "tps6105x",
2138c2ecf20Sopenharmony_ci		.of_match_table = tps6105x_of_match,
2148c2ecf20Sopenharmony_ci	},
2158c2ecf20Sopenharmony_ci	.probe		= tps6105x_probe,
2168c2ecf20Sopenharmony_ci	.remove		= tps6105x_remove,
2178c2ecf20Sopenharmony_ci	.id_table	= tps6105x_id,
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic int __init tps6105x_init(void)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	return i2c_add_driver(&tps6105x_driver);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_cisubsys_initcall(tps6105x_init);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic void __exit tps6105x_exit(void)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	i2c_del_driver(&tps6105x_driver);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_cimodule_exit(tps6105x_exit);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciMODULE_AUTHOR("Linus Walleij");
2338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
235