ts-nbus.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NBUS driver for TS-4600 based boards
  4. *
  5. * Copyright (c) 2016 - Savoir-faire Linux
  6. * Author: Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
  7. *
  8. * This driver implements a GPIOs bit-banged bus, called the NBUS by Technologic
  9. * Systems. It is used to communicate with the peripherals in the FPGA on the
  10. * TS-4600 SoM.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pwm.h>
  20. #include <linux/ts-nbus.h>
  21. #define TS_NBUS_DIRECTION_IN 0
  22. #define TS_NBUS_DIRECTION_OUT 1
  23. #define TS_NBUS_WRITE_ADR 0
  24. #define TS_NBUS_WRITE_VAL 1
  25. struct ts_nbus {
  26. struct pwm_device *pwm;
  27. struct gpio_descs *data;
  28. struct gpio_desc *csn;
  29. struct gpio_desc *txrx;
  30. struct gpio_desc *strobe;
  31. struct gpio_desc *ale;
  32. struct gpio_desc *rdy;
  33. struct mutex lock;
  34. };
  35. /*
  36. * request all gpios required by the bus.
  37. */
  38. static int ts_nbus_init_pdata(struct platform_device *pdev,
  39. struct ts_nbus *ts_nbus)
  40. {
  41. ts_nbus->data = devm_gpiod_get_array(&pdev->dev, "ts,data",
  42. GPIOD_OUT_HIGH);
  43. if (IS_ERR(ts_nbus->data))
  44. return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->data),
  45. "failed to retrieve ts,data-gpio from dts\n");
  46. ts_nbus->csn = devm_gpiod_get(&pdev->dev, "ts,csn", GPIOD_OUT_HIGH);
  47. if (IS_ERR(ts_nbus->csn))
  48. return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->csn),
  49. "failed to retrieve ts,csn-gpio from dts\n");
  50. ts_nbus->txrx = devm_gpiod_get(&pdev->dev, "ts,txrx", GPIOD_OUT_HIGH);
  51. if (IS_ERR(ts_nbus->txrx))
  52. return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->txrx),
  53. "failed to retrieve ts,txrx-gpio from dts\n");
  54. ts_nbus->strobe = devm_gpiod_get(&pdev->dev, "ts,strobe", GPIOD_OUT_HIGH);
  55. if (IS_ERR(ts_nbus->strobe))
  56. return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->strobe),
  57. "failed to retrieve ts,strobe-gpio from dts\n");
  58. ts_nbus->ale = devm_gpiod_get(&pdev->dev, "ts,ale", GPIOD_OUT_HIGH);
  59. if (IS_ERR(ts_nbus->ale))
  60. return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->ale),
  61. "failed to retrieve ts,ale-gpio from dts\n");
  62. ts_nbus->rdy = devm_gpiod_get(&pdev->dev, "ts,rdy", GPIOD_IN);
  63. if (IS_ERR(ts_nbus->rdy))
  64. return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->rdy),
  65. "failed to retrieve ts,rdy-gpio from dts\n");
  66. return 0;
  67. }
  68. /*
  69. * the data gpios are used for reading and writing values, their directions
  70. * should be adjusted accordingly.
  71. */
  72. static void ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
  73. {
  74. int i;
  75. for (i = 0; i < 8; i++) {
  76. if (direction == TS_NBUS_DIRECTION_IN)
  77. gpiod_direction_input(ts_nbus->data->desc[i]);
  78. else
  79. /* when used as output the default state of the data
  80. * lines are set to high */
  81. gpiod_direction_output(ts_nbus->data->desc[i], 1);
  82. }
  83. }
  84. /*
  85. * reset the bus in its initial state.
  86. * The data, csn, strobe and ale lines must be zero'ed to let the FPGA knows a
  87. * new transaction can be process.
  88. */
  89. static void ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
  90. {
  91. DECLARE_BITMAP(values, 8);
  92. values[0] = 0;
  93. gpiod_set_array_value_cansleep(8, ts_nbus->data->desc,
  94. ts_nbus->data->info, values);
  95. gpiod_set_value_cansleep(ts_nbus->csn, 0);
  96. gpiod_set_value_cansleep(ts_nbus->strobe, 0);
  97. gpiod_set_value_cansleep(ts_nbus->ale, 0);
  98. }
  99. /*
  100. * let the FPGA knows it can process.
  101. */
  102. static void ts_nbus_start_transaction(struct ts_nbus *ts_nbus)
  103. {
  104. gpiod_set_value_cansleep(ts_nbus->strobe, 1);
  105. }
  106. /*
  107. * read a byte value from the data gpios.
  108. * return 0 on success or negative errno on failure.
  109. */
  110. static int ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
  111. {
  112. struct gpio_descs *gpios = ts_nbus->data;
  113. int ret, i;
  114. *val = 0;
  115. for (i = 0; i < 8; i++) {
  116. ret = gpiod_get_value_cansleep(gpios->desc[i]);
  117. if (ret < 0)
  118. return ret;
  119. if (ret)
  120. *val |= BIT(i);
  121. }
  122. return 0;
  123. }
  124. /*
  125. * set the data gpios accordingly to the byte value.
  126. */
  127. static void ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
  128. {
  129. struct gpio_descs *gpios = ts_nbus->data;
  130. DECLARE_BITMAP(values, 8);
  131. values[0] = byte;
  132. gpiod_set_array_value_cansleep(8, gpios->desc, gpios->info, values);
  133. }
  134. /*
  135. * reading the bus consists of resetting the bus, then notifying the FPGA to
  136. * send the data in the data gpios and return the read value.
  137. * return 0 on success or negative errno on failure.
  138. */
  139. static int ts_nbus_read_bus(struct ts_nbus *ts_nbus, u8 *val)
  140. {
  141. ts_nbus_reset_bus(ts_nbus);
  142. ts_nbus_start_transaction(ts_nbus);
  143. return ts_nbus_read_byte(ts_nbus, val);
  144. }
  145. /*
  146. * writing to the bus consists of resetting the bus, then define the type of
  147. * command (address/value), write the data and notify the FPGA to retrieve the
  148. * value in the data gpios.
  149. */
  150. static void ts_nbus_write_bus(struct ts_nbus *ts_nbus, int cmd, u8 val)
  151. {
  152. ts_nbus_reset_bus(ts_nbus);
  153. if (cmd == TS_NBUS_WRITE_ADR)
  154. gpiod_set_value_cansleep(ts_nbus->ale, 1);
  155. ts_nbus_write_byte(ts_nbus, val);
  156. ts_nbus_start_transaction(ts_nbus);
  157. }
  158. /*
  159. * read the value in the FPGA register at the given address.
  160. * return 0 on success or negative errno on failure.
  161. */
  162. int ts_nbus_read(struct ts_nbus *ts_nbus, u8 adr, u16 *val)
  163. {
  164. int ret, i;
  165. u8 byte;
  166. /* bus access must be atomic */
  167. mutex_lock(&ts_nbus->lock);
  168. /* set the bus in read mode */
  169. gpiod_set_value_cansleep(ts_nbus->txrx, 0);
  170. /* write address */
  171. ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
  172. /* set the data gpios direction as input before reading */
  173. ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_IN);
  174. /* reading value MSB first */
  175. do {
  176. *val = 0;
  177. byte = 0;
  178. for (i = 1; i >= 0; i--) {
  179. /* read a byte from the bus, leave on error */
  180. ret = ts_nbus_read_bus(ts_nbus, &byte);
  181. if (ret < 0)
  182. goto err;
  183. /* append the byte read to the final value */
  184. *val |= byte << (i * 8);
  185. }
  186. gpiod_set_value_cansleep(ts_nbus->csn, 1);
  187. ret = gpiod_get_value_cansleep(ts_nbus->rdy);
  188. } while (ret);
  189. err:
  190. /* restore the data gpios direction as output after reading */
  191. ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_OUT);
  192. mutex_unlock(&ts_nbus->lock);
  193. return ret;
  194. }
  195. EXPORT_SYMBOL_GPL(ts_nbus_read);
  196. /*
  197. * write the desired value in the FPGA register at the given address.
  198. */
  199. int ts_nbus_write(struct ts_nbus *ts_nbus, u8 adr, u16 val)
  200. {
  201. int i;
  202. /* bus access must be atomic */
  203. mutex_lock(&ts_nbus->lock);
  204. /* set the bus in write mode */
  205. gpiod_set_value_cansleep(ts_nbus->txrx, 1);
  206. /* write address */
  207. ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
  208. /* writing value MSB first */
  209. for (i = 1; i >= 0; i--)
  210. ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_VAL, (u8)(val >> (i * 8)));
  211. /* wait for completion */
  212. gpiod_set_value_cansleep(ts_nbus->csn, 1);
  213. while (gpiod_get_value_cansleep(ts_nbus->rdy) != 0) {
  214. gpiod_set_value_cansleep(ts_nbus->csn, 0);
  215. gpiod_set_value_cansleep(ts_nbus->csn, 1);
  216. }
  217. mutex_unlock(&ts_nbus->lock);
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(ts_nbus_write);
  221. static int ts_nbus_probe(struct platform_device *pdev)
  222. {
  223. struct pwm_device *pwm;
  224. struct pwm_state state;
  225. struct device *dev = &pdev->dev;
  226. struct ts_nbus *ts_nbus;
  227. int ret;
  228. ts_nbus = devm_kzalloc(dev, sizeof(*ts_nbus), GFP_KERNEL);
  229. if (!ts_nbus)
  230. return -ENOMEM;
  231. mutex_init(&ts_nbus->lock);
  232. ret = ts_nbus_init_pdata(pdev, ts_nbus);
  233. if (ret < 0)
  234. return ret;
  235. pwm = devm_pwm_get(dev, NULL);
  236. if (IS_ERR(pwm))
  237. return dev_err_probe(dev, PTR_ERR(pwm),
  238. "unable to request PWM\n");
  239. pwm_init_state(pwm, &state);
  240. if (!state.period)
  241. return dev_err_probe(dev, -EINVAL, "invalid PWM period\n");
  242. state.duty_cycle = state.period;
  243. state.enabled = true;
  244. ret = pwm_apply_might_sleep(pwm, &state);
  245. if (ret < 0)
  246. return dev_err_probe(dev, ret, "failed to configure PWM\n");
  247. /*
  248. * we can now start the FPGA and populate the peripherals.
  249. */
  250. ts_nbus->pwm = pwm;
  251. /*
  252. * let the child nodes retrieve this instance of the ts-nbus.
  253. */
  254. dev_set_drvdata(dev, ts_nbus);
  255. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  256. if (ret < 0)
  257. return dev_err_probe(dev, ret,
  258. "failed to populate platform devices on bus\n");
  259. dev_info(dev, "initialized\n");
  260. return 0;
  261. }
  262. static void ts_nbus_remove(struct platform_device *pdev)
  263. {
  264. struct ts_nbus *ts_nbus = dev_get_drvdata(&pdev->dev);
  265. /* shutdown the FPGA */
  266. mutex_lock(&ts_nbus->lock);
  267. pwm_disable(ts_nbus->pwm);
  268. mutex_unlock(&ts_nbus->lock);
  269. }
  270. static const struct of_device_id ts_nbus_of_match[] = {
  271. { .compatible = "technologic,ts-nbus", },
  272. { },
  273. };
  274. MODULE_DEVICE_TABLE(of, ts_nbus_of_match);
  275. static struct platform_driver ts_nbus_driver = {
  276. .probe = ts_nbus_probe,
  277. .remove_new = ts_nbus_remove,
  278. .driver = {
  279. .name = "ts_nbus",
  280. .of_match_table = ts_nbus_of_match,
  281. },
  282. };
  283. module_platform_driver(ts_nbus_driver);
  284. MODULE_ALIAS("platform:ts_nbus");
  285. MODULE_AUTHOR("Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>");
  286. MODULE_DESCRIPTION("Technologic Systems NBUS");
  287. MODULE_LICENSE("GPL v2");