104-quad-8.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * IIO driver for the ACCES 104-QUAD-8
  3. * Copyright (C) 2016 William Breathitt Gray
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * This driver supports the ACCES 104-QUAD-8 and ACCES 104-QUAD-4.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/device.h>
  18. #include <linux/errno.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/types.h>
  21. #include <linux/io.h>
  22. #include <linux/ioport.h>
  23. #include <linux/isa.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/types.h>
  28. #define QUAD8_EXTENT 32
  29. static unsigned int base[max_num_isa_dev(QUAD8_EXTENT)];
  30. static unsigned int num_quad8;
  31. module_param_array(base, uint, &num_quad8, 0);
  32. MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
  33. #define QUAD8_NUM_COUNTERS 8
  34. /**
  35. * struct quad8_iio - IIO device private data structure
  36. * @preset: array of preset values
  37. * @count_mode: array of count mode configurations
  38. * @quadrature_mode: array of quadrature mode configurations
  39. * @quadrature_scale: array of quadrature mode scale configurations
  40. * @ab_enable: array of A and B inputs enable configurations
  41. * @preset_enable: array of set_to_preset_on_index attribute configurations
  42. * @synchronous_mode: array of index function synchronous mode configurations
  43. * @index_polarity: array of index function polarity configurations
  44. * @base: base port address of the IIO device
  45. */
  46. struct quad8_iio {
  47. unsigned int preset[QUAD8_NUM_COUNTERS];
  48. unsigned int count_mode[QUAD8_NUM_COUNTERS];
  49. unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
  50. unsigned int quadrature_scale[QUAD8_NUM_COUNTERS];
  51. unsigned int ab_enable[QUAD8_NUM_COUNTERS];
  52. unsigned int preset_enable[QUAD8_NUM_COUNTERS];
  53. unsigned int synchronous_mode[QUAD8_NUM_COUNTERS];
  54. unsigned int index_polarity[QUAD8_NUM_COUNTERS];
  55. unsigned int base;
  56. };
  57. #define QUAD8_REG_CHAN_OP 0x11
  58. #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
  59. /* Borrow Toggle flip-flop */
  60. #define QUAD8_FLAG_BT BIT(0)
  61. /* Carry Toggle flip-flop */
  62. #define QUAD8_FLAG_CT BIT(1)
  63. /* Error flag */
  64. #define QUAD8_FLAG_E BIT(4)
  65. /* Up/Down flag */
  66. #define QUAD8_FLAG_UD BIT(5)
  67. /* Reset and Load Signal Decoders */
  68. #define QUAD8_CTR_RLD 0x00
  69. /* Counter Mode Register */
  70. #define QUAD8_CTR_CMR 0x20
  71. /* Input / Output Control Register */
  72. #define QUAD8_CTR_IOR 0x40
  73. /* Index Control Register */
  74. #define QUAD8_CTR_IDR 0x60
  75. /* Reset Byte Pointer (three byte data pointer) */
  76. #define QUAD8_RLD_RESET_BP 0x01
  77. /* Reset Counter */
  78. #define QUAD8_RLD_RESET_CNTR 0x02
  79. /* Reset Borrow Toggle, Carry Toggle, Compare Toggle, and Sign flags */
  80. #define QUAD8_RLD_RESET_FLAGS 0x04
  81. /* Reset Error flag */
  82. #define QUAD8_RLD_RESET_E 0x06
  83. /* Preset Register to Counter */
  84. #define QUAD8_RLD_PRESET_CNTR 0x08
  85. /* Transfer Counter to Output Latch */
  86. #define QUAD8_RLD_CNTR_OUT 0x10
  87. #define QUAD8_CHAN_OP_ENABLE_COUNTERS 0x00
  88. #define QUAD8_CHAN_OP_RESET_COUNTERS 0x01
  89. static int quad8_read_raw(struct iio_dev *indio_dev,
  90. struct iio_chan_spec const *chan, int *val, int *val2, long mask)
  91. {
  92. struct quad8_iio *const priv = iio_priv(indio_dev);
  93. const int base_offset = priv->base + 2 * chan->channel;
  94. unsigned int flags;
  95. unsigned int borrow;
  96. unsigned int carry;
  97. int i;
  98. switch (mask) {
  99. case IIO_CHAN_INFO_RAW:
  100. if (chan->type == IIO_INDEX) {
  101. *val = !!(inb(priv->base + QUAD8_REG_INDEX_INPUT_LEVELS)
  102. & BIT(chan->channel));
  103. return IIO_VAL_INT;
  104. }
  105. flags = inb(base_offset + 1);
  106. borrow = flags & QUAD8_FLAG_BT;
  107. carry = !!(flags & QUAD8_FLAG_CT);
  108. /* Borrow XOR Carry effectively doubles count range */
  109. *val = (borrow ^ carry) << 24;
  110. /* Reset Byte Pointer; transfer Counter to Output Latch */
  111. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
  112. base_offset + 1);
  113. for (i = 0; i < 3; i++)
  114. *val |= (unsigned int)inb(base_offset) << (8 * i);
  115. return IIO_VAL_INT;
  116. case IIO_CHAN_INFO_ENABLE:
  117. *val = priv->ab_enable[chan->channel];
  118. return IIO_VAL_INT;
  119. case IIO_CHAN_INFO_SCALE:
  120. *val = 1;
  121. *val2 = priv->quadrature_scale[chan->channel];
  122. return IIO_VAL_FRACTIONAL_LOG2;
  123. }
  124. return -EINVAL;
  125. }
  126. static int quad8_write_raw(struct iio_dev *indio_dev,
  127. struct iio_chan_spec const *chan, int val, int val2, long mask)
  128. {
  129. struct quad8_iio *const priv = iio_priv(indio_dev);
  130. const int base_offset = priv->base + 2 * chan->channel;
  131. int i;
  132. unsigned int ior_cfg;
  133. switch (mask) {
  134. case IIO_CHAN_INFO_RAW:
  135. if (chan->type == IIO_INDEX)
  136. return -EINVAL;
  137. /* Only 24-bit values are supported */
  138. if ((unsigned int)val > 0xFFFFFF)
  139. return -EINVAL;
  140. /* Reset Byte Pointer */
  141. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
  142. /* Counter can only be set via Preset Register */
  143. for (i = 0; i < 3; i++)
  144. outb(val >> (8 * i), base_offset);
  145. /* Transfer Preset Register to Counter */
  146. outb(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, base_offset + 1);
  147. /* Reset Byte Pointer */
  148. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
  149. /* Set Preset Register back to original value */
  150. val = priv->preset[chan->channel];
  151. for (i = 0; i < 3; i++)
  152. outb(val >> (8 * i), base_offset);
  153. /* Reset Borrow, Carry, Compare, and Sign flags */
  154. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
  155. /* Reset Error flag */
  156. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
  157. return 0;
  158. case IIO_CHAN_INFO_ENABLE:
  159. /* only boolean values accepted */
  160. if (val < 0 || val > 1)
  161. return -EINVAL;
  162. priv->ab_enable[chan->channel] = val;
  163. ior_cfg = val | priv->preset_enable[chan->channel] << 1;
  164. /* Load I/O control configuration */
  165. outb(QUAD8_CTR_IOR | ior_cfg, base_offset + 1);
  166. return 0;
  167. case IIO_CHAN_INFO_SCALE:
  168. /* Quadrature scaling only available in quadrature mode */
  169. if (!priv->quadrature_mode[chan->channel] && (val2 || val != 1))
  170. return -EINVAL;
  171. /* Only three gain states (1, 0.5, 0.25) */
  172. if (val == 1 && !val2)
  173. priv->quadrature_scale[chan->channel] = 0;
  174. else if (!val)
  175. switch (val2) {
  176. case 500000:
  177. priv->quadrature_scale[chan->channel] = 1;
  178. break;
  179. case 250000:
  180. priv->quadrature_scale[chan->channel] = 2;
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. else
  186. return -EINVAL;
  187. return 0;
  188. }
  189. return -EINVAL;
  190. }
  191. static const struct iio_info quad8_info = {
  192. .read_raw = quad8_read_raw,
  193. .write_raw = quad8_write_raw
  194. };
  195. static ssize_t quad8_read_preset(struct iio_dev *indio_dev, uintptr_t private,
  196. const struct iio_chan_spec *chan, char *buf)
  197. {
  198. const struct quad8_iio *const priv = iio_priv(indio_dev);
  199. return snprintf(buf, PAGE_SIZE, "%u\n", priv->preset[chan->channel]);
  200. }
  201. static ssize_t quad8_write_preset(struct iio_dev *indio_dev, uintptr_t private,
  202. const struct iio_chan_spec *chan, const char *buf, size_t len)
  203. {
  204. struct quad8_iio *const priv = iio_priv(indio_dev);
  205. const int base_offset = priv->base + 2 * chan->channel;
  206. unsigned int preset;
  207. int ret;
  208. int i;
  209. ret = kstrtouint(buf, 0, &preset);
  210. if (ret)
  211. return ret;
  212. /* Only 24-bit values are supported */
  213. if (preset > 0xFFFFFF)
  214. return -EINVAL;
  215. priv->preset[chan->channel] = preset;
  216. /* Reset Byte Pointer */
  217. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
  218. /* Set Preset Register */
  219. for (i = 0; i < 3; i++)
  220. outb(preset >> (8 * i), base_offset);
  221. return len;
  222. }
  223. static ssize_t quad8_read_set_to_preset_on_index(struct iio_dev *indio_dev,
  224. uintptr_t private, const struct iio_chan_spec *chan, char *buf)
  225. {
  226. const struct quad8_iio *const priv = iio_priv(indio_dev);
  227. return snprintf(buf, PAGE_SIZE, "%u\n",
  228. !priv->preset_enable[chan->channel]);
  229. }
  230. static ssize_t quad8_write_set_to_preset_on_index(struct iio_dev *indio_dev,
  231. uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
  232. size_t len)
  233. {
  234. struct quad8_iio *const priv = iio_priv(indio_dev);
  235. const int base_offset = priv->base + 2 * chan->channel + 1;
  236. bool preset_enable;
  237. int ret;
  238. unsigned int ior_cfg;
  239. ret = kstrtobool(buf, &preset_enable);
  240. if (ret)
  241. return ret;
  242. /* Preset enable is active low in Input/Output Control register */
  243. preset_enable = !preset_enable;
  244. priv->preset_enable[chan->channel] = preset_enable;
  245. ior_cfg = priv->ab_enable[chan->channel] |
  246. (unsigned int)preset_enable << 1;
  247. /* Load I/O control configuration to Input / Output Control Register */
  248. outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
  249. return len;
  250. }
  251. static const char *const quad8_noise_error_states[] = {
  252. "No excessive noise is present at the count inputs",
  253. "Excessive noise is present at the count inputs"
  254. };
  255. static int quad8_get_noise_error(struct iio_dev *indio_dev,
  256. const struct iio_chan_spec *chan)
  257. {
  258. struct quad8_iio *const priv = iio_priv(indio_dev);
  259. const int base_offset = priv->base + 2 * chan->channel + 1;
  260. return !!(inb(base_offset) & QUAD8_FLAG_E);
  261. }
  262. static const struct iio_enum quad8_noise_error_enum = {
  263. .items = quad8_noise_error_states,
  264. .num_items = ARRAY_SIZE(quad8_noise_error_states),
  265. .get = quad8_get_noise_error
  266. };
  267. static const char *const quad8_count_direction_states[] = {
  268. "down",
  269. "up"
  270. };
  271. static int quad8_get_count_direction(struct iio_dev *indio_dev,
  272. const struct iio_chan_spec *chan)
  273. {
  274. struct quad8_iio *const priv = iio_priv(indio_dev);
  275. const int base_offset = priv->base + 2 * chan->channel + 1;
  276. return !!(inb(base_offset) & QUAD8_FLAG_UD);
  277. }
  278. static const struct iio_enum quad8_count_direction_enum = {
  279. .items = quad8_count_direction_states,
  280. .num_items = ARRAY_SIZE(quad8_count_direction_states),
  281. .get = quad8_get_count_direction
  282. };
  283. static const char *const quad8_count_modes[] = {
  284. "normal",
  285. "range limit",
  286. "non-recycle",
  287. "modulo-n"
  288. };
  289. static int quad8_set_count_mode(struct iio_dev *indio_dev,
  290. const struct iio_chan_spec *chan, unsigned int count_mode)
  291. {
  292. struct quad8_iio *const priv = iio_priv(indio_dev);
  293. unsigned int mode_cfg = count_mode << 1;
  294. const int base_offset = priv->base + 2 * chan->channel + 1;
  295. priv->count_mode[chan->channel] = count_mode;
  296. /* Add quadrature mode configuration */
  297. if (priv->quadrature_mode[chan->channel])
  298. mode_cfg |= (priv->quadrature_scale[chan->channel] + 1) << 3;
  299. /* Load mode configuration to Counter Mode Register */
  300. outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
  301. return 0;
  302. }
  303. static int quad8_get_count_mode(struct iio_dev *indio_dev,
  304. const struct iio_chan_spec *chan)
  305. {
  306. const struct quad8_iio *const priv = iio_priv(indio_dev);
  307. return priv->count_mode[chan->channel];
  308. }
  309. static const struct iio_enum quad8_count_mode_enum = {
  310. .items = quad8_count_modes,
  311. .num_items = ARRAY_SIZE(quad8_count_modes),
  312. .set = quad8_set_count_mode,
  313. .get = quad8_get_count_mode
  314. };
  315. static const char *const quad8_synchronous_modes[] = {
  316. "non-synchronous",
  317. "synchronous"
  318. };
  319. static int quad8_set_synchronous_mode(struct iio_dev *indio_dev,
  320. const struct iio_chan_spec *chan, unsigned int synchronous_mode)
  321. {
  322. struct quad8_iio *const priv = iio_priv(indio_dev);
  323. const unsigned int idr_cfg = synchronous_mode |
  324. priv->index_polarity[chan->channel] << 1;
  325. const int base_offset = priv->base + 2 * chan->channel + 1;
  326. /* Index function must be non-synchronous in non-quadrature mode */
  327. if (synchronous_mode && !priv->quadrature_mode[chan->channel])
  328. return -EINVAL;
  329. priv->synchronous_mode[chan->channel] = synchronous_mode;
  330. /* Load Index Control configuration to Index Control Register */
  331. outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
  332. return 0;
  333. }
  334. static int quad8_get_synchronous_mode(struct iio_dev *indio_dev,
  335. const struct iio_chan_spec *chan)
  336. {
  337. const struct quad8_iio *const priv = iio_priv(indio_dev);
  338. return priv->synchronous_mode[chan->channel];
  339. }
  340. static const struct iio_enum quad8_synchronous_mode_enum = {
  341. .items = quad8_synchronous_modes,
  342. .num_items = ARRAY_SIZE(quad8_synchronous_modes),
  343. .set = quad8_set_synchronous_mode,
  344. .get = quad8_get_synchronous_mode
  345. };
  346. static const char *const quad8_quadrature_modes[] = {
  347. "non-quadrature",
  348. "quadrature"
  349. };
  350. static int quad8_set_quadrature_mode(struct iio_dev *indio_dev,
  351. const struct iio_chan_spec *chan, unsigned int quadrature_mode)
  352. {
  353. struct quad8_iio *const priv = iio_priv(indio_dev);
  354. unsigned int mode_cfg = priv->count_mode[chan->channel] << 1;
  355. const int base_offset = priv->base + 2 * chan->channel + 1;
  356. if (quadrature_mode)
  357. mode_cfg |= (priv->quadrature_scale[chan->channel] + 1) << 3;
  358. else {
  359. /* Quadrature scaling only available in quadrature mode */
  360. priv->quadrature_scale[chan->channel] = 0;
  361. /* Synchronous function not supported in non-quadrature mode */
  362. if (priv->synchronous_mode[chan->channel])
  363. quad8_set_synchronous_mode(indio_dev, chan, 0);
  364. }
  365. priv->quadrature_mode[chan->channel] = quadrature_mode;
  366. /* Load mode configuration to Counter Mode Register */
  367. outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
  368. return 0;
  369. }
  370. static int quad8_get_quadrature_mode(struct iio_dev *indio_dev,
  371. const struct iio_chan_spec *chan)
  372. {
  373. const struct quad8_iio *const priv = iio_priv(indio_dev);
  374. return priv->quadrature_mode[chan->channel];
  375. }
  376. static const struct iio_enum quad8_quadrature_mode_enum = {
  377. .items = quad8_quadrature_modes,
  378. .num_items = ARRAY_SIZE(quad8_quadrature_modes),
  379. .set = quad8_set_quadrature_mode,
  380. .get = quad8_get_quadrature_mode
  381. };
  382. static const char *const quad8_index_polarity_modes[] = {
  383. "negative",
  384. "positive"
  385. };
  386. static int quad8_set_index_polarity(struct iio_dev *indio_dev,
  387. const struct iio_chan_spec *chan, unsigned int index_polarity)
  388. {
  389. struct quad8_iio *const priv = iio_priv(indio_dev);
  390. const unsigned int idr_cfg = priv->synchronous_mode[chan->channel] |
  391. index_polarity << 1;
  392. const int base_offset = priv->base + 2 * chan->channel + 1;
  393. priv->index_polarity[chan->channel] = index_polarity;
  394. /* Load Index Control configuration to Index Control Register */
  395. outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
  396. return 0;
  397. }
  398. static int quad8_get_index_polarity(struct iio_dev *indio_dev,
  399. const struct iio_chan_spec *chan)
  400. {
  401. const struct quad8_iio *const priv = iio_priv(indio_dev);
  402. return priv->index_polarity[chan->channel];
  403. }
  404. static const struct iio_enum quad8_index_polarity_enum = {
  405. .items = quad8_index_polarity_modes,
  406. .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
  407. .set = quad8_set_index_polarity,
  408. .get = quad8_get_index_polarity
  409. };
  410. static const struct iio_chan_spec_ext_info quad8_count_ext_info[] = {
  411. {
  412. .name = "preset",
  413. .shared = IIO_SEPARATE,
  414. .read = quad8_read_preset,
  415. .write = quad8_write_preset
  416. },
  417. {
  418. .name = "set_to_preset_on_index",
  419. .shared = IIO_SEPARATE,
  420. .read = quad8_read_set_to_preset_on_index,
  421. .write = quad8_write_set_to_preset_on_index
  422. },
  423. IIO_ENUM("noise_error", IIO_SEPARATE, &quad8_noise_error_enum),
  424. IIO_ENUM_AVAILABLE("noise_error", &quad8_noise_error_enum),
  425. IIO_ENUM("count_direction", IIO_SEPARATE, &quad8_count_direction_enum),
  426. IIO_ENUM_AVAILABLE("count_direction", &quad8_count_direction_enum),
  427. IIO_ENUM("count_mode", IIO_SEPARATE, &quad8_count_mode_enum),
  428. IIO_ENUM_AVAILABLE("count_mode", &quad8_count_mode_enum),
  429. IIO_ENUM("quadrature_mode", IIO_SEPARATE, &quad8_quadrature_mode_enum),
  430. IIO_ENUM_AVAILABLE("quadrature_mode", &quad8_quadrature_mode_enum),
  431. {}
  432. };
  433. static const struct iio_chan_spec_ext_info quad8_index_ext_info[] = {
  434. IIO_ENUM("synchronous_mode", IIO_SEPARATE,
  435. &quad8_synchronous_mode_enum),
  436. IIO_ENUM_AVAILABLE("synchronous_mode", &quad8_synchronous_mode_enum),
  437. IIO_ENUM("index_polarity", IIO_SEPARATE, &quad8_index_polarity_enum),
  438. IIO_ENUM_AVAILABLE("index_polarity", &quad8_index_polarity_enum),
  439. {}
  440. };
  441. #define QUAD8_COUNT_CHAN(_chan) { \
  442. .type = IIO_COUNT, \
  443. .channel = (_chan), \
  444. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  445. BIT(IIO_CHAN_INFO_ENABLE) | BIT(IIO_CHAN_INFO_SCALE), \
  446. .ext_info = quad8_count_ext_info, \
  447. .indexed = 1 \
  448. }
  449. #define QUAD8_INDEX_CHAN(_chan) { \
  450. .type = IIO_INDEX, \
  451. .channel = (_chan), \
  452. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  453. .ext_info = quad8_index_ext_info, \
  454. .indexed = 1 \
  455. }
  456. static const struct iio_chan_spec quad8_channels[] = {
  457. QUAD8_COUNT_CHAN(0), QUAD8_INDEX_CHAN(0),
  458. QUAD8_COUNT_CHAN(1), QUAD8_INDEX_CHAN(1),
  459. QUAD8_COUNT_CHAN(2), QUAD8_INDEX_CHAN(2),
  460. QUAD8_COUNT_CHAN(3), QUAD8_INDEX_CHAN(3),
  461. QUAD8_COUNT_CHAN(4), QUAD8_INDEX_CHAN(4),
  462. QUAD8_COUNT_CHAN(5), QUAD8_INDEX_CHAN(5),
  463. QUAD8_COUNT_CHAN(6), QUAD8_INDEX_CHAN(6),
  464. QUAD8_COUNT_CHAN(7), QUAD8_INDEX_CHAN(7)
  465. };
  466. static int quad8_probe(struct device *dev, unsigned int id)
  467. {
  468. struct iio_dev *indio_dev;
  469. struct quad8_iio *priv;
  470. int i, j;
  471. unsigned int base_offset;
  472. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  473. if (!indio_dev)
  474. return -ENOMEM;
  475. if (!devm_request_region(dev, base[id], QUAD8_EXTENT,
  476. dev_name(dev))) {
  477. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  478. base[id], base[id] + QUAD8_EXTENT);
  479. return -EBUSY;
  480. }
  481. indio_dev->info = &quad8_info;
  482. indio_dev->modes = INDIO_DIRECT_MODE;
  483. indio_dev->num_channels = ARRAY_SIZE(quad8_channels);
  484. indio_dev->channels = quad8_channels;
  485. indio_dev->name = dev_name(dev);
  486. indio_dev->dev.parent = dev;
  487. priv = iio_priv(indio_dev);
  488. priv->base = base[id];
  489. /* Reset all counters and disable interrupt function */
  490. outb(QUAD8_CHAN_OP_RESET_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
  491. /* Set initial configuration for all counters */
  492. for (i = 0; i < QUAD8_NUM_COUNTERS; i++) {
  493. base_offset = base[id] + 2 * i;
  494. /* Reset Byte Pointer */
  495. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
  496. /* Reset Preset Register */
  497. for (j = 0; j < 3; j++)
  498. outb(0x00, base_offset);
  499. /* Reset Borrow, Carry, Compare, and Sign flags */
  500. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
  501. /* Reset Error flag */
  502. outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
  503. /* Binary encoding; Normal count; non-quadrature mode */
  504. outb(QUAD8_CTR_CMR, base_offset + 1);
  505. /* Disable A and B inputs; preset on index; FLG1 as Carry */
  506. outb(QUAD8_CTR_IOR, base_offset + 1);
  507. /* Disable index function; negative index polarity */
  508. outb(QUAD8_CTR_IDR, base_offset + 1);
  509. }
  510. /* Enable all counters */
  511. outb(QUAD8_CHAN_OP_ENABLE_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
  512. return devm_iio_device_register(dev, indio_dev);
  513. }
  514. static struct isa_driver quad8_driver = {
  515. .probe = quad8_probe,
  516. .driver = {
  517. .name = "104-quad-8"
  518. }
  519. };
  520. module_isa_driver(quad8_driver, num_quad8);
  521. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  522. MODULE_DESCRIPTION("ACCES 104-QUAD-8 IIO driver");
  523. MODULE_LICENSE("GPL v2");