Skip to content

Commit 6562793

Browse files
gwendalcrEnric Balletbo i Serra
authored andcommitted
iio: cros_ec: Expose hwfifo_timeout
Expose EC minimal interrupt period through buffer/hwfifo_timeout: - Maximal timeout is limited to 65s. - When timeout for all sensors is set to 0, EC will not send events, even if the sensor sampling rate is greater than 0. Rename frequency to sampling_frequency to match IIO ABI. Signed-off-by: Gwendal Grignou <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Signed-off-by: Enric Balletbo i Serra <[email protected]>
1 parent 2861be4 commit 6562793

File tree

5 files changed

+82
-30
lines changed

5 files changed

+82
-30
lines changed

drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
236236
if (ret)
237237
return ret;
238238

239+
iio_buffer_set_attrs(indio_dev->buffer, cros_ec_sensor_fifo_attributes);
240+
239241
indio_dev->info = &ec_sensors_info;
240242
state = iio_priv(indio_dev);
241243
for (channel = state->channels, i = CROS_EC_SENSOR_X;
@@ -247,7 +249,6 @@ static int cros_ec_sensors_probe(struct platform_device *pdev)
247249
BIT(IIO_CHAN_INFO_CALIBSCALE);
248250
channel->info_mask_shared_by_all =
249251
BIT(IIO_CHAN_INFO_SCALE) |
250-
BIT(IIO_CHAN_INFO_FREQUENCY) |
251252
BIT(IIO_CHAN_INFO_SAMP_FREQ);
252253
channel->info_mask_shared_by_all_available =
253254
BIT(IIO_CHAN_INFO_SAMP_FREQ);

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/iio/common/cros_ec_sensors_core.h>
1212
#include <linux/iio/iio.h>
1313
#include <linux/iio/kfifo_buf.h>
14+
#include <linux/iio/sysfs.h>
1415
#include <linux/iio/trigger_consumer.h>
1516
#include <linux/iio/triggered_buffer.h>
1617
#include <linux/kernel.h>
@@ -83,6 +84,77 @@ static void get_default_min_max_freq(enum motionsensor_type type,
8384
}
8485
}
8586

87+
static int cros_ec_sensor_set_ec_rate(struct cros_ec_sensors_core_state *st,
88+
int rate)
89+
{
90+
int ret;
91+
92+
if (rate > U16_MAX)
93+
rate = U16_MAX;
94+
95+
mutex_lock(&st->cmd_lock);
96+
st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
97+
st->param.ec_rate.data = rate;
98+
ret = cros_ec_motion_send_host_cmd(st, 0);
99+
mutex_unlock(&st->cmd_lock);
100+
return ret;
101+
}
102+
103+
static ssize_t cros_ec_sensor_set_report_latency(struct device *dev,
104+
struct device_attribute *attr,
105+
const char *buf, size_t len)
106+
{
107+
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
108+
struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
109+
int integer, fract, ret;
110+
int latency;
111+
112+
ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
113+
if (ret)
114+
return ret;
115+
116+
/* EC rate is in ms. */
117+
latency = integer * 1000 + fract / 1000;
118+
ret = cros_ec_sensor_set_ec_rate(st, latency);
119+
if (ret < 0)
120+
return ret;
121+
122+
return len;
123+
}
124+
125+
static ssize_t cros_ec_sensor_get_report_latency(struct device *dev,
126+
struct device_attribute *attr,
127+
char *buf)
128+
{
129+
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
130+
struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
131+
int latency, ret;
132+
133+
mutex_lock(&st->cmd_lock);
134+
st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
135+
st->param.ec_rate.data = EC_MOTION_SENSE_NO_VALUE;
136+
137+
ret = cros_ec_motion_send_host_cmd(st, 0);
138+
latency = st->resp->ec_rate.ret;
139+
mutex_unlock(&st->cmd_lock);
140+
if (ret < 0)
141+
return ret;
142+
143+
return sprintf(buf, "%d.%06u\n",
144+
latency / 1000,
145+
(latency % 1000) * 1000);
146+
}
147+
148+
static IIO_DEVICE_ATTR(hwfifo_timeout, 0644,
149+
cros_ec_sensor_get_report_latency,
150+
cros_ec_sensor_set_report_latency, 0);
151+
152+
const struct attribute *cros_ec_sensor_fifo_attributes[] = {
153+
&iio_dev_attr_hwfifo_timeout.dev_attr.attr,
154+
NULL,
155+
};
156+
EXPORT_SYMBOL_GPL(cros_ec_sensor_fifo_attributes);
157+
86158
int cros_ec_sensors_push_data(struct iio_dev *indio_dev,
87159
s16 *data,
88160
s64 timestamp)
@@ -631,18 +703,6 @@ int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
631703

632704
switch (mask) {
633705
case IIO_CHAN_INFO_SAMP_FREQ:
634-
st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
635-
st->param.ec_rate.data =
636-
EC_MOTION_SENSE_NO_VALUE;
637-
638-
ret = cros_ec_motion_send_host_cmd(st, 0);
639-
if (ret)
640-
break;
641-
642-
*val = st->resp->ec_rate.ret;
643-
ret = IIO_VAL_INT;
644-
break;
645-
case IIO_CHAN_INFO_FREQUENCY:
646706
st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
647707
st->param.sensor_odr.data =
648708
EC_MOTION_SENSE_NO_VALUE;
@@ -712,7 +772,7 @@ int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
712772
int ret;
713773

714774
switch (mask) {
715-
case IIO_CHAN_INFO_FREQUENCY:
775+
case IIO_CHAN_INFO_SAMP_FREQ:
716776
st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
717777
st->param.sensor_odr.data = val;
718778

@@ -721,15 +781,6 @@ int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
721781

722782
ret = cros_ec_motion_send_host_cmd(st, 0);
723783
break;
724-
case IIO_CHAN_INFO_SAMP_FREQ:
725-
st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
726-
st->param.ec_rate.data = val;
727-
728-
ret = cros_ec_motion_send_host_cmd(st, 0);
729-
if (ret)
730-
break;
731-
st->curr_sampl_freq = val;
732-
break;
733784
default:
734785
ret = -EINVAL;
735786
break;

drivers/iio/light/cros_ec_light_prox.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
183183
if (ret)
184184
return ret;
185185

186+
iio_buffer_set_attrs(indio_dev->buffer, cros_ec_sensor_fifo_attributes);
187+
186188
indio_dev->info = &cros_ec_light_prox_info;
187189
state = iio_priv(indio_dev);
188190
state->core.type = state->core.resp->info.type;
@@ -191,8 +193,7 @@ static int cros_ec_light_prox_probe(struct platform_device *pdev)
191193

192194
/* Common part */
193195
channel->info_mask_shared_by_all =
194-
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
195-
BIT(IIO_CHAN_INFO_FREQUENCY);
196+
BIT(IIO_CHAN_INFO_SAMP_FREQ);
196197
channel->info_mask_shared_by_all_available =
197198
BIT(IIO_CHAN_INFO_SAMP_FREQ);
198199
channel->scan_type.realbits = CROS_EC_SENSOR_BITS;

drivers/iio/pressure/cros_ec_baro.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ static int cros_ec_baro_probe(struct platform_device *pdev)
140140
if (ret)
141141
return ret;
142142

143+
iio_buffer_set_attrs(indio_dev->buffer, cros_ec_sensor_fifo_attributes);
144+
143145
indio_dev->info = &cros_ec_baro_info;
144146
state = iio_priv(indio_dev);
145147
state->core.type = state->core.resp->info.type;
@@ -149,8 +151,7 @@ static int cros_ec_baro_probe(struct platform_device *pdev)
149151
channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
150152
channel->info_mask_shared_by_all =
151153
BIT(IIO_CHAN_INFO_SCALE) |
152-
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
153-
BIT(IIO_CHAN_INFO_FREQUENCY);
154+
BIT(IIO_CHAN_INFO_SAMP_FREQ);
154155
channel->info_mask_shared_by_all_available =
155156
BIT(IIO_CHAN_INFO_SAMP_FREQ);
156157
channel->scan_type.realbits = CROS_EC_SENSOR_BITS;

include/linux/iio/common/cros_ec_sensors_core.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ typedef irqreturn_t (*cros_ec_sensors_capture_t)(int irq, void *p);
5050
* the timestamp. The timestamp is always last and
5151
* is always 8-byte aligned.
5252
* @read_ec_sensors_data: function used for accessing sensors values
53-
* @cuur_sampl_freq: current sampling period
5453
*/
5554
struct cros_ec_sensors_core_state {
5655
struct cros_ec_device *ec;
@@ -73,8 +72,6 @@ struct cros_ec_sensors_core_state {
7372
int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
7473
unsigned long scan_mask, s16 *data);
7574

76-
int curr_sampl_freq;
77-
7875
/* Table of known available frequencies : 0, Min and Max in mHz */
7976
int frequencies[3];
8077
};
@@ -116,5 +113,6 @@ int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
116113

117114
/* List of extended channel specification for all sensors */
118115
extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
116+
extern const struct attribute *cros_ec_sensor_fifo_attributes[];
119117

120118
#endif /* __CROS_EC_SENSORS_CORE_H */

0 commit comments

Comments
 (0)