nokia-modem.c 7.0 KB

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