Skip to content

Commit 8a5c191

Browse files
pmeerwjic23
authored andcommitted
staging:iio:hmc5843: Add pointer to i2c client to data struct
and use it to simplify code Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent c2b2db7 commit 8a5c191

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

drivers/staging/iio/magnetometer/hmc5843.c

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ struct hmc5843_chip_info {
182182

183183
/* Each client has this additional data */
184184
struct hmc5843_data {
185+
struct i2c_client *client;
185186
struct mutex lock;
186187
u8 rate;
187188
u8 meas_conf;
@@ -200,31 +201,28 @@ static s32 hmc5843_configure(struct i2c_client *client,
200201
}
201202

202203
/* Return the measurement value from the specified channel */
203-
static int hmc5843_read_measurement(struct iio_dev *indio_dev,
204-
int address,
205-
int *val)
204+
static int hmc5843_read_measurement(struct hmc5843_data *data,
205+
int address, int *val)
206206
{
207-
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
208-
struct hmc5843_data *data = iio_priv(indio_dev);
209207
s32 result;
210208
int tries = 150;
211209

212210
mutex_lock(&data->lock);
213211
while (tries-- > 0) {
214-
result = i2c_smbus_read_byte_data(client,
212+
result = i2c_smbus_read_byte_data(data->client,
215213
HMC5843_STATUS_REG);
216214
if (result & HMC5843_DATA_READY)
217215
break;
218216
msleep(20);
219217
}
220218

221219
if (tries < 0) {
222-
dev_err(&client->dev, "data not ready\n");
220+
dev_err(&data->client->dev, "data not ready\n");
223221
mutex_unlock(&data->lock);
224222
return -EIO;
225223
}
226224

227-
result = i2c_smbus_read_word_swapped(client, address);
225+
result = i2c_smbus_read_word_swapped(data->client, address);
228226
mutex_unlock(&data->lock);
229227
if (result < 0)
230228
return -EINVAL;
@@ -318,15 +316,13 @@ static IIO_DEVICE_ATTR(operating_mode,
318316
* and BN.
319317
*
320318
*/
321-
static s32 hmc5843_set_meas_conf(struct i2c_client *client,
322-
u8 meas_conf)
319+
static s32 hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf)
323320
{
324-
struct iio_dev *indio_dev = i2c_get_clientdata(client);
325-
struct hmc5843_data *data = iio_priv(indio_dev);
326321
u8 reg_val;
327322
reg_val = (meas_conf & HMC5843_MEAS_CONF_MASK) |
328323
(data->rate << HMC5843_RATE_OFFSET);
329-
return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
324+
return i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
325+
reg_val);
330326
}
331327

332328
static ssize_t hmc5843_show_measurement_configuration(struct device *dev,
@@ -344,7 +340,6 @@ static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
344340
size_t count)
345341
{
346342
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
347-
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
348343
struct hmc5843_data *data = iio_priv(indio_dev);
349344
unsigned long meas_conf = 0;
350345
int error;
@@ -357,7 +352,7 @@ static ssize_t hmc5843_set_measurement_configuration(struct device *dev,
357352

358353
mutex_lock(&data->lock);
359354
dev_dbg(dev, "set measurement configuration to %lu\n", meas_conf);
360-
if (hmc5843_set_meas_conf(client, meas_conf)) {
355+
if (hmc5843_set_meas_conf(data, meas_conf)) {
361356
count = -EINVAL;
362357
goto exit;
363358
}
@@ -396,21 +391,19 @@ static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
396391

397392
static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_sampling_frequencies_available);
398393

399-
static s32 hmc5843_set_rate(struct i2c_client *client,
400-
u8 rate)
394+
static s32 hmc5843_set_rate(struct hmc5843_data *data, u8 rate)
401395
{
402-
struct iio_dev *indio_dev = i2c_get_clientdata(client);
403-
struct hmc5843_data *data = iio_priv(indio_dev);
404396
u8 reg_val;
405397

406398
if (rate >= HMC5843_RATE_NOT_USED) {
407-
dev_err(&client->dev,
399+
dev_err(&data->client->dev,
408400
"data output rate is not supported\n");
409401
return -EINVAL;
410402
}
411403

412404
reg_val = data->meas_conf | (rate << HMC5843_RATE_OFFSET);
413-
return i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_A, reg_val);
405+
return i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
406+
reg_val);
414407
}
415408

416409
static int hmc5843_check_sampling_frequency(struct hmc5843_data *data,
@@ -433,20 +426,19 @@ static ssize_t hmc5843_set_sampling_frequency(struct device *dev,
433426
{
434427

435428
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
436-
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
437429
struct hmc5843_data *data = iio_priv(indio_dev);
438430
int rate;
439431

440432
rate = hmc5843_check_sampling_frequency(data, buf);
441433
if (rate < 0) {
442-
dev_err(&client->dev,
434+
dev_err(&data->client->dev,
443435
"sampling frequency is not supported\n");
444436
return rate;
445437
}
446438

447439
mutex_lock(&data->lock);
448440
dev_dbg(dev, "set rate to %d\n", rate);
449-
if (hmc5843_set_rate(client, rate)) {
441+
if (hmc5843_set_rate(data, rate)) {
450442
count = -EINVAL;
451443
goto exit;
452444
}
@@ -461,12 +453,11 @@ static ssize_t hmc5843_show_sampling_frequency(struct device *dev,
461453
struct device_attribute *attr, char *buf)
462454
{
463455
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
464-
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
465456
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
466457
struct hmc5843_data *data = iio_priv(indio_dev);
467458
s32 rate;
468459

469-
rate = i2c_smbus_read_byte_data(client, this_attr->address);
460+
rate = i2c_smbus_read_byte_data(data->client, this_attr->address);
470461
if (rate < 0)
471462
return rate;
472463
rate = (rate & HMC5843_RATE_BITMASK) >> HMC5843_RATE_OFFSET;
@@ -497,7 +488,6 @@ static ssize_t hmc5843_set_range_gain(struct device *dev,
497488
size_t count)
498489
{
499490
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
500-
struct i2c_client *client = to_i2c_client(indio_dev->dev.parent);
501491
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
502492
struct hmc5843_data *data = iio_priv(indio_dev);
503493
unsigned long range = 0;
@@ -518,7 +508,7 @@ static ssize_t hmc5843_set_range_gain(struct device *dev,
518508

519509
data->range = range;
520510
range = range << HMC5843_RANGE_GAIN_OFFSET;
521-
if (i2c_smbus_write_byte_data(client, this_attr->address, range))
511+
if (i2c_smbus_write_byte_data(data->client, this_attr->address, range))
522512
count = -EINVAL;
523513

524514
exit:
@@ -541,9 +531,7 @@ static int hmc5843_read_raw(struct iio_dev *indio_dev,
541531

542532
switch (mask) {
543533
case IIO_CHAN_INFO_RAW:
544-
return hmc5843_read_measurement(indio_dev,
545-
chan->address,
546-
val);
534+
return hmc5843_read_measurement(data, chan->address, val);
547535
case IIO_CHAN_INFO_SCALE:
548536
*val = 0;
549537
*val2 = data->variant->regval_to_nanoscale[data->range];
@@ -621,8 +609,8 @@ static void hmc5843_init_client(struct i2c_client *client,
621609
data->variant = &hmc5843_chip_info_tbl[id->driver_data];
622610
indio_dev->channels = data->variant->channels;
623611
indio_dev->num_channels = 3;
624-
hmc5843_set_meas_conf(client, data->meas_conf);
625-
hmc5843_set_rate(client, data->rate);
612+
hmc5843_set_meas_conf(data, data->meas_conf);
613+
hmc5843_set_rate(data, data->rate);
626614
hmc5843_configure(client, data->operating_mode);
627615
i2c_smbus_write_byte_data(client, HMC5843_CONFIG_REG_B, data->range);
628616
mutex_init(&data->lock);
@@ -649,6 +637,7 @@ static int hmc5843_probe(struct i2c_client *client,
649637

650638
/* default settings at probe */
651639
data = iio_priv(indio_dev);
640+
data->client = client;
652641
data->meas_conf = HMC5843_MEAS_CONF_NORMAL;
653642
data->range = HMC5843_RANGE_GAIN_DEFAULT;
654643
data->operating_mode = HMC5843_MODE_CONVERSION_CONTINUOUS;
@@ -687,10 +676,10 @@ static int hmc5843_suspend(struct device *dev)
687676

688677
static int hmc5843_resume(struct device *dev)
689678
{
690-
struct i2c_client *client = to_i2c_client(dev);
691-
struct hmc5843_data *data = iio_priv(i2c_get_clientdata(client));
679+
struct hmc5843_data *data = iio_priv(i2c_get_clientdata(
680+
to_i2c_client(dev)));
692681

693-
hmc5843_configure(client, data->operating_mode);
682+
hmc5843_configure(data->client, data->operating_mode);
694683

695684
return 0;
696685
}

0 commit comments

Comments
 (0)