blob: 36524ed8ada9b0fdf5ab5a4ab98ae4348e3a5f25 [file] [log] [blame]
Mathias Leblanc251a7b02012-11-28 18:22:24 +01001/*
2 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009, 2010 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * STMicroelectronics version 1.2.0, Copyright (C) 2010
20 * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
21 * This is free software, and you are welcome to redistribute it
22 * under certain conditions.
23 *
24 * @Author: Christophe RICARD tpmsupport@st.com
25 *
26 * @File: tpm_stm_st33_i2c.c
27 *
28 * @Synopsis:
29 * 09/15/2010: First shot driver tpm_tis driver for
30 lpc is used as model.
31 */
32
Kent Yoder3d7a7bd2012-12-05 16:52:43 -060033#include <linux/pci.h>
34#include <linux/module.h>
35#include <linux/platform_device.h>
36#include <linux/i2c.h>
37#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/init.h>
43#include <linux/wait.h>
44#include <linux/string.h>
45#include <linux/interrupt.h>
46#include <linux/spinlock.h>
47#include <linux/sysfs.h>
48#include <linux/gpio.h>
49#include <linux/sched.h>
50#include <linux/uaccess.h>
51#include <linux/io.h>
52#include <linux/slab.h>
53#include <linux/sched.h>
Mathias Leblanc251a7b02012-11-28 18:22:24 +010054
Kent Yoder3d7a7bd2012-12-05 16:52:43 -060055#include "tpm.h"
Kent Yoder9da228e2013-01-28 07:52:44 -060056#include "tpm_i2c_stm_st33.h"
Mathias Leblanc251a7b02012-11-28 18:22:24 +010057
58enum stm33zp24_access {
59 TPM_ACCESS_VALID = 0x80,
60 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
61 TPM_ACCESS_REQUEST_PENDING = 0x04,
62 TPM_ACCESS_REQUEST_USE = 0x02,
63};
64
65enum stm33zp24_status {
66 TPM_STS_VALID = 0x80,
67 TPM_STS_COMMAND_READY = 0x40,
68 TPM_STS_GO = 0x20,
69 TPM_STS_DATA_AVAIL = 0x10,
70 TPM_STS_DATA_EXPECT = 0x08,
71};
72
73enum stm33zp24_int_flags {
74 TPM_GLOBAL_INT_ENABLE = 0x80,
75 TPM_INTF_CMD_READY_INT = 0x080,
76 TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
77 TPM_INTF_WAKE_UP_READY_INT = 0x020,
78 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
79 TPM_INTF_STS_VALID_INT = 0x002,
80 TPM_INTF_DATA_AVAIL_INT = 0x001,
81};
82
83enum tis_defaults {
84 TIS_SHORT_TIMEOUT = 750,
85 TIS_LONG_TIMEOUT = 2000,
86};
87
88/*
89 * write8_reg
90 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
91 * @param: tpm_register, the tpm tis register where the data should be written
92 * @param: tpm_data, the tpm_data to write inside the tpm_register
93 * @param: tpm_size, The length of the data
94 * @return: Returns negative errno, or else the number of bytes written.
95 */
96static int write8_reg(struct i2c_client *client, u8 tpm_register,
97 u8 *tpm_data, u16 tpm_size)
98{
99 u8 data;
100 int value = 0;
101 struct st33zp24_platform_data *pin_infos;
102
103 pin_infos = client->dev.platform_data;
104
105 data = tpm_register;
106 memcpy(pin_infos->tpm_i2c_buffer[0], &data, sizeof(data));
107 memcpy(pin_infos->tpm_i2c_buffer[0] + 1, tpm_data, tpm_size);
108 value = i2c_master_send(client, pin_infos->tpm_i2c_buffer[0],
109 tpm_size + 1);
110 return value;
111} /* write8_reg() */
112
113/*
114 * read8_reg
115 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
116 * @param: tpm_register, the tpm tis register where the data should be read
117 * @param: tpm_data, the TPM response
118 * @param: tpm_size, tpm TPM response size to read.
119 * @return: number of byte read successfully: should be one if success.
120 */
121static int read8_reg(struct i2c_client *client, u8 tpm_register,
122 u8 *tpm_data, int tpm_size)
123{
124 u8 status = 0;
125 u8 data;
126 struct st33zp24_platform_data *pin_infos;
127
128 pin_infos = client->dev.platform_data;
129
130 data = TPM_DUMMY_BYTE;
131 status = write8_reg(client, tpm_register, &data, 1);
132 if (status == 2)
133 status = i2c_master_recv(client, tpm_data, tpm_size);
134 return status;
135} /* read8_reg() */
136
137/*
138 * I2C_WRITE_DATA
139 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
140 * @param: client, the chip description
141 * @param: tpm_register, the tpm tis register where the data should be written
142 * @param: tpm_data, the tpm_data to write inside the tpm_register
143 * @param: tpm_size, The length of the data
144 * @return: number of byte written successfully: should be one if success.
145 */
146#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \
147 (write8_reg(client, tpm_register | \
148 TPM_WRITE_DIRECTION, tpm_data, tpm_size))
149
150/*
151 * I2C_READ_DATA
152 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
153 * @param: tpm, the chip description
154 * @param: tpm_register, the tpm tis register where the data should be read
155 * @param: tpm_data, the TPM response
156 * @param: tpm_size, tpm TPM response size to read.
157 * @return: number of byte read successfully: should be one if success.
158 */
159#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \
160 (read8_reg(client, tpm_register, tpm_data, tpm_size))
161
162/*
163 * clear_interruption
164 * clear the TPM interrupt register.
165 * @param: tpm, the chip description
166 */
167static void clear_interruption(struct i2c_client *client)
168{
169 u8 interrupt;
170 I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
171 I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1);
172 I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
173} /* clear_interruption() */
174
175/*
176 * _wait_for_interrupt_serirq_timeout
177 * @param: tpm, the chip description
178 * @param: timeout, the timeout of the interrupt
179 * @return: the status of the interruption.
180 */
181static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
182 unsigned long timeout)
183{
184 long status;
185 struct i2c_client *client;
186 struct st33zp24_platform_data *pin_infos;
187
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600188 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100189 pin_infos = client->dev.platform_data;
190
191 status = wait_for_completion_interruptible_timeout(
192 &pin_infos->irq_detection,
193 timeout);
194 if (status > 0)
195 enable_irq(gpio_to_irq(pin_infos->io_serirq));
196 gpio_direction_input(pin_infos->io_serirq);
197
198 return status;
199} /* wait_for_interrupt_serirq_timeout() */
200
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600201static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100202 unsigned long timeout)
203{
204 int status = 2;
205 struct i2c_client *client;
206 struct st33zp24_platform_data *pin_infos;
207
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600208 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100209 pin_infos = client->dev.platform_data;
210
211 status = _wait_for_interrupt_serirq_timeout(chip, timeout);
212 if (!status) {
213 status = -EBUSY;
214 } else{
215 clear_interruption(client);
216 if (condition)
217 status = 1;
218 }
219 return status;
220}
221
222/*
223 * tpm_stm_i2c_cancel, cancel is not implemented.
224 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
225 */
226static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
227{
228 struct i2c_client *client;
229 u8 data;
230
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600231 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100232
233 data = TPM_STS_COMMAND_READY;
234 I2C_WRITE_DATA(client, TPM_STS, &data, 1);
235 if (chip->vendor.irq)
236 wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
237} /* tpm_stm_i2c_cancel() */
238
239/*
240 * tpm_stm_spi_status return the TPM_STS register
241 * @param: chip, the tpm chip description
242 * @return: the TPM_STS register value.
243 */
244static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
245{
246 struct i2c_client *client;
247 u8 data;
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600248 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100249
250 I2C_READ_DATA(client, TPM_STS, &data, 1);
251 return data;
252} /* tpm_stm_i2c_status() */
253
254
255/*
256 * check_locality if the locality is active
257 * @param: chip, the tpm chip description
258 * @return: the active locality or -EACCESS.
259 */
260static int check_locality(struct tpm_chip *chip)
261{
262 struct i2c_client *client;
263 u8 data;
264 u8 status;
265
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600266 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100267
268 status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1);
269 if (status && (data &
270 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
271 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
272 return chip->vendor.locality;
273
274 return -EACCES;
275
276} /* check_locality() */
277
278/*
279 * request_locality request the TPM locality
280 * @param: chip, the chip description
281 * @return: the active locality or EACCESS.
282 */
283static int request_locality(struct tpm_chip *chip)
284{
285 unsigned long stop;
286 long rc;
287 struct i2c_client *client;
288 u8 data;
289
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600290 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100291
292 if (check_locality(chip) == chip->vendor.locality)
293 return chip->vendor.locality;
294
295 data = TPM_ACCESS_REQUEST_USE;
296 rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
297 if (rc < 0)
298 goto end;
299
300 if (chip->vendor.irq) {
301 rc = wait_for_serirq_timeout(chip, (check_locality
302 (chip) >= 0),
303 chip->vendor.timeout_a);
304 if (rc > 0)
305 return chip->vendor.locality;
306 } else{
307 stop = jiffies + chip->vendor.timeout_a;
308 do {
309 if (check_locality(chip) >= 0)
310 return chip->vendor.locality;
311 msleep(TPM_TIMEOUT);
312 } while (time_before(jiffies, stop));
313 }
314 rc = -EACCES;
315end:
316 return rc;
317} /* request_locality() */
318
319/*
320 * release_locality release the active locality
321 * @param: chip, the tpm chip description.
322 */
323static void release_locality(struct tpm_chip *chip)
324{
325 struct i2c_client *client;
326 u8 data;
327
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600328 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100329 data = TPM_ACCESS_ACTIVE_LOCALITY;
330
331 I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
332}
333
334/*
335 * get_burstcount return the burstcount address 0x19 0x1A
336 * @param: chip, the chip description
337 * return: the burstcount.
338 */
339static int get_burstcount(struct tpm_chip *chip)
340{
341 unsigned long stop;
342 int burstcnt, status;
343 u8 tpm_reg, temp;
344
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600345 struct i2c_client *client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100346
347 stop = jiffies + chip->vendor.timeout_d;
348 do {
349 tpm_reg = TPM_STS + 1;
350 status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
351 if (status < 0)
352 goto end;
353
354 tpm_reg = tpm_reg + 1;
355 burstcnt = temp;
356 status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
357 if (status < 0)
358 goto end;
359
360 burstcnt |= temp << 8;
361 if (burstcnt)
362 return burstcnt;
363 msleep(TPM_TIMEOUT);
364 } while (time_before(jiffies, stop));
365
366end:
367 return -EBUSY;
368} /* get_burstcount() */
369
370/*
371 * wait_for_stat wait for a TPM_STS value
372 * @param: chip, the tpm chip description
373 * @param: mask, the value mask to wait
374 * @param: timeout, the timeout
375 * @param: queue, the wait queue.
376 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
377 */
378static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
379 wait_queue_head_t *queue)
380{
381 unsigned long stop;
382 long rc;
383 u8 status;
384
385 if (chip->vendor.irq) {
386 rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status
387 (chip) & mask) ==
388 mask), timeout);
389 if (rc > 0)
390 return 0;
391 } else{
392 stop = jiffies + timeout;
393 do {
394 msleep(TPM_TIMEOUT);
395 status = tpm_stm_i2c_status(chip);
396 if ((status & mask) == mask)
397 return 0;
398 } while (time_before(jiffies, stop));
399 }
400 return -ETIME;
401} /* wait_for_stat() */
402
403/*
404 * recv_data receive data
405 * @param: chip, the tpm chip description
406 * @param: buf, the buffer where the data are received
407 * @param: count, the number of data to receive
408 * @return: the number of bytes read from TPM FIFO.
409 */
410static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
411{
412 int size = 0, burstcnt, len;
413 struct i2c_client *client;
414
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600415 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100416
417 while (size < count &&
418 wait_for_stat(chip,
419 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
420 chip->vendor.timeout_c,
421 &chip->vendor.read_queue)
422 == 0) {
423 burstcnt = get_burstcount(chip);
424 len = min_t(int, burstcnt, count - size);
425 I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len);
426 size += len;
427 }
428 return size;
429}
430
431/*
432 * tpm_ioserirq_handler the serirq irq handler
433 * @param: irq, the tpm chip description
434 * @param: dev_id, the description of the chip
435 * @return: the status of the handler.
436 */
437static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
438{
439 struct tpm_chip *chip = dev_id;
440 struct i2c_client *client;
441 struct st33zp24_platform_data *pin_infos;
442
443 disable_irq_nosync(irq);
444
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600445 client = (struct i2c_client *) TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100446 pin_infos = client->dev.platform_data;
447
448 complete(&pin_infos->irq_detection);
449 return IRQ_HANDLED;
450} /* tpm_ioserirq_handler() */
451
452
453/*
454 * tpm_stm_i2c_send send TPM commands through the I2C bus.
455 *
456 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
457 * @param: buf, the buffer to send.
458 * @param: count, the number of bytes to send.
459 * @return: In case of success the number of bytes sent.
460 * In other case, a < 0 value describing the issue.
461 */
462static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
463 size_t len)
464{
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600465 u32 ordinal,
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100466 status,
467 burstcnt = 0, i, size;
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600468 int ret;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100469 u8 data;
470 struct i2c_client *client;
471 struct st33zp24_platform_data *pin_infos;
472
473 if (chip == NULL)
474 return -EBUSY;
475 if (len < TPM_HEADER_SIZE)
476 return -EBUSY;
477
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600478 client = (struct i2c_client *)TPM_VPRIV(chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100479 pin_infos = client->dev.platform_data;
480
481 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
482
483 client->flags = 0;
484
485 ret = request_locality(chip);
486 if (ret < 0)
487 return ret;
488
489 status = tpm_stm_i2c_status(chip);
490 if ((status & TPM_STS_COMMAND_READY) == 0) {
491 tpm_stm_i2c_cancel(chip);
492 if (wait_for_stat
493 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
494 &chip->vendor.int_queue) < 0) {
495 ret = -ETIME;
496 goto out_err;
497 }
498 }
499
500 for (i = 0 ; i < len - 1 ;) {
501 burstcnt = get_burstcount(chip);
502 size = min_t(int, len - i - 1, burstcnt);
503 ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf, size);
504 if (ret < 0)
505 goto out_err;
506
507 i += size;
508 }
509
510 status = tpm_stm_i2c_status(chip);
511 if ((status & TPM_STS_DATA_EXPECT) == 0) {
512 ret = -EIO;
513 goto out_err;
514 }
515
516 ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1);
517 if (ret < 0)
518 goto out_err;
519
520 status = tpm_stm_i2c_status(chip);
521 if ((status & TPM_STS_DATA_EXPECT) != 0) {
522 ret = -EIO;
523 goto out_err;
524 }
525
526 data = TPM_STS_GO;
527 I2C_WRITE_DATA(client, TPM_STS, &data, 1);
528
529 return len;
530out_err:
531 tpm_stm_i2c_cancel(chip);
532 release_locality(chip);
533 return ret;
534}
535
536/*
537 * tpm_stm_i2c_recv received TPM response through the I2C bus.
538 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
539 * @param: buf, the buffer to store datas.
540 * @param: count, the number of bytes to send.
541 * @return: In case of success the number of bytes received.
542 * In other case, a < 0 value describing the issue.
543 */
544static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
545 size_t count)
546{
547 int size = 0;
548 int expected;
549
550 struct i2c_client *client;
551 struct st33zp24_platform_data *pin_infos;
552
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100553 if (chip == NULL)
554 return -EBUSY;
555
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600556 client = (struct i2c_client *)TPM_VPRIV(chip);
557 pin_infos = client->dev.platform_data;
558
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100559 if (count < TPM_HEADER_SIZE) {
560 size = -EIO;
561 goto out;
562 }
563
564 size = recv_data(chip, buf, TPM_HEADER_SIZE);
565 if (size < TPM_HEADER_SIZE) {
566 dev_err(chip->dev, "Unable to read header\n");
567 goto out;
568 }
569
570 expected = be32_to_cpu(*(__be32 *) (buf + 2));
571 if (expected > count) {
572 size = -EIO;
573 goto out;
574 }
575
576 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
577 expected - TPM_HEADER_SIZE);
578 if (size < expected) {
579 dev_err(chip->dev, "Unable to read remainder of result\n");
580 size = -ETIME;
581 goto out;
582 }
583
584out:
585 chip->vendor.cancel(chip);
586 release_locality(chip);
587 return size;
588}
589
590static const struct file_operations tpm_st33_i2c_fops = {
591 .owner = THIS_MODULE,
592 .llseek = no_llseek,
593 .read = tpm_read,
594 .write = tpm_write,
595 .open = tpm_open,
596 .release = tpm_release,
597};
598
599static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
600static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
601static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
602static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
603static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
604static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
605static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
606static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
607
608static struct attribute *stm_tpm_attrs[] = {
609 &dev_attr_pubek.attr,
610 &dev_attr_pcrs.attr,
611 &dev_attr_enabled.attr,
612 &dev_attr_active.attr,
613 &dev_attr_owned.attr,
614 &dev_attr_temp_deactivated.attr,
615 &dev_attr_caps.attr,
616 &dev_attr_cancel.attr, NULL,
617};
618
619static struct attribute_group stm_tpm_attr_grp = {
620 .attrs = stm_tpm_attrs
621};
622
623static struct tpm_vendor_specific st_i2c_tpm = {
624 .send = tpm_stm_i2c_send,
625 .recv = tpm_stm_i2c_recv,
626 .cancel = tpm_stm_i2c_cancel,
627 .status = tpm_stm_i2c_status,
628 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
629 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
630 .req_canceled = TPM_STS_COMMAND_READY,
631 .attr_group = &stm_tpm_attr_grp,
632 .miscdev = {.fops = &tpm_st33_i2c_fops,},
633};
634
635static int interrupts ;
636module_param(interrupts, int, 0444);
637MODULE_PARM_DESC(interrupts, "Enable interrupts");
638
639static int power_mgt = 1;
640module_param(power_mgt, int, 0444);
641MODULE_PARM_DESC(power_mgt, "Power Management");
642
643/*
644 * tpm_st33_i2c_probe initialize the TPM device
645 * @param: client, the i2c_client drescription (TPM I2C description).
646 * @param: id, the i2c_device_id struct.
647 * @return: 0 in case of success.
648 * -1 in other case.
649 */
650static int
651tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
652{
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600653 int err;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100654 u8 intmask;
655 struct tpm_chip *chip;
656 struct st33zp24_platform_data *platform_data;
657
658 err = 0;
659
660 if (client == NULL) {
Kent Yoder1fbc5e92013-01-18 17:42:25 -0600661 pr_info("%s: i2c client is NULL. Device not accessible.\n",
662 __func__);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100663 err = -ENODEV;
664 goto end;
665 }
666
667 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
668 dev_info(&client->dev, "client not i2c capable\n");
669 err = -ENODEV;
670 goto end;
671 }
672
673 chip = tpm_register_hardware(&client->dev, &st_i2c_tpm);
674 if (!chip) {
675 dev_info(&client->dev, "fail chip\n");
676 err = -ENODEV;
677 goto end;
678 }
679
680 platform_data = client->dev.platform_data;
Kent Yoder1fbc5e92013-01-18 17:42:25 -0600681
682 if (!platform_data) {
683 dev_info(&client->dev, "chip not available\n");
684 err = -ENODEV;
685 goto _tpm_clean_answer;
686 }
687
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100688 platform_data->tpm_i2c_buffer[0] =
689 kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
690 if (platform_data->tpm_i2c_buffer[0] == NULL) {
691 err = -ENOMEM;
692 goto _tpm_clean_answer;
693 }
694 platform_data->tpm_i2c_buffer[1] =
695 kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
696 if (platform_data->tpm_i2c_buffer[1] == NULL) {
697 err = -ENOMEM;
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600698 goto _tpm_clean_response1;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100699 }
700
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600701 TPM_VPRIV(chip) = client;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100702
703 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
704 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
705 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
706 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
707
708 chip->vendor.locality = LOCALITY0;
709
710 if (power_mgt) {
711 err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
712 if (err)
713 goto _gpio_init1;
714 gpio_set_value(platform_data->io_lpcpd, 1);
715 }
716
717 if (interrupts) {
718 init_completion(&platform_data->irq_detection);
719 if (request_locality(chip) != LOCALITY0) {
720 err = -ENODEV;
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600721 goto _tpm_clean_response2;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100722 }
723 err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
724 if (err)
725 goto _gpio_init2;
726
727 clear_interruption(client);
728 err = request_irq(gpio_to_irq(platform_data->io_serirq),
729 &tpm_ioserirq_handler,
730 IRQF_TRIGGER_HIGH,
731 "TPM SERIRQ management", chip);
732 if (err < 0) {
733 dev_err(chip->dev , "TPM SERIRQ signals %d not available\n",
734 gpio_to_irq(platform_data->io_serirq));
735 goto _irq_set;
736 }
737
738 err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1);
739 if (err < 0)
740 goto _irq_set;
741
742 intmask |= TPM_INTF_CMD_READY_INT
743 | TPM_INTF_FIFO_AVALAIBLE_INT
744 | TPM_INTF_WAKE_UP_READY_INT
745 | TPM_INTF_LOCALITY_CHANGE_INT
746 | TPM_INTF_STS_VALID_INT
747 | TPM_INTF_DATA_AVAIL_INT;
748
749 err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1);
750 if (err < 0)
751 goto _irq_set;
752
753 intmask = TPM_GLOBAL_INT_ENABLE;
754 err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1);
755 if (err < 0)
756 goto _irq_set;
757
758 err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1);
759 if (err < 0)
760 goto _irq_set;
761
762 chip->vendor.irq = interrupts;
763
764 tpm_gen_interrupt(chip);
765 }
766
767 tpm_get_timeouts(chip);
768
769 i2c_set_clientdata(client, chip);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100770
771 dev_info(chip->dev, "TPM I2C Initialized\n");
772 return 0;
773_irq_set:
774 free_irq(gpio_to_irq(platform_data->io_serirq), (void *) chip);
775_gpio_init2:
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600776 if (interrupts)
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100777 gpio_free(platform_data->io_serirq);
778_gpio_init1:
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600779 if (power_mgt)
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100780 gpio_free(platform_data->io_lpcpd);
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600781_tpm_clean_response2:
782 kzfree(platform_data->tpm_i2c_buffer[1]);
783 platform_data->tpm_i2c_buffer[1] = NULL;
784_tpm_clean_response1:
785 kzfree(platform_data->tpm_i2c_buffer[0]);
786 platform_data->tpm_i2c_buffer[0] = NULL;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100787_tpm_clean_answer:
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600788 tpm_remove_hardware(chip->dev);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100789end:
790 pr_info("TPM I2C initialisation fail\n");
791 return err;
792}
793
794/*
795 * tpm_st33_i2c_remove remove the TPM device
796 * @param: client, the i2c_client drescription (TPM I2C description).
797 clear_bit(0, &chip->is_open);
798 * @return: 0 in case of success.
799 */
800static __devexit int tpm_st33_i2c_remove(struct i2c_client *client)
801{
802 struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client);
803 struct st33zp24_platform_data *pin_infos =
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600804 ((struct i2c_client *) TPM_VPRIV(chip))->dev.platform_data;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100805
806 if (pin_infos != NULL) {
807 free_irq(pin_infos->io_serirq, chip);
808
809 gpio_free(pin_infos->io_serirq);
810 gpio_free(pin_infos->io_lpcpd);
811
Kent Yoder1fbc5e92013-01-18 17:42:25 -0600812 tpm_remove_hardware(chip->dev);
813
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100814 if (pin_infos->tpm_i2c_buffer[1] != NULL) {
815 kzfree(pin_infos->tpm_i2c_buffer[1]);
816 pin_infos->tpm_i2c_buffer[1] = NULL;
817 }
818 if (pin_infos->tpm_i2c_buffer[0] != NULL) {
819 kzfree(pin_infos->tpm_i2c_buffer[0]);
820 pin_infos->tpm_i2c_buffer[0] = NULL;
821 }
822 }
823
824 return 0;
825}
826
Peter Huewed4593352012-12-06 01:20:51 +0100827#ifdef CONFIG_PM_SLEEP
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100828/*
829 * tpm_st33_i2c_pm_suspend suspend the TPM device
830 * Added: Work around when suspend and no tpm application is running, suspend
831 * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
832 * TPM core)
833 * @param: client, the i2c_client drescription (TPM I2C description).
834 * @param: mesg, the power management message.
835 * @return: 0 in case of success.
836 */
Peter Huewed4593352012-12-06 01:20:51 +0100837static int tpm_st33_i2c_pm_suspend(struct device *dev)
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100838{
Peter Huewed4593352012-12-06 01:20:51 +0100839 struct tpm_chip *chip = dev_get_drvdata(dev);
840 struct st33zp24_platform_data *pin_infos = dev->platform_data;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100841 int ret = 0;
842
843 if (power_mgt)
844 gpio_set_value(pin_infos->io_lpcpd, 0);
845 else{
846 if (chip->data_buffer == NULL)
847 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
Peter Huewed4593352012-12-06 01:20:51 +0100848 ret = tpm_pm_suspend(dev);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100849 }
850 return ret;
851} /* tpm_st33_i2c_suspend() */
852
853/*
854 * tpm_st33_i2c_pm_resume resume the TPM device
855 * @param: client, the i2c_client drescription (TPM I2C description).
856 * @return: 0 in case of success.
857 */
Peter Huewed4593352012-12-06 01:20:51 +0100858static int tpm_st33_i2c_pm_resume(struct device *dev)
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100859{
Peter Huewed4593352012-12-06 01:20:51 +0100860 struct tpm_chip *chip = dev_get_drvdata(dev);
861 struct st33zp24_platform_data *pin_infos = dev->platform_data;
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100862
863 int ret = 0;
864
865 if (power_mgt) {
866 gpio_set_value(pin_infos->io_lpcpd, 1);
867 ret = wait_for_serirq_timeout(chip,
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600868 (chip->vendor.status(chip) &
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100869 TPM_STS_VALID) == TPM_STS_VALID,
870 chip->vendor.timeout_b);
871 } else{
872 if (chip->data_buffer == NULL)
873 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
Peter Huewed4593352012-12-06 01:20:51 +0100874 ret = tpm_pm_resume(dev);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100875 if (!ret)
876 tpm_do_selftest(chip);
877 }
878 return ret;
879} /* tpm_st33_i2c_pm_resume() */
Peter Huewed4593352012-12-06 01:20:51 +0100880#endif
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100881
882static const struct i2c_device_id tpm_st33_i2c_id[] = {
883 {TPM_ST33_I2C, 0},
884 {}
885};
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100886MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id);
Peter Huewed4593352012-12-06 01:20:51 +0100887static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend, tpm_st33_i2c_pm_resume);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100888static struct i2c_driver tpm_st33_i2c_driver = {
889 .driver = {
890 .owner = THIS_MODULE,
891 .name = TPM_ST33_I2C,
Peter Huewed4593352012-12-06 01:20:51 +0100892 .pm = &tpm_st33_i2c_ops,
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100893 },
894 .probe = tpm_st33_i2c_probe,
895 .remove = tpm_st33_i2c_remove,
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100896 .id_table = tpm_st33_i2c_id
897};
898
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600899module_i2c_driver(tpm_st33_i2c_driver);
Mathias Leblanc251a7b02012-11-28 18:22:24 +0100900
901MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
902MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
903MODULE_VERSION("1.2.0");
Kent Yoder3d7a7bd2012-12-05 16:52:43 -0600904MODULE_LICENSE("GPL");