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>
90db434f5SRob Herring #include <linux/i2c.h>
101e349600SKeerthy #include <linux/interrupt.h>
111e349600SKeerthy #include <linux/mfd/core.h>
120db434f5SRob Herring #include <linux/mod_devicetable.h>
131e349600SKeerthy #include <linux/module.h>
141e349600SKeerthy #include <linux/regmap.h>
151e349600SKeerthy
161e349600SKeerthy #include <linux/mfd/lp87565.h>
171e349600SKeerthy
181e349600SKeerthy static const struct regmap_config lp87565_regmap_config = {
191e349600SKeerthy .reg_bits = 8,
201e349600SKeerthy .val_bits = 8,
211e349600SKeerthy .max_register = LP87565_REG_MAX,
221e349600SKeerthy };
231e349600SKeerthy
241e349600SKeerthy static const struct mfd_cell lp87565_cells[] = {
251e349600SKeerthy { .name = "lp87565-q1-regulator", },
261e349600SKeerthy { .name = "lp87565-q1-gpio", },
271e349600SKeerthy };
281e349600SKeerthy
291e349600SKeerthy static const struct of_device_id of_lp87565_match_table[] = {
301e349600SKeerthy { .compatible = "ti,lp87565", },
311e349600SKeerthy {
324b6ec08fSLuca Ceresoli .compatible = "ti,lp87524-q1",
334b6ec08fSLuca Ceresoli .data = (void *)LP87565_DEVICE_TYPE_LP87524_Q1,
344b6ec08fSLuca Ceresoli },
354b6ec08fSLuca Ceresoli {
361e349600SKeerthy .compatible = "ti,lp87565-q1",
371e349600SKeerthy .data = (void *)LP87565_DEVICE_TYPE_LP87565_Q1,
381e349600SKeerthy },
39013e868bSKeerthy {
40013e868bSKeerthy .compatible = "ti,lp87561-q1",
41013e868bSKeerthy .data = (void *)LP87565_DEVICE_TYPE_LP87561_Q1,
42013e868bSKeerthy },
431e349600SKeerthy {}
441e349600SKeerthy };
451e349600SKeerthy MODULE_DEVICE_TABLE(of, of_lp87565_match_table);
461e349600SKeerthy
lp87565_probe(struct i2c_client * client)477ec69cc2SUwe Kleine-König static int lp87565_probe(struct i2c_client *client)
481e349600SKeerthy {
491e349600SKeerthy struct lp87565 *lp87565;
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;
920db434f5SRob Herring lp87565->dev_type = (uintptr_t)i2c_get_match_data(client);
931e349600SKeerthy
941e349600SKeerthy i2c_set_clientdata(client, lp87565);
951e349600SKeerthy
96ea3993a9SAxel Lin return devm_mfd_add_devices(lp87565->dev, PLATFORM_DEVID_AUTO,
97ea3993a9SAxel Lin lp87565_cells, ARRAY_SIZE(lp87565_cells),
98ea3993a9SAxel Lin NULL, 0, NULL);
991e349600SKeerthy }
1001e349600SKeerthy
lp87565_shutdown(struct i2c_client * client)10150e4d7a2SLuca Ceresoli static void lp87565_shutdown(struct i2c_client *client)
10250e4d7a2SLuca Ceresoli {
10350e4d7a2SLuca Ceresoli struct lp87565 *lp87565 = i2c_get_clientdata(client);
10450e4d7a2SLuca Ceresoli
10550e4d7a2SLuca Ceresoli gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
10650e4d7a2SLuca Ceresoli }
10750e4d7a2SLuca Ceresoli
1081e349600SKeerthy static const struct i2c_device_id lp87565_id_table[] = {
109*5e9ea43cSUwe Kleine-König { "lp87565-q1" },
110*5e9ea43cSUwe Kleine-König { }
1111e349600SKeerthy };
1121e349600SKeerthy MODULE_DEVICE_TABLE(i2c, lp87565_id_table);
1131e349600SKeerthy
1141e349600SKeerthy static struct i2c_driver lp87565_driver = {
1151e349600SKeerthy .driver = {
1161e349600SKeerthy .name = "lp87565",
1171e349600SKeerthy .of_match_table = of_lp87565_match_table,
1181e349600SKeerthy },
1199816d859SUwe Kleine-König .probe = lp87565_probe,
12050e4d7a2SLuca Ceresoli .shutdown = lp87565_shutdown,
1211e349600SKeerthy .id_table = lp87565_id_table,
1221e349600SKeerthy };
1231e349600SKeerthy module_i2c_driver(lp87565_driver);
1241e349600SKeerthy
1251e349600SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
1261e349600SKeerthy MODULE_DESCRIPTION("lp87565 chip family Multi-Function Device driver");
1271e349600SKeerthy MODULE_LICENSE("GPL v2");
128