Advertisement
Guest User

Untitled

a guest
May 18th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1.  
  2. #include <linux/bitops.h>
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <linux/device.h>
  6. #include <linux/gpio/driver.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/regmap.h>
  11. #include <linux/serial_core.h>
  12. #include <linux/serial.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/uaccess.h>
  17.  
  18. #define test_NAME "test_spi_uart"
  19. #define test_UART_NRMAX 3
  20.  
  21. struct test_devtype {
  22. char name[9];
  23. int nr;
  24. };
  25.  
  26. struct test_one {
  27. struct uart_port port;
  28. struct work_struct tx_work;
  29. struct work_struct md_work;
  30. struct work_struct rs_work;
  31.  
  32. u8 wr_header;
  33. u8 rd_header;
  34. };
  35. #define to_test_port(_port) \
  36. container_of(_port, struct test_one, port)
  37.  
  38. struct test_port {
  39. struct test_devtype *devtype;
  40. struct clk *clk;
  41. struct test_one p[0];
  42. struct regmap *regmap;
  43. };
  44.  
  45. static struct uart_driver test_uart = {
  46. .owner = THIS_MODULE,
  47. .dev_name = "ttyTH",
  48. .nr = test_UART_NRMAX,
  49. };
  50.  
  51. static DECLARE_BITMAP(test_lines, test_UART_NRMAX);
  52.  
  53. static void test_start_tx(struct uart_port *port)
  54. {
  55. struct test_one *one = to_test_port(port);
  56.  
  57. dev_info(port->dev, "calling: %s\n", __func__);
  58. }
  59.  
  60. static void test_tx_proc(struct work_struct *ws)
  61. {
  62. struct test_one *one = container_of(ws, struct test_one, tx_work);
  63. }
  64.  
  65. static unsigned int test_tx_empty(struct uart_port *port)
  66. {
  67. return 0;
  68. }
  69.  
  70. static unsigned int test_get_mctrl(struct uart_port *port)
  71. {
  72. /* DCD and DSR are not wired and CTS/RTS is handled automatically
  73. * so just indicate DSR and CAR asserted
  74. */
  75. dev_info(port->dev, "calling: %s\n", __func__);
  76. return TIOCM_DSR | TIOCM_CAR;
  77. }
  78.  
  79. static void test_md_proc(struct work_struct *ws)
  80. {
  81. struct test_one *one = container_of(ws, struct test_one, md_work);
  82. }
  83.  
  84. static void test_set_mctrl(struct uart_port *port, unsigned int mctrl)
  85. {
  86. struct test_one *one = to_test_port(port);
  87. dev_info(port->dev, "calling: %s\n", __func__);
  88. }
  89.  
  90. static void test_break_ctl(struct uart_port *port, int break_state)
  91. {
  92. dev_info(port->dev, "calling: %s\n", __func__);
  93. }
  94.  
  95. static void test_set_termios(struct uart_port *port,
  96. struct ktermios *termios,
  97. struct ktermios *old)
  98. {
  99. unsigned int lcr = 0, flow = 0;
  100. int baud;
  101. dev_info(port->dev, "calling: %s\n", __func__);
  102. /* Mask termios capabilities we don't support */
  103. termios->c_cflag &= ~CMSPAR;
  104. }
  105.  
  106. static void test_rs_proc(struct work_struct *ws)
  107. {
  108. struct test_one *one = container_of(ws, struct test_one, rs_work);
  109. unsigned int delay, mode1 = 0, mode2 = 0;
  110. }
  111.  
  112. static int test_rs485_config(struct uart_port *port,
  113. struct serial_rs485 *rs485)
  114. {
  115. return 0;
  116. }
  117.  
  118. static int test_startup(struct uart_port *port)
  119. {
  120. struct test_port *s = dev_get_drvdata(port->dev);
  121. unsigned int val;
  122. dev_info(port->dev, "calling: %s\n", __func__);
  123.  
  124. return 0;
  125. }
  126.  
  127. static void test_shutdown(struct uart_port *port)
  128. {
  129. struct test_port *s = dev_get_drvdata(port->dev);
  130. dev_info(port->dev, "calling: %s\n", __func__);
  131. }
  132.  
  133. static const char *test_type(struct uart_port *port)
  134. {
  135. struct test_port *s = dev_get_drvdata(port->dev);
  136.  
  137. return (port->type == PORT_test) ? s->devtype->name : NULL;
  138. }
  139.  
  140. static int test_request_port(struct uart_port *port)
  141. {
  142. /* Do nothing */
  143. return 0;
  144. }
  145.  
  146. static void test_config_port(struct uart_port *port, int flags)
  147. {
  148. if (flags & UART_CONFIG_TYPE)
  149. port->type = PORT_test;
  150. dev_info(port->dev, "calling: %s\n", __func__);
  151. }
  152.  
  153. static int test_verify_port(struct uart_port *port, struct serial_struct *s)
  154. {
  155. dev_info(port->dev, "calling: %s\n", __func__);
  156. if ((s->type != PORT_UNKNOWN) && (s->type != PORT_test))
  157. return -EINVAL;
  158. if (s->irq != port->irq)
  159. return -EINVAL;
  160.  
  161. return 0;
  162. }
  163.  
  164. static void test_null_void(struct uart_port *port)
  165. {
  166. /* Do nothing */
  167. }
  168.  
  169. static const struct test_devtype th_devtype = {
  170. .name = "test",
  171. .nr = 16,
  172. };
  173.  
  174. static const struct uart_ops test_ops = {
  175. .tx_empty = test_tx_empty,
  176. .set_mctrl = test_set_mctrl,
  177. .get_mctrl = test_get_mctrl,
  178. .stop_tx = test_null_void,
  179. .start_tx = test_start_tx,
  180. .stop_rx = test_null_void,
  181. .break_ctl = test_break_ctl,
  182. .startup = test_startup,
  183. .shutdown = test_shutdown,
  184. .set_termios = test_set_termios,
  185. .type = test_type,
  186. .request_port = test_request_port,
  187. .release_port = test_null_void,
  188. .config_port = test_config_port,
  189. .verify_port = test_verify_port,
  190. };
  191.  
  192. static int test_probe(struct device *dev, struct test_devtype *devtype,
  193. struct regmap *regmap, int irq)
  194. {
  195. int i, ret, fmin, fmax, freq, uartclk = 150000;
  196. struct clk *clk_osc, *clk_xtal;
  197. struct test_port *s;
  198. bool xtal = false;
  199.  
  200. if (IS_ERR(regmap))
  201. return PTR_ERR(regmap);
  202.  
  203. /* Alloc port structure */
  204. s = devm_kzalloc(dev, struct_size(s, p, 3), GFP_KERNEL);
  205. if (!s) {
  206. dev_err(dev, "Error allocating port structure\n");
  207. return -ENOMEM;
  208. }
  209.  
  210.  
  211. s->devtype = devtype;
  212. dev_set_drvdata(dev, s);
  213.  
  214. for (i = 0; i < 3; i++) {
  215. unsigned int line;
  216.  
  217. line = find_first_zero_bit(test_lines, test_UART_NRMAX);
  218. if (line == test_UART_NRMAX) {
  219. ret = -ERANGE;
  220. goto out_uart;
  221. }
  222.  
  223. /* Initialize port data */
  224. s->p[i].port.line = line;
  225. s->p[i].port.dev = dev;
  226. s->p[i].port.type = PORT_test;
  227. s->p[i].port.fifosize = 300;
  228. s->p[i].port.flags = UPF_SKIP_TEST | UPF_AUTO_IRQ;
  229. s->p[i].port.iotype = UPIO_PORT;
  230. s->p[i].port.iobase = i + 1;
  231. s->p[i].port.uartclk = uartclk;
  232. s->p[i].port.ops = &test_ops;
  233. /* Initialize queue for start TX */
  234. //INIT_WORK(&s->p[i].tx_work, test_tx_proc);
  235. /* Initialize queue for changing LOOPBACK mode */
  236. //INIT_WORK(&s->p[i].md_work, test_md_proc);
  237. /* Initialize queue for changing RS485 mode */
  238. //INIT_WORK(&s->p[i].rs_work, test_rs_proc);
  239. /* Initialize SPI-transfer buffers */
  240. //s->p[i].wr_header = (s->p[i].port.iobase + test_THR_REG) |
  241. // test_WRITE_BIT;
  242. //s->p[i].rd_header = (s->p[i].port.iobase + test_RHR_REG);
  243.  
  244. /* Register port */
  245. ret = uart_add_one_port(&test_uart, &s->p[i].port);
  246. if (ret < 0) {
  247. s->p[i].port.dev = NULL;
  248. dev_err(dev, "failed to add uart port:%d\n", ret);
  249. goto out_uart;
  250. } else {
  251. }
  252. set_bit(line, test_lines);
  253. }
  254.  
  255. out_uart:
  256. for (i = 0; i < 3; i++) {
  257. if (s->p[i].port.dev) {
  258. uart_remove_one_port(&test_uart, &s->p[i].port);
  259. clear_bit(s->p[i].port.line, test_lines);
  260. }
  261. }
  262.  
  263. out_clk:
  264.  
  265. return ret;
  266. }
  267.  
  268. static int test_remove(struct device *dev)
  269. {
  270. struct test_port *s = dev_get_drvdata(dev);
  271. int i;
  272.  
  273. for (i = 0; i < 3; i++) {
  274. // cancel_work_sync(&s->p[i].tx_work);
  275. // cancel_work_sync(&s->p[i].md_work);
  276. // cancel_work_sync(&s->p[i].rs_work);
  277. uart_remove_one_port(&test_uart, &s->p[i].port);
  278. clear_bit(s->p[i].port.line, test_lines);
  279. // s->devtype->power(&s->p[i].port, 0);
  280. }
  281.  
  282. //clk_disable_unprepare(s->clk);
  283.  
  284. return 0;
  285. }
  286.  
  287. static const struct of_device_id __maybe_unused test_dt_ids[] = {
  288. { .compatible = "th,spi_uart", .data = &th_devtype },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(of, test_dt_ids);
  292.  
  293. static int test_spi_probe(struct spi_device *spi)
  294. {
  295. struct test_devtype *devtype;
  296. int ret;
  297.  
  298. /* Setup SPI bus */
  299. spi->bits_per_word = 8;
  300. spi->mode = spi->mode ? : SPI_MODE_0;
  301. spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
  302. ret = spi_setup(spi);
  303. if (ret)
  304. return ret;
  305.  
  306. if (spi->dev.of_node) {
  307. const struct of_device_id *of_id =
  308. of_match_device(test_dt_ids, &spi->dev);
  309. if (!of_id)
  310. return -ENODEV;
  311.  
  312. devtype = (struct test_devtype *)of_id->data;
  313. }
  314.  
  315. return test_probe(&spi->dev, devtype, NULL, spi->irq);
  316. }
  317.  
  318. static int test_spi_remove(struct spi_device *spi)
  319. {
  320. return test_remove(&spi->dev);
  321. }
  322.  
  323. static const struct spi_device_id test_id_table[] = {
  324. { "th_spi_uart" },
  325. { }
  326. };
  327. MODULE_DEVICE_TABLE(spi, test_id_table);
  328.  
  329. static struct spi_driver test_spi_driver = {
  330. .driver = {
  331. .name = test_NAME,
  332. .of_match_table = of_match_ptr(test_dt_ids),
  333. },
  334. .probe = test_spi_probe,
  335. .remove = test_spi_remove,
  336. .id_table = test_id_table,
  337. };
  338.  
  339. static int __init test_uart_init(void)
  340. {
  341. int ret;
  342.  
  343. bitmap_zero(test_lines, test_UART_NRMAX);
  344.  
  345. ret = uart_register_driver(&test_uart);
  346. if (ret) {
  347. pr_err("Failed to init driver:%d\n", ret);
  348. return ret;
  349. }
  350.  
  351. ret = spi_register_driver(&test_spi_driver);
  352.  
  353. return ret;
  354. }
  355. module_init(test_uart_init);
  356.  
  357. static void __exit test_uart_exit(void)
  358. {
  359. spi_unregister_driver(&test_spi_driver);
  360. uart_unregister_driver(&test_uart);
  361. }
  362. module_exit(test_uart_exit);
  363.  
  364. MODULE_LICENSE("GPL");
  365. MODULE_AUTHOR("Marek Belisko <marek.belisko@gmail.com>");
  366. MODULE_DESCRIPTION("Test serial driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement