nokia-modem.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * nokia-modem.c
  3. *
  4. * HSI client driver for Nokia N900 modem.
  5. *
  6. * Copyright (C) 2014 Sebastian Reichel <sre@kernel.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/hsi/hsi.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/of.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/hsi/ssi_protocol.h>
  30. static unsigned int pm = 1;
  31. module_param(pm, int, 0400);
  32. MODULE_PARM_DESC(pm,
  33. "Enable power management (0=disabled, 1=userland based [default])");
  34. struct nokia_modem_gpio {
  35. struct gpio_desc *gpio;
  36. const char *name;
  37. };
  38. struct nokia_modem_device {
  39. struct tasklet_struct nokia_modem_rst_ind_tasklet;
  40. int nokia_modem_rst_ind_irq;
  41. struct device *device;
  42. struct nokia_modem_gpio *gpios;
  43. int gpio_amount;
  44. struct hsi_client *ssi_protocol;
  45. };
  46. static void do_nokia_modem_rst_ind_tasklet(unsigned long data)
  47. {
  48. struct nokia_modem_device *modem = (struct nokia_modem_device *)data;
  49. if (!modem)
  50. return;
  51. dev_info(modem->device, "CMT rst line change detected\n");
  52. if (modem->ssi_protocol)
  53. ssip_reset_event(modem->ssi_protocol);
  54. }
  55. static irqreturn_t nokia_modem_rst_ind_isr(int irq, void *data)
  56. {
  57. struct nokia_modem_device *modem = (struct nokia_modem_device *)data;
  58. tasklet_schedule(&modem->nokia_modem_rst_ind_tasklet);
  59. return IRQ_HANDLED;
  60. }
  61. static void nokia_modem_gpio_unexport(struct device *dev)
  62. {
  63. struct nokia_modem_device *modem = dev_get_drvdata(dev);
  64. int i;
  65. for (i = 0; i < modem->gpio_amount; i++) {
  66. sysfs_remove_link(&dev->kobj, modem->gpios[i].name);
  67. gpiod_unexport(modem->gpios[i].gpio);
  68. }
  69. }
  70. static int nokia_modem_gpio_probe(struct device *dev)
  71. {
  72. struct device_node *np = dev->of_node;
  73. struct nokia_modem_device *modem = dev_get_drvdata(dev);
  74. int gpio_count, gpio_name_count, i, err;
  75. gpio_count = of_gpio_count(np);
  76. if (gpio_count < 0) {
  77. dev_err(dev, "missing gpios: %d\n", gpio_count);
  78. return gpio_count;
  79. }
  80. gpio_name_count = of_property_count_strings(np, "gpio-names");
  81. if (gpio_count != gpio_name_count) {
  82. dev_err(dev, "number of gpios does not equal number of gpio names\n");
  83. return -EINVAL;
  84. }
  85. modem->gpios = devm_kzalloc(dev, gpio_count *
  86. sizeof(struct nokia_modem_gpio), GFP_KERNEL);
  87. if (!modem->gpios) {
  88. dev_err(dev, "Could not allocate memory for gpios\n");
  89. return -ENOMEM;
  90. }
  91. modem->gpio_amount = gpio_count;
  92. for (i = 0; i < gpio_count; i++) {
  93. modem->gpios[i].gpio = devm_gpiod_get_index(dev, NULL, i);
  94. if (IS_ERR(modem->gpios[i].gpio)) {
  95. dev_err(dev, "Could not get gpio %d\n", i);
  96. return PTR_ERR(modem->gpios[i].gpio);
  97. }
  98. err = of_property_read_string_index(np, "gpio-names", i,
  99. &(modem->gpios[i].name));
  100. if (err) {
  101. dev_err(dev, "Could not get gpio name %d\n", i);
  102. return err;
  103. }
  104. err = gpiod_direction_output(modem->gpios[i].gpio, 0);
  105. if (err)
  106. return err;
  107. err = gpiod_export(modem->gpios[i].gpio, 0);
  108. if (err)
  109. return err;
  110. err = gpiod_export_link(dev, modem->gpios[i].name,
  111. modem->gpios[i].gpio);
  112. if (err)
  113. return err;
  114. }
  115. return 0;
  116. }
  117. static int nokia_modem_probe(struct device *dev)
  118. {
  119. struct device_node *np;
  120. struct nokia_modem_device *modem;
  121. struct hsi_client *cl = to_hsi_client(dev);
  122. struct hsi_port *port = hsi_get_port(cl);
  123. int irq, pflags, err;
  124. struct hsi_board_info ssip;
  125. np = dev->of_node;
  126. if (!np) {
  127. dev_err(dev, "device tree node not found\n");
  128. return -ENXIO;
  129. }
  130. modem = devm_kzalloc(dev, sizeof(*modem), GFP_KERNEL);
  131. if (!modem) {
  132. dev_err(dev, "Could not allocate memory for nokia_modem_device\n");
  133. return -ENOMEM;
  134. }
  135. dev_set_drvdata(dev, modem);
  136. irq = irq_of_parse_and_map(np, 0);
  137. if (!irq) {
  138. dev_err(dev, "Invalid rst_ind interrupt (%d)\n", irq);
  139. return -EINVAL;
  140. }
  141. modem->nokia_modem_rst_ind_irq = irq;
  142. pflags = irq_get_trigger_type(irq);
  143. tasklet_init(&modem->nokia_modem_rst_ind_tasklet,
  144. do_nokia_modem_rst_ind_tasklet, (unsigned long)modem);
  145. err = devm_request_irq(dev, irq, nokia_modem_rst_ind_isr,
  146. pflags, "modem_rst_ind", modem);
  147. if (err < 0) {
  148. dev_err(dev, "Request rst_ind irq(%d) failed (flags %d)\n",
  149. irq, pflags);
  150. return err;
  151. }
  152. enable_irq_wake(irq);
  153. if(pm) {
  154. err = nokia_modem_gpio_probe(dev);
  155. if (err < 0) {
  156. dev_err(dev, "Could not probe GPIOs\n");
  157. goto error1;
  158. }
  159. }
  160. ssip.name = "ssi-protocol";
  161. ssip.tx_cfg = cl->tx_cfg;
  162. ssip.rx_cfg = cl->rx_cfg;
  163. ssip.platform_data = NULL;
  164. ssip.archdata = NULL;
  165. modem->ssi_protocol = hsi_new_client(port, &ssip);
  166. if (!modem->ssi_protocol) {
  167. dev_err(dev, "Could not register ssi-protocol device\n");
  168. goto error2;
  169. }
  170. err = device_attach(&modem->ssi_protocol->device);
  171. if (err == 0) {
  172. dev_err(dev, "Missing ssi-protocol driver\n");
  173. err = -EPROBE_DEFER;
  174. goto error3;
  175. } else if (err < 0) {
  176. dev_err(dev, "Could not load ssi-protocol driver (%d)\n", err);
  177. goto error3;
  178. }
  179. /* TODO: register cmt-speech hsi client */
  180. dev_info(dev, "Registered Nokia HSI modem\n");
  181. return 0;
  182. error3:
  183. hsi_remove_client(&modem->ssi_protocol->device, NULL);
  184. error2:
  185. nokia_modem_gpio_unexport(dev);
  186. error1:
  187. disable_irq_wake(modem->nokia_modem_rst_ind_irq);
  188. tasklet_kill(&modem->nokia_modem_rst_ind_tasklet);
  189. return err;
  190. }
  191. static int nokia_modem_remove(struct device *dev)
  192. {
  193. struct nokia_modem_device *modem = dev_get_drvdata(dev);
  194. if (!modem)
  195. return 0;
  196. if (modem->ssi_protocol) {
  197. hsi_remove_client(&modem->ssi_protocol->device, NULL);
  198. modem->ssi_protocol = NULL;
  199. }
  200. nokia_modem_gpio_unexport(dev);
  201. dev_set_drvdata(dev, NULL);
  202. disable_irq_wake(modem->nokia_modem_rst_ind_irq);
  203. tasklet_kill(&modem->nokia_modem_rst_ind_tasklet);
  204. return 0;
  205. }
  206. #ifdef CONFIG_OF
  207. static const struct of_device_id nokia_modem_of_match[] = {
  208. { .compatible = "nokia,n900-modem", },
  209. {},
  210. };
  211. MODULE_DEVICE_TABLE(of, nokia_modem_of_match);
  212. #endif
  213. static struct hsi_client_driver nokia_modem_driver = {
  214. .driver = {
  215. .name = "nokia-modem",
  216. .owner = THIS_MODULE,
  217. .probe = nokia_modem_probe,
  218. .remove = nokia_modem_remove,
  219. .of_match_table = of_match_ptr(nokia_modem_of_match),
  220. },
  221. };
  222. static int __init nokia_modem_init(void)
  223. {
  224. return hsi_register_client_driver(&nokia_modem_driver);
  225. }
  226. module_init(nokia_modem_init);
  227. static void __exit nokia_modem_exit(void)
  228. {
  229. hsi_unregister_client_driver(&nokia_modem_driver);
  230. }
  231. module_exit(nokia_modem_exit);
  232. MODULE_ALIAS("hsi:nokia-modem");
  233. MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
  234. MODULE_DESCRIPTION("HSI driver module for Nokia N900 Modem");
  235. MODULE_LICENSE("GPL");