Skip to content

Commit 059ff0f

Browse files
Jakko3jic23
authored andcommitted
iio: magnetometer: yas530: Add function pointers to "chip_info"
Add function pointers to the "chip_info" structure to ease the handling of different YAS variants. In the function yas5xx_probe(), the function call for "measure_offsets" was added as a conditional "if (ci->measure_offsets)". This is a preparatory step for YAS537, as this variant doesn't need an offset measurement. Signed-off-by: Jakob Hauser <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Link: https://lore.kernel.org/r/4bd3f96262e0132b7f9720521a801da3c18abd95.1660337264.git.jahau@rocketmail.com Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 2d6676e commit 059ff0f

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

drivers/iio/magnetometer/yamaha-yas530.c

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ struct yas5xx;
131131
* @scaling_val2: scaling value for IIO_CHAN_INFO_SCALE
132132
* @t_ref: number of counts at reference temperature 20 °C
133133
* @min_temp_x10: starting point of temperature counting in 1/10:s degrees Celsius
134+
* @get_measure: function pointer to get a measurement
135+
* @get_calibration_data: function pointer to get calibration data
136+
* @dump_calibration: function pointer to dump calibration for debugging
137+
* @measure_offsets: function pointer to measure the offsets
138+
* @power_on: function pointer to power-on procedure
134139
*
135140
* The "t_ref" value for YAS532/533 is known from the Android driver.
136141
* For YAS530 it was approximately measured.
@@ -147,12 +152,17 @@ struct yas5xx_chip_info {
147152
u32 scaling_val2;
148153
u16 t_ref;
149154
s16 min_temp_x10;
155+
int (*get_measure)(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo);
156+
int (*get_calibration_data)(struct yas5xx *yas5xx);
157+
void (*dump_calibration)(struct yas5xx *yas5xx);
158+
int (*measure_offsets)(struct yas5xx *yas5xx);
159+
int (*power_on)(struct yas5xx *yas5xx);
150160
};
151161

152162
/**
153163
* struct yas5xx - state container for the YAS5xx driver
154164
* @dev: parent device pointer
155-
* @chip_info: device-specific data
165+
* @chip_info: device-specific data and function pointers
156166
* @version: device version
157167
* @calibration: calibration settings from the OTP storage
158168
* @hard_offsets: offsets for each axis measured with initcoil actuated
@@ -461,7 +471,7 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
461471
case IIO_CHAN_INFO_PROCESSED:
462472
case IIO_CHAN_INFO_RAW:
463473
pm_runtime_get_sync(yas5xx->dev);
464-
ret = yas530_get_measure(yas5xx, &t, &x, &y, &z);
474+
ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
465475
pm_runtime_mark_last_busy(yas5xx->dev);
466476
pm_runtime_put_autosuspend(yas5xx->dev);
467477
if (ret)
@@ -497,11 +507,12 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
497507
static void yas5xx_fill_buffer(struct iio_dev *indio_dev)
498508
{
499509
struct yas5xx *yas5xx = iio_priv(indio_dev);
510+
const struct yas5xx_chip_info *ci = yas5xx->chip_info;
500511
s32 t, x, y, z;
501512
int ret;
502513

503514
pm_runtime_get_sync(yas5xx->dev);
504-
ret = yas530_get_measure(yas5xx, &t, &x, &y, &z);
515+
ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
505516
pm_runtime_mark_last_busy(yas5xx->dev);
506517
pm_runtime_put_autosuspend(yas5xx->dev);
507518
if (ret) {
@@ -916,6 +927,11 @@ static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
916927
.scaling_val2 = 100000000, /* picotesla to Gauss */
917928
.t_ref = 182, /* counts */
918929
.min_temp_x10 = -620, /* 1/10:s degrees Celsius */
930+
.get_measure = yas530_get_measure,
931+
.get_calibration_data = yas530_get_calibration_data,
932+
.dump_calibration = yas530_dump_calibration,
933+
.measure_offsets = yas530_measure_offsets,
934+
.power_on = yas530_power_on,
919935
},
920936
[yas532] = {
921937
.devid = YAS532_DEVICE_ID,
@@ -926,6 +942,11 @@ static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
926942
.scaling_val2 = 100000, /* nanotesla to Gauss */
927943
.t_ref = 390, /* counts */
928944
.min_temp_x10 = -500, /* 1/10:s degrees Celsius */
945+
.get_measure = yas530_get_measure,
946+
.get_calibration_data = yas532_get_calibration_data,
947+
.dump_calibration = yas530_dump_calibration,
948+
.measure_offsets = yas530_measure_offsets,
949+
.power_on = yas530_power_on,
929950
},
930951
[yas533] = {
931952
.devid = YAS532_DEVICE_ID,
@@ -936,6 +957,11 @@ static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
936957
.scaling_val2 = 100000, /* nanotesla to Gauss */
937958
.t_ref = 390, /* counts */
938959
.min_temp_x10 = -500, /* 1/10:s degrees Celsius */
960+
.get_measure = yas530_get_measure,
961+
.get_calibration_data = yas532_get_calibration_data,
962+
.dump_calibration = yas530_dump_calibration,
963+
.measure_offsets = yas530_measure_offsets,
964+
.power_on = yas530_power_on,
939965
},
940966
};
941967

@@ -1007,34 +1033,25 @@ static int yas5xx_probe(struct i2c_client *i2c,
10071033
goto assert_reset;
10081034
}
10091035

1010-
switch (ci->devid) {
1011-
case YAS530_DEVICE_ID:
1012-
ret = yas530_get_calibration_data(yas5xx);
1013-
if (ret)
1014-
goto assert_reset;
1015-
break;
1016-
case YAS532_DEVICE_ID:
1017-
ret = yas532_get_calibration_data(yas5xx);
1018-
if (ret)
1019-
goto assert_reset;
1020-
break;
1021-
default:
1022-
ret = -ENODEV;
1023-
dev_err(dev, "unhandled device ID %02x\n", ci->devid);
1036+
ret = ci->get_calibration_data(yas5xx);
1037+
if (ret)
10241038
goto assert_reset;
1025-
}
10261039

10271040
dev_info(dev, "detected %s %s\n", ci->product_name,
10281041
ci->version_names[yas5xx->version]);
10291042

1030-
yas530_dump_calibration(yas5xx);
1031-
ret = yas530_power_on(yas5xx);
1032-
if (ret)
1033-
goto assert_reset;
1034-
ret = yas530_measure_offsets(yas5xx);
1043+
ci->dump_calibration(yas5xx);
1044+
1045+
ret = ci->power_on(yas5xx);
10351046
if (ret)
10361047
goto assert_reset;
10371048

1049+
if (ci->measure_offsets) {
1050+
ret = ci->measure_offsets(yas5xx);
1051+
if (ret)
1052+
goto assert_reset;
1053+
}
1054+
10381055
indio_dev->info = &yas5xx_info;
10391056
indio_dev->available_scan_masks = yas5xx_scan_masks;
10401057
indio_dev->modes = INDIO_DIRECT_MODE;
@@ -1114,6 +1131,7 @@ static int yas5xx_runtime_resume(struct device *dev)
11141131
{
11151132
struct iio_dev *indio_dev = dev_get_drvdata(dev);
11161133
struct yas5xx *yas5xx = iio_priv(indio_dev);
1134+
const struct yas5xx_chip_info *ci = yas5xx->chip_info;
11171135
int ret;
11181136

11191137
ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
@@ -1130,7 +1148,7 @@ static int yas5xx_runtime_resume(struct device *dev)
11301148
usleep_range(31000, 40000);
11311149
gpiod_set_value_cansleep(yas5xx->reset, 0);
11321150

1133-
ret = yas530_power_on(yas5xx);
1151+
ret = ci->power_on(yas5xx);
11341152
if (ret) {
11351153
dev_err(dev, "cannot power on\n");
11361154
goto out_reset;

0 commit comments

Comments
 (0)