es705-uart-common.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * es705-uart-common.c -- Audience eS705 UART interface
  3. *
  4. * Copyright 2013 Audience, Inc.
  5. *
  6. * Author: Matt Lupfer <mlupfer@cardinalpeak.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. //#define DEBUG
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/kthread.h>
  20. #include <linux/esxxx.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/tty.h>
  23. #include <linux/fs.h>
  24. #include <linux/gpio.h>
  25. #include "es705.h"
  26. #include "es705-uart-common.h"
  27. #define MAX_EAGAIN_RETRY 20
  28. #define EAGAIN_RETRY_DELAY 1000
  29. int es705_uart_read(struct es705_priv *es705, void *buf, int len)
  30. {
  31. int rc;
  32. mm_segment_t oldfs;
  33. loff_t pos = 0;
  34. int retry = MAX_EAGAIN_RETRY;
  35. dev_dbg(es705->dev, "%s(): size %d\n", __func__, len);
  36. /*
  37. * we may call from user context via char dev, so allow
  38. * read buffer in kernel address space
  39. */
  40. oldfs = get_fs();
  41. set_fs(KERNEL_DS);
  42. do {
  43. rc = vfs_read(es705->uart_dev.file,
  44. (char __user *)buf, len, &pos);
  45. if (rc == -EAGAIN) {
  46. usleep_range(EAGAIN_RETRY_DELAY, EAGAIN_RETRY_DELAY);
  47. retry--;
  48. }
  49. } while (rc == -EAGAIN && retry);
  50. /* restore old fs context */
  51. set_fs(oldfs);
  52. if (rc >= 0)
  53. dev_dbg(es705->dev, "%s(): read bytes %d\n", __func__, rc);
  54. else
  55. dev_dbg(es705->dev, "%s(): UART read error %d\n", __func__, rc);
  56. return rc;
  57. }
  58. int es705_uart_write(struct es705_priv *es705, const void *buf, int len)
  59. {
  60. int rc = 0;
  61. int count_remain = len;
  62. int bytes_wr = 0;
  63. mm_segment_t oldfs;
  64. loff_t pos = 0;
  65. dev_dbg(es705->dev, "%s(): size %d\n", __func__, len);
  66. /*
  67. * we may call from user context via char dev, so allow
  68. * read buffer in kernel address space
  69. */
  70. oldfs = get_fs();
  71. set_fs(KERNEL_DS);
  72. while (count_remain > 0) {
  73. /* block until tx buffer space is available */
  74. while (tty_write_room(es705->uart_dev.tty) < UART_TTY_WRITE_SZ)
  75. usleep_range(2000, 2000);
  76. rc = vfs_write(es705->uart_dev.file, buf + bytes_wr,
  77. min(UART_TTY_WRITE_SZ, count_remain), &pos);
  78. if (rc < 0) {
  79. bytes_wr = rc;
  80. goto err_out;
  81. }
  82. bytes_wr += rc;
  83. count_remain -= rc;
  84. }
  85. err_out:
  86. /* restore old fs context */
  87. set_fs(oldfs);
  88. dev_dbg(es705->dev, "%s(): write bytes %d\n", __func__, bytes_wr);
  89. return bytes_wr;
  90. }
  91. int es705_uart_write_then_read(struct es705_priv *es705, const void *buf,
  92. int len, u32 *rspn, int match)
  93. {
  94. int rc;
  95. u32 response = 0;
  96. rc = es705_uart_write(es705, buf, len);
  97. if (rc > 0) {
  98. rc = es705_uart_read(es705, &response, len);
  99. response = le32_to_cpu(response);
  100. if (rc < 0) {
  101. dev_err(es705->dev, "%s(): UART read fail\n",
  102. __func__);
  103. } else if (match && *rspn != response) {
  104. dev_err(es705->dev, "%s(): unexpected response 0x%08x\n",
  105. __func__, be32_to_cpu(response));
  106. rc = -EIO;
  107. } else {
  108. *rspn = response;
  109. }
  110. }
  111. return rc;
  112. }
  113. int es705_uart_cmd(struct es705_priv *es705, u32 cmd, int sr, u32 *resp)
  114. {
  115. int err;
  116. u32 rv;
  117. dev_dbg(es705->dev, "%s(): cmd=0x%08x sr=%i\n", __func__, cmd, sr);
  118. cmd = cpu_to_be32(cmd);
  119. err = es705_uart_write(es705, &cmd, sizeof(cmd));
  120. if (err < 0 || sr)
  121. goto es705_uart_cmd_exit;
  122. err = es705_uart_read(es705, &rv, sizeof(rv));
  123. if (err > 0) {
  124. *resp = be32_to_cpu(rv);
  125. dev_dbg(es705->dev, "%s(): response=0x%08x\n", __func__, *resp);
  126. } else if (err == 0) {
  127. dev_dbg(es705->dev, "%s(): No response\n", __func__);
  128. } else {
  129. dev_err(es705->dev, "%s(): UART read fail\n", __func__);
  130. }
  131. es705_uart_cmd_exit:
  132. return err;
  133. }
  134. int es705_configure_tty(struct tty_struct *tty, u32 bps, int stop)
  135. {
  136. int rc = 0;
  137. struct ktermios termios;
  138. termios = *tty->termios;
  139. dev_dbg(es705_priv.dev, "%s(): Requesting baud %u\n", __func__, bps);
  140. termios.c_cflag &= ~(CBAUD | CSIZE | PARENB); /* clear csize, baud */
  141. termios.c_cflag |= BOTHER; /* allow arbitrary baud */
  142. termios.c_cflag |= CS8;
  143. if (stop == 2)
  144. termios.c_cflag |= CSTOPB;
  145. /* set uart port to raw mode (see termios man page for flags) */
  146. termios.c_iflag &= ~(PARMRK /* disable mark parity errors */
  147. | ISTRIP /* disable clear high bit of input characters */
  148. | INLCR /* disable translate NL to CR */
  149. | IGNCR /* disable ignore CR */
  150. | ICRNL /* disable translate CR to NL */
  151. | IXON); /* disable enable XON/XOFF flow control */
  152. termios.c_iflag |= IGNBRK; /* enable ignore break */
  153. termios.c_oflag &= ~(OPOST);
  154. termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  155. /* set baud rate */
  156. termios.c_ospeed = bps;
  157. termios.c_ispeed = bps;
  158. rc = tty_set_termios(tty, &termios);
  159. dev_dbg(es705_priv.dev,
  160. "%s(): New baud %u\n", __func__, tty->termios->c_ospeed);
  161. return rc;
  162. }
  163. const int host_uart_baud_rate[] = {
  164. UART_TTY_BAUD_RATE_BOOTLOADER, /* Default 460.8KBps */
  165. UART_TTY_BAUD_RATE_BOOTLOADER, /* 460.8KBps */
  166. UART_TTY_BAUD_RATE_FW_DOWNLOAD, /* 1MBps */
  167. UART_TTY_BAUD_RATE_FIRMWARE, /* 3MBps */
  168. };
  169. void es705_set_tty_baud_rate(int index)
  170. {
  171. /* set baudrate to FW baud (common case) */
  172. es705_configure_tty(es705_priv.uart_dev.tty,
  173. host_uart_baud_rate[index], UART_TTY_STOP_BITS);
  174. }
  175. int es705_uart_open(struct es705_priv *es705)
  176. {
  177. long err = 0;
  178. struct file *fp = NULL;
  179. unsigned long timeout = jiffies + msecs_to_jiffies(250);
  180. int attempt = 0;
  181. dev_dbg(es705->dev, "%s(): start open tty\n",
  182. __func__);
  183. if (es705->uart_state == UART_OPEN)
  184. es705_uart_close(es705);
  185. /* SAMSUNG - uart_gpio control */
  186. if (es705->pdata->uart_gpio != -1)
  187. gpio_set_value(es705->pdata->uart_gpio, 1);
  188. /* try to probe tty node every 50 ms for 250 ms */
  189. do {
  190. if (attempt > 0)
  191. msleep(50);
  192. dev_dbg(es705->dev,
  193. "%s(): probing for tty on %s (attempt %d)\n",
  194. __func__, UART_TTY_DEVICE_NODE, ++attempt);
  195. fp = filp_open(UART_TTY_DEVICE_NODE,
  196. O_RDWR | O_NONBLOCK | O_NOCTTY,
  197. 0);
  198. err = PTR_ERR(fp);
  199. } while (time_before(jiffies, timeout) && err == -ENOENT);
  200. if (IS_ERR_OR_NULL(fp)) {
  201. dev_err(es705->dev, "%s(): UART device node open failed, err = %ld\n",
  202. __func__, err);
  203. return -ENODEV;
  204. }
  205. /* device node found */
  206. dev_dbg(es705->dev, "%s(): successfully opened tty\n",
  207. __func__);
  208. /* set uart_dev members */
  209. es705_priv.uart_dev.file = fp;
  210. es705_priv.uart_dev.tty =
  211. ((struct tty_file_private *)fp->private_data)->tty;
  212. es705->uart_state = UART_OPEN;
  213. return 0;
  214. }
  215. int es705_uart_close(struct es705_priv *es705)
  216. {
  217. dev_dbg(es705->dev, "%s(): start close tty\n",
  218. __func__);
  219. if (es705->uart_state == UART_OPEN)
  220. filp_close(es705->uart_dev.file, 0);
  221. es705->uart_state = UART_CLOSE;
  222. dev_dbg(es705->dev, "%s(): successfully closed tty\n",
  223. __func__);
  224. /* SAMSUNG - uart_gpio control */
  225. if (es705->pdata->uart_gpio != -1)
  226. gpio_set_value(es705->pdata->uart_gpio, 0);
  227. return 0;
  228. }
  229. int es705_uart_wait(struct es705_priv *es705)
  230. {
  231. /* wait on tty read queue until awoken or for 50ms */
  232. return wait_event_interruptible_timeout(
  233. es705->uart_dev.tty->read_wait,
  234. es705->uart_dev.tty->read_cnt,
  235. msecs_to_jiffies(50));
  236. }
  237. struct es_stream_device uart_streamdev = {
  238. .open = es705_uart_open,
  239. .read = es705_uart_read,
  240. .close = es705_uart_close,
  241. .wait = es705_uart_wait,
  242. .intf = ES705_UART_INTF,
  243. };
  244. MODULE_DESCRIPTION("ASoC ES705 driver");
  245. MODULE_AUTHOR("Greg Clemson <gclemson@audience.com>");
  246. MODULE_LICENSE("GPL v2");
  247. MODULE_ALIAS("platform:es705-codec");