Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2011 Jonathan Cameron |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * A reference industrial I/O driver to illustrate the functionality available. |
| 9 | * |
| 10 | * There are numerous real drivers to illustrate the finer points. |
| 11 | * The purpose of this driver is to provide a driver with far more comments |
| 12 | * and explanatory notes than any 'real' driver would have. |
| 13 | * Anyone starting out writing an IIO driver should first make sure they |
| 14 | * understand all of this driver except those bits specifically marked |
| 15 | * as being present to allow us to 'fake' the presence of hardware. |
| 16 | */ |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/module.h> |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 20 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
| 23 | #include <linux/iio/events.h> |
| 24 | #include <linux/iio/buffer.h> |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 25 | #include "iio_simple_dummy.h" |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * A few elements needed to fake a bus for this driver |
Peter Meerwald | 25c38aa | 2012-06-15 19:27:10 +0200 | [diff] [blame] | 29 | * Note instances parameter controls how many of these |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 30 | * dummy devices are registered. |
| 31 | */ |
| 32 | static unsigned instances = 1; |
Vladimirs Ambrosovs | 4dcaa5f | 2015-05-30 11:20:17 +0300 | [diff] [blame] | 33 | module_param(instances, uint, 0); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 34 | |
| 35 | /* Pointer array used to fake bus elements */ |
| 36 | static struct iio_dev **iio_dummy_devs; |
| 37 | |
| 38 | /* Fake a name for the part number, usually obtained from the id table */ |
| 39 | static const char *iio_dummy_part_number = "iio_dummy_part_no"; |
| 40 | |
| 41 | /** |
| 42 | * struct iio_dummy_accel_calibscale - realworld to register mapping |
| 43 | * @val: first value in read_raw - here integer part. |
| 44 | * @val2: second value in read_raw etc - here micro part. |
| 45 | * @regval: register value - magic device specific numbers. |
| 46 | */ |
| 47 | struct iio_dummy_accel_calibscale { |
| 48 | int val; |
| 49 | int val2; |
| 50 | int regval; /* what would be written to hardware */ |
| 51 | }; |
| 52 | |
| 53 | static const struct iio_dummy_accel_calibscale dummy_scales[] = { |
| 54 | { 0, 100, 0x8 }, /* 0.000100 */ |
| 55 | { 0, 133, 0x7 }, /* 0.000133 */ |
Alexander Stein | 569c4ac | 2012-12-19 17:56:00 +0000 | [diff] [blame] | 56 | { 733, 13, 0x9 }, /* 733.000013 */ |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
| 60 | |
| 61 | /* |
| 62 | * simple event - triggered when value rises above |
| 63 | * a threshold |
| 64 | */ |
| 65 | static const struct iio_event_spec iio_dummy_event = { |
| 66 | .type = IIO_EV_TYPE_THRESH, |
| 67 | .dir = IIO_EV_DIR_RISING, |
| 68 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
| 69 | }; |
| 70 | |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 71 | /* |
| 72 | * simple step detect event - triggered when a step is detected |
| 73 | */ |
| 74 | static const struct iio_event_spec step_detect_event = { |
Irina Tirdea | 17a2cbc | 2015-01-11 21:10:12 +0200 | [diff] [blame] | 75 | .type = IIO_EV_TYPE_CHANGE, |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 76 | .dir = IIO_EV_DIR_NONE, |
| 77 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * simple transition event - triggered when the reported running confidence |
| 82 | * value rises above a threshold value |
| 83 | */ |
| 84 | static const struct iio_event_spec iio_running_event = { |
| 85 | .type = IIO_EV_TYPE_THRESH, |
| 86 | .dir = IIO_EV_DIR_RISING, |
| 87 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * simple transition event - triggered when the reported walking confidence |
| 92 | * value falls under a threshold value |
| 93 | */ |
| 94 | static const struct iio_event_spec iio_walking_event = { |
| 95 | .type = IIO_EV_TYPE_THRESH, |
| 96 | .dir = IIO_EV_DIR_FALLING, |
| 97 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
| 98 | }; |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 99 | #endif |
| 100 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 101 | /* |
| 102 | * iio_dummy_channels - Description of available channels |
| 103 | * |
| 104 | * This array of structures tells the IIO core about what the device |
| 105 | * actually provides for a given channel. |
| 106 | */ |
Lars-Peter Clausen | f4e4b95 | 2012-08-09 08:51:00 +0100 | [diff] [blame] | 107 | static const struct iio_chan_spec iio_dummy_channels[] = { |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 108 | /* indexed ADC channel in_voltage0_raw etc */ |
| 109 | { |
| 110 | .type = IIO_VOLTAGE, |
| 111 | /* Channel has a numeric index of 0 */ |
| 112 | .indexed = 1, |
| 113 | .channel = 0, |
| 114 | /* What other information is available? */ |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 115 | .info_mask_separate = |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 116 | /* |
Lars-Peter Clausen | 41fd935 | 2012-04-15 17:41:27 +0100 | [diff] [blame] | 117 | * in_voltage0_raw |
| 118 | * Raw (unscaled no bias removal etc) measurement |
| 119 | * from the device. |
| 120 | */ |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 121 | BIT(IIO_CHAN_INFO_RAW) | |
Lars-Peter Clausen | 41fd935 | 2012-04-15 17:41:27 +0100 | [diff] [blame] | 122 | /* |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 123 | * in_voltage0_offset |
| 124 | * Offset for userspace to apply prior to scale |
| 125 | * when converting to standard units (microvolts) |
| 126 | */ |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 127 | BIT(IIO_CHAN_INFO_OFFSET) | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 128 | /* |
| 129 | * in_voltage0_scale |
| 130 | * Multipler for userspace to apply post offset |
| 131 | * when converting to standard units (microvolts) |
| 132 | */ |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 133 | BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 6a63aa0 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 134 | /* |
| 135 | * sampling_frequency |
| 136 | * The frequency in Hz at which the channels are sampled |
| 137 | */ |
| 138 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 139 | /* The ordering of elements in the buffer via an enum */ |
| 140 | .scan_index = voltage0, |
| 141 | .scan_type = { /* Description of storage in buffer */ |
| 142 | .sign = 'u', /* unsigned */ |
| 143 | .realbits = 13, /* 13 bits */ |
| 144 | .storagebits = 16, /* 16 bits used for storage */ |
| 145 | .shift = 0, /* zero shift */ |
| 146 | }, |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 147 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 148 | .event_spec = &iio_dummy_event, |
| 149 | .num_event_specs = 1, |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 150 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 151 | }, |
| 152 | /* Differential ADC channel in_voltage1-voltage2_raw etc*/ |
| 153 | { |
| 154 | .type = IIO_VOLTAGE, |
| 155 | .differential = 1, |
| 156 | /* |
| 157 | * Indexing for differential channels uses channel |
| 158 | * for the positive part, channel2 for the negative. |
| 159 | */ |
| 160 | .indexed = 1, |
| 161 | .channel = 1, |
| 162 | .channel2 = 2, |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 163 | /* |
Lars-Peter Clausen | 41fd935 | 2012-04-15 17:41:27 +0100 | [diff] [blame] | 164 | * in_voltage1-voltage2_raw |
| 165 | * Raw (unscaled no bias removal etc) measurement |
| 166 | * from the device. |
| 167 | */ |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 168 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Lars-Peter Clausen | 41fd935 | 2012-04-15 17:41:27 +0100 | [diff] [blame] | 169 | /* |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 170 | * in_voltage-voltage_scale |
| 171 | * Shared version of scale - shared by differential |
| 172 | * input channels of type IIO_VOLTAGE. |
| 173 | */ |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 174 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 6a63aa0 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 175 | /* |
| 176 | * sampling_frequency |
| 177 | * The frequency in Hz at which the channels are sampled |
| 178 | */ |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 179 | .scan_index = diffvoltage1m2, |
| 180 | .scan_type = { /* Description of storage in buffer */ |
| 181 | .sign = 's', /* signed */ |
| 182 | .realbits = 12, /* 12 bits */ |
| 183 | .storagebits = 16, /* 16 bits used for storage */ |
| 184 | .shift = 0, /* zero shift */ |
| 185 | }, |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 186 | }, |
| 187 | /* Differential ADC channel in_voltage3-voltage4_raw etc*/ |
| 188 | { |
| 189 | .type = IIO_VOLTAGE, |
| 190 | .differential = 1, |
| 191 | .indexed = 1, |
| 192 | .channel = 3, |
| 193 | .channel2 = 4, |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 194 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 195 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 6a63aa0 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 196 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 197 | .scan_index = diffvoltage3m4, |
| 198 | .scan_type = { |
| 199 | .sign = 's', |
| 200 | .realbits = 11, |
| 201 | .storagebits = 16, |
| 202 | .shift = 0, |
| 203 | }, |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 204 | }, |
| 205 | /* |
| 206 | * 'modified' (i.e. axis specified) acceleration channel |
| 207 | * in_accel_z_raw |
| 208 | */ |
| 209 | { |
| 210 | .type = IIO_ACCEL, |
| 211 | .modified = 1, |
| 212 | /* Channel 2 is use for modifiers */ |
| 213 | .channel2 = IIO_MOD_X, |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 214 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 215 | /* |
Jin Feng | ff3fc8e | 2013-04-05 05:51:00 +0100 | [diff] [blame] | 216 | * Internal bias and gain correction values. Applied |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 217 | * by the hardware or driver prior to userspace |
| 218 | * seeing the readings. Typically part of hardware |
| 219 | * calibration. |
| 220 | */ |
Jin Feng | ff3fc8e | 2013-04-05 05:51:00 +0100 | [diff] [blame] | 221 | BIT(IIO_CHAN_INFO_CALIBSCALE) | |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 222 | BIT(IIO_CHAN_INFO_CALIBBIAS), |
Jonathan Cameron | 6a63aa0 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 223 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 224 | .scan_index = accelx, |
| 225 | .scan_type = { /* Description of storage in buffer */ |
| 226 | .sign = 's', /* signed */ |
Peter Meerwald | 25c38aa | 2012-06-15 19:27:10 +0200 | [diff] [blame] | 227 | .realbits = 16, /* 16 bits */ |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 228 | .storagebits = 16, /* 16 bits used for storage */ |
| 229 | .shift = 0, /* zero shift */ |
| 230 | }, |
| 231 | }, |
| 232 | /* |
| 233 | * Convenience macro for timestamps. 4 is the index in |
| 234 | * the buffer. |
| 235 | */ |
| 236 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 237 | /* DAC channel out_voltage0_raw */ |
| 238 | { |
| 239 | .type = IIO_VOLTAGE, |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 240 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Lars-Peter Clausen | 4ae0301 | 2014-11-26 18:55:11 +0100 | [diff] [blame] | 241 | .scan_index = -1, /* No buffer support */ |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 242 | .output = 1, |
| 243 | .indexed = 1, |
| 244 | .channel = 0, |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 245 | }, |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 246 | { |
| 247 | .type = IIO_STEPS, |
| 248 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) | |
| 249 | BIT(IIO_CHAN_INFO_CALIBHEIGHT), |
| 250 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
Lars-Peter Clausen | 4ae0301 | 2014-11-26 18:55:11 +0100 | [diff] [blame] | 251 | .scan_index = -1, /* No buffer support */ |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 252 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
| 253 | .event_spec = &step_detect_event, |
| 254 | .num_event_specs = 1, |
| 255 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
| 256 | }, |
| 257 | { |
| 258 | .type = IIO_ACTIVITY, |
| 259 | .modified = 1, |
| 260 | .channel2 = IIO_MOD_RUNNING, |
| 261 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
Lars-Peter Clausen | 4ae0301 | 2014-11-26 18:55:11 +0100 | [diff] [blame] | 262 | .scan_index = -1, /* No buffer support */ |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 263 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
| 264 | .event_spec = &iio_running_event, |
| 265 | .num_event_specs = 1, |
| 266 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
| 267 | }, |
| 268 | { |
| 269 | .type = IIO_ACTIVITY, |
| 270 | .modified = 1, |
| 271 | .channel2 = IIO_MOD_WALKING, |
| 272 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
Lars-Peter Clausen | 4ae0301 | 2014-11-26 18:55:11 +0100 | [diff] [blame] | 273 | .scan_index = -1, /* No buffer support */ |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 274 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
| 275 | .event_spec = &iio_walking_event, |
| 276 | .num_event_specs = 1, |
| 277 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
| 278 | }, |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | /** |
| 282 | * iio_dummy_read_raw() - data read function. |
| 283 | * @indio_dev: the struct iio_dev associated with this device instance |
| 284 | * @chan: the channel whose data is to be read |
| 285 | * @val: first element of returned value (typically INT) |
| 286 | * @val2: second element of returned value (typically MICRO) |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 287 | * @mask: what we actually want to read as per the info_mask_* |
| 288 | * in iio_chan_spec. |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 289 | */ |
| 290 | static int iio_dummy_read_raw(struct iio_dev *indio_dev, |
| 291 | struct iio_chan_spec const *chan, |
| 292 | int *val, |
| 293 | int *val2, |
| 294 | long mask) |
| 295 | { |
| 296 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 297 | int ret = -EINVAL; |
| 298 | |
| 299 | mutex_lock(&st->lock); |
| 300 | switch (mask) { |
Lars-Peter Clausen | 41fd935 | 2012-04-15 17:41:27 +0100 | [diff] [blame] | 301 | case IIO_CHAN_INFO_RAW: /* magic value - channel value read */ |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 302 | switch (chan->type) { |
| 303 | case IIO_VOLTAGE: |
| 304 | if (chan->output) { |
| 305 | /* Set integer part to cached value */ |
| 306 | *val = st->dac_val; |
| 307 | ret = IIO_VAL_INT; |
| 308 | } else if (chan->differential) { |
| 309 | if (chan->channel == 1) |
| 310 | *val = st->differential_adc_val[0]; |
| 311 | else |
| 312 | *val = st->differential_adc_val[1]; |
| 313 | ret = IIO_VAL_INT; |
| 314 | } else { |
| 315 | *val = st->single_ended_adc_val; |
| 316 | ret = IIO_VAL_INT; |
| 317 | } |
| 318 | break; |
| 319 | case IIO_ACCEL: |
| 320 | *val = st->accel_val; |
| 321 | ret = IIO_VAL_INT; |
| 322 | break; |
| 323 | default: |
| 324 | break; |
| 325 | } |
| 326 | break; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 327 | case IIO_CHAN_INFO_PROCESSED: |
| 328 | switch (chan->type) { |
| 329 | case IIO_STEPS: |
| 330 | *val = st->steps; |
| 331 | ret = IIO_VAL_INT; |
| 332 | break; |
| 333 | case IIO_ACTIVITY: |
| 334 | switch (chan->channel2) { |
| 335 | case IIO_MOD_RUNNING: |
| 336 | *val = st->activity_running; |
| 337 | ret = IIO_VAL_INT; |
| 338 | break; |
| 339 | case IIO_MOD_WALKING: |
| 340 | *val = st->activity_walking; |
| 341 | ret = IIO_VAL_INT; |
| 342 | break; |
| 343 | default: |
| 344 | break; |
| 345 | } |
| 346 | break; |
| 347 | default: |
| 348 | break; |
| 349 | } |
| 350 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 351 | case IIO_CHAN_INFO_OFFSET: |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 352 | /* only single ended adc -> 7 */ |
| 353 | *val = 7; |
| 354 | ret = IIO_VAL_INT; |
| 355 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 356 | case IIO_CHAN_INFO_SCALE: |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 357 | switch (chan->type) { |
| 358 | case IIO_VOLTAGE: |
| 359 | switch (chan->differential) { |
| 360 | case 0: |
| 361 | /* only single ended adc -> 0.001333 */ |
| 362 | *val = 0; |
| 363 | *val2 = 1333; |
| 364 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 365 | break; |
| 366 | case 1: |
| 367 | /* all differential adc channels -> |
| 368 | * 0.000001344 */ |
| 369 | *val = 0; |
| 370 | *val2 = 1344; |
| 371 | ret = IIO_VAL_INT_PLUS_NANO; |
| 372 | } |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 373 | break; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 374 | default: |
| 375 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 376 | } |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 377 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 378 | case IIO_CHAN_INFO_CALIBBIAS: |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 379 | /* only the acceleration axis - read from cache */ |
| 380 | *val = st->accel_calibbias; |
| 381 | ret = IIO_VAL_INT; |
| 382 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 383 | case IIO_CHAN_INFO_CALIBSCALE: |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 384 | *val = st->accel_calibscale->val; |
| 385 | *val2 = st->accel_calibscale->val2; |
| 386 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 387 | break; |
Jonathan Cameron | 6a63aa0 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 388 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 389 | *val = 3; |
| 390 | *val2 = 33; |
| 391 | ret = IIO_VAL_INT_PLUS_NANO; |
| 392 | break; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 393 | case IIO_CHAN_INFO_ENABLE: |
| 394 | switch (chan->type) { |
| 395 | case IIO_STEPS: |
| 396 | *val = st->steps_enabled; |
| 397 | ret = IIO_VAL_INT; |
| 398 | break; |
| 399 | default: |
| 400 | break; |
| 401 | } |
| 402 | break; |
| 403 | case IIO_CHAN_INFO_CALIBHEIGHT: |
| 404 | switch (chan->type) { |
| 405 | case IIO_STEPS: |
| 406 | *val = st->height; |
| 407 | ret = IIO_VAL_INT; |
| 408 | break; |
| 409 | default: |
| 410 | break; |
| 411 | } |
| 412 | break; |
| 413 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 414 | default: |
| 415 | break; |
| 416 | } |
| 417 | mutex_unlock(&st->lock); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * iio_dummy_write_raw() - data write function. |
| 423 | * @indio_dev: the struct iio_dev associated with this device instance |
Alexander Stein | 569c4ac | 2012-12-19 17:56:00 +0000 | [diff] [blame] | 424 | * @chan: the channel whose data is to be written |
Peter Meerwald | 25c38aa | 2012-06-15 19:27:10 +0200 | [diff] [blame] | 425 | * @val: first element of value to set (typically INT) |
| 426 | * @val2: second element of value to set (typically MICRO) |
Jonathan Cameron | fa357a6 | 2013-02-19 21:12:21 +0000 | [diff] [blame] | 427 | * @mask: what we actually want to write as per the info_mask_* |
| 428 | * in iio_chan_spec. |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 429 | * |
| 430 | * Note that all raw writes are assumed IIO_VAL_INT and info mask elements |
| 431 | * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt |
| 432 | * in struct iio_info is provided by the driver. |
| 433 | */ |
| 434 | static int iio_dummy_write_raw(struct iio_dev *indio_dev, |
| 435 | struct iio_chan_spec const *chan, |
| 436 | int val, |
| 437 | int val2, |
| 438 | long mask) |
| 439 | { |
| 440 | int i; |
| 441 | int ret = 0; |
| 442 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 443 | |
| 444 | switch (mask) { |
Lars-Peter Clausen | 41fd935 | 2012-04-15 17:41:27 +0100 | [diff] [blame] | 445 | case IIO_CHAN_INFO_RAW: |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 446 | switch (chan->type) { |
| 447 | case IIO_VOLTAGE: |
| 448 | if (chan->output == 0) |
| 449 | return -EINVAL; |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 450 | |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 451 | /* Locking not required as writing single value */ |
| 452 | mutex_lock(&st->lock); |
| 453 | st->dac_val = val; |
| 454 | mutex_unlock(&st->lock); |
| 455 | return 0; |
| 456 | default: |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | case IIO_CHAN_INFO_PROCESSED: |
| 460 | switch (chan->type) { |
| 461 | case IIO_STEPS: |
| 462 | mutex_lock(&st->lock); |
| 463 | st->steps = val; |
| 464 | mutex_unlock(&st->lock); |
| 465 | return 0; |
| 466 | case IIO_ACTIVITY: |
| 467 | if (val < 0) |
| 468 | val = 0; |
| 469 | if (val > 100) |
| 470 | val = 100; |
| 471 | switch (chan->channel2) { |
| 472 | case IIO_MOD_RUNNING: |
| 473 | st->activity_running = val; |
| 474 | return 0; |
| 475 | case IIO_MOD_WALKING: |
| 476 | st->activity_walking = val; |
| 477 | return 0; |
| 478 | default: |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | break; |
| 482 | default: |
| 483 | return -EINVAL; |
| 484 | } |
Jin Feng | ff3fc8e | 2013-04-05 05:51:00 +0100 | [diff] [blame] | 485 | case IIO_CHAN_INFO_CALIBSCALE: |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 486 | mutex_lock(&st->lock); |
| 487 | /* Compare against table - hard matching here */ |
| 488 | for (i = 0; i < ARRAY_SIZE(dummy_scales); i++) |
| 489 | if (val == dummy_scales[i].val && |
| 490 | val2 == dummy_scales[i].val2) |
| 491 | break; |
| 492 | if (i == ARRAY_SIZE(dummy_scales)) |
| 493 | ret = -EINVAL; |
| 494 | else |
| 495 | st->accel_calibscale = &dummy_scales[i]; |
| 496 | mutex_unlock(&st->lock); |
| 497 | return ret; |
Jin Feng | ff3fc8e | 2013-04-05 05:51:00 +0100 | [diff] [blame] | 498 | case IIO_CHAN_INFO_CALIBBIAS: |
| 499 | mutex_lock(&st->lock); |
| 500 | st->accel_calibbias = val; |
| 501 | mutex_unlock(&st->lock); |
| 502 | return 0; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 503 | case IIO_CHAN_INFO_ENABLE: |
| 504 | switch (chan->type) { |
| 505 | case IIO_STEPS: |
| 506 | mutex_lock(&st->lock); |
| 507 | st->steps_enabled = val; |
| 508 | mutex_unlock(&st->lock); |
| 509 | return 0; |
| 510 | default: |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | case IIO_CHAN_INFO_CALIBHEIGHT: |
| 514 | switch (chan->type) { |
| 515 | case IIO_STEPS: |
| 516 | st->height = val; |
| 517 | return 0; |
| 518 | default: |
| 519 | return -EINVAL; |
| 520 | } |
Jin Feng | ff3fc8e | 2013-04-05 05:51:00 +0100 | [diff] [blame] | 521 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 522 | default: |
| 523 | return -EINVAL; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Device type specific information. |
| 529 | */ |
| 530 | static const struct iio_info iio_dummy_info = { |
| 531 | .driver_module = THIS_MODULE, |
| 532 | .read_raw = &iio_dummy_read_raw, |
| 533 | .write_raw = &iio_dummy_write_raw, |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 534 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 535 | .read_event_config = &iio_simple_dummy_read_event_config, |
| 536 | .write_event_config = &iio_simple_dummy_write_event_config, |
| 537 | .read_event_value = &iio_simple_dummy_read_event_value, |
| 538 | .write_event_value = &iio_simple_dummy_write_event_value, |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 539 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | /** |
| 543 | * iio_dummy_init_device() - device instance specific init |
| 544 | * @indio_dev: the iio device structure |
| 545 | * |
| 546 | * Most drivers have one of these to set up default values, |
| 547 | * reset the device to known state etc. |
| 548 | */ |
| 549 | static int iio_dummy_init_device(struct iio_dev *indio_dev) |
| 550 | { |
| 551 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 552 | |
| 553 | st->dac_val = 0; |
| 554 | st->single_ended_adc_val = 73; |
| 555 | st->differential_adc_val[0] = 33; |
| 556 | st->differential_adc_val[1] = -34; |
| 557 | st->accel_val = 34; |
| 558 | st->accel_calibbias = -7; |
| 559 | st->accel_calibscale = &dummy_scales[0]; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 560 | st->steps = 47; |
| 561 | st->activity_running = 98; |
| 562 | st->activity_walking = 4; |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * iio_dummy_probe() - device instance probe |
| 569 | * @index: an id number for this instance. |
| 570 | * |
| 571 | * Arguments are bus type specific. |
| 572 | * I2C: iio_dummy_probe(struct i2c_client *client, |
| 573 | * const struct i2c_device_id *id) |
| 574 | * SPI: iio_dummy_probe(struct spi_device *spi) |
| 575 | */ |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 576 | static int iio_dummy_probe(int index) |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 577 | { |
| 578 | int ret; |
| 579 | struct iio_dev *indio_dev; |
| 580 | struct iio_dummy_state *st; |
| 581 | |
| 582 | /* |
| 583 | * Allocate an IIO device. |
| 584 | * |
| 585 | * This structure contains all generic state |
| 586 | * information about the device instance. |
| 587 | * It also has a region (accessed by iio_priv() |
| 588 | * for chip specific state information. |
| 589 | */ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 590 | indio_dev = iio_device_alloc(sizeof(*st)); |
Cristina Opriceana | 8f94c31 | 2015-03-31 12:51:38 +0300 | [diff] [blame] | 591 | if (!indio_dev) { |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 592 | ret = -ENOMEM; |
| 593 | goto error_ret; |
| 594 | } |
| 595 | |
| 596 | st = iio_priv(indio_dev); |
| 597 | mutex_init(&st->lock); |
| 598 | |
| 599 | iio_dummy_init_device(indio_dev); |
| 600 | /* |
| 601 | * With hardware: Set the parent device. |
| 602 | * indio_dev->dev.parent = &spi->dev; |
| 603 | * indio_dev->dev.parent = &client->dev; |
| 604 | */ |
| 605 | |
| 606 | /* |
| 607 | * Make the iio_dev struct available to remove function. |
| 608 | * Bus equivalents |
| 609 | * i2c_set_clientdata(client, indio_dev); |
| 610 | * spi_set_drvdata(spi, indio_dev); |
| 611 | */ |
| 612 | iio_dummy_devs[index] = indio_dev; |
| 613 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 614 | /* |
| 615 | * Set the device name. |
| 616 | * |
| 617 | * This is typically a part number and obtained from the module |
| 618 | * id table. |
| 619 | * e.g. for i2c and spi: |
| 620 | * indio_dev->name = id->name; |
| 621 | * indio_dev->name = spi_get_device_id(spi)->name; |
| 622 | */ |
| 623 | indio_dev->name = iio_dummy_part_number; |
| 624 | |
| 625 | /* Provide description of available channels */ |
| 626 | indio_dev->channels = iio_dummy_channels; |
| 627 | indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels); |
| 628 | |
| 629 | /* |
| 630 | * Provide device type specific interface functions and |
| 631 | * constant data. |
| 632 | */ |
| 633 | indio_dev->info = &iio_dummy_info; |
| 634 | |
| 635 | /* Specify that device provides sysfs type interfaces */ |
| 636 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 637 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 638 | ret = iio_simple_dummy_events_register(indio_dev); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 639 | if (ret < 0) |
| 640 | goto error_free_device; |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 641 | |
Lars-Peter Clausen | 4ae0301 | 2014-11-26 18:55:11 +0100 | [diff] [blame] | 642 | ret = iio_simple_dummy_configure_buffer(indio_dev); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 643 | if (ret < 0) |
| 644 | goto error_unregister_events; |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 645 | |
Lars-Peter Clausen | 3fff227 | 2012-09-12 12:06:00 +0100 | [diff] [blame] | 646 | ret = iio_device_register(indio_dev); |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 647 | if (ret < 0) |
| 648 | goto error_unconfigure_buffer; |
| 649 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 650 | return 0; |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 651 | error_unconfigure_buffer: |
| 652 | iio_simple_dummy_unconfigure_buffer(indio_dev); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 653 | error_unregister_events: |
| 654 | iio_simple_dummy_events_unregister(indio_dev); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 655 | error_free_device: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 656 | iio_device_free(indio_dev); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 657 | error_ret: |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * iio_dummy_remove() - device instance removal function |
| 663 | * @index: device index. |
| 664 | * |
| 665 | * Parameters follow those of iio_dummy_probe for buses. |
| 666 | */ |
Vladimirs Ambrosovs | 62a90da | 2015-05-30 11:20:16 +0300 | [diff] [blame] | 667 | static void iio_dummy_remove(int index) |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 668 | { |
| 669 | /* |
| 670 | * Get a pointer to the device instance iio_dev structure |
| 671 | * from the bus subsystem. E.g. |
| 672 | * struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 673 | * struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 674 | */ |
| 675 | struct iio_dev *indio_dev = iio_dummy_devs[index]; |
| 676 | |
| 677 | /* Unregister the device */ |
| 678 | iio_device_unregister(indio_dev); |
| 679 | |
| 680 | /* Device specific code to power down etc */ |
| 681 | |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 682 | /* Buffered capture related cleanup */ |
Jonathan Cameron | 9ad2e2e | 2011-10-14 16:34:15 +0100 | [diff] [blame] | 683 | iio_simple_dummy_unconfigure_buffer(indio_dev); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 684 | |
Vladimirs Ambrosovs | 62a90da | 2015-05-30 11:20:16 +0300 | [diff] [blame] | 685 | iio_simple_dummy_events_unregister(indio_dev); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 686 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 687 | /* Free all structures */ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 688 | iio_device_free(indio_dev); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /** |
| 692 | * iio_dummy_init() - device driver registration |
| 693 | * |
| 694 | * Varies depending on bus type of the device. As there is no device |
| 695 | * here, call probe directly. For information on device registration |
| 696 | * i2c: |
| 697 | * Documentation/i2c/writing-clients |
| 698 | * spi: |
| 699 | * Documentation/spi/spi-summary |
| 700 | */ |
| 701 | static __init int iio_dummy_init(void) |
| 702 | { |
| 703 | int i, ret; |
Jimmy Picard | aff8945 | 2014-06-13 06:56:00 +0100 | [diff] [blame] | 704 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 705 | if (instances > 10) { |
| 706 | instances = 1; |
| 707 | return -EINVAL; |
| 708 | } |
Lars-Peter Clausen | 3fff227 | 2012-09-12 12:06:00 +0100 | [diff] [blame] | 709 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 710 | /* Fake a bus */ |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 711 | iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs), |
| 712 | GFP_KERNEL); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 713 | /* Here we have no actual device so call probe */ |
| 714 | for (i = 0; i < instances; i++) { |
| 715 | ret = iio_dummy_probe(i); |
| 716 | if (ret < 0) |
Vladimirs Ambrosovs | b3f6af3 | 2015-05-30 11:20:15 +0300 | [diff] [blame] | 717 | goto error_remove_devs; |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 718 | } |
| 719 | return 0; |
Vladimirs Ambrosovs | b3f6af3 | 2015-05-30 11:20:15 +0300 | [diff] [blame] | 720 | |
| 721 | error_remove_devs: |
| 722 | while (i--) |
| 723 | iio_dummy_remove(i); |
| 724 | |
| 725 | kfree(iio_dummy_devs); |
| 726 | return ret; |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 727 | } |
| 728 | module_init(iio_dummy_init); |
| 729 | |
| 730 | /** |
| 731 | * iio_dummy_exit() - device driver removal |
| 732 | * |
| 733 | * Varies depending on bus type of the device. |
| 734 | * As there is no device here, call remove directly. |
| 735 | */ |
| 736 | static __exit void iio_dummy_exit(void) |
| 737 | { |
| 738 | int i; |
Jimmy Picard | aff8945 | 2014-06-13 06:56:00 +0100 | [diff] [blame] | 739 | |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 740 | for (i = 0; i < instances; i++) |
| 741 | iio_dummy_remove(i); |
| 742 | kfree(iio_dummy_devs); |
| 743 | } |
| 744 | module_exit(iio_dummy_exit); |
| 745 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 746 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | 3a84331 | 2011-10-14 16:34:13 +0100 | [diff] [blame] | 747 | MODULE_DESCRIPTION("IIO dummy driver"); |
| 748 | MODULE_LICENSE("GPL v2"); |