iio_simple_dummy.c 19 KB

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