11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2bf38b871SChristophe Ricard /*
3bf38b871SChristophe Ricard * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
48bb273f2SChristophe RICARD * Copyright (C) 2009 - 2016 STMicroelectronics
5bf38b871SChristophe Ricard */
6bf38b871SChristophe Ricard
7bf38b871SChristophe Ricard #include <linux/module.h>
8bf38b871SChristophe Ricard #include <linux/i2c.h>
93f801909SDmitry Torokhov #include <linux/of.h>
1022eb90dbSChristophe RICARD #include <linux/acpi.h>
11bf38b871SChristophe Ricard #include <linux/tpm.h>
12bf38b871SChristophe Ricard
139e0d39d8SChristophe Ricard #include "../tpm.h"
14bf38b871SChristophe Ricard #include "st33zp24.h"
15bf38b871SChristophe Ricard
16bf38b871SChristophe Ricard #define TPM_DUMMY_BYTE 0xAA
17bf38b871SChristophe Ricard
18bf38b871SChristophe Ricard struct st33zp24_i2c_phy {
19bf38b871SChristophe Ricard struct i2c_client *client;
208ab547a2SJarkko Sakkinen u8 buf[ST33ZP24_BUFSIZE + 1];
21bf38b871SChristophe Ricard };
22bf38b871SChristophe Ricard
23bf38b871SChristophe Ricard /*
24bf38b871SChristophe Ricard * write8_reg
25bf38b871SChristophe Ricard * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
26bf38b871SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be written
27bf38b871SChristophe Ricard * @param: tpm_data, the tpm_data to write inside the tpm_register
28bf38b871SChristophe Ricard * @param: tpm_size, The length of the data
29bf38b871SChristophe Ricard * @return: Returns negative errno, or else the number of bytes written.
30bf38b871SChristophe Ricard */
write8_reg(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)31bf38b871SChristophe Ricard static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
32bf38b871SChristophe Ricard {
33bf38b871SChristophe Ricard struct st33zp24_i2c_phy *phy = phy_id;
34bf38b871SChristophe Ricard
35bf38b871SChristophe Ricard phy->buf[0] = tpm_register;
36bf38b871SChristophe Ricard memcpy(phy->buf + 1, tpm_data, tpm_size);
37bf38b871SChristophe Ricard return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
38bf38b871SChristophe Ricard } /* write8_reg() */
39bf38b871SChristophe Ricard
40bf38b871SChristophe Ricard /*
41bf38b871SChristophe Ricard * read8_reg
42bf38b871SChristophe Ricard * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
43bf38b871SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be read
44bf38b871SChristophe Ricard * @param: tpm_data, the TPM response
45bf38b871SChristophe Ricard * @param: tpm_size, tpm TPM response size to read.
46bf38b871SChristophe Ricard * @return: number of byte read successfully: should be one if success.
47bf38b871SChristophe Ricard */
read8_reg(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)48bf38b871SChristophe Ricard static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
49bf38b871SChristophe Ricard {
50bf38b871SChristophe Ricard struct st33zp24_i2c_phy *phy = phy_id;
51bf38b871SChristophe Ricard u8 status = 0;
52bf38b871SChristophe Ricard u8 data;
53bf38b871SChristophe Ricard
54bf38b871SChristophe Ricard data = TPM_DUMMY_BYTE;
55bf38b871SChristophe Ricard status = write8_reg(phy, tpm_register, &data, 1);
56bf38b871SChristophe Ricard if (status == 2)
57bf38b871SChristophe Ricard status = i2c_master_recv(phy->client, tpm_data, tpm_size);
58bf38b871SChristophe Ricard return status;
59bf38b871SChristophe Ricard } /* read8_reg() */
60bf38b871SChristophe Ricard
61bf38b871SChristophe Ricard /*
62bf38b871SChristophe Ricard * st33zp24_i2c_send
63bf38b871SChristophe Ricard * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
64bf38b871SChristophe Ricard * @param: phy_id, the phy description
65bf38b871SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be written
66bf38b871SChristophe Ricard * @param: tpm_data, the tpm_data to write inside the tpm_register
67bf38b871SChristophe Ricard * @param: tpm_size, the length of the data
68bf38b871SChristophe Ricard * @return: number of byte written successfully: should be one if success.
69bf38b871SChristophe Ricard */
st33zp24_i2c_send(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)70bf38b871SChristophe Ricard static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
71bf38b871SChristophe Ricard int tpm_size)
72bf38b871SChristophe Ricard {
73bf38b871SChristophe Ricard return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
74bf38b871SChristophe Ricard tpm_size);
75bf38b871SChristophe Ricard }
76bf38b871SChristophe Ricard
77bf38b871SChristophe Ricard /*
78bf38b871SChristophe Ricard * st33zp24_i2c_recv
79bf38b871SChristophe Ricard * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
80bf38b871SChristophe Ricard * @param: phy_id, the phy description
81bf38b871SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be read
82bf38b871SChristophe Ricard * @param: tpm_data, the TPM response
83bf38b871SChristophe Ricard * @param: tpm_size, tpm TPM response size to read.
84bf38b871SChristophe Ricard * @return: number of byte read successfully: should be one if success.
85bf38b871SChristophe Ricard */
st33zp24_i2c_recv(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)86bf38b871SChristophe Ricard static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
87bf38b871SChristophe Ricard int tpm_size)
88bf38b871SChristophe Ricard {
89bf38b871SChristophe Ricard return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
90bf38b871SChristophe Ricard }
91bf38b871SChristophe Ricard
92bf38b871SChristophe Ricard static const struct st33zp24_phy_ops i2c_phy_ops = {
93bf38b871SChristophe Ricard .send = st33zp24_i2c_send,
94bf38b871SChristophe Ricard .recv = st33zp24_i2c_recv,
95bf38b871SChristophe Ricard };
96bf38b871SChristophe Ricard
97bf38b871SChristophe Ricard /*
98bf38b871SChristophe Ricard * st33zp24_i2c_probe initialize the TPM device
9982efeb16SBinbin Zhou * @param: client, the i2c_client description (TPM I2C description).
100bf38b871SChristophe Ricard * @param: id, the i2c_device_id struct.
101bf38b871SChristophe Ricard * @return: 0 in case of success.
102bf38b871SChristophe Ricard * -1 in other case.
103bf38b871SChristophe Ricard */
st33zp24_i2c_probe(struct i2c_client * client)104376f88f4SUwe Kleine-König static int st33zp24_i2c_probe(struct i2c_client *client)
105bf38b871SChristophe Ricard {
106bf38b871SChristophe Ricard struct st33zp24_i2c_phy *phy;
107bf38b871SChristophe Ricard
108bf38b871SChristophe Ricard if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
109bf38b871SChristophe Ricard dev_info(&client->dev, "client not i2c capable\n");
110bf38b871SChristophe Ricard return -ENODEV;
111bf38b871SChristophe Ricard }
112bf38b871SChristophe Ricard
113bf38b871SChristophe Ricard phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
114bf38b871SChristophe Ricard GFP_KERNEL);
115bf38b871SChristophe Ricard if (!phy)
116bf38b871SChristophe Ricard return -ENOMEM;
117bf38b871SChristophe Ricard
118bf38b871SChristophe Ricard phy->client = client;
119740ec346SChristophe RICARD
1203f801909SDmitry Torokhov return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq);
121bf38b871SChristophe Ricard }
122bf38b871SChristophe Ricard
123bf38b871SChristophe Ricard /*
124bf38b871SChristophe Ricard * st33zp24_i2c_remove remove the TPM device
125bf38b871SChristophe Ricard * @param: client, the i2c_client description (TPM I2C description).
126bf38b871SChristophe Ricard * @return: 0 in case of success.
127bf38b871SChristophe Ricard */
st33zp24_i2c_remove(struct i2c_client * client)128ed5c2f5fSUwe Kleine-König static void st33zp24_i2c_remove(struct i2c_client *client)
129bf38b871SChristophe Ricard {
130bf38b871SChristophe Ricard struct tpm_chip *chip = i2c_get_clientdata(client);
131bf38b871SChristophe Ricard
132316f569dSUwe Kleine-König st33zp24_remove(chip);
133bf38b871SChristophe Ricard }
134bf38b871SChristophe Ricard
135bf38b871SChristophe Ricard static const struct i2c_device_id st33zp24_i2c_id[] = {
136bf38b871SChristophe Ricard {TPM_ST33_I2C, 0},
137bf38b871SChristophe Ricard {}
138bf38b871SChristophe Ricard };
139bf38b871SChristophe Ricard MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
140bf38b871SChristophe Ricard
141c3985d8bSKrzysztof Kozlowski static const struct of_device_id of_st33zp24_i2c_match[] __maybe_unused = {
142bf38b871SChristophe Ricard { .compatible = "st,st33zp24-i2c", },
143bf38b871SChristophe Ricard {}
144bf38b871SChristophe Ricard };
145bf38b871SChristophe Ricard MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
146bf38b871SChristophe Ricard
147c3985d8bSKrzysztof Kozlowski static const struct acpi_device_id st33zp24_i2c_acpi_match[] __maybe_unused = {
14822eb90dbSChristophe RICARD {"SMO3324"},
14922eb90dbSChristophe RICARD {}
15022eb90dbSChristophe RICARD };
15122eb90dbSChristophe RICARD MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
15222eb90dbSChristophe RICARD
153bf38b871SChristophe Ricard static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
154bf38b871SChristophe Ricard st33zp24_pm_resume);
155bf38b871SChristophe Ricard
156bf38b871SChristophe Ricard static struct i2c_driver st33zp24_i2c_driver = {
157bf38b871SChristophe Ricard .driver = {
158bf38b871SChristophe Ricard .name = TPM_ST33_I2C,
159bf38b871SChristophe Ricard .pm = &st33zp24_i2c_ops,
160bf38b871SChristophe Ricard .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
16122eb90dbSChristophe RICARD .acpi_match_table = ACPI_PTR(st33zp24_i2c_acpi_match),
162bf38b871SChristophe Ricard },
163*be6f48a7SUwe Kleine-König .probe = st33zp24_i2c_probe,
164bf38b871SChristophe Ricard .remove = st33zp24_i2c_remove,
165bf38b871SChristophe Ricard .id_table = st33zp24_i2c_id
166bf38b871SChristophe Ricard };
167bf38b871SChristophe Ricard
168bf38b871SChristophe Ricard module_i2c_driver(st33zp24_i2c_driver);
169bf38b871SChristophe Ricard
170bf38b871SChristophe Ricard MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
171bf38b871SChristophe Ricard MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
172bf38b871SChristophe Ricard MODULE_VERSION("1.3.0");
173bf38b871SChristophe Ricard MODULE_LICENSE("GPL");
174