nokia-modem.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. struct hsi_client *cmt_speech;
  46. };
  47. static void do_nokia_modem_rst_ind_tasklet(unsigned long data)
  48. {
  49. struct nokia_modem_device *modem = (struct nokia_modem_device *)data;
  50. if (!modem)
  51. return;
  52. dev_info(modem->device, "CMT rst line change detected\n");
  53. if (modem->ssi_protocol)
  54. ssip_reset_event(modem->ssi_protocol);
  55. }
  56. static irqreturn_t nokia_modem_rst_ind_isr(int irq, void *data)
  57. {
  58. struct nokia_modem_device *modem = (struct nokia_modem_device *)data;
  59. tasklet_schedule(&modem->nokia_modem_rst_ind_tasklet);
  60. return IRQ_HANDLED;
  61. }
  62. static void nokia_modem_gpio_unexport(struct device *dev)
  63. {
  64. struct nokia_modem_device *modem = dev_get_drvdata(dev);
  65. int i;
  66. for (i = 0; i < modem->gpio_amount; i++) {
  67. sysfs_remove_link(&dev->kobj, modem->gpios[i].name);
  68. gpiod_unexport(modem->gpios[i].gpio);
  69. }
  70. }
  71. static int nokia_modem_gpio_probe(struct device *dev)
  72. {
  73. struct device_node *np = dev->of_node;
  74. struct nokia_modem_device *modem = dev_get_drvdata(dev);
  75. int gpio_count, gpio_name_count, i, err;
  76. gpio_count = of_gpio_count(np);
  77. if (gpio_count < 0) {
  78. dev_err(dev, "missing gpios: %d\n", gpio_count);
  79. return gpio_count;
  80. }
  81. gpio_name_count = of_property_count_strings(np, "gpio-names");
  82. if (gpio_count != gpio_name_count) {
  83. dev_err(dev, "number of gpios does not equal number of gpio names\n");
  84. return -EINVAL;
  85. }
  86. modem->gpios = devm_kzalloc(dev, gpio_count *
  87. sizeof(struct nokia_modem_gpio), GFP_KERNEL);
  88. if (!modem->gpios) {
  89. dev_err(dev, "Could not allocate memory for gpios\n");
  90. return -ENOMEM;
  91. }
  92. modem->gpio_amount = gpio_count;
  93. for (i = 0; i < gpio_count; i++) {
  94. modem->gpios[i].gpio = devm_gpiod_get_index(dev, NULL, i,
  95. GPIOD_OUT_LOW);
  96. if (IS_ERR(modem->gpios[i].gpio)) {
  97. dev_err(dev, "Could not get gpio %d\n", i);
  98. return PTR_ERR(modem->gpios[i].gpio);
  99. }
  100. err = of_property_read_string_index(np, "gpio-names", i,
  101. &(modem->gpios[i].name));
  102. if (err) {
  103. dev_err(dev, "Could not get gpio name %d\n", i);
  104. return err;
  105. }
  106. err = gpiod_export(modem->gpios[i].gpio, 0);
  107. if (err)
  108. return err;
  109. err = gpiod_export_link(dev, modem->gpios[i].name,
  110. modem->gpios[i].gpio);
  111. if (err)
  112. return err;
  113. }
  114. return 0;
  115. }
  116. static int nokia_modem_probe(struct device *dev)
  117. {
  118. struct device_node *np;
  119. struct nokia_modem_device *modem;
  120. struct hsi_client *cl = to_hsi_client(dev);
  121. struct hsi_port *port = hsi_get_port(cl);
  122. int irq, pflags, err;
  123. struct hsi_board_info ssip;
  124. struct hsi_board_info cmtspeech;
  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. modem->device = dev;
  137. irq = irq_of_parse_and_map(np, 0);
  138. if (!irq) {
  139. dev_err(dev, "Invalid rst_ind interrupt (%d)\n", irq);
  140. return -EINVAL;
  141. }
  142. modem->nokia_modem_rst_ind_irq = irq;
  143. pflags = irq_get_trigger_type(irq);
  144. tasklet_init(&modem->nokia_modem_rst_ind_tasklet,
  145. do_nokia_modem_rst_ind_tasklet, (unsigned long)modem);
  146. err = devm_request_irq(dev, irq, nokia_modem_rst_ind_isr,
  147. pflags, "modem_rst_ind", modem);
  148. if (err < 0) {
  149. dev_err(dev, "Request rst_ind irq(%d) failed (flags %d)\n",
  150. irq, pflags);
  151. return err;
  152. }
  153. enable_irq_wake(irq);
  154. if(pm) {
  155. err = nokia_modem_gpio_probe(dev);
  156. if (err < 0) {
  157. dev_err(dev, "Could not probe GPIOs\n");
  158. goto error1;
  159. }
  160. }
  161. ssip.name = "ssi-protocol";
  162. ssip.tx_cfg = cl->tx_cfg;
  163. ssip.rx_cfg = cl->rx_cfg;
  164. ssip.platform_data = NULL;
  165. ssip.archdata = NULL;
  166. modem->ssi_protocol = hsi_new_client(port, &ssip);
  167. if (!modem->ssi_protocol) {
  168. dev_err(dev, "Could not register ssi-protocol device\n");
  169. err = -ENOMEM;
  170. goto error2;
  171. }
  172. err = device_attach(&modem->ssi_protocol->device);
  173. if (err == 0) {
  174. dev_dbg(dev, "Missing ssi-protocol driver\n");
  175. err = -EPROBE_DEFER;
  176. goto error3;
  177. } else if (err < 0) {
  178. dev_err(dev, "Could not load ssi-protocol driver (%d)\n", err);
  179. goto error3;
  180. }
  181. cmtspeech.name = "cmt-speech";
  182. cmtspeech.tx_cfg = cl->tx_cfg;
  183. cmtspeech.rx_cfg = cl->rx_cfg;
  184. cmtspeech.platform_data = NULL;
  185. cmtspeech.archdata = NULL;
  186. modem->cmt_speech = hsi_new_client(port, &cmtspeech);
  187. if (!modem->cmt_speech) {
  188. dev_err(dev, "Could not register cmt-speech device\n");
  189. err = -ENOMEM;
  190. goto error3;
  191. }
  192. err = device_attach(&modem->cmt_speech->device);
  193. if (err == 0) {
  194. dev_dbg(dev, "Missing cmt-speech driver\n");
  195. err = -EPROBE_DEFER;
  196. goto error4;
  197. } else if (err < 0) {
  198. dev_err(dev, "Could not load cmt-speech driver (%d)\n", err);
  199. goto error4;
  200. }
  201. dev_info(dev, "Registered Nokia HSI modem\n");
  202. return 0;
  203. error4:
  204. hsi_remove_client(&modem->cmt_speech->device, NULL);
  205. error3:
  206. hsi_remove_client(&modem->ssi_protocol->device, NULL);
  207. error2:
  208. nokia_modem_gpio_unexport(dev);
  209. error1:
  210. disable_irq_wake(modem->nokia_modem_rst_ind_irq);
  211. tasklet_kill(&modem->nokia_modem_rst_ind_tasklet);
  212. return err;
  213. }
  214. static int nokia_modem_remove(struct device *dev)
  215. {
  216. struct nokia_modem_device *modem = dev_get_drvdata(dev);
  217. if (!modem)
  218. return 0;
  219. if (modem->cmt_speech) {
  220. hsi_remove_client(&modem->cmt_speech->device, NULL);
  221. modem->cmt_speech = NULL;
  222. }
  223. if (modem->ssi_protocol) {
  224. hsi_remove_client(&modem->ssi_protocol->device, NULL);
  225. modem->ssi_protocol = NULL;
  226. }
  227. nokia_modem_gpio_unexport(dev);
  228. dev_set_drvdata(dev, NULL);
  229. disable_irq_wake(modem->nokia_modem_rst_ind_irq);
  230. tasklet_kill(&modem->nokia_modem_rst_ind_tasklet);
  231. return 0;
  232. }
  233. #ifdef CONFIG_OF
  234. static const struct of_device_id nokia_modem_of_match[] = {
  235. { .compatible = "nokia,n900-modem", },
  236. {},
  237. };
  238. MODULE_DEVICE_TABLE(of, nokia_modem_of_match);
  239. #endif
  240. static struct hsi_client_driver nokia_modem_driver = {
  241. .driver = {
  242. .name = "nokia-modem",
  243. .owner = THIS_MODULE,
  244. .probe = nokia_modem_probe,
  245. .remove = nokia_modem_remove,
  246. .of_match_table = of_match_ptr(nokia_modem_of_match),
  247. },
  248. };
  249. static int __init nokia_modem_init(void)
  250. {
  251. return hsi_register_client_driver(&nokia_modem_driver);
  252. }
  253. module_init(nokia_modem_init);
  254. static void __exit nokia_modem_exit(void)
  255. {
  256. hsi_unregister_client_driver(&nokia_modem_driver);
  257. }
  258. module_exit(nokia_modem_exit);
  259. MODULE_ALIAS("hsi:nokia-modem");
  260. MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
  261. MODULE_DESCRIPTION("HSI driver module for Nokia N900 Modem");
  262. MODULE_LICENSE("GPL");