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