ts-nbus.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <[email protected]>
  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, struct ts_nbus
  39. *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. dev_err(&pdev->dev, "failed to retrieve ts,data-gpio from dts\n");
  45. return PTR_ERR(ts_nbus->data);
  46. }
  47. ts_nbus->csn = devm_gpiod_get(&pdev->dev, "ts,csn", GPIOD_OUT_HIGH);
  48. if (IS_ERR(ts_nbus->csn)) {
  49. dev_err(&pdev->dev, "failed to retrieve ts,csn-gpio from dts\n");
  50. return PTR_ERR(ts_nbus->csn);
  51. }
  52. ts_nbus->txrx = devm_gpiod_get(&pdev->dev, "ts,txrx", GPIOD_OUT_HIGH);
  53. if (IS_ERR(ts_nbus->txrx)) {
  54. dev_err(&pdev->dev, "failed to retrieve ts,txrx-gpio from dts\n");
  55. return PTR_ERR(ts_nbus->txrx);
  56. }
  57. ts_nbus->strobe = devm_gpiod_get(&pdev->dev, "ts,strobe", GPIOD_OUT_HIGH);
  58. if (IS_ERR(ts_nbus->strobe)) {
  59. dev_err(&pdev->dev, "failed to retrieve ts,strobe-gpio from dts\n");
  60. return PTR_ERR(ts_nbus->strobe);
  61. }
  62. ts_nbus->ale = devm_gpiod_get(&pdev->dev, "ts,ale", GPIOD_OUT_HIGH);
  63. if (IS_ERR(ts_nbus->ale)) {
  64. dev_err(&pdev->dev, "failed to retrieve ts,ale-gpio from dts\n");
  65. return PTR_ERR(ts_nbus->ale);
  66. }
  67. ts_nbus->rdy = devm_gpiod_get(&pdev->dev, "ts,rdy", GPIOD_IN);
  68. if (IS_ERR(ts_nbus->rdy)) {
  69. dev_err(&pdev->dev, "failed to retrieve ts,rdy-gpio from dts\n");
  70. return PTR_ERR(ts_nbus->rdy);
  71. }
  72. return 0;
  73. }
  74. /*
  75. * the data gpios are used for reading and writing values, their directions
  76. * should be adjusted accordingly.
  77. */
  78. static void ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
  79. {
  80. int i;
  81. for (i = 0; i < 8; i++) {
  82. if (direction == TS_NBUS_DIRECTION_IN)
  83. gpiod_direction_input(ts_nbus->data->desc[i]);
  84. else
  85. /* when used as output the default state of the data
  86. * lines are set to high */
  87. gpiod_direction_output(ts_nbus->data->desc[i], 1);
  88. }
  89. }
  90. /*
  91. * reset the bus in its initial state.
  92. * The data, csn, strobe and ale lines must be zero'ed to let the FPGA knows a
  93. * new transaction can be process.
  94. */
  95. static void ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
  96. {
  97. DECLARE_BITMAP(values, 8);
  98. values[0] = 0;
  99. gpiod_set_array_value_cansleep(8, ts_nbus->data->desc,
  100. ts_nbus->data->info, values);
  101. gpiod_set_value_cansleep(ts_nbus->csn, 0);
  102. gpiod_set_value_cansleep(ts_nbus->strobe, 0);
  103. gpiod_set_value_cansleep(ts_nbus->ale, 0);
  104. }
  105. /*
  106. * let the FPGA knows it can process.
  107. */
  108. static void ts_nbus_start_transaction(struct ts_nbus *ts_nbus)
  109. {
  110. gpiod_set_value_cansleep(ts_nbus->strobe, 1);
  111. }
  112. /*
  113. * read a byte value from the data gpios.
  114. * return 0 on success or negative errno on failure.
  115. */
  116. static int ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
  117. {
  118. struct gpio_descs *gpios = ts_nbus->data;
  119. int ret, i;
  120. *val = 0;
  121. for (i = 0; i < 8; i++) {
  122. ret = gpiod_get_value_cansleep(gpios->desc[i]);
  123. if (ret < 0)
  124. return ret;
  125. if (ret)
  126. *val |= BIT(i);
  127. }
  128. return 0;
  129. }
  130. /*
  131. * set the data gpios accordingly to the byte value.
  132. */
  133. static void ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
  134. {
  135. struct gpio_descs *gpios = ts_nbus->data;
  136. DECLARE_BITMAP(values, 8);
  137. values[0] = byte;
  138. gpiod_set_array_value_cansleep(8, gpios->desc, gpios->info, values);
  139. }
  140. /*
  141. * reading the bus consists of resetting the bus, then notifying the FPGA to
  142. * send the data in the data gpios and return the read value.
  143. * return 0 on success or negative errno on failure.
  144. */
  145. static int ts_nbus_read_bus(struct ts_nbus *ts_nbus, u8 *val)
  146. {
  147. ts_nbus_reset_bus(ts_nbus);
  148. ts_nbus_start_transaction(ts_nbus);
  149. return ts_nbus_read_byte(ts_nbus, val);
  150. }
  151. /*
  152. * writing to the bus consists of resetting the bus, then define the type of
  153. * command (address/value), write the data and notify the FPGA to retrieve the
  154. * value in the data gpios.
  155. */
  156. static void ts_nbus_write_bus(struct ts_nbus *ts_nbus, int cmd, u8 val)
  157. {
  158. ts_nbus_reset_bus(ts_nbus);
  159. if (cmd == TS_NBUS_WRITE_ADR)
  160. gpiod_set_value_cansleep(ts_nbus->ale, 1);
  161. ts_nbus_write_byte(ts_nbus, val);
  162. ts_nbus_start_transaction(ts_nbus);
  163. }
  164. /*
  165. * read the value in the FPGA register at the given address.
  166. * return 0 on success or negative errno on failure.
  167. */
  168. int ts_nbus_read(struct ts_nbus *ts_nbus, u8 adr, u16 *val)
  169. {
  170. int ret, i;
  171. u8 byte;
  172. /* bus access must be atomic */
  173. mutex_lock(&ts_nbus->lock);
  174. /* set the bus in read mode */
  175. gpiod_set_value_cansleep(ts_nbus->txrx, 0);
  176. /* write address */
  177. ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
  178. /* set the data gpios direction as input before reading */
  179. ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_IN);
  180. /* reading value MSB first */
  181. do {
  182. *val = 0;
  183. byte = 0;
  184. for (i = 1; i >= 0; i--) {
  185. /* read a byte from the bus, leave on error */
  186. ret = ts_nbus_read_bus(ts_nbus, &byte);
  187. if (ret < 0)
  188. goto err;
  189. /* append the byte read to the final value */
  190. *val |= byte << (i * 8);
  191. }
  192. gpiod_set_value_cansleep(ts_nbus->csn, 1);
  193. ret = gpiod_get_value_cansleep(ts_nbus->rdy);
  194. } while (ret);
  195. err:
  196. /* restore the data gpios direction as output after reading */
  197. ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_OUT);
  198. mutex_unlock(&ts_nbus->lock);
  199. return ret;
  200. }
  201. EXPORT_SYMBOL_GPL(ts_nbus_read);
  202. /*
  203. * write the desired value in the FPGA register at the given address.
  204. */
  205. int ts_nbus_write(struct ts_nbus *ts_nbus, u8 adr, u16 val)
  206. {
  207. int i;
  208. /* bus access must be atomic */
  209. mutex_lock(&ts_nbus->lock);
  210. /* set the bus in write mode */
  211. gpiod_set_value_cansleep(ts_nbus->txrx, 1);
  212. /* write address */
  213. ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
  214. /* writing value MSB first */
  215. for (i = 1; i >= 0; i--)
  216. ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_VAL, (u8)(val >> (i * 8)));
  217. /* wait for completion */
  218. gpiod_set_value_cansleep(ts_nbus->csn, 1);
  219. while (gpiod_get_value_cansleep(ts_nbus->rdy) != 0) {
  220. gpiod_set_value_cansleep(ts_nbus->csn, 0);
  221. gpiod_set_value_cansleep(ts_nbus->csn, 1);
  222. }
  223. mutex_unlock(&ts_nbus->lock);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(ts_nbus_write);
  227. static int ts_nbus_probe(struct platform_device *pdev)
  228. {
  229. struct pwm_device *pwm;
  230. struct pwm_args pargs;
  231. struct device *dev = &pdev->dev;
  232. struct ts_nbus *ts_nbus;
  233. int ret;
  234. ts_nbus = devm_kzalloc(dev, sizeof(*ts_nbus), GFP_KERNEL);
  235. if (!ts_nbus)
  236. return -ENOMEM;
  237. mutex_init(&ts_nbus->lock);
  238. ret = ts_nbus_init_pdata(pdev, ts_nbus);
  239. if (ret < 0)
  240. return ret;
  241. pwm = devm_pwm_get(dev, NULL);
  242. if (IS_ERR(pwm)) {
  243. ret = PTR_ERR(pwm);
  244. if (ret != -EPROBE_DEFER)
  245. dev_err(dev, "unable to request PWM\n");
  246. return ret;
  247. }
  248. pwm_get_args(pwm, &pargs);
  249. if (!pargs.period) {
  250. dev_err(&pdev->dev, "invalid PWM period\n");
  251. return -EINVAL;
  252. }
  253. /*
  254. * FIXME: pwm_apply_args() should be removed when switching to
  255. * the atomic PWM API.
  256. */
  257. pwm_apply_args(pwm);
  258. ret = pwm_config(pwm, pargs.period, pargs.period);
  259. if (ret < 0)
  260. return ret;
  261. /*
  262. * we can now start the FPGA and populate the peripherals.
  263. */
  264. pwm_enable(pwm);
  265. ts_nbus->pwm = pwm;
  266. /*
  267. * let the child nodes retrieve this instance of the ts-nbus.
  268. */
  269. dev_set_drvdata(dev, ts_nbus);
  270. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  271. if (ret < 0)
  272. return ret;
  273. dev_info(dev, "initialized\n");
  274. return 0;
  275. }
  276. static int ts_nbus_remove(struct platform_device *pdev)
  277. {
  278. struct ts_nbus *ts_nbus = dev_get_drvdata(&pdev->dev);
  279. /* shutdown the FPGA */
  280. mutex_lock(&ts_nbus->lock);
  281. pwm_disable(ts_nbus->pwm);
  282. mutex_unlock(&ts_nbus->lock);
  283. return 0;
  284. }
  285. static const struct of_device_id ts_nbus_of_match[] = {
  286. { .compatible = "technologic,ts-nbus", },
  287. { },
  288. };
  289. MODULE_DEVICE_TABLE(of, ts_nbus_of_match);
  290. static struct platform_driver ts_nbus_driver = {
  291. .probe = ts_nbus_probe,
  292. .remove = ts_nbus_remove,
  293. .driver = {
  294. .name = "ts_nbus",
  295. .of_match_table = ts_nbus_of_match,
  296. },
  297. };
  298. module_platform_driver(ts_nbus_driver);
  299. MODULE_ALIAS("platform:ts_nbus");
  300. MODULE_AUTHOR("Sebastien Bourdelin <[email protected]>");
  301. MODULE_DESCRIPTION("Technologic Systems NBUS");
  302. MODULE_LICENSE("GPL v2");