1 // SPDX-License-Identifier: GPL-2.0-only
5 * HSI client driver for Nokia N900 modem.
7 * Copyright (C) 2014 Sebastian Reichel <sre@kernel.org>
10 #include <linux/gpio/consumer.h>
11 #include <linux/hsi/hsi.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/of_irq.h>
16 #include <linux/hsi/ssi_protocol.h>
18 static unsigned int pm
= 1;
19 module_param(pm
, int, 0400);
21 "Enable power management (0=disabled, 1=userland based [default])");
23 struct nokia_modem_gpio
{
24 struct gpio_desc
*gpio
;
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
;
34 struct hsi_client
*ssi_protocol
;
35 struct hsi_client
*cmt_speech
;
38 static void do_nokia_modem_rst_ind_tasklet(unsigned long data
)
40 struct nokia_modem_device
*modem
= (struct nokia_modem_device
*)data
;
45 dev_info(modem
->device
, "CMT rst line change detected\n");
47 if (modem
->ssi_protocol
)
48 ssip_reset_event(modem
->ssi_protocol
);
51 static irqreturn_t
nokia_modem_rst_ind_isr(int irq
, void *data
)
53 struct nokia_modem_device
*modem
= (struct nokia_modem_device
*)data
;
55 tasklet_schedule(&modem
->nokia_modem_rst_ind_tasklet
);
60 static void nokia_modem_gpio_unexport(struct device
*dev
)
62 struct nokia_modem_device
*modem
= dev_get_drvdata(dev
);
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
);
71 static int nokia_modem_gpio_probe(struct device
*dev
)
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
;
77 gpio_count
= gpiod_count(dev
, NULL
);
79 dev_err(dev
, "missing gpios: %d\n", gpio_count
);
83 gpio_name_count
= of_property_count_strings(np
, "gpio-names");
85 if (gpio_count
!= gpio_name_count
) {
86 dev_err(dev
, "number of gpios does not equal number of gpio names\n");
90 modem
->gpios
= devm_kcalloc(dev
, gpio_count
, sizeof(*modem
->gpios
),
95 modem
->gpio_amount
= gpio_count
;
97 for (i
= 0; i
< gpio_count
; i
++) {
98 modem
->gpios
[i
].gpio
= devm_gpiod_get_index(dev
, NULL
, i
,
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
);
105 err
= of_property_read_string_index(np
, "gpio-names", i
,
106 &(modem
->gpios
[i
].name
));
108 dev_err(dev
, "Could not get gpio name %d\n", i
);
112 err
= gpiod_export(modem
->gpios
[i
].gpio
, 0);
116 err
= gpiod_export_link(dev
, modem
->gpios
[i
].name
,
117 modem
->gpios
[i
].gpio
);
125 static int nokia_modem_probe(struct device
*dev
)
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
;
137 dev_err(dev
, "device tree node not found\n");
141 modem
= devm_kzalloc(dev
, sizeof(*modem
), GFP_KERNEL
);
145 dev_set_drvdata(dev
, modem
);
148 irq
= irq_of_parse_and_map(np
, 0);
150 dev_err(dev
, "Invalid rst_ind interrupt (%d)\n", irq
);
153 modem
->nokia_modem_rst_ind_irq
= irq
;
154 pflags
= irq_get_trigger_type(irq
);
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
);
161 dev_err(dev
, "Request rst_ind irq(%d) failed (flags %d)\n",
165 enable_irq_wake(irq
);
168 err
= nokia_modem_gpio_probe(dev
);
170 dev_err(dev
, "Could not probe GPIOs\n");
175 ssip
.name
= "ssi-protocol";
176 ssip
.tx_cfg
= cl
->tx_cfg
;
177 ssip
.rx_cfg
= cl
->rx_cfg
;
178 ssip
.platform_data
= NULL
;
179 ssip
.archdata
= NULL
;
181 modem
->ssi_protocol
= hsi_new_client(port
, &ssip
);
182 if (!modem
->ssi_protocol
) {
183 dev_err(dev
, "Could not register ssi-protocol device\n");
188 err
= device_attach(&modem
->ssi_protocol
->device
);
190 dev_dbg(dev
, "Missing ssi-protocol driver\n");
193 } else if (err
< 0) {
194 dev_err(dev
, "Could not load ssi-protocol driver (%d)\n", err
);
198 cmtspeech
.name
= "cmt-speech";
199 cmtspeech
.tx_cfg
= cl
->tx_cfg
;
200 cmtspeech
.rx_cfg
= cl
->rx_cfg
;
201 cmtspeech
.platform_data
= NULL
;
202 cmtspeech
.archdata
= NULL
;
204 modem
->cmt_speech
= hsi_new_client(port
, &cmtspeech
);
205 if (!modem
->cmt_speech
) {
206 dev_err(dev
, "Could not register cmt-speech device\n");
211 err
= device_attach(&modem
->cmt_speech
->device
);
213 dev_dbg(dev
, "Missing cmt-speech driver\n");
216 } else if (err
< 0) {
217 dev_err(dev
, "Could not load cmt-speech driver (%d)\n", err
);
221 dev_info(dev
, "Registered Nokia HSI modem\n");
226 hsi_remove_client(&modem
->cmt_speech
->device
, NULL
);
228 hsi_remove_client(&modem
->ssi_protocol
->device
, NULL
);
230 nokia_modem_gpio_unexport(dev
);
232 disable_irq_wake(modem
->nokia_modem_rst_ind_irq
);
233 tasklet_kill(&modem
->nokia_modem_rst_ind_tasklet
);
238 static int nokia_modem_remove(struct device
*dev
)
240 struct nokia_modem_device
*modem
= dev_get_drvdata(dev
);
245 if (modem
->cmt_speech
) {
246 hsi_remove_client(&modem
->cmt_speech
->device
, NULL
);
247 modem
->cmt_speech
= NULL
;
250 if (modem
->ssi_protocol
) {
251 hsi_remove_client(&modem
->ssi_protocol
->device
, NULL
);
252 modem
->ssi_protocol
= NULL
;
255 nokia_modem_gpio_unexport(dev
);
256 dev_set_drvdata(dev
, NULL
);
257 disable_irq_wake(modem
->nokia_modem_rst_ind_irq
);
258 tasklet_kill(&modem
->nokia_modem_rst_ind_tasklet
);
264 static const struct of_device_id nokia_modem_of_match
[] = {
265 { .compatible
= "nokia,n900-modem", },
266 { .compatible
= "nokia,n950-modem", },
267 { .compatible
= "nokia,n9-modem", },
270 MODULE_DEVICE_TABLE(of
, nokia_modem_of_match
);
273 static struct hsi_client_driver nokia_modem_driver
= {
275 .name
= "nokia-modem",
276 .owner
= THIS_MODULE
,
277 .probe
= nokia_modem_probe
,
278 .remove
= nokia_modem_remove
,
279 .of_match_table
= of_match_ptr(nokia_modem_of_match
),
283 static int __init
nokia_modem_init(void)
285 return hsi_register_client_driver(&nokia_modem_driver
);
287 module_init(nokia_modem_init
);
289 static void __exit
nokia_modem_exit(void)
291 hsi_unregister_client_driver(&nokia_modem_driver
);
293 module_exit(nokia_modem_exit
);
295 MODULE_ALIAS("hsi:nokia-modem");
296 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
297 MODULE_DESCRIPTION("HSI driver module for Nokia N900 Modem");
298 MODULE_LICENSE("GPL");