aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/ads1015.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/ads1015.c')
-rw-r--r--drivers/hwmon/ads1015.c33
1 files changed, 16 insertions, 17 deletions
diff --git a/drivers/hwmon/ads1015.c b/drivers/hwmon/ads1015.c
index 7765e4f74ec5..1958f03efd7a 100644
--- a/drivers/hwmon/ads1015.c
+++ b/drivers/hwmon/ads1015.c
@@ -59,14 +59,11 @@ struct ads1015_data {
59 struct ads1015_channel_data channel_data[ADS1015_CHANNELS]; 59 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
60}; 60};
61 61
62static int ads1015_read_value(struct i2c_client *client, unsigned int channel, 62static int ads1015_read_adc(struct i2c_client *client, unsigned int channel)
63 int *value)
64{ 63{
65 u16 config; 64 u16 config;
66 s16 conversion;
67 struct ads1015_data *data = i2c_get_clientdata(client); 65 struct ads1015_data *data = i2c_get_clientdata(client);
68 unsigned int pga = data->channel_data[channel].pga; 66 unsigned int pga = data->channel_data[channel].pga;
69 int fullscale;
70 unsigned int data_rate = data->channel_data[channel].data_rate; 67 unsigned int data_rate = data->channel_data[channel].data_rate;
71 unsigned int conversion_time_ms; 68 unsigned int conversion_time_ms;
72 int res; 69 int res;
@@ -78,7 +75,6 @@ static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
78 if (res < 0) 75 if (res < 0)
79 goto err_unlock; 76 goto err_unlock;
80 config = res; 77 config = res;
81 fullscale = fullscale_table[pga];
82 conversion_time_ms = DIV_ROUND_UP(1000, data_rate_table[data_rate]); 78 conversion_time_ms = DIV_ROUND_UP(1000, data_rate_table[data_rate]);
83 79
84 /* setup and start single conversion */ 80 /* setup and start single conversion */
@@ -105,33 +101,36 @@ static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
105 } 101 }
106 102
107 res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION); 103 res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
108 if (res < 0)
109 goto err_unlock;
110 conversion = res;
111
112 mutex_unlock(&data->update_lock);
113
114 *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0);
115
116 return 0;
117 104
118err_unlock: 105err_unlock:
119 mutex_unlock(&data->update_lock); 106 mutex_unlock(&data->update_lock);
120 return res; 107 return res;
121} 108}
122 109
110static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel,
111 s16 reg)
112{
113 struct ads1015_data *data = i2c_get_clientdata(client);
114 unsigned int pga = data->channel_data[channel].pga;
115 int fullscale = fullscale_table[pga];
116
117 return DIV_ROUND_CLOSEST(reg * fullscale, 0x7ff0);
118}
119
123/* sysfs callback function */ 120/* sysfs callback function */
124static ssize_t show_in(struct device *dev, struct device_attribute *da, 121static ssize_t show_in(struct device *dev, struct device_attribute *da,
125 char *buf) 122 char *buf)
126{ 123{
127 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 124 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
128 struct i2c_client *client = to_i2c_client(dev); 125 struct i2c_client *client = to_i2c_client(dev);
129 int in;
130 int res; 126 int res;
127 int index = attr->index;
131 128
132 res = ads1015_read_value(client, attr->index, &in); 129 res = ads1015_read_adc(client, index);
130 if (res < 0)
131 return res;
133 132
134 return (res < 0) ? res : sprintf(buf, "%d\n", in); 133 return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res));
135} 134}
136 135
137static const struct sensor_device_attribute ads1015_in[] = { 136static const struct sensor_device_attribute ads1015_in[] = {