Skip to content

Commit c004686

Browse files
Dirk EibachJean Delvare
authored andcommitted
hwmon: (ads1015) Make gain and datarate configurable
Configuration for ads1015 gain and datarate is possible via devicetree or platform data. This is a followup patch to previous ads1015 patches on Jean Delvares tree. Signed-off-by: Dirk Eibach <eibach@gdsys.de> Signed-off-by: Jean Delvare <khali@linux-fr.org>
1 parent fdf241a commit c004686

4 files changed

Lines changed: 211 additions & 85 deletions

File tree

‎Documentation/devicetree/bindings/hwmon/ads1015.txt‎

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,69 @@ This device is a 12-bit A-D converter with 4 inputs.
55
The inputs can be used single ended or in certain differential combinations.
66

77
For configuration all possible combinations are mapped to 8 channels:
8-
0: Voltage over AIN0 and AIN1.
9-
1: Voltage over AIN0 and AIN3.
10-
2: Voltage over AIN1 and AIN3.
11-
3: Voltage over AIN2 and AIN3.
12-
4: Voltage over AIN0 and GND.
13-
5: Voltage over AIN1 and GND.
14-
6: Voltage over AIN2 and GND.
15-
7: Voltage over AIN3 and GND.
16-
17-
Optional properties:
18-
19-
- exported-channels : exported_channels is a bitmask that specifies which
20-
channels should be accessable by the user.
21-
22-
Example:
23-
ads1015@49 {
24-
compatible = "ti,ads1015";
25-
reg = <0x49>;
26-
exported-channels = <0x14>;
27-
};
28-
29-
In this example only channel 2 and 4 would be accessable by the user.
8+
0: Voltage over AIN0 and AIN1.
9+
1: Voltage over AIN0 and AIN3.
10+
2: Voltage over AIN1 and AIN3.
11+
3: Voltage over AIN2 and AIN3.
12+
4: Voltage over AIN0 and GND.
13+
5: Voltage over AIN1 and GND.
14+
6: Voltage over AIN2 and GND.
15+
7: Voltage over AIN3 and GND.
16+
17+
Each channel can be configured individually:
18+
- pga is the programmable gain amplifier (values are full scale)
19+
0: +/- 6.144 V
20+
1: +/- 4.096 V
21+
2: +/- 2.048 V (default)
22+
3: +/- 1.024 V
23+
4: +/- 0.512 V
24+
5: +/- 0.256 V
25+
- data_rate in samples per second
26+
0: 128
27+
1: 250
28+
2: 490
29+
3: 920
30+
4: 1600 (default)
31+
5: 2400
32+
6: 3300
33+
34+
1) The /ads1015 node
35+
36+
Required properties:
37+
38+
- compatible : must be "ti,ads1015"
39+
- reg : I2C bus address of the device
40+
- #address-cells : must be <1>
41+
- #size-cells : must be <0>
42+
43+
The node contains child nodes for each channel that the platform uses.
44+
45+
Example ADS1015 node:
46+
47+
ads1015@49 {
48+
compatible = "ti,ads1015";
49+
reg = <0x49>;
50+
#address-cells = <1>;
51+
#size-cells = <0>;
52+
53+
[ child node definitions... ]
54+
}
55+
56+
2) channel nodes
57+
58+
Required properties:
59+
60+
- reg : the channel number
61+
62+
Optional properties:
63+
64+
- ti,gain : the programmable gain amplifier setting
65+
- ti,datarate : the converter data rate
66+
67+
Example ADS1015 channel node:
68+
69+
channel@4 {
70+
reg = <4>;
71+
ti,gain = <3>;
72+
ti,datarate = <5>;
73+
};

‎Documentation/hwmon/ads1015‎

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This device is a 12-bit A-D converter with 4 inputs.
1919

2020
The inputs can be used single ended or in certain differential combinations.
2121

22-
The inputs can be exported to 8 sysfs input files in0_input - in7_input:
22+
The inputs can be made available by 8 sysfs input files in0_input - in7_input:
2323
in0: Voltage over AIN0 and AIN1.
2424
in1: Voltage over AIN0 and AIN3.
2525
in2: Voltage over AIN1 and AIN3.
@@ -29,39 +29,44 @@ in5: Voltage over AIN1 and GND.
2929
in6: Voltage over AIN2 and GND.
3030
in7: Voltage over AIN3 and GND.
3131

32-
Which inputs are exported can be configured using platform data or devicetree.
32+
Which inputs are available can be configured using platform data or devicetree.
3333

3434
By default all inputs are exported.
3535

3636
Platform Data
3737
-------------
3838

39-
In linux/i2c/ads1015.h platform data is defined as:
40-
41-
struct ads1015_platform_data {
42-
unsigned int exported_channels;
43-
};
44-
45-
exported_channels is a bitmask that specifies which inputs should be exported.
39+
In linux/i2c/ads1015.h platform data is defined, channel_data contains
40+
configuration data for the used input combinations:
41+
- pga is the programmable gain amplifier (values are full scale)
42+
0: +/- 6.144 V
43+
1: +/- 4.096 V
44+
2: +/- 2.048 V
45+
3: +/- 1.024 V
46+
4: +/- 0.512 V
47+
5: +/- 0.256 V
48+
- data_rate in samples per second
49+
0: 128
50+
1: 250
51+
2: 490
52+
3: 920
53+
4: 1600
54+
5: 2400
55+
6: 3300
4656

4757
Example:
4858
struct ads1015_platform_data data = {
49-
.exported_channels = (1 << 2) | (1 << 4)
59+
.channel_data = {
60+
[2] = { .enabled = true, .pga = 1, .data_rate = 0 },
61+
[4] = { .enabled = true, .pga = 4, .data_rate = 5 },
62+
}
5063
};
5164

52-
In this case only in2_input and in4_input would be created.
65+
In this case only in2_input (FS +/- 4.096 V, 128 SPS) and in4_input
66+
(FS +/- 0.512 V, 2400 SPS) would be created.
5367

5468
Devicetree
5569
----------
5670

57-
The ads1015 node may have an "exported-channels" property.
58-
exported_channels is a bitmask that specifies which inputs should be exported.
59-
60-
Example:
61-
ads1015@49 {
62-
compatible = "ti,ads1015";
63-
reg = <0x49>;
64-
exported-channels = < 0x14 >;
65-
};
66-
67-
In this case only in2_input and in4_input would be created.
71+
Configuration is also possible via devicetree:
72+
Documentation/devicetree/bindings/hwmon/ads1015.txt

‎drivers/hwmon/ads1015.c‎

Lines changed: 109 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ enum {
4545
static const unsigned int fullscale_table[8] = {
4646
6144, 4096, 2048, 1024, 512, 256, 256, 256 };
4747

48-
#define ADS1015_CONFIG_CHANNELS 8
48+
/* Data rates in samples per second */
49+
static const unsigned int data_rate_table[8] = {
50+
128, 250, 490, 920, 1600, 2400, 3300, 3300 };
51+
4952
#define ADS1015_DEFAULT_CHANNELS 0xff
53+
#define ADS1015_DEFAULT_PGA 2
54+
#define ADS1015_DEFAULT_DATA_RATE 4
5055

5156
struct ads1015_data {
5257
struct device *hwmon_dev;
5358
struct mutex update_lock; /* mutex protect updates */
59+
struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
5460
};
5561

5662
static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
@@ -71,40 +77,42 @@ static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
7177
{
7278
u16 config;
7379
s16 conversion;
74-
unsigned int pga;
75-
int fullscale;
76-
unsigned int k;
7780
struct ads1015_data *data = i2c_get_clientdata(client);
81+
unsigned int pga = data->channel_data[channel].pga;
82+
int fullscale;
83+
unsigned int data_rate = data->channel_data[channel].data_rate;
84+
unsigned int conversion_time_ms;
7885
int res;
7986

8087
mutex_lock(&data->update_lock);
8188

82-
/* get fullscale voltage */
89+
/* get channel parameters */
8390
res = ads1015_read_reg(client, ADS1015_CONFIG);
8491
if (res < 0)
8592
goto err_unlock;
8693
config = res;
87-
pga = (config >> 9) & 0x0007;
8894
fullscale = fullscale_table[pga];
95+
conversion_time_ms = DIV_ROUND_UP(1000, data_rate_table[data_rate]);
8996

90-
/* set channel and start single conversion */
91-
config &= ~(0x0007 << 12);
92-
config |= (1 << 15) | (1 << 8) | (channel & 0x0007) << 12;
97+
/* setup and start single conversion */
98+
config &= 0x001f;
99+
config |= (1 << 15) | (1 << 8);
100+
config |= (channel & 0x0007) << 12;
101+
config |= (pga & 0x0007) << 9;
102+
config |= (data_rate & 0x0007) << 5;
93103

94-
/* wait until conversion finished */
95104
res = ads1015_write_reg(client, ADS1015_CONFIG, config);
96105
if (res < 0)
97106
goto err_unlock;
98-
for (k = 0; k < 5; ++k) {
99-
msleep(1);
100-
res = ads1015_read_reg(client, ADS1015_CONFIG);
101-
if (res < 0)
102-
goto err_unlock;
103-
config = res;
104-
if (config & (1 << 15))
105-
break;
106-
}
107-
if (k == 5) {
107+
108+
/* wait until conversion finished */
109+
msleep(conversion_time_ms);
110+
res = ads1015_read_reg(client, ADS1015_CONFIG);
111+
if (res < 0)
112+
goto err_unlock;
113+
config = res;
114+
if (!(config & (1 << 15))) {
115+
/* conversion not finished in time */
108116
res = -EIO;
109117
goto err_unlock;
110118
}
@@ -160,43 +168,104 @@ static int ads1015_remove(struct i2c_client *client)
160168
int k;
161169

162170
hwmon_device_unregister(data->hwmon_dev);
163-
for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k)
171+
for (k = 0; k < ADS1015_CHANNELS; ++k)
164172
device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
165173
kfree(data);
166174
return 0;
167175
}
168176

169-
static unsigned int ads1015_get_exported_channels(struct i2c_client *client)
170-
{
171-
struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
172177
#ifdef CONFIG_OF
173-
struct device_node *np = client->dev.of_node;
174-
const __be32 *of_channels;
175-
int of_channels_size;
178+
static int ads1015_get_channels_config_of(struct i2c_client *client)
179+
{
180+
struct ads1015_data *data = i2c_get_clientdata(client);
181+
struct device_node *node;
182+
183+
if (!client->dev.of_node
184+
|| !of_get_next_child(client->dev.of_node, NULL))
185+
return -EINVAL;
186+
187+
for_each_child_of_node(client->dev.of_node, node) {
188+
const __be32 *property;
189+
int len;
190+
unsigned int channel;
191+
unsigned int pga = ADS1015_DEFAULT_PGA;
192+
unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
193+
194+
property = of_get_property(node, "reg", &len);
195+
if (!property || len != sizeof(int)) {
196+
dev_err(&client->dev, "invalid reg on %s\n",
197+
node->full_name);
198+
continue;
199+
}
200+
201+
channel = be32_to_cpup(property);
202+
if (channel > ADS1015_CHANNELS) {
203+
dev_err(&client->dev,
204+
"invalid channel index %d on %s\n",
205+
channel, node->full_name);
206+
continue;
207+
}
208+
209+
property = of_get_property(node, "ti,gain", &len);
210+
if (property && len == sizeof(int)) {
211+
pga = be32_to_cpup(property);
212+
if (pga > 6) {
213+
dev_err(&client->dev,
214+
"invalid gain on %s\n",
215+
node->full_name);
216+
}
217+
}
218+
219+
property = of_get_property(node, "ti,datarate", &len);
220+
if (property && len == sizeof(int)) {
221+
data_rate = be32_to_cpup(property);
222+
if (data_rate > 7) {
223+
dev_err(&client->dev,
224+
"invalid data_rate on %s\n",
225+
node->full_name);
226+
}
227+
}
228+
229+
data->channel_data[channel].enabled = true;
230+
data->channel_data[channel].pga = pga;
231+
data->channel_data[channel].data_rate = data_rate;
232+
}
233+
234+
return 0;
235+
}
176236
#endif
177237

238+
static void ads1015_get_channels_config(struct i2c_client *client)
239+
{
240+
unsigned int k;
241+
struct ads1015_data *data = i2c_get_clientdata(client);
242+
struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
243+
178244
/* prefer platform data */
179-
if (pdata)
180-
return pdata->exported_channels;
245+
if (pdata) {
246+
memcpy(data->channel_data, pdata->channel_data,
247+
sizeof(data->channel_data));
248+
return;
249+
}
181250

182251
#ifdef CONFIG_OF
183-
/* fallback on OF */
184-
of_channels = of_get_property(np, "exported-channels",
185-
&of_channels_size);
186-
if (of_channels && (of_channels_size == sizeof(*of_channels)))
187-
return be32_to_cpup(of_channels);
252+
if (!ads1015_get_channels_config_of(client))
253+
return;
188254
#endif
189255

190256
/* fallback on default configuration */
191-
return ADS1015_DEFAULT_CHANNELS;
257+
for (k = 0; k < ADS1015_CHANNELS; ++k) {
258+
data->channel_data[k].enabled = true;
259+
data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
260+
data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
261+
}
192262
}
193263

194264
static int ads1015_probe(struct i2c_client *client,
195265
const struct i2c_device_id *id)
196266
{
197267
struct ads1015_data *data;
198268
int err;
199-
unsigned int exported_channels;
200269
unsigned int k;
201270

202271
data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL);
@@ -209,9 +278,9 @@ static int ads1015_probe(struct i2c_client *client,
209278
mutex_init(&data->update_lock);
210279

211280
/* build sysfs attribute group */
212-
exported_channels = ads1015_get_exported_channels(client);
213-
for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k) {
214-
if (!(exported_channels & (1<<k)))
281+
ads1015_get_channels_config(client);
282+
for (k = 0; k < ADS1015_CHANNELS; ++k) {
283+
if (!data->channel_data[k].enabled)
215284
continue;
216285
err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
217286
if (err)
@@ -227,7 +296,7 @@ static int ads1015_probe(struct i2c_client *client,
227296
return 0;
228297

229298
exit_remove:
230-
for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k)
299+
for (k = 0; k < ADS1015_CHANNELS; ++k)
231300
device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
232301
exit_free:
233302
kfree(data);

0 commit comments

Comments
 (0)