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 <linux/module.h>
  39. #include <asm/mrst.h>
  40. #include "max3107.h"
  41. /* GPIO direction to input function */
  42. static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  43. {
  44. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  45. u16 buf[1]; /* Buffer for SPI transfer */
  46. if (offset >= MAX3107_GPIO_COUNT) {
  47. dev_err(&s->spi->dev, "Invalid GPIO\n");
  48. return -EINVAL;
  49. }
  50. /* Read current GPIO configuration register */
  51. buf[0] = MAX3107_GPIOCFG_REG;
  52. /* Perform SPI transfer */
  53. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
  54. dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
  55. return -EIO;
  56. }
  57. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  58. /* Set GPIO to input */
  59. buf[0] &= ~(0x0001 << offset);
  60. /* Write new GPIO configuration register value */
  61. buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
  62. /* Perform SPI transfer */
  63. if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
  64. dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
  65. return -EIO;
  66. }
  67. return 0;
  68. }
  69. /* GPIO direction to output function */
  70. static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
  71. int value)
  72. {
  73. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  74. u16 buf[2]; /* Buffer for SPI transfers */
  75. if (offset >= MAX3107_GPIO_COUNT) {
  76. dev_err(&s->spi->dev, "Invalid GPIO\n");
  77. return -EINVAL;
  78. }
  79. /* Read current GPIO configuration and data registers */
  80. buf[0] = MAX3107_GPIOCFG_REG;
  81. buf[1] = MAX3107_GPIODATA_REG;
  82. /* Perform SPI transfer */
  83. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
  84. dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
  85. return -EIO;
  86. }
  87. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  88. buf[1] &= MAX3107_SPI_RX_DATA_MASK;
  89. /* Set GPIO to output */
  90. buf[0] |= (0x0001 << offset);
  91. /* Set value */
  92. if (value)
  93. buf[1] |= (0x0001 << offset);
  94. else
  95. buf[1] &= ~(0x0001 << offset);
  96. /* Write new GPIO configuration and data register values */
  97. buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
  98. buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
  99. /* Perform SPI transfer */
  100. if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
  101. dev_err(&s->spi->dev,
  102. "SPI transfer for GPIO conf data w failed\n");
  103. return -EIO;
  104. }
  105. return 0;
  106. }
  107. /* GPIO value query function */
  108. static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
  109. {
  110. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  111. u16 buf[1]; /* Buffer for SPI transfer */
  112. if (offset >= MAX3107_GPIO_COUNT) {
  113. dev_err(&s->spi->dev, "Invalid GPIO\n");
  114. return -EINVAL;
  115. }
  116. /* Read current GPIO data register */
  117. buf[0] = MAX3107_GPIODATA_REG;
  118. /* Perform SPI transfer */
  119. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
  120. dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
  121. return -EIO;
  122. }
  123. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  124. /* Return value */
  125. return buf[0] & (0x0001 << offset);
  126. }
  127. /* GPIO value set function */
  128. static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  129. {
  130. struct max3107_port *s = container_of(chip, struct max3107_port, chip);
  131. u16 buf[2]; /* Buffer for SPI transfers */
  132. if (offset >= MAX3107_GPIO_COUNT) {
  133. dev_err(&s->spi->dev, "Invalid GPIO\n");
  134. return;
  135. }
  136. /* Read current GPIO configuration registers*/
  137. buf[0] = MAX3107_GPIODATA_REG;
  138. buf[1] = MAX3107_GPIOCFG_REG;
  139. /* Perform SPI transfer */
  140. if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
  141. dev_err(&s->spi->dev,
  142. "SPI transfer for GPIO data and config read failed\n");
  143. return;
  144. }
  145. buf[0] &= MAX3107_SPI_RX_DATA_MASK;
  146. buf[1] &= MAX3107_SPI_RX_DATA_MASK;
  147. if (!(buf[1] & (0x0001 << offset))) {
  148. /* Configured as input, can't set value */
  149. dev_warn(&s->spi->dev,
  150. "Trying to set value for input GPIO\n");
  151. return;
  152. }
  153. /* Set value */
  154. if (value)
  155. buf[0] |= (0x0001 << offset);
  156. else
  157. buf[0] &= ~(0x0001 << offset);
  158. /* Write new GPIO data register value */
  159. buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
  160. /* Perform SPI transfer */
  161. if (max3107_rw(s, (u8 *)buf, NULL, 2))
  162. dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
  163. }
  164. /* GPIO chip data */
  165. static struct gpio_chip max3107_gpio_chip = {
  166. .owner = THIS_MODULE,
  167. .direction_input = max3107_gpio_direction_in,
  168. .direction_output = max3107_gpio_direction_out,
  169. .get = max3107_gpio_get,
  170. .set = max3107_gpio_set,
  171. .can_sleep = 1,
  172. .base = MAX3107_GPIO_BASE,
  173. .ngpio = MAX3107_GPIO_COUNT,
  174. };
  175. /**
  176. * max3107_aava_reset - reset on AAVA systems
  177. * @spi: The SPI device we are probing
  178. *
  179. * Reset the device ready for probing.
  180. */
  181. static int max3107_aava_reset(struct spi_device *spi)
  182. {
  183. /* Reset the chip */
  184. if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
  185. pr_err("Requesting RESET GPIO failed\n");
  186. return -EIO;
  187. }
  188. if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
  189. pr_err("Setting RESET GPIO to 0 failed\n");
  190. gpio_free(MAX3107_RESET_GPIO);
  191. return -EIO;
  192. }
  193. msleep(MAX3107_RESET_DELAY);
  194. if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
  195. pr_err("Setting RESET GPIO to 1 failed\n");
  196. gpio_free(MAX3107_RESET_GPIO);
  197. return -EIO;
  198. }
  199. gpio_free(MAX3107_RESET_GPIO);
  200. msleep(MAX3107_WAKEUP_DELAY);
  201. return 0;
  202. }
  203. static int max3107_aava_configure(struct max3107_port *s)
  204. {
  205. int retval;
  206. /* Initialize GPIO chip data */
  207. s->chip = max3107_gpio_chip;
  208. s->chip.label = s->spi->modalias;
  209. s->chip.dev = &s->spi->dev;
  210. /* Add GPIO chip */
  211. retval = gpiochip_add(&s->chip);
  212. if (retval) {
  213. dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
  214. return retval;
  215. }
  216. /* Temporary fix for EV2 boot problems, set modem reset to 0 */
  217. max3107_gpio_direction_out(&s->chip, 3, 0);
  218. return 0;
  219. }
  220. #if 0
  221. /* This will get enabled once we have the board stuff merged for this
  222. specific case */
  223. static const struct baud_table brg13_ext[] = {
  224. { 300, MAX3107_BRG13_B300 },
  225. { 600, MAX3107_BRG13_B600 },
  226. { 1200, MAX3107_BRG13_B1200 },
  227. { 2400, MAX3107_BRG13_B2400 },
  228. { 4800, MAX3107_BRG13_B4800 },
  229. { 9600, MAX3107_BRG13_B9600 },
  230. { 19200, MAX3107_BRG13_B19200 },
  231. { 57600, MAX3107_BRG13_B57600 },
  232. { 115200, MAX3107_BRG13_B115200 },
  233. { 230400, MAX3107_BRG13_B230400 },
  234. { 460800, MAX3107_BRG13_B460800 },
  235. { 921600, MAX3107_BRG13_B921600 },
  236. { 0, 0 }
  237. };
  238. static void max3107_aava_init(struct max3107_port *s)
  239. {
  240. /*override for AAVA SC specific*/
  241. if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
  242. if (get_koski_build_id() <= KOSKI_EV2)
  243. if (s->ext_clk) {
  244. s->brg_cfg = MAX3107_BRG13_B9600;
  245. s->baud_tbl = (struct baud_table *)brg13_ext;
  246. }
  247. }
  248. }
  249. #endif
  250. static int __devexit max3107_aava_remove(struct spi_device *spi)
  251. {
  252. struct max3107_port *s = dev_get_drvdata(&spi->dev);
  253. /* Remove GPIO chip */
  254. if (gpiochip_remove(&s->chip))
  255. dev_warn(&spi->dev, "Removing GPIO chip failed\n");
  256. /* Then do the default remove */
  257. return max3107_remove(spi);
  258. }
  259. /* Platform data */
  260. static struct max3107_plat aava_plat_data = {
  261. .loopback = 0,
  262. .ext_clk = 1,
  263. /* .init = max3107_aava_init, */
  264. .configure = max3107_aava_configure,
  265. .hw_suspend = max3107_hw_susp,
  266. .polled_mode = 0,
  267. .poll_time = 0,
  268. };
  269. static int __devinit max3107_probe_aava(struct spi_device *spi)
  270. {
  271. int err = max3107_aava_reset(spi);
  272. if (err < 0)
  273. return err;
  274. return max3107_probe(spi, &aava_plat_data);
  275. }
  276. /* Spi driver data */
  277. static struct spi_driver max3107_driver = {
  278. .driver = {
  279. .name = "aava-max3107",
  280. .bus = &spi_bus_type,
  281. .owner = THIS_MODULE,
  282. },
  283. .probe = max3107_probe_aava,
  284. .remove = __devexit_p(max3107_aava_remove),
  285. .suspend = max3107_suspend,
  286. .resume = max3107_resume,
  287. };
  288. /* Driver init function */
  289. static int __init max3107_init(void)
  290. {
  291. return spi_register_driver(&max3107_driver);
  292. }
  293. /* Driver exit function */
  294. static void __exit max3107_exit(void)
  295. {
  296. spi_unregister_driver(&max3107_driver);
  297. }
  298. module_init(max3107_init);
  299. module_exit(max3107_exit);
  300. MODULE_DESCRIPTION("MAX3107 driver");
  301. MODULE_AUTHOR("Aavamobile");
  302. MODULE_ALIAS("spi:aava-max3107");
  303. MODULE_LICENSE("GPL v2");