blob: a52ab76febb3492cff5c2b6e7ea22c08c17c7cac [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001// SPDX-License-Identifier: GPL-2.0-only
Keerthy1e349602017-06-13 10:28:40 +05302/*
Alexander A. Klimov4f4ed4542020-07-22 21:24:54 +02003 * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
Keerthy1e349602017-06-13 10:28:40 +05304 *
5 * Author: Keerthy <j-keerthy@ti.com>
Keerthy1e349602017-06-13 10:28:40 +05306 */
7
Luca Ceresoli50e4d7a2021-02-26 15:28:52 +01008#include <linux/gpio/consumer.h>
Keerthy1e349602017-06-13 10:28:40 +05309#include <linux/interrupt.h>
10#include <linux/mfd/core.h>
11#include <linux/module.h>
12#include <linux/of_device.h>
13#include <linux/regmap.h>
14
15#include <linux/mfd/lp87565.h>
16
17static const struct regmap_config lp87565_regmap_config = {
18 .reg_bits = 8,
19 .val_bits = 8,
20 .max_register = LP87565_REG_MAX,
21};
22
23static const struct mfd_cell lp87565_cells[] = {
24 { .name = "lp87565-q1-regulator", },
25 { .name = "lp87565-q1-gpio", },
26};
27
28static const struct of_device_id of_lp87565_match_table[] = {
29 { .compatible = "ti,lp87565", },
30 {
Luca Ceresoli4b6ec082020-09-02 16:22:59 +020031 .compatible = "ti,lp87524-q1",
32 .data = (void *)LP87565_DEVICE_TYPE_LP87524_Q1,
33 },
34 {
Keerthy1e349602017-06-13 10:28:40 +053035 .compatible = "ti,lp87565-q1",
36 .data = (void *)LP87565_DEVICE_TYPE_LP87565_Q1,
37 },
Keerthy013e8682019-05-15 15:38:47 +053038 {
39 .compatible = "ti,lp87561-q1",
40 .data = (void *)LP87565_DEVICE_TYPE_LP87561_Q1,
41 },
Keerthy1e349602017-06-13 10:28:40 +053042 {}
43};
44MODULE_DEVICE_TABLE(of, of_lp87565_match_table);
45
46static int lp87565_probe(struct i2c_client *client,
47 const struct i2c_device_id *ids)
48{
49 struct lp87565 *lp87565;
50 const struct of_device_id *of_id;
51 int ret;
52 unsigned int otpid;
53
54 lp87565 = devm_kzalloc(&client->dev, sizeof(*lp87565), GFP_KERNEL);
55 if (!lp87565)
56 return -ENOMEM;
57
58 lp87565->dev = &client->dev;
59
60 lp87565->regmap = devm_regmap_init_i2c(client, &lp87565_regmap_config);
61 if (IS_ERR(lp87565->regmap)) {
62 ret = PTR_ERR(lp87565->regmap);
63 dev_err(lp87565->dev,
64 "Failed to initialize register map: %d\n", ret);
65 return ret;
66 }
67
Luca Ceresoli50e4d7a2021-02-26 15:28:52 +010068 lp87565->reset_gpio = devm_gpiod_get_optional(lp87565->dev, "reset",
69 GPIOD_OUT_LOW);
70 if (IS_ERR(lp87565->reset_gpio)) {
71 ret = PTR_ERR(lp87565->reset_gpio);
72 if (ret == -EPROBE_DEFER)
73 return ret;
74 }
75
76 if (lp87565->reset_gpio) {
77 gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
78 /* The minimum assertion time is undocumented, just guess */
79 usleep_range(2000, 4000);
80
81 gpiod_set_value_cansleep(lp87565->reset_gpio, 0);
82 /* Min 1.2 ms before first I2C transaction */
83 usleep_range(1500, 3000);
84 }
85
Keerthy1e349602017-06-13 10:28:40 +053086 ret = regmap_read(lp87565->regmap, LP87565_REG_OTP_REV, &otpid);
87 if (ret) {
88 dev_err(lp87565->dev, "Failed to read OTP ID\n");
89 return ret;
90 }
91
92 lp87565->rev = otpid & LP87565_OTP_REV_OTP_ID;
93
94 of_id = of_match_device(of_lp87565_match_table, &client->dev);
95 if (of_id)
96 lp87565->dev_type = (enum lp87565_device_type)of_id->data;
97
98 i2c_set_clientdata(client, lp87565);
99
Axel Linea3993a2017-07-30 18:58:15 +0800100 return devm_mfd_add_devices(lp87565->dev, PLATFORM_DEVID_AUTO,
101 lp87565_cells, ARRAY_SIZE(lp87565_cells),
102 NULL, 0, NULL);
Keerthy1e349602017-06-13 10:28:40 +0530103}
104
Luca Ceresoli50e4d7a2021-02-26 15:28:52 +0100105static void lp87565_shutdown(struct i2c_client *client)
106{
107 struct lp87565 *lp87565 = i2c_get_clientdata(client);
108
109 gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
110}
111
Keerthy1e349602017-06-13 10:28:40 +0530112static const struct i2c_device_id lp87565_id_table[] = {
113 { "lp87565-q1", 0 },
114 { },
115};
116MODULE_DEVICE_TABLE(i2c, lp87565_id_table);
117
118static struct i2c_driver lp87565_driver = {
119 .driver = {
120 .name = "lp87565",
121 .of_match_table = of_lp87565_match_table,
122 },
123 .probe = lp87565_probe,
Luca Ceresoli50e4d7a2021-02-26 15:28:52 +0100124 .shutdown = lp87565_shutdown,
Keerthy1e349602017-06-13 10:28:40 +0530125 .id_table = lp87565_id_table,
126};
127module_i2c_driver(lp87565_driver);
128
129MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
130MODULE_DESCRIPTION("lp87565 chip family Multi-Function Device driver");
131MODULE_LICENSE("GPL v2");