stx104.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * IIO driver for the Apex Embedded Systems STX104
  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. #include <linux/bitops.h>
  15. #include <linux/device.h>
  16. #include <linux/errno.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/types.h>
  20. #include <linux/io.h>
  21. #include <linux/ioport.h>
  22. #include <linux/isa.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/spinlock.h>
  27. #define STX104_OUT_CHAN(chan) { \
  28. .type = IIO_VOLTAGE, \
  29. .channel = chan, \
  30. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  31. .indexed = 1, \
  32. .output = 1 \
  33. }
  34. #define STX104_IN_CHAN(chan, diff) { \
  35. .type = IIO_VOLTAGE, \
  36. .channel = chan, \
  37. .channel2 = chan, \
  38. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
  39. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), \
  40. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  41. .indexed = 1, \
  42. .differential = diff \
  43. }
  44. #define STX104_NUM_OUT_CHAN 2
  45. #define STX104_EXTENT 16
  46. static unsigned int base[max_num_isa_dev(STX104_EXTENT)];
  47. static unsigned int num_stx104;
  48. module_param_hw_array(base, uint, ioport, &num_stx104, 0);
  49. MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses");
  50. /**
  51. * struct stx104_iio - IIO device private data structure
  52. * @chan_out_states: channels' output states
  53. * @base: base port address of the IIO device
  54. */
  55. struct stx104_iio {
  56. unsigned int chan_out_states[STX104_NUM_OUT_CHAN];
  57. unsigned int base;
  58. };
  59. /**
  60. * struct stx104_gpio - GPIO device private data structure
  61. * @chip: instance of the gpio_chip
  62. * @lock: synchronization lock to prevent I/O race conditions
  63. * @base: base port address of the GPIO device
  64. * @out_state: output bits state
  65. */
  66. struct stx104_gpio {
  67. struct gpio_chip chip;
  68. spinlock_t lock;
  69. unsigned int base;
  70. unsigned int out_state;
  71. };
  72. static int stx104_read_raw(struct iio_dev *indio_dev,
  73. struct iio_chan_spec const *chan, int *val, int *val2, long mask)
  74. {
  75. struct stx104_iio *const priv = iio_priv(indio_dev);
  76. unsigned int adc_config;
  77. int adbu;
  78. int gain;
  79. switch (mask) {
  80. case IIO_CHAN_INFO_HARDWAREGAIN:
  81. /* get gain configuration */
  82. adc_config = inb(priv->base + 11);
  83. gain = adc_config & 0x3;
  84. *val = 1 << gain;
  85. return IIO_VAL_INT;
  86. case IIO_CHAN_INFO_RAW:
  87. if (chan->output) {
  88. *val = priv->chan_out_states[chan->channel];
  89. return IIO_VAL_INT;
  90. }
  91. /* select ADC channel */
  92. outb(chan->channel | (chan->channel << 4), priv->base + 2);
  93. /* trigger ADC sample capture and wait for completion */
  94. outb(0, priv->base);
  95. while (inb(priv->base + 8) & BIT(7));
  96. *val = inw(priv->base);
  97. return IIO_VAL_INT;
  98. case IIO_CHAN_INFO_OFFSET:
  99. /* get ADC bipolar/unipolar configuration */
  100. adc_config = inb(priv->base + 11);
  101. adbu = !(adc_config & BIT(2));
  102. *val = -32768 * adbu;
  103. return IIO_VAL_INT;
  104. case IIO_CHAN_INFO_SCALE:
  105. /* get ADC bipolar/unipolar and gain configuration */
  106. adc_config = inb(priv->base + 11);
  107. adbu = !(adc_config & BIT(2));
  108. gain = adc_config & 0x3;
  109. *val = 5;
  110. *val2 = 15 - adbu + gain;
  111. return IIO_VAL_FRACTIONAL_LOG2;
  112. }
  113. return -EINVAL;
  114. }
  115. static int stx104_write_raw(struct iio_dev *indio_dev,
  116. struct iio_chan_spec const *chan, int val, int val2, long mask)
  117. {
  118. struct stx104_iio *const priv = iio_priv(indio_dev);
  119. switch (mask) {
  120. case IIO_CHAN_INFO_HARDWAREGAIN:
  121. /* Only four gain states (x1, x2, x4, x8) */
  122. switch (val) {
  123. case 1:
  124. outb(0, priv->base + 11);
  125. break;
  126. case 2:
  127. outb(1, priv->base + 11);
  128. break;
  129. case 4:
  130. outb(2, priv->base + 11);
  131. break;
  132. case 8:
  133. outb(3, priv->base + 11);
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. return 0;
  139. case IIO_CHAN_INFO_RAW:
  140. if (chan->output) {
  141. /* DAC can only accept up to a 16-bit value */
  142. if ((unsigned int)val > 65535)
  143. return -EINVAL;
  144. priv->chan_out_states[chan->channel] = val;
  145. outw(val, priv->base + 4 + 2 * chan->channel);
  146. return 0;
  147. }
  148. return -EINVAL;
  149. }
  150. return -EINVAL;
  151. }
  152. static const struct iio_info stx104_info = {
  153. .read_raw = stx104_read_raw,
  154. .write_raw = stx104_write_raw
  155. };
  156. /* single-ended input channels configuration */
  157. static const struct iio_chan_spec stx104_channels_sing[] = {
  158. STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
  159. STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
  160. STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
  161. STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
  162. STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
  163. STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
  164. STX104_IN_CHAN(15, 0)
  165. };
  166. /* differential input channels configuration */
  167. static const struct iio_chan_spec stx104_channels_diff[] = {
  168. STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
  169. STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
  170. STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
  171. STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
  172. };
  173. static int stx104_gpio_get_direction(struct gpio_chip *chip,
  174. unsigned int offset)
  175. {
  176. /* GPIO 0-3 are input only, while the rest are output only */
  177. if (offset < 4)
  178. return 1;
  179. return 0;
  180. }
  181. static int stx104_gpio_direction_input(struct gpio_chip *chip,
  182. unsigned int offset)
  183. {
  184. if (offset >= 4)
  185. return -EINVAL;
  186. return 0;
  187. }
  188. static int stx104_gpio_direction_output(struct gpio_chip *chip,
  189. unsigned int offset, int value)
  190. {
  191. if (offset < 4)
  192. return -EINVAL;
  193. chip->set(chip, offset, value);
  194. return 0;
  195. }
  196. static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
  197. {
  198. struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
  199. if (offset >= 4)
  200. return -EINVAL;
  201. return !!(inb(stx104gpio->base) & BIT(offset));
  202. }
  203. static int stx104_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
  204. unsigned long *bits)
  205. {
  206. struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
  207. *bits = inb(stx104gpio->base);
  208. return 0;
  209. }
  210. static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
  211. int value)
  212. {
  213. struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
  214. const unsigned int mask = BIT(offset) >> 4;
  215. unsigned long flags;
  216. if (offset < 4)
  217. return;
  218. spin_lock_irqsave(&stx104gpio->lock, flags);
  219. if (value)
  220. stx104gpio->out_state |= mask;
  221. else
  222. stx104gpio->out_state &= ~mask;
  223. outb(stx104gpio->out_state, stx104gpio->base);
  224. spin_unlock_irqrestore(&stx104gpio->lock, flags);
  225. }
  226. #define STX104_NGPIO 8
  227. static const char *stx104_names[STX104_NGPIO] = {
  228. "DIN0", "DIN1", "DIN2", "DIN3", "DOUT0", "DOUT1", "DOUT2", "DOUT3"
  229. };
  230. static void stx104_gpio_set_multiple(struct gpio_chip *chip,
  231. unsigned long *mask, unsigned long *bits)
  232. {
  233. struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
  234. unsigned long flags;
  235. /* verify masked GPIO are output */
  236. if (!(*mask & 0xF0))
  237. return;
  238. *mask >>= 4;
  239. *bits >>= 4;
  240. spin_lock_irqsave(&stx104gpio->lock, flags);
  241. stx104gpio->out_state &= ~*mask;
  242. stx104gpio->out_state |= *mask & *bits;
  243. outb(stx104gpio->out_state, stx104gpio->base);
  244. spin_unlock_irqrestore(&stx104gpio->lock, flags);
  245. }
  246. static int stx104_probe(struct device *dev, unsigned int id)
  247. {
  248. struct iio_dev *indio_dev;
  249. struct stx104_iio *priv;
  250. struct stx104_gpio *stx104gpio;
  251. int err;
  252. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  253. if (!indio_dev)
  254. return -ENOMEM;
  255. stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
  256. if (!stx104gpio)
  257. return -ENOMEM;
  258. if (!devm_request_region(dev, base[id], STX104_EXTENT,
  259. dev_name(dev))) {
  260. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  261. base[id], base[id] + STX104_EXTENT);
  262. return -EBUSY;
  263. }
  264. indio_dev->info = &stx104_info;
  265. indio_dev->modes = INDIO_DIRECT_MODE;
  266. /* determine if differential inputs */
  267. if (inb(base[id] + 8) & BIT(5)) {
  268. indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff);
  269. indio_dev->channels = stx104_channels_diff;
  270. } else {
  271. indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing);
  272. indio_dev->channels = stx104_channels_sing;
  273. }
  274. indio_dev->name = dev_name(dev);
  275. indio_dev->dev.parent = dev;
  276. priv = iio_priv(indio_dev);
  277. priv->base = base[id];
  278. /* configure device for software trigger operation */
  279. outb(0, base[id] + 9);
  280. /* initialize gain setting to x1 */
  281. outb(0, base[id] + 11);
  282. /* initialize DAC output to 0V */
  283. outw(0, base[id] + 4);
  284. outw(0, base[id] + 6);
  285. stx104gpio->chip.label = dev_name(dev);
  286. stx104gpio->chip.parent = dev;
  287. stx104gpio->chip.owner = THIS_MODULE;
  288. stx104gpio->chip.base = -1;
  289. stx104gpio->chip.ngpio = STX104_NGPIO;
  290. stx104gpio->chip.names = stx104_names;
  291. stx104gpio->chip.get_direction = stx104_gpio_get_direction;
  292. stx104gpio->chip.direction_input = stx104_gpio_direction_input;
  293. stx104gpio->chip.direction_output = stx104_gpio_direction_output;
  294. stx104gpio->chip.get = stx104_gpio_get;
  295. stx104gpio->chip.get_multiple = stx104_gpio_get_multiple;
  296. stx104gpio->chip.set = stx104_gpio_set;
  297. stx104gpio->chip.set_multiple = stx104_gpio_set_multiple;
  298. stx104gpio->base = base[id] + 3;
  299. stx104gpio->out_state = 0x0;
  300. spin_lock_init(&stx104gpio->lock);
  301. err = devm_gpiochip_add_data(dev, &stx104gpio->chip, stx104gpio);
  302. if (err) {
  303. dev_err(dev, "GPIO registering failed (%d)\n", err);
  304. return err;
  305. }
  306. return devm_iio_device_register(dev, indio_dev);
  307. }
  308. static struct isa_driver stx104_driver = {
  309. .probe = stx104_probe,
  310. .driver = {
  311. .name = "stx104"
  312. },
  313. };
  314. module_isa_driver(stx104_driver, num_stx104);
  315. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  316. MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
  317. MODULE_LICENSE("GPL v2");