char/tpm/tpm_i2c_stm_st33: Remove __devexit attribute
[cascardo/linux.git] / drivers / char / tpm / tpm_i2c_stm_st33.c
1 /*
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
33 #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>
54
55 #include "tpm.h"
56 #include "tpm_i2c_stm_st33.h"
57
58 enum 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
65 enum 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
73 enum 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
83 enum 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  */
96 static int write8_reg(struct i2c_client *client, u8 tpm_register,
97                       u8 *tpm_data, u16 tpm_size)
98 {
99         int value = 0;
100         struct st33zp24_platform_data *pin_infos;
101
102         pin_infos = client->dev.platform_data;
103
104         pin_infos->tpm_i2c_buffer[0][0] = tpm_register;
105         memcpy(&pin_infos->tpm_i2c_buffer[0][1], tpm_data, tpm_size);
106         value = i2c_master_send(client, pin_infos->tpm_i2c_buffer[0],
107                                 tpm_size + 1);
108         return value;
109 } /* write8_reg() */
110
111 /*
112  * read8_reg
113  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
114  * @param: tpm_register, the tpm tis register where the data should be read
115  * @param: tpm_data, the TPM response
116  * @param: tpm_size, tpm TPM response size to read.
117  * @return: number of byte read successfully: should be one if success.
118  */
119 static int read8_reg(struct i2c_client *client, u8 tpm_register,
120                     u8 *tpm_data, int tpm_size)
121 {
122         u8 status = 0;
123         u8 data;
124
125         data = TPM_DUMMY_BYTE;
126         status = write8_reg(client, tpm_register, &data, 1);
127         if (status == 2)
128                 status = i2c_master_recv(client, tpm_data, tpm_size);
129         return status;
130 } /* read8_reg() */
131
132 /*
133  * I2C_WRITE_DATA
134  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
135  * @param: client, the chip description
136  * @param: tpm_register, the tpm tis register where the data should be written
137  * @param: tpm_data, the tpm_data to write inside the tpm_register
138  * @param: tpm_size, The length of the data
139  * @return: number of byte written successfully: should be one if success.
140  */
141 #define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \
142         (write8_reg(client, tpm_register | \
143         TPM_WRITE_DIRECTION, tpm_data, tpm_size))
144
145 /*
146  * I2C_READ_DATA
147  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
148  * @param: tpm, the chip description
149  * @param: tpm_register, the tpm tis register where the data should be read
150  * @param: tpm_data, the TPM response
151  * @param: tpm_size, tpm TPM response size to read.
152  * @return: number of byte read successfully: should be one if success.
153  */
154 #define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \
155         (read8_reg(client, tpm_register, tpm_data, tpm_size))
156
157 /*
158  * clear_interruption
159  * clear the TPM interrupt register.
160  * @param: tpm, the chip description
161  */
162 static void clear_interruption(struct i2c_client *client)
163 {
164         u8 interrupt;
165         I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
166         I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1);
167         I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
168 } /* clear_interruption() */
169
170 /*
171  * _wait_for_interrupt_serirq_timeout
172  * @param: tpm, the chip description
173  * @param: timeout, the timeout of the interrupt
174  * @return: the status of the interruption.
175  */
176 static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
177                                                 unsigned long timeout)
178 {
179         long status;
180         struct i2c_client *client;
181         struct st33zp24_platform_data *pin_infos;
182
183         client = (struct i2c_client *) TPM_VPRIV(chip);
184         pin_infos = client->dev.platform_data;
185
186         status = wait_for_completion_interruptible_timeout(
187                                         &pin_infos->irq_detection,
188                                                 timeout);
189         if (status > 0)
190                 enable_irq(gpio_to_irq(pin_infos->io_serirq));
191         gpio_direction_input(pin_infos->io_serirq);
192
193         return status;
194 } /* wait_for_interrupt_serirq_timeout() */
195
196 static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
197                                  unsigned long timeout)
198 {
199         int status = 2;
200         struct i2c_client *client;
201
202         client = (struct i2c_client *) TPM_VPRIV(chip);
203
204         status = _wait_for_interrupt_serirq_timeout(chip, timeout);
205         if (!status) {
206                 status = -EBUSY;
207         } else{
208                 clear_interruption(client);
209                 if (condition)
210                         status = 1;
211         }
212         return status;
213 }
214
215 /*
216  * tpm_stm_i2c_cancel, cancel is not implemented.
217  * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
218  */
219 static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
220 {
221         struct i2c_client *client;
222         u8 data;
223
224         client = (struct i2c_client *) TPM_VPRIV(chip);
225
226         data = TPM_STS_COMMAND_READY;
227         I2C_WRITE_DATA(client, TPM_STS, &data, 1);
228         if (chip->vendor.irq)
229                 wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
230 }       /* tpm_stm_i2c_cancel() */
231
232 /*
233  * tpm_stm_spi_status return the TPM_STS register
234  * @param: chip, the tpm chip description
235  * @return: the TPM_STS register value.
236  */
237 static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
238 {
239         struct i2c_client *client;
240         u8 data;
241         client = (struct i2c_client *) TPM_VPRIV(chip);
242
243         I2C_READ_DATA(client, TPM_STS, &data, 1);
244         return data;
245 }                               /* tpm_stm_i2c_status() */
246
247
248 /*
249  * check_locality if the locality is active
250  * @param: chip, the tpm chip description
251  * @return: the active locality or -EACCESS.
252  */
253 static int check_locality(struct tpm_chip *chip)
254 {
255         struct i2c_client *client;
256         u8 data;
257         u8 status;
258
259         client = (struct i2c_client *) TPM_VPRIV(chip);
260
261         status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1);
262         if (status && (data &
263                 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
264                 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
265                 return chip->vendor.locality;
266
267         return -EACCES;
268
269 } /* check_locality() */
270
271 /*
272  * request_locality request the TPM locality
273  * @param: chip, the chip description
274  * @return: the active locality or EACCESS.
275  */
276 static int request_locality(struct tpm_chip *chip)
277 {
278         unsigned long stop;
279         long rc;
280         struct i2c_client *client;
281         u8 data;
282
283         client = (struct i2c_client *) TPM_VPRIV(chip);
284
285         if (check_locality(chip) == chip->vendor.locality)
286                 return chip->vendor.locality;
287
288         data = TPM_ACCESS_REQUEST_USE;
289         rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
290         if (rc < 0)
291                 goto end;
292
293         if (chip->vendor.irq) {
294                 rc = wait_for_serirq_timeout(chip, (check_locality
295                                                        (chip) >= 0),
296                                                       chip->vendor.timeout_a);
297                 if (rc > 0)
298                         return chip->vendor.locality;
299         } else{
300                 stop = jiffies + chip->vendor.timeout_a;
301                 do {
302                         if (check_locality(chip) >= 0)
303                                 return chip->vendor.locality;
304                         msleep(TPM_TIMEOUT);
305                 } while (time_before(jiffies, stop));
306         }
307         rc = -EACCES;
308 end:
309         return rc;
310 } /* request_locality() */
311
312 /*
313  * release_locality release the active locality
314  * @param: chip, the tpm chip description.
315  */
316 static void release_locality(struct tpm_chip *chip)
317 {
318         struct i2c_client *client;
319         u8 data;
320
321         client = (struct i2c_client *) TPM_VPRIV(chip);
322         data = TPM_ACCESS_ACTIVE_LOCALITY;
323
324         I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
325 }
326
327 /*
328  * get_burstcount return the burstcount address 0x19 0x1A
329  * @param: chip, the chip description
330  * return: the burstcount.
331  */
332 static int get_burstcount(struct tpm_chip *chip)
333 {
334         unsigned long stop;
335         int burstcnt, status;
336         u8 tpm_reg, temp;
337
338         struct i2c_client *client = (struct i2c_client *) TPM_VPRIV(chip);
339
340         stop = jiffies + chip->vendor.timeout_d;
341         do {
342                 tpm_reg = TPM_STS + 1;
343                 status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
344                 if (status < 0)
345                         goto end;
346
347                 tpm_reg = tpm_reg + 1;
348                 burstcnt = temp;
349                 status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
350                 if (status < 0)
351                         goto end;
352
353                 burstcnt |= temp << 8;
354                 if (burstcnt)
355                         return burstcnt;
356                 msleep(TPM_TIMEOUT);
357         } while (time_before(jiffies, stop));
358
359 end:
360         return -EBUSY;
361 } /* get_burstcount() */
362
363 /*
364  * wait_for_stat wait for a TPM_STS value
365  * @param: chip, the tpm chip description
366  * @param: mask, the value mask to wait
367  * @param: timeout, the timeout
368  * @param: queue, the wait queue.
369  * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
370  */
371 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
372                          wait_queue_head_t *queue)
373 {
374         unsigned long stop;
375         long rc;
376         u8 status;
377
378          if (chip->vendor.irq) {
379                 rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status
380                                                         (chip) & mask) ==
381                                                        mask), timeout);
382                 if (rc > 0)
383                         return 0;
384         } else{
385                 stop = jiffies + timeout;
386                 do {
387                         msleep(TPM_TIMEOUT);
388                         status = tpm_stm_i2c_status(chip);
389                         if ((status & mask) == mask)
390                                 return 0;
391                 } while (time_before(jiffies, stop));
392         }
393         return -ETIME;
394 } /* wait_for_stat() */
395
396 /*
397  * recv_data receive data
398  * @param: chip, the tpm chip description
399  * @param: buf, the buffer where the data are received
400  * @param: count, the number of data to receive
401  * @return: the number of bytes read from TPM FIFO.
402  */
403 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
404 {
405         int size = 0, burstcnt, len;
406         struct i2c_client *client;
407
408         client = (struct i2c_client *) TPM_VPRIV(chip);
409
410         while (size < count &&
411                wait_for_stat(chip,
412                              TPM_STS_DATA_AVAIL | TPM_STS_VALID,
413                              chip->vendor.timeout_c,
414                              &chip->vendor.read_queue)
415                == 0) {
416                 burstcnt = get_burstcount(chip);
417                 len = min_t(int, burstcnt, count - size);
418                 I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len);
419                 size += len;
420         }
421         return size;
422 }
423
424 /*
425  * tpm_ioserirq_handler the serirq irq handler
426  * @param: irq, the tpm chip description
427  * @param: dev_id, the description of the chip
428  * @return: the status of the handler.
429  */
430 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
431 {
432         struct tpm_chip *chip = dev_id;
433         struct i2c_client *client;
434         struct st33zp24_platform_data *pin_infos;
435
436         disable_irq_nosync(irq);
437
438         client = (struct i2c_client *) TPM_VPRIV(chip);
439         pin_infos = client->dev.platform_data;
440
441         complete(&pin_infos->irq_detection);
442         return IRQ_HANDLED;
443 } /* tpm_ioserirq_handler() */
444
445
446 /*
447  * tpm_stm_i2c_send send TPM commands through the I2C bus.
448  *
449  * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
450  * @param: buf, the buffer to send.
451  * @param: count, the number of bytes to send.
452  * @return: In case of success the number of bytes sent.
453  *                      In other case, a < 0 value describing the issue.
454  */
455 static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
456                             size_t len)
457 {
458         u32 status,
459             burstcnt = 0, i, size;
460         int ret;
461         u8 data;
462         struct i2c_client *client;
463
464         if (chip == NULL)
465                 return -EBUSY;
466         if (len < TPM_HEADER_SIZE)
467                 return -EBUSY;
468
469         client = (struct i2c_client *)TPM_VPRIV(chip);
470
471         client->flags = 0;
472
473         ret = request_locality(chip);
474         if (ret < 0)
475                 return ret;
476
477         status = tpm_stm_i2c_status(chip);
478         if ((status & TPM_STS_COMMAND_READY) == 0) {
479                 tpm_stm_i2c_cancel(chip);
480                 if (wait_for_stat
481                     (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
482                      &chip->vendor.int_queue) < 0) {
483                         ret = -ETIME;
484                         goto out_err;
485                 }
486         }
487
488         for (i = 0 ; i < len - 1 ;) {
489                 burstcnt = get_burstcount(chip);
490                 size = min_t(int, len - i - 1, burstcnt);
491                 ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf, size);
492                 if (ret < 0)
493                         goto out_err;
494
495                 i += size;
496         }
497
498         status = tpm_stm_i2c_status(chip);
499         if ((status & TPM_STS_DATA_EXPECT) == 0) {
500                 ret = -EIO;
501                 goto out_err;
502         }
503
504         ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1);
505         if (ret < 0)
506                 goto out_err;
507
508         status = tpm_stm_i2c_status(chip);
509         if ((status & TPM_STS_DATA_EXPECT) != 0) {
510                 ret = -EIO;
511                 goto out_err;
512         }
513
514         data = TPM_STS_GO;
515         I2C_WRITE_DATA(client, TPM_STS, &data, 1);
516
517         return len;
518 out_err:
519         tpm_stm_i2c_cancel(chip);
520         release_locality(chip);
521         return ret;
522 }
523
524 /*
525  * tpm_stm_i2c_recv received TPM response through the I2C bus.
526  * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
527  * @param: buf, the buffer to store datas.
528  * @param: count, the number of bytes to send.
529  * @return: In case of success the number of bytes received.
530  *              In other case, a < 0 value describing the issue.
531  */
532 static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
533                             size_t count)
534 {
535         int size = 0;
536         int expected;
537
538         if (chip == NULL)
539                 return -EBUSY;
540
541         if (count < TPM_HEADER_SIZE) {
542                 size = -EIO;
543                 goto out;
544         }
545
546         size = recv_data(chip, buf, TPM_HEADER_SIZE);
547         if (size < TPM_HEADER_SIZE) {
548                 dev_err(chip->dev, "Unable to read header\n");
549                 goto out;
550         }
551
552         expected = be32_to_cpu(*(__be32 *) (buf + 2));
553         if (expected > count) {
554                 size = -EIO;
555                 goto out;
556         }
557
558         size += recv_data(chip, &buf[TPM_HEADER_SIZE],
559                                         expected - TPM_HEADER_SIZE);
560         if (size < expected) {
561                 dev_err(chip->dev, "Unable to read remainder of result\n");
562                 size = -ETIME;
563                 goto out;
564         }
565
566 out:
567         chip->vendor.cancel(chip);
568         release_locality(chip);
569         return size;
570 }
571
572 static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status)
573 {
574        return (status == TPM_STS_COMMAND_READY);
575 }
576
577 static const struct file_operations tpm_st33_i2c_fops = {
578         .owner = THIS_MODULE,
579         .llseek = no_llseek,
580         .read = tpm_read,
581         .write = tpm_write,
582         .open = tpm_open,
583         .release = tpm_release,
584 };
585
586 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
587 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
588 static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
589 static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
590 static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
591 static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
592 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
593 static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
594
595 static struct attribute *stm_tpm_attrs[] = {
596         &dev_attr_pubek.attr,
597         &dev_attr_pcrs.attr,
598         &dev_attr_enabled.attr,
599         &dev_attr_active.attr,
600         &dev_attr_owned.attr,
601         &dev_attr_temp_deactivated.attr,
602         &dev_attr_caps.attr,
603         &dev_attr_cancel.attr, NULL,
604 };
605
606 static struct attribute_group stm_tpm_attr_grp = {
607         .attrs = stm_tpm_attrs
608 };
609
610 static struct tpm_vendor_specific st_i2c_tpm = {
611         .send = tpm_stm_i2c_send,
612         .recv = tpm_stm_i2c_recv,
613         .cancel = tpm_stm_i2c_cancel,
614         .status = tpm_stm_i2c_status,
615         .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
616         .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
617         .req_canceled = tpm_st33_i2c_req_canceled,
618         .attr_group = &stm_tpm_attr_grp,
619         .miscdev = {.fops = &tpm_st33_i2c_fops,},
620 };
621
622 static int interrupts ;
623 module_param(interrupts, int, 0444);
624 MODULE_PARM_DESC(interrupts, "Enable interrupts");
625
626 static int power_mgt = 1;
627 module_param(power_mgt, int, 0444);
628 MODULE_PARM_DESC(power_mgt, "Power Management");
629
630 /*
631  * tpm_st33_i2c_probe initialize the TPM device
632  * @param: client, the i2c_client drescription (TPM I2C description).
633  * @param: id, the i2c_device_id struct.
634  * @return: 0 in case of success.
635  *       -1 in other case.
636  */
637 static int
638 tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
639 {
640         int err;
641         u8 intmask;
642         struct tpm_chip *chip;
643         struct st33zp24_platform_data *platform_data;
644
645         err = 0;
646
647         if (client == NULL) {
648                 pr_info("%s: i2c client is NULL. Device not accessible.\n",
649                         __func__);
650                 err = -ENODEV;
651                 goto end;
652         }
653
654         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
655                 dev_info(&client->dev, "client not i2c capable\n");
656                 err = -ENODEV;
657                 goto end;
658         }
659
660         chip = tpm_register_hardware(&client->dev, &st_i2c_tpm);
661         if (!chip) {
662                 dev_info(&client->dev, "fail chip\n");
663                 err = -ENODEV;
664                 goto end;
665         }
666
667         platform_data = client->dev.platform_data;
668
669         if (!platform_data) {
670                 dev_info(&client->dev, "chip not available\n");
671                 err = -ENODEV;
672                 goto _tpm_clean_answer;
673         }
674
675         platform_data->tpm_i2c_buffer[0] =
676             kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
677         if (platform_data->tpm_i2c_buffer[0] == NULL) {
678                 err = -ENOMEM;
679                 goto _tpm_clean_answer;
680         }
681         platform_data->tpm_i2c_buffer[1] =
682             kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
683         if (platform_data->tpm_i2c_buffer[1] == NULL) {
684                 err = -ENOMEM;
685                 goto _tpm_clean_response1;
686         }
687
688         TPM_VPRIV(chip) = client;
689
690         chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
691         chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
692         chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
693         chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
694
695         chip->vendor.locality = LOCALITY0;
696
697         if (power_mgt) {
698                 err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
699                 if (err)
700                         goto _gpio_init1;
701                 gpio_set_value(platform_data->io_lpcpd, 1);
702         }
703
704         if (interrupts) {
705                 init_completion(&platform_data->irq_detection);
706                 if (request_locality(chip) != LOCALITY0) {
707                         err = -ENODEV;
708                         goto _tpm_clean_response2;
709                 }
710                 err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
711                 if (err)
712                         goto _gpio_init2;
713
714                 clear_interruption(client);
715                 err = request_irq(gpio_to_irq(platform_data->io_serirq),
716                                 &tpm_ioserirq_handler,
717                                 IRQF_TRIGGER_HIGH,
718                                 "TPM SERIRQ management", chip);
719                 if (err < 0) {
720                         dev_err(chip->dev , "TPM SERIRQ signals %d not available\n",
721                         gpio_to_irq(platform_data->io_serirq));
722                         goto _irq_set;
723                 }
724
725                 err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1);
726                 if (err < 0)
727                         goto _irq_set;
728
729                 intmask |= TPM_INTF_CMD_READY_INT
730                         |  TPM_INTF_FIFO_AVALAIBLE_INT
731                         |  TPM_INTF_WAKE_UP_READY_INT
732                         |  TPM_INTF_LOCALITY_CHANGE_INT
733                         |  TPM_INTF_STS_VALID_INT
734                         |  TPM_INTF_DATA_AVAIL_INT;
735
736                 err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1);
737                 if (err < 0)
738                         goto _irq_set;
739
740                 intmask = TPM_GLOBAL_INT_ENABLE;
741                 err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1);
742                 if (err < 0)
743                         goto _irq_set;
744
745                 err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1);
746                 if (err < 0)
747                         goto _irq_set;
748
749                 chip->vendor.irq = interrupts;
750
751                 tpm_gen_interrupt(chip);
752         }
753
754         tpm_get_timeouts(chip);
755
756         i2c_set_clientdata(client, chip);
757
758         dev_info(chip->dev, "TPM I2C Initialized\n");
759         return 0;
760 _irq_set:
761         free_irq(gpio_to_irq(platform_data->io_serirq), (void *) chip);
762 _gpio_init2:
763         if (interrupts)
764                 gpio_free(platform_data->io_serirq);
765 _gpio_init1:
766         if (power_mgt)
767                 gpio_free(platform_data->io_lpcpd);
768 _tpm_clean_response2:
769         kzfree(platform_data->tpm_i2c_buffer[1]);
770         platform_data->tpm_i2c_buffer[1] = NULL;
771 _tpm_clean_response1:
772         kzfree(platform_data->tpm_i2c_buffer[0]);
773         platform_data->tpm_i2c_buffer[0] = NULL;
774 _tpm_clean_answer:
775         tpm_remove_hardware(chip->dev);
776 end:
777         pr_info("TPM I2C initialisation fail\n");
778         return err;
779 }
780
781 /*
782  * tpm_st33_i2c_remove remove the TPM device
783  * @param: client, the i2c_client drescription (TPM I2C description).
784                 clear_bit(0, &chip->is_open);
785  * @return: 0 in case of success.
786  */
787 static int tpm_st33_i2c_remove(struct i2c_client *client)
788 {
789         struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client);
790         struct st33zp24_platform_data *pin_infos =
791                 ((struct i2c_client *) TPM_VPRIV(chip))->dev.platform_data;
792
793         if (pin_infos != NULL) {
794                 free_irq(pin_infos->io_serirq, chip);
795
796                 gpio_free(pin_infos->io_serirq);
797                 gpio_free(pin_infos->io_lpcpd);
798
799                 tpm_remove_hardware(chip->dev);
800
801                 if (pin_infos->tpm_i2c_buffer[1] != NULL) {
802                         kzfree(pin_infos->tpm_i2c_buffer[1]);
803                         pin_infos->tpm_i2c_buffer[1] = NULL;
804                 }
805                 if (pin_infos->tpm_i2c_buffer[0] != NULL) {
806                         kzfree(pin_infos->tpm_i2c_buffer[0]);
807                         pin_infos->tpm_i2c_buffer[0] = NULL;
808                 }
809         }
810
811         return 0;
812 }
813
814 #ifdef CONFIG_PM_SLEEP
815 /*
816  * tpm_st33_i2c_pm_suspend suspend the TPM device
817  * Added: Work around when suspend and no tpm application is running, suspend
818  * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
819  * TPM core)
820  * @param: client, the i2c_client drescription (TPM I2C description).
821  * @param: mesg, the power management message.
822  * @return: 0 in case of success.
823  */
824 static int tpm_st33_i2c_pm_suspend(struct device *dev)
825 {
826         struct tpm_chip *chip = dev_get_drvdata(dev);
827         struct st33zp24_platform_data *pin_infos = dev->platform_data;
828         int ret = 0;
829
830         if (power_mgt)
831                 gpio_set_value(pin_infos->io_lpcpd, 0);
832         else{
833                 if (chip->data_buffer == NULL)
834                         chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
835                 ret = tpm_pm_suspend(dev);
836         }
837         return ret;
838 }                               /* tpm_st33_i2c_suspend() */
839
840 /*
841  * tpm_st33_i2c_pm_resume resume the TPM device
842  * @param: client, the i2c_client drescription (TPM I2C description).
843  * @return: 0 in case of success.
844  */
845 static int tpm_st33_i2c_pm_resume(struct device *dev)
846 {
847         struct tpm_chip *chip = dev_get_drvdata(dev);
848         struct st33zp24_platform_data *pin_infos = dev->platform_data;
849
850         int ret = 0;
851
852         if (power_mgt) {
853                 gpio_set_value(pin_infos->io_lpcpd, 1);
854                 ret = wait_for_serirq_timeout(chip,
855                                           (chip->vendor.status(chip) &
856                                           TPM_STS_VALID) == TPM_STS_VALID,
857                                           chip->vendor.timeout_b);
858         } else{
859         if (chip->data_buffer == NULL)
860                 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
861         ret = tpm_pm_resume(dev);
862         if (!ret)
863                 tpm_do_selftest(chip);
864         }
865         return ret;
866 }                               /* tpm_st33_i2c_pm_resume() */
867 #endif
868
869 static const struct i2c_device_id tpm_st33_i2c_id[] = {
870         {TPM_ST33_I2C, 0},
871         {}
872 };
873 MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id);
874 static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend, tpm_st33_i2c_pm_resume);
875 static struct i2c_driver tpm_st33_i2c_driver = {
876         .driver = {
877                    .owner = THIS_MODULE,
878                    .name = TPM_ST33_I2C,
879                    .pm = &tpm_st33_i2c_ops,
880                    },
881         .probe = tpm_st33_i2c_probe,
882         .remove = tpm_st33_i2c_remove,
883         .id_table = tpm_st33_i2c_id
884 };
885
886 module_i2c_driver(tpm_st33_i2c_driver);
887
888 MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
889 MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
890 MODULE_VERSION("1.2.0");
891 MODULE_LICENSE("GPL");