tpm: Fix null pointer dereference on chip register error path

If clk_enable is not defined and chip initialization
is canceled code hits null dereference.

Easily reproducible with vTPM init fail:
swtpm chardev --tpmstate dir=nonexistent_dir --tpm2 --vtpm-proxy

BUG: kernel NULL pointer dereference, address: 00000000
...
Call Trace:
tpm_chip_start+0x9d/0xa0 [tpm]
tpm_chip_register+0x10/0x1a0 [tpm]
vtpm_proxy_work+0x11/0x30 [tpm_vtpm_proxy]
process_one_work+0x214/0x5a0
worker_thread+0x134/0x3e0
? process_one_work+0x5a0/0x5a0
kthread+0xd4/0x100
? process_one_work+0x5a0/0x5a0
? kthread_park+0x90/0x90
ret_from_fork+0x19/0x24

Fixes: 719b7d81f204 ("tpm: introduce tpm_chip_start() and tpm_chip_stop()")
Cc: stable@vger.kernel.org # v5.1+
Signed-off-by: Milan Broz <gmazyland@gmail.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

authored by Milan Broz and committed by Jarkko Sakkinen 1e5ac630 4b6f2316

Changed files
+16 -7
drivers
char
+16 -7
drivers/char/tpm/tpm-chip.c
··· 77 77 return chip->ops->go_idle(chip); 78 78 } 79 79 80 + static void tpm_clk_enable(struct tpm_chip *chip) 81 + { 82 + if (chip->ops->clk_enable) 83 + chip->ops->clk_enable(chip, true); 84 + } 85 + 86 + static void tpm_clk_disable(struct tpm_chip *chip) 87 + { 88 + if (chip->ops->clk_enable) 89 + chip->ops->clk_enable(chip, false); 90 + } 91 + 80 92 /** 81 93 * tpm_chip_start() - power on the TPM 82 94 * @chip: a TPM chip to use ··· 101 89 { 102 90 int ret; 103 91 104 - if (chip->ops->clk_enable) 105 - chip->ops->clk_enable(chip, true); 92 + tpm_clk_enable(chip); 106 93 107 94 if (chip->locality == -1) { 108 95 ret = tpm_request_locality(chip); 109 96 if (ret) { 110 - chip->ops->clk_enable(chip, false); 97 + tpm_clk_disable(chip); 111 98 return ret; 112 99 } 113 100 } ··· 114 103 ret = tpm_cmd_ready(chip); 115 104 if (ret) { 116 105 tpm_relinquish_locality(chip); 117 - if (chip->ops->clk_enable) 118 - chip->ops->clk_enable(chip, false); 106 + tpm_clk_disable(chip); 119 107 return ret; 120 108 } 121 109 ··· 134 124 { 135 125 tpm_go_idle(chip); 136 126 tpm_relinquish_locality(chip); 137 - if (chip->ops->clk_enable) 138 - chip->ops->clk_enable(chip, false); 127 + tpm_clk_disable(chip); 139 128 } 140 129 EXPORT_SYMBOL_GPL(tpm_chip_stop); 141 130