ts-nbus.c 8.9 KB

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