iio_simple_dummy.c 18 KB

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