xref: /linux/drivers/mfd/lp87565.c (revision 9816d859239f6d037fef168bdfa1ea1c12e3a76e)
1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e349600SKeerthy /*
34f4ed454SAlexander A. Klimov  * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
41e349600SKeerthy  *
51e349600SKeerthy  * Author: Keerthy <j-keerthy@ti.com>
61e349600SKeerthy  */
71e349600SKeerthy 
850e4d7a2SLuca Ceresoli #include <linux/gpio/consumer.h>
91e349600SKeerthy #include <linux/interrupt.h>
101e349600SKeerthy #include <linux/mfd/core.h>
111e349600SKeerthy #include <linux/module.h>
121e349600SKeerthy #include <linux/of_device.h>
131e349600SKeerthy #include <linux/regmap.h>
141e349600SKeerthy 
151e349600SKeerthy #include <linux/mfd/lp87565.h>
161e349600SKeerthy 
171e349600SKeerthy static const struct regmap_config lp87565_regmap_config = {
181e349600SKeerthy 	.reg_bits = 8,
191e349600SKeerthy 	.val_bits = 8,
201e349600SKeerthy 	.max_register = LP87565_REG_MAX,
211e349600SKeerthy };
221e349600SKeerthy 
231e349600SKeerthy static const struct mfd_cell lp87565_cells[] = {
241e349600SKeerthy 	{ .name = "lp87565-q1-regulator", },
251e349600SKeerthy 	{ .name = "lp87565-q1-gpio", },
261e349600SKeerthy };
271e349600SKeerthy 
281e349600SKeerthy static const struct of_device_id of_lp87565_match_table[] = {
291e349600SKeerthy 	{ .compatible = "ti,lp87565", },
301e349600SKeerthy 	{
314b6ec08fSLuca Ceresoli 		.compatible = "ti,lp87524-q1",
324b6ec08fSLuca Ceresoli 		.data = (void *)LP87565_DEVICE_TYPE_LP87524_Q1,
334b6ec08fSLuca Ceresoli 	},
344b6ec08fSLuca Ceresoli 	{
351e349600SKeerthy 		.compatible = "ti,lp87565-q1",
361e349600SKeerthy 		.data = (void *)LP87565_DEVICE_TYPE_LP87565_Q1,
371e349600SKeerthy 	},
38013e868bSKeerthy 	{
39013e868bSKeerthy 		.compatible = "ti,lp87561-q1",
40013e868bSKeerthy 		.data = (void *)LP87565_DEVICE_TYPE_LP87561_Q1,
41013e868bSKeerthy 	},
421e349600SKeerthy 	{}
431e349600SKeerthy };
441e349600SKeerthy MODULE_DEVICE_TABLE(of, of_lp87565_match_table);
451e349600SKeerthy 
467ec69cc2SUwe Kleine-König static int lp87565_probe(struct i2c_client *client)
471e349600SKeerthy {
481e349600SKeerthy 	struct lp87565 *lp87565;
491e349600SKeerthy 	const struct of_device_id *of_id;
501e349600SKeerthy 	int ret;
511e349600SKeerthy 	unsigned int otpid;
521e349600SKeerthy 
531e349600SKeerthy 	lp87565 = devm_kzalloc(&client->dev, sizeof(*lp87565), GFP_KERNEL);
541e349600SKeerthy 	if (!lp87565)
551e349600SKeerthy 		return -ENOMEM;
561e349600SKeerthy 
571e349600SKeerthy 	lp87565->dev = &client->dev;
581e349600SKeerthy 
591e349600SKeerthy 	lp87565->regmap = devm_regmap_init_i2c(client, &lp87565_regmap_config);
601e349600SKeerthy 	if (IS_ERR(lp87565->regmap)) {
611e349600SKeerthy 		ret = PTR_ERR(lp87565->regmap);
621e349600SKeerthy 		dev_err(lp87565->dev,
631e349600SKeerthy 			"Failed to initialize register map: %d\n", ret);
641e349600SKeerthy 		return ret;
651e349600SKeerthy 	}
661e349600SKeerthy 
6750e4d7a2SLuca Ceresoli 	lp87565->reset_gpio = devm_gpiod_get_optional(lp87565->dev, "reset",
6850e4d7a2SLuca Ceresoli 						      GPIOD_OUT_LOW);
6950e4d7a2SLuca Ceresoli 	if (IS_ERR(lp87565->reset_gpio)) {
7050e4d7a2SLuca Ceresoli 		ret = PTR_ERR(lp87565->reset_gpio);
7150e4d7a2SLuca Ceresoli 		if (ret == -EPROBE_DEFER)
7250e4d7a2SLuca Ceresoli 			return ret;
7350e4d7a2SLuca Ceresoli 	}
7450e4d7a2SLuca Ceresoli 
7550e4d7a2SLuca Ceresoli 	if (lp87565->reset_gpio) {
7650e4d7a2SLuca Ceresoli 		gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
7750e4d7a2SLuca Ceresoli 		/* The minimum assertion time is undocumented, just guess */
7850e4d7a2SLuca Ceresoli 		usleep_range(2000, 4000);
7950e4d7a2SLuca Ceresoli 
8050e4d7a2SLuca Ceresoli 		gpiod_set_value_cansleep(lp87565->reset_gpio, 0);
8150e4d7a2SLuca Ceresoli 		/* Min 1.2 ms before first I2C transaction */
8250e4d7a2SLuca Ceresoli 		usleep_range(1500, 3000);
8350e4d7a2SLuca Ceresoli 	}
8450e4d7a2SLuca Ceresoli 
851e349600SKeerthy 	ret = regmap_read(lp87565->regmap, LP87565_REG_OTP_REV, &otpid);
861e349600SKeerthy 	if (ret) {
871e349600SKeerthy 		dev_err(lp87565->dev, "Failed to read OTP ID\n");
881e349600SKeerthy 		return ret;
891e349600SKeerthy 	}
901e349600SKeerthy 
911e349600SKeerthy 	lp87565->rev = otpid & LP87565_OTP_REV_OTP_ID;
921e349600SKeerthy 
931e349600SKeerthy 	of_id = of_match_device(of_lp87565_match_table, &client->dev);
941e349600SKeerthy 	if (of_id)
951e349600SKeerthy 		lp87565->dev_type = (enum lp87565_device_type)of_id->data;
961e349600SKeerthy 
971e349600SKeerthy 	i2c_set_clientdata(client, lp87565);
981e349600SKeerthy 
99ea3993a9SAxel Lin 	return devm_mfd_add_devices(lp87565->dev, PLATFORM_DEVID_AUTO,
100ea3993a9SAxel Lin 				    lp87565_cells, ARRAY_SIZE(lp87565_cells),
101ea3993a9SAxel Lin 				    NULL, 0, NULL);
1021e349600SKeerthy }
1031e349600SKeerthy 
10450e4d7a2SLuca Ceresoli static void lp87565_shutdown(struct i2c_client *client)
10550e4d7a2SLuca Ceresoli {
10650e4d7a2SLuca Ceresoli 	struct lp87565 *lp87565 = i2c_get_clientdata(client);
10750e4d7a2SLuca Ceresoli 
10850e4d7a2SLuca Ceresoli 	gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
10950e4d7a2SLuca Ceresoli }
11050e4d7a2SLuca Ceresoli 
1111e349600SKeerthy static const struct i2c_device_id lp87565_id_table[] = {
1121e349600SKeerthy 	{ "lp87565-q1", 0 },
1131e349600SKeerthy 	{ },
1141e349600SKeerthy };
1151e349600SKeerthy MODULE_DEVICE_TABLE(i2c, lp87565_id_table);
1161e349600SKeerthy 
1171e349600SKeerthy static struct i2c_driver lp87565_driver = {
1181e349600SKeerthy 	.driver	= {
1191e349600SKeerthy 		.name	= "lp87565",
1201e349600SKeerthy 		.of_match_table = of_lp87565_match_table,
1211e349600SKeerthy 	},
122*9816d859SUwe Kleine-König 	.probe = lp87565_probe,
12350e4d7a2SLuca Ceresoli 	.shutdown = lp87565_shutdown,
1241e349600SKeerthy 	.id_table = lp87565_id_table,
1251e349600SKeerthy };
1261e349600SKeerthy module_i2c_driver(lp87565_driver);
1271e349600SKeerthy 
1281e349600SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
1291e349600SKeerthy MODULE_DESCRIPTION("lp87565 chip family Multi-Function Device driver");
1301e349600SKeerthy MODULE_LICENSE("GPL v2");
131