xref: /linux/drivers/mfd/lp87565.c (revision 26ba30221c03364d6ed9910be8da4c1fd871b07b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
4  *
5  * Author: Keerthy <j-keerthy@ti.com>
6  */
7 
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/interrupt.h>
11 #include <linux/mfd/core.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 
15 #include <linux/mfd/lp87565.h>
16 
17 static const struct regmap_config lp87565_regmap_config = {
18 	.reg_bits = 8,
19 	.val_bits = 8,
20 	.max_register = LP87565_REG_MAX,
21 };
22 
23 static const struct mfd_cell lp87565_cells[] = {
24 	{ .name = "lp87565-q1-regulator", },
25 	{ .name = "lp87565-q1-gpio", },
26 };
27 
28 static const struct of_device_id of_lp87565_match_table[] = {
29 	{ .compatible = "ti,lp87565", },
30 	{
31 		.compatible = "ti,lp87524-q1",
32 		.data = (void *)LP87565_DEVICE_TYPE_LP87524_Q1,
33 	},
34 	{
35 		.compatible = "ti,lp87565-q1",
36 		.data = (void *)LP87565_DEVICE_TYPE_LP87565_Q1,
37 	},
38 	{
39 		.compatible = "ti,lp87561-q1",
40 		.data = (void *)LP87565_DEVICE_TYPE_LP87561_Q1,
41 	},
42 	{}
43 };
44 MODULE_DEVICE_TABLE(of, of_lp87565_match_table);
45 
46 static int lp87565_probe(struct i2c_client *client)
47 {
48 	struct lp87565 *lp87565;
49 	int ret;
50 	unsigned int otpid;
51 
52 	lp87565 = devm_kzalloc(&client->dev, sizeof(*lp87565), GFP_KERNEL);
53 	if (!lp87565)
54 		return -ENOMEM;
55 
56 	lp87565->dev = &client->dev;
57 
58 	lp87565->regmap = devm_regmap_init_i2c(client, &lp87565_regmap_config);
59 	if (IS_ERR(lp87565->regmap)) {
60 		ret = PTR_ERR(lp87565->regmap);
61 		dev_err(lp87565->dev,
62 			"Failed to initialize register map: %d\n", ret);
63 		return ret;
64 	}
65 
66 	lp87565->reset_gpio = devm_gpiod_get_optional(lp87565->dev, "reset",
67 						      GPIOD_OUT_LOW);
68 	if (IS_ERR(lp87565->reset_gpio)) {
69 		ret = PTR_ERR(lp87565->reset_gpio);
70 		if (ret == -EPROBE_DEFER)
71 			return ret;
72 	}
73 
74 	if (lp87565->reset_gpio) {
75 		gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
76 		/* The minimum assertion time is undocumented, just guess */
77 		usleep_range(2000, 4000);
78 
79 		gpiod_set_value_cansleep(lp87565->reset_gpio, 0);
80 		/* Min 1.2 ms before first I2C transaction */
81 		usleep_range(1500, 3000);
82 	}
83 
84 	ret = regmap_read(lp87565->regmap, LP87565_REG_OTP_REV, &otpid);
85 	if (ret) {
86 		dev_err(lp87565->dev, "Failed to read OTP ID\n");
87 		return ret;
88 	}
89 
90 	lp87565->rev = otpid & LP87565_OTP_REV_OTP_ID;
91 	lp87565->dev_type = (uintptr_t)i2c_get_match_data(client);
92 
93 	i2c_set_clientdata(client, lp87565);
94 
95 	return devm_mfd_add_devices(lp87565->dev, PLATFORM_DEVID_AUTO,
96 				    lp87565_cells, ARRAY_SIZE(lp87565_cells),
97 				    NULL, 0, NULL);
98 }
99 
100 static void lp87565_shutdown(struct i2c_client *client)
101 {
102 	struct lp87565 *lp87565 = i2c_get_clientdata(client);
103 
104 	gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
105 }
106 
107 static const struct i2c_device_id lp87565_id_table[] = {
108 	{ "lp87565-q1" },
109 	{ }
110 };
111 MODULE_DEVICE_TABLE(i2c, lp87565_id_table);
112 
113 static struct i2c_driver lp87565_driver = {
114 	.driver	= {
115 		.name	= "lp87565",
116 		.of_match_table = of_lp87565_match_table,
117 	},
118 	.probe = lp87565_probe,
119 	.shutdown = lp87565_shutdown,
120 	.id_table = lp87565_id_table,
121 };
122 module_i2c_driver(lp87565_driver);
123 
124 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
125 MODULE_DESCRIPTION("lp87565 chip family Multi-Function Device driver");
126 MODULE_LICENSE("GPL v2");
127