ts-nbus.c 8.9 KB

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