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