xref: /openbmc/linux/drivers/char/tpm/st33zp24/i2c.c (revision 82e6fdd6)
1 /*
2  * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
3  * Copyright (C) 2009 - 2016 STMicroelectronics
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_gpio.h>
25 #include <linux/acpi.h>
26 #include <linux/tpm.h>
27 #include <linux/platform_data/st33zp24.h>
28 
29 #include "../tpm.h"
30 #include "st33zp24.h"
31 
32 #define TPM_DUMMY_BYTE			0xAA
33 
34 struct st33zp24_i2c_phy {
35 	struct i2c_client *client;
36 	u8 buf[TPM_BUFSIZE + 1];
37 	int io_lpcpd;
38 };
39 
40 /*
41  * write8_reg
42  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
43  * @param: tpm_register, the tpm tis register where the data should be written
44  * @param: tpm_data, the tpm_data to write inside the tpm_register
45  * @param: tpm_size, The length of the data
46  * @return: Returns negative errno, or else the number of bytes written.
47  */
48 static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
49 {
50 	struct st33zp24_i2c_phy *phy = phy_id;
51 
52 	phy->buf[0] = tpm_register;
53 	memcpy(phy->buf + 1, tpm_data, tpm_size);
54 	return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
55 } /* write8_reg() */
56 
57 /*
58  * read8_reg
59  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
60  * @param: tpm_register, the tpm tis register where the data should be read
61  * @param: tpm_data, the TPM response
62  * @param: tpm_size, tpm TPM response size to read.
63  * @return: number of byte read successfully: should be one if success.
64  */
65 static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
66 {
67 	struct st33zp24_i2c_phy *phy = phy_id;
68 	u8 status = 0;
69 	u8 data;
70 
71 	data = TPM_DUMMY_BYTE;
72 	status = write8_reg(phy, tpm_register, &data, 1);
73 	if (status == 2)
74 		status = i2c_master_recv(phy->client, tpm_data, tpm_size);
75 	return status;
76 } /* read8_reg() */
77 
78 /*
79  * st33zp24_i2c_send
80  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
81  * @param: phy_id, the phy description
82  * @param: tpm_register, the tpm tis register where the data should be written
83  * @param: tpm_data, the tpm_data to write inside the tpm_register
84  * @param: tpm_size, the length of the data
85  * @return: number of byte written successfully: should be one if success.
86  */
87 static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
88 			     int tpm_size)
89 {
90 	return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
91 			  tpm_size);
92 }
93 
94 /*
95  * st33zp24_i2c_recv
96  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
97  * @param: phy_id, the phy description
98  * @param: tpm_register, the tpm tis register where the data should be read
99  * @param: tpm_data, the TPM response
100  * @param: tpm_size, tpm TPM response size to read.
101  * @return: number of byte read successfully: should be one if success.
102  */
103 static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
104 			     int tpm_size)
105 {
106 	return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
107 }
108 
109 static const struct st33zp24_phy_ops i2c_phy_ops = {
110 	.send = st33zp24_i2c_send,
111 	.recv = st33zp24_i2c_recv,
112 };
113 
114 static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
115 
116 static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
117 	{ "lpcpd-gpios", &lpcpd_gpios, 1 },
118 	{},
119 };
120 
121 static int st33zp24_i2c_acpi_request_resources(struct i2c_client *client)
122 {
123 	struct tpm_chip *chip = i2c_get_clientdata(client);
124 	struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
125 	struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
126 	struct gpio_desc *gpiod_lpcpd;
127 	struct device *dev = &client->dev;
128 	int ret;
129 
130 	ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
131 	if (ret)
132 		return ret;
133 
134 	/* Get LPCPD GPIO from ACPI */
135 	gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
136 	if (IS_ERR(gpiod_lpcpd)) {
137 		dev_err(&client->dev,
138 			"Failed to retrieve lpcpd-gpios from acpi.\n");
139 		phy->io_lpcpd = -1;
140 		/*
141 		 * lpcpd pin is not specified. This is not an issue as
142 		 * power management can be also managed by TPM specific
143 		 * commands. So leave with a success status code.
144 		 */
145 		return 0;
146 	}
147 
148 	phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
149 
150 	return 0;
151 }
152 
153 static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
154 {
155 	struct tpm_chip *chip = i2c_get_clientdata(client);
156 	struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
157 	struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
158 	struct device_node *pp;
159 	int gpio;
160 	int ret;
161 
162 	pp = client->dev.of_node;
163 	if (!pp) {
164 		dev_err(&client->dev, "No platform data\n");
165 		return -ENODEV;
166 	}
167 
168 	/* Get GPIO from device tree */
169 	gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
170 	if (gpio < 0) {
171 		dev_err(&client->dev,
172 			"Failed to retrieve lpcpd-gpios from dts.\n");
173 		phy->io_lpcpd = -1;
174 		/*
175 		 * lpcpd pin is not specified. This is not an issue as
176 		 * power management can be also managed by TPM specific
177 		 * commands. So leave with a success status code.
178 		 */
179 		return 0;
180 	}
181 	/* GPIO request and configuration */
182 	ret = devm_gpio_request_one(&client->dev, gpio,
183 			GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
184 	if (ret) {
185 		dev_err(&client->dev, "Failed to request lpcpd pin\n");
186 		return -ENODEV;
187 	}
188 	phy->io_lpcpd = gpio;
189 
190 	return 0;
191 }
192 
193 static int st33zp24_i2c_request_resources(struct i2c_client *client)
194 {
195 	struct tpm_chip *chip = i2c_get_clientdata(client);
196 	struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
197 	struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
198 	struct st33zp24_platform_data *pdata;
199 	int ret;
200 
201 	pdata = client->dev.platform_data;
202 	if (!pdata) {
203 		dev_err(&client->dev, "No platform data\n");
204 		return -ENODEV;
205 	}
206 
207 	/* store for late use */
208 	phy->io_lpcpd = pdata->io_lpcpd;
209 
210 	if (gpio_is_valid(pdata->io_lpcpd)) {
211 		ret = devm_gpio_request_one(&client->dev,
212 				pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
213 				"TPM IO_LPCPD");
214 		if (ret) {
215 			dev_err(&client->dev, "Failed to request lpcpd pin\n");
216 			return ret;
217 		}
218 	}
219 
220 	return 0;
221 }
222 
223 /*
224  * st33zp24_i2c_probe initialize the TPM device
225  * @param: client, the i2c_client drescription (TPM I2C description).
226  * @param: id, the i2c_device_id struct.
227  * @return: 0 in case of success.
228  *	 -1 in other case.
229  */
230 static int st33zp24_i2c_probe(struct i2c_client *client,
231 			      const struct i2c_device_id *id)
232 {
233 	int ret;
234 	struct st33zp24_platform_data *pdata;
235 	struct st33zp24_i2c_phy *phy;
236 
237 	if (!client) {
238 		pr_info("%s: i2c client is NULL. Device not accessible.\n",
239 			__func__);
240 		return -ENODEV;
241 	}
242 
243 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
244 		dev_info(&client->dev, "client not i2c capable\n");
245 		return -ENODEV;
246 	}
247 
248 	phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
249 			   GFP_KERNEL);
250 	if (!phy)
251 		return -ENOMEM;
252 
253 	phy->client = client;
254 
255 	pdata = client->dev.platform_data;
256 	if (!pdata && client->dev.of_node) {
257 		ret = st33zp24_i2c_of_request_resources(client);
258 		if (ret)
259 			return ret;
260 	} else if (pdata) {
261 		ret = st33zp24_i2c_request_resources(client);
262 		if (ret)
263 			return ret;
264 	} else if (ACPI_HANDLE(&client->dev)) {
265 		ret = st33zp24_i2c_acpi_request_resources(client);
266 		if (ret)
267 			return ret;
268 	}
269 
270 	return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
271 			      phy->io_lpcpd);
272 }
273 
274 /*
275  * st33zp24_i2c_remove remove the TPM device
276  * @param: client, the i2c_client description (TPM I2C description).
277  * @return: 0 in case of success.
278  */
279 static int st33zp24_i2c_remove(struct i2c_client *client)
280 {
281 	struct tpm_chip *chip = i2c_get_clientdata(client);
282 	int ret;
283 
284 	ret = st33zp24_remove(chip);
285 	if (ret)
286 		return ret;
287 
288 	return 0;
289 }
290 
291 static const struct i2c_device_id st33zp24_i2c_id[] = {
292 	{TPM_ST33_I2C, 0},
293 	{}
294 };
295 MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
296 
297 static const struct of_device_id of_st33zp24_i2c_match[] = {
298 	{ .compatible = "st,st33zp24-i2c", },
299 	{}
300 };
301 MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
302 
303 static const struct acpi_device_id st33zp24_i2c_acpi_match[] = {
304 	{"SMO3324"},
305 	{}
306 };
307 MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
308 
309 static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
310 			 st33zp24_pm_resume);
311 
312 static struct i2c_driver st33zp24_i2c_driver = {
313 	.driver = {
314 		.name = TPM_ST33_I2C,
315 		.pm = &st33zp24_i2c_ops,
316 		.of_match_table = of_match_ptr(of_st33zp24_i2c_match),
317 		.acpi_match_table = ACPI_PTR(st33zp24_i2c_acpi_match),
318 	},
319 	.probe = st33zp24_i2c_probe,
320 	.remove = st33zp24_i2c_remove,
321 	.id_table = st33zp24_i2c_id
322 };
323 
324 module_i2c_driver(st33zp24_i2c_driver);
325 
326 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
327 MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
328 MODULE_VERSION("1.3.0");
329 MODULE_LICENSE("GPL");
330