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

iio: dac: ti-dac082s085: Read chip spec from device table

The two properties unique to each supported chip, resolution and number
of channels, are currently gleaned from the chip's name.
E.g. dac102s085 is a dual channel 10-bit DAC.
^^^
This was deemed unmaintainable by the subsystem maintainer once the
driver is extended to support further chips, hence it was requested
to add an explicit table for chip-specific information and use an
enum to reference into it.

This adds 17 LoC without any immediate gain, so make the change in a
separate commit which can be reverted if we determine in 10 years that
it was unnecessary.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lukas Wunner and committed by
Jonathan Cameron
f98677cf 61011264

+26 -9
+26 -9
drivers/iio/dac/ti-dac082s085.c
··· 20 20 #include <linux/regulator/consumer.h> 21 21 #include <linux/spi/spi.h> 22 22 23 + enum { dual_8bit, dual_10bit, dual_12bit, quad_8bit, quad_10bit, quad_12bit }; 24 + 25 + struct ti_dac_spec { 26 + u8 num_channels; 27 + u8 resolution; 28 + }; 29 + 30 + static const struct ti_dac_spec ti_dac_spec[] = { 31 + [dual_8bit] = { .num_channels = 2, .resolution = 8 }, 32 + [dual_10bit] = { .num_channels = 2, .resolution = 10 }, 33 + [dual_12bit] = { .num_channels = 2, .resolution = 12 }, 34 + [quad_8bit] = { .num_channels = 4, .resolution = 8 }, 35 + [quad_10bit] = { .num_channels = 4, .resolution = 10 }, 36 + [quad_12bit] = { .num_channels = 4, .resolution = 12 }, 37 + }; 38 + 23 39 /** 24 40 * struct ti_dac_chip - TI DAC chip 25 41 * @lock: protects write sequences ··· 262 246 static int ti_dac_probe(struct spi_device *spi) 263 247 { 264 248 struct device *dev = &spi->dev; 249 + const struct ti_dac_spec *spec; 265 250 struct ti_dac_chip *ti_dac; 266 251 struct iio_dev *indio_dev; 267 252 int ret; ··· 284 267 spi_message_init_with_transfers(&ti_dac->mesg, &ti_dac->xfer, 1); 285 268 ti_dac->mesg.spi = spi; 286 269 287 - ret = sscanf(spi->modalias, "dac%2hhu%1d", 288 - &ti_dac->resolution, &indio_dev->num_channels); 289 - WARN_ON(ret != 2); 270 + spec = &ti_dac_spec[spi_get_device_id(spi)->driver_data]; 271 + indio_dev->num_channels = spec->num_channels; 272 + ti_dac->resolution = spec->resolution; 290 273 291 274 ti_dac->vref = devm_regulator_get(dev, "vref"); 292 275 if (IS_ERR(ti_dac->vref)) ··· 342 325 #endif 343 326 344 327 static const struct spi_device_id ti_dac_spi_id[] = { 345 - { "dac082s085" }, 346 - { "dac102s085" }, 347 - { "dac122s085" }, 348 - { "dac084s085" }, 349 - { "dac104s085" }, 350 - { "dac124s085" }, 328 + { "dac082s085", dual_8bit }, 329 + { "dac102s085", dual_10bit }, 330 + { "dac122s085", dual_12bit }, 331 + { "dac084s085", quad_8bit }, 332 + { "dac104s085", quad_10bit }, 333 + { "dac124s085", quad_12bit }, 351 334 { } 352 335 }; 353 336 MODULE_DEVICE_TABLE(spi, ti_dac_spi_id);