max3107-aava.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * max3107.c - spi uart protocol driver for Maxim 3107
  3. * Based on max3100.c
  4. * by Christian Pellegrin <chripell@evolware.org>
  5. * and max3110.c
  6. * by Feng Tang <feng.tang@intel.com>
  7. *
  8. * Copyright (C) Aavamobile 2009
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. *
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/device.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/serial.h>
  33. #include <linux/spi/spi.h>
  34. #include <linux/freezer.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/gpio.h>
  37. #include <linux/sfi.h>
  38. #include <asm/mrst.h>
  39. #include "max3107.h"
  40. /* GPIO direction to input function */
  41. static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  42. {
  43. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  44. u16 buf[1]; /* Buffer for SPI transfer */
  45. if (offset >= MAX3107_GPIO_COUNT) {
  46. dev_err(&s->spi->dev, "Invalid GPIO\n");
  47. return -EINVAL;
  48. }
  49. /* Read current GPIO configuration register */
  50. buf[0] = MAX3107_GPIOCFG_REG;
  51. /* Perform SPI transfer */
  52. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
  53. dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
  54. return -EIO;
  55. }
  56. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  57. /* Set GPIO to input */
  58. buf[0] &= ~(0x0001 << offset);
  59. /* Write new GPIO configuration register value */
  60. buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
  61. /* Perform SPI transfer */
  62. if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
  63. dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
  64. return -EIO;
  65. }
  66. return 0;
  67. }
  68. /* GPIO direction to output function */
  69. static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
  70. int value)
  71. {
  72. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  73. u16 buf[2]; /* Buffer for SPI transfers */
  74. if (offset >= MAX3107_GPIO_COUNT) {
  75. dev_err(&s->spi->dev, "Invalid GPIO\n");
  76. return -EINVAL;
  77. }
  78. /* Read current GPIO configuration and data registers */
  79. buf[0] = MAX3107_GPIOCFG_REG;
  80. buf[1] = MAX3107_GPIODATA_REG;
  81. /* Perform SPI transfer */
  82. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
  83. dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
  84. return -EIO;
  85. }
  86. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  87. buf[1] &= MAX3107_SPI_RX_DATA_MASK;
  88. /* Set GPIO to output */
  89. buf[0] |= (0x0001 << offset);
  90. /* Set value */
  91. if (value)
  92. buf[1] |= (0x0001 << offset);
  93. else
  94. buf[1] &= ~(0x0001 << offset);
  95. /* Write new GPIO configuration and data register values */
  96. buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
  97. buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
  98. /* Perform SPI transfer */
  99. if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
  100. dev_err(&s->spi->dev,
  101. "SPI transfer for GPIO conf data w failed\n");
  102. return -EIO;
  103. }
  104. return 0;
  105. }
  106. /* GPIO value query function */
  107. static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
  108. {
  109. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  110. u16 buf[1]; /* Buffer for SPI transfer */
  111. if (offset >= MAX3107_GPIO_COUNT) {
  112. dev_err(&s->spi->dev, "Invalid GPIO\n");
  113. return -EINVAL;
  114. }
  115. /* Read current GPIO data register */
  116. buf[0] = MAX3107_GPIODATA_REG;
  117. /* Perform SPI transfer */
  118. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
  119. dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
  120. return -EIO;
  121. }
  122. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  123. /* Return value */
  124. return buf[0] & (0x0001 << offset);
  125. }
  126. /* GPIO value set function */
  127. static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  128. {
  129. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  130. u16 buf[2]; /* Buffer for SPI transfers */
  131. if (offset >= MAX3107_GPIO_COUNT) {
  132. dev_err(&s->spi->dev, "Invalid GPIO\n");
  133. return;
  134. }
  135. /* Read current GPIO configuration registers*/
  136. buf[0] = MAX3107_GPIODATA_REG;
  137. buf[1] = MAX3107_GPIOCFG_REG;
  138. /* Perform SPI transfer */
  139. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
  140. dev_err(&s->spi->dev,
  141. "SPI transfer for GPIO data and config read failed\n");
  142. return;
  143. }
  144. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  145. buf[1] &= MAX3107_SPI_RX_DATA_MASK;
  146. if (!(buf[1] & (0x0001 << offset))) {
  147. /* Configured as input, can't set value */
  148. dev_warn(&s->spi->dev,
  149. "Trying to set value for input GPIO\n");
  150. return;
  151. }
  152. /* Set value */
  153. if (value)
  154. buf[0] |= (0x0001 << offset);
  155. else
  156. buf[0] &= ~(0x0001 << offset);
  157. /* Write new GPIO data register value */
  158. buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
  159. /* Perform SPI transfer */
  160. if (max3107_rw(s, (u8 *)buf, NULL, 2))
  161. dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
  162. }
  163. /* GPIO chip data */
  164. static struct gpio_chip max3107_gpio_chip = {
  165. .owner = THIS_MODULE,
  166. .direction_input = max3107_gpio_direction_in,
  167. .direction_output = max3107_gpio_direction_out,
  168. .get = max3107_gpio_get,
  169. .set = max3107_gpio_set,
  170. .can_sleep = 1,
  171. .base = MAX3107_GPIO_BASE,
  172. .ngpio = MAX3107_GPIO_COUNT,
  173. };
  174. /**
  175. * max3107_aava_reset - reset on AAVA systems
  176. * @spi: The SPI device we are probing
  177. *
  178. * Reset the device ready for probing.
  179. */
  180. static int max3107_aava_reset(struct spi_device *spi)
  181. {
  182. /* Reset the chip */
  183. if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
  184. pr_err("Requesting RESET GPIO failed\n");
  185. return -EIO;
  186. }
  187. if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
  188. pr_err("Setting RESET GPIO to 0 failed\n");
  189. gpio_free(MAX3107_RESET_GPIO);
  190. return -EIO;
  191. }
  192. msleep(MAX3107_RESET_DELAY);
  193. if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
  194. pr_err("Setting RESET GPIO to 1 failed\n");
  195. gpio_free(MAX3107_RESET_GPIO);
  196. return -EIO;
  197. }
  198. gpio_free(MAX3107_RESET_GPIO);
  199. msleep(MAX3107_WAKEUP_DELAY);
  200. return 0;
  201. }
  202. static int max3107_aava_configure(struct max3107_port *s)
  203. {
  204. int retval;
  205. /* Initialize GPIO chip data */
  206. s->chip = max3107_gpio_chip;
  207. s->chip.label = s->spi->modalias;
  208. s->chip.dev = &s->spi->dev;
  209. /* Add GPIO chip */
  210. retval = gpiochip_add(&s->chip);
  211. if (retval) {
  212. dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
  213. return retval;
  214. }
  215. /* Temporary fix for EV2 boot problems, set modem reset to 0 */
  216. max3107_gpio_direction_out(&s->chip, 3, 0);
  217. return 0;
  218. }
  219. #if 0
  220. /* This will get enabled once we have the board stuff merged for this
  221. specific case */
  222. static const struct baud_table brg13_ext[] = {
  223. { 300, MAX3107_BRG13_B300 },
  224. { 600, MAX3107_BRG13_B600 },
  225. { 1200, MAX3107_BRG13_B1200 },
  226. { 2400, MAX3107_BRG13_B2400 },
  227. { 4800, MAX3107_BRG13_B4800 },
  228. { 9600, MAX3107_BRG13_B9600 },
  229. { 19200, MAX3107_BRG13_B19200 },
  230. { 57600, MAX3107_BRG13_B57600 },
  231. { 115200, MAX3107_BRG13_B115200 },
  232. { 230400, MAX3107_BRG13_B230400 },
  233. { 460800, MAX3107_BRG13_B460800 },
  234. { 921600, MAX3107_BRG13_B921600 },
  235. { 0, 0 }
  236. };
  237. static void max3107_aava_init(struct max3107_port *s)
  238. {
  239. /*override for AAVA SC specific*/
  240. if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
  241. if (get_koski_build_id() <= KOSKI_EV2)
  242. if (s->ext_clk) {
  243. s->brg_cfg = MAX3107_BRG13_B9600;
  244. s->baud_tbl = (struct baud_table *)brg13_ext;
  245. }
  246. }
  247. }
  248. #endif
  249. static int __devexit max3107_aava_remove(struct spi_device *spi)
  250. {
  251. struct max3107_port *s = dev_get_drvdata(&spi->dev);
  252. /* Remove GPIO chip */
  253. if (gpiochip_remove(&s->chip))
  254. dev_warn(&spi->dev, "Removing GPIO chip failed\n");
  255. /* Then do the default remove */
  256. return max3107_remove(spi);
  257. }
  258. /* Platform data */
  259. static struct max3107_plat aava_plat_data = {
  260. .loopback = 0,
  261. .ext_clk = 1,
  262. /* .init = max3107_aava_init, */
  263. .configure = max3107_aava_configure,
  264. .hw_suspend = max3107_hw_susp,
  265. .polled_mode = 0,
  266. .poll_time = 0,
  267. };
  268. static int __devinit max3107_probe_aava(struct spi_device *spi)
  269. {
  270. int err = max3107_aava_reset(spi);
  271. if (err < 0)
  272. return err;
  273. return max3107_probe(spi, &aava_plat_data);
  274. }
  275. /* Spi driver data */
  276. static struct spi_driver max3107_driver = {
  277. .driver = {
  278. .name = "aava-max3107",
  279. .bus = &spi_bus_type,
  280. .owner = THIS_MODULE,
  281. },
  282. .probe = max3107_probe_aava,
  283. .remove = __devexit_p(max3107_aava_remove),
  284. .suspend = max3107_suspend,
  285. .resume = max3107_resume,
  286. };
  287. /* Driver init function */
  288. static int __init max3107_init(void)
  289. {
  290. return spi_register_driver(&max3107_driver);
  291. }
  292. /* Driver exit function */
  293. static void __exit max3107_exit(void)
  294. {
  295. spi_unregister_driver(&max3107_driver);
  296. }
  297. module_init(max3107_init);
  298. module_exit(max3107_exit);
  299. MODULE_DESCRIPTION("MAX3107 driver");
  300. MODULE_AUTHOR("Aavamobile");
  301. MODULE_ALIAS("spi:aava-max3107");
  302. MODULE_LICENSE("GPL v2");