1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
4 * Copyright (C) 2009 - 2016 STMicroelectronics
7 #include <linux/module.h>
9 #include <linux/gpio.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/of_irq.h>
12 #include <linux/of_gpio.h>
13 #include <linux/acpi.h>
14 #include <linux/tpm.h>
15 #include <linux/platform_data/st33zp24.h>
20 #define TPM_DUMMY_BYTE 0xAA
22 struct st33zp24_i2c_phy {
23 struct i2c_client *client;
24 u8 buf[ST33ZP24_BUFSIZE + 1];
30 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
31 * @param: tpm_register, the tpm tis register where the data should be written
32 * @param: tpm_data, the tpm_data to write inside the tpm_register
33 * @param: tpm_size, The length of the data
34 * @return: Returns negative errno, or else the number of bytes written.
36 static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
38 struct st33zp24_i2c_phy *phy = phy_id;
40 phy->buf[0] = tpm_register;
41 memcpy(phy->buf + 1, tpm_data, tpm_size);
42 return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
47 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
48 * @param: tpm_register, the tpm tis register where the data should be read
49 * @param: tpm_data, the TPM response
50 * @param: tpm_size, tpm TPM response size to read.
51 * @return: number of byte read successfully: should be one if success.
53 static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
55 struct st33zp24_i2c_phy *phy = phy_id;
59 data = TPM_DUMMY_BYTE;
60 status = write8_reg(phy, tpm_register, &data, 1);
62 status = i2c_master_recv(phy->client, tpm_data, tpm_size);
68 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
69 * @param: phy_id, the phy description
70 * @param: tpm_register, the tpm tis register where the data should be written
71 * @param: tpm_data, the tpm_data to write inside the tpm_register
72 * @param: tpm_size, the length of the data
73 * @return: number of byte written successfully: should be one if success.
75 static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
78 return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
84 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
85 * @param: phy_id, the phy description
86 * @param: tpm_register, the tpm tis register where the data should be read
87 * @param: tpm_data, the TPM response
88 * @param: tpm_size, tpm TPM response size to read.
89 * @return: number of byte read successfully: should be one if success.
91 static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
94 return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
97 static const struct st33zp24_phy_ops i2c_phy_ops = {
98 .send = st33zp24_i2c_send,
99 .recv = st33zp24_i2c_recv,
102 static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
104 static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
105 { "lpcpd-gpios", &lpcpd_gpios, 1 },
109 static int st33zp24_i2c_acpi_request_resources(struct i2c_client *client)
111 struct tpm_chip *chip = i2c_get_clientdata(client);
112 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
113 struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
114 struct gpio_desc *gpiod_lpcpd;
115 struct device *dev = &client->dev;
118 ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
122 /* Get LPCPD GPIO from ACPI */
123 gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
124 if (IS_ERR(gpiod_lpcpd)) {
125 dev_err(&client->dev,
126 "Failed to retrieve lpcpd-gpios from acpi.\n");
129 * lpcpd pin is not specified. This is not an issue as
130 * power management can be also managed by TPM specific
131 * commands. So leave with a success status code.
136 phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
141 static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
143 struct tpm_chip *chip = i2c_get_clientdata(client);
144 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
145 struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
146 struct device_node *pp;
150 pp = client->dev.of_node;
152 dev_err(&client->dev, "No platform data\n");
156 /* Get GPIO from device tree */
157 gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
159 dev_err(&client->dev,
160 "Failed to retrieve lpcpd-gpios from dts.\n");
163 * lpcpd pin is not specified. This is not an issue as
164 * power management can be also managed by TPM specific
165 * commands. So leave with a success status code.
169 /* GPIO request and configuration */
170 ret = devm_gpio_request_one(&client->dev, gpio,
171 GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
173 dev_err(&client->dev, "Failed to request lpcpd pin\n");
176 phy->io_lpcpd = gpio;
181 static int st33zp24_i2c_request_resources(struct i2c_client *client)
183 struct tpm_chip *chip = i2c_get_clientdata(client);
184 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
185 struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
186 struct st33zp24_platform_data *pdata;
189 pdata = client->dev.platform_data;
191 dev_err(&client->dev, "No platform data\n");
195 /* store for late use */
196 phy->io_lpcpd = pdata->io_lpcpd;
198 if (gpio_is_valid(pdata->io_lpcpd)) {
199 ret = devm_gpio_request_one(&client->dev,
200 pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
203 dev_err(&client->dev, "Failed to request lpcpd pin\n");
212 * st33zp24_i2c_probe initialize the TPM device
213 * @param: client, the i2c_client description (TPM I2C description).
214 * @param: id, the i2c_device_id struct.
215 * @return: 0 in case of success.
218 static int st33zp24_i2c_probe(struct i2c_client *client,
219 const struct i2c_device_id *id)
222 struct st33zp24_platform_data *pdata;
223 struct st33zp24_i2c_phy *phy;
226 pr_info("%s: i2c client is NULL. Device not accessible.\n",
231 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
232 dev_info(&client->dev, "client not i2c capable\n");
236 phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
241 phy->client = client;
243 pdata = client->dev.platform_data;
244 if (!pdata && client->dev.of_node) {
245 ret = st33zp24_i2c_of_request_resources(client);
249 ret = st33zp24_i2c_request_resources(client);
252 } else if (ACPI_HANDLE(&client->dev)) {
253 ret = st33zp24_i2c_acpi_request_resources(client);
258 return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
263 * st33zp24_i2c_remove remove the TPM device
264 * @param: client, the i2c_client description (TPM I2C description).
265 * @return: 0 in case of success.
267 static void st33zp24_i2c_remove(struct i2c_client *client)
269 struct tpm_chip *chip = i2c_get_clientdata(client);
271 st33zp24_remove(chip);
274 static const struct i2c_device_id st33zp24_i2c_id[] = {
278 MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
280 static const struct of_device_id of_st33zp24_i2c_match[] = {
281 { .compatible = "st,st33zp24-i2c", },
284 MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
286 static const struct acpi_device_id st33zp24_i2c_acpi_match[] = {
290 MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
292 static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
295 static struct i2c_driver st33zp24_i2c_driver = {
297 .name = TPM_ST33_I2C,
298 .pm = &st33zp24_i2c_ops,
299 .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
300 .acpi_match_table = ACPI_PTR(st33zp24_i2c_acpi_match),
302 .probe = st33zp24_i2c_probe,
303 .remove = st33zp24_i2c_remove,
304 .id_table = st33zp24_i2c_id
307 module_i2c_driver(st33zp24_i2c_driver);
310 MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
311 MODULE_VERSION("1.3.0");
312 MODULE_LICENSE("GPL");