iio_simple_dummy.c 18 KB

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