blob: be18c7a174461fd3f3c37052dae4f1fde08cf96a [file] [log] [blame]
Pavan Savoyd0088ce2010-04-08 13:16:54 -05001/*
2 * Shared Transport Line discipline driver Core
3 * Init Manager module responsible for GPIO control
4 * and firmware download
5 * Copyright (C) 2009 Texas Instruments
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#define pr_fmt(fmt) "(stk) :" fmt
23#include <linux/platform_device.h>
24#include <linux/jiffies.h>
25#include <linux/firmware.h>
26#include <linux/delay.h>
27#include <linux/wait.h>
28#include <linux/gpio.h>
Pavan Savoyc1afac12010-07-28 02:25:59 -050029#include <linux/debugfs.h>
30#include <linux/seq_file.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050031
32#include <linux/sched.h>
33
34#include "st_kim.h"
35/* understand BT events for fw response */
36#include <net/bluetooth/bluetooth.h>
37#include <net/bluetooth/hci_core.h>
38#include <net/bluetooth/hci.h>
39
40
41static int kim_probe(struct platform_device *pdev);
42static int kim_remove(struct platform_device *pdev);
43
44/* KIM platform device driver structure */
45static struct platform_driver kim_platform_driver = {
46 .probe = kim_probe,
47 .remove = kim_remove,
48 /* TODO: ST driver power management during suspend/resume ?
49 */
50#if 0
51 .suspend = kim_suspend,
52 .resume = kim_resume,
53#endif
54 .driver = {
55 .name = "kim",
56 .owner = THIS_MODULE,
57 },
58};
59
Pavan Savoyd0088ce2010-04-08 13:16:54 -050060static int kim_toggle_radio(void*, bool);
61static const struct rfkill_ops kim_rfkill_ops = {
62 .set_block = kim_toggle_radio,
63};
Pavan Savoyd0088ce2010-04-08 13:16:54 -050064
65/* strings to be used for rfkill entries and by
66 * ST Core to be used for sysfs debug entry
67 */
68#define PROTO_ENTRY(type, name) name
69const unsigned char *protocol_names[] = {
70 PROTO_ENTRY(ST_BT, "Bluetooth"),
71 PROTO_ENTRY(ST_FM, "FM"),
72 PROTO_ENTRY(ST_GPS, "GPS"),
73};
74
Pavan Savoydbd3a872010-08-19 14:08:51 -040075#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
76struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050077
78/**********************************************************************/
79/* internal functions */
80
Pavan Savoy36b5aee2010-07-22 05:32:06 -050081/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040082 * st_get_plat_device -
83 * function which returns the reference to the platform device
84 * requested by id. As of now only 1 such device exists (id=0)
85 * the context requesting for reference can get the id to be
86 * requested by a. The protocol driver which is registering or
87 * b. the tty device which is opened.
88 */
89static struct platform_device *st_get_plat_device(int id)
90{
91 return st_kim_devices[id];
92}
93
94/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050095 * validate_firmware_response -
96 * function to return whether the firmware response was proper
97 * in case of error don't complete so that waiting for proper
98 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -050099 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500100void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500101{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500102 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500103 if (unlikely(skb->data[5] != 0)) {
104 pr_err("no proper response during fw download");
105 pr_err("data6 %x", skb->data[5]);
106 return; /* keep waiting for the proper response */
107 }
108 /* becos of all the script being downloaded */
109 complete_all(&kim_gdata->kim_rcvd);
110 kfree_skb(skb);
111}
112
113/* check for data len received inside kim_int_recv
114 * most often hit the last case to update state to waiting for data
115 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500116static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500117{
118 register int room = skb_tailroom(kim_gdata->rx_skb);
119
Pavan Savoye6d9e642010-07-22 05:32:05 -0500120 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500121
122 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500123 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500124 } else if (len > room) {
125 /* Received packet's payload length is larger.
126 * We can't accommodate it in created skb.
127 */
128 pr_err("Data length is too large len %d room %d", len,
129 room);
130 kfree_skb(kim_gdata->rx_skb);
131 } else {
132 /* Packet header has non-zero payload length and
133 * we have enough space in created skb. Lets read
134 * payload data */
135 kim_gdata->rx_state = ST_BT_W4_DATA;
136 kim_gdata->rx_count = len;
137 return len;
138 }
139
140 /* Change ST LL state to continue to process next
141 * packet */
142 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
143 kim_gdata->rx_skb = NULL;
144 kim_gdata->rx_count = 0;
145
146 return 0;
147}
148
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500149/**
150 * kim_int_recv - receive function called during firmware download
151 * firmware download responses on different UART drivers
152 * have been observed to come in bursts of different
153 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500154 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500155void kim_int_recv(struct kim_data_s *kim_gdata,
156 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500157{
158 register char *ptr;
159 struct hci_event_hdr *eh;
160 register int len = 0, type = 0;
161
Pavan Savoye6d9e642010-07-22 05:32:05 -0500162 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500163 /* Decode received bytes here */
164 ptr = (char *)data;
165 if (unlikely(ptr == NULL)) {
166 pr_err(" received null from TTY ");
167 return;
168 }
169 while (count) {
170 if (kim_gdata->rx_count) {
171 len = min_t(unsigned int, kim_gdata->rx_count, count);
172 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
173 kim_gdata->rx_count -= len;
174 count -= len;
175 ptr += len;
176
177 if (kim_gdata->rx_count)
178 continue;
179
180 /* Check ST RX state machine , where are we? */
181 switch (kim_gdata->rx_state) {
182 /* Waiting for complete packet ? */
183 case ST_BT_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500184 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500185 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500186 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
187 kim_gdata->rx_skb = NULL;
188 continue;
189 /* Waiting for Bluetooth event header ? */
190 case ST_BT_W4_EVENT_HDR:
191 eh = (struct hci_event_hdr *)kim_gdata->
192 rx_skb->data;
Pavan Savoye6d9e642010-07-22 05:32:05 -0500193 pr_debug("Event header: evt 0x%2.2x"
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500194 "plen %d", eh->evt, eh->plen);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500195 kim_check_data_len(kim_gdata, eh->plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500196 continue;
197 } /* end of switch */
198 } /* end of if rx_state */
199 switch (*ptr) {
200 /* Bluetooth event packet? */
201 case HCI_EVENT_PKT:
202 pr_info("Event packet");
203 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
204 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
205 type = HCI_EVENT_PKT;
206 break;
207 default:
208 pr_info("unknown packet");
209 ptr++;
210 count--;
211 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500212 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500213 ptr++;
214 count--;
215 kim_gdata->rx_skb =
216 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
217 if (!kim_gdata->rx_skb) {
218 pr_err("can't allocate mem for new packet");
219 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
220 kim_gdata->rx_count = 0;
221 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500222 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500223 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500224 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500225 pr_info("done %s", __func__);
226 return;
227}
228
Pavan Savoy38d9df42010-07-08 08:55:17 -0500229static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500230{
231 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
232 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
233
Pavan Savoye6d9e642010-07-22 05:32:05 -0500234 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500235
236 INIT_COMPLETION(kim_gdata->kim_rcvd);
237 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
238 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy320920c2010-07-14 08:21:12 -0500239 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500240 }
241
242 if (!wait_for_completion_timeout
243 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
244 pr_err(" waiting for ver info- timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500245 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500246 }
247
248 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500249 MAKEWORD(kim_gdata->resp_buffer[13],
250 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500251 chip = (version & 0x7C00) >> 10;
252 min_ver = (version & 0x007F);
253 maj_ver = (version & 0x0380) >> 7;
254
255 if (version & 0x8000)
256 maj_ver |= 0x0008;
257
258 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500259
260 /* to be accessed later via sysfs entry */
261 kim_gdata->version.full = version;
262 kim_gdata->version.chip = chip;
263 kim_gdata->version.maj_ver = maj_ver;
264 kim_gdata->version.min_ver = min_ver;
265
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500266 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500267 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500268}
269
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500270/**
271 * download_firmware -
272 * internal function which parses through the .bts firmware
273 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500274 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500275static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500276{
Pavan Savoy320920c2010-07-14 08:21:12 -0500277 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500278 long len = 0;
279 register unsigned char *ptr = NULL;
280 register unsigned char *action_ptr = NULL;
281 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
282
Pavan Savoy38d9df42010-07-08 08:55:17 -0500283 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500284 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500285 pr_err("kim: failed to read local ver");
286 return err;
287 }
288 err =
289 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
290 &kim_gdata->kim_pdev->dev);
291 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
292 (kim_gdata->fw_entry->size == 0))) {
293 pr_err(" request_firmware failed(errno %ld) for %s", err,
294 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500295 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500296 }
297 ptr = (void *)kim_gdata->fw_entry->data;
298 len = kim_gdata->fw_entry->size;
299 /* bts_header to remove out magic number and
300 * version
301 */
302 ptr += sizeof(struct bts_header);
303 len -= sizeof(struct bts_header);
304
305 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500306 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500307 ((struct bts_action *)ptr)->size,
308 ((struct bts_action *)ptr)->type);
309
310 switch (((struct bts_action *)ptr)->type) {
311 case ACTION_SEND_COMMAND: /* action send */
312 action_ptr = &(((struct bts_action *)ptr)->data[0]);
313 if (unlikely
314 (((struct hci_command *)action_ptr)->opcode ==
315 0xFF36)) {
316 /* ignore remote change
317 * baud rate HCI VS command */
318 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500319 (" change remote baud"
320 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500321 break;
322 }
323
324 INIT_COMPLETION(kim_gdata->kim_rcvd);
325 err = st_int_write(kim_gdata->core_data,
326 ((struct bts_action_send *)action_ptr)->data,
327 ((struct bts_action *)ptr)->size);
328 if (unlikely(err < 0)) {
329 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500330 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500331 }
332 if (!wait_for_completion_timeout
333 (&kim_gdata->kim_rcvd,
334 msecs_to_jiffies(CMD_RESP_TIME))) {
335 pr_err
336 (" response timeout during fw download ");
337 /* timed out */
338 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500339 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500340 }
341 break;
342 case ACTION_DELAY: /* sleep */
343 pr_info("sleep command in scr");
344 action_ptr = &(((struct bts_action *)ptr)->data[0]);
345 mdelay(((struct bts_action_delay *)action_ptr)->msec);
346 break;
347 }
348 len =
349 len - (sizeof(struct bts_action) +
350 ((struct bts_action *)ptr)->size);
351 ptr =
352 ptr + sizeof(struct bts_action) +
353 ((struct bts_action *)ptr)->size;
354 }
355 /* fw download complete */
356 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500357 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500358}
359
360/**********************************************************************/
361/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500362/* function to toggle the GPIO
363 * needs to know whether the GPIO is active high or active low
364 */
365void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
366{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500367 struct platform_device *kim_pdev;
368 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500369 pr_info(" %s ", __func__);
370
Pavan Savoydbd3a872010-08-19 14:08:51 -0400371 kim_pdev = st_get_plat_device(0);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500372 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
373
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500374 if (kim_gdata->gpios[type] == -1) {
375 pr_info(" gpio not requested for protocol %s",
376 protocol_names[type]);
377 return;
378 }
379 switch (type) {
380 case ST_BT:
381 /*Do Nothing */
382 break;
383
384 case ST_FM:
385 if (state == KIM_GPIO_ACTIVE)
386 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
387 else
388 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
389 break;
390
391 case ST_GPS:
392 if (state == KIM_GPIO_ACTIVE)
393 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
394 else
395 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
396 break;
397
398 case ST_MAX:
399 default:
400 break;
401 }
402
403 return;
404}
405
406/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
407 * can be because of
408 * 1. response to read local version
409 * 2. during send/recv's of firmware download
410 */
411void st_kim_recv(void *disc_data, const unsigned char *data, long count)
412{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500413 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
414 struct kim_data_s *kim_gdata = st_gdata->kim_data;
415
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500416 pr_info(" %s ", __func__);
417 /* copy to local buffer */
418 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
419 /* must be the read_ver_cmd */
420 memcpy(kim_gdata->resp_buffer, data, count);
421 complete_all(&kim_gdata->kim_rcvd);
422 return;
423 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500424 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500425 /* either completes or times out */
426 }
427 return;
428}
429
430/* to signal completion of line discipline installation
431 * called from ST Core, upon tty_open
432 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500433void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500434{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500435 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500436 complete(&kim_gdata->ldisc_installed);
437}
438
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500439/**
440 * st_kim_start - called from ST Core upon 1st registration
441 * This involves toggling the chip enable gpio, reading
442 * the firmware version from chip, forming the fw file name
443 * based on the chip version, requesting the fw, parsing it
444 * and perform download(send/recv).
445 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500446long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500447{
Pavan Savoy320920c2010-07-14 08:21:12 -0500448 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500449 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500450 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
451
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500452 pr_info(" %s", __func__);
453
454 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500455 /* TODO: this is only because rfkill sub-system
456 * doesn't send events to user-space if the state
457 * isn't changed
458 */
459 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500460 /* Configure BT nShutdown to HIGH state */
461 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
462 mdelay(5); /* FIXME: a proper toggle */
463 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
464 mdelay(100);
465 /* re-initialize the completion */
466 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500467#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500468 /* send signal to UIM */
469 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
470 if (err != 0) {
471 pr_info(" sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500472 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500473 continue;
474 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500475#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500476 /* unblock and send event to UIM via /dev/rfkill */
477 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500478 /* wait for ldisc to be installed */
479 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
480 msecs_to_jiffies(LDISC_TIME));
481 if (!err) { /* timeout */
482 pr_err("line disc installation timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500483 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500484 continue;
485 } else {
486 /* ldisc installed now */
487 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500488 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500489 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500490 pr_err("download firmware failed");
491 continue;
492 } else { /* on success don't retry */
493 break;
494 }
495 }
496 } while (retry--);
497 return err;
498}
499
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500500/**
501 * st_kim_stop - called from ST Core, on the last un-registration
502 * toggle low the chip enable gpio
503 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500504long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500505{
Pavan Savoy320920c2010-07-14 08:21:12 -0500506 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500507 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500508
509 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500510#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500511 /* send signal to UIM */
512 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
513 if (err != 0) {
514 pr_err("sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500515 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500516 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500517#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500518 /* set BT rfkill to be blocked */
519 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500520
521 /* wait for ldisc to be un-installed */
522 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
523 msecs_to_jiffies(LDISC_TIME));
524 if (!err) { /* timeout */
525 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500526 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500527 }
528
529 /* By default configure BT nShutdown to LOW state */
530 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
531 mdelay(1);
532 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
533 mdelay(1);
534 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
535 return err;
536}
537
538/**********************************************************************/
539/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500540/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500541
Pavan Savoyc1afac12010-07-28 02:25:59 -0500542static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500543{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500544 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
545 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500546 kim_gdata->version.chip, kim_gdata->version.maj_ver,
547 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500548 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500549}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500550
Pavan Savoyc1afac12010-07-28 02:25:59 -0500551static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500552{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500553 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
554 kim_st_list_protocols(kim_gdata->core_data, s);
555 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500556}
557
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500558/* function called from rfkill subsystem, when someone from
559 * user space would write 0/1 on the sysfs entry
560 * /sys/class/rfkill/rfkill0,1,3/state
561 */
562static int kim_toggle_radio(void *data, bool blocked)
563{
564 enum proto_type type = *((enum proto_type *)data);
Pavan Savoye6d9e642010-07-22 05:32:05 -0500565 pr_debug(" %s: %d ", __func__, type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500566
567 switch (type) {
568 case ST_BT:
569 /* do nothing */
570 break;
571 case ST_FM:
572 case ST_GPS:
573 if (blocked)
574 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
575 else
576 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
577 break;
578 case ST_MAX:
579 pr_err(" wrong proto type ");
580 break;
581 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500582 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500583}
584
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500585/**
586 * st_kim_ref - reference the core's data
587 * This references the per-ST platform device in the arch/xx/
588 * board-xx.c file.
589 * This would enable multiple such platform devices to exist
590 * on a given platform
591 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400592void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500593{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500594 struct platform_device *pdev;
595 struct kim_data_s *kim_gdata;
596 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400597 pdev = st_get_plat_device(id);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500598 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500599 *core_data = kim_gdata->core_data;
600}
601
Pavan Savoyc1afac12010-07-28 02:25:59 -0500602static int kim_version_open(struct inode *i, struct file *f)
603{
604 return single_open(f, show_version, i->i_private);
605}
606
607static int kim_list_open(struct inode *i, struct file *f)
608{
609 return single_open(f, show_list, i->i_private);
610}
611
612static const struct file_operations version_debugfs_fops = {
613 /* version info */
614 .open = kim_version_open,
615 .read = seq_read,
616 .llseek = seq_lseek,
617 .release = single_release,
618};
619static const struct file_operations list_debugfs_fops = {
620 /* protocols info */
621 .open = kim_list_open,
622 .read = seq_read,
623 .llseek = seq_lseek,
624 .release = single_release,
625};
626
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500627/**********************************************************************/
628/* functions called from platform device driver subsystem
629 * need to have a relevant platform device entry in the platform's
630 * board-*.c file
631 */
632
Pavan Savoyc1afac12010-07-28 02:25:59 -0500633struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500634static int kim_probe(struct platform_device *pdev)
635{
636 long status;
637 long proto;
638 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500639 struct kim_data_s *kim_gdata;
640
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400641 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
642 /* multiple devices could exist */
643 st_kim_devices[pdev->id] = pdev;
644 } else {
645 /* platform's sure about existance of 1 device */
646 st_kim_devices[0] = pdev;
647 }
648
Pavan Savoy38d9df42010-07-08 08:55:17 -0500649 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
650 if (!kim_gdata) {
651 pr_err("no mem to allocate");
652 return -ENOMEM;
653 }
654 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500655
656 status = st_core_init(&kim_gdata->core_data);
657 if (status != 0) {
658 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500659 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500660 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500661 /* refer to itself */
662 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500663
664 for (proto = 0; proto < ST_MAX; proto++) {
665 kim_gdata->gpios[proto] = gpios[proto];
666 pr_info(" %ld gpio to be requested", gpios[proto]);
667 }
668
669 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
670 /* Claim the Bluetooth/FM/GPIO
671 * nShutdown gpio from the system
672 */
673 status = gpio_request(gpios[proto], "kim");
674 if (unlikely(status)) {
675 pr_err(" gpio %ld request failed ", gpios[proto]);
676 proto -= 1;
677 while (proto >= 0) {
678 if (gpios[proto] != -1)
679 gpio_free(gpios[proto]);
680 }
681 return status;
682 }
683
684 /* Configure nShutdown GPIO as output=0 */
685 status =
686 gpio_direction_output(gpios[proto], 0);
687 if (unlikely(status)) {
688 pr_err(" unable to configure gpio %ld",
689 gpios[proto]);
690 proto -= 1;
691 while (proto >= 0) {
692 if (gpios[proto] != -1)
693 gpio_free(gpios[proto]);
694 }
695 return status;
696 }
697 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500698 /* get reference of pdev for request_firmware
699 */
700 kim_gdata->kim_pdev = pdev;
701 init_completion(&kim_gdata->kim_rcvd);
702 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500703
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500704 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
705 /* TODO: should all types be rfkill_type_bt ? */
706 kim_gdata->rf_protos[proto] = proto;
707 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
708 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
709 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
710 if (kim_gdata->rfkill[proto] == NULL) {
711 pr_err("cannot create rfkill entry for gpio %ld",
712 gpios[proto]);
713 continue;
714 }
715 /* block upon creation */
716 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
717 status = rfkill_register(kim_gdata->rfkill[proto]);
718 if (unlikely(status)) {
719 pr_err("rfkill registration failed for gpio %ld",
720 gpios[proto]);
721 rfkill_unregister(kim_gdata->rfkill[proto]);
722 continue;
723 }
724 pr_info("rfkill entry created for %ld", gpios[proto]);
725 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500726
Pavan Savoyc1afac12010-07-28 02:25:59 -0500727 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
728 if (IS_ERR(kim_debugfs_dir)) {
729 pr_err(" debugfs entries creation failed ");
730 kim_debugfs_dir = NULL;
Naveen Jaine2a53282010-06-09 03:45:33 -0500731 return -1;
732 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500733
734 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
735 kim_gdata, &version_debugfs_fops);
736 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
737 kim_gdata, &list_debugfs_fops);
738 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500739 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500740}
741
742static int kim_remove(struct platform_device *pdev)
743{
744 /* free the GPIOs requested
745 */
746 long *gpios = pdev->dev.platform_data;
747 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500748 struct kim_data_s *kim_gdata;
749
750 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500751
752 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
753 /* Claim the Bluetooth/FM/GPIO
754 * nShutdown gpio from the system
755 */
756 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500757 rfkill_unregister(kim_gdata->rfkill[proto]);
758 rfkill_destroy(kim_gdata->rfkill[proto]);
759 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500760 }
761 pr_info("kim: GPIO Freed");
Pavan Savoyc1afac12010-07-28 02:25:59 -0500762 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500763 kim_gdata->kim_pdev = NULL;
764 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500765
766 kfree(kim_gdata);
767 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500768 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500769}
770
771/**********************************************************************/
772/* entry point for ST KIM module, called in from ST Core */
773
774static int __init st_kim_init(void)
775{
Pavan Savoy320920c2010-07-14 08:21:12 -0500776 long ret = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500777 ret = platform_driver_register(&kim_platform_driver);
778 if (ret != 0) {
779 pr_err("platform drv registration failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500780 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500781 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500782 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500783}
784
785static void __exit st_kim_deinit(void)
786{
787 /* the following returns void */
788 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500789}
790
791
792module_init(st_kim_init);
793module_exit(st_kim_deinit);
794MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
795MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
796MODULE_LICENSE("GPL");