Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: temperature: tmp117: improve fallback capabilities

Don't error if the device-id found don't match the device-id for the
TMP117 sensor since other TMPxxx might be compatible to the TMP117. The
fallback mechanism tries to gather the required information from the
of_device_id or from the i2c_client information.

The commit also prepares the driver for adding new devices more easily
by making use of switch-case at the relevant parts.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
Link: https://lore.kernel.org/r/20230228090518.529811-3-m.felsch@pengutronix.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Marco Felsch and committed by
Jonathan Cameron
07cc6899 70ed9ced

+34 -10
+34 -10
drivers/iio/temperature/tmp117.c
··· 16 16 #include <linux/types.h> 17 17 #include <linux/kernel.h> 18 18 #include <linux/limits.h> 19 + #include <linux/property.h> 19 20 20 21 #include <linux/iio/iio.h> 21 22 ··· 116 115 117 116 static int tmp117_identify(struct i2c_client *client) 118 117 { 118 + const struct i2c_device_id *id; 119 + unsigned long match_data; 119 120 int dev_id; 120 121 121 122 dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID); 122 123 if (dev_id < 0) 123 124 return dev_id; 124 - if (dev_id != TMP117_DEVICE_ID) { 125 - dev_err(&client->dev, "TMP117 not found\n"); 126 - return -ENODEV; 125 + 126 + switch (dev_id) { 127 + case TMP117_DEVICE_ID: 128 + return dev_id; 127 129 } 128 - return 0; 130 + 131 + dev_info(&client->dev, "Unknown device id (0x%x), use fallback compatible\n", 132 + dev_id); 133 + 134 + match_data = (uintptr_t)device_get_match_data(&client->dev); 135 + if (match_data) 136 + return match_data; 137 + 138 + id = i2c_client_get_device_id(client); 139 + if (id) 140 + return id->driver_data; 141 + 142 + dev_err(&client->dev, "Failed to identify unsupported device\n"); 143 + 144 + return -ENODEV; 129 145 } 130 146 131 147 static int tmp117_probe(struct i2c_client *client) 132 148 { 133 149 struct tmp117_data *data; 134 150 struct iio_dev *indio_dev; 135 - int ret; 151 + int ret, dev_id; 136 152 137 153 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 138 154 return -EOPNOTSUPP; ··· 157 139 ret = tmp117_identify(client); 158 140 if (ret < 0) 159 141 return ret; 142 + 143 + dev_id = ret; 160 144 161 145 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 162 146 if (!indio_dev) ··· 168 148 data->client = client; 169 149 data->calibbias = 0; 170 150 171 - indio_dev->name = "tmp117"; 172 151 indio_dev->modes = INDIO_DIRECT_MODE; 173 152 indio_dev->info = &tmp117_info; 174 153 175 - indio_dev->channels = tmp117_channels; 176 - indio_dev->num_channels = ARRAY_SIZE(tmp117_channels); 154 + switch (dev_id) { 155 + case TMP117_DEVICE_ID: 156 + indio_dev->channels = tmp117_channels; 157 + indio_dev->num_channels = ARRAY_SIZE(tmp117_channels); 158 + indio_dev->name = "tmp117"; 159 + break; 160 + } 177 161 178 162 return devm_iio_device_register(&client->dev, indio_dev); 179 163 } 180 164 181 165 static const struct of_device_id tmp117_of_match[] = { 182 - { .compatible = "ti,tmp117", }, 166 + { .compatible = "ti,tmp117", .data = (void *)TMP117_DEVICE_ID }, 183 167 { } 184 168 }; 185 169 MODULE_DEVICE_TABLE(of, tmp117_of_match); 186 170 187 171 static const struct i2c_device_id tmp117_id[] = { 188 - { "tmp117", 0 }, 172 + { "tmp117", TMP117_DEVICE_ID }, 189 173 { } 190 174 }; 191 175 MODULE_DEVICE_TABLE(i2c, tmp117_id);