spi-rb4xx-cpld.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * SPI driver for the CPLD chip on the Mikrotik RB4xx boards
  3. *
  4. * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This file was based on the patches for Linux 2.6.27.39 published by
  7. * MikroTik for their RouterBoard 4xx series devices.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/bitops.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/gpio.h>
  22. #include <linux/slab.h>
  23. #include <asm/mach-ath79/rb4xx_cpld.h>
  24. #define DRV_NAME "spi-rb4xx-cpld"
  25. #define DRV_DESC "RB4xx CPLD driver"
  26. #define DRV_VERSION "0.1.0"
  27. #define CPLD_CMD_WRITE_NAND 0x08 /* send cmd, n x send data, send indle */
  28. #define CPLD_CMD_WRITE_CFG 0x09 /* send cmd, n x send cfg */
  29. #define CPLD_CMD_READ_NAND 0x0a /* send cmd, send idle, n x read data */
  30. #define CPLD_CMD_READ_FAST 0x0b /* send cmd, 4 x idle, n x read data */
  31. #define CPLD_CMD_LED5_ON 0x0c /* send cmd */
  32. #define CPLD_CMD_LED5_OFF 0x0d /* send cmd */
  33. struct rb4xx_cpld {
  34. struct spi_device *spi;
  35. struct mutex lock;
  36. struct gpio_chip chip;
  37. unsigned int config;
  38. };
  39. static struct rb4xx_cpld *rb4xx_cpld;
  40. static inline struct rb4xx_cpld *gpio_to_cpld(struct gpio_chip *chip)
  41. {
  42. return container_of(chip, struct rb4xx_cpld, chip);
  43. }
  44. static int rb4xx_cpld_write_cmd(struct rb4xx_cpld *cpld, unsigned char cmd)
  45. {
  46. struct spi_transfer t[1];
  47. struct spi_message m;
  48. unsigned char tx_buf[1];
  49. int err;
  50. spi_message_init(&m);
  51. memset(&t, 0, sizeof(t));
  52. t[0].tx_buf = tx_buf;
  53. t[0].len = sizeof(tx_buf);
  54. spi_message_add_tail(&t[0], &m);
  55. tx_buf[0] = cmd;
  56. err = spi_sync(cpld->spi, &m);
  57. return err;
  58. }
  59. static int rb4xx_cpld_write_cfg(struct rb4xx_cpld *cpld, unsigned char config)
  60. {
  61. struct spi_transfer t[1];
  62. struct spi_message m;
  63. unsigned char cmd[2];
  64. int err;
  65. spi_message_init(&m);
  66. memset(&t, 0, sizeof(t));
  67. t[0].tx_buf = cmd;
  68. t[0].len = sizeof(cmd);
  69. spi_message_add_tail(&t[0], &m);
  70. cmd[0] = CPLD_CMD_WRITE_CFG;
  71. cmd[1] = config;
  72. err = spi_sync(cpld->spi, &m);
  73. return err;
  74. }
  75. static int __rb4xx_cpld_change_cfg(struct rb4xx_cpld *cpld, unsigned mask,
  76. unsigned value)
  77. {
  78. unsigned int config;
  79. int err;
  80. config = cpld->config & ~mask;
  81. config |= value;
  82. if ((cpld->config ^ config) & 0xff) {
  83. err = rb4xx_cpld_write_cfg(cpld, config);
  84. if (err)
  85. return err;
  86. }
  87. if ((cpld->config ^ config) & CPLD_CFG_nLED5) {
  88. err = rb4xx_cpld_write_cmd(cpld, (value) ? CPLD_CMD_LED5_ON :
  89. CPLD_CMD_LED5_OFF);
  90. if (err)
  91. return err;
  92. }
  93. cpld->config = config;
  94. return 0;
  95. }
  96. int rb4xx_cpld_change_cfg(unsigned mask, unsigned value)
  97. {
  98. int ret;
  99. if (rb4xx_cpld == NULL)
  100. return -ENODEV;
  101. mutex_lock(&rb4xx_cpld->lock);
  102. ret = __rb4xx_cpld_change_cfg(rb4xx_cpld, mask, value);
  103. mutex_unlock(&rb4xx_cpld->lock);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(rb4xx_cpld_change_cfg);
  107. int rb4xx_cpld_read_from(unsigned addr, unsigned char *rx_buf,
  108. const unsigned char *verify_buf, unsigned count)
  109. {
  110. const unsigned char cmd[5] = {
  111. CPLD_CMD_READ_FAST,
  112. (addr >> 16) & 0xff,
  113. (addr >> 8) & 0xff,
  114. addr & 0xff,
  115. 0
  116. };
  117. struct spi_transfer t[2] = {
  118. {
  119. .tx_buf = &cmd,
  120. .len = 5,
  121. },
  122. {
  123. .tx_buf = verify_buf,
  124. .rx_buf = rx_buf,
  125. .len = count,
  126. .verify = (verify_buf != NULL),
  127. },
  128. };
  129. struct spi_message m;
  130. if (rb4xx_cpld == NULL)
  131. return -ENODEV;
  132. spi_message_init(&m);
  133. m.fast_read = 1;
  134. spi_message_add_tail(&t[0], &m);
  135. spi_message_add_tail(&t[1], &m);
  136. return spi_sync(rb4xx_cpld->spi, &m);
  137. }
  138. EXPORT_SYMBOL_GPL(rb4xx_cpld_read_from);
  139. #if 0
  140. int rb4xx_cpld_read(unsigned char *buf, unsigned char *verify_buf,
  141. unsigned count)
  142. {
  143. struct spi_transfer t[2];
  144. struct spi_message m;
  145. unsigned char cmd[2];
  146. if (rb4xx_cpld == NULL)
  147. return -ENODEV;
  148. spi_message_init(&m);
  149. memset(&t, 0, sizeof(t));
  150. /* send command */
  151. t[0].tx_buf = cmd;
  152. t[0].len = sizeof(cmd);
  153. spi_message_add_tail(&t[0], &m);
  154. cmd[0] = CPLD_CMD_READ_NAND;
  155. cmd[1] = 0;
  156. /* read data */
  157. t[1].rx_buf = buf;
  158. t[1].len = count;
  159. spi_message_add_tail(&t[1], &m);
  160. return spi_sync(rb4xx_cpld->spi, &m);
  161. }
  162. #else
  163. int rb4xx_cpld_read(unsigned char *rx_buf, const unsigned char *verify_buf,
  164. unsigned count)
  165. {
  166. static const unsigned char cmd[2] = { CPLD_CMD_READ_NAND, 0 };
  167. struct spi_transfer t[2] = {
  168. {
  169. .tx_buf = &cmd,
  170. .len = 2,
  171. }, {
  172. .tx_buf = verify_buf,
  173. .rx_buf = rx_buf,
  174. .len = count,
  175. .verify = (verify_buf != NULL),
  176. },
  177. };
  178. struct spi_message m;
  179. if (rb4xx_cpld == NULL)
  180. return -ENODEV;
  181. spi_message_init(&m);
  182. spi_message_add_tail(&t[0], &m);
  183. spi_message_add_tail(&t[1], &m);
  184. return spi_sync(rb4xx_cpld->spi, &m);
  185. }
  186. #endif
  187. EXPORT_SYMBOL_GPL(rb4xx_cpld_read);
  188. int rb4xx_cpld_write(const unsigned char *buf, unsigned count)
  189. {
  190. #if 0
  191. struct spi_transfer t[3];
  192. struct spi_message m;
  193. unsigned char cmd[1];
  194. if (rb4xx_cpld == NULL)
  195. return -ENODEV;
  196. memset(&t, 0, sizeof(t));
  197. spi_message_init(&m);
  198. /* send command */
  199. t[0].tx_buf = cmd;
  200. t[0].len = sizeof(cmd);
  201. spi_message_add_tail(&t[0], &m);
  202. cmd[0] = CPLD_CMD_WRITE_NAND;
  203. /* write data */
  204. t[1].tx_buf = buf;
  205. t[1].len = count;
  206. spi_message_add_tail(&t[1], &m);
  207. /* send idle */
  208. t[2].len = 1;
  209. spi_message_add_tail(&t[2], &m);
  210. return spi_sync(rb4xx_cpld->spi, &m);
  211. #else
  212. static const unsigned char cmd = CPLD_CMD_WRITE_NAND;
  213. struct spi_transfer t[3] = {
  214. {
  215. .tx_buf = &cmd,
  216. .len = 1,
  217. }, {
  218. .tx_buf = buf,
  219. .len = count,
  220. .fast_write = 1,
  221. }, {
  222. .len = 1,
  223. .fast_write = 1,
  224. },
  225. };
  226. struct spi_message m;
  227. if (rb4xx_cpld == NULL)
  228. return -ENODEV;
  229. spi_message_init(&m);
  230. spi_message_add_tail(&t[0], &m);
  231. spi_message_add_tail(&t[1], &m);
  232. spi_message_add_tail(&t[2], &m);
  233. return spi_sync(rb4xx_cpld->spi, &m);
  234. #endif
  235. }
  236. EXPORT_SYMBOL_GPL(rb4xx_cpld_write);
  237. static int rb4xx_cpld_gpio_get(struct gpio_chip *chip, unsigned offset)
  238. {
  239. struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
  240. int ret;
  241. mutex_lock(&cpld->lock);
  242. ret = (cpld->config >> offset) & 1;
  243. mutex_unlock(&cpld->lock);
  244. return ret;
  245. }
  246. static void rb4xx_cpld_gpio_set(struct gpio_chip *chip, unsigned offset,
  247. int value)
  248. {
  249. struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
  250. mutex_lock(&cpld->lock);
  251. __rb4xx_cpld_change_cfg(cpld, (1 << offset), !!value << offset);
  252. mutex_unlock(&cpld->lock);
  253. }
  254. static int rb4xx_cpld_gpio_direction_input(struct gpio_chip *chip,
  255. unsigned offset)
  256. {
  257. return -EOPNOTSUPP;
  258. }
  259. static int rb4xx_cpld_gpio_direction_output(struct gpio_chip *chip,
  260. unsigned offset,
  261. int value)
  262. {
  263. struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
  264. int ret;
  265. mutex_lock(&cpld->lock);
  266. ret = __rb4xx_cpld_change_cfg(cpld, (1 << offset), !!value << offset);
  267. mutex_unlock(&cpld->lock);
  268. return ret;
  269. }
  270. static int rb4xx_cpld_gpio_init(struct rb4xx_cpld *cpld, unsigned int base)
  271. {
  272. int err;
  273. /* init config */
  274. cpld->config = CPLD_CFG_nLED1 | CPLD_CFG_nLED2 | CPLD_CFG_nLED3 |
  275. CPLD_CFG_nLED4 | CPLD_CFG_nCE;
  276. rb4xx_cpld_write_cfg(cpld, cpld->config);
  277. /* setup GPIO chip */
  278. cpld->chip.label = DRV_NAME;
  279. cpld->chip.get = rb4xx_cpld_gpio_get;
  280. cpld->chip.set = rb4xx_cpld_gpio_set;
  281. cpld->chip.direction_input = rb4xx_cpld_gpio_direction_input;
  282. cpld->chip.direction_output = rb4xx_cpld_gpio_direction_output;
  283. cpld->chip.base = base;
  284. cpld->chip.ngpio = CPLD_NUM_GPIOS;
  285. cpld->chip.can_sleep = 1;
  286. cpld->chip.dev = &cpld->spi->dev;
  287. cpld->chip.owner = THIS_MODULE;
  288. err = gpiochip_add(&cpld->chip);
  289. if (err)
  290. dev_err(&cpld->spi->dev, "adding GPIO chip failed, err=%d\n",
  291. err);
  292. return err;
  293. }
  294. static int rb4xx_cpld_probe(struct spi_device *spi)
  295. {
  296. struct rb4xx_cpld *cpld;
  297. struct rb4xx_cpld_platform_data *pdata;
  298. int err;
  299. pdata = spi->dev.platform_data;
  300. if (!pdata) {
  301. dev_dbg(&spi->dev, "no platform data\n");
  302. return -EINVAL;
  303. }
  304. cpld = kzalloc(sizeof(*cpld), GFP_KERNEL);
  305. if (!cpld) {
  306. dev_err(&spi->dev, "no memory for private data\n");
  307. return -ENOMEM;
  308. }
  309. mutex_init(&cpld->lock);
  310. cpld->spi = spi_dev_get(spi);
  311. dev_set_drvdata(&spi->dev, cpld);
  312. spi->mode = SPI_MODE_0;
  313. spi->bits_per_word = 8;
  314. err = spi_setup(spi);
  315. if (err) {
  316. dev_err(&spi->dev, "spi_setup failed, err=%d\n", err);
  317. goto err_drvdata;
  318. }
  319. err = rb4xx_cpld_gpio_init(cpld, pdata->gpio_base);
  320. if (err)
  321. goto err_drvdata;
  322. rb4xx_cpld = cpld;
  323. return 0;
  324. err_drvdata:
  325. dev_set_drvdata(&spi->dev, NULL);
  326. kfree(cpld);
  327. return err;
  328. }
  329. static int rb4xx_cpld_remove(struct spi_device *spi)
  330. {
  331. struct rb4xx_cpld *cpld;
  332. rb4xx_cpld = NULL;
  333. cpld = dev_get_drvdata(&spi->dev);
  334. dev_set_drvdata(&spi->dev, NULL);
  335. kfree(cpld);
  336. return 0;
  337. }
  338. static struct spi_driver rb4xx_cpld_driver = {
  339. .driver = {
  340. .name = DRV_NAME,
  341. .bus = &spi_bus_type,
  342. .owner = THIS_MODULE,
  343. },
  344. .probe = rb4xx_cpld_probe,
  345. .remove = rb4xx_cpld_remove,
  346. };
  347. static int __init rb4xx_cpld_init(void)
  348. {
  349. return spi_register_driver(&rb4xx_cpld_driver);
  350. }
  351. module_init(rb4xx_cpld_init);
  352. static void __exit rb4xx_cpld_exit(void)
  353. {
  354. spi_unregister_driver(&rb4xx_cpld_driver);
  355. }
  356. module_exit(rb4xx_cpld_exit);
  357. MODULE_DESCRIPTION(DRV_DESC);
  358. MODULE_VERSION(DRV_VERSION);
  359. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  360. MODULE_LICENSE("GPL v2");