blob: 73b6c8b0e8696db9dbe8a71590bf7b62fb3b5532 [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
Pavan Savoya0cc2f32010-10-06 12:18:14 -04005 * Copyright (C) 2009-2010 Texas Instruments
6 * Author: Pavan Savoy <pavan_savoy@ti.com>
Pavan Savoyd0088ce2010-04-08 13:16:54 -05007 *
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>
Pavan Savoyc1afac12010-07-28 02:25:59 -050030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050032#include <linux/sched.h>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040033#include <linux/rfkill.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050034
Pavan Savoyd0088ce2010-04-08 13:16:54 -050035/* 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
Pavan Savoye5558672010-09-30 16:13:30 -040040#include <linux/ti_wilink_st.h>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040041
Pavan Savoyd0088ce2010-04-08 13:16:54 -050042
43static int kim_probe(struct platform_device *pdev);
44static int kim_remove(struct platform_device *pdev);
45
46/* KIM platform device driver structure */
47static struct platform_driver kim_platform_driver = {
48 .probe = kim_probe,
49 .remove = kim_remove,
50 /* TODO: ST driver power management during suspend/resume ?
51 */
52#if 0
53 .suspend = kim_suspend,
54 .resume = kim_resume,
55#endif
56 .driver = {
57 .name = "kim",
58 .owner = THIS_MODULE,
59 },
60};
61
Pavan Savoyd0088ce2010-04-08 13:16:54 -050062static int kim_toggle_radio(void*, bool);
63static const struct rfkill_ops kim_rfkill_ops = {
64 .set_block = kim_toggle_radio,
65};
Pavan Savoyd0088ce2010-04-08 13:16:54 -050066
67/* strings to be used for rfkill entries and by
68 * ST Core to be used for sysfs debug entry
69 */
70#define PROTO_ENTRY(type, name) name
71const unsigned char *protocol_names[] = {
72 PROTO_ENTRY(ST_BT, "Bluetooth"),
73 PROTO_ENTRY(ST_FM, "FM"),
74 PROTO_ENTRY(ST_GPS, "GPS"),
75};
76
Pavan Savoydbd3a872010-08-19 14:08:51 -040077#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
Pavan Savoy73f12e82010-10-12 16:27:38 -040078static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050079
80/**********************************************************************/
81/* internal functions */
82
Pavan Savoy36b5aee2010-07-22 05:32:06 -050083/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040084 * st_get_plat_device -
85 * function which returns the reference to the platform device
86 * requested by id. As of now only 1 such device exists (id=0)
87 * the context requesting for reference can get the id to be
88 * requested by a. The protocol driver which is registering or
89 * b. the tty device which is opened.
90 */
91static struct platform_device *st_get_plat_device(int id)
92{
93 return st_kim_devices[id];
94}
95
96/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050097 * validate_firmware_response -
98 * function to return whether the firmware response was proper
99 * in case of error don't complete so that waiting for proper
100 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500101 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500102void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500103{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500104 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500105 if (unlikely(skb->data[5] != 0)) {
106 pr_err("no proper response during fw download");
107 pr_err("data6 %x", skb->data[5]);
108 return; /* keep waiting for the proper response */
109 }
110 /* becos of all the script being downloaded */
111 complete_all(&kim_gdata->kim_rcvd);
112 kfree_skb(skb);
113}
114
115/* check for data len received inside kim_int_recv
116 * most often hit the last case to update state to waiting for data
117 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500118static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500119{
120 register int room = skb_tailroom(kim_gdata->rx_skb);
121
Pavan Savoye6d9e642010-07-22 05:32:05 -0500122 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500123
124 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500125 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500126 } else if (len > room) {
127 /* Received packet's payload length is larger.
128 * We can't accommodate it in created skb.
129 */
130 pr_err("Data length is too large len %d room %d", len,
131 room);
132 kfree_skb(kim_gdata->rx_skb);
133 } else {
134 /* Packet header has non-zero payload length and
135 * we have enough space in created skb. Lets read
136 * payload data */
137 kim_gdata->rx_state = ST_BT_W4_DATA;
138 kim_gdata->rx_count = len;
139 return len;
140 }
141
142 /* Change ST LL state to continue to process next
143 * packet */
144 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
145 kim_gdata->rx_skb = NULL;
146 kim_gdata->rx_count = 0;
147
148 return 0;
149}
150
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500151/**
152 * kim_int_recv - receive function called during firmware download
153 * firmware download responses on different UART drivers
154 * have been observed to come in bursts of different
155 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500156 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500157void kim_int_recv(struct kim_data_s *kim_gdata,
158 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500159{
Pavan Savoy73f12e82010-10-12 16:27:38 -0400160 const unsigned char *ptr;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500161 struct hci_event_hdr *eh;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400162 int len = 0, type = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500163
Pavan Savoye6d9e642010-07-22 05:32:05 -0500164 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500165 /* Decode received bytes here */
Pavan Savoy73f12e82010-10-12 16:27:38 -0400166 ptr = data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500167 if (unlikely(ptr == NULL)) {
168 pr_err(" received null from TTY ");
169 return;
170 }
Pavan Savoy73f12e82010-10-12 16:27:38 -0400171
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500172 while (count) {
173 if (kim_gdata->rx_count) {
174 len = min_t(unsigned int, kim_gdata->rx_count, count);
175 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
176 kim_gdata->rx_count -= len;
177 count -= len;
178 ptr += len;
179
180 if (kim_gdata->rx_count)
181 continue;
182
183 /* Check ST RX state machine , where are we? */
184 switch (kim_gdata->rx_state) {
185 /* Waiting for complete packet ? */
186 case ST_BT_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500187 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500188 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500189 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
190 kim_gdata->rx_skb = NULL;
191 continue;
192 /* Waiting for Bluetooth event header ? */
193 case ST_BT_W4_EVENT_HDR:
194 eh = (struct hci_event_hdr *)kim_gdata->
195 rx_skb->data;
Pavan Savoye6d9e642010-07-22 05:32:05 -0500196 pr_debug("Event header: evt 0x%2.2x"
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500197 "plen %d", eh->evt, eh->plen);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500198 kim_check_data_len(kim_gdata, eh->plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500199 continue;
200 } /* end of switch */
201 } /* end of if rx_state */
202 switch (*ptr) {
203 /* Bluetooth event packet? */
204 case HCI_EVENT_PKT:
205 pr_info("Event packet");
206 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
207 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
208 type = HCI_EVENT_PKT;
209 break;
210 default:
211 pr_info("unknown packet");
212 ptr++;
213 count--;
214 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500215 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500216 ptr++;
217 count--;
218 kim_gdata->rx_skb =
219 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
220 if (!kim_gdata->rx_skb) {
221 pr_err("can't allocate mem for new packet");
222 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
223 kim_gdata->rx_count = 0;
224 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500225 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500226 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500227 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500228 pr_info("done %s", __func__);
229 return;
230}
231
Pavan Savoy38d9df42010-07-08 08:55:17 -0500232static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500233{
234 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400235 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500236
Pavan Savoye6d9e642010-07-22 05:32:05 -0500237 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500238
239 INIT_COMPLETION(kim_gdata->kim_rcvd);
240 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
241 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy320920c2010-07-14 08:21:12 -0500242 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500243 }
244
245 if (!wait_for_completion_timeout
246 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
247 pr_err(" waiting for ver info- timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500248 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500249 }
250
251 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500252 MAKEWORD(kim_gdata->resp_buffer[13],
253 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500254 chip = (version & 0x7C00) >> 10;
255 min_ver = (version & 0x007F);
256 maj_ver = (version & 0x0380) >> 7;
257
258 if (version & 0x8000)
259 maj_ver |= 0x0008;
260
261 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500262
263 /* to be accessed later via sysfs entry */
264 kim_gdata->version.full = version;
265 kim_gdata->version.chip = chip;
266 kim_gdata->version.maj_ver = maj_ver;
267 kim_gdata->version.min_ver = min_ver;
268
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500269 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500270 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500271}
272
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500273/**
274 * download_firmware -
275 * internal function which parses through the .bts firmware
276 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500277 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500278static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500279{
Pavan Savoy320920c2010-07-14 08:21:12 -0500280 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500281 long len = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400282 unsigned char *ptr = NULL;
283 unsigned char *action_ptr = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500284 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
285
Pavan Savoy38d9df42010-07-08 08:55:17 -0500286 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500287 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500288 pr_err("kim: failed to read local ver");
289 return err;
290 }
291 err =
292 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
293 &kim_gdata->kim_pdev->dev);
294 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
295 (kim_gdata->fw_entry->size == 0))) {
296 pr_err(" request_firmware failed(errno %ld) for %s", err,
297 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500298 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500299 }
300 ptr = (void *)kim_gdata->fw_entry->data;
301 len = kim_gdata->fw_entry->size;
302 /* bts_header to remove out magic number and
303 * version
304 */
305 ptr += sizeof(struct bts_header);
306 len -= sizeof(struct bts_header);
307
308 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500309 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500310 ((struct bts_action *)ptr)->size,
311 ((struct bts_action *)ptr)->type);
312
313 switch (((struct bts_action *)ptr)->type) {
314 case ACTION_SEND_COMMAND: /* action send */
315 action_ptr = &(((struct bts_action *)ptr)->data[0]);
316 if (unlikely
317 (((struct hci_command *)action_ptr)->opcode ==
318 0xFF36)) {
319 /* ignore remote change
320 * baud rate HCI VS command */
321 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500322 (" change remote baud"
323 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500324 break;
325 }
326
327 INIT_COMPLETION(kim_gdata->kim_rcvd);
328 err = st_int_write(kim_gdata->core_data,
329 ((struct bts_action_send *)action_ptr)->data,
330 ((struct bts_action *)ptr)->size);
331 if (unlikely(err < 0)) {
332 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500333 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500334 }
335 if (!wait_for_completion_timeout
336 (&kim_gdata->kim_rcvd,
337 msecs_to_jiffies(CMD_RESP_TIME))) {
338 pr_err
339 (" response timeout during fw download ");
340 /* timed out */
341 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500342 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500343 }
344 break;
345 case ACTION_DELAY: /* sleep */
346 pr_info("sleep command in scr");
347 action_ptr = &(((struct bts_action *)ptr)->data[0]);
348 mdelay(((struct bts_action_delay *)action_ptr)->msec);
349 break;
350 }
351 len =
352 len - (sizeof(struct bts_action) +
353 ((struct bts_action *)ptr)->size);
354 ptr =
355 ptr + sizeof(struct bts_action) +
356 ((struct bts_action *)ptr)->size;
357 }
358 /* fw download complete */
359 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500360 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500361}
362
363/**********************************************************************/
364/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500365/* function to toggle the GPIO
366 * needs to know whether the GPIO is active high or active low
367 */
368void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
369{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500370 struct platform_device *kim_pdev;
371 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500372 pr_info(" %s ", __func__);
373
Pavan Savoydbd3a872010-08-19 14:08:51 -0400374 kim_pdev = st_get_plat_device(0);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500375 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
376
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500377 if (kim_gdata->gpios[type] == -1) {
378 pr_info(" gpio not requested for protocol %s",
379 protocol_names[type]);
380 return;
381 }
382 switch (type) {
383 case ST_BT:
384 /*Do Nothing */
385 break;
386
387 case ST_FM:
388 if (state == KIM_GPIO_ACTIVE)
389 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
390 else
391 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
392 break;
393
394 case ST_GPS:
395 if (state == KIM_GPIO_ACTIVE)
396 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
397 else
398 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
399 break;
400
401 case ST_MAX:
402 default:
403 break;
404 }
405
406 return;
407}
408
409/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
410 * can be because of
411 * 1. response to read local version
412 * 2. during send/recv's of firmware download
413 */
414void st_kim_recv(void *disc_data, const unsigned char *data, long count)
415{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500416 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
417 struct kim_data_s *kim_gdata = st_gdata->kim_data;
418
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500419 pr_info(" %s ", __func__);
420 /* copy to local buffer */
421 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
422 /* must be the read_ver_cmd */
423 memcpy(kim_gdata->resp_buffer, data, count);
424 complete_all(&kim_gdata->kim_rcvd);
425 return;
426 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500427 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500428 /* either completes or times out */
429 }
430 return;
431}
432
433/* to signal completion of line discipline installation
434 * called from ST Core, upon tty_open
435 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500436void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500437{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500438 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500439 complete(&kim_gdata->ldisc_installed);
440}
441
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500442/**
443 * st_kim_start - called from ST Core upon 1st registration
444 * This involves toggling the chip enable gpio, reading
445 * the firmware version from chip, forming the fw file name
446 * based on the chip version, requesting the fw, parsing it
447 * and perform download(send/recv).
448 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500449long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500450{
Pavan Savoy320920c2010-07-14 08:21:12 -0500451 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500452 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500453 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
454
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500455 pr_info(" %s", __func__);
456
457 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500458 /* TODO: this is only because rfkill sub-system
459 * doesn't send events to user-space if the state
460 * isn't changed
461 */
462 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500463 /* Configure BT nShutdown to HIGH state */
464 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
465 mdelay(5); /* FIXME: a proper toggle */
466 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
467 mdelay(100);
468 /* re-initialize the completion */
469 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500470#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500471 /* send signal to UIM */
472 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
473 if (err != 0) {
474 pr_info(" sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500475 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500476 continue;
477 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500478#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500479 /* unblock and send event to UIM via /dev/rfkill */
480 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500481 /* wait for ldisc to be installed */
482 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
483 msecs_to_jiffies(LDISC_TIME));
484 if (!err) { /* timeout */
485 pr_err("line disc installation timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500486 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500487 continue;
488 } else {
489 /* ldisc installed now */
490 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500491 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500492 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500493 pr_err("download firmware failed");
494 continue;
495 } else { /* on success don't retry */
496 break;
497 }
498 }
499 } while (retry--);
500 return err;
501}
502
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500503/**
504 * st_kim_stop - called from ST Core, on the last un-registration
505 * toggle low the chip enable gpio
506 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500507long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500508{
Pavan Savoy320920c2010-07-14 08:21:12 -0500509 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500510 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500511
512 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500513#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500514 /* send signal to UIM */
515 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
516 if (err != 0) {
517 pr_err("sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500518 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500519 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500520#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500521 /* set BT rfkill to be blocked */
522 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500523
524 /* wait for ldisc to be un-installed */
525 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
526 msecs_to_jiffies(LDISC_TIME));
527 if (!err) { /* timeout */
528 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500529 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500530 }
531
532 /* By default configure BT nShutdown to LOW state */
533 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
534 mdelay(1);
535 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
536 mdelay(1);
537 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
538 return err;
539}
540
541/**********************************************************************/
542/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500543/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500544
Pavan Savoyc1afac12010-07-28 02:25:59 -0500545static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500546{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500547 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
548 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500549 kim_gdata->version.chip, kim_gdata->version.maj_ver,
550 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500551 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500552}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500553
Pavan Savoyc1afac12010-07-28 02:25:59 -0500554static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500555{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500556 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
557 kim_st_list_protocols(kim_gdata->core_data, s);
558 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500559}
560
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500561/* function called from rfkill subsystem, when someone from
562 * user space would write 0/1 on the sysfs entry
563 * /sys/class/rfkill/rfkill0,1,3/state
564 */
565static int kim_toggle_radio(void *data, bool blocked)
566{
567 enum proto_type type = *((enum proto_type *)data);
Pavan Savoye6d9e642010-07-22 05:32:05 -0500568 pr_debug(" %s: %d ", __func__, type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500569
570 switch (type) {
571 case ST_BT:
572 /* do nothing */
573 break;
574 case ST_FM:
575 case ST_GPS:
576 if (blocked)
577 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
578 else
579 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
580 break;
581 case ST_MAX:
582 pr_err(" wrong proto type ");
583 break;
584 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500585 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500586}
587
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500588/**
589 * st_kim_ref - reference the core's data
590 * This references the per-ST platform device in the arch/xx/
591 * board-xx.c file.
592 * This would enable multiple such platform devices to exist
593 * on a given platform
594 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400595void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500596{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500597 struct platform_device *pdev;
598 struct kim_data_s *kim_gdata;
599 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400600 pdev = st_get_plat_device(id);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500601 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500602 *core_data = kim_gdata->core_data;
603}
604
Pavan Savoyc1afac12010-07-28 02:25:59 -0500605static int kim_version_open(struct inode *i, struct file *f)
606{
607 return single_open(f, show_version, i->i_private);
608}
609
610static int kim_list_open(struct inode *i, struct file *f)
611{
612 return single_open(f, show_list, i->i_private);
613}
614
615static const struct file_operations version_debugfs_fops = {
616 /* version info */
617 .open = kim_version_open,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = single_release,
621};
622static const struct file_operations list_debugfs_fops = {
623 /* protocols info */
624 .open = kim_list_open,
625 .read = seq_read,
626 .llseek = seq_lseek,
627 .release = single_release,
628};
629
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500630/**********************************************************************/
631/* functions called from platform device driver subsystem
632 * need to have a relevant platform device entry in the platform's
633 * board-*.c file
634 */
635
Pavan Savoyc1afac12010-07-28 02:25:59 -0500636struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500637static int kim_probe(struct platform_device *pdev)
638{
639 long status;
640 long proto;
641 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500642 struct kim_data_s *kim_gdata;
643
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400644 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
645 /* multiple devices could exist */
646 st_kim_devices[pdev->id] = pdev;
647 } else {
648 /* platform's sure about existance of 1 device */
649 st_kim_devices[0] = pdev;
650 }
651
Pavan Savoy38d9df42010-07-08 08:55:17 -0500652 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
653 if (!kim_gdata) {
654 pr_err("no mem to allocate");
655 return -ENOMEM;
656 }
657 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500658
659 status = st_core_init(&kim_gdata->core_data);
660 if (status != 0) {
661 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500662 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500663 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500664 /* refer to itself */
665 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500666
667 for (proto = 0; proto < ST_MAX; proto++) {
668 kim_gdata->gpios[proto] = gpios[proto];
669 pr_info(" %ld gpio to be requested", gpios[proto]);
670 }
671
672 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
673 /* Claim the Bluetooth/FM/GPIO
674 * nShutdown gpio from the system
675 */
676 status = gpio_request(gpios[proto], "kim");
677 if (unlikely(status)) {
678 pr_err(" gpio %ld request failed ", gpios[proto]);
679 proto -= 1;
680 while (proto >= 0) {
681 if (gpios[proto] != -1)
682 gpio_free(gpios[proto]);
683 }
684 return status;
685 }
686
687 /* Configure nShutdown GPIO as output=0 */
688 status =
689 gpio_direction_output(gpios[proto], 0);
690 if (unlikely(status)) {
691 pr_err(" unable to configure gpio %ld",
692 gpios[proto]);
693 proto -= 1;
694 while (proto >= 0) {
695 if (gpios[proto] != -1)
696 gpio_free(gpios[proto]);
697 }
698 return status;
699 }
700 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500701 /* get reference of pdev for request_firmware
702 */
703 kim_gdata->kim_pdev = pdev;
704 init_completion(&kim_gdata->kim_rcvd);
705 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500706
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500707 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
708 /* TODO: should all types be rfkill_type_bt ? */
709 kim_gdata->rf_protos[proto] = proto;
710 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
711 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
712 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
713 if (kim_gdata->rfkill[proto] == NULL) {
714 pr_err("cannot create rfkill entry for gpio %ld",
715 gpios[proto]);
716 continue;
717 }
718 /* block upon creation */
719 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
720 status = rfkill_register(kim_gdata->rfkill[proto]);
721 if (unlikely(status)) {
722 pr_err("rfkill registration failed for gpio %ld",
723 gpios[proto]);
724 rfkill_unregister(kim_gdata->rfkill[proto]);
725 continue;
726 }
727 pr_info("rfkill entry created for %ld", gpios[proto]);
728 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500729
Pavan Savoyc1afac12010-07-28 02:25:59 -0500730 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
731 if (IS_ERR(kim_debugfs_dir)) {
732 pr_err(" debugfs entries creation failed ");
733 kim_debugfs_dir = NULL;
Naveen Jaine2a53282010-06-09 03:45:33 -0500734 return -1;
735 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500736
737 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
738 kim_gdata, &version_debugfs_fops);
739 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
740 kim_gdata, &list_debugfs_fops);
741 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500742 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500743}
744
745static int kim_remove(struct platform_device *pdev)
746{
747 /* free the GPIOs requested
748 */
749 long *gpios = pdev->dev.platform_data;
750 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500751 struct kim_data_s *kim_gdata;
752
753 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500754
755 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
756 /* Claim the Bluetooth/FM/GPIO
757 * nShutdown gpio from the system
758 */
759 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500760 rfkill_unregister(kim_gdata->rfkill[proto]);
761 rfkill_destroy(kim_gdata->rfkill[proto]);
762 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500763 }
764 pr_info("kim: GPIO Freed");
Pavan Savoyc1afac12010-07-28 02:25:59 -0500765 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500766 kim_gdata->kim_pdev = NULL;
767 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500768
769 kfree(kim_gdata);
770 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500771 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500772}
773
774/**********************************************************************/
775/* entry point for ST KIM module, called in from ST Core */
776
777static int __init st_kim_init(void)
778{
Pavan Savoy320920c2010-07-14 08:21:12 -0500779 long ret = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500780 ret = platform_driver_register(&kim_platform_driver);
781 if (ret != 0) {
782 pr_err("platform drv registration failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500783 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500784 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500785 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500786}
787
788static void __exit st_kim_deinit(void)
789{
790 /* the following returns void */
791 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500792}
793
794
795module_init(st_kim_init);
796module_exit(st_kim_deinit);
797MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
798MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
799MODULE_LICENSE("GPL");