Skip to content

Commit ae7b02a

Browse files
Fabien Lahouderejic23
authored andcommitted
iio: common: cros_ec_sensors: Expose cros_ec_sensors frequency range via iio sysfs
Embedded controller return minimum and maximum frequencies, unfortunately we have no way to know the step for all available frequencies. Even if not complete, we can return a list of known values using the standard read_avail callback (IIO_CHAN_INFO_SAMP_FREQ) to provide them to userland. Now cros_ec_* sensors provides frequencies values in sysfs like this: "0 min max". 0 is always true to disable the sensor. Default frequencies are provided for earlier protocol. Signed-off-by: Nick Vaccaro <[email protected]> Signed-off-by: Fabien Lahoudere <[email protected]> [rebased on top of iio/testing and solved conflicts] Signed-off-by: Enric Balletbo i Serra <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 9566cb1 commit ae7b02a

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ static int cros_ec_sensors_write(struct iio_dev *indio_dev,
215215
static const struct iio_info ec_sensors_info = {
216216
.read_raw = &cros_ec_sensors_read,
217217
.write_raw = &cros_ec_sensors_write,
218+
.read_avail = &cros_ec_sensors_core_read_avail,
218219
};
219220

220221
static int cros_ec_sensors_probe(struct platform_device *pdev)
@@ -252,6 +253,8 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
252253
BIT(IIO_CHAN_INFO_SCALE) |
253254
BIT(IIO_CHAN_INFO_FREQUENCY) |
254255
BIT(IIO_CHAN_INFO_SAMP_FREQ);
256+
channel->info_mask_shared_by_all_available =
257+
BIT(IIO_CHAN_INFO_SAMP_FREQ);
255258
channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
256259
channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
257260
channel->scan_index = i;

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
5050
return ret;
5151
}
5252

53+
static void get_default_min_max_freq(enum motionsensor_type type,
54+
u32 *min_freq,
55+
u32 *max_freq)
56+
{
57+
switch (type) {
58+
case MOTIONSENSE_TYPE_ACCEL:
59+
case MOTIONSENSE_TYPE_GYRO:
60+
*min_freq = 12500;
61+
*max_freq = 100000;
62+
break;
63+
case MOTIONSENSE_TYPE_MAG:
64+
*min_freq = 5000;
65+
*max_freq = 25000;
66+
break;
67+
case MOTIONSENSE_TYPE_PROX:
68+
case MOTIONSENSE_TYPE_LIGHT:
69+
*min_freq = 100;
70+
*max_freq = 50000;
71+
break;
72+
case MOTIONSENSE_TYPE_BARO:
73+
*min_freq = 250;
74+
*max_freq = 20000;
75+
break;
76+
case MOTIONSENSE_TYPE_ACTIVITY:
77+
default:
78+
*min_freq = 0;
79+
*max_freq = 0;
80+
break;
81+
}
82+
}
83+
5384
int cros_ec_sensors_core_init(struct platform_device *pdev,
5485
struct iio_dev *indio_dev,
5586
bool physical_device)
@@ -104,6 +135,19 @@ int cros_ec_sensors_core_init(struct platform_device *pdev,
104135

105136
/* Set sign vector, only used for backward compatibility. */
106137
memset(state->sign, 1, CROS_EC_SENSOR_MAX_AXIS);
138+
139+
/* 0 is a correct value used to stop the device */
140+
state->frequencies[0] = 0;
141+
if (state->msg->version < 3) {
142+
get_default_min_max_freq(state->resp->info.type,
143+
&state->frequencies[1],
144+
&state->frequencies[2]);
145+
} else {
146+
state->frequencies[1] =
147+
state->resp->info_3.min_frequency;
148+
state->frequencies[2] =
149+
state->resp->info_3.max_frequency;
150+
}
107151
}
108152

109153
return 0;
@@ -471,6 +515,27 @@ int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
471515
}
472516
EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
473517

518+
int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
519+
struct iio_chan_spec const *chan,
520+
const int **vals,
521+
int *type,
522+
int *length,
523+
long mask)
524+
{
525+
struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
526+
527+
switch (mask) {
528+
case IIO_CHAN_INFO_SAMP_FREQ:
529+
*length = ARRAY_SIZE(state->frequencies);
530+
*vals = (const int *)&state->frequencies;
531+
*type = IIO_VAL_INT;
532+
return IIO_AVAIL_LIST;
533+
}
534+
535+
return -EINVAL;
536+
}
537+
EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
538+
474539
int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
475540
struct iio_chan_spec const *chan,
476541
int val, int val2, long mask)

drivers/iio/light/cros_ec_light_prox.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ static int cros_ec_light_prox_write(struct iio_dev *indio_dev,
162162
static const struct iio_info cros_ec_light_prox_info = {
163163
.read_raw = &cros_ec_light_prox_read,
164164
.write_raw = &cros_ec_light_prox_write,
165+
.read_avail = &cros_ec_sensors_core_read_avail,
165166
};
166167

167168
static int cros_ec_light_prox_probe(struct platform_device *pdev)
@@ -196,6 +197,8 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
196197
channel->info_mask_shared_by_all =
197198
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
198199
BIT(IIO_CHAN_INFO_FREQUENCY);
200+
channel->info_mask_shared_by_all_available =
201+
BIT(IIO_CHAN_INFO_SAMP_FREQ);
199202
channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
200203
channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
201204
channel->scan_type.shift = 0;

include/linux/iio/common/cros_ec_sensors_core.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ struct cros_ec_sensors_core_state {
7373
unsigned long scan_mask, s16 *data);
7474

7575
int curr_sampl_freq;
76+
77+
/* Table of known available frequencies : 0, Min and Max in mHz */
78+
int frequencies[3];
7679
};
7780

7881
/**
@@ -153,6 +156,24 @@ int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
153156
struct iio_chan_spec const *chan,
154157
int *val, int *val2, long mask);
155158

159+
/**
160+
* cros_ec_sensors_core_read_avail() - get available values
161+
* @indio_dev: pointer to state information for device
162+
* @chan: channel specification structure table
163+
* @vals: list of available values
164+
* @type: type of data returned
165+
* @length: number of data returned in the array
166+
* @mask: specifies which values to be requested
167+
*
168+
* Return: an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST
169+
*/
170+
int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
171+
struct iio_chan_spec const *chan,
172+
const int **vals,
173+
int *type,
174+
int *length,
175+
long mask);
176+
156177
/**
157178
* cros_ec_sensors_core_write() - function to write a value to the sensor
158179
* @st: pointer to state information for device

0 commit comments

Comments
 (0)