stx104.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_array(base, uint, &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. /**
  73. * struct stx104_dev - STX104 device private data structure
  74. * @indio_dev: IIO device
  75. * @chip: instance of the gpio_chip
  76. */
  77. struct stx104_dev {
  78. struct iio_dev *indio_dev;
  79. struct gpio_chip *chip;
  80. };
  81. static int stx104_read_raw(struct iio_dev *indio_dev,
  82. struct iio_chan_spec const *chan, int *val, int *val2, long mask)
  83. {
  84. struct stx104_iio *const priv = iio_priv(indio_dev);
  85. unsigned int adc_config;
  86. int adbu;
  87. int gain;
  88. switch (mask) {
  89. case IIO_CHAN_INFO_HARDWAREGAIN:
  90. /* get gain configuration */
  91. adc_config = inb(priv->base + 11);
  92. gain = adc_config & 0x3;
  93. *val = 1 << gain;
  94. return IIO_VAL_INT;
  95. case IIO_CHAN_INFO_RAW:
  96. if (chan->output) {
  97. *val = priv->chan_out_states[chan->channel];
  98. return IIO_VAL_INT;
  99. }
  100. /* select ADC channel */
  101. outb(chan->channel | (chan->channel << 4), priv->base + 2);
  102. /* trigger ADC sample capture and wait for completion */
  103. outb(0, priv->base);
  104. while (inb(priv->base + 8) & BIT(7));
  105. *val = inw(priv->base);
  106. return IIO_VAL_INT;
  107. case IIO_CHAN_INFO_OFFSET:
  108. /* get ADC bipolar/unipolar configuration */
  109. adc_config = inb(priv->base + 11);
  110. adbu = !(adc_config & BIT(2));
  111. *val = -32768 * adbu;
  112. return IIO_VAL_INT;
  113. case IIO_CHAN_INFO_SCALE:
  114. /* get ADC bipolar/unipolar and gain configuration */
  115. adc_config = inb(priv->base + 11);
  116. adbu = !(adc_config & BIT(2));
  117. gain = adc_config & 0x3;
  118. *val = 5;
  119. *val2 = 15 - adbu + gain;
  120. return IIO_VAL_FRACTIONAL_LOG2;
  121. }
  122. return -EINVAL;
  123. }
  124. static int stx104_write_raw(struct iio_dev *indio_dev,
  125. struct iio_chan_spec const *chan, int val, int val2, long mask)
  126. {
  127. struct stx104_iio *const priv = iio_priv(indio_dev);
  128. switch (mask) {
  129. case IIO_CHAN_INFO_HARDWAREGAIN:
  130. /* Only four gain states (x1, x2, x4, x8) */
  131. switch (val) {
  132. case 1:
  133. outb(0, priv->base + 11);
  134. break;
  135. case 2:
  136. outb(1, priv->base + 11);
  137. break;
  138. case 4:
  139. outb(2, priv->base + 11);
  140. break;
  141. case 8:
  142. outb(3, priv->base + 11);
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. return 0;
  148. case IIO_CHAN_INFO_RAW:
  149. if (chan->output) {
  150. /* DAC can only accept up to a 16-bit value */
  151. if ((unsigned int)val > 65535)
  152. return -EINVAL;
  153. priv->chan_out_states[chan->channel] = val;
  154. outw(val, priv->base + 4 + 2 * chan->channel);
  155. return 0;
  156. }
  157. return -EINVAL;
  158. }
  159. return -EINVAL;
  160. }
  161. static const struct iio_info stx104_info = {
  162. .driver_module = THIS_MODULE,
  163. .read_raw = stx104_read_raw,
  164. .write_raw = stx104_write_raw
  165. };
  166. /* single-ended input channels configuration */
  167. static const struct iio_chan_spec stx104_channels_sing[] = {
  168. STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
  169. STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
  170. STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
  171. STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
  172. STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
  173. STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
  174. STX104_IN_CHAN(15, 0)
  175. };
  176. /* differential input channels configuration */
  177. static const struct iio_chan_spec stx104_channels_diff[] = {
  178. STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
  179. STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
  180. STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
  181. STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
  182. };
  183. static int stx104_gpio_get_direction(struct gpio_chip *chip,
  184. unsigned int offset)
  185. {
  186. /* GPIO 0-3 are input only, while the rest are output only */
  187. if (offset < 4)
  188. return 1;
  189. return 0;
  190. }
  191. static int stx104_gpio_direction_input(struct gpio_chip *chip,
  192. unsigned int offset)
  193. {
  194. if (offset >= 4)
  195. return -EINVAL;
  196. return 0;
  197. }
  198. static int stx104_gpio_direction_output(struct gpio_chip *chip,
  199. unsigned int offset, int value)
  200. {
  201. if (offset < 4)
  202. return -EINVAL;
  203. chip->set(chip, offset, value);
  204. return 0;
  205. }
  206. static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset)
  207. {
  208. struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
  209. if (offset >= 4)
  210. return -EINVAL;
  211. return !!(inb(stx104gpio->base) & BIT(offset));
  212. }
  213. static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset,
  214. int value)
  215. {
  216. struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip);
  217. const unsigned int mask = BIT(offset) >> 4;
  218. unsigned long flags;
  219. if (offset < 4)
  220. return;
  221. spin_lock_irqsave(&stx104gpio->lock, flags);
  222. if (value)
  223. stx104gpio->out_state |= mask;
  224. else
  225. stx104gpio->out_state &= ~mask;
  226. outb(stx104gpio->out_state, stx104gpio->base);
  227. spin_unlock_irqrestore(&stx104gpio->lock, flags);
  228. }
  229. static int stx104_probe(struct device *dev, unsigned int id)
  230. {
  231. struct iio_dev *indio_dev;
  232. struct stx104_iio *priv;
  233. struct stx104_gpio *stx104gpio;
  234. struct stx104_dev *stx104dev;
  235. int err;
  236. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  237. if (!indio_dev)
  238. return -ENOMEM;
  239. stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL);
  240. if (!stx104gpio)
  241. return -ENOMEM;
  242. stx104dev = devm_kzalloc(dev, sizeof(*stx104dev), GFP_KERNEL);
  243. if (!stx104dev)
  244. return -ENOMEM;
  245. if (!devm_request_region(dev, base[id], STX104_EXTENT,
  246. dev_name(dev))) {
  247. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  248. base[id], base[id] + STX104_EXTENT);
  249. return -EBUSY;
  250. }
  251. indio_dev->info = &stx104_info;
  252. indio_dev->modes = INDIO_DIRECT_MODE;
  253. /* determine if differential inputs */
  254. if (inb(base[id] + 8) & BIT(5)) {
  255. indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff);
  256. indio_dev->channels = stx104_channels_diff;
  257. } else {
  258. indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing);
  259. indio_dev->channels = stx104_channels_sing;
  260. }
  261. indio_dev->name = dev_name(dev);
  262. priv = iio_priv(indio_dev);
  263. priv->base = base[id];
  264. /* configure device for software trigger operation */
  265. outb(0, base[id] + 9);
  266. /* initialize gain setting to x1 */
  267. outb(0, base[id] + 11);
  268. /* initialize DAC output to 0V */
  269. outw(0, base[id] + 4);
  270. outw(0, base[id] + 6);
  271. stx104gpio->chip.label = dev_name(dev);
  272. stx104gpio->chip.parent = dev;
  273. stx104gpio->chip.owner = THIS_MODULE;
  274. stx104gpio->chip.base = -1;
  275. stx104gpio->chip.ngpio = 8;
  276. stx104gpio->chip.get_direction = stx104_gpio_get_direction;
  277. stx104gpio->chip.direction_input = stx104_gpio_direction_input;
  278. stx104gpio->chip.direction_output = stx104_gpio_direction_output;
  279. stx104gpio->chip.get = stx104_gpio_get;
  280. stx104gpio->chip.set = stx104_gpio_set;
  281. stx104gpio->base = base[id] + 3;
  282. stx104gpio->out_state = 0x0;
  283. spin_lock_init(&stx104gpio->lock);
  284. stx104dev->indio_dev = indio_dev;
  285. stx104dev->chip = &stx104gpio->chip;
  286. dev_set_drvdata(dev, stx104dev);
  287. err = gpiochip_add_data(&stx104gpio->chip, stx104gpio);
  288. if (err) {
  289. dev_err(dev, "GPIO registering failed (%d)\n", err);
  290. return err;
  291. }
  292. err = iio_device_register(indio_dev);
  293. if (err) {
  294. dev_err(dev, "IIO device registering failed (%d)\n", err);
  295. gpiochip_remove(&stx104gpio->chip);
  296. return err;
  297. }
  298. return 0;
  299. }
  300. static int stx104_remove(struct device *dev, unsigned int id)
  301. {
  302. struct stx104_dev *const stx104dev = dev_get_drvdata(dev);
  303. iio_device_unregister(stx104dev->indio_dev);
  304. gpiochip_remove(stx104dev->chip);
  305. return 0;
  306. }
  307. static struct isa_driver stx104_driver = {
  308. .probe = stx104_probe,
  309. .driver = {
  310. .name = "stx104"
  311. },
  312. .remove = stx104_remove
  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");