Skip to content

Commit 7a1d220

Browse files
committed
hwmon: (lm90) Introduce function to update configuration register
The code to update the configuration register is repeated several times. Move it into a separate function. At the same time, un-inline lm90_select_remote_channel() and leave it up to the compiler to decide what to do with it. Also remove the 'client' argument from lm90_select_remote_channel() and from lm90_write_convrate() and take it from struct lm90_data instead where needed. This patch reduces code size by more than 800 bytes on x86_64. Cc: Boyang Yu <byu@arista.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent b849e5d commit 7a1d220

1 file changed

Lines changed: 40 additions & 49 deletions

File tree

drivers/hwmon/lm90.c

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,21 @@ static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
543543
return (newh << 8) | l;
544544
}
545545

546+
static int lm90_update_confreg(struct lm90_data *data, u8 config)
547+
{
548+
if (data->config != config) {
549+
int err;
550+
551+
err = i2c_smbus_write_byte_data(data->client,
552+
LM90_REG_W_CONFIG1,
553+
config);
554+
if (err)
555+
return err;
556+
data->config = config;
557+
}
558+
return 0;
559+
}
560+
546561
/*
547562
* client->update_lock must be held when calling this function (unless we are
548563
* in detection or initialization steps), and while a remote channel other
@@ -551,53 +566,37 @@ static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
551566
* various registers have different meanings as a result of selecting a
552567
* non-default remote channel.
553568
*/
554-
static inline int lm90_select_remote_channel(struct i2c_client *client,
555-
struct lm90_data *data,
556-
int channel)
569+
static int lm90_select_remote_channel(struct lm90_data *data, int channel)
557570
{
571+
int err = 0;
572+
558573
if (data->kind == max6696) {
559574
u8 config = data->config & ~0x08;
560-
int err;
561575

562576
if (channel)
563577
config |= 0x08;
564-
if (data->config != config) {
565-
err = i2c_smbus_write_byte_data(client,
566-
LM90_REG_W_CONFIG1,
567-
config);
568-
if (err)
569-
return err;
570-
data->config = config;
571-
}
578+
err = lm90_update_confreg(data, config);
572579
}
573-
return 0;
580+
return err;
574581
}
575582

576-
static int lm90_write_convrate(struct i2c_client *client,
577-
struct lm90_data *data, int val)
583+
static int lm90_write_convrate(struct lm90_data *data, int val)
578584
{
579585
u8 config = data->config;
580586
int err;
581587

582588
/* Save config and pause conversion */
583589
if (data->flags & LM90_PAUSE_FOR_CONFIG) {
584-
config |= 0x40;
585-
if (data->config != config) {
586-
err = i2c_smbus_write_byte_data(client,
587-
LM90_REG_W_CONFIG1,
588-
config);
589-
if (err < 0)
590-
return err;
591-
}
590+
err = lm90_update_confreg(data, config | 0x40);
591+
if (err < 0)
592+
return err;
592593
}
593594

594595
/* Set conv rate */
595-
err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
596+
err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
596597

597598
/* Revert change to config */
598-
if (data->config != config)
599-
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
600-
data->config);
599+
lm90_update_confreg(data, config);
601600

602601
return err;
603602
}
@@ -622,7 +621,7 @@ static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
622621
if (interval >= update_interval * 3 / 4)
623622
break;
624623

625-
err = lm90_write_convrate(client, data, i);
624+
err = lm90_write_convrate(data, i);
626625
data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
627626
return err;
628627
}
@@ -693,7 +692,7 @@ static int lm90_update_limits(struct device *dev)
693692
}
694693

695694
if (data->kind == max6696) {
696-
val = lm90_select_remote_channel(client, data, 1);
695+
val = lm90_select_remote_channel(data, 1);
697696
if (val < 0)
698697
return val;
699698

@@ -717,7 +716,7 @@ static int lm90_update_limits(struct device *dev)
717716
return val;
718717
data->temp11[REMOTE2_HIGH] = val << 8;
719718

720-
lm90_select_remote_channel(client, data, 0);
719+
lm90_select_remote_channel(data, 0);
721720
}
722721

723722
return 0;
@@ -777,19 +776,19 @@ static int lm90_update_device(struct device *dev)
777776
data->alarms = val; /* lower 8 bit of alarms */
778777

779778
if (data->kind == max6696) {
780-
val = lm90_select_remote_channel(client, data, 1);
779+
val = lm90_select_remote_channel(data, 1);
781780
if (val < 0)
782781
return val;
783782

784783
val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
785784
LM90_REG_R_REMOTE_TEMPL);
786785
if (val < 0) {
787-
lm90_select_remote_channel(client, data, 0);
786+
lm90_select_remote_channel(data, 0);
788787
return val;
789788
}
790789
data->temp11[REMOTE2_TEMP] = val;
791790

792-
lm90_select_remote_channel(client, data, 0);
791+
lm90_select_remote_channel(data, 0);
793792

794793
val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
795794
if (val < 0)
@@ -805,10 +804,7 @@ static int lm90_update_device(struct device *dev)
805804
!(data->alarms & data->alert_alarms)) {
806805
if (data->config & 0x80) {
807806
dev_dbg(&client->dev, "Re-enabling ALERT#\n");
808-
data->config &= ~0x80;
809-
i2c_smbus_write_byte_data(client,
810-
LM90_REG_W_CONFIG1,
811-
data->config);
807+
lm90_update_confreg(data, data->config & ~0x80);
812808
}
813809
}
814810

@@ -1026,7 +1022,7 @@ static int lm90_set_temp11(struct lm90_data *data, int index, long val)
10261022
else
10271023
data->temp11[index] = temp_to_s8(val) << 8;
10281024

1029-
lm90_select_remote_channel(client, data, index >= 3);
1025+
lm90_select_remote_channel(data, index >= 3);
10301026
err = i2c_smbus_write_byte_data(client, regp->high,
10311027
data->temp11[index] >> 8);
10321028
if (err < 0)
@@ -1035,7 +1031,7 @@ static int lm90_set_temp11(struct lm90_data *data, int index, long val)
10351031
err = i2c_smbus_write_byte_data(client, regp->low,
10361032
data->temp11[index] & 0xff);
10371033

1038-
lm90_select_remote_channel(client, data, 0);
1034+
lm90_select_remote_channel(data, 0);
10391035
return err;
10401036
}
10411037

@@ -1084,9 +1080,9 @@ static int lm90_set_temp8(struct lm90_data *data, int index, long val)
10841080
else
10851081
data->temp8[index] = temp_to_s8(val);
10861082

1087-
lm90_select_remote_channel(client, data, index >= 6);
1083+
lm90_select_remote_channel(data, index >= 6);
10881084
err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1089-
lm90_select_remote_channel(client, data, 0);
1085+
lm90_select_remote_channel(data, 0);
10901086

10911087
return err;
10921088
}
@@ -1625,7 +1621,7 @@ static void lm90_restore_conf(void *_data)
16251621
struct i2c_client *client = data->client;
16261622

16271623
/* Restore initial configuration */
1628-
lm90_write_convrate(client, data, data->convrate_orig);
1624+
lm90_write_convrate(data, data->convrate_orig);
16291625
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
16301626
data->config_orig);
16311627
}
@@ -1671,10 +1667,7 @@ static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
16711667
config &= ~0x08;
16721668

16731669
config &= 0xBF; /* run */
1674-
if (config != data->config) { /* Only write if changed */
1675-
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1676-
data->config = config;
1677-
}
1670+
lm90_update_confreg(data, config);
16781671

16791672
return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
16801673
}
@@ -1909,9 +1902,7 @@ static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
19091902
if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
19101903
(alarms & data->alert_alarms)) {
19111904
dev_dbg(&client->dev, "Disabling ALERT#\n");
1912-
data->config |= 0x80;
1913-
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1914-
data->config);
1905+
lm90_update_confreg(data, data->config | 0x80);
19151906
}
19161907
} else {
19171908
dev_info(&client->dev, "Everything OK\n");

0 commit comments

Comments
 (0)