Commit | Line | Data |
---|---|---|
d0088ce1 PS |
1 | /* |
2 | * Shared Transport Line discipline driver Core | |
3 | * Init Manager module responsible for GPIO control | |
4 | * and firmware download | |
a0cc2f3b PS |
5 | * Copyright (C) 2009-2010 Texas Instruments |
6 | * Author: Pavan Savoy <pavan_savoy@ti.com> | |
d0088ce1 PS |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | ||
23 | #define pr_fmt(fmt) "(stk) :" fmt | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/jiffies.h> | |
26 | #include <linux/firmware.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/wait.h> | |
29 | #include <linux/gpio.h> | |
c1afac15 PS |
30 | #include <linux/debugfs.h> |
31 | #include <linux/seq_file.h> | |
d0088ce1 | 32 | #include <linux/sched.h> |
773d6790 | 33 | #include <linux/sysfs.h> |
ec60d0ad | 34 | #include <linux/tty.h> |
d0088ce1 | 35 | |
5c88b021 | 36 | #include <linux/skbuff.h> |
e5558679 | 37 | #include <linux/ti_wilink_st.h> |
eb12a679 | 38 | #include <linux/module.h> |
46d0d333 GJ |
39 | #include <linux/of.h> |
40 | #include <linux/of_device.h> | |
d0088ce1 | 41 | |
dbd3a870 | 42 | #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */ |
73f12e8d | 43 | static struct platform_device *st_kim_devices[MAX_ST_DEVICES]; |
d0088ce1 PS |
44 | |
45 | /**********************************************************************/ | |
46 | /* internal functions */ | |
47 | ||
46d0d333 GJ |
48 | struct ti_st_plat_data *dt_pdata; |
49 | static struct ti_st_plat_data *get_platform_data(struct device *dev); | |
50 | ||
dbd3a870 PS |
51 | /** |
52 | * st_get_plat_device - | |
53 | * function which returns the reference to the platform device | |
54 | * requested by id. As of now only 1 such device exists (id=0) | |
55 | * the context requesting for reference can get the id to be | |
56 | * requested by a. The protocol driver which is registering or | |
57 | * b. the tty device which is opened. | |
58 | */ | |
59 | static struct platform_device *st_get_plat_device(int id) | |
60 | { | |
61 | return st_kim_devices[id]; | |
62 | } | |
63 | ||
36b5aee4 PS |
64 | /** |
65 | * validate_firmware_response - | |
66 | * function to return whether the firmware response was proper | |
67 | * in case of error don't complete so that waiting for proper | |
68 | * response times out | |
d0088ce1 | 69 | */ |
27712b39 | 70 | static void validate_firmware_response(struct kim_data_s *kim_gdata) |
d0088ce1 | 71 | { |
38d9df49 | 72 | struct sk_buff *skb = kim_gdata->rx_skb; |
8565adbc PS |
73 | if (!skb) |
74 | return; | |
75 | ||
76 | /* these magic numbers are the position in the response buffer which | |
77 | * allows us to distinguish whether the response is for the read | |
78 | * version info. command | |
79 | */ | |
80 | if (skb->data[2] == 0x01 && skb->data[3] == 0x01 && | |
81 | skb->data[4] == 0x10 && skb->data[5] == 0x00) { | |
82 | /* fw version response */ | |
83 | memcpy(kim_gdata->resp_buffer, | |
84 | kim_gdata->rx_skb->data, | |
85 | kim_gdata->rx_skb->len); | |
86 | complete_all(&kim_gdata->kim_rcvd); | |
87 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; | |
88 | kim_gdata->rx_skb = NULL; | |
89 | kim_gdata->rx_count = 0; | |
90 | } else if (unlikely(skb->data[5] != 0)) { | |
d0088ce1 PS |
91 | pr_err("no proper response during fw download"); |
92 | pr_err("data6 %x", skb->data[5]); | |
76ff0e64 | 93 | kfree_skb(skb); |
d0088ce1 PS |
94 | return; /* keep waiting for the proper response */ |
95 | } | |
96 | /* becos of all the script being downloaded */ | |
97 | complete_all(&kim_gdata->kim_rcvd); | |
98 | kfree_skb(skb); | |
99 | } | |
100 | ||
101 | /* check for data len received inside kim_int_recv | |
102 | * most often hit the last case to update state to waiting for data | |
103 | */ | |
38d9df49 | 104 | static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len) |
d0088ce1 PS |
105 | { |
106 | register int room = skb_tailroom(kim_gdata->rx_skb); | |
107 | ||
e6d9e64e | 108 | pr_debug("len %d room %d", len, room); |
d0088ce1 PS |
109 | |
110 | if (!len) { | |
38d9df49 | 111 | validate_firmware_response(kim_gdata); |
d0088ce1 PS |
112 | } else if (len > room) { |
113 | /* Received packet's payload length is larger. | |
114 | * We can't accommodate it in created skb. | |
115 | */ | |
116 | pr_err("Data length is too large len %d room %d", len, | |
117 | room); | |
118 | kfree_skb(kim_gdata->rx_skb); | |
119 | } else { | |
120 | /* Packet header has non-zero payload length and | |
121 | * we have enough space in created skb. Lets read | |
122 | * payload data */ | |
5c88b021 | 123 | kim_gdata->rx_state = ST_W4_DATA; |
d0088ce1 PS |
124 | kim_gdata->rx_count = len; |
125 | return len; | |
126 | } | |
127 | ||
128 | /* Change ST LL state to continue to process next | |
129 | * packet */ | |
130 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; | |
131 | kim_gdata->rx_skb = NULL; | |
132 | kim_gdata->rx_count = 0; | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
36b5aee4 PS |
137 | /** |
138 | * kim_int_recv - receive function called during firmware download | |
139 | * firmware download responses on different UART drivers | |
140 | * have been observed to come in bursts of different | |
141 | * tty_receive and hence the logic | |
d0088ce1 | 142 | */ |
27712b39 | 143 | static void kim_int_recv(struct kim_data_s *kim_gdata, |
38d9df49 | 144 | const unsigned char *data, long count) |
d0088ce1 | 145 | { |
73f12e8d | 146 | const unsigned char *ptr; |
73f12e8d | 147 | int len = 0, type = 0; |
5c88b021 | 148 | unsigned char *plen; |
d0088ce1 | 149 | |
e6d9e64e | 150 | pr_debug("%s", __func__); |
d0088ce1 | 151 | /* Decode received bytes here */ |
73f12e8d | 152 | ptr = data; |
d0088ce1 PS |
153 | if (unlikely(ptr == NULL)) { |
154 | pr_err(" received null from TTY "); | |
155 | return; | |
156 | } | |
73f12e8d | 157 | |
d0088ce1 PS |
158 | while (count) { |
159 | if (kim_gdata->rx_count) { | |
160 | len = min_t(unsigned int, kim_gdata->rx_count, count); | |
161 | memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len); | |
162 | kim_gdata->rx_count -= len; | |
163 | count -= len; | |
164 | ptr += len; | |
165 | ||
166 | if (kim_gdata->rx_count) | |
167 | continue; | |
168 | ||
169 | /* Check ST RX state machine , where are we? */ | |
170 | switch (kim_gdata->rx_state) { | |
171 | /* Waiting for complete packet ? */ | |
5c88b021 | 172 | case ST_W4_DATA: |
e6d9e64e | 173 | pr_debug("Complete pkt received"); |
38d9df49 | 174 | validate_firmware_response(kim_gdata); |
d0088ce1 PS |
175 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; |
176 | kim_gdata->rx_skb = NULL; | |
177 | continue; | |
178 | /* Waiting for Bluetooth event header ? */ | |
5c88b021 PS |
179 | case ST_W4_HEADER: |
180 | plen = | |
181 | (unsigned char *)&kim_gdata->rx_skb->data[1]; | |
182 | pr_debug("event hdr: plen 0x%02x\n", *plen); | |
183 | kim_check_data_len(kim_gdata, *plen); | |
d0088ce1 PS |
184 | continue; |
185 | } /* end of switch */ | |
186 | } /* end of if rx_state */ | |
187 | switch (*ptr) { | |
188 | /* Bluetooth event packet? */ | |
5c88b021 PS |
189 | case 0x04: |
190 | kim_gdata->rx_state = ST_W4_HEADER; | |
191 | kim_gdata->rx_count = 2; | |
192 | type = *ptr; | |
d0088ce1 PS |
193 | break; |
194 | default: | |
195 | pr_info("unknown packet"); | |
196 | ptr++; | |
197 | count--; | |
198 | continue; | |
36b5aee4 | 199 | } |
d0088ce1 PS |
200 | ptr++; |
201 | count--; | |
202 | kim_gdata->rx_skb = | |
5c88b021 | 203 | alloc_skb(1024+8, GFP_ATOMIC); |
d0088ce1 PS |
204 | if (!kim_gdata->rx_skb) { |
205 | pr_err("can't allocate mem for new packet"); | |
206 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; | |
207 | kim_gdata->rx_count = 0; | |
208 | return; | |
36b5aee4 | 209 | } |
5c88b021 PS |
210 | skb_reserve(kim_gdata->rx_skb, 8); |
211 | kim_gdata->rx_skb->cb[0] = 4; | |
212 | kim_gdata->rx_skb->cb[1] = 0; | |
213 | ||
36b5aee4 | 214 | } |
d0088ce1 PS |
215 | return; |
216 | } | |
217 | ||
38d9df49 | 218 | static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name) |
d0088ce1 PS |
219 | { |
220 | unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0; | |
73f12e8d | 221 | const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 }; |
d0088ce1 | 222 | |
e6d9e64e | 223 | pr_debug("%s", __func__); |
d0088ce1 | 224 | |
16735d02 | 225 | reinit_completion(&kim_gdata->kim_rcvd); |
d0088ce1 PS |
226 | if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) { |
227 | pr_err("kim: couldn't write 4 bytes"); | |
70442664 | 228 | return -EIO; |
d0088ce1 PS |
229 | } |
230 | ||
53702358 PS |
231 | if (!wait_for_completion_interruptible_timeout( |
232 | &kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) { | |
d0088ce1 | 233 | pr_err(" waiting for ver info- timed out "); |
70442664 | 234 | return -ETIMEDOUT; |
d0088ce1 | 235 | } |
16735d02 | 236 | reinit_completion(&kim_gdata->kim_rcvd); |
8565adbc PS |
237 | /* the positions 12 & 13 in the response buffer provide with the |
238 | * chip, major & minor numbers | |
239 | */ | |
d0088ce1 PS |
240 | |
241 | version = | |
8565adbc PS |
242 | MAKEWORD(kim_gdata->resp_buffer[12], |
243 | kim_gdata->resp_buffer[13]); | |
d0088ce1 PS |
244 | chip = (version & 0x7C00) >> 10; |
245 | min_ver = (version & 0x007F); | |
246 | maj_ver = (version & 0x0380) >> 7; | |
247 | ||
248 | if (version & 0x8000) | |
249 | maj_ver |= 0x0008; | |
250 | ||
4dcc2ab3 EBS |
251 | sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts", |
252 | chip, maj_ver, min_ver); | |
e2a53282 NJ |
253 | |
254 | /* to be accessed later via sysfs entry */ | |
255 | kim_gdata->version.full = version; | |
256 | kim_gdata->version.chip = chip; | |
257 | kim_gdata->version.maj_ver = maj_ver; | |
258 | kim_gdata->version.min_ver = min_ver; | |
259 | ||
d0088ce1 | 260 | pr_info("%s", bts_scr_name); |
320920cb | 261 | return 0; |
d0088ce1 PS |
262 | } |
263 | ||
27712b39 | 264 | static void skip_change_remote_baud(unsigned char **ptr, long *len) |
ef04d121 PS |
265 | { |
266 | unsigned char *nxt_action, *cur_action; | |
267 | cur_action = *ptr; | |
268 | ||
269 | nxt_action = cur_action + sizeof(struct bts_action) + | |
270 | ((struct bts_action *) cur_action)->size; | |
271 | ||
272 | if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) { | |
273 | pr_err("invalid action after change remote baud command"); | |
274 | } else { | |
275 | *ptr = *ptr + sizeof(struct bts_action) + | |
9d031d94 | 276 | ((struct bts_action *)cur_action)->size; |
ef04d121 | 277 | *len = *len - (sizeof(struct bts_action) + |
9d031d94 | 278 | ((struct bts_action *)cur_action)->size); |
ef04d121 PS |
279 | /* warn user on not commenting these in firmware */ |
280 | pr_warn("skipping the wait event of change remote baud"); | |
281 | } | |
282 | } | |
283 | ||
36b5aee4 PS |
284 | /** |
285 | * download_firmware - | |
286 | * internal function which parses through the .bts firmware | |
287 | * script file intreprets SEND, DELAY actions only as of now | |
d0088ce1 | 288 | */ |
38d9df49 | 289 | static long download_firmware(struct kim_data_s *kim_gdata) |
d0088ce1 | 290 | { |
320920cb | 291 | long err = 0; |
d0088ce1 | 292 | long len = 0; |
73f12e8d PS |
293 | unsigned char *ptr = NULL; |
294 | unsigned char *action_ptr = NULL; | |
4dcc2ab3 | 295 | unsigned char bts_scr_name[40] = { 0 }; /* 40 char long bts scr name? */ |
ef04d121 PS |
296 | int wr_room_space; |
297 | int cmd_size; | |
298 | unsigned long timeout; | |
d0088ce1 | 299 | |
38d9df49 | 300 | err = read_local_version(kim_gdata, bts_scr_name); |
320920cb | 301 | if (err != 0) { |
d0088ce1 PS |
302 | pr_err("kim: failed to read local ver"); |
303 | return err; | |
304 | } | |
305 | err = | |
306 | request_firmware(&kim_gdata->fw_entry, bts_scr_name, | |
307 | &kim_gdata->kim_pdev->dev); | |
308 | if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) || | |
309 | (kim_gdata->fw_entry->size == 0))) { | |
310 | pr_err(" request_firmware failed(errno %ld) for %s", err, | |
311 | bts_scr_name); | |
70442664 | 312 | return -EINVAL; |
d0088ce1 PS |
313 | } |
314 | ptr = (void *)kim_gdata->fw_entry->data; | |
315 | len = kim_gdata->fw_entry->size; | |
316 | /* bts_header to remove out magic number and | |
317 | * version | |
318 | */ | |
319 | ptr += sizeof(struct bts_header); | |
320 | len -= sizeof(struct bts_header); | |
321 | ||
322 | while (len > 0 && ptr) { | |
e6d9e64e | 323 | pr_debug(" action size %d, type %d ", |
d0088ce1 PS |
324 | ((struct bts_action *)ptr)->size, |
325 | ((struct bts_action *)ptr)->type); | |
326 | ||
327 | switch (((struct bts_action *)ptr)->type) { | |
328 | case ACTION_SEND_COMMAND: /* action send */ | |
2f81a02c | 329 | pr_debug("S"); |
d0088ce1 PS |
330 | action_ptr = &(((struct bts_action *)ptr)->data[0]); |
331 | if (unlikely | |
332 | (((struct hci_command *)action_ptr)->opcode == | |
333 | 0xFF36)) { | |
334 | /* ignore remote change | |
335 | * baud rate HCI VS command */ | |
ef04d121 | 336 | pr_warn("change remote baud" |
e6d9e64e | 337 | " rate command in firmware"); |
ef04d121 | 338 | skip_change_remote_baud(&ptr, &len); |
d0088ce1 PS |
339 | break; |
340 | } | |
ef04d121 PS |
341 | /* |
342 | * Make sure we have enough free space in uart | |
343 | * tx buffer to write current firmware command | |
344 | */ | |
345 | cmd_size = ((struct bts_action *)ptr)->size; | |
346 | timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME); | |
347 | do { | |
348 | wr_room_space = | |
349 | st_get_uart_wr_room(kim_gdata->core_data); | |
350 | if (wr_room_space < 0) { | |
351 | pr_err("Unable to get free " | |
352 | "space info from uart tx buffer"); | |
353 | release_firmware(kim_gdata->fw_entry); | |
354 | return wr_room_space; | |
355 | } | |
356 | mdelay(1); /* wait 1ms before checking room */ | |
357 | } while ((wr_room_space < cmd_size) && | |
358 | time_before(jiffies, timeout)); | |
359 | ||
360 | /* Timeout happened ? */ | |
361 | if (time_after_eq(jiffies, timeout)) { | |
362 | pr_err("Timeout while waiting for free " | |
363 | "free space in uart tx buffer"); | |
364 | release_firmware(kim_gdata->fw_entry); | |
365 | return -ETIMEDOUT; | |
366 | } | |
2f81a02c PS |
367 | /* reinit completion before sending for the |
368 | * relevant wait | |
369 | */ | |
16735d02 | 370 | reinit_completion(&kim_gdata->kim_rcvd); |
d0088ce1 | 371 | |
ef04d121 PS |
372 | /* |
373 | * Free space found in uart buffer, call st_int_write | |
374 | * to send current firmware command to the uart tx | |
375 | * buffer. | |
376 | */ | |
d0088ce1 PS |
377 | err = st_int_write(kim_gdata->core_data, |
378 | ((struct bts_action_send *)action_ptr)->data, | |
379 | ((struct bts_action *)ptr)->size); | |
380 | if (unlikely(err < 0)) { | |
381 | release_firmware(kim_gdata->fw_entry); | |
70442664 | 382 | return err; |
d0088ce1 | 383 | } |
ef04d121 PS |
384 | /* |
385 | * Check number of bytes written to the uart tx buffer | |
386 | * and requested command write size | |
387 | */ | |
388 | if (err != cmd_size) { | |
389 | pr_err("Number of bytes written to uart " | |
390 | "tx buffer are not matching with " | |
391 | "requested cmd write size"); | |
392 | release_firmware(kim_gdata->fw_entry); | |
393 | return -EIO; | |
394 | } | |
395 | break; | |
396 | case ACTION_WAIT_EVENT: /* wait */ | |
2f81a02c | 397 | pr_debug("W"); |
53702358 PS |
398 | if (!wait_for_completion_interruptible_timeout( |
399 | &kim_gdata->kim_rcvd, | |
400 | msecs_to_jiffies(CMD_RESP_TIME))) { | |
ef04d121 | 401 | pr_err("response timeout during fw download "); |
d0088ce1 PS |
402 | /* timed out */ |
403 | release_firmware(kim_gdata->fw_entry); | |
70442664 | 404 | return -ETIMEDOUT; |
d0088ce1 | 405 | } |
16735d02 | 406 | reinit_completion(&kim_gdata->kim_rcvd); |
d0088ce1 PS |
407 | break; |
408 | case ACTION_DELAY: /* sleep */ | |
409 | pr_info("sleep command in scr"); | |
410 | action_ptr = &(((struct bts_action *)ptr)->data[0]); | |
411 | mdelay(((struct bts_action_delay *)action_ptr)->msec); | |
412 | break; | |
413 | } | |
414 | len = | |
415 | len - (sizeof(struct bts_action) + | |
416 | ((struct bts_action *)ptr)->size); | |
417 | ptr = | |
418 | ptr + sizeof(struct bts_action) + | |
419 | ((struct bts_action *)ptr)->size; | |
420 | } | |
421 | /* fw download complete */ | |
422 | release_firmware(kim_gdata->fw_entry); | |
320920cb | 423 | return 0; |
d0088ce1 PS |
424 | } |
425 | ||
426 | /**********************************************************************/ | |
427 | /* functions called from ST core */ | |
d0088ce1 PS |
428 | /* called from ST Core, when REG_IN_PROGRESS (registration in progress) |
429 | * can be because of | |
430 | * 1. response to read local version | |
431 | * 2. during send/recv's of firmware download | |
432 | */ | |
433 | void st_kim_recv(void *disc_data, const unsigned char *data, long count) | |
434 | { | |
38d9df49 PS |
435 | struct st_data_s *st_gdata = (struct st_data_s *)disc_data; |
436 | struct kim_data_s *kim_gdata = st_gdata->kim_data; | |
437 | ||
8565adbc PS |
438 | /* proceed to gather all data and distinguish read fw version response |
439 | * from other fw responses when data gathering is complete | |
440 | */ | |
441 | kim_int_recv(kim_gdata, data, count); | |
d0088ce1 PS |
442 | return; |
443 | } | |
444 | ||
445 | /* to signal completion of line discipline installation | |
446 | * called from ST Core, upon tty_open | |
447 | */ | |
38d9df49 | 448 | void st_kim_complete(void *kim_data) |
d0088ce1 | 449 | { |
38d9df49 | 450 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
d0088ce1 PS |
451 | complete(&kim_gdata->ldisc_installed); |
452 | } | |
453 | ||
36b5aee4 PS |
454 | /** |
455 | * st_kim_start - called from ST Core upon 1st registration | |
456 | * This involves toggling the chip enable gpio, reading | |
457 | * the firmware version from chip, forming the fw file name | |
458 | * based on the chip version, requesting the fw, parsing it | |
459 | * and perform download(send/recv). | |
460 | */ | |
38d9df49 | 461 | long st_kim_start(void *kim_data) |
d0088ce1 | 462 | { |
320920cb | 463 | long err = 0; |
d0088ce1 | 464 | long retry = POR_RETRY_COUNT; |
0d7c5f25 | 465 | struct ti_st_plat_data *pdata; |
38d9df49 PS |
466 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
467 | ||
d0088ce1 | 468 | pr_info(" %s", __func__); |
46d0d333 GJ |
469 | if (kim_gdata->kim_pdev->dev.of_node) { |
470 | pr_debug("use device tree data"); | |
471 | pdata = dt_pdata; | |
472 | } else { | |
473 | pdata = kim_gdata->kim_pdev->dev.platform_data; | |
474 | } | |
d0088ce1 PS |
475 | |
476 | do { | |
0d7c5f25 PS |
477 | /* platform specific enabling code here */ |
478 | if (pdata->chip_enable) | |
479 | pdata->chip_enable(kim_gdata); | |
480 | ||
a7e2ca17 LC |
481 | /* Configure BT nShutdown to HIGH state */ |
482 | gpio_set_value(kim_gdata->nshutdown, GPIO_LOW); | |
483 | mdelay(5); /* FIXME: a proper toggle */ | |
484 | gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH); | |
485 | mdelay(100); | |
d0088ce1 | 486 | /* re-initialize the completion */ |
16735d02 | 487 | reinit_completion(&kim_gdata->ldisc_installed); |
ec60d0ad PS |
488 | /* send notification to UIM */ |
489 | kim_gdata->ldisc_install = 1; | |
490 | pr_info("ldisc_install = 1"); | |
491 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, | |
492 | NULL, "install"); | |
d0088ce1 | 493 | /* wait for ldisc to be installed */ |
53702358 PS |
494 | err = wait_for_completion_interruptible_timeout( |
495 | &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME)); | |
18ccecf9 PS |
496 | if (!err) { |
497 | /* ldisc installation timeout, | |
498 | * flush uart, power cycle BT_EN */ | |
499 | pr_err("ldisc installation timeout"); | |
500 | err = st_kim_stop(kim_gdata); | |
d0088ce1 PS |
501 | continue; |
502 | } else { | |
503 | /* ldisc installed now */ | |
18ccecf9 | 504 | pr_info("line discipline installed"); |
38d9df49 | 505 | err = download_firmware(kim_gdata); |
320920cb | 506 | if (err != 0) { |
18ccecf9 PS |
507 | /* ldisc installed but fw download failed, |
508 | * flush uart & power cycle BT_EN */ | |
d0088ce1 | 509 | pr_err("download firmware failed"); |
18ccecf9 | 510 | err = st_kim_stop(kim_gdata); |
d0088ce1 PS |
511 | continue; |
512 | } else { /* on success don't retry */ | |
513 | break; | |
514 | } | |
515 | } | |
516 | } while (retry--); | |
517 | return err; | |
518 | } | |
519 | ||
36b5aee4 | 520 | /** |
18ccecf9 PS |
521 | * st_kim_stop - stop communication with chip. |
522 | * This can be called from ST Core/KIM, on the- | |
523 | * (a) last un-register when chip need not be powered there-after, | |
524 | * (b) upon failure to either install ldisc or download firmware. | |
525 | * The function is responsible to (a) notify UIM about un-installation, | |
526 | * (b) flush UART if the ldisc was installed. | |
a7e2ca17 LC |
527 | * (c) reset BT_EN - pull down nshutdown at the end. |
528 | * (d) invoke platform's chip disabling routine. | |
36b5aee4 | 529 | */ |
38d9df49 | 530 | long st_kim_stop(void *kim_data) |
d0088ce1 | 531 | { |
320920cb | 532 | long err = 0; |
38d9df49 | 533 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
46d0d333 | 534 | struct ti_st_plat_data *pdata; |
18ccecf9 | 535 | struct tty_struct *tty = kim_gdata->core_data->tty; |
d0088ce1 | 536 | |
16735d02 | 537 | reinit_completion(&kim_gdata->ldisc_installed); |
ec60d0ad | 538 | |
46d0d333 GJ |
539 | if (kim_gdata->kim_pdev->dev.of_node) { |
540 | pr_debug("use device tree data"); | |
541 | pdata = dt_pdata; | |
542 | } else | |
543 | pdata = kim_gdata->kim_pdev->dev.platform_data; | |
544 | ||
545 | ||
18ccecf9 PS |
546 | if (tty) { /* can be called before ldisc is installed */ |
547 | /* Flush any pending characters in the driver and discipline. */ | |
548 | tty_ldisc_flush(tty); | |
549 | tty_driver_flush_buffer(tty); | |
18ccecf9 | 550 | } |
ec60d0ad PS |
551 | |
552 | /* send uninstall notification to UIM */ | |
553 | pr_info("ldisc_install = 0"); | |
554 | kim_gdata->ldisc_install = 0; | |
555 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install"); | |
d0088ce1 PS |
556 | |
557 | /* wait for ldisc to be un-installed */ | |
53702358 PS |
558 | err = wait_for_completion_interruptible_timeout( |
559 | &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME)); | |
d0088ce1 PS |
560 | if (!err) { /* timeout */ |
561 | pr_err(" timed out waiting for ldisc to be un-installed"); | |
b64365a5 | 562 | err = -ETIMEDOUT; |
d0088ce1 PS |
563 | } |
564 | ||
a7e2ca17 LC |
565 | /* By default configure BT nShutdown to LOW state */ |
566 | gpio_set_value(kim_gdata->nshutdown, GPIO_LOW); | |
567 | mdelay(1); | |
568 | gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH); | |
569 | mdelay(1); | |
570 | gpio_set_value(kim_gdata->nshutdown, GPIO_LOW); | |
571 | ||
0d7c5f25 PS |
572 | /* platform specific disable */ |
573 | if (pdata->chip_disable) | |
574 | pdata->chip_disable(kim_gdata); | |
d0088ce1 PS |
575 | return err; |
576 | } | |
577 | ||
578 | /**********************************************************************/ | |
579 | /* functions called from subsystems */ | |
c1afac15 | 580 | /* called when debugfs entry is read from */ |
e2a53282 | 581 | |
c1afac15 | 582 | static int show_version(struct seq_file *s, void *unused) |
e2a53282 | 583 | { |
c1afac15 PS |
584 | struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; |
585 | seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full, | |
e2a53282 NJ |
586 | kim_gdata->version.chip, kim_gdata->version.maj_ver, |
587 | kim_gdata->version.min_ver); | |
c1afac15 | 588 | return 0; |
d0088ce1 PS |
589 | } |
590 | ||
c1afac15 | 591 | static int show_list(struct seq_file *s, void *unused) |
d0088ce1 | 592 | { |
c1afac15 PS |
593 | struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; |
594 | kim_st_list_protocols(kim_gdata->core_data, s); | |
595 | return 0; | |
d0088ce1 PS |
596 | } |
597 | ||
ec60d0ad PS |
598 | static ssize_t show_install(struct device *dev, |
599 | struct device_attribute *attr, char *buf) | |
d0088ce1 | 600 | { |
ec60d0ad PS |
601 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
602 | return sprintf(buf, "%d\n", kim_data->ldisc_install); | |
603 | } | |
d0088ce1 | 604 | |
933aae54 PS |
605 | #ifdef DEBUG |
606 | static ssize_t store_dev_name(struct device *dev, | |
607 | struct device_attribute *attr, const char *buf, size_t count) | |
608 | { | |
609 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
610 | pr_debug("storing dev name >%s<", buf); | |
611 | strncpy(kim_data->dev_name, buf, count); | |
612 | pr_debug("stored dev name >%s<", kim_data->dev_name); | |
613 | return count; | |
614 | } | |
615 | ||
616 | static ssize_t store_baud_rate(struct device *dev, | |
617 | struct device_attribute *attr, const char *buf, size_t count) | |
618 | { | |
619 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
620 | pr_debug("storing baud rate >%s<", buf); | |
621 | sscanf(buf, "%ld", &kim_data->baud_rate); | |
622 | pr_debug("stored baud rate >%ld<", kim_data->baud_rate); | |
623 | return count; | |
624 | } | |
625 | #endif /* if DEBUG */ | |
626 | ||
ec60d0ad PS |
627 | static ssize_t show_dev_name(struct device *dev, |
628 | struct device_attribute *attr, char *buf) | |
629 | { | |
630 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
631 | return sprintf(buf, "%s\n", kim_data->dev_name); | |
632 | } | |
633 | ||
634 | static ssize_t show_baud_rate(struct device *dev, | |
635 | struct device_attribute *attr, char *buf) | |
636 | { | |
637 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
638 | return sprintf(buf, "%ld\n", kim_data->baud_rate); | |
639 | } | |
640 | ||
641 | static ssize_t show_flow_cntrl(struct device *dev, | |
642 | struct device_attribute *attr, char *buf) | |
643 | { | |
644 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
645 | return sprintf(buf, "%d\n", kim_data->flow_cntrl); | |
d0088ce1 PS |
646 | } |
647 | ||
ec60d0ad PS |
648 | /* structures specific for sysfs entries */ |
649 | static struct kobj_attribute ldisc_install = | |
650 | __ATTR(install, 0444, (void *)show_install, NULL); | |
651 | ||
652 | static struct kobj_attribute uart_dev_name = | |
933aae54 PS |
653 | #ifdef DEBUG /* TODO: move this to debug-fs if possible */ |
654 | __ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name); | |
655 | #else | |
ec60d0ad | 656 | __ATTR(dev_name, 0444, (void *)show_dev_name, NULL); |
933aae54 | 657 | #endif |
ec60d0ad PS |
658 | |
659 | static struct kobj_attribute uart_baud_rate = | |
933aae54 PS |
660 | #ifdef DEBUG /* TODO: move to debugfs */ |
661 | __ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate); | |
662 | #else | |
ec60d0ad | 663 | __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL); |
933aae54 | 664 | #endif |
ec60d0ad PS |
665 | |
666 | static struct kobj_attribute uart_flow_cntrl = | |
667 | __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL); | |
668 | ||
669 | static struct attribute *uim_attrs[] = { | |
670 | &ldisc_install.attr, | |
671 | &uart_dev_name.attr, | |
672 | &uart_baud_rate.attr, | |
673 | &uart_flow_cntrl.attr, | |
674 | NULL, | |
675 | }; | |
676 | ||
677 | static struct attribute_group uim_attr_grp = { | |
678 | .attrs = uim_attrs, | |
679 | }; | |
680 | ||
36b5aee4 PS |
681 | /** |
682 | * st_kim_ref - reference the core's data | |
683 | * This references the per-ST platform device in the arch/xx/ | |
684 | * board-xx.c file. | |
685 | * This would enable multiple such platform devices to exist | |
686 | * on a given platform | |
687 | */ | |
dbd3a870 | 688 | void st_kim_ref(struct st_data_s **core_data, int id) |
d0088ce1 | 689 | { |
38d9df49 PS |
690 | struct platform_device *pdev; |
691 | struct kim_data_s *kim_gdata; | |
692 | /* get kim_gdata reference from platform device */ | |
dbd3a870 | 693 | pdev = st_get_plat_device(id); |
7316a9f2 SR |
694 | if (!pdev) { |
695 | *core_data = NULL; | |
696 | return; | |
697 | } | |
9093ca88 | 698 | kim_gdata = platform_get_drvdata(pdev); |
d0088ce1 PS |
699 | *core_data = kim_gdata->core_data; |
700 | } | |
701 | ||
c1afac15 PS |
702 | static int kim_version_open(struct inode *i, struct file *f) |
703 | { | |
704 | return single_open(f, show_version, i->i_private); | |
705 | } | |
706 | ||
707 | static int kim_list_open(struct inode *i, struct file *f) | |
708 | { | |
709 | return single_open(f, show_list, i->i_private); | |
710 | } | |
711 | ||
712 | static const struct file_operations version_debugfs_fops = { | |
713 | /* version info */ | |
714 | .open = kim_version_open, | |
715 | .read = seq_read, | |
716 | .llseek = seq_lseek, | |
717 | .release = single_release, | |
718 | }; | |
719 | static const struct file_operations list_debugfs_fops = { | |
720 | /* protocols info */ | |
721 | .open = kim_list_open, | |
722 | .read = seq_read, | |
723 | .llseek = seq_lseek, | |
724 | .release = single_release, | |
725 | }; | |
726 | ||
d0088ce1 PS |
727 | /**********************************************************************/ |
728 | /* functions called from platform device driver subsystem | |
729 | * need to have a relevant platform device entry in the platform's | |
730 | * board-*.c file | |
731 | */ | |
732 | ||
46d0d333 GJ |
733 | static const struct of_device_id kim_of_match[] = { |
734 | { | |
735 | .compatible = "kim", | |
736 | }, | |
737 | {} | |
738 | }; | |
739 | MODULE_DEVICE_TABLE(of, kim_of_match); | |
740 | ||
741 | static struct ti_st_plat_data *get_platform_data(struct device *dev) | |
742 | { | |
743 | struct device_node *np = dev->of_node; | |
744 | const u32 *dt_property; | |
745 | int len; | |
746 | ||
747 | dt_pdata = kzalloc(sizeof(*dt_pdata), GFP_KERNEL); | |
748 | ||
749 | if (!dt_pdata) | |
750 | pr_err("Can't allocate device_tree platform data\n"); | |
751 | ||
752 | dt_property = of_get_property(np, "dev_name", &len); | |
753 | if (dt_property) | |
754 | memcpy(&dt_pdata->dev_name, dt_property, len); | |
755 | of_property_read_u32(np, "nshutdown_gpio", | |
756 | (u32 *)&dt_pdata->nshutdown_gpio); | |
757 | of_property_read_u32(np, "flow_cntrl", (u32 *)&dt_pdata->flow_cntrl); | |
758 | of_property_read_u32(np, "baud_rate", (u32 *)&dt_pdata->baud_rate); | |
759 | ||
760 | return dt_pdata; | |
761 | } | |
762 | ||
27712b39 | 763 | static struct dentry *kim_debugfs_dir; |
d0088ce1 PS |
764 | static int kim_probe(struct platform_device *pdev) |
765 | { | |
38d9df49 | 766 | struct kim_data_s *kim_gdata; |
46d0d333 | 767 | struct ti_st_plat_data *pdata; |
b2997387 | 768 | int err; |
38d9df49 | 769 | |
46d0d333 GJ |
770 | if (pdev->dev.of_node) |
771 | pdata = get_platform_data(&pdev->dev); | |
772 | else | |
773 | pdata = pdev->dev.platform_data; | |
774 | ||
775 | if (pdata == NULL) { | |
776 | dev_err(&pdev->dev, "Platform Data is missing\n"); | |
777 | return -ENXIO; | |
778 | } | |
779 | ||
dfb7ef7d PS |
780 | if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) { |
781 | /* multiple devices could exist */ | |
782 | st_kim_devices[pdev->id] = pdev; | |
783 | } else { | |
25985edc | 784 | /* platform's sure about existence of 1 device */ |
dfb7ef7d PS |
785 | st_kim_devices[0] = pdev; |
786 | } | |
787 | ||
38d9df49 PS |
788 | kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC); |
789 | if (!kim_gdata) { | |
790 | pr_err("no mem to allocate"); | |
791 | return -ENOMEM; | |
792 | } | |
9093ca88 | 793 | platform_set_drvdata(pdev, kim_gdata); |
d0088ce1 | 794 | |
b2997387 MK |
795 | err = st_core_init(&kim_gdata->core_data); |
796 | if (err != 0) { | |
d0088ce1 | 797 | pr_err(" ST core init failed"); |
b2997387 MK |
798 | err = -EIO; |
799 | goto err_core_init; | |
d0088ce1 | 800 | } |
38d9df49 PS |
801 | /* refer to itself */ |
802 | kim_gdata->core_data->kim_data = kim_gdata; | |
d0088ce1 | 803 | |
a7e2ca17 LC |
804 | /* Claim the chip enable nShutdown gpio from the system */ |
805 | kim_gdata->nshutdown = pdata->nshutdown_gpio; | |
806 | err = gpio_request(kim_gdata->nshutdown, "kim"); | |
807 | if (unlikely(err)) { | |
808 | pr_err(" gpio %ld request failed ", kim_gdata->nshutdown); | |
809 | return err; | |
810 | } | |
811 | ||
812 | /* Configure nShutdown GPIO as output=0 */ | |
813 | err = gpio_direction_output(kim_gdata->nshutdown, 0); | |
814 | if (unlikely(err)) { | |
815 | pr_err(" unable to configure gpio %ld", kim_gdata->nshutdown); | |
816 | return err; | |
817 | } | |
d0088ce1 PS |
818 | /* get reference of pdev for request_firmware |
819 | */ | |
820 | kim_gdata->kim_pdev = pdev; | |
821 | init_completion(&kim_gdata->kim_rcvd); | |
822 | init_completion(&kim_gdata->ldisc_installed); | |
b38fc2d9 | 823 | |
b2997387 MK |
824 | err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp); |
825 | if (err) { | |
ec60d0ad | 826 | pr_err("failed to create sysfs entries"); |
b2997387 | 827 | goto err_sysfs_group; |
d0088ce1 | 828 | } |
e2a53282 | 829 | |
ec60d0ad PS |
830 | /* copying platform data */ |
831 | strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN); | |
832 | kim_gdata->flow_cntrl = pdata->flow_cntrl; | |
833 | kim_gdata->baud_rate = pdata->baud_rate; | |
834 | pr_info("sysfs entries created\n"); | |
835 | ||
c1afac15 | 836 | kim_debugfs_dir = debugfs_create_dir("ti-st", NULL); |
3768528a | 837 | if (!kim_debugfs_dir) { |
c1afac15 | 838 | pr_err(" debugfs entries creation failed "); |
c6ec0fb4 | 839 | return 0; |
e2a53282 | 840 | } |
c1afac15 PS |
841 | |
842 | debugfs_create_file("version", S_IRUGO, kim_debugfs_dir, | |
843 | kim_gdata, &version_debugfs_fops); | |
844 | debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir, | |
845 | kim_gdata, &list_debugfs_fops); | |
320920cb | 846 | return 0; |
b2997387 | 847 | |
b2997387 MK |
848 | err_sysfs_group: |
849 | st_core_exit(kim_gdata->core_data); | |
850 | ||
851 | err_core_init: | |
852 | kfree(kim_gdata); | |
853 | ||
854 | return err; | |
d0088ce1 PS |
855 | } |
856 | ||
857 | static int kim_remove(struct platform_device *pdev) | |
858 | { | |
a7e2ca17 | 859 | /* free the GPIOs requested */ |
46d0d333 | 860 | struct ti_st_plat_data *pdata; |
38d9df49 PS |
861 | struct kim_data_s *kim_gdata; |
862 | ||
46d0d333 GJ |
863 | if (pdev->dev.of_node) { |
864 | pr_debug("use device tree data"); | |
865 | pdata = dt_pdata; | |
866 | } else { | |
867 | pdata = pdev->dev.platform_data; | |
868 | } | |
869 | ||
9093ca88 | 870 | kim_gdata = platform_get_drvdata(pdev); |
d0088ce1 | 871 | |
a7e2ca17 LC |
872 | /* Free the Bluetooth/FM/GPIO |
873 | * nShutdown gpio from the system | |
874 | */ | |
875 | gpio_free(pdata->nshutdown_gpio); | |
876 | pr_info("nshutdown GPIO Freed"); | |
877 | ||
781a7395 | 878 | debugfs_remove_recursive(kim_debugfs_dir); |
ec60d0ad | 879 | sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); |
781a7395 PS |
880 | pr_info("sysfs entries removed"); |
881 | ||
d0088ce1 PS |
882 | kim_gdata->kim_pdev = NULL; |
883 | st_core_exit(kim_gdata->core_data); | |
38d9df49 PS |
884 | |
885 | kfree(kim_gdata); | |
886 | kim_gdata = NULL; | |
46d0d333 GJ |
887 | kfree(dt_pdata); |
888 | dt_pdata = NULL; | |
889 | ||
320920cb | 890 | return 0; |
d0088ce1 PS |
891 | } |
892 | ||
27712b39 | 893 | static int kim_suspend(struct platform_device *pdev, pm_message_t state) |
ec60d0ad | 894 | { |
46d0d333 GJ |
895 | struct ti_st_plat_data *pdata; |
896 | ||
897 | if (pdev->dev.of_node) { | |
898 | pr_debug("use device tree data"); | |
899 | pdata = dt_pdata; | |
900 | } else { | |
901 | pdata = pdev->dev.platform_data; | |
902 | } | |
ec60d0ad PS |
903 | |
904 | if (pdata->suspend) | |
905 | return pdata->suspend(pdev, state); | |
906 | ||
4b4aa3ab | 907 | return 0; |
ec60d0ad PS |
908 | } |
909 | ||
27712b39 | 910 | static int kim_resume(struct platform_device *pdev) |
ec60d0ad | 911 | { |
46d0d333 GJ |
912 | struct ti_st_plat_data *pdata; |
913 | ||
914 | if (pdev->dev.of_node) { | |
915 | pr_debug("use device tree data"); | |
916 | pdata = dt_pdata; | |
917 | } else { | |
918 | pdata = pdev->dev.platform_data; | |
919 | } | |
ec60d0ad PS |
920 | |
921 | if (pdata->resume) | |
922 | return pdata->resume(pdev); | |
923 | ||
4b4aa3ab | 924 | return 0; |
ec60d0ad PS |
925 | } |
926 | ||
d0088ce1 PS |
927 | /**********************************************************************/ |
928 | /* entry point for ST KIM module, called in from ST Core */ | |
ec60d0ad PS |
929 | static struct platform_driver kim_platform_driver = { |
930 | .probe = kim_probe, | |
931 | .remove = kim_remove, | |
932 | .suspend = kim_suspend, | |
933 | .resume = kim_resume, | |
934 | .driver = { | |
935 | .name = "kim", | |
46d0d333 GJ |
936 | .owner = THIS_MODULE, |
937 | .of_match_table = of_match_ptr(kim_of_match), | |
ec60d0ad PS |
938 | }, |
939 | }; | |
d0088ce1 | 940 | |
b00e126f | 941 | module_platform_driver(kim_platform_driver); |
d0088ce1 | 942 | |
d0088ce1 PS |
943 | MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>"); |
944 | MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips "); | |
945 | MODULE_LICENSE("GPL"); |