gpio-max3191x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * gpio-max3191x.c - GPIO driver for Maxim MAX3191x industrial serializer
  3. *
  4. * Copyright (C) 2017 KUNBUS GmbH
  5. *
  6. * The MAX3191x makes 8 digital 24V inputs available via SPI.
  7. * Multiple chips can be daisy-chained, the spec does not impose
  8. * a limit on the number of chips and neither does this driver.
  9. *
  10. * Either of two modes is selectable: In 8-bit mode, only the state
  11. * of the inputs is clocked out to achieve high readout speeds;
  12. * In 16-bit mode, an additional status byte is clocked out with
  13. * a CRC and indicator bits for undervoltage and overtemperature.
  14. * The driver returns an error instead of potentially bogus data
  15. * if any of these fault conditions occur. However it does allow
  16. * readout of non-faulting chips in the same daisy-chain.
  17. *
  18. * MAX3191x supports four debounce settings and the driver is
  19. * capable of configuring these differently for each chip in the
  20. * daisy-chain.
  21. *
  22. * If the chips are hardwired to 8-bit mode ("modesel" pulled high),
  23. * gpio-pisosr.c can be used alternatively to this driver.
  24. *
  25. * https://datasheets.maximintegrated.com/en/ds/MAX31910.pdf
  26. * https://datasheets.maximintegrated.com/en/ds/MAX31911.pdf
  27. * https://datasheets.maximintegrated.com/en/ds/MAX31912.pdf
  28. * https://datasheets.maximintegrated.com/en/ds/MAX31913.pdf
  29. * https://datasheets.maximintegrated.com/en/ds/MAX31953-MAX31963.pdf
  30. *
  31. * This program is free software; you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License (version 2) as
  33. * published by the Free Software Foundation.
  34. */
  35. #include <linux/bitmap.h>
  36. #include <linux/crc8.h>
  37. #include <linux/gpio/consumer.h>
  38. #include <linux/gpio/driver.h>
  39. #include <linux/module.h>
  40. #include <linux/spi/spi.h>
  41. enum max3191x_mode {
  42. STATUS_BYTE_ENABLED,
  43. STATUS_BYTE_DISABLED,
  44. };
  45. /**
  46. * struct max3191x_chip - max3191x daisy-chain
  47. * @gpio: GPIO controller struct
  48. * @lock: protects read sequences
  49. * @nchips: number of chips in the daisy-chain
  50. * @mode: current mode, 0 for 16-bit, 1 for 8-bit;
  51. * for simplicity, all chips in the daisy-chain are assumed
  52. * to use the same mode
  53. * @modesel_pins: GPIO pins to configure modesel of each chip
  54. * @fault_pins: GPIO pins to detect fault of each chip
  55. * @db0_pins: GPIO pins to configure debounce of each chip
  56. * @db1_pins: GPIO pins to configure debounce of each chip
  57. * @mesg: SPI message to perform a readout
  58. * @xfer: SPI transfer used by @mesg
  59. * @crc_error: bitmap signaling CRC error for each chip
  60. * @overtemp: bitmap signaling overtemperature alarm for each chip
  61. * @undervolt1: bitmap signaling undervoltage alarm for each chip
  62. * @undervolt2: bitmap signaling undervoltage warning for each chip
  63. * @fault: bitmap signaling assertion of @fault_pins for each chip
  64. * @ignore_uv: whether to ignore undervoltage alarms;
  65. * set by a device property if the chips are powered through
  66. * 5VOUT instead of VCC24V, in which case they will constantly
  67. * signal undervoltage;
  68. * for simplicity, all chips in the daisy-chain are assumed
  69. * to be powered the same way
  70. */
  71. struct max3191x_chip {
  72. struct gpio_chip gpio;
  73. struct mutex lock;
  74. u32 nchips;
  75. enum max3191x_mode mode;
  76. struct gpio_descs *modesel_pins;
  77. struct gpio_descs *fault_pins;
  78. struct gpio_descs *db0_pins;
  79. struct gpio_descs *db1_pins;
  80. struct spi_message mesg;
  81. struct spi_transfer xfer;
  82. unsigned long *crc_error;
  83. unsigned long *overtemp;
  84. unsigned long *undervolt1;
  85. unsigned long *undervolt2;
  86. unsigned long *fault;
  87. bool ignore_uv;
  88. };
  89. #define MAX3191X_NGPIO 8
  90. #define MAX3191X_CRC8_POLYNOMIAL 0xa8 /* (x^5) + x^4 + x^2 + x^0 */
  91. DECLARE_CRC8_TABLE(max3191x_crc8);
  92. static int max3191x_get_direction(struct gpio_chip *gpio, unsigned int offset)
  93. {
  94. return 1; /* always in */
  95. }
  96. static int max3191x_direction_input(struct gpio_chip *gpio, unsigned int offset)
  97. {
  98. return 0;
  99. }
  100. static int max3191x_direction_output(struct gpio_chip *gpio,
  101. unsigned int offset, int value)
  102. {
  103. return -EINVAL;
  104. }
  105. static void max3191x_set(struct gpio_chip *gpio, unsigned int offset, int value)
  106. { }
  107. static void max3191x_set_multiple(struct gpio_chip *gpio, unsigned long *mask,
  108. unsigned long *bits)
  109. { }
  110. static unsigned int max3191x_wordlen(struct max3191x_chip *max3191x)
  111. {
  112. return max3191x->mode == STATUS_BYTE_ENABLED ? 2 : 1;
  113. }
  114. static int max3191x_readout_locked(struct max3191x_chip *max3191x)
  115. {
  116. struct device *dev = max3191x->gpio.parent;
  117. struct spi_device *spi = to_spi_device(dev);
  118. int val, i, ot = 0, uv1 = 0;
  119. val = spi_sync(spi, &max3191x->mesg);
  120. if (val) {
  121. dev_err_ratelimited(dev, "SPI receive error %d\n", val);
  122. return val;
  123. }
  124. for (i = 0; i < max3191x->nchips; i++) {
  125. if (max3191x->mode == STATUS_BYTE_ENABLED) {
  126. u8 in = ((u8 *)max3191x->xfer.rx_buf)[i * 2];
  127. u8 status = ((u8 *)max3191x->xfer.rx_buf)[i * 2 + 1];
  128. val = (status & 0xf8) != crc8(max3191x_crc8, &in, 1, 0);
  129. __assign_bit(i, max3191x->crc_error, val);
  130. if (val)
  131. dev_err_ratelimited(dev,
  132. "chip %d: CRC error\n", i);
  133. ot = (status >> 1) & 1;
  134. __assign_bit(i, max3191x->overtemp, ot);
  135. if (ot)
  136. dev_err_ratelimited(dev,
  137. "chip %d: overtemperature\n", i);
  138. if (!max3191x->ignore_uv) {
  139. uv1 = !((status >> 2) & 1);
  140. __assign_bit(i, max3191x->undervolt1, uv1);
  141. if (uv1)
  142. dev_err_ratelimited(dev,
  143. "chip %d: undervoltage\n", i);
  144. val = !(status & 1);
  145. __assign_bit(i, max3191x->undervolt2, val);
  146. if (val && !uv1)
  147. dev_warn_ratelimited(dev,
  148. "chip %d: voltage warn\n", i);
  149. }
  150. }
  151. if (max3191x->fault_pins && !max3191x->ignore_uv) {
  152. /* fault pin shared by all chips or per chip */
  153. struct gpio_desc *fault_pin =
  154. (max3191x->fault_pins->ndescs == 1)
  155. ? max3191x->fault_pins->desc[0]
  156. : max3191x->fault_pins->desc[i];
  157. val = gpiod_get_value_cansleep(fault_pin);
  158. if (val < 0) {
  159. dev_err_ratelimited(dev,
  160. "GPIO read error %d\n", val);
  161. return val;
  162. }
  163. __assign_bit(i, max3191x->fault, val);
  164. if (val && !uv1 && !ot)
  165. dev_err_ratelimited(dev,
  166. "chip %d: fault\n", i);
  167. }
  168. }
  169. return 0;
  170. }
  171. static bool max3191x_chip_is_faulting(struct max3191x_chip *max3191x,
  172. unsigned int chipnum)
  173. {
  174. /* without status byte the only diagnostic is the fault pin */
  175. if (!max3191x->ignore_uv && test_bit(chipnum, max3191x->fault))
  176. return true;
  177. if (max3191x->mode == STATUS_BYTE_DISABLED)
  178. return false;
  179. return test_bit(chipnum, max3191x->crc_error) ||
  180. test_bit(chipnum, max3191x->overtemp) ||
  181. (!max3191x->ignore_uv &&
  182. test_bit(chipnum, max3191x->undervolt1));
  183. }
  184. static int max3191x_get(struct gpio_chip *gpio, unsigned int offset)
  185. {
  186. struct max3191x_chip *max3191x = gpiochip_get_data(gpio);
  187. int ret, chipnum, wordlen = max3191x_wordlen(max3191x);
  188. u8 in;
  189. mutex_lock(&max3191x->lock);
  190. ret = max3191x_readout_locked(max3191x);
  191. if (ret)
  192. goto out_unlock;
  193. chipnum = offset / MAX3191X_NGPIO;
  194. if (max3191x_chip_is_faulting(max3191x, chipnum)) {
  195. ret = -EIO;
  196. goto out_unlock;
  197. }
  198. in = ((u8 *)max3191x->xfer.rx_buf)[chipnum * wordlen];
  199. ret = (in >> (offset % MAX3191X_NGPIO)) & 1;
  200. out_unlock:
  201. mutex_unlock(&max3191x->lock);
  202. return ret;
  203. }
  204. static int max3191x_get_multiple(struct gpio_chip *gpio, unsigned long *mask,
  205. unsigned long *bits)
  206. {
  207. struct max3191x_chip *max3191x = gpiochip_get_data(gpio);
  208. int ret, bit = 0, wordlen = max3191x_wordlen(max3191x);
  209. mutex_lock(&max3191x->lock);
  210. ret = max3191x_readout_locked(max3191x);
  211. if (ret)
  212. goto out_unlock;
  213. while ((bit = find_next_bit(mask, gpio->ngpio, bit)) != gpio->ngpio) {
  214. unsigned int chipnum = bit / MAX3191X_NGPIO;
  215. unsigned long in, shift, index;
  216. if (max3191x_chip_is_faulting(max3191x, chipnum)) {
  217. ret = -EIO;
  218. goto out_unlock;
  219. }
  220. in = ((u8 *)max3191x->xfer.rx_buf)[chipnum * wordlen];
  221. shift = round_down(bit % BITS_PER_LONG, MAX3191X_NGPIO);
  222. index = bit / BITS_PER_LONG;
  223. bits[index] &= ~(mask[index] & (0xff << shift));
  224. bits[index] |= mask[index] & (in << shift); /* copy bits */
  225. bit = (chipnum + 1) * MAX3191X_NGPIO; /* go to next chip */
  226. }
  227. out_unlock:
  228. mutex_unlock(&max3191x->lock);
  229. return ret;
  230. }
  231. static int max3191x_set_config(struct gpio_chip *gpio, unsigned int offset,
  232. unsigned long config)
  233. {
  234. struct max3191x_chip *max3191x = gpiochip_get_data(gpio);
  235. u32 debounce, chipnum, db0_val, db1_val;
  236. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  237. return -ENOTSUPP;
  238. if (!max3191x->db0_pins || !max3191x->db1_pins)
  239. return -EINVAL;
  240. debounce = pinconf_to_config_argument(config);
  241. switch (debounce) {
  242. case 0:
  243. db0_val = 0;
  244. db1_val = 0;
  245. break;
  246. case 1 ... 25:
  247. db0_val = 0;
  248. db1_val = 1;
  249. break;
  250. case 26 ... 750:
  251. db0_val = 1;
  252. db1_val = 0;
  253. break;
  254. case 751 ... 3000:
  255. db0_val = 1;
  256. db1_val = 1;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. if (max3191x->db0_pins->ndescs == 1)
  262. chipnum = 0; /* all chips use the same pair of debounce pins */
  263. else
  264. chipnum = offset / MAX3191X_NGPIO; /* per chip debounce pins */
  265. mutex_lock(&max3191x->lock);
  266. gpiod_set_value_cansleep(max3191x->db0_pins->desc[chipnum], db0_val);
  267. gpiod_set_value_cansleep(max3191x->db1_pins->desc[chipnum], db1_val);
  268. mutex_unlock(&max3191x->lock);
  269. return 0;
  270. }
  271. static void gpiod_set_array_single_value_cansleep(unsigned int ndescs,
  272. struct gpio_desc **desc,
  273. int value)
  274. {
  275. int i, *values;
  276. values = kmalloc_array(ndescs, sizeof(*values), GFP_KERNEL);
  277. if (!values)
  278. return;
  279. for (i = 0; i < ndescs; i++)
  280. values[i] = value;
  281. gpiod_set_array_value_cansleep(ndescs, desc, values);
  282. kfree(values);
  283. }
  284. static struct gpio_descs *devm_gpiod_get_array_optional_count(
  285. struct device *dev, const char *con_id,
  286. enum gpiod_flags flags, unsigned int expected)
  287. {
  288. struct gpio_descs *descs;
  289. int found = gpiod_count(dev, con_id);
  290. if (found == -ENOENT)
  291. return NULL;
  292. if (found != expected && found != 1) {
  293. dev_err(dev, "ignoring %s-gpios: found %d, expected %u or 1\n",
  294. con_id, found, expected);
  295. return NULL;
  296. }
  297. descs = devm_gpiod_get_array_optional(dev, con_id, flags);
  298. if (IS_ERR(descs)) {
  299. dev_err(dev, "failed to get %s-gpios: %ld\n",
  300. con_id, PTR_ERR(descs));
  301. return NULL;
  302. }
  303. return descs;
  304. }
  305. static int max3191x_probe(struct spi_device *spi)
  306. {
  307. struct device *dev = &spi->dev;
  308. struct max3191x_chip *max3191x;
  309. int n, ret;
  310. max3191x = devm_kzalloc(dev, sizeof(*max3191x), GFP_KERNEL);
  311. if (!max3191x)
  312. return -ENOMEM;
  313. spi_set_drvdata(spi, max3191x);
  314. max3191x->nchips = 1;
  315. device_property_read_u32(dev, "#daisy-chained-devices",
  316. &max3191x->nchips);
  317. n = BITS_TO_LONGS(max3191x->nchips);
  318. max3191x->crc_error = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
  319. max3191x->undervolt1 = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
  320. max3191x->undervolt2 = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
  321. max3191x->overtemp = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
  322. max3191x->fault = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
  323. max3191x->xfer.rx_buf = devm_kcalloc(dev, max3191x->nchips,
  324. 2, GFP_KERNEL);
  325. if (!max3191x->crc_error || !max3191x->undervolt1 ||
  326. !max3191x->overtemp || !max3191x->undervolt2 ||
  327. !max3191x->fault || !max3191x->xfer.rx_buf)
  328. return -ENOMEM;
  329. max3191x->modesel_pins = devm_gpiod_get_array_optional_count(dev,
  330. "maxim,modesel", GPIOD_ASIS, max3191x->nchips);
  331. max3191x->fault_pins = devm_gpiod_get_array_optional_count(dev,
  332. "maxim,fault", GPIOD_IN, max3191x->nchips);
  333. max3191x->db0_pins = devm_gpiod_get_array_optional_count(dev,
  334. "maxim,db0", GPIOD_OUT_LOW, max3191x->nchips);
  335. max3191x->db1_pins = devm_gpiod_get_array_optional_count(dev,
  336. "maxim,db1", GPIOD_OUT_LOW, max3191x->nchips);
  337. max3191x->mode = device_property_read_bool(dev, "maxim,modesel-8bit")
  338. ? STATUS_BYTE_DISABLED : STATUS_BYTE_ENABLED;
  339. if (max3191x->modesel_pins)
  340. gpiod_set_array_single_value_cansleep(
  341. max3191x->modesel_pins->ndescs,
  342. max3191x->modesel_pins->desc, max3191x->mode);
  343. max3191x->ignore_uv = device_property_read_bool(dev,
  344. "maxim,ignore-undervoltage");
  345. if (max3191x->db0_pins && max3191x->db1_pins &&
  346. max3191x->db0_pins->ndescs != max3191x->db1_pins->ndescs) {
  347. dev_err(dev, "ignoring maxim,db*-gpios: array len mismatch\n");
  348. devm_gpiod_put_array(dev, max3191x->db0_pins);
  349. devm_gpiod_put_array(dev, max3191x->db1_pins);
  350. max3191x->db0_pins = NULL;
  351. max3191x->db1_pins = NULL;
  352. }
  353. max3191x->xfer.len = max3191x->nchips * max3191x_wordlen(max3191x);
  354. spi_message_init_with_transfers(&max3191x->mesg, &max3191x->xfer, 1);
  355. max3191x->gpio.label = spi->modalias;
  356. max3191x->gpio.owner = THIS_MODULE;
  357. max3191x->gpio.parent = dev;
  358. max3191x->gpio.base = -1;
  359. max3191x->gpio.ngpio = max3191x->nchips * MAX3191X_NGPIO;
  360. max3191x->gpio.can_sleep = true;
  361. max3191x->gpio.get_direction = max3191x_get_direction;
  362. max3191x->gpio.direction_input = max3191x_direction_input;
  363. max3191x->gpio.direction_output = max3191x_direction_output;
  364. max3191x->gpio.set = max3191x_set;
  365. max3191x->gpio.set_multiple = max3191x_set_multiple;
  366. max3191x->gpio.get = max3191x_get;
  367. max3191x->gpio.get_multiple = max3191x_get_multiple;
  368. max3191x->gpio.set_config = max3191x_set_config;
  369. mutex_init(&max3191x->lock);
  370. ret = gpiochip_add_data(&max3191x->gpio, max3191x);
  371. if (ret) {
  372. mutex_destroy(&max3191x->lock);
  373. return ret;
  374. }
  375. return 0;
  376. }
  377. static int max3191x_remove(struct spi_device *spi)
  378. {
  379. struct max3191x_chip *max3191x = spi_get_drvdata(spi);
  380. gpiochip_remove(&max3191x->gpio);
  381. mutex_destroy(&max3191x->lock);
  382. return 0;
  383. }
  384. static int __init max3191x_register_driver(struct spi_driver *sdrv)
  385. {
  386. crc8_populate_msb(max3191x_crc8, MAX3191X_CRC8_POLYNOMIAL);
  387. return spi_register_driver(sdrv);
  388. }
  389. #ifdef CONFIG_OF
  390. static const struct of_device_id max3191x_of_id[] = {
  391. { .compatible = "maxim,max31910" },
  392. { .compatible = "maxim,max31911" },
  393. { .compatible = "maxim,max31912" },
  394. { .compatible = "maxim,max31913" },
  395. { .compatible = "maxim,max31953" },
  396. { .compatible = "maxim,max31963" },
  397. { }
  398. };
  399. MODULE_DEVICE_TABLE(of, max3191x_of_id);
  400. #endif
  401. static const struct spi_device_id max3191x_spi_id[] = {
  402. { "max31910" },
  403. { "max31911" },
  404. { "max31912" },
  405. { "max31913" },
  406. { "max31953" },
  407. { "max31963" },
  408. { }
  409. };
  410. MODULE_DEVICE_TABLE(spi, max3191x_spi_id);
  411. static struct spi_driver max3191x_driver = {
  412. .driver = {
  413. .name = "max3191x",
  414. .of_match_table = of_match_ptr(max3191x_of_id),
  415. },
  416. .probe = max3191x_probe,
  417. .remove = max3191x_remove,
  418. .id_table = max3191x_spi_id,
  419. };
  420. module_driver(max3191x_driver, max3191x_register_driver, spi_unregister_driver);
  421. MODULE_AUTHOR("Lukas Wunner <lukas@wunner.de>");
  422. MODULE_DESCRIPTION("GPIO driver for Maxim MAX3191x industrial serializer");
  423. MODULE_LICENSE("GPL v2");