dbab8d0d875eca2c1c3a86478c1e8f1df609cf58
[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, 2014  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  * STMicroelectronics version 1.2.1, Copyright (C) 2014
19  * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
20  * This is free software, and you are welcome to redistribute it
21  * under certain conditions.
22  *
23  * @Author: Christophe RICARD tpmsupport@st.com
24  *
25  * @File: tpm_stm_st33_i2c.c
26  *
27  * @Synopsis:
28  *      09/15/2010:     First shot driver tpm_tis driver for
29  *                       lpc is used as model.
30  */
31
32 #include <linux/pci.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/i2c.h>
36 #include <linux/fs.h>
37 #include <linux/miscdevice.h>
38 #include <linux/kernel.h>
39 #include <linux/delay.h>
40 #include <linux/wait.h>
41 #include <linux/freezer.h>
42 #include <linux/string.h>
43 #include <linux/interrupt.h>
44 #include <linux/sysfs.h>
45 #include <linux/gpio.h>
46 #include <linux/sched.h>
47 #include <linux/uaccess.h>
48 #include <linux/io.h>
49 #include <linux/slab.h>
50 #include <linux/of_irq.h>
51 #include <linux/of_gpio.h>
52
53 #include <linux/platform_data/tpm_stm_st33.h>
54 #include "tpm.h"
55
56 #define TPM_ACCESS                      0x0
57 #define TPM_STS                         0x18
58 #define TPM_HASH_END                    0x20
59 #define TPM_DATA_FIFO                   0x24
60 #define TPM_HASH_DATA                   0x24
61 #define TPM_HASH_START                  0x28
62 #define TPM_INTF_CAPABILITY             0x14
63 #define TPM_INT_STATUS                  0x10
64 #define TPM_INT_ENABLE                  0x08
65
66 #define TPM_DUMMY_BYTE                  0xAA
67 #define TPM_WRITE_DIRECTION             0x80
68 #define TPM_HEADER_SIZE                 10
69 #define TPM_BUFSIZE                     2048
70
71 #define LOCALITY0               0
72
73
74 enum stm33zp24_access {
75         TPM_ACCESS_VALID = 0x80,
76         TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
77         TPM_ACCESS_REQUEST_PENDING = 0x04,
78         TPM_ACCESS_REQUEST_USE = 0x02,
79 };
80
81 enum stm33zp24_status {
82         TPM_STS_VALID = 0x80,
83         TPM_STS_COMMAND_READY = 0x40,
84         TPM_STS_GO = 0x20,
85         TPM_STS_DATA_AVAIL = 0x10,
86         TPM_STS_DATA_EXPECT = 0x08,
87 };
88
89 enum stm33zp24_int_flags {
90         TPM_GLOBAL_INT_ENABLE = 0x80,
91         TPM_INTF_CMD_READY_INT = 0x080,
92         TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
93         TPM_INTF_WAKE_UP_READY_INT = 0x020,
94         TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
95         TPM_INTF_STS_VALID_INT = 0x002,
96         TPM_INTF_DATA_AVAIL_INT = 0x001,
97 };
98
99 enum tis_defaults {
100         TIS_SHORT_TIMEOUT = 750,
101         TIS_LONG_TIMEOUT = 2000,
102 };
103
104 struct tpm_stm_dev {
105         struct i2c_client *client;
106         struct tpm_chip *chip;
107         u8 buf[TPM_BUFSIZE + 1];
108         u32 intrs;
109         int io_lpcpd;
110 };
111
112 /*
113  * write8_reg
114  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
115  * @param: tpm_register, the tpm tis register where the data should be written
116  * @param: tpm_data, the tpm_data to write inside the tpm_register
117  * @param: tpm_size, The length of the data
118  * @return: Returns negative errno, or else the number of bytes written.
119  */
120 static int write8_reg(struct tpm_stm_dev *tpm_dev, u8 tpm_register,
121                       u8 *tpm_data, u16 tpm_size)
122 {
123         tpm_dev->buf[0] = tpm_register;
124         memcpy(tpm_dev->buf + 1, tpm_data, tpm_size);
125         return i2c_master_send(tpm_dev->client, tpm_dev->buf, tpm_size + 1);
126 } /* write8_reg() */
127
128 /*
129  * read8_reg
130  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
131  * @param: tpm_register, the tpm tis register where the data should be read
132  * @param: tpm_data, the TPM response
133  * @param: tpm_size, tpm TPM response size to read.
134  * @return: number of byte read successfully: should be one if success.
135  */
136 static int read8_reg(struct tpm_stm_dev *tpm_dev, u8 tpm_register,
137                     u8 *tpm_data, int tpm_size)
138 {
139         u8 status = 0;
140         u8 data;
141
142         data = TPM_DUMMY_BYTE;
143         status = write8_reg(tpm_dev, tpm_register, &data, 1);
144         if (status == 2)
145                 status = i2c_master_recv(tpm_dev->client, tpm_data, tpm_size);
146         return status;
147 } /* read8_reg() */
148
149 /*
150  * I2C_WRITE_DATA
151  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
152  * @param: tpm_dev, the chip description
153  * @param: tpm_register, the tpm tis register where the data should be written
154  * @param: tpm_data, the tpm_data to write inside the tpm_register
155  * @param: tpm_size, The length of the data
156  * @return: number of byte written successfully: should be one if success.
157  */
158 #define I2C_WRITE_DATA(tpm_dev, tpm_register, tpm_data, tpm_size) \
159         (write8_reg(tpm_dev, tpm_register | \
160         TPM_WRITE_DIRECTION, tpm_data, tpm_size))
161
162 /*
163  * I2C_READ_DATA
164  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
165  * @param: tpm_dev, the chip description
166  * @param: tpm_register, the tpm tis register where the data should be read
167  * @param: tpm_data, the TPM response
168  * @param: tpm_size, tpm TPM response size to read.
169  * @return: number of byte read successfully: should be one if success.
170  */
171 #define I2C_READ_DATA(tpm_dev, tpm_register, tpm_data, tpm_size) \
172         (read8_reg(tpm_dev, tpm_register, tpm_data, tpm_size))
173
174 /*
175  * clear_interruption
176  * clear the TPM interrupt register.
177  * @param: tpm, the chip description
178  * @return: the TPM_INT_STATUS value
179  */
180 static u8 clear_interruption(struct tpm_stm_dev *tpm_dev)
181 {
182         u8 interrupt;
183
184         I2C_READ_DATA(tpm_dev, TPM_INT_STATUS, &interrupt, 1);
185         I2C_WRITE_DATA(tpm_dev, TPM_INT_STATUS, &interrupt, 1);
186         return interrupt;
187 } /* clear_interruption() */
188
189 /*
190  * tpm_stm_i2c_cancel, cancel is not implemented.
191  * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
192  */
193 static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
194 {
195         struct tpm_stm_dev *tpm_dev;
196         u8 data;
197
198         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
199
200         data = TPM_STS_COMMAND_READY;
201         I2C_WRITE_DATA(tpm_dev, TPM_STS, &data, 1);
202 } /* tpm_stm_i2c_cancel() */
203
204 /*
205  * tpm_stm_spi_status return the TPM_STS register
206  * @param: chip, the tpm chip description
207  * @return: the TPM_STS register value.
208  */
209 static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
210 {
211         struct tpm_stm_dev *tpm_dev;
212         u8 data;
213
214         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
215
216         I2C_READ_DATA(tpm_dev, TPM_STS, &data, 1);
217         return data;
218 } /* tpm_stm_i2c_status() */
219
220
221 /*
222  * check_locality if the locality is active
223  * @param: chip, the tpm chip description
224  * @return: the active locality or -EACCESS.
225  */
226 static int check_locality(struct tpm_chip *chip)
227 {
228         struct tpm_stm_dev *tpm_dev;
229         u8 data;
230         u8 status;
231
232         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
233
234         status = I2C_READ_DATA(tpm_dev, TPM_ACCESS, &data, 1);
235         if (status && (data &
236                 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
237                 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
238                 return chip->vendor.locality;
239
240         return -EACCES;
241 } /* check_locality() */
242
243 /*
244  * request_locality request the TPM locality
245  * @param: chip, the chip description
246  * @return: the active locality or EACCESS.
247  */
248 static int request_locality(struct tpm_chip *chip)
249 {
250         unsigned long stop;
251         long ret;
252         struct tpm_stm_dev *tpm_dev;
253         u8 data;
254
255         if (check_locality(chip) == chip->vendor.locality)
256                 return chip->vendor.locality;
257
258         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
259
260         data = TPM_ACCESS_REQUEST_USE;
261         ret = I2C_WRITE_DATA(tpm_dev, TPM_ACCESS, &data, 1);
262         if (ret < 0)
263                 goto end;
264
265         stop = jiffies + chip->vendor.timeout_a;
266
267         /* Request locality is usually effective after the request */
268         do {
269                 if (check_locality(chip) >= 0)
270                         return chip->vendor.locality;
271                 msleep(TPM_TIMEOUT);
272         } while (time_before(jiffies, stop));
273         ret = -EACCES;
274 end:
275         return ret;
276 } /* request_locality() */
277
278 /*
279  * release_locality release the active locality
280  * @param: chip, the tpm chip description.
281  */
282 static void release_locality(struct tpm_chip *chip)
283 {
284         struct tpm_stm_dev *tpm_dev;
285         u8 data;
286
287         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
288         data = TPM_ACCESS_ACTIVE_LOCALITY;
289
290         I2C_WRITE_DATA(tpm_dev, TPM_ACCESS, &data, 1);
291 }
292
293 /*
294  * get_burstcount return the burstcount address 0x19 0x1A
295  * @param: chip, the chip description
296  * return: the burstcount.
297  */
298 static int get_burstcount(struct tpm_chip *chip)
299 {
300         unsigned long stop;
301         int burstcnt, status;
302         u8 tpm_reg, temp;
303         struct tpm_stm_dev *tpm_dev;
304
305         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
306
307         stop = jiffies + chip->vendor.timeout_d;
308         do {
309                 tpm_reg = TPM_STS + 1;
310                 status = I2C_READ_DATA(tpm_dev, tpm_reg, &temp, 1);
311                 if (status < 0)
312                         goto end;
313
314                 tpm_reg = tpm_reg + 1;
315                 burstcnt = temp;
316                 status = I2C_READ_DATA(tpm_dev, tpm_reg, &temp, 1);
317                 if (status < 0)
318                         goto end;
319
320                 burstcnt |= temp << 8;
321                 if (burstcnt)
322                         return burstcnt;
323                 msleep(TPM_TIMEOUT);
324         } while (time_before(jiffies, stop));
325
326 end:
327         return -EBUSY;
328 } /* get_burstcount() */
329
330 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
331                                 bool check_cancel, bool *canceled)
332 {
333         u8 status = chip->ops->status(chip);
334
335         *canceled = false;
336         if ((status & mask) == mask)
337                 return true;
338         if (check_cancel && chip->ops->req_canceled(chip, status)) {
339                 *canceled = true;
340                 return true;
341         }
342         return false;
343 }
344
345 /*
346  * interrupt_to_status
347  * @param: irq_mask, the irq mask value to wait
348  * @return: the corresponding tpm_sts value
349  */
350 static u8 interrupt_to_status(u8 irq_mask)
351 {
352         u8 status = 0;
353
354         if ((irq_mask & TPM_INTF_STS_VALID_INT) == TPM_INTF_STS_VALID_INT)
355                 status |= TPM_STS_VALID;
356         if ((irq_mask & TPM_INTF_DATA_AVAIL_INT) == TPM_INTF_DATA_AVAIL_INT)
357                 status |= TPM_STS_DATA_AVAIL;
358         if ((irq_mask & TPM_INTF_CMD_READY_INT) == TPM_INTF_CMD_READY_INT)
359                 status |= TPM_STS_COMMAND_READY;
360
361         return status;
362 } /* status_to_interrupt() */
363
364 /*
365  * wait_for_stat wait for a TPM_STS value
366  * @param: chip, the tpm chip description
367  * @param: mask, the value mask to wait
368  * @param: timeout, the timeout
369  * @param: queue, the wait queue.
370  * @param: check_cancel, does the command can be cancelled ?
371  * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
372  */
373 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
374                         wait_queue_head_t *queue, bool check_cancel)
375 {
376         unsigned long stop;
377         int ret;
378         bool canceled = false;
379         bool condition;
380         u32 cur_intrs;
381         u8 interrupt, status;
382         struct tpm_stm_dev *tpm_dev;
383
384         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
385
386         /* check current status */
387         status = tpm_stm_i2c_status(chip);
388         if ((status & mask) == mask)
389                 return 0;
390
391         stop = jiffies + timeout;
392
393         if (chip->vendor.irq) {
394                 cur_intrs = tpm_dev->intrs;
395                 interrupt = clear_interruption(tpm_dev);
396                 enable_irq(chip->vendor.irq);
397
398 again:
399                 timeout = stop - jiffies;
400                 if ((long) timeout <= 0)
401                         return -1;
402
403                 ret = wait_event_interruptible_timeout(*queue,
404                                         cur_intrs != tpm_dev->intrs, timeout);
405
406                 interrupt |= clear_interruption(tpm_dev);
407                 status = interrupt_to_status(interrupt);
408                 condition = wait_for_tpm_stat_cond(chip, mask,
409                                                    check_cancel, &canceled);
410
411                 if (ret >= 0 && condition) {
412                         if (canceled)
413                                 return -ECANCELED;
414                         return 0;
415                 }
416                 if (ret == -ERESTARTSYS && freezing(current)) {
417                         clear_thread_flag(TIF_SIGPENDING);
418                         goto again;
419                 }
420                 disable_irq_nosync(chip->vendor.irq);
421
422         } else {
423                 do {
424                         msleep(TPM_TIMEOUT);
425                         status = chip->ops->status(chip);
426                         if ((status & mask) == mask)
427                                 return 0;
428                 } while (time_before(jiffies, stop));
429         }
430
431         return -ETIME;
432 } /* wait_for_stat() */
433
434 /*
435  * recv_data receive data
436  * @param: chip, the tpm chip description
437  * @param: buf, the buffer where the data are received
438  * @param: count, the number of data to receive
439  * @return: the number of bytes read from TPM FIFO.
440  */
441 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
442 {
443         int size = 0, burstcnt, len, ret;
444         struct tpm_stm_dev *tpm_dev;
445
446         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
447
448         while (size < count &&
449                wait_for_stat(chip,
450                              TPM_STS_DATA_AVAIL | TPM_STS_VALID,
451                              chip->vendor.timeout_c,
452                              &chip->vendor.read_queue, true) == 0) {
453                 burstcnt = get_burstcount(chip);
454                 if (burstcnt < 0)
455                         return burstcnt;
456                 len = min_t(int, burstcnt, count - size);
457                 ret = I2C_READ_DATA(tpm_dev, TPM_DATA_FIFO, buf + size, len);
458                 if (ret < 0)
459                         return ret;
460
461                 size += len;
462         }
463         return size;
464 }
465
466 /*
467  * tpm_ioserirq_handler the serirq irq handler
468  * @param: irq, the tpm chip description
469  * @param: dev_id, the description of the chip
470  * @return: the status of the handler.
471  */
472 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
473 {
474         struct tpm_chip *chip = dev_id;
475         struct tpm_stm_dev *tpm_dev;
476
477         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
478
479         tpm_dev->intrs++;
480         wake_up_interruptible(&chip->vendor.read_queue);
481         disable_irq_nosync(chip->vendor.irq);
482
483         return IRQ_HANDLED;
484 } /* tpm_ioserirq_handler() */
485
486
487 /*
488  * tpm_stm_i2c_send send TPM commands through the I2C bus.
489  *
490  * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
491  * @param: buf, the buffer to send.
492  * @param: count, the number of bytes to send.
493  * @return: In case of success the number of bytes sent.
494  *                      In other case, a < 0 value describing the issue.
495  */
496 static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
497                             size_t len)
498 {
499         u32 status, i, size;
500         int burstcnt = 0;
501         int ret;
502         u8 data;
503         struct i2c_client *client;
504         struct tpm_stm_dev *tpm_dev;
505
506         if (!chip)
507                 return -EBUSY;
508         if (len < TPM_HEADER_SIZE)
509                 return -EBUSY;
510
511         tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
512         client = tpm_dev->client;
513
514         client->flags = 0;
515
516         ret = request_locality(chip);
517         if (ret < 0)
518                 return ret;
519
520         status = tpm_stm_i2c_status(chip);
521         if ((status & TPM_STS_COMMAND_READY) == 0) {
522                 tpm_stm_i2c_cancel(chip);
523                 if (wait_for_stat
524                     (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
525                      &chip->vendor.read_queue, false) < 0) {
526                         ret = -ETIME;
527                         goto out_err;
528                 }
529         }
530
531         for (i = 0; i < len - 1;) {
532                 burstcnt = get_burstcount(chip);
533                 if (burstcnt < 0)
534                         return burstcnt;
535                 size = min_t(int, len - i - 1, burstcnt);
536                 ret = I2C_WRITE_DATA(tpm_dev, TPM_DATA_FIFO, buf + i, size);
537                 if (ret < 0)
538                         goto out_err;
539
540                 i += size;
541         }
542
543         status = tpm_stm_i2c_status(chip);
544         if ((status & TPM_STS_DATA_EXPECT) == 0) {
545                 ret = -EIO;
546                 goto out_err;
547         }
548
549         ret = I2C_WRITE_DATA(tpm_dev, TPM_DATA_FIFO, buf + len - 1, 1);
550         if (ret < 0)
551                 goto out_err;
552
553         status = tpm_stm_i2c_status(chip);
554         if ((status & TPM_STS_DATA_EXPECT) != 0) {
555                 ret = -EIO;
556                 goto out_err;
557         }
558
559         data = TPM_STS_GO;
560         I2C_WRITE_DATA(tpm_dev, TPM_STS, &data, 1);
561
562         return len;
563 out_err:
564         tpm_stm_i2c_cancel(chip);
565         release_locality(chip);
566         return ret;
567 }
568
569 /*
570  * tpm_stm_i2c_recv received TPM response through the I2C bus.
571  * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
572  * @param: buf, the buffer to store datas.
573  * @param: count, the number of bytes to send.
574  * @return: In case of success the number of bytes received.
575  *          In other case, a < 0 value describing the issue.
576  */
577 static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
578                             size_t count)
579 {
580         int size = 0;
581         int expected;
582
583         if (!chip)
584                 return -EBUSY;
585
586         if (count < TPM_HEADER_SIZE) {
587                 size = -EIO;
588                 goto out;
589         }
590
591         size = recv_data(chip, buf, TPM_HEADER_SIZE);
592         if (size < TPM_HEADER_SIZE) {
593                 dev_err(chip->pdev, "Unable to read header\n");
594                 goto out;
595         }
596
597         expected = be32_to_cpu(*(__be32 *)(buf + 2));
598         if (expected > count) {
599                 size = -EIO;
600                 goto out;
601         }
602
603         size += recv_data(chip, &buf[TPM_HEADER_SIZE],
604                         expected - TPM_HEADER_SIZE);
605         if (size < expected) {
606                 dev_err(chip->pdev, "Unable to read remainder of result\n");
607                 size = -ETIME;
608                 goto out;
609         }
610
611 out:
612         chip->ops->cancel(chip);
613         release_locality(chip);
614         return size;
615 }
616
617 static bool tpm_stm_i2c_req_canceled(struct tpm_chip *chip, u8 status)
618 {
619         return (status == TPM_STS_COMMAND_READY);
620 }
621
622 static const struct tpm_class_ops st_i2c_tpm = {
623         .send = tpm_stm_i2c_send,
624         .recv = tpm_stm_i2c_recv,
625         .cancel = tpm_stm_i2c_cancel,
626         .status = tpm_stm_i2c_status,
627         .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
628         .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
629         .req_canceled = tpm_stm_i2c_req_canceled,
630 };
631
632 #ifdef CONFIG_OF
633 static int tpm_stm_i2c_of_request_resources(struct tpm_chip *chip)
634 {
635         struct device_node *pp;
636         struct tpm_stm_dev *tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
637         struct i2c_client *client = tpm_dev->client;
638         int gpio;
639         int ret;
640
641         pp = client->dev.of_node;
642         if (!pp) {
643                 dev_err(chip->pdev, "No platform data\n");
644                 return -ENODEV;
645         }
646
647         /* Get GPIO from device tree */
648         gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
649         if (gpio < 0) {
650                 dev_err(chip->pdev, "Failed to retrieve lpcpd-gpios from dts.\n");
651                 tpm_dev->io_lpcpd = -1;
652                 /*
653                  * lpcpd pin is not specified. This is not an issue as
654                  * power management can be also managed by TPM specific
655                  * commands. So leave with a success status code.
656                  */
657                 return 0;
658         }
659         /* GPIO request and configuration */
660         ret = devm_gpio_request_one(&client->dev, gpio,
661                         GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
662         if (ret) {
663                 dev_err(chip->pdev, "Failed to request lpcpd pin\n");
664                 return -ENODEV;
665         }
666         tpm_dev->io_lpcpd = gpio;
667
668         return 0;
669 }
670 #else
671 static int tpm_stm_i2c_of_request_resources(struct tpm_chip *chip)
672 {
673         return -ENODEV;
674 }
675 #endif
676
677 static int tpm_stm_i2c_request_resources(struct i2c_client *client,
678                                          struct tpm_chip *chip)
679 {
680         struct st33zp24_platform_data *pdata;
681         struct tpm_stm_dev *tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip);
682         int ret;
683
684         pdata = client->dev.platform_data;
685         if (!pdata) {
686                 dev_err(chip->pdev, "No platform data\n");
687                 return -ENODEV;
688         }
689
690         /* store for late use */
691         tpm_dev->io_lpcpd = pdata->io_lpcpd;
692
693         if (gpio_is_valid(pdata->io_lpcpd)) {
694                 ret = devm_gpio_request_one(&client->dev,
695                                 pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
696                                 "TPM IO_LPCPD");
697                 if (ret) {
698                         dev_err(chip->pdev, "%s : reset gpio_request failed\n",
699                                 __FILE__);
700                         return ret;
701                 }
702         }
703
704         return 0;
705 }
706
707 /*
708  * tpm_stm_i2c_probe initialize the TPM device
709  * @param: client, the i2c_client drescription (TPM I2C description).
710  * @param: id, the i2c_device_id struct.
711  * @return: 0 in case of success.
712  *       -1 in other case.
713  */
714 static int
715 tpm_stm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
716 {
717         int ret;
718         u8 intmask = 0;
719         struct tpm_chip *chip;
720         struct st33zp24_platform_data *platform_data;
721         struct tpm_stm_dev *tpm_dev;
722
723         if (!client) {
724                 pr_info("%s: i2c client is NULL. Device not accessible.\n",
725                         __func__);
726                 return -ENODEV;
727         }
728
729         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
730                 dev_info(&client->dev, "client not i2c capable\n");
731                 return -ENODEV;
732         }
733
734         tpm_dev = devm_kzalloc(&client->dev, sizeof(struct tpm_stm_dev),
735                                GFP_KERNEL);
736         if (!tpm_dev)
737                 return -ENOMEM;
738
739         chip = tpmm_chip_alloc(&client->dev, &st_i2c_tpm);
740         if (IS_ERR(chip))
741                 return PTR_ERR(chip);
742
743         TPM_VPRIV(chip) = tpm_dev;
744         tpm_dev->client = client;
745
746         platform_data = client->dev.platform_data;
747         if (!platform_data && client->dev.of_node) {
748                 ret = tpm_stm_i2c_of_request_resources(chip);
749                 if (ret)
750                         goto _tpm_clean_answer;
751         } else if (platform_data) {
752                 ret = tpm_stm_i2c_request_resources(client, chip);
753                 if (ret)
754                         goto _tpm_clean_answer;
755         }
756
757         chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
758         chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
759         chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
760         chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
761
762         chip->vendor.locality = LOCALITY0;
763
764         if (client->irq) {
765                 /* INTERRUPT Setup */
766                 init_waitqueue_head(&chip->vendor.read_queue);
767                 tpm_dev->intrs = 0;
768
769                 if (request_locality(chip) != LOCALITY0) {
770                         ret = -ENODEV;
771                         goto _tpm_clean_answer;
772                 }
773
774                 clear_interruption(tpm_dev);
775                 ret = devm_request_irq(&client->dev, client->irq,
776                                 tpm_ioserirq_handler,
777                                 IRQF_TRIGGER_HIGH,
778                                 "TPM SERIRQ management", chip);
779                 if (ret < 0) {
780                         dev_err(chip->pdev , "TPM SERIRQ signals %d not available\n",
781                                 client->irq);
782                         goto _tpm_clean_answer;
783                 }
784
785                 intmask |= TPM_INTF_CMD_READY_INT
786                         |  TPM_INTF_STS_VALID_INT
787                         |  TPM_INTF_DATA_AVAIL_INT;
788
789                 ret = I2C_WRITE_DATA(tpm_dev, TPM_INT_ENABLE, &intmask, 1);
790                 if (ret < 0)
791                         goto _tpm_clean_answer;
792
793                 intmask = TPM_GLOBAL_INT_ENABLE;
794                 ret = I2C_WRITE_DATA(tpm_dev, (TPM_INT_ENABLE + 3),
795                                      &intmask, 1);
796                 if (ret < 0)
797                         goto _tpm_clean_answer;
798
799                 chip->vendor.irq = client->irq;
800
801                 disable_irq_nosync(chip->vendor.irq);
802
803                 tpm_gen_interrupt(chip);
804         }
805
806         tpm_get_timeouts(chip);
807         tpm_do_selftest(chip);
808
809         return tpm_chip_register(chip);
810 _tpm_clean_answer:
811         dev_info(chip->pdev, "TPM I2C initialisation fail\n");
812         return ret;
813 }
814
815 /*
816  * tpm_stm_i2c_remove remove the TPM device
817  * @param: client, the i2c_client description (TPM I2C description).
818  * @return: 0 in case of success.
819  */
820 static int tpm_stm_i2c_remove(struct i2c_client *client)
821 {
822         struct tpm_chip *chip =
823                 (struct tpm_chip *) i2c_get_clientdata(client);
824
825         if (chip)
826                 tpm_chip_unregister(chip);
827
828         return 0;
829 }
830
831 #ifdef CONFIG_PM_SLEEP
832 /*
833  * tpm_stm_i2c_pm_suspend suspend the TPM device
834  * @param: client, the i2c_client drescription (TPM I2C description).
835  * @param: mesg, the power management message.
836  * @return: 0 in case of success.
837  */
838 static int tpm_stm_i2c_pm_suspend(struct device *dev)
839 {
840         struct st33zp24_platform_data *pin_infos = dev->platform_data;
841         int ret = 0;
842
843         if (gpio_is_valid(pin_infos->io_lpcpd))
844                 gpio_set_value(pin_infos->io_lpcpd, 0);
845         else
846                 ret = tpm_pm_suspend(dev);
847
848         return ret;
849 } /* tpm_stm_i2c_suspend() */
850
851 /*
852  * tpm_stm_i2c_pm_resume resume the TPM device
853  * @param: client, the i2c_client drescription (TPM I2C description).
854  * @return: 0 in case of success.
855  */
856 static int tpm_stm_i2c_pm_resume(struct device *dev)
857 {
858         struct tpm_chip *chip = dev_get_drvdata(dev);
859         struct st33zp24_platform_data *pin_infos = dev->platform_data;
860
861         int ret = 0;
862
863         if (gpio_is_valid(pin_infos->io_lpcpd)) {
864                 gpio_set_value(pin_infos->io_lpcpd, 1);
865                 ret = wait_for_stat(chip,
866                                 TPM_STS_VALID, chip->vendor.timeout_b,
867                                 &chip->vendor.read_queue, false);
868         } else {
869                 ret = tpm_pm_resume(dev);
870                 if (!ret)
871                         tpm_do_selftest(chip);
872         }
873         return ret;
874 } /* tpm_stm_i2c_pm_resume() */
875 #endif
876
877 static const struct i2c_device_id tpm_stm_i2c_id[] = {
878         {TPM_ST33_I2C, 0},
879         {}
880 };
881 MODULE_DEVICE_TABLE(i2c, tpm_stm_i2c_id);
882
883 #ifdef CONFIG_OF
884 static const struct of_device_id of_st33zp24_i2c_match[] = {
885         { .compatible = "st,st33zp24-i2c", },
886         {}
887 };
888 MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
889 #endif
890
891 static SIMPLE_DEV_PM_OPS(tpm_stm_i2c_ops, tpm_stm_i2c_pm_suspend,
892                          tpm_stm_i2c_pm_resume);
893
894 static struct i2c_driver tpm_stm_i2c_driver = {
895         .driver = {
896                 .owner = THIS_MODULE,
897                 .name = TPM_ST33_I2C,
898                 .pm = &tpm_stm_i2c_ops,
899                 .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
900         },
901         .probe = tpm_stm_i2c_probe,
902         .remove = tpm_stm_i2c_remove,
903         .id_table = tpm_stm_i2c_id
904 };
905
906 module_i2c_driver(tpm_stm_i2c_driver);
907
908 MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
909 MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
910 MODULE_VERSION("1.2.1");
911 MODULE_LICENSE("GPL");