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